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