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;
155 struct fcoe_port *port;
157 fcoe = container_of(ptype, struct fcoe_interface, fip_packet_type);
159 fcoe_ctlr_recv(&port->ctlr, skb);
164 * fcoe_fip_send() - send an Ethernet-encapsulated FIP frame.
165 * @fip: FCoE controller.
168 static void fcoe_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
170 skb->dev = fcoe_from_ctlr(fip)->fcoe->netdev;
175 * fcoe_update_src_mac() - Update Ethernet MAC filters.
176 * @fip: FCoE controller.
177 * @old: Unicast MAC address to delete if the MAC is non-zero.
178 * @new: Unicast MAC address to add.
180 * Remove any previously-set unicast MAC filter.
181 * Add secondary FCoE MAC address filter for our OUI.
183 static void fcoe_update_src_mac(struct fcoe_ctlr *fip, u8 *old, u8 *new)
185 struct fcoe_interface *fcoe;
186 struct fcoe_port *port;
188 port = fcoe_from_ctlr(fip);
192 if (!is_zero_ether_addr(old))
193 dev_unicast_delete(fcoe->netdev, old);
194 dev_unicast_add(fcoe->netdev, new);
199 * fcoe_lport_config() - sets up the fc_lport
200 * @lp: ptr to the fc_lport
202 * Returns: 0 for success
204 static int fcoe_lport_config(struct fc_lport *lp)
208 lp->max_retry_count = 3;
209 lp->max_rport_retry_count = 3;
210 lp->e_d_tov = 2 * 1000; /* FC-FS default */
211 lp->r_a_tov = 2 * 2 * 1000;
212 lp->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
213 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
215 fc_lport_init_stats(lp);
217 /* lport fc_lport related configuration */
220 /* offload related configuration */
231 * fcoe_netdev_cleanup() - clean up netdev configurations
232 * @port: ptr to the fcoe_port
234 void fcoe_netdev_cleanup(struct fcoe_port *port)
236 u8 flogi_maddr[ETH_ALEN];
237 struct fcoe_interface *fcoe = port->fcoe;
239 /* Don't listen for Ethernet packets anymore */
240 dev_remove_pack(&fcoe->fcoe_packet_type);
241 dev_remove_pack(&fcoe->fip_packet_type);
243 /* Delete secondary MAC addresses */
245 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
246 dev_unicast_delete(fcoe->netdev, flogi_maddr);
247 if (!is_zero_ether_addr(port->ctlr.data_src_addr))
248 dev_unicast_delete(fcoe->netdev, port->ctlr.data_src_addr);
250 dev_unicast_delete(fcoe->netdev, port->ctlr.ctl_src_addr);
251 dev_mc_delete(fcoe->netdev, FIP_ALL_ENODE_MACS, ETH_ALEN, 0);
256 * fcoe_queue_timer() - fcoe queue timer
257 * @lp: the fc_lport pointer
259 * Calls fcoe_check_wait_queue on timeout
262 static void fcoe_queue_timer(ulong lp)
264 fcoe_check_wait_queue((struct fc_lport *)lp, NULL);
268 * fcoe_netdev_config() - Set up netdev for SW FCoE
269 * @lp : ptr to the fc_lport
270 * @netdev : ptr to the associated netdevice struct
272 * Must be called after fcoe_lport_config() as it will use lport mutex
274 * Returns : 0 for success
276 static int fcoe_netdev_config(struct fc_lport *lp, struct net_device *netdev)
280 struct fcoe_interface *fcoe;
281 struct fcoe_port *port;
282 u8 flogi_maddr[ETH_ALEN];
283 struct netdev_hw_addr *ha;
285 /* Setup lport private data to point to fcoe softc */
286 port = lport_priv(lp);
289 fcoe->netdev = netdev;
291 /* Do not support for bonding device */
292 if ((netdev->priv_flags & IFF_MASTER_ALB) ||
293 (netdev->priv_flags & IFF_SLAVE_INACTIVE) ||
294 (netdev->priv_flags & IFF_MASTER_8023AD)) {
299 * Determine max frame size based on underlying device and optional
300 * user-configured limit. If the MFS is too low, fcoe_link_ok()
301 * will return 0, so do this first.
303 mfs = netdev->mtu - (sizeof(struct fcoe_hdr) +
304 sizeof(struct fcoe_crc_eof));
305 if (fc_set_mfs(lp, mfs))
308 /* offload features support */
309 if (netdev->features & NETIF_F_SG)
312 if (netdev->features & NETIF_F_FCOE_CRC) {
314 FCOE_NETDEV_DBG(netdev, "Supports FCCRC offload\n");
316 if (netdev->features & NETIF_F_FSO) {
318 lp->lso_max = netdev->gso_max_size;
319 FCOE_NETDEV_DBG(netdev, "Supports LSO for max len 0x%x\n",
322 if (netdev->fcoe_ddp_xid) {
324 lp->lro_xid = netdev->fcoe_ddp_xid;
325 FCOE_NETDEV_DBG(netdev, "Supports LRO for max xid 0x%x\n",
328 skb_queue_head_init(&port->fcoe_pending_queue);
329 port->fcoe_pending_queue_active = 0;
330 setup_timer(&port->timer, fcoe_queue_timer, (unsigned long)lp);
332 /* look for SAN MAC address, if multiple SAN MACs exist, only
333 * use the first one for SPMA */
335 for_each_dev_addr(netdev, ha) {
336 if ((ha->type == NETDEV_HW_ADDR_T_SAN) &&
337 (is_valid_ether_addr(port->ctlr.ctl_src_addr))) {
338 memcpy(port->ctlr.ctl_src_addr, ha->addr, ETH_ALEN);
345 /* setup Source Mac Address */
346 if (!port->ctlr.spma)
347 memcpy(port->ctlr.ctl_src_addr, netdev->dev_addr,
350 wwnn = fcoe_wwn_from_mac(netdev->dev_addr, 1, 0);
351 fc_set_wwnn(lp, wwnn);
352 /* XXX - 3rd arg needs to be vlan id */
353 wwpn = fcoe_wwn_from_mac(netdev->dev_addr, 2, 0);
354 fc_set_wwpn(lp, wwpn);
357 * Add FCoE MAC address as second unicast MAC address
358 * or enter promiscuous mode if not capable of listening
359 * for multiple unicast MACs.
362 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
363 dev_unicast_add(netdev, flogi_maddr);
365 dev_unicast_add(netdev, port->ctlr.ctl_src_addr);
366 dev_mc_add(netdev, FIP_ALL_ENODE_MACS, ETH_ALEN, 0);
370 * setup the receive function from ethernet driver
371 * on the ethertype for the given device
373 fcoe->fcoe_packet_type.func = fcoe_rcv;
374 fcoe->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
375 fcoe->fcoe_packet_type.dev = netdev;
376 dev_add_pack(&fcoe->fcoe_packet_type);
378 fcoe->fip_packet_type.func = fcoe_fip_recv;
379 fcoe->fip_packet_type.type = htons(ETH_P_FIP);
380 fcoe->fip_packet_type.dev = netdev;
381 dev_add_pack(&fcoe->fip_packet_type);
387 * fcoe_shost_config() - Sets up fc_lport->host
388 * @lp : ptr to the fc_lport
389 * @shost : ptr to the associated scsi host
390 * @dev : device associated to scsi host
392 * Must be called after fcoe_lport_config() and fcoe_netdev_config()
394 * Returns : 0 for success
396 static int fcoe_shost_config(struct fc_lport *lp, struct Scsi_Host *shost,
401 /* lport scsi host config */
404 lp->host->max_lun = FCOE_MAX_LUN;
405 lp->host->max_id = FCOE_MAX_FCP_TARGET;
406 lp->host->max_channel = 0;
407 lp->host->transportt = scsi_transport_fcoe_sw;
409 /* add the new host to the SCSI-ml */
410 rc = scsi_add_host(lp->host, dev);
412 FCOE_NETDEV_DBG(fcoe_netdev(lp), "fcoe_shost_config: "
413 "error on scsi_add_host\n");
416 sprintf(fc_host_symbolic_name(lp->host), "%s v%s over %s",
417 FCOE_NAME, FCOE_VERSION,
418 fcoe_netdev(lp)->name);
424 * fcoe_oem_match() - match for read types IO
425 * @fp: the fc_frame for new IO.
427 * Returns : true for read types IO, otherwise returns false.
429 bool fcoe_oem_match(struct fc_frame *fp)
431 return fc_fcp_is_read(fr_fsp(fp)) &&
432 (fr_fsp(fp)->data_len > fcoe_ddp_min);
436 * fcoe_em_config() - allocates em for this lport
437 * @lp: the fcoe that em is to allocated for
439 * Called with write fcoe_hostlist_lock held.
441 * Returns : 0 on success
443 static inline int fcoe_em_config(struct fc_lport *lp)
445 struct fcoe_port *port = lport_priv(lp);
446 struct fcoe_port *oldport = NULL;
447 struct fcoe_interface *fcoe = port->fcoe;
448 struct fcoe_interface *oldfcoe = NULL;
449 struct net_device *old_real_dev, *cur_real_dev;
450 u16 min_xid = FCOE_MIN_XID;
451 u16 max_xid = FCOE_MAX_XID;
454 * Check if need to allocate an em instance for
455 * offload exchange ids to be shared across all VN_PORTs/lport.
457 if (!lp->lro_enabled || !lp->lro_xid || (lp->lro_xid >= max_xid)) {
463 * Reuse existing offload em instance in case
464 * it is already allocated on real eth device
466 if (fcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
467 cur_real_dev = vlan_dev_real_dev(fcoe->netdev);
469 cur_real_dev = fcoe->netdev;
471 list_for_each_entry(oldfcoe, &fcoe_hostlist, list) {
472 oldport = oldfcoe->priv;
473 if (oldfcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
474 old_real_dev = vlan_dev_real_dev(oldfcoe->netdev);
476 old_real_dev = oldfcoe->netdev;
478 if (cur_real_dev == old_real_dev) {
479 port->oem = oldport->oem;
485 if (!fc_exch_mgr_add(lp, port->oem, fcoe_oem_match)) {
486 printk(KERN_ERR "fcoe_em_config: failed to add "
487 "offload em:%p on interface:%s\n",
488 port->oem, fcoe->netdev->name);
492 port->oem = fc_exch_mgr_alloc(lp, FC_CLASS_3,
493 FCOE_MIN_XID, lp->lro_xid,
496 printk(KERN_ERR "fcoe_em_config: failed to allocate "
497 "em for offload exches on interface:%s\n",
504 * Exclude offload EM xid range from next EM xid range.
506 min_xid += lp->lro_xid + 1;
509 if (!fc_exch_mgr_alloc(lp, FC_CLASS_3, min_xid, max_xid, NULL)) {
510 printk(KERN_ERR "fcoe_em_config: failed to "
511 "allocate em on interface %s\n", fcoe->netdev->name);
519 * fcoe_if_destroy() - FCoE software HBA tear-down function
520 * @lport: fc_lport to destroy
522 static void fcoe_if_destroy(struct fc_lport *lport)
524 struct fcoe_port *port = lport_priv(lport);
525 struct fcoe_interface *fcoe = port->fcoe;
526 struct net_device *netdev = fcoe->netdev;
528 FCOE_NETDEV_DBG(netdev, "Destroying interface\n");
530 /* Logout of the fabric */
531 fc_fabric_logoff(lport);
533 /* Remove the instance from fcoe's list */
534 fcoe_hostlist_remove(lport);
536 /* clean up netdev configurations */
537 fcoe_netdev_cleanup(port);
539 /* tear-down the FCoE controller */
540 fcoe_ctlr_destroy(&port->ctlr);
542 /* Free queued packets for the per-CPU receive threads */
543 fcoe_percpu_clean(lport);
545 /* Cleanup the fc_lport */
546 fc_lport_destroy(lport);
547 fc_fcp_destroy(lport);
549 /* Detach from the scsi-ml */
550 fc_remove_host(lport->host);
551 scsi_remove_host(lport->host);
553 /* There are no more rports or I/O, free the EM */
554 fc_exch_mgr_free(lport);
556 /* Free existing skbs */
557 fcoe_clean_pending_queue(lport);
560 del_timer_sync(&port->timer);
562 /* Free memory used by statistical counters */
563 fc_lport_free_stats(lport);
565 /* Release the net_device and Scsi_Host */
567 scsi_host_put(lport->host);
568 kfree(fcoe); /* TODO, should be refcounted */
572 * fcoe_ddp_setup - calls LLD's ddp_setup through net_device
573 * @lp: the corresponding fc_lport
574 * @xid: the exchange id for this ddp transfer
575 * @sgl: the scatterlist describing this transfer
576 * @sgc: number of sg items
580 static int fcoe_ddp_setup(struct fc_lport *lp, u16 xid,
581 struct scatterlist *sgl, unsigned int sgc)
583 struct net_device *n = fcoe_netdev(lp);
585 if (n->netdev_ops && n->netdev_ops->ndo_fcoe_ddp_setup)
586 return n->netdev_ops->ndo_fcoe_ddp_setup(n, xid, sgl, sgc);
592 * fcoe_ddp_done - calls LLD's ddp_done through net_device
593 * @lp: the corresponding fc_lport
594 * @xid: the exchange id for this ddp transfer
596 * Returns : the length of data that have been completed by ddp
598 static int fcoe_ddp_done(struct fc_lport *lp, u16 xid)
600 struct net_device *n = fcoe_netdev(lp);
602 if (n->netdev_ops && n->netdev_ops->ndo_fcoe_ddp_done)
603 return n->netdev_ops->ndo_fcoe_ddp_done(n, xid);
607 static struct libfc_function_template fcoe_libfc_fcn_templ = {
608 .frame_send = fcoe_xmit,
609 .ddp_setup = fcoe_ddp_setup,
610 .ddp_done = fcoe_ddp_done,
614 * fcoe_if_create() - this function creates the fcoe interface
615 * @netdev: pointer the associated netdevice
616 * @parent: device pointer to be the parent in sysfs for the SCSI host
618 * Creates fc_lport struct and scsi_host for lport, configures lport
619 * and starts fabric login.
621 * Returns : The allocated fc_lport or an error pointer
623 static struct fc_lport *fcoe_if_create(struct net_device *netdev,
624 struct device *parent)
627 struct fc_lport *lport = NULL;
628 struct fcoe_port *port;
629 struct fcoe_interface *fcoe;
630 struct Scsi_Host *shost;
632 FCOE_NETDEV_DBG(netdev, "Create Interface\n");
634 fcoe = kzalloc(sizeof(*fcoe), GFP_KERNEL);
636 FCOE_NETDEV_DBG(netdev, "Could not allocate fcoe structure\n");
641 shost = libfc_host_alloc(&fcoe_shost_template,
642 sizeof(struct fcoe_port));
644 FCOE_NETDEV_DBG(netdev, "Could not allocate host structure\n");
648 lport = shost_priv(shost);
649 port = lport_priv(lport);
654 /* configure fc_lport, e.g., em */
655 rc = fcoe_lport_config(lport);
657 FCOE_NETDEV_DBG(netdev, "Could not configure lport for the "
665 fcoe_ctlr_init(&port->ctlr);
666 port->ctlr.send = fcoe_fip_send;
667 port->ctlr.update_mac = fcoe_update_src_mac;
669 /* configure lport network properties */
670 rc = fcoe_netdev_config(lport, netdev);
672 FCOE_NETDEV_DBG(netdev, "Could not configure netdev for the "
674 goto out_netdev_cleanup;
677 /* configure lport scsi host properties */
678 rc = fcoe_shost_config(lport, shost, parent);
680 FCOE_NETDEV_DBG(netdev, "Could not configure shost for the "
682 goto out_netdev_cleanup;
685 /* Initialize the library */
686 rc = fcoe_libfc_config(lport, &fcoe_libfc_fcn_templ);
688 FCOE_NETDEV_DBG(netdev, "Could not configure libfc for the "
694 * fcoe_em_alloc() and fcoe_hostlist_add() both
695 * need to be atomic under fcoe_hostlist_lock
696 * since fcoe_em_alloc() looks for an existing EM
697 * instance on host list updated by fcoe_hostlist_add().
699 write_lock(&fcoe_hostlist_lock);
700 /* lport exch manager allocation */
701 rc = fcoe_em_config(lport);
703 FCOE_NETDEV_DBG(netdev, "Could not configure the EM for the "
708 /* add to lports list */
709 fcoe_hostlist_add(lport);
710 write_unlock(&fcoe_hostlist_lock);
712 lport->boot_time = jiffies;
714 fc_fabric_login(lport);
716 if (!fcoe_link_ok(lport))
717 fcoe_ctlr_link_up(&port->ctlr);
724 fc_exch_mgr_free(lport);
726 fcoe_netdev_cleanup(port);
728 scsi_host_put(lport->host);
736 * fcoe_if_init() - attach to scsi transport
738 * Returns : 0 on success
740 static int __init fcoe_if_init(void)
742 /* attach to scsi transport */
743 scsi_transport_fcoe_sw =
744 fc_attach_transport(&fcoe_transport_function);
746 if (!scsi_transport_fcoe_sw) {
747 printk(KERN_ERR "fcoe: Failed to attach to the FC transport\n");
755 * fcoe_if_exit() - detach from scsi transport
757 * Returns : 0 on success
759 int __exit fcoe_if_exit(void)
761 fc_release_transport(scsi_transport_fcoe_sw);
766 * fcoe_percpu_thread_create() - Create a receive thread for an online cpu
767 * @cpu: cpu index for the online cpu
769 static void fcoe_percpu_thread_create(unsigned int cpu)
771 struct fcoe_percpu_s *p;
772 struct task_struct *thread;
774 p = &per_cpu(fcoe_percpu, cpu);
776 thread = kthread_create(fcoe_percpu_receive_thread,
777 (void *)p, "fcoethread/%d", cpu);
779 if (likely(!IS_ERR(p->thread))) {
780 kthread_bind(thread, cpu);
781 wake_up_process(thread);
783 spin_lock_bh(&p->fcoe_rx_list.lock);
785 spin_unlock_bh(&p->fcoe_rx_list.lock);
790 * fcoe_percpu_thread_destroy() - removes the rx thread for the given cpu
791 * @cpu: cpu index the rx thread is to be removed
793 * Destroys a per-CPU Rx thread. Any pending skbs are moved to the
794 * current CPU's Rx thread. If the thread being destroyed is bound to
795 * the CPU processing this context the skbs will be freed.
797 static void fcoe_percpu_thread_destroy(unsigned int cpu)
799 struct fcoe_percpu_s *p;
800 struct task_struct *thread;
801 struct page *crc_eof;
804 struct fcoe_percpu_s *p0;
805 unsigned targ_cpu = smp_processor_id();
806 #endif /* CONFIG_SMP */
808 FCOE_DBG("Destroying receive thread for CPU %d\n", cpu);
810 /* Prevent any new skbs from being queued for this CPU. */
811 p = &per_cpu(fcoe_percpu, cpu);
812 spin_lock_bh(&p->fcoe_rx_list.lock);
815 crc_eof = p->crc_eof_page;
816 p->crc_eof_page = NULL;
817 p->crc_eof_offset = 0;
818 spin_unlock_bh(&p->fcoe_rx_list.lock);
822 * Don't bother moving the skb's if this context is running
823 * on the same CPU that is having its thread destroyed. This
824 * can easily happen when the module is removed.
826 if (cpu != targ_cpu) {
827 p0 = &per_cpu(fcoe_percpu, targ_cpu);
828 spin_lock_bh(&p0->fcoe_rx_list.lock);
830 FCOE_DBG("Moving frames from CPU %d to CPU %d\n",
833 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
834 __skb_queue_tail(&p0->fcoe_rx_list, skb);
835 spin_unlock_bh(&p0->fcoe_rx_list.lock);
838 * The targeted CPU is not initialized and cannot accept
839 * new skbs. Unlock the targeted CPU and drop the skbs
840 * on the CPU that is going offline.
842 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
844 spin_unlock_bh(&p0->fcoe_rx_list.lock);
848 * This scenario occurs when the module is being removed
849 * and all threads are being destroyed. skbs will continue
850 * to be shifted from the CPU thread that is being removed
851 * to the CPU thread associated with the CPU that is processing
852 * the module removal. Once there is only one CPU Rx thread it
853 * will reach this case and we will drop all skbs and later
856 spin_lock_bh(&p->fcoe_rx_list.lock);
857 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
859 spin_unlock_bh(&p->fcoe_rx_list.lock);
863 * This a non-SMP scenario where the singular Rx thread is
864 * being removed. Free all skbs and stop the thread.
866 spin_lock_bh(&p->fcoe_rx_list.lock);
867 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
869 spin_unlock_bh(&p->fcoe_rx_list.lock);
873 kthread_stop(thread);
880 * fcoe_cpu_callback() - fcoe cpu hotplug event callback
881 * @nfb: callback data block
882 * @action: event triggering the callback
883 * @hcpu: index for the cpu of this event
885 * This creates or destroys per cpu data for fcoe
887 * Returns NOTIFY_OK always.
889 static int fcoe_cpu_callback(struct notifier_block *nfb,
890 unsigned long action, void *hcpu)
892 unsigned cpu = (unsigned long)hcpu;
896 case CPU_ONLINE_FROZEN:
897 FCOE_DBG("CPU %x online: Create Rx thread\n", cpu);
898 fcoe_percpu_thread_create(cpu);
901 case CPU_DEAD_FROZEN:
902 FCOE_DBG("CPU %x offline: Remove Rx thread\n", cpu);
903 fcoe_percpu_thread_destroy(cpu);
911 static struct notifier_block fcoe_cpu_notifier = {
912 .notifier_call = fcoe_cpu_callback,
916 * fcoe_rcv() - this is the fcoe receive function called by NET_RX_SOFTIRQ
917 * @skb: the receive skb
918 * @dev: associated net device
920 * @olddev: last device
922 * this function will receive the packet and build fc frame and pass it up
924 * Returns: 0 for success
926 int fcoe_rcv(struct sk_buff *skb, struct net_device *dev,
927 struct packet_type *ptype, struct net_device *olddev)
930 struct fcoe_rcv_info *fr;
931 struct fcoe_interface *fcoe;
932 struct fcoe_port *port;
933 struct fc_frame_header *fh;
934 struct fcoe_percpu_s *fps;
937 fcoe = container_of(ptype, struct fcoe_interface, fcoe_packet_type);
940 if (unlikely(lp == NULL)) {
941 FCOE_NETDEV_DBG(dev, "Cannot find hba structure");
947 FCOE_NETDEV_DBG(dev, "skb_info: len:%d data_len:%d head:%p "
948 "data:%p tail:%p end:%p sum:%d dev:%s",
949 skb->len, skb->data_len, skb->head, skb->data,
950 skb_tail_pointer(skb), skb_end_pointer(skb),
951 skb->csum, skb->dev ? skb->dev->name : "<NULL>");
953 /* check for FCOE packet type */
954 if (unlikely(eth_hdr(skb)->h_proto != htons(ETH_P_FCOE))) {
955 FCOE_NETDEV_DBG(dev, "Wrong FC type frame");
960 * Check for minimum frame length, and make sure required FCoE
961 * and FC headers are pulled into the linear data area.
963 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
964 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
967 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
968 fh = (struct fc_frame_header *) skb_transport_header(skb);
970 fr = fcoe_dev_from_skb(skb);
975 * In case the incoming frame's exchange is originated from
976 * the initiator, then received frame's exchange id is ANDed
977 * with fc_cpu_mask bits to get the same cpu on which exchange
978 * was originated, otherwise just use the current cpu.
980 if (ntoh24(fh->fh_f_ctl) & FC_FC_EX_CTX)
981 cpu = ntohs(fh->fh_ox_id) & fc_cpu_mask;
983 cpu = smp_processor_id();
985 fps = &per_cpu(fcoe_percpu, cpu);
986 spin_lock_bh(&fps->fcoe_rx_list.lock);
987 if (unlikely(!fps->thread)) {
989 * The targeted CPU is not ready, let's target
990 * the first CPU now. For non-SMP systems this
991 * will check the same CPU twice.
993 FCOE_NETDEV_DBG(dev, "CPU is online, but no receive thread "
994 "ready for incoming skb- using first online "
997 spin_unlock_bh(&fps->fcoe_rx_list.lock);
998 cpu = first_cpu(cpu_online_map);
999 fps = &per_cpu(fcoe_percpu, cpu);
1000 spin_lock_bh(&fps->fcoe_rx_list.lock);
1002 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1008 * We now have a valid CPU that we're targeting for
1009 * this skb. We also have this receive thread locked,
1010 * so we're free to queue skbs into it's queue.
1012 __skb_queue_tail(&fps->fcoe_rx_list, skb);
1013 if (fps->fcoe_rx_list.qlen == 1)
1014 wake_up_process(fps->thread);
1016 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1020 fc_lport_get_stats(lp)->ErrorFrames++;
1028 * fcoe_start_io() - pass to netdev to start xmit for fcoe
1029 * @skb: the skb to be xmitted
1031 * Returns: 0 for success
1033 static inline int fcoe_start_io(struct sk_buff *skb)
1038 rc = dev_queue_xmit(skb);
1046 * fcoe_get_paged_crc_eof() - in case we need to alloc a page for crc_eof
1047 * @skb: the skb to be xmitted
1050 * Returns: 0 for success
1052 static int fcoe_get_paged_crc_eof(struct sk_buff *skb, int tlen)
1054 struct fcoe_percpu_s *fps;
1057 fps = &get_cpu_var(fcoe_percpu);
1058 page = fps->crc_eof_page;
1060 page = alloc_page(GFP_ATOMIC);
1062 put_cpu_var(fcoe_percpu);
1065 fps->crc_eof_page = page;
1066 fps->crc_eof_offset = 0;
1070 skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags, page,
1071 fps->crc_eof_offset, tlen);
1073 skb->data_len += tlen;
1074 skb->truesize += tlen;
1075 fps->crc_eof_offset += sizeof(struct fcoe_crc_eof);
1077 if (fps->crc_eof_offset >= PAGE_SIZE) {
1078 fps->crc_eof_page = NULL;
1079 fps->crc_eof_offset = 0;
1082 put_cpu_var(fcoe_percpu);
1087 * fcoe_fc_crc() - calculates FC CRC in this fcoe skb
1088 * @fp: the fc_frame containing data to be checksummed
1090 * This uses crc32() to calculate the crc for port frame
1091 * Return : 32 bit crc
1093 u32 fcoe_fc_crc(struct fc_frame *fp)
1095 struct sk_buff *skb = fp_skb(fp);
1096 struct skb_frag_struct *frag;
1097 unsigned char *data;
1098 unsigned long off, len, clen;
1102 crc = crc32(~0, skb->data, skb_headlen(skb));
1104 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1105 frag = &skb_shinfo(skb)->frags[i];
1106 off = frag->page_offset;
1109 clen = min(len, PAGE_SIZE - (off & ~PAGE_MASK));
1110 data = kmap_atomic(frag->page + (off >> PAGE_SHIFT),
1111 KM_SKB_DATA_SOFTIRQ);
1112 crc = crc32(crc, data + (off & ~PAGE_MASK), clen);
1113 kunmap_atomic(data, KM_SKB_DATA_SOFTIRQ);
1122 * fcoe_xmit() - FCoE frame transmit function
1123 * @lp: the associated local fcoe
1124 * @fp: the fc_frame to be transmitted
1126 * Return : 0 for success
1128 int fcoe_xmit(struct fc_lport *lp, struct fc_frame *fp)
1133 struct fcoe_crc_eof *cp;
1134 struct sk_buff *skb;
1135 struct fcoe_dev_stats *stats;
1136 struct fc_frame_header *fh;
1137 unsigned int hlen; /* header length implies the version */
1138 unsigned int tlen; /* trailer length */
1139 unsigned int elen; /* eth header, may include vlan */
1140 struct fcoe_port *port;
1142 struct fcoe_hdr *hp;
1144 WARN_ON((fr_len(fp) % sizeof(u32)) != 0);
1146 port = lport_priv(lp);
1147 fh = fc_frame_header_get(fp);
1149 wlen = skb->len / FCOE_WORD_TO_BYTE;
1156 if (unlikely(fh->fh_r_ctl == FC_RCTL_ELS_REQ) &&
1157 fcoe_ctlr_els_send(&port->ctlr, skb))
1163 elen = sizeof(struct ethhdr);
1164 hlen = sizeof(struct fcoe_hdr);
1165 tlen = sizeof(struct fcoe_crc_eof);
1166 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
1169 if (likely(lp->crc_offload)) {
1170 skb->ip_summed = CHECKSUM_PARTIAL;
1171 skb->csum_start = skb_headroom(skb);
1172 skb->csum_offset = skb->len;
1175 skb->ip_summed = CHECKSUM_NONE;
1176 crc = fcoe_fc_crc(fp);
1179 /* copy port crc and eof to the skb buff */
1180 if (skb_is_nonlinear(skb)) {
1182 if (fcoe_get_paged_crc_eof(skb, tlen)) {
1186 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
1187 cp = kmap_atomic(frag->page, KM_SKB_DATA_SOFTIRQ)
1188 + frag->page_offset;
1190 cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
1193 memset(cp, 0, sizeof(*cp));
1195 cp->fcoe_crc32 = cpu_to_le32(~crc);
1197 if (skb_is_nonlinear(skb)) {
1198 kunmap_atomic(cp, KM_SKB_DATA_SOFTIRQ);
1202 /* adjust skb network/transport offsets to match mac/fcoe/port */
1203 skb_push(skb, elen + hlen);
1204 skb_reset_mac_header(skb);
1205 skb_reset_network_header(skb);
1206 skb->mac_len = elen;
1207 skb->protocol = htons(ETH_P_FCOE);
1208 skb->dev = port->fcoe->netdev;
1210 /* fill up mac and fcoe headers */
1212 eh->h_proto = htons(ETH_P_FCOE);
1213 if (port->ctlr.map_dest)
1214 fc_fcoe_set_mac(eh->h_dest, fh->fh_d_id);
1216 /* insert GW address */
1217 memcpy(eh->h_dest, port->ctlr.dest_addr, ETH_ALEN);
1219 if (unlikely(port->ctlr.flogi_oxid != FC_XID_UNKNOWN))
1220 memcpy(eh->h_source, port->ctlr.ctl_src_addr, ETH_ALEN);
1222 memcpy(eh->h_source, port->ctlr.data_src_addr, ETH_ALEN);
1224 hp = (struct fcoe_hdr *)(eh + 1);
1225 memset(hp, 0, sizeof(*hp));
1227 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
1230 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
1231 if (lp->seq_offload && fr_max_payload(fp)) {
1232 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
1233 skb_shinfo(skb)->gso_size = fr_max_payload(fp);
1235 skb_shinfo(skb)->gso_type = 0;
1236 skb_shinfo(skb)->gso_size = 0;
1238 /* update tx stats: regardless if LLD fails */
1239 stats = fc_lport_get_stats(lp);
1241 stats->TxWords += wlen;
1243 /* send down to lld */
1245 if (port->fcoe_pending_queue.qlen)
1246 fcoe_check_wait_queue(lp, skb);
1247 else if (fcoe_start_io(skb))
1248 fcoe_check_wait_queue(lp, skb);
1254 * fcoe_percpu_receive_thread() - recv thread per cpu
1255 * @arg: ptr to the fcoe per cpu struct
1257 * Return: 0 for success
1259 int fcoe_percpu_receive_thread(void *arg)
1261 struct fcoe_percpu_s *p = arg;
1263 struct fc_lport *lp;
1264 struct fcoe_rcv_info *fr;
1265 struct fcoe_dev_stats *stats;
1266 struct fc_frame_header *fh;
1267 struct sk_buff *skb;
1268 struct fcoe_crc_eof crc_eof;
1269 struct fc_frame *fp;
1271 struct fcoe_port *port;
1272 struct fcoe_hdr *hp;
1274 set_user_nice(current, -20);
1276 while (!kthread_should_stop()) {
1278 spin_lock_bh(&p->fcoe_rx_list.lock);
1279 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) == NULL) {
1280 set_current_state(TASK_INTERRUPTIBLE);
1281 spin_unlock_bh(&p->fcoe_rx_list.lock);
1283 set_current_state(TASK_RUNNING);
1284 if (kthread_should_stop())
1286 spin_lock_bh(&p->fcoe_rx_list.lock);
1288 spin_unlock_bh(&p->fcoe_rx_list.lock);
1289 fr = fcoe_dev_from_skb(skb);
1291 if (unlikely(lp == NULL)) {
1292 FCOE_NETDEV_DBG(skb->dev, "Invalid HBA Structure");
1297 FCOE_NETDEV_DBG(skb->dev, "skb_info: len:%d data_len:%d "
1298 "head:%p data:%p tail:%p end:%p sum:%d dev:%s",
1299 skb->len, skb->data_len,
1300 skb->head, skb->data, skb_tail_pointer(skb),
1301 skb_end_pointer(skb), skb->csum,
1302 skb->dev ? skb->dev->name : "<NULL>");
1305 * Save source MAC address before discarding header.
1307 port = lport_priv(lp);
1308 if (skb_is_nonlinear(skb))
1309 skb_linearize(skb); /* not ideal */
1310 mac = eth_hdr(skb)->h_source;
1313 * Frame length checks and setting up the header pointers
1314 * was done in fcoe_rcv already.
1316 hp = (struct fcoe_hdr *) skb_network_header(skb);
1317 fh = (struct fc_frame_header *) skb_transport_header(skb);
1319 stats = fc_lport_get_stats(lp);
1320 if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) {
1321 if (stats->ErrorFrames < 5)
1322 printk(KERN_WARNING "fcoe: FCoE version "
1323 "mismatch: The frame has "
1324 "version %x, but the "
1325 "initiator supports version "
1326 "%x\n", FC_FCOE_DECAPS_VER(hp),
1328 stats->ErrorFrames++;
1333 skb_pull(skb, sizeof(struct fcoe_hdr));
1334 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
1337 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
1339 fp = (struct fc_frame *)skb;
1342 fr_sof(fp) = hp->fcoe_sof;
1344 /* Copy out the CRC and EOF trailer for access */
1345 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof))) {
1349 fr_eof(fp) = crc_eof.fcoe_eof;
1350 fr_crc(fp) = crc_eof.fcoe_crc32;
1351 if (pskb_trim(skb, fr_len)) {
1357 * We only check CRC if no offload is available and if it is
1358 * it's solicited data, in which case, the FCP layer would
1359 * check it during the copy.
1361 if (lp->crc_offload && skb->ip_summed == CHECKSUM_UNNECESSARY)
1362 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1364 fr_flags(fp) |= FCPHF_CRC_UNCHECKED;
1366 fh = fc_frame_header_get(fp);
1367 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA &&
1368 fh->fh_type == FC_TYPE_FCP) {
1369 fc_exch_recv(lp, fp);
1372 if (fr_flags(fp) & FCPHF_CRC_UNCHECKED) {
1373 if (le32_to_cpu(fr_crc(fp)) !=
1374 ~crc32(~0, skb->data, fr_len)) {
1375 if (stats->InvalidCRCCount < 5)
1376 printk(KERN_WARNING "fcoe: dropping "
1377 "frame with CRC error\n");
1378 stats->InvalidCRCCount++;
1379 stats->ErrorFrames++;
1383 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1385 if (unlikely(port->ctlr.flogi_oxid != FC_XID_UNKNOWN) &&
1386 fcoe_ctlr_recv_flogi(&port->ctlr, fp, mac)) {
1390 fc_exch_recv(lp, fp);
1396 * fcoe_check_wait_queue() - attempt to clear the transmit backlog
1399 * This empties the wait_queue, dequeue the head of the wait_queue queue
1400 * and calls fcoe_start_io() for each packet, if all skb have been
1401 * transmitted, return qlen or -1 if a error occurs, then restore
1402 * wait_queue and try again later.
1404 * The wait_queue is used when the skb transmit fails. skb will go
1405 * in the wait_queue which will be emptied by the timer function or
1406 * by the next skb transmit.
1408 static void fcoe_check_wait_queue(struct fc_lport *lp, struct sk_buff *skb)
1410 struct fcoe_port *port = lport_priv(lp);
1413 spin_lock_bh(&port->fcoe_pending_queue.lock);
1416 __skb_queue_tail(&port->fcoe_pending_queue, skb);
1418 if (port->fcoe_pending_queue_active)
1420 port->fcoe_pending_queue_active = 1;
1422 while (port->fcoe_pending_queue.qlen) {
1423 /* keep qlen > 0 until fcoe_start_io succeeds */
1424 port->fcoe_pending_queue.qlen++;
1425 skb = __skb_dequeue(&port->fcoe_pending_queue);
1427 spin_unlock_bh(&port->fcoe_pending_queue.lock);
1428 rc = fcoe_start_io(skb);
1429 spin_lock_bh(&port->fcoe_pending_queue.lock);
1432 __skb_queue_head(&port->fcoe_pending_queue, skb);
1433 /* undo temporary increment above */
1434 port->fcoe_pending_queue.qlen--;
1437 /* undo temporary increment above */
1438 port->fcoe_pending_queue.qlen--;
1441 if (port->fcoe_pending_queue.qlen < FCOE_LOW_QUEUE_DEPTH)
1443 if (port->fcoe_pending_queue.qlen && !timer_pending(&port->timer))
1444 mod_timer(&port->timer, jiffies + 2);
1445 port->fcoe_pending_queue_active = 0;
1447 if (port->fcoe_pending_queue.qlen > FCOE_MAX_QUEUE_DEPTH)
1449 spin_unlock_bh(&port->fcoe_pending_queue.lock);
1454 * fcoe_dev_setup() - setup link change notification interface
1456 static void fcoe_dev_setup(void)
1458 register_netdevice_notifier(&fcoe_notifier);
1462 * fcoe_dev_cleanup() - cleanup link change notification interface
1464 static void fcoe_dev_cleanup(void)
1466 unregister_netdevice_notifier(&fcoe_notifier);
1470 * fcoe_device_notification() - netdev event notification callback
1471 * @notifier: context of the notification
1472 * @event: type of event
1473 * @ptr: fixed array for output parsed ifname
1475 * This function is called by the ethernet driver in case of link change event
1477 * Returns: 0 for success
1479 static int fcoe_device_notification(struct notifier_block *notifier,
1480 ulong event, void *ptr)
1482 struct fc_lport *lp = NULL;
1483 struct net_device *netdev = ptr;
1484 struct fcoe_interface *fcoe;
1485 struct fcoe_port *port = NULL;
1486 struct fcoe_dev_stats *stats;
1487 u32 link_possible = 1;
1491 read_lock(&fcoe_hostlist_lock);
1492 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
1494 if (fcoe->netdev == netdev) {
1499 read_unlock(&fcoe_hostlist_lock);
1507 case NETDEV_GOING_DOWN:
1513 case NETDEV_CHANGEMTU:
1514 mfs = netdev->mtu - (sizeof(struct fcoe_hdr) +
1515 sizeof(struct fcoe_crc_eof));
1516 if (mfs >= FC_MIN_MAX_FRAME)
1517 fc_set_mfs(lp, mfs);
1519 case NETDEV_REGISTER:
1522 FCOE_NETDEV_DBG(netdev, "Unknown event %ld "
1523 "from netdev netlink\n", event);
1525 if (link_possible && !fcoe_link_ok(lp))
1526 fcoe_ctlr_link_up(&port->ctlr);
1527 else if (fcoe_ctlr_link_down(&port->ctlr)) {
1528 stats = fc_lport_get_stats(lp);
1529 stats->LinkFailureCount++;
1530 fcoe_clean_pending_queue(lp);
1537 * fcoe_if_to_netdev() - parse a name buffer to get netdev
1538 * @buffer: incoming buffer to be copied
1540 * Returns: NULL or ptr to net_device
1542 static struct net_device *fcoe_if_to_netdev(const char *buffer)
1545 char ifname[IFNAMSIZ + 2];
1548 strlcpy(ifname, buffer, IFNAMSIZ);
1549 cp = ifname + strlen(ifname);
1550 while (--cp >= ifname && *cp == '\n')
1552 return dev_get_by_name(&init_net, ifname);
1558 * fcoe_netdev_to_module_owner() - finds out the driver module of the netdev
1559 * @netdev: the target netdev
1561 * Returns: ptr to the struct module, NULL for failure
1563 static struct module *
1564 fcoe_netdev_to_module_owner(const struct net_device *netdev)
1571 dev = netdev->dev.parent;
1578 return dev->driver->owner;
1582 * fcoe_ethdrv_get() - Hold the Ethernet driver
1583 * @netdev: the target netdev
1585 * Holds the Ethernet driver module by try_module_get() for
1586 * the corresponding netdev.
1588 * Returns: 0 for success
1590 static int fcoe_ethdrv_get(const struct net_device *netdev)
1592 struct module *owner;
1594 owner = fcoe_netdev_to_module_owner(netdev);
1596 FCOE_NETDEV_DBG(netdev, "Hold driver module %s\n",
1597 module_name(owner));
1598 return try_module_get(owner);
1604 * fcoe_ethdrv_put() - Release the Ethernet driver
1605 * @netdev: the target netdev
1607 * Releases the Ethernet driver module by module_put for
1608 * the corresponding netdev.
1610 * Returns: 0 for success
1612 static int fcoe_ethdrv_put(const struct net_device *netdev)
1614 struct module *owner;
1616 owner = fcoe_netdev_to_module_owner(netdev);
1618 FCOE_NETDEV_DBG(netdev, "Release driver module %s\n",
1619 module_name(owner));
1627 * fcoe_destroy() - handles the destroy from sysfs
1628 * @buffer: expected to be an eth if name
1629 * @kp: associated kernel param
1631 * Returns: 0 for success
1633 static int fcoe_destroy(const char *buffer, struct kernel_param *kp)
1635 struct net_device *netdev;
1636 struct fc_lport *lport;
1639 netdev = fcoe_if_to_netdev(buffer);
1644 /* look for existing lport */
1645 lport = fcoe_hostlist_lookup(netdev);
1650 fcoe_if_destroy(lport);
1651 fcoe_ethdrv_put(netdev);
1660 * fcoe_create() - Handles the create call from sysfs
1661 * @buffer: expected to be an eth if name
1662 * @kp: associated kernel param
1664 * Returns: 0 for success
1666 static int fcoe_create(const char *buffer, struct kernel_param *kp)
1669 struct fc_lport *lport;
1670 struct net_device *netdev;
1672 netdev = fcoe_if_to_netdev(buffer);
1677 /* look for existing lport */
1678 if (fcoe_hostlist_lookup(netdev)) {
1682 fcoe_ethdrv_get(netdev);
1684 lport = fcoe_if_create(netdev, &netdev->dev);
1685 if (IS_ERR(lport)) {
1686 printk(KERN_ERR "fcoe: Failed to create interface (%s)\n",
1688 fcoe_ethdrv_put(netdev);
1699 module_param_call(create, fcoe_create, NULL, NULL, S_IWUSR);
1700 __MODULE_PARM_TYPE(create, "string");
1701 MODULE_PARM_DESC(create, "Create fcoe fcoe using net device passed in.");
1702 module_param_call(destroy, fcoe_destroy, NULL, NULL, S_IWUSR);
1703 __MODULE_PARM_TYPE(destroy, "string");
1704 MODULE_PARM_DESC(destroy, "Destroy fcoe fcoe");
1707 * fcoe_link_ok() - Check if link is ok for the fc_lport
1708 * @lp: ptr to the fc_lport
1710 * Any permanently-disqualifying conditions have been previously checked.
1711 * This also updates the speed setting, which may change with link for 100/1000.
1713 * This function should probably be checking for PAUSE support at some point
1714 * in the future. Currently Per-priority-pause is not determinable using
1715 * ethtool, so we shouldn't be restrictive until that problem is resolved.
1717 * Returns: 0 if link is OK for use by FCoE.
1720 int fcoe_link_ok(struct fc_lport *lp)
1722 struct fcoe_port *port = lport_priv(lp);
1723 struct net_device *dev = port->fcoe->netdev;
1724 struct ethtool_cmd ecmd = { ETHTOOL_GSET };
1726 if ((dev->flags & IFF_UP) && netif_carrier_ok(dev) &&
1727 (!dev_ethtool_get_settings(dev, &ecmd))) {
1728 lp->link_supported_speeds &=
1729 ~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
1730 if (ecmd.supported & (SUPPORTED_1000baseT_Half |
1731 SUPPORTED_1000baseT_Full))
1732 lp->link_supported_speeds |= FC_PORTSPEED_1GBIT;
1733 if (ecmd.supported & SUPPORTED_10000baseT_Full)
1734 lp->link_supported_speeds |=
1735 FC_PORTSPEED_10GBIT;
1736 if (ecmd.speed == SPEED_1000)
1737 lp->link_speed = FC_PORTSPEED_1GBIT;
1738 if (ecmd.speed == SPEED_10000)
1739 lp->link_speed = FC_PORTSPEED_10GBIT;
1747 * fcoe_percpu_clean() - Clear the pending skbs for an lport
1750 void fcoe_percpu_clean(struct fc_lport *lp)
1752 struct fcoe_percpu_s *pp;
1753 struct fcoe_rcv_info *fr;
1754 struct sk_buff_head *list;
1755 struct sk_buff *skb, *next;
1756 struct sk_buff *head;
1759 for_each_possible_cpu(cpu) {
1760 pp = &per_cpu(fcoe_percpu, cpu);
1761 spin_lock_bh(&pp->fcoe_rx_list.lock);
1762 list = &pp->fcoe_rx_list;
1764 for (skb = head; skb != (struct sk_buff *)list;
1767 fr = fcoe_dev_from_skb(skb);
1768 if (fr->fr_dev == lp) {
1769 __skb_unlink(skb, list);
1773 spin_unlock_bh(&pp->fcoe_rx_list.lock);
1778 * fcoe_clean_pending_queue() - Dequeue a skb and free it
1779 * @lp: the corresponding fc_lport
1783 void fcoe_clean_pending_queue(struct fc_lport *lp)
1785 struct fcoe_port *port = lport_priv(lp);
1786 struct sk_buff *skb;
1788 spin_lock_bh(&port->fcoe_pending_queue.lock);
1789 while ((skb = __skb_dequeue(&port->fcoe_pending_queue)) != NULL) {
1790 spin_unlock_bh(&port->fcoe_pending_queue.lock);
1792 spin_lock_bh(&port->fcoe_pending_queue.lock);
1794 spin_unlock_bh(&port->fcoe_pending_queue.lock);
1798 * fcoe_reset() - Resets the fcoe
1799 * @shost: shost the reset is from
1803 int fcoe_reset(struct Scsi_Host *shost)
1805 struct fc_lport *lport = shost_priv(shost);
1806 fc_lport_reset(lport);
1811 * fcoe_hostlist_lookup_port() - find the corresponding lport by a given device
1812 * @dev: this is currently ptr to net_device
1814 * Called with fcoe_hostlist_lock held.
1816 * Returns: NULL or the located fcoe_port
1818 static struct fcoe_interface *
1819 fcoe_hostlist_lookup_port(const struct net_device *dev)
1821 struct fcoe_interface *fcoe;
1823 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
1824 if (fcoe->netdev == dev)
1831 * fcoe_hostlist_lookup() - Find the corresponding lport by netdev
1832 * @netdev: ptr to net_device
1834 * Returns: 0 for success
1836 struct fc_lport *fcoe_hostlist_lookup(const struct net_device *netdev)
1838 struct fcoe_interface *fcoe;
1840 read_lock(&fcoe_hostlist_lock);
1841 fcoe = fcoe_hostlist_lookup_port(netdev);
1842 read_unlock(&fcoe_hostlist_lock);
1844 return (fcoe) ? fcoe->priv->ctlr.lp : NULL;
1848 * fcoe_hostlist_add() - Add a lport to lports list
1849 * @lp: ptr to the fc_lport to be added
1851 * Called with write fcoe_hostlist_lock held.
1853 * Returns: 0 for success
1855 int fcoe_hostlist_add(const struct fc_lport *lport)
1857 struct fcoe_interface *fcoe;
1858 struct fcoe_port *port;
1860 fcoe = fcoe_hostlist_lookup_port(fcoe_netdev(lport));
1862 port = lport_priv(lport);
1864 list_add_tail(&fcoe->list, &fcoe_hostlist);
1870 * fcoe_hostlist_remove() - remove a lport from lports list
1871 * @lp: ptr to the fc_lport to be removed
1873 * Returns: 0 for success
1875 int fcoe_hostlist_remove(const struct fc_lport *lport)
1877 struct fcoe_interface *fcoe;
1879 write_lock_bh(&fcoe_hostlist_lock);
1880 fcoe = fcoe_hostlist_lookup_port(fcoe_netdev(lport));
1882 list_del(&fcoe->list);
1883 write_unlock_bh(&fcoe_hostlist_lock);
1889 * fcoe_init() - fcoe module loading initialization
1891 * Returns 0 on success, negative on failure
1893 static int __init fcoe_init(void)
1897 struct fcoe_percpu_s *p;
1899 for_each_possible_cpu(cpu) {
1900 p = &per_cpu(fcoe_percpu, cpu);
1901 skb_queue_head_init(&p->fcoe_rx_list);
1904 for_each_online_cpu(cpu)
1905 fcoe_percpu_thread_create(cpu);
1907 /* Initialize per CPU interrupt thread */
1908 rc = register_hotcpu_notifier(&fcoe_cpu_notifier);
1912 /* Setup link change notification */
1915 rc = fcoe_if_init();
1922 for_each_online_cpu(cpu) {
1923 fcoe_percpu_thread_destroy(cpu);
1928 module_init(fcoe_init);
1931 * fcoe_exit() - fcoe module unloading cleanup
1933 * Returns 0 on success, negative on failure
1935 static void __exit fcoe_exit(void)
1938 struct fcoe_interface *fcoe, *tmp;
1942 /* releases the associated fcoe hosts */
1943 list_for_each_entry_safe(fcoe, tmp, &fcoe_hostlist, list)
1944 fcoe_if_destroy(fcoe->priv->ctlr.lp);
1946 unregister_hotcpu_notifier(&fcoe_cpu_notifier);
1948 for_each_online_cpu(cpu)
1949 fcoe_percpu_thread_destroy(cpu);
1951 /* detach from scsi transport */
1954 module_exit(fcoe_exit);