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>
45 #include <scsi/libfc.h>
46 #include <scsi/fc_frame.h>
47 #include <scsi/libfcoe.h>
51 MODULE_AUTHOR("Open-FCoE.org");
52 MODULE_DESCRIPTION("FCoE");
53 MODULE_LICENSE("GPL v2");
55 /* Performance tuning parameters for fcoe */
56 static unsigned int fcoe_ddp_min = 4096;
57 module_param_named(ddp_min, fcoe_ddp_min, uint, S_IRUGO | S_IWUSR);
58 MODULE_PARM_DESC(ddp_min, "Minimum I/O size in bytes for " \
59 "Direct Data Placement (DDP).");
61 DEFINE_MUTEX(fcoe_config_mutex);
63 static struct workqueue_struct *fcoe_wq;
65 /* fcoe_percpu_clean completion. Waiter protected by fcoe_create_mutex */
66 static DECLARE_COMPLETION(fcoe_flush_completion);
69 /* must only by accessed under the RTNL mutex */
70 LIST_HEAD(fcoe_hostlist);
71 DEFINE_PER_CPU(struct fcoe_percpu_s, fcoe_percpu);
73 /* Function Prototypes */
74 static int fcoe_reset(struct Scsi_Host *);
75 static int fcoe_xmit(struct fc_lport *, struct fc_frame *);
76 static int fcoe_rcv(struct sk_buff *, struct net_device *,
77 struct packet_type *, struct net_device *);
78 static int fcoe_percpu_receive_thread(void *);
79 static void fcoe_percpu_clean(struct fc_lport *);
80 static int fcoe_link_speed_update(struct fc_lport *);
81 static int fcoe_link_ok(struct fc_lport *);
83 static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *);
84 static int fcoe_hostlist_add(const struct fc_lport *);
86 static int fcoe_device_notification(struct notifier_block *, ulong, void *);
87 static void fcoe_dev_setup(void);
88 static void fcoe_dev_cleanup(void);
89 static struct fcoe_interface
90 *fcoe_hostlist_lookup_port(const struct net_device *);
92 static int fcoe_fip_recv(struct sk_buff *, struct net_device *,
93 struct packet_type *, struct net_device *);
95 static void fcoe_fip_send(struct fcoe_ctlr *, struct sk_buff *);
96 static void fcoe_update_src_mac(struct fc_lport *, u8 *);
97 static u8 *fcoe_get_src_mac(struct fc_lport *);
98 static void fcoe_destroy_work(struct work_struct *);
100 static int fcoe_ddp_setup(struct fc_lport *, u16, struct scatterlist *,
102 static int fcoe_ddp_done(struct fc_lport *, u16);
103 static int fcoe_ddp_target(struct fc_lport *, u16, struct scatterlist *,
105 static int fcoe_cpu_callback(struct notifier_block *, unsigned long, void *);
106 static int fcoe_dcb_app_notification(struct notifier_block *notifier,
107 ulong event, void *ptr);
109 static bool fcoe_match(struct net_device *netdev);
110 static int fcoe_create(struct net_device *netdev, enum fip_state fip_mode);
111 static int fcoe_destroy(struct net_device *netdev);
112 static int fcoe_enable(struct net_device *netdev);
113 static int fcoe_disable(struct net_device *netdev);
115 static struct fc_seq *fcoe_elsct_send(struct fc_lport *,
116 u32 did, struct fc_frame *,
118 void (*resp)(struct fc_seq *,
121 void *, u32 timeout);
122 static void fcoe_recv_frame(struct sk_buff *skb);
124 static void fcoe_get_lesb(struct fc_lport *, struct fc_els_lesb *);
126 /* notification function for packets from net device */
127 static struct notifier_block fcoe_notifier = {
128 .notifier_call = fcoe_device_notification,
131 /* notification function for CPU hotplug events */
132 static struct notifier_block fcoe_cpu_notifier = {
133 .notifier_call = fcoe_cpu_callback,
136 /* notification function for DCB events */
137 static struct notifier_block dcb_notifier = {
138 .notifier_call = fcoe_dcb_app_notification,
141 static struct scsi_transport_template *fcoe_nport_scsi_transport;
142 static struct scsi_transport_template *fcoe_vport_scsi_transport;
144 static int fcoe_vport_destroy(struct fc_vport *);
145 static int fcoe_vport_create(struct fc_vport *, bool disabled);
146 static int fcoe_vport_disable(struct fc_vport *, bool disable);
147 static void fcoe_set_vport_symbolic_name(struct fc_vport *);
148 static void fcoe_set_port_id(struct fc_lport *, u32, struct fc_frame *);
150 static struct libfc_function_template fcoe_libfc_fcn_templ = {
151 .frame_send = fcoe_xmit,
152 .ddp_setup = fcoe_ddp_setup,
153 .ddp_done = fcoe_ddp_done,
154 .ddp_target = fcoe_ddp_target,
155 .elsct_send = fcoe_elsct_send,
156 .get_lesb = fcoe_get_lesb,
157 .lport_set_port_id = fcoe_set_port_id,
160 struct fc_function_template fcoe_nport_fc_functions = {
161 .show_host_node_name = 1,
162 .show_host_port_name = 1,
163 .show_host_supported_classes = 1,
164 .show_host_supported_fc4s = 1,
165 .show_host_active_fc4s = 1,
166 .show_host_maxframe_size = 1,
168 .show_host_port_id = 1,
169 .show_host_supported_speeds = 1,
170 .get_host_speed = fc_get_host_speed,
171 .show_host_speed = 1,
172 .show_host_port_type = 1,
173 .get_host_port_state = fc_get_host_port_state,
174 .show_host_port_state = 1,
175 .show_host_symbolic_name = 1,
177 .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
178 .show_rport_maxframe_size = 1,
179 .show_rport_supported_classes = 1,
181 .show_host_fabric_name = 1,
182 .show_starget_node_name = 1,
183 .show_starget_port_name = 1,
184 .show_starget_port_id = 1,
185 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
186 .show_rport_dev_loss_tmo = 1,
187 .get_fc_host_stats = fc_get_host_stats,
188 .issue_fc_host_lip = fcoe_reset,
190 .terminate_rport_io = fc_rport_terminate_io,
192 .vport_create = fcoe_vport_create,
193 .vport_delete = fcoe_vport_destroy,
194 .vport_disable = fcoe_vport_disable,
195 .set_vport_symbolic_name = fcoe_set_vport_symbolic_name,
197 .bsg_request = fc_lport_bsg_request,
200 struct fc_function_template fcoe_vport_fc_functions = {
201 .show_host_node_name = 1,
202 .show_host_port_name = 1,
203 .show_host_supported_classes = 1,
204 .show_host_supported_fc4s = 1,
205 .show_host_active_fc4s = 1,
206 .show_host_maxframe_size = 1,
208 .show_host_port_id = 1,
209 .show_host_supported_speeds = 1,
210 .get_host_speed = fc_get_host_speed,
211 .show_host_speed = 1,
212 .show_host_port_type = 1,
213 .get_host_port_state = fc_get_host_port_state,
214 .show_host_port_state = 1,
215 .show_host_symbolic_name = 1,
217 .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
218 .show_rport_maxframe_size = 1,
219 .show_rport_supported_classes = 1,
221 .show_host_fabric_name = 1,
222 .show_starget_node_name = 1,
223 .show_starget_port_name = 1,
224 .show_starget_port_id = 1,
225 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
226 .show_rport_dev_loss_tmo = 1,
227 .get_fc_host_stats = fc_get_host_stats,
228 .issue_fc_host_lip = fcoe_reset,
230 .terminate_rport_io = fc_rport_terminate_io,
232 .bsg_request = fc_lport_bsg_request,
235 static struct scsi_host_template fcoe_shost_template = {
236 .module = THIS_MODULE,
237 .name = "FCoE Driver",
238 .proc_name = FCOE_NAME,
239 .queuecommand = fc_queuecommand,
240 .eh_abort_handler = fc_eh_abort,
241 .eh_device_reset_handler = fc_eh_device_reset,
242 .eh_host_reset_handler = fc_eh_host_reset,
243 .slave_alloc = fc_slave_alloc,
244 .change_queue_depth = fc_change_queue_depth,
245 .change_queue_type = fc_change_queue_type,
248 .can_queue = FCOE_MAX_OUTSTANDING_COMMANDS,
249 .use_clustering = ENABLE_CLUSTERING,
250 .sg_tablesize = SG_ALL,
251 .max_sectors = 0xffff,
255 * fcoe_interface_setup() - Setup a FCoE interface
256 * @fcoe: The new FCoE interface
257 * @netdev: The net device that the fcoe interface is on
259 * Returns : 0 for success
260 * Locking: must be called with the RTNL mutex held
262 static int fcoe_interface_setup(struct fcoe_interface *fcoe,
263 struct net_device *netdev)
265 struct fcoe_ctlr *fip = &fcoe->ctlr;
266 struct netdev_hw_addr *ha;
267 struct net_device *real_dev;
268 u8 flogi_maddr[ETH_ALEN];
269 const struct net_device_ops *ops;
271 fcoe->netdev = netdev;
273 /* Let LLD initialize for FCoE */
274 ops = netdev->netdev_ops;
275 if (ops->ndo_fcoe_enable) {
276 if (ops->ndo_fcoe_enable(netdev))
277 FCOE_NETDEV_DBG(netdev, "Failed to enable FCoE"
278 " specific feature for LLD.\n");
281 /* Do not support for bonding device */
282 if (netdev->priv_flags & IFF_BONDING && netdev->flags & IFF_MASTER) {
283 FCOE_NETDEV_DBG(netdev, "Bonded interfaces not supported\n");
287 /* look for SAN MAC address, if multiple SAN MACs exist, only
288 * use the first one for SPMA */
289 real_dev = (netdev->priv_flags & IFF_802_1Q_VLAN) ?
290 vlan_dev_real_dev(netdev) : netdev;
291 fcoe->realdev = real_dev;
293 for_each_dev_addr(real_dev, ha) {
294 if ((ha->type == NETDEV_HW_ADDR_T_SAN) &&
295 (is_valid_ether_addr(ha->addr))) {
296 memcpy(fip->ctl_src_addr, ha->addr, ETH_ALEN);
303 /* setup Source Mac Address */
305 memcpy(fip->ctl_src_addr, netdev->dev_addr, netdev->addr_len);
308 * Add FCoE MAC address as second unicast MAC address
309 * or enter promiscuous mode if not capable of listening
310 * for multiple unicast MACs.
312 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
313 dev_uc_add(netdev, flogi_maddr);
315 dev_uc_add(netdev, fip->ctl_src_addr);
316 if (fip->mode == FIP_MODE_VN2VN) {
317 dev_mc_add(netdev, FIP_ALL_VN2VN_MACS);
318 dev_mc_add(netdev, FIP_ALL_P2P_MACS);
320 dev_mc_add(netdev, FIP_ALL_ENODE_MACS);
323 * setup the receive function from ethernet driver
324 * on the ethertype for the given device
326 fcoe->fcoe_packet_type.func = fcoe_rcv;
327 fcoe->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
328 fcoe->fcoe_packet_type.dev = netdev;
329 dev_add_pack(&fcoe->fcoe_packet_type);
331 fcoe->fip_packet_type.func = fcoe_fip_recv;
332 fcoe->fip_packet_type.type = htons(ETH_P_FIP);
333 fcoe->fip_packet_type.dev = netdev;
334 dev_add_pack(&fcoe->fip_packet_type);
340 * fcoe_interface_create() - Create a FCoE interface on a net device
341 * @netdev: The net device to create the FCoE interface on
342 * @fip_mode: The mode to use for FIP
344 * Returns: pointer to a struct fcoe_interface or NULL on error
346 static struct fcoe_interface *fcoe_interface_create(struct net_device *netdev,
347 enum fip_state fip_mode)
349 struct fcoe_interface *fcoe;
352 if (!try_module_get(THIS_MODULE)) {
353 FCOE_NETDEV_DBG(netdev,
354 "Could not get a reference to the module\n");
355 fcoe = ERR_PTR(-EBUSY);
359 fcoe = kzalloc(sizeof(*fcoe), GFP_KERNEL);
361 FCOE_NETDEV_DBG(netdev, "Could not allocate fcoe structure\n");
362 fcoe = ERR_PTR(-ENOMEM);
367 kref_init(&fcoe->kref);
372 fcoe_ctlr_init(&fcoe->ctlr, fip_mode);
373 fcoe->ctlr.send = fcoe_fip_send;
374 fcoe->ctlr.update_mac = fcoe_update_src_mac;
375 fcoe->ctlr.get_src_addr = fcoe_get_src_mac;
377 err = fcoe_interface_setup(fcoe, netdev);
379 fcoe_ctlr_destroy(&fcoe->ctlr);
389 module_put(THIS_MODULE);
395 * fcoe_interface_release() - fcoe_port kref release function
396 * @kref: Embedded reference count in an fcoe_interface struct
398 static void fcoe_interface_release(struct kref *kref)
400 struct fcoe_interface *fcoe;
401 struct net_device *netdev;
403 fcoe = container_of(kref, struct fcoe_interface, kref);
404 netdev = fcoe->netdev;
405 /* tear-down the FCoE controller */
406 fcoe_ctlr_destroy(&fcoe->ctlr);
409 module_put(THIS_MODULE);
413 * fcoe_interface_get() - Get a reference to a FCoE interface
414 * @fcoe: The FCoE interface to be held
416 static inline void fcoe_interface_get(struct fcoe_interface *fcoe)
418 kref_get(&fcoe->kref);
422 * fcoe_interface_put() - Put a reference to a FCoE interface
423 * @fcoe: The FCoE interface to be released
425 static inline void fcoe_interface_put(struct fcoe_interface *fcoe)
427 kref_put(&fcoe->kref, fcoe_interface_release);
431 * fcoe_interface_cleanup() - Clean up a FCoE interface
432 * @fcoe: The FCoE interface to be cleaned up
434 * Caller must be holding the RTNL mutex
436 void fcoe_interface_cleanup(struct fcoe_interface *fcoe)
438 struct net_device *netdev = fcoe->netdev;
439 struct fcoe_ctlr *fip = &fcoe->ctlr;
440 u8 flogi_maddr[ETH_ALEN];
441 const struct net_device_ops *ops;
446 * Don't listen for Ethernet packets anymore.
447 * synchronize_net() ensures that the packet handlers are not running
448 * on another CPU. dev_remove_pack() would do that, this calls the
449 * unsyncronized version __dev_remove_pack() to avoid multiple delays.
451 __dev_remove_pack(&fcoe->fcoe_packet_type);
452 __dev_remove_pack(&fcoe->fip_packet_type);
455 /* Delete secondary MAC addresses */
456 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
457 dev_uc_del(netdev, flogi_maddr);
459 dev_uc_del(netdev, fip->ctl_src_addr);
460 if (fip->mode == FIP_MODE_VN2VN) {
461 dev_mc_del(netdev, FIP_ALL_VN2VN_MACS);
462 dev_mc_del(netdev, FIP_ALL_P2P_MACS);
464 dev_mc_del(netdev, FIP_ALL_ENODE_MACS);
466 /* Tell the LLD we are done w/ FCoE */
467 ops = netdev->netdev_ops;
468 if (ops->ndo_fcoe_disable) {
469 if (ops->ndo_fcoe_disable(netdev))
470 FCOE_NETDEV_DBG(netdev, "Failed to disable FCoE"
471 " specific feature for LLD.\n");
476 /* Release the self-reference taken during fcoe_interface_create() */
477 fcoe_interface_put(fcoe);
481 * fcoe_fip_recv() - Handler for received FIP frames
482 * @skb: The receive skb
483 * @netdev: The associated net device
484 * @ptype: The packet_type structure which was used to register this handler
485 * @orig_dev: The original net_device the the skb was received on.
486 * (in case dev is a bond)
488 * Returns: 0 for success
490 static int fcoe_fip_recv(struct sk_buff *skb, struct net_device *netdev,
491 struct packet_type *ptype,
492 struct net_device *orig_dev)
494 struct fcoe_interface *fcoe;
496 fcoe = container_of(ptype, struct fcoe_interface, fip_packet_type);
497 fcoe_ctlr_recv(&fcoe->ctlr, skb);
502 * fcoe_port_send() - Send an Ethernet-encapsulated FIP/FCoE frame
503 * @port: The FCoE port
504 * @skb: The FIP/FCoE packet to be sent
506 static void fcoe_port_send(struct fcoe_port *port, struct sk_buff *skb)
508 if (port->fcoe_pending_queue.qlen)
509 fcoe_check_wait_queue(port->lport, skb);
510 else if (fcoe_start_io(skb))
511 fcoe_check_wait_queue(port->lport, skb);
515 * fcoe_fip_send() - Send an Ethernet-encapsulated FIP frame
516 * @fip: The FCoE controller
517 * @skb: The FIP packet to be sent
519 static void fcoe_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
521 skb->dev = fcoe_from_ctlr(fip)->netdev;
522 fcoe_port_send(lport_priv(fip->lp), skb);
526 * fcoe_update_src_mac() - Update the Ethernet MAC filters
527 * @lport: The local port to update the source MAC on
528 * @addr: Unicast MAC address to add
530 * Remove any previously-set unicast MAC filter.
531 * Add secondary FCoE MAC address filter for our OUI.
533 static void fcoe_update_src_mac(struct fc_lport *lport, u8 *addr)
535 struct fcoe_port *port = lport_priv(lport);
536 struct fcoe_interface *fcoe = port->priv;
539 if (!is_zero_ether_addr(port->data_src_addr))
540 dev_uc_del(fcoe->netdev, port->data_src_addr);
541 if (!is_zero_ether_addr(addr))
542 dev_uc_add(fcoe->netdev, addr);
543 memcpy(port->data_src_addr, addr, ETH_ALEN);
548 * fcoe_get_src_mac() - return the Ethernet source address for an lport
549 * @lport: libfc lport
551 static u8 *fcoe_get_src_mac(struct fc_lport *lport)
553 struct fcoe_port *port = lport_priv(lport);
555 return port->data_src_addr;
559 * fcoe_lport_config() - Set up a local port
560 * @lport: The local port to be setup
562 * Returns: 0 for success
564 static int fcoe_lport_config(struct fc_lport *lport)
568 lport->max_retry_count = 3;
569 lport->max_rport_retry_count = 3;
570 lport->e_d_tov = 2 * 1000; /* FC-FS default */
571 lport->r_a_tov = 2 * 2 * 1000;
572 lport->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
573 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
574 lport->does_npiv = 1;
576 fc_lport_init_stats(lport);
578 /* lport fc_lport related configuration */
579 fc_lport_config(lport);
581 /* offload related configuration */
582 lport->crc_offload = 0;
583 lport->seq_offload = 0;
584 lport->lro_enabled = 0;
592 * fcoe_netdev_features_change - Updates the lport's offload flags based
593 * on the LLD netdev's FCoE feature flags
595 static void fcoe_netdev_features_change(struct fc_lport *lport,
596 struct net_device *netdev)
598 mutex_lock(&lport->lp_mutex);
600 if (netdev->features & NETIF_F_SG)
605 if (netdev->features & NETIF_F_FCOE_CRC) {
606 lport->crc_offload = 1;
607 FCOE_NETDEV_DBG(netdev, "Supports FCCRC offload\n");
609 lport->crc_offload = 0;
612 if (netdev->features & NETIF_F_FSO) {
613 lport->seq_offload = 1;
614 lport->lso_max = netdev->gso_max_size;
615 FCOE_NETDEV_DBG(netdev, "Supports LSO for max len 0x%x\n",
618 lport->seq_offload = 0;
622 if (netdev->fcoe_ddp_xid) {
623 lport->lro_enabled = 1;
624 lport->lro_xid = netdev->fcoe_ddp_xid;
625 FCOE_NETDEV_DBG(netdev, "Supports LRO for max xid 0x%x\n",
628 lport->lro_enabled = 0;
632 mutex_unlock(&lport->lp_mutex);
636 * fcoe_netdev_config() - Set up net devive for SW FCoE
637 * @lport: The local port that is associated with the net device
638 * @netdev: The associated net device
640 * Must be called after fcoe_lport_config() as it will use local port mutex
642 * Returns: 0 for success
644 static int fcoe_netdev_config(struct fc_lport *lport, struct net_device *netdev)
648 struct fcoe_interface *fcoe;
649 struct fcoe_port *port;
651 /* Setup lport private data to point to fcoe softc */
652 port = lport_priv(lport);
656 * Determine max frame size based on underlying device and optional
657 * user-configured limit. If the MFS is too low, fcoe_link_ok()
658 * will return 0, so do this first.
661 if (netdev->features & NETIF_F_FCOE_MTU) {
663 FCOE_NETDEV_DBG(netdev, "Supports FCOE_MTU of %d bytes\n", mfs);
665 mfs -= (sizeof(struct fcoe_hdr) + sizeof(struct fcoe_crc_eof));
666 if (fc_set_mfs(lport, mfs))
669 /* offload features support */
670 fcoe_netdev_features_change(lport, netdev);
672 skb_queue_head_init(&port->fcoe_pending_queue);
673 port->fcoe_pending_queue_active = 0;
674 setup_timer(&port->timer, fcoe_queue_timer, (unsigned long)lport);
676 fcoe_link_speed_update(lport);
679 if (fcoe_get_wwn(netdev, &wwnn, NETDEV_FCOE_WWNN))
680 wwnn = fcoe_wwn_from_mac(fcoe->ctlr.ctl_src_addr, 1, 0);
681 fc_set_wwnn(lport, wwnn);
682 if (fcoe_get_wwn(netdev, &wwpn, NETDEV_FCOE_WWPN))
683 wwpn = fcoe_wwn_from_mac(fcoe->ctlr.ctl_src_addr,
685 fc_set_wwpn(lport, wwpn);
692 * fcoe_shost_config() - Set up the SCSI host associated with a local port
693 * @lport: The local port
694 * @dev: The device associated with the SCSI host
696 * Must be called after fcoe_lport_config() and fcoe_netdev_config()
698 * Returns: 0 for success
700 static int fcoe_shost_config(struct fc_lport *lport, struct device *dev)
704 /* lport scsi host config */
705 lport->host->max_lun = FCOE_MAX_LUN;
706 lport->host->max_id = FCOE_MAX_FCP_TARGET;
707 lport->host->max_channel = 0;
708 lport->host->max_cmd_len = FCOE_MAX_CMD_LEN;
711 lport->host->transportt = fcoe_vport_scsi_transport;
713 lport->host->transportt = fcoe_nport_scsi_transport;
715 /* add the new host to the SCSI-ml */
716 rc = scsi_add_host(lport->host, dev);
718 FCOE_NETDEV_DBG(fcoe_netdev(lport), "fcoe_shost_config: "
719 "error on scsi_add_host\n");
724 fc_host_max_npiv_vports(lport->host) = USHRT_MAX;
726 snprintf(fc_host_symbolic_name(lport->host), FC_SYMBOLIC_NAME_SIZE,
727 "%s v%s over %s", FCOE_NAME, FCOE_VERSION,
728 fcoe_netdev(lport)->name);
734 * fcoe_oem_match() - The match routine for the offloaded exchange manager
737 * This routine will be associated with an exchange manager (EM). When
738 * the libfc exchange handling code is looking for an EM to use it will
739 * call this routine and pass it the frame that it wishes to send. This
740 * routine will return True if the associated EM is to be used and False
741 * if the echange code should continue looking for an EM.
743 * The offload EM that this routine is associated with will handle any
744 * packets that are for SCSI read requests.
746 * This has been enhanced to work when FCoE stack is operating in target
749 * Returns: True for read types I/O, otherwise returns false.
751 bool fcoe_oem_match(struct fc_frame *fp)
753 struct fc_frame_header *fh = fc_frame_header_get(fp);
754 struct fcp_cmnd *fcp;
756 if (fc_fcp_is_read(fr_fsp(fp)) &&
757 (fr_fsp(fp)->data_len > fcoe_ddp_min))
759 else if (!(ntoh24(fh->fh_f_ctl) & FC_FC_EX_CTX)) {
760 fcp = fc_frame_payload_get(fp, sizeof(*fcp));
761 if (ntohs(fh->fh_rx_id) == FC_XID_UNKNOWN &&
762 fcp && (ntohl(fcp->fc_dl) > fcoe_ddp_min) &&
763 (fcp->fc_flags & FCP_CFL_WRDATA))
770 * fcoe_em_config() - Allocate and configure an exchange manager
771 * @lport: The local port that the new EM will be associated with
773 * Returns: 0 on success
775 static inline int fcoe_em_config(struct fc_lport *lport)
777 struct fcoe_port *port = lport_priv(lport);
778 struct fcoe_interface *fcoe = port->priv;
779 struct fcoe_interface *oldfcoe = NULL;
780 struct net_device *old_real_dev, *cur_real_dev;
781 u16 min_xid = FCOE_MIN_XID;
782 u16 max_xid = FCOE_MAX_XID;
785 * Check if need to allocate an em instance for
786 * offload exchange ids to be shared across all VN_PORTs/lport.
788 if (!lport->lro_enabled || !lport->lro_xid ||
789 (lport->lro_xid >= max_xid)) {
795 * Reuse existing offload em instance in case
796 * it is already allocated on real eth device
798 if (fcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
799 cur_real_dev = vlan_dev_real_dev(fcoe->netdev);
801 cur_real_dev = fcoe->netdev;
803 list_for_each_entry(oldfcoe, &fcoe_hostlist, list) {
804 if (oldfcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
805 old_real_dev = vlan_dev_real_dev(oldfcoe->netdev);
807 old_real_dev = oldfcoe->netdev;
809 if (cur_real_dev == old_real_dev) {
810 fcoe->oem = oldfcoe->oem;
816 if (!fc_exch_mgr_add(lport, fcoe->oem, fcoe_oem_match)) {
817 printk(KERN_ERR "fcoe_em_config: failed to add "
818 "offload em:%p on interface:%s\n",
819 fcoe->oem, fcoe->netdev->name);
823 fcoe->oem = fc_exch_mgr_alloc(lport, FC_CLASS_3,
824 FCOE_MIN_XID, lport->lro_xid,
827 printk(KERN_ERR "fcoe_em_config: failed to allocate "
828 "em for offload exches on interface:%s\n",
835 * Exclude offload EM xid range from next EM xid range.
837 min_xid += lport->lro_xid + 1;
840 if (!fc_exch_mgr_alloc(lport, FC_CLASS_3, min_xid, max_xid, NULL)) {
841 printk(KERN_ERR "fcoe_em_config: failed to "
842 "allocate em on interface %s\n", fcoe->netdev->name);
850 * fcoe_if_destroy() - Tear down a SW FCoE instance
851 * @lport: The local port to be destroyed
854 static void fcoe_if_destroy(struct fc_lport *lport)
856 struct fcoe_port *port = lport_priv(lport);
857 struct fcoe_interface *fcoe = port->priv;
858 struct net_device *netdev = fcoe->netdev;
860 FCOE_NETDEV_DBG(netdev, "Destroying interface\n");
862 /* Logout of the fabric */
863 fc_fabric_logoff(lport);
865 /* Cleanup the fc_lport */
866 fc_lport_destroy(lport);
868 /* Stop the transmit retry timer */
869 del_timer_sync(&port->timer);
871 /* Free existing transmit skbs */
872 fcoe_clean_pending_queue(lport);
875 if (!is_zero_ether_addr(port->data_src_addr))
876 dev_uc_del(netdev, port->data_src_addr);
879 /* Release reference held in fcoe_if_create() */
880 fcoe_interface_put(fcoe);
882 /* Free queued packets for the per-CPU receive threads */
883 fcoe_percpu_clean(lport);
885 /* Detach from the scsi-ml */
886 fc_remove_host(lport->host);
887 scsi_remove_host(lport->host);
889 /* Destroy lport scsi_priv */
890 fc_fcp_destroy(lport);
892 /* There are no more rports or I/O, free the EM */
893 fc_exch_mgr_free(lport);
895 /* Free memory used by statistical counters */
896 fc_lport_free_stats(lport);
898 /* Release the Scsi_Host */
899 scsi_host_put(lport->host);
903 * fcoe_ddp_setup() - Call a LLD's ddp_setup through the net device
904 * @lport: The local port to setup DDP for
905 * @xid: The exchange ID for this DDP transfer
906 * @sgl: The scatterlist describing this transfer
907 * @sgc: The number of sg items
909 * Returns: 0 if the DDP context was not configured
911 static int fcoe_ddp_setup(struct fc_lport *lport, u16 xid,
912 struct scatterlist *sgl, unsigned int sgc)
914 struct net_device *netdev = fcoe_netdev(lport);
916 if (netdev->netdev_ops->ndo_fcoe_ddp_setup)
917 return netdev->netdev_ops->ndo_fcoe_ddp_setup(netdev,
925 * fcoe_ddp_target() - Call a LLD's ddp_target through the net device
926 * @lport: The local port to setup DDP for
927 * @xid: The exchange ID for this DDP transfer
928 * @sgl: The scatterlist describing this transfer
929 * @sgc: The number of sg items
931 * Returns: 0 if the DDP context was not configured
933 static int fcoe_ddp_target(struct fc_lport *lport, u16 xid,
934 struct scatterlist *sgl, unsigned int sgc)
936 struct net_device *netdev = fcoe_netdev(lport);
938 if (netdev->netdev_ops->ndo_fcoe_ddp_target)
939 return netdev->netdev_ops->ndo_fcoe_ddp_target(netdev, xid,
947 * fcoe_ddp_done() - Call a LLD's ddp_done through the net device
948 * @lport: The local port to complete DDP on
949 * @xid: The exchange ID for this DDP transfer
951 * Returns: the length of data that have been completed by DDP
953 static int fcoe_ddp_done(struct fc_lport *lport, u16 xid)
955 struct net_device *netdev = fcoe_netdev(lport);
957 if (netdev->netdev_ops->ndo_fcoe_ddp_done)
958 return netdev->netdev_ops->ndo_fcoe_ddp_done(netdev, xid);
963 * fcoe_if_create() - Create a FCoE instance on an interface
964 * @fcoe: The FCoE interface to create a local port on
965 * @parent: The device pointer to be the parent in sysfs for the SCSI host
966 * @npiv: Indicates if the port is a vport or not
968 * Creates a fc_lport instance and a Scsi_Host instance and configure them.
970 * Returns: The allocated fc_lport or an error pointer
972 static struct fc_lport *fcoe_if_create(struct fcoe_interface *fcoe,
973 struct device *parent, int npiv)
975 struct net_device *netdev = fcoe->netdev;
976 struct fc_lport *lport, *n_port;
977 struct fcoe_port *port;
978 struct Scsi_Host *shost;
981 * parent is only a vport if npiv is 1,
982 * but we'll only use vport in that case so go ahead and set it
984 struct fc_vport *vport = dev_to_vport(parent);
986 FCOE_NETDEV_DBG(netdev, "Create Interface\n");
989 lport = libfc_host_alloc(&fcoe_shost_template, sizeof(*port));
991 lport = libfc_vport_create(vport, sizeof(*port));
994 FCOE_NETDEV_DBG(netdev, "Could not allocate host structure\n");
998 port = lport_priv(lport);
1001 port->max_queue_depth = FCOE_MAX_QUEUE_DEPTH;
1002 port->min_queue_depth = FCOE_MIN_QUEUE_DEPTH;
1003 INIT_WORK(&port->destroy_work, fcoe_destroy_work);
1005 /* configure a fc_lport including the exchange manager */
1006 rc = fcoe_lport_config(lport);
1008 FCOE_NETDEV_DBG(netdev, "Could not configure lport for the "
1014 FCOE_NETDEV_DBG(netdev, "Setting vport names, "
1015 "%16.16llx %16.16llx\n",
1016 vport->node_name, vport->port_name);
1017 fc_set_wwnn(lport, vport->node_name);
1018 fc_set_wwpn(lport, vport->port_name);
1021 /* configure lport network properties */
1022 rc = fcoe_netdev_config(lport, netdev);
1024 FCOE_NETDEV_DBG(netdev, "Could not configure netdev for the "
1026 goto out_lp_destroy;
1029 /* configure lport scsi host properties */
1030 rc = fcoe_shost_config(lport, parent);
1032 FCOE_NETDEV_DBG(netdev, "Could not configure shost for the "
1034 goto out_lp_destroy;
1037 /* Initialize the library */
1038 rc = fcoe_libfc_config(lport, &fcoe->ctlr, &fcoe_libfc_fcn_templ, 1);
1040 FCOE_NETDEV_DBG(netdev, "Could not configure libfc for the "
1042 goto out_lp_destroy;
1046 * fcoe_em_alloc() and fcoe_hostlist_add() both
1047 * need to be atomic with respect to other changes to the
1048 * hostlist since fcoe_em_alloc() looks for an existing EM
1049 * instance on host list updated by fcoe_hostlist_add().
1051 * This is currently handled through the fcoe_config_mutex
1055 /* lport exch manager allocation */
1056 rc = fcoe_em_config(lport);
1058 shost = vport_to_shost(vport);
1059 n_port = shost_priv(shost);
1060 rc = fc_exch_mgr_list_clone(n_port, lport);
1064 FCOE_NETDEV_DBG(netdev, "Could not configure the EM\n");
1065 goto out_lp_destroy;
1068 fcoe_interface_get(fcoe);
1072 fc_exch_mgr_free(lport);
1074 scsi_host_put(lport->host);
1080 * fcoe_if_init() - Initialization routine for fcoe.ko
1082 * Attaches the SW FCoE transport to the FC transport
1084 * Returns: 0 on success
1086 static int __init fcoe_if_init(void)
1088 /* attach to scsi transport */
1089 fcoe_nport_scsi_transport =
1090 fc_attach_transport(&fcoe_nport_fc_functions);
1091 fcoe_vport_scsi_transport =
1092 fc_attach_transport(&fcoe_vport_fc_functions);
1094 if (!fcoe_nport_scsi_transport) {
1095 printk(KERN_ERR "fcoe: Failed to attach to the FC transport\n");
1103 * fcoe_if_exit() - Tear down fcoe.ko
1105 * Detaches the SW FCoE transport from the FC transport
1107 * Returns: 0 on success
1109 int __exit fcoe_if_exit(void)
1111 fc_release_transport(fcoe_nport_scsi_transport);
1112 fc_release_transport(fcoe_vport_scsi_transport);
1113 fcoe_nport_scsi_transport = NULL;
1114 fcoe_vport_scsi_transport = NULL;
1119 * fcoe_percpu_thread_create() - Create a receive thread for an online CPU
1120 * @cpu: The CPU index of the CPU to create a receive thread for
1122 static void fcoe_percpu_thread_create(unsigned int cpu)
1124 struct fcoe_percpu_s *p;
1125 struct task_struct *thread;
1127 p = &per_cpu(fcoe_percpu, cpu);
1129 thread = kthread_create_on_node(fcoe_percpu_receive_thread,
1130 (void *)p, cpu_to_node(cpu),
1131 "fcoethread/%d", cpu);
1133 if (likely(!IS_ERR(thread))) {
1134 kthread_bind(thread, cpu);
1135 wake_up_process(thread);
1137 spin_lock_bh(&p->fcoe_rx_list.lock);
1139 spin_unlock_bh(&p->fcoe_rx_list.lock);
1144 * fcoe_percpu_thread_destroy() - Remove the receive thread of a CPU
1145 * @cpu: The CPU index of the CPU whose receive thread is to be destroyed
1147 * Destroys a per-CPU Rx thread. Any pending skbs are moved to the
1148 * current CPU's Rx thread. If the thread being destroyed is bound to
1149 * the CPU processing this context the skbs will be freed.
1151 static void fcoe_percpu_thread_destroy(unsigned int cpu)
1153 struct fcoe_percpu_s *p;
1154 struct task_struct *thread;
1155 struct page *crc_eof;
1156 struct sk_buff *skb;
1158 struct fcoe_percpu_s *p0;
1159 unsigned targ_cpu = get_cpu();
1160 #endif /* CONFIG_SMP */
1162 FCOE_DBG("Destroying receive thread for CPU %d\n", cpu);
1164 /* Prevent any new skbs from being queued for this CPU. */
1165 p = &per_cpu(fcoe_percpu, cpu);
1166 spin_lock_bh(&p->fcoe_rx_list.lock);
1169 crc_eof = p->crc_eof_page;
1170 p->crc_eof_page = NULL;
1171 p->crc_eof_offset = 0;
1172 spin_unlock_bh(&p->fcoe_rx_list.lock);
1176 * Don't bother moving the skb's if this context is running
1177 * on the same CPU that is having its thread destroyed. This
1178 * can easily happen when the module is removed.
1180 if (cpu != targ_cpu) {
1181 p0 = &per_cpu(fcoe_percpu, targ_cpu);
1182 spin_lock_bh(&p0->fcoe_rx_list.lock);
1184 FCOE_DBG("Moving frames from CPU %d to CPU %d\n",
1187 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1188 __skb_queue_tail(&p0->fcoe_rx_list, skb);
1189 spin_unlock_bh(&p0->fcoe_rx_list.lock);
1192 * The targeted CPU is not initialized and cannot accept
1193 * new skbs. Unlock the targeted CPU and drop the skbs
1194 * on the CPU that is going offline.
1196 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1198 spin_unlock_bh(&p0->fcoe_rx_list.lock);
1202 * This scenario occurs when the module is being removed
1203 * and all threads are being destroyed. skbs will continue
1204 * to be shifted from the CPU thread that is being removed
1205 * to the CPU thread associated with the CPU that is processing
1206 * the module removal. Once there is only one CPU Rx thread it
1207 * will reach this case and we will drop all skbs and later
1210 spin_lock_bh(&p->fcoe_rx_list.lock);
1211 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1213 spin_unlock_bh(&p->fcoe_rx_list.lock);
1218 * This a non-SMP scenario where the singular Rx thread is
1219 * being removed. Free all skbs and stop the thread.
1221 spin_lock_bh(&p->fcoe_rx_list.lock);
1222 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1224 spin_unlock_bh(&p->fcoe_rx_list.lock);
1228 kthread_stop(thread);
1235 * fcoe_cpu_callback() - Handler for CPU hotplug events
1236 * @nfb: The callback data block
1237 * @action: The event triggering the callback
1238 * @hcpu: The index of the CPU that the event is for
1240 * This creates or destroys per-CPU data for fcoe
1242 * Returns NOTIFY_OK always.
1244 static int fcoe_cpu_callback(struct notifier_block *nfb,
1245 unsigned long action, void *hcpu)
1247 unsigned cpu = (unsigned long)hcpu;
1251 case CPU_ONLINE_FROZEN:
1252 FCOE_DBG("CPU %x online: Create Rx thread\n", cpu);
1253 fcoe_percpu_thread_create(cpu);
1256 case CPU_DEAD_FROZEN:
1257 FCOE_DBG("CPU %x offline: Remove Rx thread\n", cpu);
1258 fcoe_percpu_thread_destroy(cpu);
1267 * fcoe_select_cpu() - Selects CPU to handle post-processing of incoming
1270 * This routine selects next CPU based on cpumask to distribute
1271 * incoming requests in round robin.
1273 * Returns: int CPU number
1275 static inline unsigned int fcoe_select_cpu(void)
1277 static unsigned int selected_cpu;
1279 selected_cpu = cpumask_next(selected_cpu, cpu_online_mask);
1280 if (selected_cpu >= nr_cpu_ids)
1281 selected_cpu = cpumask_first(cpu_online_mask);
1283 return selected_cpu;
1287 * fcoe_rcv() - Receive packets from a net device
1288 * @skb: The received packet
1289 * @netdev: The net device that the packet was received on
1290 * @ptype: The packet type context
1291 * @olddev: The last device net device
1293 * This routine is called by NET_RX_SOFTIRQ. It receives a packet, builds a
1294 * FC frame and passes the frame to libfc.
1296 * Returns: 0 for success
1298 int fcoe_rcv(struct sk_buff *skb, struct net_device *netdev,
1299 struct packet_type *ptype, struct net_device *olddev)
1301 struct fc_lport *lport;
1302 struct fcoe_rcv_info *fr;
1303 struct fcoe_interface *fcoe;
1304 struct fc_frame_header *fh;
1305 struct fcoe_percpu_s *fps;
1309 fcoe = container_of(ptype, struct fcoe_interface, fcoe_packet_type);
1310 lport = fcoe->ctlr.lp;
1311 if (unlikely(!lport)) {
1312 FCOE_NETDEV_DBG(netdev, "Cannot find hba structure");
1315 if (!lport->link_up)
1318 FCOE_NETDEV_DBG(netdev, "skb_info: len:%d data_len:%d head:%p "
1319 "data:%p tail:%p end:%p sum:%d dev:%s",
1320 skb->len, skb->data_len, skb->head, skb->data,
1321 skb_tail_pointer(skb), skb_end_pointer(skb),
1322 skb->csum, skb->dev ? skb->dev->name : "<NULL>");
1326 if (is_fip_mode(&fcoe->ctlr) &&
1327 compare_ether_addr(eh->h_source, fcoe->ctlr.dest_addr)) {
1328 FCOE_NETDEV_DBG(netdev, "wrong source mac address:%pM\n",
1334 * Check for minimum frame length, and make sure required FCoE
1335 * and FC headers are pulled into the linear data area.
1337 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
1338 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
1341 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
1342 fh = (struct fc_frame_header *) skb_transport_header(skb);
1344 if (ntoh24(&eh->h_dest[3]) != ntoh24(fh->fh_d_id)) {
1345 FCOE_NETDEV_DBG(netdev, "FC frame d_id mismatch with MAC:%pM\n",
1350 fr = fcoe_dev_from_skb(skb);
1354 * In case the incoming frame's exchange is originated from
1355 * the initiator, then received frame's exchange id is ANDed
1356 * with fc_cpu_mask bits to get the same cpu on which exchange
1357 * was originated, otherwise select cpu using rx exchange id
1358 * or fcoe_select_cpu().
1360 if (ntoh24(fh->fh_f_ctl) & FC_FC_EX_CTX)
1361 cpu = ntohs(fh->fh_ox_id) & fc_cpu_mask;
1363 if (ntohs(fh->fh_rx_id) == FC_XID_UNKNOWN)
1364 cpu = fcoe_select_cpu();
1366 cpu = ntohs(fh->fh_rx_id) & fc_cpu_mask;
1369 if (cpu >= nr_cpu_ids)
1372 fps = &per_cpu(fcoe_percpu, cpu);
1373 spin_lock_bh(&fps->fcoe_rx_list.lock);
1374 if (unlikely(!fps->thread)) {
1376 * The targeted CPU is not ready, let's target
1377 * the first CPU now. For non-SMP systems this
1378 * will check the same CPU twice.
1380 FCOE_NETDEV_DBG(netdev, "CPU is online, but no receive thread "
1381 "ready for incoming skb- using first online "
1384 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1385 cpu = cpumask_first(cpu_online_mask);
1386 fps = &per_cpu(fcoe_percpu, cpu);
1387 spin_lock_bh(&fps->fcoe_rx_list.lock);
1389 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1395 * We now have a valid CPU that we're targeting for
1396 * this skb. We also have this receive thread locked,
1397 * so we're free to queue skbs into it's queue.
1400 /* If this is a SCSI-FCP frame, and this is already executing on the
1401 * correct CPU, and the queue for this CPU is empty, then go ahead
1402 * and process the frame directly in the softirq context.
1403 * This lets us process completions without context switching from the
1404 * NET_RX softirq, to our receive processing thread, and then back to
1405 * BLOCK softirq context.
1407 if (fh->fh_type == FC_TYPE_FCP &&
1408 cpu == smp_processor_id() &&
1409 skb_queue_empty(&fps->fcoe_rx_list)) {
1410 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1411 fcoe_recv_frame(skb);
1413 __skb_queue_tail(&fps->fcoe_rx_list, skb);
1414 if (fps->fcoe_rx_list.qlen == 1)
1415 wake_up_process(fps->thread);
1416 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1421 per_cpu_ptr(lport->dev_stats, get_cpu())->ErrorFrames++;
1429 * fcoe_alloc_paged_crc_eof() - Allocate a page to be used for the trailer CRC
1430 * @skb: The packet to be transmitted
1431 * @tlen: The total length of the trailer
1433 * Returns: 0 for success
1435 static int fcoe_alloc_paged_crc_eof(struct sk_buff *skb, int tlen)
1437 struct fcoe_percpu_s *fps;
1440 fps = &get_cpu_var(fcoe_percpu);
1441 rc = fcoe_get_paged_crc_eof(skb, tlen, fps);
1442 put_cpu_var(fcoe_percpu);
1448 * fcoe_xmit() - Transmit a FCoE frame
1449 * @lport: The local port that the frame is to be transmitted for
1450 * @fp: The frame to be transmitted
1452 * Return: 0 for success
1454 int fcoe_xmit(struct fc_lport *lport, struct fc_frame *fp)
1459 struct fcoe_crc_eof *cp;
1460 struct sk_buff *skb;
1461 struct fcoe_dev_stats *stats;
1462 struct fc_frame_header *fh;
1463 unsigned int hlen; /* header length implies the version */
1464 unsigned int tlen; /* trailer length */
1465 unsigned int elen; /* eth header, may include vlan */
1466 struct fcoe_port *port = lport_priv(lport);
1467 struct fcoe_interface *fcoe = port->priv;
1469 struct fcoe_hdr *hp;
1471 WARN_ON((fr_len(fp) % sizeof(u32)) != 0);
1473 fh = fc_frame_header_get(fp);
1475 wlen = skb->len / FCOE_WORD_TO_BYTE;
1477 if (!lport->link_up) {
1482 if (unlikely(fh->fh_type == FC_TYPE_ELS) &&
1483 fcoe_ctlr_els_send(&fcoe->ctlr, lport, skb))
1489 elen = sizeof(struct ethhdr);
1490 hlen = sizeof(struct fcoe_hdr);
1491 tlen = sizeof(struct fcoe_crc_eof);
1492 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
1495 if (likely(lport->crc_offload)) {
1496 skb->ip_summed = CHECKSUM_PARTIAL;
1497 skb->csum_start = skb_headroom(skb);
1498 skb->csum_offset = skb->len;
1501 skb->ip_summed = CHECKSUM_NONE;
1502 crc = fcoe_fc_crc(fp);
1505 /* copy port crc and eof to the skb buff */
1506 if (skb_is_nonlinear(skb)) {
1508 if (fcoe_alloc_paged_crc_eof(skb, tlen)) {
1512 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
1513 cp = kmap_atomic(skb_frag_page(frag), KM_SKB_DATA_SOFTIRQ)
1514 + frag->page_offset;
1516 cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
1519 memset(cp, 0, sizeof(*cp));
1521 cp->fcoe_crc32 = cpu_to_le32(~crc);
1523 if (skb_is_nonlinear(skb)) {
1524 kunmap_atomic(cp, KM_SKB_DATA_SOFTIRQ);
1528 /* adjust skb network/transport offsets to match mac/fcoe/port */
1529 skb_push(skb, elen + hlen);
1530 skb_reset_mac_header(skb);
1531 skb_reset_network_header(skb);
1532 skb->mac_len = elen;
1533 skb->protocol = htons(ETH_P_FCOE);
1534 skb->priority = port->priority;
1536 if (fcoe->netdev->priv_flags & IFF_802_1Q_VLAN &&
1537 fcoe->realdev->features & NETIF_F_HW_VLAN_TX) {
1538 skb->vlan_tci = VLAN_TAG_PRESENT |
1539 vlan_dev_vlan_id(fcoe->netdev);
1540 skb->dev = fcoe->realdev;
1542 skb->dev = fcoe->netdev;
1544 /* fill up mac and fcoe headers */
1546 eh->h_proto = htons(ETH_P_FCOE);
1547 memcpy(eh->h_dest, fcoe->ctlr.dest_addr, ETH_ALEN);
1548 if (fcoe->ctlr.map_dest)
1549 memcpy(eh->h_dest + 3, fh->fh_d_id, 3);
1551 if (unlikely(fcoe->ctlr.flogi_oxid != FC_XID_UNKNOWN))
1552 memcpy(eh->h_source, fcoe->ctlr.ctl_src_addr, ETH_ALEN);
1554 memcpy(eh->h_source, port->data_src_addr, ETH_ALEN);
1556 hp = (struct fcoe_hdr *)(eh + 1);
1557 memset(hp, 0, sizeof(*hp));
1559 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
1562 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
1563 if (lport->seq_offload && fr_max_payload(fp)) {
1564 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
1565 skb_shinfo(skb)->gso_size = fr_max_payload(fp);
1567 skb_shinfo(skb)->gso_type = 0;
1568 skb_shinfo(skb)->gso_size = 0;
1570 /* update tx stats: regardless if LLD fails */
1571 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
1573 stats->TxWords += wlen;
1576 /* send down to lld */
1578 fcoe_port_send(port, skb);
1583 * fcoe_percpu_flush_done() - Indicate per-CPU queue flush completion
1584 * @skb: The completed skb (argument required by destructor)
1586 static void fcoe_percpu_flush_done(struct sk_buff *skb)
1588 complete(&fcoe_flush_completion);
1592 * fcoe_filter_frames() - filter out bad fcoe frames, i.e. bad CRC
1593 * @lport: The local port the frame was received on
1594 * @fp: The received frame
1596 * Return: 0 on passing filtering checks
1598 static inline int fcoe_filter_frames(struct fc_lport *lport,
1599 struct fc_frame *fp)
1601 struct fcoe_interface *fcoe;
1602 struct fc_frame_header *fh;
1603 struct sk_buff *skb = (struct sk_buff *)fp;
1604 struct fcoe_dev_stats *stats;
1607 * We only check CRC if no offload is available and if it is
1608 * it's solicited data, in which case, the FCP layer would
1609 * check it during the copy.
1611 if (lport->crc_offload && skb->ip_summed == CHECKSUM_UNNECESSARY)
1612 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1614 fr_flags(fp) |= FCPHF_CRC_UNCHECKED;
1616 fh = (struct fc_frame_header *) skb_transport_header(skb);
1617 fh = fc_frame_header_get(fp);
1618 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA && fh->fh_type == FC_TYPE_FCP)
1621 fcoe = ((struct fcoe_port *)lport_priv(lport))->priv;
1622 if (is_fip_mode(&fcoe->ctlr) && fc_frame_payload_op(fp) == ELS_LOGO &&
1623 ntoh24(fh->fh_s_id) == FC_FID_FLOGI) {
1624 FCOE_DBG("fcoe: dropping FCoE lport LOGO in fip mode\n");
1628 if (!(fr_flags(fp) & FCPHF_CRC_UNCHECKED) ||
1629 le32_to_cpu(fr_crc(fp)) == ~crc32(~0, skb->data, skb->len)) {
1630 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1634 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
1635 stats->InvalidCRCCount++;
1636 if (stats->InvalidCRCCount < 5)
1637 printk(KERN_WARNING "fcoe: dropping frame with CRC error\n");
1643 * fcoe_recv_frame() - process a single received frame
1644 * @skb: frame to process
1646 static void fcoe_recv_frame(struct sk_buff *skb)
1649 struct fc_lport *lport;
1650 struct fcoe_rcv_info *fr;
1651 struct fcoe_dev_stats *stats;
1652 struct fcoe_crc_eof crc_eof;
1653 struct fc_frame *fp;
1654 struct fcoe_port *port;
1655 struct fcoe_hdr *hp;
1657 fr = fcoe_dev_from_skb(skb);
1659 if (unlikely(!lport)) {
1660 if (skb->destructor != fcoe_percpu_flush_done)
1661 FCOE_NETDEV_DBG(skb->dev, "NULL lport in skb");
1666 FCOE_NETDEV_DBG(skb->dev, "skb_info: len:%d data_len:%d "
1667 "head:%p data:%p tail:%p end:%p sum:%d dev:%s",
1668 skb->len, skb->data_len,
1669 skb->head, skb->data, skb_tail_pointer(skb),
1670 skb_end_pointer(skb), skb->csum,
1671 skb->dev ? skb->dev->name : "<NULL>");
1673 port = lport_priv(lport);
1674 if (skb_is_nonlinear(skb))
1675 skb_linearize(skb); /* not ideal */
1678 * Frame length checks and setting up the header pointers
1679 * was done in fcoe_rcv already.
1681 hp = (struct fcoe_hdr *) skb_network_header(skb);
1683 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
1684 if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) {
1685 if (stats->ErrorFrames < 5)
1686 printk(KERN_WARNING "fcoe: FCoE version "
1687 "mismatch: The frame has "
1688 "version %x, but the "
1689 "initiator supports version "
1690 "%x\n", FC_FCOE_DECAPS_VER(hp),
1695 skb_pull(skb, sizeof(struct fcoe_hdr));
1696 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
1699 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
1701 fp = (struct fc_frame *)skb;
1704 fr_sof(fp) = hp->fcoe_sof;
1706 /* Copy out the CRC and EOF trailer for access */
1707 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof)))
1709 fr_eof(fp) = crc_eof.fcoe_eof;
1710 fr_crc(fp) = crc_eof.fcoe_crc32;
1711 if (pskb_trim(skb, fr_len))
1714 if (!fcoe_filter_frames(lport, fp)) {
1716 fc_exch_recv(lport, fp);
1720 stats->ErrorFrames++;
1726 * fcoe_percpu_receive_thread() - The per-CPU packet receive thread
1727 * @arg: The per-CPU context
1729 * Return: 0 for success
1731 int fcoe_percpu_receive_thread(void *arg)
1733 struct fcoe_percpu_s *p = arg;
1734 struct sk_buff *skb;
1736 set_user_nice(current, -20);
1738 while (!kthread_should_stop()) {
1740 spin_lock_bh(&p->fcoe_rx_list.lock);
1741 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) == NULL) {
1742 set_current_state(TASK_INTERRUPTIBLE);
1743 spin_unlock_bh(&p->fcoe_rx_list.lock);
1745 set_current_state(TASK_RUNNING);
1746 if (kthread_should_stop())
1748 spin_lock_bh(&p->fcoe_rx_list.lock);
1750 spin_unlock_bh(&p->fcoe_rx_list.lock);
1751 fcoe_recv_frame(skb);
1757 * fcoe_dev_setup() - Setup the link change notification interface
1759 static void fcoe_dev_setup(void)
1761 register_dcbevent_notifier(&dcb_notifier);
1762 register_netdevice_notifier(&fcoe_notifier);
1766 * fcoe_dev_cleanup() - Cleanup the link change notification interface
1768 static void fcoe_dev_cleanup(void)
1770 unregister_dcbevent_notifier(&dcb_notifier);
1771 unregister_netdevice_notifier(&fcoe_notifier);
1774 static struct fcoe_interface *
1775 fcoe_hostlist_lookup_realdev_port(struct net_device *netdev)
1777 struct fcoe_interface *fcoe;
1778 struct net_device *real_dev;
1780 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
1781 if (fcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
1782 real_dev = vlan_dev_real_dev(fcoe->netdev);
1784 real_dev = fcoe->netdev;
1786 if (netdev == real_dev)
1792 static int fcoe_dcb_app_notification(struct notifier_block *notifier,
1793 ulong event, void *ptr)
1795 struct dcb_app_type *entry = ptr;
1796 struct fcoe_interface *fcoe;
1797 struct net_device *netdev;
1798 struct fcoe_port *port;
1801 if (entry->app.selector != DCB_APP_IDTYPE_ETHTYPE)
1804 netdev = dev_get_by_index(&init_net, entry->ifindex);
1808 fcoe = fcoe_hostlist_lookup_realdev_port(netdev);
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 fcoe->ctlr.priority = prio;
1825 if (entry->app.protocol == ETH_P_FCOE) {
1826 port = lport_priv(fcoe->ctlr.lp);
1827 port->priority = prio;
1834 * fcoe_device_notification() - Handler for net device events
1835 * @notifier: The context of the notification
1836 * @event: The type of event
1837 * @ptr: The net device that the event was on
1839 * This function is called by the Ethernet driver in case of link change event.
1841 * Returns: 0 for success
1843 static int fcoe_device_notification(struct notifier_block *notifier,
1844 ulong event, void *ptr)
1846 struct fc_lport *lport = NULL;
1847 struct net_device *netdev = ptr;
1848 struct fcoe_interface *fcoe;
1849 struct fcoe_port *port;
1850 struct fcoe_dev_stats *stats;
1851 u32 link_possible = 1;
1855 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
1856 if (fcoe->netdev == netdev) {
1857 lport = fcoe->ctlr.lp;
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 port = lport_priv(fcoe->ctlr.lp);
1887 queue_work(fcoe_wq, &port->destroy_work);
1890 case NETDEV_FEAT_CHANGE:
1891 fcoe_netdev_features_change(lport, netdev);
1894 FCOE_NETDEV_DBG(netdev, "Unknown event %ld "
1895 "from netdev netlink\n", event);
1898 fcoe_link_speed_update(lport);
1900 if (link_possible && !fcoe_link_ok(lport))
1901 fcoe_ctlr_link_up(&fcoe->ctlr);
1902 else if (fcoe_ctlr_link_down(&fcoe->ctlr)) {
1903 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
1904 stats->LinkFailureCount++;
1906 fcoe_clean_pending_queue(lport);
1913 * fcoe_disable() - Disables a FCoE interface
1914 * @netdev : The net_device object the Ethernet interface to create on
1916 * Called from fcoe transport.
1918 * Returns: 0 for success
1920 static int fcoe_disable(struct net_device *netdev)
1922 struct fcoe_interface *fcoe;
1925 mutex_lock(&fcoe_config_mutex);
1928 fcoe = fcoe_hostlist_lookup_port(netdev);
1932 fcoe_ctlr_link_down(&fcoe->ctlr);
1933 fcoe_clean_pending_queue(fcoe->ctlr.lp);
1937 mutex_unlock(&fcoe_config_mutex);
1942 * fcoe_enable() - Enables a FCoE interface
1943 * @netdev : The net_device object the Ethernet interface to create on
1945 * Called from fcoe transport.
1947 * Returns: 0 for success
1949 static int fcoe_enable(struct net_device *netdev)
1951 struct fcoe_interface *fcoe;
1954 mutex_lock(&fcoe_config_mutex);
1956 fcoe = fcoe_hostlist_lookup_port(netdev);
1961 else if (!fcoe_link_ok(fcoe->ctlr.lp))
1962 fcoe_ctlr_link_up(&fcoe->ctlr);
1964 mutex_unlock(&fcoe_config_mutex);
1969 * fcoe_destroy() - Destroy a FCoE interface
1970 * @netdev : The net_device object the Ethernet interface to create on
1972 * Called from fcoe transport
1974 * Returns: 0 for success
1976 static int fcoe_destroy(struct net_device *netdev)
1978 struct fcoe_interface *fcoe;
1979 struct fc_lport *lport;
1980 struct fcoe_port *port;
1983 mutex_lock(&fcoe_config_mutex);
1985 fcoe = fcoe_hostlist_lookup_port(netdev);
1990 lport = fcoe->ctlr.lp;
1991 port = lport_priv(lport);
1992 list_del(&fcoe->list);
1993 queue_work(fcoe_wq, &port->destroy_work);
1996 mutex_unlock(&fcoe_config_mutex);
2001 * fcoe_destroy_work() - Destroy a FCoE port in a deferred work context
2002 * @work: Handle to the FCoE port to be destroyed
2004 static void fcoe_destroy_work(struct work_struct *work)
2006 struct fcoe_port *port;
2007 struct fcoe_interface *fcoe;
2010 port = container_of(work, struct fcoe_port, destroy_work);
2011 mutex_lock(&fcoe_config_mutex);
2013 /* set if this is an NPIV port */
2014 npiv = port->lport->vport ? 1 : 0;
2017 fcoe_if_destroy(port->lport);
2019 /* Do not tear down the fcoe interface for NPIV port */
2021 fcoe_interface_cleanup(fcoe);
2023 mutex_unlock(&fcoe_config_mutex);
2027 * fcoe_match() - Check if the FCoE is supported on the given netdevice
2028 * @netdev : The net_device object the Ethernet interface to create on
2030 * Called from fcoe transport.
2032 * Returns: always returns true as this is the default FCoE transport,
2033 * i.e., support all netdevs.
2035 static bool fcoe_match(struct net_device *netdev)
2041 * fcoe_dcb_create() - Initialize DCB attributes and hooks
2042 * @netdev: The net_device object of the L2 link that should be queried
2043 * @port: The fcoe_port to bind FCoE APP priority with
2046 static void fcoe_dcb_create(struct fcoe_interface *fcoe)
2051 struct net_device *netdev = fcoe->realdev;
2052 struct fcoe_port *port = lport_priv(fcoe->ctlr.lp);
2053 struct dcb_app app = {
2055 .protocol = ETH_P_FCOE
2058 /* setup DCB priority attributes. */
2059 if (netdev && netdev->dcbnl_ops && netdev->dcbnl_ops->getdcbx) {
2060 dcbx = netdev->dcbnl_ops->getdcbx(netdev);
2062 if (dcbx & DCB_CAP_DCBX_VER_IEEE) {
2063 app.selector = IEEE_8021QAZ_APP_SEL_ETHERTYPE;
2064 up = dcb_ieee_getapp_mask(netdev, &app);
2065 app.protocol = ETH_P_FIP;
2066 fup = dcb_ieee_getapp_mask(netdev, &app);
2068 app.selector = DCB_APP_IDTYPE_ETHTYPE;
2069 up = dcb_getapp(netdev, &app);
2070 app.protocol = ETH_P_FIP;
2071 fup = dcb_getapp(netdev, &app);
2074 port->priority = ffs(up) ? ffs(up) - 1 : 0;
2075 fcoe->ctlr.priority = ffs(fup) ? ffs(fup) - 1 : port->priority;
2081 * fcoe_create() - Create a fcoe interface
2082 * @netdev : The net_device object the Ethernet interface to create on
2083 * @fip_mode: The FIP mode for this creation
2085 * Called from fcoe transport
2087 * Returns: 0 for success
2089 static int fcoe_create(struct net_device *netdev, enum fip_state fip_mode)
2092 struct fcoe_interface *fcoe;
2093 struct fc_lport *lport;
2095 mutex_lock(&fcoe_config_mutex);
2098 /* look for existing lport */
2099 if (fcoe_hostlist_lookup(netdev)) {
2104 fcoe = fcoe_interface_create(netdev, fip_mode);
2110 lport = fcoe_if_create(fcoe, &netdev->dev, 0);
2111 if (IS_ERR(lport)) {
2112 printk(KERN_ERR "fcoe: Failed to create interface (%s)\n",
2116 fcoe_interface_cleanup(fcoe);
2120 /* Make this the "master" N_Port */
2121 fcoe->ctlr.lp = lport;
2123 /* setup DCB priority attributes. */
2124 fcoe_dcb_create(fcoe);
2126 /* add to lports list */
2127 fcoe_hostlist_add(lport);
2129 /* start FIP Discovery and FLOGI */
2130 lport->boot_time = jiffies;
2131 fc_fabric_login(lport);
2132 if (!fcoe_link_ok(lport))
2133 fcoe_ctlr_link_up(&fcoe->ctlr);
2138 mutex_unlock(&fcoe_config_mutex);
2143 * fcoe_link_speed_update() - Update the supported and actual link speeds
2144 * @lport: The local port to update speeds for
2146 * Returns: 0 if the ethtool query was successful
2147 * -1 if the ethtool query failed
2149 int fcoe_link_speed_update(struct fc_lport *lport)
2151 struct net_device *netdev = fcoe_netdev(lport);
2152 struct ethtool_cmd ecmd;
2154 if (!__ethtool_get_settings(netdev, &ecmd)) {
2155 lport->link_supported_speeds &=
2156 ~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
2157 if (ecmd.supported & (SUPPORTED_1000baseT_Half |
2158 SUPPORTED_1000baseT_Full))
2159 lport->link_supported_speeds |= FC_PORTSPEED_1GBIT;
2160 if (ecmd.supported & SUPPORTED_10000baseT_Full)
2161 lport->link_supported_speeds |=
2162 FC_PORTSPEED_10GBIT;
2163 switch (ethtool_cmd_speed(&ecmd)) {
2165 lport->link_speed = FC_PORTSPEED_1GBIT;
2168 lport->link_speed = FC_PORTSPEED_10GBIT;
2177 * fcoe_link_ok() - Check if the link is OK for a local port
2178 * @lport: The local port to check link on
2180 * Returns: 0 if link is UP and OK, -1 if not
2183 int fcoe_link_ok(struct fc_lport *lport)
2185 struct net_device *netdev = fcoe_netdev(lport);
2187 if (netif_oper_up(netdev))
2193 * fcoe_percpu_clean() - Clear all pending skbs for an local port
2194 * @lport: The local port whose skbs are to be cleared
2196 * Must be called with fcoe_create_mutex held to single-thread completion.
2198 * This flushes the pending skbs by adding a new skb to each queue and
2199 * waiting until they are all freed. This assures us that not only are
2200 * there no packets that will be handled by the lport, but also that any
2201 * threads already handling packet have returned.
2203 void fcoe_percpu_clean(struct fc_lport *lport)
2205 struct fcoe_percpu_s *pp;
2206 struct fcoe_rcv_info *fr;
2207 struct sk_buff_head *list;
2208 struct sk_buff *skb, *next;
2209 struct sk_buff *head;
2212 for_each_possible_cpu(cpu) {
2213 pp = &per_cpu(fcoe_percpu, cpu);
2214 spin_lock_bh(&pp->fcoe_rx_list.lock);
2215 list = &pp->fcoe_rx_list;
2217 for (skb = head; skb != (struct sk_buff *)list;
2220 fr = fcoe_dev_from_skb(skb);
2221 if (fr->fr_dev == lport) {
2222 __skb_unlink(skb, list);
2227 if (!pp->thread || !cpu_online(cpu)) {
2228 spin_unlock_bh(&pp->fcoe_rx_list.lock);
2232 skb = dev_alloc_skb(0);
2234 spin_unlock_bh(&pp->fcoe_rx_list.lock);
2237 skb->destructor = fcoe_percpu_flush_done;
2239 __skb_queue_tail(&pp->fcoe_rx_list, skb);
2240 if (pp->fcoe_rx_list.qlen == 1)
2241 wake_up_process(pp->thread);
2242 spin_unlock_bh(&pp->fcoe_rx_list.lock);
2244 wait_for_completion(&fcoe_flush_completion);
2249 * fcoe_reset() - Reset a local port
2250 * @shost: The SCSI host associated with the local port to be reset
2252 * Returns: Always 0 (return value required by FC transport template)
2254 int fcoe_reset(struct Scsi_Host *shost)
2256 struct fc_lport *lport = shost_priv(shost);
2257 struct fcoe_port *port = lport_priv(lport);
2258 struct fcoe_interface *fcoe = port->priv;
2260 fcoe_ctlr_link_down(&fcoe->ctlr);
2261 fcoe_clean_pending_queue(fcoe->ctlr.lp);
2262 if (!fcoe_link_ok(fcoe->ctlr.lp))
2263 fcoe_ctlr_link_up(&fcoe->ctlr);
2268 * fcoe_hostlist_lookup_port() - Find the FCoE interface associated with a net device
2269 * @netdev: The net device used as a key
2271 * Locking: Must be called with the RNL mutex held.
2273 * Returns: NULL or the FCoE interface
2275 static struct fcoe_interface *
2276 fcoe_hostlist_lookup_port(const struct net_device *netdev)
2278 struct fcoe_interface *fcoe;
2280 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
2281 if (fcoe->netdev == netdev)
2288 * fcoe_hostlist_lookup() - Find the local port associated with a
2290 * @netdev: The netdevice used as a key
2292 * Locking: Must be called with the RTNL mutex held
2294 * Returns: NULL or the local port
2296 static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *netdev)
2298 struct fcoe_interface *fcoe;
2300 fcoe = fcoe_hostlist_lookup_port(netdev);
2301 return (fcoe) ? fcoe->ctlr.lp : NULL;
2305 * fcoe_hostlist_add() - Add the FCoE interface identified by a local
2306 * port to the hostlist
2307 * @lport: The local port that identifies the FCoE interface to be added
2309 * Locking: must be called with the RTNL mutex held
2311 * Returns: 0 for success
2313 static int fcoe_hostlist_add(const struct fc_lport *lport)
2315 struct fcoe_interface *fcoe;
2316 struct fcoe_port *port;
2318 fcoe = fcoe_hostlist_lookup_port(fcoe_netdev(lport));
2320 port = lport_priv(lport);
2322 list_add_tail(&fcoe->list, &fcoe_hostlist);
2328 static struct fcoe_transport fcoe_sw_transport = {
2329 .name = {FCOE_TRANSPORT_DEFAULT},
2331 .list = LIST_HEAD_INIT(fcoe_sw_transport.list),
2332 .match = fcoe_match,
2333 .create = fcoe_create,
2334 .destroy = fcoe_destroy,
2335 .enable = fcoe_enable,
2336 .disable = fcoe_disable,
2340 * fcoe_init() - Initialize fcoe.ko
2342 * Returns: 0 on success, or a negative value on failure
2344 static int __init fcoe_init(void)
2346 struct fcoe_percpu_s *p;
2350 fcoe_wq = alloc_workqueue("fcoe", 0, 0);
2354 /* register as a fcoe transport */
2355 rc = fcoe_transport_attach(&fcoe_sw_transport);
2357 printk(KERN_ERR "failed to register an fcoe transport, check "
2358 "if libfcoe is loaded\n");
2362 mutex_lock(&fcoe_config_mutex);
2364 for_each_possible_cpu(cpu) {
2365 p = &per_cpu(fcoe_percpu, cpu);
2366 skb_queue_head_init(&p->fcoe_rx_list);
2369 for_each_online_cpu(cpu)
2370 fcoe_percpu_thread_create(cpu);
2372 /* Initialize per CPU interrupt thread */
2373 rc = register_hotcpu_notifier(&fcoe_cpu_notifier);
2377 /* Setup link change notification */
2380 rc = fcoe_if_init();
2384 mutex_unlock(&fcoe_config_mutex);
2388 for_each_online_cpu(cpu) {
2389 fcoe_percpu_thread_destroy(cpu);
2391 mutex_unlock(&fcoe_config_mutex);
2392 destroy_workqueue(fcoe_wq);
2395 module_init(fcoe_init);
2398 * fcoe_exit() - Clean up fcoe.ko
2400 * Returns: 0 on success or a negative value on failure
2402 static void __exit fcoe_exit(void)
2404 struct fcoe_interface *fcoe, *tmp;
2405 struct fcoe_port *port;
2408 mutex_lock(&fcoe_config_mutex);
2412 /* releases the associated fcoe hosts */
2414 list_for_each_entry_safe(fcoe, tmp, &fcoe_hostlist, list) {
2415 list_del(&fcoe->list);
2416 port = lport_priv(fcoe->ctlr.lp);
2417 queue_work(fcoe_wq, &port->destroy_work);
2421 unregister_hotcpu_notifier(&fcoe_cpu_notifier);
2423 for_each_online_cpu(cpu)
2424 fcoe_percpu_thread_destroy(cpu);
2426 mutex_unlock(&fcoe_config_mutex);
2429 * destroy_work's may be chained but destroy_workqueue()
2430 * can take care of them. Just kill the fcoe_wq.
2432 destroy_workqueue(fcoe_wq);
2435 * Detaching from the scsi transport must happen after all
2436 * destroys are done on the fcoe_wq. destroy_workqueue will
2437 * enusre the fcoe_wq is flushed.
2441 /* detach from fcoe transport */
2442 fcoe_transport_detach(&fcoe_sw_transport);
2444 module_exit(fcoe_exit);
2447 * fcoe_flogi_resp() - FCoE specific FLOGI and FDISC response handler
2448 * @seq: active sequence in the FLOGI or FDISC exchange
2449 * @fp: response frame, or error encoded in a pointer (timeout)
2450 * @arg: pointer the the fcoe_ctlr structure
2452 * This handles MAC address management for FCoE, then passes control on to
2453 * the libfc FLOGI response handler.
2455 static void fcoe_flogi_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
2457 struct fcoe_ctlr *fip = arg;
2458 struct fc_exch *exch = fc_seq_exch(seq);
2459 struct fc_lport *lport = exch->lp;
2465 mac = fr_cb(fp)->granted_mac;
2467 if (is_zero_ether_addr(mac))
2468 fcoe_ctlr_recv_flogi(fip, lport, fp);
2469 if (!is_zero_ether_addr(mac))
2470 fcoe_update_src_mac(lport, mac);
2472 fc_lport_flogi_resp(seq, fp, lport);
2476 * fcoe_logo_resp() - FCoE specific LOGO response handler
2477 * @seq: active sequence in the LOGO exchange
2478 * @fp: response frame, or error encoded in a pointer (timeout)
2479 * @arg: pointer the the fcoe_ctlr structure
2481 * This handles MAC address management for FCoE, then passes control on to
2482 * the libfc LOGO response handler.
2484 static void fcoe_logo_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
2486 struct fc_lport *lport = arg;
2487 static u8 zero_mac[ETH_ALEN] = { 0 };
2490 fcoe_update_src_mac(lport, zero_mac);
2491 fc_lport_logo_resp(seq, fp, lport);
2495 * fcoe_elsct_send - FCoE specific ELS handler
2497 * This does special case handling of FIP encapsualted ELS exchanges for FCoE,
2498 * using FCoE specific response handlers and passing the FIP controller as
2499 * the argument (the lport is still available from the exchange).
2501 * Most of the work here is just handed off to the libfc routine.
2503 static struct fc_seq *fcoe_elsct_send(struct fc_lport *lport, u32 did,
2504 struct fc_frame *fp, unsigned int op,
2505 void (*resp)(struct fc_seq *,
2508 void *arg, u32 timeout)
2510 struct fcoe_port *port = lport_priv(lport);
2511 struct fcoe_interface *fcoe = port->priv;
2512 struct fcoe_ctlr *fip = &fcoe->ctlr;
2513 struct fc_frame_header *fh = fc_frame_header_get(fp);
2518 if (lport->point_to_multipoint)
2520 return fc_elsct_send(lport, did, fp, op, fcoe_flogi_resp,
2523 /* only hook onto fabric logouts, not port logouts */
2524 if (ntoh24(fh->fh_d_id) != FC_FID_FLOGI)
2526 return fc_elsct_send(lport, did, fp, op, fcoe_logo_resp,
2529 return fc_elsct_send(lport, did, fp, op, resp, arg, timeout);
2533 * fcoe_vport_create() - create an fc_host/scsi_host for a vport
2534 * @vport: fc_vport object to create a new fc_host for
2535 * @disabled: start the new fc_host in a disabled state by default?
2537 * Returns: 0 for success
2539 static int fcoe_vport_create(struct fc_vport *vport, bool disabled)
2541 struct Scsi_Host *shost = vport_to_shost(vport);
2542 struct fc_lport *n_port = shost_priv(shost);
2543 struct fcoe_port *port = lport_priv(n_port);
2544 struct fcoe_interface *fcoe = port->priv;
2545 struct net_device *netdev = fcoe->netdev;
2546 struct fc_lport *vn_port;
2550 rc = fcoe_validate_vport_create(vport);
2552 fcoe_wwn_to_str(vport->port_name, buf, sizeof(buf));
2553 printk(KERN_ERR "fcoe: Failed to create vport, "
2554 "WWPN (0x%s) already exists\n",
2559 mutex_lock(&fcoe_config_mutex);
2561 vn_port = fcoe_if_create(fcoe, &vport->dev, 1);
2563 mutex_unlock(&fcoe_config_mutex);
2565 if (IS_ERR(vn_port)) {
2566 printk(KERN_ERR "fcoe: fcoe_vport_create(%s) failed\n",
2572 fc_vport_set_state(vport, FC_VPORT_DISABLED);
2574 vn_port->boot_time = jiffies;
2575 fc_fabric_login(vn_port);
2576 fc_vport_setlink(vn_port);
2582 * fcoe_vport_destroy() - destroy the fc_host/scsi_host for a vport
2583 * @vport: fc_vport object that is being destroyed
2585 * Returns: 0 for success
2587 static int fcoe_vport_destroy(struct fc_vport *vport)
2589 struct Scsi_Host *shost = vport_to_shost(vport);
2590 struct fc_lport *n_port = shost_priv(shost);
2591 struct fc_lport *vn_port = vport->dd_data;
2592 struct fcoe_port *port = lport_priv(vn_port);
2594 mutex_lock(&n_port->lp_mutex);
2595 list_del(&vn_port->list);
2596 mutex_unlock(&n_port->lp_mutex);
2597 queue_work(fcoe_wq, &port->destroy_work);
2602 * fcoe_vport_disable() - change vport state
2603 * @vport: vport to bring online/offline
2604 * @disable: should the vport be disabled?
2606 static int fcoe_vport_disable(struct fc_vport *vport, bool disable)
2608 struct fc_lport *lport = vport->dd_data;
2611 fc_vport_set_state(vport, FC_VPORT_DISABLED);
2612 fc_fabric_logoff(lport);
2614 lport->boot_time = jiffies;
2615 fc_fabric_login(lport);
2616 fc_vport_setlink(lport);
2623 * fcoe_vport_set_symbolic_name() - append vport string to symbolic name
2624 * @vport: fc_vport with a new symbolic name string
2626 * After generating a new symbolic name string, a new RSPN_ID request is
2627 * sent to the name server. There is no response handler, so if it fails
2628 * for some reason it will not be retried.
2630 static void fcoe_set_vport_symbolic_name(struct fc_vport *vport)
2632 struct fc_lport *lport = vport->dd_data;
2633 struct fc_frame *fp;
2636 snprintf(fc_host_symbolic_name(lport->host), FC_SYMBOLIC_NAME_SIZE,
2637 "%s v%s over %s : %s", FCOE_NAME, FCOE_VERSION,
2638 fcoe_netdev(lport)->name, vport->symbolic_name);
2640 if (lport->state != LPORT_ST_READY)
2643 len = strnlen(fc_host_symbolic_name(lport->host), 255);
2644 fp = fc_frame_alloc(lport,
2645 sizeof(struct fc_ct_hdr) +
2646 sizeof(struct fc_ns_rspn) + len);
2649 lport->tt.elsct_send(lport, FC_FID_DIR_SERV, fp, FC_NS_RSPN_ID,
2650 NULL, NULL, 3 * lport->r_a_tov);
2654 * fcoe_get_lesb() - Fill the FCoE Link Error Status Block
2655 * @lport: the local port
2656 * @fc_lesb: the link error status block
2658 static void fcoe_get_lesb(struct fc_lport *lport,
2659 struct fc_els_lesb *fc_lesb)
2661 struct net_device *netdev = fcoe_netdev(lport);
2663 __fcoe_get_lesb(lport, fc_lesb, netdev);
2667 * fcoe_set_port_id() - Callback from libfc when Port_ID is set.
2668 * @lport: the local port
2669 * @port_id: the port ID
2670 * @fp: the received frame, if any, that caused the port_id to be set.
2672 * This routine handles the case where we received a FLOGI and are
2673 * entering point-to-point mode. We need to call fcoe_ctlr_recv_flogi()
2674 * so it can set the non-mapped mode and gateway address.
2676 * The FLOGI LS_ACC is handled by fcoe_flogi_resp().
2678 static void fcoe_set_port_id(struct fc_lport *lport,
2679 u32 port_id, struct fc_frame *fp)
2681 struct fcoe_port *port = lport_priv(lport);
2682 struct fcoe_interface *fcoe = port->priv;
2684 if (fp && fc_frame_payload_op(fp) == ELS_FLOGI)
2685 fcoe_ctlr_recv_flogi(&fcoe->ctlr, lport, fp);