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