FreeBSD/sys/dev/mrsas/mrsas.c
/*
 * mrsas_attach:	PCI entry point
 * input:			pointer to device struct
 *
 * Performs setup of PCI and registers, initializes mutexes and linked lists,
 * registers interrupts and CAM, and initializes   the adapter/controller to
 * its proper state.
 */
PCI entry point
input:                       pointer to device struct

Performs setup of PCI and registers, initializes mutexes and linked lists,
registers interrupts and CAM, and initializes   the adapter/controller to
its proper state.
PCI entry point input : pointer to device struct Performs setup of PCI and registers initializes mutexes and linked lists registers interrupts and CAM and initializes the adapter / controller to its proper state .
static int
mrsas_attach(device_t dev)
{
	struct mrsas_softc *sc = device_get_softc(dev);
	uint32_t cmd, bar, error;
	memset(sc, 0, sizeof(struct mrsas_softc));
	sc->mrsas_dev = dev;
	sc->device_id = pci_get_device(dev);
	mrsas_get_tunables(sc);
	cmd = pci_read_config(dev, PCIR_COMMAND, 2);
	if ((cmd & PCIM_CMD_PORTEN) == 0) {
		return (ENXIO);
	}
	cmd |= PCIM_CMD_BUSMASTEREN;
	pci_write_config(dev, PCIR_COMMAND, cmd, 2);
	bar = pci_read_config(dev, MRSAS_PCI_BAR1, 4);
	sc->reg_res_id = MRSAS_PCI_BAR1;
	if ((sc->reg_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
	    &(sc->reg_res_id), RF_ACTIVE))
	    == NULL) {
		device_printf(dev, "Cannot allocate PCI registers\n");
		goto attach_fail;
	}
	sc->bus_tag = rman_get_bustag(sc->reg_res);
	sc->bus_handle = rman_get_bushandle(sc->reg_res);
	mtx_init(&sc->sim_lock, "mrsas_sim_lock", NULL, MTX_DEF);
	mtx_init(&sc->pci_lock, "mrsas_pci_lock", NULL, MTX_DEF);
	mtx_init(&sc->io_lock, "mrsas_io_lock", NULL, MTX_DEF);
	mtx_init(&sc->aen_lock, "mrsas_aen_lock", NULL, MTX_DEF);
	mtx_init(&sc->ioctl_lock, "mrsas_ioctl_lock", NULL, MTX_SPIN);
	mtx_init(&sc->mpt_cmd_pool_lock, "mrsas_mpt_cmd_pool_lock", NULL, MTX_DEF);
	mtx_init(&sc->mfi_cmd_pool_lock, "mrsas_mfi_cmd_pool_lock", NULL, MTX_DEF);
	mtx_init(&sc->raidmap_lock, "mrsas_raidmap_lock", NULL, MTX_DEF);
	TAILQ_INIT(&sc->mrsas_mpt_cmd_list_head);
	TAILQ_INIT(&sc->mrsas_mfi_cmd_list_head);
	mrsas_atomic_set(&sc->fw_outstanding, 0);
	sc->io_cmds_highwater = 0;
	sc->adprecovery = MRSAS_HBA_OPERATIONAL;
	sc->UnevenSpanSupport = 0;
	sc->msix_enable = 0;
	if (mrsas_init_fw(sc) != SUCCESS) {
		goto attach_fail_fw;
	}
	if ((mrsas_cam_attach(sc) != SUCCESS)) {
		goto attach_fail_cam;
	}
	if (mrsas_setup_irq(sc) != SUCCESS) {
		goto attach_fail_irq;
	}
	error = mrsas_kproc_create(mrsas_ocr_thread, sc,
	    &sc->ocr_thread, 0, 0, "mrsas_ocr%d",
	    device_get_unit(sc->mrsas_dev));
	if (error) {
		device_printf(sc->mrsas_dev, "Error %d starting OCR thread\n", error);
		goto attach_fail_ocr_thread;
	}
	sc->mrsas_ich.ich_func = mrsas_ich_startup;
	sc->mrsas_ich.ich_arg = sc;
	if (config_intrhook_establish(&sc->mrsas_ich) != 0) {
		device_printf(sc->mrsas_dev, "Config hook is already established\n");
	}
	mrsas_setup_sysctl(sc);
	return SUCCESS;
attach_fail_ocr_thread:
	if (sc->ocr_thread_active)
		wakeup(&sc->ocr_chan);
attach_fail_irq:
	mrsas_teardown_intr(sc);
attach_fail_cam:
	mrsas_cam_detach(sc);
attach_fail_fw:
	if (sc->msix_enable == 1)
		pci_release_msi(sc->mrsas_dev);
	mrsas_free_mem(sc);
	mtx_destroy(&sc->sim_lock);
	mtx_destroy(&sc->aen_lock);
	mtx_destroy(&sc->pci_lock);
	mtx_destroy(&sc->io_lock);
	mtx_destroy(&sc->ioctl_lock);
	mtx_destroy(&sc->mpt_cmd_pool_lock);
	mtx_destroy(&sc->mfi_cmd_pool_lock);
	mtx_destroy(&sc->raidmap_lock);
attach_fail:
	if (sc->reg_res) {
		bus_release_resource(sc->mrsas_dev, SYS_RES_MEMORY,
		    sc->reg_res_id, sc->reg_res);
	}
	return (ENXIO);
}
static int mrsas_attach ( device_t dev ) { struct mrsas_softc * sc = device_get_softc ( dev ) ; uint32_t cmd , bar , error ; memset ( sc , 0 , sizeof ( struct mrsas_softc ) ) ; sc -> mrsas_dev = dev ; sc -> device_id = pci_get_device ( dev ) ; mrsas_get_tunables ( sc ) ; cmd = pci_read_config ( dev , PCIR_COMMAND , 2 ) ; if ( ( cmd & PCIM_CMD_PORTEN ) == 0 ) { return ( ENXIO ) ; } cmd |= PCIM_CMD_BUSMASTEREN ; pci_write_config ( dev , PCIR_COMMAND , cmd , 2 ) ; bar = pci_read_config ( dev , MRSAS_PCI_BAR1 , 4 ) ; sc -> reg_res_id = MRSAS_PCI_BAR1 ; if ( ( sc -> reg_res = bus_alloc_resource_any ( dev , SYS_RES_MEMORY , & ( sc -> reg_res_id ) , RF_ACTIVE ) ) == NULL ) { device_printf ( dev , " \n " ) ; goto attach_fail ; } sc -> bus_tag = rman_get_bustag ( sc -> reg_res ) ; sc -> bus_handle = rman_get_bushandle ( sc -> reg_res ) ; mtx_init ( & sc -> sim_lock , " " , NULL , MTX_DEF ) ; mtx_init ( & sc -> pci_lock , " " , NULL , MTX_DEF ) ; mtx_init ( & sc -> io_lock , " " , NULL , MTX_DEF ) ; mtx_init ( & sc -> aen_lock , " " , NULL , MTX_DEF ) ; mtx_init ( & sc -> ioctl_lock , " " , NULL , MTX_SPIN ) ; mtx_init ( & sc -> mpt_cmd_pool_lock , " " , NULL , MTX_DEF ) ; mtx_init ( & sc -> mfi_cmd_pool_lock , " " , NULL , MTX_DEF ) ; mtx_init ( & sc -> raidmap_lock , " " , NULL , MTX_DEF ) ; TAILQ_INIT ( & sc -> mrsas_mpt_cmd_list_head ) ; TAILQ_INIT ( & sc -> mrsas_mfi_cmd_list_head ) ; mrsas_atomic_set ( & sc -> fw_outstanding , 0 ) ; sc -> io_cmds_highwater = 0 ; sc -> adprecovery = MRSAS_HBA_OPERATIONAL ; sc -> UnevenSpanSupport = 0 ; sc -> msix_enable = 0 ; if ( mrsas_init_fw ( sc ) != SUCCESS ) { goto attach_fail_fw ; } if ( ( mrsas_cam_attach ( sc ) != SUCCESS ) ) { goto attach_fail_cam ; } if ( mrsas_setup_irq ( sc ) != SUCCESS ) { goto attach_fail_irq ; } error = mrsas_kproc_create ( mrsas_ocr_thread , sc , & sc -> ocr_thread , 0 , 0 , " " , device_get_unit ( sc -> mrsas_dev ) ) ; if ( error ) { device_printf ( sc -> mrsas_dev , " \n " , error ) ; goto attach_fail_ocr_thread ; } sc -> mrsas_ich . ich_func = mrsas_ich_startup ; sc -> mrsas_ich . ich_arg = sc ; if ( config_intrhook_establish ( & sc -> mrsas_ich ) != 0 ) { device_printf ( sc -> mrsas_dev , " \n " ) ; } mrsas_setup_sysctl ( sc ) ; return SUCCESS ; attach_fail_ocr_thread : if ( sc -> ocr_thread_active ) wakeup ( & sc -> ocr_chan ) ; attach_fail_irq : mrsas_teardown_intr ( sc ) ; attach_fail_cam : mrsas_cam_detach ( sc ) ; attach_fail_fw : if ( sc -> msix_enable == 1 ) pci_release_msi ( sc -> mrsas_dev ) ; mrsas_free_mem ( sc ) ; mtx_destroy ( & sc -> sim_lock ) ; mtx_destroy ( & sc -> aen_lock ) ; mtx_destroy ( & sc -> pci_lock ) ; mtx_destroy ( & sc -> io_lock ) ; mtx_destroy ( & sc -> ioctl_lock ) ; mtx_destroy ( & sc -> mpt_cmd_pool_lock ) ; mtx_destroy ( & sc -> mfi_cmd_pool_lock ) ; mtx_destroy ( & sc -> raidmap_lock ) ; attach_fail : if ( sc -> reg_res ) { bus_release_resource ( sc -> mrsas_dev , SYS_RES_MEMORY , sc -> reg_res_id , sc -> reg_res ) ; } return ( ENXIO ) ; }
mrsas_attach:        PCI entry point
 input:                       pointer to device struct
mrsas_attach : PCI entry point input : pointer to device struct
/* Look up our softc and initialize its fields. */ /*
	 * Set up PCI and registers
	 */ /* Force the busmaster enable bit on. */ /* BAR1 offset */ /* Intialize mutexes */ /* Intialize linked list */ /* Initialize Firmware */ /* Register mrsas to CAM layer */ /* Register IRQs */ /*
	 * After FW initialization and OCR thread creation
	 * we will defer the cdev creation, AEN setup on ICH callback
	 */ /* if MSIX vector is allocated and FW Init FAILED then release MSIX */