blob: 79f8b86e30e3e34b0dc52200a706ee9b5528831d [file] [log] [blame]
Jing Huang7725ccf2009-09-23 17:46:15 -07001/*
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07002 * Copyright (c) 2005-2010 Brocade Communications Systems, Inc.
Jing Huang7725ccf2009-09-23 17:46:15 -07003 * All rights reserved
4 * www.brocade.com
5 *
6 * Linux driver for Brocade Fibre Channel Host Bus Adapter.
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License (GPL) Version 2 as
10 * published by the Free Software Foundation
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 */
17
18/**
19 * bfad.c Linux driver PCI interface module.
20 */
Jing Huang7725ccf2009-09-23 17:46:15 -070021#include <linux/module.h>
Krishna Gudipatie6714322010-03-03 17:44:02 -080022#include <linux/kthread.h>
Krishna Gudipatia36c61f2010-09-15 11:50:55 -070023#include <linux/errno.h>
24#include <linux/sched.h>
25#include <linux/init.h>
26#include <linux/fs.h>
27#include <linux/pci.h>
28#include <linux/firmware.h>
29#include <asm/uaccess.h>
30#include <asm/fcntl.h>
31
Jing Huang7725ccf2009-09-23 17:46:15 -070032#include "bfad_drv.h"
33#include "bfad_im.h"
Krishna Gudipatia36c61f2010-09-15 11:50:55 -070034#include "bfa_fcs.h"
35#include "bfa_os_inc.h"
36#include "bfa_defs.h"
37#include "bfa.h"
Jing Huang7725ccf2009-09-23 17:46:15 -070038
39BFA_TRC_FILE(LDRV, BFAD);
Jing Huang42b426e2010-03-19 11:07:09 -070040DEFINE_MUTEX(bfad_mutex);
Jing Huang7725ccf2009-09-23 17:46:15 -070041LIST_HEAD(bfad_list);
Jing Huang7725ccf2009-09-23 17:46:15 -070042
Krishna Gudipatia36c61f2010-09-15 11:50:55 -070043static int bfad_inst;
44static int num_sgpgs_parm;
45int supported_fc4s;
46char *host_name, *os_name, *os_patch;
47int num_rports, num_ios, num_tms;
48int num_fcxps, num_ufbufs;
49int reqq_size, rspq_size, num_sgpgs;
50int rport_del_timeout = BFA_FCS_RPORT_DEF_DEL_TIMEOUT;
51int bfa_lun_queue_depth = BFAD_LUN_QUEUE_DEPTH;
52int bfa_io_max_sge = BFAD_IO_MAX_SGE;
53int log_level = 3; /* WARNING log level */
54int ioc_auto_recover = BFA_TRUE;
55int bfa_linkup_delay = -1;
56int fdmi_enable = BFA_TRUE;
57int pcie_max_read_reqsz;
Jing Huangab2a9ba2010-07-08 20:02:55 -070058int bfa_debugfs_enable = 1;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -070059int msix_disable_cb = 0, msix_disable_ct = 0;
60
61u32 bfi_image_ct_fc_size, bfi_image_ct_cna_size, bfi_image_cb_fc_size;
62u32 *bfi_image_ct_fc, *bfi_image_ct_cna, *bfi_image_cb_fc;
63
64const char *msix_name_ct[] = {
65 "cpe0", "cpe1", "cpe2", "cpe3",
66 "rme0", "rme1", "rme2", "rme3",
67 "ctrl" };
68
69const char *msix_name_cb[] = {
70 "cpe0", "cpe1", "cpe2", "cpe3",
71 "rme0", "rme1", "rme2", "rme3",
72 "eemc", "elpu0", "elpu1", "epss", "mlpu" };
73
74MODULE_FIRMWARE(BFAD_FW_FILE_CT_FC);
75MODULE_FIRMWARE(BFAD_FW_FILE_CT_CNA);
76MODULE_FIRMWARE(BFAD_FW_FILE_CB_FC);
Jing Huang7725ccf2009-09-23 17:46:15 -070077
78module_param(os_name, charp, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -070079MODULE_PARM_DESC(os_name, "OS name of the hba host machine");
Jing Huang7725ccf2009-09-23 17:46:15 -070080module_param(os_patch, charp, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -070081MODULE_PARM_DESC(os_patch, "OS patch level of the hba host machine");
Jing Huang7725ccf2009-09-23 17:46:15 -070082module_param(host_name, charp, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -070083MODULE_PARM_DESC(host_name, "Hostname of the hba host machine");
Jing Huang7725ccf2009-09-23 17:46:15 -070084module_param(num_rports, int, S_IRUGO | S_IWUSR);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -070085MODULE_PARM_DESC(num_rports, "Max number of rports supported per port "
86 "(physical/logical), default=1024");
Jing Huang7725ccf2009-09-23 17:46:15 -070087module_param(num_ios, int, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -070088MODULE_PARM_DESC(num_ios, "Max number of ioim requests, default=2000");
Jing Huang7725ccf2009-09-23 17:46:15 -070089module_param(num_tms, int, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -070090MODULE_PARM_DESC(num_tms, "Max number of task im requests, default=128");
Jing Huang7725ccf2009-09-23 17:46:15 -070091module_param(num_fcxps, int, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -070092MODULE_PARM_DESC(num_fcxps, "Max number of fcxp requests, default=64");
Jing Huang7725ccf2009-09-23 17:46:15 -070093module_param(num_ufbufs, int, S_IRUGO | S_IWUSR);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -070094MODULE_PARM_DESC(num_ufbufs, "Max number of unsolicited frame "
95 "buffers, default=64");
Jing Huang7725ccf2009-09-23 17:46:15 -070096module_param(reqq_size, int, S_IRUGO | S_IWUSR);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -070097MODULE_PARM_DESC(reqq_size, "Max number of request queue elements, "
98 "default=256");
Jing Huang7725ccf2009-09-23 17:46:15 -070099module_param(rspq_size, int, S_IRUGO | S_IWUSR);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700100MODULE_PARM_DESC(rspq_size, "Max number of response queue elements, "
101 "default=64");
Jing Huang7725ccf2009-09-23 17:46:15 -0700102module_param(num_sgpgs, int, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -0700103MODULE_PARM_DESC(num_sgpgs, "Number of scatter/gather pages, default=2048");
Jing Huang7725ccf2009-09-23 17:46:15 -0700104module_param(rport_del_timeout, int, S_IRUGO | S_IWUSR);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700105MODULE_PARM_DESC(rport_del_timeout, "Rport delete timeout, default=90 secs, "
106 "Range[>0]");
Jing Huang7725ccf2009-09-23 17:46:15 -0700107module_param(bfa_lun_queue_depth, int, S_IRUGO | S_IWUSR);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700108MODULE_PARM_DESC(bfa_lun_queue_depth, "Lun queue depth, default=32, Range[>0]");
Jing Huang7725ccf2009-09-23 17:46:15 -0700109module_param(bfa_io_max_sge, int, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -0700110MODULE_PARM_DESC(bfa_io_max_sge, "Max io scatter/gather elements, default=255");
Jing Huang7725ccf2009-09-23 17:46:15 -0700111module_param(log_level, int, S_IRUGO | S_IWUSR);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700112MODULE_PARM_DESC(log_level, "Driver log level, default=3, "
113 "Range[Critical:1|Error:2|Warning:3|Info:4]");
Jing Huang7725ccf2009-09-23 17:46:15 -0700114module_param(ioc_auto_recover, int, S_IRUGO | S_IWUSR);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700115MODULE_PARM_DESC(ioc_auto_recover, "IOC auto recovery, default=1, "
116 "Range[off:0|on:1]");
Jing Huang7725ccf2009-09-23 17:46:15 -0700117module_param(bfa_linkup_delay, int, S_IRUGO | S_IWUSR);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700118MODULE_PARM_DESC(bfa_linkup_delay, "Link up delay, default=30 secs for "
119 "boot port. Otherwise 10 secs in RHEL4 & 0 for "
120 "[RHEL5, SLES10, ESX40] Range[>0]");
121module_param(msix_disable_cb, int, S_IRUGO | S_IWUSR);
122MODULE_PARM_DESC(msix_disable_cb, "Disable Message Signaled Interrupts "
123 "for Brocade-415/425/815/825 cards, default=0, "
124 " Range[false:0|true:1]");
125module_param(msix_disable_ct, int, S_IRUGO | S_IWUSR);
126MODULE_PARM_DESC(msix_disable_ct, "Disable Message Signaled Interrupts "
127 "if possible for Brocade-1010/1020/804/1007/902/1741 "
128 "cards, default=0, Range[false:0|true:1]");
Jing Huang604158ad2010-07-08 19:59:49 -0700129module_param(fdmi_enable, int, S_IRUGO | S_IWUSR);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700130MODULE_PARM_DESC(fdmi_enable, "Enables fdmi registration, default=1, "
131 "Range[false:0|true:1]");
132module_param(pcie_max_read_reqsz, int, S_IRUGO | S_IWUSR);
133MODULE_PARM_DESC(pcie_max_read_reqsz, "PCIe max read request size, default=0 "
134 "(use system setting), Range[128|256|512|1024|2048|4096]");
Jing Huangab2a9ba2010-07-08 20:02:55 -0700135module_param(bfa_debugfs_enable, int, S_IRUGO | S_IWUSR);
136MODULE_PARM_DESC(bfa_debugfs_enable, "Enables debugfs feature, default=1,"
137 " Range[false:0|true:1]");
Jing Huang7725ccf2009-09-23 17:46:15 -0700138
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700139static void
140bfad_sm_uninit(struct bfad_s *bfad, enum bfad_sm_event event);
141static void
142bfad_sm_created(struct bfad_s *bfad, enum bfad_sm_event event);
143static void
144bfad_sm_initializing(struct bfad_s *bfad, enum bfad_sm_event event);
145static void
146bfad_sm_operational(struct bfad_s *bfad, enum bfad_sm_event event);
147static void
148bfad_sm_stopping(struct bfad_s *bfad, enum bfad_sm_event event);
149static void
150bfad_sm_failed(struct bfad_s *bfad, enum bfad_sm_event event);
151static void
152bfad_sm_fcs_exit(struct bfad_s *bfad, enum bfad_sm_event event);
153
154/**
155 * Beginning state for the driver instance, awaiting the pci_probe event
Jing Huang7725ccf2009-09-23 17:46:15 -0700156 */
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700157static void
158bfad_sm_uninit(struct bfad_s *bfad, enum bfad_sm_event event)
Jing Huang7725ccf2009-09-23 17:46:15 -0700159{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700160 bfa_trc(bfad, event);
Jing Huang7725ccf2009-09-23 17:46:15 -0700161
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700162 switch (event) {
163 case BFAD_E_CREATE:
164 bfa_sm_set_state(bfad, bfad_sm_created);
165 bfad->bfad_tsk = kthread_create(bfad_worker, (void *) bfad,
166 "%s", "bfad_worker");
167 if (IS_ERR(bfad->bfad_tsk)) {
168 printk(KERN_INFO "bfad[%d]: Kernel thread "
169 "creation failed!\n", bfad->inst_no);
170 bfa_sm_send_event(bfad, BFAD_E_KTHREAD_CREATE_FAILED);
171 }
172 bfa_sm_send_event(bfad, BFAD_E_INIT);
173 break;
Jing Huang7725ccf2009-09-23 17:46:15 -0700174
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700175 case BFAD_E_STOP:
176 /* Ignore stop; already in uninit */
177 break;
Jing Huang7725ccf2009-09-23 17:46:15 -0700178
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700179 default:
180 bfa_sm_fault(bfad, event);
181 }
182}
Krishna Gudipatie6714322010-03-03 17:44:02 -0800183
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700184/**
185 * Driver Instance is created, awaiting event INIT to initialize the bfad
186 */
187static void
188bfad_sm_created(struct bfad_s *bfad, enum bfad_sm_event event)
189{
190 unsigned long flags;
191
192 bfa_trc(bfad, event);
193
194 switch (event) {
195 case BFAD_E_INIT:
196 bfa_sm_set_state(bfad, bfad_sm_initializing);
197
198 init_completion(&bfad->comp);
199
200 /* Enable Interrupt and wait bfa_init completion */
201 if (bfad_setup_intr(bfad)) {
202 printk(KERN_WARNING "bfad%d: bfad_setup_intr failed\n",
203 bfad->inst_no);
204 bfa_sm_send_event(bfad, BFAD_E_INTR_INIT_FAILED);
205 break;
206 }
207
208 spin_lock_irqsave(&bfad->bfad_lock, flags);
209 bfa_init(&bfad->bfa);
210 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
211
212 /* Set up interrupt handler for each vectors */
213 if ((bfad->bfad_flags & BFAD_MSIX_ON) &&
214 bfad_install_msix_handler(bfad)) {
215 printk(KERN_WARNING "%s: install_msix failed, bfad%d\n",
216 __func__, bfad->inst_no);
217 }
218
219 bfad_init_timer(bfad);
220
221 wait_for_completion(&bfad->comp);
222
223 if ((bfad->bfad_flags & BFAD_HAL_INIT_DONE)) {
224 bfa_sm_send_event(bfad, BFAD_E_INIT_SUCCESS);
225 } else {
226 bfad->bfad_flags |= BFAD_HAL_INIT_FAIL;
227 bfa_sm_send_event(bfad, BFAD_E_INIT_FAILED);
228 }
229
230 break;
231
232 case BFAD_E_KTHREAD_CREATE_FAILED:
233 bfa_sm_set_state(bfad, bfad_sm_uninit);
234 break;
235
236 default:
237 bfa_sm_fault(bfad, event);
238 }
Jing Huang7725ccf2009-09-23 17:46:15 -0700239}
240
241static void
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700242bfad_sm_initializing(struct bfad_s *bfad, enum bfad_sm_event event)
Jing Huang7725ccf2009-09-23 17:46:15 -0700243{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700244 int retval;
245 unsigned long flags;
246
247 bfa_trc(bfad, event);
248
249 switch (event) {
250 case BFAD_E_INIT_SUCCESS:
251 kthread_stop(bfad->bfad_tsk);
252 spin_lock_irqsave(&bfad->bfad_lock, flags);
253 bfad->bfad_tsk = NULL;
254 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
255
256 retval = bfad_start_ops(bfad);
257 if (retval != BFA_STATUS_OK)
258 break;
259 bfa_sm_set_state(bfad, bfad_sm_operational);
260 break;
261
262 case BFAD_E_INTR_INIT_FAILED:
263 bfa_sm_set_state(bfad, bfad_sm_uninit);
264 kthread_stop(bfad->bfad_tsk);
265 spin_lock_irqsave(&bfad->bfad_lock, flags);
266 bfad->bfad_tsk = NULL;
267 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
268 break;
269
270 case BFAD_E_INIT_FAILED:
271 bfa_sm_set_state(bfad, bfad_sm_failed);
272 break;
273 default:
274 bfa_sm_fault(bfad, event);
275 }
Jing Huang7725ccf2009-09-23 17:46:15 -0700276}
277
278static void
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700279bfad_sm_failed(struct bfad_s *bfad, enum bfad_sm_event event)
Jing Huang7725ccf2009-09-23 17:46:15 -0700280{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700281 int retval;
Jing Huang7725ccf2009-09-23 17:46:15 -0700282
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700283 bfa_trc(bfad, event);
Jing Huang7725ccf2009-09-23 17:46:15 -0700284
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700285 switch (event) {
286 case BFAD_E_INIT_SUCCESS:
287 retval = bfad_start_ops(bfad);
288 if (retval != BFA_STATUS_OK)
289 break;
290 bfa_sm_set_state(bfad, bfad_sm_operational);
291 break;
Jing Huang7725ccf2009-09-23 17:46:15 -0700292
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700293 case BFAD_E_STOP:
294 if (bfad->bfad_flags & BFAD_CFG_PPORT_DONE)
295 bfad_uncfg_pport(bfad);
296 if (bfad->bfad_flags & BFAD_FC4_PROBE_DONE) {
297 bfad_im_probe_undo(bfad);
298 bfad->bfad_flags &= ~BFAD_FC4_PROBE_DONE;
299 }
300 bfad_stop(bfad);
301 break;
Jing Huang7725ccf2009-09-23 17:46:15 -0700302
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700303 case BFAD_E_EXIT_COMP:
304 bfa_sm_set_state(bfad, bfad_sm_uninit);
305 bfad_remove_intr(bfad);
306 del_timer_sync(&bfad->hal_tmo);
307 break;
Jing Huang7725ccf2009-09-23 17:46:15 -0700308
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700309 default:
310 bfa_sm_fault(bfad, event);
311 }
Jing Huang7725ccf2009-09-23 17:46:15 -0700312}
313
314static void
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700315bfad_sm_operational(struct bfad_s *bfad, enum bfad_sm_event event)
Jing Huang7725ccf2009-09-23 17:46:15 -0700316{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700317 bfa_trc(bfad, event);
Jing Huang7725ccf2009-09-23 17:46:15 -0700318
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700319 switch (event) {
320 case BFAD_E_STOP:
321 bfa_sm_set_state(bfad, bfad_sm_fcs_exit);
322 bfad_fcs_stop(bfad);
323 break;
Jing Huang7725ccf2009-09-23 17:46:15 -0700324
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700325 default:
326 bfa_sm_fault(bfad, event);
327 }
328}
329
330static void
331bfad_sm_fcs_exit(struct bfad_s *bfad, enum bfad_sm_event event)
332{
333 bfa_trc(bfad, event);
334
335 switch (event) {
336 case BFAD_E_FCS_EXIT_COMP:
337 bfa_sm_set_state(bfad, bfad_sm_stopping);
338 bfad_stop(bfad);
339 break;
340
341 default:
342 bfa_sm_fault(bfad, event);
343 }
344}
345
346static void
347bfad_sm_stopping(struct bfad_s *bfad, enum bfad_sm_event event)
348{
349 bfa_trc(bfad, event);
350
351 switch (event) {
352 case BFAD_E_EXIT_COMP:
353 bfa_sm_set_state(bfad, bfad_sm_uninit);
354 bfad_remove_intr(bfad);
355 del_timer_sync(&bfad->hal_tmo);
356 bfad_im_probe_undo(bfad);
357 bfad->bfad_flags &= ~BFAD_FC4_PROBE_DONE;
358 bfad_uncfg_pport(bfad);
359 break;
360
361 default:
362 bfa_sm_fault(bfad, event);
363 break;
364 }
Jing Huang7725ccf2009-09-23 17:46:15 -0700365}
366
367/**
368 * BFA callbacks
369 */
370void
371bfad_hcb_comp(void *arg, bfa_status_t status)
372{
373 struct bfad_hal_comp *fcomp = (struct bfad_hal_comp *)arg;
374
375 fcomp->status = status;
376 complete(&fcomp->comp);
377}
378
379/**
380 * bfa_init callback
381 */
382void
383bfa_cb_init(void *drv, bfa_status_t init_status)
384{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700385 struct bfad_s *bfad = drv;
Jing Huang7725ccf2009-09-23 17:46:15 -0700386
Krishna Gudipatie6714322010-03-03 17:44:02 -0800387 if (init_status == BFA_STATUS_OK) {
Jing Huang7725ccf2009-09-23 17:46:15 -0700388 bfad->bfad_flags |= BFAD_HAL_INIT_DONE;
389
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700390 /*
391 * If BFAD_HAL_INIT_FAIL flag is set:
Krishna Gudipatie6714322010-03-03 17:44:02 -0800392 * Wake up the kernel thread to start
393 * the bfad operations after HAL init done
394 */
395 if ((bfad->bfad_flags & BFAD_HAL_INIT_FAIL)) {
396 bfad->bfad_flags &= ~BFAD_HAL_INIT_FAIL;
397 wake_up_process(bfad->bfad_tsk);
398 }
399 }
400
Jing Huang7725ccf2009-09-23 17:46:15 -0700401 complete(&bfad->comp);
402}
403
Jing Huang7725ccf2009-09-23 17:46:15 -0700404/**
405 * BFA_FCS callbacks
406 */
Jing Huang7725ccf2009-09-23 17:46:15 -0700407struct bfad_port_s *
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700408bfa_fcb_lport_new(struct bfad_s *bfad, struct bfa_fcs_lport_s *port,
409 enum bfa_lport_role roles, struct bfad_vf_s *vf_drv,
Jing Huang7725ccf2009-09-23 17:46:15 -0700410 struct bfad_vport_s *vp_drv)
411{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700412 bfa_status_t rc;
413 struct bfad_port_s *port_drv;
Jing Huang7725ccf2009-09-23 17:46:15 -0700414
415 if (!vp_drv && !vf_drv) {
416 port_drv = &bfad->pport;
417 port_drv->pvb_type = BFAD_PORT_PHYS_BASE;
418 } else if (!vp_drv && vf_drv) {
419 port_drv = &vf_drv->base_port;
420 port_drv->pvb_type = BFAD_PORT_VF_BASE;
421 } else if (vp_drv && !vf_drv) {
422 port_drv = &vp_drv->drv_port;
423 port_drv->pvb_type = BFAD_PORT_PHYS_VPORT;
424 } else {
425 port_drv = &vp_drv->drv_port;
426 port_drv->pvb_type = BFAD_PORT_VF_VPORT;
427 }
428
429 port_drv->fcs_port = port;
430 port_drv->roles = roles;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700431
432 if (roles & BFA_LPORT_ROLE_FCP_IM) {
433 rc = bfad_im_port_new(bfad, port_drv);
434 if (rc != BFA_STATUS_OK) {
435 bfad_im_port_delete(bfad, port_drv);
436 port_drv = NULL;
437 }
Jing Huang7725ccf2009-09-23 17:46:15 -0700438 }
439
440 return port_drv;
441}
442
443void
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700444bfa_fcb_lport_delete(struct bfad_s *bfad, enum bfa_lport_role roles,
Jing Huang7725ccf2009-09-23 17:46:15 -0700445 struct bfad_vf_s *vf_drv, struct bfad_vport_s *vp_drv)
446{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700447 struct bfad_port_s *port_drv;
Jing Huang7725ccf2009-09-23 17:46:15 -0700448
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700449 /* this will be only called from rmmod context */
Jing Huang7725ccf2009-09-23 17:46:15 -0700450 if (vp_drv && !vp_drv->comp_del) {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700451 port_drv = (vp_drv) ? (&(vp_drv)->drv_port) :
452 ((vf_drv) ? (&(vf_drv)->base_port) :
453 (&(bfad)->pport));
Jing Huang7725ccf2009-09-23 17:46:15 -0700454 bfa_trc(bfad, roles);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700455 if (roles & BFA_LPORT_ROLE_FCP_IM)
456 bfad_im_port_delete(bfad, port_drv);
Jing Huang7725ccf2009-09-23 17:46:15 -0700457 }
Jing Huang7725ccf2009-09-23 17:46:15 -0700458}
459
460/**
461 * FCS RPORT alloc callback, after successful PLOGI by FCS
462 */
463bfa_status_t
464bfa_fcb_rport_alloc(struct bfad_s *bfad, struct bfa_fcs_rport_s **rport,
465 struct bfad_rport_s **rport_drv)
466{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700467 bfa_status_t rc = BFA_STATUS_OK;
Jing Huang7725ccf2009-09-23 17:46:15 -0700468
469 *rport_drv = kzalloc(sizeof(struct bfad_rport_s), GFP_ATOMIC);
470 if (*rport_drv == NULL) {
471 rc = BFA_STATUS_ENOMEM;
472 goto ext;
473 }
474
475 *rport = &(*rport_drv)->fcs_rport;
476
477ext:
478 return rc;
479}
480
Jing Huangd9883542010-07-08 19:46:26 -0700481/**
Jing Huangd9883542010-07-08 19:46:26 -0700482 * FCS PBC VPORT Create
483 */
484void
485bfa_fcb_pbc_vport_create(struct bfad_s *bfad, struct bfi_pbc_vport_s pbc_vport)
486{
Jing Huang7725ccf2009-09-23 17:46:15 -0700487
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700488 struct bfa_lport_cfg_s port_cfg = {0};
489 struct bfad_vport_s *vport;
490 int rc;
Jing Huangd9883542010-07-08 19:46:26 -0700491
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700492 vport = kzalloc(sizeof(struct bfad_vport_s), GFP_KERNEL);
493 if (!vport) {
Jing Huangd9883542010-07-08 19:46:26 -0700494 bfa_trc(bfad, 0);
495 return;
496 }
497
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700498 vport->drv_port.bfad = bfad;
499 port_cfg.roles = BFA_LPORT_ROLE_FCP_IM;
500 port_cfg.pwwn = pbc_vport.vp_pwwn;
501 port_cfg.nwwn = pbc_vport.vp_nwwn;
502 port_cfg.preboot_vp = BFA_TRUE;
Jing Huangd9883542010-07-08 19:46:26 -0700503
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700504 rc = bfa_fcs_pbc_vport_create(&vport->fcs_vport, &bfad->bfa_fcs, 0,
505 &port_cfg, vport);
Jing Huangd9883542010-07-08 19:46:26 -0700506
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700507 if (rc != BFA_STATUS_OK) {
508 bfa_trc(bfad, 0);
509 return;
510 }
511
512 list_add_tail(&vport->list_entry, &bfad->pbc_vport_list);
Jing Huangd9883542010-07-08 19:46:26 -0700513}
Jing Huang7725ccf2009-09-23 17:46:15 -0700514
515void
516bfad_hal_mem_release(struct bfad_s *bfad)
517{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700518 int i;
Jing Huang7725ccf2009-09-23 17:46:15 -0700519 struct bfa_meminfo_s *hal_meminfo = &bfad->meminfo;
520 struct bfa_mem_elem_s *meminfo_elem;
521
522 for (i = 0; i < BFA_MEM_TYPE_MAX; i++) {
523 meminfo_elem = &hal_meminfo->meminfo[i];
524 if (meminfo_elem->kva != NULL) {
525 switch (meminfo_elem->mem_type) {
526 case BFA_MEM_TYPE_KVA:
527 vfree(meminfo_elem->kva);
528 break;
529 case BFA_MEM_TYPE_DMA:
530 dma_free_coherent(&bfad->pcidev->dev,
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700531 meminfo_elem->mem_len,
532 meminfo_elem->kva,
533 (dma_addr_t) meminfo_elem->dma);
Jing Huang7725ccf2009-09-23 17:46:15 -0700534 break;
535 default:
536 bfa_assert(0);
537 break;
538 }
539 }
540 }
541
542 memset(hal_meminfo, 0, sizeof(struct bfa_meminfo_s));
543}
544
545void
546bfad_update_hal_cfg(struct bfa_iocfc_cfg_s *bfa_cfg)
547{
548 if (num_rports > 0)
549 bfa_cfg->fwcfg.num_rports = num_rports;
550 if (num_ios > 0)
551 bfa_cfg->fwcfg.num_ioim_reqs = num_ios;
552 if (num_tms > 0)
553 bfa_cfg->fwcfg.num_tskim_reqs = num_tms;
554 if (num_fcxps > 0)
555 bfa_cfg->fwcfg.num_fcxp_reqs = num_fcxps;
556 if (num_ufbufs > 0)
557 bfa_cfg->fwcfg.num_uf_bufs = num_ufbufs;
558 if (reqq_size > 0)
559 bfa_cfg->drvcfg.num_reqq_elems = reqq_size;
560 if (rspq_size > 0)
561 bfa_cfg->drvcfg.num_rspq_elems = rspq_size;
562 if (num_sgpgs > 0)
563 bfa_cfg->drvcfg.num_sgpgs = num_sgpgs;
564
565 /*
566 * populate the hal values back to the driver for sysfs use.
567 * otherwise, the default values will be shown as 0 in sysfs
568 */
569 num_rports = bfa_cfg->fwcfg.num_rports;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700570 num_ios = bfa_cfg->fwcfg.num_ioim_reqs;
571 num_tms = bfa_cfg->fwcfg.num_tskim_reqs;
572 num_fcxps = bfa_cfg->fwcfg.num_fcxp_reqs;
Jing Huang7725ccf2009-09-23 17:46:15 -0700573 num_ufbufs = bfa_cfg->fwcfg.num_uf_bufs;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700574 reqq_size = bfa_cfg->drvcfg.num_reqq_elems;
575 rspq_size = bfa_cfg->drvcfg.num_rspq_elems;
576 num_sgpgs = bfa_cfg->drvcfg.num_sgpgs;
Jing Huang7725ccf2009-09-23 17:46:15 -0700577}
578
579bfa_status_t
580bfad_hal_mem_alloc(struct bfad_s *bfad)
581{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700582 int i;
Jing Huang7725ccf2009-09-23 17:46:15 -0700583 struct bfa_meminfo_s *hal_meminfo = &bfad->meminfo;
584 struct bfa_mem_elem_s *meminfo_elem;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700585 dma_addr_t phys_addr;
586 void *kva;
587 bfa_status_t rc = BFA_STATUS_OK;
588 int retry_count = 0;
589 int reset_value = 1;
590 int min_num_sgpgs = 512;
Jing Huang7725ccf2009-09-23 17:46:15 -0700591
592 bfa_cfg_get_default(&bfad->ioc_cfg);
593
594retry:
595 bfad_update_hal_cfg(&bfad->ioc_cfg);
596 bfad->cfg_data.ioc_queue_depth = bfad->ioc_cfg.fwcfg.num_ioim_reqs;
597 bfa_cfg_get_meminfo(&bfad->ioc_cfg, hal_meminfo);
598
599 for (i = 0; i < BFA_MEM_TYPE_MAX; i++) {
600 meminfo_elem = &hal_meminfo->meminfo[i];
601 switch (meminfo_elem->mem_type) {
602 case BFA_MEM_TYPE_KVA:
603 kva = vmalloc(meminfo_elem->mem_len);
604 if (kva == NULL) {
605 bfad_hal_mem_release(bfad);
606 rc = BFA_STATUS_ENOMEM;
607 goto ext;
608 }
609 memset(kva, 0, meminfo_elem->mem_len);
610 meminfo_elem->kva = kva;
611 break;
612 case BFA_MEM_TYPE_DMA:
613 kva = dma_alloc_coherent(&bfad->pcidev->dev,
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700614 meminfo_elem->mem_len, &phys_addr, GFP_KERNEL);
Jing Huang7725ccf2009-09-23 17:46:15 -0700615 if (kva == NULL) {
616 bfad_hal_mem_release(bfad);
617 /*
618 * If we cannot allocate with default
619 * num_sgpages try with half the value.
620 */
621 if (num_sgpgs > min_num_sgpgs) {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700622 printk(KERN_INFO
623 "bfad[%d]: memory allocation failed"
624 " with num_sgpgs: %d\n",
Jing Huang7725ccf2009-09-23 17:46:15 -0700625 bfad->inst_no, num_sgpgs);
626 nextLowerInt(&num_sgpgs);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700627 printk(KERN_INFO
628 "bfad[%d]: trying to allocate memory"
629 " with num_sgpgs: %d\n",
Jing Huang7725ccf2009-09-23 17:46:15 -0700630 bfad->inst_no, num_sgpgs);
631 retry_count++;
632 goto retry;
633 } else {
634 if (num_sgpgs_parm > 0)
635 num_sgpgs = num_sgpgs_parm;
636 else {
637 reset_value =
638 (1 << retry_count);
639 num_sgpgs *= reset_value;
640 }
641 rc = BFA_STATUS_ENOMEM;
642 goto ext;
643 }
644 }
645
646 if (num_sgpgs_parm > 0)
647 num_sgpgs = num_sgpgs_parm;
648 else {
649 reset_value = (1 << retry_count);
650 num_sgpgs *= reset_value;
651 }
652
653 memset(kva, 0, meminfo_elem->mem_len);
654 meminfo_elem->kva = kva;
655 meminfo_elem->dma = phys_addr;
656 break;
657 default:
658 break;
659
660 }
661 }
662ext:
663 return rc;
664}
665
666/**
667 * Create a vport under a vf.
668 */
669bfa_status_t
670bfad_vport_create(struct bfad_s *bfad, u16 vf_id,
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700671 struct bfa_lport_cfg_s *port_cfg, struct device *dev)
Jing Huang7725ccf2009-09-23 17:46:15 -0700672{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700673 struct bfad_vport_s *vport;
674 int rc = BFA_STATUS_OK;
675 unsigned long flags;
Jing Huang7725ccf2009-09-23 17:46:15 -0700676 struct completion fcomp;
677
678 vport = kzalloc(sizeof(struct bfad_vport_s), GFP_KERNEL);
679 if (!vport) {
680 rc = BFA_STATUS_ENOMEM;
681 goto ext;
682 }
683
684 vport->drv_port.bfad = bfad;
685 spin_lock_irqsave(&bfad->bfad_lock, flags);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700686 rc = bfa_fcs_vport_create(&vport->fcs_vport, &bfad->bfa_fcs, vf_id,
687 port_cfg, vport);
Jing Huang7725ccf2009-09-23 17:46:15 -0700688 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
689
690 if (rc != BFA_STATUS_OK)
691 goto ext_free_vport;
692
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700693 if (port_cfg->roles & BFA_LPORT_ROLE_FCP_IM) {
Jing Huangb5042932010-03-19 11:05:39 -0700694 rc = bfad_im_scsi_host_alloc(bfad, vport->drv_port.im_port,
695 dev);
Jing Huang7725ccf2009-09-23 17:46:15 -0700696 if (rc != BFA_STATUS_OK)
697 goto ext_free_fcs_vport;
698 }
699
700 spin_lock_irqsave(&bfad->bfad_lock, flags);
701 bfa_fcs_vport_start(&vport->fcs_vport);
702 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
703
704 return BFA_STATUS_OK;
705
706ext_free_fcs_vport:
707 spin_lock_irqsave(&bfad->bfad_lock, flags);
708 vport->comp_del = &fcomp;
709 init_completion(vport->comp_del);
710 bfa_fcs_vport_delete(&vport->fcs_vport);
711 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
712 wait_for_completion(vport->comp_del);
713ext_free_vport:
714 kfree(vport);
715ext:
716 return rc;
717}
718
Jing Huang7725ccf2009-09-23 17:46:15 -0700719void
720bfad_bfa_tmo(unsigned long data)
721{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700722 struct bfad_s *bfad = (struct bfad_s *) data;
723 unsigned long flags;
724 struct list_head doneq;
Jing Huang7725ccf2009-09-23 17:46:15 -0700725
726 spin_lock_irqsave(&bfad->bfad_lock, flags);
727
728 bfa_timer_tick(&bfad->bfa);
729
730 bfa_comp_deq(&bfad->bfa, &doneq);
731 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
732
733 if (!list_empty(&doneq)) {
734 bfa_comp_process(&bfad->bfa, &doneq);
735 spin_lock_irqsave(&bfad->bfad_lock, flags);
736 bfa_comp_free(&bfad->bfa, &doneq);
737 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
738 }
739
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700740 mod_timer(&bfad->hal_tmo,
741 jiffies + msecs_to_jiffies(BFA_TIMER_FREQ));
Jing Huang7725ccf2009-09-23 17:46:15 -0700742}
743
744void
745bfad_init_timer(struct bfad_s *bfad)
746{
747 init_timer(&bfad->hal_tmo);
748 bfad->hal_tmo.function = bfad_bfa_tmo;
749 bfad->hal_tmo.data = (unsigned long)bfad;
750
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700751 mod_timer(&bfad->hal_tmo,
752 jiffies + msecs_to_jiffies(BFA_TIMER_FREQ));
Jing Huang7725ccf2009-09-23 17:46:15 -0700753}
754
755int
756bfad_pci_init(struct pci_dev *pdev, struct bfad_s *bfad)
757{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700758 int rc = -ENODEV;
Jing Huang7725ccf2009-09-23 17:46:15 -0700759
760 if (pci_enable_device(pdev)) {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700761 printk(KERN_ERR "pci_enable_device fail %p\n", pdev);
Jing Huang7725ccf2009-09-23 17:46:15 -0700762 goto out;
763 }
764
765 if (pci_request_regions(pdev, BFAD_DRIVER_NAME))
766 goto out_disable_device;
767
768 pci_set_master(pdev);
769
770
771 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0)
772 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0) {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700773 printk(KERN_ERR "pci_set_dma_mask fail %p\n", pdev);
Jing Huang7725ccf2009-09-23 17:46:15 -0700774 goto out_release_region;
775 }
776
Jing Huangb3522f02010-03-19 11:06:44 -0700777 bfad->pci_bar0_kva = pci_iomap(pdev, 0, pci_resource_len(pdev, 0));
Jing Huang7725ccf2009-09-23 17:46:15 -0700778
779 if (bfad->pci_bar0_kva == NULL) {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700780 printk(KERN_ERR "Fail to map bar0\n");
Jing Huang7725ccf2009-09-23 17:46:15 -0700781 goto out_release_region;
782 }
783
784 bfad->hal_pcidev.pci_slot = PCI_SLOT(pdev->devfn);
785 bfad->hal_pcidev.pci_func = PCI_FUNC(pdev->devfn);
786 bfad->hal_pcidev.pci_bar_kva = bfad->pci_bar0_kva;
787 bfad->hal_pcidev.device_id = pdev->device;
788 bfad->pci_name = pci_name(pdev);
789
790 bfad->pci_attr.vendor_id = pdev->vendor;
791 bfad->pci_attr.device_id = pdev->device;
792 bfad->pci_attr.ssid = pdev->subsystem_device;
793 bfad->pci_attr.ssvid = pdev->subsystem_vendor;
794 bfad->pci_attr.pcifn = PCI_FUNC(pdev->devfn);
795
796 bfad->pcidev = pdev;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700797
798 /* Adjust PCIe Maximum Read Request Size */
799 if (pcie_max_read_reqsz > 0) {
800 int pcie_cap_reg;
801 u16 pcie_dev_ctl;
802 u16 mask = 0xffff;
803
804 switch (pcie_max_read_reqsz) {
805 case 128:
806 mask = 0x0;
807 break;
808 case 256:
809 mask = 0x1000;
810 break;
811 case 512:
812 mask = 0x2000;
813 break;
814 case 1024:
815 mask = 0x3000;
816 break;
817 case 2048:
818 mask = 0x4000;
819 break;
820 case 4096:
821 mask = 0x5000;
822 break;
823 default:
824 break;
825 }
826
827 pcie_cap_reg = pci_find_capability(pdev, PCI_CAP_ID_EXP);
828 if (mask != 0xffff && pcie_cap_reg) {
829 pcie_cap_reg += 0x08;
830 pci_read_config_word(pdev, pcie_cap_reg, &pcie_dev_ctl);
831 if ((pcie_dev_ctl & 0x7000) != mask) {
832 printk(KERN_WARNING "BFA[%s]: "
833 "pcie_max_read_request_size is %d, "
834 "reset to %d\n", bfad->pci_name,
835 (1 << ((pcie_dev_ctl & 0x7000) >> 12)) << 7,
836 pcie_max_read_reqsz);
837
838 pcie_dev_ctl &= ~0x7000;
839 pci_write_config_word(pdev, pcie_cap_reg,
840 pcie_dev_ctl | mask);
841 }
842 }
843 }
844
Jing Huang7725ccf2009-09-23 17:46:15 -0700845 return 0;
846
847out_release_region:
848 pci_release_regions(pdev);
849out_disable_device:
850 pci_disable_device(pdev);
851out:
852 return rc;
853}
854
855void
856bfad_pci_uninit(struct pci_dev *pdev, struct bfad_s *bfad)
857{
Jing Huang7725ccf2009-09-23 17:46:15 -0700858 pci_iounmap(pdev, bfad->pci_bar0_kva);
Jing Huang7725ccf2009-09-23 17:46:15 -0700859 pci_release_regions(pdev);
860 pci_disable_device(pdev);
861 pci_set_drvdata(pdev, NULL);
862}
863
Jing Huang7725ccf2009-09-23 17:46:15 -0700864bfa_status_t
865bfad_drv_init(struct bfad_s *bfad)
866{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700867 bfa_status_t rc;
868 unsigned long flags;
Jing Huang7725ccf2009-09-23 17:46:15 -0700869
870 bfad->cfg_data.rport_del_timeout = rport_del_timeout;
871 bfad->cfg_data.lun_queue_depth = bfa_lun_queue_depth;
872 bfad->cfg_data.io_max_sge = bfa_io_max_sge;
873 bfad->cfg_data.binding_method = FCP_PWWN_BINDING;
874
875 rc = bfad_hal_mem_alloc(bfad);
876 if (rc != BFA_STATUS_OK) {
877 printk(KERN_WARNING "bfad%d bfad_hal_mem_alloc failure\n",
878 bfad->inst_no);
879 printk(KERN_WARNING
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700880 "Not enough memory to attach all Brocade HBA ports, %s",
881 "System may need more memory.\n");
Jing Huang7725ccf2009-09-23 17:46:15 -0700882 goto out_hal_mem_alloc_failure;
883 }
884
Jing Huang7725ccf2009-09-23 17:46:15 -0700885 bfa_init_trc(&bfad->bfa, bfad->trcmod);
Jing Huang7725ccf2009-09-23 17:46:15 -0700886 bfa_init_plog(&bfad->bfa, &bfad->plog_buf);
887 bfa_plog_init(&bfad->plog_buf);
888 bfa_plog_str(&bfad->plog_buf, BFA_PL_MID_DRVR, BFA_PL_EID_DRIVER_START,
889 0, "Driver Attach");
890
891 bfa_attach(&bfad->bfa, bfad, &bfad->ioc_cfg, &bfad->meminfo,
892 &bfad->hal_pcidev);
893
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700894 /* FCS INIT */
Jing Huang7725ccf2009-09-23 17:46:15 -0700895 spin_lock_irqsave(&bfad->bfad_lock, flags);
Jing Huang7725ccf2009-09-23 17:46:15 -0700896 bfa_fcs_trc_init(&bfad->bfa_fcs, bfad->trcmod);
Krishna Gudipati82794a22010-03-03 17:43:30 -0800897 bfa_fcs_attach(&bfad->bfa_fcs, &bfad->bfa, bfad, BFA_FALSE);
Krishna Gudipati5b098082010-03-03 17:43:19 -0800898 bfa_fcs_set_fdmi_param(&bfad->bfa_fcs, fdmi_enable);
Jing Huang7725ccf2009-09-23 17:46:15 -0700899 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
900
901 bfad->bfad_flags |= BFAD_DRV_INIT_DONE;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700902
Jing Huang7725ccf2009-09-23 17:46:15 -0700903 return BFA_STATUS_OK;
904
Jing Huang7725ccf2009-09-23 17:46:15 -0700905out_hal_mem_alloc_failure:
906 return BFA_STATUS_FAILED;
907}
908
909void
910bfad_drv_uninit(struct bfad_s *bfad)
911{
Krishna Gudipatie6714322010-03-03 17:44:02 -0800912 unsigned long flags;
913
914 spin_lock_irqsave(&bfad->bfad_lock, flags);
915 init_completion(&bfad->comp);
916 bfa_stop(&bfad->bfa);
917 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
918 wait_for_completion(&bfad->comp);
919
Jing Huang7725ccf2009-09-23 17:46:15 -0700920 del_timer_sync(&bfad->hal_tmo);
921 bfa_isr_disable(&bfad->bfa);
922 bfa_detach(&bfad->bfa);
923 bfad_remove_intr(bfad);
Jing Huang7725ccf2009-09-23 17:46:15 -0700924 bfad_hal_mem_release(bfad);
Krishna Gudipatie6714322010-03-03 17:44:02 -0800925
926 bfad->bfad_flags &= ~BFAD_DRV_INIT_DONE;
Jing Huang7725ccf2009-09-23 17:46:15 -0700927}
928
929void
930bfad_drv_start(struct bfad_s *bfad)
931{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700932 unsigned long flags;
Jing Huang7725ccf2009-09-23 17:46:15 -0700933
934 spin_lock_irqsave(&bfad->bfad_lock, flags);
935 bfa_start(&bfad->bfa);
936 bfa_fcs_start(&bfad->bfa_fcs);
937 bfad->bfad_flags |= BFAD_HAL_START_DONE;
938 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
939
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700940 if (bfad->im)
941 flush_workqueue(bfad->im->drv_workq);
Jing Huang7725ccf2009-09-23 17:46:15 -0700942}
943
944void
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700945bfad_fcs_stop(struct bfad_s *bfad)
Jing Huang7725ccf2009-09-23 17:46:15 -0700946{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700947 unsigned long flags;
Jing Huang7725ccf2009-09-23 17:46:15 -0700948
949 spin_lock_irqsave(&bfad->bfad_lock, flags);
950 init_completion(&bfad->comp);
951 bfad->pport.flags |= BFAD_PORT_DELETE;
952 bfa_fcs_exit(&bfad->bfa_fcs);
953 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
954 wait_for_completion(&bfad->comp);
955
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700956 bfa_sm_send_event(bfad, BFAD_E_FCS_EXIT_COMP);
957}
958
959void
960bfad_stop(struct bfad_s *bfad)
961{
962 unsigned long flags;
963
Jing Huang7725ccf2009-09-23 17:46:15 -0700964 spin_lock_irqsave(&bfad->bfad_lock, flags);
965 init_completion(&bfad->comp);
966 bfa_stop(&bfad->bfa);
967 bfad->bfad_flags &= ~BFAD_HAL_START_DONE;
968 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
969 wait_for_completion(&bfad->comp);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700970
971 bfa_sm_send_event(bfad, BFAD_E_EXIT_COMP);
Jing Huang7725ccf2009-09-23 17:46:15 -0700972}
973
974bfa_status_t
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700975bfad_cfg_pport(struct bfad_s *bfad, enum bfa_lport_role role)
Jing Huang7725ccf2009-09-23 17:46:15 -0700976{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700977 int rc = BFA_STATUS_OK;
Jing Huang7725ccf2009-09-23 17:46:15 -0700978
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700979 /* Allocate scsi_host for the physical port */
980 if ((supported_fc4s & BFA_LPORT_ROLE_FCP_IM) &&
981 (role & BFA_LPORT_ROLE_FCP_IM)) {
Jing Huang7725ccf2009-09-23 17:46:15 -0700982 if (bfad->pport.im_port == NULL) {
983 rc = BFA_STATUS_FAILED;
984 goto out;
985 }
986
Jing Huangb5042932010-03-19 11:05:39 -0700987 rc = bfad_im_scsi_host_alloc(bfad, bfad->pport.im_port,
988 &bfad->pcidev->dev);
Jing Huang7725ccf2009-09-23 17:46:15 -0700989 if (rc != BFA_STATUS_OK)
990 goto out;
991
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700992 bfad->pport.roles |= BFA_LPORT_ROLE_FCP_IM;
Jing Huang7725ccf2009-09-23 17:46:15 -0700993 }
994
Jing Huangab2a9ba2010-07-08 20:02:55 -0700995 /* Setup the debugfs node for this scsi_host */
996 if (bfa_debugfs_enable)
997 bfad_debugfs_init(&bfad->pport);
998
Jing Huang7725ccf2009-09-23 17:46:15 -0700999 bfad->bfad_flags |= BFAD_CFG_PPORT_DONE;
1000
1001out:
1002 return rc;
1003}
1004
1005void
1006bfad_uncfg_pport(struct bfad_s *bfad)
1007{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001008 /* Remove the debugfs node for this scsi_host */
Jing Huangab2a9ba2010-07-08 20:02:55 -07001009 kfree(bfad->regdata);
1010 bfad_debugfs_exit(&bfad->pport);
1011
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001012 if ((supported_fc4s & BFA_LPORT_ROLE_FCP_IM) &&
1013 (bfad->pport.roles & BFA_LPORT_ROLE_FCP_IM)) {
Jing Huang7725ccf2009-09-23 17:46:15 -07001014 bfad_im_scsi_host_free(bfad, bfad->pport.im_port);
1015 bfad_im_port_clean(bfad->pport.im_port);
1016 kfree(bfad->pport.im_port);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001017 bfad->pport.roles &= ~BFA_LPORT_ROLE_FCP_IM;
Jing Huang7725ccf2009-09-23 17:46:15 -07001018 }
1019
1020 bfad->bfad_flags &= ~BFAD_CFG_PPORT_DONE;
1021}
1022
Krishna Gudipatie6714322010-03-03 17:44:02 -08001023bfa_status_t
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001024bfad_start_ops(struct bfad_s *bfad) {
1025
1026 int retval;
1027 unsigned long flags;
1028 struct bfad_vport_s *vport, *vport_new;
1029 struct bfa_fcs_driver_info_s driver_info;
1030
1031 /* Fill the driver_info info to fcs*/
1032 memset(&driver_info, 0, sizeof(driver_info));
1033 strncpy(driver_info.version, BFAD_DRIVER_VERSION,
1034 sizeof(driver_info.version) - 1);
1035 if (host_name)
1036 strncpy(driver_info.host_machine_name, host_name,
1037 sizeof(driver_info.host_machine_name) - 1);
1038 if (os_name)
1039 strncpy(driver_info.host_os_name, os_name,
1040 sizeof(driver_info.host_os_name) - 1);
1041 if (os_patch)
1042 strncpy(driver_info.host_os_patch, os_patch,
1043 sizeof(driver_info.host_os_patch) - 1);
1044
1045 strncpy(driver_info.os_device_name, bfad->pci_name,
1046 sizeof(driver_info.os_device_name - 1));
1047
1048 /* FCS INIT */
1049 spin_lock_irqsave(&bfad->bfad_lock, flags);
1050 bfa_fcs_driver_info_init(&bfad->bfa_fcs, &driver_info);
1051 bfa_fcs_init(&bfad->bfa_fcs);
1052 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
Krishna Gudipatie6714322010-03-03 17:44:02 -08001053
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001054 retval = bfad_cfg_pport(bfad, BFA_LPORT_ROLE_FCP_IM);
Krishna Gudipatie6714322010-03-03 17:44:02 -08001055 if (retval != BFA_STATUS_OK) {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001056 if (bfa_sm_cmp_state(bfad, bfad_sm_initializing))
1057 bfa_sm_set_state(bfad, bfad_sm_failed);
1058 bfad_stop(bfad);
1059 return BFA_STATUS_FAILED;
Krishna Gudipatie6714322010-03-03 17:44:02 -08001060 }
1061
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001062 /* BFAD level FC4 IM specific resource allocation */
1063 retval = bfad_im_probe(bfad);
1064 if (retval != BFA_STATUS_OK) {
1065 printk(KERN_WARNING "bfad_im_probe failed\n");
1066 if (bfa_sm_cmp_state(bfad, bfad_sm_initializing))
1067 bfa_sm_set_state(bfad, bfad_sm_failed);
1068 bfad_im_probe_undo(bfad);
1069 bfad->bfad_flags &= ~BFAD_FC4_PROBE_DONE;
1070 bfad_uncfg_pport(bfad);
1071 bfad_stop(bfad);
1072 return BFA_STATUS_FAILED;
1073 } else
1074 bfad->bfad_flags |= BFAD_FC4_PROBE_DONE;
1075
Krishna Gudipatie6714322010-03-03 17:44:02 -08001076 bfad_drv_start(bfad);
1077
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001078 /* Complete pbc vport create */
1079 list_for_each_entry_safe(vport, vport_new, &bfad->pbc_vport_list,
1080 list_entry) {
Jing Huangd9883542010-07-08 19:46:26 -07001081 struct fc_vport_identifiers vid;
1082 struct fc_vport *fc_vport;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001083 char pwwn_buf[BFA_STRING_32];
Jing Huangd9883542010-07-08 19:46:26 -07001084
1085 memset(&vid, 0, sizeof(vid));
1086 vid.roles = FC_PORT_ROLE_FCP_INITIATOR;
1087 vid.vport_type = FC_PORTTYPE_NPIV;
1088 vid.disable = false;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001089 vid.node_name = wwn_to_u64((u8 *)
1090 (&((vport->fcs_vport).lport.port_cfg.nwwn)));
1091 vid.port_name = wwn_to_u64((u8 *)
1092 (&((vport->fcs_vport).lport.port_cfg.pwwn)));
Jing Huangd9883542010-07-08 19:46:26 -07001093 fc_vport = fc_vport_create(bfad->pport.im_port->shost, 0, &vid);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001094 if (!fc_vport) {
1095 wwn2str(pwwn_buf, vid.port_name);
Jing Huangd9883542010-07-08 19:46:26 -07001096 printk(KERN_WARNING "bfad%d: failed to create pbc vport"
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001097 " %s\n", bfad->inst_no, pwwn_buf);
1098 }
1099 list_del(&vport->list_entry);
1100 kfree(vport);
Jing Huangd9883542010-07-08 19:46:26 -07001101 }
1102
Krishna Gudipatie6714322010-03-03 17:44:02 -08001103 /*
1104 * If bfa_linkup_delay is set to -1 default; try to retrive the
1105 * value using the bfad_os_get_linkup_delay(); else use the
1106 * passed in module param value as the bfa_linkup_delay.
1107 */
1108 if (bfa_linkup_delay < 0) {
Krishna Gudipatie6714322010-03-03 17:44:02 -08001109 bfa_linkup_delay = bfad_os_get_linkup_delay(bfad);
1110 bfad_os_rport_online_wait(bfad);
1111 bfa_linkup_delay = -1;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001112 } else
Krishna Gudipatie6714322010-03-03 17:44:02 -08001113 bfad_os_rport_online_wait(bfad);
Krishna Gudipatie6714322010-03-03 17:44:02 -08001114
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001115 BFA_LOG(KERN_INFO, bfad, log_level, "bfa device claimed\n");
Krishna Gudipatie6714322010-03-03 17:44:02 -08001116
1117 return BFA_STATUS_OK;
Krishna Gudipatie6714322010-03-03 17:44:02 -08001118}
1119
1120int
Jing Huangd9883542010-07-08 19:46:26 -07001121bfad_worker(void *ptr)
Krishna Gudipatie6714322010-03-03 17:44:02 -08001122{
1123 struct bfad_s *bfad;
1124 unsigned long flags;
1125
1126 bfad = (struct bfad_s *)ptr;
1127
1128 while (!kthread_should_stop()) {
1129
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001130 /* Send event BFAD_E_INIT_SUCCESS */
1131 bfa_sm_send_event(bfad, BFAD_E_INIT_SUCCESS);
Krishna Gudipatie6714322010-03-03 17:44:02 -08001132
1133 spin_lock_irqsave(&bfad->bfad_lock, flags);
1134 bfad->bfad_tsk = NULL;
1135 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1136
1137 break;
1138 }
1139
1140 return 0;
1141}
1142
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001143/**
1144 * BFA driver interrupt functions
1145 */
1146irqreturn_t
1147bfad_intx(int irq, void *dev_id)
1148{
1149 struct bfad_s *bfad = dev_id;
1150 struct list_head doneq;
1151 unsigned long flags;
1152 bfa_boolean_t rc;
1153
1154 spin_lock_irqsave(&bfad->bfad_lock, flags);
1155 rc = bfa_intx(&bfad->bfa);
1156 if (!rc) {
1157 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1158 return IRQ_NONE;
1159 }
1160
1161 bfa_comp_deq(&bfad->bfa, &doneq);
1162 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1163
1164 if (!list_empty(&doneq)) {
1165 bfa_comp_process(&bfad->bfa, &doneq);
1166
1167 spin_lock_irqsave(&bfad->bfad_lock, flags);
1168 bfa_comp_free(&bfad->bfa, &doneq);
1169 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1170 bfa_trc_fp(bfad, irq);
1171 }
1172
1173 return IRQ_HANDLED;
1174
1175}
1176
1177static irqreturn_t
1178bfad_msix(int irq, void *dev_id)
1179{
1180 struct bfad_msix_s *vec = dev_id;
1181 struct bfad_s *bfad = vec->bfad;
1182 struct list_head doneq;
1183 unsigned long flags;
1184
1185 spin_lock_irqsave(&bfad->bfad_lock, flags);
1186
1187 bfa_msix(&bfad->bfa, vec->msix.entry);
1188 bfa_comp_deq(&bfad->bfa, &doneq);
1189 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1190
1191 if (!list_empty(&doneq)) {
1192 bfa_comp_process(&bfad->bfa, &doneq);
1193
1194 spin_lock_irqsave(&bfad->bfad_lock, flags);
1195 bfa_comp_free(&bfad->bfa, &doneq);
1196 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1197 }
1198
1199 return IRQ_HANDLED;
1200}
1201
1202/**
1203 * Initialize the MSIX entry table.
1204 */
1205static void
1206bfad_init_msix_entry(struct bfad_s *bfad, struct msix_entry *msix_entries,
1207 int mask, int max_bit)
1208{
1209 int i;
1210 int match = 0x00000001;
1211
1212 for (i = 0, bfad->nvec = 0; i < MAX_MSIX_ENTRY; i++) {
1213 if (mask & match) {
1214 bfad->msix_tab[bfad->nvec].msix.entry = i;
1215 bfad->msix_tab[bfad->nvec].bfad = bfad;
1216 msix_entries[bfad->nvec].entry = i;
1217 bfad->nvec++;
1218 }
1219
1220 match <<= 1;
1221 }
1222
1223}
1224
1225int
1226bfad_install_msix_handler(struct bfad_s *bfad)
1227{
1228 int i, error = 0;
1229
1230 for (i = 0; i < bfad->nvec; i++) {
1231 sprintf(bfad->msix_tab[i].name, "bfa-%s-%s",
1232 bfad->pci_name,
1233 ((bfa_asic_id_ct(bfad->hal_pcidev.device_id)) ?
1234 msix_name_ct[i] : msix_name_cb[i]));
1235
1236 error = request_irq(bfad->msix_tab[i].msix.vector,
1237 (irq_handler_t) bfad_msix, 0,
1238 bfad->msix_tab[i].name, &bfad->msix_tab[i]);
1239 bfa_trc(bfad, i);
1240 bfa_trc(bfad, bfad->msix_tab[i].msix.vector);
1241 if (error) {
1242 int j;
1243
1244 for (j = 0; j < i; j++)
1245 free_irq(bfad->msix_tab[j].msix.vector,
1246 &bfad->msix_tab[j]);
1247
1248 return 1;
1249 }
1250 }
1251
1252 return 0;
1253}
1254
1255/**
1256 * Setup MSIX based interrupt.
1257 */
1258int
1259bfad_setup_intr(struct bfad_s *bfad)
1260{
1261 int error = 0;
1262 u32 mask = 0, i, num_bit = 0, max_bit = 0;
1263 struct msix_entry msix_entries[MAX_MSIX_ENTRY];
1264 struct pci_dev *pdev = bfad->pcidev;
1265
1266 /* Call BFA to get the msix map for this PCI function. */
1267 bfa_msix_getvecs(&bfad->bfa, &mask, &num_bit, &max_bit);
1268
1269 /* Set up the msix entry table */
1270 bfad_init_msix_entry(bfad, msix_entries, mask, max_bit);
1271
1272 if ((bfa_asic_id_ct(pdev->device) && !msix_disable_ct) ||
1273 (!bfa_asic_id_ct(pdev->device) && !msix_disable_cb)) {
1274
1275 error = pci_enable_msix(bfad->pcidev, msix_entries, bfad->nvec);
1276 if (error) {
1277 /*
1278 * Only error number of vector is available.
1279 * We don't have a mechanism to map multiple
1280 * interrupts into one vector, so even if we
1281 * can try to request less vectors, we don't
1282 * know how to associate interrupt events to
1283 * vectors. Linux doesn't dupicate vectors
1284 * in the MSIX table for this case.
1285 */
1286
1287 printk(KERN_WARNING "bfad%d: "
1288 "pci_enable_msix failed (%d),"
1289 " use line based.\n", bfad->inst_no, error);
1290
1291 goto line_based;
1292 }
1293
1294 /* Save the vectors */
1295 for (i = 0; i < bfad->nvec; i++) {
1296 bfa_trc(bfad, msix_entries[i].vector);
1297 bfad->msix_tab[i].msix.vector = msix_entries[i].vector;
1298 }
1299
1300 bfa_msix_init(&bfad->bfa, bfad->nvec);
1301
1302 bfad->bfad_flags |= BFAD_MSIX_ON;
1303
1304 return error;
1305 }
1306
1307line_based:
1308 error = 0;
1309 if (request_irq
1310 (bfad->pcidev->irq, (irq_handler_t) bfad_intx, BFAD_IRQ_FLAGS,
1311 BFAD_DRIVER_NAME, bfad) != 0) {
1312 /* Enable interrupt handler failed */
1313 return 1;
1314 }
1315
1316 return error;
1317}
1318
1319void
1320bfad_remove_intr(struct bfad_s *bfad)
1321{
1322 int i;
1323
1324 if (bfad->bfad_flags & BFAD_MSIX_ON) {
1325 for (i = 0; i < bfad->nvec; i++)
1326 free_irq(bfad->msix_tab[i].msix.vector,
1327 &bfad->msix_tab[i]);
1328
1329 pci_disable_msix(bfad->pcidev);
1330 bfad->bfad_flags &= ~BFAD_MSIX_ON;
1331 } else {
1332 free_irq(bfad->pcidev->irq, bfad);
1333 }
1334}
Jing Huang7725ccf2009-09-23 17:46:15 -07001335
1336/**
1337 * PCI probe entry.
1338 */
1339int
1340bfad_pci_probe(struct pci_dev *pdev, const struct pci_device_id *pid)
1341{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001342 struct bfad_s *bfad;
1343 int error = -ENODEV, retval;
Jing Huang7725ccf2009-09-23 17:46:15 -07001344
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001345 /* For single port cards - only claim function 0 */
1346 if ((pdev->device == BFA_PCI_DEVICE_ID_FC_8G1P) &&
1347 (PCI_FUNC(pdev->devfn) != 0))
Jing Huang7725ccf2009-09-23 17:46:15 -07001348 return -ENODEV;
1349
Jing Huang7725ccf2009-09-23 17:46:15 -07001350 bfad = kzalloc(sizeof(struct bfad_s), GFP_KERNEL);
1351 if (!bfad) {
1352 error = -ENOMEM;
1353 goto out;
1354 }
1355
1356 bfad->trcmod = kzalloc(sizeof(struct bfa_trc_mod_s), GFP_KERNEL);
1357 if (!bfad->trcmod) {
1358 printk(KERN_WARNING "Error alloc trace buffer!\n");
1359 error = -ENOMEM;
1360 goto out_alloc_trace_failure;
1361 }
1362
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001363 /* TRACE INIT */
Jing Huang7725ccf2009-09-23 17:46:15 -07001364 bfa_trc_init(bfad->trcmod);
1365 bfa_trc(bfad, bfad_inst);
1366
Jing Huang7725ccf2009-09-23 17:46:15 -07001367 if (!(bfad_load_fwimg(pdev))) {
Jing Huang7725ccf2009-09-23 17:46:15 -07001368 kfree(bfad->trcmod);
1369 goto out_alloc_trace_failure;
1370 }
1371
1372 retval = bfad_pci_init(pdev, bfad);
1373 if (retval) {
1374 printk(KERN_WARNING "bfad_pci_init failure!\n");
1375 error = retval;
1376 goto out_pci_init_failure;
1377 }
1378
1379 mutex_lock(&bfad_mutex);
1380 bfad->inst_no = bfad_inst++;
1381 list_add_tail(&bfad->list_entry, &bfad_list);
1382 mutex_unlock(&bfad_mutex);
1383
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001384 /* Initializing the state machine: State set to uninit */
1385 bfa_sm_set_state(bfad, bfad_sm_uninit);
1386
Jing Huang7725ccf2009-09-23 17:46:15 -07001387 spin_lock_init(&bfad->bfad_lock);
1388 pci_set_drvdata(pdev, bfad);
1389
1390 bfad->ref_count = 0;
1391 bfad->pport.bfad = bfad;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001392 INIT_LIST_HEAD(&bfad->pbc_vport_list);
Krishna Gudipatie6714322010-03-03 17:44:02 -08001393
Jing Huang7725ccf2009-09-23 17:46:15 -07001394 retval = bfad_drv_init(bfad);
1395 if (retval != BFA_STATUS_OK)
1396 goto out_drv_init_failure;
Jing Huang7725ccf2009-09-23 17:46:15 -07001397
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001398 bfa_sm_send_event(bfad, BFAD_E_CREATE);
Jing Huang7725ccf2009-09-23 17:46:15 -07001399
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001400 if (bfa_sm_cmp_state(bfad, bfad_sm_uninit))
1401 goto out_bfad_sm_failure;
Jing Huang7725ccf2009-09-23 17:46:15 -07001402
Jing Huang7725ccf2009-09-23 17:46:15 -07001403 return 0;
1404
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001405out_bfad_sm_failure:
1406 bfa_detach(&bfad->bfa);
1407 bfad_hal_mem_release(bfad);
Jing Huang7725ccf2009-09-23 17:46:15 -07001408out_drv_init_failure:
1409 mutex_lock(&bfad_mutex);
1410 bfad_inst--;
1411 list_del(&bfad->list_entry);
1412 mutex_unlock(&bfad_mutex);
1413 bfad_pci_uninit(pdev, bfad);
1414out_pci_init_failure:
1415 kfree(bfad->trcmod);
1416out_alloc_trace_failure:
1417 kfree(bfad);
1418out:
1419 return error;
1420}
1421
1422/**
1423 * PCI remove entry.
1424 */
1425void
1426bfad_pci_remove(struct pci_dev *pdev)
1427{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001428 struct bfad_s *bfad = pci_get_drvdata(pdev);
1429 unsigned long flags;
Jing Huang7725ccf2009-09-23 17:46:15 -07001430
1431 bfa_trc(bfad, bfad->inst_no);
1432
Krishna Gudipatie6714322010-03-03 17:44:02 -08001433 spin_lock_irqsave(&bfad->bfad_lock, flags);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001434 if (bfad->bfad_tsk != NULL) {
1435 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
Krishna Gudipatie6714322010-03-03 17:44:02 -08001436 kthread_stop(bfad->bfad_tsk);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001437 } else {
Jing Huang7725ccf2009-09-23 17:46:15 -07001438 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
Jing Huang7725ccf2009-09-23 17:46:15 -07001439 }
1440
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001441 /* Send Event BFAD_E_STOP */
1442 bfa_sm_send_event(bfad, BFAD_E_STOP);
Jing Huang7725ccf2009-09-23 17:46:15 -07001443
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001444 /* Driver detach and dealloc mem */
Jing Huang7725ccf2009-09-23 17:46:15 -07001445 spin_lock_irqsave(&bfad->bfad_lock, flags);
1446 bfa_detach(&bfad->bfa);
1447 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1448 bfad_hal_mem_release(bfad);
Jing Huang7725ccf2009-09-23 17:46:15 -07001449
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001450 /* Cleaning the BFAD instance */
Jing Huang7725ccf2009-09-23 17:46:15 -07001451 mutex_lock(&bfad_mutex);
1452 bfad_inst--;
1453 list_del(&bfad->list_entry);
1454 mutex_unlock(&bfad_mutex);
1455 bfad_pci_uninit(pdev, bfad);
1456
1457 kfree(bfad->trcmod);
1458 kfree(bfad);
1459}
1460
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001461struct pci_device_id bfad_id_table[] = {
Jing Huang7725ccf2009-09-23 17:46:15 -07001462 {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001463 .vendor = BFA_PCI_VENDOR_ID_BROCADE,
1464 .device = BFA_PCI_DEVICE_ID_FC_8G2P,
1465 .subvendor = PCI_ANY_ID,
1466 .subdevice = PCI_ANY_ID,
1467 },
Jing Huang7725ccf2009-09-23 17:46:15 -07001468 {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001469 .vendor = BFA_PCI_VENDOR_ID_BROCADE,
1470 .device = BFA_PCI_DEVICE_ID_FC_8G1P,
1471 .subvendor = PCI_ANY_ID,
1472 .subdevice = PCI_ANY_ID,
1473 },
Jing Huang7725ccf2009-09-23 17:46:15 -07001474 {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001475 .vendor = BFA_PCI_VENDOR_ID_BROCADE,
1476 .device = BFA_PCI_DEVICE_ID_CT,
1477 .subvendor = PCI_ANY_ID,
1478 .subdevice = PCI_ANY_ID,
1479 .class = (PCI_CLASS_SERIAL_FIBER << 8),
1480 .class_mask = ~0,
1481 },
Jing Huang293f82d2010-07-08 19:45:20 -07001482 {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001483 .vendor = BFA_PCI_VENDOR_ID_BROCADE,
1484 .device = BFA_PCI_DEVICE_ID_CT_FC,
1485 .subvendor = PCI_ANY_ID,
1486 .subdevice = PCI_ANY_ID,
1487 .class = (PCI_CLASS_SERIAL_FIBER << 8),
1488 .class_mask = ~0,
Jing Huang293f82d2010-07-08 19:45:20 -07001489 },
Jing Huang7725ccf2009-09-23 17:46:15 -07001490
1491 {0, 0},
1492};
1493
1494MODULE_DEVICE_TABLE(pci, bfad_id_table);
1495
1496static struct pci_driver bfad_pci_driver = {
1497 .name = BFAD_DRIVER_NAME,
1498 .id_table = bfad_id_table,
1499 .probe = bfad_pci_probe,
1500 .remove = __devexit_p(bfad_pci_remove),
1501};
1502
1503/**
Jing Huang7725ccf2009-09-23 17:46:15 -07001504 * Driver module init.
1505 */
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001506static int __init
Jing Huang7725ccf2009-09-23 17:46:15 -07001507bfad_init(void)
1508{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001509 int error = 0;
Jing Huang7725ccf2009-09-23 17:46:15 -07001510
1511 printk(KERN_INFO "Brocade BFA FC/FCOE SCSI driver - version: %s\n",
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001512 BFAD_DRIVER_VERSION);
Jing Huang7725ccf2009-09-23 17:46:15 -07001513
1514 if (num_sgpgs > 0)
1515 num_sgpgs_parm = num_sgpgs;
1516
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001517 error = bfad_im_module_init();
Jing Huang7725ccf2009-09-23 17:46:15 -07001518 if (error) {
1519 error = -ENOMEM;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001520 printk(KERN_WARNING "bfad_im_module_init failure\n");
Jing Huang7725ccf2009-09-23 17:46:15 -07001521 goto ext;
1522 }
1523
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001524 if (strcmp(FCPI_NAME, " fcpim") == 0)
1525 supported_fc4s |= BFA_LPORT_ROLE_FCP_IM;
Jing Huang7725ccf2009-09-23 17:46:15 -07001526
1527 bfa_ioc_auto_recover(ioc_auto_recover);
1528 bfa_fcs_rport_set_del_timeout(rport_del_timeout);
Jing Huang7725ccf2009-09-23 17:46:15 -07001529
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001530 error = pci_register_driver(&bfad_pci_driver);
Jing Huang7725ccf2009-09-23 17:46:15 -07001531 if (error) {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001532 printk(KERN_WARNING "pci_register_driver failure\n");
Jing Huang7725ccf2009-09-23 17:46:15 -07001533 goto ext;
1534 }
1535
1536 return 0;
1537
1538ext:
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001539 bfad_im_module_exit();
Jing Huang7725ccf2009-09-23 17:46:15 -07001540 return error;
1541}
1542
1543/**
1544 * Driver module exit.
1545 */
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001546static void __exit
Jing Huang7725ccf2009-09-23 17:46:15 -07001547bfad_exit(void)
1548{
1549 pci_unregister_driver(&bfad_pci_driver);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001550 bfad_im_module_exit();
Jing Huang7725ccf2009-09-23 17:46:15 -07001551 bfad_free_fwimg();
1552}
1553
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001554/* Firmware handling */
1555u32 *
1556bfad_read_firmware(struct pci_dev *pdev, u32 **bfi_image,
1557 u32 *bfi_image_size, char *fw_name)
1558{
1559 const struct firmware *fw;
1560
1561 if (request_firmware(&fw, fw_name, &pdev->dev)) {
1562 printk(KERN_ALERT "Can't locate firmware %s\n", fw_name);
1563 goto error;
1564 }
1565
1566 *bfi_image = vmalloc(fw->size);
1567 if (NULL == *bfi_image) {
1568 printk(KERN_ALERT "Fail to allocate buffer for fw image "
1569 "size=%x!\n", (u32) fw->size);
1570 goto error;
1571 }
1572
1573 memcpy(*bfi_image, fw->data, fw->size);
1574 *bfi_image_size = fw->size/sizeof(u32);
1575
1576 return *bfi_image;
1577
1578error:
1579 return NULL;
1580}
1581
1582u32 *
1583bfad_get_firmware_buf(struct pci_dev *pdev)
1584{
1585 if (pdev->device == BFA_PCI_DEVICE_ID_CT_FC) {
1586 if (bfi_image_ct_fc_size == 0)
1587 bfad_read_firmware(pdev, &bfi_image_ct_fc,
1588 &bfi_image_ct_fc_size, BFAD_FW_FILE_CT_FC);
1589 return bfi_image_ct_fc;
1590 } else if (pdev->device == BFA_PCI_DEVICE_ID_CT) {
1591 if (bfi_image_ct_cna_size == 0)
1592 bfad_read_firmware(pdev, &bfi_image_ct_cna,
1593 &bfi_image_ct_cna_size, BFAD_FW_FILE_CT_CNA);
1594 return bfi_image_ct_cna;
1595 } else {
1596 if (bfi_image_cb_fc_size == 0)
1597 bfad_read_firmware(pdev, &bfi_image_cb_fc,
1598 &bfi_image_cb_fc_size, BFAD_FW_FILE_CB_FC);
1599 return bfi_image_cb_fc;
1600 }
1601}
Jing Huang7725ccf2009-09-23 17:46:15 -07001602
1603module_init(bfad_init);
1604module_exit(bfad_exit);
1605MODULE_LICENSE("GPL");
1606MODULE_DESCRIPTION("Brocade Fibre Channel HBA Driver" BFAD_PROTO_NAME);
1607MODULE_AUTHOR("Brocade Communications Systems, Inc.");
1608MODULE_VERSION(BFAD_DRIVER_VERSION);