blob: 2e6e3ac2d41bc39129da559e313d12674868da9c [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;
Krishna Gudipati61e62e22011-06-24 20:29:07 -070059int max_xfer_size = BFAD_MAX_SECTORS >> 1;
Krishna Gudipati61ba4392012-08-22 19:52:58 -070060int max_rport_logins = BFA_FCS_MAX_RPORT_LOGINS;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -070061
Jing Huang61338a02011-04-13 11:44:03 -070062/* Firmware releated */
Krishna Gudipati11189202011-06-13 15:50:35 -070063u32 bfi_image_cb_size, bfi_image_ct_size, bfi_image_ct2_size;
64u32 *bfi_image_cb, *bfi_image_ct, *bfi_image_ct2;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -070065
Rasesh Mody71b3ba72013-08-23 16:05:57 -070066#define BFAD_FW_FILE_CB "cbfw-3.2.1.1.bin"
67#define BFAD_FW_FILE_CT "ctfw-3.2.1.1.bin"
68#define BFAD_FW_FILE_CT2 "ct2fw-3.2.1.1.bin"
Jing Huang61338a02011-04-13 11:44:03 -070069
70static u32 *bfad_load_fwimg(struct pci_dev *pdev);
71static void bfad_free_fwimg(void);
72static void bfad_read_firmware(struct pci_dev *pdev, u32 **bfi_image,
73 u32 *bfi_image_size, char *fw_name);
74
Maggie52f94b62010-11-29 18:21:32 -080075static const char *msix_name_ct[] = {
Krishna Gudipati11189202011-06-13 15:50:35 -070076 "ctrl",
Krishna Gudipatia36c61f2010-09-15 11:50:55 -070077 "cpe0", "cpe1", "cpe2", "cpe3",
Krishna Gudipati11189202011-06-13 15:50:35 -070078 "rme0", "rme1", "rme2", "rme3" };
Krishna Gudipatia36c61f2010-09-15 11:50:55 -070079
Maggie52f94b62010-11-29 18:21:32 -080080static const char *msix_name_cb[] = {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -070081 "cpe0", "cpe1", "cpe2", "cpe3",
82 "rme0", "rme1", "rme2", "rme3",
83 "eemc", "elpu0", "elpu1", "epss", "mlpu" };
84
Krishna Gudipati11189202011-06-13 15:50:35 -070085MODULE_FIRMWARE(BFAD_FW_FILE_CB);
86MODULE_FIRMWARE(BFAD_FW_FILE_CT);
87MODULE_FIRMWARE(BFAD_FW_FILE_CT2);
Jing Huang7725ccf2009-09-23 17:46:15 -070088
89module_param(os_name, charp, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -070090MODULE_PARM_DESC(os_name, "OS name of the hba host machine");
Jing Huang7725ccf2009-09-23 17:46:15 -070091module_param(os_patch, charp, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -070092MODULE_PARM_DESC(os_patch, "OS patch level of the hba host machine");
Jing Huang7725ccf2009-09-23 17:46:15 -070093module_param(host_name, charp, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -070094MODULE_PARM_DESC(host_name, "Hostname of the hba host machine");
Jing Huang7725ccf2009-09-23 17:46:15 -070095module_param(num_rports, int, S_IRUGO | S_IWUSR);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -070096MODULE_PARM_DESC(num_rports, "Max number of rports supported per port "
97 "(physical/logical), default=1024");
Jing Huang7725ccf2009-09-23 17:46:15 -070098module_param(num_ios, int, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -070099MODULE_PARM_DESC(num_ios, "Max number of ioim requests, default=2000");
Jing Huang7725ccf2009-09-23 17:46:15 -0700100module_param(num_tms, int, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -0700101MODULE_PARM_DESC(num_tms, "Max number of task im requests, default=128");
Jing Huang7725ccf2009-09-23 17:46:15 -0700102module_param(num_fcxps, int, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -0700103MODULE_PARM_DESC(num_fcxps, "Max number of fcxp requests, default=64");
Jing Huang7725ccf2009-09-23 17:46:15 -0700104module_param(num_ufbufs, int, S_IRUGO | S_IWUSR);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700105MODULE_PARM_DESC(num_ufbufs, "Max number of unsolicited frame "
106 "buffers, default=64");
Jing Huang7725ccf2009-09-23 17:46:15 -0700107module_param(reqq_size, int, S_IRUGO | S_IWUSR);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700108MODULE_PARM_DESC(reqq_size, "Max number of request queue elements, "
109 "default=256");
Jing Huang7725ccf2009-09-23 17:46:15 -0700110module_param(rspq_size, int, S_IRUGO | S_IWUSR);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700111MODULE_PARM_DESC(rspq_size, "Max number of response queue elements, "
112 "default=64");
Jing Huang7725ccf2009-09-23 17:46:15 -0700113module_param(num_sgpgs, int, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -0700114MODULE_PARM_DESC(num_sgpgs, "Number of scatter/gather pages, default=2048");
Jing Huang7725ccf2009-09-23 17:46:15 -0700115module_param(rport_del_timeout, int, S_IRUGO | S_IWUSR);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700116MODULE_PARM_DESC(rport_del_timeout, "Rport delete timeout, default=90 secs, "
117 "Range[>0]");
Jing Huang7725ccf2009-09-23 17:46:15 -0700118module_param(bfa_lun_queue_depth, int, S_IRUGO | S_IWUSR);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700119MODULE_PARM_DESC(bfa_lun_queue_depth, "Lun queue depth, default=32, Range[>0]");
Jing Huang7725ccf2009-09-23 17:46:15 -0700120module_param(bfa_io_max_sge, int, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -0700121MODULE_PARM_DESC(bfa_io_max_sge, "Max io scatter/gather elements, default=255");
Jing Huang88166242010-12-09 17:11:53 -0800122module_param(bfa_log_level, int, S_IRUGO | S_IWUSR);
123MODULE_PARM_DESC(bfa_log_level, "Driver log level, default=3, "
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700124 "Range[Critical:1|Error:2|Warning:3|Info:4]");
Jing Huang7725ccf2009-09-23 17:46:15 -0700125module_param(ioc_auto_recover, int, S_IRUGO | S_IWUSR);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700126MODULE_PARM_DESC(ioc_auto_recover, "IOC auto recovery, default=1, "
127 "Range[off:0|on:1]");
Jing Huang7725ccf2009-09-23 17:46:15 -0700128module_param(bfa_linkup_delay, int, S_IRUGO | S_IWUSR);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700129MODULE_PARM_DESC(bfa_linkup_delay, "Link up delay, default=30 secs for "
130 "boot port. Otherwise 10 secs in RHEL4 & 0 for "
131 "[RHEL5, SLES10, ESX40] Range[>0]");
132module_param(msix_disable_cb, int, S_IRUGO | S_IWUSR);
133MODULE_PARM_DESC(msix_disable_cb, "Disable Message Signaled Interrupts "
134 "for Brocade-415/425/815/825 cards, default=0, "
135 " Range[false:0|true:1]");
136module_param(msix_disable_ct, int, S_IRUGO | S_IWUSR);
137MODULE_PARM_DESC(msix_disable_ct, "Disable Message Signaled Interrupts "
138 "if possible for Brocade-1010/1020/804/1007/902/1741 "
139 "cards, default=0, Range[false:0|true:1]");
Jing Huang604158ad2010-07-08 19:59:49 -0700140module_param(fdmi_enable, int, S_IRUGO | S_IWUSR);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700141MODULE_PARM_DESC(fdmi_enable, "Enables fdmi registration, default=1, "
142 "Range[false:0|true:1]");
143module_param(pcie_max_read_reqsz, int, S_IRUGO | S_IWUSR);
144MODULE_PARM_DESC(pcie_max_read_reqsz, "PCIe max read request size, default=0 "
145 "(use system setting), Range[128|256|512|1024|2048|4096]");
Jing Huangab2a9ba2010-07-08 20:02:55 -0700146module_param(bfa_debugfs_enable, int, S_IRUGO | S_IWUSR);
147MODULE_PARM_DESC(bfa_debugfs_enable, "Enables debugfs feature, default=1,"
148 " Range[false:0|true:1]");
Krishna Gudipati61e62e22011-06-24 20:29:07 -0700149module_param(max_xfer_size, int, S_IRUGO | S_IWUSR);
150MODULE_PARM_DESC(max_xfer_size, "default=32MB,"
151 " Range[64k|128k|256k|512k|1024k|2048k]");
Krishna Gudipati61ba4392012-08-22 19:52:58 -0700152module_param(max_rport_logins, int, S_IRUGO | S_IWUSR);
153MODULE_PARM_DESC(max_rport_logins, "Max number of logins to initiator and target rports on a port (physical/logical), default=1024");
Jing Huang7725ccf2009-09-23 17:46:15 -0700154
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700155static void
156bfad_sm_uninit(struct bfad_s *bfad, enum bfad_sm_event event);
157static void
158bfad_sm_created(struct bfad_s *bfad, enum bfad_sm_event event);
159static void
160bfad_sm_initializing(struct bfad_s *bfad, enum bfad_sm_event event);
161static void
162bfad_sm_operational(struct bfad_s *bfad, enum bfad_sm_event event);
163static void
164bfad_sm_stopping(struct bfad_s *bfad, enum bfad_sm_event event);
165static void
166bfad_sm_failed(struct bfad_s *bfad, enum bfad_sm_event event);
167static void
168bfad_sm_fcs_exit(struct bfad_s *bfad, enum bfad_sm_event event);
169
Jing Huang5fbe25c2010-10-18 17:17:23 -0700170/*
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700171 * Beginning state for the driver instance, awaiting the pci_probe event
Jing Huang7725ccf2009-09-23 17:46:15 -0700172 */
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700173static void
174bfad_sm_uninit(struct bfad_s *bfad, enum bfad_sm_event event)
Jing Huang7725ccf2009-09-23 17:46:15 -0700175{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700176 bfa_trc(bfad, event);
Jing Huang7725ccf2009-09-23 17:46:15 -0700177
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700178 switch (event) {
179 case BFAD_E_CREATE:
180 bfa_sm_set_state(bfad, bfad_sm_created);
181 bfad->bfad_tsk = kthread_create(bfad_worker, (void *) bfad,
182 "%s", "bfad_worker");
183 if (IS_ERR(bfad->bfad_tsk)) {
184 printk(KERN_INFO "bfad[%d]: Kernel thread "
185 "creation failed!\n", bfad->inst_no);
186 bfa_sm_send_event(bfad, BFAD_E_KTHREAD_CREATE_FAILED);
187 }
188 bfa_sm_send_event(bfad, BFAD_E_INIT);
189 break;
Jing Huang7725ccf2009-09-23 17:46:15 -0700190
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700191 case BFAD_E_STOP:
192 /* Ignore stop; already in uninit */
193 break;
Jing Huang7725ccf2009-09-23 17:46:15 -0700194
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700195 default:
196 bfa_sm_fault(bfad, event);
197 }
198}
Krishna Gudipatie6714322010-03-03 17:44:02 -0800199
Jing Huang5fbe25c2010-10-18 17:17:23 -0700200/*
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700201 * Driver Instance is created, awaiting event INIT to initialize the bfad
202 */
203static void
204bfad_sm_created(struct bfad_s *bfad, enum bfad_sm_event event)
205{
206 unsigned long flags;
207
208 bfa_trc(bfad, event);
209
210 switch (event) {
211 case BFAD_E_INIT:
212 bfa_sm_set_state(bfad, bfad_sm_initializing);
213
214 init_completion(&bfad->comp);
215
216 /* Enable Interrupt and wait bfa_init completion */
217 if (bfad_setup_intr(bfad)) {
218 printk(KERN_WARNING "bfad%d: bfad_setup_intr failed\n",
219 bfad->inst_no);
220 bfa_sm_send_event(bfad, BFAD_E_INTR_INIT_FAILED);
221 break;
222 }
223
224 spin_lock_irqsave(&bfad->bfad_lock, flags);
Maggie Zhangf7f738122010-12-09 19:08:43 -0800225 bfa_iocfc_init(&bfad->bfa);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700226 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
227
228 /* Set up interrupt handler for each vectors */
229 if ((bfad->bfad_flags & BFAD_MSIX_ON) &&
230 bfad_install_msix_handler(bfad)) {
231 printk(KERN_WARNING "%s: install_msix failed, bfad%d\n",
232 __func__, bfad->inst_no);
233 }
234
235 bfad_init_timer(bfad);
236
237 wait_for_completion(&bfad->comp);
238
239 if ((bfad->bfad_flags & BFAD_HAL_INIT_DONE)) {
240 bfa_sm_send_event(bfad, BFAD_E_INIT_SUCCESS);
241 } else {
Krishna Gudipati7c38c052011-04-14 16:50:35 -0700242 printk(KERN_WARNING
243 "bfa %s: bfa init failed\n",
244 bfad->pci_name);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700245 bfad->bfad_flags |= BFAD_HAL_INIT_FAIL;
246 bfa_sm_send_event(bfad, BFAD_E_INIT_FAILED);
247 }
248
249 break;
250
251 case BFAD_E_KTHREAD_CREATE_FAILED:
252 bfa_sm_set_state(bfad, bfad_sm_uninit);
253 break;
254
255 default:
256 bfa_sm_fault(bfad, event);
257 }
Jing Huang7725ccf2009-09-23 17:46:15 -0700258}
259
260static void
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700261bfad_sm_initializing(struct bfad_s *bfad, enum bfad_sm_event event)
Jing Huang7725ccf2009-09-23 17:46:15 -0700262{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700263 int retval;
264 unsigned long flags;
265
266 bfa_trc(bfad, event);
267
268 switch (event) {
269 case BFAD_E_INIT_SUCCESS:
270 kthread_stop(bfad->bfad_tsk);
271 spin_lock_irqsave(&bfad->bfad_lock, flags);
272 bfad->bfad_tsk = NULL;
273 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
274
275 retval = bfad_start_ops(bfad);
276 if (retval != BFA_STATUS_OK)
277 break;
278 bfa_sm_set_state(bfad, bfad_sm_operational);
279 break;
280
281 case BFAD_E_INTR_INIT_FAILED:
282 bfa_sm_set_state(bfad, bfad_sm_uninit);
283 kthread_stop(bfad->bfad_tsk);
284 spin_lock_irqsave(&bfad->bfad_lock, flags);
285 bfad->bfad_tsk = NULL;
286 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
287 break;
288
289 case BFAD_E_INIT_FAILED:
290 bfa_sm_set_state(bfad, bfad_sm_failed);
291 break;
292 default:
293 bfa_sm_fault(bfad, event);
294 }
Jing Huang7725ccf2009-09-23 17:46:15 -0700295}
296
297static void
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700298bfad_sm_failed(struct bfad_s *bfad, enum bfad_sm_event event)
Jing Huang7725ccf2009-09-23 17:46:15 -0700299{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700300 int retval;
Jing Huang7725ccf2009-09-23 17:46:15 -0700301
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700302 bfa_trc(bfad, event);
Jing Huang7725ccf2009-09-23 17:46:15 -0700303
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700304 switch (event) {
305 case BFAD_E_INIT_SUCCESS:
306 retval = bfad_start_ops(bfad);
307 if (retval != BFA_STATUS_OK)
308 break;
309 bfa_sm_set_state(bfad, bfad_sm_operational);
310 break;
Jing Huang7725ccf2009-09-23 17:46:15 -0700311
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700312 case BFAD_E_STOP:
313 if (bfad->bfad_flags & BFAD_CFG_PPORT_DONE)
314 bfad_uncfg_pport(bfad);
315 if (bfad->bfad_flags & BFAD_FC4_PROBE_DONE) {
316 bfad_im_probe_undo(bfad);
317 bfad->bfad_flags &= ~BFAD_FC4_PROBE_DONE;
318 }
319 bfad_stop(bfad);
320 break;
Jing Huang7725ccf2009-09-23 17:46:15 -0700321
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700322 case BFAD_E_EXIT_COMP:
323 bfa_sm_set_state(bfad, bfad_sm_uninit);
324 bfad_remove_intr(bfad);
325 del_timer_sync(&bfad->hal_tmo);
326 break;
Jing Huang7725ccf2009-09-23 17:46:15 -0700327
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700328 default:
329 bfa_sm_fault(bfad, event);
330 }
Jing Huang7725ccf2009-09-23 17:46:15 -0700331}
332
333static void
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700334bfad_sm_operational(struct bfad_s *bfad, enum bfad_sm_event event)
Jing Huang7725ccf2009-09-23 17:46:15 -0700335{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700336 bfa_trc(bfad, event);
Jing Huang7725ccf2009-09-23 17:46:15 -0700337
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700338 switch (event) {
339 case BFAD_E_STOP:
340 bfa_sm_set_state(bfad, bfad_sm_fcs_exit);
341 bfad_fcs_stop(bfad);
342 break;
Jing Huang7725ccf2009-09-23 17:46:15 -0700343
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700344 default:
345 bfa_sm_fault(bfad, event);
346 }
347}
348
349static void
350bfad_sm_fcs_exit(struct bfad_s *bfad, enum bfad_sm_event event)
351{
352 bfa_trc(bfad, event);
353
354 switch (event) {
355 case BFAD_E_FCS_EXIT_COMP:
356 bfa_sm_set_state(bfad, bfad_sm_stopping);
357 bfad_stop(bfad);
358 break;
359
360 default:
361 bfa_sm_fault(bfad, event);
362 }
363}
364
365static void
366bfad_sm_stopping(struct bfad_s *bfad, enum bfad_sm_event event)
367{
368 bfa_trc(bfad, event);
369
370 switch (event) {
371 case BFAD_E_EXIT_COMP:
372 bfa_sm_set_state(bfad, bfad_sm_uninit);
373 bfad_remove_intr(bfad);
374 del_timer_sync(&bfad->hal_tmo);
375 bfad_im_probe_undo(bfad);
376 bfad->bfad_flags &= ~BFAD_FC4_PROBE_DONE;
377 bfad_uncfg_pport(bfad);
378 break;
379
380 default:
381 bfa_sm_fault(bfad, event);
382 break;
383 }
Jing Huang7725ccf2009-09-23 17:46:15 -0700384}
385
Jing Huang5fbe25c2010-10-18 17:17:23 -0700386/*
Jing Huang7725ccf2009-09-23 17:46:15 -0700387 * BFA callbacks
388 */
389void
390bfad_hcb_comp(void *arg, bfa_status_t status)
391{
392 struct bfad_hal_comp *fcomp = (struct bfad_hal_comp *)arg;
393
394 fcomp->status = status;
395 complete(&fcomp->comp);
396}
397
Jing Huang5fbe25c2010-10-18 17:17:23 -0700398/*
Jing Huang7725ccf2009-09-23 17:46:15 -0700399 * bfa_init callback
400 */
401void
402bfa_cb_init(void *drv, bfa_status_t init_status)
403{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700404 struct bfad_s *bfad = drv;
Jing Huang7725ccf2009-09-23 17:46:15 -0700405
Krishna Gudipatie6714322010-03-03 17:44:02 -0800406 if (init_status == BFA_STATUS_OK) {
Jing Huang7725ccf2009-09-23 17:46:15 -0700407 bfad->bfad_flags |= BFAD_HAL_INIT_DONE;
408
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700409 /*
410 * If BFAD_HAL_INIT_FAIL flag is set:
Krishna Gudipatie6714322010-03-03 17:44:02 -0800411 * Wake up the kernel thread to start
412 * the bfad operations after HAL init done
413 */
414 if ((bfad->bfad_flags & BFAD_HAL_INIT_FAIL)) {
415 bfad->bfad_flags &= ~BFAD_HAL_INIT_FAIL;
416 wake_up_process(bfad->bfad_tsk);
417 }
418 }
419
Jing Huang7725ccf2009-09-23 17:46:15 -0700420 complete(&bfad->comp);
421}
422
Jing Huang5fbe25c2010-10-18 17:17:23 -0700423/*
Jing Huang7725ccf2009-09-23 17:46:15 -0700424 * BFA_FCS callbacks
425 */
Jing Huang7725ccf2009-09-23 17:46:15 -0700426struct bfad_port_s *
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700427bfa_fcb_lport_new(struct bfad_s *bfad, struct bfa_fcs_lport_s *port,
428 enum bfa_lport_role roles, struct bfad_vf_s *vf_drv,
Jing Huang7725ccf2009-09-23 17:46:15 -0700429 struct bfad_vport_s *vp_drv)
430{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700431 bfa_status_t rc;
432 struct bfad_port_s *port_drv;
Jing Huang7725ccf2009-09-23 17:46:15 -0700433
434 if (!vp_drv && !vf_drv) {
435 port_drv = &bfad->pport;
436 port_drv->pvb_type = BFAD_PORT_PHYS_BASE;
437 } else if (!vp_drv && vf_drv) {
438 port_drv = &vf_drv->base_port;
439 port_drv->pvb_type = BFAD_PORT_VF_BASE;
440 } else if (vp_drv && !vf_drv) {
441 port_drv = &vp_drv->drv_port;
442 port_drv->pvb_type = BFAD_PORT_PHYS_VPORT;
443 } else {
444 port_drv = &vp_drv->drv_port;
445 port_drv->pvb_type = BFAD_PORT_VF_VPORT;
446 }
447
448 port_drv->fcs_port = port;
449 port_drv->roles = roles;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700450
451 if (roles & BFA_LPORT_ROLE_FCP_IM) {
452 rc = bfad_im_port_new(bfad, port_drv);
453 if (rc != BFA_STATUS_OK) {
454 bfad_im_port_delete(bfad, port_drv);
455 port_drv = NULL;
456 }
Jing Huang7725ccf2009-09-23 17:46:15 -0700457 }
458
459 return port_drv;
460}
461
Jing Huang5fbe25c2010-10-18 17:17:23 -0700462/*
Jing Huang7725ccf2009-09-23 17:46:15 -0700463 * FCS RPORT alloc callback, after successful PLOGI by FCS
464 */
465bfa_status_t
466bfa_fcb_rport_alloc(struct bfad_s *bfad, struct bfa_fcs_rport_s **rport,
467 struct bfad_rport_s **rport_drv)
468{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700469 bfa_status_t rc = BFA_STATUS_OK;
Jing Huang7725ccf2009-09-23 17:46:15 -0700470
471 *rport_drv = kzalloc(sizeof(struct bfad_rport_s), GFP_ATOMIC);
472 if (*rport_drv == NULL) {
473 rc = BFA_STATUS_ENOMEM;
474 goto ext;
475 }
476
477 *rport = &(*rport_drv)->fcs_rport;
478
479ext:
480 return rc;
481}
482
Jing Huang5fbe25c2010-10-18 17:17:23 -0700483/*
Jing Huangd9883542010-07-08 19:46:26 -0700484 * FCS PBC VPORT Create
485 */
486void
487bfa_fcb_pbc_vport_create(struct bfad_s *bfad, struct bfi_pbc_vport_s pbc_vport)
488{
Jing Huang7725ccf2009-09-23 17:46:15 -0700489
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700490 struct bfa_lport_cfg_s port_cfg = {0};
491 struct bfad_vport_s *vport;
492 int rc;
Jing Huangd9883542010-07-08 19:46:26 -0700493
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700494 vport = kzalloc(sizeof(struct bfad_vport_s), GFP_KERNEL);
495 if (!vport) {
Jing Huangd9883542010-07-08 19:46:26 -0700496 bfa_trc(bfad, 0);
497 return;
498 }
499
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700500 vport->drv_port.bfad = bfad;
501 port_cfg.roles = BFA_LPORT_ROLE_FCP_IM;
502 port_cfg.pwwn = pbc_vport.vp_pwwn;
503 port_cfg.nwwn = pbc_vport.vp_nwwn;
504 port_cfg.preboot_vp = BFA_TRUE;
Jing Huangd9883542010-07-08 19:46:26 -0700505
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700506 rc = bfa_fcs_pbc_vport_create(&vport->fcs_vport, &bfad->bfa_fcs, 0,
507 &port_cfg, vport);
Jing Huangd9883542010-07-08 19:46:26 -0700508
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700509 if (rc != BFA_STATUS_OK) {
510 bfa_trc(bfad, 0);
511 return;
512 }
513
514 list_add_tail(&vport->list_entry, &bfad->pbc_vport_list);
Jing Huangd9883542010-07-08 19:46:26 -0700515}
Jing Huang7725ccf2009-09-23 17:46:15 -0700516
517void
518bfad_hal_mem_release(struct bfad_s *bfad)
519{
Jing Huang7725ccf2009-09-23 17:46:15 -0700520 struct bfa_meminfo_s *hal_meminfo = &bfad->meminfo;
Krishna Gudipati45070252011-06-24 20:24:29 -0700521 struct bfa_mem_dma_s *dma_info, *dma_elem;
522 struct bfa_mem_kva_s *kva_info, *kva_elem;
523 struct list_head *dm_qe, *km_qe;
Jing Huang7725ccf2009-09-23 17:46:15 -0700524
Krishna Gudipati45070252011-06-24 20:24:29 -0700525 dma_info = &hal_meminfo->dma_info;
526 kva_info = &hal_meminfo->kva_info;
527
528 /* Iterate through the KVA meminfo queue */
529 list_for_each(km_qe, &kva_info->qe) {
530 kva_elem = (struct bfa_mem_kva_s *) km_qe;
531 vfree(kva_elem->kva);
532 }
533
534 /* Iterate through the DMA meminfo queue */
535 list_for_each(dm_qe, &dma_info->qe) {
536 dma_elem = (struct bfa_mem_dma_s *) dm_qe;
537 dma_free_coherent(&bfad->pcidev->dev,
538 dma_elem->mem_len, dma_elem->kva,
539 (dma_addr_t) dma_elem->dma);
Jing Huang7725ccf2009-09-23 17:46:15 -0700540 }
541
542 memset(hal_meminfo, 0, sizeof(struct bfa_meminfo_s));
543}
544
545void
546bfad_update_hal_cfg(struct bfa_iocfc_cfg_s *bfa_cfg)
547{
548 if (num_rports > 0)
549 bfa_cfg->fwcfg.num_rports = num_rports;
550 if (num_ios > 0)
551 bfa_cfg->fwcfg.num_ioim_reqs = num_ios;
552 if (num_tms > 0)
553 bfa_cfg->fwcfg.num_tskim_reqs = num_tms;
Krishna Gudipati45070252011-06-24 20:24:29 -0700554 if (num_fcxps > 0 && num_fcxps <= BFA_FCXP_MAX)
Jing Huang7725ccf2009-09-23 17:46:15 -0700555 bfa_cfg->fwcfg.num_fcxp_reqs = num_fcxps;
Krishna Gudipati45070252011-06-24 20:24:29 -0700556 if (num_ufbufs > 0 && num_ufbufs <= BFA_UF_MAX)
Jing Huang7725ccf2009-09-23 17:46:15 -0700557 bfa_cfg->fwcfg.num_uf_bufs = num_ufbufs;
558 if (reqq_size > 0)
559 bfa_cfg->drvcfg.num_reqq_elems = reqq_size;
560 if (rspq_size > 0)
561 bfa_cfg->drvcfg.num_rspq_elems = rspq_size;
Krishna Gudipati45070252011-06-24 20:24:29 -0700562 if (num_sgpgs > 0 && num_sgpgs <= BFA_SGPG_MAX)
Jing Huang7725ccf2009-09-23 17:46:15 -0700563 bfa_cfg->drvcfg.num_sgpgs = num_sgpgs;
564
565 /*
566 * populate the hal values back to the driver for sysfs use.
567 * otherwise, the default values will be shown as 0 in sysfs
568 */
569 num_rports = bfa_cfg->fwcfg.num_rports;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700570 num_ios = bfa_cfg->fwcfg.num_ioim_reqs;
571 num_tms = bfa_cfg->fwcfg.num_tskim_reqs;
572 num_fcxps = bfa_cfg->fwcfg.num_fcxp_reqs;
Jing Huang7725ccf2009-09-23 17:46:15 -0700573 num_ufbufs = bfa_cfg->fwcfg.num_uf_bufs;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700574 reqq_size = bfa_cfg->drvcfg.num_reqq_elems;
575 rspq_size = bfa_cfg->drvcfg.num_rspq_elems;
576 num_sgpgs = bfa_cfg->drvcfg.num_sgpgs;
Jing Huang7725ccf2009-09-23 17:46:15 -0700577}
578
579bfa_status_t
580bfad_hal_mem_alloc(struct bfad_s *bfad)
581{
582 struct bfa_meminfo_s *hal_meminfo = &bfad->meminfo;
Krishna Gudipati45070252011-06-24 20:24:29 -0700583 struct bfa_mem_dma_s *dma_info, *dma_elem;
584 struct bfa_mem_kva_s *kva_info, *kva_elem;
585 struct list_head *dm_qe, *km_qe;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700586 bfa_status_t rc = BFA_STATUS_OK;
Krishna Gudipati45070252011-06-24 20:24:29 -0700587 dma_addr_t phys_addr;
Jing Huang7725ccf2009-09-23 17:46:15 -0700588
589 bfa_cfg_get_default(&bfad->ioc_cfg);
Jing Huang7725ccf2009-09-23 17:46:15 -0700590 bfad_update_hal_cfg(&bfad->ioc_cfg);
591 bfad->cfg_data.ioc_queue_depth = bfad->ioc_cfg.fwcfg.num_ioim_reqs;
Krishna Gudipati45070252011-06-24 20:24:29 -0700592 bfa_cfg_get_meminfo(&bfad->ioc_cfg, hal_meminfo, &bfad->bfa);
Jing Huang7725ccf2009-09-23 17:46:15 -0700593
Krishna Gudipati45070252011-06-24 20:24:29 -0700594 dma_info = &hal_meminfo->dma_info;
595 kva_info = &hal_meminfo->kva_info;
Jing Huang7725ccf2009-09-23 17:46:15 -0700596
Krishna Gudipati45070252011-06-24 20:24:29 -0700597 /* Iterate through the KVA meminfo queue */
598 list_for_each(km_qe, &kva_info->qe) {
599 kva_elem = (struct bfa_mem_kva_s *) km_qe;
600 kva_elem->kva = vmalloc(kva_elem->mem_len);
601 if (kva_elem->kva == NULL) {
602 bfad_hal_mem_release(bfad);
603 rc = BFA_STATUS_ENOMEM;
604 goto ext;
Jing Huang7725ccf2009-09-23 17:46:15 -0700605 }
Krishna Gudipati45070252011-06-24 20:24:29 -0700606 memset(kva_elem->kva, 0, kva_elem->mem_len);
607 }
608
609 /* Iterate through the DMA meminfo queue */
610 list_for_each(dm_qe, &dma_info->qe) {
611 dma_elem = (struct bfa_mem_dma_s *) dm_qe;
612 dma_elem->kva = dma_alloc_coherent(&bfad->pcidev->dev,
613 dma_elem->mem_len,
614 &phys_addr, GFP_KERNEL);
615 if (dma_elem->kva == NULL) {
616 bfad_hal_mem_release(bfad);
617 rc = BFA_STATUS_ENOMEM;
618 goto ext;
619 }
620 dma_elem->dma = phys_addr;
621 memset(dma_elem->kva, 0, dma_elem->mem_len);
Jing Huang7725ccf2009-09-23 17:46:15 -0700622 }
623ext:
624 return rc;
625}
626
Jing Huang5fbe25c2010-10-18 17:17:23 -0700627/*
Jing Huang7725ccf2009-09-23 17:46:15 -0700628 * Create a vport under a vf.
629 */
630bfa_status_t
631bfad_vport_create(struct bfad_s *bfad, u16 vf_id,
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700632 struct bfa_lport_cfg_s *port_cfg, struct device *dev)
Jing Huang7725ccf2009-09-23 17:46:15 -0700633{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700634 struct bfad_vport_s *vport;
635 int rc = BFA_STATUS_OK;
636 unsigned long flags;
Jing Huang7725ccf2009-09-23 17:46:15 -0700637 struct completion fcomp;
638
639 vport = kzalloc(sizeof(struct bfad_vport_s), GFP_KERNEL);
640 if (!vport) {
641 rc = BFA_STATUS_ENOMEM;
642 goto ext;
643 }
644
645 vport->drv_port.bfad = bfad;
646 spin_lock_irqsave(&bfad->bfad_lock, flags);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700647 rc = bfa_fcs_vport_create(&vport->fcs_vport, &bfad->bfa_fcs, vf_id,
648 port_cfg, vport);
Jing Huang7725ccf2009-09-23 17:46:15 -0700649 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
650
651 if (rc != BFA_STATUS_OK)
652 goto ext_free_vport;
653
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700654 if (port_cfg->roles & BFA_LPORT_ROLE_FCP_IM) {
Jing Huangb5042932010-03-19 11:05:39 -0700655 rc = bfad_im_scsi_host_alloc(bfad, vport->drv_port.im_port,
656 dev);
Jing Huang7725ccf2009-09-23 17:46:15 -0700657 if (rc != BFA_STATUS_OK)
658 goto ext_free_fcs_vport;
659 }
660
661 spin_lock_irqsave(&bfad->bfad_lock, flags);
662 bfa_fcs_vport_start(&vport->fcs_vport);
Krishna Gudipati5b7db7a2011-12-20 18:58:32 -0800663 list_add_tail(&vport->list_entry, &bfad->vport_list);
Jing Huang7725ccf2009-09-23 17:46:15 -0700664 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
665
666 return BFA_STATUS_OK;
667
668ext_free_fcs_vport:
669 spin_lock_irqsave(&bfad->bfad_lock, flags);
670 vport->comp_del = &fcomp;
671 init_completion(vport->comp_del);
672 bfa_fcs_vport_delete(&vport->fcs_vport);
673 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
674 wait_for_completion(vport->comp_del);
675ext_free_vport:
676 kfree(vport);
677ext:
678 return rc;
679}
680
Jing Huang7725ccf2009-09-23 17:46:15 -0700681void
682bfad_bfa_tmo(unsigned long data)
683{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700684 struct bfad_s *bfad = (struct bfad_s *) data;
685 unsigned long flags;
686 struct list_head doneq;
Jing Huang7725ccf2009-09-23 17:46:15 -0700687
688 spin_lock_irqsave(&bfad->bfad_lock, flags);
689
Maggie Zhangf7f738122010-12-09 19:08:43 -0800690 bfa_timer_beat(&bfad->bfa.timer_mod);
Jing Huang7725ccf2009-09-23 17:46:15 -0700691
692 bfa_comp_deq(&bfad->bfa, &doneq);
693 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
694
695 if (!list_empty(&doneq)) {
696 bfa_comp_process(&bfad->bfa, &doneq);
697 spin_lock_irqsave(&bfad->bfad_lock, flags);
698 bfa_comp_free(&bfad->bfa, &doneq);
699 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
700 }
701
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700702 mod_timer(&bfad->hal_tmo,
703 jiffies + msecs_to_jiffies(BFA_TIMER_FREQ));
Jing Huang7725ccf2009-09-23 17:46:15 -0700704}
705
706void
707bfad_init_timer(struct bfad_s *bfad)
708{
709 init_timer(&bfad->hal_tmo);
710 bfad->hal_tmo.function = bfad_bfa_tmo;
711 bfad->hal_tmo.data = (unsigned long)bfad;
712
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700713 mod_timer(&bfad->hal_tmo,
714 jiffies + msecs_to_jiffies(BFA_TIMER_FREQ));
Jing Huang7725ccf2009-09-23 17:46:15 -0700715}
716
717int
718bfad_pci_init(struct pci_dev *pdev, struct bfad_s *bfad)
719{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700720 int rc = -ENODEV;
Jing Huang7725ccf2009-09-23 17:46:15 -0700721
722 if (pci_enable_device(pdev)) {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700723 printk(KERN_ERR "pci_enable_device fail %p\n", pdev);
Jing Huang7725ccf2009-09-23 17:46:15 -0700724 goto out;
725 }
726
727 if (pci_request_regions(pdev, BFAD_DRIVER_NAME))
728 goto out_disable_device;
729
730 pci_set_master(pdev);
731
732
Krishna Gudipati10a07372011-06-24 20:23:38 -0700733 if ((pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) ||
734 (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)) != 0)) {
735 if ((pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0) ||
736 (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)) {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700737 printk(KERN_ERR "pci_set_dma_mask fail %p\n", pdev);
Jing Huang7725ccf2009-09-23 17:46:15 -0700738 goto out_release_region;
739 }
Krishna Gudipati10a07372011-06-24 20:23:38 -0700740 }
Jing Huang7725ccf2009-09-23 17:46:15 -0700741
Krishna Gudipati881c1b32012-08-22 19:52:02 -0700742 /* Enable PCIE Advanced Error Recovery (AER) if kernel supports */
743 pci_enable_pcie_error_reporting(pdev);
744
Jing Huangb3522f02010-03-19 11:06:44 -0700745 bfad->pci_bar0_kva = pci_iomap(pdev, 0, pci_resource_len(pdev, 0));
Krishna Gudipati11189202011-06-13 15:50:35 -0700746 bfad->pci_bar2_kva = pci_iomap(pdev, 2, pci_resource_len(pdev, 2));
Jing Huang7725ccf2009-09-23 17:46:15 -0700747
748 if (bfad->pci_bar0_kva == NULL) {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700749 printk(KERN_ERR "Fail to map bar0\n");
Jing Huang7725ccf2009-09-23 17:46:15 -0700750 goto out_release_region;
751 }
752
753 bfad->hal_pcidev.pci_slot = PCI_SLOT(pdev->devfn);
754 bfad->hal_pcidev.pci_func = PCI_FUNC(pdev->devfn);
755 bfad->hal_pcidev.pci_bar_kva = bfad->pci_bar0_kva;
756 bfad->hal_pcidev.device_id = pdev->device;
Krishna Gudipati1a4d8e12011-06-24 20:22:28 -0700757 bfad->hal_pcidev.ssid = pdev->subsystem_device;
Jing Huang7725ccf2009-09-23 17:46:15 -0700758 bfad->pci_name = pci_name(pdev);
759
760 bfad->pci_attr.vendor_id = pdev->vendor;
761 bfad->pci_attr.device_id = pdev->device;
762 bfad->pci_attr.ssid = pdev->subsystem_device;
763 bfad->pci_attr.ssvid = pdev->subsystem_vendor;
764 bfad->pci_attr.pcifn = PCI_FUNC(pdev->devfn);
765
766 bfad->pcidev = pdev;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700767
768 /* Adjust PCIe Maximum Read Request Size */
769 if (pcie_max_read_reqsz > 0) {
770 int pcie_cap_reg;
771 u16 pcie_dev_ctl;
772 u16 mask = 0xffff;
773
774 switch (pcie_max_read_reqsz) {
775 case 128:
776 mask = 0x0;
777 break;
778 case 256:
779 mask = 0x1000;
780 break;
781 case 512:
782 mask = 0x2000;
783 break;
784 case 1024:
785 mask = 0x3000;
786 break;
787 case 2048:
788 mask = 0x4000;
789 break;
790 case 4096:
791 mask = 0x5000;
792 break;
793 default:
794 break;
795 }
796
797 pcie_cap_reg = pci_find_capability(pdev, PCI_CAP_ID_EXP);
798 if (mask != 0xffff && pcie_cap_reg) {
799 pcie_cap_reg += 0x08;
800 pci_read_config_word(pdev, pcie_cap_reg, &pcie_dev_ctl);
801 if ((pcie_dev_ctl & 0x7000) != mask) {
802 printk(KERN_WARNING "BFA[%s]: "
803 "pcie_max_read_request_size is %d, "
804 "reset to %d\n", bfad->pci_name,
805 (1 << ((pcie_dev_ctl & 0x7000) >> 12)) << 7,
806 pcie_max_read_reqsz);
807
808 pcie_dev_ctl &= ~0x7000;
809 pci_write_config_word(pdev, pcie_cap_reg,
810 pcie_dev_ctl | mask);
811 }
812 }
813 }
814
Krishna Gudipati881c1b32012-08-22 19:52:02 -0700815 pci_save_state(pdev);
816
Jing Huang7725ccf2009-09-23 17:46:15 -0700817 return 0;
818
819out_release_region:
820 pci_release_regions(pdev);
821out_disable_device:
822 pci_disable_device(pdev);
823out:
824 return rc;
825}
826
827void
828bfad_pci_uninit(struct pci_dev *pdev, struct bfad_s *bfad)
829{
Jing Huang7725ccf2009-09-23 17:46:15 -0700830 pci_iounmap(pdev, bfad->pci_bar0_kva);
Krishna Gudipati11189202011-06-13 15:50:35 -0700831 pci_iounmap(pdev, bfad->pci_bar2_kva);
Jing Huang7725ccf2009-09-23 17:46:15 -0700832 pci_release_regions(pdev);
Krishna Gudipati881c1b32012-08-22 19:52:02 -0700833 /* Disable PCIE Advanced Error Recovery (AER) */
834 pci_disable_pcie_error_reporting(pdev);
Jing Huang7725ccf2009-09-23 17:46:15 -0700835 pci_disable_device(pdev);
Jing Huang7725ccf2009-09-23 17:46:15 -0700836}
837
Jing Huang7725ccf2009-09-23 17:46:15 -0700838bfa_status_t
839bfad_drv_init(struct bfad_s *bfad)
840{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700841 bfa_status_t rc;
842 unsigned long flags;
Jing Huang7725ccf2009-09-23 17:46:15 -0700843
844 bfad->cfg_data.rport_del_timeout = rport_del_timeout;
845 bfad->cfg_data.lun_queue_depth = bfa_lun_queue_depth;
846 bfad->cfg_data.io_max_sge = bfa_io_max_sge;
847 bfad->cfg_data.binding_method = FCP_PWWN_BINDING;
848
849 rc = bfad_hal_mem_alloc(bfad);
850 if (rc != BFA_STATUS_OK) {
851 printk(KERN_WARNING "bfad%d bfad_hal_mem_alloc failure\n",
852 bfad->inst_no);
853 printk(KERN_WARNING
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700854 "Not enough memory to attach all Brocade HBA ports, %s",
855 "System may need more memory.\n");
Jing Huang7725ccf2009-09-23 17:46:15 -0700856 goto out_hal_mem_alloc_failure;
857 }
858
Maggie Zhangf7f738122010-12-09 19:08:43 -0800859 bfad->bfa.trcmod = bfad->trcmod;
860 bfad->bfa.plog = &bfad->plog_buf;
Jing Huang7725ccf2009-09-23 17:46:15 -0700861 bfa_plog_init(&bfad->plog_buf);
862 bfa_plog_str(&bfad->plog_buf, BFA_PL_MID_DRVR, BFA_PL_EID_DRIVER_START,
863 0, "Driver Attach");
864
865 bfa_attach(&bfad->bfa, bfad, &bfad->ioc_cfg, &bfad->meminfo,
866 &bfad->hal_pcidev);
867
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700868 /* FCS INIT */
Jing Huang7725ccf2009-09-23 17:46:15 -0700869 spin_lock_irqsave(&bfad->bfad_lock, flags);
Maggie Zhangf7f738122010-12-09 19:08:43 -0800870 bfad->bfa_fcs.trcmod = bfad->trcmod;
Krishna Gudipati82794a22010-03-03 17:43:30 -0800871 bfa_fcs_attach(&bfad->bfa_fcs, &bfad->bfa, bfad, BFA_FALSE);
Maggie Zhangf7f738122010-12-09 19:08:43 -0800872 bfad->bfa_fcs.fdmi_enabled = fdmi_enable;
Krishna Gudipati75332a72011-06-13 15:54:31 -0700873 bfa_fcs_init(&bfad->bfa_fcs);
Jing Huang7725ccf2009-09-23 17:46:15 -0700874 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
875
876 bfad->bfad_flags |= BFAD_DRV_INIT_DONE;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700877
Krishna Gudipati75332a72011-06-13 15:54:31 -0700878 /* configure base port */
879 rc = bfad_cfg_pport(bfad, BFA_LPORT_ROLE_FCP_IM);
880 if (rc != BFA_STATUS_OK)
881 goto out_cfg_pport_fail;
882
Jing Huang7725ccf2009-09-23 17:46:15 -0700883 return BFA_STATUS_OK;
884
Krishna Gudipati75332a72011-06-13 15:54:31 -0700885out_cfg_pport_fail:
886 /* fcs exit - on cfg pport failure */
887 spin_lock_irqsave(&bfad->bfad_lock, flags);
888 init_completion(&bfad->comp);
889 bfad->pport.flags |= BFAD_PORT_DELETE;
890 bfa_fcs_exit(&bfad->bfa_fcs);
891 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
892 wait_for_completion(&bfad->comp);
893 /* bfa detach - free hal memory */
894 bfa_detach(&bfad->bfa);
895 bfad_hal_mem_release(bfad);
Jing Huang7725ccf2009-09-23 17:46:15 -0700896out_hal_mem_alloc_failure:
897 return BFA_STATUS_FAILED;
898}
899
900void
901bfad_drv_uninit(struct bfad_s *bfad)
902{
Krishna Gudipatie6714322010-03-03 17:44:02 -0800903 unsigned long flags;
904
905 spin_lock_irqsave(&bfad->bfad_lock, flags);
906 init_completion(&bfad->comp);
Maggie Zhangf7f738122010-12-09 19:08:43 -0800907 bfa_iocfc_stop(&bfad->bfa);
Krishna Gudipatie6714322010-03-03 17:44:02 -0800908 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
909 wait_for_completion(&bfad->comp);
910
Jing Huang7725ccf2009-09-23 17:46:15 -0700911 del_timer_sync(&bfad->hal_tmo);
912 bfa_isr_disable(&bfad->bfa);
913 bfa_detach(&bfad->bfa);
914 bfad_remove_intr(bfad);
Jing Huang7725ccf2009-09-23 17:46:15 -0700915 bfad_hal_mem_release(bfad);
Krishna Gudipatie6714322010-03-03 17:44:02 -0800916
917 bfad->bfad_flags &= ~BFAD_DRV_INIT_DONE;
Jing Huang7725ccf2009-09-23 17:46:15 -0700918}
919
920void
921bfad_drv_start(struct bfad_s *bfad)
922{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700923 unsigned long flags;
Jing Huang7725ccf2009-09-23 17:46:15 -0700924
925 spin_lock_irqsave(&bfad->bfad_lock, flags);
Maggie Zhangf7f738122010-12-09 19:08:43 -0800926 bfa_iocfc_start(&bfad->bfa);
Krishna Gudipati75332a72011-06-13 15:54:31 -0700927 bfa_fcs_pbc_vport_init(&bfad->bfa_fcs);
Maggie Zhangf7f738122010-12-09 19:08:43 -0800928 bfa_fcs_fabric_modstart(&bfad->bfa_fcs);
Jing Huang7725ccf2009-09-23 17:46:15 -0700929 bfad->bfad_flags |= BFAD_HAL_START_DONE;
930 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
931
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700932 if (bfad->im)
933 flush_workqueue(bfad->im->drv_workq);
Jing Huang7725ccf2009-09-23 17:46:15 -0700934}
935
936void
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700937bfad_fcs_stop(struct bfad_s *bfad)
Jing Huang7725ccf2009-09-23 17:46:15 -0700938{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700939 unsigned long flags;
Jing Huang7725ccf2009-09-23 17:46:15 -0700940
941 spin_lock_irqsave(&bfad->bfad_lock, flags);
942 init_completion(&bfad->comp);
943 bfad->pport.flags |= BFAD_PORT_DELETE;
944 bfa_fcs_exit(&bfad->bfa_fcs);
945 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
946 wait_for_completion(&bfad->comp);
947
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700948 bfa_sm_send_event(bfad, BFAD_E_FCS_EXIT_COMP);
949}
950
951void
952bfad_stop(struct bfad_s *bfad)
953{
954 unsigned long flags;
955
Jing Huang7725ccf2009-09-23 17:46:15 -0700956 spin_lock_irqsave(&bfad->bfad_lock, flags);
957 init_completion(&bfad->comp);
Maggie Zhangf7f738122010-12-09 19:08:43 -0800958 bfa_iocfc_stop(&bfad->bfa);
Jing Huang7725ccf2009-09-23 17:46:15 -0700959 bfad->bfad_flags &= ~BFAD_HAL_START_DONE;
960 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
961 wait_for_completion(&bfad->comp);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700962
963 bfa_sm_send_event(bfad, BFAD_E_EXIT_COMP);
Jing Huang7725ccf2009-09-23 17:46:15 -0700964}
965
966bfa_status_t
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700967bfad_cfg_pport(struct bfad_s *bfad, enum bfa_lport_role role)
Jing Huang7725ccf2009-09-23 17:46:15 -0700968{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700969 int rc = BFA_STATUS_OK;
Jing Huang7725ccf2009-09-23 17:46:15 -0700970
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700971 /* Allocate scsi_host for the physical port */
972 if ((supported_fc4s & BFA_LPORT_ROLE_FCP_IM) &&
973 (role & BFA_LPORT_ROLE_FCP_IM)) {
Jing Huang7725ccf2009-09-23 17:46:15 -0700974 if (bfad->pport.im_port == NULL) {
975 rc = BFA_STATUS_FAILED;
976 goto out;
977 }
978
Jing Huangb5042932010-03-19 11:05:39 -0700979 rc = bfad_im_scsi_host_alloc(bfad, bfad->pport.im_port,
980 &bfad->pcidev->dev);
Jing Huang7725ccf2009-09-23 17:46:15 -0700981 if (rc != BFA_STATUS_OK)
982 goto out;
983
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700984 bfad->pport.roles |= BFA_LPORT_ROLE_FCP_IM;
Jing Huang7725ccf2009-09-23 17:46:15 -0700985 }
986
987 bfad->bfad_flags |= BFAD_CFG_PPORT_DONE;
988
989out:
990 return rc;
991}
992
993void
994bfad_uncfg_pport(struct bfad_s *bfad)
995{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700996 if ((supported_fc4s & BFA_LPORT_ROLE_FCP_IM) &&
997 (bfad->pport.roles & BFA_LPORT_ROLE_FCP_IM)) {
Jing Huang7725ccf2009-09-23 17:46:15 -0700998 bfad_im_scsi_host_free(bfad, bfad->pport.im_port);
999 bfad_im_port_clean(bfad->pport.im_port);
1000 kfree(bfad->pport.im_port);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001001 bfad->pport.roles &= ~BFA_LPORT_ROLE_FCP_IM;
Jing Huang7725ccf2009-09-23 17:46:15 -07001002 }
1003
1004 bfad->bfad_flags &= ~BFAD_CFG_PPORT_DONE;
1005}
1006
Krishna Gudipatie6714322010-03-03 17:44:02 -08001007bfa_status_t
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001008bfad_start_ops(struct bfad_s *bfad) {
1009
1010 int retval;
1011 unsigned long flags;
1012 struct bfad_vport_s *vport, *vport_new;
1013 struct bfa_fcs_driver_info_s driver_info;
1014
Krishna Gudipati61e62e22011-06-24 20:29:07 -07001015 /* Limit min/max. xfer size to [64k-32MB] */
1016 if (max_xfer_size < BFAD_MIN_SECTORS >> 1)
1017 max_xfer_size = BFAD_MIN_SECTORS >> 1;
1018 if (max_xfer_size > BFAD_MAX_SECTORS >> 1)
1019 max_xfer_size = BFAD_MAX_SECTORS >> 1;
1020
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001021 /* Fill the driver_info info to fcs*/
1022 memset(&driver_info, 0, sizeof(driver_info));
1023 strncpy(driver_info.version, BFAD_DRIVER_VERSION,
1024 sizeof(driver_info.version) - 1);
1025 if (host_name)
1026 strncpy(driver_info.host_machine_name, host_name,
1027 sizeof(driver_info.host_machine_name) - 1);
1028 if (os_name)
1029 strncpy(driver_info.host_os_name, os_name,
1030 sizeof(driver_info.host_os_name) - 1);
1031 if (os_patch)
1032 strncpy(driver_info.host_os_patch, os_patch,
1033 sizeof(driver_info.host_os_patch) - 1);
1034
1035 strncpy(driver_info.os_device_name, bfad->pci_name,
Dan Carpenter30ac3792013-01-10 12:06:20 +03001036 sizeof(driver_info.os_device_name) - 1);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001037
Krishna Gudipati75332a72011-06-13 15:54:31 -07001038 /* FCS driver info init */
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001039 spin_lock_irqsave(&bfad->bfad_lock, flags);
1040 bfa_fcs_driver_info_init(&bfad->bfa_fcs, &driver_info);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001041 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
Krishna Gudipatie6714322010-03-03 17:44:02 -08001042
Krishna Gudipati75332a72011-06-13 15:54:31 -07001043 /*
1044 * FCS update cfg - reset the pwwn/nwwn of fabric base logical port
1045 * with values learned during bfa_init firmware GETATTR REQ.
1046 */
1047 bfa_fcs_update_cfg(&bfad->bfa_fcs);
1048
1049 /* Setup fc host fixed attribute if the lk supports */
1050 bfad_fc_host_init(bfad->pport.im_port);
Krishna Gudipatie6714322010-03-03 17:44:02 -08001051
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001052 /* BFAD level FC4 IM specific resource allocation */
1053 retval = bfad_im_probe(bfad);
1054 if (retval != BFA_STATUS_OK) {
1055 printk(KERN_WARNING "bfad_im_probe failed\n");
1056 if (bfa_sm_cmp_state(bfad, bfad_sm_initializing))
1057 bfa_sm_set_state(bfad, bfad_sm_failed);
1058 bfad_im_probe_undo(bfad);
1059 bfad->bfad_flags &= ~BFAD_FC4_PROBE_DONE;
1060 bfad_uncfg_pport(bfad);
1061 bfad_stop(bfad);
1062 return BFA_STATUS_FAILED;
1063 } else
1064 bfad->bfad_flags |= BFAD_FC4_PROBE_DONE;
1065
Krishna Gudipatie6714322010-03-03 17:44:02 -08001066 bfad_drv_start(bfad);
1067
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001068 /* Complete pbc vport create */
1069 list_for_each_entry_safe(vport, vport_new, &bfad->pbc_vport_list,
1070 list_entry) {
Jing Huangd9883542010-07-08 19:46:26 -07001071 struct fc_vport_identifiers vid;
1072 struct fc_vport *fc_vport;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001073 char pwwn_buf[BFA_STRING_32];
Jing Huangd9883542010-07-08 19:46:26 -07001074
1075 memset(&vid, 0, sizeof(vid));
1076 vid.roles = FC_PORT_ROLE_FCP_INITIATOR;
1077 vid.vport_type = FC_PORTTYPE_NPIV;
1078 vid.disable = false;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001079 vid.node_name = wwn_to_u64((u8 *)
1080 (&((vport->fcs_vport).lport.port_cfg.nwwn)));
1081 vid.port_name = wwn_to_u64((u8 *)
1082 (&((vport->fcs_vport).lport.port_cfg.pwwn)));
Jing Huangd9883542010-07-08 19:46:26 -07001083 fc_vport = fc_vport_create(bfad->pport.im_port->shost, 0, &vid);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001084 if (!fc_vport) {
1085 wwn2str(pwwn_buf, vid.port_name);
Jing Huangd9883542010-07-08 19:46:26 -07001086 printk(KERN_WARNING "bfad%d: failed to create pbc vport"
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001087 " %s\n", bfad->inst_no, pwwn_buf);
1088 }
1089 list_del(&vport->list_entry);
1090 kfree(vport);
Jing Huangd9883542010-07-08 19:46:26 -07001091 }
1092
Krishna Gudipatie6714322010-03-03 17:44:02 -08001093 /*
1094 * If bfa_linkup_delay is set to -1 default; try to retrive the
Maggie Zhangf16a1752010-12-09 19:12:32 -08001095 * value using the bfad_get_linkup_delay(); else use the
Krishna Gudipatie6714322010-03-03 17:44:02 -08001096 * passed in module param value as the bfa_linkup_delay.
1097 */
1098 if (bfa_linkup_delay < 0) {
Maggie Zhangf16a1752010-12-09 19:12:32 -08001099 bfa_linkup_delay = bfad_get_linkup_delay(bfad);
1100 bfad_rport_online_wait(bfad);
Krishna Gudipatie6714322010-03-03 17:44:02 -08001101 bfa_linkup_delay = -1;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001102 } else
Maggie Zhangf16a1752010-12-09 19:12:32 -08001103 bfad_rport_online_wait(bfad);
Krishna Gudipatie6714322010-03-03 17:44:02 -08001104
Jing Huang88166242010-12-09 17:11:53 -08001105 BFA_LOG(KERN_INFO, bfad, bfa_log_level, "bfa device claimed\n");
Krishna Gudipatie6714322010-03-03 17:44:02 -08001106
1107 return BFA_STATUS_OK;
Krishna Gudipatie6714322010-03-03 17:44:02 -08001108}
1109
1110int
Jing Huangd9883542010-07-08 19:46:26 -07001111bfad_worker(void *ptr)
Krishna Gudipatie6714322010-03-03 17:44:02 -08001112{
1113 struct bfad_s *bfad;
1114 unsigned long flags;
1115
1116 bfad = (struct bfad_s *)ptr;
1117
1118 while (!kthread_should_stop()) {
1119
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001120 /* Send event BFAD_E_INIT_SUCCESS */
1121 bfa_sm_send_event(bfad, BFAD_E_INIT_SUCCESS);
Krishna Gudipatie6714322010-03-03 17:44:02 -08001122
1123 spin_lock_irqsave(&bfad->bfad_lock, flags);
1124 bfad->bfad_tsk = NULL;
1125 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1126
1127 break;
1128 }
1129
1130 return 0;
1131}
1132
Jing Huang5fbe25c2010-10-18 17:17:23 -07001133/*
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001134 * BFA driver interrupt functions
1135 */
1136irqreturn_t
1137bfad_intx(int irq, void *dev_id)
1138{
1139 struct bfad_s *bfad = dev_id;
1140 struct list_head doneq;
1141 unsigned long flags;
1142 bfa_boolean_t rc;
1143
1144 spin_lock_irqsave(&bfad->bfad_lock, flags);
1145 rc = bfa_intx(&bfad->bfa);
1146 if (!rc) {
1147 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1148 return IRQ_NONE;
1149 }
1150
1151 bfa_comp_deq(&bfad->bfa, &doneq);
1152 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1153
1154 if (!list_empty(&doneq)) {
1155 bfa_comp_process(&bfad->bfa, &doneq);
1156
1157 spin_lock_irqsave(&bfad->bfad_lock, flags);
1158 bfa_comp_free(&bfad->bfa, &doneq);
1159 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001160 }
1161
1162 return IRQ_HANDLED;
1163
1164}
1165
1166static irqreturn_t
1167bfad_msix(int irq, void *dev_id)
1168{
1169 struct bfad_msix_s *vec = dev_id;
1170 struct bfad_s *bfad = vec->bfad;
1171 struct list_head doneq;
1172 unsigned long flags;
1173
1174 spin_lock_irqsave(&bfad->bfad_lock, flags);
1175
1176 bfa_msix(&bfad->bfa, vec->msix.entry);
1177 bfa_comp_deq(&bfad->bfa, &doneq);
1178 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1179
1180 if (!list_empty(&doneq)) {
1181 bfa_comp_process(&bfad->bfa, &doneq);
1182
1183 spin_lock_irqsave(&bfad->bfad_lock, flags);
1184 bfa_comp_free(&bfad->bfa, &doneq);
1185 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1186 }
1187
1188 return IRQ_HANDLED;
1189}
1190
Jing Huang5fbe25c2010-10-18 17:17:23 -07001191/*
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001192 * Initialize the MSIX entry table.
1193 */
1194static void
1195bfad_init_msix_entry(struct bfad_s *bfad, struct msix_entry *msix_entries,
1196 int mask, int max_bit)
1197{
1198 int i;
1199 int match = 0x00000001;
1200
1201 for (i = 0, bfad->nvec = 0; i < MAX_MSIX_ENTRY; i++) {
1202 if (mask & match) {
1203 bfad->msix_tab[bfad->nvec].msix.entry = i;
1204 bfad->msix_tab[bfad->nvec].bfad = bfad;
1205 msix_entries[bfad->nvec].entry = i;
1206 bfad->nvec++;
1207 }
1208
1209 match <<= 1;
1210 }
1211
1212}
1213
1214int
1215bfad_install_msix_handler(struct bfad_s *bfad)
1216{
1217 int i, error = 0;
1218
1219 for (i = 0; i < bfad->nvec; i++) {
1220 sprintf(bfad->msix_tab[i].name, "bfa-%s-%s",
1221 bfad->pci_name,
Krishna Gudipati11189202011-06-13 15:50:35 -07001222 ((bfa_asic_id_cb(bfad->hal_pcidev.device_id)) ?
1223 msix_name_cb[i] : msix_name_ct[i]));
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001224
1225 error = request_irq(bfad->msix_tab[i].msix.vector,
1226 (irq_handler_t) bfad_msix, 0,
1227 bfad->msix_tab[i].name, &bfad->msix_tab[i]);
1228 bfa_trc(bfad, i);
1229 bfa_trc(bfad, bfad->msix_tab[i].msix.vector);
1230 if (error) {
1231 int j;
1232
1233 for (j = 0; j < i; j++)
1234 free_irq(bfad->msix_tab[j].msix.vector,
1235 &bfad->msix_tab[j]);
1236
Krishna Gudipati61e62e22011-06-24 20:29:07 -07001237 bfad->bfad_flags &= ~BFAD_MSIX_ON;
1238 pci_disable_msix(bfad->pcidev);
1239
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001240 return 1;
1241 }
1242 }
1243
1244 return 0;
1245}
1246
Jing Huang5fbe25c2010-10-18 17:17:23 -07001247/*
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001248 * Setup MSIX based interrupt.
1249 */
1250int
1251bfad_setup_intr(struct bfad_s *bfad)
1252{
1253 int error = 0;
1254 u32 mask = 0, i, num_bit = 0, max_bit = 0;
1255 struct msix_entry msix_entries[MAX_MSIX_ENTRY];
1256 struct pci_dev *pdev = bfad->pcidev;
Krishna Gudipati10a07372011-06-24 20:23:38 -07001257 u16 reg;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001258
1259 /* Call BFA to get the msix map for this PCI function. */
1260 bfa_msix_getvecs(&bfad->bfa, &mask, &num_bit, &max_bit);
1261
1262 /* Set up the msix entry table */
1263 bfad_init_msix_entry(bfad, msix_entries, mask, max_bit);
1264
Krishna Gudipati11189202011-06-13 15:50:35 -07001265 if ((bfa_asic_id_ctc(pdev->device) && !msix_disable_ct) ||
1266 (bfa_asic_id_cb(pdev->device) && !msix_disable_cb)) {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001267
1268 error = pci_enable_msix(bfad->pcidev, msix_entries, bfad->nvec);
1269 if (error) {
Krishna Gudipati881c1b32012-08-22 19:52:02 -07001270 /* In CT1 & CT2, try to allocate just one vector */
1271 if (bfa_asic_id_ctc(pdev->device)) {
1272 printk(KERN_WARNING "bfa %s: trying one msix "
1273 "vector failed to allocate %d[%d]\n",
1274 bfad->pci_name, bfad->nvec, error);
1275 bfad->nvec = 1;
1276 error = pci_enable_msix(bfad->pcidev,
1277 msix_entries, bfad->nvec);
1278 }
1279
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001280 /*
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 */
Krishna Gudipati881c1b32012-08-22 19:52:02 -07001289 if (error) {
1290 printk(KERN_WARNING "bfad%d: "
1291 "pci_enable_msix failed (%d), "
1292 "use line based.\n",
1293 bfad->inst_no, error);
1294 goto line_based;
1295 }
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001296 }
1297
Krishna Gudipati10a07372011-06-24 20:23:38 -07001298 /* Disable INTX in MSI-X mode */
1299 pci_read_config_word(pdev, PCI_COMMAND, &reg);
1300
1301 if (!(reg & PCI_COMMAND_INTX_DISABLE))
1302 pci_write_config_word(pdev, PCI_COMMAND,
1303 reg | PCI_COMMAND_INTX_DISABLE);
1304
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001305 /* Save the vectors */
1306 for (i = 0; i < bfad->nvec; i++) {
1307 bfa_trc(bfad, msix_entries[i].vector);
1308 bfad->msix_tab[i].msix.vector = msix_entries[i].vector;
1309 }
1310
1311 bfa_msix_init(&bfad->bfa, bfad->nvec);
1312
1313 bfad->bfad_flags |= BFAD_MSIX_ON;
1314
1315 return error;
1316 }
1317
1318line_based:
1319 error = 0;
1320 if (request_irq
1321 (bfad->pcidev->irq, (irq_handler_t) bfad_intx, BFAD_IRQ_FLAGS,
1322 BFAD_DRIVER_NAME, bfad) != 0) {
1323 /* Enable interrupt handler failed */
1324 return 1;
1325 }
Krishna Gudipati61e62e22011-06-24 20:29:07 -07001326 bfad->bfad_flags |= BFAD_INTX_ON;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001327
1328 return error;
1329}
1330
1331void
1332bfad_remove_intr(struct bfad_s *bfad)
1333{
1334 int i;
1335
1336 if (bfad->bfad_flags & BFAD_MSIX_ON) {
1337 for (i = 0; i < bfad->nvec; i++)
1338 free_irq(bfad->msix_tab[i].msix.vector,
1339 &bfad->msix_tab[i]);
1340
1341 pci_disable_msix(bfad->pcidev);
1342 bfad->bfad_flags &= ~BFAD_MSIX_ON;
Krishna Gudipati61e62e22011-06-24 20:29:07 -07001343 } else if (bfad->bfad_flags & BFAD_INTX_ON) {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001344 free_irq(bfad->pcidev->irq, bfad);
1345 }
1346}
Jing Huang7725ccf2009-09-23 17:46:15 -07001347
Jing Huang5fbe25c2010-10-18 17:17:23 -07001348/*
Jing Huang7725ccf2009-09-23 17:46:15 -07001349 * PCI probe entry.
1350 */
1351int
1352bfad_pci_probe(struct pci_dev *pdev, const struct pci_device_id *pid)
1353{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001354 struct bfad_s *bfad;
Krishna Gudipati7826f302011-07-20 16:59:13 -07001355 int error = -ENODEV, retval, i;
Jing Huang7725ccf2009-09-23 17:46:15 -07001356
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001357 /* For single port cards - only claim function 0 */
Krishna Gudipati8b070b42011-06-13 15:52:40 -07001358 if ((pdev->device == BFA_PCI_DEVICE_ID_FC_8G1P) &&
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001359 (PCI_FUNC(pdev->devfn) != 0))
Jing Huang7725ccf2009-09-23 17:46:15 -07001360 return -ENODEV;
1361
Jing Huang7725ccf2009-09-23 17:46:15 -07001362 bfad = kzalloc(sizeof(struct bfad_s), GFP_KERNEL);
1363 if (!bfad) {
1364 error = -ENOMEM;
1365 goto out;
1366 }
1367
1368 bfad->trcmod = kzalloc(sizeof(struct bfa_trc_mod_s), GFP_KERNEL);
1369 if (!bfad->trcmod) {
1370 printk(KERN_WARNING "Error alloc trace buffer!\n");
1371 error = -ENOMEM;
1372 goto out_alloc_trace_failure;
1373 }
1374
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001375 /* TRACE INIT */
Jing Huang7725ccf2009-09-23 17:46:15 -07001376 bfa_trc_init(bfad->trcmod);
1377 bfa_trc(bfad, bfad_inst);
1378
Krishna Gudipati7826f302011-07-20 16:59:13 -07001379 /* AEN INIT */
1380 INIT_LIST_HEAD(&bfad->free_aen_q);
1381 INIT_LIST_HEAD(&bfad->active_aen_q);
1382 for (i = 0; i < BFA_AEN_MAX_ENTRY; i++)
1383 list_add_tail(&bfad->aen_list[i].qe, &bfad->free_aen_q);
1384
Jing Huang7725ccf2009-09-23 17:46:15 -07001385 if (!(bfad_load_fwimg(pdev))) {
Jing Huang7725ccf2009-09-23 17:46:15 -07001386 kfree(bfad->trcmod);
1387 goto out_alloc_trace_failure;
1388 }
1389
1390 retval = bfad_pci_init(pdev, bfad);
1391 if (retval) {
1392 printk(KERN_WARNING "bfad_pci_init failure!\n");
1393 error = retval;
1394 goto out_pci_init_failure;
1395 }
1396
1397 mutex_lock(&bfad_mutex);
1398 bfad->inst_no = bfad_inst++;
1399 list_add_tail(&bfad->list_entry, &bfad_list);
1400 mutex_unlock(&bfad_mutex);
1401
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001402 /* Initializing the state machine: State set to uninit */
1403 bfa_sm_set_state(bfad, bfad_sm_uninit);
1404
Jing Huang7725ccf2009-09-23 17:46:15 -07001405 spin_lock_init(&bfad->bfad_lock);
Kyle McMartin0b1017a2012-07-11 12:42:55 -04001406 spin_lock_init(&bfad->bfad_aen_spinlock);
1407
Jing Huang7725ccf2009-09-23 17:46:15 -07001408 pci_set_drvdata(pdev, bfad);
1409
1410 bfad->ref_count = 0;
1411 bfad->pport.bfad = bfad;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001412 INIT_LIST_HEAD(&bfad->pbc_vport_list);
Krishna Gudipati5b7db7a2011-12-20 18:58:32 -08001413 INIT_LIST_HEAD(&bfad->vport_list);
Krishna Gudipatie6714322010-03-03 17:44:02 -08001414
Krishna Gudipati7c38c052011-04-14 16:50:35 -07001415 /* Setup the debugfs node for this bfad */
1416 if (bfa_debugfs_enable)
1417 bfad_debugfs_init(&bfad->pport);
1418
Jing Huang7725ccf2009-09-23 17:46:15 -07001419 retval = bfad_drv_init(bfad);
1420 if (retval != BFA_STATUS_OK)
1421 goto out_drv_init_failure;
Jing Huang7725ccf2009-09-23 17:46:15 -07001422
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001423 bfa_sm_send_event(bfad, BFAD_E_CREATE);
Jing Huang7725ccf2009-09-23 17:46:15 -07001424
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001425 if (bfa_sm_cmp_state(bfad, bfad_sm_uninit))
1426 goto out_bfad_sm_failure;
Jing Huang7725ccf2009-09-23 17:46:15 -07001427
Jing Huang7725ccf2009-09-23 17:46:15 -07001428 return 0;
1429
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001430out_bfad_sm_failure:
1431 bfa_detach(&bfad->bfa);
1432 bfad_hal_mem_release(bfad);
Jing Huang7725ccf2009-09-23 17:46:15 -07001433out_drv_init_failure:
Krishna Gudipati7c38c052011-04-14 16:50:35 -07001434 /* Remove the debugfs node for this bfad */
1435 kfree(bfad->regdata);
1436 bfad_debugfs_exit(&bfad->pport);
Jing Huang7725ccf2009-09-23 17:46:15 -07001437 mutex_lock(&bfad_mutex);
1438 bfad_inst--;
1439 list_del(&bfad->list_entry);
1440 mutex_unlock(&bfad_mutex);
1441 bfad_pci_uninit(pdev, bfad);
1442out_pci_init_failure:
1443 kfree(bfad->trcmod);
1444out_alloc_trace_failure:
1445 kfree(bfad);
1446out:
1447 return error;
1448}
1449
Jing Huang5fbe25c2010-10-18 17:17:23 -07001450/*
Jing Huang7725ccf2009-09-23 17:46:15 -07001451 * PCI remove entry.
1452 */
1453void
1454bfad_pci_remove(struct pci_dev *pdev)
1455{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001456 struct bfad_s *bfad = pci_get_drvdata(pdev);
1457 unsigned long flags;
Jing Huang7725ccf2009-09-23 17:46:15 -07001458
1459 bfa_trc(bfad, bfad->inst_no);
1460
Krishna Gudipatie6714322010-03-03 17:44:02 -08001461 spin_lock_irqsave(&bfad->bfad_lock, flags);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001462 if (bfad->bfad_tsk != NULL) {
1463 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
Krishna Gudipatie6714322010-03-03 17:44:02 -08001464 kthread_stop(bfad->bfad_tsk);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001465 } else {
Jing Huang7725ccf2009-09-23 17:46:15 -07001466 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
Jing Huang7725ccf2009-09-23 17:46:15 -07001467 }
1468
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001469 /* Send Event BFAD_E_STOP */
1470 bfa_sm_send_event(bfad, BFAD_E_STOP);
Jing Huang7725ccf2009-09-23 17:46:15 -07001471
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001472 /* Driver detach and dealloc mem */
Jing Huang7725ccf2009-09-23 17:46:15 -07001473 spin_lock_irqsave(&bfad->bfad_lock, flags);
1474 bfa_detach(&bfad->bfa);
1475 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1476 bfad_hal_mem_release(bfad);
Jing Huang7725ccf2009-09-23 17:46:15 -07001477
Krishna Gudipati7c38c052011-04-14 16:50:35 -07001478 /* Remove the debugfs node for this bfad */
1479 kfree(bfad->regdata);
1480 bfad_debugfs_exit(&bfad->pport);
1481
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001482 /* Cleaning the BFAD instance */
Jing Huang7725ccf2009-09-23 17:46:15 -07001483 mutex_lock(&bfad_mutex);
1484 bfad_inst--;
1485 list_del(&bfad->list_entry);
1486 mutex_unlock(&bfad_mutex);
1487 bfad_pci_uninit(pdev, bfad);
1488
1489 kfree(bfad->trcmod);
1490 kfree(bfad);
1491}
1492
Krishna Gudipati881c1b32012-08-22 19:52:02 -07001493/*
1494 * PCI Error Recovery entry, error detected.
1495 */
1496static pci_ers_result_t
1497bfad_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
1498{
1499 struct bfad_s *bfad = pci_get_drvdata(pdev);
1500 unsigned long flags;
1501 pci_ers_result_t ret = PCI_ERS_RESULT_NONE;
1502
1503 dev_printk(KERN_ERR, &pdev->dev,
1504 "error detected state: %d - flags: 0x%x\n",
1505 state, bfad->bfad_flags);
1506
1507 switch (state) {
1508 case pci_channel_io_normal: /* non-fatal error */
1509 spin_lock_irqsave(&bfad->bfad_lock, flags);
1510 bfad->bfad_flags &= ~BFAD_EEH_BUSY;
1511 /* Suspend/fail all bfa operations */
1512 bfa_ioc_suspend(&bfad->bfa.ioc);
1513 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1514 del_timer_sync(&bfad->hal_tmo);
1515 ret = PCI_ERS_RESULT_CAN_RECOVER;
1516 break;
1517 case pci_channel_io_frozen: /* fatal error */
1518 init_completion(&bfad->comp);
1519 spin_lock_irqsave(&bfad->bfad_lock, flags);
1520 bfad->bfad_flags |= BFAD_EEH_BUSY;
1521 /* Suspend/fail all bfa operations */
1522 bfa_ioc_suspend(&bfad->bfa.ioc);
1523 bfa_fcs_stop(&bfad->bfa_fcs);
1524 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1525 wait_for_completion(&bfad->comp);
1526
1527 bfad_remove_intr(bfad);
1528 del_timer_sync(&bfad->hal_tmo);
1529 pci_disable_device(pdev);
1530 ret = PCI_ERS_RESULT_NEED_RESET;
1531 break;
1532 case pci_channel_io_perm_failure: /* PCI Card is DEAD */
1533 spin_lock_irqsave(&bfad->bfad_lock, flags);
1534 bfad->bfad_flags |= BFAD_EEH_BUSY |
1535 BFAD_EEH_PCI_CHANNEL_IO_PERM_FAILURE;
1536 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1537
1538 /* If the error_detected handler is called with the reason
1539 * pci_channel_io_perm_failure - it will subsequently call
1540 * pci_remove() entry point to remove the pci device from the
1541 * system - So defer the cleanup to pci_remove(); cleaning up
1542 * here causes inconsistent state during pci_remove().
1543 */
1544 ret = PCI_ERS_RESULT_DISCONNECT;
1545 break;
1546 default:
1547 WARN_ON(1);
1548 }
1549
1550 return ret;
1551}
1552
1553int
1554restart_bfa(struct bfad_s *bfad)
1555{
1556 unsigned long flags;
1557 struct pci_dev *pdev = bfad->pcidev;
1558
1559 bfa_attach(&bfad->bfa, bfad, &bfad->ioc_cfg,
1560 &bfad->meminfo, &bfad->hal_pcidev);
1561
1562 /* Enable Interrupt and wait bfa_init completion */
1563 if (bfad_setup_intr(bfad)) {
1564 dev_printk(KERN_WARNING, &pdev->dev,
1565 "%s: bfad_setup_intr failed\n", bfad->pci_name);
1566 bfa_sm_send_event(bfad, BFAD_E_INTR_INIT_FAILED);
1567 return -1;
1568 }
1569
1570 init_completion(&bfad->comp);
1571 spin_lock_irqsave(&bfad->bfad_lock, flags);
1572 bfa_iocfc_init(&bfad->bfa);
1573 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1574
1575 /* Set up interrupt handler for each vectors */
1576 if ((bfad->bfad_flags & BFAD_MSIX_ON) &&
1577 bfad_install_msix_handler(bfad))
1578 dev_printk(KERN_WARNING, &pdev->dev,
1579 "%s: install_msix failed.\n", bfad->pci_name);
1580
1581 bfad_init_timer(bfad);
1582 wait_for_completion(&bfad->comp);
1583 bfad_drv_start(bfad);
1584
1585 return 0;
1586}
1587
1588/*
1589 * PCI Error Recovery entry, re-initialize the chip.
1590 */
1591static pci_ers_result_t
1592bfad_pci_slot_reset(struct pci_dev *pdev)
1593{
1594 struct bfad_s *bfad = pci_get_drvdata(pdev);
1595 u8 byte;
1596
1597 dev_printk(KERN_ERR, &pdev->dev,
1598 "bfad_pci_slot_reset flags: 0x%x\n", bfad->bfad_flags);
1599
1600 if (pci_enable_device(pdev)) {
1601 dev_printk(KERN_ERR, &pdev->dev, "Cannot re-enable "
1602 "PCI device after reset.\n");
1603 return PCI_ERS_RESULT_DISCONNECT;
1604 }
1605
1606 pci_restore_state(pdev);
1607
1608 /*
1609 * Read some byte (e.g. DMA max. payload size which can't
1610 * be 0xff any time) to make sure - we did not hit another PCI error
1611 * in the middle of recovery. If we did, then declare permanent failure.
1612 */
1613 pci_read_config_byte(pdev, 0x68, &byte);
1614 if (byte == 0xff) {
1615 dev_printk(KERN_ERR, &pdev->dev,
1616 "slot_reset failed ... got another PCI error !\n");
1617 goto out_disable_device;
1618 }
1619
1620 pci_save_state(pdev);
1621 pci_set_master(pdev);
1622
1623 if (pci_set_dma_mask(bfad->pcidev, DMA_BIT_MASK(64)) != 0)
1624 if (pci_set_dma_mask(bfad->pcidev, DMA_BIT_MASK(32)) != 0)
1625 goto out_disable_device;
1626
1627 pci_cleanup_aer_uncorrect_error_status(pdev);
1628
1629 if (restart_bfa(bfad) == -1)
1630 goto out_disable_device;
1631
1632 pci_enable_pcie_error_reporting(pdev);
1633 dev_printk(KERN_WARNING, &pdev->dev,
1634 "slot_reset completed flags: 0x%x!\n", bfad->bfad_flags);
1635
1636 return PCI_ERS_RESULT_RECOVERED;
1637
1638out_disable_device:
1639 pci_disable_device(pdev);
1640 return PCI_ERS_RESULT_DISCONNECT;
1641}
1642
1643static pci_ers_result_t
1644bfad_pci_mmio_enabled(struct pci_dev *pdev)
1645{
1646 unsigned long flags;
1647 struct bfad_s *bfad = pci_get_drvdata(pdev);
1648
1649 dev_printk(KERN_INFO, &pdev->dev, "mmio_enabled\n");
1650
1651 /* Fetch FW diagnostic information */
1652 bfa_ioc_debug_save_ftrc(&bfad->bfa.ioc);
1653
1654 /* Cancel all pending IOs */
1655 spin_lock_irqsave(&bfad->bfad_lock, flags);
1656 init_completion(&bfad->comp);
1657 bfa_fcs_stop(&bfad->bfa_fcs);
1658 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1659 wait_for_completion(&bfad->comp);
1660
1661 bfad_remove_intr(bfad);
1662 del_timer_sync(&bfad->hal_tmo);
1663 pci_disable_device(pdev);
1664
1665 return PCI_ERS_RESULT_NEED_RESET;
1666}
1667
1668static void
1669bfad_pci_resume(struct pci_dev *pdev)
1670{
1671 unsigned long flags;
1672 struct bfad_s *bfad = pci_get_drvdata(pdev);
1673
1674 dev_printk(KERN_WARNING, &pdev->dev, "resume\n");
1675
1676 /* wait until the link is online */
1677 bfad_rport_online_wait(bfad);
1678
1679 spin_lock_irqsave(&bfad->bfad_lock, flags);
1680 bfad->bfad_flags &= ~BFAD_EEH_BUSY;
1681 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1682}
1683
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001684struct pci_device_id bfad_id_table[] = {
Jing Huang7725ccf2009-09-23 17:46:15 -07001685 {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001686 .vendor = BFA_PCI_VENDOR_ID_BROCADE,
1687 .device = BFA_PCI_DEVICE_ID_FC_8G2P,
1688 .subvendor = PCI_ANY_ID,
1689 .subdevice = PCI_ANY_ID,
1690 },
Jing Huang7725ccf2009-09-23 17:46:15 -07001691 {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001692 .vendor = BFA_PCI_VENDOR_ID_BROCADE,
1693 .device = BFA_PCI_DEVICE_ID_FC_8G1P,
1694 .subvendor = PCI_ANY_ID,
1695 .subdevice = PCI_ANY_ID,
1696 },
Jing Huang7725ccf2009-09-23 17:46:15 -07001697 {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001698 .vendor = BFA_PCI_VENDOR_ID_BROCADE,
1699 .device = BFA_PCI_DEVICE_ID_CT,
1700 .subvendor = PCI_ANY_ID,
1701 .subdevice = PCI_ANY_ID,
1702 .class = (PCI_CLASS_SERIAL_FIBER << 8),
1703 .class_mask = ~0,
1704 },
Jing Huang293f82d2010-07-08 19:45:20 -07001705 {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001706 .vendor = BFA_PCI_VENDOR_ID_BROCADE,
1707 .device = BFA_PCI_DEVICE_ID_CT_FC,
1708 .subvendor = PCI_ANY_ID,
1709 .subdevice = PCI_ANY_ID,
1710 .class = (PCI_CLASS_SERIAL_FIBER << 8),
1711 .class_mask = ~0,
Jing Huang293f82d2010-07-08 19:45:20 -07001712 },
Krishna Gudipati11189202011-06-13 15:50:35 -07001713 {
1714 .vendor = BFA_PCI_VENDOR_ID_BROCADE,
1715 .device = BFA_PCI_DEVICE_ID_CT2,
1716 .subvendor = PCI_ANY_ID,
1717 .subdevice = PCI_ANY_ID,
1718 .class = (PCI_CLASS_SERIAL_FIBER << 8),
1719 .class_mask = ~0,
1720 },
Jing Huang7725ccf2009-09-23 17:46:15 -07001721
Vijaya Mohan Guvvaf9c867b2013-05-13 02:33:33 -07001722 {
1723 .vendor = BFA_PCI_VENDOR_ID_BROCADE,
1724 .device = BFA_PCI_DEVICE_ID_CT2_QUAD,
1725 .subvendor = PCI_ANY_ID,
1726 .subdevice = PCI_ANY_ID,
1727 .class = (PCI_CLASS_SERIAL_FIBER << 8),
1728 .class_mask = ~0,
1729 },
Jing Huang7725ccf2009-09-23 17:46:15 -07001730 {0, 0},
1731};
1732
1733MODULE_DEVICE_TABLE(pci, bfad_id_table);
1734
Krishna Gudipati881c1b32012-08-22 19:52:02 -07001735/*
1736 * PCI error recovery handlers.
1737 */
1738static struct pci_error_handlers bfad_err_handler = {
1739 .error_detected = bfad_pci_error_detected,
1740 .slot_reset = bfad_pci_slot_reset,
1741 .mmio_enabled = bfad_pci_mmio_enabled,
1742 .resume = bfad_pci_resume,
1743};
1744
Jing Huang7725ccf2009-09-23 17:46:15 -07001745static struct pci_driver bfad_pci_driver = {
1746 .name = BFAD_DRIVER_NAME,
1747 .id_table = bfad_id_table,
1748 .probe = bfad_pci_probe,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08001749 .remove = bfad_pci_remove,
Krishna Gudipati881c1b32012-08-22 19:52:02 -07001750 .err_handler = &bfad_err_handler,
Jing Huang7725ccf2009-09-23 17:46:15 -07001751};
1752
Jing Huang5fbe25c2010-10-18 17:17:23 -07001753/*
Jing Huang7725ccf2009-09-23 17:46:15 -07001754 * Driver module init.
1755 */
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001756static int __init
Jing Huang7725ccf2009-09-23 17:46:15 -07001757bfad_init(void)
1758{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001759 int error = 0;
Jing Huang7725ccf2009-09-23 17:46:15 -07001760
1761 printk(KERN_INFO "Brocade BFA FC/FCOE SCSI driver - version: %s\n",
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001762 BFAD_DRIVER_VERSION);
Jing Huang7725ccf2009-09-23 17:46:15 -07001763
1764 if (num_sgpgs > 0)
1765 num_sgpgs_parm = num_sgpgs;
1766
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001767 error = bfad_im_module_init();
Jing Huang7725ccf2009-09-23 17:46:15 -07001768 if (error) {
1769 error = -ENOMEM;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001770 printk(KERN_WARNING "bfad_im_module_init failure\n");
Jing Huang7725ccf2009-09-23 17:46:15 -07001771 goto ext;
1772 }
1773
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001774 if (strcmp(FCPI_NAME, " fcpim") == 0)
1775 supported_fc4s |= BFA_LPORT_ROLE_FCP_IM;
Jing Huang7725ccf2009-09-23 17:46:15 -07001776
Maggie Zhangf7f738122010-12-09 19:08:43 -08001777 bfa_auto_recover = ioc_auto_recover;
Jing Huang7725ccf2009-09-23 17:46:15 -07001778 bfa_fcs_rport_set_del_timeout(rport_del_timeout);
Krishna Gudipati61ba4392012-08-22 19:52:58 -07001779 bfa_fcs_rport_set_max_logins(max_rport_logins);
Jing Huang7725ccf2009-09-23 17:46:15 -07001780
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001781 error = pci_register_driver(&bfad_pci_driver);
Jing Huang7725ccf2009-09-23 17:46:15 -07001782 if (error) {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001783 printk(KERN_WARNING "pci_register_driver failure\n");
Jing Huang7725ccf2009-09-23 17:46:15 -07001784 goto ext;
1785 }
1786
1787 return 0;
1788
1789ext:
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001790 bfad_im_module_exit();
Jing Huang7725ccf2009-09-23 17:46:15 -07001791 return error;
1792}
1793
Jing Huang5fbe25c2010-10-18 17:17:23 -07001794/*
Jing Huang7725ccf2009-09-23 17:46:15 -07001795 * Driver module exit.
1796 */
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001797static void __exit
Jing Huang7725ccf2009-09-23 17:46:15 -07001798bfad_exit(void)
1799{
1800 pci_unregister_driver(&bfad_pci_driver);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001801 bfad_im_module_exit();
Jing Huang7725ccf2009-09-23 17:46:15 -07001802 bfad_free_fwimg();
1803}
1804
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001805/* Firmware handling */
Jing Huang61338a02011-04-13 11:44:03 -07001806static void
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001807bfad_read_firmware(struct pci_dev *pdev, u32 **bfi_image,
1808 u32 *bfi_image_size, char *fw_name)
1809{
1810 const struct firmware *fw;
1811
1812 if (request_firmware(&fw, fw_name, &pdev->dev)) {
1813 printk(KERN_ALERT "Can't locate firmware %s\n", fw_name);
Jing Huang61338a02011-04-13 11:44:03 -07001814 *bfi_image = NULL;
1815 goto out;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001816 }
1817
1818 *bfi_image = vmalloc(fw->size);
1819 if (NULL == *bfi_image) {
1820 printk(KERN_ALERT "Fail to allocate buffer for fw image "
1821 "size=%x!\n", (u32) fw->size);
Jing Huang61338a02011-04-13 11:44:03 -07001822 goto out;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001823 }
1824
1825 memcpy(*bfi_image, fw->data, fw->size);
1826 *bfi_image_size = fw->size/sizeof(u32);
Jing Huang61338a02011-04-13 11:44:03 -07001827out:
1828 release_firmware(fw);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001829}
1830
Jing Huang61338a02011-04-13 11:44:03 -07001831static u32 *
1832bfad_load_fwimg(struct pci_dev *pdev)
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001833{
Krishna Gudipati11189202011-06-13 15:50:35 -07001834 if (pdev->device == BFA_PCI_DEVICE_ID_CT2) {
1835 if (bfi_image_ct2_size == 0)
1836 bfad_read_firmware(pdev, &bfi_image_ct2,
1837 &bfi_image_ct2_size, BFAD_FW_FILE_CT2);
1838 return bfi_image_ct2;
1839 } else if (bfa_asic_id_ct(pdev->device)) {
1840 if (bfi_image_ct_size == 0)
1841 bfad_read_firmware(pdev, &bfi_image_ct,
1842 &bfi_image_ct_size, BFAD_FW_FILE_CT);
1843 return bfi_image_ct;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001844 } else {
Krishna Gudipati11189202011-06-13 15:50:35 -07001845 if (bfi_image_cb_size == 0)
1846 bfad_read_firmware(pdev, &bfi_image_cb,
1847 &bfi_image_cb_size, BFAD_FW_FILE_CB);
1848 return bfi_image_cb;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001849 }
1850}
Jing Huang7725ccf2009-09-23 17:46:15 -07001851
Jing Huang61338a02011-04-13 11:44:03 -07001852static void
1853bfad_free_fwimg(void)
1854{
Krishna Gudipati11189202011-06-13 15:50:35 -07001855 if (bfi_image_ct2_size && bfi_image_ct2)
1856 vfree(bfi_image_ct2);
1857 if (bfi_image_ct_size && bfi_image_ct)
1858 vfree(bfi_image_ct);
1859 if (bfi_image_cb_size && bfi_image_cb)
1860 vfree(bfi_image_cb);
Jing Huang61338a02011-04-13 11:44:03 -07001861}
1862
Jing Huang7725ccf2009-09-23 17:46:15 -07001863module_init(bfad_init);
1864module_exit(bfad_exit);
1865MODULE_LICENSE("GPL");
1866MODULE_DESCRIPTION("Brocade Fibre Channel HBA Driver" BFAD_PROTO_NAME);
1867MODULE_AUTHOR("Brocade Communications Systems, Inc.");
1868MODULE_VERSION(BFAD_DRIVER_VERSION);