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