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