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