tracing: Change tracing_stats_fops to rely on tracing_get_cpu()
[linux-2.6.git] / drivers / scsi / fcoe / fcoe_transport.c
blob74277c20f6a5e5601f7b5740e77eaf5f9c638fb9
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 a ethernet interface");
63 module_param_call(create_vn2vn, fcoe_transport_create, NULL,
64 (void *)FIP_MODE_VN2VN, S_IWUSR);
65 __MODULE_PARM_TYPE(create_vn2vn, "string");
66 MODULE_PARM_DESC(create_vn2vn, " Creates a VN_node to VN_node FCoE instance "
67 "on an Ethernet interface");
69 module_param_call(destroy, fcoe_transport_destroy, NULL, NULL, S_IWUSR);
70 __MODULE_PARM_TYPE(destroy, "string");
71 MODULE_PARM_DESC(destroy, " Destroys fcoe instance on a ethernet interface");
73 module_param_call(enable, fcoe_transport_enable, NULL, NULL, S_IWUSR);
74 __MODULE_PARM_TYPE(enable, "string");
75 MODULE_PARM_DESC(enable, " Enables fcoe on a ethernet interface.");
77 module_param_call(disable, fcoe_transport_disable, NULL, NULL, S_IWUSR);
78 __MODULE_PARM_TYPE(disable, "string");
79 MODULE_PARM_DESC(disable, " Disables fcoe on a ethernet interface.");
81 /* notification function for packets from net device */
82 static struct notifier_block libfcoe_notifier = {
83 .notifier_call = libfcoe_device_notification,
86 /**
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 &=
100 ~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
101 if (ecmd.supported & (SUPPORTED_1000baseT_Half |
102 SUPPORTED_1000baseT_Full))
103 lport->link_supported_speeds |= FC_PORTSPEED_1GBIT;
104 if (ecmd.supported & SUPPORTED_10000baseT_Full)
105 lport->link_supported_speeds |=
106 FC_PORTSPEED_10GBIT;
107 switch (ethtool_cmd_speed(&ecmd)) {
108 case SPEED_1000:
109 lport->link_speed = FC_PORTSPEED_1GBIT;
110 break;
111 case SPEED_10000:
112 lport->link_speed = FC_PORTSPEED_10GBIT;
113 break;
115 return 0;
117 return -1;
119 EXPORT_SYMBOL_GPL(fcoe_link_speed_update);
122 * __fcoe_get_lesb() - Get the Link Error Status Block (LESB) for a given lport
123 * @lport: The local port to update speeds for
124 * @fc_lesb: Pointer to the LESB to be filled up
125 * @netdev: Pointer to the netdev that is associated with the lport
127 * Note, the Link Error Status Block (LESB) for FCoE is defined in FC-BB-6
128 * Clause 7.11 in v1.04.
130 void __fcoe_get_lesb(struct fc_lport *lport,
131 struct fc_els_lesb *fc_lesb,
132 struct net_device *netdev)
134 unsigned int cpu;
135 u32 lfc, vlfc, mdac;
136 struct fc_stats *stats;
137 struct fcoe_fc_els_lesb *lesb;
138 struct rtnl_link_stats64 temp;
140 lfc = 0;
141 vlfc = 0;
142 mdac = 0;
143 lesb = (struct fcoe_fc_els_lesb *)fc_lesb;
144 memset(lesb, 0, sizeof(*lesb));
145 for_each_possible_cpu(cpu) {
146 stats = per_cpu_ptr(lport->stats, cpu);
147 lfc += stats->LinkFailureCount;
148 vlfc += stats->VLinkFailureCount;
149 mdac += stats->MissDiscAdvCount;
151 lesb->lesb_link_fail = htonl(lfc);
152 lesb->lesb_vlink_fail = htonl(vlfc);
153 lesb->lesb_miss_fka = htonl(mdac);
154 lesb->lesb_fcs_error =
155 htonl(dev_get_stats(netdev, &temp)->rx_crc_errors);
157 EXPORT_SYMBOL_GPL(__fcoe_get_lesb);
160 * fcoe_get_lesb() - Fill the FCoE Link Error Status Block
161 * @lport: the local port
162 * @fc_lesb: the link error status block
164 void fcoe_get_lesb(struct fc_lport *lport,
165 struct fc_els_lesb *fc_lesb)
167 struct net_device *netdev = fcoe_get_netdev(lport);
169 __fcoe_get_lesb(lport, fc_lesb, netdev);
171 EXPORT_SYMBOL_GPL(fcoe_get_lesb);
174 * fcoe_ctlr_get_lesb() - Get the Link Error Status Block (LESB) for a given
175 * fcoe controller device
176 * @ctlr_dev: The given fcoe controller device
179 void fcoe_ctlr_get_lesb(struct fcoe_ctlr_device *ctlr_dev)
181 struct fcoe_ctlr *fip = fcoe_ctlr_device_priv(ctlr_dev);
182 struct net_device *netdev = fcoe_get_netdev(fip->lp);
183 struct fc_els_lesb *fc_lesb;
185 fc_lesb = (struct fc_els_lesb *)(&ctlr_dev->lesb);
186 __fcoe_get_lesb(fip->lp, fc_lesb, netdev);
188 EXPORT_SYMBOL_GPL(fcoe_ctlr_get_lesb);
190 void fcoe_wwn_to_str(u64 wwn, char *buf, int len)
192 u8 wwpn[8];
194 u64_to_wwn(wwn, wwpn);
195 snprintf(buf, len, "%02x%02x%02x%02x%02x%02x%02x%02x",
196 wwpn[0], wwpn[1], wwpn[2], wwpn[3],
197 wwpn[4], wwpn[5], wwpn[6], wwpn[7]);
199 EXPORT_SYMBOL_GPL(fcoe_wwn_to_str);
202 * fcoe_validate_vport_create() - Validate a vport before creating it
203 * @vport: NPIV port to be created
205 * This routine is meant to add validation for a vport before creating it
206 * via fcoe_vport_create().
207 * Current validations are:
208 * - WWPN supplied is unique for given lport
210 int fcoe_validate_vport_create(struct fc_vport *vport)
212 struct Scsi_Host *shost = vport_to_shost(vport);
213 struct fc_lport *n_port = shost_priv(shost);
214 struct fc_lport *vn_port;
215 int rc = 0;
216 char buf[32];
218 mutex_lock(&n_port->lp_mutex);
220 fcoe_wwn_to_str(vport->port_name, buf, sizeof(buf));
221 /* Check if the wwpn is not same as that of the lport */
222 if (!memcmp(&n_port->wwpn, &vport->port_name, sizeof(u64))) {
223 LIBFCOE_TRANSPORT_DBG("vport WWPN 0x%s is same as that of the "
224 "base port WWPN\n", buf);
225 rc = -EINVAL;
226 goto out;
229 /* Check if there is any existing vport with same wwpn */
230 list_for_each_entry(vn_port, &n_port->vports, list) {
231 if (!memcmp(&vn_port->wwpn, &vport->port_name, sizeof(u64))) {
232 LIBFCOE_TRANSPORT_DBG("vport with given WWPN 0x%s "
233 "already exists\n", buf);
234 rc = -EINVAL;
235 break;
238 out:
239 mutex_unlock(&n_port->lp_mutex);
240 return rc;
242 EXPORT_SYMBOL_GPL(fcoe_validate_vport_create);
245 * fcoe_get_wwn() - Get the world wide name from LLD if it supports it
246 * @netdev: the associated net device
247 * @wwn: the output WWN
248 * @type: the type of WWN (WWPN or WWNN)
250 * Returns: 0 for success
252 int fcoe_get_wwn(struct net_device *netdev, u64 *wwn, int type)
254 const struct net_device_ops *ops = netdev->netdev_ops;
256 if (ops->ndo_fcoe_get_wwn)
257 return ops->ndo_fcoe_get_wwn(netdev, wwn, type);
258 return -EINVAL;
260 EXPORT_SYMBOL_GPL(fcoe_get_wwn);
263 * fcoe_fc_crc() - Calculates the CRC for a given frame
264 * @fp: The frame to be checksumed
266 * This uses crc32() routine to calculate the CRC for a frame
268 * Return: The 32 bit CRC value
270 u32 fcoe_fc_crc(struct fc_frame *fp)
272 struct sk_buff *skb = fp_skb(fp);
273 struct skb_frag_struct *frag;
274 unsigned char *data;
275 unsigned long off, len, clen;
276 u32 crc;
277 unsigned i;
279 crc = crc32(~0, skb->data, skb_headlen(skb));
281 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
282 frag = &skb_shinfo(skb)->frags[i];
283 off = frag->page_offset;
284 len = skb_frag_size(frag);
285 while (len > 0) {
286 clen = min(len, PAGE_SIZE - (off & ~PAGE_MASK));
287 data = kmap_atomic(
288 skb_frag_page(frag) + (off >> PAGE_SHIFT));
289 crc = crc32(crc, data + (off & ~PAGE_MASK), clen);
290 kunmap_atomic(data);
291 off += clen;
292 len -= clen;
295 return crc;
297 EXPORT_SYMBOL_GPL(fcoe_fc_crc);
300 * fcoe_start_io() - Start FCoE I/O
301 * @skb: The packet to be transmitted
303 * This routine is called from the net device to start transmitting
304 * FCoE packets.
306 * Returns: 0 for success
308 int fcoe_start_io(struct sk_buff *skb)
310 struct sk_buff *nskb;
311 int rc;
313 nskb = skb_clone(skb, GFP_ATOMIC);
314 if (!nskb)
315 return -ENOMEM;
316 rc = dev_queue_xmit(nskb);
317 if (rc != 0)
318 return rc;
319 kfree_skb(skb);
320 return 0;
322 EXPORT_SYMBOL_GPL(fcoe_start_io);
326 * fcoe_clean_pending_queue() - Dequeue a skb and free it
327 * @lport: The local port to dequeue a skb on
329 void fcoe_clean_pending_queue(struct fc_lport *lport)
331 struct fcoe_port *port = lport_priv(lport);
332 struct sk_buff *skb;
334 spin_lock_bh(&port->fcoe_pending_queue.lock);
335 while ((skb = __skb_dequeue(&port->fcoe_pending_queue)) != NULL) {
336 spin_unlock_bh(&port->fcoe_pending_queue.lock);
337 kfree_skb(skb);
338 spin_lock_bh(&port->fcoe_pending_queue.lock);
340 spin_unlock_bh(&port->fcoe_pending_queue.lock);
342 EXPORT_SYMBOL_GPL(fcoe_clean_pending_queue);
345 * fcoe_check_wait_queue() - Attempt to clear the transmit backlog
346 * @lport: The local port whose backlog is to be cleared
348 * This empties the wait_queue, dequeues the head of the wait_queue queue
349 * and calls fcoe_start_io() for each packet. If all skb have been
350 * transmitted it returns the qlen. If an error occurs it restores
351 * wait_queue (to try again later) and returns -1.
353 * The wait_queue is used when the skb transmit fails. The failed skb
354 * will go in the wait_queue which will be emptied by the timer function or
355 * by the next skb transmit.
357 void fcoe_check_wait_queue(struct fc_lport *lport, struct sk_buff *skb)
359 struct fcoe_port *port = lport_priv(lport);
360 int rc;
362 spin_lock_bh(&port->fcoe_pending_queue.lock);
364 if (skb)
365 __skb_queue_tail(&port->fcoe_pending_queue, skb);
367 if (port->fcoe_pending_queue_active)
368 goto out;
369 port->fcoe_pending_queue_active = 1;
371 while (port->fcoe_pending_queue.qlen) {
372 /* keep qlen > 0 until fcoe_start_io succeeds */
373 port->fcoe_pending_queue.qlen++;
374 skb = __skb_dequeue(&port->fcoe_pending_queue);
376 spin_unlock_bh(&port->fcoe_pending_queue.lock);
377 rc = fcoe_start_io(skb);
378 spin_lock_bh(&port->fcoe_pending_queue.lock);
380 if (rc) {
381 __skb_queue_head(&port->fcoe_pending_queue, skb);
382 /* undo temporary increment above */
383 port->fcoe_pending_queue.qlen--;
384 break;
386 /* undo temporary increment above */
387 port->fcoe_pending_queue.qlen--;
390 if (port->fcoe_pending_queue.qlen < port->min_queue_depth)
391 lport->qfull = 0;
392 if (port->fcoe_pending_queue.qlen && !timer_pending(&port->timer))
393 mod_timer(&port->timer, jiffies + 2);
394 port->fcoe_pending_queue_active = 0;
395 out:
396 if (port->fcoe_pending_queue.qlen > port->max_queue_depth)
397 lport->qfull = 1;
398 spin_unlock_bh(&port->fcoe_pending_queue.lock);
400 EXPORT_SYMBOL_GPL(fcoe_check_wait_queue);
403 * fcoe_queue_timer() - The fcoe queue timer
404 * @lport: The local port
406 * Calls fcoe_check_wait_queue on timeout
408 void fcoe_queue_timer(ulong lport)
410 fcoe_check_wait_queue((struct fc_lport *)lport, NULL);
412 EXPORT_SYMBOL_GPL(fcoe_queue_timer);
415 * fcoe_get_paged_crc_eof() - Allocate a page to be used for the trailer CRC
416 * @skb: The packet to be transmitted
417 * @tlen: The total length of the trailer
418 * @fps: The fcoe context
420 * This routine allocates a page for frame trailers. The page is re-used if
421 * there is enough room left on it for the current trailer. If there isn't
422 * enough buffer left a new page is allocated for the trailer. Reference to
423 * the page from this function as well as the skbs using the page fragments
424 * ensure that the page is freed at the appropriate time.
426 * Returns: 0 for success
428 int fcoe_get_paged_crc_eof(struct sk_buff *skb, int tlen,
429 struct fcoe_percpu_s *fps)
431 struct page *page;
433 page = fps->crc_eof_page;
434 if (!page) {
435 page = alloc_page(GFP_ATOMIC);
436 if (!page)
437 return -ENOMEM;
439 fps->crc_eof_page = page;
440 fps->crc_eof_offset = 0;
443 get_page(page);
444 skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags, page,
445 fps->crc_eof_offset, tlen);
446 skb->len += tlen;
447 skb->data_len += tlen;
448 skb->truesize += tlen;
449 fps->crc_eof_offset += sizeof(struct fcoe_crc_eof);
451 if (fps->crc_eof_offset >= PAGE_SIZE) {
452 fps->crc_eof_page = NULL;
453 fps->crc_eof_offset = 0;
454 put_page(page);
457 return 0;
459 EXPORT_SYMBOL_GPL(fcoe_get_paged_crc_eof);
462 * fcoe_transport_lookup - find an fcoe transport that matches a netdev
463 * @netdev: The netdev to look for from all attached transports
465 * Returns : ptr to the fcoe transport that supports this netdev or NULL
466 * if not found.
468 * The ft_mutex should be held when this is called
470 static struct fcoe_transport *fcoe_transport_lookup(struct net_device *netdev)
472 struct fcoe_transport *ft = NULL;
474 list_for_each_entry(ft, &fcoe_transports, list)
475 if (ft->match && ft->match(netdev))
476 return ft;
477 return NULL;
481 * fcoe_transport_attach - Attaches an FCoE transport
482 * @ft: The fcoe transport to be attached
484 * Returns : 0 for success
486 int fcoe_transport_attach(struct fcoe_transport *ft)
488 int rc = 0;
490 mutex_lock(&ft_mutex);
491 if (ft->attached) {
492 LIBFCOE_TRANSPORT_DBG("transport %s already attached\n",
493 ft->name);
494 rc = -EEXIST;
495 goto out_attach;
498 /* Add default transport to the tail */
499 if (strcmp(ft->name, FCOE_TRANSPORT_DEFAULT))
500 list_add(&ft->list, &fcoe_transports);
501 else
502 list_add_tail(&ft->list, &fcoe_transports);
504 ft->attached = true;
505 LIBFCOE_TRANSPORT_DBG("attaching transport %s\n", ft->name);
507 out_attach:
508 mutex_unlock(&ft_mutex);
509 return rc;
511 EXPORT_SYMBOL(fcoe_transport_attach);
514 * fcoe_transport_detach - Detaches an FCoE transport
515 * @ft: The fcoe transport to be attached
517 * Returns : 0 for success
519 int fcoe_transport_detach(struct fcoe_transport *ft)
521 int rc = 0;
522 struct fcoe_netdev_mapping *nm = NULL, *tmp;
524 mutex_lock(&ft_mutex);
525 if (!ft->attached) {
526 LIBFCOE_TRANSPORT_DBG("transport %s already detached\n",
527 ft->name);
528 rc = -ENODEV;
529 goto out_attach;
532 /* remove netdev mapping for this transport as it is going away */
533 mutex_lock(&fn_mutex);
534 list_for_each_entry_safe(nm, tmp, &fcoe_netdevs, list) {
535 if (nm->ft == ft) {
536 LIBFCOE_TRANSPORT_DBG("transport %s going away, "
537 "remove its netdev mapping for %s\n",
538 ft->name, nm->netdev->name);
539 list_del(&nm->list);
540 kfree(nm);
543 mutex_unlock(&fn_mutex);
545 list_del(&ft->list);
546 ft->attached = false;
547 LIBFCOE_TRANSPORT_DBG("detaching transport %s\n", ft->name);
549 out_attach:
550 mutex_unlock(&ft_mutex);
551 return rc;
554 EXPORT_SYMBOL(fcoe_transport_detach);
556 static int fcoe_transport_show(char *buffer, const struct kernel_param *kp)
558 int i, j;
559 struct fcoe_transport *ft = NULL;
561 i = j = sprintf(buffer, "Attached FCoE transports:");
562 mutex_lock(&ft_mutex);
563 list_for_each_entry(ft, &fcoe_transports, list) {
564 if (i >= PAGE_SIZE - IFNAMSIZ)
565 break;
566 i += snprintf(&buffer[i], IFNAMSIZ, "%s ", ft->name);
568 mutex_unlock(&ft_mutex);
569 if (i == j)
570 i += snprintf(&buffer[i], IFNAMSIZ, "none");
571 return i;
574 static int __init fcoe_transport_init(void)
576 register_netdevice_notifier(&libfcoe_notifier);
577 return 0;
580 static int fcoe_transport_exit(void)
582 struct fcoe_transport *ft;
584 unregister_netdevice_notifier(&libfcoe_notifier);
585 mutex_lock(&ft_mutex);
586 list_for_each_entry(ft, &fcoe_transports, list)
587 printk(KERN_ERR "FCoE transport %s is still attached!\n",
588 ft->name);
589 mutex_unlock(&ft_mutex);
590 return 0;
594 static int fcoe_add_netdev_mapping(struct net_device *netdev,
595 struct fcoe_transport *ft)
597 struct fcoe_netdev_mapping *nm;
599 nm = kmalloc(sizeof(*nm), GFP_KERNEL);
600 if (!nm) {
601 printk(KERN_ERR "Unable to allocate netdev_mapping");
602 return -ENOMEM;
605 nm->netdev = netdev;
606 nm->ft = ft;
608 mutex_lock(&fn_mutex);
609 list_add(&nm->list, &fcoe_netdevs);
610 mutex_unlock(&fn_mutex);
611 return 0;
615 static void fcoe_del_netdev_mapping(struct net_device *netdev)
617 struct fcoe_netdev_mapping *nm = NULL, *tmp;
619 mutex_lock(&fn_mutex);
620 list_for_each_entry_safe(nm, tmp, &fcoe_netdevs, list) {
621 if (nm->netdev == netdev) {
622 list_del(&nm->list);
623 kfree(nm);
624 mutex_unlock(&fn_mutex);
625 return;
628 mutex_unlock(&fn_mutex);
633 * fcoe_netdev_map_lookup - find the fcoe transport that matches the netdev on which
634 * it was created
636 * Returns : ptr to the fcoe transport that supports this netdev or NULL
637 * if not found.
639 * The ft_mutex should be held when this is called
641 static struct fcoe_transport *fcoe_netdev_map_lookup(struct net_device *netdev)
643 struct fcoe_transport *ft = NULL;
644 struct fcoe_netdev_mapping *nm;
646 mutex_lock(&fn_mutex);
647 list_for_each_entry(nm, &fcoe_netdevs, list) {
648 if (netdev == nm->netdev) {
649 ft = nm->ft;
650 mutex_unlock(&fn_mutex);
651 return ft;
655 mutex_unlock(&fn_mutex);
656 return NULL;
660 * fcoe_if_to_netdev() - Parse a name buffer to get a net device
661 * @buffer: The name of the net device
663 * Returns: NULL or a ptr to net_device
665 static struct net_device *fcoe_if_to_netdev(const char *buffer)
667 char *cp;
668 char ifname[IFNAMSIZ + 2];
670 if (buffer) {
671 strlcpy(ifname, buffer, IFNAMSIZ);
672 cp = ifname + strlen(ifname);
673 while (--cp >= ifname && *cp == '\n')
674 *cp = '\0';
675 return dev_get_by_name(&init_net, ifname);
677 return NULL;
681 * libfcoe_device_notification() - Handler for net device events
682 * @notifier: The context of the notification
683 * @event: The type of event
684 * @ptr: The net device that the event was on
686 * This function is called by the Ethernet driver in case of link change event.
688 * Returns: 0 for success
690 static int libfcoe_device_notification(struct notifier_block *notifier,
691 ulong event, void *ptr)
693 struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
695 switch (event) {
696 case NETDEV_UNREGISTER:
697 LIBFCOE_TRANSPORT_DBG("NETDEV_UNREGISTER %s\n",
698 netdev->name);
699 fcoe_del_netdev_mapping(netdev);
700 break;
702 return NOTIFY_OK;
705 ssize_t fcoe_ctlr_create_store(struct bus_type *bus,
706 const char *buf, size_t count)
708 struct net_device *netdev = NULL;
709 struct fcoe_transport *ft = NULL;
710 int rc = 0;
711 int err;
713 mutex_lock(&ft_mutex);
715 netdev = fcoe_if_to_netdev(buf);
716 if (!netdev) {
717 LIBFCOE_TRANSPORT_DBG("Invalid device %s.\n", buf);
718 rc = -ENODEV;
719 goto out_nodev;
722 ft = fcoe_netdev_map_lookup(netdev);
723 if (ft) {
724 LIBFCOE_TRANSPORT_DBG("transport %s already has existing "
725 "FCoE instance on %s.\n",
726 ft->name, netdev->name);
727 rc = -EEXIST;
728 goto out_putdev;
731 ft = fcoe_transport_lookup(netdev);
732 if (!ft) {
733 LIBFCOE_TRANSPORT_DBG("no FCoE transport found for %s.\n",
734 netdev->name);
735 rc = -ENODEV;
736 goto out_putdev;
739 /* pass to transport create */
740 err = ft->alloc ? ft->alloc(netdev) : -ENODEV;
741 if (err) {
742 fcoe_del_netdev_mapping(netdev);
743 rc = -ENOMEM;
744 goto out_putdev;
747 err = fcoe_add_netdev_mapping(netdev, ft);
748 if (err) {
749 LIBFCOE_TRANSPORT_DBG("failed to add new netdev mapping "
750 "for FCoE transport %s for %s.\n",
751 ft->name, netdev->name);
752 rc = -ENODEV;
753 goto out_putdev;
756 LIBFCOE_TRANSPORT_DBG("transport %s succeeded to create fcoe on %s.\n",
757 ft->name, netdev->name);
759 out_putdev:
760 dev_put(netdev);
761 out_nodev:
762 mutex_unlock(&ft_mutex);
763 if (rc)
764 return rc;
765 return count;
768 ssize_t fcoe_ctlr_destroy_store(struct bus_type *bus,
769 const char *buf, size_t count)
771 int rc = -ENODEV;
772 struct net_device *netdev = NULL;
773 struct fcoe_transport *ft = NULL;
775 mutex_lock(&ft_mutex);
777 netdev = fcoe_if_to_netdev(buf);
778 if (!netdev) {
779 LIBFCOE_TRANSPORT_DBG("invalid device %s.\n", buf);
780 goto out_nodev;
783 ft = fcoe_netdev_map_lookup(netdev);
784 if (!ft) {
785 LIBFCOE_TRANSPORT_DBG("no FCoE transport found for %s.\n",
786 netdev->name);
787 goto out_putdev;
790 /* pass to transport destroy */
791 rc = ft->destroy(netdev);
792 if (rc)
793 goto out_putdev;
795 fcoe_del_netdev_mapping(netdev);
796 LIBFCOE_TRANSPORT_DBG("transport %s %s to destroy fcoe on %s.\n",
797 ft->name, (rc) ? "failed" : "succeeded",
798 netdev->name);
799 rc = count; /* required for successful return */
800 out_putdev:
801 dev_put(netdev);
802 out_nodev:
803 mutex_unlock(&ft_mutex);
804 return rc;
806 EXPORT_SYMBOL(fcoe_ctlr_destroy_store);
809 * fcoe_transport_create() - Create a fcoe interface
810 * @buffer: The name of the Ethernet interface to create on
811 * @kp: The associated kernel param
813 * Called from sysfs. This holds the ft_mutex while calling the
814 * registered fcoe transport's create function.
816 * Returns: 0 for success
818 static int fcoe_transport_create(const char *buffer, struct kernel_param *kp)
820 int rc = -ENODEV;
821 struct net_device *netdev = NULL;
822 struct fcoe_transport *ft = NULL;
823 enum fip_state fip_mode = (enum fip_state)(long)kp->arg;
825 mutex_lock(&ft_mutex);
827 netdev = fcoe_if_to_netdev(buffer);
828 if (!netdev) {
829 LIBFCOE_TRANSPORT_DBG("Invalid device %s.\n", buffer);
830 goto out_nodev;
833 ft = fcoe_netdev_map_lookup(netdev);
834 if (ft) {
835 LIBFCOE_TRANSPORT_DBG("transport %s already has existing "
836 "FCoE instance on %s.\n",
837 ft->name, netdev->name);
838 rc = -EEXIST;
839 goto out_putdev;
842 ft = fcoe_transport_lookup(netdev);
843 if (!ft) {
844 LIBFCOE_TRANSPORT_DBG("no FCoE transport found for %s.\n",
845 netdev->name);
846 goto out_putdev;
849 rc = fcoe_add_netdev_mapping(netdev, ft);
850 if (rc) {
851 LIBFCOE_TRANSPORT_DBG("failed to add new netdev mapping "
852 "for FCoE transport %s for %s.\n",
853 ft->name, netdev->name);
854 goto out_putdev;
857 /* pass to transport create */
858 rc = ft->create ? ft->create(netdev, fip_mode) : -ENODEV;
859 if (rc)
860 fcoe_del_netdev_mapping(netdev);
862 LIBFCOE_TRANSPORT_DBG("transport %s %s to create fcoe on %s.\n",
863 ft->name, (rc) ? "failed" : "succeeded",
864 netdev->name);
866 out_putdev:
867 dev_put(netdev);
868 out_nodev:
869 mutex_unlock(&ft_mutex);
870 return rc;
874 * fcoe_transport_destroy() - Destroy a FCoE interface
875 * @buffer: The name of the Ethernet interface to be destroyed
876 * @kp: The associated kernel parameter
878 * Called from sysfs. This holds the ft_mutex while calling the
879 * registered fcoe transport's destroy function.
881 * Returns: 0 for success
883 static int fcoe_transport_destroy(const char *buffer, struct kernel_param *kp)
885 int rc = -ENODEV;
886 struct net_device *netdev = NULL;
887 struct fcoe_transport *ft = NULL;
889 mutex_lock(&ft_mutex);
891 netdev = fcoe_if_to_netdev(buffer);
892 if (!netdev) {
893 LIBFCOE_TRANSPORT_DBG("invalid device %s.\n", buffer);
894 goto out_nodev;
897 ft = fcoe_netdev_map_lookup(netdev);
898 if (!ft) {
899 LIBFCOE_TRANSPORT_DBG("no FCoE transport found for %s.\n",
900 netdev->name);
901 goto out_putdev;
904 /* pass to transport destroy */
905 rc = ft->destroy ? ft->destroy(netdev) : -ENODEV;
906 fcoe_del_netdev_mapping(netdev);
907 LIBFCOE_TRANSPORT_DBG("transport %s %s to destroy fcoe on %s.\n",
908 ft->name, (rc) ? "failed" : "succeeded",
909 netdev->name);
911 out_putdev:
912 dev_put(netdev);
913 out_nodev:
914 mutex_unlock(&ft_mutex);
915 return rc;
919 * fcoe_transport_disable() - Disables a FCoE interface
920 * @buffer: The name of the Ethernet interface to be disabled
921 * @kp: The associated kernel parameter
923 * Called from sysfs.
925 * Returns: 0 for success
927 static int fcoe_transport_disable(const char *buffer, struct kernel_param *kp)
929 int rc = -ENODEV;
930 struct net_device *netdev = NULL;
931 struct fcoe_transport *ft = NULL;
933 mutex_lock(&ft_mutex);
935 netdev = fcoe_if_to_netdev(buffer);
936 if (!netdev)
937 goto out_nodev;
939 ft = fcoe_netdev_map_lookup(netdev);
940 if (!ft)
941 goto out_putdev;
943 rc = ft->disable ? ft->disable(netdev) : -ENODEV;
945 out_putdev:
946 dev_put(netdev);
947 out_nodev:
948 mutex_unlock(&ft_mutex);
949 return rc;
953 * fcoe_transport_enable() - Enables a FCoE interface
954 * @buffer: The name of the Ethernet interface to be enabled
955 * @kp: The associated kernel parameter
957 * Called from sysfs.
959 * Returns: 0 for success
961 static int fcoe_transport_enable(const char *buffer, struct kernel_param *kp)
963 int rc = -ENODEV;
964 struct net_device *netdev = NULL;
965 struct fcoe_transport *ft = NULL;
967 mutex_lock(&ft_mutex);
969 netdev = fcoe_if_to_netdev(buffer);
970 if (!netdev)
971 goto out_nodev;
973 ft = fcoe_netdev_map_lookup(netdev);
974 if (!ft)
975 goto out_putdev;
977 rc = ft->enable ? ft->enable(netdev) : -ENODEV;
979 out_putdev:
980 dev_put(netdev);
981 out_nodev:
982 mutex_unlock(&ft_mutex);
983 return rc;
987 * libfcoe_init() - Initialization routine for libfcoe.ko
989 static int __init libfcoe_init(void)
991 int rc = 0;
993 rc = fcoe_transport_init();
994 if (rc)
995 return rc;
997 rc = fcoe_sysfs_setup();
998 if (rc)
999 fcoe_transport_exit();
1001 return rc;
1003 module_init(libfcoe_init);
1006 * libfcoe_exit() - Tear down libfcoe.ko
1008 static void __exit libfcoe_exit(void)
1010 fcoe_sysfs_teardown();
1011 fcoe_transport_exit();
1013 module_exit(libfcoe_exit);