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