1 /* bnx2fc_fcoe.c: QLogic Linux FCoE offload driver.
2  * This file contains the code that interacts with libfc, libfcoe,
3  * cnic modules to create FCoE instances, send/receive non-offloaded
4  * FIP/FCoE packets, listen to link events etc.
5  *
6  * Copyright (c) 2008-2013 Broadcom Corporation
7  * Copyright (c) 2014-2016 QLogic Corporation
8  * Copyright (c) 2016-2017 Cavium Inc.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation.
13  *
14  * Written by: Bhanu Prakash Gollapudi (bprakash@broadcom.com)
15  */
16 
17 #include "bnx2fc.h"
18 
19 static struct list_head adapter_list;
20 static struct list_head if_list;
21 static u32 adapter_count;
22 static DEFINE_MUTEX(bnx2fc_dev_lock);
23 DEFINE_PER_CPU(struct bnx2fc_percpu_s, bnx2fc_percpu);
24 
25 #define DRV_MODULE_NAME		"bnx2fc"
26 #define DRV_MODULE_VERSION	BNX2FC_VERSION
27 #define DRV_MODULE_RELDATE	"October 15, 2015"
28 
29 
30 static char version[] =
31 		"QLogic FCoE Driver " DRV_MODULE_NAME \
32 		" v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")\n";
33 
34 
35 MODULE_AUTHOR("Bhanu Prakash Gollapudi <bprakash@broadcom.com>");
36 MODULE_DESCRIPTION("QLogic FCoE Driver");
37 MODULE_LICENSE("GPL");
38 MODULE_VERSION(DRV_MODULE_VERSION);
39 
40 #define BNX2FC_MAX_QUEUE_DEPTH	256
41 #define BNX2FC_MIN_QUEUE_DEPTH	32
42 #define FCOE_WORD_TO_BYTE  4
43 
44 static struct scsi_transport_template	*bnx2fc_transport_template;
45 static struct scsi_transport_template	*bnx2fc_vport_xport_template;
46 
47 struct workqueue_struct *bnx2fc_wq;
48 
49 /* bnx2fc structure needs only one instance of the fcoe_percpu_s structure.
50  * Here the io threads are per cpu but the l2 thread is just one
51  */
52 struct fcoe_percpu_s bnx2fc_global;
53 static DEFINE_SPINLOCK(bnx2fc_global_lock);
54 
55 static struct cnic_ulp_ops bnx2fc_cnic_cb;
56 static struct libfc_function_template bnx2fc_libfc_fcn_templ;
57 static struct scsi_host_template bnx2fc_shost_template;
58 static struct fc_function_template bnx2fc_transport_function;
59 static struct fcoe_sysfs_function_template bnx2fc_fcoe_sysfs_templ;
60 static struct fc_function_template bnx2fc_vport_xport_function;
61 static int bnx2fc_create(struct net_device *netdev, enum fip_mode fip_mode);
62 static void __bnx2fc_destroy(struct bnx2fc_interface *interface);
63 static int bnx2fc_destroy(struct net_device *net_device);
64 static int bnx2fc_enable(struct net_device *netdev);
65 static int bnx2fc_disable(struct net_device *netdev);
66 
67 /* fcoe_syfs control interface handlers */
68 static int bnx2fc_ctlr_alloc(struct net_device *netdev);
69 static int bnx2fc_ctlr_enabled(struct fcoe_ctlr_device *cdev);
70 
71 static void bnx2fc_recv_frame(struct sk_buff *skb);
72 
73 static void bnx2fc_start_disc(struct bnx2fc_interface *interface);
74 static int bnx2fc_shost_config(struct fc_lport *lport, struct device *dev);
75 static int bnx2fc_lport_config(struct fc_lport *lport);
76 static int bnx2fc_em_config(struct fc_lport *lport, struct bnx2fc_hba *hba);
77 static int bnx2fc_bind_adapter_devices(struct bnx2fc_hba *hba);
78 static void bnx2fc_unbind_adapter_devices(struct bnx2fc_hba *hba);
79 static int bnx2fc_bind_pcidev(struct bnx2fc_hba *hba);
80 static void bnx2fc_unbind_pcidev(struct bnx2fc_hba *hba);
81 static struct fc_lport *bnx2fc_if_create(struct bnx2fc_interface *interface,
82 				  struct device *parent, int npiv);
83 static void bnx2fc_destroy_work(struct work_struct *work);
84 
85 static struct bnx2fc_hba *bnx2fc_hba_lookup(struct net_device *phys_dev);
86 static struct bnx2fc_interface *bnx2fc_interface_lookup(struct net_device
87 							*phys_dev);
88 static inline void bnx2fc_interface_put(struct bnx2fc_interface *interface);
89 static struct bnx2fc_hba *bnx2fc_find_hba_for_cnic(struct cnic_dev *cnic);
90 
91 static int bnx2fc_fw_init(struct bnx2fc_hba *hba);
92 static void bnx2fc_fw_destroy(struct bnx2fc_hba *hba);
93 
94 static void bnx2fc_port_shutdown(struct fc_lport *lport);
95 static void bnx2fc_stop(struct bnx2fc_interface *interface);
96 static int __init bnx2fc_mod_init(void);
97 static void __exit bnx2fc_mod_exit(void);
98 
99 unsigned int bnx2fc_debug_level;
100 module_param_named(debug_logging, bnx2fc_debug_level, int, S_IRUGO|S_IWUSR);
101 MODULE_PARM_DESC(debug_logging,
102 		"Option to enable extended logging,\n"
103 		"\t\tDefault is 0 - no logging.\n"
104 		"\t\t0x01 - SCSI cmd error, cleanup.\n"
105 		"\t\t0x02 - Session setup, cleanup, etc.\n"
106 		"\t\t0x04 - lport events, link, mtu, etc.\n"
107 		"\t\t0x08 - ELS logs.\n"
108 		"\t\t0x10 - fcoe L2 fame related logs.\n"
109 		"\t\t0xff - LOG all messages.");
110 
111 static uint bnx2fc_devloss_tmo;
112 module_param_named(devloss_tmo, bnx2fc_devloss_tmo, uint, S_IRUGO);
113 MODULE_PARM_DESC(devloss_tmo, " Change devloss_tmo for the remote ports "
114 	"attached via bnx2fc.");
115 
116 static uint bnx2fc_max_luns = BNX2FC_MAX_LUN;
117 module_param_named(max_luns, bnx2fc_max_luns, uint, S_IRUGO);
118 MODULE_PARM_DESC(max_luns, " Change the default max_lun per SCSI host. Default "
119 	"0xffff.");
120 
121 static uint bnx2fc_queue_depth;
122 module_param_named(queue_depth, bnx2fc_queue_depth, uint, S_IRUGO);
123 MODULE_PARM_DESC(queue_depth, " Change the default queue depth of SCSI devices "
124 	"attached via bnx2fc.");
125 
126 static uint bnx2fc_log_fka;
127 module_param_named(log_fka, bnx2fc_log_fka, uint, S_IRUGO|S_IWUSR);
128 MODULE_PARM_DESC(log_fka, " Print message to kernel log when fcoe is "
129 	"initiating a FIP keep alive when debug logging is enabled.");
130 
131 static inline struct net_device *bnx2fc_netdev(const struct fc_lport *lport)
132 {
133 	return ((struct bnx2fc_interface *)
134 		((struct fcoe_port *)lport_priv(lport))->priv)->netdev;
135 }
136 
137 static void bnx2fc_fcf_get_vlan_id(struct fcoe_fcf_device *fcf_dev)
138 {
139 	struct fcoe_ctlr_device *ctlr_dev =
140 		fcoe_fcf_dev_to_ctlr_dev(fcf_dev);
141 	struct fcoe_ctlr *ctlr = fcoe_ctlr_device_priv(ctlr_dev);
142 	struct bnx2fc_interface *fcoe = fcoe_ctlr_priv(ctlr);
143 
144 	fcf_dev->vlan_id = fcoe->vlan_id;
145 }
146 
147 static void bnx2fc_clean_rx_queue(struct fc_lport *lp)
148 {
149 	struct fcoe_percpu_s *bg;
150 	struct fcoe_rcv_info *fr;
151 	struct sk_buff_head *list;
152 	struct sk_buff *skb, *next;
153 
154 	bg = &bnx2fc_global;
155 	spin_lock_bh(&bg->fcoe_rx_list.lock);
156 	list = &bg->fcoe_rx_list;
157 	skb_queue_walk_safe(list, skb, next) {
158 		fr = fcoe_dev_from_skb(skb);
159 		if (fr->fr_dev == lp) {
160 			__skb_unlink(skb, list);
161 			kfree_skb(skb);
162 		}
163 	}
164 	spin_unlock_bh(&bg->fcoe_rx_list.lock);
165 }
166 
167 int bnx2fc_get_paged_crc_eof(struct sk_buff *skb, int tlen)
168 {
169 	int rc;
170 	spin_lock(&bnx2fc_global_lock);
171 	rc = fcoe_get_paged_crc_eof(skb, tlen, &bnx2fc_global);
172 	spin_unlock(&bnx2fc_global_lock);
173 
174 	return rc;
175 }
176 
177 static void bnx2fc_abort_io(struct fc_lport *lport)
178 {
179 	/*
180 	 * This function is no-op for bnx2fc, but we do
181 	 * not want to leave it as NULL either, as libfc
182 	 * can call the default function which is
183 	 * fc_fcp_abort_io.
184 	 */
185 }
186 
187 static void bnx2fc_cleanup(struct fc_lport *lport)
188 {
189 	struct fcoe_port *port = lport_priv(lport);
190 	struct bnx2fc_interface *interface = port->priv;
191 	struct bnx2fc_hba *hba = interface->hba;
192 	struct bnx2fc_rport *tgt;
193 	int i;
194 
195 	BNX2FC_MISC_DBG("Entered %s\n", __func__);
196 	mutex_lock(&hba->hba_mutex);
197 	spin_lock_bh(&hba->hba_lock);
198 	for (i = 0; i < BNX2FC_NUM_MAX_SESS; i++) {
199 		tgt = hba->tgt_ofld_list[i];
200 		if (tgt) {
201 			/* Cleanup IOs belonging to requested vport */
202 			if (tgt->port == port) {
203 				spin_unlock_bh(&hba->hba_lock);
204 				BNX2FC_TGT_DBG(tgt, "flush/cleanup\n");
205 				bnx2fc_flush_active_ios(tgt);
206 				spin_lock_bh(&hba->hba_lock);
207 			}
208 		}
209 	}
210 	spin_unlock_bh(&hba->hba_lock);
211 	mutex_unlock(&hba->hba_mutex);
212 }
213 
214 static int bnx2fc_xmit_l2_frame(struct bnx2fc_rport *tgt,
215 			     struct fc_frame *fp)
216 {
217 	struct fc_rport_priv *rdata = tgt->rdata;
218 	struct fc_frame_header *fh;
219 	int rc = 0;
220 
221 	fh = fc_frame_header_get(fp);
222 	BNX2FC_TGT_DBG(tgt, "Xmit L2 frame rport = 0x%x, oxid = 0x%x, "
223 			"r_ctl = 0x%x\n", rdata->ids.port_id,
224 			ntohs(fh->fh_ox_id), fh->fh_r_ctl);
225 	if ((fh->fh_type == FC_TYPE_ELS) &&
226 	    (fh->fh_r_ctl == FC_RCTL_ELS_REQ)) {
227 
228 		switch (fc_frame_payload_op(fp)) {
229 		case ELS_ADISC:
230 			rc = bnx2fc_send_adisc(tgt, fp);
231 			break;
232 		case ELS_LOGO:
233 			rc = bnx2fc_send_logo(tgt, fp);
234 			break;
235 		case ELS_RLS:
236 			rc = bnx2fc_send_rls(tgt, fp);
237 			break;
238 		default:
239 			break;
240 		}
241 	} else if ((fh->fh_type ==  FC_TYPE_BLS) &&
242 	    (fh->fh_r_ctl == FC_RCTL_BA_ABTS))
243 		BNX2FC_TGT_DBG(tgt, "ABTS frame\n");
244 	else {
245 		BNX2FC_TGT_DBG(tgt, "Send L2 frame type 0x%x "
246 				"rctl 0x%x thru non-offload path\n",
247 				fh->fh_type, fh->fh_r_ctl);
248 		return -ENODEV;
249 	}
250 	if (rc)
251 		return -ENOMEM;
252 	else
253 		return 0;
254 }
255 
256 /**
257  * bnx2fc_xmit - bnx2fc's FCoE frame transmit function
258  *
259  * @lport:	the associated local port
260  * @fp:	the fc_frame to be transmitted
261  */
262 static int bnx2fc_xmit(struct fc_lport *lport, struct fc_frame *fp)
263 {
264 	struct ethhdr		*eh;
265 	struct fcoe_crc_eof	*cp;
266 	struct sk_buff		*skb;
267 	struct fc_frame_header	*fh;
268 	struct bnx2fc_interface	*interface;
269 	struct fcoe_ctlr        *ctlr;
270 	struct bnx2fc_hba *hba;
271 	struct fcoe_port	*port;
272 	struct fcoe_hdr		*hp;
273 	struct bnx2fc_rport	*tgt;
274 	struct fc_stats		*stats;
275 	u8			sof, eof;
276 	u32			crc;
277 	unsigned int		hlen, tlen, elen;
278 	int			wlen, rc = 0;
279 
280 	port = (struct fcoe_port *)lport_priv(lport);
281 	interface = port->priv;
282 	ctlr = bnx2fc_to_ctlr(interface);
283 	hba = interface->hba;
284 
285 	fh = fc_frame_header_get(fp);
286 
287 	skb = fp_skb(fp);
288 	if (!lport->link_up) {
289 		BNX2FC_HBA_DBG(lport, "bnx2fc_xmit link down\n");
290 		kfree_skb(skb);
291 		return 0;
292 	}
293 
294 	if (unlikely(fh->fh_r_ctl == FC_RCTL_ELS_REQ)) {
295 		if (!ctlr->sel_fcf) {
296 			BNX2FC_HBA_DBG(lport, "FCF not selected yet!\n");
297 			kfree_skb(skb);
298 			return -EINVAL;
299 		}
300 		if (fcoe_ctlr_els_send(ctlr, lport, skb))
301 			return 0;
302 	}
303 
304 	sof = fr_sof(fp);
305 	eof = fr_eof(fp);
306 
307 	/*
308 	 * Snoop the frame header to check if the frame is for
309 	 * an offloaded session
310 	 */
311 	/*
312 	 * tgt_ofld_list access is synchronized using
313 	 * both hba mutex and hba lock. Atleast hba mutex or
314 	 * hba lock needs to be held for read access.
315 	 */
316 
317 	spin_lock_bh(&hba->hba_lock);
318 	tgt = bnx2fc_tgt_lookup(port, ntoh24(fh->fh_d_id));
319 	if (tgt && (test_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags))) {
320 		/* This frame is for offloaded session */
321 		BNX2FC_HBA_DBG(lport, "xmit: Frame is for offloaded session "
322 				"port_id = 0x%x\n", ntoh24(fh->fh_d_id));
323 		spin_unlock_bh(&hba->hba_lock);
324 		rc = bnx2fc_xmit_l2_frame(tgt, fp);
325 		if (rc != -ENODEV) {
326 			kfree_skb(skb);
327 			return rc;
328 		}
329 	} else {
330 		spin_unlock_bh(&hba->hba_lock);
331 	}
332 
333 	elen = sizeof(struct ethhdr);
334 	hlen = sizeof(struct fcoe_hdr);
335 	tlen = sizeof(struct fcoe_crc_eof);
336 	wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
337 
338 	skb->ip_summed = CHECKSUM_NONE;
339 	crc = fcoe_fc_crc(fp);
340 
341 	/* copy port crc and eof to the skb buff */
342 	if (skb_is_nonlinear(skb)) {
343 		skb_frag_t *frag;
344 		if (bnx2fc_get_paged_crc_eof(skb, tlen)) {
345 			kfree_skb(skb);
346 			return -ENOMEM;
347 		}
348 		frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
349 		cp = kmap_atomic(skb_frag_page(frag)) + skb_frag_off(frag);
350 	} else {
351 		cp = skb_put(skb, tlen);
352 	}
353 
354 	memset(cp, 0, sizeof(*cp));
355 	cp->fcoe_eof = eof;
356 	cp->fcoe_crc32 = cpu_to_le32(~crc);
357 	if (skb_is_nonlinear(skb)) {
358 		kunmap_atomic(cp);
359 		cp = NULL;
360 	}
361 
362 	/* adjust skb network/transport offsets to match mac/fcoe/port */
363 	skb_push(skb, elen + hlen);
364 	skb_reset_mac_header(skb);
365 	skb_reset_network_header(skb);
366 	skb->mac_len = elen;
367 	skb->protocol = htons(ETH_P_FCOE);
368 	skb->dev = interface->netdev;
369 
370 	/* fill up mac and fcoe headers */
371 	eh = eth_hdr(skb);
372 	eh->h_proto = htons(ETH_P_FCOE);
373 	if (ctlr->map_dest)
374 		fc_fcoe_set_mac(eh->h_dest, fh->fh_d_id);
375 	else
376 		/* insert GW address */
377 		memcpy(eh->h_dest, ctlr->dest_addr, ETH_ALEN);
378 
379 	if (unlikely(ctlr->flogi_oxid != FC_XID_UNKNOWN))
380 		memcpy(eh->h_source, ctlr->ctl_src_addr, ETH_ALEN);
381 	else
382 		memcpy(eh->h_source, port->data_src_addr, ETH_ALEN);
383 
384 	hp = (struct fcoe_hdr *)(eh + 1);
385 	memset(hp, 0, sizeof(*hp));
386 	if (FC_FCOE_VER)
387 		FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
388 	hp->fcoe_sof = sof;
389 
390 	/* fcoe lso, mss is in max_payload which is non-zero for FCP data */
391 	if (lport->seq_offload && fr_max_payload(fp)) {
392 		skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
393 		skb_shinfo(skb)->gso_size = fr_max_payload(fp);
394 	} else {
395 		skb_shinfo(skb)->gso_type = 0;
396 		skb_shinfo(skb)->gso_size = 0;
397 	}
398 
399 	/*update tx stats */
400 	stats = per_cpu_ptr(lport->stats, get_cpu());
401 	stats->TxFrames++;
402 	stats->TxWords += wlen;
403 	put_cpu();
404 
405 	/* send down to lld */
406 	fr_dev(fp) = lport;
407 	if (port->fcoe_pending_queue.qlen)
408 		fcoe_check_wait_queue(lport, skb);
409 	else if (fcoe_start_io(skb))
410 		fcoe_check_wait_queue(lport, skb);
411 
412 	return 0;
413 }
414 
415 /**
416  * bnx2fc_rcv - This is bnx2fc's receive function called by NET_RX_SOFTIRQ
417  *
418  * @skb:	the receive socket buffer
419  * @dev:	associated net device
420  * @ptype:	context
421  * @olddev:	last device
422  *
423  * This function receives the packet and builds FC frame and passes it up
424  */
425 static int bnx2fc_rcv(struct sk_buff *skb, struct net_device *dev,
426 		struct packet_type *ptype, struct net_device *olddev)
427 {
428 	struct fc_lport *lport;
429 	struct bnx2fc_interface *interface;
430 	struct fcoe_ctlr *ctlr;
431 	struct fcoe_rcv_info *fr;
432 	struct fcoe_percpu_s *bg;
433 	struct sk_buff *tmp_skb;
434 
435 	interface = container_of(ptype, struct bnx2fc_interface,
436 				 fcoe_packet_type);
437 	ctlr = bnx2fc_to_ctlr(interface);
438 	lport = ctlr->lp;
439 
440 	if (unlikely(lport == NULL)) {
441 		printk(KERN_ERR PFX "bnx2fc_rcv: lport is NULL\n");
442 		goto err;
443 	}
444 
445 	tmp_skb = skb_share_check(skb, GFP_ATOMIC);
446 	if (!tmp_skb)
447 		goto err;
448 
449 	skb = tmp_skb;
450 
451 	if (unlikely(eth_hdr(skb)->h_proto != htons(ETH_P_FCOE))) {
452 		printk(KERN_ERR PFX "bnx2fc_rcv: Wrong FC type frame\n");
453 		goto err;
454 	}
455 
456 	/*
457 	 * Check for minimum frame length, and make sure required FCoE
458 	 * and FC headers are pulled into the linear data area.
459 	 */
460 	if (unlikely((skb->len < FCOE_MIN_FRAME) ||
461 	    !pskb_may_pull(skb, FCOE_HEADER_LEN)))
462 		goto err;
463 
464 	skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
465 
466 	fr = fcoe_dev_from_skb(skb);
467 	fr->fr_dev = lport;
468 
469 	bg = &bnx2fc_global;
470 	spin_lock(&bg->fcoe_rx_list.lock);
471 
472 	__skb_queue_tail(&bg->fcoe_rx_list, skb);
473 	if (bg->fcoe_rx_list.qlen == 1)
474 		wake_up_process(bg->kthread);
475 
476 	spin_unlock(&bg->fcoe_rx_list.lock);
477 
478 	return 0;
479 err:
480 	kfree_skb(skb);
481 	return -1;
482 }
483 
484 static int bnx2fc_l2_rcv_thread(void *arg)
485 {
486 	struct fcoe_percpu_s *bg = arg;
487 	struct sk_buff *skb;
488 
489 	set_user_nice(current, MIN_NICE);
490 	set_current_state(TASK_INTERRUPTIBLE);
491 	while (!kthread_should_stop()) {
492 		schedule();
493 		spin_lock_bh(&bg->fcoe_rx_list.lock);
494 		while ((skb = __skb_dequeue(&bg->fcoe_rx_list)) != NULL) {
495 			spin_unlock_bh(&bg->fcoe_rx_list.lock);
496 			bnx2fc_recv_frame(skb);
497 			spin_lock_bh(&bg->fcoe_rx_list.lock);
498 		}
499 		__set_current_state(TASK_INTERRUPTIBLE);
500 		spin_unlock_bh(&bg->fcoe_rx_list.lock);
501 	}
502 	__set_current_state(TASK_RUNNING);
503 	return 0;
504 }
505 
506 
507 static void bnx2fc_recv_frame(struct sk_buff *skb)
508 {
509 	u32 fr_len;
510 	struct fc_lport *lport;
511 	struct fcoe_rcv_info *fr;
512 	struct fc_stats *stats;
513 	struct fc_frame_header *fh;
514 	struct fcoe_crc_eof crc_eof;
515 	struct fc_frame *fp;
516 	struct fc_lport *vn_port;
517 	struct fcoe_port *port, *phys_port;
518 	u8 *mac = NULL;
519 	u8 *dest_mac = NULL;
520 	struct fcoe_hdr *hp;
521 	struct bnx2fc_interface *interface;
522 	struct fcoe_ctlr *ctlr;
523 
524 	fr = fcoe_dev_from_skb(skb);
525 	lport = fr->fr_dev;
526 	if (unlikely(lport == NULL)) {
527 		printk(KERN_ERR PFX "Invalid lport struct\n");
528 		kfree_skb(skb);
529 		return;
530 	}
531 
532 	if (skb_is_nonlinear(skb))
533 		skb_linearize(skb);
534 	mac = eth_hdr(skb)->h_source;
535 	dest_mac = eth_hdr(skb)->h_dest;
536 
537 	/* Pull the header */
538 	hp = (struct fcoe_hdr *) skb_network_header(skb);
539 	fh = (struct fc_frame_header *) skb_transport_header(skb);
540 	skb_pull(skb, sizeof(struct fcoe_hdr));
541 	fr_len = skb->len - sizeof(struct fcoe_crc_eof);
542 
543 	fp = (struct fc_frame *)skb;
544 	fc_frame_init(fp);
545 	fr_dev(fp) = lport;
546 	fr_sof(fp) = hp->fcoe_sof;
547 	if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof))) {
548 		kfree_skb(skb);
549 		return;
550 	}
551 	fr_eof(fp) = crc_eof.fcoe_eof;
552 	fr_crc(fp) = crc_eof.fcoe_crc32;
553 	if (pskb_trim(skb, fr_len)) {
554 		kfree_skb(skb);
555 		return;
556 	}
557 
558 	phys_port = lport_priv(lport);
559 	interface = phys_port->priv;
560 	ctlr = bnx2fc_to_ctlr(interface);
561 
562 	fh = fc_frame_header_get(fp);
563 
564 	if (ntoh24(&dest_mac[3]) != ntoh24(fh->fh_d_id)) {
565 		BNX2FC_HBA_DBG(lport, "FC frame d_id mismatch with MAC %pM.\n",
566 		    dest_mac);
567 		kfree_skb(skb);
568 		return;
569 	}
570 
571 	vn_port = fc_vport_id_lookup(lport, ntoh24(fh->fh_d_id));
572 	if (vn_port) {
573 		port = lport_priv(vn_port);
574 		if (!ether_addr_equal(port->data_src_addr, dest_mac)) {
575 			BNX2FC_HBA_DBG(lport, "fpma mismatch\n");
576 			kfree_skb(skb);
577 			return;
578 		}
579 	}
580 	if (ctlr->state) {
581 		if (!ether_addr_equal(mac, ctlr->dest_addr)) {
582 			BNX2FC_HBA_DBG(lport, "Wrong source address: mac:%pM dest_addr:%pM.\n",
583 			    mac, ctlr->dest_addr);
584 			kfree_skb(skb);
585 			return;
586 		}
587 	}
588 	if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA &&
589 	    fh->fh_type == FC_TYPE_FCP) {
590 		/* Drop FCP data. We dont this in L2 path */
591 		kfree_skb(skb);
592 		return;
593 	}
594 	if (fh->fh_r_ctl == FC_RCTL_ELS_REQ &&
595 	    fh->fh_type == FC_TYPE_ELS) {
596 		switch (fc_frame_payload_op(fp)) {
597 		case ELS_LOGO:
598 			if (ntoh24(fh->fh_s_id) == FC_FID_FLOGI) {
599 				/* drop non-FIP LOGO */
600 				kfree_skb(skb);
601 				return;
602 			}
603 			break;
604 		}
605 	}
606 
607 	if (fh->fh_r_ctl == FC_RCTL_BA_ABTS) {
608 		/* Drop incoming ABTS */
609 		kfree_skb(skb);
610 		return;
611 	}
612 
613 	/*
614 	 * If the destination ID from the frame header does not match what we
615 	 * have on record for lport and the search for a NPIV port came up
616 	 * empty then this is not addressed to our port so simply drop it.
617 	 */
618 	if (lport->port_id != ntoh24(fh->fh_d_id) && !vn_port) {
619 		BNX2FC_HBA_DBG(lport, "Dropping frame due to destination mismatch: lport->port_id=%x fh->d_id=%x.\n",
620 		    lport->port_id, ntoh24(fh->fh_d_id));
621 		kfree_skb(skb);
622 		return;
623 	}
624 
625 	stats = per_cpu_ptr(lport->stats, smp_processor_id());
626 	stats->RxFrames++;
627 	stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
628 
629 	if (le32_to_cpu(fr_crc(fp)) !=
630 			~crc32(~0, skb->data, fr_len)) {
631 		if (stats->InvalidCRCCount < 5)
632 			printk(KERN_WARNING PFX "dropping frame with "
633 			       "CRC error\n");
634 		stats->InvalidCRCCount++;
635 		kfree_skb(skb);
636 		return;
637 	}
638 	fc_exch_recv(lport, fp);
639 }
640 
641 /**
642  * bnx2fc_percpu_io_thread - thread per cpu for ios
643  *
644  * @arg:	ptr to bnx2fc_percpu_info structure
645  */
646 static int bnx2fc_percpu_io_thread(void *arg)
647 {
648 	struct bnx2fc_percpu_s *p = arg;
649 	struct bnx2fc_work *work, *tmp;
650 	LIST_HEAD(work_list);
651 
652 	set_user_nice(current, MIN_NICE);
653 	set_current_state(TASK_INTERRUPTIBLE);
654 	while (!kthread_should_stop()) {
655 		schedule();
656 		spin_lock_bh(&p->fp_work_lock);
657 		while (!list_empty(&p->work_list)) {
658 			list_splice_init(&p->work_list, &work_list);
659 			spin_unlock_bh(&p->fp_work_lock);
660 
661 			list_for_each_entry_safe(work, tmp, &work_list, list) {
662 				list_del_init(&work->list);
663 				bnx2fc_process_cq_compl(work->tgt, work->wqe,
664 							work->rq_data,
665 							work->num_rq,
666 							work->task);
667 				kfree(work);
668 			}
669 
670 			spin_lock_bh(&p->fp_work_lock);
671 		}
672 		__set_current_state(TASK_INTERRUPTIBLE);
673 		spin_unlock_bh(&p->fp_work_lock);
674 	}
675 	__set_current_state(TASK_RUNNING);
676 
677 	return 0;
678 }
679 
680 static struct fc_host_statistics *bnx2fc_get_host_stats(struct Scsi_Host *shost)
681 {
682 	struct fc_host_statistics *bnx2fc_stats;
683 	struct fc_lport *lport = shost_priv(shost);
684 	struct fcoe_port *port = lport_priv(lport);
685 	struct bnx2fc_interface *interface = port->priv;
686 	struct bnx2fc_hba *hba = interface->hba;
687 	struct fcoe_statistics_params *fw_stats;
688 	int rc = 0;
689 
690 	fw_stats = (struct fcoe_statistics_params *)hba->stats_buffer;
691 	if (!fw_stats)
692 		return NULL;
693 
694 	mutex_lock(&hba->hba_stats_mutex);
695 
696 	bnx2fc_stats = fc_get_host_stats(shost);
697 
698 	init_completion(&hba->stat_req_done);
699 	if (bnx2fc_send_stat_req(hba))
700 		goto unlock_stats_mutex;
701 	rc = wait_for_completion_timeout(&hba->stat_req_done, (2 * HZ));
702 	if (!rc) {
703 		BNX2FC_HBA_DBG(lport, "FW stat req timed out\n");
704 		goto unlock_stats_mutex;
705 	}
706 	BNX2FC_STATS(hba, rx_stat2, fc_crc_cnt);
707 	bnx2fc_stats->invalid_crc_count += hba->bfw_stats.fc_crc_cnt;
708 	BNX2FC_STATS(hba, tx_stat, fcoe_tx_pkt_cnt);
709 	bnx2fc_stats->tx_frames += hba->bfw_stats.fcoe_tx_pkt_cnt;
710 	BNX2FC_STATS(hba, tx_stat, fcoe_tx_byte_cnt);
711 	bnx2fc_stats->tx_words += ((hba->bfw_stats.fcoe_tx_byte_cnt) / 4);
712 	BNX2FC_STATS(hba, rx_stat0, fcoe_rx_pkt_cnt);
713 	bnx2fc_stats->rx_frames += hba->bfw_stats.fcoe_rx_pkt_cnt;
714 	BNX2FC_STATS(hba, rx_stat0, fcoe_rx_byte_cnt);
715 	bnx2fc_stats->rx_words += ((hba->bfw_stats.fcoe_rx_byte_cnt) / 4);
716 
717 	bnx2fc_stats->dumped_frames = 0;
718 	bnx2fc_stats->lip_count = 0;
719 	bnx2fc_stats->nos_count = 0;
720 	bnx2fc_stats->loss_of_sync_count = 0;
721 	bnx2fc_stats->loss_of_signal_count = 0;
722 	bnx2fc_stats->prim_seq_protocol_err_count = 0;
723 
724 	memcpy(&hba->prev_stats, hba->stats_buffer,
725 	       sizeof(struct fcoe_statistics_params));
726 
727 unlock_stats_mutex:
728 	mutex_unlock(&hba->hba_stats_mutex);
729 	return bnx2fc_stats;
730 }
731 
732 static int bnx2fc_shost_config(struct fc_lport *lport, struct device *dev)
733 {
734 	struct fcoe_port *port = lport_priv(lport);
735 	struct bnx2fc_interface *interface = port->priv;
736 	struct bnx2fc_hba *hba = interface->hba;
737 	struct Scsi_Host *shost = lport->host;
738 	int rc = 0;
739 
740 	shost->max_cmd_len = BNX2FC_MAX_CMD_LEN;
741 	shost->max_lun = bnx2fc_max_luns;
742 	shost->max_id = BNX2FC_MAX_FCP_TGT;
743 	shost->max_channel = 0;
744 	if (lport->vport)
745 		shost->transportt = bnx2fc_vport_xport_template;
746 	else
747 		shost->transportt = bnx2fc_transport_template;
748 
749 	/* Add the new host to SCSI-ml */
750 	rc = scsi_add_host(lport->host, dev);
751 	if (rc) {
752 		printk(KERN_ERR PFX "Error on scsi_add_host\n");
753 		return rc;
754 	}
755 	if (!lport->vport)
756 		fc_host_max_npiv_vports(lport->host) = USHRT_MAX;
757 	snprintf(fc_host_symbolic_name(lport->host), 256,
758 		 "%s (QLogic %s) v%s over %s",
759 		BNX2FC_NAME, hba->chip_num, BNX2FC_VERSION,
760 		interface->netdev->name);
761 
762 	return 0;
763 }
764 
765 static int bnx2fc_link_ok(struct fc_lport *lport)
766 {
767 	struct fcoe_port *port = lport_priv(lport);
768 	struct bnx2fc_interface *interface = port->priv;
769 	struct bnx2fc_hba *hba = interface->hba;
770 	struct net_device *dev = hba->phys_dev;
771 	int rc = 0;
772 
773 	if ((dev->flags & IFF_UP) && netif_carrier_ok(dev))
774 		clear_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
775 	else {
776 		set_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
777 		rc = -1;
778 	}
779 	return rc;
780 }
781 
782 /**
783  * bnx2fc_get_link_state - get network link state
784  *
785  * @hba:	adapter instance pointer
786  *
787  * updates adapter structure flag based on netdev state
788  */
789 void bnx2fc_get_link_state(struct bnx2fc_hba *hba)
790 {
791 	if (test_bit(__LINK_STATE_NOCARRIER, &hba->phys_dev->state))
792 		set_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
793 	else
794 		clear_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
795 }
796 
797 static int bnx2fc_net_config(struct fc_lport *lport, struct net_device *netdev)
798 {
799 	struct bnx2fc_hba *hba;
800 	struct bnx2fc_interface *interface;
801 	struct fcoe_ctlr *ctlr;
802 	struct fcoe_port *port;
803 	u64 wwnn, wwpn;
804 
805 	port = lport_priv(lport);
806 	interface = port->priv;
807 	ctlr = bnx2fc_to_ctlr(interface);
808 	hba = interface->hba;
809 
810 	/* require support for get_pauseparam ethtool op. */
811 	if (!hba->phys_dev->ethtool_ops ||
812 	    !hba->phys_dev->ethtool_ops->get_pauseparam)
813 		return -EOPNOTSUPP;
814 
815 	if (fc_set_mfs(lport, BNX2FC_MFS))
816 		return -EINVAL;
817 
818 	skb_queue_head_init(&port->fcoe_pending_queue);
819 	port->fcoe_pending_queue_active = 0;
820 	timer_setup(&port->timer, fcoe_queue_timer, 0);
821 
822 	fcoe_link_speed_update(lport);
823 
824 	if (!lport->vport) {
825 		if (fcoe_get_wwn(netdev, &wwnn, NETDEV_FCOE_WWNN))
826 			wwnn = fcoe_wwn_from_mac(ctlr->ctl_src_addr,
827 						 1, 0);
828 		BNX2FC_HBA_DBG(lport, "WWNN = 0x%llx\n", wwnn);
829 		fc_set_wwnn(lport, wwnn);
830 
831 		if (fcoe_get_wwn(netdev, &wwpn, NETDEV_FCOE_WWPN))
832 			wwpn = fcoe_wwn_from_mac(ctlr->ctl_src_addr,
833 						 2, 0);
834 
835 		BNX2FC_HBA_DBG(lport, "WWPN = 0x%llx\n", wwpn);
836 		fc_set_wwpn(lport, wwpn);
837 	}
838 
839 	return 0;
840 }
841 
842 static void bnx2fc_destroy_timer(struct timer_list *t)
843 {
844 	struct bnx2fc_hba *hba = from_timer(hba, t, destroy_timer);
845 
846 	printk(KERN_ERR PFX "ERROR:bnx2fc_destroy_timer - "
847 	       "Destroy compl not received!!\n");
848 	set_bit(BNX2FC_FLAG_DESTROY_CMPL, &hba->flags);
849 	wake_up_interruptible(&hba->destroy_wait);
850 }
851 
852 /**
853  * bnx2fc_indicate_netevent - Generic netdev event handler
854  *
855  * @context:	adapter structure pointer
856  * @event:	event type
857  * @vlan_id:	vlan id - associated vlan id with this event
858  *
859  * Handles NETDEV_UP, NETDEV_DOWN, NETDEV_GOING_DOWN,NETDEV_CHANGE and
860  * NETDEV_CHANGE_MTU events. Handle NETDEV_UNREGISTER only for vlans.
861  */
862 static void bnx2fc_indicate_netevent(void *context, unsigned long event,
863 				     u16 vlan_id)
864 {
865 	struct bnx2fc_hba *hba = (struct bnx2fc_hba *)context;
866 	struct fcoe_ctlr_device *cdev;
867 	struct fc_lport *lport;
868 	struct fc_lport *vport;
869 	struct bnx2fc_interface *interface, *tmp;
870 	struct fcoe_ctlr *ctlr;
871 	int wait_for_upload = 0;
872 	u32 link_possible = 1;
873 
874 	if (vlan_id != 0 && event != NETDEV_UNREGISTER)
875 		return;
876 
877 	switch (event) {
878 	case NETDEV_UP:
879 		if (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state))
880 			printk(KERN_ERR "indicate_netevent: "\
881 					"hba is not UP!!\n");
882 		break;
883 
884 	case NETDEV_DOWN:
885 		clear_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state);
886 		clear_bit(ADAPTER_STATE_UP, &hba->adapter_state);
887 		link_possible = 0;
888 		break;
889 
890 	case NETDEV_GOING_DOWN:
891 		set_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state);
892 		link_possible = 0;
893 		break;
894 
895 	case NETDEV_CHANGE:
896 		break;
897 
898 	case NETDEV_UNREGISTER:
899 		if (!vlan_id)
900 			return;
901 		mutex_lock(&bnx2fc_dev_lock);
902 		list_for_each_entry_safe(interface, tmp, &if_list, list) {
903 			if (interface->hba == hba &&
904 			    interface->vlan_id == (vlan_id & VLAN_VID_MASK))
905 				__bnx2fc_destroy(interface);
906 		}
907 		mutex_unlock(&bnx2fc_dev_lock);
908 
909 		/* Ensure ALL destroy work has been completed before return */
910 		flush_workqueue(bnx2fc_wq);
911 		return;
912 
913 	default:
914 		return;
915 	}
916 
917 	mutex_lock(&bnx2fc_dev_lock);
918 	list_for_each_entry(interface, &if_list, list) {
919 
920 		if (interface->hba != hba)
921 			continue;
922 
923 		ctlr = bnx2fc_to_ctlr(interface);
924 		lport = ctlr->lp;
925 		BNX2FC_HBA_DBG(lport, "netevent handler - event=%s %ld\n",
926 				interface->netdev->name, event);
927 
928 		fcoe_link_speed_update(lport);
929 
930 		cdev = fcoe_ctlr_to_ctlr_dev(ctlr);
931 
932 		if (link_possible && !bnx2fc_link_ok(lport)) {
933 			switch (cdev->enabled) {
934 			case FCOE_CTLR_DISABLED:
935 				pr_info("Link up while interface is disabled.\n");
936 				break;
937 			case FCOE_CTLR_ENABLED:
938 			case FCOE_CTLR_UNUSED:
939 				/* Reset max recv frame size to default */
940 				fc_set_mfs(lport, BNX2FC_MFS);
941 				/*
942 				 * ctlr link up will only be handled during
943 				 * enable to avoid sending discovery
944 				 * solicitation on a stale vlan
945 				 */
946 				if (interface->enabled)
947 					fcoe_ctlr_link_up(ctlr);
948 			}
949 		} else if (fcoe_ctlr_link_down(ctlr)) {
950 			switch (cdev->enabled) {
951 			case FCOE_CTLR_DISABLED:
952 				pr_info("Link down while interface is disabled.\n");
953 				break;
954 			case FCOE_CTLR_ENABLED:
955 			case FCOE_CTLR_UNUSED:
956 				mutex_lock(&lport->lp_mutex);
957 				list_for_each_entry(vport, &lport->vports, list)
958 					fc_host_port_type(vport->host) =
959 					FC_PORTTYPE_UNKNOWN;
960 				mutex_unlock(&lport->lp_mutex);
961 				fc_host_port_type(lport->host) =
962 					FC_PORTTYPE_UNKNOWN;
963 				per_cpu_ptr(lport->stats,
964 					    get_cpu())->LinkFailureCount++;
965 				put_cpu();
966 				fcoe_clean_pending_queue(lport);
967 				wait_for_upload = 1;
968 			}
969 		}
970 	}
971 	mutex_unlock(&bnx2fc_dev_lock);
972 
973 	if (wait_for_upload) {
974 		clear_bit(ADAPTER_STATE_READY, &hba->adapter_state);
975 		init_waitqueue_head(&hba->shutdown_wait);
976 		BNX2FC_MISC_DBG("indicate_netevent "
977 				"num_ofld_sess = %d\n",
978 				hba->num_ofld_sess);
979 		hba->wait_for_link_down = 1;
980 		wait_event_interruptible(hba->shutdown_wait,
981 					 (hba->num_ofld_sess == 0));
982 		BNX2FC_MISC_DBG("wakeup - num_ofld_sess = %d\n",
983 				hba->num_ofld_sess);
984 		hba->wait_for_link_down = 0;
985 
986 		if (signal_pending(current))
987 			flush_signals(current);
988 	}
989 }
990 
991 static int bnx2fc_libfc_config(struct fc_lport *lport)
992 {
993 
994 	/* Set the function pointers set by bnx2fc driver */
995 	memcpy(&lport->tt, &bnx2fc_libfc_fcn_templ,
996 		sizeof(struct libfc_function_template));
997 	fc_elsct_init(lport);
998 	fc_exch_init(lport);
999 	fc_disc_init(lport);
1000 	fc_disc_config(lport, lport);
1001 	return 0;
1002 }
1003 
1004 static int bnx2fc_em_config(struct fc_lport *lport, struct bnx2fc_hba *hba)
1005 {
1006 	int fcoe_min_xid, fcoe_max_xid;
1007 
1008 	fcoe_min_xid = hba->max_xid + 1;
1009 	if (nr_cpu_ids <= 2)
1010 		fcoe_max_xid = hba->max_xid + FCOE_XIDS_PER_CPU_OFFSET;
1011 	else
1012 		fcoe_max_xid = hba->max_xid + FCOE_MAX_XID_OFFSET;
1013 	if (!fc_exch_mgr_alloc(lport, FC_CLASS_3, fcoe_min_xid,
1014 			       fcoe_max_xid, NULL)) {
1015 		printk(KERN_ERR PFX "em_config:fc_exch_mgr_alloc failed\n");
1016 		return -ENOMEM;
1017 	}
1018 
1019 	return 0;
1020 }
1021 
1022 static int bnx2fc_lport_config(struct fc_lport *lport)
1023 {
1024 	lport->link_up = 0;
1025 	lport->qfull = 0;
1026 	lport->max_retry_count = BNX2FC_MAX_RETRY_CNT;
1027 	lport->max_rport_retry_count = BNX2FC_MAX_RPORT_RETRY_CNT;
1028 	lport->e_d_tov = 2 * 1000;
1029 	lport->r_a_tov = 10 * 1000;
1030 
1031 	lport->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
1032 				FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
1033 	lport->does_npiv = 1;
1034 
1035 	memset(&lport->rnid_gen, 0, sizeof(struct fc_els_rnid_gen));
1036 	lport->rnid_gen.rnid_atype = BNX2FC_RNID_HBA;
1037 
1038 	/* alloc stats structure */
1039 	if (fc_lport_init_stats(lport))
1040 		return -ENOMEM;
1041 
1042 	/* Finish fc_lport configuration */
1043 	fc_lport_config(lport);
1044 
1045 	return 0;
1046 }
1047 
1048 /**
1049  * bnx2fc_fip_recv - handle a received FIP frame.
1050  *
1051  * @skb: the received skb
1052  * @dev: associated &net_device
1053  * @ptype: the &packet_type structure which was used to register this handler.
1054  * @orig_dev: original receive &net_device, in case @ dev is a bond.
1055  *
1056  * Returns: 0 for success
1057  */
1058 static int bnx2fc_fip_recv(struct sk_buff *skb, struct net_device *dev,
1059 			   struct packet_type *ptype,
1060 			   struct net_device *orig_dev)
1061 {
1062 	struct bnx2fc_interface *interface;
1063 	struct fcoe_ctlr *ctlr;
1064 	interface = container_of(ptype, struct bnx2fc_interface,
1065 				 fip_packet_type);
1066 	ctlr = bnx2fc_to_ctlr(interface);
1067 	fcoe_ctlr_recv(ctlr, skb);
1068 	return 0;
1069 }
1070 
1071 /**
1072  * bnx2fc_update_src_mac - Update Ethernet MAC filters.
1073  *
1074  * @lport: The local port
1075  * @addr: Location of data to copy
1076  *
1077  * Remove any previously-set unicast MAC filter.
1078  * Add secondary FCoE MAC address filter for our OUI.
1079  */
1080 static void bnx2fc_update_src_mac(struct fc_lport *lport, u8 *addr)
1081 {
1082 	struct fcoe_port *port = lport_priv(lport);
1083 
1084 	memcpy(port->data_src_addr, addr, ETH_ALEN);
1085 }
1086 
1087 /**
1088  * bnx2fc_get_src_mac - return the ethernet source address for an lport
1089  *
1090  * @lport: libfc port
1091  */
1092 static u8 *bnx2fc_get_src_mac(struct fc_lport *lport)
1093 {
1094 	struct fcoe_port *port;
1095 
1096 	port = (struct fcoe_port *)lport_priv(lport);
1097 	return port->data_src_addr;
1098 }
1099 
1100 /**
1101  * bnx2fc_fip_send - send an Ethernet-encapsulated FIP frame.
1102  *
1103  * @fip: FCoE controller.
1104  * @skb: FIP Packet.
1105  */
1106 static void bnx2fc_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
1107 {
1108 	struct fip_header *fiph;
1109 	struct ethhdr *eth_hdr;
1110 	u16 op;
1111 	u8 sub;
1112 
1113 	fiph = (struct fip_header *) ((void *)skb->data + 2 * ETH_ALEN + 2);
1114 	eth_hdr = (struct ethhdr *)skb_mac_header(skb);
1115 	op = ntohs(fiph->fip_op);
1116 	sub = fiph->fip_subcode;
1117 
1118 	if (op == FIP_OP_CTRL && sub == FIP_SC_SOL && bnx2fc_log_fka)
1119 		BNX2FC_MISC_DBG("Sending FKA from %pM to %pM.\n",
1120 		    eth_hdr->h_source, eth_hdr->h_dest);
1121 
1122 	skb->dev = bnx2fc_from_ctlr(fip)->netdev;
1123 	dev_queue_xmit(skb);
1124 }
1125 
1126 static int bnx2fc_vport_create(struct fc_vport *vport, bool disabled)
1127 {
1128 	struct Scsi_Host *shost = vport_to_shost(vport);
1129 	struct fc_lport *n_port = shost_priv(shost);
1130 	struct fcoe_port *port = lport_priv(n_port);
1131 	struct bnx2fc_interface *interface = port->priv;
1132 	struct net_device *netdev = interface->netdev;
1133 	struct fc_lport *vn_port;
1134 	int rc;
1135 	char buf[32];
1136 
1137 	rc = fcoe_validate_vport_create(vport);
1138 	if (rc) {
1139 		fcoe_wwn_to_str(vport->port_name, buf, sizeof(buf));
1140 		printk(KERN_ERR PFX "Failed to create vport, "
1141 		       "WWPN (0x%s) already exists\n",
1142 		       buf);
1143 		return rc;
1144 	}
1145 
1146 	if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &interface->hba->flags)) {
1147 		printk(KERN_ERR PFX "vn ports cannot be created on"
1148 			"this interface\n");
1149 		return -EIO;
1150 	}
1151 	rtnl_lock();
1152 	mutex_lock(&bnx2fc_dev_lock);
1153 	vn_port = bnx2fc_if_create(interface, &vport->dev, 1);
1154 	mutex_unlock(&bnx2fc_dev_lock);
1155 	rtnl_unlock();
1156 
1157 	if (!vn_port) {
1158 		printk(KERN_ERR PFX "bnx2fc_vport_create (%s) failed\n",
1159 			netdev->name);
1160 		return -EIO;
1161 	}
1162 
1163 	if (bnx2fc_devloss_tmo)
1164 		fc_host_dev_loss_tmo(vn_port->host) = bnx2fc_devloss_tmo;
1165 
1166 	if (disabled) {
1167 		fc_vport_set_state(vport, FC_VPORT_DISABLED);
1168 	} else {
1169 		vn_port->boot_time = jiffies;
1170 		fc_lport_init(vn_port);
1171 		fc_fabric_login(vn_port);
1172 		fc_vport_setlink(vn_port);
1173 	}
1174 	return 0;
1175 }
1176 
1177 static void bnx2fc_free_vport(struct bnx2fc_hba *hba, struct fc_lport *lport)
1178 {
1179 	struct bnx2fc_lport *blport, *tmp;
1180 
1181 	spin_lock_bh(&hba->hba_lock);
1182 	list_for_each_entry_safe(blport, tmp, &hba->vports, list) {
1183 		if (blport->lport == lport) {
1184 			list_del(&blport->list);
1185 			kfree(blport);
1186 		}
1187 	}
1188 	spin_unlock_bh(&hba->hba_lock);
1189 }
1190 
1191 static int bnx2fc_vport_destroy(struct fc_vport *vport)
1192 {
1193 	struct Scsi_Host *shost = vport_to_shost(vport);
1194 	struct fc_lport *n_port = shost_priv(shost);
1195 	struct fc_lport *vn_port = vport->dd_data;
1196 	struct fcoe_port *port = lport_priv(vn_port);
1197 	struct bnx2fc_interface *interface = port->priv;
1198 	struct fc_lport *v_port;
1199 	bool found = false;
1200 
1201 	mutex_lock(&n_port->lp_mutex);
1202 	list_for_each_entry(v_port, &n_port->vports, list)
1203 		if (v_port->vport == vport) {
1204 			found = true;
1205 			break;
1206 		}
1207 
1208 	if (!found) {
1209 		mutex_unlock(&n_port->lp_mutex);
1210 		return -ENOENT;
1211 	}
1212 	list_del(&vn_port->list);
1213 	mutex_unlock(&n_port->lp_mutex);
1214 	bnx2fc_free_vport(interface->hba, port->lport);
1215 	bnx2fc_port_shutdown(port->lport);
1216 	bnx2fc_interface_put(interface);
1217 	queue_work(bnx2fc_wq, &port->destroy_work);
1218 	return 0;
1219 }
1220 
1221 static int bnx2fc_vport_disable(struct fc_vport *vport, bool disable)
1222 {
1223 	struct fc_lport *lport = vport->dd_data;
1224 
1225 	if (disable) {
1226 		fc_vport_set_state(vport, FC_VPORT_DISABLED);
1227 		fc_fabric_logoff(lport);
1228 	} else {
1229 		lport->boot_time = jiffies;
1230 		fc_fabric_login(lport);
1231 		fc_vport_setlink(lport);
1232 	}
1233 	return 0;
1234 }
1235 
1236 
1237 static int bnx2fc_interface_setup(struct bnx2fc_interface *interface)
1238 {
1239 	struct net_device *netdev = interface->netdev;
1240 	struct net_device *physdev = interface->hba->phys_dev;
1241 	struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
1242 	struct netdev_hw_addr *ha;
1243 	int sel_san_mac = 0;
1244 
1245 	/* setup Source MAC Address */
1246 	rcu_read_lock();
1247 	for_each_dev_addr(physdev, ha) {
1248 		BNX2FC_MISC_DBG("net_config: ha->type = %d, fip_mac = ",
1249 				ha->type);
1250 		printk(KERN_INFO "%2x:%2x:%2x:%2x:%2x:%2x\n", ha->addr[0],
1251 				ha->addr[1], ha->addr[2], ha->addr[3],
1252 				ha->addr[4], ha->addr[5]);
1253 
1254 		if ((ha->type == NETDEV_HW_ADDR_T_SAN) &&
1255 		    (is_valid_ether_addr(ha->addr))) {
1256 			memcpy(ctlr->ctl_src_addr, ha->addr,
1257 			       ETH_ALEN);
1258 			sel_san_mac = 1;
1259 			BNX2FC_MISC_DBG("Found SAN MAC\n");
1260 		}
1261 	}
1262 	rcu_read_unlock();
1263 
1264 	if (!sel_san_mac)
1265 		return -ENODEV;
1266 
1267 	interface->fip_packet_type.func = bnx2fc_fip_recv;
1268 	interface->fip_packet_type.type = htons(ETH_P_FIP);
1269 	interface->fip_packet_type.dev = netdev;
1270 	dev_add_pack(&interface->fip_packet_type);
1271 
1272 	interface->fcoe_packet_type.func = bnx2fc_rcv;
1273 	interface->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
1274 	interface->fcoe_packet_type.dev = netdev;
1275 	dev_add_pack(&interface->fcoe_packet_type);
1276 
1277 	return 0;
1278 }
1279 
1280 static int bnx2fc_attach_transport(void)
1281 {
1282 	bnx2fc_transport_template =
1283 		fc_attach_transport(&bnx2fc_transport_function);
1284 
1285 	if (bnx2fc_transport_template == NULL) {
1286 		printk(KERN_ERR PFX "Failed to attach FC transport\n");
1287 		return -ENODEV;
1288 	}
1289 
1290 	bnx2fc_vport_xport_template =
1291 		fc_attach_transport(&bnx2fc_vport_xport_function);
1292 	if (bnx2fc_vport_xport_template == NULL) {
1293 		printk(KERN_ERR PFX
1294 		       "Failed to attach FC transport for vport\n");
1295 		fc_release_transport(bnx2fc_transport_template);
1296 		bnx2fc_transport_template = NULL;
1297 		return -ENODEV;
1298 	}
1299 	return 0;
1300 }
1301 static void bnx2fc_release_transport(void)
1302 {
1303 	fc_release_transport(bnx2fc_transport_template);
1304 	fc_release_transport(bnx2fc_vport_xport_template);
1305 	bnx2fc_transport_template = NULL;
1306 	bnx2fc_vport_xport_template = NULL;
1307 }
1308 
1309 static void bnx2fc_interface_release(struct kref *kref)
1310 {
1311 	struct fcoe_ctlr_device *ctlr_dev;
1312 	struct bnx2fc_interface *interface;
1313 	struct fcoe_ctlr *ctlr;
1314 	struct net_device *netdev;
1315 
1316 	interface = container_of(kref, struct bnx2fc_interface, kref);
1317 	BNX2FC_MISC_DBG("Interface is being released\n");
1318 
1319 	ctlr = bnx2fc_to_ctlr(interface);
1320 	ctlr_dev = fcoe_ctlr_to_ctlr_dev(ctlr);
1321 	netdev = interface->netdev;
1322 
1323 	/* tear-down FIP controller */
1324 	if (test_and_clear_bit(BNX2FC_CTLR_INIT_DONE, &interface->if_flags))
1325 		fcoe_ctlr_destroy(ctlr);
1326 
1327 	fcoe_ctlr_device_delete(ctlr_dev);
1328 
1329 	dev_put(netdev);
1330 	module_put(THIS_MODULE);
1331 }
1332 
1333 static inline void bnx2fc_interface_get(struct bnx2fc_interface *interface)
1334 {
1335 	kref_get(&interface->kref);
1336 }
1337 
1338 static inline void bnx2fc_interface_put(struct bnx2fc_interface *interface)
1339 {
1340 	kref_put(&interface->kref, bnx2fc_interface_release);
1341 }
1342 static void bnx2fc_hba_destroy(struct bnx2fc_hba *hba)
1343 {
1344 	/* Free the command manager */
1345 	if (hba->cmd_mgr) {
1346 		bnx2fc_cmd_mgr_free(hba->cmd_mgr);
1347 		hba->cmd_mgr = NULL;
1348 	}
1349 	kfree(hba->tgt_ofld_list);
1350 	bnx2fc_unbind_pcidev(hba);
1351 	kfree(hba);
1352 }
1353 
1354 /**
1355  * bnx2fc_hba_create - create a new bnx2fc hba
1356  *
1357  * @cnic:	pointer to cnic device
1358  *
1359  * Creates a new FCoE hba on the given device.
1360  *
1361  */
1362 static struct bnx2fc_hba *bnx2fc_hba_create(struct cnic_dev *cnic)
1363 {
1364 	struct bnx2fc_hba *hba;
1365 	struct fcoe_capabilities *fcoe_cap;
1366 	int rc;
1367 
1368 	hba = kzalloc(sizeof(*hba), GFP_KERNEL);
1369 	if (!hba) {
1370 		printk(KERN_ERR PFX "Unable to allocate hba structure\n");
1371 		return NULL;
1372 	}
1373 	spin_lock_init(&hba->hba_lock);
1374 	mutex_init(&hba->hba_mutex);
1375 	mutex_init(&hba->hba_stats_mutex);
1376 
1377 	hba->cnic = cnic;
1378 
1379 	hba->max_tasks = cnic->max_fcoe_exchanges;
1380 	hba->elstm_xids = (hba->max_tasks / 2);
1381 	hba->max_outstanding_cmds = hba->elstm_xids;
1382 	hba->max_xid = (hba->max_tasks - 1);
1383 
1384 	rc = bnx2fc_bind_pcidev(hba);
1385 	if (rc) {
1386 		printk(KERN_ERR PFX "create_adapter:  bind error\n");
1387 		goto bind_err;
1388 	}
1389 	hba->phys_dev = cnic->netdev;
1390 	hba->next_conn_id = 0;
1391 
1392 	hba->tgt_ofld_list =
1393 		kcalloc(BNX2FC_NUM_MAX_SESS, sizeof(struct bnx2fc_rport *),
1394 			GFP_KERNEL);
1395 	if (!hba->tgt_ofld_list) {
1396 		printk(KERN_ERR PFX "Unable to allocate tgt offload list\n");
1397 		goto tgtofld_err;
1398 	}
1399 
1400 	hba->num_ofld_sess = 0;
1401 
1402 	hba->cmd_mgr = bnx2fc_cmd_mgr_alloc(hba);
1403 	if (!hba->cmd_mgr) {
1404 		printk(KERN_ERR PFX "em_config:bnx2fc_cmd_mgr_alloc failed\n");
1405 		goto cmgr_err;
1406 	}
1407 	fcoe_cap = &hba->fcoe_cap;
1408 
1409 	fcoe_cap->capability1 = BNX2FC_TM_MAX_SQES <<
1410 					FCOE_IOS_PER_CONNECTION_SHIFT;
1411 	fcoe_cap->capability1 |= BNX2FC_NUM_MAX_SESS <<
1412 					FCOE_LOGINS_PER_PORT_SHIFT;
1413 	fcoe_cap->capability2 = hba->max_outstanding_cmds <<
1414 					FCOE_NUMBER_OF_EXCHANGES_SHIFT;
1415 	fcoe_cap->capability2 |= BNX2FC_MAX_NPIV <<
1416 					FCOE_NPIV_WWN_PER_PORT_SHIFT;
1417 	fcoe_cap->capability3 = BNX2FC_NUM_MAX_SESS <<
1418 					FCOE_TARGETS_SUPPORTED_SHIFT;
1419 	fcoe_cap->capability3 |= hba->max_outstanding_cmds <<
1420 					FCOE_OUTSTANDING_COMMANDS_SHIFT;
1421 	fcoe_cap->capability4 = FCOE_CAPABILITY4_STATEFUL;
1422 
1423 	init_waitqueue_head(&hba->shutdown_wait);
1424 	init_waitqueue_head(&hba->destroy_wait);
1425 	INIT_LIST_HEAD(&hba->vports);
1426 
1427 	return hba;
1428 
1429 cmgr_err:
1430 	kfree(hba->tgt_ofld_list);
1431 tgtofld_err:
1432 	bnx2fc_unbind_pcidev(hba);
1433 bind_err:
1434 	kfree(hba);
1435 	return NULL;
1436 }
1437 
1438 static struct bnx2fc_interface *
1439 bnx2fc_interface_create(struct bnx2fc_hba *hba,
1440 			struct net_device *netdev,
1441 			enum fip_mode fip_mode)
1442 {
1443 	struct fcoe_ctlr_device *ctlr_dev;
1444 	struct bnx2fc_interface *interface;
1445 	struct fcoe_ctlr *ctlr;
1446 	int size;
1447 	int rc = 0;
1448 
1449 	size = (sizeof(*interface) + sizeof(struct fcoe_ctlr));
1450 	ctlr_dev = fcoe_ctlr_device_add(&netdev->dev, &bnx2fc_fcoe_sysfs_templ,
1451 					 size);
1452 	if (!ctlr_dev) {
1453 		printk(KERN_ERR PFX "Unable to allocate interface structure\n");
1454 		return NULL;
1455 	}
1456 	ctlr = fcoe_ctlr_device_priv(ctlr_dev);
1457 	ctlr->cdev = ctlr_dev;
1458 	interface = fcoe_ctlr_priv(ctlr);
1459 	dev_hold(netdev);
1460 	kref_init(&interface->kref);
1461 	interface->hba = hba;
1462 	interface->netdev = netdev;
1463 
1464 	/* Initialize FIP */
1465 	fcoe_ctlr_init(ctlr, fip_mode);
1466 	ctlr->send = bnx2fc_fip_send;
1467 	ctlr->update_mac = bnx2fc_update_src_mac;
1468 	ctlr->get_src_addr = bnx2fc_get_src_mac;
1469 	set_bit(BNX2FC_CTLR_INIT_DONE, &interface->if_flags);
1470 
1471 	rc = bnx2fc_interface_setup(interface);
1472 	if (!rc)
1473 		return interface;
1474 
1475 	fcoe_ctlr_destroy(ctlr);
1476 	dev_put(netdev);
1477 	fcoe_ctlr_device_delete(ctlr_dev);
1478 	return NULL;
1479 }
1480 
1481 /**
1482  * bnx2fc_if_create - Create FCoE instance on a given interface
1483  *
1484  * @interface:	FCoE interface to create a local port on
1485  * @parent:	Device pointer to be the parent in sysfs for the SCSI host
1486  * @npiv:	Indicates if the port is vport or not
1487  *
1488  * Creates a fc_lport instance and a Scsi_Host instance and configure them.
1489  *
1490  * Returns:	Allocated fc_lport or an error pointer
1491  */
1492 static struct fc_lport *bnx2fc_if_create(struct bnx2fc_interface *interface,
1493 				  struct device *parent, int npiv)
1494 {
1495 	struct fcoe_ctlr        *ctlr = bnx2fc_to_ctlr(interface);
1496 	struct fc_lport		*lport, *n_port;
1497 	struct fcoe_port	*port;
1498 	struct Scsi_Host	*shost;
1499 	struct fc_vport		*vport = dev_to_vport(parent);
1500 	struct bnx2fc_lport	*blport;
1501 	struct bnx2fc_hba	*hba = interface->hba;
1502 	int			rc = 0;
1503 
1504 	blport = kzalloc(sizeof(struct bnx2fc_lport), GFP_KERNEL);
1505 	if (!blport) {
1506 		BNX2FC_HBA_DBG(ctlr->lp, "Unable to alloc blport\n");
1507 		return NULL;
1508 	}
1509 
1510 	/* Allocate Scsi_Host structure */
1511 	bnx2fc_shost_template.can_queue = hba->max_outstanding_cmds;
1512 	if (!npiv)
1513 		lport = libfc_host_alloc(&bnx2fc_shost_template, sizeof(*port));
1514 	else
1515 		lport = libfc_vport_create(vport, sizeof(*port));
1516 
1517 	if (!lport) {
1518 		printk(KERN_ERR PFX "could not allocate scsi host structure\n");
1519 		goto free_blport;
1520 	}
1521 	shost = lport->host;
1522 	port = lport_priv(lport);
1523 	port->lport = lport;
1524 	port->priv = interface;
1525 	port->get_netdev = bnx2fc_netdev;
1526 	INIT_WORK(&port->destroy_work, bnx2fc_destroy_work);
1527 
1528 	/* Configure fcoe_port */
1529 	rc = bnx2fc_lport_config(lport);
1530 	if (rc)
1531 		goto lp_config_err;
1532 
1533 	if (npiv) {
1534 		printk(KERN_ERR PFX "Setting vport names, 0x%llX 0x%llX\n",
1535 			vport->node_name, vport->port_name);
1536 		fc_set_wwnn(lport, vport->node_name);
1537 		fc_set_wwpn(lport, vport->port_name);
1538 	}
1539 	/* Configure netdev and networking properties of the lport */
1540 	rc = bnx2fc_net_config(lport, interface->netdev);
1541 	if (rc) {
1542 		printk(KERN_ERR PFX "Error on bnx2fc_net_config\n");
1543 		goto lp_config_err;
1544 	}
1545 
1546 	rc = bnx2fc_shost_config(lport, parent);
1547 	if (rc) {
1548 		printk(KERN_ERR PFX "Couldn't configure shost for %s\n",
1549 			interface->netdev->name);
1550 		goto lp_config_err;
1551 	}
1552 
1553 	/* Initialize the libfc library */
1554 	rc = bnx2fc_libfc_config(lport);
1555 	if (rc) {
1556 		printk(KERN_ERR PFX "Couldn't configure libfc\n");
1557 		goto shost_err;
1558 	}
1559 	fc_host_port_type(lport->host) = FC_PORTTYPE_UNKNOWN;
1560 
1561 	if (bnx2fc_devloss_tmo)
1562 		fc_host_dev_loss_tmo(shost) = bnx2fc_devloss_tmo;
1563 
1564 	/* Allocate exchange manager */
1565 	if (!npiv)
1566 		rc = bnx2fc_em_config(lport, hba);
1567 	else {
1568 		shost = vport_to_shost(vport);
1569 		n_port = shost_priv(shost);
1570 		rc = fc_exch_mgr_list_clone(n_port, lport);
1571 	}
1572 
1573 	if (rc) {
1574 		printk(KERN_ERR PFX "Error on bnx2fc_em_config\n");
1575 		goto shost_err;
1576 	}
1577 
1578 	bnx2fc_interface_get(interface);
1579 
1580 	spin_lock_bh(&hba->hba_lock);
1581 	blport->lport = lport;
1582 	list_add_tail(&blport->list, &hba->vports);
1583 	spin_unlock_bh(&hba->hba_lock);
1584 
1585 	return lport;
1586 
1587 shost_err:
1588 	scsi_remove_host(shost);
1589 lp_config_err:
1590 	scsi_host_put(lport->host);
1591 free_blport:
1592 	kfree(blport);
1593 	return NULL;
1594 }
1595 
1596 static void bnx2fc_net_cleanup(struct bnx2fc_interface *interface)
1597 {
1598 	/* Dont listen for Ethernet packets anymore */
1599 	__dev_remove_pack(&interface->fcoe_packet_type);
1600 	__dev_remove_pack(&interface->fip_packet_type);
1601 	synchronize_net();
1602 }
1603 
1604 static void bnx2fc_interface_cleanup(struct bnx2fc_interface *interface)
1605 {
1606 	struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
1607 	struct fc_lport *lport = ctlr->lp;
1608 	struct fcoe_port *port = lport_priv(lport);
1609 	struct bnx2fc_hba *hba = interface->hba;
1610 
1611 	/* Stop the transmit retry timer */
1612 	del_timer_sync(&port->timer);
1613 
1614 	/* Free existing transmit skbs */
1615 	fcoe_clean_pending_queue(lport);
1616 
1617 	bnx2fc_net_cleanup(interface);
1618 
1619 	bnx2fc_free_vport(hba, lport);
1620 }
1621 
1622 static void bnx2fc_if_destroy(struct fc_lport *lport)
1623 {
1624 
1625 	/* Free queued packets for the receive thread */
1626 	bnx2fc_clean_rx_queue(lport);
1627 
1628 	/* Detach from scsi-ml */
1629 	fc_remove_host(lport->host);
1630 	scsi_remove_host(lport->host);
1631 
1632 	/*
1633 	 * Note that only the physical lport will have the exchange manager.
1634 	 * for vports, this function is NOP
1635 	 */
1636 	fc_exch_mgr_free(lport);
1637 
1638 	/* Free memory used by statistical counters */
1639 	fc_lport_free_stats(lport);
1640 
1641 	/* Release Scsi_Host */
1642 	scsi_host_put(lport->host);
1643 }
1644 
1645 static void __bnx2fc_destroy(struct bnx2fc_interface *interface)
1646 {
1647 	struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
1648 	struct fc_lport *lport = ctlr->lp;
1649 	struct fcoe_port *port = lport_priv(lport);
1650 
1651 	bnx2fc_interface_cleanup(interface);
1652 	bnx2fc_stop(interface);
1653 	list_del(&interface->list);
1654 	bnx2fc_interface_put(interface);
1655 	queue_work(bnx2fc_wq, &port->destroy_work);
1656 }
1657 
1658 /**
1659  * bnx2fc_destroy - Destroy a bnx2fc FCoE interface
1660  *
1661  * @netdev: The net device that the FCoE interface is on
1662  *
1663  * Called from sysfs.
1664  *
1665  * Returns: 0 for success
1666  */
1667 static int bnx2fc_destroy(struct net_device *netdev)
1668 {
1669 	struct bnx2fc_interface *interface = NULL;
1670 	struct workqueue_struct *timer_work_queue;
1671 	struct fcoe_ctlr *ctlr;
1672 	int rc = 0;
1673 
1674 	rtnl_lock();
1675 	mutex_lock(&bnx2fc_dev_lock);
1676 
1677 	interface = bnx2fc_interface_lookup(netdev);
1678 	ctlr = bnx2fc_to_ctlr(interface);
1679 	if (!interface || !ctlr->lp) {
1680 		rc = -ENODEV;
1681 		printk(KERN_ERR PFX "bnx2fc_destroy: interface or lport not found\n");
1682 		goto netdev_err;
1683 	}
1684 
1685 	timer_work_queue = interface->timer_work_queue;
1686 	__bnx2fc_destroy(interface);
1687 	destroy_workqueue(timer_work_queue);
1688 
1689 netdev_err:
1690 	mutex_unlock(&bnx2fc_dev_lock);
1691 	rtnl_unlock();
1692 	return rc;
1693 }
1694 
1695 static void bnx2fc_destroy_work(struct work_struct *work)
1696 {
1697 	struct fcoe_port *port;
1698 	struct fc_lport *lport;
1699 
1700 	port = container_of(work, struct fcoe_port, destroy_work);
1701 	lport = port->lport;
1702 
1703 	BNX2FC_HBA_DBG(lport, "Entered bnx2fc_destroy_work\n");
1704 
1705 	bnx2fc_if_destroy(lport);
1706 }
1707 
1708 static void bnx2fc_unbind_adapter_devices(struct bnx2fc_hba *hba)
1709 {
1710 	bnx2fc_free_fw_resc(hba);
1711 	bnx2fc_free_task_ctx(hba);
1712 }
1713 
1714 /**
1715  * bnx2fc_bind_adapter_devices - binds bnx2fc adapter with the associated
1716  *			pci structure
1717  *
1718  * @hba:		Adapter instance
1719  */
1720 static int bnx2fc_bind_adapter_devices(struct bnx2fc_hba *hba)
1721 {
1722 	if (bnx2fc_setup_task_ctx(hba))
1723 		goto mem_err;
1724 
1725 	if (bnx2fc_setup_fw_resc(hba))
1726 		goto mem_err;
1727 
1728 	return 0;
1729 mem_err:
1730 	bnx2fc_unbind_adapter_devices(hba);
1731 	return -ENOMEM;
1732 }
1733 
1734 static int bnx2fc_bind_pcidev(struct bnx2fc_hba *hba)
1735 {
1736 	struct cnic_dev *cnic;
1737 	struct pci_dev *pdev;
1738 
1739 	if (!hba->cnic) {
1740 		printk(KERN_ERR PFX "cnic is NULL\n");
1741 		return -ENODEV;
1742 	}
1743 	cnic = hba->cnic;
1744 	pdev = hba->pcidev = cnic->pcidev;
1745 	if (!hba->pcidev)
1746 		return -ENODEV;
1747 
1748 	switch (pdev->device) {
1749 	case PCI_DEVICE_ID_NX2_57710:
1750 		strncpy(hba->chip_num, "BCM57710", BCM_CHIP_LEN);
1751 		break;
1752 	case PCI_DEVICE_ID_NX2_57711:
1753 		strncpy(hba->chip_num, "BCM57711", BCM_CHIP_LEN);
1754 		break;
1755 	case PCI_DEVICE_ID_NX2_57712:
1756 	case PCI_DEVICE_ID_NX2_57712_MF:
1757 	case PCI_DEVICE_ID_NX2_57712_VF:
1758 		strncpy(hba->chip_num, "BCM57712", BCM_CHIP_LEN);
1759 		break;
1760 	case PCI_DEVICE_ID_NX2_57800:
1761 	case PCI_DEVICE_ID_NX2_57800_MF:
1762 	case PCI_DEVICE_ID_NX2_57800_VF:
1763 		strncpy(hba->chip_num, "BCM57800", BCM_CHIP_LEN);
1764 		break;
1765 	case PCI_DEVICE_ID_NX2_57810:
1766 	case PCI_DEVICE_ID_NX2_57810_MF:
1767 	case PCI_DEVICE_ID_NX2_57810_VF:
1768 		strncpy(hba->chip_num, "BCM57810", BCM_CHIP_LEN);
1769 		break;
1770 	case PCI_DEVICE_ID_NX2_57840:
1771 	case PCI_DEVICE_ID_NX2_57840_MF:
1772 	case PCI_DEVICE_ID_NX2_57840_VF:
1773 	case PCI_DEVICE_ID_NX2_57840_2_20:
1774 	case PCI_DEVICE_ID_NX2_57840_4_10:
1775 		strncpy(hba->chip_num, "BCM57840", BCM_CHIP_LEN);
1776 		break;
1777 	default:
1778 		pr_err(PFX "Unknown device id 0x%x\n", pdev->device);
1779 		break;
1780 	}
1781 	pci_dev_get(hba->pcidev);
1782 	return 0;
1783 }
1784 
1785 static void bnx2fc_unbind_pcidev(struct bnx2fc_hba *hba)
1786 {
1787 	if (hba->pcidev) {
1788 		hba->chip_num[0] = '\0';
1789 		pci_dev_put(hba->pcidev);
1790 	}
1791 	hba->pcidev = NULL;
1792 }
1793 
1794 /**
1795  * bnx2fc_ulp_get_stats - cnic callback to populate FCoE stats
1796  *
1797  * @handle:    transport handle pointing to adapter struture
1798  */
1799 static int bnx2fc_ulp_get_stats(void *handle)
1800 {
1801 	struct bnx2fc_hba *hba = handle;
1802 	struct cnic_dev *cnic;
1803 	struct fcoe_stats_info *stats_addr;
1804 
1805 	if (!hba)
1806 		return -EINVAL;
1807 
1808 	cnic = hba->cnic;
1809 	stats_addr = &cnic->stats_addr->fcoe_stat;
1810 	if (!stats_addr)
1811 		return -EINVAL;
1812 
1813 	strncpy(stats_addr->version, BNX2FC_VERSION,
1814 		sizeof(stats_addr->version));
1815 	stats_addr->txq_size = BNX2FC_SQ_WQES_MAX;
1816 	stats_addr->rxq_size = BNX2FC_CQ_WQES_MAX;
1817 
1818 	return 0;
1819 }
1820 
1821 
1822 /**
1823  * bnx2fc_ulp_start - cnic callback to initialize & start adapter instance
1824  *
1825  * @handle:	transport handle pointing to adapter structure
1826  *
1827  * This function maps adapter structure to pcidev structure and initiates
1828  *	firmware handshake to enable/initialize on-chip FCoE components.
1829  *	This bnx2fc - cnic interface api callback is used after following
1830  *	conditions are met -
1831  *	a) underlying network interface is up (marked by event NETDEV_UP
1832  *		from netdev
1833  *	b) bnx2fc adatper structure is registered.
1834  */
1835 static void bnx2fc_ulp_start(void *handle)
1836 {
1837 	struct bnx2fc_hba *hba = handle;
1838 	struct bnx2fc_interface *interface;
1839 	struct fcoe_ctlr *ctlr;
1840 	struct fc_lport *lport;
1841 
1842 	mutex_lock(&bnx2fc_dev_lock);
1843 
1844 	if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags))
1845 		bnx2fc_fw_init(hba);
1846 
1847 	BNX2FC_MISC_DBG("bnx2fc started.\n");
1848 
1849 	list_for_each_entry(interface, &if_list, list) {
1850 		if (interface->hba == hba) {
1851 			ctlr = bnx2fc_to_ctlr(interface);
1852 			lport = ctlr->lp;
1853 			/* Kick off Fabric discovery*/
1854 			printk(KERN_ERR PFX "ulp_init: start discovery\n");
1855 			lport->tt.frame_send = bnx2fc_xmit;
1856 			bnx2fc_start_disc(interface);
1857 		}
1858 	}
1859 
1860 	mutex_unlock(&bnx2fc_dev_lock);
1861 }
1862 
1863 static void bnx2fc_port_shutdown(struct fc_lport *lport)
1864 {
1865 	BNX2FC_MISC_DBG("Entered %s\n", __func__);
1866 	fc_fabric_logoff(lport);
1867 	fc_lport_destroy(lport);
1868 }
1869 
1870 static void bnx2fc_stop(struct bnx2fc_interface *interface)
1871 {
1872 	struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
1873 	struct fc_lport *lport;
1874 	struct fc_lport *vport;
1875 
1876 	if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &interface->hba->flags))
1877 		return;
1878 
1879 	lport = ctlr->lp;
1880 	bnx2fc_port_shutdown(lport);
1881 
1882 	mutex_lock(&lport->lp_mutex);
1883 	list_for_each_entry(vport, &lport->vports, list)
1884 		fc_host_port_type(vport->host) =
1885 					FC_PORTTYPE_UNKNOWN;
1886 	mutex_unlock(&lport->lp_mutex);
1887 	fc_host_port_type(lport->host) = FC_PORTTYPE_UNKNOWN;
1888 	fcoe_ctlr_link_down(ctlr);
1889 	fcoe_clean_pending_queue(lport);
1890 }
1891 
1892 static int bnx2fc_fw_init(struct bnx2fc_hba *hba)
1893 {
1894 #define BNX2FC_INIT_POLL_TIME		(1000 / HZ)
1895 	int rc = -1;
1896 	int i = HZ;
1897 
1898 	rc = bnx2fc_bind_adapter_devices(hba);
1899 	if (rc) {
1900 		printk(KERN_ALERT PFX
1901 			"bnx2fc_bind_adapter_devices failed - rc = %d\n", rc);
1902 		goto err_out;
1903 	}
1904 
1905 	rc = bnx2fc_send_fw_fcoe_init_msg(hba);
1906 	if (rc) {
1907 		printk(KERN_ALERT PFX
1908 			"bnx2fc_send_fw_fcoe_init_msg failed - rc = %d\n", rc);
1909 		goto err_unbind;
1910 	}
1911 
1912 	/*
1913 	 * Wait until the adapter init message is complete, and adapter
1914 	 * state is UP.
1915 	 */
1916 	while (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state) && i--)
1917 		msleep(BNX2FC_INIT_POLL_TIME);
1918 
1919 	if (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state)) {
1920 		printk(KERN_ERR PFX "bnx2fc_start: %s failed to initialize.  "
1921 				"Ignoring...\n",
1922 				hba->cnic->netdev->name);
1923 		rc = -1;
1924 		goto err_unbind;
1925 	}
1926 
1927 
1928 	set_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags);
1929 	return 0;
1930 
1931 err_unbind:
1932 	bnx2fc_unbind_adapter_devices(hba);
1933 err_out:
1934 	return rc;
1935 }
1936 
1937 static void bnx2fc_fw_destroy(struct bnx2fc_hba *hba)
1938 {
1939 	if (test_and_clear_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags)) {
1940 		if (bnx2fc_send_fw_fcoe_destroy_msg(hba) == 0) {
1941 			timer_setup(&hba->destroy_timer, bnx2fc_destroy_timer,
1942 				    0);
1943 			hba->destroy_timer.expires = BNX2FC_FW_TIMEOUT +
1944 								jiffies;
1945 			add_timer(&hba->destroy_timer);
1946 			wait_event_interruptible(hba->destroy_wait,
1947 					test_bit(BNX2FC_FLAG_DESTROY_CMPL,
1948 						 &hba->flags));
1949 			clear_bit(BNX2FC_FLAG_DESTROY_CMPL, &hba->flags);
1950 			/* This should never happen */
1951 			if (signal_pending(current))
1952 				flush_signals(current);
1953 
1954 			del_timer_sync(&hba->destroy_timer);
1955 		}
1956 		bnx2fc_unbind_adapter_devices(hba);
1957 	}
1958 }
1959 
1960 /**
1961  * bnx2fc_ulp_stop - cnic callback to shutdown adapter instance
1962  *
1963  * @handle:	transport handle pointing to adapter structure
1964  *
1965  * Driver checks if adapter is already in shutdown mode, if not start
1966  *	the shutdown process.
1967  */
1968 static void bnx2fc_ulp_stop(void *handle)
1969 {
1970 	struct bnx2fc_hba *hba = handle;
1971 	struct bnx2fc_interface *interface;
1972 
1973 	printk(KERN_ERR "ULP_STOP\n");
1974 
1975 	mutex_lock(&bnx2fc_dev_lock);
1976 	if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags))
1977 		goto exit;
1978 	list_for_each_entry(interface, &if_list, list) {
1979 		if (interface->hba == hba)
1980 			bnx2fc_stop(interface);
1981 	}
1982 	BUG_ON(hba->num_ofld_sess != 0);
1983 
1984 	mutex_lock(&hba->hba_mutex);
1985 	clear_bit(ADAPTER_STATE_UP, &hba->adapter_state);
1986 	clear_bit(ADAPTER_STATE_GOING_DOWN,
1987 		  &hba->adapter_state);
1988 
1989 	clear_bit(ADAPTER_STATE_READY, &hba->adapter_state);
1990 	mutex_unlock(&hba->hba_mutex);
1991 
1992 	bnx2fc_fw_destroy(hba);
1993 exit:
1994 	mutex_unlock(&bnx2fc_dev_lock);
1995 }
1996 
1997 static void bnx2fc_start_disc(struct bnx2fc_interface *interface)
1998 {
1999 	struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
2000 	struct fc_lport *lport;
2001 	int wait_cnt = 0;
2002 
2003 	BNX2FC_MISC_DBG("Entered %s\n", __func__);
2004 	/* Kick off FIP/FLOGI */
2005 	if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &interface->hba->flags)) {
2006 		printk(KERN_ERR PFX "Init not done yet\n");
2007 		return;
2008 	}
2009 
2010 	lport = ctlr->lp;
2011 	BNX2FC_HBA_DBG(lport, "calling fc_fabric_login\n");
2012 
2013 	if (!bnx2fc_link_ok(lport) && interface->enabled) {
2014 		BNX2FC_HBA_DBG(lport, "ctlr_link_up\n");
2015 		fcoe_ctlr_link_up(ctlr);
2016 		fc_host_port_type(lport->host) = FC_PORTTYPE_NPORT;
2017 		set_bit(ADAPTER_STATE_READY, &interface->hba->adapter_state);
2018 	}
2019 
2020 	/* wait for the FCF to be selected before issuing FLOGI */
2021 	while (!ctlr->sel_fcf) {
2022 		msleep(250);
2023 		/* give up after 3 secs */
2024 		if (++wait_cnt > 12)
2025 			break;
2026 	}
2027 
2028 	/* Reset max receive frame size to default */
2029 	if (fc_set_mfs(lport, BNX2FC_MFS))
2030 		return;
2031 
2032 	fc_lport_init(lport);
2033 	fc_fabric_login(lport);
2034 }
2035 
2036 
2037 /**
2038  * bnx2fc_ulp_init - Initialize an adapter instance
2039  *
2040  * @dev :	cnic device handle
2041  * Called from cnic_register_driver() context to initialize all
2042  *	enumerated cnic devices. This routine allocates adapter structure
2043  *	and other device specific resources.
2044  */
2045 static void bnx2fc_ulp_init(struct cnic_dev *dev)
2046 {
2047 	struct bnx2fc_hba *hba;
2048 	int rc = 0;
2049 
2050 	BNX2FC_MISC_DBG("Entered %s\n", __func__);
2051 	/* bnx2fc works only when bnx2x is loaded */
2052 	if (!test_bit(CNIC_F_BNX2X_CLASS, &dev->flags) ||
2053 	    (dev->max_fcoe_conn == 0)) {
2054 		printk(KERN_ERR PFX "bnx2fc FCoE not supported on %s,"
2055 				    " flags: %lx fcoe_conn: %d\n",
2056 			dev->netdev->name, dev->flags, dev->max_fcoe_conn);
2057 		return;
2058 	}
2059 
2060 	hba = bnx2fc_hba_create(dev);
2061 	if (!hba) {
2062 		printk(KERN_ERR PFX "hba initialization failed\n");
2063 		return;
2064 	}
2065 
2066 	pr_info(PFX "FCoE initialized for %s.\n", dev->netdev->name);
2067 
2068 	/* Add HBA to the adapter list */
2069 	mutex_lock(&bnx2fc_dev_lock);
2070 	list_add_tail(&hba->list, &adapter_list);
2071 	adapter_count++;
2072 	mutex_unlock(&bnx2fc_dev_lock);
2073 
2074 	dev->fcoe_cap = &hba->fcoe_cap;
2075 	clear_bit(BNX2FC_CNIC_REGISTERED, &hba->reg_with_cnic);
2076 	rc = dev->register_device(dev, CNIC_ULP_FCOE,
2077 						(void *) hba);
2078 	if (rc)
2079 		printk(KERN_ERR PFX "register_device failed, rc = %d\n", rc);
2080 	else
2081 		set_bit(BNX2FC_CNIC_REGISTERED, &hba->reg_with_cnic);
2082 }
2083 
2084 /* Assumes rtnl_lock and the bnx2fc_dev_lock are already taken */
2085 static int __bnx2fc_disable(struct fcoe_ctlr *ctlr)
2086 {
2087 	struct bnx2fc_interface *interface = fcoe_ctlr_priv(ctlr);
2088 
2089 	if (interface->enabled == true) {
2090 		if (!ctlr->lp) {
2091 			pr_err(PFX "__bnx2fc_disable: lport not found\n");
2092 			return -ENODEV;
2093 		} else {
2094 			interface->enabled = false;
2095 			fcoe_ctlr_link_down(ctlr);
2096 			fcoe_clean_pending_queue(ctlr->lp);
2097 		}
2098 	}
2099 	return 0;
2100 }
2101 
2102 /*
2103  * Deperecated: Use bnx2fc_enabled()
2104  */
2105 static int bnx2fc_disable(struct net_device *netdev)
2106 {
2107 	struct bnx2fc_interface *interface;
2108 	struct fcoe_ctlr *ctlr;
2109 	int rc = 0;
2110 
2111 	rtnl_lock();
2112 	mutex_lock(&bnx2fc_dev_lock);
2113 
2114 	interface = bnx2fc_interface_lookup(netdev);
2115 	ctlr = bnx2fc_to_ctlr(interface);
2116 
2117 	if (!interface) {
2118 		rc = -ENODEV;
2119 		pr_err(PFX "bnx2fc_disable: interface not found\n");
2120 	} else {
2121 		rc = __bnx2fc_disable(ctlr);
2122 	}
2123 	mutex_unlock(&bnx2fc_dev_lock);
2124 	rtnl_unlock();
2125 	return rc;
2126 }
2127 
2128 static uint bnx2fc_npiv_create_vports(struct fc_lport *lport,
2129 				      struct cnic_fc_npiv_tbl *npiv_tbl)
2130 {
2131 	struct fc_vport_identifiers vpid;
2132 	uint i, created = 0;
2133 	u64 wwnn = 0;
2134 	char wwpn_str[32];
2135 	char wwnn_str[32];
2136 
2137 	if (npiv_tbl->count > MAX_NPIV_ENTRIES) {
2138 		BNX2FC_HBA_DBG(lport, "Exceeded count max of npiv table\n");
2139 		goto done;
2140 	}
2141 
2142 	/* Sanity check the first entry to make sure it's not 0 */
2143 	if (wwn_to_u64(npiv_tbl->wwnn[0]) == 0 &&
2144 	    wwn_to_u64(npiv_tbl->wwpn[0]) == 0) {
2145 		BNX2FC_HBA_DBG(lport, "First NPIV table entries invalid.\n");
2146 		goto done;
2147 	}
2148 
2149 	vpid.roles = FC_PORT_ROLE_FCP_INITIATOR;
2150 	vpid.vport_type = FC_PORTTYPE_NPIV;
2151 	vpid.disable = false;
2152 
2153 	for (i = 0; i < npiv_tbl->count; i++) {
2154 		wwnn = wwn_to_u64(npiv_tbl->wwnn[i]);
2155 		if (wwnn == 0) {
2156 			/*
2157 			 * If we get a 0 element from for the WWNN then assume
2158 			 * the WWNN should be the same as the physical port.
2159 			 */
2160 			wwnn = lport->wwnn;
2161 		}
2162 		vpid.node_name = wwnn;
2163 		vpid.port_name = wwn_to_u64(npiv_tbl->wwpn[i]);
2164 		scnprintf(vpid.symbolic_name, sizeof(vpid.symbolic_name),
2165 		    "NPIV[%u]:%016llx-%016llx",
2166 		    created, vpid.port_name, vpid.node_name);
2167 		fcoe_wwn_to_str(vpid.node_name, wwnn_str, sizeof(wwnn_str));
2168 		fcoe_wwn_to_str(vpid.port_name, wwpn_str, sizeof(wwpn_str));
2169 		BNX2FC_HBA_DBG(lport, "Creating vport %s:%s.\n", wwnn_str,
2170 		    wwpn_str);
2171 		if (fc_vport_create(lport->host, 0, &vpid))
2172 			created++;
2173 		else
2174 			BNX2FC_HBA_DBG(lport, "Failed to create vport\n");
2175 	}
2176 done:
2177 	return created;
2178 }
2179 
2180 static int __bnx2fc_enable(struct fcoe_ctlr *ctlr)
2181 {
2182 	struct bnx2fc_interface *interface = fcoe_ctlr_priv(ctlr);
2183 	struct bnx2fc_hba *hba;
2184 	struct cnic_fc_npiv_tbl *npiv_tbl;
2185 	struct fc_lport *lport;
2186 
2187 	if (interface->enabled == false) {
2188 		if (!ctlr->lp) {
2189 			pr_err(PFX "__bnx2fc_enable: lport not found\n");
2190 			return -ENODEV;
2191 		} else if (!bnx2fc_link_ok(ctlr->lp)) {
2192 			fcoe_ctlr_link_up(ctlr);
2193 			interface->enabled = true;
2194 		}
2195 	}
2196 
2197 	/* Create static NPIV ports if any are contained in NVRAM */
2198 	hba = interface->hba;
2199 	lport = ctlr->lp;
2200 
2201 	if (!hba)
2202 		goto done;
2203 
2204 	if (!hba->cnic)
2205 		goto done;
2206 
2207 	if (!lport)
2208 		goto done;
2209 
2210 	if (!lport->host)
2211 		goto done;
2212 
2213 	if (!hba->cnic->get_fc_npiv_tbl)
2214 		goto done;
2215 
2216 	npiv_tbl = kzalloc(sizeof(struct cnic_fc_npiv_tbl), GFP_KERNEL);
2217 	if (!npiv_tbl)
2218 		goto done;
2219 
2220 	if (hba->cnic->get_fc_npiv_tbl(hba->cnic, npiv_tbl))
2221 		goto done_free;
2222 
2223 	bnx2fc_npiv_create_vports(lport, npiv_tbl);
2224 done_free:
2225 	kfree(npiv_tbl);
2226 done:
2227 	return 0;
2228 }
2229 
2230 /*
2231  * Deprecated: Use bnx2fc_enabled()
2232  */
2233 static int bnx2fc_enable(struct net_device *netdev)
2234 {
2235 	struct bnx2fc_interface *interface;
2236 	struct fcoe_ctlr *ctlr;
2237 	int rc = 0;
2238 
2239 	rtnl_lock();
2240 	mutex_lock(&bnx2fc_dev_lock);
2241 
2242 	interface = bnx2fc_interface_lookup(netdev);
2243 	ctlr = bnx2fc_to_ctlr(interface);
2244 	if (!interface) {
2245 		rc = -ENODEV;
2246 		pr_err(PFX "bnx2fc_enable: interface not found\n");
2247 	} else {
2248 		rc = __bnx2fc_enable(ctlr);
2249 	}
2250 
2251 	mutex_unlock(&bnx2fc_dev_lock);
2252 	rtnl_unlock();
2253 	return rc;
2254 }
2255 
2256 /**
2257  * bnx2fc_ctlr_enabled() - Enable or disable an FCoE Controller
2258  * @cdev: The FCoE Controller that is being enabled or disabled
2259  *
2260  * fcoe_sysfs will ensure that the state of 'enabled' has
2261  * changed, so no checking is necessary here. This routine simply
2262  * calls fcoe_enable or fcoe_disable, both of which are deprecated.
2263  * When those routines are removed the functionality can be merged
2264  * here.
2265  */
2266 static int bnx2fc_ctlr_enabled(struct fcoe_ctlr_device *cdev)
2267 {
2268 	struct fcoe_ctlr *ctlr = fcoe_ctlr_device_priv(cdev);
2269 
2270 	switch (cdev->enabled) {
2271 	case FCOE_CTLR_ENABLED:
2272 		return __bnx2fc_enable(ctlr);
2273 	case FCOE_CTLR_DISABLED:
2274 		return __bnx2fc_disable(ctlr);
2275 	case FCOE_CTLR_UNUSED:
2276 	default:
2277 		return -ENOTSUPP;
2278 	};
2279 }
2280 
2281 enum bnx2fc_create_link_state {
2282 	BNX2FC_CREATE_LINK_DOWN,
2283 	BNX2FC_CREATE_LINK_UP,
2284 };
2285 
2286 /**
2287  * _bnx2fc_create() - Create bnx2fc FCoE interface
2288  * @netdev  :   The net_device object the Ethernet interface to create on
2289  * @fip_mode:   The FIP mode for this creation
2290  * @link_state: The ctlr link state on creation
2291  *
2292  * Called from either the libfcoe 'create' module parameter
2293  * via fcoe_create or from fcoe_syfs's ctlr_create file.
2294  *
2295  * libfcoe's 'create' module parameter is deprecated so some
2296  * consolidation of code can be done when that interface is
2297  * removed.
2298  *
2299  * Returns: 0 for success
2300  */
2301 static int _bnx2fc_create(struct net_device *netdev,
2302 			  enum fip_mode fip_mode,
2303 			  enum bnx2fc_create_link_state link_state)
2304 {
2305 	struct fcoe_ctlr_device *cdev;
2306 	struct fcoe_ctlr *ctlr;
2307 	struct bnx2fc_interface *interface;
2308 	struct bnx2fc_hba *hba;
2309 	struct net_device *phys_dev = netdev;
2310 	struct fc_lport *lport;
2311 	struct ethtool_drvinfo drvinfo;
2312 	int rc = 0;
2313 	int vlan_id = 0;
2314 
2315 	BNX2FC_MISC_DBG("Entered bnx2fc_create\n");
2316 	if (fip_mode != FIP_MODE_FABRIC) {
2317 		printk(KERN_ERR "fip mode not FABRIC\n");
2318 		return -EIO;
2319 	}
2320 
2321 	rtnl_lock();
2322 
2323 	mutex_lock(&bnx2fc_dev_lock);
2324 
2325 	if (!try_module_get(THIS_MODULE)) {
2326 		rc = -EINVAL;
2327 		goto mod_err;
2328 	}
2329 
2330 	/* obtain physical netdev */
2331 	if (is_vlan_dev(netdev))
2332 		phys_dev = vlan_dev_real_dev(netdev);
2333 
2334 	/* verify if the physical device is a netxtreme2 device */
2335 	if (phys_dev->ethtool_ops && phys_dev->ethtool_ops->get_drvinfo) {
2336 		memset(&drvinfo, 0, sizeof(drvinfo));
2337 		phys_dev->ethtool_ops->get_drvinfo(phys_dev, &drvinfo);
2338 		if (strncmp(drvinfo.driver, "bnx2x", strlen("bnx2x"))) {
2339 			printk(KERN_ERR PFX "Not a netxtreme2 device\n");
2340 			rc = -EINVAL;
2341 			goto netdev_err;
2342 		}
2343 	} else {
2344 		printk(KERN_ERR PFX "unable to obtain drv_info\n");
2345 		rc = -EINVAL;
2346 		goto netdev_err;
2347 	}
2348 
2349 	/* obtain interface and initialize rest of the structure */
2350 	hba = bnx2fc_hba_lookup(phys_dev);
2351 	if (!hba) {
2352 		rc = -ENODEV;
2353 		printk(KERN_ERR PFX "bnx2fc_create: hba not found\n");
2354 		goto netdev_err;
2355 	}
2356 
2357 	if (bnx2fc_interface_lookup(netdev)) {
2358 		rc = -EEXIST;
2359 		goto netdev_err;
2360 	}
2361 
2362 	interface = bnx2fc_interface_create(hba, netdev, fip_mode);
2363 	if (!interface) {
2364 		printk(KERN_ERR PFX "bnx2fc_interface_create failed\n");
2365 		rc = -ENOMEM;
2366 		goto netdev_err;
2367 	}
2368 
2369 	if (is_vlan_dev(netdev)) {
2370 		vlan_id = vlan_dev_vlan_id(netdev);
2371 		interface->vlan_enabled = 1;
2372 	}
2373 
2374 	ctlr = bnx2fc_to_ctlr(interface);
2375 	cdev = fcoe_ctlr_to_ctlr_dev(ctlr);
2376 	interface->vlan_id = vlan_id;
2377 	interface->tm_timeout = BNX2FC_TM_TIMEOUT;
2378 
2379 	interface->timer_work_queue =
2380 			create_singlethread_workqueue("bnx2fc_timer_wq");
2381 	if (!interface->timer_work_queue) {
2382 		printk(KERN_ERR PFX "ulp_init could not create timer_wq\n");
2383 		rc = -EINVAL;
2384 		goto ifput_err;
2385 	}
2386 
2387 	lport = bnx2fc_if_create(interface, &cdev->dev, 0);
2388 	if (!lport) {
2389 		printk(KERN_ERR PFX "Failed to create interface (%s)\n",
2390 			netdev->name);
2391 		rc = -EINVAL;
2392 		goto if_create_err;
2393 	}
2394 
2395 	/* Add interface to if_list */
2396 	list_add_tail(&interface->list, &if_list);
2397 
2398 	lport->boot_time = jiffies;
2399 
2400 	/* Make this master N_port */
2401 	ctlr->lp = lport;
2402 
2403 	if (link_state == BNX2FC_CREATE_LINK_UP)
2404 		cdev->enabled = FCOE_CTLR_ENABLED;
2405 	else
2406 		cdev->enabled = FCOE_CTLR_DISABLED;
2407 
2408 	if (link_state == BNX2FC_CREATE_LINK_UP &&
2409 	    !bnx2fc_link_ok(lport)) {
2410 		fcoe_ctlr_link_up(ctlr);
2411 		fc_host_port_type(lport->host) = FC_PORTTYPE_NPORT;
2412 		set_bit(ADAPTER_STATE_READY, &interface->hba->adapter_state);
2413 	}
2414 
2415 	BNX2FC_HBA_DBG(lport, "create: START DISC\n");
2416 	bnx2fc_start_disc(interface);
2417 
2418 	if (link_state == BNX2FC_CREATE_LINK_UP)
2419 		interface->enabled = true;
2420 
2421 	/*
2422 	 * Release from kref_init in bnx2fc_interface_setup, on success
2423 	 * lport should be holding a reference taken in bnx2fc_if_create
2424 	 */
2425 	bnx2fc_interface_put(interface);
2426 	/* put netdev that was held while calling dev_get_by_name */
2427 	mutex_unlock(&bnx2fc_dev_lock);
2428 	rtnl_unlock();
2429 	return 0;
2430 
2431 if_create_err:
2432 	destroy_workqueue(interface->timer_work_queue);
2433 ifput_err:
2434 	bnx2fc_net_cleanup(interface);
2435 	bnx2fc_interface_put(interface);
2436 	goto mod_err;
2437 netdev_err:
2438 	module_put(THIS_MODULE);
2439 mod_err:
2440 	mutex_unlock(&bnx2fc_dev_lock);
2441 	rtnl_unlock();
2442 	return rc;
2443 }
2444 
2445 /**
2446  * bnx2fc_create() - Create a bnx2fc interface
2447  * @netdev  : The net_device object the Ethernet interface to create on
2448  * @fip_mode: The FIP mode for this creation
2449  *
2450  * Called from fcoe transport
2451  *
2452  * Returns: 0 for success
2453  */
2454 static int bnx2fc_create(struct net_device *netdev, enum fip_mode fip_mode)
2455 {
2456 	return _bnx2fc_create(netdev, fip_mode, BNX2FC_CREATE_LINK_UP);
2457 }
2458 
2459 /**
2460  * bnx2fc_ctlr_alloc() - Allocate a bnx2fc interface from fcoe_sysfs
2461  * @netdev: The net_device to be used by the allocated FCoE Controller
2462  *
2463  * This routine is called from fcoe_sysfs. It will start the fcoe_ctlr
2464  * in a link_down state. The allows the user an opportunity to configure
2465  * the FCoE Controller from sysfs before enabling the FCoE Controller.
2466  *
2467  * Creating in with this routine starts the FCoE Controller in Fabric
2468  * mode. The user can change to VN2VN or another mode before enabling.
2469  */
2470 static int bnx2fc_ctlr_alloc(struct net_device *netdev)
2471 {
2472 	return _bnx2fc_create(netdev, FIP_MODE_FABRIC,
2473 			      BNX2FC_CREATE_LINK_DOWN);
2474 }
2475 
2476 /**
2477  * bnx2fc_find_hba_for_cnic - maps cnic instance to bnx2fc hba instance
2478  *
2479  * @cnic:	Pointer to cnic device instance
2480  *
2481  **/
2482 static struct bnx2fc_hba *bnx2fc_find_hba_for_cnic(struct cnic_dev *cnic)
2483 {
2484 	struct bnx2fc_hba *hba;
2485 
2486 	/* Called with bnx2fc_dev_lock held */
2487 	list_for_each_entry(hba, &adapter_list, list) {
2488 		if (hba->cnic == cnic)
2489 			return hba;
2490 	}
2491 	return NULL;
2492 }
2493 
2494 static struct bnx2fc_interface *bnx2fc_interface_lookup(struct net_device
2495 							*netdev)
2496 {
2497 	struct bnx2fc_interface *interface;
2498 
2499 	/* Called with bnx2fc_dev_lock held */
2500 	list_for_each_entry(interface, &if_list, list) {
2501 		if (interface->netdev == netdev)
2502 			return interface;
2503 	}
2504 	return NULL;
2505 }
2506 
2507 static struct bnx2fc_hba *bnx2fc_hba_lookup(struct net_device
2508 						      *phys_dev)
2509 {
2510 	struct bnx2fc_hba *hba;
2511 
2512 	/* Called with bnx2fc_dev_lock held */
2513 	list_for_each_entry(hba, &adapter_list, list) {
2514 		if (hba->phys_dev == phys_dev)
2515 			return hba;
2516 	}
2517 	printk(KERN_ERR PFX "adapter_lookup: hba NULL\n");
2518 	return NULL;
2519 }
2520 
2521 /**
2522  * bnx2fc_ulp_exit - shuts down adapter instance and frees all resources
2523  *
2524  * @dev:	cnic device handle
2525  */
2526 static void bnx2fc_ulp_exit(struct cnic_dev *dev)
2527 {
2528 	struct bnx2fc_hba *hba;
2529 	struct bnx2fc_interface *interface, *tmp;
2530 
2531 	BNX2FC_MISC_DBG("Entered bnx2fc_ulp_exit\n");
2532 
2533 	if (!test_bit(CNIC_F_BNX2X_CLASS, &dev->flags)) {
2534 		printk(KERN_ERR PFX "bnx2fc port check: %s, flags: %lx\n",
2535 			dev->netdev->name, dev->flags);
2536 		return;
2537 	}
2538 
2539 	mutex_lock(&bnx2fc_dev_lock);
2540 	hba = bnx2fc_find_hba_for_cnic(dev);
2541 	if (!hba) {
2542 		printk(KERN_ERR PFX "bnx2fc_ulp_exit: hba not found, dev 0%p\n",
2543 		       dev);
2544 		mutex_unlock(&bnx2fc_dev_lock);
2545 		return;
2546 	}
2547 
2548 	list_del_init(&hba->list);
2549 	adapter_count--;
2550 
2551 	list_for_each_entry_safe(interface, tmp, &if_list, list)
2552 		/* destroy not called yet, move to quiesced list */
2553 		if (interface->hba == hba)
2554 			__bnx2fc_destroy(interface);
2555 	mutex_unlock(&bnx2fc_dev_lock);
2556 
2557 	/* Ensure ALL destroy work has been completed before return */
2558 	flush_workqueue(bnx2fc_wq);
2559 
2560 	bnx2fc_ulp_stop(hba);
2561 	/* unregister cnic device */
2562 	if (test_and_clear_bit(BNX2FC_CNIC_REGISTERED, &hba->reg_with_cnic))
2563 		hba->cnic->unregister_device(hba->cnic, CNIC_ULP_FCOE);
2564 	bnx2fc_hba_destroy(hba);
2565 }
2566 
2567 static void bnx2fc_rport_terminate_io(struct fc_rport *rport)
2568 {
2569 	/* This is a no-op */
2570 }
2571 
2572 /**
2573  * bnx2fc_fcoe_reset - Resets the fcoe
2574  *
2575  * @shost: shost the reset is from
2576  *
2577  * Returns: always 0
2578  */
2579 static int bnx2fc_fcoe_reset(struct Scsi_Host *shost)
2580 {
2581 	struct fc_lport *lport = shost_priv(shost);
2582 	fc_lport_reset(lport);
2583 	return 0;
2584 }
2585 
2586 
2587 static bool bnx2fc_match(struct net_device *netdev)
2588 {
2589 	struct net_device *phys_dev = netdev;
2590 
2591 	mutex_lock(&bnx2fc_dev_lock);
2592 	if (is_vlan_dev(netdev))
2593 		phys_dev = vlan_dev_real_dev(netdev);
2594 
2595 	if (bnx2fc_hba_lookup(phys_dev)) {
2596 		mutex_unlock(&bnx2fc_dev_lock);
2597 		return true;
2598 	}
2599 
2600 	mutex_unlock(&bnx2fc_dev_lock);
2601 	return false;
2602 }
2603 
2604 
2605 static struct fcoe_transport bnx2fc_transport = {
2606 	.name = {"bnx2fc"},
2607 	.attached = false,
2608 	.list = LIST_HEAD_INIT(bnx2fc_transport.list),
2609 	.alloc = bnx2fc_ctlr_alloc,
2610 	.match = bnx2fc_match,
2611 	.create = bnx2fc_create,
2612 	.destroy = bnx2fc_destroy,
2613 	.enable = bnx2fc_enable,
2614 	.disable = bnx2fc_disable,
2615 };
2616 
2617 /**
2618  * bnx2fc_cpu_online - Create a receive thread for an  online CPU
2619  *
2620  * @cpu: cpu index for the online cpu
2621  */
2622 static int bnx2fc_cpu_online(unsigned int cpu)
2623 {
2624 	struct bnx2fc_percpu_s *p;
2625 	struct task_struct *thread;
2626 
2627 	p = &per_cpu(bnx2fc_percpu, cpu);
2628 
2629 	thread = kthread_create_on_node(bnx2fc_percpu_io_thread,
2630 					(void *)p, cpu_to_node(cpu),
2631 					"bnx2fc_thread/%d", cpu);
2632 	if (IS_ERR(thread))
2633 		return PTR_ERR(thread);
2634 
2635 	/* bind thread to the cpu */
2636 	kthread_bind(thread, cpu);
2637 	p->iothread = thread;
2638 	wake_up_process(thread);
2639 	return 0;
2640 }
2641 
2642 static int bnx2fc_cpu_offline(unsigned int cpu)
2643 {
2644 	struct bnx2fc_percpu_s *p;
2645 	struct task_struct *thread;
2646 	struct bnx2fc_work *work, *tmp;
2647 
2648 	BNX2FC_MISC_DBG("destroying io thread for CPU %d\n", cpu);
2649 
2650 	/* Prevent any new work from being queued for this CPU */
2651 	p = &per_cpu(bnx2fc_percpu, cpu);
2652 	spin_lock_bh(&p->fp_work_lock);
2653 	thread = p->iothread;
2654 	p->iothread = NULL;
2655 
2656 	/* Free all work in the list */
2657 	list_for_each_entry_safe(work, tmp, &p->work_list, list) {
2658 		list_del_init(&work->list);
2659 		bnx2fc_process_cq_compl(work->tgt, work->wqe, work->rq_data,
2660 					work->num_rq, work->task);
2661 		kfree(work);
2662 	}
2663 
2664 	spin_unlock_bh(&p->fp_work_lock);
2665 
2666 	if (thread)
2667 		kthread_stop(thread);
2668 	return 0;
2669 }
2670 
2671 static int bnx2fc_slave_configure(struct scsi_device *sdev)
2672 {
2673 	if (!bnx2fc_queue_depth)
2674 		return 0;
2675 
2676 	scsi_change_queue_depth(sdev, bnx2fc_queue_depth);
2677 	return 0;
2678 }
2679 
2680 static enum cpuhp_state bnx2fc_online_state;
2681 
2682 /**
2683  * bnx2fc_mod_init - module init entry point
2684  *
2685  * Initialize driver wide global data structures, and register
2686  * with cnic module
2687  **/
2688 static int __init bnx2fc_mod_init(void)
2689 {
2690 	struct fcoe_percpu_s *bg;
2691 	struct task_struct *l2_thread;
2692 	int rc = 0;
2693 	unsigned int cpu = 0;
2694 	struct bnx2fc_percpu_s *p;
2695 
2696 	printk(KERN_INFO PFX "%s", version);
2697 
2698 	/* register as a fcoe transport */
2699 	rc = fcoe_transport_attach(&bnx2fc_transport);
2700 	if (rc) {
2701 		printk(KERN_ERR "failed to register an fcoe transport, check "
2702 			"if libfcoe is loaded\n");
2703 		goto out;
2704 	}
2705 
2706 	INIT_LIST_HEAD(&adapter_list);
2707 	INIT_LIST_HEAD(&if_list);
2708 	mutex_init(&bnx2fc_dev_lock);
2709 	adapter_count = 0;
2710 
2711 	/* Attach FC transport template */
2712 	rc = bnx2fc_attach_transport();
2713 	if (rc)
2714 		goto detach_ft;
2715 
2716 	bnx2fc_wq = alloc_workqueue("bnx2fc", 0, 0);
2717 	if (!bnx2fc_wq) {
2718 		rc = -ENOMEM;
2719 		goto release_bt;
2720 	}
2721 
2722 	bg = &bnx2fc_global;
2723 	skb_queue_head_init(&bg->fcoe_rx_list);
2724 	l2_thread = kthread_create(bnx2fc_l2_rcv_thread,
2725 				   (void *)bg,
2726 				   "bnx2fc_l2_thread");
2727 	if (IS_ERR(l2_thread)) {
2728 		rc = PTR_ERR(l2_thread);
2729 		goto free_wq;
2730 	}
2731 	wake_up_process(l2_thread);
2732 	spin_lock_bh(&bg->fcoe_rx_list.lock);
2733 	bg->kthread = l2_thread;
2734 	spin_unlock_bh(&bg->fcoe_rx_list.lock);
2735 
2736 	for_each_possible_cpu(cpu) {
2737 		p = &per_cpu(bnx2fc_percpu, cpu);
2738 		INIT_LIST_HEAD(&p->work_list);
2739 		spin_lock_init(&p->fp_work_lock);
2740 	}
2741 
2742 	rc = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "scsi/bnx2fc:online",
2743 			       bnx2fc_cpu_online, bnx2fc_cpu_offline);
2744 	if (rc < 0)
2745 		goto stop_thread;
2746 	bnx2fc_online_state = rc;
2747 
2748 	cnic_register_driver(CNIC_ULP_FCOE, &bnx2fc_cnic_cb);
2749 	return 0;
2750 
2751 stop_thread:
2752 	kthread_stop(l2_thread);
2753 free_wq:
2754 	destroy_workqueue(bnx2fc_wq);
2755 release_bt:
2756 	bnx2fc_release_transport();
2757 detach_ft:
2758 	fcoe_transport_detach(&bnx2fc_transport);
2759 out:
2760 	return rc;
2761 }
2762 
2763 static void __exit bnx2fc_mod_exit(void)
2764 {
2765 	LIST_HEAD(to_be_deleted);
2766 	struct bnx2fc_hba *hba, *next;
2767 	struct fcoe_percpu_s *bg;
2768 	struct task_struct *l2_thread;
2769 	struct sk_buff *skb;
2770 
2771 	/*
2772 	 * NOTE: Since cnic calls register_driver routine rtnl_lock,
2773 	 * it will have higher precedence than bnx2fc_dev_lock.
2774 	 * unregister_device() cannot be called with bnx2fc_dev_lock
2775 	 * held.
2776 	 */
2777 	mutex_lock(&bnx2fc_dev_lock);
2778 	list_splice_init(&adapter_list, &to_be_deleted);
2779 	adapter_count = 0;
2780 	mutex_unlock(&bnx2fc_dev_lock);
2781 
2782 	/* Unregister with cnic */
2783 	list_for_each_entry_safe(hba, next, &to_be_deleted, list) {
2784 		list_del_init(&hba->list);
2785 		printk(KERN_ERR PFX "MOD_EXIT:destroy hba = 0x%p\n",
2786 		       hba);
2787 		bnx2fc_ulp_stop(hba);
2788 		/* unregister cnic device */
2789 		if (test_and_clear_bit(BNX2FC_CNIC_REGISTERED,
2790 				       &hba->reg_with_cnic))
2791 			hba->cnic->unregister_device(hba->cnic,
2792 							 CNIC_ULP_FCOE);
2793 		bnx2fc_hba_destroy(hba);
2794 	}
2795 	cnic_unregister_driver(CNIC_ULP_FCOE);
2796 
2797 	/* Destroy global thread */
2798 	bg = &bnx2fc_global;
2799 	spin_lock_bh(&bg->fcoe_rx_list.lock);
2800 	l2_thread = bg->kthread;
2801 	bg->kthread = NULL;
2802 	while ((skb = __skb_dequeue(&bg->fcoe_rx_list)) != NULL)
2803 		kfree_skb(skb);
2804 
2805 	spin_unlock_bh(&bg->fcoe_rx_list.lock);
2806 
2807 	if (l2_thread)
2808 		kthread_stop(l2_thread);
2809 
2810 	cpuhp_remove_state(bnx2fc_online_state);
2811 
2812 	destroy_workqueue(bnx2fc_wq);
2813 	/*
2814 	 * detach from scsi transport
2815 	 * must happen after all destroys are done
2816 	 */
2817 	bnx2fc_release_transport();
2818 
2819 	/* detach from fcoe transport */
2820 	fcoe_transport_detach(&bnx2fc_transport);
2821 }
2822 
2823 module_init(bnx2fc_mod_init);
2824 module_exit(bnx2fc_mod_exit);
2825 
2826 static struct fcoe_sysfs_function_template bnx2fc_fcoe_sysfs_templ = {
2827 	.set_fcoe_ctlr_enabled = bnx2fc_ctlr_enabled,
2828 	.get_fcoe_ctlr_link_fail = fcoe_ctlr_get_lesb,
2829 	.get_fcoe_ctlr_vlink_fail = fcoe_ctlr_get_lesb,
2830 	.get_fcoe_ctlr_miss_fka = fcoe_ctlr_get_lesb,
2831 	.get_fcoe_ctlr_symb_err = fcoe_ctlr_get_lesb,
2832 	.get_fcoe_ctlr_err_block = fcoe_ctlr_get_lesb,
2833 	.get_fcoe_ctlr_fcs_error = fcoe_ctlr_get_lesb,
2834 
2835 	.get_fcoe_fcf_selected = fcoe_fcf_get_selected,
2836 	.get_fcoe_fcf_vlan_id = bnx2fc_fcf_get_vlan_id,
2837 };
2838 
2839 static struct fc_function_template bnx2fc_transport_function = {
2840 	.show_host_node_name = 1,
2841 	.show_host_port_name = 1,
2842 	.show_host_supported_classes = 1,
2843 	.show_host_supported_fc4s = 1,
2844 	.show_host_active_fc4s = 1,
2845 	.show_host_maxframe_size = 1,
2846 
2847 	.show_host_port_id = 1,
2848 	.show_host_supported_speeds = 1,
2849 	.get_host_speed = fc_get_host_speed,
2850 	.show_host_speed = 1,
2851 	.show_host_port_type = 1,
2852 	.get_host_port_state = fc_get_host_port_state,
2853 	.show_host_port_state = 1,
2854 	.show_host_symbolic_name = 1,
2855 
2856 	.dd_fcrport_size = (sizeof(struct fc_rport_libfc_priv) +
2857 				sizeof(struct bnx2fc_rport)),
2858 	.show_rport_maxframe_size = 1,
2859 	.show_rport_supported_classes = 1,
2860 
2861 	.show_host_fabric_name = 1,
2862 	.show_starget_node_name = 1,
2863 	.show_starget_port_name = 1,
2864 	.show_starget_port_id = 1,
2865 	.set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
2866 	.show_rport_dev_loss_tmo = 1,
2867 	.get_fc_host_stats = bnx2fc_get_host_stats,
2868 
2869 	.issue_fc_host_lip = bnx2fc_fcoe_reset,
2870 
2871 	.terminate_rport_io = bnx2fc_rport_terminate_io,
2872 
2873 	.vport_create = bnx2fc_vport_create,
2874 	.vport_delete = bnx2fc_vport_destroy,
2875 	.vport_disable = bnx2fc_vport_disable,
2876 	.bsg_request = fc_lport_bsg_request,
2877 };
2878 
2879 static struct fc_function_template bnx2fc_vport_xport_function = {
2880 	.show_host_node_name = 1,
2881 	.show_host_port_name = 1,
2882 	.show_host_supported_classes = 1,
2883 	.show_host_supported_fc4s = 1,
2884 	.show_host_active_fc4s = 1,
2885 	.show_host_maxframe_size = 1,
2886 
2887 	.show_host_port_id = 1,
2888 	.show_host_supported_speeds = 1,
2889 	.get_host_speed = fc_get_host_speed,
2890 	.show_host_speed = 1,
2891 	.show_host_port_type = 1,
2892 	.get_host_port_state = fc_get_host_port_state,
2893 	.show_host_port_state = 1,
2894 	.show_host_symbolic_name = 1,
2895 
2896 	.dd_fcrport_size = (sizeof(struct fc_rport_libfc_priv) +
2897 				sizeof(struct bnx2fc_rport)),
2898 	.show_rport_maxframe_size = 1,
2899 	.show_rport_supported_classes = 1,
2900 
2901 	.show_host_fabric_name = 1,
2902 	.show_starget_node_name = 1,
2903 	.show_starget_port_name = 1,
2904 	.show_starget_port_id = 1,
2905 	.set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
2906 	.show_rport_dev_loss_tmo = 1,
2907 	.get_fc_host_stats = fc_get_host_stats,
2908 	.issue_fc_host_lip = bnx2fc_fcoe_reset,
2909 	.terminate_rport_io = fc_rport_terminate_io,
2910 	.bsg_request = fc_lport_bsg_request,
2911 };
2912 
2913 /*
2914  * Additional scsi_host attributes.
2915  */
2916 static ssize_t
2917 bnx2fc_tm_timeout_show(struct device *dev, struct device_attribute *attr,
2918 	char *buf)
2919 {
2920 	struct Scsi_Host *shost = class_to_shost(dev);
2921 	struct fc_lport *lport = shost_priv(shost);
2922 	struct fcoe_port *port = lport_priv(lport);
2923 	struct bnx2fc_interface *interface = port->priv;
2924 
2925 	sprintf(buf, "%u\n", interface->tm_timeout);
2926 	return strlen(buf);
2927 }
2928 
2929 static ssize_t
2930 bnx2fc_tm_timeout_store(struct device *dev,
2931 	struct device_attribute *attr, const char *buf, size_t count)
2932 {
2933 	struct Scsi_Host *shost = class_to_shost(dev);
2934 	struct fc_lport *lport = shost_priv(shost);
2935 	struct fcoe_port *port = lport_priv(lport);
2936 	struct bnx2fc_interface *interface = port->priv;
2937 	int rval, val;
2938 
2939 	rval = kstrtouint(buf, 10, &val);
2940 	if (rval)
2941 		return rval;
2942 	if (val > 255)
2943 		return -ERANGE;
2944 
2945 	interface->tm_timeout = (u8)val;
2946 	return strlen(buf);
2947 }
2948 
2949 static DEVICE_ATTR(tm_timeout, S_IRUGO|S_IWUSR, bnx2fc_tm_timeout_show,
2950 	bnx2fc_tm_timeout_store);
2951 
2952 static struct device_attribute *bnx2fc_host_attrs[] = {
2953 	&dev_attr_tm_timeout,
2954 	NULL,
2955 };
2956 
2957 /*
2958  * scsi_host_template structure used while registering with SCSI-ml
2959  */
2960 static struct scsi_host_template bnx2fc_shost_template = {
2961 	.module			= THIS_MODULE,
2962 	.name			= "QLogic Offload FCoE Initiator",
2963 	.queuecommand		= bnx2fc_queuecommand,
2964 	.eh_timed_out		= fc_eh_timed_out,
2965 	.eh_abort_handler	= bnx2fc_eh_abort,	  /* abts */
2966 	.eh_device_reset_handler = bnx2fc_eh_device_reset, /* lun reset */
2967 	.eh_target_reset_handler = bnx2fc_eh_target_reset, /* tgt reset */
2968 	.eh_host_reset_handler	= fc_eh_host_reset,
2969 	.slave_alloc		= fc_slave_alloc,
2970 	.change_queue_depth	= scsi_change_queue_depth,
2971 	.this_id		= -1,
2972 	.cmd_per_lun		= 3,
2973 	.sg_tablesize		= BNX2FC_MAX_BDS_PER_CMD,
2974 	.dma_boundary           = 0x7fff,
2975 	.max_sectors		= 0x3fbf,
2976 	.track_queue_depth	= 1,
2977 	.slave_configure	= bnx2fc_slave_configure,
2978 	.shost_attrs		= bnx2fc_host_attrs,
2979 };
2980 
2981 static struct libfc_function_template bnx2fc_libfc_fcn_templ = {
2982 	.frame_send		= bnx2fc_xmit,
2983 	.elsct_send		= bnx2fc_elsct_send,
2984 	.fcp_abort_io		= bnx2fc_abort_io,
2985 	.fcp_cleanup		= bnx2fc_cleanup,
2986 	.get_lesb		= fcoe_get_lesb,
2987 	.rport_event_callback	= bnx2fc_rport_event_handler,
2988 };
2989 
2990 /*
2991  * bnx2fc_cnic_cb - global template of bnx2fc - cnic driver interface
2992  *			structure carrying callback function pointers
2993  */
2994 static struct cnic_ulp_ops bnx2fc_cnic_cb = {
2995 	.owner			= THIS_MODULE,
2996 	.cnic_init		= bnx2fc_ulp_init,
2997 	.cnic_exit		= bnx2fc_ulp_exit,
2998 	.cnic_start		= bnx2fc_ulp_start,
2999 	.cnic_stop		= bnx2fc_ulp_stop,
3000 	.indicate_kcqes		= bnx2fc_indicate_kcqe,
3001 	.indicate_netevent	= bnx2fc_indicate_netevent,
3002 	.cnic_get_stats		= bnx2fc_ulp_get_stats,
3003 };
3004