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