blob: 59b5e9b61d71dc3385959a2dab5117af981b74e1 [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
Jing Huang5fbe25c2010-10-18 17:17:23 -070018/*
Jing Huang7725ccf2009-09-23 17:46:15 -070019 * 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"
Krishna Gudipatia36c61f2010-09-15 11:50:55 -070035#include "bfa_defs.h"
36#include "bfa.h"
Jing Huang7725ccf2009-09-23 17:46:15 -070037
38BFA_TRC_FILE(LDRV, BFAD);
Jing Huang42b426e2010-03-19 11:07:09 -070039DEFINE_MUTEX(bfad_mutex);
Jing Huang7725ccf2009-09-23 17:46:15 -070040LIST_HEAD(bfad_list);
Jing Huang7725ccf2009-09-23 17:46:15 -070041
Krishna Gudipatia36c61f2010-09-15 11:50:55 -070042static int bfad_inst;
43static int num_sgpgs_parm;
44int supported_fc4s;
45char *host_name, *os_name, *os_patch;
46int num_rports, num_ios, num_tms;
47int num_fcxps, num_ufbufs;
48int reqq_size, rspq_size, num_sgpgs;
49int rport_del_timeout = BFA_FCS_RPORT_DEF_DEL_TIMEOUT;
50int bfa_lun_queue_depth = BFAD_LUN_QUEUE_DEPTH;
51int bfa_io_max_sge = BFAD_IO_MAX_SGE;
Jing Huang88166242010-12-09 17:11:53 -080052int bfa_log_level = 3; /* WARNING log level */
Krishna Gudipatia36c61f2010-09-15 11:50:55 -070053int ioc_auto_recover = BFA_TRUE;
54int bfa_linkup_delay = -1;
55int fdmi_enable = BFA_TRUE;
56int pcie_max_read_reqsz;
Jing Huangab2a9ba2010-07-08 20:02:55 -070057int bfa_debugfs_enable = 1;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -070058int msix_disable_cb = 0, msix_disable_ct = 0;
59
Jing Huang61338a02011-04-13 11:44:03 -070060/* Firmware releated */
Krishna Gudipatia36c61f2010-09-15 11:50:55 -070061u32 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
Jing Huang61338a02011-04-13 11:44:03 -070064#define BFAD_FW_FILE_CT_FC "ctfw_fc.bin"
65#define BFAD_FW_FILE_CT_CNA "ctfw_cna.bin"
66#define BFAD_FW_FILE_CB_FC "cbfw_fc.bin"
67
68static u32 *bfad_load_fwimg(struct pci_dev *pdev);
69static void bfad_free_fwimg(void);
70static void bfad_read_firmware(struct pci_dev *pdev, u32 **bfi_image,
71 u32 *bfi_image_size, char *fw_name);
72
Maggie52f94b62010-11-29 18:21:32 -080073static const char *msix_name_ct[] = {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -070074 "cpe0", "cpe1", "cpe2", "cpe3",
75 "rme0", "rme1", "rme2", "rme3",
76 "ctrl" };
77
Maggie52f94b62010-11-29 18:21:32 -080078static const char *msix_name_cb[] = {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -070079 "cpe0", "cpe1", "cpe2", "cpe3",
80 "rme0", "rme1", "rme2", "rme3",
81 "eemc", "elpu0", "elpu1", "epss", "mlpu" };
82
83MODULE_FIRMWARE(BFAD_FW_FILE_CT_FC);
84MODULE_FIRMWARE(BFAD_FW_FILE_CT_CNA);
85MODULE_FIRMWARE(BFAD_FW_FILE_CB_FC);
Jing Huang7725ccf2009-09-23 17:46:15 -070086
87module_param(os_name, charp, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -070088MODULE_PARM_DESC(os_name, "OS name of the hba host machine");
Jing Huang7725ccf2009-09-23 17:46:15 -070089module_param(os_patch, charp, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -070090MODULE_PARM_DESC(os_patch, "OS patch level of the hba host machine");
Jing Huang7725ccf2009-09-23 17:46:15 -070091module_param(host_name, charp, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -070092MODULE_PARM_DESC(host_name, "Hostname of the hba host machine");
Jing Huang7725ccf2009-09-23 17:46:15 -070093module_param(num_rports, int, S_IRUGO | S_IWUSR);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -070094MODULE_PARM_DESC(num_rports, "Max number of rports supported per port "
95 "(physical/logical), default=1024");
Jing Huang7725ccf2009-09-23 17:46:15 -070096module_param(num_ios, int, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -070097MODULE_PARM_DESC(num_ios, "Max number of ioim requests, default=2000");
Jing Huang7725ccf2009-09-23 17:46:15 -070098module_param(num_tms, int, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -070099MODULE_PARM_DESC(num_tms, "Max number of task im requests, default=128");
Jing Huang7725ccf2009-09-23 17:46:15 -0700100module_param(num_fcxps, int, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -0700101MODULE_PARM_DESC(num_fcxps, "Max number of fcxp requests, default=64");
Jing Huang7725ccf2009-09-23 17:46:15 -0700102module_param(num_ufbufs, int, S_IRUGO | S_IWUSR);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700103MODULE_PARM_DESC(num_ufbufs, "Max number of unsolicited frame "
104 "buffers, default=64");
Jing Huang7725ccf2009-09-23 17:46:15 -0700105module_param(reqq_size, int, S_IRUGO | S_IWUSR);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700106MODULE_PARM_DESC(reqq_size, "Max number of request queue elements, "
107 "default=256");
Jing Huang7725ccf2009-09-23 17:46:15 -0700108module_param(rspq_size, int, S_IRUGO | S_IWUSR);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700109MODULE_PARM_DESC(rspq_size, "Max number of response queue elements, "
110 "default=64");
Jing Huang7725ccf2009-09-23 17:46:15 -0700111module_param(num_sgpgs, int, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -0700112MODULE_PARM_DESC(num_sgpgs, "Number of scatter/gather pages, default=2048");
Jing Huang7725ccf2009-09-23 17:46:15 -0700113module_param(rport_del_timeout, int, S_IRUGO | S_IWUSR);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700114MODULE_PARM_DESC(rport_del_timeout, "Rport delete timeout, default=90 secs, "
115 "Range[>0]");
Jing Huang7725ccf2009-09-23 17:46:15 -0700116module_param(bfa_lun_queue_depth, int, S_IRUGO | S_IWUSR);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700117MODULE_PARM_DESC(bfa_lun_queue_depth, "Lun queue depth, default=32, Range[>0]");
Jing Huang7725ccf2009-09-23 17:46:15 -0700118module_param(bfa_io_max_sge, int, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -0700119MODULE_PARM_DESC(bfa_io_max_sge, "Max io scatter/gather elements, default=255");
Jing Huang88166242010-12-09 17:11:53 -0800120module_param(bfa_log_level, int, S_IRUGO | S_IWUSR);
121MODULE_PARM_DESC(bfa_log_level, "Driver log level, default=3, "
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700122 "Range[Critical:1|Error:2|Warning:3|Info:4]");
Jing Huang7725ccf2009-09-23 17:46:15 -0700123module_param(ioc_auto_recover, int, S_IRUGO | S_IWUSR);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700124MODULE_PARM_DESC(ioc_auto_recover, "IOC auto recovery, default=1, "
125 "Range[off:0|on:1]");
Jing Huang7725ccf2009-09-23 17:46:15 -0700126module_param(bfa_linkup_delay, int, S_IRUGO | S_IWUSR);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700127MODULE_PARM_DESC(bfa_linkup_delay, "Link up delay, default=30 secs for "
128 "boot port. Otherwise 10 secs in RHEL4 & 0 for "
129 "[RHEL5, SLES10, ESX40] Range[>0]");
130module_param(msix_disable_cb, int, S_IRUGO | S_IWUSR);
131MODULE_PARM_DESC(msix_disable_cb, "Disable Message Signaled Interrupts "
132 "for Brocade-415/425/815/825 cards, default=0, "
133 " Range[false:0|true:1]");
134module_param(msix_disable_ct, int, S_IRUGO | S_IWUSR);
135MODULE_PARM_DESC(msix_disable_ct, "Disable Message Signaled Interrupts "
136 "if possible for Brocade-1010/1020/804/1007/902/1741 "
137 "cards, default=0, Range[false:0|true:1]");
Jing Huang604158ad2010-07-08 19:59:49 -0700138module_param(fdmi_enable, int, S_IRUGO | S_IWUSR);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700139MODULE_PARM_DESC(fdmi_enable, "Enables fdmi registration, default=1, "
140 "Range[false:0|true:1]");
141module_param(pcie_max_read_reqsz, int, S_IRUGO | S_IWUSR);
142MODULE_PARM_DESC(pcie_max_read_reqsz, "PCIe max read request size, default=0 "
143 "(use system setting), Range[128|256|512|1024|2048|4096]");
Jing Huangab2a9ba2010-07-08 20:02:55 -0700144module_param(bfa_debugfs_enable, int, S_IRUGO | S_IWUSR);
145MODULE_PARM_DESC(bfa_debugfs_enable, "Enables debugfs feature, default=1,"
146 " Range[false:0|true:1]");
Jing Huang7725ccf2009-09-23 17:46:15 -0700147
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700148static void
149bfad_sm_uninit(struct bfad_s *bfad, enum bfad_sm_event event);
150static void
151bfad_sm_created(struct bfad_s *bfad, enum bfad_sm_event event);
152static void
153bfad_sm_initializing(struct bfad_s *bfad, enum bfad_sm_event event);
154static void
155bfad_sm_operational(struct bfad_s *bfad, enum bfad_sm_event event);
156static void
157bfad_sm_stopping(struct bfad_s *bfad, enum bfad_sm_event event);
158static void
159bfad_sm_failed(struct bfad_s *bfad, enum bfad_sm_event event);
160static void
161bfad_sm_fcs_exit(struct bfad_s *bfad, enum bfad_sm_event event);
162
Jing Huang5fbe25c2010-10-18 17:17:23 -0700163/*
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700164 * Beginning state for the driver instance, awaiting the pci_probe event
Jing Huang7725ccf2009-09-23 17:46:15 -0700165 */
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700166static void
167bfad_sm_uninit(struct bfad_s *bfad, enum bfad_sm_event event)
Jing Huang7725ccf2009-09-23 17:46:15 -0700168{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700169 bfa_trc(bfad, event);
Jing Huang7725ccf2009-09-23 17:46:15 -0700170
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700171 switch (event) {
172 case BFAD_E_CREATE:
173 bfa_sm_set_state(bfad, bfad_sm_created);
174 bfad->bfad_tsk = kthread_create(bfad_worker, (void *) bfad,
175 "%s", "bfad_worker");
176 if (IS_ERR(bfad->bfad_tsk)) {
177 printk(KERN_INFO "bfad[%d]: Kernel thread "
178 "creation failed!\n", bfad->inst_no);
179 bfa_sm_send_event(bfad, BFAD_E_KTHREAD_CREATE_FAILED);
180 }
181 bfa_sm_send_event(bfad, BFAD_E_INIT);
182 break;
Jing Huang7725ccf2009-09-23 17:46:15 -0700183
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700184 case BFAD_E_STOP:
185 /* Ignore stop; already in uninit */
186 break;
Jing Huang7725ccf2009-09-23 17:46:15 -0700187
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700188 default:
189 bfa_sm_fault(bfad, event);
190 }
191}
Krishna Gudipatie6714322010-03-03 17:44:02 -0800192
Jing Huang5fbe25c2010-10-18 17:17:23 -0700193/*
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700194 * Driver Instance is created, awaiting event INIT to initialize the bfad
195 */
196static void
197bfad_sm_created(struct bfad_s *bfad, enum bfad_sm_event event)
198{
199 unsigned long flags;
200
201 bfa_trc(bfad, event);
202
203 switch (event) {
204 case BFAD_E_INIT:
205 bfa_sm_set_state(bfad, bfad_sm_initializing);
206
207 init_completion(&bfad->comp);
208
209 /* Enable Interrupt and wait bfa_init completion */
210 if (bfad_setup_intr(bfad)) {
211 printk(KERN_WARNING "bfad%d: bfad_setup_intr failed\n",
212 bfad->inst_no);
213 bfa_sm_send_event(bfad, BFAD_E_INTR_INIT_FAILED);
214 break;
215 }
216
217 spin_lock_irqsave(&bfad->bfad_lock, flags);
Maggie Zhangf7f738122010-12-09 19:08:43 -0800218 bfa_iocfc_init(&bfad->bfa);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700219 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
220
221 /* Set up interrupt handler for each vectors */
222 if ((bfad->bfad_flags & BFAD_MSIX_ON) &&
223 bfad_install_msix_handler(bfad)) {
224 printk(KERN_WARNING "%s: install_msix failed, bfad%d\n",
225 __func__, bfad->inst_no);
226 }
227
228 bfad_init_timer(bfad);
229
230 wait_for_completion(&bfad->comp);
231
232 if ((bfad->bfad_flags & BFAD_HAL_INIT_DONE)) {
233 bfa_sm_send_event(bfad, BFAD_E_INIT_SUCCESS);
234 } else {
Krishna Gudipati7c38c052011-04-14 16:50:35 -0700235 printk(KERN_WARNING
236 "bfa %s: bfa init failed\n",
237 bfad->pci_name);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700238 bfad->bfad_flags |= BFAD_HAL_INIT_FAIL;
239 bfa_sm_send_event(bfad, BFAD_E_INIT_FAILED);
240 }
241
242 break;
243
244 case BFAD_E_KTHREAD_CREATE_FAILED:
245 bfa_sm_set_state(bfad, bfad_sm_uninit);
246 break;
247
248 default:
249 bfa_sm_fault(bfad, event);
250 }
Jing Huang7725ccf2009-09-23 17:46:15 -0700251}
252
253static void
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700254bfad_sm_initializing(struct bfad_s *bfad, enum bfad_sm_event event)
Jing Huang7725ccf2009-09-23 17:46:15 -0700255{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700256 int retval;
257 unsigned long flags;
258
259 bfa_trc(bfad, event);
260
261 switch (event) {
262 case BFAD_E_INIT_SUCCESS:
263 kthread_stop(bfad->bfad_tsk);
264 spin_lock_irqsave(&bfad->bfad_lock, flags);
265 bfad->bfad_tsk = NULL;
266 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
267
268 retval = bfad_start_ops(bfad);
269 if (retval != BFA_STATUS_OK)
270 break;
271 bfa_sm_set_state(bfad, bfad_sm_operational);
272 break;
273
274 case BFAD_E_INTR_INIT_FAILED:
275 bfa_sm_set_state(bfad, bfad_sm_uninit);
276 kthread_stop(bfad->bfad_tsk);
277 spin_lock_irqsave(&bfad->bfad_lock, flags);
278 bfad->bfad_tsk = NULL;
279 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
280 break;
281
282 case BFAD_E_INIT_FAILED:
283 bfa_sm_set_state(bfad, bfad_sm_failed);
284 break;
285 default:
286 bfa_sm_fault(bfad, event);
287 }
Jing Huang7725ccf2009-09-23 17:46:15 -0700288}
289
290static void
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700291bfad_sm_failed(struct bfad_s *bfad, enum bfad_sm_event event)
Jing Huang7725ccf2009-09-23 17:46:15 -0700292{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700293 int retval;
Jing Huang7725ccf2009-09-23 17:46:15 -0700294
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700295 bfa_trc(bfad, event);
Jing Huang7725ccf2009-09-23 17:46:15 -0700296
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700297 switch (event) {
298 case BFAD_E_INIT_SUCCESS:
299 retval = bfad_start_ops(bfad);
300 if (retval != BFA_STATUS_OK)
301 break;
302 bfa_sm_set_state(bfad, bfad_sm_operational);
303 break;
Jing Huang7725ccf2009-09-23 17:46:15 -0700304
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700305 case BFAD_E_STOP:
306 if (bfad->bfad_flags & BFAD_CFG_PPORT_DONE)
307 bfad_uncfg_pport(bfad);
308 if (bfad->bfad_flags & BFAD_FC4_PROBE_DONE) {
309 bfad_im_probe_undo(bfad);
310 bfad->bfad_flags &= ~BFAD_FC4_PROBE_DONE;
311 }
312 bfad_stop(bfad);
313 break;
Jing Huang7725ccf2009-09-23 17:46:15 -0700314
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700315 case BFAD_E_EXIT_COMP:
316 bfa_sm_set_state(bfad, bfad_sm_uninit);
317 bfad_remove_intr(bfad);
318 del_timer_sync(&bfad->hal_tmo);
319 break;
Jing Huang7725ccf2009-09-23 17:46:15 -0700320
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700321 default:
322 bfa_sm_fault(bfad, event);
323 }
Jing Huang7725ccf2009-09-23 17:46:15 -0700324}
325
326static void
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700327bfad_sm_operational(struct bfad_s *bfad, enum bfad_sm_event event)
Jing Huang7725ccf2009-09-23 17:46:15 -0700328{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700329 bfa_trc(bfad, event);
Jing Huang7725ccf2009-09-23 17:46:15 -0700330
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700331 switch (event) {
332 case BFAD_E_STOP:
333 bfa_sm_set_state(bfad, bfad_sm_fcs_exit);
334 bfad_fcs_stop(bfad);
335 break;
Jing Huang7725ccf2009-09-23 17:46:15 -0700336
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700337 default:
338 bfa_sm_fault(bfad, event);
339 }
340}
341
342static void
343bfad_sm_fcs_exit(struct bfad_s *bfad, enum bfad_sm_event event)
344{
345 bfa_trc(bfad, event);
346
347 switch (event) {
348 case BFAD_E_FCS_EXIT_COMP:
349 bfa_sm_set_state(bfad, bfad_sm_stopping);
350 bfad_stop(bfad);
351 break;
352
353 default:
354 bfa_sm_fault(bfad, event);
355 }
356}
357
358static void
359bfad_sm_stopping(struct bfad_s *bfad, enum bfad_sm_event event)
360{
361 bfa_trc(bfad, event);
362
363 switch (event) {
364 case BFAD_E_EXIT_COMP:
365 bfa_sm_set_state(bfad, bfad_sm_uninit);
366 bfad_remove_intr(bfad);
367 del_timer_sync(&bfad->hal_tmo);
368 bfad_im_probe_undo(bfad);
369 bfad->bfad_flags &= ~BFAD_FC4_PROBE_DONE;
370 bfad_uncfg_pport(bfad);
371 break;
372
373 default:
374 bfa_sm_fault(bfad, event);
375 break;
376 }
Jing Huang7725ccf2009-09-23 17:46:15 -0700377}
378
Jing Huang5fbe25c2010-10-18 17:17:23 -0700379/*
Jing Huang7725ccf2009-09-23 17:46:15 -0700380 * BFA callbacks
381 */
382void
383bfad_hcb_comp(void *arg, bfa_status_t status)
384{
385 struct bfad_hal_comp *fcomp = (struct bfad_hal_comp *)arg;
386
387 fcomp->status = status;
388 complete(&fcomp->comp);
389}
390
Jing Huang5fbe25c2010-10-18 17:17:23 -0700391/*
Jing Huang7725ccf2009-09-23 17:46:15 -0700392 * bfa_init callback
393 */
394void
395bfa_cb_init(void *drv, bfa_status_t init_status)
396{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700397 struct bfad_s *bfad = drv;
Jing Huang7725ccf2009-09-23 17:46:15 -0700398
Krishna Gudipatie6714322010-03-03 17:44:02 -0800399 if (init_status == BFA_STATUS_OK) {
Jing Huang7725ccf2009-09-23 17:46:15 -0700400 bfad->bfad_flags |= BFAD_HAL_INIT_DONE;
401
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700402 /*
403 * If BFAD_HAL_INIT_FAIL flag is set:
Krishna Gudipatie6714322010-03-03 17:44:02 -0800404 * Wake up the kernel thread to start
405 * the bfad operations after HAL init done
406 */
407 if ((bfad->bfad_flags & BFAD_HAL_INIT_FAIL)) {
408 bfad->bfad_flags &= ~BFAD_HAL_INIT_FAIL;
409 wake_up_process(bfad->bfad_tsk);
410 }
411 }
412
Jing Huang7725ccf2009-09-23 17:46:15 -0700413 complete(&bfad->comp);
414}
415
Jing Huang5fbe25c2010-10-18 17:17:23 -0700416/*
Jing Huang7725ccf2009-09-23 17:46:15 -0700417 * BFA_FCS callbacks
418 */
Jing Huang7725ccf2009-09-23 17:46:15 -0700419struct bfad_port_s *
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700420bfa_fcb_lport_new(struct bfad_s *bfad, struct bfa_fcs_lport_s *port,
421 enum bfa_lport_role roles, struct bfad_vf_s *vf_drv,
Jing Huang7725ccf2009-09-23 17:46:15 -0700422 struct bfad_vport_s *vp_drv)
423{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700424 bfa_status_t rc;
425 struct bfad_port_s *port_drv;
Jing Huang7725ccf2009-09-23 17:46:15 -0700426
427 if (!vp_drv && !vf_drv) {
428 port_drv = &bfad->pport;
429 port_drv->pvb_type = BFAD_PORT_PHYS_BASE;
430 } else if (!vp_drv && vf_drv) {
431 port_drv = &vf_drv->base_port;
432 port_drv->pvb_type = BFAD_PORT_VF_BASE;
433 } else if (vp_drv && !vf_drv) {
434 port_drv = &vp_drv->drv_port;
435 port_drv->pvb_type = BFAD_PORT_PHYS_VPORT;
436 } else {
437 port_drv = &vp_drv->drv_port;
438 port_drv->pvb_type = BFAD_PORT_VF_VPORT;
439 }
440
441 port_drv->fcs_port = port;
442 port_drv->roles = roles;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700443
444 if (roles & BFA_LPORT_ROLE_FCP_IM) {
445 rc = bfad_im_port_new(bfad, port_drv);
446 if (rc != BFA_STATUS_OK) {
447 bfad_im_port_delete(bfad, port_drv);
448 port_drv = NULL;
449 }
Jing Huang7725ccf2009-09-23 17:46:15 -0700450 }
451
452 return port_drv;
453}
454
455void
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700456bfa_fcb_lport_delete(struct bfad_s *bfad, enum bfa_lport_role roles,
Jing Huang7725ccf2009-09-23 17:46:15 -0700457 struct bfad_vf_s *vf_drv, struct bfad_vport_s *vp_drv)
458{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700459 struct bfad_port_s *port_drv;
Jing Huang7725ccf2009-09-23 17:46:15 -0700460
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700461 /* this will be only called from rmmod context */
Jing Huang7725ccf2009-09-23 17:46:15 -0700462 if (vp_drv && !vp_drv->comp_del) {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700463 port_drv = (vp_drv) ? (&(vp_drv)->drv_port) :
464 ((vf_drv) ? (&(vf_drv)->base_port) :
465 (&(bfad)->pport));
Jing Huang7725ccf2009-09-23 17:46:15 -0700466 bfa_trc(bfad, roles);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700467 if (roles & BFA_LPORT_ROLE_FCP_IM)
468 bfad_im_port_delete(bfad, port_drv);
Jing Huang7725ccf2009-09-23 17:46:15 -0700469 }
Jing Huang7725ccf2009-09-23 17:46:15 -0700470}
471
Jing Huang5fbe25c2010-10-18 17:17:23 -0700472/*
Jing Huang7725ccf2009-09-23 17:46:15 -0700473 * FCS RPORT alloc callback, after successful PLOGI by FCS
474 */
475bfa_status_t
476bfa_fcb_rport_alloc(struct bfad_s *bfad, struct bfa_fcs_rport_s **rport,
477 struct bfad_rport_s **rport_drv)
478{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700479 bfa_status_t rc = BFA_STATUS_OK;
Jing Huang7725ccf2009-09-23 17:46:15 -0700480
481 *rport_drv = kzalloc(sizeof(struct bfad_rport_s), GFP_ATOMIC);
482 if (*rport_drv == NULL) {
483 rc = BFA_STATUS_ENOMEM;
484 goto ext;
485 }
486
487 *rport = &(*rport_drv)->fcs_rport;
488
489ext:
490 return rc;
491}
492
Jing Huang5fbe25c2010-10-18 17:17:23 -0700493/*
Jing Huangd9883542010-07-08 19:46:26 -0700494 * FCS PBC VPORT Create
495 */
496void
497bfa_fcb_pbc_vport_create(struct bfad_s *bfad, struct bfi_pbc_vport_s pbc_vport)
498{
Jing Huang7725ccf2009-09-23 17:46:15 -0700499
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700500 struct bfa_lport_cfg_s port_cfg = {0};
501 struct bfad_vport_s *vport;
502 int rc;
Jing Huangd9883542010-07-08 19:46:26 -0700503
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700504 vport = kzalloc(sizeof(struct bfad_vport_s), GFP_KERNEL);
505 if (!vport) {
Jing Huangd9883542010-07-08 19:46:26 -0700506 bfa_trc(bfad, 0);
507 return;
508 }
509
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700510 vport->drv_port.bfad = bfad;
511 port_cfg.roles = BFA_LPORT_ROLE_FCP_IM;
512 port_cfg.pwwn = pbc_vport.vp_pwwn;
513 port_cfg.nwwn = pbc_vport.vp_nwwn;
514 port_cfg.preboot_vp = BFA_TRUE;
Jing Huangd9883542010-07-08 19:46:26 -0700515
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700516 rc = bfa_fcs_pbc_vport_create(&vport->fcs_vport, &bfad->bfa_fcs, 0,
517 &port_cfg, vport);
Jing Huangd9883542010-07-08 19:46:26 -0700518
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700519 if (rc != BFA_STATUS_OK) {
520 bfa_trc(bfad, 0);
521 return;
522 }
523
524 list_add_tail(&vport->list_entry, &bfad->pbc_vport_list);
Jing Huangd9883542010-07-08 19:46:26 -0700525}
Jing Huang7725ccf2009-09-23 17:46:15 -0700526
527void
528bfad_hal_mem_release(struct bfad_s *bfad)
529{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700530 int i;
Jing Huang7725ccf2009-09-23 17:46:15 -0700531 struct bfa_meminfo_s *hal_meminfo = &bfad->meminfo;
532 struct bfa_mem_elem_s *meminfo_elem;
533
534 for (i = 0; i < BFA_MEM_TYPE_MAX; i++) {
535 meminfo_elem = &hal_meminfo->meminfo[i];
536 if (meminfo_elem->kva != NULL) {
537 switch (meminfo_elem->mem_type) {
538 case BFA_MEM_TYPE_KVA:
539 vfree(meminfo_elem->kva);
540 break;
541 case BFA_MEM_TYPE_DMA:
542 dma_free_coherent(&bfad->pcidev->dev,
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700543 meminfo_elem->mem_len,
544 meminfo_elem->kva,
545 (dma_addr_t) meminfo_elem->dma);
Jing Huang7725ccf2009-09-23 17:46:15 -0700546 break;
547 default:
Jing Huangd4b671c2010-12-26 21:46:35 -0800548 WARN_ON(1);
Jing Huang7725ccf2009-09-23 17:46:15 -0700549 break;
550 }
551 }
552 }
553
554 memset(hal_meminfo, 0, sizeof(struct bfa_meminfo_s));
555}
556
557void
558bfad_update_hal_cfg(struct bfa_iocfc_cfg_s *bfa_cfg)
559{
560 if (num_rports > 0)
561 bfa_cfg->fwcfg.num_rports = num_rports;
562 if (num_ios > 0)
563 bfa_cfg->fwcfg.num_ioim_reqs = num_ios;
564 if (num_tms > 0)
565 bfa_cfg->fwcfg.num_tskim_reqs = num_tms;
566 if (num_fcxps > 0)
567 bfa_cfg->fwcfg.num_fcxp_reqs = num_fcxps;
568 if (num_ufbufs > 0)
569 bfa_cfg->fwcfg.num_uf_bufs = num_ufbufs;
570 if (reqq_size > 0)
571 bfa_cfg->drvcfg.num_reqq_elems = reqq_size;
572 if (rspq_size > 0)
573 bfa_cfg->drvcfg.num_rspq_elems = rspq_size;
574 if (num_sgpgs > 0)
575 bfa_cfg->drvcfg.num_sgpgs = num_sgpgs;
576
577 /*
578 * populate the hal values back to the driver for sysfs use.
579 * otherwise, the default values will be shown as 0 in sysfs
580 */
581 num_rports = bfa_cfg->fwcfg.num_rports;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700582 num_ios = bfa_cfg->fwcfg.num_ioim_reqs;
583 num_tms = bfa_cfg->fwcfg.num_tskim_reqs;
584 num_fcxps = bfa_cfg->fwcfg.num_fcxp_reqs;
Jing Huang7725ccf2009-09-23 17:46:15 -0700585 num_ufbufs = bfa_cfg->fwcfg.num_uf_bufs;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700586 reqq_size = bfa_cfg->drvcfg.num_reqq_elems;
587 rspq_size = bfa_cfg->drvcfg.num_rspq_elems;
588 num_sgpgs = bfa_cfg->drvcfg.num_sgpgs;
Jing Huang7725ccf2009-09-23 17:46:15 -0700589}
590
591bfa_status_t
592bfad_hal_mem_alloc(struct bfad_s *bfad)
593{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700594 int i;
Jing Huang7725ccf2009-09-23 17:46:15 -0700595 struct bfa_meminfo_s *hal_meminfo = &bfad->meminfo;
596 struct bfa_mem_elem_s *meminfo_elem;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700597 dma_addr_t phys_addr;
598 void *kva;
599 bfa_status_t rc = BFA_STATUS_OK;
600 int retry_count = 0;
601 int reset_value = 1;
602 int min_num_sgpgs = 512;
Jing Huang7725ccf2009-09-23 17:46:15 -0700603
604 bfa_cfg_get_default(&bfad->ioc_cfg);
605
606retry:
607 bfad_update_hal_cfg(&bfad->ioc_cfg);
608 bfad->cfg_data.ioc_queue_depth = bfad->ioc_cfg.fwcfg.num_ioim_reqs;
609 bfa_cfg_get_meminfo(&bfad->ioc_cfg, hal_meminfo);
610
611 for (i = 0; i < BFA_MEM_TYPE_MAX; i++) {
612 meminfo_elem = &hal_meminfo->meminfo[i];
613 switch (meminfo_elem->mem_type) {
614 case BFA_MEM_TYPE_KVA:
615 kva = vmalloc(meminfo_elem->mem_len);
616 if (kva == NULL) {
617 bfad_hal_mem_release(bfad);
618 rc = BFA_STATUS_ENOMEM;
619 goto ext;
620 }
621 memset(kva, 0, meminfo_elem->mem_len);
622 meminfo_elem->kva = kva;
623 break;
624 case BFA_MEM_TYPE_DMA:
625 kva = dma_alloc_coherent(&bfad->pcidev->dev,
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700626 meminfo_elem->mem_len, &phys_addr, GFP_KERNEL);
Jing Huang7725ccf2009-09-23 17:46:15 -0700627 if (kva == NULL) {
628 bfad_hal_mem_release(bfad);
629 /*
630 * If we cannot allocate with default
631 * num_sgpages try with half the value.
632 */
633 if (num_sgpgs > min_num_sgpgs) {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700634 printk(KERN_INFO
635 "bfad[%d]: memory allocation failed"
636 " with num_sgpgs: %d\n",
Jing Huang7725ccf2009-09-23 17:46:15 -0700637 bfad->inst_no, num_sgpgs);
638 nextLowerInt(&num_sgpgs);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700639 printk(KERN_INFO
640 "bfad[%d]: trying to allocate memory"
641 " with num_sgpgs: %d\n",
Jing Huang7725ccf2009-09-23 17:46:15 -0700642 bfad->inst_no, num_sgpgs);
643 retry_count++;
644 goto retry;
645 } else {
646 if (num_sgpgs_parm > 0)
647 num_sgpgs = num_sgpgs_parm;
648 else {
649 reset_value =
650 (1 << retry_count);
651 num_sgpgs *= reset_value;
652 }
653 rc = BFA_STATUS_ENOMEM;
654 goto ext;
655 }
656 }
657
658 if (num_sgpgs_parm > 0)
659 num_sgpgs = num_sgpgs_parm;
660 else {
661 reset_value = (1 << retry_count);
662 num_sgpgs *= reset_value;
663 }
664
665 memset(kva, 0, meminfo_elem->mem_len);
666 meminfo_elem->kva = kva;
667 meminfo_elem->dma = phys_addr;
668 break;
669 default:
670 break;
671
672 }
673 }
674ext:
675 return rc;
676}
677
Jing Huang5fbe25c2010-10-18 17:17:23 -0700678/*
Jing Huang7725ccf2009-09-23 17:46:15 -0700679 * Create a vport under a vf.
680 */
681bfa_status_t
682bfad_vport_create(struct bfad_s *bfad, u16 vf_id,
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700683 struct bfa_lport_cfg_s *port_cfg, struct device *dev)
Jing Huang7725ccf2009-09-23 17:46:15 -0700684{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700685 struct bfad_vport_s *vport;
686 int rc = BFA_STATUS_OK;
687 unsigned long flags;
Jing Huang7725ccf2009-09-23 17:46:15 -0700688 struct completion fcomp;
689
690 vport = kzalloc(sizeof(struct bfad_vport_s), GFP_KERNEL);
691 if (!vport) {
692 rc = BFA_STATUS_ENOMEM;
693 goto ext;
694 }
695
696 vport->drv_port.bfad = bfad;
697 spin_lock_irqsave(&bfad->bfad_lock, flags);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700698 rc = bfa_fcs_vport_create(&vport->fcs_vport, &bfad->bfa_fcs, vf_id,
699 port_cfg, vport);
Jing Huang7725ccf2009-09-23 17:46:15 -0700700 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
701
702 if (rc != BFA_STATUS_OK)
703 goto ext_free_vport;
704
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700705 if (port_cfg->roles & BFA_LPORT_ROLE_FCP_IM) {
Jing Huangb5042932010-03-19 11:05:39 -0700706 rc = bfad_im_scsi_host_alloc(bfad, vport->drv_port.im_port,
707 dev);
Jing Huang7725ccf2009-09-23 17:46:15 -0700708 if (rc != BFA_STATUS_OK)
709 goto ext_free_fcs_vport;
710 }
711
712 spin_lock_irqsave(&bfad->bfad_lock, flags);
713 bfa_fcs_vport_start(&vport->fcs_vport);
714 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
715
716 return BFA_STATUS_OK;
717
718ext_free_fcs_vport:
719 spin_lock_irqsave(&bfad->bfad_lock, flags);
720 vport->comp_del = &fcomp;
721 init_completion(vport->comp_del);
722 bfa_fcs_vport_delete(&vport->fcs_vport);
723 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
724 wait_for_completion(vport->comp_del);
725ext_free_vport:
726 kfree(vport);
727ext:
728 return rc;
729}
730
Jing Huang7725ccf2009-09-23 17:46:15 -0700731void
732bfad_bfa_tmo(unsigned long data)
733{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700734 struct bfad_s *bfad = (struct bfad_s *) data;
735 unsigned long flags;
736 struct list_head doneq;
Jing Huang7725ccf2009-09-23 17:46:15 -0700737
738 spin_lock_irqsave(&bfad->bfad_lock, flags);
739
Maggie Zhangf7f738122010-12-09 19:08:43 -0800740 bfa_timer_beat(&bfad->bfa.timer_mod);
Jing Huang7725ccf2009-09-23 17:46:15 -0700741
742 bfa_comp_deq(&bfad->bfa, &doneq);
743 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
744
745 if (!list_empty(&doneq)) {
746 bfa_comp_process(&bfad->bfa, &doneq);
747 spin_lock_irqsave(&bfad->bfad_lock, flags);
748 bfa_comp_free(&bfad->bfa, &doneq);
749 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
750 }
751
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700752 mod_timer(&bfad->hal_tmo,
753 jiffies + msecs_to_jiffies(BFA_TIMER_FREQ));
Jing Huang7725ccf2009-09-23 17:46:15 -0700754}
755
756void
757bfad_init_timer(struct bfad_s *bfad)
758{
759 init_timer(&bfad->hal_tmo);
760 bfad->hal_tmo.function = bfad_bfa_tmo;
761 bfad->hal_tmo.data = (unsigned long)bfad;
762
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700763 mod_timer(&bfad->hal_tmo,
764 jiffies + msecs_to_jiffies(BFA_TIMER_FREQ));
Jing Huang7725ccf2009-09-23 17:46:15 -0700765}
766
767int
768bfad_pci_init(struct pci_dev *pdev, struct bfad_s *bfad)
769{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700770 int rc = -ENODEV;
Jing Huang7725ccf2009-09-23 17:46:15 -0700771
772 if (pci_enable_device(pdev)) {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700773 printk(KERN_ERR "pci_enable_device fail %p\n", pdev);
Jing Huang7725ccf2009-09-23 17:46:15 -0700774 goto out;
775 }
776
777 if (pci_request_regions(pdev, BFAD_DRIVER_NAME))
778 goto out_disable_device;
779
780 pci_set_master(pdev);
781
782
783 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0)
784 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0) {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700785 printk(KERN_ERR "pci_set_dma_mask fail %p\n", pdev);
Jing Huang7725ccf2009-09-23 17:46:15 -0700786 goto out_release_region;
787 }
788
Jing Huangb3522f02010-03-19 11:06:44 -0700789 bfad->pci_bar0_kva = pci_iomap(pdev, 0, pci_resource_len(pdev, 0));
Jing Huang7725ccf2009-09-23 17:46:15 -0700790
791 if (bfad->pci_bar0_kva == NULL) {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700792 printk(KERN_ERR "Fail to map bar0\n");
Jing Huang7725ccf2009-09-23 17:46:15 -0700793 goto out_release_region;
794 }
795
796 bfad->hal_pcidev.pci_slot = PCI_SLOT(pdev->devfn);
797 bfad->hal_pcidev.pci_func = PCI_FUNC(pdev->devfn);
798 bfad->hal_pcidev.pci_bar_kva = bfad->pci_bar0_kva;
799 bfad->hal_pcidev.device_id = pdev->device;
800 bfad->pci_name = pci_name(pdev);
801
802 bfad->pci_attr.vendor_id = pdev->vendor;
803 bfad->pci_attr.device_id = pdev->device;
804 bfad->pci_attr.ssid = pdev->subsystem_device;
805 bfad->pci_attr.ssvid = pdev->subsystem_vendor;
806 bfad->pci_attr.pcifn = PCI_FUNC(pdev->devfn);
807
808 bfad->pcidev = pdev;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700809
810 /* Adjust PCIe Maximum Read Request Size */
811 if (pcie_max_read_reqsz > 0) {
812 int pcie_cap_reg;
813 u16 pcie_dev_ctl;
814 u16 mask = 0xffff;
815
816 switch (pcie_max_read_reqsz) {
817 case 128:
818 mask = 0x0;
819 break;
820 case 256:
821 mask = 0x1000;
822 break;
823 case 512:
824 mask = 0x2000;
825 break;
826 case 1024:
827 mask = 0x3000;
828 break;
829 case 2048:
830 mask = 0x4000;
831 break;
832 case 4096:
833 mask = 0x5000;
834 break;
835 default:
836 break;
837 }
838
839 pcie_cap_reg = pci_find_capability(pdev, PCI_CAP_ID_EXP);
840 if (mask != 0xffff && pcie_cap_reg) {
841 pcie_cap_reg += 0x08;
842 pci_read_config_word(pdev, pcie_cap_reg, &pcie_dev_ctl);
843 if ((pcie_dev_ctl & 0x7000) != mask) {
844 printk(KERN_WARNING "BFA[%s]: "
845 "pcie_max_read_request_size is %d, "
846 "reset to %d\n", bfad->pci_name,
847 (1 << ((pcie_dev_ctl & 0x7000) >> 12)) << 7,
848 pcie_max_read_reqsz);
849
850 pcie_dev_ctl &= ~0x7000;
851 pci_write_config_word(pdev, pcie_cap_reg,
852 pcie_dev_ctl | mask);
853 }
854 }
855 }
856
Jing Huang7725ccf2009-09-23 17:46:15 -0700857 return 0;
858
859out_release_region:
860 pci_release_regions(pdev);
861out_disable_device:
862 pci_disable_device(pdev);
863out:
864 return rc;
865}
866
867void
868bfad_pci_uninit(struct pci_dev *pdev, struct bfad_s *bfad)
869{
Jing Huang7725ccf2009-09-23 17:46:15 -0700870 pci_iounmap(pdev, bfad->pci_bar0_kva);
Jing Huang7725ccf2009-09-23 17:46:15 -0700871 pci_release_regions(pdev);
872 pci_disable_device(pdev);
873 pci_set_drvdata(pdev, NULL);
874}
875
Jing Huang7725ccf2009-09-23 17:46:15 -0700876bfa_status_t
877bfad_drv_init(struct bfad_s *bfad)
878{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700879 bfa_status_t rc;
880 unsigned long flags;
Jing Huang7725ccf2009-09-23 17:46:15 -0700881
882 bfad->cfg_data.rport_del_timeout = rport_del_timeout;
883 bfad->cfg_data.lun_queue_depth = bfa_lun_queue_depth;
884 bfad->cfg_data.io_max_sge = bfa_io_max_sge;
885 bfad->cfg_data.binding_method = FCP_PWWN_BINDING;
886
887 rc = bfad_hal_mem_alloc(bfad);
888 if (rc != BFA_STATUS_OK) {
889 printk(KERN_WARNING "bfad%d bfad_hal_mem_alloc failure\n",
890 bfad->inst_no);
891 printk(KERN_WARNING
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700892 "Not enough memory to attach all Brocade HBA ports, %s",
893 "System may need more memory.\n");
Jing Huang7725ccf2009-09-23 17:46:15 -0700894 goto out_hal_mem_alloc_failure;
895 }
896
Maggie Zhangf7f738122010-12-09 19:08:43 -0800897 bfad->bfa.trcmod = bfad->trcmod;
898 bfad->bfa.plog = &bfad->plog_buf;
Jing Huang7725ccf2009-09-23 17:46:15 -0700899 bfa_plog_init(&bfad->plog_buf);
900 bfa_plog_str(&bfad->plog_buf, BFA_PL_MID_DRVR, BFA_PL_EID_DRIVER_START,
901 0, "Driver Attach");
902
903 bfa_attach(&bfad->bfa, bfad, &bfad->ioc_cfg, &bfad->meminfo,
904 &bfad->hal_pcidev);
905
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700906 /* FCS INIT */
Jing Huang7725ccf2009-09-23 17:46:15 -0700907 spin_lock_irqsave(&bfad->bfad_lock, flags);
Maggie Zhangf7f738122010-12-09 19:08:43 -0800908 bfad->bfa_fcs.trcmod = bfad->trcmod;
Krishna Gudipati82794a22010-03-03 17:43:30 -0800909 bfa_fcs_attach(&bfad->bfa_fcs, &bfad->bfa, bfad, BFA_FALSE);
Maggie Zhangf7f738122010-12-09 19:08:43 -0800910 bfad->bfa_fcs.fdmi_enabled = fdmi_enable;
Jing Huang7725ccf2009-09-23 17:46:15 -0700911 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
912
913 bfad->bfad_flags |= BFAD_DRV_INIT_DONE;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700914
Jing Huang7725ccf2009-09-23 17:46:15 -0700915 return BFA_STATUS_OK;
916
Jing Huang7725ccf2009-09-23 17:46:15 -0700917out_hal_mem_alloc_failure:
918 return BFA_STATUS_FAILED;
919}
920
921void
922bfad_drv_uninit(struct bfad_s *bfad)
923{
Krishna Gudipatie6714322010-03-03 17:44:02 -0800924 unsigned long flags;
925
926 spin_lock_irqsave(&bfad->bfad_lock, flags);
927 init_completion(&bfad->comp);
Maggie Zhangf7f738122010-12-09 19:08:43 -0800928 bfa_iocfc_stop(&bfad->bfa);
Krishna Gudipatie6714322010-03-03 17:44:02 -0800929 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
930 wait_for_completion(&bfad->comp);
931
Jing Huang7725ccf2009-09-23 17:46:15 -0700932 del_timer_sync(&bfad->hal_tmo);
933 bfa_isr_disable(&bfad->bfa);
934 bfa_detach(&bfad->bfa);
935 bfad_remove_intr(bfad);
Jing Huang7725ccf2009-09-23 17:46:15 -0700936 bfad_hal_mem_release(bfad);
Krishna Gudipatie6714322010-03-03 17:44:02 -0800937
938 bfad->bfad_flags &= ~BFAD_DRV_INIT_DONE;
Jing Huang7725ccf2009-09-23 17:46:15 -0700939}
940
941void
942bfad_drv_start(struct bfad_s *bfad)
943{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700944 unsigned long flags;
Jing Huang7725ccf2009-09-23 17:46:15 -0700945
946 spin_lock_irqsave(&bfad->bfad_lock, flags);
Maggie Zhangf7f738122010-12-09 19:08:43 -0800947 bfa_iocfc_start(&bfad->bfa);
948 bfa_fcs_fabric_modstart(&bfad->bfa_fcs);
Jing Huang7725ccf2009-09-23 17:46:15 -0700949 bfad->bfad_flags |= BFAD_HAL_START_DONE;
950 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
951
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700952 if (bfad->im)
953 flush_workqueue(bfad->im->drv_workq);
Jing Huang7725ccf2009-09-23 17:46:15 -0700954}
955
956void
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700957bfad_fcs_stop(struct bfad_s *bfad)
Jing Huang7725ccf2009-09-23 17:46:15 -0700958{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700959 unsigned long flags;
Jing Huang7725ccf2009-09-23 17:46:15 -0700960
961 spin_lock_irqsave(&bfad->bfad_lock, flags);
962 init_completion(&bfad->comp);
963 bfad->pport.flags |= BFAD_PORT_DELETE;
964 bfa_fcs_exit(&bfad->bfa_fcs);
965 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
966 wait_for_completion(&bfad->comp);
967
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700968 bfa_sm_send_event(bfad, BFAD_E_FCS_EXIT_COMP);
969}
970
971void
972bfad_stop(struct bfad_s *bfad)
973{
974 unsigned long flags;
975
Jing Huang7725ccf2009-09-23 17:46:15 -0700976 spin_lock_irqsave(&bfad->bfad_lock, flags);
977 init_completion(&bfad->comp);
Maggie Zhangf7f738122010-12-09 19:08:43 -0800978 bfa_iocfc_stop(&bfad->bfa);
Jing Huang7725ccf2009-09-23 17:46:15 -0700979 bfad->bfad_flags &= ~BFAD_HAL_START_DONE;
980 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
981 wait_for_completion(&bfad->comp);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700982
983 bfa_sm_send_event(bfad, BFAD_E_EXIT_COMP);
Jing Huang7725ccf2009-09-23 17:46:15 -0700984}
985
986bfa_status_t
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700987bfad_cfg_pport(struct bfad_s *bfad, enum bfa_lport_role role)
Jing Huang7725ccf2009-09-23 17:46:15 -0700988{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700989 int rc = BFA_STATUS_OK;
Jing Huang7725ccf2009-09-23 17:46:15 -0700990
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700991 /* Allocate scsi_host for the physical port */
992 if ((supported_fc4s & BFA_LPORT_ROLE_FCP_IM) &&
993 (role & BFA_LPORT_ROLE_FCP_IM)) {
Jing Huang7725ccf2009-09-23 17:46:15 -0700994 if (bfad->pport.im_port == NULL) {
995 rc = BFA_STATUS_FAILED;
996 goto out;
997 }
998
Jing Huangb5042932010-03-19 11:05:39 -0700999 rc = bfad_im_scsi_host_alloc(bfad, bfad->pport.im_port,
1000 &bfad->pcidev->dev);
Jing Huang7725ccf2009-09-23 17:46:15 -07001001 if (rc != BFA_STATUS_OK)
1002 goto out;
1003
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001004 bfad->pport.roles |= BFA_LPORT_ROLE_FCP_IM;
Jing Huang7725ccf2009-09-23 17:46:15 -07001005 }
1006
1007 bfad->bfad_flags |= BFAD_CFG_PPORT_DONE;
1008
1009out:
1010 return rc;
1011}
1012
1013void
1014bfad_uncfg_pport(struct bfad_s *bfad)
1015{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001016 if ((supported_fc4s & BFA_LPORT_ROLE_FCP_IM) &&
1017 (bfad->pport.roles & BFA_LPORT_ROLE_FCP_IM)) {
Jing Huang7725ccf2009-09-23 17:46:15 -07001018 bfad_im_scsi_host_free(bfad, bfad->pport.im_port);
1019 bfad_im_port_clean(bfad->pport.im_port);
1020 kfree(bfad->pport.im_port);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001021 bfad->pport.roles &= ~BFA_LPORT_ROLE_FCP_IM;
Jing Huang7725ccf2009-09-23 17:46:15 -07001022 }
1023
1024 bfad->bfad_flags &= ~BFAD_CFG_PPORT_DONE;
1025}
1026
Krishna Gudipatie6714322010-03-03 17:44:02 -08001027bfa_status_t
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001028bfad_start_ops(struct bfad_s *bfad) {
1029
1030 int retval;
1031 unsigned long flags;
1032 struct bfad_vport_s *vport, *vport_new;
1033 struct bfa_fcs_driver_info_s driver_info;
1034
1035 /* Fill the driver_info info to fcs*/
1036 memset(&driver_info, 0, sizeof(driver_info));
1037 strncpy(driver_info.version, BFAD_DRIVER_VERSION,
1038 sizeof(driver_info.version) - 1);
1039 if (host_name)
1040 strncpy(driver_info.host_machine_name, host_name,
1041 sizeof(driver_info.host_machine_name) - 1);
1042 if (os_name)
1043 strncpy(driver_info.host_os_name, os_name,
1044 sizeof(driver_info.host_os_name) - 1);
1045 if (os_patch)
1046 strncpy(driver_info.host_os_patch, os_patch,
1047 sizeof(driver_info.host_os_patch) - 1);
1048
1049 strncpy(driver_info.os_device_name, bfad->pci_name,
1050 sizeof(driver_info.os_device_name - 1));
1051
1052 /* FCS INIT */
1053 spin_lock_irqsave(&bfad->bfad_lock, flags);
1054 bfa_fcs_driver_info_init(&bfad->bfa_fcs, &driver_info);
1055 bfa_fcs_init(&bfad->bfa_fcs);
1056 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
Krishna Gudipatie6714322010-03-03 17:44:02 -08001057
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001058 retval = bfad_cfg_pport(bfad, BFA_LPORT_ROLE_FCP_IM);
Krishna Gudipatie6714322010-03-03 17:44:02 -08001059 if (retval != BFA_STATUS_OK) {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001060 if (bfa_sm_cmp_state(bfad, bfad_sm_initializing))
1061 bfa_sm_set_state(bfad, bfad_sm_failed);
1062 bfad_stop(bfad);
1063 return BFA_STATUS_FAILED;
Krishna Gudipatie6714322010-03-03 17:44:02 -08001064 }
1065
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001066 /* BFAD level FC4 IM specific resource allocation */
1067 retval = bfad_im_probe(bfad);
1068 if (retval != BFA_STATUS_OK) {
1069 printk(KERN_WARNING "bfad_im_probe failed\n");
1070 if (bfa_sm_cmp_state(bfad, bfad_sm_initializing))
1071 bfa_sm_set_state(bfad, bfad_sm_failed);
1072 bfad_im_probe_undo(bfad);
1073 bfad->bfad_flags &= ~BFAD_FC4_PROBE_DONE;
1074 bfad_uncfg_pport(bfad);
1075 bfad_stop(bfad);
1076 return BFA_STATUS_FAILED;
1077 } else
1078 bfad->bfad_flags |= BFAD_FC4_PROBE_DONE;
1079
Krishna Gudipatie6714322010-03-03 17:44:02 -08001080 bfad_drv_start(bfad);
1081
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001082 /* Complete pbc vport create */
1083 list_for_each_entry_safe(vport, vport_new, &bfad->pbc_vport_list,
1084 list_entry) {
Jing Huangd9883542010-07-08 19:46:26 -07001085 struct fc_vport_identifiers vid;
1086 struct fc_vport *fc_vport;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001087 char pwwn_buf[BFA_STRING_32];
Jing Huangd9883542010-07-08 19:46:26 -07001088
1089 memset(&vid, 0, sizeof(vid));
1090 vid.roles = FC_PORT_ROLE_FCP_INITIATOR;
1091 vid.vport_type = FC_PORTTYPE_NPIV;
1092 vid.disable = false;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001093 vid.node_name = wwn_to_u64((u8 *)
1094 (&((vport->fcs_vport).lport.port_cfg.nwwn)));
1095 vid.port_name = wwn_to_u64((u8 *)
1096 (&((vport->fcs_vport).lport.port_cfg.pwwn)));
Jing Huangd9883542010-07-08 19:46:26 -07001097 fc_vport = fc_vport_create(bfad->pport.im_port->shost, 0, &vid);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001098 if (!fc_vport) {
1099 wwn2str(pwwn_buf, vid.port_name);
Jing Huangd9883542010-07-08 19:46:26 -07001100 printk(KERN_WARNING "bfad%d: failed to create pbc vport"
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001101 " %s\n", bfad->inst_no, pwwn_buf);
1102 }
1103 list_del(&vport->list_entry);
1104 kfree(vport);
Jing Huangd9883542010-07-08 19:46:26 -07001105 }
1106
Krishna Gudipatie6714322010-03-03 17:44:02 -08001107 /*
1108 * If bfa_linkup_delay is set to -1 default; try to retrive the
Maggie Zhangf16a1752010-12-09 19:12:32 -08001109 * value using the bfad_get_linkup_delay(); else use the
Krishna Gudipatie6714322010-03-03 17:44:02 -08001110 * passed in module param value as the bfa_linkup_delay.
1111 */
1112 if (bfa_linkup_delay < 0) {
Maggie Zhangf16a1752010-12-09 19:12:32 -08001113 bfa_linkup_delay = bfad_get_linkup_delay(bfad);
1114 bfad_rport_online_wait(bfad);
Krishna Gudipatie6714322010-03-03 17:44:02 -08001115 bfa_linkup_delay = -1;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001116 } else
Maggie Zhangf16a1752010-12-09 19:12:32 -08001117 bfad_rport_online_wait(bfad);
Krishna Gudipatie6714322010-03-03 17:44:02 -08001118
Jing Huang88166242010-12-09 17:11:53 -08001119 BFA_LOG(KERN_INFO, bfad, bfa_log_level, "bfa device claimed\n");
Krishna Gudipatie6714322010-03-03 17:44:02 -08001120
1121 return BFA_STATUS_OK;
Krishna Gudipatie6714322010-03-03 17:44:02 -08001122}
1123
1124int
Jing Huangd9883542010-07-08 19:46:26 -07001125bfad_worker(void *ptr)
Krishna Gudipatie6714322010-03-03 17:44:02 -08001126{
1127 struct bfad_s *bfad;
1128 unsigned long flags;
1129
1130 bfad = (struct bfad_s *)ptr;
1131
1132 while (!kthread_should_stop()) {
1133
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001134 /* Send event BFAD_E_INIT_SUCCESS */
1135 bfa_sm_send_event(bfad, BFAD_E_INIT_SUCCESS);
Krishna Gudipatie6714322010-03-03 17:44:02 -08001136
1137 spin_lock_irqsave(&bfad->bfad_lock, flags);
1138 bfad->bfad_tsk = NULL;
1139 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1140
1141 break;
1142 }
1143
1144 return 0;
1145}
1146
Jing Huang5fbe25c2010-10-18 17:17:23 -07001147/*
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001148 * BFA driver interrupt functions
1149 */
1150irqreturn_t
1151bfad_intx(int irq, void *dev_id)
1152{
1153 struct bfad_s *bfad = dev_id;
1154 struct list_head doneq;
1155 unsigned long flags;
1156 bfa_boolean_t rc;
1157
1158 spin_lock_irqsave(&bfad->bfad_lock, flags);
1159 rc = bfa_intx(&bfad->bfa);
1160 if (!rc) {
1161 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1162 return IRQ_NONE;
1163 }
1164
1165 bfa_comp_deq(&bfad->bfa, &doneq);
1166 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1167
1168 if (!list_empty(&doneq)) {
1169 bfa_comp_process(&bfad->bfa, &doneq);
1170
1171 spin_lock_irqsave(&bfad->bfad_lock, flags);
1172 bfa_comp_free(&bfad->bfa, &doneq);
1173 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001174 }
1175
1176 return IRQ_HANDLED;
1177
1178}
1179
1180static irqreturn_t
1181bfad_msix(int irq, void *dev_id)
1182{
1183 struct bfad_msix_s *vec = dev_id;
1184 struct bfad_s *bfad = vec->bfad;
1185 struct list_head doneq;
1186 unsigned long flags;
1187
1188 spin_lock_irqsave(&bfad->bfad_lock, flags);
1189
1190 bfa_msix(&bfad->bfa, vec->msix.entry);
1191 bfa_comp_deq(&bfad->bfa, &doneq);
1192 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1193
1194 if (!list_empty(&doneq)) {
1195 bfa_comp_process(&bfad->bfa, &doneq);
1196
1197 spin_lock_irqsave(&bfad->bfad_lock, flags);
1198 bfa_comp_free(&bfad->bfa, &doneq);
1199 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1200 }
1201
1202 return IRQ_HANDLED;
1203}
1204
Jing Huang5fbe25c2010-10-18 17:17:23 -07001205/*
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001206 * Initialize the MSIX entry table.
1207 */
1208static void
1209bfad_init_msix_entry(struct bfad_s *bfad, struct msix_entry *msix_entries,
1210 int mask, int max_bit)
1211{
1212 int i;
1213 int match = 0x00000001;
1214
1215 for (i = 0, bfad->nvec = 0; i < MAX_MSIX_ENTRY; i++) {
1216 if (mask & match) {
1217 bfad->msix_tab[bfad->nvec].msix.entry = i;
1218 bfad->msix_tab[bfad->nvec].bfad = bfad;
1219 msix_entries[bfad->nvec].entry = i;
1220 bfad->nvec++;
1221 }
1222
1223 match <<= 1;
1224 }
1225
1226}
1227
1228int
1229bfad_install_msix_handler(struct bfad_s *bfad)
1230{
1231 int i, error = 0;
1232
1233 for (i = 0; i < bfad->nvec; i++) {
1234 sprintf(bfad->msix_tab[i].name, "bfa-%s-%s",
1235 bfad->pci_name,
1236 ((bfa_asic_id_ct(bfad->hal_pcidev.device_id)) ?
1237 msix_name_ct[i] : msix_name_cb[i]));
1238
1239 error = request_irq(bfad->msix_tab[i].msix.vector,
1240 (irq_handler_t) bfad_msix, 0,
1241 bfad->msix_tab[i].name, &bfad->msix_tab[i]);
1242 bfa_trc(bfad, i);
1243 bfa_trc(bfad, bfad->msix_tab[i].msix.vector);
1244 if (error) {
1245 int j;
1246
1247 for (j = 0; j < i; j++)
1248 free_irq(bfad->msix_tab[j].msix.vector,
1249 &bfad->msix_tab[j]);
1250
1251 return 1;
1252 }
1253 }
1254
1255 return 0;
1256}
1257
Jing Huang5fbe25c2010-10-18 17:17:23 -07001258/*
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001259 * Setup MSIX based interrupt.
1260 */
1261int
1262bfad_setup_intr(struct bfad_s *bfad)
1263{
1264 int error = 0;
1265 u32 mask = 0, i, num_bit = 0, max_bit = 0;
1266 struct msix_entry msix_entries[MAX_MSIX_ENTRY];
1267 struct pci_dev *pdev = bfad->pcidev;
1268
1269 /* Call BFA to get the msix map for this PCI function. */
1270 bfa_msix_getvecs(&bfad->bfa, &mask, &num_bit, &max_bit);
1271
1272 /* Set up the msix entry table */
1273 bfad_init_msix_entry(bfad, msix_entries, mask, max_bit);
1274
1275 if ((bfa_asic_id_ct(pdev->device) && !msix_disable_ct) ||
1276 (!bfa_asic_id_ct(pdev->device) && !msix_disable_cb)) {
1277
1278 error = pci_enable_msix(bfad->pcidev, msix_entries, bfad->nvec);
1279 if (error) {
1280 /*
1281 * Only error number of vector is available.
1282 * We don't have a mechanism to map multiple
1283 * interrupts into one vector, so even if we
1284 * can try to request less vectors, we don't
1285 * know how to associate interrupt events to
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001286 * vectors. Linux doesn't duplicate vectors
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001287 * in the MSIX table for this case.
1288 */
1289
1290 printk(KERN_WARNING "bfad%d: "
1291 "pci_enable_msix failed (%d),"
1292 " use line based.\n", bfad->inst_no, error);
1293
1294 goto line_based;
1295 }
1296
1297 /* Save the vectors */
1298 for (i = 0; i < bfad->nvec; i++) {
1299 bfa_trc(bfad, msix_entries[i].vector);
1300 bfad->msix_tab[i].msix.vector = msix_entries[i].vector;
1301 }
1302
1303 bfa_msix_init(&bfad->bfa, bfad->nvec);
1304
1305 bfad->bfad_flags |= BFAD_MSIX_ON;
1306
1307 return error;
1308 }
1309
1310line_based:
1311 error = 0;
1312 if (request_irq
1313 (bfad->pcidev->irq, (irq_handler_t) bfad_intx, BFAD_IRQ_FLAGS,
1314 BFAD_DRIVER_NAME, bfad) != 0) {
1315 /* Enable interrupt handler failed */
1316 return 1;
1317 }
1318
1319 return error;
1320}
1321
1322void
1323bfad_remove_intr(struct bfad_s *bfad)
1324{
1325 int i;
1326
1327 if (bfad->bfad_flags & BFAD_MSIX_ON) {
1328 for (i = 0; i < bfad->nvec; i++)
1329 free_irq(bfad->msix_tab[i].msix.vector,
1330 &bfad->msix_tab[i]);
1331
1332 pci_disable_msix(bfad->pcidev);
1333 bfad->bfad_flags &= ~BFAD_MSIX_ON;
1334 } else {
1335 free_irq(bfad->pcidev->irq, bfad);
1336 }
1337}
Jing Huang7725ccf2009-09-23 17:46:15 -07001338
Jing Huang5fbe25c2010-10-18 17:17:23 -07001339/*
Jing Huang7725ccf2009-09-23 17:46:15 -07001340 * PCI probe entry.
1341 */
1342int
1343bfad_pci_probe(struct pci_dev *pdev, const struct pci_device_id *pid)
1344{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001345 struct bfad_s *bfad;
1346 int error = -ENODEV, retval;
Jing Huang7725ccf2009-09-23 17:46:15 -07001347
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001348 /* For single port cards - only claim function 0 */
1349 if ((pdev->device == BFA_PCI_DEVICE_ID_FC_8G1P) &&
1350 (PCI_FUNC(pdev->devfn) != 0))
Jing Huang7725ccf2009-09-23 17:46:15 -07001351 return -ENODEV;
1352
Jing Huang7725ccf2009-09-23 17:46:15 -07001353 bfad = kzalloc(sizeof(struct bfad_s), GFP_KERNEL);
1354 if (!bfad) {
1355 error = -ENOMEM;
1356 goto out;
1357 }
1358
1359 bfad->trcmod = kzalloc(sizeof(struct bfa_trc_mod_s), GFP_KERNEL);
1360 if (!bfad->trcmod) {
1361 printk(KERN_WARNING "Error alloc trace buffer!\n");
1362 error = -ENOMEM;
1363 goto out_alloc_trace_failure;
1364 }
1365
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001366 /* TRACE INIT */
Jing Huang7725ccf2009-09-23 17:46:15 -07001367 bfa_trc_init(bfad->trcmod);
1368 bfa_trc(bfad, bfad_inst);
1369
Jing Huang7725ccf2009-09-23 17:46:15 -07001370 if (!(bfad_load_fwimg(pdev))) {
Jing Huang7725ccf2009-09-23 17:46:15 -07001371 kfree(bfad->trcmod);
1372 goto out_alloc_trace_failure;
1373 }
1374
1375 retval = bfad_pci_init(pdev, bfad);
1376 if (retval) {
1377 printk(KERN_WARNING "bfad_pci_init failure!\n");
1378 error = retval;
1379 goto out_pci_init_failure;
1380 }
1381
1382 mutex_lock(&bfad_mutex);
1383 bfad->inst_no = bfad_inst++;
1384 list_add_tail(&bfad->list_entry, &bfad_list);
1385 mutex_unlock(&bfad_mutex);
1386
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001387 /* Initializing the state machine: State set to uninit */
1388 bfa_sm_set_state(bfad, bfad_sm_uninit);
1389
Jing Huang7725ccf2009-09-23 17:46:15 -07001390 spin_lock_init(&bfad->bfad_lock);
1391 pci_set_drvdata(pdev, bfad);
1392
1393 bfad->ref_count = 0;
1394 bfad->pport.bfad = bfad;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001395 INIT_LIST_HEAD(&bfad->pbc_vport_list);
Krishna Gudipatie6714322010-03-03 17:44:02 -08001396
Krishna Gudipati7c38c052011-04-14 16:50:35 -07001397 /* Setup the debugfs node for this bfad */
1398 if (bfa_debugfs_enable)
1399 bfad_debugfs_init(&bfad->pport);
1400
Jing Huang7725ccf2009-09-23 17:46:15 -07001401 retval = bfad_drv_init(bfad);
1402 if (retval != BFA_STATUS_OK)
1403 goto out_drv_init_failure;
Jing Huang7725ccf2009-09-23 17:46:15 -07001404
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001405 bfa_sm_send_event(bfad, BFAD_E_CREATE);
Jing Huang7725ccf2009-09-23 17:46:15 -07001406
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001407 if (bfa_sm_cmp_state(bfad, bfad_sm_uninit))
1408 goto out_bfad_sm_failure;
Jing Huang7725ccf2009-09-23 17:46:15 -07001409
Jing Huang7725ccf2009-09-23 17:46:15 -07001410 return 0;
1411
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001412out_bfad_sm_failure:
1413 bfa_detach(&bfad->bfa);
1414 bfad_hal_mem_release(bfad);
Jing Huang7725ccf2009-09-23 17:46:15 -07001415out_drv_init_failure:
Krishna Gudipati7c38c052011-04-14 16:50:35 -07001416 /* Remove the debugfs node for this bfad */
1417 kfree(bfad->regdata);
1418 bfad_debugfs_exit(&bfad->pport);
Jing Huang7725ccf2009-09-23 17:46:15 -07001419 mutex_lock(&bfad_mutex);
1420 bfad_inst--;
1421 list_del(&bfad->list_entry);
1422 mutex_unlock(&bfad_mutex);
1423 bfad_pci_uninit(pdev, bfad);
1424out_pci_init_failure:
1425 kfree(bfad->trcmod);
1426out_alloc_trace_failure:
1427 kfree(bfad);
1428out:
1429 return error;
1430}
1431
Jing Huang5fbe25c2010-10-18 17:17:23 -07001432/*
Jing Huang7725ccf2009-09-23 17:46:15 -07001433 * PCI remove entry.
1434 */
1435void
1436bfad_pci_remove(struct pci_dev *pdev)
1437{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001438 struct bfad_s *bfad = pci_get_drvdata(pdev);
1439 unsigned long flags;
Jing Huang7725ccf2009-09-23 17:46:15 -07001440
1441 bfa_trc(bfad, bfad->inst_no);
1442
Krishna Gudipatie6714322010-03-03 17:44:02 -08001443 spin_lock_irqsave(&bfad->bfad_lock, flags);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001444 if (bfad->bfad_tsk != NULL) {
1445 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
Krishna Gudipatie6714322010-03-03 17:44:02 -08001446 kthread_stop(bfad->bfad_tsk);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001447 } else {
Jing Huang7725ccf2009-09-23 17:46:15 -07001448 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
Jing Huang7725ccf2009-09-23 17:46:15 -07001449 }
1450
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001451 /* Send Event BFAD_E_STOP */
1452 bfa_sm_send_event(bfad, BFAD_E_STOP);
Jing Huang7725ccf2009-09-23 17:46:15 -07001453
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001454 /* Driver detach and dealloc mem */
Jing Huang7725ccf2009-09-23 17:46:15 -07001455 spin_lock_irqsave(&bfad->bfad_lock, flags);
1456 bfa_detach(&bfad->bfa);
1457 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1458 bfad_hal_mem_release(bfad);
Jing Huang7725ccf2009-09-23 17:46:15 -07001459
Krishna Gudipati7c38c052011-04-14 16:50:35 -07001460 /* Remove the debugfs node for this bfad */
1461 kfree(bfad->regdata);
1462 bfad_debugfs_exit(&bfad->pport);
1463
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001464 /* Cleaning the BFAD instance */
Jing Huang7725ccf2009-09-23 17:46:15 -07001465 mutex_lock(&bfad_mutex);
1466 bfad_inst--;
1467 list_del(&bfad->list_entry);
1468 mutex_unlock(&bfad_mutex);
1469 bfad_pci_uninit(pdev, bfad);
1470
1471 kfree(bfad->trcmod);
1472 kfree(bfad);
1473}
1474
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001475struct pci_device_id bfad_id_table[] = {
Jing Huang7725ccf2009-09-23 17:46:15 -07001476 {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001477 .vendor = BFA_PCI_VENDOR_ID_BROCADE,
1478 .device = BFA_PCI_DEVICE_ID_FC_8G2P,
1479 .subvendor = PCI_ANY_ID,
1480 .subdevice = PCI_ANY_ID,
1481 },
Jing Huang7725ccf2009-09-23 17:46:15 -07001482 {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001483 .vendor = BFA_PCI_VENDOR_ID_BROCADE,
1484 .device = BFA_PCI_DEVICE_ID_FC_8G1P,
1485 .subvendor = PCI_ANY_ID,
1486 .subdevice = PCI_ANY_ID,
1487 },
Jing Huang7725ccf2009-09-23 17:46:15 -07001488 {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001489 .vendor = BFA_PCI_VENDOR_ID_BROCADE,
1490 .device = BFA_PCI_DEVICE_ID_CT,
1491 .subvendor = PCI_ANY_ID,
1492 .subdevice = PCI_ANY_ID,
1493 .class = (PCI_CLASS_SERIAL_FIBER << 8),
1494 .class_mask = ~0,
1495 },
Jing Huang293f82d2010-07-08 19:45:20 -07001496 {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001497 .vendor = BFA_PCI_VENDOR_ID_BROCADE,
1498 .device = BFA_PCI_DEVICE_ID_CT_FC,
1499 .subvendor = PCI_ANY_ID,
1500 .subdevice = PCI_ANY_ID,
1501 .class = (PCI_CLASS_SERIAL_FIBER << 8),
1502 .class_mask = ~0,
Jing Huang293f82d2010-07-08 19:45:20 -07001503 },
Jing Huang7725ccf2009-09-23 17:46:15 -07001504
1505 {0, 0},
1506};
1507
1508MODULE_DEVICE_TABLE(pci, bfad_id_table);
1509
1510static struct pci_driver bfad_pci_driver = {
1511 .name = BFAD_DRIVER_NAME,
1512 .id_table = bfad_id_table,
1513 .probe = bfad_pci_probe,
1514 .remove = __devexit_p(bfad_pci_remove),
1515};
1516
Jing Huang5fbe25c2010-10-18 17:17:23 -07001517/*
Jing Huang7725ccf2009-09-23 17:46:15 -07001518 * Driver module init.
1519 */
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001520static int __init
Jing Huang7725ccf2009-09-23 17:46:15 -07001521bfad_init(void)
1522{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001523 int error = 0;
Jing Huang7725ccf2009-09-23 17:46:15 -07001524
1525 printk(KERN_INFO "Brocade BFA FC/FCOE SCSI driver - version: %s\n",
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001526 BFAD_DRIVER_VERSION);
Jing Huang7725ccf2009-09-23 17:46:15 -07001527
1528 if (num_sgpgs > 0)
1529 num_sgpgs_parm = num_sgpgs;
1530
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001531 error = bfad_im_module_init();
Jing Huang7725ccf2009-09-23 17:46:15 -07001532 if (error) {
1533 error = -ENOMEM;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001534 printk(KERN_WARNING "bfad_im_module_init failure\n");
Jing Huang7725ccf2009-09-23 17:46:15 -07001535 goto ext;
1536 }
1537
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001538 if (strcmp(FCPI_NAME, " fcpim") == 0)
1539 supported_fc4s |= BFA_LPORT_ROLE_FCP_IM;
Jing Huang7725ccf2009-09-23 17:46:15 -07001540
Maggie Zhangf7f738122010-12-09 19:08:43 -08001541 bfa_auto_recover = ioc_auto_recover;
Jing Huang7725ccf2009-09-23 17:46:15 -07001542 bfa_fcs_rport_set_del_timeout(rport_del_timeout);
Jing Huang7725ccf2009-09-23 17:46:15 -07001543
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001544 error = pci_register_driver(&bfad_pci_driver);
Jing Huang7725ccf2009-09-23 17:46:15 -07001545 if (error) {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001546 printk(KERN_WARNING "pci_register_driver failure\n");
Jing Huang7725ccf2009-09-23 17:46:15 -07001547 goto ext;
1548 }
1549
1550 return 0;
1551
1552ext:
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001553 bfad_im_module_exit();
Jing Huang7725ccf2009-09-23 17:46:15 -07001554 return error;
1555}
1556
Jing Huang5fbe25c2010-10-18 17:17:23 -07001557/*
Jing Huang7725ccf2009-09-23 17:46:15 -07001558 * Driver module exit.
1559 */
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001560static void __exit
Jing Huang7725ccf2009-09-23 17:46:15 -07001561bfad_exit(void)
1562{
1563 pci_unregister_driver(&bfad_pci_driver);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001564 bfad_im_module_exit();
Jing Huang7725ccf2009-09-23 17:46:15 -07001565 bfad_free_fwimg();
1566}
1567
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001568/* Firmware handling */
Jing Huang61338a02011-04-13 11:44:03 -07001569static void
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001570bfad_read_firmware(struct pci_dev *pdev, u32 **bfi_image,
1571 u32 *bfi_image_size, char *fw_name)
1572{
1573 const struct firmware *fw;
1574
1575 if (request_firmware(&fw, fw_name, &pdev->dev)) {
1576 printk(KERN_ALERT "Can't locate firmware %s\n", fw_name);
Jing Huang61338a02011-04-13 11:44:03 -07001577 *bfi_image = NULL;
1578 goto out;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001579 }
1580
1581 *bfi_image = vmalloc(fw->size);
1582 if (NULL == *bfi_image) {
1583 printk(KERN_ALERT "Fail to allocate buffer for fw image "
1584 "size=%x!\n", (u32) fw->size);
Jing Huang61338a02011-04-13 11:44:03 -07001585 goto out;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001586 }
1587
1588 memcpy(*bfi_image, fw->data, fw->size);
1589 *bfi_image_size = fw->size/sizeof(u32);
Jing Huang61338a02011-04-13 11:44:03 -07001590out:
1591 release_firmware(fw);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001592}
1593
Jing Huang61338a02011-04-13 11:44:03 -07001594static u32 *
1595bfad_load_fwimg(struct pci_dev *pdev)
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001596{
1597 if (pdev->device == BFA_PCI_DEVICE_ID_CT_FC) {
1598 if (bfi_image_ct_fc_size == 0)
1599 bfad_read_firmware(pdev, &bfi_image_ct_fc,
1600 &bfi_image_ct_fc_size, BFAD_FW_FILE_CT_FC);
1601 return bfi_image_ct_fc;
1602 } else if (pdev->device == BFA_PCI_DEVICE_ID_CT) {
1603 if (bfi_image_ct_cna_size == 0)
1604 bfad_read_firmware(pdev, &bfi_image_ct_cna,
1605 &bfi_image_ct_cna_size, BFAD_FW_FILE_CT_CNA);
1606 return bfi_image_ct_cna;
1607 } else {
1608 if (bfi_image_cb_fc_size == 0)
1609 bfad_read_firmware(pdev, &bfi_image_cb_fc,
1610 &bfi_image_cb_fc_size, BFAD_FW_FILE_CB_FC);
1611 return bfi_image_cb_fc;
1612 }
1613}
Jing Huang7725ccf2009-09-23 17:46:15 -07001614
Jing Huang61338a02011-04-13 11:44:03 -07001615static void
1616bfad_free_fwimg(void)
1617{
1618 if (bfi_image_ct_fc_size && bfi_image_ct_fc)
1619 vfree(bfi_image_ct_fc);
1620 if (bfi_image_ct_cna_size && bfi_image_ct_cna)
1621 vfree(bfi_image_ct_cna);
1622 if (bfi_image_cb_fc_size && bfi_image_cb_fc)
1623 vfree(bfi_image_cb_fc);
1624}
1625
Jing Huang7725ccf2009-09-23 17:46:15 -07001626module_init(bfad_init);
1627module_exit(bfad_exit);
1628MODULE_LICENSE("GPL");
1629MODULE_DESCRIPTION("Brocade Fibre Channel HBA Driver" BFAD_PROTO_NAME);
1630MODULE_AUTHOR("Brocade Communications Systems, Inc.");
1631MODULE_VERSION(BFAD_DRIVER_VERSION);