2 * Copyright(c) 2007 - 2009 Intel Corporation. All rights reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
17 * Maintained at www.Open-FCoE.org
20 #include <linux/module.h>
21 #include <linux/version.h>
22 #include <linux/spinlock.h>
23 #include <linux/netdevice.h>
24 #include <linux/etherdevice.h>
25 #include <linux/ethtool.h>
26 #include <linux/if_ether.h>
27 #include <linux/if_vlan.h>
28 #include <linux/crc32.h>
29 #include <linux/cpu.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>
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).");
59 LIST_HEAD(fcoe_hostlist);
60 DEFINE_RWLOCK(fcoe_hostlist_lock);
61 DEFINE_PER_CPU(struct fcoe_percpu_s, fcoe_percpu);
63 /* Function Prototypes */
64 static int fcoe_reset(struct Scsi_Host *shost);
65 static int fcoe_xmit(struct fc_lport *, struct fc_frame *);
66 static int fcoe_rcv(struct sk_buff *, struct net_device *,
67 struct packet_type *, struct net_device *);
68 static int fcoe_percpu_receive_thread(void *arg);
69 static void fcoe_clean_pending_queue(struct fc_lport *lp);
70 static void fcoe_percpu_clean(struct fc_lport *lp);
71 static int fcoe_link_ok(struct fc_lport *lp);
73 static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *);
74 static int fcoe_hostlist_add(const struct fc_lport *);
75 static int fcoe_hostlist_remove(const struct fc_lport *);
77 static void fcoe_check_wait_queue(struct fc_lport *, struct sk_buff *);
78 static int fcoe_device_notification(struct notifier_block *, ulong, void *);
79 static void fcoe_dev_setup(void);
80 static void fcoe_dev_cleanup(void);
82 /* notification function from net device */
83 static struct notifier_block fcoe_notifier = {
84 .notifier_call = fcoe_device_notification,
87 static struct scsi_transport_template *scsi_transport_fcoe_sw;
89 struct fc_function_template fcoe_transport_function = {
90 .show_host_node_name = 1,
91 .show_host_port_name = 1,
92 .show_host_supported_classes = 1,
93 .show_host_supported_fc4s = 1,
94 .show_host_active_fc4s = 1,
95 .show_host_maxframe_size = 1,
97 .show_host_port_id = 1,
98 .show_host_supported_speeds = 1,
99 .get_host_speed = fc_get_host_speed,
100 .show_host_speed = 1,
101 .show_host_port_type = 1,
102 .get_host_port_state = fc_get_host_port_state,
103 .show_host_port_state = 1,
104 .show_host_symbolic_name = 1,
106 .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
107 .show_rport_maxframe_size = 1,
108 .show_rport_supported_classes = 1,
110 .show_host_fabric_name = 1,
111 .show_starget_node_name = 1,
112 .show_starget_port_name = 1,
113 .show_starget_port_id = 1,
114 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
115 .show_rport_dev_loss_tmo = 1,
116 .get_fc_host_stats = fc_get_host_stats,
117 .issue_fc_host_lip = fcoe_reset,
119 .terminate_rport_io = fc_rport_terminate_io,
122 static struct scsi_host_template fcoe_shost_template = {
123 .module = THIS_MODULE,
124 .name = "FCoE Driver",
125 .proc_name = FCOE_NAME,
126 .queuecommand = fc_queuecommand,
127 .eh_abort_handler = fc_eh_abort,
128 .eh_device_reset_handler = fc_eh_device_reset,
129 .eh_host_reset_handler = fc_eh_host_reset,
130 .slave_alloc = fc_slave_alloc,
131 .change_queue_depth = fc_change_queue_depth,
132 .change_queue_type = fc_change_queue_type,
135 .can_queue = FCOE_MAX_OUTSTANDING_COMMANDS,
136 .use_clustering = ENABLE_CLUSTERING,
137 .sg_tablesize = SG_ALL,
138 .max_sectors = 0xffff,
141 static int fcoe_fip_recv(struct sk_buff *skb, struct net_device *dev,
142 struct packet_type *ptype,
143 struct net_device *orig_dev);
145 * fcoe_interface_setup()
146 * @fcoe: new fcoe_interface
147 * @netdev : ptr to the associated netdevice struct
149 * Returns : 0 for success
151 static int fcoe_interface_setup(struct fcoe_interface *fcoe,
152 struct net_device *netdev)
154 struct fcoe_ctlr *fip = &fcoe->ctlr;
155 struct netdev_hw_addr *ha;
156 u8 flogi_maddr[ETH_ALEN];
158 fcoe->netdev = netdev;
160 /* Do not support for bonding device */
161 if ((netdev->priv_flags & IFF_MASTER_ALB) ||
162 (netdev->priv_flags & IFF_SLAVE_INACTIVE) ||
163 (netdev->priv_flags & IFF_MASTER_8023AD)) {
167 /* look for SAN MAC address, if multiple SAN MACs exist, only
168 * use the first one for SPMA */
170 for_each_dev_addr(netdev, ha) {
171 if ((ha->type == NETDEV_HW_ADDR_T_SAN) &&
172 (is_valid_ether_addr(fip->ctl_src_addr))) {
173 memcpy(fip->ctl_src_addr, ha->addr, ETH_ALEN);
180 /* setup Source Mac Address */
182 memcpy(fip->ctl_src_addr, netdev->dev_addr, netdev->addr_len);
185 * Add FCoE MAC address as second unicast MAC address
186 * or enter promiscuous mode if not capable of listening
187 * for multiple unicast MACs.
190 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
191 dev_unicast_add(netdev, flogi_maddr);
193 dev_unicast_add(netdev, fip->ctl_src_addr);
194 dev_mc_add(netdev, FIP_ALL_ENODE_MACS, ETH_ALEN, 0);
198 * setup the receive function from ethernet driver
199 * on the ethertype for the given device
201 fcoe->fcoe_packet_type.func = fcoe_rcv;
202 fcoe->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
203 fcoe->fcoe_packet_type.dev = netdev;
204 dev_add_pack(&fcoe->fcoe_packet_type);
206 fcoe->fip_packet_type.func = fcoe_fip_recv;
207 fcoe->fip_packet_type.type = htons(ETH_P_FIP);
208 fcoe->fip_packet_type.dev = netdev;
209 dev_add_pack(&fcoe->fip_packet_type);
214 static void fcoe_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb);
215 static void fcoe_update_src_mac(struct fcoe_ctlr *fip, u8 *old, u8 *new);
218 * fcoe_interface_create()
219 * @netdev: network interface
221 * Returns: pointer to a struct fcoe_interface or NULL on error
223 static struct fcoe_interface *fcoe_interface_create(struct net_device *netdev)
225 struct fcoe_interface *fcoe;
227 fcoe = kzalloc(sizeof(*fcoe), GFP_KERNEL);
229 FCOE_NETDEV_DBG(netdev, "Could not allocate fcoe structure\n");
233 kref_init(&fcoe->kref);
238 fcoe_ctlr_init(&fcoe->ctlr);
239 fcoe->ctlr.send = fcoe_fip_send;
240 fcoe->ctlr.update_mac = fcoe_update_src_mac;
242 fcoe_interface_setup(fcoe, netdev);
248 * fcoe_interface_cleanup() - clean up netdev configurations
251 void fcoe_interface_cleanup(struct fcoe_interface *fcoe)
253 struct net_device *netdev = fcoe->netdev;
254 struct fcoe_ctlr *fip = &fcoe->ctlr;
255 u8 flogi_maddr[ETH_ALEN];
258 * Don't listen for Ethernet packets anymore.
259 * synchronize_net() ensures that the packet handlers are not running
260 * on another CPU. dev_remove_pack() would do that, this calls the
261 * unsyncronized version __dev_remove_pack() to avoid multiple delays.
263 __dev_remove_pack(&fcoe->fcoe_packet_type);
264 __dev_remove_pack(&fcoe->fip_packet_type);
267 /* tear-down the FCoE controller */
268 fcoe_ctlr_destroy(&fcoe->ctlr);
270 /* Delete secondary MAC addresses */
272 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
273 dev_unicast_delete(netdev, flogi_maddr);
274 if (!is_zero_ether_addr(fip->data_src_addr))
275 dev_unicast_delete(netdev, fip->data_src_addr);
277 dev_unicast_delete(netdev, fip->ctl_src_addr);
278 dev_mc_delete(netdev, FIP_ALL_ENODE_MACS, ETH_ALEN, 0);
283 * fcoe_interface_release() - fcoe_port kref release function
284 * @kref: embedded reference count in an fcoe_interface struct
286 static void fcoe_interface_release(struct kref *kref)
288 struct fcoe_interface *fcoe;
290 fcoe = container_of(kref, struct fcoe_interface, kref);
291 fcoe_interface_cleanup(fcoe);
296 * fcoe_interface_get()
299 static inline void fcoe_interface_get(struct fcoe_interface *fcoe)
301 kref_get(&fcoe->kref);
305 * fcoe_interface_put()
308 static inline void fcoe_interface_put(struct fcoe_interface *fcoe)
310 kref_put(&fcoe->kref, fcoe_interface_release);
314 * fcoe_fip_recv - handle a received FIP frame.
315 * @skb: the receive skb
316 * @dev: associated &net_device
317 * @ptype: the &packet_type structure which was used to register this handler.
318 * @orig_dev: original receive &net_device, in case @dev is a bond.
320 * Returns: 0 for success
322 static int fcoe_fip_recv(struct sk_buff *skb, struct net_device *dev,
323 struct packet_type *ptype,
324 struct net_device *orig_dev)
326 struct fcoe_interface *fcoe;
328 fcoe = container_of(ptype, struct fcoe_interface, fip_packet_type);
329 fcoe_ctlr_recv(&fcoe->ctlr, skb);
334 * fcoe_fip_send() - send an Ethernet-encapsulated FIP frame.
335 * @fip: FCoE controller.
338 static void fcoe_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
340 skb->dev = fcoe_from_ctlr(fip)->netdev;
345 * fcoe_update_src_mac() - Update Ethernet MAC filters.
346 * @fip: FCoE controller.
347 * @old: Unicast MAC address to delete if the MAC is non-zero.
348 * @new: Unicast MAC address to add.
350 * Remove any previously-set unicast MAC filter.
351 * Add secondary FCoE MAC address filter for our OUI.
353 static void fcoe_update_src_mac(struct fcoe_ctlr *fip, u8 *old, u8 *new)
355 struct fcoe_interface *fcoe;
357 fcoe = fcoe_from_ctlr(fip);
359 if (!is_zero_ether_addr(old))
360 dev_unicast_delete(fcoe->netdev, old);
361 dev_unicast_add(fcoe->netdev, new);
366 * fcoe_lport_config() - sets up the fc_lport
367 * @lp: ptr to the fc_lport
369 * Returns: 0 for success
371 static int fcoe_lport_config(struct fc_lport *lp)
375 lp->max_retry_count = 3;
376 lp->max_rport_retry_count = 3;
377 lp->e_d_tov = 2 * 1000; /* FC-FS default */
378 lp->r_a_tov = 2 * 2 * 1000;
379 lp->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
380 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
382 fc_lport_init_stats(lp);
384 /* lport fc_lport related configuration */
387 /* offload related configuration */
398 * fcoe_queue_timer() - fcoe queue timer
399 * @lp: the fc_lport pointer
401 * Calls fcoe_check_wait_queue on timeout
404 static void fcoe_queue_timer(ulong lp)
406 fcoe_check_wait_queue((struct fc_lport *)lp, NULL);
410 * fcoe_netdev_config() - Set up netdev for SW FCoE
411 * @lp : ptr to the fc_lport
412 * @netdev : ptr to the associated netdevice struct
414 * Must be called after fcoe_lport_config() as it will use lport mutex
416 * Returns : 0 for success
418 static int fcoe_netdev_config(struct fc_lport *lp, struct net_device *netdev)
422 struct fcoe_interface *fcoe;
423 struct fcoe_port *port;
425 /* Setup lport private data to point to fcoe softc */
426 port = lport_priv(lp);
430 * Determine max frame size based on underlying device and optional
431 * user-configured limit. If the MFS is too low, fcoe_link_ok()
432 * will return 0, so do this first.
434 mfs = netdev->mtu - (sizeof(struct fcoe_hdr) +
435 sizeof(struct fcoe_crc_eof));
436 if (fc_set_mfs(lp, mfs))
439 /* offload features support */
440 if (netdev->features & NETIF_F_SG)
443 if (netdev->features & NETIF_F_FCOE_CRC) {
445 FCOE_NETDEV_DBG(netdev, "Supports FCCRC offload\n");
447 if (netdev->features & NETIF_F_FSO) {
449 lp->lso_max = netdev->gso_max_size;
450 FCOE_NETDEV_DBG(netdev, "Supports LSO for max len 0x%x\n",
453 if (netdev->fcoe_ddp_xid) {
455 lp->lro_xid = netdev->fcoe_ddp_xid;
456 FCOE_NETDEV_DBG(netdev, "Supports LRO for max xid 0x%x\n",
459 skb_queue_head_init(&port->fcoe_pending_queue);
460 port->fcoe_pending_queue_active = 0;
461 setup_timer(&port->timer, fcoe_queue_timer, (unsigned long)lp);
463 wwnn = fcoe_wwn_from_mac(netdev->dev_addr, 1, 0);
464 fc_set_wwnn(lp, wwnn);
465 /* XXX - 3rd arg needs to be vlan id */
466 wwpn = fcoe_wwn_from_mac(netdev->dev_addr, 2, 0);
467 fc_set_wwpn(lp, wwpn);
473 * fcoe_shost_config() - Sets up fc_lport->host
474 * @lp : ptr to the fc_lport
475 * @shost : ptr to the associated scsi host
476 * @dev : device associated to scsi host
478 * Must be called after fcoe_lport_config() and fcoe_netdev_config()
480 * Returns : 0 for success
482 static int fcoe_shost_config(struct fc_lport *lp, struct Scsi_Host *shost,
487 /* lport scsi host config */
490 lp->host->max_lun = FCOE_MAX_LUN;
491 lp->host->max_id = FCOE_MAX_FCP_TARGET;
492 lp->host->max_channel = 0;
493 lp->host->transportt = scsi_transport_fcoe_sw;
495 /* add the new host to the SCSI-ml */
496 rc = scsi_add_host(lp->host, dev);
498 FCOE_NETDEV_DBG(fcoe_netdev(lp), "fcoe_shost_config: "
499 "error on scsi_add_host\n");
502 sprintf(fc_host_symbolic_name(lp->host), "%s v%s over %s",
503 FCOE_NAME, FCOE_VERSION,
504 fcoe_netdev(lp)->name);
510 * fcoe_oem_match() - match for read types IO
511 * @fp: the fc_frame for new IO.
513 * Returns : true for read types IO, otherwise returns false.
515 bool fcoe_oem_match(struct fc_frame *fp)
517 return fc_fcp_is_read(fr_fsp(fp)) &&
518 (fr_fsp(fp)->data_len > fcoe_ddp_min);
522 * fcoe_em_config() - allocates em for this lport
523 * @lp: the fcoe that em is to allocated for
525 * Called with write fcoe_hostlist_lock held.
527 * Returns : 0 on success
529 static inline int fcoe_em_config(struct fc_lport *lp)
531 struct fcoe_port *port = lport_priv(lp);
532 struct fcoe_interface *fcoe = port->fcoe;
533 struct fcoe_interface *oldfcoe = NULL;
534 struct net_device *old_real_dev, *cur_real_dev;
535 u16 min_xid = FCOE_MIN_XID;
536 u16 max_xid = FCOE_MAX_XID;
539 * Check if need to allocate an em instance for
540 * offload exchange ids to be shared across all VN_PORTs/lport.
542 if (!lp->lro_enabled || !lp->lro_xid || (lp->lro_xid >= max_xid)) {
548 * Reuse existing offload em instance in case
549 * it is already allocated on real eth device
551 if (fcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
552 cur_real_dev = vlan_dev_real_dev(fcoe->netdev);
554 cur_real_dev = fcoe->netdev;
556 list_for_each_entry(oldfcoe, &fcoe_hostlist, list) {
557 if (oldfcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
558 old_real_dev = vlan_dev_real_dev(oldfcoe->netdev);
560 old_real_dev = oldfcoe->netdev;
562 if (cur_real_dev == old_real_dev) {
563 fcoe->oem = oldfcoe->oem;
569 if (!fc_exch_mgr_add(lp, fcoe->oem, fcoe_oem_match)) {
570 printk(KERN_ERR "fcoe_em_config: failed to add "
571 "offload em:%p on interface:%s\n",
572 fcoe->oem, fcoe->netdev->name);
576 fcoe->oem = fc_exch_mgr_alloc(lp, FC_CLASS_3,
577 FCOE_MIN_XID, lp->lro_xid,
580 printk(KERN_ERR "fcoe_em_config: failed to allocate "
581 "em for offload exches on interface:%s\n",
588 * Exclude offload EM xid range from next EM xid range.
590 min_xid += lp->lro_xid + 1;
593 if (!fc_exch_mgr_alloc(lp, FC_CLASS_3, min_xid, max_xid, NULL)) {
594 printk(KERN_ERR "fcoe_em_config: failed to "
595 "allocate em on interface %s\n", fcoe->netdev->name);
603 * fcoe_if_destroy() - FCoE software HBA tear-down function
604 * @lport: fc_lport to destroy
606 static void fcoe_if_destroy(struct fc_lport *lport)
608 struct fcoe_port *port = lport_priv(lport);
609 struct fcoe_interface *fcoe = port->fcoe;
610 struct net_device *netdev = fcoe->netdev;
612 FCOE_NETDEV_DBG(netdev, "Destroying interface\n");
614 /* Logout of the fabric */
615 fc_fabric_logoff(lport);
617 /* Remove the instance from fcoe's list */
618 fcoe_hostlist_remove(lport);
620 /* Cleanup the fc_lport */
621 fc_lport_destroy(lport);
622 fc_fcp_destroy(lport);
624 /* Stop the transmit retry timer */
625 del_timer_sync(&port->timer);
627 /* Free existing transmit skbs */
628 fcoe_clean_pending_queue(lport);
630 /* receives may not be stopped until after this */
631 fcoe_interface_put(fcoe);
633 /* Free queued packets for the per-CPU receive threads */
634 fcoe_percpu_clean(lport);
636 /* Detach from the scsi-ml */
637 fc_remove_host(lport->host);
638 scsi_remove_host(lport->host);
640 /* There are no more rports or I/O, free the EM */
641 fc_exch_mgr_free(lport);
643 /* Free memory used by statistical counters */
644 fc_lport_free_stats(lport);
646 /* Release the net_device and Scsi_Host */
648 scsi_host_put(lport->host);
652 * fcoe_ddp_setup - calls LLD's ddp_setup through net_device
653 * @lp: the corresponding fc_lport
654 * @xid: the exchange id for this ddp transfer
655 * @sgl: the scatterlist describing this transfer
656 * @sgc: number of sg items
660 static int fcoe_ddp_setup(struct fc_lport *lp, u16 xid,
661 struct scatterlist *sgl, unsigned int sgc)
663 struct net_device *n = fcoe_netdev(lp);
665 if (n->netdev_ops && n->netdev_ops->ndo_fcoe_ddp_setup)
666 return n->netdev_ops->ndo_fcoe_ddp_setup(n, xid, sgl, sgc);
672 * fcoe_ddp_done - calls LLD's ddp_done through net_device
673 * @lp: the corresponding fc_lport
674 * @xid: the exchange id for this ddp transfer
676 * Returns : the length of data that have been completed by ddp
678 static int fcoe_ddp_done(struct fc_lport *lp, u16 xid)
680 struct net_device *n = fcoe_netdev(lp);
682 if (n->netdev_ops && n->netdev_ops->ndo_fcoe_ddp_done)
683 return n->netdev_ops->ndo_fcoe_ddp_done(n, xid);
687 static struct libfc_function_template fcoe_libfc_fcn_templ = {
688 .frame_send = fcoe_xmit,
689 .ddp_setup = fcoe_ddp_setup,
690 .ddp_done = fcoe_ddp_done,
694 * fcoe_if_create() - this function creates the fcoe port
695 * @fcoe: fcoe_interface structure to create an fc_lport instance on
696 * @parent: device pointer to be the parent in sysfs for the SCSI host
698 * Creates fc_lport struct and scsi_host for lport, configures lport.
700 * Returns : The allocated fc_lport or an error pointer
702 static struct fc_lport *fcoe_if_create(struct fcoe_interface *fcoe,
703 struct device *parent)
706 struct fc_lport *lport = NULL;
707 struct fcoe_port *port;
708 struct Scsi_Host *shost;
709 struct net_device *netdev = fcoe->netdev;
711 FCOE_NETDEV_DBG(netdev, "Create Interface\n");
713 shost = libfc_host_alloc(&fcoe_shost_template,
714 sizeof(struct fcoe_port));
716 FCOE_NETDEV_DBG(netdev, "Could not allocate host structure\n");
720 lport = shost_priv(shost);
721 port = lport_priv(lport);
724 /* configure fc_lport, e.g., em */
725 rc = fcoe_lport_config(lport);
727 FCOE_NETDEV_DBG(netdev, "Could not configure lport for the "
732 /* configure lport network properties */
733 rc = fcoe_netdev_config(lport, netdev);
735 FCOE_NETDEV_DBG(netdev, "Could not configure netdev for the "
740 /* configure lport scsi host properties */
741 rc = fcoe_shost_config(lport, shost, parent);
743 FCOE_NETDEV_DBG(netdev, "Could not configure shost for the "
748 /* Initialize the library */
749 rc = fcoe_libfc_config(lport, &fcoe_libfc_fcn_templ);
751 FCOE_NETDEV_DBG(netdev, "Could not configure libfc for the "
757 * fcoe_em_alloc() and fcoe_hostlist_add() both
758 * need to be atomic under fcoe_hostlist_lock
759 * since fcoe_em_alloc() looks for an existing EM
760 * instance on host list updated by fcoe_hostlist_add().
762 write_lock(&fcoe_hostlist_lock);
763 /* lport exch manager allocation */
764 rc = fcoe_em_config(lport);
766 FCOE_NETDEV_DBG(netdev, "Could not configure the EM for the "
771 /* add to lports list */
772 fcoe_hostlist_add(lport);
773 write_unlock(&fcoe_hostlist_lock);
776 fcoe_interface_get(fcoe);
780 fc_exch_mgr_free(lport);
782 scsi_host_put(lport->host);
788 * fcoe_if_init() - attach to scsi transport
790 * Returns : 0 on success
792 static int __init fcoe_if_init(void)
794 /* attach to scsi transport */
795 scsi_transport_fcoe_sw =
796 fc_attach_transport(&fcoe_transport_function);
798 if (!scsi_transport_fcoe_sw) {
799 printk(KERN_ERR "fcoe: Failed to attach to the FC transport\n");
807 * fcoe_if_exit() - detach from scsi transport
809 * Returns : 0 on success
811 int __exit fcoe_if_exit(void)
813 fc_release_transport(scsi_transport_fcoe_sw);
818 * fcoe_percpu_thread_create() - Create a receive thread for an online cpu
819 * @cpu: cpu index for the online cpu
821 static void fcoe_percpu_thread_create(unsigned int cpu)
823 struct fcoe_percpu_s *p;
824 struct task_struct *thread;
826 p = &per_cpu(fcoe_percpu, cpu);
828 thread = kthread_create(fcoe_percpu_receive_thread,
829 (void *)p, "fcoethread/%d", cpu);
831 if (likely(!IS_ERR(p->thread))) {
832 kthread_bind(thread, cpu);
833 wake_up_process(thread);
835 spin_lock_bh(&p->fcoe_rx_list.lock);
837 spin_unlock_bh(&p->fcoe_rx_list.lock);
842 * fcoe_percpu_thread_destroy() - removes the rx thread for the given cpu
843 * @cpu: cpu index the rx thread is to be removed
845 * Destroys a per-CPU Rx thread. Any pending skbs are moved to the
846 * current CPU's Rx thread. If the thread being destroyed is bound to
847 * the CPU processing this context the skbs will be freed.
849 static void fcoe_percpu_thread_destroy(unsigned int cpu)
851 struct fcoe_percpu_s *p;
852 struct task_struct *thread;
853 struct page *crc_eof;
856 struct fcoe_percpu_s *p0;
857 unsigned targ_cpu = smp_processor_id();
858 #endif /* CONFIG_SMP */
860 FCOE_DBG("Destroying receive thread for CPU %d\n", cpu);
862 /* Prevent any new skbs from being queued for this CPU. */
863 p = &per_cpu(fcoe_percpu, cpu);
864 spin_lock_bh(&p->fcoe_rx_list.lock);
867 crc_eof = p->crc_eof_page;
868 p->crc_eof_page = NULL;
869 p->crc_eof_offset = 0;
870 spin_unlock_bh(&p->fcoe_rx_list.lock);
874 * Don't bother moving the skb's if this context is running
875 * on the same CPU that is having its thread destroyed. This
876 * can easily happen when the module is removed.
878 if (cpu != targ_cpu) {
879 p0 = &per_cpu(fcoe_percpu, targ_cpu);
880 spin_lock_bh(&p0->fcoe_rx_list.lock);
882 FCOE_DBG("Moving frames from CPU %d to CPU %d\n",
885 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
886 __skb_queue_tail(&p0->fcoe_rx_list, skb);
887 spin_unlock_bh(&p0->fcoe_rx_list.lock);
890 * The targeted CPU is not initialized and cannot accept
891 * new skbs. Unlock the targeted CPU and drop the skbs
892 * on the CPU that is going offline.
894 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
896 spin_unlock_bh(&p0->fcoe_rx_list.lock);
900 * This scenario occurs when the module is being removed
901 * and all threads are being destroyed. skbs will continue
902 * to be shifted from the CPU thread that is being removed
903 * to the CPU thread associated with the CPU that is processing
904 * the module removal. Once there is only one CPU Rx thread it
905 * will reach this case and we will drop all skbs and later
908 spin_lock_bh(&p->fcoe_rx_list.lock);
909 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
911 spin_unlock_bh(&p->fcoe_rx_list.lock);
915 * This a non-SMP scenario where the singular Rx thread is
916 * being removed. Free all skbs and stop the thread.
918 spin_lock_bh(&p->fcoe_rx_list.lock);
919 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
921 spin_unlock_bh(&p->fcoe_rx_list.lock);
925 kthread_stop(thread);
932 * fcoe_cpu_callback() - fcoe cpu hotplug event callback
933 * @nfb: callback data block
934 * @action: event triggering the callback
935 * @hcpu: index for the cpu of this event
937 * This creates or destroys per cpu data for fcoe
939 * Returns NOTIFY_OK always.
941 static int fcoe_cpu_callback(struct notifier_block *nfb,
942 unsigned long action, void *hcpu)
944 unsigned cpu = (unsigned long)hcpu;
948 case CPU_ONLINE_FROZEN:
949 FCOE_DBG("CPU %x online: Create Rx thread\n", cpu);
950 fcoe_percpu_thread_create(cpu);
953 case CPU_DEAD_FROZEN:
954 FCOE_DBG("CPU %x offline: Remove Rx thread\n", cpu);
955 fcoe_percpu_thread_destroy(cpu);
963 static struct notifier_block fcoe_cpu_notifier = {
964 .notifier_call = fcoe_cpu_callback,
968 * fcoe_rcv() - this is the fcoe receive function called by NET_RX_SOFTIRQ
969 * @skb: the receive skb
970 * @dev: associated net device
972 * @olddev: last device
974 * this function will receive the packet and build fc frame and pass it up
976 * Returns: 0 for success
978 int fcoe_rcv(struct sk_buff *skb, struct net_device *dev,
979 struct packet_type *ptype, struct net_device *olddev)
982 struct fcoe_rcv_info *fr;
983 struct fcoe_interface *fcoe;
984 struct fc_frame_header *fh;
985 struct fcoe_percpu_s *fps;
988 fcoe = container_of(ptype, struct fcoe_interface, fcoe_packet_type);
990 if (unlikely(lp == NULL)) {
991 FCOE_NETDEV_DBG(dev, "Cannot find hba structure");
997 FCOE_NETDEV_DBG(dev, "skb_info: len:%d data_len:%d head:%p "
998 "data:%p tail:%p end:%p sum:%d dev:%s",
999 skb->len, skb->data_len, skb->head, skb->data,
1000 skb_tail_pointer(skb), skb_end_pointer(skb),
1001 skb->csum, skb->dev ? skb->dev->name : "<NULL>");
1003 /* check for FCOE packet type */
1004 if (unlikely(eth_hdr(skb)->h_proto != htons(ETH_P_FCOE))) {
1005 FCOE_NETDEV_DBG(dev, "Wrong FC type frame");
1010 * Check for minimum frame length, and make sure required FCoE
1011 * and FC headers are pulled into the linear data area.
1013 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
1014 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
1017 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
1018 fh = (struct fc_frame_header *) skb_transport_header(skb);
1020 fr = fcoe_dev_from_skb(skb);
1025 * In case the incoming frame's exchange is originated from
1026 * the initiator, then received frame's exchange id is ANDed
1027 * with fc_cpu_mask bits to get the same cpu on which exchange
1028 * was originated, otherwise just use the current cpu.
1030 if (ntoh24(fh->fh_f_ctl) & FC_FC_EX_CTX)
1031 cpu = ntohs(fh->fh_ox_id) & fc_cpu_mask;
1033 cpu = smp_processor_id();
1035 fps = &per_cpu(fcoe_percpu, cpu);
1036 spin_lock_bh(&fps->fcoe_rx_list.lock);
1037 if (unlikely(!fps->thread)) {
1039 * The targeted CPU is not ready, let's target
1040 * the first CPU now. For non-SMP systems this
1041 * will check the same CPU twice.
1043 FCOE_NETDEV_DBG(dev, "CPU is online, but no receive thread "
1044 "ready for incoming skb- using first online "
1047 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1048 cpu = first_cpu(cpu_online_map);
1049 fps = &per_cpu(fcoe_percpu, cpu);
1050 spin_lock_bh(&fps->fcoe_rx_list.lock);
1052 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1058 * We now have a valid CPU that we're targeting for
1059 * this skb. We also have this receive thread locked,
1060 * so we're free to queue skbs into it's queue.
1062 __skb_queue_tail(&fps->fcoe_rx_list, skb);
1063 if (fps->fcoe_rx_list.qlen == 1)
1064 wake_up_process(fps->thread);
1066 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1070 fc_lport_get_stats(lp)->ErrorFrames++;
1078 * fcoe_start_io() - pass to netdev to start xmit for fcoe
1079 * @skb: the skb to be xmitted
1081 * Returns: 0 for success
1083 static inline int fcoe_start_io(struct sk_buff *skb)
1088 rc = dev_queue_xmit(skb);
1096 * fcoe_get_paged_crc_eof() - in case we need to alloc a page for crc_eof
1097 * @skb: the skb to be xmitted
1100 * Returns: 0 for success
1102 static int fcoe_get_paged_crc_eof(struct sk_buff *skb, int tlen)
1104 struct fcoe_percpu_s *fps;
1107 fps = &get_cpu_var(fcoe_percpu);
1108 page = fps->crc_eof_page;
1110 page = alloc_page(GFP_ATOMIC);
1112 put_cpu_var(fcoe_percpu);
1115 fps->crc_eof_page = page;
1116 fps->crc_eof_offset = 0;
1120 skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags, page,
1121 fps->crc_eof_offset, tlen);
1123 skb->data_len += tlen;
1124 skb->truesize += tlen;
1125 fps->crc_eof_offset += sizeof(struct fcoe_crc_eof);
1127 if (fps->crc_eof_offset >= PAGE_SIZE) {
1128 fps->crc_eof_page = NULL;
1129 fps->crc_eof_offset = 0;
1132 put_cpu_var(fcoe_percpu);
1137 * fcoe_fc_crc() - calculates FC CRC in this fcoe skb
1138 * @fp: the fc_frame containing data to be checksummed
1140 * This uses crc32() to calculate the crc for port frame
1141 * Return : 32 bit crc
1143 u32 fcoe_fc_crc(struct fc_frame *fp)
1145 struct sk_buff *skb = fp_skb(fp);
1146 struct skb_frag_struct *frag;
1147 unsigned char *data;
1148 unsigned long off, len, clen;
1152 crc = crc32(~0, skb->data, skb_headlen(skb));
1154 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1155 frag = &skb_shinfo(skb)->frags[i];
1156 off = frag->page_offset;
1159 clen = min(len, PAGE_SIZE - (off & ~PAGE_MASK));
1160 data = kmap_atomic(frag->page + (off >> PAGE_SHIFT),
1161 KM_SKB_DATA_SOFTIRQ);
1162 crc = crc32(crc, data + (off & ~PAGE_MASK), clen);
1163 kunmap_atomic(data, KM_SKB_DATA_SOFTIRQ);
1172 * fcoe_xmit() - FCoE frame transmit function
1173 * @lp: the associated local fcoe
1174 * @fp: the fc_frame to be transmitted
1176 * Return : 0 for success
1178 int fcoe_xmit(struct fc_lport *lp, struct fc_frame *fp)
1183 struct fcoe_crc_eof *cp;
1184 struct sk_buff *skb;
1185 struct fcoe_dev_stats *stats;
1186 struct fc_frame_header *fh;
1187 unsigned int hlen; /* header length implies the version */
1188 unsigned int tlen; /* trailer length */
1189 unsigned int elen; /* eth header, may include vlan */
1190 struct fcoe_port *port = lport_priv(lp);
1191 struct fcoe_interface *fcoe = port->fcoe;
1193 struct fcoe_hdr *hp;
1195 WARN_ON((fr_len(fp) % sizeof(u32)) != 0);
1197 fh = fc_frame_header_get(fp);
1199 wlen = skb->len / FCOE_WORD_TO_BYTE;
1206 if (unlikely(fh->fh_r_ctl == FC_RCTL_ELS_REQ) &&
1207 fcoe_ctlr_els_send(&fcoe->ctlr, skb))
1213 elen = sizeof(struct ethhdr);
1214 hlen = sizeof(struct fcoe_hdr);
1215 tlen = sizeof(struct fcoe_crc_eof);
1216 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
1219 if (likely(lp->crc_offload)) {
1220 skb->ip_summed = CHECKSUM_PARTIAL;
1221 skb->csum_start = skb_headroom(skb);
1222 skb->csum_offset = skb->len;
1225 skb->ip_summed = CHECKSUM_NONE;
1226 crc = fcoe_fc_crc(fp);
1229 /* copy port crc and eof to the skb buff */
1230 if (skb_is_nonlinear(skb)) {
1232 if (fcoe_get_paged_crc_eof(skb, tlen)) {
1236 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
1237 cp = kmap_atomic(frag->page, KM_SKB_DATA_SOFTIRQ)
1238 + frag->page_offset;
1240 cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
1243 memset(cp, 0, sizeof(*cp));
1245 cp->fcoe_crc32 = cpu_to_le32(~crc);
1247 if (skb_is_nonlinear(skb)) {
1248 kunmap_atomic(cp, KM_SKB_DATA_SOFTIRQ);
1252 /* adjust skb network/transport offsets to match mac/fcoe/port */
1253 skb_push(skb, elen + hlen);
1254 skb_reset_mac_header(skb);
1255 skb_reset_network_header(skb);
1256 skb->mac_len = elen;
1257 skb->protocol = htons(ETH_P_FCOE);
1258 skb->dev = fcoe->netdev;
1260 /* fill up mac and fcoe headers */
1262 eh->h_proto = htons(ETH_P_FCOE);
1263 if (fcoe->ctlr.map_dest)
1264 fc_fcoe_set_mac(eh->h_dest, fh->fh_d_id);
1266 /* insert GW address */
1267 memcpy(eh->h_dest, fcoe->ctlr.dest_addr, ETH_ALEN);
1269 if (unlikely(fcoe->ctlr.flogi_oxid != FC_XID_UNKNOWN))
1270 memcpy(eh->h_source, fcoe->ctlr.ctl_src_addr, ETH_ALEN);
1272 memcpy(eh->h_source, fcoe->ctlr.data_src_addr, ETH_ALEN);
1274 hp = (struct fcoe_hdr *)(eh + 1);
1275 memset(hp, 0, sizeof(*hp));
1277 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
1280 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
1281 if (lp->seq_offload && fr_max_payload(fp)) {
1282 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
1283 skb_shinfo(skb)->gso_size = fr_max_payload(fp);
1285 skb_shinfo(skb)->gso_type = 0;
1286 skb_shinfo(skb)->gso_size = 0;
1288 /* update tx stats: regardless if LLD fails */
1289 stats = fc_lport_get_stats(lp);
1291 stats->TxWords += wlen;
1293 /* send down to lld */
1295 if (port->fcoe_pending_queue.qlen)
1296 fcoe_check_wait_queue(lp, skb);
1297 else if (fcoe_start_io(skb))
1298 fcoe_check_wait_queue(lp, skb);
1304 * fcoe_percpu_receive_thread() - recv thread per cpu
1305 * @arg: ptr to the fcoe per cpu struct
1307 * Return: 0 for success
1309 int fcoe_percpu_receive_thread(void *arg)
1311 struct fcoe_percpu_s *p = arg;
1313 struct fc_lport *lp;
1314 struct fcoe_rcv_info *fr;
1315 struct fcoe_dev_stats *stats;
1316 struct fc_frame_header *fh;
1317 struct sk_buff *skb;
1318 struct fcoe_crc_eof crc_eof;
1319 struct fc_frame *fp;
1321 struct fcoe_port *port;
1322 struct fcoe_hdr *hp;
1324 set_user_nice(current, -20);
1326 while (!kthread_should_stop()) {
1328 spin_lock_bh(&p->fcoe_rx_list.lock);
1329 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) == NULL) {
1330 set_current_state(TASK_INTERRUPTIBLE);
1331 spin_unlock_bh(&p->fcoe_rx_list.lock);
1333 set_current_state(TASK_RUNNING);
1334 if (kthread_should_stop())
1336 spin_lock_bh(&p->fcoe_rx_list.lock);
1338 spin_unlock_bh(&p->fcoe_rx_list.lock);
1339 fr = fcoe_dev_from_skb(skb);
1341 if (unlikely(lp == NULL)) {
1342 FCOE_NETDEV_DBG(skb->dev, "Invalid HBA Structure");
1347 FCOE_NETDEV_DBG(skb->dev, "skb_info: len:%d data_len:%d "
1348 "head:%p data:%p tail:%p end:%p sum:%d dev:%s",
1349 skb->len, skb->data_len,
1350 skb->head, skb->data, skb_tail_pointer(skb),
1351 skb_end_pointer(skb), skb->csum,
1352 skb->dev ? skb->dev->name : "<NULL>");
1355 * Save source MAC address before discarding header.
1357 port = lport_priv(lp);
1358 if (skb_is_nonlinear(skb))
1359 skb_linearize(skb); /* not ideal */
1360 mac = eth_hdr(skb)->h_source;
1363 * Frame length checks and setting up the header pointers
1364 * was done in fcoe_rcv already.
1366 hp = (struct fcoe_hdr *) skb_network_header(skb);
1367 fh = (struct fc_frame_header *) skb_transport_header(skb);
1369 stats = fc_lport_get_stats(lp);
1370 if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) {
1371 if (stats->ErrorFrames < 5)
1372 printk(KERN_WARNING "fcoe: FCoE version "
1373 "mismatch: The frame has "
1374 "version %x, but the "
1375 "initiator supports version "
1376 "%x\n", FC_FCOE_DECAPS_VER(hp),
1378 stats->ErrorFrames++;
1383 skb_pull(skb, sizeof(struct fcoe_hdr));
1384 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
1387 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
1389 fp = (struct fc_frame *)skb;
1392 fr_sof(fp) = hp->fcoe_sof;
1394 /* Copy out the CRC and EOF trailer for access */
1395 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof))) {
1399 fr_eof(fp) = crc_eof.fcoe_eof;
1400 fr_crc(fp) = crc_eof.fcoe_crc32;
1401 if (pskb_trim(skb, fr_len)) {
1407 * We only check CRC if no offload is available and if it is
1408 * it's solicited data, in which case, the FCP layer would
1409 * check it during the copy.
1411 if (lp->crc_offload && skb->ip_summed == CHECKSUM_UNNECESSARY)
1412 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1414 fr_flags(fp) |= FCPHF_CRC_UNCHECKED;
1416 fh = fc_frame_header_get(fp);
1417 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA &&
1418 fh->fh_type == FC_TYPE_FCP) {
1419 fc_exch_recv(lp, fp);
1422 if (fr_flags(fp) & FCPHF_CRC_UNCHECKED) {
1423 if (le32_to_cpu(fr_crc(fp)) !=
1424 ~crc32(~0, skb->data, fr_len)) {
1425 if (stats->InvalidCRCCount < 5)
1426 printk(KERN_WARNING "fcoe: dropping "
1427 "frame with CRC error\n");
1428 stats->InvalidCRCCount++;
1429 stats->ErrorFrames++;
1433 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1435 if (unlikely(port->fcoe->ctlr.flogi_oxid != FC_XID_UNKNOWN) &&
1436 fcoe_ctlr_recv_flogi(&port->fcoe->ctlr, fp, mac)) {
1440 fc_exch_recv(lp, fp);
1446 * fcoe_check_wait_queue() - attempt to clear the transmit backlog
1449 * This empties the wait_queue, dequeue the head of the wait_queue queue
1450 * and calls fcoe_start_io() for each packet, if all skb have been
1451 * transmitted, return qlen or -1 if a error occurs, then restore
1452 * wait_queue and try again later.
1454 * The wait_queue is used when the skb transmit fails. skb will go
1455 * in the wait_queue which will be emptied by the timer function or
1456 * by the next skb transmit.
1458 static void fcoe_check_wait_queue(struct fc_lport *lp, struct sk_buff *skb)
1460 struct fcoe_port *port = lport_priv(lp);
1463 spin_lock_bh(&port->fcoe_pending_queue.lock);
1466 __skb_queue_tail(&port->fcoe_pending_queue, skb);
1468 if (port->fcoe_pending_queue_active)
1470 port->fcoe_pending_queue_active = 1;
1472 while (port->fcoe_pending_queue.qlen) {
1473 /* keep qlen > 0 until fcoe_start_io succeeds */
1474 port->fcoe_pending_queue.qlen++;
1475 skb = __skb_dequeue(&port->fcoe_pending_queue);
1477 spin_unlock_bh(&port->fcoe_pending_queue.lock);
1478 rc = fcoe_start_io(skb);
1479 spin_lock_bh(&port->fcoe_pending_queue.lock);
1482 __skb_queue_head(&port->fcoe_pending_queue, skb);
1483 /* undo temporary increment above */
1484 port->fcoe_pending_queue.qlen--;
1487 /* undo temporary increment above */
1488 port->fcoe_pending_queue.qlen--;
1491 if (port->fcoe_pending_queue.qlen < FCOE_LOW_QUEUE_DEPTH)
1493 if (port->fcoe_pending_queue.qlen && !timer_pending(&port->timer))
1494 mod_timer(&port->timer, jiffies + 2);
1495 port->fcoe_pending_queue_active = 0;
1497 if (port->fcoe_pending_queue.qlen > FCOE_MAX_QUEUE_DEPTH)
1499 spin_unlock_bh(&port->fcoe_pending_queue.lock);
1504 * fcoe_dev_setup() - setup link change notification interface
1506 static void fcoe_dev_setup(void)
1508 register_netdevice_notifier(&fcoe_notifier);
1512 * fcoe_dev_cleanup() - cleanup link change notification interface
1514 static void fcoe_dev_cleanup(void)
1516 unregister_netdevice_notifier(&fcoe_notifier);
1520 * fcoe_device_notification() - netdev event notification callback
1521 * @notifier: context of the notification
1522 * @event: type of event
1523 * @ptr: fixed array for output parsed ifname
1525 * This function is called by the ethernet driver in case of link change event
1527 * Returns: 0 for success
1529 static int fcoe_device_notification(struct notifier_block *notifier,
1530 ulong event, void *ptr)
1532 struct fc_lport *lp = NULL;
1533 struct net_device *netdev = ptr;
1534 struct fcoe_interface *fcoe;
1535 struct fcoe_dev_stats *stats;
1536 u32 link_possible = 1;
1540 read_lock(&fcoe_hostlist_lock);
1541 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
1542 if (fcoe->netdev == netdev) {
1547 read_unlock(&fcoe_hostlist_lock);
1555 case NETDEV_GOING_DOWN:
1561 case NETDEV_CHANGEMTU:
1562 mfs = netdev->mtu - (sizeof(struct fcoe_hdr) +
1563 sizeof(struct fcoe_crc_eof));
1564 if (mfs >= FC_MIN_MAX_FRAME)
1565 fc_set_mfs(lp, mfs);
1567 case NETDEV_REGISTER:
1570 FCOE_NETDEV_DBG(netdev, "Unknown event %ld "
1571 "from netdev netlink\n", event);
1573 if (link_possible && !fcoe_link_ok(lp))
1574 fcoe_ctlr_link_up(&fcoe->ctlr);
1575 else if (fcoe_ctlr_link_down(&fcoe->ctlr)) {
1576 stats = fc_lport_get_stats(lp);
1577 stats->LinkFailureCount++;
1578 fcoe_clean_pending_queue(lp);
1585 * fcoe_if_to_netdev() - parse a name buffer to get netdev
1586 * @buffer: incoming buffer to be copied
1588 * Returns: NULL or ptr to net_device
1590 static struct net_device *fcoe_if_to_netdev(const char *buffer)
1593 char ifname[IFNAMSIZ + 2];
1596 strlcpy(ifname, buffer, IFNAMSIZ);
1597 cp = ifname + strlen(ifname);
1598 while (--cp >= ifname && *cp == '\n')
1600 return dev_get_by_name(&init_net, ifname);
1606 * fcoe_netdev_to_module_owner() - finds out the driver module of the netdev
1607 * @netdev: the target netdev
1609 * Returns: ptr to the struct module, NULL for failure
1611 static struct module *
1612 fcoe_netdev_to_module_owner(const struct net_device *netdev)
1619 dev = netdev->dev.parent;
1626 return dev->driver->owner;
1630 * fcoe_ethdrv_get() - Hold the Ethernet driver
1631 * @netdev: the target netdev
1633 * Holds the Ethernet driver module by try_module_get() for
1634 * the corresponding netdev.
1636 * Returns: 0 for success
1638 static int fcoe_ethdrv_get(const struct net_device *netdev)
1640 struct module *owner;
1642 owner = fcoe_netdev_to_module_owner(netdev);
1644 FCOE_NETDEV_DBG(netdev, "Hold driver module %s\n",
1645 module_name(owner));
1646 return try_module_get(owner);
1652 * fcoe_ethdrv_put() - Release the Ethernet driver
1653 * @netdev: the target netdev
1655 * Releases the Ethernet driver module by module_put for
1656 * the corresponding netdev.
1658 * Returns: 0 for success
1660 static int fcoe_ethdrv_put(const struct net_device *netdev)
1662 struct module *owner;
1664 owner = fcoe_netdev_to_module_owner(netdev);
1666 FCOE_NETDEV_DBG(netdev, "Release driver module %s\n",
1667 module_name(owner));
1675 * fcoe_destroy() - handles the destroy from sysfs
1676 * @buffer: expected to be an eth if name
1677 * @kp: associated kernel param
1679 * Returns: 0 for success
1681 static int fcoe_destroy(const char *buffer, struct kernel_param *kp)
1683 struct net_device *netdev;
1684 struct fcoe_interface *fcoe;
1685 struct fcoe_port *port;
1686 struct fc_lport *lport;
1689 netdev = fcoe_if_to_netdev(buffer);
1694 /* look for existing lport */
1695 lport = fcoe_hostlist_lookup(netdev);
1700 port = lport_priv(lport);
1702 fcoe_if_destroy(lport);
1703 fcoe_ethdrv_put(netdev);
1712 * fcoe_create() - Handles the create call from sysfs
1713 * @buffer: expected to be an eth if name
1714 * @kp: associated kernel param
1716 * Returns: 0 for success
1718 static int fcoe_create(const char *buffer, struct kernel_param *kp)
1721 struct fcoe_interface *fcoe;
1722 struct fc_lport *lport;
1723 struct net_device *netdev;
1725 netdev = fcoe_if_to_netdev(buffer);
1730 /* look for existing lport */
1731 if (fcoe_hostlist_lookup(netdev)) {
1735 fcoe_ethdrv_get(netdev);
1737 fcoe = fcoe_interface_create(netdev);
1743 lport = fcoe_if_create(fcoe, &netdev->dev);
1744 if (IS_ERR(lport)) {
1745 printk(KERN_ERR "fcoe: Failed to create interface (%s)\n",
1747 fcoe_ethdrv_put(netdev);
1752 /* Make this the "master" N_Port */
1753 fcoe->ctlr.lp = lport;
1755 /* start FIP Discovery and FLOGI */
1756 lport->boot_time = jiffies;
1757 fc_fabric_login(lport);
1758 if (!fcoe_link_ok(lport))
1759 fcoe_ctlr_link_up(&fcoe->ctlr);
1764 * Release from init in fcoe_interface_create(), on success lport
1765 * should be holding a reference taken in fcoe_if_create().
1767 fcoe_interface_put(fcoe);
1774 module_param_call(create, fcoe_create, NULL, NULL, S_IWUSR);
1775 __MODULE_PARM_TYPE(create, "string");
1776 MODULE_PARM_DESC(create, "Create fcoe fcoe using net device passed in.");
1777 module_param_call(destroy, fcoe_destroy, NULL, NULL, S_IWUSR);
1778 __MODULE_PARM_TYPE(destroy, "string");
1779 MODULE_PARM_DESC(destroy, "Destroy fcoe fcoe");
1782 * fcoe_link_ok() - Check if link is ok for the fc_lport
1783 * @lp: ptr to the fc_lport
1785 * Any permanently-disqualifying conditions have been previously checked.
1786 * This also updates the speed setting, which may change with link for 100/1000.
1788 * This function should probably be checking for PAUSE support at some point
1789 * in the future. Currently Per-priority-pause is not determinable using
1790 * ethtool, so we shouldn't be restrictive until that problem is resolved.
1792 * Returns: 0 if link is OK for use by FCoE.
1795 int fcoe_link_ok(struct fc_lport *lp)
1797 struct fcoe_port *port = lport_priv(lp);
1798 struct net_device *dev = port->fcoe->netdev;
1799 struct ethtool_cmd ecmd = { ETHTOOL_GSET };
1801 if ((dev->flags & IFF_UP) && netif_carrier_ok(dev) &&
1802 (!dev_ethtool_get_settings(dev, &ecmd))) {
1803 lp->link_supported_speeds &=
1804 ~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
1805 if (ecmd.supported & (SUPPORTED_1000baseT_Half |
1806 SUPPORTED_1000baseT_Full))
1807 lp->link_supported_speeds |= FC_PORTSPEED_1GBIT;
1808 if (ecmd.supported & SUPPORTED_10000baseT_Full)
1809 lp->link_supported_speeds |=
1810 FC_PORTSPEED_10GBIT;
1811 if (ecmd.speed == SPEED_1000)
1812 lp->link_speed = FC_PORTSPEED_1GBIT;
1813 if (ecmd.speed == SPEED_10000)
1814 lp->link_speed = FC_PORTSPEED_10GBIT;
1822 * fcoe_percpu_clean() - Clear the pending skbs for an lport
1825 void fcoe_percpu_clean(struct fc_lport *lp)
1827 struct fcoe_percpu_s *pp;
1828 struct fcoe_rcv_info *fr;
1829 struct sk_buff_head *list;
1830 struct sk_buff *skb, *next;
1831 struct sk_buff *head;
1834 for_each_possible_cpu(cpu) {
1835 pp = &per_cpu(fcoe_percpu, cpu);
1836 spin_lock_bh(&pp->fcoe_rx_list.lock);
1837 list = &pp->fcoe_rx_list;
1839 for (skb = head; skb != (struct sk_buff *)list;
1842 fr = fcoe_dev_from_skb(skb);
1843 if (fr->fr_dev == lp) {
1844 __skb_unlink(skb, list);
1848 spin_unlock_bh(&pp->fcoe_rx_list.lock);
1853 * fcoe_clean_pending_queue() - Dequeue a skb and free it
1854 * @lp: the corresponding fc_lport
1858 void fcoe_clean_pending_queue(struct fc_lport *lp)
1860 struct fcoe_port *port = lport_priv(lp);
1861 struct sk_buff *skb;
1863 spin_lock_bh(&port->fcoe_pending_queue.lock);
1864 while ((skb = __skb_dequeue(&port->fcoe_pending_queue)) != NULL) {
1865 spin_unlock_bh(&port->fcoe_pending_queue.lock);
1867 spin_lock_bh(&port->fcoe_pending_queue.lock);
1869 spin_unlock_bh(&port->fcoe_pending_queue.lock);
1873 * fcoe_reset() - Resets the fcoe
1874 * @shost: shost the reset is from
1878 int fcoe_reset(struct Scsi_Host *shost)
1880 struct fc_lport *lport = shost_priv(shost);
1881 fc_lport_reset(lport);
1886 * fcoe_hostlist_lookup_port() - find the corresponding lport by a given device
1887 * @dev: this is currently ptr to net_device
1889 * Called with fcoe_hostlist_lock held.
1891 * Returns: NULL or the located fcoe_port
1893 static struct fcoe_interface *
1894 fcoe_hostlist_lookup_port(const struct net_device *dev)
1896 struct fcoe_interface *fcoe;
1898 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
1899 if (fcoe->netdev == dev)
1906 * fcoe_hostlist_lookup() - Find the corresponding lport by netdev
1907 * @netdev: ptr to net_device
1909 * Returns: 0 for success
1911 struct fc_lport *fcoe_hostlist_lookup(const struct net_device *netdev)
1913 struct fcoe_interface *fcoe;
1915 read_lock(&fcoe_hostlist_lock);
1916 fcoe = fcoe_hostlist_lookup_port(netdev);
1917 read_unlock(&fcoe_hostlist_lock);
1919 return (fcoe) ? fcoe->ctlr.lp : NULL;
1923 * fcoe_hostlist_add() - Add a lport to lports list
1924 * @lp: ptr to the fc_lport to be added
1926 * Called with write fcoe_hostlist_lock held.
1928 * Returns: 0 for success
1930 int fcoe_hostlist_add(const struct fc_lport *lport)
1932 struct fcoe_interface *fcoe;
1933 struct fcoe_port *port;
1935 fcoe = fcoe_hostlist_lookup_port(fcoe_netdev(lport));
1937 port = lport_priv(lport);
1939 list_add_tail(&fcoe->list, &fcoe_hostlist);
1945 * fcoe_hostlist_remove() - remove a lport from lports list
1946 * @lp: ptr to the fc_lport to be removed
1948 * Returns: 0 for success
1950 int fcoe_hostlist_remove(const struct fc_lport *lport)
1952 struct fcoe_interface *fcoe;
1954 write_lock_bh(&fcoe_hostlist_lock);
1955 fcoe = fcoe_hostlist_lookup_port(fcoe_netdev(lport));
1957 list_del(&fcoe->list);
1958 write_unlock_bh(&fcoe_hostlist_lock);
1964 * fcoe_init() - fcoe module loading initialization
1966 * Returns 0 on success, negative on failure
1968 static int __init fcoe_init(void)
1972 struct fcoe_percpu_s *p;
1974 for_each_possible_cpu(cpu) {
1975 p = &per_cpu(fcoe_percpu, cpu);
1976 skb_queue_head_init(&p->fcoe_rx_list);
1979 for_each_online_cpu(cpu)
1980 fcoe_percpu_thread_create(cpu);
1982 /* Initialize per CPU interrupt thread */
1983 rc = register_hotcpu_notifier(&fcoe_cpu_notifier);
1987 /* Setup link change notification */
1990 rc = fcoe_if_init();
1997 for_each_online_cpu(cpu) {
1998 fcoe_percpu_thread_destroy(cpu);
2003 module_init(fcoe_init);
2006 * fcoe_exit() - fcoe module unloading cleanup
2008 * Returns 0 on success, negative on failure
2010 static void __exit fcoe_exit(void)
2013 struct fcoe_interface *fcoe, *tmp;
2017 /* releases the associated fcoe hosts */
2018 list_for_each_entry_safe(fcoe, tmp, &fcoe_hostlist, list)
2019 fcoe_if_destroy(fcoe->ctlr.lp);
2021 unregister_hotcpu_notifier(&fcoe_cpu_notifier);
2023 for_each_online_cpu(cpu)
2024 fcoe_percpu_thread_destroy(cpu);
2026 /* detach from scsi transport */
2029 module_exit(fcoe_exit);