blob: 9d253cb83ee762692c1ec0f56303f3c7fc8b416e [file] [log] [blame]
Jing Huang7725ccf2009-09-23 17:46:15 -07001/*
Anil Gurumurthy889d0d42015-11-26 03:54:45 -05002 * Copyright (c) 2005-2014 Brocade Communications Systems, Inc.
3 * Copyright (c) 2014- QLogic Corporation.
Jing Huang7725ccf2009-09-23 17:46:15 -07004 * All rights reserved
Anil Gurumurthy889d0d42015-11-26 03:54:45 -05005 * www.qlogic.com
Jing Huang7725ccf2009-09-23 17:46:15 -07006 *
Anil Gurumurthy31e1d562015-11-26 03:54:46 -05007 * Linux driver for QLogic BR-series Fibre Channel Host Bus Adapter.
Jing Huang7725ccf2009-09-23 17:46:15 -07008 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License (GPL) Version 2 as
11 * published by the Free Software Foundation
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 */
18
Jing Huang5fbe25c2010-10-18 17:17:23 -070019/*
Jing Huang7725ccf2009-09-23 17:46:15 -070020 * bfad.c Linux driver PCI interface module.
21 */
Jing Huang7725ccf2009-09-23 17:46:15 -070022#include <linux/module.h>
Krishna Gudipatie6714322010-03-03 17:44:02 -080023#include <linux/kthread.h>
Krishna Gudipatia36c61f2010-09-15 11:50:55 -070024#include <linux/errno.h>
25#include <linux/sched.h>
26#include <linux/init.h>
27#include <linux/fs.h>
28#include <linux/pci.h>
29#include <linux/firmware.h>
30#include <asm/uaccess.h>
31#include <asm/fcntl.h>
32
Jing Huang7725ccf2009-09-23 17:46:15 -070033#include "bfad_drv.h"
34#include "bfad_im.h"
Krishna Gudipatia36c61f2010-09-15 11:50:55 -070035#include "bfa_fcs.h"
Krishna Gudipatia36c61f2010-09-15 11:50:55 -070036#include "bfa_defs.h"
37#include "bfa.h"
Jing Huang7725ccf2009-09-23 17:46:15 -070038
39BFA_TRC_FILE(LDRV, BFAD);
Jing Huang42b426e2010-03-19 11:07:09 -070040DEFINE_MUTEX(bfad_mutex);
Jing Huang7725ccf2009-09-23 17:46:15 -070041LIST_HEAD(bfad_list);
Jing Huang7725ccf2009-09-23 17:46:15 -070042
Krishna Gudipatia36c61f2010-09-15 11:50:55 -070043static int bfad_inst;
44static int num_sgpgs_parm;
45int supported_fc4s;
46char *host_name, *os_name, *os_patch;
47int num_rports, num_ios, num_tms;
48int num_fcxps, num_ufbufs;
49int reqq_size, rspq_size, num_sgpgs;
50int rport_del_timeout = BFA_FCS_RPORT_DEF_DEL_TIMEOUT;
51int bfa_lun_queue_depth = BFAD_LUN_QUEUE_DEPTH;
52int bfa_io_max_sge = BFAD_IO_MAX_SGE;
Jing Huang88166242010-12-09 17:11:53 -080053int bfa_log_level = 3; /* WARNING log level */
Krishna Gudipatia36c61f2010-09-15 11:50:55 -070054int ioc_auto_recover = BFA_TRUE;
55int bfa_linkup_delay = -1;
56int fdmi_enable = BFA_TRUE;
57int pcie_max_read_reqsz;
Jing Huangab2a9ba2010-07-08 20:02:55 -070058int bfa_debugfs_enable = 1;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -070059int msix_disable_cb = 0, msix_disable_ct = 0;
Krishna Gudipati61e62e22011-06-24 20:29:07 -070060int max_xfer_size = BFAD_MAX_SECTORS >> 1;
Krishna Gudipati61ba4392012-08-22 19:52:58 -070061int max_rport_logins = BFA_FCS_MAX_RPORT_LOGINS;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -070062
Jing Huang61338a02011-04-13 11:44:03 -070063/* Firmware releated */
Krishna Gudipati11189202011-06-13 15:50:35 -070064u32 bfi_image_cb_size, bfi_image_ct_size, bfi_image_ct2_size;
65u32 *bfi_image_cb, *bfi_image_ct, *bfi_image_ct2;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -070066
Vijaya Mohan Guvva2da11ad2013-11-21 01:37:50 -080067#define BFAD_FW_FILE_CB "cbfw-3.2.3.0.bin"
68#define BFAD_FW_FILE_CT "ctfw-3.2.3.0.bin"
69#define BFAD_FW_FILE_CT2 "ct2fw-3.2.3.0.bin"
Jing Huang61338a02011-04-13 11:44:03 -070070
71static u32 *bfad_load_fwimg(struct pci_dev *pdev);
72static void bfad_free_fwimg(void);
73static void bfad_read_firmware(struct pci_dev *pdev, u32 **bfi_image,
74 u32 *bfi_image_size, char *fw_name);
75
Maggie52f94b62010-11-29 18:21:32 -080076static const char *msix_name_ct[] = {
Krishna Gudipati11189202011-06-13 15:50:35 -070077 "ctrl",
Krishna Gudipatia36c61f2010-09-15 11:50:55 -070078 "cpe0", "cpe1", "cpe2", "cpe3",
Krishna Gudipati11189202011-06-13 15:50:35 -070079 "rme0", "rme1", "rme2", "rme3" };
Krishna Gudipatia36c61f2010-09-15 11:50:55 -070080
Maggie52f94b62010-11-29 18:21:32 -080081static const char *msix_name_cb[] = {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -070082 "cpe0", "cpe1", "cpe2", "cpe3",
83 "rme0", "rme1", "rme2", "rme3",
84 "eemc", "elpu0", "elpu1", "epss", "mlpu" };
85
Krishna Gudipati11189202011-06-13 15:50:35 -070086MODULE_FIRMWARE(BFAD_FW_FILE_CB);
87MODULE_FIRMWARE(BFAD_FW_FILE_CT);
88MODULE_FIRMWARE(BFAD_FW_FILE_CT2);
Jing Huang7725ccf2009-09-23 17:46:15 -070089
90module_param(os_name, charp, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -070091MODULE_PARM_DESC(os_name, "OS name of the hba host machine");
Jing Huang7725ccf2009-09-23 17:46:15 -070092module_param(os_patch, charp, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -070093MODULE_PARM_DESC(os_patch, "OS patch level of the hba host machine");
Jing Huang7725ccf2009-09-23 17:46:15 -070094module_param(host_name, charp, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -070095MODULE_PARM_DESC(host_name, "Hostname of the hba host machine");
Jing Huang7725ccf2009-09-23 17:46:15 -070096module_param(num_rports, int, S_IRUGO | S_IWUSR);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -070097MODULE_PARM_DESC(num_rports, "Max number of rports supported per port "
98 "(physical/logical), default=1024");
Jing Huang7725ccf2009-09-23 17:46:15 -070099module_param(num_ios, int, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -0700100MODULE_PARM_DESC(num_ios, "Max number of ioim requests, default=2000");
Jing Huang7725ccf2009-09-23 17:46:15 -0700101module_param(num_tms, int, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -0700102MODULE_PARM_DESC(num_tms, "Max number of task im requests, default=128");
Jing Huang7725ccf2009-09-23 17:46:15 -0700103module_param(num_fcxps, int, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -0700104MODULE_PARM_DESC(num_fcxps, "Max number of fcxp requests, default=64");
Jing Huang7725ccf2009-09-23 17:46:15 -0700105module_param(num_ufbufs, int, S_IRUGO | S_IWUSR);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700106MODULE_PARM_DESC(num_ufbufs, "Max number of unsolicited frame "
107 "buffers, default=64");
Jing Huang7725ccf2009-09-23 17:46:15 -0700108module_param(reqq_size, int, S_IRUGO | S_IWUSR);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700109MODULE_PARM_DESC(reqq_size, "Max number of request queue elements, "
110 "default=256");
Jing Huang7725ccf2009-09-23 17:46:15 -0700111module_param(rspq_size, int, S_IRUGO | S_IWUSR);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700112MODULE_PARM_DESC(rspq_size, "Max number of response queue elements, "
113 "default=64");
Jing Huang7725ccf2009-09-23 17:46:15 -0700114module_param(num_sgpgs, int, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -0700115MODULE_PARM_DESC(num_sgpgs, "Number of scatter/gather pages, default=2048");
Jing Huang7725ccf2009-09-23 17:46:15 -0700116module_param(rport_del_timeout, int, S_IRUGO | S_IWUSR);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700117MODULE_PARM_DESC(rport_del_timeout, "Rport delete timeout, default=90 secs, "
118 "Range[>0]");
Jing Huang7725ccf2009-09-23 17:46:15 -0700119module_param(bfa_lun_queue_depth, int, S_IRUGO | S_IWUSR);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700120MODULE_PARM_DESC(bfa_lun_queue_depth, "Lun queue depth, default=32, Range[>0]");
Jing Huang7725ccf2009-09-23 17:46:15 -0700121module_param(bfa_io_max_sge, int, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -0700122MODULE_PARM_DESC(bfa_io_max_sge, "Max io scatter/gather elements, default=255");
Jing Huang88166242010-12-09 17:11:53 -0800123module_param(bfa_log_level, int, S_IRUGO | S_IWUSR);
124MODULE_PARM_DESC(bfa_log_level, "Driver log level, default=3, "
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700125 "Range[Critical:1|Error:2|Warning:3|Info:4]");
Jing Huang7725ccf2009-09-23 17:46:15 -0700126module_param(ioc_auto_recover, int, S_IRUGO | S_IWUSR);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700127MODULE_PARM_DESC(ioc_auto_recover, "IOC auto recovery, default=1, "
128 "Range[off:0|on:1]");
Jing Huang7725ccf2009-09-23 17:46:15 -0700129module_param(bfa_linkup_delay, int, S_IRUGO | S_IWUSR);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700130MODULE_PARM_DESC(bfa_linkup_delay, "Link up delay, default=30 secs for "
131 "boot port. Otherwise 10 secs in RHEL4 & 0 for "
132 "[RHEL5, SLES10, ESX40] Range[>0]");
133module_param(msix_disable_cb, int, S_IRUGO | S_IWUSR);
Anil Gurumurthy31e1d562015-11-26 03:54:46 -0500134MODULE_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 -0700135module_param(msix_disable_ct, int, S_IRUGO | S_IWUSR);
Anil Gurumurthy31e1d562015-11-26 03:54:46 -0500136MODULE_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 -0700137module_param(fdmi_enable, int, S_IRUGO | S_IWUSR);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700138MODULE_PARM_DESC(fdmi_enable, "Enables fdmi registration, default=1, "
139 "Range[false:0|true:1]");
140module_param(pcie_max_read_reqsz, int, S_IRUGO | S_IWUSR);
141MODULE_PARM_DESC(pcie_max_read_reqsz, "PCIe max read request size, default=0 "
142 "(use system setting), Range[128|256|512|1024|2048|4096]");
Jing Huangab2a9ba2010-07-08 20:02:55 -0700143module_param(bfa_debugfs_enable, int, S_IRUGO | S_IWUSR);
144MODULE_PARM_DESC(bfa_debugfs_enable, "Enables debugfs feature, default=1,"
145 " Range[false:0|true:1]");
Krishna Gudipati61e62e22011-06-24 20:29:07 -0700146module_param(max_xfer_size, int, S_IRUGO | S_IWUSR);
147MODULE_PARM_DESC(max_xfer_size, "default=32MB,"
148 " Range[64k|128k|256k|512k|1024k|2048k]");
Krishna Gudipati61ba4392012-08-22 19:52:58 -0700149module_param(max_rport_logins, int, S_IRUGO | S_IWUSR);
150MODULE_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 -0700151
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700152static void
153bfad_sm_uninit(struct bfad_s *bfad, enum bfad_sm_event event);
154static void
155bfad_sm_created(struct bfad_s *bfad, enum bfad_sm_event event);
156static void
157bfad_sm_initializing(struct bfad_s *bfad, enum bfad_sm_event event);
158static void
159bfad_sm_operational(struct bfad_s *bfad, enum bfad_sm_event event);
160static void
161bfad_sm_stopping(struct bfad_s *bfad, enum bfad_sm_event event);
162static void
163bfad_sm_failed(struct bfad_s *bfad, enum bfad_sm_event event);
164static void
165bfad_sm_fcs_exit(struct bfad_s *bfad, enum bfad_sm_event event);
166
Jing Huang5fbe25c2010-10-18 17:17:23 -0700167/*
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700168 * Beginning state for the driver instance, awaiting the pci_probe event
Jing Huang7725ccf2009-09-23 17:46:15 -0700169 */
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700170static void
171bfad_sm_uninit(struct bfad_s *bfad, enum bfad_sm_event event)
Jing Huang7725ccf2009-09-23 17:46:15 -0700172{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700173 bfa_trc(bfad, event);
Jing Huang7725ccf2009-09-23 17:46:15 -0700174
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700175 switch (event) {
176 case BFAD_E_CREATE:
177 bfa_sm_set_state(bfad, bfad_sm_created);
178 bfad->bfad_tsk = kthread_create(bfad_worker, (void *) bfad,
179 "%s", "bfad_worker");
180 if (IS_ERR(bfad->bfad_tsk)) {
181 printk(KERN_INFO "bfad[%d]: Kernel thread "
182 "creation failed!\n", bfad->inst_no);
183 bfa_sm_send_event(bfad, BFAD_E_KTHREAD_CREATE_FAILED);
184 }
185 bfa_sm_send_event(bfad, BFAD_E_INIT);
186 break;
Jing Huang7725ccf2009-09-23 17:46:15 -0700187
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700188 case BFAD_E_STOP:
189 /* Ignore stop; already in uninit */
190 break;
Jing Huang7725ccf2009-09-23 17:46:15 -0700191
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700192 default:
193 bfa_sm_fault(bfad, event);
194 }
195}
Krishna Gudipatie6714322010-03-03 17:44:02 -0800196
Jing Huang5fbe25c2010-10-18 17:17:23 -0700197/*
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700198 * Driver Instance is created, awaiting event INIT to initialize the bfad
199 */
200static void
201bfad_sm_created(struct bfad_s *bfad, enum bfad_sm_event event)
202{
203 unsigned long flags;
Vijaya Mohan Guvvada3e0be2013-11-21 01:37:33 -0800204 bfa_status_t ret;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700205
206 bfa_trc(bfad, event);
207
208 switch (event) {
209 case BFAD_E_INIT:
210 bfa_sm_set_state(bfad, bfad_sm_initializing);
211
212 init_completion(&bfad->comp);
213
214 /* Enable Interrupt and wait bfa_init completion */
215 if (bfad_setup_intr(bfad)) {
216 printk(KERN_WARNING "bfad%d: bfad_setup_intr failed\n",
217 bfad->inst_no);
Vijaya Mohan Guvvada3e0be2013-11-21 01:37:33 -0800218 bfa_sm_send_event(bfad, BFAD_E_INIT_FAILED);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700219 break;
220 }
221
222 spin_lock_irqsave(&bfad->bfad_lock, flags);
Maggie Zhangf7f738122010-12-09 19:08:43 -0800223 bfa_iocfc_init(&bfad->bfa);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700224 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
225
226 /* Set up interrupt handler for each vectors */
227 if ((bfad->bfad_flags & BFAD_MSIX_ON) &&
228 bfad_install_msix_handler(bfad)) {
229 printk(KERN_WARNING "%s: install_msix failed, bfad%d\n",
230 __func__, bfad->inst_no);
231 }
232
233 bfad_init_timer(bfad);
234
235 wait_for_completion(&bfad->comp);
236
237 if ((bfad->bfad_flags & BFAD_HAL_INIT_DONE)) {
238 bfa_sm_send_event(bfad, BFAD_E_INIT_SUCCESS);
239 } else {
Krishna Gudipati7c38c052011-04-14 16:50:35 -0700240 printk(KERN_WARNING
241 "bfa %s: bfa init failed\n",
242 bfad->pci_name);
Vijaya Mohan Guvvada3e0be2013-11-21 01:37:33 -0800243 spin_lock_irqsave(&bfad->bfad_lock, flags);
244 bfa_fcs_init(&bfad->bfa_fcs);
245 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
246
247 ret = bfad_cfg_pport(bfad, BFA_LPORT_ROLE_FCP_IM);
248 if (ret != BFA_STATUS_OK) {
249 init_completion(&bfad->comp);
250
251 spin_lock_irqsave(&bfad->bfad_lock, flags);
252 bfad->pport.flags |= BFAD_PORT_DELETE;
253 bfa_fcs_exit(&bfad->bfa_fcs);
254 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
255
256 wait_for_completion(&bfad->comp);
257
258 bfa_sm_send_event(bfad, BFAD_E_INIT_FAILED);
259 break;
260 }
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700261 bfad->bfad_flags |= BFAD_HAL_INIT_FAIL;
Vijaya Mohan Guvvada3e0be2013-11-21 01:37:33 -0800262 bfa_sm_send_event(bfad, BFAD_E_HAL_INIT_FAILED);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700263 }
264
265 break;
266
267 case BFAD_E_KTHREAD_CREATE_FAILED:
268 bfa_sm_set_state(bfad, bfad_sm_uninit);
269 break;
270
271 default:
272 bfa_sm_fault(bfad, event);
273 }
Jing Huang7725ccf2009-09-23 17:46:15 -0700274}
275
276static void
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700277bfad_sm_initializing(struct bfad_s *bfad, enum bfad_sm_event event)
Jing Huang7725ccf2009-09-23 17:46:15 -0700278{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700279 int retval;
280 unsigned long flags;
281
282 bfa_trc(bfad, event);
283
284 switch (event) {
285 case BFAD_E_INIT_SUCCESS:
286 kthread_stop(bfad->bfad_tsk);
287 spin_lock_irqsave(&bfad->bfad_lock, flags);
288 bfad->bfad_tsk = NULL;
289 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
290
291 retval = bfad_start_ops(bfad);
Vijaya Mohan Guvvada3e0be2013-11-21 01:37:33 -0800292 if (retval != BFA_STATUS_OK) {
293 bfa_sm_set_state(bfad, bfad_sm_failed);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700294 break;
Vijaya Mohan Guvvada3e0be2013-11-21 01:37:33 -0800295 }
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700296 bfa_sm_set_state(bfad, bfad_sm_operational);
297 break;
298
Vijaya Mohan Guvvada3e0be2013-11-21 01:37:33 -0800299 case BFAD_E_INIT_FAILED:
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700300 bfa_sm_set_state(bfad, bfad_sm_uninit);
301 kthread_stop(bfad->bfad_tsk);
302 spin_lock_irqsave(&bfad->bfad_lock, flags);
303 bfad->bfad_tsk = NULL;
304 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
305 break;
306
Vijaya Mohan Guvvada3e0be2013-11-21 01:37:33 -0800307 case BFAD_E_HAL_INIT_FAILED:
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700308 bfa_sm_set_state(bfad, bfad_sm_failed);
309 break;
310 default:
311 bfa_sm_fault(bfad, event);
312 }
Jing Huang7725ccf2009-09-23 17:46:15 -0700313}
314
315static void
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700316bfad_sm_failed(struct bfad_s *bfad, enum bfad_sm_event event)
Jing Huang7725ccf2009-09-23 17:46:15 -0700317{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700318 int retval;
Jing Huang7725ccf2009-09-23 17:46:15 -0700319
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700320 bfa_trc(bfad, event);
Jing Huang7725ccf2009-09-23 17:46:15 -0700321
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700322 switch (event) {
323 case BFAD_E_INIT_SUCCESS:
324 retval = bfad_start_ops(bfad);
325 if (retval != BFA_STATUS_OK)
326 break;
327 bfa_sm_set_state(bfad, bfad_sm_operational);
328 break;
Jing Huang7725ccf2009-09-23 17:46:15 -0700329
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700330 case BFAD_E_STOP:
Vijaya Mohan Guvvada3e0be2013-11-21 01:37:33 -0800331 bfa_sm_set_state(bfad, bfad_sm_fcs_exit);
332 bfa_sm_send_event(bfad, BFAD_E_FCS_EXIT_COMP);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700333 break;
Jing Huang7725ccf2009-09-23 17:46:15 -0700334
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700335 case BFAD_E_EXIT_COMP:
336 bfa_sm_set_state(bfad, bfad_sm_uninit);
337 bfad_remove_intr(bfad);
338 del_timer_sync(&bfad->hal_tmo);
339 break;
Jing Huang7725ccf2009-09-23 17:46:15 -0700340
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700341 default:
342 bfa_sm_fault(bfad, event);
343 }
Jing Huang7725ccf2009-09-23 17:46:15 -0700344}
345
346static void
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700347bfad_sm_operational(struct bfad_s *bfad, enum bfad_sm_event event)
Jing Huang7725ccf2009-09-23 17:46:15 -0700348{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700349 bfa_trc(bfad, event);
Jing Huang7725ccf2009-09-23 17:46:15 -0700350
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700351 switch (event) {
352 case BFAD_E_STOP:
353 bfa_sm_set_state(bfad, bfad_sm_fcs_exit);
354 bfad_fcs_stop(bfad);
355 break;
Jing Huang7725ccf2009-09-23 17:46:15 -0700356
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700357 default:
358 bfa_sm_fault(bfad, event);
359 }
360}
361
362static void
363bfad_sm_fcs_exit(struct bfad_s *bfad, enum bfad_sm_event event)
364{
365 bfa_trc(bfad, event);
366
367 switch (event) {
368 case BFAD_E_FCS_EXIT_COMP:
369 bfa_sm_set_state(bfad, bfad_sm_stopping);
370 bfad_stop(bfad);
371 break;
372
373 default:
374 bfa_sm_fault(bfad, event);
375 }
376}
377
378static void
379bfad_sm_stopping(struct bfad_s *bfad, enum bfad_sm_event event)
380{
381 bfa_trc(bfad, event);
382
383 switch (event) {
384 case BFAD_E_EXIT_COMP:
385 bfa_sm_set_state(bfad, bfad_sm_uninit);
386 bfad_remove_intr(bfad);
387 del_timer_sync(&bfad->hal_tmo);
388 bfad_im_probe_undo(bfad);
389 bfad->bfad_flags &= ~BFAD_FC4_PROBE_DONE;
390 bfad_uncfg_pport(bfad);
391 break;
392
393 default:
394 bfa_sm_fault(bfad, event);
395 break;
396 }
Jing Huang7725ccf2009-09-23 17:46:15 -0700397}
398
Jing Huang5fbe25c2010-10-18 17:17:23 -0700399/*
Jing Huang7725ccf2009-09-23 17:46:15 -0700400 * BFA callbacks
401 */
402void
403bfad_hcb_comp(void *arg, bfa_status_t status)
404{
405 struct bfad_hal_comp *fcomp = (struct bfad_hal_comp *)arg;
406
407 fcomp->status = status;
408 complete(&fcomp->comp);
409}
410
Jing Huang5fbe25c2010-10-18 17:17:23 -0700411/*
Jing Huang7725ccf2009-09-23 17:46:15 -0700412 * bfa_init callback
413 */
414void
415bfa_cb_init(void *drv, bfa_status_t init_status)
416{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700417 struct bfad_s *bfad = drv;
Jing Huang7725ccf2009-09-23 17:46:15 -0700418
Krishna Gudipatie6714322010-03-03 17:44:02 -0800419 if (init_status == BFA_STATUS_OK) {
Jing Huang7725ccf2009-09-23 17:46:15 -0700420 bfad->bfad_flags |= BFAD_HAL_INIT_DONE;
421
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700422 /*
423 * If BFAD_HAL_INIT_FAIL flag is set:
Krishna Gudipatie6714322010-03-03 17:44:02 -0800424 * Wake up the kernel thread to start
425 * the bfad operations after HAL init done
426 */
427 if ((bfad->bfad_flags & BFAD_HAL_INIT_FAIL)) {
428 bfad->bfad_flags &= ~BFAD_HAL_INIT_FAIL;
429 wake_up_process(bfad->bfad_tsk);
430 }
431 }
432
Jing Huang7725ccf2009-09-23 17:46:15 -0700433 complete(&bfad->comp);
434}
435
Jing Huang5fbe25c2010-10-18 17:17:23 -0700436/*
Jing Huang7725ccf2009-09-23 17:46:15 -0700437 * BFA_FCS callbacks
438 */
Jing Huang7725ccf2009-09-23 17:46:15 -0700439struct bfad_port_s *
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700440bfa_fcb_lport_new(struct bfad_s *bfad, struct bfa_fcs_lport_s *port,
441 enum bfa_lport_role roles, struct bfad_vf_s *vf_drv,
Jing Huang7725ccf2009-09-23 17:46:15 -0700442 struct bfad_vport_s *vp_drv)
443{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700444 bfa_status_t rc;
445 struct bfad_port_s *port_drv;
Jing Huang7725ccf2009-09-23 17:46:15 -0700446
447 if (!vp_drv && !vf_drv) {
448 port_drv = &bfad->pport;
449 port_drv->pvb_type = BFAD_PORT_PHYS_BASE;
450 } else if (!vp_drv && vf_drv) {
451 port_drv = &vf_drv->base_port;
452 port_drv->pvb_type = BFAD_PORT_VF_BASE;
453 } else if (vp_drv && !vf_drv) {
454 port_drv = &vp_drv->drv_port;
455 port_drv->pvb_type = BFAD_PORT_PHYS_VPORT;
456 } else {
457 port_drv = &vp_drv->drv_port;
458 port_drv->pvb_type = BFAD_PORT_VF_VPORT;
459 }
460
461 port_drv->fcs_port = port;
462 port_drv->roles = roles;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700463
464 if (roles & BFA_LPORT_ROLE_FCP_IM) {
465 rc = bfad_im_port_new(bfad, port_drv);
466 if (rc != BFA_STATUS_OK) {
467 bfad_im_port_delete(bfad, port_drv);
468 port_drv = NULL;
469 }
Jing Huang7725ccf2009-09-23 17:46:15 -0700470 }
471
472 return port_drv;
473}
474
Jing Huang5fbe25c2010-10-18 17:17:23 -0700475/*
Jing Huang7725ccf2009-09-23 17:46:15 -0700476 * FCS RPORT alloc callback, after successful PLOGI by FCS
477 */
478bfa_status_t
479bfa_fcb_rport_alloc(struct bfad_s *bfad, struct bfa_fcs_rport_s **rport,
480 struct bfad_rport_s **rport_drv)
481{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700482 bfa_status_t rc = BFA_STATUS_OK;
Jing Huang7725ccf2009-09-23 17:46:15 -0700483
484 *rport_drv = kzalloc(sizeof(struct bfad_rport_s), GFP_ATOMIC);
485 if (*rport_drv == NULL) {
486 rc = BFA_STATUS_ENOMEM;
487 goto ext;
488 }
489
490 *rport = &(*rport_drv)->fcs_rport;
491
492ext:
493 return rc;
494}
495
Jing Huang5fbe25c2010-10-18 17:17:23 -0700496/*
Jing Huangd9883542010-07-08 19:46:26 -0700497 * FCS PBC VPORT Create
498 */
499void
500bfa_fcb_pbc_vport_create(struct bfad_s *bfad, struct bfi_pbc_vport_s pbc_vport)
501{
Jing Huang7725ccf2009-09-23 17:46:15 -0700502
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700503 struct bfa_lport_cfg_s port_cfg = {0};
504 struct bfad_vport_s *vport;
505 int rc;
Jing Huangd9883542010-07-08 19:46:26 -0700506
Alexey Khoroshilovdc6d2a02014-04-18 00:58:42 -0700507 vport = kzalloc(sizeof(struct bfad_vport_s), GFP_ATOMIC);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700508 if (!vport) {
Jing Huangd9883542010-07-08 19:46:26 -0700509 bfa_trc(bfad, 0);
510 return;
511 }
512
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700513 vport->drv_port.bfad = bfad;
514 port_cfg.roles = BFA_LPORT_ROLE_FCP_IM;
515 port_cfg.pwwn = pbc_vport.vp_pwwn;
516 port_cfg.nwwn = pbc_vport.vp_nwwn;
517 port_cfg.preboot_vp = BFA_TRUE;
Jing Huangd9883542010-07-08 19:46:26 -0700518
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700519 rc = bfa_fcs_pbc_vport_create(&vport->fcs_vport, &bfad->bfa_fcs, 0,
520 &port_cfg, vport);
Jing Huangd9883542010-07-08 19:46:26 -0700521
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700522 if (rc != BFA_STATUS_OK) {
523 bfa_trc(bfad, 0);
524 return;
525 }
526
527 list_add_tail(&vport->list_entry, &bfad->pbc_vport_list);
Jing Huangd9883542010-07-08 19:46:26 -0700528}
Jing Huang7725ccf2009-09-23 17:46:15 -0700529
530void
531bfad_hal_mem_release(struct bfad_s *bfad)
532{
Jing Huang7725ccf2009-09-23 17:46:15 -0700533 struct bfa_meminfo_s *hal_meminfo = &bfad->meminfo;
Krishna Gudipati45070252011-06-24 20:24:29 -0700534 struct bfa_mem_dma_s *dma_info, *dma_elem;
535 struct bfa_mem_kva_s *kva_info, *kva_elem;
536 struct list_head *dm_qe, *km_qe;
Jing Huang7725ccf2009-09-23 17:46:15 -0700537
Krishna Gudipati45070252011-06-24 20:24:29 -0700538 dma_info = &hal_meminfo->dma_info;
539 kva_info = &hal_meminfo->kva_info;
540
541 /* Iterate through the KVA meminfo queue */
542 list_for_each(km_qe, &kva_info->qe) {
543 kva_elem = (struct bfa_mem_kva_s *) km_qe;
544 vfree(kva_elem->kva);
545 }
546
547 /* Iterate through the DMA meminfo queue */
548 list_for_each(dm_qe, &dma_info->qe) {
549 dma_elem = (struct bfa_mem_dma_s *) dm_qe;
550 dma_free_coherent(&bfad->pcidev->dev,
551 dma_elem->mem_len, dma_elem->kva,
552 (dma_addr_t) dma_elem->dma);
Jing Huang7725ccf2009-09-23 17:46:15 -0700553 }
554
555 memset(hal_meminfo, 0, sizeof(struct bfa_meminfo_s));
556}
557
558void
559bfad_update_hal_cfg(struct bfa_iocfc_cfg_s *bfa_cfg)
560{
561 if (num_rports > 0)
562 bfa_cfg->fwcfg.num_rports = num_rports;
563 if (num_ios > 0)
564 bfa_cfg->fwcfg.num_ioim_reqs = num_ios;
565 if (num_tms > 0)
566 bfa_cfg->fwcfg.num_tskim_reqs = num_tms;
Krishna Gudipati45070252011-06-24 20:24:29 -0700567 if (num_fcxps > 0 && num_fcxps <= BFA_FCXP_MAX)
Jing Huang7725ccf2009-09-23 17:46:15 -0700568 bfa_cfg->fwcfg.num_fcxp_reqs = num_fcxps;
Krishna Gudipati45070252011-06-24 20:24:29 -0700569 if (num_ufbufs > 0 && num_ufbufs <= BFA_UF_MAX)
Jing Huang7725ccf2009-09-23 17:46:15 -0700570 bfa_cfg->fwcfg.num_uf_bufs = num_ufbufs;
571 if (reqq_size > 0)
572 bfa_cfg->drvcfg.num_reqq_elems = reqq_size;
573 if (rspq_size > 0)
574 bfa_cfg->drvcfg.num_rspq_elems = rspq_size;
Krishna Gudipati45070252011-06-24 20:24:29 -0700575 if (num_sgpgs > 0 && num_sgpgs <= BFA_SGPG_MAX)
Jing Huang7725ccf2009-09-23 17:46:15 -0700576 bfa_cfg->drvcfg.num_sgpgs = num_sgpgs;
577
578 /*
579 * populate the hal values back to the driver for sysfs use.
580 * otherwise, the default values will be shown as 0 in sysfs
581 */
582 num_rports = bfa_cfg->fwcfg.num_rports;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700583 num_ios = bfa_cfg->fwcfg.num_ioim_reqs;
584 num_tms = bfa_cfg->fwcfg.num_tskim_reqs;
585 num_fcxps = bfa_cfg->fwcfg.num_fcxp_reqs;
Jing Huang7725ccf2009-09-23 17:46:15 -0700586 num_ufbufs = bfa_cfg->fwcfg.num_uf_bufs;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700587 reqq_size = bfa_cfg->drvcfg.num_reqq_elems;
588 rspq_size = bfa_cfg->drvcfg.num_rspq_elems;
589 num_sgpgs = bfa_cfg->drvcfg.num_sgpgs;
Jing Huang7725ccf2009-09-23 17:46:15 -0700590}
591
592bfa_status_t
593bfad_hal_mem_alloc(struct bfad_s *bfad)
594{
595 struct bfa_meminfo_s *hal_meminfo = &bfad->meminfo;
Krishna Gudipati45070252011-06-24 20:24:29 -0700596 struct bfa_mem_dma_s *dma_info, *dma_elem;
597 struct bfa_mem_kva_s *kva_info, *kva_elem;
598 struct list_head *dm_qe, *km_qe;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700599 bfa_status_t rc = BFA_STATUS_OK;
Krishna Gudipati45070252011-06-24 20:24:29 -0700600 dma_addr_t phys_addr;
Jing Huang7725ccf2009-09-23 17:46:15 -0700601
602 bfa_cfg_get_default(&bfad->ioc_cfg);
Jing Huang7725ccf2009-09-23 17:46:15 -0700603 bfad_update_hal_cfg(&bfad->ioc_cfg);
604 bfad->cfg_data.ioc_queue_depth = bfad->ioc_cfg.fwcfg.num_ioim_reqs;
Krishna Gudipati45070252011-06-24 20:24:29 -0700605 bfa_cfg_get_meminfo(&bfad->ioc_cfg, hal_meminfo, &bfad->bfa);
Jing Huang7725ccf2009-09-23 17:46:15 -0700606
Krishna Gudipati45070252011-06-24 20:24:29 -0700607 dma_info = &hal_meminfo->dma_info;
608 kva_info = &hal_meminfo->kva_info;
Jing Huang7725ccf2009-09-23 17:46:15 -0700609
Krishna Gudipati45070252011-06-24 20:24:29 -0700610 /* Iterate through the KVA meminfo queue */
611 list_for_each(km_qe, &kva_info->qe) {
612 kva_elem = (struct bfa_mem_kva_s *) km_qe;
613 kva_elem->kva = vmalloc(kva_elem->mem_len);
614 if (kva_elem->kva == NULL) {
615 bfad_hal_mem_release(bfad);
616 rc = BFA_STATUS_ENOMEM;
617 goto ext;
Jing Huang7725ccf2009-09-23 17:46:15 -0700618 }
Krishna Gudipati45070252011-06-24 20:24:29 -0700619 memset(kva_elem->kva, 0, kva_elem->mem_len);
620 }
621
622 /* Iterate through the DMA meminfo queue */
623 list_for_each(dm_qe, &dma_info->qe) {
624 dma_elem = (struct bfa_mem_dma_s *) dm_qe;
625 dma_elem->kva = dma_alloc_coherent(&bfad->pcidev->dev,
626 dma_elem->mem_len,
627 &phys_addr, GFP_KERNEL);
628 if (dma_elem->kva == NULL) {
629 bfad_hal_mem_release(bfad);
630 rc = BFA_STATUS_ENOMEM;
631 goto ext;
632 }
633 dma_elem->dma = phys_addr;
634 memset(dma_elem->kva, 0, dma_elem->mem_len);
Jing Huang7725ccf2009-09-23 17:46:15 -0700635 }
636ext:
637 return rc;
638}
639
Jing Huang5fbe25c2010-10-18 17:17:23 -0700640/*
Jing Huang7725ccf2009-09-23 17:46:15 -0700641 * Create a vport under a vf.
642 */
643bfa_status_t
644bfad_vport_create(struct bfad_s *bfad, u16 vf_id,
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700645 struct bfa_lport_cfg_s *port_cfg, struct device *dev)
Jing Huang7725ccf2009-09-23 17:46:15 -0700646{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700647 struct bfad_vport_s *vport;
648 int rc = BFA_STATUS_OK;
649 unsigned long flags;
Jing Huang7725ccf2009-09-23 17:46:15 -0700650 struct completion fcomp;
651
652 vport = kzalloc(sizeof(struct bfad_vport_s), GFP_KERNEL);
653 if (!vport) {
654 rc = BFA_STATUS_ENOMEM;
655 goto ext;
656 }
657
658 vport->drv_port.bfad = bfad;
659 spin_lock_irqsave(&bfad->bfad_lock, flags);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700660 rc = bfa_fcs_vport_create(&vport->fcs_vport, &bfad->bfa_fcs, vf_id,
661 port_cfg, vport);
Jing Huang7725ccf2009-09-23 17:46:15 -0700662 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
663
664 if (rc != BFA_STATUS_OK)
665 goto ext_free_vport;
666
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700667 if (port_cfg->roles & BFA_LPORT_ROLE_FCP_IM) {
Jing Huangb5042932010-03-19 11:05:39 -0700668 rc = bfad_im_scsi_host_alloc(bfad, vport->drv_port.im_port,
669 dev);
Jing Huang7725ccf2009-09-23 17:46:15 -0700670 if (rc != BFA_STATUS_OK)
671 goto ext_free_fcs_vport;
672 }
673
674 spin_lock_irqsave(&bfad->bfad_lock, flags);
675 bfa_fcs_vport_start(&vport->fcs_vport);
Krishna Gudipati5b7db7a2011-12-20 18:58:32 -0800676 list_add_tail(&vport->list_entry, &bfad->vport_list);
Jing Huang7725ccf2009-09-23 17:46:15 -0700677 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
678
679 return BFA_STATUS_OK;
680
681ext_free_fcs_vport:
682 spin_lock_irqsave(&bfad->bfad_lock, flags);
683 vport->comp_del = &fcomp;
684 init_completion(vport->comp_del);
685 bfa_fcs_vport_delete(&vport->fcs_vport);
686 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
687 wait_for_completion(vport->comp_del);
688ext_free_vport:
689 kfree(vport);
690ext:
691 return rc;
692}
693
Jing Huang7725ccf2009-09-23 17:46:15 -0700694void
695bfad_bfa_tmo(unsigned long data)
696{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700697 struct bfad_s *bfad = (struct bfad_s *) data;
698 unsigned long flags;
699 struct list_head doneq;
Jing Huang7725ccf2009-09-23 17:46:15 -0700700
701 spin_lock_irqsave(&bfad->bfad_lock, flags);
702
Maggie Zhangf7f738122010-12-09 19:08:43 -0800703 bfa_timer_beat(&bfad->bfa.timer_mod);
Jing Huang7725ccf2009-09-23 17:46:15 -0700704
705 bfa_comp_deq(&bfad->bfa, &doneq);
706 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
707
708 if (!list_empty(&doneq)) {
709 bfa_comp_process(&bfad->bfa, &doneq);
710 spin_lock_irqsave(&bfad->bfad_lock, flags);
711 bfa_comp_free(&bfad->bfa, &doneq);
712 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
713 }
714
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
719void
720bfad_init_timer(struct bfad_s *bfad)
721{
722 init_timer(&bfad->hal_tmo);
723 bfad->hal_tmo.function = bfad_bfa_tmo;
724 bfad->hal_tmo.data = (unsigned long)bfad;
725
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700726 mod_timer(&bfad->hal_tmo,
727 jiffies + msecs_to_jiffies(BFA_TIMER_FREQ));
Jing Huang7725ccf2009-09-23 17:46:15 -0700728}
729
730int
731bfad_pci_init(struct pci_dev *pdev, struct bfad_s *bfad)
732{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700733 int rc = -ENODEV;
Jing Huang7725ccf2009-09-23 17:46:15 -0700734
735 if (pci_enable_device(pdev)) {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700736 printk(KERN_ERR "pci_enable_device fail %p\n", pdev);
Jing Huang7725ccf2009-09-23 17:46:15 -0700737 goto out;
738 }
739
740 if (pci_request_regions(pdev, BFAD_DRIVER_NAME))
741 goto out_disable_device;
742
743 pci_set_master(pdev);
744
745
Krishna Gudipati10a07372011-06-24 20:23:38 -0700746 if ((pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) ||
747 (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)) != 0)) {
748 if ((pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0) ||
749 (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)) {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700750 printk(KERN_ERR "pci_set_dma_mask fail %p\n", pdev);
Jing Huang7725ccf2009-09-23 17:46:15 -0700751 goto out_release_region;
752 }
Krishna Gudipati10a07372011-06-24 20:23:38 -0700753 }
Jing Huang7725ccf2009-09-23 17:46:15 -0700754
Krishna Gudipati881c1b32012-08-22 19:52:02 -0700755 /* Enable PCIE Advanced Error Recovery (AER) if kernel supports */
756 pci_enable_pcie_error_reporting(pdev);
757
Jing Huangb3522f02010-03-19 11:06:44 -0700758 bfad->pci_bar0_kva = pci_iomap(pdev, 0, pci_resource_len(pdev, 0));
Krishna Gudipati11189202011-06-13 15:50:35 -0700759 bfad->pci_bar2_kva = pci_iomap(pdev, 2, pci_resource_len(pdev, 2));
Jing Huang7725ccf2009-09-23 17:46:15 -0700760
761 if (bfad->pci_bar0_kva == NULL) {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700762 printk(KERN_ERR "Fail to map bar0\n");
Jing Huang7725ccf2009-09-23 17:46:15 -0700763 goto out_release_region;
764 }
765
766 bfad->hal_pcidev.pci_slot = PCI_SLOT(pdev->devfn);
767 bfad->hal_pcidev.pci_func = PCI_FUNC(pdev->devfn);
768 bfad->hal_pcidev.pci_bar_kva = bfad->pci_bar0_kva;
769 bfad->hal_pcidev.device_id = pdev->device;
Krishna Gudipati1a4d8e12011-06-24 20:22:28 -0700770 bfad->hal_pcidev.ssid = pdev->subsystem_device;
Jing Huang7725ccf2009-09-23 17:46:15 -0700771 bfad->pci_name = pci_name(pdev);
772
773 bfad->pci_attr.vendor_id = pdev->vendor;
774 bfad->pci_attr.device_id = pdev->device;
775 bfad->pci_attr.ssid = pdev->subsystem_device;
776 bfad->pci_attr.ssvid = pdev->subsystem_vendor;
777 bfad->pci_attr.pcifn = PCI_FUNC(pdev->devfn);
778
779 bfad->pcidev = pdev;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700780
781 /* Adjust PCIe Maximum Read Request Size */
Yijing Wangc0102c02013-09-05 15:55:25 +0800782 if (pci_is_pcie(pdev) && pcie_max_read_reqsz) {
783 if (pcie_max_read_reqsz >= 128 &&
784 pcie_max_read_reqsz <= 4096 &&
785 is_power_of_2(pcie_max_read_reqsz)) {
786 int max_rq = pcie_get_readrq(pdev);
787 printk(KERN_WARNING "BFA[%s]: "
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700788 "pcie_max_read_request_size is %d, "
Yijing Wangc0102c02013-09-05 15:55:25 +0800789 "reset to %d\n", bfad->pci_name, max_rq,
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700790 pcie_max_read_reqsz);
Yijing Wangc0102c02013-09-05 15:55:25 +0800791 pcie_set_readrq(pdev, pcie_max_read_reqsz);
792 } else {
793 printk(KERN_WARNING "BFA[%s]: invalid "
794 "pcie_max_read_request_size %d ignored\n",
795 bfad->pci_name, pcie_max_read_reqsz);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700796 }
797 }
798
Krishna Gudipati881c1b32012-08-22 19:52:02 -0700799 pci_save_state(pdev);
800
Jing Huang7725ccf2009-09-23 17:46:15 -0700801 return 0;
802
803out_release_region:
804 pci_release_regions(pdev);
805out_disable_device:
806 pci_disable_device(pdev);
807out:
808 return rc;
809}
810
811void
812bfad_pci_uninit(struct pci_dev *pdev, struct bfad_s *bfad)
813{
Jing Huang7725ccf2009-09-23 17:46:15 -0700814 pci_iounmap(pdev, bfad->pci_bar0_kva);
Krishna Gudipati11189202011-06-13 15:50:35 -0700815 pci_iounmap(pdev, bfad->pci_bar2_kva);
Jing Huang7725ccf2009-09-23 17:46:15 -0700816 pci_release_regions(pdev);
Krishna Gudipati881c1b32012-08-22 19:52:02 -0700817 /* Disable PCIE Advanced Error Recovery (AER) */
818 pci_disable_pcie_error_reporting(pdev);
Jing Huang7725ccf2009-09-23 17:46:15 -0700819 pci_disable_device(pdev);
Jing Huang7725ccf2009-09-23 17:46:15 -0700820}
821
Jing Huang7725ccf2009-09-23 17:46:15 -0700822bfa_status_t
823bfad_drv_init(struct bfad_s *bfad)
824{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700825 bfa_status_t rc;
826 unsigned long flags;
Jing Huang7725ccf2009-09-23 17:46:15 -0700827
828 bfad->cfg_data.rport_del_timeout = rport_del_timeout;
829 bfad->cfg_data.lun_queue_depth = bfa_lun_queue_depth;
830 bfad->cfg_data.io_max_sge = bfa_io_max_sge;
831 bfad->cfg_data.binding_method = FCP_PWWN_BINDING;
832
833 rc = bfad_hal_mem_alloc(bfad);
834 if (rc != BFA_STATUS_OK) {
835 printk(KERN_WARNING "bfad%d bfad_hal_mem_alloc failure\n",
836 bfad->inst_no);
837 printk(KERN_WARNING
Anil Gurumurthy31e1d562015-11-26 03:54:46 -0500838 "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 -0800839 return BFA_STATUS_FAILED;
Jing Huang7725ccf2009-09-23 17:46:15 -0700840 }
841
Maggie Zhangf7f738122010-12-09 19:08:43 -0800842 bfad->bfa.trcmod = bfad->trcmod;
843 bfad->bfa.plog = &bfad->plog_buf;
Jing Huang7725ccf2009-09-23 17:46:15 -0700844 bfa_plog_init(&bfad->plog_buf);
845 bfa_plog_str(&bfad->plog_buf, BFA_PL_MID_DRVR, BFA_PL_EID_DRIVER_START,
846 0, "Driver Attach");
847
848 bfa_attach(&bfad->bfa, bfad, &bfad->ioc_cfg, &bfad->meminfo,
849 &bfad->hal_pcidev);
850
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700851 /* FCS INIT */
Jing Huang7725ccf2009-09-23 17:46:15 -0700852 spin_lock_irqsave(&bfad->bfad_lock, flags);
Maggie Zhangf7f738122010-12-09 19:08:43 -0800853 bfad->bfa_fcs.trcmod = bfad->trcmod;
Krishna Gudipati82794a22010-03-03 17:43:30 -0800854 bfa_fcs_attach(&bfad->bfa_fcs, &bfad->bfa, bfad, BFA_FALSE);
Maggie Zhangf7f738122010-12-09 19:08:43 -0800855 bfad->bfa_fcs.fdmi_enabled = fdmi_enable;
Jing Huang7725ccf2009-09-23 17:46:15 -0700856 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
857
858 bfad->bfad_flags |= BFAD_DRV_INIT_DONE;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700859
Jing Huang7725ccf2009-09-23 17:46:15 -0700860 return BFA_STATUS_OK;
Jing Huang7725ccf2009-09-23 17:46:15 -0700861}
862
863void
864bfad_drv_uninit(struct bfad_s *bfad)
865{
Krishna Gudipatie6714322010-03-03 17:44:02 -0800866 unsigned long flags;
867
868 spin_lock_irqsave(&bfad->bfad_lock, flags);
869 init_completion(&bfad->comp);
Maggie Zhangf7f738122010-12-09 19:08:43 -0800870 bfa_iocfc_stop(&bfad->bfa);
Krishna Gudipatie6714322010-03-03 17:44:02 -0800871 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
872 wait_for_completion(&bfad->comp);
873
Jing Huang7725ccf2009-09-23 17:46:15 -0700874 del_timer_sync(&bfad->hal_tmo);
875 bfa_isr_disable(&bfad->bfa);
876 bfa_detach(&bfad->bfa);
877 bfad_remove_intr(bfad);
Jing Huang7725ccf2009-09-23 17:46:15 -0700878 bfad_hal_mem_release(bfad);
Krishna Gudipatie6714322010-03-03 17:44:02 -0800879
880 bfad->bfad_flags &= ~BFAD_DRV_INIT_DONE;
Jing Huang7725ccf2009-09-23 17:46:15 -0700881}
882
883void
884bfad_drv_start(struct bfad_s *bfad)
885{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700886 unsigned long flags;
Jing Huang7725ccf2009-09-23 17:46:15 -0700887
888 spin_lock_irqsave(&bfad->bfad_lock, flags);
Maggie Zhangf7f738122010-12-09 19:08:43 -0800889 bfa_iocfc_start(&bfad->bfa);
Krishna Gudipati75332a72011-06-13 15:54:31 -0700890 bfa_fcs_pbc_vport_init(&bfad->bfa_fcs);
Maggie Zhangf7f738122010-12-09 19:08:43 -0800891 bfa_fcs_fabric_modstart(&bfad->bfa_fcs);
Jing Huang7725ccf2009-09-23 17:46:15 -0700892 bfad->bfad_flags |= BFAD_HAL_START_DONE;
893 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
894
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700895 if (bfad->im)
896 flush_workqueue(bfad->im->drv_workq);
Jing Huang7725ccf2009-09-23 17:46:15 -0700897}
898
899void
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700900bfad_fcs_stop(struct bfad_s *bfad)
Jing Huang7725ccf2009-09-23 17:46:15 -0700901{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700902 unsigned long flags;
Jing Huang7725ccf2009-09-23 17:46:15 -0700903
904 spin_lock_irqsave(&bfad->bfad_lock, flags);
905 init_completion(&bfad->comp);
906 bfad->pport.flags |= BFAD_PORT_DELETE;
907 bfa_fcs_exit(&bfad->bfa_fcs);
908 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
909 wait_for_completion(&bfad->comp);
910
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700911 bfa_sm_send_event(bfad, BFAD_E_FCS_EXIT_COMP);
912}
913
914void
915bfad_stop(struct bfad_s *bfad)
916{
917 unsigned long flags;
918
Jing Huang7725ccf2009-09-23 17:46:15 -0700919 spin_lock_irqsave(&bfad->bfad_lock, flags);
920 init_completion(&bfad->comp);
Maggie Zhangf7f738122010-12-09 19:08:43 -0800921 bfa_iocfc_stop(&bfad->bfa);
Jing Huang7725ccf2009-09-23 17:46:15 -0700922 bfad->bfad_flags &= ~BFAD_HAL_START_DONE;
923 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
924 wait_for_completion(&bfad->comp);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700925
926 bfa_sm_send_event(bfad, BFAD_E_EXIT_COMP);
Jing Huang7725ccf2009-09-23 17:46:15 -0700927}
928
929bfa_status_t
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700930bfad_cfg_pport(struct bfad_s *bfad, enum bfa_lport_role role)
Jing Huang7725ccf2009-09-23 17:46:15 -0700931{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700932 int rc = BFA_STATUS_OK;
Jing Huang7725ccf2009-09-23 17:46:15 -0700933
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700934 /* Allocate scsi_host for the physical port */
935 if ((supported_fc4s & BFA_LPORT_ROLE_FCP_IM) &&
936 (role & BFA_LPORT_ROLE_FCP_IM)) {
Jing Huang7725ccf2009-09-23 17:46:15 -0700937 if (bfad->pport.im_port == NULL) {
938 rc = BFA_STATUS_FAILED;
939 goto out;
940 }
941
Jing Huangb5042932010-03-19 11:05:39 -0700942 rc = bfad_im_scsi_host_alloc(bfad, bfad->pport.im_port,
943 &bfad->pcidev->dev);
Jing Huang7725ccf2009-09-23 17:46:15 -0700944 if (rc != BFA_STATUS_OK)
945 goto out;
946
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700947 bfad->pport.roles |= BFA_LPORT_ROLE_FCP_IM;
Jing Huang7725ccf2009-09-23 17:46:15 -0700948 }
949
950 bfad->bfad_flags |= BFAD_CFG_PPORT_DONE;
951
952out:
953 return rc;
954}
955
956void
957bfad_uncfg_pport(struct bfad_s *bfad)
958{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700959 if ((supported_fc4s & BFA_LPORT_ROLE_FCP_IM) &&
960 (bfad->pport.roles & BFA_LPORT_ROLE_FCP_IM)) {
Jing Huang7725ccf2009-09-23 17:46:15 -0700961 bfad_im_scsi_host_free(bfad, bfad->pport.im_port);
962 bfad_im_port_clean(bfad->pport.im_port);
963 kfree(bfad->pport.im_port);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700964 bfad->pport.roles &= ~BFA_LPORT_ROLE_FCP_IM;
Jing Huang7725ccf2009-09-23 17:46:15 -0700965 }
966
967 bfad->bfad_flags &= ~BFAD_CFG_PPORT_DONE;
968}
969
Krishna Gudipatie6714322010-03-03 17:44:02 -0800970bfa_status_t
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700971bfad_start_ops(struct bfad_s *bfad) {
972
973 int retval;
974 unsigned long flags;
975 struct bfad_vport_s *vport, *vport_new;
976 struct bfa_fcs_driver_info_s driver_info;
977
Krishna Gudipati61e62e22011-06-24 20:29:07 -0700978 /* Limit min/max. xfer size to [64k-32MB] */
979 if (max_xfer_size < BFAD_MIN_SECTORS >> 1)
980 max_xfer_size = BFAD_MIN_SECTORS >> 1;
981 if (max_xfer_size > BFAD_MAX_SECTORS >> 1)
982 max_xfer_size = BFAD_MAX_SECTORS >> 1;
983
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700984 /* Fill the driver_info info to fcs*/
985 memset(&driver_info, 0, sizeof(driver_info));
986 strncpy(driver_info.version, BFAD_DRIVER_VERSION,
987 sizeof(driver_info.version) - 1);
988 if (host_name)
989 strncpy(driver_info.host_machine_name, host_name,
990 sizeof(driver_info.host_machine_name) - 1);
991 if (os_name)
992 strncpy(driver_info.host_os_name, os_name,
993 sizeof(driver_info.host_os_name) - 1);
994 if (os_patch)
995 strncpy(driver_info.host_os_patch, os_patch,
996 sizeof(driver_info.host_os_patch) - 1);
997
998 strncpy(driver_info.os_device_name, bfad->pci_name,
Dan Carpenter30ac3792013-01-10 12:06:20 +0300999 sizeof(driver_info.os_device_name) - 1);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001000
Krishna Gudipati75332a72011-06-13 15:54:31 -07001001 /* FCS driver info init */
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001002 spin_lock_irqsave(&bfad->bfad_lock, flags);
1003 bfa_fcs_driver_info_init(&bfad->bfa_fcs, &driver_info);
Vijaya Mohan Guvvada3e0be2013-11-21 01:37:33 -08001004
1005 if (bfad->bfad_flags & BFAD_CFG_PPORT_DONE)
1006 bfa_fcs_update_cfg(&bfad->bfa_fcs);
1007 else
1008 bfa_fcs_init(&bfad->bfa_fcs);
1009
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001010 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
Krishna Gudipatie6714322010-03-03 17:44:02 -08001011
Vijaya Mohan Guvvada3e0be2013-11-21 01:37:33 -08001012 if (!(bfad->bfad_flags & BFAD_CFG_PPORT_DONE)) {
1013 retval = bfad_cfg_pport(bfad, BFA_LPORT_ROLE_FCP_IM);
1014 if (retval != BFA_STATUS_OK)
1015 return BFA_STATUS_FAILED;
1016 }
Krishna Gudipati75332a72011-06-13 15:54:31 -07001017
1018 /* Setup fc host fixed attribute if the lk supports */
1019 bfad_fc_host_init(bfad->pport.im_port);
Krishna Gudipatie6714322010-03-03 17:44:02 -08001020
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001021 /* BFAD level FC4 IM specific resource allocation */
1022 retval = bfad_im_probe(bfad);
1023 if (retval != BFA_STATUS_OK) {
1024 printk(KERN_WARNING "bfad_im_probe failed\n");
1025 if (bfa_sm_cmp_state(bfad, bfad_sm_initializing))
1026 bfa_sm_set_state(bfad, bfad_sm_failed);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001027 return BFA_STATUS_FAILED;
1028 } else
1029 bfad->bfad_flags |= BFAD_FC4_PROBE_DONE;
1030
Krishna Gudipatie6714322010-03-03 17:44:02 -08001031 bfad_drv_start(bfad);
1032
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001033 /* Complete pbc vport create */
1034 list_for_each_entry_safe(vport, vport_new, &bfad->pbc_vport_list,
1035 list_entry) {
Jing Huangd9883542010-07-08 19:46:26 -07001036 struct fc_vport_identifiers vid;
1037 struct fc_vport *fc_vport;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001038 char pwwn_buf[BFA_STRING_32];
Jing Huangd9883542010-07-08 19:46:26 -07001039
1040 memset(&vid, 0, sizeof(vid));
1041 vid.roles = FC_PORT_ROLE_FCP_INITIATOR;
1042 vid.vport_type = FC_PORTTYPE_NPIV;
1043 vid.disable = false;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001044 vid.node_name = wwn_to_u64((u8 *)
1045 (&((vport->fcs_vport).lport.port_cfg.nwwn)));
1046 vid.port_name = wwn_to_u64((u8 *)
1047 (&((vport->fcs_vport).lport.port_cfg.pwwn)));
Jing Huangd9883542010-07-08 19:46:26 -07001048 fc_vport = fc_vport_create(bfad->pport.im_port->shost, 0, &vid);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001049 if (!fc_vport) {
1050 wwn2str(pwwn_buf, vid.port_name);
Jing Huangd9883542010-07-08 19:46:26 -07001051 printk(KERN_WARNING "bfad%d: failed to create pbc vport"
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001052 " %s\n", bfad->inst_no, pwwn_buf);
1053 }
1054 list_del(&vport->list_entry);
1055 kfree(vport);
Jing Huangd9883542010-07-08 19:46:26 -07001056 }
1057
Krishna Gudipatie6714322010-03-03 17:44:02 -08001058 /*
1059 * If bfa_linkup_delay is set to -1 default; try to retrive the
Maggie Zhangf16a1752010-12-09 19:12:32 -08001060 * value using the bfad_get_linkup_delay(); else use the
Krishna Gudipatie6714322010-03-03 17:44:02 -08001061 * passed in module param value as the bfa_linkup_delay.
1062 */
1063 if (bfa_linkup_delay < 0) {
Maggie Zhangf16a1752010-12-09 19:12:32 -08001064 bfa_linkup_delay = bfad_get_linkup_delay(bfad);
1065 bfad_rport_online_wait(bfad);
Krishna Gudipatie6714322010-03-03 17:44:02 -08001066 bfa_linkup_delay = -1;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001067 } else
Maggie Zhangf16a1752010-12-09 19:12:32 -08001068 bfad_rport_online_wait(bfad);
Krishna Gudipatie6714322010-03-03 17:44:02 -08001069
Jing Huang88166242010-12-09 17:11:53 -08001070 BFA_LOG(KERN_INFO, bfad, bfa_log_level, "bfa device claimed\n");
Krishna Gudipatie6714322010-03-03 17:44:02 -08001071
1072 return BFA_STATUS_OK;
Krishna Gudipatie6714322010-03-03 17:44:02 -08001073}
1074
1075int
Jing Huangd9883542010-07-08 19:46:26 -07001076bfad_worker(void *ptr)
Krishna Gudipatie6714322010-03-03 17:44:02 -08001077{
Jiri Slaby8f7d3f02015-04-16 12:48:29 -07001078 struct bfad_s *bfad = ptr;
1079 unsigned long flags;
Krishna Gudipatie6714322010-03-03 17:44:02 -08001080
Jiri Slaby8f7d3f02015-04-16 12:48:29 -07001081 if (kthread_should_stop())
1082 return 0;
Krishna Gudipatie6714322010-03-03 17:44:02 -08001083
Jiri Slaby8f7d3f02015-04-16 12:48:29 -07001084 /* Send event BFAD_E_INIT_SUCCESS */
1085 bfa_sm_send_event(bfad, BFAD_E_INIT_SUCCESS);
Krishna Gudipatie6714322010-03-03 17:44:02 -08001086
Jiri Slaby8f7d3f02015-04-16 12:48:29 -07001087 spin_lock_irqsave(&bfad->bfad_lock, flags);
1088 bfad->bfad_tsk = NULL;
1089 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
Krishna Gudipatie6714322010-03-03 17:44:02 -08001090
1091 return 0;
1092}
1093
Jing Huang5fbe25c2010-10-18 17:17:23 -07001094/*
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001095 * BFA driver interrupt functions
1096 */
1097irqreturn_t
1098bfad_intx(int irq, void *dev_id)
1099{
1100 struct bfad_s *bfad = dev_id;
1101 struct list_head doneq;
1102 unsigned long flags;
1103 bfa_boolean_t rc;
1104
1105 spin_lock_irqsave(&bfad->bfad_lock, flags);
1106 rc = bfa_intx(&bfad->bfa);
1107 if (!rc) {
1108 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1109 return IRQ_NONE;
1110 }
1111
1112 bfa_comp_deq(&bfad->bfa, &doneq);
1113 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1114
1115 if (!list_empty(&doneq)) {
1116 bfa_comp_process(&bfad->bfa, &doneq);
1117
1118 spin_lock_irqsave(&bfad->bfad_lock, flags);
1119 bfa_comp_free(&bfad->bfa, &doneq);
1120 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001121 }
1122
1123 return IRQ_HANDLED;
1124
1125}
1126
1127static irqreturn_t
1128bfad_msix(int irq, void *dev_id)
1129{
1130 struct bfad_msix_s *vec = dev_id;
1131 struct bfad_s *bfad = vec->bfad;
1132 struct list_head doneq;
1133 unsigned long flags;
1134
1135 spin_lock_irqsave(&bfad->bfad_lock, flags);
1136
1137 bfa_msix(&bfad->bfa, vec->msix.entry);
1138 bfa_comp_deq(&bfad->bfa, &doneq);
1139 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1140
1141 if (!list_empty(&doneq)) {
1142 bfa_comp_process(&bfad->bfa, &doneq);
1143
1144 spin_lock_irqsave(&bfad->bfad_lock, flags);
1145 bfa_comp_free(&bfad->bfa, &doneq);
1146 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1147 }
1148
1149 return IRQ_HANDLED;
1150}
1151
Jing Huang5fbe25c2010-10-18 17:17:23 -07001152/*
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001153 * Initialize the MSIX entry table.
1154 */
1155static void
1156bfad_init_msix_entry(struct bfad_s *bfad, struct msix_entry *msix_entries,
1157 int mask, int max_bit)
1158{
1159 int i;
1160 int match = 0x00000001;
1161
1162 for (i = 0, bfad->nvec = 0; i < MAX_MSIX_ENTRY; i++) {
1163 if (mask & match) {
1164 bfad->msix_tab[bfad->nvec].msix.entry = i;
1165 bfad->msix_tab[bfad->nvec].bfad = bfad;
1166 msix_entries[bfad->nvec].entry = i;
1167 bfad->nvec++;
1168 }
1169
1170 match <<= 1;
1171 }
1172
1173}
1174
1175int
1176bfad_install_msix_handler(struct bfad_s *bfad)
1177{
1178 int i, error = 0;
1179
1180 for (i = 0; i < bfad->nvec; i++) {
1181 sprintf(bfad->msix_tab[i].name, "bfa-%s-%s",
1182 bfad->pci_name,
Krishna Gudipati11189202011-06-13 15:50:35 -07001183 ((bfa_asic_id_cb(bfad->hal_pcidev.device_id)) ?
1184 msix_name_cb[i] : msix_name_ct[i]));
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001185
1186 error = request_irq(bfad->msix_tab[i].msix.vector,
1187 (irq_handler_t) bfad_msix, 0,
1188 bfad->msix_tab[i].name, &bfad->msix_tab[i]);
1189 bfa_trc(bfad, i);
1190 bfa_trc(bfad, bfad->msix_tab[i].msix.vector);
1191 if (error) {
1192 int j;
1193
1194 for (j = 0; j < i; j++)
1195 free_irq(bfad->msix_tab[j].msix.vector,
1196 &bfad->msix_tab[j]);
1197
Krishna Gudipati61e62e22011-06-24 20:29:07 -07001198 bfad->bfad_flags &= ~BFAD_MSIX_ON;
1199 pci_disable_msix(bfad->pcidev);
1200
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001201 return 1;
1202 }
1203 }
1204
1205 return 0;
1206}
1207
Jing Huang5fbe25c2010-10-18 17:17:23 -07001208/*
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001209 * Setup MSIX based interrupt.
1210 */
1211int
1212bfad_setup_intr(struct bfad_s *bfad)
1213{
Alexander Gordeev8cb7f632014-07-16 20:05:07 +02001214 int error;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001215 u32 mask = 0, i, num_bit = 0, max_bit = 0;
1216 struct msix_entry msix_entries[MAX_MSIX_ENTRY];
1217 struct pci_dev *pdev = bfad->pcidev;
Krishna Gudipati10a07372011-06-24 20:23:38 -07001218 u16 reg;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001219
1220 /* Call BFA to get the msix map for this PCI function. */
1221 bfa_msix_getvecs(&bfad->bfa, &mask, &num_bit, &max_bit);
1222
1223 /* Set up the msix entry table */
1224 bfad_init_msix_entry(bfad, msix_entries, mask, max_bit);
1225
Krishna Gudipati11189202011-06-13 15:50:35 -07001226 if ((bfa_asic_id_ctc(pdev->device) && !msix_disable_ct) ||
1227 (bfa_asic_id_cb(pdev->device) && !msix_disable_cb)) {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001228
Alexander Gordeevb427d002014-07-16 20:05:08 +02001229 error = pci_enable_msix_exact(bfad->pcidev,
1230 msix_entries, bfad->nvec);
Alexander Gordeevcd69a092014-07-16 20:05:06 +02001231 /* In CT1 & CT2, try to allocate just one vector */
Alexander Gordeevb427d002014-07-16 20:05:08 +02001232 if (error == -ENOSPC && bfa_asic_id_ctc(pdev->device)) {
Alexander Gordeevcd69a092014-07-16 20:05:06 +02001233 printk(KERN_WARNING "bfa %s: trying one msix "
1234 "vector failed to allocate %d[%d]\n",
1235 bfad->pci_name, bfad->nvec, error);
1236 bfad->nvec = 1;
Alexander Gordeevb427d002014-07-16 20:05:08 +02001237 error = pci_enable_msix_exact(bfad->pcidev,
1238 msix_entries, 1);
Alexander Gordeevcd69a092014-07-16 20:05:06 +02001239 }
Krishna Gudipati881c1b32012-08-22 19:52:02 -07001240
Alexander Gordeevcd69a092014-07-16 20:05:06 +02001241 if (error) {
1242 printk(KERN_WARNING "bfad%d: "
Alexander Gordeevb427d002014-07-16 20:05:08 +02001243 "pci_enable_msix_exact failed (%d), "
Alexander Gordeevcd69a092014-07-16 20:05:06 +02001244 "use line based.\n",
1245 bfad->inst_no, error);
1246 goto line_based;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001247 }
1248
Krishna Gudipati10a07372011-06-24 20:23:38 -07001249 /* Disable INTX in MSI-X mode */
1250 pci_read_config_word(pdev, PCI_COMMAND, &reg);
1251
1252 if (!(reg & PCI_COMMAND_INTX_DISABLE))
1253 pci_write_config_word(pdev, PCI_COMMAND,
1254 reg | PCI_COMMAND_INTX_DISABLE);
1255
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001256 /* Save the vectors */
1257 for (i = 0; i < bfad->nvec; i++) {
1258 bfa_trc(bfad, msix_entries[i].vector);
1259 bfad->msix_tab[i].msix.vector = msix_entries[i].vector;
1260 }
1261
1262 bfa_msix_init(&bfad->bfa, bfad->nvec);
1263
1264 bfad->bfad_flags |= BFAD_MSIX_ON;
1265
Alexander Gordeev8cb7f632014-07-16 20:05:07 +02001266 return 0;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001267 }
1268
1269line_based:
Alexander Gordeev8cb7f632014-07-16 20:05:07 +02001270 error = request_irq(bfad->pcidev->irq, (irq_handler_t)bfad_intx,
1271 BFAD_IRQ_FLAGS, BFAD_DRIVER_NAME, bfad);
1272 if (error)
1273 return error;
1274
Krishna Gudipati61e62e22011-06-24 20:29:07 -07001275 bfad->bfad_flags |= BFAD_INTX_ON;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001276
Alexander Gordeev8cb7f632014-07-16 20:05:07 +02001277 return 0;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001278}
1279
1280void
1281bfad_remove_intr(struct bfad_s *bfad)
1282{
1283 int i;
1284
1285 if (bfad->bfad_flags & BFAD_MSIX_ON) {
1286 for (i = 0; i < bfad->nvec; i++)
1287 free_irq(bfad->msix_tab[i].msix.vector,
1288 &bfad->msix_tab[i]);
1289
1290 pci_disable_msix(bfad->pcidev);
1291 bfad->bfad_flags &= ~BFAD_MSIX_ON;
Krishna Gudipati61e62e22011-06-24 20:29:07 -07001292 } else if (bfad->bfad_flags & BFAD_INTX_ON) {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001293 free_irq(bfad->pcidev->irq, bfad);
1294 }
1295}
Jing Huang7725ccf2009-09-23 17:46:15 -07001296
Jing Huang5fbe25c2010-10-18 17:17:23 -07001297/*
Jing Huang7725ccf2009-09-23 17:46:15 -07001298 * PCI probe entry.
1299 */
1300int
1301bfad_pci_probe(struct pci_dev *pdev, const struct pci_device_id *pid)
1302{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001303 struct bfad_s *bfad;
Krishna Gudipati7826f302011-07-20 16:59:13 -07001304 int error = -ENODEV, retval, i;
Jing Huang7725ccf2009-09-23 17:46:15 -07001305
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001306 /* For single port cards - only claim function 0 */
Krishna Gudipati8b070b42011-06-13 15:52:40 -07001307 if ((pdev->device == BFA_PCI_DEVICE_ID_FC_8G1P) &&
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001308 (PCI_FUNC(pdev->devfn) != 0))
Jing Huang7725ccf2009-09-23 17:46:15 -07001309 return -ENODEV;
1310
Jing Huang7725ccf2009-09-23 17:46:15 -07001311 bfad = kzalloc(sizeof(struct bfad_s), GFP_KERNEL);
1312 if (!bfad) {
1313 error = -ENOMEM;
1314 goto out;
1315 }
1316
1317 bfad->trcmod = kzalloc(sizeof(struct bfa_trc_mod_s), GFP_KERNEL);
1318 if (!bfad->trcmod) {
1319 printk(KERN_WARNING "Error alloc trace buffer!\n");
1320 error = -ENOMEM;
1321 goto out_alloc_trace_failure;
1322 }
1323
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001324 /* TRACE INIT */
Jing Huang7725ccf2009-09-23 17:46:15 -07001325 bfa_trc_init(bfad->trcmod);
1326 bfa_trc(bfad, bfad_inst);
1327
Krishna Gudipati7826f302011-07-20 16:59:13 -07001328 /* AEN INIT */
1329 INIT_LIST_HEAD(&bfad->free_aen_q);
1330 INIT_LIST_HEAD(&bfad->active_aen_q);
1331 for (i = 0; i < BFA_AEN_MAX_ENTRY; i++)
1332 list_add_tail(&bfad->aen_list[i].qe, &bfad->free_aen_q);
1333
Jing Huang7725ccf2009-09-23 17:46:15 -07001334 if (!(bfad_load_fwimg(pdev))) {
Jing Huang7725ccf2009-09-23 17:46:15 -07001335 kfree(bfad->trcmod);
1336 goto out_alloc_trace_failure;
1337 }
1338
1339 retval = bfad_pci_init(pdev, bfad);
1340 if (retval) {
1341 printk(KERN_WARNING "bfad_pci_init failure!\n");
1342 error = retval;
1343 goto out_pci_init_failure;
1344 }
1345
1346 mutex_lock(&bfad_mutex);
1347 bfad->inst_no = bfad_inst++;
1348 list_add_tail(&bfad->list_entry, &bfad_list);
1349 mutex_unlock(&bfad_mutex);
1350
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001351 /* Initializing the state machine: State set to uninit */
1352 bfa_sm_set_state(bfad, bfad_sm_uninit);
1353
Jing Huang7725ccf2009-09-23 17:46:15 -07001354 spin_lock_init(&bfad->bfad_lock);
Kyle McMartin0b1017a2012-07-11 12:42:55 -04001355 spin_lock_init(&bfad->bfad_aen_spinlock);
1356
Jing Huang7725ccf2009-09-23 17:46:15 -07001357 pci_set_drvdata(pdev, bfad);
1358
1359 bfad->ref_count = 0;
1360 bfad->pport.bfad = bfad;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001361 INIT_LIST_HEAD(&bfad->pbc_vport_list);
Krishna Gudipati5b7db7a2011-12-20 18:58:32 -08001362 INIT_LIST_HEAD(&bfad->vport_list);
Krishna Gudipatie6714322010-03-03 17:44:02 -08001363
Krishna Gudipati7c38c052011-04-14 16:50:35 -07001364 /* Setup the debugfs node for this bfad */
1365 if (bfa_debugfs_enable)
1366 bfad_debugfs_init(&bfad->pport);
1367
Jing Huang7725ccf2009-09-23 17:46:15 -07001368 retval = bfad_drv_init(bfad);
1369 if (retval != BFA_STATUS_OK)
1370 goto out_drv_init_failure;
Jing Huang7725ccf2009-09-23 17:46:15 -07001371
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001372 bfa_sm_send_event(bfad, BFAD_E_CREATE);
Jing Huang7725ccf2009-09-23 17:46:15 -07001373
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001374 if (bfa_sm_cmp_state(bfad, bfad_sm_uninit))
1375 goto out_bfad_sm_failure;
Jing Huang7725ccf2009-09-23 17:46:15 -07001376
Jing Huang7725ccf2009-09-23 17:46:15 -07001377 return 0;
1378
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001379out_bfad_sm_failure:
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001380 bfad_hal_mem_release(bfad);
Jing Huang7725ccf2009-09-23 17:46:15 -07001381out_drv_init_failure:
Krishna Gudipati7c38c052011-04-14 16:50:35 -07001382 /* Remove the debugfs node for this bfad */
1383 kfree(bfad->regdata);
1384 bfad_debugfs_exit(&bfad->pport);
Jing Huang7725ccf2009-09-23 17:46:15 -07001385 mutex_lock(&bfad_mutex);
1386 bfad_inst--;
1387 list_del(&bfad->list_entry);
1388 mutex_unlock(&bfad_mutex);
1389 bfad_pci_uninit(pdev, bfad);
1390out_pci_init_failure:
1391 kfree(bfad->trcmod);
1392out_alloc_trace_failure:
1393 kfree(bfad);
1394out:
1395 return error;
1396}
1397
Jing Huang5fbe25c2010-10-18 17:17:23 -07001398/*
Jing Huang7725ccf2009-09-23 17:46:15 -07001399 * PCI remove entry.
1400 */
1401void
1402bfad_pci_remove(struct pci_dev *pdev)
1403{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001404 struct bfad_s *bfad = pci_get_drvdata(pdev);
1405 unsigned long flags;
Jing Huang7725ccf2009-09-23 17:46:15 -07001406
1407 bfa_trc(bfad, bfad->inst_no);
1408
Krishna Gudipatie6714322010-03-03 17:44:02 -08001409 spin_lock_irqsave(&bfad->bfad_lock, flags);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001410 if (bfad->bfad_tsk != NULL) {
1411 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
Krishna Gudipatie6714322010-03-03 17:44:02 -08001412 kthread_stop(bfad->bfad_tsk);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001413 } else {
Jing Huang7725ccf2009-09-23 17:46:15 -07001414 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
Jing Huang7725ccf2009-09-23 17:46:15 -07001415 }
1416
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001417 /* Send Event BFAD_E_STOP */
1418 bfa_sm_send_event(bfad, BFAD_E_STOP);
Jing Huang7725ccf2009-09-23 17:46:15 -07001419
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001420 /* Driver detach and dealloc mem */
Jing Huang7725ccf2009-09-23 17:46:15 -07001421 spin_lock_irqsave(&bfad->bfad_lock, flags);
1422 bfa_detach(&bfad->bfa);
1423 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1424 bfad_hal_mem_release(bfad);
Jing Huang7725ccf2009-09-23 17:46:15 -07001425
Krishna Gudipati7c38c052011-04-14 16:50:35 -07001426 /* Remove the debugfs node for this bfad */
1427 kfree(bfad->regdata);
1428 bfad_debugfs_exit(&bfad->pport);
1429
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001430 /* Cleaning the BFAD instance */
Jing Huang7725ccf2009-09-23 17:46:15 -07001431 mutex_lock(&bfad_mutex);
1432 bfad_inst--;
1433 list_del(&bfad->list_entry);
1434 mutex_unlock(&bfad_mutex);
1435 bfad_pci_uninit(pdev, bfad);
1436
1437 kfree(bfad->trcmod);
1438 kfree(bfad);
1439}
1440
Krishna Gudipati881c1b32012-08-22 19:52:02 -07001441/*
1442 * PCI Error Recovery entry, error detected.
1443 */
1444static pci_ers_result_t
1445bfad_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
1446{
1447 struct bfad_s *bfad = pci_get_drvdata(pdev);
1448 unsigned long flags;
1449 pci_ers_result_t ret = PCI_ERS_RESULT_NONE;
1450
1451 dev_printk(KERN_ERR, &pdev->dev,
1452 "error detected state: %d - flags: 0x%x\n",
1453 state, bfad->bfad_flags);
1454
1455 switch (state) {
1456 case pci_channel_io_normal: /* non-fatal error */
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 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1462 del_timer_sync(&bfad->hal_tmo);
1463 ret = PCI_ERS_RESULT_CAN_RECOVER;
1464 break;
1465 case pci_channel_io_frozen: /* fatal error */
1466 init_completion(&bfad->comp);
1467 spin_lock_irqsave(&bfad->bfad_lock, flags);
1468 bfad->bfad_flags |= BFAD_EEH_BUSY;
1469 /* Suspend/fail all bfa operations */
1470 bfa_ioc_suspend(&bfad->bfa.ioc);
1471 bfa_fcs_stop(&bfad->bfa_fcs);
1472 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1473 wait_for_completion(&bfad->comp);
1474
1475 bfad_remove_intr(bfad);
1476 del_timer_sync(&bfad->hal_tmo);
1477 pci_disable_device(pdev);
1478 ret = PCI_ERS_RESULT_NEED_RESET;
1479 break;
1480 case pci_channel_io_perm_failure: /* PCI Card is DEAD */
1481 spin_lock_irqsave(&bfad->bfad_lock, flags);
1482 bfad->bfad_flags |= BFAD_EEH_BUSY |
1483 BFAD_EEH_PCI_CHANNEL_IO_PERM_FAILURE;
1484 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1485
1486 /* If the error_detected handler is called with the reason
1487 * pci_channel_io_perm_failure - it will subsequently call
1488 * pci_remove() entry point to remove the pci device from the
1489 * system - So defer the cleanup to pci_remove(); cleaning up
1490 * here causes inconsistent state during pci_remove().
1491 */
1492 ret = PCI_ERS_RESULT_DISCONNECT;
1493 break;
1494 default:
1495 WARN_ON(1);
1496 }
1497
1498 return ret;
1499}
1500
1501int
1502restart_bfa(struct bfad_s *bfad)
1503{
1504 unsigned long flags;
1505 struct pci_dev *pdev = bfad->pcidev;
1506
1507 bfa_attach(&bfad->bfa, bfad, &bfad->ioc_cfg,
1508 &bfad->meminfo, &bfad->hal_pcidev);
1509
1510 /* Enable Interrupt and wait bfa_init completion */
1511 if (bfad_setup_intr(bfad)) {
1512 dev_printk(KERN_WARNING, &pdev->dev,
1513 "%s: bfad_setup_intr failed\n", bfad->pci_name);
Vijaya Mohan Guvvada3e0be2013-11-21 01:37:33 -08001514 bfa_sm_send_event(bfad, BFAD_E_INIT_FAILED);
Krishna Gudipati881c1b32012-08-22 19:52:02 -07001515 return -1;
1516 }
1517
1518 init_completion(&bfad->comp);
1519 spin_lock_irqsave(&bfad->bfad_lock, flags);
1520 bfa_iocfc_init(&bfad->bfa);
1521 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1522
1523 /* Set up interrupt handler for each vectors */
1524 if ((bfad->bfad_flags & BFAD_MSIX_ON) &&
1525 bfad_install_msix_handler(bfad))
1526 dev_printk(KERN_WARNING, &pdev->dev,
1527 "%s: install_msix failed.\n", bfad->pci_name);
1528
1529 bfad_init_timer(bfad);
1530 wait_for_completion(&bfad->comp);
1531 bfad_drv_start(bfad);
1532
1533 return 0;
1534}
1535
1536/*
1537 * PCI Error Recovery entry, re-initialize the chip.
1538 */
1539static pci_ers_result_t
1540bfad_pci_slot_reset(struct pci_dev *pdev)
1541{
1542 struct bfad_s *bfad = pci_get_drvdata(pdev);
1543 u8 byte;
1544
1545 dev_printk(KERN_ERR, &pdev->dev,
1546 "bfad_pci_slot_reset flags: 0x%x\n", bfad->bfad_flags);
1547
1548 if (pci_enable_device(pdev)) {
1549 dev_printk(KERN_ERR, &pdev->dev, "Cannot re-enable "
1550 "PCI device after reset.\n");
1551 return PCI_ERS_RESULT_DISCONNECT;
1552 }
1553
1554 pci_restore_state(pdev);
1555
1556 /*
1557 * Read some byte (e.g. DMA max. payload size which can't
1558 * be 0xff any time) to make sure - we did not hit another PCI error
1559 * in the middle of recovery. If we did, then declare permanent failure.
1560 */
1561 pci_read_config_byte(pdev, 0x68, &byte);
1562 if (byte == 0xff) {
1563 dev_printk(KERN_ERR, &pdev->dev,
1564 "slot_reset failed ... got another PCI error !\n");
1565 goto out_disable_device;
1566 }
1567
1568 pci_save_state(pdev);
1569 pci_set_master(pdev);
1570
1571 if (pci_set_dma_mask(bfad->pcidev, DMA_BIT_MASK(64)) != 0)
1572 if (pci_set_dma_mask(bfad->pcidev, DMA_BIT_MASK(32)) != 0)
1573 goto out_disable_device;
1574
1575 pci_cleanup_aer_uncorrect_error_status(pdev);
1576
1577 if (restart_bfa(bfad) == -1)
1578 goto out_disable_device;
1579
1580 pci_enable_pcie_error_reporting(pdev);
1581 dev_printk(KERN_WARNING, &pdev->dev,
1582 "slot_reset completed flags: 0x%x!\n", bfad->bfad_flags);
1583
1584 return PCI_ERS_RESULT_RECOVERED;
1585
1586out_disable_device:
1587 pci_disable_device(pdev);
1588 return PCI_ERS_RESULT_DISCONNECT;
1589}
1590
1591static pci_ers_result_t
1592bfad_pci_mmio_enabled(struct pci_dev *pdev)
1593{
1594 unsigned long flags;
1595 struct bfad_s *bfad = pci_get_drvdata(pdev);
1596
1597 dev_printk(KERN_INFO, &pdev->dev, "mmio_enabled\n");
1598
1599 /* Fetch FW diagnostic information */
1600 bfa_ioc_debug_save_ftrc(&bfad->bfa.ioc);
1601
1602 /* Cancel all pending IOs */
1603 spin_lock_irqsave(&bfad->bfad_lock, flags);
1604 init_completion(&bfad->comp);
1605 bfa_fcs_stop(&bfad->bfa_fcs);
1606 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1607 wait_for_completion(&bfad->comp);
1608
1609 bfad_remove_intr(bfad);
1610 del_timer_sync(&bfad->hal_tmo);
1611 pci_disable_device(pdev);
1612
1613 return PCI_ERS_RESULT_NEED_RESET;
1614}
1615
1616static void
1617bfad_pci_resume(struct pci_dev *pdev)
1618{
1619 unsigned long flags;
1620 struct bfad_s *bfad = pci_get_drvdata(pdev);
1621
1622 dev_printk(KERN_WARNING, &pdev->dev, "resume\n");
1623
1624 /* wait until the link is online */
1625 bfad_rport_online_wait(bfad);
1626
1627 spin_lock_irqsave(&bfad->bfad_lock, flags);
1628 bfad->bfad_flags &= ~BFAD_EEH_BUSY;
1629 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1630}
1631
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001632struct pci_device_id bfad_id_table[] = {
Jing Huang7725ccf2009-09-23 17:46:15 -07001633 {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001634 .vendor = BFA_PCI_VENDOR_ID_BROCADE,
1635 .device = BFA_PCI_DEVICE_ID_FC_8G2P,
1636 .subvendor = PCI_ANY_ID,
1637 .subdevice = PCI_ANY_ID,
1638 },
Jing Huang7725ccf2009-09-23 17:46:15 -07001639 {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001640 .vendor = BFA_PCI_VENDOR_ID_BROCADE,
1641 .device = BFA_PCI_DEVICE_ID_FC_8G1P,
1642 .subvendor = PCI_ANY_ID,
1643 .subdevice = PCI_ANY_ID,
1644 },
Jing Huang7725ccf2009-09-23 17:46:15 -07001645 {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001646 .vendor = BFA_PCI_VENDOR_ID_BROCADE,
1647 .device = BFA_PCI_DEVICE_ID_CT,
1648 .subvendor = PCI_ANY_ID,
1649 .subdevice = PCI_ANY_ID,
1650 .class = (PCI_CLASS_SERIAL_FIBER << 8),
1651 .class_mask = ~0,
1652 },
Jing Huang293f82d2010-07-08 19:45:20 -07001653 {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001654 .vendor = BFA_PCI_VENDOR_ID_BROCADE,
1655 .device = BFA_PCI_DEVICE_ID_CT_FC,
1656 .subvendor = PCI_ANY_ID,
1657 .subdevice = PCI_ANY_ID,
1658 .class = (PCI_CLASS_SERIAL_FIBER << 8),
1659 .class_mask = ~0,
Jing Huang293f82d2010-07-08 19:45:20 -07001660 },
Krishna Gudipati11189202011-06-13 15:50:35 -07001661 {
1662 .vendor = BFA_PCI_VENDOR_ID_BROCADE,
1663 .device = BFA_PCI_DEVICE_ID_CT2,
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
Vijaya Mohan Guvvaf9c867b2013-05-13 02:33:33 -07001670 {
1671 .vendor = BFA_PCI_VENDOR_ID_BROCADE,
1672 .device = BFA_PCI_DEVICE_ID_CT2_QUAD,
1673 .subvendor = PCI_ANY_ID,
1674 .subdevice = PCI_ANY_ID,
1675 .class = (PCI_CLASS_SERIAL_FIBER << 8),
1676 .class_mask = ~0,
1677 },
Jing Huang7725ccf2009-09-23 17:46:15 -07001678 {0, 0},
1679};
1680
1681MODULE_DEVICE_TABLE(pci, bfad_id_table);
1682
Krishna Gudipati881c1b32012-08-22 19:52:02 -07001683/*
1684 * PCI error recovery handlers.
1685 */
1686static struct pci_error_handlers bfad_err_handler = {
1687 .error_detected = bfad_pci_error_detected,
1688 .slot_reset = bfad_pci_slot_reset,
1689 .mmio_enabled = bfad_pci_mmio_enabled,
1690 .resume = bfad_pci_resume,
1691};
1692
Jing Huang7725ccf2009-09-23 17:46:15 -07001693static struct pci_driver bfad_pci_driver = {
1694 .name = BFAD_DRIVER_NAME,
1695 .id_table = bfad_id_table,
1696 .probe = bfad_pci_probe,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08001697 .remove = bfad_pci_remove,
Krishna Gudipati881c1b32012-08-22 19:52:02 -07001698 .err_handler = &bfad_err_handler,
Jing Huang7725ccf2009-09-23 17:46:15 -07001699};
1700
Jing Huang5fbe25c2010-10-18 17:17:23 -07001701/*
Jing Huang7725ccf2009-09-23 17:46:15 -07001702 * Driver module init.
1703 */
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001704static int __init
Jing Huang7725ccf2009-09-23 17:46:15 -07001705bfad_init(void)
1706{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001707 int error = 0;
Jing Huang7725ccf2009-09-23 17:46:15 -07001708
Anil Gurumurthy31e1d562015-11-26 03:54:46 -05001709 pr_info("QLogic BR-series BFA FC/FCOE SCSI driver - version: %s\n",
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001710 BFAD_DRIVER_VERSION);
Jing Huang7725ccf2009-09-23 17:46:15 -07001711
1712 if (num_sgpgs > 0)
1713 num_sgpgs_parm = num_sgpgs;
1714
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001715 error = bfad_im_module_init();
Jing Huang7725ccf2009-09-23 17:46:15 -07001716 if (error) {
1717 error = -ENOMEM;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001718 printk(KERN_WARNING "bfad_im_module_init failure\n");
Jing Huang7725ccf2009-09-23 17:46:15 -07001719 goto ext;
1720 }
1721
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001722 if (strcmp(FCPI_NAME, " fcpim") == 0)
1723 supported_fc4s |= BFA_LPORT_ROLE_FCP_IM;
Jing Huang7725ccf2009-09-23 17:46:15 -07001724
Maggie Zhangf7f738122010-12-09 19:08:43 -08001725 bfa_auto_recover = ioc_auto_recover;
Jing Huang7725ccf2009-09-23 17:46:15 -07001726 bfa_fcs_rport_set_del_timeout(rport_del_timeout);
Krishna Gudipati61ba4392012-08-22 19:52:58 -07001727 bfa_fcs_rport_set_max_logins(max_rport_logins);
Jing Huang7725ccf2009-09-23 17:46:15 -07001728
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001729 error = pci_register_driver(&bfad_pci_driver);
Jing Huang7725ccf2009-09-23 17:46:15 -07001730 if (error) {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001731 printk(KERN_WARNING "pci_register_driver failure\n");
Jing Huang7725ccf2009-09-23 17:46:15 -07001732 goto ext;
1733 }
1734
1735 return 0;
1736
1737ext:
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001738 bfad_im_module_exit();
Jing Huang7725ccf2009-09-23 17:46:15 -07001739 return error;
1740}
1741
Jing Huang5fbe25c2010-10-18 17:17:23 -07001742/*
Jing Huang7725ccf2009-09-23 17:46:15 -07001743 * Driver module exit.
1744 */
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001745static void __exit
Jing Huang7725ccf2009-09-23 17:46:15 -07001746bfad_exit(void)
1747{
1748 pci_unregister_driver(&bfad_pci_driver);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001749 bfad_im_module_exit();
Jing Huang7725ccf2009-09-23 17:46:15 -07001750 bfad_free_fwimg();
1751}
1752
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001753/* Firmware handling */
Jing Huang61338a02011-04-13 11:44:03 -07001754static void
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001755bfad_read_firmware(struct pci_dev *pdev, u32 **bfi_image,
1756 u32 *bfi_image_size, char *fw_name)
1757{
1758 const struct firmware *fw;
1759
1760 if (request_firmware(&fw, fw_name, &pdev->dev)) {
1761 printk(KERN_ALERT "Can't locate firmware %s\n", fw_name);
Jing Huang61338a02011-04-13 11:44:03 -07001762 *bfi_image = NULL;
1763 goto out;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001764 }
1765
1766 *bfi_image = vmalloc(fw->size);
1767 if (NULL == *bfi_image) {
1768 printk(KERN_ALERT "Fail to allocate buffer for fw image "
1769 "size=%x!\n", (u32) fw->size);
Jing Huang61338a02011-04-13 11:44:03 -07001770 goto out;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001771 }
1772
1773 memcpy(*bfi_image, fw->data, fw->size);
1774 *bfi_image_size = fw->size/sizeof(u32);
Jing Huang61338a02011-04-13 11:44:03 -07001775out:
1776 release_firmware(fw);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001777}
1778
Jing Huang61338a02011-04-13 11:44:03 -07001779static u32 *
1780bfad_load_fwimg(struct pci_dev *pdev)
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001781{
Vijaya Mohan Guvvadcaf9ae2013-12-04 05:43:58 -08001782 if (bfa_asic_id_ct2(pdev->device)) {
Krishna Gudipati11189202011-06-13 15:50:35 -07001783 if (bfi_image_ct2_size == 0)
1784 bfad_read_firmware(pdev, &bfi_image_ct2,
1785 &bfi_image_ct2_size, BFAD_FW_FILE_CT2);
1786 return bfi_image_ct2;
1787 } else if (bfa_asic_id_ct(pdev->device)) {
1788 if (bfi_image_ct_size == 0)
1789 bfad_read_firmware(pdev, &bfi_image_ct,
1790 &bfi_image_ct_size, BFAD_FW_FILE_CT);
1791 return bfi_image_ct;
Vijaya Mohan Guvvadcaf9ae2013-12-04 05:43:58 -08001792 } else if (bfa_asic_id_cb(pdev->device)) {
Krishna Gudipati11189202011-06-13 15:50:35 -07001793 if (bfi_image_cb_size == 0)
1794 bfad_read_firmware(pdev, &bfi_image_cb,
1795 &bfi_image_cb_size, BFAD_FW_FILE_CB);
1796 return bfi_image_cb;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001797 }
Vijaya Mohan Guvvadcaf9ae2013-12-04 05:43:58 -08001798
1799 return NULL;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001800}
Jing Huang7725ccf2009-09-23 17:46:15 -07001801
Jing Huang61338a02011-04-13 11:44:03 -07001802static void
1803bfad_free_fwimg(void)
1804{
Krishna Gudipati11189202011-06-13 15:50:35 -07001805 if (bfi_image_ct2_size && bfi_image_ct2)
1806 vfree(bfi_image_ct2);
1807 if (bfi_image_ct_size && bfi_image_ct)
1808 vfree(bfi_image_ct);
1809 if (bfi_image_cb_size && bfi_image_cb)
1810 vfree(bfi_image_cb);
Jing Huang61338a02011-04-13 11:44:03 -07001811}
1812
Jing Huang7725ccf2009-09-23 17:46:15 -07001813module_init(bfad_init);
1814module_exit(bfad_exit);
1815MODULE_LICENSE("GPL");
Anil Gurumurthy31e1d562015-11-26 03:54:46 -05001816MODULE_DESCRIPTION("QLogic BR-series Fibre Channel HBA Driver" BFAD_PROTO_NAME);
1817MODULE_AUTHOR("QLogic Corporation");
Jing Huang7725ccf2009-09-23 17:46:15 -07001818MODULE_VERSION(BFAD_DRIVER_VERSION);