Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-next-2.6
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / scsi / fcoe / fcoe.c
blobcc23bd9480b2580eaa1c4eae3a40fb1d57c11585
1 /*
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
11 * more details.
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>
31 #include <linux/fs.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>
48 #include "fcoe.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);
67 /* fcoe host list */
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 *,
100 unsigned int);
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 *,
113 unsigned int op,
114 void (*resp)(struct fc_seq *,
115 struct fc_frame *,
116 void *),
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,
236 .this_id = -1,
237 .cmd_per_lun = 3,
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");
274 return -EOPNOTSUPP;
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;
281 rcu_read_lock();
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);
286 fip->spma = 1;
287 break;
290 rcu_read_unlock();
292 /* setup Source Mac Address */
293 if (!fip->spma)
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);
303 if (fip->spma)
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);
308 } else
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);
325 return 0;
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;
339 int err;
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);
345 goto out;
348 fcoe = kzalloc(sizeof(*fcoe), GFP_KERNEL);
349 if (!fcoe) {
350 FCOE_NETDEV_DBG(netdev, "Could not allocate fcoe structure\n");
351 fcoe = ERR_PTR(-ENOMEM);
352 goto out_nomod;
355 dev_hold(netdev);
356 kref_init(&fcoe->kref);
359 * Initialize FIP.
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);
367 if (err) {
368 fcoe_ctlr_destroy(&fcoe->ctlr);
369 kfree(fcoe);
370 dev_put(netdev);
371 fcoe = ERR_PTR(err);
372 goto out_nomod;
375 goto out;
377 out_nomod:
378 module_put(THIS_MODULE);
379 out:
380 return fcoe;
384 * fcoe_interface_release() - fcoe_port kref release function
385 * @kref: Embedded reference count in an fcoe_interface struct
387 static void fcoe_interface_release(struct kref *kref)
389 struct fcoe_interface *fcoe;
390 struct net_device *netdev;
392 fcoe = container_of(kref, struct fcoe_interface, kref);
393 netdev = fcoe->netdev;
394 /* tear-down the FCoE controller */
395 fcoe_ctlr_destroy(&fcoe->ctlr);
396 kfree(fcoe);
397 dev_put(netdev);
398 module_put(THIS_MODULE);
402 * fcoe_interface_get() - Get a reference to a FCoE interface
403 * @fcoe: The FCoE interface to be held
405 static inline void fcoe_interface_get(struct fcoe_interface *fcoe)
407 kref_get(&fcoe->kref);
411 * fcoe_interface_put() - Put a reference to a FCoE interface
412 * @fcoe: The FCoE interface to be released
414 static inline void fcoe_interface_put(struct fcoe_interface *fcoe)
416 kref_put(&fcoe->kref, fcoe_interface_release);
420 * fcoe_interface_cleanup() - Clean up a FCoE interface
421 * @fcoe: The FCoE interface to be cleaned up
423 * Caller must be holding the RTNL mutex
425 void fcoe_interface_cleanup(struct fcoe_interface *fcoe)
427 struct net_device *netdev = fcoe->netdev;
428 struct fcoe_ctlr *fip = &fcoe->ctlr;
429 u8 flogi_maddr[ETH_ALEN];
430 const struct net_device_ops *ops;
431 struct fcoe_port *port = lport_priv(fcoe->ctlr.lp);
433 FCOE_NETDEV_DBG(netdev, "Destroying interface\n");
435 /* Logout of the fabric */
436 fc_fabric_logoff(fcoe->ctlr.lp);
438 /* Cleanup the fc_lport */
439 fc_lport_destroy(fcoe->ctlr.lp);
441 /* Stop the transmit retry timer */
442 del_timer_sync(&port->timer);
444 /* Free existing transmit skbs */
445 fcoe_clean_pending_queue(fcoe->ctlr.lp);
448 * Don't listen for Ethernet packets anymore.
449 * synchronize_net() ensures that the packet handlers are not running
450 * on another CPU. dev_remove_pack() would do that, this calls the
451 * unsyncronized version __dev_remove_pack() to avoid multiple delays.
453 __dev_remove_pack(&fcoe->fcoe_packet_type);
454 __dev_remove_pack(&fcoe->fip_packet_type);
455 synchronize_net();
457 /* Delete secondary MAC addresses */
458 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
459 dev_uc_del(netdev, flogi_maddr);
460 if (fip->spma)
461 dev_uc_del(netdev, fip->ctl_src_addr);
462 if (fip->mode == FIP_MODE_VN2VN) {
463 dev_mc_del(netdev, FIP_ALL_VN2VN_MACS);
464 dev_mc_del(netdev, FIP_ALL_P2P_MACS);
465 } else
466 dev_mc_del(netdev, FIP_ALL_ENODE_MACS);
468 if (!is_zero_ether_addr(port->data_src_addr))
469 dev_uc_del(netdev, port->data_src_addr);
471 /* Tell the LLD we are done w/ FCoE */
472 ops = netdev->netdev_ops;
473 if (ops->ndo_fcoe_disable) {
474 if (ops->ndo_fcoe_disable(netdev))
475 FCOE_NETDEV_DBG(netdev, "Failed to disable FCoE"
476 " specific feature for LLD.\n");
478 fcoe_interface_put(fcoe);
482 * fcoe_fip_recv() - Handler for received FIP frames
483 * @skb: The receive skb
484 * @netdev: The associated net device
485 * @ptype: The packet_type structure which was used to register this handler
486 * @orig_dev: The original net_device the the skb was received on.
487 * (in case dev is a bond)
489 * Returns: 0 for success
491 static int fcoe_fip_recv(struct sk_buff *skb, struct net_device *netdev,
492 struct packet_type *ptype,
493 struct net_device *orig_dev)
495 struct fcoe_interface *fcoe;
497 fcoe = container_of(ptype, struct fcoe_interface, fip_packet_type);
498 fcoe_ctlr_recv(&fcoe->ctlr, skb);
499 return 0;
503 * fcoe_fip_send() - Send an Ethernet-encapsulated FIP frame
504 * @fip: The FCoE controller
505 * @skb: The FIP packet to be sent
507 static void fcoe_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
509 skb->dev = fcoe_from_ctlr(fip)->netdev;
510 dev_queue_xmit(skb);
514 * fcoe_update_src_mac() - Update the Ethernet MAC filters
515 * @lport: The local port to update the source MAC on
516 * @addr: Unicast MAC address to add
518 * Remove any previously-set unicast MAC filter.
519 * Add secondary FCoE MAC address filter for our OUI.
521 static void fcoe_update_src_mac(struct fc_lport *lport, u8 *addr)
523 struct fcoe_port *port = lport_priv(lport);
524 struct fcoe_interface *fcoe = port->priv;
526 rtnl_lock();
527 if (!is_zero_ether_addr(port->data_src_addr))
528 dev_uc_del(fcoe->netdev, port->data_src_addr);
529 if (!is_zero_ether_addr(addr))
530 dev_uc_add(fcoe->netdev, addr);
531 memcpy(port->data_src_addr, addr, ETH_ALEN);
532 rtnl_unlock();
536 * fcoe_get_src_mac() - return the Ethernet source address for an lport
537 * @lport: libfc lport
539 static u8 *fcoe_get_src_mac(struct fc_lport *lport)
541 struct fcoe_port *port = lport_priv(lport);
543 return port->data_src_addr;
547 * fcoe_lport_config() - Set up a local port
548 * @lport: The local port to be setup
550 * Returns: 0 for success
552 static int fcoe_lport_config(struct fc_lport *lport)
554 lport->link_up = 0;
555 lport->qfull = 0;
556 lport->max_retry_count = 3;
557 lport->max_rport_retry_count = 3;
558 lport->e_d_tov = 2 * 1000; /* FC-FS default */
559 lport->r_a_tov = 2 * 2 * 1000;
560 lport->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
561 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
562 lport->does_npiv = 1;
564 fc_lport_init_stats(lport);
566 /* lport fc_lport related configuration */
567 fc_lport_config(lport);
569 /* offload related configuration */
570 lport->crc_offload = 0;
571 lport->seq_offload = 0;
572 lport->lro_enabled = 0;
573 lport->lro_xid = 0;
574 lport->lso_max = 0;
576 return 0;
580 * fcoe_get_wwn() - Get the world wide name from LLD if it supports it
581 * @netdev: the associated net device
582 * @wwn: the output WWN
583 * @type: the type of WWN (WWPN or WWNN)
585 * Returns: 0 for success
587 static int fcoe_get_wwn(struct net_device *netdev, u64 *wwn, int type)
589 const struct net_device_ops *ops = netdev->netdev_ops;
591 if (ops->ndo_fcoe_get_wwn)
592 return ops->ndo_fcoe_get_wwn(netdev, wwn, type);
593 return -EINVAL;
597 * fcoe_netdev_features_change - Updates the lport's offload flags based
598 * on the LLD netdev's FCoE feature flags
600 static void fcoe_netdev_features_change(struct fc_lport *lport,
601 struct net_device *netdev)
603 mutex_lock(&lport->lp_mutex);
605 if (netdev->features & NETIF_F_SG)
606 lport->sg_supp = 1;
607 else
608 lport->sg_supp = 0;
610 if (netdev->features & NETIF_F_FCOE_CRC) {
611 lport->crc_offload = 1;
612 FCOE_NETDEV_DBG(netdev, "Supports FCCRC offload\n");
613 } else {
614 lport->crc_offload = 0;
617 if (netdev->features & NETIF_F_FSO) {
618 lport->seq_offload = 1;
619 lport->lso_max = netdev->gso_max_size;
620 FCOE_NETDEV_DBG(netdev, "Supports LSO for max len 0x%x\n",
621 lport->lso_max);
622 } else {
623 lport->seq_offload = 0;
624 lport->lso_max = 0;
627 if (netdev->fcoe_ddp_xid) {
628 lport->lro_enabled = 1;
629 lport->lro_xid = netdev->fcoe_ddp_xid;
630 FCOE_NETDEV_DBG(netdev, "Supports LRO for max xid 0x%x\n",
631 lport->lro_xid);
632 } else {
633 lport->lro_enabled = 0;
634 lport->lro_xid = 0;
637 mutex_unlock(&lport->lp_mutex);
641 * fcoe_netdev_config() - Set up net devive for SW FCoE
642 * @lport: The local port that is associated with the net device
643 * @netdev: The associated net device
645 * Must be called after fcoe_lport_config() as it will use local port mutex
647 * Returns: 0 for success
649 static int fcoe_netdev_config(struct fc_lport *lport, struct net_device *netdev)
651 u32 mfs;
652 u64 wwnn, wwpn;
653 struct fcoe_interface *fcoe;
654 struct fcoe_port *port;
656 /* Setup lport private data to point to fcoe softc */
657 port = lport_priv(lport);
658 fcoe = port->priv;
661 * Determine max frame size based on underlying device and optional
662 * user-configured limit. If the MFS is too low, fcoe_link_ok()
663 * will return 0, so do this first.
665 mfs = netdev->mtu;
666 if (netdev->features & NETIF_F_FCOE_MTU) {
667 mfs = FCOE_MTU;
668 FCOE_NETDEV_DBG(netdev, "Supports FCOE_MTU of %d bytes\n", mfs);
670 mfs -= (sizeof(struct fcoe_hdr) + sizeof(struct fcoe_crc_eof));
671 if (fc_set_mfs(lport, mfs))
672 return -EINVAL;
674 /* offload features support */
675 fcoe_netdev_features_change(lport, netdev);
677 skb_queue_head_init(&port->fcoe_pending_queue);
678 port->fcoe_pending_queue_active = 0;
679 setup_timer(&port->timer, fcoe_queue_timer, (unsigned long)lport);
681 fcoe_link_speed_update(lport);
683 if (!lport->vport) {
684 if (fcoe_get_wwn(netdev, &wwnn, NETDEV_FCOE_WWNN))
685 wwnn = fcoe_wwn_from_mac(fcoe->ctlr.ctl_src_addr, 1, 0);
686 fc_set_wwnn(lport, wwnn);
687 if (fcoe_get_wwn(netdev, &wwpn, NETDEV_FCOE_WWPN))
688 wwpn = fcoe_wwn_from_mac(fcoe->ctlr.ctl_src_addr,
689 2, 0);
690 fc_set_wwpn(lport, wwpn);
693 return 0;
697 * fcoe_shost_config() - Set up the SCSI host associated with a local port
698 * @lport: The local port
699 * @dev: The device associated with the SCSI host
701 * Must be called after fcoe_lport_config() and fcoe_netdev_config()
703 * Returns: 0 for success
705 static int fcoe_shost_config(struct fc_lport *lport, struct device *dev)
707 int rc = 0;
709 /* lport scsi host config */
710 lport->host->max_lun = FCOE_MAX_LUN;
711 lport->host->max_id = FCOE_MAX_FCP_TARGET;
712 lport->host->max_channel = 0;
713 lport->host->max_cmd_len = FCOE_MAX_CMD_LEN;
715 if (lport->vport)
716 lport->host->transportt = fcoe_vport_scsi_transport;
717 else
718 lport->host->transportt = fcoe_nport_scsi_transport;
720 /* add the new host to the SCSI-ml */
721 rc = scsi_add_host(lport->host, dev);
722 if (rc) {
723 FCOE_NETDEV_DBG(fcoe_netdev(lport), "fcoe_shost_config: "
724 "error on scsi_add_host\n");
725 return rc;
728 if (!lport->vport)
729 fc_host_max_npiv_vports(lport->host) = USHRT_MAX;
731 snprintf(fc_host_symbolic_name(lport->host), FC_SYMBOLIC_NAME_SIZE,
732 "%s v%s over %s", FCOE_NAME, FCOE_VERSION,
733 fcoe_netdev(lport)->name);
735 return 0;
739 * fcoe_oem_match() - The match routine for the offloaded exchange manager
740 * @fp: The I/O frame
742 * This routine will be associated with an exchange manager (EM). When
743 * the libfc exchange handling code is looking for an EM to use it will
744 * call this routine and pass it the frame that it wishes to send. This
745 * routine will return True if the associated EM is to be used and False
746 * if the echange code should continue looking for an EM.
748 * The offload EM that this routine is associated with will handle any
749 * packets that are for SCSI read requests.
751 * Returns: True for read types I/O, otherwise returns false.
753 bool fcoe_oem_match(struct fc_frame *fp)
755 return fc_fcp_is_read(fr_fsp(fp)) &&
756 (fr_fsp(fp)->data_len > fcoe_ddp_min);
760 * fcoe_em_config() - Allocate and configure an exchange manager
761 * @lport: The local port that the new EM will be associated with
763 * Returns: 0 on success
765 static inline int fcoe_em_config(struct fc_lport *lport)
767 struct fcoe_port *port = lport_priv(lport);
768 struct fcoe_interface *fcoe = port->priv;
769 struct fcoe_interface *oldfcoe = NULL;
770 struct net_device *old_real_dev, *cur_real_dev;
771 u16 min_xid = FCOE_MIN_XID;
772 u16 max_xid = FCOE_MAX_XID;
775 * Check if need to allocate an em instance for
776 * offload exchange ids to be shared across all VN_PORTs/lport.
778 if (!lport->lro_enabled || !lport->lro_xid ||
779 (lport->lro_xid >= max_xid)) {
780 lport->lro_xid = 0;
781 goto skip_oem;
785 * Reuse existing offload em instance in case
786 * it is already allocated on real eth device
788 if (fcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
789 cur_real_dev = vlan_dev_real_dev(fcoe->netdev);
790 else
791 cur_real_dev = fcoe->netdev;
793 list_for_each_entry(oldfcoe, &fcoe_hostlist, list) {
794 if (oldfcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
795 old_real_dev = vlan_dev_real_dev(oldfcoe->netdev);
796 else
797 old_real_dev = oldfcoe->netdev;
799 if (cur_real_dev == old_real_dev) {
800 fcoe->oem = oldfcoe->oem;
801 break;
805 if (fcoe->oem) {
806 if (!fc_exch_mgr_add(lport, fcoe->oem, fcoe_oem_match)) {
807 printk(KERN_ERR "fcoe_em_config: failed to add "
808 "offload em:%p on interface:%s\n",
809 fcoe->oem, fcoe->netdev->name);
810 return -ENOMEM;
812 } else {
813 fcoe->oem = fc_exch_mgr_alloc(lport, FC_CLASS_3,
814 FCOE_MIN_XID, lport->lro_xid,
815 fcoe_oem_match);
816 if (!fcoe->oem) {
817 printk(KERN_ERR "fcoe_em_config: failed to allocate "
818 "em for offload exches on interface:%s\n",
819 fcoe->netdev->name);
820 return -ENOMEM;
825 * Exclude offload EM xid range from next EM xid range.
827 min_xid += lport->lro_xid + 1;
829 skip_oem:
830 if (!fc_exch_mgr_alloc(lport, FC_CLASS_3, min_xid, max_xid, NULL)) {
831 printk(KERN_ERR "fcoe_em_config: failed to "
832 "allocate em on interface %s\n", fcoe->netdev->name);
833 return -ENOMEM;
836 return 0;
840 * fcoe_if_destroy() - Tear down a SW FCoE instance
841 * @lport: The local port to be destroyed
844 static void fcoe_if_destroy(struct fc_lport *lport)
846 /* Free queued packets for the per-CPU receive threads */
847 fcoe_percpu_clean(lport);
849 /* Detach from the scsi-ml */
850 fc_remove_host(lport->host);
851 scsi_remove_host(lport->host);
853 /* Destroy lport scsi_priv */
854 fc_fcp_destroy(lport);
856 /* There are no more rports or I/O, free the EM */
857 fc_exch_mgr_free(lport);
859 /* Free memory used by statistical counters */
860 fc_lport_free_stats(lport);
862 /* Release the Scsi_Host */
863 scsi_host_put(lport->host);
867 * fcoe_ddp_setup() - Call a LLD's ddp_setup through the net device
868 * @lport: The local port to setup DDP for
869 * @xid: The exchange ID for this DDP transfer
870 * @sgl: The scatterlist describing this transfer
871 * @sgc: The number of sg items
873 * Returns: 0 if the DDP context was not configured
875 static int fcoe_ddp_setup(struct fc_lport *lport, u16 xid,
876 struct scatterlist *sgl, unsigned int sgc)
878 struct net_device *netdev = fcoe_netdev(lport);
880 if (netdev->netdev_ops->ndo_fcoe_ddp_setup)
881 return netdev->netdev_ops->ndo_fcoe_ddp_setup(netdev,
882 xid, sgl,
883 sgc);
885 return 0;
889 * fcoe_ddp_done() - Call a LLD's ddp_done through the net device
890 * @lport: The local port to complete DDP on
891 * @xid: The exchange ID for this DDP transfer
893 * Returns: the length of data that have been completed by DDP
895 static int fcoe_ddp_done(struct fc_lport *lport, u16 xid)
897 struct net_device *netdev = fcoe_netdev(lport);
899 if (netdev->netdev_ops->ndo_fcoe_ddp_done)
900 return netdev->netdev_ops->ndo_fcoe_ddp_done(netdev, xid);
901 return 0;
905 * fcoe_if_create() - Create a FCoE instance on an interface
906 * @fcoe: The FCoE interface to create a local port on
907 * @parent: The device pointer to be the parent in sysfs for the SCSI host
908 * @npiv: Indicates if the port is a vport or not
910 * Creates a fc_lport instance and a Scsi_Host instance and configure them.
912 * Returns: The allocated fc_lport or an error pointer
914 static struct fc_lport *fcoe_if_create(struct fcoe_interface *fcoe,
915 struct device *parent, int npiv)
917 struct net_device *netdev = fcoe->netdev;
918 struct fc_lport *lport, *n_port;
919 struct fcoe_port *port;
920 struct Scsi_Host *shost;
921 int rc;
923 * parent is only a vport if npiv is 1,
924 * but we'll only use vport in that case so go ahead and set it
926 struct fc_vport *vport = dev_to_vport(parent);
928 FCOE_NETDEV_DBG(netdev, "Create Interface\n");
930 if (!npiv)
931 lport = libfc_host_alloc(&fcoe_shost_template, sizeof(*port));
932 else
933 lport = libfc_vport_create(vport, sizeof(*port));
935 if (!lport) {
936 FCOE_NETDEV_DBG(netdev, "Could not allocate host structure\n");
937 rc = -ENOMEM;
938 goto out;
940 port = lport_priv(lport);
941 port->lport = lport;
942 port->priv = fcoe;
943 port->max_queue_depth = FCOE_MAX_QUEUE_DEPTH;
944 port->min_queue_depth = FCOE_MIN_QUEUE_DEPTH;
945 INIT_WORK(&port->destroy_work, fcoe_destroy_work);
947 /* configure a fc_lport including the exchange manager */
948 rc = fcoe_lport_config(lport);
949 if (rc) {
950 FCOE_NETDEV_DBG(netdev, "Could not configure lport for the "
951 "interface\n");
952 goto out_host_put;
955 if (npiv) {
956 FCOE_NETDEV_DBG(netdev, "Setting vport names, "
957 "%16.16llx %16.16llx\n",
958 vport->node_name, vport->port_name);
959 fc_set_wwnn(lport, vport->node_name);
960 fc_set_wwpn(lport, vport->port_name);
963 /* configure lport network properties */
964 rc = fcoe_netdev_config(lport, netdev);
965 if (rc) {
966 FCOE_NETDEV_DBG(netdev, "Could not configure netdev for the "
967 "interface\n");
968 goto out_lp_destroy;
971 /* configure lport scsi host properties */
972 rc = fcoe_shost_config(lport, parent);
973 if (rc) {
974 FCOE_NETDEV_DBG(netdev, "Could not configure shost for the "
975 "interface\n");
976 goto out_lp_destroy;
979 /* Initialize the library */
980 rc = fcoe_libfc_config(lport, &fcoe->ctlr, &fcoe_libfc_fcn_templ, 1);
981 if (rc) {
982 FCOE_NETDEV_DBG(netdev, "Could not configure libfc for the "
983 "interface\n");
984 goto out_lp_destroy;
988 * fcoe_em_alloc() and fcoe_hostlist_add() both
989 * need to be atomic with respect to other changes to the
990 * hostlist since fcoe_em_alloc() looks for an existing EM
991 * instance on host list updated by fcoe_hostlist_add().
993 * This is currently handled through the fcoe_config_mutex
994 * begin held.
996 if (!npiv)
997 /* lport exch manager allocation */
998 rc = fcoe_em_config(lport);
999 else {
1000 shost = vport_to_shost(vport);
1001 n_port = shost_priv(shost);
1002 rc = fc_exch_mgr_list_clone(n_port, lport);
1005 if (rc) {
1006 FCOE_NETDEV_DBG(netdev, "Could not configure the EM\n");
1007 goto out_lp_destroy;
1010 fcoe_interface_get(fcoe);
1011 return lport;
1013 out_lp_destroy:
1014 fc_exch_mgr_free(lport);
1015 out_host_put:
1016 scsi_host_put(lport->host);
1017 out:
1018 return ERR_PTR(rc);
1022 * fcoe_if_init() - Initialization routine for fcoe.ko
1024 * Attaches the SW FCoE transport to the FC transport
1026 * Returns: 0 on success
1028 static int __init fcoe_if_init(void)
1030 /* attach to scsi transport */
1031 fcoe_nport_scsi_transport =
1032 fc_attach_transport(&fcoe_nport_fc_functions);
1033 fcoe_vport_scsi_transport =
1034 fc_attach_transport(&fcoe_vport_fc_functions);
1036 if (!fcoe_nport_scsi_transport) {
1037 printk(KERN_ERR "fcoe: Failed to attach to the FC transport\n");
1038 return -ENODEV;
1041 return 0;
1045 * fcoe_if_exit() - Tear down fcoe.ko
1047 * Detaches the SW FCoE transport from the FC transport
1049 * Returns: 0 on success
1051 int __exit fcoe_if_exit(void)
1053 fc_release_transport(fcoe_nport_scsi_transport);
1054 fc_release_transport(fcoe_vport_scsi_transport);
1055 fcoe_nport_scsi_transport = NULL;
1056 fcoe_vport_scsi_transport = NULL;
1057 return 0;
1061 * fcoe_percpu_thread_create() - Create a receive thread for an online CPU
1062 * @cpu: The CPU index of the CPU to create a receive thread for
1064 static void fcoe_percpu_thread_create(unsigned int cpu)
1066 struct fcoe_percpu_s *p;
1067 struct task_struct *thread;
1069 p = &per_cpu(fcoe_percpu, cpu);
1071 thread = kthread_create(fcoe_percpu_receive_thread,
1072 (void *)p, "fcoethread/%d", cpu);
1074 if (likely(!IS_ERR(thread))) {
1075 kthread_bind(thread, cpu);
1076 wake_up_process(thread);
1078 spin_lock_bh(&p->fcoe_rx_list.lock);
1079 p->thread = thread;
1080 spin_unlock_bh(&p->fcoe_rx_list.lock);
1085 * fcoe_percpu_thread_destroy() - Remove the receive thread of a CPU
1086 * @cpu: The CPU index of the CPU whose receive thread is to be destroyed
1088 * Destroys a per-CPU Rx thread. Any pending skbs are moved to the
1089 * current CPU's Rx thread. If the thread being destroyed is bound to
1090 * the CPU processing this context the skbs will be freed.
1092 static void fcoe_percpu_thread_destroy(unsigned int cpu)
1094 struct fcoe_percpu_s *p;
1095 struct task_struct *thread;
1096 struct page *crc_eof;
1097 struct sk_buff *skb;
1098 #ifdef CONFIG_SMP
1099 struct fcoe_percpu_s *p0;
1100 unsigned targ_cpu = get_cpu();
1101 #endif /* CONFIG_SMP */
1103 FCOE_DBG("Destroying receive thread for CPU %d\n", cpu);
1105 /* Prevent any new skbs from being queued for this CPU. */
1106 p = &per_cpu(fcoe_percpu, cpu);
1107 spin_lock_bh(&p->fcoe_rx_list.lock);
1108 thread = p->thread;
1109 p->thread = NULL;
1110 crc_eof = p->crc_eof_page;
1111 p->crc_eof_page = NULL;
1112 p->crc_eof_offset = 0;
1113 spin_unlock_bh(&p->fcoe_rx_list.lock);
1115 #ifdef CONFIG_SMP
1117 * Don't bother moving the skb's if this context is running
1118 * on the same CPU that is having its thread destroyed. This
1119 * can easily happen when the module is removed.
1121 if (cpu != targ_cpu) {
1122 p0 = &per_cpu(fcoe_percpu, targ_cpu);
1123 spin_lock_bh(&p0->fcoe_rx_list.lock);
1124 if (p0->thread) {
1125 FCOE_DBG("Moving frames from CPU %d to CPU %d\n",
1126 cpu, targ_cpu);
1128 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1129 __skb_queue_tail(&p0->fcoe_rx_list, skb);
1130 spin_unlock_bh(&p0->fcoe_rx_list.lock);
1131 } else {
1133 * The targeted CPU is not initialized and cannot accept
1134 * new skbs. Unlock the targeted CPU and drop the skbs
1135 * on the CPU that is going offline.
1137 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1138 kfree_skb(skb);
1139 spin_unlock_bh(&p0->fcoe_rx_list.lock);
1141 } else {
1143 * This scenario occurs when the module is being removed
1144 * and all threads are being destroyed. skbs will continue
1145 * to be shifted from the CPU thread that is being removed
1146 * to the CPU thread associated with the CPU that is processing
1147 * the module removal. Once there is only one CPU Rx thread it
1148 * will reach this case and we will drop all skbs and later
1149 * stop the thread.
1151 spin_lock_bh(&p->fcoe_rx_list.lock);
1152 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1153 kfree_skb(skb);
1154 spin_unlock_bh(&p->fcoe_rx_list.lock);
1156 put_cpu();
1157 #else
1159 * This a non-SMP scenario where the singular Rx thread is
1160 * being removed. Free all skbs and stop the thread.
1162 spin_lock_bh(&p->fcoe_rx_list.lock);
1163 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1164 kfree_skb(skb);
1165 spin_unlock_bh(&p->fcoe_rx_list.lock);
1166 #endif
1168 if (thread)
1169 kthread_stop(thread);
1171 if (crc_eof)
1172 put_page(crc_eof);
1176 * fcoe_cpu_callback() - Handler for CPU hotplug events
1177 * @nfb: The callback data block
1178 * @action: The event triggering the callback
1179 * @hcpu: The index of the CPU that the event is for
1181 * This creates or destroys per-CPU data for fcoe
1183 * Returns NOTIFY_OK always.
1185 static int fcoe_cpu_callback(struct notifier_block *nfb,
1186 unsigned long action, void *hcpu)
1188 unsigned cpu = (unsigned long)hcpu;
1190 switch (action) {
1191 case CPU_ONLINE:
1192 case CPU_ONLINE_FROZEN:
1193 FCOE_DBG("CPU %x online: Create Rx thread\n", cpu);
1194 fcoe_percpu_thread_create(cpu);
1195 break;
1196 case CPU_DEAD:
1197 case CPU_DEAD_FROZEN:
1198 FCOE_DBG("CPU %x offline: Remove Rx thread\n", cpu);
1199 fcoe_percpu_thread_destroy(cpu);
1200 break;
1201 default:
1202 break;
1204 return NOTIFY_OK;
1208 * fcoe_rcv() - Receive packets from a net device
1209 * @skb: The received packet
1210 * @netdev: The net device that the packet was received on
1211 * @ptype: The packet type context
1212 * @olddev: The last device net device
1214 * This routine is called by NET_RX_SOFTIRQ. It receives a packet, builds a
1215 * FC frame and passes the frame to libfc.
1217 * Returns: 0 for success
1219 int fcoe_rcv(struct sk_buff *skb, struct net_device *netdev,
1220 struct packet_type *ptype, struct net_device *olddev)
1222 struct fc_lport *lport;
1223 struct fcoe_rcv_info *fr;
1224 struct fcoe_interface *fcoe;
1225 struct fc_frame_header *fh;
1226 struct fcoe_percpu_s *fps;
1227 struct ethhdr *eh;
1228 unsigned int cpu;
1230 fcoe = container_of(ptype, struct fcoe_interface, fcoe_packet_type);
1231 lport = fcoe->ctlr.lp;
1232 if (unlikely(!lport)) {
1233 FCOE_NETDEV_DBG(netdev, "Cannot find hba structure");
1234 goto err2;
1236 if (!lport->link_up)
1237 goto err2;
1239 FCOE_NETDEV_DBG(netdev, "skb_info: len:%d data_len:%d head:%p "
1240 "data:%p tail:%p end:%p sum:%d dev:%s",
1241 skb->len, skb->data_len, skb->head, skb->data,
1242 skb_tail_pointer(skb), skb_end_pointer(skb),
1243 skb->csum, skb->dev ? skb->dev->name : "<NULL>");
1245 eh = eth_hdr(skb);
1247 if (is_fip_mode(&fcoe->ctlr) &&
1248 compare_ether_addr(eh->h_source, fcoe->ctlr.dest_addr)) {
1249 FCOE_NETDEV_DBG(netdev, "wrong source mac address:%pM\n",
1250 eh->h_source);
1251 goto err;
1255 * Check for minimum frame length, and make sure required FCoE
1256 * and FC headers are pulled into the linear data area.
1258 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
1259 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
1260 goto err;
1262 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
1263 fh = (struct fc_frame_header *) skb_transport_header(skb);
1265 if (ntoh24(&eh->h_dest[3]) != ntoh24(fh->fh_d_id)) {
1266 FCOE_NETDEV_DBG(netdev, "FC frame d_id mismatch with MAC:%pM\n",
1267 eh->h_dest);
1268 goto err;
1271 fr = fcoe_dev_from_skb(skb);
1272 fr->fr_dev = lport;
1273 fr->ptype = ptype;
1276 * In case the incoming frame's exchange is originated from
1277 * the initiator, then received frame's exchange id is ANDed
1278 * with fc_cpu_mask bits to get the same cpu on which exchange
1279 * was originated, otherwise just use the current cpu.
1281 if (ntoh24(fh->fh_f_ctl) & FC_FC_EX_CTX)
1282 cpu = ntohs(fh->fh_ox_id) & fc_cpu_mask;
1283 else
1284 cpu = smp_processor_id();
1286 fps = &per_cpu(fcoe_percpu, cpu);
1287 spin_lock_bh(&fps->fcoe_rx_list.lock);
1288 if (unlikely(!fps->thread)) {
1290 * The targeted CPU is not ready, let's target
1291 * the first CPU now. For non-SMP systems this
1292 * will check the same CPU twice.
1294 FCOE_NETDEV_DBG(netdev, "CPU is online, but no receive thread "
1295 "ready for incoming skb- using first online "
1296 "CPU.\n");
1298 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1299 cpu = cpumask_first(cpu_online_mask);
1300 fps = &per_cpu(fcoe_percpu, cpu);
1301 spin_lock_bh(&fps->fcoe_rx_list.lock);
1302 if (!fps->thread) {
1303 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1304 goto err;
1309 * We now have a valid CPU that we're targeting for
1310 * this skb. We also have this receive thread locked,
1311 * so we're free to queue skbs into it's queue.
1314 /* If this is a SCSI-FCP frame, and this is already executing on the
1315 * correct CPU, and the queue for this CPU is empty, then go ahead
1316 * and process the frame directly in the softirq context.
1317 * This lets us process completions without context switching from the
1318 * NET_RX softirq, to our receive processing thread, and then back to
1319 * BLOCK softirq context.
1321 if (fh->fh_type == FC_TYPE_FCP &&
1322 cpu == smp_processor_id() &&
1323 skb_queue_empty(&fps->fcoe_rx_list)) {
1324 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1325 fcoe_recv_frame(skb);
1326 } else {
1327 __skb_queue_tail(&fps->fcoe_rx_list, skb);
1328 if (fps->fcoe_rx_list.qlen == 1)
1329 wake_up_process(fps->thread);
1330 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1333 return 0;
1334 err:
1335 per_cpu_ptr(lport->dev_stats, get_cpu())->ErrorFrames++;
1336 put_cpu();
1337 err2:
1338 kfree_skb(skb);
1339 return -1;
1343 * fcoe_alloc_paged_crc_eof() - Allocate a page to be used for the trailer CRC
1344 * @skb: The packet to be transmitted
1345 * @tlen: The total length of the trailer
1347 * Returns: 0 for success
1349 static int fcoe_alloc_paged_crc_eof(struct sk_buff *skb, int tlen)
1351 struct fcoe_percpu_s *fps;
1352 int rc;
1354 fps = &get_cpu_var(fcoe_percpu);
1355 rc = fcoe_get_paged_crc_eof(skb, tlen, fps);
1356 put_cpu_var(fcoe_percpu);
1358 return rc;
1362 * fcoe_xmit() - Transmit a FCoE frame
1363 * @lport: The local port that the frame is to be transmitted for
1364 * @fp: The frame to be transmitted
1366 * Return: 0 for success
1368 int fcoe_xmit(struct fc_lport *lport, struct fc_frame *fp)
1370 int wlen;
1371 u32 crc;
1372 struct ethhdr *eh;
1373 struct fcoe_crc_eof *cp;
1374 struct sk_buff *skb;
1375 struct fcoe_dev_stats *stats;
1376 struct fc_frame_header *fh;
1377 unsigned int hlen; /* header length implies the version */
1378 unsigned int tlen; /* trailer length */
1379 unsigned int elen; /* eth header, may include vlan */
1380 struct fcoe_port *port = lport_priv(lport);
1381 struct fcoe_interface *fcoe = port->priv;
1382 u8 sof, eof;
1383 struct fcoe_hdr *hp;
1385 WARN_ON((fr_len(fp) % sizeof(u32)) != 0);
1387 fh = fc_frame_header_get(fp);
1388 skb = fp_skb(fp);
1389 wlen = skb->len / FCOE_WORD_TO_BYTE;
1391 if (!lport->link_up) {
1392 kfree_skb(skb);
1393 return 0;
1396 if (unlikely(fh->fh_type == FC_TYPE_ELS) &&
1397 fcoe_ctlr_els_send(&fcoe->ctlr, lport, skb))
1398 return 0;
1400 sof = fr_sof(fp);
1401 eof = fr_eof(fp);
1403 elen = sizeof(struct ethhdr);
1404 hlen = sizeof(struct fcoe_hdr);
1405 tlen = sizeof(struct fcoe_crc_eof);
1406 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
1408 /* crc offload */
1409 if (likely(lport->crc_offload)) {
1410 skb->ip_summed = CHECKSUM_PARTIAL;
1411 skb->csum_start = skb_headroom(skb);
1412 skb->csum_offset = skb->len;
1413 crc = 0;
1414 } else {
1415 skb->ip_summed = CHECKSUM_NONE;
1416 crc = fcoe_fc_crc(fp);
1419 /* copy port crc and eof to the skb buff */
1420 if (skb_is_nonlinear(skb)) {
1421 skb_frag_t *frag;
1422 if (fcoe_alloc_paged_crc_eof(skb, tlen)) {
1423 kfree_skb(skb);
1424 return -ENOMEM;
1426 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
1427 cp = kmap_atomic(frag->page, KM_SKB_DATA_SOFTIRQ)
1428 + frag->page_offset;
1429 } else {
1430 cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
1433 memset(cp, 0, sizeof(*cp));
1434 cp->fcoe_eof = eof;
1435 cp->fcoe_crc32 = cpu_to_le32(~crc);
1437 if (skb_is_nonlinear(skb)) {
1438 kunmap_atomic(cp, KM_SKB_DATA_SOFTIRQ);
1439 cp = NULL;
1442 /* adjust skb network/transport offsets to match mac/fcoe/port */
1443 skb_push(skb, elen + hlen);
1444 skb_reset_mac_header(skb);
1445 skb_reset_network_header(skb);
1446 skb->mac_len = elen;
1447 skb->protocol = htons(ETH_P_FCOE);
1448 skb->dev = fcoe->netdev;
1450 /* fill up mac and fcoe headers */
1451 eh = eth_hdr(skb);
1452 eh->h_proto = htons(ETH_P_FCOE);
1453 memcpy(eh->h_dest, fcoe->ctlr.dest_addr, ETH_ALEN);
1454 if (fcoe->ctlr.map_dest)
1455 memcpy(eh->h_dest + 3, fh->fh_d_id, 3);
1457 if (unlikely(fcoe->ctlr.flogi_oxid != FC_XID_UNKNOWN))
1458 memcpy(eh->h_source, fcoe->ctlr.ctl_src_addr, ETH_ALEN);
1459 else
1460 memcpy(eh->h_source, port->data_src_addr, ETH_ALEN);
1462 hp = (struct fcoe_hdr *)(eh + 1);
1463 memset(hp, 0, sizeof(*hp));
1464 if (FC_FCOE_VER)
1465 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
1466 hp->fcoe_sof = sof;
1468 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
1469 if (lport->seq_offload && fr_max_payload(fp)) {
1470 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
1471 skb_shinfo(skb)->gso_size = fr_max_payload(fp);
1472 } else {
1473 skb_shinfo(skb)->gso_type = 0;
1474 skb_shinfo(skb)->gso_size = 0;
1476 /* update tx stats: regardless if LLD fails */
1477 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
1478 stats->TxFrames++;
1479 stats->TxWords += wlen;
1480 put_cpu();
1482 /* send down to lld */
1483 fr_dev(fp) = lport;
1484 if (port->fcoe_pending_queue.qlen)
1485 fcoe_check_wait_queue(lport, skb);
1486 else if (fcoe_start_io(skb))
1487 fcoe_check_wait_queue(lport, skb);
1489 return 0;
1493 * fcoe_percpu_flush_done() - Indicate per-CPU queue flush completion
1494 * @skb: The completed skb (argument required by destructor)
1496 static void fcoe_percpu_flush_done(struct sk_buff *skb)
1498 complete(&fcoe_flush_completion);
1502 * fcoe_filter_frames() - filter out bad fcoe frames, i.e. bad CRC
1503 * @lport: The local port the frame was received on
1504 * @fp: The received frame
1506 * Return: 0 on passing filtering checks
1508 static inline int fcoe_filter_frames(struct fc_lport *lport,
1509 struct fc_frame *fp)
1511 struct fcoe_interface *fcoe;
1512 struct fc_frame_header *fh;
1513 struct sk_buff *skb = (struct sk_buff *)fp;
1514 struct fcoe_dev_stats *stats;
1517 * We only check CRC if no offload is available and if it is
1518 * it's solicited data, in which case, the FCP layer would
1519 * check it during the copy.
1521 if (lport->crc_offload && skb->ip_summed == CHECKSUM_UNNECESSARY)
1522 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1523 else
1524 fr_flags(fp) |= FCPHF_CRC_UNCHECKED;
1526 fh = (struct fc_frame_header *) skb_transport_header(skb);
1527 fh = fc_frame_header_get(fp);
1528 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA && fh->fh_type == FC_TYPE_FCP)
1529 return 0;
1531 fcoe = ((struct fcoe_port *)lport_priv(lport))->priv;
1532 if (is_fip_mode(&fcoe->ctlr) && fc_frame_payload_op(fp) == ELS_LOGO &&
1533 ntoh24(fh->fh_s_id) == FC_FID_FLOGI) {
1534 FCOE_DBG("fcoe: dropping FCoE lport LOGO in fip mode\n");
1535 return -EINVAL;
1538 if (!(fr_flags(fp) & FCPHF_CRC_UNCHECKED) ||
1539 le32_to_cpu(fr_crc(fp)) == ~crc32(~0, skb->data, skb->len)) {
1540 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1541 return 0;
1544 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
1545 stats->InvalidCRCCount++;
1546 if (stats->InvalidCRCCount < 5)
1547 printk(KERN_WARNING "fcoe: dropping frame with CRC error\n");
1548 return -EINVAL;
1552 * fcoe_recv_frame() - process a single received frame
1553 * @skb: frame to process
1555 static void fcoe_recv_frame(struct sk_buff *skb)
1557 u32 fr_len;
1558 struct fc_lport *lport;
1559 struct fcoe_rcv_info *fr;
1560 struct fcoe_dev_stats *stats;
1561 struct fcoe_crc_eof crc_eof;
1562 struct fc_frame *fp;
1563 struct fcoe_port *port;
1564 struct fcoe_hdr *hp;
1566 fr = fcoe_dev_from_skb(skb);
1567 lport = fr->fr_dev;
1568 if (unlikely(!lport)) {
1569 if (skb->destructor != fcoe_percpu_flush_done)
1570 FCOE_NETDEV_DBG(skb->dev, "NULL lport in skb");
1571 kfree_skb(skb);
1572 return;
1575 FCOE_NETDEV_DBG(skb->dev, "skb_info: len:%d data_len:%d "
1576 "head:%p data:%p tail:%p end:%p sum:%d dev:%s",
1577 skb->len, skb->data_len,
1578 skb->head, skb->data, skb_tail_pointer(skb),
1579 skb_end_pointer(skb), skb->csum,
1580 skb->dev ? skb->dev->name : "<NULL>");
1582 port = lport_priv(lport);
1583 if (skb_is_nonlinear(skb))
1584 skb_linearize(skb); /* not ideal */
1587 * Frame length checks and setting up the header pointers
1588 * was done in fcoe_rcv already.
1590 hp = (struct fcoe_hdr *) skb_network_header(skb);
1592 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
1593 if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) {
1594 if (stats->ErrorFrames < 5)
1595 printk(KERN_WARNING "fcoe: FCoE version "
1596 "mismatch: The frame has "
1597 "version %x, but the "
1598 "initiator supports version "
1599 "%x\n", FC_FCOE_DECAPS_VER(hp),
1600 FC_FCOE_VER);
1601 goto drop;
1604 skb_pull(skb, sizeof(struct fcoe_hdr));
1605 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
1607 stats->RxFrames++;
1608 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
1610 fp = (struct fc_frame *)skb;
1611 fc_frame_init(fp);
1612 fr_dev(fp) = lport;
1613 fr_sof(fp) = hp->fcoe_sof;
1615 /* Copy out the CRC and EOF trailer for access */
1616 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof)))
1617 goto drop;
1618 fr_eof(fp) = crc_eof.fcoe_eof;
1619 fr_crc(fp) = crc_eof.fcoe_crc32;
1620 if (pskb_trim(skb, fr_len))
1621 goto drop;
1623 if (!fcoe_filter_frames(lport, fp)) {
1624 put_cpu();
1625 fc_exch_recv(lport, fp);
1626 return;
1628 drop:
1629 stats->ErrorFrames++;
1630 put_cpu();
1631 kfree_skb(skb);
1635 * fcoe_percpu_receive_thread() - The per-CPU packet receive thread
1636 * @arg: The per-CPU context
1638 * Return: 0 for success
1640 int fcoe_percpu_receive_thread(void *arg)
1642 struct fcoe_percpu_s *p = arg;
1643 struct sk_buff *skb;
1645 set_user_nice(current, -20);
1647 while (!kthread_should_stop()) {
1649 spin_lock_bh(&p->fcoe_rx_list.lock);
1650 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) == NULL) {
1651 set_current_state(TASK_INTERRUPTIBLE);
1652 spin_unlock_bh(&p->fcoe_rx_list.lock);
1653 schedule();
1654 set_current_state(TASK_RUNNING);
1655 if (kthread_should_stop())
1656 return 0;
1657 spin_lock_bh(&p->fcoe_rx_list.lock);
1659 spin_unlock_bh(&p->fcoe_rx_list.lock);
1660 fcoe_recv_frame(skb);
1662 return 0;
1666 * fcoe_dev_setup() - Setup the link change notification interface
1668 static void fcoe_dev_setup(void)
1670 register_netdevice_notifier(&fcoe_notifier);
1674 * fcoe_dev_cleanup() - Cleanup the link change notification interface
1676 static void fcoe_dev_cleanup(void)
1678 unregister_netdevice_notifier(&fcoe_notifier);
1682 * fcoe_device_notification() - Handler for net device events
1683 * @notifier: The context of the notification
1684 * @event: The type of event
1685 * @ptr: The net device that the event was on
1687 * This function is called by the Ethernet driver in case of link change event.
1689 * Returns: 0 for success
1691 static int fcoe_device_notification(struct notifier_block *notifier,
1692 ulong event, void *ptr)
1694 struct fc_lport *lport = NULL;
1695 struct net_device *netdev = ptr;
1696 struct fcoe_interface *fcoe;
1697 struct fcoe_port *port;
1698 struct fcoe_dev_stats *stats;
1699 u32 link_possible = 1;
1700 u32 mfs;
1701 int rc = NOTIFY_OK;
1703 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
1704 if (fcoe->netdev == netdev) {
1705 lport = fcoe->ctlr.lp;
1706 break;
1709 if (!lport) {
1710 rc = NOTIFY_DONE;
1711 goto out;
1714 switch (event) {
1715 case NETDEV_DOWN:
1716 case NETDEV_GOING_DOWN:
1717 link_possible = 0;
1718 break;
1719 case NETDEV_UP:
1720 case NETDEV_CHANGE:
1721 break;
1722 case NETDEV_CHANGEMTU:
1723 if (netdev->features & NETIF_F_FCOE_MTU)
1724 break;
1725 mfs = netdev->mtu - (sizeof(struct fcoe_hdr) +
1726 sizeof(struct fcoe_crc_eof));
1727 if (mfs >= FC_MIN_MAX_FRAME)
1728 fc_set_mfs(lport, mfs);
1729 break;
1730 case NETDEV_REGISTER:
1731 break;
1732 case NETDEV_UNREGISTER:
1733 list_del(&fcoe->list);
1734 port = lport_priv(fcoe->ctlr.lp);
1735 fcoe_interface_cleanup(fcoe);
1736 queue_work(fcoe_wq, &port->destroy_work);
1737 goto out;
1738 break;
1739 case NETDEV_FEAT_CHANGE:
1740 fcoe_netdev_features_change(lport, netdev);
1741 break;
1742 default:
1743 FCOE_NETDEV_DBG(netdev, "Unknown event %ld "
1744 "from netdev netlink\n", event);
1747 fcoe_link_speed_update(lport);
1749 if (link_possible && !fcoe_link_ok(lport))
1750 fcoe_ctlr_link_up(&fcoe->ctlr);
1751 else if (fcoe_ctlr_link_down(&fcoe->ctlr)) {
1752 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
1753 stats->LinkFailureCount++;
1754 put_cpu();
1755 fcoe_clean_pending_queue(lport);
1757 out:
1758 return rc;
1762 * fcoe_disable() - Disables a FCoE interface
1763 * @netdev : The net_device object the Ethernet interface to create on
1765 * Called from fcoe transport.
1767 * Returns: 0 for success
1769 static int fcoe_disable(struct net_device *netdev)
1771 struct fcoe_interface *fcoe;
1772 int rc = 0;
1774 mutex_lock(&fcoe_config_mutex);
1776 rtnl_lock();
1777 fcoe = fcoe_hostlist_lookup_port(netdev);
1778 rtnl_unlock();
1780 if (fcoe) {
1781 fcoe_ctlr_link_down(&fcoe->ctlr);
1782 fcoe_clean_pending_queue(fcoe->ctlr.lp);
1783 } else
1784 rc = -ENODEV;
1786 mutex_unlock(&fcoe_config_mutex);
1787 return rc;
1791 * fcoe_enable() - Enables a FCoE interface
1792 * @netdev : The net_device object the Ethernet interface to create on
1794 * Called from fcoe transport.
1796 * Returns: 0 for success
1798 static int fcoe_enable(struct net_device *netdev)
1800 struct fcoe_interface *fcoe;
1801 int rc = 0;
1803 mutex_lock(&fcoe_config_mutex);
1804 rtnl_lock();
1805 fcoe = fcoe_hostlist_lookup_port(netdev);
1806 rtnl_unlock();
1808 if (!fcoe)
1809 rc = -ENODEV;
1810 else if (!fcoe_link_ok(fcoe->ctlr.lp))
1811 fcoe_ctlr_link_up(&fcoe->ctlr);
1813 mutex_unlock(&fcoe_config_mutex);
1814 return rc;
1818 * fcoe_destroy() - Destroy 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_destroy(struct net_device *netdev)
1827 struct fcoe_interface *fcoe;
1828 struct fc_lport *lport;
1829 int rc = 0;
1831 mutex_lock(&fcoe_config_mutex);
1832 rtnl_lock();
1833 fcoe = fcoe_hostlist_lookup_port(netdev);
1834 if (!fcoe) {
1835 rtnl_unlock();
1836 rc = -ENODEV;
1837 goto out_nodev;
1839 lport = fcoe->ctlr.lp;
1840 list_del(&fcoe->list);
1841 fcoe_interface_cleanup(fcoe);
1842 rtnl_unlock();
1843 fcoe_if_destroy(lport);
1844 out_nodev:
1845 mutex_unlock(&fcoe_config_mutex);
1846 return rc;
1850 * fcoe_destroy_work() - Destroy a FCoE port in a deferred work context
1851 * @work: Handle to the FCoE port to be destroyed
1853 static void fcoe_destroy_work(struct work_struct *work)
1855 struct fcoe_port *port;
1857 port = container_of(work, struct fcoe_port, destroy_work);
1858 mutex_lock(&fcoe_config_mutex);
1859 fcoe_if_destroy(port->lport);
1860 mutex_unlock(&fcoe_config_mutex);
1864 * fcoe_match() - Check if the FCoE is supported on the given netdevice
1865 * @netdev : The net_device object the Ethernet interface to create on
1867 * Called from fcoe transport.
1869 * Returns: always returns true as this is the default FCoE transport,
1870 * i.e., support all netdevs.
1872 static bool fcoe_match(struct net_device *netdev)
1874 return true;
1878 * fcoe_create() - Create a fcoe interface
1879 * @netdev : The net_device object the Ethernet interface to create on
1880 * @fip_mode: The FIP mode for this creation
1882 * Called from fcoe transport
1884 * Returns: 0 for success
1886 static int fcoe_create(struct net_device *netdev, enum fip_state fip_mode)
1888 int rc;
1889 struct fcoe_interface *fcoe;
1890 struct fc_lport *lport;
1892 mutex_lock(&fcoe_config_mutex);
1893 rtnl_lock();
1895 /* look for existing lport */
1896 if (fcoe_hostlist_lookup(netdev)) {
1897 rc = -EEXIST;
1898 goto out_nodev;
1901 fcoe = fcoe_interface_create(netdev, fip_mode);
1902 if (IS_ERR(fcoe)) {
1903 rc = PTR_ERR(fcoe);
1904 goto out_nodev;
1907 lport = fcoe_if_create(fcoe, &netdev->dev, 0);
1908 if (IS_ERR(lport)) {
1909 printk(KERN_ERR "fcoe: Failed to create interface (%s)\n",
1910 netdev->name);
1911 rc = -EIO;
1912 fcoe_interface_cleanup(fcoe);
1913 goto out_free;
1916 /* Make this the "master" N_Port */
1917 fcoe->ctlr.lp = lport;
1919 /* add to lports list */
1920 fcoe_hostlist_add(lport);
1922 /* start FIP Discovery and FLOGI */
1923 lport->boot_time = jiffies;
1924 fc_fabric_login(lport);
1925 if (!fcoe_link_ok(lport))
1926 fcoe_ctlr_link_up(&fcoe->ctlr);
1929 * Release from init in fcoe_interface_create(), on success lport
1930 * should be holding a reference taken in fcoe_if_create().
1932 fcoe_interface_put(fcoe);
1933 rtnl_unlock();
1934 mutex_unlock(&fcoe_config_mutex);
1936 return 0;
1937 out_free:
1938 fcoe_interface_put(fcoe);
1939 out_nodev:
1940 rtnl_unlock();
1941 mutex_unlock(&fcoe_config_mutex);
1942 return rc;
1946 * fcoe_link_speed_update() - Update the supported and actual link speeds
1947 * @lport: The local port to update speeds for
1949 * Returns: 0 if the ethtool query was successful
1950 * -1 if the ethtool query failed
1952 int fcoe_link_speed_update(struct fc_lport *lport)
1954 struct net_device *netdev = fcoe_netdev(lport);
1955 struct ethtool_cmd ecmd;
1957 if (!dev_ethtool_get_settings(netdev, &ecmd)) {
1958 lport->link_supported_speeds &=
1959 ~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
1960 if (ecmd.supported & (SUPPORTED_1000baseT_Half |
1961 SUPPORTED_1000baseT_Full))
1962 lport->link_supported_speeds |= FC_PORTSPEED_1GBIT;
1963 if (ecmd.supported & SUPPORTED_10000baseT_Full)
1964 lport->link_supported_speeds |=
1965 FC_PORTSPEED_10GBIT;
1966 switch (ethtool_cmd_speed(&ecmd)) {
1967 case SPEED_1000:
1968 lport->link_speed = FC_PORTSPEED_1GBIT;
1969 break;
1970 case SPEED_10000:
1971 lport->link_speed = FC_PORTSPEED_10GBIT;
1972 break;
1974 return 0;
1976 return -1;
1980 * fcoe_link_ok() - Check if the link is OK for a local port
1981 * @lport: The local port to check link on
1983 * Returns: 0 if link is UP and OK, -1 if not
1986 int fcoe_link_ok(struct fc_lport *lport)
1988 struct net_device *netdev = fcoe_netdev(lport);
1990 if (netif_oper_up(netdev))
1991 return 0;
1992 return -1;
1996 * fcoe_percpu_clean() - Clear all pending skbs for an local port
1997 * @lport: The local port whose skbs are to be cleared
1999 * Must be called with fcoe_create_mutex held to single-thread completion.
2001 * This flushes the pending skbs by adding a new skb to each queue and
2002 * waiting until they are all freed. This assures us that not only are
2003 * there no packets that will be handled by the lport, but also that any
2004 * threads already handling packet have returned.
2006 void fcoe_percpu_clean(struct fc_lport *lport)
2008 struct fcoe_percpu_s *pp;
2009 struct fcoe_rcv_info *fr;
2010 struct sk_buff_head *list;
2011 struct sk_buff *skb, *next;
2012 struct sk_buff *head;
2013 unsigned int cpu;
2015 for_each_possible_cpu(cpu) {
2016 pp = &per_cpu(fcoe_percpu, cpu);
2017 spin_lock_bh(&pp->fcoe_rx_list.lock);
2018 list = &pp->fcoe_rx_list;
2019 head = list->next;
2020 for (skb = head; skb != (struct sk_buff *)list;
2021 skb = next) {
2022 next = skb->next;
2023 fr = fcoe_dev_from_skb(skb);
2024 if (fr->fr_dev == lport) {
2025 __skb_unlink(skb, list);
2026 kfree_skb(skb);
2030 if (!pp->thread || !cpu_online(cpu)) {
2031 spin_unlock_bh(&pp->fcoe_rx_list.lock);
2032 continue;
2035 skb = dev_alloc_skb(0);
2036 if (!skb) {
2037 spin_unlock_bh(&pp->fcoe_rx_list.lock);
2038 continue;
2040 skb->destructor = fcoe_percpu_flush_done;
2042 __skb_queue_tail(&pp->fcoe_rx_list, skb);
2043 if (pp->fcoe_rx_list.qlen == 1)
2044 wake_up_process(pp->thread);
2045 spin_unlock_bh(&pp->fcoe_rx_list.lock);
2047 wait_for_completion(&fcoe_flush_completion);
2052 * fcoe_reset() - Reset a local port
2053 * @shost: The SCSI host associated with the local port to be reset
2055 * Returns: Always 0 (return value required by FC transport template)
2057 int fcoe_reset(struct Scsi_Host *shost)
2059 struct fc_lport *lport = shost_priv(shost);
2060 struct fcoe_port *port = lport_priv(lport);
2061 struct fcoe_interface *fcoe = port->priv;
2063 fcoe_ctlr_link_down(&fcoe->ctlr);
2064 fcoe_clean_pending_queue(fcoe->ctlr.lp);
2065 if (!fcoe_link_ok(fcoe->ctlr.lp))
2066 fcoe_ctlr_link_up(&fcoe->ctlr);
2067 return 0;
2071 * fcoe_hostlist_lookup_port() - Find the FCoE interface associated with a net device
2072 * @netdev: The net device used as a key
2074 * Locking: Must be called with the RNL mutex held.
2076 * Returns: NULL or the FCoE interface
2078 static struct fcoe_interface *
2079 fcoe_hostlist_lookup_port(const struct net_device *netdev)
2081 struct fcoe_interface *fcoe;
2083 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
2084 if (fcoe->netdev == netdev)
2085 return fcoe;
2087 return NULL;
2091 * fcoe_hostlist_lookup() - Find the local port associated with a
2092 * given net device
2093 * @netdev: The netdevice used as a key
2095 * Locking: Must be called with the RTNL mutex held
2097 * Returns: NULL or the local port
2099 static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *netdev)
2101 struct fcoe_interface *fcoe;
2103 fcoe = fcoe_hostlist_lookup_port(netdev);
2104 return (fcoe) ? fcoe->ctlr.lp : NULL;
2108 * fcoe_hostlist_add() - Add the FCoE interface identified by a local
2109 * port to the hostlist
2110 * @lport: The local port that identifies the FCoE interface to be added
2112 * Locking: must be called with the RTNL mutex held
2114 * Returns: 0 for success
2116 static int fcoe_hostlist_add(const struct fc_lport *lport)
2118 struct fcoe_interface *fcoe;
2119 struct fcoe_port *port;
2121 fcoe = fcoe_hostlist_lookup_port(fcoe_netdev(lport));
2122 if (!fcoe) {
2123 port = lport_priv(lport);
2124 fcoe = port->priv;
2125 list_add_tail(&fcoe->list, &fcoe_hostlist);
2127 return 0;
2131 static struct fcoe_transport fcoe_sw_transport = {
2132 .name = {FCOE_TRANSPORT_DEFAULT},
2133 .attached = false,
2134 .list = LIST_HEAD_INIT(fcoe_sw_transport.list),
2135 .match = fcoe_match,
2136 .create = fcoe_create,
2137 .destroy = fcoe_destroy,
2138 .enable = fcoe_enable,
2139 .disable = fcoe_disable,
2143 * fcoe_init() - Initialize fcoe.ko
2145 * Returns: 0 on success, or a negative value on failure
2147 static int __init fcoe_init(void)
2149 struct fcoe_percpu_s *p;
2150 unsigned int cpu;
2151 int rc = 0;
2153 fcoe_wq = alloc_workqueue("fcoe", 0, 0);
2154 if (!fcoe_wq)
2155 return -ENOMEM;
2157 /* register as a fcoe transport */
2158 rc = fcoe_transport_attach(&fcoe_sw_transport);
2159 if (rc) {
2160 printk(KERN_ERR "failed to register an fcoe transport, check "
2161 "if libfcoe is loaded\n");
2162 return rc;
2165 mutex_lock(&fcoe_config_mutex);
2167 for_each_possible_cpu(cpu) {
2168 p = &per_cpu(fcoe_percpu, cpu);
2169 skb_queue_head_init(&p->fcoe_rx_list);
2172 for_each_online_cpu(cpu)
2173 fcoe_percpu_thread_create(cpu);
2175 /* Initialize per CPU interrupt thread */
2176 rc = register_hotcpu_notifier(&fcoe_cpu_notifier);
2177 if (rc)
2178 goto out_free;
2180 /* Setup link change notification */
2181 fcoe_dev_setup();
2183 rc = fcoe_if_init();
2184 if (rc)
2185 goto out_free;
2187 mutex_unlock(&fcoe_config_mutex);
2188 return 0;
2190 out_free:
2191 for_each_online_cpu(cpu) {
2192 fcoe_percpu_thread_destroy(cpu);
2194 mutex_unlock(&fcoe_config_mutex);
2195 destroy_workqueue(fcoe_wq);
2196 return rc;
2198 module_init(fcoe_init);
2201 * fcoe_exit() - Clean up fcoe.ko
2203 * Returns: 0 on success or a negative value on failure
2205 static void __exit fcoe_exit(void)
2207 struct fcoe_interface *fcoe, *tmp;
2208 struct fcoe_port *port;
2209 unsigned int cpu;
2211 mutex_lock(&fcoe_config_mutex);
2213 fcoe_dev_cleanup();
2215 /* releases the associated fcoe hosts */
2216 rtnl_lock();
2217 list_for_each_entry_safe(fcoe, tmp, &fcoe_hostlist, list) {
2218 list_del(&fcoe->list);
2219 port = lport_priv(fcoe->ctlr.lp);
2220 fcoe_interface_cleanup(fcoe);
2221 queue_work(fcoe_wq, &port->destroy_work);
2223 rtnl_unlock();
2225 unregister_hotcpu_notifier(&fcoe_cpu_notifier);
2227 for_each_online_cpu(cpu)
2228 fcoe_percpu_thread_destroy(cpu);
2230 mutex_unlock(&fcoe_config_mutex);
2233 * destroy_work's may be chained but destroy_workqueue()
2234 * can take care of them. Just kill the fcoe_wq.
2236 destroy_workqueue(fcoe_wq);
2239 * Detaching from the scsi transport must happen after all
2240 * destroys are done on the fcoe_wq. destroy_workqueue will
2241 * enusre the fcoe_wq is flushed.
2243 fcoe_if_exit();
2245 /* detach from fcoe transport */
2246 fcoe_transport_detach(&fcoe_sw_transport);
2248 module_exit(fcoe_exit);
2251 * fcoe_flogi_resp() - FCoE specific FLOGI and FDISC response handler
2252 * @seq: active sequence in the FLOGI or FDISC exchange
2253 * @fp: response frame, or error encoded in a pointer (timeout)
2254 * @arg: pointer the the fcoe_ctlr structure
2256 * This handles MAC address management for FCoE, then passes control on to
2257 * the libfc FLOGI response handler.
2259 static void fcoe_flogi_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
2261 struct fcoe_ctlr *fip = arg;
2262 struct fc_exch *exch = fc_seq_exch(seq);
2263 struct fc_lport *lport = exch->lp;
2264 u8 *mac;
2266 if (IS_ERR(fp))
2267 goto done;
2269 mac = fr_cb(fp)->granted_mac;
2270 if (is_zero_ether_addr(mac)) {
2271 /* pre-FIP */
2272 if (fcoe_ctlr_recv_flogi(fip, lport, fp)) {
2273 fc_frame_free(fp);
2274 return;
2277 fcoe_update_src_mac(lport, mac);
2278 done:
2279 fc_lport_flogi_resp(seq, fp, lport);
2283 * fcoe_logo_resp() - FCoE specific LOGO response handler
2284 * @seq: active sequence in the LOGO exchange
2285 * @fp: response frame, or error encoded in a pointer (timeout)
2286 * @arg: pointer the the fcoe_ctlr structure
2288 * This handles MAC address management for FCoE, then passes control on to
2289 * the libfc LOGO response handler.
2291 static void fcoe_logo_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
2293 struct fc_lport *lport = arg;
2294 static u8 zero_mac[ETH_ALEN] = { 0 };
2296 if (!IS_ERR(fp))
2297 fcoe_update_src_mac(lport, zero_mac);
2298 fc_lport_logo_resp(seq, fp, lport);
2302 * fcoe_elsct_send - FCoE specific ELS handler
2304 * This does special case handling of FIP encapsualted ELS exchanges for FCoE,
2305 * using FCoE specific response handlers and passing the FIP controller as
2306 * the argument (the lport is still available from the exchange).
2308 * Most of the work here is just handed off to the libfc routine.
2310 static struct fc_seq *fcoe_elsct_send(struct fc_lport *lport, u32 did,
2311 struct fc_frame *fp, unsigned int op,
2312 void (*resp)(struct fc_seq *,
2313 struct fc_frame *,
2314 void *),
2315 void *arg, u32 timeout)
2317 struct fcoe_port *port = lport_priv(lport);
2318 struct fcoe_interface *fcoe = port->priv;
2319 struct fcoe_ctlr *fip = &fcoe->ctlr;
2320 struct fc_frame_header *fh = fc_frame_header_get(fp);
2322 switch (op) {
2323 case ELS_FLOGI:
2324 case ELS_FDISC:
2325 if (lport->point_to_multipoint)
2326 break;
2327 return fc_elsct_send(lport, did, fp, op, fcoe_flogi_resp,
2328 fip, timeout);
2329 case ELS_LOGO:
2330 /* only hook onto fabric logouts, not port logouts */
2331 if (ntoh24(fh->fh_d_id) != FC_FID_FLOGI)
2332 break;
2333 return fc_elsct_send(lport, did, fp, op, fcoe_logo_resp,
2334 lport, timeout);
2336 return fc_elsct_send(lport, did, fp, op, resp, arg, timeout);
2340 * fcoe_vport_create() - create an fc_host/scsi_host for a vport
2341 * @vport: fc_vport object to create a new fc_host for
2342 * @disabled: start the new fc_host in a disabled state by default?
2344 * Returns: 0 for success
2346 static int fcoe_vport_create(struct fc_vport *vport, bool disabled)
2348 struct Scsi_Host *shost = vport_to_shost(vport);
2349 struct fc_lport *n_port = shost_priv(shost);
2350 struct fcoe_port *port = lport_priv(n_port);
2351 struct fcoe_interface *fcoe = port->priv;
2352 struct net_device *netdev = fcoe->netdev;
2353 struct fc_lport *vn_port;
2355 mutex_lock(&fcoe_config_mutex);
2356 vn_port = fcoe_if_create(fcoe, &vport->dev, 1);
2357 mutex_unlock(&fcoe_config_mutex);
2359 if (IS_ERR(vn_port)) {
2360 printk(KERN_ERR "fcoe: fcoe_vport_create(%s) failed\n",
2361 netdev->name);
2362 return -EIO;
2365 if (disabled) {
2366 fc_vport_set_state(vport, FC_VPORT_DISABLED);
2367 } else {
2368 vn_port->boot_time = jiffies;
2369 fc_fabric_login(vn_port);
2370 fc_vport_setlink(vn_port);
2372 return 0;
2376 * fcoe_vport_destroy() - destroy the fc_host/scsi_host for a vport
2377 * @vport: fc_vport object that is being destroyed
2379 * Returns: 0 for success
2381 static int fcoe_vport_destroy(struct fc_vport *vport)
2383 struct Scsi_Host *shost = vport_to_shost(vport);
2384 struct fc_lport *n_port = shost_priv(shost);
2385 struct fc_lport *vn_port = vport->dd_data;
2386 struct fcoe_port *port = lport_priv(vn_port);
2388 mutex_lock(&n_port->lp_mutex);
2389 list_del(&vn_port->list);
2390 mutex_unlock(&n_port->lp_mutex);
2391 queue_work(fcoe_wq, &port->destroy_work);
2392 return 0;
2396 * fcoe_vport_disable() - change vport state
2397 * @vport: vport to bring online/offline
2398 * @disable: should the vport be disabled?
2400 static int fcoe_vport_disable(struct fc_vport *vport, bool disable)
2402 struct fc_lport *lport = vport->dd_data;
2404 if (disable) {
2405 fc_vport_set_state(vport, FC_VPORT_DISABLED);
2406 fc_fabric_logoff(lport);
2407 } else {
2408 lport->boot_time = jiffies;
2409 fc_fabric_login(lport);
2410 fc_vport_setlink(lport);
2413 return 0;
2417 * fcoe_vport_set_symbolic_name() - append vport string to symbolic name
2418 * @vport: fc_vport with a new symbolic name string
2420 * After generating a new symbolic name string, a new RSPN_ID request is
2421 * sent to the name server. There is no response handler, so if it fails
2422 * for some reason it will not be retried.
2424 static void fcoe_set_vport_symbolic_name(struct fc_vport *vport)
2426 struct fc_lport *lport = vport->dd_data;
2427 struct fc_frame *fp;
2428 size_t len;
2430 snprintf(fc_host_symbolic_name(lport->host), FC_SYMBOLIC_NAME_SIZE,
2431 "%s v%s over %s : %s", FCOE_NAME, FCOE_VERSION,
2432 fcoe_netdev(lport)->name, vport->symbolic_name);
2434 if (lport->state != LPORT_ST_READY)
2435 return;
2437 len = strnlen(fc_host_symbolic_name(lport->host), 255);
2438 fp = fc_frame_alloc(lport,
2439 sizeof(struct fc_ct_hdr) +
2440 sizeof(struct fc_ns_rspn) + len);
2441 if (!fp)
2442 return;
2443 lport->tt.elsct_send(lport, FC_FID_DIR_SERV, fp, FC_NS_RSPN_ID,
2444 NULL, NULL, 3 * lport->r_a_tov);
2448 * fcoe_get_lesb() - Fill the FCoE Link Error Status Block
2449 * @lport: the local port
2450 * @fc_lesb: the link error status block
2452 static void fcoe_get_lesb(struct fc_lport *lport,
2453 struct fc_els_lesb *fc_lesb)
2455 unsigned int cpu;
2456 u32 lfc, vlfc, mdac;
2457 struct fcoe_dev_stats *devst;
2458 struct fcoe_fc_els_lesb *lesb;
2459 struct rtnl_link_stats64 temp;
2460 struct net_device *netdev = fcoe_netdev(lport);
2462 lfc = 0;
2463 vlfc = 0;
2464 mdac = 0;
2465 lesb = (struct fcoe_fc_els_lesb *)fc_lesb;
2466 memset(lesb, 0, sizeof(*lesb));
2467 for_each_possible_cpu(cpu) {
2468 devst = per_cpu_ptr(lport->dev_stats, cpu);
2469 lfc += devst->LinkFailureCount;
2470 vlfc += devst->VLinkFailureCount;
2471 mdac += devst->MissDiscAdvCount;
2473 lesb->lesb_link_fail = htonl(lfc);
2474 lesb->lesb_vlink_fail = htonl(vlfc);
2475 lesb->lesb_miss_fka = htonl(mdac);
2476 lesb->lesb_fcs_error = htonl(dev_get_stats(netdev, &temp)->rx_crc_errors);
2480 * fcoe_set_port_id() - Callback from libfc when Port_ID is set.
2481 * @lport: the local port
2482 * @port_id: the port ID
2483 * @fp: the received frame, if any, that caused the port_id to be set.
2485 * This routine handles the case where we received a FLOGI and are
2486 * entering point-to-point mode. We need to call fcoe_ctlr_recv_flogi()
2487 * so it can set the non-mapped mode and gateway address.
2489 * The FLOGI LS_ACC is handled by fcoe_flogi_resp().
2491 static void fcoe_set_port_id(struct fc_lport *lport,
2492 u32 port_id, struct fc_frame *fp)
2494 struct fcoe_port *port = lport_priv(lport);
2495 struct fcoe_interface *fcoe = port->priv;
2497 if (fp && fc_frame_payload_op(fp) == ELS_FLOGI)
2498 fcoe_ctlr_recv_flogi(&fcoe->ctlr, lport, fp);