2 * Copyright(c) 2008 - 2011 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/types.h>
21 #include <linux/module.h>
22 #include <linux/kernel.h>
23 #include <linux/list.h>
24 #include <linux/netdevice.h>
25 #include <linux/errno.h>
26 #include <linux/crc32.h>
27 #include <scsi/libfcoe.h>
31 MODULE_AUTHOR("Open-FCoE.org");
32 MODULE_DESCRIPTION("FIP discovery protocol and FCoE transport for FCoE HBAs");
33 MODULE_LICENSE("GPL v2");
35 static int fcoe_transport_create(const char *, struct kernel_param *);
36 static int fcoe_transport_destroy(const char *, struct kernel_param *);
37 static int fcoe_transport_show(char *buffer, const struct kernel_param *kp);
38 static struct fcoe_transport *fcoe_transport_lookup(struct net_device *device);
39 static struct fcoe_transport *fcoe_netdev_map_lookup(struct net_device *device);
40 static int fcoe_transport_enable(const char *, struct kernel_param *);
41 static int fcoe_transport_disable(const char *, struct kernel_param *);
42 static int libfcoe_device_notification(struct notifier_block *notifier,
43 ulong event, void *ptr);
45 static LIST_HEAD(fcoe_transports);
46 static DEFINE_MUTEX(ft_mutex);
47 static LIST_HEAD(fcoe_netdevs);
48 static DEFINE_MUTEX(fn_mutex);
50 unsigned int libfcoe_debug_logging;
51 module_param_named(debug_logging, libfcoe_debug_logging, int, S_IRUGO|S_IWUSR);
52 MODULE_PARM_DESC(debug_logging, "a bit mask of logging levels");
54 module_param_call(show, NULL, fcoe_transport_show, NULL, S_IRUSR);
55 __MODULE_PARM_TYPE(show, "string");
56 MODULE_PARM_DESC(show, " Show attached FCoE transports");
58 module_param_call(create, fcoe_transport_create, NULL,
59 (void *)FIP_MODE_FABRIC, S_IWUSR);
60 __MODULE_PARM_TYPE(create, "string");
61 MODULE_PARM_DESC(create, " Creates fcoe instance on a ethernet interface");
63 module_param_call(create_vn2vn, fcoe_transport_create, NULL,
64 (void *)FIP_MODE_VN2VN, S_IWUSR);
65 __MODULE_PARM_TYPE(create_vn2vn, "string");
66 MODULE_PARM_DESC(create_vn2vn, " Creates a VN_node to VN_node FCoE instance "
67 "on an Ethernet interface");
69 module_param_call(destroy, fcoe_transport_destroy, NULL, NULL, S_IWUSR);
70 __MODULE_PARM_TYPE(destroy, "string");
71 MODULE_PARM_DESC(destroy, " Destroys fcoe instance on a ethernet interface");
73 module_param_call(enable, fcoe_transport_enable, NULL, NULL, S_IWUSR);
74 __MODULE_PARM_TYPE(enable, "string");
75 MODULE_PARM_DESC(enable, " Enables fcoe on a ethernet interface.");
77 module_param_call(disable, fcoe_transport_disable, NULL, NULL, S_IWUSR);
78 __MODULE_PARM_TYPE(disable, "string");
79 MODULE_PARM_DESC(disable, " Disables fcoe on a ethernet interface.");
81 /* notification function for packets from net device */
82 static struct notifier_block libfcoe_notifier = {
83 .notifier_call = libfcoe_device_notification,
86 void __fcoe_get_lesb(struct fc_lport *lport,
87 struct fc_els_lesb *fc_lesb,
88 struct net_device *netdev)
92 struct fcoe_dev_stats *devst;
93 struct fcoe_fc_els_lesb *lesb;
94 struct rtnl_link_stats64 temp;
99 lesb = (struct fcoe_fc_els_lesb *)fc_lesb;
100 memset(lesb, 0, sizeof(*lesb));
101 for_each_possible_cpu(cpu) {
102 devst = per_cpu_ptr(lport->dev_stats, cpu);
103 lfc += devst->LinkFailureCount;
104 vlfc += devst->VLinkFailureCount;
105 mdac += devst->MissDiscAdvCount;
107 lesb->lesb_link_fail = htonl(lfc);
108 lesb->lesb_vlink_fail = htonl(vlfc);
109 lesb->lesb_miss_fka = htonl(mdac);
110 lesb->lesb_fcs_error =
111 htonl(dev_get_stats(netdev, &temp)->rx_crc_errors);
113 EXPORT_SYMBOL_GPL(__fcoe_get_lesb);
115 void fcoe_wwn_to_str(u64 wwn, char *buf, int len)
119 u64_to_wwn(wwn, wwpn);
120 snprintf(buf, len, "%02x%02x%02x%02x%02x%02x%02x%02x",
121 wwpn[0], wwpn[1], wwpn[2], wwpn[3],
122 wwpn[4], wwpn[5], wwpn[6], wwpn[7]);
124 EXPORT_SYMBOL_GPL(fcoe_wwn_to_str);
127 * fcoe_validate_vport_create() - Validate a vport before creating it
128 * @vport: NPIV port to be created
130 * This routine is meant to add validation for a vport before creating it
131 * via fcoe_vport_create().
132 * Current validations are:
133 * - WWPN supplied is unique for given lport
135 int fcoe_validate_vport_create(struct fc_vport *vport)
137 struct Scsi_Host *shost = vport_to_shost(vport);
138 struct fc_lport *n_port = shost_priv(shost);
139 struct fc_lport *vn_port;
143 mutex_lock(&n_port->lp_mutex);
145 fcoe_wwn_to_str(vport->port_name, buf, sizeof(buf));
146 /* Check if the wwpn is not same as that of the lport */
147 if (!memcmp(&n_port->wwpn, &vport->port_name, sizeof(u64))) {
148 LIBFCOE_TRANSPORT_DBG("vport WWPN 0x%s is same as that of the "
149 "base port WWPN\n", buf);
154 /* Check if there is any existing vport with same wwpn */
155 list_for_each_entry(vn_port, &n_port->vports, list) {
156 if (!memcmp(&vn_port->wwpn, &vport->port_name, sizeof(u64))) {
157 LIBFCOE_TRANSPORT_DBG("vport with given WWPN 0x%s "
158 "already exists\n", buf);
164 mutex_unlock(&n_port->lp_mutex);
167 EXPORT_SYMBOL_GPL(fcoe_validate_vport_create);
170 * fcoe_get_wwn() - Get the world wide name from LLD if it supports it
171 * @netdev: the associated net device
172 * @wwn: the output WWN
173 * @type: the type of WWN (WWPN or WWNN)
175 * Returns: 0 for success
177 int fcoe_get_wwn(struct net_device *netdev, u64 *wwn, int type)
179 const struct net_device_ops *ops = netdev->netdev_ops;
181 if (ops->ndo_fcoe_get_wwn)
182 return ops->ndo_fcoe_get_wwn(netdev, wwn, type);
185 EXPORT_SYMBOL_GPL(fcoe_get_wwn);
188 * fcoe_fc_crc() - Calculates the CRC for a given frame
189 * @fp: The frame to be checksumed
191 * This uses crc32() routine to calculate the CRC for a frame
193 * Return: The 32 bit CRC value
195 u32 fcoe_fc_crc(struct fc_frame *fp)
197 struct sk_buff *skb = fp_skb(fp);
198 struct skb_frag_struct *frag;
200 unsigned long off, len, clen;
204 crc = crc32(~0, skb->data, skb_headlen(skb));
206 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
207 frag = &skb_shinfo(skb)->frags[i];
208 off = frag->page_offset;
209 len = skb_frag_size(frag);
211 clen = min(len, PAGE_SIZE - (off & ~PAGE_MASK));
213 skb_frag_page(frag) + (off >> PAGE_SHIFT));
214 crc = crc32(crc, data + (off & ~PAGE_MASK), clen);
222 EXPORT_SYMBOL_GPL(fcoe_fc_crc);
225 * fcoe_start_io() - Start FCoE I/O
226 * @skb: The packet to be transmitted
228 * This routine is called from the net device to start transmitting
231 * Returns: 0 for success
233 int fcoe_start_io(struct sk_buff *skb)
235 struct sk_buff *nskb;
238 nskb = skb_clone(skb, GFP_ATOMIC);
241 rc = dev_queue_xmit(nskb);
247 EXPORT_SYMBOL_GPL(fcoe_start_io);
251 * fcoe_clean_pending_queue() - Dequeue a skb and free it
252 * @lport: The local port to dequeue a skb on
254 void fcoe_clean_pending_queue(struct fc_lport *lport)
256 struct fcoe_port *port = lport_priv(lport);
259 spin_lock_bh(&port->fcoe_pending_queue.lock);
260 while ((skb = __skb_dequeue(&port->fcoe_pending_queue)) != NULL) {
261 spin_unlock_bh(&port->fcoe_pending_queue.lock);
263 spin_lock_bh(&port->fcoe_pending_queue.lock);
265 spin_unlock_bh(&port->fcoe_pending_queue.lock);
267 EXPORT_SYMBOL_GPL(fcoe_clean_pending_queue);
270 * fcoe_check_wait_queue() - Attempt to clear the transmit backlog
271 * @lport: The local port whose backlog is to be cleared
273 * This empties the wait_queue, dequeues the head of the wait_queue queue
274 * and calls fcoe_start_io() for each packet. If all skb have been
275 * transmitted it returns the qlen. If an error occurs it restores
276 * wait_queue (to try again later) and returns -1.
278 * The wait_queue is used when the skb transmit fails. The failed skb
279 * will go in the wait_queue which will be emptied by the timer function or
280 * by the next skb transmit.
282 void fcoe_check_wait_queue(struct fc_lport *lport, struct sk_buff *skb)
284 struct fcoe_port *port = lport_priv(lport);
287 spin_lock_bh(&port->fcoe_pending_queue.lock);
290 __skb_queue_tail(&port->fcoe_pending_queue, skb);
292 if (port->fcoe_pending_queue_active)
294 port->fcoe_pending_queue_active = 1;
296 while (port->fcoe_pending_queue.qlen) {
297 /* keep qlen > 0 until fcoe_start_io succeeds */
298 port->fcoe_pending_queue.qlen++;
299 skb = __skb_dequeue(&port->fcoe_pending_queue);
301 spin_unlock_bh(&port->fcoe_pending_queue.lock);
302 rc = fcoe_start_io(skb);
303 spin_lock_bh(&port->fcoe_pending_queue.lock);
306 __skb_queue_head(&port->fcoe_pending_queue, skb);
307 /* undo temporary increment above */
308 port->fcoe_pending_queue.qlen--;
311 /* undo temporary increment above */
312 port->fcoe_pending_queue.qlen--;
315 if (port->fcoe_pending_queue.qlen < port->min_queue_depth)
317 if (port->fcoe_pending_queue.qlen && !timer_pending(&port->timer))
318 mod_timer(&port->timer, jiffies + 2);
319 port->fcoe_pending_queue_active = 0;
321 if (port->fcoe_pending_queue.qlen > port->max_queue_depth)
323 spin_unlock_bh(&port->fcoe_pending_queue.lock);
325 EXPORT_SYMBOL_GPL(fcoe_check_wait_queue);
328 * fcoe_queue_timer() - The fcoe queue timer
329 * @lport: The local port
331 * Calls fcoe_check_wait_queue on timeout
333 void fcoe_queue_timer(ulong lport)
335 fcoe_check_wait_queue((struct fc_lport *)lport, NULL);
337 EXPORT_SYMBOL_GPL(fcoe_queue_timer);
340 * fcoe_get_paged_crc_eof() - Allocate a page to be used for the trailer CRC
341 * @skb: The packet to be transmitted
342 * @tlen: The total length of the trailer
343 * @fps: The fcoe context
345 * This routine allocates a page for frame trailers. The page is re-used if
346 * there is enough room left on it for the current trailer. If there isn't
347 * enough buffer left a new page is allocated for the trailer. Reference to
348 * the page from this function as well as the skbs using the page fragments
349 * ensure that the page is freed at the appropriate time.
351 * Returns: 0 for success
353 int fcoe_get_paged_crc_eof(struct sk_buff *skb, int tlen,
354 struct fcoe_percpu_s *fps)
358 page = fps->crc_eof_page;
360 page = alloc_page(GFP_ATOMIC);
364 fps->crc_eof_page = page;
365 fps->crc_eof_offset = 0;
369 skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags, page,
370 fps->crc_eof_offset, tlen);
372 skb->data_len += tlen;
373 skb->truesize += tlen;
374 fps->crc_eof_offset += sizeof(struct fcoe_crc_eof);
376 if (fps->crc_eof_offset >= PAGE_SIZE) {
377 fps->crc_eof_page = NULL;
378 fps->crc_eof_offset = 0;
384 EXPORT_SYMBOL_GPL(fcoe_get_paged_crc_eof);
387 * fcoe_transport_lookup - find an fcoe transport that matches a netdev
388 * @netdev: The netdev to look for from all attached transports
390 * Returns : ptr to the fcoe transport that supports this netdev or NULL
393 * The ft_mutex should be held when this is called
395 static struct fcoe_transport *fcoe_transport_lookup(struct net_device *netdev)
397 struct fcoe_transport *ft = NULL;
399 list_for_each_entry(ft, &fcoe_transports, list)
400 if (ft->match && ft->match(netdev))
406 * fcoe_transport_attach - Attaches an FCoE transport
407 * @ft: The fcoe transport to be attached
409 * Returns : 0 for success
411 int fcoe_transport_attach(struct fcoe_transport *ft)
415 mutex_lock(&ft_mutex);
417 LIBFCOE_TRANSPORT_DBG("transport %s already attached\n",
423 /* Add default transport to the tail */
424 if (strcmp(ft->name, FCOE_TRANSPORT_DEFAULT))
425 list_add(&ft->list, &fcoe_transports);
427 list_add_tail(&ft->list, &fcoe_transports);
430 LIBFCOE_TRANSPORT_DBG("attaching transport %s\n", ft->name);
433 mutex_unlock(&ft_mutex);
436 EXPORT_SYMBOL(fcoe_transport_attach);
439 * fcoe_transport_detach - Detaches an FCoE transport
440 * @ft: The fcoe transport to be attached
442 * Returns : 0 for success
444 int fcoe_transport_detach(struct fcoe_transport *ft)
447 struct fcoe_netdev_mapping *nm = NULL, *tmp;
449 mutex_lock(&ft_mutex);
451 LIBFCOE_TRANSPORT_DBG("transport %s already detached\n",
457 /* remove netdev mapping for this transport as it is going away */
458 mutex_lock(&fn_mutex);
459 list_for_each_entry_safe(nm, tmp, &fcoe_netdevs, list) {
461 LIBFCOE_TRANSPORT_DBG("transport %s going away, "
462 "remove its netdev mapping for %s\n",
463 ft->name, nm->netdev->name);
468 mutex_unlock(&fn_mutex);
471 ft->attached = false;
472 LIBFCOE_TRANSPORT_DBG("detaching transport %s\n", ft->name);
475 mutex_unlock(&ft_mutex);
479 EXPORT_SYMBOL(fcoe_transport_detach);
481 static int fcoe_transport_show(char *buffer, const struct kernel_param *kp)
484 struct fcoe_transport *ft = NULL;
486 i = j = sprintf(buffer, "Attached FCoE transports:");
487 mutex_lock(&ft_mutex);
488 list_for_each_entry(ft, &fcoe_transports, list) {
489 if (i >= PAGE_SIZE - IFNAMSIZ)
491 i += snprintf(&buffer[i], IFNAMSIZ, "%s ", ft->name);
493 mutex_unlock(&ft_mutex);
495 i += snprintf(&buffer[i], IFNAMSIZ, "none");
499 static int __init fcoe_transport_init(void)
501 register_netdevice_notifier(&libfcoe_notifier);
505 static int __exit fcoe_transport_exit(void)
507 struct fcoe_transport *ft;
509 unregister_netdevice_notifier(&libfcoe_notifier);
510 mutex_lock(&ft_mutex);
511 list_for_each_entry(ft, &fcoe_transports, list)
512 printk(KERN_ERR "FCoE transport %s is still attached!\n",
514 mutex_unlock(&ft_mutex);
519 static int fcoe_add_netdev_mapping(struct net_device *netdev,
520 struct fcoe_transport *ft)
522 struct fcoe_netdev_mapping *nm;
524 nm = kmalloc(sizeof(*nm), GFP_KERNEL);
526 printk(KERN_ERR "Unable to allocate netdev_mapping");
533 mutex_lock(&fn_mutex);
534 list_add(&nm->list, &fcoe_netdevs);
535 mutex_unlock(&fn_mutex);
540 static void fcoe_del_netdev_mapping(struct net_device *netdev)
542 struct fcoe_netdev_mapping *nm = NULL, *tmp;
544 mutex_lock(&fn_mutex);
545 list_for_each_entry_safe(nm, tmp, &fcoe_netdevs, list) {
546 if (nm->netdev == netdev) {
549 mutex_unlock(&fn_mutex);
553 mutex_unlock(&fn_mutex);
558 * fcoe_netdev_map_lookup - find the fcoe transport that matches the netdev on which
561 * Returns : ptr to the fcoe transport that supports this netdev or NULL
564 * The ft_mutex should be held when this is called
566 static struct fcoe_transport *fcoe_netdev_map_lookup(struct net_device *netdev)
568 struct fcoe_transport *ft = NULL;
569 struct fcoe_netdev_mapping *nm;
571 mutex_lock(&fn_mutex);
572 list_for_each_entry(nm, &fcoe_netdevs, list) {
573 if (netdev == nm->netdev) {
575 mutex_unlock(&fn_mutex);
580 mutex_unlock(&fn_mutex);
585 * fcoe_if_to_netdev() - Parse a name buffer to get a net device
586 * @buffer: The name of the net device
588 * Returns: NULL or a ptr to net_device
590 static struct net_device *fcoe_if_to_netdev(const char *buffer)
593 char ifname[IFNAMSIZ + 2];
596 strlcpy(ifname, buffer, IFNAMSIZ);
597 cp = ifname + strlen(ifname);
598 while (--cp >= ifname && *cp == '\n')
600 return dev_get_by_name(&init_net, ifname);
606 * libfcoe_device_notification() - Handler for net device events
607 * @notifier: The context of the notification
608 * @event: The type of event
609 * @ptr: The net device that the event was on
611 * This function is called by the Ethernet driver in case of link change event.
613 * Returns: 0 for success
615 static int libfcoe_device_notification(struct notifier_block *notifier,
616 ulong event, void *ptr)
618 struct net_device *netdev = ptr;
621 case NETDEV_UNREGISTER:
622 LIBFCOE_TRANSPORT_DBG("NETDEV_UNREGISTER %s\n",
624 fcoe_del_netdev_mapping(netdev);
632 * fcoe_transport_create() - Create a fcoe interface
633 * @buffer: The name of the Ethernet interface to create on
634 * @kp: The associated kernel param
636 * Called from sysfs. This holds the ft_mutex while calling the
637 * registered fcoe transport's create function.
639 * Returns: 0 for success
641 static int fcoe_transport_create(const char *buffer, struct kernel_param *kp)
644 struct net_device *netdev = NULL;
645 struct fcoe_transport *ft = NULL;
646 enum fip_state fip_mode = (enum fip_state)(long)kp->arg;
648 mutex_lock(&ft_mutex);
650 netdev = fcoe_if_to_netdev(buffer);
652 LIBFCOE_TRANSPORT_DBG("Invalid device %s.\n", buffer);
656 ft = fcoe_netdev_map_lookup(netdev);
658 LIBFCOE_TRANSPORT_DBG("transport %s already has existing "
659 "FCoE instance on %s.\n",
660 ft->name, netdev->name);
665 ft = fcoe_transport_lookup(netdev);
667 LIBFCOE_TRANSPORT_DBG("no FCoE transport found for %s.\n",
672 rc = fcoe_add_netdev_mapping(netdev, ft);
674 LIBFCOE_TRANSPORT_DBG("failed to add new netdev mapping "
675 "for FCoE transport %s for %s.\n",
676 ft->name, netdev->name);
680 /* pass to transport create */
681 rc = ft->create ? ft->create(netdev, fip_mode) : -ENODEV;
683 fcoe_del_netdev_mapping(netdev);
685 LIBFCOE_TRANSPORT_DBG("transport %s %s to create fcoe on %s.\n",
686 ft->name, (rc) ? "failed" : "succeeded",
692 mutex_unlock(&ft_mutex);
697 * fcoe_transport_destroy() - Destroy a FCoE interface
698 * @buffer: The name of the Ethernet interface to be destroyed
699 * @kp: The associated kernel parameter
701 * Called from sysfs. This holds the ft_mutex while calling the
702 * registered fcoe transport's destroy function.
704 * Returns: 0 for success
706 static int fcoe_transport_destroy(const char *buffer, struct kernel_param *kp)
709 struct net_device *netdev = NULL;
710 struct fcoe_transport *ft = NULL;
712 mutex_lock(&ft_mutex);
714 netdev = fcoe_if_to_netdev(buffer);
716 LIBFCOE_TRANSPORT_DBG("invalid device %s.\n", buffer);
720 ft = fcoe_netdev_map_lookup(netdev);
722 LIBFCOE_TRANSPORT_DBG("no FCoE transport found for %s.\n",
727 /* pass to transport destroy */
728 rc = ft->destroy ? ft->destroy(netdev) : -ENODEV;
729 fcoe_del_netdev_mapping(netdev);
730 LIBFCOE_TRANSPORT_DBG("transport %s %s to destroy fcoe on %s.\n",
731 ft->name, (rc) ? "failed" : "succeeded",
737 mutex_unlock(&ft_mutex);
742 * fcoe_transport_disable() - Disables a FCoE interface
743 * @buffer: The name of the Ethernet interface to be disabled
744 * @kp: The associated kernel parameter
748 * Returns: 0 for success
750 static int fcoe_transport_disable(const char *buffer, struct kernel_param *kp)
753 struct net_device *netdev = NULL;
754 struct fcoe_transport *ft = NULL;
756 mutex_lock(&ft_mutex);
758 netdev = fcoe_if_to_netdev(buffer);
762 ft = fcoe_netdev_map_lookup(netdev);
766 rc = ft->disable ? ft->disable(netdev) : -ENODEV;
771 mutex_unlock(&ft_mutex);
773 if (rc == -ERESTARTSYS)
774 return restart_syscall();
780 * fcoe_transport_enable() - Enables a FCoE interface
781 * @buffer: The name of the Ethernet interface to be enabled
782 * @kp: The associated kernel parameter
786 * Returns: 0 for success
788 static int fcoe_transport_enable(const char *buffer, struct kernel_param *kp)
791 struct net_device *netdev = NULL;
792 struct fcoe_transport *ft = NULL;
794 mutex_lock(&ft_mutex);
796 netdev = fcoe_if_to_netdev(buffer);
800 ft = fcoe_netdev_map_lookup(netdev);
804 rc = ft->enable ? ft->enable(netdev) : -ENODEV;
809 mutex_unlock(&ft_mutex);
814 * libfcoe_init() - Initialization routine for libfcoe.ko
816 static int __init libfcoe_init(void)
820 rc = fcoe_transport_init();
824 rc = fcoe_sysfs_setup();
826 fcoe_transport_exit();
830 module_init(libfcoe_init);
833 * libfcoe_exit() - Tear down libfcoe.ko
835 static void __exit libfcoe_exit(void)
837 fcoe_sysfs_teardown();
838 fcoe_transport_exit();
840 module_exit(libfcoe_exit);