]> git.karo-electronics.de Git - karo-tx-linux.git/blob - net/9p/trans_xen.c
xen/9pfs: connect to the backend
[karo-tx-linux.git] / net / 9p / trans_xen.c
1 /*
2  * linux/fs/9p/trans_xen
3  *
4  * Xen transport layer.
5  *
6  * Copyright (C) 2017 by Stefano Stabellini <stefano@aporeto.com>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License version 2
10  * as published by the Free Software Foundation; or, when distributed
11  * separately from the Linux kernel or incorporated into other
12  * software packages, subject to the following license:
13  *
14  * Permission is hereby granted, free of charge, to any person obtaining a copy
15  * of this source file (the "Software"), to deal in the Software without
16  * restriction, including without limitation the rights to use, copy, modify,
17  * merge, publish, distribute, sublicense, and/or sell copies of the Software,
18  * and to permit persons to whom the Software is furnished to do so, subject to
19  * the following conditions:
20  *
21  * The above copyright notice and this permission notice shall be included in
22  * all copies or substantial portions of the Software.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30  * IN THE SOFTWARE.
31  */
32
33 #include <xen/events.h>
34 #include <xen/grant_table.h>
35 #include <xen/xen.h>
36 #include <xen/xenbus.h>
37 #include <xen/interface/io/9pfs.h>
38
39 #include <linux/module.h>
40 #include <linux/spinlock.h>
41 #include <linux/rwlock.h>
42 #include <net/9p/9p.h>
43 #include <net/9p/client.h>
44 #include <net/9p/transport.h>
45
46 #define XEN_9PFS_NUM_RINGS 2
47 #define XEN_9PFS_RING_ORDER 6
48 #define XEN_9PFS_RING_SIZE  XEN_FLEX_RING_SIZE(XEN_9PFS_RING_ORDER)
49
50 struct xen_9pfs_header {
51         uint32_t size;
52         uint8_t id;
53         uint16_t tag;
54
55         /* uint8_t sdata[]; */
56 } __attribute__((packed));
57
58 /* One per ring, more than one per 9pfs share */
59 struct xen_9pfs_dataring {
60         struct xen_9pfs_front_priv *priv;
61
62         struct xen_9pfs_data_intf *intf;
63         grant_ref_t ref;
64         int evtchn;
65         int irq;
66         /* protect a ring from concurrent accesses */
67         spinlock_t lock;
68
69         struct xen_9pfs_data data;
70         wait_queue_head_t wq;
71         struct work_struct work;
72 };
73
74 /* One per 9pfs share */
75 struct xen_9pfs_front_priv {
76         struct list_head list;
77         struct xenbus_device *dev;
78         char *tag;
79         struct p9_client *client;
80
81         int num_rings;
82         struct xen_9pfs_dataring *rings;
83 };
84
85 static LIST_HEAD(xen_9pfs_devs);
86 static DEFINE_RWLOCK(xen_9pfs_lock);
87
88 static int p9_xen_cancel(struct p9_client *client, struct p9_req_t *req)
89 {
90         return 0;
91 }
92
93 static int p9_xen_create(struct p9_client *client, const char *addr, char *args)
94 {
95         return 0;
96 }
97
98 static void p9_xen_close(struct p9_client *client)
99 {
100 }
101
102 static int p9_xen_request(struct p9_client *client, struct p9_req_t *p9_req)
103 {
104         return 0;
105 }
106
107 static void p9_xen_response(struct work_struct *work)
108 {
109 }
110
111 static irqreturn_t xen_9pfs_front_event_handler(int irq, void *r)
112 {
113         struct xen_9pfs_dataring *ring = r;
114
115         if (!ring || !ring->priv->client) {
116                 /* ignore spurious interrupt */
117                 return IRQ_HANDLED;
118         }
119
120         wake_up_interruptible(&ring->wq);
121         schedule_work(&ring->work);
122
123         return IRQ_HANDLED;
124 }
125
126 static struct p9_trans_module p9_xen_trans = {
127         .name = "xen",
128         .maxsize = 1 << (XEN_9PFS_RING_ORDER + XEN_PAGE_SHIFT),
129         .def = 1,
130         .create = p9_xen_create,
131         .close = p9_xen_close,
132         .request = p9_xen_request,
133         .cancel = p9_xen_cancel,
134         .owner = THIS_MODULE,
135 };
136
137 static const struct xenbus_device_id xen_9pfs_front_ids[] = {
138         { "9pfs" },
139         { "" }
140 };
141
142 static void xen_9pfs_front_free(struct xen_9pfs_front_priv *priv)
143 {
144         int i, j;
145
146         write_lock(&xen_9pfs_lock);
147         list_del(&priv->list);
148         write_unlock(&xen_9pfs_lock);
149
150         for (i = 0; i < priv->num_rings; i++) {
151                 if (!priv->rings[i].intf)
152                         break;
153                 if (priv->rings[i].irq > 0)
154                         unbind_from_irqhandler(priv->rings[i].irq, priv->dev);
155                 if (priv->rings[i].data.in) {
156                         for (j = 0; j < (1 << XEN_9PFS_RING_ORDER); j++) {
157                                 grant_ref_t ref;
158
159                                 ref = priv->rings[i].intf->ref[j];
160                                 gnttab_end_foreign_access(ref, 0, 0);
161                         }
162                         free_pages((unsigned long)priv->rings[i].data.in,
163                                    XEN_9PFS_RING_ORDER -
164                                    (PAGE_SHIFT - XEN_PAGE_SHIFT));
165                 }
166                 gnttab_end_foreign_access(priv->rings[i].ref, 0, 0);
167                 free_page((unsigned long)priv->rings[i].intf);
168         }
169         kfree(priv->rings);
170         kfree(priv->tag);
171         kfree(priv);
172 }
173
174 static int xen_9pfs_front_remove(struct xenbus_device *dev)
175 {
176         struct xen_9pfs_front_priv *priv = dev_get_drvdata(&dev->dev);
177
178         dev_set_drvdata(&dev->dev, NULL);
179         xen_9pfs_front_free(priv);
180         return 0;
181 }
182
183 static int xen_9pfs_front_alloc_dataring(struct xenbus_device *dev,
184                                          struct xen_9pfs_dataring *ring)
185 {
186         int i = 0;
187         int ret = -ENOMEM;
188         void *bytes = NULL;
189
190         init_waitqueue_head(&ring->wq);
191         spin_lock_init(&ring->lock);
192         INIT_WORK(&ring->work, p9_xen_response);
193
194         ring->intf = (struct xen_9pfs_data_intf *)get_zeroed_page(GFP_KERNEL);
195         if (!ring->intf)
196                 return ret;
197         ret = gnttab_grant_foreign_access(dev->otherend_id,
198                                           virt_to_gfn(ring->intf), 0);
199         if (ret < 0)
200                 goto out;
201         ring->ref = ret;
202         bytes = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
203                         XEN_9PFS_RING_ORDER - (PAGE_SHIFT - XEN_PAGE_SHIFT));
204         if (!bytes) {
205                 ret = -ENOMEM;
206                 goto out;
207         }
208         for (; i < (1 << XEN_9PFS_RING_ORDER); i++) {
209                 ret = gnttab_grant_foreign_access(
210                                 dev->otherend_id, virt_to_gfn(bytes) + i, 0);
211                 if (ret < 0)
212                         goto out;
213                 ring->intf->ref[i] = ret;
214         }
215         ring->intf->ring_order = XEN_9PFS_RING_ORDER;
216         ring->data.in = bytes;
217         ring->data.out = bytes + XEN_9PFS_RING_SIZE;
218
219         ret = xenbus_alloc_evtchn(dev, &ring->evtchn);
220         if (ret)
221                 goto out;
222         ring->irq = bind_evtchn_to_irqhandler(ring->evtchn,
223                                               xen_9pfs_front_event_handler,
224                                               0, "xen_9pfs-frontend", ring);
225         if (ring->irq >= 0)
226                 return 0;
227
228         xenbus_free_evtchn(dev, ring->evtchn);
229         ret = ring->irq;
230 out:
231         if (bytes) {
232                 for (i--; i >= 0; i--)
233                         gnttab_end_foreign_access(ring->intf->ref[i], 0, 0);
234                 free_pages((unsigned long)bytes,
235                            XEN_9PFS_RING_ORDER -
236                            (PAGE_SHIFT - XEN_PAGE_SHIFT));
237         }
238         gnttab_end_foreign_access(ring->ref, 0, 0);
239         free_page((unsigned long)ring->intf);
240         return ret;
241 }
242
243 static int xen_9pfs_front_probe(struct xenbus_device *dev,
244                                 const struct xenbus_device_id *id)
245 {
246         int ret, i;
247         struct xenbus_transaction xbt;
248         struct xen_9pfs_front_priv *priv = NULL;
249         char *versions;
250         unsigned int max_rings, max_ring_order, len;
251
252         versions = xenbus_read(XBT_NIL, dev->otherend, "versions", &len);
253         if (!len)
254                 return -EINVAL;
255         if (strcmp(versions, "1")) {
256                 kfree(versions);
257                 return -EINVAL;
258         }
259         kfree(versions);
260         max_rings = xenbus_read_unsigned(dev->otherend, "max-rings", 0);
261         if (max_rings < XEN_9PFS_NUM_RINGS)
262                 return -EINVAL;
263         max_ring_order = xenbus_read_unsigned(dev->otherend,
264                                               "max-ring-page-order", 0);
265         if (max_ring_order < XEN_9PFS_RING_ORDER)
266                 return -EINVAL;
267
268         priv = kzalloc(sizeof(*priv), GFP_KERNEL);
269         if (!priv)
270                 return -ENOMEM;
271
272         priv->dev = dev;
273         priv->num_rings = XEN_9PFS_NUM_RINGS;
274         priv->rings = kcalloc(priv->num_rings, sizeof(*priv->rings),
275                               GFP_KERNEL);
276         if (!priv->rings) {
277                 kfree(priv);
278                 return -ENOMEM;
279         }
280
281         for (i = 0; i < priv->num_rings; i++) {
282                 priv->rings[i].priv = priv;
283                 ret = xen_9pfs_front_alloc_dataring(dev, &priv->rings[i]);
284                 if (ret < 0)
285                         goto error;
286         }
287
288  again:
289         ret = xenbus_transaction_start(&xbt);
290         if (ret) {
291                 xenbus_dev_fatal(dev, ret, "starting transaction");
292                 goto error;
293         }
294         ret = xenbus_printf(xbt, dev->nodename, "version", "%u", 1);
295         if (ret)
296                 goto error_xenbus;
297         ret = xenbus_printf(xbt, dev->nodename, "num-rings", "%u",
298                             priv->num_rings);
299         if (ret)
300                 goto error_xenbus;
301         for (i = 0; i < priv->num_rings; i++) {
302                 char str[16];
303
304                 BUILD_BUG_ON(XEN_9PFS_NUM_RINGS > 9);
305                 sprintf(str, "ring-ref%u", i);
306                 ret = xenbus_printf(xbt, dev->nodename, str, "%d",
307                                     priv->rings[i].ref);
308                 if (ret)
309                         goto error_xenbus;
310
311                 sprintf(str, "event-channel-%u", i);
312                 ret = xenbus_printf(xbt, dev->nodename, str, "%u",
313                                     priv->rings[i].evtchn);
314                 if (ret)
315                         goto error_xenbus;
316         }
317         priv->tag = xenbus_read(xbt, dev->nodename, "tag", NULL);
318         if (!priv->tag) {
319                 ret = -EINVAL;
320                 goto error_xenbus;
321         }
322         ret = xenbus_transaction_end(xbt, 0);
323         if (ret) {
324                 if (ret == -EAGAIN)
325                         goto again;
326                 xenbus_dev_fatal(dev, ret, "completing transaction");
327                 goto error;
328         }
329
330         write_lock(&xen_9pfs_lock);
331         list_add_tail(&priv->list, &xen_9pfs_devs);
332         write_unlock(&xen_9pfs_lock);
333         dev_set_drvdata(&dev->dev, priv);
334         xenbus_switch_state(dev, XenbusStateInitialised);
335
336         return 0;
337
338  error_xenbus:
339         xenbus_transaction_end(xbt, 1);
340         xenbus_dev_fatal(dev, ret, "writing xenstore");
341  error:
342         dev_set_drvdata(&dev->dev, NULL);
343         xen_9pfs_front_free(priv);
344         return ret;
345 }
346
347 static int xen_9pfs_front_resume(struct xenbus_device *dev)
348 {
349         dev_warn(&dev->dev, "suspsend/resume unsupported\n");
350         return 0;
351 }
352
353 static void xen_9pfs_front_changed(struct xenbus_device *dev,
354                                    enum xenbus_state backend_state)
355 {
356         switch (backend_state) {
357         case XenbusStateReconfiguring:
358         case XenbusStateReconfigured:
359         case XenbusStateInitialising:
360         case XenbusStateInitialised:
361         case XenbusStateUnknown:
362                 break;
363
364         case XenbusStateInitWait:
365                 break;
366
367         case XenbusStateConnected:
368                 xenbus_switch_state(dev, XenbusStateConnected);
369                 break;
370
371         case XenbusStateClosed:
372                 if (dev->state == XenbusStateClosed)
373                         break;
374                 /* Missed the backend's CLOSING state -- fallthrough */
375         case XenbusStateClosing:
376                 xenbus_frontend_closed(dev);
377                 break;
378         }
379 }
380
381 static struct xenbus_driver xen_9pfs_front_driver = {
382         .ids = xen_9pfs_front_ids,
383         .probe = xen_9pfs_front_probe,
384         .remove = xen_9pfs_front_remove,
385         .resume = xen_9pfs_front_resume,
386         .otherend_changed = xen_9pfs_front_changed,
387 };
388
389 int p9_trans_xen_init(void)
390 {
391         if (!xen_domain())
392                 return -ENODEV;
393
394         pr_info("Initialising Xen transport for 9pfs\n");
395
396         v9fs_register_trans(&p9_xen_trans);
397         return xenbus_register_frontend(&xen_9pfs_front_driver);
398 }
399 module_init(p9_trans_xen_init);
400
401 void p9_trans_xen_exit(void)
402 {
403         v9fs_unregister_trans(&p9_xen_trans);
404         return xenbus_unregister_driver(&xen_9pfs_front_driver);
405 }
406 module_exit(p9_trans_xen_exit);