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