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_interface *fcoe;
156 fcoe = container_of(ptype, struct fcoe_interface, fip_packet_type);
157 fcoe_ctlr_recv(&fcoe->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_interface *fcoe;
185 fcoe = fcoe_from_ctlr(fip);
187 if (!is_zero_ether_addr(old))
188 dev_unicast_delete(fcoe->netdev, old);
189 dev_unicast_add(fcoe->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 * @port: ptr to the fcoe_port
229 void fcoe_netdev_cleanup(struct fcoe_port *port)
231 u8 flogi_maddr[ETH_ALEN];
232 struct fcoe_interface *fcoe = port->fcoe;
234 /* Don't listen for Ethernet packets anymore */
235 dev_remove_pack(&fcoe->fcoe_packet_type);
236 dev_remove_pack(&fcoe->fip_packet_type);
238 /* Delete secondary MAC addresses */
240 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
241 dev_unicast_delete(fcoe->netdev, flogi_maddr);
242 if (!is_zero_ether_addr(fcoe->ctlr.data_src_addr))
243 dev_unicast_delete(fcoe->netdev, fcoe->ctlr.data_src_addr);
245 dev_unicast_delete(fcoe->netdev, fcoe->ctlr.ctl_src_addr);
246 dev_mc_delete(fcoe->netdev, FIP_ALL_ENODE_MACS, ETH_ALEN, 0);
251 * fcoe_queue_timer() - fcoe queue timer
252 * @lp: the fc_lport pointer
254 * Calls fcoe_check_wait_queue on timeout
257 static void fcoe_queue_timer(ulong lp)
259 fcoe_check_wait_queue((struct fc_lport *)lp, NULL);
263 * fcoe_netdev_config() - Set up netdev for SW FCoE
264 * @lp : ptr to the fc_lport
265 * @netdev : ptr to the associated netdevice struct
267 * Must be called after fcoe_lport_config() as it will use lport mutex
269 * Returns : 0 for success
271 static int fcoe_netdev_config(struct fc_lport *lp, struct net_device *netdev)
275 struct fcoe_interface *fcoe;
276 struct fcoe_port *port;
277 u8 flogi_maddr[ETH_ALEN];
278 struct netdev_hw_addr *ha;
280 /* Setup lport private data to point to fcoe softc */
281 port = lport_priv(lp);
284 fcoe->netdev = netdev;
286 /* Do not support for bonding device */
287 if ((netdev->priv_flags & IFF_MASTER_ALB) ||
288 (netdev->priv_flags & IFF_SLAVE_INACTIVE) ||
289 (netdev->priv_flags & IFF_MASTER_8023AD)) {
294 * Determine max frame size based on underlying device and optional
295 * user-configured limit. If the MFS is too low, fcoe_link_ok()
296 * will return 0, so do this first.
298 mfs = netdev->mtu - (sizeof(struct fcoe_hdr) +
299 sizeof(struct fcoe_crc_eof));
300 if (fc_set_mfs(lp, mfs))
303 /* offload features support */
304 if (netdev->features & NETIF_F_SG)
307 if (netdev->features & NETIF_F_FCOE_CRC) {
309 FCOE_NETDEV_DBG(netdev, "Supports FCCRC offload\n");
311 if (netdev->features & NETIF_F_FSO) {
313 lp->lso_max = netdev->gso_max_size;
314 FCOE_NETDEV_DBG(netdev, "Supports LSO for max len 0x%x\n",
317 if (netdev->fcoe_ddp_xid) {
319 lp->lro_xid = netdev->fcoe_ddp_xid;
320 FCOE_NETDEV_DBG(netdev, "Supports LRO for max xid 0x%x\n",
323 skb_queue_head_init(&port->fcoe_pending_queue);
324 port->fcoe_pending_queue_active = 0;
325 setup_timer(&port->timer, fcoe_queue_timer, (unsigned long)lp);
327 /* look for SAN MAC address, if multiple SAN MACs exist, only
328 * use the first one for SPMA */
330 for_each_dev_addr(netdev, ha) {
331 if ((ha->type == NETDEV_HW_ADDR_T_SAN) &&
332 (is_valid_ether_addr(fcoe->ctlr.ctl_src_addr))) {
333 memcpy(fcoe->ctlr.ctl_src_addr, ha->addr, ETH_ALEN);
340 /* setup Source Mac Address */
341 if (!fcoe->ctlr.spma)
342 memcpy(fcoe->ctlr.ctl_src_addr, netdev->dev_addr,
345 wwnn = fcoe_wwn_from_mac(netdev->dev_addr, 1, 0);
346 fc_set_wwnn(lp, wwnn);
347 /* XXX - 3rd arg needs to be vlan id */
348 wwpn = fcoe_wwn_from_mac(netdev->dev_addr, 2, 0);
349 fc_set_wwpn(lp, wwpn);
352 * Add FCoE MAC address as second unicast MAC address
353 * or enter promiscuous mode if not capable of listening
354 * for multiple unicast MACs.
357 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
358 dev_unicast_add(netdev, flogi_maddr);
360 dev_unicast_add(netdev, fcoe->ctlr.ctl_src_addr);
361 dev_mc_add(netdev, FIP_ALL_ENODE_MACS, ETH_ALEN, 0);
365 * setup the receive function from ethernet driver
366 * on the ethertype for the given device
368 fcoe->fcoe_packet_type.func = fcoe_rcv;
369 fcoe->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
370 fcoe->fcoe_packet_type.dev = netdev;
371 dev_add_pack(&fcoe->fcoe_packet_type);
373 fcoe->fip_packet_type.func = fcoe_fip_recv;
374 fcoe->fip_packet_type.type = htons(ETH_P_FIP);
375 fcoe->fip_packet_type.dev = netdev;
376 dev_add_pack(&fcoe->fip_packet_type);
382 * fcoe_shost_config() - Sets up fc_lport->host
383 * @lp : ptr to the fc_lport
384 * @shost : ptr to the associated scsi host
385 * @dev : device associated to scsi host
387 * Must be called after fcoe_lport_config() and fcoe_netdev_config()
389 * Returns : 0 for success
391 static int fcoe_shost_config(struct fc_lport *lp, struct Scsi_Host *shost,
396 /* lport scsi host config */
399 lp->host->max_lun = FCOE_MAX_LUN;
400 lp->host->max_id = FCOE_MAX_FCP_TARGET;
401 lp->host->max_channel = 0;
402 lp->host->transportt = scsi_transport_fcoe_sw;
404 /* add the new host to the SCSI-ml */
405 rc = scsi_add_host(lp->host, dev);
407 FCOE_NETDEV_DBG(fcoe_netdev(lp), "fcoe_shost_config: "
408 "error on scsi_add_host\n");
411 sprintf(fc_host_symbolic_name(lp->host), "%s v%s over %s",
412 FCOE_NAME, FCOE_VERSION,
413 fcoe_netdev(lp)->name);
419 * fcoe_oem_match() - match for read types IO
420 * @fp: the fc_frame for new IO.
422 * Returns : true for read types IO, otherwise returns false.
424 bool fcoe_oem_match(struct fc_frame *fp)
426 return fc_fcp_is_read(fr_fsp(fp)) &&
427 (fr_fsp(fp)->data_len > fcoe_ddp_min);
431 * fcoe_em_config() - allocates em for this lport
432 * @lp: the fcoe that em is to allocated for
434 * Called with write fcoe_hostlist_lock held.
436 * Returns : 0 on success
438 static inline int fcoe_em_config(struct fc_lport *lp)
440 struct fcoe_port *port = lport_priv(lp);
441 struct fcoe_interface *fcoe = port->fcoe;
442 struct fcoe_interface *oldfcoe = NULL;
443 struct net_device *old_real_dev, *cur_real_dev;
444 u16 min_xid = FCOE_MIN_XID;
445 u16 max_xid = FCOE_MAX_XID;
448 * Check if need to allocate an em instance for
449 * offload exchange ids to be shared across all VN_PORTs/lport.
451 if (!lp->lro_enabled || !lp->lro_xid || (lp->lro_xid >= max_xid)) {
457 * Reuse existing offload em instance in case
458 * it is already allocated on real eth device
460 if (fcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
461 cur_real_dev = vlan_dev_real_dev(fcoe->netdev);
463 cur_real_dev = fcoe->netdev;
465 list_for_each_entry(oldfcoe, &fcoe_hostlist, list) {
466 if (oldfcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
467 old_real_dev = vlan_dev_real_dev(oldfcoe->netdev);
469 old_real_dev = oldfcoe->netdev;
471 if (cur_real_dev == old_real_dev) {
472 fcoe->oem = oldfcoe->oem;
478 if (!fc_exch_mgr_add(lp, fcoe->oem, fcoe_oem_match)) {
479 printk(KERN_ERR "fcoe_em_config: failed to add "
480 "offload em:%p on interface:%s\n",
481 fcoe->oem, fcoe->netdev->name);
485 fcoe->oem = fc_exch_mgr_alloc(lp, FC_CLASS_3,
486 FCOE_MIN_XID, lp->lro_xid,
489 printk(KERN_ERR "fcoe_em_config: failed to allocate "
490 "em for offload exches on interface:%s\n",
497 * Exclude offload EM xid range from next EM xid range.
499 min_xid += lp->lro_xid + 1;
502 if (!fc_exch_mgr_alloc(lp, FC_CLASS_3, min_xid, max_xid, NULL)) {
503 printk(KERN_ERR "fcoe_em_config: failed to "
504 "allocate em on interface %s\n", fcoe->netdev->name);
512 * fcoe_if_destroy() - FCoE software HBA tear-down function
513 * @lport: fc_lport to destroy
515 static void fcoe_if_destroy(struct fc_lport *lport)
517 struct fcoe_port *port = lport_priv(lport);
518 struct fcoe_interface *fcoe = port->fcoe;
519 struct net_device *netdev = fcoe->netdev;
521 FCOE_NETDEV_DBG(netdev, "Destroying interface\n");
523 /* Logout of the fabric */
524 fc_fabric_logoff(lport);
526 /* Remove the instance from fcoe's list */
527 fcoe_hostlist_remove(lport);
529 /* clean up netdev configurations */
530 fcoe_netdev_cleanup(port);
532 /* tear-down the FCoE controller */
533 fcoe_ctlr_destroy(&fcoe->ctlr);
535 /* Free queued packets for the per-CPU receive threads */
536 fcoe_percpu_clean(lport);
538 /* Cleanup the fc_lport */
539 fc_lport_destroy(lport);
540 fc_fcp_destroy(lport);
542 /* Detach from the scsi-ml */
543 fc_remove_host(lport->host);
544 scsi_remove_host(lport->host);
546 /* There are no more rports or I/O, free the EM */
547 fc_exch_mgr_free(lport);
549 /* Free existing skbs */
550 fcoe_clean_pending_queue(lport);
553 del_timer_sync(&port->timer);
555 /* Free memory used by statistical counters */
556 fc_lport_free_stats(lport);
558 /* Release the net_device and Scsi_Host */
560 scsi_host_put(lport->host);
561 kfree(fcoe); /* TODO, should be refcounted */
565 * fcoe_ddp_setup - calls LLD's ddp_setup through net_device
566 * @lp: the corresponding fc_lport
567 * @xid: the exchange id for this ddp transfer
568 * @sgl: the scatterlist describing this transfer
569 * @sgc: number of sg items
573 static int fcoe_ddp_setup(struct fc_lport *lp, u16 xid,
574 struct scatterlist *sgl, unsigned int sgc)
576 struct net_device *n = fcoe_netdev(lp);
578 if (n->netdev_ops && n->netdev_ops->ndo_fcoe_ddp_setup)
579 return n->netdev_ops->ndo_fcoe_ddp_setup(n, xid, sgl, sgc);
585 * fcoe_ddp_done - calls LLD's ddp_done through net_device
586 * @lp: the corresponding fc_lport
587 * @xid: the exchange id for this ddp transfer
589 * Returns : the length of data that have been completed by ddp
591 static int fcoe_ddp_done(struct fc_lport *lp, u16 xid)
593 struct net_device *n = fcoe_netdev(lp);
595 if (n->netdev_ops && n->netdev_ops->ndo_fcoe_ddp_done)
596 return n->netdev_ops->ndo_fcoe_ddp_done(n, xid);
600 static struct libfc_function_template fcoe_libfc_fcn_templ = {
601 .frame_send = fcoe_xmit,
602 .ddp_setup = fcoe_ddp_setup,
603 .ddp_done = fcoe_ddp_done,
607 * fcoe_if_create() - this function creates the fcoe interface
608 * @netdev: pointer the associated netdevice
609 * @parent: device pointer to be the parent in sysfs for the SCSI host
611 * Creates fc_lport struct and scsi_host for lport, configures lport
612 * and starts fabric login.
614 * Returns : The allocated fc_lport or an error pointer
616 static struct fc_lport *fcoe_if_create(struct net_device *netdev,
617 struct device *parent)
620 struct fc_lport *lport = NULL;
621 struct fcoe_port *port;
622 struct fcoe_interface *fcoe;
623 struct Scsi_Host *shost;
625 FCOE_NETDEV_DBG(netdev, "Create Interface\n");
627 fcoe = kzalloc(sizeof(*fcoe), GFP_KERNEL);
629 FCOE_NETDEV_DBG(netdev, "Could not allocate fcoe structure\n");
634 shost = libfc_host_alloc(&fcoe_shost_template,
635 sizeof(struct fcoe_port));
637 FCOE_NETDEV_DBG(netdev, "Could not allocate host structure\n");
641 lport = shost_priv(shost);
642 port = lport_priv(lport);
645 /* configure fc_lport, e.g., em */
646 rc = fcoe_lport_config(lport);
648 FCOE_NETDEV_DBG(netdev, "Could not configure lport for the "
656 fcoe_ctlr_init(&fcoe->ctlr);
657 fcoe->ctlr.send = fcoe_fip_send;
658 fcoe->ctlr.update_mac = fcoe_update_src_mac;
660 /* configure lport network properties */
661 rc = fcoe_netdev_config(lport, 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(lport, shost, parent);
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(lport, &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(lport);
694 FCOE_NETDEV_DBG(netdev, "Could not configure the EM for the "
699 /* add to lports list */
700 fcoe_hostlist_add(lport);
701 write_unlock(&fcoe_hostlist_lock);
703 lport->boot_time = jiffies;
705 fc_fabric_login(lport);
707 if (!fcoe_link_ok(lport))
708 fcoe_ctlr_link_up(&fcoe->ctlr);
715 fc_exch_mgr_free(lport);
717 fcoe_netdev_cleanup(port);
719 scsi_host_put(lport->host);
727 * fcoe_if_init() - attach to scsi transport
729 * Returns : 0 on success
731 static int __init fcoe_if_init(void)
733 /* attach to scsi transport */
734 scsi_transport_fcoe_sw =
735 fc_attach_transport(&fcoe_transport_function);
737 if (!scsi_transport_fcoe_sw) {
738 printk(KERN_ERR "fcoe: Failed to attach to the FC transport\n");
746 * fcoe_if_exit() - detach from scsi transport
748 * Returns : 0 on success
750 int __exit fcoe_if_exit(void)
752 fc_release_transport(scsi_transport_fcoe_sw);
757 * fcoe_percpu_thread_create() - Create a receive thread for an online cpu
758 * @cpu: cpu index for the online cpu
760 static void fcoe_percpu_thread_create(unsigned int cpu)
762 struct fcoe_percpu_s *p;
763 struct task_struct *thread;
765 p = &per_cpu(fcoe_percpu, cpu);
767 thread = kthread_create(fcoe_percpu_receive_thread,
768 (void *)p, "fcoethread/%d", cpu);
770 if (likely(!IS_ERR(p->thread))) {
771 kthread_bind(thread, cpu);
772 wake_up_process(thread);
774 spin_lock_bh(&p->fcoe_rx_list.lock);
776 spin_unlock_bh(&p->fcoe_rx_list.lock);
781 * fcoe_percpu_thread_destroy() - removes the rx thread for the given cpu
782 * @cpu: cpu index the rx thread is to be removed
784 * Destroys a per-CPU Rx thread. Any pending skbs are moved to the
785 * current CPU's Rx thread. If the thread being destroyed is bound to
786 * the CPU processing this context the skbs will be freed.
788 static void fcoe_percpu_thread_destroy(unsigned int cpu)
790 struct fcoe_percpu_s *p;
791 struct task_struct *thread;
792 struct page *crc_eof;
795 struct fcoe_percpu_s *p0;
796 unsigned targ_cpu = smp_processor_id();
797 #endif /* CONFIG_SMP */
799 FCOE_DBG("Destroying receive thread for CPU %d\n", cpu);
801 /* Prevent any new skbs from being queued for this CPU. */
802 p = &per_cpu(fcoe_percpu, cpu);
803 spin_lock_bh(&p->fcoe_rx_list.lock);
806 crc_eof = p->crc_eof_page;
807 p->crc_eof_page = NULL;
808 p->crc_eof_offset = 0;
809 spin_unlock_bh(&p->fcoe_rx_list.lock);
813 * Don't bother moving the skb's if this context is running
814 * on the same CPU that is having its thread destroyed. This
815 * can easily happen when the module is removed.
817 if (cpu != targ_cpu) {
818 p0 = &per_cpu(fcoe_percpu, targ_cpu);
819 spin_lock_bh(&p0->fcoe_rx_list.lock);
821 FCOE_DBG("Moving frames from CPU %d to CPU %d\n",
824 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
825 __skb_queue_tail(&p0->fcoe_rx_list, skb);
826 spin_unlock_bh(&p0->fcoe_rx_list.lock);
829 * The targeted CPU is not initialized and cannot accept
830 * new skbs. Unlock the targeted CPU and drop the skbs
831 * on the CPU that is going offline.
833 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
835 spin_unlock_bh(&p0->fcoe_rx_list.lock);
839 * This scenario occurs when the module is being removed
840 * and all threads are being destroyed. skbs will continue
841 * to be shifted from the CPU thread that is being removed
842 * to the CPU thread associated with the CPU that is processing
843 * the module removal. Once there is only one CPU Rx thread it
844 * will reach this case and we will drop all skbs and later
847 spin_lock_bh(&p->fcoe_rx_list.lock);
848 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
850 spin_unlock_bh(&p->fcoe_rx_list.lock);
854 * This a non-SMP scenario where the singular Rx thread is
855 * being removed. Free all skbs and stop the thread.
857 spin_lock_bh(&p->fcoe_rx_list.lock);
858 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
860 spin_unlock_bh(&p->fcoe_rx_list.lock);
864 kthread_stop(thread);
871 * fcoe_cpu_callback() - fcoe cpu hotplug event callback
872 * @nfb: callback data block
873 * @action: event triggering the callback
874 * @hcpu: index for the cpu of this event
876 * This creates or destroys per cpu data for fcoe
878 * Returns NOTIFY_OK always.
880 static int fcoe_cpu_callback(struct notifier_block *nfb,
881 unsigned long action, void *hcpu)
883 unsigned cpu = (unsigned long)hcpu;
887 case CPU_ONLINE_FROZEN:
888 FCOE_DBG("CPU %x online: Create Rx thread\n", cpu);
889 fcoe_percpu_thread_create(cpu);
892 case CPU_DEAD_FROZEN:
893 FCOE_DBG("CPU %x offline: Remove Rx thread\n", cpu);
894 fcoe_percpu_thread_destroy(cpu);
902 static struct notifier_block fcoe_cpu_notifier = {
903 .notifier_call = fcoe_cpu_callback,
907 * fcoe_rcv() - this is the fcoe receive function called by NET_RX_SOFTIRQ
908 * @skb: the receive skb
909 * @dev: associated net device
911 * @olddev: last device
913 * this function will receive the packet and build fc frame and pass it up
915 * Returns: 0 for success
917 int fcoe_rcv(struct sk_buff *skb, struct net_device *dev,
918 struct packet_type *ptype, struct net_device *olddev)
921 struct fcoe_rcv_info *fr;
922 struct fcoe_interface *fcoe;
923 struct fc_frame_header *fh;
924 struct fcoe_percpu_s *fps;
927 fcoe = container_of(ptype, struct fcoe_interface, fcoe_packet_type);
929 if (unlikely(lp == NULL)) {
930 FCOE_NETDEV_DBG(dev, "Cannot find hba structure");
936 FCOE_NETDEV_DBG(dev, "skb_info: len:%d data_len:%d head:%p "
937 "data:%p tail:%p end:%p sum:%d dev:%s",
938 skb->len, skb->data_len, skb->head, skb->data,
939 skb_tail_pointer(skb), skb_end_pointer(skb),
940 skb->csum, skb->dev ? skb->dev->name : "<NULL>");
942 /* check for FCOE packet type */
943 if (unlikely(eth_hdr(skb)->h_proto != htons(ETH_P_FCOE))) {
944 FCOE_NETDEV_DBG(dev, "Wrong FC type frame");
949 * Check for minimum frame length, and make sure required FCoE
950 * and FC headers are pulled into the linear data area.
952 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
953 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
956 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
957 fh = (struct fc_frame_header *) skb_transport_header(skb);
959 fr = fcoe_dev_from_skb(skb);
964 * In case the incoming frame's exchange is originated from
965 * the initiator, then received frame's exchange id is ANDed
966 * with fc_cpu_mask bits to get the same cpu on which exchange
967 * was originated, otherwise just use the current cpu.
969 if (ntoh24(fh->fh_f_ctl) & FC_FC_EX_CTX)
970 cpu = ntohs(fh->fh_ox_id) & fc_cpu_mask;
972 cpu = smp_processor_id();
974 fps = &per_cpu(fcoe_percpu, cpu);
975 spin_lock_bh(&fps->fcoe_rx_list.lock);
976 if (unlikely(!fps->thread)) {
978 * The targeted CPU is not ready, let's target
979 * the first CPU now. For non-SMP systems this
980 * will check the same CPU twice.
982 FCOE_NETDEV_DBG(dev, "CPU is online, but no receive thread "
983 "ready for incoming skb- using first online "
986 spin_unlock_bh(&fps->fcoe_rx_list.lock);
987 cpu = first_cpu(cpu_online_map);
988 fps = &per_cpu(fcoe_percpu, cpu);
989 spin_lock_bh(&fps->fcoe_rx_list.lock);
991 spin_unlock_bh(&fps->fcoe_rx_list.lock);
997 * We now have a valid CPU that we're targeting for
998 * this skb. We also have this receive thread locked,
999 * so we're free to queue skbs into it's queue.
1001 __skb_queue_tail(&fps->fcoe_rx_list, skb);
1002 if (fps->fcoe_rx_list.qlen == 1)
1003 wake_up_process(fps->thread);
1005 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1009 fc_lport_get_stats(lp)->ErrorFrames++;
1017 * fcoe_start_io() - pass to netdev to start xmit for fcoe
1018 * @skb: the skb to be xmitted
1020 * Returns: 0 for success
1022 static inline int fcoe_start_io(struct sk_buff *skb)
1027 rc = dev_queue_xmit(skb);
1035 * fcoe_get_paged_crc_eof() - in case we need to alloc a page for crc_eof
1036 * @skb: the skb to be xmitted
1039 * Returns: 0 for success
1041 static int fcoe_get_paged_crc_eof(struct sk_buff *skb, int tlen)
1043 struct fcoe_percpu_s *fps;
1046 fps = &get_cpu_var(fcoe_percpu);
1047 page = fps->crc_eof_page;
1049 page = alloc_page(GFP_ATOMIC);
1051 put_cpu_var(fcoe_percpu);
1054 fps->crc_eof_page = page;
1055 fps->crc_eof_offset = 0;
1059 skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags, page,
1060 fps->crc_eof_offset, tlen);
1062 skb->data_len += tlen;
1063 skb->truesize += tlen;
1064 fps->crc_eof_offset += sizeof(struct fcoe_crc_eof);
1066 if (fps->crc_eof_offset >= PAGE_SIZE) {
1067 fps->crc_eof_page = NULL;
1068 fps->crc_eof_offset = 0;
1071 put_cpu_var(fcoe_percpu);
1076 * fcoe_fc_crc() - calculates FC CRC in this fcoe skb
1077 * @fp: the fc_frame containing data to be checksummed
1079 * This uses crc32() to calculate the crc for port frame
1080 * Return : 32 bit crc
1082 u32 fcoe_fc_crc(struct fc_frame *fp)
1084 struct sk_buff *skb = fp_skb(fp);
1085 struct skb_frag_struct *frag;
1086 unsigned char *data;
1087 unsigned long off, len, clen;
1091 crc = crc32(~0, skb->data, skb_headlen(skb));
1093 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1094 frag = &skb_shinfo(skb)->frags[i];
1095 off = frag->page_offset;
1098 clen = min(len, PAGE_SIZE - (off & ~PAGE_MASK));
1099 data = kmap_atomic(frag->page + (off >> PAGE_SHIFT),
1100 KM_SKB_DATA_SOFTIRQ);
1101 crc = crc32(crc, data + (off & ~PAGE_MASK), clen);
1102 kunmap_atomic(data, KM_SKB_DATA_SOFTIRQ);
1111 * fcoe_xmit() - FCoE frame transmit function
1112 * @lp: the associated local fcoe
1113 * @fp: the fc_frame to be transmitted
1115 * Return : 0 for success
1117 int fcoe_xmit(struct fc_lport *lp, struct fc_frame *fp)
1122 struct fcoe_crc_eof *cp;
1123 struct sk_buff *skb;
1124 struct fcoe_dev_stats *stats;
1125 struct fc_frame_header *fh;
1126 unsigned int hlen; /* header length implies the version */
1127 unsigned int tlen; /* trailer length */
1128 unsigned int elen; /* eth header, may include vlan */
1129 struct fcoe_port *port = lport_priv(lp);
1130 struct fcoe_interface *fcoe = port->fcoe;
1132 struct fcoe_hdr *hp;
1134 WARN_ON((fr_len(fp) % sizeof(u32)) != 0);
1136 fh = fc_frame_header_get(fp);
1138 wlen = skb->len / FCOE_WORD_TO_BYTE;
1145 if (unlikely(fh->fh_r_ctl == FC_RCTL_ELS_REQ) &&
1146 fcoe_ctlr_els_send(&fcoe->ctlr, skb))
1152 elen = sizeof(struct ethhdr);
1153 hlen = sizeof(struct fcoe_hdr);
1154 tlen = sizeof(struct fcoe_crc_eof);
1155 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
1158 if (likely(lp->crc_offload)) {
1159 skb->ip_summed = CHECKSUM_PARTIAL;
1160 skb->csum_start = skb_headroom(skb);
1161 skb->csum_offset = skb->len;
1164 skb->ip_summed = CHECKSUM_NONE;
1165 crc = fcoe_fc_crc(fp);
1168 /* copy port crc and eof to the skb buff */
1169 if (skb_is_nonlinear(skb)) {
1171 if (fcoe_get_paged_crc_eof(skb, tlen)) {
1175 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
1176 cp = kmap_atomic(frag->page, KM_SKB_DATA_SOFTIRQ)
1177 + frag->page_offset;
1179 cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
1182 memset(cp, 0, sizeof(*cp));
1184 cp->fcoe_crc32 = cpu_to_le32(~crc);
1186 if (skb_is_nonlinear(skb)) {
1187 kunmap_atomic(cp, KM_SKB_DATA_SOFTIRQ);
1191 /* adjust skb network/transport offsets to match mac/fcoe/port */
1192 skb_push(skb, elen + hlen);
1193 skb_reset_mac_header(skb);
1194 skb_reset_network_header(skb);
1195 skb->mac_len = elen;
1196 skb->protocol = htons(ETH_P_FCOE);
1197 skb->dev = fcoe->netdev;
1199 /* fill up mac and fcoe headers */
1201 eh->h_proto = htons(ETH_P_FCOE);
1202 if (fcoe->ctlr.map_dest)
1203 fc_fcoe_set_mac(eh->h_dest, fh->fh_d_id);
1205 /* insert GW address */
1206 memcpy(eh->h_dest, fcoe->ctlr.dest_addr, ETH_ALEN);
1208 if (unlikely(fcoe->ctlr.flogi_oxid != FC_XID_UNKNOWN))
1209 memcpy(eh->h_source, fcoe->ctlr.ctl_src_addr, ETH_ALEN);
1211 memcpy(eh->h_source, fcoe->ctlr.data_src_addr, ETH_ALEN);
1213 hp = (struct fcoe_hdr *)(eh + 1);
1214 memset(hp, 0, sizeof(*hp));
1216 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
1219 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
1220 if (lp->seq_offload && fr_max_payload(fp)) {
1221 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
1222 skb_shinfo(skb)->gso_size = fr_max_payload(fp);
1224 skb_shinfo(skb)->gso_type = 0;
1225 skb_shinfo(skb)->gso_size = 0;
1227 /* update tx stats: regardless if LLD fails */
1228 stats = fc_lport_get_stats(lp);
1230 stats->TxWords += wlen;
1232 /* send down to lld */
1234 if (port->fcoe_pending_queue.qlen)
1235 fcoe_check_wait_queue(lp, skb);
1236 else if (fcoe_start_io(skb))
1237 fcoe_check_wait_queue(lp, skb);
1243 * fcoe_percpu_receive_thread() - recv thread per cpu
1244 * @arg: ptr to the fcoe per cpu struct
1246 * Return: 0 for success
1248 int fcoe_percpu_receive_thread(void *arg)
1250 struct fcoe_percpu_s *p = arg;
1252 struct fc_lport *lp;
1253 struct fcoe_rcv_info *fr;
1254 struct fcoe_dev_stats *stats;
1255 struct fc_frame_header *fh;
1256 struct sk_buff *skb;
1257 struct fcoe_crc_eof crc_eof;
1258 struct fc_frame *fp;
1260 struct fcoe_port *port;
1261 struct fcoe_hdr *hp;
1263 set_user_nice(current, -20);
1265 while (!kthread_should_stop()) {
1267 spin_lock_bh(&p->fcoe_rx_list.lock);
1268 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) == NULL) {
1269 set_current_state(TASK_INTERRUPTIBLE);
1270 spin_unlock_bh(&p->fcoe_rx_list.lock);
1272 set_current_state(TASK_RUNNING);
1273 if (kthread_should_stop())
1275 spin_lock_bh(&p->fcoe_rx_list.lock);
1277 spin_unlock_bh(&p->fcoe_rx_list.lock);
1278 fr = fcoe_dev_from_skb(skb);
1280 if (unlikely(lp == NULL)) {
1281 FCOE_NETDEV_DBG(skb->dev, "Invalid HBA Structure");
1286 FCOE_NETDEV_DBG(skb->dev, "skb_info: len:%d data_len:%d "
1287 "head:%p data:%p tail:%p end:%p sum:%d dev:%s",
1288 skb->len, skb->data_len,
1289 skb->head, skb->data, skb_tail_pointer(skb),
1290 skb_end_pointer(skb), skb->csum,
1291 skb->dev ? skb->dev->name : "<NULL>");
1294 * Save source MAC address before discarding header.
1296 port = lport_priv(lp);
1297 if (skb_is_nonlinear(skb))
1298 skb_linearize(skb); /* not ideal */
1299 mac = eth_hdr(skb)->h_source;
1302 * Frame length checks and setting up the header pointers
1303 * was done in fcoe_rcv already.
1305 hp = (struct fcoe_hdr *) skb_network_header(skb);
1306 fh = (struct fc_frame_header *) skb_transport_header(skb);
1308 stats = fc_lport_get_stats(lp);
1309 if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) {
1310 if (stats->ErrorFrames < 5)
1311 printk(KERN_WARNING "fcoe: FCoE version "
1312 "mismatch: The frame has "
1313 "version %x, but the "
1314 "initiator supports version "
1315 "%x\n", FC_FCOE_DECAPS_VER(hp),
1317 stats->ErrorFrames++;
1322 skb_pull(skb, sizeof(struct fcoe_hdr));
1323 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
1326 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
1328 fp = (struct fc_frame *)skb;
1331 fr_sof(fp) = hp->fcoe_sof;
1333 /* Copy out the CRC and EOF trailer for access */
1334 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof))) {
1338 fr_eof(fp) = crc_eof.fcoe_eof;
1339 fr_crc(fp) = crc_eof.fcoe_crc32;
1340 if (pskb_trim(skb, fr_len)) {
1346 * We only check CRC if no offload is available and if it is
1347 * it's solicited data, in which case, the FCP layer would
1348 * check it during the copy.
1350 if (lp->crc_offload && skb->ip_summed == CHECKSUM_UNNECESSARY)
1351 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1353 fr_flags(fp) |= FCPHF_CRC_UNCHECKED;
1355 fh = fc_frame_header_get(fp);
1356 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA &&
1357 fh->fh_type == FC_TYPE_FCP) {
1358 fc_exch_recv(lp, fp);
1361 if (fr_flags(fp) & FCPHF_CRC_UNCHECKED) {
1362 if (le32_to_cpu(fr_crc(fp)) !=
1363 ~crc32(~0, skb->data, fr_len)) {
1364 if (stats->InvalidCRCCount < 5)
1365 printk(KERN_WARNING "fcoe: dropping "
1366 "frame with CRC error\n");
1367 stats->InvalidCRCCount++;
1368 stats->ErrorFrames++;
1372 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1374 if (unlikely(port->fcoe->ctlr.flogi_oxid != FC_XID_UNKNOWN) &&
1375 fcoe_ctlr_recv_flogi(&port->fcoe->ctlr, fp, mac)) {
1379 fc_exch_recv(lp, fp);
1385 * fcoe_check_wait_queue() - attempt to clear the transmit backlog
1388 * This empties the wait_queue, dequeue the head of the wait_queue queue
1389 * and calls fcoe_start_io() for each packet, if all skb have been
1390 * transmitted, return qlen or -1 if a error occurs, then restore
1391 * wait_queue and try again later.
1393 * The wait_queue is used when the skb transmit fails. skb will go
1394 * in the wait_queue which will be emptied by the timer function or
1395 * by the next skb transmit.
1397 static void fcoe_check_wait_queue(struct fc_lport *lp, struct sk_buff *skb)
1399 struct fcoe_port *port = lport_priv(lp);
1402 spin_lock_bh(&port->fcoe_pending_queue.lock);
1405 __skb_queue_tail(&port->fcoe_pending_queue, skb);
1407 if (port->fcoe_pending_queue_active)
1409 port->fcoe_pending_queue_active = 1;
1411 while (port->fcoe_pending_queue.qlen) {
1412 /* keep qlen > 0 until fcoe_start_io succeeds */
1413 port->fcoe_pending_queue.qlen++;
1414 skb = __skb_dequeue(&port->fcoe_pending_queue);
1416 spin_unlock_bh(&port->fcoe_pending_queue.lock);
1417 rc = fcoe_start_io(skb);
1418 spin_lock_bh(&port->fcoe_pending_queue.lock);
1421 __skb_queue_head(&port->fcoe_pending_queue, skb);
1422 /* undo temporary increment above */
1423 port->fcoe_pending_queue.qlen--;
1426 /* undo temporary increment above */
1427 port->fcoe_pending_queue.qlen--;
1430 if (port->fcoe_pending_queue.qlen < FCOE_LOW_QUEUE_DEPTH)
1432 if (port->fcoe_pending_queue.qlen && !timer_pending(&port->timer))
1433 mod_timer(&port->timer, jiffies + 2);
1434 port->fcoe_pending_queue_active = 0;
1436 if (port->fcoe_pending_queue.qlen > FCOE_MAX_QUEUE_DEPTH)
1438 spin_unlock_bh(&port->fcoe_pending_queue.lock);
1443 * fcoe_dev_setup() - setup link change notification interface
1445 static void fcoe_dev_setup(void)
1447 register_netdevice_notifier(&fcoe_notifier);
1451 * fcoe_dev_cleanup() - cleanup link change notification interface
1453 static void fcoe_dev_cleanup(void)
1455 unregister_netdevice_notifier(&fcoe_notifier);
1459 * fcoe_device_notification() - netdev event notification callback
1460 * @notifier: context of the notification
1461 * @event: type of event
1462 * @ptr: fixed array for output parsed ifname
1464 * This function is called by the ethernet driver in case of link change event
1466 * Returns: 0 for success
1468 static int fcoe_device_notification(struct notifier_block *notifier,
1469 ulong event, void *ptr)
1471 struct fc_lport *lp = NULL;
1472 struct net_device *netdev = ptr;
1473 struct fcoe_interface *fcoe;
1474 struct fcoe_dev_stats *stats;
1475 u32 link_possible = 1;
1479 read_lock(&fcoe_hostlist_lock);
1480 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
1481 if (fcoe->netdev == netdev) {
1486 read_unlock(&fcoe_hostlist_lock);
1494 case NETDEV_GOING_DOWN:
1500 case NETDEV_CHANGEMTU:
1501 mfs = netdev->mtu - (sizeof(struct fcoe_hdr) +
1502 sizeof(struct fcoe_crc_eof));
1503 if (mfs >= FC_MIN_MAX_FRAME)
1504 fc_set_mfs(lp, mfs);
1506 case NETDEV_REGISTER:
1509 FCOE_NETDEV_DBG(netdev, "Unknown event %ld "
1510 "from netdev netlink\n", event);
1512 if (link_possible && !fcoe_link_ok(lp))
1513 fcoe_ctlr_link_up(&fcoe->ctlr);
1514 else if (fcoe_ctlr_link_down(&fcoe->ctlr)) {
1515 stats = fc_lport_get_stats(lp);
1516 stats->LinkFailureCount++;
1517 fcoe_clean_pending_queue(lp);
1524 * fcoe_if_to_netdev() - parse a name buffer to get netdev
1525 * @buffer: incoming buffer to be copied
1527 * Returns: NULL or ptr to net_device
1529 static struct net_device *fcoe_if_to_netdev(const char *buffer)
1532 char ifname[IFNAMSIZ + 2];
1535 strlcpy(ifname, buffer, IFNAMSIZ);
1536 cp = ifname + strlen(ifname);
1537 while (--cp >= ifname && *cp == '\n')
1539 return dev_get_by_name(&init_net, ifname);
1545 * fcoe_netdev_to_module_owner() - finds out the driver module of the netdev
1546 * @netdev: the target netdev
1548 * Returns: ptr to the struct module, NULL for failure
1550 static struct module *
1551 fcoe_netdev_to_module_owner(const struct net_device *netdev)
1558 dev = netdev->dev.parent;
1565 return dev->driver->owner;
1569 * fcoe_ethdrv_get() - Hold the Ethernet driver
1570 * @netdev: the target netdev
1572 * Holds the Ethernet driver module by try_module_get() for
1573 * the corresponding netdev.
1575 * Returns: 0 for success
1577 static int fcoe_ethdrv_get(const struct net_device *netdev)
1579 struct module *owner;
1581 owner = fcoe_netdev_to_module_owner(netdev);
1583 FCOE_NETDEV_DBG(netdev, "Hold driver module %s\n",
1584 module_name(owner));
1585 return try_module_get(owner);
1591 * fcoe_ethdrv_put() - Release the Ethernet driver
1592 * @netdev: the target netdev
1594 * Releases the Ethernet driver module by module_put for
1595 * the corresponding netdev.
1597 * Returns: 0 for success
1599 static int fcoe_ethdrv_put(const struct net_device *netdev)
1601 struct module *owner;
1603 owner = fcoe_netdev_to_module_owner(netdev);
1605 FCOE_NETDEV_DBG(netdev, "Release driver module %s\n",
1606 module_name(owner));
1614 * fcoe_destroy() - handles the destroy from sysfs
1615 * @buffer: expected to be an eth if name
1616 * @kp: associated kernel param
1618 * Returns: 0 for success
1620 static int fcoe_destroy(const char *buffer, struct kernel_param *kp)
1622 struct net_device *netdev;
1623 struct fc_lport *lport;
1626 netdev = fcoe_if_to_netdev(buffer);
1631 /* look for existing lport */
1632 lport = fcoe_hostlist_lookup(netdev);
1637 fcoe_if_destroy(lport);
1638 fcoe_ethdrv_put(netdev);
1647 * fcoe_create() - Handles the create call from sysfs
1648 * @buffer: expected to be an eth if name
1649 * @kp: associated kernel param
1651 * Returns: 0 for success
1653 static int fcoe_create(const char *buffer, struct kernel_param *kp)
1656 struct fc_lport *lport;
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 lport = fcoe_if_create(netdev, &netdev->dev);
1672 if (IS_ERR(lport)) {
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 fcoe 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 fcoe");
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_port *port = lport_priv(lp);
1710 struct net_device *dev = port->fcoe->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_port *port = lport_priv(lp);
1773 struct sk_buff *skb;
1775 spin_lock_bh(&port->fcoe_pending_queue.lock);
1776 while ((skb = __skb_dequeue(&port->fcoe_pending_queue)) != NULL) {
1777 spin_unlock_bh(&port->fcoe_pending_queue.lock);
1779 spin_lock_bh(&port->fcoe_pending_queue.lock);
1781 spin_unlock_bh(&port->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_port() - 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_port
1805 static struct fcoe_interface *
1806 fcoe_hostlist_lookup_port(const struct net_device *dev)
1808 struct fcoe_interface *fcoe;
1810 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
1811 if (fcoe->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_interface *fcoe;
1827 read_lock(&fcoe_hostlist_lock);
1828 fcoe = fcoe_hostlist_lookup_port(netdev);
1829 read_unlock(&fcoe_hostlist_lock);
1831 return (fcoe) ? fcoe->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 *lport)
1844 struct fcoe_interface *fcoe;
1845 struct fcoe_port *port;
1847 fcoe = fcoe_hostlist_lookup_port(fcoe_netdev(lport));
1849 port = lport_priv(lport);
1851 list_add_tail(&fcoe->list, &fcoe_hostlist);
1857 * fcoe_hostlist_remove() - remove a lport from lports list
1858 * @lp: ptr to the fc_lport to be removed
1860 * Returns: 0 for success
1862 int fcoe_hostlist_remove(const struct fc_lport *lport)
1864 struct fcoe_interface *fcoe;
1866 write_lock_bh(&fcoe_hostlist_lock);
1867 fcoe = fcoe_hostlist_lookup_port(fcoe_netdev(lport));
1869 list_del(&fcoe->list);
1870 write_unlock_bh(&fcoe_hostlist_lock);
1876 * fcoe_init() - fcoe module loading initialization
1878 * Returns 0 on success, negative on failure
1880 static int __init fcoe_init(void)
1884 struct fcoe_percpu_s *p;
1886 for_each_possible_cpu(cpu) {
1887 p = &per_cpu(fcoe_percpu, cpu);
1888 skb_queue_head_init(&p->fcoe_rx_list);
1891 for_each_online_cpu(cpu)
1892 fcoe_percpu_thread_create(cpu);
1894 /* Initialize per CPU interrupt thread */
1895 rc = register_hotcpu_notifier(&fcoe_cpu_notifier);
1899 /* Setup link change notification */
1902 rc = fcoe_if_init();
1909 for_each_online_cpu(cpu) {
1910 fcoe_percpu_thread_destroy(cpu);
1915 module_init(fcoe_init);
1918 * fcoe_exit() - fcoe module unloading cleanup
1920 * Returns 0 on success, negative on failure
1922 static void __exit fcoe_exit(void)
1925 struct fcoe_interface *fcoe, *tmp;
1929 /* releases the associated fcoe hosts */
1930 list_for_each_entry_safe(fcoe, tmp, &fcoe_hostlist, list)
1931 fcoe_if_destroy(fcoe->ctlr.lp);
1933 unregister_hotcpu_notifier(&fcoe_cpu_notifier);
1935 for_each_online_cpu(cpu)
1936 fcoe_percpu_thread_destroy(cpu);
1938 /* detach from scsi transport */
1941 module_exit(fcoe_exit);