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