bfad.c 40.7 KB
Newer Older
1
/*
K
Krishna Gudipati 已提交
2
 * Copyright (c) 2005-2010 Brocade Communications Systems, Inc.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
 * All rights reserved
 * www.brocade.com
 *
 * Linux driver for Brocade Fibre Channel Host Bus Adapter.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License (GPL) Version 2 as
 * published by the Free Software Foundation
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 */

J
Jing Huang 已提交
18
/*
19 20 21
 *  bfad.c Linux driver PCI interface module.
 */
#include <linux/module.h>
22
#include <linux/kthread.h>
K
Krishna Gudipati 已提交
23 24 25 26 27 28 29 30 31
#include <linux/errno.h>
#include <linux/sched.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/pci.h>
#include <linux/firmware.h>
#include <asm/uaccess.h>
#include <asm/fcntl.h>

32 33
#include "bfad_drv.h"
#include "bfad_im.h"
K
Krishna Gudipati 已提交
34 35 36
#include "bfa_fcs.h"
#include "bfa_defs.h"
#include "bfa.h"
37 38

BFA_TRC_FILE(LDRV, BFAD);
39
DEFINE_MUTEX(bfad_mutex);
40
LIST_HEAD(bfad_list);
K
Krishna Gudipati 已提交
41 42 43 44 45 46 47 48 49 50 51

static int	bfad_inst;
static int      num_sgpgs_parm;
int		supported_fc4s;
char		*host_name, *os_name, *os_patch;
int		num_rports, num_ios, num_tms;
int		num_fcxps, num_ufbufs;
int		reqq_size, rspq_size, num_sgpgs;
int		rport_del_timeout = BFA_FCS_RPORT_DEF_DEL_TIMEOUT;
int		bfa_lun_queue_depth = BFAD_LUN_QUEUE_DEPTH;
int		bfa_io_max_sge = BFAD_IO_MAX_SGE;
52
int		bfa_log_level = 3; /* WARNING log level */
K
Krishna Gudipati 已提交
53 54 55 56
int		ioc_auto_recover = BFA_TRUE;
int		bfa_linkup_delay = -1;
int		fdmi_enable = BFA_TRUE;
int		pcie_max_read_reqsz;
J
Jing Huang 已提交
57
int		bfa_debugfs_enable = 1;
K
Krishna Gudipati 已提交
58
int		msix_disable_cb = 0, msix_disable_ct = 0;
59
int		max_xfer_size = BFAD_MAX_SECTORS >> 1;
K
Krishna Gudipati 已提交
60

J
Jing Huang 已提交
61
/* Firmware releated */
62 63
u32	bfi_image_cb_size, bfi_image_ct_size, bfi_image_ct2_size;
u32	*bfi_image_cb, *bfi_image_ct, *bfi_image_ct2;
K
Krishna Gudipati 已提交
64

65 66 67
#define BFAD_FW_FILE_CB		"cbfw.bin"
#define BFAD_FW_FILE_CT		"ctfw.bin"
#define BFAD_FW_FILE_CT2	"ct2fw.bin"
J
Jing Huang 已提交
68 69 70 71 72 73

static u32 *bfad_load_fwimg(struct pci_dev *pdev);
static void bfad_free_fwimg(void);
static void bfad_read_firmware(struct pci_dev *pdev, u32 **bfi_image,
		u32 *bfi_image_size, char *fw_name);

74
static const char *msix_name_ct[] = {
75
	"ctrl",
K
Krishna Gudipati 已提交
76
	"cpe0", "cpe1", "cpe2", "cpe3",
77
	"rme0", "rme1", "rme2", "rme3" };
K
Krishna Gudipati 已提交
78

79
static const char *msix_name_cb[] = {
K
Krishna Gudipati 已提交
80 81 82 83
	"cpe0", "cpe1", "cpe2", "cpe3",
	"rme0", "rme1", "rme2", "rme3",
	"eemc", "elpu0", "elpu1", "epss", "mlpu" };

84 85 86
MODULE_FIRMWARE(BFAD_FW_FILE_CB);
MODULE_FIRMWARE(BFAD_FW_FILE_CT);
MODULE_FIRMWARE(BFAD_FW_FILE_CT2);
87 88

module_param(os_name, charp, S_IRUGO | S_IWUSR);
89
MODULE_PARM_DESC(os_name, "OS name of the hba host machine");
90
module_param(os_patch, charp, S_IRUGO | S_IWUSR);
91
MODULE_PARM_DESC(os_patch, "OS patch level of the hba host machine");
92
module_param(host_name, charp, S_IRUGO | S_IWUSR);
93
MODULE_PARM_DESC(host_name, "Hostname of the hba host machine");
94
module_param(num_rports, int, S_IRUGO | S_IWUSR);
K
Krishna Gudipati 已提交
95 96
MODULE_PARM_DESC(num_rports, "Max number of rports supported per port "
				"(physical/logical), default=1024");
97
module_param(num_ios, int, S_IRUGO | S_IWUSR);
98
MODULE_PARM_DESC(num_ios, "Max number of ioim requests, default=2000");
99
module_param(num_tms, int, S_IRUGO | S_IWUSR);
100
MODULE_PARM_DESC(num_tms, "Max number of task im requests, default=128");
101
module_param(num_fcxps, int, S_IRUGO | S_IWUSR);
102
MODULE_PARM_DESC(num_fcxps, "Max number of fcxp requests, default=64");
103
module_param(num_ufbufs, int, S_IRUGO | S_IWUSR);
K
Krishna Gudipati 已提交
104 105
MODULE_PARM_DESC(num_ufbufs, "Max number of unsolicited frame "
				"buffers, default=64");
106
module_param(reqq_size, int, S_IRUGO | S_IWUSR);
K
Krishna Gudipati 已提交
107 108
MODULE_PARM_DESC(reqq_size, "Max number of request queue elements, "
				"default=256");
109
module_param(rspq_size, int, S_IRUGO | S_IWUSR);
K
Krishna Gudipati 已提交
110 111
MODULE_PARM_DESC(rspq_size, "Max number of response queue elements, "
				"default=64");
112
module_param(num_sgpgs, int, S_IRUGO | S_IWUSR);
113
MODULE_PARM_DESC(num_sgpgs, "Number of scatter/gather pages, default=2048");
114
module_param(rport_del_timeout, int, S_IRUGO | S_IWUSR);
K
Krishna Gudipati 已提交
115 116
MODULE_PARM_DESC(rport_del_timeout, "Rport delete timeout, default=90 secs, "
					"Range[>0]");
117
module_param(bfa_lun_queue_depth, int, S_IRUGO | S_IWUSR);
K
Krishna Gudipati 已提交
118
MODULE_PARM_DESC(bfa_lun_queue_depth, "Lun queue depth, default=32, Range[>0]");
119
module_param(bfa_io_max_sge, int, S_IRUGO | S_IWUSR);
120
MODULE_PARM_DESC(bfa_io_max_sge, "Max io scatter/gather elements, default=255");
121 122
module_param(bfa_log_level, int, S_IRUGO | S_IWUSR);
MODULE_PARM_DESC(bfa_log_level, "Driver log level, default=3, "
K
Krishna Gudipati 已提交
123
				"Range[Critical:1|Error:2|Warning:3|Info:4]");
124
module_param(ioc_auto_recover, int, S_IRUGO | S_IWUSR);
K
Krishna Gudipati 已提交
125 126
MODULE_PARM_DESC(ioc_auto_recover, "IOC auto recovery, default=1, "
				"Range[off:0|on:1]");
127
module_param(bfa_linkup_delay, int, S_IRUGO | S_IWUSR);
K
Krishna Gudipati 已提交
128 129 130 131 132 133 134 135 136 137 138
MODULE_PARM_DESC(bfa_linkup_delay, "Link up delay, default=30 secs for "
			"boot port. Otherwise 10 secs in RHEL4 & 0 for "
			"[RHEL5, SLES10, ESX40] Range[>0]");
module_param(msix_disable_cb, int, S_IRUGO | S_IWUSR);
MODULE_PARM_DESC(msix_disable_cb, "Disable Message Signaled Interrupts "
			"for Brocade-415/425/815/825 cards, default=0, "
			" Range[false:0|true:1]");
module_param(msix_disable_ct, int, S_IRUGO | S_IWUSR);
MODULE_PARM_DESC(msix_disable_ct, "Disable Message Signaled Interrupts "
			"if possible for Brocade-1010/1020/804/1007/902/1741 "
			"cards, default=0, Range[false:0|true:1]");
139
module_param(fdmi_enable, int, S_IRUGO | S_IWUSR);
K
Krishna Gudipati 已提交
140 141 142 143 144
MODULE_PARM_DESC(fdmi_enable, "Enables fdmi registration, default=1, "
				"Range[false:0|true:1]");
module_param(pcie_max_read_reqsz, int, S_IRUGO | S_IWUSR);
MODULE_PARM_DESC(pcie_max_read_reqsz, "PCIe max read request size, default=0 "
		"(use system setting), Range[128|256|512|1024|2048|4096]");
J
Jing Huang 已提交
145 146 147
module_param(bfa_debugfs_enable, int, S_IRUGO | S_IWUSR);
MODULE_PARM_DESC(bfa_debugfs_enable, "Enables debugfs feature, default=1,"
		" Range[false:0|true:1]");
148 149 150
module_param(max_xfer_size, int, S_IRUGO | S_IWUSR);
MODULE_PARM_DESC(max_xfer_size, "default=32MB,"
		" Range[64k|128k|256k|512k|1024k|2048k]");
151

K
Krishna Gudipati 已提交
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
static void
bfad_sm_uninit(struct bfad_s *bfad, enum bfad_sm_event event);
static void
bfad_sm_created(struct bfad_s *bfad, enum bfad_sm_event event);
static void
bfad_sm_initializing(struct bfad_s *bfad, enum bfad_sm_event event);
static void
bfad_sm_operational(struct bfad_s *bfad, enum bfad_sm_event event);
static void
bfad_sm_stopping(struct bfad_s *bfad, enum bfad_sm_event event);
static void
bfad_sm_failed(struct bfad_s *bfad, enum bfad_sm_event event);
static void
bfad_sm_fcs_exit(struct bfad_s *bfad, enum bfad_sm_event event);

J
Jing Huang 已提交
167
/*
K
Krishna Gudipati 已提交
168
 * Beginning state for the driver instance, awaiting the pci_probe event
169
 */
K
Krishna Gudipati 已提交
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
static void
bfad_sm_uninit(struct bfad_s *bfad, enum bfad_sm_event event)
{
	bfa_trc(bfad, event);

	switch (event) {
	case BFAD_E_CREATE:
		bfa_sm_set_state(bfad, bfad_sm_created);
		bfad->bfad_tsk = kthread_create(bfad_worker, (void *) bfad,
						"%s", "bfad_worker");
		if (IS_ERR(bfad->bfad_tsk)) {
			printk(KERN_INFO "bfad[%d]: Kernel thread "
				"creation failed!\n", bfad->inst_no);
			bfa_sm_send_event(bfad, BFAD_E_KTHREAD_CREATE_FAILED);
		}
		bfa_sm_send_event(bfad, BFAD_E_INIT);
		break;

	case BFAD_E_STOP:
		/* Ignore stop; already in uninit */
		break;

	default:
		bfa_sm_fault(bfad, event);
	}
}
196

J
Jing Huang 已提交
197
/*
K
Krishna Gudipati 已提交
198 199 200 201
 * Driver Instance is created, awaiting event INIT to initialize the bfad
 */
static void
bfad_sm_created(struct bfad_s *bfad, enum bfad_sm_event event)
202
{
K
Krishna Gudipati 已提交
203
	unsigned long flags;
204

K
Krishna Gudipati 已提交
205
	bfa_trc(bfad, event);
206

K
Krishna Gudipati 已提交
207 208 209
	switch (event) {
	case BFAD_E_INIT:
		bfa_sm_set_state(bfad, bfad_sm_initializing);
210

K
Krishna Gudipati 已提交
211
		init_completion(&bfad->comp);
212

K
Krishna Gudipati 已提交
213 214 215 216 217 218 219 220 221
		/* Enable Interrupt and wait bfa_init completion */
		if (bfad_setup_intr(bfad)) {
			printk(KERN_WARNING "bfad%d: bfad_setup_intr failed\n",
					bfad->inst_no);
			bfa_sm_send_event(bfad, BFAD_E_INTR_INIT_FAILED);
			break;
		}

		spin_lock_irqsave(&bfad->bfad_lock, flags);
222
		bfa_iocfc_init(&bfad->bfa);
K
Krishna Gudipati 已提交
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
		spin_unlock_irqrestore(&bfad->bfad_lock, flags);

		/* Set up interrupt handler for each vectors */
		if ((bfad->bfad_flags & BFAD_MSIX_ON) &&
			bfad_install_msix_handler(bfad)) {
			printk(KERN_WARNING "%s: install_msix failed, bfad%d\n",
				__func__, bfad->inst_no);
		}

		bfad_init_timer(bfad);

		wait_for_completion(&bfad->comp);

		if ((bfad->bfad_flags & BFAD_HAL_INIT_DONE)) {
			bfa_sm_send_event(bfad, BFAD_E_INIT_SUCCESS);
		} else {
239 240 241
			printk(KERN_WARNING
				"bfa %s: bfa init failed\n",
				bfad->pci_name);
K
Krishna Gudipati 已提交
242 243 244 245 246 247 248 249 250 251 252 253 254
			bfad->bfad_flags |= BFAD_HAL_INIT_FAIL;
			bfa_sm_send_event(bfad, BFAD_E_INIT_FAILED);
		}

		break;

	case BFAD_E_KTHREAD_CREATE_FAILED:
		bfa_sm_set_state(bfad, bfad_sm_uninit);
		break;

	default:
		bfa_sm_fault(bfad, event);
	}
255 256 257
}

static void
K
Krishna Gudipati 已提交
258
bfad_sm_initializing(struct bfad_s *bfad, enum bfad_sm_event event)
259
{
K
Krishna Gudipati 已提交
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
	int	retval;
	unsigned long	flags;

	bfa_trc(bfad, event);

	switch (event) {
	case BFAD_E_INIT_SUCCESS:
		kthread_stop(bfad->bfad_tsk);
		spin_lock_irqsave(&bfad->bfad_lock, flags);
		bfad->bfad_tsk = NULL;
		spin_unlock_irqrestore(&bfad->bfad_lock, flags);

		retval = bfad_start_ops(bfad);
		if (retval != BFA_STATUS_OK)
			break;
		bfa_sm_set_state(bfad, bfad_sm_operational);
		break;

	case BFAD_E_INTR_INIT_FAILED:
		bfa_sm_set_state(bfad, bfad_sm_uninit);
		kthread_stop(bfad->bfad_tsk);
		spin_lock_irqsave(&bfad->bfad_lock, flags);
		bfad->bfad_tsk = NULL;
		spin_unlock_irqrestore(&bfad->bfad_lock, flags);
		break;

	case BFAD_E_INIT_FAILED:
		bfa_sm_set_state(bfad, bfad_sm_failed);
		break;
	default:
		bfa_sm_fault(bfad, event);
	}
292 293 294
}

static void
K
Krishna Gudipati 已提交
295
bfad_sm_failed(struct bfad_s *bfad, enum bfad_sm_event event)
296
{
K
Krishna Gudipati 已提交
297
	int	retval;
298

K
Krishna Gudipati 已提交
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
	bfa_trc(bfad, event);

	switch (event) {
	case BFAD_E_INIT_SUCCESS:
		retval = bfad_start_ops(bfad);
		if (retval != BFA_STATUS_OK)
			break;
		bfa_sm_set_state(bfad, bfad_sm_operational);
		break;

	case BFAD_E_STOP:
		if (bfad->bfad_flags & BFAD_CFG_PPORT_DONE)
			bfad_uncfg_pport(bfad);
		if (bfad->bfad_flags & BFAD_FC4_PROBE_DONE) {
			bfad_im_probe_undo(bfad);
			bfad->bfad_flags &= ~BFAD_FC4_PROBE_DONE;
		}
		bfad_stop(bfad);
		break;

	case BFAD_E_EXIT_COMP:
		bfa_sm_set_state(bfad, bfad_sm_uninit);
		bfad_remove_intr(bfad);
		del_timer_sync(&bfad->hal_tmo);
		break;

	default:
		bfa_sm_fault(bfad, event);
	}
328 329
}

K
Krishna Gudipati 已提交
330 331
static void
bfad_sm_operational(struct bfad_s *bfad, enum bfad_sm_event event)
332
{
K
Krishna Gudipati 已提交
333
	bfa_trc(bfad, event);
334

K
Krishna Gudipati 已提交
335 336 337 338 339
	switch (event) {
	case BFAD_E_STOP:
		bfa_sm_set_state(bfad, bfad_sm_fcs_exit);
		bfad_fcs_stop(bfad);
		break;
340

K
Krishna Gudipati 已提交
341 342 343 344
	default:
		bfa_sm_fault(bfad, event);
	}
}
345

K
Krishna Gudipati 已提交
346 347 348 349 350 351 352 353 354 355 356 357 358 359
static void
bfad_sm_fcs_exit(struct bfad_s *bfad, enum bfad_sm_event event)
{
	bfa_trc(bfad, event);

	switch (event) {
	case BFAD_E_FCS_EXIT_COMP:
		bfa_sm_set_state(bfad, bfad_sm_stopping);
		bfad_stop(bfad);
		break;

	default:
		bfa_sm_fault(bfad, event);
	}
360 361 362
}

static void
K
Krishna Gudipati 已提交
363
bfad_sm_stopping(struct bfad_s *bfad, enum bfad_sm_event event)
364
{
K
Krishna Gudipati 已提交
365
	bfa_trc(bfad, event);
366

K
Krishna Gudipati 已提交
367 368 369 370 371 372 373 374 375
	switch (event) {
	case BFAD_E_EXIT_COMP:
		bfa_sm_set_state(bfad, bfad_sm_uninit);
		bfad_remove_intr(bfad);
		del_timer_sync(&bfad->hal_tmo);
		bfad_im_probe_undo(bfad);
		bfad->bfad_flags &= ~BFAD_FC4_PROBE_DONE;
		bfad_uncfg_pport(bfad);
		break;
376

K
Krishna Gudipati 已提交
377 378 379 380
	default:
		bfa_sm_fault(bfad, event);
		break;
	}
381 382
}

J
Jing Huang 已提交
383
/*
384 385 386 387 388 389 390 391 392 393 394
 *  BFA callbacks
 */
void
bfad_hcb_comp(void *arg, bfa_status_t status)
{
	struct bfad_hal_comp *fcomp = (struct bfad_hal_comp *)arg;

	fcomp->status = status;
	complete(&fcomp->comp);
}

J
Jing Huang 已提交
395
/*
396 397 398 399 400
 * bfa_init callback
 */
void
bfa_cb_init(void *drv, bfa_status_t init_status)
{
K
Krishna Gudipati 已提交
401
	struct bfad_s	      *bfad = drv;
402

403
	if (init_status == BFA_STATUS_OK) {
404 405
		bfad->bfad_flags |= BFAD_HAL_INIT_DONE;

K
Krishna Gudipati 已提交
406 407
		/*
		 * If BFAD_HAL_INIT_FAIL flag is set:
408 409 410 411 412 413 414 415 416
		 * Wake up the kernel thread to start
		 * the bfad operations after HAL init done
		 */
		if ((bfad->bfad_flags & BFAD_HAL_INIT_FAIL)) {
			bfad->bfad_flags &= ~BFAD_HAL_INIT_FAIL;
			wake_up_process(bfad->bfad_tsk);
		}
	}

417 418 419
	complete(&bfad->comp);
}

J
Jing Huang 已提交
420
/*
421 422 423
 *  BFA_FCS callbacks
 */
struct bfad_port_s *
K
Krishna Gudipati 已提交
424 425
bfa_fcb_lport_new(struct bfad_s *bfad, struct bfa_fcs_lport_s *port,
		 enum bfa_lport_role roles, struct bfad_vf_s *vf_drv,
426 427
		 struct bfad_vport_s *vp_drv)
{
K
Krishna Gudipati 已提交
428 429
	bfa_status_t	rc;
	struct bfad_port_s    *port_drv;
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446

	if (!vp_drv && !vf_drv) {
		port_drv = &bfad->pport;
		port_drv->pvb_type = BFAD_PORT_PHYS_BASE;
	} else if (!vp_drv && vf_drv) {
		port_drv = &vf_drv->base_port;
		port_drv->pvb_type = BFAD_PORT_VF_BASE;
	} else if (vp_drv && !vf_drv) {
		port_drv = &vp_drv->drv_port;
		port_drv->pvb_type = BFAD_PORT_PHYS_VPORT;
	} else {
		port_drv = &vp_drv->drv_port;
		port_drv->pvb_type = BFAD_PORT_VF_VPORT;
	}

	port_drv->fcs_port = port;
	port_drv->roles = roles;
K
Krishna Gudipati 已提交
447 448 449 450 451 452 453

	if (roles & BFA_LPORT_ROLE_FCP_IM) {
		rc = bfad_im_port_new(bfad, port_drv);
		if (rc != BFA_STATUS_OK) {
			bfad_im_port_delete(bfad, port_drv);
			port_drv = NULL;
		}
454 455 456 457 458 459
	}

	return port_drv;
}

void
K
Krishna Gudipati 已提交
460
bfa_fcb_lport_delete(struct bfad_s *bfad, enum bfa_lport_role roles,
461 462
		    struct bfad_vf_s *vf_drv, struct bfad_vport_s *vp_drv)
{
K
Krishna Gudipati 已提交
463
	struct bfad_port_s    *port_drv;
464

K
Krishna Gudipati 已提交
465
	/* this will be only called from rmmod context */
466
	if (vp_drv && !vp_drv->comp_del) {
K
Krishna Gudipati 已提交
467 468 469
		port_drv = (vp_drv) ? (&(vp_drv)->drv_port) :
				((vf_drv) ? (&(vf_drv)->base_port) :
				(&(bfad)->pport));
470
		bfa_trc(bfad, roles);
K
Krishna Gudipati 已提交
471 472
		if (roles & BFA_LPORT_ROLE_FCP_IM)
			bfad_im_port_delete(bfad, port_drv);
473 474 475
	}
}

J
Jing Huang 已提交
476
/*
477 478 479 480 481 482
 * FCS RPORT alloc callback, after successful PLOGI by FCS
 */
bfa_status_t
bfa_fcb_rport_alloc(struct bfad_s *bfad, struct bfa_fcs_rport_s **rport,
		    struct bfad_rport_s **rport_drv)
{
K
Krishna Gudipati 已提交
483
	bfa_status_t	rc = BFA_STATUS_OK;
484 485 486 487 488 489 490 491 492 493 494 495 496

	*rport_drv = kzalloc(sizeof(struct bfad_rport_s), GFP_ATOMIC);
	if (*rport_drv == NULL) {
		rc = BFA_STATUS_ENOMEM;
		goto ext;
	}

	*rport = &(*rport_drv)->fcs_rport;

ext:
	return rc;
}

J
Jing Huang 已提交
497
/*
J
Jing Huang 已提交
498 499 500 501 502 503
 * FCS PBC VPORT Create
 */
void
bfa_fcb_pbc_vport_create(struct bfad_s *bfad, struct bfi_pbc_vport_s pbc_vport)
{

K
Krishna Gudipati 已提交
504 505 506
	struct bfa_lport_cfg_s port_cfg = {0};
	struct bfad_vport_s   *vport;
	int rc;
J
Jing Huang 已提交
507

K
Krishna Gudipati 已提交
508 509
	vport = kzalloc(sizeof(struct bfad_vport_s), GFP_KERNEL);
	if (!vport) {
J
Jing Huang 已提交
510 511 512
		bfa_trc(bfad, 0);
		return;
	}
513

K
Krishna Gudipati 已提交
514 515 516 517 518 519 520 521
	vport->drv_port.bfad = bfad;
	port_cfg.roles = BFA_LPORT_ROLE_FCP_IM;
	port_cfg.pwwn = pbc_vport.vp_pwwn;
	port_cfg.nwwn = pbc_vport.vp_nwwn;
	port_cfg.preboot_vp  = BFA_TRUE;

	rc = bfa_fcs_pbc_vport_create(&vport->fcs_vport, &bfad->bfa_fcs, 0,
				  &port_cfg, vport);
J
Jing Huang 已提交
522

K
Krishna Gudipati 已提交
523 524 525 526
	if (rc != BFA_STATUS_OK) {
		bfa_trc(bfad, 0);
		return;
	}
J
Jing Huang 已提交
527

K
Krishna Gudipati 已提交
528
	list_add_tail(&vport->list_entry, &bfad->pbc_vport_list);
J
Jing Huang 已提交
529
}
530 531 532 533 534

void
bfad_hal_mem_release(struct bfad_s *bfad)
{
	struct bfa_meminfo_s *hal_meminfo = &bfad->meminfo;
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
	struct bfa_mem_dma_s *dma_info, *dma_elem;
	struct bfa_mem_kva_s *kva_info, *kva_elem;
	struct list_head *dm_qe, *km_qe;

	dma_info = &hal_meminfo->dma_info;
	kva_info = &hal_meminfo->kva_info;

	/* Iterate through the KVA meminfo queue */
	list_for_each(km_qe, &kva_info->qe) {
		kva_elem = (struct bfa_mem_kva_s *) km_qe;
		vfree(kva_elem->kva);
	}

	/* Iterate through the DMA meminfo queue */
	list_for_each(dm_qe, &dma_info->qe) {
		dma_elem = (struct bfa_mem_dma_s *) dm_qe;
		dma_free_coherent(&bfad->pcidev->dev,
				dma_elem->mem_len, dma_elem->kva,
				(dma_addr_t) dma_elem->dma);
554 555 556 557 558 559 560 561 562 563 564 565 566 567
	}

	memset(hal_meminfo, 0, sizeof(struct bfa_meminfo_s));
}

void
bfad_update_hal_cfg(struct bfa_iocfc_cfg_s *bfa_cfg)
{
	if (num_rports > 0)
		bfa_cfg->fwcfg.num_rports = num_rports;
	if (num_ios > 0)
		bfa_cfg->fwcfg.num_ioim_reqs = num_ios;
	if (num_tms > 0)
		bfa_cfg->fwcfg.num_tskim_reqs = num_tms;
568
	if (num_fcxps > 0 && num_fcxps <= BFA_FCXP_MAX)
569
		bfa_cfg->fwcfg.num_fcxp_reqs = num_fcxps;
570
	if (num_ufbufs > 0 && num_ufbufs <= BFA_UF_MAX)
571 572 573 574 575
		bfa_cfg->fwcfg.num_uf_bufs = num_ufbufs;
	if (reqq_size > 0)
		bfa_cfg->drvcfg.num_reqq_elems = reqq_size;
	if (rspq_size > 0)
		bfa_cfg->drvcfg.num_rspq_elems = rspq_size;
576
	if (num_sgpgs > 0 && num_sgpgs <= BFA_SGPG_MAX)
577 578 579 580 581 582 583
		bfa_cfg->drvcfg.num_sgpgs = num_sgpgs;

	/*
	 * populate the hal values back to the driver for sysfs use.
	 * otherwise, the default values will be shown as 0 in sysfs
	 */
	num_rports = bfa_cfg->fwcfg.num_rports;
K
Krishna Gudipati 已提交
584 585 586
	num_ios = bfa_cfg->fwcfg.num_ioim_reqs;
	num_tms = bfa_cfg->fwcfg.num_tskim_reqs;
	num_fcxps = bfa_cfg->fwcfg.num_fcxp_reqs;
587
	num_ufbufs = bfa_cfg->fwcfg.num_uf_bufs;
K
Krishna Gudipati 已提交
588 589 590
	reqq_size = bfa_cfg->drvcfg.num_reqq_elems;
	rspq_size = bfa_cfg->drvcfg.num_rspq_elems;
	num_sgpgs = bfa_cfg->drvcfg.num_sgpgs;
591 592 593 594 595 596
}

bfa_status_t
bfad_hal_mem_alloc(struct bfad_s *bfad)
{
	struct bfa_meminfo_s *hal_meminfo = &bfad->meminfo;
597 598 599
	struct bfa_mem_dma_s *dma_info, *dma_elem;
	struct bfa_mem_kva_s *kva_info, *kva_elem;
	struct list_head *dm_qe, *km_qe;
K
Krishna Gudipati 已提交
600
	bfa_status_t	rc = BFA_STATUS_OK;
601
	dma_addr_t	phys_addr;
602 603 604 605

	bfa_cfg_get_default(&bfad->ioc_cfg);
	bfad_update_hal_cfg(&bfad->ioc_cfg);
	bfad->cfg_data.ioc_queue_depth = bfad->ioc_cfg.fwcfg.num_ioim_reqs;
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
	bfa_cfg_get_meminfo(&bfad->ioc_cfg, hal_meminfo, &bfad->bfa);

	dma_info = &hal_meminfo->dma_info;
	kva_info = &hal_meminfo->kva_info;

	/* Iterate through the KVA meminfo queue */
	list_for_each(km_qe, &kva_info->qe) {
		kva_elem = (struct bfa_mem_kva_s *) km_qe;
		kva_elem->kva = vmalloc(kva_elem->mem_len);
		if (kva_elem->kva == NULL) {
			bfad_hal_mem_release(bfad);
			rc = BFA_STATUS_ENOMEM;
			goto ext;
		}
		memset(kva_elem->kva, 0, kva_elem->mem_len);
	}
622

623 624 625 626 627 628 629 630 631 632
	/* Iterate through the DMA meminfo queue */
	list_for_each(dm_qe, &dma_info->qe) {
		dma_elem = (struct bfa_mem_dma_s *) dm_qe;
		dma_elem->kva = dma_alloc_coherent(&bfad->pcidev->dev,
						dma_elem->mem_len,
						&phys_addr, GFP_KERNEL);
		if (dma_elem->kva == NULL) {
			bfad_hal_mem_release(bfad);
			rc = BFA_STATUS_ENOMEM;
			goto ext;
633
		}
634 635
		dma_elem->dma = phys_addr;
		memset(dma_elem->kva, 0, dma_elem->mem_len);
636 637 638 639 640
	}
ext:
	return rc;
}

J
Jing Huang 已提交
641
/*
642 643 644 645
 * Create a vport under a vf.
 */
bfa_status_t
bfad_vport_create(struct bfad_s *bfad, u16 vf_id,
K
Krishna Gudipati 已提交
646
		  struct bfa_lport_cfg_s *port_cfg, struct device *dev)
647
{
K
Krishna Gudipati 已提交
648 649 650
	struct bfad_vport_s   *vport;
	int		rc = BFA_STATUS_OK;
	unsigned long	flags;
651 652 653 654 655 656 657 658 659 660
	struct completion fcomp;

	vport = kzalloc(sizeof(struct bfad_vport_s), GFP_KERNEL);
	if (!vport) {
		rc = BFA_STATUS_ENOMEM;
		goto ext;
	}

	vport->drv_port.bfad = bfad;
	spin_lock_irqsave(&bfad->bfad_lock, flags);
K
Krishna Gudipati 已提交
661 662
	rc = bfa_fcs_vport_create(&vport->fcs_vport, &bfad->bfa_fcs, vf_id,
				  port_cfg, vport);
663 664 665 666 667
	spin_unlock_irqrestore(&bfad->bfad_lock, flags);

	if (rc != BFA_STATUS_OK)
		goto ext_free_vport;

K
Krishna Gudipati 已提交
668
	if (port_cfg->roles & BFA_LPORT_ROLE_FCP_IM) {
669 670
		rc = bfad_im_scsi_host_alloc(bfad, vport->drv_port.im_port,
							dev);
671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696
		if (rc != BFA_STATUS_OK)
			goto ext_free_fcs_vport;
	}

	spin_lock_irqsave(&bfad->bfad_lock, flags);
	bfa_fcs_vport_start(&vport->fcs_vport);
	spin_unlock_irqrestore(&bfad->bfad_lock, flags);

	return BFA_STATUS_OK;

ext_free_fcs_vport:
	spin_lock_irqsave(&bfad->bfad_lock, flags);
	vport->comp_del = &fcomp;
	init_completion(vport->comp_del);
	bfa_fcs_vport_delete(&vport->fcs_vport);
	spin_unlock_irqrestore(&bfad->bfad_lock, flags);
	wait_for_completion(vport->comp_del);
ext_free_vport:
	kfree(vport);
ext:
	return rc;
}

void
bfad_bfa_tmo(unsigned long data)
{
K
Krishna Gudipati 已提交
697 698 699
	struct bfad_s	      *bfad = (struct bfad_s *) data;
	unsigned long	flags;
	struct list_head	       doneq;
700 701 702

	spin_lock_irqsave(&bfad->bfad_lock, flags);

703
	bfa_timer_beat(&bfad->bfa.timer_mod);
704 705 706 707 708 709 710 711 712 713 714

	bfa_comp_deq(&bfad->bfa, &doneq);
	spin_unlock_irqrestore(&bfad->bfad_lock, flags);

	if (!list_empty(&doneq)) {
		bfa_comp_process(&bfad->bfa, &doneq);
		spin_lock_irqsave(&bfad->bfad_lock, flags);
		bfa_comp_free(&bfad->bfa, &doneq);
		spin_unlock_irqrestore(&bfad->bfad_lock, flags);
	}

K
Krishna Gudipati 已提交
715 716
	mod_timer(&bfad->hal_tmo,
		  jiffies + msecs_to_jiffies(BFA_TIMER_FREQ));
717 718 719 720 721 722 723 724 725
}

void
bfad_init_timer(struct bfad_s *bfad)
{
	init_timer(&bfad->hal_tmo);
	bfad->hal_tmo.function = bfad_bfa_tmo;
	bfad->hal_tmo.data = (unsigned long)bfad;

K
Krishna Gudipati 已提交
726 727
	mod_timer(&bfad->hal_tmo,
		  jiffies + msecs_to_jiffies(BFA_TIMER_FREQ));
728 729 730 731 732
}

int
bfad_pci_init(struct pci_dev *pdev, struct bfad_s *bfad)
{
K
Krishna Gudipati 已提交
733
	int		rc = -ENODEV;
734 735

	if (pci_enable_device(pdev)) {
K
Krishna Gudipati 已提交
736
		printk(KERN_ERR "pci_enable_device fail %p\n", pdev);
737 738 739 740 741 742 743 744 745
		goto out;
	}

	if (pci_request_regions(pdev, BFAD_DRIVER_NAME))
		goto out_disable_device;

	pci_set_master(pdev);


746 747 748 749
	if ((pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) ||
	    (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)) != 0)) {
		if ((pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0) ||
		   (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)) {
K
Krishna Gudipati 已提交
750
			printk(KERN_ERR "pci_set_dma_mask fail %p\n", pdev);
751 752
			goto out_release_region;
		}
753
	}
754

755
	bfad->pci_bar0_kva = pci_iomap(pdev, 0, pci_resource_len(pdev, 0));
756
	bfad->pci_bar2_kva = pci_iomap(pdev, 2, pci_resource_len(pdev, 2));
757 758

	if (bfad->pci_bar0_kva == NULL) {
K
Krishna Gudipati 已提交
759
		printk(KERN_ERR "Fail to map bar0\n");
760 761 762 763 764 765 766
		goto out_release_region;
	}

	bfad->hal_pcidev.pci_slot = PCI_SLOT(pdev->devfn);
	bfad->hal_pcidev.pci_func = PCI_FUNC(pdev->devfn);
	bfad->hal_pcidev.pci_bar_kva = bfad->pci_bar0_kva;
	bfad->hal_pcidev.device_id = pdev->device;
767
	bfad->hal_pcidev.ssid = pdev->subsystem_device;
768 769 770 771 772 773 774 775 776
	bfad->pci_name = pci_name(pdev);

	bfad->pci_attr.vendor_id = pdev->vendor;
	bfad->pci_attr.device_id = pdev->device;
	bfad->pci_attr.ssid = pdev->subsystem_device;
	bfad->pci_attr.ssvid = pdev->subsystem_vendor;
	bfad->pci_attr.pcifn = PCI_FUNC(pdev->devfn);

	bfad->pcidev = pdev;
K
Krishna Gudipati 已提交
777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824

	/* Adjust PCIe Maximum Read Request Size */
	if (pcie_max_read_reqsz > 0) {
		int pcie_cap_reg;
		u16 pcie_dev_ctl;
		u16 mask = 0xffff;

		switch (pcie_max_read_reqsz) {
		case 128:
			mask = 0x0;
			break;
		case 256:
			mask = 0x1000;
			break;
		case 512:
			mask = 0x2000;
			break;
		case 1024:
			mask = 0x3000;
			break;
		case 2048:
			mask = 0x4000;
			break;
		case 4096:
			mask = 0x5000;
			break;
		default:
			break;
		}

		pcie_cap_reg = pci_find_capability(pdev, PCI_CAP_ID_EXP);
		if (mask != 0xffff && pcie_cap_reg) {
			pcie_cap_reg += 0x08;
			pci_read_config_word(pdev, pcie_cap_reg, &pcie_dev_ctl);
			if ((pcie_dev_ctl & 0x7000) != mask) {
				printk(KERN_WARNING "BFA[%s]: "
				"pcie_max_read_request_size is %d, "
				"reset to %d\n", bfad->pci_name,
				(1 << ((pcie_dev_ctl & 0x7000) >> 12)) << 7,
				pcie_max_read_reqsz);

				pcie_dev_ctl &= ~0x7000;
				pci_write_config_word(pdev, pcie_cap_reg,
						pcie_dev_ctl | mask);
			}
		}
	}

825 826 827 828 829 830 831 832 833 834 835 836 837 838
	return 0;

out_release_region:
	pci_release_regions(pdev);
out_disable_device:
	pci_disable_device(pdev);
out:
	return rc;
}

void
bfad_pci_uninit(struct pci_dev *pdev, struct bfad_s *bfad)
{
	pci_iounmap(pdev, bfad->pci_bar0_kva);
839
	pci_iounmap(pdev, bfad->pci_bar2_kva);
840 841 842 843 844 845 846 847
	pci_release_regions(pdev);
	pci_disable_device(pdev);
	pci_set_drvdata(pdev, NULL);
}

bfa_status_t
bfad_drv_init(struct bfad_s *bfad)
{
K
Krishna Gudipati 已提交
848 849
	bfa_status_t	rc;
	unsigned long	flags;
850 851 852 853 854 855 856 857 858 859 860

	bfad->cfg_data.rport_del_timeout = rport_del_timeout;
	bfad->cfg_data.lun_queue_depth = bfa_lun_queue_depth;
	bfad->cfg_data.io_max_sge = bfa_io_max_sge;
	bfad->cfg_data.binding_method = FCP_PWWN_BINDING;

	rc = bfad_hal_mem_alloc(bfad);
	if (rc != BFA_STATUS_OK) {
		printk(KERN_WARNING "bfad%d bfad_hal_mem_alloc failure\n",
		       bfad->inst_no);
		printk(KERN_WARNING
K
Krishna Gudipati 已提交
861 862
			"Not enough memory to attach all Brocade HBA ports, %s",
			"System may need more memory.\n");
863 864 865
		goto out_hal_mem_alloc_failure;
	}

866 867
	bfad->bfa.trcmod = bfad->trcmod;
	bfad->bfa.plog = &bfad->plog_buf;
868 869 870 871 872 873 874
	bfa_plog_init(&bfad->plog_buf);
	bfa_plog_str(&bfad->plog_buf, BFA_PL_MID_DRVR, BFA_PL_EID_DRIVER_START,
		     0, "Driver Attach");

	bfa_attach(&bfad->bfa, bfad, &bfad->ioc_cfg, &bfad->meminfo,
		   &bfad->hal_pcidev);

K
Krishna Gudipati 已提交
875
	/* FCS INIT */
876
	spin_lock_irqsave(&bfad->bfad_lock, flags);
877
	bfad->bfa_fcs.trcmod = bfad->trcmod;
878
	bfa_fcs_attach(&bfad->bfa_fcs, &bfad->bfa, bfad, BFA_FALSE);
879
	bfad->bfa_fcs.fdmi_enabled = fdmi_enable;
880
	bfa_fcs_init(&bfad->bfa_fcs);
881 882 883
	spin_unlock_irqrestore(&bfad->bfad_lock, flags);

	bfad->bfad_flags |= BFAD_DRV_INIT_DONE;
K
Krishna Gudipati 已提交
884

885 886 887 888 889
	/* configure base port */
	rc = bfad_cfg_pport(bfad, BFA_LPORT_ROLE_FCP_IM);
	if (rc != BFA_STATUS_OK)
		goto out_cfg_pport_fail;

890 891
	return BFA_STATUS_OK;

892 893 894 895 896 897 898 899 900 901 902
out_cfg_pport_fail:
	/* fcs exit - on cfg pport failure */
	spin_lock_irqsave(&bfad->bfad_lock, flags);
	init_completion(&bfad->comp);
	bfad->pport.flags |= BFAD_PORT_DELETE;
	bfa_fcs_exit(&bfad->bfa_fcs);
	spin_unlock_irqrestore(&bfad->bfad_lock, flags);
	wait_for_completion(&bfad->comp);
	/* bfa detach - free hal memory */
	bfa_detach(&bfad->bfa);
	bfad_hal_mem_release(bfad);
903 904 905 906 907 908 909
out_hal_mem_alloc_failure:
	return BFA_STATUS_FAILED;
}

void
bfad_drv_uninit(struct bfad_s *bfad)
{
910 911 912 913
	unsigned long   flags;

	spin_lock_irqsave(&bfad->bfad_lock, flags);
	init_completion(&bfad->comp);
914
	bfa_iocfc_stop(&bfad->bfa);
915 916 917
	spin_unlock_irqrestore(&bfad->bfad_lock, flags);
	wait_for_completion(&bfad->comp);

918 919 920 921 922
	del_timer_sync(&bfad->hal_tmo);
	bfa_isr_disable(&bfad->bfa);
	bfa_detach(&bfad->bfa);
	bfad_remove_intr(bfad);
	bfad_hal_mem_release(bfad);
923 924

	bfad->bfad_flags &= ~BFAD_DRV_INIT_DONE;
925 926 927 928 929
}

void
bfad_drv_start(struct bfad_s *bfad)
{
K
Krishna Gudipati 已提交
930
	unsigned long	flags;
931 932

	spin_lock_irqsave(&bfad->bfad_lock, flags);
933
	bfa_iocfc_start(&bfad->bfa);
934
	bfa_fcs_pbc_vport_init(&bfad->bfa_fcs);
935
	bfa_fcs_fabric_modstart(&bfad->bfa_fcs);
936 937 938
	bfad->bfad_flags |= BFAD_HAL_START_DONE;
	spin_unlock_irqrestore(&bfad->bfad_lock, flags);

K
Krishna Gudipati 已提交
939 940
	if (bfad->im)
		flush_workqueue(bfad->im->drv_workq);
941 942 943
}

void
K
Krishna Gudipati 已提交
944
bfad_fcs_stop(struct bfad_s *bfad)
945
{
K
Krishna Gudipati 已提交
946
	unsigned long	flags;
947 948 949 950 951 952 953 954

	spin_lock_irqsave(&bfad->bfad_lock, flags);
	init_completion(&bfad->comp);
	bfad->pport.flags |= BFAD_PORT_DELETE;
	bfa_fcs_exit(&bfad->bfa_fcs);
	spin_unlock_irqrestore(&bfad->bfad_lock, flags);
	wait_for_completion(&bfad->comp);

K
Krishna Gudipati 已提交
955 956 957 958 959 960 961 962
	bfa_sm_send_event(bfad, BFAD_E_FCS_EXIT_COMP);
}

void
bfad_stop(struct bfad_s *bfad)
{
	unsigned long	flags;

963 964
	spin_lock_irqsave(&bfad->bfad_lock, flags);
	init_completion(&bfad->comp);
965
	bfa_iocfc_stop(&bfad->bfa);
966 967 968
	bfad->bfad_flags &= ~BFAD_HAL_START_DONE;
	spin_unlock_irqrestore(&bfad->bfad_lock, flags);
	wait_for_completion(&bfad->comp);
K
Krishna Gudipati 已提交
969 970

	bfa_sm_send_event(bfad, BFAD_E_EXIT_COMP);
971 972 973
}

bfa_status_t
K
Krishna Gudipati 已提交
974
bfad_cfg_pport(struct bfad_s *bfad, enum bfa_lport_role role)
975
{
K
Krishna Gudipati 已提交
976
	int		rc = BFA_STATUS_OK;
977

K
Krishna Gudipati 已提交
978 979 980
	/* Allocate scsi_host for the physical port */
	if ((supported_fc4s & BFA_LPORT_ROLE_FCP_IM) &&
	    (role & BFA_LPORT_ROLE_FCP_IM)) {
981 982 983 984 985
		if (bfad->pport.im_port == NULL) {
			rc = BFA_STATUS_FAILED;
			goto out;
		}

986 987
		rc = bfad_im_scsi_host_alloc(bfad, bfad->pport.im_port,
						&bfad->pcidev->dev);
988 989 990
		if (rc != BFA_STATUS_OK)
			goto out;

K
Krishna Gudipati 已提交
991
		bfad->pport.roles |= BFA_LPORT_ROLE_FCP_IM;
992 993 994 995 996 997 998 999 1000 1001 1002
	}

	bfad->bfad_flags |= BFAD_CFG_PPORT_DONE;

out:
	return rc;
}

void
bfad_uncfg_pport(struct bfad_s *bfad)
{
K
Krishna Gudipati 已提交
1003 1004
	if ((supported_fc4s & BFA_LPORT_ROLE_FCP_IM) &&
	    (bfad->pport.roles & BFA_LPORT_ROLE_FCP_IM)) {
1005 1006 1007
		bfad_im_scsi_host_free(bfad, bfad->pport.im_port);
		bfad_im_port_clean(bfad->pport.im_port);
		kfree(bfad->pport.im_port);
K
Krishna Gudipati 已提交
1008
		bfad->pport.roles &= ~BFA_LPORT_ROLE_FCP_IM;
1009 1010 1011 1012 1013
	}

	bfad->bfad_flags &= ~BFAD_CFG_PPORT_DONE;
}

1014
bfa_status_t
K
Krishna Gudipati 已提交
1015 1016 1017 1018 1019 1020 1021
bfad_start_ops(struct bfad_s *bfad) {

	int	retval;
	unsigned long	flags;
	struct bfad_vport_s *vport, *vport_new;
	struct bfa_fcs_driver_info_s driver_info;

1022 1023 1024 1025 1026 1027
	/* Limit min/max. xfer size to [64k-32MB] */
	if (max_xfer_size < BFAD_MIN_SECTORS >> 1)
		max_xfer_size = BFAD_MIN_SECTORS >> 1;
	if (max_xfer_size > BFAD_MAX_SECTORS >> 1)
		max_xfer_size = BFAD_MAX_SECTORS >> 1;

K
Krishna Gudipati 已提交
1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044
	/* Fill the driver_info info to fcs*/
	memset(&driver_info, 0, sizeof(driver_info));
	strncpy(driver_info.version, BFAD_DRIVER_VERSION,
		sizeof(driver_info.version) - 1);
	if (host_name)
		strncpy(driver_info.host_machine_name, host_name,
			sizeof(driver_info.host_machine_name) - 1);
	if (os_name)
		strncpy(driver_info.host_os_name, os_name,
			sizeof(driver_info.host_os_name) - 1);
	if (os_patch)
		strncpy(driver_info.host_os_patch, os_patch,
			sizeof(driver_info.host_os_patch) - 1);

	strncpy(driver_info.os_device_name, bfad->pci_name,
		sizeof(driver_info.os_device_name - 1));

1045
	/* FCS driver info init */
K
Krishna Gudipati 已提交
1046 1047 1048
	spin_lock_irqsave(&bfad->bfad_lock, flags);
	bfa_fcs_driver_info_init(&bfad->bfa_fcs, &driver_info);
	spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1049

1050 1051 1052 1053 1054 1055 1056 1057
	/*
	 * FCS update cfg - reset the pwwn/nwwn of fabric base logical port
	 * with values learned during bfa_init firmware GETATTR REQ.
	 */
	bfa_fcs_update_cfg(&bfad->bfa_fcs);

	/* Setup fc host fixed attribute if the lk supports */
	bfad_fc_host_init(bfad->pport.im_port);
1058

K
Krishna Gudipati 已提交
1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072
	/* BFAD level FC4 IM specific resource allocation */
	retval = bfad_im_probe(bfad);
	if (retval != BFA_STATUS_OK) {
		printk(KERN_WARNING "bfad_im_probe failed\n");
		if (bfa_sm_cmp_state(bfad, bfad_sm_initializing))
			bfa_sm_set_state(bfad, bfad_sm_failed);
		bfad_im_probe_undo(bfad);
		bfad->bfad_flags &= ~BFAD_FC4_PROBE_DONE;
		bfad_uncfg_pport(bfad);
		bfad_stop(bfad);
		return BFA_STATUS_FAILED;
	} else
		bfad->bfad_flags |= BFAD_FC4_PROBE_DONE;

1073 1074
	bfad_drv_start(bfad);

K
Krishna Gudipati 已提交
1075 1076 1077
	/* Complete pbc vport create */
	list_for_each_entry_safe(vport, vport_new, &bfad->pbc_vport_list,
				list_entry) {
J
Jing Huang 已提交
1078 1079
		struct fc_vport_identifiers vid;
		struct fc_vport *fc_vport;
K
Krishna Gudipati 已提交
1080
		char pwwn_buf[BFA_STRING_32];
J
Jing Huang 已提交
1081 1082 1083 1084 1085

		memset(&vid, 0, sizeof(vid));
		vid.roles = FC_PORT_ROLE_FCP_INITIATOR;
		vid.vport_type = FC_PORTTYPE_NPIV;
		vid.disable = false;
K
Krishna Gudipati 已提交
1086 1087 1088 1089
		vid.node_name = wwn_to_u64((u8 *)
				(&((vport->fcs_vport).lport.port_cfg.nwwn)));
		vid.port_name = wwn_to_u64((u8 *)
				(&((vport->fcs_vport).lport.port_cfg.pwwn)));
J
Jing Huang 已提交
1090
		fc_vport = fc_vport_create(bfad->pport.im_port->shost, 0, &vid);
K
Krishna Gudipati 已提交
1091 1092
		if (!fc_vport) {
			wwn2str(pwwn_buf, vid.port_name);
J
Jing Huang 已提交
1093
			printk(KERN_WARNING "bfad%d: failed to create pbc vport"
K
Krishna Gudipati 已提交
1094 1095 1096 1097
				" %s\n", bfad->inst_no, pwwn_buf);
		}
		list_del(&vport->list_entry);
		kfree(vport);
J
Jing Huang 已提交
1098 1099
	}

1100 1101
	/*
	 * If bfa_linkup_delay is set to -1 default; try to retrive the
M
Maggie Zhang 已提交
1102
	 * value using the bfad_get_linkup_delay(); else use the
1103 1104 1105
	 * passed in module param value as the bfa_linkup_delay.
	 */
	if (bfa_linkup_delay < 0) {
M
Maggie Zhang 已提交
1106 1107
		bfa_linkup_delay = bfad_get_linkup_delay(bfad);
		bfad_rport_online_wait(bfad);
1108
		bfa_linkup_delay = -1;
K
Krishna Gudipati 已提交
1109
	} else
M
Maggie Zhang 已提交
1110
		bfad_rport_online_wait(bfad);
1111

1112
	BFA_LOG(KERN_INFO, bfad, bfa_log_level, "bfa device claimed\n");
1113 1114 1115 1116 1117

	return BFA_STATUS_OK;
}

int
J
Jing Huang 已提交
1118
bfad_worker(void *ptr)
1119 1120 1121 1122 1123 1124 1125 1126
{
	struct bfad_s *bfad;
	unsigned long   flags;

	bfad = (struct bfad_s *)ptr;

	while (!kthread_should_stop()) {

K
Krishna Gudipati 已提交
1127 1128
		/* Send event BFAD_E_INIT_SUCCESS */
		bfa_sm_send_event(bfad, BFAD_E_INIT_SUCCESS);
1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139

		spin_lock_irqsave(&bfad->bfad_lock, flags);
		bfad->bfad_tsk = NULL;
		spin_unlock_irqrestore(&bfad->bfad_lock, flags);

		break;
	}

	return 0;
}

J
Jing Huang 已提交
1140
/*
K
Krishna Gudipati 已提交
1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197
 *  BFA driver interrupt functions
 */
irqreturn_t
bfad_intx(int irq, void *dev_id)
{
	struct bfad_s	*bfad = dev_id;
	struct list_head	doneq;
	unsigned long	flags;
	bfa_boolean_t rc;

	spin_lock_irqsave(&bfad->bfad_lock, flags);
	rc = bfa_intx(&bfad->bfa);
	if (!rc) {
		spin_unlock_irqrestore(&bfad->bfad_lock, flags);
		return IRQ_NONE;
	}

	bfa_comp_deq(&bfad->bfa, &doneq);
	spin_unlock_irqrestore(&bfad->bfad_lock, flags);

	if (!list_empty(&doneq)) {
		bfa_comp_process(&bfad->bfa, &doneq);

		spin_lock_irqsave(&bfad->bfad_lock, flags);
		bfa_comp_free(&bfad->bfa, &doneq);
		spin_unlock_irqrestore(&bfad->bfad_lock, flags);
	}

	return IRQ_HANDLED;

}

static irqreturn_t
bfad_msix(int irq, void *dev_id)
{
	struct bfad_msix_s *vec = dev_id;
	struct bfad_s *bfad = vec->bfad;
	struct list_head doneq;
	unsigned long   flags;

	spin_lock_irqsave(&bfad->bfad_lock, flags);

	bfa_msix(&bfad->bfa, vec->msix.entry);
	bfa_comp_deq(&bfad->bfa, &doneq);
	spin_unlock_irqrestore(&bfad->bfad_lock, flags);

	if (!list_empty(&doneq)) {
		bfa_comp_process(&bfad->bfa, &doneq);

		spin_lock_irqsave(&bfad->bfad_lock, flags);
		bfa_comp_free(&bfad->bfa, &doneq);
		spin_unlock_irqrestore(&bfad->bfad_lock, flags);
	}

	return IRQ_HANDLED;
}

J
Jing Huang 已提交
1198
/*
K
Krishna Gudipati 已提交
1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228
 * Initialize the MSIX entry table.
 */
static void
bfad_init_msix_entry(struct bfad_s *bfad, struct msix_entry *msix_entries,
			 int mask, int max_bit)
{
	int	i;
	int	match = 0x00000001;

	for (i = 0, bfad->nvec = 0; i < MAX_MSIX_ENTRY; i++) {
		if (mask & match) {
			bfad->msix_tab[bfad->nvec].msix.entry = i;
			bfad->msix_tab[bfad->nvec].bfad = bfad;
			msix_entries[bfad->nvec].entry = i;
			bfad->nvec++;
		}

		match <<= 1;
	}

}

int
bfad_install_msix_handler(struct bfad_s *bfad)
{
	int i, error = 0;

	for (i = 0; i < bfad->nvec; i++) {
		sprintf(bfad->msix_tab[i].name, "bfa-%s-%s",
				bfad->pci_name,
1229 1230
				((bfa_asic_id_cb(bfad->hal_pcidev.device_id)) ?
				msix_name_cb[i] : msix_name_ct[i]));
K
Krishna Gudipati 已提交
1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243

		error = request_irq(bfad->msix_tab[i].msix.vector,
				    (irq_handler_t) bfad_msix, 0,
				    bfad->msix_tab[i].name, &bfad->msix_tab[i]);
		bfa_trc(bfad, i);
		bfa_trc(bfad, bfad->msix_tab[i].msix.vector);
		if (error) {
			int	j;

			for (j = 0; j < i; j++)
				free_irq(bfad->msix_tab[j].msix.vector,
						&bfad->msix_tab[j]);

1244 1245 1246
			bfad->bfad_flags &= ~BFAD_MSIX_ON;
			pci_disable_msix(bfad->pcidev);

K
Krishna Gudipati 已提交
1247 1248 1249 1250 1251 1252 1253
			return 1;
		}
	}

	return 0;
}

J
Jing Huang 已提交
1254
/*
K
Krishna Gudipati 已提交
1255 1256 1257 1258 1259 1260 1261 1262 1263
 * Setup MSIX based interrupt.
 */
int
bfad_setup_intr(struct bfad_s *bfad)
{
	int error = 0;
	u32 mask = 0, i, num_bit = 0, max_bit = 0;
	struct msix_entry msix_entries[MAX_MSIX_ENTRY];
	struct pci_dev *pdev = bfad->pcidev;
1264
	u16	reg;
K
Krishna Gudipati 已提交
1265 1266 1267 1268 1269 1270 1271

	/* Call BFA to get the msix map for this PCI function.  */
	bfa_msix_getvecs(&bfad->bfa, &mask, &num_bit, &max_bit);

	/* Set up the msix entry table */
	bfad_init_msix_entry(bfad, msix_entries, mask, max_bit);

1272 1273
	if ((bfa_asic_id_ctc(pdev->device) && !msix_disable_ct) ||
	   (bfa_asic_id_cb(pdev->device) && !msix_disable_cb)) {
K
Krishna Gudipati 已提交
1274 1275 1276 1277 1278 1279 1280 1281 1282

		error = pci_enable_msix(bfad->pcidev, msix_entries, bfad->nvec);
		if (error) {
			/*
			 * Only error number of vector is available.
			 * We don't have a mechanism to map multiple
			 * interrupts into one vector, so even if we
			 * can try to request less vectors, we don't
			 * know how to associate interrupt events to
L
Lucas De Marchi 已提交
1283
			 *  vectors. Linux doesn't duplicate vectors
K
Krishna Gudipati 已提交
1284 1285 1286 1287 1288 1289 1290 1291 1292 1293
			 * in the MSIX table for this case.
			 */

			printk(KERN_WARNING "bfad%d: "
				"pci_enable_msix failed (%d),"
				" use line based.\n", bfad->inst_no, error);

			goto line_based;
		}

1294 1295 1296 1297 1298 1299 1300
		/* Disable INTX in MSI-X mode */
		pci_read_config_word(pdev, PCI_COMMAND, &reg);

		if (!(reg & PCI_COMMAND_INTX_DISABLE))
			pci_write_config_word(pdev, PCI_COMMAND,
				reg | PCI_COMMAND_INTX_DISABLE);

K
Krishna Gudipati 已提交
1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321
		/* Save the vectors */
		for (i = 0; i < bfad->nvec; i++) {
			bfa_trc(bfad, msix_entries[i].vector);
			bfad->msix_tab[i].msix.vector = msix_entries[i].vector;
		}

		bfa_msix_init(&bfad->bfa, bfad->nvec);

		bfad->bfad_flags |= BFAD_MSIX_ON;

		return error;
	}

line_based:
	error = 0;
	if (request_irq
	    (bfad->pcidev->irq, (irq_handler_t) bfad_intx, BFAD_IRQ_FLAGS,
	     BFAD_DRIVER_NAME, bfad) != 0) {
		/* Enable interrupt handler failed */
		return 1;
	}
1322
	bfad->bfad_flags |= BFAD_INTX_ON;
K
Krishna Gudipati 已提交
1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338

	return error;
}

void
bfad_remove_intr(struct bfad_s *bfad)
{
	int	i;

	if (bfad->bfad_flags & BFAD_MSIX_ON) {
		for (i = 0; i < bfad->nvec; i++)
			free_irq(bfad->msix_tab[i].msix.vector,
					&bfad->msix_tab[i]);

		pci_disable_msix(bfad->pcidev);
		bfad->bfad_flags &= ~BFAD_MSIX_ON;
1339
	} else if (bfad->bfad_flags & BFAD_INTX_ON) {
K
Krishna Gudipati 已提交
1340 1341 1342
		free_irq(bfad->pcidev->irq, bfad);
	}
}
1343

J
Jing Huang 已提交
1344
/*
1345 1346 1347 1348 1349
 * PCI probe entry.
 */
int
bfad_pci_probe(struct pci_dev *pdev, const struct pci_device_id *pid)
{
K
Krishna Gudipati 已提交
1350 1351
	struct bfad_s	*bfad;
	int		error = -ENODEV, retval;
1352

K
Krishna Gudipati 已提交
1353
	/* For single port cards - only claim function 0 */
1354
	if ((pdev->device == BFA_PCI_DEVICE_ID_FC_8G1P) &&
K
Krishna Gudipati 已提交
1355
		(PCI_FUNC(pdev->devfn) != 0))
1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370
		return -ENODEV;

	bfad = kzalloc(sizeof(struct bfad_s), GFP_KERNEL);
	if (!bfad) {
		error = -ENOMEM;
		goto out;
	}

	bfad->trcmod = kzalloc(sizeof(struct bfa_trc_mod_s), GFP_KERNEL);
	if (!bfad->trcmod) {
		printk(KERN_WARNING "Error alloc trace buffer!\n");
		error = -ENOMEM;
		goto out_alloc_trace_failure;
	}

K
Krishna Gudipati 已提交
1371
	/* TRACE INIT */
1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391
	bfa_trc_init(bfad->trcmod);
	bfa_trc(bfad, bfad_inst);

	if (!(bfad_load_fwimg(pdev))) {
		kfree(bfad->trcmod);
		goto out_alloc_trace_failure;
	}

	retval = bfad_pci_init(pdev, bfad);
	if (retval) {
		printk(KERN_WARNING "bfad_pci_init failure!\n");
		error = retval;
		goto out_pci_init_failure;
	}

	mutex_lock(&bfad_mutex);
	bfad->inst_no = bfad_inst++;
	list_add_tail(&bfad->list_entry, &bfad_list);
	mutex_unlock(&bfad_mutex);

K
Krishna Gudipati 已提交
1392 1393 1394
	/* Initializing the state machine: State set to uninit */
	bfa_sm_set_state(bfad, bfad_sm_uninit);

1395 1396 1397 1398 1399
	spin_lock_init(&bfad->bfad_lock);
	pci_set_drvdata(pdev, bfad);

	bfad->ref_count = 0;
	bfad->pport.bfad = bfad;
K
Krishna Gudipati 已提交
1400
	INIT_LIST_HEAD(&bfad->pbc_vport_list);
1401

1402 1403 1404 1405
	/* Setup the debugfs node for this bfad */
	if (bfa_debugfs_enable)
		bfad_debugfs_init(&bfad->pport);

1406 1407 1408 1409
	retval = bfad_drv_init(bfad);
	if (retval != BFA_STATUS_OK)
		goto out_drv_init_failure;

K
Krishna Gudipati 已提交
1410
	bfa_sm_send_event(bfad, BFAD_E_CREATE);
1411

K
Krishna Gudipati 已提交
1412 1413
	if (bfa_sm_cmp_state(bfad, bfad_sm_uninit))
		goto out_bfad_sm_failure;
1414 1415 1416

	return 0;

K
Krishna Gudipati 已提交
1417 1418 1419
out_bfad_sm_failure:
	bfa_detach(&bfad->bfa);
	bfad_hal_mem_release(bfad);
1420
out_drv_init_failure:
1421 1422 1423
	/* Remove the debugfs node for this bfad */
	kfree(bfad->regdata);
	bfad_debugfs_exit(&bfad->pport);
1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436
	mutex_lock(&bfad_mutex);
	bfad_inst--;
	list_del(&bfad->list_entry);
	mutex_unlock(&bfad_mutex);
	bfad_pci_uninit(pdev, bfad);
out_pci_init_failure:
	kfree(bfad->trcmod);
out_alloc_trace_failure:
	kfree(bfad);
out:
	return error;
}

J
Jing Huang 已提交
1437
/*
1438 1439 1440 1441 1442
 * PCI remove entry.
 */
void
bfad_pci_remove(struct pci_dev *pdev)
{
K
Krishna Gudipati 已提交
1443 1444
	struct bfad_s	      *bfad = pci_get_drvdata(pdev);
	unsigned long	flags;
1445 1446 1447

	bfa_trc(bfad, bfad->inst_no);

1448
	spin_lock_irqsave(&bfad->bfad_lock, flags);
K
Krishna Gudipati 已提交
1449
	if (bfad->bfad_tsk != NULL) {
1450
		spin_unlock_irqrestore(&bfad->bfad_lock, flags);
K
Krishna Gudipati 已提交
1451 1452
		kthread_stop(bfad->bfad_tsk);
	} else {
1453 1454
		spin_unlock_irqrestore(&bfad->bfad_lock, flags);
	}
1455

K
Krishna Gudipati 已提交
1456 1457
	/* Send Event BFAD_E_STOP */
	bfa_sm_send_event(bfad, BFAD_E_STOP);
1458

K
Krishna Gudipati 已提交
1459
	/* Driver detach and dealloc mem */
1460 1461 1462 1463 1464
	spin_lock_irqsave(&bfad->bfad_lock, flags);
	bfa_detach(&bfad->bfa);
	spin_unlock_irqrestore(&bfad->bfad_lock, flags);
	bfad_hal_mem_release(bfad);

1465 1466 1467 1468
	/* Remove the debugfs node for this bfad */
	kfree(bfad->regdata);
	bfad_debugfs_exit(&bfad->pport);

K
Krishna Gudipati 已提交
1469
	/* Cleaning the BFAD instance */
1470 1471 1472 1473 1474 1475 1476 1477 1478 1479
	mutex_lock(&bfad_mutex);
	bfad_inst--;
	list_del(&bfad->list_entry);
	mutex_unlock(&bfad_mutex);
	bfad_pci_uninit(pdev, bfad);

	kfree(bfad->trcmod);
	kfree(bfad);
}

K
Krishna Gudipati 已提交
1480
struct pci_device_id bfad_id_table[] = {
1481
	{
K
Krishna Gudipati 已提交
1482 1483 1484 1485 1486
		.vendor = BFA_PCI_VENDOR_ID_BROCADE,
		.device = BFA_PCI_DEVICE_ID_FC_8G2P,
		.subvendor = PCI_ANY_ID,
		.subdevice = PCI_ANY_ID,
	},
1487
	{
K
Krishna Gudipati 已提交
1488 1489 1490 1491 1492
		.vendor = BFA_PCI_VENDOR_ID_BROCADE,
		.device = BFA_PCI_DEVICE_ID_FC_8G1P,
		.subvendor = PCI_ANY_ID,
		.subdevice = PCI_ANY_ID,
	},
1493
	{
K
Krishna Gudipati 已提交
1494 1495 1496 1497 1498 1499 1500
		.vendor = BFA_PCI_VENDOR_ID_BROCADE,
		.device = BFA_PCI_DEVICE_ID_CT,
		.subvendor = PCI_ANY_ID,
		.subdevice = PCI_ANY_ID,
		.class = (PCI_CLASS_SERIAL_FIBER << 8),
		.class_mask = ~0,
	},
J
Jing Huang 已提交
1501
	{
K
Krishna Gudipati 已提交
1502 1503 1504 1505 1506 1507
		.vendor = BFA_PCI_VENDOR_ID_BROCADE,
		.device = BFA_PCI_DEVICE_ID_CT_FC,
		.subvendor = PCI_ANY_ID,
		.subdevice = PCI_ANY_ID,
		.class = (PCI_CLASS_SERIAL_FIBER << 8),
		.class_mask = ~0,
J
Jing Huang 已提交
1508
	},
1509 1510 1511 1512 1513 1514 1515 1516
	{
		.vendor = BFA_PCI_VENDOR_ID_BROCADE,
		.device = BFA_PCI_DEVICE_ID_CT2,
		.subvendor = PCI_ANY_ID,
		.subdevice = PCI_ANY_ID,
		.class = (PCI_CLASS_SERIAL_FIBER << 8),
		.class_mask = ~0,
	},
1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529

	{0, 0},
};

MODULE_DEVICE_TABLE(pci, bfad_id_table);

static struct pci_driver bfad_pci_driver = {
	.name = BFAD_DRIVER_NAME,
	.id_table = bfad_id_table,
	.probe = bfad_pci_probe,
	.remove = __devexit_p(bfad_pci_remove),
};

J
Jing Huang 已提交
1530
/*
1531 1532
 * Driver module init.
 */
K
Krishna Gudipati 已提交
1533
static int __init
1534 1535
bfad_init(void)
{
K
Krishna Gudipati 已提交
1536
	int		error = 0;
1537 1538

	printk(KERN_INFO "Brocade BFA FC/FCOE SCSI driver - version: %s\n",
K
Krishna Gudipati 已提交
1539
			BFAD_DRIVER_VERSION);
1540 1541 1542 1543

	if (num_sgpgs > 0)
		num_sgpgs_parm = num_sgpgs;

K
Krishna Gudipati 已提交
1544
	error = bfad_im_module_init();
1545 1546
	if (error) {
		error = -ENOMEM;
K
Krishna Gudipati 已提交
1547
		printk(KERN_WARNING "bfad_im_module_init failure\n");
1548 1549 1550
		goto ext;
	}

K
Krishna Gudipati 已提交
1551 1552
	if (strcmp(FCPI_NAME, " fcpim") == 0)
		supported_fc4s |= BFA_LPORT_ROLE_FCP_IM;
1553

1554
	bfa_auto_recover = ioc_auto_recover;
1555 1556
	bfa_fcs_rport_set_del_timeout(rport_del_timeout);

K
Krishna Gudipati 已提交
1557
	error = pci_register_driver(&bfad_pci_driver);
1558
	if (error) {
K
Krishna Gudipati 已提交
1559
		printk(KERN_WARNING "pci_register_driver failure\n");
1560 1561 1562 1563 1564 1565
		goto ext;
	}

	return 0;

ext:
K
Krishna Gudipati 已提交
1566
	bfad_im_module_exit();
1567 1568 1569
	return error;
}

J
Jing Huang 已提交
1570
/*
1571 1572
 * Driver module exit.
 */
K
Krishna Gudipati 已提交
1573
static void __exit
1574 1575 1576
bfad_exit(void)
{
	pci_unregister_driver(&bfad_pci_driver);
K
Krishna Gudipati 已提交
1577
	bfad_im_module_exit();
1578 1579 1580
	bfad_free_fwimg();
}

K
Krishna Gudipati 已提交
1581
/* Firmware handling */
J
Jing Huang 已提交
1582
static void
K
Krishna Gudipati 已提交
1583 1584 1585 1586 1587 1588 1589
bfad_read_firmware(struct pci_dev *pdev, u32 **bfi_image,
		u32 *bfi_image_size, char *fw_name)
{
	const struct firmware *fw;

	if (request_firmware(&fw, fw_name, &pdev->dev)) {
		printk(KERN_ALERT "Can't locate firmware %s\n", fw_name);
J
Jing Huang 已提交
1590 1591
		*bfi_image = NULL;
		goto out;
K
Krishna Gudipati 已提交
1592 1593 1594 1595 1596 1597
	}

	*bfi_image = vmalloc(fw->size);
	if (NULL == *bfi_image) {
		printk(KERN_ALERT "Fail to allocate buffer for fw image "
			"size=%x!\n", (u32) fw->size);
J
Jing Huang 已提交
1598
		goto out;
K
Krishna Gudipati 已提交
1599 1600 1601 1602
	}

	memcpy(*bfi_image, fw->data, fw->size);
	*bfi_image_size = fw->size/sizeof(u32);
J
Jing Huang 已提交
1603 1604
out:
	release_firmware(fw);
K
Krishna Gudipati 已提交
1605 1606
}

J
Jing Huang 已提交
1607 1608
static u32 *
bfad_load_fwimg(struct pci_dev *pdev)
K
Krishna Gudipati 已提交
1609
{
1610 1611 1612 1613 1614 1615 1616 1617 1618 1619
	if (pdev->device == BFA_PCI_DEVICE_ID_CT2) {
		if (bfi_image_ct2_size == 0)
			bfad_read_firmware(pdev, &bfi_image_ct2,
				&bfi_image_ct2_size, BFAD_FW_FILE_CT2);
		return bfi_image_ct2;
	} else if (bfa_asic_id_ct(pdev->device)) {
		if (bfi_image_ct_size == 0)
			bfad_read_firmware(pdev, &bfi_image_ct,
				&bfi_image_ct_size, BFAD_FW_FILE_CT);
		return bfi_image_ct;
K
Krishna Gudipati 已提交
1620
	} else {
1621 1622 1623 1624
		if (bfi_image_cb_size == 0)
			bfad_read_firmware(pdev, &bfi_image_cb,
				&bfi_image_cb_size, BFAD_FW_FILE_CB);
		return bfi_image_cb;
K
Krishna Gudipati 已提交
1625 1626
	}
}
1627

J
Jing Huang 已提交
1628 1629 1630
static void
bfad_free_fwimg(void)
{
1631 1632 1633 1634 1635 1636
	if (bfi_image_ct2_size && bfi_image_ct2)
		vfree(bfi_image_ct2);
	if (bfi_image_ct_size && bfi_image_ct)
		vfree(bfi_image_ct);
	if (bfi_image_cb_size && bfi_image_cb)
		vfree(bfi_image_cb);
J
Jing Huang 已提交
1637 1638
}

1639 1640 1641 1642 1643 1644
module_init(bfad_init);
module_exit(bfad_exit);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Brocade Fibre Channel HBA Driver" BFAD_PROTO_NAME);
MODULE_AUTHOR("Brocade Communications Systems, Inc.");
MODULE_VERSION(BFAD_DRIVER_VERSION);