2 * Copyright(c) 2007 - 2008 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 static int debug_fcoe;
50 MODULE_AUTHOR("Open-FCoE.org");
51 MODULE_DESCRIPTION("FCoE");
52 MODULE_LICENSE("GPL v2");
55 LIST_HEAD(fcoe_hostlist);
56 DEFINE_RWLOCK(fcoe_hostlist_lock);
57 DEFINE_TIMER(fcoe_timer, NULL, 0, 0);
58 DEFINE_PER_CPU(struct fcoe_percpu_s, fcoe_percpu);
60 /* Function Prototypes */
61 static int fcoe_reset(struct Scsi_Host *shost);
62 static int fcoe_xmit(struct fc_lport *, struct fc_frame *);
63 static int fcoe_rcv(struct sk_buff *, struct net_device *,
64 struct packet_type *, struct net_device *);
65 static int fcoe_percpu_receive_thread(void *arg);
66 static void fcoe_clean_pending_queue(struct fc_lport *lp);
67 static void fcoe_percpu_clean(struct fc_lport *lp);
68 static int fcoe_link_ok(struct fc_lport *lp);
70 static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *);
71 static int fcoe_hostlist_add(const struct fc_lport *);
72 static int fcoe_hostlist_remove(const struct fc_lport *);
74 static int fcoe_check_wait_queue(struct fc_lport *);
75 static int fcoe_device_notification(struct notifier_block *, ulong, void *);
76 static void fcoe_dev_setup(void);
77 static void fcoe_dev_cleanup(void);
79 /* notification function from net device */
80 static struct notifier_block fcoe_notifier = {
81 .notifier_call = fcoe_device_notification,
84 static struct scsi_transport_template *scsi_transport_fcoe_sw;
86 struct fc_function_template fcoe_transport_function = {
87 .show_host_node_name = 1,
88 .show_host_port_name = 1,
89 .show_host_supported_classes = 1,
90 .show_host_supported_fc4s = 1,
91 .show_host_active_fc4s = 1,
92 .show_host_maxframe_size = 1,
94 .show_host_port_id = 1,
95 .show_host_supported_speeds = 1,
96 .get_host_speed = fc_get_host_speed,
98 .show_host_port_type = 1,
99 .get_host_port_state = fc_get_host_port_state,
100 .show_host_port_state = 1,
101 .show_host_symbolic_name = 1,
103 .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
104 .show_rport_maxframe_size = 1,
105 .show_rport_supported_classes = 1,
107 .show_host_fabric_name = 1,
108 .show_starget_node_name = 1,
109 .show_starget_port_name = 1,
110 .show_starget_port_id = 1,
111 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
112 .show_rport_dev_loss_tmo = 1,
113 .get_fc_host_stats = fc_get_host_stats,
114 .issue_fc_host_lip = fcoe_reset,
116 .terminate_rport_io = fc_rport_terminate_io,
119 static struct scsi_host_template fcoe_shost_template = {
120 .module = THIS_MODULE,
121 .name = "FCoE Driver",
122 .proc_name = FCOE_NAME,
123 .queuecommand = fc_queuecommand,
124 .eh_abort_handler = fc_eh_abort,
125 .eh_device_reset_handler = fc_eh_device_reset,
126 .eh_host_reset_handler = fc_eh_host_reset,
127 .slave_alloc = fc_slave_alloc,
128 .change_queue_depth = fc_change_queue_depth,
129 .change_queue_type = fc_change_queue_type,
132 .can_queue = FCOE_MAX_OUTSTANDING_COMMANDS,
133 .use_clustering = ENABLE_CLUSTERING,
134 .sg_tablesize = SG_ALL,
135 .max_sectors = 0xffff,
139 * fcoe_fip_recv - handle a received FIP frame.
140 * @skb: the receive skb
141 * @dev: associated &net_device
142 * @ptype: the &packet_type structure which was used to register this handler.
143 * @orig_dev: original receive &net_device, in case @dev is a bond.
145 * Returns: 0 for success
147 static int fcoe_fip_recv(struct sk_buff *skb, struct net_device *dev,
148 struct packet_type *ptype,
149 struct net_device *orig_dev)
151 struct fcoe_softc *fc;
153 fc = container_of(ptype, struct fcoe_softc, fip_packet_type);
154 fcoe_ctlr_recv(&fc->ctlr, skb);
159 * fcoe_fip_send() - send an Ethernet-encapsulated FIP frame.
160 * @fip: FCoE controller.
163 static void fcoe_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
165 skb->dev = fcoe_from_ctlr(fip)->real_dev;
170 * fcoe_update_src_mac() - Update Ethernet MAC filters.
171 * @fip: FCoE controller.
172 * @old: Unicast MAC address to delete if the MAC is non-zero.
173 * @new: Unicast MAC address to add.
175 * Remove any previously-set unicast MAC filter.
176 * Add secondary FCoE MAC address filter for our OUI.
178 static void fcoe_update_src_mac(struct fcoe_ctlr *fip, u8 *old, u8 *new)
180 struct fcoe_softc *fc;
182 fc = fcoe_from_ctlr(fip);
184 if (!is_zero_ether_addr(old))
185 dev_unicast_delete(fc->real_dev, old);
186 dev_unicast_add(fc->real_dev, new);
191 * fcoe_lport_config() - sets up the fc_lport
192 * @lp: ptr to the fc_lport
194 * Returns: 0 for success
196 static int fcoe_lport_config(struct fc_lport *lp)
200 lp->max_retry_count = 3;
201 lp->e_d_tov = 2 * 1000; /* FC-FS default */
202 lp->r_a_tov = 2 * 2 * 1000;
203 lp->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
204 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
206 fc_lport_init_stats(lp);
208 /* lport fc_lport related configuration */
211 /* offload related configuration */
222 * fcoe_netdev_cleanup() - clean up netdev configurations
223 * @fc: ptr to the fcoe_softc
225 void fcoe_netdev_cleanup(struct fcoe_softc *fc)
227 u8 flogi_maddr[ETH_ALEN];
229 /* Don't listen for Ethernet packets anymore */
230 dev_remove_pack(&fc->fcoe_packet_type);
231 dev_remove_pack(&fc->fip_packet_type);
233 /* Delete secondary MAC addresses */
235 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
236 dev_unicast_delete(fc->real_dev, flogi_maddr);
237 if (!is_zero_ether_addr(fc->ctlr.data_src_addr))
238 dev_unicast_delete(fc->real_dev, fc->ctlr.data_src_addr);
240 dev_unicast_delete(fc->real_dev, fc->ctlr.ctl_src_addr);
241 dev_mc_delete(fc->real_dev, FIP_ALL_ENODE_MACS, ETH_ALEN, 0);
246 * fcoe_netdev_config() - Set up netdev for SW FCoE
247 * @lp : ptr to the fc_lport
248 * @netdev : ptr to the associated netdevice struct
250 * Must be called after fcoe_lport_config() as it will use lport mutex
252 * Returns : 0 for success
254 static int fcoe_netdev_config(struct fc_lport *lp, struct net_device *netdev)
258 struct fcoe_softc *fc;
259 u8 flogi_maddr[ETH_ALEN];
260 struct netdev_hw_addr *ha;
262 /* Setup lport private data to point to fcoe softc */
265 fc->real_dev = netdev;
266 fc->phys_dev = netdev;
268 /* Require support for get_pauseparam ethtool op. */
269 if (netdev->priv_flags & IFF_802_1Q_VLAN)
270 fc->phys_dev = vlan_dev_real_dev(netdev);
272 /* Do not support for bonding device */
273 if ((fc->real_dev->priv_flags & IFF_MASTER_ALB) ||
274 (fc->real_dev->priv_flags & IFF_SLAVE_INACTIVE) ||
275 (fc->real_dev->priv_flags & IFF_MASTER_8023AD)) {
280 * Determine max frame size based on underlying device and optional
281 * user-configured limit. If the MFS is too low, fcoe_link_ok()
282 * will return 0, so do this first.
284 mfs = fc->real_dev->mtu - (sizeof(struct fcoe_hdr) +
285 sizeof(struct fcoe_crc_eof));
286 if (fc_set_mfs(lp, mfs))
289 /* offload features support */
290 if (fc->real_dev->features & NETIF_F_SG)
293 #ifdef NETIF_F_FCOE_CRC
294 if (netdev->features & NETIF_F_FCOE_CRC) {
296 printk(KERN_DEBUG "fcoe:%s supports FCCRC offload\n",
301 if (netdev->features & NETIF_F_FSO) {
303 lp->lso_max = netdev->gso_max_size;
304 printk(KERN_DEBUG "fcoe:%s supports LSO for max len 0x%x\n",
305 netdev->name, lp->lso_max);
308 if (netdev->fcoe_ddp_xid) {
310 lp->lro_xid = netdev->fcoe_ddp_xid;
311 printk(KERN_DEBUG "fcoe:%s supports LRO for max xid 0x%x\n",
312 netdev->name, lp->lro_xid);
314 skb_queue_head_init(&fc->fcoe_pending_queue);
315 fc->fcoe_pending_queue_active = 0;
317 /* look for SAN MAC address, if multiple SAN MACs exist, only
318 * use the first one for SPMA */
320 for_each_dev_addr(netdev, ha) {
321 if ((ha->type == NETDEV_HW_ADDR_T_SAN) &&
322 (is_valid_ether_addr(fc->ctlr.ctl_src_addr))) {
323 memcpy(fc->ctlr.ctl_src_addr, ha->addr, ETH_ALEN);
330 /* setup Source Mac Address */
332 memcpy(fc->ctlr.ctl_src_addr, fc->real_dev->dev_addr,
333 fc->real_dev->addr_len);
335 wwnn = fcoe_wwn_from_mac(fc->real_dev->dev_addr, 1, 0);
336 fc_set_wwnn(lp, wwnn);
337 /* XXX - 3rd arg needs to be vlan id */
338 wwpn = fcoe_wwn_from_mac(fc->real_dev->dev_addr, 2, 0);
339 fc_set_wwpn(lp, wwpn);
342 * Add FCoE MAC address as second unicast MAC address
343 * or enter promiscuous mode if not capable of listening
344 * for multiple unicast MACs.
347 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
348 dev_unicast_add(fc->real_dev, flogi_maddr);
350 dev_unicast_add(fc->real_dev, fc->ctlr.ctl_src_addr);
351 dev_mc_add(fc->real_dev, FIP_ALL_ENODE_MACS, ETH_ALEN, 0);
355 * setup the receive function from ethernet driver
356 * on the ethertype for the given device
358 fc->fcoe_packet_type.func = fcoe_rcv;
359 fc->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
360 fc->fcoe_packet_type.dev = fc->real_dev;
361 dev_add_pack(&fc->fcoe_packet_type);
363 fc->fip_packet_type.func = fcoe_fip_recv;
364 fc->fip_packet_type.type = htons(ETH_P_FIP);
365 fc->fip_packet_type.dev = fc->real_dev;
366 dev_add_pack(&fc->fip_packet_type);
372 * fcoe_shost_config() - Sets up fc_lport->host
373 * @lp : ptr to the fc_lport
374 * @shost : ptr to the associated scsi host
375 * @dev : device associated to scsi host
377 * Must be called after fcoe_lport_config() and fcoe_netdev_config()
379 * Returns : 0 for success
381 static int fcoe_shost_config(struct fc_lport *lp, struct Scsi_Host *shost,
386 /* lport scsi host config */
389 lp->host->max_lun = FCOE_MAX_LUN;
390 lp->host->max_id = FCOE_MAX_FCP_TARGET;
391 lp->host->max_channel = 0;
392 lp->host->transportt = scsi_transport_fcoe_sw;
394 /* add the new host to the SCSI-ml */
395 rc = scsi_add_host(lp->host, dev);
397 FC_DBG("fcoe_shost_config:error on scsi_add_host\n");
400 sprintf(fc_host_symbolic_name(lp->host), "%s v%s over %s",
401 FCOE_NAME, FCOE_VERSION,
402 fcoe_netdev(lp)->name);
408 * fcoe_em_config() - allocates em for this lport
409 * @lp: the port that em is to allocated for
411 * Returns : 0 on success
413 static inline int fcoe_em_config(struct fc_lport *lp)
417 lp->emp = fc_exch_mgr_alloc(lp, FC_CLASS_3,
418 FCOE_MIN_XID, FCOE_MAX_XID);
426 * fcoe_if_destroy() - FCoE software HBA tear-down function
427 * @netdev: ptr to the associated net_device
429 * Returns: 0 if link is OK for use by FCoE.
431 static int fcoe_if_destroy(struct net_device *netdev)
433 struct fc_lport *lp = NULL;
434 struct fcoe_softc *fc;
438 printk(KERN_DEBUG "fcoe_if_destroy:interface on %s\n",
441 lp = fcoe_hostlist_lookup(netdev);
447 /* Logout of the fabric */
448 fc_fabric_logoff(lp);
450 /* Remove the instance from fcoe's list */
451 fcoe_hostlist_remove(lp);
453 /* clean up netdev configurations */
454 fcoe_netdev_cleanup(fc);
456 /* tear-down the FCoE controller */
457 fcoe_ctlr_destroy(&fc->ctlr);
459 /* Cleanup the fc_lport */
460 fc_lport_destroy(lp);
463 /* Detach from the scsi-ml */
464 fc_remove_host(lp->host);
465 scsi_remove_host(lp->host);
467 /* There are no more rports or I/O, free the EM */
469 fc_exch_mgr_free(lp->emp);
471 /* Free the per-CPU receive threads */
472 fcoe_percpu_clean(lp);
474 /* Free existing skbs */
475 fcoe_clean_pending_queue(lp);
477 /* Free memory used by statistical counters */
478 fc_lport_free_stats(lp);
480 /* Release the net_device and Scsi_Host */
481 dev_put(fc->real_dev);
482 scsi_host_put(lp->host);
488 * fcoe_ddp_setup - calls LLD's ddp_setup through net_device
489 * @lp: the corresponding fc_lport
490 * @xid: the exchange id for this ddp transfer
491 * @sgl: the scatterlist describing this transfer
492 * @sgc: number of sg items
496 static int fcoe_ddp_setup(struct fc_lport *lp, u16 xid,
497 struct scatterlist *sgl, unsigned int sgc)
499 struct net_device *n = fcoe_netdev(lp);
501 if (n->netdev_ops && n->netdev_ops->ndo_fcoe_ddp_setup)
502 return n->netdev_ops->ndo_fcoe_ddp_setup(n, xid, sgl, sgc);
508 * fcoe_ddp_done - calls LLD's ddp_done through net_device
509 * @lp: the corresponding fc_lport
510 * @xid: the exchange id for this ddp transfer
512 * Returns : the length of data that have been completed by ddp
514 static int fcoe_ddp_done(struct fc_lport *lp, u16 xid)
516 struct net_device *n = fcoe_netdev(lp);
518 if (n->netdev_ops && n->netdev_ops->ndo_fcoe_ddp_done)
519 return n->netdev_ops->ndo_fcoe_ddp_done(n, xid);
523 static struct libfc_function_template fcoe_libfc_fcn_templ = {
524 .frame_send = fcoe_xmit,
525 .ddp_setup = fcoe_ddp_setup,
526 .ddp_done = fcoe_ddp_done,
530 * fcoe_if_create() - this function creates the fcoe interface
531 * @netdev: pointer the associated netdevice
533 * Creates fc_lport struct and scsi_host for lport, configures lport
534 * and starts fabric login.
536 * Returns : 0 on success
538 static int fcoe_if_create(struct net_device *netdev)
541 struct fc_lport *lp = NULL;
542 struct fcoe_softc *fc;
543 struct Scsi_Host *shost;
547 printk(KERN_DEBUG "fcoe_if_create:interface on %s\n",
550 lp = fcoe_hostlist_lookup(netdev);
554 shost = libfc_host_alloc(&fcoe_shost_template,
555 sizeof(struct fcoe_softc));
557 FC_DBG("Could not allocate host structure\n");
560 lp = shost_priv(shost);
563 /* configure fc_lport, e.g., em */
564 rc = fcoe_lport_config(lp);
566 FC_DBG("Could not configure lport\n");
573 fcoe_ctlr_init(&fc->ctlr);
574 fc->ctlr.send = fcoe_fip_send;
575 fc->ctlr.update_mac = fcoe_update_src_mac;
577 /* configure lport network properties */
578 rc = fcoe_netdev_config(lp, netdev);
580 FC_DBG("Could not configure netdev for the interface\n");
581 goto out_netdev_cleanup;
584 /* configure lport scsi host properties */
585 rc = fcoe_shost_config(lp, shost, &netdev->dev);
587 FC_DBG("Could not configure shost for lport\n");
588 goto out_netdev_cleanup;
591 /* lport exch manager allocation */
592 rc = fcoe_em_config(lp);
594 FC_DBG("Could not configure em for lport\n");
595 goto out_netdev_cleanup;
598 /* Initialize the library */
599 rc = fcoe_libfc_config(lp, &fcoe_libfc_fcn_templ);
601 FC_DBG("Could not configure libfc for lport!\n");
605 /* add to lports list */
606 fcoe_hostlist_add(lp);
608 lp->boot_time = jiffies;
612 if (!fcoe_link_ok(lp))
613 fcoe_ctlr_link_up(&fc->ctlr);
620 fc_exch_mgr_free(lp->emp); /* Free the EM */
622 fcoe_netdev_cleanup(fc);
624 scsi_host_put(lp->host);
629 * fcoe_if_init() - attach to scsi transport
631 * Returns : 0 on success
633 static int __init fcoe_if_init(void)
635 /* attach to scsi transport */
636 scsi_transport_fcoe_sw =
637 fc_attach_transport(&fcoe_transport_function);
639 if (!scsi_transport_fcoe_sw) {
640 printk(KERN_ERR "fcoe_init:fc_attach_transport() failed\n");
648 * fcoe_if_exit() - detach from scsi transport
650 * Returns : 0 on success
652 int __exit fcoe_if_exit(void)
654 fc_release_transport(scsi_transport_fcoe_sw);
659 * fcoe_percpu_thread_create() - Create a receive thread for an online cpu
660 * @cpu: cpu index for the online cpu
662 static void fcoe_percpu_thread_create(unsigned int cpu)
664 struct fcoe_percpu_s *p;
665 struct task_struct *thread;
667 p = &per_cpu(fcoe_percpu, cpu);
669 thread = kthread_create(fcoe_percpu_receive_thread,
670 (void *)p, "fcoethread/%d", cpu);
672 if (likely(!IS_ERR(p->thread))) {
673 kthread_bind(thread, cpu);
674 wake_up_process(thread);
676 spin_lock_bh(&p->fcoe_rx_list.lock);
678 spin_unlock_bh(&p->fcoe_rx_list.lock);
683 * fcoe_percpu_thread_destroy() - removes the rx thread for the given cpu
684 * @cpu: cpu index the rx thread is to be removed
686 * Destroys a per-CPU Rx thread. Any pending skbs are moved to the
687 * current CPU's Rx thread. If the thread being destroyed is bound to
688 * the CPU processing this context the skbs will be freed.
690 static void fcoe_percpu_thread_destroy(unsigned int cpu)
692 struct fcoe_percpu_s *p;
693 struct task_struct *thread;
694 struct page *crc_eof;
697 struct fcoe_percpu_s *p0;
698 unsigned targ_cpu = smp_processor_id();
699 #endif /* CONFIG_SMP */
701 printk(KERN_DEBUG "fcoe: Destroying receive thread for CPU %d\n", cpu);
703 /* Prevent any new skbs from being queued for this CPU. */
704 p = &per_cpu(fcoe_percpu, cpu);
705 spin_lock_bh(&p->fcoe_rx_list.lock);
708 crc_eof = p->crc_eof_page;
709 p->crc_eof_page = NULL;
710 p->crc_eof_offset = 0;
711 spin_unlock_bh(&p->fcoe_rx_list.lock);
715 * Don't bother moving the skb's if this context is running
716 * on the same CPU that is having its thread destroyed. This
717 * can easily happen when the module is removed.
719 if (cpu != targ_cpu) {
720 p0 = &per_cpu(fcoe_percpu, targ_cpu);
721 spin_lock_bh(&p0->fcoe_rx_list.lock);
723 FC_DBG("Moving frames from CPU %d to CPU %d\n",
726 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
727 __skb_queue_tail(&p0->fcoe_rx_list, skb);
728 spin_unlock_bh(&p0->fcoe_rx_list.lock);
731 * The targeted CPU is not initialized and cannot accept
732 * new skbs. Unlock the targeted CPU and drop the skbs
733 * on the CPU that is going offline.
735 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
737 spin_unlock_bh(&p0->fcoe_rx_list.lock);
741 * This scenario occurs when the module is being removed
742 * and all threads are being destroyed. skbs will continue
743 * to be shifted from the CPU thread that is being removed
744 * to the CPU thread associated with the CPU that is processing
745 * the module removal. Once there is only one CPU Rx thread it
746 * will reach this case and we will drop all skbs and later
749 spin_lock_bh(&p->fcoe_rx_list.lock);
750 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
752 spin_unlock_bh(&p->fcoe_rx_list.lock);
756 * This a non-SMP scenario where the singular Rx thread is
757 * being removed. Free all skbs and stop the thread.
759 spin_lock_bh(&p->fcoe_rx_list.lock);
760 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
762 spin_unlock_bh(&p->fcoe_rx_list.lock);
766 kthread_stop(thread);
773 * fcoe_cpu_callback() - fcoe cpu hotplug event callback
774 * @nfb: callback data block
775 * @action: event triggering the callback
776 * @hcpu: index for the cpu of this event
778 * This creates or destroys per cpu data for fcoe
780 * Returns NOTIFY_OK always.
782 static int fcoe_cpu_callback(struct notifier_block *nfb,
783 unsigned long action, void *hcpu)
785 unsigned cpu = (unsigned long)hcpu;
789 case CPU_ONLINE_FROZEN:
790 FC_DBG("CPU %x online: Create Rx thread\n", cpu);
791 fcoe_percpu_thread_create(cpu);
794 case CPU_DEAD_FROZEN:
795 FC_DBG("CPU %x offline: Remove Rx thread\n", cpu);
796 fcoe_percpu_thread_destroy(cpu);
804 static struct notifier_block fcoe_cpu_notifier = {
805 .notifier_call = fcoe_cpu_callback,
809 * fcoe_rcv() - this is the fcoe receive function called by NET_RX_SOFTIRQ
810 * @skb: the receive skb
811 * @dev: associated net device
813 * @olddev: last device
815 * this function will receive the packet and build fc frame and pass it up
817 * Returns: 0 for success
819 int fcoe_rcv(struct sk_buff *skb, struct net_device *dev,
820 struct packet_type *ptype, struct net_device *olddev)
823 struct fcoe_rcv_info *fr;
824 struct fcoe_softc *fc;
825 struct fc_frame_header *fh;
826 struct fcoe_percpu_s *fps;
828 unsigned int cpu = 0;
830 fc = container_of(ptype, struct fcoe_softc, fcoe_packet_type);
832 if (unlikely(lp == NULL)) {
833 FC_DBG("cannot find hba structure");
839 if (unlikely(debug_fcoe)) {
840 FC_DBG("skb_info: len:%d data_len:%d head:%p data:%p tail:%p "
841 "end:%p sum:%d dev:%s", skb->len, skb->data_len,
842 skb->head, skb->data, skb_tail_pointer(skb),
843 skb_end_pointer(skb), skb->csum,
844 skb->dev ? skb->dev->name : "<NULL>");
848 /* check for FCOE packet type */
849 if (unlikely(eth_hdr(skb)->h_proto != htons(ETH_P_FCOE))) {
850 FC_DBG("wrong FC type frame");
855 * Check for minimum frame length, and make sure required FCoE
856 * and FC headers are pulled into the linear data area.
858 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
859 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
862 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
863 fh = (struct fc_frame_header *) skb_transport_header(skb);
865 oxid = ntohs(fh->fh_ox_id);
867 fr = fcoe_dev_from_skb(skb);
873 * The incoming frame exchange id(oxid) is ANDed with num of online
874 * cpu bits to get cpu and then this cpu is used for selecting
875 * a per cpu kernel thread from fcoe_percpu.
877 cpu = oxid & (num_online_cpus() - 1);
880 fps = &per_cpu(fcoe_percpu, cpu);
881 spin_lock_bh(&fps->fcoe_rx_list.lock);
882 if (unlikely(!fps->thread)) {
884 * The targeted CPU is not ready, let's target
885 * the first CPU now. For non-SMP systems this
886 * will check the same CPU twice.
888 FC_DBG("CPU is online, but no receive thread ready "
889 "for incoming skb- using first online CPU.\n");
891 spin_unlock_bh(&fps->fcoe_rx_list.lock);
892 cpu = first_cpu(cpu_online_map);
893 fps = &per_cpu(fcoe_percpu, cpu);
894 spin_lock_bh(&fps->fcoe_rx_list.lock);
896 spin_unlock_bh(&fps->fcoe_rx_list.lock);
902 * We now have a valid CPU that we're targeting for
903 * this skb. We also have this receive thread locked,
904 * so we're free to queue skbs into it's queue.
906 __skb_queue_tail(&fps->fcoe_rx_list, skb);
907 if (fps->fcoe_rx_list.qlen == 1)
908 wake_up_process(fps->thread);
910 spin_unlock_bh(&fps->fcoe_rx_list.lock);
914 fc_lport_get_stats(lp)->ErrorFrames++;
922 * fcoe_start_io() - pass to netdev to start xmit for fcoe
923 * @skb: the skb to be xmitted
925 * Returns: 0 for success
927 static inline int fcoe_start_io(struct sk_buff *skb)
932 rc = dev_queue_xmit(skb);
940 * fcoe_get_paged_crc_eof() - in case we need to alloc a page for crc_eof
941 * @skb: the skb to be xmitted
944 * Returns: 0 for success
946 static int fcoe_get_paged_crc_eof(struct sk_buff *skb, int tlen)
948 struct fcoe_percpu_s *fps;
951 fps = &get_cpu_var(fcoe_percpu);
952 page = fps->crc_eof_page;
954 page = alloc_page(GFP_ATOMIC);
956 put_cpu_var(fcoe_percpu);
959 fps->crc_eof_page = page;
960 fps->crc_eof_offset = 0;
964 skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags, page,
965 fps->crc_eof_offset, tlen);
967 skb->data_len += tlen;
968 skb->truesize += tlen;
969 fps->crc_eof_offset += sizeof(struct fcoe_crc_eof);
971 if (fps->crc_eof_offset >= PAGE_SIZE) {
972 fps->crc_eof_page = NULL;
973 fps->crc_eof_offset = 0;
976 put_cpu_var(fcoe_percpu);
981 * fcoe_fc_crc() - calculates FC CRC in this fcoe skb
982 * @fp: the fc_frame containing data to be checksummed
984 * This uses crc32() to calculate the crc for fc frame
985 * Return : 32 bit crc
987 u32 fcoe_fc_crc(struct fc_frame *fp)
989 struct sk_buff *skb = fp_skb(fp);
990 struct skb_frag_struct *frag;
992 unsigned long off, len, clen;
996 crc = crc32(~0, skb->data, skb_headlen(skb));
998 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
999 frag = &skb_shinfo(skb)->frags[i];
1000 off = frag->page_offset;
1003 clen = min(len, PAGE_SIZE - (off & ~PAGE_MASK));
1004 data = kmap_atomic(frag->page + (off >> PAGE_SHIFT),
1005 KM_SKB_DATA_SOFTIRQ);
1006 crc = crc32(crc, data + (off & ~PAGE_MASK), clen);
1007 kunmap_atomic(data, KM_SKB_DATA_SOFTIRQ);
1016 * fcoe_xmit() - FCoE frame transmit function
1017 * @lp: the associated local port
1018 * @fp: the fc_frame to be transmitted
1020 * Return : 0 for success
1022 int fcoe_xmit(struct fc_lport *lp, struct fc_frame *fp)
1027 struct fcoe_crc_eof *cp;
1028 struct sk_buff *skb;
1029 struct fcoe_dev_stats *stats;
1030 struct fc_frame_header *fh;
1031 unsigned int hlen; /* header length implies the version */
1032 unsigned int tlen; /* trailer length */
1033 unsigned int elen; /* eth header, may include vlan */
1034 struct fcoe_softc *fc;
1036 struct fcoe_hdr *hp;
1038 WARN_ON((fr_len(fp) % sizeof(u32)) != 0);
1040 fc = lport_priv(lp);
1041 fh = fc_frame_header_get(fp);
1043 wlen = skb->len / FCOE_WORD_TO_BYTE;
1050 if (unlikely(fh->fh_r_ctl == FC_RCTL_ELS_REQ) &&
1051 fcoe_ctlr_els_send(&fc->ctlr, skb))
1057 elen = (fc->real_dev->priv_flags & IFF_802_1Q_VLAN) ?
1058 sizeof(struct vlan_ethhdr) : sizeof(struct ethhdr);
1059 hlen = sizeof(struct fcoe_hdr);
1060 tlen = sizeof(struct fcoe_crc_eof);
1061 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
1064 if (likely(lp->crc_offload)) {
1065 skb->ip_summed = CHECKSUM_PARTIAL;
1066 skb->csum_start = skb_headroom(skb);
1067 skb->csum_offset = skb->len;
1070 skb->ip_summed = CHECKSUM_NONE;
1071 crc = fcoe_fc_crc(fp);
1074 /* copy fc crc and eof to the skb buff */
1075 if (skb_is_nonlinear(skb)) {
1077 if (fcoe_get_paged_crc_eof(skb, tlen)) {
1081 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
1082 cp = kmap_atomic(frag->page, KM_SKB_DATA_SOFTIRQ)
1083 + frag->page_offset;
1085 cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
1088 memset(cp, 0, sizeof(*cp));
1090 cp->fcoe_crc32 = cpu_to_le32(~crc);
1092 if (skb_is_nonlinear(skb)) {
1093 kunmap_atomic(cp, KM_SKB_DATA_SOFTIRQ);
1097 /* adjust skb network/transport offsets to match mac/fcoe/fc */
1098 skb_push(skb, elen + hlen);
1099 skb_reset_mac_header(skb);
1100 skb_reset_network_header(skb);
1101 skb->mac_len = elen;
1102 skb->protocol = htons(ETH_P_FCOE);
1103 skb->dev = fc->real_dev;
1105 /* fill up mac and fcoe headers */
1107 eh->h_proto = htons(ETH_P_FCOE);
1108 if (fc->ctlr.map_dest)
1109 fc_fcoe_set_mac(eh->h_dest, fh->fh_d_id);
1111 /* insert GW address */
1112 memcpy(eh->h_dest, fc->ctlr.dest_addr, ETH_ALEN);
1114 if (unlikely(fc->ctlr.flogi_oxid != FC_XID_UNKNOWN))
1115 memcpy(eh->h_source, fc->ctlr.ctl_src_addr, ETH_ALEN);
1117 memcpy(eh->h_source, fc->ctlr.data_src_addr, ETH_ALEN);
1119 hp = (struct fcoe_hdr *)(eh + 1);
1120 memset(hp, 0, sizeof(*hp));
1122 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
1126 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
1127 if (lp->seq_offload && fr_max_payload(fp)) {
1128 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
1129 skb_shinfo(skb)->gso_size = fr_max_payload(fp);
1131 skb_shinfo(skb)->gso_type = 0;
1132 skb_shinfo(skb)->gso_size = 0;
1135 /* update tx stats: regardless if LLD fails */
1136 stats = fc_lport_get_stats(lp);
1138 stats->TxWords += wlen;
1140 /* send down to lld */
1142 if (fc->fcoe_pending_queue.qlen)
1143 rc = fcoe_check_wait_queue(lp);
1146 rc = fcoe_start_io(skb);
1149 spin_lock_bh(&fc->fcoe_pending_queue.lock);
1150 __skb_queue_tail(&fc->fcoe_pending_queue, skb);
1151 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
1152 if (fc->fcoe_pending_queue.qlen > FCOE_MAX_QUEUE_DEPTH)
1160 * fcoe_percpu_receive_thread() - recv thread per cpu
1161 * @arg: ptr to the fcoe per cpu struct
1163 * Return: 0 for success
1165 int fcoe_percpu_receive_thread(void *arg)
1167 struct fcoe_percpu_s *p = arg;
1169 struct fc_lport *lp;
1170 struct fcoe_rcv_info *fr;
1171 struct fcoe_dev_stats *stats;
1172 struct fc_frame_header *fh;
1173 struct sk_buff *skb;
1174 struct fcoe_crc_eof crc_eof;
1175 struct fc_frame *fp;
1177 struct fcoe_softc *fc;
1178 struct fcoe_hdr *hp;
1180 set_user_nice(current, -20);
1182 while (!kthread_should_stop()) {
1184 spin_lock_bh(&p->fcoe_rx_list.lock);
1185 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) == NULL) {
1186 set_current_state(TASK_INTERRUPTIBLE);
1187 spin_unlock_bh(&p->fcoe_rx_list.lock);
1189 set_current_state(TASK_RUNNING);
1190 if (kthread_should_stop())
1192 spin_lock_bh(&p->fcoe_rx_list.lock);
1194 spin_unlock_bh(&p->fcoe_rx_list.lock);
1195 fr = fcoe_dev_from_skb(skb);
1197 if (unlikely(lp == NULL)) {
1198 FC_DBG("invalid HBA Structure");
1203 if (unlikely(debug_fcoe)) {
1204 FC_DBG("skb_info: len:%d data_len:%d head:%p data:%p "
1205 "tail:%p end:%p sum:%d dev:%s",
1206 skb->len, skb->data_len,
1207 skb->head, skb->data, skb_tail_pointer(skb),
1208 skb_end_pointer(skb), skb->csum,
1209 skb->dev ? skb->dev->name : "<NULL>");
1213 * Save source MAC address before discarding header.
1215 fc = lport_priv(lp);
1216 if (skb_is_nonlinear(skb))
1217 skb_linearize(skb); /* not ideal */
1218 mac = eth_hdr(skb)->h_source;
1221 * Frame length checks and setting up the header pointers
1222 * was done in fcoe_rcv already.
1224 hp = (struct fcoe_hdr *) skb_network_header(skb);
1225 fh = (struct fc_frame_header *) skb_transport_header(skb);
1227 stats = fc_lport_get_stats(lp);
1228 if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) {
1229 if (stats->ErrorFrames < 5)
1230 printk(KERN_WARNING "FCoE version "
1231 "mismatch: The frame has "
1232 "version %x, but the "
1233 "initiator supports version "
1234 "%x\n", FC_FCOE_DECAPS_VER(hp),
1236 stats->ErrorFrames++;
1241 skb_pull(skb, sizeof(struct fcoe_hdr));
1242 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
1245 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
1247 fp = (struct fc_frame *)skb;
1250 fr_sof(fp) = hp->fcoe_sof;
1252 /* Copy out the CRC and EOF trailer for access */
1253 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof))) {
1257 fr_eof(fp) = crc_eof.fcoe_eof;
1258 fr_crc(fp) = crc_eof.fcoe_crc32;
1259 if (pskb_trim(skb, fr_len)) {
1265 * We only check CRC if no offload is available and if it is
1266 * it's solicited data, in which case, the FCP layer would
1267 * check it during the copy.
1269 if (lp->crc_offload && skb->ip_summed == CHECKSUM_UNNECESSARY)
1270 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1272 fr_flags(fp) |= FCPHF_CRC_UNCHECKED;
1274 fh = fc_frame_header_get(fp);
1275 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA &&
1276 fh->fh_type == FC_TYPE_FCP) {
1277 fc_exch_recv(lp, lp->emp, fp);
1280 if (fr_flags(fp) & FCPHF_CRC_UNCHECKED) {
1281 if (le32_to_cpu(fr_crc(fp)) !=
1282 ~crc32(~0, skb->data, fr_len)) {
1283 if (debug_fcoe || stats->InvalidCRCCount < 5)
1284 printk(KERN_WARNING "fcoe: dropping "
1285 "frame with CRC error\n");
1286 stats->InvalidCRCCount++;
1287 stats->ErrorFrames++;
1291 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1293 if (unlikely(fc->ctlr.flogi_oxid != FC_XID_UNKNOWN) &&
1294 fcoe_ctlr_recv_flogi(&fc->ctlr, fp, mac)) {
1298 fc_exch_recv(lp, lp->emp, fp);
1304 * fcoe_watchdog() - fcoe timer callback
1307 * This checks the pending queue length for fcoe and set lport qfull
1308 * if the FCOE_MAX_QUEUE_DEPTH is reached. This is done for all fc_lport on the
1311 * Returns: 0 for success
1313 void fcoe_watchdog(ulong vp)
1315 struct fcoe_softc *fc;
1317 read_lock(&fcoe_hostlist_lock);
1318 list_for_each_entry(fc, &fcoe_hostlist, list) {
1320 fcoe_check_wait_queue(fc->ctlr.lp);
1322 read_unlock(&fcoe_hostlist_lock);
1324 fcoe_timer.expires = jiffies + (1 * HZ);
1325 add_timer(&fcoe_timer);
1330 * fcoe_check_wait_queue() - attempt to clear the transmit backlog
1333 * This empties the wait_queue, dequeue the head of the wait_queue queue
1334 * and calls fcoe_start_io() for each packet, if all skb have been
1335 * transmitted, return qlen or -1 if a error occurs, then restore
1336 * wait_queue and try again later.
1338 * The wait_queue is used when the skb transmit fails. skb will go
1339 * in the wait_queue which will be emptied by the timer function or
1340 * by the next skb transmit.
1342 * Returns: 0 for success
1344 static int fcoe_check_wait_queue(struct fc_lport *lp)
1346 struct fcoe_softc *fc = lport_priv(lp);
1347 struct sk_buff *skb;
1350 spin_lock_bh(&fc->fcoe_pending_queue.lock);
1351 if (fc->fcoe_pending_queue_active)
1353 fc->fcoe_pending_queue_active = 1;
1355 while (fc->fcoe_pending_queue.qlen) {
1356 /* keep qlen > 0 until fcoe_start_io succeeds */
1357 fc->fcoe_pending_queue.qlen++;
1358 skb = __skb_dequeue(&fc->fcoe_pending_queue);
1360 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
1361 rc = fcoe_start_io(skb);
1362 spin_lock_bh(&fc->fcoe_pending_queue.lock);
1365 __skb_queue_head(&fc->fcoe_pending_queue, skb);
1366 /* undo temporary increment above */
1367 fc->fcoe_pending_queue.qlen--;
1370 /* undo temporary increment above */
1371 fc->fcoe_pending_queue.qlen--;
1374 if (fc->fcoe_pending_queue.qlen < FCOE_LOW_QUEUE_DEPTH)
1376 fc->fcoe_pending_queue_active = 0;
1377 rc = fc->fcoe_pending_queue.qlen;
1379 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
1384 * fcoe_dev_setup() - setup link change notification interface
1386 static void fcoe_dev_setup()
1388 register_netdevice_notifier(&fcoe_notifier);
1392 * fcoe_dev_setup() - cleanup link change notification interface
1394 static void fcoe_dev_cleanup(void)
1396 unregister_netdevice_notifier(&fcoe_notifier);
1400 * fcoe_device_notification() - netdev event notification callback
1401 * @notifier: context of the notification
1402 * @event: type of event
1403 * @ptr: fixed array for output parsed ifname
1405 * This function is called by the ethernet driver in case of link change event
1407 * Returns: 0 for success
1409 static int fcoe_device_notification(struct notifier_block *notifier,
1410 ulong event, void *ptr)
1412 struct fc_lport *lp = NULL;
1413 struct net_device *real_dev = ptr;
1414 struct fcoe_softc *fc;
1415 struct fcoe_dev_stats *stats;
1416 u32 link_possible = 1;
1420 read_lock(&fcoe_hostlist_lock);
1421 list_for_each_entry(fc, &fcoe_hostlist, list) {
1422 if (fc->real_dev == real_dev) {
1427 read_unlock(&fcoe_hostlist_lock);
1435 case NETDEV_GOING_DOWN:
1441 case NETDEV_CHANGEMTU:
1442 mfs = fc->real_dev->mtu -
1443 (sizeof(struct fcoe_hdr) +
1444 sizeof(struct fcoe_crc_eof));
1445 if (mfs >= FC_MIN_MAX_FRAME)
1446 fc_set_mfs(lp, mfs);
1448 case NETDEV_REGISTER:
1451 FC_DBG("Unknown event %ld from netdev netlink\n", event);
1453 if (link_possible && !fcoe_link_ok(lp))
1454 fcoe_ctlr_link_up(&fc->ctlr);
1455 else if (fcoe_ctlr_link_down(&fc->ctlr)) {
1456 stats = fc_lport_get_stats(lp);
1457 stats->LinkFailureCount++;
1458 fcoe_clean_pending_queue(lp);
1465 * fcoe_if_to_netdev() - parse a name buffer to get netdev
1466 * @buffer: incoming buffer to be copied
1468 * Returns: NULL or ptr to net_device
1470 static struct net_device *fcoe_if_to_netdev(const char *buffer)
1473 char ifname[IFNAMSIZ + 2];
1476 strlcpy(ifname, buffer, IFNAMSIZ);
1477 cp = ifname + strlen(ifname);
1478 while (--cp >= ifname && *cp == '\n')
1480 return dev_get_by_name(&init_net, ifname);
1486 * fcoe_netdev_to_module_owner() - finds out the driver module of the netdev
1487 * @netdev: the target netdev
1489 * Returns: ptr to the struct module, NULL for failure
1491 static struct module *
1492 fcoe_netdev_to_module_owner(const struct net_device *netdev)
1499 dev = netdev->dev.parent;
1506 return dev->driver->owner;
1510 * fcoe_ethdrv_get() - Hold the Ethernet driver
1511 * @netdev: the target netdev
1513 * Holds the Ethernet driver module by try_module_get() for
1514 * the corresponding netdev.
1516 * Returns: 0 for success
1518 static int fcoe_ethdrv_get(const struct net_device *netdev)
1520 struct module *owner;
1522 owner = fcoe_netdev_to_module_owner(netdev);
1524 printk(KERN_DEBUG "fcoe:hold driver module %s for %s\n",
1525 module_name(owner), netdev->name);
1526 return try_module_get(owner);
1532 * fcoe_ethdrv_put() - Release the Ethernet driver
1533 * @netdev: the target netdev
1535 * Releases the Ethernet driver module by module_put for
1536 * the corresponding netdev.
1538 * Returns: 0 for success
1540 static int fcoe_ethdrv_put(const struct net_device *netdev)
1542 struct module *owner;
1544 owner = fcoe_netdev_to_module_owner(netdev);
1546 printk(KERN_DEBUG "fcoe:release driver module %s for %s\n",
1547 module_name(owner), netdev->name);
1555 * fcoe_destroy() - handles the destroy from sysfs
1556 * @buffer: expected to be an eth if name
1557 * @kp: associated kernel param
1559 * Returns: 0 for success
1561 static int fcoe_destroy(const char *buffer, struct kernel_param *kp)
1564 struct net_device *netdev;
1566 netdev = fcoe_if_to_netdev(buffer);
1571 /* look for existing lport */
1572 if (!fcoe_hostlist_lookup(netdev)) {
1576 rc = fcoe_if_destroy(netdev);
1578 printk(KERN_ERR "fcoe: fcoe_if_destroy(%s) failed\n",
1583 fcoe_ethdrv_put(netdev);
1592 * fcoe_create() - Handles the create call from sysfs
1593 * @buffer: expected to be an eth if name
1594 * @kp: associated kernel param
1596 * Returns: 0 for success
1598 static int fcoe_create(const char *buffer, struct kernel_param *kp)
1601 struct net_device *netdev;
1603 netdev = fcoe_if_to_netdev(buffer);
1608 /* look for existing lport */
1609 if (fcoe_hostlist_lookup(netdev)) {
1613 fcoe_ethdrv_get(netdev);
1615 rc = fcoe_if_create(netdev);
1617 printk(KERN_ERR "fcoe: fcoe_if_create(%s) failed\n",
1619 fcoe_ethdrv_put(netdev);
1630 module_param_call(create, fcoe_create, NULL, NULL, S_IWUSR);
1631 __MODULE_PARM_TYPE(create, "string");
1632 MODULE_PARM_DESC(create, "Create fcoe port using net device passed in.");
1633 module_param_call(destroy, fcoe_destroy, NULL, NULL, S_IWUSR);
1634 __MODULE_PARM_TYPE(destroy, "string");
1635 MODULE_PARM_DESC(destroy, "Destroy fcoe port");
1638 * fcoe_link_ok() - Check if link is ok for the fc_lport
1639 * @lp: ptr to the fc_lport
1641 * Any permanently-disqualifying conditions have been previously checked.
1642 * This also updates the speed setting, which may change with link for 100/1000.
1644 * This function should probably be checking for PAUSE support at some point
1645 * in the future. Currently Per-priority-pause is not determinable using
1646 * ethtool, so we shouldn't be restrictive until that problem is resolved.
1648 * Returns: 0 if link is OK for use by FCoE.
1651 int fcoe_link_ok(struct fc_lport *lp)
1653 struct fcoe_softc *fc = lport_priv(lp);
1654 struct net_device *dev = fc->real_dev;
1655 struct ethtool_cmd ecmd = { ETHTOOL_GSET };
1658 if ((dev->flags & IFF_UP) && netif_carrier_ok(dev)) {
1660 if (dev->ethtool_ops->get_settings) {
1661 dev->ethtool_ops->get_settings(dev, &ecmd);
1662 lp->link_supported_speeds &=
1663 ~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
1664 if (ecmd.supported & (SUPPORTED_1000baseT_Half |
1665 SUPPORTED_1000baseT_Full))
1666 lp->link_supported_speeds |= FC_PORTSPEED_1GBIT;
1667 if (ecmd.supported & SUPPORTED_10000baseT_Full)
1668 lp->link_supported_speeds |=
1669 FC_PORTSPEED_10GBIT;
1670 if (ecmd.speed == SPEED_1000)
1671 lp->link_speed = FC_PORTSPEED_1GBIT;
1672 if (ecmd.speed == SPEED_10000)
1673 lp->link_speed = FC_PORTSPEED_10GBIT;
1682 * fcoe_percpu_clean() - Clear the pending skbs for an lport
1685 void fcoe_percpu_clean(struct fc_lport *lp)
1687 struct fcoe_percpu_s *pp;
1688 struct fcoe_rcv_info *fr;
1689 struct sk_buff_head *list;
1690 struct sk_buff *skb, *next;
1691 struct sk_buff *head;
1694 for_each_possible_cpu(cpu) {
1695 pp = &per_cpu(fcoe_percpu, cpu);
1696 spin_lock_bh(&pp->fcoe_rx_list.lock);
1697 list = &pp->fcoe_rx_list;
1699 for (skb = head; skb != (struct sk_buff *)list;
1702 fr = fcoe_dev_from_skb(skb);
1703 if (fr->fr_dev == lp) {
1704 __skb_unlink(skb, list);
1708 spin_unlock_bh(&pp->fcoe_rx_list.lock);
1713 * fcoe_clean_pending_queue() - Dequeue a skb and free it
1714 * @lp: the corresponding fc_lport
1718 void fcoe_clean_pending_queue(struct fc_lport *lp)
1720 struct fcoe_softc *fc = lport_priv(lp);
1721 struct sk_buff *skb;
1723 spin_lock_bh(&fc->fcoe_pending_queue.lock);
1724 while ((skb = __skb_dequeue(&fc->fcoe_pending_queue)) != NULL) {
1725 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
1727 spin_lock_bh(&fc->fcoe_pending_queue.lock);
1729 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
1733 * fcoe_reset() - Resets the fcoe
1734 * @shost: shost the reset is from
1738 int fcoe_reset(struct Scsi_Host *shost)
1740 struct fc_lport *lport = shost_priv(shost);
1741 fc_lport_reset(lport);
1746 * fcoe_hostlist_lookup_softc() - find the corresponding lport by a given device
1747 * @dev: this is currently ptr to net_device
1749 * Returns: NULL or the located fcoe_softc
1751 static struct fcoe_softc *
1752 fcoe_hostlist_lookup_softc(const struct net_device *dev)
1754 struct fcoe_softc *fc;
1756 read_lock(&fcoe_hostlist_lock);
1757 list_for_each_entry(fc, &fcoe_hostlist, list) {
1758 if (fc->real_dev == dev) {
1759 read_unlock(&fcoe_hostlist_lock);
1763 read_unlock(&fcoe_hostlist_lock);
1768 * fcoe_hostlist_lookup() - Find the corresponding lport by netdev
1769 * @netdev: ptr to net_device
1771 * Returns: 0 for success
1773 struct fc_lport *fcoe_hostlist_lookup(const struct net_device *netdev)
1775 struct fcoe_softc *fc;
1777 fc = fcoe_hostlist_lookup_softc(netdev);
1779 return (fc) ? fc->ctlr.lp : NULL;
1783 * fcoe_hostlist_add() - Add a lport to lports list
1784 * @lp: ptr to the fc_lport to be added
1786 * Returns: 0 for success
1788 int fcoe_hostlist_add(const struct fc_lport *lp)
1790 struct fcoe_softc *fc;
1792 fc = fcoe_hostlist_lookup_softc(fcoe_netdev(lp));
1794 fc = lport_priv(lp);
1795 write_lock_bh(&fcoe_hostlist_lock);
1796 list_add_tail(&fc->list, &fcoe_hostlist);
1797 write_unlock_bh(&fcoe_hostlist_lock);
1803 * fcoe_hostlist_remove() - remove a lport from lports list
1804 * @lp: ptr to the fc_lport to be removed
1806 * Returns: 0 for success
1808 int fcoe_hostlist_remove(const struct fc_lport *lp)
1810 struct fcoe_softc *fc;
1812 fc = fcoe_hostlist_lookup_softc(fcoe_netdev(lp));
1814 write_lock_bh(&fcoe_hostlist_lock);
1815 list_del(&fc->list);
1816 write_unlock_bh(&fcoe_hostlist_lock);
1822 * fcoe_init() - fcoe module loading initialization
1824 * Returns 0 on success, negative on failure
1826 static int __init fcoe_init(void)
1830 struct fcoe_percpu_s *p;
1832 INIT_LIST_HEAD(&fcoe_hostlist);
1833 rwlock_init(&fcoe_hostlist_lock);
1835 for_each_possible_cpu(cpu) {
1836 p = &per_cpu(fcoe_percpu, cpu);
1837 skb_queue_head_init(&p->fcoe_rx_list);
1840 for_each_online_cpu(cpu)
1841 fcoe_percpu_thread_create(cpu);
1843 /* Initialize per CPU interrupt thread */
1844 rc = register_hotcpu_notifier(&fcoe_cpu_notifier);
1848 /* Setup link change notification */
1851 setup_timer(&fcoe_timer, fcoe_watchdog, 0);
1853 mod_timer(&fcoe_timer, jiffies + (10 * HZ));
1860 for_each_online_cpu(cpu) {
1861 fcoe_percpu_thread_destroy(cpu);
1866 module_init(fcoe_init);
1869 * fcoe_exit() - fcoe module unloading cleanup
1871 * Returns 0 on success, negative on failure
1873 static void __exit fcoe_exit(void)
1876 struct fcoe_softc *fc, *tmp;
1880 /* Stop the timer */
1881 del_timer_sync(&fcoe_timer);
1883 /* releases the associated fcoe hosts */
1884 list_for_each_entry_safe(fc, tmp, &fcoe_hostlist, list)
1885 fcoe_if_destroy(fc->real_dev);
1887 unregister_hotcpu_notifier(&fcoe_cpu_notifier);
1889 for_each_online_cpu(cpu) {
1890 fcoe_percpu_thread_destroy(cpu);
1893 /* detach from scsi transport */
1896 module_exit(fcoe_exit);