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
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>
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>
42 #include <scsi/fc/fc_encaps.h>
43 #include <scsi/fc/fc_fip.h>
44 #include <scsi/fc/fc_fcoe.h>
46 #include <scsi/libfc.h>
47 #include <scsi/fc_frame.h>
48 #include <scsi/libfcoe.h>
52 MODULE_AUTHOR("Open-FCoE.org");
53 MODULE_DESCRIPTION("FCoE");
54 MODULE_LICENSE("GPL v2");
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).");
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");
66 static DEFINE_MUTEX(fcoe_config_mutex);
68 static struct workqueue_struct *fcoe_wq;
70 /* fcoe_percpu_clean completion. Waiter protected by fcoe_create_mutex */
71 static DECLARE_COMPLETION(fcoe_flush_completion);
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);
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_ok(struct fc_lport *);
87 static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *);
88 static int fcoe_hostlist_add(const struct fc_lport *);
89 static void fcoe_hostlist_del(const struct fc_lport *);
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 *);
97 static int fcoe_fip_recv(struct sk_buff *, struct net_device *,
98 struct packet_type *, struct net_device *);
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 *);
105 static int fcoe_ddp_setup(struct fc_lport *, u16, struct scatterlist *,
107 static int fcoe_ddp_done(struct fc_lport *, u16);
108 static int fcoe_ddp_target(struct fc_lport *, u16, struct scatterlist *,
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);
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);
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);
125 static struct fc_seq *fcoe_elsct_send(struct fc_lport *,
126 u32 did, struct fc_frame *,
128 void (*resp)(struct fc_seq *,
131 void *, u32 timeout);
132 static void fcoe_recv_frame(struct sk_buff *skb);
134 /* notification function for packets from net device */
135 static struct notifier_block fcoe_notifier = {
136 .notifier_call = fcoe_device_notification,
139 /* notification function for CPU hotplug events */
140 static struct notifier_block fcoe_cpu_notifier = {
141 .notifier_call = fcoe_cpu_callback,
144 /* notification function for DCB events */
145 static struct notifier_block dcb_notifier = {
146 .notifier_call = fcoe_dcb_app_notification,
149 static struct scsi_transport_template *fcoe_nport_scsi_transport;
150 static struct scsi_transport_template *fcoe_vport_scsi_transport;
152 static int fcoe_vport_destroy(struct fc_vport *);
153 static int fcoe_vport_create(struct fc_vport *, bool disabled);
154 static int fcoe_vport_disable(struct fc_vport *, bool disable);
155 static void fcoe_set_vport_symbolic_name(struct fc_vport *);
156 static void fcoe_set_port_id(struct fc_lport *, u32, struct fc_frame *);
157 static void fcoe_fcf_get_vlan_id(struct fcoe_fcf_device *);
159 static struct fcoe_sysfs_function_template fcoe_sysfs_templ = {
160 .set_fcoe_ctlr_mode = fcoe_ctlr_set_fip_mode,
161 .set_fcoe_ctlr_enabled = fcoe_ctlr_enabled,
162 .get_fcoe_ctlr_link_fail = fcoe_ctlr_get_lesb,
163 .get_fcoe_ctlr_vlink_fail = fcoe_ctlr_get_lesb,
164 .get_fcoe_ctlr_miss_fka = fcoe_ctlr_get_lesb,
165 .get_fcoe_ctlr_symb_err = fcoe_ctlr_get_lesb,
166 .get_fcoe_ctlr_err_block = fcoe_ctlr_get_lesb,
167 .get_fcoe_ctlr_fcs_error = fcoe_ctlr_get_lesb,
169 .get_fcoe_fcf_selected = fcoe_fcf_get_selected,
170 .get_fcoe_fcf_vlan_id = fcoe_fcf_get_vlan_id,
173 static struct libfc_function_template fcoe_libfc_fcn_templ = {
174 .frame_send = fcoe_xmit,
175 .ddp_setup = fcoe_ddp_setup,
176 .ddp_done = fcoe_ddp_done,
177 .ddp_target = fcoe_ddp_target,
178 .elsct_send = fcoe_elsct_send,
179 .get_lesb = fcoe_get_lesb,
180 .lport_set_port_id = fcoe_set_port_id,
183 static struct fc_function_template fcoe_nport_fc_functions = {
184 .show_host_node_name = 1,
185 .show_host_port_name = 1,
186 .show_host_supported_classes = 1,
187 .show_host_supported_fc4s = 1,
188 .show_host_active_fc4s = 1,
189 .show_host_maxframe_size = 1,
190 .show_host_serial_number = 1,
191 .show_host_manufacturer = 1,
192 .show_host_model = 1,
193 .show_host_model_description = 1,
194 .show_host_hardware_version = 1,
195 .show_host_driver_version = 1,
196 .show_host_firmware_version = 1,
197 .show_host_optionrom_version = 1,
199 .show_host_port_id = 1,
200 .show_host_supported_speeds = 1,
201 .get_host_speed = fc_get_host_speed,
202 .show_host_speed = 1,
203 .show_host_port_type = 1,
204 .get_host_port_state = fc_get_host_port_state,
205 .show_host_port_state = 1,
206 .show_host_symbolic_name = 1,
208 .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
209 .show_rport_maxframe_size = 1,
210 .show_rport_supported_classes = 1,
212 .show_host_fabric_name = 1,
213 .show_starget_node_name = 1,
214 .show_starget_port_name = 1,
215 .show_starget_port_id = 1,
216 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
217 .show_rport_dev_loss_tmo = 1,
218 .get_fc_host_stats = fc_get_host_stats,
219 .issue_fc_host_lip = fcoe_reset,
221 .terminate_rport_io = fc_rport_terminate_io,
223 .vport_create = fcoe_vport_create,
224 .vport_delete = fcoe_vport_destroy,
225 .vport_disable = fcoe_vport_disable,
226 .set_vport_symbolic_name = fcoe_set_vport_symbolic_name,
228 .bsg_request = fc_lport_bsg_request,
231 static struct fc_function_template fcoe_vport_fc_functions = {
232 .show_host_node_name = 1,
233 .show_host_port_name = 1,
234 .show_host_supported_classes = 1,
235 .show_host_supported_fc4s = 1,
236 .show_host_active_fc4s = 1,
237 .show_host_maxframe_size = 1,
238 .show_host_serial_number = 1,
239 .show_host_manufacturer = 1,
240 .show_host_model = 1,
241 .show_host_model_description = 1,
242 .show_host_hardware_version = 1,
243 .show_host_driver_version = 1,
244 .show_host_firmware_version = 1,
245 .show_host_optionrom_version = 1,
247 .show_host_port_id = 1,
248 .show_host_supported_speeds = 1,
249 .get_host_speed = fc_get_host_speed,
250 .show_host_speed = 1,
251 .show_host_port_type = 1,
252 .get_host_port_state = fc_get_host_port_state,
253 .show_host_port_state = 1,
254 .show_host_symbolic_name = 1,
256 .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
257 .show_rport_maxframe_size = 1,
258 .show_rport_supported_classes = 1,
260 .show_host_fabric_name = 1,
261 .show_starget_node_name = 1,
262 .show_starget_port_name = 1,
263 .show_starget_port_id = 1,
264 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
265 .show_rport_dev_loss_tmo = 1,
266 .get_fc_host_stats = fc_get_host_stats,
267 .issue_fc_host_lip = fcoe_reset,
269 .terminate_rport_io = fc_rport_terminate_io,
271 .bsg_request = fc_lport_bsg_request,
274 static struct scsi_host_template fcoe_shost_template = {
275 .module = THIS_MODULE,
276 .name = "FCoE Driver",
277 .proc_name = FCOE_NAME,
278 .queuecommand = fc_queuecommand,
279 .eh_abort_handler = fc_eh_abort,
280 .eh_device_reset_handler = fc_eh_device_reset,
281 .eh_host_reset_handler = fc_eh_host_reset,
282 .slave_alloc = fc_slave_alloc,
283 .change_queue_depth = fc_change_queue_depth,
284 .change_queue_type = fc_change_queue_type,
287 .can_queue = FCOE_MAX_OUTSTANDING_COMMANDS,
288 .use_clustering = ENABLE_CLUSTERING,
289 .sg_tablesize = SG_ALL,
290 .max_sectors = 0xffff,
294 * fcoe_interface_setup() - Setup a FCoE interface
295 * @fcoe: The new FCoE interface
296 * @netdev: The net device that the fcoe interface is on
298 * Returns : 0 for success
299 * Locking: must be called with the RTNL mutex held
301 static int fcoe_interface_setup(struct fcoe_interface *fcoe,
302 struct net_device *netdev)
304 struct fcoe_ctlr *fip = fcoe_to_ctlr(fcoe);
305 struct netdev_hw_addr *ha;
306 struct net_device *real_dev;
307 u8 flogi_maddr[ETH_ALEN];
308 const struct net_device_ops *ops;
310 fcoe->netdev = netdev;
312 /* Let LLD initialize for FCoE */
313 ops = netdev->netdev_ops;
314 if (ops->ndo_fcoe_enable) {
315 if (ops->ndo_fcoe_enable(netdev))
316 FCOE_NETDEV_DBG(netdev, "Failed to enable FCoE"
317 " specific feature for LLD.\n");
320 /* Do not support for bonding device */
321 if (netdev->priv_flags & IFF_BONDING && netdev->flags & IFF_MASTER) {
322 FCOE_NETDEV_DBG(netdev, "Bonded interfaces not supported\n");
326 /* look for SAN MAC address, if multiple SAN MACs exist, only
327 * use the first one for SPMA */
328 real_dev = (netdev->priv_flags & IFF_802_1Q_VLAN) ?
329 vlan_dev_real_dev(netdev) : netdev;
330 fcoe->realdev = real_dev;
332 for_each_dev_addr(real_dev, ha) {
333 if ((ha->type == NETDEV_HW_ADDR_T_SAN) &&
334 (is_valid_ether_addr(ha->addr))) {
335 memcpy(fip->ctl_src_addr, ha->addr, ETH_ALEN);
342 /* setup Source Mac Address */
344 memcpy(fip->ctl_src_addr, netdev->dev_addr, netdev->addr_len);
347 * Add FCoE MAC address as second unicast MAC address
348 * or enter promiscuous mode if not capable of listening
349 * for multiple unicast MACs.
351 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
352 dev_uc_add(netdev, flogi_maddr);
354 dev_uc_add(netdev, fip->ctl_src_addr);
355 if (fip->mode == FIP_MODE_VN2VN) {
356 dev_mc_add(netdev, FIP_ALL_VN2VN_MACS);
357 dev_mc_add(netdev, FIP_ALL_P2P_MACS);
359 dev_mc_add(netdev, FIP_ALL_ENODE_MACS);
362 * setup the receive function from ethernet driver
363 * on the ethertype for the given device
365 fcoe->fcoe_packet_type.func = fcoe_rcv;
366 fcoe->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
367 fcoe->fcoe_packet_type.dev = netdev;
368 dev_add_pack(&fcoe->fcoe_packet_type);
370 fcoe->fip_packet_type.func = fcoe_fip_recv;
371 fcoe->fip_packet_type.type = htons(ETH_P_FIP);
372 fcoe->fip_packet_type.dev = netdev;
373 dev_add_pack(&fcoe->fip_packet_type);
379 * fcoe_interface_create() - Create a FCoE interface on a net device
380 * @netdev: The net device to create the FCoE interface on
381 * @fip_mode: The mode to use for FIP
383 * Returns: pointer to a struct fcoe_interface or NULL on error
385 static struct fcoe_interface *fcoe_interface_create(struct net_device *netdev,
386 enum fip_state fip_mode)
388 struct fcoe_ctlr_device *ctlr_dev;
389 struct fcoe_ctlr *ctlr;
390 struct fcoe_interface *fcoe;
394 if (!try_module_get(THIS_MODULE)) {
395 FCOE_NETDEV_DBG(netdev,
396 "Could not get a reference to the module\n");
397 fcoe = ERR_PTR(-EBUSY);
401 size = sizeof(struct fcoe_ctlr) + sizeof(struct fcoe_interface);
402 ctlr_dev = fcoe_ctlr_device_add(&netdev->dev, &fcoe_sysfs_templ,
405 FCOE_DBG("Failed to add fcoe_ctlr_device\n");
406 fcoe = ERR_PTR(-ENOMEM);
410 ctlr = fcoe_ctlr_device_priv(ctlr_dev);
411 fcoe = fcoe_ctlr_priv(ctlr);
418 fcoe_ctlr_init(ctlr, fip_mode);
419 ctlr->send = fcoe_fip_send;
420 ctlr->update_mac = fcoe_update_src_mac;
421 ctlr->get_src_addr = fcoe_get_src_mac;
423 err = fcoe_interface_setup(fcoe, netdev);
425 fcoe_ctlr_destroy(ctlr);
426 fcoe_ctlr_device_delete(ctlr_dev);
435 module_put(THIS_MODULE);
441 * fcoe_interface_remove() - remove FCoE interface from netdev
442 * @fcoe: The FCoE interface to be cleaned up
444 * Caller must be holding the RTNL mutex
446 static void fcoe_interface_remove(struct fcoe_interface *fcoe)
448 struct net_device *netdev = fcoe->netdev;
449 struct fcoe_ctlr *fip = fcoe_to_ctlr(fcoe);
450 u8 flogi_maddr[ETH_ALEN];
451 const struct net_device_ops *ops;
454 * Don't listen for Ethernet packets anymore.
455 * synchronize_net() ensures that the packet handlers are not running
456 * on another CPU. dev_remove_pack() would do that, this calls the
457 * unsyncronized version __dev_remove_pack() to avoid multiple delays.
459 __dev_remove_pack(&fcoe->fcoe_packet_type);
460 __dev_remove_pack(&fcoe->fip_packet_type);
463 /* Delete secondary MAC addresses */
464 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
465 dev_uc_del(netdev, flogi_maddr);
467 dev_uc_del(netdev, fip->ctl_src_addr);
468 if (fip->mode == FIP_MODE_VN2VN) {
469 dev_mc_del(netdev, FIP_ALL_VN2VN_MACS);
470 dev_mc_del(netdev, FIP_ALL_P2P_MACS);
472 dev_mc_del(netdev, FIP_ALL_ENODE_MACS);
474 /* Tell the LLD we are done w/ FCoE */
475 ops = netdev->netdev_ops;
476 if (ops->ndo_fcoe_disable) {
477 if (ops->ndo_fcoe_disable(netdev))
478 FCOE_NETDEV_DBG(netdev, "Failed to disable FCoE"
479 " specific feature for LLD.\n");
486 * fcoe_interface_cleanup() - Clean up a FCoE interface
487 * @fcoe: The FCoE interface to be cleaned up
489 static void fcoe_interface_cleanup(struct fcoe_interface *fcoe)
491 struct net_device *netdev = fcoe->netdev;
492 struct fcoe_ctlr *fip = fcoe_to_ctlr(fcoe);
493 struct fcoe_ctlr_device *ctlr_dev = fcoe_ctlr_to_ctlr_dev(fip);
497 fcoe_interface_remove(fcoe);
500 /* Release the self-reference taken during fcoe_interface_create() */
501 /* tear-down the FCoE controller */
502 fcoe_ctlr_destroy(fip);
503 scsi_host_put(fip->lp->host);
504 fcoe_ctlr_device_delete(ctlr_dev);
506 module_put(THIS_MODULE);
510 * fcoe_fip_recv() - Handler for received FIP frames
511 * @skb: The receive skb
512 * @netdev: The associated net device
513 * @ptype: The packet_type structure which was used to register this handler
514 * @orig_dev: The original net_device the the skb was received on.
515 * (in case dev is a bond)
517 * Returns: 0 for success
519 static int fcoe_fip_recv(struct sk_buff *skb, struct net_device *netdev,
520 struct packet_type *ptype,
521 struct net_device *orig_dev)
523 struct fcoe_interface *fcoe;
524 struct fcoe_ctlr *ctlr;
526 fcoe = container_of(ptype, struct fcoe_interface, fip_packet_type);
527 ctlr = fcoe_to_ctlr(fcoe);
528 fcoe_ctlr_recv(ctlr, skb);
533 * fcoe_port_send() - Send an Ethernet-encapsulated FIP/FCoE frame
534 * @port: The FCoE port
535 * @skb: The FIP/FCoE packet to be sent
537 static void fcoe_port_send(struct fcoe_port *port, struct sk_buff *skb)
539 if (port->fcoe_pending_queue.qlen)
540 fcoe_check_wait_queue(port->lport, skb);
541 else if (fcoe_start_io(skb))
542 fcoe_check_wait_queue(port->lport, skb);
546 * fcoe_fip_send() - Send an Ethernet-encapsulated FIP frame
547 * @fip: The FCoE controller
548 * @skb: The FIP packet to be sent
550 static void fcoe_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
552 skb->dev = fcoe_from_ctlr(fip)->netdev;
553 fcoe_port_send(lport_priv(fip->lp), skb);
557 * fcoe_update_src_mac() - Update the Ethernet MAC filters
558 * @lport: The local port to update the source MAC on
559 * @addr: Unicast MAC address to add
561 * Remove any previously-set unicast MAC filter.
562 * Add secondary FCoE MAC address filter for our OUI.
564 static void fcoe_update_src_mac(struct fc_lport *lport, u8 *addr)
566 struct fcoe_port *port = lport_priv(lport);
567 struct fcoe_interface *fcoe = port->priv;
569 if (!is_zero_ether_addr(port->data_src_addr))
570 dev_uc_del(fcoe->netdev, port->data_src_addr);
571 if (!is_zero_ether_addr(addr))
572 dev_uc_add(fcoe->netdev, addr);
573 memcpy(port->data_src_addr, addr, ETH_ALEN);
577 * fcoe_get_src_mac() - return the Ethernet source address for an lport
578 * @lport: libfc lport
580 static u8 *fcoe_get_src_mac(struct fc_lport *lport)
582 struct fcoe_port *port = lport_priv(lport);
584 return port->data_src_addr;
588 * fcoe_lport_config() - Set up a local port
589 * @lport: The local port to be setup
591 * Returns: 0 for success
593 static int fcoe_lport_config(struct fc_lport *lport)
597 lport->max_retry_count = 3;
598 lport->max_rport_retry_count = 3;
599 lport->e_d_tov = 2 * 1000; /* FC-FS default */
600 lport->r_a_tov = 2 * 2 * 1000;
601 lport->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
602 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
603 lport->does_npiv = 1;
605 fc_lport_init_stats(lport);
607 /* lport fc_lport related configuration */
608 fc_lport_config(lport);
610 /* offload related configuration */
611 lport->crc_offload = 0;
612 lport->seq_offload = 0;
613 lport->lro_enabled = 0;
621 * fcoe_netdev_features_change - Updates the lport's offload flags based
622 * on the LLD netdev's FCoE feature flags
624 static void fcoe_netdev_features_change(struct fc_lport *lport,
625 struct net_device *netdev)
627 mutex_lock(&lport->lp_mutex);
629 if (netdev->features & NETIF_F_SG)
634 if (netdev->features & NETIF_F_FCOE_CRC) {
635 lport->crc_offload = 1;
636 FCOE_NETDEV_DBG(netdev, "Supports FCCRC offload\n");
638 lport->crc_offload = 0;
641 if (netdev->features & NETIF_F_FSO) {
642 lport->seq_offload = 1;
643 lport->lso_max = netdev->gso_max_size;
644 FCOE_NETDEV_DBG(netdev, "Supports LSO for max len 0x%x\n",
647 lport->seq_offload = 0;
651 if (netdev->fcoe_ddp_xid) {
652 lport->lro_enabled = 1;
653 lport->lro_xid = netdev->fcoe_ddp_xid;
654 FCOE_NETDEV_DBG(netdev, "Supports LRO for max xid 0x%x\n",
657 lport->lro_enabled = 0;
661 mutex_unlock(&lport->lp_mutex);
665 * fcoe_netdev_config() - Set up net devive for SW FCoE
666 * @lport: The local port that is associated with the net device
667 * @netdev: The associated net device
669 * Must be called after fcoe_lport_config() as it will use local port mutex
671 * Returns: 0 for success
673 static int fcoe_netdev_config(struct fc_lport *lport, struct net_device *netdev)
677 struct fcoe_interface *fcoe;
678 struct fcoe_ctlr *ctlr;
679 struct fcoe_port *port;
681 /* Setup lport private data to point to fcoe softc */
682 port = lport_priv(lport);
684 ctlr = fcoe_to_ctlr(fcoe);
687 * Determine max frame size based on underlying device and optional
688 * user-configured limit. If the MFS is too low, fcoe_link_ok()
689 * will return 0, so do this first.
692 if (netdev->features & NETIF_F_FCOE_MTU) {
694 FCOE_NETDEV_DBG(netdev, "Supports FCOE_MTU of %d bytes\n", mfs);
696 mfs -= (sizeof(struct fcoe_hdr) + sizeof(struct fcoe_crc_eof));
697 if (fc_set_mfs(lport, mfs))
700 /* offload features support */
701 fcoe_netdev_features_change(lport, netdev);
703 skb_queue_head_init(&port->fcoe_pending_queue);
704 port->fcoe_pending_queue_active = 0;
705 setup_timer(&port->timer, fcoe_queue_timer, (unsigned long)lport);
707 fcoe_link_speed_update(lport);
710 if (fcoe_get_wwn(netdev, &wwnn, NETDEV_FCOE_WWNN))
711 wwnn = fcoe_wwn_from_mac(ctlr->ctl_src_addr, 1, 0);
712 fc_set_wwnn(lport, wwnn);
713 if (fcoe_get_wwn(netdev, &wwpn, NETDEV_FCOE_WWPN))
714 wwpn = fcoe_wwn_from_mac(ctlr->ctl_src_addr,
716 fc_set_wwpn(lport, wwpn);
723 * fcoe_shost_config() - Set up the SCSI host associated with a local port
724 * @lport: The local port
725 * @dev: The device associated with the SCSI host
727 * Must be called after fcoe_lport_config() and fcoe_netdev_config()
729 * Returns: 0 for success
731 static int fcoe_shost_config(struct fc_lport *lport, struct device *dev)
735 /* lport scsi host config */
736 lport->host->max_lun = FCOE_MAX_LUN;
737 lport->host->max_id = FCOE_MAX_FCP_TARGET;
738 lport->host->max_channel = 0;
739 lport->host->max_cmd_len = FCOE_MAX_CMD_LEN;
742 lport->host->transportt = fcoe_vport_scsi_transport;
744 lport->host->transportt = fcoe_nport_scsi_transport;
746 /* add the new host to the SCSI-ml */
747 rc = scsi_add_host(lport->host, dev);
749 FCOE_NETDEV_DBG(fcoe_netdev(lport), "fcoe_shost_config: "
750 "error on scsi_add_host\n");
755 fc_host_max_npiv_vports(lport->host) = USHRT_MAX;
757 snprintf(fc_host_symbolic_name(lport->host), FC_SYMBOLIC_NAME_SIZE,
758 "%s v%s over %s", FCOE_NAME, FCOE_VERSION,
759 fcoe_netdev(lport)->name);
766 * fcoe_fdmi_info() - Get FDMI related info from net devive for SW FCoE
767 * @lport: The local port that is associated with the net device
768 * @netdev: The associated net device
770 * Must be called after fcoe_shost_config() as it will use local port mutex
773 static void fcoe_fdmi_info(struct fc_lport *lport, struct net_device *netdev)
775 struct fcoe_interface *fcoe;
776 struct fcoe_port *port;
777 struct net_device *realdev;
779 struct netdev_fcoe_hbainfo fdmi;
781 port = lport_priv(lport);
783 realdev = fcoe->realdev;
788 /* No FDMI state m/c for NPIV ports */
792 if (realdev->netdev_ops->ndo_fcoe_get_hbainfo) {
793 memset(&fdmi, 0, sizeof(fdmi));
794 rc = realdev->netdev_ops->ndo_fcoe_get_hbainfo(realdev,
797 printk(KERN_INFO "fcoe: Failed to retrieve FDMI "
798 "information from netdev.\n");
802 snprintf(fc_host_serial_number(lport->host),
803 FC_SERIAL_NUMBER_SIZE,
806 snprintf(fc_host_manufacturer(lport->host),
807 FC_SERIAL_NUMBER_SIZE,
810 snprintf(fc_host_model(lport->host),
811 FC_SYMBOLIC_NAME_SIZE,
814 snprintf(fc_host_model_description(lport->host),
815 FC_SYMBOLIC_NAME_SIZE,
817 fdmi.model_description);
818 snprintf(fc_host_hardware_version(lport->host),
819 FC_VERSION_STRING_SIZE,
821 fdmi.hardware_version);
822 snprintf(fc_host_driver_version(lport->host),
823 FC_VERSION_STRING_SIZE,
825 fdmi.driver_version);
826 snprintf(fc_host_optionrom_version(lport->host),
827 FC_VERSION_STRING_SIZE,
829 fdmi.optionrom_version);
830 snprintf(fc_host_firmware_version(lport->host),
831 FC_VERSION_STRING_SIZE,
833 fdmi.firmware_version);
835 /* Enable FDMI lport states */
836 lport->fdmi_enabled = 1;
838 lport->fdmi_enabled = 0;
839 printk(KERN_INFO "fcoe: No FDMI support.\n");
844 * fcoe_oem_match() - The match routine for the offloaded exchange manager
847 * This routine will be associated with an exchange manager (EM). When
848 * the libfc exchange handling code is looking for an EM to use it will
849 * call this routine and pass it the frame that it wishes to send. This
850 * routine will return True if the associated EM is to be used and False
851 * if the echange code should continue looking for an EM.
853 * The offload EM that this routine is associated with will handle any
854 * packets that are for SCSI read requests.
856 * This has been enhanced to work when FCoE stack is operating in target
859 * Returns: True for read types I/O, otherwise returns false.
861 static bool fcoe_oem_match(struct fc_frame *fp)
863 struct fc_frame_header *fh = fc_frame_header_get(fp);
864 struct fcp_cmnd *fcp;
866 if (fc_fcp_is_read(fr_fsp(fp)) &&
867 (fr_fsp(fp)->data_len > fcoe_ddp_min))
869 else if ((fr_fsp(fp) == NULL) &&
870 (fh->fh_r_ctl == FC_RCTL_DD_UNSOL_CMD) &&
871 (ntohs(fh->fh_rx_id) == FC_XID_UNKNOWN)) {
872 fcp = fc_frame_payload_get(fp, sizeof(*fcp));
873 if ((fcp->fc_flags & FCP_CFL_WRDATA) &&
874 (ntohl(fcp->fc_dl) > fcoe_ddp_min))
881 * fcoe_em_config() - Allocate and configure an exchange manager
882 * @lport: The local port that the new EM will be associated with
884 * Returns: 0 on success
886 static inline int fcoe_em_config(struct fc_lport *lport)
888 struct fcoe_port *port = lport_priv(lport);
889 struct fcoe_interface *fcoe = port->priv;
890 struct fcoe_interface *oldfcoe = NULL;
891 struct net_device *old_real_dev, *cur_real_dev;
892 u16 min_xid = FCOE_MIN_XID;
893 u16 max_xid = FCOE_MAX_XID;
896 * Check if need to allocate an em instance for
897 * offload exchange ids to be shared across all VN_PORTs/lport.
899 if (!lport->lro_enabled || !lport->lro_xid ||
900 (lport->lro_xid >= max_xid)) {
906 * Reuse existing offload em instance in case
907 * it is already allocated on real eth device
909 if (fcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
910 cur_real_dev = vlan_dev_real_dev(fcoe->netdev);
912 cur_real_dev = fcoe->netdev;
914 list_for_each_entry(oldfcoe, &fcoe_hostlist, list) {
915 if (oldfcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
916 old_real_dev = vlan_dev_real_dev(oldfcoe->netdev);
918 old_real_dev = oldfcoe->netdev;
920 if (cur_real_dev == old_real_dev) {
921 fcoe->oem = oldfcoe->oem;
927 if (!fc_exch_mgr_add(lport, fcoe->oem, fcoe_oem_match)) {
928 printk(KERN_ERR "fcoe_em_config: failed to add "
929 "offload em:%p on interface:%s\n",
930 fcoe->oem, fcoe->netdev->name);
934 fcoe->oem = fc_exch_mgr_alloc(lport, FC_CLASS_3,
935 FCOE_MIN_XID, lport->lro_xid,
938 printk(KERN_ERR "fcoe_em_config: failed to allocate "
939 "em for offload exches on interface:%s\n",
946 * Exclude offload EM xid range from next EM xid range.
948 min_xid += lport->lro_xid + 1;
951 if (!fc_exch_mgr_alloc(lport, FC_CLASS_3, min_xid, max_xid, NULL)) {
952 printk(KERN_ERR "fcoe_em_config: failed to "
953 "allocate em on interface %s\n", fcoe->netdev->name);
961 * fcoe_if_destroy() - Tear down a SW FCoE instance
962 * @lport: The local port to be destroyed
965 static void fcoe_if_destroy(struct fc_lport *lport)
967 struct fcoe_port *port = lport_priv(lport);
968 struct fcoe_interface *fcoe = port->priv;
969 struct net_device *netdev = fcoe->netdev;
971 FCOE_NETDEV_DBG(netdev, "Destroying interface\n");
973 /* Logout of the fabric */
974 fc_fabric_logoff(lport);
976 /* Cleanup the fc_lport */
977 fc_lport_destroy(lport);
979 /* Stop the transmit retry timer */
980 del_timer_sync(&port->timer);
982 /* Free existing transmit skbs */
983 fcoe_clean_pending_queue(lport);
986 if (!is_zero_ether_addr(port->data_src_addr))
987 dev_uc_del(netdev, port->data_src_addr);
991 fcoe_interface_remove(fcoe);
994 /* Free queued packets for the per-CPU receive threads */
995 fcoe_percpu_clean(lport);
997 /* Detach from the scsi-ml */
998 fc_remove_host(lport->host);
999 scsi_remove_host(lport->host);
1001 /* Destroy lport scsi_priv */
1002 fc_fcp_destroy(lport);
1004 /* There are no more rports or I/O, free the EM */
1005 fc_exch_mgr_free(lport);
1007 /* Free memory used by statistical counters */
1008 fc_lport_free_stats(lport);
1011 * Release the Scsi_Host for vport but hold on to
1012 * master lport until it fcoe interface fully cleaned-up.
1015 scsi_host_put(lport->host);
1019 * fcoe_ddp_setup() - Call a LLD's ddp_setup through the net device
1020 * @lport: The local port to setup DDP for
1021 * @xid: The exchange ID for this DDP transfer
1022 * @sgl: The scatterlist describing this transfer
1023 * @sgc: The number of sg items
1025 * Returns: 0 if the DDP context was not configured
1027 static int fcoe_ddp_setup(struct fc_lport *lport, u16 xid,
1028 struct scatterlist *sgl, unsigned int sgc)
1030 struct net_device *netdev = fcoe_netdev(lport);
1032 if (netdev->netdev_ops->ndo_fcoe_ddp_setup)
1033 return netdev->netdev_ops->ndo_fcoe_ddp_setup(netdev,
1041 * fcoe_ddp_target() - Call a LLD's ddp_target through the net device
1042 * @lport: The local port to setup DDP for
1043 * @xid: The exchange ID for this DDP transfer
1044 * @sgl: The scatterlist describing this transfer
1045 * @sgc: The number of sg items
1047 * Returns: 0 if the DDP context was not configured
1049 static int fcoe_ddp_target(struct fc_lport *lport, u16 xid,
1050 struct scatterlist *sgl, unsigned int sgc)
1052 struct net_device *netdev = fcoe_netdev(lport);
1054 if (netdev->netdev_ops->ndo_fcoe_ddp_target)
1055 return netdev->netdev_ops->ndo_fcoe_ddp_target(netdev, xid,
1063 * fcoe_ddp_done() - Call a LLD's ddp_done through the net device
1064 * @lport: The local port to complete DDP on
1065 * @xid: The exchange ID for this DDP transfer
1067 * Returns: the length of data that have been completed by DDP
1069 static int fcoe_ddp_done(struct fc_lport *lport, u16 xid)
1071 struct net_device *netdev = fcoe_netdev(lport);
1073 if (netdev->netdev_ops->ndo_fcoe_ddp_done)
1074 return netdev->netdev_ops->ndo_fcoe_ddp_done(netdev, xid);
1079 * fcoe_if_create() - Create a FCoE instance on an interface
1080 * @fcoe: The FCoE interface to create a local port on
1081 * @parent: The device pointer to be the parent in sysfs for the SCSI host
1082 * @npiv: Indicates if the port is a vport or not
1084 * Creates a fc_lport instance and a Scsi_Host instance and configure them.
1086 * Returns: The allocated fc_lport or an error pointer
1088 static struct fc_lport *fcoe_if_create(struct fcoe_interface *fcoe,
1089 struct device *parent, int npiv)
1091 struct fcoe_ctlr *ctlr = fcoe_to_ctlr(fcoe);
1092 struct net_device *netdev = fcoe->netdev;
1093 struct fc_lport *lport, *n_port;
1094 struct fcoe_port *port;
1095 struct Scsi_Host *shost;
1098 * parent is only a vport if npiv is 1,
1099 * but we'll only use vport in that case so go ahead and set it
1101 struct fc_vport *vport = dev_to_vport(parent);
1103 FCOE_NETDEV_DBG(netdev, "Create Interface\n");
1106 lport = libfc_host_alloc(&fcoe_shost_template, sizeof(*port));
1108 lport = libfc_vport_create(vport, sizeof(*port));
1111 FCOE_NETDEV_DBG(netdev, "Could not allocate host structure\n");
1115 port = lport_priv(lport);
1116 port->lport = lport;
1118 port->get_netdev = fcoe_netdev;
1119 port->max_queue_depth = FCOE_MAX_QUEUE_DEPTH;
1120 port->min_queue_depth = FCOE_MIN_QUEUE_DEPTH;
1121 INIT_WORK(&port->destroy_work, fcoe_destroy_work);
1124 * Need to add the lport to the hostlist
1125 * so we catch NETDEV_CHANGE events.
1127 fcoe_hostlist_add(lport);
1129 /* configure a fc_lport including the exchange manager */
1130 rc = fcoe_lport_config(lport);
1132 FCOE_NETDEV_DBG(netdev, "Could not configure lport for the "
1138 FCOE_NETDEV_DBG(netdev, "Setting vport names, "
1139 "%16.16llx %16.16llx\n",
1140 vport->node_name, vport->port_name);
1141 fc_set_wwnn(lport, vport->node_name);
1142 fc_set_wwpn(lport, vport->port_name);
1145 /* configure lport network properties */
1146 rc = fcoe_netdev_config(lport, netdev);
1148 FCOE_NETDEV_DBG(netdev, "Could not configure netdev for the "
1150 goto out_lp_destroy;
1153 /* configure lport scsi host properties */
1154 rc = fcoe_shost_config(lport, parent);
1156 FCOE_NETDEV_DBG(netdev, "Could not configure shost for the "
1158 goto out_lp_destroy;
1161 /* Initialize the library */
1162 rc = fcoe_libfc_config(lport, ctlr, &fcoe_libfc_fcn_templ, 1);
1164 FCOE_NETDEV_DBG(netdev, "Could not configure libfc for the "
1166 goto out_lp_destroy;
1169 /* Initialized FDMI information */
1170 fcoe_fdmi_info(lport, netdev);
1173 * fcoe_em_alloc() and fcoe_hostlist_add() both
1174 * need to be atomic with respect to other changes to the
1175 * hostlist since fcoe_em_alloc() looks for an existing EM
1176 * instance on host list updated by fcoe_hostlist_add().
1178 * This is currently handled through the fcoe_config_mutex
1182 /* lport exch manager allocation */
1183 rc = fcoe_em_config(lport);
1185 shost = vport_to_shost(vport);
1186 n_port = shost_priv(shost);
1187 rc = fc_exch_mgr_list_clone(n_port, lport);
1191 FCOE_NETDEV_DBG(netdev, "Could not configure the EM\n");
1192 goto out_lp_destroy;
1198 fc_exch_mgr_free(lport);
1200 fcoe_hostlist_del(lport);
1201 scsi_host_put(lport->host);
1207 * fcoe_if_init() - Initialization routine for fcoe.ko
1209 * Attaches the SW FCoE transport to the FC transport
1211 * Returns: 0 on success
1213 static int __init fcoe_if_init(void)
1215 /* attach to scsi transport */
1216 fcoe_nport_scsi_transport =
1217 fc_attach_transport(&fcoe_nport_fc_functions);
1218 fcoe_vport_scsi_transport =
1219 fc_attach_transport(&fcoe_vport_fc_functions);
1221 if (!fcoe_nport_scsi_transport) {
1222 printk(KERN_ERR "fcoe: Failed to attach to the FC transport\n");
1230 * fcoe_if_exit() - Tear down fcoe.ko
1232 * Detaches the SW FCoE transport from the FC transport
1234 * Returns: 0 on success
1236 static int __exit fcoe_if_exit(void)
1238 fc_release_transport(fcoe_nport_scsi_transport);
1239 fc_release_transport(fcoe_vport_scsi_transport);
1240 fcoe_nport_scsi_transport = NULL;
1241 fcoe_vport_scsi_transport = NULL;
1246 * fcoe_percpu_thread_create() - Create a receive thread for an online CPU
1247 * @cpu: The CPU index of the CPU to create a receive thread for
1249 static void fcoe_percpu_thread_create(unsigned int cpu)
1251 struct fcoe_percpu_s *p;
1252 struct task_struct *thread;
1254 p = &per_cpu(fcoe_percpu, cpu);
1256 thread = kthread_create_on_node(fcoe_percpu_receive_thread,
1257 (void *)p, cpu_to_node(cpu),
1258 "fcoethread/%d", cpu);
1260 if (likely(!IS_ERR(thread))) {
1261 kthread_bind(thread, cpu);
1262 wake_up_process(thread);
1264 spin_lock_bh(&p->fcoe_rx_list.lock);
1266 spin_unlock_bh(&p->fcoe_rx_list.lock);
1271 * fcoe_percpu_thread_destroy() - Remove the receive thread of a CPU
1272 * @cpu: The CPU index of the CPU whose receive thread is to be destroyed
1274 * Destroys a per-CPU Rx thread. Any pending skbs are moved to the
1275 * current CPU's Rx thread. If the thread being destroyed is bound to
1276 * the CPU processing this context the skbs will be freed.
1278 static void fcoe_percpu_thread_destroy(unsigned int cpu)
1280 struct fcoe_percpu_s *p;
1281 struct task_struct *thread;
1282 struct page *crc_eof;
1283 struct sk_buff *skb;
1285 struct fcoe_percpu_s *p0;
1286 unsigned targ_cpu = get_cpu();
1287 #endif /* CONFIG_SMP */
1289 FCOE_DBG("Destroying receive thread for CPU %d\n", cpu);
1291 /* Prevent any new skbs from being queued for this CPU. */
1292 p = &per_cpu(fcoe_percpu, cpu);
1293 spin_lock_bh(&p->fcoe_rx_list.lock);
1296 crc_eof = p->crc_eof_page;
1297 p->crc_eof_page = NULL;
1298 p->crc_eof_offset = 0;
1299 spin_unlock_bh(&p->fcoe_rx_list.lock);
1303 * Don't bother moving the skb's if this context is running
1304 * on the same CPU that is having its thread destroyed. This
1305 * can easily happen when the module is removed.
1307 if (cpu != targ_cpu) {
1308 p0 = &per_cpu(fcoe_percpu, targ_cpu);
1309 spin_lock_bh(&p0->fcoe_rx_list.lock);
1311 FCOE_DBG("Moving frames from CPU %d to CPU %d\n",
1314 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1315 __skb_queue_tail(&p0->fcoe_rx_list, skb);
1316 spin_unlock_bh(&p0->fcoe_rx_list.lock);
1319 * The targeted CPU is not initialized and cannot accept
1320 * new skbs. Unlock the targeted CPU and drop the skbs
1321 * on the CPU that is going offline.
1323 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1325 spin_unlock_bh(&p0->fcoe_rx_list.lock);
1329 * This scenario occurs when the module is being removed
1330 * and all threads are being destroyed. skbs will continue
1331 * to be shifted from the CPU thread that is being removed
1332 * to the CPU thread associated with the CPU that is processing
1333 * the module removal. Once there is only one CPU Rx thread it
1334 * will reach this case and we will drop all skbs and later
1337 spin_lock_bh(&p->fcoe_rx_list.lock);
1338 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1340 spin_unlock_bh(&p->fcoe_rx_list.lock);
1345 * This a non-SMP scenario where the singular Rx thread is
1346 * being removed. Free all skbs and stop the thread.
1348 spin_lock_bh(&p->fcoe_rx_list.lock);
1349 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1351 spin_unlock_bh(&p->fcoe_rx_list.lock);
1355 kthread_stop(thread);
1362 * fcoe_cpu_callback() - Handler for CPU hotplug events
1363 * @nfb: The callback data block
1364 * @action: The event triggering the callback
1365 * @hcpu: The index of the CPU that the event is for
1367 * This creates or destroys per-CPU data for fcoe
1369 * Returns NOTIFY_OK always.
1371 static int fcoe_cpu_callback(struct notifier_block *nfb,
1372 unsigned long action, void *hcpu)
1374 unsigned cpu = (unsigned long)hcpu;
1378 case CPU_ONLINE_FROZEN:
1379 FCOE_DBG("CPU %x online: Create Rx thread\n", cpu);
1380 fcoe_percpu_thread_create(cpu);
1383 case CPU_DEAD_FROZEN:
1384 FCOE_DBG("CPU %x offline: Remove Rx thread\n", cpu);
1385 fcoe_percpu_thread_destroy(cpu);
1394 * fcoe_select_cpu() - Selects CPU to handle post-processing of incoming
1397 * This routine selects next CPU based on cpumask to distribute
1398 * incoming requests in round robin.
1400 * Returns: int CPU number
1402 static inline unsigned int fcoe_select_cpu(void)
1404 static unsigned int selected_cpu;
1406 selected_cpu = cpumask_next(selected_cpu, cpu_online_mask);
1407 if (selected_cpu >= nr_cpu_ids)
1408 selected_cpu = cpumask_first(cpu_online_mask);
1410 return selected_cpu;
1414 * fcoe_rcv() - Receive packets from a net device
1415 * @skb: The received packet
1416 * @netdev: The net device that the packet was received on
1417 * @ptype: The packet type context
1418 * @olddev: The last device net device
1420 * This routine is called by NET_RX_SOFTIRQ. It receives a packet, builds a
1421 * FC frame and passes the frame to libfc.
1423 * Returns: 0 for success
1425 static int fcoe_rcv(struct sk_buff *skb, struct net_device *netdev,
1426 struct packet_type *ptype, struct net_device *olddev)
1428 struct fc_lport *lport;
1429 struct fcoe_rcv_info *fr;
1430 struct fcoe_ctlr *ctlr;
1431 struct fcoe_interface *fcoe;
1432 struct fc_frame_header *fh;
1433 struct fcoe_percpu_s *fps;
1437 fcoe = container_of(ptype, struct fcoe_interface, fcoe_packet_type);
1438 ctlr = fcoe_to_ctlr(fcoe);
1440 if (unlikely(!lport)) {
1441 FCOE_NETDEV_DBG(netdev, "Cannot find hba structure");
1444 if (!lport->link_up)
1447 FCOE_NETDEV_DBG(netdev, "skb_info: len:%d data_len:%d head:%p "
1448 "data:%p tail:%p end:%p sum:%d dev:%s",
1449 skb->len, skb->data_len, skb->head, skb->data,
1450 skb_tail_pointer(skb), skb_end_pointer(skb),
1451 skb->csum, skb->dev ? skb->dev->name : "<NULL>");
1455 if (is_fip_mode(ctlr) &&
1456 compare_ether_addr(eh->h_source, ctlr->dest_addr)) {
1457 FCOE_NETDEV_DBG(netdev, "wrong source mac address:%pM\n",
1463 * Check for minimum frame length, and make sure required FCoE
1464 * and FC headers are pulled into the linear data area.
1466 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
1467 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
1470 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
1471 fh = (struct fc_frame_header *) skb_transport_header(skb);
1473 if (ntoh24(&eh->h_dest[3]) != ntoh24(fh->fh_d_id)) {
1474 FCOE_NETDEV_DBG(netdev, "FC frame d_id mismatch with MAC:%pM\n",
1479 fr = fcoe_dev_from_skb(skb);
1483 * In case the incoming frame's exchange is originated from
1484 * the initiator, then received frame's exchange id is ANDed
1485 * with fc_cpu_mask bits to get the same cpu on which exchange
1486 * was originated, otherwise select cpu using rx exchange id
1487 * or fcoe_select_cpu().
1489 if (ntoh24(fh->fh_f_ctl) & FC_FC_EX_CTX)
1490 cpu = ntohs(fh->fh_ox_id) & fc_cpu_mask;
1492 if (ntohs(fh->fh_rx_id) == FC_XID_UNKNOWN)
1493 cpu = fcoe_select_cpu();
1495 cpu = ntohs(fh->fh_rx_id) & fc_cpu_mask;
1498 if (cpu >= nr_cpu_ids)
1501 fps = &per_cpu(fcoe_percpu, cpu);
1502 spin_lock(&fps->fcoe_rx_list.lock);
1503 if (unlikely(!fps->thread)) {
1505 * The targeted CPU is not ready, let's target
1506 * the first CPU now. For non-SMP systems this
1507 * will check the same CPU twice.
1509 FCOE_NETDEV_DBG(netdev, "CPU is online, but no receive thread "
1510 "ready for incoming skb- using first online "
1513 spin_unlock(&fps->fcoe_rx_list.lock);
1514 cpu = cpumask_first(cpu_online_mask);
1515 fps = &per_cpu(fcoe_percpu, cpu);
1516 spin_lock(&fps->fcoe_rx_list.lock);
1518 spin_unlock(&fps->fcoe_rx_list.lock);
1524 * We now have a valid CPU that we're targeting for
1525 * this skb. We also have this receive thread locked,
1526 * so we're free to queue skbs into it's queue.
1530 * Note: We used to have a set of conditions under which we would
1531 * call fcoe_recv_frame directly, rather than queuing to the rx list
1532 * as it could save a few cycles, but doing so is prohibited, as
1533 * fcoe_recv_frame has several paths that may sleep, which is forbidden
1534 * in softirq context.
1536 __skb_queue_tail(&fps->fcoe_rx_list, skb);
1537 if (fps->thread->state == TASK_INTERRUPTIBLE)
1538 wake_up_process(fps->thread);
1539 spin_unlock(&fps->fcoe_rx_list.lock);
1543 per_cpu_ptr(lport->stats, get_cpu())->ErrorFrames++;
1551 * fcoe_alloc_paged_crc_eof() - Allocate a page to be used for the trailer CRC
1552 * @skb: The packet to be transmitted
1553 * @tlen: The total length of the trailer
1555 * Returns: 0 for success
1557 static int fcoe_alloc_paged_crc_eof(struct sk_buff *skb, int tlen)
1559 struct fcoe_percpu_s *fps;
1562 fps = &get_cpu_var(fcoe_percpu);
1563 rc = fcoe_get_paged_crc_eof(skb, tlen, fps);
1564 put_cpu_var(fcoe_percpu);
1570 * fcoe_xmit() - Transmit a FCoE frame
1571 * @lport: The local port that the frame is to be transmitted for
1572 * @fp: The frame to be transmitted
1574 * Return: 0 for success
1576 static int fcoe_xmit(struct fc_lport *lport, struct fc_frame *fp)
1581 struct fcoe_crc_eof *cp;
1582 struct sk_buff *skb;
1583 struct fc_stats *stats;
1584 struct fc_frame_header *fh;
1585 unsigned int hlen; /* header length implies the version */
1586 unsigned int tlen; /* trailer length */
1587 unsigned int elen; /* eth header, may include vlan */
1588 struct fcoe_port *port = lport_priv(lport);
1589 struct fcoe_interface *fcoe = port->priv;
1590 struct fcoe_ctlr *ctlr = fcoe_to_ctlr(fcoe);
1592 struct fcoe_hdr *hp;
1594 WARN_ON((fr_len(fp) % sizeof(u32)) != 0);
1596 fh = fc_frame_header_get(fp);
1598 wlen = skb->len / FCOE_WORD_TO_BYTE;
1600 if (!lport->link_up) {
1605 if (unlikely(fh->fh_type == FC_TYPE_ELS) &&
1606 fcoe_ctlr_els_send(ctlr, lport, skb))
1612 elen = sizeof(struct ethhdr);
1613 hlen = sizeof(struct fcoe_hdr);
1614 tlen = sizeof(struct fcoe_crc_eof);
1615 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
1618 if (likely(lport->crc_offload)) {
1619 skb->ip_summed = CHECKSUM_UNNECESSARY;
1620 skb->csum_start = skb_headroom(skb);
1621 skb->csum_offset = skb->len;
1624 skb->ip_summed = CHECKSUM_NONE;
1625 crc = fcoe_fc_crc(fp);
1628 /* copy port crc and eof to the skb buff */
1629 if (skb_is_nonlinear(skb)) {
1631 if (fcoe_alloc_paged_crc_eof(skb, tlen)) {
1635 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
1636 cp = kmap_atomic(skb_frag_page(frag))
1637 + frag->page_offset;
1639 cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
1642 memset(cp, 0, sizeof(*cp));
1644 cp->fcoe_crc32 = cpu_to_le32(~crc);
1646 if (skb_is_nonlinear(skb)) {
1651 /* adjust skb network/transport offsets to match mac/fcoe/port */
1652 skb_push(skb, elen + hlen);
1653 skb_reset_mac_header(skb);
1654 skb_reset_network_header(skb);
1655 skb->mac_len = elen;
1656 skb->protocol = htons(ETH_P_FCOE);
1657 skb->priority = fcoe->priority;
1659 if (fcoe->netdev->priv_flags & IFF_802_1Q_VLAN &&
1660 fcoe->realdev->features & NETIF_F_HW_VLAN_TX) {
1661 skb->vlan_tci = VLAN_TAG_PRESENT |
1662 vlan_dev_vlan_id(fcoe->netdev);
1663 skb->dev = fcoe->realdev;
1665 skb->dev = fcoe->netdev;
1667 /* fill up mac and fcoe headers */
1669 eh->h_proto = htons(ETH_P_FCOE);
1670 memcpy(eh->h_dest, ctlr->dest_addr, ETH_ALEN);
1672 memcpy(eh->h_dest + 3, fh->fh_d_id, 3);
1674 if (unlikely(ctlr->flogi_oxid != FC_XID_UNKNOWN))
1675 memcpy(eh->h_source, ctlr->ctl_src_addr, ETH_ALEN);
1677 memcpy(eh->h_source, port->data_src_addr, ETH_ALEN);
1679 hp = (struct fcoe_hdr *)(eh + 1);
1680 memset(hp, 0, sizeof(*hp));
1682 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
1685 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
1686 if (lport->seq_offload && fr_max_payload(fp)) {
1687 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
1688 skb_shinfo(skb)->gso_size = fr_max_payload(fp);
1690 skb_shinfo(skb)->gso_type = 0;
1691 skb_shinfo(skb)->gso_size = 0;
1693 /* update tx stats: regardless if LLD fails */
1694 stats = per_cpu_ptr(lport->stats, get_cpu());
1696 stats->TxWords += wlen;
1699 /* send down to lld */
1701 fcoe_port_send(port, skb);
1706 * fcoe_percpu_flush_done() - Indicate per-CPU queue flush completion
1707 * @skb: The completed skb (argument required by destructor)
1709 static void fcoe_percpu_flush_done(struct sk_buff *skb)
1711 complete(&fcoe_flush_completion);
1715 * fcoe_filter_frames() - filter out bad fcoe frames, i.e. bad CRC
1716 * @lport: The local port the frame was received on
1717 * @fp: The received frame
1719 * Return: 0 on passing filtering checks
1721 static inline int fcoe_filter_frames(struct fc_lport *lport,
1722 struct fc_frame *fp)
1724 struct fcoe_ctlr *ctlr;
1725 struct fcoe_interface *fcoe;
1726 struct fc_frame_header *fh;
1727 struct sk_buff *skb = (struct sk_buff *)fp;
1728 struct fc_stats *stats;
1731 * We only check CRC if no offload is available and if it is
1732 * it's solicited data, in which case, the FCP layer would
1733 * check it during the copy.
1735 if (lport->crc_offload && skb->ip_summed == CHECKSUM_UNNECESSARY)
1736 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1738 fr_flags(fp) |= FCPHF_CRC_UNCHECKED;
1740 fh = (struct fc_frame_header *) skb_transport_header(skb);
1741 fh = fc_frame_header_get(fp);
1742 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA && fh->fh_type == FC_TYPE_FCP)
1745 fcoe = ((struct fcoe_port *)lport_priv(lport))->priv;
1746 ctlr = fcoe_to_ctlr(fcoe);
1747 if (is_fip_mode(ctlr) && fc_frame_payload_op(fp) == ELS_LOGO &&
1748 ntoh24(fh->fh_s_id) == FC_FID_FLOGI) {
1749 FCOE_DBG("fcoe: dropping FCoE lport LOGO in fip mode\n");
1753 if (!(fr_flags(fp) & FCPHF_CRC_UNCHECKED) ||
1754 le32_to_cpu(fr_crc(fp)) == ~crc32(~0, skb->data, skb->len)) {
1755 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1759 stats = per_cpu_ptr(lport->stats, get_cpu());
1760 stats->InvalidCRCCount++;
1761 if (stats->InvalidCRCCount < 5)
1762 printk(KERN_WARNING "fcoe: dropping frame with CRC error\n");
1768 * fcoe_recv_frame() - process a single received frame
1769 * @skb: frame to process
1771 static void fcoe_recv_frame(struct sk_buff *skb)
1774 struct fc_lport *lport;
1775 struct fcoe_rcv_info *fr;
1776 struct fc_stats *stats;
1777 struct fcoe_crc_eof crc_eof;
1778 struct fc_frame *fp;
1779 struct fcoe_port *port;
1780 struct fcoe_hdr *hp;
1782 fr = fcoe_dev_from_skb(skb);
1784 if (unlikely(!lport)) {
1785 if (skb->destructor != fcoe_percpu_flush_done)
1786 FCOE_NETDEV_DBG(skb->dev, "NULL lport in skb");
1791 FCOE_NETDEV_DBG(skb->dev, "skb_info: len:%d data_len:%d "
1792 "head:%p data:%p tail:%p end:%p sum:%d dev:%s",
1793 skb->len, skb->data_len,
1794 skb->head, skb->data, skb_tail_pointer(skb),
1795 skb_end_pointer(skb), skb->csum,
1796 skb->dev ? skb->dev->name : "<NULL>");
1798 port = lport_priv(lport);
1799 skb_linearize(skb); /* check for skb_is_nonlinear is within skb_linearize */
1802 * Frame length checks and setting up the header pointers
1803 * was done in fcoe_rcv already.
1805 hp = (struct fcoe_hdr *) skb_network_header(skb);
1807 stats = per_cpu_ptr(lport->stats, get_cpu());
1808 if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) {
1809 if (stats->ErrorFrames < 5)
1810 printk(KERN_WARNING "fcoe: FCoE version "
1811 "mismatch: The frame has "
1812 "version %x, but the "
1813 "initiator supports version "
1814 "%x\n", FC_FCOE_DECAPS_VER(hp),
1819 skb_pull(skb, sizeof(struct fcoe_hdr));
1820 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
1823 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
1825 fp = (struct fc_frame *)skb;
1828 fr_sof(fp) = hp->fcoe_sof;
1830 /* Copy out the CRC and EOF trailer for access */
1831 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof)))
1833 fr_eof(fp) = crc_eof.fcoe_eof;
1834 fr_crc(fp) = crc_eof.fcoe_crc32;
1835 if (pskb_trim(skb, fr_len))
1838 if (!fcoe_filter_frames(lport, fp)) {
1840 fc_exch_recv(lport, fp);
1844 stats->ErrorFrames++;
1850 * fcoe_percpu_receive_thread() - The per-CPU packet receive thread
1851 * @arg: The per-CPU context
1853 * Return: 0 for success
1855 static int fcoe_percpu_receive_thread(void *arg)
1857 struct fcoe_percpu_s *p = arg;
1858 struct sk_buff *skb;
1859 struct sk_buff_head tmp;
1861 skb_queue_head_init(&tmp);
1863 set_user_nice(current, -20);
1866 while (!kthread_should_stop()) {
1868 spin_lock_bh(&p->fcoe_rx_list.lock);
1869 skb_queue_splice_init(&p->fcoe_rx_list, &tmp);
1871 if (!skb_queue_len(&tmp)) {
1872 set_current_state(TASK_INTERRUPTIBLE);
1873 spin_unlock_bh(&p->fcoe_rx_list.lock);
1875 set_current_state(TASK_RUNNING);
1879 spin_unlock_bh(&p->fcoe_rx_list.lock);
1881 while ((skb = __skb_dequeue(&tmp)) != NULL)
1882 fcoe_recv_frame(skb);
1889 * fcoe_dev_setup() - Setup the link change notification interface
1891 static void fcoe_dev_setup(void)
1893 register_dcbevent_notifier(&dcb_notifier);
1894 register_netdevice_notifier(&fcoe_notifier);
1898 * fcoe_dev_cleanup() - Cleanup the link change notification interface
1900 static void fcoe_dev_cleanup(void)
1902 unregister_dcbevent_notifier(&dcb_notifier);
1903 unregister_netdevice_notifier(&fcoe_notifier);
1906 static struct fcoe_interface *
1907 fcoe_hostlist_lookup_realdev_port(struct net_device *netdev)
1909 struct fcoe_interface *fcoe;
1910 struct net_device *real_dev;
1912 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
1913 if (fcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
1914 real_dev = vlan_dev_real_dev(fcoe->netdev);
1916 real_dev = fcoe->netdev;
1918 if (netdev == real_dev)
1924 static int fcoe_dcb_app_notification(struct notifier_block *notifier,
1925 ulong event, void *ptr)
1927 struct dcb_app_type *entry = ptr;
1928 struct fcoe_ctlr *ctlr;
1929 struct fcoe_interface *fcoe;
1930 struct net_device *netdev;
1933 if (entry->app.selector != DCB_APP_IDTYPE_ETHTYPE)
1936 netdev = dev_get_by_index(&init_net, entry->ifindex);
1940 fcoe = fcoe_hostlist_lookup_realdev_port(netdev);
1945 ctlr = fcoe_to_ctlr(fcoe);
1947 if (entry->dcbx & DCB_CAP_DCBX_VER_CEE)
1948 prio = ffs(entry->app.priority) - 1;
1950 prio = entry->app.priority;
1955 if (entry->app.protocol == ETH_P_FIP ||
1956 entry->app.protocol == ETH_P_FCOE)
1957 ctlr->priority = prio;
1959 if (entry->app.protocol == ETH_P_FCOE)
1960 fcoe->priority = prio;
1966 * fcoe_device_notification() - Handler for net device events
1967 * @notifier: The context of the notification
1968 * @event: The type of event
1969 * @ptr: The net device that the event was on
1971 * This function is called by the Ethernet driver in case of link change event.
1973 * Returns: 0 for success
1975 static int fcoe_device_notification(struct notifier_block *notifier,
1976 ulong event, void *ptr)
1978 struct fcoe_ctlr_device *cdev;
1979 struct fc_lport *lport = NULL;
1980 struct net_device *netdev = ptr;
1981 struct fcoe_ctlr *ctlr;
1982 struct fcoe_interface *fcoe;
1983 struct fcoe_port *port;
1984 struct fc_stats *stats;
1985 u32 link_possible = 1;
1989 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
1990 if (fcoe->netdev == netdev) {
1991 ctlr = fcoe_to_ctlr(fcoe);
2003 case NETDEV_GOING_DOWN:
2009 case NETDEV_CHANGEMTU:
2010 if (netdev->features & NETIF_F_FCOE_MTU)
2012 mfs = netdev->mtu - (sizeof(struct fcoe_hdr) +
2013 sizeof(struct fcoe_crc_eof));
2014 if (mfs >= FC_MIN_MAX_FRAME)
2015 fc_set_mfs(lport, mfs);
2017 case NETDEV_REGISTER:
2019 case NETDEV_UNREGISTER:
2020 list_del(&fcoe->list);
2021 port = lport_priv(ctlr->lp);
2022 queue_work(fcoe_wq, &port->destroy_work);
2025 case NETDEV_FEAT_CHANGE:
2026 fcoe_netdev_features_change(lport, netdev);
2029 FCOE_NETDEV_DBG(netdev, "Unknown event %ld "
2030 "from netdev netlink\n", event);
2033 fcoe_link_speed_update(lport);
2035 cdev = fcoe_ctlr_to_ctlr_dev(ctlr);
2037 if (link_possible && !fcoe_link_ok(lport)) {
2038 switch (cdev->enabled) {
2039 case FCOE_CTLR_DISABLED:
2040 pr_info("Link up while interface is disabled.\n");
2042 case FCOE_CTLR_ENABLED:
2043 case FCOE_CTLR_UNUSED:
2044 fcoe_ctlr_link_up(ctlr);
2046 } else if (fcoe_ctlr_link_down(ctlr)) {
2047 switch (cdev->enabled) {
2048 case FCOE_CTLR_DISABLED:
2049 pr_info("Link down while interface is disabled.\n");
2051 case FCOE_CTLR_ENABLED:
2052 case FCOE_CTLR_UNUSED:
2053 stats = per_cpu_ptr(lport->stats, get_cpu());
2054 stats->LinkFailureCount++;
2056 fcoe_clean_pending_queue(lport);
2064 * fcoe_disable() - Disables a FCoE interface
2065 * @netdev : The net_device object the Ethernet interface to create on
2067 * Called from fcoe transport.
2069 * Returns: 0 for success
2071 * Deprecated: use fcoe_ctlr_enabled()
2073 static int fcoe_disable(struct net_device *netdev)
2075 struct fcoe_ctlr *ctlr;
2076 struct fcoe_interface *fcoe;
2079 mutex_lock(&fcoe_config_mutex);
2082 fcoe = fcoe_hostlist_lookup_port(netdev);
2086 ctlr = fcoe_to_ctlr(fcoe);
2087 fcoe_ctlr_link_down(ctlr);
2088 fcoe_clean_pending_queue(ctlr->lp);
2092 mutex_unlock(&fcoe_config_mutex);
2097 * fcoe_enable() - Enables a FCoE interface
2098 * @netdev : The net_device object the Ethernet interface to create on
2100 * Called from fcoe transport.
2102 * Returns: 0 for success
2104 static int fcoe_enable(struct net_device *netdev)
2106 struct fcoe_ctlr *ctlr;
2107 struct fcoe_interface *fcoe;
2110 mutex_lock(&fcoe_config_mutex);
2112 fcoe = fcoe_hostlist_lookup_port(netdev);
2120 ctlr = fcoe_to_ctlr(fcoe);
2122 if (!fcoe_link_ok(ctlr->lp))
2123 fcoe_ctlr_link_up(ctlr);
2126 mutex_unlock(&fcoe_config_mutex);
2131 * fcoe_ctlr_enabled() - Enable or disable an FCoE Controller
2132 * @cdev: The FCoE Controller that is being enabled or disabled
2134 * fcoe_sysfs will ensure that the state of 'enabled' has
2135 * changed, so no checking is necessary here. This routine simply
2136 * calls fcoe_enable or fcoe_disable, both of which are deprecated.
2137 * When those routines are removed the functionality can be merged
2140 static int fcoe_ctlr_enabled(struct fcoe_ctlr_device *cdev)
2142 struct fcoe_ctlr *ctlr = fcoe_ctlr_device_priv(cdev);
2143 struct fc_lport *lport = ctlr->lp;
2144 struct net_device *netdev = fcoe_netdev(lport);
2146 switch (cdev->enabled) {
2147 case FCOE_CTLR_ENABLED:
2148 return fcoe_enable(netdev);
2149 case FCOE_CTLR_DISABLED:
2150 return fcoe_disable(netdev);
2151 case FCOE_CTLR_UNUSED:
2158 * fcoe_destroy() - Destroy a FCoE interface
2159 * @netdev : The net_device object the Ethernet interface to create on
2161 * Called from fcoe transport
2163 * Returns: 0 for success
2165 static int fcoe_destroy(struct net_device *netdev)
2167 struct fcoe_ctlr *ctlr;
2168 struct fcoe_interface *fcoe;
2169 struct fc_lport *lport;
2170 struct fcoe_port *port;
2173 mutex_lock(&fcoe_config_mutex);
2175 fcoe = fcoe_hostlist_lookup_port(netdev);
2180 ctlr = fcoe_to_ctlr(fcoe);
2182 port = lport_priv(lport);
2183 list_del(&fcoe->list);
2184 queue_work(fcoe_wq, &port->destroy_work);
2187 mutex_unlock(&fcoe_config_mutex);
2192 * fcoe_destroy_work() - Destroy a FCoE port in a deferred work context
2193 * @work: Handle to the FCoE port to be destroyed
2195 static void fcoe_destroy_work(struct work_struct *work)
2197 struct fcoe_port *port;
2198 struct fcoe_interface *fcoe;
2199 struct Scsi_Host *shost;
2200 struct fc_host_attrs *fc_host;
2201 unsigned long flags;
2202 struct fc_vport *vport;
2203 struct fc_vport *next_vport;
2205 port = container_of(work, struct fcoe_port, destroy_work);
2206 shost = port->lport->host;
2207 fc_host = shost_to_fc_host(shost);
2209 /* Loop through all the vports and mark them for deletion */
2210 spin_lock_irqsave(shost->host_lock, flags);
2211 list_for_each_entry_safe(vport, next_vport, &fc_host->vports, peers) {
2212 if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING)) {
2215 vport->flags |= FC_VPORT_DELETING;
2216 queue_work(fc_host_work_q(shost),
2217 &vport->vport_delete_work);
2220 spin_unlock_irqrestore(shost->host_lock, flags);
2222 flush_workqueue(fc_host_work_q(shost));
2224 mutex_lock(&fcoe_config_mutex);
2227 fcoe_if_destroy(port->lport);
2228 fcoe_interface_cleanup(fcoe);
2230 mutex_unlock(&fcoe_config_mutex);
2234 * fcoe_match() - Check if the FCoE is supported on the given netdevice
2235 * @netdev : The net_device object the Ethernet interface to create on
2237 * Called from fcoe transport.
2239 * Returns: always returns true as this is the default FCoE transport,
2240 * i.e., support all netdevs.
2242 static bool fcoe_match(struct net_device *netdev)
2248 * fcoe_dcb_create() - Initialize DCB attributes and hooks
2249 * @netdev: The net_device object of the L2 link that should be queried
2250 * @port: The fcoe_port to bind FCoE APP priority with
2253 static void fcoe_dcb_create(struct fcoe_interface *fcoe)
2258 struct net_device *netdev = fcoe->realdev;
2259 struct fcoe_ctlr *ctlr = fcoe_to_ctlr(fcoe);
2260 struct dcb_app app = {
2262 .protocol = ETH_P_FCOE
2265 /* setup DCB priority attributes. */
2266 if (netdev && netdev->dcbnl_ops && netdev->dcbnl_ops->getdcbx) {
2267 dcbx = netdev->dcbnl_ops->getdcbx(netdev);
2269 if (dcbx & DCB_CAP_DCBX_VER_IEEE) {
2270 app.selector = IEEE_8021QAZ_APP_SEL_ETHERTYPE;
2271 up = dcb_ieee_getapp_mask(netdev, &app);
2272 app.protocol = ETH_P_FIP;
2273 fup = dcb_ieee_getapp_mask(netdev, &app);
2275 app.selector = DCB_APP_IDTYPE_ETHTYPE;
2276 up = dcb_getapp(netdev, &app);
2277 app.protocol = ETH_P_FIP;
2278 fup = dcb_getapp(netdev, &app);
2281 fcoe->priority = ffs(up) ? ffs(up) - 1 : 0;
2282 ctlr->priority = ffs(fup) ? ffs(fup) - 1 : fcoe->priority;
2287 enum fcoe_create_link_state {
2288 FCOE_CREATE_LINK_DOWN,
2289 FCOE_CREATE_LINK_UP,
2293 * _fcoe_create() - (internal) Create a fcoe interface
2294 * @netdev : The net_device object the Ethernet interface to create on
2295 * @fip_mode: The FIP mode for this creation
2296 * @link_state: The ctlr link state on creation
2298 * Called from either the libfcoe 'create' module parameter
2299 * via fcoe_create or from fcoe_syfs's ctlr_create file.
2301 * libfcoe's 'create' module parameter is deprecated so some
2302 * consolidation of code can be done when that interface is
2305 static int _fcoe_create(struct net_device *netdev, enum fip_state fip_mode,
2306 enum fcoe_create_link_state link_state)
2309 struct fcoe_ctlr_device *ctlr_dev;
2310 struct fcoe_ctlr *ctlr;
2311 struct fcoe_interface *fcoe;
2312 struct fc_lport *lport;
2314 mutex_lock(&fcoe_config_mutex);
2317 /* look for existing lport */
2318 if (fcoe_hostlist_lookup(netdev)) {
2323 fcoe = fcoe_interface_create(netdev, fip_mode);
2329 ctlr = fcoe_to_ctlr(fcoe);
2330 ctlr_dev = fcoe_ctlr_to_ctlr_dev(ctlr);
2331 lport = fcoe_if_create(fcoe, &ctlr_dev->dev, 0);
2332 if (IS_ERR(lport)) {
2333 printk(KERN_ERR "fcoe: Failed to create interface (%s)\n",
2337 fcoe_interface_cleanup(fcoe);
2341 /* Make this the "master" N_Port */
2344 /* setup DCB priority attributes. */
2345 fcoe_dcb_create(fcoe);
2347 /* start FIP Discovery and FLOGI */
2348 lport->boot_time = jiffies;
2349 fc_fabric_login(lport);
2352 * If the fcoe_ctlr_device is to be set to DISABLED
2353 * it must be done after the lport is added to the
2354 * hostlist, but before the rtnl_lock is released.
2355 * This is because the rtnl_lock protects the
2356 * hostlist that fcoe_device_notification uses. If
2357 * the FCoE Controller is intended to be created
2358 * DISABLED then 'enabled' needs to be considered
2359 * handling link events. 'enabled' must be set
2360 * before the lport can be found in the hostlist
2361 * when a link up event is received.
2363 if (link_state == FCOE_CREATE_LINK_UP)
2364 ctlr_dev->enabled = FCOE_CTLR_ENABLED;
2366 ctlr_dev->enabled = FCOE_CTLR_DISABLED;
2368 if (link_state == FCOE_CREATE_LINK_UP &&
2369 !fcoe_link_ok(lport)) {
2371 fcoe_ctlr_link_up(ctlr);
2372 mutex_unlock(&fcoe_config_mutex);
2379 mutex_unlock(&fcoe_config_mutex);
2384 * fcoe_create() - Create a fcoe interface
2385 * @netdev : The net_device object the Ethernet interface to create on
2386 * @fip_mode: The FIP mode for this creation
2388 * Called from fcoe transport
2390 * Returns: 0 for success
2392 static int fcoe_create(struct net_device *netdev, enum fip_state fip_mode)
2394 return _fcoe_create(netdev, fip_mode, FCOE_CREATE_LINK_UP);
2398 * fcoe_ctlr_alloc() - Allocate a fcoe interface from fcoe_sysfs
2399 * @netdev: The net_device to be used by the allocated FCoE Controller
2401 * This routine is called from fcoe_sysfs. It will start the fcoe_ctlr
2402 * in a link_down state. The allows the user an opportunity to configure
2403 * the FCoE Controller from sysfs before enabling the FCoE Controller.
2405 * Creating in with this routine starts the FCoE Controller in Fabric
2406 * mode. The user can change to VN2VN or another mode before enabling.
2408 static int fcoe_ctlr_alloc(struct net_device *netdev)
2410 return _fcoe_create(netdev, FIP_MODE_FABRIC,
2411 FCOE_CREATE_LINK_DOWN);
2415 * fcoe_link_ok() - Check if the link is OK for a local port
2416 * @lport: The local port to check link on
2418 * Returns: 0 if link is UP and OK, -1 if not
2421 static int fcoe_link_ok(struct fc_lport *lport)
2423 struct net_device *netdev = fcoe_netdev(lport);
2425 if (netif_oper_up(netdev))
2431 * fcoe_percpu_clean() - Clear all pending skbs for an local port
2432 * @lport: The local port whose skbs are to be cleared
2434 * Must be called with fcoe_create_mutex held to single-thread completion.
2436 * This flushes the pending skbs by adding a new skb to each queue and
2437 * waiting until they are all freed. This assures us that not only are
2438 * there no packets that will be handled by the lport, but also that any
2439 * threads already handling packet have returned.
2441 static void fcoe_percpu_clean(struct fc_lport *lport)
2443 struct fcoe_percpu_s *pp;
2444 struct sk_buff *skb;
2447 for_each_possible_cpu(cpu) {
2448 pp = &per_cpu(fcoe_percpu, cpu);
2450 if (!pp->thread || !cpu_online(cpu))
2453 skb = dev_alloc_skb(0);
2457 skb->destructor = fcoe_percpu_flush_done;
2459 spin_lock_bh(&pp->fcoe_rx_list.lock);
2460 __skb_queue_tail(&pp->fcoe_rx_list, skb);
2461 if (pp->fcoe_rx_list.qlen == 1)
2462 wake_up_process(pp->thread);
2463 spin_unlock_bh(&pp->fcoe_rx_list.lock);
2465 wait_for_completion(&fcoe_flush_completion);
2470 * fcoe_reset() - Reset a local port
2471 * @shost: The SCSI host associated with the local port to be reset
2473 * Returns: Always 0 (return value required by FC transport template)
2475 static int fcoe_reset(struct Scsi_Host *shost)
2477 struct fc_lport *lport = shost_priv(shost);
2478 struct fcoe_port *port = lport_priv(lport);
2479 struct fcoe_interface *fcoe = port->priv;
2480 struct fcoe_ctlr *ctlr = fcoe_to_ctlr(fcoe);
2481 struct fcoe_ctlr_device *cdev = fcoe_ctlr_to_ctlr_dev(ctlr);
2483 fcoe_ctlr_link_down(ctlr);
2484 fcoe_clean_pending_queue(ctlr->lp);
2486 if (cdev->enabled != FCOE_CTLR_DISABLED &&
2487 !fcoe_link_ok(ctlr->lp))
2488 fcoe_ctlr_link_up(ctlr);
2493 * fcoe_hostlist_lookup_port() - Find the FCoE interface associated with a net device
2494 * @netdev: The net device used as a key
2496 * Locking: Must be called with the RNL mutex held.
2498 * Returns: NULL or the FCoE interface
2500 static struct fcoe_interface *
2501 fcoe_hostlist_lookup_port(const struct net_device *netdev)
2503 struct fcoe_interface *fcoe;
2505 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
2506 if (fcoe->netdev == netdev)
2513 * fcoe_hostlist_lookup() - Find the local port associated with a
2515 * @netdev: The netdevice used as a key
2517 * Locking: Must be called with the RTNL mutex held
2519 * Returns: NULL or the local port
2521 static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *netdev)
2523 struct fcoe_ctlr *ctlr;
2524 struct fcoe_interface *fcoe;
2526 fcoe = fcoe_hostlist_lookup_port(netdev);
2527 ctlr = fcoe_to_ctlr(fcoe);
2528 return (fcoe) ? ctlr->lp : NULL;
2532 * fcoe_hostlist_add() - Add the FCoE interface identified by a local
2533 * port to the hostlist
2534 * @lport: The local port that identifies the FCoE interface to be added
2536 * Locking: must be called with the RTNL mutex held
2538 * Returns: 0 for success
2540 static int fcoe_hostlist_add(const struct fc_lport *lport)
2542 struct fcoe_interface *fcoe;
2543 struct fcoe_port *port;
2545 fcoe = fcoe_hostlist_lookup_port(fcoe_netdev(lport));
2547 port = lport_priv(lport);
2549 list_add_tail(&fcoe->list, &fcoe_hostlist);
2555 * fcoe_hostlist_del() - Remove the FCoE interface identified by a local
2556 * port to the hostlist
2557 * @lport: The local port that identifies the FCoE interface to be added
2559 * Locking: must be called with the RTNL mutex held
2562 static void fcoe_hostlist_del(const struct fc_lport *lport)
2564 struct fcoe_interface *fcoe;
2565 struct fcoe_port *port;
2567 port = lport_priv(lport);
2569 list_del(&fcoe->list);
2573 static struct fcoe_transport fcoe_sw_transport = {
2574 .name = {FCOE_TRANSPORT_DEFAULT},
2576 .list = LIST_HEAD_INIT(fcoe_sw_transport.list),
2577 .match = fcoe_match,
2578 .alloc = fcoe_ctlr_alloc,
2579 .create = fcoe_create,
2580 .destroy = fcoe_destroy,
2581 .enable = fcoe_enable,
2582 .disable = fcoe_disable,
2586 * fcoe_init() - Initialize fcoe.ko
2588 * Returns: 0 on success, or a negative value on failure
2590 static int __init fcoe_init(void)
2592 struct fcoe_percpu_s *p;
2596 fcoe_wq = alloc_workqueue("fcoe", 0, 0);
2600 /* register as a fcoe transport */
2601 rc = fcoe_transport_attach(&fcoe_sw_transport);
2603 printk(KERN_ERR "failed to register an fcoe transport, check "
2604 "if libfcoe is loaded\n");
2608 mutex_lock(&fcoe_config_mutex);
2610 for_each_possible_cpu(cpu) {
2611 p = &per_cpu(fcoe_percpu, cpu);
2612 skb_queue_head_init(&p->fcoe_rx_list);
2615 for_each_online_cpu(cpu)
2616 fcoe_percpu_thread_create(cpu);
2618 /* Initialize per CPU interrupt thread */
2619 rc = register_hotcpu_notifier(&fcoe_cpu_notifier);
2623 /* Setup link change notification */
2626 rc = fcoe_if_init();
2630 mutex_unlock(&fcoe_config_mutex);
2634 for_each_online_cpu(cpu) {
2635 fcoe_percpu_thread_destroy(cpu);
2637 mutex_unlock(&fcoe_config_mutex);
2638 destroy_workqueue(fcoe_wq);
2641 module_init(fcoe_init);
2644 * fcoe_exit() - Clean up fcoe.ko
2646 * Returns: 0 on success or a negative value on failure
2648 static void __exit fcoe_exit(void)
2650 struct fcoe_interface *fcoe, *tmp;
2651 struct fcoe_ctlr *ctlr;
2652 struct fcoe_port *port;
2655 mutex_lock(&fcoe_config_mutex);
2659 /* releases the associated fcoe hosts */
2661 list_for_each_entry_safe(fcoe, tmp, &fcoe_hostlist, list) {
2662 ctlr = fcoe_to_ctlr(fcoe);
2663 port = lport_priv(ctlr->lp);
2664 fcoe_hostlist_del(port->lport);
2665 queue_work(fcoe_wq, &port->destroy_work);
2669 unregister_hotcpu_notifier(&fcoe_cpu_notifier);
2671 for_each_online_cpu(cpu)
2672 fcoe_percpu_thread_destroy(cpu);
2674 mutex_unlock(&fcoe_config_mutex);
2677 * destroy_work's may be chained but destroy_workqueue()
2678 * can take care of them. Just kill the fcoe_wq.
2680 destroy_workqueue(fcoe_wq);
2683 * Detaching from the scsi transport must happen after all
2684 * destroys are done on the fcoe_wq. destroy_workqueue will
2685 * enusre the fcoe_wq is flushed.
2689 /* detach from fcoe transport */
2690 fcoe_transport_detach(&fcoe_sw_transport);
2692 module_exit(fcoe_exit);
2695 * fcoe_flogi_resp() - FCoE specific FLOGI and FDISC response handler
2696 * @seq: active sequence in the FLOGI or FDISC exchange
2697 * @fp: response frame, or error encoded in a pointer (timeout)
2698 * @arg: pointer the the fcoe_ctlr structure
2700 * This handles MAC address management for FCoE, then passes control on to
2701 * the libfc FLOGI response handler.
2703 static void fcoe_flogi_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
2705 struct fcoe_ctlr *fip = arg;
2706 struct fc_exch *exch = fc_seq_exch(seq);
2707 struct fc_lport *lport = exch->lp;
2713 mac = fr_cb(fp)->granted_mac;
2715 if (is_zero_ether_addr(mac))
2716 fcoe_ctlr_recv_flogi(fip, lport, fp);
2717 if (!is_zero_ether_addr(mac))
2718 fcoe_update_src_mac(lport, mac);
2720 fc_lport_flogi_resp(seq, fp, lport);
2724 * fcoe_logo_resp() - FCoE specific LOGO response handler
2725 * @seq: active sequence in the LOGO exchange
2726 * @fp: response frame, or error encoded in a pointer (timeout)
2727 * @arg: pointer the the fcoe_ctlr structure
2729 * This handles MAC address management for FCoE, then passes control on to
2730 * the libfc LOGO response handler.
2732 static void fcoe_logo_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
2734 struct fc_lport *lport = arg;
2735 static u8 zero_mac[ETH_ALEN] = { 0 };
2738 fcoe_update_src_mac(lport, zero_mac);
2739 fc_lport_logo_resp(seq, fp, lport);
2743 * fcoe_elsct_send - FCoE specific ELS handler
2745 * This does special case handling of FIP encapsualted ELS exchanges for FCoE,
2746 * using FCoE specific response handlers and passing the FIP controller as
2747 * the argument (the lport is still available from the exchange).
2749 * Most of the work here is just handed off to the libfc routine.
2751 static struct fc_seq *fcoe_elsct_send(struct fc_lport *lport, u32 did,
2752 struct fc_frame *fp, unsigned int op,
2753 void (*resp)(struct fc_seq *,
2756 void *arg, u32 timeout)
2758 struct fcoe_port *port = lport_priv(lport);
2759 struct fcoe_interface *fcoe = port->priv;
2760 struct fcoe_ctlr *fip = fcoe_to_ctlr(fcoe);
2761 struct fc_frame_header *fh = fc_frame_header_get(fp);
2766 if (lport->point_to_multipoint)
2768 return fc_elsct_send(lport, did, fp, op, fcoe_flogi_resp,
2771 /* only hook onto fabric logouts, not port logouts */
2772 if (ntoh24(fh->fh_d_id) != FC_FID_FLOGI)
2774 return fc_elsct_send(lport, did, fp, op, fcoe_logo_resp,
2777 return fc_elsct_send(lport, did, fp, op, resp, arg, timeout);
2781 * fcoe_vport_create() - create an fc_host/scsi_host for a vport
2782 * @vport: fc_vport object to create a new fc_host for
2783 * @disabled: start the new fc_host in a disabled state by default?
2785 * Returns: 0 for success
2787 static int fcoe_vport_create(struct fc_vport *vport, bool disabled)
2789 struct Scsi_Host *shost = vport_to_shost(vport);
2790 struct fc_lport *n_port = shost_priv(shost);
2791 struct fcoe_port *port = lport_priv(n_port);
2792 struct fcoe_interface *fcoe = port->priv;
2793 struct net_device *netdev = fcoe->netdev;
2794 struct fc_lport *vn_port;
2798 rc = fcoe_validate_vport_create(vport);
2800 fcoe_wwn_to_str(vport->port_name, buf, sizeof(buf));
2801 printk(KERN_ERR "fcoe: Failed to create vport, "
2802 "WWPN (0x%s) already exists\n",
2807 mutex_lock(&fcoe_config_mutex);
2809 vn_port = fcoe_if_create(fcoe, &vport->dev, 1);
2811 mutex_unlock(&fcoe_config_mutex);
2813 if (IS_ERR(vn_port)) {
2814 printk(KERN_ERR "fcoe: fcoe_vport_create(%s) failed\n",
2820 fc_vport_set_state(vport, FC_VPORT_DISABLED);
2822 vn_port->boot_time = jiffies;
2823 fc_fabric_login(vn_port);
2824 fc_vport_setlink(vn_port);
2830 * fcoe_vport_destroy() - destroy the fc_host/scsi_host for a vport
2831 * @vport: fc_vport object that is being destroyed
2833 * Returns: 0 for success
2835 static int fcoe_vport_destroy(struct fc_vport *vport)
2837 struct Scsi_Host *shost = vport_to_shost(vport);
2838 struct fc_lport *n_port = shost_priv(shost);
2839 struct fc_lport *vn_port = vport->dd_data;
2841 mutex_lock(&n_port->lp_mutex);
2842 list_del(&vn_port->list);
2843 mutex_unlock(&n_port->lp_mutex);
2845 mutex_lock(&fcoe_config_mutex);
2846 fcoe_if_destroy(vn_port);
2847 mutex_unlock(&fcoe_config_mutex);
2853 * fcoe_vport_disable() - change vport state
2854 * @vport: vport to bring online/offline
2855 * @disable: should the vport be disabled?
2857 static int fcoe_vport_disable(struct fc_vport *vport, bool disable)
2859 struct fc_lport *lport = vport->dd_data;
2862 fc_vport_set_state(vport, FC_VPORT_DISABLED);
2863 fc_fabric_logoff(lport);
2865 lport->boot_time = jiffies;
2866 fc_fabric_login(lport);
2867 fc_vport_setlink(lport);
2874 * fcoe_vport_set_symbolic_name() - append vport string to symbolic name
2875 * @vport: fc_vport with a new symbolic name string
2877 * After generating a new symbolic name string, a new RSPN_ID request is
2878 * sent to the name server. There is no response handler, so if it fails
2879 * for some reason it will not be retried.
2881 static void fcoe_set_vport_symbolic_name(struct fc_vport *vport)
2883 struct fc_lport *lport = vport->dd_data;
2884 struct fc_frame *fp;
2887 snprintf(fc_host_symbolic_name(lport->host), FC_SYMBOLIC_NAME_SIZE,
2888 "%s v%s over %s : %s", FCOE_NAME, FCOE_VERSION,
2889 fcoe_netdev(lport)->name, vport->symbolic_name);
2891 if (lport->state != LPORT_ST_READY)
2894 len = strnlen(fc_host_symbolic_name(lport->host), 255);
2895 fp = fc_frame_alloc(lport,
2896 sizeof(struct fc_ct_hdr) +
2897 sizeof(struct fc_ns_rspn) + len);
2900 lport->tt.elsct_send(lport, FC_FID_DIR_SERV, fp, FC_NS_RSPN_ID,
2901 NULL, NULL, 3 * lport->r_a_tov);
2904 static void fcoe_fcf_get_vlan_id(struct fcoe_fcf_device *fcf_dev)
2906 struct fcoe_ctlr_device *ctlr_dev =
2907 fcoe_fcf_dev_to_ctlr_dev(fcf_dev);
2908 struct fcoe_ctlr *ctlr = fcoe_ctlr_device_priv(ctlr_dev);
2909 struct fcoe_interface *fcoe = fcoe_ctlr_priv(ctlr);
2911 fcf_dev->vlan_id = vlan_dev_vlan_id(fcoe->netdev);
2915 * fcoe_set_port_id() - Callback from libfc when Port_ID is set.
2916 * @lport: the local port
2917 * @port_id: the port ID
2918 * @fp: the received frame, if any, that caused the port_id to be set.
2920 * This routine handles the case where we received a FLOGI and are
2921 * entering point-to-point mode. We need to call fcoe_ctlr_recv_flogi()
2922 * so it can set the non-mapped mode and gateway address.
2924 * The FLOGI LS_ACC is handled by fcoe_flogi_resp().
2926 static void fcoe_set_port_id(struct fc_lport *lport,
2927 u32 port_id, struct fc_frame *fp)
2929 struct fcoe_port *port = lport_priv(lport);
2930 struct fcoe_interface *fcoe = port->priv;
2931 struct fcoe_ctlr *ctlr = fcoe_to_ctlr(fcoe);
2933 if (fp && fc_frame_payload_op(fp) == ELS_FLOGI)
2934 fcoe_ctlr_recv_flogi(ctlr, lport, fp);