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