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