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