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 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_softc *fc;
156 fc = container_of(ptype, struct fcoe_softc, fip_packet_type);
157 fcoe_ctlr_recv(&fc->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)->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_softc *fc;
185 fc = fcoe_from_ctlr(fip);
187 if (!is_zero_ether_addr(old))
188 dev_unicast_delete(fc->netdev, old);
189 dev_unicast_add(fc->netdev, new);
194 * fcoe_lport_config() - sets up the fc_lport
195 * @lp: ptr to the fc_lport
197 * Returns: 0 for success
199 static int fcoe_lport_config(struct fc_lport *lp)
203 lp->max_retry_count = 3;
204 lp->max_rport_retry_count = 3;
205 lp->e_d_tov = 2 * 1000; /* FC-FS default */
206 lp->r_a_tov = 2 * 2 * 1000;
207 lp->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
208 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
210 fc_lport_init_stats(lp);
212 /* lport fc_lport related configuration */
215 /* offload related configuration */
226 * fcoe_netdev_cleanup() - clean up netdev configurations
227 * @fc: ptr to the fcoe_softc
229 void fcoe_netdev_cleanup(struct fcoe_softc *fc)
231 u8 flogi_maddr[ETH_ALEN];
233 /* Don't listen for Ethernet packets anymore */
234 dev_remove_pack(&fc->fcoe_packet_type);
235 dev_remove_pack(&fc->fip_packet_type);
237 /* Delete secondary MAC addresses */
239 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
240 dev_unicast_delete(fc->netdev, flogi_maddr);
241 if (!is_zero_ether_addr(fc->ctlr.data_src_addr))
242 dev_unicast_delete(fc->netdev, fc->ctlr.data_src_addr);
244 dev_unicast_delete(fc->netdev, fc->ctlr.ctl_src_addr);
245 dev_mc_delete(fc->netdev, FIP_ALL_ENODE_MACS, ETH_ALEN, 0);
250 * fcoe_queue_timer() - fcoe queue timer
251 * @lp: the fc_lport pointer
253 * Calls fcoe_check_wait_queue on timeout
256 static void fcoe_queue_timer(ulong lp)
258 fcoe_check_wait_queue((struct fc_lport *)lp, NULL);
262 * fcoe_netdev_config() - Set up netdev for SW FCoE
263 * @lp : ptr to the fc_lport
264 * @netdev : ptr to the associated netdevice struct
266 * Must be called after fcoe_lport_config() as it will use lport mutex
268 * Returns : 0 for success
270 static int fcoe_netdev_config(struct fc_lport *lp, struct net_device *netdev)
274 struct fcoe_softc *fc;
275 u8 flogi_maddr[ETH_ALEN];
276 struct netdev_hw_addr *ha;
278 /* Setup lport private data to point to fcoe softc */
283 /* Do not support for bonding device */
284 if ((netdev->priv_flags & IFF_MASTER_ALB) ||
285 (netdev->priv_flags & IFF_SLAVE_INACTIVE) ||
286 (netdev->priv_flags & IFF_MASTER_8023AD)) {
291 * Determine max frame size based on underlying device and optional
292 * user-configured limit. If the MFS is too low, fcoe_link_ok()
293 * will return 0, so do this first.
295 mfs = netdev->mtu - (sizeof(struct fcoe_hdr) +
296 sizeof(struct fcoe_crc_eof));
297 if (fc_set_mfs(lp, mfs))
300 /* offload features support */
301 if (netdev->features & NETIF_F_SG)
304 if (netdev->features & NETIF_F_FCOE_CRC) {
306 FCOE_NETDEV_DBG(netdev, "Supports FCCRC offload\n");
308 if (netdev->features & NETIF_F_FSO) {
310 lp->lso_max = netdev->gso_max_size;
311 FCOE_NETDEV_DBG(netdev, "Supports LSO for max len 0x%x\n",
314 if (netdev->fcoe_ddp_xid) {
316 lp->lro_xid = netdev->fcoe_ddp_xid;
317 FCOE_NETDEV_DBG(netdev, "Supports LRO for max xid 0x%x\n",
320 skb_queue_head_init(&fc->fcoe_pending_queue);
321 fc->fcoe_pending_queue_active = 0;
322 setup_timer(&fc->timer, fcoe_queue_timer, (unsigned long)lp);
324 /* look for SAN MAC address, if multiple SAN MACs exist, only
325 * use the first one for SPMA */
327 for_each_dev_addr(netdev, ha) {
328 if ((ha->type == NETDEV_HW_ADDR_T_SAN) &&
329 (is_valid_ether_addr(ha->addr))) {
330 memcpy(fc->ctlr.ctl_src_addr, ha->addr, ETH_ALEN);
337 /* setup Source Mac Address */
339 memcpy(fc->ctlr.ctl_src_addr, netdev->dev_addr,
340 fc->netdev->addr_len);
342 wwnn = fcoe_wwn_from_mac(netdev->dev_addr, 1, 0);
343 fc_set_wwnn(lp, wwnn);
344 /* XXX - 3rd arg needs to be vlan id */
345 wwpn = fcoe_wwn_from_mac(netdev->dev_addr, 2, 0);
346 fc_set_wwpn(lp, wwpn);
349 * Add FCoE MAC address as second unicast MAC address
350 * or enter promiscuous mode if not capable of listening
351 * for multiple unicast MACs.
354 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
355 dev_unicast_add(netdev, flogi_maddr);
357 dev_unicast_add(netdev, fc->ctlr.ctl_src_addr);
358 dev_mc_add(netdev, FIP_ALL_ENODE_MACS, ETH_ALEN, 0);
362 * setup the receive function from ethernet driver
363 * on the ethertype for the given device
365 fc->fcoe_packet_type.func = fcoe_rcv;
366 fc->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
367 fc->fcoe_packet_type.dev = netdev;
368 dev_add_pack(&fc->fcoe_packet_type);
370 fc->fip_packet_type.func = fcoe_fip_recv;
371 fc->fip_packet_type.type = htons(ETH_P_FIP);
372 fc->fip_packet_type.dev = netdev;
373 dev_add_pack(&fc->fip_packet_type);
379 * fcoe_shost_config() - Sets up fc_lport->host
380 * @lp : ptr to the fc_lport
381 * @shost : ptr to the associated scsi host
382 * @dev : device associated to scsi host
384 * Must be called after fcoe_lport_config() and fcoe_netdev_config()
386 * Returns : 0 for success
388 static int fcoe_shost_config(struct fc_lport *lp, struct Scsi_Host *shost,
393 /* lport scsi host config */
396 lp->host->max_lun = FCOE_MAX_LUN;
397 lp->host->max_id = FCOE_MAX_FCP_TARGET;
398 lp->host->max_channel = 0;
399 lp->host->transportt = scsi_transport_fcoe_sw;
401 /* add the new host to the SCSI-ml */
402 rc = scsi_add_host(lp->host, dev);
404 FCOE_NETDEV_DBG(fcoe_netdev(lp), "fcoe_shost_config: "
405 "error on scsi_add_host\n");
408 sprintf(fc_host_symbolic_name(lp->host), "%s v%s over %s",
409 FCOE_NAME, FCOE_VERSION,
410 fcoe_netdev(lp)->name);
416 * fcoe_oem_match() - match for read types IO
417 * @fp: the fc_frame for new IO.
419 * Returns : true for read types IO, otherwise returns false.
421 bool fcoe_oem_match(struct fc_frame *fp)
423 return fc_fcp_is_read(fr_fsp(fp)) &&
424 (fr_fsp(fp)->data_len > fcoe_ddp_min);
428 * fcoe_em_config() - allocates em for this lport
429 * @lp: the port that em is to allocated for
431 * Called with write fcoe_hostlist_lock held.
433 * Returns : 0 on success
435 static inline int fcoe_em_config(struct fc_lport *lp)
437 struct fcoe_softc *fc = lport_priv(lp);
438 struct fcoe_softc *oldfc = NULL;
439 struct net_device *old_real_dev, *cur_real_dev;
440 u16 min_xid = FCOE_MIN_XID;
441 u16 max_xid = FCOE_MAX_XID;
444 * Check if need to allocate an em instance for
445 * offload exchange ids to be shared across all VN_PORTs/lport.
447 if (!lp->lro_enabled || !lp->lro_xid || (lp->lro_xid >= max_xid)) {
453 * Reuse existing offload em instance in case
454 * it is already allocated on real eth device
456 if (fc->netdev->priv_flags & IFF_802_1Q_VLAN)
457 cur_real_dev = vlan_dev_real_dev(fc->netdev);
459 cur_real_dev = fc->netdev;
461 list_for_each_entry(oldfc, &fcoe_hostlist, list) {
462 if (oldfc->netdev->priv_flags & IFF_802_1Q_VLAN)
463 old_real_dev = vlan_dev_real_dev(oldfc->netdev);
465 old_real_dev = oldfc->netdev;
467 if (cur_real_dev == old_real_dev) {
468 fc->oem = oldfc->oem;
474 if (!fc_exch_mgr_add(lp, fc->oem, fcoe_oem_match)) {
475 printk(KERN_ERR "fcoe_em_config: failed to add "
476 "offload em:%p on interface:%s\n",
477 fc->oem, fc->netdev->name);
481 fc->oem = fc_exch_mgr_alloc(lp, FC_CLASS_3,
482 FCOE_MIN_XID, lp->lro_xid,
485 printk(KERN_ERR "fcoe_em_config: failed to allocate "
486 "em for offload exches on interface:%s\n",
493 * Exclude offload EM xid range from next EM xid range.
495 min_xid += lp->lro_xid + 1;
498 if (!fc_exch_mgr_alloc(lp, FC_CLASS_3, min_xid, max_xid, NULL)) {
499 printk(KERN_ERR "fcoe_em_config: failed to "
500 "allocate em on interface %s\n", fc->netdev->name);
508 * fcoe_if_destroy() - FCoE software HBA tear-down function
509 * @netdev: ptr to the associated net_device
511 * Returns: 0 if link is OK for use by FCoE.
513 static int fcoe_if_destroy(struct net_device *netdev)
515 struct fc_lport *lp = NULL;
516 struct fcoe_softc *fc;
520 FCOE_NETDEV_DBG(netdev, "Destroying interface\n");
522 lp = fcoe_hostlist_lookup(netdev);
528 /* Logout of the fabric */
529 fc_fabric_logoff(lp);
531 /* Remove the instance from fcoe's list */
532 fcoe_hostlist_remove(lp);
534 /* clean up netdev configurations */
535 fcoe_netdev_cleanup(fc);
537 /* tear-down the FCoE controller */
538 fcoe_ctlr_destroy(&fc->ctlr);
540 /* Free queued packets for the per-CPU receive threads */
541 fcoe_percpu_clean(lp);
543 /* Cleanup the fc_lport */
544 fc_lport_destroy(lp);
547 /* Detach from the scsi-ml */
548 fc_remove_host(lp->host);
549 scsi_remove_host(lp->host);
551 /* There are no more rports or I/O, free the EM */
552 fc_exch_mgr_free(lp);
554 /* Free existing skbs */
555 fcoe_clean_pending_queue(lp);
558 del_timer_sync(&fc->timer);
560 /* Free memory used by statistical counters */
561 fc_lport_free_stats(lp);
563 /* Release the net_device and Scsi_Host */
565 scsi_host_put(lp->host);
571 * fcoe_ddp_setup - calls LLD's ddp_setup through net_device
572 * @lp: the corresponding fc_lport
573 * @xid: the exchange id for this ddp transfer
574 * @sgl: the scatterlist describing this transfer
575 * @sgc: number of sg items
579 static int fcoe_ddp_setup(struct fc_lport *lp, u16 xid,
580 struct scatterlist *sgl, unsigned int sgc)
582 struct net_device *n = fcoe_netdev(lp);
584 if (n->netdev_ops && n->netdev_ops->ndo_fcoe_ddp_setup)
585 return n->netdev_ops->ndo_fcoe_ddp_setup(n, xid, sgl, sgc);
591 * fcoe_ddp_done - calls LLD's ddp_done through net_device
592 * @lp: the corresponding fc_lport
593 * @xid: the exchange id for this ddp transfer
595 * Returns : the length of data that have been completed by ddp
597 static int fcoe_ddp_done(struct fc_lport *lp, u16 xid)
599 struct net_device *n = fcoe_netdev(lp);
601 if (n->netdev_ops && n->netdev_ops->ndo_fcoe_ddp_done)
602 return n->netdev_ops->ndo_fcoe_ddp_done(n, xid);
606 static struct libfc_function_template fcoe_libfc_fcn_templ = {
607 .frame_send = fcoe_xmit,
608 .ddp_setup = fcoe_ddp_setup,
609 .ddp_done = fcoe_ddp_done,
613 * fcoe_if_create() - this function creates the fcoe interface
614 * @netdev: pointer the associated netdevice
616 * Creates fc_lport struct and scsi_host for lport, configures lport
617 * and starts fabric login.
619 * Returns : 0 on success
621 static int fcoe_if_create(struct net_device *netdev)
624 struct fc_lport *lp = NULL;
625 struct fcoe_softc *fc;
626 struct Scsi_Host *shost;
630 FCOE_NETDEV_DBG(netdev, "Create Interface\n");
632 lp = fcoe_hostlist_lookup(netdev);
636 shost = libfc_host_alloc(&fcoe_shost_template,
637 sizeof(struct fcoe_softc));
639 FCOE_NETDEV_DBG(netdev, "Could not allocate host structure\n");
642 lp = shost_priv(shost);
645 /* configure fc_lport, e.g., em */
646 rc = fcoe_lport_config(lp);
648 FCOE_NETDEV_DBG(netdev, "Could not configure lport for the "
656 fcoe_ctlr_init(&fc->ctlr);
657 fc->ctlr.send = fcoe_fip_send;
658 fc->ctlr.update_mac = fcoe_update_src_mac;
660 /* configure lport network properties */
661 rc = fcoe_netdev_config(lp, netdev);
663 FCOE_NETDEV_DBG(netdev, "Could not configure netdev for the "
665 goto out_netdev_cleanup;
668 /* configure lport scsi host properties */
669 rc = fcoe_shost_config(lp, shost, &netdev->dev);
671 FCOE_NETDEV_DBG(netdev, "Could not configure shost for the "
673 goto out_netdev_cleanup;
676 /* Initialize the library */
677 rc = fcoe_libfc_config(lp, &fcoe_libfc_fcn_templ);
679 FCOE_NETDEV_DBG(netdev, "Could not configure libfc for the "
685 * fcoe_em_alloc() and fcoe_hostlist_add() both
686 * need to be atomic under fcoe_hostlist_lock
687 * since fcoe_em_alloc() looks for an existing EM
688 * instance on host list updated by fcoe_hostlist_add().
690 write_lock(&fcoe_hostlist_lock);
691 /* lport exch manager allocation */
692 rc = fcoe_em_config(lp);
694 FCOE_NETDEV_DBG(netdev, "Could not configure the EM for the "
699 /* add to lports list */
700 fcoe_hostlist_add(lp);
701 write_unlock(&fcoe_hostlist_lock);
703 lp->boot_time = jiffies;
707 if (!fcoe_link_ok(lp))
708 fcoe_ctlr_link_up(&fc->ctlr);
715 fc_exch_mgr_free(lp);
717 fcoe_netdev_cleanup(fc);
719 scsi_host_put(lp->host);
724 * fcoe_if_init() - attach to scsi transport
726 * Returns : 0 on success
728 static int __init fcoe_if_init(void)
730 /* attach to scsi transport */
731 scsi_transport_fcoe_sw =
732 fc_attach_transport(&fcoe_transport_function);
734 if (!scsi_transport_fcoe_sw) {
735 printk(KERN_ERR "fcoe: Failed to attach to the FC transport\n");
743 * fcoe_if_exit() - detach from scsi transport
745 * Returns : 0 on success
747 int __exit fcoe_if_exit(void)
749 fc_release_transport(scsi_transport_fcoe_sw);
754 * fcoe_percpu_thread_create() - Create a receive thread for an online cpu
755 * @cpu: cpu index for the online cpu
757 static void fcoe_percpu_thread_create(unsigned int cpu)
759 struct fcoe_percpu_s *p;
760 struct task_struct *thread;
762 p = &per_cpu(fcoe_percpu, cpu);
764 thread = kthread_create(fcoe_percpu_receive_thread,
765 (void *)p, "fcoethread/%d", cpu);
767 if (likely(!IS_ERR(p->thread))) {
768 kthread_bind(thread, cpu);
769 wake_up_process(thread);
771 spin_lock_bh(&p->fcoe_rx_list.lock);
773 spin_unlock_bh(&p->fcoe_rx_list.lock);
778 * fcoe_percpu_thread_destroy() - removes the rx thread for the given cpu
779 * @cpu: cpu index the rx thread is to be removed
781 * Destroys a per-CPU Rx thread. Any pending skbs are moved to the
782 * current CPU's Rx thread. If the thread being destroyed is bound to
783 * the CPU processing this context the skbs will be freed.
785 static void fcoe_percpu_thread_destroy(unsigned int cpu)
787 struct fcoe_percpu_s *p;
788 struct task_struct *thread;
789 struct page *crc_eof;
792 struct fcoe_percpu_s *p0;
793 unsigned targ_cpu = smp_processor_id();
794 #endif /* CONFIG_SMP */
796 FCOE_DBG("Destroying receive thread for CPU %d\n", cpu);
798 /* Prevent any new skbs from being queued for this CPU. */
799 p = &per_cpu(fcoe_percpu, cpu);
800 spin_lock_bh(&p->fcoe_rx_list.lock);
803 crc_eof = p->crc_eof_page;
804 p->crc_eof_page = NULL;
805 p->crc_eof_offset = 0;
806 spin_unlock_bh(&p->fcoe_rx_list.lock);
810 * Don't bother moving the skb's if this context is running
811 * on the same CPU that is having its thread destroyed. This
812 * can easily happen when the module is removed.
814 if (cpu != targ_cpu) {
815 p0 = &per_cpu(fcoe_percpu, targ_cpu);
816 spin_lock_bh(&p0->fcoe_rx_list.lock);
818 FCOE_DBG("Moving frames from CPU %d to CPU %d\n",
821 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
822 __skb_queue_tail(&p0->fcoe_rx_list, skb);
823 spin_unlock_bh(&p0->fcoe_rx_list.lock);
826 * The targeted CPU is not initialized and cannot accept
827 * new skbs. Unlock the targeted CPU and drop the skbs
828 * on the CPU that is going offline.
830 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
832 spin_unlock_bh(&p0->fcoe_rx_list.lock);
836 * This scenario occurs when the module is being removed
837 * and all threads are being destroyed. skbs will continue
838 * to be shifted from the CPU thread that is being removed
839 * to the CPU thread associated with the CPU that is processing
840 * the module removal. Once there is only one CPU Rx thread it
841 * will reach this case and we will drop all skbs and later
844 spin_lock_bh(&p->fcoe_rx_list.lock);
845 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
847 spin_unlock_bh(&p->fcoe_rx_list.lock);
851 * This a non-SMP scenario where the singular Rx thread is
852 * being removed. Free all skbs and stop the thread.
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 kthread_stop(thread);
868 * fcoe_cpu_callback() - fcoe cpu hotplug event callback
869 * @nfb: callback data block
870 * @action: event triggering the callback
871 * @hcpu: index for the cpu of this event
873 * This creates or destroys per cpu data for fcoe
875 * Returns NOTIFY_OK always.
877 static int fcoe_cpu_callback(struct notifier_block *nfb,
878 unsigned long action, void *hcpu)
880 unsigned cpu = (unsigned long)hcpu;
884 case CPU_ONLINE_FROZEN:
885 FCOE_DBG("CPU %x online: Create Rx thread\n", cpu);
886 fcoe_percpu_thread_create(cpu);
889 case CPU_DEAD_FROZEN:
890 FCOE_DBG("CPU %x offline: Remove Rx thread\n", cpu);
891 fcoe_percpu_thread_destroy(cpu);
899 static struct notifier_block fcoe_cpu_notifier = {
900 .notifier_call = fcoe_cpu_callback,
904 * fcoe_rcv() - this is the fcoe receive function called by NET_RX_SOFTIRQ
905 * @skb: the receive skb
906 * @dev: associated net device
908 * @olddev: last device
910 * this function will receive the packet and build fc frame and pass it up
912 * Returns: 0 for success
914 int fcoe_rcv(struct sk_buff *skb, struct net_device *dev,
915 struct packet_type *ptype, struct net_device *olddev)
918 struct fcoe_rcv_info *fr;
919 struct fcoe_softc *fc;
920 struct fc_frame_header *fh;
921 struct fcoe_percpu_s *fps;
924 fc = container_of(ptype, struct fcoe_softc, fcoe_packet_type);
926 if (unlikely(lp == NULL)) {
927 FCOE_NETDEV_DBG(dev, "Cannot find hba structure");
933 FCOE_NETDEV_DBG(dev, "skb_info: len:%d data_len:%d head:%p "
934 "data:%p tail:%p end:%p sum:%d dev:%s",
935 skb->len, skb->data_len, skb->head, skb->data,
936 skb_tail_pointer(skb), skb_end_pointer(skb),
937 skb->csum, skb->dev ? skb->dev->name : "<NULL>");
939 /* check for FCOE packet type */
940 if (unlikely(eth_hdr(skb)->h_proto != htons(ETH_P_FCOE))) {
941 FCOE_NETDEV_DBG(dev, "Wrong FC type frame");
946 * Check for minimum frame length, and make sure required FCoE
947 * and FC headers are pulled into the linear data area.
949 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
950 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
953 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
954 fh = (struct fc_frame_header *) skb_transport_header(skb);
956 fr = fcoe_dev_from_skb(skb);
961 * In case the incoming frame's exchange is originated from
962 * the initiator, then received frame's exchange id is ANDed
963 * with fc_cpu_mask bits to get the same cpu on which exchange
964 * was originated, otherwise just use the current cpu.
966 if (ntoh24(fh->fh_f_ctl) & FC_FC_EX_CTX)
967 cpu = ntohs(fh->fh_ox_id) & fc_cpu_mask;
969 cpu = smp_processor_id();
971 fps = &per_cpu(fcoe_percpu, cpu);
972 spin_lock_bh(&fps->fcoe_rx_list.lock);
973 if (unlikely(!fps->thread)) {
975 * The targeted CPU is not ready, let's target
976 * the first CPU now. For non-SMP systems this
977 * will check the same CPU twice.
979 FCOE_NETDEV_DBG(dev, "CPU is online, but no receive thread "
980 "ready for incoming skb- using first online "
983 spin_unlock_bh(&fps->fcoe_rx_list.lock);
984 cpu = first_cpu(cpu_online_map);
985 fps = &per_cpu(fcoe_percpu, cpu);
986 spin_lock_bh(&fps->fcoe_rx_list.lock);
988 spin_unlock_bh(&fps->fcoe_rx_list.lock);
994 * We now have a valid CPU that we're targeting for
995 * this skb. We also have this receive thread locked,
996 * so we're free to queue skbs into it's queue.
998 __skb_queue_tail(&fps->fcoe_rx_list, skb);
999 if (fps->fcoe_rx_list.qlen == 1)
1000 wake_up_process(fps->thread);
1002 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1006 fc_lport_get_stats(lp)->ErrorFrames++;
1014 * fcoe_start_io() - pass to netdev to start xmit for fcoe
1015 * @skb: the skb to be xmitted
1017 * Returns: 0 for success
1019 static inline int fcoe_start_io(struct sk_buff *skb)
1024 rc = dev_queue_xmit(skb);
1032 * fcoe_get_paged_crc_eof() - in case we need to alloc a page for crc_eof
1033 * @skb: the skb to be xmitted
1036 * Returns: 0 for success
1038 static int fcoe_get_paged_crc_eof(struct sk_buff *skb, int tlen)
1040 struct fcoe_percpu_s *fps;
1043 fps = &get_cpu_var(fcoe_percpu);
1044 page = fps->crc_eof_page;
1046 page = alloc_page(GFP_ATOMIC);
1048 put_cpu_var(fcoe_percpu);
1051 fps->crc_eof_page = page;
1052 fps->crc_eof_offset = 0;
1056 skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags, page,
1057 fps->crc_eof_offset, tlen);
1059 skb->data_len += tlen;
1060 skb->truesize += tlen;
1061 fps->crc_eof_offset += sizeof(struct fcoe_crc_eof);
1063 if (fps->crc_eof_offset >= PAGE_SIZE) {
1064 fps->crc_eof_page = NULL;
1065 fps->crc_eof_offset = 0;
1068 put_cpu_var(fcoe_percpu);
1073 * fcoe_fc_crc() - calculates FC CRC in this fcoe skb
1074 * @fp: the fc_frame containing data to be checksummed
1076 * This uses crc32() to calculate the crc for fc frame
1077 * Return : 32 bit crc
1079 u32 fcoe_fc_crc(struct fc_frame *fp)
1081 struct sk_buff *skb = fp_skb(fp);
1082 struct skb_frag_struct *frag;
1083 unsigned char *data;
1084 unsigned long off, len, clen;
1088 crc = crc32(~0, skb->data, skb_headlen(skb));
1090 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1091 frag = &skb_shinfo(skb)->frags[i];
1092 off = frag->page_offset;
1095 clen = min(len, PAGE_SIZE - (off & ~PAGE_MASK));
1096 data = kmap_atomic(frag->page + (off >> PAGE_SHIFT),
1097 KM_SKB_DATA_SOFTIRQ);
1098 crc = crc32(crc, data + (off & ~PAGE_MASK), clen);
1099 kunmap_atomic(data, KM_SKB_DATA_SOFTIRQ);
1108 * fcoe_xmit() - FCoE frame transmit function
1109 * @lp: the associated local port
1110 * @fp: the fc_frame to be transmitted
1112 * Return : 0 for success
1114 int fcoe_xmit(struct fc_lport *lp, struct fc_frame *fp)
1119 struct fcoe_crc_eof *cp;
1120 struct sk_buff *skb;
1121 struct fcoe_dev_stats *stats;
1122 struct fc_frame_header *fh;
1123 unsigned int hlen; /* header length implies the version */
1124 unsigned int tlen; /* trailer length */
1125 unsigned int elen; /* eth header, may include vlan */
1126 struct fcoe_softc *fc;
1128 struct fcoe_hdr *hp;
1130 WARN_ON((fr_len(fp) % sizeof(u32)) != 0);
1132 fc = lport_priv(lp);
1133 fh = fc_frame_header_get(fp);
1135 wlen = skb->len / FCOE_WORD_TO_BYTE;
1142 if (unlikely(fh->fh_r_ctl == FC_RCTL_ELS_REQ) &&
1143 fcoe_ctlr_els_send(&fc->ctlr, skb))
1149 elen = sizeof(struct ethhdr);
1150 hlen = sizeof(struct fcoe_hdr);
1151 tlen = sizeof(struct fcoe_crc_eof);
1152 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
1155 if (likely(lp->crc_offload)) {
1156 skb->ip_summed = CHECKSUM_PARTIAL;
1157 skb->csum_start = skb_headroom(skb);
1158 skb->csum_offset = skb->len;
1161 skb->ip_summed = CHECKSUM_NONE;
1162 crc = fcoe_fc_crc(fp);
1165 /* copy fc crc and eof to the skb buff */
1166 if (skb_is_nonlinear(skb)) {
1168 if (fcoe_get_paged_crc_eof(skb, tlen)) {
1172 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
1173 cp = kmap_atomic(frag->page, KM_SKB_DATA_SOFTIRQ)
1174 + frag->page_offset;
1176 cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
1179 memset(cp, 0, sizeof(*cp));
1181 cp->fcoe_crc32 = cpu_to_le32(~crc);
1183 if (skb_is_nonlinear(skb)) {
1184 kunmap_atomic(cp, KM_SKB_DATA_SOFTIRQ);
1188 /* adjust skb network/transport offsets to match mac/fcoe/fc */
1189 skb_push(skb, elen + hlen);
1190 skb_reset_mac_header(skb);
1191 skb_reset_network_header(skb);
1192 skb->mac_len = elen;
1193 skb->protocol = htons(ETH_P_FCOE);
1194 skb->dev = fc->netdev;
1196 /* fill up mac and fcoe headers */
1198 eh->h_proto = htons(ETH_P_FCOE);
1199 if (fc->ctlr.map_dest)
1200 fc_fcoe_set_mac(eh->h_dest, fh->fh_d_id);
1202 /* insert GW address */
1203 memcpy(eh->h_dest, fc->ctlr.dest_addr, ETH_ALEN);
1205 if (unlikely(fc->ctlr.flogi_oxid != FC_XID_UNKNOWN))
1206 memcpy(eh->h_source, fc->ctlr.ctl_src_addr, ETH_ALEN);
1208 memcpy(eh->h_source, fc->ctlr.data_src_addr, ETH_ALEN);
1210 hp = (struct fcoe_hdr *)(eh + 1);
1211 memset(hp, 0, sizeof(*hp));
1213 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
1216 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
1217 if (lp->seq_offload && fr_max_payload(fp)) {
1218 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
1219 skb_shinfo(skb)->gso_size = fr_max_payload(fp);
1221 skb_shinfo(skb)->gso_type = 0;
1222 skb_shinfo(skb)->gso_size = 0;
1224 /* update tx stats: regardless if LLD fails */
1225 stats = fc_lport_get_stats(lp);
1227 stats->TxWords += wlen;
1229 /* send down to lld */
1231 if (fc->fcoe_pending_queue.qlen)
1232 fcoe_check_wait_queue(lp, skb);
1233 else if (fcoe_start_io(skb))
1234 fcoe_check_wait_queue(lp, skb);
1240 * fcoe_percpu_receive_thread() - recv thread per cpu
1241 * @arg: ptr to the fcoe per cpu struct
1243 * Return: 0 for success
1245 int fcoe_percpu_receive_thread(void *arg)
1247 struct fcoe_percpu_s *p = arg;
1249 struct fc_lport *lp;
1250 struct fcoe_rcv_info *fr;
1251 struct fcoe_dev_stats *stats;
1252 struct fc_frame_header *fh;
1253 struct sk_buff *skb;
1254 struct fcoe_crc_eof crc_eof;
1255 struct fc_frame *fp;
1257 struct fcoe_softc *fc;
1258 struct fcoe_hdr *hp;
1260 set_user_nice(current, -20);
1262 while (!kthread_should_stop()) {
1264 spin_lock_bh(&p->fcoe_rx_list.lock);
1265 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) == NULL) {
1266 set_current_state(TASK_INTERRUPTIBLE);
1267 spin_unlock_bh(&p->fcoe_rx_list.lock);
1269 set_current_state(TASK_RUNNING);
1270 if (kthread_should_stop())
1272 spin_lock_bh(&p->fcoe_rx_list.lock);
1274 spin_unlock_bh(&p->fcoe_rx_list.lock);
1275 fr = fcoe_dev_from_skb(skb);
1277 if (unlikely(lp == NULL)) {
1278 FCOE_NETDEV_DBG(skb->dev, "Invalid HBA Structure");
1283 FCOE_NETDEV_DBG(skb->dev, "skb_info: len:%d data_len:%d "
1284 "head:%p data:%p tail:%p end:%p sum:%d dev:%s",
1285 skb->len, skb->data_len,
1286 skb->head, skb->data, skb_tail_pointer(skb),
1287 skb_end_pointer(skb), skb->csum,
1288 skb->dev ? skb->dev->name : "<NULL>");
1291 * Save source MAC address before discarding header.
1293 fc = lport_priv(lp);
1294 if (skb_is_nonlinear(skb))
1295 skb_linearize(skb); /* not ideal */
1296 mac = eth_hdr(skb)->h_source;
1299 * Frame length checks and setting up the header pointers
1300 * was done in fcoe_rcv already.
1302 hp = (struct fcoe_hdr *) skb_network_header(skb);
1303 fh = (struct fc_frame_header *) skb_transport_header(skb);
1305 stats = fc_lport_get_stats(lp);
1306 if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) {
1307 if (stats->ErrorFrames < 5)
1308 printk(KERN_WARNING "fcoe: FCoE version "
1309 "mismatch: The frame has "
1310 "version %x, but the "
1311 "initiator supports version "
1312 "%x\n", FC_FCOE_DECAPS_VER(hp),
1314 stats->ErrorFrames++;
1319 skb_pull(skb, sizeof(struct fcoe_hdr));
1320 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
1323 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
1325 fp = (struct fc_frame *)skb;
1328 fr_sof(fp) = hp->fcoe_sof;
1330 /* Copy out the CRC and EOF trailer for access */
1331 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof))) {
1335 fr_eof(fp) = crc_eof.fcoe_eof;
1336 fr_crc(fp) = crc_eof.fcoe_crc32;
1337 if (pskb_trim(skb, fr_len)) {
1343 * We only check CRC if no offload is available and if it is
1344 * it's solicited data, in which case, the FCP layer would
1345 * check it during the copy.
1347 if (lp->crc_offload && skb->ip_summed == CHECKSUM_UNNECESSARY)
1348 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1350 fr_flags(fp) |= FCPHF_CRC_UNCHECKED;
1352 fh = fc_frame_header_get(fp);
1353 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA &&
1354 fh->fh_type == FC_TYPE_FCP) {
1355 fc_exch_recv(lp, fp);
1358 if (fr_flags(fp) & FCPHF_CRC_UNCHECKED) {
1359 if (le32_to_cpu(fr_crc(fp)) !=
1360 ~crc32(~0, skb->data, fr_len)) {
1361 if (stats->InvalidCRCCount < 5)
1362 printk(KERN_WARNING "fcoe: dropping "
1363 "frame with CRC error\n");
1364 stats->InvalidCRCCount++;
1365 stats->ErrorFrames++;
1369 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1371 if (unlikely(fc->ctlr.flogi_oxid != FC_XID_UNKNOWN) &&
1372 fcoe_ctlr_recv_flogi(&fc->ctlr, fp, mac)) {
1376 fc_exch_recv(lp, fp);
1382 * fcoe_check_wait_queue() - attempt to clear the transmit backlog
1385 * This empties the wait_queue, dequeue the head of the wait_queue queue
1386 * and calls fcoe_start_io() for each packet, if all skb have been
1387 * transmitted, return qlen or -1 if a error occurs, then restore
1388 * wait_queue and try again later.
1390 * The wait_queue is used when the skb transmit fails. skb will go
1391 * in the wait_queue which will be emptied by the timer function or
1392 * by the next skb transmit.
1394 static void fcoe_check_wait_queue(struct fc_lport *lp, struct sk_buff *skb)
1396 struct fcoe_softc *fc = lport_priv(lp);
1399 spin_lock_bh(&fc->fcoe_pending_queue.lock);
1402 __skb_queue_tail(&fc->fcoe_pending_queue, skb);
1404 if (fc->fcoe_pending_queue_active)
1406 fc->fcoe_pending_queue_active = 1;
1408 while (fc->fcoe_pending_queue.qlen) {
1409 /* keep qlen > 0 until fcoe_start_io succeeds */
1410 fc->fcoe_pending_queue.qlen++;
1411 skb = __skb_dequeue(&fc->fcoe_pending_queue);
1413 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
1414 rc = fcoe_start_io(skb);
1415 spin_lock_bh(&fc->fcoe_pending_queue.lock);
1418 __skb_queue_head(&fc->fcoe_pending_queue, skb);
1419 /* undo temporary increment above */
1420 fc->fcoe_pending_queue.qlen--;
1423 /* undo temporary increment above */
1424 fc->fcoe_pending_queue.qlen--;
1427 if (fc->fcoe_pending_queue.qlen < FCOE_LOW_QUEUE_DEPTH)
1429 if (fc->fcoe_pending_queue.qlen && !timer_pending(&fc->timer))
1430 mod_timer(&fc->timer, jiffies + 2);
1431 fc->fcoe_pending_queue_active = 0;
1433 if (fc->fcoe_pending_queue.qlen > FCOE_MAX_QUEUE_DEPTH)
1435 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
1440 * fcoe_dev_setup() - setup link change notification interface
1442 static void fcoe_dev_setup(void)
1444 register_netdevice_notifier(&fcoe_notifier);
1448 * fcoe_dev_cleanup() - cleanup link change notification interface
1450 static void fcoe_dev_cleanup(void)
1452 unregister_netdevice_notifier(&fcoe_notifier);
1456 * fcoe_device_notification() - netdev event notification callback
1457 * @notifier: context of the notification
1458 * @event: type of event
1459 * @ptr: fixed array for output parsed ifname
1461 * This function is called by the ethernet driver in case of link change event
1463 * Returns: 0 for success
1465 static int fcoe_device_notification(struct notifier_block *notifier,
1466 ulong event, void *ptr)
1468 struct fc_lport *lp = NULL;
1469 struct net_device *netdev = ptr;
1470 struct fcoe_softc *fc;
1471 struct fcoe_dev_stats *stats;
1472 u32 link_possible = 1;
1476 read_lock(&fcoe_hostlist_lock);
1477 list_for_each_entry(fc, &fcoe_hostlist, list) {
1478 if (fc->netdev == netdev) {
1483 read_unlock(&fcoe_hostlist_lock);
1491 case NETDEV_GOING_DOWN:
1497 case NETDEV_CHANGEMTU:
1498 mfs = netdev->mtu - (sizeof(struct fcoe_hdr) +
1499 sizeof(struct fcoe_crc_eof));
1500 if (mfs >= FC_MIN_MAX_FRAME)
1501 fc_set_mfs(lp, mfs);
1503 case NETDEV_REGISTER:
1506 FCOE_NETDEV_DBG(netdev, "Unknown event %ld "
1507 "from netdev netlink\n", event);
1509 if (link_possible && !fcoe_link_ok(lp))
1510 fcoe_ctlr_link_up(&fc->ctlr);
1511 else if (fcoe_ctlr_link_down(&fc->ctlr)) {
1512 stats = fc_lport_get_stats(lp);
1513 stats->LinkFailureCount++;
1514 fcoe_clean_pending_queue(lp);
1521 * fcoe_if_to_netdev() - parse a name buffer to get netdev
1522 * @buffer: incoming buffer to be copied
1524 * Returns: NULL or ptr to net_device
1526 static struct net_device *fcoe_if_to_netdev(const char *buffer)
1529 char ifname[IFNAMSIZ + 2];
1532 strlcpy(ifname, buffer, IFNAMSIZ);
1533 cp = ifname + strlen(ifname);
1534 while (--cp >= ifname && *cp == '\n')
1536 return dev_get_by_name(&init_net, ifname);
1542 * fcoe_netdev_to_module_owner() - finds out the driver module of the netdev
1543 * @netdev: the target netdev
1545 * Returns: ptr to the struct module, NULL for failure
1547 static struct module *
1548 fcoe_netdev_to_module_owner(const struct net_device *netdev)
1555 dev = netdev->dev.parent;
1562 return dev->driver->owner;
1566 * fcoe_ethdrv_get() - Hold the Ethernet driver
1567 * @netdev: the target netdev
1569 * Holds the Ethernet driver module by try_module_get() for
1570 * the corresponding netdev.
1572 * Returns: 0 for success
1574 static int fcoe_ethdrv_get(const struct net_device *netdev)
1576 struct module *owner;
1578 owner = fcoe_netdev_to_module_owner(netdev);
1580 FCOE_NETDEV_DBG(netdev, "Hold driver module %s\n",
1581 module_name(owner));
1582 return try_module_get(owner);
1588 * fcoe_ethdrv_put() - Release the Ethernet driver
1589 * @netdev: the target netdev
1591 * Releases the Ethernet driver module by module_put for
1592 * the corresponding netdev.
1594 * Returns: 0 for success
1596 static int fcoe_ethdrv_put(const struct net_device *netdev)
1598 struct module *owner;
1600 owner = fcoe_netdev_to_module_owner(netdev);
1602 FCOE_NETDEV_DBG(netdev, "Release driver module %s\n",
1603 module_name(owner));
1611 * fcoe_destroy() - handles the destroy from sysfs
1612 * @buffer: expected to be an eth if name
1613 * @kp: associated kernel param
1615 * Returns: 0 for success
1617 static int fcoe_destroy(const char *buffer, struct kernel_param *kp)
1620 struct net_device *netdev;
1622 netdev = fcoe_if_to_netdev(buffer);
1627 /* look for existing lport */
1628 if (!fcoe_hostlist_lookup(netdev)) {
1632 rc = fcoe_if_destroy(netdev);
1634 printk(KERN_ERR "fcoe: Failed to destroy interface (%s)\n",
1639 fcoe_ethdrv_put(netdev);
1648 * fcoe_create() - Handles the create call from sysfs
1649 * @buffer: expected to be an eth if name
1650 * @kp: associated kernel param
1652 * Returns: 0 for success
1654 static int fcoe_create(const char *buffer, struct kernel_param *kp)
1657 struct net_device *netdev;
1659 netdev = fcoe_if_to_netdev(buffer);
1664 /* look for existing lport */
1665 if (fcoe_hostlist_lookup(netdev)) {
1669 fcoe_ethdrv_get(netdev);
1671 rc = fcoe_if_create(netdev);
1673 printk(KERN_ERR "fcoe: Failed to create interface (%s)\n",
1675 fcoe_ethdrv_put(netdev);
1686 module_param_call(create, fcoe_create, NULL, NULL, S_IWUSR);
1687 __MODULE_PARM_TYPE(create, "string");
1688 MODULE_PARM_DESC(create, "Create fcoe port using net device passed in.");
1689 module_param_call(destroy, fcoe_destroy, NULL, NULL, S_IWUSR);
1690 __MODULE_PARM_TYPE(destroy, "string");
1691 MODULE_PARM_DESC(destroy, "Destroy fcoe port");
1694 * fcoe_link_ok() - Check if link is ok for the fc_lport
1695 * @lp: ptr to the fc_lport
1697 * Any permanently-disqualifying conditions have been previously checked.
1698 * This also updates the speed setting, which may change with link for 100/1000.
1700 * This function should probably be checking for PAUSE support at some point
1701 * in the future. Currently Per-priority-pause is not determinable using
1702 * ethtool, so we shouldn't be restrictive until that problem is resolved.
1704 * Returns: 0 if link is OK for use by FCoE.
1707 int fcoe_link_ok(struct fc_lport *lp)
1709 struct fcoe_softc *fc = lport_priv(lp);
1710 struct net_device *dev = fc->netdev;
1711 struct ethtool_cmd ecmd = { ETHTOOL_GSET };
1713 if ((dev->flags & IFF_UP) && netif_carrier_ok(dev) &&
1714 (!dev_ethtool_get_settings(dev, &ecmd))) {
1715 lp->link_supported_speeds &=
1716 ~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
1717 if (ecmd.supported & (SUPPORTED_1000baseT_Half |
1718 SUPPORTED_1000baseT_Full))
1719 lp->link_supported_speeds |= FC_PORTSPEED_1GBIT;
1720 if (ecmd.supported & SUPPORTED_10000baseT_Full)
1721 lp->link_supported_speeds |=
1722 FC_PORTSPEED_10GBIT;
1723 if (ecmd.speed == SPEED_1000)
1724 lp->link_speed = FC_PORTSPEED_1GBIT;
1725 if (ecmd.speed == SPEED_10000)
1726 lp->link_speed = FC_PORTSPEED_10GBIT;
1734 * fcoe_percpu_clean() - Clear the pending skbs for an lport
1737 void fcoe_percpu_clean(struct fc_lport *lp)
1739 struct fcoe_percpu_s *pp;
1740 struct fcoe_rcv_info *fr;
1741 struct sk_buff_head *list;
1742 struct sk_buff *skb, *next;
1743 struct sk_buff *head;
1746 for_each_possible_cpu(cpu) {
1747 pp = &per_cpu(fcoe_percpu, cpu);
1748 spin_lock_bh(&pp->fcoe_rx_list.lock);
1749 list = &pp->fcoe_rx_list;
1751 for (skb = head; skb != (struct sk_buff *)list;
1754 fr = fcoe_dev_from_skb(skb);
1755 if (fr->fr_dev == lp) {
1756 __skb_unlink(skb, list);
1760 spin_unlock_bh(&pp->fcoe_rx_list.lock);
1765 * fcoe_clean_pending_queue() - Dequeue a skb and free it
1766 * @lp: the corresponding fc_lport
1770 void fcoe_clean_pending_queue(struct fc_lport *lp)
1772 struct fcoe_softc *fc = lport_priv(lp);
1773 struct sk_buff *skb;
1775 spin_lock_bh(&fc->fcoe_pending_queue.lock);
1776 while ((skb = __skb_dequeue(&fc->fcoe_pending_queue)) != NULL) {
1777 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
1779 spin_lock_bh(&fc->fcoe_pending_queue.lock);
1781 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
1785 * fcoe_reset() - Resets the fcoe
1786 * @shost: shost the reset is from
1790 int fcoe_reset(struct Scsi_Host *shost)
1792 struct fc_lport *lport = shost_priv(shost);
1793 fc_lport_reset(lport);
1798 * fcoe_hostlist_lookup_softc() - find the corresponding lport by a given device
1799 * @dev: this is currently ptr to net_device
1801 * Called with fcoe_hostlist_lock held.
1803 * Returns: NULL or the located fcoe_softc
1805 static struct fcoe_softc *
1806 fcoe_hostlist_lookup_softc(const struct net_device *dev)
1808 struct fcoe_softc *fc;
1810 list_for_each_entry(fc, &fcoe_hostlist, list) {
1811 if (fc->netdev == dev)
1818 * fcoe_hostlist_lookup() - Find the corresponding lport by netdev
1819 * @netdev: ptr to net_device
1821 * Returns: 0 for success
1823 struct fc_lport *fcoe_hostlist_lookup(const struct net_device *netdev)
1825 struct fcoe_softc *fc;
1827 read_lock(&fcoe_hostlist_lock);
1828 fc = fcoe_hostlist_lookup_softc(netdev);
1829 read_unlock(&fcoe_hostlist_lock);
1831 return (fc) ? fc->ctlr.lp : NULL;
1835 * fcoe_hostlist_add() - Add a lport to lports list
1836 * @lp: ptr to the fc_lport to be added
1838 * Called with write fcoe_hostlist_lock held.
1840 * Returns: 0 for success
1842 int fcoe_hostlist_add(const struct fc_lport *lp)
1844 struct fcoe_softc *fc;
1846 fc = fcoe_hostlist_lookup_softc(fcoe_netdev(lp));
1848 fc = lport_priv(lp);
1849 list_add_tail(&fc->list, &fcoe_hostlist);
1855 * fcoe_hostlist_remove() - remove a lport from lports list
1856 * @lp: ptr to the fc_lport to be removed
1858 * Returns: 0 for success
1860 int fcoe_hostlist_remove(const struct fc_lport *lp)
1862 struct fcoe_softc *fc;
1864 write_lock_bh(&fcoe_hostlist_lock);
1865 fc = fcoe_hostlist_lookup_softc(fcoe_netdev(lp));
1867 list_del(&fc->list);
1868 write_unlock_bh(&fcoe_hostlist_lock);
1874 * fcoe_init() - fcoe module loading initialization
1876 * Returns 0 on success, negative on failure
1878 static int __init fcoe_init(void)
1882 struct fcoe_percpu_s *p;
1884 INIT_LIST_HEAD(&fcoe_hostlist);
1885 rwlock_init(&fcoe_hostlist_lock);
1887 for_each_possible_cpu(cpu) {
1888 p = &per_cpu(fcoe_percpu, cpu);
1889 skb_queue_head_init(&p->fcoe_rx_list);
1892 for_each_online_cpu(cpu)
1893 fcoe_percpu_thread_create(cpu);
1895 /* Initialize per CPU interrupt thread */
1896 rc = register_hotcpu_notifier(&fcoe_cpu_notifier);
1900 /* Setup link change notification */
1903 rc = fcoe_if_init();
1910 for_each_online_cpu(cpu) {
1911 fcoe_percpu_thread_destroy(cpu);
1916 module_init(fcoe_init);
1919 * fcoe_exit() - fcoe module unloading cleanup
1921 * Returns 0 on success, negative on failure
1923 static void __exit fcoe_exit(void)
1926 struct fcoe_softc *fc, *tmp;
1930 /* releases the associated fcoe hosts */
1931 list_for_each_entry_safe(fc, tmp, &fcoe_hostlist, list)
1932 fcoe_if_destroy(fc->netdev);
1934 unregister_hotcpu_notifier(&fcoe_cpu_notifier);
1936 for_each_online_cpu(cpu) {
1937 fcoe_percpu_thread_destroy(cpu);
1940 /* detach from scsi transport */
1943 module_exit(fcoe_exit);