xen networking: add basic XDP support for xen-netfront
[linux-block.git] / drivers / net / xen-netback / xenbus.c
CommitLineData
1ccea77e 1// SPDX-License-Identifier: GPL-2.0-or-later
f942dc25
IC
2/*
3 * Xenbus code for netif backend
4 *
5 * Copyright (C) 2005 Rusty Russell <rusty@rustcorp.com.au>
6 * Copyright (C) 2005 XenSource Ltd
f942dc25
IC
7*/
8
9#include "common.h"
e9ce7cb6
WL
10#include <linux/vmalloc.h>
11#include <linux/rtnetlink.h>
f942dc25 12
4e15ee2c
PD
13static int connect_data_rings(struct backend_info *be,
14 struct xenvif_queue *queue);
e9ce7cb6
WL
15static void connect(struct backend_info *be);
16static int read_xenbus_vif_flags(struct backend_info *be);
2dd34339 17static int backend_create_xenvif(struct backend_info *be);
f942dc25 18static void unregister_hotplug_status_watch(struct backend_info *be);
edafc132 19static void xen_unregister_watchers(struct xenvif *vif);
dc62ccac
DV
20static void set_backend_state(struct backend_info *be,
21 enum xenbus_state state);
f942dc25 22
f51de243
ZK
23#ifdef CONFIG_DEBUG_FS
24struct dentry *xen_netback_dbg_root = NULL;
25
26static 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;
f48da8b1 31 struct netdev_queue *dev_queue;
f51de243
ZK
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
f48da8b1
DV
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
f51de243
ZK
99 return 0;
100}
101
102#define XENVIF_KICK_STR "kick"
5c807005 103#define BUFFER_SIZE 32
f51de243
ZK
104
105static ssize_t
106xenvif_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;
5c807005 112 char write[BUFFER_SIZE];
f51de243
ZK
113
114 /* don't allow partial writes and check the length */
115 if (*ppos != 0)
116 return 0;
5c807005 117 if (count >= sizeof(write))
f51de243
ZK
118 return -ENOSPC;
119
120 len = simple_write_to_buffer(write,
5c807005 121 sizeof(write) - 1,
f51de243
ZK
122 ppos,
123 buf,
124 count);
125 if (len < 0)
126 return len;
127
5c807005
WL
128 write[len] = '\0';
129
f51de243
ZK
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
a9339b8e 140static int xenvif_io_ring_open(struct inode *inode, struct file *filp)
f51de243
ZK
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
152static const struct file_operations xenvif_dbg_io_ring_ops_fops = {
153 .owner = THIS_MODULE,
a9339b8e 154 .open = xenvif_io_ring_open,
f51de243
ZK
155 .read = seq_read,
156 .llseek = seq_lseek,
157 .release = single_release,
158 .write = xenvif_write_io_ring,
159};
160
5061e3f4 161static int xenvif_ctrl_show(struct seq_file *m, void *v)
a9339b8e
PD
162{
163 struct xenvif *vif = m->private;
164
165 xenvif_dump_hash_info(vif, m);
166
167 return 0;
168}
5061e3f4 169DEFINE_SHOW_ATTRIBUTE(xenvif_ctrl);
a9339b8e 170
628fa76b 171static void xenvif_debugfs_addif(struct xenvif *vif)
f51de243 172{
f51de243
ZK
173 int i;
174
f51de243
ZK
175 vif->xenvif_dbg_root = debugfs_create_dir(vif->dev->name,
176 xen_netback_dbg_root);
6f20a697
GKH
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);
f51de243
ZK
189}
190
191static void xenvif_debugfs_delif(struct xenvif *vif)
192{
0527097c 193 debugfs_remove_recursive(vif->xenvif_dbg_root);
f51de243
ZK
194 vif->xenvif_dbg_root = NULL;
195}
196#endif /* CONFIG_DEBUG_FS */
197
f942dc25
IC
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 */
203static int netback_uevent(struct xenbus_device *xdev,
204 struct kobj_uevent_env *env)
205{
206 struct backend_info *be = dev_get_drvdata(&xdev->dev);
f942dc25 207
31a41898
IC
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)
f942dc25
IC
215 return 0;
216
217 return add_uevent_var(env, "vif=%s", be->vif->dev->name);
218}
219
220
2dd34339 221static int backend_create_xenvif(struct backend_info *be)
f942dc25
IC
222{
223 int err;
224 long handle;
225 struct xenbus_device *dev = be->dev;
f15650b7 226 struct xenvif *vif;
f942dc25
IC
227
228 if (be->vif != NULL)
2dd34339 229 return 0;
f942dc25
IC
230
231 err = xenbus_scanf(XBT_NIL, dev->nodename, "handle", "%li", &handle);
232 if (err != 1) {
233 xenbus_dev_fatal(dev, err, "reading handle");
2dd34339 234 return (err < 0) ? err : -EINVAL;
f942dc25
IC
235 }
236
f15650b7
JB
237 vif = xenvif_alloc(&dev->dev, dev->otherend_id, handle);
238 if (IS_ERR(vif)) {
239 err = PTR_ERR(vif);
f942dc25 240 xenbus_dev_fatal(dev, err, "creating interface");
2dd34339 241 return err;
f942dc25 242 }
f15650b7 243 be->vif = vif;
6dc400af 244 vif->be = be;
f942dc25
IC
245
246 kobject_uevent(&dev->dev.kobj, KOBJ_ONLINE);
2dd34339 247 return 0;
f942dc25
IC
248}
249
ea732dff 250static void backend_disconnect(struct backend_info *be)
f942dc25 251{
d67ce7da
PD
252 struct xenvif *vif = be->vif;
253
254 if (vif) {
b17075d5 255 unsigned int num_queues = vif->num_queues;
9a6cdf52
ID
256 unsigned int queue_index;
257
d67ce7da 258 xen_unregister_watchers(vif);
f51de243 259#ifdef CONFIG_DEBUG_FS
d67ce7da 260 xenvif_debugfs_delif(vif);
f51de243 261#endif /* CONFIG_DEBUG_FS */
d67ce7da 262 xenvif_disconnect_data(vif);
d67ce7da 263
b17075d5
ID
264 /* At this point some of the handlers may still be active
265 * so we need to have additional synchronization here.
266 */
d67ce7da 267 vif->num_queues = 0;
b17075d5 268 synchronize_net();
d67ce7da 269
b17075d5
ID
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;
a254d8f9 275
d67ce7da 276 xenvif_disconnect_ctrl(vif);
f51de243 277 }
279f438e
PD
278}
279
ea732dff 280static void backend_connect(struct backend_info *be)
279f438e 281{
ea732dff
PD
282 if (be->vif)
283 connect(be);
284}
279f438e 285
ea732dff
PD
286static 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 *
cce94483 303 * The backend state starts in Initialising and the following transitions are
ea732dff
PD
304 * allowed.
305 *
cce94483
FM
306 * Initialising -> InitWait -> Connected
307 * \
308 * \ ^ \ |
309 * \ | \ |
310 * \ | \ |
311 * \ | \ |
312 * \ | \ |
313 * \ | \ |
314 * V | V V
ea732dff 315 *
cce94483 316 * Closed <-> Closing
ea732dff
PD
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 */
321static void set_backend_state(struct backend_info *be,
322 enum xenbus_state state)
323{
324 while (be->state != state) {
325 switch (be->state) {
cce94483
FM
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;
ea732dff
PD
340 case XenbusStateClosed:
341 switch (state) {
342 case XenbusStateInitWait:
343 case XenbusStateConnected:
ea732dff
PD
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 }
f942dc25
IC
393 }
394}
395
396/**
397 * Callback received when the frontend's state changes.
398 */
399static 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
ea732dff 404 pr_debug("%s -> %s\n", dev->otherend, xenbus_strstate(frontend_state));
f942dc25
IC
405
406 be->frontend_state = frontend_state;
407
408 switch (frontend_state) {
409 case XenbusStateInitialising:
ea732dff 410 set_backend_state(be, XenbusStateInitWait);
f942dc25
IC
411 break;
412
413 case XenbusStateInitialised:
414 break;
415
416 case XenbusStateConnected:
ea732dff 417 set_backend_state(be, XenbusStateConnected);
f942dc25
IC
418 break;
419
420 case XenbusStateClosing:
ea732dff 421 set_backend_state(be, XenbusStateClosing);
f942dc25
IC
422 break;
423
424 case XenbusStateClosed:
ea732dff 425 set_backend_state(be, XenbusStateClosed);
f942dc25
IC
426 if (xenbus_dev_is_online(dev))
427 break;
7499a288 428 /* fall through - if not online */
f942dc25 429 case XenbusStateUnknown:
ea732dff 430 set_backend_state(be, XenbusStateClosed);
f942dc25
IC
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
442static 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
478static 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
edafc132 500static void xen_net_rate_changed(struct xenbus_watch *watch,
5584ea25 501 const char *path, const char *token)
edafc132
PI
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
22fae97d
PD
522static int xen_register_credit_watch(struct xenbus_device *dev,
523 struct xenvif *vif)
edafc132
PI
524{
525 int err = 0;
526 char *node;
527 unsigned maxlen = strlen(dev->nodename) + sizeof("/rate");
528
12b322ac
PI
529 if (vif->credit_watch.node)
530 return -EADDRINUSE;
531
edafc132
PI
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
22fae97d 548static void xen_unregister_credit_watch(struct xenvif *vif)
edafc132
PI
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
22fae97d 557static void xen_mcast_ctrl_changed(struct xenbus_watch *watch,
5584ea25 558 const char *path, const char *token)
22fae97d
PD
559{
560 struct xenvif *vif = container_of(watch, struct xenvif,
561 mcast_ctrl_watch);
562 struct xenbus_device *dev = xenvif_to_xenbus_device(vif);
22fae97d 563
f95842e7
JG
564 vif->multicast_control = !!xenbus_read_unsigned(dev->otherend,
565 "request-multicast-control", 0);
22fae97d
PD
566}
567
568static 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
601static 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
610static 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
617static void xen_unregister_watchers(struct xenvif *vif)
618{
619 xen_unregister_mcast_ctrl_watch(vif);
620 xen_unregister_credit_watch(vif);
621}
622
f942dc25
IC
623static 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
632static void hotplug_status_changed(struct xenbus_watch *watch,
5584ea25
JG
633 const char *path,
634 const char *token)
f942dc25
IC
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)) {
ea732dff
PD
646 /* Complete any pending state change */
647 xenbus_switch_state(be->dev, be->state);
648
f942dc25
IC
649 /* Not interested in this watch anymore. */
650 unregister_hotplug_status_watch(be);
1f256578 651 xenbus_rm(XBT_NIL, be->dev->nodename, "hotplug-status");
f942dc25
IC
652 }
653 kfree(str);
654}
655
4e15ee2c
PD
656static 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
6c27f99d
JB
665 err = xenbus_scanf(XBT_NIL, dev->otherend,
666 "ctrl-ring-ref", "%u", &val);
667 if (err < 0)
4e15ee2c
PD
668 goto done; /* The frontend does not have a control ring */
669
670 ring_ref = val;
671
6c27f99d
JB
672 err = xenbus_scanf(XBT_NIL, dev->otherend,
673 "event-channel-ctrl", "%u", &val);
674 if (err < 0) {
4e15ee2c
PD
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
691done:
692 return 0;
693
694fail:
695 return err;
696}
697
f942dc25
IC
698static void connect(struct backend_info *be)
699{
700 int err;
701 struct xenbus_device *dev = be->dev;
e9ce7cb6
WL
702 unsigned long credit_bytes, credit_usec;
703 unsigned int queue_index;
8d3d53b3 704 unsigned int requested_num_queues;
e9ce7cb6 705 struct xenvif_queue *queue;
f942dc25 706
8d3d53b3
AB
707 /* Check whether the frontend requested multiple queues
708 * and read the number requested.
709 */
f95842e7
JG
710 requested_num_queues = xenbus_read_unsigned(dev->otherend,
711 "multi-queue-num-queues", 1);
712 if (requested_num_queues > xenvif_max_queues) {
8d3d53b3 713 /* buggy or malicious guest */
0f06ac3b 714 xenbus_dev_fatal(dev, -EINVAL,
8d3d53b3
AB
715 "guest requested %u queues, exceeding the maximum of %u.",
716 requested_num_queues, xenvif_max_queues);
717 return;
718 }
719
f942dc25
IC
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
e9ce7cb6 726 xen_net_read_rate(dev, &credit_bytes, &credit_usec);
12b322ac 727 xen_unregister_watchers(be->vif);
edafc132 728 xen_register_watchers(dev, be->vif);
e9ce7cb6
WL
729 read_xenbus_vif_flags(be);
730
4e15ee2c
PD
731 err = connect_ctrl_ring(be);
732 if (err) {
733 xenbus_dev_fatal(dev, err, "connecting control ring");
734 return;
735 }
736
8d3d53b3 737 /* Use the number of queues requested by the frontend */
fad953ce
KC
738 be->vif->queues = vzalloc(array_size(requested_num_queues,
739 sizeof(struct xenvif_queue)));
833b8f18
IY
740 if (!be->vif->queues) {
741 xenbus_dev_fatal(dev, -ENOMEM,
742 "allocating queues");
743 return;
744 }
745
f7b50c4e 746 be->vif->num_queues = requested_num_queues;
ecf08d2d 747 be->vif->stalled_queues = requested_num_queues;
e9ce7cb6
WL
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);
8d3d53b3
AB
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 */
f7b50c4e 764 be->vif->num_queues = queue_index;
e9ce7cb6 765 goto err;
8d3d53b3 766 }
e9ce7cb6 767
ce0e5c52 768 queue->credit_bytes = credit_bytes;
e9ce7cb6 769 queue->remaining_credit = credit_bytes;
07ff890d 770 queue->credit_usec = credit_usec;
e9ce7cb6 771
4e15ee2c 772 err = connect_data_rings(be, queue);
8d3d53b3 773 if (err) {
4e15ee2c
PD
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.
8d3d53b3
AB
778 */
779 xenvif_deinit_queue(queue);
f7b50c4e 780 be->vif->num_queues = queue_index;
e9ce7cb6 781 goto err;
8d3d53b3 782 }
628fa76b
WL
783 }
784
f51de243 785#ifdef CONFIG_DEBUG_FS
628fa76b 786 xenvif_debugfs_addif(be->vif);
f51de243 787#endif /* CONFIG_DEBUG_FS */
e9ce7cb6 788
f7b50c4e
WL
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
e9ce7cb6 797 xenvif_carrier_on(be->vif);
f942dc25
IC
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");
ea732dff 803 if (!err)
f942dc25 804 be->have_hotplug_status_watch = 1;
f942dc25 805
e9ce7cb6
WL
806 netif_tx_wake_all_queues(be->vif->dev);
807
808 return;
809
810err:
f7b50c4e 811 if (be->vif->num_queues > 0)
4e15ee2c 812 xenvif_disconnect_data(be->vif); /* Clean up existing queues */
9a6cdf52
ID
813 for (queue_index = 0; queue_index < be->vif->num_queues; ++queue_index)
814 xenvif_deinit_queue(&be->vif->queues[queue_index]);
e9ce7cb6
WL
815 vfree(be->vif->queues);
816 be->vif->queues = NULL;
f7b50c4e 817 be->vif->num_queues = 0;
4e15ee2c 818 xenvif_disconnect_ctrl(be->vif);
e9ce7cb6 819 return;
f942dc25
IC
820}
821
822
4e15ee2c
PD
823static int connect_data_rings(struct backend_info *be,
824 struct xenvif_queue *queue)
f942dc25 825{
f942dc25 826 struct xenbus_device *dev = be->dev;
f7b50c4e 827 unsigned int num_queues = queue->vif->num_queues;
f942dc25 828 unsigned long tx_ring_ref, rx_ring_ref;
e9ce7cb6 829 unsigned int tx_evtchn, rx_evtchn;
f942dc25 830 int err;
8d3d53b3
AB
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 }
f942dc25 860
8d3d53b3 861 err = xenbus_gather(XBT_NIL, xspath,
f942dc25 862 "tx-ring-ref", "%lu", &tx_ring_ref,
e1f00a69 863 "rx-ring-ref", "%lu", &rx_ring_ref, NULL);
f942dc25
IC
864 if (err) {
865 xenbus_dev_fatal(dev, err,
e1f00a69 866 "reading %s/ring-ref",
8d3d53b3
AB
867 xspath);
868 goto err;
f942dc25
IC
869 }
870
e1f00a69 871 /* Try split event channels first, then single event channel. */
8d3d53b3 872 err = xenbus_gather(XBT_NIL, xspath,
e1f00a69
WL
873 "event-channel-tx", "%u", &tx_evtchn,
874 "event-channel-rx", "%u", &rx_evtchn, NULL);
875 if (err < 0) {
8d3d53b3 876 err = xenbus_scanf(XBT_NIL, xspath,
e1f00a69
WL
877 "event-channel", "%u", &tx_evtchn);
878 if (err < 0) {
879 xenbus_dev_fatal(dev, err,
880 "reading %s/event-channel(-tx/rx)",
8d3d53b3
AB
881 xspath);
882 goto err;
e1f00a69
WL
883 }
884 rx_evtchn = tx_evtchn;
885 }
886
e9ce7cb6 887 /* Map the shared frame, irq etc. */
4e15ee2c
PD
888 err = xenvif_connect_data(queue, tx_ring_ref, rx_ring_ref,
889 tx_evtchn, rx_evtchn);
e9ce7cb6
WL
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);
8d3d53b3 895 goto err;
e9ce7cb6
WL
896 }
897
8d3d53b3
AB
898 err = 0;
899err: /* Regular return falls through with err == 0 */
900 kfree(xspath);
901 return err;
e9ce7cb6
WL
902}
903
904static 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;
f95842e7 909 int err;
e9ce7cb6 910
f942dc25
IC
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
f95842e7 925 if (!xenbus_read_unsigned(dev->otherend, "feature-rx-notify", 0)) {
26c0e102
DV
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;
f942dc25
IC
932 }
933
f95842e7 934 vif->can_sg = !!xenbus_read_unsigned(dev->otherend, "feature-sg", 0);
f942dc25 935
82cada22 936 vif->gso_mask = 0;
82cada22 937
f95842e7 938 if (xenbus_read_unsigned(dev->otherend, "feature-gso-tcpv4", 0))
82cada22 939 vif->gso_mask |= GSO_BIT(TCPV4);
f942dc25 940
f95842e7 941 if (xenbus_read_unsigned(dev->otherend, "feature-gso-tcpv6", 0))
82cada22
PD
942 vif->gso_mask |= GSO_BIT(TCPV6);
943
f95842e7
JG
944 vif->ip_csum = !xenbus_read_unsigned(dev->otherend,
945 "feature-no-csum-offload", 0);
146c8a77 946
f95842e7
JG
947 vif->ipv6_csum = !!xenbus_read_unsigned(dev->otherend,
948 "feature-ipv6-csum-offload", 0);
f942dc25 949
f942dc25
IC
950 return 0;
951}
952
92fbeb43
PD
953static int netback_remove(struct xenbus_device *dev)
954{
955 struct backend_info *be = dev_get_drvdata(&dev->dev);
956
92fbeb43
PD
957 unregister_hotplug_status_watch(be);
958 if (be->vif) {
959 kobject_uevent(&dev->dev.kobj, KOBJ_OFFLINE);
9476654b 960 backend_disconnect(be);
92fbeb43
PD
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 */
974static 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
92fbeb43
PD
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
f55c3188
PD
1094 backend_switch_state(be, XenbusStateInitWait);
1095
92fbeb43
PD
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
1112abort_transaction:
1113 xenbus_transaction_end(xbt, 1);
1114 xenbus_dev_fatal(dev, err, "%s", message);
1115fail:
1116 pr_debug("failed\n");
1117 netback_remove(dev);
1118 return err;
1119}
1120
f942dc25
IC
1121static const struct xenbus_device_id netback_ids[] = {
1122 { "vif" },
1123 { "" }
1124};
1125
95afae48
DV
1126static struct xenbus_driver netback_driver = {
1127 .ids = netback_ids,
f942dc25
IC
1128 .probe = netback_probe,
1129 .remove = netback_remove,
1130 .uevent = netback_uevent,
1131 .otherend_changed = frontend_changed,
9476654b 1132 .allow_rebind = true,
95afae48 1133};
f942dc25
IC
1134
1135int xenvif_xenbus_init(void)
1136{
73db144b 1137 return xenbus_register_backend(&netback_driver);
f942dc25 1138}
b103f358
WL
1139
1140void xenvif_xenbus_fini(void)
1141{
1142 return xenbus_unregister_driver(&netback_driver);
1143}