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,
87 * fcoe_fc_crc() - Calculates the CRC for a given frame
88 * @fp: The frame to be checksumed
90 * This uses crc32() routine to calculate the CRC for a frame
92 * Return: The 32 bit CRC value
94 u32 fcoe_fc_crc(struct fc_frame *fp)
96 struct sk_buff *skb = fp_skb(fp);
97 struct skb_frag_struct *frag;
99 unsigned long off, len, clen;
103 crc = crc32(~0, skb->data, skb_headlen(skb));
105 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
106 frag = &skb_shinfo(skb)->frags[i];
107 off = frag->page_offset;
110 clen = min(len, PAGE_SIZE - (off & ~PAGE_MASK));
112 skb_frag_page(frag) + (off >> PAGE_SHIFT),
113 KM_SKB_DATA_SOFTIRQ);
114 crc = crc32(crc, data + (off & ~PAGE_MASK), clen);
115 kunmap_atomic(data, KM_SKB_DATA_SOFTIRQ);
122 EXPORT_SYMBOL_GPL(fcoe_fc_crc);
125 * fcoe_start_io() - Start FCoE I/O
126 * @skb: The packet to be transmitted
128 * This routine is called from the net device to start transmitting
131 * Returns: 0 for success
133 int fcoe_start_io(struct sk_buff *skb)
135 struct sk_buff *nskb;
138 nskb = skb_clone(skb, GFP_ATOMIC);
141 rc = dev_queue_xmit(nskb);
147 EXPORT_SYMBOL_GPL(fcoe_start_io);
151 * fcoe_clean_pending_queue() - Dequeue a skb and free it
152 * @lport: The local port to dequeue a skb on
154 void fcoe_clean_pending_queue(struct fc_lport *lport)
156 struct fcoe_port *port = lport_priv(lport);
159 spin_lock_bh(&port->fcoe_pending_queue.lock);
160 while ((skb = __skb_dequeue(&port->fcoe_pending_queue)) != NULL) {
161 spin_unlock_bh(&port->fcoe_pending_queue.lock);
163 spin_lock_bh(&port->fcoe_pending_queue.lock);
165 spin_unlock_bh(&port->fcoe_pending_queue.lock);
167 EXPORT_SYMBOL_GPL(fcoe_clean_pending_queue);
170 * fcoe_check_wait_queue() - Attempt to clear the transmit backlog
171 * @lport: The local port whose backlog is to be cleared
173 * This empties the wait_queue, dequeues the head of the wait_queue queue
174 * and calls fcoe_start_io() for each packet. If all skb have been
175 * transmitted it returns the qlen. If an error occurs it restores
176 * wait_queue (to try again later) and returns -1.
178 * The wait_queue is used when the skb transmit fails. The failed skb
179 * will go in the wait_queue which will be emptied by the timer function or
180 * by the next skb transmit.
182 void fcoe_check_wait_queue(struct fc_lport *lport, struct sk_buff *skb)
184 struct fcoe_port *port = lport_priv(lport);
187 spin_lock_bh(&port->fcoe_pending_queue.lock);
190 __skb_queue_tail(&port->fcoe_pending_queue, skb);
192 if (port->fcoe_pending_queue_active)
194 port->fcoe_pending_queue_active = 1;
196 while (port->fcoe_pending_queue.qlen) {
197 /* keep qlen > 0 until fcoe_start_io succeeds */
198 port->fcoe_pending_queue.qlen++;
199 skb = __skb_dequeue(&port->fcoe_pending_queue);
201 spin_unlock_bh(&port->fcoe_pending_queue.lock);
202 rc = fcoe_start_io(skb);
203 spin_lock_bh(&port->fcoe_pending_queue.lock);
206 __skb_queue_head(&port->fcoe_pending_queue, skb);
207 /* undo temporary increment above */
208 port->fcoe_pending_queue.qlen--;
211 /* undo temporary increment above */
212 port->fcoe_pending_queue.qlen--;
215 if (port->fcoe_pending_queue.qlen < port->min_queue_depth)
217 if (port->fcoe_pending_queue.qlen && !timer_pending(&port->timer))
218 mod_timer(&port->timer, jiffies + 2);
219 port->fcoe_pending_queue_active = 0;
221 if (port->fcoe_pending_queue.qlen > port->max_queue_depth)
223 spin_unlock_bh(&port->fcoe_pending_queue.lock);
225 EXPORT_SYMBOL_GPL(fcoe_check_wait_queue);
228 * fcoe_queue_timer() - The fcoe queue timer
229 * @lport: The local port
231 * Calls fcoe_check_wait_queue on timeout
233 void fcoe_queue_timer(ulong lport)
235 fcoe_check_wait_queue((struct fc_lport *)lport, NULL);
237 EXPORT_SYMBOL_GPL(fcoe_queue_timer);
240 * fcoe_get_paged_crc_eof() - Allocate a page to be used for the trailer CRC
241 * @skb: The packet to be transmitted
242 * @tlen: The total length of the trailer
243 * @fps: The fcoe context
245 * This routine allocates a page for frame trailers. The page is re-used if
246 * there is enough room left on it for the current trailer. If there isn't
247 * enough buffer left a new page is allocated for the trailer. Reference to
248 * the page from this function as well as the skbs using the page fragments
249 * ensure that the page is freed at the appropriate time.
251 * Returns: 0 for success
253 int fcoe_get_paged_crc_eof(struct sk_buff *skb, int tlen,
254 struct fcoe_percpu_s *fps)
258 page = fps->crc_eof_page;
260 page = alloc_page(GFP_ATOMIC);
264 fps->crc_eof_page = page;
265 fps->crc_eof_offset = 0;
269 skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags, page,
270 fps->crc_eof_offset, tlen);
272 skb->data_len += tlen;
273 skb->truesize += tlen;
274 fps->crc_eof_offset += sizeof(struct fcoe_crc_eof);
276 if (fps->crc_eof_offset >= PAGE_SIZE) {
277 fps->crc_eof_page = NULL;
278 fps->crc_eof_offset = 0;
284 EXPORT_SYMBOL_GPL(fcoe_get_paged_crc_eof);
287 * fcoe_transport_lookup - find an fcoe transport that matches a netdev
288 * @netdev: The netdev to look for from all attached transports
290 * Returns : ptr to the fcoe transport that supports this netdev or NULL
293 * The ft_mutex should be held when this is called
295 static struct fcoe_transport *fcoe_transport_lookup(struct net_device *netdev)
297 struct fcoe_transport *ft = NULL;
299 list_for_each_entry(ft, &fcoe_transports, list)
300 if (ft->match && ft->match(netdev))
306 * fcoe_transport_attach - Attaches an FCoE transport
307 * @ft: The fcoe transport to be attached
309 * Returns : 0 for success
311 int fcoe_transport_attach(struct fcoe_transport *ft)
315 mutex_lock(&ft_mutex);
317 LIBFCOE_TRANSPORT_DBG("transport %s already attached\n",
323 /* Add default transport to the tail */
324 if (strcmp(ft->name, FCOE_TRANSPORT_DEFAULT))
325 list_add(&ft->list, &fcoe_transports);
327 list_add_tail(&ft->list, &fcoe_transports);
330 LIBFCOE_TRANSPORT_DBG("attaching transport %s\n", ft->name);
333 mutex_unlock(&ft_mutex);
336 EXPORT_SYMBOL(fcoe_transport_attach);
339 * fcoe_transport_detach - Detaches an FCoE transport
340 * @ft: The fcoe transport to be attached
342 * Returns : 0 for success
344 int fcoe_transport_detach(struct fcoe_transport *ft)
347 struct fcoe_netdev_mapping *nm = NULL, *tmp;
349 mutex_lock(&ft_mutex);
351 LIBFCOE_TRANSPORT_DBG("transport %s already detached\n",
357 /* remove netdev mapping for this transport as it is going away */
358 mutex_lock(&fn_mutex);
359 list_for_each_entry_safe(nm, tmp, &fcoe_netdevs, list) {
361 LIBFCOE_TRANSPORT_DBG("transport %s going away, "
362 "remove its netdev mapping for %s\n",
363 ft->name, nm->netdev->name);
368 mutex_unlock(&fn_mutex);
371 ft->attached = false;
372 LIBFCOE_TRANSPORT_DBG("detaching transport %s\n", ft->name);
375 mutex_unlock(&ft_mutex);
379 EXPORT_SYMBOL(fcoe_transport_detach);
381 static int fcoe_transport_show(char *buffer, const struct kernel_param *kp)
384 struct fcoe_transport *ft = NULL;
386 i = j = sprintf(buffer, "Attached FCoE transports:");
387 mutex_lock(&ft_mutex);
388 list_for_each_entry(ft, &fcoe_transports, list) {
389 if (i >= PAGE_SIZE - IFNAMSIZ)
391 i += snprintf(&buffer[i], IFNAMSIZ, "%s ", ft->name);
393 mutex_unlock(&ft_mutex);
395 i += snprintf(&buffer[i], IFNAMSIZ, "none");
399 static int __init fcoe_transport_init(void)
401 register_netdevice_notifier(&libfcoe_notifier);
405 static int __exit fcoe_transport_exit(void)
407 struct fcoe_transport *ft;
409 unregister_netdevice_notifier(&libfcoe_notifier);
410 mutex_lock(&ft_mutex);
411 list_for_each_entry(ft, &fcoe_transports, list)
412 printk(KERN_ERR "FCoE transport %s is still attached!\n",
414 mutex_unlock(&ft_mutex);
419 static int fcoe_add_netdev_mapping(struct net_device *netdev,
420 struct fcoe_transport *ft)
422 struct fcoe_netdev_mapping *nm;
424 nm = kmalloc(sizeof(*nm), GFP_KERNEL);
426 printk(KERN_ERR "Unable to allocate netdev_mapping");
433 mutex_lock(&fn_mutex);
434 list_add(&nm->list, &fcoe_netdevs);
435 mutex_unlock(&fn_mutex);
440 static void fcoe_del_netdev_mapping(struct net_device *netdev)
442 struct fcoe_netdev_mapping *nm = NULL, *tmp;
444 mutex_lock(&fn_mutex);
445 list_for_each_entry_safe(nm, tmp, &fcoe_netdevs, list) {
446 if (nm->netdev == netdev) {
449 mutex_unlock(&fn_mutex);
453 mutex_unlock(&fn_mutex);
458 * fcoe_netdev_map_lookup - find the fcoe transport that matches the netdev on which
461 * Returns : ptr to the fcoe transport that supports this netdev or NULL
464 * The ft_mutex should be held when this is called
466 static struct fcoe_transport *fcoe_netdev_map_lookup(struct net_device *netdev)
468 struct fcoe_transport *ft = NULL;
469 struct fcoe_netdev_mapping *nm;
471 mutex_lock(&fn_mutex);
472 list_for_each_entry(nm, &fcoe_netdevs, list) {
473 if (netdev == nm->netdev) {
475 mutex_unlock(&fn_mutex);
480 mutex_unlock(&fn_mutex);
485 * fcoe_if_to_netdev() - Parse a name buffer to get a net device
486 * @buffer: The name of the net device
488 * Returns: NULL or a ptr to net_device
490 static struct net_device *fcoe_if_to_netdev(const char *buffer)
493 char ifname[IFNAMSIZ + 2];
496 strlcpy(ifname, buffer, IFNAMSIZ);
497 cp = ifname + strlen(ifname);
498 while (--cp >= ifname && *cp == '\n')
500 return dev_get_by_name(&init_net, ifname);
506 * libfcoe_device_notification() - Handler for net device events
507 * @notifier: The context of the notification
508 * @event: The type of event
509 * @ptr: The net device that the event was on
511 * This function is called by the Ethernet driver in case of link change event.
513 * Returns: 0 for success
515 static int libfcoe_device_notification(struct notifier_block *notifier,
516 ulong event, void *ptr)
518 struct net_device *netdev = ptr;
521 case NETDEV_UNREGISTER:
522 printk(KERN_ERR "libfcoe_device_notification: NETDEV_UNREGISTER %s\n",
524 fcoe_del_netdev_mapping(netdev);
532 * fcoe_transport_create() - Create a fcoe interface
533 * @buffer: The name of the Ethernet interface to create on
534 * @kp: The associated kernel param
536 * Called from sysfs. This holds the ft_mutex while calling the
537 * registered fcoe transport's create function.
539 * Returns: 0 for success
541 static int fcoe_transport_create(const char *buffer, struct kernel_param *kp)
544 struct net_device *netdev = NULL;
545 struct fcoe_transport *ft = NULL;
546 enum fip_state fip_mode = (enum fip_state)(long)kp->arg;
548 mutex_lock(&ft_mutex);
550 netdev = fcoe_if_to_netdev(buffer);
552 LIBFCOE_TRANSPORT_DBG("Invalid device %s.\n", buffer);
556 ft = fcoe_netdev_map_lookup(netdev);
558 LIBFCOE_TRANSPORT_DBG("transport %s already has existing "
559 "FCoE instance on %s.\n",
560 ft->name, netdev->name);
565 ft = fcoe_transport_lookup(netdev);
567 LIBFCOE_TRANSPORT_DBG("no FCoE transport found for %s.\n",
572 rc = fcoe_add_netdev_mapping(netdev, ft);
574 LIBFCOE_TRANSPORT_DBG("failed to add new netdev mapping "
575 "for FCoE transport %s for %s.\n",
576 ft->name, netdev->name);
580 /* pass to transport create */
581 rc = ft->create ? ft->create(netdev, fip_mode) : -ENODEV;
583 fcoe_del_netdev_mapping(netdev);
585 LIBFCOE_TRANSPORT_DBG("transport %s %s to create fcoe on %s.\n",
586 ft->name, (rc) ? "failed" : "succeeded",
592 mutex_unlock(&ft_mutex);
597 * fcoe_transport_destroy() - Destroy a FCoE interface
598 * @buffer: The name of the Ethernet interface to be destroyed
599 * @kp: The associated kernel parameter
601 * Called from sysfs. This holds the ft_mutex while calling the
602 * registered fcoe transport's destroy function.
604 * Returns: 0 for success
606 static int fcoe_transport_destroy(const char *buffer, struct kernel_param *kp)
609 struct net_device *netdev = NULL;
610 struct fcoe_transport *ft = NULL;
612 mutex_lock(&ft_mutex);
614 netdev = fcoe_if_to_netdev(buffer);
616 LIBFCOE_TRANSPORT_DBG("invalid device %s.\n", buffer);
620 ft = fcoe_netdev_map_lookup(netdev);
622 LIBFCOE_TRANSPORT_DBG("no FCoE transport found for %s.\n",
627 /* pass to transport destroy */
628 rc = ft->destroy ? ft->destroy(netdev) : -ENODEV;
629 fcoe_del_netdev_mapping(netdev);
630 LIBFCOE_TRANSPORT_DBG("transport %s %s to destroy fcoe on %s.\n",
631 ft->name, (rc) ? "failed" : "succeeded",
637 mutex_unlock(&ft_mutex);
642 * fcoe_transport_disable() - Disables a FCoE interface
643 * @buffer: The name of the Ethernet interface to be disabled
644 * @kp: The associated kernel parameter
648 * Returns: 0 for success
650 static int fcoe_transport_disable(const char *buffer, struct kernel_param *kp)
653 struct net_device *netdev = NULL;
654 struct fcoe_transport *ft = NULL;
656 mutex_lock(&ft_mutex);
658 netdev = fcoe_if_to_netdev(buffer);
662 ft = fcoe_netdev_map_lookup(netdev);
666 rc = ft->disable ? ft->disable(netdev) : -ENODEV;
671 mutex_unlock(&ft_mutex);
673 if (rc == -ERESTARTSYS)
674 return restart_syscall();
680 * fcoe_transport_enable() - Enables a FCoE interface
681 * @buffer: The name of the Ethernet interface to be enabled
682 * @kp: The associated kernel parameter
686 * Returns: 0 for success
688 static int fcoe_transport_enable(const char *buffer, struct kernel_param *kp)
691 struct net_device *netdev = NULL;
692 struct fcoe_transport *ft = NULL;
694 mutex_lock(&ft_mutex);
696 netdev = fcoe_if_to_netdev(buffer);
700 ft = fcoe_netdev_map_lookup(netdev);
704 rc = ft->enable ? ft->enable(netdev) : -ENODEV;
709 mutex_unlock(&ft_mutex);
714 * libfcoe_init() - Initialization routine for libfcoe.ko
716 static int __init libfcoe_init(void)
718 fcoe_transport_init();
722 module_init(libfcoe_init);
725 * libfcoe_exit() - Tear down libfcoe.ko
727 static void __exit libfcoe_exit(void)
729 fcoe_transport_exit();
731 module_exit(libfcoe_exit);