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