[SCSI] fcoe: Use percpu kernel funcs for struct fcoe_percpu_s
[safe/jmp/linux-2.6] / drivers / scsi / fcoe / libfcoe.c
1 /*
2  * Copyright(c) 2007 - 2008 Intel Corporation. All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms and conditions of the GNU General Public License,
6  * version 2, as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
11  * more details.
12  *
13  * You should have received a copy of the GNU General Public License along with
14  * this program; if not, write to the Free Software Foundation, Inc.,
15  * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
16  *
17  * Maintained at www.Open-FCoE.org
18  */
19
20 #include <linux/module.h>
21 #include <linux/version.h>
22 #include <linux/kernel.h>
23 #include <linux/spinlock.h>
24 #include <linux/skbuff.h>
25 #include <linux/netdevice.h>
26 #include <linux/etherdevice.h>
27 #include <linux/ethtool.h>
28 #include <linux/if_ether.h>
29 #include <linux/if_vlan.h>
30 #include <linux/kthread.h>
31 #include <linux/crc32.h>
32 #include <linux/cpu.h>
33 #include <linux/fs.h>
34 #include <linux/sysfs.h>
35 #include <linux/ctype.h>
36 #include <scsi/scsi_tcq.h>
37 #include <scsi/scsicam.h>
38 #include <scsi/scsi_transport.h>
39 #include <scsi/scsi_transport_fc.h>
40 #include <net/rtnetlink.h>
41
42 #include <scsi/fc/fc_encaps.h>
43
44 #include <scsi/libfc.h>
45 #include <scsi/fc_frame.h>
46 #include <scsi/libfcoe.h>
47 #include <scsi/fc_transport_fcoe.h>
48
49 static int debug_fcoe;
50
51 #define FCOE_MAX_QUEUE_DEPTH  256
52 #define FCOE_LOW_QUEUE_DEPTH  32
53
54 /* destination address mode */
55 #define FCOE_GW_ADDR_MODE           0x00
56 #define FCOE_FCOUI_ADDR_MODE        0x01
57
58 #define FCOE_WORD_TO_BYTE  4
59
60 MODULE_AUTHOR("Open-FCoE.org");
61 MODULE_DESCRIPTION("FCoE");
62 MODULE_LICENSE("GPL");
63
64 /* fcoe host list */
65 LIST_HEAD(fcoe_hostlist);
66 DEFINE_RWLOCK(fcoe_hostlist_lock);
67 DEFINE_TIMER(fcoe_timer, NULL, 0, 0);
68 DEFINE_PER_CPU(struct fcoe_percpu_s, fcoe_percpu);
69
70
71 /* Function Prototyes */
72 static int fcoe_check_wait_queue(struct fc_lport *);
73 static void fcoe_recv_flogi(struct fcoe_softc *, struct fc_frame *, u8 *);
74 #ifdef CONFIG_HOTPLUG_CPU
75 static int fcoe_cpu_callback(struct notifier_block *, ulong, void *);
76 #endif /* CONFIG_HOTPLUG_CPU */
77 static int fcoe_device_notification(struct notifier_block *, ulong, void *);
78 static void fcoe_dev_setup(void);
79 static void fcoe_dev_cleanup(void);
80
81 /* notification function from net device */
82 static struct notifier_block fcoe_notifier = {
83         .notifier_call = fcoe_device_notification,
84 };
85
86
87 #ifdef CONFIG_HOTPLUG_CPU
88 static struct notifier_block fcoe_cpu_notifier = {
89         .notifier_call = fcoe_cpu_callback,
90 };
91
92 /**
93  * fcoe_create_percpu_data() - creates the associated cpu data
94  * @cpu: index for the cpu where fcoe cpu data will be created
95  *
96  * create percpu stats block, from cpu add notifier
97  *
98  * Returns: none
99  */
100 static void fcoe_create_percpu_data(unsigned int cpu)
101 {
102         struct fc_lport *lp;
103         struct fcoe_softc *fc;
104
105         write_lock_bh(&fcoe_hostlist_lock);
106         list_for_each_entry(fc, &fcoe_hostlist, list) {
107                 lp = fc->lp;
108                 if (lp->dev_stats[cpu] == NULL)
109                         lp->dev_stats[cpu] =
110                                 kzalloc(sizeof(struct fcoe_dev_stats),
111                                         GFP_KERNEL);
112         }
113         write_unlock_bh(&fcoe_hostlist_lock);
114 }
115
116 /**
117  * fcoe_destroy_percpu_data() - destroys the associated cpu data
118  * @cpu: index for the cpu where fcoe cpu data will destroyed
119  *
120  * destroy percpu stats block called by cpu add/remove notifier
121  *
122  * Retuns: none
123  */
124 static void fcoe_destroy_percpu_data(unsigned int cpu)
125 {
126         struct fc_lport *lp;
127         struct fcoe_softc *fc;
128
129         write_lock_bh(&fcoe_hostlist_lock);
130         list_for_each_entry(fc, &fcoe_hostlist, list) {
131                 lp = fc->lp;
132                 kfree(lp->dev_stats[cpu]);
133                 lp->dev_stats[cpu] = NULL;
134         }
135         write_unlock_bh(&fcoe_hostlist_lock);
136 }
137
138 /**
139  * fcoe_cpu_callback() - fcoe cpu hotplug event callback
140  * @nfb: callback data block
141  * @action: event triggering the callback
142  * @hcpu: index for the cpu of this event
143  *
144  * this creates or destroys per cpu data for fcoe
145  *
146  * Returns NOTIFY_OK always.
147  */
148 static int fcoe_cpu_callback(struct notifier_block *nfb, unsigned long action,
149                              void *hcpu)
150 {
151         unsigned int cpu = (unsigned long)hcpu;
152
153         switch (action) {
154         case CPU_ONLINE:
155                 fcoe_create_percpu_data(cpu);
156                 break;
157         case CPU_DEAD:
158                 fcoe_destroy_percpu_data(cpu);
159                 break;
160         default:
161                 break;
162         }
163         return NOTIFY_OK;
164 }
165 #endif /* CONFIG_HOTPLUG_CPU */
166
167 /**
168  * fcoe_rcv() - this is the fcoe receive function called by NET_RX_SOFTIRQ
169  * @skb: the receive skb
170  * @dev: associated net device
171  * @ptype: context
172  * @odldev: last device
173  *
174  * this function will receive the packet and build fc frame and pass it up
175  *
176  * Returns: 0 for success
177  */
178 int fcoe_rcv(struct sk_buff *skb, struct net_device *dev,
179              struct packet_type *ptype, struct net_device *olddev)
180 {
181         struct fc_lport *lp;
182         struct fcoe_rcv_info *fr;
183         struct fcoe_softc *fc;
184         struct fcoe_dev_stats *stats;
185         struct fc_frame_header *fh;
186         struct fcoe_percpu_s *fps;
187         unsigned short oxid;
188         unsigned int cpu_idx;
189
190         fc = container_of(ptype, struct fcoe_softc, fcoe_packet_type);
191         lp = fc->lp;
192         if (unlikely(lp == NULL)) {
193                 FC_DBG("cannot find hba structure");
194                 goto err2;
195         }
196
197         if (unlikely(debug_fcoe)) {
198                 FC_DBG("skb_info: len:%d data_len:%d head:%p data:%p tail:%p "
199                        "end:%p sum:%d dev:%s", skb->len, skb->data_len,
200                        skb->head, skb->data, skb_tail_pointer(skb),
201                        skb_end_pointer(skb), skb->csum,
202                        skb->dev ? skb->dev->name : "<NULL>");
203
204         }
205
206         /* check for FCOE packet type */
207         if (unlikely(eth_hdr(skb)->h_proto != htons(ETH_P_FCOE))) {
208                 FC_DBG("wrong FC type frame");
209                 goto err;
210         }
211
212         /*
213          * Check for minimum frame length, and make sure required FCoE
214          * and FC headers are pulled into the linear data area.
215          */
216         if (unlikely((skb->len < FCOE_MIN_FRAME) ||
217             !pskb_may_pull(skb, FCOE_HEADER_LEN)))
218                 goto err;
219
220         skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
221         fh = (struct fc_frame_header *) skb_transport_header(skb);
222
223         oxid = ntohs(fh->fh_ox_id);
224
225         fr = fcoe_dev_from_skb(skb);
226         fr->fr_dev = lp;
227         fr->ptype = ptype;
228         cpu_idx = 0;
229
230 #ifdef CONFIG_SMP
231         /*
232          * The incoming frame exchange id(oxid) is ANDed with num of online
233          * cpu bits to get cpu_idx and then this cpu_idx is used for selecting
234          * a per cpu kernel thread from fcoe_percpu. In case the cpu is
235          * offline or no kernel thread for derived cpu_idx then cpu_idx is
236          * initialize to first online cpu index.
237          */
238         cpu_idx = oxid & (num_online_cpus() - 1);
239         if (!cpu_online(cpu_idx))
240                 cpu_idx = first_cpu(cpu_online_map);
241
242 #endif
243
244         fps = &per_cpu(fcoe_percpu, cpu_idx);
245
246         spin_lock_bh(&fps->fcoe_rx_list.lock);
247         __skb_queue_tail(&fps->fcoe_rx_list, skb);
248         if (fps->fcoe_rx_list.qlen == 1)
249                 wake_up_process(fps->thread);
250
251         spin_unlock_bh(&fps->fcoe_rx_list.lock);
252
253         return 0;
254 err:
255 #ifdef CONFIG_SMP
256         stats = lp->dev_stats[smp_processor_id()];
257 #else
258         stats = lp->dev_stats[0];
259 #endif
260         if (stats)
261                 stats->ErrorFrames++;
262
263 err2:
264         kfree_skb(skb);
265         return -1;
266 }
267 EXPORT_SYMBOL_GPL(fcoe_rcv);
268
269 /**
270  * fcoe_start_io() - pass to netdev to start xmit for fcoe
271  * @skb: the skb to be xmitted
272  *
273  * Returns: 0 for success
274  */
275 static inline int fcoe_start_io(struct sk_buff *skb)
276 {
277         int rc;
278
279         skb_get(skb);
280         rc = dev_queue_xmit(skb);
281         if (rc != 0)
282                 return rc;
283         kfree_skb(skb);
284         return 0;
285 }
286
287 /**
288  * fcoe_get_paged_crc_eof() - in case we need alloc a page for crc_eof
289  * @skb: the skb to be xmitted
290  * @tlen: total len
291  *
292  * Returns: 0 for success
293  */
294 static int fcoe_get_paged_crc_eof(struct sk_buff *skb, int tlen)
295 {
296         struct fcoe_percpu_s *fps;
297         struct page *page;
298
299         fps = &get_cpu_var(fcoe_percpu);
300         page = fps->crc_eof_page;
301         if (!page) {
302                 page = alloc_page(GFP_ATOMIC);
303                 if (!page) {
304                         put_cpu_var(fcoe_percpu);
305                         return -ENOMEM;
306                 }
307                 fps->crc_eof_page = page;
308                 WARN_ON(fps->crc_eof_offset != 0);
309         }
310
311         get_page(page);
312         skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags, page,
313                            fps->crc_eof_offset, tlen);
314         skb->len += tlen;
315         skb->data_len += tlen;
316         skb->truesize += tlen;
317         fps->crc_eof_offset += sizeof(struct fcoe_crc_eof);
318
319         if (fps->crc_eof_offset >= PAGE_SIZE) {
320                 fps->crc_eof_page = NULL;
321                 fps->crc_eof_offset = 0;
322                 put_page(page);
323         }
324         put_cpu_var(fcoe_percpu);
325         return 0;
326 }
327
328 /**
329  * fcoe_fc_crc() - calculates FC CRC in this fcoe skb
330  * @fp: the fc_frame containg data to be checksummed
331  *
332  * This uses crc32() to calculate the crc for fc frame
333  * Return   : 32 bit crc
334  */
335 u32 fcoe_fc_crc(struct fc_frame *fp)
336 {
337         struct sk_buff *skb = fp_skb(fp);
338         struct skb_frag_struct *frag;
339         unsigned char *data;
340         unsigned long off, len, clen;
341         u32 crc;
342         unsigned i;
343
344         crc = crc32(~0, skb->data, skb_headlen(skb));
345
346         for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
347                 frag = &skb_shinfo(skb)->frags[i];
348                 off = frag->page_offset;
349                 len = frag->size;
350                 while (len > 0) {
351                         clen = min(len, PAGE_SIZE - (off & ~PAGE_MASK));
352                         data = kmap_atomic(frag->page + (off >> PAGE_SHIFT),
353                                            KM_SKB_DATA_SOFTIRQ);
354                         crc = crc32(crc, data + (off & ~PAGE_MASK), clen);
355                         kunmap_atomic(data, KM_SKB_DATA_SOFTIRQ);
356                         off += clen;
357                         len -= clen;
358                 }
359         }
360         return crc;
361 }
362 EXPORT_SYMBOL_GPL(fcoe_fc_crc);
363
364 /**
365  * fcoe_xmit() - FCoE frame transmit function
366  * @lp: the associated local port
367  * @fp: the fc_frame to be transmitted
368  *
369  * Return   : 0 for success
370  */
371 int fcoe_xmit(struct fc_lport *lp, struct fc_frame *fp)
372 {
373         int wlen, rc = 0;
374         u32 crc;
375         struct ethhdr *eh;
376         struct fcoe_crc_eof *cp;
377         struct sk_buff *skb;
378         struct fcoe_dev_stats *stats;
379         struct fc_frame_header *fh;
380         unsigned int hlen;              /* header length implies the version */
381         unsigned int tlen;              /* trailer length */
382         unsigned int elen;              /* eth header, may include vlan */
383         int flogi_in_progress = 0;
384         struct fcoe_softc *fc;
385         u8 sof, eof;
386         struct fcoe_hdr *hp;
387
388         WARN_ON((fr_len(fp) % sizeof(u32)) != 0);
389
390         fc = lport_priv(lp);
391         /*
392          * if it is a flogi then we need to learn gw-addr
393          * and my own fcid
394          */
395         fh = fc_frame_header_get(fp);
396         if (unlikely(fh->fh_r_ctl == FC_RCTL_ELS_REQ)) {
397                 if (fc_frame_payload_op(fp) == ELS_FLOGI) {
398                         fc->flogi_oxid = ntohs(fh->fh_ox_id);
399                         fc->address_mode = FCOE_FCOUI_ADDR_MODE;
400                         fc->flogi_progress = 1;
401                         flogi_in_progress = 1;
402                 } else if (fc->flogi_progress && ntoh24(fh->fh_s_id) != 0) {
403                         /*
404                          * Here we must've gotten an SID by accepting an FLOGI
405                          * from a point-to-point connection.  Switch to using
406                          * the source mac based on the SID.  The destination
407                          * MAC in this case would have been set by receving the
408                          * FLOGI.
409                          */
410                         fc_fcoe_set_mac(fc->data_src_addr, fh->fh_s_id);
411                         fc->flogi_progress = 0;
412                 }
413         }
414
415         skb = fp_skb(fp);
416         sof = fr_sof(fp);
417         eof = fr_eof(fp);
418
419         elen = (fc->real_dev->priv_flags & IFF_802_1Q_VLAN) ?
420                 sizeof(struct vlan_ethhdr) : sizeof(struct ethhdr);
421         hlen = sizeof(struct fcoe_hdr);
422         tlen = sizeof(struct fcoe_crc_eof);
423         wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
424
425         /* crc offload */
426         if (likely(lp->crc_offload)) {
427                 skb->ip_summed = CHECKSUM_PARTIAL;
428                 skb->csum_start = skb_headroom(skb);
429                 skb->csum_offset = skb->len;
430                 crc = 0;
431         } else {
432                 skb->ip_summed = CHECKSUM_NONE;
433                 crc = fcoe_fc_crc(fp);
434         }
435
436         /* copy fc crc and eof to the skb buff */
437         if (skb_is_nonlinear(skb)) {
438                 skb_frag_t *frag;
439                 if (fcoe_get_paged_crc_eof(skb, tlen)) {
440                         kfree_skb(skb);
441                         return -ENOMEM;
442                 }
443                 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
444                 cp = kmap_atomic(frag->page, KM_SKB_DATA_SOFTIRQ)
445                         + frag->page_offset;
446         } else {
447                 cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
448         }
449
450         memset(cp, 0, sizeof(*cp));
451         cp->fcoe_eof = eof;
452         cp->fcoe_crc32 = cpu_to_le32(~crc);
453
454         if (skb_is_nonlinear(skb)) {
455                 kunmap_atomic(cp, KM_SKB_DATA_SOFTIRQ);
456                 cp = NULL;
457         }
458
459         /* adjust skb netowrk/transport offsets to match mac/fcoe/fc */
460         skb_push(skb, elen + hlen);
461         skb_reset_mac_header(skb);
462         skb_reset_network_header(skb);
463         skb->mac_len = elen;
464         skb->protocol = htons(ETH_P_FCOE);
465         skb->dev = fc->real_dev;
466
467         /* fill up mac and fcoe headers */
468         eh = eth_hdr(skb);
469         eh->h_proto = htons(ETH_P_FCOE);
470         if (fc->address_mode == FCOE_FCOUI_ADDR_MODE)
471                 fc_fcoe_set_mac(eh->h_dest, fh->fh_d_id);
472         else
473                 /* insert GW address */
474                 memcpy(eh->h_dest, fc->dest_addr, ETH_ALEN);
475
476         if (unlikely(flogi_in_progress))
477                 memcpy(eh->h_source, fc->ctl_src_addr, ETH_ALEN);
478         else
479                 memcpy(eh->h_source, fc->data_src_addr, ETH_ALEN);
480
481         hp = (struct fcoe_hdr *)(eh + 1);
482         memset(hp, 0, sizeof(*hp));
483         if (FC_FCOE_VER)
484                 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
485         hp->fcoe_sof = sof;
486
487 #ifdef NETIF_F_FSO
488         /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
489         if (lp->seq_offload && fr_max_payload(fp)) {
490                 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
491                 skb_shinfo(skb)->gso_size = fr_max_payload(fp);
492         } else {
493                 skb_shinfo(skb)->gso_type = 0;
494                 skb_shinfo(skb)->gso_size = 0;
495         }
496 #endif
497         /* update tx stats: regardless if LLD fails */
498         stats = lp->dev_stats[smp_processor_id()];
499         if (stats) {
500                 stats->TxFrames++;
501                 stats->TxWords += wlen;
502         }
503
504         /* send down to lld */
505         fr_dev(fp) = lp;
506         if (fc->fcoe_pending_queue.qlen)
507                 rc = fcoe_check_wait_queue(lp);
508
509         if (rc == 0)
510                 rc = fcoe_start_io(skb);
511
512         if (rc) {
513                 spin_lock_bh(&fc->fcoe_pending_queue.lock);
514                 __skb_queue_tail(&fc->fcoe_pending_queue, skb);
515                 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
516                 if (fc->fcoe_pending_queue.qlen > FCOE_MAX_QUEUE_DEPTH)
517                         lp->qfull = 1;
518         }
519
520         return 0;
521 }
522 EXPORT_SYMBOL_GPL(fcoe_xmit);
523
524 /**
525  * fcoe_percpu_receive_thread() - recv thread per cpu
526  * @arg: ptr to the fcoe per cpu struct
527  *
528  * Return: 0 for success
529  */
530 int fcoe_percpu_receive_thread(void *arg)
531 {
532         struct fcoe_percpu_s *p = arg;
533         u32 fr_len;
534         struct fc_lport *lp;
535         struct fcoe_rcv_info *fr;
536         struct fcoe_dev_stats *stats;
537         struct fc_frame_header *fh;
538         struct sk_buff *skb;
539         struct fcoe_crc_eof crc_eof;
540         struct fc_frame *fp;
541         u8 *mac = NULL;
542         struct fcoe_softc *fc;
543         struct fcoe_hdr *hp;
544
545         set_user_nice(current, -20);
546
547         while (!kthread_should_stop()) {
548
549                 spin_lock_bh(&p->fcoe_rx_list.lock);
550                 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) == NULL) {
551                         set_current_state(TASK_INTERRUPTIBLE);
552                         spin_unlock_bh(&p->fcoe_rx_list.lock);
553                         schedule();
554                         set_current_state(TASK_RUNNING);
555                         if (kthread_should_stop())
556                                 return 0;
557                         spin_lock_bh(&p->fcoe_rx_list.lock);
558                 }
559                 spin_unlock_bh(&p->fcoe_rx_list.lock);
560                 fr = fcoe_dev_from_skb(skb);
561                 lp = fr->fr_dev;
562                 if (unlikely(lp == NULL)) {
563                         FC_DBG("invalid HBA Structure");
564                         kfree_skb(skb);
565                         continue;
566                 }
567
568                 stats = lp->dev_stats[smp_processor_id()];
569
570                 if (unlikely(debug_fcoe)) {
571                         FC_DBG("skb_info: len:%d data_len:%d head:%p data:%p "
572                                "tail:%p end:%p sum:%d dev:%s",
573                                skb->len, skb->data_len,
574                                skb->head, skb->data, skb_tail_pointer(skb),
575                                skb_end_pointer(skb), skb->csum,
576                                skb->dev ? skb->dev->name : "<NULL>");
577                 }
578
579                 /*
580                  * Save source MAC address before discarding header.
581                  */
582                 fc = lport_priv(lp);
583                 if (unlikely(fc->flogi_progress))
584                         mac = eth_hdr(skb)->h_source;
585
586                 if (skb_is_nonlinear(skb))
587                         skb_linearize(skb);     /* not ideal */
588
589                 /*
590                  * Frame length checks and setting up the header pointers
591                  * was done in fcoe_rcv already.
592                  */
593                 hp = (struct fcoe_hdr *) skb_network_header(skb);
594                 fh = (struct fc_frame_header *) skb_transport_header(skb);
595
596                 if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) {
597                         if (stats) {
598                                 if (stats->ErrorFrames < 5)
599                                         FC_DBG("unknown FCoE version %x",
600                                                FC_FCOE_DECAPS_VER(hp));
601                                 stats->ErrorFrames++;
602                         }
603                         kfree_skb(skb);
604                         continue;
605                 }
606
607                 skb_pull(skb, sizeof(struct fcoe_hdr));
608                 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
609
610                 if (stats) {
611                         stats->RxFrames++;
612                         stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
613                 }
614
615                 fp = (struct fc_frame *)skb;
616                 fc_frame_init(fp);
617                 fr_dev(fp) = lp;
618                 fr_sof(fp) = hp->fcoe_sof;
619
620                 /* Copy out the CRC and EOF trailer for access */
621                 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof))) {
622                         kfree_skb(skb);
623                         continue;
624                 }
625                 fr_eof(fp) = crc_eof.fcoe_eof;
626                 fr_crc(fp) = crc_eof.fcoe_crc32;
627                 if (pskb_trim(skb, fr_len)) {
628                         kfree_skb(skb);
629                         continue;
630                 }
631
632                 /*
633                  * We only check CRC if no offload is available and if it is
634                  * it's solicited data, in which case, the FCP layer would
635                  * check it during the copy.
636                  */
637                 if (lp->crc_offload && skb->ip_summed == CHECKSUM_UNNECESSARY)
638                         fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
639                 else
640                         fr_flags(fp) |= FCPHF_CRC_UNCHECKED;
641
642                 fh = fc_frame_header_get(fp);
643                 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA &&
644                     fh->fh_type == FC_TYPE_FCP) {
645                         fc_exch_recv(lp, lp->emp, fp);
646                         continue;
647                 }
648                 if (fr_flags(fp) & FCPHF_CRC_UNCHECKED) {
649                         if (le32_to_cpu(fr_crc(fp)) !=
650                             ~crc32(~0, skb->data, fr_len)) {
651                                 if (debug_fcoe || stats->InvalidCRCCount < 5)
652                                         printk(KERN_WARNING "fcoe: dropping "
653                                                "frame with CRC error\n");
654                                 stats->InvalidCRCCount++;
655                                 stats->ErrorFrames++;
656                                 fc_frame_free(fp);
657                                 continue;
658                         }
659                         fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
660                 }
661                 /* non flogi and non data exchanges are handled here */
662                 if (unlikely(fc->flogi_progress))
663                         fcoe_recv_flogi(fc, fp, mac);
664                 fc_exch_recv(lp, lp->emp, fp);
665         }
666         return 0;
667 }
668
669 /**
670  * fcoe_recv_flogi() - flogi receive function
671  * @fc: associated fcoe_softc
672  * @fp: the recieved frame
673  * @sa: the source address of this flogi
674  *
675  * This is responsible to parse the flogi response and sets the corresponding
676  * mac address for the initiator, eitehr OUI based or GW based.
677  *
678  * Returns: none
679  */
680 static void fcoe_recv_flogi(struct fcoe_softc *fc, struct fc_frame *fp, u8 *sa)
681 {
682         struct fc_frame_header *fh;
683         u8 op;
684
685         fh = fc_frame_header_get(fp);
686         if (fh->fh_type != FC_TYPE_ELS)
687                 return;
688         op = fc_frame_payload_op(fp);
689         if (op == ELS_LS_ACC && fh->fh_r_ctl == FC_RCTL_ELS_REP &&
690             fc->flogi_oxid == ntohs(fh->fh_ox_id)) {
691                 /*
692                  * FLOGI accepted.
693                  * If the src mac addr is FC_OUI-based, then we mark the
694                  * address_mode flag to use FC_OUI-based Ethernet DA.
695                  * Otherwise we use the FCoE gateway addr
696                  */
697                 if (!compare_ether_addr(sa, (u8[6]) FC_FCOE_FLOGI_MAC)) {
698                         fc->address_mode = FCOE_FCOUI_ADDR_MODE;
699                 } else {
700                         memcpy(fc->dest_addr, sa, ETH_ALEN);
701                         fc->address_mode = FCOE_GW_ADDR_MODE;
702                 }
703
704                 /*
705                  * Remove any previously-set unicast MAC filter.
706                  * Add secondary FCoE MAC address filter for our OUI.
707                  */
708                 rtnl_lock();
709                 if (compare_ether_addr(fc->data_src_addr, (u8[6]) { 0 }))
710                         dev_unicast_delete(fc->real_dev, fc->data_src_addr,
711                                            ETH_ALEN);
712                 fc_fcoe_set_mac(fc->data_src_addr, fh->fh_d_id);
713                 dev_unicast_add(fc->real_dev, fc->data_src_addr, ETH_ALEN);
714                 rtnl_unlock();
715
716                 fc->flogi_progress = 0;
717         } else if (op == ELS_FLOGI && fh->fh_r_ctl == FC_RCTL_ELS_REQ && sa) {
718                 /*
719                  * Save source MAC for point-to-point responses.
720                  */
721                 memcpy(fc->dest_addr, sa, ETH_ALEN);
722                 fc->address_mode = FCOE_GW_ADDR_MODE;
723         }
724 }
725
726 /**
727  * fcoe_watchdog() - fcoe timer callback
728  * @vp:
729  *
730  * This checks the pending queue length for fcoe and set lport qfull
731  * if the FCOE_MAX_QUEUE_DEPTH is reached. This is done for all fc_lport on the
732  * fcoe_hostlist.
733  *
734  * Returns: 0 for success
735  */
736 void fcoe_watchdog(ulong vp)
737 {
738         struct fcoe_softc *fc;
739
740         read_lock(&fcoe_hostlist_lock);
741         list_for_each_entry(fc, &fcoe_hostlist, list) {
742                 if (fc->lp)
743                         fcoe_check_wait_queue(fc->lp);
744         }
745         read_unlock(&fcoe_hostlist_lock);
746
747         fcoe_timer.expires = jiffies + (1 * HZ);
748         add_timer(&fcoe_timer);
749 }
750
751
752 /**
753  * fcoe_check_wait_queue() - put the skb into fcoe pending xmit queue
754  * @lp: the fc_port for this skb
755  * @skb: the associated skb to be xmitted
756  *
757  * This empties the wait_queue, dequeue the head of the wait_queue queue
758  * and calls fcoe_start_io() for each packet, if all skb have been
759  * transmitted, return qlen or -1 if a error occurs, then restore
760  * wait_queue and  try again later.
761  *
762  * The wait_queue is used when the skb transmit fails. skb will go
763  * in the wait_queue which will be emptied by the time function OR
764  * by the next skb transmit.
765  *
766  * Returns: 0 for success
767  */
768 static int fcoe_check_wait_queue(struct fc_lport *lp)
769 {
770         struct fcoe_softc *fc = lport_priv(lp);
771         struct sk_buff *skb;
772         int rc = -1;
773
774         spin_lock_bh(&fc->fcoe_pending_queue.lock);
775         if (fc->fcoe_pending_queue_active)
776                 goto out;
777         fc->fcoe_pending_queue_active = 1;
778
779         while (fc->fcoe_pending_queue.qlen) {
780                 /* keep qlen > 0 until fcoe_start_io succeeds */
781                 fc->fcoe_pending_queue.qlen++;
782                 skb = __skb_dequeue(&fc->fcoe_pending_queue);
783
784                 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
785                 rc = fcoe_start_io(skb);
786                 spin_lock_bh(&fc->fcoe_pending_queue.lock);
787
788                 if (rc) {
789                         __skb_queue_head(&fc->fcoe_pending_queue, skb);
790                         /* undo temporary increment above */
791                         fc->fcoe_pending_queue.qlen--;
792                         break;
793                 }
794                 /* undo temporary increment above */
795                 fc->fcoe_pending_queue.qlen--;
796         }
797
798         if (fc->fcoe_pending_queue.qlen < FCOE_LOW_QUEUE_DEPTH)
799                 lp->qfull = 0;
800         fc->fcoe_pending_queue_active = 0;
801         rc = fc->fcoe_pending_queue.qlen;
802 out:
803         spin_unlock_bh(&fc->fcoe_pending_queue.lock);
804         return rc;
805 }
806
807 /**
808  * fcoe_dev_setup() - setup link change notification interface
809  */
810 static void fcoe_dev_setup()
811 {
812         /*
813          * here setup a interface specific wd time to
814          * monitor the link state
815          */
816         register_netdevice_notifier(&fcoe_notifier);
817 }
818
819 /**
820  * fcoe_dev_setup() - cleanup link change notification interface
821  */
822 static void fcoe_dev_cleanup(void)
823 {
824         unregister_netdevice_notifier(&fcoe_notifier);
825 }
826
827 /**
828  * fcoe_device_notification() - netdev event notification callback
829  * @notifier: context of the notification
830  * @event: type of event
831  * @ptr: fixed array for output parsed ifname
832  *
833  * This function is called by the ethernet driver in case of link change event
834  *
835  * Returns: 0 for success
836  */
837 static int fcoe_device_notification(struct notifier_block *notifier,
838                                     ulong event, void *ptr)
839 {
840         struct fc_lport *lp = NULL;
841         struct net_device *real_dev = ptr;
842         struct fcoe_softc *fc;
843         struct fcoe_dev_stats *stats;
844         u32 new_link_up;
845         u32 mfs;
846         int rc = NOTIFY_OK;
847
848         read_lock(&fcoe_hostlist_lock);
849         list_for_each_entry(fc, &fcoe_hostlist, list) {
850                 if (fc->real_dev == real_dev) {
851                         lp = fc->lp;
852                         break;
853                 }
854         }
855         read_unlock(&fcoe_hostlist_lock);
856         if (lp == NULL) {
857                 rc = NOTIFY_DONE;
858                 goto out;
859         }
860
861         new_link_up = lp->link_up;
862         switch (event) {
863         case NETDEV_DOWN:
864         case NETDEV_GOING_DOWN:
865                 new_link_up = 0;
866                 break;
867         case NETDEV_UP:
868         case NETDEV_CHANGE:
869                 new_link_up = !fcoe_link_ok(lp);
870                 break;
871         case NETDEV_CHANGEMTU:
872                 mfs = fc->real_dev->mtu -
873                         (sizeof(struct fcoe_hdr) +
874                          sizeof(struct fcoe_crc_eof));
875                 if (mfs >= FC_MIN_MAX_FRAME)
876                         fc_set_mfs(lp, mfs);
877                 new_link_up = !fcoe_link_ok(lp);
878                 break;
879         case NETDEV_REGISTER:
880                 break;
881         default:
882                 FC_DBG("unknown event %ld call", event);
883         }
884         if (lp->link_up != new_link_up) {
885                 if (new_link_up)
886                         fc_linkup(lp);
887                 else {
888                         stats = lp->dev_stats[smp_processor_id()];
889                         if (stats)
890                                 stats->LinkFailureCount++;
891                         fc_linkdown(lp);
892                         fcoe_clean_pending_queue(lp);
893                 }
894         }
895 out:
896         return rc;
897 }
898
899 /**
900  * fcoe_if_to_netdev() - parse a name buffer to get netdev
901  * @ifname: fixed array for output parsed ifname
902  * @buffer: incoming buffer to be copied
903  *
904  * Returns: NULL or ptr to netdeive
905  */
906 static struct net_device *fcoe_if_to_netdev(const char *buffer)
907 {
908         char *cp;
909         char ifname[IFNAMSIZ + 2];
910
911         if (buffer) {
912                 strlcpy(ifname, buffer, IFNAMSIZ);
913                 cp = ifname + strlen(ifname);
914                 while (--cp >= ifname && *cp == '\n')
915                         *cp = '\0';
916                 return dev_get_by_name(&init_net, ifname);
917         }
918         return NULL;
919 }
920
921 /**
922  * fcoe_netdev_to_module_owner() - finds out the nic drive moddule of the netdev
923  * @netdev: the target netdev
924  *
925  * Returns: ptr to the struct module, NULL for failure
926  */
927 static struct module *
928 fcoe_netdev_to_module_owner(const struct net_device *netdev)
929 {
930         struct device *dev;
931
932         if (!netdev)
933                 return NULL;
934
935         dev = netdev->dev.parent;
936         if (!dev)
937                 return NULL;
938
939         if (!dev->driver)
940                 return NULL;
941
942         return dev->driver->owner;
943 }
944
945 /**
946  * fcoe_ethdrv_get() - Hold the Ethernet driver
947  * @netdev: the target netdev
948  *
949  * Holds the Ethernet driver module by try_module_get() for
950  * the corresponding netdev.
951  *
952  * Returns: 0 for succsss
953  */
954 static int fcoe_ethdrv_get(const struct net_device *netdev)
955 {
956         struct module *owner;
957
958         owner = fcoe_netdev_to_module_owner(netdev);
959         if (owner) {
960                 printk(KERN_DEBUG "fcoe:hold driver module %s for %s\n",
961                        module_name(owner), netdev->name);
962                 return  try_module_get(owner);
963         }
964         return -ENODEV;
965 }
966
967 /**
968  * fcoe_ethdrv_put() - Release the Ethernet driver
969  * @netdev: the target netdev
970  *
971  * Releases the Ethernet driver module by module_put for
972  * the corresponding netdev.
973  *
974  * Returns: 0 for succsss
975  */
976 static int fcoe_ethdrv_put(const struct net_device *netdev)
977 {
978         struct module *owner;
979
980         owner = fcoe_netdev_to_module_owner(netdev);
981         if (owner) {
982                 printk(KERN_DEBUG "fcoe:release driver module %s for %s\n",
983                        module_name(owner), netdev->name);
984                 module_put(owner);
985                 return 0;
986         }
987         return -ENODEV;
988 }
989
990 /**
991  * fcoe_destroy() - handles the destroy from sysfs
992  * @buffer: expcted to be a eth if name
993  * @kp: associated kernel param
994  *
995  * Returns: 0 for success
996  */
997 static int fcoe_destroy(const char *buffer, struct kernel_param *kp)
998 {
999         int rc;
1000         struct net_device *netdev;
1001
1002         netdev = fcoe_if_to_netdev(buffer);
1003         if (!netdev) {
1004                 rc = -ENODEV;
1005                 goto out_nodev;
1006         }
1007         /* look for existing lport */
1008         if (!fcoe_hostlist_lookup(netdev)) {
1009                 rc = -ENODEV;
1010                 goto out_putdev;
1011         }
1012         /* pass to transport */
1013         rc = fcoe_transport_release(netdev);
1014         if (rc) {
1015                 printk(KERN_ERR "fcoe: fcoe_transport_release(%s) failed\n",
1016                        netdev->name);
1017                 rc = -EIO;
1018                 goto out_putdev;
1019         }
1020         fcoe_ethdrv_put(netdev);
1021         rc = 0;
1022 out_putdev:
1023         dev_put(netdev);
1024 out_nodev:
1025         return rc;
1026 }
1027
1028 /**
1029  * fcoe_create() - Handles the create call from sysfs
1030  * @buffer: expcted to be a eth if name
1031  * @kp: associated kernel param
1032  *
1033  * Returns: 0 for success
1034  */
1035 static int fcoe_create(const char *buffer, struct kernel_param *kp)
1036 {
1037         int rc;
1038         struct net_device *netdev;
1039
1040         netdev = fcoe_if_to_netdev(buffer);
1041         if (!netdev) {
1042                 rc = -ENODEV;
1043                 goto out_nodev;
1044         }
1045         /* look for existing lport */
1046         if (fcoe_hostlist_lookup(netdev)) {
1047                 rc = -EEXIST;
1048                 goto out_putdev;
1049         }
1050         fcoe_ethdrv_get(netdev);
1051
1052         /* pass to transport */
1053         rc = fcoe_transport_attach(netdev);
1054         if (rc) {
1055                 printk(KERN_ERR "fcoe: fcoe_transport_attach(%s) failed\n",
1056                        netdev->name);
1057                 fcoe_ethdrv_put(netdev);
1058                 rc = -EIO;
1059                 goto out_putdev;
1060         }
1061         rc = 0;
1062 out_putdev:
1063         dev_put(netdev);
1064 out_nodev:
1065         return rc;
1066 }
1067
1068 module_param_call(create, fcoe_create, NULL, NULL, S_IWUSR);
1069 __MODULE_PARM_TYPE(create, "string");
1070 MODULE_PARM_DESC(create, "Create fcoe port using net device passed in.");
1071 module_param_call(destroy, fcoe_destroy, NULL, NULL, S_IWUSR);
1072 __MODULE_PARM_TYPE(destroy, "string");
1073 MODULE_PARM_DESC(destroy, "Destroy fcoe port");
1074
1075 /**
1076  * fcoe_link_ok() - Check if link is ok for the fc_lport
1077  * @lp: ptr to the fc_lport
1078  *
1079  * Any permanently-disqualifying conditions have been previously checked.
1080  * This also updates the speed setting, which may change with link for 100/1000.
1081  *
1082  * This function should probably be checking for PAUSE support at some point
1083  * in the future. Currently Per-priority-pause is not determinable using
1084  * ethtool, so we shouldn't be restrictive until that problem is resolved.
1085  *
1086  * Returns: 0 if link is OK for use by FCoE.
1087  *
1088  */
1089 int fcoe_link_ok(struct fc_lport *lp)
1090 {
1091         struct fcoe_softc *fc = lport_priv(lp);
1092         struct net_device *dev = fc->real_dev;
1093         struct ethtool_cmd ecmd = { ETHTOOL_GSET };
1094         int rc = 0;
1095
1096         if ((dev->flags & IFF_UP) && netif_carrier_ok(dev)) {
1097                 dev = fc->phys_dev;
1098                 if (dev->ethtool_ops->get_settings) {
1099                         dev->ethtool_ops->get_settings(dev, &ecmd);
1100                         lp->link_supported_speeds &=
1101                                 ~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
1102                         if (ecmd.supported & (SUPPORTED_1000baseT_Half |
1103                                               SUPPORTED_1000baseT_Full))
1104                                 lp->link_supported_speeds |= FC_PORTSPEED_1GBIT;
1105                         if (ecmd.supported & SUPPORTED_10000baseT_Full)
1106                                 lp->link_supported_speeds |=
1107                                         FC_PORTSPEED_10GBIT;
1108                         if (ecmd.speed == SPEED_1000)
1109                                 lp->link_speed = FC_PORTSPEED_1GBIT;
1110                         if (ecmd.speed == SPEED_10000)
1111                                 lp->link_speed = FC_PORTSPEED_10GBIT;
1112                 }
1113         } else
1114                 rc = -1;
1115
1116         return rc;
1117 }
1118 EXPORT_SYMBOL_GPL(fcoe_link_ok);
1119
1120 /**
1121  * fcoe_percpu_clean() - Clear the pending skbs for an lport
1122  * @lp: the fc_lport
1123  */
1124 void fcoe_percpu_clean(struct fc_lport *lp)
1125 {
1126         struct fcoe_percpu_s *pp;
1127         struct fcoe_rcv_info *fr;
1128         struct sk_buff_head *list;
1129         struct sk_buff *skb, *next;
1130         struct sk_buff *head;
1131         unsigned int cpu;
1132
1133         for_each_possible_cpu(cpu) {
1134                 pp = &per_cpu(fcoe_percpu, cpu);
1135                 spin_lock_bh(&pp->fcoe_rx_list.lock);
1136                 list = &pp->fcoe_rx_list;
1137                 head = list->next;
1138                 for (skb = head; skb != (struct sk_buff *)list;
1139                      skb = next) {
1140                         next = skb->next;
1141                         fr = fcoe_dev_from_skb(skb);
1142                         if (fr->fr_dev == lp) {
1143                                 __skb_unlink(skb, list);
1144                                 kfree_skb(skb);
1145                         }
1146                 }
1147                 spin_unlock_bh(&pp->fcoe_rx_list.lock);
1148         }
1149 }
1150 EXPORT_SYMBOL_GPL(fcoe_percpu_clean);
1151
1152 /**
1153  * fcoe_clean_pending_queue() - Dequeue a skb and free it
1154  * @lp: the corresponding fc_lport
1155  *
1156  * Returns: none
1157  */
1158 void fcoe_clean_pending_queue(struct fc_lport *lp)
1159 {
1160         struct fcoe_softc  *fc = lport_priv(lp);
1161         struct sk_buff *skb;
1162
1163         spin_lock_bh(&fc->fcoe_pending_queue.lock);
1164         while ((skb = __skb_dequeue(&fc->fcoe_pending_queue)) != NULL) {
1165                 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
1166                 kfree_skb(skb);
1167                 spin_lock_bh(&fc->fcoe_pending_queue.lock);
1168         }
1169         spin_unlock_bh(&fc->fcoe_pending_queue.lock);
1170 }
1171 EXPORT_SYMBOL_GPL(fcoe_clean_pending_queue);
1172
1173 /**
1174  * libfc_host_alloc() - Allocate a Scsi_Host with room for the fc_lport
1175  * @sht: ptr to the scsi host templ
1176  * @priv_size: size of private data after fc_lport
1177  *
1178  * Returns: ptr to Scsi_Host
1179  * TODO: to libfc?
1180  */
1181 static inline struct Scsi_Host *
1182 libfc_host_alloc(struct scsi_host_template *sht, int priv_size)
1183 {
1184         return scsi_host_alloc(sht, sizeof(struct fc_lport) + priv_size);
1185 }
1186
1187 /**
1188  * fcoe_host_alloc() - Allocate a Scsi_Host with room for the fcoe_softc
1189  * @sht: ptr to the scsi host templ
1190  * @priv_size: size of private data after fc_lport
1191  *
1192  * Returns: ptr to Scsi_Host
1193  */
1194 struct Scsi_Host *fcoe_host_alloc(struct scsi_host_template *sht, int priv_size)
1195 {
1196         return libfc_host_alloc(sht, sizeof(struct fcoe_softc) + priv_size);
1197 }
1198 EXPORT_SYMBOL_GPL(fcoe_host_alloc);
1199
1200 /**
1201  * fcoe_reset() - Resets the fcoe
1202  * @shost: shost the reset is from
1203  *
1204  * Returns: always 0
1205  */
1206 int fcoe_reset(struct Scsi_Host *shost)
1207 {
1208         struct fc_lport *lport = shost_priv(shost);
1209         fc_lport_reset(lport);
1210         return 0;
1211 }
1212 EXPORT_SYMBOL_GPL(fcoe_reset);
1213
1214 /**
1215  * fcoe_wwn_from_mac() - Converts 48-bit IEEE MAC address to 64-bit FC WWN.
1216  * @mac: mac address
1217  * @scheme: check port
1218  * @port: port indicator for converting
1219  *
1220  * Returns: u64 fc world wide name
1221  */
1222 u64 fcoe_wwn_from_mac(unsigned char mac[MAX_ADDR_LEN],
1223                       unsigned int scheme, unsigned int port)
1224 {
1225         u64 wwn;
1226         u64 host_mac;
1227
1228         /* The MAC is in NO, so flip only the low 48 bits */
1229         host_mac = ((u64) mac[0] << 40) |
1230                 ((u64) mac[1] << 32) |
1231                 ((u64) mac[2] << 24) |
1232                 ((u64) mac[3] << 16) |
1233                 ((u64) mac[4] << 8) |
1234                 (u64) mac[5];
1235
1236         WARN_ON(host_mac >= (1ULL << 48));
1237         wwn = host_mac | ((u64) scheme << 60);
1238         switch (scheme) {
1239         case 1:
1240                 WARN_ON(port != 0);
1241                 break;
1242         case 2:
1243                 WARN_ON(port >= 0xfff);
1244                 wwn |= (u64) port << 48;
1245                 break;
1246         default:
1247                 WARN_ON(1);
1248                 break;
1249         }
1250
1251         return wwn;
1252 }
1253 EXPORT_SYMBOL_GPL(fcoe_wwn_from_mac);
1254
1255 /**
1256  * fcoe_hostlist_lookup_softc() - find the corresponding lport by a given device
1257  * @device: this is currently ptr to net_device
1258  *
1259  * Returns: NULL or the located fcoe_softc
1260  */
1261 static struct fcoe_softc *
1262 fcoe_hostlist_lookup_softc(const struct net_device *dev)
1263 {
1264         struct fcoe_softc *fc;
1265
1266         read_lock(&fcoe_hostlist_lock);
1267         list_for_each_entry(fc, &fcoe_hostlist, list) {
1268                 if (fc->real_dev == dev) {
1269                         read_unlock(&fcoe_hostlist_lock);
1270                         return fc;
1271                 }
1272         }
1273         read_unlock(&fcoe_hostlist_lock);
1274         return NULL;
1275 }
1276
1277 /**
1278  * fcoe_hostlist_lookup() - Find the corresponding lport by netdev
1279  * @netdev: ptr to net_device
1280  *
1281  * Returns: 0 for success
1282  */
1283 struct fc_lport *fcoe_hostlist_lookup(const struct net_device *netdev)
1284 {
1285         struct fcoe_softc *fc;
1286
1287         fc = fcoe_hostlist_lookup_softc(netdev);
1288
1289         return (fc) ? fc->lp : NULL;
1290 }
1291 EXPORT_SYMBOL_GPL(fcoe_hostlist_lookup);
1292
1293 /**
1294  * fcoe_hostlist_add() - Add a lport to lports list
1295  * @lp: ptr to the fc_lport to badded
1296  *
1297  * Returns: 0 for success
1298  */
1299 int fcoe_hostlist_add(const struct fc_lport *lp)
1300 {
1301         struct fcoe_softc *fc;
1302
1303         fc = fcoe_hostlist_lookup_softc(fcoe_netdev(lp));
1304         if (!fc) {
1305                 fc = lport_priv(lp);
1306                 write_lock_bh(&fcoe_hostlist_lock);
1307                 list_add_tail(&fc->list, &fcoe_hostlist);
1308                 write_unlock_bh(&fcoe_hostlist_lock);
1309         }
1310         return 0;
1311 }
1312 EXPORT_SYMBOL_GPL(fcoe_hostlist_add);
1313
1314 /**
1315  * fcoe_hostlist_remove() - remove a lport from lports list
1316  * @lp: ptr to the fc_lport to badded
1317  *
1318  * Returns: 0 for success
1319  */
1320 int fcoe_hostlist_remove(const struct fc_lport *lp)
1321 {
1322         struct fcoe_softc *fc;
1323
1324         fc = fcoe_hostlist_lookup_softc(fcoe_netdev(lp));
1325         BUG_ON(!fc);
1326         write_lock_bh(&fcoe_hostlist_lock);
1327         list_del(&fc->list);
1328         write_unlock_bh(&fcoe_hostlist_lock);
1329
1330         return 0;
1331 }
1332 EXPORT_SYMBOL_GPL(fcoe_hostlist_remove);
1333
1334 /**
1335  * fcoe_libfc_config() - sets up libfc related properties for lport
1336  * @lp: ptr to the fc_lport
1337  * @tt: libfc function template
1338  *
1339  * Returns : 0 for success
1340  */
1341 int fcoe_libfc_config(struct fc_lport *lp, struct libfc_function_template *tt)
1342 {
1343         /* Set the function pointers set by the LLDD */
1344         memcpy(&lp->tt, tt, sizeof(*tt));
1345         if (fc_fcp_init(lp))
1346                 return -ENOMEM;
1347         fc_exch_init(lp);
1348         fc_elsct_init(lp);
1349         fc_lport_init(lp);
1350         fc_rport_init(lp);
1351         fc_disc_init(lp);
1352
1353         return 0;
1354 }
1355 EXPORT_SYMBOL_GPL(fcoe_libfc_config);
1356
1357 /**
1358  * fcoe_init() - fcoe module loading initialization
1359  *
1360  * Initialization routine
1361  * 1. Will create fc transport software structure
1362  * 2. initialize the link list of port information structure
1363  *
1364  * Returns 0 on success, negative on failure
1365  */
1366 static int __init fcoe_init(void)
1367 {
1368         unsigned int cpu;
1369         struct fcoe_percpu_s *p;
1370
1371         INIT_LIST_HEAD(&fcoe_hostlist);
1372         rwlock_init(&fcoe_hostlist_lock);
1373
1374 #ifdef CONFIG_HOTPLUG_CPU
1375         register_cpu_notifier(&fcoe_cpu_notifier);
1376 #endif /* CONFIG_HOTPLUG_CPU */
1377
1378         for_each_possible_cpu(cpu) {
1379                 p = &per_cpu(fcoe_percpu, cpu);
1380                 skb_queue_head_init(&p->fcoe_rx_list);
1381         }
1382
1383         /*
1384          * initialize per CPU interrupt thread
1385          */
1386         for_each_online_cpu(cpu) {
1387                 p = &per_cpu(fcoe_percpu, cpu);
1388                 p->thread = kthread_create(fcoe_percpu_receive_thread,
1389                                            (void *)p, "fcoethread/%d", cpu);
1390
1391                 /*
1392                  * If there is no error then bind the thread to the CPU
1393                  * and wake it up.
1394                  */
1395                 if (!IS_ERR(p->thread)) {
1396                         kthread_bind(p->thread, cpu);
1397                         wake_up_process(p->thread);
1398                 } else {
1399                         p->thread = NULL;
1400                 }
1401         }
1402
1403         /*
1404          * setup link change notification
1405          */
1406         fcoe_dev_setup();
1407
1408         setup_timer(&fcoe_timer, fcoe_watchdog, 0);
1409
1410         mod_timer(&fcoe_timer, jiffies + (10 * HZ));
1411
1412         /* initiatlize the fcoe transport */
1413         fcoe_transport_init();
1414
1415         fcoe_sw_init();
1416
1417         return 0;
1418 }
1419 module_init(fcoe_init);
1420
1421 /**
1422  * fcoe_exit() - fcoe module unloading cleanup
1423  *
1424  * Returns 0 on success, negative on failure
1425  */
1426 static void __exit fcoe_exit(void)
1427 {
1428         unsigned int cpu;
1429         struct fcoe_softc *fc, *tmp;
1430         struct fcoe_percpu_s *p;
1431         struct sk_buff *skb;
1432
1433         /*
1434          * Stop all call back interfaces
1435          */
1436 #ifdef CONFIG_HOTPLUG_CPU
1437         unregister_cpu_notifier(&fcoe_cpu_notifier);
1438 #endif /* CONFIG_HOTPLUG_CPU */
1439         fcoe_dev_cleanup();
1440
1441         /*
1442          * stop timer
1443          */
1444         del_timer_sync(&fcoe_timer);
1445
1446         /* releases the associated fcoe transport for each lport */
1447         list_for_each_entry_safe(fc, tmp, &fcoe_hostlist, list)
1448                 fcoe_transport_release(fc->real_dev);
1449
1450         for_each_possible_cpu(cpu) {
1451                 p = &per_cpu(fcoe_percpu, cpu);
1452                 if (p->thread) {
1453                         kthread_stop(p->thread);
1454                         spin_lock_bh(&p->fcoe_rx_list.lock);
1455                         while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1456                                 kfree_skb(skb);
1457                         spin_unlock_bh(&p->fcoe_rx_list.lock);
1458                         if (p->crc_eof_page)
1459                                 put_page(p->crc_eof_page);
1460                 }
1461         }
1462
1463         /* remove sw trasnport */
1464         fcoe_sw_exit();
1465
1466         /* detach the transport */
1467         fcoe_transport_exit();
1468 }
1469 module_exit(fcoe_exit);