net: dsa: b53: Fix ARL register definitions
[linux/fpc-iii.git] / drivers / scsi / bfa / bfad.c
blobbd7e6a6fc1f184e0b0065408864e7199d0adb46b
1 /*
2 * Copyright (c) 2005-2014 Brocade Communications Systems, Inc.
3 * Copyright (c) 2014- QLogic Corporation.
4 * All rights reserved
5 * www.qlogic.com
7 * Linux driver for QLogic BR-series Fibre Channel Host Bus Adapter.
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License (GPL) Version 2 as
11 * published by the Free Software Foundation
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
20 * bfad.c Linux driver PCI interface module.
22 #include <linux/module.h>
23 #include <linux/kthread.h>
24 #include <linux/errno.h>
25 #include <linux/sched.h>
26 #include <linux/init.h>
27 #include <linux/fs.h>
28 #include <linux/pci.h>
29 #include <linux/firmware.h>
30 #include <linux/uaccess.h>
31 #include <asm/fcntl.h>
33 #include "bfad_drv.h"
34 #include "bfad_im.h"
35 #include "bfa_fcs.h"
36 #include "bfa_defs.h"
37 #include "bfa.h"
39 BFA_TRC_FILE(LDRV, BFAD);
40 DEFINE_MUTEX(bfad_mutex);
41 LIST_HEAD(bfad_list);
43 static int bfad_inst;
44 static int num_sgpgs_parm;
45 int supported_fc4s;
46 char *host_name, *os_name, *os_patch;
47 int num_rports, num_ios, num_tms;
48 int num_fcxps, num_ufbufs;
49 int reqq_size, rspq_size, num_sgpgs;
50 int rport_del_timeout = BFA_FCS_RPORT_DEF_DEL_TIMEOUT;
51 int bfa_lun_queue_depth = BFAD_LUN_QUEUE_DEPTH;
52 int bfa_io_max_sge = BFAD_IO_MAX_SGE;
53 int bfa_log_level = 3; /* WARNING log level */
54 int ioc_auto_recover = BFA_TRUE;
55 int bfa_linkup_delay = -1;
56 int fdmi_enable = BFA_TRUE;
57 int pcie_max_read_reqsz;
58 int bfa_debugfs_enable = 1;
59 int msix_disable_cb = 0, msix_disable_ct = 0;
60 int max_xfer_size = BFAD_MAX_SECTORS >> 1;
61 int max_rport_logins = BFA_FCS_MAX_RPORT_LOGINS;
63 /* Firmware releated */
64 u32 bfi_image_cb_size, bfi_image_ct_size, bfi_image_ct2_size;
65 u32 *bfi_image_cb, *bfi_image_ct, *bfi_image_ct2;
67 #define BFAD_FW_FILE_CB "cbfw-3.2.5.1.bin"
68 #define BFAD_FW_FILE_CT "ctfw-3.2.5.1.bin"
69 #define BFAD_FW_FILE_CT2 "ct2fw-3.2.5.1.bin"
71 static u32 *bfad_load_fwimg(struct pci_dev *pdev);
72 static void bfad_free_fwimg(void);
73 static void bfad_read_firmware(struct pci_dev *pdev, u32 **bfi_image,
74 u32 *bfi_image_size, char *fw_name);
76 static const char *msix_name_ct[] = {
77 "ctrl",
78 "cpe0", "cpe1", "cpe2", "cpe3",
79 "rme0", "rme1", "rme2", "rme3" };
81 static const char *msix_name_cb[] = {
82 "cpe0", "cpe1", "cpe2", "cpe3",
83 "rme0", "rme1", "rme2", "rme3",
84 "eemc", "elpu0", "elpu1", "epss", "mlpu" };
86 MODULE_FIRMWARE(BFAD_FW_FILE_CB);
87 MODULE_FIRMWARE(BFAD_FW_FILE_CT);
88 MODULE_FIRMWARE(BFAD_FW_FILE_CT2);
90 module_param(os_name, charp, S_IRUGO | S_IWUSR);
91 MODULE_PARM_DESC(os_name, "OS name of the hba host machine");
92 module_param(os_patch, charp, S_IRUGO | S_IWUSR);
93 MODULE_PARM_DESC(os_patch, "OS patch level of the hba host machine");
94 module_param(host_name, charp, S_IRUGO | S_IWUSR);
95 MODULE_PARM_DESC(host_name, "Hostname of the hba host machine");
96 module_param(num_rports, int, S_IRUGO | S_IWUSR);
97 MODULE_PARM_DESC(num_rports, "Max number of rports supported per port "
98 "(physical/logical), default=1024");
99 module_param(num_ios, int, S_IRUGO | S_IWUSR);
100 MODULE_PARM_DESC(num_ios, "Max number of ioim requests, default=2000");
101 module_param(num_tms, int, S_IRUGO | S_IWUSR);
102 MODULE_PARM_DESC(num_tms, "Max number of task im requests, default=128");
103 module_param(num_fcxps, int, S_IRUGO | S_IWUSR);
104 MODULE_PARM_DESC(num_fcxps, "Max number of fcxp requests, default=64");
105 module_param(num_ufbufs, int, S_IRUGO | S_IWUSR);
106 MODULE_PARM_DESC(num_ufbufs, "Max number of unsolicited frame "
107 "buffers, default=64");
108 module_param(reqq_size, int, S_IRUGO | S_IWUSR);
109 MODULE_PARM_DESC(reqq_size, "Max number of request queue elements, "
110 "default=256");
111 module_param(rspq_size, int, S_IRUGO | S_IWUSR);
112 MODULE_PARM_DESC(rspq_size, "Max number of response queue elements, "
113 "default=64");
114 module_param(num_sgpgs, int, S_IRUGO | S_IWUSR);
115 MODULE_PARM_DESC(num_sgpgs, "Number of scatter/gather pages, default=2048");
116 module_param(rport_del_timeout, int, S_IRUGO | S_IWUSR);
117 MODULE_PARM_DESC(rport_del_timeout, "Rport delete timeout, default=90 secs, "
118 "Range[>0]");
119 module_param(bfa_lun_queue_depth, int, S_IRUGO | S_IWUSR);
120 MODULE_PARM_DESC(bfa_lun_queue_depth, "Lun queue depth, default=32, Range[>0]");
121 module_param(bfa_io_max_sge, int, S_IRUGO | S_IWUSR);
122 MODULE_PARM_DESC(bfa_io_max_sge, "Max io scatter/gather elements, default=255");
123 module_param(bfa_log_level, int, S_IRUGO | S_IWUSR);
124 MODULE_PARM_DESC(bfa_log_level, "Driver log level, default=3, "
125 "Range[Critical:1|Error:2|Warning:3|Info:4]");
126 module_param(ioc_auto_recover, int, S_IRUGO | S_IWUSR);
127 MODULE_PARM_DESC(ioc_auto_recover, "IOC auto recovery, default=1, "
128 "Range[off:0|on:1]");
129 module_param(bfa_linkup_delay, int, S_IRUGO | S_IWUSR);
130 MODULE_PARM_DESC(bfa_linkup_delay, "Link up delay, default=30 secs for "
131 "boot port. Otherwise 10 secs in RHEL4 & 0 for "
132 "[RHEL5, SLES10, ESX40] Range[>0]");
133 module_param(msix_disable_cb, int, S_IRUGO | S_IWUSR);
134 MODULE_PARM_DESC(msix_disable_cb, "Disable Message Signaled Interrupts for QLogic-415/425/815/825 cards, default=0 Range[false:0|true:1]");
135 module_param(msix_disable_ct, int, S_IRUGO | S_IWUSR);
136 MODULE_PARM_DESC(msix_disable_ct, "Disable Message Signaled Interrupts if possible for QLogic-1010/1020/804/1007/902/1741 cards, default=0, Range[false:0|true:1]");
137 module_param(fdmi_enable, int, S_IRUGO | S_IWUSR);
138 MODULE_PARM_DESC(fdmi_enable, "Enables fdmi registration, default=1, "
139 "Range[false:0|true:1]");
140 module_param(pcie_max_read_reqsz, int, S_IRUGO | S_IWUSR);
141 MODULE_PARM_DESC(pcie_max_read_reqsz, "PCIe max read request size, default=0 "
142 "(use system setting), Range[128|256|512|1024|2048|4096]");
143 module_param(bfa_debugfs_enable, int, S_IRUGO | S_IWUSR);
144 MODULE_PARM_DESC(bfa_debugfs_enable, "Enables debugfs feature, default=1,"
145 " Range[false:0|true:1]");
146 module_param(max_xfer_size, int, S_IRUGO | S_IWUSR);
147 MODULE_PARM_DESC(max_xfer_size, "default=32MB,"
148 " Range[64k|128k|256k|512k|1024k|2048k]");
149 module_param(max_rport_logins, int, S_IRUGO | S_IWUSR);
150 MODULE_PARM_DESC(max_rport_logins, "Max number of logins to initiator and target rports on a port (physical/logical), default=1024");
152 static void
153 bfad_sm_uninit(struct bfad_s *bfad, enum bfad_sm_event event);
154 static void
155 bfad_sm_created(struct bfad_s *bfad, enum bfad_sm_event event);
156 static void
157 bfad_sm_initializing(struct bfad_s *bfad, enum bfad_sm_event event);
158 static void
159 bfad_sm_operational(struct bfad_s *bfad, enum bfad_sm_event event);
160 static void
161 bfad_sm_stopping(struct bfad_s *bfad, enum bfad_sm_event event);
162 static void
163 bfad_sm_failed(struct bfad_s *bfad, enum bfad_sm_event event);
164 static void
165 bfad_sm_fcs_exit(struct bfad_s *bfad, enum bfad_sm_event event);
168 * Beginning state for the driver instance, awaiting the pci_probe event
170 static void
171 bfad_sm_uninit(struct bfad_s *bfad, enum bfad_sm_event event)
173 bfa_trc(bfad, event);
175 switch (event) {
176 case BFAD_E_CREATE:
177 bfa_sm_set_state(bfad, bfad_sm_created);
178 bfad->bfad_tsk = kthread_create(bfad_worker, (void *) bfad,
179 "%s", "bfad_worker");
180 if (IS_ERR(bfad->bfad_tsk)) {
181 printk(KERN_INFO "bfad[%d]: Kernel thread "
182 "creation failed!\n", bfad->inst_no);
183 bfa_sm_send_event(bfad, BFAD_E_KTHREAD_CREATE_FAILED);
185 bfa_sm_send_event(bfad, BFAD_E_INIT);
186 break;
188 case BFAD_E_STOP:
189 /* Ignore stop; already in uninit */
190 break;
192 default:
193 bfa_sm_fault(bfad, event);
198 * Driver Instance is created, awaiting event INIT to initialize the bfad
200 static void
201 bfad_sm_created(struct bfad_s *bfad, enum bfad_sm_event event)
203 unsigned long flags;
204 bfa_status_t ret;
206 bfa_trc(bfad, event);
208 switch (event) {
209 case BFAD_E_INIT:
210 bfa_sm_set_state(bfad, bfad_sm_initializing);
212 init_completion(&bfad->comp);
214 /* Enable Interrupt and wait bfa_init completion */
215 if (bfad_setup_intr(bfad)) {
216 printk(KERN_WARNING "bfad%d: bfad_setup_intr failed\n",
217 bfad->inst_no);
218 bfa_sm_send_event(bfad, BFAD_E_INIT_FAILED);
219 break;
222 spin_lock_irqsave(&bfad->bfad_lock, flags);
223 bfa_iocfc_init(&bfad->bfa);
224 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
226 /* Set up interrupt handler for each vectors */
227 if ((bfad->bfad_flags & BFAD_MSIX_ON) &&
228 bfad_install_msix_handler(bfad)) {
229 printk(KERN_WARNING "%s: install_msix failed, bfad%d\n",
230 __func__, bfad->inst_no);
233 bfad_init_timer(bfad);
235 wait_for_completion(&bfad->comp);
237 if ((bfad->bfad_flags & BFAD_HAL_INIT_DONE)) {
238 bfa_sm_send_event(bfad, BFAD_E_INIT_SUCCESS);
239 } else {
240 printk(KERN_WARNING
241 "bfa %s: bfa init failed\n",
242 bfad->pci_name);
243 spin_lock_irqsave(&bfad->bfad_lock, flags);
244 bfa_fcs_init(&bfad->bfa_fcs);
245 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
247 ret = bfad_cfg_pport(bfad, BFA_LPORT_ROLE_FCP_IM);
248 if (ret != BFA_STATUS_OK) {
249 init_completion(&bfad->comp);
251 spin_lock_irqsave(&bfad->bfad_lock, flags);
252 bfad->pport.flags |= BFAD_PORT_DELETE;
253 bfa_fcs_exit(&bfad->bfa_fcs);
254 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
256 wait_for_completion(&bfad->comp);
258 bfa_sm_send_event(bfad, BFAD_E_INIT_FAILED);
259 break;
261 bfad->bfad_flags |= BFAD_HAL_INIT_FAIL;
262 bfa_sm_send_event(bfad, BFAD_E_HAL_INIT_FAILED);
265 break;
267 case BFAD_E_KTHREAD_CREATE_FAILED:
268 bfa_sm_set_state(bfad, bfad_sm_uninit);
269 break;
271 default:
272 bfa_sm_fault(bfad, event);
276 static void
277 bfad_sm_initializing(struct bfad_s *bfad, enum bfad_sm_event event)
279 int retval;
280 unsigned long flags;
282 bfa_trc(bfad, event);
284 switch (event) {
285 case BFAD_E_INIT_SUCCESS:
286 kthread_stop(bfad->bfad_tsk);
287 spin_lock_irqsave(&bfad->bfad_lock, flags);
288 bfad->bfad_tsk = NULL;
289 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
291 retval = bfad_start_ops(bfad);
292 if (retval != BFA_STATUS_OK) {
293 bfa_sm_set_state(bfad, bfad_sm_failed);
294 break;
296 bfa_sm_set_state(bfad, bfad_sm_operational);
297 break;
299 case BFAD_E_INIT_FAILED:
300 bfa_sm_set_state(bfad, bfad_sm_uninit);
301 kthread_stop(bfad->bfad_tsk);
302 spin_lock_irqsave(&bfad->bfad_lock, flags);
303 bfad->bfad_tsk = NULL;
304 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
305 break;
307 case BFAD_E_HAL_INIT_FAILED:
308 bfa_sm_set_state(bfad, bfad_sm_failed);
309 break;
310 default:
311 bfa_sm_fault(bfad, event);
315 static void
316 bfad_sm_failed(struct bfad_s *bfad, enum bfad_sm_event event)
318 int retval;
320 bfa_trc(bfad, event);
322 switch (event) {
323 case BFAD_E_INIT_SUCCESS:
324 retval = bfad_start_ops(bfad);
325 if (retval != BFA_STATUS_OK)
326 break;
327 bfa_sm_set_state(bfad, bfad_sm_operational);
328 break;
330 case BFAD_E_STOP:
331 bfa_sm_set_state(bfad, bfad_sm_fcs_exit);
332 bfa_sm_send_event(bfad, BFAD_E_FCS_EXIT_COMP);
333 break;
335 case BFAD_E_EXIT_COMP:
336 bfa_sm_set_state(bfad, bfad_sm_uninit);
337 bfad_remove_intr(bfad);
338 del_timer_sync(&bfad->hal_tmo);
339 break;
341 default:
342 bfa_sm_fault(bfad, event);
346 static void
347 bfad_sm_operational(struct bfad_s *bfad, enum bfad_sm_event event)
349 bfa_trc(bfad, event);
351 switch (event) {
352 case BFAD_E_STOP:
353 bfa_sm_set_state(bfad, bfad_sm_fcs_exit);
354 bfad_fcs_stop(bfad);
355 break;
357 default:
358 bfa_sm_fault(bfad, event);
362 static void
363 bfad_sm_fcs_exit(struct bfad_s *bfad, enum bfad_sm_event event)
365 bfa_trc(bfad, event);
367 switch (event) {
368 case BFAD_E_FCS_EXIT_COMP:
369 bfa_sm_set_state(bfad, bfad_sm_stopping);
370 bfad_stop(bfad);
371 break;
373 default:
374 bfa_sm_fault(bfad, event);
378 static void
379 bfad_sm_stopping(struct bfad_s *bfad, enum bfad_sm_event event)
381 bfa_trc(bfad, event);
383 switch (event) {
384 case BFAD_E_EXIT_COMP:
385 bfa_sm_set_state(bfad, bfad_sm_uninit);
386 bfad_remove_intr(bfad);
387 del_timer_sync(&bfad->hal_tmo);
388 bfad_im_probe_undo(bfad);
389 bfad->bfad_flags &= ~BFAD_FC4_PROBE_DONE;
390 bfad_uncfg_pport(bfad);
391 break;
393 default:
394 bfa_sm_fault(bfad, event);
395 break;
400 * BFA callbacks
402 void
403 bfad_hcb_comp(void *arg, bfa_status_t status)
405 struct bfad_hal_comp *fcomp = (struct bfad_hal_comp *)arg;
407 fcomp->status = status;
408 complete(&fcomp->comp);
412 * bfa_init callback
414 void
415 bfa_cb_init(void *drv, bfa_status_t init_status)
417 struct bfad_s *bfad = drv;
419 if (init_status == BFA_STATUS_OK) {
420 bfad->bfad_flags |= BFAD_HAL_INIT_DONE;
423 * If BFAD_HAL_INIT_FAIL flag is set:
424 * Wake up the kernel thread to start
425 * the bfad operations after HAL init done
427 if ((bfad->bfad_flags & BFAD_HAL_INIT_FAIL)) {
428 bfad->bfad_flags &= ~BFAD_HAL_INIT_FAIL;
429 wake_up_process(bfad->bfad_tsk);
433 complete(&bfad->comp);
437 * BFA_FCS callbacks
439 struct bfad_port_s *
440 bfa_fcb_lport_new(struct bfad_s *bfad, struct bfa_fcs_lport_s *port,
441 enum bfa_lport_role roles, struct bfad_vf_s *vf_drv,
442 struct bfad_vport_s *vp_drv)
444 bfa_status_t rc;
445 struct bfad_port_s *port_drv;
447 if (!vp_drv && !vf_drv) {
448 port_drv = &bfad->pport;
449 port_drv->pvb_type = BFAD_PORT_PHYS_BASE;
450 } else if (!vp_drv && vf_drv) {
451 port_drv = &vf_drv->base_port;
452 port_drv->pvb_type = BFAD_PORT_VF_BASE;
453 } else if (vp_drv && !vf_drv) {
454 port_drv = &vp_drv->drv_port;
455 port_drv->pvb_type = BFAD_PORT_PHYS_VPORT;
456 } else {
457 port_drv = &vp_drv->drv_port;
458 port_drv->pvb_type = BFAD_PORT_VF_VPORT;
461 port_drv->fcs_port = port;
462 port_drv->roles = roles;
464 if (roles & BFA_LPORT_ROLE_FCP_IM) {
465 rc = bfad_im_port_new(bfad, port_drv);
466 if (rc != BFA_STATUS_OK) {
467 bfad_im_port_delete(bfad, port_drv);
468 port_drv = NULL;
472 return port_drv;
476 * FCS RPORT alloc callback, after successful PLOGI by FCS
478 bfa_status_t
479 bfa_fcb_rport_alloc(struct bfad_s *bfad, struct bfa_fcs_rport_s **rport,
480 struct bfad_rport_s **rport_drv)
482 bfa_status_t rc = BFA_STATUS_OK;
484 *rport_drv = kzalloc(sizeof(struct bfad_rport_s), GFP_ATOMIC);
485 if (*rport_drv == NULL) {
486 rc = BFA_STATUS_ENOMEM;
487 goto ext;
490 *rport = &(*rport_drv)->fcs_rport;
492 ext:
493 return rc;
497 * FCS PBC VPORT Create
499 void
500 bfa_fcb_pbc_vport_create(struct bfad_s *bfad, struct bfi_pbc_vport_s pbc_vport)
503 struct bfa_lport_cfg_s port_cfg = {0};
504 struct bfad_vport_s *vport;
505 int rc;
507 vport = kzalloc(sizeof(struct bfad_vport_s), GFP_ATOMIC);
508 if (!vport) {
509 bfa_trc(bfad, 0);
510 return;
513 vport->drv_port.bfad = bfad;
514 port_cfg.roles = BFA_LPORT_ROLE_FCP_IM;
515 port_cfg.pwwn = pbc_vport.vp_pwwn;
516 port_cfg.nwwn = pbc_vport.vp_nwwn;
517 port_cfg.preboot_vp = BFA_TRUE;
519 rc = bfa_fcs_pbc_vport_create(&vport->fcs_vport, &bfad->bfa_fcs, 0,
520 &port_cfg, vport);
522 if (rc != BFA_STATUS_OK) {
523 bfa_trc(bfad, 0);
524 return;
527 list_add_tail(&vport->list_entry, &bfad->pbc_vport_list);
530 void
531 bfad_hal_mem_release(struct bfad_s *bfad)
533 struct bfa_meminfo_s *hal_meminfo = &bfad->meminfo;
534 struct bfa_mem_dma_s *dma_info, *dma_elem;
535 struct bfa_mem_kva_s *kva_info, *kva_elem;
536 struct list_head *dm_qe, *km_qe;
538 dma_info = &hal_meminfo->dma_info;
539 kva_info = &hal_meminfo->kva_info;
541 /* Iterate through the KVA meminfo queue */
542 list_for_each(km_qe, &kva_info->qe) {
543 kva_elem = (struct bfa_mem_kva_s *) km_qe;
544 vfree(kva_elem->kva);
547 /* Iterate through the DMA meminfo queue */
548 list_for_each(dm_qe, &dma_info->qe) {
549 dma_elem = (struct bfa_mem_dma_s *) dm_qe;
550 dma_free_coherent(&bfad->pcidev->dev,
551 dma_elem->mem_len, dma_elem->kva,
552 (dma_addr_t) dma_elem->dma);
555 memset(hal_meminfo, 0, sizeof(struct bfa_meminfo_s));
558 void
559 bfad_update_hal_cfg(struct bfa_iocfc_cfg_s *bfa_cfg)
561 if (num_rports > 0)
562 bfa_cfg->fwcfg.num_rports = num_rports;
563 if (num_ios > 0)
564 bfa_cfg->fwcfg.num_ioim_reqs = num_ios;
565 if (num_tms > 0)
566 bfa_cfg->fwcfg.num_tskim_reqs = num_tms;
567 if (num_fcxps > 0 && num_fcxps <= BFA_FCXP_MAX)
568 bfa_cfg->fwcfg.num_fcxp_reqs = num_fcxps;
569 if (num_ufbufs > 0 && num_ufbufs <= BFA_UF_MAX)
570 bfa_cfg->fwcfg.num_uf_bufs = num_ufbufs;
571 if (reqq_size > 0)
572 bfa_cfg->drvcfg.num_reqq_elems = reqq_size;
573 if (rspq_size > 0)
574 bfa_cfg->drvcfg.num_rspq_elems = rspq_size;
575 if (num_sgpgs > 0 && num_sgpgs <= BFA_SGPG_MAX)
576 bfa_cfg->drvcfg.num_sgpgs = num_sgpgs;
579 * populate the hal values back to the driver for sysfs use.
580 * otherwise, the default values will be shown as 0 in sysfs
582 num_rports = bfa_cfg->fwcfg.num_rports;
583 num_ios = bfa_cfg->fwcfg.num_ioim_reqs;
584 num_tms = bfa_cfg->fwcfg.num_tskim_reqs;
585 num_fcxps = bfa_cfg->fwcfg.num_fcxp_reqs;
586 num_ufbufs = bfa_cfg->fwcfg.num_uf_bufs;
587 reqq_size = bfa_cfg->drvcfg.num_reqq_elems;
588 rspq_size = bfa_cfg->drvcfg.num_rspq_elems;
589 num_sgpgs = bfa_cfg->drvcfg.num_sgpgs;
592 bfa_status_t
593 bfad_hal_mem_alloc(struct bfad_s *bfad)
595 struct bfa_meminfo_s *hal_meminfo = &bfad->meminfo;
596 struct bfa_mem_dma_s *dma_info, *dma_elem;
597 struct bfa_mem_kva_s *kva_info, *kva_elem;
598 struct list_head *dm_qe, *km_qe;
599 bfa_status_t rc = BFA_STATUS_OK;
600 dma_addr_t phys_addr;
602 bfa_cfg_get_default(&bfad->ioc_cfg);
603 bfad_update_hal_cfg(&bfad->ioc_cfg);
604 bfad->cfg_data.ioc_queue_depth = bfad->ioc_cfg.fwcfg.num_ioim_reqs;
605 bfa_cfg_get_meminfo(&bfad->ioc_cfg, hal_meminfo, &bfad->bfa);
607 dma_info = &hal_meminfo->dma_info;
608 kva_info = &hal_meminfo->kva_info;
610 /* Iterate through the KVA meminfo queue */
611 list_for_each(km_qe, &kva_info->qe) {
612 kva_elem = (struct bfa_mem_kva_s *) km_qe;
613 kva_elem->kva = vzalloc(kva_elem->mem_len);
614 if (kva_elem->kva == NULL) {
615 bfad_hal_mem_release(bfad);
616 rc = BFA_STATUS_ENOMEM;
617 goto ext;
621 /* Iterate through the DMA meminfo queue */
622 list_for_each(dm_qe, &dma_info->qe) {
623 dma_elem = (struct bfa_mem_dma_s *) dm_qe;
624 dma_elem->kva = dma_alloc_coherent(&bfad->pcidev->dev,
625 dma_elem->mem_len,
626 &phys_addr, GFP_KERNEL);
627 if (dma_elem->kva == NULL) {
628 bfad_hal_mem_release(bfad);
629 rc = BFA_STATUS_ENOMEM;
630 goto ext;
632 dma_elem->dma = phys_addr;
633 memset(dma_elem->kva, 0, dma_elem->mem_len);
635 ext:
636 return rc;
640 * Create a vport under a vf.
642 bfa_status_t
643 bfad_vport_create(struct bfad_s *bfad, u16 vf_id,
644 struct bfa_lport_cfg_s *port_cfg, struct device *dev)
646 struct bfad_vport_s *vport;
647 int rc = BFA_STATUS_OK;
648 unsigned long flags;
649 struct completion fcomp;
651 vport = kzalloc(sizeof(struct bfad_vport_s), GFP_KERNEL);
652 if (!vport) {
653 rc = BFA_STATUS_ENOMEM;
654 goto ext;
657 vport->drv_port.bfad = bfad;
658 spin_lock_irqsave(&bfad->bfad_lock, flags);
659 rc = bfa_fcs_vport_create(&vport->fcs_vport, &bfad->bfa_fcs, vf_id,
660 port_cfg, vport);
661 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
663 if (rc != BFA_STATUS_OK)
664 goto ext_free_vport;
666 if (port_cfg->roles & BFA_LPORT_ROLE_FCP_IM) {
667 rc = bfad_im_scsi_host_alloc(bfad, vport->drv_port.im_port,
668 dev);
669 if (rc != BFA_STATUS_OK)
670 goto ext_free_fcs_vport;
673 spin_lock_irqsave(&bfad->bfad_lock, flags);
674 bfa_fcs_vport_start(&vport->fcs_vport);
675 list_add_tail(&vport->list_entry, &bfad->vport_list);
676 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
678 return BFA_STATUS_OK;
680 ext_free_fcs_vport:
681 spin_lock_irqsave(&bfad->bfad_lock, flags);
682 vport->comp_del = &fcomp;
683 init_completion(vport->comp_del);
684 bfa_fcs_vport_delete(&vport->fcs_vport);
685 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
686 wait_for_completion(vport->comp_del);
687 ext_free_vport:
688 kfree(vport);
689 ext:
690 return rc;
693 void
694 bfad_bfa_tmo(struct timer_list *t)
696 struct bfad_s *bfad = from_timer(bfad, t, hal_tmo);
697 unsigned long flags;
698 struct list_head doneq;
700 spin_lock_irqsave(&bfad->bfad_lock, flags);
702 bfa_timer_beat(&bfad->bfa.timer_mod);
704 bfa_comp_deq(&bfad->bfa, &doneq);
705 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
707 if (!list_empty(&doneq)) {
708 bfa_comp_process(&bfad->bfa, &doneq);
709 spin_lock_irqsave(&bfad->bfad_lock, flags);
710 bfa_comp_free(&bfad->bfa, &doneq);
711 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
714 mod_timer(&bfad->hal_tmo,
715 jiffies + msecs_to_jiffies(BFA_TIMER_FREQ));
718 void
719 bfad_init_timer(struct bfad_s *bfad)
721 timer_setup(&bfad->hal_tmo, bfad_bfa_tmo, 0);
723 mod_timer(&bfad->hal_tmo,
724 jiffies + msecs_to_jiffies(BFA_TIMER_FREQ));
728 bfad_pci_init(struct pci_dev *pdev, struct bfad_s *bfad)
730 int rc = -ENODEV;
732 if (pci_enable_device(pdev)) {
733 printk(KERN_ERR "pci_enable_device fail %p\n", pdev);
734 goto out;
737 if (pci_request_regions(pdev, BFAD_DRIVER_NAME))
738 goto out_disable_device;
740 pci_set_master(pdev);
743 if ((pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) ||
744 (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)) != 0)) {
745 if ((pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0) ||
746 (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)) {
747 printk(KERN_ERR "pci_set_dma_mask fail %p\n", pdev);
748 goto out_release_region;
752 /* Enable PCIE Advanced Error Recovery (AER) if kernel supports */
753 pci_enable_pcie_error_reporting(pdev);
755 bfad->pci_bar0_kva = pci_iomap(pdev, 0, pci_resource_len(pdev, 0));
756 bfad->pci_bar2_kva = pci_iomap(pdev, 2, pci_resource_len(pdev, 2));
758 if (bfad->pci_bar0_kva == NULL) {
759 printk(KERN_ERR "Fail to map bar0\n");
760 goto out_release_region;
763 bfad->hal_pcidev.pci_slot = PCI_SLOT(pdev->devfn);
764 bfad->hal_pcidev.pci_func = PCI_FUNC(pdev->devfn);
765 bfad->hal_pcidev.pci_bar_kva = bfad->pci_bar0_kva;
766 bfad->hal_pcidev.device_id = pdev->device;
767 bfad->hal_pcidev.ssid = pdev->subsystem_device;
768 bfad->pci_name = pci_name(pdev);
770 bfad->pci_attr.vendor_id = pdev->vendor;
771 bfad->pci_attr.device_id = pdev->device;
772 bfad->pci_attr.ssid = pdev->subsystem_device;
773 bfad->pci_attr.ssvid = pdev->subsystem_vendor;
774 bfad->pci_attr.pcifn = PCI_FUNC(pdev->devfn);
776 bfad->pcidev = pdev;
778 /* Adjust PCIe Maximum Read Request Size */
779 if (pci_is_pcie(pdev) && pcie_max_read_reqsz) {
780 if (pcie_max_read_reqsz >= 128 &&
781 pcie_max_read_reqsz <= 4096 &&
782 is_power_of_2(pcie_max_read_reqsz)) {
783 int max_rq = pcie_get_readrq(pdev);
784 printk(KERN_WARNING "BFA[%s]: "
785 "pcie_max_read_request_size is %d, "
786 "reset to %d\n", bfad->pci_name, max_rq,
787 pcie_max_read_reqsz);
788 pcie_set_readrq(pdev, pcie_max_read_reqsz);
789 } else {
790 printk(KERN_WARNING "BFA[%s]: invalid "
791 "pcie_max_read_request_size %d ignored\n",
792 bfad->pci_name, pcie_max_read_reqsz);
796 pci_save_state(pdev);
798 return 0;
800 out_release_region:
801 pci_release_regions(pdev);
802 out_disable_device:
803 pci_disable_device(pdev);
804 out:
805 return rc;
808 void
809 bfad_pci_uninit(struct pci_dev *pdev, struct bfad_s *bfad)
811 pci_iounmap(pdev, bfad->pci_bar0_kva);
812 pci_iounmap(pdev, bfad->pci_bar2_kva);
813 pci_release_regions(pdev);
814 /* Disable PCIE Advanced Error Recovery (AER) */
815 pci_disable_pcie_error_reporting(pdev);
816 pci_disable_device(pdev);
819 bfa_status_t
820 bfad_drv_init(struct bfad_s *bfad)
822 bfa_status_t rc;
823 unsigned long flags;
825 bfad->cfg_data.rport_del_timeout = rport_del_timeout;
826 bfad->cfg_data.lun_queue_depth = bfa_lun_queue_depth;
827 bfad->cfg_data.io_max_sge = bfa_io_max_sge;
828 bfad->cfg_data.binding_method = FCP_PWWN_BINDING;
830 rc = bfad_hal_mem_alloc(bfad);
831 if (rc != BFA_STATUS_OK) {
832 printk(KERN_WARNING "bfad%d bfad_hal_mem_alloc failure\n",
833 bfad->inst_no);
834 printk(KERN_WARNING
835 "Not enough memory to attach all QLogic BR-series HBA ports. System may need more memory.\n");
836 return BFA_STATUS_FAILED;
839 bfad->bfa.trcmod = bfad->trcmod;
840 bfad->bfa.plog = &bfad->plog_buf;
841 bfa_plog_init(&bfad->plog_buf);
842 bfa_plog_str(&bfad->plog_buf, BFA_PL_MID_DRVR, BFA_PL_EID_DRIVER_START,
843 0, "Driver Attach");
845 bfa_attach(&bfad->bfa, bfad, &bfad->ioc_cfg, &bfad->meminfo,
846 &bfad->hal_pcidev);
848 /* FCS INIT */
849 spin_lock_irqsave(&bfad->bfad_lock, flags);
850 bfad->bfa_fcs.trcmod = bfad->trcmod;
851 bfa_fcs_attach(&bfad->bfa_fcs, &bfad->bfa, bfad, BFA_FALSE);
852 bfad->bfa_fcs.fdmi_enabled = fdmi_enable;
853 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
855 bfad->bfad_flags |= BFAD_DRV_INIT_DONE;
857 return BFA_STATUS_OK;
860 void
861 bfad_drv_uninit(struct bfad_s *bfad)
863 unsigned long flags;
865 spin_lock_irqsave(&bfad->bfad_lock, flags);
866 init_completion(&bfad->comp);
867 bfa_iocfc_stop(&bfad->bfa);
868 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
869 wait_for_completion(&bfad->comp);
871 del_timer_sync(&bfad->hal_tmo);
872 bfa_isr_disable(&bfad->bfa);
873 bfa_detach(&bfad->bfa);
874 bfad_remove_intr(bfad);
875 bfad_hal_mem_release(bfad);
877 bfad->bfad_flags &= ~BFAD_DRV_INIT_DONE;
880 void
881 bfad_drv_start(struct bfad_s *bfad)
883 unsigned long flags;
885 spin_lock_irqsave(&bfad->bfad_lock, flags);
886 bfa_iocfc_start(&bfad->bfa);
887 bfa_fcs_pbc_vport_init(&bfad->bfa_fcs);
888 bfa_fcs_fabric_modstart(&bfad->bfa_fcs);
889 bfad->bfad_flags |= BFAD_HAL_START_DONE;
890 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
892 if (bfad->im)
893 flush_workqueue(bfad->im->drv_workq);
896 void
897 bfad_fcs_stop(struct bfad_s *bfad)
899 unsigned long flags;
901 spin_lock_irqsave(&bfad->bfad_lock, flags);
902 init_completion(&bfad->comp);
903 bfad->pport.flags |= BFAD_PORT_DELETE;
904 bfa_fcs_exit(&bfad->bfa_fcs);
905 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
906 wait_for_completion(&bfad->comp);
908 bfa_sm_send_event(bfad, BFAD_E_FCS_EXIT_COMP);
911 void
912 bfad_stop(struct bfad_s *bfad)
914 unsigned long flags;
916 spin_lock_irqsave(&bfad->bfad_lock, flags);
917 init_completion(&bfad->comp);
918 bfa_iocfc_stop(&bfad->bfa);
919 bfad->bfad_flags &= ~BFAD_HAL_START_DONE;
920 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
921 wait_for_completion(&bfad->comp);
923 bfa_sm_send_event(bfad, BFAD_E_EXIT_COMP);
926 bfa_status_t
927 bfad_cfg_pport(struct bfad_s *bfad, enum bfa_lport_role role)
929 int rc = BFA_STATUS_OK;
931 /* Allocate scsi_host for the physical port */
932 if ((supported_fc4s & BFA_LPORT_ROLE_FCP_IM) &&
933 (role & BFA_LPORT_ROLE_FCP_IM)) {
934 if (bfad->pport.im_port == NULL) {
935 rc = BFA_STATUS_FAILED;
936 goto out;
939 rc = bfad_im_scsi_host_alloc(bfad, bfad->pport.im_port,
940 &bfad->pcidev->dev);
941 if (rc != BFA_STATUS_OK)
942 goto out;
944 bfad->pport.roles |= BFA_LPORT_ROLE_FCP_IM;
947 bfad->bfad_flags |= BFAD_CFG_PPORT_DONE;
949 out:
950 return rc;
953 void
954 bfad_uncfg_pport(struct bfad_s *bfad)
956 if ((supported_fc4s & BFA_LPORT_ROLE_FCP_IM) &&
957 (bfad->pport.roles & BFA_LPORT_ROLE_FCP_IM)) {
958 bfad_im_scsi_host_free(bfad, bfad->pport.im_port);
959 bfad_im_port_clean(bfad->pport.im_port);
960 kfree(bfad->pport.im_port);
961 bfad->pport.roles &= ~BFA_LPORT_ROLE_FCP_IM;
964 bfad->bfad_flags &= ~BFAD_CFG_PPORT_DONE;
967 bfa_status_t
968 bfad_start_ops(struct bfad_s *bfad) {
970 int retval;
971 unsigned long flags;
972 struct bfad_vport_s *vport, *vport_new;
973 struct bfa_fcs_driver_info_s driver_info;
975 /* Limit min/max. xfer size to [64k-32MB] */
976 if (max_xfer_size < BFAD_MIN_SECTORS >> 1)
977 max_xfer_size = BFAD_MIN_SECTORS >> 1;
978 if (max_xfer_size > BFAD_MAX_SECTORS >> 1)
979 max_xfer_size = BFAD_MAX_SECTORS >> 1;
981 /* Fill the driver_info info to fcs*/
982 memset(&driver_info, 0, sizeof(driver_info));
983 strlcpy(driver_info.version, BFAD_DRIVER_VERSION,
984 sizeof(driver_info.version));
985 if (host_name)
986 strlcpy(driver_info.host_machine_name, host_name,
987 sizeof(driver_info.host_machine_name));
988 if (os_name)
989 strlcpy(driver_info.host_os_name, os_name,
990 sizeof(driver_info.host_os_name));
991 if (os_patch)
992 strlcpy(driver_info.host_os_patch, os_patch,
993 sizeof(driver_info.host_os_patch));
995 strlcpy(driver_info.os_device_name, bfad->pci_name,
996 sizeof(driver_info.os_device_name));
998 /* FCS driver info init */
999 spin_lock_irqsave(&bfad->bfad_lock, flags);
1000 bfa_fcs_driver_info_init(&bfad->bfa_fcs, &driver_info);
1002 if (bfad->bfad_flags & BFAD_CFG_PPORT_DONE)
1003 bfa_fcs_update_cfg(&bfad->bfa_fcs);
1004 else
1005 bfa_fcs_init(&bfad->bfa_fcs);
1007 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1009 if (!(bfad->bfad_flags & BFAD_CFG_PPORT_DONE)) {
1010 retval = bfad_cfg_pport(bfad, BFA_LPORT_ROLE_FCP_IM);
1011 if (retval != BFA_STATUS_OK)
1012 return BFA_STATUS_FAILED;
1015 /* Setup fc host fixed attribute if the lk supports */
1016 bfad_fc_host_init(bfad->pport.im_port);
1018 /* BFAD level FC4 IM specific resource allocation */
1019 retval = bfad_im_probe(bfad);
1020 if (retval != BFA_STATUS_OK) {
1021 printk(KERN_WARNING "bfad_im_probe failed\n");
1022 if (bfa_sm_cmp_state(bfad, bfad_sm_initializing))
1023 bfa_sm_set_state(bfad, bfad_sm_failed);
1024 return BFA_STATUS_FAILED;
1025 } else
1026 bfad->bfad_flags |= BFAD_FC4_PROBE_DONE;
1028 bfad_drv_start(bfad);
1030 /* Complete pbc vport create */
1031 list_for_each_entry_safe(vport, vport_new, &bfad->pbc_vport_list,
1032 list_entry) {
1033 struct fc_vport_identifiers vid;
1034 struct fc_vport *fc_vport;
1035 char pwwn_buf[BFA_STRING_32];
1037 memset(&vid, 0, sizeof(vid));
1038 vid.roles = FC_PORT_ROLE_FCP_INITIATOR;
1039 vid.vport_type = FC_PORTTYPE_NPIV;
1040 vid.disable = false;
1041 vid.node_name = wwn_to_u64((u8 *)
1042 (&((vport->fcs_vport).lport.port_cfg.nwwn)));
1043 vid.port_name = wwn_to_u64((u8 *)
1044 (&((vport->fcs_vport).lport.port_cfg.pwwn)));
1045 fc_vport = fc_vport_create(bfad->pport.im_port->shost, 0, &vid);
1046 if (!fc_vport) {
1047 wwn2str(pwwn_buf, vid.port_name);
1048 printk(KERN_WARNING "bfad%d: failed to create pbc vport"
1049 " %s\n", bfad->inst_no, pwwn_buf);
1051 list_del(&vport->list_entry);
1052 kfree(vport);
1056 * If bfa_linkup_delay is set to -1 default; try to retrive the
1057 * value using the bfad_get_linkup_delay(); else use the
1058 * passed in module param value as the bfa_linkup_delay.
1060 if (bfa_linkup_delay < 0) {
1061 bfa_linkup_delay = bfad_get_linkup_delay(bfad);
1062 bfad_rport_online_wait(bfad);
1063 bfa_linkup_delay = -1;
1064 } else
1065 bfad_rport_online_wait(bfad);
1067 BFA_LOG(KERN_INFO, bfad, bfa_log_level, "bfa device claimed\n");
1069 return BFA_STATUS_OK;
1073 bfad_worker(void *ptr)
1075 struct bfad_s *bfad = ptr;
1076 unsigned long flags;
1078 if (kthread_should_stop())
1079 return 0;
1081 /* Send event BFAD_E_INIT_SUCCESS */
1082 bfa_sm_send_event(bfad, BFAD_E_INIT_SUCCESS);
1084 spin_lock_irqsave(&bfad->bfad_lock, flags);
1085 bfad->bfad_tsk = NULL;
1086 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1088 return 0;
1092 * BFA driver interrupt functions
1094 irqreturn_t
1095 bfad_intx(int irq, void *dev_id)
1097 struct bfad_s *bfad = dev_id;
1098 struct list_head doneq;
1099 unsigned long flags;
1100 bfa_boolean_t rc;
1102 spin_lock_irqsave(&bfad->bfad_lock, flags);
1103 rc = bfa_intx(&bfad->bfa);
1104 if (!rc) {
1105 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1106 return IRQ_NONE;
1109 bfa_comp_deq(&bfad->bfa, &doneq);
1110 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1112 if (!list_empty(&doneq)) {
1113 bfa_comp_process(&bfad->bfa, &doneq);
1115 spin_lock_irqsave(&bfad->bfad_lock, flags);
1116 bfa_comp_free(&bfad->bfa, &doneq);
1117 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1120 return IRQ_HANDLED;
1124 static irqreturn_t
1125 bfad_msix(int irq, void *dev_id)
1127 struct bfad_msix_s *vec = dev_id;
1128 struct bfad_s *bfad = vec->bfad;
1129 struct list_head doneq;
1130 unsigned long flags;
1132 spin_lock_irqsave(&bfad->bfad_lock, flags);
1134 bfa_msix(&bfad->bfa, vec->msix.entry);
1135 bfa_comp_deq(&bfad->bfa, &doneq);
1136 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1138 if (!list_empty(&doneq)) {
1139 bfa_comp_process(&bfad->bfa, &doneq);
1141 spin_lock_irqsave(&bfad->bfad_lock, flags);
1142 bfa_comp_free(&bfad->bfa, &doneq);
1143 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1146 return IRQ_HANDLED;
1150 * Initialize the MSIX entry table.
1152 static void
1153 bfad_init_msix_entry(struct bfad_s *bfad, struct msix_entry *msix_entries,
1154 int mask, int max_bit)
1156 int i;
1157 int match = 0x00000001;
1159 for (i = 0, bfad->nvec = 0; i < MAX_MSIX_ENTRY; i++) {
1160 if (mask & match) {
1161 bfad->msix_tab[bfad->nvec].msix.entry = i;
1162 bfad->msix_tab[bfad->nvec].bfad = bfad;
1163 msix_entries[bfad->nvec].entry = i;
1164 bfad->nvec++;
1167 match <<= 1;
1173 bfad_install_msix_handler(struct bfad_s *bfad)
1175 int i, error = 0;
1177 for (i = 0; i < bfad->nvec; i++) {
1178 sprintf(bfad->msix_tab[i].name, "bfa-%s-%s",
1179 bfad->pci_name,
1180 ((bfa_asic_id_cb(bfad->hal_pcidev.device_id)) ?
1181 msix_name_cb[i] : msix_name_ct[i]));
1183 error = request_irq(bfad->msix_tab[i].msix.vector,
1184 (irq_handler_t) bfad_msix, 0,
1185 bfad->msix_tab[i].name, &bfad->msix_tab[i]);
1186 bfa_trc(bfad, i);
1187 bfa_trc(bfad, bfad->msix_tab[i].msix.vector);
1188 if (error) {
1189 int j;
1191 for (j = 0; j < i; j++)
1192 free_irq(bfad->msix_tab[j].msix.vector,
1193 &bfad->msix_tab[j]);
1195 bfad->bfad_flags &= ~BFAD_MSIX_ON;
1196 pci_disable_msix(bfad->pcidev);
1198 return 1;
1202 return 0;
1206 * Setup MSIX based interrupt.
1209 bfad_setup_intr(struct bfad_s *bfad)
1211 int error;
1212 u32 mask = 0, i, num_bit = 0, max_bit = 0;
1213 struct msix_entry msix_entries[MAX_MSIX_ENTRY];
1214 struct pci_dev *pdev = bfad->pcidev;
1215 u16 reg;
1217 /* Call BFA to get the msix map for this PCI function. */
1218 bfa_msix_getvecs(&bfad->bfa, &mask, &num_bit, &max_bit);
1220 /* Set up the msix entry table */
1221 bfad_init_msix_entry(bfad, msix_entries, mask, max_bit);
1223 if ((bfa_asic_id_ctc(pdev->device) && !msix_disable_ct) ||
1224 (bfa_asic_id_cb(pdev->device) && !msix_disable_cb)) {
1226 error = pci_enable_msix_exact(bfad->pcidev,
1227 msix_entries, bfad->nvec);
1228 /* In CT1 & CT2, try to allocate just one vector */
1229 if (error == -ENOSPC && bfa_asic_id_ctc(pdev->device)) {
1230 printk(KERN_WARNING "bfa %s: trying one msix "
1231 "vector failed to allocate %d[%d]\n",
1232 bfad->pci_name, bfad->nvec, error);
1233 bfad->nvec = 1;
1234 error = pci_enable_msix_exact(bfad->pcidev,
1235 msix_entries, 1);
1238 if (error) {
1239 printk(KERN_WARNING "bfad%d: "
1240 "pci_enable_msix_exact failed (%d), "
1241 "use line based.\n",
1242 bfad->inst_no, error);
1243 goto line_based;
1246 /* Disable INTX in MSI-X mode */
1247 pci_read_config_word(pdev, PCI_COMMAND, &reg);
1249 if (!(reg & PCI_COMMAND_INTX_DISABLE))
1250 pci_write_config_word(pdev, PCI_COMMAND,
1251 reg | PCI_COMMAND_INTX_DISABLE);
1253 /* Save the vectors */
1254 for (i = 0; i < bfad->nvec; i++) {
1255 bfa_trc(bfad, msix_entries[i].vector);
1256 bfad->msix_tab[i].msix.vector = msix_entries[i].vector;
1259 bfa_msix_init(&bfad->bfa, bfad->nvec);
1261 bfad->bfad_flags |= BFAD_MSIX_ON;
1263 return 0;
1266 line_based:
1267 error = request_irq(bfad->pcidev->irq, (irq_handler_t)bfad_intx,
1268 BFAD_IRQ_FLAGS, BFAD_DRIVER_NAME, bfad);
1269 if (error)
1270 return error;
1272 bfad->bfad_flags |= BFAD_INTX_ON;
1274 return 0;
1277 void
1278 bfad_remove_intr(struct bfad_s *bfad)
1280 int i;
1282 if (bfad->bfad_flags & BFAD_MSIX_ON) {
1283 for (i = 0; i < bfad->nvec; i++)
1284 free_irq(bfad->msix_tab[i].msix.vector,
1285 &bfad->msix_tab[i]);
1287 pci_disable_msix(bfad->pcidev);
1288 bfad->bfad_flags &= ~BFAD_MSIX_ON;
1289 } else if (bfad->bfad_flags & BFAD_INTX_ON) {
1290 free_irq(bfad->pcidev->irq, bfad);
1295 * PCI probe entry.
1298 bfad_pci_probe(struct pci_dev *pdev, const struct pci_device_id *pid)
1300 struct bfad_s *bfad;
1301 int error = -ENODEV, retval, i;
1303 /* For single port cards - only claim function 0 */
1304 if ((pdev->device == BFA_PCI_DEVICE_ID_FC_8G1P) &&
1305 (PCI_FUNC(pdev->devfn) != 0))
1306 return -ENODEV;
1308 bfad = kzalloc(sizeof(struct bfad_s), GFP_KERNEL);
1309 if (!bfad) {
1310 error = -ENOMEM;
1311 goto out;
1314 bfad->trcmod = kzalloc(sizeof(struct bfa_trc_mod_s), GFP_KERNEL);
1315 if (!bfad->trcmod) {
1316 printk(KERN_WARNING "Error alloc trace buffer!\n");
1317 error = -ENOMEM;
1318 goto out_alloc_trace_failure;
1321 /* TRACE INIT */
1322 bfa_trc_init(bfad->trcmod);
1323 bfa_trc(bfad, bfad_inst);
1325 /* AEN INIT */
1326 INIT_LIST_HEAD(&bfad->free_aen_q);
1327 INIT_LIST_HEAD(&bfad->active_aen_q);
1328 for (i = 0; i < BFA_AEN_MAX_ENTRY; i++)
1329 list_add_tail(&bfad->aen_list[i].qe, &bfad->free_aen_q);
1331 if (!(bfad_load_fwimg(pdev))) {
1332 kfree(bfad->trcmod);
1333 goto out_alloc_trace_failure;
1336 retval = bfad_pci_init(pdev, bfad);
1337 if (retval) {
1338 printk(KERN_WARNING "bfad_pci_init failure!\n");
1339 error = retval;
1340 goto out_pci_init_failure;
1343 mutex_lock(&bfad_mutex);
1344 bfad->inst_no = bfad_inst++;
1345 list_add_tail(&bfad->list_entry, &bfad_list);
1346 mutex_unlock(&bfad_mutex);
1348 /* Initializing the state machine: State set to uninit */
1349 bfa_sm_set_state(bfad, bfad_sm_uninit);
1351 spin_lock_init(&bfad->bfad_lock);
1352 spin_lock_init(&bfad->bfad_aen_spinlock);
1354 pci_set_drvdata(pdev, bfad);
1356 bfad->ref_count = 0;
1357 bfad->pport.bfad = bfad;
1358 INIT_LIST_HEAD(&bfad->pbc_vport_list);
1359 INIT_LIST_HEAD(&bfad->vport_list);
1361 /* Setup the debugfs node for this bfad */
1362 if (bfa_debugfs_enable)
1363 bfad_debugfs_init(&bfad->pport);
1365 retval = bfad_drv_init(bfad);
1366 if (retval != BFA_STATUS_OK)
1367 goto out_drv_init_failure;
1369 bfa_sm_send_event(bfad, BFAD_E_CREATE);
1371 if (bfa_sm_cmp_state(bfad, bfad_sm_uninit))
1372 goto out_bfad_sm_failure;
1374 return 0;
1376 out_bfad_sm_failure:
1377 bfad_hal_mem_release(bfad);
1378 out_drv_init_failure:
1379 /* Remove the debugfs node for this bfad */
1380 kfree(bfad->regdata);
1381 bfad_debugfs_exit(&bfad->pport);
1382 mutex_lock(&bfad_mutex);
1383 bfad_inst--;
1384 list_del(&bfad->list_entry);
1385 mutex_unlock(&bfad_mutex);
1386 bfad_pci_uninit(pdev, bfad);
1387 out_pci_init_failure:
1388 kfree(bfad->trcmod);
1389 out_alloc_trace_failure:
1390 kfree(bfad);
1391 out:
1392 return error;
1396 * PCI remove entry.
1398 void
1399 bfad_pci_remove(struct pci_dev *pdev)
1401 struct bfad_s *bfad = pci_get_drvdata(pdev);
1402 unsigned long flags;
1404 bfa_trc(bfad, bfad->inst_no);
1406 spin_lock_irqsave(&bfad->bfad_lock, flags);
1407 if (bfad->bfad_tsk != NULL) {
1408 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1409 kthread_stop(bfad->bfad_tsk);
1410 } else {
1411 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1414 /* Send Event BFAD_E_STOP */
1415 bfa_sm_send_event(bfad, BFAD_E_STOP);
1417 /* Driver detach and dealloc mem */
1418 spin_lock_irqsave(&bfad->bfad_lock, flags);
1419 bfa_detach(&bfad->bfa);
1420 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1421 bfad_hal_mem_release(bfad);
1423 /* Remove the debugfs node for this bfad */
1424 kfree(bfad->regdata);
1425 bfad_debugfs_exit(&bfad->pport);
1427 /* Cleaning the BFAD instance */
1428 mutex_lock(&bfad_mutex);
1429 bfad_inst--;
1430 list_del(&bfad->list_entry);
1431 mutex_unlock(&bfad_mutex);
1432 bfad_pci_uninit(pdev, bfad);
1434 kfree(bfad->trcmod);
1435 kfree(bfad);
1439 * PCI Error Recovery entry, error detected.
1441 static pci_ers_result_t
1442 bfad_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
1444 struct bfad_s *bfad = pci_get_drvdata(pdev);
1445 unsigned long flags;
1446 pci_ers_result_t ret = PCI_ERS_RESULT_NONE;
1448 dev_printk(KERN_ERR, &pdev->dev,
1449 "error detected state: %d - flags: 0x%x\n",
1450 state, bfad->bfad_flags);
1452 switch (state) {
1453 case pci_channel_io_normal: /* non-fatal error */
1454 spin_lock_irqsave(&bfad->bfad_lock, flags);
1455 bfad->bfad_flags &= ~BFAD_EEH_BUSY;
1456 /* Suspend/fail all bfa operations */
1457 bfa_ioc_suspend(&bfad->bfa.ioc);
1458 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1459 del_timer_sync(&bfad->hal_tmo);
1460 ret = PCI_ERS_RESULT_CAN_RECOVER;
1461 break;
1462 case pci_channel_io_frozen: /* fatal error */
1463 init_completion(&bfad->comp);
1464 spin_lock_irqsave(&bfad->bfad_lock, flags);
1465 bfad->bfad_flags |= BFAD_EEH_BUSY;
1466 /* Suspend/fail all bfa operations */
1467 bfa_ioc_suspend(&bfad->bfa.ioc);
1468 bfa_fcs_stop(&bfad->bfa_fcs);
1469 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1470 wait_for_completion(&bfad->comp);
1472 bfad_remove_intr(bfad);
1473 del_timer_sync(&bfad->hal_tmo);
1474 pci_disable_device(pdev);
1475 ret = PCI_ERS_RESULT_NEED_RESET;
1476 break;
1477 case pci_channel_io_perm_failure: /* PCI Card is DEAD */
1478 spin_lock_irqsave(&bfad->bfad_lock, flags);
1479 bfad->bfad_flags |= BFAD_EEH_BUSY |
1480 BFAD_EEH_PCI_CHANNEL_IO_PERM_FAILURE;
1481 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1483 /* If the error_detected handler is called with the reason
1484 * pci_channel_io_perm_failure - it will subsequently call
1485 * pci_remove() entry point to remove the pci device from the
1486 * system - So defer the cleanup to pci_remove(); cleaning up
1487 * here causes inconsistent state during pci_remove().
1489 ret = PCI_ERS_RESULT_DISCONNECT;
1490 break;
1491 default:
1492 WARN_ON(1);
1495 return ret;
1499 restart_bfa(struct bfad_s *bfad)
1501 unsigned long flags;
1502 struct pci_dev *pdev = bfad->pcidev;
1504 bfa_attach(&bfad->bfa, bfad, &bfad->ioc_cfg,
1505 &bfad->meminfo, &bfad->hal_pcidev);
1507 /* Enable Interrupt and wait bfa_init completion */
1508 if (bfad_setup_intr(bfad)) {
1509 dev_printk(KERN_WARNING, &pdev->dev,
1510 "%s: bfad_setup_intr failed\n", bfad->pci_name);
1511 bfa_sm_send_event(bfad, BFAD_E_INIT_FAILED);
1512 return -1;
1515 init_completion(&bfad->comp);
1516 spin_lock_irqsave(&bfad->bfad_lock, flags);
1517 bfa_iocfc_init(&bfad->bfa);
1518 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1520 /* Set up interrupt handler for each vectors */
1521 if ((bfad->bfad_flags & BFAD_MSIX_ON) &&
1522 bfad_install_msix_handler(bfad))
1523 dev_printk(KERN_WARNING, &pdev->dev,
1524 "%s: install_msix failed.\n", bfad->pci_name);
1526 bfad_init_timer(bfad);
1527 wait_for_completion(&bfad->comp);
1528 bfad_drv_start(bfad);
1530 return 0;
1534 * PCI Error Recovery entry, re-initialize the chip.
1536 static pci_ers_result_t
1537 bfad_pci_slot_reset(struct pci_dev *pdev)
1539 struct bfad_s *bfad = pci_get_drvdata(pdev);
1540 u8 byte;
1542 dev_printk(KERN_ERR, &pdev->dev,
1543 "bfad_pci_slot_reset flags: 0x%x\n", bfad->bfad_flags);
1545 if (pci_enable_device(pdev)) {
1546 dev_printk(KERN_ERR, &pdev->dev, "Cannot re-enable "
1547 "PCI device after reset.\n");
1548 return PCI_ERS_RESULT_DISCONNECT;
1551 pci_restore_state(pdev);
1554 * Read some byte (e.g. DMA max. payload size which can't
1555 * be 0xff any time) to make sure - we did not hit another PCI error
1556 * in the middle of recovery. If we did, then declare permanent failure.
1558 pci_read_config_byte(pdev, 0x68, &byte);
1559 if (byte == 0xff) {
1560 dev_printk(KERN_ERR, &pdev->dev,
1561 "slot_reset failed ... got another PCI error !\n");
1562 goto out_disable_device;
1565 pci_save_state(pdev);
1566 pci_set_master(pdev);
1568 if (pci_set_dma_mask(bfad->pcidev, DMA_BIT_MASK(64)) != 0)
1569 if (pci_set_dma_mask(bfad->pcidev, DMA_BIT_MASK(32)) != 0)
1570 goto out_disable_device;
1572 pci_cleanup_aer_uncorrect_error_status(pdev);
1574 if (restart_bfa(bfad) == -1)
1575 goto out_disable_device;
1577 pci_enable_pcie_error_reporting(pdev);
1578 dev_printk(KERN_WARNING, &pdev->dev,
1579 "slot_reset completed flags: 0x%x!\n", bfad->bfad_flags);
1581 return PCI_ERS_RESULT_RECOVERED;
1583 out_disable_device:
1584 pci_disable_device(pdev);
1585 return PCI_ERS_RESULT_DISCONNECT;
1588 static pci_ers_result_t
1589 bfad_pci_mmio_enabled(struct pci_dev *pdev)
1591 unsigned long flags;
1592 struct bfad_s *bfad = pci_get_drvdata(pdev);
1594 dev_printk(KERN_INFO, &pdev->dev, "mmio_enabled\n");
1596 /* Fetch FW diagnostic information */
1597 bfa_ioc_debug_save_ftrc(&bfad->bfa.ioc);
1599 /* Cancel all pending IOs */
1600 spin_lock_irqsave(&bfad->bfad_lock, flags);
1601 init_completion(&bfad->comp);
1602 bfa_fcs_stop(&bfad->bfa_fcs);
1603 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1604 wait_for_completion(&bfad->comp);
1606 bfad_remove_intr(bfad);
1607 del_timer_sync(&bfad->hal_tmo);
1608 pci_disable_device(pdev);
1610 return PCI_ERS_RESULT_NEED_RESET;
1613 static void
1614 bfad_pci_resume(struct pci_dev *pdev)
1616 unsigned long flags;
1617 struct bfad_s *bfad = pci_get_drvdata(pdev);
1619 dev_printk(KERN_WARNING, &pdev->dev, "resume\n");
1621 /* wait until the link is online */
1622 bfad_rport_online_wait(bfad);
1624 spin_lock_irqsave(&bfad->bfad_lock, flags);
1625 bfad->bfad_flags &= ~BFAD_EEH_BUSY;
1626 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1629 struct pci_device_id bfad_id_table[] = {
1631 .vendor = BFA_PCI_VENDOR_ID_BROCADE,
1632 .device = BFA_PCI_DEVICE_ID_FC_8G2P,
1633 .subvendor = PCI_ANY_ID,
1634 .subdevice = PCI_ANY_ID,
1637 .vendor = BFA_PCI_VENDOR_ID_BROCADE,
1638 .device = BFA_PCI_DEVICE_ID_FC_8G1P,
1639 .subvendor = PCI_ANY_ID,
1640 .subdevice = PCI_ANY_ID,
1643 .vendor = BFA_PCI_VENDOR_ID_BROCADE,
1644 .device = BFA_PCI_DEVICE_ID_CT,
1645 .subvendor = PCI_ANY_ID,
1646 .subdevice = PCI_ANY_ID,
1647 .class = (PCI_CLASS_SERIAL_FIBER << 8),
1648 .class_mask = ~0,
1651 .vendor = BFA_PCI_VENDOR_ID_BROCADE,
1652 .device = BFA_PCI_DEVICE_ID_CT_FC,
1653 .subvendor = PCI_ANY_ID,
1654 .subdevice = PCI_ANY_ID,
1655 .class = (PCI_CLASS_SERIAL_FIBER << 8),
1656 .class_mask = ~0,
1659 .vendor = BFA_PCI_VENDOR_ID_BROCADE,
1660 .device = BFA_PCI_DEVICE_ID_CT2,
1661 .subvendor = PCI_ANY_ID,
1662 .subdevice = PCI_ANY_ID,
1663 .class = (PCI_CLASS_SERIAL_FIBER << 8),
1664 .class_mask = ~0,
1668 .vendor = BFA_PCI_VENDOR_ID_BROCADE,
1669 .device = BFA_PCI_DEVICE_ID_CT2_QUAD,
1670 .subvendor = PCI_ANY_ID,
1671 .subdevice = PCI_ANY_ID,
1672 .class = (PCI_CLASS_SERIAL_FIBER << 8),
1673 .class_mask = ~0,
1675 {0, 0},
1678 MODULE_DEVICE_TABLE(pci, bfad_id_table);
1681 * PCI error recovery handlers.
1683 static struct pci_error_handlers bfad_err_handler = {
1684 .error_detected = bfad_pci_error_detected,
1685 .slot_reset = bfad_pci_slot_reset,
1686 .mmio_enabled = bfad_pci_mmio_enabled,
1687 .resume = bfad_pci_resume,
1690 static struct pci_driver bfad_pci_driver = {
1691 .name = BFAD_DRIVER_NAME,
1692 .id_table = bfad_id_table,
1693 .probe = bfad_pci_probe,
1694 .remove = bfad_pci_remove,
1695 .err_handler = &bfad_err_handler,
1699 * Driver module init.
1701 static int __init
1702 bfad_init(void)
1704 int error = 0;
1706 pr_info("QLogic BR-series BFA FC/FCOE SCSI driver - version: %s\n",
1707 BFAD_DRIVER_VERSION);
1709 if (num_sgpgs > 0)
1710 num_sgpgs_parm = num_sgpgs;
1712 error = bfad_im_module_init();
1713 if (error) {
1714 error = -ENOMEM;
1715 printk(KERN_WARNING "bfad_im_module_init failure\n");
1716 goto ext;
1719 if (strcmp(FCPI_NAME, " fcpim") == 0)
1720 supported_fc4s |= BFA_LPORT_ROLE_FCP_IM;
1722 bfa_auto_recover = ioc_auto_recover;
1723 bfa_fcs_rport_set_del_timeout(rport_del_timeout);
1724 bfa_fcs_rport_set_max_logins(max_rport_logins);
1726 error = pci_register_driver(&bfad_pci_driver);
1727 if (error) {
1728 printk(KERN_WARNING "pci_register_driver failure\n");
1729 goto ext;
1732 return 0;
1734 ext:
1735 bfad_im_module_exit();
1736 return error;
1740 * Driver module exit.
1742 static void __exit
1743 bfad_exit(void)
1745 pci_unregister_driver(&bfad_pci_driver);
1746 bfad_im_module_exit();
1747 bfad_free_fwimg();
1750 /* Firmware handling */
1751 static void
1752 bfad_read_firmware(struct pci_dev *pdev, u32 **bfi_image,
1753 u32 *bfi_image_size, char *fw_name)
1755 const struct firmware *fw;
1757 if (request_firmware(&fw, fw_name, &pdev->dev)) {
1758 printk(KERN_ALERT "Can't locate firmware %s\n", fw_name);
1759 *bfi_image = NULL;
1760 goto out;
1763 *bfi_image = vmalloc(fw->size);
1764 if (NULL == *bfi_image) {
1765 printk(KERN_ALERT "Fail to allocate buffer for fw image "
1766 "size=%x!\n", (u32) fw->size);
1767 goto out;
1770 memcpy(*bfi_image, fw->data, fw->size);
1771 *bfi_image_size = fw->size/sizeof(u32);
1772 out:
1773 release_firmware(fw);
1776 static u32 *
1777 bfad_load_fwimg(struct pci_dev *pdev)
1779 if (bfa_asic_id_ct2(pdev->device)) {
1780 if (bfi_image_ct2_size == 0)
1781 bfad_read_firmware(pdev, &bfi_image_ct2,
1782 &bfi_image_ct2_size, BFAD_FW_FILE_CT2);
1783 return bfi_image_ct2;
1784 } else if (bfa_asic_id_ct(pdev->device)) {
1785 if (bfi_image_ct_size == 0)
1786 bfad_read_firmware(pdev, &bfi_image_ct,
1787 &bfi_image_ct_size, BFAD_FW_FILE_CT);
1788 return bfi_image_ct;
1789 } else if (bfa_asic_id_cb(pdev->device)) {
1790 if (bfi_image_cb_size == 0)
1791 bfad_read_firmware(pdev, &bfi_image_cb,
1792 &bfi_image_cb_size, BFAD_FW_FILE_CB);
1793 return bfi_image_cb;
1796 return NULL;
1799 static void
1800 bfad_free_fwimg(void)
1802 if (bfi_image_ct2_size && bfi_image_ct2)
1803 vfree(bfi_image_ct2);
1804 if (bfi_image_ct_size && bfi_image_ct)
1805 vfree(bfi_image_ct);
1806 if (bfi_image_cb_size && bfi_image_cb)
1807 vfree(bfi_image_cb);
1810 module_init(bfad_init);
1811 module_exit(bfad_exit);
1812 MODULE_LICENSE("GPL");
1813 MODULE_DESCRIPTION("QLogic BR-series Fibre Channel HBA Driver" BFAD_PROTO_NAME);
1814 MODULE_AUTHOR("QLogic Corporation");
1815 MODULE_VERSION(BFAD_DRIVER_VERSION);