1 /* bnx2fc_fcoe.c: Broadcom NetXtreme II Linux FCoE offload driver.
2  * This file contains the code that interacts with libfc, libfcoe,
3  * cnic modules to create FCoE instances, send/receive non-offloaded
4  * FIP/FCoE packets, listen to link events etc.
5  *
6  * Copyright (c) 2008 - 2013 Broadcom Corporation
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation.
11  *
12  * Written by: Bhanu Prakash Gollapudi (bprakash@broadcom.com)
13  */
14 
15 #include "bnx2fc.h"
16 
17 static struct list_head adapter_list;
18 static struct list_head if_list;
19 static u32 adapter_count;
20 static DEFINE_MUTEX(bnx2fc_dev_lock);
21 DEFINE_PER_CPU(struct bnx2fc_percpu_s, bnx2fc_percpu);
22 
23 #define DRV_MODULE_NAME		"bnx2fc"
24 #define DRV_MODULE_VERSION	BNX2FC_VERSION
25 #define DRV_MODULE_RELDATE	"Dec 11, 2013"
26 
27 
28 static char version[] =
29 		"Broadcom NetXtreme II FCoE Driver " DRV_MODULE_NAME \
30 		" v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")\n";
31 
32 
33 MODULE_AUTHOR("Bhanu Prakash Gollapudi <bprakash@broadcom.com>");
34 MODULE_DESCRIPTION("Broadcom NetXtreme II BCM57710 FCoE Driver");
35 MODULE_LICENSE("GPL");
36 MODULE_VERSION(DRV_MODULE_VERSION);
37 
38 #define BNX2FC_MAX_QUEUE_DEPTH	256
39 #define BNX2FC_MIN_QUEUE_DEPTH	32
40 #define FCOE_WORD_TO_BYTE  4
41 
42 static struct scsi_transport_template	*bnx2fc_transport_template;
43 static struct scsi_transport_template	*bnx2fc_vport_xport_template;
44 
45 struct workqueue_struct *bnx2fc_wq;
46 
47 /* bnx2fc structure needs only one instance of the fcoe_percpu_s structure.
48  * Here the io threads are per cpu but the l2 thread is just one
49  */
50 struct fcoe_percpu_s bnx2fc_global;
51 DEFINE_SPINLOCK(bnx2fc_global_lock);
52 
53 static struct cnic_ulp_ops bnx2fc_cnic_cb;
54 static struct libfc_function_template bnx2fc_libfc_fcn_templ;
55 static struct scsi_host_template bnx2fc_shost_template;
56 static struct fc_function_template bnx2fc_transport_function;
57 static struct fcoe_sysfs_function_template bnx2fc_fcoe_sysfs_templ;
58 static struct fc_function_template bnx2fc_vport_xport_function;
59 static int bnx2fc_create(struct net_device *netdev, enum fip_state fip_mode);
60 static void __bnx2fc_destroy(struct bnx2fc_interface *interface);
61 static int bnx2fc_destroy(struct net_device *net_device);
62 static int bnx2fc_enable(struct net_device *netdev);
63 static int bnx2fc_disable(struct net_device *netdev);
64 
65 /* fcoe_syfs control interface handlers */
66 static int bnx2fc_ctlr_alloc(struct net_device *netdev);
67 static int bnx2fc_ctlr_enabled(struct fcoe_ctlr_device *cdev);
68 
69 static void bnx2fc_recv_frame(struct sk_buff *skb);
70 
71 static void bnx2fc_start_disc(struct bnx2fc_interface *interface);
72 static int bnx2fc_shost_config(struct fc_lport *lport, struct device *dev);
73 static int bnx2fc_lport_config(struct fc_lport *lport);
74 static int bnx2fc_em_config(struct fc_lport *lport, struct bnx2fc_hba *hba);
75 static int bnx2fc_bind_adapter_devices(struct bnx2fc_hba *hba);
76 static void bnx2fc_unbind_adapter_devices(struct bnx2fc_hba *hba);
77 static int bnx2fc_bind_pcidev(struct bnx2fc_hba *hba);
78 static void bnx2fc_unbind_pcidev(struct bnx2fc_hba *hba);
79 static struct fc_lport *bnx2fc_if_create(struct bnx2fc_interface *interface,
80 				  struct device *parent, int npiv);
81 static void bnx2fc_destroy_work(struct work_struct *work);
82 
83 static struct bnx2fc_hba *bnx2fc_hba_lookup(struct net_device *phys_dev);
84 static struct bnx2fc_interface *bnx2fc_interface_lookup(struct net_device
85 							*phys_dev);
86 static inline void bnx2fc_interface_put(struct bnx2fc_interface *interface);
87 static struct bnx2fc_hba *bnx2fc_find_hba_for_cnic(struct cnic_dev *cnic);
88 
89 static int bnx2fc_fw_init(struct bnx2fc_hba *hba);
90 static void bnx2fc_fw_destroy(struct bnx2fc_hba *hba);
91 
92 static void bnx2fc_port_shutdown(struct fc_lport *lport);
93 static void bnx2fc_stop(struct bnx2fc_interface *interface);
94 static int __init bnx2fc_mod_init(void);
95 static void __exit bnx2fc_mod_exit(void);
96 
97 unsigned int bnx2fc_debug_level;
98 module_param_named(debug_logging, bnx2fc_debug_level, int, S_IRUGO|S_IWUSR);
99 
100 static int bnx2fc_cpu_callback(struct notifier_block *nfb,
101 			     unsigned long action, void *hcpu);
102 /* notification function for CPU hotplug events */
103 static struct notifier_block bnx2fc_cpu_notifier = {
104 	.notifier_call = bnx2fc_cpu_callback,
105 };
106 
107 static inline struct net_device *bnx2fc_netdev(const struct fc_lport *lport)
108 {
109 	return ((struct bnx2fc_interface *)
110 		((struct fcoe_port *)lport_priv(lport))->priv)->netdev;
111 }
112 
113 static void bnx2fc_fcf_get_vlan_id(struct fcoe_fcf_device *fcf_dev)
114 {
115 	struct fcoe_ctlr_device *ctlr_dev =
116 		fcoe_fcf_dev_to_ctlr_dev(fcf_dev);
117 	struct fcoe_ctlr *ctlr = fcoe_ctlr_device_priv(ctlr_dev);
118 	struct bnx2fc_interface *fcoe = fcoe_ctlr_priv(ctlr);
119 
120 	fcf_dev->vlan_id = fcoe->vlan_id;
121 }
122 
123 static void bnx2fc_clean_rx_queue(struct fc_lport *lp)
124 {
125 	struct fcoe_percpu_s *bg;
126 	struct fcoe_rcv_info *fr;
127 	struct sk_buff_head *list;
128 	struct sk_buff *skb, *next;
129 	struct sk_buff *head;
130 
131 	bg = &bnx2fc_global;
132 	spin_lock_bh(&bg->fcoe_rx_list.lock);
133 	list = &bg->fcoe_rx_list;
134 	head = list->next;
135 	for (skb = head; skb != (struct sk_buff *)list;
136 	     skb = next) {
137 		next = skb->next;
138 		fr = fcoe_dev_from_skb(skb);
139 		if (fr->fr_dev == lp) {
140 			__skb_unlink(skb, list);
141 			kfree_skb(skb);
142 		}
143 	}
144 	spin_unlock_bh(&bg->fcoe_rx_list.lock);
145 }
146 
147 int bnx2fc_get_paged_crc_eof(struct sk_buff *skb, int tlen)
148 {
149 	int rc;
150 	spin_lock(&bnx2fc_global_lock);
151 	rc = fcoe_get_paged_crc_eof(skb, tlen, &bnx2fc_global);
152 	spin_unlock(&bnx2fc_global_lock);
153 
154 	return rc;
155 }
156 
157 static void bnx2fc_abort_io(struct fc_lport *lport)
158 {
159 	/*
160 	 * This function is no-op for bnx2fc, but we do
161 	 * not want to leave it as NULL either, as libfc
162 	 * can call the default function which is
163 	 * fc_fcp_abort_io.
164 	 */
165 }
166 
167 static void bnx2fc_cleanup(struct fc_lport *lport)
168 {
169 	struct fcoe_port *port = lport_priv(lport);
170 	struct bnx2fc_interface *interface = port->priv;
171 	struct bnx2fc_hba *hba = interface->hba;
172 	struct bnx2fc_rport *tgt;
173 	int i;
174 
175 	BNX2FC_MISC_DBG("Entered %s\n", __func__);
176 	mutex_lock(&hba->hba_mutex);
177 	spin_lock_bh(&hba->hba_lock);
178 	for (i = 0; i < BNX2FC_NUM_MAX_SESS; i++) {
179 		tgt = hba->tgt_ofld_list[i];
180 		if (tgt) {
181 			/* Cleanup IOs belonging to requested vport */
182 			if (tgt->port == port) {
183 				spin_unlock_bh(&hba->hba_lock);
184 				BNX2FC_TGT_DBG(tgt, "flush/cleanup\n");
185 				bnx2fc_flush_active_ios(tgt);
186 				spin_lock_bh(&hba->hba_lock);
187 			}
188 		}
189 	}
190 	spin_unlock_bh(&hba->hba_lock);
191 	mutex_unlock(&hba->hba_mutex);
192 }
193 
194 static int bnx2fc_xmit_l2_frame(struct bnx2fc_rport *tgt,
195 			     struct fc_frame *fp)
196 {
197 	struct fc_rport_priv *rdata = tgt->rdata;
198 	struct fc_frame_header *fh;
199 	int rc = 0;
200 
201 	fh = fc_frame_header_get(fp);
202 	BNX2FC_TGT_DBG(tgt, "Xmit L2 frame rport = 0x%x, oxid = 0x%x, "
203 			"r_ctl = 0x%x\n", rdata->ids.port_id,
204 			ntohs(fh->fh_ox_id), fh->fh_r_ctl);
205 	if ((fh->fh_type == FC_TYPE_ELS) &&
206 	    (fh->fh_r_ctl == FC_RCTL_ELS_REQ)) {
207 
208 		switch (fc_frame_payload_op(fp)) {
209 		case ELS_ADISC:
210 			rc = bnx2fc_send_adisc(tgt, fp);
211 			break;
212 		case ELS_LOGO:
213 			rc = bnx2fc_send_logo(tgt, fp);
214 			break;
215 		case ELS_RLS:
216 			rc = bnx2fc_send_rls(tgt, fp);
217 			break;
218 		default:
219 			break;
220 		}
221 	} else if ((fh->fh_type ==  FC_TYPE_BLS) &&
222 	    (fh->fh_r_ctl == FC_RCTL_BA_ABTS))
223 		BNX2FC_TGT_DBG(tgt, "ABTS frame\n");
224 	else {
225 		BNX2FC_TGT_DBG(tgt, "Send L2 frame type 0x%x "
226 				"rctl 0x%x thru non-offload path\n",
227 				fh->fh_type, fh->fh_r_ctl);
228 		return -ENODEV;
229 	}
230 	if (rc)
231 		return -ENOMEM;
232 	else
233 		return 0;
234 }
235 
236 /**
237  * bnx2fc_xmit - bnx2fc's FCoE frame transmit function
238  *
239  * @lport:	the associated local port
240  * @fp:	the fc_frame to be transmitted
241  */
242 static int bnx2fc_xmit(struct fc_lport *lport, struct fc_frame *fp)
243 {
244 	struct ethhdr		*eh;
245 	struct fcoe_crc_eof	*cp;
246 	struct sk_buff		*skb;
247 	struct fc_frame_header	*fh;
248 	struct bnx2fc_interface	*interface;
249 	struct fcoe_ctlr        *ctlr;
250 	struct bnx2fc_hba *hba;
251 	struct fcoe_port	*port;
252 	struct fcoe_hdr		*hp;
253 	struct bnx2fc_rport	*tgt;
254 	struct fc_stats		*stats;
255 	u8			sof, eof;
256 	u32			crc;
257 	unsigned int		hlen, tlen, elen;
258 	int			wlen, rc = 0;
259 
260 	port = (struct fcoe_port *)lport_priv(lport);
261 	interface = port->priv;
262 	ctlr = bnx2fc_to_ctlr(interface);
263 	hba = interface->hba;
264 
265 	fh = fc_frame_header_get(fp);
266 
267 	skb = fp_skb(fp);
268 	if (!lport->link_up) {
269 		BNX2FC_HBA_DBG(lport, "bnx2fc_xmit link down\n");
270 		kfree_skb(skb);
271 		return 0;
272 	}
273 
274 	if (unlikely(fh->fh_r_ctl == FC_RCTL_ELS_REQ)) {
275 		if (!ctlr->sel_fcf) {
276 			BNX2FC_HBA_DBG(lport, "FCF not selected yet!\n");
277 			kfree_skb(skb);
278 			return -EINVAL;
279 		}
280 		if (fcoe_ctlr_els_send(ctlr, lport, skb))
281 			return 0;
282 	}
283 
284 	sof = fr_sof(fp);
285 	eof = fr_eof(fp);
286 
287 	/*
288 	 * Snoop the frame header to check if the frame is for
289 	 * an offloaded session
290 	 */
291 	/*
292 	 * tgt_ofld_list access is synchronized using
293 	 * both hba mutex and hba lock. Atleast hba mutex or
294 	 * hba lock needs to be held for read access.
295 	 */
296 
297 	spin_lock_bh(&hba->hba_lock);
298 	tgt = bnx2fc_tgt_lookup(port, ntoh24(fh->fh_d_id));
299 	if (tgt && (test_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags))) {
300 		/* This frame is for offloaded session */
301 		BNX2FC_HBA_DBG(lport, "xmit: Frame is for offloaded session "
302 				"port_id = 0x%x\n", ntoh24(fh->fh_d_id));
303 		spin_unlock_bh(&hba->hba_lock);
304 		rc = bnx2fc_xmit_l2_frame(tgt, fp);
305 		if (rc != -ENODEV) {
306 			kfree_skb(skb);
307 			return rc;
308 		}
309 	} else {
310 		spin_unlock_bh(&hba->hba_lock);
311 	}
312 
313 	elen = sizeof(struct ethhdr);
314 	hlen = sizeof(struct fcoe_hdr);
315 	tlen = sizeof(struct fcoe_crc_eof);
316 	wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
317 
318 	skb->ip_summed = CHECKSUM_NONE;
319 	crc = fcoe_fc_crc(fp);
320 
321 	/* copy port crc and eof to the skb buff */
322 	if (skb_is_nonlinear(skb)) {
323 		skb_frag_t *frag;
324 		if (bnx2fc_get_paged_crc_eof(skb, tlen)) {
325 			kfree_skb(skb);
326 			return -ENOMEM;
327 		}
328 		frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
329 		cp = kmap_atomic(skb_frag_page(frag)) + frag->page_offset;
330 	} else {
331 		cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
332 	}
333 
334 	memset(cp, 0, sizeof(*cp));
335 	cp->fcoe_eof = eof;
336 	cp->fcoe_crc32 = cpu_to_le32(~crc);
337 	if (skb_is_nonlinear(skb)) {
338 		kunmap_atomic(cp);
339 		cp = NULL;
340 	}
341 
342 	/* adjust skb network/transport offsets to match mac/fcoe/port */
343 	skb_push(skb, elen + hlen);
344 	skb_reset_mac_header(skb);
345 	skb_reset_network_header(skb);
346 	skb->mac_len = elen;
347 	skb->protocol = htons(ETH_P_FCOE);
348 	skb->dev = interface->netdev;
349 
350 	/* fill up mac and fcoe headers */
351 	eh = eth_hdr(skb);
352 	eh->h_proto = htons(ETH_P_FCOE);
353 	if (ctlr->map_dest)
354 		fc_fcoe_set_mac(eh->h_dest, fh->fh_d_id);
355 	else
356 		/* insert GW address */
357 		memcpy(eh->h_dest, ctlr->dest_addr, ETH_ALEN);
358 
359 	if (unlikely(ctlr->flogi_oxid != FC_XID_UNKNOWN))
360 		memcpy(eh->h_source, ctlr->ctl_src_addr, ETH_ALEN);
361 	else
362 		memcpy(eh->h_source, port->data_src_addr, ETH_ALEN);
363 
364 	hp = (struct fcoe_hdr *)(eh + 1);
365 	memset(hp, 0, sizeof(*hp));
366 	if (FC_FCOE_VER)
367 		FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
368 	hp->fcoe_sof = sof;
369 
370 	/* fcoe lso, mss is in max_payload which is non-zero for FCP data */
371 	if (lport->seq_offload && fr_max_payload(fp)) {
372 		skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
373 		skb_shinfo(skb)->gso_size = fr_max_payload(fp);
374 	} else {
375 		skb_shinfo(skb)->gso_type = 0;
376 		skb_shinfo(skb)->gso_size = 0;
377 	}
378 
379 	/*update tx stats */
380 	stats = per_cpu_ptr(lport->stats, get_cpu());
381 	stats->TxFrames++;
382 	stats->TxWords += wlen;
383 	put_cpu();
384 
385 	/* send down to lld */
386 	fr_dev(fp) = lport;
387 	if (port->fcoe_pending_queue.qlen)
388 		fcoe_check_wait_queue(lport, skb);
389 	else if (fcoe_start_io(skb))
390 		fcoe_check_wait_queue(lport, skb);
391 
392 	return 0;
393 }
394 
395 /**
396  * bnx2fc_rcv - This is bnx2fc's receive function called by NET_RX_SOFTIRQ
397  *
398  * @skb:	the receive socket buffer
399  * @dev:	associated net device
400  * @ptype:	context
401  * @olddev:	last device
402  *
403  * This function receives the packet and builds FC frame and passes it up
404  */
405 static int bnx2fc_rcv(struct sk_buff *skb, struct net_device *dev,
406 		struct packet_type *ptype, struct net_device *olddev)
407 {
408 	struct fc_lport *lport;
409 	struct bnx2fc_interface *interface;
410 	struct fcoe_ctlr *ctlr;
411 	struct fc_frame_header *fh;
412 	struct fcoe_rcv_info *fr;
413 	struct fcoe_percpu_s *bg;
414 	unsigned short oxid;
415 
416 	interface = container_of(ptype, struct bnx2fc_interface,
417 				 fcoe_packet_type);
418 	ctlr = bnx2fc_to_ctlr(interface);
419 	lport = ctlr->lp;
420 
421 	if (unlikely(lport == NULL)) {
422 		printk(KERN_ERR PFX "bnx2fc_rcv: lport is NULL\n");
423 		goto err;
424 	}
425 
426 	if (unlikely(eth_hdr(skb)->h_proto != htons(ETH_P_FCOE))) {
427 		printk(KERN_ERR PFX "bnx2fc_rcv: Wrong FC type frame\n");
428 		goto err;
429 	}
430 
431 	/*
432 	 * Check for minimum frame length, and make sure required FCoE
433 	 * and FC headers are pulled into the linear data area.
434 	 */
435 	if (unlikely((skb->len < FCOE_MIN_FRAME) ||
436 	    !pskb_may_pull(skb, FCOE_HEADER_LEN)))
437 		goto err;
438 
439 	skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
440 	fh = (struct fc_frame_header *) skb_transport_header(skb);
441 
442 	oxid = ntohs(fh->fh_ox_id);
443 
444 	fr = fcoe_dev_from_skb(skb);
445 	fr->fr_dev = lport;
446 
447 	bg = &bnx2fc_global;
448 	spin_lock(&bg->fcoe_rx_list.lock);
449 
450 	__skb_queue_tail(&bg->fcoe_rx_list, skb);
451 	if (bg->fcoe_rx_list.qlen == 1)
452 		wake_up_process(bg->thread);
453 
454 	spin_unlock(&bg->fcoe_rx_list.lock);
455 
456 	return 0;
457 err:
458 	kfree_skb(skb);
459 	return -1;
460 }
461 
462 static int bnx2fc_l2_rcv_thread(void *arg)
463 {
464 	struct fcoe_percpu_s *bg = arg;
465 	struct sk_buff *skb;
466 
467 	set_user_nice(current, -20);
468 	set_current_state(TASK_INTERRUPTIBLE);
469 	while (!kthread_should_stop()) {
470 		schedule();
471 		spin_lock_bh(&bg->fcoe_rx_list.lock);
472 		while ((skb = __skb_dequeue(&bg->fcoe_rx_list)) != NULL) {
473 			spin_unlock_bh(&bg->fcoe_rx_list.lock);
474 			bnx2fc_recv_frame(skb);
475 			spin_lock_bh(&bg->fcoe_rx_list.lock);
476 		}
477 		__set_current_state(TASK_INTERRUPTIBLE);
478 		spin_unlock_bh(&bg->fcoe_rx_list.lock);
479 	}
480 	__set_current_state(TASK_RUNNING);
481 	return 0;
482 }
483 
484 
485 static void bnx2fc_recv_frame(struct sk_buff *skb)
486 {
487 	u32 fr_len;
488 	struct fc_lport *lport;
489 	struct fcoe_rcv_info *fr;
490 	struct fc_stats *stats;
491 	struct fc_frame_header *fh;
492 	struct fcoe_crc_eof crc_eof;
493 	struct fc_frame *fp;
494 	struct fc_lport *vn_port;
495 	struct fcoe_port *port;
496 	u8 *mac = NULL;
497 	u8 *dest_mac = NULL;
498 	struct fcoe_hdr *hp;
499 
500 	fr = fcoe_dev_from_skb(skb);
501 	lport = fr->fr_dev;
502 	if (unlikely(lport == NULL)) {
503 		printk(KERN_ERR PFX "Invalid lport struct\n");
504 		kfree_skb(skb);
505 		return;
506 	}
507 
508 	if (skb_is_nonlinear(skb))
509 		skb_linearize(skb);
510 	mac = eth_hdr(skb)->h_source;
511 	dest_mac = eth_hdr(skb)->h_dest;
512 
513 	/* Pull the header */
514 	hp = (struct fcoe_hdr *) skb_network_header(skb);
515 	fh = (struct fc_frame_header *) skb_transport_header(skb);
516 	skb_pull(skb, sizeof(struct fcoe_hdr));
517 	fr_len = skb->len - sizeof(struct fcoe_crc_eof);
518 
519 	stats = per_cpu_ptr(lport->stats, get_cpu());
520 	stats->RxFrames++;
521 	stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
522 
523 	fp = (struct fc_frame *)skb;
524 	fc_frame_init(fp);
525 	fr_dev(fp) = lport;
526 	fr_sof(fp) = hp->fcoe_sof;
527 	if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof))) {
528 		put_cpu();
529 		kfree_skb(skb);
530 		return;
531 	}
532 	fr_eof(fp) = crc_eof.fcoe_eof;
533 	fr_crc(fp) = crc_eof.fcoe_crc32;
534 	if (pskb_trim(skb, fr_len)) {
535 		put_cpu();
536 		kfree_skb(skb);
537 		return;
538 	}
539 
540 	fh = fc_frame_header_get(fp);
541 
542 	vn_port = fc_vport_id_lookup(lport, ntoh24(fh->fh_d_id));
543 	if (vn_port) {
544 		port = lport_priv(vn_port);
545 		if (!ether_addr_equal(port->data_src_addr, dest_mac)) {
546 			BNX2FC_HBA_DBG(lport, "fpma mismatch\n");
547 			put_cpu();
548 			kfree_skb(skb);
549 			return;
550 		}
551 	}
552 	if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA &&
553 	    fh->fh_type == FC_TYPE_FCP) {
554 		/* Drop FCP data. We dont this in L2 path */
555 		put_cpu();
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 				put_cpu();
566 				kfree_skb(skb);
567 				return;
568 			}
569 			break;
570 		}
571 	}
572 
573 	if (fh->fh_r_ctl == FC_RCTL_BA_ABTS) {
574 		/* Drop incoming ABTS */
575 		put_cpu();
576 		kfree_skb(skb);
577 		return;
578 	}
579 
580 	if (le32_to_cpu(fr_crc(fp)) !=
581 			~crc32(~0, skb->data, fr_len)) {
582 		if (stats->InvalidCRCCount < 5)
583 			printk(KERN_WARNING PFX "dropping frame with "
584 			       "CRC error\n");
585 		stats->InvalidCRCCount++;
586 		put_cpu();
587 		kfree_skb(skb);
588 		return;
589 	}
590 	put_cpu();
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, -20);
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 (Broadcom %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 (IS_ERR(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 		goto ifput_err;
2206 	}
2207 
2208 	if (netdev->priv_flags & IFF_802_1Q_VLAN) {
2209 		vlan_id = vlan_dev_vlan_id(netdev);
2210 		interface->vlan_enabled = 1;
2211 	}
2212 
2213 	ctlr = bnx2fc_to_ctlr(interface);
2214 	cdev = fcoe_ctlr_to_ctlr_dev(ctlr);
2215 	interface->vlan_id = vlan_id;
2216 
2217 	interface->timer_work_queue =
2218 			create_singlethread_workqueue("bnx2fc_timer_wq");
2219 	if (!interface->timer_work_queue) {
2220 		printk(KERN_ERR PFX "ulp_init could not create timer_wq\n");
2221 		rc = -EINVAL;
2222 		goto ifput_err;
2223 	}
2224 
2225 	lport = bnx2fc_if_create(interface, &cdev->dev, 0);
2226 	if (!lport) {
2227 		printk(KERN_ERR PFX "Failed to create interface (%s)\n",
2228 			netdev->name);
2229 		rc = -EINVAL;
2230 		goto if_create_err;
2231 	}
2232 
2233 	/* Add interface to if_list */
2234 	list_add_tail(&interface->list, &if_list);
2235 
2236 	lport->boot_time = jiffies;
2237 
2238 	/* Make this master N_port */
2239 	ctlr->lp = lport;
2240 
2241 	if (link_state == BNX2FC_CREATE_LINK_UP)
2242 		cdev->enabled = FCOE_CTLR_ENABLED;
2243 	else
2244 		cdev->enabled = FCOE_CTLR_DISABLED;
2245 
2246 	if (link_state == BNX2FC_CREATE_LINK_UP &&
2247 	    !bnx2fc_link_ok(lport)) {
2248 		fcoe_ctlr_link_up(ctlr);
2249 		fc_host_port_type(lport->host) = FC_PORTTYPE_NPORT;
2250 		set_bit(ADAPTER_STATE_READY, &interface->hba->adapter_state);
2251 	}
2252 
2253 	BNX2FC_HBA_DBG(lport, "create: START DISC\n");
2254 	bnx2fc_start_disc(interface);
2255 
2256 	if (link_state == BNX2FC_CREATE_LINK_UP)
2257 		interface->enabled = true;
2258 
2259 	/*
2260 	 * Release from kref_init in bnx2fc_interface_setup, on success
2261 	 * lport should be holding a reference taken in bnx2fc_if_create
2262 	 */
2263 	bnx2fc_interface_put(interface);
2264 	/* put netdev that was held while calling dev_get_by_name */
2265 	mutex_unlock(&bnx2fc_dev_lock);
2266 	rtnl_unlock();
2267 	return 0;
2268 
2269 if_create_err:
2270 	destroy_workqueue(interface->timer_work_queue);
2271 ifput_err:
2272 	bnx2fc_net_cleanup(interface);
2273 	bnx2fc_interface_put(interface);
2274 	goto mod_err;
2275 netdev_err:
2276 	module_put(THIS_MODULE);
2277 mod_err:
2278 	mutex_unlock(&bnx2fc_dev_lock);
2279 	rtnl_unlock();
2280 	return rc;
2281 }
2282 
2283 /**
2284  * bnx2fc_create() - Create a bnx2fc interface
2285  * @netdev  : The net_device object the Ethernet interface to create on
2286  * @fip_mode: The FIP mode for this creation
2287  *
2288  * Called from fcoe transport
2289  *
2290  * Returns: 0 for success
2291  */
2292 static int bnx2fc_create(struct net_device *netdev, enum fip_state fip_mode)
2293 {
2294 	return _bnx2fc_create(netdev, fip_mode, BNX2FC_CREATE_LINK_UP);
2295 }
2296 
2297 /**
2298  * bnx2fc_ctlr_alloc() - Allocate a bnx2fc interface from fcoe_sysfs
2299  * @netdev: The net_device to be used by the allocated FCoE Controller
2300  *
2301  * This routine is called from fcoe_sysfs. It will start the fcoe_ctlr
2302  * in a link_down state. The allows the user an opportunity to configure
2303  * the FCoE Controller from sysfs before enabling the FCoE Controller.
2304  *
2305  * Creating in with this routine starts the FCoE Controller in Fabric
2306  * mode. The user can change to VN2VN or another mode before enabling.
2307  */
2308 static int bnx2fc_ctlr_alloc(struct net_device *netdev)
2309 {
2310 	return _bnx2fc_create(netdev, FIP_MODE_FABRIC,
2311 			      BNX2FC_CREATE_LINK_DOWN);
2312 }
2313 
2314 /**
2315  * bnx2fc_find_hba_for_cnic - maps cnic instance to bnx2fc hba instance
2316  *
2317  * @cnic:	Pointer to cnic device instance
2318  *
2319  **/
2320 static struct bnx2fc_hba *bnx2fc_find_hba_for_cnic(struct cnic_dev *cnic)
2321 {
2322 	struct bnx2fc_hba *hba;
2323 
2324 	/* Called with bnx2fc_dev_lock held */
2325 	list_for_each_entry(hba, &adapter_list, list) {
2326 		if (hba->cnic == cnic)
2327 			return hba;
2328 	}
2329 	return NULL;
2330 }
2331 
2332 static struct bnx2fc_interface *bnx2fc_interface_lookup(struct net_device
2333 							*netdev)
2334 {
2335 	struct bnx2fc_interface *interface;
2336 
2337 	/* Called with bnx2fc_dev_lock held */
2338 	list_for_each_entry(interface, &if_list, list) {
2339 		if (interface->netdev == netdev)
2340 			return interface;
2341 	}
2342 	return NULL;
2343 }
2344 
2345 static struct bnx2fc_hba *bnx2fc_hba_lookup(struct net_device
2346 						      *phys_dev)
2347 {
2348 	struct bnx2fc_hba *hba;
2349 
2350 	/* Called with bnx2fc_dev_lock held */
2351 	list_for_each_entry(hba, &adapter_list, list) {
2352 		if (hba->phys_dev == phys_dev)
2353 			return hba;
2354 	}
2355 	printk(KERN_ERR PFX "adapter_lookup: hba NULL\n");
2356 	return NULL;
2357 }
2358 
2359 /**
2360  * bnx2fc_ulp_exit - shuts down adapter instance and frees all resources
2361  *
2362  * @dev		cnic device handle
2363  */
2364 static void bnx2fc_ulp_exit(struct cnic_dev *dev)
2365 {
2366 	struct bnx2fc_hba *hba;
2367 	struct bnx2fc_interface *interface, *tmp;
2368 
2369 	BNX2FC_MISC_DBG("Entered bnx2fc_ulp_exit\n");
2370 
2371 	if (!test_bit(CNIC_F_BNX2X_CLASS, &dev->flags)) {
2372 		printk(KERN_ERR PFX "bnx2fc port check: %s, flags: %lx\n",
2373 			dev->netdev->name, dev->flags);
2374 		return;
2375 	}
2376 
2377 	mutex_lock(&bnx2fc_dev_lock);
2378 	hba = bnx2fc_find_hba_for_cnic(dev);
2379 	if (!hba) {
2380 		printk(KERN_ERR PFX "bnx2fc_ulp_exit: hba not found, dev 0%p\n",
2381 		       dev);
2382 		mutex_unlock(&bnx2fc_dev_lock);
2383 		return;
2384 	}
2385 
2386 	list_del_init(&hba->list);
2387 	adapter_count--;
2388 
2389 	list_for_each_entry_safe(interface, tmp, &if_list, list)
2390 		/* destroy not called yet, move to quiesced list */
2391 		if (interface->hba == hba)
2392 			__bnx2fc_destroy(interface);
2393 	mutex_unlock(&bnx2fc_dev_lock);
2394 
2395 	/* Ensure ALL destroy work has been completed before return */
2396 	flush_workqueue(bnx2fc_wq);
2397 
2398 	bnx2fc_ulp_stop(hba);
2399 	/* unregister cnic device */
2400 	if (test_and_clear_bit(BNX2FC_CNIC_REGISTERED, &hba->reg_with_cnic))
2401 		hba->cnic->unregister_device(hba->cnic, CNIC_ULP_FCOE);
2402 	bnx2fc_hba_destroy(hba);
2403 }
2404 
2405 /**
2406  * bnx2fc_fcoe_reset - Resets the fcoe
2407  *
2408  * @shost: shost the reset is from
2409  *
2410  * Returns: always 0
2411  */
2412 static int bnx2fc_fcoe_reset(struct Scsi_Host *shost)
2413 {
2414 	struct fc_lport *lport = shost_priv(shost);
2415 	fc_lport_reset(lport);
2416 	return 0;
2417 }
2418 
2419 
2420 static bool bnx2fc_match(struct net_device *netdev)
2421 {
2422 	struct net_device *phys_dev = netdev;
2423 
2424 	mutex_lock(&bnx2fc_dev_lock);
2425 	if (netdev->priv_flags & IFF_802_1Q_VLAN)
2426 		phys_dev = vlan_dev_real_dev(netdev);
2427 
2428 	if (bnx2fc_hba_lookup(phys_dev)) {
2429 		mutex_unlock(&bnx2fc_dev_lock);
2430 		return true;
2431 	}
2432 
2433 	mutex_unlock(&bnx2fc_dev_lock);
2434 	return false;
2435 }
2436 
2437 
2438 static struct fcoe_transport bnx2fc_transport = {
2439 	.name = {"bnx2fc"},
2440 	.attached = false,
2441 	.list = LIST_HEAD_INIT(bnx2fc_transport.list),
2442 	.alloc = bnx2fc_ctlr_alloc,
2443 	.match = bnx2fc_match,
2444 	.create = bnx2fc_create,
2445 	.destroy = bnx2fc_destroy,
2446 	.enable = bnx2fc_enable,
2447 	.disable = bnx2fc_disable,
2448 };
2449 
2450 /**
2451  * bnx2fc_percpu_thread_create - Create a receive thread for an
2452  *				 online CPU
2453  *
2454  * @cpu: cpu index for the online cpu
2455  */
2456 static void bnx2fc_percpu_thread_create(unsigned int cpu)
2457 {
2458 	struct bnx2fc_percpu_s *p;
2459 	struct task_struct *thread;
2460 
2461 	p = &per_cpu(bnx2fc_percpu, cpu);
2462 
2463 	thread = kthread_create_on_node(bnx2fc_percpu_io_thread,
2464 					(void *)p, cpu_to_node(cpu),
2465 					"bnx2fc_thread/%d", cpu);
2466 	/* bind thread to the cpu */
2467 	if (likely(!IS_ERR(thread))) {
2468 		kthread_bind(thread, cpu);
2469 		p->iothread = thread;
2470 		wake_up_process(thread);
2471 	}
2472 }
2473 
2474 static void bnx2fc_percpu_thread_destroy(unsigned int cpu)
2475 {
2476 	struct bnx2fc_percpu_s *p;
2477 	struct task_struct *thread;
2478 	struct bnx2fc_work *work, *tmp;
2479 
2480 	BNX2FC_MISC_DBG("destroying io thread for CPU %d\n", cpu);
2481 
2482 	/* Prevent any new work from being queued for this CPU */
2483 	p = &per_cpu(bnx2fc_percpu, cpu);
2484 	spin_lock_bh(&p->fp_work_lock);
2485 	thread = p->iothread;
2486 	p->iothread = NULL;
2487 
2488 
2489 	/* Free all work in the list */
2490 	list_for_each_entry_safe(work, tmp, &p->work_list, list) {
2491 		list_del_init(&work->list);
2492 		bnx2fc_process_cq_compl(work->tgt, work->wqe);
2493 		kfree(work);
2494 	}
2495 
2496 	spin_unlock_bh(&p->fp_work_lock);
2497 
2498 	if (thread)
2499 		kthread_stop(thread);
2500 }
2501 
2502 /**
2503  * bnx2fc_cpu_callback - Handler for CPU hotplug events
2504  *
2505  * @nfb:    The callback data block
2506  * @action: The event triggering the callback
2507  * @hcpu:   The index of the CPU that the event is for
2508  *
2509  * This creates or destroys per-CPU data for fcoe
2510  *
2511  * Returns NOTIFY_OK always.
2512  */
2513 static int bnx2fc_cpu_callback(struct notifier_block *nfb,
2514 			     unsigned long action, void *hcpu)
2515 {
2516 	unsigned cpu = (unsigned long)hcpu;
2517 
2518 	switch (action) {
2519 	case CPU_ONLINE:
2520 	case CPU_ONLINE_FROZEN:
2521 		printk(PFX "CPU %x online: Create Rx thread\n", cpu);
2522 		bnx2fc_percpu_thread_create(cpu);
2523 		break;
2524 	case CPU_DEAD:
2525 	case CPU_DEAD_FROZEN:
2526 		printk(PFX "CPU %x offline: Remove Rx thread\n", cpu);
2527 		bnx2fc_percpu_thread_destroy(cpu);
2528 		break;
2529 	default:
2530 		break;
2531 	}
2532 	return NOTIFY_OK;
2533 }
2534 
2535 /**
2536  * bnx2fc_mod_init - module init entry point
2537  *
2538  * Initialize driver wide global data structures, and register
2539  * with cnic module
2540  **/
2541 static int __init bnx2fc_mod_init(void)
2542 {
2543 	struct fcoe_percpu_s *bg;
2544 	struct task_struct *l2_thread;
2545 	int rc = 0;
2546 	unsigned int cpu = 0;
2547 	struct bnx2fc_percpu_s *p;
2548 
2549 	printk(KERN_INFO PFX "%s", version);
2550 
2551 	/* register as a fcoe transport */
2552 	rc = fcoe_transport_attach(&bnx2fc_transport);
2553 	if (rc) {
2554 		printk(KERN_ERR "failed to register an fcoe transport, check "
2555 			"if libfcoe is loaded\n");
2556 		goto out;
2557 	}
2558 
2559 	INIT_LIST_HEAD(&adapter_list);
2560 	INIT_LIST_HEAD(&if_list);
2561 	mutex_init(&bnx2fc_dev_lock);
2562 	adapter_count = 0;
2563 
2564 	/* Attach FC transport template */
2565 	rc = bnx2fc_attach_transport();
2566 	if (rc)
2567 		goto detach_ft;
2568 
2569 	bnx2fc_wq = alloc_workqueue("bnx2fc", 0, 0);
2570 	if (!bnx2fc_wq) {
2571 		rc = -ENOMEM;
2572 		goto release_bt;
2573 	}
2574 
2575 	bg = &bnx2fc_global;
2576 	skb_queue_head_init(&bg->fcoe_rx_list);
2577 	l2_thread = kthread_create(bnx2fc_l2_rcv_thread,
2578 				   (void *)bg,
2579 				   "bnx2fc_l2_thread");
2580 	if (IS_ERR(l2_thread)) {
2581 		rc = PTR_ERR(l2_thread);
2582 		goto free_wq;
2583 	}
2584 	wake_up_process(l2_thread);
2585 	spin_lock_bh(&bg->fcoe_rx_list.lock);
2586 	bg->thread = l2_thread;
2587 	spin_unlock_bh(&bg->fcoe_rx_list.lock);
2588 
2589 	for_each_possible_cpu(cpu) {
2590 		p = &per_cpu(bnx2fc_percpu, cpu);
2591 		INIT_LIST_HEAD(&p->work_list);
2592 		spin_lock_init(&p->fp_work_lock);
2593 	}
2594 
2595 	cpu_notifier_register_begin();
2596 
2597 	for_each_online_cpu(cpu) {
2598 		bnx2fc_percpu_thread_create(cpu);
2599 	}
2600 
2601 	/* Initialize per CPU interrupt thread */
2602 	__register_hotcpu_notifier(&bnx2fc_cpu_notifier);
2603 
2604 	cpu_notifier_register_done();
2605 
2606 	cnic_register_driver(CNIC_ULP_FCOE, &bnx2fc_cnic_cb);
2607 
2608 	return 0;
2609 
2610 free_wq:
2611 	destroy_workqueue(bnx2fc_wq);
2612 release_bt:
2613 	bnx2fc_release_transport();
2614 detach_ft:
2615 	fcoe_transport_detach(&bnx2fc_transport);
2616 out:
2617 	return rc;
2618 }
2619 
2620 static void __exit bnx2fc_mod_exit(void)
2621 {
2622 	LIST_HEAD(to_be_deleted);
2623 	struct bnx2fc_hba *hba, *next;
2624 	struct fcoe_percpu_s *bg;
2625 	struct task_struct *l2_thread;
2626 	struct sk_buff *skb;
2627 	unsigned int cpu = 0;
2628 
2629 	/*
2630 	 * NOTE: Since cnic calls register_driver routine rtnl_lock,
2631 	 * it will have higher precedence than bnx2fc_dev_lock.
2632 	 * unregister_device() cannot be called with bnx2fc_dev_lock
2633 	 * held.
2634 	 */
2635 	mutex_lock(&bnx2fc_dev_lock);
2636 	list_splice(&adapter_list, &to_be_deleted);
2637 	INIT_LIST_HEAD(&adapter_list);
2638 	adapter_count = 0;
2639 	mutex_unlock(&bnx2fc_dev_lock);
2640 
2641 	/* Unregister with cnic */
2642 	list_for_each_entry_safe(hba, next, &to_be_deleted, list) {
2643 		list_del_init(&hba->list);
2644 		printk(KERN_ERR PFX "MOD_EXIT:destroy hba = 0x%p\n",
2645 		       hba);
2646 		bnx2fc_ulp_stop(hba);
2647 		/* unregister cnic device */
2648 		if (test_and_clear_bit(BNX2FC_CNIC_REGISTERED,
2649 				       &hba->reg_with_cnic))
2650 			hba->cnic->unregister_device(hba->cnic,
2651 							 CNIC_ULP_FCOE);
2652 		bnx2fc_hba_destroy(hba);
2653 	}
2654 	cnic_unregister_driver(CNIC_ULP_FCOE);
2655 
2656 	/* Destroy global thread */
2657 	bg = &bnx2fc_global;
2658 	spin_lock_bh(&bg->fcoe_rx_list.lock);
2659 	l2_thread = bg->thread;
2660 	bg->thread = NULL;
2661 	while ((skb = __skb_dequeue(&bg->fcoe_rx_list)) != NULL)
2662 		kfree_skb(skb);
2663 
2664 	spin_unlock_bh(&bg->fcoe_rx_list.lock);
2665 
2666 	if (l2_thread)
2667 		kthread_stop(l2_thread);
2668 
2669 	cpu_notifier_register_begin();
2670 
2671 	/* Destroy per cpu threads */
2672 	for_each_online_cpu(cpu) {
2673 		bnx2fc_percpu_thread_destroy(cpu);
2674 	}
2675 
2676 	__unregister_hotcpu_notifier(&bnx2fc_cpu_notifier);
2677 
2678 	cpu_notifier_register_done();
2679 
2680 	destroy_workqueue(bnx2fc_wq);
2681 	/*
2682 	 * detach from scsi transport
2683 	 * must happen after all destroys are done
2684 	 */
2685 	bnx2fc_release_transport();
2686 
2687 	/* detach from fcoe transport */
2688 	fcoe_transport_detach(&bnx2fc_transport);
2689 }
2690 
2691 module_init(bnx2fc_mod_init);
2692 module_exit(bnx2fc_mod_exit);
2693 
2694 static struct fcoe_sysfs_function_template bnx2fc_fcoe_sysfs_templ = {
2695 	.set_fcoe_ctlr_enabled = bnx2fc_ctlr_enabled,
2696 	.get_fcoe_ctlr_link_fail = fcoe_ctlr_get_lesb,
2697 	.get_fcoe_ctlr_vlink_fail = fcoe_ctlr_get_lesb,
2698 	.get_fcoe_ctlr_miss_fka = fcoe_ctlr_get_lesb,
2699 	.get_fcoe_ctlr_symb_err = fcoe_ctlr_get_lesb,
2700 	.get_fcoe_ctlr_err_block = fcoe_ctlr_get_lesb,
2701 	.get_fcoe_ctlr_fcs_error = fcoe_ctlr_get_lesb,
2702 
2703 	.get_fcoe_fcf_selected = fcoe_fcf_get_selected,
2704 	.get_fcoe_fcf_vlan_id = bnx2fc_fcf_get_vlan_id,
2705 };
2706 
2707 static struct fc_function_template bnx2fc_transport_function = {
2708 	.show_host_node_name = 1,
2709 	.show_host_port_name = 1,
2710 	.show_host_supported_classes = 1,
2711 	.show_host_supported_fc4s = 1,
2712 	.show_host_active_fc4s = 1,
2713 	.show_host_maxframe_size = 1,
2714 
2715 	.show_host_port_id = 1,
2716 	.show_host_supported_speeds = 1,
2717 	.get_host_speed = fc_get_host_speed,
2718 	.show_host_speed = 1,
2719 	.show_host_port_type = 1,
2720 	.get_host_port_state = fc_get_host_port_state,
2721 	.show_host_port_state = 1,
2722 	.show_host_symbolic_name = 1,
2723 
2724 	.dd_fcrport_size = (sizeof(struct fc_rport_libfc_priv) +
2725 				sizeof(struct bnx2fc_rport)),
2726 	.show_rport_maxframe_size = 1,
2727 	.show_rport_supported_classes = 1,
2728 
2729 	.show_host_fabric_name = 1,
2730 	.show_starget_node_name = 1,
2731 	.show_starget_port_name = 1,
2732 	.show_starget_port_id = 1,
2733 	.set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
2734 	.show_rport_dev_loss_tmo = 1,
2735 	.get_fc_host_stats = bnx2fc_get_host_stats,
2736 
2737 	.issue_fc_host_lip = bnx2fc_fcoe_reset,
2738 
2739 	.terminate_rport_io = fc_rport_terminate_io,
2740 
2741 	.vport_create = bnx2fc_vport_create,
2742 	.vport_delete = bnx2fc_vport_destroy,
2743 	.vport_disable = bnx2fc_vport_disable,
2744 	.bsg_request = fc_lport_bsg_request,
2745 };
2746 
2747 static struct fc_function_template bnx2fc_vport_xport_function = {
2748 	.show_host_node_name = 1,
2749 	.show_host_port_name = 1,
2750 	.show_host_supported_classes = 1,
2751 	.show_host_supported_fc4s = 1,
2752 	.show_host_active_fc4s = 1,
2753 	.show_host_maxframe_size = 1,
2754 
2755 	.show_host_port_id = 1,
2756 	.show_host_supported_speeds = 1,
2757 	.get_host_speed = fc_get_host_speed,
2758 	.show_host_speed = 1,
2759 	.show_host_port_type = 1,
2760 	.get_host_port_state = fc_get_host_port_state,
2761 	.show_host_port_state = 1,
2762 	.show_host_symbolic_name = 1,
2763 
2764 	.dd_fcrport_size = (sizeof(struct fc_rport_libfc_priv) +
2765 				sizeof(struct bnx2fc_rport)),
2766 	.show_rport_maxframe_size = 1,
2767 	.show_rport_supported_classes = 1,
2768 
2769 	.show_host_fabric_name = 1,
2770 	.show_starget_node_name = 1,
2771 	.show_starget_port_name = 1,
2772 	.show_starget_port_id = 1,
2773 	.set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
2774 	.show_rport_dev_loss_tmo = 1,
2775 	.get_fc_host_stats = fc_get_host_stats,
2776 	.issue_fc_host_lip = bnx2fc_fcoe_reset,
2777 	.terminate_rport_io = fc_rport_terminate_io,
2778 	.bsg_request = fc_lport_bsg_request,
2779 };
2780 
2781 /**
2782  * scsi_host_template structure used while registering with SCSI-ml
2783  */
2784 static struct scsi_host_template bnx2fc_shost_template = {
2785 	.module			= THIS_MODULE,
2786 	.name			= "Broadcom Offload FCoE Initiator",
2787 	.queuecommand		= bnx2fc_queuecommand,
2788 	.eh_abort_handler	= bnx2fc_eh_abort,	  /* abts */
2789 	.eh_device_reset_handler = bnx2fc_eh_device_reset, /* lun reset */
2790 	.eh_target_reset_handler = bnx2fc_eh_target_reset, /* tgt reset */
2791 	.eh_host_reset_handler	= fc_eh_host_reset,
2792 	.slave_alloc		= fc_slave_alloc,
2793 	.change_queue_depth	= fc_change_queue_depth,
2794 	.change_queue_type	= fc_change_queue_type,
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 };
2801 
2802 static struct libfc_function_template bnx2fc_libfc_fcn_templ = {
2803 	.frame_send		= bnx2fc_xmit,
2804 	.elsct_send		= bnx2fc_elsct_send,
2805 	.fcp_abort_io		= bnx2fc_abort_io,
2806 	.fcp_cleanup		= bnx2fc_cleanup,
2807 	.get_lesb		= fcoe_get_lesb,
2808 	.rport_event_callback	= bnx2fc_rport_event_handler,
2809 };
2810 
2811 /**
2812  * bnx2fc_cnic_cb - global template of bnx2fc - cnic driver interface
2813  *			structure carrying callback function pointers
2814  */
2815 static struct cnic_ulp_ops bnx2fc_cnic_cb = {
2816 	.owner			= THIS_MODULE,
2817 	.cnic_init		= bnx2fc_ulp_init,
2818 	.cnic_exit		= bnx2fc_ulp_exit,
2819 	.cnic_start		= bnx2fc_ulp_start,
2820 	.cnic_stop		= bnx2fc_ulp_stop,
2821 	.indicate_kcqes		= bnx2fc_indicate_kcqe,
2822 	.indicate_netevent	= bnx2fc_indicate_netevent,
2823 	.cnic_get_stats		= bnx2fc_ulp_get_stats,
2824 };
2825