blob: cc3b9d3d6d4095ac5c44c4f938219f36bab5844f [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
Vijaya Mohan Guvva2da11ad2013-11-21 01:37:50 -080066#define BFAD_FW_FILE_CB "cbfw-3.2.3.0.bin"
67#define BFAD_FW_FILE_CT "ctfw-3.2.3.0.bin"
68#define BFAD_FW_FILE_CT2 "ct2fw-3.2.3.0.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;
Vijaya Mohan Guvvada3e0be2013-11-21 01:37:33 -0800207 bfa_status_t ret;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700208
209 bfa_trc(bfad, event);
210
211 switch (event) {
212 case BFAD_E_INIT:
213 bfa_sm_set_state(bfad, bfad_sm_initializing);
214
215 init_completion(&bfad->comp);
216
217 /* Enable Interrupt and wait bfa_init completion */
218 if (bfad_setup_intr(bfad)) {
219 printk(KERN_WARNING "bfad%d: bfad_setup_intr failed\n",
220 bfad->inst_no);
Vijaya Mohan Guvvada3e0be2013-11-21 01:37:33 -0800221 bfa_sm_send_event(bfad, BFAD_E_INIT_FAILED);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700222 break;
223 }
224
225 spin_lock_irqsave(&bfad->bfad_lock, flags);
Maggie Zhangf7f738122010-12-09 19:08:43 -0800226 bfa_iocfc_init(&bfad->bfa);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700227 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
228
229 /* Set up interrupt handler for each vectors */
230 if ((bfad->bfad_flags & BFAD_MSIX_ON) &&
231 bfad_install_msix_handler(bfad)) {
232 printk(KERN_WARNING "%s: install_msix failed, bfad%d\n",
233 __func__, bfad->inst_no);
234 }
235
236 bfad_init_timer(bfad);
237
238 wait_for_completion(&bfad->comp);
239
240 if ((bfad->bfad_flags & BFAD_HAL_INIT_DONE)) {
241 bfa_sm_send_event(bfad, BFAD_E_INIT_SUCCESS);
242 } else {
Krishna Gudipati7c38c052011-04-14 16:50:35 -0700243 printk(KERN_WARNING
244 "bfa %s: bfa init failed\n",
245 bfad->pci_name);
Vijaya Mohan Guvvada3e0be2013-11-21 01:37:33 -0800246 spin_lock_irqsave(&bfad->bfad_lock, flags);
247 bfa_fcs_init(&bfad->bfa_fcs);
248 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
249
250 ret = bfad_cfg_pport(bfad, BFA_LPORT_ROLE_FCP_IM);
251 if (ret != BFA_STATUS_OK) {
252 init_completion(&bfad->comp);
253
254 spin_lock_irqsave(&bfad->bfad_lock, flags);
255 bfad->pport.flags |= BFAD_PORT_DELETE;
256 bfa_fcs_exit(&bfad->bfa_fcs);
257 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
258
259 wait_for_completion(&bfad->comp);
260
261 bfa_sm_send_event(bfad, BFAD_E_INIT_FAILED);
262 break;
263 }
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700264 bfad->bfad_flags |= BFAD_HAL_INIT_FAIL;
Vijaya Mohan Guvvada3e0be2013-11-21 01:37:33 -0800265 bfa_sm_send_event(bfad, BFAD_E_HAL_INIT_FAILED);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700266 }
267
268 break;
269
270 case BFAD_E_KTHREAD_CREATE_FAILED:
271 bfa_sm_set_state(bfad, bfad_sm_uninit);
272 break;
273
274 default:
275 bfa_sm_fault(bfad, event);
276 }
Jing Huang7725ccf2009-09-23 17:46:15 -0700277}
278
279static void
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700280bfad_sm_initializing(struct bfad_s *bfad, enum bfad_sm_event event)
Jing Huang7725ccf2009-09-23 17:46:15 -0700281{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700282 int retval;
283 unsigned long flags;
284
285 bfa_trc(bfad, event);
286
287 switch (event) {
288 case BFAD_E_INIT_SUCCESS:
289 kthread_stop(bfad->bfad_tsk);
290 spin_lock_irqsave(&bfad->bfad_lock, flags);
291 bfad->bfad_tsk = NULL;
292 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
293
294 retval = bfad_start_ops(bfad);
Vijaya Mohan Guvvada3e0be2013-11-21 01:37:33 -0800295 if (retval != BFA_STATUS_OK) {
296 bfa_sm_set_state(bfad, bfad_sm_failed);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700297 break;
Vijaya Mohan Guvvada3e0be2013-11-21 01:37:33 -0800298 }
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700299 bfa_sm_set_state(bfad, bfad_sm_operational);
300 break;
301
Vijaya Mohan Guvvada3e0be2013-11-21 01:37:33 -0800302 case BFAD_E_INIT_FAILED:
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700303 bfa_sm_set_state(bfad, bfad_sm_uninit);
304 kthread_stop(bfad->bfad_tsk);
305 spin_lock_irqsave(&bfad->bfad_lock, flags);
306 bfad->bfad_tsk = NULL;
307 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
308 break;
309
Vijaya Mohan Guvvada3e0be2013-11-21 01:37:33 -0800310 case BFAD_E_HAL_INIT_FAILED:
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700311 bfa_sm_set_state(bfad, bfad_sm_failed);
312 break;
313 default:
314 bfa_sm_fault(bfad, event);
315 }
Jing Huang7725ccf2009-09-23 17:46:15 -0700316}
317
318static void
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700319bfad_sm_failed(struct bfad_s *bfad, enum bfad_sm_event event)
Jing Huang7725ccf2009-09-23 17:46:15 -0700320{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700321 int retval;
Jing Huang7725ccf2009-09-23 17:46:15 -0700322
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700323 bfa_trc(bfad, event);
Jing Huang7725ccf2009-09-23 17:46:15 -0700324
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700325 switch (event) {
326 case BFAD_E_INIT_SUCCESS:
327 retval = bfad_start_ops(bfad);
328 if (retval != BFA_STATUS_OK)
329 break;
330 bfa_sm_set_state(bfad, bfad_sm_operational);
331 break;
Jing Huang7725ccf2009-09-23 17:46:15 -0700332
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700333 case BFAD_E_STOP:
Vijaya Mohan Guvvada3e0be2013-11-21 01:37:33 -0800334 bfa_sm_set_state(bfad, bfad_sm_fcs_exit);
335 bfa_sm_send_event(bfad, BFAD_E_FCS_EXIT_COMP);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700336 break;
Jing Huang7725ccf2009-09-23 17:46:15 -0700337
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700338 case BFAD_E_EXIT_COMP:
339 bfa_sm_set_state(bfad, bfad_sm_uninit);
340 bfad_remove_intr(bfad);
341 del_timer_sync(&bfad->hal_tmo);
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 }
Jing Huang7725ccf2009-09-23 17:46:15 -0700347}
348
349static void
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700350bfad_sm_operational(struct bfad_s *bfad, enum bfad_sm_event event)
Jing Huang7725ccf2009-09-23 17:46:15 -0700351{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700352 bfa_trc(bfad, event);
Jing Huang7725ccf2009-09-23 17:46:15 -0700353
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700354 switch (event) {
355 case BFAD_E_STOP:
356 bfa_sm_set_state(bfad, bfad_sm_fcs_exit);
357 bfad_fcs_stop(bfad);
358 break;
Jing Huang7725ccf2009-09-23 17:46:15 -0700359
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700360 default:
361 bfa_sm_fault(bfad, event);
362 }
363}
364
365static void
366bfad_sm_fcs_exit(struct bfad_s *bfad, enum bfad_sm_event event)
367{
368 bfa_trc(bfad, event);
369
370 switch (event) {
371 case BFAD_E_FCS_EXIT_COMP:
372 bfa_sm_set_state(bfad, bfad_sm_stopping);
373 bfad_stop(bfad);
374 break;
375
376 default:
377 bfa_sm_fault(bfad, event);
378 }
379}
380
381static void
382bfad_sm_stopping(struct bfad_s *bfad, enum bfad_sm_event event)
383{
384 bfa_trc(bfad, event);
385
386 switch (event) {
387 case BFAD_E_EXIT_COMP:
388 bfa_sm_set_state(bfad, bfad_sm_uninit);
389 bfad_remove_intr(bfad);
390 del_timer_sync(&bfad->hal_tmo);
391 bfad_im_probe_undo(bfad);
392 bfad->bfad_flags &= ~BFAD_FC4_PROBE_DONE;
393 bfad_uncfg_pport(bfad);
394 break;
395
396 default:
397 bfa_sm_fault(bfad, event);
398 break;
399 }
Jing Huang7725ccf2009-09-23 17:46:15 -0700400}
401
Jing Huang5fbe25c2010-10-18 17:17:23 -0700402/*
Jing Huang7725ccf2009-09-23 17:46:15 -0700403 * BFA callbacks
404 */
405void
406bfad_hcb_comp(void *arg, bfa_status_t status)
407{
408 struct bfad_hal_comp *fcomp = (struct bfad_hal_comp *)arg;
409
410 fcomp->status = status;
411 complete(&fcomp->comp);
412}
413
Jing Huang5fbe25c2010-10-18 17:17:23 -0700414/*
Jing Huang7725ccf2009-09-23 17:46:15 -0700415 * bfa_init callback
416 */
417void
418bfa_cb_init(void *drv, bfa_status_t init_status)
419{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700420 struct bfad_s *bfad = drv;
Jing Huang7725ccf2009-09-23 17:46:15 -0700421
Krishna Gudipatie6714322010-03-03 17:44:02 -0800422 if (init_status == BFA_STATUS_OK) {
Jing Huang7725ccf2009-09-23 17:46:15 -0700423 bfad->bfad_flags |= BFAD_HAL_INIT_DONE;
424
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700425 /*
426 * If BFAD_HAL_INIT_FAIL flag is set:
Krishna Gudipatie6714322010-03-03 17:44:02 -0800427 * Wake up the kernel thread to start
428 * the bfad operations after HAL init done
429 */
430 if ((bfad->bfad_flags & BFAD_HAL_INIT_FAIL)) {
431 bfad->bfad_flags &= ~BFAD_HAL_INIT_FAIL;
432 wake_up_process(bfad->bfad_tsk);
433 }
434 }
435
Jing Huang7725ccf2009-09-23 17:46:15 -0700436 complete(&bfad->comp);
437}
438
Jing Huang5fbe25c2010-10-18 17:17:23 -0700439/*
Jing Huang7725ccf2009-09-23 17:46:15 -0700440 * BFA_FCS callbacks
441 */
Jing Huang7725ccf2009-09-23 17:46:15 -0700442struct bfad_port_s *
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700443bfa_fcb_lport_new(struct bfad_s *bfad, struct bfa_fcs_lport_s *port,
444 enum bfa_lport_role roles, struct bfad_vf_s *vf_drv,
Jing Huang7725ccf2009-09-23 17:46:15 -0700445 struct bfad_vport_s *vp_drv)
446{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700447 bfa_status_t rc;
448 struct bfad_port_s *port_drv;
Jing Huang7725ccf2009-09-23 17:46:15 -0700449
450 if (!vp_drv && !vf_drv) {
451 port_drv = &bfad->pport;
452 port_drv->pvb_type = BFAD_PORT_PHYS_BASE;
453 } else if (!vp_drv && vf_drv) {
454 port_drv = &vf_drv->base_port;
455 port_drv->pvb_type = BFAD_PORT_VF_BASE;
456 } else if (vp_drv && !vf_drv) {
457 port_drv = &vp_drv->drv_port;
458 port_drv->pvb_type = BFAD_PORT_PHYS_VPORT;
459 } else {
460 port_drv = &vp_drv->drv_port;
461 port_drv->pvb_type = BFAD_PORT_VF_VPORT;
462 }
463
464 port_drv->fcs_port = port;
465 port_drv->roles = roles;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700466
467 if (roles & BFA_LPORT_ROLE_FCP_IM) {
468 rc = bfad_im_port_new(bfad, port_drv);
469 if (rc != BFA_STATUS_OK) {
470 bfad_im_port_delete(bfad, port_drv);
471 port_drv = NULL;
472 }
Jing Huang7725ccf2009-09-23 17:46:15 -0700473 }
474
475 return port_drv;
476}
477
Jing Huang5fbe25c2010-10-18 17:17:23 -0700478/*
Jing Huang7725ccf2009-09-23 17:46:15 -0700479 * FCS RPORT alloc callback, after successful PLOGI by FCS
480 */
481bfa_status_t
482bfa_fcb_rport_alloc(struct bfad_s *bfad, struct bfa_fcs_rport_s **rport,
483 struct bfad_rport_s **rport_drv)
484{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700485 bfa_status_t rc = BFA_STATUS_OK;
Jing Huang7725ccf2009-09-23 17:46:15 -0700486
487 *rport_drv = kzalloc(sizeof(struct bfad_rport_s), GFP_ATOMIC);
488 if (*rport_drv == NULL) {
489 rc = BFA_STATUS_ENOMEM;
490 goto ext;
491 }
492
493 *rport = &(*rport_drv)->fcs_rport;
494
495ext:
496 return rc;
497}
498
Jing Huang5fbe25c2010-10-18 17:17:23 -0700499/*
Jing Huangd9883542010-07-08 19:46:26 -0700500 * FCS PBC VPORT Create
501 */
502void
503bfa_fcb_pbc_vport_create(struct bfad_s *bfad, struct bfi_pbc_vport_s pbc_vport)
504{
Jing Huang7725ccf2009-09-23 17:46:15 -0700505
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700506 struct bfa_lport_cfg_s port_cfg = {0};
507 struct bfad_vport_s *vport;
508 int rc;
Jing Huangd9883542010-07-08 19:46:26 -0700509
Alexey Khoroshilovdc6d2a02014-04-18 00:58:42 -0700510 vport = kzalloc(sizeof(struct bfad_vport_s), GFP_ATOMIC);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700511 if (!vport) {
Jing Huangd9883542010-07-08 19:46:26 -0700512 bfa_trc(bfad, 0);
513 return;
514 }
515
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700516 vport->drv_port.bfad = bfad;
517 port_cfg.roles = BFA_LPORT_ROLE_FCP_IM;
518 port_cfg.pwwn = pbc_vport.vp_pwwn;
519 port_cfg.nwwn = pbc_vport.vp_nwwn;
520 port_cfg.preboot_vp = BFA_TRUE;
Jing Huangd9883542010-07-08 19:46:26 -0700521
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700522 rc = bfa_fcs_pbc_vport_create(&vport->fcs_vport, &bfad->bfa_fcs, 0,
523 &port_cfg, vport);
Jing Huangd9883542010-07-08 19:46:26 -0700524
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700525 if (rc != BFA_STATUS_OK) {
526 bfa_trc(bfad, 0);
527 return;
528 }
529
530 list_add_tail(&vport->list_entry, &bfad->pbc_vport_list);
Jing Huangd9883542010-07-08 19:46:26 -0700531}
Jing Huang7725ccf2009-09-23 17:46:15 -0700532
533void
534bfad_hal_mem_release(struct bfad_s *bfad)
535{
Jing Huang7725ccf2009-09-23 17:46:15 -0700536 struct bfa_meminfo_s *hal_meminfo = &bfad->meminfo;
Krishna Gudipati45070252011-06-24 20:24:29 -0700537 struct bfa_mem_dma_s *dma_info, *dma_elem;
538 struct bfa_mem_kva_s *kva_info, *kva_elem;
539 struct list_head *dm_qe, *km_qe;
Jing Huang7725ccf2009-09-23 17:46:15 -0700540
Krishna Gudipati45070252011-06-24 20:24:29 -0700541 dma_info = &hal_meminfo->dma_info;
542 kva_info = &hal_meminfo->kva_info;
543
544 /* Iterate through the KVA meminfo queue */
545 list_for_each(km_qe, &kva_info->qe) {
546 kva_elem = (struct bfa_mem_kva_s *) km_qe;
547 vfree(kva_elem->kva);
548 }
549
550 /* Iterate through the DMA meminfo queue */
551 list_for_each(dm_qe, &dma_info->qe) {
552 dma_elem = (struct bfa_mem_dma_s *) dm_qe;
553 dma_free_coherent(&bfad->pcidev->dev,
554 dma_elem->mem_len, dma_elem->kva,
555 (dma_addr_t) dma_elem->dma);
Jing Huang7725ccf2009-09-23 17:46:15 -0700556 }
557
558 memset(hal_meminfo, 0, sizeof(struct bfa_meminfo_s));
559}
560
561void
562bfad_update_hal_cfg(struct bfa_iocfc_cfg_s *bfa_cfg)
563{
564 if (num_rports > 0)
565 bfa_cfg->fwcfg.num_rports = num_rports;
566 if (num_ios > 0)
567 bfa_cfg->fwcfg.num_ioim_reqs = num_ios;
568 if (num_tms > 0)
569 bfa_cfg->fwcfg.num_tskim_reqs = num_tms;
Krishna Gudipati45070252011-06-24 20:24:29 -0700570 if (num_fcxps > 0 && num_fcxps <= BFA_FCXP_MAX)
Jing Huang7725ccf2009-09-23 17:46:15 -0700571 bfa_cfg->fwcfg.num_fcxp_reqs = num_fcxps;
Krishna Gudipati45070252011-06-24 20:24:29 -0700572 if (num_ufbufs > 0 && num_ufbufs <= BFA_UF_MAX)
Jing Huang7725ccf2009-09-23 17:46:15 -0700573 bfa_cfg->fwcfg.num_uf_bufs = num_ufbufs;
574 if (reqq_size > 0)
575 bfa_cfg->drvcfg.num_reqq_elems = reqq_size;
576 if (rspq_size > 0)
577 bfa_cfg->drvcfg.num_rspq_elems = rspq_size;
Krishna Gudipati45070252011-06-24 20:24:29 -0700578 if (num_sgpgs > 0 && num_sgpgs <= BFA_SGPG_MAX)
Jing Huang7725ccf2009-09-23 17:46:15 -0700579 bfa_cfg->drvcfg.num_sgpgs = num_sgpgs;
580
581 /*
582 * populate the hal values back to the driver for sysfs use.
583 * otherwise, the default values will be shown as 0 in sysfs
584 */
585 num_rports = bfa_cfg->fwcfg.num_rports;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700586 num_ios = bfa_cfg->fwcfg.num_ioim_reqs;
587 num_tms = bfa_cfg->fwcfg.num_tskim_reqs;
588 num_fcxps = bfa_cfg->fwcfg.num_fcxp_reqs;
Jing Huang7725ccf2009-09-23 17:46:15 -0700589 num_ufbufs = bfa_cfg->fwcfg.num_uf_bufs;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700590 reqq_size = bfa_cfg->drvcfg.num_reqq_elems;
591 rspq_size = bfa_cfg->drvcfg.num_rspq_elems;
592 num_sgpgs = bfa_cfg->drvcfg.num_sgpgs;
Jing Huang7725ccf2009-09-23 17:46:15 -0700593}
594
595bfa_status_t
596bfad_hal_mem_alloc(struct bfad_s *bfad)
597{
598 struct bfa_meminfo_s *hal_meminfo = &bfad->meminfo;
Krishna Gudipati45070252011-06-24 20:24:29 -0700599 struct bfa_mem_dma_s *dma_info, *dma_elem;
600 struct bfa_mem_kva_s *kva_info, *kva_elem;
601 struct list_head *dm_qe, *km_qe;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700602 bfa_status_t rc = BFA_STATUS_OK;
Krishna Gudipati45070252011-06-24 20:24:29 -0700603 dma_addr_t phys_addr;
Jing Huang7725ccf2009-09-23 17:46:15 -0700604
605 bfa_cfg_get_default(&bfad->ioc_cfg);
Jing Huang7725ccf2009-09-23 17:46:15 -0700606 bfad_update_hal_cfg(&bfad->ioc_cfg);
607 bfad->cfg_data.ioc_queue_depth = bfad->ioc_cfg.fwcfg.num_ioim_reqs;
Krishna Gudipati45070252011-06-24 20:24:29 -0700608 bfa_cfg_get_meminfo(&bfad->ioc_cfg, hal_meminfo, &bfad->bfa);
Jing Huang7725ccf2009-09-23 17:46:15 -0700609
Krishna Gudipati45070252011-06-24 20:24:29 -0700610 dma_info = &hal_meminfo->dma_info;
611 kva_info = &hal_meminfo->kva_info;
Jing Huang7725ccf2009-09-23 17:46:15 -0700612
Krishna Gudipati45070252011-06-24 20:24:29 -0700613 /* Iterate through the KVA meminfo queue */
614 list_for_each(km_qe, &kva_info->qe) {
615 kva_elem = (struct bfa_mem_kva_s *) km_qe;
616 kva_elem->kva = vmalloc(kva_elem->mem_len);
617 if (kva_elem->kva == NULL) {
618 bfad_hal_mem_release(bfad);
619 rc = BFA_STATUS_ENOMEM;
620 goto ext;
Jing Huang7725ccf2009-09-23 17:46:15 -0700621 }
Krishna Gudipati45070252011-06-24 20:24:29 -0700622 memset(kva_elem->kva, 0, kva_elem->mem_len);
623 }
624
625 /* Iterate through the DMA meminfo queue */
626 list_for_each(dm_qe, &dma_info->qe) {
627 dma_elem = (struct bfa_mem_dma_s *) dm_qe;
628 dma_elem->kva = dma_alloc_coherent(&bfad->pcidev->dev,
629 dma_elem->mem_len,
630 &phys_addr, GFP_KERNEL);
631 if (dma_elem->kva == NULL) {
632 bfad_hal_mem_release(bfad);
633 rc = BFA_STATUS_ENOMEM;
634 goto ext;
635 }
636 dma_elem->dma = phys_addr;
637 memset(dma_elem->kva, 0, dma_elem->mem_len);
Jing Huang7725ccf2009-09-23 17:46:15 -0700638 }
639ext:
640 return rc;
641}
642
Jing Huang5fbe25c2010-10-18 17:17:23 -0700643/*
Jing Huang7725ccf2009-09-23 17:46:15 -0700644 * Create a vport under a vf.
645 */
646bfa_status_t
647bfad_vport_create(struct bfad_s *bfad, u16 vf_id,
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700648 struct bfa_lport_cfg_s *port_cfg, struct device *dev)
Jing Huang7725ccf2009-09-23 17:46:15 -0700649{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700650 struct bfad_vport_s *vport;
651 int rc = BFA_STATUS_OK;
652 unsigned long flags;
Jing Huang7725ccf2009-09-23 17:46:15 -0700653 struct completion fcomp;
654
655 vport = kzalloc(sizeof(struct bfad_vport_s), GFP_KERNEL);
656 if (!vport) {
657 rc = BFA_STATUS_ENOMEM;
658 goto ext;
659 }
660
661 vport->drv_port.bfad = bfad;
662 spin_lock_irqsave(&bfad->bfad_lock, flags);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700663 rc = bfa_fcs_vport_create(&vport->fcs_vport, &bfad->bfa_fcs, vf_id,
664 port_cfg, vport);
Jing Huang7725ccf2009-09-23 17:46:15 -0700665 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
666
667 if (rc != BFA_STATUS_OK)
668 goto ext_free_vport;
669
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700670 if (port_cfg->roles & BFA_LPORT_ROLE_FCP_IM) {
Jing Huangb5042932010-03-19 11:05:39 -0700671 rc = bfad_im_scsi_host_alloc(bfad, vport->drv_port.im_port,
672 dev);
Jing Huang7725ccf2009-09-23 17:46:15 -0700673 if (rc != BFA_STATUS_OK)
674 goto ext_free_fcs_vport;
675 }
676
677 spin_lock_irqsave(&bfad->bfad_lock, flags);
678 bfa_fcs_vport_start(&vport->fcs_vport);
Krishna Gudipati5b7db7a2011-12-20 18:58:32 -0800679 list_add_tail(&vport->list_entry, &bfad->vport_list);
Jing Huang7725ccf2009-09-23 17:46:15 -0700680 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
681
682 return BFA_STATUS_OK;
683
684ext_free_fcs_vport:
685 spin_lock_irqsave(&bfad->bfad_lock, flags);
686 vport->comp_del = &fcomp;
687 init_completion(vport->comp_del);
688 bfa_fcs_vport_delete(&vport->fcs_vport);
689 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
690 wait_for_completion(vport->comp_del);
691ext_free_vport:
692 kfree(vport);
693ext:
694 return rc;
695}
696
Jing Huang7725ccf2009-09-23 17:46:15 -0700697void
698bfad_bfa_tmo(unsigned long data)
699{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700700 struct bfad_s *bfad = (struct bfad_s *) data;
701 unsigned long flags;
702 struct list_head doneq;
Jing Huang7725ccf2009-09-23 17:46:15 -0700703
704 spin_lock_irqsave(&bfad->bfad_lock, flags);
705
Maggie Zhangf7f738122010-12-09 19:08:43 -0800706 bfa_timer_beat(&bfad->bfa.timer_mod);
Jing Huang7725ccf2009-09-23 17:46:15 -0700707
708 bfa_comp_deq(&bfad->bfa, &doneq);
709 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
710
711 if (!list_empty(&doneq)) {
712 bfa_comp_process(&bfad->bfa, &doneq);
713 spin_lock_irqsave(&bfad->bfad_lock, flags);
714 bfa_comp_free(&bfad->bfa, &doneq);
715 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
716 }
717
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700718 mod_timer(&bfad->hal_tmo,
719 jiffies + msecs_to_jiffies(BFA_TIMER_FREQ));
Jing Huang7725ccf2009-09-23 17:46:15 -0700720}
721
722void
723bfad_init_timer(struct bfad_s *bfad)
724{
725 init_timer(&bfad->hal_tmo);
726 bfad->hal_tmo.function = bfad_bfa_tmo;
727 bfad->hal_tmo.data = (unsigned long)bfad;
728
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700729 mod_timer(&bfad->hal_tmo,
730 jiffies + msecs_to_jiffies(BFA_TIMER_FREQ));
Jing Huang7725ccf2009-09-23 17:46:15 -0700731}
732
733int
734bfad_pci_init(struct pci_dev *pdev, struct bfad_s *bfad)
735{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700736 int rc = -ENODEV;
Jing Huang7725ccf2009-09-23 17:46:15 -0700737
738 if (pci_enable_device(pdev)) {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700739 printk(KERN_ERR "pci_enable_device fail %p\n", pdev);
Jing Huang7725ccf2009-09-23 17:46:15 -0700740 goto out;
741 }
742
743 if (pci_request_regions(pdev, BFAD_DRIVER_NAME))
744 goto out_disable_device;
745
746 pci_set_master(pdev);
747
748
Krishna Gudipati10a07372011-06-24 20:23:38 -0700749 if ((pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) ||
750 (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)) != 0)) {
751 if ((pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0) ||
752 (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)) {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700753 printk(KERN_ERR "pci_set_dma_mask fail %p\n", pdev);
Jing Huang7725ccf2009-09-23 17:46:15 -0700754 goto out_release_region;
755 }
Krishna Gudipati10a07372011-06-24 20:23:38 -0700756 }
Jing Huang7725ccf2009-09-23 17:46:15 -0700757
Krishna Gudipati881c1b32012-08-22 19:52:02 -0700758 /* Enable PCIE Advanced Error Recovery (AER) if kernel supports */
759 pci_enable_pcie_error_reporting(pdev);
760
Jing Huangb3522f02010-03-19 11:06:44 -0700761 bfad->pci_bar0_kva = pci_iomap(pdev, 0, pci_resource_len(pdev, 0));
Krishna Gudipati11189202011-06-13 15:50:35 -0700762 bfad->pci_bar2_kva = pci_iomap(pdev, 2, pci_resource_len(pdev, 2));
Jing Huang7725ccf2009-09-23 17:46:15 -0700763
764 if (bfad->pci_bar0_kva == NULL) {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700765 printk(KERN_ERR "Fail to map bar0\n");
Jing Huang7725ccf2009-09-23 17:46:15 -0700766 goto out_release_region;
767 }
768
769 bfad->hal_pcidev.pci_slot = PCI_SLOT(pdev->devfn);
770 bfad->hal_pcidev.pci_func = PCI_FUNC(pdev->devfn);
771 bfad->hal_pcidev.pci_bar_kva = bfad->pci_bar0_kva;
772 bfad->hal_pcidev.device_id = pdev->device;
Krishna Gudipati1a4d8e12011-06-24 20:22:28 -0700773 bfad->hal_pcidev.ssid = pdev->subsystem_device;
Jing Huang7725ccf2009-09-23 17:46:15 -0700774 bfad->pci_name = pci_name(pdev);
775
776 bfad->pci_attr.vendor_id = pdev->vendor;
777 bfad->pci_attr.device_id = pdev->device;
778 bfad->pci_attr.ssid = pdev->subsystem_device;
779 bfad->pci_attr.ssvid = pdev->subsystem_vendor;
780 bfad->pci_attr.pcifn = PCI_FUNC(pdev->devfn);
781
782 bfad->pcidev = pdev;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700783
784 /* Adjust PCIe Maximum Read Request Size */
Yijing Wangc0102c02013-09-05 15:55:25 +0800785 if (pci_is_pcie(pdev) && pcie_max_read_reqsz) {
786 if (pcie_max_read_reqsz >= 128 &&
787 pcie_max_read_reqsz <= 4096 &&
788 is_power_of_2(pcie_max_read_reqsz)) {
789 int max_rq = pcie_get_readrq(pdev);
790 printk(KERN_WARNING "BFA[%s]: "
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700791 "pcie_max_read_request_size is %d, "
Yijing Wangc0102c02013-09-05 15:55:25 +0800792 "reset to %d\n", bfad->pci_name, max_rq,
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700793 pcie_max_read_reqsz);
Yijing Wangc0102c02013-09-05 15:55:25 +0800794 pcie_set_readrq(pdev, pcie_max_read_reqsz);
795 } else {
796 printk(KERN_WARNING "BFA[%s]: invalid "
797 "pcie_max_read_request_size %d ignored\n",
798 bfad->pci_name, pcie_max_read_reqsz);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700799 }
800 }
801
Krishna Gudipati881c1b32012-08-22 19:52:02 -0700802 pci_save_state(pdev);
803
Jing Huang7725ccf2009-09-23 17:46:15 -0700804 return 0;
805
806out_release_region:
807 pci_release_regions(pdev);
808out_disable_device:
809 pci_disable_device(pdev);
810out:
811 return rc;
812}
813
814void
815bfad_pci_uninit(struct pci_dev *pdev, struct bfad_s *bfad)
816{
Jing Huang7725ccf2009-09-23 17:46:15 -0700817 pci_iounmap(pdev, bfad->pci_bar0_kva);
Krishna Gudipati11189202011-06-13 15:50:35 -0700818 pci_iounmap(pdev, bfad->pci_bar2_kva);
Jing Huang7725ccf2009-09-23 17:46:15 -0700819 pci_release_regions(pdev);
Krishna Gudipati881c1b32012-08-22 19:52:02 -0700820 /* Disable PCIE Advanced Error Recovery (AER) */
821 pci_disable_pcie_error_reporting(pdev);
Jing Huang7725ccf2009-09-23 17:46:15 -0700822 pci_disable_device(pdev);
Jing Huang7725ccf2009-09-23 17:46:15 -0700823}
824
Jing Huang7725ccf2009-09-23 17:46:15 -0700825bfa_status_t
826bfad_drv_init(struct bfad_s *bfad)
827{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700828 bfa_status_t rc;
829 unsigned long flags;
Jing Huang7725ccf2009-09-23 17:46:15 -0700830
831 bfad->cfg_data.rport_del_timeout = rport_del_timeout;
832 bfad->cfg_data.lun_queue_depth = bfa_lun_queue_depth;
833 bfad->cfg_data.io_max_sge = bfa_io_max_sge;
834 bfad->cfg_data.binding_method = FCP_PWWN_BINDING;
835
836 rc = bfad_hal_mem_alloc(bfad);
837 if (rc != BFA_STATUS_OK) {
838 printk(KERN_WARNING "bfad%d bfad_hal_mem_alloc failure\n",
839 bfad->inst_no);
840 printk(KERN_WARNING
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700841 "Not enough memory to attach all Brocade HBA ports, %s",
842 "System may need more memory.\n");
Vijaya Mohan Guvvada3e0be2013-11-21 01:37:33 -0800843 return BFA_STATUS_FAILED;
Jing Huang7725ccf2009-09-23 17:46:15 -0700844 }
845
Maggie Zhangf7f738122010-12-09 19:08:43 -0800846 bfad->bfa.trcmod = bfad->trcmod;
847 bfad->bfa.plog = &bfad->plog_buf;
Jing Huang7725ccf2009-09-23 17:46:15 -0700848 bfa_plog_init(&bfad->plog_buf);
849 bfa_plog_str(&bfad->plog_buf, BFA_PL_MID_DRVR, BFA_PL_EID_DRIVER_START,
850 0, "Driver Attach");
851
852 bfa_attach(&bfad->bfa, bfad, &bfad->ioc_cfg, &bfad->meminfo,
853 &bfad->hal_pcidev);
854
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700855 /* FCS INIT */
Jing Huang7725ccf2009-09-23 17:46:15 -0700856 spin_lock_irqsave(&bfad->bfad_lock, flags);
Maggie Zhangf7f738122010-12-09 19:08:43 -0800857 bfad->bfa_fcs.trcmod = bfad->trcmod;
Krishna Gudipati82794a22010-03-03 17:43:30 -0800858 bfa_fcs_attach(&bfad->bfa_fcs, &bfad->bfa, bfad, BFA_FALSE);
Maggie Zhangf7f738122010-12-09 19:08:43 -0800859 bfad->bfa_fcs.fdmi_enabled = fdmi_enable;
Jing Huang7725ccf2009-09-23 17:46:15 -0700860 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
861
862 bfad->bfad_flags |= BFAD_DRV_INIT_DONE;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700863
Jing Huang7725ccf2009-09-23 17:46:15 -0700864 return BFA_STATUS_OK;
Jing Huang7725ccf2009-09-23 17:46:15 -0700865}
866
867void
868bfad_drv_uninit(struct bfad_s *bfad)
869{
Krishna Gudipatie6714322010-03-03 17:44:02 -0800870 unsigned long flags;
871
872 spin_lock_irqsave(&bfad->bfad_lock, flags);
873 init_completion(&bfad->comp);
Maggie Zhangf7f738122010-12-09 19:08:43 -0800874 bfa_iocfc_stop(&bfad->bfa);
Krishna Gudipatie6714322010-03-03 17:44:02 -0800875 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
876 wait_for_completion(&bfad->comp);
877
Jing Huang7725ccf2009-09-23 17:46:15 -0700878 del_timer_sync(&bfad->hal_tmo);
879 bfa_isr_disable(&bfad->bfa);
880 bfa_detach(&bfad->bfa);
881 bfad_remove_intr(bfad);
Jing Huang7725ccf2009-09-23 17:46:15 -0700882 bfad_hal_mem_release(bfad);
Krishna Gudipatie6714322010-03-03 17:44:02 -0800883
884 bfad->bfad_flags &= ~BFAD_DRV_INIT_DONE;
Jing Huang7725ccf2009-09-23 17:46:15 -0700885}
886
887void
888bfad_drv_start(struct bfad_s *bfad)
889{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700890 unsigned long flags;
Jing Huang7725ccf2009-09-23 17:46:15 -0700891
892 spin_lock_irqsave(&bfad->bfad_lock, flags);
Maggie Zhangf7f738122010-12-09 19:08:43 -0800893 bfa_iocfc_start(&bfad->bfa);
Krishna Gudipati75332a72011-06-13 15:54:31 -0700894 bfa_fcs_pbc_vport_init(&bfad->bfa_fcs);
Maggie Zhangf7f738122010-12-09 19:08:43 -0800895 bfa_fcs_fabric_modstart(&bfad->bfa_fcs);
Jing Huang7725ccf2009-09-23 17:46:15 -0700896 bfad->bfad_flags |= BFAD_HAL_START_DONE;
897 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
898
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700899 if (bfad->im)
900 flush_workqueue(bfad->im->drv_workq);
Jing Huang7725ccf2009-09-23 17:46:15 -0700901}
902
903void
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700904bfad_fcs_stop(struct bfad_s *bfad)
Jing Huang7725ccf2009-09-23 17:46:15 -0700905{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700906 unsigned long flags;
Jing Huang7725ccf2009-09-23 17:46:15 -0700907
908 spin_lock_irqsave(&bfad->bfad_lock, flags);
909 init_completion(&bfad->comp);
910 bfad->pport.flags |= BFAD_PORT_DELETE;
911 bfa_fcs_exit(&bfad->bfa_fcs);
912 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
913 wait_for_completion(&bfad->comp);
914
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700915 bfa_sm_send_event(bfad, BFAD_E_FCS_EXIT_COMP);
916}
917
918void
919bfad_stop(struct bfad_s *bfad)
920{
921 unsigned long flags;
922
Jing Huang7725ccf2009-09-23 17:46:15 -0700923 spin_lock_irqsave(&bfad->bfad_lock, flags);
924 init_completion(&bfad->comp);
Maggie Zhangf7f738122010-12-09 19:08:43 -0800925 bfa_iocfc_stop(&bfad->bfa);
Jing Huang7725ccf2009-09-23 17:46:15 -0700926 bfad->bfad_flags &= ~BFAD_HAL_START_DONE;
927 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
928 wait_for_completion(&bfad->comp);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700929
930 bfa_sm_send_event(bfad, BFAD_E_EXIT_COMP);
Jing Huang7725ccf2009-09-23 17:46:15 -0700931}
932
933bfa_status_t
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700934bfad_cfg_pport(struct bfad_s *bfad, enum bfa_lport_role role)
Jing Huang7725ccf2009-09-23 17:46:15 -0700935{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700936 int rc = BFA_STATUS_OK;
Jing Huang7725ccf2009-09-23 17:46:15 -0700937
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700938 /* Allocate scsi_host for the physical port */
939 if ((supported_fc4s & BFA_LPORT_ROLE_FCP_IM) &&
940 (role & BFA_LPORT_ROLE_FCP_IM)) {
Jing Huang7725ccf2009-09-23 17:46:15 -0700941 if (bfad->pport.im_port == NULL) {
942 rc = BFA_STATUS_FAILED;
943 goto out;
944 }
945
Jing Huangb5042932010-03-19 11:05:39 -0700946 rc = bfad_im_scsi_host_alloc(bfad, bfad->pport.im_port,
947 &bfad->pcidev->dev);
Jing Huang7725ccf2009-09-23 17:46:15 -0700948 if (rc != BFA_STATUS_OK)
949 goto out;
950
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700951 bfad->pport.roles |= BFA_LPORT_ROLE_FCP_IM;
Jing Huang7725ccf2009-09-23 17:46:15 -0700952 }
953
954 bfad->bfad_flags |= BFAD_CFG_PPORT_DONE;
955
956out:
957 return rc;
958}
959
960void
961bfad_uncfg_pport(struct bfad_s *bfad)
962{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700963 if ((supported_fc4s & BFA_LPORT_ROLE_FCP_IM) &&
964 (bfad->pport.roles & BFA_LPORT_ROLE_FCP_IM)) {
Jing Huang7725ccf2009-09-23 17:46:15 -0700965 bfad_im_scsi_host_free(bfad, bfad->pport.im_port);
966 bfad_im_port_clean(bfad->pport.im_port);
967 kfree(bfad->pport.im_port);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700968 bfad->pport.roles &= ~BFA_LPORT_ROLE_FCP_IM;
Jing Huang7725ccf2009-09-23 17:46:15 -0700969 }
970
971 bfad->bfad_flags &= ~BFAD_CFG_PPORT_DONE;
972}
973
Krishna Gudipatie6714322010-03-03 17:44:02 -0800974bfa_status_t
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700975bfad_start_ops(struct bfad_s *bfad) {
976
977 int retval;
978 unsigned long flags;
979 struct bfad_vport_s *vport, *vport_new;
980 struct bfa_fcs_driver_info_s driver_info;
981
Krishna Gudipati61e62e22011-06-24 20:29:07 -0700982 /* Limit min/max. xfer size to [64k-32MB] */
983 if (max_xfer_size < BFAD_MIN_SECTORS >> 1)
984 max_xfer_size = BFAD_MIN_SECTORS >> 1;
985 if (max_xfer_size > BFAD_MAX_SECTORS >> 1)
986 max_xfer_size = BFAD_MAX_SECTORS >> 1;
987
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700988 /* Fill the driver_info info to fcs*/
989 memset(&driver_info, 0, sizeof(driver_info));
990 strncpy(driver_info.version, BFAD_DRIVER_VERSION,
991 sizeof(driver_info.version) - 1);
992 if (host_name)
993 strncpy(driver_info.host_machine_name, host_name,
994 sizeof(driver_info.host_machine_name) - 1);
995 if (os_name)
996 strncpy(driver_info.host_os_name, os_name,
997 sizeof(driver_info.host_os_name) - 1);
998 if (os_patch)
999 strncpy(driver_info.host_os_patch, os_patch,
1000 sizeof(driver_info.host_os_patch) - 1);
1001
1002 strncpy(driver_info.os_device_name, bfad->pci_name,
Dan Carpenter30ac3792013-01-10 12:06:20 +03001003 sizeof(driver_info.os_device_name) - 1);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001004
Krishna Gudipati75332a72011-06-13 15:54:31 -07001005 /* FCS driver info init */
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001006 spin_lock_irqsave(&bfad->bfad_lock, flags);
1007 bfa_fcs_driver_info_init(&bfad->bfa_fcs, &driver_info);
Vijaya Mohan Guvvada3e0be2013-11-21 01:37:33 -08001008
1009 if (bfad->bfad_flags & BFAD_CFG_PPORT_DONE)
1010 bfa_fcs_update_cfg(&bfad->bfa_fcs);
1011 else
1012 bfa_fcs_init(&bfad->bfa_fcs);
1013
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001014 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
Krishna Gudipatie6714322010-03-03 17:44:02 -08001015
Vijaya Mohan Guvvada3e0be2013-11-21 01:37:33 -08001016 if (!(bfad->bfad_flags & BFAD_CFG_PPORT_DONE)) {
1017 retval = bfad_cfg_pport(bfad, BFA_LPORT_ROLE_FCP_IM);
1018 if (retval != BFA_STATUS_OK)
1019 return BFA_STATUS_FAILED;
1020 }
Krishna Gudipati75332a72011-06-13 15:54:31 -07001021
1022 /* Setup fc host fixed attribute if the lk supports */
1023 bfad_fc_host_init(bfad->pport.im_port);
Krishna Gudipatie6714322010-03-03 17:44:02 -08001024
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001025 /* BFAD level FC4 IM specific resource allocation */
1026 retval = bfad_im_probe(bfad);
1027 if (retval != BFA_STATUS_OK) {
1028 printk(KERN_WARNING "bfad_im_probe failed\n");
1029 if (bfa_sm_cmp_state(bfad, bfad_sm_initializing))
1030 bfa_sm_set_state(bfad, bfad_sm_failed);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001031 return BFA_STATUS_FAILED;
1032 } else
1033 bfad->bfad_flags |= BFAD_FC4_PROBE_DONE;
1034
Krishna Gudipatie6714322010-03-03 17:44:02 -08001035 bfad_drv_start(bfad);
1036
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001037 /* Complete pbc vport create */
1038 list_for_each_entry_safe(vport, vport_new, &bfad->pbc_vport_list,
1039 list_entry) {
Jing Huangd9883542010-07-08 19:46:26 -07001040 struct fc_vport_identifiers vid;
1041 struct fc_vport *fc_vport;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001042 char pwwn_buf[BFA_STRING_32];
Jing Huangd9883542010-07-08 19:46:26 -07001043
1044 memset(&vid, 0, sizeof(vid));
1045 vid.roles = FC_PORT_ROLE_FCP_INITIATOR;
1046 vid.vport_type = FC_PORTTYPE_NPIV;
1047 vid.disable = false;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001048 vid.node_name = wwn_to_u64((u8 *)
1049 (&((vport->fcs_vport).lport.port_cfg.nwwn)));
1050 vid.port_name = wwn_to_u64((u8 *)
1051 (&((vport->fcs_vport).lport.port_cfg.pwwn)));
Jing Huangd9883542010-07-08 19:46:26 -07001052 fc_vport = fc_vport_create(bfad->pport.im_port->shost, 0, &vid);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001053 if (!fc_vport) {
1054 wwn2str(pwwn_buf, vid.port_name);
Jing Huangd9883542010-07-08 19:46:26 -07001055 printk(KERN_WARNING "bfad%d: failed to create pbc vport"
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001056 " %s\n", bfad->inst_no, pwwn_buf);
1057 }
1058 list_del(&vport->list_entry);
1059 kfree(vport);
Jing Huangd9883542010-07-08 19:46:26 -07001060 }
1061
Krishna Gudipatie6714322010-03-03 17:44:02 -08001062 /*
1063 * If bfa_linkup_delay is set to -1 default; try to retrive the
Maggie Zhangf16a1752010-12-09 19:12:32 -08001064 * value using the bfad_get_linkup_delay(); else use the
Krishna Gudipatie6714322010-03-03 17:44:02 -08001065 * passed in module param value as the bfa_linkup_delay.
1066 */
1067 if (bfa_linkup_delay < 0) {
Maggie Zhangf16a1752010-12-09 19:12:32 -08001068 bfa_linkup_delay = bfad_get_linkup_delay(bfad);
1069 bfad_rport_online_wait(bfad);
Krishna Gudipatie6714322010-03-03 17:44:02 -08001070 bfa_linkup_delay = -1;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001071 } else
Maggie Zhangf16a1752010-12-09 19:12:32 -08001072 bfad_rport_online_wait(bfad);
Krishna Gudipatie6714322010-03-03 17:44:02 -08001073
Jing Huang88166242010-12-09 17:11:53 -08001074 BFA_LOG(KERN_INFO, bfad, bfa_log_level, "bfa device claimed\n");
Krishna Gudipatie6714322010-03-03 17:44:02 -08001075
1076 return BFA_STATUS_OK;
Krishna Gudipatie6714322010-03-03 17:44:02 -08001077}
1078
1079int
Jing Huangd9883542010-07-08 19:46:26 -07001080bfad_worker(void *ptr)
Krishna Gudipatie6714322010-03-03 17:44:02 -08001081{
Jiri Slaby8f7d3f02015-04-16 12:48:29 -07001082 struct bfad_s *bfad = ptr;
1083 unsigned long flags;
Krishna Gudipatie6714322010-03-03 17:44:02 -08001084
Jiri Slaby8f7d3f02015-04-16 12:48:29 -07001085 if (kthread_should_stop())
1086 return 0;
Krishna Gudipatie6714322010-03-03 17:44:02 -08001087
Jiri Slaby8f7d3f02015-04-16 12:48:29 -07001088 /* Send event BFAD_E_INIT_SUCCESS */
1089 bfa_sm_send_event(bfad, BFAD_E_INIT_SUCCESS);
Krishna Gudipatie6714322010-03-03 17:44:02 -08001090
Jiri Slaby8f7d3f02015-04-16 12:48:29 -07001091 spin_lock_irqsave(&bfad->bfad_lock, flags);
1092 bfad->bfad_tsk = NULL;
1093 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
Krishna Gudipatie6714322010-03-03 17:44:02 -08001094
1095 return 0;
1096}
1097
Jing Huang5fbe25c2010-10-18 17:17:23 -07001098/*
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001099 * BFA driver interrupt functions
1100 */
1101irqreturn_t
1102bfad_intx(int irq, void *dev_id)
1103{
1104 struct bfad_s *bfad = dev_id;
1105 struct list_head doneq;
1106 unsigned long flags;
1107 bfa_boolean_t rc;
1108
1109 spin_lock_irqsave(&bfad->bfad_lock, flags);
1110 rc = bfa_intx(&bfad->bfa);
1111 if (!rc) {
1112 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1113 return IRQ_NONE;
1114 }
1115
1116 bfa_comp_deq(&bfad->bfa, &doneq);
1117 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1118
1119 if (!list_empty(&doneq)) {
1120 bfa_comp_process(&bfad->bfa, &doneq);
1121
1122 spin_lock_irqsave(&bfad->bfad_lock, flags);
1123 bfa_comp_free(&bfad->bfa, &doneq);
1124 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001125 }
1126
1127 return IRQ_HANDLED;
1128
1129}
1130
1131static irqreturn_t
1132bfad_msix(int irq, void *dev_id)
1133{
1134 struct bfad_msix_s *vec = dev_id;
1135 struct bfad_s *bfad = vec->bfad;
1136 struct list_head doneq;
1137 unsigned long flags;
1138
1139 spin_lock_irqsave(&bfad->bfad_lock, flags);
1140
1141 bfa_msix(&bfad->bfa, vec->msix.entry);
1142 bfa_comp_deq(&bfad->bfa, &doneq);
1143 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1144
1145 if (!list_empty(&doneq)) {
1146 bfa_comp_process(&bfad->bfa, &doneq);
1147
1148 spin_lock_irqsave(&bfad->bfad_lock, flags);
1149 bfa_comp_free(&bfad->bfa, &doneq);
1150 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1151 }
1152
1153 return IRQ_HANDLED;
1154}
1155
Jing Huang5fbe25c2010-10-18 17:17:23 -07001156/*
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001157 * Initialize the MSIX entry table.
1158 */
1159static void
1160bfad_init_msix_entry(struct bfad_s *bfad, struct msix_entry *msix_entries,
1161 int mask, int max_bit)
1162{
1163 int i;
1164 int match = 0x00000001;
1165
1166 for (i = 0, bfad->nvec = 0; i < MAX_MSIX_ENTRY; i++) {
1167 if (mask & match) {
1168 bfad->msix_tab[bfad->nvec].msix.entry = i;
1169 bfad->msix_tab[bfad->nvec].bfad = bfad;
1170 msix_entries[bfad->nvec].entry = i;
1171 bfad->nvec++;
1172 }
1173
1174 match <<= 1;
1175 }
1176
1177}
1178
1179int
1180bfad_install_msix_handler(struct bfad_s *bfad)
1181{
1182 int i, error = 0;
1183
1184 for (i = 0; i < bfad->nvec; i++) {
1185 sprintf(bfad->msix_tab[i].name, "bfa-%s-%s",
1186 bfad->pci_name,
Krishna Gudipati11189202011-06-13 15:50:35 -07001187 ((bfa_asic_id_cb(bfad->hal_pcidev.device_id)) ?
1188 msix_name_cb[i] : msix_name_ct[i]));
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001189
1190 error = request_irq(bfad->msix_tab[i].msix.vector,
1191 (irq_handler_t) bfad_msix, 0,
1192 bfad->msix_tab[i].name, &bfad->msix_tab[i]);
1193 bfa_trc(bfad, i);
1194 bfa_trc(bfad, bfad->msix_tab[i].msix.vector);
1195 if (error) {
1196 int j;
1197
1198 for (j = 0; j < i; j++)
1199 free_irq(bfad->msix_tab[j].msix.vector,
1200 &bfad->msix_tab[j]);
1201
Krishna Gudipati61e62e22011-06-24 20:29:07 -07001202 bfad->bfad_flags &= ~BFAD_MSIX_ON;
1203 pci_disable_msix(bfad->pcidev);
1204
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001205 return 1;
1206 }
1207 }
1208
1209 return 0;
1210}
1211
Jing Huang5fbe25c2010-10-18 17:17:23 -07001212/*
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001213 * Setup MSIX based interrupt.
1214 */
1215int
1216bfad_setup_intr(struct bfad_s *bfad)
1217{
Alexander Gordeev8cb7f632014-07-16 20:05:07 +02001218 int error;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001219 u32 mask = 0, i, num_bit = 0, max_bit = 0;
1220 struct msix_entry msix_entries[MAX_MSIX_ENTRY];
1221 struct pci_dev *pdev = bfad->pcidev;
Krishna Gudipati10a07372011-06-24 20:23:38 -07001222 u16 reg;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001223
1224 /* Call BFA to get the msix map for this PCI function. */
1225 bfa_msix_getvecs(&bfad->bfa, &mask, &num_bit, &max_bit);
1226
1227 /* Set up the msix entry table */
1228 bfad_init_msix_entry(bfad, msix_entries, mask, max_bit);
1229
Krishna Gudipati11189202011-06-13 15:50:35 -07001230 if ((bfa_asic_id_ctc(pdev->device) && !msix_disable_ct) ||
1231 (bfa_asic_id_cb(pdev->device) && !msix_disable_cb)) {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001232
Alexander Gordeevb427d002014-07-16 20:05:08 +02001233 error = pci_enable_msix_exact(bfad->pcidev,
1234 msix_entries, bfad->nvec);
Alexander Gordeevcd69a092014-07-16 20:05:06 +02001235 /* In CT1 & CT2, try to allocate just one vector */
Alexander Gordeevb427d002014-07-16 20:05:08 +02001236 if (error == -ENOSPC && bfa_asic_id_ctc(pdev->device)) {
Alexander Gordeevcd69a092014-07-16 20:05:06 +02001237 printk(KERN_WARNING "bfa %s: trying one msix "
1238 "vector failed to allocate %d[%d]\n",
1239 bfad->pci_name, bfad->nvec, error);
1240 bfad->nvec = 1;
Alexander Gordeevb427d002014-07-16 20:05:08 +02001241 error = pci_enable_msix_exact(bfad->pcidev,
1242 msix_entries, 1);
Alexander Gordeevcd69a092014-07-16 20:05:06 +02001243 }
Krishna Gudipati881c1b32012-08-22 19:52:02 -07001244
Alexander Gordeevcd69a092014-07-16 20:05:06 +02001245 if (error) {
1246 printk(KERN_WARNING "bfad%d: "
Alexander Gordeevb427d002014-07-16 20:05:08 +02001247 "pci_enable_msix_exact failed (%d), "
Alexander Gordeevcd69a092014-07-16 20:05:06 +02001248 "use line based.\n",
1249 bfad->inst_no, error);
1250 goto line_based;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001251 }
1252
Krishna Gudipati10a07372011-06-24 20:23:38 -07001253 /* Disable INTX in MSI-X mode */
1254 pci_read_config_word(pdev, PCI_COMMAND, &reg);
1255
1256 if (!(reg & PCI_COMMAND_INTX_DISABLE))
1257 pci_write_config_word(pdev, PCI_COMMAND,
1258 reg | PCI_COMMAND_INTX_DISABLE);
1259
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001260 /* Save the vectors */
1261 for (i = 0; i < bfad->nvec; i++) {
1262 bfa_trc(bfad, msix_entries[i].vector);
1263 bfad->msix_tab[i].msix.vector = msix_entries[i].vector;
1264 }
1265
1266 bfa_msix_init(&bfad->bfa, bfad->nvec);
1267
1268 bfad->bfad_flags |= BFAD_MSIX_ON;
1269
Alexander Gordeev8cb7f632014-07-16 20:05:07 +02001270 return 0;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001271 }
1272
1273line_based:
Alexander Gordeev8cb7f632014-07-16 20:05:07 +02001274 error = request_irq(bfad->pcidev->irq, (irq_handler_t)bfad_intx,
1275 BFAD_IRQ_FLAGS, BFAD_DRIVER_NAME, bfad);
1276 if (error)
1277 return error;
1278
Krishna Gudipati61e62e22011-06-24 20:29:07 -07001279 bfad->bfad_flags |= BFAD_INTX_ON;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001280
Alexander Gordeev8cb7f632014-07-16 20:05:07 +02001281 return 0;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001282}
1283
1284void
1285bfad_remove_intr(struct bfad_s *bfad)
1286{
1287 int i;
1288
1289 if (bfad->bfad_flags & BFAD_MSIX_ON) {
1290 for (i = 0; i < bfad->nvec; i++)
1291 free_irq(bfad->msix_tab[i].msix.vector,
1292 &bfad->msix_tab[i]);
1293
1294 pci_disable_msix(bfad->pcidev);
1295 bfad->bfad_flags &= ~BFAD_MSIX_ON;
Krishna Gudipati61e62e22011-06-24 20:29:07 -07001296 } else if (bfad->bfad_flags & BFAD_INTX_ON) {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001297 free_irq(bfad->pcidev->irq, bfad);
1298 }
1299}
Jing Huang7725ccf2009-09-23 17:46:15 -07001300
Jing Huang5fbe25c2010-10-18 17:17:23 -07001301/*
Jing Huang7725ccf2009-09-23 17:46:15 -07001302 * PCI probe entry.
1303 */
1304int
1305bfad_pci_probe(struct pci_dev *pdev, const struct pci_device_id *pid)
1306{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001307 struct bfad_s *bfad;
Krishna Gudipati7826f302011-07-20 16:59:13 -07001308 int error = -ENODEV, retval, i;
Jing Huang7725ccf2009-09-23 17:46:15 -07001309
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001310 /* For single port cards - only claim function 0 */
Krishna Gudipati8b070b42011-06-13 15:52:40 -07001311 if ((pdev->device == BFA_PCI_DEVICE_ID_FC_8G1P) &&
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001312 (PCI_FUNC(pdev->devfn) != 0))
Jing Huang7725ccf2009-09-23 17:46:15 -07001313 return -ENODEV;
1314
Jing Huang7725ccf2009-09-23 17:46:15 -07001315 bfad = kzalloc(sizeof(struct bfad_s), GFP_KERNEL);
1316 if (!bfad) {
1317 error = -ENOMEM;
1318 goto out;
1319 }
1320
1321 bfad->trcmod = kzalloc(sizeof(struct bfa_trc_mod_s), GFP_KERNEL);
1322 if (!bfad->trcmod) {
1323 printk(KERN_WARNING "Error alloc trace buffer!\n");
1324 error = -ENOMEM;
1325 goto out_alloc_trace_failure;
1326 }
1327
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001328 /* TRACE INIT */
Jing Huang7725ccf2009-09-23 17:46:15 -07001329 bfa_trc_init(bfad->trcmod);
1330 bfa_trc(bfad, bfad_inst);
1331
Krishna Gudipati7826f302011-07-20 16:59:13 -07001332 /* AEN INIT */
1333 INIT_LIST_HEAD(&bfad->free_aen_q);
1334 INIT_LIST_HEAD(&bfad->active_aen_q);
1335 for (i = 0; i < BFA_AEN_MAX_ENTRY; i++)
1336 list_add_tail(&bfad->aen_list[i].qe, &bfad->free_aen_q);
1337
Jing Huang7725ccf2009-09-23 17:46:15 -07001338 if (!(bfad_load_fwimg(pdev))) {
Jing Huang7725ccf2009-09-23 17:46:15 -07001339 kfree(bfad->trcmod);
1340 goto out_alloc_trace_failure;
1341 }
1342
1343 retval = bfad_pci_init(pdev, bfad);
1344 if (retval) {
1345 printk(KERN_WARNING "bfad_pci_init failure!\n");
1346 error = retval;
1347 goto out_pci_init_failure;
1348 }
1349
1350 mutex_lock(&bfad_mutex);
1351 bfad->inst_no = bfad_inst++;
1352 list_add_tail(&bfad->list_entry, &bfad_list);
1353 mutex_unlock(&bfad_mutex);
1354
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001355 /* Initializing the state machine: State set to uninit */
1356 bfa_sm_set_state(bfad, bfad_sm_uninit);
1357
Jing Huang7725ccf2009-09-23 17:46:15 -07001358 spin_lock_init(&bfad->bfad_lock);
Kyle McMartin0b1017a2012-07-11 12:42:55 -04001359 spin_lock_init(&bfad->bfad_aen_spinlock);
1360
Jing Huang7725ccf2009-09-23 17:46:15 -07001361 pci_set_drvdata(pdev, bfad);
1362
1363 bfad->ref_count = 0;
1364 bfad->pport.bfad = bfad;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001365 INIT_LIST_HEAD(&bfad->pbc_vport_list);
Krishna Gudipati5b7db7a2011-12-20 18:58:32 -08001366 INIT_LIST_HEAD(&bfad->vport_list);
Krishna Gudipatie6714322010-03-03 17:44:02 -08001367
Krishna Gudipati7c38c052011-04-14 16:50:35 -07001368 /* Setup the debugfs node for this bfad */
1369 if (bfa_debugfs_enable)
1370 bfad_debugfs_init(&bfad->pport);
1371
Jing Huang7725ccf2009-09-23 17:46:15 -07001372 retval = bfad_drv_init(bfad);
1373 if (retval != BFA_STATUS_OK)
1374 goto out_drv_init_failure;
Jing Huang7725ccf2009-09-23 17:46:15 -07001375
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001376 bfa_sm_send_event(bfad, BFAD_E_CREATE);
Jing Huang7725ccf2009-09-23 17:46:15 -07001377
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001378 if (bfa_sm_cmp_state(bfad, bfad_sm_uninit))
1379 goto out_bfad_sm_failure;
Jing Huang7725ccf2009-09-23 17:46:15 -07001380
Jing Huang7725ccf2009-09-23 17:46:15 -07001381 return 0;
1382
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001383out_bfad_sm_failure:
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001384 bfad_hal_mem_release(bfad);
Jing Huang7725ccf2009-09-23 17:46:15 -07001385out_drv_init_failure:
Krishna Gudipati7c38c052011-04-14 16:50:35 -07001386 /* Remove the debugfs node for this bfad */
1387 kfree(bfad->regdata);
1388 bfad_debugfs_exit(&bfad->pport);
Jing Huang7725ccf2009-09-23 17:46:15 -07001389 mutex_lock(&bfad_mutex);
1390 bfad_inst--;
1391 list_del(&bfad->list_entry);
1392 mutex_unlock(&bfad_mutex);
1393 bfad_pci_uninit(pdev, bfad);
1394out_pci_init_failure:
1395 kfree(bfad->trcmod);
1396out_alloc_trace_failure:
1397 kfree(bfad);
1398out:
1399 return error;
1400}
1401
Jing Huang5fbe25c2010-10-18 17:17:23 -07001402/*
Jing Huang7725ccf2009-09-23 17:46:15 -07001403 * PCI remove entry.
1404 */
1405void
1406bfad_pci_remove(struct pci_dev *pdev)
1407{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001408 struct bfad_s *bfad = pci_get_drvdata(pdev);
1409 unsigned long flags;
Jing Huang7725ccf2009-09-23 17:46:15 -07001410
1411 bfa_trc(bfad, bfad->inst_no);
1412
Krishna Gudipatie6714322010-03-03 17:44:02 -08001413 spin_lock_irqsave(&bfad->bfad_lock, flags);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001414 if (bfad->bfad_tsk != NULL) {
1415 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
Krishna Gudipatie6714322010-03-03 17:44:02 -08001416 kthread_stop(bfad->bfad_tsk);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001417 } else {
Jing Huang7725ccf2009-09-23 17:46:15 -07001418 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
Jing Huang7725ccf2009-09-23 17:46:15 -07001419 }
1420
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001421 /* Send Event BFAD_E_STOP */
1422 bfa_sm_send_event(bfad, BFAD_E_STOP);
Jing Huang7725ccf2009-09-23 17:46:15 -07001423
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001424 /* Driver detach and dealloc mem */
Jing Huang7725ccf2009-09-23 17:46:15 -07001425 spin_lock_irqsave(&bfad->bfad_lock, flags);
1426 bfa_detach(&bfad->bfa);
1427 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1428 bfad_hal_mem_release(bfad);
Jing Huang7725ccf2009-09-23 17:46:15 -07001429
Krishna Gudipati7c38c052011-04-14 16:50:35 -07001430 /* Remove the debugfs node for this bfad */
1431 kfree(bfad->regdata);
1432 bfad_debugfs_exit(&bfad->pport);
1433
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001434 /* Cleaning the BFAD instance */
Jing Huang7725ccf2009-09-23 17:46:15 -07001435 mutex_lock(&bfad_mutex);
1436 bfad_inst--;
1437 list_del(&bfad->list_entry);
1438 mutex_unlock(&bfad_mutex);
1439 bfad_pci_uninit(pdev, bfad);
1440
1441 kfree(bfad->trcmod);
1442 kfree(bfad);
1443}
1444
Krishna Gudipati881c1b32012-08-22 19:52:02 -07001445/*
1446 * PCI Error Recovery entry, error detected.
1447 */
1448static pci_ers_result_t
1449bfad_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
1450{
1451 struct bfad_s *bfad = pci_get_drvdata(pdev);
1452 unsigned long flags;
1453 pci_ers_result_t ret = PCI_ERS_RESULT_NONE;
1454
1455 dev_printk(KERN_ERR, &pdev->dev,
1456 "error detected state: %d - flags: 0x%x\n",
1457 state, bfad->bfad_flags);
1458
1459 switch (state) {
1460 case pci_channel_io_normal: /* non-fatal error */
1461 spin_lock_irqsave(&bfad->bfad_lock, flags);
1462 bfad->bfad_flags &= ~BFAD_EEH_BUSY;
1463 /* Suspend/fail all bfa operations */
1464 bfa_ioc_suspend(&bfad->bfa.ioc);
1465 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1466 del_timer_sync(&bfad->hal_tmo);
1467 ret = PCI_ERS_RESULT_CAN_RECOVER;
1468 break;
1469 case pci_channel_io_frozen: /* fatal error */
1470 init_completion(&bfad->comp);
1471 spin_lock_irqsave(&bfad->bfad_lock, flags);
1472 bfad->bfad_flags |= BFAD_EEH_BUSY;
1473 /* Suspend/fail all bfa operations */
1474 bfa_ioc_suspend(&bfad->bfa.ioc);
1475 bfa_fcs_stop(&bfad->bfa_fcs);
1476 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1477 wait_for_completion(&bfad->comp);
1478
1479 bfad_remove_intr(bfad);
1480 del_timer_sync(&bfad->hal_tmo);
1481 pci_disable_device(pdev);
1482 ret = PCI_ERS_RESULT_NEED_RESET;
1483 break;
1484 case pci_channel_io_perm_failure: /* PCI Card is DEAD */
1485 spin_lock_irqsave(&bfad->bfad_lock, flags);
1486 bfad->bfad_flags |= BFAD_EEH_BUSY |
1487 BFAD_EEH_PCI_CHANNEL_IO_PERM_FAILURE;
1488 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1489
1490 /* If the error_detected handler is called with the reason
1491 * pci_channel_io_perm_failure - it will subsequently call
1492 * pci_remove() entry point to remove the pci device from the
1493 * system - So defer the cleanup to pci_remove(); cleaning up
1494 * here causes inconsistent state during pci_remove().
1495 */
1496 ret = PCI_ERS_RESULT_DISCONNECT;
1497 break;
1498 default:
1499 WARN_ON(1);
1500 }
1501
1502 return ret;
1503}
1504
1505int
1506restart_bfa(struct bfad_s *bfad)
1507{
1508 unsigned long flags;
1509 struct pci_dev *pdev = bfad->pcidev;
1510
1511 bfa_attach(&bfad->bfa, bfad, &bfad->ioc_cfg,
1512 &bfad->meminfo, &bfad->hal_pcidev);
1513
1514 /* Enable Interrupt and wait bfa_init completion */
1515 if (bfad_setup_intr(bfad)) {
1516 dev_printk(KERN_WARNING, &pdev->dev,
1517 "%s: bfad_setup_intr failed\n", bfad->pci_name);
Vijaya Mohan Guvvada3e0be2013-11-21 01:37:33 -08001518 bfa_sm_send_event(bfad, BFAD_E_INIT_FAILED);
Krishna Gudipati881c1b32012-08-22 19:52:02 -07001519 return -1;
1520 }
1521
1522 init_completion(&bfad->comp);
1523 spin_lock_irqsave(&bfad->bfad_lock, flags);
1524 bfa_iocfc_init(&bfad->bfa);
1525 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1526
1527 /* Set up interrupt handler for each vectors */
1528 if ((bfad->bfad_flags & BFAD_MSIX_ON) &&
1529 bfad_install_msix_handler(bfad))
1530 dev_printk(KERN_WARNING, &pdev->dev,
1531 "%s: install_msix failed.\n", bfad->pci_name);
1532
1533 bfad_init_timer(bfad);
1534 wait_for_completion(&bfad->comp);
1535 bfad_drv_start(bfad);
1536
1537 return 0;
1538}
1539
1540/*
1541 * PCI Error Recovery entry, re-initialize the chip.
1542 */
1543static pci_ers_result_t
1544bfad_pci_slot_reset(struct pci_dev *pdev)
1545{
1546 struct bfad_s *bfad = pci_get_drvdata(pdev);
1547 u8 byte;
1548
1549 dev_printk(KERN_ERR, &pdev->dev,
1550 "bfad_pci_slot_reset flags: 0x%x\n", bfad->bfad_flags);
1551
1552 if (pci_enable_device(pdev)) {
1553 dev_printk(KERN_ERR, &pdev->dev, "Cannot re-enable "
1554 "PCI device after reset.\n");
1555 return PCI_ERS_RESULT_DISCONNECT;
1556 }
1557
1558 pci_restore_state(pdev);
1559
1560 /*
1561 * Read some byte (e.g. DMA max. payload size which can't
1562 * be 0xff any time) to make sure - we did not hit another PCI error
1563 * in the middle of recovery. If we did, then declare permanent failure.
1564 */
1565 pci_read_config_byte(pdev, 0x68, &byte);
1566 if (byte == 0xff) {
1567 dev_printk(KERN_ERR, &pdev->dev,
1568 "slot_reset failed ... got another PCI error !\n");
1569 goto out_disable_device;
1570 }
1571
1572 pci_save_state(pdev);
1573 pci_set_master(pdev);
1574
1575 if (pci_set_dma_mask(bfad->pcidev, DMA_BIT_MASK(64)) != 0)
1576 if (pci_set_dma_mask(bfad->pcidev, DMA_BIT_MASK(32)) != 0)
1577 goto out_disable_device;
1578
1579 pci_cleanup_aer_uncorrect_error_status(pdev);
1580
1581 if (restart_bfa(bfad) == -1)
1582 goto out_disable_device;
1583
1584 pci_enable_pcie_error_reporting(pdev);
1585 dev_printk(KERN_WARNING, &pdev->dev,
1586 "slot_reset completed flags: 0x%x!\n", bfad->bfad_flags);
1587
1588 return PCI_ERS_RESULT_RECOVERED;
1589
1590out_disable_device:
1591 pci_disable_device(pdev);
1592 return PCI_ERS_RESULT_DISCONNECT;
1593}
1594
1595static pci_ers_result_t
1596bfad_pci_mmio_enabled(struct pci_dev *pdev)
1597{
1598 unsigned long flags;
1599 struct bfad_s *bfad = pci_get_drvdata(pdev);
1600
1601 dev_printk(KERN_INFO, &pdev->dev, "mmio_enabled\n");
1602
1603 /* Fetch FW diagnostic information */
1604 bfa_ioc_debug_save_ftrc(&bfad->bfa.ioc);
1605
1606 /* Cancel all pending IOs */
1607 spin_lock_irqsave(&bfad->bfad_lock, flags);
1608 init_completion(&bfad->comp);
1609 bfa_fcs_stop(&bfad->bfa_fcs);
1610 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1611 wait_for_completion(&bfad->comp);
1612
1613 bfad_remove_intr(bfad);
1614 del_timer_sync(&bfad->hal_tmo);
1615 pci_disable_device(pdev);
1616
1617 return PCI_ERS_RESULT_NEED_RESET;
1618}
1619
1620static void
1621bfad_pci_resume(struct pci_dev *pdev)
1622{
1623 unsigned long flags;
1624 struct bfad_s *bfad = pci_get_drvdata(pdev);
1625
1626 dev_printk(KERN_WARNING, &pdev->dev, "resume\n");
1627
1628 /* wait until the link is online */
1629 bfad_rport_online_wait(bfad);
1630
1631 spin_lock_irqsave(&bfad->bfad_lock, flags);
1632 bfad->bfad_flags &= ~BFAD_EEH_BUSY;
1633 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1634}
1635
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001636struct pci_device_id bfad_id_table[] = {
Jing Huang7725ccf2009-09-23 17:46:15 -07001637 {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001638 .vendor = BFA_PCI_VENDOR_ID_BROCADE,
1639 .device = BFA_PCI_DEVICE_ID_FC_8G2P,
1640 .subvendor = PCI_ANY_ID,
1641 .subdevice = PCI_ANY_ID,
1642 },
Jing Huang7725ccf2009-09-23 17:46:15 -07001643 {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001644 .vendor = BFA_PCI_VENDOR_ID_BROCADE,
1645 .device = BFA_PCI_DEVICE_ID_FC_8G1P,
1646 .subvendor = PCI_ANY_ID,
1647 .subdevice = PCI_ANY_ID,
1648 },
Jing Huang7725ccf2009-09-23 17:46:15 -07001649 {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001650 .vendor = BFA_PCI_VENDOR_ID_BROCADE,
1651 .device = BFA_PCI_DEVICE_ID_CT,
1652 .subvendor = PCI_ANY_ID,
1653 .subdevice = PCI_ANY_ID,
1654 .class = (PCI_CLASS_SERIAL_FIBER << 8),
1655 .class_mask = ~0,
1656 },
Jing Huang293f82d2010-07-08 19:45:20 -07001657 {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001658 .vendor = BFA_PCI_VENDOR_ID_BROCADE,
1659 .device = BFA_PCI_DEVICE_ID_CT_FC,
1660 .subvendor = PCI_ANY_ID,
1661 .subdevice = PCI_ANY_ID,
1662 .class = (PCI_CLASS_SERIAL_FIBER << 8),
1663 .class_mask = ~0,
Jing Huang293f82d2010-07-08 19:45:20 -07001664 },
Krishna Gudipati11189202011-06-13 15:50:35 -07001665 {
1666 .vendor = BFA_PCI_VENDOR_ID_BROCADE,
1667 .device = BFA_PCI_DEVICE_ID_CT2,
1668 .subvendor = PCI_ANY_ID,
1669 .subdevice = PCI_ANY_ID,
1670 .class = (PCI_CLASS_SERIAL_FIBER << 8),
1671 .class_mask = ~0,
1672 },
Jing Huang7725ccf2009-09-23 17:46:15 -07001673
Vijaya Mohan Guvvaf9c867b2013-05-13 02:33:33 -07001674 {
1675 .vendor = BFA_PCI_VENDOR_ID_BROCADE,
1676 .device = BFA_PCI_DEVICE_ID_CT2_QUAD,
1677 .subvendor = PCI_ANY_ID,
1678 .subdevice = PCI_ANY_ID,
1679 .class = (PCI_CLASS_SERIAL_FIBER << 8),
1680 .class_mask = ~0,
1681 },
Jing Huang7725ccf2009-09-23 17:46:15 -07001682 {0, 0},
1683};
1684
1685MODULE_DEVICE_TABLE(pci, bfad_id_table);
1686
Krishna Gudipati881c1b32012-08-22 19:52:02 -07001687/*
1688 * PCI error recovery handlers.
1689 */
1690static struct pci_error_handlers bfad_err_handler = {
1691 .error_detected = bfad_pci_error_detected,
1692 .slot_reset = bfad_pci_slot_reset,
1693 .mmio_enabled = bfad_pci_mmio_enabled,
1694 .resume = bfad_pci_resume,
1695};
1696
Jing Huang7725ccf2009-09-23 17:46:15 -07001697static struct pci_driver bfad_pci_driver = {
1698 .name = BFAD_DRIVER_NAME,
1699 .id_table = bfad_id_table,
1700 .probe = bfad_pci_probe,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08001701 .remove = bfad_pci_remove,
Krishna Gudipati881c1b32012-08-22 19:52:02 -07001702 .err_handler = &bfad_err_handler,
Jing Huang7725ccf2009-09-23 17:46:15 -07001703};
1704
Jing Huang5fbe25c2010-10-18 17:17:23 -07001705/*
Jing Huang7725ccf2009-09-23 17:46:15 -07001706 * Driver module init.
1707 */
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001708static int __init
Jing Huang7725ccf2009-09-23 17:46:15 -07001709bfad_init(void)
1710{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001711 int error = 0;
Jing Huang7725ccf2009-09-23 17:46:15 -07001712
1713 printk(KERN_INFO "Brocade BFA FC/FCOE SCSI driver - version: %s\n",
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001714 BFAD_DRIVER_VERSION);
Jing Huang7725ccf2009-09-23 17:46:15 -07001715
1716 if (num_sgpgs > 0)
1717 num_sgpgs_parm = num_sgpgs;
1718
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001719 error = bfad_im_module_init();
Jing Huang7725ccf2009-09-23 17:46:15 -07001720 if (error) {
1721 error = -ENOMEM;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001722 printk(KERN_WARNING "bfad_im_module_init failure\n");
Jing Huang7725ccf2009-09-23 17:46:15 -07001723 goto ext;
1724 }
1725
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001726 if (strcmp(FCPI_NAME, " fcpim") == 0)
1727 supported_fc4s |= BFA_LPORT_ROLE_FCP_IM;
Jing Huang7725ccf2009-09-23 17:46:15 -07001728
Maggie Zhangf7f738122010-12-09 19:08:43 -08001729 bfa_auto_recover = ioc_auto_recover;
Jing Huang7725ccf2009-09-23 17:46:15 -07001730 bfa_fcs_rport_set_del_timeout(rport_del_timeout);
Krishna Gudipati61ba4392012-08-22 19:52:58 -07001731 bfa_fcs_rport_set_max_logins(max_rport_logins);
Jing Huang7725ccf2009-09-23 17:46:15 -07001732
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001733 error = pci_register_driver(&bfad_pci_driver);
Jing Huang7725ccf2009-09-23 17:46:15 -07001734 if (error) {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001735 printk(KERN_WARNING "pci_register_driver failure\n");
Jing Huang7725ccf2009-09-23 17:46:15 -07001736 goto ext;
1737 }
1738
1739 return 0;
1740
1741ext:
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001742 bfad_im_module_exit();
Jing Huang7725ccf2009-09-23 17:46:15 -07001743 return error;
1744}
1745
Jing Huang5fbe25c2010-10-18 17:17:23 -07001746/*
Jing Huang7725ccf2009-09-23 17:46:15 -07001747 * Driver module exit.
1748 */
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001749static void __exit
Jing Huang7725ccf2009-09-23 17:46:15 -07001750bfad_exit(void)
1751{
1752 pci_unregister_driver(&bfad_pci_driver);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001753 bfad_im_module_exit();
Jing Huang7725ccf2009-09-23 17:46:15 -07001754 bfad_free_fwimg();
1755}
1756
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001757/* Firmware handling */
Jing Huang61338a02011-04-13 11:44:03 -07001758static void
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001759bfad_read_firmware(struct pci_dev *pdev, u32 **bfi_image,
1760 u32 *bfi_image_size, char *fw_name)
1761{
1762 const struct firmware *fw;
1763
1764 if (request_firmware(&fw, fw_name, &pdev->dev)) {
1765 printk(KERN_ALERT "Can't locate firmware %s\n", fw_name);
Jing Huang61338a02011-04-13 11:44:03 -07001766 *bfi_image = NULL;
1767 goto out;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001768 }
1769
1770 *bfi_image = vmalloc(fw->size);
1771 if (NULL == *bfi_image) {
1772 printk(KERN_ALERT "Fail to allocate buffer for fw image "
1773 "size=%x!\n", (u32) fw->size);
Jing Huang61338a02011-04-13 11:44:03 -07001774 goto out;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001775 }
1776
1777 memcpy(*bfi_image, fw->data, fw->size);
1778 *bfi_image_size = fw->size/sizeof(u32);
Jing Huang61338a02011-04-13 11:44:03 -07001779out:
1780 release_firmware(fw);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001781}
1782
Jing Huang61338a02011-04-13 11:44:03 -07001783static u32 *
1784bfad_load_fwimg(struct pci_dev *pdev)
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001785{
Vijaya Mohan Guvvadcaf9ae2013-12-04 05:43:58 -08001786 if (bfa_asic_id_ct2(pdev->device)) {
Krishna Gudipati11189202011-06-13 15:50:35 -07001787 if (bfi_image_ct2_size == 0)
1788 bfad_read_firmware(pdev, &bfi_image_ct2,
1789 &bfi_image_ct2_size, BFAD_FW_FILE_CT2);
1790 return bfi_image_ct2;
1791 } else if (bfa_asic_id_ct(pdev->device)) {
1792 if (bfi_image_ct_size == 0)
1793 bfad_read_firmware(pdev, &bfi_image_ct,
1794 &bfi_image_ct_size, BFAD_FW_FILE_CT);
1795 return bfi_image_ct;
Vijaya Mohan Guvvadcaf9ae2013-12-04 05:43:58 -08001796 } else if (bfa_asic_id_cb(pdev->device)) {
Krishna Gudipati11189202011-06-13 15:50:35 -07001797 if (bfi_image_cb_size == 0)
1798 bfad_read_firmware(pdev, &bfi_image_cb,
1799 &bfi_image_cb_size, BFAD_FW_FILE_CB);
1800 return bfi_image_cb;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001801 }
Vijaya Mohan Guvvadcaf9ae2013-12-04 05:43:58 -08001802
1803 return NULL;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001804}
Jing Huang7725ccf2009-09-23 17:46:15 -07001805
Jing Huang61338a02011-04-13 11:44:03 -07001806static void
1807bfad_free_fwimg(void)
1808{
Krishna Gudipati11189202011-06-13 15:50:35 -07001809 if (bfi_image_ct2_size && bfi_image_ct2)
1810 vfree(bfi_image_ct2);
1811 if (bfi_image_ct_size && bfi_image_ct)
1812 vfree(bfi_image_ct);
1813 if (bfi_image_cb_size && bfi_image_cb)
1814 vfree(bfi_image_cb);
Jing Huang61338a02011-04-13 11:44:03 -07001815}
1816
Jing Huang7725ccf2009-09-23 17:46:15 -07001817module_init(bfad_init);
1818module_exit(bfad_exit);
1819MODULE_LICENSE("GPL");
1820MODULE_DESCRIPTION("Brocade Fibre Channel HBA Driver" BFAD_PROTO_NAME);
1821MODULE_AUTHOR("Brocade Communications Systems, Inc.");
1822MODULE_VERSION(BFAD_DRIVER_VERSION);