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