xref: /openbmc/linux/drivers/net/xen-netback/xenbus.c (revision 388f6966)
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 	int i;
174 
175 	vif->xenvif_dbg_root = debugfs_create_dir(vif->dev->name,
176 						  xen_netback_dbg_root);
177 	for (i = 0; i < vif->num_queues; ++i) {
178 		char filename[sizeof("io_ring_q") + 4];
179 
180 		snprintf(filename, sizeof(filename), "io_ring_q%d", i);
181 		debugfs_create_file(filename, 0600, vif->xenvif_dbg_root,
182 				    &vif->queues[i],
183 				    &xenvif_dbg_io_ring_ops_fops);
184 	}
185 
186 	if (vif->ctrl_irq)
187 		debugfs_create_file("ctrl", 0400, vif->xenvif_dbg_root, vif,
188 				    &xenvif_ctrl_fops);
189 }
190 
191 static void xenvif_debugfs_delif(struct xenvif *vif)
192 {
193 	debugfs_remove_recursive(vif->xenvif_dbg_root);
194 	vif->xenvif_dbg_root = NULL;
195 }
196 #endif /* CONFIG_DEBUG_FS */
197 
198 /*
199  * Handle the creation of the hotplug script environment.  We add the script
200  * and vif variables to the environment, for the benefit of the vif-* hotplug
201  * scripts.
202  */
203 static int netback_uevent(struct xenbus_device *xdev,
204 			  struct kobj_uevent_env *env)
205 {
206 	struct backend_info *be = dev_get_drvdata(&xdev->dev);
207 
208 	if (!be)
209 		return 0;
210 
211 	if (add_uevent_var(env, "script=%s", be->hotplug_script))
212 		return -ENOMEM;
213 
214 	if (!be->vif)
215 		return 0;
216 
217 	return add_uevent_var(env, "vif=%s", be->vif->dev->name);
218 }
219 
220 
221 static int backend_create_xenvif(struct backend_info *be)
222 {
223 	int err;
224 	long handle;
225 	struct xenbus_device *dev = be->dev;
226 	struct xenvif *vif;
227 
228 	if (be->vif != NULL)
229 		return 0;
230 
231 	err = xenbus_scanf(XBT_NIL, dev->nodename, "handle", "%li", &handle);
232 	if (err != 1) {
233 		xenbus_dev_fatal(dev, err, "reading handle");
234 		return (err < 0) ? err : -EINVAL;
235 	}
236 
237 	vif = xenvif_alloc(&dev->dev, dev->otherend_id, handle);
238 	if (IS_ERR(vif)) {
239 		err = PTR_ERR(vif);
240 		xenbus_dev_fatal(dev, err, "creating interface");
241 		return err;
242 	}
243 	be->vif = vif;
244 	vif->be = be;
245 
246 	kobject_uevent(&dev->dev.kobj, KOBJ_ONLINE);
247 	return 0;
248 }
249 
250 static void backend_disconnect(struct backend_info *be)
251 {
252 	struct xenvif *vif = be->vif;
253 
254 	if (vif) {
255 		unsigned int num_queues = vif->num_queues;
256 		unsigned int queue_index;
257 
258 		xen_unregister_watchers(vif);
259 #ifdef CONFIG_DEBUG_FS
260 		xenvif_debugfs_delif(vif);
261 #endif /* CONFIG_DEBUG_FS */
262 		xenvif_disconnect_data(vif);
263 
264 		/* At this point some of the handlers may still be active
265 		 * so we need to have additional synchronization here.
266 		 */
267 		vif->num_queues = 0;
268 		synchronize_net();
269 
270 		for (queue_index = 0; queue_index < num_queues; ++queue_index)
271 			xenvif_deinit_queue(&vif->queues[queue_index]);
272 
273 		vfree(vif->queues);
274 		vif->queues = NULL;
275 
276 		xenvif_disconnect_ctrl(vif);
277 	}
278 }
279 
280 static void backend_connect(struct backend_info *be)
281 {
282 	if (be->vif)
283 		connect(be);
284 }
285 
286 static inline void backend_switch_state(struct backend_info *be,
287 					enum xenbus_state state)
288 {
289 	struct xenbus_device *dev = be->dev;
290 
291 	pr_debug("%s -> %s\n", dev->nodename, xenbus_strstate(state));
292 	be->state = state;
293 
294 	/* If we are waiting for a hotplug script then defer the
295 	 * actual xenbus state change.
296 	 */
297 	if (!be->have_hotplug_status_watch)
298 		xenbus_switch_state(dev, state);
299 }
300 
301 /* Handle backend state transitions:
302  *
303  * The backend state starts in Initialising and the following transitions are
304  * allowed.
305  *
306  * Initialising -> InitWait -> Connected
307  *          \
308  *           \        ^    \         |
309  *            \       |     \        |
310  *             \      |      \       |
311  *              \     |       \      |
312  *               \    |        \     |
313  *                \   |         \    |
314  *                 V  |          V   V
315  *
316  *                  Closed  <-> Closing
317  *
318  * The state argument specifies the eventual state of the backend and the
319  * function transitions to that state via the shortest path.
320  */
321 static void set_backend_state(struct backend_info *be,
322 			      enum xenbus_state state)
323 {
324 	while (be->state != state) {
325 		switch (be->state) {
326 		case XenbusStateInitialising:
327 			switch (state) {
328 			case XenbusStateInitWait:
329 			case XenbusStateConnected:
330 			case XenbusStateClosing:
331 				backend_switch_state(be, XenbusStateInitWait);
332 				break;
333 			case XenbusStateClosed:
334 				backend_switch_state(be, XenbusStateClosed);
335 				break;
336 			default:
337 				BUG();
338 			}
339 			break;
340 		case XenbusStateClosed:
341 			switch (state) {
342 			case XenbusStateInitWait:
343 			case XenbusStateConnected:
344 				backend_switch_state(be, XenbusStateInitWait);
345 				break;
346 			case XenbusStateClosing:
347 				backend_switch_state(be, XenbusStateClosing);
348 				break;
349 			default:
350 				BUG();
351 			}
352 			break;
353 		case XenbusStateInitWait:
354 			switch (state) {
355 			case XenbusStateConnected:
356 				backend_connect(be);
357 				backend_switch_state(be, XenbusStateConnected);
358 				break;
359 			case XenbusStateClosing:
360 			case XenbusStateClosed:
361 				backend_switch_state(be, XenbusStateClosing);
362 				break;
363 			default:
364 				BUG();
365 			}
366 			break;
367 		case XenbusStateConnected:
368 			switch (state) {
369 			case XenbusStateInitWait:
370 			case XenbusStateClosing:
371 			case XenbusStateClosed:
372 				backend_disconnect(be);
373 				backend_switch_state(be, XenbusStateClosing);
374 				break;
375 			default:
376 				BUG();
377 			}
378 			break;
379 		case XenbusStateClosing:
380 			switch (state) {
381 			case XenbusStateInitWait:
382 			case XenbusStateConnected:
383 			case XenbusStateClosed:
384 				backend_switch_state(be, XenbusStateClosed);
385 				break;
386 			default:
387 				BUG();
388 			}
389 			break;
390 		default:
391 			BUG();
392 		}
393 	}
394 }
395 
396 /**
397  * Callback received when the frontend's state changes.
398  */
399 static void frontend_changed(struct xenbus_device *dev,
400 			     enum xenbus_state frontend_state)
401 {
402 	struct backend_info *be = dev_get_drvdata(&dev->dev);
403 
404 	pr_debug("%s -> %s\n", dev->otherend, xenbus_strstate(frontend_state));
405 
406 	be->frontend_state = frontend_state;
407 
408 	switch (frontend_state) {
409 	case XenbusStateInitialising:
410 		set_backend_state(be, XenbusStateInitWait);
411 		break;
412 
413 	case XenbusStateInitialised:
414 		break;
415 
416 	case XenbusStateConnected:
417 		set_backend_state(be, XenbusStateConnected);
418 		break;
419 
420 	case XenbusStateClosing:
421 		set_backend_state(be, XenbusStateClosing);
422 		break;
423 
424 	case XenbusStateClosed:
425 		set_backend_state(be, XenbusStateClosed);
426 		if (xenbus_dev_is_online(dev))
427 			break;
428 		/* fall through - if not online */
429 	case XenbusStateUnknown:
430 		set_backend_state(be, XenbusStateClosed);
431 		device_unregister(&dev->dev);
432 		break;
433 
434 	default:
435 		xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
436 				 frontend_state);
437 		break;
438 	}
439 }
440 
441 
442 static void xen_net_read_rate(struct xenbus_device *dev,
443 			      unsigned long *bytes, unsigned long *usec)
444 {
445 	char *s, *e;
446 	unsigned long b, u;
447 	char *ratestr;
448 
449 	/* Default to unlimited bandwidth. */
450 	*bytes = ~0UL;
451 	*usec = 0;
452 
453 	ratestr = xenbus_read(XBT_NIL, dev->nodename, "rate", NULL);
454 	if (IS_ERR(ratestr))
455 		return;
456 
457 	s = ratestr;
458 	b = simple_strtoul(s, &e, 10);
459 	if ((s == e) || (*e != ','))
460 		goto fail;
461 
462 	s = e + 1;
463 	u = simple_strtoul(s, &e, 10);
464 	if ((s == e) || (*e != '\0'))
465 		goto fail;
466 
467 	*bytes = b;
468 	*usec = u;
469 
470 	kfree(ratestr);
471 	return;
472 
473  fail:
474 	pr_warn("Failed to parse network rate limit. Traffic unlimited.\n");
475 	kfree(ratestr);
476 }
477 
478 static int xen_net_read_mac(struct xenbus_device *dev, u8 mac[])
479 {
480 	char *s, *e, *macstr;
481 	int i;
482 
483 	macstr = s = xenbus_read(XBT_NIL, dev->nodename, "mac", NULL);
484 	if (IS_ERR(macstr))
485 		return PTR_ERR(macstr);
486 
487 	for (i = 0; i < ETH_ALEN; i++) {
488 		mac[i] = simple_strtoul(s, &e, 16);
489 		if ((s == e) || (*e != ((i == ETH_ALEN-1) ? '\0' : ':'))) {
490 			kfree(macstr);
491 			return -ENOENT;
492 		}
493 		s = e+1;
494 	}
495 
496 	kfree(macstr);
497 	return 0;
498 }
499 
500 static void xen_net_rate_changed(struct xenbus_watch *watch,
501 				 const char *path, const char *token)
502 {
503 	struct xenvif *vif = container_of(watch, struct xenvif, credit_watch);
504 	struct xenbus_device *dev = xenvif_to_xenbus_device(vif);
505 	unsigned long   credit_bytes;
506 	unsigned long   credit_usec;
507 	unsigned int queue_index;
508 
509 	xen_net_read_rate(dev, &credit_bytes, &credit_usec);
510 	for (queue_index = 0; queue_index < vif->num_queues; queue_index++) {
511 		struct xenvif_queue *queue = &vif->queues[queue_index];
512 
513 		queue->credit_bytes = credit_bytes;
514 		queue->credit_usec = credit_usec;
515 		if (!mod_timer_pending(&queue->credit_timeout, jiffies) &&
516 			queue->remaining_credit > queue->credit_bytes) {
517 			queue->remaining_credit = queue->credit_bytes;
518 		}
519 	}
520 }
521 
522 static int xen_register_credit_watch(struct xenbus_device *dev,
523 				     struct xenvif *vif)
524 {
525 	int err = 0;
526 	char *node;
527 	unsigned maxlen = strlen(dev->nodename) + sizeof("/rate");
528 
529 	if (vif->credit_watch.node)
530 		return -EADDRINUSE;
531 
532 	node = kmalloc(maxlen, GFP_KERNEL);
533 	if (!node)
534 		return -ENOMEM;
535 	snprintf(node, maxlen, "%s/rate", dev->nodename);
536 	vif->credit_watch.node = node;
537 	vif->credit_watch.callback = xen_net_rate_changed;
538 	err = register_xenbus_watch(&vif->credit_watch);
539 	if (err) {
540 		pr_err("Failed to set watcher %s\n", vif->credit_watch.node);
541 		kfree(node);
542 		vif->credit_watch.node = NULL;
543 		vif->credit_watch.callback = NULL;
544 	}
545 	return err;
546 }
547 
548 static void xen_unregister_credit_watch(struct xenvif *vif)
549 {
550 	if (vif->credit_watch.node) {
551 		unregister_xenbus_watch(&vif->credit_watch);
552 		kfree(vif->credit_watch.node);
553 		vif->credit_watch.node = NULL;
554 	}
555 }
556 
557 static void xen_mcast_ctrl_changed(struct xenbus_watch *watch,
558 				   const char *path, const char *token)
559 {
560 	struct xenvif *vif = container_of(watch, struct xenvif,
561 					  mcast_ctrl_watch);
562 	struct xenbus_device *dev = xenvif_to_xenbus_device(vif);
563 
564 	vif->multicast_control = !!xenbus_read_unsigned(dev->otherend,
565 					"request-multicast-control", 0);
566 }
567 
568 static int xen_register_mcast_ctrl_watch(struct xenbus_device *dev,
569 					 struct xenvif *vif)
570 {
571 	int err = 0;
572 	char *node;
573 	unsigned maxlen = strlen(dev->otherend) +
574 		sizeof("/request-multicast-control");
575 
576 	if (vif->mcast_ctrl_watch.node) {
577 		pr_err_ratelimited("Watch is already registered\n");
578 		return -EADDRINUSE;
579 	}
580 
581 	node = kmalloc(maxlen, GFP_KERNEL);
582 	if (!node) {
583 		pr_err("Failed to allocate memory for watch\n");
584 		return -ENOMEM;
585 	}
586 	snprintf(node, maxlen, "%s/request-multicast-control",
587 		 dev->otherend);
588 	vif->mcast_ctrl_watch.node = node;
589 	vif->mcast_ctrl_watch.callback = xen_mcast_ctrl_changed;
590 	err = register_xenbus_watch(&vif->mcast_ctrl_watch);
591 	if (err) {
592 		pr_err("Failed to set watcher %s\n",
593 		       vif->mcast_ctrl_watch.node);
594 		kfree(node);
595 		vif->mcast_ctrl_watch.node = NULL;
596 		vif->mcast_ctrl_watch.callback = NULL;
597 	}
598 	return err;
599 }
600 
601 static void xen_unregister_mcast_ctrl_watch(struct xenvif *vif)
602 {
603 	if (vif->mcast_ctrl_watch.node) {
604 		unregister_xenbus_watch(&vif->mcast_ctrl_watch);
605 		kfree(vif->mcast_ctrl_watch.node);
606 		vif->mcast_ctrl_watch.node = NULL;
607 	}
608 }
609 
610 static void xen_register_watchers(struct xenbus_device *dev,
611 				  struct xenvif *vif)
612 {
613 	xen_register_credit_watch(dev, vif);
614 	xen_register_mcast_ctrl_watch(dev, vif);
615 }
616 
617 static void xen_unregister_watchers(struct xenvif *vif)
618 {
619 	xen_unregister_mcast_ctrl_watch(vif);
620 	xen_unregister_credit_watch(vif);
621 }
622 
623 static void unregister_hotplug_status_watch(struct backend_info *be)
624 {
625 	if (be->have_hotplug_status_watch) {
626 		unregister_xenbus_watch(&be->hotplug_status_watch);
627 		kfree(be->hotplug_status_watch.node);
628 	}
629 	be->have_hotplug_status_watch = 0;
630 }
631 
632 static void hotplug_status_changed(struct xenbus_watch *watch,
633 				   const char *path,
634 				   const char *token)
635 {
636 	struct backend_info *be = container_of(watch,
637 					       struct backend_info,
638 					       hotplug_status_watch);
639 	char *str;
640 	unsigned int len;
641 
642 	str = xenbus_read(XBT_NIL, be->dev->nodename, "hotplug-status", &len);
643 	if (IS_ERR(str))
644 		return;
645 	if (len == sizeof("connected")-1 && !memcmp(str, "connected", len)) {
646 		/* Complete any pending state change */
647 		xenbus_switch_state(be->dev, be->state);
648 
649 		/* Not interested in this watch anymore. */
650 		unregister_hotplug_status_watch(be);
651 		xenbus_rm(XBT_NIL, be->dev->nodename, "hotplug-status");
652 	}
653 	kfree(str);
654 }
655 
656 static int connect_ctrl_ring(struct backend_info *be)
657 {
658 	struct xenbus_device *dev = be->dev;
659 	struct xenvif *vif = be->vif;
660 	unsigned int val;
661 	grant_ref_t ring_ref;
662 	unsigned int evtchn;
663 	int err;
664 
665 	err = xenbus_scanf(XBT_NIL, dev->otherend,
666 			   "ctrl-ring-ref", "%u", &val);
667 	if (err < 0)
668 		goto done; /* The frontend does not have a control ring */
669 
670 	ring_ref = val;
671 
672 	err = xenbus_scanf(XBT_NIL, dev->otherend,
673 			   "event-channel-ctrl", "%u", &val);
674 	if (err < 0) {
675 		xenbus_dev_fatal(dev, err,
676 				 "reading %s/event-channel-ctrl",
677 				 dev->otherend);
678 		goto fail;
679 	}
680 
681 	evtchn = val;
682 
683 	err = xenvif_connect_ctrl(vif, ring_ref, evtchn);
684 	if (err) {
685 		xenbus_dev_fatal(dev, err,
686 				 "mapping shared-frame %u port %u",
687 				 ring_ref, evtchn);
688 		goto fail;
689 	}
690 
691 done:
692 	return 0;
693 
694 fail:
695 	return err;
696 }
697 
698 static void connect(struct backend_info *be)
699 {
700 	int err;
701 	struct xenbus_device *dev = be->dev;
702 	unsigned long credit_bytes, credit_usec;
703 	unsigned int queue_index;
704 	unsigned int requested_num_queues;
705 	struct xenvif_queue *queue;
706 
707 	/* Check whether the frontend requested multiple queues
708 	 * and read the number requested.
709 	 */
710 	requested_num_queues = xenbus_read_unsigned(dev->otherend,
711 					"multi-queue-num-queues", 1);
712 	if (requested_num_queues > xenvif_max_queues) {
713 		/* buggy or malicious guest */
714 		xenbus_dev_fatal(dev, -EINVAL,
715 				 "guest requested %u queues, exceeding the maximum of %u.",
716 				 requested_num_queues, xenvif_max_queues);
717 		return;
718 	}
719 
720 	err = xen_net_read_mac(dev, be->vif->fe_dev_addr);
721 	if (err) {
722 		xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename);
723 		return;
724 	}
725 
726 	xen_net_read_rate(dev, &credit_bytes, &credit_usec);
727 	xen_unregister_watchers(be->vif);
728 	xen_register_watchers(dev, be->vif);
729 	read_xenbus_vif_flags(be);
730 
731 	err = connect_ctrl_ring(be);
732 	if (err) {
733 		xenbus_dev_fatal(dev, err, "connecting control ring");
734 		return;
735 	}
736 
737 	/* Use the number of queues requested by the frontend */
738 	be->vif->queues = vzalloc(array_size(requested_num_queues,
739 					     sizeof(struct xenvif_queue)));
740 	if (!be->vif->queues) {
741 		xenbus_dev_fatal(dev, -ENOMEM,
742 				 "allocating queues");
743 		return;
744 	}
745 
746 	be->vif->num_queues = requested_num_queues;
747 	be->vif->stalled_queues = requested_num_queues;
748 
749 	for (queue_index = 0; queue_index < requested_num_queues; ++queue_index) {
750 		queue = &be->vif->queues[queue_index];
751 		queue->vif = be->vif;
752 		queue->id = queue_index;
753 		snprintf(queue->name, sizeof(queue->name), "%s-q%u",
754 				be->vif->dev->name, queue->id);
755 
756 		err = xenvif_init_queue(queue);
757 		if (err) {
758 			/* xenvif_init_queue() cleans up after itself on
759 			 * failure, but we need to clean up any previously
760 			 * initialised queues. Set num_queues to i so that
761 			 * earlier queues can be destroyed using the regular
762 			 * disconnect logic.
763 			 */
764 			be->vif->num_queues = queue_index;
765 			goto err;
766 		}
767 
768 		queue->credit_bytes = credit_bytes;
769 		queue->remaining_credit = credit_bytes;
770 		queue->credit_usec = credit_usec;
771 
772 		err = connect_data_rings(be, queue);
773 		if (err) {
774 			/* connect_data_rings() cleans up after itself on
775 			 * failure, but we need to clean up after
776 			 * xenvif_init_queue() here, and also clean up any
777 			 * previously initialised queues.
778 			 */
779 			xenvif_deinit_queue(queue);
780 			be->vif->num_queues = queue_index;
781 			goto err;
782 		}
783 	}
784 
785 #ifdef CONFIG_DEBUG_FS
786 	xenvif_debugfs_addif(be->vif);
787 #endif /* CONFIG_DEBUG_FS */
788 
789 	/* Initialisation completed, tell core driver the number of
790 	 * active queues.
791 	 */
792 	rtnl_lock();
793 	netif_set_real_num_tx_queues(be->vif->dev, requested_num_queues);
794 	netif_set_real_num_rx_queues(be->vif->dev, requested_num_queues);
795 	rtnl_unlock();
796 
797 	xenvif_carrier_on(be->vif);
798 
799 	unregister_hotplug_status_watch(be);
800 	err = xenbus_watch_pathfmt(dev, &be->hotplug_status_watch,
801 				   hotplug_status_changed,
802 				   "%s/%s", dev->nodename, "hotplug-status");
803 	if (!err)
804 		be->have_hotplug_status_watch = 1;
805 
806 	netif_tx_wake_all_queues(be->vif->dev);
807 
808 	return;
809 
810 err:
811 	if (be->vif->num_queues > 0)
812 		xenvif_disconnect_data(be->vif); /* Clean up existing queues */
813 	for (queue_index = 0; queue_index < be->vif->num_queues; ++queue_index)
814 		xenvif_deinit_queue(&be->vif->queues[queue_index]);
815 	vfree(be->vif->queues);
816 	be->vif->queues = NULL;
817 	be->vif->num_queues = 0;
818 	xenvif_disconnect_ctrl(be->vif);
819 	return;
820 }
821 
822 
823 static int connect_data_rings(struct backend_info *be,
824 			      struct xenvif_queue *queue)
825 {
826 	struct xenbus_device *dev = be->dev;
827 	unsigned int num_queues = queue->vif->num_queues;
828 	unsigned long tx_ring_ref, rx_ring_ref;
829 	unsigned int tx_evtchn, rx_evtchn;
830 	int err;
831 	char *xspath;
832 	size_t xspathsize;
833 	const size_t xenstore_path_ext_size = 11; /* sufficient for "/queue-NNN" */
834 
835 	/* If the frontend requested 1 queue, or we have fallen back
836 	 * to single queue due to lack of frontend support for multi-
837 	 * queue, expect the remaining XenStore keys in the toplevel
838 	 * directory. Otherwise, expect them in a subdirectory called
839 	 * queue-N.
840 	 */
841 	if (num_queues == 1) {
842 		xspath = kzalloc(strlen(dev->otherend) + 1, GFP_KERNEL);
843 		if (!xspath) {
844 			xenbus_dev_fatal(dev, -ENOMEM,
845 					 "reading ring references");
846 			return -ENOMEM;
847 		}
848 		strcpy(xspath, dev->otherend);
849 	} else {
850 		xspathsize = strlen(dev->otherend) + xenstore_path_ext_size;
851 		xspath = kzalloc(xspathsize, GFP_KERNEL);
852 		if (!xspath) {
853 			xenbus_dev_fatal(dev, -ENOMEM,
854 					 "reading ring references");
855 			return -ENOMEM;
856 		}
857 		snprintf(xspath, xspathsize, "%s/queue-%u", dev->otherend,
858 			 queue->id);
859 	}
860 
861 	err = xenbus_gather(XBT_NIL, xspath,
862 			    "tx-ring-ref", "%lu", &tx_ring_ref,
863 			    "rx-ring-ref", "%lu", &rx_ring_ref, NULL);
864 	if (err) {
865 		xenbus_dev_fatal(dev, err,
866 				 "reading %s/ring-ref",
867 				 xspath);
868 		goto err;
869 	}
870 
871 	/* Try split event channels first, then single event channel. */
872 	err = xenbus_gather(XBT_NIL, xspath,
873 			    "event-channel-tx", "%u", &tx_evtchn,
874 			    "event-channel-rx", "%u", &rx_evtchn, NULL);
875 	if (err < 0) {
876 		err = xenbus_scanf(XBT_NIL, xspath,
877 				   "event-channel", "%u", &tx_evtchn);
878 		if (err < 0) {
879 			xenbus_dev_fatal(dev, err,
880 					 "reading %s/event-channel(-tx/rx)",
881 					 xspath);
882 			goto err;
883 		}
884 		rx_evtchn = tx_evtchn;
885 	}
886 
887 	/* Map the shared frame, irq etc. */
888 	err = xenvif_connect_data(queue, tx_ring_ref, rx_ring_ref,
889 				  tx_evtchn, rx_evtchn);
890 	if (err) {
891 		xenbus_dev_fatal(dev, err,
892 				 "mapping shared-frames %lu/%lu port tx %u rx %u",
893 				 tx_ring_ref, rx_ring_ref,
894 				 tx_evtchn, rx_evtchn);
895 		goto err;
896 	}
897 
898 	err = 0;
899 err: /* Regular return falls through with err == 0 */
900 	kfree(xspath);
901 	return err;
902 }
903 
904 static int read_xenbus_vif_flags(struct backend_info *be)
905 {
906 	struct xenvif *vif = be->vif;
907 	struct xenbus_device *dev = be->dev;
908 	unsigned int rx_copy;
909 	int err;
910 
911 	err = xenbus_scanf(XBT_NIL, dev->otherend, "request-rx-copy", "%u",
912 			   &rx_copy);
913 	if (err == -ENOENT) {
914 		err = 0;
915 		rx_copy = 0;
916 	}
917 	if (err < 0) {
918 		xenbus_dev_fatal(dev, err, "reading %s/request-rx-copy",
919 				 dev->otherend);
920 		return err;
921 	}
922 	if (!rx_copy)
923 		return -EOPNOTSUPP;
924 
925 	if (!xenbus_read_unsigned(dev->otherend, "feature-rx-notify", 0)) {
926 		/* - Reduce drain timeout to poll more frequently for
927 		 *   Rx requests.
928 		 * - Disable Rx stall detection.
929 		 */
930 		be->vif->drain_timeout = msecs_to_jiffies(30);
931 		be->vif->stall_timeout = 0;
932 	}
933 
934 	vif->can_sg = !!xenbus_read_unsigned(dev->otherend, "feature-sg", 0);
935 
936 	vif->gso_mask = 0;
937 
938 	if (xenbus_read_unsigned(dev->otherend, "feature-gso-tcpv4", 0))
939 		vif->gso_mask |= GSO_BIT(TCPV4);
940 
941 	if (xenbus_read_unsigned(dev->otherend, "feature-gso-tcpv6", 0))
942 		vif->gso_mask |= GSO_BIT(TCPV6);
943 
944 	vif->ip_csum = !xenbus_read_unsigned(dev->otherend,
945 					     "feature-no-csum-offload", 0);
946 
947 	vif->ipv6_csum = !!xenbus_read_unsigned(dev->otherend,
948 						"feature-ipv6-csum-offload", 0);
949 
950 	return 0;
951 }
952 
953 static int netback_remove(struct xenbus_device *dev)
954 {
955 	struct backend_info *be = dev_get_drvdata(&dev->dev);
956 
957 	unregister_hotplug_status_watch(be);
958 	if (be->vif) {
959 		kobject_uevent(&dev->dev.kobj, KOBJ_OFFLINE);
960 		backend_disconnect(be);
961 		xenvif_free(be->vif);
962 		be->vif = NULL;
963 	}
964 	kfree(be->hotplug_script);
965 	kfree(be);
966 	dev_set_drvdata(&dev->dev, NULL);
967 	return 0;
968 }
969 
970 /**
971  * Entry point to this code when a new device is created.  Allocate the basic
972  * structures and switch to InitWait.
973  */
974 static int netback_probe(struct xenbus_device *dev,
975 			 const struct xenbus_device_id *id)
976 {
977 	const char *message;
978 	struct xenbus_transaction xbt;
979 	int err;
980 	int sg;
981 	const char *script;
982 	struct backend_info *be = kzalloc(sizeof(*be), GFP_KERNEL);
983 
984 	if (!be) {
985 		xenbus_dev_fatal(dev, -ENOMEM,
986 				 "allocating backend structure");
987 		return -ENOMEM;
988 	}
989 
990 	be->dev = dev;
991 	dev_set_drvdata(&dev->dev, be);
992 
993 	sg = 1;
994 
995 	do {
996 		err = xenbus_transaction_start(&xbt);
997 		if (err) {
998 			xenbus_dev_fatal(dev, err, "starting transaction");
999 			goto fail;
1000 		}
1001 
1002 		err = xenbus_printf(xbt, dev->nodename, "feature-sg", "%d", sg);
1003 		if (err) {
1004 			message = "writing feature-sg";
1005 			goto abort_transaction;
1006 		}
1007 
1008 		err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv4",
1009 				    "%d", sg);
1010 		if (err) {
1011 			message = "writing feature-gso-tcpv4";
1012 			goto abort_transaction;
1013 		}
1014 
1015 		err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv6",
1016 				    "%d", sg);
1017 		if (err) {
1018 			message = "writing feature-gso-tcpv6";
1019 			goto abort_transaction;
1020 		}
1021 
1022 		/* We support partial checksum setup for IPv6 packets */
1023 		err = xenbus_printf(xbt, dev->nodename,
1024 				    "feature-ipv6-csum-offload",
1025 				    "%d", 1);
1026 		if (err) {
1027 			message = "writing feature-ipv6-csum-offload";
1028 			goto abort_transaction;
1029 		}
1030 
1031 		/* We support rx-copy path. */
1032 		err = xenbus_printf(xbt, dev->nodename,
1033 				    "feature-rx-copy", "%d", 1);
1034 		if (err) {
1035 			message = "writing feature-rx-copy";
1036 			goto abort_transaction;
1037 		}
1038 
1039 		/* We don't support rx-flip path (except old guests who
1040 		 * don't grok this feature flag).
1041 		 */
1042 		err = xenbus_printf(xbt, dev->nodename,
1043 				    "feature-rx-flip", "%d", 0);
1044 		if (err) {
1045 			message = "writing feature-rx-flip";
1046 			goto abort_transaction;
1047 		}
1048 
1049 		/* We support dynamic multicast-control. */
1050 		err = xenbus_printf(xbt, dev->nodename,
1051 				    "feature-multicast-control", "%d", 1);
1052 		if (err) {
1053 			message = "writing feature-multicast-control";
1054 			goto abort_transaction;
1055 		}
1056 
1057 		err = xenbus_printf(xbt, dev->nodename,
1058 				    "feature-dynamic-multicast-control",
1059 				    "%d", 1);
1060 		if (err) {
1061 			message = "writing feature-dynamic-multicast-control";
1062 			goto abort_transaction;
1063 		}
1064 
1065 		err = xenbus_transaction_end(xbt, 0);
1066 	} while (err == -EAGAIN);
1067 
1068 	if (err) {
1069 		xenbus_dev_fatal(dev, err, "completing transaction");
1070 		goto fail;
1071 	}
1072 
1073 	/* Split event channels support, this is optional so it is not
1074 	 * put inside the above loop.
1075 	 */
1076 	err = xenbus_printf(XBT_NIL, dev->nodename,
1077 			    "feature-split-event-channels",
1078 			    "%u", separate_tx_rx_irq);
1079 	if (err)
1080 		pr_debug("Error writing feature-split-event-channels\n");
1081 
1082 	/* Multi-queue support: This is an optional feature. */
1083 	err = xenbus_printf(XBT_NIL, dev->nodename,
1084 			    "multi-queue-max-queues", "%u", xenvif_max_queues);
1085 	if (err)
1086 		pr_debug("Error writing multi-queue-max-queues\n");
1087 
1088 	err = xenbus_printf(XBT_NIL, dev->nodename,
1089 			    "feature-ctrl-ring",
1090 			    "%u", true);
1091 	if (err)
1092 		pr_debug("Error writing feature-ctrl-ring\n");
1093 
1094 	backend_switch_state(be, XenbusStateInitWait);
1095 
1096 	script = xenbus_read(XBT_NIL, dev->nodename, "script", NULL);
1097 	if (IS_ERR(script)) {
1098 		err = PTR_ERR(script);
1099 		xenbus_dev_fatal(dev, err, "reading script");
1100 		goto fail;
1101 	}
1102 
1103 	be->hotplug_script = script;
1104 
1105 	/* This kicks hotplug scripts, so do it immediately. */
1106 	err = backend_create_xenvif(be);
1107 	if (err)
1108 		goto fail;
1109 
1110 	return 0;
1111 
1112 abort_transaction:
1113 	xenbus_transaction_end(xbt, 1);
1114 	xenbus_dev_fatal(dev, err, "%s", message);
1115 fail:
1116 	pr_debug("failed\n");
1117 	netback_remove(dev);
1118 	return err;
1119 }
1120 
1121 static const struct xenbus_device_id netback_ids[] = {
1122 	{ "vif" },
1123 	{ "" }
1124 };
1125 
1126 static struct xenbus_driver netback_driver = {
1127 	.ids = netback_ids,
1128 	.probe = netback_probe,
1129 	.remove = netback_remove,
1130 	.uevent = netback_uevent,
1131 	.otherend_changed = frontend_changed,
1132 	.allow_rebind = true,
1133 };
1134 
1135 int xenvif_xenbus_init(void)
1136 {
1137 	return xenbus_register_backend(&netback_driver);
1138 }
1139 
1140 void xenvif_xenbus_fini(void)
1141 {
1142 	return xenbus_unregister_driver(&netback_driver);
1143 }
1144