thinkpad-acpi: explain errors from acpi_install_notify_handler
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / scsi / fcoe / fcoe.c
blob70ab5d0c5a484ac2e9359e11b7d21935149ead1c
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/cpu.h>
30 #include <linux/fs.h>
31 #include <linux/sysfs.h>
32 #include <linux/ctype.h>
33 #include <scsi/scsi_tcq.h>
34 #include <scsi/scsicam.h>
35 #include <scsi/scsi_transport.h>
36 #include <scsi/scsi_transport_fc.h>
37 #include <net/rtnetlink.h>
39 #include <scsi/fc/fc_encaps.h>
40 #include <scsi/fc/fc_fip.h>
42 #include <scsi/libfc.h>
43 #include <scsi/fc_frame.h>
44 #include <scsi/libfcoe.h>
46 #include "fcoe.h"
48 MODULE_AUTHOR("Open-FCoE.org");
49 MODULE_DESCRIPTION("FCoE");
50 MODULE_LICENSE("GPL v2");
52 /* Performance tuning parameters for fcoe */
53 static unsigned int fcoe_ddp_min;
54 module_param_named(ddp_min, fcoe_ddp_min, uint, S_IRUGO | S_IWUSR);
55 MODULE_PARM_DESC(ddp_min, "Minimum I/O size in bytes for " \
56 "Direct Data Placement (DDP).");
58 DEFINE_MUTEX(fcoe_config_mutex);
60 /* fcoe_percpu_clean completion. Waiter protected by fcoe_create_mutex */
61 static DECLARE_COMPLETION(fcoe_flush_completion);
63 /* fcoe host list */
64 /* must only by accessed under the RTNL mutex */
65 LIST_HEAD(fcoe_hostlist);
66 DEFINE_PER_CPU(struct fcoe_percpu_s, fcoe_percpu);
68 /* Function Prototypes */
69 static int fcoe_reset(struct Scsi_Host *shost);
70 static int fcoe_xmit(struct fc_lport *, struct fc_frame *);
71 static int fcoe_rcv(struct sk_buff *, struct net_device *,
72 struct packet_type *, struct net_device *);
73 static int fcoe_percpu_receive_thread(void *arg);
74 static void fcoe_clean_pending_queue(struct fc_lport *lp);
75 static void fcoe_percpu_clean(struct fc_lport *lp);
76 static int fcoe_link_ok(struct fc_lport *lp);
78 static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *);
79 static int fcoe_hostlist_add(const struct fc_lport *);
81 static void fcoe_check_wait_queue(struct fc_lport *, struct sk_buff *);
82 static int fcoe_device_notification(struct notifier_block *, ulong, void *);
83 static void fcoe_dev_setup(void);
84 static void fcoe_dev_cleanup(void);
85 static struct fcoe_interface *
86 fcoe_hostlist_lookup_port(const struct net_device *dev);
88 /* notification function from net device */
89 static struct notifier_block fcoe_notifier = {
90 .notifier_call = fcoe_device_notification,
93 static struct scsi_transport_template *scsi_transport_fcoe_sw;
95 struct fc_function_template fcoe_transport_function = {
96 .show_host_node_name = 1,
97 .show_host_port_name = 1,
98 .show_host_supported_classes = 1,
99 .show_host_supported_fc4s = 1,
100 .show_host_active_fc4s = 1,
101 .show_host_maxframe_size = 1,
103 .show_host_port_id = 1,
104 .show_host_supported_speeds = 1,
105 .get_host_speed = fc_get_host_speed,
106 .show_host_speed = 1,
107 .show_host_port_type = 1,
108 .get_host_port_state = fc_get_host_port_state,
109 .show_host_port_state = 1,
110 .show_host_symbolic_name = 1,
112 .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
113 .show_rport_maxframe_size = 1,
114 .show_rport_supported_classes = 1,
116 .show_host_fabric_name = 1,
117 .show_starget_node_name = 1,
118 .show_starget_port_name = 1,
119 .show_starget_port_id = 1,
120 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
121 .show_rport_dev_loss_tmo = 1,
122 .get_fc_host_stats = fc_get_host_stats,
123 .issue_fc_host_lip = fcoe_reset,
125 .terminate_rport_io = fc_rport_terminate_io,
128 static struct scsi_host_template fcoe_shost_template = {
129 .module = THIS_MODULE,
130 .name = "FCoE Driver",
131 .proc_name = FCOE_NAME,
132 .queuecommand = fc_queuecommand,
133 .eh_abort_handler = fc_eh_abort,
134 .eh_device_reset_handler = fc_eh_device_reset,
135 .eh_host_reset_handler = fc_eh_host_reset,
136 .slave_alloc = fc_slave_alloc,
137 .change_queue_depth = fc_change_queue_depth,
138 .change_queue_type = fc_change_queue_type,
139 .this_id = -1,
140 .cmd_per_lun = 3,
141 .can_queue = FCOE_MAX_OUTSTANDING_COMMANDS,
142 .use_clustering = ENABLE_CLUSTERING,
143 .sg_tablesize = SG_ALL,
144 .max_sectors = 0xffff,
147 static int fcoe_fip_recv(struct sk_buff *skb, struct net_device *dev,
148 struct packet_type *ptype,
149 struct net_device *orig_dev);
151 * fcoe_interface_setup()
152 * @fcoe: new fcoe_interface
153 * @netdev : ptr to the associated netdevice struct
155 * Returns : 0 for success
156 * Locking: must be called with the RTNL mutex held
158 static int fcoe_interface_setup(struct fcoe_interface *fcoe,
159 struct net_device *netdev)
161 struct fcoe_ctlr *fip = &fcoe->ctlr;
162 struct netdev_hw_addr *ha;
163 struct net_device *real_dev;
164 u8 flogi_maddr[ETH_ALEN];
166 fcoe->netdev = netdev;
168 /* Do not support for bonding device */
169 if ((netdev->priv_flags & IFF_MASTER_ALB) ||
170 (netdev->priv_flags & IFF_SLAVE_INACTIVE) ||
171 (netdev->priv_flags & IFF_MASTER_8023AD)) {
172 return -EOPNOTSUPP;
175 /* look for SAN MAC address, if multiple SAN MACs exist, only
176 * use the first one for SPMA */
177 real_dev = (netdev->priv_flags & IFF_802_1Q_VLAN) ?
178 vlan_dev_real_dev(netdev) : netdev;
179 rcu_read_lock();
180 for_each_dev_addr(real_dev, ha) {
181 if ((ha->type == NETDEV_HW_ADDR_T_SAN) &&
182 (is_valid_ether_addr(ha->addr))) {
183 memcpy(fip->ctl_src_addr, ha->addr, ETH_ALEN);
184 fip->spma = 1;
185 break;
188 rcu_read_unlock();
190 /* setup Source Mac Address */
191 if (!fip->spma)
192 memcpy(fip->ctl_src_addr, netdev->dev_addr, netdev->addr_len);
195 * Add FCoE MAC address as second unicast MAC address
196 * or enter promiscuous mode if not capable of listening
197 * for multiple unicast MACs.
199 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
200 dev_unicast_add(netdev, flogi_maddr);
201 if (fip->spma)
202 dev_unicast_add(netdev, fip->ctl_src_addr);
203 dev_mc_add(netdev, FIP_ALL_ENODE_MACS, ETH_ALEN, 0);
206 * setup the receive function from ethernet driver
207 * on the ethertype for the given device
209 fcoe->fcoe_packet_type.func = fcoe_rcv;
210 fcoe->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
211 fcoe->fcoe_packet_type.dev = netdev;
212 dev_add_pack(&fcoe->fcoe_packet_type);
214 fcoe->fip_packet_type.func = fcoe_fip_recv;
215 fcoe->fip_packet_type.type = htons(ETH_P_FIP);
216 fcoe->fip_packet_type.dev = netdev;
217 dev_add_pack(&fcoe->fip_packet_type);
219 return 0;
222 static void fcoe_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb);
223 static void fcoe_update_src_mac(struct fcoe_ctlr *fip, u8 *old, u8 *new);
224 static void fcoe_destroy_work(struct work_struct *work);
227 * fcoe_interface_create()
228 * @netdev: network interface
230 * Returns: pointer to a struct fcoe_interface or NULL on error
232 static struct fcoe_interface *fcoe_interface_create(struct net_device *netdev)
234 struct fcoe_interface *fcoe;
236 fcoe = kzalloc(sizeof(*fcoe), GFP_KERNEL);
237 if (!fcoe) {
238 FCOE_NETDEV_DBG(netdev, "Could not allocate fcoe structure\n");
239 return NULL;
242 dev_hold(netdev);
243 kref_init(&fcoe->kref);
246 * Initialize FIP.
248 fcoe_ctlr_init(&fcoe->ctlr);
249 fcoe->ctlr.send = fcoe_fip_send;
250 fcoe->ctlr.update_mac = fcoe_update_src_mac;
252 fcoe_interface_setup(fcoe, netdev);
254 return fcoe;
258 * fcoe_interface_cleanup() - clean up netdev configurations
259 * @fcoe:
261 * Caller must be holding the RTNL mutex
263 void fcoe_interface_cleanup(struct fcoe_interface *fcoe)
265 struct net_device *netdev = fcoe->netdev;
266 struct fcoe_ctlr *fip = &fcoe->ctlr;
267 u8 flogi_maddr[ETH_ALEN];
270 * Don't listen for Ethernet packets anymore.
271 * synchronize_net() ensures that the packet handlers are not running
272 * on another CPU. dev_remove_pack() would do that, this calls the
273 * unsyncronized version __dev_remove_pack() to avoid multiple delays.
275 __dev_remove_pack(&fcoe->fcoe_packet_type);
276 __dev_remove_pack(&fcoe->fip_packet_type);
277 synchronize_net();
279 /* Delete secondary MAC addresses */
280 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
281 dev_unicast_delete(netdev, flogi_maddr);
282 if (!is_zero_ether_addr(fip->data_src_addr))
283 dev_unicast_delete(netdev, fip->data_src_addr);
284 if (fip->spma)
285 dev_unicast_delete(netdev, fip->ctl_src_addr);
286 dev_mc_delete(netdev, FIP_ALL_ENODE_MACS, ETH_ALEN, 0);
290 * fcoe_interface_release() - fcoe_port kref release function
291 * @kref: embedded reference count in an fcoe_interface struct
293 static void fcoe_interface_release(struct kref *kref)
295 struct fcoe_interface *fcoe;
296 struct net_device *netdev;
298 fcoe = container_of(kref, struct fcoe_interface, kref);
299 netdev = fcoe->netdev;
300 /* tear-down the FCoE controller */
301 fcoe_ctlr_destroy(&fcoe->ctlr);
302 kfree(fcoe);
303 dev_put(netdev);
307 * fcoe_interface_get()
308 * @fcoe:
310 static inline void fcoe_interface_get(struct fcoe_interface *fcoe)
312 kref_get(&fcoe->kref);
316 * fcoe_interface_put()
317 * @fcoe:
319 static inline void fcoe_interface_put(struct fcoe_interface *fcoe)
321 kref_put(&fcoe->kref, fcoe_interface_release);
325 * fcoe_fip_recv - handle a received FIP frame.
326 * @skb: the receive skb
327 * @dev: associated &net_device
328 * @ptype: the &packet_type structure which was used to register this handler.
329 * @orig_dev: original receive &net_device, in case @dev is a bond.
331 * Returns: 0 for success
333 static int fcoe_fip_recv(struct sk_buff *skb, struct net_device *dev,
334 struct packet_type *ptype,
335 struct net_device *orig_dev)
337 struct fcoe_interface *fcoe;
339 fcoe = container_of(ptype, struct fcoe_interface, fip_packet_type);
340 fcoe_ctlr_recv(&fcoe->ctlr, skb);
341 return 0;
345 * fcoe_fip_send() - send an Ethernet-encapsulated FIP frame.
346 * @fip: FCoE controller.
347 * @skb: FIP Packet.
349 static void fcoe_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
351 skb->dev = fcoe_from_ctlr(fip)->netdev;
352 dev_queue_xmit(skb);
356 * fcoe_update_src_mac() - Update Ethernet MAC filters.
357 * @fip: FCoE controller.
358 * @old: Unicast MAC address to delete if the MAC is non-zero.
359 * @new: Unicast MAC address to add.
361 * Remove any previously-set unicast MAC filter.
362 * Add secondary FCoE MAC address filter for our OUI.
364 static void fcoe_update_src_mac(struct fcoe_ctlr *fip, u8 *old, u8 *new)
366 struct fcoe_interface *fcoe;
368 fcoe = fcoe_from_ctlr(fip);
369 rtnl_lock();
370 if (!is_zero_ether_addr(old))
371 dev_unicast_delete(fcoe->netdev, old);
372 dev_unicast_add(fcoe->netdev, new);
373 rtnl_unlock();
377 * fcoe_lport_config() - sets up the fc_lport
378 * @lp: ptr to the fc_lport
380 * Returns: 0 for success
382 static int fcoe_lport_config(struct fc_lport *lp)
384 lp->link_up = 0;
385 lp->qfull = 0;
386 lp->max_retry_count = 3;
387 lp->max_rport_retry_count = 3;
388 lp->e_d_tov = 2 * 1000; /* FC-FS default */
389 lp->r_a_tov = 2 * 2 * 1000;
390 lp->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
391 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
393 fc_lport_init_stats(lp);
395 /* lport fc_lport related configuration */
396 fc_lport_config(lp);
398 /* offload related configuration */
399 lp->crc_offload = 0;
400 lp->seq_offload = 0;
401 lp->lro_enabled = 0;
402 lp->lro_xid = 0;
403 lp->lso_max = 0;
405 return 0;
409 * fcoe_queue_timer() - fcoe queue timer
410 * @lp: the fc_lport pointer
412 * Calls fcoe_check_wait_queue on timeout
415 static void fcoe_queue_timer(ulong lp)
417 fcoe_check_wait_queue((struct fc_lport *)lp, NULL);
421 * fcoe_netdev_config() - Set up netdev for SW FCoE
422 * @lp : ptr to the fc_lport
423 * @netdev : ptr to the associated netdevice struct
425 * Must be called after fcoe_lport_config() as it will use lport mutex
427 * Returns : 0 for success
429 static int fcoe_netdev_config(struct fc_lport *lp, struct net_device *netdev)
431 u32 mfs;
432 u64 wwnn, wwpn;
433 struct fcoe_interface *fcoe;
434 struct fcoe_port *port;
436 /* Setup lport private data to point to fcoe softc */
437 port = lport_priv(lp);
438 fcoe = port->fcoe;
441 * Determine max frame size based on underlying device and optional
442 * user-configured limit. If the MFS is too low, fcoe_link_ok()
443 * will return 0, so do this first.
445 mfs = netdev->mtu - (sizeof(struct fcoe_hdr) +
446 sizeof(struct fcoe_crc_eof));
447 if (fc_set_mfs(lp, mfs))
448 return -EINVAL;
450 /* offload features support */
451 if (netdev->features & NETIF_F_SG)
452 lp->sg_supp = 1;
454 if (netdev->features & NETIF_F_FCOE_CRC) {
455 lp->crc_offload = 1;
456 FCOE_NETDEV_DBG(netdev, "Supports FCCRC offload\n");
458 if (netdev->features & NETIF_F_FSO) {
459 lp->seq_offload = 1;
460 lp->lso_max = netdev->gso_max_size;
461 FCOE_NETDEV_DBG(netdev, "Supports LSO for max len 0x%x\n",
462 lp->lso_max);
464 if (netdev->fcoe_ddp_xid) {
465 lp->lro_enabled = 1;
466 lp->lro_xid = netdev->fcoe_ddp_xid;
467 FCOE_NETDEV_DBG(netdev, "Supports LRO for max xid 0x%x\n",
468 lp->lro_xid);
470 skb_queue_head_init(&port->fcoe_pending_queue);
471 port->fcoe_pending_queue_active = 0;
472 setup_timer(&port->timer, fcoe_queue_timer, (unsigned long)lp);
474 wwnn = fcoe_wwn_from_mac(netdev->dev_addr, 1, 0);
475 fc_set_wwnn(lp, wwnn);
476 /* XXX - 3rd arg needs to be vlan id */
477 wwpn = fcoe_wwn_from_mac(netdev->dev_addr, 2, 0);
478 fc_set_wwpn(lp, wwpn);
480 return 0;
484 * fcoe_shost_config() - Sets up fc_lport->host
485 * @lp : ptr to the fc_lport
486 * @shost : ptr to the associated scsi host
487 * @dev : device associated to scsi host
489 * Must be called after fcoe_lport_config() and fcoe_netdev_config()
491 * Returns : 0 for success
493 static int fcoe_shost_config(struct fc_lport *lp, struct Scsi_Host *shost,
494 struct device *dev)
496 int rc = 0;
498 /* lport scsi host config */
499 lp->host = shost;
501 lp->host->max_lun = FCOE_MAX_LUN;
502 lp->host->max_id = FCOE_MAX_FCP_TARGET;
503 lp->host->max_channel = 0;
504 lp->host->transportt = scsi_transport_fcoe_sw;
506 /* add the new host to the SCSI-ml */
507 rc = scsi_add_host(lp->host, dev);
508 if (rc) {
509 FCOE_NETDEV_DBG(fcoe_netdev(lp), "fcoe_shost_config: "
510 "error on scsi_add_host\n");
511 return rc;
513 sprintf(fc_host_symbolic_name(lp->host), "%s v%s over %s",
514 FCOE_NAME, FCOE_VERSION,
515 fcoe_netdev(lp)->name);
517 return 0;
521 * fcoe_oem_match() - match for read types IO
522 * @fp: the fc_frame for new IO.
524 * Returns : true for read types IO, otherwise returns false.
526 bool fcoe_oem_match(struct fc_frame *fp)
528 return fc_fcp_is_read(fr_fsp(fp)) &&
529 (fr_fsp(fp)->data_len > fcoe_ddp_min);
533 * fcoe_em_config() - allocates em for this lport
534 * @lp: the fcoe that em is to allocated for
536 * Returns : 0 on success
538 static inline int fcoe_em_config(struct fc_lport *lp)
540 struct fcoe_port *port = lport_priv(lp);
541 struct fcoe_interface *fcoe = port->fcoe;
542 struct fcoe_interface *oldfcoe = NULL;
543 struct net_device *old_real_dev, *cur_real_dev;
544 u16 min_xid = FCOE_MIN_XID;
545 u16 max_xid = FCOE_MAX_XID;
548 * Check if need to allocate an em instance for
549 * offload exchange ids to be shared across all VN_PORTs/lport.
551 if (!lp->lro_enabled || !lp->lro_xid || (lp->lro_xid >= max_xid)) {
552 lp->lro_xid = 0;
553 goto skip_oem;
557 * Reuse existing offload em instance in case
558 * it is already allocated on real eth device
560 if (fcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
561 cur_real_dev = vlan_dev_real_dev(fcoe->netdev);
562 else
563 cur_real_dev = fcoe->netdev;
565 list_for_each_entry(oldfcoe, &fcoe_hostlist, list) {
566 if (oldfcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
567 old_real_dev = vlan_dev_real_dev(oldfcoe->netdev);
568 else
569 old_real_dev = oldfcoe->netdev;
571 if (cur_real_dev == old_real_dev) {
572 fcoe->oem = oldfcoe->oem;
573 break;
577 if (fcoe->oem) {
578 if (!fc_exch_mgr_add(lp, fcoe->oem, fcoe_oem_match)) {
579 printk(KERN_ERR "fcoe_em_config: failed to add "
580 "offload em:%p on interface:%s\n",
581 fcoe->oem, fcoe->netdev->name);
582 return -ENOMEM;
584 } else {
585 fcoe->oem = fc_exch_mgr_alloc(lp, FC_CLASS_3,
586 FCOE_MIN_XID, lp->lro_xid,
587 fcoe_oem_match);
588 if (!fcoe->oem) {
589 printk(KERN_ERR "fcoe_em_config: failed to allocate "
590 "em for offload exches on interface:%s\n",
591 fcoe->netdev->name);
592 return -ENOMEM;
597 * Exclude offload EM xid range from next EM xid range.
599 min_xid += lp->lro_xid + 1;
601 skip_oem:
602 if (!fc_exch_mgr_alloc(lp, FC_CLASS_3, min_xid, max_xid, NULL)) {
603 printk(KERN_ERR "fcoe_em_config: failed to "
604 "allocate em on interface %s\n", fcoe->netdev->name);
605 return -ENOMEM;
608 return 0;
612 * fcoe_if_destroy() - FCoE software HBA tear-down function
613 * @lport: fc_lport to destroy
615 static void fcoe_if_destroy(struct fc_lport *lport)
617 struct fcoe_port *port = lport_priv(lport);
618 struct fcoe_interface *fcoe = port->fcoe;
619 struct net_device *netdev = fcoe->netdev;
621 FCOE_NETDEV_DBG(netdev, "Destroying interface\n");
623 /* Logout of the fabric */
624 fc_fabric_logoff(lport);
626 /* Cleanup the fc_lport */
627 fc_lport_destroy(lport);
628 fc_fcp_destroy(lport);
630 /* Stop the transmit retry timer */
631 del_timer_sync(&port->timer);
633 /* Free existing transmit skbs */
634 fcoe_clean_pending_queue(lport);
636 /* receives may not be stopped until after this */
637 fcoe_interface_put(fcoe);
639 /* Free queued packets for the per-CPU receive threads */
640 fcoe_percpu_clean(lport);
642 /* Detach from the scsi-ml */
643 fc_remove_host(lport->host);
644 scsi_remove_host(lport->host);
646 /* There are no more rports or I/O, free the EM */
647 fc_exch_mgr_free(lport);
649 /* Free memory used by statistical counters */
650 fc_lport_free_stats(lport);
652 /* Release the Scsi_Host */
653 scsi_host_put(lport->host);
657 * fcoe_ddp_setup - calls LLD's ddp_setup through net_device
658 * @lp: the corresponding fc_lport
659 * @xid: the exchange id for this ddp transfer
660 * @sgl: the scatterlist describing this transfer
661 * @sgc: number of sg items
663 * Returns : 0 no ddp
665 static int fcoe_ddp_setup(struct fc_lport *lp, u16 xid,
666 struct scatterlist *sgl, unsigned int sgc)
668 struct net_device *n = fcoe_netdev(lp);
670 if (n->netdev_ops->ndo_fcoe_ddp_setup)
671 return n->netdev_ops->ndo_fcoe_ddp_setup(n, xid, sgl, sgc);
673 return 0;
677 * fcoe_ddp_done - calls LLD's ddp_done through net_device
678 * @lp: the corresponding fc_lport
679 * @xid: the exchange id for this ddp transfer
681 * Returns : the length of data that have been completed by ddp
683 static int fcoe_ddp_done(struct fc_lport *lp, u16 xid)
685 struct net_device *n = fcoe_netdev(lp);
687 if (n->netdev_ops->ndo_fcoe_ddp_done)
688 return n->netdev_ops->ndo_fcoe_ddp_done(n, xid);
689 return 0;
692 static struct libfc_function_template fcoe_libfc_fcn_templ = {
693 .frame_send = fcoe_xmit,
694 .ddp_setup = fcoe_ddp_setup,
695 .ddp_done = fcoe_ddp_done,
699 * fcoe_if_create() - this function creates the fcoe port
700 * @fcoe: fcoe_interface structure to create an fc_lport instance on
701 * @parent: device pointer to be the parent in sysfs for the SCSI host
703 * Creates fc_lport struct and scsi_host for lport, configures lport.
705 * Returns : The allocated fc_lport or an error pointer
707 static struct fc_lport *fcoe_if_create(struct fcoe_interface *fcoe,
708 struct device *parent)
710 int rc;
711 struct fc_lport *lport = NULL;
712 struct fcoe_port *port;
713 struct Scsi_Host *shost;
714 struct net_device *netdev = fcoe->netdev;
716 FCOE_NETDEV_DBG(netdev, "Create Interface\n");
718 shost = libfc_host_alloc(&fcoe_shost_template,
719 sizeof(struct fcoe_port));
720 if (!shost) {
721 FCOE_NETDEV_DBG(netdev, "Could not allocate host structure\n");
722 rc = -ENOMEM;
723 goto out;
725 lport = shost_priv(shost);
726 port = lport_priv(lport);
727 port->lport = lport;
728 port->fcoe = fcoe;
729 INIT_WORK(&port->destroy_work, fcoe_destroy_work);
731 /* configure fc_lport, e.g., em */
732 rc = fcoe_lport_config(lport);
733 if (rc) {
734 FCOE_NETDEV_DBG(netdev, "Could not configure lport for the "
735 "interface\n");
736 goto out_host_put;
739 /* configure lport network properties */
740 rc = fcoe_netdev_config(lport, netdev);
741 if (rc) {
742 FCOE_NETDEV_DBG(netdev, "Could not configure netdev for the "
743 "interface\n");
744 goto out_lp_destroy;
747 /* configure lport scsi host properties */
748 rc = fcoe_shost_config(lport, shost, parent);
749 if (rc) {
750 FCOE_NETDEV_DBG(netdev, "Could not configure shost for the "
751 "interface\n");
752 goto out_lp_destroy;
755 /* Initialize the library */
756 rc = fcoe_libfc_config(lport, &fcoe_libfc_fcn_templ);
757 if (rc) {
758 FCOE_NETDEV_DBG(netdev, "Could not configure libfc for the "
759 "interface\n");
760 goto out_lp_destroy;
764 * fcoe_em_alloc() and fcoe_hostlist_add() both
765 * need to be atomic with respect to other changes to the hostlist
766 * since fcoe_em_alloc() looks for an existing EM
767 * instance on host list updated by fcoe_hostlist_add().
769 * This is currently handled through the fcoe_config_mutex begin held.
772 /* lport exch manager allocation */
773 rc = fcoe_em_config(lport);
774 if (rc) {
775 FCOE_NETDEV_DBG(netdev, "Could not configure the EM for the "
776 "interface\n");
777 goto out_lp_destroy;
780 fcoe_interface_get(fcoe);
781 return lport;
783 out_lp_destroy:
784 fc_exch_mgr_free(lport);
785 out_host_put:
786 scsi_host_put(lport->host);
787 out:
788 return ERR_PTR(rc);
792 * fcoe_if_init() - attach to scsi transport
794 * Returns : 0 on success
796 static int __init fcoe_if_init(void)
798 /* attach to scsi transport */
799 scsi_transport_fcoe_sw =
800 fc_attach_transport(&fcoe_transport_function);
802 if (!scsi_transport_fcoe_sw) {
803 printk(KERN_ERR "fcoe: Failed to attach to the FC transport\n");
804 return -ENODEV;
807 return 0;
811 * fcoe_if_exit() - detach from scsi transport
813 * Returns : 0 on success
815 int __exit fcoe_if_exit(void)
817 fc_release_transport(scsi_transport_fcoe_sw);
818 scsi_transport_fcoe_sw = NULL;
819 return 0;
823 * fcoe_percpu_thread_create() - Create a receive thread for an online cpu
824 * @cpu: cpu index for the online cpu
826 static void fcoe_percpu_thread_create(unsigned int cpu)
828 struct fcoe_percpu_s *p;
829 struct task_struct *thread;
831 p = &per_cpu(fcoe_percpu, cpu);
833 thread = kthread_create(fcoe_percpu_receive_thread,
834 (void *)p, "fcoethread/%d", cpu);
836 if (likely(!IS_ERR(thread))) {
837 kthread_bind(thread, cpu);
838 wake_up_process(thread);
840 spin_lock_bh(&p->fcoe_rx_list.lock);
841 p->thread = thread;
842 spin_unlock_bh(&p->fcoe_rx_list.lock);
847 * fcoe_percpu_thread_destroy() - removes the rx thread for the given cpu
848 * @cpu: cpu index the rx thread is to be removed
850 * Destroys a per-CPU Rx thread. Any pending skbs are moved to the
851 * current CPU's Rx thread. If the thread being destroyed is bound to
852 * the CPU processing this context the skbs will be freed.
854 static void fcoe_percpu_thread_destroy(unsigned int cpu)
856 struct fcoe_percpu_s *p;
857 struct task_struct *thread;
858 struct page *crc_eof;
859 struct sk_buff *skb;
860 #ifdef CONFIG_SMP
861 struct fcoe_percpu_s *p0;
862 unsigned targ_cpu = smp_processor_id();
863 #endif /* CONFIG_SMP */
865 FCOE_DBG("Destroying receive thread for CPU %d\n", cpu);
867 /* Prevent any new skbs from being queued for this CPU. */
868 p = &per_cpu(fcoe_percpu, cpu);
869 spin_lock_bh(&p->fcoe_rx_list.lock);
870 thread = p->thread;
871 p->thread = NULL;
872 crc_eof = p->crc_eof_page;
873 p->crc_eof_page = NULL;
874 p->crc_eof_offset = 0;
875 spin_unlock_bh(&p->fcoe_rx_list.lock);
877 #ifdef CONFIG_SMP
879 * Don't bother moving the skb's if this context is running
880 * on the same CPU that is having its thread destroyed. This
881 * can easily happen when the module is removed.
883 if (cpu != targ_cpu) {
884 p0 = &per_cpu(fcoe_percpu, targ_cpu);
885 spin_lock_bh(&p0->fcoe_rx_list.lock);
886 if (p0->thread) {
887 FCOE_DBG("Moving frames from CPU %d to CPU %d\n",
888 cpu, targ_cpu);
890 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
891 __skb_queue_tail(&p0->fcoe_rx_list, skb);
892 spin_unlock_bh(&p0->fcoe_rx_list.lock);
893 } else {
895 * The targeted CPU is not initialized and cannot accept
896 * new skbs. Unlock the targeted CPU and drop the skbs
897 * on the CPU that is going offline.
899 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
900 kfree_skb(skb);
901 spin_unlock_bh(&p0->fcoe_rx_list.lock);
903 } else {
905 * This scenario occurs when the module is being removed
906 * and all threads are being destroyed. skbs will continue
907 * to be shifted from the CPU thread that is being removed
908 * to the CPU thread associated with the CPU that is processing
909 * the module removal. Once there is only one CPU Rx thread it
910 * will reach this case and we will drop all skbs and later
911 * stop the thread.
913 spin_lock_bh(&p->fcoe_rx_list.lock);
914 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
915 kfree_skb(skb);
916 spin_unlock_bh(&p->fcoe_rx_list.lock);
918 #else
920 * This a non-SMP scenario where the singular Rx thread is
921 * being removed. Free all skbs and stop the thread.
923 spin_lock_bh(&p->fcoe_rx_list.lock);
924 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
925 kfree_skb(skb);
926 spin_unlock_bh(&p->fcoe_rx_list.lock);
927 #endif
929 if (thread)
930 kthread_stop(thread);
932 if (crc_eof)
933 put_page(crc_eof);
937 * fcoe_cpu_callback() - fcoe cpu hotplug event callback
938 * @nfb: callback data block
939 * @action: event triggering the callback
940 * @hcpu: index for the cpu of this event
942 * This creates or destroys per cpu data for fcoe
944 * Returns NOTIFY_OK always.
946 static int fcoe_cpu_callback(struct notifier_block *nfb,
947 unsigned long action, void *hcpu)
949 unsigned cpu = (unsigned long)hcpu;
951 switch (action) {
952 case CPU_ONLINE:
953 case CPU_ONLINE_FROZEN:
954 FCOE_DBG("CPU %x online: Create Rx thread\n", cpu);
955 fcoe_percpu_thread_create(cpu);
956 break;
957 case CPU_DEAD:
958 case CPU_DEAD_FROZEN:
959 FCOE_DBG("CPU %x offline: Remove Rx thread\n", cpu);
960 fcoe_percpu_thread_destroy(cpu);
961 break;
962 default:
963 break;
965 return NOTIFY_OK;
968 static struct notifier_block fcoe_cpu_notifier = {
969 .notifier_call = fcoe_cpu_callback,
973 * fcoe_rcv() - this is the fcoe receive function called by NET_RX_SOFTIRQ
974 * @skb: the receive skb
975 * @dev: associated net device
976 * @ptype: context
977 * @olddev: last device
979 * this function will receive the packet and build fc frame and pass it up
981 * Returns: 0 for success
983 int fcoe_rcv(struct sk_buff *skb, struct net_device *dev,
984 struct packet_type *ptype, struct net_device *olddev)
986 struct fc_lport *lp;
987 struct fcoe_rcv_info *fr;
988 struct fcoe_interface *fcoe;
989 struct fc_frame_header *fh;
990 struct fcoe_percpu_s *fps;
991 unsigned int cpu;
993 fcoe = container_of(ptype, struct fcoe_interface, fcoe_packet_type);
994 lp = fcoe->ctlr.lp;
995 if (unlikely(lp == NULL)) {
996 FCOE_NETDEV_DBG(dev, "Cannot find hba structure");
997 goto err2;
999 if (!lp->link_up)
1000 goto err2;
1002 FCOE_NETDEV_DBG(dev, "skb_info: len:%d data_len:%d head:%p "
1003 "data:%p tail:%p end:%p sum:%d dev:%s",
1004 skb->len, skb->data_len, skb->head, skb->data,
1005 skb_tail_pointer(skb), skb_end_pointer(skb),
1006 skb->csum, skb->dev ? skb->dev->name : "<NULL>");
1008 /* check for FCOE packet type */
1009 if (unlikely(eth_hdr(skb)->h_proto != htons(ETH_P_FCOE))) {
1010 FCOE_NETDEV_DBG(dev, "Wrong FC type frame");
1011 goto err;
1015 * Check for minimum frame length, and make sure required FCoE
1016 * and FC headers are pulled into the linear data area.
1018 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
1019 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
1020 goto err;
1022 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
1023 fh = (struct fc_frame_header *) skb_transport_header(skb);
1025 fr = fcoe_dev_from_skb(skb);
1026 fr->fr_dev = lp;
1027 fr->ptype = ptype;
1030 * In case the incoming frame's exchange is originated from
1031 * the initiator, then received frame's exchange id is ANDed
1032 * with fc_cpu_mask bits to get the same cpu on which exchange
1033 * was originated, otherwise just use the current cpu.
1035 if (ntoh24(fh->fh_f_ctl) & FC_FC_EX_CTX)
1036 cpu = ntohs(fh->fh_ox_id) & fc_cpu_mask;
1037 else
1038 cpu = smp_processor_id();
1040 fps = &per_cpu(fcoe_percpu, cpu);
1041 spin_lock_bh(&fps->fcoe_rx_list.lock);
1042 if (unlikely(!fps->thread)) {
1044 * The targeted CPU is not ready, let's target
1045 * the first CPU now. For non-SMP systems this
1046 * will check the same CPU twice.
1048 FCOE_NETDEV_DBG(dev, "CPU is online, but no receive thread "
1049 "ready for incoming skb- using first online "
1050 "CPU.\n");
1052 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1053 cpu = first_cpu(cpu_online_map);
1054 fps = &per_cpu(fcoe_percpu, cpu);
1055 spin_lock_bh(&fps->fcoe_rx_list.lock);
1056 if (!fps->thread) {
1057 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1058 goto err;
1063 * We now have a valid CPU that we're targeting for
1064 * this skb. We also have this receive thread locked,
1065 * so we're free to queue skbs into it's queue.
1067 __skb_queue_tail(&fps->fcoe_rx_list, skb);
1068 if (fps->fcoe_rx_list.qlen == 1)
1069 wake_up_process(fps->thread);
1071 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1073 return 0;
1074 err:
1075 fc_lport_get_stats(lp)->ErrorFrames++;
1077 err2:
1078 kfree_skb(skb);
1079 return -1;
1083 * fcoe_start_io() - pass to netdev to start xmit for fcoe
1084 * @skb: the skb to be xmitted
1086 * Returns: 0 for success
1088 static inline int fcoe_start_io(struct sk_buff *skb)
1090 int rc;
1092 skb_get(skb);
1093 rc = dev_queue_xmit(skb);
1094 if (rc != 0)
1095 return rc;
1096 kfree_skb(skb);
1097 return 0;
1101 * fcoe_get_paged_crc_eof() - in case we need to alloc a page for crc_eof
1102 * @skb: the skb to be xmitted
1103 * @tlen: total len
1105 * Returns: 0 for success
1107 static int fcoe_get_paged_crc_eof(struct sk_buff *skb, int tlen)
1109 struct fcoe_percpu_s *fps;
1110 struct page *page;
1112 fps = &get_cpu_var(fcoe_percpu);
1113 page = fps->crc_eof_page;
1114 if (!page) {
1115 page = alloc_page(GFP_ATOMIC);
1116 if (!page) {
1117 put_cpu_var(fcoe_percpu);
1118 return -ENOMEM;
1120 fps->crc_eof_page = page;
1121 fps->crc_eof_offset = 0;
1124 get_page(page);
1125 skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags, page,
1126 fps->crc_eof_offset, tlen);
1127 skb->len += tlen;
1128 skb->data_len += tlen;
1129 skb->truesize += tlen;
1130 fps->crc_eof_offset += sizeof(struct fcoe_crc_eof);
1132 if (fps->crc_eof_offset >= PAGE_SIZE) {
1133 fps->crc_eof_page = NULL;
1134 fps->crc_eof_offset = 0;
1135 put_page(page);
1137 put_cpu_var(fcoe_percpu);
1138 return 0;
1142 * fcoe_fc_crc() - calculates FC CRC in this fcoe skb
1143 * @fp: the fc_frame containing data to be checksummed
1145 * This uses crc32() to calculate the crc for port frame
1146 * Return : 32 bit crc
1148 u32 fcoe_fc_crc(struct fc_frame *fp)
1150 struct sk_buff *skb = fp_skb(fp);
1151 struct skb_frag_struct *frag;
1152 unsigned char *data;
1153 unsigned long off, len, clen;
1154 u32 crc;
1155 unsigned i;
1157 crc = crc32(~0, skb->data, skb_headlen(skb));
1159 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1160 frag = &skb_shinfo(skb)->frags[i];
1161 off = frag->page_offset;
1162 len = frag->size;
1163 while (len > 0) {
1164 clen = min(len, PAGE_SIZE - (off & ~PAGE_MASK));
1165 data = kmap_atomic(frag->page + (off >> PAGE_SHIFT),
1166 KM_SKB_DATA_SOFTIRQ);
1167 crc = crc32(crc, data + (off & ~PAGE_MASK), clen);
1168 kunmap_atomic(data, KM_SKB_DATA_SOFTIRQ);
1169 off += clen;
1170 len -= clen;
1173 return crc;
1177 * fcoe_xmit() - FCoE frame transmit function
1178 * @lp: the associated local fcoe
1179 * @fp: the fc_frame to be transmitted
1181 * Return : 0 for success
1183 int fcoe_xmit(struct fc_lport *lp, struct fc_frame *fp)
1185 int wlen;
1186 u32 crc;
1187 struct ethhdr *eh;
1188 struct fcoe_crc_eof *cp;
1189 struct sk_buff *skb;
1190 struct fcoe_dev_stats *stats;
1191 struct fc_frame_header *fh;
1192 unsigned int hlen; /* header length implies the version */
1193 unsigned int tlen; /* trailer length */
1194 unsigned int elen; /* eth header, may include vlan */
1195 struct fcoe_port *port = lport_priv(lp);
1196 struct fcoe_interface *fcoe = port->fcoe;
1197 u8 sof, eof;
1198 struct fcoe_hdr *hp;
1200 WARN_ON((fr_len(fp) % sizeof(u32)) != 0);
1202 fh = fc_frame_header_get(fp);
1203 skb = fp_skb(fp);
1204 wlen = skb->len / FCOE_WORD_TO_BYTE;
1206 if (!lp->link_up) {
1207 kfree_skb(skb);
1208 return 0;
1211 if (unlikely(fh->fh_r_ctl == FC_RCTL_ELS_REQ) &&
1212 fcoe_ctlr_els_send(&fcoe->ctlr, skb))
1213 return 0;
1215 sof = fr_sof(fp);
1216 eof = fr_eof(fp);
1218 elen = sizeof(struct ethhdr);
1219 hlen = sizeof(struct fcoe_hdr);
1220 tlen = sizeof(struct fcoe_crc_eof);
1221 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
1223 /* crc offload */
1224 if (likely(lp->crc_offload)) {
1225 skb->ip_summed = CHECKSUM_PARTIAL;
1226 skb->csum_start = skb_headroom(skb);
1227 skb->csum_offset = skb->len;
1228 crc = 0;
1229 } else {
1230 skb->ip_summed = CHECKSUM_NONE;
1231 crc = fcoe_fc_crc(fp);
1234 /* copy port crc and eof to the skb buff */
1235 if (skb_is_nonlinear(skb)) {
1236 skb_frag_t *frag;
1237 if (fcoe_get_paged_crc_eof(skb, tlen)) {
1238 kfree_skb(skb);
1239 return -ENOMEM;
1241 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
1242 cp = kmap_atomic(frag->page, KM_SKB_DATA_SOFTIRQ)
1243 + frag->page_offset;
1244 } else {
1245 cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
1248 memset(cp, 0, sizeof(*cp));
1249 cp->fcoe_eof = eof;
1250 cp->fcoe_crc32 = cpu_to_le32(~crc);
1252 if (skb_is_nonlinear(skb)) {
1253 kunmap_atomic(cp, KM_SKB_DATA_SOFTIRQ);
1254 cp = NULL;
1257 /* adjust skb network/transport offsets to match mac/fcoe/port */
1258 skb_push(skb, elen + hlen);
1259 skb_reset_mac_header(skb);
1260 skb_reset_network_header(skb);
1261 skb->mac_len = elen;
1262 skb->protocol = htons(ETH_P_FCOE);
1263 skb->dev = fcoe->netdev;
1265 /* fill up mac and fcoe headers */
1266 eh = eth_hdr(skb);
1267 eh->h_proto = htons(ETH_P_FCOE);
1268 if (fcoe->ctlr.map_dest)
1269 fc_fcoe_set_mac(eh->h_dest, fh->fh_d_id);
1270 else
1271 /* insert GW address */
1272 memcpy(eh->h_dest, fcoe->ctlr.dest_addr, ETH_ALEN);
1274 if (unlikely(fcoe->ctlr.flogi_oxid != FC_XID_UNKNOWN))
1275 memcpy(eh->h_source, fcoe->ctlr.ctl_src_addr, ETH_ALEN);
1276 else
1277 memcpy(eh->h_source, fcoe->ctlr.data_src_addr, ETH_ALEN);
1279 hp = (struct fcoe_hdr *)(eh + 1);
1280 memset(hp, 0, sizeof(*hp));
1281 if (FC_FCOE_VER)
1282 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
1283 hp->fcoe_sof = sof;
1285 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
1286 if (lp->seq_offload && fr_max_payload(fp)) {
1287 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
1288 skb_shinfo(skb)->gso_size = fr_max_payload(fp);
1289 } else {
1290 skb_shinfo(skb)->gso_type = 0;
1291 skb_shinfo(skb)->gso_size = 0;
1293 /* update tx stats: regardless if LLD fails */
1294 stats = fc_lport_get_stats(lp);
1295 stats->TxFrames++;
1296 stats->TxWords += wlen;
1298 /* send down to lld */
1299 fr_dev(fp) = lp;
1300 if (port->fcoe_pending_queue.qlen)
1301 fcoe_check_wait_queue(lp, skb);
1302 else if (fcoe_start_io(skb))
1303 fcoe_check_wait_queue(lp, skb);
1305 return 0;
1309 * fcoe_percpu_flush_done() - Indicate percpu queue flush completion.
1310 * @skb: the skb being completed.
1312 static void fcoe_percpu_flush_done(struct sk_buff *skb)
1314 complete(&fcoe_flush_completion);
1318 * fcoe_percpu_receive_thread() - recv thread per cpu
1319 * @arg: ptr to the fcoe per cpu struct
1321 * Return: 0 for success
1323 int fcoe_percpu_receive_thread(void *arg)
1325 struct fcoe_percpu_s *p = arg;
1326 u32 fr_len;
1327 struct fc_lport *lp;
1328 struct fcoe_rcv_info *fr;
1329 struct fcoe_dev_stats *stats;
1330 struct fc_frame_header *fh;
1331 struct sk_buff *skb;
1332 struct fcoe_crc_eof crc_eof;
1333 struct fc_frame *fp;
1334 u8 *mac = NULL;
1335 struct fcoe_port *port;
1336 struct fcoe_hdr *hp;
1338 set_user_nice(current, -20);
1340 while (!kthread_should_stop()) {
1342 spin_lock_bh(&p->fcoe_rx_list.lock);
1343 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) == NULL) {
1344 set_current_state(TASK_INTERRUPTIBLE);
1345 spin_unlock_bh(&p->fcoe_rx_list.lock);
1346 schedule();
1347 set_current_state(TASK_RUNNING);
1348 if (kthread_should_stop())
1349 return 0;
1350 spin_lock_bh(&p->fcoe_rx_list.lock);
1352 spin_unlock_bh(&p->fcoe_rx_list.lock);
1353 fr = fcoe_dev_from_skb(skb);
1354 lp = fr->fr_dev;
1355 if (unlikely(lp == NULL)) {
1356 if (skb->destructor != fcoe_percpu_flush_done)
1357 FCOE_NETDEV_DBG(skb->dev, "NULL lport in skb");
1358 kfree_skb(skb);
1359 continue;
1362 FCOE_NETDEV_DBG(skb->dev, "skb_info: len:%d data_len:%d "
1363 "head:%p data:%p tail:%p end:%p sum:%d dev:%s",
1364 skb->len, skb->data_len,
1365 skb->head, skb->data, skb_tail_pointer(skb),
1366 skb_end_pointer(skb), skb->csum,
1367 skb->dev ? skb->dev->name : "<NULL>");
1370 * Save source MAC address before discarding header.
1372 port = lport_priv(lp);
1373 if (skb_is_nonlinear(skb))
1374 skb_linearize(skb); /* not ideal */
1375 mac = eth_hdr(skb)->h_source;
1378 * Frame length checks and setting up the header pointers
1379 * was done in fcoe_rcv already.
1381 hp = (struct fcoe_hdr *) skb_network_header(skb);
1382 fh = (struct fc_frame_header *) skb_transport_header(skb);
1384 stats = fc_lport_get_stats(lp);
1385 if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) {
1386 if (stats->ErrorFrames < 5)
1387 printk(KERN_WARNING "fcoe: FCoE version "
1388 "mismatch: The frame has "
1389 "version %x, but the "
1390 "initiator supports version "
1391 "%x\n", FC_FCOE_DECAPS_VER(hp),
1392 FC_FCOE_VER);
1393 stats->ErrorFrames++;
1394 kfree_skb(skb);
1395 continue;
1398 skb_pull(skb, sizeof(struct fcoe_hdr));
1399 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
1401 stats->RxFrames++;
1402 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
1404 fp = (struct fc_frame *)skb;
1405 fc_frame_init(fp);
1406 fr_dev(fp) = lp;
1407 fr_sof(fp) = hp->fcoe_sof;
1409 /* Copy out the CRC and EOF trailer for access */
1410 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof))) {
1411 kfree_skb(skb);
1412 continue;
1414 fr_eof(fp) = crc_eof.fcoe_eof;
1415 fr_crc(fp) = crc_eof.fcoe_crc32;
1416 if (pskb_trim(skb, fr_len)) {
1417 kfree_skb(skb);
1418 continue;
1422 * We only check CRC if no offload is available and if it is
1423 * it's solicited data, in which case, the FCP layer would
1424 * check it during the copy.
1426 if (lp->crc_offload && skb->ip_summed == CHECKSUM_UNNECESSARY)
1427 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1428 else
1429 fr_flags(fp) |= FCPHF_CRC_UNCHECKED;
1431 fh = fc_frame_header_get(fp);
1432 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA &&
1433 fh->fh_type == FC_TYPE_FCP) {
1434 fc_exch_recv(lp, fp);
1435 continue;
1437 if (fr_flags(fp) & FCPHF_CRC_UNCHECKED) {
1438 if (le32_to_cpu(fr_crc(fp)) !=
1439 ~crc32(~0, skb->data, fr_len)) {
1440 if (stats->InvalidCRCCount < 5)
1441 printk(KERN_WARNING "fcoe: dropping "
1442 "frame with CRC error\n");
1443 stats->InvalidCRCCount++;
1444 stats->ErrorFrames++;
1445 fc_frame_free(fp);
1446 continue;
1448 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1450 if (unlikely(port->fcoe->ctlr.flogi_oxid != FC_XID_UNKNOWN) &&
1451 fcoe_ctlr_recv_flogi(&port->fcoe->ctlr, fp, mac)) {
1452 fc_frame_free(fp);
1453 continue;
1455 fc_exch_recv(lp, fp);
1457 return 0;
1461 * fcoe_check_wait_queue() - attempt to clear the transmit backlog
1462 * @lp: the fc_lport
1464 * This empties the wait_queue, dequeue the head of the wait_queue queue
1465 * and calls fcoe_start_io() for each packet, if all skb have been
1466 * transmitted, return qlen or -1 if a error occurs, then restore
1467 * wait_queue and try again later.
1469 * The wait_queue is used when the skb transmit fails. skb will go
1470 * in the wait_queue which will be emptied by the timer function or
1471 * by the next skb transmit.
1473 static void fcoe_check_wait_queue(struct fc_lport *lp, struct sk_buff *skb)
1475 struct fcoe_port *port = lport_priv(lp);
1476 int rc;
1478 spin_lock_bh(&port->fcoe_pending_queue.lock);
1480 if (skb)
1481 __skb_queue_tail(&port->fcoe_pending_queue, skb);
1483 if (port->fcoe_pending_queue_active)
1484 goto out;
1485 port->fcoe_pending_queue_active = 1;
1487 while (port->fcoe_pending_queue.qlen) {
1488 /* keep qlen > 0 until fcoe_start_io succeeds */
1489 port->fcoe_pending_queue.qlen++;
1490 skb = __skb_dequeue(&port->fcoe_pending_queue);
1492 spin_unlock_bh(&port->fcoe_pending_queue.lock);
1493 rc = fcoe_start_io(skb);
1494 spin_lock_bh(&port->fcoe_pending_queue.lock);
1496 if (rc) {
1497 __skb_queue_head(&port->fcoe_pending_queue, skb);
1498 /* undo temporary increment above */
1499 port->fcoe_pending_queue.qlen--;
1500 break;
1502 /* undo temporary increment above */
1503 port->fcoe_pending_queue.qlen--;
1506 if (port->fcoe_pending_queue.qlen < FCOE_LOW_QUEUE_DEPTH)
1507 lp->qfull = 0;
1508 if (port->fcoe_pending_queue.qlen && !timer_pending(&port->timer))
1509 mod_timer(&port->timer, jiffies + 2);
1510 port->fcoe_pending_queue_active = 0;
1511 out:
1512 if (port->fcoe_pending_queue.qlen > FCOE_MAX_QUEUE_DEPTH)
1513 lp->qfull = 1;
1514 spin_unlock_bh(&port->fcoe_pending_queue.lock);
1515 return;
1519 * fcoe_dev_setup() - setup link change notification interface
1521 static void fcoe_dev_setup(void)
1523 register_netdevice_notifier(&fcoe_notifier);
1527 * fcoe_dev_cleanup() - cleanup link change notification interface
1529 static void fcoe_dev_cleanup(void)
1531 unregister_netdevice_notifier(&fcoe_notifier);
1535 * fcoe_device_notification() - netdev event notification callback
1536 * @notifier: context of the notification
1537 * @event: type of event
1538 * @ptr: fixed array for output parsed ifname
1540 * This function is called by the ethernet driver in case of link change event
1542 * Returns: 0 for success
1544 static int fcoe_device_notification(struct notifier_block *notifier,
1545 ulong event, void *ptr)
1547 struct fc_lport *lp = NULL;
1548 struct net_device *netdev = ptr;
1549 struct fcoe_interface *fcoe;
1550 struct fcoe_port *port;
1551 struct fcoe_dev_stats *stats;
1552 u32 link_possible = 1;
1553 u32 mfs;
1554 int rc = NOTIFY_OK;
1556 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
1557 if (fcoe->netdev == netdev) {
1558 lp = fcoe->ctlr.lp;
1559 break;
1562 if (lp == NULL) {
1563 rc = NOTIFY_DONE;
1564 goto out;
1567 switch (event) {
1568 case NETDEV_DOWN:
1569 case NETDEV_GOING_DOWN:
1570 link_possible = 0;
1571 break;
1572 case NETDEV_UP:
1573 case NETDEV_CHANGE:
1574 break;
1575 case NETDEV_CHANGEMTU:
1576 mfs = netdev->mtu - (sizeof(struct fcoe_hdr) +
1577 sizeof(struct fcoe_crc_eof));
1578 if (mfs >= FC_MIN_MAX_FRAME)
1579 fc_set_mfs(lp, mfs);
1580 break;
1581 case NETDEV_REGISTER:
1582 break;
1583 case NETDEV_UNREGISTER:
1584 list_del(&fcoe->list);
1585 port = lport_priv(fcoe->ctlr.lp);
1586 fcoe_interface_cleanup(fcoe);
1587 schedule_work(&port->destroy_work);
1588 goto out;
1589 break;
1590 default:
1591 FCOE_NETDEV_DBG(netdev, "Unknown event %ld "
1592 "from netdev netlink\n", event);
1594 if (link_possible && !fcoe_link_ok(lp))
1595 fcoe_ctlr_link_up(&fcoe->ctlr);
1596 else if (fcoe_ctlr_link_down(&fcoe->ctlr)) {
1597 stats = fc_lport_get_stats(lp);
1598 stats->LinkFailureCount++;
1599 fcoe_clean_pending_queue(lp);
1601 out:
1602 return rc;
1606 * fcoe_if_to_netdev() - parse a name buffer to get netdev
1607 * @buffer: incoming buffer to be copied
1609 * Returns: NULL or ptr to net_device
1611 static struct net_device *fcoe_if_to_netdev(const char *buffer)
1613 char *cp;
1614 char ifname[IFNAMSIZ + 2];
1616 if (buffer) {
1617 strlcpy(ifname, buffer, IFNAMSIZ);
1618 cp = ifname + strlen(ifname);
1619 while (--cp >= ifname && *cp == '\n')
1620 *cp = '\0';
1621 return dev_get_by_name(&init_net, ifname);
1623 return NULL;
1627 * fcoe_destroy() - handles the destroy from sysfs
1628 * @buffer: expected to be an eth if name
1629 * @kp: associated kernel param
1631 * Returns: 0 for success
1633 static int fcoe_destroy(const char *buffer, struct kernel_param *kp)
1635 struct fcoe_interface *fcoe;
1636 struct net_device *netdev;
1637 int rc = 0;
1639 mutex_lock(&fcoe_config_mutex);
1640 #ifdef CONFIG_FCOE_MODULE
1642 * Make sure the module has been initialized, and is not about to be
1643 * removed. Module paramter sysfs files are writable before the
1644 * module_init function is called and after module_exit.
1646 if (THIS_MODULE->state != MODULE_STATE_LIVE) {
1647 rc = -ENODEV;
1648 goto out_nodev;
1650 #endif
1652 netdev = fcoe_if_to_netdev(buffer);
1653 if (!netdev) {
1654 rc = -ENODEV;
1655 goto out_nodev;
1658 rtnl_lock();
1659 fcoe = fcoe_hostlist_lookup_port(netdev);
1660 if (!fcoe) {
1661 rtnl_unlock();
1662 rc = -ENODEV;
1663 goto out_putdev;
1665 list_del(&fcoe->list);
1666 fcoe_interface_cleanup(fcoe);
1667 rtnl_unlock();
1668 fcoe_if_destroy(fcoe->ctlr.lp);
1669 out_putdev:
1670 dev_put(netdev);
1671 out_nodev:
1672 mutex_unlock(&fcoe_config_mutex);
1673 return rc;
1676 static void fcoe_destroy_work(struct work_struct *work)
1678 struct fcoe_port *port;
1680 port = container_of(work, struct fcoe_port, destroy_work);
1681 mutex_lock(&fcoe_config_mutex);
1682 fcoe_if_destroy(port->lport);
1683 mutex_unlock(&fcoe_config_mutex);
1687 * fcoe_create() - Handles the create call from sysfs
1688 * @buffer: expected to be an eth if name
1689 * @kp: associated kernel param
1691 * Returns: 0 for success
1693 static int fcoe_create(const char *buffer, struct kernel_param *kp)
1695 int rc;
1696 struct fcoe_interface *fcoe;
1697 struct fc_lport *lport;
1698 struct net_device *netdev;
1700 mutex_lock(&fcoe_config_mutex);
1701 #ifdef CONFIG_FCOE_MODULE
1703 * Make sure the module has been initialized, and is not about to be
1704 * removed. Module paramter sysfs files are writable before the
1705 * module_init function is called and after module_exit.
1707 if (THIS_MODULE->state != MODULE_STATE_LIVE) {
1708 rc = -ENODEV;
1709 goto out_nodev;
1711 #endif
1713 rtnl_lock();
1714 netdev = fcoe_if_to_netdev(buffer);
1715 if (!netdev) {
1716 rc = -ENODEV;
1717 goto out_nodev;
1720 /* look for existing lport */
1721 if (fcoe_hostlist_lookup(netdev)) {
1722 rc = -EEXIST;
1723 goto out_putdev;
1726 fcoe = fcoe_interface_create(netdev);
1727 if (!fcoe) {
1728 rc = -ENOMEM;
1729 goto out_putdev;
1732 lport = fcoe_if_create(fcoe, &netdev->dev);
1733 if (IS_ERR(lport)) {
1734 printk(KERN_ERR "fcoe: Failed to create interface (%s)\n",
1735 netdev->name);
1736 rc = -EIO;
1737 fcoe_interface_cleanup(fcoe);
1738 goto out_free;
1741 /* Make this the "master" N_Port */
1742 fcoe->ctlr.lp = lport;
1744 /* add to lports list */
1745 fcoe_hostlist_add(lport);
1747 /* start FIP Discovery and FLOGI */
1748 lport->boot_time = jiffies;
1749 fc_fabric_login(lport);
1750 if (!fcoe_link_ok(lport))
1751 fcoe_ctlr_link_up(&fcoe->ctlr);
1753 rc = 0;
1754 out_free:
1756 * Release from init in fcoe_interface_create(), on success lport
1757 * should be holding a reference taken in fcoe_if_create().
1759 fcoe_interface_put(fcoe);
1760 out_putdev:
1761 dev_put(netdev);
1762 out_nodev:
1763 rtnl_unlock();
1764 mutex_unlock(&fcoe_config_mutex);
1765 return rc;
1768 module_param_call(create, fcoe_create, NULL, NULL, S_IWUSR);
1769 __MODULE_PARM_TYPE(create, "string");
1770 MODULE_PARM_DESC(create, "Create fcoe fcoe using net device passed in.");
1771 module_param_call(destroy, fcoe_destroy, NULL, NULL, S_IWUSR);
1772 __MODULE_PARM_TYPE(destroy, "string");
1773 MODULE_PARM_DESC(destroy, "Destroy fcoe fcoe");
1776 * fcoe_link_ok() - Check if link is ok for the fc_lport
1777 * @lp: ptr to the fc_lport
1779 * Any permanently-disqualifying conditions have been previously checked.
1780 * This also updates the speed setting, which may change with link for 100/1000.
1782 * This function should probably be checking for PAUSE support at some point
1783 * in the future. Currently Per-priority-pause is not determinable using
1784 * ethtool, so we shouldn't be restrictive until that problem is resolved.
1786 * Returns: 0 if link is OK for use by FCoE.
1789 int fcoe_link_ok(struct fc_lport *lp)
1791 struct fcoe_port *port = lport_priv(lp);
1792 struct net_device *dev = port->fcoe->netdev;
1793 struct ethtool_cmd ecmd = { ETHTOOL_GSET };
1795 if ((dev->flags & IFF_UP) && netif_carrier_ok(dev) &&
1796 (!dev_ethtool_get_settings(dev, &ecmd))) {
1797 lp->link_supported_speeds &=
1798 ~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
1799 if (ecmd.supported & (SUPPORTED_1000baseT_Half |
1800 SUPPORTED_1000baseT_Full))
1801 lp->link_supported_speeds |= FC_PORTSPEED_1GBIT;
1802 if (ecmd.supported & SUPPORTED_10000baseT_Full)
1803 lp->link_supported_speeds |=
1804 FC_PORTSPEED_10GBIT;
1805 if (ecmd.speed == SPEED_1000)
1806 lp->link_speed = FC_PORTSPEED_1GBIT;
1807 if (ecmd.speed == SPEED_10000)
1808 lp->link_speed = FC_PORTSPEED_10GBIT;
1810 return 0;
1812 return -1;
1816 * fcoe_percpu_clean() - Clear the pending skbs for an lport
1817 * @lp: the fc_lport
1819 * Must be called with fcoe_create_mutex held to single-thread completion.
1821 * This flushes the pending skbs by adding a new skb to each queue and
1822 * waiting until they are all freed. This assures us that not only are
1823 * there no packets that will be handled by the lport, but also that any
1824 * threads already handling packet have returned.
1826 void fcoe_percpu_clean(struct fc_lport *lp)
1828 struct fcoe_percpu_s *pp;
1829 struct fcoe_rcv_info *fr;
1830 struct sk_buff_head *list;
1831 struct sk_buff *skb, *next;
1832 struct sk_buff *head;
1833 unsigned int cpu;
1835 for_each_possible_cpu(cpu) {
1836 pp = &per_cpu(fcoe_percpu, cpu);
1837 spin_lock_bh(&pp->fcoe_rx_list.lock);
1838 list = &pp->fcoe_rx_list;
1839 head = list->next;
1840 for (skb = head; skb != (struct sk_buff *)list;
1841 skb = next) {
1842 next = skb->next;
1843 fr = fcoe_dev_from_skb(skb);
1844 if (fr->fr_dev == lp) {
1845 __skb_unlink(skb, list);
1846 kfree_skb(skb);
1850 if (!pp->thread || !cpu_online(cpu)) {
1851 spin_unlock_bh(&pp->fcoe_rx_list.lock);
1852 continue;
1855 skb = dev_alloc_skb(0);
1856 if (!skb) {
1857 spin_unlock_bh(&pp->fcoe_rx_list.lock);
1858 continue;
1860 skb->destructor = fcoe_percpu_flush_done;
1862 __skb_queue_tail(&pp->fcoe_rx_list, skb);
1863 if (pp->fcoe_rx_list.qlen == 1)
1864 wake_up_process(pp->thread);
1865 spin_unlock_bh(&pp->fcoe_rx_list.lock);
1867 wait_for_completion(&fcoe_flush_completion);
1872 * fcoe_clean_pending_queue() - Dequeue a skb and free it
1873 * @lp: the corresponding fc_lport
1875 * Returns: none
1877 void fcoe_clean_pending_queue(struct fc_lport *lp)
1879 struct fcoe_port *port = lport_priv(lp);
1880 struct sk_buff *skb;
1882 spin_lock_bh(&port->fcoe_pending_queue.lock);
1883 while ((skb = __skb_dequeue(&port->fcoe_pending_queue)) != NULL) {
1884 spin_unlock_bh(&port->fcoe_pending_queue.lock);
1885 kfree_skb(skb);
1886 spin_lock_bh(&port->fcoe_pending_queue.lock);
1888 spin_unlock_bh(&port->fcoe_pending_queue.lock);
1892 * fcoe_reset() - Resets the fcoe
1893 * @shost: shost the reset is from
1895 * Returns: always 0
1897 int fcoe_reset(struct Scsi_Host *shost)
1899 struct fc_lport *lport = shost_priv(shost);
1900 fc_lport_reset(lport);
1901 return 0;
1905 * fcoe_hostlist_lookup_port() - find the corresponding lport by a given device
1906 * @dev: this is currently ptr to net_device
1908 * Returns: NULL or the located fcoe_port
1909 * Locking: must be called with the RNL mutex held
1911 static struct fcoe_interface *
1912 fcoe_hostlist_lookup_port(const struct net_device *dev)
1914 struct fcoe_interface *fcoe;
1916 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
1917 if (fcoe->netdev == dev)
1918 return fcoe;
1920 return NULL;
1924 * fcoe_hostlist_lookup() - Find the corresponding lport by netdev
1925 * @netdev: ptr to net_device
1927 * Returns: 0 for success
1928 * Locking: must be called with the RTNL mutex held
1930 static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *netdev)
1932 struct fcoe_interface *fcoe;
1934 fcoe = fcoe_hostlist_lookup_port(netdev);
1935 return (fcoe) ? fcoe->ctlr.lp : NULL;
1939 * fcoe_hostlist_add() - Add a lport to lports list
1940 * @lp: ptr to the fc_lport to be added
1942 * Returns: 0 for success
1943 * Locking: must be called with the RTNL mutex held
1945 static int fcoe_hostlist_add(const struct fc_lport *lport)
1947 struct fcoe_interface *fcoe;
1948 struct fcoe_port *port;
1950 fcoe = fcoe_hostlist_lookup_port(fcoe_netdev(lport));
1951 if (!fcoe) {
1952 port = lport_priv(lport);
1953 fcoe = port->fcoe;
1954 list_add_tail(&fcoe->list, &fcoe_hostlist);
1956 return 0;
1960 * fcoe_init() - fcoe module loading initialization
1962 * Returns 0 on success, negative on failure
1964 static int __init fcoe_init(void)
1966 unsigned int cpu;
1967 int rc = 0;
1968 struct fcoe_percpu_s *p;
1970 mutex_lock(&fcoe_config_mutex);
1972 for_each_possible_cpu(cpu) {
1973 p = &per_cpu(fcoe_percpu, cpu);
1974 skb_queue_head_init(&p->fcoe_rx_list);
1977 for_each_online_cpu(cpu)
1978 fcoe_percpu_thread_create(cpu);
1980 /* Initialize per CPU interrupt thread */
1981 rc = register_hotcpu_notifier(&fcoe_cpu_notifier);
1982 if (rc)
1983 goto out_free;
1985 /* Setup link change notification */
1986 fcoe_dev_setup();
1988 rc = fcoe_if_init();
1989 if (rc)
1990 goto out_free;
1992 mutex_unlock(&fcoe_config_mutex);
1993 return 0;
1995 out_free:
1996 for_each_online_cpu(cpu) {
1997 fcoe_percpu_thread_destroy(cpu);
1999 mutex_unlock(&fcoe_config_mutex);
2000 return rc;
2002 module_init(fcoe_init);
2005 * fcoe_exit() - fcoe module unloading cleanup
2007 * Returns 0 on success, negative on failure
2009 static void __exit fcoe_exit(void)
2011 unsigned int cpu;
2012 struct fcoe_interface *fcoe, *tmp;
2013 struct fcoe_port *port;
2015 mutex_lock(&fcoe_config_mutex);
2017 fcoe_dev_cleanup();
2019 /* releases the associated fcoe hosts */
2020 rtnl_lock();
2021 list_for_each_entry_safe(fcoe, tmp, &fcoe_hostlist, list) {
2022 list_del(&fcoe->list);
2023 port = lport_priv(fcoe->ctlr.lp);
2024 fcoe_interface_cleanup(fcoe);
2025 schedule_work(&port->destroy_work);
2027 rtnl_unlock();
2029 unregister_hotcpu_notifier(&fcoe_cpu_notifier);
2031 for_each_online_cpu(cpu)
2032 fcoe_percpu_thread_destroy(cpu);
2034 mutex_unlock(&fcoe_config_mutex);
2036 /* flush any asyncronous interface destroys,
2037 * this should happen after the netdev notifier is unregistered */
2038 flush_scheduled_work();
2040 /* detach from scsi transport
2041 * must happen after all destroys are done, therefor after the flush */
2042 fcoe_if_exit();
2044 module_exit(fcoe_exit);