blob: 440ef32be048fee51aad6c7380d72778b09fe6ed [file] [log] [blame]
Thomas Gleixner52fa7bf2019-05-29 07:18:06 -07001// SPDX-License-Identifier: GPL-2.0-only
Jing Huang7725ccf2009-09-23 17:46:15 -07002/*
Anil Gurumurthy889d0d42015-11-26 03:54:45 -05003 * Copyright (c) 2005-2014 Brocade Communications Systems, Inc.
4 * Copyright (c) 2014- QLogic Corporation.
Jing Huang7725ccf2009-09-23 17:46:15 -07005 * All rights reserved
Anil Gurumurthy889d0d42015-11-26 03:54:45 -05006 * www.qlogic.com
Jing Huang7725ccf2009-09-23 17:46:15 -07007 *
Anil Gurumurthy31e1d562015-11-26 03:54:46 -05008 * Linux driver for QLogic BR-series Fibre Channel Host Bus Adapter.
Jing Huang7725ccf2009-09-23 17:46:15 -07009 */
10
Jing Huang5fbe25c2010-10-18 17:17:23 -070011/*
Jing Huang7725ccf2009-09-23 17:46:15 -070012 * bfad.c Linux driver PCI interface module.
13 */
Jing Huang7725ccf2009-09-23 17:46:15 -070014#include <linux/module.h>
Krishna Gudipatie6714322010-03-03 17:44:02 -080015#include <linux/kthread.h>
Krishna Gudipatia36c61f2010-09-15 11:50:55 -070016#include <linux/errno.h>
17#include <linux/sched.h>
18#include <linux/init.h>
19#include <linux/fs.h>
20#include <linux/pci.h>
21#include <linux/firmware.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080022#include <linux/uaccess.h>
Krishna Gudipatia36c61f2010-09-15 11:50:55 -070023#include <asm/fcntl.h>
24
Jing Huang7725ccf2009-09-23 17:46:15 -070025#include "bfad_drv.h"
26#include "bfad_im.h"
Krishna Gudipatia36c61f2010-09-15 11:50:55 -070027#include "bfa_fcs.h"
Krishna Gudipatia36c61f2010-09-15 11:50:55 -070028#include "bfa_defs.h"
29#include "bfa.h"
Jing Huang7725ccf2009-09-23 17:46:15 -070030
31BFA_TRC_FILE(LDRV, BFAD);
Jing Huang42b426e2010-03-19 11:07:09 -070032DEFINE_MUTEX(bfad_mutex);
Jing Huang7725ccf2009-09-23 17:46:15 -070033LIST_HEAD(bfad_list);
Jing Huang7725ccf2009-09-23 17:46:15 -070034
Krishna Gudipatia36c61f2010-09-15 11:50:55 -070035static int bfad_inst;
36static int num_sgpgs_parm;
37int supported_fc4s;
38char *host_name, *os_name, *os_patch;
39int num_rports, num_ios, num_tms;
40int num_fcxps, num_ufbufs;
41int reqq_size, rspq_size, num_sgpgs;
42int rport_del_timeout = BFA_FCS_RPORT_DEF_DEL_TIMEOUT;
43int bfa_lun_queue_depth = BFAD_LUN_QUEUE_DEPTH;
44int bfa_io_max_sge = BFAD_IO_MAX_SGE;
Jing Huang88166242010-12-09 17:11:53 -080045int bfa_log_level = 3; /* WARNING log level */
Krishna Gudipatia36c61f2010-09-15 11:50:55 -070046int ioc_auto_recover = BFA_TRUE;
47int bfa_linkup_delay = -1;
48int fdmi_enable = BFA_TRUE;
49int pcie_max_read_reqsz;
Jing Huangab2a9ba2010-07-08 20:02:55 -070050int bfa_debugfs_enable = 1;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -070051int msix_disable_cb = 0, msix_disable_ct = 0;
Krishna Gudipati61e62e22011-06-24 20:29:07 -070052int max_xfer_size = BFAD_MAX_SECTORS >> 1;
Jason Yan4d5956f2020-04-07 11:22:02 +080053static int max_rport_logins = BFA_FCS_MAX_RPORT_LOGINS;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -070054
Jing Huang61338a02011-04-13 11:44:03 -070055/* Firmware releated */
Krishna Gudipati11189202011-06-13 15:50:35 -070056u32 bfi_image_cb_size, bfi_image_ct_size, bfi_image_ct2_size;
57u32 *bfi_image_cb, *bfi_image_ct, *bfi_image_ct2;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -070058
Benjamin Poirier2d1148f2016-12-23 20:40:19 -080059#define BFAD_FW_FILE_CB "cbfw-3.2.5.1.bin"
60#define BFAD_FW_FILE_CT "ctfw-3.2.5.1.bin"
61#define BFAD_FW_FILE_CT2 "ct2fw-3.2.5.1.bin"
Jing Huang61338a02011-04-13 11:44:03 -070062
63static u32 *bfad_load_fwimg(struct pci_dev *pdev);
64static void bfad_free_fwimg(void);
65static void bfad_read_firmware(struct pci_dev *pdev, u32 **bfi_image,
66 u32 *bfi_image_size, char *fw_name);
67
Maggie52f94b62010-11-29 18:21:32 -080068static const char *msix_name_ct[] = {
Krishna Gudipati11189202011-06-13 15:50:35 -070069 "ctrl",
Krishna Gudipatia36c61f2010-09-15 11:50:55 -070070 "cpe0", "cpe1", "cpe2", "cpe3",
Krishna Gudipati11189202011-06-13 15:50:35 -070071 "rme0", "rme1", "rme2", "rme3" };
Krishna Gudipatia36c61f2010-09-15 11:50:55 -070072
Maggie52f94b62010-11-29 18:21:32 -080073static const char *msix_name_cb[] = {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -070074 "cpe0", "cpe1", "cpe2", "cpe3",
75 "rme0", "rme1", "rme2", "rme3",
76 "eemc", "elpu0", "elpu1", "epss", "mlpu" };
77
Krishna Gudipati11189202011-06-13 15:50:35 -070078MODULE_FIRMWARE(BFAD_FW_FILE_CB);
79MODULE_FIRMWARE(BFAD_FW_FILE_CT);
80MODULE_FIRMWARE(BFAD_FW_FILE_CT2);
Jing Huang7725ccf2009-09-23 17:46:15 -070081
82module_param(os_name, charp, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -070083MODULE_PARM_DESC(os_name, "OS name of the hba host machine");
Jing Huang7725ccf2009-09-23 17:46:15 -070084module_param(os_patch, charp, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -070085MODULE_PARM_DESC(os_patch, "OS patch level of the hba host machine");
Jing Huang7725ccf2009-09-23 17:46:15 -070086module_param(host_name, charp, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -070087MODULE_PARM_DESC(host_name, "Hostname of the hba host machine");
Jing Huang7725ccf2009-09-23 17:46:15 -070088module_param(num_rports, int, S_IRUGO | S_IWUSR);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -070089MODULE_PARM_DESC(num_rports, "Max number of rports supported per port "
90 "(physical/logical), default=1024");
Jing Huang7725ccf2009-09-23 17:46:15 -070091module_param(num_ios, int, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -070092MODULE_PARM_DESC(num_ios, "Max number of ioim requests, default=2000");
Jing Huang7725ccf2009-09-23 17:46:15 -070093module_param(num_tms, int, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -070094MODULE_PARM_DESC(num_tms, "Max number of task im requests, default=128");
Jing Huang7725ccf2009-09-23 17:46:15 -070095module_param(num_fcxps, int, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -070096MODULE_PARM_DESC(num_fcxps, "Max number of fcxp requests, default=64");
Jing Huang7725ccf2009-09-23 17:46:15 -070097module_param(num_ufbufs, int, S_IRUGO | S_IWUSR);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -070098MODULE_PARM_DESC(num_ufbufs, "Max number of unsolicited frame "
99 "buffers, default=64");
Jing Huang7725ccf2009-09-23 17:46:15 -0700100module_param(reqq_size, int, S_IRUGO | S_IWUSR);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700101MODULE_PARM_DESC(reqq_size, "Max number of request queue elements, "
102 "default=256");
Jing Huang7725ccf2009-09-23 17:46:15 -0700103module_param(rspq_size, int, S_IRUGO | S_IWUSR);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700104MODULE_PARM_DESC(rspq_size, "Max number of response queue elements, "
105 "default=64");
Jing Huang7725ccf2009-09-23 17:46:15 -0700106module_param(num_sgpgs, int, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -0700107MODULE_PARM_DESC(num_sgpgs, "Number of scatter/gather pages, default=2048");
Jing Huang7725ccf2009-09-23 17:46:15 -0700108module_param(rport_del_timeout, int, S_IRUGO | S_IWUSR);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700109MODULE_PARM_DESC(rport_del_timeout, "Rport delete timeout, default=90 secs, "
110 "Range[>0]");
Jing Huang7725ccf2009-09-23 17:46:15 -0700111module_param(bfa_lun_queue_depth, int, S_IRUGO | S_IWUSR);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700112MODULE_PARM_DESC(bfa_lun_queue_depth, "Lun queue depth, default=32, Range[>0]");
Jing Huang7725ccf2009-09-23 17:46:15 -0700113module_param(bfa_io_max_sge, int, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -0700114MODULE_PARM_DESC(bfa_io_max_sge, "Max io scatter/gather elements, default=255");
Jing Huang88166242010-12-09 17:11:53 -0800115module_param(bfa_log_level, int, S_IRUGO | S_IWUSR);
116MODULE_PARM_DESC(bfa_log_level, "Driver log level, default=3, "
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700117 "Range[Critical:1|Error:2|Warning:3|Info:4]");
Jing Huang7725ccf2009-09-23 17:46:15 -0700118module_param(ioc_auto_recover, int, S_IRUGO | S_IWUSR);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700119MODULE_PARM_DESC(ioc_auto_recover, "IOC auto recovery, default=1, "
120 "Range[off:0|on:1]");
Jing Huang7725ccf2009-09-23 17:46:15 -0700121module_param(bfa_linkup_delay, int, S_IRUGO | S_IWUSR);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700122MODULE_PARM_DESC(bfa_linkup_delay, "Link up delay, default=30 secs for "
123 "boot port. Otherwise 10 secs in RHEL4 & 0 for "
124 "[RHEL5, SLES10, ESX40] Range[>0]");
125module_param(msix_disable_cb, int, S_IRUGO | S_IWUSR);
Anil Gurumurthy31e1d562015-11-26 03:54:46 -0500126MODULE_PARM_DESC(msix_disable_cb, "Disable Message Signaled Interrupts for QLogic-415/425/815/825 cards, default=0 Range[false:0|true:1]");
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700127module_param(msix_disable_ct, int, S_IRUGO | S_IWUSR);
Anil Gurumurthy31e1d562015-11-26 03:54:46 -0500128MODULE_PARM_DESC(msix_disable_ct, "Disable Message Signaled Interrupts if possible for QLogic-1010/1020/804/1007/902/1741 cards, default=0, Range[false:0|true:1]");
Jing Huang604158ad2010-07-08 19:59:49 -0700129module_param(fdmi_enable, int, S_IRUGO | S_IWUSR);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700130MODULE_PARM_DESC(fdmi_enable, "Enables fdmi registration, default=1, "
131 "Range[false:0|true:1]");
132module_param(pcie_max_read_reqsz, int, S_IRUGO | S_IWUSR);
133MODULE_PARM_DESC(pcie_max_read_reqsz, "PCIe max read request size, default=0 "
134 "(use system setting), Range[128|256|512|1024|2048|4096]");
Jing Huangab2a9ba2010-07-08 20:02:55 -0700135module_param(bfa_debugfs_enable, int, S_IRUGO | S_IWUSR);
136MODULE_PARM_DESC(bfa_debugfs_enable, "Enables debugfs feature, default=1,"
137 " Range[false:0|true:1]");
Krishna Gudipati61e62e22011-06-24 20:29:07 -0700138module_param(max_xfer_size, int, S_IRUGO | S_IWUSR);
139MODULE_PARM_DESC(max_xfer_size, "default=32MB,"
140 " Range[64k|128k|256k|512k|1024k|2048k]");
Krishna Gudipati61ba4392012-08-22 19:52:58 -0700141module_param(max_rport_logins, int, S_IRUGO | S_IWUSR);
142MODULE_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 -0700143
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700144static void
145bfad_sm_uninit(struct bfad_s *bfad, enum bfad_sm_event event);
146static void
147bfad_sm_created(struct bfad_s *bfad, enum bfad_sm_event event);
148static void
149bfad_sm_initializing(struct bfad_s *bfad, enum bfad_sm_event event);
150static void
151bfad_sm_operational(struct bfad_s *bfad, enum bfad_sm_event event);
152static void
153bfad_sm_stopping(struct bfad_s *bfad, enum bfad_sm_event event);
154static void
155bfad_sm_failed(struct bfad_s *bfad, enum bfad_sm_event event);
156static void
157bfad_sm_fcs_exit(struct bfad_s *bfad, enum bfad_sm_event event);
158
Jing Huang5fbe25c2010-10-18 17:17:23 -0700159/*
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700160 * Beginning state for the driver instance, awaiting the pci_probe event
Jing Huang7725ccf2009-09-23 17:46:15 -0700161 */
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700162static void
163bfad_sm_uninit(struct bfad_s *bfad, enum bfad_sm_event event)
Jing Huang7725ccf2009-09-23 17:46:15 -0700164{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700165 bfa_trc(bfad, event);
Jing Huang7725ccf2009-09-23 17:46:15 -0700166
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700167 switch (event) {
168 case BFAD_E_CREATE:
169 bfa_sm_set_state(bfad, bfad_sm_created);
170 bfad->bfad_tsk = kthread_create(bfad_worker, (void *) bfad,
171 "%s", "bfad_worker");
172 if (IS_ERR(bfad->bfad_tsk)) {
173 printk(KERN_INFO "bfad[%d]: Kernel thread "
174 "creation failed!\n", bfad->inst_no);
175 bfa_sm_send_event(bfad, BFAD_E_KTHREAD_CREATE_FAILED);
176 }
177 bfa_sm_send_event(bfad, BFAD_E_INIT);
178 break;
Jing Huang7725ccf2009-09-23 17:46:15 -0700179
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700180 case BFAD_E_STOP:
181 /* Ignore stop; already in uninit */
182 break;
Jing Huang7725ccf2009-09-23 17:46:15 -0700183
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700184 default:
185 bfa_sm_fault(bfad, event);
186 }
187}
Krishna Gudipatie6714322010-03-03 17:44:02 -0800188
Jing Huang5fbe25c2010-10-18 17:17:23 -0700189/*
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700190 * Driver Instance is created, awaiting event INIT to initialize the bfad
191 */
192static void
193bfad_sm_created(struct bfad_s *bfad, enum bfad_sm_event event)
194{
195 unsigned long flags;
Vijaya Mohan Guvvada3e0be2013-11-21 01:37:33 -0800196 bfa_status_t ret;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700197
198 bfa_trc(bfad, event);
199
200 switch (event) {
201 case BFAD_E_INIT:
202 bfa_sm_set_state(bfad, bfad_sm_initializing);
203
204 init_completion(&bfad->comp);
205
206 /* Enable Interrupt and wait bfa_init completion */
207 if (bfad_setup_intr(bfad)) {
208 printk(KERN_WARNING "bfad%d: bfad_setup_intr failed\n",
209 bfad->inst_no);
Vijaya Mohan Guvvada3e0be2013-11-21 01:37:33 -0800210 bfa_sm_send_event(bfad, BFAD_E_INIT_FAILED);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700211 break;
212 }
213
214 spin_lock_irqsave(&bfad->bfad_lock, flags);
Maggie Zhangf7f738122010-12-09 19:08:43 -0800215 bfa_iocfc_init(&bfad->bfa);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700216 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
217
218 /* Set up interrupt handler for each vectors */
219 if ((bfad->bfad_flags & BFAD_MSIX_ON) &&
220 bfad_install_msix_handler(bfad)) {
221 printk(KERN_WARNING "%s: install_msix failed, bfad%d\n",
222 __func__, bfad->inst_no);
223 }
224
225 bfad_init_timer(bfad);
226
227 wait_for_completion(&bfad->comp);
228
229 if ((bfad->bfad_flags & BFAD_HAL_INIT_DONE)) {
230 bfa_sm_send_event(bfad, BFAD_E_INIT_SUCCESS);
231 } else {
Krishna Gudipati7c38c052011-04-14 16:50:35 -0700232 printk(KERN_WARNING
233 "bfa %s: bfa init failed\n",
234 bfad->pci_name);
Vijaya Mohan Guvvada3e0be2013-11-21 01:37:33 -0800235 spin_lock_irqsave(&bfad->bfad_lock, flags);
236 bfa_fcs_init(&bfad->bfa_fcs);
237 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
238
239 ret = bfad_cfg_pport(bfad, BFA_LPORT_ROLE_FCP_IM);
240 if (ret != BFA_STATUS_OK) {
241 init_completion(&bfad->comp);
242
243 spin_lock_irqsave(&bfad->bfad_lock, flags);
244 bfad->pport.flags |= BFAD_PORT_DELETE;
245 bfa_fcs_exit(&bfad->bfa_fcs);
246 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
247
248 wait_for_completion(&bfad->comp);
249
250 bfa_sm_send_event(bfad, BFAD_E_INIT_FAILED);
251 break;
252 }
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700253 bfad->bfad_flags |= BFAD_HAL_INIT_FAIL;
Vijaya Mohan Guvvada3e0be2013-11-21 01:37:33 -0800254 bfa_sm_send_event(bfad, BFAD_E_HAL_INIT_FAILED);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700255 }
256
257 break;
258
259 case BFAD_E_KTHREAD_CREATE_FAILED:
260 bfa_sm_set_state(bfad, bfad_sm_uninit);
261 break;
262
263 default:
264 bfa_sm_fault(bfad, event);
265 }
Jing Huang7725ccf2009-09-23 17:46:15 -0700266}
267
268static void
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700269bfad_sm_initializing(struct bfad_s *bfad, enum bfad_sm_event event)
Jing Huang7725ccf2009-09-23 17:46:15 -0700270{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700271 int retval;
272 unsigned long flags;
273
274 bfa_trc(bfad, event);
275
276 switch (event) {
277 case BFAD_E_INIT_SUCCESS:
278 kthread_stop(bfad->bfad_tsk);
279 spin_lock_irqsave(&bfad->bfad_lock, flags);
280 bfad->bfad_tsk = NULL;
281 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
282
283 retval = bfad_start_ops(bfad);
Vijaya Mohan Guvvada3e0be2013-11-21 01:37:33 -0800284 if (retval != BFA_STATUS_OK) {
285 bfa_sm_set_state(bfad, bfad_sm_failed);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700286 break;
Vijaya Mohan Guvvada3e0be2013-11-21 01:37:33 -0800287 }
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700288 bfa_sm_set_state(bfad, bfad_sm_operational);
289 break;
290
Vijaya Mohan Guvvada3e0be2013-11-21 01:37:33 -0800291 case BFAD_E_INIT_FAILED:
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700292 bfa_sm_set_state(bfad, bfad_sm_uninit);
293 kthread_stop(bfad->bfad_tsk);
294 spin_lock_irqsave(&bfad->bfad_lock, flags);
295 bfad->bfad_tsk = NULL;
296 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
297 break;
298
Vijaya Mohan Guvvada3e0be2013-11-21 01:37:33 -0800299 case BFAD_E_HAL_INIT_FAILED:
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700300 bfa_sm_set_state(bfad, bfad_sm_failed);
301 break;
302 default:
303 bfa_sm_fault(bfad, event);
304 }
Jing Huang7725ccf2009-09-23 17:46:15 -0700305}
306
307static void
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700308bfad_sm_failed(struct bfad_s *bfad, enum bfad_sm_event event)
Jing Huang7725ccf2009-09-23 17:46:15 -0700309{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700310 int retval;
Jing Huang7725ccf2009-09-23 17:46:15 -0700311
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700312 bfa_trc(bfad, event);
Jing Huang7725ccf2009-09-23 17:46:15 -0700313
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700314 switch (event) {
315 case BFAD_E_INIT_SUCCESS:
316 retval = bfad_start_ops(bfad);
317 if (retval != BFA_STATUS_OK)
318 break;
319 bfa_sm_set_state(bfad, bfad_sm_operational);
320 break;
Jing Huang7725ccf2009-09-23 17:46:15 -0700321
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700322 case BFAD_E_STOP:
Vijaya Mohan Guvvada3e0be2013-11-21 01:37:33 -0800323 bfa_sm_set_state(bfad, bfad_sm_fcs_exit);
324 bfa_sm_send_event(bfad, BFAD_E_FCS_EXIT_COMP);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700325 break;
Jing Huang7725ccf2009-09-23 17:46:15 -0700326
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700327 case BFAD_E_EXIT_COMP:
328 bfa_sm_set_state(bfad, bfad_sm_uninit);
329 bfad_remove_intr(bfad);
330 del_timer_sync(&bfad->hal_tmo);
331 break;
Jing Huang7725ccf2009-09-23 17:46:15 -0700332
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700333 default:
334 bfa_sm_fault(bfad, event);
335 }
Jing Huang7725ccf2009-09-23 17:46:15 -0700336}
337
338static void
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700339bfad_sm_operational(struct bfad_s *bfad, enum bfad_sm_event event)
Jing Huang7725ccf2009-09-23 17:46:15 -0700340{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700341 bfa_trc(bfad, event);
Jing Huang7725ccf2009-09-23 17:46:15 -0700342
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700343 switch (event) {
344 case BFAD_E_STOP:
345 bfa_sm_set_state(bfad, bfad_sm_fcs_exit);
346 bfad_fcs_stop(bfad);
347 break;
Jing Huang7725ccf2009-09-23 17:46:15 -0700348
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700349 default:
350 bfa_sm_fault(bfad, event);
351 }
352}
353
354static void
355bfad_sm_fcs_exit(struct bfad_s *bfad, enum bfad_sm_event event)
356{
357 bfa_trc(bfad, event);
358
359 switch (event) {
360 case BFAD_E_FCS_EXIT_COMP:
361 bfa_sm_set_state(bfad, bfad_sm_stopping);
362 bfad_stop(bfad);
363 break;
364
365 default:
366 bfa_sm_fault(bfad, event);
367 }
368}
369
370static void
371bfad_sm_stopping(struct bfad_s *bfad, enum bfad_sm_event event)
372{
373 bfa_trc(bfad, event);
374
375 switch (event) {
376 case BFAD_E_EXIT_COMP:
377 bfa_sm_set_state(bfad, bfad_sm_uninit);
378 bfad_remove_intr(bfad);
379 del_timer_sync(&bfad->hal_tmo);
380 bfad_im_probe_undo(bfad);
381 bfad->bfad_flags &= ~BFAD_FC4_PROBE_DONE;
382 bfad_uncfg_pport(bfad);
383 break;
384
385 default:
386 bfa_sm_fault(bfad, event);
387 break;
388 }
Jing Huang7725ccf2009-09-23 17:46:15 -0700389}
390
Jing Huang5fbe25c2010-10-18 17:17:23 -0700391/*
Jing Huang7725ccf2009-09-23 17:46:15 -0700392 * BFA callbacks
393 */
394void
395bfad_hcb_comp(void *arg, bfa_status_t status)
396{
397 struct bfad_hal_comp *fcomp = (struct bfad_hal_comp *)arg;
398
399 fcomp->status = status;
400 complete(&fcomp->comp);
401}
402
Jing Huang5fbe25c2010-10-18 17:17:23 -0700403/*
Jing Huang7725ccf2009-09-23 17:46:15 -0700404 * bfa_init callback
405 */
406void
407bfa_cb_init(void *drv, bfa_status_t init_status)
408{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700409 struct bfad_s *bfad = drv;
Jing Huang7725ccf2009-09-23 17:46:15 -0700410
Krishna Gudipatie6714322010-03-03 17:44:02 -0800411 if (init_status == BFA_STATUS_OK) {
Jing Huang7725ccf2009-09-23 17:46:15 -0700412 bfad->bfad_flags |= BFAD_HAL_INIT_DONE;
413
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700414 /*
415 * If BFAD_HAL_INIT_FAIL flag is set:
Krishna Gudipatie6714322010-03-03 17:44:02 -0800416 * Wake up the kernel thread to start
417 * the bfad operations after HAL init done
418 */
419 if ((bfad->bfad_flags & BFAD_HAL_INIT_FAIL)) {
420 bfad->bfad_flags &= ~BFAD_HAL_INIT_FAIL;
421 wake_up_process(bfad->bfad_tsk);
422 }
423 }
424
Jing Huang7725ccf2009-09-23 17:46:15 -0700425 complete(&bfad->comp);
426}
427
Jing Huang5fbe25c2010-10-18 17:17:23 -0700428/*
Jing Huang7725ccf2009-09-23 17:46:15 -0700429 * BFA_FCS callbacks
430 */
Jing Huang7725ccf2009-09-23 17:46:15 -0700431struct bfad_port_s *
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700432bfa_fcb_lport_new(struct bfad_s *bfad, struct bfa_fcs_lport_s *port,
433 enum bfa_lport_role roles, struct bfad_vf_s *vf_drv,
Jing Huang7725ccf2009-09-23 17:46:15 -0700434 struct bfad_vport_s *vp_drv)
435{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700436 bfa_status_t rc;
437 struct bfad_port_s *port_drv;
Jing Huang7725ccf2009-09-23 17:46:15 -0700438
439 if (!vp_drv && !vf_drv) {
440 port_drv = &bfad->pport;
441 port_drv->pvb_type = BFAD_PORT_PHYS_BASE;
442 } else if (!vp_drv && vf_drv) {
443 port_drv = &vf_drv->base_port;
444 port_drv->pvb_type = BFAD_PORT_VF_BASE;
445 } else if (vp_drv && !vf_drv) {
446 port_drv = &vp_drv->drv_port;
447 port_drv->pvb_type = BFAD_PORT_PHYS_VPORT;
448 } else {
449 port_drv = &vp_drv->drv_port;
450 port_drv->pvb_type = BFAD_PORT_VF_VPORT;
451 }
452
453 port_drv->fcs_port = port;
454 port_drv->roles = roles;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700455
456 if (roles & BFA_LPORT_ROLE_FCP_IM) {
457 rc = bfad_im_port_new(bfad, port_drv);
458 if (rc != BFA_STATUS_OK) {
459 bfad_im_port_delete(bfad, port_drv);
460 port_drv = NULL;
461 }
Jing Huang7725ccf2009-09-23 17:46:15 -0700462 }
463
464 return port_drv;
465}
466
Jing Huang5fbe25c2010-10-18 17:17:23 -0700467/*
Jing Huang7725ccf2009-09-23 17:46:15 -0700468 * FCS RPORT alloc callback, after successful PLOGI by FCS
469 */
470bfa_status_t
471bfa_fcb_rport_alloc(struct bfad_s *bfad, struct bfa_fcs_rport_s **rport,
472 struct bfad_rport_s **rport_drv)
473{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700474 bfa_status_t rc = BFA_STATUS_OK;
Jing Huang7725ccf2009-09-23 17:46:15 -0700475
476 *rport_drv = kzalloc(sizeof(struct bfad_rport_s), GFP_ATOMIC);
477 if (*rport_drv == NULL) {
478 rc = BFA_STATUS_ENOMEM;
479 goto ext;
480 }
481
482 *rport = &(*rport_drv)->fcs_rport;
483
484ext:
485 return rc;
486}
487
Jing Huang5fbe25c2010-10-18 17:17:23 -0700488/*
Jing Huangd9883542010-07-08 19:46:26 -0700489 * FCS PBC VPORT Create
490 */
491void
492bfa_fcb_pbc_vport_create(struct bfad_s *bfad, struct bfi_pbc_vport_s pbc_vport)
493{
Jing Huang7725ccf2009-09-23 17:46:15 -0700494
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700495 struct bfa_lport_cfg_s port_cfg = {0};
496 struct bfad_vport_s *vport;
497 int rc;
Jing Huangd9883542010-07-08 19:46:26 -0700498
Alexey Khoroshilovdc6d2a02014-04-18 00:58:42 -0700499 vport = kzalloc(sizeof(struct bfad_vport_s), GFP_ATOMIC);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700500 if (!vport) {
Jing Huangd9883542010-07-08 19:46:26 -0700501 bfa_trc(bfad, 0);
502 return;
503 }
504
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700505 vport->drv_port.bfad = bfad;
506 port_cfg.roles = BFA_LPORT_ROLE_FCP_IM;
507 port_cfg.pwwn = pbc_vport.vp_pwwn;
508 port_cfg.nwwn = pbc_vport.vp_nwwn;
509 port_cfg.preboot_vp = BFA_TRUE;
Jing Huangd9883542010-07-08 19:46:26 -0700510
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700511 rc = bfa_fcs_pbc_vport_create(&vport->fcs_vport, &bfad->bfa_fcs, 0,
512 &port_cfg, vport);
Jing Huangd9883542010-07-08 19:46:26 -0700513
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700514 if (rc != BFA_STATUS_OK) {
515 bfa_trc(bfad, 0);
516 return;
517 }
518
519 list_add_tail(&vport->list_entry, &bfad->pbc_vport_list);
Jing Huangd9883542010-07-08 19:46:26 -0700520}
Jing Huang7725ccf2009-09-23 17:46:15 -0700521
522void
523bfad_hal_mem_release(struct bfad_s *bfad)
524{
Jing Huang7725ccf2009-09-23 17:46:15 -0700525 struct bfa_meminfo_s *hal_meminfo = &bfad->meminfo;
Krishna Gudipati45070252011-06-24 20:24:29 -0700526 struct bfa_mem_dma_s *dma_info, *dma_elem;
527 struct bfa_mem_kva_s *kva_info, *kva_elem;
528 struct list_head *dm_qe, *km_qe;
Jing Huang7725ccf2009-09-23 17:46:15 -0700529
Krishna Gudipati45070252011-06-24 20:24:29 -0700530 dma_info = &hal_meminfo->dma_info;
531 kva_info = &hal_meminfo->kva_info;
532
533 /* Iterate through the KVA meminfo queue */
534 list_for_each(km_qe, &kva_info->qe) {
535 kva_elem = (struct bfa_mem_kva_s *) km_qe;
536 vfree(kva_elem->kva);
537 }
538
539 /* Iterate through the DMA meminfo queue */
540 list_for_each(dm_qe, &dma_info->qe) {
541 dma_elem = (struct bfa_mem_dma_s *) dm_qe;
542 dma_free_coherent(&bfad->pcidev->dev,
543 dma_elem->mem_len, dma_elem->kva,
544 (dma_addr_t) dma_elem->dma);
Jing Huang7725ccf2009-09-23 17:46:15 -0700545 }
546
547 memset(hal_meminfo, 0, sizeof(struct bfa_meminfo_s));
548}
549
550void
551bfad_update_hal_cfg(struct bfa_iocfc_cfg_s *bfa_cfg)
552{
553 if (num_rports > 0)
554 bfa_cfg->fwcfg.num_rports = num_rports;
555 if (num_ios > 0)
556 bfa_cfg->fwcfg.num_ioim_reqs = num_ios;
557 if (num_tms > 0)
558 bfa_cfg->fwcfg.num_tskim_reqs = num_tms;
Krishna Gudipati45070252011-06-24 20:24:29 -0700559 if (num_fcxps > 0 && num_fcxps <= BFA_FCXP_MAX)
Jing Huang7725ccf2009-09-23 17:46:15 -0700560 bfa_cfg->fwcfg.num_fcxp_reqs = num_fcxps;
Krishna Gudipati45070252011-06-24 20:24:29 -0700561 if (num_ufbufs > 0 && num_ufbufs <= BFA_UF_MAX)
Jing Huang7725ccf2009-09-23 17:46:15 -0700562 bfa_cfg->fwcfg.num_uf_bufs = num_ufbufs;
563 if (reqq_size > 0)
564 bfa_cfg->drvcfg.num_reqq_elems = reqq_size;
565 if (rspq_size > 0)
566 bfa_cfg->drvcfg.num_rspq_elems = rspq_size;
Krishna Gudipati45070252011-06-24 20:24:29 -0700567 if (num_sgpgs > 0 && num_sgpgs <= BFA_SGPG_MAX)
Jing Huang7725ccf2009-09-23 17:46:15 -0700568 bfa_cfg->drvcfg.num_sgpgs = num_sgpgs;
569
570 /*
571 * populate the hal values back to the driver for sysfs use.
572 * otherwise, the default values will be shown as 0 in sysfs
573 */
574 num_rports = bfa_cfg->fwcfg.num_rports;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700575 num_ios = bfa_cfg->fwcfg.num_ioim_reqs;
576 num_tms = bfa_cfg->fwcfg.num_tskim_reqs;
577 num_fcxps = bfa_cfg->fwcfg.num_fcxp_reqs;
Jing Huang7725ccf2009-09-23 17:46:15 -0700578 num_ufbufs = bfa_cfg->fwcfg.num_uf_bufs;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700579 reqq_size = bfa_cfg->drvcfg.num_reqq_elems;
580 rspq_size = bfa_cfg->drvcfg.num_rspq_elems;
581 num_sgpgs = bfa_cfg->drvcfg.num_sgpgs;
Jing Huang7725ccf2009-09-23 17:46:15 -0700582}
583
584bfa_status_t
585bfad_hal_mem_alloc(struct bfad_s *bfad)
586{
587 struct bfa_meminfo_s *hal_meminfo = &bfad->meminfo;
Krishna Gudipati45070252011-06-24 20:24:29 -0700588 struct bfa_mem_dma_s *dma_info, *dma_elem;
589 struct bfa_mem_kva_s *kva_info, *kva_elem;
590 struct list_head *dm_qe, *km_qe;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700591 bfa_status_t rc = BFA_STATUS_OK;
Krishna Gudipati45070252011-06-24 20:24:29 -0700592 dma_addr_t phys_addr;
Jing Huang7725ccf2009-09-23 17:46:15 -0700593
594 bfa_cfg_get_default(&bfad->ioc_cfg);
Jing Huang7725ccf2009-09-23 17:46:15 -0700595 bfad_update_hal_cfg(&bfad->ioc_cfg);
596 bfad->cfg_data.ioc_queue_depth = bfad->ioc_cfg.fwcfg.num_ioim_reqs;
Krishna Gudipati45070252011-06-24 20:24:29 -0700597 bfa_cfg_get_meminfo(&bfad->ioc_cfg, hal_meminfo, &bfad->bfa);
Jing Huang7725ccf2009-09-23 17:46:15 -0700598
Krishna Gudipati45070252011-06-24 20:24:29 -0700599 dma_info = &hal_meminfo->dma_info;
600 kva_info = &hal_meminfo->kva_info;
Jing Huang7725ccf2009-09-23 17:46:15 -0700601
Krishna Gudipati45070252011-06-24 20:24:29 -0700602 /* Iterate through the KVA meminfo queue */
603 list_for_each(km_qe, &kva_info->qe) {
604 kva_elem = (struct bfa_mem_kva_s *) km_qe;
Himanshu Jhabde70f32017-12-30 20:58:31 +0530605 kva_elem->kva = vzalloc(kva_elem->mem_len);
Krishna Gudipati45070252011-06-24 20:24:29 -0700606 if (kva_elem->kva == NULL) {
607 bfad_hal_mem_release(bfad);
608 rc = BFA_STATUS_ENOMEM;
609 goto ext;
Jing Huang7725ccf2009-09-23 17:46:15 -0700610 }
Krishna Gudipati45070252011-06-24 20:24:29 -0700611 }
612
613 /* Iterate through the DMA meminfo queue */
614 list_for_each(dm_qe, &dma_info->qe) {
615 dma_elem = (struct bfa_mem_dma_s *) dm_qe;
616 dma_elem->kva = dma_alloc_coherent(&bfad->pcidev->dev,
617 dma_elem->mem_len,
618 &phys_addr, GFP_KERNEL);
619 if (dma_elem->kva == NULL) {
620 bfad_hal_mem_release(bfad);
621 rc = BFA_STATUS_ENOMEM;
622 goto ext;
623 }
624 dma_elem->dma = phys_addr;
625 memset(dma_elem->kva, 0, dma_elem->mem_len);
Jing Huang7725ccf2009-09-23 17:46:15 -0700626 }
627ext:
628 return rc;
629}
630
Jing Huang5fbe25c2010-10-18 17:17:23 -0700631/*
Jing Huang7725ccf2009-09-23 17:46:15 -0700632 * Create a vport under a vf.
633 */
634bfa_status_t
635bfad_vport_create(struct bfad_s *bfad, u16 vf_id,
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700636 struct bfa_lport_cfg_s *port_cfg, struct device *dev)
Jing Huang7725ccf2009-09-23 17:46:15 -0700637{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700638 struct bfad_vport_s *vport;
639 int rc = BFA_STATUS_OK;
640 unsigned long flags;
Jing Huang7725ccf2009-09-23 17:46:15 -0700641 struct completion fcomp;
642
643 vport = kzalloc(sizeof(struct bfad_vport_s), GFP_KERNEL);
644 if (!vport) {
645 rc = BFA_STATUS_ENOMEM;
646 goto ext;
647 }
648
649 vport->drv_port.bfad = bfad;
650 spin_lock_irqsave(&bfad->bfad_lock, flags);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700651 rc = bfa_fcs_vport_create(&vport->fcs_vport, &bfad->bfa_fcs, vf_id,
652 port_cfg, vport);
Jing Huang7725ccf2009-09-23 17:46:15 -0700653 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
654
655 if (rc != BFA_STATUS_OK)
656 goto ext_free_vport;
657
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700658 if (port_cfg->roles & BFA_LPORT_ROLE_FCP_IM) {
Jing Huangb5042932010-03-19 11:05:39 -0700659 rc = bfad_im_scsi_host_alloc(bfad, vport->drv_port.im_port,
660 dev);
Jing Huang7725ccf2009-09-23 17:46:15 -0700661 if (rc != BFA_STATUS_OK)
662 goto ext_free_fcs_vport;
663 }
664
665 spin_lock_irqsave(&bfad->bfad_lock, flags);
666 bfa_fcs_vport_start(&vport->fcs_vport);
Krishna Gudipati5b7db7a2011-12-20 18:58:32 -0800667 list_add_tail(&vport->list_entry, &bfad->vport_list);
Jing Huang7725ccf2009-09-23 17:46:15 -0700668 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
669
670 return BFA_STATUS_OK;
671
672ext_free_fcs_vport:
673 spin_lock_irqsave(&bfad->bfad_lock, flags);
674 vport->comp_del = &fcomp;
675 init_completion(vport->comp_del);
676 bfa_fcs_vport_delete(&vport->fcs_vport);
677 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
678 wait_for_completion(vport->comp_del);
679ext_free_vport:
680 kfree(vport);
681ext:
682 return rc;
683}
684
Jing Huang7725ccf2009-09-23 17:46:15 -0700685void
Kees Cooke99e88a2017-10-16 14:43:17 -0700686bfad_bfa_tmo(struct timer_list *t)
Jing Huang7725ccf2009-09-23 17:46:15 -0700687{
Kees Cooke99e88a2017-10-16 14:43:17 -0700688 struct bfad_s *bfad = from_timer(bfad, t, hal_tmo);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700689 unsigned long flags;
690 struct list_head doneq;
Jing Huang7725ccf2009-09-23 17:46:15 -0700691
692 spin_lock_irqsave(&bfad->bfad_lock, flags);
693
Maggie Zhangf7f738122010-12-09 19:08:43 -0800694 bfa_timer_beat(&bfad->bfa.timer_mod);
Jing Huang7725ccf2009-09-23 17:46:15 -0700695
696 bfa_comp_deq(&bfad->bfa, &doneq);
697 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
698
699 if (!list_empty(&doneq)) {
700 bfa_comp_process(&bfad->bfa, &doneq);
701 spin_lock_irqsave(&bfad->bfad_lock, flags);
702 bfa_comp_free(&bfad->bfa, &doneq);
703 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
704 }
705
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700706 mod_timer(&bfad->hal_tmo,
707 jiffies + msecs_to_jiffies(BFA_TIMER_FREQ));
Jing Huang7725ccf2009-09-23 17:46:15 -0700708}
709
710void
711bfad_init_timer(struct bfad_s *bfad)
712{
Kees Cooke99e88a2017-10-16 14:43:17 -0700713 timer_setup(&bfad->hal_tmo, bfad_bfa_tmo, 0);
Jing Huang7725ccf2009-09-23 17:46:15 -0700714
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700715 mod_timer(&bfad->hal_tmo,
716 jiffies + msecs_to_jiffies(BFA_TIMER_FREQ));
Jing Huang7725ccf2009-09-23 17:46:15 -0700717}
718
719int
720bfad_pci_init(struct pci_dev *pdev, struct bfad_s *bfad)
721{
Hannes Reinecke11ea3822019-02-18 08:34:23 +0100722 int rc = -ENODEV;
Jing Huang7725ccf2009-09-23 17:46:15 -0700723
724 if (pci_enable_device(pdev)) {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700725 printk(KERN_ERR "pci_enable_device fail %p\n", pdev);
Jing Huang7725ccf2009-09-23 17:46:15 -0700726 goto out;
727 }
728
729 if (pci_request_regions(pdev, BFAD_DRIVER_NAME))
730 goto out_disable_device;
731
732 pci_set_master(pdev);
733
Hannes Reinecke11ea3822019-02-18 08:34:23 +0100734 rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
735 if (rc)
736 rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
737
738 if (rc) {
739 rc = -ENODEV;
Christoph Hellwiga69b0802018-10-18 15:10:14 +0200740 printk(KERN_ERR "dma_set_mask_and_coherent fail %p\n", pdev);
741 goto out_release_region;
Krishna Gudipati10a07372011-06-24 20:23:38 -0700742 }
Jing Huang7725ccf2009-09-23 17:46:15 -0700743
Krishna Gudipati881c1b32012-08-22 19:52:02 -0700744 /* Enable PCIE Advanced Error Recovery (AER) if kernel supports */
745 pci_enable_pcie_error_reporting(pdev);
746
Jing Huangb3522f02010-03-19 11:06:44 -0700747 bfad->pci_bar0_kva = pci_iomap(pdev, 0, pci_resource_len(pdev, 0));
Krishna Gudipati11189202011-06-13 15:50:35 -0700748 bfad->pci_bar2_kva = pci_iomap(pdev, 2, pci_resource_len(pdev, 2));
Jing Huang7725ccf2009-09-23 17:46:15 -0700749
750 if (bfad->pci_bar0_kva == NULL) {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700751 printk(KERN_ERR "Fail to map bar0\n");
Jing Xiangfengf0f6c3a2020-09-25 14:24:23 +0800752 rc = -ENODEV;
Jing Huang7725ccf2009-09-23 17:46:15 -0700753 goto out_release_region;
754 }
755
756 bfad->hal_pcidev.pci_slot = PCI_SLOT(pdev->devfn);
757 bfad->hal_pcidev.pci_func = PCI_FUNC(pdev->devfn);
758 bfad->hal_pcidev.pci_bar_kva = bfad->pci_bar0_kva;
759 bfad->hal_pcidev.device_id = pdev->device;
Krishna Gudipati1a4d8e12011-06-24 20:22:28 -0700760 bfad->hal_pcidev.ssid = pdev->subsystem_device;
Jing Huang7725ccf2009-09-23 17:46:15 -0700761 bfad->pci_name = pci_name(pdev);
762
763 bfad->pci_attr.vendor_id = pdev->vendor;
764 bfad->pci_attr.device_id = pdev->device;
765 bfad->pci_attr.ssid = pdev->subsystem_device;
766 bfad->pci_attr.ssvid = pdev->subsystem_vendor;
767 bfad->pci_attr.pcifn = PCI_FUNC(pdev->devfn);
768
769 bfad->pcidev = pdev;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700770
771 /* Adjust PCIe Maximum Read Request Size */
Yijing Wangc0102c02013-09-05 15:55:25 +0800772 if (pci_is_pcie(pdev) && pcie_max_read_reqsz) {
773 if (pcie_max_read_reqsz >= 128 &&
774 pcie_max_read_reqsz <= 4096 &&
775 is_power_of_2(pcie_max_read_reqsz)) {
776 int max_rq = pcie_get_readrq(pdev);
777 printk(KERN_WARNING "BFA[%s]: "
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700778 "pcie_max_read_request_size is %d, "
Yijing Wangc0102c02013-09-05 15:55:25 +0800779 "reset to %d\n", bfad->pci_name, max_rq,
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700780 pcie_max_read_reqsz);
Yijing Wangc0102c02013-09-05 15:55:25 +0800781 pcie_set_readrq(pdev, pcie_max_read_reqsz);
782 } else {
783 printk(KERN_WARNING "BFA[%s]: invalid "
784 "pcie_max_read_request_size %d ignored\n",
785 bfad->pci_name, pcie_max_read_reqsz);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700786 }
787 }
788
Krishna Gudipati881c1b32012-08-22 19:52:02 -0700789 pci_save_state(pdev);
790
Jing Huang7725ccf2009-09-23 17:46:15 -0700791 return 0;
792
793out_release_region:
794 pci_release_regions(pdev);
795out_disable_device:
796 pci_disable_device(pdev);
797out:
798 return rc;
799}
800
801void
802bfad_pci_uninit(struct pci_dev *pdev, struct bfad_s *bfad)
803{
Jing Huang7725ccf2009-09-23 17:46:15 -0700804 pci_iounmap(pdev, bfad->pci_bar0_kva);
Krishna Gudipati11189202011-06-13 15:50:35 -0700805 pci_iounmap(pdev, bfad->pci_bar2_kva);
Jing Huang7725ccf2009-09-23 17:46:15 -0700806 pci_release_regions(pdev);
Krishna Gudipati881c1b32012-08-22 19:52:02 -0700807 /* Disable PCIE Advanced Error Recovery (AER) */
808 pci_disable_pcie_error_reporting(pdev);
Jing Huang7725ccf2009-09-23 17:46:15 -0700809 pci_disable_device(pdev);
Jing Huang7725ccf2009-09-23 17:46:15 -0700810}
811
Jing Huang7725ccf2009-09-23 17:46:15 -0700812bfa_status_t
813bfad_drv_init(struct bfad_s *bfad)
814{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700815 bfa_status_t rc;
816 unsigned long flags;
Jing Huang7725ccf2009-09-23 17:46:15 -0700817
818 bfad->cfg_data.rport_del_timeout = rport_del_timeout;
819 bfad->cfg_data.lun_queue_depth = bfa_lun_queue_depth;
820 bfad->cfg_data.io_max_sge = bfa_io_max_sge;
821 bfad->cfg_data.binding_method = FCP_PWWN_BINDING;
822
823 rc = bfad_hal_mem_alloc(bfad);
824 if (rc != BFA_STATUS_OK) {
825 printk(KERN_WARNING "bfad%d bfad_hal_mem_alloc failure\n",
826 bfad->inst_no);
827 printk(KERN_WARNING
Anil Gurumurthy31e1d562015-11-26 03:54:46 -0500828 "Not enough memory to attach all QLogic BR-series HBA ports. System may need more memory.\n");
Vijaya Mohan Guvvada3e0be2013-11-21 01:37:33 -0800829 return BFA_STATUS_FAILED;
Jing Huang7725ccf2009-09-23 17:46:15 -0700830 }
831
Maggie Zhangf7f738122010-12-09 19:08:43 -0800832 bfad->bfa.trcmod = bfad->trcmod;
833 bfad->bfa.plog = &bfad->plog_buf;
Jing Huang7725ccf2009-09-23 17:46:15 -0700834 bfa_plog_init(&bfad->plog_buf);
835 bfa_plog_str(&bfad->plog_buf, BFA_PL_MID_DRVR, BFA_PL_EID_DRIVER_START,
836 0, "Driver Attach");
837
838 bfa_attach(&bfad->bfa, bfad, &bfad->ioc_cfg, &bfad->meminfo,
839 &bfad->hal_pcidev);
840
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700841 /* FCS INIT */
Jing Huang7725ccf2009-09-23 17:46:15 -0700842 spin_lock_irqsave(&bfad->bfad_lock, flags);
Maggie Zhangf7f738122010-12-09 19:08:43 -0800843 bfad->bfa_fcs.trcmod = bfad->trcmod;
Krishna Gudipati82794a22010-03-03 17:43:30 -0800844 bfa_fcs_attach(&bfad->bfa_fcs, &bfad->bfa, bfad, BFA_FALSE);
Maggie Zhangf7f738122010-12-09 19:08:43 -0800845 bfad->bfa_fcs.fdmi_enabled = fdmi_enable;
Jing Huang7725ccf2009-09-23 17:46:15 -0700846 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
847
848 bfad->bfad_flags |= BFAD_DRV_INIT_DONE;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700849
Jing Huang7725ccf2009-09-23 17:46:15 -0700850 return BFA_STATUS_OK;
Jing Huang7725ccf2009-09-23 17:46:15 -0700851}
852
853void
854bfad_drv_uninit(struct bfad_s *bfad)
855{
Krishna Gudipatie6714322010-03-03 17:44:02 -0800856 unsigned long flags;
857
858 spin_lock_irqsave(&bfad->bfad_lock, flags);
859 init_completion(&bfad->comp);
Maggie Zhangf7f738122010-12-09 19:08:43 -0800860 bfa_iocfc_stop(&bfad->bfa);
Krishna Gudipatie6714322010-03-03 17:44:02 -0800861 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
862 wait_for_completion(&bfad->comp);
863
Jing Huang7725ccf2009-09-23 17:46:15 -0700864 del_timer_sync(&bfad->hal_tmo);
865 bfa_isr_disable(&bfad->bfa);
866 bfa_detach(&bfad->bfa);
867 bfad_remove_intr(bfad);
Jing Huang7725ccf2009-09-23 17:46:15 -0700868 bfad_hal_mem_release(bfad);
Krishna Gudipatie6714322010-03-03 17:44:02 -0800869
870 bfad->bfad_flags &= ~BFAD_DRV_INIT_DONE;
Jing Huang7725ccf2009-09-23 17:46:15 -0700871}
872
873void
874bfad_drv_start(struct bfad_s *bfad)
875{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700876 unsigned long flags;
Jing Huang7725ccf2009-09-23 17:46:15 -0700877
878 spin_lock_irqsave(&bfad->bfad_lock, flags);
Maggie Zhangf7f738122010-12-09 19:08:43 -0800879 bfa_iocfc_start(&bfad->bfa);
Krishna Gudipati75332a72011-06-13 15:54:31 -0700880 bfa_fcs_pbc_vport_init(&bfad->bfa_fcs);
Maggie Zhangf7f738122010-12-09 19:08:43 -0800881 bfa_fcs_fabric_modstart(&bfad->bfa_fcs);
Jing Huang7725ccf2009-09-23 17:46:15 -0700882 bfad->bfad_flags |= BFAD_HAL_START_DONE;
883 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
884
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700885 if (bfad->im)
886 flush_workqueue(bfad->im->drv_workq);
Jing Huang7725ccf2009-09-23 17:46:15 -0700887}
888
889void
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700890bfad_fcs_stop(struct bfad_s *bfad)
Jing Huang7725ccf2009-09-23 17:46:15 -0700891{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700892 unsigned long flags;
Jing Huang7725ccf2009-09-23 17:46:15 -0700893
894 spin_lock_irqsave(&bfad->bfad_lock, flags);
895 init_completion(&bfad->comp);
896 bfad->pport.flags |= BFAD_PORT_DELETE;
897 bfa_fcs_exit(&bfad->bfa_fcs);
898 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
899 wait_for_completion(&bfad->comp);
900
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700901 bfa_sm_send_event(bfad, BFAD_E_FCS_EXIT_COMP);
902}
903
904void
905bfad_stop(struct bfad_s *bfad)
906{
907 unsigned long flags;
908
Jing Huang7725ccf2009-09-23 17:46:15 -0700909 spin_lock_irqsave(&bfad->bfad_lock, flags);
910 init_completion(&bfad->comp);
Maggie Zhangf7f738122010-12-09 19:08:43 -0800911 bfa_iocfc_stop(&bfad->bfa);
Jing Huang7725ccf2009-09-23 17:46:15 -0700912 bfad->bfad_flags &= ~BFAD_HAL_START_DONE;
913 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
914 wait_for_completion(&bfad->comp);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700915
916 bfa_sm_send_event(bfad, BFAD_E_EXIT_COMP);
Jing Huang7725ccf2009-09-23 17:46:15 -0700917}
918
919bfa_status_t
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700920bfad_cfg_pport(struct bfad_s *bfad, enum bfa_lport_role role)
Jing Huang7725ccf2009-09-23 17:46:15 -0700921{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700922 int rc = BFA_STATUS_OK;
Jing Huang7725ccf2009-09-23 17:46:15 -0700923
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700924 /* Allocate scsi_host for the physical port */
925 if ((supported_fc4s & BFA_LPORT_ROLE_FCP_IM) &&
926 (role & BFA_LPORT_ROLE_FCP_IM)) {
Jing Huang7725ccf2009-09-23 17:46:15 -0700927 if (bfad->pport.im_port == NULL) {
928 rc = BFA_STATUS_FAILED;
929 goto out;
930 }
931
Jing Huangb5042932010-03-19 11:05:39 -0700932 rc = bfad_im_scsi_host_alloc(bfad, bfad->pport.im_port,
933 &bfad->pcidev->dev);
Jing Huang7725ccf2009-09-23 17:46:15 -0700934 if (rc != BFA_STATUS_OK)
935 goto out;
936
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700937 bfad->pport.roles |= BFA_LPORT_ROLE_FCP_IM;
Jing Huang7725ccf2009-09-23 17:46:15 -0700938 }
939
940 bfad->bfad_flags |= BFAD_CFG_PPORT_DONE;
941
942out:
943 return rc;
944}
945
946void
947bfad_uncfg_pport(struct bfad_s *bfad)
948{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700949 if ((supported_fc4s & BFA_LPORT_ROLE_FCP_IM) &&
950 (bfad->pport.roles & BFA_LPORT_ROLE_FCP_IM)) {
Jing Huang7725ccf2009-09-23 17:46:15 -0700951 bfad_im_scsi_host_free(bfad, bfad->pport.im_port);
952 bfad_im_port_clean(bfad->pport.im_port);
953 kfree(bfad->pport.im_port);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700954 bfad->pport.roles &= ~BFA_LPORT_ROLE_FCP_IM;
Jing Huang7725ccf2009-09-23 17:46:15 -0700955 }
956
957 bfad->bfad_flags &= ~BFAD_CFG_PPORT_DONE;
958}
959
Krishna Gudipatie6714322010-03-03 17:44:02 -0800960bfa_status_t
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700961bfad_start_ops(struct bfad_s *bfad) {
962
963 int retval;
964 unsigned long flags;
965 struct bfad_vport_s *vport, *vport_new;
966 struct bfa_fcs_driver_info_s driver_info;
967
Krishna Gudipati61e62e22011-06-24 20:29:07 -0700968 /* Limit min/max. xfer size to [64k-32MB] */
969 if (max_xfer_size < BFAD_MIN_SECTORS >> 1)
970 max_xfer_size = BFAD_MIN_SECTORS >> 1;
971 if (max_xfer_size > BFAD_MAX_SECTORS >> 1)
972 max_xfer_size = BFAD_MAX_SECTORS >> 1;
973
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700974 /* Fill the driver_info info to fcs*/
975 memset(&driver_info, 0, sizeof(driver_info));
Arnd Bergmann8c5a50e2017-12-04 15:47:00 +0100976 strlcpy(driver_info.version, BFAD_DRIVER_VERSION,
977 sizeof(driver_info.version));
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700978 if (host_name)
Arnd Bergmann8c5a50e2017-12-04 15:47:00 +0100979 strlcpy(driver_info.host_machine_name, host_name,
980 sizeof(driver_info.host_machine_name));
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700981 if (os_name)
Arnd Bergmann8c5a50e2017-12-04 15:47:00 +0100982 strlcpy(driver_info.host_os_name, os_name,
983 sizeof(driver_info.host_os_name));
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700984 if (os_patch)
Arnd Bergmann8c5a50e2017-12-04 15:47:00 +0100985 strlcpy(driver_info.host_os_patch, os_patch,
986 sizeof(driver_info.host_os_patch));
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700987
Arnd Bergmann8c5a50e2017-12-04 15:47:00 +0100988 strlcpy(driver_info.os_device_name, bfad->pci_name,
989 sizeof(driver_info.os_device_name));
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700990
Krishna Gudipati75332a72011-06-13 15:54:31 -0700991 /* FCS driver info init */
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700992 spin_lock_irqsave(&bfad->bfad_lock, flags);
993 bfa_fcs_driver_info_init(&bfad->bfa_fcs, &driver_info);
Vijaya Mohan Guvvada3e0be2013-11-21 01:37:33 -0800994
995 if (bfad->bfad_flags & BFAD_CFG_PPORT_DONE)
996 bfa_fcs_update_cfg(&bfad->bfa_fcs);
997 else
998 bfa_fcs_init(&bfad->bfa_fcs);
999
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001000 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
Krishna Gudipatie6714322010-03-03 17:44:02 -08001001
Vijaya Mohan Guvvada3e0be2013-11-21 01:37:33 -08001002 if (!(bfad->bfad_flags & BFAD_CFG_PPORT_DONE)) {
1003 retval = bfad_cfg_pport(bfad, BFA_LPORT_ROLE_FCP_IM);
1004 if (retval != BFA_STATUS_OK)
1005 return BFA_STATUS_FAILED;
1006 }
Krishna Gudipati75332a72011-06-13 15:54:31 -07001007
1008 /* Setup fc host fixed attribute if the lk supports */
1009 bfad_fc_host_init(bfad->pport.im_port);
Krishna Gudipatie6714322010-03-03 17:44:02 -08001010
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001011 /* BFAD level FC4 IM specific resource allocation */
1012 retval = bfad_im_probe(bfad);
1013 if (retval != BFA_STATUS_OK) {
1014 printk(KERN_WARNING "bfad_im_probe failed\n");
1015 if (bfa_sm_cmp_state(bfad, bfad_sm_initializing))
1016 bfa_sm_set_state(bfad, bfad_sm_failed);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001017 return BFA_STATUS_FAILED;
1018 } else
1019 bfad->bfad_flags |= BFAD_FC4_PROBE_DONE;
1020
Krishna Gudipatie6714322010-03-03 17:44:02 -08001021 bfad_drv_start(bfad);
1022
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001023 /* Complete pbc vport create */
1024 list_for_each_entry_safe(vport, vport_new, &bfad->pbc_vport_list,
1025 list_entry) {
Jing Huangd9883542010-07-08 19:46:26 -07001026 struct fc_vport_identifiers vid;
1027 struct fc_vport *fc_vport;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001028 char pwwn_buf[BFA_STRING_32];
Jing Huangd9883542010-07-08 19:46:26 -07001029
1030 memset(&vid, 0, sizeof(vid));
1031 vid.roles = FC_PORT_ROLE_FCP_INITIATOR;
1032 vid.vport_type = FC_PORTTYPE_NPIV;
1033 vid.disable = false;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001034 vid.node_name = wwn_to_u64((u8 *)
1035 (&((vport->fcs_vport).lport.port_cfg.nwwn)));
1036 vid.port_name = wwn_to_u64((u8 *)
1037 (&((vport->fcs_vport).lport.port_cfg.pwwn)));
Jing Huangd9883542010-07-08 19:46:26 -07001038 fc_vport = fc_vport_create(bfad->pport.im_port->shost, 0, &vid);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001039 if (!fc_vport) {
1040 wwn2str(pwwn_buf, vid.port_name);
Jing Huangd9883542010-07-08 19:46:26 -07001041 printk(KERN_WARNING "bfad%d: failed to create pbc vport"
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001042 " %s\n", bfad->inst_no, pwwn_buf);
1043 }
1044 list_del(&vport->list_entry);
1045 kfree(vport);
Jing Huangd9883542010-07-08 19:46:26 -07001046 }
1047
Krishna Gudipatie6714322010-03-03 17:44:02 -08001048 /*
1049 * If bfa_linkup_delay is set to -1 default; try to retrive the
Maggie Zhangf16a1752010-12-09 19:12:32 -08001050 * value using the bfad_get_linkup_delay(); else use the
Krishna Gudipatie6714322010-03-03 17:44:02 -08001051 * passed in module param value as the bfa_linkup_delay.
1052 */
1053 if (bfa_linkup_delay < 0) {
Maggie Zhangf16a1752010-12-09 19:12:32 -08001054 bfa_linkup_delay = bfad_get_linkup_delay(bfad);
1055 bfad_rport_online_wait(bfad);
Krishna Gudipatie6714322010-03-03 17:44:02 -08001056 bfa_linkup_delay = -1;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001057 } else
Maggie Zhangf16a1752010-12-09 19:12:32 -08001058 bfad_rport_online_wait(bfad);
Krishna Gudipatie6714322010-03-03 17:44:02 -08001059
Jing Huang88166242010-12-09 17:11:53 -08001060 BFA_LOG(KERN_INFO, bfad, bfa_log_level, "bfa device claimed\n");
Krishna Gudipatie6714322010-03-03 17:44:02 -08001061
1062 return BFA_STATUS_OK;
Krishna Gudipatie6714322010-03-03 17:44:02 -08001063}
1064
1065int
Jing Huangd9883542010-07-08 19:46:26 -07001066bfad_worker(void *ptr)
Krishna Gudipatie6714322010-03-03 17:44:02 -08001067{
Jiri Slaby8f7d3f02015-04-16 12:48:29 -07001068 struct bfad_s *bfad = ptr;
1069 unsigned long flags;
Krishna Gudipatie6714322010-03-03 17:44:02 -08001070
Jiri Slaby8f7d3f02015-04-16 12:48:29 -07001071 if (kthread_should_stop())
1072 return 0;
Krishna Gudipatie6714322010-03-03 17:44:02 -08001073
Jiri Slaby8f7d3f02015-04-16 12:48:29 -07001074 /* Send event BFAD_E_INIT_SUCCESS */
1075 bfa_sm_send_event(bfad, BFAD_E_INIT_SUCCESS);
Krishna Gudipatie6714322010-03-03 17:44:02 -08001076
Jiri Slaby8f7d3f02015-04-16 12:48:29 -07001077 spin_lock_irqsave(&bfad->bfad_lock, flags);
1078 bfad->bfad_tsk = NULL;
1079 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
Krishna Gudipatie6714322010-03-03 17:44:02 -08001080
1081 return 0;
1082}
1083
Jing Huang5fbe25c2010-10-18 17:17:23 -07001084/*
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001085 * BFA driver interrupt functions
1086 */
1087irqreturn_t
1088bfad_intx(int irq, void *dev_id)
1089{
1090 struct bfad_s *bfad = dev_id;
1091 struct list_head doneq;
1092 unsigned long flags;
1093 bfa_boolean_t rc;
1094
1095 spin_lock_irqsave(&bfad->bfad_lock, flags);
1096 rc = bfa_intx(&bfad->bfa);
1097 if (!rc) {
1098 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1099 return IRQ_NONE;
1100 }
1101
1102 bfa_comp_deq(&bfad->bfa, &doneq);
1103 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1104
1105 if (!list_empty(&doneq)) {
1106 bfa_comp_process(&bfad->bfa, &doneq);
1107
1108 spin_lock_irqsave(&bfad->bfad_lock, flags);
1109 bfa_comp_free(&bfad->bfa, &doneq);
1110 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001111 }
1112
1113 return IRQ_HANDLED;
1114
1115}
1116
1117static irqreturn_t
1118bfad_msix(int irq, void *dev_id)
1119{
1120 struct bfad_msix_s *vec = dev_id;
1121 struct bfad_s *bfad = vec->bfad;
1122 struct list_head doneq;
1123 unsigned long flags;
1124
1125 spin_lock_irqsave(&bfad->bfad_lock, flags);
1126
1127 bfa_msix(&bfad->bfa, vec->msix.entry);
1128 bfa_comp_deq(&bfad->bfa, &doneq);
1129 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1130
1131 if (!list_empty(&doneq)) {
1132 bfa_comp_process(&bfad->bfa, &doneq);
1133
1134 spin_lock_irqsave(&bfad->bfad_lock, flags);
1135 bfa_comp_free(&bfad->bfa, &doneq);
1136 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1137 }
1138
1139 return IRQ_HANDLED;
1140}
1141
Jing Huang5fbe25c2010-10-18 17:17:23 -07001142/*
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001143 * Initialize the MSIX entry table.
1144 */
1145static void
1146bfad_init_msix_entry(struct bfad_s *bfad, struct msix_entry *msix_entries,
1147 int mask, int max_bit)
1148{
1149 int i;
1150 int match = 0x00000001;
1151
1152 for (i = 0, bfad->nvec = 0; i < MAX_MSIX_ENTRY; i++) {
1153 if (mask & match) {
1154 bfad->msix_tab[bfad->nvec].msix.entry = i;
1155 bfad->msix_tab[bfad->nvec].bfad = bfad;
1156 msix_entries[bfad->nvec].entry = i;
1157 bfad->nvec++;
1158 }
1159
1160 match <<= 1;
1161 }
1162
1163}
1164
1165int
1166bfad_install_msix_handler(struct bfad_s *bfad)
1167{
1168 int i, error = 0;
1169
1170 for (i = 0; i < bfad->nvec; i++) {
1171 sprintf(bfad->msix_tab[i].name, "bfa-%s-%s",
1172 bfad->pci_name,
Krishna Gudipati11189202011-06-13 15:50:35 -07001173 ((bfa_asic_id_cb(bfad->hal_pcidev.device_id)) ?
1174 msix_name_cb[i] : msix_name_ct[i]));
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001175
1176 error = request_irq(bfad->msix_tab[i].msix.vector,
1177 (irq_handler_t) bfad_msix, 0,
1178 bfad->msix_tab[i].name, &bfad->msix_tab[i]);
1179 bfa_trc(bfad, i);
1180 bfa_trc(bfad, bfad->msix_tab[i].msix.vector);
1181 if (error) {
1182 int j;
1183
1184 for (j = 0; j < i; j++)
1185 free_irq(bfad->msix_tab[j].msix.vector,
1186 &bfad->msix_tab[j]);
1187
Krishna Gudipati61e62e22011-06-24 20:29:07 -07001188 bfad->bfad_flags &= ~BFAD_MSIX_ON;
1189 pci_disable_msix(bfad->pcidev);
1190
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001191 return 1;
1192 }
1193 }
1194
1195 return 0;
1196}
1197
Jing Huang5fbe25c2010-10-18 17:17:23 -07001198/*
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001199 * Setup MSIX based interrupt.
1200 */
1201int
1202bfad_setup_intr(struct bfad_s *bfad)
1203{
Alexander Gordeev8cb7f632014-07-16 20:05:07 +02001204 int error;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001205 u32 mask = 0, i, num_bit = 0, max_bit = 0;
1206 struct msix_entry msix_entries[MAX_MSIX_ENTRY];
1207 struct pci_dev *pdev = bfad->pcidev;
Krishna Gudipati10a07372011-06-24 20:23:38 -07001208 u16 reg;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001209
1210 /* Call BFA to get the msix map for this PCI function. */
1211 bfa_msix_getvecs(&bfad->bfa, &mask, &num_bit, &max_bit);
1212
1213 /* Set up the msix entry table */
1214 bfad_init_msix_entry(bfad, msix_entries, mask, max_bit);
1215
Krishna Gudipati11189202011-06-13 15:50:35 -07001216 if ((bfa_asic_id_ctc(pdev->device) && !msix_disable_ct) ||
1217 (bfa_asic_id_cb(pdev->device) && !msix_disable_cb)) {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001218
Alexander Gordeevb427d002014-07-16 20:05:08 +02001219 error = pci_enable_msix_exact(bfad->pcidev,
1220 msix_entries, bfad->nvec);
Alexander Gordeevcd69a092014-07-16 20:05:06 +02001221 /* In CT1 & CT2, try to allocate just one vector */
Alexander Gordeevb427d002014-07-16 20:05:08 +02001222 if (error == -ENOSPC && bfa_asic_id_ctc(pdev->device)) {
Alexander Gordeevcd69a092014-07-16 20:05:06 +02001223 printk(KERN_WARNING "bfa %s: trying one msix "
1224 "vector failed to allocate %d[%d]\n",
1225 bfad->pci_name, bfad->nvec, error);
1226 bfad->nvec = 1;
Alexander Gordeevb427d002014-07-16 20:05:08 +02001227 error = pci_enable_msix_exact(bfad->pcidev,
1228 msix_entries, 1);
Alexander Gordeevcd69a092014-07-16 20:05:06 +02001229 }
Krishna Gudipati881c1b32012-08-22 19:52:02 -07001230
Alexander Gordeevcd69a092014-07-16 20:05:06 +02001231 if (error) {
1232 printk(KERN_WARNING "bfad%d: "
Alexander Gordeevb427d002014-07-16 20:05:08 +02001233 "pci_enable_msix_exact failed (%d), "
Alexander Gordeevcd69a092014-07-16 20:05:06 +02001234 "use line based.\n",
1235 bfad->inst_no, error);
1236 goto line_based;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001237 }
1238
Krishna Gudipati10a07372011-06-24 20:23:38 -07001239 /* Disable INTX in MSI-X mode */
1240 pci_read_config_word(pdev, PCI_COMMAND, &reg);
1241
1242 if (!(reg & PCI_COMMAND_INTX_DISABLE))
1243 pci_write_config_word(pdev, PCI_COMMAND,
1244 reg | PCI_COMMAND_INTX_DISABLE);
1245
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001246 /* Save the vectors */
1247 for (i = 0; i < bfad->nvec; i++) {
1248 bfa_trc(bfad, msix_entries[i].vector);
1249 bfad->msix_tab[i].msix.vector = msix_entries[i].vector;
1250 }
1251
1252 bfa_msix_init(&bfad->bfa, bfad->nvec);
1253
1254 bfad->bfad_flags |= BFAD_MSIX_ON;
1255
Alexander Gordeev8cb7f632014-07-16 20:05:07 +02001256 return 0;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001257 }
1258
1259line_based:
Alexander Gordeev8cb7f632014-07-16 20:05:07 +02001260 error = request_irq(bfad->pcidev->irq, (irq_handler_t)bfad_intx,
1261 BFAD_IRQ_FLAGS, BFAD_DRIVER_NAME, bfad);
1262 if (error)
1263 return error;
1264
Krishna Gudipati61e62e22011-06-24 20:29:07 -07001265 bfad->bfad_flags |= BFAD_INTX_ON;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001266
Alexander Gordeev8cb7f632014-07-16 20:05:07 +02001267 return 0;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001268}
1269
1270void
1271bfad_remove_intr(struct bfad_s *bfad)
1272{
1273 int i;
1274
1275 if (bfad->bfad_flags & BFAD_MSIX_ON) {
1276 for (i = 0; i < bfad->nvec; i++)
1277 free_irq(bfad->msix_tab[i].msix.vector,
1278 &bfad->msix_tab[i]);
1279
1280 pci_disable_msix(bfad->pcidev);
1281 bfad->bfad_flags &= ~BFAD_MSIX_ON;
Krishna Gudipati61e62e22011-06-24 20:29:07 -07001282 } else if (bfad->bfad_flags & BFAD_INTX_ON) {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001283 free_irq(bfad->pcidev->irq, bfad);
1284 }
1285}
Jing Huang7725ccf2009-09-23 17:46:15 -07001286
Jing Huang5fbe25c2010-10-18 17:17:23 -07001287/*
Jing Huang7725ccf2009-09-23 17:46:15 -07001288 * PCI probe entry.
1289 */
1290int
1291bfad_pci_probe(struct pci_dev *pdev, const struct pci_device_id *pid)
1292{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001293 struct bfad_s *bfad;
Krishna Gudipati7826f302011-07-20 16:59:13 -07001294 int error = -ENODEV, retval, i;
Jing Huang7725ccf2009-09-23 17:46:15 -07001295
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001296 /* For single port cards - only claim function 0 */
Krishna Gudipati8b070b42011-06-13 15:52:40 -07001297 if ((pdev->device == BFA_PCI_DEVICE_ID_FC_8G1P) &&
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001298 (PCI_FUNC(pdev->devfn) != 0))
Jing Huang7725ccf2009-09-23 17:46:15 -07001299 return -ENODEV;
1300
Jing Huang7725ccf2009-09-23 17:46:15 -07001301 bfad = kzalloc(sizeof(struct bfad_s), GFP_KERNEL);
1302 if (!bfad) {
1303 error = -ENOMEM;
1304 goto out;
1305 }
1306
1307 bfad->trcmod = kzalloc(sizeof(struct bfa_trc_mod_s), GFP_KERNEL);
1308 if (!bfad->trcmod) {
1309 printk(KERN_WARNING "Error alloc trace buffer!\n");
1310 error = -ENOMEM;
1311 goto out_alloc_trace_failure;
1312 }
1313
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001314 /* TRACE INIT */
Jing Huang7725ccf2009-09-23 17:46:15 -07001315 bfa_trc_init(bfad->trcmod);
1316 bfa_trc(bfad, bfad_inst);
1317
Krishna Gudipati7826f302011-07-20 16:59:13 -07001318 /* AEN INIT */
1319 INIT_LIST_HEAD(&bfad->free_aen_q);
1320 INIT_LIST_HEAD(&bfad->active_aen_q);
1321 for (i = 0; i < BFA_AEN_MAX_ENTRY; i++)
1322 list_add_tail(&bfad->aen_list[i].qe, &bfad->free_aen_q);
1323
Jing Huang7725ccf2009-09-23 17:46:15 -07001324 if (!(bfad_load_fwimg(pdev))) {
Jing Huang7725ccf2009-09-23 17:46:15 -07001325 kfree(bfad->trcmod);
1326 goto out_alloc_trace_failure;
1327 }
1328
1329 retval = bfad_pci_init(pdev, bfad);
1330 if (retval) {
1331 printk(KERN_WARNING "bfad_pci_init failure!\n");
1332 error = retval;
1333 goto out_pci_init_failure;
1334 }
1335
1336 mutex_lock(&bfad_mutex);
1337 bfad->inst_no = bfad_inst++;
1338 list_add_tail(&bfad->list_entry, &bfad_list);
1339 mutex_unlock(&bfad_mutex);
1340
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001341 /* Initializing the state machine: State set to uninit */
1342 bfa_sm_set_state(bfad, bfad_sm_uninit);
1343
Jing Huang7725ccf2009-09-23 17:46:15 -07001344 spin_lock_init(&bfad->bfad_lock);
Kyle McMartin0b1017a2012-07-11 12:42:55 -04001345 spin_lock_init(&bfad->bfad_aen_spinlock);
1346
Jing Huang7725ccf2009-09-23 17:46:15 -07001347 pci_set_drvdata(pdev, bfad);
1348
1349 bfad->ref_count = 0;
1350 bfad->pport.bfad = bfad;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001351 INIT_LIST_HEAD(&bfad->pbc_vport_list);
Krishna Gudipati5b7db7a2011-12-20 18:58:32 -08001352 INIT_LIST_HEAD(&bfad->vport_list);
Krishna Gudipatie6714322010-03-03 17:44:02 -08001353
Krishna Gudipati7c38c052011-04-14 16:50:35 -07001354 /* Setup the debugfs node for this bfad */
1355 if (bfa_debugfs_enable)
1356 bfad_debugfs_init(&bfad->pport);
1357
Jing Huang7725ccf2009-09-23 17:46:15 -07001358 retval = bfad_drv_init(bfad);
1359 if (retval != BFA_STATUS_OK)
1360 goto out_drv_init_failure;
Jing Huang7725ccf2009-09-23 17:46:15 -07001361
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001362 bfa_sm_send_event(bfad, BFAD_E_CREATE);
Jing Huang7725ccf2009-09-23 17:46:15 -07001363
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001364 if (bfa_sm_cmp_state(bfad, bfad_sm_uninit))
1365 goto out_bfad_sm_failure;
Jing Huang7725ccf2009-09-23 17:46:15 -07001366
Jing Huang7725ccf2009-09-23 17:46:15 -07001367 return 0;
1368
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001369out_bfad_sm_failure:
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001370 bfad_hal_mem_release(bfad);
Jing Huang7725ccf2009-09-23 17:46:15 -07001371out_drv_init_failure:
Krishna Gudipati7c38c052011-04-14 16:50:35 -07001372 /* Remove the debugfs node for this bfad */
1373 kfree(bfad->regdata);
1374 bfad_debugfs_exit(&bfad->pport);
Jing Huang7725ccf2009-09-23 17:46:15 -07001375 mutex_lock(&bfad_mutex);
1376 bfad_inst--;
1377 list_del(&bfad->list_entry);
1378 mutex_unlock(&bfad_mutex);
1379 bfad_pci_uninit(pdev, bfad);
1380out_pci_init_failure:
1381 kfree(bfad->trcmod);
1382out_alloc_trace_failure:
1383 kfree(bfad);
1384out:
1385 return error;
1386}
1387
Jing Huang5fbe25c2010-10-18 17:17:23 -07001388/*
Jing Huang7725ccf2009-09-23 17:46:15 -07001389 * PCI remove entry.
1390 */
1391void
1392bfad_pci_remove(struct pci_dev *pdev)
1393{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001394 struct bfad_s *bfad = pci_get_drvdata(pdev);
1395 unsigned long flags;
Jing Huang7725ccf2009-09-23 17:46:15 -07001396
1397 bfa_trc(bfad, bfad->inst_no);
1398
Krishna Gudipatie6714322010-03-03 17:44:02 -08001399 spin_lock_irqsave(&bfad->bfad_lock, flags);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001400 if (bfad->bfad_tsk != NULL) {
1401 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
Krishna Gudipatie6714322010-03-03 17:44:02 -08001402 kthread_stop(bfad->bfad_tsk);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001403 } else {
Jing Huang7725ccf2009-09-23 17:46:15 -07001404 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
Jing Huang7725ccf2009-09-23 17:46:15 -07001405 }
1406
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001407 /* Send Event BFAD_E_STOP */
1408 bfa_sm_send_event(bfad, BFAD_E_STOP);
Jing Huang7725ccf2009-09-23 17:46:15 -07001409
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001410 /* Driver detach and dealloc mem */
Jing Huang7725ccf2009-09-23 17:46:15 -07001411 spin_lock_irqsave(&bfad->bfad_lock, flags);
1412 bfa_detach(&bfad->bfa);
1413 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1414 bfad_hal_mem_release(bfad);
Jing Huang7725ccf2009-09-23 17:46:15 -07001415
Krishna Gudipati7c38c052011-04-14 16:50:35 -07001416 /* Remove the debugfs node for this bfad */
1417 kfree(bfad->regdata);
1418 bfad_debugfs_exit(&bfad->pport);
1419
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001420 /* Cleaning the BFAD instance */
Jing Huang7725ccf2009-09-23 17:46:15 -07001421 mutex_lock(&bfad_mutex);
1422 bfad_inst--;
1423 list_del(&bfad->list_entry);
1424 mutex_unlock(&bfad_mutex);
1425 bfad_pci_uninit(pdev, bfad);
1426
1427 kfree(bfad->trcmod);
1428 kfree(bfad);
1429}
1430
Krishna Gudipati881c1b32012-08-22 19:52:02 -07001431/*
1432 * PCI Error Recovery entry, error detected.
1433 */
1434static pci_ers_result_t
1435bfad_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
1436{
1437 struct bfad_s *bfad = pci_get_drvdata(pdev);
1438 unsigned long flags;
1439 pci_ers_result_t ret = PCI_ERS_RESULT_NONE;
1440
1441 dev_printk(KERN_ERR, &pdev->dev,
1442 "error detected state: %d - flags: 0x%x\n",
1443 state, bfad->bfad_flags);
1444
1445 switch (state) {
1446 case pci_channel_io_normal: /* non-fatal error */
1447 spin_lock_irqsave(&bfad->bfad_lock, flags);
1448 bfad->bfad_flags &= ~BFAD_EEH_BUSY;
1449 /* Suspend/fail all bfa operations */
1450 bfa_ioc_suspend(&bfad->bfa.ioc);
1451 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1452 del_timer_sync(&bfad->hal_tmo);
1453 ret = PCI_ERS_RESULT_CAN_RECOVER;
1454 break;
1455 case pci_channel_io_frozen: /* fatal error */
1456 init_completion(&bfad->comp);
1457 spin_lock_irqsave(&bfad->bfad_lock, flags);
1458 bfad->bfad_flags |= BFAD_EEH_BUSY;
1459 /* Suspend/fail all bfa operations */
1460 bfa_ioc_suspend(&bfad->bfa.ioc);
1461 bfa_fcs_stop(&bfad->bfa_fcs);
1462 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1463 wait_for_completion(&bfad->comp);
1464
1465 bfad_remove_intr(bfad);
1466 del_timer_sync(&bfad->hal_tmo);
1467 pci_disable_device(pdev);
1468 ret = PCI_ERS_RESULT_NEED_RESET;
1469 break;
1470 case pci_channel_io_perm_failure: /* PCI Card is DEAD */
1471 spin_lock_irqsave(&bfad->bfad_lock, flags);
1472 bfad->bfad_flags |= BFAD_EEH_BUSY |
1473 BFAD_EEH_PCI_CHANNEL_IO_PERM_FAILURE;
1474 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1475
1476 /* If the error_detected handler is called with the reason
1477 * pci_channel_io_perm_failure - it will subsequently call
1478 * pci_remove() entry point to remove the pci device from the
1479 * system - So defer the cleanup to pci_remove(); cleaning up
1480 * here causes inconsistent state during pci_remove().
1481 */
1482 ret = PCI_ERS_RESULT_DISCONNECT;
1483 break;
1484 default:
1485 WARN_ON(1);
1486 }
1487
1488 return ret;
1489}
1490
YueHaibing7cd4cb92019-09-30 17:43:27 +08001491static int restart_bfa(struct bfad_s *bfad)
Krishna Gudipati881c1b32012-08-22 19:52:02 -07001492{
1493 unsigned long flags;
1494 struct pci_dev *pdev = bfad->pcidev;
1495
1496 bfa_attach(&bfad->bfa, bfad, &bfad->ioc_cfg,
1497 &bfad->meminfo, &bfad->hal_pcidev);
1498
1499 /* Enable Interrupt and wait bfa_init completion */
1500 if (bfad_setup_intr(bfad)) {
1501 dev_printk(KERN_WARNING, &pdev->dev,
1502 "%s: bfad_setup_intr failed\n", bfad->pci_name);
Vijaya Mohan Guvvada3e0be2013-11-21 01:37:33 -08001503 bfa_sm_send_event(bfad, BFAD_E_INIT_FAILED);
Krishna Gudipati881c1b32012-08-22 19:52:02 -07001504 return -1;
1505 }
1506
1507 init_completion(&bfad->comp);
1508 spin_lock_irqsave(&bfad->bfad_lock, flags);
1509 bfa_iocfc_init(&bfad->bfa);
1510 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1511
1512 /* Set up interrupt handler for each vectors */
1513 if ((bfad->bfad_flags & BFAD_MSIX_ON) &&
1514 bfad_install_msix_handler(bfad))
1515 dev_printk(KERN_WARNING, &pdev->dev,
1516 "%s: install_msix failed.\n", bfad->pci_name);
1517
1518 bfad_init_timer(bfad);
1519 wait_for_completion(&bfad->comp);
1520 bfad_drv_start(bfad);
1521
1522 return 0;
1523}
1524
1525/*
1526 * PCI Error Recovery entry, re-initialize the chip.
1527 */
1528static pci_ers_result_t
1529bfad_pci_slot_reset(struct pci_dev *pdev)
1530{
1531 struct bfad_s *bfad = pci_get_drvdata(pdev);
1532 u8 byte;
Hannes Reinecke11ea3822019-02-18 08:34:23 +01001533 int rc;
Krishna Gudipati881c1b32012-08-22 19:52:02 -07001534
1535 dev_printk(KERN_ERR, &pdev->dev,
1536 "bfad_pci_slot_reset flags: 0x%x\n", bfad->bfad_flags);
1537
1538 if (pci_enable_device(pdev)) {
1539 dev_printk(KERN_ERR, &pdev->dev, "Cannot re-enable "
1540 "PCI device after reset.\n");
1541 return PCI_ERS_RESULT_DISCONNECT;
1542 }
1543
1544 pci_restore_state(pdev);
1545
1546 /*
1547 * Read some byte (e.g. DMA max. payload size which can't
1548 * be 0xff any time) to make sure - we did not hit another PCI error
1549 * in the middle of recovery. If we did, then declare permanent failure.
1550 */
1551 pci_read_config_byte(pdev, 0x68, &byte);
1552 if (byte == 0xff) {
1553 dev_printk(KERN_ERR, &pdev->dev,
1554 "slot_reset failed ... got another PCI error !\n");
1555 goto out_disable_device;
1556 }
1557
1558 pci_save_state(pdev);
1559 pci_set_master(pdev);
1560
Hannes Reinecke11ea3822019-02-18 08:34:23 +01001561 rc = dma_set_mask_and_coherent(&bfad->pcidev->dev, DMA_BIT_MASK(64));
1562 if (rc)
1563 rc = dma_set_mask_and_coherent(&bfad->pcidev->dev,
1564 DMA_BIT_MASK(32));
1565 if (rc)
Christoph Hellwiga69b0802018-10-18 15:10:14 +02001566 goto out_disable_device;
Krishna Gudipati881c1b32012-08-22 19:52:02 -07001567
Krishna Gudipati881c1b32012-08-22 19:52:02 -07001568 if (restart_bfa(bfad) == -1)
1569 goto out_disable_device;
1570
1571 pci_enable_pcie_error_reporting(pdev);
1572 dev_printk(KERN_WARNING, &pdev->dev,
1573 "slot_reset completed flags: 0x%x!\n", bfad->bfad_flags);
1574
1575 return PCI_ERS_RESULT_RECOVERED;
1576
1577out_disable_device:
1578 pci_disable_device(pdev);
1579 return PCI_ERS_RESULT_DISCONNECT;
1580}
1581
1582static pci_ers_result_t
1583bfad_pci_mmio_enabled(struct pci_dev *pdev)
1584{
1585 unsigned long flags;
1586 struct bfad_s *bfad = pci_get_drvdata(pdev);
1587
1588 dev_printk(KERN_INFO, &pdev->dev, "mmio_enabled\n");
1589
1590 /* Fetch FW diagnostic information */
1591 bfa_ioc_debug_save_ftrc(&bfad->bfa.ioc);
1592
1593 /* Cancel all pending IOs */
1594 spin_lock_irqsave(&bfad->bfad_lock, flags);
1595 init_completion(&bfad->comp);
1596 bfa_fcs_stop(&bfad->bfa_fcs);
1597 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1598 wait_for_completion(&bfad->comp);
1599
1600 bfad_remove_intr(bfad);
1601 del_timer_sync(&bfad->hal_tmo);
1602 pci_disable_device(pdev);
1603
1604 return PCI_ERS_RESULT_NEED_RESET;
1605}
1606
1607static void
1608bfad_pci_resume(struct pci_dev *pdev)
1609{
1610 unsigned long flags;
1611 struct bfad_s *bfad = pci_get_drvdata(pdev);
1612
1613 dev_printk(KERN_WARNING, &pdev->dev, "resume\n");
1614
1615 /* wait until the link is online */
1616 bfad_rport_online_wait(bfad);
1617
1618 spin_lock_irqsave(&bfad->bfad_lock, flags);
1619 bfad->bfad_flags &= ~BFAD_EEH_BUSY;
1620 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1621}
1622
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001623struct pci_device_id bfad_id_table[] = {
Jing Huang7725ccf2009-09-23 17:46:15 -07001624 {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001625 .vendor = BFA_PCI_VENDOR_ID_BROCADE,
1626 .device = BFA_PCI_DEVICE_ID_FC_8G2P,
1627 .subvendor = PCI_ANY_ID,
1628 .subdevice = PCI_ANY_ID,
1629 },
Jing Huang7725ccf2009-09-23 17:46:15 -07001630 {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001631 .vendor = BFA_PCI_VENDOR_ID_BROCADE,
1632 .device = BFA_PCI_DEVICE_ID_FC_8G1P,
1633 .subvendor = PCI_ANY_ID,
1634 .subdevice = PCI_ANY_ID,
1635 },
Jing Huang7725ccf2009-09-23 17:46:15 -07001636 {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001637 .vendor = BFA_PCI_VENDOR_ID_BROCADE,
1638 .device = BFA_PCI_DEVICE_ID_CT,
1639 .subvendor = PCI_ANY_ID,
1640 .subdevice = PCI_ANY_ID,
1641 .class = (PCI_CLASS_SERIAL_FIBER << 8),
1642 .class_mask = ~0,
1643 },
Jing Huang293f82d2010-07-08 19:45:20 -07001644 {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001645 .vendor = BFA_PCI_VENDOR_ID_BROCADE,
1646 .device = BFA_PCI_DEVICE_ID_CT_FC,
1647 .subvendor = PCI_ANY_ID,
1648 .subdevice = PCI_ANY_ID,
1649 .class = (PCI_CLASS_SERIAL_FIBER << 8),
1650 .class_mask = ~0,
Jing Huang293f82d2010-07-08 19:45:20 -07001651 },
Krishna Gudipati11189202011-06-13 15:50:35 -07001652 {
1653 .vendor = BFA_PCI_VENDOR_ID_BROCADE,
1654 .device = BFA_PCI_DEVICE_ID_CT2,
1655 .subvendor = PCI_ANY_ID,
1656 .subdevice = PCI_ANY_ID,
1657 .class = (PCI_CLASS_SERIAL_FIBER << 8),
1658 .class_mask = ~0,
1659 },
Jing Huang7725ccf2009-09-23 17:46:15 -07001660
Vijaya Mohan Guvvaf9c867b2013-05-13 02:33:33 -07001661 {
1662 .vendor = BFA_PCI_VENDOR_ID_BROCADE,
1663 .device = BFA_PCI_DEVICE_ID_CT2_QUAD,
1664 .subvendor = PCI_ANY_ID,
1665 .subdevice = PCI_ANY_ID,
1666 .class = (PCI_CLASS_SERIAL_FIBER << 8),
1667 .class_mask = ~0,
1668 },
Jing Huang7725ccf2009-09-23 17:46:15 -07001669 {0, 0},
1670};
1671
1672MODULE_DEVICE_TABLE(pci, bfad_id_table);
1673
Krishna Gudipati881c1b32012-08-22 19:52:02 -07001674/*
1675 * PCI error recovery handlers.
1676 */
1677static struct pci_error_handlers bfad_err_handler = {
1678 .error_detected = bfad_pci_error_detected,
1679 .slot_reset = bfad_pci_slot_reset,
1680 .mmio_enabled = bfad_pci_mmio_enabled,
1681 .resume = bfad_pci_resume,
1682};
1683
Jing Huang7725ccf2009-09-23 17:46:15 -07001684static struct pci_driver bfad_pci_driver = {
1685 .name = BFAD_DRIVER_NAME,
1686 .id_table = bfad_id_table,
1687 .probe = bfad_pci_probe,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08001688 .remove = bfad_pci_remove,
Krishna Gudipati881c1b32012-08-22 19:52:02 -07001689 .err_handler = &bfad_err_handler,
Jing Huang7725ccf2009-09-23 17:46:15 -07001690};
1691
Jing Huang5fbe25c2010-10-18 17:17:23 -07001692/*
Jing Huang7725ccf2009-09-23 17:46:15 -07001693 * Driver module init.
1694 */
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001695static int __init
Jing Huang7725ccf2009-09-23 17:46:15 -07001696bfad_init(void)
1697{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001698 int error = 0;
Jing Huang7725ccf2009-09-23 17:46:15 -07001699
Anil Gurumurthy31e1d562015-11-26 03:54:46 -05001700 pr_info("QLogic BR-series BFA FC/FCOE SCSI driver - version: %s\n",
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001701 BFAD_DRIVER_VERSION);
Jing Huang7725ccf2009-09-23 17:46:15 -07001702
1703 if (num_sgpgs > 0)
1704 num_sgpgs_parm = num_sgpgs;
1705
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001706 error = bfad_im_module_init();
Jing Huang7725ccf2009-09-23 17:46:15 -07001707 if (error) {
1708 error = -ENOMEM;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001709 printk(KERN_WARNING "bfad_im_module_init failure\n");
Jing Huang7725ccf2009-09-23 17:46:15 -07001710 goto ext;
1711 }
1712
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001713 if (strcmp(FCPI_NAME, " fcpim") == 0)
1714 supported_fc4s |= BFA_LPORT_ROLE_FCP_IM;
Jing Huang7725ccf2009-09-23 17:46:15 -07001715
Maggie Zhangf7f738122010-12-09 19:08:43 -08001716 bfa_auto_recover = ioc_auto_recover;
Jing Huang7725ccf2009-09-23 17:46:15 -07001717 bfa_fcs_rport_set_del_timeout(rport_del_timeout);
Krishna Gudipati61ba4392012-08-22 19:52:58 -07001718 bfa_fcs_rport_set_max_logins(max_rport_logins);
Jing Huang7725ccf2009-09-23 17:46:15 -07001719
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001720 error = pci_register_driver(&bfad_pci_driver);
Jing Huang7725ccf2009-09-23 17:46:15 -07001721 if (error) {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001722 printk(KERN_WARNING "pci_register_driver failure\n");
Jing Huang7725ccf2009-09-23 17:46:15 -07001723 goto ext;
1724 }
1725
1726 return 0;
1727
1728ext:
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001729 bfad_im_module_exit();
Jing Huang7725ccf2009-09-23 17:46:15 -07001730 return error;
1731}
1732
Jing Huang5fbe25c2010-10-18 17:17:23 -07001733/*
Jing Huang7725ccf2009-09-23 17:46:15 -07001734 * Driver module exit.
1735 */
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001736static void __exit
Jing Huang7725ccf2009-09-23 17:46:15 -07001737bfad_exit(void)
1738{
1739 pci_unregister_driver(&bfad_pci_driver);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001740 bfad_im_module_exit();
Jing Huang7725ccf2009-09-23 17:46:15 -07001741 bfad_free_fwimg();
1742}
1743
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001744/* Firmware handling */
Jing Huang61338a02011-04-13 11:44:03 -07001745static void
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001746bfad_read_firmware(struct pci_dev *pdev, u32 **bfi_image,
1747 u32 *bfi_image_size, char *fw_name)
1748{
1749 const struct firmware *fw;
1750
1751 if (request_firmware(&fw, fw_name, &pdev->dev)) {
1752 printk(KERN_ALERT "Can't locate firmware %s\n", fw_name);
Jing Huang61338a02011-04-13 11:44:03 -07001753 *bfi_image = NULL;
1754 goto out;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001755 }
1756
1757 *bfi_image = vmalloc(fw->size);
1758 if (NULL == *bfi_image) {
1759 printk(KERN_ALERT "Fail to allocate buffer for fw image "
1760 "size=%x!\n", (u32) fw->size);
Jing Huang61338a02011-04-13 11:44:03 -07001761 goto out;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001762 }
1763
1764 memcpy(*bfi_image, fw->data, fw->size);
1765 *bfi_image_size = fw->size/sizeof(u32);
Jing Huang61338a02011-04-13 11:44:03 -07001766out:
1767 release_firmware(fw);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001768}
1769
Jing Huang61338a02011-04-13 11:44:03 -07001770static u32 *
1771bfad_load_fwimg(struct pci_dev *pdev)
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001772{
Vijaya Mohan Guvvadcaf9ae2013-12-04 05:43:58 -08001773 if (bfa_asic_id_ct2(pdev->device)) {
Krishna Gudipati11189202011-06-13 15:50:35 -07001774 if (bfi_image_ct2_size == 0)
1775 bfad_read_firmware(pdev, &bfi_image_ct2,
1776 &bfi_image_ct2_size, BFAD_FW_FILE_CT2);
1777 return bfi_image_ct2;
1778 } else if (bfa_asic_id_ct(pdev->device)) {
1779 if (bfi_image_ct_size == 0)
1780 bfad_read_firmware(pdev, &bfi_image_ct,
1781 &bfi_image_ct_size, BFAD_FW_FILE_CT);
1782 return bfi_image_ct;
Vijaya Mohan Guvvadcaf9ae2013-12-04 05:43:58 -08001783 } else if (bfa_asic_id_cb(pdev->device)) {
Krishna Gudipati11189202011-06-13 15:50:35 -07001784 if (bfi_image_cb_size == 0)
1785 bfad_read_firmware(pdev, &bfi_image_cb,
1786 &bfi_image_cb_size, BFAD_FW_FILE_CB);
1787 return bfi_image_cb;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001788 }
Vijaya Mohan Guvvadcaf9ae2013-12-04 05:43:58 -08001789
1790 return NULL;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001791}
Jing Huang7725ccf2009-09-23 17:46:15 -07001792
Jing Huang61338a02011-04-13 11:44:03 -07001793static void
1794bfad_free_fwimg(void)
1795{
Krishna Gudipati11189202011-06-13 15:50:35 -07001796 if (bfi_image_ct2_size && bfi_image_ct2)
1797 vfree(bfi_image_ct2);
1798 if (bfi_image_ct_size && bfi_image_ct)
1799 vfree(bfi_image_ct);
1800 if (bfi_image_cb_size && bfi_image_cb)
1801 vfree(bfi_image_cb);
Jing Huang61338a02011-04-13 11:44:03 -07001802}
1803
Jing Huang7725ccf2009-09-23 17:46:15 -07001804module_init(bfad_init);
1805module_exit(bfad_exit);
1806MODULE_LICENSE("GPL");
Anil Gurumurthy31e1d562015-11-26 03:54:46 -05001807MODULE_DESCRIPTION("QLogic BR-series Fibre Channel HBA Driver" BFAD_PROTO_NAME);
1808MODULE_AUTHOR("QLogic Corporation");
Jing Huang7725ccf2009-09-23 17:46:15 -07001809MODULE_VERSION(BFAD_DRIVER_VERSION);