sysfs: Remove support for tagged directories with untagged members (again)
[linux-btrfs-devel.git] / drivers / scsi / fcoe / fcoe.c
bloba1c0ddd53aa9a30132580f531d4d1a79942fd04f
1 /*
2 * Copyright(c) 2007 - 2009 Intel Corporation. All rights reserved.
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.
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.
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.
17 * Maintained at www.Open-FCoE.org
20 #include <linux/module.h>
21 #include <linux/spinlock.h>
22 #include <linux/netdevice.h>
23 #include <linux/etherdevice.h>
24 #include <linux/ethtool.h>
25 #include <linux/if_ether.h>
26 #include <linux/if_vlan.h>
27 #include <linux/crc32.h>
28 #include <linux/slab.h>
29 #include <linux/cpu.h>
30 #include <linux/fs.h>
31 #include <linux/sysfs.h>
32 #include <linux/ctype.h>
33 #include <linux/workqueue.h>
34 #include <scsi/scsi_tcq.h>
35 #include <scsi/scsicam.h>
36 #include <scsi/scsi_transport.h>
37 #include <scsi/scsi_transport_fc.h>
38 #include <net/rtnetlink.h>
40 #include <scsi/fc/fc_encaps.h>
41 #include <scsi/fc/fc_fip.h>
43 #include <scsi/libfc.h>
44 #include <scsi/fc_frame.h>
45 #include <scsi/libfcoe.h>
47 #include "fcoe.h"
49 MODULE_AUTHOR("Open-FCoE.org");
50 MODULE_DESCRIPTION("FCoE");
51 MODULE_LICENSE("GPL v2");
53 /* Performance tuning parameters for fcoe */
54 static unsigned int fcoe_ddp_min;
55 module_param_named(ddp_min, fcoe_ddp_min, uint, S_IRUGO | S_IWUSR);
56 MODULE_PARM_DESC(ddp_min, "Minimum I/O size in bytes for " \
57 "Direct Data Placement (DDP).");
59 DEFINE_MUTEX(fcoe_config_mutex);
61 static struct workqueue_struct *fcoe_wq;
63 /* fcoe_percpu_clean completion. Waiter protected by fcoe_create_mutex */
64 static DECLARE_COMPLETION(fcoe_flush_completion);
66 /* fcoe host list */
67 /* must only by accessed under the RTNL mutex */
68 LIST_HEAD(fcoe_hostlist);
69 DEFINE_PER_CPU(struct fcoe_percpu_s, fcoe_percpu);
71 /* Function Prototypes */
72 static int fcoe_reset(struct Scsi_Host *);
73 static int fcoe_xmit(struct fc_lport *, struct fc_frame *);
74 static int fcoe_rcv(struct sk_buff *, struct net_device *,
75 struct packet_type *, struct net_device *);
76 static int fcoe_percpu_receive_thread(void *);
77 static void fcoe_percpu_clean(struct fc_lport *);
78 static int fcoe_link_speed_update(struct fc_lport *);
79 static int fcoe_link_ok(struct fc_lport *);
81 static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *);
82 static int fcoe_hostlist_add(const struct fc_lport *);
84 static int fcoe_device_notification(struct notifier_block *, ulong, void *);
85 static void fcoe_dev_setup(void);
86 static void fcoe_dev_cleanup(void);
87 static struct fcoe_interface
88 *fcoe_hostlist_lookup_port(const struct net_device *);
90 static int fcoe_fip_recv(struct sk_buff *, struct net_device *,
91 struct packet_type *, struct net_device *);
93 static void fcoe_fip_send(struct fcoe_ctlr *, struct sk_buff *);
94 static void fcoe_update_src_mac(struct fc_lport *, u8 *);
95 static u8 *fcoe_get_src_mac(struct fc_lport *);
96 static void fcoe_destroy_work(struct work_struct *);
98 static int fcoe_ddp_setup(struct fc_lport *, u16, struct scatterlist *,
99 unsigned int);
100 static int fcoe_ddp_done(struct fc_lport *, u16);
101 static int fcoe_ddp_target(struct fc_lport *, u16, struct scatterlist *,
102 unsigned int);
103 static int fcoe_cpu_callback(struct notifier_block *, unsigned long, void *);
105 static bool fcoe_match(struct net_device *netdev);
106 static int fcoe_create(struct net_device *netdev, enum fip_state fip_mode);
107 static int fcoe_destroy(struct net_device *netdev);
108 static int fcoe_enable(struct net_device *netdev);
109 static int fcoe_disable(struct net_device *netdev);
111 static struct fc_seq *fcoe_elsct_send(struct fc_lport *,
112 u32 did, struct fc_frame *,
113 unsigned int op,
114 void (*resp)(struct fc_seq *,
115 struct fc_frame *,
116 void *),
117 void *, u32 timeout);
118 static void fcoe_recv_frame(struct sk_buff *skb);
120 static void fcoe_get_lesb(struct fc_lport *, struct fc_els_lesb *);
122 /* notification function for packets from net device */
123 static struct notifier_block fcoe_notifier = {
124 .notifier_call = fcoe_device_notification,
127 /* notification function for CPU hotplug events */
128 static struct notifier_block fcoe_cpu_notifier = {
129 .notifier_call = fcoe_cpu_callback,
132 static struct scsi_transport_template *fcoe_nport_scsi_transport;
133 static struct scsi_transport_template *fcoe_vport_scsi_transport;
135 static int fcoe_vport_destroy(struct fc_vport *);
136 static int fcoe_vport_create(struct fc_vport *, bool disabled);
137 static int fcoe_vport_disable(struct fc_vport *, bool disable);
138 static void fcoe_set_vport_symbolic_name(struct fc_vport *);
139 static void fcoe_set_port_id(struct fc_lport *, u32, struct fc_frame *);
140 static int fcoe_validate_vport_create(struct fc_vport *);
142 static struct libfc_function_template fcoe_libfc_fcn_templ = {
143 .frame_send = fcoe_xmit,
144 .ddp_setup = fcoe_ddp_setup,
145 .ddp_done = fcoe_ddp_done,
146 .ddp_target = fcoe_ddp_target,
147 .elsct_send = fcoe_elsct_send,
148 .get_lesb = fcoe_get_lesb,
149 .lport_set_port_id = fcoe_set_port_id,
152 struct fc_function_template fcoe_nport_fc_functions = {
153 .show_host_node_name = 1,
154 .show_host_port_name = 1,
155 .show_host_supported_classes = 1,
156 .show_host_supported_fc4s = 1,
157 .show_host_active_fc4s = 1,
158 .show_host_maxframe_size = 1,
160 .show_host_port_id = 1,
161 .show_host_supported_speeds = 1,
162 .get_host_speed = fc_get_host_speed,
163 .show_host_speed = 1,
164 .show_host_port_type = 1,
165 .get_host_port_state = fc_get_host_port_state,
166 .show_host_port_state = 1,
167 .show_host_symbolic_name = 1,
169 .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
170 .show_rport_maxframe_size = 1,
171 .show_rport_supported_classes = 1,
173 .show_host_fabric_name = 1,
174 .show_starget_node_name = 1,
175 .show_starget_port_name = 1,
176 .show_starget_port_id = 1,
177 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
178 .show_rport_dev_loss_tmo = 1,
179 .get_fc_host_stats = fc_get_host_stats,
180 .issue_fc_host_lip = fcoe_reset,
182 .terminate_rport_io = fc_rport_terminate_io,
184 .vport_create = fcoe_vport_create,
185 .vport_delete = fcoe_vport_destroy,
186 .vport_disable = fcoe_vport_disable,
187 .set_vport_symbolic_name = fcoe_set_vport_symbolic_name,
189 .bsg_request = fc_lport_bsg_request,
192 struct fc_function_template fcoe_vport_fc_functions = {
193 .show_host_node_name = 1,
194 .show_host_port_name = 1,
195 .show_host_supported_classes = 1,
196 .show_host_supported_fc4s = 1,
197 .show_host_active_fc4s = 1,
198 .show_host_maxframe_size = 1,
200 .show_host_port_id = 1,
201 .show_host_supported_speeds = 1,
202 .get_host_speed = fc_get_host_speed,
203 .show_host_speed = 1,
204 .show_host_port_type = 1,
205 .get_host_port_state = fc_get_host_port_state,
206 .show_host_port_state = 1,
207 .show_host_symbolic_name = 1,
209 .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
210 .show_rport_maxframe_size = 1,
211 .show_rport_supported_classes = 1,
213 .show_host_fabric_name = 1,
214 .show_starget_node_name = 1,
215 .show_starget_port_name = 1,
216 .show_starget_port_id = 1,
217 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
218 .show_rport_dev_loss_tmo = 1,
219 .get_fc_host_stats = fc_get_host_stats,
220 .issue_fc_host_lip = fcoe_reset,
222 .terminate_rport_io = fc_rport_terminate_io,
224 .bsg_request = fc_lport_bsg_request,
227 static struct scsi_host_template fcoe_shost_template = {
228 .module = THIS_MODULE,
229 .name = "FCoE Driver",
230 .proc_name = FCOE_NAME,
231 .queuecommand = fc_queuecommand,
232 .eh_abort_handler = fc_eh_abort,
233 .eh_device_reset_handler = fc_eh_device_reset,
234 .eh_host_reset_handler = fc_eh_host_reset,
235 .slave_alloc = fc_slave_alloc,
236 .change_queue_depth = fc_change_queue_depth,
237 .change_queue_type = fc_change_queue_type,
238 .this_id = -1,
239 .cmd_per_lun = 3,
240 .can_queue = FCOE_MAX_OUTSTANDING_COMMANDS,
241 .use_clustering = ENABLE_CLUSTERING,
242 .sg_tablesize = SG_ALL,
243 .max_sectors = 0xffff,
247 * fcoe_interface_setup() - Setup a FCoE interface
248 * @fcoe: The new FCoE interface
249 * @netdev: The net device that the fcoe interface is on
251 * Returns : 0 for success
252 * Locking: must be called with the RTNL mutex held
254 static int fcoe_interface_setup(struct fcoe_interface *fcoe,
255 struct net_device *netdev)
257 struct fcoe_ctlr *fip = &fcoe->ctlr;
258 struct netdev_hw_addr *ha;
259 struct net_device *real_dev;
260 u8 flogi_maddr[ETH_ALEN];
261 const struct net_device_ops *ops;
263 fcoe->netdev = netdev;
265 /* Let LLD initialize for FCoE */
266 ops = netdev->netdev_ops;
267 if (ops->ndo_fcoe_enable) {
268 if (ops->ndo_fcoe_enable(netdev))
269 FCOE_NETDEV_DBG(netdev, "Failed to enable FCoE"
270 " specific feature for LLD.\n");
273 /* Do not support for bonding device */
274 if (netdev->priv_flags & IFF_BONDING && netdev->flags & IFF_MASTER) {
275 FCOE_NETDEV_DBG(netdev, "Bonded interfaces not supported\n");
276 return -EOPNOTSUPP;
279 /* look for SAN MAC address, if multiple SAN MACs exist, only
280 * use the first one for SPMA */
281 real_dev = (netdev->priv_flags & IFF_802_1Q_VLAN) ?
282 vlan_dev_real_dev(netdev) : netdev;
283 rcu_read_lock();
284 for_each_dev_addr(real_dev, ha) {
285 if ((ha->type == NETDEV_HW_ADDR_T_SAN) &&
286 (is_valid_ether_addr(ha->addr))) {
287 memcpy(fip->ctl_src_addr, ha->addr, ETH_ALEN);
288 fip->spma = 1;
289 break;
292 rcu_read_unlock();
294 /* setup Source Mac Address */
295 if (!fip->spma)
296 memcpy(fip->ctl_src_addr, netdev->dev_addr, netdev->addr_len);
299 * Add FCoE MAC address as second unicast MAC address
300 * or enter promiscuous mode if not capable of listening
301 * for multiple unicast MACs.
303 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
304 dev_uc_add(netdev, flogi_maddr);
305 if (fip->spma)
306 dev_uc_add(netdev, fip->ctl_src_addr);
307 if (fip->mode == FIP_MODE_VN2VN) {
308 dev_mc_add(netdev, FIP_ALL_VN2VN_MACS);
309 dev_mc_add(netdev, FIP_ALL_P2P_MACS);
310 } else
311 dev_mc_add(netdev, FIP_ALL_ENODE_MACS);
314 * setup the receive function from ethernet driver
315 * on the ethertype for the given device
317 fcoe->fcoe_packet_type.func = fcoe_rcv;
318 fcoe->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
319 fcoe->fcoe_packet_type.dev = netdev;
320 dev_add_pack(&fcoe->fcoe_packet_type);
322 fcoe->fip_packet_type.func = fcoe_fip_recv;
323 fcoe->fip_packet_type.type = htons(ETH_P_FIP);
324 fcoe->fip_packet_type.dev = netdev;
325 dev_add_pack(&fcoe->fip_packet_type);
327 return 0;
331 * fcoe_interface_create() - Create a FCoE interface on a net device
332 * @netdev: The net device to create the FCoE interface on
333 * @fip_mode: The mode to use for FIP
335 * Returns: pointer to a struct fcoe_interface or NULL on error
337 static struct fcoe_interface *fcoe_interface_create(struct net_device *netdev,
338 enum fip_state fip_mode)
340 struct fcoe_interface *fcoe;
341 int err;
343 if (!try_module_get(THIS_MODULE)) {
344 FCOE_NETDEV_DBG(netdev,
345 "Could not get a reference to the module\n");
346 fcoe = ERR_PTR(-EBUSY);
347 goto out;
350 fcoe = kzalloc(sizeof(*fcoe), GFP_KERNEL);
351 if (!fcoe) {
352 FCOE_NETDEV_DBG(netdev, "Could not allocate fcoe structure\n");
353 fcoe = ERR_PTR(-ENOMEM);
354 goto out_nomod;
357 dev_hold(netdev);
358 kref_init(&fcoe->kref);
361 * Initialize FIP.
363 fcoe_ctlr_init(&fcoe->ctlr, fip_mode);
364 fcoe->ctlr.send = fcoe_fip_send;
365 fcoe->ctlr.update_mac = fcoe_update_src_mac;
366 fcoe->ctlr.get_src_addr = fcoe_get_src_mac;
368 err = fcoe_interface_setup(fcoe, netdev);
369 if (err) {
370 fcoe_ctlr_destroy(&fcoe->ctlr);
371 kfree(fcoe);
372 dev_put(netdev);
373 fcoe = ERR_PTR(err);
374 goto out_nomod;
377 goto out;
379 out_nomod:
380 module_put(THIS_MODULE);
381 out:
382 return fcoe;
386 * fcoe_interface_release() - fcoe_port kref release function
387 * @kref: Embedded reference count in an fcoe_interface struct
389 static void fcoe_interface_release(struct kref *kref)
391 struct fcoe_interface *fcoe;
392 struct net_device *netdev;
394 fcoe = container_of(kref, struct fcoe_interface, kref);
395 netdev = fcoe->netdev;
396 /* tear-down the FCoE controller */
397 fcoe_ctlr_destroy(&fcoe->ctlr);
398 kfree(fcoe);
399 dev_put(netdev);
400 module_put(THIS_MODULE);
404 * fcoe_interface_get() - Get a reference to a FCoE interface
405 * @fcoe: The FCoE interface to be held
407 static inline void fcoe_interface_get(struct fcoe_interface *fcoe)
409 kref_get(&fcoe->kref);
413 * fcoe_interface_put() - Put a reference to a FCoE interface
414 * @fcoe: The FCoE interface to be released
416 static inline void fcoe_interface_put(struct fcoe_interface *fcoe)
418 kref_put(&fcoe->kref, fcoe_interface_release);
422 * fcoe_interface_cleanup() - Clean up a FCoE interface
423 * @fcoe: The FCoE interface to be cleaned up
425 * Caller must be holding the RTNL mutex
427 void fcoe_interface_cleanup(struct fcoe_interface *fcoe)
429 struct net_device *netdev = fcoe->netdev;
430 struct fcoe_ctlr *fip = &fcoe->ctlr;
431 u8 flogi_maddr[ETH_ALEN];
432 const struct net_device_ops *ops;
434 rtnl_lock();
437 * Don't listen for Ethernet packets anymore.
438 * synchronize_net() ensures that the packet handlers are not running
439 * on another CPU. dev_remove_pack() would do that, this calls the
440 * unsyncronized version __dev_remove_pack() to avoid multiple delays.
442 __dev_remove_pack(&fcoe->fcoe_packet_type);
443 __dev_remove_pack(&fcoe->fip_packet_type);
444 synchronize_net();
446 /* Delete secondary MAC addresses */
447 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
448 dev_uc_del(netdev, flogi_maddr);
449 if (fip->spma)
450 dev_uc_del(netdev, fip->ctl_src_addr);
451 if (fip->mode == FIP_MODE_VN2VN) {
452 dev_mc_del(netdev, FIP_ALL_VN2VN_MACS);
453 dev_mc_del(netdev, FIP_ALL_P2P_MACS);
454 } else
455 dev_mc_del(netdev, FIP_ALL_ENODE_MACS);
457 /* Tell the LLD we are done w/ FCoE */
458 ops = netdev->netdev_ops;
459 if (ops->ndo_fcoe_disable) {
460 if (ops->ndo_fcoe_disable(netdev))
461 FCOE_NETDEV_DBG(netdev, "Failed to disable FCoE"
462 " specific feature for LLD.\n");
465 rtnl_unlock();
467 /* Release the self-reference taken during fcoe_interface_create() */
468 fcoe_interface_put(fcoe);
472 * fcoe_fip_recv() - Handler for received FIP frames
473 * @skb: The receive skb
474 * @netdev: The associated net device
475 * @ptype: The packet_type structure which was used to register this handler
476 * @orig_dev: The original net_device the the skb was received on.
477 * (in case dev is a bond)
479 * Returns: 0 for success
481 static int fcoe_fip_recv(struct sk_buff *skb, struct net_device *netdev,
482 struct packet_type *ptype,
483 struct net_device *orig_dev)
485 struct fcoe_interface *fcoe;
487 fcoe = container_of(ptype, struct fcoe_interface, fip_packet_type);
488 fcoe_ctlr_recv(&fcoe->ctlr, skb);
489 return 0;
493 * fcoe_port_send() - Send an Ethernet-encapsulated FIP/FCoE frame
494 * @port: The FCoE port
495 * @skb: The FIP/FCoE packet to be sent
497 static void fcoe_port_send(struct fcoe_port *port, struct sk_buff *skb)
499 if (port->fcoe_pending_queue.qlen)
500 fcoe_check_wait_queue(port->lport, skb);
501 else if (fcoe_start_io(skb))
502 fcoe_check_wait_queue(port->lport, skb);
506 * fcoe_fip_send() - Send an Ethernet-encapsulated FIP frame
507 * @fip: The FCoE controller
508 * @skb: The FIP packet to be sent
510 static void fcoe_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
512 skb->dev = fcoe_from_ctlr(fip)->netdev;
513 fcoe_port_send(lport_priv(fip->lp), skb);
517 * fcoe_update_src_mac() - Update the Ethernet MAC filters
518 * @lport: The local port to update the source MAC on
519 * @addr: Unicast MAC address to add
521 * Remove any previously-set unicast MAC filter.
522 * Add secondary FCoE MAC address filter for our OUI.
524 static void fcoe_update_src_mac(struct fc_lport *lport, u8 *addr)
526 struct fcoe_port *port = lport_priv(lport);
527 struct fcoe_interface *fcoe = port->priv;
529 rtnl_lock();
530 if (!is_zero_ether_addr(port->data_src_addr))
531 dev_uc_del(fcoe->netdev, port->data_src_addr);
532 if (!is_zero_ether_addr(addr))
533 dev_uc_add(fcoe->netdev, addr);
534 memcpy(port->data_src_addr, addr, ETH_ALEN);
535 rtnl_unlock();
539 * fcoe_get_src_mac() - return the Ethernet source address for an lport
540 * @lport: libfc lport
542 static u8 *fcoe_get_src_mac(struct fc_lport *lport)
544 struct fcoe_port *port = lport_priv(lport);
546 return port->data_src_addr;
550 * fcoe_lport_config() - Set up a local port
551 * @lport: The local port to be setup
553 * Returns: 0 for success
555 static int fcoe_lport_config(struct fc_lport *lport)
557 lport->link_up = 0;
558 lport->qfull = 0;
559 lport->max_retry_count = 3;
560 lport->max_rport_retry_count = 3;
561 lport->e_d_tov = 2 * 1000; /* FC-FS default */
562 lport->r_a_tov = 2 * 2 * 1000;
563 lport->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
564 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
565 lport->does_npiv = 1;
567 fc_lport_init_stats(lport);
569 /* lport fc_lport related configuration */
570 fc_lport_config(lport);
572 /* offload related configuration */
573 lport->crc_offload = 0;
574 lport->seq_offload = 0;
575 lport->lro_enabled = 0;
576 lport->lro_xid = 0;
577 lport->lso_max = 0;
579 return 0;
583 * fcoe_get_wwn() - Get the world wide name from LLD if it supports it
584 * @netdev: the associated net device
585 * @wwn: the output WWN
586 * @type: the type of WWN (WWPN or WWNN)
588 * Returns: 0 for success
590 static int fcoe_get_wwn(struct net_device *netdev, u64 *wwn, int type)
592 const struct net_device_ops *ops = netdev->netdev_ops;
594 if (ops->ndo_fcoe_get_wwn)
595 return ops->ndo_fcoe_get_wwn(netdev, wwn, type);
596 return -EINVAL;
600 * fcoe_netdev_features_change - Updates the lport's offload flags based
601 * on the LLD netdev's FCoE feature flags
603 static void fcoe_netdev_features_change(struct fc_lport *lport,
604 struct net_device *netdev)
606 mutex_lock(&lport->lp_mutex);
608 if (netdev->features & NETIF_F_SG)
609 lport->sg_supp = 1;
610 else
611 lport->sg_supp = 0;
613 if (netdev->features & NETIF_F_FCOE_CRC) {
614 lport->crc_offload = 1;
615 FCOE_NETDEV_DBG(netdev, "Supports FCCRC offload\n");
616 } else {
617 lport->crc_offload = 0;
620 if (netdev->features & NETIF_F_FSO) {
621 lport->seq_offload = 1;
622 lport->lso_max = netdev->gso_max_size;
623 FCOE_NETDEV_DBG(netdev, "Supports LSO for max len 0x%x\n",
624 lport->lso_max);
625 } else {
626 lport->seq_offload = 0;
627 lport->lso_max = 0;
630 if (netdev->fcoe_ddp_xid) {
631 lport->lro_enabled = 1;
632 lport->lro_xid = netdev->fcoe_ddp_xid;
633 FCOE_NETDEV_DBG(netdev, "Supports LRO for max xid 0x%x\n",
634 lport->lro_xid);
635 } else {
636 lport->lro_enabled = 0;
637 lport->lro_xid = 0;
640 mutex_unlock(&lport->lp_mutex);
644 * fcoe_netdev_config() - Set up net devive for SW FCoE
645 * @lport: The local port that is associated with the net device
646 * @netdev: The associated net device
648 * Must be called after fcoe_lport_config() as it will use local port mutex
650 * Returns: 0 for success
652 static int fcoe_netdev_config(struct fc_lport *lport, struct net_device *netdev)
654 u32 mfs;
655 u64 wwnn, wwpn;
656 struct fcoe_interface *fcoe;
657 struct fcoe_port *port;
659 /* Setup lport private data to point to fcoe softc */
660 port = lport_priv(lport);
661 fcoe = port->priv;
664 * Determine max frame size based on underlying device and optional
665 * user-configured limit. If the MFS is too low, fcoe_link_ok()
666 * will return 0, so do this first.
668 mfs = netdev->mtu;
669 if (netdev->features & NETIF_F_FCOE_MTU) {
670 mfs = FCOE_MTU;
671 FCOE_NETDEV_DBG(netdev, "Supports FCOE_MTU of %d bytes\n", mfs);
673 mfs -= (sizeof(struct fcoe_hdr) + sizeof(struct fcoe_crc_eof));
674 if (fc_set_mfs(lport, mfs))
675 return -EINVAL;
677 /* offload features support */
678 fcoe_netdev_features_change(lport, netdev);
680 skb_queue_head_init(&port->fcoe_pending_queue);
681 port->fcoe_pending_queue_active = 0;
682 setup_timer(&port->timer, fcoe_queue_timer, (unsigned long)lport);
684 fcoe_link_speed_update(lport);
686 if (!lport->vport) {
687 if (fcoe_get_wwn(netdev, &wwnn, NETDEV_FCOE_WWNN))
688 wwnn = fcoe_wwn_from_mac(fcoe->ctlr.ctl_src_addr, 1, 0);
689 fc_set_wwnn(lport, wwnn);
690 if (fcoe_get_wwn(netdev, &wwpn, NETDEV_FCOE_WWPN))
691 wwpn = fcoe_wwn_from_mac(fcoe->ctlr.ctl_src_addr,
692 2, 0);
693 fc_set_wwpn(lport, wwpn);
696 return 0;
700 * fcoe_shost_config() - Set up the SCSI host associated with a local port
701 * @lport: The local port
702 * @dev: The device associated with the SCSI host
704 * Must be called after fcoe_lport_config() and fcoe_netdev_config()
706 * Returns: 0 for success
708 static int fcoe_shost_config(struct fc_lport *lport, struct device *dev)
710 int rc = 0;
712 /* lport scsi host config */
713 lport->host->max_lun = FCOE_MAX_LUN;
714 lport->host->max_id = FCOE_MAX_FCP_TARGET;
715 lport->host->max_channel = 0;
716 lport->host->max_cmd_len = FCOE_MAX_CMD_LEN;
718 if (lport->vport)
719 lport->host->transportt = fcoe_vport_scsi_transport;
720 else
721 lport->host->transportt = fcoe_nport_scsi_transport;
723 /* add the new host to the SCSI-ml */
724 rc = scsi_add_host(lport->host, dev);
725 if (rc) {
726 FCOE_NETDEV_DBG(fcoe_netdev(lport), "fcoe_shost_config: "
727 "error on scsi_add_host\n");
728 return rc;
731 if (!lport->vport)
732 fc_host_max_npiv_vports(lport->host) = USHRT_MAX;
734 snprintf(fc_host_symbolic_name(lport->host), FC_SYMBOLIC_NAME_SIZE,
735 "%s v%s over %s", FCOE_NAME, FCOE_VERSION,
736 fcoe_netdev(lport)->name);
738 return 0;
742 * fcoe_oem_match() - The match routine for the offloaded exchange manager
743 * @fp: The I/O frame
745 * This routine will be associated with an exchange manager (EM). When
746 * the libfc exchange handling code is looking for an EM to use it will
747 * call this routine and pass it the frame that it wishes to send. This
748 * routine will return True if the associated EM is to be used and False
749 * if the echange code should continue looking for an EM.
751 * The offload EM that this routine is associated with will handle any
752 * packets that are for SCSI read requests.
754 * This has been enhanced to work when FCoE stack is operating in target
755 * mode.
757 * Returns: True for read types I/O, otherwise returns false.
759 bool fcoe_oem_match(struct fc_frame *fp)
761 struct fc_frame_header *fh = fc_frame_header_get(fp);
762 struct fcp_cmnd *fcp;
764 if (fc_fcp_is_read(fr_fsp(fp)) &&
765 (fr_fsp(fp)->data_len > fcoe_ddp_min))
766 return true;
767 else if (!(ntoh24(fh->fh_f_ctl) & FC_FC_EX_CTX)) {
768 fcp = fc_frame_payload_get(fp, sizeof(*fcp));
769 if (ntohs(fh->fh_rx_id) == FC_XID_UNKNOWN &&
770 fcp && (ntohl(fcp->fc_dl) > fcoe_ddp_min) &&
771 (fcp->fc_flags & FCP_CFL_WRDATA))
772 return true;
774 return false;
778 * fcoe_em_config() - Allocate and configure an exchange manager
779 * @lport: The local port that the new EM will be associated with
781 * Returns: 0 on success
783 static inline int fcoe_em_config(struct fc_lport *lport)
785 struct fcoe_port *port = lport_priv(lport);
786 struct fcoe_interface *fcoe = port->priv;
787 struct fcoe_interface *oldfcoe = NULL;
788 struct net_device *old_real_dev, *cur_real_dev;
789 u16 min_xid = FCOE_MIN_XID;
790 u16 max_xid = FCOE_MAX_XID;
793 * Check if need to allocate an em instance for
794 * offload exchange ids to be shared across all VN_PORTs/lport.
796 if (!lport->lro_enabled || !lport->lro_xid ||
797 (lport->lro_xid >= max_xid)) {
798 lport->lro_xid = 0;
799 goto skip_oem;
803 * Reuse existing offload em instance in case
804 * it is already allocated on real eth device
806 if (fcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
807 cur_real_dev = vlan_dev_real_dev(fcoe->netdev);
808 else
809 cur_real_dev = fcoe->netdev;
811 list_for_each_entry(oldfcoe, &fcoe_hostlist, list) {
812 if (oldfcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
813 old_real_dev = vlan_dev_real_dev(oldfcoe->netdev);
814 else
815 old_real_dev = oldfcoe->netdev;
817 if (cur_real_dev == old_real_dev) {
818 fcoe->oem = oldfcoe->oem;
819 break;
823 if (fcoe->oem) {
824 if (!fc_exch_mgr_add(lport, fcoe->oem, fcoe_oem_match)) {
825 printk(KERN_ERR "fcoe_em_config: failed to add "
826 "offload em:%p on interface:%s\n",
827 fcoe->oem, fcoe->netdev->name);
828 return -ENOMEM;
830 } else {
831 fcoe->oem = fc_exch_mgr_alloc(lport, FC_CLASS_3,
832 FCOE_MIN_XID, lport->lro_xid,
833 fcoe_oem_match);
834 if (!fcoe->oem) {
835 printk(KERN_ERR "fcoe_em_config: failed to allocate "
836 "em for offload exches on interface:%s\n",
837 fcoe->netdev->name);
838 return -ENOMEM;
843 * Exclude offload EM xid range from next EM xid range.
845 min_xid += lport->lro_xid + 1;
847 skip_oem:
848 if (!fc_exch_mgr_alloc(lport, FC_CLASS_3, min_xid, max_xid, NULL)) {
849 printk(KERN_ERR "fcoe_em_config: failed to "
850 "allocate em on interface %s\n", fcoe->netdev->name);
851 return -ENOMEM;
854 return 0;
858 * fcoe_if_destroy() - Tear down a SW FCoE instance
859 * @lport: The local port to be destroyed
862 static void fcoe_if_destroy(struct fc_lport *lport)
864 struct fcoe_port *port = lport_priv(lport);
865 struct fcoe_interface *fcoe = port->priv;
866 struct net_device *netdev = fcoe->netdev;
868 FCOE_NETDEV_DBG(netdev, "Destroying interface\n");
870 /* Logout of the fabric */
871 fc_fabric_logoff(lport);
873 /* Cleanup the fc_lport */
874 fc_lport_destroy(lport);
876 /* Stop the transmit retry timer */
877 del_timer_sync(&port->timer);
879 /* Free existing transmit skbs */
880 fcoe_clean_pending_queue(lport);
882 rtnl_lock();
883 if (!is_zero_ether_addr(port->data_src_addr))
884 dev_uc_del(netdev, port->data_src_addr);
885 rtnl_unlock();
887 /* Release reference held in fcoe_if_create() */
888 fcoe_interface_put(fcoe);
890 /* Free queued packets for the per-CPU receive threads */
891 fcoe_percpu_clean(lport);
893 /* Detach from the scsi-ml */
894 fc_remove_host(lport->host);
895 scsi_remove_host(lport->host);
897 /* Destroy lport scsi_priv */
898 fc_fcp_destroy(lport);
900 /* There are no more rports or I/O, free the EM */
901 fc_exch_mgr_free(lport);
903 /* Free memory used by statistical counters */
904 fc_lport_free_stats(lport);
906 /* Release the Scsi_Host */
907 scsi_host_put(lport->host);
911 * fcoe_ddp_setup() - Call a LLD's ddp_setup through the net device
912 * @lport: The local port to setup DDP for
913 * @xid: The exchange ID for this DDP transfer
914 * @sgl: The scatterlist describing this transfer
915 * @sgc: The number of sg items
917 * Returns: 0 if the DDP context was not configured
919 static int fcoe_ddp_setup(struct fc_lport *lport, u16 xid,
920 struct scatterlist *sgl, unsigned int sgc)
922 struct net_device *netdev = fcoe_netdev(lport);
924 if (netdev->netdev_ops->ndo_fcoe_ddp_setup)
925 return netdev->netdev_ops->ndo_fcoe_ddp_setup(netdev,
926 xid, sgl,
927 sgc);
929 return 0;
933 * fcoe_ddp_target() - Call a LLD's ddp_target through the net device
934 * @lport: The local port to setup DDP for
935 * @xid: The exchange ID for this DDP transfer
936 * @sgl: The scatterlist describing this transfer
937 * @sgc: The number of sg items
939 * Returns: 0 if the DDP context was not configured
941 static int fcoe_ddp_target(struct fc_lport *lport, u16 xid,
942 struct scatterlist *sgl, unsigned int sgc)
944 struct net_device *netdev = fcoe_netdev(lport);
946 if (netdev->netdev_ops->ndo_fcoe_ddp_target)
947 return netdev->netdev_ops->ndo_fcoe_ddp_target(netdev, xid,
948 sgl, sgc);
950 return 0;
955 * fcoe_ddp_done() - Call a LLD's ddp_done through the net device
956 * @lport: The local port to complete DDP on
957 * @xid: The exchange ID for this DDP transfer
959 * Returns: the length of data that have been completed by DDP
961 static int fcoe_ddp_done(struct fc_lport *lport, u16 xid)
963 struct net_device *netdev = fcoe_netdev(lport);
965 if (netdev->netdev_ops->ndo_fcoe_ddp_done)
966 return netdev->netdev_ops->ndo_fcoe_ddp_done(netdev, xid);
967 return 0;
971 * fcoe_if_create() - Create a FCoE instance on an interface
972 * @fcoe: The FCoE interface to create a local port on
973 * @parent: The device pointer to be the parent in sysfs for the SCSI host
974 * @npiv: Indicates if the port is a vport or not
976 * Creates a fc_lport instance and a Scsi_Host instance and configure them.
978 * Returns: The allocated fc_lport or an error pointer
980 static struct fc_lport *fcoe_if_create(struct fcoe_interface *fcoe,
981 struct device *parent, int npiv)
983 struct net_device *netdev = fcoe->netdev;
984 struct fc_lport *lport, *n_port;
985 struct fcoe_port *port;
986 struct Scsi_Host *shost;
987 int rc;
989 * parent is only a vport if npiv is 1,
990 * but we'll only use vport in that case so go ahead and set it
992 struct fc_vport *vport = dev_to_vport(parent);
994 FCOE_NETDEV_DBG(netdev, "Create Interface\n");
996 if (!npiv)
997 lport = libfc_host_alloc(&fcoe_shost_template, sizeof(*port));
998 else
999 lport = libfc_vport_create(vport, sizeof(*port));
1001 if (!lport) {
1002 FCOE_NETDEV_DBG(netdev, "Could not allocate host structure\n");
1003 rc = -ENOMEM;
1004 goto out;
1006 port = lport_priv(lport);
1007 port->lport = lport;
1008 port->priv = fcoe;
1009 port->max_queue_depth = FCOE_MAX_QUEUE_DEPTH;
1010 port->min_queue_depth = FCOE_MIN_QUEUE_DEPTH;
1011 INIT_WORK(&port->destroy_work, fcoe_destroy_work);
1013 /* configure a fc_lport including the exchange manager */
1014 rc = fcoe_lport_config(lport);
1015 if (rc) {
1016 FCOE_NETDEV_DBG(netdev, "Could not configure lport for the "
1017 "interface\n");
1018 goto out_host_put;
1021 if (npiv) {
1022 FCOE_NETDEV_DBG(netdev, "Setting vport names, "
1023 "%16.16llx %16.16llx\n",
1024 vport->node_name, vport->port_name);
1025 fc_set_wwnn(lport, vport->node_name);
1026 fc_set_wwpn(lport, vport->port_name);
1029 /* configure lport network properties */
1030 rc = fcoe_netdev_config(lport, netdev);
1031 if (rc) {
1032 FCOE_NETDEV_DBG(netdev, "Could not configure netdev for the "
1033 "interface\n");
1034 goto out_lp_destroy;
1037 /* configure lport scsi host properties */
1038 rc = fcoe_shost_config(lport, parent);
1039 if (rc) {
1040 FCOE_NETDEV_DBG(netdev, "Could not configure shost for the "
1041 "interface\n");
1042 goto out_lp_destroy;
1045 /* Initialize the library */
1046 rc = fcoe_libfc_config(lport, &fcoe->ctlr, &fcoe_libfc_fcn_templ, 1);
1047 if (rc) {
1048 FCOE_NETDEV_DBG(netdev, "Could not configure libfc for the "
1049 "interface\n");
1050 goto out_lp_destroy;
1054 * fcoe_em_alloc() and fcoe_hostlist_add() both
1055 * need to be atomic with respect to other changes to the
1056 * hostlist since fcoe_em_alloc() looks for an existing EM
1057 * instance on host list updated by fcoe_hostlist_add().
1059 * This is currently handled through the fcoe_config_mutex
1060 * begin held.
1062 if (!npiv)
1063 /* lport exch manager allocation */
1064 rc = fcoe_em_config(lport);
1065 else {
1066 shost = vport_to_shost(vport);
1067 n_port = shost_priv(shost);
1068 rc = fc_exch_mgr_list_clone(n_port, lport);
1071 if (rc) {
1072 FCOE_NETDEV_DBG(netdev, "Could not configure the EM\n");
1073 goto out_lp_destroy;
1076 fcoe_interface_get(fcoe);
1077 return lport;
1079 out_lp_destroy:
1080 fc_exch_mgr_free(lport);
1081 out_host_put:
1082 scsi_host_put(lport->host);
1083 out:
1084 return ERR_PTR(rc);
1088 * fcoe_if_init() - Initialization routine for fcoe.ko
1090 * Attaches the SW FCoE transport to the FC transport
1092 * Returns: 0 on success
1094 static int __init fcoe_if_init(void)
1096 /* attach to scsi transport */
1097 fcoe_nport_scsi_transport =
1098 fc_attach_transport(&fcoe_nport_fc_functions);
1099 fcoe_vport_scsi_transport =
1100 fc_attach_transport(&fcoe_vport_fc_functions);
1102 if (!fcoe_nport_scsi_transport) {
1103 printk(KERN_ERR "fcoe: Failed to attach to the FC transport\n");
1104 return -ENODEV;
1107 return 0;
1111 * fcoe_if_exit() - Tear down fcoe.ko
1113 * Detaches the SW FCoE transport from the FC transport
1115 * Returns: 0 on success
1117 int __exit fcoe_if_exit(void)
1119 fc_release_transport(fcoe_nport_scsi_transport);
1120 fc_release_transport(fcoe_vport_scsi_transport);
1121 fcoe_nport_scsi_transport = NULL;
1122 fcoe_vport_scsi_transport = NULL;
1123 return 0;
1127 * fcoe_percpu_thread_create() - Create a receive thread for an online CPU
1128 * @cpu: The CPU index of the CPU to create a receive thread for
1130 static void fcoe_percpu_thread_create(unsigned int cpu)
1132 struct fcoe_percpu_s *p;
1133 struct task_struct *thread;
1135 p = &per_cpu(fcoe_percpu, cpu);
1137 thread = kthread_create(fcoe_percpu_receive_thread,
1138 (void *)p, "fcoethread/%d", cpu);
1140 if (likely(!IS_ERR(thread))) {
1141 kthread_bind(thread, cpu);
1142 wake_up_process(thread);
1144 spin_lock_bh(&p->fcoe_rx_list.lock);
1145 p->thread = thread;
1146 spin_unlock_bh(&p->fcoe_rx_list.lock);
1151 * fcoe_percpu_thread_destroy() - Remove the receive thread of a CPU
1152 * @cpu: The CPU index of the CPU whose receive thread is to be destroyed
1154 * Destroys a per-CPU Rx thread. Any pending skbs are moved to the
1155 * current CPU's Rx thread. If the thread being destroyed is bound to
1156 * the CPU processing this context the skbs will be freed.
1158 static void fcoe_percpu_thread_destroy(unsigned int cpu)
1160 struct fcoe_percpu_s *p;
1161 struct task_struct *thread;
1162 struct page *crc_eof;
1163 struct sk_buff *skb;
1164 #ifdef CONFIG_SMP
1165 struct fcoe_percpu_s *p0;
1166 unsigned targ_cpu = get_cpu();
1167 #endif /* CONFIG_SMP */
1169 FCOE_DBG("Destroying receive thread for CPU %d\n", cpu);
1171 /* Prevent any new skbs from being queued for this CPU. */
1172 p = &per_cpu(fcoe_percpu, cpu);
1173 spin_lock_bh(&p->fcoe_rx_list.lock);
1174 thread = p->thread;
1175 p->thread = NULL;
1176 crc_eof = p->crc_eof_page;
1177 p->crc_eof_page = NULL;
1178 p->crc_eof_offset = 0;
1179 spin_unlock_bh(&p->fcoe_rx_list.lock);
1181 #ifdef CONFIG_SMP
1183 * Don't bother moving the skb's if this context is running
1184 * on the same CPU that is having its thread destroyed. This
1185 * can easily happen when the module is removed.
1187 if (cpu != targ_cpu) {
1188 p0 = &per_cpu(fcoe_percpu, targ_cpu);
1189 spin_lock_bh(&p0->fcoe_rx_list.lock);
1190 if (p0->thread) {
1191 FCOE_DBG("Moving frames from CPU %d to CPU %d\n",
1192 cpu, targ_cpu);
1194 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1195 __skb_queue_tail(&p0->fcoe_rx_list, skb);
1196 spin_unlock_bh(&p0->fcoe_rx_list.lock);
1197 } else {
1199 * The targeted CPU is not initialized and cannot accept
1200 * new skbs. Unlock the targeted CPU and drop the skbs
1201 * on the CPU that is going offline.
1203 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1204 kfree_skb(skb);
1205 spin_unlock_bh(&p0->fcoe_rx_list.lock);
1207 } else {
1209 * This scenario occurs when the module is being removed
1210 * and all threads are being destroyed. skbs will continue
1211 * to be shifted from the CPU thread that is being removed
1212 * to the CPU thread associated with the CPU that is processing
1213 * the module removal. Once there is only one CPU Rx thread it
1214 * will reach this case and we will drop all skbs and later
1215 * stop the thread.
1217 spin_lock_bh(&p->fcoe_rx_list.lock);
1218 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1219 kfree_skb(skb);
1220 spin_unlock_bh(&p->fcoe_rx_list.lock);
1222 put_cpu();
1223 #else
1225 * This a non-SMP scenario where the singular Rx thread is
1226 * being removed. Free all skbs and stop the thread.
1228 spin_lock_bh(&p->fcoe_rx_list.lock);
1229 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1230 kfree_skb(skb);
1231 spin_unlock_bh(&p->fcoe_rx_list.lock);
1232 #endif
1234 if (thread)
1235 kthread_stop(thread);
1237 if (crc_eof)
1238 put_page(crc_eof);
1242 * fcoe_cpu_callback() - Handler for CPU hotplug events
1243 * @nfb: The callback data block
1244 * @action: The event triggering the callback
1245 * @hcpu: The index of the CPU that the event is for
1247 * This creates or destroys per-CPU data for fcoe
1249 * Returns NOTIFY_OK always.
1251 static int fcoe_cpu_callback(struct notifier_block *nfb,
1252 unsigned long action, void *hcpu)
1254 unsigned cpu = (unsigned long)hcpu;
1256 switch (action) {
1257 case CPU_ONLINE:
1258 case CPU_ONLINE_FROZEN:
1259 FCOE_DBG("CPU %x online: Create Rx thread\n", cpu);
1260 fcoe_percpu_thread_create(cpu);
1261 break;
1262 case CPU_DEAD:
1263 case CPU_DEAD_FROZEN:
1264 FCOE_DBG("CPU %x offline: Remove Rx thread\n", cpu);
1265 fcoe_percpu_thread_destroy(cpu);
1266 break;
1267 default:
1268 break;
1270 return NOTIFY_OK;
1274 * fcoe_select_cpu() - Selects CPU to handle post-processing of incoming
1275 * command.
1277 * This routine selects next CPU based on cpumask to distribute
1278 * incoming requests in round robin.
1280 * Returns: int CPU number
1282 static inline unsigned int fcoe_select_cpu(void)
1284 static unsigned int selected_cpu;
1286 selected_cpu = cpumask_next(selected_cpu, cpu_online_mask);
1287 if (selected_cpu >= nr_cpu_ids)
1288 selected_cpu = cpumask_first(cpu_online_mask);
1290 return selected_cpu;
1294 * fcoe_rcv() - Receive packets from a net device
1295 * @skb: The received packet
1296 * @netdev: The net device that the packet was received on
1297 * @ptype: The packet type context
1298 * @olddev: The last device net device
1300 * This routine is called by NET_RX_SOFTIRQ. It receives a packet, builds a
1301 * FC frame and passes the frame to libfc.
1303 * Returns: 0 for success
1305 int fcoe_rcv(struct sk_buff *skb, struct net_device *netdev,
1306 struct packet_type *ptype, struct net_device *olddev)
1308 struct fc_lport *lport;
1309 struct fcoe_rcv_info *fr;
1310 struct fcoe_interface *fcoe;
1311 struct fc_frame_header *fh;
1312 struct fcoe_percpu_s *fps;
1313 struct ethhdr *eh;
1314 unsigned int cpu;
1316 fcoe = container_of(ptype, struct fcoe_interface, fcoe_packet_type);
1317 lport = fcoe->ctlr.lp;
1318 if (unlikely(!lport)) {
1319 FCOE_NETDEV_DBG(netdev, "Cannot find hba structure");
1320 goto err2;
1322 if (!lport->link_up)
1323 goto err2;
1325 FCOE_NETDEV_DBG(netdev, "skb_info: len:%d data_len:%d head:%p "
1326 "data:%p tail:%p end:%p sum:%d dev:%s",
1327 skb->len, skb->data_len, skb->head, skb->data,
1328 skb_tail_pointer(skb), skb_end_pointer(skb),
1329 skb->csum, skb->dev ? skb->dev->name : "<NULL>");
1331 eh = eth_hdr(skb);
1333 if (is_fip_mode(&fcoe->ctlr) &&
1334 compare_ether_addr(eh->h_source, fcoe->ctlr.dest_addr)) {
1335 FCOE_NETDEV_DBG(netdev, "wrong source mac address:%pM\n",
1336 eh->h_source);
1337 goto err;
1341 * Check for minimum frame length, and make sure required FCoE
1342 * and FC headers are pulled into the linear data area.
1344 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
1345 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
1346 goto err;
1348 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
1349 fh = (struct fc_frame_header *) skb_transport_header(skb);
1351 if (ntoh24(&eh->h_dest[3]) != ntoh24(fh->fh_d_id)) {
1352 FCOE_NETDEV_DBG(netdev, "FC frame d_id mismatch with MAC:%pM\n",
1353 eh->h_dest);
1354 goto err;
1357 fr = fcoe_dev_from_skb(skb);
1358 fr->fr_dev = lport;
1361 * In case the incoming frame's exchange is originated from
1362 * the initiator, then received frame's exchange id is ANDed
1363 * with fc_cpu_mask bits to get the same cpu on which exchange
1364 * was originated, otherwise select cpu using rx exchange id
1365 * or fcoe_select_cpu().
1367 if (ntoh24(fh->fh_f_ctl) & FC_FC_EX_CTX)
1368 cpu = ntohs(fh->fh_ox_id) & fc_cpu_mask;
1369 else {
1370 if (ntohs(fh->fh_rx_id) == FC_XID_UNKNOWN)
1371 cpu = fcoe_select_cpu();
1372 else
1373 cpu = ntohs(fh->fh_rx_id) & fc_cpu_mask;
1376 if (cpu >= nr_cpu_ids)
1377 goto err;
1379 fps = &per_cpu(fcoe_percpu, cpu);
1380 spin_lock_bh(&fps->fcoe_rx_list.lock);
1381 if (unlikely(!fps->thread)) {
1383 * The targeted CPU is not ready, let's target
1384 * the first CPU now. For non-SMP systems this
1385 * will check the same CPU twice.
1387 FCOE_NETDEV_DBG(netdev, "CPU is online, but no receive thread "
1388 "ready for incoming skb- using first online "
1389 "CPU.\n");
1391 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1392 cpu = cpumask_first(cpu_online_mask);
1393 fps = &per_cpu(fcoe_percpu, cpu);
1394 spin_lock_bh(&fps->fcoe_rx_list.lock);
1395 if (!fps->thread) {
1396 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1397 goto err;
1402 * We now have a valid CPU that we're targeting for
1403 * this skb. We also have this receive thread locked,
1404 * so we're free to queue skbs into it's queue.
1407 /* If this is a SCSI-FCP frame, and this is already executing on the
1408 * correct CPU, and the queue for this CPU is empty, then go ahead
1409 * and process the frame directly in the softirq context.
1410 * This lets us process completions without context switching from the
1411 * NET_RX softirq, to our receive processing thread, and then back to
1412 * BLOCK softirq context.
1414 if (fh->fh_type == FC_TYPE_FCP &&
1415 cpu == smp_processor_id() &&
1416 skb_queue_empty(&fps->fcoe_rx_list)) {
1417 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1418 fcoe_recv_frame(skb);
1419 } else {
1420 __skb_queue_tail(&fps->fcoe_rx_list, skb);
1421 if (fps->fcoe_rx_list.qlen == 1)
1422 wake_up_process(fps->thread);
1423 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1426 return 0;
1427 err:
1428 per_cpu_ptr(lport->dev_stats, get_cpu())->ErrorFrames++;
1429 put_cpu();
1430 err2:
1431 kfree_skb(skb);
1432 return -1;
1436 * fcoe_alloc_paged_crc_eof() - Allocate a page to be used for the trailer CRC
1437 * @skb: The packet to be transmitted
1438 * @tlen: The total length of the trailer
1440 * Returns: 0 for success
1442 static int fcoe_alloc_paged_crc_eof(struct sk_buff *skb, int tlen)
1444 struct fcoe_percpu_s *fps;
1445 int rc;
1447 fps = &get_cpu_var(fcoe_percpu);
1448 rc = fcoe_get_paged_crc_eof(skb, tlen, fps);
1449 put_cpu_var(fcoe_percpu);
1451 return rc;
1455 * fcoe_xmit() - Transmit a FCoE frame
1456 * @lport: The local port that the frame is to be transmitted for
1457 * @fp: The frame to be transmitted
1459 * Return: 0 for success
1461 int fcoe_xmit(struct fc_lport *lport, struct fc_frame *fp)
1463 int wlen;
1464 u32 crc;
1465 struct ethhdr *eh;
1466 struct fcoe_crc_eof *cp;
1467 struct sk_buff *skb;
1468 struct fcoe_dev_stats *stats;
1469 struct fc_frame_header *fh;
1470 unsigned int hlen; /* header length implies the version */
1471 unsigned int tlen; /* trailer length */
1472 unsigned int elen; /* eth header, may include vlan */
1473 struct fcoe_port *port = lport_priv(lport);
1474 struct fcoe_interface *fcoe = port->priv;
1475 u8 sof, eof;
1476 struct fcoe_hdr *hp;
1478 WARN_ON((fr_len(fp) % sizeof(u32)) != 0);
1480 fh = fc_frame_header_get(fp);
1481 skb = fp_skb(fp);
1482 wlen = skb->len / FCOE_WORD_TO_BYTE;
1484 if (!lport->link_up) {
1485 kfree_skb(skb);
1486 return 0;
1489 if (unlikely(fh->fh_type == FC_TYPE_ELS) &&
1490 fcoe_ctlr_els_send(&fcoe->ctlr, lport, skb))
1491 return 0;
1493 sof = fr_sof(fp);
1494 eof = fr_eof(fp);
1496 elen = sizeof(struct ethhdr);
1497 hlen = sizeof(struct fcoe_hdr);
1498 tlen = sizeof(struct fcoe_crc_eof);
1499 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
1501 /* crc offload */
1502 if (likely(lport->crc_offload)) {
1503 skb->ip_summed = CHECKSUM_PARTIAL;
1504 skb->csum_start = skb_headroom(skb);
1505 skb->csum_offset = skb->len;
1506 crc = 0;
1507 } else {
1508 skb->ip_summed = CHECKSUM_NONE;
1509 crc = fcoe_fc_crc(fp);
1512 /* copy port crc and eof to the skb buff */
1513 if (skb_is_nonlinear(skb)) {
1514 skb_frag_t *frag;
1515 if (fcoe_alloc_paged_crc_eof(skb, tlen)) {
1516 kfree_skb(skb);
1517 return -ENOMEM;
1519 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
1520 cp = kmap_atomic(skb_frag_page(frag), KM_SKB_DATA_SOFTIRQ)
1521 + frag->page_offset;
1522 } else {
1523 cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
1526 memset(cp, 0, sizeof(*cp));
1527 cp->fcoe_eof = eof;
1528 cp->fcoe_crc32 = cpu_to_le32(~crc);
1530 if (skb_is_nonlinear(skb)) {
1531 kunmap_atomic(cp, KM_SKB_DATA_SOFTIRQ);
1532 cp = NULL;
1535 /* adjust skb network/transport offsets to match mac/fcoe/port */
1536 skb_push(skb, elen + hlen);
1537 skb_reset_mac_header(skb);
1538 skb_reset_network_header(skb);
1539 skb->mac_len = elen;
1540 skb->protocol = htons(ETH_P_FCOE);
1541 skb->dev = fcoe->netdev;
1543 /* fill up mac and fcoe headers */
1544 eh = eth_hdr(skb);
1545 eh->h_proto = htons(ETH_P_FCOE);
1546 memcpy(eh->h_dest, fcoe->ctlr.dest_addr, ETH_ALEN);
1547 if (fcoe->ctlr.map_dest)
1548 memcpy(eh->h_dest + 3, fh->fh_d_id, 3);
1550 if (unlikely(fcoe->ctlr.flogi_oxid != FC_XID_UNKNOWN))
1551 memcpy(eh->h_source, fcoe->ctlr.ctl_src_addr, ETH_ALEN);
1552 else
1553 memcpy(eh->h_source, port->data_src_addr, ETH_ALEN);
1555 hp = (struct fcoe_hdr *)(eh + 1);
1556 memset(hp, 0, sizeof(*hp));
1557 if (FC_FCOE_VER)
1558 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
1559 hp->fcoe_sof = sof;
1561 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
1562 if (lport->seq_offload && fr_max_payload(fp)) {
1563 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
1564 skb_shinfo(skb)->gso_size = fr_max_payload(fp);
1565 } else {
1566 skb_shinfo(skb)->gso_type = 0;
1567 skb_shinfo(skb)->gso_size = 0;
1569 /* update tx stats: regardless if LLD fails */
1570 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
1571 stats->TxFrames++;
1572 stats->TxWords += wlen;
1573 put_cpu();
1575 /* send down to lld */
1576 fr_dev(fp) = lport;
1577 fcoe_port_send(port, skb);
1578 return 0;
1582 * fcoe_percpu_flush_done() - Indicate per-CPU queue flush completion
1583 * @skb: The completed skb (argument required by destructor)
1585 static void fcoe_percpu_flush_done(struct sk_buff *skb)
1587 complete(&fcoe_flush_completion);
1591 * fcoe_filter_frames() - filter out bad fcoe frames, i.e. bad CRC
1592 * @lport: The local port the frame was received on
1593 * @fp: The received frame
1595 * Return: 0 on passing filtering checks
1597 static inline int fcoe_filter_frames(struct fc_lport *lport,
1598 struct fc_frame *fp)
1600 struct fcoe_interface *fcoe;
1601 struct fc_frame_header *fh;
1602 struct sk_buff *skb = (struct sk_buff *)fp;
1603 struct fcoe_dev_stats *stats;
1606 * We only check CRC if no offload is available and if it is
1607 * it's solicited data, in which case, the FCP layer would
1608 * check it during the copy.
1610 if (lport->crc_offload && skb->ip_summed == CHECKSUM_UNNECESSARY)
1611 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1612 else
1613 fr_flags(fp) |= FCPHF_CRC_UNCHECKED;
1615 fh = (struct fc_frame_header *) skb_transport_header(skb);
1616 fh = fc_frame_header_get(fp);
1617 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA && fh->fh_type == FC_TYPE_FCP)
1618 return 0;
1620 fcoe = ((struct fcoe_port *)lport_priv(lport))->priv;
1621 if (is_fip_mode(&fcoe->ctlr) && fc_frame_payload_op(fp) == ELS_LOGO &&
1622 ntoh24(fh->fh_s_id) == FC_FID_FLOGI) {
1623 FCOE_DBG("fcoe: dropping FCoE lport LOGO in fip mode\n");
1624 return -EINVAL;
1627 if (!(fr_flags(fp) & FCPHF_CRC_UNCHECKED) ||
1628 le32_to_cpu(fr_crc(fp)) == ~crc32(~0, skb->data, skb->len)) {
1629 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1630 return 0;
1633 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
1634 stats->InvalidCRCCount++;
1635 if (stats->InvalidCRCCount < 5)
1636 printk(KERN_WARNING "fcoe: dropping frame with CRC error\n");
1637 return -EINVAL;
1641 * fcoe_recv_frame() - process a single received frame
1642 * @skb: frame to process
1644 static void fcoe_recv_frame(struct sk_buff *skb)
1646 u32 fr_len;
1647 struct fc_lport *lport;
1648 struct fcoe_rcv_info *fr;
1649 struct fcoe_dev_stats *stats;
1650 struct fcoe_crc_eof crc_eof;
1651 struct fc_frame *fp;
1652 struct fcoe_port *port;
1653 struct fcoe_hdr *hp;
1655 fr = fcoe_dev_from_skb(skb);
1656 lport = fr->fr_dev;
1657 if (unlikely(!lport)) {
1658 if (skb->destructor != fcoe_percpu_flush_done)
1659 FCOE_NETDEV_DBG(skb->dev, "NULL lport in skb");
1660 kfree_skb(skb);
1661 return;
1664 FCOE_NETDEV_DBG(skb->dev, "skb_info: len:%d data_len:%d "
1665 "head:%p data:%p tail:%p end:%p sum:%d dev:%s",
1666 skb->len, skb->data_len,
1667 skb->head, skb->data, skb_tail_pointer(skb),
1668 skb_end_pointer(skb), skb->csum,
1669 skb->dev ? skb->dev->name : "<NULL>");
1671 port = lport_priv(lport);
1672 if (skb_is_nonlinear(skb))
1673 skb_linearize(skb); /* not ideal */
1676 * Frame length checks and setting up the header pointers
1677 * was done in fcoe_rcv already.
1679 hp = (struct fcoe_hdr *) skb_network_header(skb);
1681 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
1682 if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) {
1683 if (stats->ErrorFrames < 5)
1684 printk(KERN_WARNING "fcoe: FCoE version "
1685 "mismatch: The frame has "
1686 "version %x, but the "
1687 "initiator supports version "
1688 "%x\n", FC_FCOE_DECAPS_VER(hp),
1689 FC_FCOE_VER);
1690 goto drop;
1693 skb_pull(skb, sizeof(struct fcoe_hdr));
1694 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
1696 stats->RxFrames++;
1697 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
1699 fp = (struct fc_frame *)skb;
1700 fc_frame_init(fp);
1701 fr_dev(fp) = lport;
1702 fr_sof(fp) = hp->fcoe_sof;
1704 /* Copy out the CRC and EOF trailer for access */
1705 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof)))
1706 goto drop;
1707 fr_eof(fp) = crc_eof.fcoe_eof;
1708 fr_crc(fp) = crc_eof.fcoe_crc32;
1709 if (pskb_trim(skb, fr_len))
1710 goto drop;
1712 if (!fcoe_filter_frames(lport, fp)) {
1713 put_cpu();
1714 fc_exch_recv(lport, fp);
1715 return;
1717 drop:
1718 stats->ErrorFrames++;
1719 put_cpu();
1720 kfree_skb(skb);
1724 * fcoe_percpu_receive_thread() - The per-CPU packet receive thread
1725 * @arg: The per-CPU context
1727 * Return: 0 for success
1729 int fcoe_percpu_receive_thread(void *arg)
1731 struct fcoe_percpu_s *p = arg;
1732 struct sk_buff *skb;
1734 set_user_nice(current, -20);
1736 while (!kthread_should_stop()) {
1738 spin_lock_bh(&p->fcoe_rx_list.lock);
1739 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) == NULL) {
1740 set_current_state(TASK_INTERRUPTIBLE);
1741 spin_unlock_bh(&p->fcoe_rx_list.lock);
1742 schedule();
1743 set_current_state(TASK_RUNNING);
1744 if (kthread_should_stop())
1745 return 0;
1746 spin_lock_bh(&p->fcoe_rx_list.lock);
1748 spin_unlock_bh(&p->fcoe_rx_list.lock);
1749 fcoe_recv_frame(skb);
1751 return 0;
1755 * fcoe_dev_setup() - Setup the link change notification interface
1757 static void fcoe_dev_setup(void)
1759 register_netdevice_notifier(&fcoe_notifier);
1763 * fcoe_dev_cleanup() - Cleanup the link change notification interface
1765 static void fcoe_dev_cleanup(void)
1767 unregister_netdevice_notifier(&fcoe_notifier);
1771 * fcoe_device_notification() - Handler for net device events
1772 * @notifier: The context of the notification
1773 * @event: The type of event
1774 * @ptr: The net device that the event was on
1776 * This function is called by the Ethernet driver in case of link change event.
1778 * Returns: 0 for success
1780 static int fcoe_device_notification(struct notifier_block *notifier,
1781 ulong event, void *ptr)
1783 struct fc_lport *lport = NULL;
1784 struct net_device *netdev = ptr;
1785 struct fcoe_interface *fcoe;
1786 struct fcoe_port *port;
1787 struct fcoe_dev_stats *stats;
1788 u32 link_possible = 1;
1789 u32 mfs;
1790 int rc = NOTIFY_OK;
1792 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
1793 if (fcoe->netdev == netdev) {
1794 lport = fcoe->ctlr.lp;
1795 break;
1798 if (!lport) {
1799 rc = NOTIFY_DONE;
1800 goto out;
1803 switch (event) {
1804 case NETDEV_DOWN:
1805 case NETDEV_GOING_DOWN:
1806 link_possible = 0;
1807 break;
1808 case NETDEV_UP:
1809 case NETDEV_CHANGE:
1810 break;
1811 case NETDEV_CHANGEMTU:
1812 if (netdev->features & NETIF_F_FCOE_MTU)
1813 break;
1814 mfs = netdev->mtu - (sizeof(struct fcoe_hdr) +
1815 sizeof(struct fcoe_crc_eof));
1816 if (mfs >= FC_MIN_MAX_FRAME)
1817 fc_set_mfs(lport, mfs);
1818 break;
1819 case NETDEV_REGISTER:
1820 break;
1821 case NETDEV_UNREGISTER:
1822 list_del(&fcoe->list);
1823 port = lport_priv(fcoe->ctlr.lp);
1824 queue_work(fcoe_wq, &port->destroy_work);
1825 goto out;
1826 break;
1827 case NETDEV_FEAT_CHANGE:
1828 fcoe_netdev_features_change(lport, netdev);
1829 break;
1830 default:
1831 FCOE_NETDEV_DBG(netdev, "Unknown event %ld "
1832 "from netdev netlink\n", event);
1835 fcoe_link_speed_update(lport);
1837 if (link_possible && !fcoe_link_ok(lport))
1838 fcoe_ctlr_link_up(&fcoe->ctlr);
1839 else if (fcoe_ctlr_link_down(&fcoe->ctlr)) {
1840 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
1841 stats->LinkFailureCount++;
1842 put_cpu();
1843 fcoe_clean_pending_queue(lport);
1845 out:
1846 return rc;
1850 * fcoe_disable() - Disables a FCoE interface
1851 * @netdev : The net_device object the Ethernet interface to create on
1853 * Called from fcoe transport.
1855 * Returns: 0 for success
1857 static int fcoe_disable(struct net_device *netdev)
1859 struct fcoe_interface *fcoe;
1860 int rc = 0;
1862 mutex_lock(&fcoe_config_mutex);
1864 rtnl_lock();
1865 fcoe = fcoe_hostlist_lookup_port(netdev);
1866 rtnl_unlock();
1868 if (fcoe) {
1869 fcoe_ctlr_link_down(&fcoe->ctlr);
1870 fcoe_clean_pending_queue(fcoe->ctlr.lp);
1871 } else
1872 rc = -ENODEV;
1874 mutex_unlock(&fcoe_config_mutex);
1875 return rc;
1879 * fcoe_enable() - Enables a FCoE interface
1880 * @netdev : The net_device object the Ethernet interface to create on
1882 * Called from fcoe transport.
1884 * Returns: 0 for success
1886 static int fcoe_enable(struct net_device *netdev)
1888 struct fcoe_interface *fcoe;
1889 int rc = 0;
1891 mutex_lock(&fcoe_config_mutex);
1892 rtnl_lock();
1893 fcoe = fcoe_hostlist_lookup_port(netdev);
1894 rtnl_unlock();
1896 if (!fcoe)
1897 rc = -ENODEV;
1898 else if (!fcoe_link_ok(fcoe->ctlr.lp))
1899 fcoe_ctlr_link_up(&fcoe->ctlr);
1901 mutex_unlock(&fcoe_config_mutex);
1902 return rc;
1906 * fcoe_destroy() - Destroy a FCoE interface
1907 * @netdev : The net_device object the Ethernet interface to create on
1909 * Called from fcoe transport
1911 * Returns: 0 for success
1913 static int fcoe_destroy(struct net_device *netdev)
1915 struct fcoe_interface *fcoe;
1916 struct fc_lport *lport;
1917 struct fcoe_port *port;
1918 int rc = 0;
1920 mutex_lock(&fcoe_config_mutex);
1921 rtnl_lock();
1922 fcoe = fcoe_hostlist_lookup_port(netdev);
1923 if (!fcoe) {
1924 rc = -ENODEV;
1925 goto out_nodev;
1927 lport = fcoe->ctlr.lp;
1928 port = lport_priv(lport);
1929 list_del(&fcoe->list);
1930 queue_work(fcoe_wq, &port->destroy_work);
1931 out_nodev:
1932 rtnl_unlock();
1933 mutex_unlock(&fcoe_config_mutex);
1934 return rc;
1938 * fcoe_destroy_work() - Destroy a FCoE port in a deferred work context
1939 * @work: Handle to the FCoE port to be destroyed
1941 static void fcoe_destroy_work(struct work_struct *work)
1943 struct fcoe_port *port;
1944 struct fcoe_interface *fcoe;
1945 int npiv = 0;
1947 port = container_of(work, struct fcoe_port, destroy_work);
1948 mutex_lock(&fcoe_config_mutex);
1950 /* set if this is an NPIV port */
1951 npiv = port->lport->vport ? 1 : 0;
1953 fcoe = port->priv;
1954 fcoe_if_destroy(port->lport);
1956 /* Do not tear down the fcoe interface for NPIV port */
1957 if (!npiv)
1958 fcoe_interface_cleanup(fcoe);
1960 mutex_unlock(&fcoe_config_mutex);
1964 * fcoe_match() - Check if the FCoE is supported on the given netdevice
1965 * @netdev : The net_device object the Ethernet interface to create on
1967 * Called from fcoe transport.
1969 * Returns: always returns true as this is the default FCoE transport,
1970 * i.e., support all netdevs.
1972 static bool fcoe_match(struct net_device *netdev)
1974 return true;
1978 * fcoe_create() - Create a fcoe interface
1979 * @netdev : The net_device object the Ethernet interface to create on
1980 * @fip_mode: The FIP mode for this creation
1982 * Called from fcoe transport
1984 * Returns: 0 for success
1986 static int fcoe_create(struct net_device *netdev, enum fip_state fip_mode)
1988 int rc = 0;
1989 struct fcoe_interface *fcoe;
1990 struct fc_lport *lport;
1992 mutex_lock(&fcoe_config_mutex);
1993 rtnl_lock();
1995 /* look for existing lport */
1996 if (fcoe_hostlist_lookup(netdev)) {
1997 rc = -EEXIST;
1998 goto out_nodev;
2001 fcoe = fcoe_interface_create(netdev, fip_mode);
2002 if (IS_ERR(fcoe)) {
2003 rc = PTR_ERR(fcoe);
2004 goto out_nodev;
2007 lport = fcoe_if_create(fcoe, &netdev->dev, 0);
2008 if (IS_ERR(lport)) {
2009 printk(KERN_ERR "fcoe: Failed to create interface (%s)\n",
2010 netdev->name);
2011 rc = -EIO;
2012 rtnl_unlock();
2013 fcoe_interface_cleanup(fcoe);
2014 goto out_nortnl;
2017 /* Make this the "master" N_Port */
2018 fcoe->ctlr.lp = lport;
2020 /* add to lports list */
2021 fcoe_hostlist_add(lport);
2023 /* start FIP Discovery and FLOGI */
2024 lport->boot_time = jiffies;
2025 fc_fabric_login(lport);
2026 if (!fcoe_link_ok(lport))
2027 fcoe_ctlr_link_up(&fcoe->ctlr);
2029 out_nodev:
2030 rtnl_unlock();
2031 out_nortnl:
2032 mutex_unlock(&fcoe_config_mutex);
2033 return rc;
2037 * fcoe_link_speed_update() - Update the supported and actual link speeds
2038 * @lport: The local port to update speeds for
2040 * Returns: 0 if the ethtool query was successful
2041 * -1 if the ethtool query failed
2043 int fcoe_link_speed_update(struct fc_lport *lport)
2045 struct net_device *netdev = fcoe_netdev(lport);
2046 struct ethtool_cmd ecmd;
2048 if (!__ethtool_get_settings(netdev, &ecmd)) {
2049 lport->link_supported_speeds &=
2050 ~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
2051 if (ecmd.supported & (SUPPORTED_1000baseT_Half |
2052 SUPPORTED_1000baseT_Full))
2053 lport->link_supported_speeds |= FC_PORTSPEED_1GBIT;
2054 if (ecmd.supported & SUPPORTED_10000baseT_Full)
2055 lport->link_supported_speeds |=
2056 FC_PORTSPEED_10GBIT;
2057 switch (ethtool_cmd_speed(&ecmd)) {
2058 case SPEED_1000:
2059 lport->link_speed = FC_PORTSPEED_1GBIT;
2060 break;
2061 case SPEED_10000:
2062 lport->link_speed = FC_PORTSPEED_10GBIT;
2063 break;
2065 return 0;
2067 return -1;
2071 * fcoe_link_ok() - Check if the link is OK for a local port
2072 * @lport: The local port to check link on
2074 * Returns: 0 if link is UP and OK, -1 if not
2077 int fcoe_link_ok(struct fc_lport *lport)
2079 struct net_device *netdev = fcoe_netdev(lport);
2081 if (netif_oper_up(netdev))
2082 return 0;
2083 return -1;
2087 * fcoe_percpu_clean() - Clear all pending skbs for an local port
2088 * @lport: The local port whose skbs are to be cleared
2090 * Must be called with fcoe_create_mutex held to single-thread completion.
2092 * This flushes the pending skbs by adding a new skb to each queue and
2093 * waiting until they are all freed. This assures us that not only are
2094 * there no packets that will be handled by the lport, but also that any
2095 * threads already handling packet have returned.
2097 void fcoe_percpu_clean(struct fc_lport *lport)
2099 struct fcoe_percpu_s *pp;
2100 struct fcoe_rcv_info *fr;
2101 struct sk_buff_head *list;
2102 struct sk_buff *skb, *next;
2103 struct sk_buff *head;
2104 unsigned int cpu;
2106 for_each_possible_cpu(cpu) {
2107 pp = &per_cpu(fcoe_percpu, cpu);
2108 spin_lock_bh(&pp->fcoe_rx_list.lock);
2109 list = &pp->fcoe_rx_list;
2110 head = list->next;
2111 for (skb = head; skb != (struct sk_buff *)list;
2112 skb = next) {
2113 next = skb->next;
2114 fr = fcoe_dev_from_skb(skb);
2115 if (fr->fr_dev == lport) {
2116 __skb_unlink(skb, list);
2117 kfree_skb(skb);
2121 if (!pp->thread || !cpu_online(cpu)) {
2122 spin_unlock_bh(&pp->fcoe_rx_list.lock);
2123 continue;
2126 skb = dev_alloc_skb(0);
2127 if (!skb) {
2128 spin_unlock_bh(&pp->fcoe_rx_list.lock);
2129 continue;
2131 skb->destructor = fcoe_percpu_flush_done;
2133 __skb_queue_tail(&pp->fcoe_rx_list, skb);
2134 if (pp->fcoe_rx_list.qlen == 1)
2135 wake_up_process(pp->thread);
2136 spin_unlock_bh(&pp->fcoe_rx_list.lock);
2138 wait_for_completion(&fcoe_flush_completion);
2143 * fcoe_reset() - Reset a local port
2144 * @shost: The SCSI host associated with the local port to be reset
2146 * Returns: Always 0 (return value required by FC transport template)
2148 int fcoe_reset(struct Scsi_Host *shost)
2150 struct fc_lport *lport = shost_priv(shost);
2151 struct fcoe_port *port = lport_priv(lport);
2152 struct fcoe_interface *fcoe = port->priv;
2154 fcoe_ctlr_link_down(&fcoe->ctlr);
2155 fcoe_clean_pending_queue(fcoe->ctlr.lp);
2156 if (!fcoe_link_ok(fcoe->ctlr.lp))
2157 fcoe_ctlr_link_up(&fcoe->ctlr);
2158 return 0;
2162 * fcoe_hostlist_lookup_port() - Find the FCoE interface associated with a net device
2163 * @netdev: The net device used as a key
2165 * Locking: Must be called with the RNL mutex held.
2167 * Returns: NULL or the FCoE interface
2169 static struct fcoe_interface *
2170 fcoe_hostlist_lookup_port(const struct net_device *netdev)
2172 struct fcoe_interface *fcoe;
2174 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
2175 if (fcoe->netdev == netdev)
2176 return fcoe;
2178 return NULL;
2182 * fcoe_hostlist_lookup() - Find the local port associated with a
2183 * given net device
2184 * @netdev: The netdevice used as a key
2186 * Locking: Must be called with the RTNL mutex held
2188 * Returns: NULL or the local port
2190 static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *netdev)
2192 struct fcoe_interface *fcoe;
2194 fcoe = fcoe_hostlist_lookup_port(netdev);
2195 return (fcoe) ? fcoe->ctlr.lp : NULL;
2199 * fcoe_hostlist_add() - Add the FCoE interface identified by a local
2200 * port to the hostlist
2201 * @lport: The local port that identifies the FCoE interface to be added
2203 * Locking: must be called with the RTNL mutex held
2205 * Returns: 0 for success
2207 static int fcoe_hostlist_add(const struct fc_lport *lport)
2209 struct fcoe_interface *fcoe;
2210 struct fcoe_port *port;
2212 fcoe = fcoe_hostlist_lookup_port(fcoe_netdev(lport));
2213 if (!fcoe) {
2214 port = lport_priv(lport);
2215 fcoe = port->priv;
2216 list_add_tail(&fcoe->list, &fcoe_hostlist);
2218 return 0;
2222 static struct fcoe_transport fcoe_sw_transport = {
2223 .name = {FCOE_TRANSPORT_DEFAULT},
2224 .attached = false,
2225 .list = LIST_HEAD_INIT(fcoe_sw_transport.list),
2226 .match = fcoe_match,
2227 .create = fcoe_create,
2228 .destroy = fcoe_destroy,
2229 .enable = fcoe_enable,
2230 .disable = fcoe_disable,
2234 * fcoe_init() - Initialize fcoe.ko
2236 * Returns: 0 on success, or a negative value on failure
2238 static int __init fcoe_init(void)
2240 struct fcoe_percpu_s *p;
2241 unsigned int cpu;
2242 int rc = 0;
2244 fcoe_wq = alloc_workqueue("fcoe", 0, 0);
2245 if (!fcoe_wq)
2246 return -ENOMEM;
2248 /* register as a fcoe transport */
2249 rc = fcoe_transport_attach(&fcoe_sw_transport);
2250 if (rc) {
2251 printk(KERN_ERR "failed to register an fcoe transport, check "
2252 "if libfcoe is loaded\n");
2253 return rc;
2256 mutex_lock(&fcoe_config_mutex);
2258 for_each_possible_cpu(cpu) {
2259 p = &per_cpu(fcoe_percpu, cpu);
2260 skb_queue_head_init(&p->fcoe_rx_list);
2263 for_each_online_cpu(cpu)
2264 fcoe_percpu_thread_create(cpu);
2266 /* Initialize per CPU interrupt thread */
2267 rc = register_hotcpu_notifier(&fcoe_cpu_notifier);
2268 if (rc)
2269 goto out_free;
2271 /* Setup link change notification */
2272 fcoe_dev_setup();
2274 rc = fcoe_if_init();
2275 if (rc)
2276 goto out_free;
2278 mutex_unlock(&fcoe_config_mutex);
2279 return 0;
2281 out_free:
2282 for_each_online_cpu(cpu) {
2283 fcoe_percpu_thread_destroy(cpu);
2285 mutex_unlock(&fcoe_config_mutex);
2286 destroy_workqueue(fcoe_wq);
2287 return rc;
2289 module_init(fcoe_init);
2292 * fcoe_exit() - Clean up fcoe.ko
2294 * Returns: 0 on success or a negative value on failure
2296 static void __exit fcoe_exit(void)
2298 struct fcoe_interface *fcoe, *tmp;
2299 struct fcoe_port *port;
2300 unsigned int cpu;
2302 mutex_lock(&fcoe_config_mutex);
2304 fcoe_dev_cleanup();
2306 /* releases the associated fcoe hosts */
2307 rtnl_lock();
2308 list_for_each_entry_safe(fcoe, tmp, &fcoe_hostlist, list) {
2309 list_del(&fcoe->list);
2310 port = lport_priv(fcoe->ctlr.lp);
2311 queue_work(fcoe_wq, &port->destroy_work);
2313 rtnl_unlock();
2315 unregister_hotcpu_notifier(&fcoe_cpu_notifier);
2317 for_each_online_cpu(cpu)
2318 fcoe_percpu_thread_destroy(cpu);
2320 mutex_unlock(&fcoe_config_mutex);
2323 * destroy_work's may be chained but destroy_workqueue()
2324 * can take care of them. Just kill the fcoe_wq.
2326 destroy_workqueue(fcoe_wq);
2329 * Detaching from the scsi transport must happen after all
2330 * destroys are done on the fcoe_wq. destroy_workqueue will
2331 * enusre the fcoe_wq is flushed.
2333 fcoe_if_exit();
2335 /* detach from fcoe transport */
2336 fcoe_transport_detach(&fcoe_sw_transport);
2338 module_exit(fcoe_exit);
2341 * fcoe_flogi_resp() - FCoE specific FLOGI and FDISC response handler
2342 * @seq: active sequence in the FLOGI or FDISC exchange
2343 * @fp: response frame, or error encoded in a pointer (timeout)
2344 * @arg: pointer the the fcoe_ctlr structure
2346 * This handles MAC address management for FCoE, then passes control on to
2347 * the libfc FLOGI response handler.
2349 static void fcoe_flogi_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
2351 struct fcoe_ctlr *fip = arg;
2352 struct fc_exch *exch = fc_seq_exch(seq);
2353 struct fc_lport *lport = exch->lp;
2354 u8 *mac;
2356 if (IS_ERR(fp))
2357 goto done;
2359 mac = fr_cb(fp)->granted_mac;
2360 if (is_zero_ether_addr(mac)) {
2361 /* pre-FIP */
2362 if (fcoe_ctlr_recv_flogi(fip, lport, fp)) {
2363 fc_frame_free(fp);
2364 return;
2367 fcoe_update_src_mac(lport, mac);
2368 done:
2369 fc_lport_flogi_resp(seq, fp, lport);
2373 * fcoe_logo_resp() - FCoE specific LOGO response handler
2374 * @seq: active sequence in the LOGO exchange
2375 * @fp: response frame, or error encoded in a pointer (timeout)
2376 * @arg: pointer the the fcoe_ctlr structure
2378 * This handles MAC address management for FCoE, then passes control on to
2379 * the libfc LOGO response handler.
2381 static void fcoe_logo_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
2383 struct fc_lport *lport = arg;
2384 static u8 zero_mac[ETH_ALEN] = { 0 };
2386 if (!IS_ERR(fp))
2387 fcoe_update_src_mac(lport, zero_mac);
2388 fc_lport_logo_resp(seq, fp, lport);
2392 * fcoe_elsct_send - FCoE specific ELS handler
2394 * This does special case handling of FIP encapsualted ELS exchanges for FCoE,
2395 * using FCoE specific response handlers and passing the FIP controller as
2396 * the argument (the lport is still available from the exchange).
2398 * Most of the work here is just handed off to the libfc routine.
2400 static struct fc_seq *fcoe_elsct_send(struct fc_lport *lport, u32 did,
2401 struct fc_frame *fp, unsigned int op,
2402 void (*resp)(struct fc_seq *,
2403 struct fc_frame *,
2404 void *),
2405 void *arg, u32 timeout)
2407 struct fcoe_port *port = lport_priv(lport);
2408 struct fcoe_interface *fcoe = port->priv;
2409 struct fcoe_ctlr *fip = &fcoe->ctlr;
2410 struct fc_frame_header *fh = fc_frame_header_get(fp);
2412 switch (op) {
2413 case ELS_FLOGI:
2414 case ELS_FDISC:
2415 if (lport->point_to_multipoint)
2416 break;
2417 return fc_elsct_send(lport, did, fp, op, fcoe_flogi_resp,
2418 fip, timeout);
2419 case ELS_LOGO:
2420 /* only hook onto fabric logouts, not port logouts */
2421 if (ntoh24(fh->fh_d_id) != FC_FID_FLOGI)
2422 break;
2423 return fc_elsct_send(lport, did, fp, op, fcoe_logo_resp,
2424 lport, timeout);
2426 return fc_elsct_send(lport, did, fp, op, resp, arg, timeout);
2430 * fcoe_vport_create() - create an fc_host/scsi_host for a vport
2431 * @vport: fc_vport object to create a new fc_host for
2432 * @disabled: start the new fc_host in a disabled state by default?
2434 * Returns: 0 for success
2436 static int fcoe_vport_create(struct fc_vport *vport, bool disabled)
2438 struct Scsi_Host *shost = vport_to_shost(vport);
2439 struct fc_lport *n_port = shost_priv(shost);
2440 struct fcoe_port *port = lport_priv(n_port);
2441 struct fcoe_interface *fcoe = port->priv;
2442 struct net_device *netdev = fcoe->netdev;
2443 struct fc_lport *vn_port;
2444 int rc;
2445 char buf[32];
2447 rc = fcoe_validate_vport_create(vport);
2448 if (rc) {
2449 wwn_to_str(vport->port_name, buf, sizeof(buf));
2450 printk(KERN_ERR "fcoe: Failed to create vport, "
2451 "WWPN (0x%s) already exists\n",
2452 buf);
2453 return rc;
2456 mutex_lock(&fcoe_config_mutex);
2457 rtnl_lock();
2458 vn_port = fcoe_if_create(fcoe, &vport->dev, 1);
2459 rtnl_unlock();
2460 mutex_unlock(&fcoe_config_mutex);
2462 if (IS_ERR(vn_port)) {
2463 printk(KERN_ERR "fcoe: fcoe_vport_create(%s) failed\n",
2464 netdev->name);
2465 return -EIO;
2468 if (disabled) {
2469 fc_vport_set_state(vport, FC_VPORT_DISABLED);
2470 } else {
2471 vn_port->boot_time = jiffies;
2472 fc_fabric_login(vn_port);
2473 fc_vport_setlink(vn_port);
2475 return 0;
2479 * fcoe_vport_destroy() - destroy the fc_host/scsi_host for a vport
2480 * @vport: fc_vport object that is being destroyed
2482 * Returns: 0 for success
2484 static int fcoe_vport_destroy(struct fc_vport *vport)
2486 struct Scsi_Host *shost = vport_to_shost(vport);
2487 struct fc_lport *n_port = shost_priv(shost);
2488 struct fc_lport *vn_port = vport->dd_data;
2489 struct fcoe_port *port = lport_priv(vn_port);
2491 mutex_lock(&n_port->lp_mutex);
2492 list_del(&vn_port->list);
2493 mutex_unlock(&n_port->lp_mutex);
2494 queue_work(fcoe_wq, &port->destroy_work);
2495 return 0;
2499 * fcoe_vport_disable() - change vport state
2500 * @vport: vport to bring online/offline
2501 * @disable: should the vport be disabled?
2503 static int fcoe_vport_disable(struct fc_vport *vport, bool disable)
2505 struct fc_lport *lport = vport->dd_data;
2507 if (disable) {
2508 fc_vport_set_state(vport, FC_VPORT_DISABLED);
2509 fc_fabric_logoff(lport);
2510 } else {
2511 lport->boot_time = jiffies;
2512 fc_fabric_login(lport);
2513 fc_vport_setlink(lport);
2516 return 0;
2520 * fcoe_vport_set_symbolic_name() - append vport string to symbolic name
2521 * @vport: fc_vport with a new symbolic name string
2523 * After generating a new symbolic name string, a new RSPN_ID request is
2524 * sent to the name server. There is no response handler, so if it fails
2525 * for some reason it will not be retried.
2527 static void fcoe_set_vport_symbolic_name(struct fc_vport *vport)
2529 struct fc_lport *lport = vport->dd_data;
2530 struct fc_frame *fp;
2531 size_t len;
2533 snprintf(fc_host_symbolic_name(lport->host), FC_SYMBOLIC_NAME_SIZE,
2534 "%s v%s over %s : %s", FCOE_NAME, FCOE_VERSION,
2535 fcoe_netdev(lport)->name, vport->symbolic_name);
2537 if (lport->state != LPORT_ST_READY)
2538 return;
2540 len = strnlen(fc_host_symbolic_name(lport->host), 255);
2541 fp = fc_frame_alloc(lport,
2542 sizeof(struct fc_ct_hdr) +
2543 sizeof(struct fc_ns_rspn) + len);
2544 if (!fp)
2545 return;
2546 lport->tt.elsct_send(lport, FC_FID_DIR_SERV, fp, FC_NS_RSPN_ID,
2547 NULL, NULL, 3 * lport->r_a_tov);
2551 * fcoe_get_lesb() - Fill the FCoE Link Error Status Block
2552 * @lport: the local port
2553 * @fc_lesb: the link error status block
2555 static void fcoe_get_lesb(struct fc_lport *lport,
2556 struct fc_els_lesb *fc_lesb)
2558 unsigned int cpu;
2559 u32 lfc, vlfc, mdac;
2560 struct fcoe_dev_stats *devst;
2561 struct fcoe_fc_els_lesb *lesb;
2562 struct rtnl_link_stats64 temp;
2563 struct net_device *netdev = fcoe_netdev(lport);
2565 lfc = 0;
2566 vlfc = 0;
2567 mdac = 0;
2568 lesb = (struct fcoe_fc_els_lesb *)fc_lesb;
2569 memset(lesb, 0, sizeof(*lesb));
2570 for_each_possible_cpu(cpu) {
2571 devst = per_cpu_ptr(lport->dev_stats, cpu);
2572 lfc += devst->LinkFailureCount;
2573 vlfc += devst->VLinkFailureCount;
2574 mdac += devst->MissDiscAdvCount;
2576 lesb->lesb_link_fail = htonl(lfc);
2577 lesb->lesb_vlink_fail = htonl(vlfc);
2578 lesb->lesb_miss_fka = htonl(mdac);
2579 lesb->lesb_fcs_error = htonl(dev_get_stats(netdev, &temp)->rx_crc_errors);
2583 * fcoe_set_port_id() - Callback from libfc when Port_ID is set.
2584 * @lport: the local port
2585 * @port_id: the port ID
2586 * @fp: the received frame, if any, that caused the port_id to be set.
2588 * This routine handles the case where we received a FLOGI and are
2589 * entering point-to-point mode. We need to call fcoe_ctlr_recv_flogi()
2590 * so it can set the non-mapped mode and gateway address.
2592 * The FLOGI LS_ACC is handled by fcoe_flogi_resp().
2594 static void fcoe_set_port_id(struct fc_lport *lport,
2595 u32 port_id, struct fc_frame *fp)
2597 struct fcoe_port *port = lport_priv(lport);
2598 struct fcoe_interface *fcoe = port->priv;
2600 if (fp && fc_frame_payload_op(fp) == ELS_FLOGI)
2601 fcoe_ctlr_recv_flogi(&fcoe->ctlr, lport, fp);
2605 * fcoe_validate_vport_create() - Validate a vport before creating it
2606 * @vport: NPIV port to be created
2608 * This routine is meant to add validation for a vport before creating it
2609 * via fcoe_vport_create().
2610 * Current validations are:
2611 * - WWPN supplied is unique for given lport
2615 static int fcoe_validate_vport_create(struct fc_vport *vport)
2617 struct Scsi_Host *shost = vport_to_shost(vport);
2618 struct fc_lport *n_port = shost_priv(shost);
2619 struct fc_lport *vn_port;
2620 int rc = 0;
2621 char buf[32];
2623 mutex_lock(&n_port->lp_mutex);
2625 wwn_to_str(vport->port_name, buf, sizeof(buf));
2626 /* Check if the wwpn is not same as that of the lport */
2627 if (!memcmp(&n_port->wwpn, &vport->port_name, sizeof(u64))) {
2628 FCOE_DBG("vport WWPN 0x%s is same as that of the "
2629 "base port WWPN\n", buf);
2630 rc = -EINVAL;
2631 goto out;
2634 /* Check if there is any existing vport with same wwpn */
2635 list_for_each_entry(vn_port, &n_port->vports, list) {
2636 if (!memcmp(&vn_port->wwpn, &vport->port_name, sizeof(u64))) {
2637 FCOE_DBG("vport with given WWPN 0x%s already "
2638 "exists\n", buf);
2639 rc = -EINVAL;
2640 break;
2644 out:
2645 mutex_unlock(&n_port->lp_mutex);
2647 return rc;