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_speed_update(struct fc_lport *);
86 static int fcoe_link_ok(struct fc_lport *);
88 static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *);
89 static int fcoe_hostlist_add(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 static void fcoe_get_lesb(struct fc_lport *, struct fc_els_lesb *);
136 /* notification function for packets from net device */
137 static struct notifier_block fcoe_notifier = {
138 .notifier_call = fcoe_device_notification,
141 /* notification function for CPU hotplug events */
142 static struct notifier_block fcoe_cpu_notifier = {
143 .notifier_call = fcoe_cpu_callback,
146 /* notification function for DCB events */
147 static struct notifier_block dcb_notifier = {
148 .notifier_call = fcoe_dcb_app_notification,
151 static struct scsi_transport_template *fcoe_nport_scsi_transport;
152 static struct scsi_transport_template *fcoe_vport_scsi_transport;
154 static int fcoe_vport_destroy(struct fc_vport *);
155 static int fcoe_vport_create(struct fc_vport *, bool disabled);
156 static int fcoe_vport_disable(struct fc_vport *, bool disable);
157 static void fcoe_set_vport_symbolic_name(struct fc_vport *);
158 static void fcoe_set_port_id(struct fc_lport *, u32, struct fc_frame *);
159 static void fcoe_ctlr_get_lesb(struct fcoe_ctlr_device *);
160 static void fcoe_fcf_get_vlan_id(struct fcoe_fcf_device *);
162 static struct fcoe_sysfs_function_template fcoe_sysfs_templ = {
163 .set_fcoe_ctlr_mode = fcoe_ctlr_set_fip_mode,
164 .set_fcoe_ctlr_enabled = fcoe_ctlr_enabled,
165 .get_fcoe_ctlr_link_fail = fcoe_ctlr_get_lesb,
166 .get_fcoe_ctlr_vlink_fail = fcoe_ctlr_get_lesb,
167 .get_fcoe_ctlr_miss_fka = fcoe_ctlr_get_lesb,
168 .get_fcoe_ctlr_symb_err = fcoe_ctlr_get_lesb,
169 .get_fcoe_ctlr_err_block = fcoe_ctlr_get_lesb,
170 .get_fcoe_ctlr_fcs_error = fcoe_ctlr_get_lesb,
172 .get_fcoe_fcf_selected = fcoe_fcf_get_selected,
173 .get_fcoe_fcf_vlan_id = fcoe_fcf_get_vlan_id,
176 static struct libfc_function_template fcoe_libfc_fcn_templ = {
177 .frame_send = fcoe_xmit,
178 .ddp_setup = fcoe_ddp_setup,
179 .ddp_done = fcoe_ddp_done,
180 .ddp_target = fcoe_ddp_target,
181 .elsct_send = fcoe_elsct_send,
182 .get_lesb = fcoe_get_lesb,
183 .lport_set_port_id = fcoe_set_port_id,
186 static struct fc_function_template fcoe_nport_fc_functions = {
187 .show_host_node_name = 1,
188 .show_host_port_name = 1,
189 .show_host_supported_classes = 1,
190 .show_host_supported_fc4s = 1,
191 .show_host_active_fc4s = 1,
192 .show_host_maxframe_size = 1,
193 .show_host_serial_number = 1,
194 .show_host_manufacturer = 1,
195 .show_host_model = 1,
196 .show_host_model_description = 1,
197 .show_host_hardware_version = 1,
198 .show_host_driver_version = 1,
199 .show_host_firmware_version = 1,
200 .show_host_optionrom_version = 1,
202 .show_host_port_id = 1,
203 .show_host_supported_speeds = 1,
204 .get_host_speed = fc_get_host_speed,
205 .show_host_speed = 1,
206 .show_host_port_type = 1,
207 .get_host_port_state = fc_get_host_port_state,
208 .show_host_port_state = 1,
209 .show_host_symbolic_name = 1,
211 .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
212 .show_rport_maxframe_size = 1,
213 .show_rport_supported_classes = 1,
215 .show_host_fabric_name = 1,
216 .show_starget_node_name = 1,
217 .show_starget_port_name = 1,
218 .show_starget_port_id = 1,
219 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
220 .show_rport_dev_loss_tmo = 1,
221 .get_fc_host_stats = fc_get_host_stats,
222 .issue_fc_host_lip = fcoe_reset,
224 .terminate_rport_io = fc_rport_terminate_io,
226 .vport_create = fcoe_vport_create,
227 .vport_delete = fcoe_vport_destroy,
228 .vport_disable = fcoe_vport_disable,
229 .set_vport_symbolic_name = fcoe_set_vport_symbolic_name,
231 .bsg_request = fc_lport_bsg_request,
234 static struct fc_function_template fcoe_vport_fc_functions = {
235 .show_host_node_name = 1,
236 .show_host_port_name = 1,
237 .show_host_supported_classes = 1,
238 .show_host_supported_fc4s = 1,
239 .show_host_active_fc4s = 1,
240 .show_host_maxframe_size = 1,
241 .show_host_serial_number = 1,
242 .show_host_manufacturer = 1,
243 .show_host_model = 1,
244 .show_host_model_description = 1,
245 .show_host_hardware_version = 1,
246 .show_host_driver_version = 1,
247 .show_host_firmware_version = 1,
248 .show_host_optionrom_version = 1,
250 .show_host_port_id = 1,
251 .show_host_supported_speeds = 1,
252 .get_host_speed = fc_get_host_speed,
253 .show_host_speed = 1,
254 .show_host_port_type = 1,
255 .get_host_port_state = fc_get_host_port_state,
256 .show_host_port_state = 1,
257 .show_host_symbolic_name = 1,
259 .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
260 .show_rport_maxframe_size = 1,
261 .show_rport_supported_classes = 1,
263 .show_host_fabric_name = 1,
264 .show_starget_node_name = 1,
265 .show_starget_port_name = 1,
266 .show_starget_port_id = 1,
267 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
268 .show_rport_dev_loss_tmo = 1,
269 .get_fc_host_stats = fc_get_host_stats,
270 .issue_fc_host_lip = fcoe_reset,
272 .terminate_rport_io = fc_rport_terminate_io,
274 .bsg_request = fc_lport_bsg_request,
277 static struct scsi_host_template fcoe_shost_template = {
278 .module = THIS_MODULE,
279 .name = "FCoE Driver",
280 .proc_name = FCOE_NAME,
281 .queuecommand = fc_queuecommand,
282 .eh_abort_handler = fc_eh_abort,
283 .eh_device_reset_handler = fc_eh_device_reset,
284 .eh_host_reset_handler = fc_eh_host_reset,
285 .slave_alloc = fc_slave_alloc,
286 .change_queue_depth = fc_change_queue_depth,
287 .change_queue_type = fc_change_queue_type,
290 .can_queue = FCOE_MAX_OUTSTANDING_COMMANDS,
291 .use_clustering = ENABLE_CLUSTERING,
292 .sg_tablesize = SG_ALL,
293 .max_sectors = 0xffff,
297 * fcoe_interface_setup() - Setup a FCoE interface
298 * @fcoe: The new FCoE interface
299 * @netdev: The net device that the fcoe interface is on
301 * Returns : 0 for success
302 * Locking: must be called with the RTNL mutex held
304 static int fcoe_interface_setup(struct fcoe_interface *fcoe,
305 struct net_device *netdev)
307 struct fcoe_ctlr *fip = fcoe_to_ctlr(fcoe);
308 struct netdev_hw_addr *ha;
309 struct net_device *real_dev;
310 u8 flogi_maddr[ETH_ALEN];
311 const struct net_device_ops *ops;
313 fcoe->netdev = netdev;
315 /* Let LLD initialize for FCoE */
316 ops = netdev->netdev_ops;
317 if (ops->ndo_fcoe_enable) {
318 if (ops->ndo_fcoe_enable(netdev))
319 FCOE_NETDEV_DBG(netdev, "Failed to enable FCoE"
320 " specific feature for LLD.\n");
323 /* Do not support for bonding device */
324 if (netdev->priv_flags & IFF_BONDING && netdev->flags & IFF_MASTER) {
325 FCOE_NETDEV_DBG(netdev, "Bonded interfaces not supported\n");
329 /* look for SAN MAC address, if multiple SAN MACs exist, only
330 * use the first one for SPMA */
331 real_dev = (netdev->priv_flags & IFF_802_1Q_VLAN) ?
332 vlan_dev_real_dev(netdev) : netdev;
333 fcoe->realdev = real_dev;
335 for_each_dev_addr(real_dev, ha) {
336 if ((ha->type == NETDEV_HW_ADDR_T_SAN) &&
337 (is_valid_ether_addr(ha->addr))) {
338 memcpy(fip->ctl_src_addr, ha->addr, ETH_ALEN);
345 /* setup Source Mac Address */
347 memcpy(fip->ctl_src_addr, netdev->dev_addr, netdev->addr_len);
350 * Add FCoE MAC address as second unicast MAC address
351 * or enter promiscuous mode if not capable of listening
352 * for multiple unicast MACs.
354 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
355 dev_uc_add(netdev, flogi_maddr);
357 dev_uc_add(netdev, fip->ctl_src_addr);
358 if (fip->mode == FIP_MODE_VN2VN) {
359 dev_mc_add(netdev, FIP_ALL_VN2VN_MACS);
360 dev_mc_add(netdev, FIP_ALL_P2P_MACS);
362 dev_mc_add(netdev, FIP_ALL_ENODE_MACS);
365 * setup the receive function from ethernet driver
366 * on the ethertype for the given device
368 fcoe->fcoe_packet_type.func = fcoe_rcv;
369 fcoe->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
370 fcoe->fcoe_packet_type.dev = netdev;
371 dev_add_pack(&fcoe->fcoe_packet_type);
373 fcoe->fip_packet_type.func = fcoe_fip_recv;
374 fcoe->fip_packet_type.type = htons(ETH_P_FIP);
375 fcoe->fip_packet_type.dev = netdev;
376 dev_add_pack(&fcoe->fip_packet_type);
382 * fcoe_interface_create() - Create a FCoE interface on a net device
383 * @netdev: The net device to create the FCoE interface on
384 * @fip_mode: The mode to use for FIP
386 * Returns: pointer to a struct fcoe_interface or NULL on error
388 static struct fcoe_interface *fcoe_interface_create(struct net_device *netdev,
389 enum fip_state fip_mode)
391 struct fcoe_ctlr_device *ctlr_dev;
392 struct fcoe_ctlr *ctlr;
393 struct fcoe_interface *fcoe;
397 if (!try_module_get(THIS_MODULE)) {
398 FCOE_NETDEV_DBG(netdev,
399 "Could not get a reference to the module\n");
400 fcoe = ERR_PTR(-EBUSY);
404 size = sizeof(struct fcoe_ctlr) + sizeof(struct fcoe_interface);
405 ctlr_dev = fcoe_ctlr_device_add(&netdev->dev, &fcoe_sysfs_templ,
408 FCOE_DBG("Failed to add fcoe_ctlr_device\n");
409 fcoe = ERR_PTR(-ENOMEM);
413 ctlr = fcoe_ctlr_device_priv(ctlr_dev);
414 fcoe = fcoe_ctlr_priv(ctlr);
421 fcoe_ctlr_init(ctlr, fip_mode);
422 ctlr->send = fcoe_fip_send;
423 ctlr->update_mac = fcoe_update_src_mac;
424 ctlr->get_src_addr = fcoe_get_src_mac;
426 err = fcoe_interface_setup(fcoe, netdev);
428 fcoe_ctlr_destroy(ctlr);
429 fcoe_ctlr_device_delete(ctlr_dev);
438 module_put(THIS_MODULE);
444 * fcoe_interface_remove() - remove FCoE interface from netdev
445 * @fcoe: The FCoE interface to be cleaned up
447 * Caller must be holding the RTNL mutex
449 static void fcoe_interface_remove(struct fcoe_interface *fcoe)
451 struct net_device *netdev = fcoe->netdev;
452 struct fcoe_ctlr *fip = fcoe_to_ctlr(fcoe);
453 u8 flogi_maddr[ETH_ALEN];
454 const struct net_device_ops *ops;
457 * Don't listen for Ethernet packets anymore.
458 * synchronize_net() ensures that the packet handlers are not running
459 * on another CPU. dev_remove_pack() would do that, this calls the
460 * unsyncronized version __dev_remove_pack() to avoid multiple delays.
462 __dev_remove_pack(&fcoe->fcoe_packet_type);
463 __dev_remove_pack(&fcoe->fip_packet_type);
466 /* Delete secondary MAC addresses */
467 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
468 dev_uc_del(netdev, flogi_maddr);
470 dev_uc_del(netdev, fip->ctl_src_addr);
471 if (fip->mode == FIP_MODE_VN2VN) {
472 dev_mc_del(netdev, FIP_ALL_VN2VN_MACS);
473 dev_mc_del(netdev, FIP_ALL_P2P_MACS);
475 dev_mc_del(netdev, FIP_ALL_ENODE_MACS);
477 /* Tell the LLD we are done w/ FCoE */
478 ops = netdev->netdev_ops;
479 if (ops->ndo_fcoe_disable) {
480 if (ops->ndo_fcoe_disable(netdev))
481 FCOE_NETDEV_DBG(netdev, "Failed to disable FCoE"
482 " specific feature for LLD.\n");
489 * fcoe_interface_cleanup() - Clean up a FCoE interface
490 * @fcoe: The FCoE interface to be cleaned up
492 static void fcoe_interface_cleanup(struct fcoe_interface *fcoe)
494 struct net_device *netdev = fcoe->netdev;
495 struct fcoe_ctlr *fip = fcoe_to_ctlr(fcoe);
496 struct fcoe_ctlr_device *ctlr_dev = fcoe_ctlr_to_ctlr_dev(fip);
500 fcoe_interface_remove(fcoe);
503 /* Release the self-reference taken during fcoe_interface_create() */
504 /* tear-down the FCoE controller */
505 fcoe_ctlr_destroy(fip);
506 scsi_host_put(fip->lp->host);
507 fcoe_ctlr_device_delete(ctlr_dev);
509 module_put(THIS_MODULE);
513 * fcoe_fip_recv() - Handler for received FIP frames
514 * @skb: The receive skb
515 * @netdev: The associated net device
516 * @ptype: The packet_type structure which was used to register this handler
517 * @orig_dev: The original net_device the the skb was received on.
518 * (in case dev is a bond)
520 * Returns: 0 for success
522 static int fcoe_fip_recv(struct sk_buff *skb, struct net_device *netdev,
523 struct packet_type *ptype,
524 struct net_device *orig_dev)
526 struct fcoe_interface *fcoe;
527 struct fcoe_ctlr *ctlr;
529 fcoe = container_of(ptype, struct fcoe_interface, fip_packet_type);
530 ctlr = fcoe_to_ctlr(fcoe);
531 fcoe_ctlr_recv(ctlr, skb);
536 * fcoe_port_send() - Send an Ethernet-encapsulated FIP/FCoE frame
537 * @port: The FCoE port
538 * @skb: The FIP/FCoE packet to be sent
540 static void fcoe_port_send(struct fcoe_port *port, struct sk_buff *skb)
542 if (port->fcoe_pending_queue.qlen)
543 fcoe_check_wait_queue(port->lport, skb);
544 else if (fcoe_start_io(skb))
545 fcoe_check_wait_queue(port->lport, skb);
549 * fcoe_fip_send() - Send an Ethernet-encapsulated FIP frame
550 * @fip: The FCoE controller
551 * @skb: The FIP packet to be sent
553 static void fcoe_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
555 skb->dev = fcoe_from_ctlr(fip)->netdev;
556 fcoe_port_send(lport_priv(fip->lp), skb);
560 * fcoe_update_src_mac() - Update the Ethernet MAC filters
561 * @lport: The local port to update the source MAC on
562 * @addr: Unicast MAC address to add
564 * Remove any previously-set unicast MAC filter.
565 * Add secondary FCoE MAC address filter for our OUI.
567 static void fcoe_update_src_mac(struct fc_lport *lport, u8 *addr)
569 struct fcoe_port *port = lport_priv(lport);
570 struct fcoe_interface *fcoe = port->priv;
572 if (!is_zero_ether_addr(port->data_src_addr))
573 dev_uc_del(fcoe->netdev, port->data_src_addr);
574 if (!is_zero_ether_addr(addr))
575 dev_uc_add(fcoe->netdev, addr);
576 memcpy(port->data_src_addr, addr, ETH_ALEN);
580 * fcoe_get_src_mac() - return the Ethernet source address for an lport
581 * @lport: libfc lport
583 static u8 *fcoe_get_src_mac(struct fc_lport *lport)
585 struct fcoe_port *port = lport_priv(lport);
587 return port->data_src_addr;
591 * fcoe_lport_config() - Set up a local port
592 * @lport: The local port to be setup
594 * Returns: 0 for success
596 static int fcoe_lport_config(struct fc_lport *lport)
600 lport->max_retry_count = 3;
601 lport->max_rport_retry_count = 3;
602 lport->e_d_tov = 2 * 1000; /* FC-FS default */
603 lport->r_a_tov = 2 * 2 * 1000;
604 lport->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
605 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
606 lport->does_npiv = 1;
608 fc_lport_init_stats(lport);
610 /* lport fc_lport related configuration */
611 fc_lport_config(lport);
613 /* offload related configuration */
614 lport->crc_offload = 0;
615 lport->seq_offload = 0;
616 lport->lro_enabled = 0;
624 * fcoe_netdev_features_change - Updates the lport's offload flags based
625 * on the LLD netdev's FCoE feature flags
627 static void fcoe_netdev_features_change(struct fc_lport *lport,
628 struct net_device *netdev)
630 mutex_lock(&lport->lp_mutex);
632 if (netdev->features & NETIF_F_SG)
637 if (netdev->features & NETIF_F_FCOE_CRC) {
638 lport->crc_offload = 1;
639 FCOE_NETDEV_DBG(netdev, "Supports FCCRC offload\n");
641 lport->crc_offload = 0;
644 if (netdev->features & NETIF_F_FSO) {
645 lport->seq_offload = 1;
646 lport->lso_max = netdev->gso_max_size;
647 FCOE_NETDEV_DBG(netdev, "Supports LSO for max len 0x%x\n",
650 lport->seq_offload = 0;
654 if (netdev->fcoe_ddp_xid) {
655 lport->lro_enabled = 1;
656 lport->lro_xid = netdev->fcoe_ddp_xid;
657 FCOE_NETDEV_DBG(netdev, "Supports LRO for max xid 0x%x\n",
660 lport->lro_enabled = 0;
664 mutex_unlock(&lport->lp_mutex);
668 * fcoe_netdev_config() - Set up net devive for SW FCoE
669 * @lport: The local port that is associated with the net device
670 * @netdev: The associated net device
672 * Must be called after fcoe_lport_config() as it will use local port mutex
674 * Returns: 0 for success
676 static int fcoe_netdev_config(struct fc_lport *lport, struct net_device *netdev)
680 struct fcoe_interface *fcoe;
681 struct fcoe_ctlr *ctlr;
682 struct fcoe_port *port;
684 /* Setup lport private data to point to fcoe softc */
685 port = lport_priv(lport);
687 ctlr = fcoe_to_ctlr(fcoe);
690 * Determine max frame size based on underlying device and optional
691 * user-configured limit. If the MFS is too low, fcoe_link_ok()
692 * will return 0, so do this first.
695 if (netdev->features & NETIF_F_FCOE_MTU) {
697 FCOE_NETDEV_DBG(netdev, "Supports FCOE_MTU of %d bytes\n", mfs);
699 mfs -= (sizeof(struct fcoe_hdr) + sizeof(struct fcoe_crc_eof));
700 if (fc_set_mfs(lport, mfs))
703 /* offload features support */
704 fcoe_netdev_features_change(lport, netdev);
706 skb_queue_head_init(&port->fcoe_pending_queue);
707 port->fcoe_pending_queue_active = 0;
708 setup_timer(&port->timer, fcoe_queue_timer, (unsigned long)lport);
710 fcoe_link_speed_update(lport);
713 if (fcoe_get_wwn(netdev, &wwnn, NETDEV_FCOE_WWNN))
714 wwnn = fcoe_wwn_from_mac(ctlr->ctl_src_addr, 1, 0);
715 fc_set_wwnn(lport, wwnn);
716 if (fcoe_get_wwn(netdev, &wwpn, NETDEV_FCOE_WWPN))
717 wwpn = fcoe_wwn_from_mac(ctlr->ctl_src_addr,
719 fc_set_wwpn(lport, wwpn);
726 * fcoe_shost_config() - Set up the SCSI host associated with a local port
727 * @lport: The local port
728 * @dev: The device associated with the SCSI host
730 * Must be called after fcoe_lport_config() and fcoe_netdev_config()
732 * Returns: 0 for success
734 static int fcoe_shost_config(struct fc_lport *lport, struct device *dev)
738 /* lport scsi host config */
739 lport->host->max_lun = FCOE_MAX_LUN;
740 lport->host->max_id = FCOE_MAX_FCP_TARGET;
741 lport->host->max_channel = 0;
742 lport->host->max_cmd_len = FCOE_MAX_CMD_LEN;
745 lport->host->transportt = fcoe_vport_scsi_transport;
747 lport->host->transportt = fcoe_nport_scsi_transport;
749 /* add the new host to the SCSI-ml */
750 rc = scsi_add_host(lport->host, dev);
752 FCOE_NETDEV_DBG(fcoe_netdev(lport), "fcoe_shost_config: "
753 "error on scsi_add_host\n");
758 fc_host_max_npiv_vports(lport->host) = USHRT_MAX;
760 snprintf(fc_host_symbolic_name(lport->host), FC_SYMBOLIC_NAME_SIZE,
761 "%s v%s over %s", FCOE_NAME, FCOE_VERSION,
762 fcoe_netdev(lport)->name);
769 * fcoe_fdmi_info() - Get FDMI related info from net devive for SW FCoE
770 * @lport: The local port that is associated with the net device
771 * @netdev: The associated net device
773 * Must be called after fcoe_shost_config() as it will use local port mutex
776 static void fcoe_fdmi_info(struct fc_lport *lport, struct net_device *netdev)
778 struct fcoe_interface *fcoe;
779 struct fcoe_port *port;
780 struct net_device *realdev;
782 struct netdev_fcoe_hbainfo fdmi;
784 port = lport_priv(lport);
786 realdev = fcoe->realdev;
791 /* No FDMI state m/c for NPIV ports */
795 if (realdev->netdev_ops->ndo_fcoe_get_hbainfo) {
796 memset(&fdmi, 0, sizeof(fdmi));
797 rc = realdev->netdev_ops->ndo_fcoe_get_hbainfo(realdev,
800 printk(KERN_INFO "fcoe: Failed to retrieve FDMI "
801 "information from netdev.\n");
805 snprintf(fc_host_serial_number(lport->host),
806 FC_SERIAL_NUMBER_SIZE,
809 snprintf(fc_host_manufacturer(lport->host),
810 FC_SERIAL_NUMBER_SIZE,
813 snprintf(fc_host_model(lport->host),
814 FC_SYMBOLIC_NAME_SIZE,
817 snprintf(fc_host_model_description(lport->host),
818 FC_SYMBOLIC_NAME_SIZE,
820 fdmi.model_description);
821 snprintf(fc_host_hardware_version(lport->host),
822 FC_VERSION_STRING_SIZE,
824 fdmi.hardware_version);
825 snprintf(fc_host_driver_version(lport->host),
826 FC_VERSION_STRING_SIZE,
828 fdmi.driver_version);
829 snprintf(fc_host_optionrom_version(lport->host),
830 FC_VERSION_STRING_SIZE,
832 fdmi.optionrom_version);
833 snprintf(fc_host_firmware_version(lport->host),
834 FC_VERSION_STRING_SIZE,
836 fdmi.firmware_version);
838 /* Enable FDMI lport states */
839 lport->fdmi_enabled = 1;
841 lport->fdmi_enabled = 0;
842 printk(KERN_INFO "fcoe: No FDMI support.\n");
847 * fcoe_oem_match() - The match routine for the offloaded exchange manager
850 * This routine will be associated with an exchange manager (EM). When
851 * the libfc exchange handling code is looking for an EM to use it will
852 * call this routine and pass it the frame that it wishes to send. This
853 * routine will return True if the associated EM is to be used and False
854 * if the echange code should continue looking for an EM.
856 * The offload EM that this routine is associated with will handle any
857 * packets that are for SCSI read requests.
859 * This has been enhanced to work when FCoE stack is operating in target
862 * Returns: True for read types I/O, otherwise returns false.
864 static bool fcoe_oem_match(struct fc_frame *fp)
866 struct fc_frame_header *fh = fc_frame_header_get(fp);
867 struct fcp_cmnd *fcp;
869 if (fc_fcp_is_read(fr_fsp(fp)) &&
870 (fr_fsp(fp)->data_len > fcoe_ddp_min))
872 else if ((fr_fsp(fp) == NULL) &&
873 (fh->fh_r_ctl == FC_RCTL_DD_UNSOL_CMD) &&
874 (ntohs(fh->fh_rx_id) == FC_XID_UNKNOWN)) {
875 fcp = fc_frame_payload_get(fp, sizeof(*fcp));
876 if ((fcp->fc_flags & FCP_CFL_WRDATA) &&
877 (ntohl(fcp->fc_dl) > fcoe_ddp_min))
884 * fcoe_em_config() - Allocate and configure an exchange manager
885 * @lport: The local port that the new EM will be associated with
887 * Returns: 0 on success
889 static inline int fcoe_em_config(struct fc_lport *lport)
891 struct fcoe_port *port = lport_priv(lport);
892 struct fcoe_interface *fcoe = port->priv;
893 struct fcoe_interface *oldfcoe = NULL;
894 struct net_device *old_real_dev, *cur_real_dev;
895 u16 min_xid = FCOE_MIN_XID;
896 u16 max_xid = FCOE_MAX_XID;
899 * Check if need to allocate an em instance for
900 * offload exchange ids to be shared across all VN_PORTs/lport.
902 if (!lport->lro_enabled || !lport->lro_xid ||
903 (lport->lro_xid >= max_xid)) {
909 * Reuse existing offload em instance in case
910 * it is already allocated on real eth device
912 if (fcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
913 cur_real_dev = vlan_dev_real_dev(fcoe->netdev);
915 cur_real_dev = fcoe->netdev;
917 list_for_each_entry(oldfcoe, &fcoe_hostlist, list) {
918 if (oldfcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
919 old_real_dev = vlan_dev_real_dev(oldfcoe->netdev);
921 old_real_dev = oldfcoe->netdev;
923 if (cur_real_dev == old_real_dev) {
924 fcoe->oem = oldfcoe->oem;
930 if (!fc_exch_mgr_add(lport, fcoe->oem, fcoe_oem_match)) {
931 printk(KERN_ERR "fcoe_em_config: failed to add "
932 "offload em:%p on interface:%s\n",
933 fcoe->oem, fcoe->netdev->name);
937 fcoe->oem = fc_exch_mgr_alloc(lport, FC_CLASS_3,
938 FCOE_MIN_XID, lport->lro_xid,
941 printk(KERN_ERR "fcoe_em_config: failed to allocate "
942 "em for offload exches on interface:%s\n",
949 * Exclude offload EM xid range from next EM xid range.
951 min_xid += lport->lro_xid + 1;
954 if (!fc_exch_mgr_alloc(lport, FC_CLASS_3, min_xid, max_xid, NULL)) {
955 printk(KERN_ERR "fcoe_em_config: failed to "
956 "allocate em on interface %s\n", fcoe->netdev->name);
964 * fcoe_if_destroy() - Tear down a SW FCoE instance
965 * @lport: The local port to be destroyed
968 static void fcoe_if_destroy(struct fc_lport *lport)
970 struct fcoe_port *port = lport_priv(lport);
971 struct fcoe_interface *fcoe = port->priv;
972 struct net_device *netdev = fcoe->netdev;
974 FCOE_NETDEV_DBG(netdev, "Destroying interface\n");
976 /* Logout of the fabric */
977 fc_fabric_logoff(lport);
979 /* Cleanup the fc_lport */
980 fc_lport_destroy(lport);
982 /* Stop the transmit retry timer */
983 del_timer_sync(&port->timer);
985 /* Free existing transmit skbs */
986 fcoe_clean_pending_queue(lport);
989 if (!is_zero_ether_addr(port->data_src_addr))
990 dev_uc_del(netdev, port->data_src_addr);
994 fcoe_interface_remove(fcoe);
997 /* Free queued packets for the per-CPU receive threads */
998 fcoe_percpu_clean(lport);
1000 /* Detach from the scsi-ml */
1001 fc_remove_host(lport->host);
1002 scsi_remove_host(lport->host);
1004 /* Destroy lport scsi_priv */
1005 fc_fcp_destroy(lport);
1007 /* There are no more rports or I/O, free the EM */
1008 fc_exch_mgr_free(lport);
1010 /* Free memory used by statistical counters */
1011 fc_lport_free_stats(lport);
1014 * Release the Scsi_Host for vport but hold on to
1015 * master lport until it fcoe interface fully cleaned-up.
1018 scsi_host_put(lport->host);
1022 * fcoe_ddp_setup() - Call a LLD's ddp_setup through the net device
1023 * @lport: The local port to setup DDP for
1024 * @xid: The exchange ID for this DDP transfer
1025 * @sgl: The scatterlist describing this transfer
1026 * @sgc: The number of sg items
1028 * Returns: 0 if the DDP context was not configured
1030 static int fcoe_ddp_setup(struct fc_lport *lport, u16 xid,
1031 struct scatterlist *sgl, unsigned int sgc)
1033 struct net_device *netdev = fcoe_netdev(lport);
1035 if (netdev->netdev_ops->ndo_fcoe_ddp_setup)
1036 return netdev->netdev_ops->ndo_fcoe_ddp_setup(netdev,
1044 * fcoe_ddp_target() - Call a LLD's ddp_target through the net device
1045 * @lport: The local port to setup DDP for
1046 * @xid: The exchange ID for this DDP transfer
1047 * @sgl: The scatterlist describing this transfer
1048 * @sgc: The number of sg items
1050 * Returns: 0 if the DDP context was not configured
1052 static int fcoe_ddp_target(struct fc_lport *lport, u16 xid,
1053 struct scatterlist *sgl, unsigned int sgc)
1055 struct net_device *netdev = fcoe_netdev(lport);
1057 if (netdev->netdev_ops->ndo_fcoe_ddp_target)
1058 return netdev->netdev_ops->ndo_fcoe_ddp_target(netdev, xid,
1066 * fcoe_ddp_done() - Call a LLD's ddp_done through the net device
1067 * @lport: The local port to complete DDP on
1068 * @xid: The exchange ID for this DDP transfer
1070 * Returns: the length of data that have been completed by DDP
1072 static int fcoe_ddp_done(struct fc_lport *lport, u16 xid)
1074 struct net_device *netdev = fcoe_netdev(lport);
1076 if (netdev->netdev_ops->ndo_fcoe_ddp_done)
1077 return netdev->netdev_ops->ndo_fcoe_ddp_done(netdev, xid);
1082 * fcoe_if_create() - Create a FCoE instance on an interface
1083 * @fcoe: The FCoE interface to create a local port on
1084 * @parent: The device pointer to be the parent in sysfs for the SCSI host
1085 * @npiv: Indicates if the port is a vport or not
1087 * Creates a fc_lport instance and a Scsi_Host instance and configure them.
1089 * Returns: The allocated fc_lport or an error pointer
1091 static struct fc_lport *fcoe_if_create(struct fcoe_interface *fcoe,
1092 struct device *parent, int npiv)
1094 struct fcoe_ctlr *ctlr = fcoe_to_ctlr(fcoe);
1095 struct net_device *netdev = fcoe->netdev;
1096 struct fc_lport *lport, *n_port;
1097 struct fcoe_port *port;
1098 struct Scsi_Host *shost;
1101 * parent is only a vport if npiv is 1,
1102 * but we'll only use vport in that case so go ahead and set it
1104 struct fc_vport *vport = dev_to_vport(parent);
1106 FCOE_NETDEV_DBG(netdev, "Create Interface\n");
1109 lport = libfc_host_alloc(&fcoe_shost_template, sizeof(*port));
1111 lport = libfc_vport_create(vport, sizeof(*port));
1114 FCOE_NETDEV_DBG(netdev, "Could not allocate host structure\n");
1118 port = lport_priv(lport);
1119 port->lport = lport;
1121 port->max_queue_depth = FCOE_MAX_QUEUE_DEPTH;
1122 port->min_queue_depth = FCOE_MIN_QUEUE_DEPTH;
1123 INIT_WORK(&port->destroy_work, fcoe_destroy_work);
1125 /* configure a fc_lport including the exchange manager */
1126 rc = fcoe_lport_config(lport);
1128 FCOE_NETDEV_DBG(netdev, "Could not configure lport for the "
1134 FCOE_NETDEV_DBG(netdev, "Setting vport names, "
1135 "%16.16llx %16.16llx\n",
1136 vport->node_name, vport->port_name);
1137 fc_set_wwnn(lport, vport->node_name);
1138 fc_set_wwpn(lport, vport->port_name);
1141 /* configure lport network properties */
1142 rc = fcoe_netdev_config(lport, netdev);
1144 FCOE_NETDEV_DBG(netdev, "Could not configure netdev for the "
1146 goto out_lp_destroy;
1149 /* configure lport scsi host properties */
1150 rc = fcoe_shost_config(lport, parent);
1152 FCOE_NETDEV_DBG(netdev, "Could not configure shost for the "
1154 goto out_lp_destroy;
1157 /* Initialize the library */
1158 rc = fcoe_libfc_config(lport, ctlr, &fcoe_libfc_fcn_templ, 1);
1160 FCOE_NETDEV_DBG(netdev, "Could not configure libfc for the "
1162 goto out_lp_destroy;
1165 /* Initialized FDMI information */
1166 fcoe_fdmi_info(lport, netdev);
1169 * fcoe_em_alloc() and fcoe_hostlist_add() both
1170 * need to be atomic with respect to other changes to the
1171 * hostlist since fcoe_em_alloc() looks for an existing EM
1172 * instance on host list updated by fcoe_hostlist_add().
1174 * This is currently handled through the fcoe_config_mutex
1178 /* lport exch manager allocation */
1179 rc = fcoe_em_config(lport);
1181 shost = vport_to_shost(vport);
1182 n_port = shost_priv(shost);
1183 rc = fc_exch_mgr_list_clone(n_port, lport);
1187 FCOE_NETDEV_DBG(netdev, "Could not configure the EM\n");
1188 goto out_lp_destroy;
1194 fc_exch_mgr_free(lport);
1196 scsi_host_put(lport->host);
1202 * fcoe_if_init() - Initialization routine for fcoe.ko
1204 * Attaches the SW FCoE transport to the FC transport
1206 * Returns: 0 on success
1208 static int __init fcoe_if_init(void)
1210 /* attach to scsi transport */
1211 fcoe_nport_scsi_transport =
1212 fc_attach_transport(&fcoe_nport_fc_functions);
1213 fcoe_vport_scsi_transport =
1214 fc_attach_transport(&fcoe_vport_fc_functions);
1216 if (!fcoe_nport_scsi_transport) {
1217 printk(KERN_ERR "fcoe: Failed to attach to the FC transport\n");
1225 * fcoe_if_exit() - Tear down fcoe.ko
1227 * Detaches the SW FCoE transport from the FC transport
1229 * Returns: 0 on success
1231 static int __exit fcoe_if_exit(void)
1233 fc_release_transport(fcoe_nport_scsi_transport);
1234 fc_release_transport(fcoe_vport_scsi_transport);
1235 fcoe_nport_scsi_transport = NULL;
1236 fcoe_vport_scsi_transport = NULL;
1241 * fcoe_percpu_thread_create() - Create a receive thread for an online CPU
1242 * @cpu: The CPU index of the CPU to create a receive thread for
1244 static void fcoe_percpu_thread_create(unsigned int cpu)
1246 struct fcoe_percpu_s *p;
1247 struct task_struct *thread;
1249 p = &per_cpu(fcoe_percpu, cpu);
1251 thread = kthread_create_on_node(fcoe_percpu_receive_thread,
1252 (void *)p, cpu_to_node(cpu),
1253 "fcoethread/%d", cpu);
1255 if (likely(!IS_ERR(thread))) {
1256 kthread_bind(thread, cpu);
1257 wake_up_process(thread);
1259 spin_lock_bh(&p->fcoe_rx_list.lock);
1261 spin_unlock_bh(&p->fcoe_rx_list.lock);
1266 * fcoe_percpu_thread_destroy() - Remove the receive thread of a CPU
1267 * @cpu: The CPU index of the CPU whose receive thread is to be destroyed
1269 * Destroys a per-CPU Rx thread. Any pending skbs are moved to the
1270 * current CPU's Rx thread. If the thread being destroyed is bound to
1271 * the CPU processing this context the skbs will be freed.
1273 static void fcoe_percpu_thread_destroy(unsigned int cpu)
1275 struct fcoe_percpu_s *p;
1276 struct task_struct *thread;
1277 struct page *crc_eof;
1278 struct sk_buff *skb;
1280 struct fcoe_percpu_s *p0;
1281 unsigned targ_cpu = get_cpu();
1282 #endif /* CONFIG_SMP */
1284 FCOE_DBG("Destroying receive thread for CPU %d\n", cpu);
1286 /* Prevent any new skbs from being queued for this CPU. */
1287 p = &per_cpu(fcoe_percpu, cpu);
1288 spin_lock_bh(&p->fcoe_rx_list.lock);
1291 crc_eof = p->crc_eof_page;
1292 p->crc_eof_page = NULL;
1293 p->crc_eof_offset = 0;
1294 spin_unlock_bh(&p->fcoe_rx_list.lock);
1298 * Don't bother moving the skb's if this context is running
1299 * on the same CPU that is having its thread destroyed. This
1300 * can easily happen when the module is removed.
1302 if (cpu != targ_cpu) {
1303 p0 = &per_cpu(fcoe_percpu, targ_cpu);
1304 spin_lock_bh(&p0->fcoe_rx_list.lock);
1306 FCOE_DBG("Moving frames from CPU %d to CPU %d\n",
1309 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1310 __skb_queue_tail(&p0->fcoe_rx_list, skb);
1311 spin_unlock_bh(&p0->fcoe_rx_list.lock);
1314 * The targeted CPU is not initialized and cannot accept
1315 * new skbs. Unlock the targeted CPU and drop the skbs
1316 * on the CPU that is going offline.
1318 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1320 spin_unlock_bh(&p0->fcoe_rx_list.lock);
1324 * This scenario occurs when the module is being removed
1325 * and all threads are being destroyed. skbs will continue
1326 * to be shifted from the CPU thread that is being removed
1327 * to the CPU thread associated with the CPU that is processing
1328 * the module removal. Once there is only one CPU Rx thread it
1329 * will reach this case and we will drop all skbs and later
1332 spin_lock_bh(&p->fcoe_rx_list.lock);
1333 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1335 spin_unlock_bh(&p->fcoe_rx_list.lock);
1340 * This a non-SMP scenario where the singular Rx thread is
1341 * being removed. Free all skbs and stop the thread.
1343 spin_lock_bh(&p->fcoe_rx_list.lock);
1344 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1346 spin_unlock_bh(&p->fcoe_rx_list.lock);
1350 kthread_stop(thread);
1357 * fcoe_cpu_callback() - Handler for CPU hotplug events
1358 * @nfb: The callback data block
1359 * @action: The event triggering the callback
1360 * @hcpu: The index of the CPU that the event is for
1362 * This creates or destroys per-CPU data for fcoe
1364 * Returns NOTIFY_OK always.
1366 static int fcoe_cpu_callback(struct notifier_block *nfb,
1367 unsigned long action, void *hcpu)
1369 unsigned cpu = (unsigned long)hcpu;
1373 case CPU_ONLINE_FROZEN:
1374 FCOE_DBG("CPU %x online: Create Rx thread\n", cpu);
1375 fcoe_percpu_thread_create(cpu);
1378 case CPU_DEAD_FROZEN:
1379 FCOE_DBG("CPU %x offline: Remove Rx thread\n", cpu);
1380 fcoe_percpu_thread_destroy(cpu);
1389 * fcoe_select_cpu() - Selects CPU to handle post-processing of incoming
1392 * This routine selects next CPU based on cpumask to distribute
1393 * incoming requests in round robin.
1395 * Returns: int CPU number
1397 static inline unsigned int fcoe_select_cpu(void)
1399 static unsigned int selected_cpu;
1401 selected_cpu = cpumask_next(selected_cpu, cpu_online_mask);
1402 if (selected_cpu >= nr_cpu_ids)
1403 selected_cpu = cpumask_first(cpu_online_mask);
1405 return selected_cpu;
1409 * fcoe_rcv() - Receive packets from a net device
1410 * @skb: The received packet
1411 * @netdev: The net device that the packet was received on
1412 * @ptype: The packet type context
1413 * @olddev: The last device net device
1415 * This routine is called by NET_RX_SOFTIRQ. It receives a packet, builds a
1416 * FC frame and passes the frame to libfc.
1418 * Returns: 0 for success
1420 static int fcoe_rcv(struct sk_buff *skb, struct net_device *netdev,
1421 struct packet_type *ptype, struct net_device *olddev)
1423 struct fc_lport *lport;
1424 struct fcoe_rcv_info *fr;
1425 struct fcoe_ctlr *ctlr;
1426 struct fcoe_interface *fcoe;
1427 struct fc_frame_header *fh;
1428 struct fcoe_percpu_s *fps;
1432 fcoe = container_of(ptype, struct fcoe_interface, fcoe_packet_type);
1433 ctlr = fcoe_to_ctlr(fcoe);
1435 if (unlikely(!lport)) {
1436 FCOE_NETDEV_DBG(netdev, "Cannot find hba structure");
1439 if (!lport->link_up)
1442 FCOE_NETDEV_DBG(netdev, "skb_info: len:%d data_len:%d head:%p "
1443 "data:%p tail:%p end:%p sum:%d dev:%s",
1444 skb->len, skb->data_len, skb->head, skb->data,
1445 skb_tail_pointer(skb), skb_end_pointer(skb),
1446 skb->csum, skb->dev ? skb->dev->name : "<NULL>");
1450 if (is_fip_mode(ctlr) &&
1451 compare_ether_addr(eh->h_source, ctlr->dest_addr)) {
1452 FCOE_NETDEV_DBG(netdev, "wrong source mac address:%pM\n",
1458 * Check for minimum frame length, and make sure required FCoE
1459 * and FC headers are pulled into the linear data area.
1461 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
1462 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
1465 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
1466 fh = (struct fc_frame_header *) skb_transport_header(skb);
1468 if (ntoh24(&eh->h_dest[3]) != ntoh24(fh->fh_d_id)) {
1469 FCOE_NETDEV_DBG(netdev, "FC frame d_id mismatch with MAC:%pM\n",
1474 fr = fcoe_dev_from_skb(skb);
1478 * In case the incoming frame's exchange is originated from
1479 * the initiator, then received frame's exchange id is ANDed
1480 * with fc_cpu_mask bits to get the same cpu on which exchange
1481 * was originated, otherwise select cpu using rx exchange id
1482 * or fcoe_select_cpu().
1484 if (ntoh24(fh->fh_f_ctl) & FC_FC_EX_CTX)
1485 cpu = ntohs(fh->fh_ox_id) & fc_cpu_mask;
1487 if (ntohs(fh->fh_rx_id) == FC_XID_UNKNOWN)
1488 cpu = fcoe_select_cpu();
1490 cpu = ntohs(fh->fh_rx_id) & fc_cpu_mask;
1493 if (cpu >= nr_cpu_ids)
1496 fps = &per_cpu(fcoe_percpu, cpu);
1497 spin_lock(&fps->fcoe_rx_list.lock);
1498 if (unlikely(!fps->thread)) {
1500 * The targeted CPU is not ready, let's target
1501 * the first CPU now. For non-SMP systems this
1502 * will check the same CPU twice.
1504 FCOE_NETDEV_DBG(netdev, "CPU is online, but no receive thread "
1505 "ready for incoming skb- using first online "
1508 spin_unlock(&fps->fcoe_rx_list.lock);
1509 cpu = cpumask_first(cpu_online_mask);
1510 fps = &per_cpu(fcoe_percpu, cpu);
1511 spin_lock(&fps->fcoe_rx_list.lock);
1513 spin_unlock(&fps->fcoe_rx_list.lock);
1519 * We now have a valid CPU that we're targeting for
1520 * this skb. We also have this receive thread locked,
1521 * so we're free to queue skbs into it's queue.
1525 * Note: We used to have a set of conditions under which we would
1526 * call fcoe_recv_frame directly, rather than queuing to the rx list
1527 * as it could save a few cycles, but doing so is prohibited, as
1528 * fcoe_recv_frame has several paths that may sleep, which is forbidden
1529 * in softirq context.
1531 __skb_queue_tail(&fps->fcoe_rx_list, skb);
1532 if (fps->thread->state == TASK_INTERRUPTIBLE)
1533 wake_up_process(fps->thread);
1534 spin_unlock(&fps->fcoe_rx_list.lock);
1538 per_cpu_ptr(lport->stats, get_cpu())->ErrorFrames++;
1546 * fcoe_alloc_paged_crc_eof() - Allocate a page to be used for the trailer CRC
1547 * @skb: The packet to be transmitted
1548 * @tlen: The total length of the trailer
1550 * Returns: 0 for success
1552 static int fcoe_alloc_paged_crc_eof(struct sk_buff *skb, int tlen)
1554 struct fcoe_percpu_s *fps;
1557 fps = &get_cpu_var(fcoe_percpu);
1558 rc = fcoe_get_paged_crc_eof(skb, tlen, fps);
1559 put_cpu_var(fcoe_percpu);
1565 * fcoe_xmit() - Transmit a FCoE frame
1566 * @lport: The local port that the frame is to be transmitted for
1567 * @fp: The frame to be transmitted
1569 * Return: 0 for success
1571 static int fcoe_xmit(struct fc_lport *lport, struct fc_frame *fp)
1576 struct fcoe_crc_eof *cp;
1577 struct sk_buff *skb;
1578 struct fc_stats *stats;
1579 struct fc_frame_header *fh;
1580 unsigned int hlen; /* header length implies the version */
1581 unsigned int tlen; /* trailer length */
1582 unsigned int elen; /* eth header, may include vlan */
1583 struct fcoe_port *port = lport_priv(lport);
1584 struct fcoe_interface *fcoe = port->priv;
1585 struct fcoe_ctlr *ctlr = fcoe_to_ctlr(fcoe);
1587 struct fcoe_hdr *hp;
1589 WARN_ON((fr_len(fp) % sizeof(u32)) != 0);
1591 fh = fc_frame_header_get(fp);
1593 wlen = skb->len / FCOE_WORD_TO_BYTE;
1595 if (!lport->link_up) {
1600 if (unlikely(fh->fh_type == FC_TYPE_ELS) &&
1601 fcoe_ctlr_els_send(ctlr, lport, skb))
1607 elen = sizeof(struct ethhdr);
1608 hlen = sizeof(struct fcoe_hdr);
1609 tlen = sizeof(struct fcoe_crc_eof);
1610 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
1613 if (likely(lport->crc_offload)) {
1614 skb->ip_summed = CHECKSUM_UNNECESSARY;
1615 skb->csum_start = skb_headroom(skb);
1616 skb->csum_offset = skb->len;
1619 skb->ip_summed = CHECKSUM_NONE;
1620 crc = fcoe_fc_crc(fp);
1623 /* copy port crc and eof to the skb buff */
1624 if (skb_is_nonlinear(skb)) {
1626 if (fcoe_alloc_paged_crc_eof(skb, tlen)) {
1630 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
1631 cp = kmap_atomic(skb_frag_page(frag))
1632 + frag->page_offset;
1634 cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
1637 memset(cp, 0, sizeof(*cp));
1639 cp->fcoe_crc32 = cpu_to_le32(~crc);
1641 if (skb_is_nonlinear(skb)) {
1646 /* adjust skb network/transport offsets to match mac/fcoe/port */
1647 skb_push(skb, elen + hlen);
1648 skb_reset_mac_header(skb);
1649 skb_reset_network_header(skb);
1650 skb->mac_len = elen;
1651 skb->protocol = htons(ETH_P_FCOE);
1652 skb->priority = fcoe->priority;
1654 if (fcoe->netdev->priv_flags & IFF_802_1Q_VLAN &&
1655 fcoe->realdev->features & NETIF_F_HW_VLAN_TX) {
1656 skb->vlan_tci = VLAN_TAG_PRESENT |
1657 vlan_dev_vlan_id(fcoe->netdev);
1658 skb->dev = fcoe->realdev;
1660 skb->dev = fcoe->netdev;
1662 /* fill up mac and fcoe headers */
1664 eh->h_proto = htons(ETH_P_FCOE);
1665 memcpy(eh->h_dest, ctlr->dest_addr, ETH_ALEN);
1667 memcpy(eh->h_dest + 3, fh->fh_d_id, 3);
1669 if (unlikely(ctlr->flogi_oxid != FC_XID_UNKNOWN))
1670 memcpy(eh->h_source, ctlr->ctl_src_addr, ETH_ALEN);
1672 memcpy(eh->h_source, port->data_src_addr, ETH_ALEN);
1674 hp = (struct fcoe_hdr *)(eh + 1);
1675 memset(hp, 0, sizeof(*hp));
1677 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
1680 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
1681 if (lport->seq_offload && fr_max_payload(fp)) {
1682 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
1683 skb_shinfo(skb)->gso_size = fr_max_payload(fp);
1685 skb_shinfo(skb)->gso_type = 0;
1686 skb_shinfo(skb)->gso_size = 0;
1688 /* update tx stats: regardless if LLD fails */
1689 stats = per_cpu_ptr(lport->stats, get_cpu());
1691 stats->TxWords += wlen;
1694 /* send down to lld */
1696 fcoe_port_send(port, skb);
1701 * fcoe_percpu_flush_done() - Indicate per-CPU queue flush completion
1702 * @skb: The completed skb (argument required by destructor)
1704 static void fcoe_percpu_flush_done(struct sk_buff *skb)
1706 complete(&fcoe_flush_completion);
1710 * fcoe_filter_frames() - filter out bad fcoe frames, i.e. bad CRC
1711 * @lport: The local port the frame was received on
1712 * @fp: The received frame
1714 * Return: 0 on passing filtering checks
1716 static inline int fcoe_filter_frames(struct fc_lport *lport,
1717 struct fc_frame *fp)
1719 struct fcoe_ctlr *ctlr;
1720 struct fcoe_interface *fcoe;
1721 struct fc_frame_header *fh;
1722 struct sk_buff *skb = (struct sk_buff *)fp;
1723 struct fc_stats *stats;
1726 * We only check CRC if no offload is available and if it is
1727 * it's solicited data, in which case, the FCP layer would
1728 * check it during the copy.
1730 if (lport->crc_offload && skb->ip_summed == CHECKSUM_UNNECESSARY)
1731 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1733 fr_flags(fp) |= FCPHF_CRC_UNCHECKED;
1735 fh = (struct fc_frame_header *) skb_transport_header(skb);
1736 fh = fc_frame_header_get(fp);
1737 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA && fh->fh_type == FC_TYPE_FCP)
1740 fcoe = ((struct fcoe_port *)lport_priv(lport))->priv;
1741 ctlr = fcoe_to_ctlr(fcoe);
1742 if (is_fip_mode(ctlr) && fc_frame_payload_op(fp) == ELS_LOGO &&
1743 ntoh24(fh->fh_s_id) == FC_FID_FLOGI) {
1744 FCOE_DBG("fcoe: dropping FCoE lport LOGO in fip mode\n");
1748 if (!(fr_flags(fp) & FCPHF_CRC_UNCHECKED) ||
1749 le32_to_cpu(fr_crc(fp)) == ~crc32(~0, skb->data, skb->len)) {
1750 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1754 stats = per_cpu_ptr(lport->stats, get_cpu());
1755 stats->InvalidCRCCount++;
1756 if (stats->InvalidCRCCount < 5)
1757 printk(KERN_WARNING "fcoe: dropping frame with CRC error\n");
1763 * fcoe_recv_frame() - process a single received frame
1764 * @skb: frame to process
1766 static void fcoe_recv_frame(struct sk_buff *skb)
1769 struct fc_lport *lport;
1770 struct fcoe_rcv_info *fr;
1771 struct fc_stats *stats;
1772 struct fcoe_crc_eof crc_eof;
1773 struct fc_frame *fp;
1774 struct fcoe_port *port;
1775 struct fcoe_hdr *hp;
1777 fr = fcoe_dev_from_skb(skb);
1779 if (unlikely(!lport)) {
1780 if (skb->destructor != fcoe_percpu_flush_done)
1781 FCOE_NETDEV_DBG(skb->dev, "NULL lport in skb");
1786 FCOE_NETDEV_DBG(skb->dev, "skb_info: len:%d data_len:%d "
1787 "head:%p data:%p tail:%p end:%p sum:%d dev:%s",
1788 skb->len, skb->data_len,
1789 skb->head, skb->data, skb_tail_pointer(skb),
1790 skb_end_pointer(skb), skb->csum,
1791 skb->dev ? skb->dev->name : "<NULL>");
1793 port = lport_priv(lport);
1794 skb_linearize(skb); /* check for skb_is_nonlinear is within skb_linearize */
1797 * Frame length checks and setting up the header pointers
1798 * was done in fcoe_rcv already.
1800 hp = (struct fcoe_hdr *) skb_network_header(skb);
1802 stats = per_cpu_ptr(lport->stats, get_cpu());
1803 if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) {
1804 if (stats->ErrorFrames < 5)
1805 printk(KERN_WARNING "fcoe: FCoE version "
1806 "mismatch: The frame has "
1807 "version %x, but the "
1808 "initiator supports version "
1809 "%x\n", FC_FCOE_DECAPS_VER(hp),
1814 skb_pull(skb, sizeof(struct fcoe_hdr));
1815 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
1818 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
1820 fp = (struct fc_frame *)skb;
1823 fr_sof(fp) = hp->fcoe_sof;
1825 /* Copy out the CRC and EOF trailer for access */
1826 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof)))
1828 fr_eof(fp) = crc_eof.fcoe_eof;
1829 fr_crc(fp) = crc_eof.fcoe_crc32;
1830 if (pskb_trim(skb, fr_len))
1833 if (!fcoe_filter_frames(lport, fp)) {
1835 fc_exch_recv(lport, fp);
1839 stats->ErrorFrames++;
1845 * fcoe_percpu_receive_thread() - The per-CPU packet receive thread
1846 * @arg: The per-CPU context
1848 * Return: 0 for success
1850 static int fcoe_percpu_receive_thread(void *arg)
1852 struct fcoe_percpu_s *p = arg;
1853 struct sk_buff *skb;
1854 struct sk_buff_head tmp;
1856 skb_queue_head_init(&tmp);
1858 set_user_nice(current, -20);
1861 while (!kthread_should_stop()) {
1863 spin_lock_bh(&p->fcoe_rx_list.lock);
1864 skb_queue_splice_init(&p->fcoe_rx_list, &tmp);
1866 if (!skb_queue_len(&tmp)) {
1867 set_current_state(TASK_INTERRUPTIBLE);
1868 spin_unlock_bh(&p->fcoe_rx_list.lock);
1870 set_current_state(TASK_RUNNING);
1874 spin_unlock_bh(&p->fcoe_rx_list.lock);
1876 while ((skb = __skb_dequeue(&tmp)) != NULL)
1877 fcoe_recv_frame(skb);
1884 * fcoe_dev_setup() - Setup the link change notification interface
1886 static void fcoe_dev_setup(void)
1888 register_dcbevent_notifier(&dcb_notifier);
1889 register_netdevice_notifier(&fcoe_notifier);
1893 * fcoe_dev_cleanup() - Cleanup the link change notification interface
1895 static void fcoe_dev_cleanup(void)
1897 unregister_dcbevent_notifier(&dcb_notifier);
1898 unregister_netdevice_notifier(&fcoe_notifier);
1901 static struct fcoe_interface *
1902 fcoe_hostlist_lookup_realdev_port(struct net_device *netdev)
1904 struct fcoe_interface *fcoe;
1905 struct net_device *real_dev;
1907 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
1908 if (fcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
1909 real_dev = vlan_dev_real_dev(fcoe->netdev);
1911 real_dev = fcoe->netdev;
1913 if (netdev == real_dev)
1919 static int fcoe_dcb_app_notification(struct notifier_block *notifier,
1920 ulong event, void *ptr)
1922 struct dcb_app_type *entry = ptr;
1923 struct fcoe_ctlr *ctlr;
1924 struct fcoe_interface *fcoe;
1925 struct net_device *netdev;
1928 if (entry->app.selector != DCB_APP_IDTYPE_ETHTYPE)
1931 netdev = dev_get_by_index(&init_net, entry->ifindex);
1935 fcoe = fcoe_hostlist_lookup_realdev_port(netdev);
1940 ctlr = fcoe_to_ctlr(fcoe);
1942 if (entry->dcbx & DCB_CAP_DCBX_VER_CEE)
1943 prio = ffs(entry->app.priority) - 1;
1945 prio = entry->app.priority;
1950 if (entry->app.protocol == ETH_P_FIP ||
1951 entry->app.protocol == ETH_P_FCOE)
1952 ctlr->priority = prio;
1954 if (entry->app.protocol == ETH_P_FCOE)
1955 fcoe->priority = prio;
1961 * fcoe_device_notification() - Handler for net device events
1962 * @notifier: The context of the notification
1963 * @event: The type of event
1964 * @ptr: The net device that the event was on
1966 * This function is called by the Ethernet driver in case of link change event.
1968 * Returns: 0 for success
1970 static int fcoe_device_notification(struct notifier_block *notifier,
1971 ulong event, void *ptr)
1973 struct fcoe_ctlr_device *cdev;
1974 struct fc_lport *lport = NULL;
1975 struct net_device *netdev = ptr;
1976 struct fcoe_ctlr *ctlr;
1977 struct fcoe_interface *fcoe;
1978 struct fcoe_port *port;
1979 struct fc_stats *stats;
1980 u32 link_possible = 1;
1984 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
1985 if (fcoe->netdev == netdev) {
1986 ctlr = fcoe_to_ctlr(fcoe);
1998 case NETDEV_GOING_DOWN:
2004 case NETDEV_CHANGEMTU:
2005 if (netdev->features & NETIF_F_FCOE_MTU)
2007 mfs = netdev->mtu - (sizeof(struct fcoe_hdr) +
2008 sizeof(struct fcoe_crc_eof));
2009 if (mfs >= FC_MIN_MAX_FRAME)
2010 fc_set_mfs(lport, mfs);
2012 case NETDEV_REGISTER:
2014 case NETDEV_UNREGISTER:
2015 list_del(&fcoe->list);
2016 port = lport_priv(ctlr->lp);
2017 queue_work(fcoe_wq, &port->destroy_work);
2020 case NETDEV_FEAT_CHANGE:
2021 fcoe_netdev_features_change(lport, netdev);
2024 FCOE_NETDEV_DBG(netdev, "Unknown event %ld "
2025 "from netdev netlink\n", event);
2028 fcoe_link_speed_update(lport);
2030 cdev = fcoe_ctlr_to_ctlr_dev(ctlr);
2032 if (link_possible && !fcoe_link_ok(lport)) {
2033 switch (cdev->enabled) {
2034 case FCOE_CTLR_DISABLED:
2035 pr_info("Link up while interface is disabled.\n");
2037 case FCOE_CTLR_ENABLED:
2038 case FCOE_CTLR_UNUSED:
2039 fcoe_ctlr_link_up(ctlr);
2041 } else if (fcoe_ctlr_link_down(ctlr)) {
2042 switch (cdev->enabled) {
2043 case FCOE_CTLR_DISABLED:
2044 pr_info("Link down while interface is disabled.\n");
2046 case FCOE_CTLR_ENABLED:
2047 case FCOE_CTLR_UNUSED:
2048 stats = per_cpu_ptr(lport->stats, get_cpu());
2049 stats->LinkFailureCount++;
2051 fcoe_clean_pending_queue(lport);
2059 * fcoe_disable() - Disables a FCoE interface
2060 * @netdev : The net_device object the Ethernet interface to create on
2062 * Called from fcoe transport.
2064 * Returns: 0 for success
2066 * Deprecated: use fcoe_ctlr_enabled()
2068 static int fcoe_disable(struct net_device *netdev)
2070 struct fcoe_ctlr *ctlr;
2071 struct fcoe_interface *fcoe;
2074 mutex_lock(&fcoe_config_mutex);
2077 fcoe = fcoe_hostlist_lookup_port(netdev);
2081 ctlr = fcoe_to_ctlr(fcoe);
2082 fcoe_ctlr_link_down(ctlr);
2083 fcoe_clean_pending_queue(ctlr->lp);
2087 mutex_unlock(&fcoe_config_mutex);
2092 * fcoe_enable() - Enables a FCoE interface
2093 * @netdev : The net_device object the Ethernet interface to create on
2095 * Called from fcoe transport.
2097 * Returns: 0 for success
2099 static int fcoe_enable(struct net_device *netdev)
2101 struct fcoe_ctlr *ctlr;
2102 struct fcoe_interface *fcoe;
2105 mutex_lock(&fcoe_config_mutex);
2107 fcoe = fcoe_hostlist_lookup_port(netdev);
2115 ctlr = fcoe_to_ctlr(fcoe);
2117 if (!fcoe_link_ok(ctlr->lp))
2118 fcoe_ctlr_link_up(ctlr);
2121 mutex_unlock(&fcoe_config_mutex);
2126 * fcoe_ctlr_enabled() - Enable or disable an FCoE Controller
2127 * @cdev: The FCoE Controller that is being enabled or disabled
2129 * fcoe_sysfs will ensure that the state of 'enabled' has
2130 * changed, so no checking is necessary here. This routine simply
2131 * calls fcoe_enable or fcoe_disable, both of which are deprecated.
2132 * When those routines are removed the functionality can be merged
2135 static int fcoe_ctlr_enabled(struct fcoe_ctlr_device *cdev)
2137 struct fcoe_ctlr *ctlr = fcoe_ctlr_device_priv(cdev);
2138 struct fc_lport *lport = ctlr->lp;
2139 struct net_device *netdev = fcoe_netdev(lport);
2141 switch (cdev->enabled) {
2142 case FCOE_CTLR_ENABLED:
2143 return fcoe_enable(netdev);
2144 case FCOE_CTLR_DISABLED:
2145 return fcoe_disable(netdev);
2146 case FCOE_CTLR_UNUSED:
2153 * fcoe_destroy() - Destroy a FCoE interface
2154 * @netdev : The net_device object the Ethernet interface to create on
2156 * Called from fcoe transport
2158 * Returns: 0 for success
2160 static int fcoe_destroy(struct net_device *netdev)
2162 struct fcoe_ctlr *ctlr;
2163 struct fcoe_interface *fcoe;
2164 struct fc_lport *lport;
2165 struct fcoe_port *port;
2168 mutex_lock(&fcoe_config_mutex);
2170 fcoe = fcoe_hostlist_lookup_port(netdev);
2175 ctlr = fcoe_to_ctlr(fcoe);
2177 port = lport_priv(lport);
2178 list_del(&fcoe->list);
2179 queue_work(fcoe_wq, &port->destroy_work);
2182 mutex_unlock(&fcoe_config_mutex);
2187 * fcoe_destroy_work() - Destroy a FCoE port in a deferred work context
2188 * @work: Handle to the FCoE port to be destroyed
2190 static void fcoe_destroy_work(struct work_struct *work)
2192 struct fcoe_port *port;
2193 struct fcoe_interface *fcoe;
2195 port = container_of(work, struct fcoe_port, destroy_work);
2196 mutex_lock(&fcoe_config_mutex);
2199 fcoe_if_destroy(port->lport);
2200 fcoe_interface_cleanup(fcoe);
2202 mutex_unlock(&fcoe_config_mutex);
2206 * fcoe_match() - Check if the FCoE is supported on the given netdevice
2207 * @netdev : The net_device object the Ethernet interface to create on
2209 * Called from fcoe transport.
2211 * Returns: always returns true as this is the default FCoE transport,
2212 * i.e., support all netdevs.
2214 static bool fcoe_match(struct net_device *netdev)
2220 * fcoe_dcb_create() - Initialize DCB attributes and hooks
2221 * @netdev: The net_device object of the L2 link that should be queried
2222 * @port: The fcoe_port to bind FCoE APP priority with
2225 static void fcoe_dcb_create(struct fcoe_interface *fcoe)
2230 struct net_device *netdev = fcoe->realdev;
2231 struct fcoe_ctlr *ctlr = fcoe_to_ctlr(fcoe);
2232 struct dcb_app app = {
2234 .protocol = ETH_P_FCOE
2237 /* setup DCB priority attributes. */
2238 if (netdev && netdev->dcbnl_ops && netdev->dcbnl_ops->getdcbx) {
2239 dcbx = netdev->dcbnl_ops->getdcbx(netdev);
2241 if (dcbx & DCB_CAP_DCBX_VER_IEEE) {
2242 app.selector = IEEE_8021QAZ_APP_SEL_ETHERTYPE;
2243 up = dcb_ieee_getapp_mask(netdev, &app);
2244 app.protocol = ETH_P_FIP;
2245 fup = dcb_ieee_getapp_mask(netdev, &app);
2247 app.selector = DCB_APP_IDTYPE_ETHTYPE;
2248 up = dcb_getapp(netdev, &app);
2249 app.protocol = ETH_P_FIP;
2250 fup = dcb_getapp(netdev, &app);
2253 fcoe->priority = ffs(up) ? ffs(up) - 1 : 0;
2254 ctlr->priority = ffs(fup) ? ffs(fup) - 1 : fcoe->priority;
2259 enum fcoe_create_link_state {
2260 FCOE_CREATE_LINK_DOWN,
2261 FCOE_CREATE_LINK_UP,
2265 * _fcoe_create() - (internal) Create a fcoe interface
2266 * @netdev : The net_device object the Ethernet interface to create on
2267 * @fip_mode: The FIP mode for this creation
2268 * @link_state: The ctlr link state on creation
2270 * Called from either the libfcoe 'create' module parameter
2271 * via fcoe_create or from fcoe_syfs's ctlr_create file.
2273 * libfcoe's 'create' module parameter is deprecated so some
2274 * consolidation of code can be done when that interface is
2277 static int _fcoe_create(struct net_device *netdev, enum fip_state fip_mode,
2278 enum fcoe_create_link_state link_state)
2281 struct fcoe_ctlr_device *ctlr_dev;
2282 struct fcoe_ctlr *ctlr;
2283 struct fcoe_interface *fcoe;
2284 struct fc_lport *lport;
2286 mutex_lock(&fcoe_config_mutex);
2289 /* look for existing lport */
2290 if (fcoe_hostlist_lookup(netdev)) {
2295 fcoe = fcoe_interface_create(netdev, fip_mode);
2301 ctlr = fcoe_to_ctlr(fcoe);
2302 ctlr_dev = fcoe_ctlr_to_ctlr_dev(ctlr);
2303 lport = fcoe_if_create(fcoe, &ctlr_dev->dev, 0);
2304 if (IS_ERR(lport)) {
2305 printk(KERN_ERR "fcoe: Failed to create interface (%s)\n",
2309 fcoe_interface_cleanup(fcoe);
2313 /* Make this the "master" N_Port */
2316 /* setup DCB priority attributes. */
2317 fcoe_dcb_create(fcoe);
2319 /* add to lports list */
2320 fcoe_hostlist_add(lport);
2322 /* start FIP Discovery and FLOGI */
2323 lport->boot_time = jiffies;
2324 fc_fabric_login(lport);
2327 * If the fcoe_ctlr_device is to be set to DISABLED
2328 * it must be done after the lport is added to the
2329 * hostlist, but before the rtnl_lock is released.
2330 * This is because the rtnl_lock protects the
2331 * hostlist that fcoe_device_notification uses. If
2332 * the FCoE Controller is intended to be created
2333 * DISABLED then 'enabled' needs to be considered
2334 * handling link events. 'enabled' must be set
2335 * before the lport can be found in the hostlist
2336 * when a link up event is received.
2338 if (link_state == FCOE_CREATE_LINK_UP)
2339 ctlr_dev->enabled = FCOE_CTLR_ENABLED;
2341 ctlr_dev->enabled = FCOE_CTLR_DISABLED;
2343 if (link_state == FCOE_CREATE_LINK_UP &&
2344 !fcoe_link_ok(lport)) {
2346 fcoe_ctlr_link_up(ctlr);
2347 mutex_unlock(&fcoe_config_mutex);
2354 mutex_unlock(&fcoe_config_mutex);
2359 * fcoe_create() - Create a fcoe interface
2360 * @netdev : The net_device object the Ethernet interface to create on
2361 * @fip_mode: The FIP mode for this creation
2363 * Called from fcoe transport
2365 * Returns: 0 for success
2367 static int fcoe_create(struct net_device *netdev, enum fip_state fip_mode)
2369 return _fcoe_create(netdev, fip_mode, FCOE_CREATE_LINK_UP);
2373 * fcoe_ctlr_alloc() - Allocate a fcoe interface from fcoe_sysfs
2374 * @netdev: The net_device to be used by the allocated FCoE Controller
2376 * This routine is called from fcoe_sysfs. It will start the fcoe_ctlr
2377 * in a link_down state. The allows the user an opportunity to configure
2378 * the FCoE Controller from sysfs before enabling the FCoE Controller.
2380 * Creating in with this routine starts the FCoE Controller in Fabric
2381 * mode. The user can change to VN2VN or another mode before enabling.
2383 static int fcoe_ctlr_alloc(struct net_device *netdev)
2385 return _fcoe_create(netdev, FIP_MODE_FABRIC,
2386 FCOE_CREATE_LINK_DOWN);
2390 * fcoe_link_speed_update() - Update the supported and actual link speeds
2391 * @lport: The local port to update speeds for
2393 * Returns: 0 if the ethtool query was successful
2394 * -1 if the ethtool query failed
2396 static int fcoe_link_speed_update(struct fc_lport *lport)
2398 struct net_device *netdev = fcoe_netdev(lport);
2399 struct ethtool_cmd ecmd;
2401 if (!__ethtool_get_settings(netdev, &ecmd)) {
2402 lport->link_supported_speeds &=
2403 ~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
2404 if (ecmd.supported & (SUPPORTED_1000baseT_Half |
2405 SUPPORTED_1000baseT_Full))
2406 lport->link_supported_speeds |= FC_PORTSPEED_1GBIT;
2407 if (ecmd.supported & SUPPORTED_10000baseT_Full)
2408 lport->link_supported_speeds |=
2409 FC_PORTSPEED_10GBIT;
2410 switch (ethtool_cmd_speed(&ecmd)) {
2412 lport->link_speed = FC_PORTSPEED_1GBIT;
2415 lport->link_speed = FC_PORTSPEED_10GBIT;
2424 * fcoe_link_ok() - Check if the link is OK for a local port
2425 * @lport: The local port to check link on
2427 * Returns: 0 if link is UP and OK, -1 if not
2430 static int fcoe_link_ok(struct fc_lport *lport)
2432 struct net_device *netdev = fcoe_netdev(lport);
2434 if (netif_oper_up(netdev))
2440 * fcoe_percpu_clean() - Clear all pending skbs for an local port
2441 * @lport: The local port whose skbs are to be cleared
2443 * Must be called with fcoe_create_mutex held to single-thread completion.
2445 * This flushes the pending skbs by adding a new skb to each queue and
2446 * waiting until they are all freed. This assures us that not only are
2447 * there no packets that will be handled by the lport, but also that any
2448 * threads already handling packet have returned.
2450 static void fcoe_percpu_clean(struct fc_lport *lport)
2452 struct fcoe_percpu_s *pp;
2453 struct sk_buff *skb;
2456 for_each_possible_cpu(cpu) {
2457 pp = &per_cpu(fcoe_percpu, cpu);
2459 if (!pp->thread || !cpu_online(cpu))
2462 skb = dev_alloc_skb(0);
2466 skb->destructor = fcoe_percpu_flush_done;
2468 spin_lock_bh(&pp->fcoe_rx_list.lock);
2469 __skb_queue_tail(&pp->fcoe_rx_list, skb);
2470 if (pp->fcoe_rx_list.qlen == 1)
2471 wake_up_process(pp->thread);
2472 spin_unlock_bh(&pp->fcoe_rx_list.lock);
2474 wait_for_completion(&fcoe_flush_completion);
2479 * fcoe_reset() - Reset a local port
2480 * @shost: The SCSI host associated with the local port to be reset
2482 * Returns: Always 0 (return value required by FC transport template)
2484 static int fcoe_reset(struct Scsi_Host *shost)
2486 struct fc_lport *lport = shost_priv(shost);
2487 struct fcoe_port *port = lport_priv(lport);
2488 struct fcoe_interface *fcoe = port->priv;
2489 struct fcoe_ctlr *ctlr = fcoe_to_ctlr(fcoe);
2490 struct fcoe_ctlr_device *cdev = fcoe_ctlr_to_ctlr_dev(ctlr);
2492 fcoe_ctlr_link_down(ctlr);
2493 fcoe_clean_pending_queue(ctlr->lp);
2495 if (cdev->enabled != FCOE_CTLR_DISABLED &&
2496 !fcoe_link_ok(ctlr->lp))
2497 fcoe_ctlr_link_up(ctlr);
2502 * fcoe_hostlist_lookup_port() - Find the FCoE interface associated with a net device
2503 * @netdev: The net device used as a key
2505 * Locking: Must be called with the RNL mutex held.
2507 * Returns: NULL or the FCoE interface
2509 static struct fcoe_interface *
2510 fcoe_hostlist_lookup_port(const struct net_device *netdev)
2512 struct fcoe_interface *fcoe;
2514 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
2515 if (fcoe->netdev == netdev)
2522 * fcoe_hostlist_lookup() - Find the local port associated with a
2524 * @netdev: The netdevice used as a key
2526 * Locking: Must be called with the RTNL mutex held
2528 * Returns: NULL or the local port
2530 static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *netdev)
2532 struct fcoe_ctlr *ctlr;
2533 struct fcoe_interface *fcoe;
2535 fcoe = fcoe_hostlist_lookup_port(netdev);
2536 ctlr = fcoe_to_ctlr(fcoe);
2537 return (fcoe) ? ctlr->lp : NULL;
2541 * fcoe_hostlist_add() - Add the FCoE interface identified by a local
2542 * port to the hostlist
2543 * @lport: The local port that identifies the FCoE interface to be added
2545 * Locking: must be called with the RTNL mutex held
2547 * Returns: 0 for success
2549 static int fcoe_hostlist_add(const struct fc_lport *lport)
2551 struct fcoe_interface *fcoe;
2552 struct fcoe_port *port;
2554 fcoe = fcoe_hostlist_lookup_port(fcoe_netdev(lport));
2556 port = lport_priv(lport);
2558 list_add_tail(&fcoe->list, &fcoe_hostlist);
2564 static struct fcoe_transport fcoe_sw_transport = {
2565 .name = {FCOE_TRANSPORT_DEFAULT},
2567 .list = LIST_HEAD_INIT(fcoe_sw_transport.list),
2568 .match = fcoe_match,
2569 .alloc = fcoe_ctlr_alloc,
2570 .create = fcoe_create,
2571 .destroy = fcoe_destroy,
2572 .enable = fcoe_enable,
2573 .disable = fcoe_disable,
2577 * fcoe_init() - Initialize fcoe.ko
2579 * Returns: 0 on success, or a negative value on failure
2581 static int __init fcoe_init(void)
2583 struct fcoe_percpu_s *p;
2587 fcoe_wq = alloc_workqueue("fcoe", 0, 0);
2591 /* register as a fcoe transport */
2592 rc = fcoe_transport_attach(&fcoe_sw_transport);
2594 printk(KERN_ERR "failed to register an fcoe transport, check "
2595 "if libfcoe is loaded\n");
2599 mutex_lock(&fcoe_config_mutex);
2601 for_each_possible_cpu(cpu) {
2602 p = &per_cpu(fcoe_percpu, cpu);
2603 skb_queue_head_init(&p->fcoe_rx_list);
2606 for_each_online_cpu(cpu)
2607 fcoe_percpu_thread_create(cpu);
2609 /* Initialize per CPU interrupt thread */
2610 rc = register_hotcpu_notifier(&fcoe_cpu_notifier);
2614 /* Setup link change notification */
2617 rc = fcoe_if_init();
2621 mutex_unlock(&fcoe_config_mutex);
2625 for_each_online_cpu(cpu) {
2626 fcoe_percpu_thread_destroy(cpu);
2628 mutex_unlock(&fcoe_config_mutex);
2629 destroy_workqueue(fcoe_wq);
2632 module_init(fcoe_init);
2635 * fcoe_exit() - Clean up fcoe.ko
2637 * Returns: 0 on success or a negative value on failure
2639 static void __exit fcoe_exit(void)
2641 struct fcoe_interface *fcoe, *tmp;
2642 struct fcoe_ctlr *ctlr;
2643 struct fcoe_port *port;
2646 mutex_lock(&fcoe_config_mutex);
2650 /* releases the associated fcoe hosts */
2652 list_for_each_entry_safe(fcoe, tmp, &fcoe_hostlist, list) {
2653 list_del(&fcoe->list);
2654 ctlr = fcoe_to_ctlr(fcoe);
2655 port = lport_priv(ctlr->lp);
2656 queue_work(fcoe_wq, &port->destroy_work);
2660 unregister_hotcpu_notifier(&fcoe_cpu_notifier);
2662 for_each_online_cpu(cpu)
2663 fcoe_percpu_thread_destroy(cpu);
2665 mutex_unlock(&fcoe_config_mutex);
2668 * destroy_work's may be chained but destroy_workqueue()
2669 * can take care of them. Just kill the fcoe_wq.
2671 destroy_workqueue(fcoe_wq);
2674 * Detaching from the scsi transport must happen after all
2675 * destroys are done on the fcoe_wq. destroy_workqueue will
2676 * enusre the fcoe_wq is flushed.
2680 /* detach from fcoe transport */
2681 fcoe_transport_detach(&fcoe_sw_transport);
2683 module_exit(fcoe_exit);
2686 * fcoe_flogi_resp() - FCoE specific FLOGI and FDISC response handler
2687 * @seq: active sequence in the FLOGI or FDISC exchange
2688 * @fp: response frame, or error encoded in a pointer (timeout)
2689 * @arg: pointer the the fcoe_ctlr structure
2691 * This handles MAC address management for FCoE, then passes control on to
2692 * the libfc FLOGI response handler.
2694 static void fcoe_flogi_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
2696 struct fcoe_ctlr *fip = arg;
2697 struct fc_exch *exch = fc_seq_exch(seq);
2698 struct fc_lport *lport = exch->lp;
2704 mac = fr_cb(fp)->granted_mac;
2706 if (is_zero_ether_addr(mac))
2707 fcoe_ctlr_recv_flogi(fip, lport, fp);
2708 if (!is_zero_ether_addr(mac))
2709 fcoe_update_src_mac(lport, mac);
2711 fc_lport_flogi_resp(seq, fp, lport);
2715 * fcoe_logo_resp() - FCoE specific LOGO response handler
2716 * @seq: active sequence in the LOGO exchange
2717 * @fp: response frame, or error encoded in a pointer (timeout)
2718 * @arg: pointer the the fcoe_ctlr structure
2720 * This handles MAC address management for FCoE, then passes control on to
2721 * the libfc LOGO response handler.
2723 static void fcoe_logo_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
2725 struct fc_lport *lport = arg;
2726 static u8 zero_mac[ETH_ALEN] = { 0 };
2729 fcoe_update_src_mac(lport, zero_mac);
2730 fc_lport_logo_resp(seq, fp, lport);
2734 * fcoe_elsct_send - FCoE specific ELS handler
2736 * This does special case handling of FIP encapsualted ELS exchanges for FCoE,
2737 * using FCoE specific response handlers and passing the FIP controller as
2738 * the argument (the lport is still available from the exchange).
2740 * Most of the work here is just handed off to the libfc routine.
2742 static struct fc_seq *fcoe_elsct_send(struct fc_lport *lport, u32 did,
2743 struct fc_frame *fp, unsigned int op,
2744 void (*resp)(struct fc_seq *,
2747 void *arg, u32 timeout)
2749 struct fcoe_port *port = lport_priv(lport);
2750 struct fcoe_interface *fcoe = port->priv;
2751 struct fcoe_ctlr *fip = fcoe_to_ctlr(fcoe);
2752 struct fc_frame_header *fh = fc_frame_header_get(fp);
2757 if (lport->point_to_multipoint)
2759 return fc_elsct_send(lport, did, fp, op, fcoe_flogi_resp,
2762 /* only hook onto fabric logouts, not port logouts */
2763 if (ntoh24(fh->fh_d_id) != FC_FID_FLOGI)
2765 return fc_elsct_send(lport, did, fp, op, fcoe_logo_resp,
2768 return fc_elsct_send(lport, did, fp, op, resp, arg, timeout);
2772 * fcoe_vport_create() - create an fc_host/scsi_host for a vport
2773 * @vport: fc_vport object to create a new fc_host for
2774 * @disabled: start the new fc_host in a disabled state by default?
2776 * Returns: 0 for success
2778 static int fcoe_vport_create(struct fc_vport *vport, bool disabled)
2780 struct Scsi_Host *shost = vport_to_shost(vport);
2781 struct fc_lport *n_port = shost_priv(shost);
2782 struct fcoe_port *port = lport_priv(n_port);
2783 struct fcoe_interface *fcoe = port->priv;
2784 struct net_device *netdev = fcoe->netdev;
2785 struct fc_lport *vn_port;
2789 rc = fcoe_validate_vport_create(vport);
2791 fcoe_wwn_to_str(vport->port_name, buf, sizeof(buf));
2792 printk(KERN_ERR "fcoe: Failed to create vport, "
2793 "WWPN (0x%s) already exists\n",
2798 mutex_lock(&fcoe_config_mutex);
2800 vn_port = fcoe_if_create(fcoe, &vport->dev, 1);
2802 mutex_unlock(&fcoe_config_mutex);
2804 if (IS_ERR(vn_port)) {
2805 printk(KERN_ERR "fcoe: fcoe_vport_create(%s) failed\n",
2811 fc_vport_set_state(vport, FC_VPORT_DISABLED);
2813 vn_port->boot_time = jiffies;
2814 fc_fabric_login(vn_port);
2815 fc_vport_setlink(vn_port);
2821 * fcoe_vport_destroy() - destroy the fc_host/scsi_host for a vport
2822 * @vport: fc_vport object that is being destroyed
2824 * Returns: 0 for success
2826 static int fcoe_vport_destroy(struct fc_vport *vport)
2828 struct Scsi_Host *shost = vport_to_shost(vport);
2829 struct fc_lport *n_port = shost_priv(shost);
2830 struct fc_lport *vn_port = vport->dd_data;
2832 mutex_lock(&n_port->lp_mutex);
2833 list_del(&vn_port->list);
2834 mutex_unlock(&n_port->lp_mutex);
2836 mutex_lock(&fcoe_config_mutex);
2837 fcoe_if_destroy(vn_port);
2838 mutex_unlock(&fcoe_config_mutex);
2844 * fcoe_vport_disable() - change vport state
2845 * @vport: vport to bring online/offline
2846 * @disable: should the vport be disabled?
2848 static int fcoe_vport_disable(struct fc_vport *vport, bool disable)
2850 struct fc_lport *lport = vport->dd_data;
2853 fc_vport_set_state(vport, FC_VPORT_DISABLED);
2854 fc_fabric_logoff(lport);
2856 lport->boot_time = jiffies;
2857 fc_fabric_login(lport);
2858 fc_vport_setlink(lport);
2865 * fcoe_vport_set_symbolic_name() - append vport string to symbolic name
2866 * @vport: fc_vport with a new symbolic name string
2868 * After generating a new symbolic name string, a new RSPN_ID request is
2869 * sent to the name server. There is no response handler, so if it fails
2870 * for some reason it will not be retried.
2872 static void fcoe_set_vport_symbolic_name(struct fc_vport *vport)
2874 struct fc_lport *lport = vport->dd_data;
2875 struct fc_frame *fp;
2878 snprintf(fc_host_symbolic_name(lport->host), FC_SYMBOLIC_NAME_SIZE,
2879 "%s v%s over %s : %s", FCOE_NAME, FCOE_VERSION,
2880 fcoe_netdev(lport)->name, vport->symbolic_name);
2882 if (lport->state != LPORT_ST_READY)
2885 len = strnlen(fc_host_symbolic_name(lport->host), 255);
2886 fp = fc_frame_alloc(lport,
2887 sizeof(struct fc_ct_hdr) +
2888 sizeof(struct fc_ns_rspn) + len);
2891 lport->tt.elsct_send(lport, FC_FID_DIR_SERV, fp, FC_NS_RSPN_ID,
2892 NULL, NULL, 3 * lport->r_a_tov);
2896 * fcoe_get_lesb() - Fill the FCoE Link Error Status Block
2897 * @lport: the local port
2898 * @fc_lesb: the link error status block
2900 static void fcoe_get_lesb(struct fc_lport *lport,
2901 struct fc_els_lesb *fc_lesb)
2903 struct net_device *netdev = fcoe_netdev(lport);
2905 __fcoe_get_lesb(lport, fc_lesb, netdev);
2908 static void fcoe_ctlr_get_lesb(struct fcoe_ctlr_device *ctlr_dev)
2910 struct fcoe_ctlr *fip = fcoe_ctlr_device_priv(ctlr_dev);
2911 struct net_device *netdev = fcoe_netdev(fip->lp);
2912 struct fcoe_fc_els_lesb *fcoe_lesb;
2913 struct fc_els_lesb fc_lesb;
2915 __fcoe_get_lesb(fip->lp, &fc_lesb, netdev);
2916 fcoe_lesb = (struct fcoe_fc_els_lesb *)(&fc_lesb);
2918 ctlr_dev->lesb.lesb_link_fail =
2919 ntohl(fcoe_lesb->lesb_link_fail);
2920 ctlr_dev->lesb.lesb_vlink_fail =
2921 ntohl(fcoe_lesb->lesb_vlink_fail);
2922 ctlr_dev->lesb.lesb_miss_fka =
2923 ntohl(fcoe_lesb->lesb_miss_fka);
2924 ctlr_dev->lesb.lesb_symb_err =
2925 ntohl(fcoe_lesb->lesb_symb_err);
2926 ctlr_dev->lesb.lesb_err_block =
2927 ntohl(fcoe_lesb->lesb_err_block);
2928 ctlr_dev->lesb.lesb_fcs_error =
2929 ntohl(fcoe_lesb->lesb_fcs_error);
2932 static void fcoe_fcf_get_vlan_id(struct fcoe_fcf_device *fcf_dev)
2934 struct fcoe_ctlr_device *ctlr_dev =
2935 fcoe_fcf_dev_to_ctlr_dev(fcf_dev);
2936 struct fcoe_ctlr *ctlr = fcoe_ctlr_device_priv(ctlr_dev);
2937 struct fcoe_interface *fcoe = fcoe_ctlr_priv(ctlr);
2939 fcf_dev->vlan_id = vlan_dev_vlan_id(fcoe->netdev);
2943 * fcoe_set_port_id() - Callback from libfc when Port_ID is set.
2944 * @lport: the local port
2945 * @port_id: the port ID
2946 * @fp: the received frame, if any, that caused the port_id to be set.
2948 * This routine handles the case where we received a FLOGI and are
2949 * entering point-to-point mode. We need to call fcoe_ctlr_recv_flogi()
2950 * so it can set the non-mapped mode and gateway address.
2952 * The FLOGI LS_ACC is handled by fcoe_flogi_resp().
2954 static void fcoe_set_port_id(struct fc_lport *lport,
2955 u32 port_id, struct fc_frame *fp)
2957 struct fcoe_port *port = lport_priv(lport);
2958 struct fcoe_interface *fcoe = port->priv;
2959 struct fcoe_ctlr *ctlr = fcoe_to_ctlr(fcoe);
2961 if (fp && fc_frame_payload_op(fp) == ELS_FLOGI)
2962 fcoe_ctlr_recv_flogi(ctlr, lport, fp);