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