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