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