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).");
58 DEFINE_MUTEX(fcoe_config_mutex);
61 LIST_HEAD(fcoe_hostlist);
62 DEFINE_RWLOCK(fcoe_hostlist_lock);
63 DEFINE_PER_CPU(struct fcoe_percpu_s, fcoe_percpu);
65 /* Function Prototypes */
66 static int fcoe_reset(struct Scsi_Host *shost);
67 static int fcoe_xmit(struct fc_lport *, struct fc_frame *);
68 static int fcoe_rcv(struct sk_buff *, struct net_device *,
69 struct packet_type *, struct net_device *);
70 static int fcoe_percpu_receive_thread(void *arg);
71 static void fcoe_clean_pending_queue(struct fc_lport *lp);
72 static void fcoe_percpu_clean(struct fc_lport *lp);
73 static int fcoe_link_ok(struct fc_lport *lp);
75 static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *);
76 static int fcoe_hostlist_add(const struct fc_lport *);
77 static int fcoe_hostlist_remove(const struct fc_lport *);
79 static void fcoe_check_wait_queue(struct fc_lport *, struct sk_buff *);
80 static int fcoe_device_notification(struct notifier_block *, ulong, void *);
81 static void fcoe_dev_setup(void);
82 static void fcoe_dev_cleanup(void);
84 /* notification function from net device */
85 static struct notifier_block fcoe_notifier = {
86 .notifier_call = fcoe_device_notification,
89 static struct scsi_transport_template *scsi_transport_fcoe_sw;
91 struct fc_function_template fcoe_transport_function = {
92 .show_host_node_name = 1,
93 .show_host_port_name = 1,
94 .show_host_supported_classes = 1,
95 .show_host_supported_fc4s = 1,
96 .show_host_active_fc4s = 1,
97 .show_host_maxframe_size = 1,
99 .show_host_port_id = 1,
100 .show_host_supported_speeds = 1,
101 .get_host_speed = fc_get_host_speed,
102 .show_host_speed = 1,
103 .show_host_port_type = 1,
104 .get_host_port_state = fc_get_host_port_state,
105 .show_host_port_state = 1,
106 .show_host_symbolic_name = 1,
108 .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
109 .show_rport_maxframe_size = 1,
110 .show_rport_supported_classes = 1,
112 .show_host_fabric_name = 1,
113 .show_starget_node_name = 1,
114 .show_starget_port_name = 1,
115 .show_starget_port_id = 1,
116 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
117 .show_rport_dev_loss_tmo = 1,
118 .get_fc_host_stats = fc_get_host_stats,
119 .issue_fc_host_lip = fcoe_reset,
121 .terminate_rport_io = fc_rport_terminate_io,
124 static struct scsi_host_template fcoe_shost_template = {
125 .module = THIS_MODULE,
126 .name = "FCoE Driver",
127 .proc_name = FCOE_NAME,
128 .queuecommand = fc_queuecommand,
129 .eh_abort_handler = fc_eh_abort,
130 .eh_device_reset_handler = fc_eh_device_reset,
131 .eh_host_reset_handler = fc_eh_host_reset,
132 .slave_alloc = fc_slave_alloc,
133 .change_queue_depth = fc_change_queue_depth,
134 .change_queue_type = fc_change_queue_type,
137 .can_queue = FCOE_MAX_OUTSTANDING_COMMANDS,
138 .use_clustering = ENABLE_CLUSTERING,
139 .sg_tablesize = SG_ALL,
140 .max_sectors = 0xffff,
143 static int fcoe_fip_recv(struct sk_buff *skb, struct net_device *dev,
144 struct packet_type *ptype,
145 struct net_device *orig_dev);
147 * fcoe_interface_setup()
148 * @fcoe: new fcoe_interface
149 * @netdev : ptr to the associated netdevice struct
151 * Returns : 0 for success
153 static int fcoe_interface_setup(struct fcoe_interface *fcoe,
154 struct net_device *netdev)
156 struct fcoe_ctlr *fip = &fcoe->ctlr;
157 struct netdev_hw_addr *ha;
158 u8 flogi_maddr[ETH_ALEN];
160 fcoe->netdev = netdev;
162 /* Do not support for bonding device */
163 if ((netdev->priv_flags & IFF_MASTER_ALB) ||
164 (netdev->priv_flags & IFF_SLAVE_INACTIVE) ||
165 (netdev->priv_flags & IFF_MASTER_8023AD)) {
169 /* look for SAN MAC address, if multiple SAN MACs exist, only
170 * use the first one for SPMA */
172 for_each_dev_addr(netdev, ha) {
173 if ((ha->type == NETDEV_HW_ADDR_T_SAN) &&
174 (is_valid_ether_addr(fip->ctl_src_addr))) {
175 memcpy(fip->ctl_src_addr, ha->addr, ETH_ALEN);
182 /* setup Source Mac Address */
184 memcpy(fip->ctl_src_addr, netdev->dev_addr, netdev->addr_len);
187 * Add FCoE MAC address as second unicast MAC address
188 * or enter promiscuous mode if not capable of listening
189 * for multiple unicast MACs.
192 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
193 dev_unicast_add(netdev, flogi_maddr);
195 dev_unicast_add(netdev, fip->ctl_src_addr);
196 dev_mc_add(netdev, FIP_ALL_ENODE_MACS, ETH_ALEN, 0);
200 * setup the receive function from ethernet driver
201 * on the ethertype for the given device
203 fcoe->fcoe_packet_type.func = fcoe_rcv;
204 fcoe->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
205 fcoe->fcoe_packet_type.dev = netdev;
206 dev_add_pack(&fcoe->fcoe_packet_type);
208 fcoe->fip_packet_type.func = fcoe_fip_recv;
209 fcoe->fip_packet_type.type = htons(ETH_P_FIP);
210 fcoe->fip_packet_type.dev = netdev;
211 dev_add_pack(&fcoe->fip_packet_type);
216 static void fcoe_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb);
217 static void fcoe_update_src_mac(struct fcoe_ctlr *fip, u8 *old, u8 *new);
220 * fcoe_interface_create()
221 * @netdev: network interface
223 * Returns: pointer to a struct fcoe_interface or NULL on error
225 static struct fcoe_interface *fcoe_interface_create(struct net_device *netdev)
227 struct fcoe_interface *fcoe;
229 fcoe = kzalloc(sizeof(*fcoe), GFP_KERNEL);
231 FCOE_NETDEV_DBG(netdev, "Could not allocate fcoe structure\n");
235 kref_init(&fcoe->kref);
240 fcoe_ctlr_init(&fcoe->ctlr);
241 fcoe->ctlr.send = fcoe_fip_send;
242 fcoe->ctlr.update_mac = fcoe_update_src_mac;
244 fcoe_interface_setup(fcoe, netdev);
250 * fcoe_interface_cleanup() - clean up netdev configurations
253 void fcoe_interface_cleanup(struct fcoe_interface *fcoe)
255 struct net_device *netdev = fcoe->netdev;
256 struct fcoe_ctlr *fip = &fcoe->ctlr;
257 u8 flogi_maddr[ETH_ALEN];
260 * Don't listen for Ethernet packets anymore.
261 * synchronize_net() ensures that the packet handlers are not running
262 * on another CPU. dev_remove_pack() would do that, this calls the
263 * unsyncronized version __dev_remove_pack() to avoid multiple delays.
265 __dev_remove_pack(&fcoe->fcoe_packet_type);
266 __dev_remove_pack(&fcoe->fip_packet_type);
269 /* tear-down the FCoE controller */
270 fcoe_ctlr_destroy(&fcoe->ctlr);
272 /* Delete secondary MAC addresses */
274 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
275 dev_unicast_delete(netdev, flogi_maddr);
276 if (!is_zero_ether_addr(fip->data_src_addr))
277 dev_unicast_delete(netdev, fip->data_src_addr);
279 dev_unicast_delete(netdev, fip->ctl_src_addr);
280 dev_mc_delete(netdev, FIP_ALL_ENODE_MACS, ETH_ALEN, 0);
285 * fcoe_interface_release() - fcoe_port kref release function
286 * @kref: embedded reference count in an fcoe_interface struct
288 static void fcoe_interface_release(struct kref *kref)
290 struct fcoe_interface *fcoe;
292 fcoe = container_of(kref, struct fcoe_interface, kref);
293 fcoe_interface_cleanup(fcoe);
298 * fcoe_interface_get()
301 static inline void fcoe_interface_get(struct fcoe_interface *fcoe)
303 kref_get(&fcoe->kref);
307 * fcoe_interface_put()
310 static inline void fcoe_interface_put(struct fcoe_interface *fcoe)
312 kref_put(&fcoe->kref, fcoe_interface_release);
316 * fcoe_fip_recv - handle a received FIP frame.
317 * @skb: the receive skb
318 * @dev: associated &net_device
319 * @ptype: the &packet_type structure which was used to register this handler.
320 * @orig_dev: original receive &net_device, in case @dev is a bond.
322 * Returns: 0 for success
324 static int fcoe_fip_recv(struct sk_buff *skb, struct net_device *dev,
325 struct packet_type *ptype,
326 struct net_device *orig_dev)
328 struct fcoe_interface *fcoe;
330 fcoe = container_of(ptype, struct fcoe_interface, fip_packet_type);
331 fcoe_ctlr_recv(&fcoe->ctlr, skb);
336 * fcoe_fip_send() - send an Ethernet-encapsulated FIP frame.
337 * @fip: FCoE controller.
340 static void fcoe_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
342 skb->dev = fcoe_from_ctlr(fip)->netdev;
347 * fcoe_update_src_mac() - Update Ethernet MAC filters.
348 * @fip: FCoE controller.
349 * @old: Unicast MAC address to delete if the MAC is non-zero.
350 * @new: Unicast MAC address to add.
352 * Remove any previously-set unicast MAC filter.
353 * Add secondary FCoE MAC address filter for our OUI.
355 static void fcoe_update_src_mac(struct fcoe_ctlr *fip, u8 *old, u8 *new)
357 struct fcoe_interface *fcoe;
359 fcoe = fcoe_from_ctlr(fip);
361 if (!is_zero_ether_addr(old))
362 dev_unicast_delete(fcoe->netdev, old);
363 dev_unicast_add(fcoe->netdev, new);
368 * fcoe_lport_config() - sets up the fc_lport
369 * @lp: ptr to the fc_lport
371 * Returns: 0 for success
373 static int fcoe_lport_config(struct fc_lport *lp)
377 lp->max_retry_count = 3;
378 lp->max_rport_retry_count = 3;
379 lp->e_d_tov = 2 * 1000; /* FC-FS default */
380 lp->r_a_tov = 2 * 2 * 1000;
381 lp->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
382 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
384 fc_lport_init_stats(lp);
386 /* lport fc_lport related configuration */
389 /* offload related configuration */
400 * fcoe_queue_timer() - fcoe queue timer
401 * @lp: the fc_lport pointer
403 * Calls fcoe_check_wait_queue on timeout
406 static void fcoe_queue_timer(ulong lp)
408 fcoe_check_wait_queue((struct fc_lport *)lp, NULL);
412 * fcoe_netdev_config() - Set up netdev for SW FCoE
413 * @lp : ptr to the fc_lport
414 * @netdev : ptr to the associated netdevice struct
416 * Must be called after fcoe_lport_config() as it will use lport mutex
418 * Returns : 0 for success
420 static int fcoe_netdev_config(struct fc_lport *lp, struct net_device *netdev)
424 struct fcoe_interface *fcoe;
425 struct fcoe_port *port;
427 /* Setup lport private data to point to fcoe softc */
428 port = lport_priv(lp);
432 * Determine max frame size based on underlying device and optional
433 * user-configured limit. If the MFS is too low, fcoe_link_ok()
434 * will return 0, so do this first.
436 mfs = netdev->mtu - (sizeof(struct fcoe_hdr) +
437 sizeof(struct fcoe_crc_eof));
438 if (fc_set_mfs(lp, mfs))
441 /* offload features support */
442 if (netdev->features & NETIF_F_SG)
445 if (netdev->features & NETIF_F_FCOE_CRC) {
447 FCOE_NETDEV_DBG(netdev, "Supports FCCRC offload\n");
449 if (netdev->features & NETIF_F_FSO) {
451 lp->lso_max = netdev->gso_max_size;
452 FCOE_NETDEV_DBG(netdev, "Supports LSO for max len 0x%x\n",
455 if (netdev->fcoe_ddp_xid) {
457 lp->lro_xid = netdev->fcoe_ddp_xid;
458 FCOE_NETDEV_DBG(netdev, "Supports LRO for max xid 0x%x\n",
461 skb_queue_head_init(&port->fcoe_pending_queue);
462 port->fcoe_pending_queue_active = 0;
463 setup_timer(&port->timer, fcoe_queue_timer, (unsigned long)lp);
465 wwnn = fcoe_wwn_from_mac(netdev->dev_addr, 1, 0);
466 fc_set_wwnn(lp, wwnn);
467 /* XXX - 3rd arg needs to be vlan id */
468 wwpn = fcoe_wwn_from_mac(netdev->dev_addr, 2, 0);
469 fc_set_wwpn(lp, wwpn);
475 * fcoe_shost_config() - Sets up fc_lport->host
476 * @lp : ptr to the fc_lport
477 * @shost : ptr to the associated scsi host
478 * @dev : device associated to scsi host
480 * Must be called after fcoe_lport_config() and fcoe_netdev_config()
482 * Returns : 0 for success
484 static int fcoe_shost_config(struct fc_lport *lp, struct Scsi_Host *shost,
489 /* lport scsi host config */
492 lp->host->max_lun = FCOE_MAX_LUN;
493 lp->host->max_id = FCOE_MAX_FCP_TARGET;
494 lp->host->max_channel = 0;
495 lp->host->transportt = scsi_transport_fcoe_sw;
497 /* add the new host to the SCSI-ml */
498 rc = scsi_add_host(lp->host, dev);
500 FCOE_NETDEV_DBG(fcoe_netdev(lp), "fcoe_shost_config: "
501 "error on scsi_add_host\n");
504 sprintf(fc_host_symbolic_name(lp->host), "%s v%s over %s",
505 FCOE_NAME, FCOE_VERSION,
506 fcoe_netdev(lp)->name);
512 * fcoe_oem_match() - match for read types IO
513 * @fp: the fc_frame for new IO.
515 * Returns : true for read types IO, otherwise returns false.
517 bool fcoe_oem_match(struct fc_frame *fp)
519 return fc_fcp_is_read(fr_fsp(fp)) &&
520 (fr_fsp(fp)->data_len > fcoe_ddp_min);
524 * fcoe_em_config() - allocates em for this lport
525 * @lp: the fcoe that em is to allocated for
527 * Called with write fcoe_hostlist_lock held.
529 * Returns : 0 on success
531 static inline int fcoe_em_config(struct fc_lport *lp)
533 struct fcoe_port *port = lport_priv(lp);
534 struct fcoe_interface *fcoe = port->fcoe;
535 struct fcoe_interface *oldfcoe = NULL;
536 struct net_device *old_real_dev, *cur_real_dev;
537 u16 min_xid = FCOE_MIN_XID;
538 u16 max_xid = FCOE_MAX_XID;
541 * Check if need to allocate an em instance for
542 * offload exchange ids to be shared across all VN_PORTs/lport.
544 if (!lp->lro_enabled || !lp->lro_xid || (lp->lro_xid >= max_xid)) {
550 * Reuse existing offload em instance in case
551 * it is already allocated on real eth device
553 if (fcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
554 cur_real_dev = vlan_dev_real_dev(fcoe->netdev);
556 cur_real_dev = fcoe->netdev;
558 list_for_each_entry(oldfcoe, &fcoe_hostlist, list) {
559 if (oldfcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
560 old_real_dev = vlan_dev_real_dev(oldfcoe->netdev);
562 old_real_dev = oldfcoe->netdev;
564 if (cur_real_dev == old_real_dev) {
565 fcoe->oem = oldfcoe->oem;
571 if (!fc_exch_mgr_add(lp, fcoe->oem, fcoe_oem_match)) {
572 printk(KERN_ERR "fcoe_em_config: failed to add "
573 "offload em:%p on interface:%s\n",
574 fcoe->oem, fcoe->netdev->name);
578 fcoe->oem = fc_exch_mgr_alloc(lp, FC_CLASS_3,
579 FCOE_MIN_XID, lp->lro_xid,
582 printk(KERN_ERR "fcoe_em_config: failed to allocate "
583 "em for offload exches on interface:%s\n",
590 * Exclude offload EM xid range from next EM xid range.
592 min_xid += lp->lro_xid + 1;
595 if (!fc_exch_mgr_alloc(lp, FC_CLASS_3, min_xid, max_xid, NULL)) {
596 printk(KERN_ERR "fcoe_em_config: failed to "
597 "allocate em on interface %s\n", fcoe->netdev->name);
605 * fcoe_if_destroy() - FCoE software HBA tear-down function
606 * @lport: fc_lport to destroy
608 static void fcoe_if_destroy(struct fc_lport *lport)
610 struct fcoe_port *port = lport_priv(lport);
611 struct fcoe_interface *fcoe = port->fcoe;
612 struct net_device *netdev = fcoe->netdev;
614 FCOE_NETDEV_DBG(netdev, "Destroying interface\n");
616 /* Logout of the fabric */
617 fc_fabric_logoff(lport);
619 /* Cleanup the fc_lport */
620 fc_lport_destroy(lport);
621 fc_fcp_destroy(lport);
623 /* Stop the transmit retry timer */
624 del_timer_sync(&port->timer);
626 /* Free existing transmit skbs */
627 fcoe_clean_pending_queue(lport);
629 /* receives may not be stopped until after this */
630 fcoe_interface_put(fcoe);
632 /* Free queued packets for the per-CPU receive threads */
633 fcoe_percpu_clean(lport);
635 /* Detach from the scsi-ml */
636 fc_remove_host(lport->host);
637 scsi_remove_host(lport->host);
639 /* There are no more rports or I/O, free the EM */
640 fc_exch_mgr_free(lport);
642 /* Free memory used by statistical counters */
643 fc_lport_free_stats(lport);
645 /* Release the net_device and Scsi_Host */
647 scsi_host_put(lport->host);
651 * fcoe_ddp_setup - calls LLD's ddp_setup through net_device
652 * @lp: the corresponding fc_lport
653 * @xid: the exchange id for this ddp transfer
654 * @sgl: the scatterlist describing this transfer
655 * @sgc: number of sg items
659 static int fcoe_ddp_setup(struct fc_lport *lp, u16 xid,
660 struct scatterlist *sgl, unsigned int sgc)
662 struct net_device *n = fcoe_netdev(lp);
664 if (n->netdev_ops && n->netdev_ops->ndo_fcoe_ddp_setup)
665 return n->netdev_ops->ndo_fcoe_ddp_setup(n, xid, sgl, sgc);
671 * fcoe_ddp_done - calls LLD's ddp_done through net_device
672 * @lp: the corresponding fc_lport
673 * @xid: the exchange id for this ddp transfer
675 * Returns : the length of data that have been completed by ddp
677 static int fcoe_ddp_done(struct fc_lport *lp, u16 xid)
679 struct net_device *n = fcoe_netdev(lp);
681 if (n->netdev_ops && n->netdev_ops->ndo_fcoe_ddp_done)
682 return n->netdev_ops->ndo_fcoe_ddp_done(n, xid);
686 static struct libfc_function_template fcoe_libfc_fcn_templ = {
687 .frame_send = fcoe_xmit,
688 .ddp_setup = fcoe_ddp_setup,
689 .ddp_done = fcoe_ddp_done,
693 * fcoe_if_create() - this function creates the fcoe port
694 * @fcoe: fcoe_interface structure to create an fc_lport instance on
695 * @parent: device pointer to be the parent in sysfs for the SCSI host
697 * Creates fc_lport struct and scsi_host for lport, configures lport.
699 * Returns : The allocated fc_lport or an error pointer
701 static struct fc_lport *fcoe_if_create(struct fcoe_interface *fcoe,
702 struct device *parent)
705 struct fc_lport *lport = NULL;
706 struct fcoe_port *port;
707 struct Scsi_Host *shost;
708 struct net_device *netdev = fcoe->netdev;
710 FCOE_NETDEV_DBG(netdev, "Create Interface\n");
712 shost = libfc_host_alloc(&fcoe_shost_template,
713 sizeof(struct fcoe_port));
715 FCOE_NETDEV_DBG(netdev, "Could not allocate host structure\n");
719 lport = shost_priv(shost);
720 port = lport_priv(lport);
723 /* configure fc_lport, e.g., em */
724 rc = fcoe_lport_config(lport);
726 FCOE_NETDEV_DBG(netdev, "Could not configure lport for the "
731 /* configure lport network properties */
732 rc = fcoe_netdev_config(lport, netdev);
734 FCOE_NETDEV_DBG(netdev, "Could not configure netdev for the "
739 /* configure lport scsi host properties */
740 rc = fcoe_shost_config(lport, shost, parent);
742 FCOE_NETDEV_DBG(netdev, "Could not configure shost for the "
747 /* Initialize the library */
748 rc = fcoe_libfc_config(lport, &fcoe_libfc_fcn_templ);
750 FCOE_NETDEV_DBG(netdev, "Could not configure libfc for the "
756 * fcoe_em_alloc() and fcoe_hostlist_add() both
757 * need to be atomic with respect to other changes to the hostlist
758 * since fcoe_em_alloc() looks for an existing EM
759 * instance on host list updated by fcoe_hostlist_add().
761 * This is currently handled through the fcoe_config_mutex begin held.
764 /* lport exch manager allocation */
765 rc = fcoe_em_config(lport);
767 FCOE_NETDEV_DBG(netdev, "Could not configure the EM for the "
773 fcoe_interface_get(fcoe);
777 fc_exch_mgr_free(lport);
779 scsi_host_put(lport->host);
785 * fcoe_if_init() - attach to scsi transport
787 * Returns : 0 on success
789 static int __init fcoe_if_init(void)
791 /* attach to scsi transport */
792 scsi_transport_fcoe_sw =
793 fc_attach_transport(&fcoe_transport_function);
795 if (!scsi_transport_fcoe_sw) {
796 printk(KERN_ERR "fcoe: Failed to attach to the FC transport\n");
804 * fcoe_if_exit() - detach from scsi transport
806 * Returns : 0 on success
808 int __exit fcoe_if_exit(void)
810 fc_release_transport(scsi_transport_fcoe_sw);
811 scsi_transport_fcoe_sw = NULL;
816 * fcoe_percpu_thread_create() - Create a receive thread for an online cpu
817 * @cpu: cpu index for the online cpu
819 static void fcoe_percpu_thread_create(unsigned int cpu)
821 struct fcoe_percpu_s *p;
822 struct task_struct *thread;
824 p = &per_cpu(fcoe_percpu, cpu);
826 thread = kthread_create(fcoe_percpu_receive_thread,
827 (void *)p, "fcoethread/%d", cpu);
829 if (likely(!IS_ERR(p->thread))) {
830 kthread_bind(thread, cpu);
831 wake_up_process(thread);
833 spin_lock_bh(&p->fcoe_rx_list.lock);
835 spin_unlock_bh(&p->fcoe_rx_list.lock);
840 * fcoe_percpu_thread_destroy() - removes the rx thread for the given cpu
841 * @cpu: cpu index the rx thread is to be removed
843 * Destroys a per-CPU Rx thread. Any pending skbs are moved to the
844 * current CPU's Rx thread. If the thread being destroyed is bound to
845 * the CPU processing this context the skbs will be freed.
847 static void fcoe_percpu_thread_destroy(unsigned int cpu)
849 struct fcoe_percpu_s *p;
850 struct task_struct *thread;
851 struct page *crc_eof;
854 struct fcoe_percpu_s *p0;
855 unsigned targ_cpu = smp_processor_id();
856 #endif /* CONFIG_SMP */
858 FCOE_DBG("Destroying receive thread for CPU %d\n", cpu);
860 /* Prevent any new skbs from being queued for this CPU. */
861 p = &per_cpu(fcoe_percpu, cpu);
862 spin_lock_bh(&p->fcoe_rx_list.lock);
865 crc_eof = p->crc_eof_page;
866 p->crc_eof_page = NULL;
867 p->crc_eof_offset = 0;
868 spin_unlock_bh(&p->fcoe_rx_list.lock);
872 * Don't bother moving the skb's if this context is running
873 * on the same CPU that is having its thread destroyed. This
874 * can easily happen when the module is removed.
876 if (cpu != targ_cpu) {
877 p0 = &per_cpu(fcoe_percpu, targ_cpu);
878 spin_lock_bh(&p0->fcoe_rx_list.lock);
880 FCOE_DBG("Moving frames from CPU %d to CPU %d\n",
883 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
884 __skb_queue_tail(&p0->fcoe_rx_list, skb);
885 spin_unlock_bh(&p0->fcoe_rx_list.lock);
888 * The targeted CPU is not initialized and cannot accept
889 * new skbs. Unlock the targeted CPU and drop the skbs
890 * on the CPU that is going offline.
892 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
894 spin_unlock_bh(&p0->fcoe_rx_list.lock);
898 * This scenario occurs when the module is being removed
899 * and all threads are being destroyed. skbs will continue
900 * to be shifted from the CPU thread that is being removed
901 * to the CPU thread associated with the CPU that is processing
902 * the module removal. Once there is only one CPU Rx thread it
903 * will reach this case and we will drop all skbs and later
906 spin_lock_bh(&p->fcoe_rx_list.lock);
907 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
909 spin_unlock_bh(&p->fcoe_rx_list.lock);
913 * This a non-SMP scenario where the singular Rx thread is
914 * being removed. Free all skbs and stop the thread.
916 spin_lock_bh(&p->fcoe_rx_list.lock);
917 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
919 spin_unlock_bh(&p->fcoe_rx_list.lock);
923 kthread_stop(thread);
930 * fcoe_cpu_callback() - fcoe cpu hotplug event callback
931 * @nfb: callback data block
932 * @action: event triggering the callback
933 * @hcpu: index for the cpu of this event
935 * This creates or destroys per cpu data for fcoe
937 * Returns NOTIFY_OK always.
939 static int fcoe_cpu_callback(struct notifier_block *nfb,
940 unsigned long action, void *hcpu)
942 unsigned cpu = (unsigned long)hcpu;
946 case CPU_ONLINE_FROZEN:
947 FCOE_DBG("CPU %x online: Create Rx thread\n", cpu);
948 fcoe_percpu_thread_create(cpu);
951 case CPU_DEAD_FROZEN:
952 FCOE_DBG("CPU %x offline: Remove Rx thread\n", cpu);
953 fcoe_percpu_thread_destroy(cpu);
961 static struct notifier_block fcoe_cpu_notifier = {
962 .notifier_call = fcoe_cpu_callback,
966 * fcoe_rcv() - this is the fcoe receive function called by NET_RX_SOFTIRQ
967 * @skb: the receive skb
968 * @dev: associated net device
970 * @olddev: last device
972 * this function will receive the packet and build fc frame and pass it up
974 * Returns: 0 for success
976 int fcoe_rcv(struct sk_buff *skb, struct net_device *dev,
977 struct packet_type *ptype, struct net_device *olddev)
980 struct fcoe_rcv_info *fr;
981 struct fcoe_interface *fcoe;
982 struct fc_frame_header *fh;
983 struct fcoe_percpu_s *fps;
986 fcoe = container_of(ptype, struct fcoe_interface, fcoe_packet_type);
988 if (unlikely(lp == NULL)) {
989 FCOE_NETDEV_DBG(dev, "Cannot find hba structure");
995 FCOE_NETDEV_DBG(dev, "skb_info: len:%d data_len:%d head:%p "
996 "data:%p tail:%p end:%p sum:%d dev:%s",
997 skb->len, skb->data_len, skb->head, skb->data,
998 skb_tail_pointer(skb), skb_end_pointer(skb),
999 skb->csum, skb->dev ? skb->dev->name : "<NULL>");
1001 /* check for FCOE packet type */
1002 if (unlikely(eth_hdr(skb)->h_proto != htons(ETH_P_FCOE))) {
1003 FCOE_NETDEV_DBG(dev, "Wrong FC type frame");
1008 * Check for minimum frame length, and make sure required FCoE
1009 * and FC headers are pulled into the linear data area.
1011 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
1012 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
1015 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
1016 fh = (struct fc_frame_header *) skb_transport_header(skb);
1018 fr = fcoe_dev_from_skb(skb);
1023 * In case the incoming frame's exchange is originated from
1024 * the initiator, then received frame's exchange id is ANDed
1025 * with fc_cpu_mask bits to get the same cpu on which exchange
1026 * was originated, otherwise just use the current cpu.
1028 if (ntoh24(fh->fh_f_ctl) & FC_FC_EX_CTX)
1029 cpu = ntohs(fh->fh_ox_id) & fc_cpu_mask;
1031 cpu = smp_processor_id();
1033 fps = &per_cpu(fcoe_percpu, cpu);
1034 spin_lock_bh(&fps->fcoe_rx_list.lock);
1035 if (unlikely(!fps->thread)) {
1037 * The targeted CPU is not ready, let's target
1038 * the first CPU now. For non-SMP systems this
1039 * will check the same CPU twice.
1041 FCOE_NETDEV_DBG(dev, "CPU is online, but no receive thread "
1042 "ready for incoming skb- using first online "
1045 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1046 cpu = first_cpu(cpu_online_map);
1047 fps = &per_cpu(fcoe_percpu, cpu);
1048 spin_lock_bh(&fps->fcoe_rx_list.lock);
1050 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1056 * We now have a valid CPU that we're targeting for
1057 * this skb. We also have this receive thread locked,
1058 * so we're free to queue skbs into it's queue.
1060 __skb_queue_tail(&fps->fcoe_rx_list, skb);
1061 if (fps->fcoe_rx_list.qlen == 1)
1062 wake_up_process(fps->thread);
1064 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1068 fc_lport_get_stats(lp)->ErrorFrames++;
1076 * fcoe_start_io() - pass to netdev to start xmit for fcoe
1077 * @skb: the skb to be xmitted
1079 * Returns: 0 for success
1081 static inline int fcoe_start_io(struct sk_buff *skb)
1086 rc = dev_queue_xmit(skb);
1094 * fcoe_get_paged_crc_eof() - in case we need to alloc a page for crc_eof
1095 * @skb: the skb to be xmitted
1098 * Returns: 0 for success
1100 static int fcoe_get_paged_crc_eof(struct sk_buff *skb, int tlen)
1102 struct fcoe_percpu_s *fps;
1105 fps = &get_cpu_var(fcoe_percpu);
1106 page = fps->crc_eof_page;
1108 page = alloc_page(GFP_ATOMIC);
1110 put_cpu_var(fcoe_percpu);
1113 fps->crc_eof_page = page;
1114 fps->crc_eof_offset = 0;
1118 skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags, page,
1119 fps->crc_eof_offset, tlen);
1121 skb->data_len += tlen;
1122 skb->truesize += tlen;
1123 fps->crc_eof_offset += sizeof(struct fcoe_crc_eof);
1125 if (fps->crc_eof_offset >= PAGE_SIZE) {
1126 fps->crc_eof_page = NULL;
1127 fps->crc_eof_offset = 0;
1130 put_cpu_var(fcoe_percpu);
1135 * fcoe_fc_crc() - calculates FC CRC in this fcoe skb
1136 * @fp: the fc_frame containing data to be checksummed
1138 * This uses crc32() to calculate the crc for port frame
1139 * Return : 32 bit crc
1141 u32 fcoe_fc_crc(struct fc_frame *fp)
1143 struct sk_buff *skb = fp_skb(fp);
1144 struct skb_frag_struct *frag;
1145 unsigned char *data;
1146 unsigned long off, len, clen;
1150 crc = crc32(~0, skb->data, skb_headlen(skb));
1152 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1153 frag = &skb_shinfo(skb)->frags[i];
1154 off = frag->page_offset;
1157 clen = min(len, PAGE_SIZE - (off & ~PAGE_MASK));
1158 data = kmap_atomic(frag->page + (off >> PAGE_SHIFT),
1159 KM_SKB_DATA_SOFTIRQ);
1160 crc = crc32(crc, data + (off & ~PAGE_MASK), clen);
1161 kunmap_atomic(data, KM_SKB_DATA_SOFTIRQ);
1170 * fcoe_xmit() - FCoE frame transmit function
1171 * @lp: the associated local fcoe
1172 * @fp: the fc_frame to be transmitted
1174 * Return : 0 for success
1176 int fcoe_xmit(struct fc_lport *lp, struct fc_frame *fp)
1181 struct fcoe_crc_eof *cp;
1182 struct sk_buff *skb;
1183 struct fcoe_dev_stats *stats;
1184 struct fc_frame_header *fh;
1185 unsigned int hlen; /* header length implies the version */
1186 unsigned int tlen; /* trailer length */
1187 unsigned int elen; /* eth header, may include vlan */
1188 struct fcoe_port *port = lport_priv(lp);
1189 struct fcoe_interface *fcoe = port->fcoe;
1191 struct fcoe_hdr *hp;
1193 WARN_ON((fr_len(fp) % sizeof(u32)) != 0);
1195 fh = fc_frame_header_get(fp);
1197 wlen = skb->len / FCOE_WORD_TO_BYTE;
1204 if (unlikely(fh->fh_r_ctl == FC_RCTL_ELS_REQ) &&
1205 fcoe_ctlr_els_send(&fcoe->ctlr, skb))
1211 elen = sizeof(struct ethhdr);
1212 hlen = sizeof(struct fcoe_hdr);
1213 tlen = sizeof(struct fcoe_crc_eof);
1214 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
1217 if (likely(lp->crc_offload)) {
1218 skb->ip_summed = CHECKSUM_PARTIAL;
1219 skb->csum_start = skb_headroom(skb);
1220 skb->csum_offset = skb->len;
1223 skb->ip_summed = CHECKSUM_NONE;
1224 crc = fcoe_fc_crc(fp);
1227 /* copy port crc and eof to the skb buff */
1228 if (skb_is_nonlinear(skb)) {
1230 if (fcoe_get_paged_crc_eof(skb, tlen)) {
1234 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
1235 cp = kmap_atomic(frag->page, KM_SKB_DATA_SOFTIRQ)
1236 + frag->page_offset;
1238 cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
1241 memset(cp, 0, sizeof(*cp));
1243 cp->fcoe_crc32 = cpu_to_le32(~crc);
1245 if (skb_is_nonlinear(skb)) {
1246 kunmap_atomic(cp, KM_SKB_DATA_SOFTIRQ);
1250 /* adjust skb network/transport offsets to match mac/fcoe/port */
1251 skb_push(skb, elen + hlen);
1252 skb_reset_mac_header(skb);
1253 skb_reset_network_header(skb);
1254 skb->mac_len = elen;
1255 skb->protocol = htons(ETH_P_FCOE);
1256 skb->dev = fcoe->netdev;
1258 /* fill up mac and fcoe headers */
1260 eh->h_proto = htons(ETH_P_FCOE);
1261 if (fcoe->ctlr.map_dest)
1262 fc_fcoe_set_mac(eh->h_dest, fh->fh_d_id);
1264 /* insert GW address */
1265 memcpy(eh->h_dest, fcoe->ctlr.dest_addr, ETH_ALEN);
1267 if (unlikely(fcoe->ctlr.flogi_oxid != FC_XID_UNKNOWN))
1268 memcpy(eh->h_source, fcoe->ctlr.ctl_src_addr, ETH_ALEN);
1270 memcpy(eh->h_source, fcoe->ctlr.data_src_addr, ETH_ALEN);
1272 hp = (struct fcoe_hdr *)(eh + 1);
1273 memset(hp, 0, sizeof(*hp));
1275 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
1278 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
1279 if (lp->seq_offload && fr_max_payload(fp)) {
1280 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
1281 skb_shinfo(skb)->gso_size = fr_max_payload(fp);
1283 skb_shinfo(skb)->gso_type = 0;
1284 skb_shinfo(skb)->gso_size = 0;
1286 /* update tx stats: regardless if LLD fails */
1287 stats = fc_lport_get_stats(lp);
1289 stats->TxWords += wlen;
1291 /* send down to lld */
1293 if (port->fcoe_pending_queue.qlen)
1294 fcoe_check_wait_queue(lp, skb);
1295 else if (fcoe_start_io(skb))
1296 fcoe_check_wait_queue(lp, skb);
1302 * fcoe_percpu_receive_thread() - recv thread per cpu
1303 * @arg: ptr to the fcoe per cpu struct
1305 * Return: 0 for success
1307 int fcoe_percpu_receive_thread(void *arg)
1309 struct fcoe_percpu_s *p = arg;
1311 struct fc_lport *lp;
1312 struct fcoe_rcv_info *fr;
1313 struct fcoe_dev_stats *stats;
1314 struct fc_frame_header *fh;
1315 struct sk_buff *skb;
1316 struct fcoe_crc_eof crc_eof;
1317 struct fc_frame *fp;
1319 struct fcoe_port *port;
1320 struct fcoe_hdr *hp;
1322 set_user_nice(current, -20);
1324 while (!kthread_should_stop()) {
1326 spin_lock_bh(&p->fcoe_rx_list.lock);
1327 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) == NULL) {
1328 set_current_state(TASK_INTERRUPTIBLE);
1329 spin_unlock_bh(&p->fcoe_rx_list.lock);
1331 set_current_state(TASK_RUNNING);
1332 if (kthread_should_stop())
1334 spin_lock_bh(&p->fcoe_rx_list.lock);
1336 spin_unlock_bh(&p->fcoe_rx_list.lock);
1337 fr = fcoe_dev_from_skb(skb);
1339 if (unlikely(lp == NULL)) {
1340 FCOE_NETDEV_DBG(skb->dev, "Invalid HBA Structure");
1345 FCOE_NETDEV_DBG(skb->dev, "skb_info: len:%d data_len:%d "
1346 "head:%p data:%p tail:%p end:%p sum:%d dev:%s",
1347 skb->len, skb->data_len,
1348 skb->head, skb->data, skb_tail_pointer(skb),
1349 skb_end_pointer(skb), skb->csum,
1350 skb->dev ? skb->dev->name : "<NULL>");
1353 * Save source MAC address before discarding header.
1355 port = lport_priv(lp);
1356 if (skb_is_nonlinear(skb))
1357 skb_linearize(skb); /* not ideal */
1358 mac = eth_hdr(skb)->h_source;
1361 * Frame length checks and setting up the header pointers
1362 * was done in fcoe_rcv already.
1364 hp = (struct fcoe_hdr *) skb_network_header(skb);
1365 fh = (struct fc_frame_header *) skb_transport_header(skb);
1367 stats = fc_lport_get_stats(lp);
1368 if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) {
1369 if (stats->ErrorFrames < 5)
1370 printk(KERN_WARNING "fcoe: FCoE version "
1371 "mismatch: The frame has "
1372 "version %x, but the "
1373 "initiator supports version "
1374 "%x\n", FC_FCOE_DECAPS_VER(hp),
1376 stats->ErrorFrames++;
1381 skb_pull(skb, sizeof(struct fcoe_hdr));
1382 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
1385 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
1387 fp = (struct fc_frame *)skb;
1390 fr_sof(fp) = hp->fcoe_sof;
1392 /* Copy out the CRC and EOF trailer for access */
1393 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof))) {
1397 fr_eof(fp) = crc_eof.fcoe_eof;
1398 fr_crc(fp) = crc_eof.fcoe_crc32;
1399 if (pskb_trim(skb, fr_len)) {
1405 * We only check CRC if no offload is available and if it is
1406 * it's solicited data, in which case, the FCP layer would
1407 * check it during the copy.
1409 if (lp->crc_offload && skb->ip_summed == CHECKSUM_UNNECESSARY)
1410 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1412 fr_flags(fp) |= FCPHF_CRC_UNCHECKED;
1414 fh = fc_frame_header_get(fp);
1415 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA &&
1416 fh->fh_type == FC_TYPE_FCP) {
1417 fc_exch_recv(lp, fp);
1420 if (fr_flags(fp) & FCPHF_CRC_UNCHECKED) {
1421 if (le32_to_cpu(fr_crc(fp)) !=
1422 ~crc32(~0, skb->data, fr_len)) {
1423 if (stats->InvalidCRCCount < 5)
1424 printk(KERN_WARNING "fcoe: dropping "
1425 "frame with CRC error\n");
1426 stats->InvalidCRCCount++;
1427 stats->ErrorFrames++;
1431 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1433 if (unlikely(port->fcoe->ctlr.flogi_oxid != FC_XID_UNKNOWN) &&
1434 fcoe_ctlr_recv_flogi(&port->fcoe->ctlr, fp, mac)) {
1438 fc_exch_recv(lp, fp);
1444 * fcoe_check_wait_queue() - attempt to clear the transmit backlog
1447 * This empties the wait_queue, dequeue the head of the wait_queue queue
1448 * and calls fcoe_start_io() for each packet, if all skb have been
1449 * transmitted, return qlen or -1 if a error occurs, then restore
1450 * wait_queue and try again later.
1452 * The wait_queue is used when the skb transmit fails. skb will go
1453 * in the wait_queue which will be emptied by the timer function or
1454 * by the next skb transmit.
1456 static void fcoe_check_wait_queue(struct fc_lport *lp, struct sk_buff *skb)
1458 struct fcoe_port *port = lport_priv(lp);
1461 spin_lock_bh(&port->fcoe_pending_queue.lock);
1464 __skb_queue_tail(&port->fcoe_pending_queue, skb);
1466 if (port->fcoe_pending_queue_active)
1468 port->fcoe_pending_queue_active = 1;
1470 while (port->fcoe_pending_queue.qlen) {
1471 /* keep qlen > 0 until fcoe_start_io succeeds */
1472 port->fcoe_pending_queue.qlen++;
1473 skb = __skb_dequeue(&port->fcoe_pending_queue);
1475 spin_unlock_bh(&port->fcoe_pending_queue.lock);
1476 rc = fcoe_start_io(skb);
1477 spin_lock_bh(&port->fcoe_pending_queue.lock);
1480 __skb_queue_head(&port->fcoe_pending_queue, skb);
1481 /* undo temporary increment above */
1482 port->fcoe_pending_queue.qlen--;
1485 /* undo temporary increment above */
1486 port->fcoe_pending_queue.qlen--;
1489 if (port->fcoe_pending_queue.qlen < FCOE_LOW_QUEUE_DEPTH)
1491 if (port->fcoe_pending_queue.qlen && !timer_pending(&port->timer))
1492 mod_timer(&port->timer, jiffies + 2);
1493 port->fcoe_pending_queue_active = 0;
1495 if (port->fcoe_pending_queue.qlen > FCOE_MAX_QUEUE_DEPTH)
1497 spin_unlock_bh(&port->fcoe_pending_queue.lock);
1502 * fcoe_dev_setup() - setup link change notification interface
1504 static void fcoe_dev_setup(void)
1506 register_netdevice_notifier(&fcoe_notifier);
1510 * fcoe_dev_cleanup() - cleanup link change notification interface
1512 static void fcoe_dev_cleanup(void)
1514 unregister_netdevice_notifier(&fcoe_notifier);
1518 * fcoe_device_notification() - netdev event notification callback
1519 * @notifier: context of the notification
1520 * @event: type of event
1521 * @ptr: fixed array for output parsed ifname
1523 * This function is called by the ethernet driver in case of link change event
1525 * Returns: 0 for success
1527 static int fcoe_device_notification(struct notifier_block *notifier,
1528 ulong event, void *ptr)
1530 struct fc_lport *lp = NULL;
1531 struct net_device *netdev = ptr;
1532 struct fcoe_interface *fcoe;
1533 struct fcoe_dev_stats *stats;
1534 u32 link_possible = 1;
1538 read_lock(&fcoe_hostlist_lock);
1539 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
1540 if (fcoe->netdev == netdev) {
1545 read_unlock(&fcoe_hostlist_lock);
1553 case NETDEV_GOING_DOWN:
1559 case NETDEV_CHANGEMTU:
1560 mfs = netdev->mtu - (sizeof(struct fcoe_hdr) +
1561 sizeof(struct fcoe_crc_eof));
1562 if (mfs >= FC_MIN_MAX_FRAME)
1563 fc_set_mfs(lp, mfs);
1565 case NETDEV_REGISTER:
1568 FCOE_NETDEV_DBG(netdev, "Unknown event %ld "
1569 "from netdev netlink\n", event);
1571 if (link_possible && !fcoe_link_ok(lp))
1572 fcoe_ctlr_link_up(&fcoe->ctlr);
1573 else if (fcoe_ctlr_link_down(&fcoe->ctlr)) {
1574 stats = fc_lport_get_stats(lp);
1575 stats->LinkFailureCount++;
1576 fcoe_clean_pending_queue(lp);
1583 * fcoe_if_to_netdev() - parse a name buffer to get netdev
1584 * @buffer: incoming buffer to be copied
1586 * Returns: NULL or ptr to net_device
1588 static struct net_device *fcoe_if_to_netdev(const char *buffer)
1591 char ifname[IFNAMSIZ + 2];
1594 strlcpy(ifname, buffer, IFNAMSIZ);
1595 cp = ifname + strlen(ifname);
1596 while (--cp >= ifname && *cp == '\n')
1598 return dev_get_by_name(&init_net, ifname);
1604 * fcoe_netdev_to_module_owner() - finds out the driver module of the netdev
1605 * @netdev: the target netdev
1607 * Returns: ptr to the struct module, NULL for failure
1609 static struct module *
1610 fcoe_netdev_to_module_owner(const struct net_device *netdev)
1617 dev = netdev->dev.parent;
1624 return dev->driver->owner;
1628 * fcoe_ethdrv_get() - Hold the Ethernet driver
1629 * @netdev: the target netdev
1631 * Holds the Ethernet driver module by try_module_get() for
1632 * the corresponding netdev.
1634 * Returns: 0 for success
1636 static int fcoe_ethdrv_get(const struct net_device *netdev)
1638 struct module *owner;
1640 owner = fcoe_netdev_to_module_owner(netdev);
1642 FCOE_NETDEV_DBG(netdev, "Hold driver module %s\n",
1643 module_name(owner));
1644 return try_module_get(owner);
1650 * fcoe_ethdrv_put() - Release the Ethernet driver
1651 * @netdev: the target netdev
1653 * Releases the Ethernet driver module by module_put for
1654 * the corresponding netdev.
1656 * Returns: 0 for success
1658 static int fcoe_ethdrv_put(const struct net_device *netdev)
1660 struct module *owner;
1662 owner = fcoe_netdev_to_module_owner(netdev);
1664 FCOE_NETDEV_DBG(netdev, "Release driver module %s\n",
1665 module_name(owner));
1673 * fcoe_destroy() - handles the destroy from sysfs
1674 * @buffer: expected to be an eth if name
1675 * @kp: associated kernel param
1677 * Returns: 0 for success
1679 static int fcoe_destroy(const char *buffer, struct kernel_param *kp)
1681 struct net_device *netdev;
1682 struct fcoe_interface *fcoe;
1683 struct fcoe_port *port;
1684 struct fc_lport *lport;
1687 mutex_lock(&fcoe_config_mutex);
1688 #ifdef CONFIG_FCOE_MODULE
1690 * Make sure the module has been initialized, and is not about to be
1691 * removed. Module paramter sysfs files are writable before the
1692 * module_init function is called and after module_exit.
1694 if (THIS_MODULE->state != MODULE_STATE_LIVE) {
1700 netdev = fcoe_if_to_netdev(buffer);
1705 /* look for existing lport */
1706 lport = fcoe_hostlist_lookup(netdev);
1711 /* Remove the instance from fcoe's list */
1712 fcoe_hostlist_remove(lport);
1713 port = lport_priv(lport);
1715 fcoe_if_destroy(lport);
1716 fcoe_ethdrv_put(netdev);
1721 mutex_unlock(&fcoe_config_mutex);
1726 * fcoe_create() - Handles the create call from sysfs
1727 * @buffer: expected to be an eth if name
1728 * @kp: associated kernel param
1730 * Returns: 0 for success
1732 static int fcoe_create(const char *buffer, struct kernel_param *kp)
1735 struct fcoe_interface *fcoe;
1736 struct fc_lport *lport;
1737 struct net_device *netdev;
1739 mutex_lock(&fcoe_config_mutex);
1740 #ifdef CONFIG_FCOE_MODULE
1742 * Make sure the module has been initialized, and is not about to be
1743 * removed. Module paramter sysfs files are writable before the
1744 * module_init function is called and after module_exit.
1746 if (THIS_MODULE->state != MODULE_STATE_LIVE) {
1752 netdev = fcoe_if_to_netdev(buffer);
1757 /* look for existing lport */
1758 if (fcoe_hostlist_lookup(netdev)) {
1762 fcoe_ethdrv_get(netdev);
1764 fcoe = fcoe_interface_create(netdev);
1770 lport = fcoe_if_create(fcoe, &netdev->dev);
1771 if (IS_ERR(lport)) {
1772 printk(KERN_ERR "fcoe: Failed to create interface (%s)\n",
1774 fcoe_ethdrv_put(netdev);
1779 /* Make this the "master" N_Port */
1780 fcoe->ctlr.lp = lport;
1782 /* add to lports list */
1783 fcoe_hostlist_add(lport);
1785 /* start FIP Discovery and FLOGI */
1786 lport->boot_time = jiffies;
1787 fc_fabric_login(lport);
1788 if (!fcoe_link_ok(lport))
1789 fcoe_ctlr_link_up(&fcoe->ctlr);
1794 * Release from init in fcoe_interface_create(), on success lport
1795 * should be holding a reference taken in fcoe_if_create().
1797 fcoe_interface_put(fcoe);
1801 mutex_unlock(&fcoe_config_mutex);
1805 module_param_call(create, fcoe_create, NULL, NULL, S_IWUSR);
1806 __MODULE_PARM_TYPE(create, "string");
1807 MODULE_PARM_DESC(create, "Create fcoe fcoe using net device passed in.");
1808 module_param_call(destroy, fcoe_destroy, NULL, NULL, S_IWUSR);
1809 __MODULE_PARM_TYPE(destroy, "string");
1810 MODULE_PARM_DESC(destroy, "Destroy fcoe fcoe");
1813 * fcoe_link_ok() - Check if link is ok for the fc_lport
1814 * @lp: ptr to the fc_lport
1816 * Any permanently-disqualifying conditions have been previously checked.
1817 * This also updates the speed setting, which may change with link for 100/1000.
1819 * This function should probably be checking for PAUSE support at some point
1820 * in the future. Currently Per-priority-pause is not determinable using
1821 * ethtool, so we shouldn't be restrictive until that problem is resolved.
1823 * Returns: 0 if link is OK for use by FCoE.
1826 int fcoe_link_ok(struct fc_lport *lp)
1828 struct fcoe_port *port = lport_priv(lp);
1829 struct net_device *dev = port->fcoe->netdev;
1830 struct ethtool_cmd ecmd = { ETHTOOL_GSET };
1832 if ((dev->flags & IFF_UP) && netif_carrier_ok(dev) &&
1833 (!dev_ethtool_get_settings(dev, &ecmd))) {
1834 lp->link_supported_speeds &=
1835 ~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
1836 if (ecmd.supported & (SUPPORTED_1000baseT_Half |
1837 SUPPORTED_1000baseT_Full))
1838 lp->link_supported_speeds |= FC_PORTSPEED_1GBIT;
1839 if (ecmd.supported & SUPPORTED_10000baseT_Full)
1840 lp->link_supported_speeds |=
1841 FC_PORTSPEED_10GBIT;
1842 if (ecmd.speed == SPEED_1000)
1843 lp->link_speed = FC_PORTSPEED_1GBIT;
1844 if (ecmd.speed == SPEED_10000)
1845 lp->link_speed = FC_PORTSPEED_10GBIT;
1853 * fcoe_percpu_clean() - Clear the pending skbs for an lport
1856 void fcoe_percpu_clean(struct fc_lport *lp)
1858 struct fcoe_percpu_s *pp;
1859 struct fcoe_rcv_info *fr;
1860 struct sk_buff_head *list;
1861 struct sk_buff *skb, *next;
1862 struct sk_buff *head;
1865 for_each_possible_cpu(cpu) {
1866 pp = &per_cpu(fcoe_percpu, cpu);
1867 spin_lock_bh(&pp->fcoe_rx_list.lock);
1868 list = &pp->fcoe_rx_list;
1870 for (skb = head; skb != (struct sk_buff *)list;
1873 fr = fcoe_dev_from_skb(skb);
1874 if (fr->fr_dev == lp) {
1875 __skb_unlink(skb, list);
1879 spin_unlock_bh(&pp->fcoe_rx_list.lock);
1884 * fcoe_clean_pending_queue() - Dequeue a skb and free it
1885 * @lp: the corresponding fc_lport
1889 void fcoe_clean_pending_queue(struct fc_lport *lp)
1891 struct fcoe_port *port = lport_priv(lp);
1892 struct sk_buff *skb;
1894 spin_lock_bh(&port->fcoe_pending_queue.lock);
1895 while ((skb = __skb_dequeue(&port->fcoe_pending_queue)) != NULL) {
1896 spin_unlock_bh(&port->fcoe_pending_queue.lock);
1898 spin_lock_bh(&port->fcoe_pending_queue.lock);
1900 spin_unlock_bh(&port->fcoe_pending_queue.lock);
1904 * fcoe_reset() - Resets the fcoe
1905 * @shost: shost the reset is from
1909 int fcoe_reset(struct Scsi_Host *shost)
1911 struct fc_lport *lport = shost_priv(shost);
1912 fc_lport_reset(lport);
1917 * fcoe_hostlist_lookup_port() - find the corresponding lport by a given device
1918 * @dev: this is currently ptr to net_device
1920 * Called with fcoe_hostlist_lock held.
1922 * Returns: NULL or the located fcoe_port
1924 static struct fcoe_interface *
1925 fcoe_hostlist_lookup_port(const struct net_device *dev)
1927 struct fcoe_interface *fcoe;
1929 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
1930 if (fcoe->netdev == dev)
1937 * fcoe_hostlist_lookup() - Find the corresponding lport by netdev
1938 * @netdev: ptr to net_device
1940 * Returns: 0 for success
1942 struct fc_lport *fcoe_hostlist_lookup(const struct net_device *netdev)
1944 struct fcoe_interface *fcoe;
1946 read_lock(&fcoe_hostlist_lock);
1947 fcoe = fcoe_hostlist_lookup_port(netdev);
1948 read_unlock(&fcoe_hostlist_lock);
1950 return (fcoe) ? fcoe->ctlr.lp : NULL;
1954 * fcoe_hostlist_add() - Add a lport to lports list
1955 * @lp: ptr to the fc_lport to be added
1957 * Returns: 0 for success
1959 int fcoe_hostlist_add(const struct fc_lport *lport)
1961 struct fcoe_interface *fcoe;
1962 struct fcoe_port *port;
1964 write_lock_bh(&fcoe_hostlist_lock);
1965 fcoe = fcoe_hostlist_lookup_port(fcoe_netdev(lport));
1967 port = lport_priv(lport);
1969 list_add_tail(&fcoe->list, &fcoe_hostlist);
1971 write_unlock_bh(&fcoe_hostlist_lock);
1976 * fcoe_hostlist_remove() - remove a lport from lports list
1977 * @lp: ptr to the fc_lport to be removed
1979 * Returns: 0 for success
1981 int fcoe_hostlist_remove(const struct fc_lport *lport)
1983 struct fcoe_interface *fcoe;
1985 write_lock_bh(&fcoe_hostlist_lock);
1986 fcoe = fcoe_hostlist_lookup_port(fcoe_netdev(lport));
1988 list_del(&fcoe->list);
1989 write_unlock_bh(&fcoe_hostlist_lock);
1995 * fcoe_init() - fcoe module loading initialization
1997 * Returns 0 on success, negative on failure
1999 static int __init fcoe_init(void)
2003 struct fcoe_percpu_s *p;
2005 mutex_lock(&fcoe_config_mutex);
2007 for_each_possible_cpu(cpu) {
2008 p = &per_cpu(fcoe_percpu, cpu);
2009 skb_queue_head_init(&p->fcoe_rx_list);
2012 for_each_online_cpu(cpu)
2013 fcoe_percpu_thread_create(cpu);
2015 /* Initialize per CPU interrupt thread */
2016 rc = register_hotcpu_notifier(&fcoe_cpu_notifier);
2020 /* Setup link change notification */
2023 rc = fcoe_if_init();
2027 mutex_unlock(&fcoe_config_mutex);
2031 for_each_online_cpu(cpu) {
2032 fcoe_percpu_thread_destroy(cpu);
2034 mutex_unlock(&fcoe_config_mutex);
2037 module_init(fcoe_init);
2040 * fcoe_exit() - fcoe module unloading cleanup
2042 * Returns 0 on success, negative on failure
2044 static void __exit fcoe_exit(void)
2047 struct fcoe_interface *fcoe, *tmp;
2048 LIST_HEAD(local_list);
2050 mutex_lock(&fcoe_config_mutex);
2054 /* releases the associated fcoe hosts */
2055 write_lock_bh(&fcoe_hostlist_lock);
2056 list_splice_init(&fcoe_hostlist, &local_list);
2057 write_unlock_bh(&fcoe_hostlist_lock);
2059 list_for_each_entry_safe(fcoe, tmp, &local_list, list) {
2060 list_del(&fcoe->list);
2061 fcoe_if_destroy(fcoe->ctlr.lp);
2064 unregister_hotcpu_notifier(&fcoe_cpu_notifier);
2066 for_each_online_cpu(cpu)
2067 fcoe_percpu_thread_destroy(cpu);
2069 /* detach from scsi transport */
2072 mutex_unlock(&fcoe_config_mutex);
2074 module_exit(fcoe_exit);