blob: d3966bcfa20591d1bf7dd4ca32ed781bc6eab592 [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
864void
865bfad_fcs_port_cfg(struct bfad_s *bfad)
866{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700867 struct bfa_lport_cfg_s port_cfg;
868 struct bfa_port_attr_s attr;
869 char symname[BFA_SYMNAME_MAXLEN];
Jing Huang7725ccf2009-09-23 17:46:15 -0700870
871 sprintf(symname, "%s-%d", BFAD_DRIVER_NAME, bfad->inst_no);
872 memcpy(port_cfg.sym_name.symname, symname, strlen(symname));
Krishna Gudipati1c8a4c32010-03-05 19:37:37 -0800873 bfa_fcport_get_attr(&bfad->bfa, &attr);
Jing Huang7725ccf2009-09-23 17:46:15 -0700874 port_cfg.nwwn = attr.nwwn;
875 port_cfg.pwwn = attr.pwwn;
Jing Huang7725ccf2009-09-23 17:46:15 -0700876}
877
878bfa_status_t
879bfad_drv_init(struct bfad_s *bfad)
880{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700881 bfa_status_t rc;
882 unsigned long flags;
Jing Huang7725ccf2009-09-23 17:46:15 -0700883
884 bfad->cfg_data.rport_del_timeout = rport_del_timeout;
885 bfad->cfg_data.lun_queue_depth = bfa_lun_queue_depth;
886 bfad->cfg_data.io_max_sge = bfa_io_max_sge;
887 bfad->cfg_data.binding_method = FCP_PWWN_BINDING;
888
889 rc = bfad_hal_mem_alloc(bfad);
890 if (rc != BFA_STATUS_OK) {
891 printk(KERN_WARNING "bfad%d bfad_hal_mem_alloc failure\n",
892 bfad->inst_no);
893 printk(KERN_WARNING
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700894 "Not enough memory to attach all Brocade HBA ports, %s",
895 "System may need more memory.\n");
Jing Huang7725ccf2009-09-23 17:46:15 -0700896 goto out_hal_mem_alloc_failure;
897 }
898
Jing Huang7725ccf2009-09-23 17:46:15 -0700899 bfa_init_trc(&bfad->bfa, bfad->trcmod);
Jing Huang7725ccf2009-09-23 17:46:15 -0700900 bfa_init_plog(&bfad->bfa, &bfad->plog_buf);
901 bfa_plog_init(&bfad->plog_buf);
902 bfa_plog_str(&bfad->plog_buf, BFA_PL_MID_DRVR, BFA_PL_EID_DRIVER_START,
903 0, "Driver Attach");
904
905 bfa_attach(&bfad->bfa, bfad, &bfad->ioc_cfg, &bfad->meminfo,
906 &bfad->hal_pcidev);
907
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700908 /* FCS INIT */
Jing Huang7725ccf2009-09-23 17:46:15 -0700909 spin_lock_irqsave(&bfad->bfad_lock, flags);
Jing Huang7725ccf2009-09-23 17:46:15 -0700910 bfa_fcs_trc_init(&bfad->bfa_fcs, bfad->trcmod);
Krishna Gudipati82794a22010-03-03 17:43:30 -0800911 bfa_fcs_attach(&bfad->bfa_fcs, &bfad->bfa, bfad, BFA_FALSE);
Krishna Gudipati5b098082010-03-03 17:43:19 -0800912 bfa_fcs_set_fdmi_param(&bfad->bfa_fcs, fdmi_enable);
Jing Huang7725ccf2009-09-23 17:46:15 -0700913 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
914
915 bfad->bfad_flags |= BFAD_DRV_INIT_DONE;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700916
Jing Huang7725ccf2009-09-23 17:46:15 -0700917 return BFA_STATUS_OK;
918
Jing Huang7725ccf2009-09-23 17:46:15 -0700919out_hal_mem_alloc_failure:
920 return BFA_STATUS_FAILED;
921}
922
923void
924bfad_drv_uninit(struct bfad_s *bfad)
925{
Krishna Gudipatie6714322010-03-03 17:44:02 -0800926 unsigned long flags;
927
928 spin_lock_irqsave(&bfad->bfad_lock, flags);
929 init_completion(&bfad->comp);
930 bfa_stop(&bfad->bfa);
931 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
932 wait_for_completion(&bfad->comp);
933
Jing Huang7725ccf2009-09-23 17:46:15 -0700934 del_timer_sync(&bfad->hal_tmo);
935 bfa_isr_disable(&bfad->bfa);
936 bfa_detach(&bfad->bfa);
937 bfad_remove_intr(bfad);
Jing Huang7725ccf2009-09-23 17:46:15 -0700938 bfad_hal_mem_release(bfad);
Krishna Gudipatie6714322010-03-03 17:44:02 -0800939
940 bfad->bfad_flags &= ~BFAD_DRV_INIT_DONE;
Jing Huang7725ccf2009-09-23 17:46:15 -0700941}
942
943void
944bfad_drv_start(struct bfad_s *bfad)
945{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700946 unsigned long flags;
Jing Huang7725ccf2009-09-23 17:46:15 -0700947
948 spin_lock_irqsave(&bfad->bfad_lock, flags);
949 bfa_start(&bfad->bfa);
950 bfa_fcs_start(&bfad->bfa_fcs);
951 bfad->bfad_flags |= BFAD_HAL_START_DONE;
952 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
953
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700954 if (bfad->im)
955 flush_workqueue(bfad->im->drv_workq);
Jing Huang7725ccf2009-09-23 17:46:15 -0700956}
957
958void
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700959bfad_fcs_stop(struct bfad_s *bfad)
Jing Huang7725ccf2009-09-23 17:46:15 -0700960{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700961 unsigned long flags;
Jing Huang7725ccf2009-09-23 17:46:15 -0700962
963 spin_lock_irqsave(&bfad->bfad_lock, flags);
964 init_completion(&bfad->comp);
965 bfad->pport.flags |= BFAD_PORT_DELETE;
966 bfa_fcs_exit(&bfad->bfa_fcs);
967 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
968 wait_for_completion(&bfad->comp);
969
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700970 bfa_sm_send_event(bfad, BFAD_E_FCS_EXIT_COMP);
971}
972
973void
974bfad_stop(struct bfad_s *bfad)
975{
976 unsigned long flags;
977
Jing Huang7725ccf2009-09-23 17:46:15 -0700978 spin_lock_irqsave(&bfad->bfad_lock, flags);
979 init_completion(&bfad->comp);
980 bfa_stop(&bfad->bfa);
981 bfad->bfad_flags &= ~BFAD_HAL_START_DONE;
982 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
983 wait_for_completion(&bfad->comp);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700984
985 bfa_sm_send_event(bfad, BFAD_E_EXIT_COMP);
Jing Huang7725ccf2009-09-23 17:46:15 -0700986}
987
988bfa_status_t
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700989bfad_cfg_pport(struct bfad_s *bfad, enum bfa_lport_role role)
Jing Huang7725ccf2009-09-23 17:46:15 -0700990{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700991 int rc = BFA_STATUS_OK;
Jing Huang7725ccf2009-09-23 17:46:15 -0700992
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700993 /* Allocate scsi_host for the physical port */
994 if ((supported_fc4s & BFA_LPORT_ROLE_FCP_IM) &&
995 (role & BFA_LPORT_ROLE_FCP_IM)) {
Jing Huang7725ccf2009-09-23 17:46:15 -0700996 if (bfad->pport.im_port == NULL) {
997 rc = BFA_STATUS_FAILED;
998 goto out;
999 }
1000
Jing Huangb5042932010-03-19 11:05:39 -07001001 rc = bfad_im_scsi_host_alloc(bfad, bfad->pport.im_port,
1002 &bfad->pcidev->dev);
Jing Huang7725ccf2009-09-23 17:46:15 -07001003 if (rc != BFA_STATUS_OK)
1004 goto out;
1005
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001006 bfad->pport.roles |= BFA_LPORT_ROLE_FCP_IM;
Jing Huang7725ccf2009-09-23 17:46:15 -07001007 }
1008
Jing Huangab2a9ba2010-07-08 20:02:55 -07001009 /* Setup the debugfs node for this scsi_host */
1010 if (bfa_debugfs_enable)
1011 bfad_debugfs_init(&bfad->pport);
1012
Jing Huang7725ccf2009-09-23 17:46:15 -07001013 bfad->bfad_flags |= BFAD_CFG_PPORT_DONE;
1014
1015out:
1016 return rc;
1017}
1018
1019void
1020bfad_uncfg_pport(struct bfad_s *bfad)
1021{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001022 /* Remove the debugfs node for this scsi_host */
Jing Huangab2a9ba2010-07-08 20:02:55 -07001023 kfree(bfad->regdata);
1024 bfad_debugfs_exit(&bfad->pport);
1025
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001026 if ((supported_fc4s & BFA_LPORT_ROLE_FCP_IM) &&
1027 (bfad->pport.roles & BFA_LPORT_ROLE_FCP_IM)) {
Jing Huang7725ccf2009-09-23 17:46:15 -07001028 bfad_im_scsi_host_free(bfad, bfad->pport.im_port);
1029 bfad_im_port_clean(bfad->pport.im_port);
1030 kfree(bfad->pport.im_port);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001031 bfad->pport.roles &= ~BFA_LPORT_ROLE_FCP_IM;
Jing Huang7725ccf2009-09-23 17:46:15 -07001032 }
1033
1034 bfad->bfad_flags &= ~BFAD_CFG_PPORT_DONE;
1035}
1036
Krishna Gudipatie6714322010-03-03 17:44:02 -08001037bfa_status_t
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001038bfad_start_ops(struct bfad_s *bfad) {
1039
1040 int retval;
1041 unsigned long flags;
1042 struct bfad_vport_s *vport, *vport_new;
1043 struct bfa_fcs_driver_info_s driver_info;
1044
1045 /* Fill the driver_info info to fcs*/
1046 memset(&driver_info, 0, sizeof(driver_info));
1047 strncpy(driver_info.version, BFAD_DRIVER_VERSION,
1048 sizeof(driver_info.version) - 1);
1049 if (host_name)
1050 strncpy(driver_info.host_machine_name, host_name,
1051 sizeof(driver_info.host_machine_name) - 1);
1052 if (os_name)
1053 strncpy(driver_info.host_os_name, os_name,
1054 sizeof(driver_info.host_os_name) - 1);
1055 if (os_patch)
1056 strncpy(driver_info.host_os_patch, os_patch,
1057 sizeof(driver_info.host_os_patch) - 1);
1058
1059 strncpy(driver_info.os_device_name, bfad->pci_name,
1060 sizeof(driver_info.os_device_name - 1));
1061
1062 /* FCS INIT */
1063 spin_lock_irqsave(&bfad->bfad_lock, flags);
1064 bfa_fcs_driver_info_init(&bfad->bfa_fcs, &driver_info);
1065 bfa_fcs_init(&bfad->bfa_fcs);
1066 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
Krishna Gudipatie6714322010-03-03 17:44:02 -08001067
1068 /* PPORT FCS config */
1069 bfad_fcs_port_cfg(bfad);
1070
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001071 retval = bfad_cfg_pport(bfad, BFA_LPORT_ROLE_FCP_IM);
Krishna Gudipatie6714322010-03-03 17:44:02 -08001072 if (retval != BFA_STATUS_OK) {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001073 if (bfa_sm_cmp_state(bfad, bfad_sm_initializing))
1074 bfa_sm_set_state(bfad, bfad_sm_failed);
1075 bfad_stop(bfad);
1076 return BFA_STATUS_FAILED;
Krishna Gudipatie6714322010-03-03 17:44:02 -08001077 }
1078
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001079 /* BFAD level FC4 IM specific resource allocation */
1080 retval = bfad_im_probe(bfad);
1081 if (retval != BFA_STATUS_OK) {
1082 printk(KERN_WARNING "bfad_im_probe failed\n");
1083 if (bfa_sm_cmp_state(bfad, bfad_sm_initializing))
1084 bfa_sm_set_state(bfad, bfad_sm_failed);
1085 bfad_im_probe_undo(bfad);
1086 bfad->bfad_flags &= ~BFAD_FC4_PROBE_DONE;
1087 bfad_uncfg_pport(bfad);
1088 bfad_stop(bfad);
1089 return BFA_STATUS_FAILED;
1090 } else
1091 bfad->bfad_flags |= BFAD_FC4_PROBE_DONE;
1092
Krishna Gudipatie6714322010-03-03 17:44:02 -08001093 bfad_drv_start(bfad);
1094
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001095 /* Complete pbc vport create */
1096 list_for_each_entry_safe(vport, vport_new, &bfad->pbc_vport_list,
1097 list_entry) {
Jing Huangd9883542010-07-08 19:46:26 -07001098 struct fc_vport_identifiers vid;
1099 struct fc_vport *fc_vport;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001100 char pwwn_buf[BFA_STRING_32];
Jing Huangd9883542010-07-08 19:46:26 -07001101
1102 memset(&vid, 0, sizeof(vid));
1103 vid.roles = FC_PORT_ROLE_FCP_INITIATOR;
1104 vid.vport_type = FC_PORTTYPE_NPIV;
1105 vid.disable = false;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001106 vid.node_name = wwn_to_u64((u8 *)
1107 (&((vport->fcs_vport).lport.port_cfg.nwwn)));
1108 vid.port_name = wwn_to_u64((u8 *)
1109 (&((vport->fcs_vport).lport.port_cfg.pwwn)));
Jing Huangd9883542010-07-08 19:46:26 -07001110 fc_vport = fc_vport_create(bfad->pport.im_port->shost, 0, &vid);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001111 if (!fc_vport) {
1112 wwn2str(pwwn_buf, vid.port_name);
Jing Huangd9883542010-07-08 19:46:26 -07001113 printk(KERN_WARNING "bfad%d: failed to create pbc vport"
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001114 " %s\n", bfad->inst_no, pwwn_buf);
1115 }
1116 list_del(&vport->list_entry);
1117 kfree(vport);
Jing Huangd9883542010-07-08 19:46:26 -07001118 }
1119
Krishna Gudipatie6714322010-03-03 17:44:02 -08001120 /*
1121 * If bfa_linkup_delay is set to -1 default; try to retrive the
1122 * value using the bfad_os_get_linkup_delay(); else use the
1123 * passed in module param value as the bfa_linkup_delay.
1124 */
1125 if (bfa_linkup_delay < 0) {
Krishna Gudipatie6714322010-03-03 17:44:02 -08001126 bfa_linkup_delay = bfad_os_get_linkup_delay(bfad);
1127 bfad_os_rport_online_wait(bfad);
1128 bfa_linkup_delay = -1;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001129 } else
Krishna Gudipatie6714322010-03-03 17:44:02 -08001130 bfad_os_rport_online_wait(bfad);
Krishna Gudipatie6714322010-03-03 17:44:02 -08001131
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001132 BFA_LOG(KERN_INFO, bfad, log_level, "bfa device claimed\n");
Krishna Gudipatie6714322010-03-03 17:44:02 -08001133
1134 return BFA_STATUS_OK;
Krishna Gudipatie6714322010-03-03 17:44:02 -08001135}
1136
1137int
Jing Huangd9883542010-07-08 19:46:26 -07001138bfad_worker(void *ptr)
Krishna Gudipatie6714322010-03-03 17:44:02 -08001139{
1140 struct bfad_s *bfad;
1141 unsigned long flags;
1142
1143 bfad = (struct bfad_s *)ptr;
1144
1145 while (!kthread_should_stop()) {
1146
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001147 /* Send event BFAD_E_INIT_SUCCESS */
1148 bfa_sm_send_event(bfad, BFAD_E_INIT_SUCCESS);
Krishna Gudipatie6714322010-03-03 17:44:02 -08001149
1150 spin_lock_irqsave(&bfad->bfad_lock, flags);
1151 bfad->bfad_tsk = NULL;
1152 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1153
1154 break;
1155 }
1156
1157 return 0;
1158}
1159
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001160/**
1161 * BFA driver interrupt functions
1162 */
1163irqreturn_t
1164bfad_intx(int irq, void *dev_id)
1165{
1166 struct bfad_s *bfad = dev_id;
1167 struct list_head doneq;
1168 unsigned long flags;
1169 bfa_boolean_t rc;
1170
1171 spin_lock_irqsave(&bfad->bfad_lock, flags);
1172 rc = bfa_intx(&bfad->bfa);
1173 if (!rc) {
1174 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1175 return IRQ_NONE;
1176 }
1177
1178 bfa_comp_deq(&bfad->bfa, &doneq);
1179 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1180
1181 if (!list_empty(&doneq)) {
1182 bfa_comp_process(&bfad->bfa, &doneq);
1183
1184 spin_lock_irqsave(&bfad->bfad_lock, flags);
1185 bfa_comp_free(&bfad->bfa, &doneq);
1186 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1187 bfa_trc_fp(bfad, irq);
1188 }
1189
1190 return IRQ_HANDLED;
1191
1192}
1193
1194static irqreturn_t
1195bfad_msix(int irq, void *dev_id)
1196{
1197 struct bfad_msix_s *vec = dev_id;
1198 struct bfad_s *bfad = vec->bfad;
1199 struct list_head doneq;
1200 unsigned long flags;
1201
1202 spin_lock_irqsave(&bfad->bfad_lock, flags);
1203
1204 bfa_msix(&bfad->bfa, vec->msix.entry);
1205 bfa_comp_deq(&bfad->bfa, &doneq);
1206 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1207
1208 if (!list_empty(&doneq)) {
1209 bfa_comp_process(&bfad->bfa, &doneq);
1210
1211 spin_lock_irqsave(&bfad->bfad_lock, flags);
1212 bfa_comp_free(&bfad->bfa, &doneq);
1213 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1214 }
1215
1216 return IRQ_HANDLED;
1217}
1218
1219/**
1220 * Initialize the MSIX entry table.
1221 */
1222static void
1223bfad_init_msix_entry(struct bfad_s *bfad, struct msix_entry *msix_entries,
1224 int mask, int max_bit)
1225{
1226 int i;
1227 int match = 0x00000001;
1228
1229 for (i = 0, bfad->nvec = 0; i < MAX_MSIX_ENTRY; i++) {
1230 if (mask & match) {
1231 bfad->msix_tab[bfad->nvec].msix.entry = i;
1232 bfad->msix_tab[bfad->nvec].bfad = bfad;
1233 msix_entries[bfad->nvec].entry = i;
1234 bfad->nvec++;
1235 }
1236
1237 match <<= 1;
1238 }
1239
1240}
1241
1242int
1243bfad_install_msix_handler(struct bfad_s *bfad)
1244{
1245 int i, error = 0;
1246
1247 for (i = 0; i < bfad->nvec; i++) {
1248 sprintf(bfad->msix_tab[i].name, "bfa-%s-%s",
1249 bfad->pci_name,
1250 ((bfa_asic_id_ct(bfad->hal_pcidev.device_id)) ?
1251 msix_name_ct[i] : msix_name_cb[i]));
1252
1253 error = request_irq(bfad->msix_tab[i].msix.vector,
1254 (irq_handler_t) bfad_msix, 0,
1255 bfad->msix_tab[i].name, &bfad->msix_tab[i]);
1256 bfa_trc(bfad, i);
1257 bfa_trc(bfad, bfad->msix_tab[i].msix.vector);
1258 if (error) {
1259 int j;
1260
1261 for (j = 0; j < i; j++)
1262 free_irq(bfad->msix_tab[j].msix.vector,
1263 &bfad->msix_tab[j]);
1264
1265 return 1;
1266 }
1267 }
1268
1269 return 0;
1270}
1271
1272/**
1273 * Setup MSIX based interrupt.
1274 */
1275int
1276bfad_setup_intr(struct bfad_s *bfad)
1277{
1278 int error = 0;
1279 u32 mask = 0, i, num_bit = 0, max_bit = 0;
1280 struct msix_entry msix_entries[MAX_MSIX_ENTRY];
1281 struct pci_dev *pdev = bfad->pcidev;
1282
1283 /* Call BFA to get the msix map for this PCI function. */
1284 bfa_msix_getvecs(&bfad->bfa, &mask, &num_bit, &max_bit);
1285
1286 /* Set up the msix entry table */
1287 bfad_init_msix_entry(bfad, msix_entries, mask, max_bit);
1288
1289 if ((bfa_asic_id_ct(pdev->device) && !msix_disable_ct) ||
1290 (!bfa_asic_id_ct(pdev->device) && !msix_disable_cb)) {
1291
1292 error = pci_enable_msix(bfad->pcidev, msix_entries, bfad->nvec);
1293 if (error) {
1294 /*
1295 * Only error number of vector is available.
1296 * We don't have a mechanism to map multiple
1297 * interrupts into one vector, so even if we
1298 * can try to request less vectors, we don't
1299 * know how to associate interrupt events to
1300 * vectors. Linux doesn't dupicate vectors
1301 * in the MSIX table for this case.
1302 */
1303
1304 printk(KERN_WARNING "bfad%d: "
1305 "pci_enable_msix failed (%d),"
1306 " use line based.\n", bfad->inst_no, error);
1307
1308 goto line_based;
1309 }
1310
1311 /* Save the vectors */
1312 for (i = 0; i < bfad->nvec; i++) {
1313 bfa_trc(bfad, msix_entries[i].vector);
1314 bfad->msix_tab[i].msix.vector = msix_entries[i].vector;
1315 }
1316
1317 bfa_msix_init(&bfad->bfa, bfad->nvec);
1318
1319 bfad->bfad_flags |= BFAD_MSIX_ON;
1320
1321 return error;
1322 }
1323
1324line_based:
1325 error = 0;
1326 if (request_irq
1327 (bfad->pcidev->irq, (irq_handler_t) bfad_intx, BFAD_IRQ_FLAGS,
1328 BFAD_DRIVER_NAME, bfad) != 0) {
1329 /* Enable interrupt handler failed */
1330 return 1;
1331 }
1332
1333 return error;
1334}
1335
1336void
1337bfad_remove_intr(struct bfad_s *bfad)
1338{
1339 int i;
1340
1341 if (bfad->bfad_flags & BFAD_MSIX_ON) {
1342 for (i = 0; i < bfad->nvec; i++)
1343 free_irq(bfad->msix_tab[i].msix.vector,
1344 &bfad->msix_tab[i]);
1345
1346 pci_disable_msix(bfad->pcidev);
1347 bfad->bfad_flags &= ~BFAD_MSIX_ON;
1348 } else {
1349 free_irq(bfad->pcidev->irq, bfad);
1350 }
1351}
Jing Huang7725ccf2009-09-23 17:46:15 -07001352
1353/**
1354 * PCI probe entry.
1355 */
1356int
1357bfad_pci_probe(struct pci_dev *pdev, const struct pci_device_id *pid)
1358{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001359 struct bfad_s *bfad;
1360 int error = -ENODEV, retval;
Jing Huang7725ccf2009-09-23 17:46:15 -07001361
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001362 /* For single port cards - only claim function 0 */
1363 if ((pdev->device == BFA_PCI_DEVICE_ID_FC_8G1P) &&
1364 (PCI_FUNC(pdev->devfn) != 0))
Jing Huang7725ccf2009-09-23 17:46:15 -07001365 return -ENODEV;
1366
Jing Huang7725ccf2009-09-23 17:46:15 -07001367 bfad = kzalloc(sizeof(struct bfad_s), GFP_KERNEL);
1368 if (!bfad) {
1369 error = -ENOMEM;
1370 goto out;
1371 }
1372
1373 bfad->trcmod = kzalloc(sizeof(struct bfa_trc_mod_s), GFP_KERNEL);
1374 if (!bfad->trcmod) {
1375 printk(KERN_WARNING "Error alloc trace buffer!\n");
1376 error = -ENOMEM;
1377 goto out_alloc_trace_failure;
1378 }
1379
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001380 /* TRACE INIT */
Jing Huang7725ccf2009-09-23 17:46:15 -07001381 bfa_trc_init(bfad->trcmod);
1382 bfa_trc(bfad, bfad_inst);
1383
Jing Huang7725ccf2009-09-23 17:46:15 -07001384 if (!(bfad_load_fwimg(pdev))) {
Jing Huang7725ccf2009-09-23 17:46:15 -07001385 kfree(bfad->trcmod);
1386 goto out_alloc_trace_failure;
1387 }
1388
1389 retval = bfad_pci_init(pdev, bfad);
1390 if (retval) {
1391 printk(KERN_WARNING "bfad_pci_init failure!\n");
1392 error = retval;
1393 goto out_pci_init_failure;
1394 }
1395
1396 mutex_lock(&bfad_mutex);
1397 bfad->inst_no = bfad_inst++;
1398 list_add_tail(&bfad->list_entry, &bfad_list);
1399 mutex_unlock(&bfad_mutex);
1400
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001401 /* Initializing the state machine: State set to uninit */
1402 bfa_sm_set_state(bfad, bfad_sm_uninit);
1403
Jing Huang7725ccf2009-09-23 17:46:15 -07001404 spin_lock_init(&bfad->bfad_lock);
1405 pci_set_drvdata(pdev, bfad);
1406
1407 bfad->ref_count = 0;
1408 bfad->pport.bfad = bfad;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001409 INIT_LIST_HEAD(&bfad->pbc_vport_list);
Krishna Gudipatie6714322010-03-03 17:44:02 -08001410
Jing Huang7725ccf2009-09-23 17:46:15 -07001411 retval = bfad_drv_init(bfad);
1412 if (retval != BFA_STATUS_OK)
1413 goto out_drv_init_failure;
Jing Huang7725ccf2009-09-23 17:46:15 -07001414
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001415 bfa_sm_send_event(bfad, BFAD_E_CREATE);
Jing Huang7725ccf2009-09-23 17:46:15 -07001416
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001417 if (bfa_sm_cmp_state(bfad, bfad_sm_uninit))
1418 goto out_bfad_sm_failure;
Jing Huang7725ccf2009-09-23 17:46:15 -07001419
Jing Huang7725ccf2009-09-23 17:46:15 -07001420 return 0;
1421
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001422out_bfad_sm_failure:
1423 bfa_detach(&bfad->bfa);
1424 bfad_hal_mem_release(bfad);
Jing Huang7725ccf2009-09-23 17:46:15 -07001425out_drv_init_failure:
1426 mutex_lock(&bfad_mutex);
1427 bfad_inst--;
1428 list_del(&bfad->list_entry);
1429 mutex_unlock(&bfad_mutex);
1430 bfad_pci_uninit(pdev, bfad);
1431out_pci_init_failure:
1432 kfree(bfad->trcmod);
1433out_alloc_trace_failure:
1434 kfree(bfad);
1435out:
1436 return error;
1437}
1438
1439/**
1440 * PCI remove entry.
1441 */
1442void
1443bfad_pci_remove(struct pci_dev *pdev)
1444{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001445 struct bfad_s *bfad = pci_get_drvdata(pdev);
1446 unsigned long flags;
Jing Huang7725ccf2009-09-23 17:46:15 -07001447
1448 bfa_trc(bfad, bfad->inst_no);
1449
Krishna Gudipatie6714322010-03-03 17:44:02 -08001450 spin_lock_irqsave(&bfad->bfad_lock, flags);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001451 if (bfad->bfad_tsk != NULL) {
1452 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
Krishna Gudipatie6714322010-03-03 17:44:02 -08001453 kthread_stop(bfad->bfad_tsk);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001454 } else {
Jing Huang7725ccf2009-09-23 17:46:15 -07001455 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
Jing Huang7725ccf2009-09-23 17:46:15 -07001456 }
1457
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001458 /* Send Event BFAD_E_STOP */
1459 bfa_sm_send_event(bfad, BFAD_E_STOP);
Jing Huang7725ccf2009-09-23 17:46:15 -07001460
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001461 /* Driver detach and dealloc mem */
Jing Huang7725ccf2009-09-23 17:46:15 -07001462 spin_lock_irqsave(&bfad->bfad_lock, flags);
1463 bfa_detach(&bfad->bfa);
1464 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1465 bfad_hal_mem_release(bfad);
Jing Huang7725ccf2009-09-23 17:46:15 -07001466
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001467 /* Cleaning the BFAD instance */
Jing Huang7725ccf2009-09-23 17:46:15 -07001468 mutex_lock(&bfad_mutex);
1469 bfad_inst--;
1470 list_del(&bfad->list_entry);
1471 mutex_unlock(&bfad_mutex);
1472 bfad_pci_uninit(pdev, bfad);
1473
1474 kfree(bfad->trcmod);
1475 kfree(bfad);
1476}
1477
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001478struct pci_device_id bfad_id_table[] = {
Jing Huang7725ccf2009-09-23 17:46:15 -07001479 {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001480 .vendor = BFA_PCI_VENDOR_ID_BROCADE,
1481 .device = BFA_PCI_DEVICE_ID_FC_8G2P,
1482 .subvendor = PCI_ANY_ID,
1483 .subdevice = PCI_ANY_ID,
1484 },
Jing Huang7725ccf2009-09-23 17:46:15 -07001485 {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001486 .vendor = BFA_PCI_VENDOR_ID_BROCADE,
1487 .device = BFA_PCI_DEVICE_ID_FC_8G1P,
1488 .subvendor = PCI_ANY_ID,
1489 .subdevice = PCI_ANY_ID,
1490 },
Jing Huang7725ccf2009-09-23 17:46:15 -07001491 {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001492 .vendor = BFA_PCI_VENDOR_ID_BROCADE,
1493 .device = BFA_PCI_DEVICE_ID_CT,
1494 .subvendor = PCI_ANY_ID,
1495 .subdevice = PCI_ANY_ID,
1496 .class = (PCI_CLASS_SERIAL_FIBER << 8),
1497 .class_mask = ~0,
1498 },
Jing Huang293f82d2010-07-08 19:45:20 -07001499 {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001500 .vendor = BFA_PCI_VENDOR_ID_BROCADE,
1501 .device = BFA_PCI_DEVICE_ID_CT_FC,
1502 .subvendor = PCI_ANY_ID,
1503 .subdevice = PCI_ANY_ID,
1504 .class = (PCI_CLASS_SERIAL_FIBER << 8),
1505 .class_mask = ~0,
Jing Huang293f82d2010-07-08 19:45:20 -07001506 },
Jing Huang7725ccf2009-09-23 17:46:15 -07001507
1508 {0, 0},
1509};
1510
1511MODULE_DEVICE_TABLE(pci, bfad_id_table);
1512
1513static struct pci_driver bfad_pci_driver = {
1514 .name = BFAD_DRIVER_NAME,
1515 .id_table = bfad_id_table,
1516 .probe = bfad_pci_probe,
1517 .remove = __devexit_p(bfad_pci_remove),
1518};
1519
1520/**
Jing Huang7725ccf2009-09-23 17:46:15 -07001521 * Driver module init.
1522 */
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001523static int __init
Jing Huang7725ccf2009-09-23 17:46:15 -07001524bfad_init(void)
1525{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001526 int error = 0;
Jing Huang7725ccf2009-09-23 17:46:15 -07001527
1528 printk(KERN_INFO "Brocade BFA FC/FCOE SCSI driver - version: %s\n",
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001529 BFAD_DRIVER_VERSION);
Jing Huang7725ccf2009-09-23 17:46:15 -07001530
1531 if (num_sgpgs > 0)
1532 num_sgpgs_parm = num_sgpgs;
1533
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001534 error = bfad_im_module_init();
Jing Huang7725ccf2009-09-23 17:46:15 -07001535 if (error) {
1536 error = -ENOMEM;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001537 printk(KERN_WARNING "bfad_im_module_init failure\n");
Jing Huang7725ccf2009-09-23 17:46:15 -07001538 goto ext;
1539 }
1540
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001541 if (strcmp(FCPI_NAME, " fcpim") == 0)
1542 supported_fc4s |= BFA_LPORT_ROLE_FCP_IM;
Jing Huang7725ccf2009-09-23 17:46:15 -07001543
1544 bfa_ioc_auto_recover(ioc_auto_recover);
1545 bfa_fcs_rport_set_del_timeout(rport_del_timeout);
Jing Huang7725ccf2009-09-23 17:46:15 -07001546
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001547 error = pci_register_driver(&bfad_pci_driver);
Jing Huang7725ccf2009-09-23 17:46:15 -07001548 if (error) {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001549 printk(KERN_WARNING "pci_register_driver failure\n");
Jing Huang7725ccf2009-09-23 17:46:15 -07001550 goto ext;
1551 }
1552
1553 return 0;
1554
1555ext:
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001556 bfad_im_module_exit();
Jing Huang7725ccf2009-09-23 17:46:15 -07001557 return error;
1558}
1559
1560/**
1561 * Driver module exit.
1562 */
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001563static void __exit
Jing Huang7725ccf2009-09-23 17:46:15 -07001564bfad_exit(void)
1565{
1566 pci_unregister_driver(&bfad_pci_driver);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001567 bfad_im_module_exit();
Jing Huang7725ccf2009-09-23 17:46:15 -07001568 bfad_free_fwimg();
1569}
1570
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001571/* Firmware handling */
1572u32 *
1573bfad_read_firmware(struct pci_dev *pdev, u32 **bfi_image,
1574 u32 *bfi_image_size, char *fw_name)
1575{
1576 const struct firmware *fw;
1577
1578 if (request_firmware(&fw, fw_name, &pdev->dev)) {
1579 printk(KERN_ALERT "Can't locate firmware %s\n", fw_name);
1580 goto error;
1581 }
1582
1583 *bfi_image = vmalloc(fw->size);
1584 if (NULL == *bfi_image) {
1585 printk(KERN_ALERT "Fail to allocate buffer for fw image "
1586 "size=%x!\n", (u32) fw->size);
1587 goto error;
1588 }
1589
1590 memcpy(*bfi_image, fw->data, fw->size);
1591 *bfi_image_size = fw->size/sizeof(u32);
1592
1593 return *bfi_image;
1594
1595error:
1596 return NULL;
1597}
1598
1599u32 *
1600bfad_get_firmware_buf(struct pci_dev *pdev)
1601{
1602 if (pdev->device == BFA_PCI_DEVICE_ID_CT_FC) {
1603 if (bfi_image_ct_fc_size == 0)
1604 bfad_read_firmware(pdev, &bfi_image_ct_fc,
1605 &bfi_image_ct_fc_size, BFAD_FW_FILE_CT_FC);
1606 return bfi_image_ct_fc;
1607 } else if (pdev->device == BFA_PCI_DEVICE_ID_CT) {
1608 if (bfi_image_ct_cna_size == 0)
1609 bfad_read_firmware(pdev, &bfi_image_ct_cna,
1610 &bfi_image_ct_cna_size, BFAD_FW_FILE_CT_CNA);
1611 return bfi_image_ct_cna;
1612 } else {
1613 if (bfi_image_cb_fc_size == 0)
1614 bfad_read_firmware(pdev, &bfi_image_cb_fc,
1615 &bfi_image_cb_fc_size, BFAD_FW_FILE_CB_FC);
1616 return bfi_image_cb_fc;
1617 }
1618}
Jing Huang7725ccf2009-09-23 17:46:15 -07001619
1620module_init(bfad_init);
1621module_exit(bfad_exit);
1622MODULE_LICENSE("GPL");
1623MODULE_DESCRIPTION("Brocade Fibre Channel HBA Driver" BFAD_PROTO_NAME);
1624MODULE_AUTHOR("Brocade Communications Systems, Inc.");
1625MODULE_VERSION(BFAD_DRIVER_VERSION);