2 * Copyright (c) 2005-2009 Brocade Communications Systems, Inc.
6 * Linux driver for Brocade Fibre Channel Host Bus Adapter.
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License (GPL) Version 2 as
10 * published by the Free Software Foundation
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
19 * bfad.c Linux driver PCI interface module.
22 #include <linux/slab.h>
23 #include <linux/module.h>
24 #include <linux/kthread.h>
28 #include "bfad_ipfc.h"
29 #include "bfad_trcmod.h"
30 #include <fcb/bfa_fcb_vf.h>
31 #include <fcb/bfa_fcb_rport.h>
32 #include <fcb/bfa_fcb_port.h>
33 #include <fcb/bfa_fcb.h>
35 BFA_TRC_FILE(LDRV, BFAD);
36 DEFINE_MUTEX(bfad_mutex);
39 int bfad_supported_fc4s;
41 static char *host_name;
43 static char *os_patch;
44 static int num_rports;
48 static int num_ufbufs;
52 static int rport_del_timeout = BFA_FCS_RPORT_DEF_DEL_TIMEOUT;
53 static int bfa_io_max_sge = BFAD_IO_MAX_SGE;
54 static int log_level = BFA_LOG_WARNING;
55 static int ioc_auto_recover = BFA_TRUE;
56 static int ipfc_enable = BFA_FALSE;
57 static int ipfc_mtu = -1;
58 static int fdmi_enable = BFA_TRUE;
59 int bfa_lun_queue_depth = BFAD_LUN_QUEUE_DEPTH;
60 int bfa_linkup_delay = -1;
62 module_param(os_name, charp, S_IRUGO | S_IWUSR);
63 module_param(os_patch, charp, S_IRUGO | S_IWUSR);
64 module_param(host_name, charp, S_IRUGO | S_IWUSR);
65 module_param(num_rports, int, S_IRUGO | S_IWUSR);
66 module_param(num_ios, int, S_IRUGO | S_IWUSR);
67 module_param(num_tms, int, S_IRUGO | S_IWUSR);
68 module_param(num_fcxps, int, S_IRUGO | S_IWUSR);
69 module_param(num_ufbufs, int, S_IRUGO | S_IWUSR);
70 module_param(reqq_size, int, S_IRUGO | S_IWUSR);
71 module_param(rspq_size, int, S_IRUGO | S_IWUSR);
72 module_param(num_sgpgs, int, S_IRUGO | S_IWUSR);
73 module_param(rport_del_timeout, int, S_IRUGO | S_IWUSR);
74 module_param(bfa_lun_queue_depth, int, S_IRUGO | S_IWUSR);
75 module_param(bfa_io_max_sge, int, S_IRUGO | S_IWUSR);
76 module_param(log_level, int, S_IRUGO | S_IWUSR);
77 module_param(ioc_auto_recover, int, S_IRUGO | S_IWUSR);
78 module_param(ipfc_enable, int, S_IRUGO | S_IWUSR);
79 module_param(ipfc_mtu, int, S_IRUGO | S_IWUSR);
80 module_param(fdmi_enable, int, S_IRUGO | S_IWUSR);
81 module_param(bfa_linkup_delay, int, S_IRUGO | S_IWUSR);
84 * Stores the module parm num_sgpgs value;
85 * used to reset for bfad next instance.
87 static int num_sgpgs_parm;
90 bfad_fc4_probe(struct bfad_s *bfad)
94 rc = bfad_im_probe(bfad);
95 if (rc != BFA_STATUS_OK)
101 bfad_ipfc_probe(bfad);
103 bfad->bfad_flags |= BFAD_FC4_PROBE_DONE;
109 bfad_fc4_probe_undo(struct bfad_s *bfad)
111 bfad_im_probe_undo(bfad);
112 bfad_tm_probe_undo(bfad);
114 bfad_ipfc_probe_undo(bfad);
115 bfad->bfad_flags &= ~BFAD_FC4_PROBE_DONE;
119 bfad_fc4_probe_post(struct bfad_s *bfad)
122 bfad_im_probe_post(bfad->im);
124 bfad_tm_probe_post(bfad);
126 bfad_ipfc_probe_post(bfad);
130 bfad_fc4_port_new(struct bfad_s *bfad, struct bfad_port_s *port, int roles)
132 int rc = BFA_STATUS_FAILED;
134 if (roles & BFA_PORT_ROLE_FCP_IM)
135 rc = bfad_im_port_new(bfad, port);
136 if (rc != BFA_STATUS_OK)
139 if (roles & BFA_PORT_ROLE_FCP_TM)
140 rc = bfad_tm_port_new(bfad, port);
141 if (rc != BFA_STATUS_OK)
144 if ((roles & BFA_PORT_ROLE_FCP_IPFC) && ipfc_enable)
145 rc = bfad_ipfc_port_new(bfad, port, port->pvb_type);
151 bfad_fc4_port_delete(struct bfad_s *bfad, struct bfad_port_s *port, int roles)
153 if (roles & BFA_PORT_ROLE_FCP_IM)
154 bfad_im_port_delete(bfad, port);
156 if (roles & BFA_PORT_ROLE_FCP_TM)
157 bfad_tm_port_delete(bfad, port);
159 if ((roles & BFA_PORT_ROLE_FCP_IPFC) && ipfc_enable)
160 bfad_ipfc_port_delete(bfad, port);
167 bfad_hcb_comp(void *arg, bfa_status_t status)
169 struct bfad_hal_comp *fcomp = (struct bfad_hal_comp *)arg;
171 fcomp->status = status;
172 complete(&fcomp->comp);
179 bfa_cb_init(void *drv, bfa_status_t init_status)
181 struct bfad_s *bfad = drv;
183 if (init_status == BFA_STATUS_OK) {
184 bfad->bfad_flags |= BFAD_HAL_INIT_DONE;
186 /* If BFAD_HAL_INIT_FAIL flag is set:
187 * Wake up the kernel thread to start
188 * the bfad operations after HAL init done
190 if ((bfad->bfad_flags & BFAD_HAL_INIT_FAIL)) {
191 bfad->bfad_flags &= ~BFAD_HAL_INIT_FAIL;
192 wake_up_process(bfad->bfad_tsk);
196 complete(&bfad->comp);
204 static struct bfad_port_s *
205 bfad_get_drv_port(struct bfad_s *bfad, struct bfad_vf_s *vf_drv,
206 struct bfad_vport_s *vp_drv)
208 return (vp_drv) ? (&(vp_drv)->drv_port)
209 : ((vf_drv) ? (&(vf_drv)->base_port) : (&(bfad)->pport));
213 bfa_fcb_port_new(struct bfad_s *bfad, struct bfa_fcs_port_s *port,
214 enum bfa_port_role roles, struct bfad_vf_s *vf_drv,
215 struct bfad_vport_s *vp_drv)
218 struct bfad_port_s *port_drv;
220 if (!vp_drv && !vf_drv) {
221 port_drv = &bfad->pport;
222 port_drv->pvb_type = BFAD_PORT_PHYS_BASE;
223 } else if (!vp_drv && vf_drv) {
224 port_drv = &vf_drv->base_port;
225 port_drv->pvb_type = BFAD_PORT_VF_BASE;
226 } else if (vp_drv && !vf_drv) {
227 port_drv = &vp_drv->drv_port;
228 port_drv->pvb_type = BFAD_PORT_PHYS_VPORT;
230 port_drv = &vp_drv->drv_port;
231 port_drv->pvb_type = BFAD_PORT_VF_VPORT;
234 port_drv->fcs_port = port;
235 port_drv->roles = roles;
236 rc = bfad_fc4_port_new(bfad, port_drv, roles);
237 if (rc != BFA_STATUS_OK) {
238 bfad_fc4_port_delete(bfad, port_drv, roles);
246 bfa_fcb_port_delete(struct bfad_s *bfad, enum bfa_port_role roles,
247 struct bfad_vf_s *vf_drv, struct bfad_vport_s *vp_drv)
249 struct bfad_port_s *port_drv;
252 * this will be only called from rmmod context
254 if (vp_drv && !vp_drv->comp_del) {
255 port_drv = bfad_get_drv_port(bfad, vf_drv, vp_drv);
256 bfa_trc(bfad, roles);
257 bfad_fc4_port_delete(bfad, port_drv, roles);
262 bfa_fcb_port_online(struct bfad_s *bfad, enum bfa_port_role roles,
263 struct bfad_vf_s *vf_drv, struct bfad_vport_s *vp_drv)
265 struct bfad_port_s *port_drv = bfad_get_drv_port(bfad, vf_drv, vp_drv);
267 if (roles & BFA_PORT_ROLE_FCP_IM)
268 bfad_im_port_online(bfad, port_drv);
270 if (roles & BFA_PORT_ROLE_FCP_TM)
271 bfad_tm_port_online(bfad, port_drv);
273 if ((roles & BFA_PORT_ROLE_FCP_IPFC) && ipfc_enable)
274 bfad_ipfc_port_online(bfad, port_drv);
276 bfad->bfad_flags |= BFAD_PORT_ONLINE;
280 bfa_fcb_port_offline(struct bfad_s *bfad, enum bfa_port_role roles,
281 struct bfad_vf_s *vf_drv, struct bfad_vport_s *vp_drv)
283 struct bfad_port_s *port_drv = bfad_get_drv_port(bfad, vf_drv, vp_drv);
285 if (roles & BFA_PORT_ROLE_FCP_IM)
286 bfad_im_port_offline(bfad, port_drv);
288 if (roles & BFA_PORT_ROLE_FCP_TM)
289 bfad_tm_port_offline(bfad, port_drv);
291 if ((roles & BFA_PORT_ROLE_FCP_IPFC) && ipfc_enable)
292 bfad_ipfc_port_offline(bfad, port_drv);
296 bfa_fcb_vport_delete(struct bfad_vport_s *vport_drv)
298 if (vport_drv->comp_del) {
299 complete(vport_drv->comp_del);
305 * FCS RPORT alloc callback, after successful PLOGI by FCS
308 bfa_fcb_rport_alloc(struct bfad_s *bfad, struct bfa_fcs_rport_s **rport,
309 struct bfad_rport_s **rport_drv)
311 bfa_status_t rc = BFA_STATUS_OK;
313 *rport_drv = kzalloc(sizeof(struct bfad_rport_s), GFP_ATOMIC);
314 if (*rport_drv == NULL) {
315 rc = BFA_STATUS_ENOMEM;
319 *rport = &(*rport_drv)->fcs_rport;
328 bfad_hal_mem_release(struct bfad_s *bfad)
331 struct bfa_meminfo_s *hal_meminfo = &bfad->meminfo;
332 struct bfa_mem_elem_s *meminfo_elem;
334 for (i = 0; i < BFA_MEM_TYPE_MAX; i++) {
335 meminfo_elem = &hal_meminfo->meminfo[i];
336 if (meminfo_elem->kva != NULL) {
337 switch (meminfo_elem->mem_type) {
338 case BFA_MEM_TYPE_KVA:
339 vfree(meminfo_elem->kva);
341 case BFA_MEM_TYPE_DMA:
342 dma_free_coherent(&bfad->pcidev->dev,
343 meminfo_elem->mem_len,
345 (dma_addr_t) meminfo_elem->dma);
354 memset(hal_meminfo, 0, sizeof(struct bfa_meminfo_s));
358 bfad_update_hal_cfg(struct bfa_iocfc_cfg_s *bfa_cfg)
361 bfa_cfg->fwcfg.num_rports = num_rports;
363 bfa_cfg->fwcfg.num_ioim_reqs = num_ios;
365 bfa_cfg->fwcfg.num_tskim_reqs = num_tms;
367 bfa_cfg->fwcfg.num_fcxp_reqs = num_fcxps;
369 bfa_cfg->fwcfg.num_uf_bufs = num_ufbufs;
371 bfa_cfg->drvcfg.num_reqq_elems = reqq_size;
373 bfa_cfg->drvcfg.num_rspq_elems = rspq_size;
375 bfa_cfg->drvcfg.num_sgpgs = num_sgpgs;
378 * populate the hal values back to the driver for sysfs use.
379 * otherwise, the default values will be shown as 0 in sysfs
381 num_rports = bfa_cfg->fwcfg.num_rports;
382 num_ios = bfa_cfg->fwcfg.num_ioim_reqs;
383 num_tms = bfa_cfg->fwcfg.num_tskim_reqs;
384 num_fcxps = bfa_cfg->fwcfg.num_fcxp_reqs;
385 num_ufbufs = bfa_cfg->fwcfg.num_uf_bufs;
386 reqq_size = bfa_cfg->drvcfg.num_reqq_elems;
387 rspq_size = bfa_cfg->drvcfg.num_rspq_elems;
388 num_sgpgs = bfa_cfg->drvcfg.num_sgpgs;
392 bfad_hal_mem_alloc(struct bfad_s *bfad)
394 struct bfa_meminfo_s *hal_meminfo = &bfad->meminfo;
395 struct bfa_mem_elem_s *meminfo_elem;
396 bfa_status_t rc = BFA_STATUS_OK;
397 dma_addr_t phys_addr;
400 int min_num_sgpgs = 512;
404 bfa_cfg_get_default(&bfad->ioc_cfg);
407 bfad_update_hal_cfg(&bfad->ioc_cfg);
408 bfad->cfg_data.ioc_queue_depth = bfad->ioc_cfg.fwcfg.num_ioim_reqs;
409 bfa_cfg_get_meminfo(&bfad->ioc_cfg, hal_meminfo);
411 for (i = 0; i < BFA_MEM_TYPE_MAX; i++) {
412 meminfo_elem = &hal_meminfo->meminfo[i];
413 switch (meminfo_elem->mem_type) {
414 case BFA_MEM_TYPE_KVA:
415 kva = vmalloc(meminfo_elem->mem_len);
417 bfad_hal_mem_release(bfad);
418 rc = BFA_STATUS_ENOMEM;
421 memset(kva, 0, meminfo_elem->mem_len);
422 meminfo_elem->kva = kva;
424 case BFA_MEM_TYPE_DMA:
425 kva = dma_alloc_coherent(&bfad->pcidev->dev,
426 meminfo_elem->mem_len,
427 &phys_addr, GFP_KERNEL);
429 bfad_hal_mem_release(bfad);
431 * If we cannot allocate with default
432 * num_sgpages try with half the value.
434 if (num_sgpgs > min_num_sgpgs) {
435 printk(KERN_INFO "bfad[%d]: memory"
436 " allocation failed with"
438 bfad->inst_no, num_sgpgs);
439 nextLowerInt(&num_sgpgs);
440 printk(KERN_INFO "bfad[%d]: trying to"
441 " allocate memory with"
443 bfad->inst_no, num_sgpgs);
447 if (num_sgpgs_parm > 0)
448 num_sgpgs = num_sgpgs_parm;
452 num_sgpgs *= reset_value;
454 rc = BFA_STATUS_ENOMEM;
459 if (num_sgpgs_parm > 0)
460 num_sgpgs = num_sgpgs_parm;
462 reset_value = (1 << retry_count);
463 num_sgpgs *= reset_value;
466 memset(kva, 0, meminfo_elem->mem_len);
467 meminfo_elem->kva = kva;
468 meminfo_elem->dma = phys_addr;
480 * Create a vport under a vf.
483 bfad_vport_create(struct bfad_s *bfad, u16 vf_id,
484 struct bfa_port_cfg_s *port_cfg, struct device *dev)
486 struct bfad_vport_s *vport;
487 int rc = BFA_STATUS_OK;
489 struct completion fcomp;
491 vport = kzalloc(sizeof(struct bfad_vport_s), GFP_KERNEL);
493 rc = BFA_STATUS_ENOMEM;
497 vport->drv_port.bfad = bfad;
498 spin_lock_irqsave(&bfad->bfad_lock, flags);
499 rc = bfa_fcs_vport_create(&vport->fcs_vport, &bfad->bfa_fcs, vf_id,
501 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
503 if (rc != BFA_STATUS_OK)
506 if (port_cfg->roles & BFA_PORT_ROLE_FCP_IM) {
507 rc = bfad_im_scsi_host_alloc(bfad, vport->drv_port.im_port,
509 if (rc != BFA_STATUS_OK)
510 goto ext_free_fcs_vport;
513 spin_lock_irqsave(&bfad->bfad_lock, flags);
514 bfa_fcs_vport_start(&vport->fcs_vport);
515 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
517 return BFA_STATUS_OK;
520 spin_lock_irqsave(&bfad->bfad_lock, flags);
521 vport->comp_del = &fcomp;
522 init_completion(vport->comp_del);
523 bfa_fcs_vport_delete(&vport->fcs_vport);
524 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
525 wait_for_completion(vport->comp_del);
533 * Create a vf and its base vport implicitely.
536 bfad_vf_create(struct bfad_s *bfad, u16 vf_id,
537 struct bfa_port_cfg_s *port_cfg)
539 struct bfad_vf_s *vf;
540 int rc = BFA_STATUS_OK;
542 vf = kzalloc(sizeof(struct bfad_vf_s), GFP_KERNEL);
544 rc = BFA_STATUS_FAILED;
548 rc = bfa_fcs_vf_create(&vf->fcs_vf, &bfad->bfa_fcs, vf_id, port_cfg,
550 if (rc != BFA_STATUS_OK)
557 bfad_bfa_tmo(unsigned long data)
559 struct bfad_s *bfad = (struct bfad_s *)data;
561 struct list_head doneq;
563 spin_lock_irqsave(&bfad->bfad_lock, flags);
565 bfa_timer_tick(&bfad->bfa);
567 bfa_comp_deq(&bfad->bfa, &doneq);
568 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
570 if (!list_empty(&doneq)) {
571 bfa_comp_process(&bfad->bfa, &doneq);
572 spin_lock_irqsave(&bfad->bfad_lock, flags);
573 bfa_comp_free(&bfad->bfa, &doneq);
574 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
577 mod_timer(&bfad->hal_tmo, jiffies + msecs_to_jiffies(BFA_TIMER_FREQ));
581 bfad_init_timer(struct bfad_s *bfad)
583 init_timer(&bfad->hal_tmo);
584 bfad->hal_tmo.function = bfad_bfa_tmo;
585 bfad->hal_tmo.data = (unsigned long)bfad;
587 mod_timer(&bfad->hal_tmo, jiffies + msecs_to_jiffies(BFA_TIMER_FREQ));
591 bfad_pci_init(struct pci_dev *pdev, struct bfad_s *bfad)
595 if (pci_enable_device(pdev)) {
596 BFA_PRINTF(BFA_ERR, "pci_enable_device fail %p\n", pdev);
600 if (pci_request_regions(pdev, BFAD_DRIVER_NAME))
601 goto out_disable_device;
603 pci_set_master(pdev);
606 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0)
607 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0) {
608 BFA_PRINTF(BFA_ERR, "pci_set_dma_mask fail %p\n", pdev);
609 goto out_release_region;
612 bfad->pci_bar0_kva = pci_iomap(pdev, 0, pci_resource_len(pdev, 0));
614 if (bfad->pci_bar0_kva == NULL) {
615 BFA_PRINTF(BFA_ERR, "Fail to map bar0\n");
616 goto out_release_region;
619 bfad->hal_pcidev.pci_slot = PCI_SLOT(pdev->devfn);
620 bfad->hal_pcidev.pci_func = PCI_FUNC(pdev->devfn);
621 bfad->hal_pcidev.pci_bar_kva = bfad->pci_bar0_kva;
622 bfad->hal_pcidev.device_id = pdev->device;
623 bfad->pci_name = pci_name(pdev);
625 bfad->pci_attr.vendor_id = pdev->vendor;
626 bfad->pci_attr.device_id = pdev->device;
627 bfad->pci_attr.ssid = pdev->subsystem_device;
628 bfad->pci_attr.ssvid = pdev->subsystem_vendor;
629 bfad->pci_attr.pcifn = PCI_FUNC(pdev->devfn);
635 pci_release_regions(pdev);
637 pci_disable_device(pdev);
643 bfad_pci_uninit(struct pci_dev *pdev, struct bfad_s *bfad)
645 pci_iounmap(pdev, bfad->pci_bar0_kva);
646 pci_release_regions(pdev);
647 pci_disable_device(pdev);
648 pci_set_drvdata(pdev, NULL);
652 bfad_fcs_port_cfg(struct bfad_s *bfad)
654 struct bfa_port_cfg_s port_cfg;
655 struct bfa_pport_attr_s attr;
656 char symname[BFA_SYMNAME_MAXLEN];
658 sprintf(symname, "%s-%d", BFAD_DRIVER_NAME, bfad->inst_no);
659 memcpy(port_cfg.sym_name.symname, symname, strlen(symname));
660 bfa_fcport_get_attr(&bfad->bfa, &attr);
661 port_cfg.nwwn = attr.nwwn;
662 port_cfg.pwwn = attr.pwwn;
664 bfa_fcs_cfg_base_port(&bfad->bfa_fcs, &port_cfg);
668 bfad_drv_init(struct bfad_s *bfad)
672 struct bfa_fcs_driver_info_s driver_info;
674 bfad->cfg_data.rport_del_timeout = rport_del_timeout;
675 bfad->cfg_data.lun_queue_depth = bfa_lun_queue_depth;
676 bfad->cfg_data.io_max_sge = bfa_io_max_sge;
677 bfad->cfg_data.binding_method = FCP_PWWN_BINDING;
679 rc = bfad_hal_mem_alloc(bfad);
680 if (rc != BFA_STATUS_OK) {
681 printk(KERN_WARNING "bfad%d bfad_hal_mem_alloc failure\n",
684 "Not enough memory to attach all Brocade HBA ports,"
685 " System may need more memory.\n");
686 goto out_hal_mem_alloc_failure;
689 bfa_init_log(&bfad->bfa, bfad->logmod);
690 bfa_init_trc(&bfad->bfa, bfad->trcmod);
691 bfa_init_aen(&bfad->bfa, bfad->aen);
692 memset(bfad->file_map, 0, sizeof(bfad->file_map));
693 bfa_init_plog(&bfad->bfa, &bfad->plog_buf);
694 bfa_plog_init(&bfad->plog_buf);
695 bfa_plog_str(&bfad->plog_buf, BFA_PL_MID_DRVR, BFA_PL_EID_DRIVER_START,
698 bfa_attach(&bfad->bfa, bfad, &bfad->ioc_cfg, &bfad->meminfo,
701 init_completion(&bfad->comp);
704 * Enable Interrupt and wait bfa_init completion
706 if (bfad_setup_intr(bfad)) {
707 printk(KERN_WARNING "bfad%d: bfad_setup_intr failed\n",
709 goto out_setup_intr_failure;
712 spin_lock_irqsave(&bfad->bfad_lock, flags);
713 bfa_init(&bfad->bfa);
714 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
717 * Set up interrupt handler for each vectors
719 if ((bfad->bfad_flags & BFAD_MSIX_ON)
720 && bfad_install_msix_handler(bfad)) {
721 printk(KERN_WARNING "%s: install_msix failed, bfad%d\n",
722 __func__, bfad->inst_no);
725 bfad_init_timer(bfad);
727 wait_for_completion(&bfad->comp);
729 memset(&driver_info, 0, sizeof(driver_info));
730 strncpy(driver_info.version, BFAD_DRIVER_VERSION,
731 sizeof(driver_info.version) - 1);
733 strncpy(driver_info.host_machine_name, host_name,
734 sizeof(driver_info.host_machine_name) - 1);
736 strncpy(driver_info.host_os_name, os_name,
737 sizeof(driver_info.host_os_name) - 1);
739 strncpy(driver_info.host_os_patch, os_patch,
740 sizeof(driver_info.host_os_patch) - 1);
742 strncpy(driver_info.os_device_name, bfad->pci_name,
743 sizeof(driver_info.os_device_name - 1));
748 spin_lock_irqsave(&bfad->bfad_lock, flags);
749 bfa_fcs_log_init(&bfad->bfa_fcs, bfad->logmod);
750 bfa_fcs_trc_init(&bfad->bfa_fcs, bfad->trcmod);
751 bfa_fcs_aen_init(&bfad->bfa_fcs, bfad->aen);
752 bfa_fcs_attach(&bfad->bfa_fcs, &bfad->bfa, bfad, BFA_FALSE);
754 /* Do FCS init only when HAL init is done */
755 if ((bfad->bfad_flags & BFAD_HAL_INIT_DONE)) {
756 bfa_fcs_init(&bfad->bfa_fcs);
757 bfad->bfad_flags |= BFAD_FCS_INIT_DONE;
760 bfa_fcs_driver_info_init(&bfad->bfa_fcs, &driver_info);
761 bfa_fcs_set_fdmi_param(&bfad->bfa_fcs, fdmi_enable);
762 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
764 bfad->bfad_flags |= BFAD_DRV_INIT_DONE;
765 return BFA_STATUS_OK;
767 out_setup_intr_failure:
768 bfa_detach(&bfad->bfa);
769 bfad_hal_mem_release(bfad);
770 out_hal_mem_alloc_failure:
771 return BFA_STATUS_FAILED;
775 bfad_drv_uninit(struct bfad_s *bfad)
779 spin_lock_irqsave(&bfad->bfad_lock, flags);
780 init_completion(&bfad->comp);
781 bfa_stop(&bfad->bfa);
782 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
783 wait_for_completion(&bfad->comp);
785 del_timer_sync(&bfad->hal_tmo);
786 bfa_isr_disable(&bfad->bfa);
787 bfa_detach(&bfad->bfa);
788 bfad_remove_intr(bfad);
789 bfad_hal_mem_release(bfad);
791 bfad->bfad_flags &= ~BFAD_DRV_INIT_DONE;
795 bfad_drv_start(struct bfad_s *bfad)
799 spin_lock_irqsave(&bfad->bfad_lock, flags);
800 bfa_start(&bfad->bfa);
801 bfa_fcs_start(&bfad->bfa_fcs);
802 bfad->bfad_flags |= BFAD_HAL_START_DONE;
803 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
805 bfad_fc4_probe_post(bfad);
809 bfad_drv_stop(struct bfad_s *bfad)
813 spin_lock_irqsave(&bfad->bfad_lock, flags);
814 init_completion(&bfad->comp);
815 bfad->pport.flags |= BFAD_PORT_DELETE;
816 bfa_fcs_exit(&bfad->bfa_fcs);
817 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
818 wait_for_completion(&bfad->comp);
820 spin_lock_irqsave(&bfad->bfad_lock, flags);
821 init_completion(&bfad->comp);
822 bfa_stop(&bfad->bfa);
823 bfad->bfad_flags &= ~BFAD_HAL_START_DONE;
824 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
825 wait_for_completion(&bfad->comp);
829 bfad_cfg_pport(struct bfad_s *bfad, enum bfa_port_role role)
831 int rc = BFA_STATUS_OK;
834 * Allocate scsi_host for the physical port
836 if ((bfad_supported_fc4s & BFA_PORT_ROLE_FCP_IM)
837 && (role & BFA_PORT_ROLE_FCP_IM)) {
838 if (bfad->pport.im_port == NULL) {
839 rc = BFA_STATUS_FAILED;
843 rc = bfad_im_scsi_host_alloc(bfad, bfad->pport.im_port,
845 if (rc != BFA_STATUS_OK)
848 bfad->pport.roles |= BFA_PORT_ROLE_FCP_IM;
851 bfad->bfad_flags |= BFAD_CFG_PPORT_DONE;
858 bfad_uncfg_pport(struct bfad_s *bfad)
860 if ((bfad->pport.roles & BFA_PORT_ROLE_FCP_IPFC) && ipfc_enable) {
861 bfad_ipfc_port_delete(bfad, &bfad->pport);
862 bfad->pport.roles &= ~BFA_PORT_ROLE_FCP_IPFC;
865 if ((bfad_supported_fc4s & BFA_PORT_ROLE_FCP_IM)
866 && (bfad->pport.roles & BFA_PORT_ROLE_FCP_IM)) {
867 bfad_im_scsi_host_free(bfad, bfad->pport.im_port);
868 bfad_im_port_clean(bfad->pport.im_port);
869 kfree(bfad->pport.im_port);
870 bfad->pport.roles &= ~BFA_PORT_ROLE_FCP_IM;
873 bfad->bfad_flags &= ~BFAD_CFG_PPORT_DONE;
877 bfad_drv_log_level_set(struct bfad_s *bfad)
879 if (log_level > BFA_LOG_INVALID && log_level <= BFA_LOG_LEVEL_MAX)
880 bfa_log_set_level_all(&bfad->log_data, log_level);
884 bfad_start_ops(struct bfad_s *bfad)
888 /* PPORT FCS config */
889 bfad_fcs_port_cfg(bfad);
891 retval = bfad_cfg_pport(bfad, BFA_PORT_ROLE_FCP_IM);
892 if (retval != BFA_STATUS_OK)
893 goto out_cfg_pport_failure;
895 /* BFAD level FC4 (IM/TM/IPFC) specific resource allocation */
896 retval = bfad_fc4_probe(bfad);
897 if (retval != BFA_STATUS_OK) {
898 printk(KERN_WARNING "bfad_fc4_probe failed\n");
899 goto out_fc4_probe_failure;
902 bfad_drv_start(bfad);
905 * If bfa_linkup_delay is set to -1 default; try to retrive the
906 * value using the bfad_os_get_linkup_delay(); else use the
907 * passed in module param value as the bfa_linkup_delay.
909 if (bfa_linkup_delay < 0) {
911 bfa_linkup_delay = bfad_os_get_linkup_delay(bfad);
912 bfad_os_rport_online_wait(bfad);
913 bfa_linkup_delay = -1;
916 bfad_os_rport_online_wait(bfad);
919 bfa_log(bfad->logmod, BFA_LOG_LINUX_DEVICE_CLAIMED, bfad->pci_name);
921 return BFA_STATUS_OK;
923 out_fc4_probe_failure:
924 bfad_fc4_probe_undo(bfad);
925 bfad_uncfg_pport(bfad);
926 out_cfg_pport_failure:
927 return BFA_STATUS_FAILED;
931 bfad_worker (void *ptr)
936 bfad = (struct bfad_s *)ptr;
938 while (!kthread_should_stop()) {
940 /* Check if the FCS init is done from bfad_drv_init;
941 * if not done do FCS init and set the flag.
943 if (!(bfad->bfad_flags & BFAD_FCS_INIT_DONE)) {
944 spin_lock_irqsave(&bfad->bfad_lock, flags);
945 bfa_fcs_init(&bfad->bfa_fcs);
946 bfad->bfad_flags |= BFAD_FCS_INIT_DONE;
947 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
950 /* Start the bfad operations after HAL init done */
951 bfad_start_ops(bfad);
953 spin_lock_irqsave(&bfad->bfad_lock, flags);
954 bfad->bfad_tsk = NULL;
955 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
964 * PCI_entry PCI driver entries * {
971 bfad_pci_probe(struct pci_dev *pdev, const struct pci_device_id *pid)
974 int error = -ENODEV, retval;
977 * For single port cards - only claim function 0
979 if ((pdev->device == BFA_PCI_DEVICE_ID_FC_8G1P)
980 && (PCI_FUNC(pdev->devfn) != 0))
983 BFA_TRACE(BFA_INFO, "bfad_pci_probe entry");
985 bfad = kzalloc(sizeof(struct bfad_s), GFP_KERNEL);
991 bfad->trcmod = kzalloc(sizeof(struct bfa_trc_mod_s), GFP_KERNEL);
993 printk(KERN_WARNING "Error alloc trace buffer!\n");
995 goto out_alloc_trace_failure;
1001 bfa_trc_init(bfad->trcmod);
1002 bfa_trc(bfad, bfad_inst);
1004 bfad->logmod = &bfad->log_data;
1005 bfa_log_init(bfad->logmod, (char *)pci_name(pdev), bfa_os_printf);
1007 bfad_drv_log_level_set(bfad);
1009 bfad->aen = &bfad->aen_buf;
1011 if (!(bfad_load_fwimg(pdev))) {
1012 printk(KERN_WARNING "bfad_load_fwimg failure!\n");
1013 kfree(bfad->trcmod);
1014 goto out_alloc_trace_failure;
1017 retval = bfad_pci_init(pdev, bfad);
1019 printk(KERN_WARNING "bfad_pci_init failure!\n");
1021 goto out_pci_init_failure;
1024 mutex_lock(&bfad_mutex);
1025 bfad->inst_no = bfad_inst++;
1026 list_add_tail(&bfad->list_entry, &bfad_list);
1027 mutex_unlock(&bfad_mutex);
1029 spin_lock_init(&bfad->bfad_lock);
1030 pci_set_drvdata(pdev, bfad);
1032 bfad->ref_count = 0;
1033 bfad->pport.bfad = bfad;
1035 bfad->bfad_tsk = kthread_create(bfad_worker, (void *) bfad, "%s",
1037 if (IS_ERR(bfad->bfad_tsk)) {
1038 printk(KERN_INFO "bfad[%d]: Kernel thread"
1039 " creation failed!\n",
1041 goto out_kthread_create_failure;
1044 retval = bfad_drv_init(bfad);
1045 if (retval != BFA_STATUS_OK)
1046 goto out_drv_init_failure;
1047 if (!(bfad->bfad_flags & BFAD_HAL_INIT_DONE)) {
1048 bfad->bfad_flags |= BFAD_HAL_INIT_FAIL;
1049 printk(KERN_WARNING "bfad%d: hal init failed\n", bfad->inst_no);
1053 retval = bfad_start_ops(bfad);
1054 if (retval != BFA_STATUS_OK)
1055 goto out_start_ops_failure;
1057 kthread_stop(bfad->bfad_tsk);
1058 bfad->bfad_tsk = NULL;
1063 out_start_ops_failure:
1064 bfad_drv_uninit(bfad);
1065 out_drv_init_failure:
1066 kthread_stop(bfad->bfad_tsk);
1067 out_kthread_create_failure:
1068 mutex_lock(&bfad_mutex);
1070 list_del(&bfad->list_entry);
1071 mutex_unlock(&bfad_mutex);
1072 bfad_pci_uninit(pdev, bfad);
1073 out_pci_init_failure:
1074 kfree(bfad->trcmod);
1075 out_alloc_trace_failure:
1085 bfad_pci_remove(struct pci_dev *pdev)
1087 struct bfad_s *bfad = pci_get_drvdata(pdev);
1088 unsigned long flags;
1090 bfa_trc(bfad, bfad->inst_no);
1092 spin_lock_irqsave(&bfad->bfad_lock, flags);
1093 if (bfad->bfad_tsk != NULL)
1094 kthread_stop(bfad->bfad_tsk);
1095 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1097 if ((bfad->bfad_flags & BFAD_DRV_INIT_DONE)
1098 && !(bfad->bfad_flags & BFAD_HAL_INIT_DONE)) {
1100 spin_lock_irqsave(&bfad->bfad_lock, flags);
1101 init_completion(&bfad->comp);
1102 bfa_stop(&bfad->bfa);
1103 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1104 wait_for_completion(&bfad->comp);
1106 bfad_remove_intr(bfad);
1107 del_timer_sync(&bfad->hal_tmo);
1109 } else if (!(bfad->bfad_flags & BFAD_DRV_INIT_DONE)) {
1113 if (bfad->bfad_flags & BFAD_HAL_START_DONE) {
1114 bfad_drv_stop(bfad);
1115 } else if (bfad->bfad_flags & BFAD_DRV_INIT_DONE) {
1116 /* Invoking bfa_stop() before bfa_detach
1117 * when HAL and DRV init are success
1118 * but HAL start did not occur.
1120 spin_lock_irqsave(&bfad->bfad_lock, flags);
1121 init_completion(&bfad->comp);
1122 bfa_stop(&bfad->bfa);
1123 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1124 wait_for_completion(&bfad->comp);
1127 bfad_remove_intr(bfad);
1128 del_timer_sync(&bfad->hal_tmo);
1130 if (bfad->bfad_flags & BFAD_FC4_PROBE_DONE)
1131 bfad_fc4_probe_undo(bfad);
1133 if (bfad->bfad_flags & BFAD_CFG_PPORT_DONE)
1134 bfad_uncfg_pport(bfad);
1137 spin_lock_irqsave(&bfad->bfad_lock, flags);
1138 bfa_detach(&bfad->bfa);
1139 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1140 bfad_hal_mem_release(bfad);
1143 mutex_lock(&bfad_mutex);
1145 list_del(&bfad->list_entry);
1146 mutex_unlock(&bfad_mutex);
1147 bfad_pci_uninit(pdev, bfad);
1149 kfree(bfad->trcmod);
1154 static struct pci_device_id bfad_id_table[] = {
1156 .vendor = BFA_PCI_VENDOR_ID_BROCADE,
1157 .device = BFA_PCI_DEVICE_ID_FC_8G2P,
1158 .subvendor = PCI_ANY_ID,
1159 .subdevice = PCI_ANY_ID,
1162 .vendor = BFA_PCI_VENDOR_ID_BROCADE,
1163 .device = BFA_PCI_DEVICE_ID_FC_8G1P,
1164 .subvendor = PCI_ANY_ID,
1165 .subdevice = PCI_ANY_ID,
1168 .vendor = BFA_PCI_VENDOR_ID_BROCADE,
1169 .device = BFA_PCI_DEVICE_ID_CT,
1170 .subvendor = PCI_ANY_ID,
1171 .subdevice = PCI_ANY_ID,
1172 .class = (PCI_CLASS_SERIAL_FIBER << 8),
1179 MODULE_DEVICE_TABLE(pci, bfad_id_table);
1181 static struct pci_driver bfad_pci_driver = {
1182 .name = BFAD_DRIVER_NAME,
1183 .id_table = bfad_id_table,
1184 .probe = bfad_pci_probe,
1185 .remove = __devexit_p(bfad_pci_remove),
1189 * Linux driver module functions
1192 bfad_fc4_module_init(void)
1196 rc = bfad_im_module_init();
1197 if (rc != BFA_STATUS_OK)
1200 bfad_tm_module_init();
1202 bfad_ipfc_module_init();
1208 bfad_fc4_module_exit(void)
1211 bfad_ipfc_module_exit();
1212 bfad_tm_module_exit();
1213 bfad_im_module_exit();
1217 * Driver module init.
1224 printk(KERN_INFO "Brocade BFA FC/FCOE SCSI driver - version: %s\n",
1225 BFAD_DRIVER_VERSION);
1228 num_sgpgs_parm = num_sgpgs;
1230 error = bfad_fc4_module_init();
1233 printk(KERN_WARNING "bfad_fc4_module_init failure\n");
1237 if (!strcmp(FCPI_NAME, " fcpim"))
1238 bfad_supported_fc4s |= BFA_PORT_ROLE_FCP_IM;
1239 if (!strcmp(FCPT_NAME, " fcptm"))
1240 bfad_supported_fc4s |= BFA_PORT_ROLE_FCP_TM;
1241 if (!strcmp(IPFC_NAME, " ipfc"))
1242 bfad_supported_fc4s |= BFA_PORT_ROLE_FCP_IPFC;
1244 bfa_ioc_auto_recover(ioc_auto_recover);
1245 bfa_fcs_rport_set_del_timeout(rport_del_timeout);
1246 error = pci_register_driver(&bfad_pci_driver);
1249 printk(KERN_WARNING "bfad pci_register_driver failure\n");
1256 bfad_fc4_module_exit();
1261 * Driver module exit.
1266 pci_unregister_driver(&bfad_pci_driver);
1267 bfad_fc4_module_exit();
1271 #define BFAD_PROTO_NAME FCPI_NAME FCPT_NAME IPFC_NAME
1273 module_init(bfad_init);
1274 module_exit(bfad_exit);
1275 MODULE_LICENSE("GPL");
1276 MODULE_DESCRIPTION("Brocade Fibre Channel HBA Driver" BFAD_PROTO_NAME);
1277 MODULE_AUTHOR("Brocade Communications Systems, Inc.");
1278 MODULE_VERSION(BFAD_DRIVER_VERSION);