2 * Copyright(c) 2007 - 2009 Intel Corporation. All rights reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
17 * Maintained at www.Open-FCoE.org
20 #include <linux/module.h>
21 #include <linux/spinlock.h>
22 #include <linux/netdevice.h>
23 #include <linux/etherdevice.h>
24 #include <linux/ethtool.h>
25 #include <linux/if_ether.h>
26 #include <linux/if_vlan.h>
27 #include <linux/crc32.h>
28 #include <linux/slab.h>
29 #include <linux/cpu.h>
31 #include <linux/sysfs.h>
32 #include <linux/ctype.h>
33 #include <linux/workqueue.h>
34 #include <net/dcbnl.h>
35 #include <net/dcbevent.h>
36 #include <scsi/scsi_tcq.h>
37 #include <scsi/scsicam.h>
38 #include <scsi/scsi_transport.h>
39 #include <scsi/scsi_transport_fc.h>
40 #include <net/rtnetlink.h>
42 #include <scsi/fc/fc_encaps.h>
43 #include <scsi/fc/fc_fip.h>
45 #include <scsi/libfc.h>
46 #include <scsi/fc_frame.h>
47 #include <scsi/libfcoe.h>
51 MODULE_AUTHOR("Open-FCoE.org");
52 MODULE_DESCRIPTION("FCoE");
53 MODULE_LICENSE("GPL v2");
55 /* Performance tuning parameters for fcoe */
56 static unsigned int fcoe_ddp_min = 4096;
57 module_param_named(ddp_min, fcoe_ddp_min, uint, S_IRUGO | S_IWUSR);
58 MODULE_PARM_DESC(ddp_min, "Minimum I/O size in bytes for " \
59 "Direct Data Placement (DDP).");
61 unsigned int fcoe_debug_logging;
62 module_param_named(debug_logging, fcoe_debug_logging, int, S_IRUGO|S_IWUSR);
63 MODULE_PARM_DESC(debug_logging, "a bit mask of logging levels");
65 static DEFINE_MUTEX(fcoe_config_mutex);
67 static struct workqueue_struct *fcoe_wq;
69 /* fcoe_percpu_clean completion. Waiter protected by fcoe_create_mutex */
70 static DECLARE_COMPLETION(fcoe_flush_completion);
73 /* must only by accessed under the RTNL mutex */
74 static LIST_HEAD(fcoe_hostlist);
75 static DEFINE_PER_CPU(struct fcoe_percpu_s, fcoe_percpu);
77 /* Function Prototypes */
78 static int fcoe_reset(struct Scsi_Host *);
79 static int fcoe_xmit(struct fc_lport *, struct fc_frame *);
80 static int fcoe_rcv(struct sk_buff *, struct net_device *,
81 struct packet_type *, struct net_device *);
82 static int fcoe_percpu_receive_thread(void *);
83 static void fcoe_percpu_clean(struct fc_lport *);
84 static int fcoe_link_speed_update(struct fc_lport *);
85 static int fcoe_link_ok(struct fc_lport *);
87 static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *);
88 static int fcoe_hostlist_add(const struct fc_lport *);
90 static int fcoe_device_notification(struct notifier_block *, ulong, void *);
91 static void fcoe_dev_setup(void);
92 static void fcoe_dev_cleanup(void);
93 static struct fcoe_interface
94 *fcoe_hostlist_lookup_port(const struct net_device *);
96 static int fcoe_fip_recv(struct sk_buff *, struct net_device *,
97 struct packet_type *, struct net_device *);
99 static void fcoe_fip_send(struct fcoe_ctlr *, struct sk_buff *);
100 static void fcoe_update_src_mac(struct fc_lport *, u8 *);
101 static u8 *fcoe_get_src_mac(struct fc_lport *);
102 static void fcoe_destroy_work(struct work_struct *);
104 static int fcoe_ddp_setup(struct fc_lport *, u16, struct scatterlist *,
106 static int fcoe_ddp_done(struct fc_lport *, u16);
107 static int fcoe_ddp_target(struct fc_lport *, u16, struct scatterlist *,
109 static int fcoe_cpu_callback(struct notifier_block *, unsigned long, void *);
110 static int fcoe_dcb_app_notification(struct notifier_block *notifier,
111 ulong event, void *ptr);
113 static bool fcoe_match(struct net_device *netdev);
114 static int fcoe_create(struct net_device *netdev, enum fip_state fip_mode);
115 static int fcoe_destroy(struct net_device *netdev);
116 static int fcoe_enable(struct net_device *netdev);
117 static int fcoe_disable(struct net_device *netdev);
119 static struct fc_seq *fcoe_elsct_send(struct fc_lport *,
120 u32 did, struct fc_frame *,
122 void (*resp)(struct fc_seq *,
125 void *, u32 timeout);
126 static void fcoe_recv_frame(struct sk_buff *skb);
128 static void fcoe_get_lesb(struct fc_lport *, struct fc_els_lesb *);
130 /* notification function for packets from net device */
131 static struct notifier_block fcoe_notifier = {
132 .notifier_call = fcoe_device_notification,
135 /* notification function for CPU hotplug events */
136 static struct notifier_block fcoe_cpu_notifier = {
137 .notifier_call = fcoe_cpu_callback,
140 /* notification function for DCB events */
141 static struct notifier_block dcb_notifier = {
142 .notifier_call = fcoe_dcb_app_notification,
145 static struct scsi_transport_template *fcoe_nport_scsi_transport;
146 static struct scsi_transport_template *fcoe_vport_scsi_transport;
148 static int fcoe_vport_destroy(struct fc_vport *);
149 static int fcoe_vport_create(struct fc_vport *, bool disabled);
150 static int fcoe_vport_disable(struct fc_vport *, bool disable);
151 static void fcoe_set_vport_symbolic_name(struct fc_vport *);
152 static void fcoe_set_port_id(struct fc_lport *, u32, struct fc_frame *);
154 static struct libfc_function_template fcoe_libfc_fcn_templ = {
155 .frame_send = fcoe_xmit,
156 .ddp_setup = fcoe_ddp_setup,
157 .ddp_done = fcoe_ddp_done,
158 .ddp_target = fcoe_ddp_target,
159 .elsct_send = fcoe_elsct_send,
160 .get_lesb = fcoe_get_lesb,
161 .lport_set_port_id = fcoe_set_port_id,
164 static struct fc_function_template fcoe_nport_fc_functions = {
165 .show_host_node_name = 1,
166 .show_host_port_name = 1,
167 .show_host_supported_classes = 1,
168 .show_host_supported_fc4s = 1,
169 .show_host_active_fc4s = 1,
170 .show_host_maxframe_size = 1,
171 .show_host_serial_number = 1,
172 .show_host_manufacturer = 1,
173 .show_host_model = 1,
174 .show_host_model_description = 1,
175 .show_host_hardware_version = 1,
176 .show_host_driver_version = 1,
177 .show_host_firmware_version = 1,
178 .show_host_optionrom_version = 1,
180 .show_host_port_id = 1,
181 .show_host_supported_speeds = 1,
182 .get_host_speed = fc_get_host_speed,
183 .show_host_speed = 1,
184 .show_host_port_type = 1,
185 .get_host_port_state = fc_get_host_port_state,
186 .show_host_port_state = 1,
187 .show_host_symbolic_name = 1,
189 .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
190 .show_rport_maxframe_size = 1,
191 .show_rport_supported_classes = 1,
193 .show_host_fabric_name = 1,
194 .show_starget_node_name = 1,
195 .show_starget_port_name = 1,
196 .show_starget_port_id = 1,
197 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
198 .show_rport_dev_loss_tmo = 1,
199 .get_fc_host_stats = fc_get_host_stats,
200 .issue_fc_host_lip = fcoe_reset,
202 .terminate_rport_io = fc_rport_terminate_io,
204 .vport_create = fcoe_vport_create,
205 .vport_delete = fcoe_vport_destroy,
206 .vport_disable = fcoe_vport_disable,
207 .set_vport_symbolic_name = fcoe_set_vport_symbolic_name,
209 .bsg_request = fc_lport_bsg_request,
212 static struct fc_function_template fcoe_vport_fc_functions = {
213 .show_host_node_name = 1,
214 .show_host_port_name = 1,
215 .show_host_supported_classes = 1,
216 .show_host_supported_fc4s = 1,
217 .show_host_active_fc4s = 1,
218 .show_host_maxframe_size = 1,
219 .show_host_serial_number = 1,
220 .show_host_manufacturer = 1,
221 .show_host_model = 1,
222 .show_host_model_description = 1,
223 .show_host_hardware_version = 1,
224 .show_host_driver_version = 1,
225 .show_host_firmware_version = 1,
226 .show_host_optionrom_version = 1,
228 .show_host_port_id = 1,
229 .show_host_supported_speeds = 1,
230 .get_host_speed = fc_get_host_speed,
231 .show_host_speed = 1,
232 .show_host_port_type = 1,
233 .get_host_port_state = fc_get_host_port_state,
234 .show_host_port_state = 1,
235 .show_host_symbolic_name = 1,
237 .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
238 .show_rport_maxframe_size = 1,
239 .show_rport_supported_classes = 1,
241 .show_host_fabric_name = 1,
242 .show_starget_node_name = 1,
243 .show_starget_port_name = 1,
244 .show_starget_port_id = 1,
245 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
246 .show_rport_dev_loss_tmo = 1,
247 .get_fc_host_stats = fc_get_host_stats,
248 .issue_fc_host_lip = fcoe_reset,
250 .terminate_rport_io = fc_rport_terminate_io,
252 .bsg_request = fc_lport_bsg_request,
255 static struct scsi_host_template fcoe_shost_template = {
256 .module = THIS_MODULE,
257 .name = "FCoE Driver",
258 .proc_name = FCOE_NAME,
259 .queuecommand = fc_queuecommand,
260 .eh_abort_handler = fc_eh_abort,
261 .eh_device_reset_handler = fc_eh_device_reset,
262 .eh_host_reset_handler = fc_eh_host_reset,
263 .slave_alloc = fc_slave_alloc,
264 .change_queue_depth = fc_change_queue_depth,
265 .change_queue_type = fc_change_queue_type,
268 .can_queue = FCOE_MAX_OUTSTANDING_COMMANDS,
269 .use_clustering = ENABLE_CLUSTERING,
270 .sg_tablesize = SG_ALL,
271 .max_sectors = 0xffff,
275 * fcoe_interface_setup() - Setup a FCoE interface
276 * @fcoe: The new FCoE interface
277 * @netdev: The net device that the fcoe interface is on
279 * Returns : 0 for success
280 * Locking: must be called with the RTNL mutex held
282 static int fcoe_interface_setup(struct fcoe_interface *fcoe,
283 struct net_device *netdev)
285 struct fcoe_ctlr *fip = &fcoe->ctlr;
286 struct netdev_hw_addr *ha;
287 struct net_device *real_dev;
288 u8 flogi_maddr[ETH_ALEN];
289 const struct net_device_ops *ops;
291 fcoe->netdev = netdev;
293 /* Let LLD initialize for FCoE */
294 ops = netdev->netdev_ops;
295 if (ops->ndo_fcoe_enable) {
296 if (ops->ndo_fcoe_enable(netdev))
297 FCOE_NETDEV_DBG(netdev, "Failed to enable FCoE"
298 " specific feature for LLD.\n");
301 /* Do not support for bonding device */
302 if (netdev->priv_flags & IFF_BONDING && netdev->flags & IFF_MASTER) {
303 FCOE_NETDEV_DBG(netdev, "Bonded interfaces not supported\n");
307 /* look for SAN MAC address, if multiple SAN MACs exist, only
308 * use the first one for SPMA */
309 real_dev = (netdev->priv_flags & IFF_802_1Q_VLAN) ?
310 vlan_dev_real_dev(netdev) : netdev;
311 fcoe->realdev = real_dev;
313 for_each_dev_addr(real_dev, ha) {
314 if ((ha->type == NETDEV_HW_ADDR_T_SAN) &&
315 (is_valid_ether_addr(ha->addr))) {
316 memcpy(fip->ctl_src_addr, ha->addr, ETH_ALEN);
323 /* setup Source Mac Address */
325 memcpy(fip->ctl_src_addr, netdev->dev_addr, netdev->addr_len);
328 * Add FCoE MAC address as second unicast MAC address
329 * or enter promiscuous mode if not capable of listening
330 * for multiple unicast MACs.
332 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
333 dev_uc_add(netdev, flogi_maddr);
335 dev_uc_add(netdev, fip->ctl_src_addr);
336 if (fip->mode == FIP_MODE_VN2VN) {
337 dev_mc_add(netdev, FIP_ALL_VN2VN_MACS);
338 dev_mc_add(netdev, FIP_ALL_P2P_MACS);
340 dev_mc_add(netdev, FIP_ALL_ENODE_MACS);
343 * setup the receive function from ethernet driver
344 * on the ethertype for the given device
346 fcoe->fcoe_packet_type.func = fcoe_rcv;
347 fcoe->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
348 fcoe->fcoe_packet_type.dev = netdev;
349 dev_add_pack(&fcoe->fcoe_packet_type);
351 fcoe->fip_packet_type.func = fcoe_fip_recv;
352 fcoe->fip_packet_type.type = htons(ETH_P_FIP);
353 fcoe->fip_packet_type.dev = netdev;
354 dev_add_pack(&fcoe->fip_packet_type);
360 * fcoe_interface_create() - Create a FCoE interface on a net device
361 * @netdev: The net device to create the FCoE interface on
362 * @fip_mode: The mode to use for FIP
364 * Returns: pointer to a struct fcoe_interface or NULL on error
366 static struct fcoe_interface *fcoe_interface_create(struct net_device *netdev,
367 enum fip_state fip_mode)
369 struct fcoe_interface *fcoe;
372 if (!try_module_get(THIS_MODULE)) {
373 FCOE_NETDEV_DBG(netdev,
374 "Could not get a reference to the module\n");
375 fcoe = ERR_PTR(-EBUSY);
379 fcoe = kzalloc(sizeof(*fcoe), GFP_KERNEL);
381 FCOE_NETDEV_DBG(netdev, "Could not allocate fcoe structure\n");
382 fcoe = ERR_PTR(-ENOMEM);
391 fcoe_ctlr_init(&fcoe->ctlr, fip_mode);
392 fcoe->ctlr.send = fcoe_fip_send;
393 fcoe->ctlr.update_mac = fcoe_update_src_mac;
394 fcoe->ctlr.get_src_addr = fcoe_get_src_mac;
396 err = fcoe_interface_setup(fcoe, netdev);
398 fcoe_ctlr_destroy(&fcoe->ctlr);
408 module_put(THIS_MODULE);
414 * fcoe_interface_cleanup() - Clean up a FCoE interface
415 * @fcoe: The FCoE interface to be cleaned up
417 * Caller must be holding the RTNL mutex
419 static void fcoe_interface_cleanup(struct fcoe_interface *fcoe)
421 struct net_device *netdev = fcoe->netdev;
422 struct fcoe_ctlr *fip = &fcoe->ctlr;
423 u8 flogi_maddr[ETH_ALEN];
424 const struct net_device_ops *ops;
429 * Don't listen for Ethernet packets anymore.
430 * synchronize_net() ensures that the packet handlers are not running
431 * on another CPU. dev_remove_pack() would do that, this calls the
432 * unsyncronized version __dev_remove_pack() to avoid multiple delays.
434 __dev_remove_pack(&fcoe->fcoe_packet_type);
435 __dev_remove_pack(&fcoe->fip_packet_type);
438 /* Delete secondary MAC addresses */
439 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
440 dev_uc_del(netdev, flogi_maddr);
442 dev_uc_del(netdev, fip->ctl_src_addr);
443 if (fip->mode == FIP_MODE_VN2VN) {
444 dev_mc_del(netdev, FIP_ALL_VN2VN_MACS);
445 dev_mc_del(netdev, FIP_ALL_P2P_MACS);
447 dev_mc_del(netdev, FIP_ALL_ENODE_MACS);
449 /* Tell the LLD we are done w/ FCoE */
450 ops = netdev->netdev_ops;
451 if (ops->ndo_fcoe_disable) {
452 if (ops->ndo_fcoe_disable(netdev))
453 FCOE_NETDEV_DBG(netdev, "Failed to disable FCoE"
454 " specific feature for LLD.\n");
459 /* Release the self-reference taken during fcoe_interface_create() */
460 /* tear-down the FCoE controller */
461 fcoe_ctlr_destroy(fip);
464 module_put(THIS_MODULE);
468 * fcoe_fip_recv() - Handler for received FIP frames
469 * @skb: The receive skb
470 * @netdev: The associated net device
471 * @ptype: The packet_type structure which was used to register this handler
472 * @orig_dev: The original net_device the the skb was received on.
473 * (in case dev is a bond)
475 * Returns: 0 for success
477 static int fcoe_fip_recv(struct sk_buff *skb, struct net_device *netdev,
478 struct packet_type *ptype,
479 struct net_device *orig_dev)
481 struct fcoe_interface *fcoe;
483 fcoe = container_of(ptype, struct fcoe_interface, fip_packet_type);
484 fcoe_ctlr_recv(&fcoe->ctlr, skb);
489 * fcoe_port_send() - Send an Ethernet-encapsulated FIP/FCoE frame
490 * @port: The FCoE port
491 * @skb: The FIP/FCoE packet to be sent
493 static void fcoe_port_send(struct fcoe_port *port, struct sk_buff *skb)
495 if (port->fcoe_pending_queue.qlen)
496 fcoe_check_wait_queue(port->lport, skb);
497 else if (fcoe_start_io(skb))
498 fcoe_check_wait_queue(port->lport, skb);
502 * fcoe_fip_send() - Send an Ethernet-encapsulated FIP frame
503 * @fip: The FCoE controller
504 * @skb: The FIP packet to be sent
506 static void fcoe_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
508 skb->dev = fcoe_from_ctlr(fip)->netdev;
509 fcoe_port_send(lport_priv(fip->lp), skb);
513 * fcoe_update_src_mac() - Update the Ethernet MAC filters
514 * @lport: The local port to update the source MAC on
515 * @addr: Unicast MAC address to add
517 * Remove any previously-set unicast MAC filter.
518 * Add secondary FCoE MAC address filter for our OUI.
520 static void fcoe_update_src_mac(struct fc_lport *lport, u8 *addr)
522 struct fcoe_port *port = lport_priv(lport);
523 struct fcoe_interface *fcoe = port->priv;
526 if (!is_zero_ether_addr(port->data_src_addr))
527 dev_uc_del(fcoe->netdev, port->data_src_addr);
528 if (!is_zero_ether_addr(addr))
529 dev_uc_add(fcoe->netdev, addr);
530 memcpy(port->data_src_addr, addr, ETH_ALEN);
535 * fcoe_get_src_mac() - return the Ethernet source address for an lport
536 * @lport: libfc lport
538 static u8 *fcoe_get_src_mac(struct fc_lport *lport)
540 struct fcoe_port *port = lport_priv(lport);
542 return port->data_src_addr;
546 * fcoe_lport_config() - Set up a local port
547 * @lport: The local port to be setup
549 * Returns: 0 for success
551 static int fcoe_lport_config(struct fc_lport *lport)
555 lport->max_retry_count = 3;
556 lport->max_rport_retry_count = 3;
557 lport->e_d_tov = 2 * 1000; /* FC-FS default */
558 lport->r_a_tov = 2 * 2 * 1000;
559 lport->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
560 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
561 lport->does_npiv = 1;
563 fc_lport_init_stats(lport);
565 /* lport fc_lport related configuration */
566 fc_lport_config(lport);
568 /* offload related configuration */
569 lport->crc_offload = 0;
570 lport->seq_offload = 0;
571 lport->lro_enabled = 0;
579 * fcoe_netdev_features_change - Updates the lport's offload flags based
580 * on the LLD netdev's FCoE feature flags
582 static void fcoe_netdev_features_change(struct fc_lport *lport,
583 struct net_device *netdev)
585 mutex_lock(&lport->lp_mutex);
587 if (netdev->features & NETIF_F_SG)
592 if (netdev->features & NETIF_F_FCOE_CRC) {
593 lport->crc_offload = 1;
594 FCOE_NETDEV_DBG(netdev, "Supports FCCRC offload\n");
596 lport->crc_offload = 0;
599 if (netdev->features & NETIF_F_FSO) {
600 lport->seq_offload = 1;
601 lport->lso_max = netdev->gso_max_size;
602 FCOE_NETDEV_DBG(netdev, "Supports LSO for max len 0x%x\n",
605 lport->seq_offload = 0;
609 if (netdev->fcoe_ddp_xid) {
610 lport->lro_enabled = 1;
611 lport->lro_xid = netdev->fcoe_ddp_xid;
612 FCOE_NETDEV_DBG(netdev, "Supports LRO for max xid 0x%x\n",
615 lport->lro_enabled = 0;
619 mutex_unlock(&lport->lp_mutex);
623 * fcoe_netdev_config() - Set up net devive for SW FCoE
624 * @lport: The local port that is associated with the net device
625 * @netdev: The associated net device
627 * Must be called after fcoe_lport_config() as it will use local port mutex
629 * Returns: 0 for success
631 static int fcoe_netdev_config(struct fc_lport *lport, struct net_device *netdev)
635 struct fcoe_interface *fcoe;
636 struct fcoe_port *port;
638 /* Setup lport private data to point to fcoe softc */
639 port = lport_priv(lport);
643 * Determine max frame size based on underlying device and optional
644 * user-configured limit. If the MFS is too low, fcoe_link_ok()
645 * will return 0, so do this first.
648 if (netdev->features & NETIF_F_FCOE_MTU) {
650 FCOE_NETDEV_DBG(netdev, "Supports FCOE_MTU of %d bytes\n", mfs);
652 mfs -= (sizeof(struct fcoe_hdr) + sizeof(struct fcoe_crc_eof));
653 if (fc_set_mfs(lport, mfs))
656 /* offload features support */
657 fcoe_netdev_features_change(lport, netdev);
659 skb_queue_head_init(&port->fcoe_pending_queue);
660 port->fcoe_pending_queue_active = 0;
661 setup_timer(&port->timer, fcoe_queue_timer, (unsigned long)lport);
663 fcoe_link_speed_update(lport);
666 if (fcoe_get_wwn(netdev, &wwnn, NETDEV_FCOE_WWNN))
667 wwnn = fcoe_wwn_from_mac(fcoe->ctlr.ctl_src_addr, 1, 0);
668 fc_set_wwnn(lport, wwnn);
669 if (fcoe_get_wwn(netdev, &wwpn, NETDEV_FCOE_WWPN))
670 wwpn = fcoe_wwn_from_mac(fcoe->ctlr.ctl_src_addr,
672 fc_set_wwpn(lport, wwpn);
679 * fcoe_shost_config() - Set up the SCSI host associated with a local port
680 * @lport: The local port
681 * @dev: The device associated with the SCSI host
683 * Must be called after fcoe_lport_config() and fcoe_netdev_config()
685 * Returns: 0 for success
687 static int fcoe_shost_config(struct fc_lport *lport, struct device *dev)
691 /* lport scsi host config */
692 lport->host->max_lun = FCOE_MAX_LUN;
693 lport->host->max_id = FCOE_MAX_FCP_TARGET;
694 lport->host->max_channel = 0;
695 lport->host->max_cmd_len = FCOE_MAX_CMD_LEN;
698 lport->host->transportt = fcoe_vport_scsi_transport;
700 lport->host->transportt = fcoe_nport_scsi_transport;
702 /* add the new host to the SCSI-ml */
703 rc = scsi_add_host(lport->host, dev);
705 FCOE_NETDEV_DBG(fcoe_netdev(lport), "fcoe_shost_config: "
706 "error on scsi_add_host\n");
711 fc_host_max_npiv_vports(lport->host) = USHRT_MAX;
713 snprintf(fc_host_symbolic_name(lport->host), FC_SYMBOLIC_NAME_SIZE,
714 "%s v%s over %s", FCOE_NAME, FCOE_VERSION,
715 fcoe_netdev(lport)->name);
722 * fcoe_fdmi_info() - Get FDMI related info from net devive for SW FCoE
723 * @lport: The local port that is associated with the net device
724 * @netdev: The associated net device
726 * Must be called after fcoe_shost_config() as it will use local port mutex
729 static void fcoe_fdmi_info(struct fc_lport *lport, struct net_device *netdev)
731 struct fcoe_interface *fcoe;
732 struct fcoe_port *port;
733 struct net_device *realdev;
735 struct netdev_fcoe_hbainfo fdmi;
737 port = lport_priv(lport);
739 realdev = fcoe->realdev;
744 /* No FDMI state m/c for NPIV ports */
748 if (realdev->netdev_ops->ndo_fcoe_get_hbainfo) {
749 memset(&fdmi, 0, sizeof(fdmi));
750 rc = realdev->netdev_ops->ndo_fcoe_get_hbainfo(realdev,
753 printk(KERN_INFO "fcoe: Failed to retrieve FDMI "
754 "information from netdev.\n");
758 snprintf(fc_host_serial_number(lport->host),
759 FC_SERIAL_NUMBER_SIZE,
762 snprintf(fc_host_manufacturer(lport->host),
763 FC_SERIAL_NUMBER_SIZE,
766 snprintf(fc_host_model(lport->host),
767 FC_SYMBOLIC_NAME_SIZE,
770 snprintf(fc_host_model_description(lport->host),
771 FC_SYMBOLIC_NAME_SIZE,
773 fdmi.model_description);
774 snprintf(fc_host_hardware_version(lport->host),
775 FC_VERSION_STRING_SIZE,
777 fdmi.hardware_version);
778 snprintf(fc_host_driver_version(lport->host),
779 FC_VERSION_STRING_SIZE,
781 fdmi.driver_version);
782 snprintf(fc_host_optionrom_version(lport->host),
783 FC_VERSION_STRING_SIZE,
785 fdmi.optionrom_version);
786 snprintf(fc_host_firmware_version(lport->host),
787 FC_VERSION_STRING_SIZE,
789 fdmi.firmware_version);
791 /* Enable FDMI lport states */
792 lport->fdmi_enabled = 1;
794 lport->fdmi_enabled = 0;
795 printk(KERN_INFO "fcoe: No FDMI support.\n");
800 * fcoe_oem_match() - The match routine for the offloaded exchange manager
803 * This routine will be associated with an exchange manager (EM). When
804 * the libfc exchange handling code is looking for an EM to use it will
805 * call this routine and pass it the frame that it wishes to send. This
806 * routine will return True if the associated EM is to be used and False
807 * if the echange code should continue looking for an EM.
809 * The offload EM that this routine is associated with will handle any
810 * packets that are for SCSI read requests.
812 * This has been enhanced to work when FCoE stack is operating in target
815 * Returns: True for read types I/O, otherwise returns false.
817 static bool fcoe_oem_match(struct fc_frame *fp)
819 struct fc_frame_header *fh = fc_frame_header_get(fp);
820 struct fcp_cmnd *fcp;
822 if (fc_fcp_is_read(fr_fsp(fp)) &&
823 (fr_fsp(fp)->data_len > fcoe_ddp_min))
825 else if ((fr_fsp(fp) == NULL) &&
826 (fh->fh_r_ctl == FC_RCTL_DD_UNSOL_CMD) &&
827 (ntohs(fh->fh_rx_id) == FC_XID_UNKNOWN)) {
828 fcp = fc_frame_payload_get(fp, sizeof(*fcp));
829 if ((fcp->fc_flags & FCP_CFL_WRDATA) &&
830 (ntohl(fcp->fc_dl) > fcoe_ddp_min))
837 * fcoe_em_config() - Allocate and configure an exchange manager
838 * @lport: The local port that the new EM will be associated with
840 * Returns: 0 on success
842 static inline int fcoe_em_config(struct fc_lport *lport)
844 struct fcoe_port *port = lport_priv(lport);
845 struct fcoe_interface *fcoe = port->priv;
846 struct fcoe_interface *oldfcoe = NULL;
847 struct net_device *old_real_dev, *cur_real_dev;
848 u16 min_xid = FCOE_MIN_XID;
849 u16 max_xid = FCOE_MAX_XID;
852 * Check if need to allocate an em instance for
853 * offload exchange ids to be shared across all VN_PORTs/lport.
855 if (!lport->lro_enabled || !lport->lro_xid ||
856 (lport->lro_xid >= max_xid)) {
862 * Reuse existing offload em instance in case
863 * it is already allocated on real eth device
865 if (fcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
866 cur_real_dev = vlan_dev_real_dev(fcoe->netdev);
868 cur_real_dev = fcoe->netdev;
870 list_for_each_entry(oldfcoe, &fcoe_hostlist, list) {
871 if (oldfcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
872 old_real_dev = vlan_dev_real_dev(oldfcoe->netdev);
874 old_real_dev = oldfcoe->netdev;
876 if (cur_real_dev == old_real_dev) {
877 fcoe->oem = oldfcoe->oem;
883 if (!fc_exch_mgr_add(lport, fcoe->oem, fcoe_oem_match)) {
884 printk(KERN_ERR "fcoe_em_config: failed to add "
885 "offload em:%p on interface:%s\n",
886 fcoe->oem, fcoe->netdev->name);
890 fcoe->oem = fc_exch_mgr_alloc(lport, FC_CLASS_3,
891 FCOE_MIN_XID, lport->lro_xid,
894 printk(KERN_ERR "fcoe_em_config: failed to allocate "
895 "em for offload exches on interface:%s\n",
902 * Exclude offload EM xid range from next EM xid range.
904 min_xid += lport->lro_xid + 1;
907 if (!fc_exch_mgr_alloc(lport, FC_CLASS_3, min_xid, max_xid, NULL)) {
908 printk(KERN_ERR "fcoe_em_config: failed to "
909 "allocate em on interface %s\n", fcoe->netdev->name);
917 * fcoe_if_destroy() - Tear down a SW FCoE instance
918 * @lport: The local port to be destroyed
921 static void fcoe_if_destroy(struct fc_lport *lport)
923 struct fcoe_port *port = lport_priv(lport);
924 struct fcoe_interface *fcoe = port->priv;
925 struct net_device *netdev = fcoe->netdev;
927 FCOE_NETDEV_DBG(netdev, "Destroying interface\n");
929 /* Logout of the fabric */
930 fc_fabric_logoff(lport);
932 /* Cleanup the fc_lport */
933 fc_lport_destroy(lport);
935 /* Stop the transmit retry timer */
936 del_timer_sync(&port->timer);
938 /* Free existing transmit skbs */
939 fcoe_clean_pending_queue(lport);
942 if (!is_zero_ether_addr(port->data_src_addr))
943 dev_uc_del(netdev, port->data_src_addr);
946 /* Free queued packets for the per-CPU receive threads */
947 fcoe_percpu_clean(lport);
949 /* Detach from the scsi-ml */
950 fc_remove_host(lport->host);
951 scsi_remove_host(lport->host);
953 /* Destroy lport scsi_priv */
954 fc_fcp_destroy(lport);
956 /* There are no more rports or I/O, free the EM */
957 fc_exch_mgr_free(lport);
959 /* Free memory used by statistical counters */
960 fc_lport_free_stats(lport);
962 /* Release the Scsi_Host */
963 scsi_host_put(lport->host);
967 * fcoe_ddp_setup() - Call a LLD's ddp_setup through the net device
968 * @lport: The local port to setup DDP for
969 * @xid: The exchange ID for this DDP transfer
970 * @sgl: The scatterlist describing this transfer
971 * @sgc: The number of sg items
973 * Returns: 0 if the DDP context was not configured
975 static int fcoe_ddp_setup(struct fc_lport *lport, u16 xid,
976 struct scatterlist *sgl, unsigned int sgc)
978 struct net_device *netdev = fcoe_netdev(lport);
980 if (netdev->netdev_ops->ndo_fcoe_ddp_setup)
981 return netdev->netdev_ops->ndo_fcoe_ddp_setup(netdev,
989 * fcoe_ddp_target() - Call a LLD's ddp_target through the net device
990 * @lport: The local port to setup DDP for
991 * @xid: The exchange ID for this DDP transfer
992 * @sgl: The scatterlist describing this transfer
993 * @sgc: The number of sg items
995 * Returns: 0 if the DDP context was not configured
997 static int fcoe_ddp_target(struct fc_lport *lport, u16 xid,
998 struct scatterlist *sgl, unsigned int sgc)
1000 struct net_device *netdev = fcoe_netdev(lport);
1002 if (netdev->netdev_ops->ndo_fcoe_ddp_target)
1003 return netdev->netdev_ops->ndo_fcoe_ddp_target(netdev, xid,
1011 * fcoe_ddp_done() - Call a LLD's ddp_done through the net device
1012 * @lport: The local port to complete DDP on
1013 * @xid: The exchange ID for this DDP transfer
1015 * Returns: the length of data that have been completed by DDP
1017 static int fcoe_ddp_done(struct fc_lport *lport, u16 xid)
1019 struct net_device *netdev = fcoe_netdev(lport);
1021 if (netdev->netdev_ops->ndo_fcoe_ddp_done)
1022 return netdev->netdev_ops->ndo_fcoe_ddp_done(netdev, xid);
1027 * fcoe_if_create() - Create a FCoE instance on an interface
1028 * @fcoe: The FCoE interface to create a local port on
1029 * @parent: The device pointer to be the parent in sysfs for the SCSI host
1030 * @npiv: Indicates if the port is a vport or not
1032 * Creates a fc_lport instance and a Scsi_Host instance and configure them.
1034 * Returns: The allocated fc_lport or an error pointer
1036 static struct fc_lport *fcoe_if_create(struct fcoe_interface *fcoe,
1037 struct device *parent, int npiv)
1039 struct net_device *netdev = fcoe->netdev;
1040 struct fc_lport *lport, *n_port;
1041 struct fcoe_port *port;
1042 struct Scsi_Host *shost;
1045 * parent is only a vport if npiv is 1,
1046 * but we'll only use vport in that case so go ahead and set it
1048 struct fc_vport *vport = dev_to_vport(parent);
1050 FCOE_NETDEV_DBG(netdev, "Create Interface\n");
1053 lport = libfc_host_alloc(&fcoe_shost_template, sizeof(*port));
1055 lport = libfc_vport_create(vport, sizeof(*port));
1058 FCOE_NETDEV_DBG(netdev, "Could not allocate host structure\n");
1062 port = lport_priv(lport);
1063 port->lport = lport;
1065 port->max_queue_depth = FCOE_MAX_QUEUE_DEPTH;
1066 port->min_queue_depth = FCOE_MIN_QUEUE_DEPTH;
1067 INIT_WORK(&port->destroy_work, fcoe_destroy_work);
1069 /* configure a fc_lport including the exchange manager */
1070 rc = fcoe_lport_config(lport);
1072 FCOE_NETDEV_DBG(netdev, "Could not configure lport for the "
1078 FCOE_NETDEV_DBG(netdev, "Setting vport names, "
1079 "%16.16llx %16.16llx\n",
1080 vport->node_name, vport->port_name);
1081 fc_set_wwnn(lport, vport->node_name);
1082 fc_set_wwpn(lport, vport->port_name);
1085 /* configure lport network properties */
1086 rc = fcoe_netdev_config(lport, netdev);
1088 FCOE_NETDEV_DBG(netdev, "Could not configure netdev for the "
1090 goto out_lp_destroy;
1093 /* configure lport scsi host properties */
1094 rc = fcoe_shost_config(lport, parent);
1096 FCOE_NETDEV_DBG(netdev, "Could not configure shost for the "
1098 goto out_lp_destroy;
1101 /* Initialize the library */
1102 rc = fcoe_libfc_config(lport, &fcoe->ctlr, &fcoe_libfc_fcn_templ, 1);
1104 FCOE_NETDEV_DBG(netdev, "Could not configure libfc for the "
1106 goto out_lp_destroy;
1109 /* Initialized FDMI information */
1110 fcoe_fdmi_info(lport, netdev);
1113 * fcoe_em_alloc() and fcoe_hostlist_add() both
1114 * need to be atomic with respect to other changes to the
1115 * hostlist since fcoe_em_alloc() looks for an existing EM
1116 * instance on host list updated by fcoe_hostlist_add().
1118 * This is currently handled through the fcoe_config_mutex
1122 /* lport exch manager allocation */
1123 rc = fcoe_em_config(lport);
1125 shost = vport_to_shost(vport);
1126 n_port = shost_priv(shost);
1127 rc = fc_exch_mgr_list_clone(n_port, lport);
1131 FCOE_NETDEV_DBG(netdev, "Could not configure the EM\n");
1132 goto out_lp_destroy;
1138 fc_exch_mgr_free(lport);
1140 scsi_host_put(lport->host);
1146 * fcoe_if_init() - Initialization routine for fcoe.ko
1148 * Attaches the SW FCoE transport to the FC transport
1150 * Returns: 0 on success
1152 static int __init fcoe_if_init(void)
1154 /* attach to scsi transport */
1155 fcoe_nport_scsi_transport =
1156 fc_attach_transport(&fcoe_nport_fc_functions);
1157 fcoe_vport_scsi_transport =
1158 fc_attach_transport(&fcoe_vport_fc_functions);
1160 if (!fcoe_nport_scsi_transport) {
1161 printk(KERN_ERR "fcoe: Failed to attach to the FC transport\n");
1169 * fcoe_if_exit() - Tear down fcoe.ko
1171 * Detaches the SW FCoE transport from the FC transport
1173 * Returns: 0 on success
1175 static int __exit fcoe_if_exit(void)
1177 fc_release_transport(fcoe_nport_scsi_transport);
1178 fc_release_transport(fcoe_vport_scsi_transport);
1179 fcoe_nport_scsi_transport = NULL;
1180 fcoe_vport_scsi_transport = NULL;
1185 * fcoe_percpu_thread_create() - Create a receive thread for an online CPU
1186 * @cpu: The CPU index of the CPU to create a receive thread for
1188 static void fcoe_percpu_thread_create(unsigned int cpu)
1190 struct fcoe_percpu_s *p;
1191 struct task_struct *thread;
1193 p = &per_cpu(fcoe_percpu, cpu);
1195 thread = kthread_create_on_node(fcoe_percpu_receive_thread,
1196 (void *)p, cpu_to_node(cpu),
1197 "fcoethread/%d", cpu);
1199 if (likely(!IS_ERR(thread))) {
1200 kthread_bind(thread, cpu);
1201 wake_up_process(thread);
1203 spin_lock_bh(&p->fcoe_rx_list.lock);
1205 spin_unlock_bh(&p->fcoe_rx_list.lock);
1210 * fcoe_percpu_thread_destroy() - Remove the receive thread of a CPU
1211 * @cpu: The CPU index of the CPU whose receive thread is to be destroyed
1213 * Destroys a per-CPU Rx thread. Any pending skbs are moved to the
1214 * current CPU's Rx thread. If the thread being destroyed is bound to
1215 * the CPU processing this context the skbs will be freed.
1217 static void fcoe_percpu_thread_destroy(unsigned int cpu)
1219 struct fcoe_percpu_s *p;
1220 struct task_struct *thread;
1221 struct page *crc_eof;
1222 struct sk_buff *skb;
1224 struct fcoe_percpu_s *p0;
1225 unsigned targ_cpu = get_cpu();
1226 #endif /* CONFIG_SMP */
1228 FCOE_DBG("Destroying receive thread for CPU %d\n", cpu);
1230 /* Prevent any new skbs from being queued for this CPU. */
1231 p = &per_cpu(fcoe_percpu, cpu);
1232 spin_lock_bh(&p->fcoe_rx_list.lock);
1235 crc_eof = p->crc_eof_page;
1236 p->crc_eof_page = NULL;
1237 p->crc_eof_offset = 0;
1238 spin_unlock_bh(&p->fcoe_rx_list.lock);
1242 * Don't bother moving the skb's if this context is running
1243 * on the same CPU that is having its thread destroyed. This
1244 * can easily happen when the module is removed.
1246 if (cpu != targ_cpu) {
1247 p0 = &per_cpu(fcoe_percpu, targ_cpu);
1248 spin_lock_bh(&p0->fcoe_rx_list.lock);
1250 FCOE_DBG("Moving frames from CPU %d to CPU %d\n",
1253 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1254 __skb_queue_tail(&p0->fcoe_rx_list, skb);
1255 spin_unlock_bh(&p0->fcoe_rx_list.lock);
1258 * The targeted CPU is not initialized and cannot accept
1259 * new skbs. Unlock the targeted CPU and drop the skbs
1260 * on the CPU that is going offline.
1262 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1264 spin_unlock_bh(&p0->fcoe_rx_list.lock);
1268 * This scenario occurs when the module is being removed
1269 * and all threads are being destroyed. skbs will continue
1270 * to be shifted from the CPU thread that is being removed
1271 * to the CPU thread associated with the CPU that is processing
1272 * the module removal. Once there is only one CPU Rx thread it
1273 * will reach this case and we will drop all skbs and later
1276 spin_lock_bh(&p->fcoe_rx_list.lock);
1277 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1279 spin_unlock_bh(&p->fcoe_rx_list.lock);
1284 * This a non-SMP scenario where the singular Rx thread is
1285 * being removed. Free all skbs and stop the thread.
1287 spin_lock_bh(&p->fcoe_rx_list.lock);
1288 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1290 spin_unlock_bh(&p->fcoe_rx_list.lock);
1294 kthread_stop(thread);
1301 * fcoe_cpu_callback() - Handler for CPU hotplug events
1302 * @nfb: The callback data block
1303 * @action: The event triggering the callback
1304 * @hcpu: The index of the CPU that the event is for
1306 * This creates or destroys per-CPU data for fcoe
1308 * Returns NOTIFY_OK always.
1310 static int fcoe_cpu_callback(struct notifier_block *nfb,
1311 unsigned long action, void *hcpu)
1313 unsigned cpu = (unsigned long)hcpu;
1317 case CPU_ONLINE_FROZEN:
1318 FCOE_DBG("CPU %x online: Create Rx thread\n", cpu);
1319 fcoe_percpu_thread_create(cpu);
1322 case CPU_DEAD_FROZEN:
1323 FCOE_DBG("CPU %x offline: Remove Rx thread\n", cpu);
1324 fcoe_percpu_thread_destroy(cpu);
1333 * fcoe_select_cpu() - Selects CPU to handle post-processing of incoming
1336 * This routine selects next CPU based on cpumask to distribute
1337 * incoming requests in round robin.
1339 * Returns: int CPU number
1341 static inline unsigned int fcoe_select_cpu(void)
1343 static unsigned int selected_cpu;
1345 selected_cpu = cpumask_next(selected_cpu, cpu_online_mask);
1346 if (selected_cpu >= nr_cpu_ids)
1347 selected_cpu = cpumask_first(cpu_online_mask);
1349 return selected_cpu;
1353 * fcoe_rcv() - Receive packets from a net device
1354 * @skb: The received packet
1355 * @netdev: The net device that the packet was received on
1356 * @ptype: The packet type context
1357 * @olddev: The last device net device
1359 * This routine is called by NET_RX_SOFTIRQ. It receives a packet, builds a
1360 * FC frame and passes the frame to libfc.
1362 * Returns: 0 for success
1364 static int fcoe_rcv(struct sk_buff *skb, struct net_device *netdev,
1365 struct packet_type *ptype, struct net_device *olddev)
1367 struct fc_lport *lport;
1368 struct fcoe_rcv_info *fr;
1369 struct fcoe_interface *fcoe;
1370 struct fc_frame_header *fh;
1371 struct fcoe_percpu_s *fps;
1375 fcoe = container_of(ptype, struct fcoe_interface, fcoe_packet_type);
1376 lport = fcoe->ctlr.lp;
1377 if (unlikely(!lport)) {
1378 FCOE_NETDEV_DBG(netdev, "Cannot find hba structure");
1381 if (!lport->link_up)
1384 FCOE_NETDEV_DBG(netdev, "skb_info: len:%d data_len:%d head:%p "
1385 "data:%p tail:%p end:%p sum:%d dev:%s",
1386 skb->len, skb->data_len, skb->head, skb->data,
1387 skb_tail_pointer(skb), skb_end_pointer(skb),
1388 skb->csum, skb->dev ? skb->dev->name : "<NULL>");
1392 if (is_fip_mode(&fcoe->ctlr) &&
1393 compare_ether_addr(eh->h_source, fcoe->ctlr.dest_addr)) {
1394 FCOE_NETDEV_DBG(netdev, "wrong source mac address:%pM\n",
1400 * Check for minimum frame length, and make sure required FCoE
1401 * and FC headers are pulled into the linear data area.
1403 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
1404 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
1407 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
1408 fh = (struct fc_frame_header *) skb_transport_header(skb);
1410 if (ntoh24(&eh->h_dest[3]) != ntoh24(fh->fh_d_id)) {
1411 FCOE_NETDEV_DBG(netdev, "FC frame d_id mismatch with MAC:%pM\n",
1416 fr = fcoe_dev_from_skb(skb);
1420 * In case the incoming frame's exchange is originated from
1421 * the initiator, then received frame's exchange id is ANDed
1422 * with fc_cpu_mask bits to get the same cpu on which exchange
1423 * was originated, otherwise select cpu using rx exchange id
1424 * or fcoe_select_cpu().
1426 if (ntoh24(fh->fh_f_ctl) & FC_FC_EX_CTX)
1427 cpu = ntohs(fh->fh_ox_id) & fc_cpu_mask;
1429 if (ntohs(fh->fh_rx_id) == FC_XID_UNKNOWN)
1430 cpu = fcoe_select_cpu();
1432 cpu = ntohs(fh->fh_rx_id) & fc_cpu_mask;
1435 if (cpu >= nr_cpu_ids)
1438 fps = &per_cpu(fcoe_percpu, cpu);
1439 spin_lock_bh(&fps->fcoe_rx_list.lock);
1440 if (unlikely(!fps->thread)) {
1442 * The targeted CPU is not ready, let's target
1443 * the first CPU now. For non-SMP systems this
1444 * will check the same CPU twice.
1446 FCOE_NETDEV_DBG(netdev, "CPU is online, but no receive thread "
1447 "ready for incoming skb- using first online "
1450 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1451 cpu = cpumask_first(cpu_online_mask);
1452 fps = &per_cpu(fcoe_percpu, cpu);
1453 spin_lock_bh(&fps->fcoe_rx_list.lock);
1455 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1461 * We now have a valid CPU that we're targeting for
1462 * this skb. We also have this receive thread locked,
1463 * so we're free to queue skbs into it's queue.
1466 /* If this is a SCSI-FCP frame, and this is already executing on the
1467 * correct CPU, and the queue for this CPU is empty, then go ahead
1468 * and process the frame directly in the softirq context.
1469 * This lets us process completions without context switching from the
1470 * NET_RX softirq, to our receive processing thread, and then back to
1471 * BLOCK softirq context.
1473 if (fh->fh_type == FC_TYPE_FCP &&
1474 cpu == smp_processor_id() &&
1475 skb_queue_empty(&fps->fcoe_rx_list)) {
1476 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1477 fcoe_recv_frame(skb);
1479 __skb_queue_tail(&fps->fcoe_rx_list, skb);
1480 if (fps->fcoe_rx_list.qlen == 1)
1481 wake_up_process(fps->thread);
1482 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1487 per_cpu_ptr(lport->dev_stats, get_cpu())->ErrorFrames++;
1495 * fcoe_alloc_paged_crc_eof() - Allocate a page to be used for the trailer CRC
1496 * @skb: The packet to be transmitted
1497 * @tlen: The total length of the trailer
1499 * Returns: 0 for success
1501 static int fcoe_alloc_paged_crc_eof(struct sk_buff *skb, int tlen)
1503 struct fcoe_percpu_s *fps;
1506 fps = &get_cpu_var(fcoe_percpu);
1507 rc = fcoe_get_paged_crc_eof(skb, tlen, fps);
1508 put_cpu_var(fcoe_percpu);
1514 * fcoe_xmit() - Transmit a FCoE frame
1515 * @lport: The local port that the frame is to be transmitted for
1516 * @fp: The frame to be transmitted
1518 * Return: 0 for success
1520 static int fcoe_xmit(struct fc_lport *lport, struct fc_frame *fp)
1525 struct fcoe_crc_eof *cp;
1526 struct sk_buff *skb;
1527 struct fcoe_dev_stats *stats;
1528 struct fc_frame_header *fh;
1529 unsigned int hlen; /* header length implies the version */
1530 unsigned int tlen; /* trailer length */
1531 unsigned int elen; /* eth header, may include vlan */
1532 struct fcoe_port *port = lport_priv(lport);
1533 struct fcoe_interface *fcoe = port->priv;
1535 struct fcoe_hdr *hp;
1537 WARN_ON((fr_len(fp) % sizeof(u32)) != 0);
1539 fh = fc_frame_header_get(fp);
1541 wlen = skb->len / FCOE_WORD_TO_BYTE;
1543 if (!lport->link_up) {
1548 if (unlikely(fh->fh_type == FC_TYPE_ELS) &&
1549 fcoe_ctlr_els_send(&fcoe->ctlr, lport, skb))
1555 elen = sizeof(struct ethhdr);
1556 hlen = sizeof(struct fcoe_hdr);
1557 tlen = sizeof(struct fcoe_crc_eof);
1558 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
1561 if (likely(lport->crc_offload)) {
1562 skb->ip_summed = CHECKSUM_UNNECESSARY;
1563 skb->csum_start = skb_headroom(skb);
1564 skb->csum_offset = skb->len;
1567 skb->ip_summed = CHECKSUM_NONE;
1568 crc = fcoe_fc_crc(fp);
1571 /* copy port crc and eof to the skb buff */
1572 if (skb_is_nonlinear(skb)) {
1574 if (fcoe_alloc_paged_crc_eof(skb, tlen)) {
1578 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
1579 cp = kmap_atomic(skb_frag_page(frag))
1580 + frag->page_offset;
1582 cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
1585 memset(cp, 0, sizeof(*cp));
1587 cp->fcoe_crc32 = cpu_to_le32(~crc);
1589 if (skb_is_nonlinear(skb)) {
1594 /* adjust skb network/transport offsets to match mac/fcoe/port */
1595 skb_push(skb, elen + hlen);
1596 skb_reset_mac_header(skb);
1597 skb_reset_network_header(skb);
1598 skb->mac_len = elen;
1599 skb->protocol = htons(ETH_P_FCOE);
1600 skb->priority = port->priority;
1602 if (fcoe->netdev->priv_flags & IFF_802_1Q_VLAN &&
1603 fcoe->realdev->features & NETIF_F_HW_VLAN_TX) {
1604 skb->vlan_tci = VLAN_TAG_PRESENT |
1605 vlan_dev_vlan_id(fcoe->netdev);
1606 skb->dev = fcoe->realdev;
1608 skb->dev = fcoe->netdev;
1610 /* fill up mac and fcoe headers */
1612 eh->h_proto = htons(ETH_P_FCOE);
1613 memcpy(eh->h_dest, fcoe->ctlr.dest_addr, ETH_ALEN);
1614 if (fcoe->ctlr.map_dest)
1615 memcpy(eh->h_dest + 3, fh->fh_d_id, 3);
1617 if (unlikely(fcoe->ctlr.flogi_oxid != FC_XID_UNKNOWN))
1618 memcpy(eh->h_source, fcoe->ctlr.ctl_src_addr, ETH_ALEN);
1620 memcpy(eh->h_source, port->data_src_addr, ETH_ALEN);
1622 hp = (struct fcoe_hdr *)(eh + 1);
1623 memset(hp, 0, sizeof(*hp));
1625 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
1628 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
1629 if (lport->seq_offload && fr_max_payload(fp)) {
1630 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
1631 skb_shinfo(skb)->gso_size = fr_max_payload(fp);
1633 skb_shinfo(skb)->gso_type = 0;
1634 skb_shinfo(skb)->gso_size = 0;
1636 /* update tx stats: regardless if LLD fails */
1637 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
1639 stats->TxWords += wlen;
1642 /* send down to lld */
1644 fcoe_port_send(port, skb);
1649 * fcoe_percpu_flush_done() - Indicate per-CPU queue flush completion
1650 * @skb: The completed skb (argument required by destructor)
1652 static void fcoe_percpu_flush_done(struct sk_buff *skb)
1654 complete(&fcoe_flush_completion);
1658 * fcoe_filter_frames() - filter out bad fcoe frames, i.e. bad CRC
1659 * @lport: The local port the frame was received on
1660 * @fp: The received frame
1662 * Return: 0 on passing filtering checks
1664 static inline int fcoe_filter_frames(struct fc_lport *lport,
1665 struct fc_frame *fp)
1667 struct fcoe_interface *fcoe;
1668 struct fc_frame_header *fh;
1669 struct sk_buff *skb = (struct sk_buff *)fp;
1670 struct fcoe_dev_stats *stats;
1673 * We only check CRC if no offload is available and if it is
1674 * it's solicited data, in which case, the FCP layer would
1675 * check it during the copy.
1677 if (lport->crc_offload && skb->ip_summed == CHECKSUM_UNNECESSARY)
1678 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1680 fr_flags(fp) |= FCPHF_CRC_UNCHECKED;
1682 fh = (struct fc_frame_header *) skb_transport_header(skb);
1683 fh = fc_frame_header_get(fp);
1684 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA && fh->fh_type == FC_TYPE_FCP)
1687 fcoe = ((struct fcoe_port *)lport_priv(lport))->priv;
1688 if (is_fip_mode(&fcoe->ctlr) && fc_frame_payload_op(fp) == ELS_LOGO &&
1689 ntoh24(fh->fh_s_id) == FC_FID_FLOGI) {
1690 FCOE_DBG("fcoe: dropping FCoE lport LOGO in fip mode\n");
1694 if (!(fr_flags(fp) & FCPHF_CRC_UNCHECKED) ||
1695 le32_to_cpu(fr_crc(fp)) == ~crc32(~0, skb->data, skb->len)) {
1696 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1700 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
1701 stats->InvalidCRCCount++;
1702 if (stats->InvalidCRCCount < 5)
1703 printk(KERN_WARNING "fcoe: dropping frame with CRC error\n");
1709 * fcoe_recv_frame() - process a single received frame
1710 * @skb: frame to process
1712 static void fcoe_recv_frame(struct sk_buff *skb)
1715 struct fc_lport *lport;
1716 struct fcoe_rcv_info *fr;
1717 struct fcoe_dev_stats *stats;
1718 struct fcoe_crc_eof crc_eof;
1719 struct fc_frame *fp;
1720 struct fcoe_port *port;
1721 struct fcoe_hdr *hp;
1723 fr = fcoe_dev_from_skb(skb);
1725 if (unlikely(!lport)) {
1726 if (skb->destructor != fcoe_percpu_flush_done)
1727 FCOE_NETDEV_DBG(skb->dev, "NULL lport in skb");
1732 FCOE_NETDEV_DBG(skb->dev, "skb_info: len:%d data_len:%d "
1733 "head:%p data:%p tail:%p end:%p sum:%d dev:%s",
1734 skb->len, skb->data_len,
1735 skb->head, skb->data, skb_tail_pointer(skb),
1736 skb_end_pointer(skb), skb->csum,
1737 skb->dev ? skb->dev->name : "<NULL>");
1739 port = lport_priv(lport);
1740 skb_linearize(skb); /* check for skb_is_nonlinear is within skb_linearize */
1743 * Frame length checks and setting up the header pointers
1744 * was done in fcoe_rcv already.
1746 hp = (struct fcoe_hdr *) skb_network_header(skb);
1748 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
1749 if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) {
1750 if (stats->ErrorFrames < 5)
1751 printk(KERN_WARNING "fcoe: FCoE version "
1752 "mismatch: The frame has "
1753 "version %x, but the "
1754 "initiator supports version "
1755 "%x\n", FC_FCOE_DECAPS_VER(hp),
1760 skb_pull(skb, sizeof(struct fcoe_hdr));
1761 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
1764 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
1766 fp = (struct fc_frame *)skb;
1769 fr_sof(fp) = hp->fcoe_sof;
1771 /* Copy out the CRC and EOF trailer for access */
1772 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof)))
1774 fr_eof(fp) = crc_eof.fcoe_eof;
1775 fr_crc(fp) = crc_eof.fcoe_crc32;
1776 if (pskb_trim(skb, fr_len))
1779 if (!fcoe_filter_frames(lport, fp)) {
1781 fc_exch_recv(lport, fp);
1785 stats->ErrorFrames++;
1791 * fcoe_percpu_receive_thread() - The per-CPU packet receive thread
1792 * @arg: The per-CPU context
1794 * Return: 0 for success
1796 static int fcoe_percpu_receive_thread(void *arg)
1798 struct fcoe_percpu_s *p = arg;
1799 struct sk_buff *skb;
1801 set_user_nice(current, -20);
1803 while (!kthread_should_stop()) {
1805 spin_lock_bh(&p->fcoe_rx_list.lock);
1806 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) == NULL) {
1807 set_current_state(TASK_INTERRUPTIBLE);
1808 spin_unlock_bh(&p->fcoe_rx_list.lock);
1810 set_current_state(TASK_RUNNING);
1811 if (kthread_should_stop())
1813 spin_lock_bh(&p->fcoe_rx_list.lock);
1815 spin_unlock_bh(&p->fcoe_rx_list.lock);
1816 fcoe_recv_frame(skb);
1822 * fcoe_dev_setup() - Setup the link change notification interface
1824 static void fcoe_dev_setup(void)
1826 register_dcbevent_notifier(&dcb_notifier);
1827 register_netdevice_notifier(&fcoe_notifier);
1831 * fcoe_dev_cleanup() - Cleanup the link change notification interface
1833 static void fcoe_dev_cleanup(void)
1835 unregister_dcbevent_notifier(&dcb_notifier);
1836 unregister_netdevice_notifier(&fcoe_notifier);
1839 static struct fcoe_interface *
1840 fcoe_hostlist_lookup_realdev_port(struct net_device *netdev)
1842 struct fcoe_interface *fcoe;
1843 struct net_device *real_dev;
1845 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
1846 if (fcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
1847 real_dev = vlan_dev_real_dev(fcoe->netdev);
1849 real_dev = fcoe->netdev;
1851 if (netdev == real_dev)
1857 static int fcoe_dcb_app_notification(struct notifier_block *notifier,
1858 ulong event, void *ptr)
1860 struct dcb_app_type *entry = ptr;
1861 struct fcoe_interface *fcoe;
1862 struct net_device *netdev;
1863 struct fcoe_port *port;
1866 if (entry->app.selector != DCB_APP_IDTYPE_ETHTYPE)
1869 netdev = dev_get_by_index(&init_net, entry->ifindex);
1873 fcoe = fcoe_hostlist_lookup_realdev_port(netdev);
1878 if (entry->dcbx & DCB_CAP_DCBX_VER_CEE)
1879 prio = ffs(entry->app.priority) - 1;
1881 prio = entry->app.priority;
1886 if (entry->app.protocol == ETH_P_FIP ||
1887 entry->app.protocol == ETH_P_FCOE)
1888 fcoe->ctlr.priority = prio;
1890 if (entry->app.protocol == ETH_P_FCOE) {
1891 port = lport_priv(fcoe->ctlr.lp);
1892 port->priority = prio;
1899 * fcoe_device_notification() - Handler for net device events
1900 * @notifier: The context of the notification
1901 * @event: The type of event
1902 * @ptr: The net device that the event was on
1904 * This function is called by the Ethernet driver in case of link change event.
1906 * Returns: 0 for success
1908 static int fcoe_device_notification(struct notifier_block *notifier,
1909 ulong event, void *ptr)
1911 struct fc_lport *lport = NULL;
1912 struct net_device *netdev = ptr;
1913 struct fcoe_interface *fcoe;
1914 struct fcoe_port *port;
1915 struct fcoe_dev_stats *stats;
1916 u32 link_possible = 1;
1920 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
1921 if (fcoe->netdev == netdev) {
1922 lport = fcoe->ctlr.lp;
1933 case NETDEV_GOING_DOWN:
1939 case NETDEV_CHANGEMTU:
1940 if (netdev->features & NETIF_F_FCOE_MTU)
1942 mfs = netdev->mtu - (sizeof(struct fcoe_hdr) +
1943 sizeof(struct fcoe_crc_eof));
1944 if (mfs >= FC_MIN_MAX_FRAME)
1945 fc_set_mfs(lport, mfs);
1947 case NETDEV_REGISTER:
1949 case NETDEV_UNREGISTER:
1950 list_del(&fcoe->list);
1951 port = lport_priv(fcoe->ctlr.lp);
1952 queue_work(fcoe_wq, &port->destroy_work);
1955 case NETDEV_FEAT_CHANGE:
1956 fcoe_netdev_features_change(lport, netdev);
1959 FCOE_NETDEV_DBG(netdev, "Unknown event %ld "
1960 "from netdev netlink\n", event);
1963 fcoe_link_speed_update(lport);
1965 if (link_possible && !fcoe_link_ok(lport))
1966 fcoe_ctlr_link_up(&fcoe->ctlr);
1967 else if (fcoe_ctlr_link_down(&fcoe->ctlr)) {
1968 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
1969 stats->LinkFailureCount++;
1971 fcoe_clean_pending_queue(lport);
1978 * fcoe_disable() - Disables a FCoE interface
1979 * @netdev : The net_device object the Ethernet interface to create on
1981 * Called from fcoe transport.
1983 * Returns: 0 for success
1985 static int fcoe_disable(struct net_device *netdev)
1987 struct fcoe_interface *fcoe;
1990 mutex_lock(&fcoe_config_mutex);
1993 fcoe = fcoe_hostlist_lookup_port(netdev);
1997 fcoe_ctlr_link_down(&fcoe->ctlr);
1998 fcoe_clean_pending_queue(fcoe->ctlr.lp);
2002 mutex_unlock(&fcoe_config_mutex);
2007 * fcoe_enable() - Enables a FCoE interface
2008 * @netdev : The net_device object the Ethernet interface to create on
2010 * Called from fcoe transport.
2012 * Returns: 0 for success
2014 static int fcoe_enable(struct net_device *netdev)
2016 struct fcoe_interface *fcoe;
2019 mutex_lock(&fcoe_config_mutex);
2021 fcoe = fcoe_hostlist_lookup_port(netdev);
2026 else if (!fcoe_link_ok(fcoe->ctlr.lp))
2027 fcoe_ctlr_link_up(&fcoe->ctlr);
2029 mutex_unlock(&fcoe_config_mutex);
2034 * fcoe_destroy() - Destroy a FCoE interface
2035 * @netdev : The net_device object the Ethernet interface to create on
2037 * Called from fcoe transport
2039 * Returns: 0 for success
2041 static int fcoe_destroy(struct net_device *netdev)
2043 struct fcoe_interface *fcoe;
2044 struct fc_lport *lport;
2045 struct fcoe_port *port;
2048 mutex_lock(&fcoe_config_mutex);
2050 fcoe = fcoe_hostlist_lookup_port(netdev);
2055 lport = fcoe->ctlr.lp;
2056 port = lport_priv(lport);
2057 list_del(&fcoe->list);
2058 queue_work(fcoe_wq, &port->destroy_work);
2061 mutex_unlock(&fcoe_config_mutex);
2066 * fcoe_destroy_work() - Destroy a FCoE port in a deferred work context
2067 * @work: Handle to the FCoE port to be destroyed
2069 static void fcoe_destroy_work(struct work_struct *work)
2071 struct fcoe_port *port;
2072 struct fcoe_interface *fcoe;
2074 port = container_of(work, struct fcoe_port, destroy_work);
2075 mutex_lock(&fcoe_config_mutex);
2078 fcoe_if_destroy(port->lport);
2079 fcoe_interface_cleanup(fcoe);
2081 mutex_unlock(&fcoe_config_mutex);
2085 * fcoe_match() - Check if the FCoE is supported on the given netdevice
2086 * @netdev : The net_device object the Ethernet interface to create on
2088 * Called from fcoe transport.
2090 * Returns: always returns true as this is the default FCoE transport,
2091 * i.e., support all netdevs.
2093 static bool fcoe_match(struct net_device *netdev)
2099 * fcoe_dcb_create() - Initialize DCB attributes and hooks
2100 * @netdev: The net_device object of the L2 link that should be queried
2101 * @port: The fcoe_port to bind FCoE APP priority with
2104 static void fcoe_dcb_create(struct fcoe_interface *fcoe)
2109 struct net_device *netdev = fcoe->realdev;
2110 struct fcoe_port *port = lport_priv(fcoe->ctlr.lp);
2111 struct dcb_app app = {
2113 .protocol = ETH_P_FCOE
2116 /* setup DCB priority attributes. */
2117 if (netdev && netdev->dcbnl_ops && netdev->dcbnl_ops->getdcbx) {
2118 dcbx = netdev->dcbnl_ops->getdcbx(netdev);
2120 if (dcbx & DCB_CAP_DCBX_VER_IEEE) {
2121 app.selector = IEEE_8021QAZ_APP_SEL_ETHERTYPE;
2122 up = dcb_ieee_getapp_mask(netdev, &app);
2123 app.protocol = ETH_P_FIP;
2124 fup = dcb_ieee_getapp_mask(netdev, &app);
2126 app.selector = DCB_APP_IDTYPE_ETHTYPE;
2127 up = dcb_getapp(netdev, &app);
2128 app.protocol = ETH_P_FIP;
2129 fup = dcb_getapp(netdev, &app);
2132 port->priority = ffs(up) ? ffs(up) - 1 : 0;
2133 fcoe->ctlr.priority = ffs(fup) ? ffs(fup) - 1 : port->priority;
2139 * fcoe_create() - Create a fcoe interface
2140 * @netdev : The net_device object the Ethernet interface to create on
2141 * @fip_mode: The FIP mode for this creation
2143 * Called from fcoe transport
2145 * Returns: 0 for success
2147 static int fcoe_create(struct net_device *netdev, enum fip_state fip_mode)
2150 struct fcoe_interface *fcoe;
2151 struct fc_lport *lport;
2153 mutex_lock(&fcoe_config_mutex);
2156 /* look for existing lport */
2157 if (fcoe_hostlist_lookup(netdev)) {
2162 fcoe = fcoe_interface_create(netdev, fip_mode);
2168 lport = fcoe_if_create(fcoe, &netdev->dev, 0);
2169 if (IS_ERR(lport)) {
2170 printk(KERN_ERR "fcoe: Failed to create interface (%s)\n",
2174 fcoe_interface_cleanup(fcoe);
2178 /* Make this the "master" N_Port */
2179 fcoe->ctlr.lp = lport;
2181 /* setup DCB priority attributes. */
2182 fcoe_dcb_create(fcoe);
2184 /* add to lports list */
2185 fcoe_hostlist_add(lport);
2187 /* start FIP Discovery and FLOGI */
2188 lport->boot_time = jiffies;
2189 fc_fabric_login(lport);
2190 if (!fcoe_link_ok(lport))
2191 fcoe_ctlr_link_up(&fcoe->ctlr);
2196 mutex_unlock(&fcoe_config_mutex);
2201 * fcoe_link_speed_update() - Update the supported and actual link speeds
2202 * @lport: The local port to update speeds for
2204 * Returns: 0 if the ethtool query was successful
2205 * -1 if the ethtool query failed
2207 static int fcoe_link_speed_update(struct fc_lport *lport)
2209 struct net_device *netdev = fcoe_netdev(lport);
2210 struct ethtool_cmd ecmd;
2212 if (!__ethtool_get_settings(netdev, &ecmd)) {
2213 lport->link_supported_speeds &=
2214 ~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
2215 if (ecmd.supported & (SUPPORTED_1000baseT_Half |
2216 SUPPORTED_1000baseT_Full))
2217 lport->link_supported_speeds |= FC_PORTSPEED_1GBIT;
2218 if (ecmd.supported & SUPPORTED_10000baseT_Full)
2219 lport->link_supported_speeds |=
2220 FC_PORTSPEED_10GBIT;
2221 switch (ethtool_cmd_speed(&ecmd)) {
2223 lport->link_speed = FC_PORTSPEED_1GBIT;
2226 lport->link_speed = FC_PORTSPEED_10GBIT;
2235 * fcoe_link_ok() - Check if the link is OK for a local port
2236 * @lport: The local port to check link on
2238 * Returns: 0 if link is UP and OK, -1 if not
2241 static int fcoe_link_ok(struct fc_lport *lport)
2243 struct net_device *netdev = fcoe_netdev(lport);
2245 if (netif_oper_up(netdev))
2251 * fcoe_percpu_clean() - Clear all pending skbs for an local port
2252 * @lport: The local port whose skbs are to be cleared
2254 * Must be called with fcoe_create_mutex held to single-thread completion.
2256 * This flushes the pending skbs by adding a new skb to each queue and
2257 * waiting until they are all freed. This assures us that not only are
2258 * there no packets that will be handled by the lport, but also that any
2259 * threads already handling packet have returned.
2261 static void fcoe_percpu_clean(struct fc_lport *lport)
2263 struct fcoe_percpu_s *pp;
2264 struct fcoe_rcv_info *fr;
2265 struct sk_buff_head *list;
2266 struct sk_buff *skb, *next;
2267 struct sk_buff *head;
2270 for_each_possible_cpu(cpu) {
2271 pp = &per_cpu(fcoe_percpu, cpu);
2272 spin_lock_bh(&pp->fcoe_rx_list.lock);
2273 list = &pp->fcoe_rx_list;
2275 for (skb = head; skb != (struct sk_buff *)list;
2278 fr = fcoe_dev_from_skb(skb);
2279 if (fr->fr_dev == lport) {
2280 __skb_unlink(skb, list);
2285 if (!pp->thread || !cpu_online(cpu)) {
2286 spin_unlock_bh(&pp->fcoe_rx_list.lock);
2290 skb = dev_alloc_skb(0);
2292 spin_unlock_bh(&pp->fcoe_rx_list.lock);
2295 skb->destructor = fcoe_percpu_flush_done;
2297 __skb_queue_tail(&pp->fcoe_rx_list, skb);
2298 if (pp->fcoe_rx_list.qlen == 1)
2299 wake_up_process(pp->thread);
2300 spin_unlock_bh(&pp->fcoe_rx_list.lock);
2302 wait_for_completion(&fcoe_flush_completion);
2307 * fcoe_reset() - Reset a local port
2308 * @shost: The SCSI host associated with the local port to be reset
2310 * Returns: Always 0 (return value required by FC transport template)
2312 static int fcoe_reset(struct Scsi_Host *shost)
2314 struct fc_lport *lport = shost_priv(shost);
2315 struct fcoe_port *port = lport_priv(lport);
2316 struct fcoe_interface *fcoe = port->priv;
2318 fcoe_ctlr_link_down(&fcoe->ctlr);
2319 fcoe_clean_pending_queue(fcoe->ctlr.lp);
2320 if (!fcoe_link_ok(fcoe->ctlr.lp))
2321 fcoe_ctlr_link_up(&fcoe->ctlr);
2326 * fcoe_hostlist_lookup_port() - Find the FCoE interface associated with a net device
2327 * @netdev: The net device used as a key
2329 * Locking: Must be called with the RNL mutex held.
2331 * Returns: NULL or the FCoE interface
2333 static struct fcoe_interface *
2334 fcoe_hostlist_lookup_port(const struct net_device *netdev)
2336 struct fcoe_interface *fcoe;
2338 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
2339 if (fcoe->netdev == netdev)
2346 * fcoe_hostlist_lookup() - Find the local port associated with a
2348 * @netdev: The netdevice used as a key
2350 * Locking: Must be called with the RTNL mutex held
2352 * Returns: NULL or the local port
2354 static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *netdev)
2356 struct fcoe_interface *fcoe;
2358 fcoe = fcoe_hostlist_lookup_port(netdev);
2359 return (fcoe) ? fcoe->ctlr.lp : NULL;
2363 * fcoe_hostlist_add() - Add the FCoE interface identified by a local
2364 * port to the hostlist
2365 * @lport: The local port that identifies the FCoE interface to be added
2367 * Locking: must be called with the RTNL mutex held
2369 * Returns: 0 for success
2371 static int fcoe_hostlist_add(const struct fc_lport *lport)
2373 struct fcoe_interface *fcoe;
2374 struct fcoe_port *port;
2376 fcoe = fcoe_hostlist_lookup_port(fcoe_netdev(lport));
2378 port = lport_priv(lport);
2380 list_add_tail(&fcoe->list, &fcoe_hostlist);
2386 static struct fcoe_transport fcoe_sw_transport = {
2387 .name = {FCOE_TRANSPORT_DEFAULT},
2389 .list = LIST_HEAD_INIT(fcoe_sw_transport.list),
2390 .match = fcoe_match,
2391 .create = fcoe_create,
2392 .destroy = fcoe_destroy,
2393 .enable = fcoe_enable,
2394 .disable = fcoe_disable,
2398 * fcoe_init() - Initialize fcoe.ko
2400 * Returns: 0 on success, or a negative value on failure
2402 static int __init fcoe_init(void)
2404 struct fcoe_percpu_s *p;
2408 fcoe_wq = alloc_workqueue("fcoe", 0, 0);
2412 /* register as a fcoe transport */
2413 rc = fcoe_transport_attach(&fcoe_sw_transport);
2415 printk(KERN_ERR "failed to register an fcoe transport, check "
2416 "if libfcoe is loaded\n");
2420 mutex_lock(&fcoe_config_mutex);
2422 for_each_possible_cpu(cpu) {
2423 p = &per_cpu(fcoe_percpu, cpu);
2424 skb_queue_head_init(&p->fcoe_rx_list);
2427 for_each_online_cpu(cpu)
2428 fcoe_percpu_thread_create(cpu);
2430 /* Initialize per CPU interrupt thread */
2431 rc = register_hotcpu_notifier(&fcoe_cpu_notifier);
2435 /* Setup link change notification */
2438 rc = fcoe_if_init();
2442 mutex_unlock(&fcoe_config_mutex);
2446 for_each_online_cpu(cpu) {
2447 fcoe_percpu_thread_destroy(cpu);
2449 mutex_unlock(&fcoe_config_mutex);
2450 destroy_workqueue(fcoe_wq);
2453 module_init(fcoe_init);
2456 * fcoe_exit() - Clean up fcoe.ko
2458 * Returns: 0 on success or a negative value on failure
2460 static void __exit fcoe_exit(void)
2462 struct fcoe_interface *fcoe, *tmp;
2463 struct fcoe_port *port;
2466 mutex_lock(&fcoe_config_mutex);
2470 /* releases the associated fcoe hosts */
2472 list_for_each_entry_safe(fcoe, tmp, &fcoe_hostlist, list) {
2473 list_del(&fcoe->list);
2474 port = lport_priv(fcoe->ctlr.lp);
2475 queue_work(fcoe_wq, &port->destroy_work);
2479 unregister_hotcpu_notifier(&fcoe_cpu_notifier);
2481 for_each_online_cpu(cpu)
2482 fcoe_percpu_thread_destroy(cpu);
2484 mutex_unlock(&fcoe_config_mutex);
2487 * destroy_work's may be chained but destroy_workqueue()
2488 * can take care of them. Just kill the fcoe_wq.
2490 destroy_workqueue(fcoe_wq);
2493 * Detaching from the scsi transport must happen after all
2494 * destroys are done on the fcoe_wq. destroy_workqueue will
2495 * enusre the fcoe_wq is flushed.
2499 /* detach from fcoe transport */
2500 fcoe_transport_detach(&fcoe_sw_transport);
2502 module_exit(fcoe_exit);
2505 * fcoe_flogi_resp() - FCoE specific FLOGI and FDISC response handler
2506 * @seq: active sequence in the FLOGI or FDISC exchange
2507 * @fp: response frame, or error encoded in a pointer (timeout)
2508 * @arg: pointer the the fcoe_ctlr structure
2510 * This handles MAC address management for FCoE, then passes control on to
2511 * the libfc FLOGI response handler.
2513 static void fcoe_flogi_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
2515 struct fcoe_ctlr *fip = arg;
2516 struct fc_exch *exch = fc_seq_exch(seq);
2517 struct fc_lport *lport = exch->lp;
2523 mac = fr_cb(fp)->granted_mac;
2525 if (is_zero_ether_addr(mac))
2526 fcoe_ctlr_recv_flogi(fip, lport, fp);
2527 if (!is_zero_ether_addr(mac))
2528 fcoe_update_src_mac(lport, mac);
2530 fc_lport_flogi_resp(seq, fp, lport);
2534 * fcoe_logo_resp() - FCoE specific LOGO response handler
2535 * @seq: active sequence in the LOGO exchange
2536 * @fp: response frame, or error encoded in a pointer (timeout)
2537 * @arg: pointer the the fcoe_ctlr structure
2539 * This handles MAC address management for FCoE, then passes control on to
2540 * the libfc LOGO response handler.
2542 static void fcoe_logo_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
2544 struct fc_lport *lport = arg;
2545 static u8 zero_mac[ETH_ALEN] = { 0 };
2548 fcoe_update_src_mac(lport, zero_mac);
2549 fc_lport_logo_resp(seq, fp, lport);
2553 * fcoe_elsct_send - FCoE specific ELS handler
2555 * This does special case handling of FIP encapsualted ELS exchanges for FCoE,
2556 * using FCoE specific response handlers and passing the FIP controller as
2557 * the argument (the lport is still available from the exchange).
2559 * Most of the work here is just handed off to the libfc routine.
2561 static struct fc_seq *fcoe_elsct_send(struct fc_lport *lport, u32 did,
2562 struct fc_frame *fp, unsigned int op,
2563 void (*resp)(struct fc_seq *,
2566 void *arg, u32 timeout)
2568 struct fcoe_port *port = lport_priv(lport);
2569 struct fcoe_interface *fcoe = port->priv;
2570 struct fcoe_ctlr *fip = &fcoe->ctlr;
2571 struct fc_frame_header *fh = fc_frame_header_get(fp);
2576 if (lport->point_to_multipoint)
2578 return fc_elsct_send(lport, did, fp, op, fcoe_flogi_resp,
2581 /* only hook onto fabric logouts, not port logouts */
2582 if (ntoh24(fh->fh_d_id) != FC_FID_FLOGI)
2584 return fc_elsct_send(lport, did, fp, op, fcoe_logo_resp,
2587 return fc_elsct_send(lport, did, fp, op, resp, arg, timeout);
2591 * fcoe_vport_create() - create an fc_host/scsi_host for a vport
2592 * @vport: fc_vport object to create a new fc_host for
2593 * @disabled: start the new fc_host in a disabled state by default?
2595 * Returns: 0 for success
2597 static int fcoe_vport_create(struct fc_vport *vport, bool disabled)
2599 struct Scsi_Host *shost = vport_to_shost(vport);
2600 struct fc_lport *n_port = shost_priv(shost);
2601 struct fcoe_port *port = lport_priv(n_port);
2602 struct fcoe_interface *fcoe = port->priv;
2603 struct net_device *netdev = fcoe->netdev;
2604 struct fc_lport *vn_port;
2608 rc = fcoe_validate_vport_create(vport);
2610 fcoe_wwn_to_str(vport->port_name, buf, sizeof(buf));
2611 printk(KERN_ERR "fcoe: Failed to create vport, "
2612 "WWPN (0x%s) already exists\n",
2617 mutex_lock(&fcoe_config_mutex);
2619 vn_port = fcoe_if_create(fcoe, &vport->dev, 1);
2621 mutex_unlock(&fcoe_config_mutex);
2623 if (IS_ERR(vn_port)) {
2624 printk(KERN_ERR "fcoe: fcoe_vport_create(%s) failed\n",
2630 fc_vport_set_state(vport, FC_VPORT_DISABLED);
2632 vn_port->boot_time = jiffies;
2633 fc_fabric_login(vn_port);
2634 fc_vport_setlink(vn_port);
2640 * fcoe_vport_destroy() - destroy the fc_host/scsi_host for a vport
2641 * @vport: fc_vport object that is being destroyed
2643 * Returns: 0 for success
2645 static int fcoe_vport_destroy(struct fc_vport *vport)
2647 struct Scsi_Host *shost = vport_to_shost(vport);
2648 struct fc_lport *n_port = shost_priv(shost);
2649 struct fc_lport *vn_port = vport->dd_data;
2651 mutex_lock(&n_port->lp_mutex);
2652 list_del(&vn_port->list);
2653 mutex_unlock(&n_port->lp_mutex);
2655 mutex_lock(&fcoe_config_mutex);
2656 fcoe_if_destroy(vn_port);
2657 mutex_unlock(&fcoe_config_mutex);
2663 * fcoe_vport_disable() - change vport state
2664 * @vport: vport to bring online/offline
2665 * @disable: should the vport be disabled?
2667 static int fcoe_vport_disable(struct fc_vport *vport, bool disable)
2669 struct fc_lport *lport = vport->dd_data;
2672 fc_vport_set_state(vport, FC_VPORT_DISABLED);
2673 fc_fabric_logoff(lport);
2675 lport->boot_time = jiffies;
2676 fc_fabric_login(lport);
2677 fc_vport_setlink(lport);
2684 * fcoe_vport_set_symbolic_name() - append vport string to symbolic name
2685 * @vport: fc_vport with a new symbolic name string
2687 * After generating a new symbolic name string, a new RSPN_ID request is
2688 * sent to the name server. There is no response handler, so if it fails
2689 * for some reason it will not be retried.
2691 static void fcoe_set_vport_symbolic_name(struct fc_vport *vport)
2693 struct fc_lport *lport = vport->dd_data;
2694 struct fc_frame *fp;
2697 snprintf(fc_host_symbolic_name(lport->host), FC_SYMBOLIC_NAME_SIZE,
2698 "%s v%s over %s : %s", FCOE_NAME, FCOE_VERSION,
2699 fcoe_netdev(lport)->name, vport->symbolic_name);
2701 if (lport->state != LPORT_ST_READY)
2704 len = strnlen(fc_host_symbolic_name(lport->host), 255);
2705 fp = fc_frame_alloc(lport,
2706 sizeof(struct fc_ct_hdr) +
2707 sizeof(struct fc_ns_rspn) + len);
2710 lport->tt.elsct_send(lport, FC_FID_DIR_SERV, fp, FC_NS_RSPN_ID,
2711 NULL, NULL, 3 * lport->r_a_tov);
2715 * fcoe_get_lesb() - Fill the FCoE Link Error Status Block
2716 * @lport: the local port
2717 * @fc_lesb: the link error status block
2719 static void fcoe_get_lesb(struct fc_lport *lport,
2720 struct fc_els_lesb *fc_lesb)
2722 struct net_device *netdev = fcoe_netdev(lport);
2724 __fcoe_get_lesb(lport, fc_lesb, netdev);
2728 * fcoe_set_port_id() - Callback from libfc when Port_ID is set.
2729 * @lport: the local port
2730 * @port_id: the port ID
2731 * @fp: the received frame, if any, that caused the port_id to be set.
2733 * This routine handles the case where we received a FLOGI and are
2734 * entering point-to-point mode. We need to call fcoe_ctlr_recv_flogi()
2735 * so it can set the non-mapped mode and gateway address.
2737 * The FLOGI LS_ACC is handled by fcoe_flogi_resp().
2739 static void fcoe_set_port_id(struct fc_lport *lport,
2740 u32 port_id, struct fc_frame *fp)
2742 struct fcoe_port *port = lport_priv(lport);
2743 struct fcoe_interface *fcoe = port->priv;
2745 if (fp && fc_frame_payload_op(fp) == ELS_FLOGI)
2746 fcoe_ctlr_recv_flogi(&fcoe->ctlr, lport, fp);