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