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