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