xref: /openbmc/linux/drivers/net/xen-netback/xenbus.c (revision 33ac9dba)
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 struct backend_info {
26 	struct xenbus_device *dev;
27 	struct xenvif *vif;
28 
29 	/* This is the state that will be reflected in xenstore when any
30 	 * active hotplug script completes.
31 	 */
32 	enum xenbus_state state;
33 
34 	enum xenbus_state frontend_state;
35 	struct xenbus_watch hotplug_status_watch;
36 	u8 have_hotplug_status_watch:1;
37 };
38 
39 static int connect_rings(struct backend_info *be, struct xenvif_queue *queue);
40 static void connect(struct backend_info *be);
41 static int read_xenbus_vif_flags(struct backend_info *be);
42 static void backend_create_xenvif(struct backend_info *be);
43 static void unregister_hotplug_status_watch(struct backend_info *be);
44 static void set_backend_state(struct backend_info *be,
45 			      enum xenbus_state state);
46 
47 #ifdef CONFIG_DEBUG_FS
48 struct dentry *xen_netback_dbg_root = NULL;
49 
50 static int xenvif_read_io_ring(struct seq_file *m, void *v)
51 {
52 	struct xenvif_queue *queue = m->private;
53 	struct xen_netif_tx_back_ring *tx_ring = &queue->tx;
54 	struct xen_netif_rx_back_ring *rx_ring = &queue->rx;
55 
56 	if (tx_ring->sring) {
57 		struct xen_netif_tx_sring *sring = tx_ring->sring;
58 
59 		seq_printf(m, "Queue %d\nTX: nr_ents %u\n", queue->id,
60 			   tx_ring->nr_ents);
61 		seq_printf(m, "req prod %u (%d) cons %u (%d) event %u (%d)\n",
62 			   sring->req_prod,
63 			   sring->req_prod - sring->rsp_prod,
64 			   tx_ring->req_cons,
65 			   tx_ring->req_cons - sring->rsp_prod,
66 			   sring->req_event,
67 			   sring->req_event - sring->rsp_prod);
68 		seq_printf(m, "rsp prod %u (base) pvt %u (%d) event %u (%d)\n",
69 			   sring->rsp_prod,
70 			   tx_ring->rsp_prod_pvt,
71 			   tx_ring->rsp_prod_pvt - sring->rsp_prod,
72 			   sring->rsp_event,
73 			   sring->rsp_event - sring->rsp_prod);
74 		seq_printf(m, "pending prod %u pending cons %u nr_pending_reqs %u\n",
75 			   queue->pending_prod,
76 			   queue->pending_cons,
77 			   nr_pending_reqs(queue));
78 		seq_printf(m, "dealloc prod %u dealloc cons %u dealloc_queue %u\n\n",
79 			   queue->dealloc_prod,
80 			   queue->dealloc_cons,
81 			   queue->dealloc_prod - queue->dealloc_cons);
82 	}
83 
84 	if (rx_ring->sring) {
85 		struct xen_netif_rx_sring *sring = rx_ring->sring;
86 
87 		seq_printf(m, "RX: nr_ents %u\n", rx_ring->nr_ents);
88 		seq_printf(m, "req prod %u (%d) cons %u (%d) event %u (%d)\n",
89 			   sring->req_prod,
90 			   sring->req_prod - sring->rsp_prod,
91 			   rx_ring->req_cons,
92 			   rx_ring->req_cons - sring->rsp_prod,
93 			   sring->req_event,
94 			   sring->req_event - sring->rsp_prod);
95 		seq_printf(m, "rsp prod %u (base) pvt %u (%d) event %u (%d)\n\n",
96 			   sring->rsp_prod,
97 			   rx_ring->rsp_prod_pvt,
98 			   rx_ring->rsp_prod_pvt - sring->rsp_prod,
99 			   sring->rsp_event,
100 			   sring->rsp_event - sring->rsp_prod);
101 	}
102 
103 	seq_printf(m, "NAPI state: %lx NAPI weight: %d TX queue len %u\n"
104 		   "Credit timer_pending: %d, credit: %lu, usec: %lu\n"
105 		   "remaining: %lu, expires: %lu, now: %lu\n",
106 		   queue->napi.state, queue->napi.weight,
107 		   skb_queue_len(&queue->tx_queue),
108 		   timer_pending(&queue->credit_timeout),
109 		   queue->credit_bytes,
110 		   queue->credit_usec,
111 		   queue->remaining_credit,
112 		   queue->credit_timeout.expires,
113 		   jiffies);
114 
115 	return 0;
116 }
117 
118 #define XENVIF_KICK_STR "kick"
119 #define BUFFER_SIZE     32
120 
121 static ssize_t
122 xenvif_write_io_ring(struct file *filp, const char __user *buf, size_t count,
123 		     loff_t *ppos)
124 {
125 	struct xenvif_queue *queue =
126 		((struct seq_file *)filp->private_data)->private;
127 	int len;
128 	char write[BUFFER_SIZE];
129 
130 	/* don't allow partial writes and check the length */
131 	if (*ppos != 0)
132 		return 0;
133 	if (count >= sizeof(write))
134 		return -ENOSPC;
135 
136 	len = simple_write_to_buffer(write,
137 				     sizeof(write) - 1,
138 				     ppos,
139 				     buf,
140 				     count);
141 	if (len < 0)
142 		return len;
143 
144 	write[len] = '\0';
145 
146 	if (!strncmp(write, XENVIF_KICK_STR, sizeof(XENVIF_KICK_STR) - 1))
147 		xenvif_interrupt(0, (void *)queue);
148 	else {
149 		pr_warn("Unknown command to io_ring_q%d. Available: kick\n",
150 			queue->id);
151 		count = -EINVAL;
152 	}
153 	return count;
154 }
155 
156 static int xenvif_dump_open(struct inode *inode, struct file *filp)
157 {
158 	int ret;
159 	void *queue = NULL;
160 
161 	if (inode->i_private)
162 		queue = inode->i_private;
163 	ret = single_open(filp, xenvif_read_io_ring, queue);
164 	filp->f_mode |= FMODE_PWRITE;
165 	return ret;
166 }
167 
168 static const struct file_operations xenvif_dbg_io_ring_ops_fops = {
169 	.owner = THIS_MODULE,
170 	.open = xenvif_dump_open,
171 	.read = seq_read,
172 	.llseek = seq_lseek,
173 	.release = single_release,
174 	.write = xenvif_write_io_ring,
175 };
176 
177 static void xenvif_debugfs_addif(struct xenvif *vif)
178 {
179 	struct dentry *pfile;
180 	int i;
181 
182 	if (IS_ERR_OR_NULL(xen_netback_dbg_root))
183 		return;
184 
185 	vif->xenvif_dbg_root = debugfs_create_dir(vif->dev->name,
186 						  xen_netback_dbg_root);
187 	if (!IS_ERR_OR_NULL(vif->xenvif_dbg_root)) {
188 		for (i = 0; i < vif->num_queues; ++i) {
189 			char filename[sizeof("io_ring_q") + 4];
190 
191 			snprintf(filename, sizeof(filename), "io_ring_q%d", i);
192 			pfile = debugfs_create_file(filename,
193 						    S_IRUSR | S_IWUSR,
194 						    vif->xenvif_dbg_root,
195 						    &vif->queues[i],
196 						    &xenvif_dbg_io_ring_ops_fops);
197 			if (IS_ERR_OR_NULL(pfile))
198 				pr_warn("Creation of io_ring file returned %ld!\n",
199 					PTR_ERR(pfile));
200 		}
201 	} else
202 		netdev_warn(vif->dev,
203 			    "Creation of vif debugfs dir returned %ld!\n",
204 			    PTR_ERR(vif->xenvif_dbg_root));
205 }
206 
207 static void xenvif_debugfs_delif(struct xenvif *vif)
208 {
209 	if (IS_ERR_OR_NULL(xen_netback_dbg_root))
210 		return;
211 
212 	if (!IS_ERR_OR_NULL(vif->xenvif_dbg_root))
213 		debugfs_remove_recursive(vif->xenvif_dbg_root);
214 	vif->xenvif_dbg_root = NULL;
215 }
216 #endif /* CONFIG_DEBUG_FS */
217 
218 static int netback_remove(struct xenbus_device *dev)
219 {
220 	struct backend_info *be = dev_get_drvdata(&dev->dev);
221 
222 	set_backend_state(be, XenbusStateClosed);
223 
224 	unregister_hotplug_status_watch(be);
225 	if (be->vif) {
226 		kobject_uevent(&dev->dev.kobj, KOBJ_OFFLINE);
227 		xenbus_rm(XBT_NIL, dev->nodename, "hotplug-status");
228 		xenvif_free(be->vif);
229 		be->vif = NULL;
230 	}
231 	kfree(be);
232 	dev_set_drvdata(&dev->dev, NULL);
233 	return 0;
234 }
235 
236 
237 /**
238  * Entry point to this code when a new device is created.  Allocate the basic
239  * structures and switch to InitWait.
240  */
241 static int netback_probe(struct xenbus_device *dev,
242 			 const struct xenbus_device_id *id)
243 {
244 	const char *message;
245 	struct xenbus_transaction xbt;
246 	int err;
247 	int sg;
248 	struct backend_info *be = kzalloc(sizeof(struct backend_info),
249 					  GFP_KERNEL);
250 	if (!be) {
251 		xenbus_dev_fatal(dev, -ENOMEM,
252 				 "allocating backend structure");
253 		return -ENOMEM;
254 	}
255 
256 	be->dev = dev;
257 	dev_set_drvdata(&dev->dev, be);
258 
259 	sg = 1;
260 
261 	do {
262 		err = xenbus_transaction_start(&xbt);
263 		if (err) {
264 			xenbus_dev_fatal(dev, err, "starting transaction");
265 			goto fail;
266 		}
267 
268 		err = xenbus_printf(xbt, dev->nodename, "feature-sg", "%d", sg);
269 		if (err) {
270 			message = "writing feature-sg";
271 			goto abort_transaction;
272 		}
273 
274 		err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv4",
275 				    "%d", sg);
276 		if (err) {
277 			message = "writing feature-gso-tcpv4";
278 			goto abort_transaction;
279 		}
280 
281 		err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv6",
282 				    "%d", sg);
283 		if (err) {
284 			message = "writing feature-gso-tcpv6";
285 			goto abort_transaction;
286 		}
287 
288 		/* We support partial checksum setup for IPv6 packets */
289 		err = xenbus_printf(xbt, dev->nodename,
290 				    "feature-ipv6-csum-offload",
291 				    "%d", 1);
292 		if (err) {
293 			message = "writing feature-ipv6-csum-offload";
294 			goto abort_transaction;
295 		}
296 
297 		/* We support rx-copy path. */
298 		err = xenbus_printf(xbt, dev->nodename,
299 				    "feature-rx-copy", "%d", 1);
300 		if (err) {
301 			message = "writing feature-rx-copy";
302 			goto abort_transaction;
303 		}
304 
305 		/*
306 		 * We don't support rx-flip path (except old guests who don't
307 		 * grok this feature flag).
308 		 */
309 		err = xenbus_printf(xbt, dev->nodename,
310 				    "feature-rx-flip", "%d", 0);
311 		if (err) {
312 			message = "writing feature-rx-flip";
313 			goto abort_transaction;
314 		}
315 
316 		err = xenbus_transaction_end(xbt, 0);
317 	} while (err == -EAGAIN);
318 
319 	if (err) {
320 		xenbus_dev_fatal(dev, err, "completing transaction");
321 		goto fail;
322 	}
323 
324 	/*
325 	 * Split event channels support, this is optional so it is not
326 	 * put inside the above loop.
327 	 */
328 	err = xenbus_printf(XBT_NIL, dev->nodename,
329 			    "feature-split-event-channels",
330 			    "%u", separate_tx_rx_irq);
331 	if (err)
332 		pr_debug("Error writing feature-split-event-channels\n");
333 
334 	/* Multi-queue support: This is an optional feature. */
335 	err = xenbus_printf(XBT_NIL, dev->nodename,
336 			    "multi-queue-max-queues", "%u", xenvif_max_queues);
337 	if (err)
338 		pr_debug("Error writing multi-queue-max-queues\n");
339 
340 	err = xenbus_switch_state(dev, XenbusStateInitWait);
341 	if (err)
342 		goto fail;
343 
344 	be->state = XenbusStateInitWait;
345 
346 	/* This kicks hotplug scripts, so do it immediately. */
347 	backend_create_xenvif(be);
348 
349 	return 0;
350 
351 abort_transaction:
352 	xenbus_transaction_end(xbt, 1);
353 	xenbus_dev_fatal(dev, err, "%s", message);
354 fail:
355 	pr_debug("failed\n");
356 	netback_remove(dev);
357 	return err;
358 }
359 
360 
361 /*
362  * Handle the creation of the hotplug script environment.  We add the script
363  * and vif variables to the environment, for the benefit of the vif-* hotplug
364  * scripts.
365  */
366 static int netback_uevent(struct xenbus_device *xdev,
367 			  struct kobj_uevent_env *env)
368 {
369 	struct backend_info *be = dev_get_drvdata(&xdev->dev);
370 	char *val;
371 
372 	val = xenbus_read(XBT_NIL, xdev->nodename, "script", NULL);
373 	if (IS_ERR(val)) {
374 		int err = PTR_ERR(val);
375 		xenbus_dev_fatal(xdev, err, "reading script");
376 		return err;
377 	} else {
378 		if (add_uevent_var(env, "script=%s", val)) {
379 			kfree(val);
380 			return -ENOMEM;
381 		}
382 		kfree(val);
383 	}
384 
385 	if (!be || !be->vif)
386 		return 0;
387 
388 	return add_uevent_var(env, "vif=%s", be->vif->dev->name);
389 }
390 
391 
392 static void backend_create_xenvif(struct backend_info *be)
393 {
394 	int err;
395 	long handle;
396 	struct xenbus_device *dev = be->dev;
397 
398 	if (be->vif != NULL)
399 		return;
400 
401 	err = xenbus_scanf(XBT_NIL, dev->nodename, "handle", "%li", &handle);
402 	if (err != 1) {
403 		xenbus_dev_fatal(dev, err, "reading handle");
404 		return;
405 	}
406 
407 	be->vif = xenvif_alloc(&dev->dev, dev->otherend_id, handle);
408 	if (IS_ERR(be->vif)) {
409 		err = PTR_ERR(be->vif);
410 		be->vif = NULL;
411 		xenbus_dev_fatal(dev, err, "creating interface");
412 		return;
413 	}
414 
415 	kobject_uevent(&dev->dev.kobj, KOBJ_ONLINE);
416 }
417 
418 static void backend_disconnect(struct backend_info *be)
419 {
420 	if (be->vif) {
421 #ifdef CONFIG_DEBUG_FS
422 		xenvif_debugfs_delif(be->vif);
423 #endif /* CONFIG_DEBUG_FS */
424 		xenvif_disconnect(be->vif);
425 	}
426 }
427 
428 static void backend_connect(struct backend_info *be)
429 {
430 	if (be->vif)
431 		connect(be);
432 }
433 
434 static inline void backend_switch_state(struct backend_info *be,
435 					enum xenbus_state state)
436 {
437 	struct xenbus_device *dev = be->dev;
438 
439 	pr_debug("%s -> %s\n", dev->nodename, xenbus_strstate(state));
440 	be->state = state;
441 
442 	/* If we are waiting for a hotplug script then defer the
443 	 * actual xenbus state change.
444 	 */
445 	if (!be->have_hotplug_status_watch)
446 		xenbus_switch_state(dev, state);
447 }
448 
449 /* Handle backend state transitions:
450  *
451  * The backend state starts in InitWait and the following transitions are
452  * allowed.
453  *
454  * InitWait -> Connected
455  *
456  *    ^    \         |
457  *    |     \        |
458  *    |      \       |
459  *    |       \      |
460  *    |        \     |
461  *    |         \    |
462  *    |          V   V
463  *
464  *  Closed  <-> Closing
465  *
466  * The state argument specifies the eventual state of the backend and the
467  * function transitions to that state via the shortest path.
468  */
469 static void set_backend_state(struct backend_info *be,
470 			      enum xenbus_state state)
471 {
472 	while (be->state != state) {
473 		switch (be->state) {
474 		case XenbusStateClosed:
475 			switch (state) {
476 			case XenbusStateInitWait:
477 			case XenbusStateConnected:
478 				pr_info("%s: prepare for reconnect\n",
479 					be->dev->nodename);
480 				backend_switch_state(be, XenbusStateInitWait);
481 				break;
482 			case XenbusStateClosing:
483 				backend_switch_state(be, XenbusStateClosing);
484 				break;
485 			default:
486 				BUG();
487 			}
488 			break;
489 		case XenbusStateInitWait:
490 			switch (state) {
491 			case XenbusStateConnected:
492 				backend_connect(be);
493 				backend_switch_state(be, XenbusStateConnected);
494 				break;
495 			case XenbusStateClosing:
496 			case XenbusStateClosed:
497 				backend_switch_state(be, XenbusStateClosing);
498 				break;
499 			default:
500 				BUG();
501 			}
502 			break;
503 		case XenbusStateConnected:
504 			switch (state) {
505 			case XenbusStateInitWait:
506 			case XenbusStateClosing:
507 			case XenbusStateClosed:
508 				backend_disconnect(be);
509 				backend_switch_state(be, XenbusStateClosing);
510 				break;
511 			default:
512 				BUG();
513 			}
514 			break;
515 		case XenbusStateClosing:
516 			switch (state) {
517 			case XenbusStateInitWait:
518 			case XenbusStateConnected:
519 			case XenbusStateClosed:
520 				backend_switch_state(be, XenbusStateClosed);
521 				break;
522 			default:
523 				BUG();
524 			}
525 			break;
526 		default:
527 			BUG();
528 		}
529 	}
530 }
531 
532 /**
533  * Callback received when the frontend's state changes.
534  */
535 static void frontend_changed(struct xenbus_device *dev,
536 			     enum xenbus_state frontend_state)
537 {
538 	struct backend_info *be = dev_get_drvdata(&dev->dev);
539 
540 	pr_debug("%s -> %s\n", dev->otherend, xenbus_strstate(frontend_state));
541 
542 	be->frontend_state = frontend_state;
543 
544 	switch (frontend_state) {
545 	case XenbusStateInitialising:
546 		set_backend_state(be, XenbusStateInitWait);
547 		break;
548 
549 	case XenbusStateInitialised:
550 		break;
551 
552 	case XenbusStateConnected:
553 		set_backend_state(be, XenbusStateConnected);
554 		break;
555 
556 	case XenbusStateClosing:
557 		set_backend_state(be, XenbusStateClosing);
558 		break;
559 
560 	case XenbusStateClosed:
561 		set_backend_state(be, XenbusStateClosed);
562 		if (xenbus_dev_is_online(dev))
563 			break;
564 		/* fall through if not online */
565 	case XenbusStateUnknown:
566 		set_backend_state(be, XenbusStateClosed);
567 		device_unregister(&dev->dev);
568 		break;
569 
570 	default:
571 		xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
572 				 frontend_state);
573 		break;
574 	}
575 }
576 
577 
578 static void xen_net_read_rate(struct xenbus_device *dev,
579 			      unsigned long *bytes, unsigned long *usec)
580 {
581 	char *s, *e;
582 	unsigned long b, u;
583 	char *ratestr;
584 
585 	/* Default to unlimited bandwidth. */
586 	*bytes = ~0UL;
587 	*usec = 0;
588 
589 	ratestr = xenbus_read(XBT_NIL, dev->nodename, "rate", NULL);
590 	if (IS_ERR(ratestr))
591 		return;
592 
593 	s = ratestr;
594 	b = simple_strtoul(s, &e, 10);
595 	if ((s == e) || (*e != ','))
596 		goto fail;
597 
598 	s = e + 1;
599 	u = simple_strtoul(s, &e, 10);
600 	if ((s == e) || (*e != '\0'))
601 		goto fail;
602 
603 	*bytes = b;
604 	*usec = u;
605 
606 	kfree(ratestr);
607 	return;
608 
609  fail:
610 	pr_warn("Failed to parse network rate limit. Traffic unlimited.\n");
611 	kfree(ratestr);
612 }
613 
614 static int xen_net_read_mac(struct xenbus_device *dev, u8 mac[])
615 {
616 	char *s, *e, *macstr;
617 	int i;
618 
619 	macstr = s = xenbus_read(XBT_NIL, dev->nodename, "mac", NULL);
620 	if (IS_ERR(macstr))
621 		return PTR_ERR(macstr);
622 
623 	for (i = 0; i < ETH_ALEN; i++) {
624 		mac[i] = simple_strtoul(s, &e, 16);
625 		if ((s == e) || (*e != ((i == ETH_ALEN-1) ? '\0' : ':'))) {
626 			kfree(macstr);
627 			return -ENOENT;
628 		}
629 		s = e+1;
630 	}
631 
632 	kfree(macstr);
633 	return 0;
634 }
635 
636 static void unregister_hotplug_status_watch(struct backend_info *be)
637 {
638 	if (be->have_hotplug_status_watch) {
639 		unregister_xenbus_watch(&be->hotplug_status_watch);
640 		kfree(be->hotplug_status_watch.node);
641 	}
642 	be->have_hotplug_status_watch = 0;
643 }
644 
645 static void hotplug_status_changed(struct xenbus_watch *watch,
646 				   const char **vec,
647 				   unsigned int vec_size)
648 {
649 	struct backend_info *be = container_of(watch,
650 					       struct backend_info,
651 					       hotplug_status_watch);
652 	char *str;
653 	unsigned int len;
654 
655 	str = xenbus_read(XBT_NIL, be->dev->nodename, "hotplug-status", &len);
656 	if (IS_ERR(str))
657 		return;
658 	if (len == sizeof("connected")-1 && !memcmp(str, "connected", len)) {
659 		/* Complete any pending state change */
660 		xenbus_switch_state(be->dev, be->state);
661 
662 		/* Not interested in this watch anymore. */
663 		unregister_hotplug_status_watch(be);
664 	}
665 	kfree(str);
666 }
667 
668 static void connect(struct backend_info *be)
669 {
670 	int err;
671 	struct xenbus_device *dev = be->dev;
672 	unsigned long credit_bytes, credit_usec;
673 	unsigned int queue_index;
674 	unsigned int requested_num_queues;
675 	struct xenvif_queue *queue;
676 
677 	/* Check whether the frontend requested multiple queues
678 	 * and read the number requested.
679 	 */
680 	err = xenbus_scanf(XBT_NIL, dev->otherend,
681 			   "multi-queue-num-queues",
682 			   "%u", &requested_num_queues);
683 	if (err < 0) {
684 		requested_num_queues = 1; /* Fall back to single queue */
685 	} else if (requested_num_queues > xenvif_max_queues) {
686 		/* buggy or malicious guest */
687 		xenbus_dev_fatal(dev, err,
688 				 "guest requested %u queues, exceeding the maximum of %u.",
689 				 requested_num_queues, xenvif_max_queues);
690 		return;
691 	}
692 
693 	err = xen_net_read_mac(dev, be->vif->fe_dev_addr);
694 	if (err) {
695 		xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename);
696 		return;
697 	}
698 
699 	xen_net_read_rate(dev, &credit_bytes, &credit_usec);
700 	read_xenbus_vif_flags(be);
701 
702 	/* Use the number of queues requested by the frontend */
703 	be->vif->queues = vzalloc(requested_num_queues *
704 				  sizeof(struct xenvif_queue));
705 	be->vif->num_queues = requested_num_queues;
706 
707 	for (queue_index = 0; queue_index < requested_num_queues; ++queue_index) {
708 		queue = &be->vif->queues[queue_index];
709 		queue->vif = be->vif;
710 		queue->id = queue_index;
711 		snprintf(queue->name, sizeof(queue->name), "%s-q%u",
712 				be->vif->dev->name, queue->id);
713 
714 		err = xenvif_init_queue(queue);
715 		if (err) {
716 			/* xenvif_init_queue() cleans up after itself on
717 			 * failure, but we need to clean up any previously
718 			 * initialised queues. Set num_queues to i so that
719 			 * earlier queues can be destroyed using the regular
720 			 * disconnect logic.
721 			 */
722 			be->vif->num_queues = queue_index;
723 			goto err;
724 		}
725 
726 		queue->remaining_credit = credit_bytes;
727 
728 		err = connect_rings(be, queue);
729 		if (err) {
730 			/* connect_rings() cleans up after itself on failure,
731 			 * but we need to clean up after xenvif_init_queue() here,
732 			 * and also clean up any previously initialised queues.
733 			 */
734 			xenvif_deinit_queue(queue);
735 			be->vif->num_queues = queue_index;
736 			goto err;
737 		}
738 	}
739 
740 #ifdef CONFIG_DEBUG_FS
741 	xenvif_debugfs_addif(be->vif);
742 #endif /* CONFIG_DEBUG_FS */
743 
744 	/* Initialisation completed, tell core driver the number of
745 	 * active queues.
746 	 */
747 	rtnl_lock();
748 	netif_set_real_num_tx_queues(be->vif->dev, requested_num_queues);
749 	netif_set_real_num_rx_queues(be->vif->dev, requested_num_queues);
750 	rtnl_unlock();
751 
752 	xenvif_carrier_on(be->vif);
753 
754 	unregister_hotplug_status_watch(be);
755 	err = xenbus_watch_pathfmt(dev, &be->hotplug_status_watch,
756 				   hotplug_status_changed,
757 				   "%s/%s", dev->nodename, "hotplug-status");
758 	if (!err)
759 		be->have_hotplug_status_watch = 1;
760 
761 	netif_tx_wake_all_queues(be->vif->dev);
762 
763 	return;
764 
765 err:
766 	if (be->vif->num_queues > 0)
767 		xenvif_disconnect(be->vif); /* Clean up existing queues */
768 	vfree(be->vif->queues);
769 	be->vif->queues = NULL;
770 	be->vif->num_queues = 0;
771 	return;
772 }
773 
774 
775 static int connect_rings(struct backend_info *be, struct xenvif_queue *queue)
776 {
777 	struct xenbus_device *dev = be->dev;
778 	unsigned int num_queues = queue->vif->num_queues;
779 	unsigned long tx_ring_ref, rx_ring_ref;
780 	unsigned int tx_evtchn, rx_evtchn;
781 	int err;
782 	char *xspath;
783 	size_t xspathsize;
784 	const size_t xenstore_path_ext_size = 11; /* sufficient for "/queue-NNN" */
785 
786 	/* If the frontend requested 1 queue, or we have fallen back
787 	 * to single queue due to lack of frontend support for multi-
788 	 * queue, expect the remaining XenStore keys in the toplevel
789 	 * directory. Otherwise, expect them in a subdirectory called
790 	 * queue-N.
791 	 */
792 	if (num_queues == 1) {
793 		xspath = kzalloc(strlen(dev->otherend) + 1, GFP_KERNEL);
794 		if (!xspath) {
795 			xenbus_dev_fatal(dev, -ENOMEM,
796 					 "reading ring references");
797 			return -ENOMEM;
798 		}
799 		strcpy(xspath, dev->otherend);
800 	} else {
801 		xspathsize = strlen(dev->otherend) + xenstore_path_ext_size;
802 		xspath = kzalloc(xspathsize, GFP_KERNEL);
803 		if (!xspath) {
804 			xenbus_dev_fatal(dev, -ENOMEM,
805 					 "reading ring references");
806 			return -ENOMEM;
807 		}
808 		snprintf(xspath, xspathsize, "%s/queue-%u", dev->otherend,
809 			 queue->id);
810 	}
811 
812 	err = xenbus_gather(XBT_NIL, xspath,
813 			    "tx-ring-ref", "%lu", &tx_ring_ref,
814 			    "rx-ring-ref", "%lu", &rx_ring_ref, NULL);
815 	if (err) {
816 		xenbus_dev_fatal(dev, err,
817 				 "reading %s/ring-ref",
818 				 xspath);
819 		goto err;
820 	}
821 
822 	/* Try split event channels first, then single event channel. */
823 	err = xenbus_gather(XBT_NIL, xspath,
824 			    "event-channel-tx", "%u", &tx_evtchn,
825 			    "event-channel-rx", "%u", &rx_evtchn, NULL);
826 	if (err < 0) {
827 		err = xenbus_scanf(XBT_NIL, xspath,
828 				   "event-channel", "%u", &tx_evtchn);
829 		if (err < 0) {
830 			xenbus_dev_fatal(dev, err,
831 					 "reading %s/event-channel(-tx/rx)",
832 					 xspath);
833 			goto err;
834 		}
835 		rx_evtchn = tx_evtchn;
836 	}
837 
838 	/* Map the shared frame, irq etc. */
839 	err = xenvif_connect(queue, tx_ring_ref, rx_ring_ref,
840 			     tx_evtchn, rx_evtchn);
841 	if (err) {
842 		xenbus_dev_fatal(dev, err,
843 				 "mapping shared-frames %lu/%lu port tx %u rx %u",
844 				 tx_ring_ref, rx_ring_ref,
845 				 tx_evtchn, rx_evtchn);
846 		goto err;
847 	}
848 
849 	err = 0;
850 err: /* Regular return falls through with err == 0 */
851 	kfree(xspath);
852 	return err;
853 }
854 
855 static int read_xenbus_vif_flags(struct backend_info *be)
856 {
857 	struct xenvif *vif = be->vif;
858 	struct xenbus_device *dev = be->dev;
859 	unsigned int rx_copy;
860 	int err, val;
861 
862 	err = xenbus_scanf(XBT_NIL, dev->otherend, "request-rx-copy", "%u",
863 			   &rx_copy);
864 	if (err == -ENOENT) {
865 		err = 0;
866 		rx_copy = 0;
867 	}
868 	if (err < 0) {
869 		xenbus_dev_fatal(dev, err, "reading %s/request-rx-copy",
870 				 dev->otherend);
871 		return err;
872 	}
873 	if (!rx_copy)
874 		return -EOPNOTSUPP;
875 
876 	if (vif->dev->tx_queue_len != 0) {
877 		if (xenbus_scanf(XBT_NIL, dev->otherend,
878 				 "feature-rx-notify", "%d", &val) < 0)
879 			val = 0;
880 		if (val)
881 			vif->can_queue = 1;
882 		else
883 			/* Must be non-zero for pfifo_fast to work. */
884 			vif->dev->tx_queue_len = 1;
885 	}
886 
887 	if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-sg",
888 			 "%d", &val) < 0)
889 		val = 0;
890 	vif->can_sg = !!val;
891 
892 	vif->gso_mask = 0;
893 	vif->gso_prefix_mask = 0;
894 
895 	if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-gso-tcpv4",
896 			 "%d", &val) < 0)
897 		val = 0;
898 	if (val)
899 		vif->gso_mask |= GSO_BIT(TCPV4);
900 
901 	if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-gso-tcpv4-prefix",
902 			 "%d", &val) < 0)
903 		val = 0;
904 	if (val)
905 		vif->gso_prefix_mask |= GSO_BIT(TCPV4);
906 
907 	if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-gso-tcpv6",
908 			 "%d", &val) < 0)
909 		val = 0;
910 	if (val)
911 		vif->gso_mask |= GSO_BIT(TCPV6);
912 
913 	if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-gso-tcpv6-prefix",
914 			 "%d", &val) < 0)
915 		val = 0;
916 	if (val)
917 		vif->gso_prefix_mask |= GSO_BIT(TCPV6);
918 
919 	if (vif->gso_mask & vif->gso_prefix_mask) {
920 		xenbus_dev_fatal(dev, err,
921 				 "%s: gso and gso prefix flags are not "
922 				 "mutually exclusive",
923 				 dev->otherend);
924 		return -EOPNOTSUPP;
925 	}
926 
927 	if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-no-csum-offload",
928 			 "%d", &val) < 0)
929 		val = 0;
930 	vif->ip_csum = !val;
931 
932 	if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-ipv6-csum-offload",
933 			 "%d", &val) < 0)
934 		val = 0;
935 	vif->ipv6_csum = !!val;
936 
937 	return 0;
938 }
939 
940 
941 /* ** Driver Registration ** */
942 
943 
944 static const struct xenbus_device_id netback_ids[] = {
945 	{ "vif" },
946 	{ "" }
947 };
948 
949 
950 static DEFINE_XENBUS_DRIVER(netback, ,
951 	.probe = netback_probe,
952 	.remove = netback_remove,
953 	.uevent = netback_uevent,
954 	.otherend_changed = frontend_changed,
955 );
956 
957 int xenvif_xenbus_init(void)
958 {
959 	return xenbus_register_backend(&netback_driver);
960 }
961 
962 void xenvif_xenbus_fini(void)
963 {
964 	return xenbus_unregister_driver(&netback_driver);
965 }
966