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