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).");
58 DEFINE_MUTEX(fcoe_config_mutex);
60 /* fcoe_percpu_clean completion. Waiter protected by fcoe_create_mutex */
61 static DECLARE_COMPLETION(fcoe_flush_completion);
64 /* must only by accessed under the RTNL mutex */
65 LIST_HEAD(fcoe_hostlist);
66 DEFINE_PER_CPU(struct fcoe_percpu_s, fcoe_percpu);
68 /* Function Prototypes */
69 static int fcoe_reset(struct Scsi_Host *shost);
70 static int fcoe_xmit(struct fc_lport *, struct fc_frame *);
71 static int fcoe_rcv(struct sk_buff *, struct net_device *,
72 struct packet_type *, struct net_device *);
73 static int fcoe_percpu_receive_thread(void *arg);
74 static void fcoe_clean_pending_queue(struct fc_lport *lp);
75 static void fcoe_percpu_clean(struct fc_lport *lp);
76 static int fcoe_link_ok(struct fc_lport *lp);
78 static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *);
79 static int fcoe_hostlist_add(const struct fc_lport *);
81 static void fcoe_check_wait_queue(struct fc_lport *, struct sk_buff *);
82 static int fcoe_device_notification(struct notifier_block *, ulong, void *);
83 static void fcoe_dev_setup(void);
84 static void fcoe_dev_cleanup(void);
85 static struct fcoe_interface *
86 fcoe_hostlist_lookup_port(const struct net_device *dev);
88 /* notification function from net device */
89 static struct notifier_block fcoe_notifier = {
90 .notifier_call = fcoe_device_notification,
93 static struct scsi_transport_template *scsi_transport_fcoe_sw;
95 struct fc_function_template fcoe_transport_function = {
96 .show_host_node_name = 1,
97 .show_host_port_name = 1,
98 .show_host_supported_classes = 1,
99 .show_host_supported_fc4s = 1,
100 .show_host_active_fc4s = 1,
101 .show_host_maxframe_size = 1,
103 .show_host_port_id = 1,
104 .show_host_supported_speeds = 1,
105 .get_host_speed = fc_get_host_speed,
106 .show_host_speed = 1,
107 .show_host_port_type = 1,
108 .get_host_port_state = fc_get_host_port_state,
109 .show_host_port_state = 1,
110 .show_host_symbolic_name = 1,
112 .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
113 .show_rport_maxframe_size = 1,
114 .show_rport_supported_classes = 1,
116 .show_host_fabric_name = 1,
117 .show_starget_node_name = 1,
118 .show_starget_port_name = 1,
119 .show_starget_port_id = 1,
120 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
121 .show_rport_dev_loss_tmo = 1,
122 .get_fc_host_stats = fc_get_host_stats,
123 .issue_fc_host_lip = fcoe_reset,
125 .terminate_rport_io = fc_rport_terminate_io,
128 static struct scsi_host_template fcoe_shost_template = {
129 .module = THIS_MODULE,
130 .name = "FCoE Driver",
131 .proc_name = FCOE_NAME,
132 .queuecommand = fc_queuecommand,
133 .eh_abort_handler = fc_eh_abort,
134 .eh_device_reset_handler = fc_eh_device_reset,
135 .eh_host_reset_handler = fc_eh_host_reset,
136 .slave_alloc = fc_slave_alloc,
137 .change_queue_depth = fc_change_queue_depth,
138 .change_queue_type = fc_change_queue_type,
141 .can_queue = FCOE_MAX_OUTSTANDING_COMMANDS,
142 .use_clustering = ENABLE_CLUSTERING,
143 .sg_tablesize = SG_ALL,
144 .max_sectors = 0xffff,
147 static int fcoe_fip_recv(struct sk_buff *skb, struct net_device *dev,
148 struct packet_type *ptype,
149 struct net_device *orig_dev);
151 * fcoe_interface_setup()
152 * @fcoe: new fcoe_interface
153 * @netdev : ptr to the associated netdevice struct
155 * Returns : 0 for success
156 * Locking: must be called with the RTNL mutex held
158 static int fcoe_interface_setup(struct fcoe_interface *fcoe,
159 struct net_device *netdev)
161 struct fcoe_ctlr *fip = &fcoe->ctlr;
162 struct netdev_hw_addr *ha;
163 u8 flogi_maddr[ETH_ALEN];
164 const struct net_device_ops *ops;
166 fcoe->netdev = netdev;
168 /* Let LLD initialize for FCoE */
169 ops = netdev->netdev_ops;
170 if (ops->ndo_fcoe_enable) {
171 if (ops->ndo_fcoe_enable(netdev))
172 FCOE_NETDEV_DBG(netdev, "Failed to enable FCoE"
173 " specific feature for LLD.\n");
176 /* Do not support for bonding device */
177 if ((netdev->priv_flags & IFF_MASTER_ALB) ||
178 (netdev->priv_flags & IFF_SLAVE_INACTIVE) ||
179 (netdev->priv_flags & IFF_MASTER_8023AD)) {
183 /* look for SAN MAC address, if multiple SAN MACs exist, only
184 * use the first one for SPMA */
186 for_each_dev_addr(netdev, ha) {
187 if ((ha->type == NETDEV_HW_ADDR_T_SAN) &&
188 (is_valid_ether_addr(fip->ctl_src_addr))) {
189 memcpy(fip->ctl_src_addr, ha->addr, ETH_ALEN);
196 /* setup Source Mac Address */
198 memcpy(fip->ctl_src_addr, netdev->dev_addr, netdev->addr_len);
201 * Add FCoE MAC address as second unicast MAC address
202 * or enter promiscuous mode if not capable of listening
203 * for multiple unicast MACs.
205 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
206 dev_unicast_add(netdev, flogi_maddr);
208 dev_unicast_add(netdev, fip->ctl_src_addr);
209 dev_mc_add(netdev, FIP_ALL_ENODE_MACS, ETH_ALEN, 0);
212 * setup the receive function from ethernet driver
213 * on the ethertype for the given device
215 fcoe->fcoe_packet_type.func = fcoe_rcv;
216 fcoe->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
217 fcoe->fcoe_packet_type.dev = netdev;
218 dev_add_pack(&fcoe->fcoe_packet_type);
220 fcoe->fip_packet_type.func = fcoe_fip_recv;
221 fcoe->fip_packet_type.type = htons(ETH_P_FIP);
222 fcoe->fip_packet_type.dev = netdev;
223 dev_add_pack(&fcoe->fip_packet_type);
228 static void fcoe_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb);
229 static void fcoe_update_src_mac(struct fc_lport *lport, u8 *addr);
230 static u8 *fcoe_get_src_mac(struct fc_lport *lport);
231 static void fcoe_destroy_work(struct work_struct *work);
234 * fcoe_interface_create()
235 * @netdev: network interface
237 * Returns: pointer to a struct fcoe_interface or NULL on error
239 static struct fcoe_interface *fcoe_interface_create(struct net_device *netdev)
241 struct fcoe_interface *fcoe;
243 fcoe = kzalloc(sizeof(*fcoe), GFP_KERNEL);
245 FCOE_NETDEV_DBG(netdev, "Could not allocate fcoe structure\n");
250 kref_init(&fcoe->kref);
255 fcoe_ctlr_init(&fcoe->ctlr);
256 fcoe->ctlr.send = fcoe_fip_send;
257 fcoe->ctlr.update_mac = fcoe_update_src_mac;
258 fcoe->ctlr.get_src_addr = fcoe_get_src_mac;
260 fcoe_interface_setup(fcoe, netdev);
266 * fcoe_interface_cleanup() - clean up netdev configurations
269 * Caller must be holding the RTNL mutex
271 void fcoe_interface_cleanup(struct fcoe_interface *fcoe)
273 struct net_device *netdev = fcoe->netdev;
274 struct fcoe_ctlr *fip = &fcoe->ctlr;
275 u8 flogi_maddr[ETH_ALEN];
276 const struct net_device_ops *ops;
279 * Don't listen for Ethernet packets anymore.
280 * synchronize_net() ensures that the packet handlers are not running
281 * on another CPU. dev_remove_pack() would do that, this calls the
282 * unsyncronized version __dev_remove_pack() to avoid multiple delays.
284 __dev_remove_pack(&fcoe->fcoe_packet_type);
285 __dev_remove_pack(&fcoe->fip_packet_type);
288 /* Delete secondary MAC addresses */
289 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
290 dev_unicast_delete(netdev, flogi_maddr);
292 dev_unicast_delete(netdev, fip->ctl_src_addr);
293 dev_mc_delete(netdev, FIP_ALL_ENODE_MACS, ETH_ALEN, 0);
295 /* Tell the LLD we are done w/ FCoE */
296 ops = netdev->netdev_ops;
297 if (ops->ndo_fcoe_disable) {
298 if (ops->ndo_fcoe_disable(netdev))
299 FCOE_NETDEV_DBG(netdev, "Failed to disable FCoE"
300 " specific feature for LLD.\n");
305 * fcoe_interface_release() - fcoe_port kref release function
306 * @kref: embedded reference count in an fcoe_interface struct
308 static void fcoe_interface_release(struct kref *kref)
310 struct fcoe_interface *fcoe;
311 struct net_device *netdev;
313 fcoe = container_of(kref, struct fcoe_interface, kref);
314 netdev = fcoe->netdev;
315 /* tear-down the FCoE controller */
316 fcoe_ctlr_destroy(&fcoe->ctlr);
322 * fcoe_interface_get()
325 static inline void fcoe_interface_get(struct fcoe_interface *fcoe)
327 kref_get(&fcoe->kref);
331 * fcoe_interface_put()
334 static inline void fcoe_interface_put(struct fcoe_interface *fcoe)
336 kref_put(&fcoe->kref, fcoe_interface_release);
340 * fcoe_fip_recv - handle a received FIP frame.
341 * @skb: the receive skb
342 * @dev: associated &net_device
343 * @ptype: the &packet_type structure which was used to register this handler.
344 * @orig_dev: original receive &net_device, in case @dev is a bond.
346 * Returns: 0 for success
348 static int fcoe_fip_recv(struct sk_buff *skb, struct net_device *dev,
349 struct packet_type *ptype,
350 struct net_device *orig_dev)
352 struct fcoe_interface *fcoe;
354 fcoe = container_of(ptype, struct fcoe_interface, fip_packet_type);
355 fcoe_ctlr_recv(&fcoe->ctlr, skb);
360 * fcoe_fip_send() - send an Ethernet-encapsulated FIP frame.
361 * @fip: FCoE controller.
364 static void fcoe_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
366 skb->dev = fcoe_from_ctlr(fip)->netdev;
371 * fcoe_update_src_mac() - Update Ethernet MAC filters.
372 * @lport: libfc lport
373 * @addr: Unicast MAC address to add.
375 * Remove any previously-set unicast MAC filter.
376 * Add secondary FCoE MAC address filter for our OUI.
378 static void fcoe_update_src_mac(struct fc_lport *lport, u8 *addr)
380 struct fcoe_port *port = lport_priv(lport);
381 struct fcoe_interface *fcoe = port->fcoe;
384 if (!is_zero_ether_addr(port->data_src_addr))
385 dev_unicast_delete(fcoe->netdev, port->data_src_addr);
386 if (!is_zero_ether_addr(addr))
387 dev_unicast_add(fcoe->netdev, addr);
388 memcpy(port->data_src_addr, addr, ETH_ALEN);
393 * fcoe_get_src_mac() - return the Ethernet source address for an lport
394 * @lport: libfc lport
396 static u8 *fcoe_get_src_mac(struct fc_lport *lport)
398 struct fcoe_port *port = lport_priv(lport);
400 return port->data_src_addr;
404 * fcoe_lport_config() - sets up the fc_lport
405 * @lp: ptr to the fc_lport
407 * Returns: 0 for success
409 static int fcoe_lport_config(struct fc_lport *lp)
413 lp->max_retry_count = 3;
414 lp->max_rport_retry_count = 3;
415 lp->e_d_tov = 2 * 1000; /* FC-FS default */
416 lp->r_a_tov = 2 * 2 * 1000;
417 lp->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
418 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
420 fc_lport_init_stats(lp);
422 /* lport fc_lport related configuration */
425 /* offload related configuration */
436 * fcoe_queue_timer() - fcoe queue timer
437 * @lp: the fc_lport pointer
439 * Calls fcoe_check_wait_queue on timeout
442 static void fcoe_queue_timer(ulong lp)
444 fcoe_check_wait_queue((struct fc_lport *)lp, NULL);
448 * fcoe_netdev_config() - Set up netdev for SW FCoE
449 * @lp : ptr to the fc_lport
450 * @netdev : ptr to the associated netdevice struct
452 * Must be called after fcoe_lport_config() as it will use lport mutex
454 * Returns : 0 for success
456 static int fcoe_netdev_config(struct fc_lport *lp, struct net_device *netdev)
460 struct fcoe_interface *fcoe;
461 struct fcoe_port *port;
463 /* Setup lport private data to point to fcoe softc */
464 port = lport_priv(lp);
468 * Determine max frame size based on underlying device and optional
469 * user-configured limit. If the MFS is too low, fcoe_link_ok()
470 * will return 0, so do this first.
473 if (netdev->features & NETIF_F_FCOE_MTU) {
475 FCOE_NETDEV_DBG(netdev, "Supports FCOE_MTU of %d bytes\n", mfs);
477 mfs -= (sizeof(struct fcoe_hdr) + sizeof(struct fcoe_crc_eof));
478 if (fc_set_mfs(lp, mfs))
481 /* offload features support */
482 if (netdev->features & NETIF_F_SG)
485 if (netdev->features & NETIF_F_FCOE_CRC) {
487 FCOE_NETDEV_DBG(netdev, "Supports FCCRC offload\n");
489 if (netdev->features & NETIF_F_FSO) {
491 lp->lso_max = netdev->gso_max_size;
492 FCOE_NETDEV_DBG(netdev, "Supports LSO for max len 0x%x\n",
495 if (netdev->fcoe_ddp_xid) {
497 lp->lro_xid = netdev->fcoe_ddp_xid;
498 FCOE_NETDEV_DBG(netdev, "Supports LRO for max xid 0x%x\n",
501 skb_queue_head_init(&port->fcoe_pending_queue);
502 port->fcoe_pending_queue_active = 0;
503 setup_timer(&port->timer, fcoe_queue_timer, (unsigned long)lp);
505 wwnn = fcoe_wwn_from_mac(netdev->dev_addr, 1, 0);
506 fc_set_wwnn(lp, wwnn);
507 /* XXX - 3rd arg needs to be vlan id */
508 wwpn = fcoe_wwn_from_mac(netdev->dev_addr, 2, 0);
509 fc_set_wwpn(lp, wwpn);
515 * fcoe_shost_config() - Sets up fc_lport->host
516 * @lp : ptr to the fc_lport
517 * @shost : ptr to the associated scsi host
518 * @dev : device associated to scsi host
520 * Must be called after fcoe_lport_config() and fcoe_netdev_config()
522 * Returns : 0 for success
524 static int fcoe_shost_config(struct fc_lport *lp, struct Scsi_Host *shost,
529 /* lport scsi host config */
530 lp->host->max_lun = FCOE_MAX_LUN;
531 lp->host->max_id = FCOE_MAX_FCP_TARGET;
532 lp->host->max_channel = 0;
533 lp->host->transportt = scsi_transport_fcoe_sw;
535 /* add the new host to the SCSI-ml */
536 rc = scsi_add_host(lp->host, dev);
538 FCOE_NETDEV_DBG(fcoe_netdev(lp), "fcoe_shost_config: "
539 "error on scsi_add_host\n");
542 sprintf(fc_host_symbolic_name(lp->host), "%s v%s over %s",
543 FCOE_NAME, FCOE_VERSION,
544 fcoe_netdev(lp)->name);
550 * fcoe_oem_match() - match for read types IO
551 * @fp: the fc_frame for new IO.
553 * Returns : true for read types IO, otherwise returns false.
555 bool fcoe_oem_match(struct fc_frame *fp)
557 return fc_fcp_is_read(fr_fsp(fp)) &&
558 (fr_fsp(fp)->data_len > fcoe_ddp_min);
562 * fcoe_em_config() - allocates em for this lport
563 * @lp: the fcoe that em is to allocated for
565 * Returns : 0 on success
567 static inline int fcoe_em_config(struct fc_lport *lp)
569 struct fcoe_port *port = lport_priv(lp);
570 struct fcoe_interface *fcoe = port->fcoe;
571 struct fcoe_interface *oldfcoe = NULL;
572 struct net_device *old_real_dev, *cur_real_dev;
573 u16 min_xid = FCOE_MIN_XID;
574 u16 max_xid = FCOE_MAX_XID;
577 * Check if need to allocate an em instance for
578 * offload exchange ids to be shared across all VN_PORTs/lport.
580 if (!lp->lro_enabled || !lp->lro_xid || (lp->lro_xid >= max_xid)) {
586 * Reuse existing offload em instance in case
587 * it is already allocated on real eth device
589 if (fcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
590 cur_real_dev = vlan_dev_real_dev(fcoe->netdev);
592 cur_real_dev = fcoe->netdev;
594 list_for_each_entry(oldfcoe, &fcoe_hostlist, list) {
595 if (oldfcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
596 old_real_dev = vlan_dev_real_dev(oldfcoe->netdev);
598 old_real_dev = oldfcoe->netdev;
600 if (cur_real_dev == old_real_dev) {
601 fcoe->oem = oldfcoe->oem;
607 if (!fc_exch_mgr_add(lp, fcoe->oem, fcoe_oem_match)) {
608 printk(KERN_ERR "fcoe_em_config: failed to add "
609 "offload em:%p on interface:%s\n",
610 fcoe->oem, fcoe->netdev->name);
614 fcoe->oem = fc_exch_mgr_alloc(lp, FC_CLASS_3,
615 FCOE_MIN_XID, lp->lro_xid,
618 printk(KERN_ERR "fcoe_em_config: failed to allocate "
619 "em for offload exches on interface:%s\n",
626 * Exclude offload EM xid range from next EM xid range.
628 min_xid += lp->lro_xid + 1;
631 if (!fc_exch_mgr_alloc(lp, FC_CLASS_3, min_xid, max_xid, NULL)) {
632 printk(KERN_ERR "fcoe_em_config: failed to "
633 "allocate em on interface %s\n", fcoe->netdev->name);
641 * fcoe_if_destroy() - FCoE software HBA tear-down function
642 * @lport: fc_lport to destroy
644 static void fcoe_if_destroy(struct fc_lport *lport)
646 struct fcoe_port *port = lport_priv(lport);
647 struct fcoe_interface *fcoe = port->fcoe;
648 struct net_device *netdev = fcoe->netdev;
650 FCOE_NETDEV_DBG(netdev, "Destroying interface\n");
652 /* Logout of the fabric */
653 fc_fabric_logoff(lport);
655 /* Cleanup the fc_lport */
656 fc_lport_destroy(lport);
657 fc_fcp_destroy(lport);
659 /* Stop the transmit retry timer */
660 del_timer_sync(&port->timer);
662 /* Free existing transmit skbs */
663 fcoe_clean_pending_queue(lport);
666 if (!is_zero_ether_addr(port->data_src_addr))
667 dev_unicast_delete(netdev, port->data_src_addr);
670 /* receives may not be stopped until after this */
671 fcoe_interface_put(fcoe);
673 /* Free queued packets for the per-CPU receive threads */
674 fcoe_percpu_clean(lport);
676 /* Detach from the scsi-ml */
677 fc_remove_host(lport->host);
678 scsi_remove_host(lport->host);
680 /* There are no more rports or I/O, free the EM */
681 fc_exch_mgr_free(lport);
683 /* Free memory used by statistical counters */
684 fc_lport_free_stats(lport);
686 /* Release the Scsi_Host */
687 scsi_host_put(lport->host);
691 * fcoe_ddp_setup - calls LLD's ddp_setup through net_device
692 * @lp: the corresponding fc_lport
693 * @xid: the exchange id for this ddp transfer
694 * @sgl: the scatterlist describing this transfer
695 * @sgc: number of sg items
699 static int fcoe_ddp_setup(struct fc_lport *lp, u16 xid,
700 struct scatterlist *sgl, unsigned int sgc)
702 struct net_device *n = fcoe_netdev(lp);
704 if (n->netdev_ops->ndo_fcoe_ddp_setup)
705 return n->netdev_ops->ndo_fcoe_ddp_setup(n, xid, sgl, sgc);
711 * fcoe_ddp_done - calls LLD's ddp_done through net_device
712 * @lp: the corresponding fc_lport
713 * @xid: the exchange id for this ddp transfer
715 * Returns : the length of data that have been completed by ddp
717 static int fcoe_ddp_done(struct fc_lport *lp, u16 xid)
719 struct net_device *n = fcoe_netdev(lp);
721 if (n->netdev_ops->ndo_fcoe_ddp_done)
722 return n->netdev_ops->ndo_fcoe_ddp_done(n, xid);
726 static struct fc_seq *fcoe_elsct_send(struct fc_lport *lport,
727 u32 did, struct fc_frame *fp, unsigned int op,
728 void (*resp)(struct fc_seq *, struct fc_frame *, void *),
729 void *arg, u32 timeout);
731 static struct libfc_function_template fcoe_libfc_fcn_templ = {
732 .frame_send = fcoe_xmit,
733 .ddp_setup = fcoe_ddp_setup,
734 .ddp_done = fcoe_ddp_done,
735 .elsct_send = fcoe_elsct_send,
739 * fcoe_if_create() - this function creates the fcoe port
740 * @fcoe: fcoe_interface structure to create an fc_lport instance on
741 * @parent: device pointer to be the parent in sysfs for the SCSI host
743 * Creates fc_lport struct and scsi_host for lport, configures lport.
745 * Returns : The allocated fc_lport or an error pointer
747 static struct fc_lport *fcoe_if_create(struct fcoe_interface *fcoe,
748 struct device *parent)
751 struct fc_lport *lport = NULL;
752 struct fcoe_port *port;
753 struct Scsi_Host *shost;
754 struct net_device *netdev = fcoe->netdev;
756 FCOE_NETDEV_DBG(netdev, "Create Interface\n");
758 lport = libfc_host_alloc(&fcoe_shost_template,
759 sizeof(struct fcoe_port));
761 FCOE_NETDEV_DBG(netdev, "Could not allocate host structure\n");
766 port = lport_priv(lport);
769 INIT_WORK(&port->destroy_work, fcoe_destroy_work);
771 /* configure fc_lport, e.g., em */
772 rc = fcoe_lport_config(lport);
774 FCOE_NETDEV_DBG(netdev, "Could not configure lport for the "
779 /* configure lport network properties */
780 rc = fcoe_netdev_config(lport, netdev);
782 FCOE_NETDEV_DBG(netdev, "Could not configure netdev for the "
787 /* configure lport scsi host properties */
788 rc = fcoe_shost_config(lport, shost, parent);
790 FCOE_NETDEV_DBG(netdev, "Could not configure shost for the "
795 /* Initialize the library */
796 rc = fcoe_libfc_config(lport, &fcoe_libfc_fcn_templ);
798 FCOE_NETDEV_DBG(netdev, "Could not configure libfc for the "
804 * fcoe_em_alloc() and fcoe_hostlist_add() both
805 * need to be atomic with respect to other changes to the hostlist
806 * since fcoe_em_alloc() looks for an existing EM
807 * instance on host list updated by fcoe_hostlist_add().
809 * This is currently handled through the fcoe_config_mutex begin held.
812 /* lport exch manager allocation */
813 rc = fcoe_em_config(lport);
815 FCOE_NETDEV_DBG(netdev, "Could not configure the EM for the "
820 fcoe_interface_get(fcoe);
824 fc_exch_mgr_free(lport);
826 scsi_host_put(lport->host);
832 * fcoe_if_init() - attach to scsi transport
834 * Returns : 0 on success
836 static int __init fcoe_if_init(void)
838 /* attach to scsi transport */
839 scsi_transport_fcoe_sw =
840 fc_attach_transport(&fcoe_transport_function);
842 if (!scsi_transport_fcoe_sw) {
843 printk(KERN_ERR "fcoe: Failed to attach to the FC transport\n");
851 * fcoe_if_exit() - detach from scsi transport
853 * Returns : 0 on success
855 int __exit fcoe_if_exit(void)
857 fc_release_transport(scsi_transport_fcoe_sw);
858 scsi_transport_fcoe_sw = NULL;
863 * fcoe_percpu_thread_create() - Create a receive thread for an online cpu
864 * @cpu: cpu index for the online cpu
866 static void fcoe_percpu_thread_create(unsigned int cpu)
868 struct fcoe_percpu_s *p;
869 struct task_struct *thread;
871 p = &per_cpu(fcoe_percpu, cpu);
873 thread = kthread_create(fcoe_percpu_receive_thread,
874 (void *)p, "fcoethread/%d", cpu);
876 if (likely(!IS_ERR(thread))) {
877 kthread_bind(thread, cpu);
878 wake_up_process(thread);
880 spin_lock_bh(&p->fcoe_rx_list.lock);
882 spin_unlock_bh(&p->fcoe_rx_list.lock);
887 * fcoe_percpu_thread_destroy() - removes the rx thread for the given cpu
888 * @cpu: cpu index the rx thread is to be removed
890 * Destroys a per-CPU Rx thread. Any pending skbs are moved to the
891 * current CPU's Rx thread. If the thread being destroyed is bound to
892 * the CPU processing this context the skbs will be freed.
894 static void fcoe_percpu_thread_destroy(unsigned int cpu)
896 struct fcoe_percpu_s *p;
897 struct task_struct *thread;
898 struct page *crc_eof;
901 struct fcoe_percpu_s *p0;
902 unsigned targ_cpu = smp_processor_id();
903 #endif /* CONFIG_SMP */
905 FCOE_DBG("Destroying receive thread for CPU %d\n", cpu);
907 /* Prevent any new skbs from being queued for this CPU. */
908 p = &per_cpu(fcoe_percpu, cpu);
909 spin_lock_bh(&p->fcoe_rx_list.lock);
912 crc_eof = p->crc_eof_page;
913 p->crc_eof_page = NULL;
914 p->crc_eof_offset = 0;
915 spin_unlock_bh(&p->fcoe_rx_list.lock);
919 * Don't bother moving the skb's if this context is running
920 * on the same CPU that is having its thread destroyed. This
921 * can easily happen when the module is removed.
923 if (cpu != targ_cpu) {
924 p0 = &per_cpu(fcoe_percpu, targ_cpu);
925 spin_lock_bh(&p0->fcoe_rx_list.lock);
927 FCOE_DBG("Moving frames from CPU %d to CPU %d\n",
930 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
931 __skb_queue_tail(&p0->fcoe_rx_list, skb);
932 spin_unlock_bh(&p0->fcoe_rx_list.lock);
935 * The targeted CPU is not initialized and cannot accept
936 * new skbs. Unlock the targeted CPU and drop the skbs
937 * on the CPU that is going offline.
939 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
941 spin_unlock_bh(&p0->fcoe_rx_list.lock);
945 * This scenario occurs when the module is being removed
946 * and all threads are being destroyed. skbs will continue
947 * to be shifted from the CPU thread that is being removed
948 * to the CPU thread associated with the CPU that is processing
949 * the module removal. Once there is only one CPU Rx thread it
950 * will reach this case and we will drop all skbs and later
953 spin_lock_bh(&p->fcoe_rx_list.lock);
954 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
956 spin_unlock_bh(&p->fcoe_rx_list.lock);
960 * This a non-SMP scenario where the singular Rx thread is
961 * being removed. Free all skbs and stop the thread.
963 spin_lock_bh(&p->fcoe_rx_list.lock);
964 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
966 spin_unlock_bh(&p->fcoe_rx_list.lock);
970 kthread_stop(thread);
977 * fcoe_cpu_callback() - fcoe cpu hotplug event callback
978 * @nfb: callback data block
979 * @action: event triggering the callback
980 * @hcpu: index for the cpu of this event
982 * This creates or destroys per cpu data for fcoe
984 * Returns NOTIFY_OK always.
986 static int fcoe_cpu_callback(struct notifier_block *nfb,
987 unsigned long action, void *hcpu)
989 unsigned cpu = (unsigned long)hcpu;
993 case CPU_ONLINE_FROZEN:
994 FCOE_DBG("CPU %x online: Create Rx thread\n", cpu);
995 fcoe_percpu_thread_create(cpu);
998 case CPU_DEAD_FROZEN:
999 FCOE_DBG("CPU %x offline: Remove Rx thread\n", cpu);
1000 fcoe_percpu_thread_destroy(cpu);
1008 static struct notifier_block fcoe_cpu_notifier = {
1009 .notifier_call = fcoe_cpu_callback,
1013 * fcoe_rcv() - this is the fcoe receive function called by NET_RX_SOFTIRQ
1014 * @skb: the receive skb
1015 * @dev: associated net device
1017 * @olddev: last device
1019 * this function will receive the packet and build fc frame and pass it up
1021 * Returns: 0 for success
1023 int fcoe_rcv(struct sk_buff *skb, struct net_device *dev,
1024 struct packet_type *ptype, struct net_device *olddev)
1026 struct fc_lport *lp;
1027 struct fcoe_rcv_info *fr;
1028 struct fcoe_interface *fcoe;
1029 struct fc_frame_header *fh;
1030 struct fcoe_percpu_s *fps;
1033 fcoe = container_of(ptype, struct fcoe_interface, fcoe_packet_type);
1035 if (unlikely(lp == NULL)) {
1036 FCOE_NETDEV_DBG(dev, "Cannot find hba structure");
1042 FCOE_NETDEV_DBG(dev, "skb_info: len:%d data_len:%d head:%p "
1043 "data:%p tail:%p end:%p sum:%d dev:%s",
1044 skb->len, skb->data_len, skb->head, skb->data,
1045 skb_tail_pointer(skb), skb_end_pointer(skb),
1046 skb->csum, skb->dev ? skb->dev->name : "<NULL>");
1048 /* check for FCOE packet type */
1049 if (unlikely(eth_hdr(skb)->h_proto != htons(ETH_P_FCOE))) {
1050 FCOE_NETDEV_DBG(dev, "Wrong FC type frame");
1055 * Check for minimum frame length, and make sure required FCoE
1056 * and FC headers are pulled into the linear data area.
1058 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
1059 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
1062 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
1063 fh = (struct fc_frame_header *) skb_transport_header(skb);
1065 fr = fcoe_dev_from_skb(skb);
1070 * In case the incoming frame's exchange is originated from
1071 * the initiator, then received frame's exchange id is ANDed
1072 * with fc_cpu_mask bits to get the same cpu on which exchange
1073 * was originated, otherwise just use the current cpu.
1075 if (ntoh24(fh->fh_f_ctl) & FC_FC_EX_CTX)
1076 cpu = ntohs(fh->fh_ox_id) & fc_cpu_mask;
1078 cpu = smp_processor_id();
1080 fps = &per_cpu(fcoe_percpu, cpu);
1081 spin_lock_bh(&fps->fcoe_rx_list.lock);
1082 if (unlikely(!fps->thread)) {
1084 * The targeted CPU is not ready, let's target
1085 * the first CPU now. For non-SMP systems this
1086 * will check the same CPU twice.
1088 FCOE_NETDEV_DBG(dev, "CPU is online, but no receive thread "
1089 "ready for incoming skb- using first online "
1092 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1093 cpu = first_cpu(cpu_online_map);
1094 fps = &per_cpu(fcoe_percpu, cpu);
1095 spin_lock_bh(&fps->fcoe_rx_list.lock);
1097 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1103 * We now have a valid CPU that we're targeting for
1104 * this skb. We also have this receive thread locked,
1105 * so we're free to queue skbs into it's queue.
1107 __skb_queue_tail(&fps->fcoe_rx_list, skb);
1108 if (fps->fcoe_rx_list.qlen == 1)
1109 wake_up_process(fps->thread);
1111 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1115 fc_lport_get_stats(lp)->ErrorFrames++;
1123 * fcoe_start_io() - pass to netdev to start xmit for fcoe
1124 * @skb: the skb to be xmitted
1126 * Returns: 0 for success
1128 static inline int fcoe_start_io(struct sk_buff *skb)
1133 rc = dev_queue_xmit(skb);
1141 * fcoe_get_paged_crc_eof() - in case we need to alloc a page for crc_eof
1142 * @skb: the skb to be xmitted
1145 * Returns: 0 for success
1147 static int fcoe_get_paged_crc_eof(struct sk_buff *skb, int tlen)
1149 struct fcoe_percpu_s *fps;
1152 fps = &get_cpu_var(fcoe_percpu);
1153 page = fps->crc_eof_page;
1155 page = alloc_page(GFP_ATOMIC);
1157 put_cpu_var(fcoe_percpu);
1160 fps->crc_eof_page = page;
1161 fps->crc_eof_offset = 0;
1165 skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags, page,
1166 fps->crc_eof_offset, tlen);
1168 skb->data_len += tlen;
1169 skb->truesize += tlen;
1170 fps->crc_eof_offset += sizeof(struct fcoe_crc_eof);
1172 if (fps->crc_eof_offset >= PAGE_SIZE) {
1173 fps->crc_eof_page = NULL;
1174 fps->crc_eof_offset = 0;
1177 put_cpu_var(fcoe_percpu);
1182 * fcoe_fc_crc() - calculates FC CRC in this fcoe skb
1183 * @fp: the fc_frame containing data to be checksummed
1185 * This uses crc32() to calculate the crc for port frame
1186 * Return : 32 bit crc
1188 u32 fcoe_fc_crc(struct fc_frame *fp)
1190 struct sk_buff *skb = fp_skb(fp);
1191 struct skb_frag_struct *frag;
1192 unsigned char *data;
1193 unsigned long off, len, clen;
1197 crc = crc32(~0, skb->data, skb_headlen(skb));
1199 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1200 frag = &skb_shinfo(skb)->frags[i];
1201 off = frag->page_offset;
1204 clen = min(len, PAGE_SIZE - (off & ~PAGE_MASK));
1205 data = kmap_atomic(frag->page + (off >> PAGE_SHIFT),
1206 KM_SKB_DATA_SOFTIRQ);
1207 crc = crc32(crc, data + (off & ~PAGE_MASK), clen);
1208 kunmap_atomic(data, KM_SKB_DATA_SOFTIRQ);
1217 * fcoe_xmit() - FCoE frame transmit function
1218 * @lp: the associated local fcoe
1219 * @fp: the fc_frame to be transmitted
1221 * Return : 0 for success
1223 int fcoe_xmit(struct fc_lport *lp, struct fc_frame *fp)
1228 struct fcoe_crc_eof *cp;
1229 struct sk_buff *skb;
1230 struct fcoe_dev_stats *stats;
1231 struct fc_frame_header *fh;
1232 unsigned int hlen; /* header length implies the version */
1233 unsigned int tlen; /* trailer length */
1234 unsigned int elen; /* eth header, may include vlan */
1235 struct fcoe_port *port = lport_priv(lp);
1236 struct fcoe_interface *fcoe = port->fcoe;
1238 struct fcoe_hdr *hp;
1240 WARN_ON((fr_len(fp) % sizeof(u32)) != 0);
1242 fh = fc_frame_header_get(fp);
1244 wlen = skb->len / FCOE_WORD_TO_BYTE;
1251 if (unlikely(fh->fh_r_ctl == FC_RCTL_ELS_REQ) &&
1252 fcoe_ctlr_els_send(&fcoe->ctlr, lp, skb))
1258 elen = sizeof(struct ethhdr);
1259 hlen = sizeof(struct fcoe_hdr);
1260 tlen = sizeof(struct fcoe_crc_eof);
1261 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
1264 if (likely(lp->crc_offload)) {
1265 skb->ip_summed = CHECKSUM_PARTIAL;
1266 skb->csum_start = skb_headroom(skb);
1267 skb->csum_offset = skb->len;
1270 skb->ip_summed = CHECKSUM_NONE;
1271 crc = fcoe_fc_crc(fp);
1274 /* copy port crc and eof to the skb buff */
1275 if (skb_is_nonlinear(skb)) {
1277 if (fcoe_get_paged_crc_eof(skb, tlen)) {
1281 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
1282 cp = kmap_atomic(frag->page, KM_SKB_DATA_SOFTIRQ)
1283 + frag->page_offset;
1285 cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
1288 memset(cp, 0, sizeof(*cp));
1290 cp->fcoe_crc32 = cpu_to_le32(~crc);
1292 if (skb_is_nonlinear(skb)) {
1293 kunmap_atomic(cp, KM_SKB_DATA_SOFTIRQ);
1297 /* adjust skb network/transport offsets to match mac/fcoe/port */
1298 skb_push(skb, elen + hlen);
1299 skb_reset_mac_header(skb);
1300 skb_reset_network_header(skb);
1301 skb->mac_len = elen;
1302 skb->protocol = htons(ETH_P_FCOE);
1303 skb->dev = fcoe->netdev;
1305 /* fill up mac and fcoe headers */
1307 eh->h_proto = htons(ETH_P_FCOE);
1308 if (fcoe->ctlr.map_dest)
1309 fc_fcoe_set_mac(eh->h_dest, fh->fh_d_id);
1311 /* insert GW address */
1312 memcpy(eh->h_dest, fcoe->ctlr.dest_addr, ETH_ALEN);
1314 if (unlikely(fcoe->ctlr.flogi_oxid != FC_XID_UNKNOWN))
1315 memcpy(eh->h_source, fcoe->ctlr.ctl_src_addr, ETH_ALEN);
1317 memcpy(eh->h_source, port->data_src_addr, ETH_ALEN);
1319 hp = (struct fcoe_hdr *)(eh + 1);
1320 memset(hp, 0, sizeof(*hp));
1322 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
1325 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
1326 if (lp->seq_offload && fr_max_payload(fp)) {
1327 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
1328 skb_shinfo(skb)->gso_size = fr_max_payload(fp);
1330 skb_shinfo(skb)->gso_type = 0;
1331 skb_shinfo(skb)->gso_size = 0;
1333 /* update tx stats: regardless if LLD fails */
1334 stats = fc_lport_get_stats(lp);
1336 stats->TxWords += wlen;
1338 /* send down to lld */
1340 if (port->fcoe_pending_queue.qlen)
1341 fcoe_check_wait_queue(lp, skb);
1342 else if (fcoe_start_io(skb))
1343 fcoe_check_wait_queue(lp, skb);
1349 * fcoe_percpu_flush_done() - Indicate percpu queue flush completion.
1350 * @skb: the skb being completed.
1352 static void fcoe_percpu_flush_done(struct sk_buff *skb)
1354 complete(&fcoe_flush_completion);
1358 * fcoe_percpu_receive_thread() - recv thread per cpu
1359 * @arg: ptr to the fcoe per cpu struct
1361 * Return: 0 for success
1363 int fcoe_percpu_receive_thread(void *arg)
1365 struct fcoe_percpu_s *p = arg;
1367 struct fc_lport *lp;
1368 struct fcoe_rcv_info *fr;
1369 struct fcoe_dev_stats *stats;
1370 struct fc_frame_header *fh;
1371 struct sk_buff *skb;
1372 struct fcoe_crc_eof crc_eof;
1373 struct fc_frame *fp;
1375 struct fcoe_port *port;
1376 struct fcoe_hdr *hp;
1378 set_user_nice(current, -20);
1380 while (!kthread_should_stop()) {
1382 spin_lock_bh(&p->fcoe_rx_list.lock);
1383 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) == NULL) {
1384 set_current_state(TASK_INTERRUPTIBLE);
1385 spin_unlock_bh(&p->fcoe_rx_list.lock);
1387 set_current_state(TASK_RUNNING);
1388 if (kthread_should_stop())
1390 spin_lock_bh(&p->fcoe_rx_list.lock);
1392 spin_unlock_bh(&p->fcoe_rx_list.lock);
1393 fr = fcoe_dev_from_skb(skb);
1395 if (unlikely(lp == NULL)) {
1396 if (skb->destructor != fcoe_percpu_flush_done)
1397 FCOE_NETDEV_DBG(skb->dev, "NULL lport in skb");
1402 FCOE_NETDEV_DBG(skb->dev, "skb_info: len:%d data_len:%d "
1403 "head:%p data:%p tail:%p end:%p sum:%d dev:%s",
1404 skb->len, skb->data_len,
1405 skb->head, skb->data, skb_tail_pointer(skb),
1406 skb_end_pointer(skb), skb->csum,
1407 skb->dev ? skb->dev->name : "<NULL>");
1410 * Save source MAC address before discarding header.
1412 port = lport_priv(lp);
1413 if (skb_is_nonlinear(skb))
1414 skb_linearize(skb); /* not ideal */
1415 mac = eth_hdr(skb)->h_source;
1418 * Frame length checks and setting up the header pointers
1419 * was done in fcoe_rcv already.
1421 hp = (struct fcoe_hdr *) skb_network_header(skb);
1422 fh = (struct fc_frame_header *) skb_transport_header(skb);
1424 stats = fc_lport_get_stats(lp);
1425 if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) {
1426 if (stats->ErrorFrames < 5)
1427 printk(KERN_WARNING "fcoe: FCoE version "
1428 "mismatch: The frame has "
1429 "version %x, but the "
1430 "initiator supports version "
1431 "%x\n", FC_FCOE_DECAPS_VER(hp),
1433 stats->ErrorFrames++;
1438 skb_pull(skb, sizeof(struct fcoe_hdr));
1439 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
1442 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
1444 fp = (struct fc_frame *)skb;
1447 fr_sof(fp) = hp->fcoe_sof;
1449 /* Copy out the CRC and EOF trailer for access */
1450 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof))) {
1454 fr_eof(fp) = crc_eof.fcoe_eof;
1455 fr_crc(fp) = crc_eof.fcoe_crc32;
1456 if (pskb_trim(skb, fr_len)) {
1462 * We only check CRC if no offload is available and if it is
1463 * it's solicited data, in which case, the FCP layer would
1464 * check it during the copy.
1466 if (lp->crc_offload && skb->ip_summed == CHECKSUM_UNNECESSARY)
1467 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1469 fr_flags(fp) |= FCPHF_CRC_UNCHECKED;
1471 fh = fc_frame_header_get(fp);
1472 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA &&
1473 fh->fh_type == FC_TYPE_FCP) {
1474 fc_exch_recv(lp, fp);
1477 if (fr_flags(fp) & FCPHF_CRC_UNCHECKED) {
1478 if (le32_to_cpu(fr_crc(fp)) !=
1479 ~crc32(~0, skb->data, fr_len)) {
1480 if (stats->InvalidCRCCount < 5)
1481 printk(KERN_WARNING "fcoe: dropping "
1482 "frame with CRC error\n");
1483 stats->InvalidCRCCount++;
1484 stats->ErrorFrames++;
1488 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1490 fc_exch_recv(lp, fp);
1496 * fcoe_check_wait_queue() - attempt to clear the transmit backlog
1499 * This empties the wait_queue, dequeue the head of the wait_queue queue
1500 * and calls fcoe_start_io() for each packet, if all skb have been
1501 * transmitted, return qlen or -1 if a error occurs, then restore
1502 * wait_queue and try again later.
1504 * The wait_queue is used when the skb transmit fails. skb will go
1505 * in the wait_queue which will be emptied by the timer function or
1506 * by the next skb transmit.
1508 static void fcoe_check_wait_queue(struct fc_lport *lp, struct sk_buff *skb)
1510 struct fcoe_port *port = lport_priv(lp);
1513 spin_lock_bh(&port->fcoe_pending_queue.lock);
1516 __skb_queue_tail(&port->fcoe_pending_queue, skb);
1518 if (port->fcoe_pending_queue_active)
1520 port->fcoe_pending_queue_active = 1;
1522 while (port->fcoe_pending_queue.qlen) {
1523 /* keep qlen > 0 until fcoe_start_io succeeds */
1524 port->fcoe_pending_queue.qlen++;
1525 skb = __skb_dequeue(&port->fcoe_pending_queue);
1527 spin_unlock_bh(&port->fcoe_pending_queue.lock);
1528 rc = fcoe_start_io(skb);
1529 spin_lock_bh(&port->fcoe_pending_queue.lock);
1532 __skb_queue_head(&port->fcoe_pending_queue, skb);
1533 /* undo temporary increment above */
1534 port->fcoe_pending_queue.qlen--;
1537 /* undo temporary increment above */
1538 port->fcoe_pending_queue.qlen--;
1541 if (port->fcoe_pending_queue.qlen < FCOE_LOW_QUEUE_DEPTH)
1543 if (port->fcoe_pending_queue.qlen && !timer_pending(&port->timer))
1544 mod_timer(&port->timer, jiffies + 2);
1545 port->fcoe_pending_queue_active = 0;
1547 if (port->fcoe_pending_queue.qlen > FCOE_MAX_QUEUE_DEPTH)
1549 spin_unlock_bh(&port->fcoe_pending_queue.lock);
1554 * fcoe_dev_setup() - setup link change notification interface
1556 static void fcoe_dev_setup(void)
1558 register_netdevice_notifier(&fcoe_notifier);
1562 * fcoe_dev_cleanup() - cleanup link change notification interface
1564 static void fcoe_dev_cleanup(void)
1566 unregister_netdevice_notifier(&fcoe_notifier);
1570 * fcoe_device_notification() - netdev event notification callback
1571 * @notifier: context of the notification
1572 * @event: type of event
1573 * @ptr: fixed array for output parsed ifname
1575 * This function is called by the ethernet driver in case of link change event
1577 * Returns: 0 for success
1579 static int fcoe_device_notification(struct notifier_block *notifier,
1580 ulong event, void *ptr)
1582 struct fc_lport *lp = NULL;
1583 struct net_device *netdev = ptr;
1584 struct fcoe_interface *fcoe;
1585 struct fcoe_port *port;
1586 struct fcoe_dev_stats *stats;
1587 u32 link_possible = 1;
1591 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
1592 if (fcoe->netdev == netdev) {
1604 case NETDEV_GOING_DOWN:
1610 case NETDEV_CHANGEMTU:
1611 if (netdev->features & NETIF_F_FCOE_MTU)
1613 mfs = netdev->mtu - (sizeof(struct fcoe_hdr) +
1614 sizeof(struct fcoe_crc_eof));
1615 if (mfs >= FC_MIN_MAX_FRAME)
1616 fc_set_mfs(lp, mfs);
1618 case NETDEV_REGISTER:
1620 case NETDEV_UNREGISTER:
1621 list_del(&fcoe->list);
1622 port = lport_priv(fcoe->ctlr.lp);
1623 fcoe_interface_cleanup(fcoe);
1624 schedule_work(&port->destroy_work);
1628 FCOE_NETDEV_DBG(netdev, "Unknown event %ld "
1629 "from netdev netlink\n", event);
1631 if (link_possible && !fcoe_link_ok(lp))
1632 fcoe_ctlr_link_up(&fcoe->ctlr);
1633 else if (fcoe_ctlr_link_down(&fcoe->ctlr)) {
1634 stats = fc_lport_get_stats(lp);
1635 stats->LinkFailureCount++;
1636 fcoe_clean_pending_queue(lp);
1643 * fcoe_if_to_netdev() - parse a name buffer to get netdev
1644 * @buffer: incoming buffer to be copied
1646 * Returns: NULL or ptr to net_device
1648 static struct net_device *fcoe_if_to_netdev(const char *buffer)
1651 char ifname[IFNAMSIZ + 2];
1654 strlcpy(ifname, buffer, IFNAMSIZ);
1655 cp = ifname + strlen(ifname);
1656 while (--cp >= ifname && *cp == '\n')
1658 return dev_get_by_name(&init_net, ifname);
1664 * fcoe_destroy() - handles the destroy from sysfs
1665 * @buffer: expected to be an eth if name
1666 * @kp: associated kernel param
1668 * Returns: 0 for success
1670 static int fcoe_destroy(const char *buffer, struct kernel_param *kp)
1672 struct fcoe_interface *fcoe;
1673 struct net_device *netdev;
1676 mutex_lock(&fcoe_config_mutex);
1677 #ifdef CONFIG_FCOE_MODULE
1679 * Make sure the module has been initialized, and is not about to be
1680 * removed. Module paramter sysfs files are writable before the
1681 * module_init function is called and after module_exit.
1683 if (THIS_MODULE->state != MODULE_STATE_LIVE) {
1689 netdev = fcoe_if_to_netdev(buffer);
1696 fcoe = fcoe_hostlist_lookup_port(netdev);
1702 list_del(&fcoe->list);
1703 fcoe_interface_cleanup(fcoe);
1705 fcoe_if_destroy(fcoe->ctlr.lp);
1709 mutex_unlock(&fcoe_config_mutex);
1713 static void fcoe_destroy_work(struct work_struct *work)
1715 struct fcoe_port *port;
1717 port = container_of(work, struct fcoe_port, destroy_work);
1718 mutex_lock(&fcoe_config_mutex);
1719 fcoe_if_destroy(port->lport);
1720 mutex_unlock(&fcoe_config_mutex);
1724 * fcoe_create() - Handles the create call from sysfs
1725 * @buffer: expected to be an eth if name
1726 * @kp: associated kernel param
1728 * Returns: 0 for success
1730 static int fcoe_create(const char *buffer, struct kernel_param *kp)
1733 struct fcoe_interface *fcoe;
1734 struct fc_lport *lport;
1735 struct net_device *netdev;
1737 mutex_lock(&fcoe_config_mutex);
1738 #ifdef CONFIG_FCOE_MODULE
1740 * Make sure the module has been initialized, and is not about to be
1741 * removed. Module paramter sysfs files are writable before the
1742 * module_init function is called and after module_exit.
1744 if (THIS_MODULE->state != MODULE_STATE_LIVE) {
1751 netdev = fcoe_if_to_netdev(buffer);
1757 /* look for existing lport */
1758 if (fcoe_hostlist_lookup(netdev)) {
1763 fcoe = fcoe_interface_create(netdev);
1769 lport = fcoe_if_create(fcoe, &netdev->dev);
1770 if (IS_ERR(lport)) {
1771 printk(KERN_ERR "fcoe: Failed to create interface (%s)\n",
1774 fcoe_interface_cleanup(fcoe);
1778 /* Make this the "master" N_Port */
1779 fcoe->ctlr.lp = lport;
1781 /* add to lports list */
1782 fcoe_hostlist_add(lport);
1784 /* start FIP Discovery and FLOGI */
1785 lport->boot_time = jiffies;
1786 fc_fabric_login(lport);
1787 if (!fcoe_link_ok(lport))
1788 fcoe_ctlr_link_up(&fcoe->ctlr);
1793 * Release from init in fcoe_interface_create(), on success lport
1794 * should be holding a reference taken in fcoe_if_create().
1796 fcoe_interface_put(fcoe);
1801 mutex_unlock(&fcoe_config_mutex);
1805 module_param_call(create, fcoe_create, NULL, NULL, S_IWUSR);
1806 __MODULE_PARM_TYPE(create, "string");
1807 MODULE_PARM_DESC(create, "Create fcoe fcoe using net device passed in.");
1808 module_param_call(destroy, fcoe_destroy, NULL, NULL, S_IWUSR);
1809 __MODULE_PARM_TYPE(destroy, "string");
1810 MODULE_PARM_DESC(destroy, "Destroy fcoe fcoe");
1813 * fcoe_link_ok() - Check if link is ok for the fc_lport
1814 * @lp: ptr to the fc_lport
1816 * Any permanently-disqualifying conditions have been previously checked.
1817 * This also updates the speed setting, which may change with link for 100/1000.
1819 * This function should probably be checking for PAUSE support at some point
1820 * in the future. Currently Per-priority-pause is not determinable using
1821 * ethtool, so we shouldn't be restrictive until that problem is resolved.
1823 * Returns: 0 if link is OK for use by FCoE.
1826 int fcoe_link_ok(struct fc_lport *lp)
1828 struct fcoe_port *port = lport_priv(lp);
1829 struct net_device *dev = port->fcoe->netdev;
1830 struct ethtool_cmd ecmd = { ETHTOOL_GSET };
1832 if ((dev->flags & IFF_UP) && netif_carrier_ok(dev) &&
1833 (!dev_ethtool_get_settings(dev, &ecmd))) {
1834 lp->link_supported_speeds &=
1835 ~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
1836 if (ecmd.supported & (SUPPORTED_1000baseT_Half |
1837 SUPPORTED_1000baseT_Full))
1838 lp->link_supported_speeds |= FC_PORTSPEED_1GBIT;
1839 if (ecmd.supported & SUPPORTED_10000baseT_Full)
1840 lp->link_supported_speeds |=
1841 FC_PORTSPEED_10GBIT;
1842 if (ecmd.speed == SPEED_1000)
1843 lp->link_speed = FC_PORTSPEED_1GBIT;
1844 if (ecmd.speed == SPEED_10000)
1845 lp->link_speed = FC_PORTSPEED_10GBIT;
1853 * fcoe_percpu_clean() - Clear the pending skbs for an lport
1856 * Must be called with fcoe_create_mutex held to single-thread completion.
1858 * This flushes the pending skbs by adding a new skb to each queue and
1859 * waiting until they are all freed. This assures us that not only are
1860 * there no packets that will be handled by the lport, but also that any
1861 * threads already handling packet have returned.
1863 void fcoe_percpu_clean(struct fc_lport *lp)
1865 struct fcoe_percpu_s *pp;
1866 struct fcoe_rcv_info *fr;
1867 struct sk_buff_head *list;
1868 struct sk_buff *skb, *next;
1869 struct sk_buff *head;
1872 for_each_possible_cpu(cpu) {
1873 pp = &per_cpu(fcoe_percpu, cpu);
1874 spin_lock_bh(&pp->fcoe_rx_list.lock);
1875 list = &pp->fcoe_rx_list;
1877 for (skb = head; skb != (struct sk_buff *)list;
1880 fr = fcoe_dev_from_skb(skb);
1881 if (fr->fr_dev == lp) {
1882 __skb_unlink(skb, list);
1887 if (!pp->thread || !cpu_online(cpu)) {
1888 spin_unlock_bh(&pp->fcoe_rx_list.lock);
1892 skb = dev_alloc_skb(0);
1894 spin_unlock_bh(&pp->fcoe_rx_list.lock);
1897 skb->destructor = fcoe_percpu_flush_done;
1899 __skb_queue_tail(&pp->fcoe_rx_list, skb);
1900 if (pp->fcoe_rx_list.qlen == 1)
1901 wake_up_process(pp->thread);
1902 spin_unlock_bh(&pp->fcoe_rx_list.lock);
1904 wait_for_completion(&fcoe_flush_completion);
1909 * fcoe_clean_pending_queue() - Dequeue a skb and free it
1910 * @lp: the corresponding fc_lport
1914 void fcoe_clean_pending_queue(struct fc_lport *lp)
1916 struct fcoe_port *port = lport_priv(lp);
1917 struct sk_buff *skb;
1919 spin_lock_bh(&port->fcoe_pending_queue.lock);
1920 while ((skb = __skb_dequeue(&port->fcoe_pending_queue)) != NULL) {
1921 spin_unlock_bh(&port->fcoe_pending_queue.lock);
1923 spin_lock_bh(&port->fcoe_pending_queue.lock);
1925 spin_unlock_bh(&port->fcoe_pending_queue.lock);
1929 * fcoe_reset() - Resets the fcoe
1930 * @shost: shost the reset is from
1934 int fcoe_reset(struct Scsi_Host *shost)
1936 struct fc_lport *lport = shost_priv(shost);
1937 fc_lport_reset(lport);
1942 * fcoe_hostlist_lookup_port() - find the corresponding lport by a given device
1943 * @dev: this is currently ptr to net_device
1945 * Returns: NULL or the located fcoe_port
1946 * Locking: must be called with the RNL mutex held
1948 static struct fcoe_interface *
1949 fcoe_hostlist_lookup_port(const struct net_device *dev)
1951 struct fcoe_interface *fcoe;
1953 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
1954 if (fcoe->netdev == dev)
1961 * fcoe_hostlist_lookup() - Find the corresponding lport by netdev
1962 * @netdev: ptr to net_device
1964 * Returns: 0 for success
1965 * Locking: must be called with the RTNL mutex held
1967 static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *netdev)
1969 struct fcoe_interface *fcoe;
1971 fcoe = fcoe_hostlist_lookup_port(netdev);
1972 return (fcoe) ? fcoe->ctlr.lp : NULL;
1976 * fcoe_hostlist_add() - Add a lport to lports list
1977 * @lp: ptr to the fc_lport to be added
1979 * Returns: 0 for success
1980 * Locking: must be called with the RTNL mutex held
1982 static int fcoe_hostlist_add(const struct fc_lport *lport)
1984 struct fcoe_interface *fcoe;
1985 struct fcoe_port *port;
1987 fcoe = fcoe_hostlist_lookup_port(fcoe_netdev(lport));
1989 port = lport_priv(lport);
1991 list_add_tail(&fcoe->list, &fcoe_hostlist);
1997 * fcoe_init() - fcoe module loading initialization
1999 * Returns 0 on success, negative on failure
2001 static int __init fcoe_init(void)
2005 struct fcoe_percpu_s *p;
2007 mutex_lock(&fcoe_config_mutex);
2009 for_each_possible_cpu(cpu) {
2010 p = &per_cpu(fcoe_percpu, cpu);
2011 skb_queue_head_init(&p->fcoe_rx_list);
2014 for_each_online_cpu(cpu)
2015 fcoe_percpu_thread_create(cpu);
2017 /* Initialize per CPU interrupt thread */
2018 rc = register_hotcpu_notifier(&fcoe_cpu_notifier);
2022 /* Setup link change notification */
2025 rc = fcoe_if_init();
2029 mutex_unlock(&fcoe_config_mutex);
2033 for_each_online_cpu(cpu) {
2034 fcoe_percpu_thread_destroy(cpu);
2036 mutex_unlock(&fcoe_config_mutex);
2039 module_init(fcoe_init);
2042 * fcoe_exit() - fcoe module unloading cleanup
2044 * Returns 0 on success, negative on failure
2046 static void __exit fcoe_exit(void)
2049 struct fcoe_interface *fcoe, *tmp;
2050 struct fcoe_port *port;
2052 mutex_lock(&fcoe_config_mutex);
2056 /* releases the associated fcoe hosts */
2058 list_for_each_entry_safe(fcoe, tmp, &fcoe_hostlist, list) {
2059 list_del(&fcoe->list);
2060 port = lport_priv(fcoe->ctlr.lp);
2061 fcoe_interface_cleanup(fcoe);
2062 schedule_work(&port->destroy_work);
2066 unregister_hotcpu_notifier(&fcoe_cpu_notifier);
2068 for_each_online_cpu(cpu)
2069 fcoe_percpu_thread_destroy(cpu);
2071 mutex_unlock(&fcoe_config_mutex);
2073 /* flush any asyncronous interface destroys,
2074 * this should happen after the netdev notifier is unregistered */
2075 flush_scheduled_work();
2077 /* detach from scsi transport
2078 * must happen after all destroys are done, therefor after the flush */
2081 module_exit(fcoe_exit);
2084 * fcoe_flogi_resp() - FCoE specific FLOGI and FDISC response handler
2085 * @seq: active sequence in the FLOGI or FDISC exchange
2086 * @fp: response frame, or error encoded in a pointer (timeout)
2087 * @arg: pointer the the fcoe_ctlr structure
2089 * This handles MAC address managment for FCoE, then passes control on to
2090 * the libfc FLOGI response handler.
2092 static void fcoe_flogi_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
2094 struct fcoe_ctlr *fip = arg;
2095 struct fc_exch *exch = fc_seq_exch(seq);
2096 struct fc_lport *lport = exch->lp;
2102 mac = fr_cb(fp)->granted_mac;
2103 if (is_zero_ether_addr(mac)) {
2105 mac = eth_hdr(&fp->skb)->h_source;
2106 if (fcoe_ctlr_recv_flogi(fip, lport, fp, mac)) {
2111 /* FIP, libfcoe has already seen it */
2112 fip->update_mac(lport, fr_cb(fp)->granted_mac);
2115 fc_lport_flogi_resp(seq, fp, lport);
2119 * fcoe_logo_resp() - FCoE specific LOGO response handler
2120 * @seq: active sequence in the LOGO exchange
2121 * @fp: response frame, or error encoded in a pointer (timeout)
2122 * @arg: pointer the the fcoe_ctlr structure
2124 * This handles MAC address managment for FCoE, then passes control on to
2125 * the libfc LOGO response handler.
2127 static void fcoe_logo_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
2129 struct fcoe_ctlr *fip = arg;
2130 struct fc_exch *exch = fc_seq_exch(seq);
2131 struct fc_lport *lport = exch->lp;
2132 static u8 zero_mac[ETH_ALEN] = { 0 };
2135 fip->update_mac(lport, zero_mac);
2136 fc_lport_logo_resp(seq, fp, lport);
2140 * fcoe_elsct_send - FCoE specific ELS handler
2142 * This does special case handling of FIP encapsualted ELS exchanges for FCoE,
2143 * using FCoE specific response handlers and passing the FIP controller as
2144 * the argument (the lport is still available from the exchange).
2146 * Most of the work here is just handed off to the libfc routine.
2148 static struct fc_seq *fcoe_elsct_send(struct fc_lport *lport,
2149 u32 did, struct fc_frame *fp, unsigned int op,
2150 void (*resp)(struct fc_seq *, struct fc_frame *, void *),
2151 void *arg, u32 timeout)
2153 struct fcoe_port *port = lport_priv(lport);
2154 struct fcoe_interface *fcoe = port->fcoe;
2155 struct fcoe_ctlr *fip = &fcoe->ctlr;
2156 struct fc_frame_header *fh = fc_frame_header_get(fp);
2161 return fc_elsct_send(lport, did, fp, op, fcoe_flogi_resp,
2164 /* only hook onto fabric logouts, not port logouts */
2165 if (ntoh24(fh->fh_d_id) != FC_FID_FLOGI)
2167 return fc_elsct_send(lport, did, fp, op, fcoe_logo_resp,
2170 return fc_elsct_send(lport, did, fp, op, resp, arg, timeout);