[SCSI] fcoe: moves common FCoE library API functions to libfcoe module
[safe/jmp/linux-2.6] / drivers / scsi / fcoe / fcoe.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
48 static int debug_fcoe;
49
50 #define FCOE_MAX_QUEUE_DEPTH  256
51 #define FCOE_LOW_QUEUE_DEPTH  32
52
53 /* destination address mode */
54 #define FCOE_GW_ADDR_MODE           0x00
55 #define FCOE_FCOUI_ADDR_MODE        0x01
56
57 #define FCOE_WORD_TO_BYTE  4
58
59 #define FCOE_VERSION    "0.1"
60 #define FCOE_NAME       "fcoe"
61 #define FCOE_VENDOR     "Open-FCoE.org"
62
63 #define FCOE_MAX_LUN            255
64 #define FCOE_MAX_FCP_TARGET     256
65
66 #define FCOE_MAX_OUTSTANDING_COMMANDS   1024
67
68 #define FCOE_MIN_XID            0x0001  /* the min xid supported by fcoe_sw */
69 #define FCOE_MAX_XID            0x07ef  /* the max xid supported by fcoe_sw */
70
71 MODULE_AUTHOR("Open-FCoE.org");
72 MODULE_DESCRIPTION("FCoE");
73 MODULE_LICENSE("GPL v2");
74
75 /* fcoe host list */
76 LIST_HEAD(fcoe_hostlist);
77 DEFINE_RWLOCK(fcoe_hostlist_lock);
78 DEFINE_TIMER(fcoe_timer, NULL, 0, 0);
79 DEFINE_PER_CPU(struct fcoe_percpu_s, fcoe_percpu);
80
81
82 /* Function Prototyes */
83 static int fcoe_check_wait_queue(struct fc_lport *);
84 static void fcoe_recv_flogi(struct fcoe_softc *, struct fc_frame *, u8 *);
85 static int fcoe_device_notification(struct notifier_block *, ulong, void *);
86 static void fcoe_dev_setup(void);
87 static void fcoe_dev_cleanup(void);
88
89 /* notification function from net device */
90 static struct notifier_block fcoe_notifier = {
91         .notifier_call = fcoe_device_notification,
92 };
93
94 static struct scsi_transport_template *scsi_transport_fcoe_sw;
95
96 struct fc_function_template fcoe_transport_function = {
97         .show_host_node_name = 1,
98         .show_host_port_name = 1,
99         .show_host_supported_classes = 1,
100         .show_host_supported_fc4s = 1,
101         .show_host_active_fc4s = 1,
102         .show_host_maxframe_size = 1,
103
104         .show_host_port_id = 1,
105         .show_host_supported_speeds = 1,
106         .get_host_speed = fc_get_host_speed,
107         .show_host_speed = 1,
108         .show_host_port_type = 1,
109         .get_host_port_state = fc_get_host_port_state,
110         .show_host_port_state = 1,
111         .show_host_symbolic_name = 1,
112
113         .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
114         .show_rport_maxframe_size = 1,
115         .show_rport_supported_classes = 1,
116
117         .show_host_fabric_name = 1,
118         .show_starget_node_name = 1,
119         .show_starget_port_name = 1,
120         .show_starget_port_id = 1,
121         .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
122         .show_rport_dev_loss_tmo = 1,
123         .get_fc_host_stats = fc_get_host_stats,
124         .issue_fc_host_lip = fcoe_reset,
125
126         .terminate_rport_io = fc_rport_terminate_io,
127 };
128
129 static struct scsi_host_template fcoe_shost_template = {
130         .module = THIS_MODULE,
131         .name = "FCoE Driver",
132         .proc_name = FCOE_NAME,
133         .queuecommand = fc_queuecommand,
134         .eh_abort_handler = fc_eh_abort,
135         .eh_device_reset_handler = fc_eh_device_reset,
136         .eh_host_reset_handler = fc_eh_host_reset,
137         .slave_alloc = fc_slave_alloc,
138         .change_queue_depth = fc_change_queue_depth,
139         .change_queue_type = fc_change_queue_type,
140         .this_id = -1,
141         .cmd_per_lun = 32,
142         .can_queue = FCOE_MAX_OUTSTANDING_COMMANDS,
143         .use_clustering = ENABLE_CLUSTERING,
144         .sg_tablesize = SG_ALL,
145         .max_sectors = 0xffff,
146 };
147
148 /**
149  * fcoe_lport_config() - sets up the fc_lport
150  * @lp: ptr to the fc_lport
151  * @shost: ptr to the parent scsi host
152  *
153  * Returns: 0 for success
154  */
155 static int fcoe_lport_config(struct fc_lport *lp)
156 {
157         lp->link_up = 0;
158         lp->qfull = 0;
159         lp->max_retry_count = 3;
160         lp->e_d_tov = 2 * 1000; /* FC-FS default */
161         lp->r_a_tov = 2 * 2 * 1000;
162         lp->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
163                               FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
164
165         fc_lport_init_stats(lp);
166
167         /* lport fc_lport related configuration */
168         fc_lport_config(lp);
169
170         /* offload related configuration */
171         lp->crc_offload = 0;
172         lp->seq_offload = 0;
173         lp->lro_enabled = 0;
174         lp->lro_xid = 0;
175         lp->lso_max = 0;
176
177         return 0;
178 }
179
180 /**
181  * fcoe_netdev_config() - Set up netdev for SW FCoE
182  * @lp : ptr to the fc_lport
183  * @netdev : ptr to the associated netdevice struct
184  *
185  * Must be called after fcoe_lport_config() as it will use lport mutex
186  *
187  * Returns : 0 for success
188  */
189 static int fcoe_netdev_config(struct fc_lport *lp, struct net_device *netdev)
190 {
191         u32 mfs;
192         u64 wwnn, wwpn;
193         struct fcoe_softc *fc;
194         u8 flogi_maddr[ETH_ALEN];
195
196         /* Setup lport private data to point to fcoe softc */
197         fc = lport_priv(lp);
198         fc->lp = lp;
199         fc->real_dev = netdev;
200         fc->phys_dev = netdev;
201
202         /* Require support for get_pauseparam ethtool op. */
203         if (netdev->priv_flags & IFF_802_1Q_VLAN)
204                 fc->phys_dev = vlan_dev_real_dev(netdev);
205
206         /* Do not support for bonding device */
207         if ((fc->real_dev->priv_flags & IFF_MASTER_ALB) ||
208             (fc->real_dev->priv_flags & IFF_SLAVE_INACTIVE) ||
209             (fc->real_dev->priv_flags & IFF_MASTER_8023AD)) {
210                 return -EOPNOTSUPP;
211         }
212
213         /*
214          * Determine max frame size based on underlying device and optional
215          * user-configured limit.  If the MFS is too low, fcoe_link_ok()
216          * will return 0, so do this first.
217          */
218         mfs = fc->real_dev->mtu - (sizeof(struct fcoe_hdr) +
219                                    sizeof(struct fcoe_crc_eof));
220         if (fc_set_mfs(lp, mfs))
221                 return -EINVAL;
222
223         if (!fcoe_link_ok(lp))
224                 lp->link_up = 1;
225
226         /* offload features support */
227         if (fc->real_dev->features & NETIF_F_SG)
228                 lp->sg_supp = 1;
229
230 #ifdef NETIF_F_FCOE_CRC
231         if (netdev->features & NETIF_F_FCOE_CRC) {
232                 lp->crc_offload = 1;
233                 printk(KERN_DEBUG "fcoe:%s supports FCCRC offload\n",
234                        netdev->name);
235         }
236 #endif
237 #ifdef NETIF_F_FSO
238         if (netdev->features & NETIF_F_FSO) {
239                 lp->seq_offload = 1;
240                 lp->lso_max = netdev->gso_max_size;
241                 printk(KERN_DEBUG "fcoe:%s supports LSO for max len 0x%x\n",
242                        netdev->name, lp->lso_max);
243         }
244 #endif
245         if (netdev->fcoe_ddp_xid) {
246                 lp->lro_enabled = 1;
247                 lp->lro_xid = netdev->fcoe_ddp_xid;
248                 printk(KERN_DEBUG "fcoe:%s supports LRO for max xid 0x%x\n",
249                        netdev->name, lp->lro_xid);
250         }
251         skb_queue_head_init(&fc->fcoe_pending_queue);
252         fc->fcoe_pending_queue_active = 0;
253
254         /* setup Source Mac Address */
255         memcpy(fc->ctl_src_addr, fc->real_dev->dev_addr,
256                fc->real_dev->addr_len);
257
258         wwnn = fcoe_wwn_from_mac(fc->real_dev->dev_addr, 1, 0);
259         fc_set_wwnn(lp, wwnn);
260         /* XXX - 3rd arg needs to be vlan id */
261         wwpn = fcoe_wwn_from_mac(fc->real_dev->dev_addr, 2, 0);
262         fc_set_wwpn(lp, wwpn);
263
264         /*
265          * Add FCoE MAC address as second unicast MAC address
266          * or enter promiscuous mode if not capable of listening
267          * for multiple unicast MACs.
268          */
269         rtnl_lock();
270         memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
271         dev_unicast_add(fc->real_dev, flogi_maddr, ETH_ALEN);
272         rtnl_unlock();
273
274         /*
275          * setup the receive function from ethernet driver
276          * on the ethertype for the given device
277          */
278         fc->fcoe_packet_type.func = fcoe_rcv;
279         fc->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
280         fc->fcoe_packet_type.dev = fc->real_dev;
281         dev_add_pack(&fc->fcoe_packet_type);
282
283         return 0;
284 }
285
286 /**
287  * fcoe_shost_config() - Sets up fc_lport->host
288  * @lp : ptr to the fc_lport
289  * @shost : ptr to the associated scsi host
290  * @dev : device associated to scsi host
291  *
292  * Must be called after fcoe_lport_config() and fcoe_netdev_config()
293  *
294  * Returns : 0 for success
295  */
296 static int fcoe_shost_config(struct fc_lport *lp, struct Scsi_Host *shost,
297                                 struct device *dev)
298 {
299         int rc = 0;
300
301         /* lport scsi host config */
302         lp->host = shost;
303
304         lp->host->max_lun = FCOE_MAX_LUN;
305         lp->host->max_id = FCOE_MAX_FCP_TARGET;
306         lp->host->max_channel = 0;
307         lp->host->transportt = scsi_transport_fcoe_sw;
308
309         /* add the new host to the SCSI-ml */
310         rc = scsi_add_host(lp->host, dev);
311         if (rc) {
312                 FC_DBG("fcoe_shost_config:error on scsi_add_host\n");
313                 return rc;
314         }
315         sprintf(fc_host_symbolic_name(lp->host), "%s v%s over %s",
316                 FCOE_NAME, FCOE_VERSION,
317                 fcoe_netdev(lp)->name);
318
319         return 0;
320 }
321
322 /**
323  * fcoe_em_config() - allocates em for this lport
324  * @lp: the port that em is to allocated for
325  *
326  * Returns : 0 on success
327  */
328 static inline int fcoe_em_config(struct fc_lport *lp)
329 {
330         BUG_ON(lp->emp);
331
332         lp->emp = fc_exch_mgr_alloc(lp, FC_CLASS_3,
333                                     FCOE_MIN_XID, FCOE_MAX_XID);
334         if (!lp->emp)
335                 return -ENOMEM;
336
337         return 0;
338 }
339
340 /**
341  * fcoe_if_destroy() - FCoE software HBA tear-down function
342  * @netdev: ptr to the associated net_device
343  *
344  * Returns: 0 if link is OK for use by FCoE.
345  */
346 static int fcoe_if_destroy(struct net_device *netdev)
347 {
348         struct fc_lport *lp = NULL;
349         struct fcoe_softc *fc;
350         u8 flogi_maddr[ETH_ALEN];
351
352         BUG_ON(!netdev);
353
354         printk(KERN_DEBUG "fcoe_if_destroy:interface on %s\n",
355                netdev->name);
356
357         lp = fcoe_hostlist_lookup(netdev);
358         if (!lp)
359                 return -ENODEV;
360
361         fc = lport_priv(lp);
362
363         /* Logout of the fabric */
364         fc_fabric_logoff(lp);
365
366         /* Remove the instance from fcoe's list */
367         fcoe_hostlist_remove(lp);
368
369         /* Don't listen for Ethernet packets anymore */
370         dev_remove_pack(&fc->fcoe_packet_type);
371
372         /* Cleanup the fc_lport */
373         fc_lport_destroy(lp);
374         fc_fcp_destroy(lp);
375
376         /* Detach from the scsi-ml */
377         fc_remove_host(lp->host);
378         scsi_remove_host(lp->host);
379
380         /* There are no more rports or I/O, free the EM */
381         if (lp->emp)
382                 fc_exch_mgr_free(lp->emp);
383
384         /* Delete secondary MAC addresses */
385         rtnl_lock();
386         memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
387         dev_unicast_delete(fc->real_dev, flogi_maddr, ETH_ALEN);
388         if (compare_ether_addr(fc->data_src_addr, (u8[6]) { 0 }))
389                 dev_unicast_delete(fc->real_dev, fc->data_src_addr, ETH_ALEN);
390         rtnl_unlock();
391
392         /* Free the per-CPU revieve threads */
393         fcoe_percpu_clean(lp);
394
395         /* Free existing skbs */
396         fcoe_clean_pending_queue(lp);
397
398         /* Free memory used by statistical counters */
399         fc_lport_free_stats(lp);
400
401         /* Release the net_device and Scsi_Host */
402         dev_put(fc->real_dev);
403         scsi_host_put(lp->host);
404
405         return 0;
406 }
407
408 /*
409  * fcoe_ddp_setup - calls LLD's ddp_setup through net_device
410  * @lp: the corresponding fc_lport
411  * @xid: the exchange id for this ddp transfer
412  * @sgl: the scatterlist describing this transfer
413  * @sgc: number of sg items
414  *
415  * Returns : 0 no ddp
416  */
417 static int fcoe_ddp_setup(struct fc_lport *lp, u16 xid,
418                              struct scatterlist *sgl, unsigned int sgc)
419 {
420         struct net_device *n = fcoe_netdev(lp);
421
422         if (n->netdev_ops && n->netdev_ops->ndo_fcoe_ddp_setup)
423                 return n->netdev_ops->ndo_fcoe_ddp_setup(n, xid, sgl, sgc);
424
425         return 0;
426 }
427
428 /*
429  * fcoe_ddp_done - calls LLD's ddp_done through net_device
430  * @lp: the corresponding fc_lport
431  * @xid: the exchange id for this ddp transfer
432  *
433  * Returns : the length of data that have been completed by ddp
434  */
435 static int fcoe_ddp_done(struct fc_lport *lp, u16 xid)
436 {
437         struct net_device *n = fcoe_netdev(lp);
438
439         if (n->netdev_ops && n->netdev_ops->ndo_fcoe_ddp_done)
440                 return n->netdev_ops->ndo_fcoe_ddp_done(n, xid);
441         return 0;
442 }
443
444 static struct libfc_function_template fcoe_libfc_fcn_templ = {
445         .frame_send = fcoe_xmit,
446         .ddp_setup = fcoe_ddp_setup,
447         .ddp_done = fcoe_ddp_done,
448 };
449
450 /**
451  * fcoe_if_create() - this function creates the fcoe interface
452  * @netdev: pointer the associated netdevice
453  *
454  * Creates fc_lport struct and scsi_host for lport, configures lport
455  * and starts fabric login.
456  *
457  * Returns : 0 on success
458  */
459 static int fcoe_if_create(struct net_device *netdev)
460 {
461         int rc;
462         struct fc_lport *lp = NULL;
463         struct fcoe_softc *fc;
464         struct Scsi_Host *shost;
465
466         BUG_ON(!netdev);
467
468         printk(KERN_DEBUG "fcoe_if_create:interface on %s\n",
469                netdev->name);
470
471         lp = fcoe_hostlist_lookup(netdev);
472         if (lp)
473                 return -EEXIST;
474
475         shost = fcoe_host_alloc(&fcoe_shost_template,
476                                 sizeof(struct fcoe_softc));
477         if (!shost) {
478                 FC_DBG("Could not allocate host structure\n");
479                 return -ENOMEM;
480         }
481         lp = shost_priv(shost);
482         fc = lport_priv(lp);
483
484         /* configure fc_lport, e.g., em */
485         rc = fcoe_lport_config(lp);
486         if (rc) {
487                 FC_DBG("Could not configure lport\n");
488                 goto out_host_put;
489         }
490
491         /* configure lport network properties */
492         rc = fcoe_netdev_config(lp, netdev);
493         if (rc) {
494                 FC_DBG("Could not configure netdev for lport\n");
495                 goto out_host_put;
496         }
497
498         /* configure lport scsi host properties */
499         rc = fcoe_shost_config(lp, shost, &netdev->dev);
500         if (rc) {
501                 FC_DBG("Could not configure shost for lport\n");
502                 goto out_host_put;
503         }
504
505         /* lport exch manager allocation */
506         rc = fcoe_em_config(lp);
507         if (rc) {
508                 FC_DBG("Could not configure em for lport\n");
509                 goto out_host_put;
510         }
511
512         /* Initialize the library */
513         rc = fcoe_libfc_config(lp, &fcoe_libfc_fcn_templ);
514         if (rc) {
515                 FC_DBG("Could not configure libfc for lport!\n");
516                 goto out_lp_destroy;
517         }
518
519         /* add to lports list */
520         fcoe_hostlist_add(lp);
521
522         lp->boot_time = jiffies;
523
524         fc_fabric_login(lp);
525
526         dev_hold(netdev);
527
528         return rc;
529
530 out_lp_destroy:
531         fc_exch_mgr_free(lp->emp); /* Free the EM */
532 out_host_put:
533         scsi_host_put(lp->host);
534         return rc;
535 }
536
537 /**
538  * fcoe_if_init() - attach to scsi transport
539  *
540  * Returns : 0 on success
541  */
542 static int __init fcoe_if_init(void)
543 {
544         /* attach to scsi transport */
545         scsi_transport_fcoe_sw =
546                 fc_attach_transport(&fcoe_transport_function);
547
548         if (!scsi_transport_fcoe_sw) {
549                 printk(KERN_ERR "fcoe_init:fc_attach_transport() failed\n");
550                 return -ENODEV;
551         }
552
553         return 0;
554 }
555
556 /**
557  * fcoe_if_exit() - detach from scsi transport
558  *
559  * Returns : 0 on success
560  */
561 int __exit fcoe_if_exit(void)
562 {
563         fc_release_transport(scsi_transport_fcoe_sw);
564         return 0;
565 }
566
567 /**
568  * fcoe_percpu_thread_create() - Create a receive thread for an online cpu
569  * @cpu: cpu index for the online cpu
570  */
571 static void fcoe_percpu_thread_create(unsigned int cpu)
572 {
573         struct fcoe_percpu_s *p;
574         struct task_struct *thread;
575
576         p = &per_cpu(fcoe_percpu, cpu);
577
578         thread = kthread_create(fcoe_percpu_receive_thread,
579                                 (void *)p, "fcoethread/%d", cpu);
580
581         if (likely(!IS_ERR(p->thread))) {
582                 kthread_bind(thread, cpu);
583                 wake_up_process(thread);
584
585                 spin_lock_bh(&p->fcoe_rx_list.lock);
586                 p->thread = thread;
587                 spin_unlock_bh(&p->fcoe_rx_list.lock);
588         }
589 }
590
591 /**
592  * fcoe_percpu_thread_destroy() - removes the rx thread for the given cpu
593  * @cpu: cpu index the rx thread is to be removed
594  *
595  * Destroys a per-CPU Rx thread. Any pending skbs are moved to the
596  * current CPU's Rx thread. If the thread being destroyed is bound to
597  * the CPU processing this context the skbs will be freed.
598  */
599 static void fcoe_percpu_thread_destroy(unsigned int cpu)
600 {
601         struct fcoe_percpu_s *p;
602         struct task_struct *thread;
603         struct page *crc_eof;
604         struct sk_buff *skb;
605 #ifdef CONFIG_SMP
606         struct fcoe_percpu_s *p0;
607         unsigned targ_cpu = smp_processor_id();
608 #endif /* CONFIG_SMP */
609
610         printk(KERN_DEBUG "fcoe: Destroying receive thread for CPU %d\n", cpu);
611
612         /* Prevent any new skbs from being queued for this CPU. */
613         p = &per_cpu(fcoe_percpu, cpu);
614         spin_lock_bh(&p->fcoe_rx_list.lock);
615         thread = p->thread;
616         p->thread = NULL;
617         crc_eof = p->crc_eof_page;
618         p->crc_eof_page = NULL;
619         p->crc_eof_offset = 0;
620         spin_unlock_bh(&p->fcoe_rx_list.lock);
621
622 #ifdef CONFIG_SMP
623         /*
624          * Don't bother moving the skb's if this context is running
625          * on the same CPU that is having its thread destroyed. This
626          * can easily happen when the module is removed.
627          */
628         if (cpu != targ_cpu) {
629                 p0 = &per_cpu(fcoe_percpu, targ_cpu);
630                 spin_lock_bh(&p0->fcoe_rx_list.lock);
631                 if (p0->thread) {
632                         FC_DBG("Moving frames from CPU %d to CPU %d\n",
633                                cpu, targ_cpu);
634
635                         while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
636                                 __skb_queue_tail(&p0->fcoe_rx_list, skb);
637                         spin_unlock_bh(&p0->fcoe_rx_list.lock);
638                 } else {
639                         /*
640                          * The targeted CPU is not initialized and cannot accept
641                          * new  skbs. Unlock the targeted CPU and drop the skbs
642                          * on the CPU that is going offline.
643                          */
644                         while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
645                                 kfree_skb(skb);
646                         spin_unlock_bh(&p0->fcoe_rx_list.lock);
647                 }
648         } else {
649                 /*
650                  * This scenario occurs when the module is being removed
651                  * and all threads are being destroyed. skbs will continue
652                  * to be shifted from the CPU thread that is being removed
653                  * to the CPU thread associated with the CPU that is processing
654                  * the module removal. Once there is only one CPU Rx thread it
655                  * will reach this case and we will drop all skbs and later
656                  * stop the thread.
657                  */
658                 spin_lock_bh(&p->fcoe_rx_list.lock);
659                 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
660                         kfree_skb(skb);
661                 spin_unlock_bh(&p->fcoe_rx_list.lock);
662         }
663 #else
664         /*
665          * This a non-SMP scenario where the singluar Rx thread is
666          * being removed. Free all skbs and stop the thread.
667          */
668         spin_lock_bh(&p->fcoe_rx_list.lock);
669         while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
670                 kfree_skb(skb);
671         spin_unlock_bh(&p->fcoe_rx_list.lock);
672 #endif
673
674         if (thread)
675                 kthread_stop(thread);
676
677         if (crc_eof)
678                 put_page(crc_eof);
679 }
680
681 /**
682  * fcoe_cpu_callback() - fcoe cpu hotplug event callback
683  * @nfb: callback data block
684  * @action: event triggering the callback
685  * @hcpu: index for the cpu of this event
686  *
687  * This creates or destroys per cpu data for fcoe
688  *
689  * Returns NOTIFY_OK always.
690  */
691 static int fcoe_cpu_callback(struct notifier_block *nfb,
692                              unsigned long action, void *hcpu)
693 {
694         unsigned cpu = (unsigned long)hcpu;
695
696         switch (action) {
697         case CPU_ONLINE:
698         case CPU_ONLINE_FROZEN:
699                 FC_DBG("CPU %x online: Create Rx thread\n", cpu);
700                 fcoe_percpu_thread_create(cpu);
701                 break;
702         case CPU_DEAD:
703         case CPU_DEAD_FROZEN:
704                 FC_DBG("CPU %x offline: Remove Rx thread\n", cpu);
705                 fcoe_percpu_thread_destroy(cpu);
706                 break;
707         default:
708                 break;
709         }
710         return NOTIFY_OK;
711 }
712
713 static struct notifier_block fcoe_cpu_notifier = {
714         .notifier_call = fcoe_cpu_callback,
715 };
716
717 /**
718  * fcoe_rcv() - this is the fcoe receive function called by NET_RX_SOFTIRQ
719  * @skb: the receive skb
720  * @dev: associated net device
721  * @ptype: context
722  * @odldev: last device
723  *
724  * this function will receive the packet and build fc frame and pass it up
725  *
726  * Returns: 0 for success
727  */
728 int fcoe_rcv(struct sk_buff *skb, struct net_device *dev,
729              struct packet_type *ptype, struct net_device *olddev)
730 {
731         struct fc_lport *lp;
732         struct fcoe_rcv_info *fr;
733         struct fcoe_softc *fc;
734         struct fc_frame_header *fh;
735         struct fcoe_percpu_s *fps;
736         unsigned short oxid;
737         unsigned int cpu = 0;
738
739         fc = container_of(ptype, struct fcoe_softc, fcoe_packet_type);
740         lp = fc->lp;
741         if (unlikely(lp == NULL)) {
742                 FC_DBG("cannot find hba structure");
743                 goto err2;
744         }
745
746         if (unlikely(debug_fcoe)) {
747                 FC_DBG("skb_info: len:%d data_len:%d head:%p data:%p tail:%p "
748                        "end:%p sum:%d dev:%s", skb->len, skb->data_len,
749                        skb->head, skb->data, skb_tail_pointer(skb),
750                        skb_end_pointer(skb), skb->csum,
751                        skb->dev ? skb->dev->name : "<NULL>");
752
753         }
754
755         /* check for FCOE packet type */
756         if (unlikely(eth_hdr(skb)->h_proto != htons(ETH_P_FCOE))) {
757                 FC_DBG("wrong FC type frame");
758                 goto err;
759         }
760
761         /*
762          * Check for minimum frame length, and make sure required FCoE
763          * and FC headers are pulled into the linear data area.
764          */
765         if (unlikely((skb->len < FCOE_MIN_FRAME) ||
766             !pskb_may_pull(skb, FCOE_HEADER_LEN)))
767                 goto err;
768
769         skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
770         fh = (struct fc_frame_header *) skb_transport_header(skb);
771
772         oxid = ntohs(fh->fh_ox_id);
773
774         fr = fcoe_dev_from_skb(skb);
775         fr->fr_dev = lp;
776         fr->ptype = ptype;
777
778 #ifdef CONFIG_SMP
779         /*
780          * The incoming frame exchange id(oxid) is ANDed with num of online
781          * cpu bits to get cpu and then this cpu is used for selecting
782          * a per cpu kernel thread from fcoe_percpu.
783          */
784         cpu = oxid & (num_online_cpus() - 1);
785 #endif
786
787         fps = &per_cpu(fcoe_percpu, cpu);
788         spin_lock_bh(&fps->fcoe_rx_list.lock);
789         if (unlikely(!fps->thread)) {
790                 /*
791                  * The targeted CPU is not ready, let's target
792                  * the first CPU now. For non-SMP systems this
793                  * will check the same CPU twice.
794                  */
795                 FC_DBG("CPU is online, but no receive thread ready "
796                        "for incoming skb- using first online CPU.\n");
797
798                 spin_unlock_bh(&fps->fcoe_rx_list.lock);
799                 cpu = first_cpu(cpu_online_map);
800                 fps = &per_cpu(fcoe_percpu, cpu);
801                 spin_lock_bh(&fps->fcoe_rx_list.lock);
802                 if (!fps->thread) {
803                         spin_unlock_bh(&fps->fcoe_rx_list.lock);
804                         goto err;
805                 }
806         }
807
808         /*
809          * We now have a valid CPU that we're targeting for
810          * this skb. We also have this receive thread locked,
811          * so we're free to queue skbs into it's queue.
812          */
813         __skb_queue_tail(&fps->fcoe_rx_list, skb);
814         if (fps->fcoe_rx_list.qlen == 1)
815                 wake_up_process(fps->thread);
816
817         spin_unlock_bh(&fps->fcoe_rx_list.lock);
818
819         return 0;
820 err:
821         fc_lport_get_stats(lp)->ErrorFrames++;
822
823 err2:
824         kfree_skb(skb);
825         return -1;
826 }
827 EXPORT_SYMBOL_GPL(fcoe_rcv);
828
829 /**
830  * fcoe_start_io() - pass to netdev to start xmit for fcoe
831  * @skb: the skb to be xmitted
832  *
833  * Returns: 0 for success
834  */
835 static inline int fcoe_start_io(struct sk_buff *skb)
836 {
837         int rc;
838
839         skb_get(skb);
840         rc = dev_queue_xmit(skb);
841         if (rc != 0)
842                 return rc;
843         kfree_skb(skb);
844         return 0;
845 }
846
847 /**
848  * fcoe_get_paged_crc_eof() - in case we need alloc a page for crc_eof
849  * @skb: the skb to be xmitted
850  * @tlen: total len
851  *
852  * Returns: 0 for success
853  */
854 static int fcoe_get_paged_crc_eof(struct sk_buff *skb, int tlen)
855 {
856         struct fcoe_percpu_s *fps;
857         struct page *page;
858
859         fps = &get_cpu_var(fcoe_percpu);
860         page = fps->crc_eof_page;
861         if (!page) {
862                 page = alloc_page(GFP_ATOMIC);
863                 if (!page) {
864                         put_cpu_var(fcoe_percpu);
865                         return -ENOMEM;
866                 }
867                 fps->crc_eof_page = page;
868                 fps->crc_eof_offset = 0;
869         }
870
871         get_page(page);
872         skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags, page,
873                            fps->crc_eof_offset, tlen);
874         skb->len += tlen;
875         skb->data_len += tlen;
876         skb->truesize += tlen;
877         fps->crc_eof_offset += sizeof(struct fcoe_crc_eof);
878
879         if (fps->crc_eof_offset >= PAGE_SIZE) {
880                 fps->crc_eof_page = NULL;
881                 fps->crc_eof_offset = 0;
882                 put_page(page);
883         }
884         put_cpu_var(fcoe_percpu);
885         return 0;
886 }
887
888 /**
889  * fcoe_fc_crc() - calculates FC CRC in this fcoe skb
890  * @fp: the fc_frame containg data to be checksummed
891  *
892  * This uses crc32() to calculate the crc for fc frame
893  * Return   : 32 bit crc
894  */
895 u32 fcoe_fc_crc(struct fc_frame *fp)
896 {
897         struct sk_buff *skb = fp_skb(fp);
898         struct skb_frag_struct *frag;
899         unsigned char *data;
900         unsigned long off, len, clen;
901         u32 crc;
902         unsigned i;
903
904         crc = crc32(~0, skb->data, skb_headlen(skb));
905
906         for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
907                 frag = &skb_shinfo(skb)->frags[i];
908                 off = frag->page_offset;
909                 len = frag->size;
910                 while (len > 0) {
911                         clen = min(len, PAGE_SIZE - (off & ~PAGE_MASK));
912                         data = kmap_atomic(frag->page + (off >> PAGE_SHIFT),
913                                            KM_SKB_DATA_SOFTIRQ);
914                         crc = crc32(crc, data + (off & ~PAGE_MASK), clen);
915                         kunmap_atomic(data, KM_SKB_DATA_SOFTIRQ);
916                         off += clen;
917                         len -= clen;
918                 }
919         }
920         return crc;
921 }
922 EXPORT_SYMBOL_GPL(fcoe_fc_crc);
923
924 /**
925  * fcoe_xmit() - FCoE frame transmit function
926  * @lp: the associated local port
927  * @fp: the fc_frame to be transmitted
928  *
929  * Return   : 0 for success
930  */
931 int fcoe_xmit(struct fc_lport *lp, struct fc_frame *fp)
932 {
933         int wlen, rc = 0;
934         u32 crc;
935         struct ethhdr *eh;
936         struct fcoe_crc_eof *cp;
937         struct sk_buff *skb;
938         struct fcoe_dev_stats *stats;
939         struct fc_frame_header *fh;
940         unsigned int hlen;              /* header length implies the version */
941         unsigned int tlen;              /* trailer length */
942         unsigned int elen;              /* eth header, may include vlan */
943         int flogi_in_progress = 0;
944         struct fcoe_softc *fc;
945         u8 sof, eof;
946         struct fcoe_hdr *hp;
947
948         WARN_ON((fr_len(fp) % sizeof(u32)) != 0);
949
950         fc = lport_priv(lp);
951         /*
952          * if it is a flogi then we need to learn gw-addr
953          * and my own fcid
954          */
955         fh = fc_frame_header_get(fp);
956         if (unlikely(fh->fh_r_ctl == FC_RCTL_ELS_REQ)) {
957                 if (fc_frame_payload_op(fp) == ELS_FLOGI) {
958                         fc->flogi_oxid = ntohs(fh->fh_ox_id);
959                         fc->address_mode = FCOE_FCOUI_ADDR_MODE;
960                         fc->flogi_progress = 1;
961                         flogi_in_progress = 1;
962                 } else if (fc->flogi_progress && ntoh24(fh->fh_s_id) != 0) {
963                         /*
964                          * Here we must've gotten an SID by accepting an FLOGI
965                          * from a point-to-point connection.  Switch to using
966                          * the source mac based on the SID.  The destination
967                          * MAC in this case would have been set by receving the
968                          * FLOGI.
969                          */
970                         fc_fcoe_set_mac(fc->data_src_addr, fh->fh_s_id);
971                         fc->flogi_progress = 0;
972                 }
973         }
974
975         skb = fp_skb(fp);
976         sof = fr_sof(fp);
977         eof = fr_eof(fp);
978
979         elen = (fc->real_dev->priv_flags & IFF_802_1Q_VLAN) ?
980                 sizeof(struct vlan_ethhdr) : sizeof(struct ethhdr);
981         hlen = sizeof(struct fcoe_hdr);
982         tlen = sizeof(struct fcoe_crc_eof);
983         wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
984
985         /* crc offload */
986         if (likely(lp->crc_offload)) {
987                 skb->ip_summed = CHECKSUM_PARTIAL;
988                 skb->csum_start = skb_headroom(skb);
989                 skb->csum_offset = skb->len;
990                 crc = 0;
991         } else {
992                 skb->ip_summed = CHECKSUM_NONE;
993                 crc = fcoe_fc_crc(fp);
994         }
995
996         /* copy fc crc and eof to the skb buff */
997         if (skb_is_nonlinear(skb)) {
998                 skb_frag_t *frag;
999                 if (fcoe_get_paged_crc_eof(skb, tlen)) {
1000                         kfree_skb(skb);
1001                         return -ENOMEM;
1002                 }
1003                 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
1004                 cp = kmap_atomic(frag->page, KM_SKB_DATA_SOFTIRQ)
1005                         + frag->page_offset;
1006         } else {
1007                 cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
1008         }
1009
1010         memset(cp, 0, sizeof(*cp));
1011         cp->fcoe_eof = eof;
1012         cp->fcoe_crc32 = cpu_to_le32(~crc);
1013
1014         if (skb_is_nonlinear(skb)) {
1015                 kunmap_atomic(cp, KM_SKB_DATA_SOFTIRQ);
1016                 cp = NULL;
1017         }
1018
1019         /* adjust skb netowrk/transport offsets to match mac/fcoe/fc */
1020         skb_push(skb, elen + hlen);
1021         skb_reset_mac_header(skb);
1022         skb_reset_network_header(skb);
1023         skb->mac_len = elen;
1024         skb->protocol = htons(ETH_P_FCOE);
1025         skb->dev = fc->real_dev;
1026
1027         /* fill up mac and fcoe headers */
1028         eh = eth_hdr(skb);
1029         eh->h_proto = htons(ETH_P_FCOE);
1030         if (fc->address_mode == FCOE_FCOUI_ADDR_MODE)
1031                 fc_fcoe_set_mac(eh->h_dest, fh->fh_d_id);
1032         else
1033                 /* insert GW address */
1034                 memcpy(eh->h_dest, fc->dest_addr, ETH_ALEN);
1035
1036         if (unlikely(flogi_in_progress))
1037                 memcpy(eh->h_source, fc->ctl_src_addr, ETH_ALEN);
1038         else
1039                 memcpy(eh->h_source, fc->data_src_addr, ETH_ALEN);
1040
1041         hp = (struct fcoe_hdr *)(eh + 1);
1042         memset(hp, 0, sizeof(*hp));
1043         if (FC_FCOE_VER)
1044                 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
1045         hp->fcoe_sof = sof;
1046
1047 #ifdef NETIF_F_FSO
1048         /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
1049         if (lp->seq_offload && fr_max_payload(fp)) {
1050                 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
1051                 skb_shinfo(skb)->gso_size = fr_max_payload(fp);
1052         } else {
1053                 skb_shinfo(skb)->gso_type = 0;
1054                 skb_shinfo(skb)->gso_size = 0;
1055         }
1056 #endif
1057         /* update tx stats: regardless if LLD fails */
1058         stats = fc_lport_get_stats(lp);
1059         stats->TxFrames++;
1060         stats->TxWords += wlen;
1061
1062         /* send down to lld */
1063         fr_dev(fp) = lp;
1064         if (fc->fcoe_pending_queue.qlen)
1065                 rc = fcoe_check_wait_queue(lp);
1066
1067         if (rc == 0)
1068                 rc = fcoe_start_io(skb);
1069
1070         if (rc) {
1071                 spin_lock_bh(&fc->fcoe_pending_queue.lock);
1072                 __skb_queue_tail(&fc->fcoe_pending_queue, skb);
1073                 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
1074                 if (fc->fcoe_pending_queue.qlen > FCOE_MAX_QUEUE_DEPTH)
1075                         lp->qfull = 1;
1076         }
1077
1078         return 0;
1079 }
1080 EXPORT_SYMBOL_GPL(fcoe_xmit);
1081
1082 /**
1083  * fcoe_percpu_receive_thread() - recv thread per cpu
1084  * @arg: ptr to the fcoe per cpu struct
1085  *
1086  * Return: 0 for success
1087  */
1088 int fcoe_percpu_receive_thread(void *arg)
1089 {
1090         struct fcoe_percpu_s *p = arg;
1091         u32 fr_len;
1092         struct fc_lport *lp;
1093         struct fcoe_rcv_info *fr;
1094         struct fcoe_dev_stats *stats;
1095         struct fc_frame_header *fh;
1096         struct sk_buff *skb;
1097         struct fcoe_crc_eof crc_eof;
1098         struct fc_frame *fp;
1099         u8 *mac = NULL;
1100         struct fcoe_softc *fc;
1101         struct fcoe_hdr *hp;
1102
1103         set_user_nice(current, -20);
1104
1105         while (!kthread_should_stop()) {
1106
1107                 spin_lock_bh(&p->fcoe_rx_list.lock);
1108                 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) == NULL) {
1109                         set_current_state(TASK_INTERRUPTIBLE);
1110                         spin_unlock_bh(&p->fcoe_rx_list.lock);
1111                         schedule();
1112                         set_current_state(TASK_RUNNING);
1113                         if (kthread_should_stop())
1114                                 return 0;
1115                         spin_lock_bh(&p->fcoe_rx_list.lock);
1116                 }
1117                 spin_unlock_bh(&p->fcoe_rx_list.lock);
1118                 fr = fcoe_dev_from_skb(skb);
1119                 lp = fr->fr_dev;
1120                 if (unlikely(lp == NULL)) {
1121                         FC_DBG("invalid HBA Structure");
1122                         kfree_skb(skb);
1123                         continue;
1124                 }
1125
1126                 if (unlikely(debug_fcoe)) {
1127                         FC_DBG("skb_info: len:%d data_len:%d head:%p data:%p "
1128                                "tail:%p end:%p sum:%d dev:%s",
1129                                skb->len, skb->data_len,
1130                                skb->head, skb->data, skb_tail_pointer(skb),
1131                                skb_end_pointer(skb), skb->csum,
1132                                skb->dev ? skb->dev->name : "<NULL>");
1133                 }
1134
1135                 /*
1136                  * Save source MAC address before discarding header.
1137                  */
1138                 fc = lport_priv(lp);
1139                 if (unlikely(fc->flogi_progress))
1140                         mac = eth_hdr(skb)->h_source;
1141
1142                 if (skb_is_nonlinear(skb))
1143                         skb_linearize(skb);     /* not ideal */
1144
1145                 /*
1146                  * Frame length checks and setting up the header pointers
1147                  * was done in fcoe_rcv already.
1148                  */
1149                 hp = (struct fcoe_hdr *) skb_network_header(skb);
1150                 fh = (struct fc_frame_header *) skb_transport_header(skb);
1151
1152                 stats = fc_lport_get_stats(lp);
1153                 if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) {
1154                         if (stats->ErrorFrames < 5)
1155                                 printk(KERN_WARNING "FCoE version "
1156                                        "mismatch: The frame has "
1157                                        "version %x, but the "
1158                                        "initiator supports version "
1159                                        "%x\n", FC_FCOE_DECAPS_VER(hp),
1160                                        FC_FCOE_VER);
1161                         stats->ErrorFrames++;
1162                         kfree_skb(skb);
1163                         continue;
1164                 }
1165
1166                 skb_pull(skb, sizeof(struct fcoe_hdr));
1167                 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
1168
1169                 stats->RxFrames++;
1170                 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
1171
1172                 fp = (struct fc_frame *)skb;
1173                 fc_frame_init(fp);
1174                 fr_dev(fp) = lp;
1175                 fr_sof(fp) = hp->fcoe_sof;
1176
1177                 /* Copy out the CRC and EOF trailer for access */
1178                 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof))) {
1179                         kfree_skb(skb);
1180                         continue;
1181                 }
1182                 fr_eof(fp) = crc_eof.fcoe_eof;
1183                 fr_crc(fp) = crc_eof.fcoe_crc32;
1184                 if (pskb_trim(skb, fr_len)) {
1185                         kfree_skb(skb);
1186                         continue;
1187                 }
1188
1189                 /*
1190                  * We only check CRC if no offload is available and if it is
1191                  * it's solicited data, in which case, the FCP layer would
1192                  * check it during the copy.
1193                  */
1194                 if (lp->crc_offload && skb->ip_summed == CHECKSUM_UNNECESSARY)
1195                         fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1196                 else
1197                         fr_flags(fp) |= FCPHF_CRC_UNCHECKED;
1198
1199                 fh = fc_frame_header_get(fp);
1200                 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA &&
1201                     fh->fh_type == FC_TYPE_FCP) {
1202                         fc_exch_recv(lp, lp->emp, fp);
1203                         continue;
1204                 }
1205                 if (fr_flags(fp) & FCPHF_CRC_UNCHECKED) {
1206                         if (le32_to_cpu(fr_crc(fp)) !=
1207                             ~crc32(~0, skb->data, fr_len)) {
1208                                 if (debug_fcoe || stats->InvalidCRCCount < 5)
1209                                         printk(KERN_WARNING "fcoe: dropping "
1210                                                "frame with CRC error\n");
1211                                 stats->InvalidCRCCount++;
1212                                 stats->ErrorFrames++;
1213                                 fc_frame_free(fp);
1214                                 continue;
1215                         }
1216                         fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1217                 }
1218                 /* non flogi and non data exchanges are handled here */
1219                 if (unlikely(fc->flogi_progress))
1220                         fcoe_recv_flogi(fc, fp, mac);
1221                 fc_exch_recv(lp, lp->emp, fp);
1222         }
1223         return 0;
1224 }
1225
1226 /**
1227  * fcoe_recv_flogi() - flogi receive function
1228  * @fc: associated fcoe_softc
1229  * @fp: the recieved frame
1230  * @sa: the source address of this flogi
1231  *
1232  * This is responsible to parse the flogi response and sets the corresponding
1233  * mac address for the initiator, eitehr OUI based or GW based.
1234  *
1235  * Returns: none
1236  */
1237 static void fcoe_recv_flogi(struct fcoe_softc *fc, struct fc_frame *fp, u8 *sa)
1238 {
1239         struct fc_frame_header *fh;
1240         u8 op;
1241
1242         fh = fc_frame_header_get(fp);
1243         if (fh->fh_type != FC_TYPE_ELS)
1244                 return;
1245         op = fc_frame_payload_op(fp);
1246         if (op == ELS_LS_ACC && fh->fh_r_ctl == FC_RCTL_ELS_REP &&
1247             fc->flogi_oxid == ntohs(fh->fh_ox_id)) {
1248                 /*
1249                  * FLOGI accepted.
1250                  * If the src mac addr is FC_OUI-based, then we mark the
1251                  * address_mode flag to use FC_OUI-based Ethernet DA.
1252                  * Otherwise we use the FCoE gateway addr
1253                  */
1254                 if (!compare_ether_addr(sa, (u8[6]) FC_FCOE_FLOGI_MAC)) {
1255                         fc->address_mode = FCOE_FCOUI_ADDR_MODE;
1256                 } else {
1257                         memcpy(fc->dest_addr, sa, ETH_ALEN);
1258                         fc->address_mode = FCOE_GW_ADDR_MODE;
1259                 }
1260
1261                 /*
1262                  * Remove any previously-set unicast MAC filter.
1263                  * Add secondary FCoE MAC address filter for our OUI.
1264                  */
1265                 rtnl_lock();
1266                 if (compare_ether_addr(fc->data_src_addr, (u8[6]) { 0 }))
1267                         dev_unicast_delete(fc->real_dev, fc->data_src_addr,
1268                                            ETH_ALEN);
1269                 fc_fcoe_set_mac(fc->data_src_addr, fh->fh_d_id);
1270                 dev_unicast_add(fc->real_dev, fc->data_src_addr, ETH_ALEN);
1271                 rtnl_unlock();
1272
1273                 fc->flogi_progress = 0;
1274         } else if (op == ELS_FLOGI && fh->fh_r_ctl == FC_RCTL_ELS_REQ && sa) {
1275                 /*
1276                  * Save source MAC for point-to-point responses.
1277                  */
1278                 memcpy(fc->dest_addr, sa, ETH_ALEN);
1279                 fc->address_mode = FCOE_GW_ADDR_MODE;
1280         }
1281 }
1282
1283 /**
1284  * fcoe_watchdog() - fcoe timer callback
1285  * @vp:
1286  *
1287  * This checks the pending queue length for fcoe and set lport qfull
1288  * if the FCOE_MAX_QUEUE_DEPTH is reached. This is done for all fc_lport on the
1289  * fcoe_hostlist.
1290  *
1291  * Returns: 0 for success
1292  */
1293 void fcoe_watchdog(ulong vp)
1294 {
1295         struct fcoe_softc *fc;
1296
1297         read_lock(&fcoe_hostlist_lock);
1298         list_for_each_entry(fc, &fcoe_hostlist, list) {
1299                 if (fc->lp)
1300                         fcoe_check_wait_queue(fc->lp);
1301         }
1302         read_unlock(&fcoe_hostlist_lock);
1303
1304         fcoe_timer.expires = jiffies + (1 * HZ);
1305         add_timer(&fcoe_timer);
1306 }
1307
1308
1309 /**
1310  * fcoe_check_wait_queue() - put the skb into fcoe pending xmit queue
1311  * @lp: the fc_port for this skb
1312  * @skb: the associated skb to be xmitted
1313  *
1314  * This empties the wait_queue, dequeue the head of the wait_queue queue
1315  * and calls fcoe_start_io() for each packet, if all skb have been
1316  * transmitted, return qlen or -1 if a error occurs, then restore
1317  * wait_queue and  try again later.
1318  *
1319  * The wait_queue is used when the skb transmit fails. skb will go
1320  * in the wait_queue which will be emptied by the time function OR
1321  * by the next skb transmit.
1322  *
1323  * Returns: 0 for success
1324  */
1325 static int fcoe_check_wait_queue(struct fc_lport *lp)
1326 {
1327         struct fcoe_softc *fc = lport_priv(lp);
1328         struct sk_buff *skb;
1329         int rc = -1;
1330
1331         spin_lock_bh(&fc->fcoe_pending_queue.lock);
1332         if (fc->fcoe_pending_queue_active)
1333                 goto out;
1334         fc->fcoe_pending_queue_active = 1;
1335
1336         while (fc->fcoe_pending_queue.qlen) {
1337                 /* keep qlen > 0 until fcoe_start_io succeeds */
1338                 fc->fcoe_pending_queue.qlen++;
1339                 skb = __skb_dequeue(&fc->fcoe_pending_queue);
1340
1341                 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
1342                 rc = fcoe_start_io(skb);
1343                 spin_lock_bh(&fc->fcoe_pending_queue.lock);
1344
1345                 if (rc) {
1346                         __skb_queue_head(&fc->fcoe_pending_queue, skb);
1347                         /* undo temporary increment above */
1348                         fc->fcoe_pending_queue.qlen--;
1349                         break;
1350                 }
1351                 /* undo temporary increment above */
1352                 fc->fcoe_pending_queue.qlen--;
1353         }
1354
1355         if (fc->fcoe_pending_queue.qlen < FCOE_LOW_QUEUE_DEPTH)
1356                 lp->qfull = 0;
1357         fc->fcoe_pending_queue_active = 0;
1358         rc = fc->fcoe_pending_queue.qlen;
1359 out:
1360         spin_unlock_bh(&fc->fcoe_pending_queue.lock);
1361         return rc;
1362 }
1363
1364 /**
1365  * fcoe_dev_setup() - setup link change notification interface
1366  */
1367 static void fcoe_dev_setup()
1368 {
1369         /*
1370          * here setup a interface specific wd time to
1371          * monitor the link state
1372          */
1373         register_netdevice_notifier(&fcoe_notifier);
1374 }
1375
1376 /**
1377  * fcoe_dev_setup() - cleanup link change notification interface
1378  */
1379 static void fcoe_dev_cleanup(void)
1380 {
1381         unregister_netdevice_notifier(&fcoe_notifier);
1382 }
1383
1384 /**
1385  * fcoe_device_notification() - netdev event notification callback
1386  * @notifier: context of the notification
1387  * @event: type of event
1388  * @ptr: fixed array for output parsed ifname
1389  *
1390  * This function is called by the ethernet driver in case of link change event
1391  *
1392  * Returns: 0 for success
1393  */
1394 static int fcoe_device_notification(struct notifier_block *notifier,
1395                                     ulong event, void *ptr)
1396 {
1397         struct fc_lport *lp = NULL;
1398         struct net_device *real_dev = ptr;
1399         struct fcoe_softc *fc;
1400         struct fcoe_dev_stats *stats;
1401         u32 new_link_up;
1402         u32 mfs;
1403         int rc = NOTIFY_OK;
1404
1405         read_lock(&fcoe_hostlist_lock);
1406         list_for_each_entry(fc, &fcoe_hostlist, list) {
1407                 if (fc->real_dev == real_dev) {
1408                         lp = fc->lp;
1409                         break;
1410                 }
1411         }
1412         read_unlock(&fcoe_hostlist_lock);
1413         if (lp == NULL) {
1414                 rc = NOTIFY_DONE;
1415                 goto out;
1416         }
1417
1418         new_link_up = lp->link_up;
1419         switch (event) {
1420         case NETDEV_DOWN:
1421         case NETDEV_GOING_DOWN:
1422                 new_link_up = 0;
1423                 break;
1424         case NETDEV_UP:
1425         case NETDEV_CHANGE:
1426                 new_link_up = !fcoe_link_ok(lp);
1427                 break;
1428         case NETDEV_CHANGEMTU:
1429                 mfs = fc->real_dev->mtu -
1430                         (sizeof(struct fcoe_hdr) +
1431                          sizeof(struct fcoe_crc_eof));
1432                 if (mfs >= FC_MIN_MAX_FRAME)
1433                         fc_set_mfs(lp, mfs);
1434                 new_link_up = !fcoe_link_ok(lp);
1435                 break;
1436         case NETDEV_REGISTER:
1437                 break;
1438         default:
1439                 FC_DBG("unknown event %ld call", event);
1440         }
1441         if (lp->link_up != new_link_up) {
1442                 if (new_link_up)
1443                         fc_linkup(lp);
1444                 else {
1445                         stats = fc_lport_get_stats(lp);
1446                         stats->LinkFailureCount++;
1447                         fc_linkdown(lp);
1448                         fcoe_clean_pending_queue(lp);
1449                 }
1450         }
1451 out:
1452         return rc;
1453 }
1454
1455 /**
1456  * fcoe_if_to_netdev() - parse a name buffer to get netdev
1457  * @ifname: fixed array for output parsed ifname
1458  * @buffer: incoming buffer to be copied
1459  *
1460  * Returns: NULL or ptr to netdeive
1461  */
1462 static struct net_device *fcoe_if_to_netdev(const char *buffer)
1463 {
1464         char *cp;
1465         char ifname[IFNAMSIZ + 2];
1466
1467         if (buffer) {
1468                 strlcpy(ifname, buffer, IFNAMSIZ);
1469                 cp = ifname + strlen(ifname);
1470                 while (--cp >= ifname && *cp == '\n')
1471                         *cp = '\0';
1472                 return dev_get_by_name(&init_net, ifname);
1473         }
1474         return NULL;
1475 }
1476
1477 /**
1478  * fcoe_netdev_to_module_owner() - finds out the nic drive moddule of the netdev
1479  * @netdev: the target netdev
1480  *
1481  * Returns: ptr to the struct module, NULL for failure
1482  */
1483 static struct module *
1484 fcoe_netdev_to_module_owner(const struct net_device *netdev)
1485 {
1486         struct device *dev;
1487
1488         if (!netdev)
1489                 return NULL;
1490
1491         dev = netdev->dev.parent;
1492         if (!dev)
1493                 return NULL;
1494
1495         if (!dev->driver)
1496                 return NULL;
1497
1498         return dev->driver->owner;
1499 }
1500
1501 /**
1502  * fcoe_ethdrv_get() - Hold the Ethernet driver
1503  * @netdev: the target netdev
1504  *
1505  * Holds the Ethernet driver module by try_module_get() for
1506  * the corresponding netdev.
1507  *
1508  * Returns: 0 for succsss
1509  */
1510 static int fcoe_ethdrv_get(const struct net_device *netdev)
1511 {
1512         struct module *owner;
1513
1514         owner = fcoe_netdev_to_module_owner(netdev);
1515         if (owner) {
1516                 printk(KERN_DEBUG "fcoe:hold driver module %s for %s\n",
1517                        module_name(owner), netdev->name);
1518                 return  try_module_get(owner);
1519         }
1520         return -ENODEV;
1521 }
1522
1523 /**
1524  * fcoe_ethdrv_put() - Release the Ethernet driver
1525  * @netdev: the target netdev
1526  *
1527  * Releases the Ethernet driver module by module_put for
1528  * the corresponding netdev.
1529  *
1530  * Returns: 0 for succsss
1531  */
1532 static int fcoe_ethdrv_put(const struct net_device *netdev)
1533 {
1534         struct module *owner;
1535
1536         owner = fcoe_netdev_to_module_owner(netdev);
1537         if (owner) {
1538                 printk(KERN_DEBUG "fcoe:release driver module %s for %s\n",
1539                        module_name(owner), netdev->name);
1540                 module_put(owner);
1541                 return 0;
1542         }
1543         return -ENODEV;
1544 }
1545
1546 /**
1547  * fcoe_destroy() - handles the destroy from sysfs
1548  * @buffer: expcted to be a eth if name
1549  * @kp: associated kernel param
1550  *
1551  * Returns: 0 for success
1552  */
1553 static int fcoe_destroy(const char *buffer, struct kernel_param *kp)
1554 {
1555         int rc;
1556         struct net_device *netdev;
1557
1558         netdev = fcoe_if_to_netdev(buffer);
1559         if (!netdev) {
1560                 rc = -ENODEV;
1561                 goto out_nodev;
1562         }
1563         /* look for existing lport */
1564         if (!fcoe_hostlist_lookup(netdev)) {
1565                 rc = -ENODEV;
1566                 goto out_putdev;
1567         }
1568         rc = fcoe_if_destroy(netdev);
1569         if (rc) {
1570                 printk(KERN_ERR "fcoe: fcoe_if_destroy(%s) failed\n",
1571                        netdev->name);
1572                 rc = -EIO;
1573                 goto out_putdev;
1574         }
1575         fcoe_ethdrv_put(netdev);
1576         rc = 0;
1577 out_putdev:
1578         dev_put(netdev);
1579 out_nodev:
1580         return rc;
1581 }
1582
1583 /**
1584  * fcoe_create() - Handles the create call from sysfs
1585  * @buffer: expcted to be a eth if name
1586  * @kp: associated kernel param
1587  *
1588  * Returns: 0 for success
1589  */
1590 static int fcoe_create(const char *buffer, struct kernel_param *kp)
1591 {
1592         int rc;
1593         struct net_device *netdev;
1594
1595         netdev = fcoe_if_to_netdev(buffer);
1596         if (!netdev) {
1597                 rc = -ENODEV;
1598                 goto out_nodev;
1599         }
1600         /* look for existing lport */
1601         if (fcoe_hostlist_lookup(netdev)) {
1602                 rc = -EEXIST;
1603                 goto out_putdev;
1604         }
1605         fcoe_ethdrv_get(netdev);
1606
1607         rc = fcoe_if_create(netdev);
1608         if (rc) {
1609                 printk(KERN_ERR "fcoe: fcoe_if_create(%s) failed\n",
1610                        netdev->name);
1611                 fcoe_ethdrv_put(netdev);
1612                 rc = -EIO;
1613                 goto out_putdev;
1614         }
1615         rc = 0;
1616 out_putdev:
1617         dev_put(netdev);
1618 out_nodev:
1619         return rc;
1620 }
1621
1622 module_param_call(create, fcoe_create, NULL, NULL, S_IWUSR);
1623 __MODULE_PARM_TYPE(create, "string");
1624 MODULE_PARM_DESC(create, "Create fcoe port using net device passed in.");
1625 module_param_call(destroy, fcoe_destroy, NULL, NULL, S_IWUSR);
1626 __MODULE_PARM_TYPE(destroy, "string");
1627 MODULE_PARM_DESC(destroy, "Destroy fcoe port");
1628
1629 /**
1630  * fcoe_link_ok() - Check if link is ok for the fc_lport
1631  * @lp: ptr to the fc_lport
1632  *
1633  * Any permanently-disqualifying conditions have been previously checked.
1634  * This also updates the speed setting, which may change with link for 100/1000.
1635  *
1636  * This function should probably be checking for PAUSE support at some point
1637  * in the future. Currently Per-priority-pause is not determinable using
1638  * ethtool, so we shouldn't be restrictive until that problem is resolved.
1639  *
1640  * Returns: 0 if link is OK for use by FCoE.
1641  *
1642  */
1643 int fcoe_link_ok(struct fc_lport *lp)
1644 {
1645         struct fcoe_softc *fc = lport_priv(lp);
1646         struct net_device *dev = fc->real_dev;
1647         struct ethtool_cmd ecmd = { ETHTOOL_GSET };
1648         int rc = 0;
1649
1650         if ((dev->flags & IFF_UP) && netif_carrier_ok(dev)) {
1651                 dev = fc->phys_dev;
1652                 if (dev->ethtool_ops->get_settings) {
1653                         dev->ethtool_ops->get_settings(dev, &ecmd);
1654                         lp->link_supported_speeds &=
1655                                 ~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
1656                         if (ecmd.supported & (SUPPORTED_1000baseT_Half |
1657                                               SUPPORTED_1000baseT_Full))
1658                                 lp->link_supported_speeds |= FC_PORTSPEED_1GBIT;
1659                         if (ecmd.supported & SUPPORTED_10000baseT_Full)
1660                                 lp->link_supported_speeds |=
1661                                         FC_PORTSPEED_10GBIT;
1662                         if (ecmd.speed == SPEED_1000)
1663                                 lp->link_speed = FC_PORTSPEED_1GBIT;
1664                         if (ecmd.speed == SPEED_10000)
1665                                 lp->link_speed = FC_PORTSPEED_10GBIT;
1666                 }
1667         } else
1668                 rc = -1;
1669
1670         return rc;
1671 }
1672 EXPORT_SYMBOL_GPL(fcoe_link_ok);
1673
1674 /**
1675  * fcoe_percpu_clean() - Clear the pending skbs for an lport
1676  * @lp: the fc_lport
1677  */
1678 void fcoe_percpu_clean(struct fc_lport *lp)
1679 {
1680         struct fcoe_percpu_s *pp;
1681         struct fcoe_rcv_info *fr;
1682         struct sk_buff_head *list;
1683         struct sk_buff *skb, *next;
1684         struct sk_buff *head;
1685         unsigned int cpu;
1686
1687         for_each_possible_cpu(cpu) {
1688                 pp = &per_cpu(fcoe_percpu, cpu);
1689                 spin_lock_bh(&pp->fcoe_rx_list.lock);
1690                 list = &pp->fcoe_rx_list;
1691                 head = list->next;
1692                 for (skb = head; skb != (struct sk_buff *)list;
1693                      skb = next) {
1694                         next = skb->next;
1695                         fr = fcoe_dev_from_skb(skb);
1696                         if (fr->fr_dev == lp) {
1697                                 __skb_unlink(skb, list);
1698                                 kfree_skb(skb);
1699                         }
1700                 }
1701                 spin_unlock_bh(&pp->fcoe_rx_list.lock);
1702         }
1703 }
1704 EXPORT_SYMBOL_GPL(fcoe_percpu_clean);
1705
1706 /**
1707  * fcoe_clean_pending_queue() - Dequeue a skb and free it
1708  * @lp: the corresponding fc_lport
1709  *
1710  * Returns: none
1711  */
1712 void fcoe_clean_pending_queue(struct fc_lport *lp)
1713 {
1714         struct fcoe_softc  *fc = lport_priv(lp);
1715         struct sk_buff *skb;
1716
1717         spin_lock_bh(&fc->fcoe_pending_queue.lock);
1718         while ((skb = __skb_dequeue(&fc->fcoe_pending_queue)) != NULL) {
1719                 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
1720                 kfree_skb(skb);
1721                 spin_lock_bh(&fc->fcoe_pending_queue.lock);
1722         }
1723         spin_unlock_bh(&fc->fcoe_pending_queue.lock);
1724 }
1725 EXPORT_SYMBOL_GPL(fcoe_clean_pending_queue);
1726
1727 /**
1728  * libfc_host_alloc() - Allocate a Scsi_Host with room for the fc_lport
1729  * @sht: ptr to the scsi host templ
1730  * @priv_size: size of private data after fc_lport
1731  *
1732  * Returns: ptr to Scsi_Host
1733  * TODO: to libfc?
1734  */
1735 static inline struct Scsi_Host *
1736 libfc_host_alloc(struct scsi_host_template *sht, int priv_size)
1737 {
1738         return scsi_host_alloc(sht, sizeof(struct fc_lport) + priv_size);
1739 }
1740
1741 /**
1742  * fcoe_host_alloc() - Allocate a Scsi_Host with room for the fcoe_softc
1743  * @sht: ptr to the scsi host templ
1744  * @priv_size: size of private data after fc_lport
1745  *
1746  * Returns: ptr to Scsi_Host
1747  */
1748 struct Scsi_Host *fcoe_host_alloc(struct scsi_host_template *sht, int priv_size)
1749 {
1750         return libfc_host_alloc(sht, sizeof(struct fcoe_softc) + priv_size);
1751 }
1752 EXPORT_SYMBOL_GPL(fcoe_host_alloc);
1753
1754 /**
1755  * fcoe_reset() - Resets the fcoe
1756  * @shost: shost the reset is from
1757  *
1758  * Returns: always 0
1759  */
1760 int fcoe_reset(struct Scsi_Host *shost)
1761 {
1762         struct fc_lport *lport = shost_priv(shost);
1763         fc_lport_reset(lport);
1764         return 0;
1765 }
1766 EXPORT_SYMBOL_GPL(fcoe_reset);
1767
1768 /**
1769  * fcoe_hostlist_lookup_softc() - find the corresponding lport by a given device
1770  * @device: this is currently ptr to net_device
1771  *
1772  * Returns: NULL or the located fcoe_softc
1773  */
1774 static struct fcoe_softc *
1775 fcoe_hostlist_lookup_softc(const struct net_device *dev)
1776 {
1777         struct fcoe_softc *fc;
1778
1779         read_lock(&fcoe_hostlist_lock);
1780         list_for_each_entry(fc, &fcoe_hostlist, list) {
1781                 if (fc->real_dev == dev) {
1782                         read_unlock(&fcoe_hostlist_lock);
1783                         return fc;
1784                 }
1785         }
1786         read_unlock(&fcoe_hostlist_lock);
1787         return NULL;
1788 }
1789
1790 /**
1791  * fcoe_hostlist_lookup() - Find the corresponding lport by netdev
1792  * @netdev: ptr to net_device
1793  *
1794  * Returns: 0 for success
1795  */
1796 struct fc_lport *fcoe_hostlist_lookup(const struct net_device *netdev)
1797 {
1798         struct fcoe_softc *fc;
1799
1800         fc = fcoe_hostlist_lookup_softc(netdev);
1801
1802         return (fc) ? fc->lp : NULL;
1803 }
1804 EXPORT_SYMBOL_GPL(fcoe_hostlist_lookup);
1805
1806 /**
1807  * fcoe_hostlist_add() - Add a lport to lports list
1808  * @lp: ptr to the fc_lport to badded
1809  *
1810  * Returns: 0 for success
1811  */
1812 int fcoe_hostlist_add(const struct fc_lport *lp)
1813 {
1814         struct fcoe_softc *fc;
1815
1816         fc = fcoe_hostlist_lookup_softc(fcoe_netdev(lp));
1817         if (!fc) {
1818                 fc = lport_priv(lp);
1819                 write_lock_bh(&fcoe_hostlist_lock);
1820                 list_add_tail(&fc->list, &fcoe_hostlist);
1821                 write_unlock_bh(&fcoe_hostlist_lock);
1822         }
1823         return 0;
1824 }
1825 EXPORT_SYMBOL_GPL(fcoe_hostlist_add);
1826
1827 /**
1828  * fcoe_hostlist_remove() - remove a lport from lports list
1829  * @lp: ptr to the fc_lport to badded
1830  *
1831  * Returns: 0 for success
1832  */
1833 int fcoe_hostlist_remove(const struct fc_lport *lp)
1834 {
1835         struct fcoe_softc *fc;
1836
1837         fc = fcoe_hostlist_lookup_softc(fcoe_netdev(lp));
1838         BUG_ON(!fc);
1839         write_lock_bh(&fcoe_hostlist_lock);
1840         list_del(&fc->list);
1841         write_unlock_bh(&fcoe_hostlist_lock);
1842
1843         return 0;
1844 }
1845 EXPORT_SYMBOL_GPL(fcoe_hostlist_remove);
1846
1847 /**
1848  * fcoe_init() - fcoe module loading initialization
1849  *
1850  * Returns 0 on success, negative on failure
1851  */
1852 static int __init fcoe_init(void)
1853 {
1854         unsigned int cpu;
1855         int rc = 0;
1856         struct fcoe_percpu_s *p;
1857
1858         INIT_LIST_HEAD(&fcoe_hostlist);
1859         rwlock_init(&fcoe_hostlist_lock);
1860
1861         for_each_possible_cpu(cpu) {
1862                 p = &per_cpu(fcoe_percpu, cpu);
1863                 skb_queue_head_init(&p->fcoe_rx_list);
1864         }
1865
1866         for_each_online_cpu(cpu)
1867                 fcoe_percpu_thread_create(cpu);
1868
1869         /* Initialize per CPU interrupt thread */
1870         rc = register_hotcpu_notifier(&fcoe_cpu_notifier);
1871         if (rc)
1872                 goto out_free;
1873
1874         /* Setup link change notification */
1875         fcoe_dev_setup();
1876
1877         setup_timer(&fcoe_timer, fcoe_watchdog, 0);
1878
1879         mod_timer(&fcoe_timer, jiffies + (10 * HZ));
1880
1881         fcoe_if_init();
1882
1883         return 0;
1884
1885 out_free:
1886         for_each_online_cpu(cpu) {
1887                 fcoe_percpu_thread_destroy(cpu);
1888         }
1889
1890         return rc;
1891 }
1892 module_init(fcoe_init);
1893
1894 /**
1895  * fcoe_exit() - fcoe module unloading cleanup
1896  *
1897  * Returns 0 on success, negative on failure
1898  */
1899 static void __exit fcoe_exit(void)
1900 {
1901         unsigned int cpu;
1902         struct fcoe_softc *fc, *tmp;
1903
1904         fcoe_dev_cleanup();
1905
1906         /* Stop the timer */
1907         del_timer_sync(&fcoe_timer);
1908
1909         /* releases the associated fcoe hosts */
1910         list_for_each_entry_safe(fc, tmp, &fcoe_hostlist, list)
1911                 fcoe_if_destroy(fc->real_dev);
1912
1913         unregister_hotcpu_notifier(&fcoe_cpu_notifier);
1914
1915         for_each_online_cpu(cpu) {
1916                 fcoe_percpu_thread_destroy(cpu);
1917         }
1918
1919         /* detach from scsi transport */
1920         fcoe_if_exit();
1921 }
1922 module_exit(fcoe_exit);