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