Merge branch 'r6040-next'
[linux/fpc-iii.git] / drivers / scsi / fcoe / fcoe_transport.c
blob641c60e8fda33b03a3f5f32b41cced7ac9eb8943
1 /*
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
11 * more details.
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>
29 #include "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,
86 /**
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_link_ksettings ecmd;
98 if (!__ethtool_get_link_ksettings(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.link_modes.supported[0] & (
105 SUPPORTED_1000baseT_Half |
106 SUPPORTED_1000baseT_Full |
107 SUPPORTED_1000baseKX_Full))
108 lport->link_supported_speeds |= FC_PORTSPEED_1GBIT;
110 if (ecmd.link_modes.supported[0] & (
111 SUPPORTED_10000baseT_Full |
112 SUPPORTED_10000baseKX4_Full |
113 SUPPORTED_10000baseKR_Full |
114 SUPPORTED_10000baseR_FEC))
115 lport->link_supported_speeds |= FC_PORTSPEED_10GBIT;
117 if (ecmd.link_modes.supported[0] & (
118 SUPPORTED_20000baseMLD2_Full |
119 SUPPORTED_20000baseKR2_Full))
120 lport->link_supported_speeds |= FC_PORTSPEED_20GBIT;
122 if (ecmd.link_modes.supported[0] & (
123 SUPPORTED_40000baseKR4_Full |
124 SUPPORTED_40000baseCR4_Full |
125 SUPPORTED_40000baseSR4_Full |
126 SUPPORTED_40000baseLR4_Full))
127 lport->link_supported_speeds |= FC_PORTSPEED_40GBIT;
129 switch (ecmd.base.speed) {
130 case SPEED_1000:
131 lport->link_speed = FC_PORTSPEED_1GBIT;
132 break;
133 case SPEED_10000:
134 lport->link_speed = FC_PORTSPEED_10GBIT;
135 break;
136 case 20000:
137 lport->link_speed = FC_PORTSPEED_20GBIT;
138 break;
139 case 40000:
140 lport->link_speed = FC_PORTSPEED_40GBIT;
141 break;
142 default:
143 lport->link_speed = FC_PORTSPEED_UNKNOWN;
144 break;
146 return 0;
148 return -1;
150 EXPORT_SYMBOL_GPL(fcoe_link_speed_update);
153 * __fcoe_get_lesb() - Get the Link Error Status Block (LESB) for a given lport
154 * @lport: The local port to update speeds for
155 * @fc_lesb: Pointer to the LESB to be filled up
156 * @netdev: Pointer to the netdev that is associated with the lport
158 * Note, the Link Error Status Block (LESB) for FCoE is defined in FC-BB-6
159 * Clause 7.11 in v1.04.
161 void __fcoe_get_lesb(struct fc_lport *lport,
162 struct fc_els_lesb *fc_lesb,
163 struct net_device *netdev)
165 unsigned int cpu;
166 u32 lfc, vlfc, mdac;
167 struct fc_stats *stats;
168 struct fcoe_fc_els_lesb *lesb;
169 struct rtnl_link_stats64 temp;
171 lfc = 0;
172 vlfc = 0;
173 mdac = 0;
174 lesb = (struct fcoe_fc_els_lesb *)fc_lesb;
175 memset(lesb, 0, sizeof(*lesb));
176 for_each_possible_cpu(cpu) {
177 stats = per_cpu_ptr(lport->stats, cpu);
178 lfc += stats->LinkFailureCount;
179 vlfc += stats->VLinkFailureCount;
180 mdac += stats->MissDiscAdvCount;
182 lesb->lesb_link_fail = htonl(lfc);
183 lesb->lesb_vlink_fail = htonl(vlfc);
184 lesb->lesb_miss_fka = htonl(mdac);
185 lesb->lesb_fcs_error =
186 htonl(dev_get_stats(netdev, &temp)->rx_crc_errors);
188 EXPORT_SYMBOL_GPL(__fcoe_get_lesb);
191 * fcoe_get_lesb() - Fill the FCoE Link Error Status Block
192 * @lport: the local port
193 * @fc_lesb: the link error status block
195 void fcoe_get_lesb(struct fc_lport *lport,
196 struct fc_els_lesb *fc_lesb)
198 struct net_device *netdev = fcoe_get_netdev(lport);
200 __fcoe_get_lesb(lport, fc_lesb, netdev);
202 EXPORT_SYMBOL_GPL(fcoe_get_lesb);
205 * fcoe_ctlr_get_lesb() - Get the Link Error Status Block (LESB) for a given
206 * fcoe controller device
207 * @ctlr_dev: The given fcoe controller device
210 void fcoe_ctlr_get_lesb(struct fcoe_ctlr_device *ctlr_dev)
212 struct fcoe_ctlr *fip = fcoe_ctlr_device_priv(ctlr_dev);
213 struct net_device *netdev = fcoe_get_netdev(fip->lp);
214 struct fc_els_lesb *fc_lesb;
216 fc_lesb = (struct fc_els_lesb *)(&ctlr_dev->lesb);
217 __fcoe_get_lesb(fip->lp, fc_lesb, netdev);
219 EXPORT_SYMBOL_GPL(fcoe_ctlr_get_lesb);
221 void fcoe_wwn_to_str(u64 wwn, char *buf, int len)
223 u8 wwpn[8];
225 u64_to_wwn(wwn, wwpn);
226 snprintf(buf, len, "%02x%02x%02x%02x%02x%02x%02x%02x",
227 wwpn[0], wwpn[1], wwpn[2], wwpn[3],
228 wwpn[4], wwpn[5], wwpn[6], wwpn[7]);
230 EXPORT_SYMBOL_GPL(fcoe_wwn_to_str);
233 * fcoe_validate_vport_create() - Validate a vport before creating it
234 * @vport: NPIV port to be created
236 * This routine is meant to add validation for a vport before creating it
237 * via fcoe_vport_create().
238 * Current validations are:
239 * - WWPN supplied is unique for given lport
241 int fcoe_validate_vport_create(struct fc_vport *vport)
243 struct Scsi_Host *shost = vport_to_shost(vport);
244 struct fc_lport *n_port = shost_priv(shost);
245 struct fc_lport *vn_port;
246 int rc = 0;
247 char buf[32];
249 mutex_lock(&n_port->lp_mutex);
251 fcoe_wwn_to_str(vport->port_name, buf, sizeof(buf));
252 /* Check if the wwpn is not same as that of the lport */
253 if (!memcmp(&n_port->wwpn, &vport->port_name, sizeof(u64))) {
254 LIBFCOE_TRANSPORT_DBG("vport WWPN 0x%s is same as that of the "
255 "base port WWPN\n", buf);
256 rc = -EINVAL;
257 goto out;
260 /* Check if there is any existing vport with same wwpn */
261 list_for_each_entry(vn_port, &n_port->vports, list) {
262 if (!memcmp(&vn_port->wwpn, &vport->port_name, sizeof(u64))) {
263 LIBFCOE_TRANSPORT_DBG("vport with given WWPN 0x%s "
264 "already exists\n", buf);
265 rc = -EINVAL;
266 break;
269 out:
270 mutex_unlock(&n_port->lp_mutex);
271 return rc;
273 EXPORT_SYMBOL_GPL(fcoe_validate_vport_create);
276 * fcoe_get_wwn() - Get the world wide name from LLD if it supports it
277 * @netdev: the associated net device
278 * @wwn: the output WWN
279 * @type: the type of WWN (WWPN or WWNN)
281 * Returns: 0 for success
283 int fcoe_get_wwn(struct net_device *netdev, u64 *wwn, int type)
285 const struct net_device_ops *ops = netdev->netdev_ops;
287 if (ops->ndo_fcoe_get_wwn)
288 return ops->ndo_fcoe_get_wwn(netdev, wwn, type);
289 return -EINVAL;
291 EXPORT_SYMBOL_GPL(fcoe_get_wwn);
294 * fcoe_fc_crc() - Calculates the CRC for a given frame
295 * @fp: The frame to be checksumed
297 * This uses crc32() routine to calculate the CRC for a frame
299 * Return: The 32 bit CRC value
301 u32 fcoe_fc_crc(struct fc_frame *fp)
303 struct sk_buff *skb = fp_skb(fp);
304 struct skb_frag_struct *frag;
305 unsigned char *data;
306 unsigned long off, len, clen;
307 u32 crc;
308 unsigned i;
310 crc = crc32(~0, skb->data, skb_headlen(skb));
312 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
313 frag = &skb_shinfo(skb)->frags[i];
314 off = frag->page_offset;
315 len = skb_frag_size(frag);
316 while (len > 0) {
317 clen = min(len, PAGE_SIZE - (off & ~PAGE_MASK));
318 data = kmap_atomic(
319 skb_frag_page(frag) + (off >> PAGE_SHIFT));
320 crc = crc32(crc, data + (off & ~PAGE_MASK), clen);
321 kunmap_atomic(data);
322 off += clen;
323 len -= clen;
326 return crc;
328 EXPORT_SYMBOL_GPL(fcoe_fc_crc);
331 * fcoe_start_io() - Start FCoE I/O
332 * @skb: The packet to be transmitted
334 * This routine is called from the net device to start transmitting
335 * FCoE packets.
337 * Returns: 0 for success
339 int fcoe_start_io(struct sk_buff *skb)
341 struct sk_buff *nskb;
342 int rc;
344 nskb = skb_clone(skb, GFP_ATOMIC);
345 if (!nskb)
346 return -ENOMEM;
347 rc = dev_queue_xmit(nskb);
348 if (rc != 0)
349 return rc;
350 kfree_skb(skb);
351 return 0;
353 EXPORT_SYMBOL_GPL(fcoe_start_io);
357 * fcoe_clean_pending_queue() - Dequeue a skb and free it
358 * @lport: The local port to dequeue a skb on
360 void fcoe_clean_pending_queue(struct fc_lport *lport)
362 struct fcoe_port *port = lport_priv(lport);
363 struct sk_buff *skb;
365 spin_lock_bh(&port->fcoe_pending_queue.lock);
366 while ((skb = __skb_dequeue(&port->fcoe_pending_queue)) != NULL) {
367 spin_unlock_bh(&port->fcoe_pending_queue.lock);
368 kfree_skb(skb);
369 spin_lock_bh(&port->fcoe_pending_queue.lock);
371 spin_unlock_bh(&port->fcoe_pending_queue.lock);
373 EXPORT_SYMBOL_GPL(fcoe_clean_pending_queue);
376 * fcoe_check_wait_queue() - Attempt to clear the transmit backlog
377 * @lport: The local port whose backlog is to be cleared
379 * This empties the wait_queue, dequeues the head of the wait_queue queue
380 * and calls fcoe_start_io() for each packet. If all skb have been
381 * transmitted it returns the qlen. If an error occurs it restores
382 * wait_queue (to try again later) and returns -1.
384 * The wait_queue is used when the skb transmit fails. The failed skb
385 * will go in the wait_queue which will be emptied by the timer function or
386 * by the next skb transmit.
388 void fcoe_check_wait_queue(struct fc_lport *lport, struct sk_buff *skb)
390 struct fcoe_port *port = lport_priv(lport);
391 int rc;
393 spin_lock_bh(&port->fcoe_pending_queue.lock);
395 if (skb)
396 __skb_queue_tail(&port->fcoe_pending_queue, skb);
398 if (port->fcoe_pending_queue_active)
399 goto out;
400 port->fcoe_pending_queue_active = 1;
402 while (port->fcoe_pending_queue.qlen) {
403 /* keep qlen > 0 until fcoe_start_io succeeds */
404 port->fcoe_pending_queue.qlen++;
405 skb = __skb_dequeue(&port->fcoe_pending_queue);
407 spin_unlock_bh(&port->fcoe_pending_queue.lock);
408 rc = fcoe_start_io(skb);
409 spin_lock_bh(&port->fcoe_pending_queue.lock);
411 if (rc) {
412 __skb_queue_head(&port->fcoe_pending_queue, skb);
413 /* undo temporary increment above */
414 port->fcoe_pending_queue.qlen--;
415 break;
417 /* undo temporary increment above */
418 port->fcoe_pending_queue.qlen--;
421 if (port->fcoe_pending_queue.qlen < port->min_queue_depth)
422 lport->qfull = 0;
423 if (port->fcoe_pending_queue.qlen && !timer_pending(&port->timer))
424 mod_timer(&port->timer, jiffies + 2);
425 port->fcoe_pending_queue_active = 0;
426 out:
427 if (port->fcoe_pending_queue.qlen > port->max_queue_depth)
428 lport->qfull = 1;
429 spin_unlock_bh(&port->fcoe_pending_queue.lock);
431 EXPORT_SYMBOL_GPL(fcoe_check_wait_queue);
434 * fcoe_queue_timer() - The fcoe queue timer
435 * @lport: The local port
437 * Calls fcoe_check_wait_queue on timeout
439 void fcoe_queue_timer(ulong lport)
441 fcoe_check_wait_queue((struct fc_lport *)lport, NULL);
443 EXPORT_SYMBOL_GPL(fcoe_queue_timer);
446 * fcoe_get_paged_crc_eof() - Allocate a page to be used for the trailer CRC
447 * @skb: The packet to be transmitted
448 * @tlen: The total length of the trailer
449 * @fps: The fcoe context
451 * This routine allocates a page for frame trailers. The page is re-used if
452 * there is enough room left on it for the current trailer. If there isn't
453 * enough buffer left a new page is allocated for the trailer. Reference to
454 * the page from this function as well as the skbs using the page fragments
455 * ensure that the page is freed at the appropriate time.
457 * Returns: 0 for success
459 int fcoe_get_paged_crc_eof(struct sk_buff *skb, int tlen,
460 struct fcoe_percpu_s *fps)
462 struct page *page;
464 page = fps->crc_eof_page;
465 if (!page) {
466 page = alloc_page(GFP_ATOMIC);
467 if (!page)
468 return -ENOMEM;
470 fps->crc_eof_page = page;
471 fps->crc_eof_offset = 0;
474 get_page(page);
475 skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags, page,
476 fps->crc_eof_offset, tlen);
477 skb->len += tlen;
478 skb->data_len += tlen;
479 skb->truesize += tlen;
480 fps->crc_eof_offset += sizeof(struct fcoe_crc_eof);
482 if (fps->crc_eof_offset >= PAGE_SIZE) {
483 fps->crc_eof_page = NULL;
484 fps->crc_eof_offset = 0;
485 put_page(page);
488 return 0;
490 EXPORT_SYMBOL_GPL(fcoe_get_paged_crc_eof);
493 * fcoe_transport_lookup - find an fcoe transport that matches a netdev
494 * @netdev: The netdev to look for from all attached transports
496 * Returns : ptr to the fcoe transport that supports this netdev or NULL
497 * if not found.
499 * The ft_mutex should be held when this is called
501 static struct fcoe_transport *fcoe_transport_lookup(struct net_device *netdev)
503 struct fcoe_transport *ft = NULL;
505 list_for_each_entry(ft, &fcoe_transports, list)
506 if (ft->match && ft->match(netdev))
507 return ft;
508 return NULL;
512 * fcoe_transport_attach - Attaches an FCoE transport
513 * @ft: The fcoe transport to be attached
515 * Returns : 0 for success
517 int fcoe_transport_attach(struct fcoe_transport *ft)
519 int rc = 0;
521 mutex_lock(&ft_mutex);
522 if (ft->attached) {
523 LIBFCOE_TRANSPORT_DBG("transport %s already attached\n",
524 ft->name);
525 rc = -EEXIST;
526 goto out_attach;
529 /* Add default transport to the tail */
530 if (strcmp(ft->name, FCOE_TRANSPORT_DEFAULT))
531 list_add(&ft->list, &fcoe_transports);
532 else
533 list_add_tail(&ft->list, &fcoe_transports);
535 ft->attached = true;
536 LIBFCOE_TRANSPORT_DBG("attaching transport %s\n", ft->name);
538 out_attach:
539 mutex_unlock(&ft_mutex);
540 return rc;
542 EXPORT_SYMBOL(fcoe_transport_attach);
545 * fcoe_transport_detach - Detaches an FCoE transport
546 * @ft: The fcoe transport to be attached
548 * Returns : 0 for success
550 int fcoe_transport_detach(struct fcoe_transport *ft)
552 int rc = 0;
553 struct fcoe_netdev_mapping *nm = NULL, *tmp;
555 mutex_lock(&ft_mutex);
556 if (!ft->attached) {
557 LIBFCOE_TRANSPORT_DBG("transport %s already detached\n",
558 ft->name);
559 rc = -ENODEV;
560 goto out_attach;
563 /* remove netdev mapping for this transport as it is going away */
564 mutex_lock(&fn_mutex);
565 list_for_each_entry_safe(nm, tmp, &fcoe_netdevs, list) {
566 if (nm->ft == ft) {
567 LIBFCOE_TRANSPORT_DBG("transport %s going away, "
568 "remove its netdev mapping for %s\n",
569 ft->name, nm->netdev->name);
570 list_del(&nm->list);
571 kfree(nm);
574 mutex_unlock(&fn_mutex);
576 list_del(&ft->list);
577 ft->attached = false;
578 LIBFCOE_TRANSPORT_DBG("detaching transport %s\n", ft->name);
580 out_attach:
581 mutex_unlock(&ft_mutex);
582 return rc;
585 EXPORT_SYMBOL(fcoe_transport_detach);
587 static int fcoe_transport_show(char *buffer, const struct kernel_param *kp)
589 int i, j;
590 struct fcoe_transport *ft = NULL;
592 i = j = sprintf(buffer, "Attached FCoE transports:");
593 mutex_lock(&ft_mutex);
594 list_for_each_entry(ft, &fcoe_transports, list) {
595 if (i >= PAGE_SIZE - IFNAMSIZ)
596 break;
597 i += snprintf(&buffer[i], IFNAMSIZ, "%s ", ft->name);
599 mutex_unlock(&ft_mutex);
600 if (i == j)
601 i += snprintf(&buffer[i], IFNAMSIZ, "none");
602 return i;
605 static int __init fcoe_transport_init(void)
607 register_netdevice_notifier(&libfcoe_notifier);
608 return 0;
611 static int fcoe_transport_exit(void)
613 struct fcoe_transport *ft;
615 unregister_netdevice_notifier(&libfcoe_notifier);
616 mutex_lock(&ft_mutex);
617 list_for_each_entry(ft, &fcoe_transports, list)
618 printk(KERN_ERR "FCoE transport %s is still attached!\n",
619 ft->name);
620 mutex_unlock(&ft_mutex);
621 return 0;
625 static int fcoe_add_netdev_mapping(struct net_device *netdev,
626 struct fcoe_transport *ft)
628 struct fcoe_netdev_mapping *nm;
630 nm = kmalloc(sizeof(*nm), GFP_KERNEL);
631 if (!nm) {
632 printk(KERN_ERR "Unable to allocate netdev_mapping");
633 return -ENOMEM;
636 nm->netdev = netdev;
637 nm->ft = ft;
639 mutex_lock(&fn_mutex);
640 list_add(&nm->list, &fcoe_netdevs);
641 mutex_unlock(&fn_mutex);
642 return 0;
646 static void fcoe_del_netdev_mapping(struct net_device *netdev)
648 struct fcoe_netdev_mapping *nm = NULL, *tmp;
650 mutex_lock(&fn_mutex);
651 list_for_each_entry_safe(nm, tmp, &fcoe_netdevs, list) {
652 if (nm->netdev == netdev) {
653 list_del(&nm->list);
654 kfree(nm);
655 mutex_unlock(&fn_mutex);
656 return;
659 mutex_unlock(&fn_mutex);
664 * fcoe_netdev_map_lookup - find the fcoe transport that matches the netdev on which
665 * it was created
667 * Returns : ptr to the fcoe transport that supports this netdev or NULL
668 * if not found.
670 * The ft_mutex should be held when this is called
672 static struct fcoe_transport *fcoe_netdev_map_lookup(struct net_device *netdev)
674 struct fcoe_transport *ft = NULL;
675 struct fcoe_netdev_mapping *nm;
677 mutex_lock(&fn_mutex);
678 list_for_each_entry(nm, &fcoe_netdevs, list) {
679 if (netdev == nm->netdev) {
680 ft = nm->ft;
681 mutex_unlock(&fn_mutex);
682 return ft;
686 mutex_unlock(&fn_mutex);
687 return NULL;
691 * fcoe_if_to_netdev() - Parse a name buffer to get a net device
692 * @buffer: The name of the net device
694 * Returns: NULL or a ptr to net_device
696 static struct net_device *fcoe_if_to_netdev(const char *buffer)
698 char *cp;
699 char ifname[IFNAMSIZ + 2];
701 if (buffer) {
702 strlcpy(ifname, buffer, IFNAMSIZ);
703 cp = ifname + strlen(ifname);
704 while (--cp >= ifname && *cp == '\n')
705 *cp = '\0';
706 return dev_get_by_name(&init_net, ifname);
708 return NULL;
712 * libfcoe_device_notification() - Handler for net device events
713 * @notifier: The context of the notification
714 * @event: The type of event
715 * @ptr: The net device that the event was on
717 * This function is called by the Ethernet driver in case of link change event.
719 * Returns: 0 for success
721 static int libfcoe_device_notification(struct notifier_block *notifier,
722 ulong event, void *ptr)
724 struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
726 switch (event) {
727 case NETDEV_UNREGISTER:
728 LIBFCOE_TRANSPORT_DBG("NETDEV_UNREGISTER %s\n",
729 netdev->name);
730 fcoe_del_netdev_mapping(netdev);
731 break;
733 return NOTIFY_OK;
736 ssize_t fcoe_ctlr_create_store(struct bus_type *bus,
737 const char *buf, size_t count)
739 struct net_device *netdev = NULL;
740 struct fcoe_transport *ft = NULL;
741 int rc = 0;
742 int err;
744 mutex_lock(&ft_mutex);
746 netdev = fcoe_if_to_netdev(buf);
747 if (!netdev) {
748 LIBFCOE_TRANSPORT_DBG("Invalid device %s.\n", buf);
749 rc = -ENODEV;
750 goto out_nodev;
753 ft = fcoe_netdev_map_lookup(netdev);
754 if (ft) {
755 LIBFCOE_TRANSPORT_DBG("transport %s already has existing "
756 "FCoE instance on %s.\n",
757 ft->name, netdev->name);
758 rc = -EEXIST;
759 goto out_putdev;
762 ft = fcoe_transport_lookup(netdev);
763 if (!ft) {
764 LIBFCOE_TRANSPORT_DBG("no FCoE transport found for %s.\n",
765 netdev->name);
766 rc = -ENODEV;
767 goto out_putdev;
770 /* pass to transport create */
771 err = ft->alloc ? ft->alloc(netdev) : -ENODEV;
772 if (err) {
773 fcoe_del_netdev_mapping(netdev);
774 rc = -ENOMEM;
775 goto out_putdev;
778 err = fcoe_add_netdev_mapping(netdev, ft);
779 if (err) {
780 LIBFCOE_TRANSPORT_DBG("failed to add new netdev mapping "
781 "for FCoE transport %s for %s.\n",
782 ft->name, netdev->name);
783 rc = -ENODEV;
784 goto out_putdev;
787 LIBFCOE_TRANSPORT_DBG("transport %s succeeded to create fcoe on %s.\n",
788 ft->name, netdev->name);
790 out_putdev:
791 dev_put(netdev);
792 out_nodev:
793 mutex_unlock(&ft_mutex);
794 if (rc)
795 return rc;
796 return count;
799 ssize_t fcoe_ctlr_destroy_store(struct bus_type *bus,
800 const char *buf, size_t count)
802 int rc = -ENODEV;
803 struct net_device *netdev = NULL;
804 struct fcoe_transport *ft = NULL;
806 mutex_lock(&ft_mutex);
808 netdev = fcoe_if_to_netdev(buf);
809 if (!netdev) {
810 LIBFCOE_TRANSPORT_DBG("invalid device %s.\n", buf);
811 goto out_nodev;
814 ft = fcoe_netdev_map_lookup(netdev);
815 if (!ft) {
816 LIBFCOE_TRANSPORT_DBG("no FCoE transport found for %s.\n",
817 netdev->name);
818 goto out_putdev;
821 /* pass to transport destroy */
822 rc = ft->destroy(netdev);
823 if (rc)
824 goto out_putdev;
826 fcoe_del_netdev_mapping(netdev);
827 LIBFCOE_TRANSPORT_DBG("transport %s %s to destroy fcoe on %s.\n",
828 ft->name, (rc) ? "failed" : "succeeded",
829 netdev->name);
830 rc = count; /* required for successful return */
831 out_putdev:
832 dev_put(netdev);
833 out_nodev:
834 mutex_unlock(&ft_mutex);
835 return rc;
837 EXPORT_SYMBOL(fcoe_ctlr_destroy_store);
840 * fcoe_transport_create() - Create a fcoe interface
841 * @buffer: The name of the Ethernet interface to create on
842 * @kp: The associated kernel param
844 * Called from sysfs. This holds the ft_mutex while calling the
845 * registered fcoe transport's create function.
847 * Returns: 0 for success
849 static int fcoe_transport_create(const char *buffer, struct kernel_param *kp)
851 int rc = -ENODEV;
852 struct net_device *netdev = NULL;
853 struct fcoe_transport *ft = NULL;
854 enum fip_state fip_mode = (enum fip_state)(long)kp->arg;
856 mutex_lock(&ft_mutex);
858 netdev = fcoe_if_to_netdev(buffer);
859 if (!netdev) {
860 LIBFCOE_TRANSPORT_DBG("Invalid device %s.\n", buffer);
861 goto out_nodev;
864 ft = fcoe_netdev_map_lookup(netdev);
865 if (ft) {
866 LIBFCOE_TRANSPORT_DBG("transport %s already has existing "
867 "FCoE instance on %s.\n",
868 ft->name, netdev->name);
869 rc = -EEXIST;
870 goto out_putdev;
873 ft = fcoe_transport_lookup(netdev);
874 if (!ft) {
875 LIBFCOE_TRANSPORT_DBG("no FCoE transport found for %s.\n",
876 netdev->name);
877 goto out_putdev;
880 rc = fcoe_add_netdev_mapping(netdev, ft);
881 if (rc) {
882 LIBFCOE_TRANSPORT_DBG("failed to add new netdev mapping "
883 "for FCoE transport %s for %s.\n",
884 ft->name, netdev->name);
885 goto out_putdev;
888 /* pass to transport create */
889 rc = ft->create ? ft->create(netdev, fip_mode) : -ENODEV;
890 if (rc)
891 fcoe_del_netdev_mapping(netdev);
893 LIBFCOE_TRANSPORT_DBG("transport %s %s to create fcoe on %s.\n",
894 ft->name, (rc) ? "failed" : "succeeded",
895 netdev->name);
897 out_putdev:
898 dev_put(netdev);
899 out_nodev:
900 mutex_unlock(&ft_mutex);
901 return rc;
905 * fcoe_transport_destroy() - Destroy a FCoE interface
906 * @buffer: The name of the Ethernet interface to be destroyed
907 * @kp: The associated kernel parameter
909 * Called from sysfs. This holds the ft_mutex while calling the
910 * registered fcoe transport's destroy function.
912 * Returns: 0 for success
914 static int fcoe_transport_destroy(const char *buffer, struct kernel_param *kp)
916 int rc = -ENODEV;
917 struct net_device *netdev = NULL;
918 struct fcoe_transport *ft = NULL;
920 mutex_lock(&ft_mutex);
922 netdev = fcoe_if_to_netdev(buffer);
923 if (!netdev) {
924 LIBFCOE_TRANSPORT_DBG("invalid device %s.\n", buffer);
925 goto out_nodev;
928 ft = fcoe_netdev_map_lookup(netdev);
929 if (!ft) {
930 LIBFCOE_TRANSPORT_DBG("no FCoE transport found for %s.\n",
931 netdev->name);
932 goto out_putdev;
935 /* pass to transport destroy */
936 rc = ft->destroy ? ft->destroy(netdev) : -ENODEV;
937 fcoe_del_netdev_mapping(netdev);
938 LIBFCOE_TRANSPORT_DBG("transport %s %s to destroy fcoe on %s.\n",
939 ft->name, (rc) ? "failed" : "succeeded",
940 netdev->name);
942 out_putdev:
943 dev_put(netdev);
944 out_nodev:
945 mutex_unlock(&ft_mutex);
946 return rc;
950 * fcoe_transport_disable() - Disables a FCoE interface
951 * @buffer: The name of the Ethernet interface to be disabled
952 * @kp: The associated kernel parameter
954 * Called from sysfs.
956 * Returns: 0 for success
958 static int fcoe_transport_disable(const char *buffer, struct kernel_param *kp)
960 int rc = -ENODEV;
961 struct net_device *netdev = NULL;
962 struct fcoe_transport *ft = NULL;
964 mutex_lock(&ft_mutex);
966 netdev = fcoe_if_to_netdev(buffer);
967 if (!netdev)
968 goto out_nodev;
970 ft = fcoe_netdev_map_lookup(netdev);
971 if (!ft)
972 goto out_putdev;
974 rc = ft->disable ? ft->disable(netdev) : -ENODEV;
976 out_putdev:
977 dev_put(netdev);
978 out_nodev:
979 mutex_unlock(&ft_mutex);
980 return rc;
984 * fcoe_transport_enable() - Enables a FCoE interface
985 * @buffer: The name of the Ethernet interface to be enabled
986 * @kp: The associated kernel parameter
988 * Called from sysfs.
990 * Returns: 0 for success
992 static int fcoe_transport_enable(const char *buffer, struct kernel_param *kp)
994 int rc = -ENODEV;
995 struct net_device *netdev = NULL;
996 struct fcoe_transport *ft = NULL;
998 mutex_lock(&ft_mutex);
1000 netdev = fcoe_if_to_netdev(buffer);
1001 if (!netdev)
1002 goto out_nodev;
1004 ft = fcoe_netdev_map_lookup(netdev);
1005 if (!ft)
1006 goto out_putdev;
1008 rc = ft->enable ? ft->enable(netdev) : -ENODEV;
1010 out_putdev:
1011 dev_put(netdev);
1012 out_nodev:
1013 mutex_unlock(&ft_mutex);
1014 return rc;
1018 * libfcoe_init() - Initialization routine for libfcoe.ko
1020 static int __init libfcoe_init(void)
1022 int rc = 0;
1024 rc = fcoe_transport_init();
1025 if (rc)
1026 return rc;
1028 rc = fcoe_sysfs_setup();
1029 if (rc)
1030 fcoe_transport_exit();
1032 return rc;
1034 module_init(libfcoe_init);
1037 * libfcoe_exit() - Tear down libfcoe.ko
1039 static void __exit libfcoe_exit(void)
1041 fcoe_sysfs_teardown();
1042 fcoe_transport_exit();
1044 module_exit(libfcoe_exit);