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