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