1 /* bnx2fc_fcoe.c: QLogic Linux FCoE offload driver.
2  * This file contains the code that interacts with libfc, libfcoe,
3  * cnic modules to create FCoE instances, send/receive non-offloaded
4  * FIP/FCoE packets, listen to link events etc.
5  *
6  * Copyright (c) 2008-2013 Broadcom Corporation
7  * Copyright (c) 2014-2015 QLogic Corporation
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation.
12  *
13  * Written by: Bhanu Prakash Gollapudi (bprakash@broadcom.com)
14  */
15 
16 #include "bnx2fc.h"
17 
18 static struct list_head adapter_list;
19 static struct list_head if_list;
20 static u32 adapter_count;
21 static DEFINE_MUTEX(bnx2fc_dev_lock);
22 DEFINE_PER_CPU(struct bnx2fc_percpu_s, bnx2fc_percpu);
23 
24 #define DRV_MODULE_NAME		"bnx2fc"
25 #define DRV_MODULE_VERSION	BNX2FC_VERSION
26 #define DRV_MODULE_RELDATE	"October 15, 2015"
27 
28 
29 static char version[] =
30 		"QLogic FCoE Driver " DRV_MODULE_NAME \
31 		" v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")\n";
32 
33 
34 MODULE_AUTHOR("Bhanu Prakash Gollapudi <bprakash@broadcom.com>");
35 MODULE_DESCRIPTION("QLogic FCoE Driver");
36 MODULE_LICENSE("GPL");
37 MODULE_VERSION(DRV_MODULE_VERSION);
38 
39 #define BNX2FC_MAX_QUEUE_DEPTH	256
40 #define BNX2FC_MIN_QUEUE_DEPTH	32
41 #define FCOE_WORD_TO_BYTE  4
42 
43 static struct scsi_transport_template	*bnx2fc_transport_template;
44 static struct scsi_transport_template	*bnx2fc_vport_xport_template;
45 
46 struct workqueue_struct *bnx2fc_wq;
47 
48 /* bnx2fc structure needs only one instance of the fcoe_percpu_s structure.
49  * Here the io threads are per cpu but the l2 thread is just one
50  */
51 struct fcoe_percpu_s bnx2fc_global;
52 DEFINE_SPINLOCK(bnx2fc_global_lock);
53 
54 static struct cnic_ulp_ops bnx2fc_cnic_cb;
55 static struct libfc_function_template bnx2fc_libfc_fcn_templ;
56 static struct scsi_host_template bnx2fc_shost_template;
57 static struct fc_function_template bnx2fc_transport_function;
58 static struct fcoe_sysfs_function_template bnx2fc_fcoe_sysfs_templ;
59 static struct fc_function_template bnx2fc_vport_xport_function;
60 static int bnx2fc_create(struct net_device *netdev, enum fip_state fip_mode);
61 static void __bnx2fc_destroy(struct bnx2fc_interface *interface);
62 static int bnx2fc_destroy(struct net_device *net_device);
63 static int bnx2fc_enable(struct net_device *netdev);
64 static int bnx2fc_disable(struct net_device *netdev);
65 
66 /* fcoe_syfs control interface handlers */
67 static int bnx2fc_ctlr_alloc(struct net_device *netdev);
68 static int bnx2fc_ctlr_enabled(struct fcoe_ctlr_device *cdev);
69 
70 static void bnx2fc_recv_frame(struct sk_buff *skb);
71 
72 static void bnx2fc_start_disc(struct bnx2fc_interface *interface);
73 static int bnx2fc_shost_config(struct fc_lport *lport, struct device *dev);
74 static int bnx2fc_lport_config(struct fc_lport *lport);
75 static int bnx2fc_em_config(struct fc_lport *lport, struct bnx2fc_hba *hba);
76 static int bnx2fc_bind_adapter_devices(struct bnx2fc_hba *hba);
77 static void bnx2fc_unbind_adapter_devices(struct bnx2fc_hba *hba);
78 static int bnx2fc_bind_pcidev(struct bnx2fc_hba *hba);
79 static void bnx2fc_unbind_pcidev(struct bnx2fc_hba *hba);
80 static struct fc_lport *bnx2fc_if_create(struct bnx2fc_interface *interface,
81 				  struct device *parent, int npiv);
82 static void bnx2fc_destroy_work(struct work_struct *work);
83 
84 static struct bnx2fc_hba *bnx2fc_hba_lookup(struct net_device *phys_dev);
85 static struct bnx2fc_interface *bnx2fc_interface_lookup(struct net_device
86 							*phys_dev);
87 static inline void bnx2fc_interface_put(struct bnx2fc_interface *interface);
88 static struct bnx2fc_hba *bnx2fc_find_hba_for_cnic(struct cnic_dev *cnic);
89 
90 static int bnx2fc_fw_init(struct bnx2fc_hba *hba);
91 static void bnx2fc_fw_destroy(struct bnx2fc_hba *hba);
92 
93 static void bnx2fc_port_shutdown(struct fc_lport *lport);
94 static void bnx2fc_stop(struct bnx2fc_interface *interface);
95 static int __init bnx2fc_mod_init(void);
96 static void __exit bnx2fc_mod_exit(void);
97 
98 unsigned int bnx2fc_debug_level;
99 module_param_named(debug_logging, bnx2fc_debug_level, int, S_IRUGO|S_IWUSR);
100 
101 static int bnx2fc_cpu_callback(struct notifier_block *nfb,
102 			     unsigned long action, void *hcpu);
103 /* notification function for CPU hotplug events */
104 static struct notifier_block bnx2fc_cpu_notifier = {
105 	.notifier_call = bnx2fc_cpu_callback,
106 };
107 
108 static inline struct net_device *bnx2fc_netdev(const struct fc_lport *lport)
109 {
110 	return ((struct bnx2fc_interface *)
111 		((struct fcoe_port *)lport_priv(lport))->priv)->netdev;
112 }
113 
114 static void bnx2fc_fcf_get_vlan_id(struct fcoe_fcf_device *fcf_dev)
115 {
116 	struct fcoe_ctlr_device *ctlr_dev =
117 		fcoe_fcf_dev_to_ctlr_dev(fcf_dev);
118 	struct fcoe_ctlr *ctlr = fcoe_ctlr_device_priv(ctlr_dev);
119 	struct bnx2fc_interface *fcoe = fcoe_ctlr_priv(ctlr);
120 
121 	fcf_dev->vlan_id = fcoe->vlan_id;
122 }
123 
124 static void bnx2fc_clean_rx_queue(struct fc_lport *lp)
125 {
126 	struct fcoe_percpu_s *bg;
127 	struct fcoe_rcv_info *fr;
128 	struct sk_buff_head *list;
129 	struct sk_buff *skb, *next;
130 	struct sk_buff *head;
131 
132 	bg = &bnx2fc_global;
133 	spin_lock_bh(&bg->fcoe_rx_list.lock);
134 	list = &bg->fcoe_rx_list;
135 	head = list->next;
136 	for (skb = head; skb != (struct sk_buff *)list;
137 	     skb = next) {
138 		next = skb->next;
139 		fr = fcoe_dev_from_skb(skb);
140 		if (fr->fr_dev == lp) {
141 			__skb_unlink(skb, list);
142 			kfree_skb(skb);
143 		}
144 	}
145 	spin_unlock_bh(&bg->fcoe_rx_list.lock);
146 }
147 
148 int bnx2fc_get_paged_crc_eof(struct sk_buff *skb, int tlen)
149 {
150 	int rc;
151 	spin_lock(&bnx2fc_global_lock);
152 	rc = fcoe_get_paged_crc_eof(skb, tlen, &bnx2fc_global);
153 	spin_unlock(&bnx2fc_global_lock);
154 
155 	return rc;
156 }
157 
158 static void bnx2fc_abort_io(struct fc_lport *lport)
159 {
160 	/*
161 	 * This function is no-op for bnx2fc, but we do
162 	 * not want to leave it as NULL either, as libfc
163 	 * can call the default function which is
164 	 * fc_fcp_abort_io.
165 	 */
166 }
167 
168 static void bnx2fc_cleanup(struct fc_lport *lport)
169 {
170 	struct fcoe_port *port = lport_priv(lport);
171 	struct bnx2fc_interface *interface = port->priv;
172 	struct bnx2fc_hba *hba = interface->hba;
173 	struct bnx2fc_rport *tgt;
174 	int i;
175 
176 	BNX2FC_MISC_DBG("Entered %s\n", __func__);
177 	mutex_lock(&hba->hba_mutex);
178 	spin_lock_bh(&hba->hba_lock);
179 	for (i = 0; i < BNX2FC_NUM_MAX_SESS; i++) {
180 		tgt = hba->tgt_ofld_list[i];
181 		if (tgt) {
182 			/* Cleanup IOs belonging to requested vport */
183 			if (tgt->port == port) {
184 				spin_unlock_bh(&hba->hba_lock);
185 				BNX2FC_TGT_DBG(tgt, "flush/cleanup\n");
186 				bnx2fc_flush_active_ios(tgt);
187 				spin_lock_bh(&hba->hba_lock);
188 			}
189 		}
190 	}
191 	spin_unlock_bh(&hba->hba_lock);
192 	mutex_unlock(&hba->hba_mutex);
193 }
194 
195 static int bnx2fc_xmit_l2_frame(struct bnx2fc_rport *tgt,
196 			     struct fc_frame *fp)
197 {
198 	struct fc_rport_priv *rdata = tgt->rdata;
199 	struct fc_frame_header *fh;
200 	int rc = 0;
201 
202 	fh = fc_frame_header_get(fp);
203 	BNX2FC_TGT_DBG(tgt, "Xmit L2 frame rport = 0x%x, oxid = 0x%x, "
204 			"r_ctl = 0x%x\n", rdata->ids.port_id,
205 			ntohs(fh->fh_ox_id), fh->fh_r_ctl);
206 	if ((fh->fh_type == FC_TYPE_ELS) &&
207 	    (fh->fh_r_ctl == FC_RCTL_ELS_REQ)) {
208 
209 		switch (fc_frame_payload_op(fp)) {
210 		case ELS_ADISC:
211 			rc = bnx2fc_send_adisc(tgt, fp);
212 			break;
213 		case ELS_LOGO:
214 			rc = bnx2fc_send_logo(tgt, fp);
215 			break;
216 		case ELS_RLS:
217 			rc = bnx2fc_send_rls(tgt, fp);
218 			break;
219 		default:
220 			break;
221 		}
222 	} else if ((fh->fh_type ==  FC_TYPE_BLS) &&
223 	    (fh->fh_r_ctl == FC_RCTL_BA_ABTS))
224 		BNX2FC_TGT_DBG(tgt, "ABTS frame\n");
225 	else {
226 		BNX2FC_TGT_DBG(tgt, "Send L2 frame type 0x%x "
227 				"rctl 0x%x thru non-offload path\n",
228 				fh->fh_type, fh->fh_r_ctl);
229 		return -ENODEV;
230 	}
231 	if (rc)
232 		return -ENOMEM;
233 	else
234 		return 0;
235 }
236 
237 /**
238  * bnx2fc_xmit - bnx2fc's FCoE frame transmit function
239  *
240  * @lport:	the associated local port
241  * @fp:	the fc_frame to be transmitted
242  */
243 static int bnx2fc_xmit(struct fc_lport *lport, struct fc_frame *fp)
244 {
245 	struct ethhdr		*eh;
246 	struct fcoe_crc_eof	*cp;
247 	struct sk_buff		*skb;
248 	struct fc_frame_header	*fh;
249 	struct bnx2fc_interface	*interface;
250 	struct fcoe_ctlr        *ctlr;
251 	struct bnx2fc_hba *hba;
252 	struct fcoe_port	*port;
253 	struct fcoe_hdr		*hp;
254 	struct bnx2fc_rport	*tgt;
255 	struct fc_stats		*stats;
256 	u8			sof, eof;
257 	u32			crc;
258 	unsigned int		hlen, tlen, elen;
259 	int			wlen, rc = 0;
260 
261 	port = (struct fcoe_port *)lport_priv(lport);
262 	interface = port->priv;
263 	ctlr = bnx2fc_to_ctlr(interface);
264 	hba = interface->hba;
265 
266 	fh = fc_frame_header_get(fp);
267 
268 	skb = fp_skb(fp);
269 	if (!lport->link_up) {
270 		BNX2FC_HBA_DBG(lport, "bnx2fc_xmit link down\n");
271 		kfree_skb(skb);
272 		return 0;
273 	}
274 
275 	if (unlikely(fh->fh_r_ctl == FC_RCTL_ELS_REQ)) {
276 		if (!ctlr->sel_fcf) {
277 			BNX2FC_HBA_DBG(lport, "FCF not selected yet!\n");
278 			kfree_skb(skb);
279 			return -EINVAL;
280 		}
281 		if (fcoe_ctlr_els_send(ctlr, lport, skb))
282 			return 0;
283 	}
284 
285 	sof = fr_sof(fp);
286 	eof = fr_eof(fp);
287 
288 	/*
289 	 * Snoop the frame header to check if the frame is for
290 	 * an offloaded session
291 	 */
292 	/*
293 	 * tgt_ofld_list access is synchronized using
294 	 * both hba mutex and hba lock. Atleast hba mutex or
295 	 * hba lock needs to be held for read access.
296 	 */
297 
298 	spin_lock_bh(&hba->hba_lock);
299 	tgt = bnx2fc_tgt_lookup(port, ntoh24(fh->fh_d_id));
300 	if (tgt && (test_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags))) {
301 		/* This frame is for offloaded session */
302 		BNX2FC_HBA_DBG(lport, "xmit: Frame is for offloaded session "
303 				"port_id = 0x%x\n", ntoh24(fh->fh_d_id));
304 		spin_unlock_bh(&hba->hba_lock);
305 		rc = bnx2fc_xmit_l2_frame(tgt, fp);
306 		if (rc != -ENODEV) {
307 			kfree_skb(skb);
308 			return rc;
309 		}
310 	} else {
311 		spin_unlock_bh(&hba->hba_lock);
312 	}
313 
314 	elen = sizeof(struct ethhdr);
315 	hlen = sizeof(struct fcoe_hdr);
316 	tlen = sizeof(struct fcoe_crc_eof);
317 	wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
318 
319 	skb->ip_summed = CHECKSUM_NONE;
320 	crc = fcoe_fc_crc(fp);
321 
322 	/* copy port crc and eof to the skb buff */
323 	if (skb_is_nonlinear(skb)) {
324 		skb_frag_t *frag;
325 		if (bnx2fc_get_paged_crc_eof(skb, tlen)) {
326 			kfree_skb(skb);
327 			return -ENOMEM;
328 		}
329 		frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
330 		cp = kmap_atomic(skb_frag_page(frag)) + frag->page_offset;
331 	} else {
332 		cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
333 	}
334 
335 	memset(cp, 0, sizeof(*cp));
336 	cp->fcoe_eof = eof;
337 	cp->fcoe_crc32 = cpu_to_le32(~crc);
338 	if (skb_is_nonlinear(skb)) {
339 		kunmap_atomic(cp);
340 		cp = NULL;
341 	}
342 
343 	/* adjust skb network/transport offsets to match mac/fcoe/port */
344 	skb_push(skb, elen + hlen);
345 	skb_reset_mac_header(skb);
346 	skb_reset_network_header(skb);
347 	skb->mac_len = elen;
348 	skb->protocol = htons(ETH_P_FCOE);
349 	skb->dev = interface->netdev;
350 
351 	/* fill up mac and fcoe headers */
352 	eh = eth_hdr(skb);
353 	eh->h_proto = htons(ETH_P_FCOE);
354 	if (ctlr->map_dest)
355 		fc_fcoe_set_mac(eh->h_dest, fh->fh_d_id);
356 	else
357 		/* insert GW address */
358 		memcpy(eh->h_dest, ctlr->dest_addr, ETH_ALEN);
359 
360 	if (unlikely(ctlr->flogi_oxid != FC_XID_UNKNOWN))
361 		memcpy(eh->h_source, ctlr->ctl_src_addr, ETH_ALEN);
362 	else
363 		memcpy(eh->h_source, port->data_src_addr, ETH_ALEN);
364 
365 	hp = (struct fcoe_hdr *)(eh + 1);
366 	memset(hp, 0, sizeof(*hp));
367 	if (FC_FCOE_VER)
368 		FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
369 	hp->fcoe_sof = sof;
370 
371 	/* fcoe lso, mss is in max_payload which is non-zero for FCP data */
372 	if (lport->seq_offload && fr_max_payload(fp)) {
373 		skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
374 		skb_shinfo(skb)->gso_size = fr_max_payload(fp);
375 	} else {
376 		skb_shinfo(skb)->gso_type = 0;
377 		skb_shinfo(skb)->gso_size = 0;
378 	}
379 
380 	/*update tx stats */
381 	stats = per_cpu_ptr(lport->stats, get_cpu());
382 	stats->TxFrames++;
383 	stats->TxWords += wlen;
384 	put_cpu();
385 
386 	/* send down to lld */
387 	fr_dev(fp) = lport;
388 	if (port->fcoe_pending_queue.qlen)
389 		fcoe_check_wait_queue(lport, skb);
390 	else if (fcoe_start_io(skb))
391 		fcoe_check_wait_queue(lport, skb);
392 
393 	return 0;
394 }
395 
396 /**
397  * bnx2fc_rcv - This is bnx2fc's receive function called by NET_RX_SOFTIRQ
398  *
399  * @skb:	the receive socket buffer
400  * @dev:	associated net device
401  * @ptype:	context
402  * @olddev:	last device
403  *
404  * This function receives the packet and builds FC frame and passes it up
405  */
406 static int bnx2fc_rcv(struct sk_buff *skb, struct net_device *dev,
407 		struct packet_type *ptype, struct net_device *olddev)
408 {
409 	struct fc_lport *lport;
410 	struct bnx2fc_interface *interface;
411 	struct fcoe_ctlr *ctlr;
412 	struct fc_frame_header *fh;
413 	struct fcoe_rcv_info *fr;
414 	struct fcoe_percpu_s *bg;
415 	struct sk_buff *tmp_skb;
416 	unsigned short oxid;
417 
418 	interface = container_of(ptype, struct bnx2fc_interface,
419 				 fcoe_packet_type);
420 	ctlr = bnx2fc_to_ctlr(interface);
421 	lport = ctlr->lp;
422 
423 	if (unlikely(lport == NULL)) {
424 		printk(KERN_ERR PFX "bnx2fc_rcv: lport is NULL\n");
425 		goto err;
426 	}
427 
428 	tmp_skb = skb_share_check(skb, GFP_ATOMIC);
429 	if (!tmp_skb)
430 		goto err;
431 
432 	skb = tmp_skb;
433 
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 		goto err;
437 	}
438 
439 	/*
440 	 * Check for minimum frame length, and make sure required FCoE
441 	 * and FC headers are pulled into the linear data area.
442 	 */
443 	if (unlikely((skb->len < FCOE_MIN_FRAME) ||
444 	    !pskb_may_pull(skb, FCOE_HEADER_LEN)))
445 		goto err;
446 
447 	skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
448 	fh = (struct fc_frame_header *) skb_transport_header(skb);
449 
450 	oxid = ntohs(fh->fh_ox_id);
451 
452 	fr = fcoe_dev_from_skb(skb);
453 	fr->fr_dev = lport;
454 
455 	bg = &bnx2fc_global;
456 	spin_lock(&bg->fcoe_rx_list.lock);
457 
458 	__skb_queue_tail(&bg->fcoe_rx_list, skb);
459 	if (bg->fcoe_rx_list.qlen == 1)
460 		wake_up_process(bg->thread);
461 
462 	spin_unlock(&bg->fcoe_rx_list.lock);
463 
464 	return 0;
465 err:
466 	kfree_skb(skb);
467 	return -1;
468 }
469 
470 static int bnx2fc_l2_rcv_thread(void *arg)
471 {
472 	struct fcoe_percpu_s *bg = arg;
473 	struct sk_buff *skb;
474 
475 	set_user_nice(current, MIN_NICE);
476 	set_current_state(TASK_INTERRUPTIBLE);
477 	while (!kthread_should_stop()) {
478 		schedule();
479 		spin_lock_bh(&bg->fcoe_rx_list.lock);
480 		while ((skb = __skb_dequeue(&bg->fcoe_rx_list)) != NULL) {
481 			spin_unlock_bh(&bg->fcoe_rx_list.lock);
482 			bnx2fc_recv_frame(skb);
483 			spin_lock_bh(&bg->fcoe_rx_list.lock);
484 		}
485 		__set_current_state(TASK_INTERRUPTIBLE);
486 		spin_unlock_bh(&bg->fcoe_rx_list.lock);
487 	}
488 	__set_current_state(TASK_RUNNING);
489 	return 0;
490 }
491 
492 
493 static void bnx2fc_recv_frame(struct sk_buff *skb)
494 {
495 	u32 fr_len;
496 	struct fc_lport *lport;
497 	struct fcoe_rcv_info *fr;
498 	struct fc_stats *stats;
499 	struct fc_frame_header *fh;
500 	struct fcoe_crc_eof crc_eof;
501 	struct fc_frame *fp;
502 	struct fc_lport *vn_port;
503 	struct fcoe_port *port;
504 	u8 *mac = NULL;
505 	u8 *dest_mac = NULL;
506 	struct fcoe_hdr *hp;
507 
508 	fr = fcoe_dev_from_skb(skb);
509 	lport = fr->fr_dev;
510 	if (unlikely(lport == NULL)) {
511 		printk(KERN_ERR PFX "Invalid lport struct\n");
512 		kfree_skb(skb);
513 		return;
514 	}
515 
516 	if (skb_is_nonlinear(skb))
517 		skb_linearize(skb);
518 	mac = eth_hdr(skb)->h_source;
519 	dest_mac = eth_hdr(skb)->h_dest;
520 
521 	/* Pull the header */
522 	hp = (struct fcoe_hdr *) skb_network_header(skb);
523 	fh = (struct fc_frame_header *) skb_transport_header(skb);
524 	skb_pull(skb, sizeof(struct fcoe_hdr));
525 	fr_len = skb->len - sizeof(struct fcoe_crc_eof);
526 
527 	fp = (struct fc_frame *)skb;
528 	fc_frame_init(fp);
529 	fr_dev(fp) = lport;
530 	fr_sof(fp) = hp->fcoe_sof;
531 	if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof))) {
532 		kfree_skb(skb);
533 		return;
534 	}
535 	fr_eof(fp) = crc_eof.fcoe_eof;
536 	fr_crc(fp) = crc_eof.fcoe_crc32;
537 	if (pskb_trim(skb, fr_len)) {
538 		kfree_skb(skb);
539 		return;
540 	}
541 
542 	fh = fc_frame_header_get(fp);
543 
544 	vn_port = fc_vport_id_lookup(lport, ntoh24(fh->fh_d_id));
545 	if (vn_port) {
546 		port = lport_priv(vn_port);
547 		if (!ether_addr_equal(port->data_src_addr, dest_mac)) {
548 			BNX2FC_HBA_DBG(lport, "fpma mismatch\n");
549 			kfree_skb(skb);
550 			return;
551 		}
552 	}
553 	if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA &&
554 	    fh->fh_type == FC_TYPE_FCP) {
555 		/* Drop FCP data. We dont this in L2 path */
556 		kfree_skb(skb);
557 		return;
558 	}
559 	if (fh->fh_r_ctl == FC_RCTL_ELS_REQ &&
560 	    fh->fh_type == FC_TYPE_ELS) {
561 		switch (fc_frame_payload_op(fp)) {
562 		case ELS_LOGO:
563 			if (ntoh24(fh->fh_s_id) == FC_FID_FLOGI) {
564 				/* drop non-FIP LOGO */
565 				kfree_skb(skb);
566 				return;
567 			}
568 			break;
569 		}
570 	}
571 
572 	if (fh->fh_r_ctl == FC_RCTL_BA_ABTS) {
573 		/* Drop incoming ABTS */
574 		kfree_skb(skb);
575 		return;
576 	}
577 
578 	stats = per_cpu_ptr(lport->stats, smp_processor_id());
579 	stats->RxFrames++;
580 	stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
581 
582 	if (le32_to_cpu(fr_crc(fp)) !=
583 			~crc32(~0, skb->data, fr_len)) {
584 		if (stats->InvalidCRCCount < 5)
585 			printk(KERN_WARNING PFX "dropping frame with "
586 			       "CRC error\n");
587 		stats->InvalidCRCCount++;
588 		kfree_skb(skb);
589 		return;
590 	}
591 	fc_exch_recv(lport, fp);
592 }
593 
594 /**
595  * bnx2fc_percpu_io_thread - thread per cpu for ios
596  *
597  * @arg:	ptr to bnx2fc_percpu_info structure
598  */
599 int bnx2fc_percpu_io_thread(void *arg)
600 {
601 	struct bnx2fc_percpu_s *p = arg;
602 	struct bnx2fc_work *work, *tmp;
603 	LIST_HEAD(work_list);
604 
605 	set_user_nice(current, MIN_NICE);
606 	set_current_state(TASK_INTERRUPTIBLE);
607 	while (!kthread_should_stop()) {
608 		schedule();
609 		spin_lock_bh(&p->fp_work_lock);
610 		while (!list_empty(&p->work_list)) {
611 			list_splice_init(&p->work_list, &work_list);
612 			spin_unlock_bh(&p->fp_work_lock);
613 
614 			list_for_each_entry_safe(work, tmp, &work_list, list) {
615 				list_del_init(&work->list);
616 				bnx2fc_process_cq_compl(work->tgt, work->wqe);
617 				kfree(work);
618 			}
619 
620 			spin_lock_bh(&p->fp_work_lock);
621 		}
622 		__set_current_state(TASK_INTERRUPTIBLE);
623 		spin_unlock_bh(&p->fp_work_lock);
624 	}
625 	__set_current_state(TASK_RUNNING);
626 
627 	return 0;
628 }
629 
630 static struct fc_host_statistics *bnx2fc_get_host_stats(struct Scsi_Host *shost)
631 {
632 	struct fc_host_statistics *bnx2fc_stats;
633 	struct fc_lport *lport = shost_priv(shost);
634 	struct fcoe_port *port = lport_priv(lport);
635 	struct bnx2fc_interface *interface = port->priv;
636 	struct bnx2fc_hba *hba = interface->hba;
637 	struct fcoe_statistics_params *fw_stats;
638 	int rc = 0;
639 
640 	fw_stats = (struct fcoe_statistics_params *)hba->stats_buffer;
641 	if (!fw_stats)
642 		return NULL;
643 
644 	bnx2fc_stats = fc_get_host_stats(shost);
645 
646 	init_completion(&hba->stat_req_done);
647 	if (bnx2fc_send_stat_req(hba))
648 		return bnx2fc_stats;
649 	rc = wait_for_completion_timeout(&hba->stat_req_done, (2 * HZ));
650 	if (!rc) {
651 		BNX2FC_HBA_DBG(lport, "FW stat req timed out\n");
652 		return bnx2fc_stats;
653 	}
654 	BNX2FC_STATS(hba, rx_stat2, fc_crc_cnt);
655 	bnx2fc_stats->invalid_crc_count += hba->bfw_stats.fc_crc_cnt;
656 	BNX2FC_STATS(hba, tx_stat, fcoe_tx_pkt_cnt);
657 	bnx2fc_stats->tx_frames += hba->bfw_stats.fcoe_tx_pkt_cnt;
658 	BNX2FC_STATS(hba, tx_stat, fcoe_tx_byte_cnt);
659 	bnx2fc_stats->tx_words += ((hba->bfw_stats.fcoe_tx_byte_cnt) / 4);
660 	BNX2FC_STATS(hba, rx_stat0, fcoe_rx_pkt_cnt);
661 	bnx2fc_stats->rx_frames += hba->bfw_stats.fcoe_rx_pkt_cnt;
662 	BNX2FC_STATS(hba, rx_stat0, fcoe_rx_byte_cnt);
663 	bnx2fc_stats->rx_words += ((hba->bfw_stats.fcoe_rx_byte_cnt) / 4);
664 
665 	bnx2fc_stats->dumped_frames = 0;
666 	bnx2fc_stats->lip_count = 0;
667 	bnx2fc_stats->nos_count = 0;
668 	bnx2fc_stats->loss_of_sync_count = 0;
669 	bnx2fc_stats->loss_of_signal_count = 0;
670 	bnx2fc_stats->prim_seq_protocol_err_count = 0;
671 
672 	memcpy(&hba->prev_stats, hba->stats_buffer,
673 	       sizeof(struct fcoe_statistics_params));
674 	return bnx2fc_stats;
675 }
676 
677 static int bnx2fc_shost_config(struct fc_lport *lport, struct device *dev)
678 {
679 	struct fcoe_port *port = lport_priv(lport);
680 	struct bnx2fc_interface *interface = port->priv;
681 	struct bnx2fc_hba *hba = interface->hba;
682 	struct Scsi_Host *shost = lport->host;
683 	int rc = 0;
684 
685 	shost->max_cmd_len = BNX2FC_MAX_CMD_LEN;
686 	shost->max_lun = BNX2FC_MAX_LUN;
687 	shost->max_id = BNX2FC_MAX_FCP_TGT;
688 	shost->max_channel = 0;
689 	if (lport->vport)
690 		shost->transportt = bnx2fc_vport_xport_template;
691 	else
692 		shost->transportt = bnx2fc_transport_template;
693 
694 	/* Add the new host to SCSI-ml */
695 	rc = scsi_add_host(lport->host, dev);
696 	if (rc) {
697 		printk(KERN_ERR PFX "Error on scsi_add_host\n");
698 		return rc;
699 	}
700 	if (!lport->vport)
701 		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 	return 0;
708 }
709 
710 static int bnx2fc_link_ok(struct fc_lport *lport)
711 {
712 	struct fcoe_port *port = lport_priv(lport);
713 	struct bnx2fc_interface *interface = port->priv;
714 	struct bnx2fc_hba *hba = interface->hba;
715 	struct net_device *dev = hba->phys_dev;
716 	int rc = 0;
717 
718 	if ((dev->flags & IFF_UP) && netif_carrier_ok(dev))
719 		clear_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
720 	else {
721 		set_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
722 		rc = -1;
723 	}
724 	return rc;
725 }
726 
727 /**
728  * bnx2fc_get_link_state - get network link state
729  *
730  * @hba:	adapter instance pointer
731  *
732  * updates adapter structure flag based on netdev state
733  */
734 void bnx2fc_get_link_state(struct bnx2fc_hba *hba)
735 {
736 	if (test_bit(__LINK_STATE_NOCARRIER, &hba->phys_dev->state))
737 		set_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
738 	else
739 		clear_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
740 }
741 
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 	struct fcoe_port *port;
748 	u64 wwnn, wwpn;
749 
750 	port = lport_priv(lport);
751 	interface = port->priv;
752 	ctlr = bnx2fc_to_ctlr(interface);
753 	hba = interface->hba;
754 
755 	/* require support for get_pauseparam ethtool op. */
756 	if (!hba->phys_dev->ethtool_ops ||
757 	    !hba->phys_dev->ethtool_ops->get_pauseparam)
758 		return -EOPNOTSUPP;
759 
760 	if (fc_set_mfs(lport, BNX2FC_MFS))
761 		return -EINVAL;
762 
763 	skb_queue_head_init(&port->fcoe_pending_queue);
764 	port->fcoe_pending_queue_active = 0;
765 	setup_timer(&port->timer, fcoe_queue_timer, (unsigned long) lport);
766 
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 		BNX2FC_HBA_DBG(lport, "WWNN = 0x%llx\n", wwnn);
774 		fc_set_wwnn(lport, wwnn);
775 
776 		if (fcoe_get_wwn(netdev, &wwpn, NETDEV_FCOE_WWPN))
777 			wwpn = fcoe_wwn_from_mac(ctlr->ctl_src_addr,
778 						 2, 0);
779 
780 		BNX2FC_HBA_DBG(lport, "WWPN = 0x%llx\n", wwpn);
781 		fc_set_wwpn(lport, wwpn);
782 	}
783 
784 	return 0;
785 }
786 
787 static void bnx2fc_destroy_timer(unsigned long data)
788 {
789 	struct bnx2fc_hba *hba = (struct bnx2fc_hba *)data;
790 
791 	printk(KERN_ERR PFX "ERROR:bnx2fc_destroy_timer - "
792 	       "Destroy compl not received!!\n");
793 	set_bit(BNX2FC_FLAG_DESTROY_CMPL, &hba->flags);
794 	wake_up_interruptible(&hba->destroy_wait);
795 }
796 
797 /**
798  * bnx2fc_indicate_netevent - Generic netdev event handler
799  *
800  * @context:	adapter structure pointer
801  * @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 static void bnx2fc_indicate_netevent(void *context, unsigned long event,
808 				     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 	u32 link_possible = 1;
818 
819 	if (vlan_id != 0 && event != NETDEV_UNREGISTER)
820 		return;
821 
822 	switch (event) {
823 	case NETDEV_UP:
824 		if (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state))
825 			printk(KERN_ERR "indicate_netevent: "\
826 					"hba is not UP!!\n");
827 		break;
828 
829 	case NETDEV_DOWN:
830 		clear_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state);
831 		clear_bit(ADAPTER_STATE_UP, &hba->adapter_state);
832 		link_possible = 0;
833 		break;
834 
835 	case NETDEV_GOING_DOWN:
836 		set_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state);
837 		link_possible = 0;
838 		break;
839 
840 	case NETDEV_CHANGE:
841 		break;
842 
843 	case NETDEV_UNREGISTER:
844 		if (!vlan_id)
845 			return;
846 		mutex_lock(&bnx2fc_dev_lock);
847 		list_for_each_entry_safe(interface, tmp, &if_list, list) {
848 			if (interface->hba == hba &&
849 			    interface->vlan_id == (vlan_id & VLAN_VID_MASK))
850 				__bnx2fc_destroy(interface);
851 		}
852 		mutex_unlock(&bnx2fc_dev_lock);
853 
854 		/* Ensure ALL destroy work has been completed before return */
855 		flush_workqueue(bnx2fc_wq);
856 		return;
857 
858 	default:
859 		return;
860 	}
861 
862 	mutex_lock(&bnx2fc_dev_lock);
863 	list_for_each_entry(interface, &if_list, list) {
864 
865 		if (interface->hba != hba)
866 			continue;
867 
868 		ctlr = bnx2fc_to_ctlr(interface);
869 		lport = ctlr->lp;
870 		BNX2FC_HBA_DBG(lport, "netevent handler - event=%s %ld\n",
871 				interface->netdev->name, event);
872 
873 		fcoe_link_speed_update(lport);
874 
875 		cdev = fcoe_ctlr_to_ctlr_dev(ctlr);
876 
877 		if (link_possible && !bnx2fc_link_ok(lport)) {
878 			switch (cdev->enabled) {
879 			case FCOE_CTLR_DISABLED:
880 				pr_info("Link up while interface is disabled.\n");
881 				break;
882 			case FCOE_CTLR_ENABLED:
883 			case FCOE_CTLR_UNUSED:
884 				/* Reset max recv frame size to default */
885 				fc_set_mfs(lport, BNX2FC_MFS);
886 				/*
887 				 * ctlr link up will only be handled during
888 				 * enable to avoid sending discovery
889 				 * solicitation on a stale vlan
890 				 */
891 				if (interface->enabled)
892 					fcoe_ctlr_link_up(ctlr);
893 			};
894 		} else if (fcoe_ctlr_link_down(ctlr)) {
895 			switch (cdev->enabled) {
896 			case FCOE_CTLR_DISABLED:
897 				pr_info("Link down while interface is disabled.\n");
898 				break;
899 			case FCOE_CTLR_ENABLED:
900 			case FCOE_CTLR_UNUSED:
901 				mutex_lock(&lport->lp_mutex);
902 				list_for_each_entry(vport, &lport->vports, list)
903 					fc_host_port_type(vport->host) =
904 					FC_PORTTYPE_UNKNOWN;
905 				mutex_unlock(&lport->lp_mutex);
906 				fc_host_port_type(lport->host) =
907 					FC_PORTTYPE_UNKNOWN;
908 				per_cpu_ptr(lport->stats,
909 					    get_cpu())->LinkFailureCount++;
910 				put_cpu();
911 				fcoe_clean_pending_queue(lport);
912 				wait_for_upload = 1;
913 			};
914 		}
915 	}
916 	mutex_unlock(&bnx2fc_dev_lock);
917 
918 	if (wait_for_upload) {
919 		clear_bit(ADAPTER_STATE_READY, &hba->adapter_state);
920 		init_waitqueue_head(&hba->shutdown_wait);
921 		BNX2FC_MISC_DBG("indicate_netevent "
922 				"num_ofld_sess = %d\n",
923 				hba->num_ofld_sess);
924 		hba->wait_for_link_down = 1;
925 		wait_event_interruptible(hba->shutdown_wait,
926 					 (hba->num_ofld_sess == 0));
927 		BNX2FC_MISC_DBG("wakeup - num_ofld_sess = %d\n",
928 				hba->num_ofld_sess);
929 		hba->wait_for_link_down = 0;
930 
931 		if (signal_pending(current))
932 			flush_signals(current);
933 	}
934 }
935 
936 static int bnx2fc_libfc_config(struct fc_lport *lport)
937 {
938 
939 	/* Set the function pointers set by bnx2fc driver */
940 	memcpy(&lport->tt, &bnx2fc_libfc_fcn_templ,
941 		sizeof(struct libfc_function_template));
942 	fc_elsct_init(lport);
943 	fc_exch_init(lport);
944 	fc_rport_init(lport);
945 	fc_disc_init(lport);
946 	fc_disc_config(lport, lport);
947 	return 0;
948 }
949 
950 static int bnx2fc_em_config(struct fc_lport *lport, struct bnx2fc_hba *hba)
951 {
952 	int fcoe_min_xid, fcoe_max_xid;
953 
954 	fcoe_min_xid = hba->max_xid + 1;
955 	if (nr_cpu_ids <= 2)
956 		fcoe_max_xid = hba->max_xid + FCOE_XIDS_PER_CPU_OFFSET;
957 	else
958 		fcoe_max_xid = hba->max_xid + FCOE_MAX_XID_OFFSET;
959 	if (!fc_exch_mgr_alloc(lport, FC_CLASS_3, fcoe_min_xid,
960 			       fcoe_max_xid, NULL)) {
961 		printk(KERN_ERR PFX "em_config:fc_exch_mgr_alloc failed\n");
962 		return -ENOMEM;
963 	}
964 
965 	return 0;
966 }
967 
968 static int bnx2fc_lport_config(struct fc_lport *lport)
969 {
970 	lport->link_up = 0;
971 	lport->qfull = 0;
972 	lport->max_retry_count = BNX2FC_MAX_RETRY_CNT;
973 	lport->max_rport_retry_count = BNX2FC_MAX_RPORT_RETRY_CNT;
974 	lport->e_d_tov = 2 * 1000;
975 	lport->r_a_tov = 10 * 1000;
976 
977 	lport->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
978 				FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
979 	lport->does_npiv = 1;
980 
981 	memset(&lport->rnid_gen, 0, sizeof(struct fc_els_rnid_gen));
982 	lport->rnid_gen.rnid_atype = BNX2FC_RNID_HBA;
983 
984 	/* alloc stats structure */
985 	if (fc_lport_init_stats(lport))
986 		return -ENOMEM;
987 
988 	/* Finish fc_lport configuration */
989 	fc_lport_config(lport);
990 
991 	return 0;
992 }
993 
994 /**
995  * bnx2fc_fip_recv - handle a received FIP frame.
996  *
997  * @skb: the received skb
998  * @dev: associated &net_device
999  * @ptype: the &packet_type structure which was used to register this handler.
1000  * @orig_dev: original receive &net_device, in case @ dev is a bond.
1001  *
1002  * Returns: 0 for success
1003  */
1004 static int bnx2fc_fip_recv(struct sk_buff *skb, struct net_device *dev,
1005 			   struct packet_type *ptype,
1006 			   struct net_device *orig_dev)
1007 {
1008 	struct bnx2fc_interface *interface;
1009 	struct fcoe_ctlr *ctlr;
1010 	interface = container_of(ptype, struct bnx2fc_interface,
1011 				 fip_packet_type);
1012 	ctlr = bnx2fc_to_ctlr(interface);
1013 	fcoe_ctlr_recv(ctlr, skb);
1014 	return 0;
1015 }
1016 
1017 /**
1018  * bnx2fc_update_src_mac - Update Ethernet MAC filters.
1019  *
1020  * @fip: FCoE controller.
1021  * @old: Unicast MAC address to delete if the MAC is non-zero.
1022  * @new: Unicast MAC address to add.
1023  *
1024  * Remove any previously-set unicast MAC filter.
1025  * Add secondary FCoE MAC address filter for our OUI.
1026  */
1027 static void bnx2fc_update_src_mac(struct fc_lport *lport, u8 *addr)
1028 {
1029 	struct fcoe_port *port = lport_priv(lport);
1030 
1031 	memcpy(port->data_src_addr, addr, ETH_ALEN);
1032 }
1033 
1034 /**
1035  * bnx2fc_get_src_mac - return the ethernet source address for an lport
1036  *
1037  * @lport: libfc port
1038  */
1039 static u8 *bnx2fc_get_src_mac(struct fc_lport *lport)
1040 {
1041 	struct fcoe_port *port;
1042 
1043 	port = (struct fcoe_port *)lport_priv(lport);
1044 	return port->data_src_addr;
1045 }
1046 
1047 /**
1048  * bnx2fc_fip_send - send an Ethernet-encapsulated FIP frame.
1049  *
1050  * @fip: FCoE controller.
1051  * @skb: FIP Packet.
1052  */
1053 static void bnx2fc_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
1054 {
1055 	skb->dev = bnx2fc_from_ctlr(fip)->netdev;
1056 	dev_queue_xmit(skb);
1057 }
1058 
1059 static int bnx2fc_vport_create(struct fc_vport *vport, bool disabled)
1060 {
1061 	struct Scsi_Host *shost = vport_to_shost(vport);
1062 	struct fc_lport *n_port = shost_priv(shost);
1063 	struct fcoe_port *port = lport_priv(n_port);
1064 	struct bnx2fc_interface *interface = port->priv;
1065 	struct net_device *netdev = interface->netdev;
1066 	struct fc_lport *vn_port;
1067 	int rc;
1068 	char buf[32];
1069 
1070 	rc = fcoe_validate_vport_create(vport);
1071 	if (rc) {
1072 		fcoe_wwn_to_str(vport->port_name, buf, sizeof(buf));
1073 		printk(KERN_ERR PFX "Failed to create vport, "
1074 		       "WWPN (0x%s) already exists\n",
1075 		       buf);
1076 		return rc;
1077 	}
1078 
1079 	if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &interface->hba->flags)) {
1080 		printk(KERN_ERR PFX "vn ports cannot be created on"
1081 			"this interface\n");
1082 		return -EIO;
1083 	}
1084 	rtnl_lock();
1085 	mutex_lock(&bnx2fc_dev_lock);
1086 	vn_port = bnx2fc_if_create(interface, &vport->dev, 1);
1087 	mutex_unlock(&bnx2fc_dev_lock);
1088 	rtnl_unlock();
1089 
1090 	if (!vn_port) {
1091 		printk(KERN_ERR PFX "bnx2fc_vport_create (%s) failed\n",
1092 			netdev->name);
1093 		return -EIO;
1094 	}
1095 
1096 	if (disabled) {
1097 		fc_vport_set_state(vport, FC_VPORT_DISABLED);
1098 	} else {
1099 		vn_port->boot_time = jiffies;
1100 		fc_lport_init(vn_port);
1101 		fc_fabric_login(vn_port);
1102 		fc_vport_setlink(vn_port);
1103 	}
1104 	return 0;
1105 }
1106 
1107 static void bnx2fc_free_vport(struct bnx2fc_hba *hba, struct fc_lport *lport)
1108 {
1109 	struct bnx2fc_lport *blport, *tmp;
1110 
1111 	spin_lock_bh(&hba->hba_lock);
1112 	list_for_each_entry_safe(blport, tmp, &hba->vports, list) {
1113 		if (blport->lport == lport) {
1114 			list_del(&blport->list);
1115 			kfree(blport);
1116 		}
1117 	}
1118 	spin_unlock_bh(&hba->hba_lock);
1119 }
1120 
1121 static int bnx2fc_vport_destroy(struct fc_vport *vport)
1122 {
1123 	struct Scsi_Host *shost = vport_to_shost(vport);
1124 	struct fc_lport *n_port = shost_priv(shost);
1125 	struct fc_lport *vn_port = vport->dd_data;
1126 	struct fcoe_port *port = lport_priv(vn_port);
1127 	struct bnx2fc_interface *interface = port->priv;
1128 	struct fc_lport *v_port;
1129 	bool found = false;
1130 
1131 	mutex_lock(&n_port->lp_mutex);
1132 	list_for_each_entry(v_port, &n_port->vports, list)
1133 		if (v_port->vport == vport) {
1134 			found = true;
1135 			break;
1136 		}
1137 
1138 	if (!found) {
1139 		mutex_unlock(&n_port->lp_mutex);
1140 		return -ENOENT;
1141 	}
1142 	list_del(&vn_port->list);
1143 	mutex_unlock(&n_port->lp_mutex);
1144 	bnx2fc_free_vport(interface->hba, port->lport);
1145 	bnx2fc_port_shutdown(port->lport);
1146 	bnx2fc_interface_put(interface);
1147 	queue_work(bnx2fc_wq, &port->destroy_work);
1148 	return 0;
1149 }
1150 
1151 static int bnx2fc_vport_disable(struct fc_vport *vport, bool disable)
1152 {
1153 	struct fc_lport *lport = vport->dd_data;
1154 
1155 	if (disable) {
1156 		fc_vport_set_state(vport, FC_VPORT_DISABLED);
1157 		fc_fabric_logoff(lport);
1158 	} else {
1159 		lport->boot_time = jiffies;
1160 		fc_fabric_login(lport);
1161 		fc_vport_setlink(lport);
1162 	}
1163 	return 0;
1164 }
1165 
1166 
1167 static int bnx2fc_interface_setup(struct bnx2fc_interface *interface)
1168 {
1169 	struct net_device *netdev = interface->netdev;
1170 	struct net_device *physdev = interface->hba->phys_dev;
1171 	struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
1172 	struct netdev_hw_addr *ha;
1173 	int sel_san_mac = 0;
1174 
1175 	/* setup Source MAC Address */
1176 	rcu_read_lock();
1177 	for_each_dev_addr(physdev, ha) {
1178 		BNX2FC_MISC_DBG("net_config: ha->type = %d, fip_mac = ",
1179 				ha->type);
1180 		printk(KERN_INFO "%2x:%2x:%2x:%2x:%2x:%2x\n", ha->addr[0],
1181 				ha->addr[1], ha->addr[2], ha->addr[3],
1182 				ha->addr[4], ha->addr[5]);
1183 
1184 		if ((ha->type == NETDEV_HW_ADDR_T_SAN) &&
1185 		    (is_valid_ether_addr(ha->addr))) {
1186 			memcpy(ctlr->ctl_src_addr, ha->addr,
1187 			       ETH_ALEN);
1188 			sel_san_mac = 1;
1189 			BNX2FC_MISC_DBG("Found SAN MAC\n");
1190 		}
1191 	}
1192 	rcu_read_unlock();
1193 
1194 	if (!sel_san_mac)
1195 		return -ENODEV;
1196 
1197 	interface->fip_packet_type.func = bnx2fc_fip_recv;
1198 	interface->fip_packet_type.type = htons(ETH_P_FIP);
1199 	interface->fip_packet_type.dev = netdev;
1200 	dev_add_pack(&interface->fip_packet_type);
1201 
1202 	interface->fcoe_packet_type.func = bnx2fc_rcv;
1203 	interface->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
1204 	interface->fcoe_packet_type.dev = netdev;
1205 	dev_add_pack(&interface->fcoe_packet_type);
1206 
1207 	return 0;
1208 }
1209 
1210 static int bnx2fc_attach_transport(void)
1211 {
1212 	bnx2fc_transport_template =
1213 		fc_attach_transport(&bnx2fc_transport_function);
1214 
1215 	if (bnx2fc_transport_template == NULL) {
1216 		printk(KERN_ERR PFX "Failed to attach FC transport\n");
1217 		return -ENODEV;
1218 	}
1219 
1220 	bnx2fc_vport_xport_template =
1221 		fc_attach_transport(&bnx2fc_vport_xport_function);
1222 	if (bnx2fc_vport_xport_template == NULL) {
1223 		printk(KERN_ERR PFX
1224 		       "Failed to attach FC transport for vport\n");
1225 		fc_release_transport(bnx2fc_transport_template);
1226 		bnx2fc_transport_template = NULL;
1227 		return -ENODEV;
1228 	}
1229 	return 0;
1230 }
1231 static void bnx2fc_release_transport(void)
1232 {
1233 	fc_release_transport(bnx2fc_transport_template);
1234 	fc_release_transport(bnx2fc_vport_xport_template);
1235 	bnx2fc_transport_template = NULL;
1236 	bnx2fc_vport_xport_template = NULL;
1237 }
1238 
1239 static void bnx2fc_interface_release(struct kref *kref)
1240 {
1241 	struct fcoe_ctlr_device *ctlr_dev;
1242 	struct bnx2fc_interface *interface;
1243 	struct fcoe_ctlr *ctlr;
1244 	struct net_device *netdev;
1245 
1246 	interface = container_of(kref, struct bnx2fc_interface, kref);
1247 	BNX2FC_MISC_DBG("Interface is being released\n");
1248 
1249 	ctlr = bnx2fc_to_ctlr(interface);
1250 	ctlr_dev = fcoe_ctlr_to_ctlr_dev(ctlr);
1251 	netdev = interface->netdev;
1252 
1253 	/* tear-down FIP controller */
1254 	if (test_and_clear_bit(BNX2FC_CTLR_INIT_DONE, &interface->if_flags))
1255 		fcoe_ctlr_destroy(ctlr);
1256 
1257 	fcoe_ctlr_device_delete(ctlr_dev);
1258 
1259 	dev_put(netdev);
1260 	module_put(THIS_MODULE);
1261 }
1262 
1263 static inline void bnx2fc_interface_get(struct bnx2fc_interface *interface)
1264 {
1265 	kref_get(&interface->kref);
1266 }
1267 
1268 static inline void bnx2fc_interface_put(struct bnx2fc_interface *interface)
1269 {
1270 	kref_put(&interface->kref, bnx2fc_interface_release);
1271 }
1272 static void bnx2fc_hba_destroy(struct bnx2fc_hba *hba)
1273 {
1274 	/* Free the command manager */
1275 	if (hba->cmd_mgr) {
1276 		bnx2fc_cmd_mgr_free(hba->cmd_mgr);
1277 		hba->cmd_mgr = NULL;
1278 	}
1279 	kfree(hba->tgt_ofld_list);
1280 	bnx2fc_unbind_pcidev(hba);
1281 	kfree(hba);
1282 }
1283 
1284 /**
1285  * bnx2fc_hba_create - create a new bnx2fc hba
1286  *
1287  * @cnic:	pointer to cnic device
1288  *
1289  * Creates a new FCoE hba on the given device.
1290  *
1291  */
1292 static struct bnx2fc_hba *bnx2fc_hba_create(struct cnic_dev *cnic)
1293 {
1294 	struct bnx2fc_hba *hba;
1295 	struct fcoe_capabilities *fcoe_cap;
1296 	int rc;
1297 
1298 	hba = kzalloc(sizeof(*hba), GFP_KERNEL);
1299 	if (!hba) {
1300 		printk(KERN_ERR PFX "Unable to allocate hba structure\n");
1301 		return NULL;
1302 	}
1303 	spin_lock_init(&hba->hba_lock);
1304 	mutex_init(&hba->hba_mutex);
1305 
1306 	hba->cnic = cnic;
1307 
1308 	hba->max_tasks = cnic->max_fcoe_exchanges;
1309 	hba->elstm_xids = (hba->max_tasks / 2);
1310 	hba->max_outstanding_cmds = hba->elstm_xids;
1311 	hba->max_xid = (hba->max_tasks - 1);
1312 
1313 	rc = bnx2fc_bind_pcidev(hba);
1314 	if (rc) {
1315 		printk(KERN_ERR PFX "create_adapter:  bind error\n");
1316 		goto bind_err;
1317 	}
1318 	hba->phys_dev = cnic->netdev;
1319 	hba->next_conn_id = 0;
1320 
1321 	hba->tgt_ofld_list =
1322 		kzalloc(sizeof(struct bnx2fc_rport *) * BNX2FC_NUM_MAX_SESS,
1323 			GFP_KERNEL);
1324 	if (!hba->tgt_ofld_list) {
1325 		printk(KERN_ERR PFX "Unable to allocate tgt offload list\n");
1326 		goto tgtofld_err;
1327 	}
1328 
1329 	hba->num_ofld_sess = 0;
1330 
1331 	hba->cmd_mgr = bnx2fc_cmd_mgr_alloc(hba);
1332 	if (!hba->cmd_mgr) {
1333 		printk(KERN_ERR PFX "em_config:bnx2fc_cmd_mgr_alloc failed\n");
1334 		goto cmgr_err;
1335 	}
1336 	fcoe_cap = &hba->fcoe_cap;
1337 
1338 	fcoe_cap->capability1 = BNX2FC_TM_MAX_SQES <<
1339 					FCOE_IOS_PER_CONNECTION_SHIFT;
1340 	fcoe_cap->capability1 |= BNX2FC_NUM_MAX_SESS <<
1341 					FCOE_LOGINS_PER_PORT_SHIFT;
1342 	fcoe_cap->capability2 = hba->max_outstanding_cmds <<
1343 					FCOE_NUMBER_OF_EXCHANGES_SHIFT;
1344 	fcoe_cap->capability2 |= BNX2FC_MAX_NPIV <<
1345 					FCOE_NPIV_WWN_PER_PORT_SHIFT;
1346 	fcoe_cap->capability3 = BNX2FC_NUM_MAX_SESS <<
1347 					FCOE_TARGETS_SUPPORTED_SHIFT;
1348 	fcoe_cap->capability3 |= hba->max_outstanding_cmds <<
1349 					FCOE_OUTSTANDING_COMMANDS_SHIFT;
1350 	fcoe_cap->capability4 = FCOE_CAPABILITY4_STATEFUL;
1351 
1352 	init_waitqueue_head(&hba->shutdown_wait);
1353 	init_waitqueue_head(&hba->destroy_wait);
1354 	INIT_LIST_HEAD(&hba->vports);
1355 
1356 	return hba;
1357 
1358 cmgr_err:
1359 	kfree(hba->tgt_ofld_list);
1360 tgtofld_err:
1361 	bnx2fc_unbind_pcidev(hba);
1362 bind_err:
1363 	kfree(hba);
1364 	return NULL;
1365 }
1366 
1367 struct bnx2fc_interface *bnx2fc_interface_create(struct bnx2fc_hba *hba,
1368 				      struct net_device *netdev,
1369 				      enum fip_state fip_mode)
1370 {
1371 	struct fcoe_ctlr_device *ctlr_dev;
1372 	struct bnx2fc_interface *interface;
1373 	struct fcoe_ctlr *ctlr;
1374 	int size;
1375 	int rc = 0;
1376 
1377 	size = (sizeof(*interface) + sizeof(struct fcoe_ctlr));
1378 	ctlr_dev = fcoe_ctlr_device_add(&netdev->dev, &bnx2fc_fcoe_sysfs_templ,
1379 					 size);
1380 	if (!ctlr_dev) {
1381 		printk(KERN_ERR PFX "Unable to allocate interface structure\n");
1382 		return NULL;
1383 	}
1384 	ctlr = fcoe_ctlr_device_priv(ctlr_dev);
1385 	ctlr->cdev = ctlr_dev;
1386 	interface = fcoe_ctlr_priv(ctlr);
1387 	dev_hold(netdev);
1388 	kref_init(&interface->kref);
1389 	interface->hba = hba;
1390 	interface->netdev = netdev;
1391 
1392 	/* Initialize FIP */
1393 	fcoe_ctlr_init(ctlr, fip_mode);
1394 	ctlr->send = bnx2fc_fip_send;
1395 	ctlr->update_mac = bnx2fc_update_src_mac;
1396 	ctlr->get_src_addr = bnx2fc_get_src_mac;
1397 	set_bit(BNX2FC_CTLR_INIT_DONE, &interface->if_flags);
1398 
1399 	rc = bnx2fc_interface_setup(interface);
1400 	if (!rc)
1401 		return interface;
1402 
1403 	fcoe_ctlr_destroy(ctlr);
1404 	dev_put(netdev);
1405 	fcoe_ctlr_device_delete(ctlr_dev);
1406 	return NULL;
1407 }
1408 
1409 /**
1410  * bnx2fc_if_create - Create FCoE instance on a given interface
1411  *
1412  * @interface:	FCoE interface to create a local port on
1413  * @parent:	Device pointer to be the parent in sysfs for the SCSI host
1414  * @npiv:	Indicates if the port is vport or not
1415  *
1416  * Creates a fc_lport instance and a Scsi_Host instance and configure them.
1417  *
1418  * Returns:	Allocated fc_lport or an error pointer
1419  */
1420 static struct fc_lport *bnx2fc_if_create(struct bnx2fc_interface *interface,
1421 				  struct device *parent, int npiv)
1422 {
1423 	struct fcoe_ctlr        *ctlr = bnx2fc_to_ctlr(interface);
1424 	struct fc_lport		*lport, *n_port;
1425 	struct fcoe_port	*port;
1426 	struct Scsi_Host	*shost;
1427 	struct fc_vport		*vport = dev_to_vport(parent);
1428 	struct bnx2fc_lport	*blport;
1429 	struct bnx2fc_hba	*hba = interface->hba;
1430 	int			rc = 0;
1431 
1432 	blport = kzalloc(sizeof(struct bnx2fc_lport), GFP_KERNEL);
1433 	if (!blport) {
1434 		BNX2FC_HBA_DBG(ctlr->lp, "Unable to alloc blport\n");
1435 		return NULL;
1436 	}
1437 
1438 	/* Allocate Scsi_Host structure */
1439 	bnx2fc_shost_template.can_queue = hba->max_outstanding_cmds;
1440 	if (!npiv)
1441 		lport = libfc_host_alloc(&bnx2fc_shost_template, sizeof(*port));
1442 	else
1443 		lport = libfc_vport_create(vport, sizeof(*port));
1444 
1445 	if (!lport) {
1446 		printk(KERN_ERR PFX "could not allocate scsi host structure\n");
1447 		goto free_blport;
1448 	}
1449 	shost = lport->host;
1450 	port = lport_priv(lport);
1451 	port->lport = lport;
1452 	port->priv = interface;
1453 	port->get_netdev = bnx2fc_netdev;
1454 	INIT_WORK(&port->destroy_work, bnx2fc_destroy_work);
1455 
1456 	/* Configure fcoe_port */
1457 	rc = bnx2fc_lport_config(lport);
1458 	if (rc)
1459 		goto lp_config_err;
1460 
1461 	if (npiv) {
1462 		printk(KERN_ERR PFX "Setting vport names, 0x%llX 0x%llX\n",
1463 			vport->node_name, vport->port_name);
1464 		fc_set_wwnn(lport, vport->node_name);
1465 		fc_set_wwpn(lport, vport->port_name);
1466 	}
1467 	/* Configure netdev and networking properties of the lport */
1468 	rc = bnx2fc_net_config(lport, interface->netdev);
1469 	if (rc) {
1470 		printk(KERN_ERR PFX "Error on bnx2fc_net_config\n");
1471 		goto lp_config_err;
1472 	}
1473 
1474 	rc = bnx2fc_shost_config(lport, parent);
1475 	if (rc) {
1476 		printk(KERN_ERR PFX "Couldnt configure shost for %s\n",
1477 			interface->netdev->name);
1478 		goto lp_config_err;
1479 	}
1480 
1481 	/* Initialize the libfc library */
1482 	rc = bnx2fc_libfc_config(lport);
1483 	if (rc) {
1484 		printk(KERN_ERR PFX "Couldnt configure libfc\n");
1485 		goto shost_err;
1486 	}
1487 	fc_host_port_type(lport->host) = FC_PORTTYPE_UNKNOWN;
1488 
1489 	/* Allocate exchange manager */
1490 	if (!npiv)
1491 		rc = bnx2fc_em_config(lport, hba);
1492 	else {
1493 		shost = vport_to_shost(vport);
1494 		n_port = shost_priv(shost);
1495 		rc = fc_exch_mgr_list_clone(n_port, lport);
1496 	}
1497 
1498 	if (rc) {
1499 		printk(KERN_ERR PFX "Error on bnx2fc_em_config\n");
1500 		goto shost_err;
1501 	}
1502 
1503 	bnx2fc_interface_get(interface);
1504 
1505 	spin_lock_bh(&hba->hba_lock);
1506 	blport->lport = lport;
1507 	list_add_tail(&blport->list, &hba->vports);
1508 	spin_unlock_bh(&hba->hba_lock);
1509 
1510 	return lport;
1511 
1512 shost_err:
1513 	scsi_remove_host(shost);
1514 lp_config_err:
1515 	scsi_host_put(lport->host);
1516 free_blport:
1517 	kfree(blport);
1518 	return NULL;
1519 }
1520 
1521 static void bnx2fc_net_cleanup(struct bnx2fc_interface *interface)
1522 {
1523 	/* Dont listen for Ethernet packets anymore */
1524 	__dev_remove_pack(&interface->fcoe_packet_type);
1525 	__dev_remove_pack(&interface->fip_packet_type);
1526 	synchronize_net();
1527 }
1528 
1529 static void bnx2fc_interface_cleanup(struct bnx2fc_interface *interface)
1530 {
1531 	struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
1532 	struct fc_lport *lport = ctlr->lp;
1533 	struct fcoe_port *port = lport_priv(lport);
1534 	struct bnx2fc_hba *hba = interface->hba;
1535 
1536 	/* Stop the transmit retry timer */
1537 	del_timer_sync(&port->timer);
1538 
1539 	/* Free existing transmit skbs */
1540 	fcoe_clean_pending_queue(lport);
1541 
1542 	bnx2fc_net_cleanup(interface);
1543 
1544 	bnx2fc_free_vport(hba, lport);
1545 }
1546 
1547 static void bnx2fc_if_destroy(struct fc_lport *lport)
1548 {
1549 
1550 	/* Free queued packets for the receive thread */
1551 	bnx2fc_clean_rx_queue(lport);
1552 
1553 	/* Detach from scsi-ml */
1554 	fc_remove_host(lport->host);
1555 	scsi_remove_host(lport->host);
1556 
1557 	/*
1558 	 * Note that only the physical lport will have the exchange manager.
1559 	 * for vports, this function is NOP
1560 	 */
1561 	fc_exch_mgr_free(lport);
1562 
1563 	/* Free memory used by statistical counters */
1564 	fc_lport_free_stats(lport);
1565 
1566 	/* Release Scsi_Host */
1567 	scsi_host_put(lport->host);
1568 }
1569 
1570 static void __bnx2fc_destroy(struct bnx2fc_interface *interface)
1571 {
1572 	struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
1573 	struct fc_lport *lport = ctlr->lp;
1574 	struct fcoe_port *port = lport_priv(lport);
1575 
1576 	bnx2fc_interface_cleanup(interface);
1577 	bnx2fc_stop(interface);
1578 	list_del(&interface->list);
1579 	bnx2fc_interface_put(interface);
1580 	queue_work(bnx2fc_wq, &port->destroy_work);
1581 }
1582 
1583 /**
1584  * bnx2fc_destroy - Destroy a bnx2fc FCoE interface
1585  *
1586  * @buffer: The name of the Ethernet interface to be destroyed
1587  * @kp:     The associated kernel parameter
1588  *
1589  * Called from sysfs.
1590  *
1591  * Returns: 0 for success
1592  */
1593 static int bnx2fc_destroy(struct net_device *netdev)
1594 {
1595 	struct bnx2fc_interface *interface = NULL;
1596 	struct workqueue_struct *timer_work_queue;
1597 	struct fcoe_ctlr *ctlr;
1598 	int rc = 0;
1599 
1600 	rtnl_lock();
1601 	mutex_lock(&bnx2fc_dev_lock);
1602 
1603 	interface = bnx2fc_interface_lookup(netdev);
1604 	ctlr = bnx2fc_to_ctlr(interface);
1605 	if (!interface || !ctlr->lp) {
1606 		rc = -ENODEV;
1607 		printk(KERN_ERR PFX "bnx2fc_destroy: interface or lport not found\n");
1608 		goto netdev_err;
1609 	}
1610 
1611 	timer_work_queue = interface->timer_work_queue;
1612 	__bnx2fc_destroy(interface);
1613 	destroy_workqueue(timer_work_queue);
1614 
1615 netdev_err:
1616 	mutex_unlock(&bnx2fc_dev_lock);
1617 	rtnl_unlock();
1618 	return rc;
1619 }
1620 
1621 static void bnx2fc_destroy_work(struct work_struct *work)
1622 {
1623 	struct fcoe_port *port;
1624 	struct fc_lport *lport;
1625 
1626 	port = container_of(work, struct fcoe_port, destroy_work);
1627 	lport = port->lport;
1628 
1629 	BNX2FC_HBA_DBG(lport, "Entered bnx2fc_destroy_work\n");
1630 
1631 	bnx2fc_if_destroy(lport);
1632 }
1633 
1634 static void bnx2fc_unbind_adapter_devices(struct bnx2fc_hba *hba)
1635 {
1636 	bnx2fc_free_fw_resc(hba);
1637 	bnx2fc_free_task_ctx(hba);
1638 }
1639 
1640 /**
1641  * bnx2fc_bind_adapter_devices - binds bnx2fc adapter with the associated
1642  *			pci structure
1643  *
1644  * @hba:		Adapter instance
1645  */
1646 static int bnx2fc_bind_adapter_devices(struct bnx2fc_hba *hba)
1647 {
1648 	if (bnx2fc_setup_task_ctx(hba))
1649 		goto mem_err;
1650 
1651 	if (bnx2fc_setup_fw_resc(hba))
1652 		goto mem_err;
1653 
1654 	return 0;
1655 mem_err:
1656 	bnx2fc_unbind_adapter_devices(hba);
1657 	return -ENOMEM;
1658 }
1659 
1660 static int bnx2fc_bind_pcidev(struct bnx2fc_hba *hba)
1661 {
1662 	struct cnic_dev *cnic;
1663 	struct pci_dev *pdev;
1664 
1665 	if (!hba->cnic) {
1666 		printk(KERN_ERR PFX "cnic is NULL\n");
1667 		return -ENODEV;
1668 	}
1669 	cnic = hba->cnic;
1670 	pdev = hba->pcidev = cnic->pcidev;
1671 	if (!hba->pcidev)
1672 		return -ENODEV;
1673 
1674 	switch (pdev->device) {
1675 	case PCI_DEVICE_ID_NX2_57710:
1676 		strncpy(hba->chip_num, "BCM57710", BCM_CHIP_LEN);
1677 		break;
1678 	case PCI_DEVICE_ID_NX2_57711:
1679 		strncpy(hba->chip_num, "BCM57711", BCM_CHIP_LEN);
1680 		break;
1681 	case PCI_DEVICE_ID_NX2_57712:
1682 	case PCI_DEVICE_ID_NX2_57712_MF:
1683 	case PCI_DEVICE_ID_NX2_57712_VF:
1684 		strncpy(hba->chip_num, "BCM57712", BCM_CHIP_LEN);
1685 		break;
1686 	case PCI_DEVICE_ID_NX2_57800:
1687 	case PCI_DEVICE_ID_NX2_57800_MF:
1688 	case PCI_DEVICE_ID_NX2_57800_VF:
1689 		strncpy(hba->chip_num, "BCM57800", BCM_CHIP_LEN);
1690 		break;
1691 	case PCI_DEVICE_ID_NX2_57810:
1692 	case PCI_DEVICE_ID_NX2_57810_MF:
1693 	case PCI_DEVICE_ID_NX2_57810_VF:
1694 		strncpy(hba->chip_num, "BCM57810", BCM_CHIP_LEN);
1695 		break;
1696 	case PCI_DEVICE_ID_NX2_57840:
1697 	case PCI_DEVICE_ID_NX2_57840_MF:
1698 	case PCI_DEVICE_ID_NX2_57840_VF:
1699 	case PCI_DEVICE_ID_NX2_57840_2_20:
1700 	case PCI_DEVICE_ID_NX2_57840_4_10:
1701 		strncpy(hba->chip_num, "BCM57840", BCM_CHIP_LEN);
1702 		break;
1703 	default:
1704 		pr_err(PFX "Unknown device id 0x%x\n", pdev->device);
1705 		break;
1706 	}
1707 	pci_dev_get(hba->pcidev);
1708 	return 0;
1709 }
1710 
1711 static void bnx2fc_unbind_pcidev(struct bnx2fc_hba *hba)
1712 {
1713 	if (hba->pcidev) {
1714 		hba->chip_num[0] = '\0';
1715 		pci_dev_put(hba->pcidev);
1716 	}
1717 	hba->pcidev = NULL;
1718 }
1719 
1720 /**
1721  * bnx2fc_ulp_get_stats - cnic callback to populate FCoE stats
1722  *
1723  * @handle:    transport handle pointing to adapter struture
1724  */
1725 static int bnx2fc_ulp_get_stats(void *handle)
1726 {
1727 	struct bnx2fc_hba *hba = handle;
1728 	struct cnic_dev *cnic;
1729 	struct fcoe_stats_info *stats_addr;
1730 
1731 	if (!hba)
1732 		return -EINVAL;
1733 
1734 	cnic = hba->cnic;
1735 	stats_addr = &cnic->stats_addr->fcoe_stat;
1736 	if (!stats_addr)
1737 		return -EINVAL;
1738 
1739 	strncpy(stats_addr->version, BNX2FC_VERSION,
1740 		sizeof(stats_addr->version));
1741 	stats_addr->txq_size = BNX2FC_SQ_WQES_MAX;
1742 	stats_addr->rxq_size = BNX2FC_CQ_WQES_MAX;
1743 
1744 	return 0;
1745 }
1746 
1747 
1748 /**
1749  * bnx2fc_ulp_start - cnic callback to initialize & start adapter instance
1750  *
1751  * @handle:	transport handle pointing to adapter structure
1752  *
1753  * This function maps adapter structure to pcidev structure and initiates
1754  *	firmware handshake to enable/initialize on-chip FCoE components.
1755  *	This bnx2fc - cnic interface api callback is used after following
1756  *	conditions are met -
1757  *	a) underlying network interface is up (marked by event NETDEV_UP
1758  *		from netdev
1759  *	b) bnx2fc adatper structure is registered.
1760  */
1761 static void bnx2fc_ulp_start(void *handle)
1762 {
1763 	struct bnx2fc_hba *hba = handle;
1764 	struct bnx2fc_interface *interface;
1765 	struct fcoe_ctlr *ctlr;
1766 	struct fc_lport *lport;
1767 
1768 	mutex_lock(&bnx2fc_dev_lock);
1769 
1770 	if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags))
1771 		bnx2fc_fw_init(hba);
1772 
1773 	BNX2FC_MISC_DBG("bnx2fc started.\n");
1774 
1775 	list_for_each_entry(interface, &if_list, list) {
1776 		if (interface->hba == hba) {
1777 			ctlr = bnx2fc_to_ctlr(interface);
1778 			lport = ctlr->lp;
1779 			/* Kick off Fabric discovery*/
1780 			printk(KERN_ERR PFX "ulp_init: start discovery\n");
1781 			lport->tt.frame_send = bnx2fc_xmit;
1782 			bnx2fc_start_disc(interface);
1783 		}
1784 	}
1785 
1786 	mutex_unlock(&bnx2fc_dev_lock);
1787 }
1788 
1789 static void bnx2fc_port_shutdown(struct fc_lport *lport)
1790 {
1791 	BNX2FC_MISC_DBG("Entered %s\n", __func__);
1792 	fc_fabric_logoff(lport);
1793 	fc_lport_destroy(lport);
1794 }
1795 
1796 static void bnx2fc_stop(struct bnx2fc_interface *interface)
1797 {
1798 	struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
1799 	struct fc_lport *lport;
1800 	struct fc_lport *vport;
1801 
1802 	if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &interface->hba->flags))
1803 		return;
1804 
1805 	lport = ctlr->lp;
1806 	bnx2fc_port_shutdown(lport);
1807 
1808 	mutex_lock(&lport->lp_mutex);
1809 	list_for_each_entry(vport, &lport->vports, list)
1810 		fc_host_port_type(vport->host) =
1811 					FC_PORTTYPE_UNKNOWN;
1812 	mutex_unlock(&lport->lp_mutex);
1813 	fc_host_port_type(lport->host) = FC_PORTTYPE_UNKNOWN;
1814 	fcoe_ctlr_link_down(ctlr);
1815 	fcoe_clean_pending_queue(lport);
1816 }
1817 
1818 static int bnx2fc_fw_init(struct bnx2fc_hba *hba)
1819 {
1820 #define BNX2FC_INIT_POLL_TIME		(1000 / HZ)
1821 	int rc = -1;
1822 	int i = HZ;
1823 
1824 	rc = bnx2fc_bind_adapter_devices(hba);
1825 	if (rc) {
1826 		printk(KERN_ALERT PFX
1827 			"bnx2fc_bind_adapter_devices failed - rc = %d\n", rc);
1828 		goto err_out;
1829 	}
1830 
1831 	rc = bnx2fc_send_fw_fcoe_init_msg(hba);
1832 	if (rc) {
1833 		printk(KERN_ALERT PFX
1834 			"bnx2fc_send_fw_fcoe_init_msg failed - rc = %d\n", rc);
1835 		goto err_unbind;
1836 	}
1837 
1838 	/*
1839 	 * Wait until the adapter init message is complete, and adapter
1840 	 * state is UP.
1841 	 */
1842 	while (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state) && i--)
1843 		msleep(BNX2FC_INIT_POLL_TIME);
1844 
1845 	if (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state)) {
1846 		printk(KERN_ERR PFX "bnx2fc_start: %s failed to initialize.  "
1847 				"Ignoring...\n",
1848 				hba->cnic->netdev->name);
1849 		rc = -1;
1850 		goto err_unbind;
1851 	}
1852 
1853 
1854 	set_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags);
1855 	return 0;
1856 
1857 err_unbind:
1858 	bnx2fc_unbind_adapter_devices(hba);
1859 err_out:
1860 	return rc;
1861 }
1862 
1863 static void bnx2fc_fw_destroy(struct bnx2fc_hba *hba)
1864 {
1865 	if (test_and_clear_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags)) {
1866 		if (bnx2fc_send_fw_fcoe_destroy_msg(hba) == 0) {
1867 			init_timer(&hba->destroy_timer);
1868 			hba->destroy_timer.expires = BNX2FC_FW_TIMEOUT +
1869 								jiffies;
1870 			hba->destroy_timer.function = bnx2fc_destroy_timer;
1871 			hba->destroy_timer.data = (unsigned long)hba;
1872 			add_timer(&hba->destroy_timer);
1873 			wait_event_interruptible(hba->destroy_wait,
1874 					test_bit(BNX2FC_FLAG_DESTROY_CMPL,
1875 						 &hba->flags));
1876 			clear_bit(BNX2FC_FLAG_DESTROY_CMPL, &hba->flags);
1877 			/* This should never happen */
1878 			if (signal_pending(current))
1879 				flush_signals(current);
1880 
1881 			del_timer_sync(&hba->destroy_timer);
1882 		}
1883 		bnx2fc_unbind_adapter_devices(hba);
1884 	}
1885 }
1886 
1887 /**
1888  * bnx2fc_ulp_stop - cnic callback to shutdown adapter instance
1889  *
1890  * @handle:	transport handle pointing to adapter structure
1891  *
1892  * Driver checks if adapter is already in shutdown mode, if not start
1893  *	the shutdown process.
1894  */
1895 static void bnx2fc_ulp_stop(void *handle)
1896 {
1897 	struct bnx2fc_hba *hba = handle;
1898 	struct bnx2fc_interface *interface;
1899 
1900 	printk(KERN_ERR "ULP_STOP\n");
1901 
1902 	mutex_lock(&bnx2fc_dev_lock);
1903 	if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags))
1904 		goto exit;
1905 	list_for_each_entry(interface, &if_list, list) {
1906 		if (interface->hba == hba)
1907 			bnx2fc_stop(interface);
1908 	}
1909 	BUG_ON(hba->num_ofld_sess != 0);
1910 
1911 	mutex_lock(&hba->hba_mutex);
1912 	clear_bit(ADAPTER_STATE_UP, &hba->adapter_state);
1913 	clear_bit(ADAPTER_STATE_GOING_DOWN,
1914 		  &hba->adapter_state);
1915 
1916 	clear_bit(ADAPTER_STATE_READY, &hba->adapter_state);
1917 	mutex_unlock(&hba->hba_mutex);
1918 
1919 	bnx2fc_fw_destroy(hba);
1920 exit:
1921 	mutex_unlock(&bnx2fc_dev_lock);
1922 }
1923 
1924 static void bnx2fc_start_disc(struct bnx2fc_interface *interface)
1925 {
1926 	struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
1927 	struct fc_lport *lport;
1928 	int wait_cnt = 0;
1929 
1930 	BNX2FC_MISC_DBG("Entered %s\n", __func__);
1931 	/* Kick off FIP/FLOGI */
1932 	if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &interface->hba->flags)) {
1933 		printk(KERN_ERR PFX "Init not done yet\n");
1934 		return;
1935 	}
1936 
1937 	lport = ctlr->lp;
1938 	BNX2FC_HBA_DBG(lport, "calling fc_fabric_login\n");
1939 
1940 	if (!bnx2fc_link_ok(lport) && interface->enabled) {
1941 		BNX2FC_HBA_DBG(lport, "ctlr_link_up\n");
1942 		fcoe_ctlr_link_up(ctlr);
1943 		fc_host_port_type(lport->host) = FC_PORTTYPE_NPORT;
1944 		set_bit(ADAPTER_STATE_READY, &interface->hba->adapter_state);
1945 	}
1946 
1947 	/* wait for the FCF to be selected before issuing FLOGI */
1948 	while (!ctlr->sel_fcf) {
1949 		msleep(250);
1950 		/* give up after 3 secs */
1951 		if (++wait_cnt > 12)
1952 			break;
1953 	}
1954 
1955 	/* Reset max receive frame size to default */
1956 	if (fc_set_mfs(lport, BNX2FC_MFS))
1957 		return;
1958 
1959 	fc_lport_init(lport);
1960 	fc_fabric_login(lport);
1961 }
1962 
1963 
1964 /**
1965  * bnx2fc_ulp_init - Initialize an adapter instance
1966  *
1967  * @dev :	cnic device handle
1968  * Called from cnic_register_driver() context to initialize all
1969  *	enumerated cnic devices. This routine allocates adapter structure
1970  *	and other device specific resources.
1971  */
1972 static void bnx2fc_ulp_init(struct cnic_dev *dev)
1973 {
1974 	struct bnx2fc_hba *hba;
1975 	int rc = 0;
1976 
1977 	BNX2FC_MISC_DBG("Entered %s\n", __func__);
1978 	/* bnx2fc works only when bnx2x is loaded */
1979 	if (!test_bit(CNIC_F_BNX2X_CLASS, &dev->flags) ||
1980 	    (dev->max_fcoe_conn == 0)) {
1981 		printk(KERN_ERR PFX "bnx2fc FCoE not supported on %s,"
1982 				    " flags: %lx fcoe_conn: %d\n",
1983 			dev->netdev->name, dev->flags, dev->max_fcoe_conn);
1984 		return;
1985 	}
1986 
1987 	hba = bnx2fc_hba_create(dev);
1988 	if (!hba) {
1989 		printk(KERN_ERR PFX "hba initialization failed\n");
1990 		return;
1991 	}
1992 
1993 	/* Add HBA to the adapter list */
1994 	mutex_lock(&bnx2fc_dev_lock);
1995 	list_add_tail(&hba->list, &adapter_list);
1996 	adapter_count++;
1997 	mutex_unlock(&bnx2fc_dev_lock);
1998 
1999 	dev->fcoe_cap = &hba->fcoe_cap;
2000 	clear_bit(BNX2FC_CNIC_REGISTERED, &hba->reg_with_cnic);
2001 	rc = dev->register_device(dev, CNIC_ULP_FCOE,
2002 						(void *) hba);
2003 	if (rc)
2004 		printk(KERN_ERR PFX "register_device failed, rc = %d\n", rc);
2005 	else
2006 		set_bit(BNX2FC_CNIC_REGISTERED, &hba->reg_with_cnic);
2007 }
2008 
2009 /* Assumes rtnl_lock and the bnx2fc_dev_lock are already taken */
2010 static int __bnx2fc_disable(struct fcoe_ctlr *ctlr)
2011 {
2012 	struct bnx2fc_interface *interface = fcoe_ctlr_priv(ctlr);
2013 
2014 	if (interface->enabled == true) {
2015 		if (!ctlr->lp) {
2016 			pr_err(PFX "__bnx2fc_disable: lport not found\n");
2017 			return -ENODEV;
2018 		} else {
2019 			interface->enabled = false;
2020 			fcoe_ctlr_link_down(ctlr);
2021 			fcoe_clean_pending_queue(ctlr->lp);
2022 		}
2023 	}
2024 	return 0;
2025 }
2026 
2027 /**
2028  * Deperecated: Use bnx2fc_enabled()
2029  */
2030 static int bnx2fc_disable(struct net_device *netdev)
2031 {
2032 	struct bnx2fc_interface *interface;
2033 	struct fcoe_ctlr *ctlr;
2034 	int rc = 0;
2035 
2036 	rtnl_lock();
2037 	mutex_lock(&bnx2fc_dev_lock);
2038 
2039 	interface = bnx2fc_interface_lookup(netdev);
2040 	ctlr = bnx2fc_to_ctlr(interface);
2041 
2042 	if (!interface) {
2043 		rc = -ENODEV;
2044 		pr_err(PFX "bnx2fc_disable: interface not found\n");
2045 	} else {
2046 		rc = __bnx2fc_disable(ctlr);
2047 	}
2048 	mutex_unlock(&bnx2fc_dev_lock);
2049 	rtnl_unlock();
2050 	return rc;
2051 }
2052 
2053 static uint bnx2fc_npiv_create_vports(struct fc_lport *lport,
2054 				      struct cnic_fc_npiv_tbl *npiv_tbl)
2055 {
2056 	struct fc_vport_identifiers vpid;
2057 	uint i, created = 0;
2058 
2059 	if (npiv_tbl->count > MAX_NPIV_ENTRIES) {
2060 		BNX2FC_HBA_DBG(lport, "Exceeded count max of npiv table\n");
2061 		goto done;
2062 	}
2063 
2064 	/* Sanity check the first entry to make sure it's not 0 */
2065 	if (wwn_to_u64(npiv_tbl->wwnn[0]) == 0 &&
2066 	    wwn_to_u64(npiv_tbl->wwpn[0]) == 0) {
2067 		BNX2FC_HBA_DBG(lport, "First NPIV table entries invalid.\n");
2068 		goto done;
2069 	}
2070 
2071 	vpid.roles = FC_PORT_ROLE_FCP_INITIATOR;
2072 	vpid.vport_type = FC_PORTTYPE_NPIV;
2073 	vpid.disable = false;
2074 
2075 	for (i = 0; i < npiv_tbl->count; i++) {
2076 		vpid.node_name = wwn_to_u64(npiv_tbl->wwnn[i]);
2077 		vpid.port_name = wwn_to_u64(npiv_tbl->wwpn[i]);
2078 		scnprintf(vpid.symbolic_name, sizeof(vpid.symbolic_name),
2079 		    "NPIV[%u]:%016llx-%016llx",
2080 		    created, vpid.port_name, vpid.node_name);
2081 		if (fc_vport_create(lport->host, 0, &vpid))
2082 			created++;
2083 		else
2084 			BNX2FC_HBA_DBG(lport, "Failed to create vport\n");
2085 	}
2086 done:
2087 	return created;
2088 }
2089 
2090 static int __bnx2fc_enable(struct fcoe_ctlr *ctlr)
2091 {
2092 	struct bnx2fc_interface *interface = fcoe_ctlr_priv(ctlr);
2093 	struct bnx2fc_hba *hba;
2094 	struct cnic_fc_npiv_tbl *npiv_tbl;
2095 	struct fc_lport *lport;
2096 
2097 	if (interface->enabled == false) {
2098 		if (!ctlr->lp) {
2099 			pr_err(PFX "__bnx2fc_enable: lport not found\n");
2100 			return -ENODEV;
2101 		} else if (!bnx2fc_link_ok(ctlr->lp)) {
2102 			fcoe_ctlr_link_up(ctlr);
2103 			interface->enabled = true;
2104 		}
2105 	}
2106 
2107 	/* Create static NPIV ports if any are contained in NVRAM */
2108 	hba = interface->hba;
2109 	lport = ctlr->lp;
2110 
2111 	if (!hba)
2112 		goto done;
2113 
2114 	if (!hba->cnic)
2115 		goto done;
2116 
2117 	if (!lport)
2118 		goto done;
2119 
2120 	if (!lport->host)
2121 		goto done;
2122 
2123 	if (!hba->cnic->get_fc_npiv_tbl)
2124 		goto done;
2125 
2126 	npiv_tbl = kzalloc(sizeof(struct cnic_fc_npiv_tbl), GFP_KERNEL);
2127 	if (!npiv_tbl)
2128 		goto done;
2129 
2130 	if (hba->cnic->get_fc_npiv_tbl(hba->cnic, npiv_tbl))
2131 		goto done_free;
2132 
2133 	bnx2fc_npiv_create_vports(lport, npiv_tbl);
2134 done_free:
2135 	kfree(npiv_tbl);
2136 done:
2137 	return 0;
2138 }
2139 
2140 /**
2141  * Deprecated: Use bnx2fc_enabled()
2142  */
2143 static int bnx2fc_enable(struct net_device *netdev)
2144 {
2145 	struct bnx2fc_interface *interface;
2146 	struct fcoe_ctlr *ctlr;
2147 	int rc = 0;
2148 
2149 	rtnl_lock();
2150 	mutex_lock(&bnx2fc_dev_lock);
2151 
2152 	interface = bnx2fc_interface_lookup(netdev);
2153 	ctlr = bnx2fc_to_ctlr(interface);
2154 	if (!interface) {
2155 		rc = -ENODEV;
2156 		pr_err(PFX "bnx2fc_enable: interface not found\n");
2157 	} else {
2158 		rc = __bnx2fc_enable(ctlr);
2159 	}
2160 
2161 	mutex_unlock(&bnx2fc_dev_lock);
2162 	rtnl_unlock();
2163 	return rc;
2164 }
2165 
2166 /**
2167  * bnx2fc_ctlr_enabled() - Enable or disable an FCoE Controller
2168  * @cdev: The FCoE Controller that is being enabled or disabled
2169  *
2170  * fcoe_sysfs will ensure that the state of 'enabled' has
2171  * changed, so no checking is necessary here. This routine simply
2172  * calls fcoe_enable or fcoe_disable, both of which are deprecated.
2173  * When those routines are removed the functionality can be merged
2174  * here.
2175  */
2176 static int bnx2fc_ctlr_enabled(struct fcoe_ctlr_device *cdev)
2177 {
2178 	struct fcoe_ctlr *ctlr = fcoe_ctlr_device_priv(cdev);
2179 
2180 	switch (cdev->enabled) {
2181 	case FCOE_CTLR_ENABLED:
2182 		return __bnx2fc_enable(ctlr);
2183 	case FCOE_CTLR_DISABLED:
2184 		return __bnx2fc_disable(ctlr);
2185 	case FCOE_CTLR_UNUSED:
2186 	default:
2187 		return -ENOTSUPP;
2188 	};
2189 }
2190 
2191 enum bnx2fc_create_link_state {
2192 	BNX2FC_CREATE_LINK_DOWN,
2193 	BNX2FC_CREATE_LINK_UP,
2194 };
2195 
2196 /**
2197  * _bnx2fc_create() - Create bnx2fc FCoE interface
2198  * @netdev  :   The net_device object the Ethernet interface to create on
2199  * @fip_mode:   The FIP mode for this creation
2200  * @link_state: The ctlr link state on creation
2201  *
2202  * Called from either the libfcoe 'create' module parameter
2203  * via fcoe_create or from fcoe_syfs's ctlr_create file.
2204  *
2205  * libfcoe's 'create' module parameter is deprecated so some
2206  * consolidation of code can be done when that interface is
2207  * removed.
2208  *
2209  * Returns: 0 for success
2210  */
2211 static int _bnx2fc_create(struct net_device *netdev,
2212 			  enum fip_state fip_mode,
2213 			  enum bnx2fc_create_link_state link_state)
2214 {
2215 	struct fcoe_ctlr_device *cdev;
2216 	struct fcoe_ctlr *ctlr;
2217 	struct bnx2fc_interface *interface;
2218 	struct bnx2fc_hba *hba;
2219 	struct net_device *phys_dev = netdev;
2220 	struct fc_lport *lport;
2221 	struct ethtool_drvinfo drvinfo;
2222 	int rc = 0;
2223 	int vlan_id = 0;
2224 
2225 	BNX2FC_MISC_DBG("Entered bnx2fc_create\n");
2226 	if (fip_mode != FIP_MODE_FABRIC) {
2227 		printk(KERN_ERR "fip mode not FABRIC\n");
2228 		return -EIO;
2229 	}
2230 
2231 	rtnl_lock();
2232 
2233 	mutex_lock(&bnx2fc_dev_lock);
2234 
2235 	if (!try_module_get(THIS_MODULE)) {
2236 		rc = -EINVAL;
2237 		goto mod_err;
2238 	}
2239 
2240 	/* obtain physical netdev */
2241 	if (netdev->priv_flags & IFF_802_1Q_VLAN)
2242 		phys_dev = vlan_dev_real_dev(netdev);
2243 
2244 	/* verify if the physical device is a netxtreme2 device */
2245 	if (phys_dev->ethtool_ops && phys_dev->ethtool_ops->get_drvinfo) {
2246 		memset(&drvinfo, 0, sizeof(drvinfo));
2247 		phys_dev->ethtool_ops->get_drvinfo(phys_dev, &drvinfo);
2248 		if (strncmp(drvinfo.driver, "bnx2x", strlen("bnx2x"))) {
2249 			printk(KERN_ERR PFX "Not a netxtreme2 device\n");
2250 			rc = -EINVAL;
2251 			goto netdev_err;
2252 		}
2253 	} else {
2254 		printk(KERN_ERR PFX "unable to obtain drv_info\n");
2255 		rc = -EINVAL;
2256 		goto netdev_err;
2257 	}
2258 
2259 	/* obtain interface and initialize rest of the structure */
2260 	hba = bnx2fc_hba_lookup(phys_dev);
2261 	if (!hba) {
2262 		rc = -ENODEV;
2263 		printk(KERN_ERR PFX "bnx2fc_create: hba not found\n");
2264 		goto netdev_err;
2265 	}
2266 
2267 	if (bnx2fc_interface_lookup(netdev)) {
2268 		rc = -EEXIST;
2269 		goto netdev_err;
2270 	}
2271 
2272 	interface = bnx2fc_interface_create(hba, netdev, fip_mode);
2273 	if (!interface) {
2274 		printk(KERN_ERR PFX "bnx2fc_interface_create failed\n");
2275 		rc = -ENOMEM;
2276 		goto ifput_err;
2277 	}
2278 
2279 	if (netdev->priv_flags & IFF_802_1Q_VLAN) {
2280 		vlan_id = vlan_dev_vlan_id(netdev);
2281 		interface->vlan_enabled = 1;
2282 	}
2283 
2284 	ctlr = bnx2fc_to_ctlr(interface);
2285 	cdev = fcoe_ctlr_to_ctlr_dev(ctlr);
2286 	interface->vlan_id = vlan_id;
2287 
2288 	interface->timer_work_queue =
2289 			create_singlethread_workqueue("bnx2fc_timer_wq");
2290 	if (!interface->timer_work_queue) {
2291 		printk(KERN_ERR PFX "ulp_init could not create timer_wq\n");
2292 		rc = -EINVAL;
2293 		goto ifput_err;
2294 	}
2295 
2296 	lport = bnx2fc_if_create(interface, &cdev->dev, 0);
2297 	if (!lport) {
2298 		printk(KERN_ERR PFX "Failed to create interface (%s)\n",
2299 			netdev->name);
2300 		rc = -EINVAL;
2301 		goto if_create_err;
2302 	}
2303 
2304 	/* Add interface to if_list */
2305 	list_add_tail(&interface->list, &if_list);
2306 
2307 	lport->boot_time = jiffies;
2308 
2309 	/* Make this master N_port */
2310 	ctlr->lp = lport;
2311 
2312 	if (link_state == BNX2FC_CREATE_LINK_UP)
2313 		cdev->enabled = FCOE_CTLR_ENABLED;
2314 	else
2315 		cdev->enabled = FCOE_CTLR_DISABLED;
2316 
2317 	if (link_state == BNX2FC_CREATE_LINK_UP &&
2318 	    !bnx2fc_link_ok(lport)) {
2319 		fcoe_ctlr_link_up(ctlr);
2320 		fc_host_port_type(lport->host) = FC_PORTTYPE_NPORT;
2321 		set_bit(ADAPTER_STATE_READY, &interface->hba->adapter_state);
2322 	}
2323 
2324 	BNX2FC_HBA_DBG(lport, "create: START DISC\n");
2325 	bnx2fc_start_disc(interface);
2326 
2327 	if (link_state == BNX2FC_CREATE_LINK_UP)
2328 		interface->enabled = true;
2329 
2330 	/*
2331 	 * Release from kref_init in bnx2fc_interface_setup, on success
2332 	 * lport should be holding a reference taken in bnx2fc_if_create
2333 	 */
2334 	bnx2fc_interface_put(interface);
2335 	/* put netdev that was held while calling dev_get_by_name */
2336 	mutex_unlock(&bnx2fc_dev_lock);
2337 	rtnl_unlock();
2338 	return 0;
2339 
2340 if_create_err:
2341 	destroy_workqueue(interface->timer_work_queue);
2342 ifput_err:
2343 	bnx2fc_net_cleanup(interface);
2344 	bnx2fc_interface_put(interface);
2345 	goto mod_err;
2346 netdev_err:
2347 	module_put(THIS_MODULE);
2348 mod_err:
2349 	mutex_unlock(&bnx2fc_dev_lock);
2350 	rtnl_unlock();
2351 	return rc;
2352 }
2353 
2354 /**
2355  * bnx2fc_create() - Create a bnx2fc interface
2356  * @netdev  : The net_device object the Ethernet interface to create on
2357  * @fip_mode: The FIP mode for this creation
2358  *
2359  * Called from fcoe transport
2360  *
2361  * Returns: 0 for success
2362  */
2363 static int bnx2fc_create(struct net_device *netdev, enum fip_state fip_mode)
2364 {
2365 	return _bnx2fc_create(netdev, fip_mode, BNX2FC_CREATE_LINK_UP);
2366 }
2367 
2368 /**
2369  * bnx2fc_ctlr_alloc() - Allocate a bnx2fc interface from fcoe_sysfs
2370  * @netdev: The net_device to be used by the allocated FCoE Controller
2371  *
2372  * This routine is called from fcoe_sysfs. It will start the fcoe_ctlr
2373  * in a link_down state. The allows the user an opportunity to configure
2374  * the FCoE Controller from sysfs before enabling the FCoE Controller.
2375  *
2376  * Creating in with this routine starts the FCoE Controller in Fabric
2377  * mode. The user can change to VN2VN or another mode before enabling.
2378  */
2379 static int bnx2fc_ctlr_alloc(struct net_device *netdev)
2380 {
2381 	return _bnx2fc_create(netdev, FIP_MODE_FABRIC,
2382 			      BNX2FC_CREATE_LINK_DOWN);
2383 }
2384 
2385 /**
2386  * bnx2fc_find_hba_for_cnic - maps cnic instance to bnx2fc hba instance
2387  *
2388  * @cnic:	Pointer to cnic device instance
2389  *
2390  **/
2391 static struct bnx2fc_hba *bnx2fc_find_hba_for_cnic(struct cnic_dev *cnic)
2392 {
2393 	struct bnx2fc_hba *hba;
2394 
2395 	/* Called with bnx2fc_dev_lock held */
2396 	list_for_each_entry(hba, &adapter_list, list) {
2397 		if (hba->cnic == cnic)
2398 			return hba;
2399 	}
2400 	return NULL;
2401 }
2402 
2403 static struct bnx2fc_interface *bnx2fc_interface_lookup(struct net_device
2404 							*netdev)
2405 {
2406 	struct bnx2fc_interface *interface;
2407 
2408 	/* Called with bnx2fc_dev_lock held */
2409 	list_for_each_entry(interface, &if_list, list) {
2410 		if (interface->netdev == netdev)
2411 			return interface;
2412 	}
2413 	return NULL;
2414 }
2415 
2416 static struct bnx2fc_hba *bnx2fc_hba_lookup(struct net_device
2417 						      *phys_dev)
2418 {
2419 	struct bnx2fc_hba *hba;
2420 
2421 	/* Called with bnx2fc_dev_lock held */
2422 	list_for_each_entry(hba, &adapter_list, list) {
2423 		if (hba->phys_dev == phys_dev)
2424 			return hba;
2425 	}
2426 	printk(KERN_ERR PFX "adapter_lookup: hba NULL\n");
2427 	return NULL;
2428 }
2429 
2430 /**
2431  * bnx2fc_ulp_exit - shuts down adapter instance and frees all resources
2432  *
2433  * @dev		cnic device handle
2434  */
2435 static void bnx2fc_ulp_exit(struct cnic_dev *dev)
2436 {
2437 	struct bnx2fc_hba *hba;
2438 	struct bnx2fc_interface *interface, *tmp;
2439 
2440 	BNX2FC_MISC_DBG("Entered bnx2fc_ulp_exit\n");
2441 
2442 	if (!test_bit(CNIC_F_BNX2X_CLASS, &dev->flags)) {
2443 		printk(KERN_ERR PFX "bnx2fc port check: %s, flags: %lx\n",
2444 			dev->netdev->name, dev->flags);
2445 		return;
2446 	}
2447 
2448 	mutex_lock(&bnx2fc_dev_lock);
2449 	hba = bnx2fc_find_hba_for_cnic(dev);
2450 	if (!hba) {
2451 		printk(KERN_ERR PFX "bnx2fc_ulp_exit: hba not found, dev 0%p\n",
2452 		       dev);
2453 		mutex_unlock(&bnx2fc_dev_lock);
2454 		return;
2455 	}
2456 
2457 	list_del_init(&hba->list);
2458 	adapter_count--;
2459 
2460 	list_for_each_entry_safe(interface, tmp, &if_list, list)
2461 		/* destroy not called yet, move to quiesced list */
2462 		if (interface->hba == hba)
2463 			__bnx2fc_destroy(interface);
2464 	mutex_unlock(&bnx2fc_dev_lock);
2465 
2466 	/* Ensure ALL destroy work has been completed before return */
2467 	flush_workqueue(bnx2fc_wq);
2468 
2469 	bnx2fc_ulp_stop(hba);
2470 	/* unregister cnic device */
2471 	if (test_and_clear_bit(BNX2FC_CNIC_REGISTERED, &hba->reg_with_cnic))
2472 		hba->cnic->unregister_device(hba->cnic, CNIC_ULP_FCOE);
2473 	bnx2fc_hba_destroy(hba);
2474 }
2475 
2476 /**
2477  * bnx2fc_fcoe_reset - Resets the fcoe
2478  *
2479  * @shost: shost the reset is from
2480  *
2481  * Returns: always 0
2482  */
2483 static int bnx2fc_fcoe_reset(struct Scsi_Host *shost)
2484 {
2485 	struct fc_lport *lport = shost_priv(shost);
2486 	fc_lport_reset(lport);
2487 	return 0;
2488 }
2489 
2490 
2491 static bool bnx2fc_match(struct net_device *netdev)
2492 {
2493 	struct net_device *phys_dev = netdev;
2494 
2495 	mutex_lock(&bnx2fc_dev_lock);
2496 	if (netdev->priv_flags & IFF_802_1Q_VLAN)
2497 		phys_dev = vlan_dev_real_dev(netdev);
2498 
2499 	if (bnx2fc_hba_lookup(phys_dev)) {
2500 		mutex_unlock(&bnx2fc_dev_lock);
2501 		return true;
2502 	}
2503 
2504 	mutex_unlock(&bnx2fc_dev_lock);
2505 	return false;
2506 }
2507 
2508 
2509 static struct fcoe_transport bnx2fc_transport = {
2510 	.name = {"bnx2fc"},
2511 	.attached = false,
2512 	.list = LIST_HEAD_INIT(bnx2fc_transport.list),
2513 	.alloc = bnx2fc_ctlr_alloc,
2514 	.match = bnx2fc_match,
2515 	.create = bnx2fc_create,
2516 	.destroy = bnx2fc_destroy,
2517 	.enable = bnx2fc_enable,
2518 	.disable = bnx2fc_disable,
2519 };
2520 
2521 /**
2522  * bnx2fc_percpu_thread_create - Create a receive thread for an
2523  *				 online CPU
2524  *
2525  * @cpu: cpu index for the online cpu
2526  */
2527 static void bnx2fc_percpu_thread_create(unsigned int cpu)
2528 {
2529 	struct bnx2fc_percpu_s *p;
2530 	struct task_struct *thread;
2531 
2532 	p = &per_cpu(bnx2fc_percpu, cpu);
2533 
2534 	thread = kthread_create_on_node(bnx2fc_percpu_io_thread,
2535 					(void *)p, cpu_to_node(cpu),
2536 					"bnx2fc_thread/%d", cpu);
2537 	/* bind thread to the cpu */
2538 	if (likely(!IS_ERR(thread))) {
2539 		kthread_bind(thread, cpu);
2540 		p->iothread = thread;
2541 		wake_up_process(thread);
2542 	}
2543 }
2544 
2545 static void bnx2fc_percpu_thread_destroy(unsigned int cpu)
2546 {
2547 	struct bnx2fc_percpu_s *p;
2548 	struct task_struct *thread;
2549 	struct bnx2fc_work *work, *tmp;
2550 
2551 	BNX2FC_MISC_DBG("destroying io thread for CPU %d\n", cpu);
2552 
2553 	/* Prevent any new work from being queued for this CPU */
2554 	p = &per_cpu(bnx2fc_percpu, cpu);
2555 	spin_lock_bh(&p->fp_work_lock);
2556 	thread = p->iothread;
2557 	p->iothread = NULL;
2558 
2559 
2560 	/* Free all work in the list */
2561 	list_for_each_entry_safe(work, tmp, &p->work_list, list) {
2562 		list_del_init(&work->list);
2563 		bnx2fc_process_cq_compl(work->tgt, work->wqe);
2564 		kfree(work);
2565 	}
2566 
2567 	spin_unlock_bh(&p->fp_work_lock);
2568 
2569 	if (thread)
2570 		kthread_stop(thread);
2571 }
2572 
2573 /**
2574  * bnx2fc_cpu_callback - Handler for CPU hotplug events
2575  *
2576  * @nfb:    The callback data block
2577  * @action: The event triggering the callback
2578  * @hcpu:   The index of the CPU that the event is for
2579  *
2580  * This creates or destroys per-CPU data for fcoe
2581  *
2582  * Returns NOTIFY_OK always.
2583  */
2584 static int bnx2fc_cpu_callback(struct notifier_block *nfb,
2585 			     unsigned long action, void *hcpu)
2586 {
2587 	unsigned cpu = (unsigned long)hcpu;
2588 
2589 	switch (action) {
2590 	case CPU_ONLINE:
2591 	case CPU_ONLINE_FROZEN:
2592 		printk(PFX "CPU %x online: Create Rx thread\n", cpu);
2593 		bnx2fc_percpu_thread_create(cpu);
2594 		break;
2595 	case CPU_DEAD:
2596 	case CPU_DEAD_FROZEN:
2597 		printk(PFX "CPU %x offline: Remove Rx thread\n", cpu);
2598 		bnx2fc_percpu_thread_destroy(cpu);
2599 		break;
2600 	default:
2601 		break;
2602 	}
2603 	return NOTIFY_OK;
2604 }
2605 
2606 /**
2607  * bnx2fc_mod_init - module init entry point
2608  *
2609  * Initialize driver wide global data structures, and register
2610  * with cnic module
2611  **/
2612 static int __init bnx2fc_mod_init(void)
2613 {
2614 	struct fcoe_percpu_s *bg;
2615 	struct task_struct *l2_thread;
2616 	int rc = 0;
2617 	unsigned int cpu = 0;
2618 	struct bnx2fc_percpu_s *p;
2619 
2620 	printk(KERN_INFO PFX "%s", version);
2621 
2622 	/* register as a fcoe transport */
2623 	rc = fcoe_transport_attach(&bnx2fc_transport);
2624 	if (rc) {
2625 		printk(KERN_ERR "failed to register an fcoe transport, check "
2626 			"if libfcoe is loaded\n");
2627 		goto out;
2628 	}
2629 
2630 	INIT_LIST_HEAD(&adapter_list);
2631 	INIT_LIST_HEAD(&if_list);
2632 	mutex_init(&bnx2fc_dev_lock);
2633 	adapter_count = 0;
2634 
2635 	/* Attach FC transport template */
2636 	rc = bnx2fc_attach_transport();
2637 	if (rc)
2638 		goto detach_ft;
2639 
2640 	bnx2fc_wq = alloc_workqueue("bnx2fc", 0, 0);
2641 	if (!bnx2fc_wq) {
2642 		rc = -ENOMEM;
2643 		goto release_bt;
2644 	}
2645 
2646 	bg = &bnx2fc_global;
2647 	skb_queue_head_init(&bg->fcoe_rx_list);
2648 	l2_thread = kthread_create(bnx2fc_l2_rcv_thread,
2649 				   (void *)bg,
2650 				   "bnx2fc_l2_thread");
2651 	if (IS_ERR(l2_thread)) {
2652 		rc = PTR_ERR(l2_thread);
2653 		goto free_wq;
2654 	}
2655 	wake_up_process(l2_thread);
2656 	spin_lock_bh(&bg->fcoe_rx_list.lock);
2657 	bg->thread = l2_thread;
2658 	spin_unlock_bh(&bg->fcoe_rx_list.lock);
2659 
2660 	for_each_possible_cpu(cpu) {
2661 		p = &per_cpu(bnx2fc_percpu, cpu);
2662 		INIT_LIST_HEAD(&p->work_list);
2663 		spin_lock_init(&p->fp_work_lock);
2664 	}
2665 
2666 	cpu_notifier_register_begin();
2667 
2668 	for_each_online_cpu(cpu) {
2669 		bnx2fc_percpu_thread_create(cpu);
2670 	}
2671 
2672 	/* Initialize per CPU interrupt thread */
2673 	__register_hotcpu_notifier(&bnx2fc_cpu_notifier);
2674 
2675 	cpu_notifier_register_done();
2676 
2677 	cnic_register_driver(CNIC_ULP_FCOE, &bnx2fc_cnic_cb);
2678 
2679 	return 0;
2680 
2681 free_wq:
2682 	destroy_workqueue(bnx2fc_wq);
2683 release_bt:
2684 	bnx2fc_release_transport();
2685 detach_ft:
2686 	fcoe_transport_detach(&bnx2fc_transport);
2687 out:
2688 	return rc;
2689 }
2690 
2691 static void __exit bnx2fc_mod_exit(void)
2692 {
2693 	LIST_HEAD(to_be_deleted);
2694 	struct bnx2fc_hba *hba, *next;
2695 	struct fcoe_percpu_s *bg;
2696 	struct task_struct *l2_thread;
2697 	struct sk_buff *skb;
2698 	unsigned int cpu = 0;
2699 
2700 	/*
2701 	 * NOTE: Since cnic calls register_driver routine rtnl_lock,
2702 	 * it will have higher precedence than bnx2fc_dev_lock.
2703 	 * unregister_device() cannot be called with bnx2fc_dev_lock
2704 	 * held.
2705 	 */
2706 	mutex_lock(&bnx2fc_dev_lock);
2707 	list_splice(&adapter_list, &to_be_deleted);
2708 	INIT_LIST_HEAD(&adapter_list);
2709 	adapter_count = 0;
2710 	mutex_unlock(&bnx2fc_dev_lock);
2711 
2712 	/* Unregister with cnic */
2713 	list_for_each_entry_safe(hba, next, &to_be_deleted, list) {
2714 		list_del_init(&hba->list);
2715 		printk(KERN_ERR PFX "MOD_EXIT:destroy hba = 0x%p\n",
2716 		       hba);
2717 		bnx2fc_ulp_stop(hba);
2718 		/* unregister cnic device */
2719 		if (test_and_clear_bit(BNX2FC_CNIC_REGISTERED,
2720 				       &hba->reg_with_cnic))
2721 			hba->cnic->unregister_device(hba->cnic,
2722 							 CNIC_ULP_FCOE);
2723 		bnx2fc_hba_destroy(hba);
2724 	}
2725 	cnic_unregister_driver(CNIC_ULP_FCOE);
2726 
2727 	/* Destroy global thread */
2728 	bg = &bnx2fc_global;
2729 	spin_lock_bh(&bg->fcoe_rx_list.lock);
2730 	l2_thread = bg->thread;
2731 	bg->thread = NULL;
2732 	while ((skb = __skb_dequeue(&bg->fcoe_rx_list)) != NULL)
2733 		kfree_skb(skb);
2734 
2735 	spin_unlock_bh(&bg->fcoe_rx_list.lock);
2736 
2737 	if (l2_thread)
2738 		kthread_stop(l2_thread);
2739 
2740 	cpu_notifier_register_begin();
2741 
2742 	/* Destroy per cpu threads */
2743 	for_each_online_cpu(cpu) {
2744 		bnx2fc_percpu_thread_destroy(cpu);
2745 	}
2746 
2747 	__unregister_hotcpu_notifier(&bnx2fc_cpu_notifier);
2748 
2749 	cpu_notifier_register_done();
2750 
2751 	destroy_workqueue(bnx2fc_wq);
2752 	/*
2753 	 * detach from scsi transport
2754 	 * must happen after all destroys are done
2755 	 */
2756 	bnx2fc_release_transport();
2757 
2758 	/* detach from fcoe transport */
2759 	fcoe_transport_detach(&bnx2fc_transport);
2760 }
2761 
2762 module_init(bnx2fc_mod_init);
2763 module_exit(bnx2fc_mod_exit);
2764 
2765 static struct fcoe_sysfs_function_template bnx2fc_fcoe_sysfs_templ = {
2766 	.set_fcoe_ctlr_enabled = bnx2fc_ctlr_enabled,
2767 	.get_fcoe_ctlr_link_fail = fcoe_ctlr_get_lesb,
2768 	.get_fcoe_ctlr_vlink_fail = fcoe_ctlr_get_lesb,
2769 	.get_fcoe_ctlr_miss_fka = fcoe_ctlr_get_lesb,
2770 	.get_fcoe_ctlr_symb_err = fcoe_ctlr_get_lesb,
2771 	.get_fcoe_ctlr_err_block = fcoe_ctlr_get_lesb,
2772 	.get_fcoe_ctlr_fcs_error = fcoe_ctlr_get_lesb,
2773 
2774 	.get_fcoe_fcf_selected = fcoe_fcf_get_selected,
2775 	.get_fcoe_fcf_vlan_id = bnx2fc_fcf_get_vlan_id,
2776 };
2777 
2778 static struct fc_function_template bnx2fc_transport_function = {
2779 	.show_host_node_name = 1,
2780 	.show_host_port_name = 1,
2781 	.show_host_supported_classes = 1,
2782 	.show_host_supported_fc4s = 1,
2783 	.show_host_active_fc4s = 1,
2784 	.show_host_maxframe_size = 1,
2785 
2786 	.show_host_port_id = 1,
2787 	.show_host_supported_speeds = 1,
2788 	.get_host_speed = fc_get_host_speed,
2789 	.show_host_speed = 1,
2790 	.show_host_port_type = 1,
2791 	.get_host_port_state = fc_get_host_port_state,
2792 	.show_host_port_state = 1,
2793 	.show_host_symbolic_name = 1,
2794 
2795 	.dd_fcrport_size = (sizeof(struct fc_rport_libfc_priv) +
2796 				sizeof(struct bnx2fc_rport)),
2797 	.show_rport_maxframe_size = 1,
2798 	.show_rport_supported_classes = 1,
2799 
2800 	.show_host_fabric_name = 1,
2801 	.show_starget_node_name = 1,
2802 	.show_starget_port_name = 1,
2803 	.show_starget_port_id = 1,
2804 	.set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
2805 	.show_rport_dev_loss_tmo = 1,
2806 	.get_fc_host_stats = bnx2fc_get_host_stats,
2807 
2808 	.issue_fc_host_lip = bnx2fc_fcoe_reset,
2809 
2810 	.terminate_rport_io = fc_rport_terminate_io,
2811 
2812 	.vport_create = bnx2fc_vport_create,
2813 	.vport_delete = bnx2fc_vport_destroy,
2814 	.vport_disable = bnx2fc_vport_disable,
2815 	.bsg_request = fc_lport_bsg_request,
2816 };
2817 
2818 static struct fc_function_template bnx2fc_vport_xport_function = {
2819 	.show_host_node_name = 1,
2820 	.show_host_port_name = 1,
2821 	.show_host_supported_classes = 1,
2822 	.show_host_supported_fc4s = 1,
2823 	.show_host_active_fc4s = 1,
2824 	.show_host_maxframe_size = 1,
2825 
2826 	.show_host_port_id = 1,
2827 	.show_host_supported_speeds = 1,
2828 	.get_host_speed = fc_get_host_speed,
2829 	.show_host_speed = 1,
2830 	.show_host_port_type = 1,
2831 	.get_host_port_state = fc_get_host_port_state,
2832 	.show_host_port_state = 1,
2833 	.show_host_symbolic_name = 1,
2834 
2835 	.dd_fcrport_size = (sizeof(struct fc_rport_libfc_priv) +
2836 				sizeof(struct bnx2fc_rport)),
2837 	.show_rport_maxframe_size = 1,
2838 	.show_rport_supported_classes = 1,
2839 
2840 	.show_host_fabric_name = 1,
2841 	.show_starget_node_name = 1,
2842 	.show_starget_port_name = 1,
2843 	.show_starget_port_id = 1,
2844 	.set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
2845 	.show_rport_dev_loss_tmo = 1,
2846 	.get_fc_host_stats = fc_get_host_stats,
2847 	.issue_fc_host_lip = bnx2fc_fcoe_reset,
2848 	.terminate_rport_io = fc_rport_terminate_io,
2849 	.bsg_request = fc_lport_bsg_request,
2850 };
2851 
2852 /**
2853  * scsi_host_template structure used while registering with SCSI-ml
2854  */
2855 static struct scsi_host_template bnx2fc_shost_template = {
2856 	.module			= THIS_MODULE,
2857 	.name			= "QLogic Offload FCoE Initiator",
2858 	.queuecommand		= bnx2fc_queuecommand,
2859 	.eh_abort_handler	= bnx2fc_eh_abort,	  /* abts */
2860 	.eh_device_reset_handler = bnx2fc_eh_device_reset, /* lun reset */
2861 	.eh_target_reset_handler = bnx2fc_eh_target_reset, /* tgt reset */
2862 	.eh_host_reset_handler	= fc_eh_host_reset,
2863 	.slave_alloc		= fc_slave_alloc,
2864 	.change_queue_depth	= scsi_change_queue_depth,
2865 	.this_id		= -1,
2866 	.cmd_per_lun		= 3,
2867 	.use_clustering		= ENABLE_CLUSTERING,
2868 	.sg_tablesize		= BNX2FC_MAX_BDS_PER_CMD,
2869 	.max_sectors		= 1024,
2870 	.track_queue_depth	= 1,
2871 };
2872 
2873 static struct libfc_function_template bnx2fc_libfc_fcn_templ = {
2874 	.frame_send		= bnx2fc_xmit,
2875 	.elsct_send		= bnx2fc_elsct_send,
2876 	.fcp_abort_io		= bnx2fc_abort_io,
2877 	.fcp_cleanup		= bnx2fc_cleanup,
2878 	.get_lesb		= fcoe_get_lesb,
2879 	.rport_event_callback	= bnx2fc_rport_event_handler,
2880 };
2881 
2882 /**
2883  * bnx2fc_cnic_cb - global template of bnx2fc - cnic driver interface
2884  *			structure carrying callback function pointers
2885  */
2886 static struct cnic_ulp_ops bnx2fc_cnic_cb = {
2887 	.owner			= THIS_MODULE,
2888 	.cnic_init		= bnx2fc_ulp_init,
2889 	.cnic_exit		= bnx2fc_ulp_exit,
2890 	.cnic_start		= bnx2fc_ulp_start,
2891 	.cnic_stop		= bnx2fc_ulp_stop,
2892 	.indicate_kcqes		= bnx2fc_indicate_kcqe,
2893 	.indicate_netevent	= bnx2fc_indicate_netevent,
2894 	.cnic_get_stats		= bnx2fc_ulp_get_stats,
2895 };
2896