28f30f49d5dd27bddc75b38a9d336082a8cb6b50
[muen/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 /* We don't currently allow canceling of requests */
89 static int p9_xen_cancel(struct p9_client *client, struct p9_req_t *req)
90 {
91         return 1;
92 }
93
94 static int p9_xen_create(struct p9_client *client, const char *addr, char *args)
95 {
96         struct xen_9pfs_front_priv *priv;
97
98         read_lock(&xen_9pfs_lock);
99         list_for_each_entry(priv, &xen_9pfs_devs, list) {
100                 if (!strcmp(priv->tag, addr)) {
101                         priv->client = client;
102                         read_unlock(&xen_9pfs_lock);
103                         return 0;
104                 }
105         }
106         read_unlock(&xen_9pfs_lock);
107         return -EINVAL;
108 }
109
110 static void p9_xen_close(struct p9_client *client)
111 {
112         struct xen_9pfs_front_priv *priv;
113
114         read_lock(&xen_9pfs_lock);
115         list_for_each_entry(priv, &xen_9pfs_devs, list) {
116                 if (priv->client == client) {
117                         priv->client = NULL;
118                         read_unlock(&xen_9pfs_lock);
119                         return;
120                 }
121         }
122         read_unlock(&xen_9pfs_lock);
123 }
124
125 static bool p9_xen_write_todo(struct xen_9pfs_dataring *ring, RING_IDX size)
126 {
127         RING_IDX cons, prod;
128
129         cons = ring->intf->out_cons;
130         prod = ring->intf->out_prod;
131         virt_mb();
132
133         return XEN_9PFS_RING_SIZE -
134                 xen_9pfs_queued(prod, cons, XEN_9PFS_RING_SIZE) >= size;
135 }
136
137 static int p9_xen_request(struct p9_client *client, struct p9_req_t *p9_req)
138 {
139         struct xen_9pfs_front_priv *priv = NULL;
140         RING_IDX cons, prod, masked_cons, masked_prod;
141         unsigned long flags;
142         u32 size = p9_req->tc->size;
143         struct xen_9pfs_dataring *ring;
144         int num;
145
146         read_lock(&xen_9pfs_lock);
147         list_for_each_entry(priv, &xen_9pfs_devs, list) {
148                 if (priv->client == client)
149                         break;
150         }
151         read_unlock(&xen_9pfs_lock);
152         if (!priv || priv->client != client)
153                 return -EINVAL;
154
155         num = p9_req->tc->tag % priv->num_rings;
156         ring = &priv->rings[num];
157
158 again:
159         while (wait_event_interruptible(ring->wq,
160                                         p9_xen_write_todo(ring, size)) != 0)
161                 ;
162
163         spin_lock_irqsave(&ring->lock, flags);
164         cons = ring->intf->out_cons;
165         prod = ring->intf->out_prod;
166         virt_mb();
167
168         if (XEN_9PFS_RING_SIZE - xen_9pfs_queued(prod, cons,
169                                                  XEN_9PFS_RING_SIZE) < size) {
170                 spin_unlock_irqrestore(&ring->lock, flags);
171                 goto again;
172         }
173
174         masked_prod = xen_9pfs_mask(prod, XEN_9PFS_RING_SIZE);
175         masked_cons = xen_9pfs_mask(cons, XEN_9PFS_RING_SIZE);
176
177         xen_9pfs_write_packet(ring->data.out, p9_req->tc->sdata, size,
178                               &masked_prod, masked_cons, XEN_9PFS_RING_SIZE);
179
180         p9_req->status = REQ_STATUS_SENT;
181         virt_wmb();                     /* write ring before updating pointer */
182         prod += size;
183         ring->intf->out_prod = prod;
184         spin_unlock_irqrestore(&ring->lock, flags);
185         notify_remote_via_irq(ring->irq);
186
187         return 0;
188 }
189
190 static void p9_xen_response(struct work_struct *work)
191 {
192 }
193
194 static irqreturn_t xen_9pfs_front_event_handler(int irq, void *r)
195 {
196         struct xen_9pfs_dataring *ring = r;
197
198         if (!ring || !ring->priv->client) {
199                 /* ignore spurious interrupt */
200                 return IRQ_HANDLED;
201         }
202
203         wake_up_interruptible(&ring->wq);
204         schedule_work(&ring->work);
205
206         return IRQ_HANDLED;
207 }
208
209 static struct p9_trans_module p9_xen_trans = {
210         .name = "xen",
211         .maxsize = 1 << (XEN_9PFS_RING_ORDER + XEN_PAGE_SHIFT),
212         .def = 1,
213         .create = p9_xen_create,
214         .close = p9_xen_close,
215         .request = p9_xen_request,
216         .cancel = p9_xen_cancel,
217         .owner = THIS_MODULE,
218 };
219
220 static const struct xenbus_device_id xen_9pfs_front_ids[] = {
221         { "9pfs" },
222         { "" }
223 };
224
225 static void xen_9pfs_front_free(struct xen_9pfs_front_priv *priv)
226 {
227         int i, j;
228
229         write_lock(&xen_9pfs_lock);
230         list_del(&priv->list);
231         write_unlock(&xen_9pfs_lock);
232
233         for (i = 0; i < priv->num_rings; i++) {
234                 if (!priv->rings[i].intf)
235                         break;
236                 if (priv->rings[i].irq > 0)
237                         unbind_from_irqhandler(priv->rings[i].irq, priv->dev);
238                 if (priv->rings[i].data.in) {
239                         for (j = 0; j < (1 << XEN_9PFS_RING_ORDER); j++) {
240                                 grant_ref_t ref;
241
242                                 ref = priv->rings[i].intf->ref[j];
243                                 gnttab_end_foreign_access(ref, 0, 0);
244                         }
245                         free_pages((unsigned long)priv->rings[i].data.in,
246                                    XEN_9PFS_RING_ORDER -
247                                    (PAGE_SHIFT - XEN_PAGE_SHIFT));
248                 }
249                 gnttab_end_foreign_access(priv->rings[i].ref, 0, 0);
250                 free_page((unsigned long)priv->rings[i].intf);
251         }
252         kfree(priv->rings);
253         kfree(priv->tag);
254         kfree(priv);
255 }
256
257 static int xen_9pfs_front_remove(struct xenbus_device *dev)
258 {
259         struct xen_9pfs_front_priv *priv = dev_get_drvdata(&dev->dev);
260
261         dev_set_drvdata(&dev->dev, NULL);
262         xen_9pfs_front_free(priv);
263         return 0;
264 }
265
266 static int xen_9pfs_front_alloc_dataring(struct xenbus_device *dev,
267                                          struct xen_9pfs_dataring *ring)
268 {
269         int i = 0;
270         int ret = -ENOMEM;
271         void *bytes = NULL;
272
273         init_waitqueue_head(&ring->wq);
274         spin_lock_init(&ring->lock);
275         INIT_WORK(&ring->work, p9_xen_response);
276
277         ring->intf = (struct xen_9pfs_data_intf *)get_zeroed_page(GFP_KERNEL);
278         if (!ring->intf)
279                 return ret;
280         ret = gnttab_grant_foreign_access(dev->otherend_id,
281                                           virt_to_gfn(ring->intf), 0);
282         if (ret < 0)
283                 goto out;
284         ring->ref = ret;
285         bytes = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
286                         XEN_9PFS_RING_ORDER - (PAGE_SHIFT - XEN_PAGE_SHIFT));
287         if (!bytes) {
288                 ret = -ENOMEM;
289                 goto out;
290         }
291         for (; i < (1 << XEN_9PFS_RING_ORDER); i++) {
292                 ret = gnttab_grant_foreign_access(
293                                 dev->otherend_id, virt_to_gfn(bytes) + i, 0);
294                 if (ret < 0)
295                         goto out;
296                 ring->intf->ref[i] = ret;
297         }
298         ring->intf->ring_order = XEN_9PFS_RING_ORDER;
299         ring->data.in = bytes;
300         ring->data.out = bytes + XEN_9PFS_RING_SIZE;
301
302         ret = xenbus_alloc_evtchn(dev, &ring->evtchn);
303         if (ret)
304                 goto out;
305         ring->irq = bind_evtchn_to_irqhandler(ring->evtchn,
306                                               xen_9pfs_front_event_handler,
307                                               0, "xen_9pfs-frontend", ring);
308         if (ring->irq >= 0)
309                 return 0;
310
311         xenbus_free_evtchn(dev, ring->evtchn);
312         ret = ring->irq;
313 out:
314         if (bytes) {
315                 for (i--; i >= 0; i--)
316                         gnttab_end_foreign_access(ring->intf->ref[i], 0, 0);
317                 free_pages((unsigned long)bytes,
318                            XEN_9PFS_RING_ORDER -
319                            (PAGE_SHIFT - XEN_PAGE_SHIFT));
320         }
321         gnttab_end_foreign_access(ring->ref, 0, 0);
322         free_page((unsigned long)ring->intf);
323         return ret;
324 }
325
326 static int xen_9pfs_front_probe(struct xenbus_device *dev,
327                                 const struct xenbus_device_id *id)
328 {
329         int ret, i;
330         struct xenbus_transaction xbt;
331         struct xen_9pfs_front_priv *priv = NULL;
332         char *versions;
333         unsigned int max_rings, max_ring_order, len;
334
335         versions = xenbus_read(XBT_NIL, dev->otherend, "versions", &len);
336         if (!len)
337                 return -EINVAL;
338         if (strcmp(versions, "1")) {
339                 kfree(versions);
340                 return -EINVAL;
341         }
342         kfree(versions);
343         max_rings = xenbus_read_unsigned(dev->otherend, "max-rings", 0);
344         if (max_rings < XEN_9PFS_NUM_RINGS)
345                 return -EINVAL;
346         max_ring_order = xenbus_read_unsigned(dev->otherend,
347                                               "max-ring-page-order", 0);
348         if (max_ring_order < XEN_9PFS_RING_ORDER)
349                 return -EINVAL;
350
351         priv = kzalloc(sizeof(*priv), GFP_KERNEL);
352         if (!priv)
353                 return -ENOMEM;
354
355         priv->dev = dev;
356         priv->num_rings = XEN_9PFS_NUM_RINGS;
357         priv->rings = kcalloc(priv->num_rings, sizeof(*priv->rings),
358                               GFP_KERNEL);
359         if (!priv->rings) {
360                 kfree(priv);
361                 return -ENOMEM;
362         }
363
364         for (i = 0; i < priv->num_rings; i++) {
365                 priv->rings[i].priv = priv;
366                 ret = xen_9pfs_front_alloc_dataring(dev, &priv->rings[i]);
367                 if (ret < 0)
368                         goto error;
369         }
370
371  again:
372         ret = xenbus_transaction_start(&xbt);
373         if (ret) {
374                 xenbus_dev_fatal(dev, ret, "starting transaction");
375                 goto error;
376         }
377         ret = xenbus_printf(xbt, dev->nodename, "version", "%u", 1);
378         if (ret)
379                 goto error_xenbus;
380         ret = xenbus_printf(xbt, dev->nodename, "num-rings", "%u",
381                             priv->num_rings);
382         if (ret)
383                 goto error_xenbus;
384         for (i = 0; i < priv->num_rings; i++) {
385                 char str[16];
386
387                 BUILD_BUG_ON(XEN_9PFS_NUM_RINGS > 9);
388                 sprintf(str, "ring-ref%u", i);
389                 ret = xenbus_printf(xbt, dev->nodename, str, "%d",
390                                     priv->rings[i].ref);
391                 if (ret)
392                         goto error_xenbus;
393
394                 sprintf(str, "event-channel-%u", i);
395                 ret = xenbus_printf(xbt, dev->nodename, str, "%u",
396                                     priv->rings[i].evtchn);
397                 if (ret)
398                         goto error_xenbus;
399         }
400         priv->tag = xenbus_read(xbt, dev->nodename, "tag", NULL);
401         if (!priv->tag) {
402                 ret = -EINVAL;
403                 goto error_xenbus;
404         }
405         ret = xenbus_transaction_end(xbt, 0);
406         if (ret) {
407                 if (ret == -EAGAIN)
408                         goto again;
409                 xenbus_dev_fatal(dev, ret, "completing transaction");
410                 goto error;
411         }
412
413         write_lock(&xen_9pfs_lock);
414         list_add_tail(&priv->list, &xen_9pfs_devs);
415         write_unlock(&xen_9pfs_lock);
416         dev_set_drvdata(&dev->dev, priv);
417         xenbus_switch_state(dev, XenbusStateInitialised);
418
419         return 0;
420
421  error_xenbus:
422         xenbus_transaction_end(xbt, 1);
423         xenbus_dev_fatal(dev, ret, "writing xenstore");
424  error:
425         dev_set_drvdata(&dev->dev, NULL);
426         xen_9pfs_front_free(priv);
427         return ret;
428 }
429
430 static int xen_9pfs_front_resume(struct xenbus_device *dev)
431 {
432         dev_warn(&dev->dev, "suspsend/resume unsupported\n");
433         return 0;
434 }
435
436 static void xen_9pfs_front_changed(struct xenbus_device *dev,
437                                    enum xenbus_state backend_state)
438 {
439         switch (backend_state) {
440         case XenbusStateReconfiguring:
441         case XenbusStateReconfigured:
442         case XenbusStateInitialising:
443         case XenbusStateInitialised:
444         case XenbusStateUnknown:
445                 break;
446
447         case XenbusStateInitWait:
448                 break;
449
450         case XenbusStateConnected:
451                 xenbus_switch_state(dev, XenbusStateConnected);
452                 break;
453
454         case XenbusStateClosed:
455                 if (dev->state == XenbusStateClosed)
456                         break;
457                 /* Missed the backend's CLOSING state -- fallthrough */
458         case XenbusStateClosing:
459                 xenbus_frontend_closed(dev);
460                 break;
461         }
462 }
463
464 static struct xenbus_driver xen_9pfs_front_driver = {
465         .ids = xen_9pfs_front_ids,
466         .probe = xen_9pfs_front_probe,
467         .remove = xen_9pfs_front_remove,
468         .resume = xen_9pfs_front_resume,
469         .otherend_changed = xen_9pfs_front_changed,
470 };
471
472 int p9_trans_xen_init(void)
473 {
474         if (!xen_domain())
475                 return -ENODEV;
476
477         pr_info("Initialising Xen transport for 9pfs\n");
478
479         v9fs_register_trans(&p9_xen_trans);
480         return xenbus_register_frontend(&xen_9pfs_front_driver);
481 }
482 module_init(p9_trans_xen_init);
483
484 void p9_trans_xen_exit(void)
485 {
486         v9fs_unregister_trans(&p9_xen_trans);
487         return xenbus_unregister_driver(&xen_9pfs_front_driver);
488 }
489 module_exit(p9_trans_xen_exit);