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