1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Xenbus code for netif backend
6 * Copyright (C) 2005 XenSource Ltd
10 #include <linux/vmalloc.h>
11 #include <linux/rtnetlink.h>
13 static int connect_data_rings(struct backend_info *be,
14 struct xenvif_queue *queue);
15 static void connect(struct backend_info *be);
16 static int read_xenbus_vif_flags(struct backend_info *be);
17 static int backend_create_xenvif(struct backend_info *be);
18 static void unregister_hotplug_status_watch(struct backend_info *be);
19 static void xen_unregister_watchers(struct xenvif *vif);
20 static void set_backend_state(struct backend_info *be,
21 enum xenbus_state state);
23 #ifdef CONFIG_DEBUG_FS
24 struct dentry *xen_netback_dbg_root = NULL;
26 static int xenvif_read_io_ring(struct seq_file *m, void *v)
28 struct xenvif_queue *queue = m->private;
29 struct xen_netif_tx_back_ring *tx_ring = &queue->tx;
30 struct xen_netif_rx_back_ring *rx_ring = &queue->rx;
31 struct netdev_queue *dev_queue;
34 struct xen_netif_tx_sring *sring = tx_ring->sring;
36 seq_printf(m, "Queue %d\nTX: nr_ents %u\n", queue->id,
38 seq_printf(m, "req prod %u (%d) cons %u (%d) event %u (%d)\n",
40 sring->req_prod - sring->rsp_prod,
42 tx_ring->req_cons - sring->rsp_prod,
44 sring->req_event - sring->rsp_prod);
45 seq_printf(m, "rsp prod %u (base) pvt %u (%d) event %u (%d)\n",
47 tx_ring->rsp_prod_pvt,
48 tx_ring->rsp_prod_pvt - sring->rsp_prod,
50 sring->rsp_event - sring->rsp_prod);
51 seq_printf(m, "pending prod %u pending cons %u nr_pending_reqs %u\n",
54 nr_pending_reqs(queue));
55 seq_printf(m, "dealloc prod %u dealloc cons %u dealloc_queue %u\n\n",
58 queue->dealloc_prod - queue->dealloc_cons);
62 struct xen_netif_rx_sring *sring = rx_ring->sring;
64 seq_printf(m, "RX: nr_ents %u\n", rx_ring->nr_ents);
65 seq_printf(m, "req prod %u (%d) cons %u (%d) event %u (%d)\n",
67 sring->req_prod - sring->rsp_prod,
69 rx_ring->req_cons - sring->rsp_prod,
71 sring->req_event - sring->rsp_prod);
72 seq_printf(m, "rsp prod %u (base) pvt %u (%d) event %u (%d)\n\n",
74 rx_ring->rsp_prod_pvt,
75 rx_ring->rsp_prod_pvt - sring->rsp_prod,
77 sring->rsp_event - sring->rsp_prod);
80 seq_printf(m, "NAPI state: %lx NAPI weight: %d TX queue len %u\n"
81 "Credit timer_pending: %d, credit: %lu, usec: %lu\n"
82 "remaining: %lu, expires: %lu, now: %lu\n",
83 queue->napi.state, queue->napi.weight,
84 skb_queue_len(&queue->tx_queue),
85 timer_pending(&queue->credit_timeout),
88 queue->remaining_credit,
89 queue->credit_timeout.expires,
92 dev_queue = netdev_get_tx_queue(queue->vif->dev, queue->id);
94 seq_printf(m, "\nRx internal queue: len %u max %u pkts %u %s\n",
95 queue->rx_queue_len, queue->rx_queue_max,
96 skb_queue_len(&queue->rx_queue),
97 netif_tx_queue_stopped(dev_queue) ? "stopped" : "running");
102 #define XENVIF_KICK_STR "kick"
103 #define BUFFER_SIZE 32
106 xenvif_write_io_ring(struct file *filp, const char __user *buf, size_t count,
109 struct xenvif_queue *queue =
110 ((struct seq_file *)filp->private_data)->private;
112 char write[BUFFER_SIZE];
114 /* don't allow partial writes and check the length */
117 if (count >= sizeof(write))
120 len = simple_write_to_buffer(write,
130 if (!strncmp(write, XENVIF_KICK_STR, sizeof(XENVIF_KICK_STR) - 1))
131 xenvif_interrupt(0, (void *)queue);
133 pr_warn("Unknown command to io_ring_q%d. Available: kick\n",
140 static int xenvif_io_ring_open(struct inode *inode, struct file *filp)
145 if (inode->i_private)
146 queue = inode->i_private;
147 ret = single_open(filp, xenvif_read_io_ring, queue);
148 filp->f_mode |= FMODE_PWRITE;
152 static const struct file_operations xenvif_dbg_io_ring_ops_fops = {
153 .owner = THIS_MODULE,
154 .open = xenvif_io_ring_open,
157 .release = single_release,
158 .write = xenvif_write_io_ring,
161 static int xenvif_ctrl_show(struct seq_file *m, void *v)
163 struct xenvif *vif = m->private;
165 xenvif_dump_hash_info(vif, m);
169 DEFINE_SHOW_ATTRIBUTE(xenvif_ctrl);
171 static void xenvif_debugfs_addif(struct xenvif *vif)
173 struct dentry *pfile;
176 if (IS_ERR_OR_NULL(xen_netback_dbg_root))
179 vif->xenvif_dbg_root = debugfs_create_dir(vif->dev->name,
180 xen_netback_dbg_root);
181 if (!IS_ERR_OR_NULL(vif->xenvif_dbg_root)) {
182 for (i = 0; i < vif->num_queues; ++i) {
183 char filename[sizeof("io_ring_q") + 4];
185 snprintf(filename, sizeof(filename), "io_ring_q%d", i);
186 pfile = debugfs_create_file(filename,
188 vif->xenvif_dbg_root,
190 &xenvif_dbg_io_ring_ops_fops);
191 if (IS_ERR_OR_NULL(pfile))
192 pr_warn("Creation of io_ring file returned %ld!\n",
197 pfile = debugfs_create_file("ctrl",
199 vif->xenvif_dbg_root,
202 if (IS_ERR_OR_NULL(pfile))
203 pr_warn("Creation of ctrl file returned %ld!\n",
207 netdev_warn(vif->dev,
208 "Creation of vif debugfs dir returned %ld!\n",
209 PTR_ERR(vif->xenvif_dbg_root));
212 static void xenvif_debugfs_delif(struct xenvif *vif)
214 if (IS_ERR_OR_NULL(xen_netback_dbg_root))
217 debugfs_remove_recursive(vif->xenvif_dbg_root);
218 vif->xenvif_dbg_root = NULL;
220 #endif /* CONFIG_DEBUG_FS */
222 static int netback_remove(struct xenbus_device *dev)
224 struct backend_info *be = dev_get_drvdata(&dev->dev);
226 set_backend_state(be, XenbusStateClosed);
228 unregister_hotplug_status_watch(be);
230 kobject_uevent(&dev->dev.kobj, KOBJ_OFFLINE);
231 xen_unregister_watchers(be->vif);
232 xenbus_rm(XBT_NIL, dev->nodename, "hotplug-status");
233 xenvif_free(be->vif);
236 kfree(be->hotplug_script);
238 dev_set_drvdata(&dev->dev, NULL);
244 * Entry point to this code when a new device is created. Allocate the basic
245 * structures and switch to InitWait.
247 static int netback_probe(struct xenbus_device *dev,
248 const struct xenbus_device_id *id)
251 struct xenbus_transaction xbt;
255 struct backend_info *be = kzalloc(sizeof(struct backend_info),
258 xenbus_dev_fatal(dev, -ENOMEM,
259 "allocating backend structure");
264 dev_set_drvdata(&dev->dev, be);
266 be->state = XenbusStateInitialising;
267 err = xenbus_switch_state(dev, XenbusStateInitialising);
274 err = xenbus_transaction_start(&xbt);
276 xenbus_dev_fatal(dev, err, "starting transaction");
280 err = xenbus_printf(xbt, dev->nodename, "feature-sg", "%d", sg);
282 message = "writing feature-sg";
283 goto abort_transaction;
286 err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv4",
289 message = "writing feature-gso-tcpv4";
290 goto abort_transaction;
293 err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv6",
296 message = "writing feature-gso-tcpv6";
297 goto abort_transaction;
300 /* We support partial checksum setup for IPv6 packets */
301 err = xenbus_printf(xbt, dev->nodename,
302 "feature-ipv6-csum-offload",
305 message = "writing feature-ipv6-csum-offload";
306 goto abort_transaction;
309 /* We support rx-copy path. */
310 err = xenbus_printf(xbt, dev->nodename,
311 "feature-rx-copy", "%d", 1);
313 message = "writing feature-rx-copy";
314 goto abort_transaction;
318 * We don't support rx-flip path (except old guests who don't
319 * grok this feature flag).
321 err = xenbus_printf(xbt, dev->nodename,
322 "feature-rx-flip", "%d", 0);
324 message = "writing feature-rx-flip";
325 goto abort_transaction;
328 /* We support dynamic multicast-control. */
329 err = xenbus_printf(xbt, dev->nodename,
330 "feature-multicast-control", "%d", 1);
332 message = "writing feature-multicast-control";
333 goto abort_transaction;
336 err = xenbus_printf(xbt, dev->nodename,
337 "feature-dynamic-multicast-control",
340 message = "writing feature-dynamic-multicast-control";
341 goto abort_transaction;
344 err = xenbus_transaction_end(xbt, 0);
345 } while (err == -EAGAIN);
348 xenbus_dev_fatal(dev, err, "completing transaction");
353 * Split event channels support, this is optional so it is not
354 * put inside the above loop.
356 err = xenbus_printf(XBT_NIL, dev->nodename,
357 "feature-split-event-channels",
358 "%u", separate_tx_rx_irq);
360 pr_debug("Error writing feature-split-event-channels\n");
362 /* Multi-queue support: This is an optional feature. */
363 err = xenbus_printf(XBT_NIL, dev->nodename,
364 "multi-queue-max-queues", "%u", xenvif_max_queues);
366 pr_debug("Error writing multi-queue-max-queues\n");
368 err = xenbus_printf(XBT_NIL, dev->nodename,
372 pr_debug("Error writing feature-ctrl-ring\n");
374 script = xenbus_read(XBT_NIL, dev->nodename, "script", NULL);
375 if (IS_ERR(script)) {
376 err = PTR_ERR(script);
377 xenbus_dev_fatal(dev, err, "reading script");
381 be->hotplug_script = script;
384 /* This kicks hotplug scripts, so do it immediately. */
385 err = backend_create_xenvif(be);
392 xenbus_transaction_end(xbt, 1);
393 xenbus_dev_fatal(dev, err, "%s", message);
395 pr_debug("failed\n");
402 * Handle the creation of the hotplug script environment. We add the script
403 * and vif variables to the environment, for the benefit of the vif-* hotplug
406 static int netback_uevent(struct xenbus_device *xdev,
407 struct kobj_uevent_env *env)
409 struct backend_info *be = dev_get_drvdata(&xdev->dev);
414 if (add_uevent_var(env, "script=%s", be->hotplug_script))
420 return add_uevent_var(env, "vif=%s", be->vif->dev->name);
424 static int backend_create_xenvif(struct backend_info *be)
428 struct xenbus_device *dev = be->dev;
434 err = xenbus_scanf(XBT_NIL, dev->nodename, "handle", "%li", &handle);
436 xenbus_dev_fatal(dev, err, "reading handle");
437 return (err < 0) ? err : -EINVAL;
440 vif = xenvif_alloc(&dev->dev, dev->otherend_id, handle);
443 xenbus_dev_fatal(dev, err, "creating interface");
449 kobject_uevent(&dev->dev.kobj, KOBJ_ONLINE);
453 static void backend_disconnect(struct backend_info *be)
455 struct xenvif *vif = be->vif;
458 unsigned int num_queues = vif->num_queues;
459 unsigned int queue_index;
461 xen_unregister_watchers(vif);
462 #ifdef CONFIG_DEBUG_FS
463 xenvif_debugfs_delif(vif);
464 #endif /* CONFIG_DEBUG_FS */
465 xenvif_disconnect_data(vif);
467 /* At this point some of the handlers may still be active
468 * so we need to have additional synchronization here.
473 for (queue_index = 0; queue_index < num_queues; ++queue_index)
474 xenvif_deinit_queue(&vif->queues[queue_index]);
479 xenvif_disconnect_ctrl(vif);
483 static void backend_connect(struct backend_info *be)
489 static inline void backend_switch_state(struct backend_info *be,
490 enum xenbus_state state)
492 struct xenbus_device *dev = be->dev;
494 pr_debug("%s -> %s\n", dev->nodename, xenbus_strstate(state));
497 /* If we are waiting for a hotplug script then defer the
498 * actual xenbus state change.
500 if (!be->have_hotplug_status_watch)
501 xenbus_switch_state(dev, state);
504 /* Handle backend state transitions:
506 * The backend state starts in Initialising and the following transitions are
509 * Initialising -> InitWait -> Connected
521 * The state argument specifies the eventual state of the backend and the
522 * function transitions to that state via the shortest path.
524 static void set_backend_state(struct backend_info *be,
525 enum xenbus_state state)
527 while (be->state != state) {
529 case XenbusStateInitialising:
531 case XenbusStateInitWait:
532 case XenbusStateConnected:
533 case XenbusStateClosing:
534 backend_switch_state(be, XenbusStateInitWait);
536 case XenbusStateClosed:
537 backend_switch_state(be, XenbusStateClosed);
543 case XenbusStateClosed:
545 case XenbusStateInitWait:
546 case XenbusStateConnected:
547 backend_switch_state(be, XenbusStateInitWait);
549 case XenbusStateClosing:
550 backend_switch_state(be, XenbusStateClosing);
556 case XenbusStateInitWait:
558 case XenbusStateConnected:
560 backend_switch_state(be, XenbusStateConnected);
562 case XenbusStateClosing:
563 case XenbusStateClosed:
564 backend_switch_state(be, XenbusStateClosing);
570 case XenbusStateConnected:
572 case XenbusStateInitWait:
573 case XenbusStateClosing:
574 case XenbusStateClosed:
575 backend_disconnect(be);
576 backend_switch_state(be, XenbusStateClosing);
582 case XenbusStateClosing:
584 case XenbusStateInitWait:
585 case XenbusStateConnected:
586 case XenbusStateClosed:
587 backend_switch_state(be, XenbusStateClosed);
600 * Callback received when the frontend's state changes.
602 static void frontend_changed(struct xenbus_device *dev,
603 enum xenbus_state frontend_state)
605 struct backend_info *be = dev_get_drvdata(&dev->dev);
607 pr_debug("%s -> %s\n", dev->otherend, xenbus_strstate(frontend_state));
609 be->frontend_state = frontend_state;
611 switch (frontend_state) {
612 case XenbusStateInitialising:
613 set_backend_state(be, XenbusStateInitWait);
616 case XenbusStateInitialised:
619 case XenbusStateConnected:
620 set_backend_state(be, XenbusStateConnected);
623 case XenbusStateClosing:
624 set_backend_state(be, XenbusStateClosing);
627 case XenbusStateClosed:
628 set_backend_state(be, XenbusStateClosed);
629 if (xenbus_dev_is_online(dev))
631 /* fall through - if not online */
632 case XenbusStateUnknown:
633 set_backend_state(be, XenbusStateClosed);
634 device_unregister(&dev->dev);
638 xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
645 static void xen_net_read_rate(struct xenbus_device *dev,
646 unsigned long *bytes, unsigned long *usec)
652 /* Default to unlimited bandwidth. */
656 ratestr = xenbus_read(XBT_NIL, dev->nodename, "rate", NULL);
661 b = simple_strtoul(s, &e, 10);
662 if ((s == e) || (*e != ','))
666 u = simple_strtoul(s, &e, 10);
667 if ((s == e) || (*e != '\0'))
677 pr_warn("Failed to parse network rate limit. Traffic unlimited.\n");
681 static int xen_net_read_mac(struct xenbus_device *dev, u8 mac[])
683 char *s, *e, *macstr;
686 macstr = s = xenbus_read(XBT_NIL, dev->nodename, "mac", NULL);
688 return PTR_ERR(macstr);
690 for (i = 0; i < ETH_ALEN; i++) {
691 mac[i] = simple_strtoul(s, &e, 16);
692 if ((s == e) || (*e != ((i == ETH_ALEN-1) ? '\0' : ':'))) {
703 static void xen_net_rate_changed(struct xenbus_watch *watch,
704 const char *path, const char *token)
706 struct xenvif *vif = container_of(watch, struct xenvif, credit_watch);
707 struct xenbus_device *dev = xenvif_to_xenbus_device(vif);
708 unsigned long credit_bytes;
709 unsigned long credit_usec;
710 unsigned int queue_index;
712 xen_net_read_rate(dev, &credit_bytes, &credit_usec);
713 for (queue_index = 0; queue_index < vif->num_queues; queue_index++) {
714 struct xenvif_queue *queue = &vif->queues[queue_index];
716 queue->credit_bytes = credit_bytes;
717 queue->credit_usec = credit_usec;
718 if (!mod_timer_pending(&queue->credit_timeout, jiffies) &&
719 queue->remaining_credit > queue->credit_bytes) {
720 queue->remaining_credit = queue->credit_bytes;
725 static int xen_register_credit_watch(struct xenbus_device *dev,
730 unsigned maxlen = strlen(dev->nodename) + sizeof("/rate");
732 if (vif->credit_watch.node)
735 node = kmalloc(maxlen, GFP_KERNEL);
738 snprintf(node, maxlen, "%s/rate", dev->nodename);
739 vif->credit_watch.node = node;
740 vif->credit_watch.callback = xen_net_rate_changed;
741 err = register_xenbus_watch(&vif->credit_watch);
743 pr_err("Failed to set watcher %s\n", vif->credit_watch.node);
745 vif->credit_watch.node = NULL;
746 vif->credit_watch.callback = NULL;
751 static void xen_unregister_credit_watch(struct xenvif *vif)
753 if (vif->credit_watch.node) {
754 unregister_xenbus_watch(&vif->credit_watch);
755 kfree(vif->credit_watch.node);
756 vif->credit_watch.node = NULL;
760 static void xen_mcast_ctrl_changed(struct xenbus_watch *watch,
761 const char *path, const char *token)
763 struct xenvif *vif = container_of(watch, struct xenvif,
765 struct xenbus_device *dev = xenvif_to_xenbus_device(vif);
767 vif->multicast_control = !!xenbus_read_unsigned(dev->otherend,
768 "request-multicast-control", 0);
771 static int xen_register_mcast_ctrl_watch(struct xenbus_device *dev,
776 unsigned maxlen = strlen(dev->otherend) +
777 sizeof("/request-multicast-control");
779 if (vif->mcast_ctrl_watch.node) {
780 pr_err_ratelimited("Watch is already registered\n");
784 node = kmalloc(maxlen, GFP_KERNEL);
786 pr_err("Failed to allocate memory for watch\n");
789 snprintf(node, maxlen, "%s/request-multicast-control",
791 vif->mcast_ctrl_watch.node = node;
792 vif->mcast_ctrl_watch.callback = xen_mcast_ctrl_changed;
793 err = register_xenbus_watch(&vif->mcast_ctrl_watch);
795 pr_err("Failed to set watcher %s\n",
796 vif->mcast_ctrl_watch.node);
798 vif->mcast_ctrl_watch.node = NULL;
799 vif->mcast_ctrl_watch.callback = NULL;
804 static void xen_unregister_mcast_ctrl_watch(struct xenvif *vif)
806 if (vif->mcast_ctrl_watch.node) {
807 unregister_xenbus_watch(&vif->mcast_ctrl_watch);
808 kfree(vif->mcast_ctrl_watch.node);
809 vif->mcast_ctrl_watch.node = NULL;
813 static void xen_register_watchers(struct xenbus_device *dev,
816 xen_register_credit_watch(dev, vif);
817 xen_register_mcast_ctrl_watch(dev, vif);
820 static void xen_unregister_watchers(struct xenvif *vif)
822 xen_unregister_mcast_ctrl_watch(vif);
823 xen_unregister_credit_watch(vif);
826 static void unregister_hotplug_status_watch(struct backend_info *be)
828 if (be->have_hotplug_status_watch) {
829 unregister_xenbus_watch(&be->hotplug_status_watch);
830 kfree(be->hotplug_status_watch.node);
832 be->have_hotplug_status_watch = 0;
835 static void hotplug_status_changed(struct xenbus_watch *watch,
839 struct backend_info *be = container_of(watch,
841 hotplug_status_watch);
845 str = xenbus_read(XBT_NIL, be->dev->nodename, "hotplug-status", &len);
848 if (len == sizeof("connected")-1 && !memcmp(str, "connected", len)) {
849 /* Complete any pending state change */
850 xenbus_switch_state(be->dev, be->state);
852 /* Not interested in this watch anymore. */
853 unregister_hotplug_status_watch(be);
858 static int connect_ctrl_ring(struct backend_info *be)
860 struct xenbus_device *dev = be->dev;
861 struct xenvif *vif = be->vif;
863 grant_ref_t ring_ref;
867 err = xenbus_scanf(XBT_NIL, dev->otherend,
868 "ctrl-ring-ref", "%u", &val);
870 goto done; /* The frontend does not have a control ring */
874 err = xenbus_scanf(XBT_NIL, dev->otherend,
875 "event-channel-ctrl", "%u", &val);
877 xenbus_dev_fatal(dev, err,
878 "reading %s/event-channel-ctrl",
885 err = xenvif_connect_ctrl(vif, ring_ref, evtchn);
887 xenbus_dev_fatal(dev, err,
888 "mapping shared-frame %u port %u",
900 static void connect(struct backend_info *be)
903 struct xenbus_device *dev = be->dev;
904 unsigned long credit_bytes, credit_usec;
905 unsigned int queue_index;
906 unsigned int requested_num_queues;
907 struct xenvif_queue *queue;
909 /* Check whether the frontend requested multiple queues
910 * and read the number requested.
912 requested_num_queues = xenbus_read_unsigned(dev->otherend,
913 "multi-queue-num-queues", 1);
914 if (requested_num_queues > xenvif_max_queues) {
915 /* buggy or malicious guest */
916 xenbus_dev_fatal(dev, -EINVAL,
917 "guest requested %u queues, exceeding the maximum of %u.",
918 requested_num_queues, xenvif_max_queues);
922 err = xen_net_read_mac(dev, be->vif->fe_dev_addr);
924 xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename);
928 xen_net_read_rate(dev, &credit_bytes, &credit_usec);
929 xen_unregister_watchers(be->vif);
930 xen_register_watchers(dev, be->vif);
931 read_xenbus_vif_flags(be);
933 err = connect_ctrl_ring(be);
935 xenbus_dev_fatal(dev, err, "connecting control ring");
939 /* Use the number of queues requested by the frontend */
940 be->vif->queues = vzalloc(array_size(requested_num_queues,
941 sizeof(struct xenvif_queue)));
942 if (!be->vif->queues) {
943 xenbus_dev_fatal(dev, -ENOMEM,
944 "allocating queues");
948 be->vif->num_queues = requested_num_queues;
949 be->vif->stalled_queues = requested_num_queues;
951 for (queue_index = 0; queue_index < requested_num_queues; ++queue_index) {
952 queue = &be->vif->queues[queue_index];
953 queue->vif = be->vif;
954 queue->id = queue_index;
955 snprintf(queue->name, sizeof(queue->name), "%s-q%u",
956 be->vif->dev->name, queue->id);
958 err = xenvif_init_queue(queue);
960 /* xenvif_init_queue() cleans up after itself on
961 * failure, but we need to clean up any previously
962 * initialised queues. Set num_queues to i so that
963 * earlier queues can be destroyed using the regular
966 be->vif->num_queues = queue_index;
970 queue->credit_bytes = credit_bytes;
971 queue->remaining_credit = credit_bytes;
972 queue->credit_usec = credit_usec;
974 err = connect_data_rings(be, queue);
976 /* connect_data_rings() cleans up after itself on
977 * failure, but we need to clean up after
978 * xenvif_init_queue() here, and also clean up any
979 * previously initialised queues.
981 xenvif_deinit_queue(queue);
982 be->vif->num_queues = queue_index;
987 #ifdef CONFIG_DEBUG_FS
988 xenvif_debugfs_addif(be->vif);
989 #endif /* CONFIG_DEBUG_FS */
991 /* Initialisation completed, tell core driver the number of
995 netif_set_real_num_tx_queues(be->vif->dev, requested_num_queues);
996 netif_set_real_num_rx_queues(be->vif->dev, requested_num_queues);
999 xenvif_carrier_on(be->vif);
1001 unregister_hotplug_status_watch(be);
1002 err = xenbus_watch_pathfmt(dev, &be->hotplug_status_watch,
1003 hotplug_status_changed,
1004 "%s/%s", dev->nodename, "hotplug-status");
1006 be->have_hotplug_status_watch = 1;
1008 netif_tx_wake_all_queues(be->vif->dev);
1013 if (be->vif->num_queues > 0)
1014 xenvif_disconnect_data(be->vif); /* Clean up existing queues */
1015 for (queue_index = 0; queue_index < be->vif->num_queues; ++queue_index)
1016 xenvif_deinit_queue(&be->vif->queues[queue_index]);
1017 vfree(be->vif->queues);
1018 be->vif->queues = NULL;
1019 be->vif->num_queues = 0;
1020 xenvif_disconnect_ctrl(be->vif);
1025 static int connect_data_rings(struct backend_info *be,
1026 struct xenvif_queue *queue)
1028 struct xenbus_device *dev = be->dev;
1029 unsigned int num_queues = queue->vif->num_queues;
1030 unsigned long tx_ring_ref, rx_ring_ref;
1031 unsigned int tx_evtchn, rx_evtchn;
1035 const size_t xenstore_path_ext_size = 11; /* sufficient for "/queue-NNN" */
1037 /* If the frontend requested 1 queue, or we have fallen back
1038 * to single queue due to lack of frontend support for multi-
1039 * queue, expect the remaining XenStore keys in the toplevel
1040 * directory. Otherwise, expect them in a subdirectory called
1043 if (num_queues == 1) {
1044 xspath = kzalloc(strlen(dev->otherend) + 1, GFP_KERNEL);
1046 xenbus_dev_fatal(dev, -ENOMEM,
1047 "reading ring references");
1050 strcpy(xspath, dev->otherend);
1052 xspathsize = strlen(dev->otherend) + xenstore_path_ext_size;
1053 xspath = kzalloc(xspathsize, GFP_KERNEL);
1055 xenbus_dev_fatal(dev, -ENOMEM,
1056 "reading ring references");
1059 snprintf(xspath, xspathsize, "%s/queue-%u", dev->otherend,
1063 err = xenbus_gather(XBT_NIL, xspath,
1064 "tx-ring-ref", "%lu", &tx_ring_ref,
1065 "rx-ring-ref", "%lu", &rx_ring_ref, NULL);
1067 xenbus_dev_fatal(dev, err,
1068 "reading %s/ring-ref",
1073 /* Try split event channels first, then single event channel. */
1074 err = xenbus_gather(XBT_NIL, xspath,
1075 "event-channel-tx", "%u", &tx_evtchn,
1076 "event-channel-rx", "%u", &rx_evtchn, NULL);
1078 err = xenbus_scanf(XBT_NIL, xspath,
1079 "event-channel", "%u", &tx_evtchn);
1081 xenbus_dev_fatal(dev, err,
1082 "reading %s/event-channel(-tx/rx)",
1086 rx_evtchn = tx_evtchn;
1089 /* Map the shared frame, irq etc. */
1090 err = xenvif_connect_data(queue, tx_ring_ref, rx_ring_ref,
1091 tx_evtchn, rx_evtchn);
1093 xenbus_dev_fatal(dev, err,
1094 "mapping shared-frames %lu/%lu port tx %u rx %u",
1095 tx_ring_ref, rx_ring_ref,
1096 tx_evtchn, rx_evtchn);
1101 err: /* Regular return falls through with err == 0 */
1106 static int read_xenbus_vif_flags(struct backend_info *be)
1108 struct xenvif *vif = be->vif;
1109 struct xenbus_device *dev = be->dev;
1110 unsigned int rx_copy;
1113 err = xenbus_scanf(XBT_NIL, dev->otherend, "request-rx-copy", "%u",
1115 if (err == -ENOENT) {
1120 xenbus_dev_fatal(dev, err, "reading %s/request-rx-copy",
1127 if (!xenbus_read_unsigned(dev->otherend, "feature-rx-notify", 0)) {
1128 /* - Reduce drain timeout to poll more frequently for
1130 * - Disable Rx stall detection.
1132 be->vif->drain_timeout = msecs_to_jiffies(30);
1133 be->vif->stall_timeout = 0;
1136 vif->can_sg = !!xenbus_read_unsigned(dev->otherend, "feature-sg", 0);
1140 if (xenbus_read_unsigned(dev->otherend, "feature-gso-tcpv4", 0))
1141 vif->gso_mask |= GSO_BIT(TCPV4);
1143 if (xenbus_read_unsigned(dev->otherend, "feature-gso-tcpv6", 0))
1144 vif->gso_mask |= GSO_BIT(TCPV6);
1146 vif->ip_csum = !xenbus_read_unsigned(dev->otherend,
1147 "feature-no-csum-offload", 0);
1149 vif->ipv6_csum = !!xenbus_read_unsigned(dev->otherend,
1150 "feature-ipv6-csum-offload", 0);
1155 static const struct xenbus_device_id netback_ids[] = {
1160 static struct xenbus_driver netback_driver = {
1162 .probe = netback_probe,
1163 .remove = netback_remove,
1164 .uevent = netback_uevent,
1165 .otherend_changed = frontend_changed,
1168 int xenvif_xenbus_init(void)
1170 return xenbus_register_backend(&netback_driver);
1173 void xenvif_xenbus_fini(void)
1175 return xenbus_unregister_driver(&netback_driver);