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