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