Merge branch 'sched-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[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 static 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 static struct bnx2fc_interface *
1414 bnx2fc_interface_create(struct bnx2fc_hba *hba,
1415                         struct net_device *netdev,
1416                         enum fip_state fip_mode)
1417 {
1418         struct fcoe_ctlr_device *ctlr_dev;
1419         struct bnx2fc_interface *interface;
1420         struct fcoe_ctlr *ctlr;
1421         int size;
1422         int rc = 0;
1423
1424         size = (sizeof(*interface) + sizeof(struct fcoe_ctlr));
1425         ctlr_dev = fcoe_ctlr_device_add(&netdev->dev, &bnx2fc_fcoe_sysfs_templ,
1426                                          size);
1427         if (!ctlr_dev) {
1428                 printk(KERN_ERR PFX "Unable to allocate interface structure\n");
1429                 return NULL;
1430         }
1431         ctlr = fcoe_ctlr_device_priv(ctlr_dev);
1432         ctlr->cdev = ctlr_dev;
1433         interface = fcoe_ctlr_priv(ctlr);
1434         dev_hold(netdev);
1435         kref_init(&interface->kref);
1436         interface->hba = hba;
1437         interface->netdev = netdev;
1438
1439         /* Initialize FIP */
1440         fcoe_ctlr_init(ctlr, fip_mode);
1441         ctlr->send = bnx2fc_fip_send;
1442         ctlr->update_mac = bnx2fc_update_src_mac;
1443         ctlr->get_src_addr = bnx2fc_get_src_mac;
1444         set_bit(BNX2FC_CTLR_INIT_DONE, &interface->if_flags);
1445
1446         rc = bnx2fc_interface_setup(interface);
1447         if (!rc)
1448                 return interface;
1449
1450         fcoe_ctlr_destroy(ctlr);
1451         dev_put(netdev);
1452         fcoe_ctlr_device_delete(ctlr_dev);
1453         return NULL;
1454 }
1455
1456 /**
1457  * bnx2fc_if_create - Create FCoE instance on a given interface
1458  *
1459  * @interface:  FCoE interface to create a local port on
1460  * @parent:     Device pointer to be the parent in sysfs for the SCSI host
1461  * @npiv:       Indicates if the port is vport or not
1462  *
1463  * Creates a fc_lport instance and a Scsi_Host instance and configure them.
1464  *
1465  * Returns:     Allocated fc_lport or an error pointer
1466  */
1467 static struct fc_lport *bnx2fc_if_create(struct bnx2fc_interface *interface,
1468                                   struct device *parent, int npiv)
1469 {
1470         struct fcoe_ctlr        *ctlr = bnx2fc_to_ctlr(interface);
1471         struct fc_lport         *lport, *n_port;
1472         struct fcoe_port        *port;
1473         struct Scsi_Host        *shost;
1474         struct fc_vport         *vport = dev_to_vport(parent);
1475         struct bnx2fc_lport     *blport;
1476         struct bnx2fc_hba       *hba = interface->hba;
1477         int                     rc = 0;
1478
1479         blport = kzalloc(sizeof(struct bnx2fc_lport), GFP_KERNEL);
1480         if (!blport) {
1481                 BNX2FC_HBA_DBG(ctlr->lp, "Unable to alloc blport\n");
1482                 return NULL;
1483         }
1484
1485         /* Allocate Scsi_Host structure */
1486         bnx2fc_shost_template.can_queue = hba->max_outstanding_cmds;
1487         if (!npiv)
1488                 lport = libfc_host_alloc(&bnx2fc_shost_template, sizeof(*port));
1489         else
1490                 lport = libfc_vport_create(vport, sizeof(*port));
1491
1492         if (!lport) {
1493                 printk(KERN_ERR PFX "could not allocate scsi host structure\n");
1494                 goto free_blport;
1495         }
1496         shost = lport->host;
1497         port = lport_priv(lport);
1498         port->lport = lport;
1499         port->priv = interface;
1500         port->get_netdev = bnx2fc_netdev;
1501         INIT_WORK(&port->destroy_work, bnx2fc_destroy_work);
1502
1503         /* Configure fcoe_port */
1504         rc = bnx2fc_lport_config(lport);
1505         if (rc)
1506                 goto lp_config_err;
1507
1508         if (npiv) {
1509                 printk(KERN_ERR PFX "Setting vport names, 0x%llX 0x%llX\n",
1510                         vport->node_name, vport->port_name);
1511                 fc_set_wwnn(lport, vport->node_name);
1512                 fc_set_wwpn(lport, vport->port_name);
1513         }
1514         /* Configure netdev and networking properties of the lport */
1515         rc = bnx2fc_net_config(lport, interface->netdev);
1516         if (rc) {
1517                 printk(KERN_ERR PFX "Error on bnx2fc_net_config\n");
1518                 goto lp_config_err;
1519         }
1520
1521         rc = bnx2fc_shost_config(lport, parent);
1522         if (rc) {
1523                 printk(KERN_ERR PFX "Couldnt configure shost for %s\n",
1524                         interface->netdev->name);
1525                 goto lp_config_err;
1526         }
1527
1528         /* Initialize the libfc library */
1529         rc = bnx2fc_libfc_config(lport);
1530         if (rc) {
1531                 printk(KERN_ERR PFX "Couldnt configure libfc\n");
1532                 goto shost_err;
1533         }
1534         fc_host_port_type(lport->host) = FC_PORTTYPE_UNKNOWN;
1535
1536         if (bnx2fc_devloss_tmo)
1537                 fc_host_dev_loss_tmo(shost) = bnx2fc_devloss_tmo;
1538
1539         /* Allocate exchange manager */
1540         if (!npiv)
1541                 rc = bnx2fc_em_config(lport, hba);
1542         else {
1543                 shost = vport_to_shost(vport);
1544                 n_port = shost_priv(shost);
1545                 rc = fc_exch_mgr_list_clone(n_port, lport);
1546         }
1547
1548         if (rc) {
1549                 printk(KERN_ERR PFX "Error on bnx2fc_em_config\n");
1550                 goto shost_err;
1551         }
1552
1553         bnx2fc_interface_get(interface);
1554
1555         spin_lock_bh(&hba->hba_lock);
1556         blport->lport = lport;
1557         list_add_tail(&blport->list, &hba->vports);
1558         spin_unlock_bh(&hba->hba_lock);
1559
1560         return lport;
1561
1562 shost_err:
1563         scsi_remove_host(shost);
1564 lp_config_err:
1565         scsi_host_put(lport->host);
1566 free_blport:
1567         kfree(blport);
1568         return NULL;
1569 }
1570
1571 static void bnx2fc_net_cleanup(struct bnx2fc_interface *interface)
1572 {
1573         /* Dont listen for Ethernet packets anymore */
1574         __dev_remove_pack(&interface->fcoe_packet_type);
1575         __dev_remove_pack(&interface->fip_packet_type);
1576         synchronize_net();
1577 }
1578
1579 static void bnx2fc_interface_cleanup(struct bnx2fc_interface *interface)
1580 {
1581         struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
1582         struct fc_lport *lport = ctlr->lp;
1583         struct fcoe_port *port = lport_priv(lport);
1584         struct bnx2fc_hba *hba = interface->hba;
1585
1586         /* Stop the transmit retry timer */
1587         del_timer_sync(&port->timer);
1588
1589         /* Free existing transmit skbs */
1590         fcoe_clean_pending_queue(lport);
1591
1592         bnx2fc_net_cleanup(interface);
1593
1594         bnx2fc_free_vport(hba, lport);
1595 }
1596
1597 static void bnx2fc_if_destroy(struct fc_lport *lport)
1598 {
1599
1600         /* Free queued packets for the receive thread */
1601         bnx2fc_clean_rx_queue(lport);
1602
1603         /* Detach from scsi-ml */
1604         fc_remove_host(lport->host);
1605         scsi_remove_host(lport->host);
1606
1607         /*
1608          * Note that only the physical lport will have the exchange manager.
1609          * for vports, this function is NOP
1610          */
1611         fc_exch_mgr_free(lport);
1612
1613         /* Free memory used by statistical counters */
1614         fc_lport_free_stats(lport);
1615
1616         /* Release Scsi_Host */
1617         scsi_host_put(lport->host);
1618 }
1619
1620 static void __bnx2fc_destroy(struct bnx2fc_interface *interface)
1621 {
1622         struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
1623         struct fc_lport *lport = ctlr->lp;
1624         struct fcoe_port *port = lport_priv(lport);
1625
1626         bnx2fc_interface_cleanup(interface);
1627         bnx2fc_stop(interface);
1628         list_del(&interface->list);
1629         bnx2fc_interface_put(interface);
1630         queue_work(bnx2fc_wq, &port->destroy_work);
1631 }
1632
1633 /**
1634  * bnx2fc_destroy - Destroy a bnx2fc FCoE interface
1635  *
1636  * @buffer: The name of the Ethernet interface to be destroyed
1637  * @kp:     The associated kernel parameter
1638  *
1639  * Called from sysfs.
1640  *
1641  * Returns: 0 for success
1642  */
1643 static int bnx2fc_destroy(struct net_device *netdev)
1644 {
1645         struct bnx2fc_interface *interface = NULL;
1646         struct workqueue_struct *timer_work_queue;
1647         struct fcoe_ctlr *ctlr;
1648         int rc = 0;
1649
1650         rtnl_lock();
1651         mutex_lock(&bnx2fc_dev_lock);
1652
1653         interface = bnx2fc_interface_lookup(netdev);
1654         ctlr = bnx2fc_to_ctlr(interface);
1655         if (!interface || !ctlr->lp) {
1656                 rc = -ENODEV;
1657                 printk(KERN_ERR PFX "bnx2fc_destroy: interface or lport not found\n");
1658                 goto netdev_err;
1659         }
1660
1661         timer_work_queue = interface->timer_work_queue;
1662         __bnx2fc_destroy(interface);
1663         destroy_workqueue(timer_work_queue);
1664
1665 netdev_err:
1666         mutex_unlock(&bnx2fc_dev_lock);
1667         rtnl_unlock();
1668         return rc;
1669 }
1670
1671 static void bnx2fc_destroy_work(struct work_struct *work)
1672 {
1673         struct fcoe_port *port;
1674         struct fc_lport *lport;
1675
1676         port = container_of(work, struct fcoe_port, destroy_work);
1677         lport = port->lport;
1678
1679         BNX2FC_HBA_DBG(lport, "Entered bnx2fc_destroy_work\n");
1680
1681         bnx2fc_if_destroy(lport);
1682 }
1683
1684 static void bnx2fc_unbind_adapter_devices(struct bnx2fc_hba *hba)
1685 {
1686         bnx2fc_free_fw_resc(hba);
1687         bnx2fc_free_task_ctx(hba);
1688 }
1689
1690 /**
1691  * bnx2fc_bind_adapter_devices - binds bnx2fc adapter with the associated
1692  *                      pci structure
1693  *
1694  * @hba:                Adapter instance
1695  */
1696 static int bnx2fc_bind_adapter_devices(struct bnx2fc_hba *hba)
1697 {
1698         if (bnx2fc_setup_task_ctx(hba))
1699                 goto mem_err;
1700
1701         if (bnx2fc_setup_fw_resc(hba))
1702                 goto mem_err;
1703
1704         return 0;
1705 mem_err:
1706         bnx2fc_unbind_adapter_devices(hba);
1707         return -ENOMEM;
1708 }
1709
1710 static int bnx2fc_bind_pcidev(struct bnx2fc_hba *hba)
1711 {
1712         struct cnic_dev *cnic;
1713         struct pci_dev *pdev;
1714
1715         if (!hba->cnic) {
1716                 printk(KERN_ERR PFX "cnic is NULL\n");
1717                 return -ENODEV;
1718         }
1719         cnic = hba->cnic;
1720         pdev = hba->pcidev = cnic->pcidev;
1721         if (!hba->pcidev)
1722                 return -ENODEV;
1723
1724         switch (pdev->device) {
1725         case PCI_DEVICE_ID_NX2_57710:
1726                 strncpy(hba->chip_num, "BCM57710", BCM_CHIP_LEN);
1727                 break;
1728         case PCI_DEVICE_ID_NX2_57711:
1729                 strncpy(hba->chip_num, "BCM57711", BCM_CHIP_LEN);
1730                 break;
1731         case PCI_DEVICE_ID_NX2_57712:
1732         case PCI_DEVICE_ID_NX2_57712_MF:
1733         case PCI_DEVICE_ID_NX2_57712_VF:
1734                 strncpy(hba->chip_num, "BCM57712", BCM_CHIP_LEN);
1735                 break;
1736         case PCI_DEVICE_ID_NX2_57800:
1737         case PCI_DEVICE_ID_NX2_57800_MF:
1738         case PCI_DEVICE_ID_NX2_57800_VF:
1739                 strncpy(hba->chip_num, "BCM57800", BCM_CHIP_LEN);
1740                 break;
1741         case PCI_DEVICE_ID_NX2_57810:
1742         case PCI_DEVICE_ID_NX2_57810_MF:
1743         case PCI_DEVICE_ID_NX2_57810_VF:
1744                 strncpy(hba->chip_num, "BCM57810", BCM_CHIP_LEN);
1745                 break;
1746         case PCI_DEVICE_ID_NX2_57840:
1747         case PCI_DEVICE_ID_NX2_57840_MF:
1748         case PCI_DEVICE_ID_NX2_57840_VF:
1749         case PCI_DEVICE_ID_NX2_57840_2_20:
1750         case PCI_DEVICE_ID_NX2_57840_4_10:
1751                 strncpy(hba->chip_num, "BCM57840", BCM_CHIP_LEN);
1752                 break;
1753         default:
1754                 pr_err(PFX "Unknown device id 0x%x\n", pdev->device);
1755                 break;
1756         }
1757         pci_dev_get(hba->pcidev);
1758         return 0;
1759 }
1760
1761 static void bnx2fc_unbind_pcidev(struct bnx2fc_hba *hba)
1762 {
1763         if (hba->pcidev) {
1764                 hba->chip_num[0] = '\0';
1765                 pci_dev_put(hba->pcidev);
1766         }
1767         hba->pcidev = NULL;
1768 }
1769
1770 /**
1771  * bnx2fc_ulp_get_stats - cnic callback to populate FCoE stats
1772  *
1773  * @handle:    transport handle pointing to adapter struture
1774  */
1775 static int bnx2fc_ulp_get_stats(void *handle)
1776 {
1777         struct bnx2fc_hba *hba = handle;
1778         struct cnic_dev *cnic;
1779         struct fcoe_stats_info *stats_addr;
1780
1781         if (!hba)
1782                 return -EINVAL;
1783
1784         cnic = hba->cnic;
1785         stats_addr = &cnic->stats_addr->fcoe_stat;
1786         if (!stats_addr)
1787                 return -EINVAL;
1788
1789         strncpy(stats_addr->version, BNX2FC_VERSION,
1790                 sizeof(stats_addr->version));
1791         stats_addr->txq_size = BNX2FC_SQ_WQES_MAX;
1792         stats_addr->rxq_size = BNX2FC_CQ_WQES_MAX;
1793
1794         return 0;
1795 }
1796
1797
1798 /**
1799  * bnx2fc_ulp_start - cnic callback to initialize & start adapter instance
1800  *
1801  * @handle:     transport handle pointing to adapter structure
1802  *
1803  * This function maps adapter structure to pcidev structure and initiates
1804  *      firmware handshake to enable/initialize on-chip FCoE components.
1805  *      This bnx2fc - cnic interface api callback is used after following
1806  *      conditions are met -
1807  *      a) underlying network interface is up (marked by event NETDEV_UP
1808  *              from netdev
1809  *      b) bnx2fc adatper structure is registered.
1810  */
1811 static void bnx2fc_ulp_start(void *handle)
1812 {
1813         struct bnx2fc_hba *hba = handle;
1814         struct bnx2fc_interface *interface;
1815         struct fcoe_ctlr *ctlr;
1816         struct fc_lport *lport;
1817
1818         mutex_lock(&bnx2fc_dev_lock);
1819
1820         if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags))
1821                 bnx2fc_fw_init(hba);
1822
1823         BNX2FC_MISC_DBG("bnx2fc started.\n");
1824
1825         list_for_each_entry(interface, &if_list, list) {
1826                 if (interface->hba == hba) {
1827                         ctlr = bnx2fc_to_ctlr(interface);
1828                         lport = ctlr->lp;
1829                         /* Kick off Fabric discovery*/
1830                         printk(KERN_ERR PFX "ulp_init: start discovery\n");
1831                         lport->tt.frame_send = bnx2fc_xmit;
1832                         bnx2fc_start_disc(interface);
1833                 }
1834         }
1835
1836         mutex_unlock(&bnx2fc_dev_lock);
1837 }
1838
1839 static void bnx2fc_port_shutdown(struct fc_lport *lport)
1840 {
1841         BNX2FC_MISC_DBG("Entered %s\n", __func__);
1842         fc_fabric_logoff(lport);
1843         fc_lport_destroy(lport);
1844 }
1845
1846 static void bnx2fc_stop(struct bnx2fc_interface *interface)
1847 {
1848         struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
1849         struct fc_lport *lport;
1850         struct fc_lport *vport;
1851
1852         if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &interface->hba->flags))
1853                 return;
1854
1855         lport = ctlr->lp;
1856         bnx2fc_port_shutdown(lport);
1857
1858         mutex_lock(&lport->lp_mutex);
1859         list_for_each_entry(vport, &lport->vports, list)
1860                 fc_host_port_type(vport->host) =
1861                                         FC_PORTTYPE_UNKNOWN;
1862         mutex_unlock(&lport->lp_mutex);
1863         fc_host_port_type(lport->host) = FC_PORTTYPE_UNKNOWN;
1864         fcoe_ctlr_link_down(ctlr);
1865         fcoe_clean_pending_queue(lport);
1866 }
1867
1868 static int bnx2fc_fw_init(struct bnx2fc_hba *hba)
1869 {
1870 #define BNX2FC_INIT_POLL_TIME           (1000 / HZ)
1871         int rc = -1;
1872         int i = HZ;
1873
1874         rc = bnx2fc_bind_adapter_devices(hba);
1875         if (rc) {
1876                 printk(KERN_ALERT PFX
1877                         "bnx2fc_bind_adapter_devices failed - rc = %d\n", rc);
1878                 goto err_out;
1879         }
1880
1881         rc = bnx2fc_send_fw_fcoe_init_msg(hba);
1882         if (rc) {
1883                 printk(KERN_ALERT PFX
1884                         "bnx2fc_send_fw_fcoe_init_msg failed - rc = %d\n", rc);
1885                 goto err_unbind;
1886         }
1887
1888         /*
1889          * Wait until the adapter init message is complete, and adapter
1890          * state is UP.
1891          */
1892         while (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state) && i--)
1893                 msleep(BNX2FC_INIT_POLL_TIME);
1894
1895         if (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state)) {
1896                 printk(KERN_ERR PFX "bnx2fc_start: %s failed to initialize.  "
1897                                 "Ignoring...\n",
1898                                 hba->cnic->netdev->name);
1899                 rc = -1;
1900                 goto err_unbind;
1901         }
1902
1903
1904         set_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags);
1905         return 0;
1906
1907 err_unbind:
1908         bnx2fc_unbind_adapter_devices(hba);
1909 err_out:
1910         return rc;
1911 }
1912
1913 static void bnx2fc_fw_destroy(struct bnx2fc_hba *hba)
1914 {
1915         if (test_and_clear_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags)) {
1916                 if (bnx2fc_send_fw_fcoe_destroy_msg(hba) == 0) {
1917                         init_timer(&hba->destroy_timer);
1918                         hba->destroy_timer.expires = BNX2FC_FW_TIMEOUT +
1919                                                                 jiffies;
1920                         hba->destroy_timer.function = bnx2fc_destroy_timer;
1921                         hba->destroy_timer.data = (unsigned long)hba;
1922                         add_timer(&hba->destroy_timer);
1923                         wait_event_interruptible(hba->destroy_wait,
1924                                         test_bit(BNX2FC_FLAG_DESTROY_CMPL,
1925                                                  &hba->flags));
1926                         clear_bit(BNX2FC_FLAG_DESTROY_CMPL, &hba->flags);
1927                         /* This should never happen */
1928                         if (signal_pending(current))
1929                                 flush_signals(current);
1930
1931                         del_timer_sync(&hba->destroy_timer);
1932                 }
1933                 bnx2fc_unbind_adapter_devices(hba);
1934         }
1935 }
1936
1937 /**
1938  * bnx2fc_ulp_stop - cnic callback to shutdown adapter instance
1939  *
1940  * @handle:     transport handle pointing to adapter structure
1941  *
1942  * Driver checks if adapter is already in shutdown mode, if not start
1943  *      the shutdown process.
1944  */
1945 static void bnx2fc_ulp_stop(void *handle)
1946 {
1947         struct bnx2fc_hba *hba = handle;
1948         struct bnx2fc_interface *interface;
1949
1950         printk(KERN_ERR "ULP_STOP\n");
1951
1952         mutex_lock(&bnx2fc_dev_lock);
1953         if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags))
1954                 goto exit;
1955         list_for_each_entry(interface, &if_list, list) {
1956                 if (interface->hba == hba)
1957                         bnx2fc_stop(interface);
1958         }
1959         BUG_ON(hba->num_ofld_sess != 0);
1960
1961         mutex_lock(&hba->hba_mutex);
1962         clear_bit(ADAPTER_STATE_UP, &hba->adapter_state);
1963         clear_bit(ADAPTER_STATE_GOING_DOWN,
1964                   &hba->adapter_state);
1965
1966         clear_bit(ADAPTER_STATE_READY, &hba->adapter_state);
1967         mutex_unlock(&hba->hba_mutex);
1968
1969         bnx2fc_fw_destroy(hba);
1970 exit:
1971         mutex_unlock(&bnx2fc_dev_lock);
1972 }
1973
1974 static void bnx2fc_start_disc(struct bnx2fc_interface *interface)
1975 {
1976         struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
1977         struct fc_lport *lport;
1978         int wait_cnt = 0;
1979
1980         BNX2FC_MISC_DBG("Entered %s\n", __func__);
1981         /* Kick off FIP/FLOGI */
1982         if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &interface->hba->flags)) {
1983                 printk(KERN_ERR PFX "Init not done yet\n");
1984                 return;
1985         }
1986
1987         lport = ctlr->lp;
1988         BNX2FC_HBA_DBG(lport, "calling fc_fabric_login\n");
1989
1990         if (!bnx2fc_link_ok(lport) && interface->enabled) {
1991                 BNX2FC_HBA_DBG(lport, "ctlr_link_up\n");
1992                 fcoe_ctlr_link_up(ctlr);
1993                 fc_host_port_type(lport->host) = FC_PORTTYPE_NPORT;
1994                 set_bit(ADAPTER_STATE_READY, &interface->hba->adapter_state);
1995         }
1996
1997         /* wait for the FCF to be selected before issuing FLOGI */
1998         while (!ctlr->sel_fcf) {
1999                 msleep(250);
2000                 /* give up after 3 secs */
2001                 if (++wait_cnt > 12)
2002                         break;
2003         }
2004
2005         /* Reset max receive frame size to default */
2006         if (fc_set_mfs(lport, BNX2FC_MFS))
2007                 return;
2008
2009         fc_lport_init(lport);
2010         fc_fabric_login(lport);
2011 }
2012
2013
2014 /**
2015  * bnx2fc_ulp_init - Initialize an adapter instance
2016  *
2017  * @dev :       cnic device handle
2018  * Called from cnic_register_driver() context to initialize all
2019  *      enumerated cnic devices. This routine allocates adapter structure
2020  *      and other device specific resources.
2021  */
2022 static void bnx2fc_ulp_init(struct cnic_dev *dev)
2023 {
2024         struct bnx2fc_hba *hba;
2025         int rc = 0;
2026
2027         BNX2FC_MISC_DBG("Entered %s\n", __func__);
2028         /* bnx2fc works only when bnx2x is loaded */
2029         if (!test_bit(CNIC_F_BNX2X_CLASS, &dev->flags) ||
2030             (dev->max_fcoe_conn == 0)) {
2031                 printk(KERN_ERR PFX "bnx2fc FCoE not supported on %s,"
2032                                     " flags: %lx fcoe_conn: %d\n",
2033                         dev->netdev->name, dev->flags, dev->max_fcoe_conn);
2034                 return;
2035         }
2036
2037         hba = bnx2fc_hba_create(dev);
2038         if (!hba) {
2039                 printk(KERN_ERR PFX "hba initialization failed\n");
2040                 return;
2041         }
2042
2043         pr_info(PFX "FCoE initialized for %s.\n", dev->netdev->name);
2044
2045         /* Add HBA to the adapter list */
2046         mutex_lock(&bnx2fc_dev_lock);
2047         list_add_tail(&hba->list, &adapter_list);
2048         adapter_count++;
2049         mutex_unlock(&bnx2fc_dev_lock);
2050
2051         dev->fcoe_cap = &hba->fcoe_cap;
2052         clear_bit(BNX2FC_CNIC_REGISTERED, &hba->reg_with_cnic);
2053         rc = dev->register_device(dev, CNIC_ULP_FCOE,
2054                                                 (void *) hba);
2055         if (rc)
2056                 printk(KERN_ERR PFX "register_device failed, rc = %d\n", rc);
2057         else
2058                 set_bit(BNX2FC_CNIC_REGISTERED, &hba->reg_with_cnic);
2059 }
2060
2061 /* Assumes rtnl_lock and the bnx2fc_dev_lock are already taken */
2062 static int __bnx2fc_disable(struct fcoe_ctlr *ctlr)
2063 {
2064         struct bnx2fc_interface *interface = fcoe_ctlr_priv(ctlr);
2065
2066         if (interface->enabled == true) {
2067                 if (!ctlr->lp) {
2068                         pr_err(PFX "__bnx2fc_disable: lport not found\n");
2069                         return -ENODEV;
2070                 } else {
2071                         interface->enabled = false;
2072                         fcoe_ctlr_link_down(ctlr);
2073                         fcoe_clean_pending_queue(ctlr->lp);
2074                 }
2075         }
2076         return 0;
2077 }
2078
2079 /**
2080  * Deperecated: Use bnx2fc_enabled()
2081  */
2082 static int bnx2fc_disable(struct net_device *netdev)
2083 {
2084         struct bnx2fc_interface *interface;
2085         struct fcoe_ctlr *ctlr;
2086         int rc = 0;
2087
2088         rtnl_lock();
2089         mutex_lock(&bnx2fc_dev_lock);
2090
2091         interface = bnx2fc_interface_lookup(netdev);
2092         ctlr = bnx2fc_to_ctlr(interface);
2093
2094         if (!interface) {
2095                 rc = -ENODEV;
2096                 pr_err(PFX "bnx2fc_disable: interface not found\n");
2097         } else {
2098                 rc = __bnx2fc_disable(ctlr);
2099         }
2100         mutex_unlock(&bnx2fc_dev_lock);
2101         rtnl_unlock();
2102         return rc;
2103 }
2104
2105 static uint bnx2fc_npiv_create_vports(struct fc_lport *lport,
2106                                       struct cnic_fc_npiv_tbl *npiv_tbl)
2107 {
2108         struct fc_vport_identifiers vpid;
2109         uint i, created = 0;
2110
2111         if (npiv_tbl->count > MAX_NPIV_ENTRIES) {
2112                 BNX2FC_HBA_DBG(lport, "Exceeded count max of npiv table\n");
2113                 goto done;
2114         }
2115
2116         /* Sanity check the first entry to make sure it's not 0 */
2117         if (wwn_to_u64(npiv_tbl->wwnn[0]) == 0 &&
2118             wwn_to_u64(npiv_tbl->wwpn[0]) == 0) {
2119                 BNX2FC_HBA_DBG(lport, "First NPIV table entries invalid.\n");
2120                 goto done;
2121         }
2122
2123         vpid.roles = FC_PORT_ROLE_FCP_INITIATOR;
2124         vpid.vport_type = FC_PORTTYPE_NPIV;
2125         vpid.disable = false;
2126
2127         for (i = 0; i < npiv_tbl->count; i++) {
2128                 vpid.node_name = wwn_to_u64(npiv_tbl->wwnn[i]);
2129                 vpid.port_name = wwn_to_u64(npiv_tbl->wwpn[i]);
2130                 scnprintf(vpid.symbolic_name, sizeof(vpid.symbolic_name),
2131                     "NPIV[%u]:%016llx-%016llx",
2132                     created, vpid.port_name, vpid.node_name);
2133                 if (fc_vport_create(lport->host, 0, &vpid))
2134                         created++;
2135                 else
2136                         BNX2FC_HBA_DBG(lport, "Failed to create vport\n");
2137         }
2138 done:
2139         return created;
2140 }
2141
2142 static int __bnx2fc_enable(struct fcoe_ctlr *ctlr)
2143 {
2144         struct bnx2fc_interface *interface = fcoe_ctlr_priv(ctlr);
2145         struct bnx2fc_hba *hba;
2146         struct cnic_fc_npiv_tbl *npiv_tbl;
2147         struct fc_lport *lport;
2148
2149         if (interface->enabled == false) {
2150                 if (!ctlr->lp) {
2151                         pr_err(PFX "__bnx2fc_enable: lport not found\n");
2152                         return -ENODEV;
2153                 } else if (!bnx2fc_link_ok(ctlr->lp)) {
2154                         fcoe_ctlr_link_up(ctlr);
2155                         interface->enabled = true;
2156                 }
2157         }
2158
2159         /* Create static NPIV ports if any are contained in NVRAM */
2160         hba = interface->hba;
2161         lport = ctlr->lp;
2162
2163         if (!hba)
2164                 goto done;
2165
2166         if (!hba->cnic)
2167                 goto done;
2168
2169         if (!lport)
2170                 goto done;
2171
2172         if (!lport->host)
2173                 goto done;
2174
2175         if (!hba->cnic->get_fc_npiv_tbl)
2176                 goto done;
2177
2178         npiv_tbl = kzalloc(sizeof(struct cnic_fc_npiv_tbl), GFP_KERNEL);
2179         if (!npiv_tbl)
2180                 goto done;
2181
2182         if (hba->cnic->get_fc_npiv_tbl(hba->cnic, npiv_tbl))
2183                 goto done_free;
2184
2185         bnx2fc_npiv_create_vports(lport, npiv_tbl);
2186 done_free:
2187         kfree(npiv_tbl);
2188 done:
2189         return 0;
2190 }
2191
2192 /**
2193  * Deprecated: Use bnx2fc_enabled()
2194  */
2195 static int bnx2fc_enable(struct net_device *netdev)
2196 {
2197         struct bnx2fc_interface *interface;
2198         struct fcoe_ctlr *ctlr;
2199         int rc = 0;
2200
2201         rtnl_lock();
2202         mutex_lock(&bnx2fc_dev_lock);
2203
2204         interface = bnx2fc_interface_lookup(netdev);
2205         ctlr = bnx2fc_to_ctlr(interface);
2206         if (!interface) {
2207                 rc = -ENODEV;
2208                 pr_err(PFX "bnx2fc_enable: interface not found\n");
2209         } else {
2210                 rc = __bnx2fc_enable(ctlr);
2211         }
2212
2213         mutex_unlock(&bnx2fc_dev_lock);
2214         rtnl_unlock();
2215         return rc;
2216 }
2217
2218 /**
2219  * bnx2fc_ctlr_enabled() - Enable or disable an FCoE Controller
2220  * @cdev: The FCoE Controller that is being enabled or disabled
2221  *
2222  * fcoe_sysfs will ensure that the state of 'enabled' has
2223  * changed, so no checking is necessary here. This routine simply
2224  * calls fcoe_enable or fcoe_disable, both of which are deprecated.
2225  * When those routines are removed the functionality can be merged
2226  * here.
2227  */
2228 static int bnx2fc_ctlr_enabled(struct fcoe_ctlr_device *cdev)
2229 {
2230         struct fcoe_ctlr *ctlr = fcoe_ctlr_device_priv(cdev);
2231
2232         switch (cdev->enabled) {
2233         case FCOE_CTLR_ENABLED:
2234                 return __bnx2fc_enable(ctlr);
2235         case FCOE_CTLR_DISABLED:
2236                 return __bnx2fc_disable(ctlr);
2237         case FCOE_CTLR_UNUSED:
2238         default:
2239                 return -ENOTSUPP;
2240         };
2241 }
2242
2243 enum bnx2fc_create_link_state {
2244         BNX2FC_CREATE_LINK_DOWN,
2245         BNX2FC_CREATE_LINK_UP,
2246 };
2247
2248 /**
2249  * _bnx2fc_create() - Create bnx2fc FCoE interface
2250  * @netdev  :   The net_device object the Ethernet interface to create on
2251  * @fip_mode:   The FIP mode for this creation
2252  * @link_state: The ctlr link state on creation
2253  *
2254  * Called from either the libfcoe 'create' module parameter
2255  * via fcoe_create or from fcoe_syfs's ctlr_create file.
2256  *
2257  * libfcoe's 'create' module parameter is deprecated so some
2258  * consolidation of code can be done when that interface is
2259  * removed.
2260  *
2261  * Returns: 0 for success
2262  */
2263 static int _bnx2fc_create(struct net_device *netdev,
2264                           enum fip_mode fip_mode,
2265                           enum bnx2fc_create_link_state link_state)
2266 {
2267         struct fcoe_ctlr_device *cdev;
2268         struct fcoe_ctlr *ctlr;
2269         struct bnx2fc_interface *interface;
2270         struct bnx2fc_hba *hba;
2271         struct net_device *phys_dev = netdev;
2272         struct fc_lport *lport;
2273         struct ethtool_drvinfo drvinfo;
2274         int rc = 0;
2275         int vlan_id = 0;
2276
2277         BNX2FC_MISC_DBG("Entered bnx2fc_create\n");
2278         if (fip_mode != FIP_MODE_FABRIC) {
2279                 printk(KERN_ERR "fip mode not FABRIC\n");
2280                 return -EIO;
2281         }
2282
2283         rtnl_lock();
2284
2285         mutex_lock(&bnx2fc_dev_lock);
2286
2287         if (!try_module_get(THIS_MODULE)) {
2288                 rc = -EINVAL;
2289                 goto mod_err;
2290         }
2291
2292         /* obtain physical netdev */
2293         if (netdev->priv_flags & IFF_802_1Q_VLAN)
2294                 phys_dev = vlan_dev_real_dev(netdev);
2295
2296         /* verify if the physical device is a netxtreme2 device */
2297         if (phys_dev->ethtool_ops && phys_dev->ethtool_ops->get_drvinfo) {
2298                 memset(&drvinfo, 0, sizeof(drvinfo));
2299                 phys_dev->ethtool_ops->get_drvinfo(phys_dev, &drvinfo);
2300                 if (strncmp(drvinfo.driver, "bnx2x", strlen("bnx2x"))) {
2301                         printk(KERN_ERR PFX "Not a netxtreme2 device\n");
2302                         rc = -EINVAL;
2303                         goto netdev_err;
2304                 }
2305         } else {
2306                 printk(KERN_ERR PFX "unable to obtain drv_info\n");
2307                 rc = -EINVAL;
2308                 goto netdev_err;
2309         }
2310
2311         /* obtain interface and initialize rest of the structure */
2312         hba = bnx2fc_hba_lookup(phys_dev);
2313         if (!hba) {
2314                 rc = -ENODEV;
2315                 printk(KERN_ERR PFX "bnx2fc_create: hba not found\n");
2316                 goto netdev_err;
2317         }
2318
2319         if (bnx2fc_interface_lookup(netdev)) {
2320                 rc = -EEXIST;
2321                 goto netdev_err;
2322         }
2323
2324         interface = bnx2fc_interface_create(hba, netdev, fip_mode);
2325         if (!interface) {
2326                 printk(KERN_ERR PFX "bnx2fc_interface_create failed\n");
2327                 rc = -ENOMEM;
2328                 goto ifput_err;
2329         }
2330
2331         if (netdev->priv_flags & IFF_802_1Q_VLAN) {
2332                 vlan_id = vlan_dev_vlan_id(netdev);
2333                 interface->vlan_enabled = 1;
2334         }
2335
2336         ctlr = bnx2fc_to_ctlr(interface);
2337         cdev = fcoe_ctlr_to_ctlr_dev(ctlr);
2338         interface->vlan_id = vlan_id;
2339         interface->tm_timeout = BNX2FC_TM_TIMEOUT;
2340
2341         interface->timer_work_queue =
2342                         create_singlethread_workqueue("bnx2fc_timer_wq");
2343         if (!interface->timer_work_queue) {
2344                 printk(KERN_ERR PFX "ulp_init could not create timer_wq\n");
2345                 rc = -EINVAL;
2346                 goto ifput_err;
2347         }
2348
2349         lport = bnx2fc_if_create(interface, &cdev->dev, 0);
2350         if (!lport) {
2351                 printk(KERN_ERR PFX "Failed to create interface (%s)\n",
2352                         netdev->name);
2353                 rc = -EINVAL;
2354                 goto if_create_err;
2355         }
2356
2357         /* Add interface to if_list */
2358         list_add_tail(&interface->list, &if_list);
2359
2360         lport->boot_time = jiffies;
2361
2362         /* Make this master N_port */
2363         ctlr->lp = lport;
2364
2365         if (link_state == BNX2FC_CREATE_LINK_UP)
2366                 cdev->enabled = FCOE_CTLR_ENABLED;
2367         else
2368                 cdev->enabled = FCOE_CTLR_DISABLED;
2369
2370         if (link_state == BNX2FC_CREATE_LINK_UP &&
2371             !bnx2fc_link_ok(lport)) {
2372                 fcoe_ctlr_link_up(ctlr);
2373                 fc_host_port_type(lport->host) = FC_PORTTYPE_NPORT;
2374                 set_bit(ADAPTER_STATE_READY, &interface->hba->adapter_state);
2375         }
2376
2377         BNX2FC_HBA_DBG(lport, "create: START DISC\n");
2378         bnx2fc_start_disc(interface);
2379
2380         if (link_state == BNX2FC_CREATE_LINK_UP)
2381                 interface->enabled = true;
2382
2383         /*
2384          * Release from kref_init in bnx2fc_interface_setup, on success
2385          * lport should be holding a reference taken in bnx2fc_if_create
2386          */
2387         bnx2fc_interface_put(interface);
2388         /* put netdev that was held while calling dev_get_by_name */
2389         mutex_unlock(&bnx2fc_dev_lock);
2390         rtnl_unlock();
2391         return 0;
2392
2393 if_create_err:
2394         destroy_workqueue(interface->timer_work_queue);
2395 ifput_err:
2396         bnx2fc_net_cleanup(interface);
2397         bnx2fc_interface_put(interface);
2398         goto mod_err;
2399 netdev_err:
2400         module_put(THIS_MODULE);
2401 mod_err:
2402         mutex_unlock(&bnx2fc_dev_lock);
2403         rtnl_unlock();
2404         return rc;
2405 }
2406
2407 /**
2408  * bnx2fc_create() - Create a bnx2fc interface
2409  * @netdev  : The net_device object the Ethernet interface to create on
2410  * @fip_mode: The FIP mode for this creation
2411  *
2412  * Called from fcoe transport
2413  *
2414  * Returns: 0 for success
2415  */
2416 static int bnx2fc_create(struct net_device *netdev, enum fip_mode fip_mode)
2417 {
2418         return _bnx2fc_create(netdev, fip_mode, BNX2FC_CREATE_LINK_UP);
2419 }
2420
2421 /**
2422  * bnx2fc_ctlr_alloc() - Allocate a bnx2fc interface from fcoe_sysfs
2423  * @netdev: The net_device to be used by the allocated FCoE Controller
2424  *
2425  * This routine is called from fcoe_sysfs. It will start the fcoe_ctlr
2426  * in a link_down state. The allows the user an opportunity to configure
2427  * the FCoE Controller from sysfs before enabling the FCoE Controller.
2428  *
2429  * Creating in with this routine starts the FCoE Controller in Fabric
2430  * mode. The user can change to VN2VN or another mode before enabling.
2431  */
2432 static int bnx2fc_ctlr_alloc(struct net_device *netdev)
2433 {
2434         return _bnx2fc_create(netdev, FIP_MODE_FABRIC,
2435                               BNX2FC_CREATE_LINK_DOWN);
2436 }
2437
2438 /**
2439  * bnx2fc_find_hba_for_cnic - maps cnic instance to bnx2fc hba instance
2440  *
2441  * @cnic:       Pointer to cnic device instance
2442  *
2443  **/
2444 static struct bnx2fc_hba *bnx2fc_find_hba_for_cnic(struct cnic_dev *cnic)
2445 {
2446         struct bnx2fc_hba *hba;
2447
2448         /* Called with bnx2fc_dev_lock held */
2449         list_for_each_entry(hba, &adapter_list, list) {
2450                 if (hba->cnic == cnic)
2451                         return hba;
2452         }
2453         return NULL;
2454 }
2455
2456 static struct bnx2fc_interface *bnx2fc_interface_lookup(struct net_device
2457                                                         *netdev)
2458 {
2459         struct bnx2fc_interface *interface;
2460
2461         /* Called with bnx2fc_dev_lock held */
2462         list_for_each_entry(interface, &if_list, list) {
2463                 if (interface->netdev == netdev)
2464                         return interface;
2465         }
2466         return NULL;
2467 }
2468
2469 static struct bnx2fc_hba *bnx2fc_hba_lookup(struct net_device
2470                                                       *phys_dev)
2471 {
2472         struct bnx2fc_hba *hba;
2473
2474         /* Called with bnx2fc_dev_lock held */
2475         list_for_each_entry(hba, &adapter_list, list) {
2476                 if (hba->phys_dev == phys_dev)
2477                         return hba;
2478         }
2479         printk(KERN_ERR PFX "adapter_lookup: hba NULL\n");
2480         return NULL;
2481 }
2482
2483 /**
2484  * bnx2fc_ulp_exit - shuts down adapter instance and frees all resources
2485  *
2486  * @dev         cnic device handle
2487  */
2488 static void bnx2fc_ulp_exit(struct cnic_dev *dev)
2489 {
2490         struct bnx2fc_hba *hba;
2491         struct bnx2fc_interface *interface, *tmp;
2492
2493         BNX2FC_MISC_DBG("Entered bnx2fc_ulp_exit\n");
2494
2495         if (!test_bit(CNIC_F_BNX2X_CLASS, &dev->flags)) {
2496                 printk(KERN_ERR PFX "bnx2fc port check: %s, flags: %lx\n",
2497                         dev->netdev->name, dev->flags);
2498                 return;
2499         }
2500
2501         mutex_lock(&bnx2fc_dev_lock);
2502         hba = bnx2fc_find_hba_for_cnic(dev);
2503         if (!hba) {
2504                 printk(KERN_ERR PFX "bnx2fc_ulp_exit: hba not found, dev 0%p\n",
2505                        dev);
2506                 mutex_unlock(&bnx2fc_dev_lock);
2507                 return;
2508         }
2509
2510         list_del_init(&hba->list);
2511         adapter_count--;
2512
2513         list_for_each_entry_safe(interface, tmp, &if_list, list)
2514                 /* destroy not called yet, move to quiesced list */
2515                 if (interface->hba == hba)
2516                         __bnx2fc_destroy(interface);
2517         mutex_unlock(&bnx2fc_dev_lock);
2518
2519         /* Ensure ALL destroy work has been completed before return */
2520         flush_workqueue(bnx2fc_wq);
2521
2522         bnx2fc_ulp_stop(hba);
2523         /* unregister cnic device */
2524         if (test_and_clear_bit(BNX2FC_CNIC_REGISTERED, &hba->reg_with_cnic))
2525                 hba->cnic->unregister_device(hba->cnic, CNIC_ULP_FCOE);
2526         bnx2fc_hba_destroy(hba);
2527 }
2528
2529 /**
2530  * bnx2fc_fcoe_reset - Resets the fcoe
2531  *
2532  * @shost: shost the reset is from
2533  *
2534  * Returns: always 0
2535  */
2536 static int bnx2fc_fcoe_reset(struct Scsi_Host *shost)
2537 {
2538         struct fc_lport *lport = shost_priv(shost);
2539         fc_lport_reset(lport);
2540         return 0;
2541 }
2542
2543
2544 static bool bnx2fc_match(struct net_device *netdev)
2545 {
2546         struct net_device *phys_dev = netdev;
2547
2548         mutex_lock(&bnx2fc_dev_lock);
2549         if (netdev->priv_flags & IFF_802_1Q_VLAN)
2550                 phys_dev = vlan_dev_real_dev(netdev);
2551
2552         if (bnx2fc_hba_lookup(phys_dev)) {
2553                 mutex_unlock(&bnx2fc_dev_lock);
2554                 return true;
2555         }
2556
2557         mutex_unlock(&bnx2fc_dev_lock);
2558         return false;
2559 }
2560
2561
2562 static struct fcoe_transport bnx2fc_transport = {
2563         .name = {"bnx2fc"},
2564         .attached = false,
2565         .list = LIST_HEAD_INIT(bnx2fc_transport.list),
2566         .alloc = bnx2fc_ctlr_alloc,
2567         .match = bnx2fc_match,
2568         .create = bnx2fc_create,
2569         .destroy = bnx2fc_destroy,
2570         .enable = bnx2fc_enable,
2571         .disable = bnx2fc_disable,
2572 };
2573
2574 /**
2575  * bnx2fc_percpu_thread_create - Create a receive thread for an
2576  *                               online CPU
2577  *
2578  * @cpu: cpu index for the online cpu
2579  */
2580 static void bnx2fc_percpu_thread_create(unsigned int cpu)
2581 {
2582         struct bnx2fc_percpu_s *p;
2583         struct task_struct *thread;
2584
2585         p = &per_cpu(bnx2fc_percpu, cpu);
2586
2587         thread = kthread_create_on_node(bnx2fc_percpu_io_thread,
2588                                         (void *)p, cpu_to_node(cpu),
2589                                         "bnx2fc_thread/%d", cpu);
2590         /* bind thread to the cpu */
2591         if (likely(!IS_ERR(thread))) {
2592                 kthread_bind(thread, cpu);
2593                 p->iothread = thread;
2594                 wake_up_process(thread);
2595         }
2596 }
2597
2598 static void bnx2fc_percpu_thread_destroy(unsigned int cpu)
2599 {
2600         struct bnx2fc_percpu_s *p;
2601         struct task_struct *thread;
2602         struct bnx2fc_work *work, *tmp;
2603
2604         BNX2FC_MISC_DBG("destroying io thread for CPU %d\n", cpu);
2605
2606         /* Prevent any new work from being queued for this CPU */
2607         p = &per_cpu(bnx2fc_percpu, cpu);
2608         spin_lock_bh(&p->fp_work_lock);
2609         thread = p->iothread;
2610         p->iothread = NULL;
2611
2612
2613         /* Free all work in the list */
2614         list_for_each_entry_safe(work, tmp, &p->work_list, list) {
2615                 list_del_init(&work->list);
2616                 bnx2fc_process_cq_compl(work->tgt, work->wqe);
2617                 kfree(work);
2618         }
2619
2620         spin_unlock_bh(&p->fp_work_lock);
2621
2622         if (thread)
2623                 kthread_stop(thread);
2624 }
2625
2626 /**
2627  * bnx2fc_cpu_callback - Handler for CPU hotplug events
2628  *
2629  * @nfb:    The callback data block
2630  * @action: The event triggering the callback
2631  * @hcpu:   The index of the CPU that the event is for
2632  *
2633  * This creates or destroys per-CPU data for fcoe
2634  *
2635  * Returns NOTIFY_OK always.
2636  */
2637 static int bnx2fc_cpu_callback(struct notifier_block *nfb,
2638                              unsigned long action, void *hcpu)
2639 {
2640         unsigned cpu = (unsigned long)hcpu;
2641
2642         switch (action) {
2643         case CPU_ONLINE:
2644         case CPU_ONLINE_FROZEN:
2645                 printk(PFX "CPU %x online: Create Rx thread\n", cpu);
2646                 bnx2fc_percpu_thread_create(cpu);
2647                 break;
2648         case CPU_DEAD:
2649         case CPU_DEAD_FROZEN:
2650                 printk(PFX "CPU %x offline: Remove Rx thread\n", cpu);
2651                 bnx2fc_percpu_thread_destroy(cpu);
2652                 break;
2653         default:
2654                 break;
2655         }
2656         return NOTIFY_OK;
2657 }
2658
2659 static int bnx2fc_slave_configure(struct scsi_device *sdev)
2660 {
2661         if (!bnx2fc_queue_depth)
2662                 return 0;
2663
2664         scsi_change_queue_depth(sdev, bnx2fc_queue_depth);
2665         return 0;
2666 }
2667
2668 /**
2669  * bnx2fc_mod_init - module init entry point
2670  *
2671  * Initialize driver wide global data structures, and register
2672  * with cnic module
2673  **/
2674 static int __init bnx2fc_mod_init(void)
2675 {
2676         struct fcoe_percpu_s *bg;
2677         struct task_struct *l2_thread;
2678         int rc = 0;
2679         unsigned int cpu = 0;
2680         struct bnx2fc_percpu_s *p;
2681
2682         printk(KERN_INFO PFX "%s", version);
2683
2684         /* register as a fcoe transport */
2685         rc = fcoe_transport_attach(&bnx2fc_transport);
2686         if (rc) {
2687                 printk(KERN_ERR "failed to register an fcoe transport, check "
2688                         "if libfcoe is loaded\n");
2689                 goto out;
2690         }
2691
2692         INIT_LIST_HEAD(&adapter_list);
2693         INIT_LIST_HEAD(&if_list);
2694         mutex_init(&bnx2fc_dev_lock);
2695         adapter_count = 0;
2696
2697         /* Attach FC transport template */
2698         rc = bnx2fc_attach_transport();
2699         if (rc)
2700                 goto detach_ft;
2701
2702         bnx2fc_wq = alloc_workqueue("bnx2fc", 0, 0);
2703         if (!bnx2fc_wq) {
2704                 rc = -ENOMEM;
2705                 goto release_bt;
2706         }
2707
2708         bg = &bnx2fc_global;
2709         skb_queue_head_init(&bg->fcoe_rx_list);
2710         l2_thread = kthread_create(bnx2fc_l2_rcv_thread,
2711                                    (void *)bg,
2712                                    "bnx2fc_l2_thread");
2713         if (IS_ERR(l2_thread)) {
2714                 rc = PTR_ERR(l2_thread);
2715                 goto free_wq;
2716         }
2717         wake_up_process(l2_thread);
2718         spin_lock_bh(&bg->fcoe_rx_list.lock);
2719         bg->kthread = l2_thread;
2720         spin_unlock_bh(&bg->fcoe_rx_list.lock);
2721
2722         for_each_possible_cpu(cpu) {
2723                 p = &per_cpu(bnx2fc_percpu, cpu);
2724                 INIT_LIST_HEAD(&p->work_list);
2725                 spin_lock_init(&p->fp_work_lock);
2726         }
2727
2728         cpu_notifier_register_begin();
2729
2730         for_each_online_cpu(cpu) {
2731                 bnx2fc_percpu_thread_create(cpu);
2732         }
2733
2734         /* Initialize per CPU interrupt thread */
2735         __register_hotcpu_notifier(&bnx2fc_cpu_notifier);
2736
2737         cpu_notifier_register_done();
2738
2739         cnic_register_driver(CNIC_ULP_FCOE, &bnx2fc_cnic_cb);
2740
2741         return 0;
2742
2743 free_wq:
2744         destroy_workqueue(bnx2fc_wq);
2745 release_bt:
2746         bnx2fc_release_transport();
2747 detach_ft:
2748         fcoe_transport_detach(&bnx2fc_transport);
2749 out:
2750         return rc;
2751 }
2752
2753 static void __exit bnx2fc_mod_exit(void)
2754 {
2755         LIST_HEAD(to_be_deleted);
2756         struct bnx2fc_hba *hba, *next;
2757         struct fcoe_percpu_s *bg;
2758         struct task_struct *l2_thread;
2759         struct sk_buff *skb;
2760         unsigned int cpu = 0;
2761
2762         /*
2763          * NOTE: Since cnic calls register_driver routine rtnl_lock,
2764          * it will have higher precedence than bnx2fc_dev_lock.
2765          * unregister_device() cannot be called with bnx2fc_dev_lock
2766          * held.
2767          */
2768         mutex_lock(&bnx2fc_dev_lock);
2769         list_splice_init(&adapter_list, &to_be_deleted);
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 };