blob: 2c8f0c713076251bcc2e8e17e2018975e765aa00 [file] [log] [blame]
Jing Huang7725ccf2009-09-23 17:46:15 -07001/*
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07002 * Copyright (c) 2005-2010 Brocade Communications Systems, Inc.
Jing Huang7725ccf2009-09-23 17:46:15 -07003 * All rights reserved
4 * www.brocade.com
5 *
6 * Linux driver for Brocade Fibre Channel Host Bus Adapter.
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License (GPL) Version 2 as
10 * published by the Free Software Foundation
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 */
17
Jing Huang5fbe25c2010-10-18 17:17:23 -070018/*
Jing Huang7725ccf2009-09-23 17:46:15 -070019 * bfad.c Linux driver PCI interface module.
20 */
Jing Huang7725ccf2009-09-23 17:46:15 -070021#include <linux/module.h>
Krishna Gudipatie6714322010-03-03 17:44:02 -080022#include <linux/kthread.h>
Krishna Gudipatia36c61f2010-09-15 11:50:55 -070023#include <linux/errno.h>
24#include <linux/sched.h>
25#include <linux/init.h>
26#include <linux/fs.h>
27#include <linux/pci.h>
28#include <linux/firmware.h>
29#include <asm/uaccess.h>
30#include <asm/fcntl.h>
31
Jing Huang7725ccf2009-09-23 17:46:15 -070032#include "bfad_drv.h"
33#include "bfad_im.h"
Krishna Gudipatia36c61f2010-09-15 11:50:55 -070034#include "bfa_fcs.h"
Krishna Gudipatia36c61f2010-09-15 11:50:55 -070035#include "bfa_defs.h"
36#include "bfa.h"
Jing Huang7725ccf2009-09-23 17:46:15 -070037
38BFA_TRC_FILE(LDRV, BFAD);
Jing Huang42b426e2010-03-19 11:07:09 -070039DEFINE_MUTEX(bfad_mutex);
Jing Huang7725ccf2009-09-23 17:46:15 -070040LIST_HEAD(bfad_list);
Jing Huang7725ccf2009-09-23 17:46:15 -070041
Krishna Gudipatia36c61f2010-09-15 11:50:55 -070042static int bfad_inst;
43static int num_sgpgs_parm;
44int supported_fc4s;
45char *host_name, *os_name, *os_patch;
46int num_rports, num_ios, num_tms;
47int num_fcxps, num_ufbufs;
48int reqq_size, rspq_size, num_sgpgs;
49int rport_del_timeout = BFA_FCS_RPORT_DEF_DEL_TIMEOUT;
50int bfa_lun_queue_depth = BFAD_LUN_QUEUE_DEPTH;
51int bfa_io_max_sge = BFAD_IO_MAX_SGE;
Jing Huang88166242010-12-09 17:11:53 -080052int bfa_log_level = 3; /* WARNING log level */
Krishna Gudipatia36c61f2010-09-15 11:50:55 -070053int ioc_auto_recover = BFA_TRUE;
54int bfa_linkup_delay = -1;
55int fdmi_enable = BFA_TRUE;
56int pcie_max_read_reqsz;
Jing Huangab2a9ba2010-07-08 20:02:55 -070057int bfa_debugfs_enable = 1;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -070058int msix_disable_cb = 0, msix_disable_ct = 0;
Krishna Gudipati61e62e22011-06-24 20:29:07 -070059int max_xfer_size = BFAD_MAX_SECTORS >> 1;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -070060
Jing Huang61338a02011-04-13 11:44:03 -070061/* Firmware releated */
Krishna Gudipati11189202011-06-13 15:50:35 -070062u32 bfi_image_cb_size, bfi_image_ct_size, bfi_image_ct2_size;
63u32 *bfi_image_cb, *bfi_image_ct, *bfi_image_ct2;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -070064
Krishna Gudipati11189202011-06-13 15:50:35 -070065#define BFAD_FW_FILE_CB "cbfw.bin"
66#define BFAD_FW_FILE_CT "ctfw.bin"
67#define BFAD_FW_FILE_CT2 "ct2fw.bin"
Jing Huang61338a02011-04-13 11:44:03 -070068
69static u32 *bfad_load_fwimg(struct pci_dev *pdev);
70static void bfad_free_fwimg(void);
71static void bfad_read_firmware(struct pci_dev *pdev, u32 **bfi_image,
72 u32 *bfi_image_size, char *fw_name);
73
Maggie52f94b62010-11-29 18:21:32 -080074static const char *msix_name_ct[] = {
Krishna Gudipati11189202011-06-13 15:50:35 -070075 "ctrl",
Krishna Gudipatia36c61f2010-09-15 11:50:55 -070076 "cpe0", "cpe1", "cpe2", "cpe3",
Krishna Gudipati11189202011-06-13 15:50:35 -070077 "rme0", "rme1", "rme2", "rme3" };
Krishna Gudipatia36c61f2010-09-15 11:50:55 -070078
Maggie52f94b62010-11-29 18:21:32 -080079static const char *msix_name_cb[] = {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -070080 "cpe0", "cpe1", "cpe2", "cpe3",
81 "rme0", "rme1", "rme2", "rme3",
82 "eemc", "elpu0", "elpu1", "epss", "mlpu" };
83
Krishna Gudipati11189202011-06-13 15:50:35 -070084MODULE_FIRMWARE(BFAD_FW_FILE_CB);
85MODULE_FIRMWARE(BFAD_FW_FILE_CT);
86MODULE_FIRMWARE(BFAD_FW_FILE_CT2);
Jing Huang7725ccf2009-09-23 17:46:15 -070087
88module_param(os_name, charp, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -070089MODULE_PARM_DESC(os_name, "OS name of the hba host machine");
Jing Huang7725ccf2009-09-23 17:46:15 -070090module_param(os_patch, charp, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -070091MODULE_PARM_DESC(os_patch, "OS patch level of the hba host machine");
Jing Huang7725ccf2009-09-23 17:46:15 -070092module_param(host_name, charp, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -070093MODULE_PARM_DESC(host_name, "Hostname of the hba host machine");
Jing Huang7725ccf2009-09-23 17:46:15 -070094module_param(num_rports, int, S_IRUGO | S_IWUSR);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -070095MODULE_PARM_DESC(num_rports, "Max number of rports supported per port "
96 "(physical/logical), default=1024");
Jing Huang7725ccf2009-09-23 17:46:15 -070097module_param(num_ios, int, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -070098MODULE_PARM_DESC(num_ios, "Max number of ioim requests, default=2000");
Jing Huang7725ccf2009-09-23 17:46:15 -070099module_param(num_tms, int, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -0700100MODULE_PARM_DESC(num_tms, "Max number of task im requests, default=128");
Jing Huang7725ccf2009-09-23 17:46:15 -0700101module_param(num_fcxps, int, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -0700102MODULE_PARM_DESC(num_fcxps, "Max number of fcxp requests, default=64");
Jing Huang7725ccf2009-09-23 17:46:15 -0700103module_param(num_ufbufs, int, S_IRUGO | S_IWUSR);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700104MODULE_PARM_DESC(num_ufbufs, "Max number of unsolicited frame "
105 "buffers, default=64");
Jing Huang7725ccf2009-09-23 17:46:15 -0700106module_param(reqq_size, int, S_IRUGO | S_IWUSR);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700107MODULE_PARM_DESC(reqq_size, "Max number of request queue elements, "
108 "default=256");
Jing Huang7725ccf2009-09-23 17:46:15 -0700109module_param(rspq_size, int, S_IRUGO | S_IWUSR);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700110MODULE_PARM_DESC(rspq_size, "Max number of response queue elements, "
111 "default=64");
Jing Huang7725ccf2009-09-23 17:46:15 -0700112module_param(num_sgpgs, int, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -0700113MODULE_PARM_DESC(num_sgpgs, "Number of scatter/gather pages, default=2048");
Jing Huang7725ccf2009-09-23 17:46:15 -0700114module_param(rport_del_timeout, int, S_IRUGO | S_IWUSR);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700115MODULE_PARM_DESC(rport_del_timeout, "Rport delete timeout, default=90 secs, "
116 "Range[>0]");
Jing Huang7725ccf2009-09-23 17:46:15 -0700117module_param(bfa_lun_queue_depth, int, S_IRUGO | S_IWUSR);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700118MODULE_PARM_DESC(bfa_lun_queue_depth, "Lun queue depth, default=32, Range[>0]");
Jing Huang7725ccf2009-09-23 17:46:15 -0700119module_param(bfa_io_max_sge, int, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -0700120MODULE_PARM_DESC(bfa_io_max_sge, "Max io scatter/gather elements, default=255");
Jing Huang88166242010-12-09 17:11:53 -0800121module_param(bfa_log_level, int, S_IRUGO | S_IWUSR);
122MODULE_PARM_DESC(bfa_log_level, "Driver log level, default=3, "
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700123 "Range[Critical:1|Error:2|Warning:3|Info:4]");
Jing Huang7725ccf2009-09-23 17:46:15 -0700124module_param(ioc_auto_recover, int, S_IRUGO | S_IWUSR);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700125MODULE_PARM_DESC(ioc_auto_recover, "IOC auto recovery, default=1, "
126 "Range[off:0|on:1]");
Jing Huang7725ccf2009-09-23 17:46:15 -0700127module_param(bfa_linkup_delay, int, S_IRUGO | S_IWUSR);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700128MODULE_PARM_DESC(bfa_linkup_delay, "Link up delay, default=30 secs for "
129 "boot port. Otherwise 10 secs in RHEL4 & 0 for "
130 "[RHEL5, SLES10, ESX40] Range[>0]");
131module_param(msix_disable_cb, int, S_IRUGO | S_IWUSR);
132MODULE_PARM_DESC(msix_disable_cb, "Disable Message Signaled Interrupts "
133 "for Brocade-415/425/815/825 cards, default=0, "
134 " Range[false:0|true:1]");
135module_param(msix_disable_ct, int, S_IRUGO | S_IWUSR);
136MODULE_PARM_DESC(msix_disable_ct, "Disable Message Signaled Interrupts "
137 "if possible for Brocade-1010/1020/804/1007/902/1741 "
138 "cards, default=0, Range[false:0|true:1]");
Jing Huang604158ad2010-07-08 19:59:49 -0700139module_param(fdmi_enable, int, S_IRUGO | S_IWUSR);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700140MODULE_PARM_DESC(fdmi_enable, "Enables fdmi registration, default=1, "
141 "Range[false:0|true:1]");
142module_param(pcie_max_read_reqsz, int, S_IRUGO | S_IWUSR);
143MODULE_PARM_DESC(pcie_max_read_reqsz, "PCIe max read request size, default=0 "
144 "(use system setting), Range[128|256|512|1024|2048|4096]");
Jing Huangab2a9ba2010-07-08 20:02:55 -0700145module_param(bfa_debugfs_enable, int, S_IRUGO | S_IWUSR);
146MODULE_PARM_DESC(bfa_debugfs_enable, "Enables debugfs feature, default=1,"
147 " Range[false:0|true:1]");
Krishna Gudipati61e62e22011-06-24 20:29:07 -0700148module_param(max_xfer_size, int, S_IRUGO | S_IWUSR);
149MODULE_PARM_DESC(max_xfer_size, "default=32MB,"
150 " Range[64k|128k|256k|512k|1024k|2048k]");
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;
204
205 bfa_trc(bfad, event);
206
207 switch (event) {
208 case BFAD_E_INIT:
209 bfa_sm_set_state(bfad, bfad_sm_initializing);
210
211 init_completion(&bfad->comp);
212
213 /* Enable Interrupt and wait bfa_init completion */
214 if (bfad_setup_intr(bfad)) {
215 printk(KERN_WARNING "bfad%d: bfad_setup_intr failed\n",
216 bfad->inst_no);
217 bfa_sm_send_event(bfad, BFAD_E_INTR_INIT_FAILED);
218 break;
219 }
220
221 spin_lock_irqsave(&bfad->bfad_lock, flags);
Maggie Zhangf7f738122010-12-09 19:08:43 -0800222 bfa_iocfc_init(&bfad->bfa);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700223 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
224
225 /* Set up interrupt handler for each vectors */
226 if ((bfad->bfad_flags & BFAD_MSIX_ON) &&
227 bfad_install_msix_handler(bfad)) {
228 printk(KERN_WARNING "%s: install_msix failed, bfad%d\n",
229 __func__, bfad->inst_no);
230 }
231
232 bfad_init_timer(bfad);
233
234 wait_for_completion(&bfad->comp);
235
236 if ((bfad->bfad_flags & BFAD_HAL_INIT_DONE)) {
237 bfa_sm_send_event(bfad, BFAD_E_INIT_SUCCESS);
238 } else {
Krishna Gudipati7c38c052011-04-14 16:50:35 -0700239 printk(KERN_WARNING
240 "bfa %s: bfa init failed\n",
241 bfad->pci_name);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700242 bfad->bfad_flags |= BFAD_HAL_INIT_FAIL;
243 bfa_sm_send_event(bfad, BFAD_E_INIT_FAILED);
244 }
245
246 break;
247
248 case BFAD_E_KTHREAD_CREATE_FAILED:
249 bfa_sm_set_state(bfad, bfad_sm_uninit);
250 break;
251
252 default:
253 bfa_sm_fault(bfad, event);
254 }
Jing Huang7725ccf2009-09-23 17:46:15 -0700255}
256
257static void
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700258bfad_sm_initializing(struct bfad_s *bfad, enum bfad_sm_event event)
Jing Huang7725ccf2009-09-23 17:46:15 -0700259{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700260 int retval;
261 unsigned long flags;
262
263 bfa_trc(bfad, event);
264
265 switch (event) {
266 case BFAD_E_INIT_SUCCESS:
267 kthread_stop(bfad->bfad_tsk);
268 spin_lock_irqsave(&bfad->bfad_lock, flags);
269 bfad->bfad_tsk = NULL;
270 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
271
272 retval = bfad_start_ops(bfad);
273 if (retval != BFA_STATUS_OK)
274 break;
275 bfa_sm_set_state(bfad, bfad_sm_operational);
276 break;
277
278 case BFAD_E_INTR_INIT_FAILED:
279 bfa_sm_set_state(bfad, bfad_sm_uninit);
280 kthread_stop(bfad->bfad_tsk);
281 spin_lock_irqsave(&bfad->bfad_lock, flags);
282 bfad->bfad_tsk = NULL;
283 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
284 break;
285
286 case BFAD_E_INIT_FAILED:
287 bfa_sm_set_state(bfad, bfad_sm_failed);
288 break;
289 default:
290 bfa_sm_fault(bfad, event);
291 }
Jing Huang7725ccf2009-09-23 17:46:15 -0700292}
293
294static void
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700295bfad_sm_failed(struct bfad_s *bfad, enum bfad_sm_event event)
Jing Huang7725ccf2009-09-23 17:46:15 -0700296{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700297 int retval;
Jing Huang7725ccf2009-09-23 17:46:15 -0700298
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700299 bfa_trc(bfad, event);
Jing Huang7725ccf2009-09-23 17:46:15 -0700300
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700301 switch (event) {
302 case BFAD_E_INIT_SUCCESS:
303 retval = bfad_start_ops(bfad);
304 if (retval != BFA_STATUS_OK)
305 break;
306 bfa_sm_set_state(bfad, bfad_sm_operational);
307 break;
Jing Huang7725ccf2009-09-23 17:46:15 -0700308
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700309 case BFAD_E_STOP:
310 if (bfad->bfad_flags & BFAD_CFG_PPORT_DONE)
311 bfad_uncfg_pport(bfad);
312 if (bfad->bfad_flags & BFAD_FC4_PROBE_DONE) {
313 bfad_im_probe_undo(bfad);
314 bfad->bfad_flags &= ~BFAD_FC4_PROBE_DONE;
315 }
316 bfad_stop(bfad);
317 break;
Jing Huang7725ccf2009-09-23 17:46:15 -0700318
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700319 case BFAD_E_EXIT_COMP:
320 bfa_sm_set_state(bfad, bfad_sm_uninit);
321 bfad_remove_intr(bfad);
322 del_timer_sync(&bfad->hal_tmo);
323 break;
Jing Huang7725ccf2009-09-23 17:46:15 -0700324
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700325 default:
326 bfa_sm_fault(bfad, event);
327 }
Jing Huang7725ccf2009-09-23 17:46:15 -0700328}
329
330static void
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700331bfad_sm_operational(struct bfad_s *bfad, enum bfad_sm_event event)
Jing Huang7725ccf2009-09-23 17:46:15 -0700332{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700333 bfa_trc(bfad, event);
Jing Huang7725ccf2009-09-23 17:46:15 -0700334
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700335 switch (event) {
336 case BFAD_E_STOP:
337 bfa_sm_set_state(bfad, bfad_sm_fcs_exit);
338 bfad_fcs_stop(bfad);
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 }
344}
345
346static void
347bfad_sm_fcs_exit(struct bfad_s *bfad, enum bfad_sm_event event)
348{
349 bfa_trc(bfad, event);
350
351 switch (event) {
352 case BFAD_E_FCS_EXIT_COMP:
353 bfa_sm_set_state(bfad, bfad_sm_stopping);
354 bfad_stop(bfad);
355 break;
356
357 default:
358 bfa_sm_fault(bfad, event);
359 }
360}
361
362static void
363bfad_sm_stopping(struct bfad_s *bfad, enum bfad_sm_event event)
364{
365 bfa_trc(bfad, event);
366
367 switch (event) {
368 case BFAD_E_EXIT_COMP:
369 bfa_sm_set_state(bfad, bfad_sm_uninit);
370 bfad_remove_intr(bfad);
371 del_timer_sync(&bfad->hal_tmo);
372 bfad_im_probe_undo(bfad);
373 bfad->bfad_flags &= ~BFAD_FC4_PROBE_DONE;
374 bfad_uncfg_pport(bfad);
375 break;
376
377 default:
378 bfa_sm_fault(bfad, event);
379 break;
380 }
Jing Huang7725ccf2009-09-23 17:46:15 -0700381}
382
Jing Huang5fbe25c2010-10-18 17:17:23 -0700383/*
Jing Huang7725ccf2009-09-23 17:46:15 -0700384 * BFA callbacks
385 */
386void
387bfad_hcb_comp(void *arg, bfa_status_t status)
388{
389 struct bfad_hal_comp *fcomp = (struct bfad_hal_comp *)arg;
390
391 fcomp->status = status;
392 complete(&fcomp->comp);
393}
394
Jing Huang5fbe25c2010-10-18 17:17:23 -0700395/*
Jing Huang7725ccf2009-09-23 17:46:15 -0700396 * bfa_init callback
397 */
398void
399bfa_cb_init(void *drv, bfa_status_t init_status)
400{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700401 struct bfad_s *bfad = drv;
Jing Huang7725ccf2009-09-23 17:46:15 -0700402
Krishna Gudipatie6714322010-03-03 17:44:02 -0800403 if (init_status == BFA_STATUS_OK) {
Jing Huang7725ccf2009-09-23 17:46:15 -0700404 bfad->bfad_flags |= BFAD_HAL_INIT_DONE;
405
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700406 /*
407 * If BFAD_HAL_INIT_FAIL flag is set:
Krishna Gudipatie6714322010-03-03 17:44:02 -0800408 * Wake up the kernel thread to start
409 * the bfad operations after HAL init done
410 */
411 if ((bfad->bfad_flags & BFAD_HAL_INIT_FAIL)) {
412 bfad->bfad_flags &= ~BFAD_HAL_INIT_FAIL;
413 wake_up_process(bfad->bfad_tsk);
414 }
415 }
416
Jing Huang7725ccf2009-09-23 17:46:15 -0700417 complete(&bfad->comp);
418}
419
Jing Huang5fbe25c2010-10-18 17:17:23 -0700420/*
Jing Huang7725ccf2009-09-23 17:46:15 -0700421 * BFA_FCS callbacks
422 */
Jing Huang7725ccf2009-09-23 17:46:15 -0700423struct bfad_port_s *
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700424bfa_fcb_lport_new(struct bfad_s *bfad, struct bfa_fcs_lport_s *port,
425 enum bfa_lport_role roles, struct bfad_vf_s *vf_drv,
Jing Huang7725ccf2009-09-23 17:46:15 -0700426 struct bfad_vport_s *vp_drv)
427{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700428 bfa_status_t rc;
429 struct bfad_port_s *port_drv;
Jing Huang7725ccf2009-09-23 17:46:15 -0700430
431 if (!vp_drv && !vf_drv) {
432 port_drv = &bfad->pport;
433 port_drv->pvb_type = BFAD_PORT_PHYS_BASE;
434 } else if (!vp_drv && vf_drv) {
435 port_drv = &vf_drv->base_port;
436 port_drv->pvb_type = BFAD_PORT_VF_BASE;
437 } else if (vp_drv && !vf_drv) {
438 port_drv = &vp_drv->drv_port;
439 port_drv->pvb_type = BFAD_PORT_PHYS_VPORT;
440 } else {
441 port_drv = &vp_drv->drv_port;
442 port_drv->pvb_type = BFAD_PORT_VF_VPORT;
443 }
444
445 port_drv->fcs_port = port;
446 port_drv->roles = roles;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700447
448 if (roles & BFA_LPORT_ROLE_FCP_IM) {
449 rc = bfad_im_port_new(bfad, port_drv);
450 if (rc != BFA_STATUS_OK) {
451 bfad_im_port_delete(bfad, port_drv);
452 port_drv = NULL;
453 }
Jing Huang7725ccf2009-09-23 17:46:15 -0700454 }
455
456 return port_drv;
457}
458
Jing Huang5fbe25c2010-10-18 17:17:23 -0700459/*
Jing Huang7725ccf2009-09-23 17:46:15 -0700460 * FCS RPORT alloc callback, after successful PLOGI by FCS
461 */
462bfa_status_t
463bfa_fcb_rport_alloc(struct bfad_s *bfad, struct bfa_fcs_rport_s **rport,
464 struct bfad_rport_s **rport_drv)
465{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700466 bfa_status_t rc = BFA_STATUS_OK;
Jing Huang7725ccf2009-09-23 17:46:15 -0700467
468 *rport_drv = kzalloc(sizeof(struct bfad_rport_s), GFP_ATOMIC);
469 if (*rport_drv == NULL) {
470 rc = BFA_STATUS_ENOMEM;
471 goto ext;
472 }
473
474 *rport = &(*rport_drv)->fcs_rport;
475
476ext:
477 return rc;
478}
479
Jing Huang5fbe25c2010-10-18 17:17:23 -0700480/*
Jing Huangd9883542010-07-08 19:46:26 -0700481 * FCS PBC VPORT Create
482 */
483void
484bfa_fcb_pbc_vport_create(struct bfad_s *bfad, struct bfi_pbc_vport_s pbc_vport)
485{
Jing Huang7725ccf2009-09-23 17:46:15 -0700486
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700487 struct bfa_lport_cfg_s port_cfg = {0};
488 struct bfad_vport_s *vport;
489 int rc;
Jing Huangd9883542010-07-08 19:46:26 -0700490
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700491 vport = kzalloc(sizeof(struct bfad_vport_s), GFP_KERNEL);
492 if (!vport) {
Jing Huangd9883542010-07-08 19:46:26 -0700493 bfa_trc(bfad, 0);
494 return;
495 }
496
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700497 vport->drv_port.bfad = bfad;
498 port_cfg.roles = BFA_LPORT_ROLE_FCP_IM;
499 port_cfg.pwwn = pbc_vport.vp_pwwn;
500 port_cfg.nwwn = pbc_vport.vp_nwwn;
501 port_cfg.preboot_vp = BFA_TRUE;
Jing Huangd9883542010-07-08 19:46:26 -0700502
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700503 rc = bfa_fcs_pbc_vport_create(&vport->fcs_vport, &bfad->bfa_fcs, 0,
504 &port_cfg, vport);
Jing Huangd9883542010-07-08 19:46:26 -0700505
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700506 if (rc != BFA_STATUS_OK) {
507 bfa_trc(bfad, 0);
508 return;
509 }
510
511 list_add_tail(&vport->list_entry, &bfad->pbc_vport_list);
Jing Huangd9883542010-07-08 19:46:26 -0700512}
Jing Huang7725ccf2009-09-23 17:46:15 -0700513
514void
515bfad_hal_mem_release(struct bfad_s *bfad)
516{
Jing Huang7725ccf2009-09-23 17:46:15 -0700517 struct bfa_meminfo_s *hal_meminfo = &bfad->meminfo;
Krishna Gudipati45070252011-06-24 20:24:29 -0700518 struct bfa_mem_dma_s *dma_info, *dma_elem;
519 struct bfa_mem_kva_s *kva_info, *kva_elem;
520 struct list_head *dm_qe, *km_qe;
Jing Huang7725ccf2009-09-23 17:46:15 -0700521
Krishna Gudipati45070252011-06-24 20:24:29 -0700522 dma_info = &hal_meminfo->dma_info;
523 kva_info = &hal_meminfo->kva_info;
524
525 /* Iterate through the KVA meminfo queue */
526 list_for_each(km_qe, &kva_info->qe) {
527 kva_elem = (struct bfa_mem_kva_s *) km_qe;
528 vfree(kva_elem->kva);
529 }
530
531 /* Iterate through the DMA meminfo queue */
532 list_for_each(dm_qe, &dma_info->qe) {
533 dma_elem = (struct bfa_mem_dma_s *) dm_qe;
534 dma_free_coherent(&bfad->pcidev->dev,
535 dma_elem->mem_len, dma_elem->kva,
536 (dma_addr_t) dma_elem->dma);
Jing Huang7725ccf2009-09-23 17:46:15 -0700537 }
538
539 memset(hal_meminfo, 0, sizeof(struct bfa_meminfo_s));
540}
541
542void
543bfad_update_hal_cfg(struct bfa_iocfc_cfg_s *bfa_cfg)
544{
545 if (num_rports > 0)
546 bfa_cfg->fwcfg.num_rports = num_rports;
547 if (num_ios > 0)
548 bfa_cfg->fwcfg.num_ioim_reqs = num_ios;
549 if (num_tms > 0)
550 bfa_cfg->fwcfg.num_tskim_reqs = num_tms;
Krishna Gudipati45070252011-06-24 20:24:29 -0700551 if (num_fcxps > 0 && num_fcxps <= BFA_FCXP_MAX)
Jing Huang7725ccf2009-09-23 17:46:15 -0700552 bfa_cfg->fwcfg.num_fcxp_reqs = num_fcxps;
Krishna Gudipati45070252011-06-24 20:24:29 -0700553 if (num_ufbufs > 0 && num_ufbufs <= BFA_UF_MAX)
Jing Huang7725ccf2009-09-23 17:46:15 -0700554 bfa_cfg->fwcfg.num_uf_bufs = num_ufbufs;
555 if (reqq_size > 0)
556 bfa_cfg->drvcfg.num_reqq_elems = reqq_size;
557 if (rspq_size > 0)
558 bfa_cfg->drvcfg.num_rspq_elems = rspq_size;
Krishna Gudipati45070252011-06-24 20:24:29 -0700559 if (num_sgpgs > 0 && num_sgpgs <= BFA_SGPG_MAX)
Jing Huang7725ccf2009-09-23 17:46:15 -0700560 bfa_cfg->drvcfg.num_sgpgs = num_sgpgs;
561
562 /*
563 * populate the hal values back to the driver for sysfs use.
564 * otherwise, the default values will be shown as 0 in sysfs
565 */
566 num_rports = bfa_cfg->fwcfg.num_rports;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700567 num_ios = bfa_cfg->fwcfg.num_ioim_reqs;
568 num_tms = bfa_cfg->fwcfg.num_tskim_reqs;
569 num_fcxps = bfa_cfg->fwcfg.num_fcxp_reqs;
Jing Huang7725ccf2009-09-23 17:46:15 -0700570 num_ufbufs = bfa_cfg->fwcfg.num_uf_bufs;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700571 reqq_size = bfa_cfg->drvcfg.num_reqq_elems;
572 rspq_size = bfa_cfg->drvcfg.num_rspq_elems;
573 num_sgpgs = bfa_cfg->drvcfg.num_sgpgs;
Jing Huang7725ccf2009-09-23 17:46:15 -0700574}
575
576bfa_status_t
577bfad_hal_mem_alloc(struct bfad_s *bfad)
578{
579 struct bfa_meminfo_s *hal_meminfo = &bfad->meminfo;
Krishna Gudipati45070252011-06-24 20:24:29 -0700580 struct bfa_mem_dma_s *dma_info, *dma_elem;
581 struct bfa_mem_kva_s *kva_info, *kva_elem;
582 struct list_head *dm_qe, *km_qe;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700583 bfa_status_t rc = BFA_STATUS_OK;
Krishna Gudipati45070252011-06-24 20:24:29 -0700584 dma_addr_t phys_addr;
Jing Huang7725ccf2009-09-23 17:46:15 -0700585
586 bfa_cfg_get_default(&bfad->ioc_cfg);
Jing Huang7725ccf2009-09-23 17:46:15 -0700587 bfad_update_hal_cfg(&bfad->ioc_cfg);
588 bfad->cfg_data.ioc_queue_depth = bfad->ioc_cfg.fwcfg.num_ioim_reqs;
Krishna Gudipati45070252011-06-24 20:24:29 -0700589 bfa_cfg_get_meminfo(&bfad->ioc_cfg, hal_meminfo, &bfad->bfa);
Jing Huang7725ccf2009-09-23 17:46:15 -0700590
Krishna Gudipati45070252011-06-24 20:24:29 -0700591 dma_info = &hal_meminfo->dma_info;
592 kva_info = &hal_meminfo->kva_info;
Jing Huang7725ccf2009-09-23 17:46:15 -0700593
Krishna Gudipati45070252011-06-24 20:24:29 -0700594 /* Iterate through the KVA meminfo queue */
595 list_for_each(km_qe, &kva_info->qe) {
596 kva_elem = (struct bfa_mem_kva_s *) km_qe;
597 kva_elem->kva = vmalloc(kva_elem->mem_len);
598 if (kva_elem->kva == NULL) {
599 bfad_hal_mem_release(bfad);
600 rc = BFA_STATUS_ENOMEM;
601 goto ext;
Jing Huang7725ccf2009-09-23 17:46:15 -0700602 }
Krishna Gudipati45070252011-06-24 20:24:29 -0700603 memset(kva_elem->kva, 0, kva_elem->mem_len);
604 }
605
606 /* Iterate through the DMA meminfo queue */
607 list_for_each(dm_qe, &dma_info->qe) {
608 dma_elem = (struct bfa_mem_dma_s *) dm_qe;
609 dma_elem->kva = dma_alloc_coherent(&bfad->pcidev->dev,
610 dma_elem->mem_len,
611 &phys_addr, GFP_KERNEL);
612 if (dma_elem->kva == NULL) {
613 bfad_hal_mem_release(bfad);
614 rc = BFA_STATUS_ENOMEM;
615 goto ext;
616 }
617 dma_elem->dma = phys_addr;
618 memset(dma_elem->kva, 0, dma_elem->mem_len);
Jing Huang7725ccf2009-09-23 17:46:15 -0700619 }
620ext:
621 return rc;
622}
623
Jing Huang5fbe25c2010-10-18 17:17:23 -0700624/*
Jing Huang7725ccf2009-09-23 17:46:15 -0700625 * Create a vport under a vf.
626 */
627bfa_status_t
628bfad_vport_create(struct bfad_s *bfad, u16 vf_id,
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700629 struct bfa_lport_cfg_s *port_cfg, struct device *dev)
Jing Huang7725ccf2009-09-23 17:46:15 -0700630{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700631 struct bfad_vport_s *vport;
632 int rc = BFA_STATUS_OK;
633 unsigned long flags;
Jing Huang7725ccf2009-09-23 17:46:15 -0700634 struct completion fcomp;
635
636 vport = kzalloc(sizeof(struct bfad_vport_s), GFP_KERNEL);
637 if (!vport) {
638 rc = BFA_STATUS_ENOMEM;
639 goto ext;
640 }
641
642 vport->drv_port.bfad = bfad;
643 spin_lock_irqsave(&bfad->bfad_lock, flags);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700644 rc = bfa_fcs_vport_create(&vport->fcs_vport, &bfad->bfa_fcs, vf_id,
645 port_cfg, vport);
Jing Huang7725ccf2009-09-23 17:46:15 -0700646 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
647
648 if (rc != BFA_STATUS_OK)
649 goto ext_free_vport;
650
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700651 if (port_cfg->roles & BFA_LPORT_ROLE_FCP_IM) {
Jing Huangb5042932010-03-19 11:05:39 -0700652 rc = bfad_im_scsi_host_alloc(bfad, vport->drv_port.im_port,
653 dev);
Jing Huang7725ccf2009-09-23 17:46:15 -0700654 if (rc != BFA_STATUS_OK)
655 goto ext_free_fcs_vport;
656 }
657
658 spin_lock_irqsave(&bfad->bfad_lock, flags);
659 bfa_fcs_vport_start(&vport->fcs_vport);
Krishna Gudipati5b7db7a2011-12-20 18:58:32 -0800660 list_add_tail(&vport->list_entry, &bfad->vport_list);
Jing Huang7725ccf2009-09-23 17:46:15 -0700661 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
662
663 return BFA_STATUS_OK;
664
665ext_free_fcs_vport:
666 spin_lock_irqsave(&bfad->bfad_lock, flags);
667 vport->comp_del = &fcomp;
668 init_completion(vport->comp_del);
669 bfa_fcs_vport_delete(&vport->fcs_vport);
670 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
671 wait_for_completion(vport->comp_del);
672ext_free_vport:
673 kfree(vport);
674ext:
675 return rc;
676}
677
Jing Huang7725ccf2009-09-23 17:46:15 -0700678void
679bfad_bfa_tmo(unsigned long data)
680{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700681 struct bfad_s *bfad = (struct bfad_s *) data;
682 unsigned long flags;
683 struct list_head doneq;
Jing Huang7725ccf2009-09-23 17:46:15 -0700684
685 spin_lock_irqsave(&bfad->bfad_lock, flags);
686
Maggie Zhangf7f738122010-12-09 19:08:43 -0800687 bfa_timer_beat(&bfad->bfa.timer_mod);
Jing Huang7725ccf2009-09-23 17:46:15 -0700688
689 bfa_comp_deq(&bfad->bfa, &doneq);
690 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
691
692 if (!list_empty(&doneq)) {
693 bfa_comp_process(&bfad->bfa, &doneq);
694 spin_lock_irqsave(&bfad->bfad_lock, flags);
695 bfa_comp_free(&bfad->bfa, &doneq);
696 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
697 }
698
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700699 mod_timer(&bfad->hal_tmo,
700 jiffies + msecs_to_jiffies(BFA_TIMER_FREQ));
Jing Huang7725ccf2009-09-23 17:46:15 -0700701}
702
703void
704bfad_init_timer(struct bfad_s *bfad)
705{
706 init_timer(&bfad->hal_tmo);
707 bfad->hal_tmo.function = bfad_bfa_tmo;
708 bfad->hal_tmo.data = (unsigned long)bfad;
709
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700710 mod_timer(&bfad->hal_tmo,
711 jiffies + msecs_to_jiffies(BFA_TIMER_FREQ));
Jing Huang7725ccf2009-09-23 17:46:15 -0700712}
713
714int
715bfad_pci_init(struct pci_dev *pdev, struct bfad_s *bfad)
716{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700717 int rc = -ENODEV;
Jing Huang7725ccf2009-09-23 17:46:15 -0700718
719 if (pci_enable_device(pdev)) {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700720 printk(KERN_ERR "pci_enable_device fail %p\n", pdev);
Jing Huang7725ccf2009-09-23 17:46:15 -0700721 goto out;
722 }
723
724 if (pci_request_regions(pdev, BFAD_DRIVER_NAME))
725 goto out_disable_device;
726
727 pci_set_master(pdev);
728
729
Krishna Gudipati10a07372011-06-24 20:23:38 -0700730 if ((pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) ||
731 (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)) != 0)) {
732 if ((pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0) ||
733 (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)) {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700734 printk(KERN_ERR "pci_set_dma_mask fail %p\n", pdev);
Jing Huang7725ccf2009-09-23 17:46:15 -0700735 goto out_release_region;
736 }
Krishna Gudipati10a07372011-06-24 20:23:38 -0700737 }
Jing Huang7725ccf2009-09-23 17:46:15 -0700738
Jing Huangb3522f02010-03-19 11:06:44 -0700739 bfad->pci_bar0_kva = pci_iomap(pdev, 0, pci_resource_len(pdev, 0));
Krishna Gudipati11189202011-06-13 15:50:35 -0700740 bfad->pci_bar2_kva = pci_iomap(pdev, 2, pci_resource_len(pdev, 2));
Jing Huang7725ccf2009-09-23 17:46:15 -0700741
742 if (bfad->pci_bar0_kva == NULL) {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700743 printk(KERN_ERR "Fail to map bar0\n");
Jing Huang7725ccf2009-09-23 17:46:15 -0700744 goto out_release_region;
745 }
746
747 bfad->hal_pcidev.pci_slot = PCI_SLOT(pdev->devfn);
748 bfad->hal_pcidev.pci_func = PCI_FUNC(pdev->devfn);
749 bfad->hal_pcidev.pci_bar_kva = bfad->pci_bar0_kva;
750 bfad->hal_pcidev.device_id = pdev->device;
Krishna Gudipati1a4d8e12011-06-24 20:22:28 -0700751 bfad->hal_pcidev.ssid = pdev->subsystem_device;
Jing Huang7725ccf2009-09-23 17:46:15 -0700752 bfad->pci_name = pci_name(pdev);
753
754 bfad->pci_attr.vendor_id = pdev->vendor;
755 bfad->pci_attr.device_id = pdev->device;
756 bfad->pci_attr.ssid = pdev->subsystem_device;
757 bfad->pci_attr.ssvid = pdev->subsystem_vendor;
758 bfad->pci_attr.pcifn = PCI_FUNC(pdev->devfn);
759
760 bfad->pcidev = pdev;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700761
762 /* Adjust PCIe Maximum Read Request Size */
763 if (pcie_max_read_reqsz > 0) {
764 int pcie_cap_reg;
765 u16 pcie_dev_ctl;
766 u16 mask = 0xffff;
767
768 switch (pcie_max_read_reqsz) {
769 case 128:
770 mask = 0x0;
771 break;
772 case 256:
773 mask = 0x1000;
774 break;
775 case 512:
776 mask = 0x2000;
777 break;
778 case 1024:
779 mask = 0x3000;
780 break;
781 case 2048:
782 mask = 0x4000;
783 break;
784 case 4096:
785 mask = 0x5000;
786 break;
787 default:
788 break;
789 }
790
791 pcie_cap_reg = pci_find_capability(pdev, PCI_CAP_ID_EXP);
792 if (mask != 0xffff && pcie_cap_reg) {
793 pcie_cap_reg += 0x08;
794 pci_read_config_word(pdev, pcie_cap_reg, &pcie_dev_ctl);
795 if ((pcie_dev_ctl & 0x7000) != mask) {
796 printk(KERN_WARNING "BFA[%s]: "
797 "pcie_max_read_request_size is %d, "
798 "reset to %d\n", bfad->pci_name,
799 (1 << ((pcie_dev_ctl & 0x7000) >> 12)) << 7,
800 pcie_max_read_reqsz);
801
802 pcie_dev_ctl &= ~0x7000;
803 pci_write_config_word(pdev, pcie_cap_reg,
804 pcie_dev_ctl | mask);
805 }
806 }
807 }
808
Jing Huang7725ccf2009-09-23 17:46:15 -0700809 return 0;
810
811out_release_region:
812 pci_release_regions(pdev);
813out_disable_device:
814 pci_disable_device(pdev);
815out:
816 return rc;
817}
818
819void
820bfad_pci_uninit(struct pci_dev *pdev, struct bfad_s *bfad)
821{
Jing Huang7725ccf2009-09-23 17:46:15 -0700822 pci_iounmap(pdev, bfad->pci_bar0_kva);
Krishna Gudipati11189202011-06-13 15:50:35 -0700823 pci_iounmap(pdev, bfad->pci_bar2_kva);
Jing Huang7725ccf2009-09-23 17:46:15 -0700824 pci_release_regions(pdev);
825 pci_disable_device(pdev);
826 pci_set_drvdata(pdev, NULL);
827}
828
Jing Huang7725ccf2009-09-23 17:46:15 -0700829bfa_status_t
830bfad_drv_init(struct bfad_s *bfad)
831{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700832 bfa_status_t rc;
833 unsigned long flags;
Jing Huang7725ccf2009-09-23 17:46:15 -0700834
835 bfad->cfg_data.rport_del_timeout = rport_del_timeout;
836 bfad->cfg_data.lun_queue_depth = bfa_lun_queue_depth;
837 bfad->cfg_data.io_max_sge = bfa_io_max_sge;
838 bfad->cfg_data.binding_method = FCP_PWWN_BINDING;
839
840 rc = bfad_hal_mem_alloc(bfad);
841 if (rc != BFA_STATUS_OK) {
842 printk(KERN_WARNING "bfad%d bfad_hal_mem_alloc failure\n",
843 bfad->inst_no);
844 printk(KERN_WARNING
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700845 "Not enough memory to attach all Brocade HBA ports, %s",
846 "System may need more memory.\n");
Jing Huang7725ccf2009-09-23 17:46:15 -0700847 goto out_hal_mem_alloc_failure;
848 }
849
Maggie Zhangf7f738122010-12-09 19:08:43 -0800850 bfad->bfa.trcmod = bfad->trcmod;
851 bfad->bfa.plog = &bfad->plog_buf;
Jing Huang7725ccf2009-09-23 17:46:15 -0700852 bfa_plog_init(&bfad->plog_buf);
853 bfa_plog_str(&bfad->plog_buf, BFA_PL_MID_DRVR, BFA_PL_EID_DRIVER_START,
854 0, "Driver Attach");
855
856 bfa_attach(&bfad->bfa, bfad, &bfad->ioc_cfg, &bfad->meminfo,
857 &bfad->hal_pcidev);
858
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700859 /* FCS INIT */
Jing Huang7725ccf2009-09-23 17:46:15 -0700860 spin_lock_irqsave(&bfad->bfad_lock, flags);
Maggie Zhangf7f738122010-12-09 19:08:43 -0800861 bfad->bfa_fcs.trcmod = bfad->trcmod;
Krishna Gudipati82794a22010-03-03 17:43:30 -0800862 bfa_fcs_attach(&bfad->bfa_fcs, &bfad->bfa, bfad, BFA_FALSE);
Maggie Zhangf7f738122010-12-09 19:08:43 -0800863 bfad->bfa_fcs.fdmi_enabled = fdmi_enable;
Krishna Gudipati75332a72011-06-13 15:54:31 -0700864 bfa_fcs_init(&bfad->bfa_fcs);
Jing Huang7725ccf2009-09-23 17:46:15 -0700865 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
866
867 bfad->bfad_flags |= BFAD_DRV_INIT_DONE;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700868
Krishna Gudipati75332a72011-06-13 15:54:31 -0700869 /* configure base port */
870 rc = bfad_cfg_pport(bfad, BFA_LPORT_ROLE_FCP_IM);
871 if (rc != BFA_STATUS_OK)
872 goto out_cfg_pport_fail;
873
Jing Huang7725ccf2009-09-23 17:46:15 -0700874 return BFA_STATUS_OK;
875
Krishna Gudipati75332a72011-06-13 15:54:31 -0700876out_cfg_pport_fail:
877 /* fcs exit - on cfg pport failure */
878 spin_lock_irqsave(&bfad->bfad_lock, flags);
879 init_completion(&bfad->comp);
880 bfad->pport.flags |= BFAD_PORT_DELETE;
881 bfa_fcs_exit(&bfad->bfa_fcs);
882 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
883 wait_for_completion(&bfad->comp);
884 /* bfa detach - free hal memory */
885 bfa_detach(&bfad->bfa);
886 bfad_hal_mem_release(bfad);
Jing Huang7725ccf2009-09-23 17:46:15 -0700887out_hal_mem_alloc_failure:
888 return BFA_STATUS_FAILED;
889}
890
891void
892bfad_drv_uninit(struct bfad_s *bfad)
893{
Krishna Gudipatie6714322010-03-03 17:44:02 -0800894 unsigned long flags;
895
896 spin_lock_irqsave(&bfad->bfad_lock, flags);
897 init_completion(&bfad->comp);
Maggie Zhangf7f738122010-12-09 19:08:43 -0800898 bfa_iocfc_stop(&bfad->bfa);
Krishna Gudipatie6714322010-03-03 17:44:02 -0800899 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
900 wait_for_completion(&bfad->comp);
901
Jing Huang7725ccf2009-09-23 17:46:15 -0700902 del_timer_sync(&bfad->hal_tmo);
903 bfa_isr_disable(&bfad->bfa);
904 bfa_detach(&bfad->bfa);
905 bfad_remove_intr(bfad);
Jing Huang7725ccf2009-09-23 17:46:15 -0700906 bfad_hal_mem_release(bfad);
Krishna Gudipatie6714322010-03-03 17:44:02 -0800907
908 bfad->bfad_flags &= ~BFAD_DRV_INIT_DONE;
Jing Huang7725ccf2009-09-23 17:46:15 -0700909}
910
911void
912bfad_drv_start(struct bfad_s *bfad)
913{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700914 unsigned long flags;
Jing Huang7725ccf2009-09-23 17:46:15 -0700915
916 spin_lock_irqsave(&bfad->bfad_lock, flags);
Maggie Zhangf7f738122010-12-09 19:08:43 -0800917 bfa_iocfc_start(&bfad->bfa);
Krishna Gudipati75332a72011-06-13 15:54:31 -0700918 bfa_fcs_pbc_vport_init(&bfad->bfa_fcs);
Maggie Zhangf7f738122010-12-09 19:08:43 -0800919 bfa_fcs_fabric_modstart(&bfad->bfa_fcs);
Jing Huang7725ccf2009-09-23 17:46:15 -0700920 bfad->bfad_flags |= BFAD_HAL_START_DONE;
921 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
922
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700923 if (bfad->im)
924 flush_workqueue(bfad->im->drv_workq);
Jing Huang7725ccf2009-09-23 17:46:15 -0700925}
926
927void
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700928bfad_fcs_stop(struct bfad_s *bfad)
Jing Huang7725ccf2009-09-23 17:46:15 -0700929{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700930 unsigned long flags;
Jing Huang7725ccf2009-09-23 17:46:15 -0700931
932 spin_lock_irqsave(&bfad->bfad_lock, flags);
933 init_completion(&bfad->comp);
934 bfad->pport.flags |= BFAD_PORT_DELETE;
935 bfa_fcs_exit(&bfad->bfa_fcs);
936 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
937 wait_for_completion(&bfad->comp);
938
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700939 bfa_sm_send_event(bfad, BFAD_E_FCS_EXIT_COMP);
940}
941
942void
943bfad_stop(struct bfad_s *bfad)
944{
945 unsigned long flags;
946
Jing Huang7725ccf2009-09-23 17:46:15 -0700947 spin_lock_irqsave(&bfad->bfad_lock, flags);
948 init_completion(&bfad->comp);
Maggie Zhangf7f738122010-12-09 19:08:43 -0800949 bfa_iocfc_stop(&bfad->bfa);
Jing Huang7725ccf2009-09-23 17:46:15 -0700950 bfad->bfad_flags &= ~BFAD_HAL_START_DONE;
951 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
952 wait_for_completion(&bfad->comp);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700953
954 bfa_sm_send_event(bfad, BFAD_E_EXIT_COMP);
Jing Huang7725ccf2009-09-23 17:46:15 -0700955}
956
957bfa_status_t
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700958bfad_cfg_pport(struct bfad_s *bfad, enum bfa_lport_role role)
Jing Huang7725ccf2009-09-23 17:46:15 -0700959{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700960 int rc = BFA_STATUS_OK;
Jing Huang7725ccf2009-09-23 17:46:15 -0700961
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700962 /* Allocate scsi_host for the physical port */
963 if ((supported_fc4s & BFA_LPORT_ROLE_FCP_IM) &&
964 (role & BFA_LPORT_ROLE_FCP_IM)) {
Jing Huang7725ccf2009-09-23 17:46:15 -0700965 if (bfad->pport.im_port == NULL) {
966 rc = BFA_STATUS_FAILED;
967 goto out;
968 }
969
Jing Huangb5042932010-03-19 11:05:39 -0700970 rc = bfad_im_scsi_host_alloc(bfad, bfad->pport.im_port,
971 &bfad->pcidev->dev);
Jing Huang7725ccf2009-09-23 17:46:15 -0700972 if (rc != BFA_STATUS_OK)
973 goto out;
974
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700975 bfad->pport.roles |= BFA_LPORT_ROLE_FCP_IM;
Jing Huang7725ccf2009-09-23 17:46:15 -0700976 }
977
978 bfad->bfad_flags |= BFAD_CFG_PPORT_DONE;
979
980out:
981 return rc;
982}
983
984void
985bfad_uncfg_pport(struct bfad_s *bfad)
986{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700987 if ((supported_fc4s & BFA_LPORT_ROLE_FCP_IM) &&
988 (bfad->pport.roles & BFA_LPORT_ROLE_FCP_IM)) {
Jing Huang7725ccf2009-09-23 17:46:15 -0700989 bfad_im_scsi_host_free(bfad, bfad->pport.im_port);
990 bfad_im_port_clean(bfad->pport.im_port);
991 kfree(bfad->pport.im_port);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700992 bfad->pport.roles &= ~BFA_LPORT_ROLE_FCP_IM;
Jing Huang7725ccf2009-09-23 17:46:15 -0700993 }
994
995 bfad->bfad_flags &= ~BFAD_CFG_PPORT_DONE;
996}
997
Krishna Gudipatie6714322010-03-03 17:44:02 -0800998bfa_status_t
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700999bfad_start_ops(struct bfad_s *bfad) {
1000
1001 int retval;
1002 unsigned long flags;
1003 struct bfad_vport_s *vport, *vport_new;
1004 struct bfa_fcs_driver_info_s driver_info;
1005
Krishna Gudipati61e62e22011-06-24 20:29:07 -07001006 /* Limit min/max. xfer size to [64k-32MB] */
1007 if (max_xfer_size < BFAD_MIN_SECTORS >> 1)
1008 max_xfer_size = BFAD_MIN_SECTORS >> 1;
1009 if (max_xfer_size > BFAD_MAX_SECTORS >> 1)
1010 max_xfer_size = BFAD_MAX_SECTORS >> 1;
1011
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001012 /* Fill the driver_info info to fcs*/
1013 memset(&driver_info, 0, sizeof(driver_info));
1014 strncpy(driver_info.version, BFAD_DRIVER_VERSION,
1015 sizeof(driver_info.version) - 1);
1016 if (host_name)
1017 strncpy(driver_info.host_machine_name, host_name,
1018 sizeof(driver_info.host_machine_name) - 1);
1019 if (os_name)
1020 strncpy(driver_info.host_os_name, os_name,
1021 sizeof(driver_info.host_os_name) - 1);
1022 if (os_patch)
1023 strncpy(driver_info.host_os_patch, os_patch,
1024 sizeof(driver_info.host_os_patch) - 1);
1025
1026 strncpy(driver_info.os_device_name, bfad->pci_name,
1027 sizeof(driver_info.os_device_name - 1));
1028
Krishna Gudipati75332a72011-06-13 15:54:31 -07001029 /* FCS driver info init */
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001030 spin_lock_irqsave(&bfad->bfad_lock, flags);
1031 bfa_fcs_driver_info_init(&bfad->bfa_fcs, &driver_info);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001032 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
Krishna Gudipatie6714322010-03-03 17:44:02 -08001033
Krishna Gudipati75332a72011-06-13 15:54:31 -07001034 /*
1035 * FCS update cfg - reset the pwwn/nwwn of fabric base logical port
1036 * with values learned during bfa_init firmware GETATTR REQ.
1037 */
1038 bfa_fcs_update_cfg(&bfad->bfa_fcs);
1039
1040 /* Setup fc host fixed attribute if the lk supports */
1041 bfad_fc_host_init(bfad->pport.im_port);
Krishna Gudipatie6714322010-03-03 17:44:02 -08001042
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001043 /* BFAD level FC4 IM specific resource allocation */
1044 retval = bfad_im_probe(bfad);
1045 if (retval != BFA_STATUS_OK) {
1046 printk(KERN_WARNING "bfad_im_probe failed\n");
1047 if (bfa_sm_cmp_state(bfad, bfad_sm_initializing))
1048 bfa_sm_set_state(bfad, bfad_sm_failed);
1049 bfad_im_probe_undo(bfad);
1050 bfad->bfad_flags &= ~BFAD_FC4_PROBE_DONE;
1051 bfad_uncfg_pport(bfad);
1052 bfad_stop(bfad);
1053 return BFA_STATUS_FAILED;
1054 } else
1055 bfad->bfad_flags |= BFAD_FC4_PROBE_DONE;
1056
Krishna Gudipatie6714322010-03-03 17:44:02 -08001057 bfad_drv_start(bfad);
1058
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001059 /* Complete pbc vport create */
1060 list_for_each_entry_safe(vport, vport_new, &bfad->pbc_vport_list,
1061 list_entry) {
Jing Huangd9883542010-07-08 19:46:26 -07001062 struct fc_vport_identifiers vid;
1063 struct fc_vport *fc_vport;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001064 char pwwn_buf[BFA_STRING_32];
Jing Huangd9883542010-07-08 19:46:26 -07001065
1066 memset(&vid, 0, sizeof(vid));
1067 vid.roles = FC_PORT_ROLE_FCP_INITIATOR;
1068 vid.vport_type = FC_PORTTYPE_NPIV;
1069 vid.disable = false;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001070 vid.node_name = wwn_to_u64((u8 *)
1071 (&((vport->fcs_vport).lport.port_cfg.nwwn)));
1072 vid.port_name = wwn_to_u64((u8 *)
1073 (&((vport->fcs_vport).lport.port_cfg.pwwn)));
Jing Huangd9883542010-07-08 19:46:26 -07001074 fc_vport = fc_vport_create(bfad->pport.im_port->shost, 0, &vid);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001075 if (!fc_vport) {
1076 wwn2str(pwwn_buf, vid.port_name);
Jing Huangd9883542010-07-08 19:46:26 -07001077 printk(KERN_WARNING "bfad%d: failed to create pbc vport"
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001078 " %s\n", bfad->inst_no, pwwn_buf);
1079 }
1080 list_del(&vport->list_entry);
1081 kfree(vport);
Jing Huangd9883542010-07-08 19:46:26 -07001082 }
1083
Krishna Gudipatie6714322010-03-03 17:44:02 -08001084 /*
1085 * If bfa_linkup_delay is set to -1 default; try to retrive the
Maggie Zhangf16a1752010-12-09 19:12:32 -08001086 * value using the bfad_get_linkup_delay(); else use the
Krishna Gudipatie6714322010-03-03 17:44:02 -08001087 * passed in module param value as the bfa_linkup_delay.
1088 */
1089 if (bfa_linkup_delay < 0) {
Maggie Zhangf16a1752010-12-09 19:12:32 -08001090 bfa_linkup_delay = bfad_get_linkup_delay(bfad);
1091 bfad_rport_online_wait(bfad);
Krishna Gudipatie6714322010-03-03 17:44:02 -08001092 bfa_linkup_delay = -1;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001093 } else
Maggie Zhangf16a1752010-12-09 19:12:32 -08001094 bfad_rport_online_wait(bfad);
Krishna Gudipatie6714322010-03-03 17:44:02 -08001095
Jing Huang88166242010-12-09 17:11:53 -08001096 BFA_LOG(KERN_INFO, bfad, bfa_log_level, "bfa device claimed\n");
Krishna Gudipatie6714322010-03-03 17:44:02 -08001097
1098 return BFA_STATUS_OK;
Krishna Gudipatie6714322010-03-03 17:44:02 -08001099}
1100
1101int
Jing Huangd9883542010-07-08 19:46:26 -07001102bfad_worker(void *ptr)
Krishna Gudipatie6714322010-03-03 17:44:02 -08001103{
1104 struct bfad_s *bfad;
1105 unsigned long flags;
1106
1107 bfad = (struct bfad_s *)ptr;
1108
1109 while (!kthread_should_stop()) {
1110
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001111 /* Send event BFAD_E_INIT_SUCCESS */
1112 bfa_sm_send_event(bfad, BFAD_E_INIT_SUCCESS);
Krishna Gudipatie6714322010-03-03 17:44:02 -08001113
1114 spin_lock_irqsave(&bfad->bfad_lock, flags);
1115 bfad->bfad_tsk = NULL;
1116 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1117
1118 break;
1119 }
1120
1121 return 0;
1122}
1123
Jing Huang5fbe25c2010-10-18 17:17:23 -07001124/*
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001125 * BFA driver interrupt functions
1126 */
1127irqreturn_t
1128bfad_intx(int irq, void *dev_id)
1129{
1130 struct bfad_s *bfad = dev_id;
1131 struct list_head doneq;
1132 unsigned long flags;
1133 bfa_boolean_t rc;
1134
1135 spin_lock_irqsave(&bfad->bfad_lock, flags);
1136 rc = bfa_intx(&bfad->bfa);
1137 if (!rc) {
1138 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1139 return IRQ_NONE;
1140 }
1141
1142 bfa_comp_deq(&bfad->bfa, &doneq);
1143 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1144
1145 if (!list_empty(&doneq)) {
1146 bfa_comp_process(&bfad->bfa, &doneq);
1147
1148 spin_lock_irqsave(&bfad->bfad_lock, flags);
1149 bfa_comp_free(&bfad->bfa, &doneq);
1150 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001151 }
1152
1153 return IRQ_HANDLED;
1154
1155}
1156
1157static irqreturn_t
1158bfad_msix(int irq, void *dev_id)
1159{
1160 struct bfad_msix_s *vec = dev_id;
1161 struct bfad_s *bfad = vec->bfad;
1162 struct list_head doneq;
1163 unsigned long flags;
1164
1165 spin_lock_irqsave(&bfad->bfad_lock, flags);
1166
1167 bfa_msix(&bfad->bfa, vec->msix.entry);
1168 bfa_comp_deq(&bfad->bfa, &doneq);
1169 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1170
1171 if (!list_empty(&doneq)) {
1172 bfa_comp_process(&bfad->bfa, &doneq);
1173
1174 spin_lock_irqsave(&bfad->bfad_lock, flags);
1175 bfa_comp_free(&bfad->bfa, &doneq);
1176 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1177 }
1178
1179 return IRQ_HANDLED;
1180}
1181
Jing Huang5fbe25c2010-10-18 17:17:23 -07001182/*
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001183 * Initialize the MSIX entry table.
1184 */
1185static void
1186bfad_init_msix_entry(struct bfad_s *bfad, struct msix_entry *msix_entries,
1187 int mask, int max_bit)
1188{
1189 int i;
1190 int match = 0x00000001;
1191
1192 for (i = 0, bfad->nvec = 0; i < MAX_MSIX_ENTRY; i++) {
1193 if (mask & match) {
1194 bfad->msix_tab[bfad->nvec].msix.entry = i;
1195 bfad->msix_tab[bfad->nvec].bfad = bfad;
1196 msix_entries[bfad->nvec].entry = i;
1197 bfad->nvec++;
1198 }
1199
1200 match <<= 1;
1201 }
1202
1203}
1204
1205int
1206bfad_install_msix_handler(struct bfad_s *bfad)
1207{
1208 int i, error = 0;
1209
1210 for (i = 0; i < bfad->nvec; i++) {
1211 sprintf(bfad->msix_tab[i].name, "bfa-%s-%s",
1212 bfad->pci_name,
Krishna Gudipati11189202011-06-13 15:50:35 -07001213 ((bfa_asic_id_cb(bfad->hal_pcidev.device_id)) ?
1214 msix_name_cb[i] : msix_name_ct[i]));
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001215
1216 error = request_irq(bfad->msix_tab[i].msix.vector,
1217 (irq_handler_t) bfad_msix, 0,
1218 bfad->msix_tab[i].name, &bfad->msix_tab[i]);
1219 bfa_trc(bfad, i);
1220 bfa_trc(bfad, bfad->msix_tab[i].msix.vector);
1221 if (error) {
1222 int j;
1223
1224 for (j = 0; j < i; j++)
1225 free_irq(bfad->msix_tab[j].msix.vector,
1226 &bfad->msix_tab[j]);
1227
Krishna Gudipati61e62e22011-06-24 20:29:07 -07001228 bfad->bfad_flags &= ~BFAD_MSIX_ON;
1229 pci_disable_msix(bfad->pcidev);
1230
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001231 return 1;
1232 }
1233 }
1234
1235 return 0;
1236}
1237
Jing Huang5fbe25c2010-10-18 17:17:23 -07001238/*
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001239 * Setup MSIX based interrupt.
1240 */
1241int
1242bfad_setup_intr(struct bfad_s *bfad)
1243{
1244 int error = 0;
1245 u32 mask = 0, i, num_bit = 0, max_bit = 0;
1246 struct msix_entry msix_entries[MAX_MSIX_ENTRY];
1247 struct pci_dev *pdev = bfad->pcidev;
Krishna Gudipati10a07372011-06-24 20:23:38 -07001248 u16 reg;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001249
1250 /* Call BFA to get the msix map for this PCI function. */
1251 bfa_msix_getvecs(&bfad->bfa, &mask, &num_bit, &max_bit);
1252
1253 /* Set up the msix entry table */
1254 bfad_init_msix_entry(bfad, msix_entries, mask, max_bit);
1255
Krishna Gudipati11189202011-06-13 15:50:35 -07001256 if ((bfa_asic_id_ctc(pdev->device) && !msix_disable_ct) ||
1257 (bfa_asic_id_cb(pdev->device) && !msix_disable_cb)) {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001258
1259 error = pci_enable_msix(bfad->pcidev, msix_entries, bfad->nvec);
1260 if (error) {
1261 /*
1262 * Only error number of vector is available.
1263 * We don't have a mechanism to map multiple
1264 * interrupts into one vector, so even if we
1265 * can try to request less vectors, we don't
1266 * know how to associate interrupt events to
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001267 * vectors. Linux doesn't duplicate vectors
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001268 * in the MSIX table for this case.
1269 */
1270
1271 printk(KERN_WARNING "bfad%d: "
1272 "pci_enable_msix failed (%d),"
1273 " use line based.\n", bfad->inst_no, error);
1274
1275 goto line_based;
1276 }
1277
Krishna Gudipati10a07372011-06-24 20:23:38 -07001278 /* Disable INTX in MSI-X mode */
1279 pci_read_config_word(pdev, PCI_COMMAND, &reg);
1280
1281 if (!(reg & PCI_COMMAND_INTX_DISABLE))
1282 pci_write_config_word(pdev, PCI_COMMAND,
1283 reg | PCI_COMMAND_INTX_DISABLE);
1284
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001285 /* Save the vectors */
1286 for (i = 0; i < bfad->nvec; i++) {
1287 bfa_trc(bfad, msix_entries[i].vector);
1288 bfad->msix_tab[i].msix.vector = msix_entries[i].vector;
1289 }
1290
1291 bfa_msix_init(&bfad->bfa, bfad->nvec);
1292
1293 bfad->bfad_flags |= BFAD_MSIX_ON;
1294
1295 return error;
1296 }
1297
1298line_based:
1299 error = 0;
1300 if (request_irq
1301 (bfad->pcidev->irq, (irq_handler_t) bfad_intx, BFAD_IRQ_FLAGS,
1302 BFAD_DRIVER_NAME, bfad) != 0) {
1303 /* Enable interrupt handler failed */
1304 return 1;
1305 }
Krishna Gudipati61e62e22011-06-24 20:29:07 -07001306 bfad->bfad_flags |= BFAD_INTX_ON;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001307
1308 return error;
1309}
1310
1311void
1312bfad_remove_intr(struct bfad_s *bfad)
1313{
1314 int i;
1315
1316 if (bfad->bfad_flags & BFAD_MSIX_ON) {
1317 for (i = 0; i < bfad->nvec; i++)
1318 free_irq(bfad->msix_tab[i].msix.vector,
1319 &bfad->msix_tab[i]);
1320
1321 pci_disable_msix(bfad->pcidev);
1322 bfad->bfad_flags &= ~BFAD_MSIX_ON;
Krishna Gudipati61e62e22011-06-24 20:29:07 -07001323 } else if (bfad->bfad_flags & BFAD_INTX_ON) {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001324 free_irq(bfad->pcidev->irq, bfad);
1325 }
1326}
Jing Huang7725ccf2009-09-23 17:46:15 -07001327
Jing Huang5fbe25c2010-10-18 17:17:23 -07001328/*
Jing Huang7725ccf2009-09-23 17:46:15 -07001329 * PCI probe entry.
1330 */
1331int
1332bfad_pci_probe(struct pci_dev *pdev, const struct pci_device_id *pid)
1333{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001334 struct bfad_s *bfad;
Krishna Gudipati7826f302011-07-20 16:59:13 -07001335 int error = -ENODEV, retval, i;
Jing Huang7725ccf2009-09-23 17:46:15 -07001336
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001337 /* For single port cards - only claim function 0 */
Krishna Gudipati8b070b42011-06-13 15:52:40 -07001338 if ((pdev->device == BFA_PCI_DEVICE_ID_FC_8G1P) &&
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001339 (PCI_FUNC(pdev->devfn) != 0))
Jing Huang7725ccf2009-09-23 17:46:15 -07001340 return -ENODEV;
1341
Jing Huang7725ccf2009-09-23 17:46:15 -07001342 bfad = kzalloc(sizeof(struct bfad_s), GFP_KERNEL);
1343 if (!bfad) {
1344 error = -ENOMEM;
1345 goto out;
1346 }
1347
1348 bfad->trcmod = kzalloc(sizeof(struct bfa_trc_mod_s), GFP_KERNEL);
1349 if (!bfad->trcmod) {
1350 printk(KERN_WARNING "Error alloc trace buffer!\n");
1351 error = -ENOMEM;
1352 goto out_alloc_trace_failure;
1353 }
1354
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001355 /* TRACE INIT */
Jing Huang7725ccf2009-09-23 17:46:15 -07001356 bfa_trc_init(bfad->trcmod);
1357 bfa_trc(bfad, bfad_inst);
1358
Krishna Gudipati7826f302011-07-20 16:59:13 -07001359 /* AEN INIT */
1360 INIT_LIST_HEAD(&bfad->free_aen_q);
1361 INIT_LIST_HEAD(&bfad->active_aen_q);
1362 for (i = 0; i < BFA_AEN_MAX_ENTRY; i++)
1363 list_add_tail(&bfad->aen_list[i].qe, &bfad->free_aen_q);
1364
Jing Huang7725ccf2009-09-23 17:46:15 -07001365 if (!(bfad_load_fwimg(pdev))) {
Jing Huang7725ccf2009-09-23 17:46:15 -07001366 kfree(bfad->trcmod);
1367 goto out_alloc_trace_failure;
1368 }
1369
1370 retval = bfad_pci_init(pdev, bfad);
1371 if (retval) {
1372 printk(KERN_WARNING "bfad_pci_init failure!\n");
1373 error = retval;
1374 goto out_pci_init_failure;
1375 }
1376
1377 mutex_lock(&bfad_mutex);
1378 bfad->inst_no = bfad_inst++;
1379 list_add_tail(&bfad->list_entry, &bfad_list);
1380 mutex_unlock(&bfad_mutex);
1381
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001382 /* Initializing the state machine: State set to uninit */
1383 bfa_sm_set_state(bfad, bfad_sm_uninit);
1384
Jing Huang7725ccf2009-09-23 17:46:15 -07001385 spin_lock_init(&bfad->bfad_lock);
Kyle McMartin0b1017a2012-07-11 12:42:55 -04001386 spin_lock_init(&bfad->bfad_aen_spinlock);
1387
Jing Huang7725ccf2009-09-23 17:46:15 -07001388 pci_set_drvdata(pdev, bfad);
1389
1390 bfad->ref_count = 0;
1391 bfad->pport.bfad = bfad;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001392 INIT_LIST_HEAD(&bfad->pbc_vport_list);
Krishna Gudipati5b7db7a2011-12-20 18:58:32 -08001393 INIT_LIST_HEAD(&bfad->vport_list);
Krishna Gudipatie6714322010-03-03 17:44:02 -08001394
Krishna Gudipati7c38c052011-04-14 16:50:35 -07001395 /* Setup the debugfs node for this bfad */
1396 if (bfa_debugfs_enable)
1397 bfad_debugfs_init(&bfad->pport);
1398
Jing Huang7725ccf2009-09-23 17:46:15 -07001399 retval = bfad_drv_init(bfad);
1400 if (retval != BFA_STATUS_OK)
1401 goto out_drv_init_failure;
Jing Huang7725ccf2009-09-23 17:46:15 -07001402
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001403 bfa_sm_send_event(bfad, BFAD_E_CREATE);
Jing Huang7725ccf2009-09-23 17:46:15 -07001404
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001405 if (bfa_sm_cmp_state(bfad, bfad_sm_uninit))
1406 goto out_bfad_sm_failure;
Jing Huang7725ccf2009-09-23 17:46:15 -07001407
Jing Huang7725ccf2009-09-23 17:46:15 -07001408 return 0;
1409
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001410out_bfad_sm_failure:
1411 bfa_detach(&bfad->bfa);
1412 bfad_hal_mem_release(bfad);
Jing Huang7725ccf2009-09-23 17:46:15 -07001413out_drv_init_failure:
Krishna Gudipati7c38c052011-04-14 16:50:35 -07001414 /* Remove the debugfs node for this bfad */
1415 kfree(bfad->regdata);
1416 bfad_debugfs_exit(&bfad->pport);
Jing Huang7725ccf2009-09-23 17:46:15 -07001417 mutex_lock(&bfad_mutex);
1418 bfad_inst--;
1419 list_del(&bfad->list_entry);
1420 mutex_unlock(&bfad_mutex);
1421 bfad_pci_uninit(pdev, bfad);
1422out_pci_init_failure:
1423 kfree(bfad->trcmod);
1424out_alloc_trace_failure:
1425 kfree(bfad);
1426out:
1427 return error;
1428}
1429
Jing Huang5fbe25c2010-10-18 17:17:23 -07001430/*
Jing Huang7725ccf2009-09-23 17:46:15 -07001431 * PCI remove entry.
1432 */
1433void
1434bfad_pci_remove(struct pci_dev *pdev)
1435{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001436 struct bfad_s *bfad = pci_get_drvdata(pdev);
1437 unsigned long flags;
Jing Huang7725ccf2009-09-23 17:46:15 -07001438
1439 bfa_trc(bfad, bfad->inst_no);
1440
Krishna Gudipatie6714322010-03-03 17:44:02 -08001441 spin_lock_irqsave(&bfad->bfad_lock, flags);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001442 if (bfad->bfad_tsk != NULL) {
1443 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
Krishna Gudipatie6714322010-03-03 17:44:02 -08001444 kthread_stop(bfad->bfad_tsk);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001445 } else {
Jing Huang7725ccf2009-09-23 17:46:15 -07001446 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
Jing Huang7725ccf2009-09-23 17:46:15 -07001447 }
1448
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001449 /* Send Event BFAD_E_STOP */
1450 bfa_sm_send_event(bfad, BFAD_E_STOP);
Jing Huang7725ccf2009-09-23 17:46:15 -07001451
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001452 /* Driver detach and dealloc mem */
Jing Huang7725ccf2009-09-23 17:46:15 -07001453 spin_lock_irqsave(&bfad->bfad_lock, flags);
1454 bfa_detach(&bfad->bfa);
1455 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1456 bfad_hal_mem_release(bfad);
Jing Huang7725ccf2009-09-23 17:46:15 -07001457
Krishna Gudipati7c38c052011-04-14 16:50:35 -07001458 /* Remove the debugfs node for this bfad */
1459 kfree(bfad->regdata);
1460 bfad_debugfs_exit(&bfad->pport);
1461
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001462 /* Cleaning the BFAD instance */
Jing Huang7725ccf2009-09-23 17:46:15 -07001463 mutex_lock(&bfad_mutex);
1464 bfad_inst--;
1465 list_del(&bfad->list_entry);
1466 mutex_unlock(&bfad_mutex);
1467 bfad_pci_uninit(pdev, bfad);
1468
1469 kfree(bfad->trcmod);
1470 kfree(bfad);
1471}
1472
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001473struct pci_device_id bfad_id_table[] = {
Jing Huang7725ccf2009-09-23 17:46:15 -07001474 {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001475 .vendor = BFA_PCI_VENDOR_ID_BROCADE,
1476 .device = BFA_PCI_DEVICE_ID_FC_8G2P,
1477 .subvendor = PCI_ANY_ID,
1478 .subdevice = PCI_ANY_ID,
1479 },
Jing Huang7725ccf2009-09-23 17:46:15 -07001480 {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001481 .vendor = BFA_PCI_VENDOR_ID_BROCADE,
1482 .device = BFA_PCI_DEVICE_ID_FC_8G1P,
1483 .subvendor = PCI_ANY_ID,
1484 .subdevice = PCI_ANY_ID,
1485 },
Jing Huang7725ccf2009-09-23 17:46:15 -07001486 {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001487 .vendor = BFA_PCI_VENDOR_ID_BROCADE,
1488 .device = BFA_PCI_DEVICE_ID_CT,
1489 .subvendor = PCI_ANY_ID,
1490 .subdevice = PCI_ANY_ID,
1491 .class = (PCI_CLASS_SERIAL_FIBER << 8),
1492 .class_mask = ~0,
1493 },
Jing Huang293f82d2010-07-08 19:45:20 -07001494 {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001495 .vendor = BFA_PCI_VENDOR_ID_BROCADE,
1496 .device = BFA_PCI_DEVICE_ID_CT_FC,
1497 .subvendor = PCI_ANY_ID,
1498 .subdevice = PCI_ANY_ID,
1499 .class = (PCI_CLASS_SERIAL_FIBER << 8),
1500 .class_mask = ~0,
Jing Huang293f82d2010-07-08 19:45:20 -07001501 },
Krishna Gudipati11189202011-06-13 15:50:35 -07001502 {
1503 .vendor = BFA_PCI_VENDOR_ID_BROCADE,
1504 .device = BFA_PCI_DEVICE_ID_CT2,
1505 .subvendor = PCI_ANY_ID,
1506 .subdevice = PCI_ANY_ID,
1507 .class = (PCI_CLASS_SERIAL_FIBER << 8),
1508 .class_mask = ~0,
1509 },
Jing Huang7725ccf2009-09-23 17:46:15 -07001510
1511 {0, 0},
1512};
1513
1514MODULE_DEVICE_TABLE(pci, bfad_id_table);
1515
1516static struct pci_driver bfad_pci_driver = {
1517 .name = BFAD_DRIVER_NAME,
1518 .id_table = bfad_id_table,
1519 .probe = bfad_pci_probe,
1520 .remove = __devexit_p(bfad_pci_remove),
1521};
1522
Jing Huang5fbe25c2010-10-18 17:17:23 -07001523/*
Jing Huang7725ccf2009-09-23 17:46:15 -07001524 * Driver module init.
1525 */
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001526static int __init
Jing Huang7725ccf2009-09-23 17:46:15 -07001527bfad_init(void)
1528{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001529 int error = 0;
Jing Huang7725ccf2009-09-23 17:46:15 -07001530
1531 printk(KERN_INFO "Brocade BFA FC/FCOE SCSI driver - version: %s\n",
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001532 BFAD_DRIVER_VERSION);
Jing Huang7725ccf2009-09-23 17:46:15 -07001533
1534 if (num_sgpgs > 0)
1535 num_sgpgs_parm = num_sgpgs;
1536
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001537 error = bfad_im_module_init();
Jing Huang7725ccf2009-09-23 17:46:15 -07001538 if (error) {
1539 error = -ENOMEM;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001540 printk(KERN_WARNING "bfad_im_module_init failure\n");
Jing Huang7725ccf2009-09-23 17:46:15 -07001541 goto ext;
1542 }
1543
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001544 if (strcmp(FCPI_NAME, " fcpim") == 0)
1545 supported_fc4s |= BFA_LPORT_ROLE_FCP_IM;
Jing Huang7725ccf2009-09-23 17:46:15 -07001546
Maggie Zhangf7f738122010-12-09 19:08:43 -08001547 bfa_auto_recover = ioc_auto_recover;
Jing Huang7725ccf2009-09-23 17:46:15 -07001548 bfa_fcs_rport_set_del_timeout(rport_del_timeout);
Jing Huang7725ccf2009-09-23 17:46:15 -07001549
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001550 error = pci_register_driver(&bfad_pci_driver);
Jing Huang7725ccf2009-09-23 17:46:15 -07001551 if (error) {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001552 printk(KERN_WARNING "pci_register_driver failure\n");
Jing Huang7725ccf2009-09-23 17:46:15 -07001553 goto ext;
1554 }
1555
1556 return 0;
1557
1558ext:
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001559 bfad_im_module_exit();
Jing Huang7725ccf2009-09-23 17:46:15 -07001560 return error;
1561}
1562
Jing Huang5fbe25c2010-10-18 17:17:23 -07001563/*
Jing Huang7725ccf2009-09-23 17:46:15 -07001564 * Driver module exit.
1565 */
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001566static void __exit
Jing Huang7725ccf2009-09-23 17:46:15 -07001567bfad_exit(void)
1568{
1569 pci_unregister_driver(&bfad_pci_driver);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001570 bfad_im_module_exit();
Jing Huang7725ccf2009-09-23 17:46:15 -07001571 bfad_free_fwimg();
1572}
1573
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001574/* Firmware handling */
Jing Huang61338a02011-04-13 11:44:03 -07001575static void
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001576bfad_read_firmware(struct pci_dev *pdev, u32 **bfi_image,
1577 u32 *bfi_image_size, char *fw_name)
1578{
1579 const struct firmware *fw;
1580
1581 if (request_firmware(&fw, fw_name, &pdev->dev)) {
1582 printk(KERN_ALERT "Can't locate firmware %s\n", fw_name);
Jing Huang61338a02011-04-13 11:44:03 -07001583 *bfi_image = NULL;
1584 goto out;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001585 }
1586
1587 *bfi_image = vmalloc(fw->size);
1588 if (NULL == *bfi_image) {
1589 printk(KERN_ALERT "Fail to allocate buffer for fw image "
1590 "size=%x!\n", (u32) fw->size);
Jing Huang61338a02011-04-13 11:44:03 -07001591 goto out;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001592 }
1593
1594 memcpy(*bfi_image, fw->data, fw->size);
1595 *bfi_image_size = fw->size/sizeof(u32);
Jing Huang61338a02011-04-13 11:44:03 -07001596out:
1597 release_firmware(fw);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001598}
1599
Jing Huang61338a02011-04-13 11:44:03 -07001600static u32 *
1601bfad_load_fwimg(struct pci_dev *pdev)
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001602{
Krishna Gudipati11189202011-06-13 15:50:35 -07001603 if (pdev->device == BFA_PCI_DEVICE_ID_CT2) {
1604 if (bfi_image_ct2_size == 0)
1605 bfad_read_firmware(pdev, &bfi_image_ct2,
1606 &bfi_image_ct2_size, BFAD_FW_FILE_CT2);
1607 return bfi_image_ct2;
1608 } else if (bfa_asic_id_ct(pdev->device)) {
1609 if (bfi_image_ct_size == 0)
1610 bfad_read_firmware(pdev, &bfi_image_ct,
1611 &bfi_image_ct_size, BFAD_FW_FILE_CT);
1612 return bfi_image_ct;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001613 } else {
Krishna Gudipati11189202011-06-13 15:50:35 -07001614 if (bfi_image_cb_size == 0)
1615 bfad_read_firmware(pdev, &bfi_image_cb,
1616 &bfi_image_cb_size, BFAD_FW_FILE_CB);
1617 return bfi_image_cb;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001618 }
1619}
Jing Huang7725ccf2009-09-23 17:46:15 -07001620
Jing Huang61338a02011-04-13 11:44:03 -07001621static void
1622bfad_free_fwimg(void)
1623{
Krishna Gudipati11189202011-06-13 15:50:35 -07001624 if (bfi_image_ct2_size && bfi_image_ct2)
1625 vfree(bfi_image_ct2);
1626 if (bfi_image_ct_size && bfi_image_ct)
1627 vfree(bfi_image_ct);
1628 if (bfi_image_cb_size && bfi_image_cb)
1629 vfree(bfi_image_cb);
Jing Huang61338a02011-04-13 11:44:03 -07001630}
1631
Jing Huang7725ccf2009-09-23 17:46:15 -07001632module_init(bfad_init);
1633module_exit(bfad_exit);
1634MODULE_LICENSE("GPL");
1635MODULE_DESCRIPTION("Brocade Fibre Channel HBA Driver" BFAD_PROTO_NAME);
1636MODULE_AUTHOR("Brocade Communications Systems, Inc.");
1637MODULE_VERSION(BFAD_DRIVER_VERSION);