bnx2fc_fcoe.c 73.4 KB
Newer Older
1
/* bnx2fc_fcoe.c: QLogic NetXtreme II Linux FCoE offload driver.
2 3 4 5
 * This file contains the code that interacts with libfc, libfcoe,
 * cnic modules to create FCoE instances, send/receive non-offloaded
 * FIP/FCoE packets, listen to link events etc.
 *
6
 * Copyright (c) 2008 - 2013 Broadcom Corporation
7
 * Copyright (c) 2014, QLogic Corporation
8 9 10 11 12 13 14 15 16 17 18
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation.
 *
 * Written by: Bhanu Prakash Gollapudi (bprakash@broadcom.com)
 */

#include "bnx2fc.h"

static struct list_head adapter_list;
19
static struct list_head if_list;
20 21 22 23 24 25
static u32 adapter_count;
static DEFINE_MUTEX(bnx2fc_dev_lock);
DEFINE_PER_CPU(struct bnx2fc_percpu_s, bnx2fc_percpu);

#define DRV_MODULE_NAME		"bnx2fc"
#define DRV_MODULE_VERSION	BNX2FC_VERSION
26
#define DRV_MODULE_RELDATE	"Dec 11, 2013"
27 28


29
static char version[] =
30
		"QLogic NetXtreme II FCoE Driver " DRV_MODULE_NAME \
31 32 33 34
		" v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")\n";


MODULE_AUTHOR("Bhanu Prakash Gollapudi <bprakash@broadcom.com>");
35
MODULE_DESCRIPTION("QLogic NetXtreme II BCM57710 FCoE Driver");
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
MODULE_LICENSE("GPL");
MODULE_VERSION(DRV_MODULE_VERSION);

#define BNX2FC_MAX_QUEUE_DEPTH	256
#define BNX2FC_MIN_QUEUE_DEPTH	32
#define FCOE_WORD_TO_BYTE  4

static struct scsi_transport_template	*bnx2fc_transport_template;
static struct scsi_transport_template	*bnx2fc_vport_xport_template;

struct workqueue_struct *bnx2fc_wq;

/* bnx2fc structure needs only one instance of the fcoe_percpu_s structure.
 * Here the io threads are per cpu but the l2 thread is just one
 */
struct fcoe_percpu_s bnx2fc_global;
DEFINE_SPINLOCK(bnx2fc_global_lock);

static struct cnic_ulp_ops bnx2fc_cnic_cb;
static struct libfc_function_template bnx2fc_libfc_fcn_templ;
static struct scsi_host_template bnx2fc_shost_template;
static struct fc_function_template bnx2fc_transport_function;
58
static struct fcoe_sysfs_function_template bnx2fc_fcoe_sysfs_templ;
59 60
static struct fc_function_template bnx2fc_vport_xport_function;
static int bnx2fc_create(struct net_device *netdev, enum fip_state fip_mode);
61
static void __bnx2fc_destroy(struct bnx2fc_interface *interface);
62 63 64 65
static int bnx2fc_destroy(struct net_device *net_device);
static int bnx2fc_enable(struct net_device *netdev);
static int bnx2fc_disable(struct net_device *netdev);

66 67 68 69
/* fcoe_syfs control interface handlers */
static int bnx2fc_ctlr_alloc(struct net_device *netdev);
static int bnx2fc_ctlr_enabled(struct fcoe_ctlr_device *cdev);

70 71
static void bnx2fc_recv_frame(struct sk_buff *skb);

72
static void bnx2fc_start_disc(struct bnx2fc_interface *interface);
73 74
static int bnx2fc_shost_config(struct fc_lport *lport, struct device *dev);
static int bnx2fc_lport_config(struct fc_lport *lport);
75
static int bnx2fc_em_config(struct fc_lport *lport, struct bnx2fc_hba *hba);
76 77 78 79
static int bnx2fc_bind_adapter_devices(struct bnx2fc_hba *hba);
static void bnx2fc_unbind_adapter_devices(struct bnx2fc_hba *hba);
static int bnx2fc_bind_pcidev(struct bnx2fc_hba *hba);
static void bnx2fc_unbind_pcidev(struct bnx2fc_hba *hba);
80
static struct fc_lport *bnx2fc_if_create(struct bnx2fc_interface *interface,
81 82 83 84
				  struct device *parent, int npiv);
static void bnx2fc_destroy_work(struct work_struct *work);

static struct bnx2fc_hba *bnx2fc_hba_lookup(struct net_device *phys_dev);
85 86
static struct bnx2fc_interface *bnx2fc_interface_lookup(struct net_device
							*phys_dev);
87
static inline void bnx2fc_interface_put(struct bnx2fc_interface *interface);
88 89 90 91 92 93
static struct bnx2fc_hba *bnx2fc_find_hba_for_cnic(struct cnic_dev *cnic);

static int bnx2fc_fw_init(struct bnx2fc_hba *hba);
static void bnx2fc_fw_destroy(struct bnx2fc_hba *hba);

static void bnx2fc_port_shutdown(struct fc_lport *lport);
94
static void bnx2fc_stop(struct bnx2fc_interface *interface);
95 96 97 98 99 100 101 102 103 104 105 106 107
static int __init bnx2fc_mod_init(void);
static void __exit bnx2fc_mod_exit(void);

unsigned int bnx2fc_debug_level;
module_param_named(debug_logging, bnx2fc_debug_level, int, S_IRUGO|S_IWUSR);

static int bnx2fc_cpu_callback(struct notifier_block *nfb,
			     unsigned long action, void *hcpu);
/* notification function for CPU hotplug events */
static struct notifier_block bnx2fc_cpu_notifier = {
	.notifier_call = bnx2fc_cpu_callback,
};

108 109 110 111 112 113
static inline struct net_device *bnx2fc_netdev(const struct fc_lport *lport)
{
	return ((struct bnx2fc_interface *)
		((struct fcoe_port *)lport_priv(lport))->priv)->netdev;
}

114 115 116 117 118 119 120 121 122 123
static void bnx2fc_fcf_get_vlan_id(struct fcoe_fcf_device *fcf_dev)
{
	struct fcoe_ctlr_device *ctlr_dev =
		fcoe_fcf_dev_to_ctlr_dev(fcf_dev);
	struct fcoe_ctlr *ctlr = fcoe_ctlr_device_priv(ctlr_dev);
	struct bnx2fc_interface *fcoe = fcoe_ctlr_priv(ctlr);

	fcf_dev->vlan_id = fcoe->vlan_id;
}

124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
static void bnx2fc_clean_rx_queue(struct fc_lport *lp)
{
	struct fcoe_percpu_s *bg;
	struct fcoe_rcv_info *fr;
	struct sk_buff_head *list;
	struct sk_buff *skb, *next;
	struct sk_buff *head;

	bg = &bnx2fc_global;
	spin_lock_bh(&bg->fcoe_rx_list.lock);
	list = &bg->fcoe_rx_list;
	head = list->next;
	for (skb = head; skb != (struct sk_buff *)list;
	     skb = next) {
		next = skb->next;
		fr = fcoe_dev_from_skb(skb);
		if (fr->fr_dev == lp) {
			__skb_unlink(skb, list);
			kfree_skb(skb);
		}
	}
	spin_unlock_bh(&bg->fcoe_rx_list.lock);
}

int bnx2fc_get_paged_crc_eof(struct sk_buff *skb, int tlen)
{
	int rc;
	spin_lock(&bnx2fc_global_lock);
	rc = fcoe_get_paged_crc_eof(skb, tlen, &bnx2fc_global);
	spin_unlock(&bnx2fc_global_lock);

	return rc;
}

static void bnx2fc_abort_io(struct fc_lport *lport)
{
	/*
	 * This function is no-op for bnx2fc, but we do
	 * not want to leave it as NULL either, as libfc
	 * can call the default function which is
	 * fc_fcp_abort_io.
	 */
}

static void bnx2fc_cleanup(struct fc_lport *lport)
{
	struct fcoe_port *port = lport_priv(lport);
171 172
	struct bnx2fc_interface *interface = port->priv;
	struct bnx2fc_hba *hba = interface->hba;
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
	struct bnx2fc_rport *tgt;
	int i;

	BNX2FC_MISC_DBG("Entered %s\n", __func__);
	mutex_lock(&hba->hba_mutex);
	spin_lock_bh(&hba->hba_lock);
	for (i = 0; i < BNX2FC_NUM_MAX_SESS; i++) {
		tgt = hba->tgt_ofld_list[i];
		if (tgt) {
			/* Cleanup IOs belonging to requested vport */
			if (tgt->port == port) {
				spin_unlock_bh(&hba->hba_lock);
				BNX2FC_TGT_DBG(tgt, "flush/cleanup\n");
				bnx2fc_flush_active_ios(tgt);
				spin_lock_bh(&hba->hba_lock);
			}
		}
	}
	spin_unlock_bh(&hba->hba_lock);
	mutex_unlock(&hba->hba_mutex);
}

static int bnx2fc_xmit_l2_frame(struct bnx2fc_rport *tgt,
			     struct fc_frame *fp)
{
	struct fc_rport_priv *rdata = tgt->rdata;
	struct fc_frame_header *fh;
	int rc = 0;

	fh = fc_frame_header_get(fp);
	BNX2FC_TGT_DBG(tgt, "Xmit L2 frame rport = 0x%x, oxid = 0x%x, "
			"r_ctl = 0x%x\n", rdata->ids.port_id,
			ntohs(fh->fh_ox_id), fh->fh_r_ctl);
	if ((fh->fh_type == FC_TYPE_ELS) &&
	    (fh->fh_r_ctl == FC_RCTL_ELS_REQ)) {

		switch (fc_frame_payload_op(fp)) {
		case ELS_ADISC:
			rc = bnx2fc_send_adisc(tgt, fp);
			break;
		case ELS_LOGO:
			rc = bnx2fc_send_logo(tgt, fp);
			break;
		case ELS_RLS:
			rc = bnx2fc_send_rls(tgt, fp);
			break;
		default:
			break;
		}
	} else if ((fh->fh_type ==  FC_TYPE_BLS) &&
	    (fh->fh_r_ctl == FC_RCTL_BA_ABTS))
		BNX2FC_TGT_DBG(tgt, "ABTS frame\n");
	else {
		BNX2FC_TGT_DBG(tgt, "Send L2 frame type 0x%x "
				"rctl 0x%x thru non-offload path\n",
				fh->fh_type, fh->fh_r_ctl);
		return -ENODEV;
	}
	if (rc)
		return -ENOMEM;
	else
		return 0;
}

/**
 * bnx2fc_xmit - bnx2fc's FCoE frame transmit function
 *
 * @lport:	the associated local port
 * @fp:	the fc_frame to be transmitted
 */
static int bnx2fc_xmit(struct fc_lport *lport, struct fc_frame *fp)
{
	struct ethhdr		*eh;
	struct fcoe_crc_eof	*cp;
	struct sk_buff		*skb;
	struct fc_frame_header	*fh;
249
	struct bnx2fc_interface	*interface;
250
	struct fcoe_ctlr        *ctlr;
251
	struct bnx2fc_hba *hba;
252 253 254
	struct fcoe_port	*port;
	struct fcoe_hdr		*hp;
	struct bnx2fc_rport	*tgt;
255
	struct fc_stats		*stats;
256 257 258 259 260 261
	u8			sof, eof;
	u32			crc;
	unsigned int		hlen, tlen, elen;
	int			wlen, rc = 0;

	port = (struct fcoe_port *)lport_priv(lport);
262
	interface = port->priv;
263
	ctlr = bnx2fc_to_ctlr(interface);
264
	hba = interface->hba;
265 266 267 268 269 270 271 272 273 274 275

	fh = fc_frame_header_get(fp);

	skb = fp_skb(fp);
	if (!lport->link_up) {
		BNX2FC_HBA_DBG(lport, "bnx2fc_xmit link down\n");
		kfree_skb(skb);
		return 0;
	}

	if (unlikely(fh->fh_r_ctl == FC_RCTL_ELS_REQ)) {
276
		if (!ctlr->sel_fcf) {
277 278 279 280
			BNX2FC_HBA_DBG(lport, "FCF not selected yet!\n");
			kfree_skb(skb);
			return -EINVAL;
		}
281
		if (fcoe_ctlr_els_send(ctlr, lport, skb))
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 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 328 329
			return 0;
	}

	sof = fr_sof(fp);
	eof = fr_eof(fp);

	/*
	 * Snoop the frame header to check if the frame is for
	 * an offloaded session
	 */
	/*
	 * tgt_ofld_list access is synchronized using
	 * both hba mutex and hba lock. Atleast hba mutex or
	 * hba lock needs to be held for read access.
	 */

	spin_lock_bh(&hba->hba_lock);
	tgt = bnx2fc_tgt_lookup(port, ntoh24(fh->fh_d_id));
	if (tgt && (test_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags))) {
		/* This frame is for offloaded session */
		BNX2FC_HBA_DBG(lport, "xmit: Frame is for offloaded session "
				"port_id = 0x%x\n", ntoh24(fh->fh_d_id));
		spin_unlock_bh(&hba->hba_lock);
		rc = bnx2fc_xmit_l2_frame(tgt, fp);
		if (rc != -ENODEV) {
			kfree_skb(skb);
			return rc;
		}
	} else {
		spin_unlock_bh(&hba->hba_lock);
	}

	elen = sizeof(struct ethhdr);
	hlen = sizeof(struct fcoe_hdr);
	tlen = sizeof(struct fcoe_crc_eof);
	wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;

	skb->ip_summed = CHECKSUM_NONE;
	crc = fcoe_fc_crc(fp);

	/* copy port crc and eof to the skb buff */
	if (skb_is_nonlinear(skb)) {
		skb_frag_t *frag;
		if (bnx2fc_get_paged_crc_eof(skb, tlen)) {
			kfree_skb(skb);
			return -ENOMEM;
		}
		frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
330
		cp = kmap_atomic(skb_frag_page(frag)) + frag->page_offset;
331 332 333 334 335 336 337 338
	} else {
		cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
	}

	memset(cp, 0, sizeof(*cp));
	cp->fcoe_eof = eof;
	cp->fcoe_crc32 = cpu_to_le32(~crc);
	if (skb_is_nonlinear(skb)) {
339
		kunmap_atomic(cp);
340 341 342 343 344 345 346 347 348
		cp = NULL;
	}

	/* adjust skb network/transport offsets to match mac/fcoe/port */
	skb_push(skb, elen + hlen);
	skb_reset_mac_header(skb);
	skb_reset_network_header(skb);
	skb->mac_len = elen;
	skb->protocol = htons(ETH_P_FCOE);
349
	skb->dev = interface->netdev;
350 351 352 353

	/* fill up mac and fcoe headers */
	eh = eth_hdr(skb);
	eh->h_proto = htons(ETH_P_FCOE);
354
	if (ctlr->map_dest)
355 356 357
		fc_fcoe_set_mac(eh->h_dest, fh->fh_d_id);
	else
		/* insert GW address */
358
		memcpy(eh->h_dest, ctlr->dest_addr, ETH_ALEN);
359

360 361
	if (unlikely(ctlr->flogi_oxid != FC_XID_UNKNOWN))
		memcpy(eh->h_source, ctlr->ctl_src_addr, ETH_ALEN);
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
	else
		memcpy(eh->h_source, port->data_src_addr, ETH_ALEN);

	hp = (struct fcoe_hdr *)(eh + 1);
	memset(hp, 0, sizeof(*hp));
	if (FC_FCOE_VER)
		FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
	hp->fcoe_sof = sof;

	/* fcoe lso, mss is in max_payload which is non-zero for FCP data */
	if (lport->seq_offload && fr_max_payload(fp)) {
		skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
		skb_shinfo(skb)->gso_size = fr_max_payload(fp);
	} else {
		skb_shinfo(skb)->gso_type = 0;
		skb_shinfo(skb)->gso_size = 0;
	}

	/*update tx stats */
381
	stats = per_cpu_ptr(lport->stats, get_cpu());
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
	stats->TxFrames++;
	stats->TxWords += wlen;
	put_cpu();

	/* send down to lld */
	fr_dev(fp) = lport;
	if (port->fcoe_pending_queue.qlen)
		fcoe_check_wait_queue(lport, skb);
	else if (fcoe_start_io(skb))
		fcoe_check_wait_queue(lport, skb);

	return 0;
}

/**
 * bnx2fc_rcv - This is bnx2fc's receive function called by NET_RX_SOFTIRQ
 *
 * @skb:	the receive socket buffer
 * @dev:	associated net device
 * @ptype:	context
 * @olddev:	last device
 *
 * This function receives the packet and builds FC frame and passes it up
 */
static int bnx2fc_rcv(struct sk_buff *skb, struct net_device *dev,
		struct packet_type *ptype, struct net_device *olddev)
{
	struct fc_lport *lport;
410
	struct bnx2fc_interface *interface;
411
	struct fcoe_ctlr *ctlr;
412 413 414
	struct fc_frame_header *fh;
	struct fcoe_rcv_info *fr;
	struct fcoe_percpu_s *bg;
415
	struct sk_buff *tmp_skb;
416 417
	unsigned short oxid;

418 419
	interface = container_of(ptype, struct bnx2fc_interface,
				 fcoe_packet_type);
420 421
	ctlr = bnx2fc_to_ctlr(interface);
	lport = ctlr->lp;
422 423

	if (unlikely(lport == NULL)) {
424
		printk(KERN_ERR PFX "bnx2fc_rcv: lport is NULL\n");
425 426 427
		goto err;
	}

428 429 430 431 432 433
	tmp_skb = skb_share_check(skb, GFP_ATOMIC);
	if (!tmp_skb)
		goto err;

	skb = tmp_skb;

434
	if (unlikely(eth_hdr(skb)->h_proto != htons(ETH_P_FCOE))) {
435
		printk(KERN_ERR PFX "bnx2fc_rcv: Wrong FC type frame\n");
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
		goto err;
	}

	/*
	 * Check for minimum frame length, and make sure required FCoE
	 * and FC headers are pulled into the linear data area.
	 */
	if (unlikely((skb->len < FCOE_MIN_FRAME) ||
	    !pskb_may_pull(skb, FCOE_HEADER_LEN)))
		goto err;

	skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
	fh = (struct fc_frame_header *) skb_transport_header(skb);

	oxid = ntohs(fh->fh_ox_id);

	fr = fcoe_dev_from_skb(skb);
	fr->fr_dev = lport;

	bg = &bnx2fc_global;
456
	spin_lock(&bg->fcoe_rx_list.lock);
457 458 459 460 461

	__skb_queue_tail(&bg->fcoe_rx_list, skb);
	if (bg->fcoe_rx_list.qlen == 1)
		wake_up_process(bg->thread);

462
	spin_unlock(&bg->fcoe_rx_list.lock);
463 464 465 466 467 468 469 470 471 472 473 474

	return 0;
err:
	kfree_skb(skb);
	return -1;
}

static int bnx2fc_l2_rcv_thread(void *arg)
{
	struct fcoe_percpu_s *bg = arg;
	struct sk_buff *skb;

475
	set_user_nice(current, MIN_NICE);
476 477 478 479 480 481 482 483 484
	set_current_state(TASK_INTERRUPTIBLE);
	while (!kthread_should_stop()) {
		schedule();
		spin_lock_bh(&bg->fcoe_rx_list.lock);
		while ((skb = __skb_dequeue(&bg->fcoe_rx_list)) != NULL) {
			spin_unlock_bh(&bg->fcoe_rx_list.lock);
			bnx2fc_recv_frame(skb);
			spin_lock_bh(&bg->fcoe_rx_list.lock);
		}
485
		__set_current_state(TASK_INTERRUPTIBLE);
486 487
		spin_unlock_bh(&bg->fcoe_rx_list.lock);
	}
488
	__set_current_state(TASK_RUNNING);
489 490 491 492 493 494 495 496 497
	return 0;
}


static void bnx2fc_recv_frame(struct sk_buff *skb)
{
	u32 fr_len;
	struct fc_lport *lport;
	struct fcoe_rcv_info *fr;
498
	struct fc_stats *stats;
499 500 501 502 503 504 505 506 507 508 509 510
	struct fc_frame_header *fh;
	struct fcoe_crc_eof crc_eof;
	struct fc_frame *fp;
	struct fc_lport *vn_port;
	struct fcoe_port *port;
	u8 *mac = NULL;
	u8 *dest_mac = NULL;
	struct fcoe_hdr *hp;

	fr = fcoe_dev_from_skb(skb);
	lport = fr->fr_dev;
	if (unlikely(lport == NULL)) {
511
		printk(KERN_ERR PFX "Invalid lport struct\n");
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
		kfree_skb(skb);
		return;
	}

	if (skb_is_nonlinear(skb))
		skb_linearize(skb);
	mac = eth_hdr(skb)->h_source;
	dest_mac = eth_hdr(skb)->h_dest;

	/* Pull the header */
	hp = (struct fcoe_hdr *) skb_network_header(skb);
	fh = (struct fc_frame_header *) skb_transport_header(skb);
	skb_pull(skb, sizeof(struct fcoe_hdr));
	fr_len = skb->len - sizeof(struct fcoe_crc_eof);

	fp = (struct fc_frame *)skb;
	fc_frame_init(fp);
	fr_dev(fp) = lport;
	fr_sof(fp) = hp->fcoe_sof;
	if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof))) {
		kfree_skb(skb);
		return;
	}
	fr_eof(fp) = crc_eof.fcoe_eof;
	fr_crc(fp) = crc_eof.fcoe_crc32;
	if (pskb_trim(skb, fr_len)) {
		kfree_skb(skb);
		return;
	}

	fh = fc_frame_header_get(fp);

	vn_port = fc_vport_id_lookup(lport, ntoh24(fh->fh_d_id));
	if (vn_port) {
		port = lport_priv(vn_port);
547
		if (!ether_addr_equal(port->data_src_addr, dest_mac)) {
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
			BNX2FC_HBA_DBG(lport, "fpma mismatch\n");
			kfree_skb(skb);
			return;
		}
	}
	if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA &&
	    fh->fh_type == FC_TYPE_FCP) {
		/* Drop FCP data. We dont this in L2 path */
		kfree_skb(skb);
		return;
	}
	if (fh->fh_r_ctl == FC_RCTL_ELS_REQ &&
	    fh->fh_type == FC_TYPE_ELS) {
		switch (fc_frame_payload_op(fp)) {
		case ELS_LOGO:
			if (ntoh24(fh->fh_s_id) == FC_FID_FLOGI) {
				/* drop non-FIP LOGO */
				kfree_skb(skb);
				return;
			}
			break;
		}
	}
571 572 573 574 575 576 577

	if (fh->fh_r_ctl == FC_RCTL_BA_ABTS) {
		/* Drop incoming ABTS */
		kfree_skb(skb);
		return;
	}

578 579 580 581
	stats = per_cpu_ptr(lport->stats, smp_processor_id());
	stats->RxFrames++;
	stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;

582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604
	if (le32_to_cpu(fr_crc(fp)) !=
			~crc32(~0, skb->data, fr_len)) {
		if (stats->InvalidCRCCount < 5)
			printk(KERN_WARNING PFX "dropping frame with "
			       "CRC error\n");
		stats->InvalidCRCCount++;
		kfree_skb(skb);
		return;
	}
	fc_exch_recv(lport, fp);
}

/**
 * bnx2fc_percpu_io_thread - thread per cpu for ios
 *
 * @arg:	ptr to bnx2fc_percpu_info structure
 */
int bnx2fc_percpu_io_thread(void *arg)
{
	struct bnx2fc_percpu_s *p = arg;
	struct bnx2fc_work *work, *tmp;
	LIST_HEAD(work_list);

605
	set_user_nice(current, MIN_NICE);
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
	set_current_state(TASK_INTERRUPTIBLE);
	while (!kthread_should_stop()) {
		schedule();
		spin_lock_bh(&p->fp_work_lock);
		while (!list_empty(&p->work_list)) {
			list_splice_init(&p->work_list, &work_list);
			spin_unlock_bh(&p->fp_work_lock);

			list_for_each_entry_safe(work, tmp, &work_list, list) {
				list_del_init(&work->list);
				bnx2fc_process_cq_compl(work->tgt, work->wqe);
				kfree(work);
			}

			spin_lock_bh(&p->fp_work_lock);
		}
622
		__set_current_state(TASK_INTERRUPTIBLE);
623 624
		spin_unlock_bh(&p->fp_work_lock);
	}
625
	__set_current_state(TASK_RUNNING);
626 627 628 629 630 631 632 633 634

	return 0;
}

static struct fc_host_statistics *bnx2fc_get_host_stats(struct Scsi_Host *shost)
{
	struct fc_host_statistics *bnx2fc_stats;
	struct fc_lport *lport = shost_priv(shost);
	struct fcoe_port *port = lport_priv(lport);
635 636
	struct bnx2fc_interface *interface = port->priv;
	struct bnx2fc_hba *hba = interface->hba;
637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
	struct fcoe_statistics_params *fw_stats;
	int rc = 0;

	fw_stats = (struct fcoe_statistics_params *)hba->stats_buffer;
	if (!fw_stats)
		return NULL;

	bnx2fc_stats = fc_get_host_stats(shost);

	init_completion(&hba->stat_req_done);
	if (bnx2fc_send_stat_req(hba))
		return bnx2fc_stats;
	rc = wait_for_completion_timeout(&hba->stat_req_done, (2 * HZ));
	if (!rc) {
		BNX2FC_HBA_DBG(lport, "FW stat req timed out\n");
		return bnx2fc_stats;
	}
654 655 656 657 658 659 660 661 662 663
	BNX2FC_STATS(hba, rx_stat2, fc_crc_cnt);
	bnx2fc_stats->invalid_crc_count += hba->bfw_stats.fc_crc_cnt;
	BNX2FC_STATS(hba, tx_stat, fcoe_tx_pkt_cnt);
	bnx2fc_stats->tx_frames += hba->bfw_stats.fcoe_tx_pkt_cnt;
	BNX2FC_STATS(hba, tx_stat, fcoe_tx_byte_cnt);
	bnx2fc_stats->tx_words += ((hba->bfw_stats.fcoe_tx_byte_cnt) / 4);
	BNX2FC_STATS(hba, rx_stat0, fcoe_rx_pkt_cnt);
	bnx2fc_stats->rx_frames += hba->bfw_stats.fcoe_rx_pkt_cnt;
	BNX2FC_STATS(hba, rx_stat0, fcoe_rx_byte_cnt);
	bnx2fc_stats->rx_words += ((hba->bfw_stats.fcoe_rx_byte_cnt) / 4);
664 665 666 667 668 669 670 671

	bnx2fc_stats->dumped_frames = 0;
	bnx2fc_stats->lip_count = 0;
	bnx2fc_stats->nos_count = 0;
	bnx2fc_stats->loss_of_sync_count = 0;
	bnx2fc_stats->loss_of_signal_count = 0;
	bnx2fc_stats->prim_seq_protocol_err_count = 0;

672 673
	memcpy(&hba->prev_stats, hba->stats_buffer,
	       sizeof(struct fcoe_statistics_params));
674 675 676 677 678 679
	return bnx2fc_stats;
}

static int bnx2fc_shost_config(struct fc_lport *lport, struct device *dev)
{
	struct fcoe_port *port = lport_priv(lport);
680
	struct bnx2fc_interface *interface = port->priv;
681
	struct bnx2fc_hba *hba = interface->hba;
682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701
	struct Scsi_Host *shost = lport->host;
	int rc = 0;

	shost->max_cmd_len = BNX2FC_MAX_CMD_LEN;
	shost->max_lun = BNX2FC_MAX_LUN;
	shost->max_id = BNX2FC_MAX_FCP_TGT;
	shost->max_channel = 0;
	if (lport->vport)
		shost->transportt = bnx2fc_vport_xport_template;
	else
		shost->transportt = bnx2fc_transport_template;

	/* Add the new host to SCSI-ml */
	rc = scsi_add_host(lport->host, dev);
	if (rc) {
		printk(KERN_ERR PFX "Error on scsi_add_host\n");
		return rc;
	}
	if (!lport->vport)
		fc_host_max_npiv_vports(lport->host) = USHRT_MAX;
702
	snprintf(fc_host_symbolic_name(lport->host), 256,
703
		 "%s (QLogic %s) v%s over %s",
704
		BNX2FC_NAME, hba->chip_num, BNX2FC_VERSION,
705
		interface->netdev->name);
706 707 708 709 710 711 712

	return 0;
}

static int bnx2fc_link_ok(struct fc_lport *lport)
{
	struct fcoe_port *port = lport_priv(lport);
713 714
	struct bnx2fc_interface *interface = port->priv;
	struct bnx2fc_hba *hba = interface->hba;
715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735
	struct net_device *dev = hba->phys_dev;
	int rc = 0;

	if ((dev->flags & IFF_UP) && netif_carrier_ok(dev))
		clear_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
	else {
		set_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
		rc = -1;
	}
	return rc;
}

/**
 * bnx2fc_get_link_state - get network link state
 *
 * @hba:	adapter instance pointer
 *
 * updates adapter structure flag based on netdev state
 */
void bnx2fc_get_link_state(struct bnx2fc_hba *hba)
{
736
	if (test_bit(__LINK_STATE_NOCARRIER, &hba->phys_dev->state))
737 738 739 740 741
		set_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
	else
		clear_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
}

742
static int bnx2fc_net_config(struct fc_lport *lport, struct net_device *netdev)
743 744
{
	struct bnx2fc_hba *hba;
745
	struct bnx2fc_interface *interface;
746
	struct fcoe_ctlr *ctlr;
747 748 749 750
	struct fcoe_port *port;
	u64 wwnn, wwpn;

	port = lport_priv(lport);
751
	interface = port->priv;
752
	ctlr = bnx2fc_to_ctlr(interface);
753
	hba = interface->hba;
754 755 756 757 758 759

	/* require support for get_pauseparam ethtool op. */
	if (!hba->phys_dev->ethtool_ops ||
	    !hba->phys_dev->ethtool_ops->get_pauseparam)
		return -EOPNOTSUPP;

760
	if (fc_set_mfs(lport, BNX2FC_MFS))
761 762 763 764 765 766
		return -EINVAL;

	skb_queue_head_init(&port->fcoe_pending_queue);
	port->fcoe_pending_queue_active = 0;
	setup_timer(&port->timer, fcoe_queue_timer, (unsigned long) lport);

767
	fcoe_link_speed_update(lport);
768 769

	if (!lport->vport) {
770
		if (fcoe_get_wwn(netdev, &wwnn, NETDEV_FCOE_WWNN))
771
			wwnn = fcoe_wwn_from_mac(ctlr->ctl_src_addr,
772
						 1, 0);
773 774 775
		BNX2FC_HBA_DBG(lport, "WWNN = 0x%llx\n", wwnn);
		fc_set_wwnn(lport, wwnn);

776
		if (fcoe_get_wwn(netdev, &wwpn, NETDEV_FCOE_WWPN))
777
			wwpn = fcoe_wwn_from_mac(ctlr->ctl_src_addr,
778 779
						 2, 0);

780 781 782 783 784 785 786 787 788 789 790
		BNX2FC_HBA_DBG(lport, "WWPN = 0x%llx\n", wwpn);
		fc_set_wwpn(lport, wwpn);
	}

	return 0;
}

static void bnx2fc_destroy_timer(unsigned long data)
{
	struct bnx2fc_hba *hba = (struct bnx2fc_hba *)data;

791 792
	printk(KERN_ERR PFX "ERROR:bnx2fc_destroy_timer - "
	       "Destroy compl not received!!\n");
793
	set_bit(BNX2FC_FLAG_DESTROY_CMPL, &hba->flags);
794 795 796 797 798 799 800 801
	wake_up_interruptible(&hba->destroy_wait);
}

/**
 * bnx2fc_indicate_netevent - Generic netdev event handler
 *
 * @context:	adapter structure pointer
 * @event:	event type
802
 * @vlan_id:	vlan id - associated vlan id with this event
803 804
 *
 * Handles NETDEV_UP, NETDEV_DOWN, NETDEV_GOING_DOWN,NETDEV_CHANGE and
805
 * NETDEV_CHANGE_MTU events. Handle NETDEV_UNREGISTER only for vlans.
806
 */
807 808
static void bnx2fc_indicate_netevent(void *context, unsigned long event,
				     u16 vlan_id)
809 810
{
	struct bnx2fc_hba *hba = (struct bnx2fc_hba *)context;
811
	struct fcoe_ctlr_device *cdev;
812
	struct fc_lport *lport;
813
	struct fc_lport *vport;
814
	struct bnx2fc_interface *interface, *tmp;
815
	struct fcoe_ctlr *ctlr;
816
	int wait_for_upload = 0;
817 818
	u32 link_possible = 1;

819
	if (vlan_id != 0 && event != NETDEV_UNREGISTER)
820 821
		return;

822 823 824 825
	switch (event) {
	case NETDEV_UP:
		if (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state))
			printk(KERN_ERR "indicate_netevent: "\
826
					"hba is not UP!!\n");
827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842
		break;

	case NETDEV_DOWN:
		clear_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state);
		clear_bit(ADAPTER_STATE_UP, &hba->adapter_state);
		link_possible = 0;
		break;

	case NETDEV_GOING_DOWN:
		set_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state);
		link_possible = 0;
		break;

	case NETDEV_CHANGE:
		break;

843 844 845 846 847
	case NETDEV_UNREGISTER:
		if (!vlan_id)
			return;
		mutex_lock(&bnx2fc_dev_lock);
		list_for_each_entry_safe(interface, tmp, &if_list, list) {
848 849 850
			if (interface->hba == hba &&
			    interface->vlan_id == (vlan_id & VLAN_VID_MASK))
				__bnx2fc_destroy(interface);
851 852
		}
		mutex_unlock(&bnx2fc_dev_lock);
853 854 855

		/* Ensure ALL destroy work has been completed before return */
		flush_workqueue(bnx2fc_wq);
856 857
		return;

858
	default:
859
		printk(KERN_ERR PFX "Unknown netevent %ld", event);
860 861 862
		return;
	}

863 864
	mutex_lock(&bnx2fc_dev_lock);
	list_for_each_entry(interface, &if_list, list) {
865

866 867 868
		if (interface->hba != hba)
			continue;

869 870
		ctlr = bnx2fc_to_ctlr(interface);
		lport = ctlr->lp;
871 872 873
		BNX2FC_HBA_DBG(lport, "netevent handler - event=%s %ld\n",
				interface->netdev->name, event);

874
		fcoe_link_speed_update(lport);
875

876
		cdev = fcoe_ctlr_to_ctlr_dev(ctlr);
877 878

		if (link_possible && !bnx2fc_link_ok(lport)) {
879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894
			switch (cdev->enabled) {
			case FCOE_CTLR_DISABLED:
				pr_info("Link up while interface is disabled.\n");
				break;
			case FCOE_CTLR_ENABLED:
			case FCOE_CTLR_UNUSED:
				/* Reset max recv frame size to default */
				fc_set_mfs(lport, BNX2FC_MFS);
				/*
				 * ctlr link up will only be handled during
				 * enable to avoid sending discovery
				 * solicitation on a stale vlan
				 */
				if (interface->enabled)
					fcoe_ctlr_link_up(ctlr);
			};
895
		} else if (fcoe_ctlr_link_down(ctlr)) {
896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914
			switch (cdev->enabled) {
			case FCOE_CTLR_DISABLED:
				pr_info("Link down while interface is disabled.\n");
				break;
			case FCOE_CTLR_ENABLED:
			case FCOE_CTLR_UNUSED:
				mutex_lock(&lport->lp_mutex);
				list_for_each_entry(vport, &lport->vports, list)
					fc_host_port_type(vport->host) =
					FC_PORTTYPE_UNKNOWN;
				mutex_unlock(&lport->lp_mutex);
				fc_host_port_type(lport->host) =
					FC_PORTTYPE_UNKNOWN;
				per_cpu_ptr(lport->stats,
					    get_cpu())->LinkFailureCount++;
				put_cpu();
				fcoe_clean_pending_queue(lport);
				wait_for_upload = 1;
			};
915 916 917
		}
	}
	mutex_unlock(&bnx2fc_dev_lock);
918

919 920 921 922 923 924 925 926 927 928
	if (wait_for_upload) {
		clear_bit(ADAPTER_STATE_READY, &hba->adapter_state);
		init_waitqueue_head(&hba->shutdown_wait);
		BNX2FC_MISC_DBG("indicate_netevent "
				"num_ofld_sess = %d\n",
				hba->num_ofld_sess);
		hba->wait_for_link_down = 1;
		wait_event_interruptible(hba->shutdown_wait,
					 (hba->num_ofld_sess == 0));
		BNX2FC_MISC_DBG("wakeup - num_ofld_sess = %d\n",
929
				hba->num_ofld_sess);
930
		hba->wait_for_link_down = 0;
931

932 933
		if (signal_pending(current))
			flush_signals(current);
934 935 936 937 938 939 940 941 942 943 944 945
	}
}

static int bnx2fc_libfc_config(struct fc_lport *lport)
{

	/* Set the function pointers set by bnx2fc driver */
	memcpy(&lport->tt, &bnx2fc_libfc_fcn_templ,
		sizeof(struct libfc_function_template));
	fc_elsct_init(lport);
	fc_exch_init(lport);
	fc_rport_init(lport);
946 947
	fc_disc_init(lport);
	fc_disc_config(lport, lport);
948 949 950
	return 0;
}

951
static int bnx2fc_em_config(struct fc_lport *lport, struct bnx2fc_hba *hba)
952
{
953
	int fcoe_min_xid, fcoe_max_xid;
954

955
	fcoe_min_xid = hba->max_xid + 1;
956
	if (nr_cpu_ids <= 2)
957
		fcoe_max_xid = hba->max_xid + FCOE_XIDS_PER_CPU_OFFSET;
958
	else
959 960 961
		fcoe_max_xid = hba->max_xid + FCOE_MAX_XID_OFFSET;
	if (!fc_exch_mgr_alloc(lport, FC_CLASS_3, fcoe_min_xid,
			       fcoe_max_xid, NULL)) {
962 963 964 965 966 967 968 969 970 971 972
		printk(KERN_ERR PFX "em_config:fc_exch_mgr_alloc failed\n");
		return -ENOMEM;
	}

	return 0;
}

static int bnx2fc_lport_config(struct fc_lport *lport)
{
	lport->link_up = 0;
	lport->qfull = 0;
973 974
	lport->max_retry_count = BNX2FC_MAX_RETRY_CNT;
	lport->max_rport_retry_count = BNX2FC_MAX_RPORT_RETRY_CNT;
975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008
	lport->e_d_tov = 2 * 1000;
	lport->r_a_tov = 10 * 1000;

	lport->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
				FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
	lport->does_npiv = 1;

	memset(&lport->rnid_gen, 0, sizeof(struct fc_els_rnid_gen));
	lport->rnid_gen.rnid_atype = BNX2FC_RNID_HBA;

	/* alloc stats structure */
	if (fc_lport_init_stats(lport))
		return -ENOMEM;

	/* Finish fc_lport configuration */
	fc_lport_config(lport);

	return 0;
}

/**
 * bnx2fc_fip_recv - handle a received FIP frame.
 *
 * @skb: the received skb
 * @dev: associated &net_device
 * @ptype: the &packet_type structure which was used to register this handler.
 * @orig_dev: original receive &net_device, in case @ dev is a bond.
 *
 * Returns: 0 for success
 */
static int bnx2fc_fip_recv(struct sk_buff *skb, struct net_device *dev,
			   struct packet_type *ptype,
			   struct net_device *orig_dev)
{
1009
	struct bnx2fc_interface *interface;
1010
	struct fcoe_ctlr *ctlr;
1011 1012
	interface = container_of(ptype, struct bnx2fc_interface,
				 fip_packet_type);
1013 1014
	ctlr = bnx2fc_to_ctlr(interface);
	fcoe_ctlr_recv(ctlr, skb);
1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064
	return 0;
}

/**
 * bnx2fc_update_src_mac - Update Ethernet MAC filters.
 *
 * @fip: FCoE controller.
 * @old: Unicast MAC address to delete if the MAC is non-zero.
 * @new: Unicast MAC address to add.
 *
 * Remove any previously-set unicast MAC filter.
 * Add secondary FCoE MAC address filter for our OUI.
 */
static void bnx2fc_update_src_mac(struct fc_lport *lport, u8 *addr)
{
	struct fcoe_port *port = lport_priv(lport);

	memcpy(port->data_src_addr, addr, ETH_ALEN);
}

/**
 * bnx2fc_get_src_mac - return the ethernet source address for an lport
 *
 * @lport: libfc port
 */
static u8 *bnx2fc_get_src_mac(struct fc_lport *lport)
{
	struct fcoe_port *port;

	port = (struct fcoe_port *)lport_priv(lport);
	return port->data_src_addr;
}

/**
 * bnx2fc_fip_send - send an Ethernet-encapsulated FIP frame.
 *
 * @fip: FCoE controller.
 * @skb: FIP Packet.
 */
static void bnx2fc_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
{
	skb->dev = bnx2fc_from_ctlr(fip)->netdev;
	dev_queue_xmit(skb);
}

static int bnx2fc_vport_create(struct fc_vport *vport, bool disabled)
{
	struct Scsi_Host *shost = vport_to_shost(vport);
	struct fc_lport *n_port = shost_priv(shost);
	struct fcoe_port *port = lport_priv(n_port);
1065 1066
	struct bnx2fc_interface *interface = port->priv;
	struct net_device *netdev = interface->netdev;
1067
	struct fc_lport *vn_port;
1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078
	int rc;
	char buf[32];

	rc = fcoe_validate_vport_create(vport);
	if (rc) {
		fcoe_wwn_to_str(vport->port_name, buf, sizeof(buf));
		printk(KERN_ERR PFX "Failed to create vport, "
		       "WWPN (0x%s) already exists\n",
		       buf);
		return rc;
	}
1079

1080
	if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &interface->hba->flags)) {
1081
		printk(KERN_ERR PFX "vn ports cannot be created on"
1082
			"this interface\n");
1083 1084
		return -EIO;
	}
1085
	rtnl_lock();
1086
	mutex_lock(&bnx2fc_dev_lock);
1087
	vn_port = bnx2fc_if_create(interface, &vport->dev, 1);
1088
	mutex_unlock(&bnx2fc_dev_lock);
1089
	rtnl_unlock();
1090

1091
	if (!vn_port) {
1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107
		printk(KERN_ERR PFX "bnx2fc_vport_create (%s) failed\n",
			netdev->name);
		return -EIO;
	}

	if (disabled) {
		fc_vport_set_state(vport, FC_VPORT_DISABLED);
	} else {
		vn_port->boot_time = jiffies;
		fc_lport_init(vn_port);
		fc_fabric_login(vn_port);
		fc_vport_setlink(vn_port);
	}
	return 0;
}

1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121
static void bnx2fc_free_vport(struct bnx2fc_hba *hba, struct fc_lport *lport)
{
	struct bnx2fc_lport *blport, *tmp;

	spin_lock_bh(&hba->hba_lock);
	list_for_each_entry_safe(blport, tmp, &hba->vports, list) {
		if (blport->lport == lport) {
			list_del(&blport->list);
			kfree(blport);
		}
	}
	spin_unlock_bh(&hba->hba_lock);
}

1122 1123 1124 1125 1126 1127
static int bnx2fc_vport_destroy(struct fc_vport *vport)
{
	struct Scsi_Host *shost = vport_to_shost(vport);
	struct fc_lport *n_port = shost_priv(shost);
	struct fc_lport *vn_port = vport->dd_data;
	struct fcoe_port *port = lport_priv(vn_port);
1128
	struct bnx2fc_interface *interface = port->priv;
1129 1130
	struct fc_lport *v_port;
	bool found = false;
1131 1132

	mutex_lock(&n_port->lp_mutex);
1133 1134 1135 1136 1137 1138 1139 1140 1141 1142
	list_for_each_entry(v_port, &n_port->vports, list)
		if (v_port->vport == vport) {
			found = true;
			break;
		}

	if (!found) {
		mutex_unlock(&n_port->lp_mutex);
		return -ENOENT;
	}
1143 1144
	list_del(&vn_port->list);
	mutex_unlock(&n_port->lp_mutex);
1145 1146 1147
	bnx2fc_free_vport(interface->hba, port->lport);
	bnx2fc_port_shutdown(port->lport);
	bnx2fc_interface_put(interface);
1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167
	queue_work(bnx2fc_wq, &port->destroy_work);
	return 0;
}

static int bnx2fc_vport_disable(struct fc_vport *vport, bool disable)
{
	struct fc_lport *lport = vport->dd_data;

	if (disable) {
		fc_vport_set_state(vport, FC_VPORT_DISABLED);
		fc_fabric_logoff(lport);
	} else {
		lport->boot_time = jiffies;
		fc_fabric_login(lport);
		fc_vport_setlink(lport);
	}
	return 0;
}


1168
static int bnx2fc_interface_setup(struct bnx2fc_interface *interface)
1169
{
1170 1171
	struct net_device *netdev = interface->netdev;
	struct net_device *physdev = interface->hba->phys_dev;
1172
	struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186
	struct netdev_hw_addr *ha;
	int sel_san_mac = 0;

	/* setup Source MAC Address */
	rcu_read_lock();
	for_each_dev_addr(physdev, ha) {
		BNX2FC_MISC_DBG("net_config: ha->type = %d, fip_mac = ",
				ha->type);
		printk(KERN_INFO "%2x:%2x:%2x:%2x:%2x:%2x\n", ha->addr[0],
				ha->addr[1], ha->addr[2], ha->addr[3],
				ha->addr[4], ha->addr[5]);

		if ((ha->type == NETDEV_HW_ADDR_T_SAN) &&
		    (is_valid_ether_addr(ha->addr))) {
1187
			memcpy(ctlr->ctl_src_addr, ha->addr,
1188
			       ETH_ALEN);
1189 1190 1191 1192 1193 1194 1195 1196 1197
			sel_san_mac = 1;
			BNX2FC_MISC_DBG("Found SAN MAC\n");
		}
	}
	rcu_read_unlock();

	if (!sel_san_mac)
		return -ENODEV;

1198 1199 1200 1201
	interface->fip_packet_type.func = bnx2fc_fip_recv;
	interface->fip_packet_type.type = htons(ETH_P_FIP);
	interface->fip_packet_type.dev = netdev;
	dev_add_pack(&interface->fip_packet_type);
1202

1203 1204 1205 1206
	interface->fcoe_packet_type.func = bnx2fc_rcv;
	interface->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
	interface->fcoe_packet_type.dev = netdev;
	dev_add_pack(&interface->fcoe_packet_type);
1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241

	return 0;
}

static int bnx2fc_attach_transport(void)
{
	bnx2fc_transport_template =
		fc_attach_transport(&bnx2fc_transport_function);

	if (bnx2fc_transport_template == NULL) {
		printk(KERN_ERR PFX "Failed to attach FC transport\n");
		return -ENODEV;
	}

	bnx2fc_vport_xport_template =
		fc_attach_transport(&bnx2fc_vport_xport_function);
	if (bnx2fc_vport_xport_template == NULL) {
		printk(KERN_ERR PFX
		       "Failed to attach FC transport for vport\n");
		fc_release_transport(bnx2fc_transport_template);
		bnx2fc_transport_template = NULL;
		return -ENODEV;
	}
	return 0;
}
static void bnx2fc_release_transport(void)
{
	fc_release_transport(bnx2fc_transport_template);
	fc_release_transport(bnx2fc_vport_xport_template);
	bnx2fc_transport_template = NULL;
	bnx2fc_vport_xport_template = NULL;
}

static void bnx2fc_interface_release(struct kref *kref)
{
1242
	struct fcoe_ctlr_device *ctlr_dev;
1243
	struct bnx2fc_interface *interface;
1244
	struct fcoe_ctlr *ctlr;
1245 1246
	struct net_device *netdev;

1247
	interface = container_of(kref, struct bnx2fc_interface, kref);
1248
	BNX2FC_MISC_DBG("Interface is being released\n");
1249

1250
	ctlr = bnx2fc_to_ctlr(interface);
1251
	ctlr_dev = fcoe_ctlr_to_ctlr_dev(ctlr);
1252
	netdev = interface->netdev;
1253 1254

	/* tear-down FIP controller */
1255
	if (test_and_clear_bit(BNX2FC_CTLR_INIT_DONE, &interface->if_flags))
1256
		fcoe_ctlr_destroy(ctlr);
1257

1258
	fcoe_ctlr_device_delete(ctlr_dev);
1259 1260 1261 1262 1263

	dev_put(netdev);
	module_put(THIS_MODULE);
}

1264
static inline void bnx2fc_interface_get(struct bnx2fc_interface *interface)
1265
{
1266
	kref_get(&interface->kref);
1267 1268
}

1269
static inline void bnx2fc_interface_put(struct bnx2fc_interface *interface)
1270
{
1271
	kref_put(&interface->kref, bnx2fc_interface_release);
1272
}
1273
static void bnx2fc_hba_destroy(struct bnx2fc_hba *hba)
1274
{
1275 1276 1277 1278 1279 1280
	/* Free the command manager */
	if (hba->cmd_mgr) {
		bnx2fc_cmd_mgr_free(hba->cmd_mgr);
		hba->cmd_mgr = NULL;
	}
	kfree(hba->tgt_ofld_list);
1281 1282 1283 1284 1285
	bnx2fc_unbind_pcidev(hba);
	kfree(hba);
}

/**
1286
 * bnx2fc_hba_create - create a new bnx2fc hba
1287 1288 1289
 *
 * @cnic:	pointer to cnic device
 *
1290 1291
 * Creates a new FCoE hba on the given device.
 *
1292
 */
1293
static struct bnx2fc_hba *bnx2fc_hba_create(struct cnic_dev *cnic)
1294 1295
{
	struct bnx2fc_hba *hba;
1296
	struct fcoe_capabilities *fcoe_cap;
1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307
	int rc;

	hba = kzalloc(sizeof(*hba), GFP_KERNEL);
	if (!hba) {
		printk(KERN_ERR PFX "Unable to allocate hba structure\n");
		return NULL;
	}
	spin_lock_init(&hba->hba_lock);
	mutex_init(&hba->hba_mutex);

	hba->cnic = cnic;
1308 1309 1310 1311 1312 1313

	hba->max_tasks = cnic->max_fcoe_exchanges;
	hba->elstm_xids = (hba->max_tasks / 2);
	hba->max_outstanding_cmds = hba->elstm_xids;
	hba->max_xid = (hba->max_tasks - 1);

1314
	rc = bnx2fc_bind_pcidev(hba);
1315 1316
	if (rc) {
		printk(KERN_ERR PFX "create_adapter:  bind error\n");
1317
		goto bind_err;
1318
	}
1319
	hba->phys_dev = cnic->netdev;
1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331
	hba->next_conn_id = 0;

	hba->tgt_ofld_list =
		kzalloc(sizeof(struct bnx2fc_rport *) * BNX2FC_NUM_MAX_SESS,
			GFP_KERNEL);
	if (!hba->tgt_ofld_list) {
		printk(KERN_ERR PFX "Unable to allocate tgt offload list\n");
		goto tgtofld_err;
	}

	hba->num_ofld_sess = 0;

1332
	hba->cmd_mgr = bnx2fc_cmd_mgr_alloc(hba);
1333 1334 1335 1336
	if (!hba->cmd_mgr) {
		printk(KERN_ERR PFX "em_config:bnx2fc_cmd_mgr_alloc failed\n");
		goto cmgr_err;
	}
1337 1338 1339 1340 1341 1342
	fcoe_cap = &hba->fcoe_cap;

	fcoe_cap->capability1 = BNX2FC_TM_MAX_SQES <<
					FCOE_IOS_PER_CONNECTION_SHIFT;
	fcoe_cap->capability1 |= BNX2FC_NUM_MAX_SESS <<
					FCOE_LOGINS_PER_PORT_SHIFT;
1343
	fcoe_cap->capability2 = hba->max_outstanding_cmds <<
1344 1345 1346 1347 1348
					FCOE_NUMBER_OF_EXCHANGES_SHIFT;
	fcoe_cap->capability2 |= BNX2FC_MAX_NPIV <<
					FCOE_NPIV_WWN_PER_PORT_SHIFT;
	fcoe_cap->capability3 = BNX2FC_NUM_MAX_SESS <<
					FCOE_TARGETS_SUPPORTED_SHIFT;
1349
	fcoe_cap->capability3 |= hba->max_outstanding_cmds <<
1350 1351
					FCOE_OUTSTANDING_COMMANDS_SHIFT;
	fcoe_cap->capability4 = FCOE_CAPABILITY4_STATEFUL;
1352 1353 1354

	init_waitqueue_head(&hba->shutdown_wait);
	init_waitqueue_head(&hba->destroy_wait);
1355
	INIT_LIST_HEAD(&hba->vports);
1356 1357

	return hba;
1358 1359 1360 1361 1362

cmgr_err:
	kfree(hba->tgt_ofld_list);
tgtofld_err:
	bnx2fc_unbind_pcidev(hba);
1363 1364 1365 1366 1367
bind_err:
	kfree(hba);
	return NULL;
}

1368 1369 1370
struct bnx2fc_interface *bnx2fc_interface_create(struct bnx2fc_hba *hba,
				      struct net_device *netdev,
				      enum fip_state fip_mode)
1371
{
1372
	struct fcoe_ctlr_device *ctlr_dev;
1373
	struct bnx2fc_interface *interface;
1374 1375
	struct fcoe_ctlr *ctlr;
	int size;
1376 1377
	int rc = 0;

1378
	size = (sizeof(*interface) + sizeof(struct fcoe_ctlr));
1379 1380 1381
	ctlr_dev = fcoe_ctlr_device_add(&netdev->dev, &bnx2fc_fcoe_sysfs_templ,
					 size);
	if (!ctlr_dev) {
1382 1383 1384
		printk(KERN_ERR PFX "Unable to allocate interface structure\n");
		return NULL;
	}
1385
	ctlr = fcoe_ctlr_device_priv(ctlr_dev);
1386
	ctlr->cdev = ctlr_dev;
1387
	interface = fcoe_ctlr_priv(ctlr);
1388
	dev_hold(netdev);
1389 1390 1391
	kref_init(&interface->kref);
	interface->hba = hba;
	interface->netdev = netdev;
1392 1393

	/* Initialize FIP */
1394 1395 1396 1397
	fcoe_ctlr_init(ctlr, fip_mode);
	ctlr->send = bnx2fc_fip_send;
	ctlr->update_mac = bnx2fc_update_src_mac;
	ctlr->get_src_addr = bnx2fc_get_src_mac;
1398
	set_bit(BNX2FC_CTLR_INIT_DONE, &interface->if_flags);
1399

1400
	rc = bnx2fc_interface_setup(interface);
1401 1402
	if (!rc)
		return interface;
1403

1404
	fcoe_ctlr_destroy(ctlr);
1405
	dev_put(netdev);
1406
	fcoe_ctlr_device_delete(ctlr_dev);
1407
	return NULL;
1408 1409 1410 1411 1412
}

/**
 * bnx2fc_if_create - Create FCoE instance on a given interface
 *
1413
 * @interface:	FCoE interface to create a local port on
1414 1415 1416 1417 1418 1419 1420
 * @parent:	Device pointer to be the parent in sysfs for the SCSI host
 * @npiv:	Indicates if the port is vport or not
 *
 * Creates a fc_lport instance and a Scsi_Host instance and configure them.
 *
 * Returns:	Allocated fc_lport or an error pointer
 */
1421
static struct fc_lport *bnx2fc_if_create(struct bnx2fc_interface *interface,
1422 1423
				  struct device *parent, int npiv)
{
1424
	struct fcoe_ctlr        *ctlr = bnx2fc_to_ctlr(interface);
V
Vasu Dev 已提交
1425
	struct fc_lport		*lport, *n_port;
1426 1427 1428
	struct fcoe_port	*port;
	struct Scsi_Host	*shost;
	struct fc_vport		*vport = dev_to_vport(parent);
1429
	struct bnx2fc_lport	*blport;
1430
	struct bnx2fc_hba	*hba = interface->hba;
1431 1432
	int			rc = 0;

1433 1434
	blport = kzalloc(sizeof(struct bnx2fc_lport), GFP_KERNEL);
	if (!blport) {
1435
		BNX2FC_HBA_DBG(ctlr->lp, "Unable to alloc blport\n");
1436 1437 1438
		return NULL;
	}

1439
	/* Allocate Scsi_Host structure */
1440
	bnx2fc_shost_template.can_queue = hba->max_outstanding_cmds;
V
Vasu Dev 已提交
1441 1442 1443 1444
	if (!npiv)
		lport = libfc_host_alloc(&bnx2fc_shost_template, sizeof(*port));
	else
		lport = libfc_vport_create(vport, sizeof(*port));
1445 1446 1447

	if (!lport) {
		printk(KERN_ERR PFX "could not allocate scsi host structure\n");
1448
		goto free_blport;
1449 1450 1451 1452
	}
	shost = lport->host;
	port = lport_priv(lport);
	port->lport = lport;
1453
	port->priv = interface;
1454
	port->get_netdev = bnx2fc_netdev;
1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468
	INIT_WORK(&port->destroy_work, bnx2fc_destroy_work);

	/* Configure fcoe_port */
	rc = bnx2fc_lport_config(lport);
	if (rc)
		goto lp_config_err;

	if (npiv) {
		printk(KERN_ERR PFX "Setting vport names, 0x%llX 0x%llX\n",
			vport->node_name, vport->port_name);
		fc_set_wwnn(lport, vport->node_name);
		fc_set_wwpn(lport, vport->port_name);
	}
	/* Configure netdev and networking properties of the lport */
1469
	rc = bnx2fc_net_config(lport, interface->netdev);
1470 1471 1472 1473 1474 1475 1476 1477
	if (rc) {
		printk(KERN_ERR PFX "Error on bnx2fc_net_config\n");
		goto lp_config_err;
	}

	rc = bnx2fc_shost_config(lport, parent);
	if (rc) {
		printk(KERN_ERR PFX "Couldnt configure shost for %s\n",
1478
			interface->netdev->name);
1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490
		goto lp_config_err;
	}

	/* Initialize the libfc library */
	rc = bnx2fc_libfc_config(lport);
	if (rc) {
		printk(KERN_ERR PFX "Couldnt configure libfc\n");
		goto shost_err;
	}
	fc_host_port_type(lport->host) = FC_PORTTYPE_UNKNOWN;

	/* Allocate exchange manager */
V
Vasu Dev 已提交
1491
	if (!npiv)
1492
		rc = bnx2fc_em_config(lport, hba);
V
Vasu Dev 已提交
1493 1494 1495 1496 1497 1498 1499 1500 1501
	else {
		shost = vport_to_shost(vport);
		n_port = shost_priv(shost);
		rc = fc_exch_mgr_list_clone(n_port, lport);
	}

	if (rc) {
		printk(KERN_ERR PFX "Error on bnx2fc_em_config\n");
		goto shost_err;
1502 1503
	}

1504
	bnx2fc_interface_get(interface);
1505 1506 1507 1508 1509 1510

	spin_lock_bh(&hba->hba_lock);
	blport->lport = lport;
	list_add_tail(&blport->list, &hba->vports);
	spin_unlock_bh(&hba->hba_lock);

1511 1512 1513 1514 1515 1516
	return lport;

shost_err:
	scsi_remove_host(shost);
lp_config_err:
	scsi_host_put(lport->host);
1517 1518
free_blport:
	kfree(blport);
1519 1520 1521
	return NULL;
}

1522
static void bnx2fc_net_cleanup(struct bnx2fc_interface *interface)
1523 1524
{
	/* Dont listen for Ethernet packets anymore */
1525 1526
	__dev_remove_pack(&interface->fcoe_packet_type);
	__dev_remove_pack(&interface->fip_packet_type);
1527 1528 1529
	synchronize_net();
}

1530
static void bnx2fc_interface_cleanup(struct bnx2fc_interface *interface)
1531
{
1532 1533
	struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
	struct fc_lport *lport = ctlr->lp;
1534
	struct fcoe_port *port = lport_priv(lport);
1535
	struct bnx2fc_hba *hba = interface->hba;
1536 1537 1538 1539 1540 1541 1542

	/* Stop the transmit retry timer */
	del_timer_sync(&port->timer);

	/* Free existing transmit skbs */
	fcoe_clean_pending_queue(lport);

1543
	bnx2fc_net_cleanup(interface);
1544

1545
	bnx2fc_free_vport(hba, lport);
1546 1547 1548 1549 1550
}

static void bnx2fc_if_destroy(struct fc_lport *lport)
{

1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570
	/* Free queued packets for the receive thread */
	bnx2fc_clean_rx_queue(lport);

	/* Detach from scsi-ml */
	fc_remove_host(lport->host);
	scsi_remove_host(lport->host);

	/*
	 * Note that only the physical lport will have the exchange manager.
	 * for vports, this function is NOP
	 */
	fc_exch_mgr_free(lport);

	/* Free memory used by statistical counters */
	fc_lport_free_stats(lport);

	/* Release Scsi_Host */
	scsi_host_put(lport->host);
}

1571
static void __bnx2fc_destroy(struct bnx2fc_interface *interface)
1572
{
1573 1574
	struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
	struct fc_lport *lport = ctlr->lp;
1575
	struct fcoe_port *port = lport_priv(lport);
1576 1577 1578 1579 1580

	bnx2fc_interface_cleanup(interface);
	bnx2fc_stop(interface);
	list_del(&interface->list);
	bnx2fc_interface_put(interface);
1581
	queue_work(bnx2fc_wq, &port->destroy_work);
1582 1583
}

1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595
/**
 * bnx2fc_destroy - Destroy a bnx2fc FCoE interface
 *
 * @buffer: The name of the Ethernet interface to be destroyed
 * @kp:     The associated kernel parameter
 *
 * Called from sysfs.
 *
 * Returns: 0 for success
 */
static int bnx2fc_destroy(struct net_device *netdev)
{
1596
	struct bnx2fc_interface *interface = NULL;
1597
	struct workqueue_struct *timer_work_queue;
1598
	struct fcoe_ctlr *ctlr;
1599 1600
	int rc = 0;

1601
	rtnl_lock();
1602 1603
	mutex_lock(&bnx2fc_dev_lock);

1604
	interface = bnx2fc_interface_lookup(netdev);
1605 1606
	ctlr = bnx2fc_to_ctlr(interface);
	if (!interface || !ctlr->lp) {
1607
		rc = -ENODEV;
1608
		printk(KERN_ERR PFX "bnx2fc_destroy: interface or lport not found\n");
1609 1610 1611
		goto netdev_err;
	}

1612
	timer_work_queue = interface->timer_work_queue;
1613
	__bnx2fc_destroy(interface);
1614
	destroy_workqueue(timer_work_queue);
1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631

netdev_err:
	mutex_unlock(&bnx2fc_dev_lock);
	rtnl_unlock();
	return rc;
}

static void bnx2fc_destroy_work(struct work_struct *work)
{
	struct fcoe_port *port;
	struct fc_lport *lport;

	port = container_of(work, struct fcoe_port, destroy_work);
	lport = port->lport;

	BNX2FC_HBA_DBG(lport, "Entered bnx2fc_destroy_work\n");

1632
	bnx2fc_if_destroy(lport);
1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663
}

static void bnx2fc_unbind_adapter_devices(struct bnx2fc_hba *hba)
{
	bnx2fc_free_fw_resc(hba);
	bnx2fc_free_task_ctx(hba);
}

/**
 * bnx2fc_bind_adapter_devices - binds bnx2fc adapter with the associated
 *			pci structure
 *
 * @hba:		Adapter instance
 */
static int bnx2fc_bind_adapter_devices(struct bnx2fc_hba *hba)
{
	if (bnx2fc_setup_task_ctx(hba))
		goto mem_err;

	if (bnx2fc_setup_fw_resc(hba))
		goto mem_err;

	return 0;
mem_err:
	bnx2fc_unbind_adapter_devices(hba);
	return -ENOMEM;
}

static int bnx2fc_bind_pcidev(struct bnx2fc_hba *hba)
{
	struct cnic_dev *cnic;
1664
	struct pci_dev *pdev;
1665 1666 1667 1668 1669 1670

	if (!hba->cnic) {
		printk(KERN_ERR PFX "cnic is NULL\n");
		return -ENODEV;
	}
	cnic = hba->cnic;
1671 1672 1673
	pdev = hba->pcidev = cnic->pcidev;
	if (!hba->pcidev)
		return -ENODEV;
1674

1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708
	switch (pdev->device) {
	case PCI_DEVICE_ID_NX2_57710:
		strncpy(hba->chip_num, "BCM57710", BCM_CHIP_LEN);
		break;
	case PCI_DEVICE_ID_NX2_57711:
		strncpy(hba->chip_num, "BCM57711", BCM_CHIP_LEN);
		break;
	case PCI_DEVICE_ID_NX2_57712:
	case PCI_DEVICE_ID_NX2_57712_MF:
	case PCI_DEVICE_ID_NX2_57712_VF:
		strncpy(hba->chip_num, "BCM57712", BCM_CHIP_LEN);
		break;
	case PCI_DEVICE_ID_NX2_57800:
	case PCI_DEVICE_ID_NX2_57800_MF:
	case PCI_DEVICE_ID_NX2_57800_VF:
		strncpy(hba->chip_num, "BCM57800", BCM_CHIP_LEN);
		break;
	case PCI_DEVICE_ID_NX2_57810:
	case PCI_DEVICE_ID_NX2_57810_MF:
	case PCI_DEVICE_ID_NX2_57810_VF:
		strncpy(hba->chip_num, "BCM57810", BCM_CHIP_LEN);
		break;
	case PCI_DEVICE_ID_NX2_57840:
	case PCI_DEVICE_ID_NX2_57840_MF:
	case PCI_DEVICE_ID_NX2_57840_VF:
	case PCI_DEVICE_ID_NX2_57840_2_20:
	case PCI_DEVICE_ID_NX2_57840_4_10:
		strncpy(hba->chip_num, "BCM57840", BCM_CHIP_LEN);
		break;
	default:
		pr_err(PFX "Unknown device id 0x%x\n", pdev->device);
		break;
	}
	pci_dev_get(hba->pcidev);
1709 1710 1711 1712 1713
	return 0;
}

static void bnx2fc_unbind_pcidev(struct bnx2fc_hba *hba)
{
1714 1715
	if (hba->pcidev) {
		hba->chip_num[0] = '\0';
1716
		pci_dev_put(hba->pcidev);
1717
	}
1718 1719 1720
	hba->pcidev = NULL;
}

1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746
/**
 * bnx2fc_ulp_get_stats - cnic callback to populate FCoE stats
 *
 * @handle:    transport handle pointing to adapter struture
 */
static int bnx2fc_ulp_get_stats(void *handle)
{
	struct bnx2fc_hba *hba = handle;
	struct cnic_dev *cnic;
	struct fcoe_stats_info *stats_addr;

	if (!hba)
		return -EINVAL;

	cnic = hba->cnic;
	stats_addr = &cnic->stats_addr->fcoe_stat;
	if (!stats_addr)
		return -EINVAL;

	strncpy(stats_addr->version, BNX2FC_VERSION,
		sizeof(stats_addr->version));
	stats_addr->txq_size = BNX2FC_SQ_WQES_MAX;
	stats_addr->rxq_size = BNX2FC_CQ_WQES_MAX;

	return 0;
}
1747 1748 1749 1750 1751


/**
 * bnx2fc_ulp_start - cnic callback to initialize & start adapter instance
 *
1752
 * @handle:	transport handle pointing to adapter structure
1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764
 *
 * This function maps adapter structure to pcidev structure and initiates
 *	firmware handshake to enable/initialize on-chip FCoE components.
 *	This bnx2fc - cnic interface api callback is used after following
 *	conditions are met -
 *	a) underlying network interface is up (marked by event NETDEV_UP
 *		from netdev
 *	b) bnx2fc adatper structure is registered.
 */
static void bnx2fc_ulp_start(void *handle)
{
	struct bnx2fc_hba *hba = handle;
1765
	struct bnx2fc_interface *interface;
1766
	struct fcoe_ctlr *ctlr;
1767
	struct fc_lport *lport;
1768 1769 1770

	mutex_lock(&bnx2fc_dev_lock);

1771
	if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags))
1772 1773 1774 1775
		bnx2fc_fw_init(hba);

	BNX2FC_MISC_DBG("bnx2fc started.\n");

1776 1777
	list_for_each_entry(interface, &if_list, list) {
		if (interface->hba == hba) {
1778 1779
			ctlr = bnx2fc_to_ctlr(interface);
			lport = ctlr->lp;
1780 1781 1782 1783 1784
			/* Kick off Fabric discovery*/
			printk(KERN_ERR PFX "ulp_init: start discovery\n");
			lport->tt.frame_send = bnx2fc_xmit;
			bnx2fc_start_disc(interface);
		}
1785
	}
1786 1787

	mutex_unlock(&bnx2fc_dev_lock);
1788 1789 1790 1791 1792 1793 1794 1795 1796
}

static void bnx2fc_port_shutdown(struct fc_lport *lport)
{
	BNX2FC_MISC_DBG("Entered %s\n", __func__);
	fc_fabric_logoff(lport);
	fc_lport_destroy(lport);
}

1797
static void bnx2fc_stop(struct bnx2fc_interface *interface)
1798
{
1799
	struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
1800 1801 1802
	struct fc_lport *lport;
	struct fc_lport *vport;

1803 1804
	if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &interface->hba->flags))
		return;
1805

1806
	lport = ctlr->lp;
1807 1808 1809 1810 1811 1812 1813 1814
	bnx2fc_port_shutdown(lport);

	mutex_lock(&lport->lp_mutex);
	list_for_each_entry(vport, &lport->vports, list)
		fc_host_port_type(vport->host) =
					FC_PORTTYPE_UNKNOWN;
	mutex_unlock(&lport->lp_mutex);
	fc_host_port_type(lport->host) = FC_PORTTYPE_UNKNOWN;
1815
	fcoe_ctlr_link_down(ctlr);
1816
	fcoe_clean_pending_queue(lport);
1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854
}

static int bnx2fc_fw_init(struct bnx2fc_hba *hba)
{
#define BNX2FC_INIT_POLL_TIME		(1000 / HZ)
	int rc = -1;
	int i = HZ;

	rc = bnx2fc_bind_adapter_devices(hba);
	if (rc) {
		printk(KERN_ALERT PFX
			"bnx2fc_bind_adapter_devices failed - rc = %d\n", rc);
		goto err_out;
	}

	rc = bnx2fc_send_fw_fcoe_init_msg(hba);
	if (rc) {
		printk(KERN_ALERT PFX
			"bnx2fc_send_fw_fcoe_init_msg failed - rc = %d\n", rc);
		goto err_unbind;
	}

	/*
	 * Wait until the adapter init message is complete, and adapter
	 * state is UP.
	 */
	while (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state) && i--)
		msleep(BNX2FC_INIT_POLL_TIME);

	if (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state)) {
		printk(KERN_ERR PFX "bnx2fc_start: %s failed to initialize.  "
				"Ignoring...\n",
				hba->cnic->netdev->name);
		rc = -1;
		goto err_unbind;
	}


1855
	set_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags);
1856 1857 1858 1859 1860 1861 1862 1863 1864 1865
	return 0;

err_unbind:
	bnx2fc_unbind_adapter_devices(hba);
err_out:
	return rc;
}

static void bnx2fc_fw_destroy(struct bnx2fc_hba *hba)
{
1866
	if (test_and_clear_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags)) {
1867 1868 1869 1870 1871 1872 1873 1874
		if (bnx2fc_send_fw_fcoe_destroy_msg(hba) == 0) {
			init_timer(&hba->destroy_timer);
			hba->destroy_timer.expires = BNX2FC_FW_TIMEOUT +
								jiffies;
			hba->destroy_timer.function = bnx2fc_destroy_timer;
			hba->destroy_timer.data = (unsigned long)hba;
			add_timer(&hba->destroy_timer);
			wait_event_interruptible(hba->destroy_wait,
1875 1876
					test_bit(BNX2FC_FLAG_DESTROY_CMPL,
						 &hba->flags));
1877
			clear_bit(BNX2FC_FLAG_DESTROY_CMPL, &hba->flags);
1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897
			/* This should never happen */
			if (signal_pending(current))
				flush_signals(current);

			del_timer_sync(&hba->destroy_timer);
		}
		bnx2fc_unbind_adapter_devices(hba);
	}
}

/**
 * bnx2fc_ulp_stop - cnic callback to shutdown adapter instance
 *
 * @handle:	transport handle pointing to adapter structure
 *
 * Driver checks if adapter is already in shutdown mode, if not start
 *	the shutdown process.
 */
static void bnx2fc_ulp_stop(void *handle)
{
1898 1899
	struct bnx2fc_hba *hba = handle;
	struct bnx2fc_interface *interface;
1900 1901 1902 1903

	printk(KERN_ERR "ULP_STOP\n");

	mutex_lock(&bnx2fc_dev_lock);
1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919
	if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags))
		goto exit;
	list_for_each_entry(interface, &if_list, list) {
		if (interface->hba == hba)
			bnx2fc_stop(interface);
	}
	BUG_ON(hba->num_ofld_sess != 0);

	mutex_lock(&hba->hba_mutex);
	clear_bit(ADAPTER_STATE_UP, &hba->adapter_state);
	clear_bit(ADAPTER_STATE_GOING_DOWN,
		  &hba->adapter_state);

	clear_bit(ADAPTER_STATE_READY, &hba->adapter_state);
	mutex_unlock(&hba->hba_mutex);

1920
	bnx2fc_fw_destroy(hba);
1921
exit:
1922 1923 1924
	mutex_unlock(&bnx2fc_dev_lock);
}

1925
static void bnx2fc_start_disc(struct bnx2fc_interface *interface)
1926
{
1927
	struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
1928 1929 1930 1931 1932
	struct fc_lport *lport;
	int wait_cnt = 0;

	BNX2FC_MISC_DBG("Entered %s\n", __func__);
	/* Kick off FIP/FLOGI */
1933
	if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &interface->hba->flags)) {
1934 1935 1936 1937
		printk(KERN_ERR PFX "Init not done yet\n");
		return;
	}

1938
	lport = ctlr->lp;
1939 1940
	BNX2FC_HBA_DBG(lport, "calling fc_fabric_login\n");

1941
	if (!bnx2fc_link_ok(lport) && interface->enabled) {
1942
		BNX2FC_HBA_DBG(lport, "ctlr_link_up\n");
1943
		fcoe_ctlr_link_up(ctlr);
1944
		fc_host_port_type(lport->host) = FC_PORTTYPE_NPORT;
1945
		set_bit(ADAPTER_STATE_READY, &interface->hba->adapter_state);
1946 1947 1948
	}

	/* wait for the FCF to be selected before issuing FLOGI */
1949
	while (!ctlr->sel_fcf) {
1950 1951 1952 1953 1954
		msleep(250);
		/* give up after 3 secs */
		if (++wait_cnt > 12)
			break;
	}
1955 1956 1957 1958 1959

	/* Reset max receive frame size to default */
	if (fc_set_mfs(lport, BNX2FC_MFS))
		return;

1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979
	fc_lport_init(lport);
	fc_fabric_login(lport);
}


/**
 * bnx2fc_ulp_init - Initialize an adapter instance
 *
 * @dev :	cnic device handle
 * Called from cnic_register_driver() context to initialize all
 *	enumerated cnic devices. This routine allocates adapter structure
 *	and other device specific resources.
 */
static void bnx2fc_ulp_init(struct cnic_dev *dev)
{
	struct bnx2fc_hba *hba;
	int rc = 0;

	BNX2FC_MISC_DBG("Entered %s\n", __func__);
	/* bnx2fc works only when bnx2x is loaded */
1980 1981
	if (!test_bit(CNIC_F_BNX2X_CLASS, &dev->flags) ||
	    (dev->max_fcoe_conn == 0)) {
1982
		printk(KERN_ERR PFX "bnx2fc FCoE not supported on %s,"
1983 1984
				    " flags: %lx fcoe_conn: %d\n",
			dev->netdev->name, dev->flags, dev->max_fcoe_conn);
1985 1986 1987
		return;
	}

1988
	hba = bnx2fc_hba_create(dev);
1989 1990 1991 1992 1993 1994 1995
	if (!hba) {
		printk(KERN_ERR PFX "hba initialization failed\n");
		return;
	}

	/* Add HBA to the adapter list */
	mutex_lock(&bnx2fc_dev_lock);
1996
	list_add_tail(&hba->list, &adapter_list);
1997 1998 1999
	adapter_count++;
	mutex_unlock(&bnx2fc_dev_lock);

2000
	dev->fcoe_cap = &hba->fcoe_cap;
2001 2002 2003 2004
	clear_bit(BNX2FC_CNIC_REGISTERED, &hba->reg_with_cnic);
	rc = dev->register_device(dev, CNIC_ULP_FCOE,
						(void *) hba);
	if (rc)
2005
		printk(KERN_ERR PFX "register_device failed, rc = %d\n", rc);
2006 2007 2008 2009
	else
		set_bit(BNX2FC_CNIC_REGISTERED, &hba->reg_with_cnic);
}

2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027
/* Assumes rtnl_lock and the bnx2fc_dev_lock are already taken */
static int __bnx2fc_disable(struct fcoe_ctlr *ctlr)
{
	struct bnx2fc_interface *interface = fcoe_ctlr_priv(ctlr);

	if (interface->enabled == true) {
		if (!ctlr->lp) {
			pr_err(PFX "__bnx2fc_disable: lport not found\n");
			return -ENODEV;
		} else {
			interface->enabled = false;
			fcoe_ctlr_link_down(ctlr);
			fcoe_clean_pending_queue(ctlr->lp);
		}
	}
	return 0;
}

2028 2029 2030
/**
 * Deperecated: Use bnx2fc_enabled()
 */
2031 2032
static int bnx2fc_disable(struct net_device *netdev)
{
2033
	struct bnx2fc_interface *interface;
2034
	struct fcoe_ctlr *ctlr;
2035 2036
	int rc = 0;

2037
	rtnl_lock();
2038 2039
	mutex_lock(&bnx2fc_dev_lock);

2040
	interface = bnx2fc_interface_lookup(netdev);
2041
	ctlr = bnx2fc_to_ctlr(interface);
2042 2043

	if (!interface) {
2044
		rc = -ENODEV;
2045
		pr_err(PFX "bnx2fc_disable: interface not found\n");
2046
	} else {
2047
		rc = __bnx2fc_disable(ctlr);
2048 2049 2050 2051 2052 2053
	}
	mutex_unlock(&bnx2fc_dev_lock);
	rtnl_unlock();
	return rc;
}

2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090
static uint bnx2fc_npiv_create_vports(struct fc_lport *lport,
				      struct cnic_fc_npiv_tbl *npiv_tbl)
{
	struct fc_vport_identifiers vpid;
	uint i, created = 0;

	if (npiv_tbl->count > MAX_NPIV_ENTRIES) {
		BNX2FC_HBA_DBG(lport, "Exceeded count max of npiv table\n");
		goto done;
	}

	/* Sanity check the first entry to make sure it's not 0 */
	if (wwn_to_u64(npiv_tbl->wwnn[0]) == 0 &&
	    wwn_to_u64(npiv_tbl->wwpn[0]) == 0) {
		BNX2FC_HBA_DBG(lport, "First NPIV table entries invalid.\n");
		goto done;
	}

	vpid.roles = FC_PORT_ROLE_FCP_INITIATOR;
	vpid.vport_type = FC_PORTTYPE_NPIV;
	vpid.disable = false;

	for (i = 0; i < npiv_tbl->count; i++) {
		vpid.node_name = wwn_to_u64(npiv_tbl->wwnn[i]);
		vpid.port_name = wwn_to_u64(npiv_tbl->wwpn[i]);
		scnprintf(vpid.symbolic_name, sizeof(vpid.symbolic_name),
		    "NPIV[%u]:%016llx-%016llx",
		    created, vpid.port_name, vpid.node_name);
		if (fc_vport_create(lport->host, 0, &vpid))
			created++;
		else
			BNX2FC_HBA_DBG(lport, "Failed to create vport\n");
	}
done:
	return created;
}

2091 2092 2093
static int __bnx2fc_enable(struct fcoe_ctlr *ctlr)
{
	struct bnx2fc_interface *interface = fcoe_ctlr_priv(ctlr);
2094 2095 2096
	struct bnx2fc_hba *hba;
	struct cnic_fc_npiv_tbl npiv_tbl;
	struct fc_lport *lport;
2097 2098 2099 2100 2101 2102 2103 2104 2105 2106

	if (interface->enabled == false) {
		if (!ctlr->lp) {
			pr_err(PFX "__bnx2fc_enable: lport not found\n");
			return -ENODEV;
		} else if (!bnx2fc_link_ok(ctlr->lp)) {
			fcoe_ctlr_link_up(ctlr);
			interface->enabled = true;
		}
	}
2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132

	/* Create static NPIV ports if any are contained in NVRAM */
	hba = interface->hba;
	lport = ctlr->lp;

	if (!hba)
		goto done;

	if (!hba->cnic)
		goto done;

	if (!lport)
		goto done;

	if (!lport->host)
		goto done;

	if (!hba->cnic->get_fc_npiv_tbl)
		goto done;

	memset(&npiv_tbl, 0, sizeof(npiv_tbl));
	if (hba->cnic->get_fc_npiv_tbl(hba->cnic, &npiv_tbl))
		goto done;

	bnx2fc_npiv_create_vports(lport, &npiv_tbl);
done:
2133 2134 2135
	return 0;
}

2136 2137 2138
/**
 * Deprecated: Use bnx2fc_enabled()
 */
2139 2140
static int bnx2fc_enable(struct net_device *netdev)
{
2141
	struct bnx2fc_interface *interface;
2142
	struct fcoe_ctlr *ctlr;
2143 2144
	int rc = 0;

2145
	rtnl_lock();
2146 2147
	mutex_lock(&bnx2fc_dev_lock);

2148
	interface = bnx2fc_interface_lookup(netdev);
2149
	ctlr = bnx2fc_to_ctlr(interface);
2150
	if (!interface) {
2151
		rc = -ENODEV;
2152 2153 2154
		pr_err(PFX "bnx2fc_enable: interface not found\n");
	} else {
		rc = __bnx2fc_enable(ctlr);
2155
	}
2156 2157 2158 2159 2160 2161 2162

	mutex_unlock(&bnx2fc_dev_lock);
	rtnl_unlock();
	return rc;
}

/**
2163 2164
 * bnx2fc_ctlr_enabled() - Enable or disable an FCoE Controller
 * @cdev: The FCoE Controller that is being enabled or disabled
2165
 *
2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177
 * fcoe_sysfs will ensure that the state of 'enabled' has
 * changed, so no checking is necessary here. This routine simply
 * calls fcoe_enable or fcoe_disable, both of which are deprecated.
 * When those routines are removed the functionality can be merged
 * here.
 */
static int bnx2fc_ctlr_enabled(struct fcoe_ctlr_device *cdev)
{
	struct fcoe_ctlr *ctlr = fcoe_ctlr_device_priv(cdev);

	switch (cdev->enabled) {
	case FCOE_CTLR_ENABLED:
2178
		return __bnx2fc_enable(ctlr);
2179
	case FCOE_CTLR_DISABLED:
2180
		return __bnx2fc_disable(ctlr);
2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196
	case FCOE_CTLR_UNUSED:
	default:
		return -ENOTSUPP;
	};
}

enum bnx2fc_create_link_state {
	BNX2FC_CREATE_LINK_DOWN,
	BNX2FC_CREATE_LINK_UP,
};

/**
 * _bnx2fc_create() - Create bnx2fc FCoE interface
 * @netdev  :   The net_device object the Ethernet interface to create on
 * @fip_mode:   The FIP mode for this creation
 * @link_state: The ctlr link state on creation
2197
 *
2198 2199
 * Called from either the libfcoe 'create' module parameter
 * via fcoe_create or from fcoe_syfs's ctlr_create file.
2200
 *
2201 2202 2203
 * libfcoe's 'create' module parameter is deprecated so some
 * consolidation of code can be done when that interface is
 * removed.
2204 2205 2206
 *
 * Returns: 0 for success
 */
2207 2208 2209
static int _bnx2fc_create(struct net_device *netdev,
			  enum fip_state fip_mode,
			  enum bnx2fc_create_link_state link_state)
2210
{
2211
	struct fcoe_ctlr_device *cdev;
2212
	struct fcoe_ctlr *ctlr;
2213
	struct bnx2fc_interface *interface;
2214
	struct bnx2fc_hba *hba;
2215
	struct net_device *phys_dev = netdev;
2216 2217 2218
	struct fc_lport *lport;
	struct ethtool_drvinfo drvinfo;
	int rc = 0;
2219
	int vlan_id = 0;
2220 2221 2222 2223 2224 2225 2226

	BNX2FC_MISC_DBG("Entered bnx2fc_create\n");
	if (fip_mode != FIP_MODE_FABRIC) {
		printk(KERN_ERR "fip mode not FABRIC\n");
		return -EIO;
	}

2227 2228
	rtnl_lock();

2229 2230 2231 2232 2233 2234 2235 2236
	mutex_lock(&bnx2fc_dev_lock);

	if (!try_module_get(THIS_MODULE)) {
		rc = -EINVAL;
		goto mod_err;
	}

	/* obtain physical netdev */
2237
	if (netdev->priv_flags & IFF_802_1Q_VLAN)
2238
		phys_dev = vlan_dev_real_dev(netdev);
2239

2240 2241 2242 2243
	/* verify if the physical device is a netxtreme2 device */
	if (phys_dev->ethtool_ops && phys_dev->ethtool_ops->get_drvinfo) {
		memset(&drvinfo, 0, sizeof(drvinfo));
		phys_dev->ethtool_ops->get_drvinfo(phys_dev, &drvinfo);
2244
		if (strncmp(drvinfo.driver, "bnx2x", strlen("bnx2x"))) {
2245 2246 2247 2248 2249 2250 2251 2252 2253 2254
			printk(KERN_ERR PFX "Not a netxtreme2 device\n");
			rc = -EINVAL;
			goto netdev_err;
		}
	} else {
		printk(KERN_ERR PFX "unable to obtain drv_info\n");
		rc = -EINVAL;
		goto netdev_err;
	}

2255
	/* obtain interface and initialize rest of the structure */
2256 2257 2258 2259 2260 2261 2262
	hba = bnx2fc_hba_lookup(phys_dev);
	if (!hba) {
		rc = -ENODEV;
		printk(KERN_ERR PFX "bnx2fc_create: hba not found\n");
		goto netdev_err;
	}

2263
	if (bnx2fc_interface_lookup(netdev)) {
2264 2265 2266 2267
		rc = -EEXIST;
		goto netdev_err;
	}

2268 2269 2270
	interface = bnx2fc_interface_create(hba, netdev, fip_mode);
	if (!interface) {
		printk(KERN_ERR PFX "bnx2fc_interface_create failed\n");
2271
		rc = -ENOMEM;
2272 2273 2274
		goto ifput_err;
	}

2275 2276 2277 2278 2279
	if (netdev->priv_flags & IFF_802_1Q_VLAN) {
		vlan_id = vlan_dev_vlan_id(netdev);
		interface->vlan_enabled = 1;
	}

2280
	ctlr = bnx2fc_to_ctlr(interface);
2281
	cdev = fcoe_ctlr_to_ctlr_dev(ctlr);
2282 2283 2284
	interface->vlan_id = vlan_id;

	interface->timer_work_queue =
2285
			create_singlethread_workqueue("bnx2fc_timer_wq");
2286
	if (!interface->timer_work_queue) {
2287 2288 2289 2290 2291
		printk(KERN_ERR PFX "ulp_init could not create timer_wq\n");
		rc = -EINVAL;
		goto ifput_err;
	}

2292
	lport = bnx2fc_if_create(interface, &cdev->dev, 0);
2293 2294 2295 2296 2297 2298 2299
	if (!lport) {
		printk(KERN_ERR PFX "Failed to create interface (%s)\n",
			netdev->name);
		rc = -EINVAL;
		goto if_create_err;
	}

2300 2301 2302
	/* Add interface to if_list */
	list_add_tail(&interface->list, &if_list);

2303 2304 2305
	lport->boot_time = jiffies;

	/* Make this master N_port */
2306
	ctlr->lp = lport;
2307

2308 2309 2310 2311 2312 2313 2314
	if (link_state == BNX2FC_CREATE_LINK_UP)
		cdev->enabled = FCOE_CTLR_ENABLED;
	else
		cdev->enabled = FCOE_CTLR_DISABLED;

	if (link_state == BNX2FC_CREATE_LINK_UP &&
	    !bnx2fc_link_ok(lport)) {
2315
		fcoe_ctlr_link_up(ctlr);
2316 2317 2318 2319
		fc_host_port_type(lport->host) = FC_PORTTYPE_NPORT;
		set_bit(ADAPTER_STATE_READY, &interface->hba->adapter_state);
	}

2320 2321
	BNX2FC_HBA_DBG(lport, "create: START DISC\n");
	bnx2fc_start_disc(interface);
2322 2323 2324 2325

	if (link_state == BNX2FC_CREATE_LINK_UP)
		interface->enabled = true;

2326 2327 2328 2329
	/*
	 * Release from kref_init in bnx2fc_interface_setup, on success
	 * lport should be holding a reference taken in bnx2fc_if_create
	 */
2330
	bnx2fc_interface_put(interface);
2331 2332 2333 2334 2335 2336
	/* put netdev that was held while calling dev_get_by_name */
	mutex_unlock(&bnx2fc_dev_lock);
	rtnl_unlock();
	return 0;

if_create_err:
2337
	destroy_workqueue(interface->timer_work_queue);
2338
ifput_err:
2339
	bnx2fc_net_cleanup(interface);
2340
	bnx2fc_interface_put(interface);
2341
	goto mod_err;
2342 2343 2344 2345 2346 2347 2348 2349
netdev_err:
	module_put(THIS_MODULE);
mod_err:
	mutex_unlock(&bnx2fc_dev_lock);
	rtnl_unlock();
	return rc;
}

2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380
/**
 * bnx2fc_create() - Create a bnx2fc interface
 * @netdev  : The net_device object the Ethernet interface to create on
 * @fip_mode: The FIP mode for this creation
 *
 * Called from fcoe transport
 *
 * Returns: 0 for success
 */
static int bnx2fc_create(struct net_device *netdev, enum fip_state fip_mode)
{
	return _bnx2fc_create(netdev, fip_mode, BNX2FC_CREATE_LINK_UP);
}

/**
 * bnx2fc_ctlr_alloc() - Allocate a bnx2fc interface from fcoe_sysfs
 * @netdev: The net_device to be used by the allocated FCoE Controller
 *
 * This routine is called from fcoe_sysfs. It will start the fcoe_ctlr
 * in a link_down state. The allows the user an opportunity to configure
 * the FCoE Controller from sysfs before enabling the FCoE Controller.
 *
 * Creating in with this routine starts the FCoE Controller in Fabric
 * mode. The user can change to VN2VN or another mode before enabling.
 */
static int bnx2fc_ctlr_alloc(struct net_device *netdev)
{
	return _bnx2fc_create(netdev, FIP_MODE_FABRIC,
			      BNX2FC_CREATE_LINK_DOWN);
}

2381
/**
2382
 * bnx2fc_find_hba_for_cnic - maps cnic instance to bnx2fc hba instance
2383 2384 2385 2386 2387 2388 2389 2390 2391
 *
 * @cnic:	Pointer to cnic device instance
 *
 **/
static struct bnx2fc_hba *bnx2fc_find_hba_for_cnic(struct cnic_dev *cnic)
{
	struct bnx2fc_hba *hba;

	/* Called with bnx2fc_dev_lock held */
2392
	list_for_each_entry(hba, &adapter_list, list) {
2393 2394 2395 2396 2397 2398
		if (hba->cnic == cnic)
			return hba;
	}
	return NULL;
}

2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413
static struct bnx2fc_interface *bnx2fc_interface_lookup(struct net_device
							*netdev)
{
	struct bnx2fc_interface *interface;

	/* Called with bnx2fc_dev_lock held */
	list_for_each_entry(interface, &if_list, list) {
		if (interface->netdev == netdev)
			return interface;
	}
	return NULL;
}

static struct bnx2fc_hba *bnx2fc_hba_lookup(struct net_device
						      *phys_dev)
2414 2415 2416 2417
{
	struct bnx2fc_hba *hba;

	/* Called with bnx2fc_dev_lock held */
2418
	list_for_each_entry(hba, &adapter_list, list) {
2419 2420 2421
		if (hba->phys_dev == phys_dev)
			return hba;
	}
2422
	printk(KERN_ERR PFX "adapter_lookup: hba NULL\n");
2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433
	return NULL;
}

/**
 * bnx2fc_ulp_exit - shuts down adapter instance and frees all resources
 *
 * @dev		cnic device handle
 */
static void bnx2fc_ulp_exit(struct cnic_dev *dev)
{
	struct bnx2fc_hba *hba;
2434
	struct bnx2fc_interface *interface, *tmp;
2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452

	BNX2FC_MISC_DBG("Entered bnx2fc_ulp_exit\n");

	if (!test_bit(CNIC_F_BNX2X_CLASS, &dev->flags)) {
		printk(KERN_ERR PFX "bnx2fc port check: %s, flags: %lx\n",
			dev->netdev->name, dev->flags);
		return;
	}

	mutex_lock(&bnx2fc_dev_lock);
	hba = bnx2fc_find_hba_for_cnic(dev);
	if (!hba) {
		printk(KERN_ERR PFX "bnx2fc_ulp_exit: hba not found, dev 0%p\n",
		       dev);
		mutex_unlock(&bnx2fc_dev_lock);
		return;
	}

2453
	list_del_init(&hba->list);
2454 2455
	adapter_count--;

2456
	list_for_each_entry_safe(interface, tmp, &if_list, list)
2457
		/* destroy not called yet, move to quiesced list */
2458
		if (interface->hba == hba)
2459
			__bnx2fc_destroy(interface);
2460 2461
	mutex_unlock(&bnx2fc_dev_lock);

2462 2463 2464
	/* Ensure ALL destroy work has been completed before return */
	flush_workqueue(bnx2fc_wq);

2465 2466 2467 2468
	bnx2fc_ulp_stop(hba);
	/* unregister cnic device */
	if (test_and_clear_bit(BNX2FC_CNIC_REGISTERED, &hba->reg_with_cnic))
		hba->cnic->unregister_device(hba->cnic, CNIC_ULP_FCOE);
2469
	bnx2fc_hba_destroy(hba);
2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488
}

/**
 * bnx2fc_fcoe_reset - Resets the fcoe
 *
 * @shost: shost the reset is from
 *
 * Returns: always 0
 */
static int bnx2fc_fcoe_reset(struct Scsi_Host *shost)
{
	struct fc_lport *lport = shost_priv(shost);
	fc_lport_reset(lport);
	return 0;
}


static bool bnx2fc_match(struct net_device *netdev)
{
2489 2490
	struct net_device *phys_dev = netdev;

2491
	mutex_lock(&bnx2fc_dev_lock);
2492 2493
	if (netdev->priv_flags & IFF_802_1Q_VLAN)
		phys_dev = vlan_dev_real_dev(netdev);
2494

2495 2496 2497
	if (bnx2fc_hba_lookup(phys_dev)) {
		mutex_unlock(&bnx2fc_dev_lock);
		return true;
2498
	}
2499

2500 2501 2502 2503 2504 2505 2506 2507 2508
	mutex_unlock(&bnx2fc_dev_lock);
	return false;
}


static struct fcoe_transport bnx2fc_transport = {
	.name = {"bnx2fc"},
	.attached = false,
	.list = LIST_HEAD_INIT(bnx2fc_transport.list),
2509
	.alloc = bnx2fc_ctlr_alloc,
2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529
	.match = bnx2fc_match,
	.create = bnx2fc_create,
	.destroy = bnx2fc_destroy,
	.enable = bnx2fc_enable,
	.disable = bnx2fc_disable,
};

/**
 * bnx2fc_percpu_thread_create - Create a receive thread for an
 *				 online CPU
 *
 * @cpu: cpu index for the online cpu
 */
static void bnx2fc_percpu_thread_create(unsigned int cpu)
{
	struct bnx2fc_percpu_s *p;
	struct task_struct *thread;

	p = &per_cpu(bnx2fc_percpu, cpu);

2530 2531 2532
	thread = kthread_create_on_node(bnx2fc_percpu_io_thread,
					(void *)p, cpu_to_node(cpu),
					"bnx2fc_thread/%d", cpu);
2533
	/* bind thread to the cpu */
2534
	if (likely(!IS_ERR(thread))) {
2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556
		kthread_bind(thread, cpu);
		p->iothread = thread;
		wake_up_process(thread);
	}
}

static void bnx2fc_percpu_thread_destroy(unsigned int cpu)
{
	struct bnx2fc_percpu_s *p;
	struct task_struct *thread;
	struct bnx2fc_work *work, *tmp;

	BNX2FC_MISC_DBG("destroying io thread for CPU %d\n", cpu);

	/* Prevent any new work from being queued for this CPU */
	p = &per_cpu(bnx2fc_percpu, cpu);
	spin_lock_bh(&p->fp_work_lock);
	thread = p->iothread;
	p->iothread = NULL;


	/* Free all work in the list */
2557
	list_for_each_entry_safe(work, tmp, &p->work_list, list) {
2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626
		list_del_init(&work->list);
		bnx2fc_process_cq_compl(work->tgt, work->wqe);
		kfree(work);
	}

	spin_unlock_bh(&p->fp_work_lock);

	if (thread)
		kthread_stop(thread);
}

/**
 * bnx2fc_cpu_callback - Handler for CPU hotplug events
 *
 * @nfb:    The callback data block
 * @action: The event triggering the callback
 * @hcpu:   The index of the CPU that the event is for
 *
 * This creates or destroys per-CPU data for fcoe
 *
 * Returns NOTIFY_OK always.
 */
static int bnx2fc_cpu_callback(struct notifier_block *nfb,
			     unsigned long action, void *hcpu)
{
	unsigned cpu = (unsigned long)hcpu;

	switch (action) {
	case CPU_ONLINE:
	case CPU_ONLINE_FROZEN:
		printk(PFX "CPU %x online: Create Rx thread\n", cpu);
		bnx2fc_percpu_thread_create(cpu);
		break;
	case CPU_DEAD:
	case CPU_DEAD_FROZEN:
		printk(PFX "CPU %x offline: Remove Rx thread\n", cpu);
		bnx2fc_percpu_thread_destroy(cpu);
		break;
	default:
		break;
	}
	return NOTIFY_OK;
}

/**
 * bnx2fc_mod_init - module init entry point
 *
 * Initialize driver wide global data structures, and register
 * with cnic module
 **/
static int __init bnx2fc_mod_init(void)
{
	struct fcoe_percpu_s *bg;
	struct task_struct *l2_thread;
	int rc = 0;
	unsigned int cpu = 0;
	struct bnx2fc_percpu_s *p;

	printk(KERN_INFO PFX "%s", version);

	/* register as a fcoe transport */
	rc = fcoe_transport_attach(&bnx2fc_transport);
	if (rc) {
		printk(KERN_ERR "failed to register an fcoe transport, check "
			"if libfcoe is loaded\n");
		goto out;
	}

	INIT_LIST_HEAD(&adapter_list);
2627
	INIT_LIST_HEAD(&if_list);
2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661
	mutex_init(&bnx2fc_dev_lock);
	adapter_count = 0;

	/* Attach FC transport template */
	rc = bnx2fc_attach_transport();
	if (rc)
		goto detach_ft;

	bnx2fc_wq = alloc_workqueue("bnx2fc", 0, 0);
	if (!bnx2fc_wq) {
		rc = -ENOMEM;
		goto release_bt;
	}

	bg = &bnx2fc_global;
	skb_queue_head_init(&bg->fcoe_rx_list);
	l2_thread = kthread_create(bnx2fc_l2_rcv_thread,
				   (void *)bg,
				   "bnx2fc_l2_thread");
	if (IS_ERR(l2_thread)) {
		rc = PTR_ERR(l2_thread);
		goto free_wq;
	}
	wake_up_process(l2_thread);
	spin_lock_bh(&bg->fcoe_rx_list.lock);
	bg->thread = l2_thread;
	spin_unlock_bh(&bg->fcoe_rx_list.lock);

	for_each_possible_cpu(cpu) {
		p = &per_cpu(bnx2fc_percpu, cpu);
		INIT_LIST_HEAD(&p->work_list);
		spin_lock_init(&p->fp_work_lock);
	}

2662 2663
	cpu_notifier_register_begin();

2664 2665 2666 2667 2668
	for_each_online_cpu(cpu) {
		bnx2fc_percpu_thread_create(cpu);
	}

	/* Initialize per CPU interrupt thread */
2669 2670 2671
	__register_hotcpu_notifier(&bnx2fc_cpu_notifier);

	cpu_notifier_register_done();
2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708

	cnic_register_driver(CNIC_ULP_FCOE, &bnx2fc_cnic_cb);

	return 0;

free_wq:
	destroy_workqueue(bnx2fc_wq);
release_bt:
	bnx2fc_release_transport();
detach_ft:
	fcoe_transport_detach(&bnx2fc_transport);
out:
	return rc;
}

static void __exit bnx2fc_mod_exit(void)
{
	LIST_HEAD(to_be_deleted);
	struct bnx2fc_hba *hba, *next;
	struct fcoe_percpu_s *bg;
	struct task_struct *l2_thread;
	struct sk_buff *skb;
	unsigned int cpu = 0;

	/*
	 * NOTE: Since cnic calls register_driver routine rtnl_lock,
	 * it will have higher precedence than bnx2fc_dev_lock.
	 * unregister_device() cannot be called with bnx2fc_dev_lock
	 * held.
	 */
	mutex_lock(&bnx2fc_dev_lock);
	list_splice(&adapter_list, &to_be_deleted);
	INIT_LIST_HEAD(&adapter_list);
	adapter_count = 0;
	mutex_unlock(&bnx2fc_dev_lock);

	/* Unregister with cnic */
2709 2710 2711 2712
	list_for_each_entry_safe(hba, next, &to_be_deleted, list) {
		list_del_init(&hba->list);
		printk(KERN_ERR PFX "MOD_EXIT:destroy hba = 0x%p\n",
		       hba);
2713 2714 2715 2716
		bnx2fc_ulp_stop(hba);
		/* unregister cnic device */
		if (test_and_clear_bit(BNX2FC_CNIC_REGISTERED,
				       &hba->reg_with_cnic))
2717 2718 2719
			hba->cnic->unregister_device(hba->cnic,
							 CNIC_ULP_FCOE);
		bnx2fc_hba_destroy(hba);
2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735
	}
	cnic_unregister_driver(CNIC_ULP_FCOE);

	/* Destroy global thread */
	bg = &bnx2fc_global;
	spin_lock_bh(&bg->fcoe_rx_list.lock);
	l2_thread = bg->thread;
	bg->thread = NULL;
	while ((skb = __skb_dequeue(&bg->fcoe_rx_list)) != NULL)
		kfree_skb(skb);

	spin_unlock_bh(&bg->fcoe_rx_list.lock);

	if (l2_thread)
		kthread_stop(l2_thread);

2736
	cpu_notifier_register_begin();
2737 2738 2739 2740 2741 2742

	/* Destroy per cpu threads */
	for_each_online_cpu(cpu) {
		bnx2fc_percpu_thread_destroy(cpu);
	}

2743 2744 2745 2746
	__unregister_hotcpu_notifier(&bnx2fc_cpu_notifier);

	cpu_notifier_register_done();

2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760
	destroy_workqueue(bnx2fc_wq);
	/*
	 * detach from scsi transport
	 * must happen after all destroys are done
	 */
	bnx2fc_release_transport();

	/* detach from fcoe transport */
	fcoe_transport_detach(&bnx2fc_transport);
}

module_init(bnx2fc_mod_init);
module_exit(bnx2fc_mod_exit);

2761
static struct fcoe_sysfs_function_template bnx2fc_fcoe_sysfs_templ = {
2762
	.set_fcoe_ctlr_enabled = bnx2fc_ctlr_enabled,
2763 2764 2765 2766 2767 2768
	.get_fcoe_ctlr_link_fail = fcoe_ctlr_get_lesb,
	.get_fcoe_ctlr_vlink_fail = fcoe_ctlr_get_lesb,
	.get_fcoe_ctlr_miss_fka = fcoe_ctlr_get_lesb,
	.get_fcoe_ctlr_symb_err = fcoe_ctlr_get_lesb,
	.get_fcoe_ctlr_err_block = fcoe_ctlr_get_lesb,
	.get_fcoe_ctlr_fcs_error = fcoe_ctlr_get_lesb,
2769 2770 2771 2772 2773

	.get_fcoe_fcf_selected = fcoe_fcf_get_selected,
	.get_fcoe_fcf_vlan_id = bnx2fc_fcf_get_vlan_id,
};

2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810
static struct fc_function_template bnx2fc_transport_function = {
	.show_host_node_name = 1,
	.show_host_port_name = 1,
	.show_host_supported_classes = 1,
	.show_host_supported_fc4s = 1,
	.show_host_active_fc4s = 1,
	.show_host_maxframe_size = 1,

	.show_host_port_id = 1,
	.show_host_supported_speeds = 1,
	.get_host_speed = fc_get_host_speed,
	.show_host_speed = 1,
	.show_host_port_type = 1,
	.get_host_port_state = fc_get_host_port_state,
	.show_host_port_state = 1,
	.show_host_symbolic_name = 1,

	.dd_fcrport_size = (sizeof(struct fc_rport_libfc_priv) +
				sizeof(struct bnx2fc_rport)),
	.show_rport_maxframe_size = 1,
	.show_rport_supported_classes = 1,

	.show_host_fabric_name = 1,
	.show_starget_node_name = 1,
	.show_starget_port_name = 1,
	.show_starget_port_id = 1,
	.set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
	.show_rport_dev_loss_tmo = 1,
	.get_fc_host_stats = bnx2fc_get_host_stats,

	.issue_fc_host_lip = bnx2fc_fcoe_reset,

	.terminate_rport_io = fc_rport_terminate_io,

	.vport_create = bnx2fc_vport_create,
	.vport_delete = bnx2fc_vport_destroy,
	.vport_disable = bnx2fc_vport_disable,
2811
	.bsg_request = fc_lport_bsg_request,
2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844
};

static struct fc_function_template bnx2fc_vport_xport_function = {
	.show_host_node_name = 1,
	.show_host_port_name = 1,
	.show_host_supported_classes = 1,
	.show_host_supported_fc4s = 1,
	.show_host_active_fc4s = 1,
	.show_host_maxframe_size = 1,

	.show_host_port_id = 1,
	.show_host_supported_speeds = 1,
	.get_host_speed = fc_get_host_speed,
	.show_host_speed = 1,
	.show_host_port_type = 1,
	.get_host_port_state = fc_get_host_port_state,
	.show_host_port_state = 1,
	.show_host_symbolic_name = 1,

	.dd_fcrport_size = (sizeof(struct fc_rport_libfc_priv) +
				sizeof(struct bnx2fc_rport)),
	.show_rport_maxframe_size = 1,
	.show_rport_supported_classes = 1,

	.show_host_fabric_name = 1,
	.show_starget_node_name = 1,
	.show_starget_port_name = 1,
	.show_starget_port_id = 1,
	.set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
	.show_rport_dev_loss_tmo = 1,
	.get_fc_host_stats = fc_get_host_stats,
	.issue_fc_host_lip = bnx2fc_fcoe_reset,
	.terminate_rport_io = fc_rport_terminate_io,
2845
	.bsg_request = fc_lport_bsg_request,
2846 2847 2848 2849 2850 2851 2852
};

/**
 * scsi_host_template structure used while registering with SCSI-ml
 */
static struct scsi_host_template bnx2fc_shost_template = {
	.module			= THIS_MODULE,
2853
	.name			= "QLogic Offload FCoE Initiator",
2854 2855 2856 2857 2858 2859
	.queuecommand		= bnx2fc_queuecommand,
	.eh_abort_handler	= bnx2fc_eh_abort,	  /* abts */
	.eh_device_reset_handler = bnx2fc_eh_device_reset, /* lun reset */
	.eh_target_reset_handler = bnx2fc_eh_target_reset, /* tgt reset */
	.eh_host_reset_handler	= fc_eh_host_reset,
	.slave_alloc		= fc_slave_alloc,
2860
	.change_queue_depth	= scsi_change_queue_depth,
2861 2862 2863 2864
	.this_id		= -1,
	.cmd_per_lun		= 3,
	.use_clustering		= ENABLE_CLUSTERING,
	.sg_tablesize		= BNX2FC_MAX_BDS_PER_CMD,
2865
	.max_sectors		= 1024,
2866
	.use_blk_tags		= 1,
2867
	.track_queue_depth	= 1,
2868 2869 2870 2871 2872 2873 2874
};

static struct libfc_function_template bnx2fc_libfc_fcn_templ = {
	.frame_send		= bnx2fc_xmit,
	.elsct_send		= bnx2fc_elsct_send,
	.fcp_abort_io		= bnx2fc_abort_io,
	.fcp_cleanup		= bnx2fc_cleanup,
2875
	.get_lesb		= fcoe_get_lesb,
2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890
	.rport_event_callback	= bnx2fc_rport_event_handler,
};

/**
 * bnx2fc_cnic_cb - global template of bnx2fc - cnic driver interface
 *			structure carrying callback function pointers
 */
static struct cnic_ulp_ops bnx2fc_cnic_cb = {
	.owner			= THIS_MODULE,
	.cnic_init		= bnx2fc_ulp_init,
	.cnic_exit		= bnx2fc_ulp_exit,
	.cnic_start		= bnx2fc_ulp_start,
	.cnic_stop		= bnx2fc_ulp_stop,
	.indicate_kcqes		= bnx2fc_indicate_kcqe,
	.indicate_netevent	= bnx2fc_indicate_netevent,
2891
	.cnic_get_stats		= bnx2fc_ulp_get_stats,
2892
};