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 static 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
);
307 * FCS RPORT alloc callback, after successful PLOGI by FCS
310 bfa_fcb_rport_alloc(struct bfad_s
*bfad
, struct bfa_fcs_rport_s
**rport
,
311 struct bfad_rport_s
**rport_drv
)
313 bfa_status_t rc
= BFA_STATUS_OK
;
315 *rport_drv
= kzalloc(sizeof(struct bfad_rport_s
), GFP_ATOMIC
);
316 if (*rport_drv
== NULL
) {
317 rc
= BFA_STATUS_ENOMEM
;
321 *rport
= &(*rport_drv
)->fcs_rport
;
330 bfad_hal_mem_release(struct bfad_s
*bfad
)
333 struct bfa_meminfo_s
*hal_meminfo
= &bfad
->meminfo
;
334 struct bfa_mem_elem_s
*meminfo_elem
;
336 for (i
= 0; i
< BFA_MEM_TYPE_MAX
; i
++) {
337 meminfo_elem
= &hal_meminfo
->meminfo
[i
];
338 if (meminfo_elem
->kva
!= NULL
) {
339 switch (meminfo_elem
->mem_type
) {
340 case BFA_MEM_TYPE_KVA
:
341 vfree(meminfo_elem
->kva
);
343 case BFA_MEM_TYPE_DMA
:
344 dma_free_coherent(&bfad
->pcidev
->dev
,
345 meminfo_elem
->mem_len
,
347 (dma_addr_t
) meminfo_elem
->dma
);
356 memset(hal_meminfo
, 0, sizeof(struct bfa_meminfo_s
));
360 bfad_update_hal_cfg(struct bfa_iocfc_cfg_s
*bfa_cfg
)
363 bfa_cfg
->fwcfg
.num_rports
= num_rports
;
365 bfa_cfg
->fwcfg
.num_ioim_reqs
= num_ios
;
367 bfa_cfg
->fwcfg
.num_tskim_reqs
= num_tms
;
369 bfa_cfg
->fwcfg
.num_fcxp_reqs
= num_fcxps
;
371 bfa_cfg
->fwcfg
.num_uf_bufs
= num_ufbufs
;
373 bfa_cfg
->drvcfg
.num_reqq_elems
= reqq_size
;
375 bfa_cfg
->drvcfg
.num_rspq_elems
= rspq_size
;
377 bfa_cfg
->drvcfg
.num_sgpgs
= num_sgpgs
;
380 * populate the hal values back to the driver for sysfs use.
381 * otherwise, the default values will be shown as 0 in sysfs
383 num_rports
= bfa_cfg
->fwcfg
.num_rports
;
384 num_ios
= bfa_cfg
->fwcfg
.num_ioim_reqs
;
385 num_tms
= bfa_cfg
->fwcfg
.num_tskim_reqs
;
386 num_fcxps
= bfa_cfg
->fwcfg
.num_fcxp_reqs
;
387 num_ufbufs
= bfa_cfg
->fwcfg
.num_uf_bufs
;
388 reqq_size
= bfa_cfg
->drvcfg
.num_reqq_elems
;
389 rspq_size
= bfa_cfg
->drvcfg
.num_rspq_elems
;
390 num_sgpgs
= bfa_cfg
->drvcfg
.num_sgpgs
;
394 bfad_hal_mem_alloc(struct bfad_s
*bfad
)
396 struct bfa_meminfo_s
*hal_meminfo
= &bfad
->meminfo
;
397 struct bfa_mem_elem_s
*meminfo_elem
;
398 bfa_status_t rc
= BFA_STATUS_OK
;
399 dma_addr_t phys_addr
;
402 int min_num_sgpgs
= 512;
406 bfa_cfg_get_default(&bfad
->ioc_cfg
);
409 bfad_update_hal_cfg(&bfad
->ioc_cfg
);
410 bfad
->cfg_data
.ioc_queue_depth
= bfad
->ioc_cfg
.fwcfg
.num_ioim_reqs
;
411 bfa_cfg_get_meminfo(&bfad
->ioc_cfg
, hal_meminfo
);
413 for (i
= 0; i
< BFA_MEM_TYPE_MAX
; i
++) {
414 meminfo_elem
= &hal_meminfo
->meminfo
[i
];
415 switch (meminfo_elem
->mem_type
) {
416 case BFA_MEM_TYPE_KVA
:
417 kva
= vmalloc(meminfo_elem
->mem_len
);
419 bfad_hal_mem_release(bfad
);
420 rc
= BFA_STATUS_ENOMEM
;
423 memset(kva
, 0, meminfo_elem
->mem_len
);
424 meminfo_elem
->kva
= kva
;
426 case BFA_MEM_TYPE_DMA
:
427 kva
= dma_alloc_coherent(&bfad
->pcidev
->dev
,
428 meminfo_elem
->mem_len
,
429 &phys_addr
, GFP_KERNEL
);
431 bfad_hal_mem_release(bfad
);
433 * If we cannot allocate with default
434 * num_sgpages try with half the value.
436 if (num_sgpgs
> min_num_sgpgs
) {
437 printk(KERN_INFO
"bfad[%d]: memory"
438 " allocation failed with"
440 bfad
->inst_no
, num_sgpgs
);
441 nextLowerInt(&num_sgpgs
);
442 printk(KERN_INFO
"bfad[%d]: trying to"
443 " allocate memory with"
445 bfad
->inst_no
, num_sgpgs
);
449 if (num_sgpgs_parm
> 0)
450 num_sgpgs
= num_sgpgs_parm
;
454 num_sgpgs
*= reset_value
;
456 rc
= BFA_STATUS_ENOMEM
;
461 if (num_sgpgs_parm
> 0)
462 num_sgpgs
= num_sgpgs_parm
;
464 reset_value
= (1 << retry_count
);
465 num_sgpgs
*= reset_value
;
468 memset(kva
, 0, meminfo_elem
->mem_len
);
469 meminfo_elem
->kva
= kva
;
470 meminfo_elem
->dma
= phys_addr
;
482 * Create a vport under a vf.
485 bfad_vport_create(struct bfad_s
*bfad
, u16 vf_id
,
486 struct bfa_port_cfg_s
*port_cfg
)
488 struct bfad_vport_s
*vport
;
489 int rc
= BFA_STATUS_OK
;
491 struct completion fcomp
;
493 vport
= kzalloc(sizeof(struct bfad_vport_s
), GFP_KERNEL
);
495 rc
= BFA_STATUS_ENOMEM
;
499 vport
->drv_port
.bfad
= bfad
;
500 spin_lock_irqsave(&bfad
->bfad_lock
, flags
);
501 rc
= bfa_fcs_vport_create(&vport
->fcs_vport
, &bfad
->bfa_fcs
, vf_id
,
503 spin_unlock_irqrestore(&bfad
->bfad_lock
, flags
);
505 if (rc
!= BFA_STATUS_OK
)
508 if (port_cfg
->roles
& BFA_PORT_ROLE_FCP_IM
) {
509 rc
= bfad_im_scsi_host_alloc(bfad
, vport
->drv_port
.im_port
);
510 if (rc
!= BFA_STATUS_OK
)
511 goto ext_free_fcs_vport
;
514 spin_lock_irqsave(&bfad
->bfad_lock
, flags
);
515 bfa_fcs_vport_start(&vport
->fcs_vport
);
516 spin_unlock_irqrestore(&bfad
->bfad_lock
, flags
);
518 return BFA_STATUS_OK
;
521 spin_lock_irqsave(&bfad
->bfad_lock
, flags
);
522 vport
->comp_del
= &fcomp
;
523 init_completion(vport
->comp_del
);
524 bfa_fcs_vport_delete(&vport
->fcs_vport
);
525 spin_unlock_irqrestore(&bfad
->bfad_lock
, flags
);
526 wait_for_completion(vport
->comp_del
);
534 * Create a vf and its base vport implicitely.
537 bfad_vf_create(struct bfad_s
*bfad
, u16 vf_id
,
538 struct bfa_port_cfg_s
*port_cfg
)
540 struct bfad_vf_s
*vf
;
541 int rc
= BFA_STATUS_OK
;
543 vf
= kzalloc(sizeof(struct bfad_vf_s
), GFP_KERNEL
);
545 rc
= BFA_STATUS_FAILED
;
549 rc
= bfa_fcs_vf_create(&vf
->fcs_vf
, &bfad
->bfa_fcs
, vf_id
, port_cfg
,
551 if (rc
!= BFA_STATUS_OK
)
558 bfad_bfa_tmo(unsigned long data
)
560 struct bfad_s
*bfad
= (struct bfad_s
*)data
;
562 struct list_head doneq
;
564 spin_lock_irqsave(&bfad
->bfad_lock
, flags
);
566 bfa_timer_tick(&bfad
->bfa
);
568 bfa_comp_deq(&bfad
->bfa
, &doneq
);
569 spin_unlock_irqrestore(&bfad
->bfad_lock
, flags
);
571 if (!list_empty(&doneq
)) {
572 bfa_comp_process(&bfad
->bfa
, &doneq
);
573 spin_lock_irqsave(&bfad
->bfad_lock
, flags
);
574 bfa_comp_free(&bfad
->bfa
, &doneq
);
575 spin_unlock_irqrestore(&bfad
->bfad_lock
, flags
);
578 mod_timer(&bfad
->hal_tmo
, jiffies
+ msecs_to_jiffies(BFA_TIMER_FREQ
));
582 bfad_init_timer(struct bfad_s
*bfad
)
584 init_timer(&bfad
->hal_tmo
);
585 bfad
->hal_tmo
.function
= bfad_bfa_tmo
;
586 bfad
->hal_tmo
.data
= (unsigned long)bfad
;
588 mod_timer(&bfad
->hal_tmo
, jiffies
+ msecs_to_jiffies(BFA_TIMER_FREQ
));
592 bfad_pci_init(struct pci_dev
*pdev
, struct bfad_s
*bfad
)
594 unsigned long bar0_len
;
597 if (pci_enable_device(pdev
)) {
598 BFA_PRINTF(BFA_ERR
, "pci_enable_device fail %p\n", pdev
);
602 if (pci_request_regions(pdev
, BFAD_DRIVER_NAME
))
603 goto out_disable_device
;
605 pci_set_master(pdev
);
608 if (pci_set_dma_mask(pdev
, DMA_BIT_MASK(64)) != 0)
609 if (pci_set_dma_mask(pdev
, DMA_BIT_MASK(32)) != 0) {
610 BFA_PRINTF(BFA_ERR
, "pci_set_dma_mask fail %p\n", pdev
);
611 goto out_release_region
;
614 bfad
->pci_bar0_map
= pci_resource_start(pdev
, 0);
615 bar0_len
= pci_resource_len(pdev
, 0);
616 bfad
->pci_bar0_kva
= ioremap(bfad
->pci_bar0_map
, bar0_len
);
618 if (bfad
->pci_bar0_kva
== NULL
) {
619 BFA_PRINTF(BFA_ERR
, "Fail to map bar0\n");
620 goto out_release_region
;
623 bfad
->hal_pcidev
.pci_slot
= PCI_SLOT(pdev
->devfn
);
624 bfad
->hal_pcidev
.pci_func
= PCI_FUNC(pdev
->devfn
);
625 bfad
->hal_pcidev
.pci_bar_kva
= bfad
->pci_bar0_kva
;
626 bfad
->hal_pcidev
.device_id
= pdev
->device
;
627 bfad
->pci_name
= pci_name(pdev
);
629 bfad
->pci_attr
.vendor_id
= pdev
->vendor
;
630 bfad
->pci_attr
.device_id
= pdev
->device
;
631 bfad
->pci_attr
.ssid
= pdev
->subsystem_device
;
632 bfad
->pci_attr
.ssvid
= pdev
->subsystem_vendor
;
633 bfad
->pci_attr
.pcifn
= PCI_FUNC(pdev
->devfn
);
639 pci_release_regions(pdev
);
641 pci_disable_device(pdev
);
647 bfad_pci_uninit(struct pci_dev
*pdev
, struct bfad_s
*bfad
)
649 #if defined(__ia64__)
650 pci_iounmap(pdev
, bfad
->pci_bar0_kva
);
652 iounmap(bfad
->pci_bar0_kva
);
654 pci_release_regions(pdev
);
655 pci_disable_device(pdev
);
656 pci_set_drvdata(pdev
, NULL
);
660 bfad_fcs_port_cfg(struct bfad_s
*bfad
)
662 struct bfa_port_cfg_s port_cfg
;
663 struct bfa_pport_attr_s attr
;
664 char symname
[BFA_SYMNAME_MAXLEN
];
666 sprintf(symname
, "%s-%d", BFAD_DRIVER_NAME
, bfad
->inst_no
);
667 memcpy(port_cfg
.sym_name
.symname
, symname
, strlen(symname
));
668 bfa_fcport_get_attr(&bfad
->bfa
, &attr
);
669 port_cfg
.nwwn
= attr
.nwwn
;
670 port_cfg
.pwwn
= attr
.pwwn
;
672 bfa_fcs_cfg_base_port(&bfad
->bfa_fcs
, &port_cfg
);
676 bfad_drv_init(struct bfad_s
*bfad
)
680 struct bfa_fcs_driver_info_s driver_info
;
682 bfad
->cfg_data
.rport_del_timeout
= rport_del_timeout
;
683 bfad
->cfg_data
.lun_queue_depth
= bfa_lun_queue_depth
;
684 bfad
->cfg_data
.io_max_sge
= bfa_io_max_sge
;
685 bfad
->cfg_data
.binding_method
= FCP_PWWN_BINDING
;
687 rc
= bfad_hal_mem_alloc(bfad
);
688 if (rc
!= BFA_STATUS_OK
) {
689 printk(KERN_WARNING
"bfad%d bfad_hal_mem_alloc failure\n",
692 "Not enough memory to attach all Brocade HBA ports,"
693 " System may need more memory.\n");
694 goto out_hal_mem_alloc_failure
;
697 bfa_init_log(&bfad
->bfa
, bfad
->logmod
);
698 bfa_init_trc(&bfad
->bfa
, bfad
->trcmod
);
699 bfa_init_aen(&bfad
->bfa
, bfad
->aen
);
700 memset(bfad
->file_map
, 0, sizeof(bfad
->file_map
));
701 bfa_init_plog(&bfad
->bfa
, &bfad
->plog_buf
);
702 bfa_plog_init(&bfad
->plog_buf
);
703 bfa_plog_str(&bfad
->plog_buf
, BFA_PL_MID_DRVR
, BFA_PL_EID_DRIVER_START
,
706 bfa_attach(&bfad
->bfa
, bfad
, &bfad
->ioc_cfg
, &bfad
->meminfo
,
709 init_completion(&bfad
->comp
);
712 * Enable Interrupt and wait bfa_init completion
714 if (bfad_setup_intr(bfad
)) {
715 printk(KERN_WARNING
"bfad%d: bfad_setup_intr failed\n",
717 goto out_setup_intr_failure
;
720 spin_lock_irqsave(&bfad
->bfad_lock
, flags
);
721 bfa_init(&bfad
->bfa
);
722 spin_unlock_irqrestore(&bfad
->bfad_lock
, flags
);
725 * Set up interrupt handler for each vectors
727 if ((bfad
->bfad_flags
& BFAD_MSIX_ON
)
728 && bfad_install_msix_handler(bfad
)) {
729 printk(KERN_WARNING
"%s: install_msix failed, bfad%d\n",
730 __func__
, bfad
->inst_no
);
733 bfad_init_timer(bfad
);
735 wait_for_completion(&bfad
->comp
);
737 memset(&driver_info
, 0, sizeof(driver_info
));
738 strncpy(driver_info
.version
, BFAD_DRIVER_VERSION
,
739 sizeof(driver_info
.version
) - 1);
741 strncpy(driver_info
.host_machine_name
, host_name
,
742 sizeof(driver_info
.host_machine_name
) - 1);
744 strncpy(driver_info
.host_os_name
, os_name
,
745 sizeof(driver_info
.host_os_name
) - 1);
747 strncpy(driver_info
.host_os_patch
, os_patch
,
748 sizeof(driver_info
.host_os_patch
) - 1);
750 strncpy(driver_info
.os_device_name
, bfad
->pci_name
,
751 sizeof(driver_info
.os_device_name
- 1));
756 spin_lock_irqsave(&bfad
->bfad_lock
, flags
);
757 bfa_fcs_log_init(&bfad
->bfa_fcs
, bfad
->logmod
);
758 bfa_fcs_trc_init(&bfad
->bfa_fcs
, bfad
->trcmod
);
759 bfa_fcs_aen_init(&bfad
->bfa_fcs
, bfad
->aen
);
760 bfa_fcs_attach(&bfad
->bfa_fcs
, &bfad
->bfa
, bfad
, BFA_FALSE
);
762 /* Do FCS init only when HAL init is done */
763 if ((bfad
->bfad_flags
& BFAD_HAL_INIT_DONE
)) {
764 bfa_fcs_init(&bfad
->bfa_fcs
);
765 bfad
->bfad_flags
|= BFAD_FCS_INIT_DONE
;
768 bfa_fcs_driver_info_init(&bfad
->bfa_fcs
, &driver_info
);
769 bfa_fcs_set_fdmi_param(&bfad
->bfa_fcs
, fdmi_enable
);
770 spin_unlock_irqrestore(&bfad
->bfad_lock
, flags
);
772 bfad
->bfad_flags
|= BFAD_DRV_INIT_DONE
;
773 return BFA_STATUS_OK
;
775 out_setup_intr_failure
:
776 bfa_detach(&bfad
->bfa
);
777 bfad_hal_mem_release(bfad
);
778 out_hal_mem_alloc_failure
:
779 return BFA_STATUS_FAILED
;
783 bfad_drv_uninit(struct bfad_s
*bfad
)
787 spin_lock_irqsave(&bfad
->bfad_lock
, flags
);
788 init_completion(&bfad
->comp
);
789 bfa_stop(&bfad
->bfa
);
790 spin_unlock_irqrestore(&bfad
->bfad_lock
, flags
);
791 wait_for_completion(&bfad
->comp
);
793 del_timer_sync(&bfad
->hal_tmo
);
794 bfa_isr_disable(&bfad
->bfa
);
795 bfa_detach(&bfad
->bfa
);
796 bfad_remove_intr(bfad
);
797 bfad_hal_mem_release(bfad
);
799 bfad
->bfad_flags
&= ~BFAD_DRV_INIT_DONE
;
803 bfad_drv_start(struct bfad_s
*bfad
)
807 spin_lock_irqsave(&bfad
->bfad_lock
, flags
);
808 bfa_start(&bfad
->bfa
);
809 bfa_fcs_start(&bfad
->bfa_fcs
);
810 bfad
->bfad_flags
|= BFAD_HAL_START_DONE
;
811 spin_unlock_irqrestore(&bfad
->bfad_lock
, flags
);
813 bfad_fc4_probe_post(bfad
);
817 bfad_drv_stop(struct bfad_s
*bfad
)
821 spin_lock_irqsave(&bfad
->bfad_lock
, flags
);
822 init_completion(&bfad
->comp
);
823 bfad
->pport
.flags
|= BFAD_PORT_DELETE
;
824 bfa_fcs_exit(&bfad
->bfa_fcs
);
825 spin_unlock_irqrestore(&bfad
->bfad_lock
, flags
);
826 wait_for_completion(&bfad
->comp
);
828 spin_lock_irqsave(&bfad
->bfad_lock
, flags
);
829 init_completion(&bfad
->comp
);
830 bfa_stop(&bfad
->bfa
);
831 bfad
->bfad_flags
&= ~BFAD_HAL_START_DONE
;
832 spin_unlock_irqrestore(&bfad
->bfad_lock
, flags
);
833 wait_for_completion(&bfad
->comp
);
837 bfad_cfg_pport(struct bfad_s
*bfad
, enum bfa_port_role role
)
839 int rc
= BFA_STATUS_OK
;
842 * Allocate scsi_host for the physical port
844 if ((bfad_supported_fc4s
& BFA_PORT_ROLE_FCP_IM
)
845 && (role
& BFA_PORT_ROLE_FCP_IM
)) {
846 if (bfad
->pport
.im_port
== NULL
) {
847 rc
= BFA_STATUS_FAILED
;
851 rc
= bfad_im_scsi_host_alloc(bfad
, bfad
->pport
.im_port
);
852 if (rc
!= BFA_STATUS_OK
)
855 bfad
->pport
.roles
|= BFA_PORT_ROLE_FCP_IM
;
858 bfad
->bfad_flags
|= BFAD_CFG_PPORT_DONE
;
865 bfad_uncfg_pport(struct bfad_s
*bfad
)
867 if ((bfad
->pport
.roles
& BFA_PORT_ROLE_FCP_IPFC
) && ipfc_enable
) {
868 bfad_ipfc_port_delete(bfad
, &bfad
->pport
);
869 bfad
->pport
.roles
&= ~BFA_PORT_ROLE_FCP_IPFC
;
872 if ((bfad_supported_fc4s
& BFA_PORT_ROLE_FCP_IM
)
873 && (bfad
->pport
.roles
& BFA_PORT_ROLE_FCP_IM
)) {
874 bfad_im_scsi_host_free(bfad
, bfad
->pport
.im_port
);
875 bfad_im_port_clean(bfad
->pport
.im_port
);
876 kfree(bfad
->pport
.im_port
);
877 bfad
->pport
.roles
&= ~BFA_PORT_ROLE_FCP_IM
;
880 bfad
->bfad_flags
&= ~BFAD_CFG_PPORT_DONE
;
884 bfad_drv_log_level_set(struct bfad_s
*bfad
)
886 if (log_level
> BFA_LOG_INVALID
&& log_level
<= BFA_LOG_LEVEL_MAX
)
887 bfa_log_set_level_all(&bfad
->log_data
, log_level
);
891 bfad_start_ops(struct bfad_s
*bfad
)
895 /* PPORT FCS config */
896 bfad_fcs_port_cfg(bfad
);
898 retval
= bfad_cfg_pport(bfad
, BFA_PORT_ROLE_FCP_IM
);
899 if (retval
!= BFA_STATUS_OK
)
900 goto out_cfg_pport_failure
;
902 /* BFAD level FC4 (IM/TM/IPFC) specific resource allocation */
903 retval
= bfad_fc4_probe(bfad
);
904 if (retval
!= BFA_STATUS_OK
) {
905 printk(KERN_WARNING
"bfad_fc4_probe failed\n");
906 goto out_fc4_probe_failure
;
909 bfad_drv_start(bfad
);
912 * If bfa_linkup_delay is set to -1 default; try to retrive the
913 * value using the bfad_os_get_linkup_delay(); else use the
914 * passed in module param value as the bfa_linkup_delay.
916 if (bfa_linkup_delay
< 0) {
918 bfa_linkup_delay
= bfad_os_get_linkup_delay(bfad
);
919 bfad_os_rport_online_wait(bfad
);
920 bfa_linkup_delay
= -1;
923 bfad_os_rport_online_wait(bfad
);
926 bfa_log(bfad
->logmod
, BFA_LOG_LINUX_DEVICE_CLAIMED
, bfad
->pci_name
);
928 return BFA_STATUS_OK
;
930 out_fc4_probe_failure
:
931 bfad_fc4_probe_undo(bfad
);
932 bfad_uncfg_pport(bfad
);
933 out_cfg_pport_failure
:
934 return BFA_STATUS_FAILED
;
938 bfad_worker (void *ptr
)
943 bfad
= (struct bfad_s
*)ptr
;
945 while (!kthread_should_stop()) {
947 /* Check if the FCS init is done from bfad_drv_init;
948 * if not done do FCS init and set the flag.
950 if (!(bfad
->bfad_flags
& BFAD_FCS_INIT_DONE
)) {
951 spin_lock_irqsave(&bfad
->bfad_lock
, flags
);
952 bfa_fcs_init(&bfad
->bfa_fcs
);
953 bfad
->bfad_flags
|= BFAD_FCS_INIT_DONE
;
954 spin_unlock_irqrestore(&bfad
->bfad_lock
, flags
);
957 /* Start the bfad operations after HAL init done */
958 bfad_start_ops(bfad
);
960 spin_lock_irqsave(&bfad
->bfad_lock
, flags
);
961 bfad
->bfad_tsk
= NULL
;
962 spin_unlock_irqrestore(&bfad
->bfad_lock
, flags
);
971 * PCI_entry PCI driver entries * {
978 bfad_pci_probe(struct pci_dev
*pdev
, const struct pci_device_id
*pid
)
981 int error
= -ENODEV
, retval
;
984 * For single port cards - only claim function 0
986 if ((pdev
->device
== BFA_PCI_DEVICE_ID_FC_8G1P
)
987 && (PCI_FUNC(pdev
->devfn
) != 0))
990 BFA_TRACE(BFA_INFO
, "bfad_pci_probe entry");
992 bfad
= kzalloc(sizeof(struct bfad_s
), GFP_KERNEL
);
998 bfad
->trcmod
= kzalloc(sizeof(struct bfa_trc_mod_s
), GFP_KERNEL
);
1000 printk(KERN_WARNING
"Error alloc trace buffer!\n");
1002 goto out_alloc_trace_failure
;
1008 bfa_trc_init(bfad
->trcmod
);
1009 bfa_trc(bfad
, bfad_inst
);
1011 bfad
->logmod
= &bfad
->log_data
;
1012 bfa_log_init(bfad
->logmod
, (char *)pci_name(pdev
), bfa_os_printf
);
1014 bfad_drv_log_level_set(bfad
);
1016 bfad
->aen
= &bfad
->aen_buf
;
1018 if (!(bfad_load_fwimg(pdev
))) {
1019 printk(KERN_WARNING
"bfad_load_fwimg failure!\n");
1020 kfree(bfad
->trcmod
);
1021 goto out_alloc_trace_failure
;
1024 retval
= bfad_pci_init(pdev
, bfad
);
1026 printk(KERN_WARNING
"bfad_pci_init failure!\n");
1028 goto out_pci_init_failure
;
1031 mutex_lock(&bfad_mutex
);
1032 bfad
->inst_no
= bfad_inst
++;
1033 list_add_tail(&bfad
->list_entry
, &bfad_list
);
1034 mutex_unlock(&bfad_mutex
);
1036 spin_lock_init(&bfad
->bfad_lock
);
1037 pci_set_drvdata(pdev
, bfad
);
1039 bfad
->ref_count
= 0;
1040 bfad
->pport
.bfad
= bfad
;
1042 bfad
->bfad_tsk
= kthread_create(bfad_worker
, (void *) bfad
, "%s",
1044 if (IS_ERR(bfad
->bfad_tsk
)) {
1045 printk(KERN_INFO
"bfad[%d]: Kernel thread"
1046 " creation failed!\n",
1048 goto out_kthread_create_failure
;
1051 retval
= bfad_drv_init(bfad
);
1052 if (retval
!= BFA_STATUS_OK
)
1053 goto out_drv_init_failure
;
1054 if (!(bfad
->bfad_flags
& BFAD_HAL_INIT_DONE
)) {
1055 bfad
->bfad_flags
|= BFAD_HAL_INIT_FAIL
;
1056 printk(KERN_WARNING
"bfad%d: hal init failed\n", bfad
->inst_no
);
1060 retval
= bfad_start_ops(bfad
);
1061 if (retval
!= BFA_STATUS_OK
)
1062 goto out_start_ops_failure
;
1064 kthread_stop(bfad
->bfad_tsk
);
1065 bfad
->bfad_tsk
= NULL
;
1070 out_start_ops_failure
:
1071 bfad_drv_uninit(bfad
);
1072 out_drv_init_failure
:
1073 kthread_stop(bfad
->bfad_tsk
);
1074 out_kthread_create_failure
:
1075 mutex_lock(&bfad_mutex
);
1077 list_del(&bfad
->list_entry
);
1078 mutex_unlock(&bfad_mutex
);
1079 bfad_pci_uninit(pdev
, bfad
);
1080 out_pci_init_failure
:
1081 kfree(bfad
->trcmod
);
1082 out_alloc_trace_failure
:
1092 bfad_pci_remove(struct pci_dev
*pdev
)
1094 struct bfad_s
*bfad
= pci_get_drvdata(pdev
);
1095 unsigned long flags
;
1097 bfa_trc(bfad
, bfad
->inst_no
);
1099 spin_lock_irqsave(&bfad
->bfad_lock
, flags
);
1100 if (bfad
->bfad_tsk
!= NULL
)
1101 kthread_stop(bfad
->bfad_tsk
);
1102 spin_unlock_irqrestore(&bfad
->bfad_lock
, flags
);
1104 if ((bfad
->bfad_flags
& BFAD_DRV_INIT_DONE
)
1105 && !(bfad
->bfad_flags
& BFAD_HAL_INIT_DONE
)) {
1107 spin_lock_irqsave(&bfad
->bfad_lock
, flags
);
1108 init_completion(&bfad
->comp
);
1109 bfa_stop(&bfad
->bfa
);
1110 spin_unlock_irqrestore(&bfad
->bfad_lock
, flags
);
1111 wait_for_completion(&bfad
->comp
);
1113 bfad_remove_intr(bfad
);
1114 del_timer_sync(&bfad
->hal_tmo
);
1116 } else if (!(bfad
->bfad_flags
& BFAD_DRV_INIT_DONE
)) {
1120 if (bfad
->bfad_flags
& BFAD_HAL_START_DONE
) {
1121 bfad_drv_stop(bfad
);
1122 } else if (bfad
->bfad_flags
& BFAD_DRV_INIT_DONE
) {
1123 /* Invoking bfa_stop() before bfa_detach
1124 * when HAL and DRV init are success
1125 * but HAL start did not occur.
1127 spin_lock_irqsave(&bfad
->bfad_lock
, flags
);
1128 init_completion(&bfad
->comp
);
1129 bfa_stop(&bfad
->bfa
);
1130 spin_unlock_irqrestore(&bfad
->bfad_lock
, flags
);
1131 wait_for_completion(&bfad
->comp
);
1134 bfad_remove_intr(bfad
);
1135 del_timer_sync(&bfad
->hal_tmo
);
1137 if (bfad
->bfad_flags
& BFAD_FC4_PROBE_DONE
)
1138 bfad_fc4_probe_undo(bfad
);
1140 if (bfad
->bfad_flags
& BFAD_CFG_PPORT_DONE
)
1141 bfad_uncfg_pport(bfad
);
1144 spin_lock_irqsave(&bfad
->bfad_lock
, flags
);
1145 bfa_detach(&bfad
->bfa
);
1146 spin_unlock_irqrestore(&bfad
->bfad_lock
, flags
);
1147 bfad_hal_mem_release(bfad
);
1150 mutex_lock(&bfad_mutex
);
1152 list_del(&bfad
->list_entry
);
1153 mutex_unlock(&bfad_mutex
);
1154 bfad_pci_uninit(pdev
, bfad
);
1156 kfree(bfad
->trcmod
);
1161 static struct pci_device_id bfad_id_table
[] = {
1163 .vendor
= BFA_PCI_VENDOR_ID_BROCADE
,
1164 .device
= BFA_PCI_DEVICE_ID_FC_8G2P
,
1165 .subvendor
= PCI_ANY_ID
,
1166 .subdevice
= PCI_ANY_ID
,
1169 .vendor
= BFA_PCI_VENDOR_ID_BROCADE
,
1170 .device
= BFA_PCI_DEVICE_ID_FC_8G1P
,
1171 .subvendor
= PCI_ANY_ID
,
1172 .subdevice
= PCI_ANY_ID
,
1175 .vendor
= BFA_PCI_VENDOR_ID_BROCADE
,
1176 .device
= BFA_PCI_DEVICE_ID_CT
,
1177 .subvendor
= PCI_ANY_ID
,
1178 .subdevice
= PCI_ANY_ID
,
1179 .class = (PCI_CLASS_SERIAL_FIBER
<< 8),
1186 MODULE_DEVICE_TABLE(pci
, bfad_id_table
);
1188 static struct pci_driver bfad_pci_driver
= {
1189 .name
= BFAD_DRIVER_NAME
,
1190 .id_table
= bfad_id_table
,
1191 .probe
= bfad_pci_probe
,
1192 .remove
= __devexit_p(bfad_pci_remove
),
1196 * Linux driver module functions
1199 bfad_fc4_module_init(void)
1203 rc
= bfad_im_module_init();
1204 if (rc
!= BFA_STATUS_OK
)
1207 bfad_tm_module_init();
1209 bfad_ipfc_module_init();
1215 bfad_fc4_module_exit(void)
1218 bfad_ipfc_module_exit();
1219 bfad_tm_module_exit();
1220 bfad_im_module_exit();
1224 * Driver module init.
1231 printk(KERN_INFO
"Brocade BFA FC/FCOE SCSI driver - version: %s\n",
1232 BFAD_DRIVER_VERSION
);
1235 num_sgpgs_parm
= num_sgpgs
;
1237 error
= bfad_fc4_module_init();
1240 printk(KERN_WARNING
"bfad_fc4_module_init failure\n");
1244 if (!strcmp(FCPI_NAME
, " fcpim"))
1245 bfad_supported_fc4s
|= BFA_PORT_ROLE_FCP_IM
;
1246 if (!strcmp(FCPT_NAME
, " fcptm"))
1247 bfad_supported_fc4s
|= BFA_PORT_ROLE_FCP_TM
;
1248 if (!strcmp(IPFC_NAME
, " ipfc"))
1249 bfad_supported_fc4s
|= BFA_PORT_ROLE_FCP_IPFC
;
1251 bfa_ioc_auto_recover(ioc_auto_recover
);
1252 bfa_fcs_rport_set_del_timeout(rport_del_timeout
);
1253 error
= pci_register_driver(&bfad_pci_driver
);
1256 printk(KERN_WARNING
"bfad pci_register_driver failure\n");
1263 bfad_fc4_module_exit();
1268 * Driver module exit.
1273 pci_unregister_driver(&bfad_pci_driver
);
1274 bfad_fc4_module_exit();
1278 #define BFAD_PROTO_NAME FCPI_NAME FCPT_NAME IPFC_NAME
1280 module_init(bfad_init
);
1281 module_exit(bfad_exit
);
1282 MODULE_LICENSE("GPL");
1283 MODULE_DESCRIPTION("Brocade Fibre Channel HBA Driver" BFAD_PROTO_NAME
);
1284 MODULE_AUTHOR("Brocade Communications Systems, Inc.");
1285 MODULE_VERSION(BFAD_DRIVER_VERSION
);