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