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