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 <net/9p/9p.h>
42 #include <net/9p/client.h>
43 #include <net/9p/transport.h>
44
45 #define XEN_9PFS_NUM_RINGS 2
46 #define XEN_9PFS_RING_ORDER 6
47 #define XEN_9PFS_RING_SIZE XEN_FLEX_RING_SIZE(XEN_9PFS_RING_ORDER)
48
49 struct xen_9pfs_header {
50 uint32_t size;
51 uint8_t id;
52 uint16_t tag;
53
54 /* uint8_t sdata[]; */
55 } __attribute__((packed));
56
57 /* One per ring, more than one per 9pfs share */
58 struct xen_9pfs_dataring {
59 struct xen_9pfs_front_priv *priv;
60
61 struct xen_9pfs_data_intf *intf;
62 grant_ref_t ref;
63 int evtchn;
64 int irq;
65 /* protect a ring from concurrent accesses */
66 spinlock_t lock;
67
68 struct xen_9pfs_data data;
69 wait_queue_head_t wq;
70 struct work_struct work;
71 };
72
73 /* One per 9pfs share */
74 struct xen_9pfs_front_priv {
75 struct list_head list;
76 struct xenbus_device *dev;
77 char *tag;
78 struct p9_client *client;
79
80 int num_rings;
81 struct xen_9pfs_dataring *rings;
82 };
83
84 static LIST_HEAD(xen_9pfs_devs);
85 static DEFINE_RWLOCK(xen_9pfs_lock);
86
87 /* We don't currently allow canceling of requests */
p9_xen_cancel(struct p9_client * client,struct p9_req_t * req)88 static int p9_xen_cancel(struct p9_client *client, struct p9_req_t *req)
89 {
90 return 1;
91 }
92
p9_xen_create(struct p9_client * client,const char * addr,char * args)93 static int p9_xen_create(struct p9_client *client, const char *addr, char *args)
94 {
95 struct xen_9pfs_front_priv *priv;
96
97 if (addr == NULL)
98 return -EINVAL;
99
100 read_lock(&xen_9pfs_lock);
101 list_for_each_entry(priv, &xen_9pfs_devs, list) {
102 if (!strcmp(priv->tag, addr)) {
103 priv->client = client;
104 read_unlock(&xen_9pfs_lock);
105 return 0;
106 }
107 }
108 read_unlock(&xen_9pfs_lock);
109 return -EINVAL;
110 }
111
p9_xen_close(struct p9_client * client)112 static void p9_xen_close(struct p9_client *client)
113 {
114 struct xen_9pfs_front_priv *priv;
115
116 read_lock(&xen_9pfs_lock);
117 list_for_each_entry(priv, &xen_9pfs_devs, list) {
118 if (priv->client == client) {
119 priv->client = NULL;
120 read_unlock(&xen_9pfs_lock);
121 return;
122 }
123 }
124 read_unlock(&xen_9pfs_lock);
125 }
126
p9_xen_write_todo(struct xen_9pfs_dataring * ring,RING_IDX size)127 static bool p9_xen_write_todo(struct xen_9pfs_dataring *ring, RING_IDX size)
128 {
129 RING_IDX cons, prod;
130
131 cons = ring->intf->out_cons;
132 prod = ring->intf->out_prod;
133 virt_mb();
134
135 return XEN_9PFS_RING_SIZE -
136 xen_9pfs_queued(prod, cons, XEN_9PFS_RING_SIZE) >= size;
137 }
138
p9_xen_request(struct p9_client * client,struct p9_req_t * p9_req)139 static int p9_xen_request(struct p9_client *client, struct p9_req_t *p9_req)
140 {
141 struct xen_9pfs_front_priv *priv;
142 RING_IDX cons, prod, masked_cons, masked_prod;
143 unsigned long flags;
144 u32 size = p9_req->tc.size;
145 struct xen_9pfs_dataring *ring;
146 int num;
147
148 read_lock(&xen_9pfs_lock);
149 list_for_each_entry(priv, &xen_9pfs_devs, list) {
150 if (priv->client == client)
151 break;
152 }
153 read_unlock(&xen_9pfs_lock);
154 if (list_entry_is_head(priv, &xen_9pfs_devs, list))
155 return -EINVAL;
156
157 num = p9_req->tc.tag % priv->num_rings;
158 ring = &priv->rings[num];
159
160 again:
161 while (wait_event_killable(ring->wq,
162 p9_xen_write_todo(ring, size)) != 0)
163 ;
164
165 spin_lock_irqsave(&ring->lock, flags);
166 cons = ring->intf->out_cons;
167 prod = ring->intf->out_prod;
168 virt_mb();
169
170 if (XEN_9PFS_RING_SIZE - xen_9pfs_queued(prod, cons,
171 XEN_9PFS_RING_SIZE) < size) {
172 spin_unlock_irqrestore(&ring->lock, flags);
173 goto again;
174 }
175
176 masked_prod = xen_9pfs_mask(prod, XEN_9PFS_RING_SIZE);
177 masked_cons = xen_9pfs_mask(cons, XEN_9PFS_RING_SIZE);
178
179 xen_9pfs_write_packet(ring->data.out, p9_req->tc.sdata, size,
180 &masked_prod, masked_cons, XEN_9PFS_RING_SIZE);
181
182 p9_req->status = REQ_STATUS_SENT;
183 virt_wmb(); /* write ring before updating pointer */
184 prod += size;
185 ring->intf->out_prod = prod;
186 spin_unlock_irqrestore(&ring->lock, flags);
187 notify_remote_via_irq(ring->irq);
188 p9_req_put(p9_req);
189
190 return 0;
191 }
192
p9_xen_response(struct work_struct * work)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 if (h.size > req->rc.capacity) {
234 dev_warn(&priv->dev->dev,
235 "requested packet size too big: %d for tag %d with capacity %zd\n",
236 h.size, h.tag, req->rc.capacity);
237 req->status = REQ_STATUS_ERROR;
238 goto recv_error;
239 }
240
241 memcpy(&req->rc, &h, sizeof(h));
242 req->rc.offset = 0;
243
244 masked_cons = xen_9pfs_mask(cons, XEN_9PFS_RING_SIZE);
245 /* Then, read the whole packet (including the header) */
246 xen_9pfs_read_packet(req->rc.sdata, ring->data.in, h.size,
247 masked_prod, &masked_cons,
248 XEN_9PFS_RING_SIZE);
249
250 recv_error:
251 virt_mb();
252 cons += h.size;
253 ring->intf->in_cons = cons;
254
255 status = (req->status != REQ_STATUS_ERROR) ?
256 REQ_STATUS_RCVD : REQ_STATUS_ERROR;
257
258 p9_client_cb(priv->client, req, status);
259 }
260 }
261
xen_9pfs_front_event_handler(int irq,void * r)262 static irqreturn_t xen_9pfs_front_event_handler(int irq, void *r)
263 {
264 struct xen_9pfs_dataring *ring = r;
265
266 if (!ring || !ring->priv->client) {
267 /* ignore spurious interrupt */
268 return IRQ_HANDLED;
269 }
270
271 wake_up_interruptible(&ring->wq);
272 schedule_work(&ring->work);
273
274 return IRQ_HANDLED;
275 }
276
277 static struct p9_trans_module p9_xen_trans = {
278 .name = "xen",
279 .maxsize = 1 << (XEN_9PFS_RING_ORDER + XEN_PAGE_SHIFT),
280 .def = 1,
281 .create = p9_xen_create,
282 .close = p9_xen_close,
283 .request = p9_xen_request,
284 .cancel = p9_xen_cancel,
285 .owner = THIS_MODULE,
286 };
287
288 static const struct xenbus_device_id xen_9pfs_front_ids[] = {
289 { "9pfs" },
290 { "" }
291 };
292
xen_9pfs_front_free(struct xen_9pfs_front_priv * priv)293 static void xen_9pfs_front_free(struct xen_9pfs_front_priv *priv)
294 {
295 int i, j;
296
297 write_lock(&xen_9pfs_lock);
298 list_del(&priv->list);
299 write_unlock(&xen_9pfs_lock);
300
301 for (i = 0; i < priv->num_rings; i++) {
302 struct xen_9pfs_dataring *ring = &priv->rings[i];
303
304 cancel_work_sync(&ring->work);
305
306 if (!priv->rings[i].intf)
307 break;
308 if (priv->rings[i].irq > 0)
309 unbind_from_irqhandler(priv->rings[i].irq, priv->dev);
310 if (priv->rings[i].data.in) {
311 for (j = 0; j < (1 << XEN_9PFS_RING_ORDER); j++) {
312 grant_ref_t ref;
313
314 ref = priv->rings[i].intf->ref[j];
315 gnttab_end_foreign_access(ref, 0, 0);
316 }
317 free_pages_exact(priv->rings[i].data.in,
318 1UL << (XEN_9PFS_RING_ORDER +
319 XEN_PAGE_SHIFT));
320 }
321 gnttab_end_foreign_access(priv->rings[i].ref, 0, 0);
322 free_page((unsigned long)priv->rings[i].intf);
323 }
324 kfree(priv->rings);
325 kfree(priv->tag);
326 kfree(priv);
327 }
328
xen_9pfs_front_remove(struct xenbus_device * dev)329 static int xen_9pfs_front_remove(struct xenbus_device *dev)
330 {
331 struct xen_9pfs_front_priv *priv = dev_get_drvdata(&dev->dev);
332
333 dev_set_drvdata(&dev->dev, NULL);
334 xen_9pfs_front_free(priv);
335 return 0;
336 }
337
xen_9pfs_front_alloc_dataring(struct xenbus_device * dev,struct xen_9pfs_dataring * ring)338 static int xen_9pfs_front_alloc_dataring(struct xenbus_device *dev,
339 struct xen_9pfs_dataring *ring)
340 {
341 int i = 0;
342 int ret = -ENOMEM;
343 void *bytes = NULL;
344
345 init_waitqueue_head(&ring->wq);
346 spin_lock_init(&ring->lock);
347 INIT_WORK(&ring->work, p9_xen_response);
348
349 ring->intf = (struct xen_9pfs_data_intf *)get_zeroed_page(GFP_KERNEL);
350 if (!ring->intf)
351 return ret;
352 ret = gnttab_grant_foreign_access(dev->otherend_id,
353 virt_to_gfn(ring->intf), 0);
354 if (ret < 0)
355 goto out;
356 ring->ref = ret;
357 bytes = alloc_pages_exact(1UL << (XEN_9PFS_RING_ORDER + XEN_PAGE_SHIFT),
358 GFP_KERNEL | __GFP_ZERO);
359 if (!bytes) {
360 ret = -ENOMEM;
361 goto out;
362 }
363 for (; i < (1 << XEN_9PFS_RING_ORDER); i++) {
364 ret = gnttab_grant_foreign_access(
365 dev->otherend_id, virt_to_gfn(bytes) + i, 0);
366 if (ret < 0)
367 goto out;
368 ring->intf->ref[i] = ret;
369 }
370 ring->intf->ring_order = XEN_9PFS_RING_ORDER;
371 ring->data.in = bytes;
372 ring->data.out = bytes + XEN_9PFS_RING_SIZE;
373
374 ret = xenbus_alloc_evtchn(dev, &ring->evtchn);
375 if (ret)
376 goto out;
377 ring->irq = bind_evtchn_to_irqhandler(ring->evtchn,
378 xen_9pfs_front_event_handler,
379 0, "xen_9pfs-frontend", ring);
380 if (ring->irq >= 0)
381 return 0;
382
383 xenbus_free_evtchn(dev, ring->evtchn);
384 ret = ring->irq;
385 out:
386 if (bytes) {
387 for (i--; i >= 0; i--)
388 gnttab_end_foreign_access(ring->intf->ref[i], 0, 0);
389 free_pages_exact(bytes, 1UL << (XEN_9PFS_RING_ORDER + XEN_PAGE_SHIFT));
390 }
391 gnttab_end_foreign_access(ring->ref, 0, 0);
392 free_page((unsigned long)ring->intf);
393 return ret;
394 }
395
xen_9pfs_front_init(struct xenbus_device * dev)396 static int xen_9pfs_front_init(struct xenbus_device *dev)
397 {
398 int ret, i;
399 struct xenbus_transaction xbt;
400 struct xen_9pfs_front_priv *priv = dev_get_drvdata(&dev->dev);
401 char *versions, *v;
402 unsigned int max_rings, max_ring_order, len = 0;
403
404 versions = xenbus_read(XBT_NIL, dev->otherend, "versions", &len);
405 if (IS_ERR(versions))
406 return PTR_ERR(versions);
407 for (v = versions; *v; v++) {
408 if (simple_strtoul(v, &v, 10) == 1) {
409 v = NULL;
410 break;
411 }
412 }
413 if (v) {
414 kfree(versions);
415 return -EINVAL;
416 }
417 kfree(versions);
418 max_rings = xenbus_read_unsigned(dev->otherend, "max-rings", 0);
419 if (max_rings < XEN_9PFS_NUM_RINGS)
420 return -EINVAL;
421 max_ring_order = xenbus_read_unsigned(dev->otherend,
422 "max-ring-page-order", 0);
423 if (max_ring_order < XEN_9PFS_RING_ORDER)
424 return -EINVAL;
425
426 priv->num_rings = XEN_9PFS_NUM_RINGS;
427 priv->rings = kcalloc(priv->num_rings, sizeof(*priv->rings),
428 GFP_KERNEL);
429 if (!priv->rings) {
430 kfree(priv);
431 return -ENOMEM;
432 }
433
434 for (i = 0; i < priv->num_rings; i++) {
435 priv->rings[i].priv = priv;
436 ret = xen_9pfs_front_alloc_dataring(dev, &priv->rings[i]);
437 if (ret < 0)
438 goto error;
439 }
440
441 again:
442 ret = xenbus_transaction_start(&xbt);
443 if (ret) {
444 xenbus_dev_fatal(dev, ret, "starting transaction");
445 goto error;
446 }
447 ret = xenbus_printf(xbt, dev->nodename, "version", "%u", 1);
448 if (ret)
449 goto error_xenbus;
450 ret = xenbus_printf(xbt, dev->nodename, "num-rings", "%u",
451 priv->num_rings);
452 if (ret)
453 goto error_xenbus;
454 for (i = 0; i < priv->num_rings; i++) {
455 char str[16];
456
457 BUILD_BUG_ON(XEN_9PFS_NUM_RINGS > 9);
458 sprintf(str, "ring-ref%u", i);
459 ret = xenbus_printf(xbt, dev->nodename, str, "%d",
460 priv->rings[i].ref);
461 if (ret)
462 goto error_xenbus;
463
464 sprintf(str, "event-channel-%u", i);
465 ret = xenbus_printf(xbt, dev->nodename, str, "%u",
466 priv->rings[i].evtchn);
467 if (ret)
468 goto error_xenbus;
469 }
470 priv->tag = xenbus_read(xbt, dev->nodename, "tag", NULL);
471 if (IS_ERR(priv->tag)) {
472 ret = PTR_ERR(priv->tag);
473 goto error_xenbus;
474 }
475 ret = xenbus_transaction_end(xbt, 0);
476 if (ret) {
477 if (ret == -EAGAIN)
478 goto again;
479 xenbus_dev_fatal(dev, ret, "completing transaction");
480 goto error;
481 }
482
483 return 0;
484
485 error_xenbus:
486 xenbus_transaction_end(xbt, 1);
487 xenbus_dev_fatal(dev, ret, "writing xenstore");
488 error:
489 xen_9pfs_front_free(priv);
490 return ret;
491 }
492
xen_9pfs_front_probe(struct xenbus_device * dev,const struct xenbus_device_id * id)493 static int xen_9pfs_front_probe(struct xenbus_device *dev,
494 const struct xenbus_device_id *id)
495 {
496 struct xen_9pfs_front_priv *priv = NULL;
497
498 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
499 if (!priv)
500 return -ENOMEM;
501
502 priv->dev = dev;
503 dev_set_drvdata(&dev->dev, priv);
504
505 write_lock(&xen_9pfs_lock);
506 list_add_tail(&priv->list, &xen_9pfs_devs);
507 write_unlock(&xen_9pfs_lock);
508
509 return 0;
510 }
511
xen_9pfs_front_resume(struct xenbus_device * dev)512 static int xen_9pfs_front_resume(struct xenbus_device *dev)
513 {
514 dev_warn(&dev->dev, "suspend/resume unsupported\n");
515 return 0;
516 }
517
xen_9pfs_front_changed(struct xenbus_device * dev,enum xenbus_state backend_state)518 static void xen_9pfs_front_changed(struct xenbus_device *dev,
519 enum xenbus_state backend_state)
520 {
521 switch (backend_state) {
522 case XenbusStateReconfiguring:
523 case XenbusStateReconfigured:
524 case XenbusStateInitialising:
525 case XenbusStateInitialised:
526 case XenbusStateUnknown:
527 break;
528
529 case XenbusStateInitWait:
530 if (!xen_9pfs_front_init(dev))
531 xenbus_switch_state(dev, XenbusStateInitialised);
532 break;
533
534 case XenbusStateConnected:
535 xenbus_switch_state(dev, XenbusStateConnected);
536 break;
537
538 case XenbusStateClosed:
539 if (dev->state == XenbusStateClosed)
540 break;
541 /* fall through - Missed the backend's CLOSING state */
542 case XenbusStateClosing:
543 xenbus_frontend_closed(dev);
544 break;
545 }
546 }
547
548 static struct xenbus_driver xen_9pfs_front_driver = {
549 .ids = xen_9pfs_front_ids,
550 .probe = xen_9pfs_front_probe,
551 .remove = xen_9pfs_front_remove,
552 .resume = xen_9pfs_front_resume,
553 .otherend_changed = xen_9pfs_front_changed,
554 };
555
p9_trans_xen_init(void)556 static int p9_trans_xen_init(void)
557 {
558 int rc;
559
560 if (!xen_domain())
561 return -ENODEV;
562
563 pr_info("Initialising Xen transport for 9pfs\n");
564
565 v9fs_register_trans(&p9_xen_trans);
566 rc = xenbus_register_frontend(&xen_9pfs_front_driver);
567 if (rc)
568 v9fs_unregister_trans(&p9_xen_trans);
569
570 return rc;
571 }
572 module_init(p9_trans_xen_init);
573
p9_trans_xen_exit(void)574 static void p9_trans_xen_exit(void)
575 {
576 v9fs_unregister_trans(&p9_xen_trans);
577 return xenbus_unregister_driver(&xen_9pfs_front_driver);
578 }
579 module_exit(p9_trans_xen_exit);
580
581 MODULE_AUTHOR("Stefano Stabellini <stefano@aporeto.com>");
582 MODULE_DESCRIPTION("Xen Transport for 9P");
583 MODULE_LICENSE("GPL");
584