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