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 an 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 an 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 an 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 an 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_link_speed_update() - Update the supported and actual link speeds
88 * @lport: The local port to update speeds for
90 * Returns: 0 if the ethtool query was successful
91 * -1 if the ethtool query failed
93 int fcoe_link_speed_update(struct fc_lport *lport)
95 struct net_device *netdev = fcoe_get_netdev(lport);
96 struct ethtool_cmd ecmd;
98 if (!__ethtool_get_settings(netdev, &ecmd)) {
99 lport->link_supported_speeds &= ~(FC_PORTSPEED_1GBIT |
100 FC_PORTSPEED_10GBIT |
101 FC_PORTSPEED_20GBIT |
102 FC_PORTSPEED_40GBIT);
104 if (ecmd.supported & (SUPPORTED_1000baseT_Half |
105 SUPPORTED_1000baseT_Full |
106 SUPPORTED_1000baseKX_Full))
107 lport->link_supported_speeds |= FC_PORTSPEED_1GBIT;
109 if (ecmd.supported & (SUPPORTED_10000baseT_Full |
110 SUPPORTED_10000baseKX4_Full |
111 SUPPORTED_10000baseKR_Full |
112 SUPPORTED_10000baseR_FEC))
113 lport->link_supported_speeds |= FC_PORTSPEED_10GBIT;
115 if (ecmd.supported & (SUPPORTED_20000baseMLD2_Full |
116 SUPPORTED_20000baseKR2_Full))
117 lport->link_supported_speeds |= FC_PORTSPEED_20GBIT;
119 if (ecmd.supported & (SUPPORTED_40000baseKR4_Full |
120 SUPPORTED_40000baseCR4_Full |
121 SUPPORTED_40000baseSR4_Full |
122 SUPPORTED_40000baseLR4_Full))
123 lport->link_supported_speeds |= FC_PORTSPEED_40GBIT;
125 switch (ethtool_cmd_speed(&ecmd)) {
127 lport->link_speed = FC_PORTSPEED_1GBIT;
130 lport->link_speed = FC_PORTSPEED_10GBIT;
133 lport->link_speed = FC_PORTSPEED_20GBIT;
136 lport->link_speed = FC_PORTSPEED_40GBIT;
139 lport->link_speed = FC_PORTSPEED_UNKNOWN;
146 EXPORT_SYMBOL_GPL(fcoe_link_speed_update);
149 * __fcoe_get_lesb() - Get the Link Error Status Block (LESB) for a given lport
150 * @lport: The local port to update speeds for
151 * @fc_lesb: Pointer to the LESB to be filled up
152 * @netdev: Pointer to the netdev that is associated with the lport
154 * Note, the Link Error Status Block (LESB) for FCoE is defined in FC-BB-6
155 * Clause 7.11 in v1.04.
157 void __fcoe_get_lesb(struct fc_lport *lport,
158 struct fc_els_lesb *fc_lesb,
159 struct net_device *netdev)
163 struct fc_stats *stats;
164 struct fcoe_fc_els_lesb *lesb;
165 struct rtnl_link_stats64 temp;
170 lesb = (struct fcoe_fc_els_lesb *)fc_lesb;
171 memset(lesb, 0, sizeof(*lesb));
172 for_each_possible_cpu(cpu) {
173 stats = per_cpu_ptr(lport->stats, cpu);
174 lfc += stats->LinkFailureCount;
175 vlfc += stats->VLinkFailureCount;
176 mdac += stats->MissDiscAdvCount;
178 lesb->lesb_link_fail = htonl(lfc);
179 lesb->lesb_vlink_fail = htonl(vlfc);
180 lesb->lesb_miss_fka = htonl(mdac);
181 lesb->lesb_fcs_error =
182 htonl(dev_get_stats(netdev, &temp)->rx_crc_errors);
184 EXPORT_SYMBOL_GPL(__fcoe_get_lesb);
187 * fcoe_get_lesb() - Fill the FCoE Link Error Status Block
188 * @lport: the local port
189 * @fc_lesb: the link error status block
191 void fcoe_get_lesb(struct fc_lport *lport,
192 struct fc_els_lesb *fc_lesb)
194 struct net_device *netdev = fcoe_get_netdev(lport);
196 __fcoe_get_lesb(lport, fc_lesb, netdev);
198 EXPORT_SYMBOL_GPL(fcoe_get_lesb);
201 * fcoe_ctlr_get_lesb() - Get the Link Error Status Block (LESB) for a given
202 * fcoe controller device
203 * @ctlr_dev: The given fcoe controller device
206 void fcoe_ctlr_get_lesb(struct fcoe_ctlr_device *ctlr_dev)
208 struct fcoe_ctlr *fip = fcoe_ctlr_device_priv(ctlr_dev);
209 struct net_device *netdev = fcoe_get_netdev(fip->lp);
210 struct fc_els_lesb *fc_lesb;
212 fc_lesb = (struct fc_els_lesb *)(&ctlr_dev->lesb);
213 __fcoe_get_lesb(fip->lp, fc_lesb, netdev);
215 EXPORT_SYMBOL_GPL(fcoe_ctlr_get_lesb);
217 void fcoe_wwn_to_str(u64 wwn, char *buf, int len)
221 u64_to_wwn(wwn, wwpn);
222 snprintf(buf, len, "%02x%02x%02x%02x%02x%02x%02x%02x",
223 wwpn[0], wwpn[1], wwpn[2], wwpn[3],
224 wwpn[4], wwpn[5], wwpn[6], wwpn[7]);
226 EXPORT_SYMBOL_GPL(fcoe_wwn_to_str);
229 * fcoe_validate_vport_create() - Validate a vport before creating it
230 * @vport: NPIV port to be created
232 * This routine is meant to add validation for a vport before creating it
233 * via fcoe_vport_create().
234 * Current validations are:
235 * - WWPN supplied is unique for given lport
237 int fcoe_validate_vport_create(struct fc_vport *vport)
239 struct Scsi_Host *shost = vport_to_shost(vport);
240 struct fc_lport *n_port = shost_priv(shost);
241 struct fc_lport *vn_port;
245 mutex_lock(&n_port->lp_mutex);
247 fcoe_wwn_to_str(vport->port_name, buf, sizeof(buf));
248 /* Check if the wwpn is not same as that of the lport */
249 if (!memcmp(&n_port->wwpn, &vport->port_name, sizeof(u64))) {
250 LIBFCOE_TRANSPORT_DBG("vport WWPN 0x%s is same as that of the "
251 "base port WWPN\n", buf);
256 /* Check if there is any existing vport with same wwpn */
257 list_for_each_entry(vn_port, &n_port->vports, list) {
258 if (!memcmp(&vn_port->wwpn, &vport->port_name, sizeof(u64))) {
259 LIBFCOE_TRANSPORT_DBG("vport with given WWPN 0x%s "
260 "already exists\n", buf);
266 mutex_unlock(&n_port->lp_mutex);
269 EXPORT_SYMBOL_GPL(fcoe_validate_vport_create);
272 * fcoe_get_wwn() - Get the world wide name from LLD if it supports it
273 * @netdev: the associated net device
274 * @wwn: the output WWN
275 * @type: the type of WWN (WWPN or WWNN)
277 * Returns: 0 for success
279 int fcoe_get_wwn(struct net_device *netdev, u64 *wwn, int type)
281 const struct net_device_ops *ops = netdev->netdev_ops;
283 if (ops->ndo_fcoe_get_wwn)
284 return ops->ndo_fcoe_get_wwn(netdev, wwn, type);
287 EXPORT_SYMBOL_GPL(fcoe_get_wwn);
290 * fcoe_fc_crc() - Calculates the CRC for a given frame
291 * @fp: The frame to be checksumed
293 * This uses crc32() routine to calculate the CRC for a frame
295 * Return: The 32 bit CRC value
297 u32 fcoe_fc_crc(struct fc_frame *fp)
299 struct sk_buff *skb = fp_skb(fp);
300 struct skb_frag_struct *frag;
302 unsigned long off, len, clen;
306 crc = crc32(~0, skb->data, skb_headlen(skb));
308 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
309 frag = &skb_shinfo(skb)->frags[i];
310 off = frag->page_offset;
311 len = skb_frag_size(frag);
313 clen = min(len, PAGE_SIZE - (off & ~PAGE_MASK));
315 skb_frag_page(frag) + (off >> PAGE_SHIFT));
316 crc = crc32(crc, data + (off & ~PAGE_MASK), clen);
324 EXPORT_SYMBOL_GPL(fcoe_fc_crc);
327 * fcoe_start_io() - Start FCoE I/O
328 * @skb: The packet to be transmitted
330 * This routine is called from the net device to start transmitting
333 * Returns: 0 for success
335 int fcoe_start_io(struct sk_buff *skb)
337 struct sk_buff *nskb;
340 nskb = skb_clone(skb, GFP_ATOMIC);
343 rc = dev_queue_xmit(nskb);
349 EXPORT_SYMBOL_GPL(fcoe_start_io);
353 * fcoe_clean_pending_queue() - Dequeue a skb and free it
354 * @lport: The local port to dequeue a skb on
356 void fcoe_clean_pending_queue(struct fc_lport *lport)
358 struct fcoe_port *port = lport_priv(lport);
361 spin_lock_bh(&port->fcoe_pending_queue.lock);
362 while ((skb = __skb_dequeue(&port->fcoe_pending_queue)) != NULL) {
363 spin_unlock_bh(&port->fcoe_pending_queue.lock);
365 spin_lock_bh(&port->fcoe_pending_queue.lock);
367 spin_unlock_bh(&port->fcoe_pending_queue.lock);
369 EXPORT_SYMBOL_GPL(fcoe_clean_pending_queue);
372 * fcoe_check_wait_queue() - Attempt to clear the transmit backlog
373 * @lport: The local port whose backlog is to be cleared
375 * This empties the wait_queue, dequeues the head of the wait_queue queue
376 * and calls fcoe_start_io() for each packet. If all skb have been
377 * transmitted it returns the qlen. If an error occurs it restores
378 * wait_queue (to try again later) and returns -1.
380 * The wait_queue is used when the skb transmit fails. The failed skb
381 * will go in the wait_queue which will be emptied by the timer function or
382 * by the next skb transmit.
384 void fcoe_check_wait_queue(struct fc_lport *lport, struct sk_buff *skb)
386 struct fcoe_port *port = lport_priv(lport);
389 spin_lock_bh(&port->fcoe_pending_queue.lock);
392 __skb_queue_tail(&port->fcoe_pending_queue, skb);
394 if (port->fcoe_pending_queue_active)
396 port->fcoe_pending_queue_active = 1;
398 while (port->fcoe_pending_queue.qlen) {
399 /* keep qlen > 0 until fcoe_start_io succeeds */
400 port->fcoe_pending_queue.qlen++;
401 skb = __skb_dequeue(&port->fcoe_pending_queue);
403 spin_unlock_bh(&port->fcoe_pending_queue.lock);
404 rc = fcoe_start_io(skb);
405 spin_lock_bh(&port->fcoe_pending_queue.lock);
408 __skb_queue_head(&port->fcoe_pending_queue, skb);
409 /* undo temporary increment above */
410 port->fcoe_pending_queue.qlen--;
413 /* undo temporary increment above */
414 port->fcoe_pending_queue.qlen--;
417 if (port->fcoe_pending_queue.qlen < port->min_queue_depth)
419 if (port->fcoe_pending_queue.qlen && !timer_pending(&port->timer))
420 mod_timer(&port->timer, jiffies + 2);
421 port->fcoe_pending_queue_active = 0;
423 if (port->fcoe_pending_queue.qlen > port->max_queue_depth)
425 spin_unlock_bh(&port->fcoe_pending_queue.lock);
427 EXPORT_SYMBOL_GPL(fcoe_check_wait_queue);
430 * fcoe_queue_timer() - The fcoe queue timer
431 * @lport: The local port
433 * Calls fcoe_check_wait_queue on timeout
435 void fcoe_queue_timer(ulong lport)
437 fcoe_check_wait_queue((struct fc_lport *)lport, NULL);
439 EXPORT_SYMBOL_GPL(fcoe_queue_timer);
442 * fcoe_get_paged_crc_eof() - Allocate a page to be used for the trailer CRC
443 * @skb: The packet to be transmitted
444 * @tlen: The total length of the trailer
445 * @fps: The fcoe context
447 * This routine allocates a page for frame trailers. The page is re-used if
448 * there is enough room left on it for the current trailer. If there isn't
449 * enough buffer left a new page is allocated for the trailer. Reference to
450 * the page from this function as well as the skbs using the page fragments
451 * ensure that the page is freed at the appropriate time.
453 * Returns: 0 for success
455 int fcoe_get_paged_crc_eof(struct sk_buff *skb, int tlen,
456 struct fcoe_percpu_s *fps)
460 page = fps->crc_eof_page;
462 page = alloc_page(GFP_ATOMIC);
466 fps->crc_eof_page = page;
467 fps->crc_eof_offset = 0;
471 skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags, page,
472 fps->crc_eof_offset, tlen);
474 skb->data_len += tlen;
475 skb->truesize += tlen;
476 fps->crc_eof_offset += sizeof(struct fcoe_crc_eof);
478 if (fps->crc_eof_offset >= PAGE_SIZE) {
479 fps->crc_eof_page = NULL;
480 fps->crc_eof_offset = 0;
486 EXPORT_SYMBOL_GPL(fcoe_get_paged_crc_eof);
489 * fcoe_transport_lookup - find an fcoe transport that matches a netdev
490 * @netdev: The netdev to look for from all attached transports
492 * Returns : ptr to the fcoe transport that supports this netdev or NULL
495 * The ft_mutex should be held when this is called
497 static struct fcoe_transport *fcoe_transport_lookup(struct net_device *netdev)
499 struct fcoe_transport *ft = NULL;
501 list_for_each_entry(ft, &fcoe_transports, list)
502 if (ft->match && ft->match(netdev))
508 * fcoe_transport_attach - Attaches an FCoE transport
509 * @ft: The fcoe transport to be attached
511 * Returns : 0 for success
513 int fcoe_transport_attach(struct fcoe_transport *ft)
517 mutex_lock(&ft_mutex);
519 LIBFCOE_TRANSPORT_DBG("transport %s already attached\n",
525 /* Add default transport to the tail */
526 if (strcmp(ft->name, FCOE_TRANSPORT_DEFAULT))
527 list_add(&ft->list, &fcoe_transports);
529 list_add_tail(&ft->list, &fcoe_transports);
532 LIBFCOE_TRANSPORT_DBG("attaching transport %s\n", ft->name);
535 mutex_unlock(&ft_mutex);
538 EXPORT_SYMBOL(fcoe_transport_attach);
541 * fcoe_transport_detach - Detaches an FCoE transport
542 * @ft: The fcoe transport to be attached
544 * Returns : 0 for success
546 int fcoe_transport_detach(struct fcoe_transport *ft)
549 struct fcoe_netdev_mapping *nm = NULL, *tmp;
551 mutex_lock(&ft_mutex);
553 LIBFCOE_TRANSPORT_DBG("transport %s already detached\n",
559 /* remove netdev mapping for this transport as it is going away */
560 mutex_lock(&fn_mutex);
561 list_for_each_entry_safe(nm, tmp, &fcoe_netdevs, list) {
563 LIBFCOE_TRANSPORT_DBG("transport %s going away, "
564 "remove its netdev mapping for %s\n",
565 ft->name, nm->netdev->name);
570 mutex_unlock(&fn_mutex);
573 ft->attached = false;
574 LIBFCOE_TRANSPORT_DBG("detaching transport %s\n", ft->name);
577 mutex_unlock(&ft_mutex);
581 EXPORT_SYMBOL(fcoe_transport_detach);
583 static int fcoe_transport_show(char *buffer, const struct kernel_param *kp)
586 struct fcoe_transport *ft = NULL;
588 i = j = sprintf(buffer, "Attached FCoE transports:");
589 mutex_lock(&ft_mutex);
590 list_for_each_entry(ft, &fcoe_transports, list) {
591 if (i >= PAGE_SIZE - IFNAMSIZ)
593 i += snprintf(&buffer[i], IFNAMSIZ, "%s ", ft->name);
595 mutex_unlock(&ft_mutex);
597 i += snprintf(&buffer[i], IFNAMSIZ, "none");
601 static int __init fcoe_transport_init(void)
603 register_netdevice_notifier(&libfcoe_notifier);
607 static int fcoe_transport_exit(void)
609 struct fcoe_transport *ft;
611 unregister_netdevice_notifier(&libfcoe_notifier);
612 mutex_lock(&ft_mutex);
613 list_for_each_entry(ft, &fcoe_transports, list)
614 printk(KERN_ERR "FCoE transport %s is still attached!\n",
616 mutex_unlock(&ft_mutex);
621 static int fcoe_add_netdev_mapping(struct net_device *netdev,
622 struct fcoe_transport *ft)
624 struct fcoe_netdev_mapping *nm;
626 nm = kmalloc(sizeof(*nm), GFP_KERNEL);
628 printk(KERN_ERR "Unable to allocate netdev_mapping");
635 mutex_lock(&fn_mutex);
636 list_add(&nm->list, &fcoe_netdevs);
637 mutex_unlock(&fn_mutex);
642 static void fcoe_del_netdev_mapping(struct net_device *netdev)
644 struct fcoe_netdev_mapping *nm = NULL, *tmp;
646 mutex_lock(&fn_mutex);
647 list_for_each_entry_safe(nm, tmp, &fcoe_netdevs, list) {
648 if (nm->netdev == netdev) {
651 mutex_unlock(&fn_mutex);
655 mutex_unlock(&fn_mutex);
660 * fcoe_netdev_map_lookup - find the fcoe transport that matches the netdev on which
663 * Returns : ptr to the fcoe transport that supports this netdev or NULL
666 * The ft_mutex should be held when this is called
668 static struct fcoe_transport *fcoe_netdev_map_lookup(struct net_device *netdev)
670 struct fcoe_transport *ft = NULL;
671 struct fcoe_netdev_mapping *nm;
673 mutex_lock(&fn_mutex);
674 list_for_each_entry(nm, &fcoe_netdevs, list) {
675 if (netdev == nm->netdev) {
677 mutex_unlock(&fn_mutex);
682 mutex_unlock(&fn_mutex);
687 * fcoe_if_to_netdev() - Parse a name buffer to get a net device
688 * @buffer: The name of the net device
690 * Returns: NULL or a ptr to net_device
692 static struct net_device *fcoe_if_to_netdev(const char *buffer)
695 char ifname[IFNAMSIZ + 2];
698 strlcpy(ifname, buffer, IFNAMSIZ);
699 cp = ifname + strlen(ifname);
700 while (--cp >= ifname && *cp == '\n')
702 return dev_get_by_name(&init_net, ifname);
708 * libfcoe_device_notification() - Handler for net device events
709 * @notifier: The context of the notification
710 * @event: The type of event
711 * @ptr: The net device that the event was on
713 * This function is called by the Ethernet driver in case of link change event.
715 * Returns: 0 for success
717 static int libfcoe_device_notification(struct notifier_block *notifier,
718 ulong event, void *ptr)
720 struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
723 case NETDEV_UNREGISTER:
724 LIBFCOE_TRANSPORT_DBG("NETDEV_UNREGISTER %s\n",
726 fcoe_del_netdev_mapping(netdev);
732 ssize_t fcoe_ctlr_create_store(struct bus_type *bus,
733 const char *buf, size_t count)
735 struct net_device *netdev = NULL;
736 struct fcoe_transport *ft = NULL;
740 mutex_lock(&ft_mutex);
742 netdev = fcoe_if_to_netdev(buf);
744 LIBFCOE_TRANSPORT_DBG("Invalid device %s.\n", buf);
749 ft = fcoe_netdev_map_lookup(netdev);
751 LIBFCOE_TRANSPORT_DBG("transport %s already has existing "
752 "FCoE instance on %s.\n",
753 ft->name, netdev->name);
758 ft = fcoe_transport_lookup(netdev);
760 LIBFCOE_TRANSPORT_DBG("no FCoE transport found for %s.\n",
766 /* pass to transport create */
767 err = ft->alloc ? ft->alloc(netdev) : -ENODEV;
769 fcoe_del_netdev_mapping(netdev);
774 err = fcoe_add_netdev_mapping(netdev, ft);
776 LIBFCOE_TRANSPORT_DBG("failed to add new netdev mapping "
777 "for FCoE transport %s for %s.\n",
778 ft->name, netdev->name);
783 LIBFCOE_TRANSPORT_DBG("transport %s succeeded to create fcoe on %s.\n",
784 ft->name, netdev->name);
789 mutex_unlock(&ft_mutex);
795 ssize_t fcoe_ctlr_destroy_store(struct bus_type *bus,
796 const char *buf, size_t count)
799 struct net_device *netdev = NULL;
800 struct fcoe_transport *ft = NULL;
802 mutex_lock(&ft_mutex);
804 netdev = fcoe_if_to_netdev(buf);
806 LIBFCOE_TRANSPORT_DBG("invalid device %s.\n", buf);
810 ft = fcoe_netdev_map_lookup(netdev);
812 LIBFCOE_TRANSPORT_DBG("no FCoE transport found for %s.\n",
817 /* pass to transport destroy */
818 rc = ft->destroy(netdev);
822 fcoe_del_netdev_mapping(netdev);
823 LIBFCOE_TRANSPORT_DBG("transport %s %s to destroy fcoe on %s.\n",
824 ft->name, (rc) ? "failed" : "succeeded",
826 rc = count; /* required for successful return */
830 mutex_unlock(&ft_mutex);
833 EXPORT_SYMBOL(fcoe_ctlr_destroy_store);
836 * fcoe_transport_create() - Create a fcoe interface
837 * @buffer: The name of the Ethernet interface to create on
838 * @kp: The associated kernel param
840 * Called from sysfs. This holds the ft_mutex while calling the
841 * registered fcoe transport's create function.
843 * Returns: 0 for success
845 static int fcoe_transport_create(const char *buffer, struct kernel_param *kp)
848 struct net_device *netdev = NULL;
849 struct fcoe_transport *ft = NULL;
850 enum fip_state fip_mode = (enum fip_state)(long)kp->arg;
852 mutex_lock(&ft_mutex);
854 netdev = fcoe_if_to_netdev(buffer);
856 LIBFCOE_TRANSPORT_DBG("Invalid device %s.\n", buffer);
860 ft = fcoe_netdev_map_lookup(netdev);
862 LIBFCOE_TRANSPORT_DBG("transport %s already has existing "
863 "FCoE instance on %s.\n",
864 ft->name, netdev->name);
869 ft = fcoe_transport_lookup(netdev);
871 LIBFCOE_TRANSPORT_DBG("no FCoE transport found for %s.\n",
876 rc = fcoe_add_netdev_mapping(netdev, ft);
878 LIBFCOE_TRANSPORT_DBG("failed to add new netdev mapping "
879 "for FCoE transport %s for %s.\n",
880 ft->name, netdev->name);
884 /* pass to transport create */
885 rc = ft->create ? ft->create(netdev, fip_mode) : -ENODEV;
887 fcoe_del_netdev_mapping(netdev);
889 LIBFCOE_TRANSPORT_DBG("transport %s %s to create fcoe on %s.\n",
890 ft->name, (rc) ? "failed" : "succeeded",
896 mutex_unlock(&ft_mutex);
901 * fcoe_transport_destroy() - Destroy a FCoE interface
902 * @buffer: The name of the Ethernet interface to be destroyed
903 * @kp: The associated kernel parameter
905 * Called from sysfs. This holds the ft_mutex while calling the
906 * registered fcoe transport's destroy function.
908 * Returns: 0 for success
910 static int fcoe_transport_destroy(const char *buffer, struct kernel_param *kp)
913 struct net_device *netdev = NULL;
914 struct fcoe_transport *ft = NULL;
916 mutex_lock(&ft_mutex);
918 netdev = fcoe_if_to_netdev(buffer);
920 LIBFCOE_TRANSPORT_DBG("invalid device %s.\n", buffer);
924 ft = fcoe_netdev_map_lookup(netdev);
926 LIBFCOE_TRANSPORT_DBG("no FCoE transport found for %s.\n",
931 /* pass to transport destroy */
932 rc = ft->destroy ? ft->destroy(netdev) : -ENODEV;
933 fcoe_del_netdev_mapping(netdev);
934 LIBFCOE_TRANSPORT_DBG("transport %s %s to destroy fcoe on %s.\n",
935 ft->name, (rc) ? "failed" : "succeeded",
941 mutex_unlock(&ft_mutex);
946 * fcoe_transport_disable() - Disables a FCoE interface
947 * @buffer: The name of the Ethernet interface to be disabled
948 * @kp: The associated kernel parameter
952 * Returns: 0 for success
954 static int fcoe_transport_disable(const char *buffer, struct kernel_param *kp)
957 struct net_device *netdev = NULL;
958 struct fcoe_transport *ft = NULL;
960 mutex_lock(&ft_mutex);
962 netdev = fcoe_if_to_netdev(buffer);
966 ft = fcoe_netdev_map_lookup(netdev);
970 rc = ft->disable ? ft->disable(netdev) : -ENODEV;
975 mutex_unlock(&ft_mutex);
980 * fcoe_transport_enable() - Enables a FCoE interface
981 * @buffer: The name of the Ethernet interface to be enabled
982 * @kp: The associated kernel parameter
986 * Returns: 0 for success
988 static int fcoe_transport_enable(const char *buffer, struct kernel_param *kp)
991 struct net_device *netdev = NULL;
992 struct fcoe_transport *ft = NULL;
994 mutex_lock(&ft_mutex);
996 netdev = fcoe_if_to_netdev(buffer);
1000 ft = fcoe_netdev_map_lookup(netdev);
1004 rc = ft->enable ? ft->enable(netdev) : -ENODEV;
1009 mutex_unlock(&ft_mutex);
1014 * libfcoe_init() - Initialization routine for libfcoe.ko
1016 static int __init libfcoe_init(void)
1020 rc = fcoe_transport_init();
1024 rc = fcoe_sysfs_setup();
1026 fcoe_transport_exit();
1030 module_init(libfcoe_init);
1033 * libfcoe_exit() - Tear down libfcoe.ko
1035 static void __exit libfcoe_exit(void)
1037 fcoe_sysfs_teardown();
1038 fcoe_transport_exit();
1040 module_exit(libfcoe_exit);