SCSI: fcoe: Fix preempt count leak in fcoe_filter_frames()
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / scsi / fcoe / fcoe.c
blobf829adcb3b79f64833e50b81da021651293c1588
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 *);
140 static int fcoe_validate_vport_create(struct fc_vport *);
142 static struct libfc_function_template fcoe_libfc_fcn_templ = {
143 .frame_send = fcoe_xmit,
144 .ddp_setup = fcoe_ddp_setup,
145 .ddp_done = fcoe_ddp_done,
146 .elsct_send = fcoe_elsct_send,
147 .get_lesb = fcoe_get_lesb,
148 .lport_set_port_id = fcoe_set_port_id,
151 struct fc_function_template fcoe_nport_fc_functions = {
152 .show_host_node_name = 1,
153 .show_host_port_name = 1,
154 .show_host_supported_classes = 1,
155 .show_host_supported_fc4s = 1,
156 .show_host_active_fc4s = 1,
157 .show_host_maxframe_size = 1,
159 .show_host_port_id = 1,
160 .show_host_supported_speeds = 1,
161 .get_host_speed = fc_get_host_speed,
162 .show_host_speed = 1,
163 .show_host_port_type = 1,
164 .get_host_port_state = fc_get_host_port_state,
165 .show_host_port_state = 1,
166 .show_host_symbolic_name = 1,
168 .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
169 .show_rport_maxframe_size = 1,
170 .show_rport_supported_classes = 1,
172 .show_host_fabric_name = 1,
173 .show_starget_node_name = 1,
174 .show_starget_port_name = 1,
175 .show_starget_port_id = 1,
176 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
177 .show_rport_dev_loss_tmo = 1,
178 .get_fc_host_stats = fc_get_host_stats,
179 .issue_fc_host_lip = fcoe_reset,
181 .terminate_rport_io = fc_rport_terminate_io,
183 .vport_create = fcoe_vport_create,
184 .vport_delete = fcoe_vport_destroy,
185 .vport_disable = fcoe_vport_disable,
186 .set_vport_symbolic_name = fcoe_set_vport_symbolic_name,
188 .bsg_request = fc_lport_bsg_request,
191 struct fc_function_template fcoe_vport_fc_functions = {
192 .show_host_node_name = 1,
193 .show_host_port_name = 1,
194 .show_host_supported_classes = 1,
195 .show_host_supported_fc4s = 1,
196 .show_host_active_fc4s = 1,
197 .show_host_maxframe_size = 1,
199 .show_host_port_id = 1,
200 .show_host_supported_speeds = 1,
201 .get_host_speed = fc_get_host_speed,
202 .show_host_speed = 1,
203 .show_host_port_type = 1,
204 .get_host_port_state = fc_get_host_port_state,
205 .show_host_port_state = 1,
206 .show_host_symbolic_name = 1,
208 .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
209 .show_rport_maxframe_size = 1,
210 .show_rport_supported_classes = 1,
212 .show_host_fabric_name = 1,
213 .show_starget_node_name = 1,
214 .show_starget_port_name = 1,
215 .show_starget_port_id = 1,
216 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
217 .show_rport_dev_loss_tmo = 1,
218 .get_fc_host_stats = fc_get_host_stats,
219 .issue_fc_host_lip = fcoe_reset,
221 .terminate_rport_io = fc_rport_terminate_io,
223 .bsg_request = fc_lport_bsg_request,
226 static struct scsi_host_template fcoe_shost_template = {
227 .module = THIS_MODULE,
228 .name = "FCoE Driver",
229 .proc_name = FCOE_NAME,
230 .queuecommand = fc_queuecommand,
231 .eh_abort_handler = fc_eh_abort,
232 .eh_device_reset_handler = fc_eh_device_reset,
233 .eh_host_reset_handler = fc_eh_host_reset,
234 .slave_alloc = fc_slave_alloc,
235 .change_queue_depth = fc_change_queue_depth,
236 .change_queue_type = fc_change_queue_type,
237 .this_id = -1,
238 .cmd_per_lun = 3,
239 .can_queue = FCOE_MAX_OUTSTANDING_COMMANDS,
240 .use_clustering = ENABLE_CLUSTERING,
241 .sg_tablesize = SG_ALL,
242 .max_sectors = 0xffff,
246 * fcoe_interface_setup() - Setup a FCoE interface
247 * @fcoe: The new FCoE interface
248 * @netdev: The net device that the fcoe interface is on
250 * Returns : 0 for success
251 * Locking: must be called with the RTNL mutex held
253 static int fcoe_interface_setup(struct fcoe_interface *fcoe,
254 struct net_device *netdev)
256 struct fcoe_ctlr *fip = &fcoe->ctlr;
257 struct netdev_hw_addr *ha;
258 struct net_device *real_dev;
259 u8 flogi_maddr[ETH_ALEN];
260 const struct net_device_ops *ops;
262 fcoe->netdev = netdev;
264 /* Let LLD initialize for FCoE */
265 ops = netdev->netdev_ops;
266 if (ops->ndo_fcoe_enable) {
267 if (ops->ndo_fcoe_enable(netdev))
268 FCOE_NETDEV_DBG(netdev, "Failed to enable FCoE"
269 " specific feature for LLD.\n");
272 /* Do not support for bonding device */
273 if (netdev->priv_flags & IFF_BONDING && netdev->flags & IFF_MASTER) {
274 FCOE_NETDEV_DBG(netdev, "Bonded interfaces not supported\n");
275 return -EOPNOTSUPP;
278 /* look for SAN MAC address, if multiple SAN MACs exist, only
279 * use the first one for SPMA */
280 real_dev = (netdev->priv_flags & IFF_802_1Q_VLAN) ?
281 vlan_dev_real_dev(netdev) : netdev;
282 rcu_read_lock();
283 for_each_dev_addr(real_dev, ha) {
284 if ((ha->type == NETDEV_HW_ADDR_T_SAN) &&
285 (is_valid_ether_addr(ha->addr))) {
286 memcpy(fip->ctl_src_addr, ha->addr, ETH_ALEN);
287 fip->spma = 1;
288 break;
291 rcu_read_unlock();
293 /* setup Source Mac Address */
294 if (!fip->spma)
295 memcpy(fip->ctl_src_addr, netdev->dev_addr, netdev->addr_len);
298 * Add FCoE MAC address as second unicast MAC address
299 * or enter promiscuous mode if not capable of listening
300 * for multiple unicast MACs.
302 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
303 dev_uc_add(netdev, flogi_maddr);
304 if (fip->spma)
305 dev_uc_add(netdev, fip->ctl_src_addr);
306 if (fip->mode == FIP_MODE_VN2VN) {
307 dev_mc_add(netdev, FIP_ALL_VN2VN_MACS);
308 dev_mc_add(netdev, FIP_ALL_P2P_MACS);
309 } else
310 dev_mc_add(netdev, FIP_ALL_ENODE_MACS);
313 * setup the receive function from ethernet driver
314 * on the ethertype for the given device
316 fcoe->fcoe_packet_type.func = fcoe_rcv;
317 fcoe->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
318 fcoe->fcoe_packet_type.dev = netdev;
319 dev_add_pack(&fcoe->fcoe_packet_type);
321 fcoe->fip_packet_type.func = fcoe_fip_recv;
322 fcoe->fip_packet_type.type = htons(ETH_P_FIP);
323 fcoe->fip_packet_type.dev = netdev;
324 dev_add_pack(&fcoe->fip_packet_type);
326 return 0;
330 * fcoe_interface_create() - Create a FCoE interface on a net device
331 * @netdev: The net device to create the FCoE interface on
332 * @fip_mode: The mode to use for FIP
334 * Returns: pointer to a struct fcoe_interface or NULL on error
336 static struct fcoe_interface *fcoe_interface_create(struct net_device *netdev,
337 enum fip_state fip_mode)
339 struct fcoe_interface *fcoe;
340 int err;
342 if (!try_module_get(THIS_MODULE)) {
343 FCOE_NETDEV_DBG(netdev,
344 "Could not get a reference to the module\n");
345 fcoe = ERR_PTR(-EBUSY);
346 goto out;
349 fcoe = kzalloc(sizeof(*fcoe), GFP_KERNEL);
350 if (!fcoe) {
351 FCOE_NETDEV_DBG(netdev, "Could not allocate fcoe structure\n");
352 fcoe = ERR_PTR(-ENOMEM);
353 goto out_nomod;
356 dev_hold(netdev);
357 kref_init(&fcoe->kref);
360 * Initialize FIP.
362 fcoe_ctlr_init(&fcoe->ctlr, fip_mode);
363 fcoe->ctlr.send = fcoe_fip_send;
364 fcoe->ctlr.update_mac = fcoe_update_src_mac;
365 fcoe->ctlr.get_src_addr = fcoe_get_src_mac;
367 err = fcoe_interface_setup(fcoe, netdev);
368 if (err) {
369 fcoe_ctlr_destroy(&fcoe->ctlr);
370 kfree(fcoe);
371 dev_put(netdev);
372 fcoe = ERR_PTR(err);
373 goto out_nomod;
376 goto out;
378 out_nomod:
379 module_put(THIS_MODULE);
380 out:
381 return fcoe;
385 * fcoe_interface_release() - fcoe_port kref release function
386 * @kref: Embedded reference count in an fcoe_interface struct
388 static void fcoe_interface_release(struct kref *kref)
390 struct fcoe_interface *fcoe;
391 struct net_device *netdev;
393 fcoe = container_of(kref, struct fcoe_interface, kref);
394 netdev = fcoe->netdev;
395 /* tear-down the FCoE controller */
396 fcoe_ctlr_destroy(&fcoe->ctlr);
397 kfree(fcoe);
398 dev_put(netdev);
399 module_put(THIS_MODULE);
403 * fcoe_interface_get() - Get a reference to a FCoE interface
404 * @fcoe: The FCoE interface to be held
406 static inline void fcoe_interface_get(struct fcoe_interface *fcoe)
408 kref_get(&fcoe->kref);
412 * fcoe_interface_put() - Put a reference to a FCoE interface
413 * @fcoe: The FCoE interface to be released
415 static inline void fcoe_interface_put(struct fcoe_interface *fcoe)
417 kref_put(&fcoe->kref, fcoe_interface_release);
421 * fcoe_interface_cleanup() - Clean up a FCoE interface
422 * @fcoe: The FCoE interface to be cleaned up
424 * Caller must be holding the RTNL mutex
426 void fcoe_interface_cleanup(struct fcoe_interface *fcoe)
428 struct net_device *netdev = fcoe->netdev;
429 struct fcoe_ctlr *fip = &fcoe->ctlr;
430 u8 flogi_maddr[ETH_ALEN];
431 const struct net_device_ops *ops;
432 struct fcoe_port *port = lport_priv(fcoe->ctlr.lp);
434 FCOE_NETDEV_DBG(netdev, "Destroying interface\n");
436 /* Logout of the fabric */
437 fc_fabric_logoff(fcoe->ctlr.lp);
439 /* Cleanup the fc_lport */
440 fc_lport_destroy(fcoe->ctlr.lp);
442 /* Stop the transmit retry timer */
443 del_timer_sync(&port->timer);
445 /* Free existing transmit skbs */
446 fcoe_clean_pending_queue(fcoe->ctlr.lp);
449 * Don't listen for Ethernet packets anymore.
450 * synchronize_net() ensures that the packet handlers are not running
451 * on another CPU. dev_remove_pack() would do that, this calls the
452 * unsyncronized version __dev_remove_pack() to avoid multiple delays.
454 __dev_remove_pack(&fcoe->fcoe_packet_type);
455 __dev_remove_pack(&fcoe->fip_packet_type);
456 synchronize_net();
458 /* Delete secondary MAC addresses */
459 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
460 dev_uc_del(netdev, flogi_maddr);
461 if (fip->spma)
462 dev_uc_del(netdev, fip->ctl_src_addr);
463 if (fip->mode == FIP_MODE_VN2VN) {
464 dev_mc_del(netdev, FIP_ALL_VN2VN_MACS);
465 dev_mc_del(netdev, FIP_ALL_P2P_MACS);
466 } else
467 dev_mc_del(netdev, FIP_ALL_ENODE_MACS);
469 if (!is_zero_ether_addr(port->data_src_addr))
470 dev_uc_del(netdev, port->data_src_addr);
472 /* Tell the LLD we are done w/ FCoE */
473 ops = netdev->netdev_ops;
474 if (ops->ndo_fcoe_disable) {
475 if (ops->ndo_fcoe_disable(netdev))
476 FCOE_NETDEV_DBG(netdev, "Failed to disable FCoE"
477 " specific feature for LLD.\n");
479 fcoe_interface_put(fcoe);
483 * fcoe_fip_recv() - Handler for received FIP frames
484 * @skb: The receive skb
485 * @netdev: The associated net device
486 * @ptype: The packet_type structure which was used to register this handler
487 * @orig_dev: The original net_device the the skb was received on.
488 * (in case dev is a bond)
490 * Returns: 0 for success
492 static int fcoe_fip_recv(struct sk_buff *skb, struct net_device *netdev,
493 struct packet_type *ptype,
494 struct net_device *orig_dev)
496 struct fcoe_interface *fcoe;
498 fcoe = container_of(ptype, struct fcoe_interface, fip_packet_type);
499 fcoe_ctlr_recv(&fcoe->ctlr, skb);
500 return 0;
504 * fcoe_fip_send() - Send an Ethernet-encapsulated FIP frame
505 * @fip: The FCoE controller
506 * @skb: The FIP packet to be sent
508 static void fcoe_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
510 skb->dev = fcoe_from_ctlr(fip)->netdev;
511 dev_queue_xmit(skb);
515 * fcoe_update_src_mac() - Update the Ethernet MAC filters
516 * @lport: The local port to update the source MAC on
517 * @addr: Unicast MAC address to add
519 * Remove any previously-set unicast MAC filter.
520 * Add secondary FCoE MAC address filter for our OUI.
522 static void fcoe_update_src_mac(struct fc_lport *lport, u8 *addr)
524 struct fcoe_port *port = lport_priv(lport);
525 struct fcoe_interface *fcoe = port->priv;
527 rtnl_lock();
528 if (!is_zero_ether_addr(port->data_src_addr))
529 dev_uc_del(fcoe->netdev, port->data_src_addr);
530 if (!is_zero_ether_addr(addr))
531 dev_uc_add(fcoe->netdev, addr);
532 memcpy(port->data_src_addr, addr, ETH_ALEN);
533 rtnl_unlock();
537 * fcoe_get_src_mac() - return the Ethernet source address for an lport
538 * @lport: libfc lport
540 static u8 *fcoe_get_src_mac(struct fc_lport *lport)
542 struct fcoe_port *port = lport_priv(lport);
544 return port->data_src_addr;
548 * fcoe_lport_config() - Set up a local port
549 * @lport: The local port to be setup
551 * Returns: 0 for success
553 static int fcoe_lport_config(struct fc_lport *lport)
555 lport->link_up = 0;
556 lport->qfull = 0;
557 lport->max_retry_count = 3;
558 lport->max_rport_retry_count = 3;
559 lport->e_d_tov = 2 * 1000; /* FC-FS default */
560 lport->r_a_tov = 2 * 2 * 1000;
561 lport->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
562 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
563 lport->does_npiv = 1;
565 fc_lport_init_stats(lport);
567 /* lport fc_lport related configuration */
568 fc_lport_config(lport);
570 /* offload related configuration */
571 lport->crc_offload = 0;
572 lport->seq_offload = 0;
573 lport->lro_enabled = 0;
574 lport->lro_xid = 0;
575 lport->lso_max = 0;
577 return 0;
581 * fcoe_get_wwn() - Get the world wide name from LLD if it supports it
582 * @netdev: the associated net device
583 * @wwn: the output WWN
584 * @type: the type of WWN (WWPN or WWNN)
586 * Returns: 0 for success
588 static int fcoe_get_wwn(struct net_device *netdev, u64 *wwn, int type)
590 const struct net_device_ops *ops = netdev->netdev_ops;
592 if (ops->ndo_fcoe_get_wwn)
593 return ops->ndo_fcoe_get_wwn(netdev, wwn, type);
594 return -EINVAL;
598 * fcoe_netdev_features_change - Updates the lport's offload flags based
599 * on the LLD netdev's FCoE feature flags
601 static void fcoe_netdev_features_change(struct fc_lport *lport,
602 struct net_device *netdev)
604 mutex_lock(&lport->lp_mutex);
606 if (netdev->features & NETIF_F_SG)
607 lport->sg_supp = 1;
608 else
609 lport->sg_supp = 0;
611 if (netdev->features & NETIF_F_FCOE_CRC) {
612 lport->crc_offload = 1;
613 FCOE_NETDEV_DBG(netdev, "Supports FCCRC offload\n");
614 } else {
615 lport->crc_offload = 0;
618 if (netdev->features & NETIF_F_FSO) {
619 lport->seq_offload = 1;
620 lport->lso_max = netdev->gso_max_size;
621 FCOE_NETDEV_DBG(netdev, "Supports LSO for max len 0x%x\n",
622 lport->lso_max);
623 } else {
624 lport->seq_offload = 0;
625 lport->lso_max = 0;
628 if (netdev->fcoe_ddp_xid) {
629 lport->lro_enabled = 1;
630 lport->lro_xid = netdev->fcoe_ddp_xid;
631 FCOE_NETDEV_DBG(netdev, "Supports LRO for max xid 0x%x\n",
632 lport->lro_xid);
633 } else {
634 lport->lro_enabled = 0;
635 lport->lro_xid = 0;
638 mutex_unlock(&lport->lp_mutex);
642 * fcoe_netdev_config() - Set up net devive for SW FCoE
643 * @lport: The local port that is associated with the net device
644 * @netdev: The associated net device
646 * Must be called after fcoe_lport_config() as it will use local port mutex
648 * Returns: 0 for success
650 static int fcoe_netdev_config(struct fc_lport *lport, struct net_device *netdev)
652 u32 mfs;
653 u64 wwnn, wwpn;
654 struct fcoe_interface *fcoe;
655 struct fcoe_port *port;
657 /* Setup lport private data to point to fcoe softc */
658 port = lport_priv(lport);
659 fcoe = port->priv;
662 * Determine max frame size based on underlying device and optional
663 * user-configured limit. If the MFS is too low, fcoe_link_ok()
664 * will return 0, so do this first.
666 mfs = netdev->mtu;
667 if (netdev->features & NETIF_F_FCOE_MTU) {
668 mfs = FCOE_MTU;
669 FCOE_NETDEV_DBG(netdev, "Supports FCOE_MTU of %d bytes\n", mfs);
671 mfs -= (sizeof(struct fcoe_hdr) + sizeof(struct fcoe_crc_eof));
672 if (fc_set_mfs(lport, mfs))
673 return -EINVAL;
675 /* offload features support */
676 fcoe_netdev_features_change(lport, netdev);
678 skb_queue_head_init(&port->fcoe_pending_queue);
679 port->fcoe_pending_queue_active = 0;
680 setup_timer(&port->timer, fcoe_queue_timer, (unsigned long)lport);
682 fcoe_link_speed_update(lport);
684 if (!lport->vport) {
685 if (fcoe_get_wwn(netdev, &wwnn, NETDEV_FCOE_WWNN))
686 wwnn = fcoe_wwn_from_mac(fcoe->ctlr.ctl_src_addr, 1, 0);
687 fc_set_wwnn(lport, wwnn);
688 if (fcoe_get_wwn(netdev, &wwpn, NETDEV_FCOE_WWPN))
689 wwpn = fcoe_wwn_from_mac(fcoe->ctlr.ctl_src_addr,
690 2, 0);
691 fc_set_wwpn(lport, wwpn);
694 return 0;
698 * fcoe_shost_config() - Set up the SCSI host associated with a local port
699 * @lport: The local port
700 * @dev: The device associated with the SCSI host
702 * Must be called after fcoe_lport_config() and fcoe_netdev_config()
704 * Returns: 0 for success
706 static int fcoe_shost_config(struct fc_lport *lport, struct device *dev)
708 int rc = 0;
710 /* lport scsi host config */
711 lport->host->max_lun = FCOE_MAX_LUN;
712 lport->host->max_id = FCOE_MAX_FCP_TARGET;
713 lport->host->max_channel = 0;
714 lport->host->max_cmd_len = FCOE_MAX_CMD_LEN;
716 if (lport->vport)
717 lport->host->transportt = fcoe_vport_scsi_transport;
718 else
719 lport->host->transportt = fcoe_nport_scsi_transport;
721 /* add the new host to the SCSI-ml */
722 rc = scsi_add_host(lport->host, dev);
723 if (rc) {
724 FCOE_NETDEV_DBG(fcoe_netdev(lport), "fcoe_shost_config: "
725 "error on scsi_add_host\n");
726 return rc;
729 if (!lport->vport)
730 fc_host_max_npiv_vports(lport->host) = USHRT_MAX;
732 snprintf(fc_host_symbolic_name(lport->host), FC_SYMBOLIC_NAME_SIZE,
733 "%s v%s over %s", FCOE_NAME, FCOE_VERSION,
734 fcoe_netdev(lport)->name);
736 return 0;
740 * fcoe_oem_match() - The match routine for the offloaded exchange manager
741 * @fp: The I/O frame
743 * This routine will be associated with an exchange manager (EM). When
744 * the libfc exchange handling code is looking for an EM to use it will
745 * call this routine and pass it the frame that it wishes to send. This
746 * routine will return True if the associated EM is to be used and False
747 * if the echange code should continue looking for an EM.
749 * The offload EM that this routine is associated with will handle any
750 * packets that are for SCSI read requests.
752 * This has been enhanced to work when FCoE stack is operating in target
753 * mode.
755 * Returns: True for read types I/O, otherwise returns false.
757 bool fcoe_oem_match(struct fc_frame *fp)
759 struct fc_frame_header *fh = fc_frame_header_get(fp);
760 struct fcp_cmnd *fcp;
762 if (fc_fcp_is_read(fr_fsp(fp)) &&
763 (fr_fsp(fp)->data_len > fcoe_ddp_min))
764 return true;
765 else if (!(ntoh24(fh->fh_f_ctl) & FC_FC_EX_CTX)) {
766 fcp = fc_frame_payload_get(fp, sizeof(*fcp));
767 if (ntohs(fh->fh_rx_id) == FC_XID_UNKNOWN &&
768 fcp && (ntohl(fcp->fc_dl) > fcoe_ddp_min) &&
769 (fcp->fc_flags & FCP_CFL_WRDATA))
770 return true;
772 return false;
776 * fcoe_em_config() - Allocate and configure an exchange manager
777 * @lport: The local port that the new EM will be associated with
779 * Returns: 0 on success
781 static inline int fcoe_em_config(struct fc_lport *lport)
783 struct fcoe_port *port = lport_priv(lport);
784 struct fcoe_interface *fcoe = port->priv;
785 struct fcoe_interface *oldfcoe = NULL;
786 struct net_device *old_real_dev, *cur_real_dev;
787 u16 min_xid = FCOE_MIN_XID;
788 u16 max_xid = FCOE_MAX_XID;
791 * Check if need to allocate an em instance for
792 * offload exchange ids to be shared across all VN_PORTs/lport.
794 if (!lport->lro_enabled || !lport->lro_xid ||
795 (lport->lro_xid >= max_xid)) {
796 lport->lro_xid = 0;
797 goto skip_oem;
801 * Reuse existing offload em instance in case
802 * it is already allocated on real eth device
804 if (fcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
805 cur_real_dev = vlan_dev_real_dev(fcoe->netdev);
806 else
807 cur_real_dev = fcoe->netdev;
809 list_for_each_entry(oldfcoe, &fcoe_hostlist, list) {
810 if (oldfcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
811 old_real_dev = vlan_dev_real_dev(oldfcoe->netdev);
812 else
813 old_real_dev = oldfcoe->netdev;
815 if (cur_real_dev == old_real_dev) {
816 fcoe->oem = oldfcoe->oem;
817 break;
821 if (fcoe->oem) {
822 if (!fc_exch_mgr_add(lport, fcoe->oem, fcoe_oem_match)) {
823 printk(KERN_ERR "fcoe_em_config: failed to add "
824 "offload em:%p on interface:%s\n",
825 fcoe->oem, fcoe->netdev->name);
826 return -ENOMEM;
828 } else {
829 fcoe->oem = fc_exch_mgr_alloc(lport, FC_CLASS_3,
830 FCOE_MIN_XID, lport->lro_xid,
831 fcoe_oem_match);
832 if (!fcoe->oem) {
833 printk(KERN_ERR "fcoe_em_config: failed to allocate "
834 "em for offload exches on interface:%s\n",
835 fcoe->netdev->name);
836 return -ENOMEM;
841 * Exclude offload EM xid range from next EM xid range.
843 min_xid += lport->lro_xid + 1;
845 skip_oem:
846 if (!fc_exch_mgr_alloc(lport, FC_CLASS_3, min_xid, max_xid, NULL)) {
847 printk(KERN_ERR "fcoe_em_config: failed to "
848 "allocate em on interface %s\n", fcoe->netdev->name);
849 return -ENOMEM;
852 return 0;
856 * fcoe_if_destroy() - Tear down a SW FCoE instance
857 * @lport: The local port to be destroyed
860 static void fcoe_if_destroy(struct fc_lport *lport)
862 /* Free queued packets for the per-CPU receive threads */
863 fcoe_percpu_clean(lport);
865 /* Detach from the scsi-ml */
866 fc_remove_host(lport->host);
867 scsi_remove_host(lport->host);
869 /* Destroy lport scsi_priv */
870 fc_fcp_destroy(lport);
872 /* There are no more rports or I/O, free the EM */
873 fc_exch_mgr_free(lport);
875 /* Free memory used by statistical counters */
876 fc_lport_free_stats(lport);
878 /* Release the Scsi_Host */
879 scsi_host_put(lport->host);
883 * fcoe_ddp_setup() - Call a LLD's ddp_setup through the net device
884 * @lport: The local port to setup DDP for
885 * @xid: The exchange ID for this DDP transfer
886 * @sgl: The scatterlist describing this transfer
887 * @sgc: The number of sg items
889 * Returns: 0 if the DDP context was not configured
891 static int fcoe_ddp_setup(struct fc_lport *lport, u16 xid,
892 struct scatterlist *sgl, unsigned int sgc)
894 struct net_device *netdev = fcoe_netdev(lport);
896 if (netdev->netdev_ops->ndo_fcoe_ddp_setup)
897 return netdev->netdev_ops->ndo_fcoe_ddp_setup(netdev,
898 xid, sgl,
899 sgc);
901 return 0;
905 * fcoe_ddp_done() - Call a LLD's ddp_done through the net device
906 * @lport: The local port to complete DDP on
907 * @xid: The exchange ID for this DDP transfer
909 * Returns: the length of data that have been completed by DDP
911 static int fcoe_ddp_done(struct fc_lport *lport, u16 xid)
913 struct net_device *netdev = fcoe_netdev(lport);
915 if (netdev->netdev_ops->ndo_fcoe_ddp_done)
916 return netdev->netdev_ops->ndo_fcoe_ddp_done(netdev, xid);
917 return 0;
921 * fcoe_if_create() - Create a FCoE instance on an interface
922 * @fcoe: The FCoE interface to create a local port on
923 * @parent: The device pointer to be the parent in sysfs for the SCSI host
924 * @npiv: Indicates if the port is a vport or not
926 * Creates a fc_lport instance and a Scsi_Host instance and configure them.
928 * Returns: The allocated fc_lport or an error pointer
930 static struct fc_lport *fcoe_if_create(struct fcoe_interface *fcoe,
931 struct device *parent, int npiv)
933 struct net_device *netdev = fcoe->netdev;
934 struct fc_lport *lport, *n_port;
935 struct fcoe_port *port;
936 struct Scsi_Host *shost;
937 int rc;
939 * parent is only a vport if npiv is 1,
940 * but we'll only use vport in that case so go ahead and set it
942 struct fc_vport *vport = dev_to_vport(parent);
944 FCOE_NETDEV_DBG(netdev, "Create Interface\n");
946 if (!npiv)
947 lport = libfc_host_alloc(&fcoe_shost_template, sizeof(*port));
948 else
949 lport = libfc_vport_create(vport, sizeof(*port));
951 if (!lport) {
952 FCOE_NETDEV_DBG(netdev, "Could not allocate host structure\n");
953 rc = -ENOMEM;
954 goto out;
956 port = lport_priv(lport);
957 port->lport = lport;
958 port->priv = fcoe;
959 port->max_queue_depth = FCOE_MAX_QUEUE_DEPTH;
960 port->min_queue_depth = FCOE_MIN_QUEUE_DEPTH;
961 INIT_WORK(&port->destroy_work, fcoe_destroy_work);
963 /* configure a fc_lport including the exchange manager */
964 rc = fcoe_lport_config(lport);
965 if (rc) {
966 FCOE_NETDEV_DBG(netdev, "Could not configure lport for the "
967 "interface\n");
968 goto out_host_put;
971 if (npiv) {
972 FCOE_NETDEV_DBG(netdev, "Setting vport names, "
973 "%16.16llx %16.16llx\n",
974 vport->node_name, vport->port_name);
975 fc_set_wwnn(lport, vport->node_name);
976 fc_set_wwpn(lport, vport->port_name);
979 /* configure lport network properties */
980 rc = fcoe_netdev_config(lport, netdev);
981 if (rc) {
982 FCOE_NETDEV_DBG(netdev, "Could not configure netdev for the "
983 "interface\n");
984 goto out_lp_destroy;
987 /* configure lport scsi host properties */
988 rc = fcoe_shost_config(lport, parent);
989 if (rc) {
990 FCOE_NETDEV_DBG(netdev, "Could not configure shost for the "
991 "interface\n");
992 goto out_lp_destroy;
995 /* Initialize the library */
996 rc = fcoe_libfc_config(lport, &fcoe->ctlr, &fcoe_libfc_fcn_templ, 1);
997 if (rc) {
998 FCOE_NETDEV_DBG(netdev, "Could not configure libfc for the "
999 "interface\n");
1000 goto out_lp_destroy;
1004 * fcoe_em_alloc() and fcoe_hostlist_add() both
1005 * need to be atomic with respect to other changes to the
1006 * hostlist since fcoe_em_alloc() looks for an existing EM
1007 * instance on host list updated by fcoe_hostlist_add().
1009 * This is currently handled through the fcoe_config_mutex
1010 * begin held.
1012 if (!npiv)
1013 /* lport exch manager allocation */
1014 rc = fcoe_em_config(lport);
1015 else {
1016 shost = vport_to_shost(vport);
1017 n_port = shost_priv(shost);
1018 rc = fc_exch_mgr_list_clone(n_port, lport);
1021 if (rc) {
1022 FCOE_NETDEV_DBG(netdev, "Could not configure the EM\n");
1023 goto out_lp_destroy;
1026 fcoe_interface_get(fcoe);
1027 return lport;
1029 out_lp_destroy:
1030 fc_exch_mgr_free(lport);
1031 out_host_put:
1032 scsi_host_put(lport->host);
1033 out:
1034 return ERR_PTR(rc);
1038 * fcoe_if_init() - Initialization routine for fcoe.ko
1040 * Attaches the SW FCoE transport to the FC transport
1042 * Returns: 0 on success
1044 static int __init fcoe_if_init(void)
1046 /* attach to scsi transport */
1047 fcoe_nport_scsi_transport =
1048 fc_attach_transport(&fcoe_nport_fc_functions);
1049 fcoe_vport_scsi_transport =
1050 fc_attach_transport(&fcoe_vport_fc_functions);
1052 if (!fcoe_nport_scsi_transport) {
1053 printk(KERN_ERR "fcoe: Failed to attach to the FC transport\n");
1054 return -ENODEV;
1057 return 0;
1061 * fcoe_if_exit() - Tear down fcoe.ko
1063 * Detaches the SW FCoE transport from the FC transport
1065 * Returns: 0 on success
1067 int __exit fcoe_if_exit(void)
1069 fc_release_transport(fcoe_nport_scsi_transport);
1070 fc_release_transport(fcoe_vport_scsi_transport);
1071 fcoe_nport_scsi_transport = NULL;
1072 fcoe_vport_scsi_transport = NULL;
1073 return 0;
1077 * fcoe_percpu_thread_create() - Create a receive thread for an online CPU
1078 * @cpu: The CPU index of the CPU to create a receive thread for
1080 static void fcoe_percpu_thread_create(unsigned int cpu)
1082 struct fcoe_percpu_s *p;
1083 struct task_struct *thread;
1085 p = &per_cpu(fcoe_percpu, cpu);
1087 thread = kthread_create(fcoe_percpu_receive_thread,
1088 (void *)p, "fcoethread/%d", cpu);
1090 if (likely(!IS_ERR(thread))) {
1091 kthread_bind(thread, cpu);
1092 wake_up_process(thread);
1094 spin_lock_bh(&p->fcoe_rx_list.lock);
1095 p->thread = thread;
1096 spin_unlock_bh(&p->fcoe_rx_list.lock);
1101 * fcoe_percpu_thread_destroy() - Remove the receive thread of a CPU
1102 * @cpu: The CPU index of the CPU whose receive thread is to be destroyed
1104 * Destroys a per-CPU Rx thread. Any pending skbs are moved to the
1105 * current CPU's Rx thread. If the thread being destroyed is bound to
1106 * the CPU processing this context the skbs will be freed.
1108 static void fcoe_percpu_thread_destroy(unsigned int cpu)
1110 struct fcoe_percpu_s *p;
1111 struct task_struct *thread;
1112 struct page *crc_eof;
1113 struct sk_buff *skb;
1114 #ifdef CONFIG_SMP
1115 struct fcoe_percpu_s *p0;
1116 unsigned targ_cpu = get_cpu();
1117 #endif /* CONFIG_SMP */
1119 FCOE_DBG("Destroying receive thread for CPU %d\n", cpu);
1121 /* Prevent any new skbs from being queued for this CPU. */
1122 p = &per_cpu(fcoe_percpu, cpu);
1123 spin_lock_bh(&p->fcoe_rx_list.lock);
1124 thread = p->thread;
1125 p->thread = NULL;
1126 crc_eof = p->crc_eof_page;
1127 p->crc_eof_page = NULL;
1128 p->crc_eof_offset = 0;
1129 spin_unlock_bh(&p->fcoe_rx_list.lock);
1131 #ifdef CONFIG_SMP
1133 * Don't bother moving the skb's if this context is running
1134 * on the same CPU that is having its thread destroyed. This
1135 * can easily happen when the module is removed.
1137 if (cpu != targ_cpu) {
1138 p0 = &per_cpu(fcoe_percpu, targ_cpu);
1139 spin_lock_bh(&p0->fcoe_rx_list.lock);
1140 if (p0->thread) {
1141 FCOE_DBG("Moving frames from CPU %d to CPU %d\n",
1142 cpu, targ_cpu);
1144 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1145 __skb_queue_tail(&p0->fcoe_rx_list, skb);
1146 spin_unlock_bh(&p0->fcoe_rx_list.lock);
1147 } else {
1149 * The targeted CPU is not initialized and cannot accept
1150 * new skbs. Unlock the targeted CPU and drop the skbs
1151 * on the CPU that is going offline.
1153 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1154 kfree_skb(skb);
1155 spin_unlock_bh(&p0->fcoe_rx_list.lock);
1157 } else {
1159 * This scenario occurs when the module is being removed
1160 * and all threads are being destroyed. skbs will continue
1161 * to be shifted from the CPU thread that is being removed
1162 * to the CPU thread associated with the CPU that is processing
1163 * the module removal. Once there is only one CPU Rx thread it
1164 * will reach this case and we will drop all skbs and later
1165 * stop the thread.
1167 spin_lock_bh(&p->fcoe_rx_list.lock);
1168 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1169 kfree_skb(skb);
1170 spin_unlock_bh(&p->fcoe_rx_list.lock);
1172 put_cpu();
1173 #else
1175 * This a non-SMP scenario where the singular Rx thread is
1176 * being removed. Free all skbs and stop the thread.
1178 spin_lock_bh(&p->fcoe_rx_list.lock);
1179 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1180 kfree_skb(skb);
1181 spin_unlock_bh(&p->fcoe_rx_list.lock);
1182 #endif
1184 if (thread)
1185 kthread_stop(thread);
1187 if (crc_eof)
1188 put_page(crc_eof);
1192 * fcoe_cpu_callback() - Handler for CPU hotplug events
1193 * @nfb: The callback data block
1194 * @action: The event triggering the callback
1195 * @hcpu: The index of the CPU that the event is for
1197 * This creates or destroys per-CPU data for fcoe
1199 * Returns NOTIFY_OK always.
1201 static int fcoe_cpu_callback(struct notifier_block *nfb,
1202 unsigned long action, void *hcpu)
1204 unsigned cpu = (unsigned long)hcpu;
1206 switch (action) {
1207 case CPU_ONLINE:
1208 case CPU_ONLINE_FROZEN:
1209 FCOE_DBG("CPU %x online: Create Rx thread\n", cpu);
1210 fcoe_percpu_thread_create(cpu);
1211 break;
1212 case CPU_DEAD:
1213 case CPU_DEAD_FROZEN:
1214 FCOE_DBG("CPU %x offline: Remove Rx thread\n", cpu);
1215 fcoe_percpu_thread_destroy(cpu);
1216 break;
1217 default:
1218 break;
1220 return NOTIFY_OK;
1224 * fcoe_rcv() - Receive packets from a net device
1225 * @skb: The received packet
1226 * @netdev: The net device that the packet was received on
1227 * @ptype: The packet type context
1228 * @olddev: The last device net device
1230 * This routine is called by NET_RX_SOFTIRQ. It receives a packet, builds a
1231 * FC frame and passes the frame to libfc.
1233 * Returns: 0 for success
1235 int fcoe_rcv(struct sk_buff *skb, struct net_device *netdev,
1236 struct packet_type *ptype, struct net_device *olddev)
1238 struct fc_lport *lport;
1239 struct fcoe_rcv_info *fr;
1240 struct fcoe_interface *fcoe;
1241 struct fc_frame_header *fh;
1242 struct fcoe_percpu_s *fps;
1243 struct ethhdr *eh;
1244 unsigned int cpu;
1246 fcoe = container_of(ptype, struct fcoe_interface, fcoe_packet_type);
1247 lport = fcoe->ctlr.lp;
1248 if (unlikely(!lport)) {
1249 FCOE_NETDEV_DBG(netdev, "Cannot find hba structure");
1250 goto err2;
1252 if (!lport->link_up)
1253 goto err2;
1255 FCOE_NETDEV_DBG(netdev, "skb_info: len:%d data_len:%d head:%p "
1256 "data:%p tail:%p end:%p sum:%d dev:%s",
1257 skb->len, skb->data_len, skb->head, skb->data,
1258 skb_tail_pointer(skb), skb_end_pointer(skb),
1259 skb->csum, skb->dev ? skb->dev->name : "<NULL>");
1261 eh = eth_hdr(skb);
1263 if (is_fip_mode(&fcoe->ctlr) &&
1264 compare_ether_addr(eh->h_source, fcoe->ctlr.dest_addr)) {
1265 FCOE_NETDEV_DBG(netdev, "wrong source mac address:%pM\n",
1266 eh->h_source);
1267 goto err;
1271 * Check for minimum frame length, and make sure required FCoE
1272 * and FC headers are pulled into the linear data area.
1274 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
1275 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
1276 goto err;
1278 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
1279 fh = (struct fc_frame_header *) skb_transport_header(skb);
1281 if (ntoh24(&eh->h_dest[3]) != ntoh24(fh->fh_d_id)) {
1282 FCOE_NETDEV_DBG(netdev, "FC frame d_id mismatch with MAC:%pM\n",
1283 eh->h_dest);
1284 goto err;
1287 fr = fcoe_dev_from_skb(skb);
1288 fr->fr_dev = lport;
1289 fr->ptype = ptype;
1292 * In case the incoming frame's exchange is originated from
1293 * the initiator, then received frame's exchange id is ANDed
1294 * with fc_cpu_mask bits to get the same cpu on which exchange
1295 * was originated, otherwise just use the current cpu.
1297 if (ntoh24(fh->fh_f_ctl) & FC_FC_EX_CTX)
1298 cpu = ntohs(fh->fh_ox_id) & fc_cpu_mask;
1299 else
1300 cpu = smp_processor_id();
1302 fps = &per_cpu(fcoe_percpu, cpu);
1303 spin_lock_bh(&fps->fcoe_rx_list.lock);
1304 if (unlikely(!fps->thread)) {
1306 * The targeted CPU is not ready, let's target
1307 * the first CPU now. For non-SMP systems this
1308 * will check the same CPU twice.
1310 FCOE_NETDEV_DBG(netdev, "CPU is online, but no receive thread "
1311 "ready for incoming skb- using first online "
1312 "CPU.\n");
1314 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1315 cpu = cpumask_first(cpu_online_mask);
1316 fps = &per_cpu(fcoe_percpu, cpu);
1317 spin_lock_bh(&fps->fcoe_rx_list.lock);
1318 if (!fps->thread) {
1319 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1320 goto err;
1325 * We now have a valid CPU that we're targeting for
1326 * this skb. We also have this receive thread locked,
1327 * so we're free to queue skbs into it's queue.
1330 /* If this is a SCSI-FCP frame, and this is already executing on the
1331 * correct CPU, and the queue for this CPU is empty, then go ahead
1332 * and process the frame directly in the softirq context.
1333 * This lets us process completions without context switching from the
1334 * NET_RX softirq, to our receive processing thread, and then back to
1335 * BLOCK softirq context.
1337 if (fh->fh_type == FC_TYPE_FCP &&
1338 cpu == smp_processor_id() &&
1339 skb_queue_empty(&fps->fcoe_rx_list)) {
1340 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1341 fcoe_recv_frame(skb);
1342 } else {
1343 __skb_queue_tail(&fps->fcoe_rx_list, skb);
1344 if (fps->fcoe_rx_list.qlen == 1)
1345 wake_up_process(fps->thread);
1346 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1349 return 0;
1350 err:
1351 per_cpu_ptr(lport->dev_stats, get_cpu())->ErrorFrames++;
1352 put_cpu();
1353 err2:
1354 kfree_skb(skb);
1355 return -1;
1359 * fcoe_alloc_paged_crc_eof() - Allocate a page to be used for the trailer CRC
1360 * @skb: The packet to be transmitted
1361 * @tlen: The total length of the trailer
1363 * Returns: 0 for success
1365 static int fcoe_alloc_paged_crc_eof(struct sk_buff *skb, int tlen)
1367 struct fcoe_percpu_s *fps;
1368 int rc;
1370 fps = &get_cpu_var(fcoe_percpu);
1371 rc = fcoe_get_paged_crc_eof(skb, tlen, fps);
1372 put_cpu_var(fcoe_percpu);
1374 return rc;
1378 * fcoe_xmit() - Transmit a FCoE frame
1379 * @lport: The local port that the frame is to be transmitted for
1380 * @fp: The frame to be transmitted
1382 * Return: 0 for success
1384 int fcoe_xmit(struct fc_lport *lport, struct fc_frame *fp)
1386 int wlen;
1387 u32 crc;
1388 struct ethhdr *eh;
1389 struct fcoe_crc_eof *cp;
1390 struct sk_buff *skb;
1391 struct fcoe_dev_stats *stats;
1392 struct fc_frame_header *fh;
1393 unsigned int hlen; /* header length implies the version */
1394 unsigned int tlen; /* trailer length */
1395 unsigned int elen; /* eth header, may include vlan */
1396 struct fcoe_port *port = lport_priv(lport);
1397 struct fcoe_interface *fcoe = port->priv;
1398 u8 sof, eof;
1399 struct fcoe_hdr *hp;
1401 WARN_ON((fr_len(fp) % sizeof(u32)) != 0);
1403 fh = fc_frame_header_get(fp);
1404 skb = fp_skb(fp);
1405 wlen = skb->len / FCOE_WORD_TO_BYTE;
1407 if (!lport->link_up) {
1408 kfree_skb(skb);
1409 return 0;
1412 if (unlikely(fh->fh_type == FC_TYPE_ELS) &&
1413 fcoe_ctlr_els_send(&fcoe->ctlr, lport, skb))
1414 return 0;
1416 sof = fr_sof(fp);
1417 eof = fr_eof(fp);
1419 elen = sizeof(struct ethhdr);
1420 hlen = sizeof(struct fcoe_hdr);
1421 tlen = sizeof(struct fcoe_crc_eof);
1422 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
1424 /* crc offload */
1425 if (likely(lport->crc_offload)) {
1426 skb->ip_summed = CHECKSUM_PARTIAL;
1427 skb->csum_start = skb_headroom(skb);
1428 skb->csum_offset = skb->len;
1429 crc = 0;
1430 } else {
1431 skb->ip_summed = CHECKSUM_NONE;
1432 crc = fcoe_fc_crc(fp);
1435 /* copy port crc and eof to the skb buff */
1436 if (skb_is_nonlinear(skb)) {
1437 skb_frag_t *frag;
1438 if (fcoe_alloc_paged_crc_eof(skb, tlen)) {
1439 kfree_skb(skb);
1440 return -ENOMEM;
1442 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
1443 cp = kmap_atomic(frag->page, KM_SKB_DATA_SOFTIRQ)
1444 + frag->page_offset;
1445 } else {
1446 cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
1449 memset(cp, 0, sizeof(*cp));
1450 cp->fcoe_eof = eof;
1451 cp->fcoe_crc32 = cpu_to_le32(~crc);
1453 if (skb_is_nonlinear(skb)) {
1454 kunmap_atomic(cp, KM_SKB_DATA_SOFTIRQ);
1455 cp = NULL;
1458 /* adjust skb network/transport offsets to match mac/fcoe/port */
1459 skb_push(skb, elen + hlen);
1460 skb_reset_mac_header(skb);
1461 skb_reset_network_header(skb);
1462 skb->mac_len = elen;
1463 skb->protocol = htons(ETH_P_FCOE);
1464 skb->dev = fcoe->netdev;
1466 /* fill up mac and fcoe headers */
1467 eh = eth_hdr(skb);
1468 eh->h_proto = htons(ETH_P_FCOE);
1469 memcpy(eh->h_dest, fcoe->ctlr.dest_addr, ETH_ALEN);
1470 if (fcoe->ctlr.map_dest)
1471 memcpy(eh->h_dest + 3, fh->fh_d_id, 3);
1473 if (unlikely(fcoe->ctlr.flogi_oxid != FC_XID_UNKNOWN))
1474 memcpy(eh->h_source, fcoe->ctlr.ctl_src_addr, ETH_ALEN);
1475 else
1476 memcpy(eh->h_source, port->data_src_addr, ETH_ALEN);
1478 hp = (struct fcoe_hdr *)(eh + 1);
1479 memset(hp, 0, sizeof(*hp));
1480 if (FC_FCOE_VER)
1481 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
1482 hp->fcoe_sof = sof;
1484 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
1485 if (lport->seq_offload && fr_max_payload(fp)) {
1486 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
1487 skb_shinfo(skb)->gso_size = fr_max_payload(fp);
1488 } else {
1489 skb_shinfo(skb)->gso_type = 0;
1490 skb_shinfo(skb)->gso_size = 0;
1492 /* update tx stats: regardless if LLD fails */
1493 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
1494 stats->TxFrames++;
1495 stats->TxWords += wlen;
1496 put_cpu();
1498 /* send down to lld */
1499 fr_dev(fp) = lport;
1500 if (port->fcoe_pending_queue.qlen)
1501 fcoe_check_wait_queue(lport, skb);
1502 else if (fcoe_start_io(skb))
1503 fcoe_check_wait_queue(lport, skb);
1505 return 0;
1509 * fcoe_percpu_flush_done() - Indicate per-CPU queue flush completion
1510 * @skb: The completed skb (argument required by destructor)
1512 static void fcoe_percpu_flush_done(struct sk_buff *skb)
1514 complete(&fcoe_flush_completion);
1518 * fcoe_filter_frames() - filter out bad fcoe frames, i.e. bad CRC
1519 * @lport: The local port the frame was received on
1520 * @fp: The received frame
1522 * Return: 0 on passing filtering checks
1524 static inline int fcoe_filter_frames(struct fc_lport *lport,
1525 struct fc_frame *fp)
1527 struct fcoe_interface *fcoe;
1528 struct fc_frame_header *fh;
1529 struct sk_buff *skb = (struct sk_buff *)fp;
1530 struct fcoe_dev_stats *stats;
1533 * We only check CRC if no offload is available and if it is
1534 * it's solicited data, in which case, the FCP layer would
1535 * check it during the copy.
1537 if (lport->crc_offload && skb->ip_summed == CHECKSUM_UNNECESSARY)
1538 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1539 else
1540 fr_flags(fp) |= FCPHF_CRC_UNCHECKED;
1542 fh = (struct fc_frame_header *) skb_transport_header(skb);
1543 fh = fc_frame_header_get(fp);
1544 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA && fh->fh_type == FC_TYPE_FCP)
1545 return 0;
1547 fcoe = ((struct fcoe_port *)lport_priv(lport))->priv;
1548 if (is_fip_mode(&fcoe->ctlr) && fc_frame_payload_op(fp) == ELS_LOGO &&
1549 ntoh24(fh->fh_s_id) == FC_FID_FLOGI) {
1550 FCOE_DBG("fcoe: dropping FCoE lport LOGO in fip mode\n");
1551 return -EINVAL;
1554 if (!(fr_flags(fp) & FCPHF_CRC_UNCHECKED) ||
1555 le32_to_cpu(fr_crc(fp)) == ~crc32(~0, skb->data, skb->len)) {
1556 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1557 return 0;
1560 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
1561 stats->InvalidCRCCount++;
1562 if (stats->InvalidCRCCount < 5)
1563 printk(KERN_WARNING "fcoe: dropping frame with CRC error\n");
1564 put_cpu();
1565 return -EINVAL;
1569 * fcoe_recv_frame() - process a single received frame
1570 * @skb: frame to process
1572 static void fcoe_recv_frame(struct sk_buff *skb)
1574 u32 fr_len;
1575 struct fc_lport *lport;
1576 struct fcoe_rcv_info *fr;
1577 struct fcoe_dev_stats *stats;
1578 struct fcoe_crc_eof crc_eof;
1579 struct fc_frame *fp;
1580 struct fcoe_port *port;
1581 struct fcoe_hdr *hp;
1583 fr = fcoe_dev_from_skb(skb);
1584 lport = fr->fr_dev;
1585 if (unlikely(!lport)) {
1586 if (skb->destructor != fcoe_percpu_flush_done)
1587 FCOE_NETDEV_DBG(skb->dev, "NULL lport in skb");
1588 kfree_skb(skb);
1589 return;
1592 FCOE_NETDEV_DBG(skb->dev, "skb_info: len:%d data_len:%d "
1593 "head:%p data:%p tail:%p end:%p sum:%d dev:%s",
1594 skb->len, skb->data_len,
1595 skb->head, skb->data, skb_tail_pointer(skb),
1596 skb_end_pointer(skb), skb->csum,
1597 skb->dev ? skb->dev->name : "<NULL>");
1599 port = lport_priv(lport);
1600 if (skb_is_nonlinear(skb))
1601 skb_linearize(skb); /* not ideal */
1604 * Frame length checks and setting up the header pointers
1605 * was done in fcoe_rcv already.
1607 hp = (struct fcoe_hdr *) skb_network_header(skb);
1609 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
1610 if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) {
1611 if (stats->ErrorFrames < 5)
1612 printk(KERN_WARNING "fcoe: FCoE version "
1613 "mismatch: The frame has "
1614 "version %x, but the "
1615 "initiator supports version "
1616 "%x\n", FC_FCOE_DECAPS_VER(hp),
1617 FC_FCOE_VER);
1618 goto drop;
1621 skb_pull(skb, sizeof(struct fcoe_hdr));
1622 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
1624 stats->RxFrames++;
1625 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
1627 fp = (struct fc_frame *)skb;
1628 fc_frame_init(fp);
1629 fr_dev(fp) = lport;
1630 fr_sof(fp) = hp->fcoe_sof;
1632 /* Copy out the CRC and EOF trailer for access */
1633 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof)))
1634 goto drop;
1635 fr_eof(fp) = crc_eof.fcoe_eof;
1636 fr_crc(fp) = crc_eof.fcoe_crc32;
1637 if (pskb_trim(skb, fr_len))
1638 goto drop;
1640 if (!fcoe_filter_frames(lport, fp)) {
1641 put_cpu();
1642 fc_exch_recv(lport, fp);
1643 return;
1645 drop:
1646 stats->ErrorFrames++;
1647 put_cpu();
1648 kfree_skb(skb);
1652 * fcoe_percpu_receive_thread() - The per-CPU packet receive thread
1653 * @arg: The per-CPU context
1655 * Return: 0 for success
1657 int fcoe_percpu_receive_thread(void *arg)
1659 struct fcoe_percpu_s *p = arg;
1660 struct sk_buff *skb;
1662 set_user_nice(current, -20);
1664 while (!kthread_should_stop()) {
1666 spin_lock_bh(&p->fcoe_rx_list.lock);
1667 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) == NULL) {
1668 set_current_state(TASK_INTERRUPTIBLE);
1669 spin_unlock_bh(&p->fcoe_rx_list.lock);
1670 schedule();
1671 set_current_state(TASK_RUNNING);
1672 if (kthread_should_stop())
1673 return 0;
1674 spin_lock_bh(&p->fcoe_rx_list.lock);
1676 spin_unlock_bh(&p->fcoe_rx_list.lock);
1677 fcoe_recv_frame(skb);
1679 return 0;
1683 * fcoe_dev_setup() - Setup the link change notification interface
1685 static void fcoe_dev_setup(void)
1687 register_netdevice_notifier(&fcoe_notifier);
1691 * fcoe_dev_cleanup() - Cleanup the link change notification interface
1693 static void fcoe_dev_cleanup(void)
1695 unregister_netdevice_notifier(&fcoe_notifier);
1699 * fcoe_device_notification() - Handler for net device events
1700 * @notifier: The context of the notification
1701 * @event: The type of event
1702 * @ptr: The net device that the event was on
1704 * This function is called by the Ethernet driver in case of link change event.
1706 * Returns: 0 for success
1708 static int fcoe_device_notification(struct notifier_block *notifier,
1709 ulong event, void *ptr)
1711 struct fc_lport *lport = NULL;
1712 struct net_device *netdev = ptr;
1713 struct fcoe_interface *fcoe;
1714 struct fcoe_port *port;
1715 struct fcoe_dev_stats *stats;
1716 u32 link_possible = 1;
1717 u32 mfs;
1718 int rc = NOTIFY_OK;
1720 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
1721 if (fcoe->netdev == netdev) {
1722 lport = fcoe->ctlr.lp;
1723 break;
1726 if (!lport) {
1727 rc = NOTIFY_DONE;
1728 goto out;
1731 switch (event) {
1732 case NETDEV_DOWN:
1733 case NETDEV_GOING_DOWN:
1734 link_possible = 0;
1735 break;
1736 case NETDEV_UP:
1737 case NETDEV_CHANGE:
1738 break;
1739 case NETDEV_CHANGEMTU:
1740 if (netdev->features & NETIF_F_FCOE_MTU)
1741 break;
1742 mfs = netdev->mtu - (sizeof(struct fcoe_hdr) +
1743 sizeof(struct fcoe_crc_eof));
1744 if (mfs >= FC_MIN_MAX_FRAME)
1745 fc_set_mfs(lport, mfs);
1746 break;
1747 case NETDEV_REGISTER:
1748 break;
1749 case NETDEV_UNREGISTER:
1750 list_del(&fcoe->list);
1751 port = lport_priv(fcoe->ctlr.lp);
1752 fcoe_interface_cleanup(fcoe);
1753 queue_work(fcoe_wq, &port->destroy_work);
1754 goto out;
1755 break;
1756 case NETDEV_FEAT_CHANGE:
1757 fcoe_netdev_features_change(lport, netdev);
1758 break;
1759 default:
1760 FCOE_NETDEV_DBG(netdev, "Unknown event %ld "
1761 "from netdev netlink\n", event);
1764 fcoe_link_speed_update(lport);
1766 if (link_possible && !fcoe_link_ok(lport))
1767 fcoe_ctlr_link_up(&fcoe->ctlr);
1768 else if (fcoe_ctlr_link_down(&fcoe->ctlr)) {
1769 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
1770 stats->LinkFailureCount++;
1771 put_cpu();
1772 fcoe_clean_pending_queue(lport);
1774 out:
1775 return rc;
1779 * fcoe_disable() - Disables a FCoE interface
1780 * @netdev : The net_device object the Ethernet interface to create on
1782 * Called from fcoe transport.
1784 * Returns: 0 for success
1786 static int fcoe_disable(struct net_device *netdev)
1788 struct fcoe_interface *fcoe;
1789 int rc = 0;
1791 mutex_lock(&fcoe_config_mutex);
1793 rtnl_lock();
1794 fcoe = fcoe_hostlist_lookup_port(netdev);
1795 rtnl_unlock();
1797 if (fcoe) {
1798 fcoe_ctlr_link_down(&fcoe->ctlr);
1799 fcoe_clean_pending_queue(fcoe->ctlr.lp);
1800 } else
1801 rc = -ENODEV;
1803 mutex_unlock(&fcoe_config_mutex);
1804 return rc;
1808 * fcoe_enable() - Enables a FCoE interface
1809 * @netdev : The net_device object the Ethernet interface to create on
1811 * Called from fcoe transport.
1813 * Returns: 0 for success
1815 static int fcoe_enable(struct net_device *netdev)
1817 struct fcoe_interface *fcoe;
1818 int rc = 0;
1820 mutex_lock(&fcoe_config_mutex);
1821 rtnl_lock();
1822 fcoe = fcoe_hostlist_lookup_port(netdev);
1823 rtnl_unlock();
1825 if (!fcoe)
1826 rc = -ENODEV;
1827 else if (!fcoe_link_ok(fcoe->ctlr.lp))
1828 fcoe_ctlr_link_up(&fcoe->ctlr);
1830 mutex_unlock(&fcoe_config_mutex);
1831 return rc;
1835 * fcoe_destroy() - Destroy a FCoE interface
1836 * @netdev : The net_device object the Ethernet interface to create on
1838 * Called from fcoe transport
1840 * Returns: 0 for success
1842 static int fcoe_destroy(struct net_device *netdev)
1844 struct fcoe_interface *fcoe;
1845 struct fc_lport *lport;
1846 int rc = 0;
1848 mutex_lock(&fcoe_config_mutex);
1849 rtnl_lock();
1850 fcoe = fcoe_hostlist_lookup_port(netdev);
1851 if (!fcoe) {
1852 rtnl_unlock();
1853 rc = -ENODEV;
1854 goto out_nodev;
1856 lport = fcoe->ctlr.lp;
1857 list_del(&fcoe->list);
1858 fcoe_interface_cleanup(fcoe);
1859 rtnl_unlock();
1860 fcoe_if_destroy(lport);
1861 out_nodev:
1862 mutex_unlock(&fcoe_config_mutex);
1863 return rc;
1867 * fcoe_destroy_work() - Destroy a FCoE port in a deferred work context
1868 * @work: Handle to the FCoE port to be destroyed
1870 static void fcoe_destroy_work(struct work_struct *work)
1872 struct fcoe_port *port;
1874 port = container_of(work, struct fcoe_port, destroy_work);
1875 mutex_lock(&fcoe_config_mutex);
1876 fcoe_if_destroy(port->lport);
1877 mutex_unlock(&fcoe_config_mutex);
1881 * fcoe_match() - Check if the FCoE is supported on the given netdevice
1882 * @netdev : The net_device object the Ethernet interface to create on
1884 * Called from fcoe transport.
1886 * Returns: always returns true as this is the default FCoE transport,
1887 * i.e., support all netdevs.
1889 static bool fcoe_match(struct net_device *netdev)
1891 return true;
1895 * fcoe_create() - Create a fcoe interface
1896 * @netdev : The net_device object the Ethernet interface to create on
1897 * @fip_mode: The FIP mode for this creation
1899 * Called from fcoe transport
1901 * Returns: 0 for success
1903 static int fcoe_create(struct net_device *netdev, enum fip_state fip_mode)
1905 int rc;
1906 struct fcoe_interface *fcoe;
1907 struct fc_lport *lport;
1909 mutex_lock(&fcoe_config_mutex);
1910 rtnl_lock();
1912 /* look for existing lport */
1913 if (fcoe_hostlist_lookup(netdev)) {
1914 rc = -EEXIST;
1915 goto out_nodev;
1918 fcoe = fcoe_interface_create(netdev, fip_mode);
1919 if (IS_ERR(fcoe)) {
1920 rc = PTR_ERR(fcoe);
1921 goto out_nodev;
1924 lport = fcoe_if_create(fcoe, &netdev->dev, 0);
1925 if (IS_ERR(lport)) {
1926 printk(KERN_ERR "fcoe: Failed to create interface (%s)\n",
1927 netdev->name);
1928 rc = -EIO;
1929 fcoe_interface_cleanup(fcoe);
1930 goto out_free;
1933 /* Make this the "master" N_Port */
1934 fcoe->ctlr.lp = lport;
1936 /* add to lports list */
1937 fcoe_hostlist_add(lport);
1939 /* start FIP Discovery and FLOGI */
1940 lport->boot_time = jiffies;
1941 fc_fabric_login(lport);
1942 if (!fcoe_link_ok(lport))
1943 fcoe_ctlr_link_up(&fcoe->ctlr);
1946 * Release from init in fcoe_interface_create(), on success lport
1947 * should be holding a reference taken in fcoe_if_create().
1949 fcoe_interface_put(fcoe);
1950 rtnl_unlock();
1951 mutex_unlock(&fcoe_config_mutex);
1953 return 0;
1954 out_free:
1955 fcoe_interface_put(fcoe);
1956 out_nodev:
1957 rtnl_unlock();
1958 mutex_unlock(&fcoe_config_mutex);
1959 return rc;
1963 * fcoe_link_speed_update() - Update the supported and actual link speeds
1964 * @lport: The local port to update speeds for
1966 * Returns: 0 if the ethtool query was successful
1967 * -1 if the ethtool query failed
1969 int fcoe_link_speed_update(struct fc_lport *lport)
1971 struct net_device *netdev = fcoe_netdev(lport);
1972 struct ethtool_cmd ecmd;
1974 if (!dev_ethtool_get_settings(netdev, &ecmd)) {
1975 lport->link_supported_speeds &=
1976 ~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
1977 if (ecmd.supported & (SUPPORTED_1000baseT_Half |
1978 SUPPORTED_1000baseT_Full))
1979 lport->link_supported_speeds |= FC_PORTSPEED_1GBIT;
1980 if (ecmd.supported & SUPPORTED_10000baseT_Full)
1981 lport->link_supported_speeds |=
1982 FC_PORTSPEED_10GBIT;
1983 switch (ethtool_cmd_speed(&ecmd)) {
1984 case SPEED_1000:
1985 lport->link_speed = FC_PORTSPEED_1GBIT;
1986 break;
1987 case SPEED_10000:
1988 lport->link_speed = FC_PORTSPEED_10GBIT;
1989 break;
1991 return 0;
1993 return -1;
1997 * fcoe_link_ok() - Check if the link is OK for a local port
1998 * @lport: The local port to check link on
2000 * Returns: 0 if link is UP and OK, -1 if not
2003 int fcoe_link_ok(struct fc_lport *lport)
2005 struct net_device *netdev = fcoe_netdev(lport);
2007 if (netif_oper_up(netdev))
2008 return 0;
2009 return -1;
2013 * fcoe_percpu_clean() - Clear all pending skbs for an local port
2014 * @lport: The local port whose skbs are to be cleared
2016 * Must be called with fcoe_create_mutex held to single-thread completion.
2018 * This flushes the pending skbs by adding a new skb to each queue and
2019 * waiting until they are all freed. This assures us that not only are
2020 * there no packets that will be handled by the lport, but also that any
2021 * threads already handling packet have returned.
2023 void fcoe_percpu_clean(struct fc_lport *lport)
2025 struct fcoe_percpu_s *pp;
2026 struct fcoe_rcv_info *fr;
2027 struct sk_buff_head *list;
2028 struct sk_buff *skb, *next;
2029 struct sk_buff *head;
2030 unsigned int cpu;
2032 for_each_possible_cpu(cpu) {
2033 pp = &per_cpu(fcoe_percpu, cpu);
2034 spin_lock_bh(&pp->fcoe_rx_list.lock);
2035 list = &pp->fcoe_rx_list;
2036 head = list->next;
2037 for (skb = head; skb != (struct sk_buff *)list;
2038 skb = next) {
2039 next = skb->next;
2040 fr = fcoe_dev_from_skb(skb);
2041 if (fr->fr_dev == lport) {
2042 __skb_unlink(skb, list);
2043 kfree_skb(skb);
2047 if (!pp->thread || !cpu_online(cpu)) {
2048 spin_unlock_bh(&pp->fcoe_rx_list.lock);
2049 continue;
2052 skb = dev_alloc_skb(0);
2053 if (!skb) {
2054 spin_unlock_bh(&pp->fcoe_rx_list.lock);
2055 continue;
2057 skb->destructor = fcoe_percpu_flush_done;
2059 __skb_queue_tail(&pp->fcoe_rx_list, skb);
2060 if (pp->fcoe_rx_list.qlen == 1)
2061 wake_up_process(pp->thread);
2062 spin_unlock_bh(&pp->fcoe_rx_list.lock);
2064 wait_for_completion(&fcoe_flush_completion);
2069 * fcoe_reset() - Reset a local port
2070 * @shost: The SCSI host associated with the local port to be reset
2072 * Returns: Always 0 (return value required by FC transport template)
2074 int fcoe_reset(struct Scsi_Host *shost)
2076 struct fc_lport *lport = shost_priv(shost);
2077 struct fcoe_port *port = lport_priv(lport);
2078 struct fcoe_interface *fcoe = port->priv;
2080 fcoe_ctlr_link_down(&fcoe->ctlr);
2081 fcoe_clean_pending_queue(fcoe->ctlr.lp);
2082 if (!fcoe_link_ok(fcoe->ctlr.lp))
2083 fcoe_ctlr_link_up(&fcoe->ctlr);
2084 return 0;
2088 * fcoe_hostlist_lookup_port() - Find the FCoE interface associated with a net device
2089 * @netdev: The net device used as a key
2091 * Locking: Must be called with the RNL mutex held.
2093 * Returns: NULL or the FCoE interface
2095 static struct fcoe_interface *
2096 fcoe_hostlist_lookup_port(const struct net_device *netdev)
2098 struct fcoe_interface *fcoe;
2100 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
2101 if (fcoe->netdev == netdev)
2102 return fcoe;
2104 return NULL;
2108 * fcoe_hostlist_lookup() - Find the local port associated with a
2109 * given net device
2110 * @netdev: The netdevice used as a key
2112 * Locking: Must be called with the RTNL mutex held
2114 * Returns: NULL or the local port
2116 static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *netdev)
2118 struct fcoe_interface *fcoe;
2120 fcoe = fcoe_hostlist_lookup_port(netdev);
2121 return (fcoe) ? fcoe->ctlr.lp : NULL;
2125 * fcoe_hostlist_add() - Add the FCoE interface identified by a local
2126 * port to the hostlist
2127 * @lport: The local port that identifies the FCoE interface to be added
2129 * Locking: must be called with the RTNL mutex held
2131 * Returns: 0 for success
2133 static int fcoe_hostlist_add(const struct fc_lport *lport)
2135 struct fcoe_interface *fcoe;
2136 struct fcoe_port *port;
2138 fcoe = fcoe_hostlist_lookup_port(fcoe_netdev(lport));
2139 if (!fcoe) {
2140 port = lport_priv(lport);
2141 fcoe = port->priv;
2142 list_add_tail(&fcoe->list, &fcoe_hostlist);
2144 return 0;
2148 static struct fcoe_transport fcoe_sw_transport = {
2149 .name = {FCOE_TRANSPORT_DEFAULT},
2150 .attached = false,
2151 .list = LIST_HEAD_INIT(fcoe_sw_transport.list),
2152 .match = fcoe_match,
2153 .create = fcoe_create,
2154 .destroy = fcoe_destroy,
2155 .enable = fcoe_enable,
2156 .disable = fcoe_disable,
2160 * fcoe_init() - Initialize fcoe.ko
2162 * Returns: 0 on success, or a negative value on failure
2164 static int __init fcoe_init(void)
2166 struct fcoe_percpu_s *p;
2167 unsigned int cpu;
2168 int rc = 0;
2170 fcoe_wq = alloc_workqueue("fcoe", 0, 0);
2171 if (!fcoe_wq)
2172 return -ENOMEM;
2174 /* register as a fcoe transport */
2175 rc = fcoe_transport_attach(&fcoe_sw_transport);
2176 if (rc) {
2177 printk(KERN_ERR "failed to register an fcoe transport, check "
2178 "if libfcoe is loaded\n");
2179 return rc;
2182 mutex_lock(&fcoe_config_mutex);
2184 for_each_possible_cpu(cpu) {
2185 p = &per_cpu(fcoe_percpu, cpu);
2186 skb_queue_head_init(&p->fcoe_rx_list);
2189 for_each_online_cpu(cpu)
2190 fcoe_percpu_thread_create(cpu);
2192 /* Initialize per CPU interrupt thread */
2193 rc = register_hotcpu_notifier(&fcoe_cpu_notifier);
2194 if (rc)
2195 goto out_free;
2197 /* Setup link change notification */
2198 fcoe_dev_setup();
2200 rc = fcoe_if_init();
2201 if (rc)
2202 goto out_free;
2204 mutex_unlock(&fcoe_config_mutex);
2205 return 0;
2207 out_free:
2208 for_each_online_cpu(cpu) {
2209 fcoe_percpu_thread_destroy(cpu);
2211 mutex_unlock(&fcoe_config_mutex);
2212 destroy_workqueue(fcoe_wq);
2213 return rc;
2215 module_init(fcoe_init);
2218 * fcoe_exit() - Clean up fcoe.ko
2220 * Returns: 0 on success or a negative value on failure
2222 static void __exit fcoe_exit(void)
2224 struct fcoe_interface *fcoe, *tmp;
2225 struct fcoe_port *port;
2226 unsigned int cpu;
2228 mutex_lock(&fcoe_config_mutex);
2230 fcoe_dev_cleanup();
2232 /* releases the associated fcoe hosts */
2233 rtnl_lock();
2234 list_for_each_entry_safe(fcoe, tmp, &fcoe_hostlist, list) {
2235 list_del(&fcoe->list);
2236 port = lport_priv(fcoe->ctlr.lp);
2237 fcoe_interface_cleanup(fcoe);
2238 queue_work(fcoe_wq, &port->destroy_work);
2240 rtnl_unlock();
2242 unregister_hotcpu_notifier(&fcoe_cpu_notifier);
2244 for_each_online_cpu(cpu)
2245 fcoe_percpu_thread_destroy(cpu);
2247 mutex_unlock(&fcoe_config_mutex);
2250 * destroy_work's may be chained but destroy_workqueue()
2251 * can take care of them. Just kill the fcoe_wq.
2253 destroy_workqueue(fcoe_wq);
2256 * Detaching from the scsi transport must happen after all
2257 * destroys are done on the fcoe_wq. destroy_workqueue will
2258 * enusre the fcoe_wq is flushed.
2260 fcoe_if_exit();
2262 /* detach from fcoe transport */
2263 fcoe_transport_detach(&fcoe_sw_transport);
2265 module_exit(fcoe_exit);
2268 * fcoe_flogi_resp() - FCoE specific FLOGI and FDISC response handler
2269 * @seq: active sequence in the FLOGI or FDISC exchange
2270 * @fp: response frame, or error encoded in a pointer (timeout)
2271 * @arg: pointer the the fcoe_ctlr structure
2273 * This handles MAC address management for FCoE, then passes control on to
2274 * the libfc FLOGI response handler.
2276 static void fcoe_flogi_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
2278 struct fcoe_ctlr *fip = arg;
2279 struct fc_exch *exch = fc_seq_exch(seq);
2280 struct fc_lport *lport = exch->lp;
2281 u8 *mac;
2283 if (IS_ERR(fp))
2284 goto done;
2286 mac = fr_cb(fp)->granted_mac;
2287 if (is_zero_ether_addr(mac)) {
2288 /* pre-FIP */
2289 if (fcoe_ctlr_recv_flogi(fip, lport, fp)) {
2290 fc_frame_free(fp);
2291 return;
2294 fcoe_update_src_mac(lport, mac);
2295 done:
2296 fc_lport_flogi_resp(seq, fp, lport);
2300 * fcoe_logo_resp() - FCoE specific LOGO response handler
2301 * @seq: active sequence in the LOGO exchange
2302 * @fp: response frame, or error encoded in a pointer (timeout)
2303 * @arg: pointer the the fcoe_ctlr structure
2305 * This handles MAC address management for FCoE, then passes control on to
2306 * the libfc LOGO response handler.
2308 static void fcoe_logo_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
2310 struct fc_lport *lport = arg;
2311 static u8 zero_mac[ETH_ALEN] = { 0 };
2313 if (!IS_ERR(fp))
2314 fcoe_update_src_mac(lport, zero_mac);
2315 fc_lport_logo_resp(seq, fp, lport);
2319 * fcoe_elsct_send - FCoE specific ELS handler
2321 * This does special case handling of FIP encapsualted ELS exchanges for FCoE,
2322 * using FCoE specific response handlers and passing the FIP controller as
2323 * the argument (the lport is still available from the exchange).
2325 * Most of the work here is just handed off to the libfc routine.
2327 static struct fc_seq *fcoe_elsct_send(struct fc_lport *lport, u32 did,
2328 struct fc_frame *fp, unsigned int op,
2329 void (*resp)(struct fc_seq *,
2330 struct fc_frame *,
2331 void *),
2332 void *arg, u32 timeout)
2334 struct fcoe_port *port = lport_priv(lport);
2335 struct fcoe_interface *fcoe = port->priv;
2336 struct fcoe_ctlr *fip = &fcoe->ctlr;
2337 struct fc_frame_header *fh = fc_frame_header_get(fp);
2339 switch (op) {
2340 case ELS_FLOGI:
2341 case ELS_FDISC:
2342 if (lport->point_to_multipoint)
2343 break;
2344 return fc_elsct_send(lport, did, fp, op, fcoe_flogi_resp,
2345 fip, timeout);
2346 case ELS_LOGO:
2347 /* only hook onto fabric logouts, not port logouts */
2348 if (ntoh24(fh->fh_d_id) != FC_FID_FLOGI)
2349 break;
2350 return fc_elsct_send(lport, did, fp, op, fcoe_logo_resp,
2351 lport, timeout);
2353 return fc_elsct_send(lport, did, fp, op, resp, arg, timeout);
2357 * fcoe_vport_create() - create an fc_host/scsi_host for a vport
2358 * @vport: fc_vport object to create a new fc_host for
2359 * @disabled: start the new fc_host in a disabled state by default?
2361 * Returns: 0 for success
2363 static int fcoe_vport_create(struct fc_vport *vport, bool disabled)
2365 struct Scsi_Host *shost = vport_to_shost(vport);
2366 struct fc_lport *n_port = shost_priv(shost);
2367 struct fcoe_port *port = lport_priv(n_port);
2368 struct fcoe_interface *fcoe = port->priv;
2369 struct net_device *netdev = fcoe->netdev;
2370 struct fc_lport *vn_port;
2371 int rc;
2372 char buf[32];
2374 rc = fcoe_validate_vport_create(vport);
2375 if (rc) {
2376 wwn_to_str(vport->port_name, buf, sizeof(buf));
2377 printk(KERN_ERR "fcoe: Failed to create vport, "
2378 "WWPN (0x%s) already exists\n",
2379 buf);
2380 return rc;
2383 mutex_lock(&fcoe_config_mutex);
2384 vn_port = fcoe_if_create(fcoe, &vport->dev, 1);
2385 mutex_unlock(&fcoe_config_mutex);
2387 if (IS_ERR(vn_port)) {
2388 printk(KERN_ERR "fcoe: fcoe_vport_create(%s) failed\n",
2389 netdev->name);
2390 return -EIO;
2393 if (disabled) {
2394 fc_vport_set_state(vport, FC_VPORT_DISABLED);
2395 } else {
2396 vn_port->boot_time = jiffies;
2397 fc_fabric_login(vn_port);
2398 fc_vport_setlink(vn_port);
2400 return 0;
2404 * fcoe_vport_destroy() - destroy the fc_host/scsi_host for a vport
2405 * @vport: fc_vport object that is being destroyed
2407 * Returns: 0 for success
2409 static int fcoe_vport_destroy(struct fc_vport *vport)
2411 struct Scsi_Host *shost = vport_to_shost(vport);
2412 struct fc_lport *n_port = shost_priv(shost);
2413 struct fc_lport *vn_port = vport->dd_data;
2414 struct fcoe_port *port = lport_priv(vn_port);
2416 mutex_lock(&n_port->lp_mutex);
2417 list_del(&vn_port->list);
2418 mutex_unlock(&n_port->lp_mutex);
2419 queue_work(fcoe_wq, &port->destroy_work);
2420 return 0;
2424 * fcoe_vport_disable() - change vport state
2425 * @vport: vport to bring online/offline
2426 * @disable: should the vport be disabled?
2428 static int fcoe_vport_disable(struct fc_vport *vport, bool disable)
2430 struct fc_lport *lport = vport->dd_data;
2432 if (disable) {
2433 fc_vport_set_state(vport, FC_VPORT_DISABLED);
2434 fc_fabric_logoff(lport);
2435 } else {
2436 lport->boot_time = jiffies;
2437 fc_fabric_login(lport);
2438 fc_vport_setlink(lport);
2441 return 0;
2445 * fcoe_vport_set_symbolic_name() - append vport string to symbolic name
2446 * @vport: fc_vport with a new symbolic name string
2448 * After generating a new symbolic name string, a new RSPN_ID request is
2449 * sent to the name server. There is no response handler, so if it fails
2450 * for some reason it will not be retried.
2452 static void fcoe_set_vport_symbolic_name(struct fc_vport *vport)
2454 struct fc_lport *lport = vport->dd_data;
2455 struct fc_frame *fp;
2456 size_t len;
2458 snprintf(fc_host_symbolic_name(lport->host), FC_SYMBOLIC_NAME_SIZE,
2459 "%s v%s over %s : %s", FCOE_NAME, FCOE_VERSION,
2460 fcoe_netdev(lport)->name, vport->symbolic_name);
2462 if (lport->state != LPORT_ST_READY)
2463 return;
2465 len = strnlen(fc_host_symbolic_name(lport->host), 255);
2466 fp = fc_frame_alloc(lport,
2467 sizeof(struct fc_ct_hdr) +
2468 sizeof(struct fc_ns_rspn) + len);
2469 if (!fp)
2470 return;
2471 lport->tt.elsct_send(lport, FC_FID_DIR_SERV, fp, FC_NS_RSPN_ID,
2472 NULL, NULL, 3 * lport->r_a_tov);
2476 * fcoe_get_lesb() - Fill the FCoE Link Error Status Block
2477 * @lport: the local port
2478 * @fc_lesb: the link error status block
2480 static void fcoe_get_lesb(struct fc_lport *lport,
2481 struct fc_els_lesb *fc_lesb)
2483 unsigned int cpu;
2484 u32 lfc, vlfc, mdac;
2485 struct fcoe_dev_stats *devst;
2486 struct fcoe_fc_els_lesb *lesb;
2487 struct rtnl_link_stats64 temp;
2488 struct net_device *netdev = fcoe_netdev(lport);
2490 lfc = 0;
2491 vlfc = 0;
2492 mdac = 0;
2493 lesb = (struct fcoe_fc_els_lesb *)fc_lesb;
2494 memset(lesb, 0, sizeof(*lesb));
2495 for_each_possible_cpu(cpu) {
2496 devst = per_cpu_ptr(lport->dev_stats, cpu);
2497 lfc += devst->LinkFailureCount;
2498 vlfc += devst->VLinkFailureCount;
2499 mdac += devst->MissDiscAdvCount;
2501 lesb->lesb_link_fail = htonl(lfc);
2502 lesb->lesb_vlink_fail = htonl(vlfc);
2503 lesb->lesb_miss_fka = htonl(mdac);
2504 lesb->lesb_fcs_error = htonl(dev_get_stats(netdev, &temp)->rx_crc_errors);
2508 * fcoe_set_port_id() - Callback from libfc when Port_ID is set.
2509 * @lport: the local port
2510 * @port_id: the port ID
2511 * @fp: the received frame, if any, that caused the port_id to be set.
2513 * This routine handles the case where we received a FLOGI and are
2514 * entering point-to-point mode. We need to call fcoe_ctlr_recv_flogi()
2515 * so it can set the non-mapped mode and gateway address.
2517 * The FLOGI LS_ACC is handled by fcoe_flogi_resp().
2519 static void fcoe_set_port_id(struct fc_lport *lport,
2520 u32 port_id, struct fc_frame *fp)
2522 struct fcoe_port *port = lport_priv(lport);
2523 struct fcoe_interface *fcoe = port->priv;
2525 if (fp && fc_frame_payload_op(fp) == ELS_FLOGI)
2526 fcoe_ctlr_recv_flogi(&fcoe->ctlr, lport, fp);
2530 * fcoe_validate_vport_create() - Validate a vport before creating it
2531 * @vport: NPIV port to be created
2533 * This routine is meant to add validation for a vport before creating it
2534 * via fcoe_vport_create().
2535 * Current validations are:
2536 * - WWPN supplied is unique for given lport
2540 static int fcoe_validate_vport_create(struct fc_vport *vport)
2542 struct Scsi_Host *shost = vport_to_shost(vport);
2543 struct fc_lport *n_port = shost_priv(shost);
2544 struct fc_lport *vn_port;
2545 int rc = 0;
2546 char buf[32];
2548 mutex_lock(&n_port->lp_mutex);
2550 wwn_to_str(vport->port_name, buf, sizeof(buf));
2551 /* Check if the wwpn is not same as that of the lport */
2552 if (!memcmp(&n_port->wwpn, &vport->port_name, sizeof(u64))) {
2553 FCOE_DBG("vport WWPN 0x%s is same as that of the "
2554 "base port WWPN\n", buf);
2555 rc = -EINVAL;
2556 goto out;
2559 /* Check if there is any existing vport with same wwpn */
2560 list_for_each_entry(vn_port, &n_port->vports, list) {
2561 if (!memcmp(&vn_port->wwpn, &vport->port_name, sizeof(u64))) {
2562 FCOE_DBG("vport with given WWPN 0x%s already "
2563 "exists\n", buf);
2564 rc = -EINVAL;
2565 break;
2569 out:
2570 mutex_unlock(&n_port->lp_mutex);
2572 return rc;