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