blob: d9360bf18d33ce31fade8715dc02ee024e1508d6 [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;
59
Jing Huang61338a02011-04-13 11:44:03 -070060/* Firmware releated */
Krishna Gudipatia36c61f2010-09-15 11:50:55 -070061u32 bfi_image_ct_fc_size, bfi_image_ct_cna_size, bfi_image_cb_fc_size;
62u32 *bfi_image_ct_fc, *bfi_image_ct_cna, *bfi_image_cb_fc;
63
Jing Huang61338a02011-04-13 11:44:03 -070064#define BFAD_FW_FILE_CT_FC "ctfw_fc.bin"
65#define BFAD_FW_FILE_CT_CNA "ctfw_cna.bin"
66#define BFAD_FW_FILE_CB_FC "cbfw_fc.bin"
67
68static u32 *bfad_load_fwimg(struct pci_dev *pdev);
69static void bfad_free_fwimg(void);
70static void bfad_read_firmware(struct pci_dev *pdev, u32 **bfi_image,
71 u32 *bfi_image_size, char *fw_name);
72
Maggie52f94b62010-11-29 18:21:32 -080073static const char *msix_name_ct[] = {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -070074 "cpe0", "cpe1", "cpe2", "cpe3",
75 "rme0", "rme1", "rme2", "rme3",
76 "ctrl" };
77
Maggie52f94b62010-11-29 18:21:32 -080078static const char *msix_name_cb[] = {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -070079 "cpe0", "cpe1", "cpe2", "cpe3",
80 "rme0", "rme1", "rme2", "rme3",
81 "eemc", "elpu0", "elpu1", "epss", "mlpu" };
82
83MODULE_FIRMWARE(BFAD_FW_FILE_CT_FC);
84MODULE_FIRMWARE(BFAD_FW_FILE_CT_CNA);
85MODULE_FIRMWARE(BFAD_FW_FILE_CB_FC);
Jing Huang7725ccf2009-09-23 17:46:15 -070086
87module_param(os_name, charp, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -070088MODULE_PARM_DESC(os_name, "OS name of the hba host machine");
Jing Huang7725ccf2009-09-23 17:46:15 -070089module_param(os_patch, charp, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -070090MODULE_PARM_DESC(os_patch, "OS patch level of the hba host machine");
Jing Huang7725ccf2009-09-23 17:46:15 -070091module_param(host_name, charp, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -070092MODULE_PARM_DESC(host_name, "Hostname of the hba host machine");
Jing Huang7725ccf2009-09-23 17:46:15 -070093module_param(num_rports, int, S_IRUGO | S_IWUSR);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -070094MODULE_PARM_DESC(num_rports, "Max number of rports supported per port "
95 "(physical/logical), default=1024");
Jing Huang7725ccf2009-09-23 17:46:15 -070096module_param(num_ios, int, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -070097MODULE_PARM_DESC(num_ios, "Max number of ioim requests, default=2000");
Jing Huang7725ccf2009-09-23 17:46:15 -070098module_param(num_tms, int, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -070099MODULE_PARM_DESC(num_tms, "Max number of task im requests, default=128");
Jing Huang7725ccf2009-09-23 17:46:15 -0700100module_param(num_fcxps, int, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -0700101MODULE_PARM_DESC(num_fcxps, "Max number of fcxp requests, default=64");
Jing Huang7725ccf2009-09-23 17:46:15 -0700102module_param(num_ufbufs, int, S_IRUGO | S_IWUSR);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700103MODULE_PARM_DESC(num_ufbufs, "Max number of unsolicited frame "
104 "buffers, default=64");
Jing Huang7725ccf2009-09-23 17:46:15 -0700105module_param(reqq_size, int, S_IRUGO | S_IWUSR);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700106MODULE_PARM_DESC(reqq_size, "Max number of request queue elements, "
107 "default=256");
Jing Huang7725ccf2009-09-23 17:46:15 -0700108module_param(rspq_size, int, S_IRUGO | S_IWUSR);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700109MODULE_PARM_DESC(rspq_size, "Max number of response queue elements, "
110 "default=64");
Jing Huang7725ccf2009-09-23 17:46:15 -0700111module_param(num_sgpgs, int, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -0700112MODULE_PARM_DESC(num_sgpgs, "Number of scatter/gather pages, default=2048");
Jing Huang7725ccf2009-09-23 17:46:15 -0700113module_param(rport_del_timeout, int, S_IRUGO | S_IWUSR);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700114MODULE_PARM_DESC(rport_del_timeout, "Rport delete timeout, default=90 secs, "
115 "Range[>0]");
Jing Huang7725ccf2009-09-23 17:46:15 -0700116module_param(bfa_lun_queue_depth, int, S_IRUGO | S_IWUSR);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700117MODULE_PARM_DESC(bfa_lun_queue_depth, "Lun queue depth, default=32, Range[>0]");
Jing Huang7725ccf2009-09-23 17:46:15 -0700118module_param(bfa_io_max_sge, int, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -0700119MODULE_PARM_DESC(bfa_io_max_sge, "Max io scatter/gather elements, default=255");
Jing Huang88166242010-12-09 17:11:53 -0800120module_param(bfa_log_level, int, S_IRUGO | S_IWUSR);
121MODULE_PARM_DESC(bfa_log_level, "Driver log level, default=3, "
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700122 "Range[Critical:1|Error:2|Warning:3|Info:4]");
Jing Huang7725ccf2009-09-23 17:46:15 -0700123module_param(ioc_auto_recover, int, S_IRUGO | S_IWUSR);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700124MODULE_PARM_DESC(ioc_auto_recover, "IOC auto recovery, default=1, "
125 "Range[off:0|on:1]");
Jing Huang7725ccf2009-09-23 17:46:15 -0700126module_param(bfa_linkup_delay, int, S_IRUGO | S_IWUSR);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700127MODULE_PARM_DESC(bfa_linkup_delay, "Link up delay, default=30 secs for "
128 "boot port. Otherwise 10 secs in RHEL4 & 0 for "
129 "[RHEL5, SLES10, ESX40] Range[>0]");
130module_param(msix_disable_cb, int, S_IRUGO | S_IWUSR);
131MODULE_PARM_DESC(msix_disable_cb, "Disable Message Signaled Interrupts "
132 "for Brocade-415/425/815/825 cards, default=0, "
133 " Range[false:0|true:1]");
134module_param(msix_disable_ct, int, S_IRUGO | S_IWUSR);
135MODULE_PARM_DESC(msix_disable_ct, "Disable Message Signaled Interrupts "
136 "if possible for Brocade-1010/1020/804/1007/902/1741 "
137 "cards, default=0, Range[false:0|true:1]");
Jing Huang604158ad2010-07-08 19:59:49 -0700138module_param(fdmi_enable, int, S_IRUGO | S_IWUSR);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700139MODULE_PARM_DESC(fdmi_enable, "Enables fdmi registration, default=1, "
140 "Range[false:0|true:1]");
141module_param(pcie_max_read_reqsz, int, S_IRUGO | S_IWUSR);
142MODULE_PARM_DESC(pcie_max_read_reqsz, "PCIe max read request size, default=0 "
143 "(use system setting), Range[128|256|512|1024|2048|4096]");
Jing Huangab2a9ba2010-07-08 20:02:55 -0700144module_param(bfa_debugfs_enable, int, S_IRUGO | S_IWUSR);
145MODULE_PARM_DESC(bfa_debugfs_enable, "Enables debugfs feature, default=1,"
146 " Range[false:0|true:1]");
Jing Huang7725ccf2009-09-23 17:46:15 -0700147
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700148static void
149bfad_sm_uninit(struct bfad_s *bfad, enum bfad_sm_event event);
150static void
151bfad_sm_created(struct bfad_s *bfad, enum bfad_sm_event event);
152static void
153bfad_sm_initializing(struct bfad_s *bfad, enum bfad_sm_event event);
154static void
155bfad_sm_operational(struct bfad_s *bfad, enum bfad_sm_event event);
156static void
157bfad_sm_stopping(struct bfad_s *bfad, enum bfad_sm_event event);
158static void
159bfad_sm_failed(struct bfad_s *bfad, enum bfad_sm_event event);
160static void
161bfad_sm_fcs_exit(struct bfad_s *bfad, enum bfad_sm_event event);
162
Jing Huang5fbe25c2010-10-18 17:17:23 -0700163/*
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700164 * Beginning state for the driver instance, awaiting the pci_probe event
Jing Huang7725ccf2009-09-23 17:46:15 -0700165 */
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700166static void
167bfad_sm_uninit(struct bfad_s *bfad, enum bfad_sm_event event)
Jing Huang7725ccf2009-09-23 17:46:15 -0700168{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700169 bfa_trc(bfad, event);
Jing Huang7725ccf2009-09-23 17:46:15 -0700170
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700171 switch (event) {
172 case BFAD_E_CREATE:
173 bfa_sm_set_state(bfad, bfad_sm_created);
174 bfad->bfad_tsk = kthread_create(bfad_worker, (void *) bfad,
175 "%s", "bfad_worker");
176 if (IS_ERR(bfad->bfad_tsk)) {
177 printk(KERN_INFO "bfad[%d]: Kernel thread "
178 "creation failed!\n", bfad->inst_no);
179 bfa_sm_send_event(bfad, BFAD_E_KTHREAD_CREATE_FAILED);
180 }
181 bfa_sm_send_event(bfad, BFAD_E_INIT);
182 break;
Jing Huang7725ccf2009-09-23 17:46:15 -0700183
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700184 case BFAD_E_STOP:
185 /* Ignore stop; already in uninit */
186 break;
Jing Huang7725ccf2009-09-23 17:46:15 -0700187
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700188 default:
189 bfa_sm_fault(bfad, event);
190 }
191}
Krishna Gudipatie6714322010-03-03 17:44:02 -0800192
Jing Huang5fbe25c2010-10-18 17:17:23 -0700193/*
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700194 * Driver Instance is created, awaiting event INIT to initialize the bfad
195 */
196static void
197bfad_sm_created(struct bfad_s *bfad, enum bfad_sm_event event)
198{
199 unsigned long flags;
200
201 bfa_trc(bfad, event);
202
203 switch (event) {
204 case BFAD_E_INIT:
205 bfa_sm_set_state(bfad, bfad_sm_initializing);
206
207 init_completion(&bfad->comp);
208
209 /* Enable Interrupt and wait bfa_init completion */
210 if (bfad_setup_intr(bfad)) {
211 printk(KERN_WARNING "bfad%d: bfad_setup_intr failed\n",
212 bfad->inst_no);
213 bfa_sm_send_event(bfad, BFAD_E_INTR_INIT_FAILED);
214 break;
215 }
216
217 spin_lock_irqsave(&bfad->bfad_lock, flags);
Maggie Zhangf7f738122010-12-09 19:08:43 -0800218 bfa_iocfc_init(&bfad->bfa);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700219 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
220
221 /* Set up interrupt handler for each vectors */
222 if ((bfad->bfad_flags & BFAD_MSIX_ON) &&
223 bfad_install_msix_handler(bfad)) {
224 printk(KERN_WARNING "%s: install_msix failed, bfad%d\n",
225 __func__, bfad->inst_no);
226 }
227
228 bfad_init_timer(bfad);
229
230 wait_for_completion(&bfad->comp);
231
232 if ((bfad->bfad_flags & BFAD_HAL_INIT_DONE)) {
233 bfa_sm_send_event(bfad, BFAD_E_INIT_SUCCESS);
234 } else {
235 bfad->bfad_flags |= BFAD_HAL_INIT_FAIL;
236 bfa_sm_send_event(bfad, BFAD_E_INIT_FAILED);
237 }
238
239 break;
240
241 case BFAD_E_KTHREAD_CREATE_FAILED:
242 bfa_sm_set_state(bfad, bfad_sm_uninit);
243 break;
244
245 default:
246 bfa_sm_fault(bfad, event);
247 }
Jing Huang7725ccf2009-09-23 17:46:15 -0700248}
249
250static void
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700251bfad_sm_initializing(struct bfad_s *bfad, enum bfad_sm_event event)
Jing Huang7725ccf2009-09-23 17:46:15 -0700252{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700253 int retval;
254 unsigned long flags;
255
256 bfa_trc(bfad, event);
257
258 switch (event) {
259 case BFAD_E_INIT_SUCCESS:
260 kthread_stop(bfad->bfad_tsk);
261 spin_lock_irqsave(&bfad->bfad_lock, flags);
262 bfad->bfad_tsk = NULL;
263 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
264
265 retval = bfad_start_ops(bfad);
266 if (retval != BFA_STATUS_OK)
267 break;
268 bfa_sm_set_state(bfad, bfad_sm_operational);
269 break;
270
271 case BFAD_E_INTR_INIT_FAILED:
272 bfa_sm_set_state(bfad, bfad_sm_uninit);
273 kthread_stop(bfad->bfad_tsk);
274 spin_lock_irqsave(&bfad->bfad_lock, flags);
275 bfad->bfad_tsk = NULL;
276 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
277 break;
278
279 case BFAD_E_INIT_FAILED:
280 bfa_sm_set_state(bfad, bfad_sm_failed);
281 break;
282 default:
283 bfa_sm_fault(bfad, event);
284 }
Jing Huang7725ccf2009-09-23 17:46:15 -0700285}
286
287static void
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700288bfad_sm_failed(struct bfad_s *bfad, enum bfad_sm_event event)
Jing Huang7725ccf2009-09-23 17:46:15 -0700289{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700290 int retval;
Jing Huang7725ccf2009-09-23 17:46:15 -0700291
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700292 bfa_trc(bfad, event);
Jing Huang7725ccf2009-09-23 17:46:15 -0700293
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700294 switch (event) {
295 case BFAD_E_INIT_SUCCESS:
296 retval = bfad_start_ops(bfad);
297 if (retval != BFA_STATUS_OK)
298 break;
299 bfa_sm_set_state(bfad, bfad_sm_operational);
300 break;
Jing Huang7725ccf2009-09-23 17:46:15 -0700301
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700302 case BFAD_E_STOP:
303 if (bfad->bfad_flags & BFAD_CFG_PPORT_DONE)
304 bfad_uncfg_pport(bfad);
305 if (bfad->bfad_flags & BFAD_FC4_PROBE_DONE) {
306 bfad_im_probe_undo(bfad);
307 bfad->bfad_flags &= ~BFAD_FC4_PROBE_DONE;
308 }
309 bfad_stop(bfad);
310 break;
Jing Huang7725ccf2009-09-23 17:46:15 -0700311
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700312 case BFAD_E_EXIT_COMP:
313 bfa_sm_set_state(bfad, bfad_sm_uninit);
314 bfad_remove_intr(bfad);
315 del_timer_sync(&bfad->hal_tmo);
316 break;
Jing Huang7725ccf2009-09-23 17:46:15 -0700317
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700318 default:
319 bfa_sm_fault(bfad, event);
320 }
Jing Huang7725ccf2009-09-23 17:46:15 -0700321}
322
323static void
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700324bfad_sm_operational(struct bfad_s *bfad, enum bfad_sm_event event)
Jing Huang7725ccf2009-09-23 17:46:15 -0700325{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700326 bfa_trc(bfad, event);
Jing Huang7725ccf2009-09-23 17:46:15 -0700327
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700328 switch (event) {
329 case BFAD_E_STOP:
330 bfa_sm_set_state(bfad, bfad_sm_fcs_exit);
331 bfad_fcs_stop(bfad);
332 break;
Jing Huang7725ccf2009-09-23 17:46:15 -0700333
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700334 default:
335 bfa_sm_fault(bfad, event);
336 }
337}
338
339static void
340bfad_sm_fcs_exit(struct bfad_s *bfad, enum bfad_sm_event event)
341{
342 bfa_trc(bfad, event);
343
344 switch (event) {
345 case BFAD_E_FCS_EXIT_COMP:
346 bfa_sm_set_state(bfad, bfad_sm_stopping);
347 bfad_stop(bfad);
348 break;
349
350 default:
351 bfa_sm_fault(bfad, event);
352 }
353}
354
355static void
356bfad_sm_stopping(struct bfad_s *bfad, enum bfad_sm_event event)
357{
358 bfa_trc(bfad, event);
359
360 switch (event) {
361 case BFAD_E_EXIT_COMP:
362 bfa_sm_set_state(bfad, bfad_sm_uninit);
363 bfad_remove_intr(bfad);
364 del_timer_sync(&bfad->hal_tmo);
365 bfad_im_probe_undo(bfad);
366 bfad->bfad_flags &= ~BFAD_FC4_PROBE_DONE;
367 bfad_uncfg_pport(bfad);
368 break;
369
370 default:
371 bfa_sm_fault(bfad, event);
372 break;
373 }
Jing Huang7725ccf2009-09-23 17:46:15 -0700374}
375
Jing Huang5fbe25c2010-10-18 17:17:23 -0700376/*
Jing Huang7725ccf2009-09-23 17:46:15 -0700377 * BFA callbacks
378 */
379void
380bfad_hcb_comp(void *arg, bfa_status_t status)
381{
382 struct bfad_hal_comp *fcomp = (struct bfad_hal_comp *)arg;
383
384 fcomp->status = status;
385 complete(&fcomp->comp);
386}
387
Jing Huang5fbe25c2010-10-18 17:17:23 -0700388/*
Jing Huang7725ccf2009-09-23 17:46:15 -0700389 * bfa_init callback
390 */
391void
392bfa_cb_init(void *drv, bfa_status_t init_status)
393{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700394 struct bfad_s *bfad = drv;
Jing Huang7725ccf2009-09-23 17:46:15 -0700395
Krishna Gudipatie6714322010-03-03 17:44:02 -0800396 if (init_status == BFA_STATUS_OK) {
Jing Huang7725ccf2009-09-23 17:46:15 -0700397 bfad->bfad_flags |= BFAD_HAL_INIT_DONE;
398
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700399 /*
400 * If BFAD_HAL_INIT_FAIL flag is set:
Krishna Gudipatie6714322010-03-03 17:44:02 -0800401 * Wake up the kernel thread to start
402 * the bfad operations after HAL init done
403 */
404 if ((bfad->bfad_flags & BFAD_HAL_INIT_FAIL)) {
405 bfad->bfad_flags &= ~BFAD_HAL_INIT_FAIL;
406 wake_up_process(bfad->bfad_tsk);
407 }
408 }
409
Jing Huang7725ccf2009-09-23 17:46:15 -0700410 complete(&bfad->comp);
411}
412
Jing Huang5fbe25c2010-10-18 17:17:23 -0700413/*
Jing Huang7725ccf2009-09-23 17:46:15 -0700414 * BFA_FCS callbacks
415 */
Jing Huang7725ccf2009-09-23 17:46:15 -0700416struct bfad_port_s *
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700417bfa_fcb_lport_new(struct bfad_s *bfad, struct bfa_fcs_lport_s *port,
418 enum bfa_lport_role roles, struct bfad_vf_s *vf_drv,
Jing Huang7725ccf2009-09-23 17:46:15 -0700419 struct bfad_vport_s *vp_drv)
420{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700421 bfa_status_t rc;
422 struct bfad_port_s *port_drv;
Jing Huang7725ccf2009-09-23 17:46:15 -0700423
424 if (!vp_drv && !vf_drv) {
425 port_drv = &bfad->pport;
426 port_drv->pvb_type = BFAD_PORT_PHYS_BASE;
427 } else if (!vp_drv && vf_drv) {
428 port_drv = &vf_drv->base_port;
429 port_drv->pvb_type = BFAD_PORT_VF_BASE;
430 } else if (vp_drv && !vf_drv) {
431 port_drv = &vp_drv->drv_port;
432 port_drv->pvb_type = BFAD_PORT_PHYS_VPORT;
433 } else {
434 port_drv = &vp_drv->drv_port;
435 port_drv->pvb_type = BFAD_PORT_VF_VPORT;
436 }
437
438 port_drv->fcs_port = port;
439 port_drv->roles = roles;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700440
441 if (roles & BFA_LPORT_ROLE_FCP_IM) {
442 rc = bfad_im_port_new(bfad, port_drv);
443 if (rc != BFA_STATUS_OK) {
444 bfad_im_port_delete(bfad, port_drv);
445 port_drv = NULL;
446 }
Jing Huang7725ccf2009-09-23 17:46:15 -0700447 }
448
449 return port_drv;
450}
451
452void
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700453bfa_fcb_lport_delete(struct bfad_s *bfad, enum bfa_lport_role roles,
Jing Huang7725ccf2009-09-23 17:46:15 -0700454 struct bfad_vf_s *vf_drv, struct bfad_vport_s *vp_drv)
455{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700456 struct bfad_port_s *port_drv;
Jing Huang7725ccf2009-09-23 17:46:15 -0700457
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700458 /* this will be only called from rmmod context */
Jing Huang7725ccf2009-09-23 17:46:15 -0700459 if (vp_drv && !vp_drv->comp_del) {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700460 port_drv = (vp_drv) ? (&(vp_drv)->drv_port) :
461 ((vf_drv) ? (&(vf_drv)->base_port) :
462 (&(bfad)->pport));
Jing Huang7725ccf2009-09-23 17:46:15 -0700463 bfa_trc(bfad, roles);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700464 if (roles & BFA_LPORT_ROLE_FCP_IM)
465 bfad_im_port_delete(bfad, port_drv);
Jing Huang7725ccf2009-09-23 17:46:15 -0700466 }
Jing Huang7725ccf2009-09-23 17:46:15 -0700467}
468
Jing Huang5fbe25c2010-10-18 17:17:23 -0700469/*
Jing Huang7725ccf2009-09-23 17:46:15 -0700470 * FCS RPORT alloc callback, after successful PLOGI by FCS
471 */
472bfa_status_t
473bfa_fcb_rport_alloc(struct bfad_s *bfad, struct bfa_fcs_rport_s **rport,
474 struct bfad_rport_s **rport_drv)
475{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700476 bfa_status_t rc = BFA_STATUS_OK;
Jing Huang7725ccf2009-09-23 17:46:15 -0700477
478 *rport_drv = kzalloc(sizeof(struct bfad_rport_s), GFP_ATOMIC);
479 if (*rport_drv == NULL) {
480 rc = BFA_STATUS_ENOMEM;
481 goto ext;
482 }
483
484 *rport = &(*rport_drv)->fcs_rport;
485
486ext:
487 return rc;
488}
489
Jing Huang5fbe25c2010-10-18 17:17:23 -0700490/*
Jing Huangd9883542010-07-08 19:46:26 -0700491 * FCS PBC VPORT Create
492 */
493void
494bfa_fcb_pbc_vport_create(struct bfad_s *bfad, struct bfi_pbc_vport_s pbc_vport)
495{
Jing Huang7725ccf2009-09-23 17:46:15 -0700496
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700497 struct bfa_lport_cfg_s port_cfg = {0};
498 struct bfad_vport_s *vport;
499 int rc;
Jing Huangd9883542010-07-08 19:46:26 -0700500
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700501 vport = kzalloc(sizeof(struct bfad_vport_s), GFP_KERNEL);
502 if (!vport) {
Jing Huangd9883542010-07-08 19:46:26 -0700503 bfa_trc(bfad, 0);
504 return;
505 }
506
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700507 vport->drv_port.bfad = bfad;
508 port_cfg.roles = BFA_LPORT_ROLE_FCP_IM;
509 port_cfg.pwwn = pbc_vport.vp_pwwn;
510 port_cfg.nwwn = pbc_vport.vp_nwwn;
511 port_cfg.preboot_vp = BFA_TRUE;
Jing Huangd9883542010-07-08 19:46:26 -0700512
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700513 rc = bfa_fcs_pbc_vport_create(&vport->fcs_vport, &bfad->bfa_fcs, 0,
514 &port_cfg, vport);
Jing Huangd9883542010-07-08 19:46:26 -0700515
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700516 if (rc != BFA_STATUS_OK) {
517 bfa_trc(bfad, 0);
518 return;
519 }
520
521 list_add_tail(&vport->list_entry, &bfad->pbc_vport_list);
Jing Huangd9883542010-07-08 19:46:26 -0700522}
Jing Huang7725ccf2009-09-23 17:46:15 -0700523
524void
525bfad_hal_mem_release(struct bfad_s *bfad)
526{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700527 int i;
Jing Huang7725ccf2009-09-23 17:46:15 -0700528 struct bfa_meminfo_s *hal_meminfo = &bfad->meminfo;
529 struct bfa_mem_elem_s *meminfo_elem;
530
531 for (i = 0; i < BFA_MEM_TYPE_MAX; i++) {
532 meminfo_elem = &hal_meminfo->meminfo[i];
533 if (meminfo_elem->kva != NULL) {
534 switch (meminfo_elem->mem_type) {
535 case BFA_MEM_TYPE_KVA:
536 vfree(meminfo_elem->kva);
537 break;
538 case BFA_MEM_TYPE_DMA:
539 dma_free_coherent(&bfad->pcidev->dev,
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700540 meminfo_elem->mem_len,
541 meminfo_elem->kva,
542 (dma_addr_t) meminfo_elem->dma);
Jing Huang7725ccf2009-09-23 17:46:15 -0700543 break;
544 default:
Jing Huangd4b671c2010-12-26 21:46:35 -0800545 WARN_ON(1);
Jing Huang7725ccf2009-09-23 17:46:15 -0700546 break;
547 }
548 }
549 }
550
551 memset(hal_meminfo, 0, sizeof(struct bfa_meminfo_s));
552}
553
554void
555bfad_update_hal_cfg(struct bfa_iocfc_cfg_s *bfa_cfg)
556{
557 if (num_rports > 0)
558 bfa_cfg->fwcfg.num_rports = num_rports;
559 if (num_ios > 0)
560 bfa_cfg->fwcfg.num_ioim_reqs = num_ios;
561 if (num_tms > 0)
562 bfa_cfg->fwcfg.num_tskim_reqs = num_tms;
563 if (num_fcxps > 0)
564 bfa_cfg->fwcfg.num_fcxp_reqs = num_fcxps;
565 if (num_ufbufs > 0)
566 bfa_cfg->fwcfg.num_uf_bufs = num_ufbufs;
567 if (reqq_size > 0)
568 bfa_cfg->drvcfg.num_reqq_elems = reqq_size;
569 if (rspq_size > 0)
570 bfa_cfg->drvcfg.num_rspq_elems = rspq_size;
571 if (num_sgpgs > 0)
572 bfa_cfg->drvcfg.num_sgpgs = num_sgpgs;
573
574 /*
575 * populate the hal values back to the driver for sysfs use.
576 * otherwise, the default values will be shown as 0 in sysfs
577 */
578 num_rports = bfa_cfg->fwcfg.num_rports;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700579 num_ios = bfa_cfg->fwcfg.num_ioim_reqs;
580 num_tms = bfa_cfg->fwcfg.num_tskim_reqs;
581 num_fcxps = bfa_cfg->fwcfg.num_fcxp_reqs;
Jing Huang7725ccf2009-09-23 17:46:15 -0700582 num_ufbufs = bfa_cfg->fwcfg.num_uf_bufs;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700583 reqq_size = bfa_cfg->drvcfg.num_reqq_elems;
584 rspq_size = bfa_cfg->drvcfg.num_rspq_elems;
585 num_sgpgs = bfa_cfg->drvcfg.num_sgpgs;
Jing Huang7725ccf2009-09-23 17:46:15 -0700586}
587
588bfa_status_t
589bfad_hal_mem_alloc(struct bfad_s *bfad)
590{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700591 int i;
Jing Huang7725ccf2009-09-23 17:46:15 -0700592 struct bfa_meminfo_s *hal_meminfo = &bfad->meminfo;
593 struct bfa_mem_elem_s *meminfo_elem;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700594 dma_addr_t phys_addr;
595 void *kva;
596 bfa_status_t rc = BFA_STATUS_OK;
597 int retry_count = 0;
598 int reset_value = 1;
599 int min_num_sgpgs = 512;
Jing Huang7725ccf2009-09-23 17:46:15 -0700600
601 bfa_cfg_get_default(&bfad->ioc_cfg);
602
603retry:
604 bfad_update_hal_cfg(&bfad->ioc_cfg);
605 bfad->cfg_data.ioc_queue_depth = bfad->ioc_cfg.fwcfg.num_ioim_reqs;
606 bfa_cfg_get_meminfo(&bfad->ioc_cfg, hal_meminfo);
607
608 for (i = 0; i < BFA_MEM_TYPE_MAX; i++) {
609 meminfo_elem = &hal_meminfo->meminfo[i];
610 switch (meminfo_elem->mem_type) {
611 case BFA_MEM_TYPE_KVA:
612 kva = vmalloc(meminfo_elem->mem_len);
613 if (kva == NULL) {
614 bfad_hal_mem_release(bfad);
615 rc = BFA_STATUS_ENOMEM;
616 goto ext;
617 }
618 memset(kva, 0, meminfo_elem->mem_len);
619 meminfo_elem->kva = kva;
620 break;
621 case BFA_MEM_TYPE_DMA:
622 kva = dma_alloc_coherent(&bfad->pcidev->dev,
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700623 meminfo_elem->mem_len, &phys_addr, GFP_KERNEL);
Jing Huang7725ccf2009-09-23 17:46:15 -0700624 if (kva == NULL) {
625 bfad_hal_mem_release(bfad);
626 /*
627 * If we cannot allocate with default
628 * num_sgpages try with half the value.
629 */
630 if (num_sgpgs > min_num_sgpgs) {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700631 printk(KERN_INFO
632 "bfad[%d]: memory allocation failed"
633 " with num_sgpgs: %d\n",
Jing Huang7725ccf2009-09-23 17:46:15 -0700634 bfad->inst_no, num_sgpgs);
635 nextLowerInt(&num_sgpgs);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700636 printk(KERN_INFO
637 "bfad[%d]: trying to allocate memory"
638 " with num_sgpgs: %d\n",
Jing Huang7725ccf2009-09-23 17:46:15 -0700639 bfad->inst_no, num_sgpgs);
640 retry_count++;
641 goto retry;
642 } else {
643 if (num_sgpgs_parm > 0)
644 num_sgpgs = num_sgpgs_parm;
645 else {
646 reset_value =
647 (1 << retry_count);
648 num_sgpgs *= reset_value;
649 }
650 rc = BFA_STATUS_ENOMEM;
651 goto ext;
652 }
653 }
654
655 if (num_sgpgs_parm > 0)
656 num_sgpgs = num_sgpgs_parm;
657 else {
658 reset_value = (1 << retry_count);
659 num_sgpgs *= reset_value;
660 }
661
662 memset(kva, 0, meminfo_elem->mem_len);
663 meminfo_elem->kva = kva;
664 meminfo_elem->dma = phys_addr;
665 break;
666 default:
667 break;
668
669 }
670 }
671ext:
672 return rc;
673}
674
Jing Huang5fbe25c2010-10-18 17:17:23 -0700675/*
Jing Huang7725ccf2009-09-23 17:46:15 -0700676 * Create a vport under a vf.
677 */
678bfa_status_t
679bfad_vport_create(struct bfad_s *bfad, u16 vf_id,
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700680 struct bfa_lport_cfg_s *port_cfg, struct device *dev)
Jing Huang7725ccf2009-09-23 17:46:15 -0700681{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700682 struct bfad_vport_s *vport;
683 int rc = BFA_STATUS_OK;
684 unsigned long flags;
Jing Huang7725ccf2009-09-23 17:46:15 -0700685 struct completion fcomp;
686
687 vport = kzalloc(sizeof(struct bfad_vport_s), GFP_KERNEL);
688 if (!vport) {
689 rc = BFA_STATUS_ENOMEM;
690 goto ext;
691 }
692
693 vport->drv_port.bfad = bfad;
694 spin_lock_irqsave(&bfad->bfad_lock, flags);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700695 rc = bfa_fcs_vport_create(&vport->fcs_vport, &bfad->bfa_fcs, vf_id,
696 port_cfg, vport);
Jing Huang7725ccf2009-09-23 17:46:15 -0700697 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
698
699 if (rc != BFA_STATUS_OK)
700 goto ext_free_vport;
701
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700702 if (port_cfg->roles & BFA_LPORT_ROLE_FCP_IM) {
Jing Huangb5042932010-03-19 11:05:39 -0700703 rc = bfad_im_scsi_host_alloc(bfad, vport->drv_port.im_port,
704 dev);
Jing Huang7725ccf2009-09-23 17:46:15 -0700705 if (rc != BFA_STATUS_OK)
706 goto ext_free_fcs_vport;
707 }
708
709 spin_lock_irqsave(&bfad->bfad_lock, flags);
710 bfa_fcs_vport_start(&vport->fcs_vport);
711 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
712
713 return BFA_STATUS_OK;
714
715ext_free_fcs_vport:
716 spin_lock_irqsave(&bfad->bfad_lock, flags);
717 vport->comp_del = &fcomp;
718 init_completion(vport->comp_del);
719 bfa_fcs_vport_delete(&vport->fcs_vport);
720 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
721 wait_for_completion(vport->comp_del);
722ext_free_vport:
723 kfree(vport);
724ext:
725 return rc;
726}
727
Jing Huang7725ccf2009-09-23 17:46:15 -0700728void
729bfad_bfa_tmo(unsigned long data)
730{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700731 struct bfad_s *bfad = (struct bfad_s *) data;
732 unsigned long flags;
733 struct list_head doneq;
Jing Huang7725ccf2009-09-23 17:46:15 -0700734
735 spin_lock_irqsave(&bfad->bfad_lock, flags);
736
Maggie Zhangf7f738122010-12-09 19:08:43 -0800737 bfa_timer_beat(&bfad->bfa.timer_mod);
Jing Huang7725ccf2009-09-23 17:46:15 -0700738
739 bfa_comp_deq(&bfad->bfa, &doneq);
740 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
741
742 if (!list_empty(&doneq)) {
743 bfa_comp_process(&bfad->bfa, &doneq);
744 spin_lock_irqsave(&bfad->bfad_lock, flags);
745 bfa_comp_free(&bfad->bfa, &doneq);
746 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
747 }
748
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700749 mod_timer(&bfad->hal_tmo,
750 jiffies + msecs_to_jiffies(BFA_TIMER_FREQ));
Jing Huang7725ccf2009-09-23 17:46:15 -0700751}
752
753void
754bfad_init_timer(struct bfad_s *bfad)
755{
756 init_timer(&bfad->hal_tmo);
757 bfad->hal_tmo.function = bfad_bfa_tmo;
758 bfad->hal_tmo.data = (unsigned long)bfad;
759
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700760 mod_timer(&bfad->hal_tmo,
761 jiffies + msecs_to_jiffies(BFA_TIMER_FREQ));
Jing Huang7725ccf2009-09-23 17:46:15 -0700762}
763
764int
765bfad_pci_init(struct pci_dev *pdev, struct bfad_s *bfad)
766{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700767 int rc = -ENODEV;
Jing Huang7725ccf2009-09-23 17:46:15 -0700768
769 if (pci_enable_device(pdev)) {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700770 printk(KERN_ERR "pci_enable_device fail %p\n", pdev);
Jing Huang7725ccf2009-09-23 17:46:15 -0700771 goto out;
772 }
773
774 if (pci_request_regions(pdev, BFAD_DRIVER_NAME))
775 goto out_disable_device;
776
777 pci_set_master(pdev);
778
779
780 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0)
781 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0) {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700782 printk(KERN_ERR "pci_set_dma_mask fail %p\n", pdev);
Jing Huang7725ccf2009-09-23 17:46:15 -0700783 goto out_release_region;
784 }
785
Jing Huangb3522f02010-03-19 11:06:44 -0700786 bfad->pci_bar0_kva = pci_iomap(pdev, 0, pci_resource_len(pdev, 0));
Jing Huang7725ccf2009-09-23 17:46:15 -0700787
788 if (bfad->pci_bar0_kva == NULL) {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700789 printk(KERN_ERR "Fail to map bar0\n");
Jing Huang7725ccf2009-09-23 17:46:15 -0700790 goto out_release_region;
791 }
792
793 bfad->hal_pcidev.pci_slot = PCI_SLOT(pdev->devfn);
794 bfad->hal_pcidev.pci_func = PCI_FUNC(pdev->devfn);
795 bfad->hal_pcidev.pci_bar_kva = bfad->pci_bar0_kva;
796 bfad->hal_pcidev.device_id = pdev->device;
797 bfad->pci_name = pci_name(pdev);
798
799 bfad->pci_attr.vendor_id = pdev->vendor;
800 bfad->pci_attr.device_id = pdev->device;
801 bfad->pci_attr.ssid = pdev->subsystem_device;
802 bfad->pci_attr.ssvid = pdev->subsystem_vendor;
803 bfad->pci_attr.pcifn = PCI_FUNC(pdev->devfn);
804
805 bfad->pcidev = pdev;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700806
807 /* Adjust PCIe Maximum Read Request Size */
808 if (pcie_max_read_reqsz > 0) {
809 int pcie_cap_reg;
810 u16 pcie_dev_ctl;
811 u16 mask = 0xffff;
812
813 switch (pcie_max_read_reqsz) {
814 case 128:
815 mask = 0x0;
816 break;
817 case 256:
818 mask = 0x1000;
819 break;
820 case 512:
821 mask = 0x2000;
822 break;
823 case 1024:
824 mask = 0x3000;
825 break;
826 case 2048:
827 mask = 0x4000;
828 break;
829 case 4096:
830 mask = 0x5000;
831 break;
832 default:
833 break;
834 }
835
836 pcie_cap_reg = pci_find_capability(pdev, PCI_CAP_ID_EXP);
837 if (mask != 0xffff && pcie_cap_reg) {
838 pcie_cap_reg += 0x08;
839 pci_read_config_word(pdev, pcie_cap_reg, &pcie_dev_ctl);
840 if ((pcie_dev_ctl & 0x7000) != mask) {
841 printk(KERN_WARNING "BFA[%s]: "
842 "pcie_max_read_request_size is %d, "
843 "reset to %d\n", bfad->pci_name,
844 (1 << ((pcie_dev_ctl & 0x7000) >> 12)) << 7,
845 pcie_max_read_reqsz);
846
847 pcie_dev_ctl &= ~0x7000;
848 pci_write_config_word(pdev, pcie_cap_reg,
849 pcie_dev_ctl | mask);
850 }
851 }
852 }
853
Jing Huang7725ccf2009-09-23 17:46:15 -0700854 return 0;
855
856out_release_region:
857 pci_release_regions(pdev);
858out_disable_device:
859 pci_disable_device(pdev);
860out:
861 return rc;
862}
863
864void
865bfad_pci_uninit(struct pci_dev *pdev, struct bfad_s *bfad)
866{
Jing Huang7725ccf2009-09-23 17:46:15 -0700867 pci_iounmap(pdev, bfad->pci_bar0_kva);
Jing Huang7725ccf2009-09-23 17:46:15 -0700868 pci_release_regions(pdev);
869 pci_disable_device(pdev);
870 pci_set_drvdata(pdev, NULL);
871}
872
Jing Huang7725ccf2009-09-23 17:46:15 -0700873bfa_status_t
874bfad_drv_init(struct bfad_s *bfad)
875{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700876 bfa_status_t rc;
877 unsigned long flags;
Jing Huang7725ccf2009-09-23 17:46:15 -0700878
879 bfad->cfg_data.rport_del_timeout = rport_del_timeout;
880 bfad->cfg_data.lun_queue_depth = bfa_lun_queue_depth;
881 bfad->cfg_data.io_max_sge = bfa_io_max_sge;
882 bfad->cfg_data.binding_method = FCP_PWWN_BINDING;
883
884 rc = bfad_hal_mem_alloc(bfad);
885 if (rc != BFA_STATUS_OK) {
886 printk(KERN_WARNING "bfad%d bfad_hal_mem_alloc failure\n",
887 bfad->inst_no);
888 printk(KERN_WARNING
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700889 "Not enough memory to attach all Brocade HBA ports, %s",
890 "System may need more memory.\n");
Jing Huang7725ccf2009-09-23 17:46:15 -0700891 goto out_hal_mem_alloc_failure;
892 }
893
Maggie Zhangf7f738122010-12-09 19:08:43 -0800894 bfad->bfa.trcmod = bfad->trcmod;
895 bfad->bfa.plog = &bfad->plog_buf;
Jing Huang7725ccf2009-09-23 17:46:15 -0700896 bfa_plog_init(&bfad->plog_buf);
897 bfa_plog_str(&bfad->plog_buf, BFA_PL_MID_DRVR, BFA_PL_EID_DRIVER_START,
898 0, "Driver Attach");
899
900 bfa_attach(&bfad->bfa, bfad, &bfad->ioc_cfg, &bfad->meminfo,
901 &bfad->hal_pcidev);
902
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700903 /* FCS INIT */
Jing Huang7725ccf2009-09-23 17:46:15 -0700904 spin_lock_irqsave(&bfad->bfad_lock, flags);
Maggie Zhangf7f738122010-12-09 19:08:43 -0800905 bfad->bfa_fcs.trcmod = bfad->trcmod;
Krishna Gudipati82794a22010-03-03 17:43:30 -0800906 bfa_fcs_attach(&bfad->bfa_fcs, &bfad->bfa, bfad, BFA_FALSE);
Maggie Zhangf7f738122010-12-09 19:08:43 -0800907 bfad->bfa_fcs.fdmi_enabled = fdmi_enable;
Jing Huang7725ccf2009-09-23 17:46:15 -0700908 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
909
910 bfad->bfad_flags |= BFAD_DRV_INIT_DONE;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700911
Jing Huang7725ccf2009-09-23 17:46:15 -0700912 return BFA_STATUS_OK;
913
Jing Huang7725ccf2009-09-23 17:46:15 -0700914out_hal_mem_alloc_failure:
915 return BFA_STATUS_FAILED;
916}
917
918void
919bfad_drv_uninit(struct bfad_s *bfad)
920{
Krishna Gudipatie6714322010-03-03 17:44:02 -0800921 unsigned long flags;
922
923 spin_lock_irqsave(&bfad->bfad_lock, flags);
924 init_completion(&bfad->comp);
Maggie Zhangf7f738122010-12-09 19:08:43 -0800925 bfa_iocfc_stop(&bfad->bfa);
Krishna Gudipatie6714322010-03-03 17:44:02 -0800926 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
927 wait_for_completion(&bfad->comp);
928
Jing Huang7725ccf2009-09-23 17:46:15 -0700929 del_timer_sync(&bfad->hal_tmo);
930 bfa_isr_disable(&bfad->bfa);
931 bfa_detach(&bfad->bfa);
932 bfad_remove_intr(bfad);
Jing Huang7725ccf2009-09-23 17:46:15 -0700933 bfad_hal_mem_release(bfad);
Krishna Gudipatie6714322010-03-03 17:44:02 -0800934
935 bfad->bfad_flags &= ~BFAD_DRV_INIT_DONE;
Jing Huang7725ccf2009-09-23 17:46:15 -0700936}
937
938void
939bfad_drv_start(struct bfad_s *bfad)
940{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700941 unsigned long flags;
Jing Huang7725ccf2009-09-23 17:46:15 -0700942
943 spin_lock_irqsave(&bfad->bfad_lock, flags);
Maggie Zhangf7f738122010-12-09 19:08:43 -0800944 bfa_iocfc_start(&bfad->bfa);
945 bfa_fcs_fabric_modstart(&bfad->bfa_fcs);
Jing Huang7725ccf2009-09-23 17:46:15 -0700946 bfad->bfad_flags |= BFAD_HAL_START_DONE;
947 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
948
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700949 if (bfad->im)
950 flush_workqueue(bfad->im->drv_workq);
Jing Huang7725ccf2009-09-23 17:46:15 -0700951}
952
953void
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700954bfad_fcs_stop(struct bfad_s *bfad)
Jing Huang7725ccf2009-09-23 17:46:15 -0700955{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700956 unsigned long flags;
Jing Huang7725ccf2009-09-23 17:46:15 -0700957
958 spin_lock_irqsave(&bfad->bfad_lock, flags);
959 init_completion(&bfad->comp);
960 bfad->pport.flags |= BFAD_PORT_DELETE;
961 bfa_fcs_exit(&bfad->bfa_fcs);
962 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
963 wait_for_completion(&bfad->comp);
964
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700965 bfa_sm_send_event(bfad, BFAD_E_FCS_EXIT_COMP);
966}
967
968void
969bfad_stop(struct bfad_s *bfad)
970{
971 unsigned long flags;
972
Jing Huang7725ccf2009-09-23 17:46:15 -0700973 spin_lock_irqsave(&bfad->bfad_lock, flags);
974 init_completion(&bfad->comp);
Maggie Zhangf7f738122010-12-09 19:08:43 -0800975 bfa_iocfc_stop(&bfad->bfa);
Jing Huang7725ccf2009-09-23 17:46:15 -0700976 bfad->bfad_flags &= ~BFAD_HAL_START_DONE;
977 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
978 wait_for_completion(&bfad->comp);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700979
980 bfa_sm_send_event(bfad, BFAD_E_EXIT_COMP);
Jing Huang7725ccf2009-09-23 17:46:15 -0700981}
982
983bfa_status_t
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700984bfad_cfg_pport(struct bfad_s *bfad, enum bfa_lport_role role)
Jing Huang7725ccf2009-09-23 17:46:15 -0700985{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700986 int rc = BFA_STATUS_OK;
Jing Huang7725ccf2009-09-23 17:46:15 -0700987
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700988 /* Allocate scsi_host for the physical port */
989 if ((supported_fc4s & BFA_LPORT_ROLE_FCP_IM) &&
990 (role & BFA_LPORT_ROLE_FCP_IM)) {
Jing Huang7725ccf2009-09-23 17:46:15 -0700991 if (bfad->pport.im_port == NULL) {
992 rc = BFA_STATUS_FAILED;
993 goto out;
994 }
995
Jing Huangb5042932010-03-19 11:05:39 -0700996 rc = bfad_im_scsi_host_alloc(bfad, bfad->pport.im_port,
997 &bfad->pcidev->dev);
Jing Huang7725ccf2009-09-23 17:46:15 -0700998 if (rc != BFA_STATUS_OK)
999 goto out;
1000
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001001 bfad->pport.roles |= BFA_LPORT_ROLE_FCP_IM;
Jing Huang7725ccf2009-09-23 17:46:15 -07001002 }
1003
Jing Huangab2a9ba2010-07-08 20:02:55 -07001004 /* Setup the debugfs node for this scsi_host */
1005 if (bfa_debugfs_enable)
1006 bfad_debugfs_init(&bfad->pport);
1007
Jing Huang7725ccf2009-09-23 17:46:15 -07001008 bfad->bfad_flags |= BFAD_CFG_PPORT_DONE;
1009
1010out:
1011 return rc;
1012}
1013
1014void
1015bfad_uncfg_pport(struct bfad_s *bfad)
1016{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001017 /* Remove the debugfs node for this scsi_host */
Jing Huangab2a9ba2010-07-08 20:02:55 -07001018 kfree(bfad->regdata);
1019 bfad_debugfs_exit(&bfad->pport);
1020
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001021 if ((supported_fc4s & BFA_LPORT_ROLE_FCP_IM) &&
1022 (bfad->pport.roles & BFA_LPORT_ROLE_FCP_IM)) {
Jing Huang7725ccf2009-09-23 17:46:15 -07001023 bfad_im_scsi_host_free(bfad, bfad->pport.im_port);
1024 bfad_im_port_clean(bfad->pport.im_port);
1025 kfree(bfad->pport.im_port);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001026 bfad->pport.roles &= ~BFA_LPORT_ROLE_FCP_IM;
Jing Huang7725ccf2009-09-23 17:46:15 -07001027 }
1028
1029 bfad->bfad_flags &= ~BFAD_CFG_PPORT_DONE;
1030}
1031
Krishna Gudipatie6714322010-03-03 17:44:02 -08001032bfa_status_t
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001033bfad_start_ops(struct bfad_s *bfad) {
1034
1035 int retval;
1036 unsigned long flags;
1037 struct bfad_vport_s *vport, *vport_new;
1038 struct bfa_fcs_driver_info_s driver_info;
1039
1040 /* Fill the driver_info info to fcs*/
1041 memset(&driver_info, 0, sizeof(driver_info));
1042 strncpy(driver_info.version, BFAD_DRIVER_VERSION,
1043 sizeof(driver_info.version) - 1);
1044 if (host_name)
1045 strncpy(driver_info.host_machine_name, host_name,
1046 sizeof(driver_info.host_machine_name) - 1);
1047 if (os_name)
1048 strncpy(driver_info.host_os_name, os_name,
1049 sizeof(driver_info.host_os_name) - 1);
1050 if (os_patch)
1051 strncpy(driver_info.host_os_patch, os_patch,
1052 sizeof(driver_info.host_os_patch) - 1);
1053
1054 strncpy(driver_info.os_device_name, bfad->pci_name,
1055 sizeof(driver_info.os_device_name - 1));
1056
1057 /* FCS INIT */
1058 spin_lock_irqsave(&bfad->bfad_lock, flags);
1059 bfa_fcs_driver_info_init(&bfad->bfa_fcs, &driver_info);
1060 bfa_fcs_init(&bfad->bfa_fcs);
1061 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
Krishna Gudipatie6714322010-03-03 17:44:02 -08001062
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001063 retval = bfad_cfg_pport(bfad, BFA_LPORT_ROLE_FCP_IM);
Krishna Gudipatie6714322010-03-03 17:44:02 -08001064 if (retval != BFA_STATUS_OK) {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001065 if (bfa_sm_cmp_state(bfad, bfad_sm_initializing))
1066 bfa_sm_set_state(bfad, bfad_sm_failed);
1067 bfad_stop(bfad);
1068 return BFA_STATUS_FAILED;
Krishna Gudipatie6714322010-03-03 17:44:02 -08001069 }
1070
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001071 /* BFAD level FC4 IM specific resource allocation */
1072 retval = bfad_im_probe(bfad);
1073 if (retval != BFA_STATUS_OK) {
1074 printk(KERN_WARNING "bfad_im_probe failed\n");
1075 if (bfa_sm_cmp_state(bfad, bfad_sm_initializing))
1076 bfa_sm_set_state(bfad, bfad_sm_failed);
1077 bfad_im_probe_undo(bfad);
1078 bfad->bfad_flags &= ~BFAD_FC4_PROBE_DONE;
1079 bfad_uncfg_pport(bfad);
1080 bfad_stop(bfad);
1081 return BFA_STATUS_FAILED;
1082 } else
1083 bfad->bfad_flags |= BFAD_FC4_PROBE_DONE;
1084
Krishna Gudipatie6714322010-03-03 17:44:02 -08001085 bfad_drv_start(bfad);
1086
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001087 /* Complete pbc vport create */
1088 list_for_each_entry_safe(vport, vport_new, &bfad->pbc_vport_list,
1089 list_entry) {
Jing Huangd9883542010-07-08 19:46:26 -07001090 struct fc_vport_identifiers vid;
1091 struct fc_vport *fc_vport;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001092 char pwwn_buf[BFA_STRING_32];
Jing Huangd9883542010-07-08 19:46:26 -07001093
1094 memset(&vid, 0, sizeof(vid));
1095 vid.roles = FC_PORT_ROLE_FCP_INITIATOR;
1096 vid.vport_type = FC_PORTTYPE_NPIV;
1097 vid.disable = false;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001098 vid.node_name = wwn_to_u64((u8 *)
1099 (&((vport->fcs_vport).lport.port_cfg.nwwn)));
1100 vid.port_name = wwn_to_u64((u8 *)
1101 (&((vport->fcs_vport).lport.port_cfg.pwwn)));
Jing Huangd9883542010-07-08 19:46:26 -07001102 fc_vport = fc_vport_create(bfad->pport.im_port->shost, 0, &vid);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001103 if (!fc_vport) {
1104 wwn2str(pwwn_buf, vid.port_name);
Jing Huangd9883542010-07-08 19:46:26 -07001105 printk(KERN_WARNING "bfad%d: failed to create pbc vport"
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001106 " %s\n", bfad->inst_no, pwwn_buf);
1107 }
1108 list_del(&vport->list_entry);
1109 kfree(vport);
Jing Huangd9883542010-07-08 19:46:26 -07001110 }
1111
Krishna Gudipatie6714322010-03-03 17:44:02 -08001112 /*
1113 * If bfa_linkup_delay is set to -1 default; try to retrive the
Maggie Zhangf16a1752010-12-09 19:12:32 -08001114 * value using the bfad_get_linkup_delay(); else use the
Krishna Gudipatie6714322010-03-03 17:44:02 -08001115 * passed in module param value as the bfa_linkup_delay.
1116 */
1117 if (bfa_linkup_delay < 0) {
Maggie Zhangf16a1752010-12-09 19:12:32 -08001118 bfa_linkup_delay = bfad_get_linkup_delay(bfad);
1119 bfad_rport_online_wait(bfad);
Krishna Gudipatie6714322010-03-03 17:44:02 -08001120 bfa_linkup_delay = -1;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001121 } else
Maggie Zhangf16a1752010-12-09 19:12:32 -08001122 bfad_rport_online_wait(bfad);
Krishna Gudipatie6714322010-03-03 17:44:02 -08001123
Jing Huang88166242010-12-09 17:11:53 -08001124 BFA_LOG(KERN_INFO, bfad, bfa_log_level, "bfa device claimed\n");
Krishna Gudipatie6714322010-03-03 17:44:02 -08001125
1126 return BFA_STATUS_OK;
Krishna Gudipatie6714322010-03-03 17:44:02 -08001127}
1128
1129int
Jing Huangd9883542010-07-08 19:46:26 -07001130bfad_worker(void *ptr)
Krishna Gudipatie6714322010-03-03 17:44:02 -08001131{
1132 struct bfad_s *bfad;
1133 unsigned long flags;
1134
1135 bfad = (struct bfad_s *)ptr;
1136
1137 while (!kthread_should_stop()) {
1138
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001139 /* Send event BFAD_E_INIT_SUCCESS */
1140 bfa_sm_send_event(bfad, BFAD_E_INIT_SUCCESS);
Krishna Gudipatie6714322010-03-03 17:44:02 -08001141
1142 spin_lock_irqsave(&bfad->bfad_lock, flags);
1143 bfad->bfad_tsk = NULL;
1144 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1145
1146 break;
1147 }
1148
1149 return 0;
1150}
1151
Jing Huang5fbe25c2010-10-18 17:17:23 -07001152/*
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001153 * BFA driver interrupt functions
1154 */
1155irqreturn_t
1156bfad_intx(int irq, void *dev_id)
1157{
1158 struct bfad_s *bfad = dev_id;
1159 struct list_head doneq;
1160 unsigned long flags;
1161 bfa_boolean_t rc;
1162
1163 spin_lock_irqsave(&bfad->bfad_lock, flags);
1164 rc = bfa_intx(&bfad->bfa);
1165 if (!rc) {
1166 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1167 return IRQ_NONE;
1168 }
1169
1170 bfa_comp_deq(&bfad->bfa, &doneq);
1171 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1172
1173 if (!list_empty(&doneq)) {
1174 bfa_comp_process(&bfad->bfa, &doneq);
1175
1176 spin_lock_irqsave(&bfad->bfad_lock, flags);
1177 bfa_comp_free(&bfad->bfa, &doneq);
1178 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001179 }
1180
1181 return IRQ_HANDLED;
1182
1183}
1184
1185static irqreturn_t
1186bfad_msix(int irq, void *dev_id)
1187{
1188 struct bfad_msix_s *vec = dev_id;
1189 struct bfad_s *bfad = vec->bfad;
1190 struct list_head doneq;
1191 unsigned long flags;
1192
1193 spin_lock_irqsave(&bfad->bfad_lock, flags);
1194
1195 bfa_msix(&bfad->bfa, vec->msix.entry);
1196 bfa_comp_deq(&bfad->bfa, &doneq);
1197 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1198
1199 if (!list_empty(&doneq)) {
1200 bfa_comp_process(&bfad->bfa, &doneq);
1201
1202 spin_lock_irqsave(&bfad->bfad_lock, flags);
1203 bfa_comp_free(&bfad->bfa, &doneq);
1204 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1205 }
1206
1207 return IRQ_HANDLED;
1208}
1209
Jing Huang5fbe25c2010-10-18 17:17:23 -07001210/*
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001211 * Initialize the MSIX entry table.
1212 */
1213static void
1214bfad_init_msix_entry(struct bfad_s *bfad, struct msix_entry *msix_entries,
1215 int mask, int max_bit)
1216{
1217 int i;
1218 int match = 0x00000001;
1219
1220 for (i = 0, bfad->nvec = 0; i < MAX_MSIX_ENTRY; i++) {
1221 if (mask & match) {
1222 bfad->msix_tab[bfad->nvec].msix.entry = i;
1223 bfad->msix_tab[bfad->nvec].bfad = bfad;
1224 msix_entries[bfad->nvec].entry = i;
1225 bfad->nvec++;
1226 }
1227
1228 match <<= 1;
1229 }
1230
1231}
1232
1233int
1234bfad_install_msix_handler(struct bfad_s *bfad)
1235{
1236 int i, error = 0;
1237
1238 for (i = 0; i < bfad->nvec; i++) {
1239 sprintf(bfad->msix_tab[i].name, "bfa-%s-%s",
1240 bfad->pci_name,
1241 ((bfa_asic_id_ct(bfad->hal_pcidev.device_id)) ?
1242 msix_name_ct[i] : msix_name_cb[i]));
1243
1244 error = request_irq(bfad->msix_tab[i].msix.vector,
1245 (irq_handler_t) bfad_msix, 0,
1246 bfad->msix_tab[i].name, &bfad->msix_tab[i]);
1247 bfa_trc(bfad, i);
1248 bfa_trc(bfad, bfad->msix_tab[i].msix.vector);
1249 if (error) {
1250 int j;
1251
1252 for (j = 0; j < i; j++)
1253 free_irq(bfad->msix_tab[j].msix.vector,
1254 &bfad->msix_tab[j]);
1255
1256 return 1;
1257 }
1258 }
1259
1260 return 0;
1261}
1262
Jing Huang5fbe25c2010-10-18 17:17:23 -07001263/*
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001264 * Setup MSIX based interrupt.
1265 */
1266int
1267bfad_setup_intr(struct bfad_s *bfad)
1268{
1269 int error = 0;
1270 u32 mask = 0, i, num_bit = 0, max_bit = 0;
1271 struct msix_entry msix_entries[MAX_MSIX_ENTRY];
1272 struct pci_dev *pdev = bfad->pcidev;
1273
1274 /* Call BFA to get the msix map for this PCI function. */
1275 bfa_msix_getvecs(&bfad->bfa, &mask, &num_bit, &max_bit);
1276
1277 /* Set up the msix entry table */
1278 bfad_init_msix_entry(bfad, msix_entries, mask, max_bit);
1279
1280 if ((bfa_asic_id_ct(pdev->device) && !msix_disable_ct) ||
1281 (!bfa_asic_id_ct(pdev->device) && !msix_disable_cb)) {
1282
1283 error = pci_enable_msix(bfad->pcidev, msix_entries, bfad->nvec);
1284 if (error) {
1285 /*
1286 * Only error number of vector is available.
1287 * We don't have a mechanism to map multiple
1288 * interrupts into one vector, so even if we
1289 * can try to request less vectors, we don't
1290 * know how to associate interrupt events to
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001291 * vectors. Linux doesn't duplicate vectors
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001292 * in the MSIX table for this case.
1293 */
1294
1295 printk(KERN_WARNING "bfad%d: "
1296 "pci_enable_msix failed (%d),"
1297 " use line based.\n", bfad->inst_no, error);
1298
1299 goto line_based;
1300 }
1301
1302 /* Save the vectors */
1303 for (i = 0; i < bfad->nvec; i++) {
1304 bfa_trc(bfad, msix_entries[i].vector);
1305 bfad->msix_tab[i].msix.vector = msix_entries[i].vector;
1306 }
1307
1308 bfa_msix_init(&bfad->bfa, bfad->nvec);
1309
1310 bfad->bfad_flags |= BFAD_MSIX_ON;
1311
1312 return error;
1313 }
1314
1315line_based:
1316 error = 0;
1317 if (request_irq
1318 (bfad->pcidev->irq, (irq_handler_t) bfad_intx, BFAD_IRQ_FLAGS,
1319 BFAD_DRIVER_NAME, bfad) != 0) {
1320 /* Enable interrupt handler failed */
1321 return 1;
1322 }
1323
1324 return error;
1325}
1326
1327void
1328bfad_remove_intr(struct bfad_s *bfad)
1329{
1330 int i;
1331
1332 if (bfad->bfad_flags & BFAD_MSIX_ON) {
1333 for (i = 0; i < bfad->nvec; i++)
1334 free_irq(bfad->msix_tab[i].msix.vector,
1335 &bfad->msix_tab[i]);
1336
1337 pci_disable_msix(bfad->pcidev);
1338 bfad->bfad_flags &= ~BFAD_MSIX_ON;
1339 } else {
1340 free_irq(bfad->pcidev->irq, bfad);
1341 }
1342}
Jing Huang7725ccf2009-09-23 17:46:15 -07001343
Jing Huang5fbe25c2010-10-18 17:17:23 -07001344/*
Jing Huang7725ccf2009-09-23 17:46:15 -07001345 * PCI probe entry.
1346 */
1347int
1348bfad_pci_probe(struct pci_dev *pdev, const struct pci_device_id *pid)
1349{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001350 struct bfad_s *bfad;
1351 int error = -ENODEV, retval;
Jing Huang7725ccf2009-09-23 17:46:15 -07001352
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001353 /* For single port cards - only claim function 0 */
1354 if ((pdev->device == BFA_PCI_DEVICE_ID_FC_8G1P) &&
1355 (PCI_FUNC(pdev->devfn) != 0))
Jing Huang7725ccf2009-09-23 17:46:15 -07001356 return -ENODEV;
1357
Jing Huang7725ccf2009-09-23 17:46:15 -07001358 bfad = kzalloc(sizeof(struct bfad_s), GFP_KERNEL);
1359 if (!bfad) {
1360 error = -ENOMEM;
1361 goto out;
1362 }
1363
1364 bfad->trcmod = kzalloc(sizeof(struct bfa_trc_mod_s), GFP_KERNEL);
1365 if (!bfad->trcmod) {
1366 printk(KERN_WARNING "Error alloc trace buffer!\n");
1367 error = -ENOMEM;
1368 goto out_alloc_trace_failure;
1369 }
1370
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001371 /* TRACE INIT */
Jing Huang7725ccf2009-09-23 17:46:15 -07001372 bfa_trc_init(bfad->trcmod);
1373 bfa_trc(bfad, bfad_inst);
1374
Jing Huang7725ccf2009-09-23 17:46:15 -07001375 if (!(bfad_load_fwimg(pdev))) {
Jing Huang7725ccf2009-09-23 17:46:15 -07001376 kfree(bfad->trcmod);
1377 goto out_alloc_trace_failure;
1378 }
1379
1380 retval = bfad_pci_init(pdev, bfad);
1381 if (retval) {
1382 printk(KERN_WARNING "bfad_pci_init failure!\n");
1383 error = retval;
1384 goto out_pci_init_failure;
1385 }
1386
1387 mutex_lock(&bfad_mutex);
1388 bfad->inst_no = bfad_inst++;
1389 list_add_tail(&bfad->list_entry, &bfad_list);
1390 mutex_unlock(&bfad_mutex);
1391
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001392 /* Initializing the state machine: State set to uninit */
1393 bfa_sm_set_state(bfad, bfad_sm_uninit);
1394
Jing Huang7725ccf2009-09-23 17:46:15 -07001395 spin_lock_init(&bfad->bfad_lock);
1396 pci_set_drvdata(pdev, bfad);
1397
1398 bfad->ref_count = 0;
1399 bfad->pport.bfad = bfad;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001400 INIT_LIST_HEAD(&bfad->pbc_vport_list);
Krishna Gudipatie6714322010-03-03 17:44:02 -08001401
Jing Huang7725ccf2009-09-23 17:46:15 -07001402 retval = bfad_drv_init(bfad);
1403 if (retval != BFA_STATUS_OK)
1404 goto out_drv_init_failure;
Jing Huang7725ccf2009-09-23 17:46:15 -07001405
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001406 bfa_sm_send_event(bfad, BFAD_E_CREATE);
Jing Huang7725ccf2009-09-23 17:46:15 -07001407
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001408 if (bfa_sm_cmp_state(bfad, bfad_sm_uninit))
1409 goto out_bfad_sm_failure;
Jing Huang7725ccf2009-09-23 17:46:15 -07001410
Jing Huang7725ccf2009-09-23 17:46:15 -07001411 return 0;
1412
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001413out_bfad_sm_failure:
1414 bfa_detach(&bfad->bfa);
1415 bfad_hal_mem_release(bfad);
Jing Huang7725ccf2009-09-23 17:46:15 -07001416out_drv_init_failure:
1417 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 Gudipatia36c61f2010-09-15 11:50:55 -07001458 /* Cleaning the BFAD instance */
Jing Huang7725ccf2009-09-23 17:46:15 -07001459 mutex_lock(&bfad_mutex);
1460 bfad_inst--;
1461 list_del(&bfad->list_entry);
1462 mutex_unlock(&bfad_mutex);
1463 bfad_pci_uninit(pdev, bfad);
1464
1465 kfree(bfad->trcmod);
1466 kfree(bfad);
1467}
1468
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001469struct pci_device_id bfad_id_table[] = {
Jing Huang7725ccf2009-09-23 17:46:15 -07001470 {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001471 .vendor = BFA_PCI_VENDOR_ID_BROCADE,
1472 .device = BFA_PCI_DEVICE_ID_FC_8G2P,
1473 .subvendor = PCI_ANY_ID,
1474 .subdevice = PCI_ANY_ID,
1475 },
Jing Huang7725ccf2009-09-23 17:46:15 -07001476 {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001477 .vendor = BFA_PCI_VENDOR_ID_BROCADE,
1478 .device = BFA_PCI_DEVICE_ID_FC_8G1P,
1479 .subvendor = PCI_ANY_ID,
1480 .subdevice = PCI_ANY_ID,
1481 },
Jing Huang7725ccf2009-09-23 17:46:15 -07001482 {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001483 .vendor = BFA_PCI_VENDOR_ID_BROCADE,
1484 .device = BFA_PCI_DEVICE_ID_CT,
1485 .subvendor = PCI_ANY_ID,
1486 .subdevice = PCI_ANY_ID,
1487 .class = (PCI_CLASS_SERIAL_FIBER << 8),
1488 .class_mask = ~0,
1489 },
Jing Huang293f82d2010-07-08 19:45:20 -07001490 {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001491 .vendor = BFA_PCI_VENDOR_ID_BROCADE,
1492 .device = BFA_PCI_DEVICE_ID_CT_FC,
1493 .subvendor = PCI_ANY_ID,
1494 .subdevice = PCI_ANY_ID,
1495 .class = (PCI_CLASS_SERIAL_FIBER << 8),
1496 .class_mask = ~0,
Jing Huang293f82d2010-07-08 19:45:20 -07001497 },
Jing Huang7725ccf2009-09-23 17:46:15 -07001498
1499 {0, 0},
1500};
1501
1502MODULE_DEVICE_TABLE(pci, bfad_id_table);
1503
1504static struct pci_driver bfad_pci_driver = {
1505 .name = BFAD_DRIVER_NAME,
1506 .id_table = bfad_id_table,
1507 .probe = bfad_pci_probe,
1508 .remove = __devexit_p(bfad_pci_remove),
1509};
1510
Jing Huang5fbe25c2010-10-18 17:17:23 -07001511/*
Jing Huang7725ccf2009-09-23 17:46:15 -07001512 * Driver module init.
1513 */
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001514static int __init
Jing Huang7725ccf2009-09-23 17:46:15 -07001515bfad_init(void)
1516{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001517 int error = 0;
Jing Huang7725ccf2009-09-23 17:46:15 -07001518
1519 printk(KERN_INFO "Brocade BFA FC/FCOE SCSI driver - version: %s\n",
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001520 BFAD_DRIVER_VERSION);
Jing Huang7725ccf2009-09-23 17:46:15 -07001521
1522 if (num_sgpgs > 0)
1523 num_sgpgs_parm = num_sgpgs;
1524
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001525 error = bfad_im_module_init();
Jing Huang7725ccf2009-09-23 17:46:15 -07001526 if (error) {
1527 error = -ENOMEM;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001528 printk(KERN_WARNING "bfad_im_module_init failure\n");
Jing Huang7725ccf2009-09-23 17:46:15 -07001529 goto ext;
1530 }
1531
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001532 if (strcmp(FCPI_NAME, " fcpim") == 0)
1533 supported_fc4s |= BFA_LPORT_ROLE_FCP_IM;
Jing Huang7725ccf2009-09-23 17:46:15 -07001534
Maggie Zhangf7f738122010-12-09 19:08:43 -08001535 bfa_auto_recover = ioc_auto_recover;
Jing Huang7725ccf2009-09-23 17:46:15 -07001536 bfa_fcs_rport_set_del_timeout(rport_del_timeout);
Jing Huang7725ccf2009-09-23 17:46:15 -07001537
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001538 error = pci_register_driver(&bfad_pci_driver);
Jing Huang7725ccf2009-09-23 17:46:15 -07001539 if (error) {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001540 printk(KERN_WARNING "pci_register_driver failure\n");
Jing Huang7725ccf2009-09-23 17:46:15 -07001541 goto ext;
1542 }
1543
1544 return 0;
1545
1546ext:
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001547 bfad_im_module_exit();
Jing Huang7725ccf2009-09-23 17:46:15 -07001548 return error;
1549}
1550
Jing Huang5fbe25c2010-10-18 17:17:23 -07001551/*
Jing Huang7725ccf2009-09-23 17:46:15 -07001552 * Driver module exit.
1553 */
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001554static void __exit
Jing Huang7725ccf2009-09-23 17:46:15 -07001555bfad_exit(void)
1556{
1557 pci_unregister_driver(&bfad_pci_driver);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001558 bfad_im_module_exit();
Jing Huang7725ccf2009-09-23 17:46:15 -07001559 bfad_free_fwimg();
1560}
1561
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001562/* Firmware handling */
Jing Huang61338a02011-04-13 11:44:03 -07001563static void
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001564bfad_read_firmware(struct pci_dev *pdev, u32 **bfi_image,
1565 u32 *bfi_image_size, char *fw_name)
1566{
1567 const struct firmware *fw;
1568
1569 if (request_firmware(&fw, fw_name, &pdev->dev)) {
1570 printk(KERN_ALERT "Can't locate firmware %s\n", fw_name);
Jing Huang61338a02011-04-13 11:44:03 -07001571 *bfi_image = NULL;
1572 goto out;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001573 }
1574
1575 *bfi_image = vmalloc(fw->size);
1576 if (NULL == *bfi_image) {
1577 printk(KERN_ALERT "Fail to allocate buffer for fw image "
1578 "size=%x!\n", (u32) fw->size);
Jing Huang61338a02011-04-13 11:44:03 -07001579 goto out;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001580 }
1581
1582 memcpy(*bfi_image, fw->data, fw->size);
1583 *bfi_image_size = fw->size/sizeof(u32);
Jing Huang61338a02011-04-13 11:44:03 -07001584out:
1585 release_firmware(fw);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001586}
1587
Jing Huang61338a02011-04-13 11:44:03 -07001588static u32 *
1589bfad_load_fwimg(struct pci_dev *pdev)
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001590{
1591 if (pdev->device == BFA_PCI_DEVICE_ID_CT_FC) {
1592 if (bfi_image_ct_fc_size == 0)
1593 bfad_read_firmware(pdev, &bfi_image_ct_fc,
1594 &bfi_image_ct_fc_size, BFAD_FW_FILE_CT_FC);
1595 return bfi_image_ct_fc;
1596 } else if (pdev->device == BFA_PCI_DEVICE_ID_CT) {
1597 if (bfi_image_ct_cna_size == 0)
1598 bfad_read_firmware(pdev, &bfi_image_ct_cna,
1599 &bfi_image_ct_cna_size, BFAD_FW_FILE_CT_CNA);
1600 return bfi_image_ct_cna;
1601 } else {
1602 if (bfi_image_cb_fc_size == 0)
1603 bfad_read_firmware(pdev, &bfi_image_cb_fc,
1604 &bfi_image_cb_fc_size, BFAD_FW_FILE_CB_FC);
1605 return bfi_image_cb_fc;
1606 }
1607}
Jing Huang7725ccf2009-09-23 17:46:15 -07001608
Jing Huang61338a02011-04-13 11:44:03 -07001609static void
1610bfad_free_fwimg(void)
1611{
1612 if (bfi_image_ct_fc_size && bfi_image_ct_fc)
1613 vfree(bfi_image_ct_fc);
1614 if (bfi_image_ct_cna_size && bfi_image_ct_cna)
1615 vfree(bfi_image_ct_cna);
1616 if (bfi_image_cb_fc_size && bfi_image_cb_fc)
1617 vfree(bfi_image_cb_fc);
1618}
1619
Jing Huang7725ccf2009-09-23 17:46:15 -07001620module_init(bfad_init);
1621module_exit(bfad_exit);
1622MODULE_LICENSE("GPL");
1623MODULE_DESCRIPTION("Brocade Fibre Channel HBA Driver" BFAD_PROTO_NAME);
1624MODULE_AUTHOR("Brocade Communications Systems, Inc.");
1625MODULE_VERSION(BFAD_DRIVER_VERSION);