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