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