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