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/version.h>
22 #include <linux/spinlock.h>
23 #include <linux/netdevice.h>
24 #include <linux/etherdevice.h>
25 #include <linux/ethtool.h>
26 #include <linux/if_ether.h>
27 #include <linux/if_vlan.h>
28 #include <linux/crc32.h>
29 #include <linux/slab.h>
30 #include <linux/cpu.h>
32 #include <linux/sysfs.h>
33 #include <linux/ctype.h>
34 #include <linux/workqueue.h>
35 #include <scsi/scsi_tcq.h>
36 #include <scsi/scsicam.h>
37 #include <scsi/scsi_transport.h>
38 #include <scsi/scsi_transport_fc.h>
39 #include <net/rtnetlink.h>
41 #include <scsi/fc/fc_encaps.h>
42 #include <scsi/fc/fc_fip.h>
44 #include <scsi/libfc.h>
45 #include <scsi/fc_frame.h>
46 #include <scsi/libfcoe.h>
50 MODULE_AUTHOR("Open-FCoE.org");
51 MODULE_DESCRIPTION("FCoE");
52 MODULE_LICENSE("GPL v2");
54 /* Performance tuning parameters for fcoe */
55 static unsigned int fcoe_ddp_min;
56 module_param_named(ddp_min, fcoe_ddp_min, uint, S_IRUGO | S_IWUSR);
57 MODULE_PARM_DESC(ddp_min, "Minimum I/O size in bytes for " \
58 "Direct Data Placement (DDP).");
60 DEFINE_MUTEX(fcoe_config_mutex);
62 static struct workqueue_struct *fcoe_wq;
64 /* fcoe_percpu_clean completion. Waiter protected by fcoe_create_mutex */
65 static DECLARE_COMPLETION(fcoe_flush_completion);
68 /* must only by accessed under the RTNL mutex */
69 LIST_HEAD(fcoe_hostlist);
70 DEFINE_PER_CPU(struct fcoe_percpu_s, fcoe_percpu);
72 /* Function Prototypes */
73 static int fcoe_reset(struct Scsi_Host *);
74 static int fcoe_xmit(struct fc_lport *, struct fc_frame *);
75 static int fcoe_rcv(struct sk_buff *, struct net_device *,
76 struct packet_type *, struct net_device *);
77 static int fcoe_percpu_receive_thread(void *);
78 static void fcoe_percpu_clean(struct fc_lport *);
79 static int fcoe_link_speed_update(struct fc_lport *);
80 static int fcoe_link_ok(struct fc_lport *);
82 static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *);
83 static int fcoe_hostlist_add(const struct fc_lport *);
85 static int fcoe_device_notification(struct notifier_block *, ulong, void *);
86 static void fcoe_dev_setup(void);
87 static void fcoe_dev_cleanup(void);
88 static struct fcoe_interface
89 *fcoe_hostlist_lookup_port(const struct net_device *);
91 static int fcoe_fip_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);
103 static int fcoe_cpu_callback(struct notifier_block *, unsigned long, void *);
105 static bool fcoe_match(struct net_device *netdev);
106 static int fcoe_create(struct net_device *netdev, enum fip_state fip_mode);
107 static int fcoe_destroy(struct net_device *netdev);
108 static int fcoe_enable(struct net_device *netdev);
109 static int fcoe_disable(struct net_device *netdev);
111 static struct fc_seq *fcoe_elsct_send(struct fc_lport *,
112 u32 did, struct fc_frame *,
114 void (*resp)(struct fc_seq *,
117 void *, u32 timeout);
118 static void fcoe_recv_frame(struct sk_buff *skb);
120 static void fcoe_get_lesb(struct fc_lport *, struct fc_els_lesb *);
122 /* notification function for packets from net device */
123 static struct notifier_block fcoe_notifier = {
124 .notifier_call = fcoe_device_notification,
127 /* notification function for CPU hotplug events */
128 static struct notifier_block fcoe_cpu_notifier = {
129 .notifier_call = fcoe_cpu_callback,
132 static struct scsi_transport_template *fcoe_nport_scsi_transport;
133 static struct scsi_transport_template *fcoe_vport_scsi_transport;
135 static int fcoe_vport_destroy(struct fc_vport *);
136 static int fcoe_vport_create(struct fc_vport *, bool disabled);
137 static int fcoe_vport_disable(struct fc_vport *, bool disable);
138 static void fcoe_set_vport_symbolic_name(struct fc_vport *);
139 static void fcoe_set_port_id(struct fc_lport *, u32, struct fc_frame *);
141 static struct libfc_function_template fcoe_libfc_fcn_templ = {
142 .frame_send = fcoe_xmit,
143 .ddp_setup = fcoe_ddp_setup,
144 .ddp_done = fcoe_ddp_done,
145 .elsct_send = fcoe_elsct_send,
146 .get_lesb = fcoe_get_lesb,
147 .lport_set_port_id = fcoe_set_port_id,
150 struct fc_function_template fcoe_nport_fc_functions = {
151 .show_host_node_name = 1,
152 .show_host_port_name = 1,
153 .show_host_supported_classes = 1,
154 .show_host_supported_fc4s = 1,
155 .show_host_active_fc4s = 1,
156 .show_host_maxframe_size = 1,
158 .show_host_port_id = 1,
159 .show_host_supported_speeds = 1,
160 .get_host_speed = fc_get_host_speed,
161 .show_host_speed = 1,
162 .show_host_port_type = 1,
163 .get_host_port_state = fc_get_host_port_state,
164 .show_host_port_state = 1,
165 .show_host_symbolic_name = 1,
167 .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
168 .show_rport_maxframe_size = 1,
169 .show_rport_supported_classes = 1,
171 .show_host_fabric_name = 1,
172 .show_starget_node_name = 1,
173 .show_starget_port_name = 1,
174 .show_starget_port_id = 1,
175 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
176 .show_rport_dev_loss_tmo = 1,
177 .get_fc_host_stats = fc_get_host_stats,
178 .issue_fc_host_lip = fcoe_reset,
180 .terminate_rport_io = fc_rport_terminate_io,
182 .vport_create = fcoe_vport_create,
183 .vport_delete = fcoe_vport_destroy,
184 .vport_disable = fcoe_vport_disable,
185 .set_vport_symbolic_name = fcoe_set_vport_symbolic_name,
187 .bsg_request = fc_lport_bsg_request,
190 struct fc_function_template fcoe_vport_fc_functions = {
191 .show_host_node_name = 1,
192 .show_host_port_name = 1,
193 .show_host_supported_classes = 1,
194 .show_host_supported_fc4s = 1,
195 .show_host_active_fc4s = 1,
196 .show_host_maxframe_size = 1,
198 .show_host_port_id = 1,
199 .show_host_supported_speeds = 1,
200 .get_host_speed = fc_get_host_speed,
201 .show_host_speed = 1,
202 .show_host_port_type = 1,
203 .get_host_port_state = fc_get_host_port_state,
204 .show_host_port_state = 1,
205 .show_host_symbolic_name = 1,
207 .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
208 .show_rport_maxframe_size = 1,
209 .show_rport_supported_classes = 1,
211 .show_host_fabric_name = 1,
212 .show_starget_node_name = 1,
213 .show_starget_port_name = 1,
214 .show_starget_port_id = 1,
215 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
216 .show_rport_dev_loss_tmo = 1,
217 .get_fc_host_stats = fc_get_host_stats,
218 .issue_fc_host_lip = fcoe_reset,
220 .terminate_rport_io = fc_rport_terminate_io,
222 .bsg_request = fc_lport_bsg_request,
225 static struct scsi_host_template fcoe_shost_template = {
226 .module = THIS_MODULE,
227 .name = "FCoE Driver",
228 .proc_name = FCOE_NAME,
229 .queuecommand = fc_queuecommand,
230 .eh_abort_handler = fc_eh_abort,
231 .eh_device_reset_handler = fc_eh_device_reset,
232 .eh_host_reset_handler = fc_eh_host_reset,
233 .slave_alloc = fc_slave_alloc,
234 .change_queue_depth = fc_change_queue_depth,
235 .change_queue_type = fc_change_queue_type,
238 .can_queue = FCOE_MAX_OUTSTANDING_COMMANDS,
239 .use_clustering = ENABLE_CLUSTERING,
240 .sg_tablesize = SG_ALL,
241 .max_sectors = 0xffff,
245 * fcoe_interface_setup() - Setup a FCoE interface
246 * @fcoe: The new FCoE interface
247 * @netdev: The net device that the fcoe interface is on
249 * Returns : 0 for success
250 * Locking: must be called with the RTNL mutex held
252 static int fcoe_interface_setup(struct fcoe_interface *fcoe,
253 struct net_device *netdev)
255 struct fcoe_ctlr *fip = &fcoe->ctlr;
256 struct netdev_hw_addr *ha;
257 struct net_device *real_dev;
258 u8 flogi_maddr[ETH_ALEN];
259 const struct net_device_ops *ops;
261 fcoe->netdev = netdev;
263 /* Let LLD initialize for FCoE */
264 ops = netdev->netdev_ops;
265 if (ops->ndo_fcoe_enable) {
266 if (ops->ndo_fcoe_enable(netdev))
267 FCOE_NETDEV_DBG(netdev, "Failed to enable FCoE"
268 " specific feature for LLD.\n");
271 /* Do not support for bonding device */
272 if (netdev->priv_flags & IFF_BONDING && netdev->flags & IFF_MASTER) {
273 FCOE_NETDEV_DBG(netdev, "Bonded interfaces not supported\n");
277 /* look for SAN MAC address, if multiple SAN MACs exist, only
278 * use the first one for SPMA */
279 real_dev = (netdev->priv_flags & IFF_802_1Q_VLAN) ?
280 vlan_dev_real_dev(netdev) : netdev;
282 for_each_dev_addr(real_dev, ha) {
283 if ((ha->type == NETDEV_HW_ADDR_T_SAN) &&
284 (is_valid_ether_addr(ha->addr))) {
285 memcpy(fip->ctl_src_addr, ha->addr, ETH_ALEN);
292 /* setup Source Mac Address */
294 memcpy(fip->ctl_src_addr, netdev->dev_addr, netdev->addr_len);
297 * Add FCoE MAC address as second unicast MAC address
298 * or enter promiscuous mode if not capable of listening
299 * for multiple unicast MACs.
301 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
302 dev_uc_add(netdev, flogi_maddr);
304 dev_uc_add(netdev, fip->ctl_src_addr);
305 if (fip->mode == FIP_MODE_VN2VN) {
306 dev_mc_add(netdev, FIP_ALL_VN2VN_MACS);
307 dev_mc_add(netdev, FIP_ALL_P2P_MACS);
309 dev_mc_add(netdev, FIP_ALL_ENODE_MACS);
312 * setup the receive function from ethernet driver
313 * on the ethertype for the given device
315 fcoe->fcoe_packet_type.func = fcoe_rcv;
316 fcoe->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
317 fcoe->fcoe_packet_type.dev = netdev;
318 dev_add_pack(&fcoe->fcoe_packet_type);
320 fcoe->fip_packet_type.func = fcoe_fip_recv;
321 fcoe->fip_packet_type.type = htons(ETH_P_FIP);
322 fcoe->fip_packet_type.dev = netdev;
323 dev_add_pack(&fcoe->fip_packet_type);
329 * fcoe_interface_create() - Create a FCoE interface on a net device
330 * @netdev: The net device to create the FCoE interface on
331 * @fip_mode: The mode to use for FIP
333 * Returns: pointer to a struct fcoe_interface or NULL on error
335 static struct fcoe_interface *fcoe_interface_create(struct net_device *netdev,
336 enum fip_state fip_mode)
338 struct fcoe_interface *fcoe;
341 if (!try_module_get(THIS_MODULE)) {
342 FCOE_NETDEV_DBG(netdev,
343 "Could not get a reference to the module\n");
344 fcoe = ERR_PTR(-EBUSY);
348 fcoe = kzalloc(sizeof(*fcoe), GFP_KERNEL);
350 FCOE_NETDEV_DBG(netdev, "Could not allocate fcoe structure\n");
351 fcoe = ERR_PTR(-ENOMEM);
356 kref_init(&fcoe->kref);
361 fcoe_ctlr_init(&fcoe->ctlr, fip_mode);
362 fcoe->ctlr.send = fcoe_fip_send;
363 fcoe->ctlr.update_mac = fcoe_update_src_mac;
364 fcoe->ctlr.get_src_addr = fcoe_get_src_mac;
366 err = fcoe_interface_setup(fcoe, netdev);
368 fcoe_ctlr_destroy(&fcoe->ctlr);
378 module_put(THIS_MODULE);
384 * fcoe_interface_cleanup() - Clean up a FCoE interface
385 * @fcoe: The FCoE interface to be cleaned up
387 * Caller must be holding the RTNL mutex
389 void fcoe_interface_cleanup(struct fcoe_interface *fcoe)
391 struct net_device *netdev = fcoe->netdev;
392 struct fcoe_ctlr *fip = &fcoe->ctlr;
393 u8 flogi_maddr[ETH_ALEN];
394 const struct net_device_ops *ops;
397 * Don't listen for Ethernet packets anymore.
398 * synchronize_net() ensures that the packet handlers are not running
399 * on another CPU. dev_remove_pack() would do that, this calls the
400 * unsyncronized version __dev_remove_pack() to avoid multiple delays.
402 __dev_remove_pack(&fcoe->fcoe_packet_type);
403 __dev_remove_pack(&fcoe->fip_packet_type);
406 /* Delete secondary MAC addresses */
407 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
408 dev_uc_del(netdev, flogi_maddr);
410 dev_uc_del(netdev, fip->ctl_src_addr);
411 if (fip->mode == FIP_MODE_VN2VN) {
412 dev_mc_del(netdev, FIP_ALL_VN2VN_MACS);
413 dev_mc_del(netdev, FIP_ALL_P2P_MACS);
415 dev_mc_del(netdev, FIP_ALL_ENODE_MACS);
417 /* Tell the LLD we are done w/ FCoE */
418 ops = netdev->netdev_ops;
419 if (ops->ndo_fcoe_disable) {
420 if (ops->ndo_fcoe_disable(netdev))
421 FCOE_NETDEV_DBG(netdev, "Failed to disable FCoE"
422 " specific feature for LLD.\n");
427 * fcoe_interface_release() - fcoe_port kref release function
428 * @kref: Embedded reference count in an fcoe_interface struct
430 static void fcoe_interface_release(struct kref *kref)
432 struct fcoe_interface *fcoe;
433 struct net_device *netdev;
435 fcoe = container_of(kref, struct fcoe_interface, kref);
436 netdev = fcoe->netdev;
437 /* tear-down the FCoE controller */
438 fcoe_ctlr_destroy(&fcoe->ctlr);
441 module_put(THIS_MODULE);
445 * fcoe_interface_get() - Get a reference to a FCoE interface
446 * @fcoe: The FCoE interface to be held
448 static inline void fcoe_interface_get(struct fcoe_interface *fcoe)
450 kref_get(&fcoe->kref);
454 * fcoe_interface_put() - Put a reference to a FCoE interface
455 * @fcoe: The FCoE interface to be released
457 static inline void fcoe_interface_put(struct fcoe_interface *fcoe)
459 kref_put(&fcoe->kref, fcoe_interface_release);
463 * fcoe_fip_recv() - Handler for received FIP frames
464 * @skb: The receive skb
465 * @netdev: The associated net device
466 * @ptype: The packet_type structure which was used to register this handler
467 * @orig_dev: The original net_device the the skb was received on.
468 * (in case dev is a bond)
470 * Returns: 0 for success
472 static int fcoe_fip_recv(struct sk_buff *skb, struct net_device *netdev,
473 struct packet_type *ptype,
474 struct net_device *orig_dev)
476 struct fcoe_interface *fcoe;
478 fcoe = container_of(ptype, struct fcoe_interface, fip_packet_type);
479 fcoe_ctlr_recv(&fcoe->ctlr, skb);
484 * fcoe_fip_send() - Send an Ethernet-encapsulated FIP frame
485 * @fip: The FCoE controller
486 * @skb: The FIP packet to be sent
488 static void fcoe_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
490 skb->dev = fcoe_from_ctlr(fip)->netdev;
495 * fcoe_update_src_mac() - Update the Ethernet MAC filters
496 * @lport: The local port to update the source MAC on
497 * @addr: Unicast MAC address to add
499 * Remove any previously-set unicast MAC filter.
500 * Add secondary FCoE MAC address filter for our OUI.
502 static void fcoe_update_src_mac(struct fc_lport *lport, u8 *addr)
504 struct fcoe_port *port = lport_priv(lport);
505 struct fcoe_interface *fcoe = port->priv;
508 if (!is_zero_ether_addr(port->data_src_addr))
509 dev_uc_del(fcoe->netdev, port->data_src_addr);
510 if (!is_zero_ether_addr(addr))
511 dev_uc_add(fcoe->netdev, addr);
512 memcpy(port->data_src_addr, addr, ETH_ALEN);
517 * fcoe_get_src_mac() - return the Ethernet source address for an lport
518 * @lport: libfc lport
520 static u8 *fcoe_get_src_mac(struct fc_lport *lport)
522 struct fcoe_port *port = lport_priv(lport);
524 return port->data_src_addr;
528 * fcoe_lport_config() - Set up a local port
529 * @lport: The local port to be setup
531 * Returns: 0 for success
533 static int fcoe_lport_config(struct fc_lport *lport)
537 lport->max_retry_count = 3;
538 lport->max_rport_retry_count = 3;
539 lport->e_d_tov = 2 * 1000; /* FC-FS default */
540 lport->r_a_tov = 2 * 2 * 1000;
541 lport->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
542 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
543 lport->does_npiv = 1;
545 fc_lport_init_stats(lport);
547 /* lport fc_lport related configuration */
548 fc_lport_config(lport);
550 /* offload related configuration */
551 lport->crc_offload = 0;
552 lport->seq_offload = 0;
553 lport->lro_enabled = 0;
561 * fcoe_get_wwn() - Get the world wide name from LLD if it supports it
562 * @netdev: the associated net device
563 * @wwn: the output WWN
564 * @type: the type of WWN (WWPN or WWNN)
566 * Returns: 0 for success
568 static int fcoe_get_wwn(struct net_device *netdev, u64 *wwn, int type)
570 const struct net_device_ops *ops = netdev->netdev_ops;
572 if (ops->ndo_fcoe_get_wwn)
573 return ops->ndo_fcoe_get_wwn(netdev, wwn, type);
578 * fcoe_netdev_features_change - Updates the lport's offload flags based
579 * on the LLD netdev's FCoE feature flags
581 static void fcoe_netdev_features_change(struct fc_lport *lport,
582 struct net_device *netdev)
584 mutex_lock(&lport->lp_mutex);
586 if (netdev->features & NETIF_F_SG)
591 if (netdev->features & NETIF_F_FCOE_CRC) {
592 lport->crc_offload = 1;
593 FCOE_NETDEV_DBG(netdev, "Supports FCCRC offload\n");
595 lport->crc_offload = 0;
598 if (netdev->features & NETIF_F_FSO) {
599 lport->seq_offload = 1;
600 lport->lso_max = netdev->gso_max_size;
601 FCOE_NETDEV_DBG(netdev, "Supports LSO for max len 0x%x\n",
604 lport->seq_offload = 0;
608 if (netdev->fcoe_ddp_xid) {
609 lport->lro_enabled = 1;
610 lport->lro_xid = netdev->fcoe_ddp_xid;
611 FCOE_NETDEV_DBG(netdev, "Supports LRO for max xid 0x%x\n",
614 lport->lro_enabled = 0;
618 mutex_unlock(&lport->lp_mutex);
622 * fcoe_netdev_config() - Set up net devive for SW FCoE
623 * @lport: The local port that is associated with the net device
624 * @netdev: The associated net device
626 * Must be called after fcoe_lport_config() as it will use local port mutex
628 * Returns: 0 for success
630 static int fcoe_netdev_config(struct fc_lport *lport, struct net_device *netdev)
634 struct fcoe_interface *fcoe;
635 struct fcoe_port *port;
637 /* Setup lport private data to point to fcoe softc */
638 port = lport_priv(lport);
642 * Determine max frame size based on underlying device and optional
643 * user-configured limit. If the MFS is too low, fcoe_link_ok()
644 * will return 0, so do this first.
647 if (netdev->features & NETIF_F_FCOE_MTU) {
649 FCOE_NETDEV_DBG(netdev, "Supports FCOE_MTU of %d bytes\n", mfs);
651 mfs -= (sizeof(struct fcoe_hdr) + sizeof(struct fcoe_crc_eof));
652 if (fc_set_mfs(lport, mfs))
655 /* offload features support */
656 fcoe_netdev_features_change(lport, netdev);
658 skb_queue_head_init(&port->fcoe_pending_queue);
659 port->fcoe_pending_queue_active = 0;
660 setup_timer(&port->timer, fcoe_queue_timer, (unsigned long)lport);
662 fcoe_link_speed_update(lport);
665 if (fcoe_get_wwn(netdev, &wwnn, NETDEV_FCOE_WWNN))
666 wwnn = fcoe_wwn_from_mac(fcoe->ctlr.ctl_src_addr, 1, 0);
667 fc_set_wwnn(lport, wwnn);
668 if (fcoe_get_wwn(netdev, &wwpn, NETDEV_FCOE_WWPN))
669 wwpn = fcoe_wwn_from_mac(fcoe->ctlr.ctl_src_addr,
671 fc_set_wwpn(lport, wwpn);
678 * fcoe_shost_config() - Set up the SCSI host associated with a local port
679 * @lport: The local port
680 * @dev: The device associated with the SCSI host
682 * Must be called after fcoe_lport_config() and fcoe_netdev_config()
684 * Returns: 0 for success
686 static int fcoe_shost_config(struct fc_lport *lport, struct device *dev)
690 /* lport scsi host config */
691 lport->host->max_lun = FCOE_MAX_LUN;
692 lport->host->max_id = FCOE_MAX_FCP_TARGET;
693 lport->host->max_channel = 0;
694 lport->host->max_cmd_len = FCOE_MAX_CMD_LEN;
697 lport->host->transportt = fcoe_vport_scsi_transport;
699 lport->host->transportt = fcoe_nport_scsi_transport;
701 /* add the new host to the SCSI-ml */
702 rc = scsi_add_host(lport->host, dev);
704 FCOE_NETDEV_DBG(fcoe_netdev(lport), "fcoe_shost_config: "
705 "error on scsi_add_host\n");
710 fc_host_max_npiv_vports(lport->host) = USHRT_MAX;
712 snprintf(fc_host_symbolic_name(lport->host), FC_SYMBOLIC_NAME_SIZE,
713 "%s v%s over %s", FCOE_NAME, FCOE_VERSION,
714 fcoe_netdev(lport)->name);
720 * fcoe_oem_match() - The match routine for the offloaded exchange manager
723 * This routine will be associated with an exchange manager (EM). When
724 * the libfc exchange handling code is looking for an EM to use it will
725 * call this routine and pass it the frame that it wishes to send. This
726 * routine will return True if the associated EM is to be used and False
727 * if the echange code should continue looking for an EM.
729 * The offload EM that this routine is associated with will handle any
730 * packets that are for SCSI read requests.
732 * Returns: True for read types I/O, otherwise returns false.
734 bool fcoe_oem_match(struct fc_frame *fp)
736 return fc_fcp_is_read(fr_fsp(fp)) &&
737 (fr_fsp(fp)->data_len > fcoe_ddp_min);
741 * fcoe_em_config() - Allocate and configure an exchange manager
742 * @lport: The local port that the new EM will be associated with
744 * Returns: 0 on success
746 static inline int fcoe_em_config(struct fc_lport *lport)
748 struct fcoe_port *port = lport_priv(lport);
749 struct fcoe_interface *fcoe = port->priv;
750 struct fcoe_interface *oldfcoe = NULL;
751 struct net_device *old_real_dev, *cur_real_dev;
752 u16 min_xid = FCOE_MIN_XID;
753 u16 max_xid = FCOE_MAX_XID;
756 * Check if need to allocate an em instance for
757 * offload exchange ids to be shared across all VN_PORTs/lport.
759 if (!lport->lro_enabled || !lport->lro_xid ||
760 (lport->lro_xid >= max_xid)) {
766 * Reuse existing offload em instance in case
767 * it is already allocated on real eth device
769 if (fcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
770 cur_real_dev = vlan_dev_real_dev(fcoe->netdev);
772 cur_real_dev = fcoe->netdev;
774 list_for_each_entry(oldfcoe, &fcoe_hostlist, list) {
775 if (oldfcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
776 old_real_dev = vlan_dev_real_dev(oldfcoe->netdev);
778 old_real_dev = oldfcoe->netdev;
780 if (cur_real_dev == old_real_dev) {
781 fcoe->oem = oldfcoe->oem;
787 if (!fc_exch_mgr_add(lport, fcoe->oem, fcoe_oem_match)) {
788 printk(KERN_ERR "fcoe_em_config: failed to add "
789 "offload em:%p on interface:%s\n",
790 fcoe->oem, fcoe->netdev->name);
794 fcoe->oem = fc_exch_mgr_alloc(lport, FC_CLASS_3,
795 FCOE_MIN_XID, lport->lro_xid,
798 printk(KERN_ERR "fcoe_em_config: failed to allocate "
799 "em for offload exches on interface:%s\n",
806 * Exclude offload EM xid range from next EM xid range.
808 min_xid += lport->lro_xid + 1;
811 if (!fc_exch_mgr_alloc(lport, FC_CLASS_3, min_xid, max_xid, NULL)) {
812 printk(KERN_ERR "fcoe_em_config: failed to "
813 "allocate em on interface %s\n", fcoe->netdev->name);
821 * fcoe_if_destroy() - Tear down a SW FCoE instance
822 * @lport: The local port to be destroyed
824 * Locking: must be called with the RTNL mutex held and RTNL mutex
825 * needed to be dropped by this function since not dropping RTNL
826 * would cause circular locking warning on synchronous fip worker
827 * cancelling thru fcoe_interface_put invoked by this function.
830 static void fcoe_if_destroy(struct fc_lport *lport)
832 struct fcoe_port *port = lport_priv(lport);
833 struct fcoe_interface *fcoe = port->priv;
834 struct net_device *netdev = fcoe->netdev;
836 FCOE_NETDEV_DBG(netdev, "Destroying interface\n");
838 /* Logout of the fabric */
839 fc_fabric_logoff(lport);
841 /* Cleanup the fc_lport */
842 fc_lport_destroy(lport);
844 /* Stop the transmit retry timer */
845 del_timer_sync(&port->timer);
847 /* Free existing transmit skbs */
848 fcoe_clean_pending_queue(lport);
850 if (!is_zero_ether_addr(port->data_src_addr))
851 dev_uc_del(netdev, port->data_src_addr);
854 /* receives may not be stopped until after this */
855 fcoe_interface_put(fcoe);
857 /* Free queued packets for the per-CPU receive threads */
858 fcoe_percpu_clean(lport);
860 /* Detach from the scsi-ml */
861 fc_remove_host(lport->host);
862 scsi_remove_host(lport->host);
864 /* Destroy lport scsi_priv */
865 fc_fcp_destroy(lport);
867 /* There are no more rports or I/O, free the EM */
868 fc_exch_mgr_free(lport);
870 /* Free memory used by statistical counters */
871 fc_lport_free_stats(lport);
873 /* Release the Scsi_Host */
874 scsi_host_put(lport->host);
878 * fcoe_ddp_setup() - Call a LLD's ddp_setup through the net device
879 * @lport: The local port to setup DDP for
880 * @xid: The exchange ID for this DDP transfer
881 * @sgl: The scatterlist describing this transfer
882 * @sgc: The number of sg items
884 * Returns: 0 if the DDP context was not configured
886 static int fcoe_ddp_setup(struct fc_lport *lport, u16 xid,
887 struct scatterlist *sgl, unsigned int sgc)
889 struct net_device *netdev = fcoe_netdev(lport);
891 if (netdev->netdev_ops->ndo_fcoe_ddp_setup)
892 return netdev->netdev_ops->ndo_fcoe_ddp_setup(netdev,
900 * fcoe_ddp_done() - Call a LLD's ddp_done through the net device
901 * @lport: The local port to complete DDP on
902 * @xid: The exchange ID for this DDP transfer
904 * Returns: the length of data that have been completed by DDP
906 static int fcoe_ddp_done(struct fc_lport *lport, u16 xid)
908 struct net_device *netdev = fcoe_netdev(lport);
910 if (netdev->netdev_ops->ndo_fcoe_ddp_done)
911 return netdev->netdev_ops->ndo_fcoe_ddp_done(netdev, xid);
916 * fcoe_if_create() - Create a FCoE instance on an interface
917 * @fcoe: The FCoE interface to create a local port on
918 * @parent: The device pointer to be the parent in sysfs for the SCSI host
919 * @npiv: Indicates if the port is a vport or not
921 * Creates a fc_lport instance and a Scsi_Host instance and configure them.
923 * Returns: The allocated fc_lport or an error pointer
925 static struct fc_lport *fcoe_if_create(struct fcoe_interface *fcoe,
926 struct device *parent, int npiv)
928 struct net_device *netdev = fcoe->netdev;
929 struct fc_lport *lport, *n_port;
930 struct fcoe_port *port;
931 struct Scsi_Host *shost;
934 * parent is only a vport if npiv is 1,
935 * but we'll only use vport in that case so go ahead and set it
937 struct fc_vport *vport = dev_to_vport(parent);
939 FCOE_NETDEV_DBG(netdev, "Create Interface\n");
942 lport = libfc_host_alloc(&fcoe_shost_template, sizeof(*port));
944 lport = libfc_vport_create(vport, sizeof(*port));
947 FCOE_NETDEV_DBG(netdev, "Could not allocate host structure\n");
951 port = lport_priv(lport);
954 port->max_queue_depth = FCOE_MAX_QUEUE_DEPTH;
955 port->min_queue_depth = FCOE_MIN_QUEUE_DEPTH;
956 INIT_WORK(&port->destroy_work, fcoe_destroy_work);
958 /* configure a fc_lport including the exchange manager */
959 rc = fcoe_lport_config(lport);
961 FCOE_NETDEV_DBG(netdev, "Could not configure lport for the "
967 FCOE_NETDEV_DBG(netdev, "Setting vport names, "
968 "%16.16llx %16.16llx\n",
969 vport->node_name, vport->port_name);
970 fc_set_wwnn(lport, vport->node_name);
971 fc_set_wwpn(lport, vport->port_name);
974 /* configure lport network properties */
975 rc = fcoe_netdev_config(lport, netdev);
977 FCOE_NETDEV_DBG(netdev, "Could not configure netdev for the "
982 /* configure lport scsi host properties */
983 rc = fcoe_shost_config(lport, parent);
985 FCOE_NETDEV_DBG(netdev, "Could not configure shost for the "
990 /* Initialize the library */
991 rc = fcoe_libfc_config(lport, &fcoe->ctlr, &fcoe_libfc_fcn_templ, 1);
993 FCOE_NETDEV_DBG(netdev, "Could not configure libfc for the "
999 * fcoe_em_alloc() and fcoe_hostlist_add() both
1000 * need to be atomic with respect to other changes to the
1001 * hostlist since fcoe_em_alloc() looks for an existing EM
1002 * instance on host list updated by fcoe_hostlist_add().
1004 * This is currently handled through the fcoe_config_mutex
1008 /* lport exch manager allocation */
1009 rc = fcoe_em_config(lport);
1011 shost = vport_to_shost(vport);
1012 n_port = shost_priv(shost);
1013 rc = fc_exch_mgr_list_clone(n_port, lport);
1017 FCOE_NETDEV_DBG(netdev, "Could not configure the EM\n");
1018 goto out_lp_destroy;
1021 fcoe_interface_get(fcoe);
1025 fc_exch_mgr_free(lport);
1027 scsi_host_put(lport->host);
1033 * fcoe_if_init() - Initialization routine for fcoe.ko
1035 * Attaches the SW FCoE transport to the FC transport
1037 * Returns: 0 on success
1039 static int __init fcoe_if_init(void)
1041 /* attach to scsi transport */
1042 fcoe_nport_scsi_transport =
1043 fc_attach_transport(&fcoe_nport_fc_functions);
1044 fcoe_vport_scsi_transport =
1045 fc_attach_transport(&fcoe_vport_fc_functions);
1047 if (!fcoe_nport_scsi_transport) {
1048 printk(KERN_ERR "fcoe: Failed to attach to the FC transport\n");
1056 * fcoe_if_exit() - Tear down fcoe.ko
1058 * Detaches the SW FCoE transport from the FC transport
1060 * Returns: 0 on success
1062 int __exit fcoe_if_exit(void)
1064 fc_release_transport(fcoe_nport_scsi_transport);
1065 fc_release_transport(fcoe_vport_scsi_transport);
1066 fcoe_nport_scsi_transport = NULL;
1067 fcoe_vport_scsi_transport = NULL;
1072 * fcoe_percpu_thread_create() - Create a receive thread for an online CPU
1073 * @cpu: The CPU index of the CPU to create a receive thread for
1075 static void fcoe_percpu_thread_create(unsigned int cpu)
1077 struct fcoe_percpu_s *p;
1078 struct task_struct *thread;
1080 p = &per_cpu(fcoe_percpu, cpu);
1082 thread = kthread_create(fcoe_percpu_receive_thread,
1083 (void *)p, "fcoethread/%d", cpu);
1085 if (likely(!IS_ERR(thread))) {
1086 kthread_bind(thread, cpu);
1087 wake_up_process(thread);
1089 spin_lock_bh(&p->fcoe_rx_list.lock);
1091 spin_unlock_bh(&p->fcoe_rx_list.lock);
1096 * fcoe_percpu_thread_destroy() - Remove the receive thread of a CPU
1097 * @cpu: The CPU index of the CPU whose receive thread is to be destroyed
1099 * Destroys a per-CPU Rx thread. Any pending skbs are moved to the
1100 * current CPU's Rx thread. If the thread being destroyed is bound to
1101 * the CPU processing this context the skbs will be freed.
1103 static void fcoe_percpu_thread_destroy(unsigned int cpu)
1105 struct fcoe_percpu_s *p;
1106 struct task_struct *thread;
1107 struct page *crc_eof;
1108 struct sk_buff *skb;
1110 struct fcoe_percpu_s *p0;
1111 unsigned targ_cpu = get_cpu();
1112 #endif /* CONFIG_SMP */
1114 FCOE_DBG("Destroying receive thread for CPU %d\n", cpu);
1116 /* Prevent any new skbs from being queued for this CPU. */
1117 p = &per_cpu(fcoe_percpu, cpu);
1118 spin_lock_bh(&p->fcoe_rx_list.lock);
1121 crc_eof = p->crc_eof_page;
1122 p->crc_eof_page = NULL;
1123 p->crc_eof_offset = 0;
1124 spin_unlock_bh(&p->fcoe_rx_list.lock);
1128 * Don't bother moving the skb's if this context is running
1129 * on the same CPU that is having its thread destroyed. This
1130 * can easily happen when the module is removed.
1132 if (cpu != targ_cpu) {
1133 p0 = &per_cpu(fcoe_percpu, targ_cpu);
1134 spin_lock_bh(&p0->fcoe_rx_list.lock);
1136 FCOE_DBG("Moving frames from CPU %d to CPU %d\n",
1139 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1140 __skb_queue_tail(&p0->fcoe_rx_list, skb);
1141 spin_unlock_bh(&p0->fcoe_rx_list.lock);
1144 * The targeted CPU is not initialized and cannot accept
1145 * new skbs. Unlock the targeted CPU and drop the skbs
1146 * on the CPU that is going offline.
1148 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1150 spin_unlock_bh(&p0->fcoe_rx_list.lock);
1154 * This scenario occurs when the module is being removed
1155 * and all threads are being destroyed. skbs will continue
1156 * to be shifted from the CPU thread that is being removed
1157 * to the CPU thread associated with the CPU that is processing
1158 * the module removal. Once there is only one CPU Rx thread it
1159 * will reach this case and we will drop all skbs and later
1162 spin_lock_bh(&p->fcoe_rx_list.lock);
1163 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1165 spin_unlock_bh(&p->fcoe_rx_list.lock);
1170 * This a non-SMP scenario where the singular Rx thread is
1171 * being removed. Free all skbs and stop the thread.
1173 spin_lock_bh(&p->fcoe_rx_list.lock);
1174 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1176 spin_unlock_bh(&p->fcoe_rx_list.lock);
1180 kthread_stop(thread);
1187 * fcoe_cpu_callback() - Handler for CPU hotplug events
1188 * @nfb: The callback data block
1189 * @action: The event triggering the callback
1190 * @hcpu: The index of the CPU that the event is for
1192 * This creates or destroys per-CPU data for fcoe
1194 * Returns NOTIFY_OK always.
1196 static int fcoe_cpu_callback(struct notifier_block *nfb,
1197 unsigned long action, void *hcpu)
1199 unsigned cpu = (unsigned long)hcpu;
1203 case CPU_ONLINE_FROZEN:
1204 FCOE_DBG("CPU %x online: Create Rx thread\n", cpu);
1205 fcoe_percpu_thread_create(cpu);
1208 case CPU_DEAD_FROZEN:
1209 FCOE_DBG("CPU %x offline: Remove Rx thread\n", cpu);
1210 fcoe_percpu_thread_destroy(cpu);
1219 * fcoe_rcv() - Receive packets from a net device
1220 * @skb: The received packet
1221 * @netdev: The net device that the packet was received on
1222 * @ptype: The packet type context
1223 * @olddev: The last device net device
1225 * This routine is called by NET_RX_SOFTIRQ. It receives a packet, builds a
1226 * FC frame and passes the frame to libfc.
1228 * Returns: 0 for success
1230 int fcoe_rcv(struct sk_buff *skb, struct net_device *netdev,
1231 struct packet_type *ptype, struct net_device *olddev)
1233 struct fc_lport *lport;
1234 struct fcoe_rcv_info *fr;
1235 struct fcoe_interface *fcoe;
1236 struct fc_frame_header *fh;
1237 struct fcoe_percpu_s *fps;
1241 fcoe = container_of(ptype, struct fcoe_interface, fcoe_packet_type);
1242 lport = fcoe->ctlr.lp;
1243 if (unlikely(!lport)) {
1244 FCOE_NETDEV_DBG(netdev, "Cannot find hba structure");
1247 if (!lport->link_up)
1250 FCOE_NETDEV_DBG(netdev, "skb_info: len:%d data_len:%d head:%p "
1251 "data:%p tail:%p end:%p sum:%d dev:%s",
1252 skb->len, skb->data_len, skb->head, skb->data,
1253 skb_tail_pointer(skb), skb_end_pointer(skb),
1254 skb->csum, skb->dev ? skb->dev->name : "<NULL>");
1258 if (is_fip_mode(&fcoe->ctlr) &&
1259 compare_ether_addr(eh->h_source, fcoe->ctlr.dest_addr)) {
1260 FCOE_NETDEV_DBG(netdev, "wrong source mac address:%pM\n",
1266 * Check for minimum frame length, and make sure required FCoE
1267 * and FC headers are pulled into the linear data area.
1269 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
1270 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
1273 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
1274 fh = (struct fc_frame_header *) skb_transport_header(skb);
1276 if (ntoh24(&eh->h_dest[3]) != ntoh24(fh->fh_d_id)) {
1277 FCOE_NETDEV_DBG(netdev, "FC frame d_id mismatch with MAC:%pM\n",
1282 fr = fcoe_dev_from_skb(skb);
1287 * In case the incoming frame's exchange is originated from
1288 * the initiator, then received frame's exchange id is ANDed
1289 * with fc_cpu_mask bits to get the same cpu on which exchange
1290 * was originated, otherwise just use the current cpu.
1292 if (ntoh24(fh->fh_f_ctl) & FC_FC_EX_CTX)
1293 cpu = ntohs(fh->fh_ox_id) & fc_cpu_mask;
1295 cpu = smp_processor_id();
1297 fps = &per_cpu(fcoe_percpu, cpu);
1298 spin_lock_bh(&fps->fcoe_rx_list.lock);
1299 if (unlikely(!fps->thread)) {
1301 * The targeted CPU is not ready, let's target
1302 * the first CPU now. For non-SMP systems this
1303 * will check the same CPU twice.
1305 FCOE_NETDEV_DBG(netdev, "CPU is online, but no receive thread "
1306 "ready for incoming skb- using first online "
1309 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1310 cpu = cpumask_first(cpu_online_mask);
1311 fps = &per_cpu(fcoe_percpu, cpu);
1312 spin_lock_bh(&fps->fcoe_rx_list.lock);
1314 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1320 * We now have a valid CPU that we're targeting for
1321 * this skb. We also have this receive thread locked,
1322 * so we're free to queue skbs into it's queue.
1325 /* If this is a SCSI-FCP frame, and this is already executing on the
1326 * correct CPU, and the queue for this CPU is empty, then go ahead
1327 * and process the frame directly in the softirq context.
1328 * This lets us process completions without context switching from the
1329 * NET_RX softirq, to our receive processing thread, and then back to
1330 * BLOCK softirq context.
1332 if (fh->fh_type == FC_TYPE_FCP &&
1333 cpu == smp_processor_id() &&
1334 skb_queue_empty(&fps->fcoe_rx_list)) {
1335 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1336 fcoe_recv_frame(skb);
1338 __skb_queue_tail(&fps->fcoe_rx_list, skb);
1339 if (fps->fcoe_rx_list.qlen == 1)
1340 wake_up_process(fps->thread);
1341 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1346 per_cpu_ptr(lport->dev_stats, get_cpu())->ErrorFrames++;
1354 * fcoe_alloc_paged_crc_eof() - Allocate a page to be used for the trailer CRC
1355 * @skb: The packet to be transmitted
1356 * @tlen: The total length of the trailer
1358 * Returns: 0 for success
1360 static int fcoe_alloc_paged_crc_eof(struct sk_buff *skb, int tlen)
1362 struct fcoe_percpu_s *fps;
1365 fps = &get_cpu_var(fcoe_percpu);
1366 rc = fcoe_get_paged_crc_eof(skb, tlen, fps);
1367 put_cpu_var(fcoe_percpu);
1373 * fcoe_xmit() - Transmit a FCoE frame
1374 * @lport: The local port that the frame is to be transmitted for
1375 * @fp: The frame to be transmitted
1377 * Return: 0 for success
1379 int fcoe_xmit(struct fc_lport *lport, struct fc_frame *fp)
1384 struct fcoe_crc_eof *cp;
1385 struct sk_buff *skb;
1386 struct fcoe_dev_stats *stats;
1387 struct fc_frame_header *fh;
1388 unsigned int hlen; /* header length implies the version */
1389 unsigned int tlen; /* trailer length */
1390 unsigned int elen; /* eth header, may include vlan */
1391 struct fcoe_port *port = lport_priv(lport);
1392 struct fcoe_interface *fcoe = port->priv;
1394 struct fcoe_hdr *hp;
1396 WARN_ON((fr_len(fp) % sizeof(u32)) != 0);
1398 fh = fc_frame_header_get(fp);
1400 wlen = skb->len / FCOE_WORD_TO_BYTE;
1402 if (!lport->link_up) {
1407 if (unlikely(fh->fh_type == FC_TYPE_ELS) &&
1408 fcoe_ctlr_els_send(&fcoe->ctlr, lport, skb))
1414 elen = sizeof(struct ethhdr);
1415 hlen = sizeof(struct fcoe_hdr);
1416 tlen = sizeof(struct fcoe_crc_eof);
1417 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
1420 if (likely(lport->crc_offload)) {
1421 skb->ip_summed = CHECKSUM_PARTIAL;
1422 skb->csum_start = skb_headroom(skb);
1423 skb->csum_offset = skb->len;
1426 skb->ip_summed = CHECKSUM_NONE;
1427 crc = fcoe_fc_crc(fp);
1430 /* copy port crc and eof to the skb buff */
1431 if (skb_is_nonlinear(skb)) {
1433 if (fcoe_alloc_paged_crc_eof(skb, tlen)) {
1437 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
1438 cp = kmap_atomic(frag->page, KM_SKB_DATA_SOFTIRQ)
1439 + frag->page_offset;
1441 cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
1444 memset(cp, 0, sizeof(*cp));
1446 cp->fcoe_crc32 = cpu_to_le32(~crc);
1448 if (skb_is_nonlinear(skb)) {
1449 kunmap_atomic(cp, KM_SKB_DATA_SOFTIRQ);
1453 /* adjust skb network/transport offsets to match mac/fcoe/port */
1454 skb_push(skb, elen + hlen);
1455 skb_reset_mac_header(skb);
1456 skb_reset_network_header(skb);
1457 skb->mac_len = elen;
1458 skb->protocol = htons(ETH_P_FCOE);
1459 skb->dev = fcoe->netdev;
1461 /* fill up mac and fcoe headers */
1463 eh->h_proto = htons(ETH_P_FCOE);
1464 memcpy(eh->h_dest, fcoe->ctlr.dest_addr, ETH_ALEN);
1465 if (fcoe->ctlr.map_dest)
1466 memcpy(eh->h_dest + 3, fh->fh_d_id, 3);
1468 if (unlikely(fcoe->ctlr.flogi_oxid != FC_XID_UNKNOWN))
1469 memcpy(eh->h_source, fcoe->ctlr.ctl_src_addr, ETH_ALEN);
1471 memcpy(eh->h_source, port->data_src_addr, ETH_ALEN);
1473 hp = (struct fcoe_hdr *)(eh + 1);
1474 memset(hp, 0, sizeof(*hp));
1476 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
1479 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
1480 if (lport->seq_offload && fr_max_payload(fp)) {
1481 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
1482 skb_shinfo(skb)->gso_size = fr_max_payload(fp);
1484 skb_shinfo(skb)->gso_type = 0;
1485 skb_shinfo(skb)->gso_size = 0;
1487 /* update tx stats: regardless if LLD fails */
1488 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
1490 stats->TxWords += wlen;
1493 /* send down to lld */
1495 if (port->fcoe_pending_queue.qlen)
1496 fcoe_check_wait_queue(lport, skb);
1497 else if (fcoe_start_io(skb))
1498 fcoe_check_wait_queue(lport, skb);
1504 * fcoe_percpu_flush_done() - Indicate per-CPU queue flush completion
1505 * @skb: The completed skb (argument required by destructor)
1507 static void fcoe_percpu_flush_done(struct sk_buff *skb)
1509 complete(&fcoe_flush_completion);
1513 * fcoe_filter_frames() - filter out bad fcoe frames, i.e. bad CRC
1514 * @lport: The local port the frame was received on
1515 * @fp: The received frame
1517 * Return: 0 on passing filtering checks
1519 static inline int fcoe_filter_frames(struct fc_lport *lport,
1520 struct fc_frame *fp)
1522 struct fcoe_interface *fcoe;
1523 struct fc_frame_header *fh;
1524 struct sk_buff *skb = (struct sk_buff *)fp;
1525 struct fcoe_dev_stats *stats;
1528 * We only check CRC if no offload is available and if it is
1529 * it's solicited data, in which case, the FCP layer would
1530 * check it during the copy.
1532 if (lport->crc_offload && skb->ip_summed == CHECKSUM_UNNECESSARY)
1533 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1535 fr_flags(fp) |= FCPHF_CRC_UNCHECKED;
1537 fh = (struct fc_frame_header *) skb_transport_header(skb);
1538 fh = fc_frame_header_get(fp);
1539 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA && fh->fh_type == FC_TYPE_FCP)
1542 fcoe = ((struct fcoe_port *)lport_priv(lport))->priv;
1543 if (is_fip_mode(&fcoe->ctlr) && fc_frame_payload_op(fp) == ELS_LOGO &&
1544 ntoh24(fh->fh_s_id) == FC_FID_FLOGI) {
1545 FCOE_DBG("fcoe: dropping FCoE lport LOGO in fip mode\n");
1549 if (!(fr_flags(fp) & FCPHF_CRC_UNCHECKED) ||
1550 le32_to_cpu(fr_crc(fp)) == ~crc32(~0, skb->data, skb->len)) {
1551 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1555 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
1556 stats->InvalidCRCCount++;
1557 if (stats->InvalidCRCCount < 5)
1558 printk(KERN_WARNING "fcoe: dropping frame with CRC error\n");
1563 * fcoe_recv_frame() - process a single received frame
1564 * @skb: frame to process
1566 static void fcoe_recv_frame(struct sk_buff *skb)
1569 struct fc_lport *lport;
1570 struct fcoe_rcv_info *fr;
1571 struct fcoe_dev_stats *stats;
1572 struct fcoe_crc_eof crc_eof;
1573 struct fc_frame *fp;
1574 struct fcoe_port *port;
1575 struct fcoe_hdr *hp;
1577 fr = fcoe_dev_from_skb(skb);
1579 if (unlikely(!lport)) {
1580 if (skb->destructor != fcoe_percpu_flush_done)
1581 FCOE_NETDEV_DBG(skb->dev, "NULL lport in skb");
1586 FCOE_NETDEV_DBG(skb->dev, "skb_info: len:%d data_len:%d "
1587 "head:%p data:%p tail:%p end:%p sum:%d dev:%s",
1588 skb->len, skb->data_len,
1589 skb->head, skb->data, skb_tail_pointer(skb),
1590 skb_end_pointer(skb), skb->csum,
1591 skb->dev ? skb->dev->name : "<NULL>");
1593 port = lport_priv(lport);
1594 if (skb_is_nonlinear(skb))
1595 skb_linearize(skb); /* not ideal */
1598 * Frame length checks and setting up the header pointers
1599 * was done in fcoe_rcv already.
1601 hp = (struct fcoe_hdr *) skb_network_header(skb);
1603 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
1604 if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) {
1605 if (stats->ErrorFrames < 5)
1606 printk(KERN_WARNING "fcoe: FCoE version "
1607 "mismatch: The frame has "
1608 "version %x, but the "
1609 "initiator supports version "
1610 "%x\n", FC_FCOE_DECAPS_VER(hp),
1615 skb_pull(skb, sizeof(struct fcoe_hdr));
1616 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
1619 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
1621 fp = (struct fc_frame *)skb;
1624 fr_sof(fp) = hp->fcoe_sof;
1626 /* Copy out the CRC and EOF trailer for access */
1627 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof)))
1629 fr_eof(fp) = crc_eof.fcoe_eof;
1630 fr_crc(fp) = crc_eof.fcoe_crc32;
1631 if (pskb_trim(skb, fr_len))
1634 if (!fcoe_filter_frames(lport, fp)) {
1636 fc_exch_recv(lport, fp);
1640 stats->ErrorFrames++;
1646 * fcoe_percpu_receive_thread() - The per-CPU packet receive thread
1647 * @arg: The per-CPU context
1649 * Return: 0 for success
1651 int fcoe_percpu_receive_thread(void *arg)
1653 struct fcoe_percpu_s *p = arg;
1654 struct sk_buff *skb;
1656 set_user_nice(current, -20);
1658 while (!kthread_should_stop()) {
1660 spin_lock_bh(&p->fcoe_rx_list.lock);
1661 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) == NULL) {
1662 set_current_state(TASK_INTERRUPTIBLE);
1663 spin_unlock_bh(&p->fcoe_rx_list.lock);
1665 set_current_state(TASK_RUNNING);
1666 if (kthread_should_stop())
1668 spin_lock_bh(&p->fcoe_rx_list.lock);
1670 spin_unlock_bh(&p->fcoe_rx_list.lock);
1671 fcoe_recv_frame(skb);
1677 * fcoe_dev_setup() - Setup the link change notification interface
1679 static void fcoe_dev_setup(void)
1681 register_netdevice_notifier(&fcoe_notifier);
1685 * fcoe_dev_cleanup() - Cleanup the link change notification interface
1687 static void fcoe_dev_cleanup(void)
1689 unregister_netdevice_notifier(&fcoe_notifier);
1693 * fcoe_device_notification() - Handler for net device events
1694 * @notifier: The context of the notification
1695 * @event: The type of event
1696 * @ptr: The net device that the event was on
1698 * This function is called by the Ethernet driver in case of link change event.
1700 * Returns: 0 for success
1702 static int fcoe_device_notification(struct notifier_block *notifier,
1703 ulong event, void *ptr)
1705 struct fc_lport *lport = NULL;
1706 struct net_device *netdev = ptr;
1707 struct fcoe_interface *fcoe;
1708 struct fcoe_port *port;
1709 struct fcoe_dev_stats *stats;
1710 u32 link_possible = 1;
1714 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
1715 if (fcoe->netdev == netdev) {
1716 lport = fcoe->ctlr.lp;
1727 case NETDEV_GOING_DOWN:
1733 case NETDEV_CHANGEMTU:
1734 if (netdev->features & NETIF_F_FCOE_MTU)
1736 mfs = netdev->mtu - (sizeof(struct fcoe_hdr) +
1737 sizeof(struct fcoe_crc_eof));
1738 if (mfs >= FC_MIN_MAX_FRAME)
1739 fc_set_mfs(lport, mfs);
1741 case NETDEV_REGISTER:
1743 case NETDEV_UNREGISTER:
1744 list_del(&fcoe->list);
1745 port = lport_priv(fcoe->ctlr.lp);
1746 fcoe_interface_cleanup(fcoe);
1747 queue_work(fcoe_wq, &port->destroy_work);
1750 case NETDEV_FEAT_CHANGE:
1751 fcoe_netdev_features_change(lport, netdev);
1754 FCOE_NETDEV_DBG(netdev, "Unknown event %ld "
1755 "from netdev netlink\n", event);
1758 fcoe_link_speed_update(lport);
1760 if (link_possible && !fcoe_link_ok(lport))
1761 fcoe_ctlr_link_up(&fcoe->ctlr);
1762 else if (fcoe_ctlr_link_down(&fcoe->ctlr)) {
1763 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
1764 stats->LinkFailureCount++;
1766 fcoe_clean_pending_queue(lport);
1773 * fcoe_disable() - Disables a FCoE interface
1774 * @netdev : The net_device object the Ethernet interface to create on
1776 * Called from fcoe transport.
1778 * Returns: 0 for success
1780 static int fcoe_disable(struct net_device *netdev)
1782 struct fcoe_interface *fcoe;
1785 mutex_lock(&fcoe_config_mutex);
1786 #ifdef CONFIG_FCOE_MODULE
1788 * Make sure the module has been initialized, and is not about to be
1789 * removed. Module paramter sysfs files are writable before the
1790 * module_init function is called and after module_exit.
1792 if (THIS_MODULE->state != MODULE_STATE_LIVE) {
1798 if (!rtnl_trylock()) {
1799 mutex_unlock(&fcoe_config_mutex);
1800 return -ERESTARTSYS;
1803 fcoe = fcoe_hostlist_lookup_port(netdev);
1807 fcoe_ctlr_link_down(&fcoe->ctlr);
1808 fcoe_clean_pending_queue(fcoe->ctlr.lp);
1813 mutex_unlock(&fcoe_config_mutex);
1818 * fcoe_enable() - Enables a FCoE interface
1819 * @netdev : The net_device object the Ethernet interface to create on
1821 * Called from fcoe transport.
1823 * Returns: 0 for success
1825 static int fcoe_enable(struct net_device *netdev)
1827 struct fcoe_interface *fcoe;
1830 mutex_lock(&fcoe_config_mutex);
1831 #ifdef CONFIG_FCOE_MODULE
1833 * Make sure the module has been initialized, and is not about to be
1834 * removed. Module paramter sysfs files are writable before the
1835 * module_init function is called and after module_exit.
1837 if (THIS_MODULE->state != MODULE_STATE_LIVE) {
1842 if (!rtnl_trylock()) {
1843 mutex_unlock(&fcoe_config_mutex);
1844 return -ERESTARTSYS;
1847 fcoe = fcoe_hostlist_lookup_port(netdev);
1852 else if (!fcoe_link_ok(fcoe->ctlr.lp))
1853 fcoe_ctlr_link_up(&fcoe->ctlr);
1856 mutex_unlock(&fcoe_config_mutex);
1861 * fcoe_destroy() - Destroy a FCoE interface
1862 * @netdev : The net_device object the Ethernet interface to create on
1864 * Called from fcoe transport
1866 * Returns: 0 for success
1868 static int fcoe_destroy(struct net_device *netdev)
1870 struct fcoe_interface *fcoe;
1873 mutex_lock(&fcoe_config_mutex);
1874 #ifdef CONFIG_FCOE_MODULE
1876 * Make sure the module has been initialized, and is not about to be
1877 * removed. Module paramter sysfs files are writable before the
1878 * module_init function is called and after module_exit.
1880 if (THIS_MODULE->state != MODULE_STATE_LIVE) {
1885 if (!rtnl_trylock()) {
1886 mutex_unlock(&fcoe_config_mutex);
1887 return -ERESTARTSYS;
1890 fcoe = fcoe_hostlist_lookup_port(netdev);
1896 fcoe_interface_cleanup(fcoe);
1897 list_del(&fcoe->list);
1898 /* RTNL mutex is dropped by fcoe_if_destroy */
1899 fcoe_if_destroy(fcoe->ctlr.lp);
1901 mutex_unlock(&fcoe_config_mutex);
1906 * fcoe_destroy_work() - Destroy a FCoE port in a deferred work context
1907 * @work: Handle to the FCoE port to be destroyed
1909 static void fcoe_destroy_work(struct work_struct *work)
1911 struct fcoe_port *port;
1913 port = container_of(work, struct fcoe_port, destroy_work);
1914 mutex_lock(&fcoe_config_mutex);
1916 /* RTNL mutex is dropped by fcoe_if_destroy */
1917 fcoe_if_destroy(port->lport);
1918 mutex_unlock(&fcoe_config_mutex);
1922 * fcoe_match() - Check if the FCoE is supported on the given netdevice
1923 * @netdev : The net_device object the Ethernet interface to create on
1925 * Called from fcoe transport.
1927 * Returns: always returns true as this is the default FCoE transport,
1928 * i.e., support all netdevs.
1930 static bool fcoe_match(struct net_device *netdev)
1936 * fcoe_create() - Create a fcoe interface
1937 * @netdev : The net_device object the Ethernet interface to create on
1938 * @fip_mode: The FIP mode for this creation
1940 * Called from fcoe transport
1942 * Returns: 0 for success
1944 static int fcoe_create(struct net_device *netdev, enum fip_state fip_mode)
1947 struct fcoe_interface *fcoe;
1948 struct fc_lport *lport;
1950 mutex_lock(&fcoe_config_mutex);
1952 if (!rtnl_trylock()) {
1953 mutex_unlock(&fcoe_config_mutex);
1954 return -ERESTARTSYS;
1957 #ifdef CONFIG_FCOE_MODULE
1959 * Make sure the module has been initialized, and is not about to be
1960 * removed. Module paramter sysfs files are writable before the
1961 * module_init function is called and after module_exit.
1963 if (THIS_MODULE->state != MODULE_STATE_LIVE) {
1969 /* look for existing lport */
1970 if (fcoe_hostlist_lookup(netdev)) {
1975 fcoe = fcoe_interface_create(netdev, fip_mode);
1981 lport = fcoe_if_create(fcoe, &netdev->dev, 0);
1982 if (IS_ERR(lport)) {
1983 printk(KERN_ERR "fcoe: Failed to create interface (%s)\n",
1986 fcoe_interface_cleanup(fcoe);
1990 /* Make this the "master" N_Port */
1991 fcoe->ctlr.lp = lport;
1993 /* add to lports list */
1994 fcoe_hostlist_add(lport);
1996 /* start FIP Discovery and FLOGI */
1997 lport->boot_time = jiffies;
1998 fc_fabric_login(lport);
1999 if (!fcoe_link_ok(lport))
2000 fcoe_ctlr_link_up(&fcoe->ctlr);
2003 * Release from init in fcoe_interface_create(), on success lport
2004 * should be holding a reference taken in fcoe_if_create().
2006 fcoe_interface_put(fcoe);
2008 mutex_unlock(&fcoe_config_mutex);
2012 fcoe_interface_put(fcoe);
2015 mutex_unlock(&fcoe_config_mutex);
2020 * fcoe_link_speed_update() - Update the supported and actual link speeds
2021 * @lport: The local port to update speeds for
2023 * Returns: 0 if the ethtool query was successful
2024 * -1 if the ethtool query failed
2026 int fcoe_link_speed_update(struct fc_lport *lport)
2028 struct net_device *netdev = fcoe_netdev(lport);
2029 struct ethtool_cmd ecmd = { ETHTOOL_GSET };
2031 if (!dev_ethtool_get_settings(netdev, &ecmd)) {
2032 lport->link_supported_speeds &=
2033 ~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
2034 if (ecmd.supported & (SUPPORTED_1000baseT_Half |
2035 SUPPORTED_1000baseT_Full))
2036 lport->link_supported_speeds |= FC_PORTSPEED_1GBIT;
2037 if (ecmd.supported & SUPPORTED_10000baseT_Full)
2038 lport->link_supported_speeds |=
2039 FC_PORTSPEED_10GBIT;
2040 if (ecmd.speed == SPEED_1000)
2041 lport->link_speed = FC_PORTSPEED_1GBIT;
2042 if (ecmd.speed == SPEED_10000)
2043 lport->link_speed = FC_PORTSPEED_10GBIT;
2051 * fcoe_link_ok() - Check if the link is OK for a local port
2052 * @lport: The local port to check link on
2054 * Returns: 0 if link is UP and OK, -1 if not
2057 int fcoe_link_ok(struct fc_lport *lport)
2059 struct net_device *netdev = fcoe_netdev(lport);
2061 if (netif_oper_up(netdev))
2067 * fcoe_percpu_clean() - Clear all pending skbs for an local port
2068 * @lport: The local port whose skbs are to be cleared
2070 * Must be called with fcoe_create_mutex held to single-thread completion.
2072 * This flushes the pending skbs by adding a new skb to each queue and
2073 * waiting until they are all freed. This assures us that not only are
2074 * there no packets that will be handled by the lport, but also that any
2075 * threads already handling packet have returned.
2077 void fcoe_percpu_clean(struct fc_lport *lport)
2079 struct fcoe_percpu_s *pp;
2080 struct fcoe_rcv_info *fr;
2081 struct sk_buff_head *list;
2082 struct sk_buff *skb, *next;
2083 struct sk_buff *head;
2086 for_each_possible_cpu(cpu) {
2087 pp = &per_cpu(fcoe_percpu, cpu);
2088 spin_lock_bh(&pp->fcoe_rx_list.lock);
2089 list = &pp->fcoe_rx_list;
2091 for (skb = head; skb != (struct sk_buff *)list;
2094 fr = fcoe_dev_from_skb(skb);
2095 if (fr->fr_dev == lport) {
2096 __skb_unlink(skb, list);
2101 if (!pp->thread || !cpu_online(cpu)) {
2102 spin_unlock_bh(&pp->fcoe_rx_list.lock);
2106 skb = dev_alloc_skb(0);
2108 spin_unlock_bh(&pp->fcoe_rx_list.lock);
2111 skb->destructor = fcoe_percpu_flush_done;
2113 __skb_queue_tail(&pp->fcoe_rx_list, skb);
2114 if (pp->fcoe_rx_list.qlen == 1)
2115 wake_up_process(pp->thread);
2116 spin_unlock_bh(&pp->fcoe_rx_list.lock);
2118 wait_for_completion(&fcoe_flush_completion);
2123 * fcoe_reset() - Reset a local port
2124 * @shost: The SCSI host associated with the local port to be reset
2126 * Returns: Always 0 (return value required by FC transport template)
2128 int fcoe_reset(struct Scsi_Host *shost)
2130 struct fc_lport *lport = shost_priv(shost);
2131 struct fcoe_port *port = lport_priv(lport);
2132 struct fcoe_interface *fcoe = port->priv;
2134 fcoe_ctlr_link_down(&fcoe->ctlr);
2135 fcoe_clean_pending_queue(fcoe->ctlr.lp);
2136 if (!fcoe_link_ok(fcoe->ctlr.lp))
2137 fcoe_ctlr_link_up(&fcoe->ctlr);
2142 * fcoe_hostlist_lookup_port() - Find the FCoE interface associated with a net device
2143 * @netdev: The net device used as a key
2145 * Locking: Must be called with the RNL mutex held.
2147 * Returns: NULL or the FCoE interface
2149 static struct fcoe_interface *
2150 fcoe_hostlist_lookup_port(const struct net_device *netdev)
2152 struct fcoe_interface *fcoe;
2154 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
2155 if (fcoe->netdev == netdev)
2162 * fcoe_hostlist_lookup() - Find the local port associated with a
2164 * @netdev: The netdevice used as a key
2166 * Locking: Must be called with the RTNL mutex held
2168 * Returns: NULL or the local port
2170 static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *netdev)
2172 struct fcoe_interface *fcoe;
2174 fcoe = fcoe_hostlist_lookup_port(netdev);
2175 return (fcoe) ? fcoe->ctlr.lp : NULL;
2179 * fcoe_hostlist_add() - Add the FCoE interface identified by a local
2180 * port to the hostlist
2181 * @lport: The local port that identifies the FCoE interface to be added
2183 * Locking: must be called with the RTNL mutex held
2185 * Returns: 0 for success
2187 static int fcoe_hostlist_add(const struct fc_lport *lport)
2189 struct fcoe_interface *fcoe;
2190 struct fcoe_port *port;
2192 fcoe = fcoe_hostlist_lookup_port(fcoe_netdev(lport));
2194 port = lport_priv(lport);
2196 list_add_tail(&fcoe->list, &fcoe_hostlist);
2202 static struct fcoe_transport fcoe_sw_transport = {
2203 .name = {FCOE_TRANSPORT_DEFAULT},
2205 .list = LIST_HEAD_INIT(fcoe_sw_transport.list),
2206 .match = fcoe_match,
2207 .create = fcoe_create,
2208 .destroy = fcoe_destroy,
2209 .enable = fcoe_enable,
2210 .disable = fcoe_disable,
2214 * fcoe_init() - Initialize fcoe.ko
2216 * Returns: 0 on success, or a negative value on failure
2218 static int __init fcoe_init(void)
2220 struct fcoe_percpu_s *p;
2224 fcoe_wq = alloc_workqueue("fcoe", 0, 0);
2228 /* register as a fcoe transport */
2229 rc = fcoe_transport_attach(&fcoe_sw_transport);
2231 printk(KERN_ERR "failed to register an fcoe transport, check "
2232 "if libfcoe is loaded\n");
2236 mutex_lock(&fcoe_config_mutex);
2238 for_each_possible_cpu(cpu) {
2239 p = &per_cpu(fcoe_percpu, cpu);
2240 skb_queue_head_init(&p->fcoe_rx_list);
2243 for_each_online_cpu(cpu)
2244 fcoe_percpu_thread_create(cpu);
2246 /* Initialize per CPU interrupt thread */
2247 rc = register_hotcpu_notifier(&fcoe_cpu_notifier);
2251 /* Setup link change notification */
2254 rc = fcoe_if_init();
2258 mutex_unlock(&fcoe_config_mutex);
2262 for_each_online_cpu(cpu) {
2263 fcoe_percpu_thread_destroy(cpu);
2265 mutex_unlock(&fcoe_config_mutex);
2266 destroy_workqueue(fcoe_wq);
2269 module_init(fcoe_init);
2272 * fcoe_exit() - Clean up fcoe.ko
2274 * Returns: 0 on success or a negative value on failure
2276 static void __exit fcoe_exit(void)
2278 struct fcoe_interface *fcoe, *tmp;
2279 struct fcoe_port *port;
2282 mutex_lock(&fcoe_config_mutex);
2286 /* releases the associated fcoe hosts */
2288 list_for_each_entry_safe(fcoe, tmp, &fcoe_hostlist, list) {
2289 list_del(&fcoe->list);
2290 port = lport_priv(fcoe->ctlr.lp);
2291 fcoe_interface_cleanup(fcoe);
2292 queue_work(fcoe_wq, &port->destroy_work);
2296 unregister_hotcpu_notifier(&fcoe_cpu_notifier);
2298 for_each_online_cpu(cpu)
2299 fcoe_percpu_thread_destroy(cpu);
2301 mutex_unlock(&fcoe_config_mutex);
2304 * destroy_work's may be chained but destroy_workqueue()
2305 * can take care of them. Just kill the fcoe_wq.
2307 destroy_workqueue(fcoe_wq);
2310 * Detaching from the scsi transport must happen after all
2311 * destroys are done on the fcoe_wq. destroy_workqueue will
2312 * enusre the fcoe_wq is flushed.
2316 /* detach from fcoe transport */
2317 fcoe_transport_detach(&fcoe_sw_transport);
2319 module_exit(fcoe_exit);
2322 * fcoe_flogi_resp() - FCoE specific FLOGI and FDISC response handler
2323 * @seq: active sequence in the FLOGI or FDISC exchange
2324 * @fp: response frame, or error encoded in a pointer (timeout)
2325 * @arg: pointer the the fcoe_ctlr structure
2327 * This handles MAC address management for FCoE, then passes control on to
2328 * the libfc FLOGI response handler.
2330 static void fcoe_flogi_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
2332 struct fcoe_ctlr *fip = arg;
2333 struct fc_exch *exch = fc_seq_exch(seq);
2334 struct fc_lport *lport = exch->lp;
2340 mac = fr_cb(fp)->granted_mac;
2341 if (is_zero_ether_addr(mac)) {
2343 if (fcoe_ctlr_recv_flogi(fip, lport, fp)) {
2348 fcoe_update_src_mac(lport, mac);
2350 fc_lport_flogi_resp(seq, fp, lport);
2354 * fcoe_logo_resp() - FCoE specific LOGO response handler
2355 * @seq: active sequence in the LOGO exchange
2356 * @fp: response frame, or error encoded in a pointer (timeout)
2357 * @arg: pointer the the fcoe_ctlr structure
2359 * This handles MAC address management for FCoE, then passes control on to
2360 * the libfc LOGO response handler.
2362 static void fcoe_logo_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
2364 struct fc_lport *lport = arg;
2365 static u8 zero_mac[ETH_ALEN] = { 0 };
2368 fcoe_update_src_mac(lport, zero_mac);
2369 fc_lport_logo_resp(seq, fp, lport);
2373 * fcoe_elsct_send - FCoE specific ELS handler
2375 * This does special case handling of FIP encapsualted ELS exchanges for FCoE,
2376 * using FCoE specific response handlers and passing the FIP controller as
2377 * the argument (the lport is still available from the exchange).
2379 * Most of the work here is just handed off to the libfc routine.
2381 static struct fc_seq *fcoe_elsct_send(struct fc_lport *lport, u32 did,
2382 struct fc_frame *fp, unsigned int op,
2383 void (*resp)(struct fc_seq *,
2386 void *arg, u32 timeout)
2388 struct fcoe_port *port = lport_priv(lport);
2389 struct fcoe_interface *fcoe = port->priv;
2390 struct fcoe_ctlr *fip = &fcoe->ctlr;
2391 struct fc_frame_header *fh = fc_frame_header_get(fp);
2396 if (lport->point_to_multipoint)
2398 return fc_elsct_send(lport, did, fp, op, fcoe_flogi_resp,
2401 /* only hook onto fabric logouts, not port logouts */
2402 if (ntoh24(fh->fh_d_id) != FC_FID_FLOGI)
2404 return fc_elsct_send(lport, did, fp, op, fcoe_logo_resp,
2407 return fc_elsct_send(lport, did, fp, op, resp, arg, timeout);
2411 * fcoe_vport_create() - create an fc_host/scsi_host for a vport
2412 * @vport: fc_vport object to create a new fc_host for
2413 * @disabled: start the new fc_host in a disabled state by default?
2415 * Returns: 0 for success
2417 static int fcoe_vport_create(struct fc_vport *vport, bool disabled)
2419 struct Scsi_Host *shost = vport_to_shost(vport);
2420 struct fc_lport *n_port = shost_priv(shost);
2421 struct fcoe_port *port = lport_priv(n_port);
2422 struct fcoe_interface *fcoe = port->priv;
2423 struct net_device *netdev = fcoe->netdev;
2424 struct fc_lport *vn_port;
2426 mutex_lock(&fcoe_config_mutex);
2427 vn_port = fcoe_if_create(fcoe, &vport->dev, 1);
2428 mutex_unlock(&fcoe_config_mutex);
2430 if (IS_ERR(vn_port)) {
2431 printk(KERN_ERR "fcoe: fcoe_vport_create(%s) failed\n",
2437 fc_vport_set_state(vport, FC_VPORT_DISABLED);
2439 vn_port->boot_time = jiffies;
2440 fc_fabric_login(vn_port);
2441 fc_vport_setlink(vn_port);
2447 * fcoe_vport_destroy() - destroy the fc_host/scsi_host for a vport
2448 * @vport: fc_vport object that is being destroyed
2450 * Returns: 0 for success
2452 static int fcoe_vport_destroy(struct fc_vport *vport)
2454 struct Scsi_Host *shost = vport_to_shost(vport);
2455 struct fc_lport *n_port = shost_priv(shost);
2456 struct fc_lport *vn_port = vport->dd_data;
2457 struct fcoe_port *port = lport_priv(vn_port);
2459 mutex_lock(&n_port->lp_mutex);
2460 list_del(&vn_port->list);
2461 mutex_unlock(&n_port->lp_mutex);
2462 queue_work(fcoe_wq, &port->destroy_work);
2467 * fcoe_vport_disable() - change vport state
2468 * @vport: vport to bring online/offline
2469 * @disable: should the vport be disabled?
2471 static int fcoe_vport_disable(struct fc_vport *vport, bool disable)
2473 struct fc_lport *lport = vport->dd_data;
2476 fc_vport_set_state(vport, FC_VPORT_DISABLED);
2477 fc_fabric_logoff(lport);
2479 lport->boot_time = jiffies;
2480 fc_fabric_login(lport);
2481 fc_vport_setlink(lport);
2488 * fcoe_vport_set_symbolic_name() - append vport string to symbolic name
2489 * @vport: fc_vport with a new symbolic name string
2491 * After generating a new symbolic name string, a new RSPN_ID request is
2492 * sent to the name server. There is no response handler, so if it fails
2493 * for some reason it will not be retried.
2495 static void fcoe_set_vport_symbolic_name(struct fc_vport *vport)
2497 struct fc_lport *lport = vport->dd_data;
2498 struct fc_frame *fp;
2501 snprintf(fc_host_symbolic_name(lport->host), FC_SYMBOLIC_NAME_SIZE,
2502 "%s v%s over %s : %s", FCOE_NAME, FCOE_VERSION,
2503 fcoe_netdev(lport)->name, vport->symbolic_name);
2505 if (lport->state != LPORT_ST_READY)
2508 len = strnlen(fc_host_symbolic_name(lport->host), 255);
2509 fp = fc_frame_alloc(lport,
2510 sizeof(struct fc_ct_hdr) +
2511 sizeof(struct fc_ns_rspn) + len);
2514 lport->tt.elsct_send(lport, FC_FID_DIR_SERV, fp, FC_NS_RSPN_ID,
2515 NULL, NULL, 3 * lport->r_a_tov);
2519 * fcoe_get_lesb() - Fill the FCoE Link Error Status Block
2520 * @lport: the local port
2521 * @fc_lesb: the link error status block
2523 static void fcoe_get_lesb(struct fc_lport *lport,
2524 struct fc_els_lesb *fc_lesb)
2527 u32 lfc, vlfc, mdac;
2528 struct fcoe_dev_stats *devst;
2529 struct fcoe_fc_els_lesb *lesb;
2530 struct rtnl_link_stats64 temp;
2531 struct net_device *netdev = fcoe_netdev(lport);
2536 lesb = (struct fcoe_fc_els_lesb *)fc_lesb;
2537 memset(lesb, 0, sizeof(*lesb));
2538 for_each_possible_cpu(cpu) {
2539 devst = per_cpu_ptr(lport->dev_stats, cpu);
2540 lfc += devst->LinkFailureCount;
2541 vlfc += devst->VLinkFailureCount;
2542 mdac += devst->MissDiscAdvCount;
2544 lesb->lesb_link_fail = htonl(lfc);
2545 lesb->lesb_vlink_fail = htonl(vlfc);
2546 lesb->lesb_miss_fka = htonl(mdac);
2547 lesb->lesb_fcs_error = htonl(dev_get_stats(netdev, &temp)->rx_crc_errors);
2551 * fcoe_set_port_id() - Callback from libfc when Port_ID is set.
2552 * @lport: the local port
2553 * @port_id: the port ID
2554 * @fp: the received frame, if any, that caused the port_id to be set.
2556 * This routine handles the case where we received a FLOGI and are
2557 * entering point-to-point mode. We need to call fcoe_ctlr_recv_flogi()
2558 * so it can set the non-mapped mode and gateway address.
2560 * The FLOGI LS_ACC is handled by fcoe_flogi_resp().
2562 static void fcoe_set_port_id(struct fc_lport *lport,
2563 u32 port_id, struct fc_frame *fp)
2565 struct fcoe_port *port = lport_priv(lport);
2566 struct fcoe_interface *fcoe = port->priv;
2568 if (fp && fc_frame_payload_op(fp) == ELS_FLOGI)
2569 fcoe_ctlr_recv_flogi(&fcoe->ctlr, lport, fp);