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