blob: 8ddae0d85fa8320081e2ef6f07d198380cc00d0d [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 Gudipati11189202011-06-13 15:50:35 -070061u32 bfi_image_cb_size, bfi_image_ct_size, bfi_image_ct2_size;
62u32 *bfi_image_cb, *bfi_image_ct, *bfi_image_ct2;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -070063
Krishna Gudipati11189202011-06-13 15:50:35 -070064#define BFAD_FW_FILE_CB "cbfw.bin"
65#define BFAD_FW_FILE_CT "ctfw.bin"
66#define BFAD_FW_FILE_CT2 "ct2fw.bin"
Jing Huang61338a02011-04-13 11:44:03 -070067
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 Gudipati11189202011-06-13 15:50:35 -070074 "ctrl",
Krishna Gudipatia36c61f2010-09-15 11:50:55 -070075 "cpe0", "cpe1", "cpe2", "cpe3",
Krishna Gudipati11189202011-06-13 15:50:35 -070076 "rme0", "rme1", "rme2", "rme3" };
Krishna Gudipatia36c61f2010-09-15 11:50:55 -070077
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
Krishna Gudipati11189202011-06-13 15:50:35 -070083MODULE_FIRMWARE(BFAD_FW_FILE_CB);
84MODULE_FIRMWARE(BFAD_FW_FILE_CT);
85MODULE_FIRMWARE(BFAD_FW_FILE_CT2);
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 Zhangf7f73812010-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 Zhangf7f73812010-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));
Krishna Gudipati11189202011-06-13 15:50:35 -0700790 bfad->pci_bar2_kva = pci_iomap(pdev, 2, pci_resource_len(pdev, 2));
Jing Huang7725ccf2009-09-23 17:46:15 -0700791
792 if (bfad->pci_bar0_kva == NULL) {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700793 printk(KERN_ERR "Fail to map bar0\n");
Jing Huang7725ccf2009-09-23 17:46:15 -0700794 goto out_release_region;
795 }
796
797 bfad->hal_pcidev.pci_slot = PCI_SLOT(pdev->devfn);
798 bfad->hal_pcidev.pci_func = PCI_FUNC(pdev->devfn);
799 bfad->hal_pcidev.pci_bar_kva = bfad->pci_bar0_kva;
800 bfad->hal_pcidev.device_id = pdev->device;
801 bfad->pci_name = pci_name(pdev);
802
803 bfad->pci_attr.vendor_id = pdev->vendor;
804 bfad->pci_attr.device_id = pdev->device;
805 bfad->pci_attr.ssid = pdev->subsystem_device;
806 bfad->pci_attr.ssvid = pdev->subsystem_vendor;
807 bfad->pci_attr.pcifn = PCI_FUNC(pdev->devfn);
808
809 bfad->pcidev = pdev;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700810
811 /* Adjust PCIe Maximum Read Request Size */
812 if (pcie_max_read_reqsz > 0) {
813 int pcie_cap_reg;
814 u16 pcie_dev_ctl;
815 u16 mask = 0xffff;
816
817 switch (pcie_max_read_reqsz) {
818 case 128:
819 mask = 0x0;
820 break;
821 case 256:
822 mask = 0x1000;
823 break;
824 case 512:
825 mask = 0x2000;
826 break;
827 case 1024:
828 mask = 0x3000;
829 break;
830 case 2048:
831 mask = 0x4000;
832 break;
833 case 4096:
834 mask = 0x5000;
835 break;
836 default:
837 break;
838 }
839
840 pcie_cap_reg = pci_find_capability(pdev, PCI_CAP_ID_EXP);
841 if (mask != 0xffff && pcie_cap_reg) {
842 pcie_cap_reg += 0x08;
843 pci_read_config_word(pdev, pcie_cap_reg, &pcie_dev_ctl);
844 if ((pcie_dev_ctl & 0x7000) != mask) {
845 printk(KERN_WARNING "BFA[%s]: "
846 "pcie_max_read_request_size is %d, "
847 "reset to %d\n", bfad->pci_name,
848 (1 << ((pcie_dev_ctl & 0x7000) >> 12)) << 7,
849 pcie_max_read_reqsz);
850
851 pcie_dev_ctl &= ~0x7000;
852 pci_write_config_word(pdev, pcie_cap_reg,
853 pcie_dev_ctl | mask);
854 }
855 }
856 }
857
Jing Huang7725ccf2009-09-23 17:46:15 -0700858 return 0;
859
860out_release_region:
861 pci_release_regions(pdev);
862out_disable_device:
863 pci_disable_device(pdev);
864out:
865 return rc;
866}
867
868void
869bfad_pci_uninit(struct pci_dev *pdev, struct bfad_s *bfad)
870{
Jing Huang7725ccf2009-09-23 17:46:15 -0700871 pci_iounmap(pdev, bfad->pci_bar0_kva);
Krishna Gudipati11189202011-06-13 15:50:35 -0700872 pci_iounmap(pdev, bfad->pci_bar2_kva);
Jing Huang7725ccf2009-09-23 17:46:15 -0700873 pci_release_regions(pdev);
874 pci_disable_device(pdev);
875 pci_set_drvdata(pdev, NULL);
876}
877
Jing Huang7725ccf2009-09-23 17:46:15 -0700878bfa_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
Maggie Zhangf7f73812010-12-09 19:08:43 -0800899 bfad->bfa.trcmod = bfad->trcmod;
900 bfad->bfa.plog = &bfad->plog_buf;
Jing Huang7725ccf2009-09-23 17:46:15 -0700901 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);
Maggie Zhangf7f73812010-12-09 19:08:43 -0800910 bfad->bfa_fcs.trcmod = bfad->trcmod;
Krishna Gudipati82794a22010-03-03 17:43:30 -0800911 bfa_fcs_attach(&bfad->bfa_fcs, &bfad->bfa, bfad, BFA_FALSE);
Maggie Zhangf7f73812010-12-09 19:08:43 -0800912 bfad->bfa_fcs.fdmi_enabled = 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);
Maggie Zhangf7f73812010-12-09 19:08:43 -0800930 bfa_iocfc_stop(&bfad->bfa);
Krishna Gudipatie6714322010-03-03 17:44:02 -0800931 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);
Maggie Zhangf7f73812010-12-09 19:08:43 -0800949 bfa_iocfc_start(&bfad->bfa);
950 bfa_fcs_fabric_modstart(&bfad->bfa_fcs);
Jing Huang7725ccf2009-09-23 17:46:15 -0700951 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);
Maggie Zhangf7f73812010-12-09 19:08:43 -0800980 bfa_iocfc_stop(&bfad->bfa);
Jing Huang7725ccf2009-09-23 17:46:15 -0700981 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
1009 bfad->bfad_flags |= BFAD_CFG_PPORT_DONE;
1010
1011out:
1012 return rc;
1013}
1014
1015void
1016bfad_uncfg_pport(struct bfad_s *bfad)
1017{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001018 if ((supported_fc4s & BFA_LPORT_ROLE_FCP_IM) &&
1019 (bfad->pport.roles & BFA_LPORT_ROLE_FCP_IM)) {
Jing Huang7725ccf2009-09-23 17:46:15 -07001020 bfad_im_scsi_host_free(bfad, bfad->pport.im_port);
1021 bfad_im_port_clean(bfad->pport.im_port);
1022 kfree(bfad->pport.im_port);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001023 bfad->pport.roles &= ~BFA_LPORT_ROLE_FCP_IM;
Jing Huang7725ccf2009-09-23 17:46:15 -07001024 }
1025
1026 bfad->bfad_flags &= ~BFAD_CFG_PPORT_DONE;
1027}
1028
Krishna Gudipatie6714322010-03-03 17:44:02 -08001029bfa_status_t
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001030bfad_start_ops(struct bfad_s *bfad) {
1031
1032 int retval;
1033 unsigned long flags;
1034 struct bfad_vport_s *vport, *vport_new;
1035 struct bfa_fcs_driver_info_s driver_info;
1036
1037 /* Fill the driver_info info to fcs*/
1038 memset(&driver_info, 0, sizeof(driver_info));
1039 strncpy(driver_info.version, BFAD_DRIVER_VERSION,
1040 sizeof(driver_info.version) - 1);
1041 if (host_name)
1042 strncpy(driver_info.host_machine_name, host_name,
1043 sizeof(driver_info.host_machine_name) - 1);
1044 if (os_name)
1045 strncpy(driver_info.host_os_name, os_name,
1046 sizeof(driver_info.host_os_name) - 1);
1047 if (os_patch)
1048 strncpy(driver_info.host_os_patch, os_patch,
1049 sizeof(driver_info.host_os_patch) - 1);
1050
1051 strncpy(driver_info.os_device_name, bfad->pci_name,
1052 sizeof(driver_info.os_device_name - 1));
1053
1054 /* FCS INIT */
1055 spin_lock_irqsave(&bfad->bfad_lock, flags);
1056 bfa_fcs_driver_info_init(&bfad->bfa_fcs, &driver_info);
1057 bfa_fcs_init(&bfad->bfa_fcs);
1058 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
Krishna Gudipatie6714322010-03-03 17:44:02 -08001059
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001060 retval = bfad_cfg_pport(bfad, BFA_LPORT_ROLE_FCP_IM);
Krishna Gudipatie6714322010-03-03 17:44:02 -08001061 if (retval != BFA_STATUS_OK) {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001062 if (bfa_sm_cmp_state(bfad, bfad_sm_initializing))
1063 bfa_sm_set_state(bfad, bfad_sm_failed);
1064 bfad_stop(bfad);
1065 return BFA_STATUS_FAILED;
Krishna Gudipatie6714322010-03-03 17:44:02 -08001066 }
1067
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001068 /* BFAD level FC4 IM specific resource allocation */
1069 retval = bfad_im_probe(bfad);
1070 if (retval != BFA_STATUS_OK) {
1071 printk(KERN_WARNING "bfad_im_probe failed\n");
1072 if (bfa_sm_cmp_state(bfad, bfad_sm_initializing))
1073 bfa_sm_set_state(bfad, bfad_sm_failed);
1074 bfad_im_probe_undo(bfad);
1075 bfad->bfad_flags &= ~BFAD_FC4_PROBE_DONE;
1076 bfad_uncfg_pport(bfad);
1077 bfad_stop(bfad);
1078 return BFA_STATUS_FAILED;
1079 } else
1080 bfad->bfad_flags |= BFAD_FC4_PROBE_DONE;
1081
Krishna Gudipatie6714322010-03-03 17:44:02 -08001082 bfad_drv_start(bfad);
1083
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001084 /* Complete pbc vport create */
1085 list_for_each_entry_safe(vport, vport_new, &bfad->pbc_vport_list,
1086 list_entry) {
Jing Huangd9883542010-07-08 19:46:26 -07001087 struct fc_vport_identifiers vid;
1088 struct fc_vport *fc_vport;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001089 char pwwn_buf[BFA_STRING_32];
Jing Huangd9883542010-07-08 19:46:26 -07001090
1091 memset(&vid, 0, sizeof(vid));
1092 vid.roles = FC_PORT_ROLE_FCP_INITIATOR;
1093 vid.vport_type = FC_PORTTYPE_NPIV;
1094 vid.disable = false;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001095 vid.node_name = wwn_to_u64((u8 *)
1096 (&((vport->fcs_vport).lport.port_cfg.nwwn)));
1097 vid.port_name = wwn_to_u64((u8 *)
1098 (&((vport->fcs_vport).lport.port_cfg.pwwn)));
Jing Huangd9883542010-07-08 19:46:26 -07001099 fc_vport = fc_vport_create(bfad->pport.im_port->shost, 0, &vid);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001100 if (!fc_vport) {
1101 wwn2str(pwwn_buf, vid.port_name);
Jing Huangd9883542010-07-08 19:46:26 -07001102 printk(KERN_WARNING "bfad%d: failed to create pbc vport"
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001103 " %s\n", bfad->inst_no, pwwn_buf);
1104 }
1105 list_del(&vport->list_entry);
1106 kfree(vport);
Jing Huangd9883542010-07-08 19:46:26 -07001107 }
1108
Krishna Gudipatie6714322010-03-03 17:44:02 -08001109 /*
1110 * If bfa_linkup_delay is set to -1 default; try to retrive the
Maggie Zhangf16a1752010-12-09 19:12:32 -08001111 * value using the bfad_get_linkup_delay(); else use the
Krishna Gudipatie6714322010-03-03 17:44:02 -08001112 * passed in module param value as the bfa_linkup_delay.
1113 */
1114 if (bfa_linkup_delay < 0) {
Maggie Zhangf16a1752010-12-09 19:12:32 -08001115 bfa_linkup_delay = bfad_get_linkup_delay(bfad);
1116 bfad_rport_online_wait(bfad);
Krishna Gudipatie6714322010-03-03 17:44:02 -08001117 bfa_linkup_delay = -1;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001118 } else
Maggie Zhangf16a1752010-12-09 19:12:32 -08001119 bfad_rport_online_wait(bfad);
Krishna Gudipatie6714322010-03-03 17:44:02 -08001120
Jing Huang88166242010-12-09 17:11:53 -08001121 BFA_LOG(KERN_INFO, bfad, bfa_log_level, "bfa device claimed\n");
Krishna Gudipatie6714322010-03-03 17:44:02 -08001122
1123 return BFA_STATUS_OK;
Krishna Gudipatie6714322010-03-03 17:44:02 -08001124}
1125
1126int
Jing Huangd9883542010-07-08 19:46:26 -07001127bfad_worker(void *ptr)
Krishna Gudipatie6714322010-03-03 17:44:02 -08001128{
1129 struct bfad_s *bfad;
1130 unsigned long flags;
1131
1132 bfad = (struct bfad_s *)ptr;
1133
1134 while (!kthread_should_stop()) {
1135
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001136 /* Send event BFAD_E_INIT_SUCCESS */
1137 bfa_sm_send_event(bfad, BFAD_E_INIT_SUCCESS);
Krishna Gudipatie6714322010-03-03 17:44:02 -08001138
1139 spin_lock_irqsave(&bfad->bfad_lock, flags);
1140 bfad->bfad_tsk = NULL;
1141 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1142
1143 break;
1144 }
1145
1146 return 0;
1147}
1148
Jing Huang5fbe25c2010-10-18 17:17:23 -07001149/*
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001150 * BFA driver interrupt functions
1151 */
1152irqreturn_t
1153bfad_intx(int irq, void *dev_id)
1154{
1155 struct bfad_s *bfad = dev_id;
1156 struct list_head doneq;
1157 unsigned long flags;
1158 bfa_boolean_t rc;
1159
1160 spin_lock_irqsave(&bfad->bfad_lock, flags);
1161 rc = bfa_intx(&bfad->bfa);
1162 if (!rc) {
1163 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1164 return IRQ_NONE;
1165 }
1166
1167 bfa_comp_deq(&bfad->bfa, &doneq);
1168 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1169
1170 if (!list_empty(&doneq)) {
1171 bfa_comp_process(&bfad->bfa, &doneq);
1172
1173 spin_lock_irqsave(&bfad->bfad_lock, flags);
1174 bfa_comp_free(&bfad->bfa, &doneq);
1175 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001176 }
1177
1178 return IRQ_HANDLED;
1179
1180}
1181
1182static irqreturn_t
1183bfad_msix(int irq, void *dev_id)
1184{
1185 struct bfad_msix_s *vec = dev_id;
1186 struct bfad_s *bfad = vec->bfad;
1187 struct list_head doneq;
1188 unsigned long flags;
1189
1190 spin_lock_irqsave(&bfad->bfad_lock, flags);
1191
1192 bfa_msix(&bfad->bfa, vec->msix.entry);
1193 bfa_comp_deq(&bfad->bfa, &doneq);
1194 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1195
1196 if (!list_empty(&doneq)) {
1197 bfa_comp_process(&bfad->bfa, &doneq);
1198
1199 spin_lock_irqsave(&bfad->bfad_lock, flags);
1200 bfa_comp_free(&bfad->bfa, &doneq);
1201 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1202 }
1203
1204 return IRQ_HANDLED;
1205}
1206
Jing Huang5fbe25c2010-10-18 17:17:23 -07001207/*
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001208 * Initialize the MSIX entry table.
1209 */
1210static void
1211bfad_init_msix_entry(struct bfad_s *bfad, struct msix_entry *msix_entries,
1212 int mask, int max_bit)
1213{
1214 int i;
1215 int match = 0x00000001;
1216
1217 for (i = 0, bfad->nvec = 0; i < MAX_MSIX_ENTRY; i++) {
1218 if (mask & match) {
1219 bfad->msix_tab[bfad->nvec].msix.entry = i;
1220 bfad->msix_tab[bfad->nvec].bfad = bfad;
1221 msix_entries[bfad->nvec].entry = i;
1222 bfad->nvec++;
1223 }
1224
1225 match <<= 1;
1226 }
1227
1228}
1229
1230int
1231bfad_install_msix_handler(struct bfad_s *bfad)
1232{
1233 int i, error = 0;
1234
1235 for (i = 0; i < bfad->nvec; i++) {
1236 sprintf(bfad->msix_tab[i].name, "bfa-%s-%s",
1237 bfad->pci_name,
Krishna Gudipati11189202011-06-13 15:50:35 -07001238 ((bfa_asic_id_cb(bfad->hal_pcidev.device_id)) ?
1239 msix_name_cb[i] : msix_name_ct[i]));
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001240
1241 error = request_irq(bfad->msix_tab[i].msix.vector,
1242 (irq_handler_t) bfad_msix, 0,
1243 bfad->msix_tab[i].name, &bfad->msix_tab[i]);
1244 bfa_trc(bfad, i);
1245 bfa_trc(bfad, bfad->msix_tab[i].msix.vector);
1246 if (error) {
1247 int j;
1248
1249 for (j = 0; j < i; j++)
1250 free_irq(bfad->msix_tab[j].msix.vector,
1251 &bfad->msix_tab[j]);
1252
1253 return 1;
1254 }
1255 }
1256
1257 return 0;
1258}
1259
Jing Huang5fbe25c2010-10-18 17:17:23 -07001260/*
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001261 * Setup MSIX based interrupt.
1262 */
1263int
1264bfad_setup_intr(struct bfad_s *bfad)
1265{
1266 int error = 0;
1267 u32 mask = 0, i, num_bit = 0, max_bit = 0;
1268 struct msix_entry msix_entries[MAX_MSIX_ENTRY];
1269 struct pci_dev *pdev = bfad->pcidev;
1270
1271 /* Call BFA to get the msix map for this PCI function. */
1272 bfa_msix_getvecs(&bfad->bfa, &mask, &num_bit, &max_bit);
1273
1274 /* Set up the msix entry table */
1275 bfad_init_msix_entry(bfad, msix_entries, mask, max_bit);
1276
Krishna Gudipati11189202011-06-13 15:50:35 -07001277 if ((bfa_asic_id_ctc(pdev->device) && !msix_disable_ct) ||
1278 (bfa_asic_id_cb(pdev->device) && !msix_disable_cb)) {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001279
1280 error = pci_enable_msix(bfad->pcidev, msix_entries, bfad->nvec);
1281 if (error) {
1282 /*
1283 * Only error number of vector is available.
1284 * We don't have a mechanism to map multiple
1285 * interrupts into one vector, so even if we
1286 * can try to request less vectors, we don't
1287 * know how to associate interrupt events to
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001288 * vectors. Linux doesn't duplicate vectors
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001289 * in the MSIX table for this case.
1290 */
1291
1292 printk(KERN_WARNING "bfad%d: "
1293 "pci_enable_msix failed (%d),"
1294 " use line based.\n", bfad->inst_no, error);
1295
1296 goto line_based;
1297 }
1298
1299 /* Save the vectors */
1300 for (i = 0; i < bfad->nvec; i++) {
1301 bfa_trc(bfad, msix_entries[i].vector);
1302 bfad->msix_tab[i].msix.vector = msix_entries[i].vector;
1303 }
1304
1305 bfa_msix_init(&bfad->bfa, bfad->nvec);
1306
1307 bfad->bfad_flags |= BFAD_MSIX_ON;
1308
1309 return error;
1310 }
1311
1312line_based:
1313 error = 0;
1314 if (request_irq
1315 (bfad->pcidev->irq, (irq_handler_t) bfad_intx, BFAD_IRQ_FLAGS,
1316 BFAD_DRIVER_NAME, bfad) != 0) {
1317 /* Enable interrupt handler failed */
1318 return 1;
1319 }
1320
1321 return error;
1322}
1323
1324void
1325bfad_remove_intr(struct bfad_s *bfad)
1326{
1327 int i;
1328
1329 if (bfad->bfad_flags & BFAD_MSIX_ON) {
1330 for (i = 0; i < bfad->nvec; i++)
1331 free_irq(bfad->msix_tab[i].msix.vector,
1332 &bfad->msix_tab[i]);
1333
1334 pci_disable_msix(bfad->pcidev);
1335 bfad->bfad_flags &= ~BFAD_MSIX_ON;
1336 } else {
1337 free_irq(bfad->pcidev->irq, bfad);
1338 }
1339}
Jing Huang7725ccf2009-09-23 17:46:15 -07001340
Jing Huang5fbe25c2010-10-18 17:17:23 -07001341/*
Jing Huang7725ccf2009-09-23 17:46:15 -07001342 * PCI probe entry.
1343 */
1344int
1345bfad_pci_probe(struct pci_dev *pdev, const struct pci_device_id *pid)
1346{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001347 struct bfad_s *bfad;
1348 int error = -ENODEV, retval;
Jing Huang7725ccf2009-09-23 17:46:15 -07001349
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001350 /* For single port cards - only claim function 0 */
Krishna Gudipati11189202011-06-13 15:50:35 -07001351 if ((pdev->device == BFA_PCI_DEVICE_ID_FC_8G1P ||
1352 pdev->device == BFA_PCI_DEVICE_ID_CT2) &&
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001353 (PCI_FUNC(pdev->devfn) != 0))
Jing Huang7725ccf2009-09-23 17:46:15 -07001354 return -ENODEV;
1355
Jing Huang7725ccf2009-09-23 17:46:15 -07001356 bfad = kzalloc(sizeof(struct bfad_s), GFP_KERNEL);
1357 if (!bfad) {
1358 error = -ENOMEM;
1359 goto out;
1360 }
1361
1362 bfad->trcmod = kzalloc(sizeof(struct bfa_trc_mod_s), GFP_KERNEL);
1363 if (!bfad->trcmod) {
1364 printk(KERN_WARNING "Error alloc trace buffer!\n");
1365 error = -ENOMEM;
1366 goto out_alloc_trace_failure;
1367 }
1368
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001369 /* TRACE INIT */
Jing Huang7725ccf2009-09-23 17:46:15 -07001370 bfa_trc_init(bfad->trcmod);
1371 bfa_trc(bfad, bfad_inst);
1372
Jing Huang7725ccf2009-09-23 17:46:15 -07001373 if (!(bfad_load_fwimg(pdev))) {
Jing Huang7725ccf2009-09-23 17:46:15 -07001374 kfree(bfad->trcmod);
1375 goto out_alloc_trace_failure;
1376 }
1377
1378 retval = bfad_pci_init(pdev, bfad);
1379 if (retval) {
1380 printk(KERN_WARNING "bfad_pci_init failure!\n");
1381 error = retval;
1382 goto out_pci_init_failure;
1383 }
1384
1385 mutex_lock(&bfad_mutex);
1386 bfad->inst_no = bfad_inst++;
1387 list_add_tail(&bfad->list_entry, &bfad_list);
1388 mutex_unlock(&bfad_mutex);
1389
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001390 /* Initializing the state machine: State set to uninit */
1391 bfa_sm_set_state(bfad, bfad_sm_uninit);
1392
Jing Huang7725ccf2009-09-23 17:46:15 -07001393 spin_lock_init(&bfad->bfad_lock);
1394 pci_set_drvdata(pdev, bfad);
1395
1396 bfad->ref_count = 0;
1397 bfad->pport.bfad = bfad;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001398 INIT_LIST_HEAD(&bfad->pbc_vport_list);
Krishna Gudipatie6714322010-03-03 17:44:02 -08001399
Krishna Gudipati7c38c052011-04-14 16:50:35 -07001400 /* Setup the debugfs node for this bfad */
1401 if (bfa_debugfs_enable)
1402 bfad_debugfs_init(&bfad->pport);
1403
Jing Huang7725ccf2009-09-23 17:46:15 -07001404 retval = bfad_drv_init(bfad);
1405 if (retval != BFA_STATUS_OK)
1406 goto out_drv_init_failure;
Jing Huang7725ccf2009-09-23 17:46:15 -07001407
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001408 bfa_sm_send_event(bfad, BFAD_E_CREATE);
Jing Huang7725ccf2009-09-23 17:46:15 -07001409
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001410 if (bfa_sm_cmp_state(bfad, bfad_sm_uninit))
1411 goto out_bfad_sm_failure;
Jing Huang7725ccf2009-09-23 17:46:15 -07001412
Jing Huang7725ccf2009-09-23 17:46:15 -07001413 return 0;
1414
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001415out_bfad_sm_failure:
1416 bfa_detach(&bfad->bfa);
1417 bfad_hal_mem_release(bfad);
Jing Huang7725ccf2009-09-23 17:46:15 -07001418out_drv_init_failure:
Krishna Gudipati7c38c052011-04-14 16:50:35 -07001419 /* Remove the debugfs node for this bfad */
1420 kfree(bfad->regdata);
1421 bfad_debugfs_exit(&bfad->pport);
Jing Huang7725ccf2009-09-23 17:46:15 -07001422 mutex_lock(&bfad_mutex);
1423 bfad_inst--;
1424 list_del(&bfad->list_entry);
1425 mutex_unlock(&bfad_mutex);
1426 bfad_pci_uninit(pdev, bfad);
1427out_pci_init_failure:
1428 kfree(bfad->trcmod);
1429out_alloc_trace_failure:
1430 kfree(bfad);
1431out:
1432 return error;
1433}
1434
Jing Huang5fbe25c2010-10-18 17:17:23 -07001435/*
Jing Huang7725ccf2009-09-23 17:46:15 -07001436 * PCI remove entry.
1437 */
1438void
1439bfad_pci_remove(struct pci_dev *pdev)
1440{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001441 struct bfad_s *bfad = pci_get_drvdata(pdev);
1442 unsigned long flags;
Jing Huang7725ccf2009-09-23 17:46:15 -07001443
1444 bfa_trc(bfad, bfad->inst_no);
1445
Krishna Gudipatie6714322010-03-03 17:44:02 -08001446 spin_lock_irqsave(&bfad->bfad_lock, flags);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001447 if (bfad->bfad_tsk != NULL) {
1448 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
Krishna Gudipatie6714322010-03-03 17:44:02 -08001449 kthread_stop(bfad->bfad_tsk);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001450 } else {
Jing Huang7725ccf2009-09-23 17:46:15 -07001451 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
Jing Huang7725ccf2009-09-23 17:46:15 -07001452 }
1453
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001454 /* Send Event BFAD_E_STOP */
1455 bfa_sm_send_event(bfad, BFAD_E_STOP);
Jing Huang7725ccf2009-09-23 17:46:15 -07001456
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001457 /* Driver detach and dealloc mem */
Jing Huang7725ccf2009-09-23 17:46:15 -07001458 spin_lock_irqsave(&bfad->bfad_lock, flags);
1459 bfa_detach(&bfad->bfa);
1460 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1461 bfad_hal_mem_release(bfad);
Jing Huang7725ccf2009-09-23 17:46:15 -07001462
Krishna Gudipati7c38c052011-04-14 16:50:35 -07001463 /* Remove the debugfs node for this bfad */
1464 kfree(bfad->regdata);
1465 bfad_debugfs_exit(&bfad->pport);
1466
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 },
Krishna Gudipati11189202011-06-13 15:50:35 -07001507 {
1508 .vendor = BFA_PCI_VENDOR_ID_BROCADE,
1509 .device = BFA_PCI_DEVICE_ID_CT2,
1510 .subvendor = PCI_ANY_ID,
1511 .subdevice = PCI_ANY_ID,
1512 .class = (PCI_CLASS_SERIAL_FIBER << 8),
1513 .class_mask = ~0,
1514 },
Jing Huang7725ccf2009-09-23 17:46:15 -07001515
1516 {0, 0},
1517};
1518
1519MODULE_DEVICE_TABLE(pci, bfad_id_table);
1520
1521static struct pci_driver bfad_pci_driver = {
1522 .name = BFAD_DRIVER_NAME,
1523 .id_table = bfad_id_table,
1524 .probe = bfad_pci_probe,
1525 .remove = __devexit_p(bfad_pci_remove),
1526};
1527
Jing Huang5fbe25c2010-10-18 17:17:23 -07001528/*
Jing Huang7725ccf2009-09-23 17:46:15 -07001529 * Driver module init.
1530 */
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001531static int __init
Jing Huang7725ccf2009-09-23 17:46:15 -07001532bfad_init(void)
1533{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001534 int error = 0;
Jing Huang7725ccf2009-09-23 17:46:15 -07001535
1536 printk(KERN_INFO "Brocade BFA FC/FCOE SCSI driver - version: %s\n",
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001537 BFAD_DRIVER_VERSION);
Jing Huang7725ccf2009-09-23 17:46:15 -07001538
1539 if (num_sgpgs > 0)
1540 num_sgpgs_parm = num_sgpgs;
1541
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001542 error = bfad_im_module_init();
Jing Huang7725ccf2009-09-23 17:46:15 -07001543 if (error) {
1544 error = -ENOMEM;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001545 printk(KERN_WARNING "bfad_im_module_init failure\n");
Jing Huang7725ccf2009-09-23 17:46:15 -07001546 goto ext;
1547 }
1548
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001549 if (strcmp(FCPI_NAME, " fcpim") == 0)
1550 supported_fc4s |= BFA_LPORT_ROLE_FCP_IM;
Jing Huang7725ccf2009-09-23 17:46:15 -07001551
Maggie Zhangf7f73812010-12-09 19:08:43 -08001552 bfa_auto_recover = ioc_auto_recover;
Jing Huang7725ccf2009-09-23 17:46:15 -07001553 bfa_fcs_rport_set_del_timeout(rport_del_timeout);
Jing Huang7725ccf2009-09-23 17:46:15 -07001554
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001555 error = pci_register_driver(&bfad_pci_driver);
Jing Huang7725ccf2009-09-23 17:46:15 -07001556 if (error) {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001557 printk(KERN_WARNING "pci_register_driver failure\n");
Jing Huang7725ccf2009-09-23 17:46:15 -07001558 goto ext;
1559 }
1560
1561 return 0;
1562
1563ext:
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001564 bfad_im_module_exit();
Jing Huang7725ccf2009-09-23 17:46:15 -07001565 return error;
1566}
1567
Jing Huang5fbe25c2010-10-18 17:17:23 -07001568/*
Jing Huang7725ccf2009-09-23 17:46:15 -07001569 * Driver module exit.
1570 */
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001571static void __exit
Jing Huang7725ccf2009-09-23 17:46:15 -07001572bfad_exit(void)
1573{
1574 pci_unregister_driver(&bfad_pci_driver);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001575 bfad_im_module_exit();
Jing Huang7725ccf2009-09-23 17:46:15 -07001576 bfad_free_fwimg();
1577}
1578
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001579/* Firmware handling */
Jing Huang61338a02011-04-13 11:44:03 -07001580static void
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001581bfad_read_firmware(struct pci_dev *pdev, u32 **bfi_image,
1582 u32 *bfi_image_size, char *fw_name)
1583{
1584 const struct firmware *fw;
1585
1586 if (request_firmware(&fw, fw_name, &pdev->dev)) {
1587 printk(KERN_ALERT "Can't locate firmware %s\n", fw_name);
Jing Huang61338a02011-04-13 11:44:03 -07001588 *bfi_image = NULL;
1589 goto out;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001590 }
1591
1592 *bfi_image = vmalloc(fw->size);
1593 if (NULL == *bfi_image) {
1594 printk(KERN_ALERT "Fail to allocate buffer for fw image "
1595 "size=%x!\n", (u32) fw->size);
Jing Huang61338a02011-04-13 11:44:03 -07001596 goto out;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001597 }
1598
1599 memcpy(*bfi_image, fw->data, fw->size);
1600 *bfi_image_size = fw->size/sizeof(u32);
Jing Huang61338a02011-04-13 11:44:03 -07001601out:
1602 release_firmware(fw);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001603}
1604
Jing Huang61338a02011-04-13 11:44:03 -07001605static u32 *
1606bfad_load_fwimg(struct pci_dev *pdev)
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001607{
Krishna Gudipati11189202011-06-13 15:50:35 -07001608 if (pdev->device == BFA_PCI_DEVICE_ID_CT2) {
1609 if (bfi_image_ct2_size == 0)
1610 bfad_read_firmware(pdev, &bfi_image_ct2,
1611 &bfi_image_ct2_size, BFAD_FW_FILE_CT2);
1612 return bfi_image_ct2;
1613 } else if (bfa_asic_id_ct(pdev->device)) {
1614 if (bfi_image_ct_size == 0)
1615 bfad_read_firmware(pdev, &bfi_image_ct,
1616 &bfi_image_ct_size, BFAD_FW_FILE_CT);
1617 return bfi_image_ct;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001618 } else {
Krishna Gudipati11189202011-06-13 15:50:35 -07001619 if (bfi_image_cb_size == 0)
1620 bfad_read_firmware(pdev, &bfi_image_cb,
1621 &bfi_image_cb_size, BFAD_FW_FILE_CB);
1622 return bfi_image_cb;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001623 }
1624}
Jing Huang7725ccf2009-09-23 17:46:15 -07001625
Jing Huang61338a02011-04-13 11:44:03 -07001626static void
1627bfad_free_fwimg(void)
1628{
Krishna Gudipati11189202011-06-13 15:50:35 -07001629 if (bfi_image_ct2_size && bfi_image_ct2)
1630 vfree(bfi_image_ct2);
1631 if (bfi_image_ct_size && bfi_image_ct)
1632 vfree(bfi_image_ct);
1633 if (bfi_image_cb_size && bfi_image_cb)
1634 vfree(bfi_image_cb);
Jing Huang61338a02011-04-13 11:44:03 -07001635}
1636
Jing Huang7725ccf2009-09-23 17:46:15 -07001637module_init(bfad_init);
1638module_exit(bfad_exit);
1639MODULE_LICENSE("GPL");
1640MODULE_DESCRIPTION("Brocade Fibre Channel HBA Driver" BFAD_PROTO_NAME);
1641MODULE_AUTHOR("Brocade Communications Systems, Inc.");
1642MODULE_VERSION(BFAD_DRIVER_VERSION);