1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
5 * Maintained at www.Open-FCoE.org
8 #include <linux/types.h>
9 #include <linux/module.h>
10 #include <linux/kernel.h>
11 #include <linux/list.h>
12 #include <linux/netdevice.h>
13 #include <linux/errno.h>
14 #include <linux/crc32.h>
15 #include <scsi/libfcoe.h>
19 MODULE_AUTHOR("Open-FCoE.org");
20 MODULE_DESCRIPTION("FIP discovery protocol and FCoE transport for FCoE HBAs");
21 MODULE_LICENSE("GPL v2");
23 static int fcoe_transport_create(const char *, const struct kernel_param *);
24 static int fcoe_transport_destroy(const char *, const struct kernel_param *);
25 static int fcoe_transport_show(char *buffer, const struct kernel_param *kp);
26 static struct fcoe_transport *fcoe_transport_lookup(struct net_device *device);
27 static struct fcoe_transport *fcoe_netdev_map_lookup(struct net_device *device);
28 static int fcoe_transport_enable(const char *, const struct kernel_param *);
29 static int fcoe_transport_disable(const char *, const struct kernel_param *);
30 static int libfcoe_device_notification(struct notifier_block *notifier,
31 ulong event, void *ptr);
33 static LIST_HEAD(fcoe_transports);
34 static DEFINE_MUTEX(ft_mutex);
35 static LIST_HEAD(fcoe_netdevs);
36 static DEFINE_MUTEX(fn_mutex);
38 unsigned int libfcoe_debug_logging;
39 module_param_named(debug_logging, libfcoe_debug_logging, int, S_IRUGO|S_IWUSR);
40 MODULE_PARM_DESC(debug_logging, "a bit mask of logging levels");
42 module_param_call(show, NULL, fcoe_transport_show, NULL, S_IRUSR);
43 __MODULE_PARM_TYPE(show, "string");
44 MODULE_PARM_DESC(show, " Show attached FCoE transports");
46 module_param_call(create, fcoe_transport_create, NULL,
47 (void *)FIP_MODE_FABRIC, S_IWUSR);
48 __MODULE_PARM_TYPE(create, "string");
49 MODULE_PARM_DESC(create, " Creates fcoe instance on an ethernet interface");
51 module_param_call(create_vn2vn, fcoe_transport_create, NULL,
52 (void *)FIP_MODE_VN2VN, S_IWUSR);
53 __MODULE_PARM_TYPE(create_vn2vn, "string");
54 MODULE_PARM_DESC(create_vn2vn, " Creates a VN_node to VN_node FCoE instance "
55 "on an Ethernet interface");
57 module_param_call(destroy, fcoe_transport_destroy, NULL, NULL, S_IWUSR);
58 __MODULE_PARM_TYPE(destroy, "string");
59 MODULE_PARM_DESC(destroy, " Destroys fcoe instance on an ethernet interface");
61 module_param_call(enable, fcoe_transport_enable, NULL, NULL, S_IWUSR);
62 __MODULE_PARM_TYPE(enable, "string");
63 MODULE_PARM_DESC(enable, " Enables fcoe on an ethernet interface.");
65 module_param_call(disable, fcoe_transport_disable, NULL, NULL, S_IWUSR);
66 __MODULE_PARM_TYPE(disable, "string");
67 MODULE_PARM_DESC(disable, " Disables fcoe on an ethernet interface.");
69 /* notification function for packets from net device */
70 static struct notifier_block libfcoe_notifier = {
71 .notifier_call = libfcoe_device_notification,
76 #define SPEED_2000 2000
77 #define SPEED_4000 4000
78 #define SPEED_8000 8000
79 #define SPEED_16000 16000
80 #define SPEED_32000 32000
82 } fcoe_port_speed_mapping[] = {
83 { FC_PORTSPEED_1GBIT, SPEED_1000 },
84 { FC_PORTSPEED_2GBIT, SPEED_2000 },
85 { FC_PORTSPEED_4GBIT, SPEED_4000 },
86 { FC_PORTSPEED_8GBIT, SPEED_8000 },
87 { FC_PORTSPEED_10GBIT, SPEED_10000 },
88 { FC_PORTSPEED_16GBIT, SPEED_16000 },
89 { FC_PORTSPEED_20GBIT, SPEED_20000 },
90 { FC_PORTSPEED_25GBIT, SPEED_25000 },
91 { FC_PORTSPEED_32GBIT, SPEED_32000 },
92 { FC_PORTSPEED_40GBIT, SPEED_40000 },
93 { FC_PORTSPEED_50GBIT, SPEED_50000 },
94 { FC_PORTSPEED_100GBIT, SPEED_100000 },
97 static inline u32 eth2fc_speed(u32 eth_port_speed)
101 for (i = 0; i < ARRAY_SIZE(fcoe_port_speed_mapping); i++) {
102 if (fcoe_port_speed_mapping[i].eth_port_speed == eth_port_speed)
103 return fcoe_port_speed_mapping[i].fc_port_speed;
106 return FC_PORTSPEED_UNKNOWN;
110 * fcoe_link_speed_update() - Update the supported and actual link speeds
111 * @lport: The local port to update speeds for
113 * Returns: 0 if the ethtool query was successful
114 * -1 if the ethtool query failed
116 int fcoe_link_speed_update(struct fc_lport *lport)
118 struct net_device *netdev = fcoe_get_netdev(lport);
119 struct ethtool_link_ksettings ecmd;
121 if (!__ethtool_get_link_ksettings(netdev, &ecmd)) {
122 lport->link_supported_speeds &= ~(FC_PORTSPEED_1GBIT |
123 FC_PORTSPEED_10GBIT |
124 FC_PORTSPEED_20GBIT |
125 FC_PORTSPEED_40GBIT);
127 if (ecmd.link_modes.supported[0] & (
128 SUPPORTED_1000baseT_Half |
129 SUPPORTED_1000baseT_Full |
130 SUPPORTED_1000baseKX_Full))
131 lport->link_supported_speeds |= FC_PORTSPEED_1GBIT;
133 if (ecmd.link_modes.supported[0] & (
134 SUPPORTED_10000baseT_Full |
135 SUPPORTED_10000baseKX4_Full |
136 SUPPORTED_10000baseKR_Full |
137 SUPPORTED_10000baseR_FEC))
138 lport->link_supported_speeds |= FC_PORTSPEED_10GBIT;
140 if (ecmd.link_modes.supported[0] & (
141 SUPPORTED_20000baseMLD2_Full |
142 SUPPORTED_20000baseKR2_Full))
143 lport->link_supported_speeds |= FC_PORTSPEED_20GBIT;
145 if (ecmd.link_modes.supported[0] & (
146 SUPPORTED_40000baseKR4_Full |
147 SUPPORTED_40000baseCR4_Full |
148 SUPPORTED_40000baseSR4_Full |
149 SUPPORTED_40000baseLR4_Full))
150 lport->link_supported_speeds |= FC_PORTSPEED_40GBIT;
152 lport->link_speed = eth2fc_speed(ecmd.base.speed);
157 EXPORT_SYMBOL_GPL(fcoe_link_speed_update);
160 * __fcoe_get_lesb() - Get the Link Error Status Block (LESB) for a given lport
161 * @lport: The local port to update speeds for
162 * @fc_lesb: Pointer to the LESB to be filled up
163 * @netdev: Pointer to the netdev that is associated with the lport
165 * Note, the Link Error Status Block (LESB) for FCoE is defined in FC-BB-6
166 * Clause 7.11 in v1.04.
168 void __fcoe_get_lesb(struct fc_lport *lport,
169 struct fc_els_lesb *fc_lesb,
170 struct net_device *netdev)
174 struct fc_stats *stats;
175 struct fcoe_fc_els_lesb *lesb;
176 struct rtnl_link_stats64 temp;
181 lesb = (struct fcoe_fc_els_lesb *)fc_lesb;
182 memset(lesb, 0, sizeof(*lesb));
183 for_each_possible_cpu(cpu) {
184 stats = per_cpu_ptr(lport->stats, cpu);
185 lfc += stats->LinkFailureCount;
186 vlfc += stats->VLinkFailureCount;
187 mdac += stats->MissDiscAdvCount;
189 lesb->lesb_link_fail = htonl(lfc);
190 lesb->lesb_vlink_fail = htonl(vlfc);
191 lesb->lesb_miss_fka = htonl(mdac);
192 lesb->lesb_fcs_error =
193 htonl(dev_get_stats(netdev, &temp)->rx_crc_errors);
195 EXPORT_SYMBOL_GPL(__fcoe_get_lesb);
198 * fcoe_get_lesb() - Fill the FCoE Link Error Status Block
199 * @lport: the local port
200 * @fc_lesb: the link error status block
202 void fcoe_get_lesb(struct fc_lport *lport,
203 struct fc_els_lesb *fc_lesb)
205 struct net_device *netdev = fcoe_get_netdev(lport);
207 __fcoe_get_lesb(lport, fc_lesb, netdev);
209 EXPORT_SYMBOL_GPL(fcoe_get_lesb);
212 * fcoe_ctlr_get_lesb() - Get the Link Error Status Block (LESB) for a given
213 * fcoe controller device
214 * @ctlr_dev: The given fcoe controller device
217 void fcoe_ctlr_get_lesb(struct fcoe_ctlr_device *ctlr_dev)
219 struct fcoe_ctlr *fip = fcoe_ctlr_device_priv(ctlr_dev);
220 struct net_device *netdev = fcoe_get_netdev(fip->lp);
221 struct fc_els_lesb *fc_lesb;
223 fc_lesb = (struct fc_els_lesb *)(&ctlr_dev->lesb);
224 __fcoe_get_lesb(fip->lp, fc_lesb, netdev);
226 EXPORT_SYMBOL_GPL(fcoe_ctlr_get_lesb);
228 void fcoe_wwn_to_str(u64 wwn, char *buf, int len)
232 u64_to_wwn(wwn, wwpn);
233 snprintf(buf, len, "%02x%02x%02x%02x%02x%02x%02x%02x",
234 wwpn[0], wwpn[1], wwpn[2], wwpn[3],
235 wwpn[4], wwpn[5], wwpn[6], wwpn[7]);
237 EXPORT_SYMBOL_GPL(fcoe_wwn_to_str);
240 * fcoe_validate_vport_create() - Validate a vport before creating it
241 * @vport: NPIV port to be created
243 * This routine is meant to add validation for a vport before creating it
244 * via fcoe_vport_create().
245 * Current validations are:
246 * - WWPN supplied is unique for given lport
248 int fcoe_validate_vport_create(struct fc_vport *vport)
250 struct Scsi_Host *shost = vport_to_shost(vport);
251 struct fc_lport *n_port = shost_priv(shost);
252 struct fc_lport *vn_port;
256 mutex_lock(&n_port->lp_mutex);
258 fcoe_wwn_to_str(vport->port_name, buf, sizeof(buf));
259 /* Check if the wwpn is not same as that of the lport */
260 if (!memcmp(&n_port->wwpn, &vport->port_name, sizeof(u64))) {
261 LIBFCOE_TRANSPORT_DBG("vport WWPN 0x%s is same as that of the "
262 "base port WWPN\n", buf);
267 /* Check if there is any existing vport with same wwpn */
268 list_for_each_entry(vn_port, &n_port->vports, list) {
269 if (!memcmp(&vn_port->wwpn, &vport->port_name, sizeof(u64))) {
270 LIBFCOE_TRANSPORT_DBG("vport with given WWPN 0x%s "
271 "already exists\n", buf);
277 mutex_unlock(&n_port->lp_mutex);
280 EXPORT_SYMBOL_GPL(fcoe_validate_vport_create);
283 * fcoe_get_wwn() - Get the world wide name from LLD if it supports it
284 * @netdev: the associated net device
285 * @wwn: the output WWN
286 * @type: the type of WWN (WWPN or WWNN)
288 * Returns: 0 for success
290 int fcoe_get_wwn(struct net_device *netdev, u64 *wwn, int type)
292 const struct net_device_ops *ops = netdev->netdev_ops;
294 if (ops->ndo_fcoe_get_wwn)
295 return ops->ndo_fcoe_get_wwn(netdev, wwn, type);
298 EXPORT_SYMBOL_GPL(fcoe_get_wwn);
301 * fcoe_fc_crc() - Calculates the CRC for a given frame
302 * @fp: The frame to be checksumed
304 * This uses crc32() routine to calculate the CRC for a frame
306 * Return: The 32 bit CRC value
308 u32 fcoe_fc_crc(struct fc_frame *fp)
310 struct sk_buff *skb = fp_skb(fp);
313 unsigned long off, len, clen;
317 crc = crc32(~0, skb->data, skb_headlen(skb));
319 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
320 frag = &skb_shinfo(skb)->frags[i];
321 off = skb_frag_off(frag);
322 len = skb_frag_size(frag);
324 clen = min(len, PAGE_SIZE - (off & ~PAGE_MASK));
326 skb_frag_page(frag) + (off >> PAGE_SHIFT));
327 crc = crc32(crc, data + (off & ~PAGE_MASK), clen);
335 EXPORT_SYMBOL_GPL(fcoe_fc_crc);
338 * fcoe_start_io() - Start FCoE I/O
339 * @skb: The packet to be transmitted
341 * This routine is called from the net device to start transmitting
344 * Returns: 0 for success
346 int fcoe_start_io(struct sk_buff *skb)
348 struct sk_buff *nskb;
351 nskb = skb_clone(skb, GFP_ATOMIC);
354 rc = dev_queue_xmit(nskb);
360 EXPORT_SYMBOL_GPL(fcoe_start_io);
364 * fcoe_clean_pending_queue() - Dequeue a skb and free it
365 * @lport: The local port to dequeue a skb on
367 void fcoe_clean_pending_queue(struct fc_lport *lport)
369 struct fcoe_port *port = lport_priv(lport);
372 spin_lock_bh(&port->fcoe_pending_queue.lock);
373 while ((skb = __skb_dequeue(&port->fcoe_pending_queue)) != NULL) {
374 spin_unlock_bh(&port->fcoe_pending_queue.lock);
376 spin_lock_bh(&port->fcoe_pending_queue.lock);
378 spin_unlock_bh(&port->fcoe_pending_queue.lock);
380 EXPORT_SYMBOL_GPL(fcoe_clean_pending_queue);
383 * fcoe_check_wait_queue() - Attempt to clear the transmit backlog
384 * @lport: The local port whose backlog is to be cleared
385 * @skb: The received FIP packet
387 * This empties the wait_queue, dequeues the head of the wait_queue queue
388 * and calls fcoe_start_io() for each packet. If all skb have been
389 * transmitted it returns the qlen. If an error occurs it restores
390 * wait_queue (to try again later) and returns -1.
392 * The wait_queue is used when the skb transmit fails. The failed skb
393 * will go in the wait_queue which will be emptied by the timer function or
394 * by the next skb transmit.
396 void fcoe_check_wait_queue(struct fc_lport *lport, struct sk_buff *skb)
398 struct fcoe_port *port = lport_priv(lport);
401 spin_lock_bh(&port->fcoe_pending_queue.lock);
404 __skb_queue_tail(&port->fcoe_pending_queue, skb);
406 if (port->fcoe_pending_queue_active)
408 port->fcoe_pending_queue_active = 1;
410 while (port->fcoe_pending_queue.qlen) {
411 /* keep qlen > 0 until fcoe_start_io succeeds */
412 port->fcoe_pending_queue.qlen++;
413 skb = __skb_dequeue(&port->fcoe_pending_queue);
415 spin_unlock_bh(&port->fcoe_pending_queue.lock);
416 rc = fcoe_start_io(skb);
417 spin_lock_bh(&port->fcoe_pending_queue.lock);
420 __skb_queue_head(&port->fcoe_pending_queue, skb);
421 /* undo temporary increment above */
422 port->fcoe_pending_queue.qlen--;
425 /* undo temporary increment above */
426 port->fcoe_pending_queue.qlen--;
429 if (port->fcoe_pending_queue.qlen < port->min_queue_depth)
431 if (port->fcoe_pending_queue.qlen && !timer_pending(&port->timer))
432 mod_timer(&port->timer, jiffies + 2);
433 port->fcoe_pending_queue_active = 0;
435 if (port->fcoe_pending_queue.qlen > port->max_queue_depth)
437 spin_unlock_bh(&port->fcoe_pending_queue.lock);
439 EXPORT_SYMBOL_GPL(fcoe_check_wait_queue);
442 * fcoe_queue_timer() - The fcoe queue timer
443 * @t: Timer context use to obtain the FCoE port
445 * Calls fcoe_check_wait_queue on timeout
447 void fcoe_queue_timer(struct timer_list *t)
449 struct fcoe_port *port = from_timer(port, t, timer);
451 fcoe_check_wait_queue(port->lport, NULL);
453 EXPORT_SYMBOL_GPL(fcoe_queue_timer);
456 * fcoe_get_paged_crc_eof() - Allocate a page to be used for the trailer CRC
457 * @skb: The packet to be transmitted
458 * @tlen: The total length of the trailer
459 * @fps: The fcoe context
461 * This routine allocates a page for frame trailers. The page is re-used if
462 * there is enough room left on it for the current trailer. If there isn't
463 * enough buffer left a new page is allocated for the trailer. Reference to
464 * the page from this function as well as the skbs using the page fragments
465 * ensure that the page is freed at the appropriate time.
467 * Returns: 0 for success
469 int fcoe_get_paged_crc_eof(struct sk_buff *skb, int tlen,
470 struct fcoe_percpu_s *fps)
474 page = fps->crc_eof_page;
476 page = alloc_page(GFP_ATOMIC);
480 fps->crc_eof_page = page;
481 fps->crc_eof_offset = 0;
485 skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags, page,
486 fps->crc_eof_offset, tlen);
488 skb->data_len += tlen;
489 skb->truesize += tlen;
490 fps->crc_eof_offset += sizeof(struct fcoe_crc_eof);
492 if (fps->crc_eof_offset >= PAGE_SIZE) {
493 fps->crc_eof_page = NULL;
494 fps->crc_eof_offset = 0;
500 EXPORT_SYMBOL_GPL(fcoe_get_paged_crc_eof);
503 * fcoe_transport_lookup - find an fcoe transport that matches a netdev
504 * @netdev: The netdev to look for from all attached transports
506 * Returns : ptr to the fcoe transport that supports this netdev or NULL
509 * The ft_mutex should be held when this is called
511 static struct fcoe_transport *fcoe_transport_lookup(struct net_device *netdev)
513 struct fcoe_transport *ft = NULL;
515 list_for_each_entry(ft, &fcoe_transports, list)
516 if (ft->match && ft->match(netdev))
522 * fcoe_transport_attach - Attaches an FCoE transport
523 * @ft: The fcoe transport to be attached
525 * Returns : 0 for success
527 int fcoe_transport_attach(struct fcoe_transport *ft)
531 mutex_lock(&ft_mutex);
533 LIBFCOE_TRANSPORT_DBG("transport %s already attached\n",
539 /* Add default transport to the tail */
540 if (strcmp(ft->name, FCOE_TRANSPORT_DEFAULT))
541 list_add(&ft->list, &fcoe_transports);
543 list_add_tail(&ft->list, &fcoe_transports);
546 LIBFCOE_TRANSPORT_DBG("attaching transport %s\n", ft->name);
549 mutex_unlock(&ft_mutex);
552 EXPORT_SYMBOL(fcoe_transport_attach);
555 * fcoe_transport_detach - Detaches an FCoE transport
556 * @ft: The fcoe transport to be attached
558 * Returns : 0 for success
560 int fcoe_transport_detach(struct fcoe_transport *ft)
563 struct fcoe_netdev_mapping *nm = NULL, *tmp;
565 mutex_lock(&ft_mutex);
567 LIBFCOE_TRANSPORT_DBG("transport %s already detached\n",
573 /* remove netdev mapping for this transport as it is going away */
574 mutex_lock(&fn_mutex);
575 list_for_each_entry_safe(nm, tmp, &fcoe_netdevs, list) {
577 LIBFCOE_TRANSPORT_DBG("transport %s going away, "
578 "remove its netdev mapping for %s\n",
579 ft->name, nm->netdev->name);
584 mutex_unlock(&fn_mutex);
587 ft->attached = false;
588 LIBFCOE_TRANSPORT_DBG("detaching transport %s\n", ft->name);
591 mutex_unlock(&ft_mutex);
595 EXPORT_SYMBOL(fcoe_transport_detach);
597 static int fcoe_transport_show(char *buffer, const struct kernel_param *kp)
600 struct fcoe_transport *ft = NULL;
602 i = j = sprintf(buffer, "Attached FCoE transports:");
603 mutex_lock(&ft_mutex);
604 list_for_each_entry(ft, &fcoe_transports, list) {
605 if (i >= PAGE_SIZE - IFNAMSIZ)
607 i += snprintf(&buffer[i], IFNAMSIZ, "%s ", ft->name);
609 mutex_unlock(&ft_mutex);
611 i += snprintf(&buffer[i], IFNAMSIZ, "none");
615 static int __init fcoe_transport_init(void)
617 register_netdevice_notifier(&libfcoe_notifier);
621 static int fcoe_transport_exit(void)
623 struct fcoe_transport *ft;
625 unregister_netdevice_notifier(&libfcoe_notifier);
626 mutex_lock(&ft_mutex);
627 list_for_each_entry(ft, &fcoe_transports, list)
628 printk(KERN_ERR "FCoE transport %s is still attached!\n",
630 mutex_unlock(&ft_mutex);
635 static int fcoe_add_netdev_mapping(struct net_device *netdev,
636 struct fcoe_transport *ft)
638 struct fcoe_netdev_mapping *nm;
640 nm = kmalloc(sizeof(*nm), GFP_KERNEL);
642 printk(KERN_ERR "Unable to allocate netdev_mapping");
649 mutex_lock(&fn_mutex);
650 list_add(&nm->list, &fcoe_netdevs);
651 mutex_unlock(&fn_mutex);
656 static void fcoe_del_netdev_mapping(struct net_device *netdev)
658 struct fcoe_netdev_mapping *nm = NULL, *tmp;
660 mutex_lock(&fn_mutex);
661 list_for_each_entry_safe(nm, tmp, &fcoe_netdevs, list) {
662 if (nm->netdev == netdev) {
665 mutex_unlock(&fn_mutex);
669 mutex_unlock(&fn_mutex);
674 * fcoe_netdev_map_lookup - find the fcoe transport that matches the netdev on which
676 * @netdev: The net device that the FCoE interface is on
678 * Returns : ptr to the fcoe transport that supports this netdev or NULL
681 * The ft_mutex should be held when this is called
683 static struct fcoe_transport *fcoe_netdev_map_lookup(struct net_device *netdev)
685 struct fcoe_transport *ft = NULL;
686 struct fcoe_netdev_mapping *nm;
688 mutex_lock(&fn_mutex);
689 list_for_each_entry(nm, &fcoe_netdevs, list) {
690 if (netdev == nm->netdev) {
692 mutex_unlock(&fn_mutex);
697 mutex_unlock(&fn_mutex);
702 * fcoe_if_to_netdev() - Parse a name buffer to get a net device
703 * @buffer: The name of the net device
705 * Returns: NULL or a ptr to net_device
707 static struct net_device *fcoe_if_to_netdev(const char *buffer)
710 char ifname[IFNAMSIZ + 2];
713 strlcpy(ifname, buffer, IFNAMSIZ);
714 cp = ifname + strlen(ifname);
715 while (--cp >= ifname && *cp == '\n')
717 return dev_get_by_name(&init_net, ifname);
723 * libfcoe_device_notification() - Handler for net device events
724 * @notifier: The context of the notification
725 * @event: The type of event
726 * @ptr: The net device that the event was on
728 * This function is called by the Ethernet driver in case of link change event.
730 * Returns: 0 for success
732 static int libfcoe_device_notification(struct notifier_block *notifier,
733 ulong event, void *ptr)
735 struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
738 case NETDEV_UNREGISTER:
739 LIBFCOE_TRANSPORT_DBG("NETDEV_UNREGISTER %s\n",
741 fcoe_del_netdev_mapping(netdev);
747 ssize_t fcoe_ctlr_create_store(struct bus_type *bus,
748 const char *buf, size_t count)
750 struct net_device *netdev = NULL;
751 struct fcoe_transport *ft = NULL;
755 mutex_lock(&ft_mutex);
757 netdev = fcoe_if_to_netdev(buf);
759 LIBFCOE_TRANSPORT_DBG("Invalid device %s.\n", buf);
764 ft = fcoe_netdev_map_lookup(netdev);
766 LIBFCOE_TRANSPORT_DBG("transport %s already has existing "
767 "FCoE instance on %s.\n",
768 ft->name, netdev->name);
773 ft = fcoe_transport_lookup(netdev);
775 LIBFCOE_TRANSPORT_DBG("no FCoE transport found for %s.\n",
781 /* pass to transport create */
782 err = ft->alloc ? ft->alloc(netdev) : -ENODEV;
784 fcoe_del_netdev_mapping(netdev);
789 err = fcoe_add_netdev_mapping(netdev, ft);
791 LIBFCOE_TRANSPORT_DBG("failed to add new netdev mapping "
792 "for FCoE transport %s for %s.\n",
793 ft->name, netdev->name);
798 LIBFCOE_TRANSPORT_DBG("transport %s succeeded to create fcoe on %s.\n",
799 ft->name, netdev->name);
804 mutex_unlock(&ft_mutex);
810 ssize_t fcoe_ctlr_destroy_store(struct bus_type *bus,
811 const char *buf, size_t count)
814 struct net_device *netdev = NULL;
815 struct fcoe_transport *ft = NULL;
817 mutex_lock(&ft_mutex);
819 netdev = fcoe_if_to_netdev(buf);
821 LIBFCOE_TRANSPORT_DBG("invalid device %s.\n", buf);
825 ft = fcoe_netdev_map_lookup(netdev);
827 LIBFCOE_TRANSPORT_DBG("no FCoE transport found for %s.\n",
832 /* pass to transport destroy */
833 rc = ft->destroy(netdev);
837 fcoe_del_netdev_mapping(netdev);
838 LIBFCOE_TRANSPORT_DBG("transport %s %s to destroy fcoe on %s.\n",
839 ft->name, (rc) ? "failed" : "succeeded",
841 rc = count; /* required for successful return */
845 mutex_unlock(&ft_mutex);
850 * fcoe_transport_create() - Create a fcoe interface
851 * @buffer: The name of the Ethernet interface to create on
852 * @kp: The associated kernel param
854 * Called from sysfs. This holds the ft_mutex while calling the
855 * registered fcoe transport's create function.
857 * Returns: 0 for success
859 static int fcoe_transport_create(const char *buffer,
860 const struct kernel_param *kp)
863 struct net_device *netdev = NULL;
864 struct fcoe_transport *ft = NULL;
865 enum fip_mode fip_mode = (enum fip_mode)kp->arg;
867 mutex_lock(&ft_mutex);
869 netdev = fcoe_if_to_netdev(buffer);
871 LIBFCOE_TRANSPORT_DBG("Invalid device %s.\n", buffer);
875 ft = fcoe_netdev_map_lookup(netdev);
877 LIBFCOE_TRANSPORT_DBG("transport %s already has existing "
878 "FCoE instance on %s.\n",
879 ft->name, netdev->name);
884 ft = fcoe_transport_lookup(netdev);
886 LIBFCOE_TRANSPORT_DBG("no FCoE transport found for %s.\n",
891 rc = fcoe_add_netdev_mapping(netdev, ft);
893 LIBFCOE_TRANSPORT_DBG("failed to add new netdev mapping "
894 "for FCoE transport %s for %s.\n",
895 ft->name, netdev->name);
899 /* pass to transport create */
900 rc = ft->create ? ft->create(netdev, fip_mode) : -ENODEV;
902 fcoe_del_netdev_mapping(netdev);
904 LIBFCOE_TRANSPORT_DBG("transport %s %s to create fcoe on %s.\n",
905 ft->name, (rc) ? "failed" : "succeeded",
911 mutex_unlock(&ft_mutex);
916 * fcoe_transport_destroy() - Destroy a FCoE interface
917 * @buffer: The name of the Ethernet interface to be destroyed
918 * @kp: The associated kernel parameter
920 * Called from sysfs. This holds the ft_mutex while calling the
921 * registered fcoe transport's destroy function.
923 * Returns: 0 for success
925 static int fcoe_transport_destroy(const char *buffer,
926 const struct kernel_param *kp)
929 struct net_device *netdev = NULL;
930 struct fcoe_transport *ft = NULL;
932 mutex_lock(&ft_mutex);
934 netdev = fcoe_if_to_netdev(buffer);
936 LIBFCOE_TRANSPORT_DBG("invalid device %s.\n", buffer);
940 ft = fcoe_netdev_map_lookup(netdev);
942 LIBFCOE_TRANSPORT_DBG("no FCoE transport found for %s.\n",
947 /* pass to transport destroy */
948 rc = ft->destroy ? ft->destroy(netdev) : -ENODEV;
949 fcoe_del_netdev_mapping(netdev);
950 LIBFCOE_TRANSPORT_DBG("transport %s %s to destroy fcoe on %s.\n",
951 ft->name, (rc) ? "failed" : "succeeded",
957 mutex_unlock(&ft_mutex);
962 * fcoe_transport_disable() - Disables a FCoE interface
963 * @buffer: The name of the Ethernet interface to be disabled
964 * @kp: The associated kernel parameter
968 * Returns: 0 for success
970 static int fcoe_transport_disable(const char *buffer,
971 const struct kernel_param *kp)
974 struct net_device *netdev = NULL;
975 struct fcoe_transport *ft = NULL;
977 mutex_lock(&ft_mutex);
979 netdev = fcoe_if_to_netdev(buffer);
983 ft = fcoe_netdev_map_lookup(netdev);
987 rc = ft->disable ? ft->disable(netdev) : -ENODEV;
992 mutex_unlock(&ft_mutex);
997 * fcoe_transport_enable() - Enables a FCoE interface
998 * @buffer: The name of the Ethernet interface to be enabled
999 * @kp: The associated kernel parameter
1001 * Called from sysfs.
1003 * Returns: 0 for success
1005 static int fcoe_transport_enable(const char *buffer,
1006 const struct kernel_param *kp)
1009 struct net_device *netdev = NULL;
1010 struct fcoe_transport *ft = NULL;
1012 mutex_lock(&ft_mutex);
1014 netdev = fcoe_if_to_netdev(buffer);
1018 ft = fcoe_netdev_map_lookup(netdev);
1022 rc = ft->enable ? ft->enable(netdev) : -ENODEV;
1027 mutex_unlock(&ft_mutex);
1032 * libfcoe_init() - Initialization routine for libfcoe.ko
1034 static int __init libfcoe_init(void)
1038 rc = fcoe_transport_init();
1042 rc = fcoe_sysfs_setup();
1044 fcoe_transport_exit();
1048 module_init(libfcoe_init);
1051 * libfcoe_exit() - Tear down libfcoe.ko
1053 static void __exit libfcoe_exit(void)
1055 fcoe_sysfs_teardown();
1056 fcoe_transport_exit();
1058 module_exit(libfcoe_exit);