FreeBSD/sys/dev/mrsas/mrsas_ioctl.c
/*
 * mrsas_alloc_mfi_cmds:	Allocates the command packets
 * input:					Adapter instance soft state
 *
 * Each IOCTL or passthru command that is issued to the FW are wrapped in a
 * local data structure called mrsas_mfi_cmd.  The frame embedded in this
 * mrsas_mfi is issued to FW. The array is used only to look up the
 * mrsas_mfi_cmd given the context. The free commands are maintained in a
 * linked list.
 */
Allocates the command packets
input:                                       Adapter instance soft state

Each IOCTL or passthru command that is issued to the FW are wrapped in a
local data structure called mrsas_mfi_cmd.  The frame embedded in this
mrsas_mfi is issued to FW. The array is used only to look up the
mrsas_mfi_cmd given the context. The free commands are maintained in a
linked list.
Allocates the command packets input : Adapter instance soft state Each IOCTL or passthru command that is issued to the FW are wrapped in a local data structure called mrsas_mfi_cmd . The frame embedded in this mrsas_mfi is issued to FW . The array is used only to look up the mrsas_mfi_cmd given the context . The free commands are maintained in a linked list .
int
mrsas_alloc_mfi_cmds(struct mrsas_softc *sc)
{
	int i, j;
	u_int32_t max_cmd;
	struct mrsas_mfi_cmd *cmd;
	max_cmd = MRSAS_MAX_MFI_CMDS;
	sc->mfi_cmd_list = malloc(sizeof(struct mrsas_mfi_cmd *) * max_cmd, M_MRSAS, M_NOWAIT);
	if (!sc->mfi_cmd_list) {
		device_printf(sc->mrsas_dev, "Cannot alloc memory for mfi_cmd cmd_list.\n");
		return (ENOMEM);
	}
	memset(sc->mfi_cmd_list, 0, sizeof(struct mrsas_mfi_cmd *) * max_cmd);
	for (i = 0; i < max_cmd; i++) {
		sc->mfi_cmd_list[i] = malloc(sizeof(struct mrsas_mfi_cmd),
		    M_MRSAS, M_NOWAIT);
		if (!sc->mfi_cmd_list[i]) {
			for (j = 0; j < i; j++)
				free(sc->mfi_cmd_list[j], M_MRSAS);
			free(sc->mfi_cmd_list, M_MRSAS);
			sc->mfi_cmd_list = NULL;
			return (ENOMEM);
		}
	}
	for (i = 0; i < max_cmd; i++) {
		cmd = sc->mfi_cmd_list[i];
		memset(cmd, 0, sizeof(struct mrsas_mfi_cmd));
		cmd->index = i;
		cmd->ccb_ptr = NULL;
		cmd->sc = sc;
		TAILQ_INSERT_TAIL(&(sc->mrsas_mfi_cmd_list_head), cmd, next);
	}
	if (mrsas_create_frame_pool(sc)) {
		device_printf(sc->mrsas_dev, "Cannot allocate DMA frame pool.\n");
		for (i = 0; i < MRSAS_MAX_MFI_CMDS; i++) {
			cmd = sc->mfi_cmd_list[i];
			mrsas_free_frame(sc, cmd);
		}
		if (sc->mficmd_frame_tag != NULL)
			bus_dma_tag_destroy(sc->mficmd_frame_tag);
		return (ENOMEM);
	}
	return (0);
}
int mrsas_alloc_mfi_cmds ( struct mrsas_softc * sc ) { int i , j ; u_int32_t max_cmd ; struct mrsas_mfi_cmd * cmd ; max_cmd = MRSAS_MAX_MFI_CMDS ; sc -> mfi_cmd_list = malloc ( sizeof ( struct mrsas_mfi_cmd * ) * max_cmd , M_MRSAS , M_NOWAIT ) ; if ( ! sc -> mfi_cmd_list ) { device_printf ( sc -> mrsas_dev , " \n " ) ; return ( ENOMEM ) ; } memset ( sc -> mfi_cmd_list , 0 , sizeof ( struct mrsas_mfi_cmd * ) * max_cmd ) ; for ( i = 0 ; i < max_cmd ; i ++ ) { sc -> mfi_cmd_list [ i ] = malloc ( sizeof ( struct mrsas_mfi_cmd ) , M_MRSAS , M_NOWAIT ) ; if ( ! sc -> mfi_cmd_list [ i ] ) { for ( j = 0 ; j < i ; j ++ ) free ( sc -> mfi_cmd_list [ j ] , M_MRSAS ) ; free ( sc -> mfi_cmd_list , M_MRSAS ) ; sc -> mfi_cmd_list = NULL ; return ( ENOMEM ) ; } } for ( i = 0 ; i < max_cmd ; i ++ ) { cmd = sc -> mfi_cmd_list [ i ] ; memset ( cmd , 0 , sizeof ( struct mrsas_mfi_cmd ) ) ; cmd -> index = i ; cmd -> ccb_ptr = NULL ; cmd -> sc = sc ; TAILQ_INSERT_TAIL ( & ( sc -> mrsas_mfi_cmd_list_head ) , cmd , next ) ; } if ( mrsas_create_frame_pool ( sc ) ) { device_printf ( sc -> mrsas_dev , " \n " ) ; for ( i = 0 ; i < MRSAS_MAX_MFI_CMDS ; i ++ ) { cmd = sc -> mfi_cmd_list [ i ] ; mrsas_free_frame ( sc , cmd ) ; } if ( sc -> mficmd_frame_tag != NULL ) bus_dma_tag_destroy ( sc -> mficmd_frame_tag ) ; return ( ENOMEM ) ; } return ( 0 ) ; }
mrsas_alloc_mfi_cmds:        Allocates the command packets
 input:                                       Adapter instance soft state
mrsas_alloc_mfi_cmds : Allocates the command packets input : Adapter instance soft state
/*
	 * sc->mfi_cmd_list is an array of struct mrsas_mfi_cmd pointers.
	 * Allocate the dynamic array first and then allocate individual
	 * commands.
	 */ /* create a frame pool and assign one frame to each command */ /* Free the frames */