xref: /openbmc/linux/drivers/scsi/bfa/bfad.c (revision df687341)
1 /*
2  * Copyright (c) 2005-2014 Brocade Communications Systems, Inc.
3  * Copyright (c) 2014- QLogic Corporation.
4  * All rights reserved
5  * www.qlogic.com
6  *
7  * Linux driver for QLogic BR-series Fibre Channel Host Bus Adapter.
8  *
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
12  *
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.
17  */
18 
19 /*
20  *  bfad.c Linux driver PCI interface module.
21  */
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>
32 
33 #include "bfad_drv.h"
34 #include "bfad_im.h"
35 #include "bfa_fcs.h"
36 #include "bfa_defs.h"
37 #include "bfa.h"
38 
39 BFA_TRC_FILE(LDRV, BFAD);
40 DEFINE_MUTEX(bfad_mutex);
41 LIST_HEAD(bfad_list);
42 
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;
62 
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;
66 
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"
70 
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);
75 
76 static const char *msix_name_ct[] = {
77 	"ctrl",
78 	"cpe0", "cpe1", "cpe2", "cpe3",
79 	"rme0", "rme1", "rme2", "rme3" };
80 
81 static const char *msix_name_cb[] = {
82 	"cpe0", "cpe1", "cpe2", "cpe3",
83 	"rme0", "rme1", "rme2", "rme3",
84 	"eemc", "elpu0", "elpu1", "epss", "mlpu" };
85 
86 MODULE_FIRMWARE(BFAD_FW_FILE_CB);
87 MODULE_FIRMWARE(BFAD_FW_FILE_CT);
88 MODULE_FIRMWARE(BFAD_FW_FILE_CT2);
89 
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");
151 
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);
166 
167 /*
168  * Beginning state for the driver instance, awaiting the pci_probe event
169  */
170 static void
171 bfad_sm_uninit(struct bfad_s *bfad, enum bfad_sm_event event)
172 {
173 	bfa_trc(bfad, event);
174 
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);
184 		}
185 		bfa_sm_send_event(bfad, BFAD_E_INIT);
186 		break;
187 
188 	case BFAD_E_STOP:
189 		/* Ignore stop; already in uninit */
190 		break;
191 
192 	default:
193 		bfa_sm_fault(bfad, event);
194 	}
195 }
196 
197 /*
198  * Driver Instance is created, awaiting event INIT to initialize the bfad
199  */
200 static void
201 bfad_sm_created(struct bfad_s *bfad, enum bfad_sm_event event)
202 {
203 	unsigned long flags;
204 	bfa_status_t ret;
205 
206 	bfa_trc(bfad, event);
207 
208 	switch (event) {
209 	case BFAD_E_INIT:
210 		bfa_sm_set_state(bfad, bfad_sm_initializing);
211 
212 		init_completion(&bfad->comp);
213 
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;
220 		}
221 
222 		spin_lock_irqsave(&bfad->bfad_lock, flags);
223 		bfa_iocfc_init(&bfad->bfa);
224 		spin_unlock_irqrestore(&bfad->bfad_lock, flags);
225 
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);
231 		}
232 
233 		bfad_init_timer(bfad);
234 
235 		wait_for_completion(&bfad->comp);
236 
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);
246 
247 			ret = bfad_cfg_pport(bfad, BFA_LPORT_ROLE_FCP_IM);
248 			if (ret != BFA_STATUS_OK) {
249 				init_completion(&bfad->comp);
250 
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);
255 
256 				wait_for_completion(&bfad->comp);
257 
258 				bfa_sm_send_event(bfad, BFAD_E_INIT_FAILED);
259 				break;
260 			}
261 			bfad->bfad_flags |= BFAD_HAL_INIT_FAIL;
262 			bfa_sm_send_event(bfad, BFAD_E_HAL_INIT_FAILED);
263 		}
264 
265 		break;
266 
267 	case BFAD_E_KTHREAD_CREATE_FAILED:
268 		bfa_sm_set_state(bfad, bfad_sm_uninit);
269 		break;
270 
271 	default:
272 		bfa_sm_fault(bfad, event);
273 	}
274 }
275 
276 static void
277 bfad_sm_initializing(struct bfad_s *bfad, enum bfad_sm_event event)
278 {
279 	int	retval;
280 	unsigned long	flags;
281 
282 	bfa_trc(bfad, event);
283 
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);
290 
291 		retval = bfad_start_ops(bfad);
292 		if (retval != BFA_STATUS_OK) {
293 			bfa_sm_set_state(bfad, bfad_sm_failed);
294 			break;
295 		}
296 		bfa_sm_set_state(bfad, bfad_sm_operational);
297 		break;
298 
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;
306 
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);
312 	}
313 }
314 
315 static void
316 bfad_sm_failed(struct bfad_s *bfad, enum bfad_sm_event event)
317 {
318 	int	retval;
319 
320 	bfa_trc(bfad, event);
321 
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;
329 
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;
334 
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;
340 
341 	default:
342 		bfa_sm_fault(bfad, event);
343 	}
344 }
345 
346 static void
347 bfad_sm_operational(struct bfad_s *bfad, enum bfad_sm_event event)
348 {
349 	bfa_trc(bfad, event);
350 
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;
356 
357 	default:
358 		bfa_sm_fault(bfad, event);
359 	}
360 }
361 
362 static void
363 bfad_sm_fcs_exit(struct bfad_s *bfad, enum bfad_sm_event event)
364 {
365 	bfa_trc(bfad, event);
366 
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;
372 
373 	default:
374 		bfa_sm_fault(bfad, event);
375 	}
376 }
377 
378 static void
379 bfad_sm_stopping(struct bfad_s *bfad, enum bfad_sm_event event)
380 {
381 	bfa_trc(bfad, event);
382 
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;
392 
393 	default:
394 		bfa_sm_fault(bfad, event);
395 		break;
396 	}
397 }
398 
399 /*
400  *  BFA callbacks
401  */
402 void
403 bfad_hcb_comp(void *arg, bfa_status_t status)
404 {
405 	struct bfad_hal_comp *fcomp = (struct bfad_hal_comp *)arg;
406 
407 	fcomp->status = status;
408 	complete(&fcomp->comp);
409 }
410 
411 /*
412  * bfa_init callback
413  */
414 void
415 bfa_cb_init(void *drv, bfa_status_t init_status)
416 {
417 	struct bfad_s	      *bfad = drv;
418 
419 	if (init_status == BFA_STATUS_OK) {
420 		bfad->bfad_flags |= BFAD_HAL_INIT_DONE;
421 
422 		/*
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
426 		 */
427 		if ((bfad->bfad_flags & BFAD_HAL_INIT_FAIL)) {
428 			bfad->bfad_flags &= ~BFAD_HAL_INIT_FAIL;
429 			wake_up_process(bfad->bfad_tsk);
430 		}
431 	}
432 
433 	complete(&bfad->comp);
434 }
435 
436 /*
437  *  BFA_FCS callbacks
438  */
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)
443 {
444 	bfa_status_t	rc;
445 	struct bfad_port_s    *port_drv;
446 
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;
459 	}
460 
461 	port_drv->fcs_port = port;
462 	port_drv->roles = roles;
463 
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;
469 		}
470 	}
471 
472 	return port_drv;
473 }
474 
475 /*
476  * FCS RPORT alloc callback, after successful PLOGI by FCS
477  */
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)
481 {
482 	bfa_status_t	rc = BFA_STATUS_OK;
483 
484 	*rport_drv = kzalloc(sizeof(struct bfad_rport_s), GFP_ATOMIC);
485 	if (*rport_drv == NULL) {
486 		rc = BFA_STATUS_ENOMEM;
487 		goto ext;
488 	}
489 
490 	*rport = &(*rport_drv)->fcs_rport;
491 
492 ext:
493 	return rc;
494 }
495 
496 /*
497  * FCS PBC VPORT Create
498  */
499 void
500 bfa_fcb_pbc_vport_create(struct bfad_s *bfad, struct bfi_pbc_vport_s pbc_vport)
501 {
502 
503 	struct bfa_lport_cfg_s port_cfg = {0};
504 	struct bfad_vport_s   *vport;
505 	int rc;
506 
507 	vport = kzalloc(sizeof(struct bfad_vport_s), GFP_ATOMIC);
508 	if (!vport) {
509 		bfa_trc(bfad, 0);
510 		return;
511 	}
512 
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;
518 
519 	rc = bfa_fcs_pbc_vport_create(&vport->fcs_vport, &bfad->bfa_fcs, 0,
520 				  &port_cfg, vport);
521 
522 	if (rc != BFA_STATUS_OK) {
523 		bfa_trc(bfad, 0);
524 		return;
525 	}
526 
527 	list_add_tail(&vport->list_entry, &bfad->pbc_vport_list);
528 }
529 
530 void
531 bfad_hal_mem_release(struct bfad_s *bfad)
532 {
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;
537 
538 	dma_info = &hal_meminfo->dma_info;
539 	kva_info = &hal_meminfo->kva_info;
540 
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);
545 	}
546 
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);
553 	}
554 
555 	memset(hal_meminfo, 0, sizeof(struct bfa_meminfo_s));
556 }
557 
558 void
559 bfad_update_hal_cfg(struct bfa_iocfc_cfg_s *bfa_cfg)
560 {
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;
577 
578 	/*
579 	 * populate the hal values back to the driver for sysfs use.
580 	 * otherwise, the default values will be shown as 0 in sysfs
581 	 */
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;
590 }
591 
592 bfa_status_t
593 bfad_hal_mem_alloc(struct bfad_s *bfad)
594 {
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;
601 
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);
606 
607 	dma_info = &hal_meminfo->dma_info;
608 	kva_info = &hal_meminfo->kva_info;
609 
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;
618 		}
619 	}
620 
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;
631 		}
632 		dma_elem->dma = phys_addr;
633 		memset(dma_elem->kva, 0, dma_elem->mem_len);
634 	}
635 ext:
636 	return rc;
637 }
638 
639 /*
640  * Create a vport under a vf.
641  */
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)
645 {
646 	struct bfad_vport_s   *vport;
647 	int		rc = BFA_STATUS_OK;
648 	unsigned long	flags;
649 	struct completion fcomp;
650 
651 	vport = kzalloc(sizeof(struct bfad_vport_s), GFP_KERNEL);
652 	if (!vport) {
653 		rc = BFA_STATUS_ENOMEM;
654 		goto ext;
655 	}
656 
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);
662 
663 	if (rc != BFA_STATUS_OK)
664 		goto ext_free_vport;
665 
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;
671 	}
672 
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);
677 
678 	return BFA_STATUS_OK;
679 
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;
691 }
692 
693 void
694 bfad_bfa_tmo(struct timer_list *t)
695 {
696 	struct bfad_s	      *bfad = from_timer(bfad, t, hal_tmo);
697 	unsigned long	flags;
698 	struct list_head	       doneq;
699 
700 	spin_lock_irqsave(&bfad->bfad_lock, flags);
701 
702 	bfa_timer_beat(&bfad->bfa.timer_mod);
703 
704 	bfa_comp_deq(&bfad->bfa, &doneq);
705 	spin_unlock_irqrestore(&bfad->bfad_lock, flags);
706 
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);
712 	}
713 
714 	mod_timer(&bfad->hal_tmo,
715 		  jiffies + msecs_to_jiffies(BFA_TIMER_FREQ));
716 }
717 
718 void
719 bfad_init_timer(struct bfad_s *bfad)
720 {
721 	timer_setup(&bfad->hal_tmo, bfad_bfa_tmo, 0);
722 
723 	mod_timer(&bfad->hal_tmo,
724 		  jiffies + msecs_to_jiffies(BFA_TIMER_FREQ));
725 }
726 
727 int
728 bfad_pci_init(struct pci_dev *pdev, struct bfad_s *bfad)
729 {
730 	int		rc = -ENODEV;
731 
732 	if (pci_enable_device(pdev)) {
733 		printk(KERN_ERR "pci_enable_device fail %p\n", pdev);
734 		goto out;
735 	}
736 
737 	if (pci_request_regions(pdev, BFAD_DRIVER_NAME))
738 		goto out_disable_device;
739 
740 	pci_set_master(pdev);
741 
742 	if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) ||
743 	    dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32))) {
744 		printk(KERN_ERR "dma_set_mask_and_coherent fail %p\n", pdev);
745 		goto out_release_region;
746 	}
747 
748 	/* Enable PCIE Advanced Error Recovery (AER) if kernel supports */
749 	pci_enable_pcie_error_reporting(pdev);
750 
751 	bfad->pci_bar0_kva = pci_iomap(pdev, 0, pci_resource_len(pdev, 0));
752 	bfad->pci_bar2_kva = pci_iomap(pdev, 2, pci_resource_len(pdev, 2));
753 
754 	if (bfad->pci_bar0_kva == NULL) {
755 		printk(KERN_ERR "Fail to map bar0\n");
756 		goto out_release_region;
757 	}
758 
759 	bfad->hal_pcidev.pci_slot = PCI_SLOT(pdev->devfn);
760 	bfad->hal_pcidev.pci_func = PCI_FUNC(pdev->devfn);
761 	bfad->hal_pcidev.pci_bar_kva = bfad->pci_bar0_kva;
762 	bfad->hal_pcidev.device_id = pdev->device;
763 	bfad->hal_pcidev.ssid = pdev->subsystem_device;
764 	bfad->pci_name = pci_name(pdev);
765 
766 	bfad->pci_attr.vendor_id = pdev->vendor;
767 	bfad->pci_attr.device_id = pdev->device;
768 	bfad->pci_attr.ssid = pdev->subsystem_device;
769 	bfad->pci_attr.ssvid = pdev->subsystem_vendor;
770 	bfad->pci_attr.pcifn = PCI_FUNC(pdev->devfn);
771 
772 	bfad->pcidev = pdev;
773 
774 	/* Adjust PCIe Maximum Read Request Size */
775 	if (pci_is_pcie(pdev) && pcie_max_read_reqsz) {
776 		if (pcie_max_read_reqsz >= 128 &&
777 		    pcie_max_read_reqsz <= 4096 &&
778 		    is_power_of_2(pcie_max_read_reqsz)) {
779 			int max_rq = pcie_get_readrq(pdev);
780 			printk(KERN_WARNING "BFA[%s]: "
781 				"pcie_max_read_request_size is %d, "
782 				"reset to %d\n", bfad->pci_name, max_rq,
783 				pcie_max_read_reqsz);
784 			pcie_set_readrq(pdev, pcie_max_read_reqsz);
785 		} else {
786 			printk(KERN_WARNING "BFA[%s]: invalid "
787 			       "pcie_max_read_request_size %d ignored\n",
788 			       bfad->pci_name, pcie_max_read_reqsz);
789 		}
790 	}
791 
792 	pci_save_state(pdev);
793 
794 	return 0;
795 
796 out_release_region:
797 	pci_release_regions(pdev);
798 out_disable_device:
799 	pci_disable_device(pdev);
800 out:
801 	return rc;
802 }
803 
804 void
805 bfad_pci_uninit(struct pci_dev *pdev, struct bfad_s *bfad)
806 {
807 	pci_iounmap(pdev, bfad->pci_bar0_kva);
808 	pci_iounmap(pdev, bfad->pci_bar2_kva);
809 	pci_release_regions(pdev);
810 	/* Disable PCIE Advanced Error Recovery (AER) */
811 	pci_disable_pcie_error_reporting(pdev);
812 	pci_disable_device(pdev);
813 }
814 
815 bfa_status_t
816 bfad_drv_init(struct bfad_s *bfad)
817 {
818 	bfa_status_t	rc;
819 	unsigned long	flags;
820 
821 	bfad->cfg_data.rport_del_timeout = rport_del_timeout;
822 	bfad->cfg_data.lun_queue_depth = bfa_lun_queue_depth;
823 	bfad->cfg_data.io_max_sge = bfa_io_max_sge;
824 	bfad->cfg_data.binding_method = FCP_PWWN_BINDING;
825 
826 	rc = bfad_hal_mem_alloc(bfad);
827 	if (rc != BFA_STATUS_OK) {
828 		printk(KERN_WARNING "bfad%d bfad_hal_mem_alloc failure\n",
829 		       bfad->inst_no);
830 		printk(KERN_WARNING
831 			"Not enough memory to attach all QLogic BR-series HBA ports. System may need more memory.\n");
832 		return BFA_STATUS_FAILED;
833 	}
834 
835 	bfad->bfa.trcmod = bfad->trcmod;
836 	bfad->bfa.plog = &bfad->plog_buf;
837 	bfa_plog_init(&bfad->plog_buf);
838 	bfa_plog_str(&bfad->plog_buf, BFA_PL_MID_DRVR, BFA_PL_EID_DRIVER_START,
839 		     0, "Driver Attach");
840 
841 	bfa_attach(&bfad->bfa, bfad, &bfad->ioc_cfg, &bfad->meminfo,
842 		   &bfad->hal_pcidev);
843 
844 	/* FCS INIT */
845 	spin_lock_irqsave(&bfad->bfad_lock, flags);
846 	bfad->bfa_fcs.trcmod = bfad->trcmod;
847 	bfa_fcs_attach(&bfad->bfa_fcs, &bfad->bfa, bfad, BFA_FALSE);
848 	bfad->bfa_fcs.fdmi_enabled = fdmi_enable;
849 	spin_unlock_irqrestore(&bfad->bfad_lock, flags);
850 
851 	bfad->bfad_flags |= BFAD_DRV_INIT_DONE;
852 
853 	return BFA_STATUS_OK;
854 }
855 
856 void
857 bfad_drv_uninit(struct bfad_s *bfad)
858 {
859 	unsigned long   flags;
860 
861 	spin_lock_irqsave(&bfad->bfad_lock, flags);
862 	init_completion(&bfad->comp);
863 	bfa_iocfc_stop(&bfad->bfa);
864 	spin_unlock_irqrestore(&bfad->bfad_lock, flags);
865 	wait_for_completion(&bfad->comp);
866 
867 	del_timer_sync(&bfad->hal_tmo);
868 	bfa_isr_disable(&bfad->bfa);
869 	bfa_detach(&bfad->bfa);
870 	bfad_remove_intr(bfad);
871 	bfad_hal_mem_release(bfad);
872 
873 	bfad->bfad_flags &= ~BFAD_DRV_INIT_DONE;
874 }
875 
876 void
877 bfad_drv_start(struct bfad_s *bfad)
878 {
879 	unsigned long	flags;
880 
881 	spin_lock_irqsave(&bfad->bfad_lock, flags);
882 	bfa_iocfc_start(&bfad->bfa);
883 	bfa_fcs_pbc_vport_init(&bfad->bfa_fcs);
884 	bfa_fcs_fabric_modstart(&bfad->bfa_fcs);
885 	bfad->bfad_flags |= BFAD_HAL_START_DONE;
886 	spin_unlock_irqrestore(&bfad->bfad_lock, flags);
887 
888 	if (bfad->im)
889 		flush_workqueue(bfad->im->drv_workq);
890 }
891 
892 void
893 bfad_fcs_stop(struct bfad_s *bfad)
894 {
895 	unsigned long	flags;
896 
897 	spin_lock_irqsave(&bfad->bfad_lock, flags);
898 	init_completion(&bfad->comp);
899 	bfad->pport.flags |= BFAD_PORT_DELETE;
900 	bfa_fcs_exit(&bfad->bfa_fcs);
901 	spin_unlock_irqrestore(&bfad->bfad_lock, flags);
902 	wait_for_completion(&bfad->comp);
903 
904 	bfa_sm_send_event(bfad, BFAD_E_FCS_EXIT_COMP);
905 }
906 
907 void
908 bfad_stop(struct bfad_s *bfad)
909 {
910 	unsigned long	flags;
911 
912 	spin_lock_irqsave(&bfad->bfad_lock, flags);
913 	init_completion(&bfad->comp);
914 	bfa_iocfc_stop(&bfad->bfa);
915 	bfad->bfad_flags &= ~BFAD_HAL_START_DONE;
916 	spin_unlock_irqrestore(&bfad->bfad_lock, flags);
917 	wait_for_completion(&bfad->comp);
918 
919 	bfa_sm_send_event(bfad, BFAD_E_EXIT_COMP);
920 }
921 
922 bfa_status_t
923 bfad_cfg_pport(struct bfad_s *bfad, enum bfa_lport_role role)
924 {
925 	int		rc = BFA_STATUS_OK;
926 
927 	/* Allocate scsi_host for the physical port */
928 	if ((supported_fc4s & BFA_LPORT_ROLE_FCP_IM) &&
929 	    (role & BFA_LPORT_ROLE_FCP_IM)) {
930 		if (bfad->pport.im_port == NULL) {
931 			rc = BFA_STATUS_FAILED;
932 			goto out;
933 		}
934 
935 		rc = bfad_im_scsi_host_alloc(bfad, bfad->pport.im_port,
936 						&bfad->pcidev->dev);
937 		if (rc != BFA_STATUS_OK)
938 			goto out;
939 
940 		bfad->pport.roles |= BFA_LPORT_ROLE_FCP_IM;
941 	}
942 
943 	bfad->bfad_flags |= BFAD_CFG_PPORT_DONE;
944 
945 out:
946 	return rc;
947 }
948 
949 void
950 bfad_uncfg_pport(struct bfad_s *bfad)
951 {
952 	if ((supported_fc4s & BFA_LPORT_ROLE_FCP_IM) &&
953 	    (bfad->pport.roles & BFA_LPORT_ROLE_FCP_IM)) {
954 		bfad_im_scsi_host_free(bfad, bfad->pport.im_port);
955 		bfad_im_port_clean(bfad->pport.im_port);
956 		kfree(bfad->pport.im_port);
957 		bfad->pport.roles &= ~BFA_LPORT_ROLE_FCP_IM;
958 	}
959 
960 	bfad->bfad_flags &= ~BFAD_CFG_PPORT_DONE;
961 }
962 
963 bfa_status_t
964 bfad_start_ops(struct bfad_s *bfad) {
965 
966 	int	retval;
967 	unsigned long	flags;
968 	struct bfad_vport_s *vport, *vport_new;
969 	struct bfa_fcs_driver_info_s driver_info;
970 
971 	/* Limit min/max. xfer size to [64k-32MB] */
972 	if (max_xfer_size < BFAD_MIN_SECTORS >> 1)
973 		max_xfer_size = BFAD_MIN_SECTORS >> 1;
974 	if (max_xfer_size > BFAD_MAX_SECTORS >> 1)
975 		max_xfer_size = BFAD_MAX_SECTORS >> 1;
976 
977 	/* Fill the driver_info info to fcs*/
978 	memset(&driver_info, 0, sizeof(driver_info));
979 	strlcpy(driver_info.version, BFAD_DRIVER_VERSION,
980 		sizeof(driver_info.version));
981 	if (host_name)
982 		strlcpy(driver_info.host_machine_name, host_name,
983 			sizeof(driver_info.host_machine_name));
984 	if (os_name)
985 		strlcpy(driver_info.host_os_name, os_name,
986 			sizeof(driver_info.host_os_name));
987 	if (os_patch)
988 		strlcpy(driver_info.host_os_patch, os_patch,
989 			sizeof(driver_info.host_os_patch));
990 
991 	strlcpy(driver_info.os_device_name, bfad->pci_name,
992 		sizeof(driver_info.os_device_name));
993 
994 	/* FCS driver info init */
995 	spin_lock_irqsave(&bfad->bfad_lock, flags);
996 	bfa_fcs_driver_info_init(&bfad->bfa_fcs, &driver_info);
997 
998 	if (bfad->bfad_flags & BFAD_CFG_PPORT_DONE)
999 		bfa_fcs_update_cfg(&bfad->bfa_fcs);
1000 	else
1001 		bfa_fcs_init(&bfad->bfa_fcs);
1002 
1003 	spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1004 
1005 	if (!(bfad->bfad_flags & BFAD_CFG_PPORT_DONE)) {
1006 		retval = bfad_cfg_pport(bfad, BFA_LPORT_ROLE_FCP_IM);
1007 		if (retval != BFA_STATUS_OK)
1008 			return BFA_STATUS_FAILED;
1009 	}
1010 
1011 	/* Setup fc host fixed attribute if the lk supports */
1012 	bfad_fc_host_init(bfad->pport.im_port);
1013 
1014 	/* BFAD level FC4 IM specific resource allocation */
1015 	retval = bfad_im_probe(bfad);
1016 	if (retval != BFA_STATUS_OK) {
1017 		printk(KERN_WARNING "bfad_im_probe failed\n");
1018 		if (bfa_sm_cmp_state(bfad, bfad_sm_initializing))
1019 			bfa_sm_set_state(bfad, bfad_sm_failed);
1020 		return BFA_STATUS_FAILED;
1021 	} else
1022 		bfad->bfad_flags |= BFAD_FC4_PROBE_DONE;
1023 
1024 	bfad_drv_start(bfad);
1025 
1026 	/* Complete pbc vport create */
1027 	list_for_each_entry_safe(vport, vport_new, &bfad->pbc_vport_list,
1028 				list_entry) {
1029 		struct fc_vport_identifiers vid;
1030 		struct fc_vport *fc_vport;
1031 		char pwwn_buf[BFA_STRING_32];
1032 
1033 		memset(&vid, 0, sizeof(vid));
1034 		vid.roles = FC_PORT_ROLE_FCP_INITIATOR;
1035 		vid.vport_type = FC_PORTTYPE_NPIV;
1036 		vid.disable = false;
1037 		vid.node_name = wwn_to_u64((u8 *)
1038 				(&((vport->fcs_vport).lport.port_cfg.nwwn)));
1039 		vid.port_name = wwn_to_u64((u8 *)
1040 				(&((vport->fcs_vport).lport.port_cfg.pwwn)));
1041 		fc_vport = fc_vport_create(bfad->pport.im_port->shost, 0, &vid);
1042 		if (!fc_vport) {
1043 			wwn2str(pwwn_buf, vid.port_name);
1044 			printk(KERN_WARNING "bfad%d: failed to create pbc vport"
1045 				" %s\n", bfad->inst_no, pwwn_buf);
1046 		}
1047 		list_del(&vport->list_entry);
1048 		kfree(vport);
1049 	}
1050 
1051 	/*
1052 	 * If bfa_linkup_delay is set to -1 default; try to retrive the
1053 	 * value using the bfad_get_linkup_delay(); else use the
1054 	 * passed in module param value as the bfa_linkup_delay.
1055 	 */
1056 	if (bfa_linkup_delay < 0) {
1057 		bfa_linkup_delay = bfad_get_linkup_delay(bfad);
1058 		bfad_rport_online_wait(bfad);
1059 		bfa_linkup_delay = -1;
1060 	} else
1061 		bfad_rport_online_wait(bfad);
1062 
1063 	BFA_LOG(KERN_INFO, bfad, bfa_log_level, "bfa device claimed\n");
1064 
1065 	return BFA_STATUS_OK;
1066 }
1067 
1068 int
1069 bfad_worker(void *ptr)
1070 {
1071 	struct bfad_s *bfad = ptr;
1072 	unsigned long flags;
1073 
1074 	if (kthread_should_stop())
1075 		return 0;
1076 
1077 	/* Send event BFAD_E_INIT_SUCCESS */
1078 	bfa_sm_send_event(bfad, BFAD_E_INIT_SUCCESS);
1079 
1080 	spin_lock_irqsave(&bfad->bfad_lock, flags);
1081 	bfad->bfad_tsk = NULL;
1082 	spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1083 
1084 	return 0;
1085 }
1086 
1087 /*
1088  *  BFA driver interrupt functions
1089  */
1090 irqreturn_t
1091 bfad_intx(int irq, void *dev_id)
1092 {
1093 	struct bfad_s	*bfad = dev_id;
1094 	struct list_head	doneq;
1095 	unsigned long	flags;
1096 	bfa_boolean_t rc;
1097 
1098 	spin_lock_irqsave(&bfad->bfad_lock, flags);
1099 	rc = bfa_intx(&bfad->bfa);
1100 	if (!rc) {
1101 		spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1102 		return IRQ_NONE;
1103 	}
1104 
1105 	bfa_comp_deq(&bfad->bfa, &doneq);
1106 	spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1107 
1108 	if (!list_empty(&doneq)) {
1109 		bfa_comp_process(&bfad->bfa, &doneq);
1110 
1111 		spin_lock_irqsave(&bfad->bfad_lock, flags);
1112 		bfa_comp_free(&bfad->bfa, &doneq);
1113 		spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1114 	}
1115 
1116 	return IRQ_HANDLED;
1117 
1118 }
1119 
1120 static irqreturn_t
1121 bfad_msix(int irq, void *dev_id)
1122 {
1123 	struct bfad_msix_s *vec = dev_id;
1124 	struct bfad_s *bfad = vec->bfad;
1125 	struct list_head doneq;
1126 	unsigned long   flags;
1127 
1128 	spin_lock_irqsave(&bfad->bfad_lock, flags);
1129 
1130 	bfa_msix(&bfad->bfa, vec->msix.entry);
1131 	bfa_comp_deq(&bfad->bfa, &doneq);
1132 	spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1133 
1134 	if (!list_empty(&doneq)) {
1135 		bfa_comp_process(&bfad->bfa, &doneq);
1136 
1137 		spin_lock_irqsave(&bfad->bfad_lock, flags);
1138 		bfa_comp_free(&bfad->bfa, &doneq);
1139 		spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1140 	}
1141 
1142 	return IRQ_HANDLED;
1143 }
1144 
1145 /*
1146  * Initialize the MSIX entry table.
1147  */
1148 static void
1149 bfad_init_msix_entry(struct bfad_s *bfad, struct msix_entry *msix_entries,
1150 			 int mask, int max_bit)
1151 {
1152 	int	i;
1153 	int	match = 0x00000001;
1154 
1155 	for (i = 0, bfad->nvec = 0; i < MAX_MSIX_ENTRY; i++) {
1156 		if (mask & match) {
1157 			bfad->msix_tab[bfad->nvec].msix.entry = i;
1158 			bfad->msix_tab[bfad->nvec].bfad = bfad;
1159 			msix_entries[bfad->nvec].entry = i;
1160 			bfad->nvec++;
1161 		}
1162 
1163 		match <<= 1;
1164 	}
1165 
1166 }
1167 
1168 int
1169 bfad_install_msix_handler(struct bfad_s *bfad)
1170 {
1171 	int i, error = 0;
1172 
1173 	for (i = 0; i < bfad->nvec; i++) {
1174 		sprintf(bfad->msix_tab[i].name, "bfa-%s-%s",
1175 				bfad->pci_name,
1176 				((bfa_asic_id_cb(bfad->hal_pcidev.device_id)) ?
1177 				msix_name_cb[i] : msix_name_ct[i]));
1178 
1179 		error = request_irq(bfad->msix_tab[i].msix.vector,
1180 				    (irq_handler_t) bfad_msix, 0,
1181 				    bfad->msix_tab[i].name, &bfad->msix_tab[i]);
1182 		bfa_trc(bfad, i);
1183 		bfa_trc(bfad, bfad->msix_tab[i].msix.vector);
1184 		if (error) {
1185 			int	j;
1186 
1187 			for (j = 0; j < i; j++)
1188 				free_irq(bfad->msix_tab[j].msix.vector,
1189 						&bfad->msix_tab[j]);
1190 
1191 			bfad->bfad_flags &= ~BFAD_MSIX_ON;
1192 			pci_disable_msix(bfad->pcidev);
1193 
1194 			return 1;
1195 		}
1196 	}
1197 
1198 	return 0;
1199 }
1200 
1201 /*
1202  * Setup MSIX based interrupt.
1203  */
1204 int
1205 bfad_setup_intr(struct bfad_s *bfad)
1206 {
1207 	int error;
1208 	u32 mask = 0, i, num_bit = 0, max_bit = 0;
1209 	struct msix_entry msix_entries[MAX_MSIX_ENTRY];
1210 	struct pci_dev *pdev = bfad->pcidev;
1211 	u16	reg;
1212 
1213 	/* Call BFA to get the msix map for this PCI function.  */
1214 	bfa_msix_getvecs(&bfad->bfa, &mask, &num_bit, &max_bit);
1215 
1216 	/* Set up the msix entry table */
1217 	bfad_init_msix_entry(bfad, msix_entries, mask, max_bit);
1218 
1219 	if ((bfa_asic_id_ctc(pdev->device) && !msix_disable_ct) ||
1220 	   (bfa_asic_id_cb(pdev->device) && !msix_disable_cb)) {
1221 
1222 		error = pci_enable_msix_exact(bfad->pcidev,
1223 					      msix_entries, bfad->nvec);
1224 		/* In CT1 & CT2, try to allocate just one vector */
1225 		if (error == -ENOSPC && bfa_asic_id_ctc(pdev->device)) {
1226 			printk(KERN_WARNING "bfa %s: trying one msix "
1227 			       "vector failed to allocate %d[%d]\n",
1228 			       bfad->pci_name, bfad->nvec, error);
1229 			bfad->nvec = 1;
1230 			error = pci_enable_msix_exact(bfad->pcidev,
1231 						      msix_entries, 1);
1232 		}
1233 
1234 		if (error) {
1235 			printk(KERN_WARNING "bfad%d: "
1236 			       "pci_enable_msix_exact failed (%d), "
1237 			       "use line based.\n",
1238 				bfad->inst_no, error);
1239 			goto line_based;
1240 		}
1241 
1242 		/* Disable INTX in MSI-X mode */
1243 		pci_read_config_word(pdev, PCI_COMMAND, &reg);
1244 
1245 		if (!(reg & PCI_COMMAND_INTX_DISABLE))
1246 			pci_write_config_word(pdev, PCI_COMMAND,
1247 				reg | PCI_COMMAND_INTX_DISABLE);
1248 
1249 		/* Save the vectors */
1250 		for (i = 0; i < bfad->nvec; i++) {
1251 			bfa_trc(bfad, msix_entries[i].vector);
1252 			bfad->msix_tab[i].msix.vector = msix_entries[i].vector;
1253 		}
1254 
1255 		bfa_msix_init(&bfad->bfa, bfad->nvec);
1256 
1257 		bfad->bfad_flags |= BFAD_MSIX_ON;
1258 
1259 		return 0;
1260 	}
1261 
1262 line_based:
1263 	error = request_irq(bfad->pcidev->irq, (irq_handler_t)bfad_intx,
1264 			    BFAD_IRQ_FLAGS, BFAD_DRIVER_NAME, bfad);
1265 	if (error)
1266 		return error;
1267 
1268 	bfad->bfad_flags |= BFAD_INTX_ON;
1269 
1270 	return 0;
1271 }
1272 
1273 void
1274 bfad_remove_intr(struct bfad_s *bfad)
1275 {
1276 	int	i;
1277 
1278 	if (bfad->bfad_flags & BFAD_MSIX_ON) {
1279 		for (i = 0; i < bfad->nvec; i++)
1280 			free_irq(bfad->msix_tab[i].msix.vector,
1281 					&bfad->msix_tab[i]);
1282 
1283 		pci_disable_msix(bfad->pcidev);
1284 		bfad->bfad_flags &= ~BFAD_MSIX_ON;
1285 	} else if (bfad->bfad_flags & BFAD_INTX_ON) {
1286 		free_irq(bfad->pcidev->irq, bfad);
1287 	}
1288 }
1289 
1290 /*
1291  * PCI probe entry.
1292  */
1293 int
1294 bfad_pci_probe(struct pci_dev *pdev, const struct pci_device_id *pid)
1295 {
1296 	struct bfad_s	*bfad;
1297 	int		error = -ENODEV, retval, i;
1298 
1299 	/* For single port cards - only claim function 0 */
1300 	if ((pdev->device == BFA_PCI_DEVICE_ID_FC_8G1P) &&
1301 		(PCI_FUNC(pdev->devfn) != 0))
1302 		return -ENODEV;
1303 
1304 	bfad = kzalloc(sizeof(struct bfad_s), GFP_KERNEL);
1305 	if (!bfad) {
1306 		error = -ENOMEM;
1307 		goto out;
1308 	}
1309 
1310 	bfad->trcmod = kzalloc(sizeof(struct bfa_trc_mod_s), GFP_KERNEL);
1311 	if (!bfad->trcmod) {
1312 		printk(KERN_WARNING "Error alloc trace buffer!\n");
1313 		error = -ENOMEM;
1314 		goto out_alloc_trace_failure;
1315 	}
1316 
1317 	/* TRACE INIT */
1318 	bfa_trc_init(bfad->trcmod);
1319 	bfa_trc(bfad, bfad_inst);
1320 
1321 	/* AEN INIT */
1322 	INIT_LIST_HEAD(&bfad->free_aen_q);
1323 	INIT_LIST_HEAD(&bfad->active_aen_q);
1324 	for (i = 0; i < BFA_AEN_MAX_ENTRY; i++)
1325 		list_add_tail(&bfad->aen_list[i].qe, &bfad->free_aen_q);
1326 
1327 	if (!(bfad_load_fwimg(pdev))) {
1328 		kfree(bfad->trcmod);
1329 		goto out_alloc_trace_failure;
1330 	}
1331 
1332 	retval = bfad_pci_init(pdev, bfad);
1333 	if (retval) {
1334 		printk(KERN_WARNING "bfad_pci_init failure!\n");
1335 		error = retval;
1336 		goto out_pci_init_failure;
1337 	}
1338 
1339 	mutex_lock(&bfad_mutex);
1340 	bfad->inst_no = bfad_inst++;
1341 	list_add_tail(&bfad->list_entry, &bfad_list);
1342 	mutex_unlock(&bfad_mutex);
1343 
1344 	/* Initializing the state machine: State set to uninit */
1345 	bfa_sm_set_state(bfad, bfad_sm_uninit);
1346 
1347 	spin_lock_init(&bfad->bfad_lock);
1348 	spin_lock_init(&bfad->bfad_aen_spinlock);
1349 
1350 	pci_set_drvdata(pdev, bfad);
1351 
1352 	bfad->ref_count = 0;
1353 	bfad->pport.bfad = bfad;
1354 	INIT_LIST_HEAD(&bfad->pbc_vport_list);
1355 	INIT_LIST_HEAD(&bfad->vport_list);
1356 
1357 	/* Setup the debugfs node for this bfad */
1358 	if (bfa_debugfs_enable)
1359 		bfad_debugfs_init(&bfad->pport);
1360 
1361 	retval = bfad_drv_init(bfad);
1362 	if (retval != BFA_STATUS_OK)
1363 		goto out_drv_init_failure;
1364 
1365 	bfa_sm_send_event(bfad, BFAD_E_CREATE);
1366 
1367 	if (bfa_sm_cmp_state(bfad, bfad_sm_uninit))
1368 		goto out_bfad_sm_failure;
1369 
1370 	return 0;
1371 
1372 out_bfad_sm_failure:
1373 	bfad_hal_mem_release(bfad);
1374 out_drv_init_failure:
1375 	/* Remove the debugfs node for this bfad */
1376 	kfree(bfad->regdata);
1377 	bfad_debugfs_exit(&bfad->pport);
1378 	mutex_lock(&bfad_mutex);
1379 	bfad_inst--;
1380 	list_del(&bfad->list_entry);
1381 	mutex_unlock(&bfad_mutex);
1382 	bfad_pci_uninit(pdev, bfad);
1383 out_pci_init_failure:
1384 	kfree(bfad->trcmod);
1385 out_alloc_trace_failure:
1386 	kfree(bfad);
1387 out:
1388 	return error;
1389 }
1390 
1391 /*
1392  * PCI remove entry.
1393  */
1394 void
1395 bfad_pci_remove(struct pci_dev *pdev)
1396 {
1397 	struct bfad_s	      *bfad = pci_get_drvdata(pdev);
1398 	unsigned long	flags;
1399 
1400 	bfa_trc(bfad, bfad->inst_no);
1401 
1402 	spin_lock_irqsave(&bfad->bfad_lock, flags);
1403 	if (bfad->bfad_tsk != NULL) {
1404 		spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1405 		kthread_stop(bfad->bfad_tsk);
1406 	} else {
1407 		spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1408 	}
1409 
1410 	/* Send Event BFAD_E_STOP */
1411 	bfa_sm_send_event(bfad, BFAD_E_STOP);
1412 
1413 	/* Driver detach and dealloc mem */
1414 	spin_lock_irqsave(&bfad->bfad_lock, flags);
1415 	bfa_detach(&bfad->bfa);
1416 	spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1417 	bfad_hal_mem_release(bfad);
1418 
1419 	/* Remove the debugfs node for this bfad */
1420 	kfree(bfad->regdata);
1421 	bfad_debugfs_exit(&bfad->pport);
1422 
1423 	/* Cleaning the BFAD instance */
1424 	mutex_lock(&bfad_mutex);
1425 	bfad_inst--;
1426 	list_del(&bfad->list_entry);
1427 	mutex_unlock(&bfad_mutex);
1428 	bfad_pci_uninit(pdev, bfad);
1429 
1430 	kfree(bfad->trcmod);
1431 	kfree(bfad);
1432 }
1433 
1434 /*
1435  * PCI Error Recovery entry, error detected.
1436  */
1437 static pci_ers_result_t
1438 bfad_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
1439 {
1440 	struct bfad_s *bfad = pci_get_drvdata(pdev);
1441 	unsigned long	flags;
1442 	pci_ers_result_t ret = PCI_ERS_RESULT_NONE;
1443 
1444 	dev_printk(KERN_ERR, &pdev->dev,
1445 		   "error detected state: %d - flags: 0x%x\n",
1446 		   state, bfad->bfad_flags);
1447 
1448 	switch (state) {
1449 	case pci_channel_io_normal: /* non-fatal error */
1450 		spin_lock_irqsave(&bfad->bfad_lock, flags);
1451 		bfad->bfad_flags &= ~BFAD_EEH_BUSY;
1452 		/* Suspend/fail all bfa operations */
1453 		bfa_ioc_suspend(&bfad->bfa.ioc);
1454 		spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1455 		del_timer_sync(&bfad->hal_tmo);
1456 		ret = PCI_ERS_RESULT_CAN_RECOVER;
1457 		break;
1458 	case pci_channel_io_frozen: /* fatal error */
1459 		init_completion(&bfad->comp);
1460 		spin_lock_irqsave(&bfad->bfad_lock, flags);
1461 		bfad->bfad_flags |= BFAD_EEH_BUSY;
1462 		/* Suspend/fail all bfa operations */
1463 		bfa_ioc_suspend(&bfad->bfa.ioc);
1464 		bfa_fcs_stop(&bfad->bfa_fcs);
1465 		spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1466 		wait_for_completion(&bfad->comp);
1467 
1468 		bfad_remove_intr(bfad);
1469 		del_timer_sync(&bfad->hal_tmo);
1470 		pci_disable_device(pdev);
1471 		ret = PCI_ERS_RESULT_NEED_RESET;
1472 		break;
1473 	case pci_channel_io_perm_failure: /* PCI Card is DEAD */
1474 		spin_lock_irqsave(&bfad->bfad_lock, flags);
1475 		bfad->bfad_flags |= BFAD_EEH_BUSY |
1476 				    BFAD_EEH_PCI_CHANNEL_IO_PERM_FAILURE;
1477 		spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1478 
1479 		/* If the error_detected handler is called with the reason
1480 		 * pci_channel_io_perm_failure - it will subsequently call
1481 		 * pci_remove() entry point to remove the pci device from the
1482 		 * system - So defer the cleanup to pci_remove(); cleaning up
1483 		 * here causes inconsistent state during pci_remove().
1484 		 */
1485 		ret = PCI_ERS_RESULT_DISCONNECT;
1486 		break;
1487 	default:
1488 		WARN_ON(1);
1489 	}
1490 
1491 	return ret;
1492 }
1493 
1494 int
1495 restart_bfa(struct bfad_s *bfad)
1496 {
1497 	unsigned long flags;
1498 	struct pci_dev *pdev = bfad->pcidev;
1499 
1500 	bfa_attach(&bfad->bfa, bfad, &bfad->ioc_cfg,
1501 		   &bfad->meminfo, &bfad->hal_pcidev);
1502 
1503 	/* Enable Interrupt and wait bfa_init completion */
1504 	if (bfad_setup_intr(bfad)) {
1505 		dev_printk(KERN_WARNING, &pdev->dev,
1506 			   "%s: bfad_setup_intr failed\n", bfad->pci_name);
1507 		bfa_sm_send_event(bfad, BFAD_E_INIT_FAILED);
1508 		return -1;
1509 	}
1510 
1511 	init_completion(&bfad->comp);
1512 	spin_lock_irqsave(&bfad->bfad_lock, flags);
1513 	bfa_iocfc_init(&bfad->bfa);
1514 	spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1515 
1516 	/* Set up interrupt handler for each vectors */
1517 	if ((bfad->bfad_flags & BFAD_MSIX_ON) &&
1518 	    bfad_install_msix_handler(bfad))
1519 		dev_printk(KERN_WARNING, &pdev->dev,
1520 			   "%s: install_msix failed.\n", bfad->pci_name);
1521 
1522 	bfad_init_timer(bfad);
1523 	wait_for_completion(&bfad->comp);
1524 	bfad_drv_start(bfad);
1525 
1526 	return 0;
1527 }
1528 
1529 /*
1530  * PCI Error Recovery entry, re-initialize the chip.
1531  */
1532 static pci_ers_result_t
1533 bfad_pci_slot_reset(struct pci_dev *pdev)
1534 {
1535 	struct bfad_s *bfad = pci_get_drvdata(pdev);
1536 	u8 byte;
1537 
1538 	dev_printk(KERN_ERR, &pdev->dev,
1539 		   "bfad_pci_slot_reset flags: 0x%x\n", bfad->bfad_flags);
1540 
1541 	if (pci_enable_device(pdev)) {
1542 		dev_printk(KERN_ERR, &pdev->dev, "Cannot re-enable "
1543 			   "PCI device after reset.\n");
1544 		return PCI_ERS_RESULT_DISCONNECT;
1545 	}
1546 
1547 	pci_restore_state(pdev);
1548 
1549 	/*
1550 	 * Read some byte (e.g. DMA max. payload size which can't
1551 	 * be 0xff any time) to make sure - we did not hit another PCI error
1552 	 * in the middle of recovery. If we did, then declare permanent failure.
1553 	 */
1554 	pci_read_config_byte(pdev, 0x68, &byte);
1555 	if (byte == 0xff) {
1556 		dev_printk(KERN_ERR, &pdev->dev,
1557 			   "slot_reset failed ... got another PCI error !\n");
1558 		goto out_disable_device;
1559 	}
1560 
1561 	pci_save_state(pdev);
1562 	pci_set_master(pdev);
1563 
1564 	if (dma_set_mask_and_coherent(&bfad->pcidev->dev, DMA_BIT_MASK(64)) ||
1565 	    dma_set_mask_and_coherent(&bfad->pcidev->dev, DMA_BIT_MASK(32)))
1566 		goto out_disable_device;
1567 
1568 	if (restart_bfa(bfad) == -1)
1569 		goto out_disable_device;
1570 
1571 	pci_enable_pcie_error_reporting(pdev);
1572 	dev_printk(KERN_WARNING, &pdev->dev,
1573 		   "slot_reset completed  flags: 0x%x!\n", bfad->bfad_flags);
1574 
1575 	return PCI_ERS_RESULT_RECOVERED;
1576 
1577 out_disable_device:
1578 	pci_disable_device(pdev);
1579 	return PCI_ERS_RESULT_DISCONNECT;
1580 }
1581 
1582 static pci_ers_result_t
1583 bfad_pci_mmio_enabled(struct pci_dev *pdev)
1584 {
1585 	unsigned long	flags;
1586 	struct bfad_s *bfad = pci_get_drvdata(pdev);
1587 
1588 	dev_printk(KERN_INFO, &pdev->dev, "mmio_enabled\n");
1589 
1590 	/* Fetch FW diagnostic information */
1591 	bfa_ioc_debug_save_ftrc(&bfad->bfa.ioc);
1592 
1593 	/* Cancel all pending IOs */
1594 	spin_lock_irqsave(&bfad->bfad_lock, flags);
1595 	init_completion(&bfad->comp);
1596 	bfa_fcs_stop(&bfad->bfa_fcs);
1597 	spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1598 	wait_for_completion(&bfad->comp);
1599 
1600 	bfad_remove_intr(bfad);
1601 	del_timer_sync(&bfad->hal_tmo);
1602 	pci_disable_device(pdev);
1603 
1604 	return PCI_ERS_RESULT_NEED_RESET;
1605 }
1606 
1607 static void
1608 bfad_pci_resume(struct pci_dev *pdev)
1609 {
1610 	unsigned long	flags;
1611 	struct bfad_s *bfad = pci_get_drvdata(pdev);
1612 
1613 	dev_printk(KERN_WARNING, &pdev->dev, "resume\n");
1614 
1615 	/* wait until the link is online */
1616 	bfad_rport_online_wait(bfad);
1617 
1618 	spin_lock_irqsave(&bfad->bfad_lock, flags);
1619 	bfad->bfad_flags &= ~BFAD_EEH_BUSY;
1620 	spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1621 }
1622 
1623 struct pci_device_id bfad_id_table[] = {
1624 	{
1625 		.vendor = BFA_PCI_VENDOR_ID_BROCADE,
1626 		.device = BFA_PCI_DEVICE_ID_FC_8G2P,
1627 		.subvendor = PCI_ANY_ID,
1628 		.subdevice = PCI_ANY_ID,
1629 	},
1630 	{
1631 		.vendor = BFA_PCI_VENDOR_ID_BROCADE,
1632 		.device = BFA_PCI_DEVICE_ID_FC_8G1P,
1633 		.subvendor = PCI_ANY_ID,
1634 		.subdevice = PCI_ANY_ID,
1635 	},
1636 	{
1637 		.vendor = BFA_PCI_VENDOR_ID_BROCADE,
1638 		.device = BFA_PCI_DEVICE_ID_CT,
1639 		.subvendor = PCI_ANY_ID,
1640 		.subdevice = PCI_ANY_ID,
1641 		.class = (PCI_CLASS_SERIAL_FIBER << 8),
1642 		.class_mask = ~0,
1643 	},
1644 	{
1645 		.vendor = BFA_PCI_VENDOR_ID_BROCADE,
1646 		.device = BFA_PCI_DEVICE_ID_CT_FC,
1647 		.subvendor = PCI_ANY_ID,
1648 		.subdevice = PCI_ANY_ID,
1649 		.class = (PCI_CLASS_SERIAL_FIBER << 8),
1650 		.class_mask = ~0,
1651 	},
1652 	{
1653 		.vendor = BFA_PCI_VENDOR_ID_BROCADE,
1654 		.device = BFA_PCI_DEVICE_ID_CT2,
1655 		.subvendor = PCI_ANY_ID,
1656 		.subdevice = PCI_ANY_ID,
1657 		.class = (PCI_CLASS_SERIAL_FIBER << 8),
1658 		.class_mask = ~0,
1659 	},
1660 
1661 	{
1662 		.vendor = BFA_PCI_VENDOR_ID_BROCADE,
1663 		.device = BFA_PCI_DEVICE_ID_CT2_QUAD,
1664 		.subvendor = PCI_ANY_ID,
1665 		.subdevice = PCI_ANY_ID,
1666 		.class = (PCI_CLASS_SERIAL_FIBER << 8),
1667 		.class_mask = ~0,
1668 	},
1669 	{0, 0},
1670 };
1671 
1672 MODULE_DEVICE_TABLE(pci, bfad_id_table);
1673 
1674 /*
1675  * PCI error recovery handlers.
1676  */
1677 static struct pci_error_handlers bfad_err_handler = {
1678 	.error_detected = bfad_pci_error_detected,
1679 	.slot_reset = bfad_pci_slot_reset,
1680 	.mmio_enabled = bfad_pci_mmio_enabled,
1681 	.resume = bfad_pci_resume,
1682 };
1683 
1684 static struct pci_driver bfad_pci_driver = {
1685 	.name = BFAD_DRIVER_NAME,
1686 	.id_table = bfad_id_table,
1687 	.probe = bfad_pci_probe,
1688 	.remove = bfad_pci_remove,
1689 	.err_handler = &bfad_err_handler,
1690 };
1691 
1692 /*
1693  * Driver module init.
1694  */
1695 static int __init
1696 bfad_init(void)
1697 {
1698 	int		error = 0;
1699 
1700 	pr_info("QLogic BR-series BFA FC/FCOE SCSI driver - version: %s\n",
1701 			BFAD_DRIVER_VERSION);
1702 
1703 	if (num_sgpgs > 0)
1704 		num_sgpgs_parm = num_sgpgs;
1705 
1706 	error = bfad_im_module_init();
1707 	if (error) {
1708 		error = -ENOMEM;
1709 		printk(KERN_WARNING "bfad_im_module_init failure\n");
1710 		goto ext;
1711 	}
1712 
1713 	if (strcmp(FCPI_NAME, " fcpim") == 0)
1714 		supported_fc4s |= BFA_LPORT_ROLE_FCP_IM;
1715 
1716 	bfa_auto_recover = ioc_auto_recover;
1717 	bfa_fcs_rport_set_del_timeout(rport_del_timeout);
1718 	bfa_fcs_rport_set_max_logins(max_rport_logins);
1719 
1720 	error = pci_register_driver(&bfad_pci_driver);
1721 	if (error) {
1722 		printk(KERN_WARNING "pci_register_driver failure\n");
1723 		goto ext;
1724 	}
1725 
1726 	return 0;
1727 
1728 ext:
1729 	bfad_im_module_exit();
1730 	return error;
1731 }
1732 
1733 /*
1734  * Driver module exit.
1735  */
1736 static void __exit
1737 bfad_exit(void)
1738 {
1739 	pci_unregister_driver(&bfad_pci_driver);
1740 	bfad_im_module_exit();
1741 	bfad_free_fwimg();
1742 }
1743 
1744 /* Firmware handling */
1745 static void
1746 bfad_read_firmware(struct pci_dev *pdev, u32 **bfi_image,
1747 		u32 *bfi_image_size, char *fw_name)
1748 {
1749 	const struct firmware *fw;
1750 
1751 	if (request_firmware(&fw, fw_name, &pdev->dev)) {
1752 		printk(KERN_ALERT "Can't locate firmware %s\n", fw_name);
1753 		*bfi_image = NULL;
1754 		goto out;
1755 	}
1756 
1757 	*bfi_image = vmalloc(fw->size);
1758 	if (NULL == *bfi_image) {
1759 		printk(KERN_ALERT "Fail to allocate buffer for fw image "
1760 			"size=%x!\n", (u32) fw->size);
1761 		goto out;
1762 	}
1763 
1764 	memcpy(*bfi_image, fw->data, fw->size);
1765 	*bfi_image_size = fw->size/sizeof(u32);
1766 out:
1767 	release_firmware(fw);
1768 }
1769 
1770 static u32 *
1771 bfad_load_fwimg(struct pci_dev *pdev)
1772 {
1773 	if (bfa_asic_id_ct2(pdev->device)) {
1774 		if (bfi_image_ct2_size == 0)
1775 			bfad_read_firmware(pdev, &bfi_image_ct2,
1776 				&bfi_image_ct2_size, BFAD_FW_FILE_CT2);
1777 		return bfi_image_ct2;
1778 	} else if (bfa_asic_id_ct(pdev->device)) {
1779 		if (bfi_image_ct_size == 0)
1780 			bfad_read_firmware(pdev, &bfi_image_ct,
1781 				&bfi_image_ct_size, BFAD_FW_FILE_CT);
1782 		return bfi_image_ct;
1783 	} else if (bfa_asic_id_cb(pdev->device)) {
1784 		if (bfi_image_cb_size == 0)
1785 			bfad_read_firmware(pdev, &bfi_image_cb,
1786 				&bfi_image_cb_size, BFAD_FW_FILE_CB);
1787 		return bfi_image_cb;
1788 	}
1789 
1790 	return NULL;
1791 }
1792 
1793 static void
1794 bfad_free_fwimg(void)
1795 {
1796 	if (bfi_image_ct2_size && bfi_image_ct2)
1797 		vfree(bfi_image_ct2);
1798 	if (bfi_image_ct_size && bfi_image_ct)
1799 		vfree(bfi_image_ct);
1800 	if (bfi_image_cb_size && bfi_image_cb)
1801 		vfree(bfi_image_cb);
1802 }
1803 
1804 module_init(bfad_init);
1805 module_exit(bfad_exit);
1806 MODULE_LICENSE("GPL");
1807 MODULE_DESCRIPTION("QLogic BR-series Fibre Channel HBA Driver" BFAD_PROTO_NAME);
1808 MODULE_AUTHOR("QLogic Corporation");
1809 MODULE_VERSION(BFAD_DRIVER_VERSION);
1810