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,
142 * fcoe_fip_recv - handle a received FIP frame.
143 * @skb: the receive skb
144 * @dev: associated &net_device
145 * @ptype: the &packet_type structure which was used to register this handler.
146 * @orig_dev: original receive &net_device, in case @dev is a bond.
148 * Returns: 0 for success
150 static int fcoe_fip_recv(struct sk_buff *skb, struct net_device *dev,
151 struct packet_type *ptype,
152 struct net_device *orig_dev)
154 struct fcoe_port *port;
156 port = container_of(ptype, struct fcoe_port, fip_packet_type);
157 fcoe_ctlr_recv(&port->ctlr, skb);
162 * fcoe_fip_send() - send an Ethernet-encapsulated FIP frame.
163 * @fip: FCoE controller.
166 static void fcoe_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
168 skb->dev = fcoe_from_ctlr(fip)->fcoe->netdev;
173 * fcoe_update_src_mac() - Update Ethernet MAC filters.
174 * @fip: FCoE controller.
175 * @old: Unicast MAC address to delete if the MAC is non-zero.
176 * @new: Unicast MAC address to add.
178 * Remove any previously-set unicast MAC filter.
179 * Add secondary FCoE MAC address filter for our OUI.
181 static void fcoe_update_src_mac(struct fcoe_ctlr *fip, u8 *old, u8 *new)
183 struct fcoe_interface *fcoe;
184 struct fcoe_port *port;
186 port = fcoe_from_ctlr(fip);
190 if (!is_zero_ether_addr(old))
191 dev_unicast_delete(fcoe->netdev, old);
192 dev_unicast_add(fcoe->netdev, new);
197 * fcoe_lport_config() - sets up the fc_lport
198 * @lp: ptr to the fc_lport
200 * Returns: 0 for success
202 static int fcoe_lport_config(struct fc_lport *lp)
206 lp->max_retry_count = 3;
207 lp->max_rport_retry_count = 3;
208 lp->e_d_tov = 2 * 1000; /* FC-FS default */
209 lp->r_a_tov = 2 * 2 * 1000;
210 lp->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
211 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
213 fc_lport_init_stats(lp);
215 /* lport fc_lport related configuration */
218 /* offload related configuration */
229 * fcoe_netdev_cleanup() - clean up netdev configurations
230 * @port: ptr to the fcoe_port
232 void fcoe_netdev_cleanup(struct fcoe_port *port)
234 u8 flogi_maddr[ETH_ALEN];
235 struct fcoe_interface *fcoe = port->fcoe;
237 /* Don't listen for Ethernet packets anymore */
238 dev_remove_pack(&port->fcoe_packet_type);
239 dev_remove_pack(&port->fip_packet_type);
241 /* Delete secondary MAC addresses */
243 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
244 dev_unicast_delete(fcoe->netdev, flogi_maddr);
245 if (!is_zero_ether_addr(port->ctlr.data_src_addr))
246 dev_unicast_delete(fcoe->netdev, port->ctlr.data_src_addr);
248 dev_unicast_delete(fcoe->netdev, port->ctlr.ctl_src_addr);
249 dev_mc_delete(fcoe->netdev, FIP_ALL_ENODE_MACS, ETH_ALEN, 0);
254 * fcoe_queue_timer() - fcoe queue timer
255 * @lp: the fc_lport pointer
257 * Calls fcoe_check_wait_queue on timeout
260 static void fcoe_queue_timer(ulong lp)
262 fcoe_check_wait_queue((struct fc_lport *)lp, NULL);
266 * fcoe_netdev_config() - Set up netdev for SW FCoE
267 * @lp : ptr to the fc_lport
268 * @netdev : ptr to the associated netdevice struct
270 * Must be called after fcoe_lport_config() as it will use lport mutex
272 * Returns : 0 for success
274 static int fcoe_netdev_config(struct fc_lport *lp, struct net_device *netdev)
278 struct fcoe_interface *fcoe;
279 struct fcoe_port *port;
280 u8 flogi_maddr[ETH_ALEN];
281 struct netdev_hw_addr *ha;
283 /* Setup lport private data to point to fcoe softc */
284 port = lport_priv(lp);
287 fcoe->netdev = netdev;
289 /* Do not support for bonding device */
290 if ((netdev->priv_flags & IFF_MASTER_ALB) ||
291 (netdev->priv_flags & IFF_SLAVE_INACTIVE) ||
292 (netdev->priv_flags & IFF_MASTER_8023AD)) {
297 * Determine max frame size based on underlying device and optional
298 * user-configured limit. If the MFS is too low, fcoe_link_ok()
299 * will return 0, so do this first.
301 mfs = netdev->mtu - (sizeof(struct fcoe_hdr) +
302 sizeof(struct fcoe_crc_eof));
303 if (fc_set_mfs(lp, mfs))
306 /* offload features support */
307 if (netdev->features & NETIF_F_SG)
310 if (netdev->features & NETIF_F_FCOE_CRC) {
312 FCOE_NETDEV_DBG(netdev, "Supports FCCRC offload\n");
314 if (netdev->features & NETIF_F_FSO) {
316 lp->lso_max = netdev->gso_max_size;
317 FCOE_NETDEV_DBG(netdev, "Supports LSO for max len 0x%x\n",
320 if (netdev->fcoe_ddp_xid) {
322 lp->lro_xid = netdev->fcoe_ddp_xid;
323 FCOE_NETDEV_DBG(netdev, "Supports LRO for max xid 0x%x\n",
326 skb_queue_head_init(&port->fcoe_pending_queue);
327 port->fcoe_pending_queue_active = 0;
328 setup_timer(&port->timer, fcoe_queue_timer, (unsigned long)lp);
330 /* look for SAN MAC address, if multiple SAN MACs exist, only
331 * use the first one for SPMA */
333 for_each_dev_addr(netdev, ha) {
334 if ((ha->type == NETDEV_HW_ADDR_T_SAN) &&
335 (is_valid_ether_addr(port->ctlr.ctl_src_addr))) {
336 memcpy(port->ctlr.ctl_src_addr, ha->addr, ETH_ALEN);
343 /* setup Source Mac Address */
344 if (!port->ctlr.spma)
345 memcpy(port->ctlr.ctl_src_addr, netdev->dev_addr,
348 wwnn = fcoe_wwn_from_mac(netdev->dev_addr, 1, 0);
349 fc_set_wwnn(lp, wwnn);
350 /* XXX - 3rd arg needs to be vlan id */
351 wwpn = fcoe_wwn_from_mac(netdev->dev_addr, 2, 0);
352 fc_set_wwpn(lp, wwpn);
355 * Add FCoE MAC address as second unicast MAC address
356 * or enter promiscuous mode if not capable of listening
357 * for multiple unicast MACs.
360 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
361 dev_unicast_add(netdev, flogi_maddr);
363 dev_unicast_add(netdev, port->ctlr.ctl_src_addr);
364 dev_mc_add(netdev, FIP_ALL_ENODE_MACS, ETH_ALEN, 0);
368 * setup the receive function from ethernet driver
369 * on the ethertype for the given device
371 port->fcoe_packet_type.func = fcoe_rcv;
372 port->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
373 port->fcoe_packet_type.dev = netdev;
374 dev_add_pack(&port->fcoe_packet_type);
376 port->fip_packet_type.func = fcoe_fip_recv;
377 port->fip_packet_type.type = htons(ETH_P_FIP);
378 port->fip_packet_type.dev = netdev;
379 dev_add_pack(&port->fip_packet_type);
385 * fcoe_shost_config() - Sets up fc_lport->host
386 * @lp : ptr to the fc_lport
387 * @shost : ptr to the associated scsi host
388 * @dev : device associated to scsi host
390 * Must be called after fcoe_lport_config() and fcoe_netdev_config()
392 * Returns : 0 for success
394 static int fcoe_shost_config(struct fc_lport *lp, struct Scsi_Host *shost,
399 /* lport scsi host config */
402 lp->host->max_lun = FCOE_MAX_LUN;
403 lp->host->max_id = FCOE_MAX_FCP_TARGET;
404 lp->host->max_channel = 0;
405 lp->host->transportt = scsi_transport_fcoe_sw;
407 /* add the new host to the SCSI-ml */
408 rc = scsi_add_host(lp->host, dev);
410 FCOE_NETDEV_DBG(fcoe_netdev(lp), "fcoe_shost_config: "
411 "error on scsi_add_host\n");
414 sprintf(fc_host_symbolic_name(lp->host), "%s v%s over %s",
415 FCOE_NAME, FCOE_VERSION,
416 fcoe_netdev(lp)->name);
422 * fcoe_oem_match() - match for read types IO
423 * @fp: the fc_frame for new IO.
425 * Returns : true for read types IO, otherwise returns false.
427 bool fcoe_oem_match(struct fc_frame *fp)
429 return fc_fcp_is_read(fr_fsp(fp)) &&
430 (fr_fsp(fp)->data_len > fcoe_ddp_min);
434 * fcoe_em_config() - allocates em for this lport
435 * @lp: the fcoe that em is to allocated for
437 * Called with write fcoe_hostlist_lock held.
439 * Returns : 0 on success
441 static inline int fcoe_em_config(struct fc_lport *lp)
443 struct fcoe_port *port = lport_priv(lp);
444 struct fcoe_port *oldport = NULL;
445 struct fcoe_interface *fcoe = port->fcoe;
446 struct fcoe_interface *oldfcoe = NULL;
447 struct net_device *old_real_dev, *cur_real_dev;
448 u16 min_xid = FCOE_MIN_XID;
449 u16 max_xid = FCOE_MAX_XID;
452 * Check if need to allocate an em instance for
453 * offload exchange ids to be shared across all VN_PORTs/lport.
455 if (!lp->lro_enabled || !lp->lro_xid || (lp->lro_xid >= max_xid)) {
461 * Reuse existing offload em instance in case
462 * it is already allocated on real eth device
464 if (fcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
465 cur_real_dev = vlan_dev_real_dev(fcoe->netdev);
467 cur_real_dev = fcoe->netdev;
469 list_for_each_entry(oldfcoe, &fcoe_hostlist, list) {
470 oldport = oldfcoe->priv;
471 if (oldfcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
472 old_real_dev = vlan_dev_real_dev(oldfcoe->netdev);
474 old_real_dev = oldfcoe->netdev;
476 if (cur_real_dev == old_real_dev) {
477 port->oem = oldport->oem;
483 if (!fc_exch_mgr_add(lp, port->oem, fcoe_oem_match)) {
484 printk(KERN_ERR "fcoe_em_config: failed to add "
485 "offload em:%p on interface:%s\n",
486 port->oem, fcoe->netdev->name);
490 port->oem = fc_exch_mgr_alloc(lp, FC_CLASS_3,
491 FCOE_MIN_XID, lp->lro_xid,
494 printk(KERN_ERR "fcoe_em_config: failed to allocate "
495 "em for offload exches on interface:%s\n",
502 * Exclude offload EM xid range from next EM xid range.
504 min_xid += lp->lro_xid + 1;
507 if (!fc_exch_mgr_alloc(lp, FC_CLASS_3, min_xid, max_xid, NULL)) {
508 printk(KERN_ERR "fcoe_em_config: failed to "
509 "allocate em on interface %s\n", fcoe->netdev->name);
517 * fcoe_if_destroy() - FCoE software HBA tear-down function
518 * @lport: fc_lport to destroy
520 static void fcoe_if_destroy(struct fc_lport *lport)
522 struct fcoe_port *port = lport_priv(lport);
523 struct fcoe_interface *fcoe = port->fcoe;
524 struct net_device *netdev = fcoe->netdev;
526 FCOE_NETDEV_DBG(netdev, "Destroying interface\n");
528 /* Logout of the fabric */
529 fc_fabric_logoff(lport);
531 /* Remove the instance from fcoe's list */
532 fcoe_hostlist_remove(lport);
534 /* clean up netdev configurations */
535 fcoe_netdev_cleanup(port);
537 /* tear-down the FCoE controller */
538 fcoe_ctlr_destroy(&port->ctlr);
540 /* Free queued packets for the per-CPU receive threads */
541 fcoe_percpu_clean(lport);
543 /* Cleanup the fc_lport */
544 fc_lport_destroy(lport);
545 fc_fcp_destroy(lport);
547 /* Detach from the scsi-ml */
548 fc_remove_host(lport->host);
549 scsi_remove_host(lport->host);
551 /* There are no more rports or I/O, free the EM */
552 fc_exch_mgr_free(lport);
554 /* Free existing skbs */
555 fcoe_clean_pending_queue(lport);
558 del_timer_sync(&port->timer);
560 /* Free memory used by statistical counters */
561 fc_lport_free_stats(lport);
563 /* Release the net_device and Scsi_Host */
565 scsi_host_put(lport->host);
566 kfree(fcoe); /* TODO, should be refcounted */
570 * fcoe_ddp_setup - calls LLD's ddp_setup through net_device
571 * @lp: the corresponding fc_lport
572 * @xid: the exchange id for this ddp transfer
573 * @sgl: the scatterlist describing this transfer
574 * @sgc: number of sg items
578 static int fcoe_ddp_setup(struct fc_lport *lp, u16 xid,
579 struct scatterlist *sgl, unsigned int sgc)
581 struct net_device *n = fcoe_netdev(lp);
583 if (n->netdev_ops && n->netdev_ops->ndo_fcoe_ddp_setup)
584 return n->netdev_ops->ndo_fcoe_ddp_setup(n, xid, sgl, sgc);
590 * fcoe_ddp_done - calls LLD's ddp_done through net_device
591 * @lp: the corresponding fc_lport
592 * @xid: the exchange id for this ddp transfer
594 * Returns : the length of data that have been completed by ddp
596 static int fcoe_ddp_done(struct fc_lport *lp, u16 xid)
598 struct net_device *n = fcoe_netdev(lp);
600 if (n->netdev_ops && n->netdev_ops->ndo_fcoe_ddp_done)
601 return n->netdev_ops->ndo_fcoe_ddp_done(n, xid);
605 static struct libfc_function_template fcoe_libfc_fcn_templ = {
606 .frame_send = fcoe_xmit,
607 .ddp_setup = fcoe_ddp_setup,
608 .ddp_done = fcoe_ddp_done,
612 * fcoe_if_create() - this function creates the fcoe interface
613 * @netdev: pointer the associated netdevice
614 * @parent: device pointer to be the parent in sysfs for the SCSI host
616 * Creates fc_lport struct and scsi_host for lport, configures lport
617 * and starts fabric login.
619 * Returns : The allocated fc_lport or an error pointer
621 static struct fc_lport *fcoe_if_create(struct net_device *netdev,
622 struct device *parent)
625 struct fc_lport *lport = NULL;
626 struct fcoe_port *port;
627 struct fcoe_interface *fcoe;
628 struct Scsi_Host *shost;
630 FCOE_NETDEV_DBG(netdev, "Create Interface\n");
632 fcoe = kzalloc(sizeof(*fcoe), GFP_KERNEL);
634 FCOE_NETDEV_DBG(netdev, "Could not allocate fcoe structure\n");
639 shost = libfc_host_alloc(&fcoe_shost_template,
640 sizeof(struct fcoe_port));
642 FCOE_NETDEV_DBG(netdev, "Could not allocate host structure\n");
646 lport = shost_priv(shost);
647 port = lport_priv(lport);
652 /* configure fc_lport, e.g., em */
653 rc = fcoe_lport_config(lport);
655 FCOE_NETDEV_DBG(netdev, "Could not configure lport for the "
663 fcoe_ctlr_init(&port->ctlr);
664 port->ctlr.send = fcoe_fip_send;
665 port->ctlr.update_mac = fcoe_update_src_mac;
667 /* configure lport network properties */
668 rc = fcoe_netdev_config(lport, netdev);
670 FCOE_NETDEV_DBG(netdev, "Could not configure netdev for the "
672 goto out_netdev_cleanup;
675 /* configure lport scsi host properties */
676 rc = fcoe_shost_config(lport, shost, parent);
678 FCOE_NETDEV_DBG(netdev, "Could not configure shost for the "
680 goto out_netdev_cleanup;
683 /* Initialize the library */
684 rc = fcoe_libfc_config(lport, &fcoe_libfc_fcn_templ);
686 FCOE_NETDEV_DBG(netdev, "Could not configure libfc for the "
692 * fcoe_em_alloc() and fcoe_hostlist_add() both
693 * need to be atomic under fcoe_hostlist_lock
694 * since fcoe_em_alloc() looks for an existing EM
695 * instance on host list updated by fcoe_hostlist_add().
697 write_lock(&fcoe_hostlist_lock);
698 /* lport exch manager allocation */
699 rc = fcoe_em_config(lport);
701 FCOE_NETDEV_DBG(netdev, "Could not configure the EM for the "
706 /* add to lports list */
707 fcoe_hostlist_add(lport);
708 write_unlock(&fcoe_hostlist_lock);
710 lport->boot_time = jiffies;
712 fc_fabric_login(lport);
714 if (!fcoe_link_ok(lport))
715 fcoe_ctlr_link_up(&port->ctlr);
722 fc_exch_mgr_free(lport);
724 fcoe_netdev_cleanup(port);
726 scsi_host_put(lport->host);
734 * fcoe_if_init() - attach to scsi transport
736 * Returns : 0 on success
738 static int __init fcoe_if_init(void)
740 /* attach to scsi transport */
741 scsi_transport_fcoe_sw =
742 fc_attach_transport(&fcoe_transport_function);
744 if (!scsi_transport_fcoe_sw) {
745 printk(KERN_ERR "fcoe: Failed to attach to the FC transport\n");
753 * fcoe_if_exit() - detach from scsi transport
755 * Returns : 0 on success
757 int __exit fcoe_if_exit(void)
759 fc_release_transport(scsi_transport_fcoe_sw);
764 * fcoe_percpu_thread_create() - Create a receive thread for an online cpu
765 * @cpu: cpu index for the online cpu
767 static void fcoe_percpu_thread_create(unsigned int cpu)
769 struct fcoe_percpu_s *p;
770 struct task_struct *thread;
772 p = &per_cpu(fcoe_percpu, cpu);
774 thread = kthread_create(fcoe_percpu_receive_thread,
775 (void *)p, "fcoethread/%d", cpu);
777 if (likely(!IS_ERR(p->thread))) {
778 kthread_bind(thread, cpu);
779 wake_up_process(thread);
781 spin_lock_bh(&p->fcoe_rx_list.lock);
783 spin_unlock_bh(&p->fcoe_rx_list.lock);
788 * fcoe_percpu_thread_destroy() - removes the rx thread for the given cpu
789 * @cpu: cpu index the rx thread is to be removed
791 * Destroys a per-CPU Rx thread. Any pending skbs are moved to the
792 * current CPU's Rx thread. If the thread being destroyed is bound to
793 * the CPU processing this context the skbs will be freed.
795 static void fcoe_percpu_thread_destroy(unsigned int cpu)
797 struct fcoe_percpu_s *p;
798 struct task_struct *thread;
799 struct page *crc_eof;
802 struct fcoe_percpu_s *p0;
803 unsigned targ_cpu = smp_processor_id();
804 #endif /* CONFIG_SMP */
806 FCOE_DBG("Destroying receive thread for CPU %d\n", cpu);
808 /* Prevent any new skbs from being queued for this CPU. */
809 p = &per_cpu(fcoe_percpu, cpu);
810 spin_lock_bh(&p->fcoe_rx_list.lock);
813 crc_eof = p->crc_eof_page;
814 p->crc_eof_page = NULL;
815 p->crc_eof_offset = 0;
816 spin_unlock_bh(&p->fcoe_rx_list.lock);
820 * Don't bother moving the skb's if this context is running
821 * on the same CPU that is having its thread destroyed. This
822 * can easily happen when the module is removed.
824 if (cpu != targ_cpu) {
825 p0 = &per_cpu(fcoe_percpu, targ_cpu);
826 spin_lock_bh(&p0->fcoe_rx_list.lock);
828 FCOE_DBG("Moving frames from CPU %d to CPU %d\n",
831 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
832 __skb_queue_tail(&p0->fcoe_rx_list, skb);
833 spin_unlock_bh(&p0->fcoe_rx_list.lock);
836 * The targeted CPU is not initialized and cannot accept
837 * new skbs. Unlock the targeted CPU and drop the skbs
838 * on the CPU that is going offline.
840 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
842 spin_unlock_bh(&p0->fcoe_rx_list.lock);
846 * This scenario occurs when the module is being removed
847 * and all threads are being destroyed. skbs will continue
848 * to be shifted from the CPU thread that is being removed
849 * to the CPU thread associated with the CPU that is processing
850 * the module removal. Once there is only one CPU Rx thread it
851 * will reach this case and we will drop all skbs and later
854 spin_lock_bh(&p->fcoe_rx_list.lock);
855 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
857 spin_unlock_bh(&p->fcoe_rx_list.lock);
861 * This a non-SMP scenario where the singular Rx thread is
862 * being removed. Free all skbs and stop the thread.
864 spin_lock_bh(&p->fcoe_rx_list.lock);
865 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
867 spin_unlock_bh(&p->fcoe_rx_list.lock);
871 kthread_stop(thread);
878 * fcoe_cpu_callback() - fcoe cpu hotplug event callback
879 * @nfb: callback data block
880 * @action: event triggering the callback
881 * @hcpu: index for the cpu of this event
883 * This creates or destroys per cpu data for fcoe
885 * Returns NOTIFY_OK always.
887 static int fcoe_cpu_callback(struct notifier_block *nfb,
888 unsigned long action, void *hcpu)
890 unsigned cpu = (unsigned long)hcpu;
894 case CPU_ONLINE_FROZEN:
895 FCOE_DBG("CPU %x online: Create Rx thread\n", cpu);
896 fcoe_percpu_thread_create(cpu);
899 case CPU_DEAD_FROZEN:
900 FCOE_DBG("CPU %x offline: Remove Rx thread\n", cpu);
901 fcoe_percpu_thread_destroy(cpu);
909 static struct notifier_block fcoe_cpu_notifier = {
910 .notifier_call = fcoe_cpu_callback,
914 * fcoe_rcv() - this is the fcoe receive function called by NET_RX_SOFTIRQ
915 * @skb: the receive skb
916 * @dev: associated net device
918 * @olddev: last device
920 * this function will receive the packet and build fc frame and pass it up
922 * Returns: 0 for success
924 int fcoe_rcv(struct sk_buff *skb, struct net_device *dev,
925 struct packet_type *ptype, struct net_device *olddev)
928 struct fcoe_rcv_info *fr;
929 struct fcoe_port *port;
930 struct fc_frame_header *fh;
931 struct fcoe_percpu_s *fps;
934 port = container_of(ptype, struct fcoe_port, fcoe_packet_type);
936 if (unlikely(lp == NULL)) {
937 FCOE_NETDEV_DBG(dev, "Cannot find hba structure");
943 FCOE_NETDEV_DBG(dev, "skb_info: len:%d data_len:%d head:%p "
944 "data:%p tail:%p end:%p sum:%d dev:%s",
945 skb->len, skb->data_len, skb->head, skb->data,
946 skb_tail_pointer(skb), skb_end_pointer(skb),
947 skb->csum, skb->dev ? skb->dev->name : "<NULL>");
949 /* check for FCOE packet type */
950 if (unlikely(eth_hdr(skb)->h_proto != htons(ETH_P_FCOE))) {
951 FCOE_NETDEV_DBG(dev, "Wrong FC type frame");
956 * Check for minimum frame length, and make sure required FCoE
957 * and FC headers are pulled into the linear data area.
959 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
960 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
963 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
964 fh = (struct fc_frame_header *) skb_transport_header(skb);
966 fr = fcoe_dev_from_skb(skb);
971 * In case the incoming frame's exchange is originated from
972 * the initiator, then received frame's exchange id is ANDed
973 * with fc_cpu_mask bits to get the same cpu on which exchange
974 * was originated, otherwise just use the current cpu.
976 if (ntoh24(fh->fh_f_ctl) & FC_FC_EX_CTX)
977 cpu = ntohs(fh->fh_ox_id) & fc_cpu_mask;
979 cpu = smp_processor_id();
981 fps = &per_cpu(fcoe_percpu, cpu);
982 spin_lock_bh(&fps->fcoe_rx_list.lock);
983 if (unlikely(!fps->thread)) {
985 * The targeted CPU is not ready, let's target
986 * the first CPU now. For non-SMP systems this
987 * will check the same CPU twice.
989 FCOE_NETDEV_DBG(dev, "CPU is online, but no receive thread "
990 "ready for incoming skb- using first online "
993 spin_unlock_bh(&fps->fcoe_rx_list.lock);
994 cpu = first_cpu(cpu_online_map);
995 fps = &per_cpu(fcoe_percpu, cpu);
996 spin_lock_bh(&fps->fcoe_rx_list.lock);
998 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1004 * We now have a valid CPU that we're targeting for
1005 * this skb. We also have this receive thread locked,
1006 * so we're free to queue skbs into it's queue.
1008 __skb_queue_tail(&fps->fcoe_rx_list, skb);
1009 if (fps->fcoe_rx_list.qlen == 1)
1010 wake_up_process(fps->thread);
1012 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1016 fc_lport_get_stats(lp)->ErrorFrames++;
1024 * fcoe_start_io() - pass to netdev to start xmit for fcoe
1025 * @skb: the skb to be xmitted
1027 * Returns: 0 for success
1029 static inline int fcoe_start_io(struct sk_buff *skb)
1034 rc = dev_queue_xmit(skb);
1042 * fcoe_get_paged_crc_eof() - in case we need to alloc a page for crc_eof
1043 * @skb: the skb to be xmitted
1046 * Returns: 0 for success
1048 static int fcoe_get_paged_crc_eof(struct sk_buff *skb, int tlen)
1050 struct fcoe_percpu_s *fps;
1053 fps = &get_cpu_var(fcoe_percpu);
1054 page = fps->crc_eof_page;
1056 page = alloc_page(GFP_ATOMIC);
1058 put_cpu_var(fcoe_percpu);
1061 fps->crc_eof_page = page;
1062 fps->crc_eof_offset = 0;
1066 skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags, page,
1067 fps->crc_eof_offset, tlen);
1069 skb->data_len += tlen;
1070 skb->truesize += tlen;
1071 fps->crc_eof_offset += sizeof(struct fcoe_crc_eof);
1073 if (fps->crc_eof_offset >= PAGE_SIZE) {
1074 fps->crc_eof_page = NULL;
1075 fps->crc_eof_offset = 0;
1078 put_cpu_var(fcoe_percpu);
1083 * fcoe_fc_crc() - calculates FC CRC in this fcoe skb
1084 * @fp: the fc_frame containing data to be checksummed
1086 * This uses crc32() to calculate the crc for port frame
1087 * Return : 32 bit crc
1089 u32 fcoe_fc_crc(struct fc_frame *fp)
1091 struct sk_buff *skb = fp_skb(fp);
1092 struct skb_frag_struct *frag;
1093 unsigned char *data;
1094 unsigned long off, len, clen;
1098 crc = crc32(~0, skb->data, skb_headlen(skb));
1100 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1101 frag = &skb_shinfo(skb)->frags[i];
1102 off = frag->page_offset;
1105 clen = min(len, PAGE_SIZE - (off & ~PAGE_MASK));
1106 data = kmap_atomic(frag->page + (off >> PAGE_SHIFT),
1107 KM_SKB_DATA_SOFTIRQ);
1108 crc = crc32(crc, data + (off & ~PAGE_MASK), clen);
1109 kunmap_atomic(data, KM_SKB_DATA_SOFTIRQ);
1118 * fcoe_xmit() - FCoE frame transmit function
1119 * @lp: the associated local fcoe
1120 * @fp: the fc_frame to be transmitted
1122 * Return : 0 for success
1124 int fcoe_xmit(struct fc_lport *lp, struct fc_frame *fp)
1129 struct fcoe_crc_eof *cp;
1130 struct sk_buff *skb;
1131 struct fcoe_dev_stats *stats;
1132 struct fc_frame_header *fh;
1133 unsigned int hlen; /* header length implies the version */
1134 unsigned int tlen; /* trailer length */
1135 unsigned int elen; /* eth header, may include vlan */
1136 struct fcoe_port *port;
1138 struct fcoe_hdr *hp;
1140 WARN_ON((fr_len(fp) % sizeof(u32)) != 0);
1142 port = lport_priv(lp);
1143 fh = fc_frame_header_get(fp);
1145 wlen = skb->len / FCOE_WORD_TO_BYTE;
1152 if (unlikely(fh->fh_r_ctl == FC_RCTL_ELS_REQ) &&
1153 fcoe_ctlr_els_send(&port->ctlr, skb))
1159 elen = sizeof(struct ethhdr);
1160 hlen = sizeof(struct fcoe_hdr);
1161 tlen = sizeof(struct fcoe_crc_eof);
1162 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
1165 if (likely(lp->crc_offload)) {
1166 skb->ip_summed = CHECKSUM_PARTIAL;
1167 skb->csum_start = skb_headroom(skb);
1168 skb->csum_offset = skb->len;
1171 skb->ip_summed = CHECKSUM_NONE;
1172 crc = fcoe_fc_crc(fp);
1175 /* copy port crc and eof to the skb buff */
1176 if (skb_is_nonlinear(skb)) {
1178 if (fcoe_get_paged_crc_eof(skb, tlen)) {
1182 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
1183 cp = kmap_atomic(frag->page, KM_SKB_DATA_SOFTIRQ)
1184 + frag->page_offset;
1186 cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
1189 memset(cp, 0, sizeof(*cp));
1191 cp->fcoe_crc32 = cpu_to_le32(~crc);
1193 if (skb_is_nonlinear(skb)) {
1194 kunmap_atomic(cp, KM_SKB_DATA_SOFTIRQ);
1198 /* adjust skb network/transport offsets to match mac/fcoe/port */
1199 skb_push(skb, elen + hlen);
1200 skb_reset_mac_header(skb);
1201 skb_reset_network_header(skb);
1202 skb->mac_len = elen;
1203 skb->protocol = htons(ETH_P_FCOE);
1204 skb->dev = port->fcoe->netdev;
1206 /* fill up mac and fcoe headers */
1208 eh->h_proto = htons(ETH_P_FCOE);
1209 if (port->ctlr.map_dest)
1210 fc_fcoe_set_mac(eh->h_dest, fh->fh_d_id);
1212 /* insert GW address */
1213 memcpy(eh->h_dest, port->ctlr.dest_addr, ETH_ALEN);
1215 if (unlikely(port->ctlr.flogi_oxid != FC_XID_UNKNOWN))
1216 memcpy(eh->h_source, port->ctlr.ctl_src_addr, ETH_ALEN);
1218 memcpy(eh->h_source, port->ctlr.data_src_addr, ETH_ALEN);
1220 hp = (struct fcoe_hdr *)(eh + 1);
1221 memset(hp, 0, sizeof(*hp));
1223 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
1226 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
1227 if (lp->seq_offload && fr_max_payload(fp)) {
1228 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
1229 skb_shinfo(skb)->gso_size = fr_max_payload(fp);
1231 skb_shinfo(skb)->gso_type = 0;
1232 skb_shinfo(skb)->gso_size = 0;
1234 /* update tx stats: regardless if LLD fails */
1235 stats = fc_lport_get_stats(lp);
1237 stats->TxWords += wlen;
1239 /* send down to lld */
1241 if (port->fcoe_pending_queue.qlen)
1242 fcoe_check_wait_queue(lp, skb);
1243 else if (fcoe_start_io(skb))
1244 fcoe_check_wait_queue(lp, skb);
1250 * fcoe_percpu_receive_thread() - recv thread per cpu
1251 * @arg: ptr to the fcoe per cpu struct
1253 * Return: 0 for success
1255 int fcoe_percpu_receive_thread(void *arg)
1257 struct fcoe_percpu_s *p = arg;
1259 struct fc_lport *lp;
1260 struct fcoe_rcv_info *fr;
1261 struct fcoe_dev_stats *stats;
1262 struct fc_frame_header *fh;
1263 struct sk_buff *skb;
1264 struct fcoe_crc_eof crc_eof;
1265 struct fc_frame *fp;
1267 struct fcoe_port *port;
1268 struct fcoe_hdr *hp;
1270 set_user_nice(current, -20);
1272 while (!kthread_should_stop()) {
1274 spin_lock_bh(&p->fcoe_rx_list.lock);
1275 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) == NULL) {
1276 set_current_state(TASK_INTERRUPTIBLE);
1277 spin_unlock_bh(&p->fcoe_rx_list.lock);
1279 set_current_state(TASK_RUNNING);
1280 if (kthread_should_stop())
1282 spin_lock_bh(&p->fcoe_rx_list.lock);
1284 spin_unlock_bh(&p->fcoe_rx_list.lock);
1285 fr = fcoe_dev_from_skb(skb);
1287 if (unlikely(lp == NULL)) {
1288 FCOE_NETDEV_DBG(skb->dev, "Invalid HBA Structure");
1293 FCOE_NETDEV_DBG(skb->dev, "skb_info: len:%d data_len:%d "
1294 "head:%p data:%p tail:%p end:%p sum:%d dev:%s",
1295 skb->len, skb->data_len,
1296 skb->head, skb->data, skb_tail_pointer(skb),
1297 skb_end_pointer(skb), skb->csum,
1298 skb->dev ? skb->dev->name : "<NULL>");
1301 * Save source MAC address before discarding header.
1303 port = lport_priv(lp);
1304 if (skb_is_nonlinear(skb))
1305 skb_linearize(skb); /* not ideal */
1306 mac = eth_hdr(skb)->h_source;
1309 * Frame length checks and setting up the header pointers
1310 * was done in fcoe_rcv already.
1312 hp = (struct fcoe_hdr *) skb_network_header(skb);
1313 fh = (struct fc_frame_header *) skb_transport_header(skb);
1315 stats = fc_lport_get_stats(lp);
1316 if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) {
1317 if (stats->ErrorFrames < 5)
1318 printk(KERN_WARNING "fcoe: FCoE version "
1319 "mismatch: The frame has "
1320 "version %x, but the "
1321 "initiator supports version "
1322 "%x\n", FC_FCOE_DECAPS_VER(hp),
1324 stats->ErrorFrames++;
1329 skb_pull(skb, sizeof(struct fcoe_hdr));
1330 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
1333 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
1335 fp = (struct fc_frame *)skb;
1338 fr_sof(fp) = hp->fcoe_sof;
1340 /* Copy out the CRC and EOF trailer for access */
1341 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof))) {
1345 fr_eof(fp) = crc_eof.fcoe_eof;
1346 fr_crc(fp) = crc_eof.fcoe_crc32;
1347 if (pskb_trim(skb, fr_len)) {
1353 * We only check CRC if no offload is available and if it is
1354 * it's solicited data, in which case, the FCP layer would
1355 * check it during the copy.
1357 if (lp->crc_offload && skb->ip_summed == CHECKSUM_UNNECESSARY)
1358 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1360 fr_flags(fp) |= FCPHF_CRC_UNCHECKED;
1362 fh = fc_frame_header_get(fp);
1363 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA &&
1364 fh->fh_type == FC_TYPE_FCP) {
1365 fc_exch_recv(lp, fp);
1368 if (fr_flags(fp) & FCPHF_CRC_UNCHECKED) {
1369 if (le32_to_cpu(fr_crc(fp)) !=
1370 ~crc32(~0, skb->data, fr_len)) {
1371 if (stats->InvalidCRCCount < 5)
1372 printk(KERN_WARNING "fcoe: dropping "
1373 "frame with CRC error\n");
1374 stats->InvalidCRCCount++;
1375 stats->ErrorFrames++;
1379 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1381 if (unlikely(port->ctlr.flogi_oxid != FC_XID_UNKNOWN) &&
1382 fcoe_ctlr_recv_flogi(&port->ctlr, fp, mac)) {
1386 fc_exch_recv(lp, fp);
1392 * fcoe_check_wait_queue() - attempt to clear the transmit backlog
1395 * This empties the wait_queue, dequeue the head of the wait_queue queue
1396 * and calls fcoe_start_io() for each packet, if all skb have been
1397 * transmitted, return qlen or -1 if a error occurs, then restore
1398 * wait_queue and try again later.
1400 * The wait_queue is used when the skb transmit fails. skb will go
1401 * in the wait_queue which will be emptied by the timer function or
1402 * by the next skb transmit.
1404 static void fcoe_check_wait_queue(struct fc_lport *lp, struct sk_buff *skb)
1406 struct fcoe_port *port = lport_priv(lp);
1409 spin_lock_bh(&port->fcoe_pending_queue.lock);
1412 __skb_queue_tail(&port->fcoe_pending_queue, skb);
1414 if (port->fcoe_pending_queue_active)
1416 port->fcoe_pending_queue_active = 1;
1418 while (port->fcoe_pending_queue.qlen) {
1419 /* keep qlen > 0 until fcoe_start_io succeeds */
1420 port->fcoe_pending_queue.qlen++;
1421 skb = __skb_dequeue(&port->fcoe_pending_queue);
1423 spin_unlock_bh(&port->fcoe_pending_queue.lock);
1424 rc = fcoe_start_io(skb);
1425 spin_lock_bh(&port->fcoe_pending_queue.lock);
1428 __skb_queue_head(&port->fcoe_pending_queue, skb);
1429 /* undo temporary increment above */
1430 port->fcoe_pending_queue.qlen--;
1433 /* undo temporary increment above */
1434 port->fcoe_pending_queue.qlen--;
1437 if (port->fcoe_pending_queue.qlen < FCOE_LOW_QUEUE_DEPTH)
1439 if (port->fcoe_pending_queue.qlen && !timer_pending(&port->timer))
1440 mod_timer(&port->timer, jiffies + 2);
1441 port->fcoe_pending_queue_active = 0;
1443 if (port->fcoe_pending_queue.qlen > FCOE_MAX_QUEUE_DEPTH)
1445 spin_unlock_bh(&port->fcoe_pending_queue.lock);
1450 * fcoe_dev_setup() - setup link change notification interface
1452 static void fcoe_dev_setup(void)
1454 register_netdevice_notifier(&fcoe_notifier);
1458 * fcoe_dev_cleanup() - cleanup link change notification interface
1460 static void fcoe_dev_cleanup(void)
1462 unregister_netdevice_notifier(&fcoe_notifier);
1466 * fcoe_device_notification() - netdev event notification callback
1467 * @notifier: context of the notification
1468 * @event: type of event
1469 * @ptr: fixed array for output parsed ifname
1471 * This function is called by the ethernet driver in case of link change event
1473 * Returns: 0 for success
1475 static int fcoe_device_notification(struct notifier_block *notifier,
1476 ulong event, void *ptr)
1478 struct fc_lport *lp = NULL;
1479 struct net_device *netdev = ptr;
1480 struct fcoe_interface *fcoe;
1481 struct fcoe_port *port = NULL;
1482 struct fcoe_dev_stats *stats;
1483 u32 link_possible = 1;
1487 read_lock(&fcoe_hostlist_lock);
1488 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
1490 if (fcoe->netdev == netdev) {
1495 read_unlock(&fcoe_hostlist_lock);
1503 case NETDEV_GOING_DOWN:
1509 case NETDEV_CHANGEMTU:
1510 mfs = netdev->mtu - (sizeof(struct fcoe_hdr) +
1511 sizeof(struct fcoe_crc_eof));
1512 if (mfs >= FC_MIN_MAX_FRAME)
1513 fc_set_mfs(lp, mfs);
1515 case NETDEV_REGISTER:
1518 FCOE_NETDEV_DBG(netdev, "Unknown event %ld "
1519 "from netdev netlink\n", event);
1521 if (link_possible && !fcoe_link_ok(lp))
1522 fcoe_ctlr_link_up(&port->ctlr);
1523 else if (fcoe_ctlr_link_down(&port->ctlr)) {
1524 stats = fc_lport_get_stats(lp);
1525 stats->LinkFailureCount++;
1526 fcoe_clean_pending_queue(lp);
1533 * fcoe_if_to_netdev() - parse a name buffer to get netdev
1534 * @buffer: incoming buffer to be copied
1536 * Returns: NULL or ptr to net_device
1538 static struct net_device *fcoe_if_to_netdev(const char *buffer)
1541 char ifname[IFNAMSIZ + 2];
1544 strlcpy(ifname, buffer, IFNAMSIZ);
1545 cp = ifname + strlen(ifname);
1546 while (--cp >= ifname && *cp == '\n')
1548 return dev_get_by_name(&init_net, ifname);
1554 * fcoe_netdev_to_module_owner() - finds out the driver module of the netdev
1555 * @netdev: the target netdev
1557 * Returns: ptr to the struct module, NULL for failure
1559 static struct module *
1560 fcoe_netdev_to_module_owner(const struct net_device *netdev)
1567 dev = netdev->dev.parent;
1574 return dev->driver->owner;
1578 * fcoe_ethdrv_get() - Hold the Ethernet driver
1579 * @netdev: the target netdev
1581 * Holds the Ethernet driver module by try_module_get() for
1582 * the corresponding netdev.
1584 * Returns: 0 for success
1586 static int fcoe_ethdrv_get(const struct net_device *netdev)
1588 struct module *owner;
1590 owner = fcoe_netdev_to_module_owner(netdev);
1592 FCOE_NETDEV_DBG(netdev, "Hold driver module %s\n",
1593 module_name(owner));
1594 return try_module_get(owner);
1600 * fcoe_ethdrv_put() - Release the Ethernet driver
1601 * @netdev: the target netdev
1603 * Releases the Ethernet driver module by module_put for
1604 * the corresponding netdev.
1606 * Returns: 0 for success
1608 static int fcoe_ethdrv_put(const struct net_device *netdev)
1610 struct module *owner;
1612 owner = fcoe_netdev_to_module_owner(netdev);
1614 FCOE_NETDEV_DBG(netdev, "Release driver module %s\n",
1615 module_name(owner));
1623 * fcoe_destroy() - handles the destroy from sysfs
1624 * @buffer: expected to be an eth if name
1625 * @kp: associated kernel param
1627 * Returns: 0 for success
1629 static int fcoe_destroy(const char *buffer, struct kernel_param *kp)
1631 struct net_device *netdev;
1632 struct fc_lport *lport;
1635 netdev = fcoe_if_to_netdev(buffer);
1640 /* look for existing lport */
1641 lport = fcoe_hostlist_lookup(netdev);
1646 fcoe_if_destroy(lport);
1647 fcoe_ethdrv_put(netdev);
1656 * fcoe_create() - Handles the create call from sysfs
1657 * @buffer: expected to be an eth if name
1658 * @kp: associated kernel param
1660 * Returns: 0 for success
1662 static int fcoe_create(const char *buffer, struct kernel_param *kp)
1665 struct fc_lport *lport;
1666 struct net_device *netdev;
1668 netdev = fcoe_if_to_netdev(buffer);
1673 /* look for existing lport */
1674 if (fcoe_hostlist_lookup(netdev)) {
1678 fcoe_ethdrv_get(netdev);
1680 lport = fcoe_if_create(netdev, &netdev->dev);
1681 if (IS_ERR(lport)) {
1682 printk(KERN_ERR "fcoe: Failed to create interface (%s)\n",
1684 fcoe_ethdrv_put(netdev);
1695 module_param_call(create, fcoe_create, NULL, NULL, S_IWUSR);
1696 __MODULE_PARM_TYPE(create, "string");
1697 MODULE_PARM_DESC(create, "Create fcoe fcoe using net device passed in.");
1698 module_param_call(destroy, fcoe_destroy, NULL, NULL, S_IWUSR);
1699 __MODULE_PARM_TYPE(destroy, "string");
1700 MODULE_PARM_DESC(destroy, "Destroy fcoe fcoe");
1703 * fcoe_link_ok() - Check if link is ok for the fc_lport
1704 * @lp: ptr to the fc_lport
1706 * Any permanently-disqualifying conditions have been previously checked.
1707 * This also updates the speed setting, which may change with link for 100/1000.
1709 * This function should probably be checking for PAUSE support at some point
1710 * in the future. Currently Per-priority-pause is not determinable using
1711 * ethtool, so we shouldn't be restrictive until that problem is resolved.
1713 * Returns: 0 if link is OK for use by FCoE.
1716 int fcoe_link_ok(struct fc_lport *lp)
1718 struct fcoe_port *port = lport_priv(lp);
1719 struct net_device *dev = port->fcoe->netdev;
1720 struct ethtool_cmd ecmd = { ETHTOOL_GSET };
1722 if ((dev->flags & IFF_UP) && netif_carrier_ok(dev) &&
1723 (!dev_ethtool_get_settings(dev, &ecmd))) {
1724 lp->link_supported_speeds &=
1725 ~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
1726 if (ecmd.supported & (SUPPORTED_1000baseT_Half |
1727 SUPPORTED_1000baseT_Full))
1728 lp->link_supported_speeds |= FC_PORTSPEED_1GBIT;
1729 if (ecmd.supported & SUPPORTED_10000baseT_Full)
1730 lp->link_supported_speeds |=
1731 FC_PORTSPEED_10GBIT;
1732 if (ecmd.speed == SPEED_1000)
1733 lp->link_speed = FC_PORTSPEED_1GBIT;
1734 if (ecmd.speed == SPEED_10000)
1735 lp->link_speed = FC_PORTSPEED_10GBIT;
1743 * fcoe_percpu_clean() - Clear the pending skbs for an lport
1746 void fcoe_percpu_clean(struct fc_lport *lp)
1748 struct fcoe_percpu_s *pp;
1749 struct fcoe_rcv_info *fr;
1750 struct sk_buff_head *list;
1751 struct sk_buff *skb, *next;
1752 struct sk_buff *head;
1755 for_each_possible_cpu(cpu) {
1756 pp = &per_cpu(fcoe_percpu, cpu);
1757 spin_lock_bh(&pp->fcoe_rx_list.lock);
1758 list = &pp->fcoe_rx_list;
1760 for (skb = head; skb != (struct sk_buff *)list;
1763 fr = fcoe_dev_from_skb(skb);
1764 if (fr->fr_dev == lp) {
1765 __skb_unlink(skb, list);
1769 spin_unlock_bh(&pp->fcoe_rx_list.lock);
1774 * fcoe_clean_pending_queue() - Dequeue a skb and free it
1775 * @lp: the corresponding fc_lport
1779 void fcoe_clean_pending_queue(struct fc_lport *lp)
1781 struct fcoe_port *port = lport_priv(lp);
1782 struct sk_buff *skb;
1784 spin_lock_bh(&port->fcoe_pending_queue.lock);
1785 while ((skb = __skb_dequeue(&port->fcoe_pending_queue)) != NULL) {
1786 spin_unlock_bh(&port->fcoe_pending_queue.lock);
1788 spin_lock_bh(&port->fcoe_pending_queue.lock);
1790 spin_unlock_bh(&port->fcoe_pending_queue.lock);
1794 * fcoe_reset() - Resets the fcoe
1795 * @shost: shost the reset is from
1799 int fcoe_reset(struct Scsi_Host *shost)
1801 struct fc_lport *lport = shost_priv(shost);
1802 fc_lport_reset(lport);
1807 * fcoe_hostlist_lookup_port() - find the corresponding lport by a given device
1808 * @dev: this is currently ptr to net_device
1810 * Called with fcoe_hostlist_lock held.
1812 * Returns: NULL or the located fcoe_port
1814 static struct fcoe_interface *
1815 fcoe_hostlist_lookup_port(const struct net_device *dev)
1817 struct fcoe_interface *fcoe;
1819 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
1820 if (fcoe->netdev == dev)
1827 * fcoe_hostlist_lookup() - Find the corresponding lport by netdev
1828 * @netdev: ptr to net_device
1830 * Returns: 0 for success
1832 struct fc_lport *fcoe_hostlist_lookup(const struct net_device *netdev)
1834 struct fcoe_interface *fcoe;
1836 read_lock(&fcoe_hostlist_lock);
1837 fcoe = fcoe_hostlist_lookup_port(netdev);
1838 read_unlock(&fcoe_hostlist_lock);
1840 return (fcoe) ? fcoe->priv->ctlr.lp : NULL;
1844 * fcoe_hostlist_add() - Add a lport to lports list
1845 * @lp: ptr to the fc_lport to be added
1847 * Called with write fcoe_hostlist_lock held.
1849 * Returns: 0 for success
1851 int fcoe_hostlist_add(const struct fc_lport *lport)
1853 struct fcoe_interface *fcoe;
1854 struct fcoe_port *port;
1856 fcoe = fcoe_hostlist_lookup_port(fcoe_netdev(lport));
1858 port = lport_priv(lport);
1860 list_add_tail(&fcoe->list, &fcoe_hostlist);
1866 * fcoe_hostlist_remove() - remove a lport from lports list
1867 * @lp: ptr to the fc_lport to be removed
1869 * Returns: 0 for success
1871 int fcoe_hostlist_remove(const struct fc_lport *lport)
1873 struct fcoe_interface *fcoe;
1875 write_lock_bh(&fcoe_hostlist_lock);
1876 fcoe = fcoe_hostlist_lookup_port(fcoe_netdev(lport));
1878 list_del(&fcoe->list);
1879 write_unlock_bh(&fcoe_hostlist_lock);
1885 * fcoe_init() - fcoe module loading initialization
1887 * Returns 0 on success, negative on failure
1889 static int __init fcoe_init(void)
1893 struct fcoe_percpu_s *p;
1895 for_each_possible_cpu(cpu) {
1896 p = &per_cpu(fcoe_percpu, cpu);
1897 skb_queue_head_init(&p->fcoe_rx_list);
1900 for_each_online_cpu(cpu)
1901 fcoe_percpu_thread_create(cpu);
1903 /* Initialize per CPU interrupt thread */
1904 rc = register_hotcpu_notifier(&fcoe_cpu_notifier);
1908 /* Setup link change notification */
1911 rc = fcoe_if_init();
1918 for_each_online_cpu(cpu) {
1919 fcoe_percpu_thread_destroy(cpu);
1924 module_init(fcoe_init);
1927 * fcoe_exit() - fcoe module unloading cleanup
1929 * Returns 0 on success, negative on failure
1931 static void __exit fcoe_exit(void)
1934 struct fcoe_interface *fcoe, *tmp;
1938 /* releases the associated fcoe hosts */
1939 list_for_each_entry_safe(fcoe, tmp, &fcoe_hostlist, list)
1940 fcoe_if_destroy(fcoe->priv->ctlr.lp);
1942 unregister_hotcpu_notifier(&fcoe_cpu_notifier);
1944 for_each_online_cpu(cpu)
1945 fcoe_percpu_thread_destroy(cpu);
1947 /* detach from scsi transport */
1950 module_exit(fcoe_exit);