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