1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright(c) 2007 - 2009 Intel Corporation. All rights reserved.
5 * Maintained at www.Open-FCoE.org
8 #include <linux/module.h>
9 #include <linux/spinlock.h>
10 #include <linux/netdevice.h>
11 #include <linux/etherdevice.h>
12 #include <linux/ethtool.h>
13 #include <linux/if_ether.h>
14 #include <linux/if_vlan.h>
15 #include <linux/crc32.h>
16 #include <linux/slab.h>
17 #include <linux/cpu.h>
19 #include <linux/sysfs.h>
20 #include <linux/ctype.h>
21 #include <linux/workqueue.h>
22 #include <net/dcbnl.h>
23 #include <net/dcbevent.h>
24 #include <scsi/scsi_tcq.h>
25 #include <scsi/scsicam.h>
26 #include <scsi/scsi_transport.h>
27 #include <scsi/scsi_transport_fc.h>
28 #include <net/rtnetlink.h>
30 #include <scsi/fc/fc_encaps.h>
31 #include <scsi/fc/fc_fip.h>
32 #include <scsi/fc/fc_fcoe.h>
34 #include <scsi/libfc.h>
35 #include <scsi/fc_frame.h>
36 #include <scsi/libfcoe.h>
40 MODULE_AUTHOR("Open-FCoE.org");
41 MODULE_DESCRIPTION("FCoE");
42 MODULE_LICENSE("GPL v2");
44 /* Performance tuning parameters for fcoe */
45 static unsigned int fcoe_ddp_min = 4096;
46 module_param_named(ddp_min, fcoe_ddp_min, uint, S_IRUGO | S_IWUSR);
47 MODULE_PARM_DESC(ddp_min, "Minimum I/O size in bytes for " \
48 "Direct Data Placement (DDP).");
50 unsigned int fcoe_debug_logging;
51 module_param_named(debug_logging, fcoe_debug_logging, int, S_IRUGO|S_IWUSR);
52 MODULE_PARM_DESC(debug_logging, "a bit mask of logging levels");
54 static unsigned int fcoe_e_d_tov = 2 * 1000;
55 module_param_named(e_d_tov, fcoe_e_d_tov, int, S_IRUGO|S_IWUSR);
56 MODULE_PARM_DESC(e_d_tov, "E_D_TOV in ms, default 2000");
58 static unsigned int fcoe_r_a_tov = 2 * 2 * 1000;
59 module_param_named(r_a_tov, fcoe_r_a_tov, int, S_IRUGO|S_IWUSR);
60 MODULE_PARM_DESC(r_a_tov, "R_A_TOV in ms, default 4000");
62 static DEFINE_MUTEX(fcoe_config_mutex);
64 static struct workqueue_struct *fcoe_wq;
67 /* must only by accessed under the RTNL mutex */
68 static LIST_HEAD(fcoe_hostlist);
69 static DEFINE_PER_CPU(struct fcoe_percpu_s, fcoe_percpu);
71 /* Function Prototypes */
72 static int fcoe_reset(struct Scsi_Host *);
73 static int fcoe_xmit(struct fc_lport *, struct fc_frame *);
74 static int fcoe_rcv(struct sk_buff *, struct net_device *,
75 struct packet_type *, struct net_device *);
76 static void fcoe_percpu_clean(struct fc_lport *);
77 static int fcoe_link_ok(struct fc_lport *);
79 static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *);
80 static int fcoe_hostlist_add(const struct fc_lport *);
81 static void fcoe_hostlist_del(const struct fc_lport *);
83 static int fcoe_device_notification(struct notifier_block *, ulong, void *);
84 static void fcoe_dev_setup(void);
85 static void fcoe_dev_cleanup(void);
86 static struct fcoe_interface
87 *fcoe_hostlist_lookup_port(const struct net_device *);
89 static int fcoe_fip_recv(struct sk_buff *, struct net_device *,
90 struct packet_type *, struct net_device *);
91 static int fcoe_fip_vlan_recv(struct sk_buff *, struct net_device *,
92 struct packet_type *, struct net_device *);
94 static void fcoe_fip_send(struct fcoe_ctlr *, struct sk_buff *);
95 static void fcoe_update_src_mac(struct fc_lport *, u8 *);
96 static u8 *fcoe_get_src_mac(struct fc_lport *);
97 static void fcoe_destroy_work(struct work_struct *);
99 static int fcoe_ddp_setup(struct fc_lport *, u16, struct scatterlist *,
101 static int fcoe_ddp_done(struct fc_lport *, u16);
102 static int fcoe_ddp_target(struct fc_lport *, u16, struct scatterlist *,
104 static int fcoe_dcb_app_notification(struct notifier_block *notifier,
105 ulong event, void *ptr);
107 static bool fcoe_match(struct net_device *netdev);
108 static int fcoe_create(struct net_device *netdev, enum fip_mode fip_mode);
109 static int fcoe_destroy(struct net_device *netdev);
110 static int fcoe_enable(struct net_device *netdev);
111 static int fcoe_disable(struct net_device *netdev);
113 /* fcoe_syfs control interface handlers */
114 static int fcoe_ctlr_alloc(struct net_device *netdev);
115 static int fcoe_ctlr_enabled(struct fcoe_ctlr_device *cdev);
116 static void fcoe_ctlr_mode(struct fcoe_ctlr_device *ctlr_dev);
118 static struct fc_seq *fcoe_elsct_send(struct fc_lport *,
119 u32 did, struct fc_frame *,
121 void (*resp)(struct fc_seq *,
124 void *, u32 timeout);
125 static void fcoe_recv_frame(struct sk_buff *skb);
127 /* notification function for packets from net device */
128 static struct notifier_block fcoe_notifier = {
129 .notifier_call = fcoe_device_notification,
132 /* notification function for DCB events */
133 static struct notifier_block dcb_notifier = {
134 .notifier_call = fcoe_dcb_app_notification,
137 static struct scsi_transport_template *fcoe_nport_scsi_transport;
138 static struct scsi_transport_template *fcoe_vport_scsi_transport;
140 static int fcoe_vport_destroy(struct fc_vport *);
141 static int fcoe_vport_create(struct fc_vport *, bool disabled);
142 static int fcoe_vport_disable(struct fc_vport *, bool disable);
143 static void fcoe_set_vport_symbolic_name(struct fc_vport *);
144 static void fcoe_set_port_id(struct fc_lport *, u32, struct fc_frame *);
145 static void fcoe_fcf_get_vlan_id(struct fcoe_fcf_device *);
146 static void fcoe_vport_remove(struct fc_lport *);
148 static struct fcoe_sysfs_function_template fcoe_sysfs_templ = {
149 .set_fcoe_ctlr_mode = fcoe_ctlr_mode,
150 .set_fcoe_ctlr_enabled = fcoe_ctlr_enabled,
151 .get_fcoe_ctlr_link_fail = fcoe_ctlr_get_lesb,
152 .get_fcoe_ctlr_vlink_fail = fcoe_ctlr_get_lesb,
153 .get_fcoe_ctlr_miss_fka = fcoe_ctlr_get_lesb,
154 .get_fcoe_ctlr_symb_err = fcoe_ctlr_get_lesb,
155 .get_fcoe_ctlr_err_block = fcoe_ctlr_get_lesb,
156 .get_fcoe_ctlr_fcs_error = fcoe_ctlr_get_lesb,
158 .get_fcoe_fcf_selected = fcoe_fcf_get_selected,
159 .get_fcoe_fcf_vlan_id = fcoe_fcf_get_vlan_id,
162 static struct libfc_function_template fcoe_libfc_fcn_templ = {
163 .frame_send = fcoe_xmit,
164 .ddp_setup = fcoe_ddp_setup,
165 .ddp_done = fcoe_ddp_done,
166 .ddp_target = fcoe_ddp_target,
167 .elsct_send = fcoe_elsct_send,
168 .get_lesb = fcoe_get_lesb,
169 .lport_set_port_id = fcoe_set_port_id,
172 static struct fc_function_template fcoe_nport_fc_functions = {
173 .show_host_node_name = 1,
174 .show_host_port_name = 1,
175 .show_host_supported_classes = 1,
176 .show_host_supported_fc4s = 1,
177 .show_host_active_fc4s = 1,
178 .show_host_maxframe_size = 1,
179 .show_host_serial_number = 1,
180 .show_host_manufacturer = 1,
181 .show_host_model = 1,
182 .show_host_model_description = 1,
183 .show_host_hardware_version = 1,
184 .show_host_driver_version = 1,
185 .show_host_firmware_version = 1,
186 .show_host_optionrom_version = 1,
188 .show_host_port_id = 1,
189 .show_host_supported_speeds = 1,
190 .get_host_speed = fc_get_host_speed,
191 .show_host_speed = 1,
192 .show_host_port_type = 1,
193 .get_host_port_state = fc_get_host_port_state,
194 .show_host_port_state = 1,
195 .show_host_symbolic_name = 1,
197 .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
198 .show_rport_maxframe_size = 1,
199 .show_rport_supported_classes = 1,
201 .show_host_fabric_name = 1,
202 .show_starget_node_name = 1,
203 .show_starget_port_name = 1,
204 .show_starget_port_id = 1,
205 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
206 .show_rport_dev_loss_tmo = 1,
207 .get_fc_host_stats = fc_get_host_stats,
208 .issue_fc_host_lip = fcoe_reset,
210 .terminate_rport_io = fc_rport_terminate_io,
212 .vport_create = fcoe_vport_create,
213 .vport_delete = fcoe_vport_destroy,
214 .vport_disable = fcoe_vport_disable,
215 .set_vport_symbolic_name = fcoe_set_vport_symbolic_name,
217 .bsg_request = fc_lport_bsg_request,
220 static struct fc_function_template fcoe_vport_fc_functions = {
221 .show_host_node_name = 1,
222 .show_host_port_name = 1,
223 .show_host_supported_classes = 1,
224 .show_host_supported_fc4s = 1,
225 .show_host_active_fc4s = 1,
226 .show_host_maxframe_size = 1,
227 .show_host_serial_number = 1,
228 .show_host_manufacturer = 1,
229 .show_host_model = 1,
230 .show_host_model_description = 1,
231 .show_host_hardware_version = 1,
232 .show_host_driver_version = 1,
233 .show_host_firmware_version = 1,
234 .show_host_optionrom_version = 1,
236 .show_host_port_id = 1,
237 .show_host_supported_speeds = 1,
238 .get_host_speed = fc_get_host_speed,
239 .show_host_speed = 1,
240 .show_host_port_type = 1,
241 .get_host_port_state = fc_get_host_port_state,
242 .show_host_port_state = 1,
243 .show_host_symbolic_name = 1,
245 .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
246 .show_rport_maxframe_size = 1,
247 .show_rport_supported_classes = 1,
249 .show_host_fabric_name = 1,
250 .show_starget_node_name = 1,
251 .show_starget_port_name = 1,
252 .show_starget_port_id = 1,
253 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
254 .show_rport_dev_loss_tmo = 1,
255 .get_fc_host_stats = fc_get_host_stats,
256 .issue_fc_host_lip = fcoe_reset,
258 .terminate_rport_io = fc_rport_terminate_io,
260 .bsg_request = fc_lport_bsg_request,
263 static struct scsi_host_template fcoe_shost_template = {
264 .module = THIS_MODULE,
265 .name = "FCoE Driver",
266 .proc_name = FCOE_NAME,
267 .queuecommand = fc_queuecommand,
268 .eh_timed_out = fc_eh_timed_out,
269 .eh_abort_handler = fc_eh_abort,
270 .eh_device_reset_handler = fc_eh_device_reset,
271 .eh_host_reset_handler = fc_eh_host_reset,
272 .slave_alloc = fc_slave_alloc,
273 .change_queue_depth = scsi_change_queue_depth,
276 .can_queue = FCOE_MAX_OUTSTANDING_COMMANDS,
277 .sg_tablesize = SG_ALL,
278 .max_sectors = 0xffff,
279 .track_queue_depth = 1,
283 * fcoe_interface_setup() - Setup a FCoE interface
284 * @fcoe: The new FCoE interface
285 * @netdev: The net device that the fcoe interface is on
287 * Returns : 0 for success
288 * Locking: must be called with the RTNL mutex held
290 static int fcoe_interface_setup(struct fcoe_interface *fcoe,
291 struct net_device *netdev)
293 struct fcoe_ctlr *fip = fcoe_to_ctlr(fcoe);
294 struct netdev_hw_addr *ha;
295 struct net_device *real_dev;
296 static const u8 flogi_maddr[ETH_ALEN] = FC_FCOE_FLOGI_MAC;
297 const struct net_device_ops *ops;
299 fcoe->netdev = netdev;
301 /* Let LLD initialize for FCoE */
302 ops = netdev->netdev_ops;
303 if (ops->ndo_fcoe_enable) {
304 if (ops->ndo_fcoe_enable(netdev))
305 FCOE_NETDEV_DBG(netdev, "Failed to enable FCoE"
306 " specific feature for LLD.\n");
309 /* Do not support for bonding device */
310 if (netdev->priv_flags & IFF_BONDING && netdev->flags & IFF_MASTER) {
311 FCOE_NETDEV_DBG(netdev, "Bonded interfaces not supported\n");
315 /* look for SAN MAC address, if multiple SAN MACs exist, only
316 * use the first one for SPMA */
317 real_dev = is_vlan_dev(netdev) ? vlan_dev_real_dev(netdev) : netdev;
318 fcoe->realdev = real_dev;
320 for_each_dev_addr(real_dev, ha) {
321 if ((ha->type == NETDEV_HW_ADDR_T_SAN) &&
322 (is_valid_ether_addr(ha->addr))) {
323 memcpy(fip->ctl_src_addr, ha->addr, ETH_ALEN);
330 /* setup Source Mac Address */
332 memcpy(fip->ctl_src_addr, netdev->dev_addr, netdev->addr_len);
335 * Add FCoE MAC address as second unicast MAC address
336 * or enter promiscuous mode if not capable of listening
337 * for multiple unicast MACs.
339 dev_uc_add(netdev, flogi_maddr);
341 dev_uc_add(netdev, fip->ctl_src_addr);
342 if (fip->mode == FIP_MODE_VN2VN) {
343 dev_mc_add(netdev, FIP_ALL_VN2VN_MACS);
344 dev_mc_add(netdev, FIP_ALL_P2P_MACS);
346 dev_mc_add(netdev, FIP_ALL_ENODE_MACS);
349 * setup the receive function from ethernet driver
350 * on the ethertype for the given device
352 fcoe->fcoe_packet_type.func = fcoe_rcv;
353 fcoe->fcoe_packet_type.type = htons(ETH_P_FCOE);
354 fcoe->fcoe_packet_type.dev = netdev;
355 dev_add_pack(&fcoe->fcoe_packet_type);
357 fcoe->fip_packet_type.func = fcoe_fip_recv;
358 fcoe->fip_packet_type.type = htons(ETH_P_FIP);
359 fcoe->fip_packet_type.dev = netdev;
360 dev_add_pack(&fcoe->fip_packet_type);
362 if (netdev != real_dev) {
363 fcoe->fip_vlan_packet_type.func = fcoe_fip_vlan_recv;
364 fcoe->fip_vlan_packet_type.type = htons(ETH_P_FIP);
365 fcoe->fip_vlan_packet_type.dev = real_dev;
366 dev_add_pack(&fcoe->fip_vlan_packet_type);
372 * fcoe_interface_create() - Create a FCoE interface on a net device
373 * @netdev: The net device to create the FCoE interface on
374 * @fip_mode: The mode to use for FIP
376 * Returns: pointer to a struct fcoe_interface or NULL on error
378 static struct fcoe_interface *fcoe_interface_create(struct net_device *netdev,
379 enum fip_mode fip_mode)
381 struct fcoe_ctlr_device *ctlr_dev;
382 struct fcoe_ctlr *ctlr;
383 struct fcoe_interface *fcoe;
387 if (!try_module_get(THIS_MODULE)) {
388 FCOE_NETDEV_DBG(netdev,
389 "Could not get a reference to the module\n");
390 fcoe = ERR_PTR(-EBUSY);
394 size = sizeof(struct fcoe_ctlr) + sizeof(struct fcoe_interface);
395 ctlr_dev = fcoe_ctlr_device_add(&netdev->dev, &fcoe_sysfs_templ,
398 FCOE_DBG("Failed to add fcoe_ctlr_device\n");
399 fcoe = ERR_PTR(-ENOMEM);
403 ctlr = fcoe_ctlr_device_priv(ctlr_dev);
404 ctlr->cdev = ctlr_dev;
405 fcoe = fcoe_ctlr_priv(ctlr);
412 fcoe_ctlr_init(ctlr, fip_mode);
413 ctlr->send = fcoe_fip_send;
414 ctlr->update_mac = fcoe_update_src_mac;
415 ctlr->get_src_addr = fcoe_get_src_mac;
417 err = fcoe_interface_setup(fcoe, netdev);
419 fcoe_ctlr_destroy(ctlr);
420 fcoe_ctlr_device_delete(ctlr_dev);
429 module_put(THIS_MODULE);
435 * fcoe_interface_remove() - remove FCoE interface from netdev
436 * @fcoe: The FCoE interface to be cleaned up
438 * Caller must be holding the RTNL mutex
440 static void fcoe_interface_remove(struct fcoe_interface *fcoe)
442 struct net_device *netdev = fcoe->netdev;
443 struct fcoe_ctlr *fip = fcoe_to_ctlr(fcoe);
444 static const u8 flogi_maddr[ETH_ALEN] = FC_FCOE_FLOGI_MAC;
445 const struct net_device_ops *ops;
448 * Don't listen for Ethernet packets anymore.
449 * synchronize_net() ensures that the packet handlers are not running
450 * on another CPU. dev_remove_pack() would do that, this calls the
451 * unsyncronized version __dev_remove_pack() to avoid multiple delays.
453 __dev_remove_pack(&fcoe->fcoe_packet_type);
454 __dev_remove_pack(&fcoe->fip_packet_type);
455 if (netdev != fcoe->realdev)
456 __dev_remove_pack(&fcoe->fip_vlan_packet_type);
459 /* Delete secondary MAC addresses */
460 dev_uc_del(netdev, flogi_maddr);
462 dev_uc_del(netdev, fip->ctl_src_addr);
463 if (fip->mode == FIP_MODE_VN2VN) {
464 dev_mc_del(netdev, FIP_ALL_VN2VN_MACS);
465 dev_mc_del(netdev, FIP_ALL_P2P_MACS);
467 dev_mc_del(netdev, FIP_ALL_ENODE_MACS);
469 /* Tell the LLD we are done w/ FCoE */
470 ops = netdev->netdev_ops;
471 if (ops->ndo_fcoe_disable) {
472 if (ops->ndo_fcoe_disable(netdev))
473 FCOE_NETDEV_DBG(netdev, "Failed to disable FCoE"
474 " specific feature for LLD.\n");
481 * fcoe_interface_cleanup() - Clean up a FCoE interface
482 * @fcoe: The FCoE interface to be cleaned up
484 static void fcoe_interface_cleanup(struct fcoe_interface *fcoe)
486 struct net_device *netdev = fcoe->netdev;
487 struct fcoe_ctlr *fip = fcoe_to_ctlr(fcoe);
489 /* Release the self-reference taken during fcoe_interface_create() */
490 /* tear-down the FCoE controller */
491 fcoe_ctlr_destroy(fip);
492 scsi_host_put(fip->lp->host);
494 module_put(THIS_MODULE);
498 * fcoe_fip_recv() - Handler for received FIP frames
499 * @skb: The receive skb
500 * @netdev: The associated net device
501 * @ptype: The packet_type structure which was used to register this handler
502 * @orig_dev: The original net_device the skb was received on.
503 * (in case dev is a bond)
505 * Returns: 0 for success
507 static int fcoe_fip_recv(struct sk_buff *skb, struct net_device *netdev,
508 struct packet_type *ptype,
509 struct net_device *orig_dev)
511 struct fcoe_interface *fcoe;
512 struct fcoe_ctlr *ctlr;
514 fcoe = container_of(ptype, struct fcoe_interface, fip_packet_type);
515 ctlr = fcoe_to_ctlr(fcoe);
516 fcoe_ctlr_recv(ctlr, skb);
521 * fcoe_fip_vlan_recv() - Handler for received FIP VLAN discovery frames
522 * @skb: The receive skb
523 * @netdev: The associated net device
524 * @ptype: The packet_type structure which was used to register this handler
525 * @orig_dev: The original net_device the skb was received on.
526 * (in case dev is a bond)
528 * Returns: 0 for success
530 static int fcoe_fip_vlan_recv(struct sk_buff *skb, struct net_device *netdev,
531 struct packet_type *ptype,
532 struct net_device *orig_dev)
534 struct fcoe_interface *fcoe;
535 struct fcoe_ctlr *ctlr;
537 fcoe = container_of(ptype, struct fcoe_interface, fip_vlan_packet_type);
538 ctlr = fcoe_to_ctlr(fcoe);
539 fcoe_ctlr_recv(ctlr, skb);
544 * fcoe_port_send() - Send an Ethernet-encapsulated FIP/FCoE frame
545 * @port: The FCoE port
546 * @skb: The FIP/FCoE packet to be sent
548 static void fcoe_port_send(struct fcoe_port *port, struct sk_buff *skb)
550 if (port->fcoe_pending_queue.qlen)
551 fcoe_check_wait_queue(port->lport, skb);
552 else if (fcoe_start_io(skb))
553 fcoe_check_wait_queue(port->lport, skb);
557 * fcoe_fip_send() - Send an Ethernet-encapsulated FIP frame
558 * @fip: The FCoE controller
559 * @skb: The FIP packet to be sent
561 static void fcoe_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
563 struct fcoe_interface *fcoe = fcoe_from_ctlr(fip);
566 struct fip_header fip;
570 * Use default VLAN for FIP VLAN discovery protocol
572 frame = (struct fip_frame *)skb->data;
573 if (ntohs(frame->eth.h_proto) == ETH_P_FIP &&
574 ntohs(frame->fip.fip_op) == FIP_OP_VLAN &&
575 fcoe->realdev != fcoe->netdev)
576 skb->dev = fcoe->realdev;
578 skb->dev = fcoe->netdev;
579 fcoe_port_send(lport_priv(fip->lp), skb);
583 * fcoe_update_src_mac() - Update the Ethernet MAC filters
584 * @lport: The local port to update the source MAC on
585 * @addr: Unicast MAC address to add
587 * Remove any previously-set unicast MAC filter.
588 * Add secondary FCoE MAC address filter for our OUI.
590 static void fcoe_update_src_mac(struct fc_lport *lport, u8 *addr)
592 struct fcoe_port *port = lport_priv(lport);
593 struct fcoe_interface *fcoe = port->priv;
595 if (!is_zero_ether_addr(port->data_src_addr))
596 dev_uc_del(fcoe->netdev, port->data_src_addr);
597 if (!is_zero_ether_addr(addr))
598 dev_uc_add(fcoe->netdev, addr);
599 memcpy(port->data_src_addr, addr, ETH_ALEN);
603 * fcoe_get_src_mac() - return the Ethernet source address for an lport
604 * @lport: libfc lport
606 static u8 *fcoe_get_src_mac(struct fc_lport *lport)
608 struct fcoe_port *port = lport_priv(lport);
610 return port->data_src_addr;
614 * fcoe_lport_config() - Set up a local port
615 * @lport: The local port to be setup
617 * Returns: 0 for success
619 static int fcoe_lport_config(struct fc_lport *lport)
623 lport->max_retry_count = 3;
624 lport->max_rport_retry_count = 3;
625 lport->e_d_tov = fcoe_e_d_tov;
626 lport->r_a_tov = fcoe_r_a_tov;
627 lport->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
628 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
629 lport->does_npiv = 1;
631 fc_lport_init_stats(lport);
633 /* lport fc_lport related configuration */
634 fc_lport_config(lport);
636 /* offload related configuration */
637 lport->crc_offload = 0;
638 lport->seq_offload = 0;
639 lport->lro_enabled = 0;
647 * fcoe_netdev_features_change - Updates the lport's offload flags based
648 * on the LLD netdev's FCoE feature flags
650 static void fcoe_netdev_features_change(struct fc_lport *lport,
651 struct net_device *netdev)
653 mutex_lock(&lport->lp_mutex);
655 if (netdev->features & NETIF_F_SG)
660 if (netdev->features & NETIF_F_FCOE_CRC) {
661 lport->crc_offload = 1;
662 FCOE_NETDEV_DBG(netdev, "Supports FCCRC offload\n");
664 lport->crc_offload = 0;
667 if (netdev->features & NETIF_F_FSO) {
668 lport->seq_offload = 1;
669 lport->lso_max = netdev->gso_max_size;
670 FCOE_NETDEV_DBG(netdev, "Supports LSO for max len 0x%x\n",
673 lport->seq_offload = 0;
677 if (netdev->fcoe_ddp_xid) {
678 lport->lro_enabled = 1;
679 lport->lro_xid = netdev->fcoe_ddp_xid;
680 FCOE_NETDEV_DBG(netdev, "Supports LRO for max xid 0x%x\n",
683 lport->lro_enabled = 0;
687 mutex_unlock(&lport->lp_mutex);
691 * fcoe_netdev_config() - Set up net devive for SW FCoE
692 * @lport: The local port that is associated with the net device
693 * @netdev: The associated net device
695 * Must be called after fcoe_lport_config() as it will use local port mutex
697 * Returns: 0 for success
699 static int fcoe_netdev_config(struct fc_lport *lport, struct net_device *netdev)
703 struct fcoe_interface *fcoe;
704 struct fcoe_ctlr *ctlr;
705 struct fcoe_port *port;
707 /* Setup lport private data to point to fcoe softc */
708 port = lport_priv(lport);
710 ctlr = fcoe_to_ctlr(fcoe);
712 /* Figure out the VLAN ID, if any */
713 if (is_vlan_dev(netdev))
714 lport->vlan = vlan_dev_vlan_id(netdev);
719 * Determine max frame size based on underlying device and optional
720 * user-configured limit. If the MFS is too low, fcoe_link_ok()
721 * will return 0, so do this first.
724 if (netdev->features & NETIF_F_FCOE_MTU) {
726 FCOE_NETDEV_DBG(netdev, "Supports FCOE_MTU of %d bytes\n", mfs);
728 mfs -= (sizeof(struct fcoe_hdr) + sizeof(struct fcoe_crc_eof));
729 if (fc_set_mfs(lport, mfs))
732 /* offload features support */
733 fcoe_netdev_features_change(lport, netdev);
735 skb_queue_head_init(&port->fcoe_pending_queue);
736 port->fcoe_pending_queue_active = 0;
737 timer_setup(&port->timer, fcoe_queue_timer, 0);
739 fcoe_link_speed_update(lport);
742 if (fcoe_get_wwn(netdev, &wwnn, NETDEV_FCOE_WWNN))
743 wwnn = fcoe_wwn_from_mac(ctlr->ctl_src_addr, 1, 0);
744 fc_set_wwnn(lport, wwnn);
745 if (fcoe_get_wwn(netdev, &wwpn, NETDEV_FCOE_WWPN))
746 wwpn = fcoe_wwn_from_mac(ctlr->ctl_src_addr,
748 fc_set_wwpn(lport, wwpn);
755 * fcoe_shost_config() - Set up the SCSI host associated with a local port
756 * @lport: The local port
757 * @dev: The device associated with the SCSI host
759 * Must be called after fcoe_lport_config() and fcoe_netdev_config()
761 * Returns: 0 for success
763 static int fcoe_shost_config(struct fc_lport *lport, struct device *dev)
767 /* lport scsi host config */
768 lport->host->max_lun = FCOE_MAX_LUN;
769 lport->host->max_id = FCOE_MAX_FCP_TARGET;
770 lport->host->max_channel = 0;
771 lport->host->max_cmd_len = FCOE_MAX_CMD_LEN;
774 lport->host->transportt = fcoe_vport_scsi_transport;
776 lport->host->transportt = fcoe_nport_scsi_transport;
778 /* add the new host to the SCSI-ml */
779 rc = scsi_add_host(lport->host, dev);
781 FCOE_NETDEV_DBG(fcoe_netdev(lport), "fcoe_shost_config: "
782 "error on scsi_add_host\n");
787 fc_host_max_npiv_vports(lport->host) = USHRT_MAX;
789 snprintf(fc_host_symbolic_name(lport->host), FC_SYMBOLIC_NAME_SIZE,
790 "%s v%s over %s", FCOE_NAME, FCOE_VERSION,
791 fcoe_netdev(lport)->name);
798 * fcoe_fdmi_info() - Get FDMI related info from net devive for SW FCoE
799 * @lport: The local port that is associated with the net device
800 * @netdev: The associated net device
802 * Must be called after fcoe_shost_config() as it will use local port mutex
805 static void fcoe_fdmi_info(struct fc_lport *lport, struct net_device *netdev)
807 struct fcoe_interface *fcoe;
808 struct fcoe_port *port;
809 struct net_device *realdev;
812 port = lport_priv(lport);
814 realdev = fcoe->realdev;
816 /* No FDMI state m/c for NPIV ports */
820 if (realdev->netdev_ops->ndo_fcoe_get_hbainfo) {
821 struct netdev_fcoe_hbainfo *fdmi;
822 fdmi = kzalloc(sizeof(*fdmi), GFP_KERNEL);
826 rc = realdev->netdev_ops->ndo_fcoe_get_hbainfo(realdev,
829 printk(KERN_INFO "fcoe: Failed to retrieve FDMI "
830 "information from netdev.\n");
834 snprintf(fc_host_serial_number(lport->host),
835 FC_SERIAL_NUMBER_SIZE,
837 fdmi->serial_number);
838 snprintf(fc_host_manufacturer(lport->host),
839 FC_SERIAL_NUMBER_SIZE,
842 snprintf(fc_host_model(lport->host),
843 FC_SYMBOLIC_NAME_SIZE,
846 snprintf(fc_host_model_description(lport->host),
847 FC_SYMBOLIC_NAME_SIZE,
849 fdmi->model_description);
850 snprintf(fc_host_hardware_version(lport->host),
851 FC_VERSION_STRING_SIZE,
853 fdmi->hardware_version);
854 snprintf(fc_host_driver_version(lport->host),
855 FC_VERSION_STRING_SIZE,
857 fdmi->driver_version);
858 snprintf(fc_host_optionrom_version(lport->host),
859 FC_VERSION_STRING_SIZE,
861 fdmi->optionrom_version);
862 snprintf(fc_host_firmware_version(lport->host),
863 FC_VERSION_STRING_SIZE,
865 fdmi->firmware_version);
867 /* Enable FDMI lport states */
868 lport->fdmi_enabled = 1;
871 lport->fdmi_enabled = 0;
872 printk(KERN_INFO "fcoe: No FDMI support.\n");
877 * fcoe_oem_match() - The match routine for the offloaded exchange manager
880 * This routine will be associated with an exchange manager (EM). When
881 * the libfc exchange handling code is looking for an EM to use it will
882 * call this routine and pass it the frame that it wishes to send. This
883 * routine will return True if the associated EM is to be used and False
884 * if the echange code should continue looking for an EM.
886 * The offload EM that this routine is associated with will handle any
887 * packets that are for SCSI read requests.
889 * This has been enhanced to work when FCoE stack is operating in target
892 * Returns: True for read types I/O, otherwise returns false.
894 static bool fcoe_oem_match(struct fc_frame *fp)
896 struct fc_frame_header *fh = fc_frame_header_get(fp);
897 struct fcp_cmnd *fcp;
899 if (fc_fcp_is_read(fr_fsp(fp)) &&
900 (fr_fsp(fp)->data_len > fcoe_ddp_min))
902 else if ((fr_fsp(fp) == NULL) &&
903 (fh->fh_r_ctl == FC_RCTL_DD_UNSOL_CMD) &&
904 (ntohs(fh->fh_rx_id) == FC_XID_UNKNOWN)) {
905 fcp = fc_frame_payload_get(fp, sizeof(*fcp));
906 if ((fcp->fc_flags & FCP_CFL_WRDATA) &&
907 (ntohl(fcp->fc_dl) > fcoe_ddp_min))
914 * fcoe_em_config() - Allocate and configure an exchange manager
915 * @lport: The local port that the new EM will be associated with
917 * Returns: 0 on success
919 static inline int fcoe_em_config(struct fc_lport *lport)
921 struct fcoe_port *port = lport_priv(lport);
922 struct fcoe_interface *fcoe = port->priv;
923 struct fcoe_interface *oldfcoe = NULL;
924 struct net_device *old_real_dev, *cur_real_dev;
925 u16 min_xid = FCOE_MIN_XID;
926 u16 max_xid = FCOE_MAX_XID;
929 * Check if need to allocate an em instance for
930 * offload exchange ids to be shared across all VN_PORTs/lport.
932 if (!lport->lro_enabled || !lport->lro_xid ||
933 (lport->lro_xid >= max_xid)) {
939 * Reuse existing offload em instance in case
940 * it is already allocated on real eth device
942 if (is_vlan_dev(fcoe->netdev))
943 cur_real_dev = vlan_dev_real_dev(fcoe->netdev);
945 cur_real_dev = fcoe->netdev;
947 list_for_each_entry(oldfcoe, &fcoe_hostlist, list) {
948 if (is_vlan_dev(oldfcoe->netdev))
949 old_real_dev = vlan_dev_real_dev(oldfcoe->netdev);
951 old_real_dev = oldfcoe->netdev;
953 if (cur_real_dev == old_real_dev) {
954 fcoe->oem = oldfcoe->oem;
960 if (!fc_exch_mgr_add(lport, fcoe->oem, fcoe_oem_match)) {
961 printk(KERN_ERR "fcoe_em_config: failed to add "
962 "offload em:%p on interface:%s\n",
963 fcoe->oem, fcoe->netdev->name);
967 fcoe->oem = fc_exch_mgr_alloc(lport, FC_CLASS_3,
968 FCOE_MIN_XID, lport->lro_xid,
971 printk(KERN_ERR "fcoe_em_config: failed to allocate "
972 "em for offload exches on interface:%s\n",
979 * Exclude offload EM xid range from next EM xid range.
981 min_xid += lport->lro_xid + 1;
984 if (!fc_exch_mgr_alloc(lport, FC_CLASS_3, min_xid, max_xid, NULL)) {
985 printk(KERN_ERR "fcoe_em_config: failed to "
986 "allocate em on interface %s\n", fcoe->netdev->name);
994 * fcoe_if_destroy() - Tear down a SW FCoE instance
995 * @lport: The local port to be destroyed
997 * Locking: Must be called with the RTNL mutex held.
1000 static void fcoe_if_destroy(struct fc_lport *lport)
1002 struct fcoe_port *port = lport_priv(lport);
1003 struct fcoe_interface *fcoe = port->priv;
1004 struct net_device *netdev = fcoe->netdev;
1006 FCOE_NETDEV_DBG(netdev, "Destroying interface\n");
1008 /* Logout of the fabric */
1009 fc_fabric_logoff(lport);
1011 /* Cleanup the fc_lport */
1012 fc_lport_destroy(lport);
1014 /* Stop the transmit retry timer */
1015 del_timer_sync(&port->timer);
1017 /* Free existing transmit skbs */
1018 fcoe_clean_pending_queue(lport);
1020 if (!is_zero_ether_addr(port->data_src_addr))
1021 dev_uc_del(netdev, port->data_src_addr);
1025 fcoe_interface_remove(fcoe);
1027 /* Free queued packets for the per-CPU receive threads */
1028 fcoe_percpu_clean(lport);
1030 /* Detach from the scsi-ml */
1031 fc_remove_host(lport->host);
1032 scsi_remove_host(lport->host);
1034 /* Destroy lport scsi_priv */
1035 fc_fcp_destroy(lport);
1037 /* There are no more rports or I/O, free the EM */
1038 fc_exch_mgr_free(lport);
1040 /* Free memory used by statistical counters */
1041 fc_lport_free_stats(lport);
1044 * Release the Scsi_Host for vport but hold on to
1045 * master lport until it fcoe interface fully cleaned-up.
1048 scsi_host_put(lport->host);
1052 * fcoe_ddp_setup() - Call a LLD's ddp_setup through the net device
1053 * @lport: The local port to setup DDP for
1054 * @xid: The exchange ID for this DDP transfer
1055 * @sgl: The scatterlist describing this transfer
1056 * @sgc: The number of sg items
1058 * Returns: 0 if the DDP context was not configured
1060 static int fcoe_ddp_setup(struct fc_lport *lport, u16 xid,
1061 struct scatterlist *sgl, unsigned int sgc)
1063 struct net_device *netdev = fcoe_netdev(lport);
1065 if (netdev->netdev_ops->ndo_fcoe_ddp_setup)
1066 return netdev->netdev_ops->ndo_fcoe_ddp_setup(netdev,
1074 * fcoe_ddp_target() - Call a LLD's ddp_target through the net device
1075 * @lport: The local port to setup DDP for
1076 * @xid: The exchange ID for this DDP transfer
1077 * @sgl: The scatterlist describing this transfer
1078 * @sgc: The number of sg items
1080 * Returns: 0 if the DDP context was not configured
1082 static int fcoe_ddp_target(struct fc_lport *lport, u16 xid,
1083 struct scatterlist *sgl, unsigned int sgc)
1085 struct net_device *netdev = fcoe_netdev(lport);
1087 if (netdev->netdev_ops->ndo_fcoe_ddp_target)
1088 return netdev->netdev_ops->ndo_fcoe_ddp_target(netdev, xid,
1096 * fcoe_ddp_done() - Call a LLD's ddp_done through the net device
1097 * @lport: The local port to complete DDP on
1098 * @xid: The exchange ID for this DDP transfer
1100 * Returns: the length of data that have been completed by DDP
1102 static int fcoe_ddp_done(struct fc_lport *lport, u16 xid)
1104 struct net_device *netdev = fcoe_netdev(lport);
1106 if (netdev->netdev_ops->ndo_fcoe_ddp_done)
1107 return netdev->netdev_ops->ndo_fcoe_ddp_done(netdev, xid);
1112 * fcoe_if_create() - Create a FCoE instance on an interface
1113 * @fcoe: The FCoE interface to create a local port on
1114 * @parent: The device pointer to be the parent in sysfs for the SCSI host
1115 * @npiv: Indicates if the port is a vport or not
1117 * Creates a fc_lport instance and a Scsi_Host instance and configure them.
1119 * Returns: The allocated fc_lport or an error pointer
1121 static struct fc_lport *fcoe_if_create(struct fcoe_interface *fcoe,
1122 struct device *parent, int npiv)
1124 struct fcoe_ctlr *ctlr = fcoe_to_ctlr(fcoe);
1125 struct net_device *netdev = fcoe->netdev;
1126 struct fc_lport *lport, *n_port;
1127 struct fcoe_port *port;
1128 struct Scsi_Host *shost;
1131 * parent is only a vport if npiv is 1,
1132 * but we'll only use vport in that case so go ahead and set it
1134 struct fc_vport *vport = dev_to_vport(parent);
1136 FCOE_NETDEV_DBG(netdev, "Create Interface\n");
1139 lport = libfc_host_alloc(&fcoe_shost_template, sizeof(*port));
1141 lport = libfc_vport_create(vport, sizeof(*port));
1144 FCOE_NETDEV_DBG(netdev, "Could not allocate host structure\n");
1148 port = lport_priv(lport);
1149 port->lport = lport;
1151 port->get_netdev = fcoe_netdev;
1152 port->max_queue_depth = FCOE_MAX_QUEUE_DEPTH;
1153 port->min_queue_depth = FCOE_MIN_QUEUE_DEPTH;
1154 INIT_WORK(&port->destroy_work, fcoe_destroy_work);
1157 * Need to add the lport to the hostlist
1158 * so we catch NETDEV_CHANGE events.
1160 fcoe_hostlist_add(lport);
1162 /* configure a fc_lport including the exchange manager */
1163 rc = fcoe_lport_config(lport);
1165 FCOE_NETDEV_DBG(netdev, "Could not configure lport for the "
1171 FCOE_NETDEV_DBG(netdev, "Setting vport names, "
1172 "%16.16llx %16.16llx\n",
1173 vport->node_name, vport->port_name);
1174 fc_set_wwnn(lport, vport->node_name);
1175 fc_set_wwpn(lport, vport->port_name);
1178 /* configure lport network properties */
1179 rc = fcoe_netdev_config(lport, netdev);
1181 FCOE_NETDEV_DBG(netdev, "Could not configure netdev for the "
1183 goto out_lp_destroy;
1186 /* configure lport scsi host properties */
1187 rc = fcoe_shost_config(lport, parent);
1189 FCOE_NETDEV_DBG(netdev, "Could not configure shost for the "
1191 goto out_lp_destroy;
1194 /* Initialize the library */
1195 rc = fcoe_libfc_config(lport, ctlr, &fcoe_libfc_fcn_templ, 1);
1197 FCOE_NETDEV_DBG(netdev, "Could not configure libfc for the "
1199 goto out_lp_destroy;
1202 /* Initialized FDMI information */
1203 fcoe_fdmi_info(lport, netdev);
1206 * fcoe_em_alloc() and fcoe_hostlist_add() both
1207 * need to be atomic with respect to other changes to the
1208 * hostlist since fcoe_em_alloc() looks for an existing EM
1209 * instance on host list updated by fcoe_hostlist_add().
1211 * This is currently handled through the fcoe_config_mutex
1215 /* lport exch manager allocation */
1216 rc = fcoe_em_config(lport);
1218 shost = vport_to_shost(vport);
1219 n_port = shost_priv(shost);
1220 rc = fc_exch_mgr_list_clone(n_port, lport);
1224 FCOE_NETDEV_DBG(netdev, "Could not configure the EM\n");
1225 goto out_lp_destroy;
1231 fc_exch_mgr_free(lport);
1233 fcoe_hostlist_del(lport);
1234 scsi_host_put(lport->host);
1240 * fcoe_if_init() - Initialization routine for fcoe.ko
1242 * Attaches the SW FCoE transport to the FC transport
1244 * Returns: 0 on success
1246 static int __init fcoe_if_init(void)
1248 /* attach to scsi transport */
1249 fcoe_nport_scsi_transport =
1250 fc_attach_transport(&fcoe_nport_fc_functions);
1251 if (!fcoe_nport_scsi_transport)
1254 fcoe_vport_scsi_transport =
1255 fc_attach_transport(&fcoe_vport_fc_functions);
1256 if (!fcoe_vport_scsi_transport)
1262 fc_release_transport(fcoe_nport_scsi_transport);
1264 printk(KERN_ERR "fcoe: Failed to attach to the FC transport\n");
1269 * fcoe_if_exit() - Tear down fcoe.ko
1271 * Detaches the SW FCoE transport from the FC transport
1273 * Returns: 0 on success
1275 static int __exit fcoe_if_exit(void)
1277 fc_release_transport(fcoe_nport_scsi_transport);
1278 fc_release_transport(fcoe_vport_scsi_transport);
1279 fcoe_nport_scsi_transport = NULL;
1280 fcoe_vport_scsi_transport = NULL;
1284 static void fcoe_thread_cleanup_local(unsigned int cpu)
1286 struct page *crc_eof;
1287 struct fcoe_percpu_s *p;
1289 p = per_cpu_ptr(&fcoe_percpu, cpu);
1290 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 flush_work(&p->work);
1302 * fcoe_select_cpu() - Selects CPU to handle post-processing of incoming
1305 * This routine selects next CPU based on cpumask to distribute
1306 * incoming requests in round robin.
1308 * Returns: int CPU number
1310 static inline unsigned int fcoe_select_cpu(void)
1312 static unsigned int selected_cpu;
1314 selected_cpu = cpumask_next(selected_cpu, cpu_online_mask);
1315 if (selected_cpu >= nr_cpu_ids)
1316 selected_cpu = cpumask_first(cpu_online_mask);
1318 return selected_cpu;
1322 * fcoe_rcv() - Receive packets from a net device
1323 * @skb: The received packet
1324 * @netdev: The net device that the packet was received on
1325 * @ptype: The packet type context
1326 * @olddev: The last device net device
1328 * This routine is called by NET_RX_SOFTIRQ. It receives a packet, builds a
1329 * FC frame and passes the frame to libfc.
1331 * Returns: 0 for success
1333 static int fcoe_rcv(struct sk_buff *skb, struct net_device *netdev,
1334 struct packet_type *ptype, struct net_device *olddev)
1336 struct fc_lport *lport;
1337 struct fcoe_rcv_info *fr;
1338 struct fcoe_ctlr *ctlr;
1339 struct fcoe_interface *fcoe;
1340 struct fc_frame_header *fh;
1341 struct fcoe_percpu_s *fps;
1345 fcoe = container_of(ptype, struct fcoe_interface, fcoe_packet_type);
1346 ctlr = fcoe_to_ctlr(fcoe);
1348 if (unlikely(!lport)) {
1349 FCOE_NETDEV_DBG(netdev, "Cannot find hba structure\n");
1352 if (!lport->link_up)
1355 FCOE_NETDEV_DBG(netdev,
1356 "skb_info: len:%d data_len:%d head:%p data:%p tail:%p end:%p sum:%d dev:%s\n",
1357 skb->len, skb->data_len, skb->head, skb->data,
1358 skb_tail_pointer(skb), skb_end_pointer(skb),
1359 skb->csum, skb->dev ? skb->dev->name : "<NULL>");
1362 skb = skb_share_check(skb, GFP_ATOMIC);
1369 if (is_fip_mode(ctlr) &&
1370 !ether_addr_equal(eh->h_source, ctlr->dest_addr)) {
1371 FCOE_NETDEV_DBG(netdev, "wrong source mac address:%pM\n",
1377 * Check for minimum frame length, and make sure required FCoE
1378 * and FC headers are pulled into the linear data area.
1380 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
1381 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
1384 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
1385 fh = (struct fc_frame_header *) skb_transport_header(skb);
1387 if (ntoh24(&eh->h_dest[3]) != ntoh24(fh->fh_d_id)) {
1388 FCOE_NETDEV_DBG(netdev, "FC frame d_id mismatch with MAC:%pM\n",
1393 fr = fcoe_dev_from_skb(skb);
1397 * In case the incoming frame's exchange is originated from
1398 * the initiator, then received frame's exchange id is ANDed
1399 * with fc_cpu_mask bits to get the same cpu on which exchange
1400 * was originated, otherwise select cpu using rx exchange id
1401 * or fcoe_select_cpu().
1403 if (ntoh24(fh->fh_f_ctl) & FC_FC_EX_CTX)
1404 cpu = ntohs(fh->fh_ox_id) & fc_cpu_mask;
1406 if (ntohs(fh->fh_rx_id) == FC_XID_UNKNOWN)
1407 cpu = fcoe_select_cpu();
1409 cpu = ntohs(fh->fh_rx_id) & fc_cpu_mask;
1412 if (cpu >= nr_cpu_ids)
1415 fps = &per_cpu(fcoe_percpu, cpu);
1416 spin_lock(&fps->fcoe_rx_list.lock);
1418 * We now have a valid CPU that we're targeting for
1419 * this skb. We also have this receive thread locked,
1420 * so we're free to queue skbs into it's queue.
1424 * Note: We used to have a set of conditions under which we would
1425 * call fcoe_recv_frame directly, rather than queuing to the rx list
1426 * as it could save a few cycles, but doing so is prohibited, as
1427 * fcoe_recv_frame has several paths that may sleep, which is forbidden
1428 * in softirq context.
1430 __skb_queue_tail(&fps->fcoe_rx_list, skb);
1431 schedule_work_on(cpu, &fps->work);
1432 spin_unlock(&fps->fcoe_rx_list.lock);
1434 return NET_RX_SUCCESS;
1436 per_cpu_ptr(lport->stats, get_cpu())->ErrorFrames++;
1444 * fcoe_alloc_paged_crc_eof() - Allocate a page to be used for the trailer CRC
1445 * @skb: The packet to be transmitted
1446 * @tlen: The total length of the trailer
1448 * Returns: 0 for success
1450 static int fcoe_alloc_paged_crc_eof(struct sk_buff *skb, int tlen)
1452 struct fcoe_percpu_s *fps;
1455 fps = &get_cpu_var(fcoe_percpu);
1456 rc = fcoe_get_paged_crc_eof(skb, tlen, fps);
1457 put_cpu_var(fcoe_percpu);
1463 * fcoe_xmit() - Transmit a FCoE frame
1464 * @lport: The local port that the frame is to be transmitted for
1465 * @fp: The frame to be transmitted
1467 * Return: 0 for success
1469 static int fcoe_xmit(struct fc_lport *lport, struct fc_frame *fp)
1474 struct fcoe_crc_eof *cp;
1475 struct sk_buff *skb;
1476 struct fc_stats *stats;
1477 struct fc_frame_header *fh;
1478 unsigned int hlen; /* header length implies the version */
1479 unsigned int tlen; /* trailer length */
1480 unsigned int elen; /* eth header, may include vlan */
1481 struct fcoe_port *port = lport_priv(lport);
1482 struct fcoe_interface *fcoe = port->priv;
1483 struct fcoe_ctlr *ctlr = fcoe_to_ctlr(fcoe);
1485 struct fcoe_hdr *hp;
1487 WARN_ON((fr_len(fp) % sizeof(u32)) != 0);
1489 fh = fc_frame_header_get(fp);
1491 wlen = skb->len / FCOE_WORD_TO_BYTE;
1493 if (!lport->link_up) {
1498 if (unlikely(fh->fh_type == FC_TYPE_ELS) &&
1499 fcoe_ctlr_els_send(ctlr, lport, skb))
1505 elen = sizeof(struct ethhdr);
1506 hlen = sizeof(struct fcoe_hdr);
1507 tlen = sizeof(struct fcoe_crc_eof);
1508 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
1511 if (likely(lport->crc_offload)) {
1512 skb->ip_summed = CHECKSUM_PARTIAL;
1513 skb->csum_start = skb_headroom(skb);
1514 skb->csum_offset = skb->len;
1517 skb->ip_summed = CHECKSUM_NONE;
1518 crc = fcoe_fc_crc(fp);
1521 /* copy port crc and eof to the skb buff */
1522 if (skb_is_nonlinear(skb)) {
1524 if (fcoe_alloc_paged_crc_eof(skb, tlen)) {
1528 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
1529 cp = kmap_atomic(skb_frag_page(frag)) + skb_frag_off(frag);
1531 cp = skb_put(skb, tlen);
1534 memset(cp, 0, sizeof(*cp));
1536 cp->fcoe_crc32 = cpu_to_le32(~crc);
1538 if (skb_is_nonlinear(skb)) {
1543 /* adjust skb network/transport offsets to match mac/fcoe/port */
1544 skb_push(skb, elen + hlen);
1545 skb_reset_mac_header(skb);
1546 skb_reset_network_header(skb);
1547 skb->mac_len = elen;
1548 skb->protocol = htons(ETH_P_FCOE);
1549 skb->priority = fcoe->priority;
1551 if (is_vlan_dev(fcoe->netdev) &&
1552 fcoe->realdev->features & NETIF_F_HW_VLAN_CTAG_TX) {
1553 /* must set skb->dev before calling vlan_put_tag */
1554 skb->dev = fcoe->realdev;
1555 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
1556 vlan_dev_vlan_id(fcoe->netdev));
1558 skb->dev = fcoe->netdev;
1560 /* fill up mac and fcoe headers */
1562 eh->h_proto = htons(ETH_P_FCOE);
1563 memcpy(eh->h_dest, ctlr->dest_addr, ETH_ALEN);
1565 memcpy(eh->h_dest + 3, fh->fh_d_id, 3);
1567 if (unlikely(ctlr->flogi_oxid != FC_XID_UNKNOWN))
1568 memcpy(eh->h_source, ctlr->ctl_src_addr, ETH_ALEN);
1570 memcpy(eh->h_source, port->data_src_addr, ETH_ALEN);
1572 hp = (struct fcoe_hdr *)(eh + 1);
1573 memset(hp, 0, sizeof(*hp));
1575 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
1578 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
1579 if (lport->seq_offload && fr_max_payload(fp)) {
1580 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
1581 skb_shinfo(skb)->gso_size = fr_max_payload(fp);
1583 skb_shinfo(skb)->gso_type = 0;
1584 skb_shinfo(skb)->gso_size = 0;
1586 /* update tx stats: regardless if LLD fails */
1587 stats = per_cpu_ptr(lport->stats, get_cpu());
1589 stats->TxWords += wlen;
1592 /* send down to lld */
1594 fcoe_port_send(port, skb);
1599 * fcoe_filter_frames() - filter out bad fcoe frames, i.e. bad CRC
1600 * @lport: The local port the frame was received on
1601 * @fp: The received frame
1603 * Return: 0 on passing filtering checks
1605 static inline int fcoe_filter_frames(struct fc_lport *lport,
1606 struct fc_frame *fp)
1608 struct fcoe_ctlr *ctlr;
1609 struct fcoe_interface *fcoe;
1610 struct fc_frame_header *fh;
1611 struct sk_buff *skb = (struct sk_buff *)fp;
1612 struct fc_stats *stats;
1615 * We only check CRC if no offload is available and if it is
1616 * it's solicited data, in which case, the FCP layer would
1617 * check it during the copy.
1619 if (lport->crc_offload && skb->ip_summed == CHECKSUM_UNNECESSARY)
1620 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1622 fr_flags(fp) |= FCPHF_CRC_UNCHECKED;
1624 fh = fc_frame_header_get(fp);
1625 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA && fh->fh_type == FC_TYPE_FCP)
1628 fcoe = ((struct fcoe_port *)lport_priv(lport))->priv;
1629 ctlr = fcoe_to_ctlr(fcoe);
1630 if (is_fip_mode(ctlr) && fc_frame_payload_op(fp) == ELS_LOGO &&
1631 ntoh24(fh->fh_s_id) == FC_FID_FLOGI) {
1632 FCOE_DBG("fcoe: dropping FCoE lport LOGO in fip mode\n");
1636 if (!(fr_flags(fp) & FCPHF_CRC_UNCHECKED) ||
1637 le32_to_cpu(fr_crc(fp)) == ~crc32(~0, skb->data, skb->len)) {
1638 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1642 stats = per_cpu_ptr(lport->stats, get_cpu());
1643 stats->InvalidCRCCount++;
1644 if (stats->InvalidCRCCount < 5)
1645 printk(KERN_WARNING "fcoe: dropping frame with CRC error\n");
1651 * fcoe_recv_frame() - process a single received frame
1652 * @skb: frame to process
1654 static void fcoe_recv_frame(struct sk_buff *skb)
1657 struct fc_lport *lport;
1658 struct fcoe_rcv_info *fr;
1659 struct fc_stats *stats;
1660 struct fcoe_crc_eof crc_eof;
1661 struct fc_frame *fp;
1662 struct fcoe_hdr *hp;
1664 fr = fcoe_dev_from_skb(skb);
1666 if (unlikely(!lport)) {
1667 FCOE_NETDEV_DBG(skb->dev, "NULL lport in skb\n");
1672 FCOE_NETDEV_DBG(skb->dev,
1673 "skb_info: len:%d data_len:%d head:%p data:%p tail:%p end:%p sum:%d dev:%s\n",
1674 skb->len, skb->data_len,
1675 skb->head, skb->data, skb_tail_pointer(skb),
1676 skb_end_pointer(skb), skb->csum,
1677 skb->dev ? skb->dev->name : "<NULL>");
1679 skb_linearize(skb); /* check for skb_is_nonlinear is within skb_linearize */
1682 * Frame length checks and setting up the header pointers
1683 * was done in fcoe_rcv already.
1685 hp = (struct fcoe_hdr *) skb_network_header(skb);
1687 stats = per_cpu_ptr(lport->stats, get_cpu());
1688 if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) {
1689 if (stats->ErrorFrames < 5)
1690 printk(KERN_WARNING "fcoe: FCoE version "
1691 "mismatch: The frame has "
1692 "version %x, but the "
1693 "initiator supports version "
1694 "%x\n", FC_FCOE_DECAPS_VER(hp),
1699 skb_pull(skb, sizeof(struct fcoe_hdr));
1700 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
1703 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
1705 fp = (struct fc_frame *)skb;
1708 fr_sof(fp) = hp->fcoe_sof;
1710 /* Copy out the CRC and EOF trailer for access */
1711 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof)))
1713 fr_eof(fp) = crc_eof.fcoe_eof;
1714 fr_crc(fp) = crc_eof.fcoe_crc32;
1715 if (pskb_trim(skb, fr_len))
1718 if (!fcoe_filter_frames(lport, fp)) {
1720 fc_exch_recv(lport, fp);
1724 stats->ErrorFrames++;
1730 * fcoe_receive_work() - The per-CPU worker
1731 * @work: The work struct
1734 static void fcoe_receive_work(struct work_struct *work)
1736 struct fcoe_percpu_s *p;
1737 struct sk_buff *skb;
1738 struct sk_buff_head tmp;
1740 p = container_of(work, struct fcoe_percpu_s, work);
1741 skb_queue_head_init(&tmp);
1743 spin_lock_bh(&p->fcoe_rx_list.lock);
1744 skb_queue_splice_init(&p->fcoe_rx_list, &tmp);
1745 spin_unlock_bh(&p->fcoe_rx_list.lock);
1747 if (!skb_queue_len(&tmp))
1750 while ((skb = __skb_dequeue(&tmp)))
1751 fcoe_recv_frame(skb);
1755 * fcoe_dev_setup() - Setup the link change notification interface
1757 static void fcoe_dev_setup(void)
1759 register_dcbevent_notifier(&dcb_notifier);
1760 register_netdevice_notifier(&fcoe_notifier);
1764 * fcoe_dev_cleanup() - Cleanup the link change notification interface
1766 static void fcoe_dev_cleanup(void)
1768 unregister_dcbevent_notifier(&dcb_notifier);
1769 unregister_netdevice_notifier(&fcoe_notifier);
1772 static struct fcoe_interface *
1773 fcoe_hostlist_lookup_realdev_port(struct net_device *netdev)
1775 struct fcoe_interface *fcoe;
1776 struct net_device *real_dev;
1778 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
1779 if (is_vlan_dev(fcoe->netdev))
1780 real_dev = vlan_dev_real_dev(fcoe->netdev);
1782 real_dev = fcoe->netdev;
1784 if (netdev == real_dev)
1790 static int fcoe_dcb_app_notification(struct notifier_block *notifier,
1791 ulong event, void *ptr)
1793 struct dcb_app_type *entry = ptr;
1794 struct fcoe_ctlr *ctlr;
1795 struct fcoe_interface *fcoe;
1796 struct net_device *netdev;
1799 if (entry->app.selector != DCB_APP_IDTYPE_ETHTYPE)
1802 netdev = dev_get_by_index(&init_net, entry->ifindex);
1806 fcoe = fcoe_hostlist_lookup_realdev_port(netdev);
1811 ctlr = fcoe_to_ctlr(fcoe);
1813 if (entry->dcbx & DCB_CAP_DCBX_VER_CEE)
1814 prio = ffs(entry->app.priority) - 1;
1816 prio = entry->app.priority;
1821 if (entry->app.protocol == ETH_P_FIP ||
1822 entry->app.protocol == ETH_P_FCOE)
1823 ctlr->priority = prio;
1825 if (entry->app.protocol == ETH_P_FCOE)
1826 fcoe->priority = prio;
1832 * fcoe_device_notification() - Handler for net device events
1833 * @notifier: The context of the notification
1834 * @event: The type of event
1835 * @ptr: The net device that the event was on
1837 * This function is called by the Ethernet driver in case of link change event.
1839 * Returns: 0 for success
1841 static int fcoe_device_notification(struct notifier_block *notifier,
1842 ulong event, void *ptr)
1844 struct fcoe_ctlr_device *cdev;
1845 struct fc_lport *lport = NULL;
1846 struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
1847 struct fcoe_ctlr *ctlr;
1848 struct fcoe_interface *fcoe;
1849 struct fc_stats *stats;
1850 u32 link_possible = 1;
1854 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
1855 if (fcoe->netdev == netdev) {
1856 ctlr = fcoe_to_ctlr(fcoe);
1868 case NETDEV_GOING_DOWN:
1874 case NETDEV_CHANGEMTU:
1875 if (netdev->features & NETIF_F_FCOE_MTU)
1877 mfs = netdev->mtu - (sizeof(struct fcoe_hdr) +
1878 sizeof(struct fcoe_crc_eof));
1879 if (mfs >= FC_MIN_MAX_FRAME)
1880 fc_set_mfs(lport, mfs);
1882 case NETDEV_REGISTER:
1884 case NETDEV_UNREGISTER:
1885 list_del(&fcoe->list);
1886 fcoe_vport_remove(lport);
1887 mutex_lock(&fcoe_config_mutex);
1888 fcoe_if_destroy(lport);
1890 fcoe_interface_remove(fcoe);
1891 fcoe_interface_cleanup(fcoe);
1892 mutex_unlock(&fcoe_config_mutex);
1893 fcoe_ctlr_device_delete(fcoe_ctlr_to_ctlr_dev(ctlr));
1895 case NETDEV_FEAT_CHANGE:
1896 fcoe_netdev_features_change(lport, netdev);
1899 FCOE_NETDEV_DBG(netdev, "Unknown event %ld "
1900 "from netdev netlink\n", event);
1903 fcoe_link_speed_update(lport);
1905 cdev = fcoe_ctlr_to_ctlr_dev(ctlr);
1907 if (link_possible && !fcoe_link_ok(lport)) {
1908 switch (cdev->enabled) {
1909 case FCOE_CTLR_DISABLED:
1910 pr_info("Link up while interface is disabled.\n");
1912 case FCOE_CTLR_ENABLED:
1913 case FCOE_CTLR_UNUSED:
1914 fcoe_ctlr_link_up(ctlr);
1916 } else if (fcoe_ctlr_link_down(ctlr)) {
1917 switch (cdev->enabled) {
1918 case FCOE_CTLR_DISABLED:
1919 pr_info("Link down while interface is disabled.\n");
1921 case FCOE_CTLR_ENABLED:
1922 case FCOE_CTLR_UNUSED:
1923 stats = per_cpu_ptr(lport->stats, get_cpu());
1924 stats->LinkFailureCount++;
1926 fcoe_clean_pending_queue(lport);
1934 * fcoe_disable() - Disables a FCoE interface
1935 * @netdev : The net_device object the Ethernet interface to create on
1937 * Called from fcoe transport.
1939 * Returns: 0 for success
1941 * Deprecated: use fcoe_ctlr_enabled()
1943 static int fcoe_disable(struct net_device *netdev)
1945 struct fcoe_ctlr *ctlr;
1946 struct fcoe_interface *fcoe;
1949 mutex_lock(&fcoe_config_mutex);
1952 fcoe = fcoe_hostlist_lookup_port(netdev);
1956 ctlr = fcoe_to_ctlr(fcoe);
1957 fcoe_ctlr_link_down(ctlr);
1958 fcoe_clean_pending_queue(ctlr->lp);
1962 mutex_unlock(&fcoe_config_mutex);
1967 * fcoe_enable() - Enables a FCoE interface
1968 * @netdev : The net_device object the Ethernet interface to create on
1970 * Called from fcoe transport.
1972 * Returns: 0 for success
1974 static int fcoe_enable(struct net_device *netdev)
1976 struct fcoe_ctlr *ctlr;
1977 struct fcoe_interface *fcoe;
1980 mutex_lock(&fcoe_config_mutex);
1982 fcoe = fcoe_hostlist_lookup_port(netdev);
1990 ctlr = fcoe_to_ctlr(fcoe);
1992 if (!fcoe_link_ok(ctlr->lp))
1993 fcoe_ctlr_link_up(ctlr);
1996 mutex_unlock(&fcoe_config_mutex);
2001 * fcoe_ctlr_enabled() - Enable or disable an FCoE Controller
2002 * @cdev: The FCoE Controller that is being enabled or disabled
2004 * fcoe_sysfs will ensure that the state of 'enabled' has
2005 * changed, so no checking is necessary here. This routine simply
2006 * calls fcoe_enable or fcoe_disable, both of which are deprecated.
2007 * When those routines are removed the functionality can be merged
2010 static int fcoe_ctlr_enabled(struct fcoe_ctlr_device *cdev)
2012 struct fcoe_ctlr *ctlr = fcoe_ctlr_device_priv(cdev);
2013 struct fc_lport *lport = ctlr->lp;
2014 struct net_device *netdev = fcoe_netdev(lport);
2016 switch (cdev->enabled) {
2017 case FCOE_CTLR_ENABLED:
2018 return fcoe_enable(netdev);
2019 case FCOE_CTLR_DISABLED:
2020 return fcoe_disable(netdev);
2021 case FCOE_CTLR_UNUSED:
2028 * fcoe_ctlr_mode() - Switch FIP mode
2029 * @ctlr_dev: The FCoE Controller that is being modified
2031 * When the FIP mode has been changed we need to update
2032 * the multicast addresses to ensure we get the correct
2035 static void fcoe_ctlr_mode(struct fcoe_ctlr_device *ctlr_dev)
2037 struct fcoe_ctlr *ctlr = fcoe_ctlr_device_priv(ctlr_dev);
2038 struct fcoe_interface *fcoe = fcoe_ctlr_priv(ctlr);
2040 if (ctlr_dev->mode == FIP_CONN_TYPE_VN2VN &&
2041 ctlr->mode != FIP_MODE_VN2VN) {
2042 dev_mc_del(fcoe->netdev, FIP_ALL_ENODE_MACS);
2043 dev_mc_add(fcoe->netdev, FIP_ALL_VN2VN_MACS);
2044 dev_mc_add(fcoe->netdev, FIP_ALL_P2P_MACS);
2045 } else if (ctlr->mode != FIP_MODE_FABRIC) {
2046 dev_mc_del(fcoe->netdev, FIP_ALL_VN2VN_MACS);
2047 dev_mc_del(fcoe->netdev, FIP_ALL_P2P_MACS);
2048 dev_mc_add(fcoe->netdev, FIP_ALL_ENODE_MACS);
2050 fcoe_ctlr_set_fip_mode(ctlr_dev);
2054 * fcoe_destroy() - Destroy a FCoE interface
2055 * @netdev : The net_device object the Ethernet interface to create on
2057 * Called from fcoe transport
2059 * Returns: 0 for success
2061 static int fcoe_destroy(struct net_device *netdev)
2063 struct fcoe_ctlr *ctlr;
2064 struct fcoe_interface *fcoe;
2065 struct fc_lport *lport;
2066 struct fcoe_port *port;
2069 mutex_lock(&fcoe_config_mutex);
2071 fcoe = fcoe_hostlist_lookup_port(netdev);
2076 ctlr = fcoe_to_ctlr(fcoe);
2078 port = lport_priv(lport);
2079 list_del(&fcoe->list);
2080 queue_work(fcoe_wq, &port->destroy_work);
2083 mutex_unlock(&fcoe_config_mutex);
2088 * fcoe_destroy_work() - Destroy a FCoE port in a deferred work context
2089 * @work: Handle to the FCoE port to be destroyed
2091 static void fcoe_destroy_work(struct work_struct *work)
2093 struct fcoe_ctlr_device *cdev;
2094 struct fcoe_ctlr *ctlr;
2095 struct fcoe_port *port;
2096 struct fcoe_interface *fcoe;
2098 port = container_of(work, struct fcoe_port, destroy_work);
2100 fcoe_vport_remove(port->lport);
2102 mutex_lock(&fcoe_config_mutex);
2105 ctlr = fcoe_to_ctlr(fcoe);
2106 cdev = fcoe_ctlr_to_ctlr_dev(ctlr);
2109 fcoe_if_destroy(port->lport);
2111 fcoe_interface_remove(fcoe);
2113 fcoe_interface_cleanup(fcoe);
2115 mutex_unlock(&fcoe_config_mutex);
2117 fcoe_ctlr_device_delete(cdev);
2121 * fcoe_match() - Check if the FCoE is supported on the given netdevice
2122 * @netdev : The net_device object the Ethernet interface to create on
2124 * Called from fcoe transport.
2126 * Returns: always returns true as this is the default FCoE transport,
2127 * i.e., support all netdevs.
2129 static bool fcoe_match(struct net_device *netdev)
2135 * fcoe_dcb_create() - Initialize DCB attributes and hooks
2136 * @fcoe: The new FCoE interface
2138 static void fcoe_dcb_create(struct fcoe_interface *fcoe)
2140 int ctlr_prio = TC_PRIO_BESTEFFORT;
2141 int fcoe_prio = TC_PRIO_INTERACTIVE;
2142 struct fcoe_ctlr *ctlr = fcoe_to_ctlr(fcoe);
2146 struct net_device *netdev = fcoe->realdev;
2147 struct dcb_app app = {
2149 .protocol = ETH_P_FCOE
2152 /* setup DCB priority attributes. */
2153 if (netdev && netdev->dcbnl_ops && netdev->dcbnl_ops->getdcbx) {
2154 dcbx = netdev->dcbnl_ops->getdcbx(netdev);
2156 if (dcbx & DCB_CAP_DCBX_VER_IEEE) {
2157 app.selector = IEEE_8021QAZ_APP_SEL_ETHERTYPE;
2158 up = dcb_ieee_getapp_mask(netdev, &app);
2159 app.protocol = ETH_P_FIP;
2160 fup = dcb_ieee_getapp_mask(netdev, &app);
2162 app.selector = DCB_APP_IDTYPE_ETHTYPE;
2163 up = dcb_getapp(netdev, &app);
2164 app.protocol = ETH_P_FIP;
2165 fup = dcb_getapp(netdev, &app);
2168 fcoe_prio = ffs(up) ? ffs(up) - 1 : 0;
2169 ctlr_prio = ffs(fup) ? ffs(fup) - 1 : fcoe_prio;
2172 fcoe->priority = fcoe_prio;
2173 ctlr->priority = ctlr_prio;
2176 enum fcoe_create_link_state {
2177 FCOE_CREATE_LINK_DOWN,
2178 FCOE_CREATE_LINK_UP,
2182 * _fcoe_create() - (internal) Create a fcoe interface
2183 * @netdev : The net_device object the Ethernet interface to create on
2184 * @fip_mode: The FIP mode for this creation
2185 * @link_state: The ctlr link state on creation
2187 * Called from either the libfcoe 'create' module parameter
2188 * via fcoe_create or from fcoe_syfs's ctlr_create file.
2190 * libfcoe's 'create' module parameter is deprecated so some
2191 * consolidation of code can be done when that interface is
2194 static int _fcoe_create(struct net_device *netdev, enum fip_mode fip_mode,
2195 enum fcoe_create_link_state link_state)
2198 struct fcoe_ctlr_device *ctlr_dev;
2199 struct fcoe_ctlr *ctlr;
2200 struct fcoe_interface *fcoe;
2201 struct fc_lport *lport;
2203 mutex_lock(&fcoe_config_mutex);
2206 /* look for existing lport */
2207 if (fcoe_hostlist_lookup(netdev)) {
2212 fcoe = fcoe_interface_create(netdev, fip_mode);
2218 ctlr = fcoe_to_ctlr(fcoe);
2219 ctlr_dev = fcoe_ctlr_to_ctlr_dev(ctlr);
2220 lport = fcoe_if_create(fcoe, &ctlr_dev->dev, 0);
2221 if (IS_ERR(lport)) {
2222 printk(KERN_ERR "fcoe: Failed to create interface (%s)\n",
2226 fcoe_interface_remove(fcoe);
2228 fcoe_interface_cleanup(fcoe);
2229 mutex_unlock(&fcoe_config_mutex);
2230 fcoe_ctlr_device_delete(ctlr_dev);
2234 /* Make this the "master" N_Port */
2237 /* setup DCB priority attributes. */
2238 fcoe_dcb_create(fcoe);
2240 /* start FIP Discovery and FLOGI */
2241 lport->boot_time = jiffies;
2242 fc_fabric_login(lport);
2245 * If the fcoe_ctlr_device is to be set to DISABLED
2246 * it must be done after the lport is added to the
2247 * hostlist, but before the rtnl_lock is released.
2248 * This is because the rtnl_lock protects the
2249 * hostlist that fcoe_device_notification uses. If
2250 * the FCoE Controller is intended to be created
2251 * DISABLED then 'enabled' needs to be considered
2252 * handling link events. 'enabled' must be set
2253 * before the lport can be found in the hostlist
2254 * when a link up event is received.
2256 if (link_state == FCOE_CREATE_LINK_UP)
2257 ctlr_dev->enabled = FCOE_CTLR_ENABLED;
2259 ctlr_dev->enabled = FCOE_CTLR_DISABLED;
2261 if (link_state == FCOE_CREATE_LINK_UP &&
2262 !fcoe_link_ok(lport)) {
2264 fcoe_ctlr_link_up(ctlr);
2265 mutex_unlock(&fcoe_config_mutex);
2271 mutex_unlock(&fcoe_config_mutex);
2277 * fcoe_create() - Create a fcoe interface
2278 * @netdev : The net_device object the Ethernet interface to create on
2279 * @fip_mode: The FIP mode for this creation
2281 * Called from fcoe transport
2283 * Returns: 0 for success
2285 static int fcoe_create(struct net_device *netdev, enum fip_mode fip_mode)
2287 return _fcoe_create(netdev, fip_mode, FCOE_CREATE_LINK_UP);
2291 * fcoe_ctlr_alloc() - Allocate a fcoe interface from fcoe_sysfs
2292 * @netdev: The net_device to be used by the allocated FCoE Controller
2294 * This routine is called from fcoe_sysfs. It will start the fcoe_ctlr
2295 * in a link_down state. The allows the user an opportunity to configure
2296 * the FCoE Controller from sysfs before enabling the FCoE Controller.
2298 * Creating in with this routine starts the FCoE Controller in Fabric
2299 * mode. The user can change to VN2VN or another mode before enabling.
2301 static int fcoe_ctlr_alloc(struct net_device *netdev)
2303 return _fcoe_create(netdev, FIP_MODE_FABRIC,
2304 FCOE_CREATE_LINK_DOWN);
2308 * fcoe_link_ok() - Check if the link is OK for a local port
2309 * @lport: The local port to check link on
2311 * Returns: 0 if link is UP and OK, -1 if not
2314 static int fcoe_link_ok(struct fc_lport *lport)
2316 struct net_device *netdev = fcoe_netdev(lport);
2318 if (netif_oper_up(netdev))
2324 * fcoe_percpu_clean() - Clear all pending skbs for an local port
2325 * @lport: The local port whose skbs are to be cleared
2327 * Must be called with fcoe_create_mutex held to single-thread completion.
2329 * This flushes the pending skbs by flush the work item for each CPU. The work
2330 * item on each possible CPU is flushed because we may have used the per-CPU
2331 * struct of an offline CPU.
2333 static void fcoe_percpu_clean(struct fc_lport *lport)
2335 struct fcoe_percpu_s *pp;
2338 for_each_possible_cpu(cpu) {
2339 pp = &per_cpu(fcoe_percpu, cpu);
2341 flush_work(&pp->work);
2346 * fcoe_reset() - Reset a local port
2347 * @shost: The SCSI host associated with the local port to be reset
2349 * Returns: Always 0 (return value required by FC transport template)
2351 static int fcoe_reset(struct Scsi_Host *shost)
2353 struct fc_lport *lport = shost_priv(shost);
2354 struct fcoe_port *port = lport_priv(lport);
2355 struct fcoe_interface *fcoe = port->priv;
2356 struct fcoe_ctlr *ctlr = fcoe_to_ctlr(fcoe);
2357 struct fcoe_ctlr_device *cdev = fcoe_ctlr_to_ctlr_dev(ctlr);
2359 fcoe_ctlr_link_down(ctlr);
2360 fcoe_clean_pending_queue(ctlr->lp);
2362 if (cdev->enabled != FCOE_CTLR_DISABLED &&
2363 !fcoe_link_ok(ctlr->lp))
2364 fcoe_ctlr_link_up(ctlr);
2369 * fcoe_hostlist_lookup_port() - Find the FCoE interface associated with a net device
2370 * @netdev: The net device used as a key
2372 * Locking: Must be called with the RNL mutex held.
2374 * Returns: NULL or the FCoE interface
2376 static struct fcoe_interface *
2377 fcoe_hostlist_lookup_port(const struct net_device *netdev)
2379 struct fcoe_interface *fcoe;
2381 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
2382 if (fcoe->netdev == netdev)
2389 * fcoe_hostlist_lookup() - Find the local port associated with a
2391 * @netdev: The netdevice used as a key
2393 * Locking: Must be called with the RTNL mutex held
2395 * Returns: NULL or the local port
2397 static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *netdev)
2399 struct fcoe_ctlr *ctlr;
2400 struct fcoe_interface *fcoe;
2402 fcoe = fcoe_hostlist_lookup_port(netdev);
2403 ctlr = fcoe_to_ctlr(fcoe);
2404 return (fcoe) ? ctlr->lp : NULL;
2408 * fcoe_hostlist_add() - Add the FCoE interface identified by a local
2409 * port to the hostlist
2410 * @lport: The local port that identifies the FCoE interface to be added
2412 * Locking: must be called with the RTNL mutex held
2414 * Returns: 0 for success
2416 static int fcoe_hostlist_add(const struct fc_lport *lport)
2418 struct fcoe_interface *fcoe;
2419 struct fcoe_port *port;
2421 fcoe = fcoe_hostlist_lookup_port(fcoe_netdev(lport));
2423 port = lport_priv(lport);
2425 list_add_tail(&fcoe->list, &fcoe_hostlist);
2431 * fcoe_hostlist_del() - Remove the FCoE interface identified by a local
2432 * port to the hostlist
2433 * @lport: The local port that identifies the FCoE interface to be added
2435 * Locking: must be called with the RTNL mutex held
2438 static void fcoe_hostlist_del(const struct fc_lport *lport)
2440 struct fcoe_interface *fcoe;
2441 struct fcoe_port *port;
2443 port = lport_priv(lport);
2445 list_del(&fcoe->list);
2449 static struct fcoe_transport fcoe_sw_transport = {
2450 .name = {FCOE_TRANSPORT_DEFAULT},
2452 .list = LIST_HEAD_INIT(fcoe_sw_transport.list),
2453 .match = fcoe_match,
2454 .alloc = fcoe_ctlr_alloc,
2455 .create = fcoe_create,
2456 .destroy = fcoe_destroy,
2457 .enable = fcoe_enable,
2458 .disable = fcoe_disable,
2462 * fcoe_init() - Initialize fcoe.ko
2464 * Returns: 0 on success, or a negative value on failure
2466 static int __init fcoe_init(void)
2468 struct fcoe_percpu_s *p;
2472 fcoe_wq = alloc_workqueue("fcoe", 0, 0);
2476 /* register as a fcoe transport */
2477 rc = fcoe_transport_attach(&fcoe_sw_transport);
2479 printk(KERN_ERR "failed to register an fcoe transport, check "
2480 "if libfcoe is loaded\n");
2484 mutex_lock(&fcoe_config_mutex);
2486 for_each_possible_cpu(cpu) {
2487 p = per_cpu_ptr(&fcoe_percpu, cpu);
2488 INIT_WORK(&p->work, fcoe_receive_work);
2489 skb_queue_head_init(&p->fcoe_rx_list);
2492 /* Setup link change notification */
2495 rc = fcoe_if_init();
2499 mutex_unlock(&fcoe_config_mutex);
2503 mutex_unlock(&fcoe_config_mutex);
2505 destroy_workqueue(fcoe_wq);
2508 module_init(fcoe_init);
2511 * fcoe_exit() - Clean up fcoe.ko
2513 * Returns: 0 on success or a negative value on failure
2515 static void __exit fcoe_exit(void)
2517 struct fcoe_interface *fcoe, *tmp;
2518 struct fcoe_ctlr *ctlr;
2519 struct fcoe_port *port;
2522 mutex_lock(&fcoe_config_mutex);
2526 /* releases the associated fcoe hosts */
2528 list_for_each_entry_safe(fcoe, tmp, &fcoe_hostlist, list) {
2529 ctlr = fcoe_to_ctlr(fcoe);
2530 port = lport_priv(ctlr->lp);
2531 fcoe_hostlist_del(port->lport);
2532 queue_work(fcoe_wq, &port->destroy_work);
2536 for_each_possible_cpu(cpu)
2537 fcoe_thread_cleanup_local(cpu);
2539 mutex_unlock(&fcoe_config_mutex);
2542 * destroy_work's may be chained but destroy_workqueue()
2543 * can take care of them. Just kill the fcoe_wq.
2545 destroy_workqueue(fcoe_wq);
2548 * Detaching from the scsi transport must happen after all
2549 * destroys are done on the fcoe_wq. destroy_workqueue will
2550 * enusre the fcoe_wq is flushed.
2554 /* detach from fcoe transport */
2555 fcoe_transport_detach(&fcoe_sw_transport);
2557 module_exit(fcoe_exit);
2560 * fcoe_flogi_resp() - FCoE specific FLOGI and FDISC response handler
2561 * @seq: active sequence in the FLOGI or FDISC exchange
2562 * @fp: response frame, or error encoded in a pointer (timeout)
2563 * @arg: pointer to the fcoe_ctlr structure
2565 * This handles MAC address management for FCoE, then passes control on to
2566 * the libfc FLOGI response handler.
2568 static void fcoe_flogi_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
2570 struct fcoe_ctlr *fip = arg;
2571 struct fc_exch *exch = fc_seq_exch(seq);
2572 struct fc_lport *lport = exch->lp;
2578 mac = fr_cb(fp)->granted_mac;
2580 if (is_zero_ether_addr(mac))
2581 fcoe_ctlr_recv_flogi(fip, lport, fp);
2582 if (!is_zero_ether_addr(mac))
2583 fcoe_update_src_mac(lport, mac);
2585 fc_lport_flogi_resp(seq, fp, lport);
2589 * fcoe_logo_resp() - FCoE specific LOGO response handler
2590 * @seq: active sequence in the LOGO exchange
2591 * @fp: response frame, or error encoded in a pointer (timeout)
2592 * @arg: pointer to the fcoe_ctlr structure
2594 * This handles MAC address management for FCoE, then passes control on to
2595 * the libfc LOGO response handler.
2597 static void fcoe_logo_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
2599 struct fc_lport *lport = arg;
2600 static u8 zero_mac[ETH_ALEN] = { 0 };
2603 fcoe_update_src_mac(lport, zero_mac);
2604 fc_lport_logo_resp(seq, fp, lport);
2608 * fcoe_elsct_send - FCoE specific ELS handler
2610 * This does special case handling of FIP encapsualted ELS exchanges for FCoE,
2611 * using FCoE specific response handlers and passing the FIP controller as
2612 * the argument (the lport is still available from the exchange).
2614 * Most of the work here is just handed off to the libfc routine.
2616 static struct fc_seq *fcoe_elsct_send(struct fc_lport *lport, u32 did,
2617 struct fc_frame *fp, unsigned int op,
2618 void (*resp)(struct fc_seq *,
2621 void *arg, u32 timeout)
2623 struct fcoe_port *port = lport_priv(lport);
2624 struct fcoe_interface *fcoe = port->priv;
2625 struct fcoe_ctlr *fip = fcoe_to_ctlr(fcoe);
2626 struct fc_frame_header *fh = fc_frame_header_get(fp);
2631 if (lport->point_to_multipoint)
2633 return fc_elsct_send(lport, did, fp, op, fcoe_flogi_resp,
2636 /* only hook onto fabric logouts, not port logouts */
2637 if (ntoh24(fh->fh_d_id) != FC_FID_FLOGI)
2639 return fc_elsct_send(lport, did, fp, op, fcoe_logo_resp,
2642 return fc_elsct_send(lport, did, fp, op, resp, arg, timeout);
2646 * fcoe_vport_create() - create an fc_host/scsi_host for a vport
2647 * @vport: fc_vport object to create a new fc_host for
2648 * @disabled: start the new fc_host in a disabled state by default?
2650 * Returns: 0 for success
2652 static int fcoe_vport_create(struct fc_vport *vport, bool disabled)
2654 struct Scsi_Host *shost = vport_to_shost(vport);
2655 struct fc_lport *n_port = shost_priv(shost);
2656 struct fcoe_port *port = lport_priv(n_port);
2657 struct fcoe_interface *fcoe = port->priv;
2658 struct net_device *netdev = fcoe->netdev;
2659 struct fc_lport *vn_port;
2663 rc = fcoe_validate_vport_create(vport);
2665 fcoe_wwn_to_str(vport->port_name, buf, sizeof(buf));
2666 printk(KERN_ERR "fcoe: Failed to create vport, "
2667 "WWPN (0x%s) already exists\n",
2672 mutex_lock(&fcoe_config_mutex);
2674 vn_port = fcoe_if_create(fcoe, &vport->dev, 1);
2676 mutex_unlock(&fcoe_config_mutex);
2678 if (IS_ERR(vn_port)) {
2679 printk(KERN_ERR "fcoe: fcoe_vport_create(%s) failed\n",
2685 fc_vport_set_state(vport, FC_VPORT_DISABLED);
2687 vn_port->boot_time = jiffies;
2688 fc_fabric_login(vn_port);
2689 fc_vport_setlink(vn_port);
2695 * fcoe_vport_destroy() - destroy the fc_host/scsi_host for a vport
2696 * @vport: fc_vport object that is being destroyed
2698 * Returns: 0 for success
2700 static int fcoe_vport_destroy(struct fc_vport *vport)
2702 struct Scsi_Host *shost = vport_to_shost(vport);
2703 struct fc_lport *n_port = shost_priv(shost);
2704 struct fc_lport *vn_port = vport->dd_data;
2706 mutex_lock(&n_port->lp_mutex);
2707 list_del(&vn_port->list);
2708 mutex_unlock(&n_port->lp_mutex);
2710 mutex_lock(&fcoe_config_mutex);
2712 fcoe_if_destroy(vn_port);
2714 mutex_unlock(&fcoe_config_mutex);
2720 * fcoe_vport_remove() - remove attached vports
2721 * @lport: lport for which the vports should be removed
2723 static void fcoe_vport_remove(struct fc_lport *lport)
2725 struct Scsi_Host *shost;
2726 struct fc_host_attrs *fc_host;
2727 unsigned long flags;
2728 struct fc_vport *vport;
2729 struct fc_vport *next_vport;
2731 shost = lport->host;
2732 fc_host = shost_to_fc_host(shost);
2734 /* Loop through all the vports and mark them for deletion */
2735 spin_lock_irqsave(shost->host_lock, flags);
2736 list_for_each_entry_safe(vport, next_vport, &fc_host->vports, peers) {
2737 if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING)) {
2740 vport->flags |= FC_VPORT_DELETING;
2741 queue_work(fc_host_work_q(shost),
2742 &vport->vport_delete_work);
2745 spin_unlock_irqrestore(shost->host_lock, flags);
2747 flush_workqueue(fc_host_work_q(shost));
2751 * fcoe_vport_disable() - change vport state
2752 * @vport: vport to bring online/offline
2753 * @disable: should the vport be disabled?
2755 static int fcoe_vport_disable(struct fc_vport *vport, bool disable)
2757 struct fc_lport *lport = vport->dd_data;
2760 fc_vport_set_state(vport, FC_VPORT_DISABLED);
2761 fc_fabric_logoff(lport);
2763 lport->boot_time = jiffies;
2764 fc_fabric_login(lport);
2765 fc_vport_setlink(lport);
2772 * fcoe_set_vport_symbolic_name() - append vport string to symbolic name
2773 * @vport: fc_vport with a new symbolic name string
2775 * After generating a new symbolic name string, a new RSPN_ID request is
2776 * sent to the name server. There is no response handler, so if it fails
2777 * for some reason it will not be retried.
2779 static void fcoe_set_vport_symbolic_name(struct fc_vport *vport)
2781 struct fc_lport *lport = vport->dd_data;
2782 struct fc_frame *fp;
2785 snprintf(fc_host_symbolic_name(lport->host), FC_SYMBOLIC_NAME_SIZE,
2786 "%s v%s over %s : %s", FCOE_NAME, FCOE_VERSION,
2787 fcoe_netdev(lport)->name, vport->symbolic_name);
2789 if (lport->state != LPORT_ST_READY)
2792 len = strnlen(fc_host_symbolic_name(lport->host), 255);
2793 fp = fc_frame_alloc(lport,
2794 sizeof(struct fc_ct_hdr) +
2795 sizeof(struct fc_ns_rspn) + len);
2798 lport->tt.elsct_send(lport, FC_FID_DIR_SERV, fp, FC_NS_RSPN_ID,
2799 NULL, NULL, 3 * lport->r_a_tov);
2802 static void fcoe_fcf_get_vlan_id(struct fcoe_fcf_device *fcf_dev)
2804 struct fcoe_ctlr_device *ctlr_dev =
2805 fcoe_fcf_dev_to_ctlr_dev(fcf_dev);
2806 struct fcoe_ctlr *ctlr = fcoe_ctlr_device_priv(ctlr_dev);
2807 struct fcoe_interface *fcoe = fcoe_ctlr_priv(ctlr);
2809 fcf_dev->vlan_id = vlan_dev_vlan_id(fcoe->netdev);
2813 * fcoe_set_port_id() - Callback from libfc when Port_ID is set.
2814 * @lport: the local port
2815 * @port_id: the port ID
2816 * @fp: the received frame, if any, that caused the port_id to be set.
2818 * This routine handles the case where we received a FLOGI and are
2819 * entering point-to-point mode. We need to call fcoe_ctlr_recv_flogi()
2820 * so it can set the non-mapped mode and gateway address.
2822 * The FLOGI LS_ACC is handled by fcoe_flogi_resp().
2824 static void fcoe_set_port_id(struct fc_lport *lport,
2825 u32 port_id, struct fc_frame *fp)
2827 struct fcoe_port *port = lport_priv(lport);
2828 struct fcoe_interface *fcoe = port->priv;
2829 struct fcoe_ctlr *ctlr = fcoe_to_ctlr(fcoe);
2831 if (fp && fc_frame_payload_op(fp) == ELS_FLOGI)
2832 fcoe_ctlr_recv_flogi(ctlr, lport, fp);