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