]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/net/xen-netback/xenbus.c
Merge tag 'hix5hd2-dt-for-3.19' of git://github.com/hisilicon/linux-hisi into next/dt
[karo-tx-linux.git] / drivers / net / xen-netback / xenbus.c
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
18  * along with this program; if not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "common.h"
22 #include <linux/vmalloc.h>
23 #include <linux/rtnetlink.h>
24
25 struct backend_info {
26         struct xenbus_device *dev;
27         struct xenvif *vif;
28
29         /* This is the state that will be reflected in xenstore when any
30          * active hotplug script completes.
31          */
32         enum xenbus_state state;
33
34         enum xenbus_state frontend_state;
35         struct xenbus_watch hotplug_status_watch;
36         u8 have_hotplug_status_watch:1;
37 };
38
39 static int connect_rings(struct backend_info *be, struct xenvif_queue *queue);
40 static void connect(struct backend_info *be);
41 static int read_xenbus_vif_flags(struct backend_info *be);
42 static void backend_create_xenvif(struct backend_info *be);
43 static void unregister_hotplug_status_watch(struct backend_info *be);
44 static void set_backend_state(struct backend_info *be,
45                               enum xenbus_state state);
46
47 #ifdef CONFIG_DEBUG_FS
48 struct dentry *xen_netback_dbg_root = NULL;
49
50 static int xenvif_read_io_ring(struct seq_file *m, void *v)
51 {
52         struct xenvif_queue *queue = m->private;
53         struct xen_netif_tx_back_ring *tx_ring = &queue->tx;
54         struct xen_netif_rx_back_ring *rx_ring = &queue->rx;
55         struct netdev_queue *dev_queue;
56
57         if (tx_ring->sring) {
58                 struct xen_netif_tx_sring *sring = tx_ring->sring;
59
60                 seq_printf(m, "Queue %d\nTX: nr_ents %u\n", queue->id,
61                            tx_ring->nr_ents);
62                 seq_printf(m, "req prod %u (%d) cons %u (%d) event %u (%d)\n",
63                            sring->req_prod,
64                            sring->req_prod - sring->rsp_prod,
65                            tx_ring->req_cons,
66                            tx_ring->req_cons - sring->rsp_prod,
67                            sring->req_event,
68                            sring->req_event - sring->rsp_prod);
69                 seq_printf(m, "rsp prod %u (base) pvt %u (%d) event %u (%d)\n",
70                            sring->rsp_prod,
71                            tx_ring->rsp_prod_pvt,
72                            tx_ring->rsp_prod_pvt - sring->rsp_prod,
73                            sring->rsp_event,
74                            sring->rsp_event - sring->rsp_prod);
75                 seq_printf(m, "pending prod %u pending cons %u nr_pending_reqs %u\n",
76                            queue->pending_prod,
77                            queue->pending_cons,
78                            nr_pending_reqs(queue));
79                 seq_printf(m, "dealloc prod %u dealloc cons %u dealloc_queue %u\n\n",
80                            queue->dealloc_prod,
81                            queue->dealloc_cons,
82                            queue->dealloc_prod - queue->dealloc_cons);
83         }
84
85         if (rx_ring->sring) {
86                 struct xen_netif_rx_sring *sring = rx_ring->sring;
87
88                 seq_printf(m, "RX: nr_ents %u\n", rx_ring->nr_ents);
89                 seq_printf(m, "req prod %u (%d) cons %u (%d) event %u (%d)\n",
90                            sring->req_prod,
91                            sring->req_prod - sring->rsp_prod,
92                            rx_ring->req_cons,
93                            rx_ring->req_cons - sring->rsp_prod,
94                            sring->req_event,
95                            sring->req_event - sring->rsp_prod);
96                 seq_printf(m, "rsp prod %u (base) pvt %u (%d) event %u (%d)\n\n",
97                            sring->rsp_prod,
98                            rx_ring->rsp_prod_pvt,
99                            rx_ring->rsp_prod_pvt - sring->rsp_prod,
100                            sring->rsp_event,
101                            sring->rsp_event - sring->rsp_prod);
102         }
103
104         seq_printf(m, "NAPI state: %lx NAPI weight: %d TX queue len %u\n"
105                    "Credit timer_pending: %d, credit: %lu, usec: %lu\n"
106                    "remaining: %lu, expires: %lu, now: %lu\n",
107                    queue->napi.state, queue->napi.weight,
108                    skb_queue_len(&queue->tx_queue),
109                    timer_pending(&queue->credit_timeout),
110                    queue->credit_bytes,
111                    queue->credit_usec,
112                    queue->remaining_credit,
113                    queue->credit_timeout.expires,
114                    jiffies);
115
116         dev_queue = netdev_get_tx_queue(queue->vif->dev, queue->id);
117
118         seq_printf(m, "\nRx internal queue: len %u max %u pkts %u %s\n",
119                    queue->rx_queue_len, queue->rx_queue_max,
120                    skb_queue_len(&queue->rx_queue),
121                    netif_tx_queue_stopped(dev_queue) ? "stopped" : "running");
122
123         return 0;
124 }
125
126 #define XENVIF_KICK_STR "kick"
127 #define BUFFER_SIZE     32
128
129 static ssize_t
130 xenvif_write_io_ring(struct file *filp, const char __user *buf, size_t count,
131                      loff_t *ppos)
132 {
133         struct xenvif_queue *queue =
134                 ((struct seq_file *)filp->private_data)->private;
135         int len;
136         char write[BUFFER_SIZE];
137
138         /* don't allow partial writes and check the length */
139         if (*ppos != 0)
140                 return 0;
141         if (count >= sizeof(write))
142                 return -ENOSPC;
143
144         len = simple_write_to_buffer(write,
145                                      sizeof(write) - 1,
146                                      ppos,
147                                      buf,
148                                      count);
149         if (len < 0)
150                 return len;
151
152         write[len] = '\0';
153
154         if (!strncmp(write, XENVIF_KICK_STR, sizeof(XENVIF_KICK_STR) - 1))
155                 xenvif_interrupt(0, (void *)queue);
156         else {
157                 pr_warn("Unknown command to io_ring_q%d. Available: kick\n",
158                         queue->id);
159                 count = -EINVAL;
160         }
161         return count;
162 }
163
164 static int xenvif_dump_open(struct inode *inode, struct file *filp)
165 {
166         int ret;
167         void *queue = NULL;
168
169         if (inode->i_private)
170                 queue = inode->i_private;
171         ret = single_open(filp, xenvif_read_io_ring, queue);
172         filp->f_mode |= FMODE_PWRITE;
173         return ret;
174 }
175
176 static const struct file_operations xenvif_dbg_io_ring_ops_fops = {
177         .owner = THIS_MODULE,
178         .open = xenvif_dump_open,
179         .read = seq_read,
180         .llseek = seq_lseek,
181         .release = single_release,
182         .write = xenvif_write_io_ring,
183 };
184
185 static void xenvif_debugfs_addif(struct xenvif *vif)
186 {
187         struct dentry *pfile;
188         int i;
189
190         if (IS_ERR_OR_NULL(xen_netback_dbg_root))
191                 return;
192
193         vif->xenvif_dbg_root = debugfs_create_dir(vif->dev->name,
194                                                   xen_netback_dbg_root);
195         if (!IS_ERR_OR_NULL(vif->xenvif_dbg_root)) {
196                 for (i = 0; i < vif->num_queues; ++i) {
197                         char filename[sizeof("io_ring_q") + 4];
198
199                         snprintf(filename, sizeof(filename), "io_ring_q%d", i);
200                         pfile = debugfs_create_file(filename,
201                                                     S_IRUSR | S_IWUSR,
202                                                     vif->xenvif_dbg_root,
203                                                     &vif->queues[i],
204                                                     &xenvif_dbg_io_ring_ops_fops);
205                         if (IS_ERR_OR_NULL(pfile))
206                                 pr_warn("Creation of io_ring file returned %ld!\n",
207                                         PTR_ERR(pfile));
208                 }
209         } else
210                 netdev_warn(vif->dev,
211                             "Creation of vif debugfs dir returned %ld!\n",
212                             PTR_ERR(vif->xenvif_dbg_root));
213 }
214
215 static void xenvif_debugfs_delif(struct xenvif *vif)
216 {
217         if (IS_ERR_OR_NULL(xen_netback_dbg_root))
218                 return;
219
220         if (!IS_ERR_OR_NULL(vif->xenvif_dbg_root))
221                 debugfs_remove_recursive(vif->xenvif_dbg_root);
222         vif->xenvif_dbg_root = NULL;
223 }
224 #endif /* CONFIG_DEBUG_FS */
225
226 static int netback_remove(struct xenbus_device *dev)
227 {
228         struct backend_info *be = dev_get_drvdata(&dev->dev);
229
230         set_backend_state(be, XenbusStateClosed);
231
232         unregister_hotplug_status_watch(be);
233         if (be->vif) {
234                 kobject_uevent(&dev->dev.kobj, KOBJ_OFFLINE);
235                 xenbus_rm(XBT_NIL, dev->nodename, "hotplug-status");
236                 xenvif_free(be->vif);
237                 be->vif = NULL;
238         }
239         kfree(be);
240         dev_set_drvdata(&dev->dev, NULL);
241         return 0;
242 }
243
244
245 /**
246  * Entry point to this code when a new device is created.  Allocate the basic
247  * structures and switch to InitWait.
248  */
249 static int netback_probe(struct xenbus_device *dev,
250                          const struct xenbus_device_id *id)
251 {
252         const char *message;
253         struct xenbus_transaction xbt;
254         int err;
255         int sg;
256         struct backend_info *be = kzalloc(sizeof(struct backend_info),
257                                           GFP_KERNEL);
258         if (!be) {
259                 xenbus_dev_fatal(dev, -ENOMEM,
260                                  "allocating backend structure");
261                 return -ENOMEM;
262         }
263
264         be->dev = dev;
265         dev_set_drvdata(&dev->dev, be);
266
267         sg = 1;
268
269         do {
270                 err = xenbus_transaction_start(&xbt);
271                 if (err) {
272                         xenbus_dev_fatal(dev, err, "starting transaction");
273                         goto fail;
274                 }
275
276                 err = xenbus_printf(xbt, dev->nodename, "feature-sg", "%d", sg);
277                 if (err) {
278                         message = "writing feature-sg";
279                         goto abort_transaction;
280                 }
281
282                 err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv4",
283                                     "%d", sg);
284                 if (err) {
285                         message = "writing feature-gso-tcpv4";
286                         goto abort_transaction;
287                 }
288
289                 err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv6",
290                                     "%d", sg);
291                 if (err) {
292                         message = "writing feature-gso-tcpv6";
293                         goto abort_transaction;
294                 }
295
296                 /* We support partial checksum setup for IPv6 packets */
297                 err = xenbus_printf(xbt, dev->nodename,
298                                     "feature-ipv6-csum-offload",
299                                     "%d", 1);
300                 if (err) {
301                         message = "writing feature-ipv6-csum-offload";
302                         goto abort_transaction;
303                 }
304
305                 /* We support rx-copy path. */
306                 err = xenbus_printf(xbt, dev->nodename,
307                                     "feature-rx-copy", "%d", 1);
308                 if (err) {
309                         message = "writing feature-rx-copy";
310                         goto abort_transaction;
311                 }
312
313                 /*
314                  * We don't support rx-flip path (except old guests who don't
315                  * grok this feature flag).
316                  */
317                 err = xenbus_printf(xbt, dev->nodename,
318                                     "feature-rx-flip", "%d", 0);
319                 if (err) {
320                         message = "writing feature-rx-flip";
321                         goto abort_transaction;
322                 }
323
324                 err = xenbus_transaction_end(xbt, 0);
325         } while (err == -EAGAIN);
326
327         if (err) {
328                 xenbus_dev_fatal(dev, err, "completing transaction");
329                 goto fail;
330         }
331
332         /*
333          * Split event channels support, this is optional so it is not
334          * put inside the above loop.
335          */
336         err = xenbus_printf(XBT_NIL, dev->nodename,
337                             "feature-split-event-channels",
338                             "%u", separate_tx_rx_irq);
339         if (err)
340                 pr_debug("Error writing feature-split-event-channels\n");
341
342         /* Multi-queue support: This is an optional feature. */
343         err = xenbus_printf(XBT_NIL, dev->nodename,
344                             "multi-queue-max-queues", "%u", xenvif_max_queues);
345         if (err)
346                 pr_debug("Error writing multi-queue-max-queues\n");
347
348         err = xenbus_switch_state(dev, XenbusStateInitWait);
349         if (err)
350                 goto fail;
351
352         be->state = XenbusStateInitWait;
353
354         /* This kicks hotplug scripts, so do it immediately. */
355         backend_create_xenvif(be);
356
357         return 0;
358
359 abort_transaction:
360         xenbus_transaction_end(xbt, 1);
361         xenbus_dev_fatal(dev, err, "%s", message);
362 fail:
363         pr_debug("failed\n");
364         netback_remove(dev);
365         return err;
366 }
367
368
369 /*
370  * Handle the creation of the hotplug script environment.  We add the script
371  * and vif variables to the environment, for the benefit of the vif-* hotplug
372  * scripts.
373  */
374 static int netback_uevent(struct xenbus_device *xdev,
375                           struct kobj_uevent_env *env)
376 {
377         struct backend_info *be = dev_get_drvdata(&xdev->dev);
378         char *val;
379
380         val = xenbus_read(XBT_NIL, xdev->nodename, "script", NULL);
381         if (IS_ERR(val)) {
382                 int err = PTR_ERR(val);
383                 xenbus_dev_fatal(xdev, err, "reading script");
384                 return err;
385         } else {
386                 if (add_uevent_var(env, "script=%s", val)) {
387                         kfree(val);
388                         return -ENOMEM;
389                 }
390                 kfree(val);
391         }
392
393         if (!be || !be->vif)
394                 return 0;
395
396         return add_uevent_var(env, "vif=%s", be->vif->dev->name);
397 }
398
399
400 static void backend_create_xenvif(struct backend_info *be)
401 {
402         int err;
403         long handle;
404         struct xenbus_device *dev = be->dev;
405
406         if (be->vif != NULL)
407                 return;
408
409         err = xenbus_scanf(XBT_NIL, dev->nodename, "handle", "%li", &handle);
410         if (err != 1) {
411                 xenbus_dev_fatal(dev, err, "reading handle");
412                 return;
413         }
414
415         be->vif = xenvif_alloc(&dev->dev, dev->otherend_id, handle);
416         if (IS_ERR(be->vif)) {
417                 err = PTR_ERR(be->vif);
418                 be->vif = NULL;
419                 xenbus_dev_fatal(dev, err, "creating interface");
420                 return;
421         }
422
423         kobject_uevent(&dev->dev.kobj, KOBJ_ONLINE);
424 }
425
426 static void backend_disconnect(struct backend_info *be)
427 {
428         if (be->vif) {
429 #ifdef CONFIG_DEBUG_FS
430                 xenvif_debugfs_delif(be->vif);
431 #endif /* CONFIG_DEBUG_FS */
432                 xenvif_disconnect(be->vif);
433         }
434 }
435
436 static void backend_connect(struct backend_info *be)
437 {
438         if (be->vif)
439                 connect(be);
440 }
441
442 static inline void backend_switch_state(struct backend_info *be,
443                                         enum xenbus_state state)
444 {
445         struct xenbus_device *dev = be->dev;
446
447         pr_debug("%s -> %s\n", dev->nodename, xenbus_strstate(state));
448         be->state = state;
449
450         /* If we are waiting for a hotplug script then defer the
451          * actual xenbus state change.
452          */
453         if (!be->have_hotplug_status_watch)
454                 xenbus_switch_state(dev, state);
455 }
456
457 /* Handle backend state transitions:
458  *
459  * The backend state starts in InitWait and the following transitions are
460  * allowed.
461  *
462  * InitWait -> Connected
463  *
464  *    ^    \         |
465  *    |     \        |
466  *    |      \       |
467  *    |       \      |
468  *    |        \     |
469  *    |         \    |
470  *    |          V   V
471  *
472  *  Closed  <-> Closing
473  *
474  * The state argument specifies the eventual state of the backend and the
475  * function transitions to that state via the shortest path.
476  */
477 static void set_backend_state(struct backend_info *be,
478                               enum xenbus_state state)
479 {
480         while (be->state != state) {
481                 switch (be->state) {
482                 case XenbusStateClosed:
483                         switch (state) {
484                         case XenbusStateInitWait:
485                         case XenbusStateConnected:
486                                 pr_info("%s: prepare for reconnect\n",
487                                         be->dev->nodename);
488                                 backend_switch_state(be, XenbusStateInitWait);
489                                 break;
490                         case XenbusStateClosing:
491                                 backend_switch_state(be, XenbusStateClosing);
492                                 break;
493                         default:
494                                 BUG();
495                         }
496                         break;
497                 case XenbusStateInitWait:
498                         switch (state) {
499                         case XenbusStateConnected:
500                                 backend_connect(be);
501                                 backend_switch_state(be, XenbusStateConnected);
502                                 break;
503                         case XenbusStateClosing:
504                         case XenbusStateClosed:
505                                 backend_switch_state(be, XenbusStateClosing);
506                                 break;
507                         default:
508                                 BUG();
509                         }
510                         break;
511                 case XenbusStateConnected:
512                         switch (state) {
513                         case XenbusStateInitWait:
514                         case XenbusStateClosing:
515                         case XenbusStateClosed:
516                                 backend_disconnect(be);
517                                 backend_switch_state(be, XenbusStateClosing);
518                                 break;
519                         default:
520                                 BUG();
521                         }
522                         break;
523                 case XenbusStateClosing:
524                         switch (state) {
525                         case XenbusStateInitWait:
526                         case XenbusStateConnected:
527                         case XenbusStateClosed:
528                                 backend_switch_state(be, XenbusStateClosed);
529                                 break;
530                         default:
531                                 BUG();
532                         }
533                         break;
534                 default:
535                         BUG();
536                 }
537         }
538 }
539
540 /**
541  * Callback received when the frontend's state changes.
542  */
543 static void frontend_changed(struct xenbus_device *dev,
544                              enum xenbus_state frontend_state)
545 {
546         struct backend_info *be = dev_get_drvdata(&dev->dev);
547
548         pr_debug("%s -> %s\n", dev->otherend, xenbus_strstate(frontend_state));
549
550         be->frontend_state = frontend_state;
551
552         switch (frontend_state) {
553         case XenbusStateInitialising:
554                 set_backend_state(be, XenbusStateInitWait);
555                 break;
556
557         case XenbusStateInitialised:
558                 break;
559
560         case XenbusStateConnected:
561                 set_backend_state(be, XenbusStateConnected);
562                 break;
563
564         case XenbusStateClosing:
565                 set_backend_state(be, XenbusStateClosing);
566                 break;
567
568         case XenbusStateClosed:
569                 set_backend_state(be, XenbusStateClosed);
570                 if (xenbus_dev_is_online(dev))
571                         break;
572                 /* fall through if not online */
573         case XenbusStateUnknown:
574                 set_backend_state(be, XenbusStateClosed);
575                 device_unregister(&dev->dev);
576                 break;
577
578         default:
579                 xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
580                                  frontend_state);
581                 break;
582         }
583 }
584
585
586 static void xen_net_read_rate(struct xenbus_device *dev,
587                               unsigned long *bytes, unsigned long *usec)
588 {
589         char *s, *e;
590         unsigned long b, u;
591         char *ratestr;
592
593         /* Default to unlimited bandwidth. */
594         *bytes = ~0UL;
595         *usec = 0;
596
597         ratestr = xenbus_read(XBT_NIL, dev->nodename, "rate", NULL);
598         if (IS_ERR(ratestr))
599                 return;
600
601         s = ratestr;
602         b = simple_strtoul(s, &e, 10);
603         if ((s == e) || (*e != ','))
604                 goto fail;
605
606         s = e + 1;
607         u = simple_strtoul(s, &e, 10);
608         if ((s == e) || (*e != '\0'))
609                 goto fail;
610
611         *bytes = b;
612         *usec = u;
613
614         kfree(ratestr);
615         return;
616
617  fail:
618         pr_warn("Failed to parse network rate limit. Traffic unlimited.\n");
619         kfree(ratestr);
620 }
621
622 static int xen_net_read_mac(struct xenbus_device *dev, u8 mac[])
623 {
624         char *s, *e, *macstr;
625         int i;
626
627         macstr = s = xenbus_read(XBT_NIL, dev->nodename, "mac", NULL);
628         if (IS_ERR(macstr))
629                 return PTR_ERR(macstr);
630
631         for (i = 0; i < ETH_ALEN; i++) {
632                 mac[i] = simple_strtoul(s, &e, 16);
633                 if ((s == e) || (*e != ((i == ETH_ALEN-1) ? '\0' : ':'))) {
634                         kfree(macstr);
635                         return -ENOENT;
636                 }
637                 s = e+1;
638         }
639
640         kfree(macstr);
641         return 0;
642 }
643
644 static void unregister_hotplug_status_watch(struct backend_info *be)
645 {
646         if (be->have_hotplug_status_watch) {
647                 unregister_xenbus_watch(&be->hotplug_status_watch);
648                 kfree(be->hotplug_status_watch.node);
649         }
650         be->have_hotplug_status_watch = 0;
651 }
652
653 static void hotplug_status_changed(struct xenbus_watch *watch,
654                                    const char **vec,
655                                    unsigned int vec_size)
656 {
657         struct backend_info *be = container_of(watch,
658                                                struct backend_info,
659                                                hotplug_status_watch);
660         char *str;
661         unsigned int len;
662
663         str = xenbus_read(XBT_NIL, be->dev->nodename, "hotplug-status", &len);
664         if (IS_ERR(str))
665                 return;
666         if (len == sizeof("connected")-1 && !memcmp(str, "connected", len)) {
667                 /* Complete any pending state change */
668                 xenbus_switch_state(be->dev, be->state);
669
670                 /* Not interested in this watch anymore. */
671                 unregister_hotplug_status_watch(be);
672         }
673         kfree(str);
674 }
675
676 static void connect(struct backend_info *be)
677 {
678         int err;
679         struct xenbus_device *dev = be->dev;
680         unsigned long credit_bytes, credit_usec;
681         unsigned int queue_index;
682         unsigned int requested_num_queues;
683         struct xenvif_queue *queue;
684
685         /* Check whether the frontend requested multiple queues
686          * and read the number requested.
687          */
688         err = xenbus_scanf(XBT_NIL, dev->otherend,
689                            "multi-queue-num-queues",
690                            "%u", &requested_num_queues);
691         if (err < 0) {
692                 requested_num_queues = 1; /* Fall back to single queue */
693         } else if (requested_num_queues > xenvif_max_queues) {
694                 /* buggy or malicious guest */
695                 xenbus_dev_fatal(dev, err,
696                                  "guest requested %u queues, exceeding the maximum of %u.",
697                                  requested_num_queues, xenvif_max_queues);
698                 return;
699         }
700
701         err = xen_net_read_mac(dev, be->vif->fe_dev_addr);
702         if (err) {
703                 xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename);
704                 return;
705         }
706
707         xen_net_read_rate(dev, &credit_bytes, &credit_usec);
708         read_xenbus_vif_flags(be);
709
710         /* Use the number of queues requested by the frontend */
711         be->vif->queues = vzalloc(requested_num_queues *
712                                   sizeof(struct xenvif_queue));
713         be->vif->num_queues = requested_num_queues;
714         be->vif->stalled_queues = requested_num_queues;
715
716         for (queue_index = 0; queue_index < requested_num_queues; ++queue_index) {
717                 queue = &be->vif->queues[queue_index];
718                 queue->vif = be->vif;
719                 queue->id = queue_index;
720                 snprintf(queue->name, sizeof(queue->name), "%s-q%u",
721                                 be->vif->dev->name, queue->id);
722
723                 err = xenvif_init_queue(queue);
724                 if (err) {
725                         /* xenvif_init_queue() cleans up after itself on
726                          * failure, but we need to clean up any previously
727                          * initialised queues. Set num_queues to i so that
728                          * earlier queues can be destroyed using the regular
729                          * disconnect logic.
730                          */
731                         be->vif->num_queues = queue_index;
732                         goto err;
733                 }
734
735                 queue->remaining_credit = credit_bytes;
736
737                 err = connect_rings(be, queue);
738                 if (err) {
739                         /* connect_rings() cleans up after itself on failure,
740                          * but we need to clean up after xenvif_init_queue() here,
741                          * and also clean up any previously initialised queues.
742                          */
743                         xenvif_deinit_queue(queue);
744                         be->vif->num_queues = queue_index;
745                         goto err;
746                 }
747         }
748
749 #ifdef CONFIG_DEBUG_FS
750         xenvif_debugfs_addif(be->vif);
751 #endif /* CONFIG_DEBUG_FS */
752
753         /* Initialisation completed, tell core driver the number of
754          * active queues.
755          */
756         rtnl_lock();
757         netif_set_real_num_tx_queues(be->vif->dev, requested_num_queues);
758         netif_set_real_num_rx_queues(be->vif->dev, requested_num_queues);
759         rtnl_unlock();
760
761         xenvif_carrier_on(be->vif);
762
763         unregister_hotplug_status_watch(be);
764         err = xenbus_watch_pathfmt(dev, &be->hotplug_status_watch,
765                                    hotplug_status_changed,
766                                    "%s/%s", dev->nodename, "hotplug-status");
767         if (!err)
768                 be->have_hotplug_status_watch = 1;
769
770         netif_tx_wake_all_queues(be->vif->dev);
771
772         return;
773
774 err:
775         if (be->vif->num_queues > 0)
776                 xenvif_disconnect(be->vif); /* Clean up existing queues */
777         vfree(be->vif->queues);
778         be->vif->queues = NULL;
779         be->vif->num_queues = 0;
780         return;
781 }
782
783
784 static int connect_rings(struct backend_info *be, struct xenvif_queue *queue)
785 {
786         struct xenbus_device *dev = be->dev;
787         unsigned int num_queues = queue->vif->num_queues;
788         unsigned long tx_ring_ref, rx_ring_ref;
789         unsigned int tx_evtchn, rx_evtchn;
790         int err;
791         char *xspath;
792         size_t xspathsize;
793         const size_t xenstore_path_ext_size = 11; /* sufficient for "/queue-NNN" */
794
795         /* If the frontend requested 1 queue, or we have fallen back
796          * to single queue due to lack of frontend support for multi-
797          * queue, expect the remaining XenStore keys in the toplevel
798          * directory. Otherwise, expect them in a subdirectory called
799          * queue-N.
800          */
801         if (num_queues == 1) {
802                 xspath = kzalloc(strlen(dev->otherend) + 1, GFP_KERNEL);
803                 if (!xspath) {
804                         xenbus_dev_fatal(dev, -ENOMEM,
805                                          "reading ring references");
806                         return -ENOMEM;
807                 }
808                 strcpy(xspath, dev->otherend);
809         } else {
810                 xspathsize = strlen(dev->otherend) + xenstore_path_ext_size;
811                 xspath = kzalloc(xspathsize, GFP_KERNEL);
812                 if (!xspath) {
813                         xenbus_dev_fatal(dev, -ENOMEM,
814                                          "reading ring references");
815                         return -ENOMEM;
816                 }
817                 snprintf(xspath, xspathsize, "%s/queue-%u", dev->otherend,
818                          queue->id);
819         }
820
821         err = xenbus_gather(XBT_NIL, xspath,
822                             "tx-ring-ref", "%lu", &tx_ring_ref,
823                             "rx-ring-ref", "%lu", &rx_ring_ref, NULL);
824         if (err) {
825                 xenbus_dev_fatal(dev, err,
826                                  "reading %s/ring-ref",
827                                  xspath);
828                 goto err;
829         }
830
831         /* Try split event channels first, then single event channel. */
832         err = xenbus_gather(XBT_NIL, xspath,
833                             "event-channel-tx", "%u", &tx_evtchn,
834                             "event-channel-rx", "%u", &rx_evtchn, NULL);
835         if (err < 0) {
836                 err = xenbus_scanf(XBT_NIL, xspath,
837                                    "event-channel", "%u", &tx_evtchn);
838                 if (err < 0) {
839                         xenbus_dev_fatal(dev, err,
840                                          "reading %s/event-channel(-tx/rx)",
841                                          xspath);
842                         goto err;
843                 }
844                 rx_evtchn = tx_evtchn;
845         }
846
847         /* Map the shared frame, irq etc. */
848         err = xenvif_connect(queue, tx_ring_ref, rx_ring_ref,
849                              tx_evtchn, rx_evtchn);
850         if (err) {
851                 xenbus_dev_fatal(dev, err,
852                                  "mapping shared-frames %lu/%lu port tx %u rx %u",
853                                  tx_ring_ref, rx_ring_ref,
854                                  tx_evtchn, rx_evtchn);
855                 goto err;
856         }
857
858         err = 0;
859 err: /* Regular return falls through with err == 0 */
860         kfree(xspath);
861         return err;
862 }
863
864 static int read_xenbus_vif_flags(struct backend_info *be)
865 {
866         struct xenvif *vif = be->vif;
867         struct xenbus_device *dev = be->dev;
868         unsigned int rx_copy;
869         int err, val;
870
871         err = xenbus_scanf(XBT_NIL, dev->otherend, "request-rx-copy", "%u",
872                            &rx_copy);
873         if (err == -ENOENT) {
874                 err = 0;
875                 rx_copy = 0;
876         }
877         if (err < 0) {
878                 xenbus_dev_fatal(dev, err, "reading %s/request-rx-copy",
879                                  dev->otherend);
880                 return err;
881         }
882         if (!rx_copy)
883                 return -EOPNOTSUPP;
884
885         if (xenbus_scanf(XBT_NIL, dev->otherend,
886                          "feature-rx-notify", "%d", &val) < 0 || val == 0) {
887                 xenbus_dev_fatal(dev, -EINVAL, "feature-rx-notify is mandatory");
888                 return -EINVAL;
889         }
890
891         if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-sg",
892                          "%d", &val) < 0)
893                 val = 0;
894         vif->can_sg = !!val;
895
896         vif->gso_mask = 0;
897         vif->gso_prefix_mask = 0;
898
899         if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-gso-tcpv4",
900                          "%d", &val) < 0)
901                 val = 0;
902         if (val)
903                 vif->gso_mask |= GSO_BIT(TCPV4);
904
905         if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-gso-tcpv4-prefix",
906                          "%d", &val) < 0)
907                 val = 0;
908         if (val)
909                 vif->gso_prefix_mask |= GSO_BIT(TCPV4);
910
911         if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-gso-tcpv6",
912                          "%d", &val) < 0)
913                 val = 0;
914         if (val)
915                 vif->gso_mask |= GSO_BIT(TCPV6);
916
917         if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-gso-tcpv6-prefix",
918                          "%d", &val) < 0)
919                 val = 0;
920         if (val)
921                 vif->gso_prefix_mask |= GSO_BIT(TCPV6);
922
923         if (vif->gso_mask & vif->gso_prefix_mask) {
924                 xenbus_dev_fatal(dev, err,
925                                  "%s: gso and gso prefix flags are not "
926                                  "mutually exclusive",
927                                  dev->otherend);
928                 return -EOPNOTSUPP;
929         }
930
931         if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-no-csum-offload",
932                          "%d", &val) < 0)
933                 val = 0;
934         vif->ip_csum = !val;
935
936         if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-ipv6-csum-offload",
937                          "%d", &val) < 0)
938                 val = 0;
939         vif->ipv6_csum = !!val;
940
941         return 0;
942 }
943
944 static const struct xenbus_device_id netback_ids[] = {
945         { "vif" },
946         { "" }
947 };
948
949 static struct xenbus_driver netback_driver = {
950         .ids = netback_ids,
951         .probe = netback_probe,
952         .remove = netback_remove,
953         .uevent = netback_uevent,
954         .otherend_changed = frontend_changed,
955 };
956
957 int xenvif_xenbus_init(void)
958 {
959         return xenbus_register_backend(&netback_driver);
960 }
961
962 void xenvif_xenbus_fini(void)
963 {
964         return xenbus_unregister_driver(&netback_driver);
965 }