xref: /openbmc/linux/drivers/net/xen-netback/xenbus.c (revision 3557b3fd)
1 /*
2  * Xenbus code for netif backend
3  *
4  * Copyright (C) 2005 Rusty Russell <rusty@rustcorp.com.au>
5  * Copyright (C) 2005 XenSource Ltd
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 #include "common.h"
22 #include <linux/vmalloc.h>
23 #include <linux/rtnetlink.h>
24 
25 static int connect_data_rings(struct backend_info *be,
26 			      struct xenvif_queue *queue);
27 static void connect(struct backend_info *be);
28 static int read_xenbus_vif_flags(struct backend_info *be);
29 static int backend_create_xenvif(struct backend_info *be);
30 static void unregister_hotplug_status_watch(struct backend_info *be);
31 static void xen_unregister_watchers(struct xenvif *vif);
32 static void set_backend_state(struct backend_info *be,
33 			      enum xenbus_state state);
34 
35 #ifdef CONFIG_DEBUG_FS
36 struct dentry *xen_netback_dbg_root = NULL;
37 
38 static int xenvif_read_io_ring(struct seq_file *m, void *v)
39 {
40 	struct xenvif_queue *queue = m->private;
41 	struct xen_netif_tx_back_ring *tx_ring = &queue->tx;
42 	struct xen_netif_rx_back_ring *rx_ring = &queue->rx;
43 	struct netdev_queue *dev_queue;
44 
45 	if (tx_ring->sring) {
46 		struct xen_netif_tx_sring *sring = tx_ring->sring;
47 
48 		seq_printf(m, "Queue %d\nTX: nr_ents %u\n", queue->id,
49 			   tx_ring->nr_ents);
50 		seq_printf(m, "req prod %u (%d) cons %u (%d) event %u (%d)\n",
51 			   sring->req_prod,
52 			   sring->req_prod - sring->rsp_prod,
53 			   tx_ring->req_cons,
54 			   tx_ring->req_cons - sring->rsp_prod,
55 			   sring->req_event,
56 			   sring->req_event - sring->rsp_prod);
57 		seq_printf(m, "rsp prod %u (base) pvt %u (%d) event %u (%d)\n",
58 			   sring->rsp_prod,
59 			   tx_ring->rsp_prod_pvt,
60 			   tx_ring->rsp_prod_pvt - sring->rsp_prod,
61 			   sring->rsp_event,
62 			   sring->rsp_event - sring->rsp_prod);
63 		seq_printf(m, "pending prod %u pending cons %u nr_pending_reqs %u\n",
64 			   queue->pending_prod,
65 			   queue->pending_cons,
66 			   nr_pending_reqs(queue));
67 		seq_printf(m, "dealloc prod %u dealloc cons %u dealloc_queue %u\n\n",
68 			   queue->dealloc_prod,
69 			   queue->dealloc_cons,
70 			   queue->dealloc_prod - queue->dealloc_cons);
71 	}
72 
73 	if (rx_ring->sring) {
74 		struct xen_netif_rx_sring *sring = rx_ring->sring;
75 
76 		seq_printf(m, "RX: nr_ents %u\n", rx_ring->nr_ents);
77 		seq_printf(m, "req prod %u (%d) cons %u (%d) event %u (%d)\n",
78 			   sring->req_prod,
79 			   sring->req_prod - sring->rsp_prod,
80 			   rx_ring->req_cons,
81 			   rx_ring->req_cons - sring->rsp_prod,
82 			   sring->req_event,
83 			   sring->req_event - sring->rsp_prod);
84 		seq_printf(m, "rsp prod %u (base) pvt %u (%d) event %u (%d)\n\n",
85 			   sring->rsp_prod,
86 			   rx_ring->rsp_prod_pvt,
87 			   rx_ring->rsp_prod_pvt - sring->rsp_prod,
88 			   sring->rsp_event,
89 			   sring->rsp_event - sring->rsp_prod);
90 	}
91 
92 	seq_printf(m, "NAPI state: %lx NAPI weight: %d TX queue len %u\n"
93 		   "Credit timer_pending: %d, credit: %lu, usec: %lu\n"
94 		   "remaining: %lu, expires: %lu, now: %lu\n",
95 		   queue->napi.state, queue->napi.weight,
96 		   skb_queue_len(&queue->tx_queue),
97 		   timer_pending(&queue->credit_timeout),
98 		   queue->credit_bytes,
99 		   queue->credit_usec,
100 		   queue->remaining_credit,
101 		   queue->credit_timeout.expires,
102 		   jiffies);
103 
104 	dev_queue = netdev_get_tx_queue(queue->vif->dev, queue->id);
105 
106 	seq_printf(m, "\nRx internal queue: len %u max %u pkts %u %s\n",
107 		   queue->rx_queue_len, queue->rx_queue_max,
108 		   skb_queue_len(&queue->rx_queue),
109 		   netif_tx_queue_stopped(dev_queue) ? "stopped" : "running");
110 
111 	return 0;
112 }
113 
114 #define XENVIF_KICK_STR "kick"
115 #define BUFFER_SIZE     32
116 
117 static ssize_t
118 xenvif_write_io_ring(struct file *filp, const char __user *buf, size_t count,
119 		     loff_t *ppos)
120 {
121 	struct xenvif_queue *queue =
122 		((struct seq_file *)filp->private_data)->private;
123 	int len;
124 	char write[BUFFER_SIZE];
125 
126 	/* don't allow partial writes and check the length */
127 	if (*ppos != 0)
128 		return 0;
129 	if (count >= sizeof(write))
130 		return -ENOSPC;
131 
132 	len = simple_write_to_buffer(write,
133 				     sizeof(write) - 1,
134 				     ppos,
135 				     buf,
136 				     count);
137 	if (len < 0)
138 		return len;
139 
140 	write[len] = '\0';
141 
142 	if (!strncmp(write, XENVIF_KICK_STR, sizeof(XENVIF_KICK_STR) - 1))
143 		xenvif_interrupt(0, (void *)queue);
144 	else {
145 		pr_warn("Unknown command to io_ring_q%d. Available: kick\n",
146 			queue->id);
147 		count = -EINVAL;
148 	}
149 	return count;
150 }
151 
152 static int xenvif_io_ring_open(struct inode *inode, struct file *filp)
153 {
154 	int ret;
155 	void *queue = NULL;
156 
157 	if (inode->i_private)
158 		queue = inode->i_private;
159 	ret = single_open(filp, xenvif_read_io_ring, queue);
160 	filp->f_mode |= FMODE_PWRITE;
161 	return ret;
162 }
163 
164 static const struct file_operations xenvif_dbg_io_ring_ops_fops = {
165 	.owner = THIS_MODULE,
166 	.open = xenvif_io_ring_open,
167 	.read = seq_read,
168 	.llseek = seq_lseek,
169 	.release = single_release,
170 	.write = xenvif_write_io_ring,
171 };
172 
173 static int xenvif_ctrl_show(struct seq_file *m, void *v)
174 {
175 	struct xenvif *vif = m->private;
176 
177 	xenvif_dump_hash_info(vif, m);
178 
179 	return 0;
180 }
181 DEFINE_SHOW_ATTRIBUTE(xenvif_ctrl);
182 
183 static void xenvif_debugfs_addif(struct xenvif *vif)
184 {
185 	struct dentry *pfile;
186 	int i;
187 
188 	if (IS_ERR_OR_NULL(xen_netback_dbg_root))
189 		return;
190 
191 	vif->xenvif_dbg_root = debugfs_create_dir(vif->dev->name,
192 						  xen_netback_dbg_root);
193 	if (!IS_ERR_OR_NULL(vif->xenvif_dbg_root)) {
194 		for (i = 0; i < vif->num_queues; ++i) {
195 			char filename[sizeof("io_ring_q") + 4];
196 
197 			snprintf(filename, sizeof(filename), "io_ring_q%d", i);
198 			pfile = debugfs_create_file(filename,
199 						    0600,
200 						    vif->xenvif_dbg_root,
201 						    &vif->queues[i],
202 						    &xenvif_dbg_io_ring_ops_fops);
203 			if (IS_ERR_OR_NULL(pfile))
204 				pr_warn("Creation of io_ring file returned %ld!\n",
205 					PTR_ERR(pfile));
206 		}
207 
208 		if (vif->ctrl_irq) {
209 			pfile = debugfs_create_file("ctrl",
210 						    0400,
211 						    vif->xenvif_dbg_root,
212 						    vif,
213 						    &xenvif_ctrl_fops);
214 			if (IS_ERR_OR_NULL(pfile))
215 				pr_warn("Creation of ctrl file returned %ld!\n",
216 					PTR_ERR(pfile));
217 		}
218 	} else
219 		netdev_warn(vif->dev,
220 			    "Creation of vif debugfs dir returned %ld!\n",
221 			    PTR_ERR(vif->xenvif_dbg_root));
222 }
223 
224 static void xenvif_debugfs_delif(struct xenvif *vif)
225 {
226 	if (IS_ERR_OR_NULL(xen_netback_dbg_root))
227 		return;
228 
229 	debugfs_remove_recursive(vif->xenvif_dbg_root);
230 	vif->xenvif_dbg_root = NULL;
231 }
232 #endif /* CONFIG_DEBUG_FS */
233 
234 static int netback_remove(struct xenbus_device *dev)
235 {
236 	struct backend_info *be = dev_get_drvdata(&dev->dev);
237 
238 	set_backend_state(be, XenbusStateClosed);
239 
240 	unregister_hotplug_status_watch(be);
241 	if (be->vif) {
242 		kobject_uevent(&dev->dev.kobj, KOBJ_OFFLINE);
243 		xen_unregister_watchers(be->vif);
244 		xenbus_rm(XBT_NIL, dev->nodename, "hotplug-status");
245 		xenvif_free(be->vif);
246 		be->vif = NULL;
247 	}
248 	kfree(be->hotplug_script);
249 	kfree(be);
250 	dev_set_drvdata(&dev->dev, NULL);
251 	return 0;
252 }
253 
254 
255 /**
256  * Entry point to this code when a new device is created.  Allocate the basic
257  * structures and switch to InitWait.
258  */
259 static int netback_probe(struct xenbus_device *dev,
260 			 const struct xenbus_device_id *id)
261 {
262 	const char *message;
263 	struct xenbus_transaction xbt;
264 	int err;
265 	int sg;
266 	const char *script;
267 	struct backend_info *be = kzalloc(sizeof(struct backend_info),
268 					  GFP_KERNEL);
269 	if (!be) {
270 		xenbus_dev_fatal(dev, -ENOMEM,
271 				 "allocating backend structure");
272 		return -ENOMEM;
273 	}
274 
275 	be->dev = dev;
276 	dev_set_drvdata(&dev->dev, be);
277 
278 	be->state = XenbusStateInitialising;
279 	err = xenbus_switch_state(dev, XenbusStateInitialising);
280 	if (err)
281 		goto fail;
282 
283 	sg = 1;
284 
285 	do {
286 		err = xenbus_transaction_start(&xbt);
287 		if (err) {
288 			xenbus_dev_fatal(dev, err, "starting transaction");
289 			goto fail;
290 		}
291 
292 		err = xenbus_printf(xbt, dev->nodename, "feature-sg", "%d", sg);
293 		if (err) {
294 			message = "writing feature-sg";
295 			goto abort_transaction;
296 		}
297 
298 		err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv4",
299 				    "%d", sg);
300 		if (err) {
301 			message = "writing feature-gso-tcpv4";
302 			goto abort_transaction;
303 		}
304 
305 		err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv6",
306 				    "%d", sg);
307 		if (err) {
308 			message = "writing feature-gso-tcpv6";
309 			goto abort_transaction;
310 		}
311 
312 		/* We support partial checksum setup for IPv6 packets */
313 		err = xenbus_printf(xbt, dev->nodename,
314 				    "feature-ipv6-csum-offload",
315 				    "%d", 1);
316 		if (err) {
317 			message = "writing feature-ipv6-csum-offload";
318 			goto abort_transaction;
319 		}
320 
321 		/* We support rx-copy path. */
322 		err = xenbus_printf(xbt, dev->nodename,
323 				    "feature-rx-copy", "%d", 1);
324 		if (err) {
325 			message = "writing feature-rx-copy";
326 			goto abort_transaction;
327 		}
328 
329 		/*
330 		 * We don't support rx-flip path (except old guests who don't
331 		 * grok this feature flag).
332 		 */
333 		err = xenbus_printf(xbt, dev->nodename,
334 				    "feature-rx-flip", "%d", 0);
335 		if (err) {
336 			message = "writing feature-rx-flip";
337 			goto abort_transaction;
338 		}
339 
340 		/* We support dynamic multicast-control. */
341 		err = xenbus_printf(xbt, dev->nodename,
342 				    "feature-multicast-control", "%d", 1);
343 		if (err) {
344 			message = "writing feature-multicast-control";
345 			goto abort_transaction;
346 		}
347 
348 		err = xenbus_printf(xbt, dev->nodename,
349 				    "feature-dynamic-multicast-control",
350 				    "%d", 1);
351 		if (err) {
352 			message = "writing feature-dynamic-multicast-control";
353 			goto abort_transaction;
354 		}
355 
356 		err = xenbus_transaction_end(xbt, 0);
357 	} while (err == -EAGAIN);
358 
359 	if (err) {
360 		xenbus_dev_fatal(dev, err, "completing transaction");
361 		goto fail;
362 	}
363 
364 	/*
365 	 * Split event channels support, this is optional so it is not
366 	 * put inside the above loop.
367 	 */
368 	err = xenbus_printf(XBT_NIL, dev->nodename,
369 			    "feature-split-event-channels",
370 			    "%u", separate_tx_rx_irq);
371 	if (err)
372 		pr_debug("Error writing feature-split-event-channels\n");
373 
374 	/* Multi-queue support: This is an optional feature. */
375 	err = xenbus_printf(XBT_NIL, dev->nodename,
376 			    "multi-queue-max-queues", "%u", xenvif_max_queues);
377 	if (err)
378 		pr_debug("Error writing multi-queue-max-queues\n");
379 
380 	err = xenbus_printf(XBT_NIL, dev->nodename,
381 			    "feature-ctrl-ring",
382 			    "%u", true);
383 	if (err)
384 		pr_debug("Error writing feature-ctrl-ring\n");
385 
386 	script = xenbus_read(XBT_NIL, dev->nodename, "script", NULL);
387 	if (IS_ERR(script)) {
388 		err = PTR_ERR(script);
389 		xenbus_dev_fatal(dev, err, "reading script");
390 		goto fail;
391 	}
392 
393 	be->hotplug_script = script;
394 
395 
396 	/* This kicks hotplug scripts, so do it immediately. */
397 	err = backend_create_xenvif(be);
398 	if (err)
399 		goto fail;
400 
401 	return 0;
402 
403 abort_transaction:
404 	xenbus_transaction_end(xbt, 1);
405 	xenbus_dev_fatal(dev, err, "%s", message);
406 fail:
407 	pr_debug("failed\n");
408 	netback_remove(dev);
409 	return err;
410 }
411 
412 
413 /*
414  * Handle the creation of the hotplug script environment.  We add the script
415  * and vif variables to the environment, for the benefit of the vif-* hotplug
416  * scripts.
417  */
418 static int netback_uevent(struct xenbus_device *xdev,
419 			  struct kobj_uevent_env *env)
420 {
421 	struct backend_info *be = dev_get_drvdata(&xdev->dev);
422 
423 	if (!be)
424 		return 0;
425 
426 	if (add_uevent_var(env, "script=%s", be->hotplug_script))
427 		return -ENOMEM;
428 
429 	if (!be->vif)
430 		return 0;
431 
432 	return add_uevent_var(env, "vif=%s", be->vif->dev->name);
433 }
434 
435 
436 static int backend_create_xenvif(struct backend_info *be)
437 {
438 	int err;
439 	long handle;
440 	struct xenbus_device *dev = be->dev;
441 	struct xenvif *vif;
442 
443 	if (be->vif != NULL)
444 		return 0;
445 
446 	err = xenbus_scanf(XBT_NIL, dev->nodename, "handle", "%li", &handle);
447 	if (err != 1) {
448 		xenbus_dev_fatal(dev, err, "reading handle");
449 		return (err < 0) ? err : -EINVAL;
450 	}
451 
452 	vif = xenvif_alloc(&dev->dev, dev->otherend_id, handle);
453 	if (IS_ERR(vif)) {
454 		err = PTR_ERR(vif);
455 		xenbus_dev_fatal(dev, err, "creating interface");
456 		return err;
457 	}
458 	be->vif = vif;
459 	vif->be = be;
460 
461 	kobject_uevent(&dev->dev.kobj, KOBJ_ONLINE);
462 	return 0;
463 }
464 
465 static void backend_disconnect(struct backend_info *be)
466 {
467 	struct xenvif *vif = be->vif;
468 
469 	if (vif) {
470 		unsigned int num_queues = vif->num_queues;
471 		unsigned int queue_index;
472 
473 		xen_unregister_watchers(vif);
474 #ifdef CONFIG_DEBUG_FS
475 		xenvif_debugfs_delif(vif);
476 #endif /* CONFIG_DEBUG_FS */
477 		xenvif_disconnect_data(vif);
478 
479 		/* At this point some of the handlers may still be active
480 		 * so we need to have additional synchronization here.
481 		 */
482 		vif->num_queues = 0;
483 		synchronize_net();
484 
485 		for (queue_index = 0; queue_index < num_queues; ++queue_index)
486 			xenvif_deinit_queue(&vif->queues[queue_index]);
487 
488 		vfree(vif->queues);
489 		vif->queues = NULL;
490 
491 		xenvif_disconnect_ctrl(vif);
492 	}
493 }
494 
495 static void backend_connect(struct backend_info *be)
496 {
497 	if (be->vif)
498 		connect(be);
499 }
500 
501 static inline void backend_switch_state(struct backend_info *be,
502 					enum xenbus_state state)
503 {
504 	struct xenbus_device *dev = be->dev;
505 
506 	pr_debug("%s -> %s\n", dev->nodename, xenbus_strstate(state));
507 	be->state = state;
508 
509 	/* If we are waiting for a hotplug script then defer the
510 	 * actual xenbus state change.
511 	 */
512 	if (!be->have_hotplug_status_watch)
513 		xenbus_switch_state(dev, state);
514 }
515 
516 /* Handle backend state transitions:
517  *
518  * The backend state starts in Initialising and the following transitions are
519  * allowed.
520  *
521  * Initialising -> InitWait -> Connected
522  *          \
523  *           \        ^    \         |
524  *            \       |     \        |
525  *             \      |      \       |
526  *              \     |       \      |
527  *               \    |        \     |
528  *                \   |         \    |
529  *                 V  |          V   V
530  *
531  *                  Closed  <-> Closing
532  *
533  * The state argument specifies the eventual state of the backend and the
534  * function transitions to that state via the shortest path.
535  */
536 static void set_backend_state(struct backend_info *be,
537 			      enum xenbus_state state)
538 {
539 	while (be->state != state) {
540 		switch (be->state) {
541 		case XenbusStateInitialising:
542 			switch (state) {
543 			case XenbusStateInitWait:
544 			case XenbusStateConnected:
545 			case XenbusStateClosing:
546 				backend_switch_state(be, XenbusStateInitWait);
547 				break;
548 			case XenbusStateClosed:
549 				backend_switch_state(be, XenbusStateClosed);
550 				break;
551 			default:
552 				BUG();
553 			}
554 			break;
555 		case XenbusStateClosed:
556 			switch (state) {
557 			case XenbusStateInitWait:
558 			case XenbusStateConnected:
559 				backend_switch_state(be, XenbusStateInitWait);
560 				break;
561 			case XenbusStateClosing:
562 				backend_switch_state(be, XenbusStateClosing);
563 				break;
564 			default:
565 				BUG();
566 			}
567 			break;
568 		case XenbusStateInitWait:
569 			switch (state) {
570 			case XenbusStateConnected:
571 				backend_connect(be);
572 				backend_switch_state(be, XenbusStateConnected);
573 				break;
574 			case XenbusStateClosing:
575 			case XenbusStateClosed:
576 				backend_switch_state(be, XenbusStateClosing);
577 				break;
578 			default:
579 				BUG();
580 			}
581 			break;
582 		case XenbusStateConnected:
583 			switch (state) {
584 			case XenbusStateInitWait:
585 			case XenbusStateClosing:
586 			case XenbusStateClosed:
587 				backend_disconnect(be);
588 				backend_switch_state(be, XenbusStateClosing);
589 				break;
590 			default:
591 				BUG();
592 			}
593 			break;
594 		case XenbusStateClosing:
595 			switch (state) {
596 			case XenbusStateInitWait:
597 			case XenbusStateConnected:
598 			case XenbusStateClosed:
599 				backend_switch_state(be, XenbusStateClosed);
600 				break;
601 			default:
602 				BUG();
603 			}
604 			break;
605 		default:
606 			BUG();
607 		}
608 	}
609 }
610 
611 /**
612  * Callback received when the frontend's state changes.
613  */
614 static void frontend_changed(struct xenbus_device *dev,
615 			     enum xenbus_state frontend_state)
616 {
617 	struct backend_info *be = dev_get_drvdata(&dev->dev);
618 
619 	pr_debug("%s -> %s\n", dev->otherend, xenbus_strstate(frontend_state));
620 
621 	be->frontend_state = frontend_state;
622 
623 	switch (frontend_state) {
624 	case XenbusStateInitialising:
625 		set_backend_state(be, XenbusStateInitWait);
626 		break;
627 
628 	case XenbusStateInitialised:
629 		break;
630 
631 	case XenbusStateConnected:
632 		set_backend_state(be, XenbusStateConnected);
633 		break;
634 
635 	case XenbusStateClosing:
636 		set_backend_state(be, XenbusStateClosing);
637 		break;
638 
639 	case XenbusStateClosed:
640 		set_backend_state(be, XenbusStateClosed);
641 		if (xenbus_dev_is_online(dev))
642 			break;
643 		/* fall through - if not online */
644 	case XenbusStateUnknown:
645 		set_backend_state(be, XenbusStateClosed);
646 		device_unregister(&dev->dev);
647 		break;
648 
649 	default:
650 		xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
651 				 frontend_state);
652 		break;
653 	}
654 }
655 
656 
657 static void xen_net_read_rate(struct xenbus_device *dev,
658 			      unsigned long *bytes, unsigned long *usec)
659 {
660 	char *s, *e;
661 	unsigned long b, u;
662 	char *ratestr;
663 
664 	/* Default to unlimited bandwidth. */
665 	*bytes = ~0UL;
666 	*usec = 0;
667 
668 	ratestr = xenbus_read(XBT_NIL, dev->nodename, "rate", NULL);
669 	if (IS_ERR(ratestr))
670 		return;
671 
672 	s = ratestr;
673 	b = simple_strtoul(s, &e, 10);
674 	if ((s == e) || (*e != ','))
675 		goto fail;
676 
677 	s = e + 1;
678 	u = simple_strtoul(s, &e, 10);
679 	if ((s == e) || (*e != '\0'))
680 		goto fail;
681 
682 	*bytes = b;
683 	*usec = u;
684 
685 	kfree(ratestr);
686 	return;
687 
688  fail:
689 	pr_warn("Failed to parse network rate limit. Traffic unlimited.\n");
690 	kfree(ratestr);
691 }
692 
693 static int xen_net_read_mac(struct xenbus_device *dev, u8 mac[])
694 {
695 	char *s, *e, *macstr;
696 	int i;
697 
698 	macstr = s = xenbus_read(XBT_NIL, dev->nodename, "mac", NULL);
699 	if (IS_ERR(macstr))
700 		return PTR_ERR(macstr);
701 
702 	for (i = 0; i < ETH_ALEN; i++) {
703 		mac[i] = simple_strtoul(s, &e, 16);
704 		if ((s == e) || (*e != ((i == ETH_ALEN-1) ? '\0' : ':'))) {
705 			kfree(macstr);
706 			return -ENOENT;
707 		}
708 		s = e+1;
709 	}
710 
711 	kfree(macstr);
712 	return 0;
713 }
714 
715 static void xen_net_rate_changed(struct xenbus_watch *watch,
716 				 const char *path, const char *token)
717 {
718 	struct xenvif *vif = container_of(watch, struct xenvif, credit_watch);
719 	struct xenbus_device *dev = xenvif_to_xenbus_device(vif);
720 	unsigned long   credit_bytes;
721 	unsigned long   credit_usec;
722 	unsigned int queue_index;
723 
724 	xen_net_read_rate(dev, &credit_bytes, &credit_usec);
725 	for (queue_index = 0; queue_index < vif->num_queues; queue_index++) {
726 		struct xenvif_queue *queue = &vif->queues[queue_index];
727 
728 		queue->credit_bytes = credit_bytes;
729 		queue->credit_usec = credit_usec;
730 		if (!mod_timer_pending(&queue->credit_timeout, jiffies) &&
731 			queue->remaining_credit > queue->credit_bytes) {
732 			queue->remaining_credit = queue->credit_bytes;
733 		}
734 	}
735 }
736 
737 static int xen_register_credit_watch(struct xenbus_device *dev,
738 				     struct xenvif *vif)
739 {
740 	int err = 0;
741 	char *node;
742 	unsigned maxlen = strlen(dev->nodename) + sizeof("/rate");
743 
744 	if (vif->credit_watch.node)
745 		return -EADDRINUSE;
746 
747 	node = kmalloc(maxlen, GFP_KERNEL);
748 	if (!node)
749 		return -ENOMEM;
750 	snprintf(node, maxlen, "%s/rate", dev->nodename);
751 	vif->credit_watch.node = node;
752 	vif->credit_watch.callback = xen_net_rate_changed;
753 	err = register_xenbus_watch(&vif->credit_watch);
754 	if (err) {
755 		pr_err("Failed to set watcher %s\n", vif->credit_watch.node);
756 		kfree(node);
757 		vif->credit_watch.node = NULL;
758 		vif->credit_watch.callback = NULL;
759 	}
760 	return err;
761 }
762 
763 static void xen_unregister_credit_watch(struct xenvif *vif)
764 {
765 	if (vif->credit_watch.node) {
766 		unregister_xenbus_watch(&vif->credit_watch);
767 		kfree(vif->credit_watch.node);
768 		vif->credit_watch.node = NULL;
769 	}
770 }
771 
772 static void xen_mcast_ctrl_changed(struct xenbus_watch *watch,
773 				   const char *path, const char *token)
774 {
775 	struct xenvif *vif = container_of(watch, struct xenvif,
776 					  mcast_ctrl_watch);
777 	struct xenbus_device *dev = xenvif_to_xenbus_device(vif);
778 
779 	vif->multicast_control = !!xenbus_read_unsigned(dev->otherend,
780 					"request-multicast-control", 0);
781 }
782 
783 static int xen_register_mcast_ctrl_watch(struct xenbus_device *dev,
784 					 struct xenvif *vif)
785 {
786 	int err = 0;
787 	char *node;
788 	unsigned maxlen = strlen(dev->otherend) +
789 		sizeof("/request-multicast-control");
790 
791 	if (vif->mcast_ctrl_watch.node) {
792 		pr_err_ratelimited("Watch is already registered\n");
793 		return -EADDRINUSE;
794 	}
795 
796 	node = kmalloc(maxlen, GFP_KERNEL);
797 	if (!node) {
798 		pr_err("Failed to allocate memory for watch\n");
799 		return -ENOMEM;
800 	}
801 	snprintf(node, maxlen, "%s/request-multicast-control",
802 		 dev->otherend);
803 	vif->mcast_ctrl_watch.node = node;
804 	vif->mcast_ctrl_watch.callback = xen_mcast_ctrl_changed;
805 	err = register_xenbus_watch(&vif->mcast_ctrl_watch);
806 	if (err) {
807 		pr_err("Failed to set watcher %s\n",
808 		       vif->mcast_ctrl_watch.node);
809 		kfree(node);
810 		vif->mcast_ctrl_watch.node = NULL;
811 		vif->mcast_ctrl_watch.callback = NULL;
812 	}
813 	return err;
814 }
815 
816 static void xen_unregister_mcast_ctrl_watch(struct xenvif *vif)
817 {
818 	if (vif->mcast_ctrl_watch.node) {
819 		unregister_xenbus_watch(&vif->mcast_ctrl_watch);
820 		kfree(vif->mcast_ctrl_watch.node);
821 		vif->mcast_ctrl_watch.node = NULL;
822 	}
823 }
824 
825 static void xen_register_watchers(struct xenbus_device *dev,
826 				  struct xenvif *vif)
827 {
828 	xen_register_credit_watch(dev, vif);
829 	xen_register_mcast_ctrl_watch(dev, vif);
830 }
831 
832 static void xen_unregister_watchers(struct xenvif *vif)
833 {
834 	xen_unregister_mcast_ctrl_watch(vif);
835 	xen_unregister_credit_watch(vif);
836 }
837 
838 static void unregister_hotplug_status_watch(struct backend_info *be)
839 {
840 	if (be->have_hotplug_status_watch) {
841 		unregister_xenbus_watch(&be->hotplug_status_watch);
842 		kfree(be->hotplug_status_watch.node);
843 	}
844 	be->have_hotplug_status_watch = 0;
845 }
846 
847 static void hotplug_status_changed(struct xenbus_watch *watch,
848 				   const char *path,
849 				   const char *token)
850 {
851 	struct backend_info *be = container_of(watch,
852 					       struct backend_info,
853 					       hotplug_status_watch);
854 	char *str;
855 	unsigned int len;
856 
857 	str = xenbus_read(XBT_NIL, be->dev->nodename, "hotplug-status", &len);
858 	if (IS_ERR(str))
859 		return;
860 	if (len == sizeof("connected")-1 && !memcmp(str, "connected", len)) {
861 		/* Complete any pending state change */
862 		xenbus_switch_state(be->dev, be->state);
863 
864 		/* Not interested in this watch anymore. */
865 		unregister_hotplug_status_watch(be);
866 	}
867 	kfree(str);
868 }
869 
870 static int connect_ctrl_ring(struct backend_info *be)
871 {
872 	struct xenbus_device *dev = be->dev;
873 	struct xenvif *vif = be->vif;
874 	unsigned int val;
875 	grant_ref_t ring_ref;
876 	unsigned int evtchn;
877 	int err;
878 
879 	err = xenbus_scanf(XBT_NIL, dev->otherend,
880 			   "ctrl-ring-ref", "%u", &val);
881 	if (err < 0)
882 		goto done; /* The frontend does not have a control ring */
883 
884 	ring_ref = val;
885 
886 	err = xenbus_scanf(XBT_NIL, dev->otherend,
887 			   "event-channel-ctrl", "%u", &val);
888 	if (err < 0) {
889 		xenbus_dev_fatal(dev, err,
890 				 "reading %s/event-channel-ctrl",
891 				 dev->otherend);
892 		goto fail;
893 	}
894 
895 	evtchn = val;
896 
897 	err = xenvif_connect_ctrl(vif, ring_ref, evtchn);
898 	if (err) {
899 		xenbus_dev_fatal(dev, err,
900 				 "mapping shared-frame %u port %u",
901 				 ring_ref, evtchn);
902 		goto fail;
903 	}
904 
905 done:
906 	return 0;
907 
908 fail:
909 	return err;
910 }
911 
912 static void connect(struct backend_info *be)
913 {
914 	int err;
915 	struct xenbus_device *dev = be->dev;
916 	unsigned long credit_bytes, credit_usec;
917 	unsigned int queue_index;
918 	unsigned int requested_num_queues;
919 	struct xenvif_queue *queue;
920 
921 	/* Check whether the frontend requested multiple queues
922 	 * and read the number requested.
923 	 */
924 	requested_num_queues = xenbus_read_unsigned(dev->otherend,
925 					"multi-queue-num-queues", 1);
926 	if (requested_num_queues > xenvif_max_queues) {
927 		/* buggy or malicious guest */
928 		xenbus_dev_fatal(dev, -EINVAL,
929 				 "guest requested %u queues, exceeding the maximum of %u.",
930 				 requested_num_queues, xenvif_max_queues);
931 		return;
932 	}
933 
934 	err = xen_net_read_mac(dev, be->vif->fe_dev_addr);
935 	if (err) {
936 		xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename);
937 		return;
938 	}
939 
940 	xen_net_read_rate(dev, &credit_bytes, &credit_usec);
941 	xen_unregister_watchers(be->vif);
942 	xen_register_watchers(dev, be->vif);
943 	read_xenbus_vif_flags(be);
944 
945 	err = connect_ctrl_ring(be);
946 	if (err) {
947 		xenbus_dev_fatal(dev, err, "connecting control ring");
948 		return;
949 	}
950 
951 	/* Use the number of queues requested by the frontend */
952 	be->vif->queues = vzalloc(array_size(requested_num_queues,
953 					     sizeof(struct xenvif_queue)));
954 	if (!be->vif->queues) {
955 		xenbus_dev_fatal(dev, -ENOMEM,
956 				 "allocating queues");
957 		return;
958 	}
959 
960 	be->vif->num_queues = requested_num_queues;
961 	be->vif->stalled_queues = requested_num_queues;
962 
963 	for (queue_index = 0; queue_index < requested_num_queues; ++queue_index) {
964 		queue = &be->vif->queues[queue_index];
965 		queue->vif = be->vif;
966 		queue->id = queue_index;
967 		snprintf(queue->name, sizeof(queue->name), "%s-q%u",
968 				be->vif->dev->name, queue->id);
969 
970 		err = xenvif_init_queue(queue);
971 		if (err) {
972 			/* xenvif_init_queue() cleans up after itself on
973 			 * failure, but we need to clean up any previously
974 			 * initialised queues. Set num_queues to i so that
975 			 * earlier queues can be destroyed using the regular
976 			 * disconnect logic.
977 			 */
978 			be->vif->num_queues = queue_index;
979 			goto err;
980 		}
981 
982 		queue->credit_bytes = credit_bytes;
983 		queue->remaining_credit = credit_bytes;
984 		queue->credit_usec = credit_usec;
985 
986 		err = connect_data_rings(be, queue);
987 		if (err) {
988 			/* connect_data_rings() cleans up after itself on
989 			 * failure, but we need to clean up after
990 			 * xenvif_init_queue() here, and also clean up any
991 			 * previously initialised queues.
992 			 */
993 			xenvif_deinit_queue(queue);
994 			be->vif->num_queues = queue_index;
995 			goto err;
996 		}
997 	}
998 
999 #ifdef CONFIG_DEBUG_FS
1000 	xenvif_debugfs_addif(be->vif);
1001 #endif /* CONFIG_DEBUG_FS */
1002 
1003 	/* Initialisation completed, tell core driver the number of
1004 	 * active queues.
1005 	 */
1006 	rtnl_lock();
1007 	netif_set_real_num_tx_queues(be->vif->dev, requested_num_queues);
1008 	netif_set_real_num_rx_queues(be->vif->dev, requested_num_queues);
1009 	rtnl_unlock();
1010 
1011 	xenvif_carrier_on(be->vif);
1012 
1013 	unregister_hotplug_status_watch(be);
1014 	err = xenbus_watch_pathfmt(dev, &be->hotplug_status_watch,
1015 				   hotplug_status_changed,
1016 				   "%s/%s", dev->nodename, "hotplug-status");
1017 	if (!err)
1018 		be->have_hotplug_status_watch = 1;
1019 
1020 	netif_tx_wake_all_queues(be->vif->dev);
1021 
1022 	return;
1023 
1024 err:
1025 	if (be->vif->num_queues > 0)
1026 		xenvif_disconnect_data(be->vif); /* Clean up existing queues */
1027 	for (queue_index = 0; queue_index < be->vif->num_queues; ++queue_index)
1028 		xenvif_deinit_queue(&be->vif->queues[queue_index]);
1029 	vfree(be->vif->queues);
1030 	be->vif->queues = NULL;
1031 	be->vif->num_queues = 0;
1032 	xenvif_disconnect_ctrl(be->vif);
1033 	return;
1034 }
1035 
1036 
1037 static int connect_data_rings(struct backend_info *be,
1038 			      struct xenvif_queue *queue)
1039 {
1040 	struct xenbus_device *dev = be->dev;
1041 	unsigned int num_queues = queue->vif->num_queues;
1042 	unsigned long tx_ring_ref, rx_ring_ref;
1043 	unsigned int tx_evtchn, rx_evtchn;
1044 	int err;
1045 	char *xspath;
1046 	size_t xspathsize;
1047 	const size_t xenstore_path_ext_size = 11; /* sufficient for "/queue-NNN" */
1048 
1049 	/* If the frontend requested 1 queue, or we have fallen back
1050 	 * to single queue due to lack of frontend support for multi-
1051 	 * queue, expect the remaining XenStore keys in the toplevel
1052 	 * directory. Otherwise, expect them in a subdirectory called
1053 	 * queue-N.
1054 	 */
1055 	if (num_queues == 1) {
1056 		xspath = kzalloc(strlen(dev->otherend) + 1, GFP_KERNEL);
1057 		if (!xspath) {
1058 			xenbus_dev_fatal(dev, -ENOMEM,
1059 					 "reading ring references");
1060 			return -ENOMEM;
1061 		}
1062 		strcpy(xspath, dev->otherend);
1063 	} else {
1064 		xspathsize = strlen(dev->otherend) + xenstore_path_ext_size;
1065 		xspath = kzalloc(xspathsize, GFP_KERNEL);
1066 		if (!xspath) {
1067 			xenbus_dev_fatal(dev, -ENOMEM,
1068 					 "reading ring references");
1069 			return -ENOMEM;
1070 		}
1071 		snprintf(xspath, xspathsize, "%s/queue-%u", dev->otherend,
1072 			 queue->id);
1073 	}
1074 
1075 	err = xenbus_gather(XBT_NIL, xspath,
1076 			    "tx-ring-ref", "%lu", &tx_ring_ref,
1077 			    "rx-ring-ref", "%lu", &rx_ring_ref, NULL);
1078 	if (err) {
1079 		xenbus_dev_fatal(dev, err,
1080 				 "reading %s/ring-ref",
1081 				 xspath);
1082 		goto err;
1083 	}
1084 
1085 	/* Try split event channels first, then single event channel. */
1086 	err = xenbus_gather(XBT_NIL, xspath,
1087 			    "event-channel-tx", "%u", &tx_evtchn,
1088 			    "event-channel-rx", "%u", &rx_evtchn, NULL);
1089 	if (err < 0) {
1090 		err = xenbus_scanf(XBT_NIL, xspath,
1091 				   "event-channel", "%u", &tx_evtchn);
1092 		if (err < 0) {
1093 			xenbus_dev_fatal(dev, err,
1094 					 "reading %s/event-channel(-tx/rx)",
1095 					 xspath);
1096 			goto err;
1097 		}
1098 		rx_evtchn = tx_evtchn;
1099 	}
1100 
1101 	/* Map the shared frame, irq etc. */
1102 	err = xenvif_connect_data(queue, tx_ring_ref, rx_ring_ref,
1103 				  tx_evtchn, rx_evtchn);
1104 	if (err) {
1105 		xenbus_dev_fatal(dev, err,
1106 				 "mapping shared-frames %lu/%lu port tx %u rx %u",
1107 				 tx_ring_ref, rx_ring_ref,
1108 				 tx_evtchn, rx_evtchn);
1109 		goto err;
1110 	}
1111 
1112 	err = 0;
1113 err: /* Regular return falls through with err == 0 */
1114 	kfree(xspath);
1115 	return err;
1116 }
1117 
1118 static int read_xenbus_vif_flags(struct backend_info *be)
1119 {
1120 	struct xenvif *vif = be->vif;
1121 	struct xenbus_device *dev = be->dev;
1122 	unsigned int rx_copy;
1123 	int err;
1124 
1125 	err = xenbus_scanf(XBT_NIL, dev->otherend, "request-rx-copy", "%u",
1126 			   &rx_copy);
1127 	if (err == -ENOENT) {
1128 		err = 0;
1129 		rx_copy = 0;
1130 	}
1131 	if (err < 0) {
1132 		xenbus_dev_fatal(dev, err, "reading %s/request-rx-copy",
1133 				 dev->otherend);
1134 		return err;
1135 	}
1136 	if (!rx_copy)
1137 		return -EOPNOTSUPP;
1138 
1139 	if (!xenbus_read_unsigned(dev->otherend, "feature-rx-notify", 0)) {
1140 		/* - Reduce drain timeout to poll more frequently for
1141 		 *   Rx requests.
1142 		 * - Disable Rx stall detection.
1143 		 */
1144 		be->vif->drain_timeout = msecs_to_jiffies(30);
1145 		be->vif->stall_timeout = 0;
1146 	}
1147 
1148 	vif->can_sg = !!xenbus_read_unsigned(dev->otherend, "feature-sg", 0);
1149 
1150 	vif->gso_mask = 0;
1151 
1152 	if (xenbus_read_unsigned(dev->otherend, "feature-gso-tcpv4", 0))
1153 		vif->gso_mask |= GSO_BIT(TCPV4);
1154 
1155 	if (xenbus_read_unsigned(dev->otherend, "feature-gso-tcpv6", 0))
1156 		vif->gso_mask |= GSO_BIT(TCPV6);
1157 
1158 	vif->ip_csum = !xenbus_read_unsigned(dev->otherend,
1159 					     "feature-no-csum-offload", 0);
1160 
1161 	vif->ipv6_csum = !!xenbus_read_unsigned(dev->otherend,
1162 						"feature-ipv6-csum-offload", 0);
1163 
1164 	return 0;
1165 }
1166 
1167 static const struct xenbus_device_id netback_ids[] = {
1168 	{ "vif" },
1169 	{ "" }
1170 };
1171 
1172 static struct xenbus_driver netback_driver = {
1173 	.ids = netback_ids,
1174 	.probe = netback_probe,
1175 	.remove = netback_remove,
1176 	.uevent = netback_uevent,
1177 	.otherend_changed = frontend_changed,
1178 };
1179 
1180 int xenvif_xenbus_init(void)
1181 {
1182 	return xenbus_register_backend(&netback_driver);
1183 }
1184 
1185 void xenvif_xenbus_fini(void)
1186 {
1187 	return xenbus_unregister_driver(&netback_driver);
1188 }
1189