2 * linux/fs/9p/trans_xen
6 * Copyright (C) 2017 by Stefano Stabellini <stefano@aporeto.com>
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:
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:
21 * The above copyright notice and this permission notice shall be included in
22 * all copies or substantial portions of the Software.
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
33 #include <xen/events.h>
34 #include <xen/grant_table.h>
36 #include <xen/xenbus.h>
37 #include <xen/interface/io/9pfs.h>
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>
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)
50 struct xen_9pfs_header {
55 /* uint8_t sdata[]; */
56 } __attribute__((packed));
58 /* One per ring, more than one per 9pfs share */
59 struct xen_9pfs_dataring {
60 struct xen_9pfs_front_priv *priv;
62 struct xen_9pfs_data_intf *intf;
66 /* protect a ring from concurrent accesses */
69 struct xen_9pfs_data data;
71 struct work_struct work;
74 /* One per 9pfs share */
75 struct xen_9pfs_front_priv {
76 struct list_head list;
77 struct xenbus_device *dev;
79 struct p9_client *client;
82 struct xen_9pfs_dataring *rings;
85 static LIST_HEAD(xen_9pfs_devs);
86 static DEFINE_RWLOCK(xen_9pfs_lock);
88 /* We don't currently allow canceling of requests */
89 static int p9_xen_cancel(struct p9_client *client, struct p9_req_t *req)
94 static int p9_xen_create(struct p9_client *client, const char *addr, char *args)
96 struct xen_9pfs_front_priv *priv;
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);
109 read_unlock(&xen_9pfs_lock);
113 static void p9_xen_close(struct p9_client *client)
115 struct xen_9pfs_front_priv *priv;
117 read_lock(&xen_9pfs_lock);
118 list_for_each_entry(priv, &xen_9pfs_devs, list) {
119 if (priv->client == client) {
121 read_unlock(&xen_9pfs_lock);
125 read_unlock(&xen_9pfs_lock);
128 static bool p9_xen_write_todo(struct xen_9pfs_dataring *ring, RING_IDX size)
132 cons = ring->intf->out_cons;
133 prod = ring->intf->out_prod;
136 return XEN_9PFS_RING_SIZE -
137 xen_9pfs_queued(prod, cons, XEN_9PFS_RING_SIZE) >= size;
140 static int p9_xen_request(struct p9_client *client, struct p9_req_t *p9_req)
142 struct xen_9pfs_front_priv *priv;
143 RING_IDX cons, prod, masked_cons, masked_prod;
145 u32 size = p9_req->tc->size;
146 struct xen_9pfs_dataring *ring;
149 read_lock(&xen_9pfs_lock);
150 list_for_each_entry(priv, &xen_9pfs_devs, list) {
151 if (priv->client == client)
154 read_unlock(&xen_9pfs_lock);
155 if (list_entry_is_head(priv, &xen_9pfs_devs, list))
158 num = p9_req->tc->tag % priv->num_rings;
159 ring = &priv->rings[num];
162 while (wait_event_killable(ring->wq,
163 p9_xen_write_todo(ring, size)) != 0)
166 spin_lock_irqsave(&ring->lock, flags);
167 cons = ring->intf->out_cons;
168 prod = ring->intf->out_prod;
171 if (XEN_9PFS_RING_SIZE - xen_9pfs_queued(prod, cons,
172 XEN_9PFS_RING_SIZE) < size) {
173 spin_unlock_irqrestore(&ring->lock, flags);
177 masked_prod = xen_9pfs_mask(prod, XEN_9PFS_RING_SIZE);
178 masked_cons = xen_9pfs_mask(cons, XEN_9PFS_RING_SIZE);
180 xen_9pfs_write_packet(ring->data.out, p9_req->tc->sdata, size,
181 &masked_prod, masked_cons, XEN_9PFS_RING_SIZE);
183 p9_req->status = REQ_STATUS_SENT;
184 virt_wmb(); /* write ring before updating pointer */
186 ring->intf->out_prod = prod;
187 spin_unlock_irqrestore(&ring->lock, flags);
188 notify_remote_via_irq(ring->irq);
193 static void p9_xen_response(struct work_struct *work)
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;
202 ring = container_of(work, struct xen_9pfs_dataring, work);
206 cons = ring->intf->in_cons;
207 prod = ring->intf->in_prod;
210 if (xen_9pfs_queued(prod, cons, XEN_9PFS_RING_SIZE) <
212 notify_remote_via_irq(ring->irq);
216 masked_prod = xen_9pfs_mask(prod, XEN_9PFS_RING_SIZE);
217 masked_cons = xen_9pfs_mask(cons, XEN_9PFS_RING_SIZE);
219 /* First, read just the header */
220 xen_9pfs_read_packet(&h, ring->data.in, sizeof(h),
221 masked_prod, &masked_cons,
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);
229 ring->intf->in_cons = cons;
233 memcpy(req->rc, &h, sizeof(h));
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,
244 ring->intf->in_cons = cons;
246 status = (req->status != REQ_STATUS_ERROR) ?
247 REQ_STATUS_RCVD : REQ_STATUS_ERROR;
249 p9_client_cb(priv->client, req, status);
253 static irqreturn_t xen_9pfs_front_event_handler(int irq, void *r)
255 struct xen_9pfs_dataring *ring = r;
257 if (!ring || !ring->priv->client) {
258 /* ignore spurious interrupt */
262 wake_up_interruptible(&ring->wq);
263 schedule_work(&ring->work);
268 static struct p9_trans_module p9_xen_trans = {
270 .maxsize = 1 << (XEN_9PFS_RING_ORDER + XEN_PAGE_SHIFT),
272 .create = p9_xen_create,
273 .close = p9_xen_close,
274 .request = p9_xen_request,
275 .cancel = p9_xen_cancel,
276 .owner = THIS_MODULE,
279 static const struct xenbus_device_id xen_9pfs_front_ids[] = {
284 static void xen_9pfs_front_free(struct xen_9pfs_front_priv *priv)
288 write_lock(&xen_9pfs_lock);
289 list_del(&priv->list);
290 write_unlock(&xen_9pfs_lock);
292 for (i = 0; i < priv->num_rings; i++) {
293 struct xen_9pfs_dataring *ring = &priv->rings[i];
295 cancel_work_sync(&ring->work);
297 if (!priv->rings[i].intf)
299 if (priv->rings[i].irq > 0)
300 unbind_from_irqhandler(priv->rings[i].irq, priv->dev);
301 if (priv->rings[i].data.in) {
302 for (j = 0; j < (1 << XEN_9PFS_RING_ORDER); j++) {
305 ref = priv->rings[i].intf->ref[j];
306 gnttab_end_foreign_access(ref, 0, 0);
308 free_pages_exact(priv->rings[i].data.in,
309 1UL << (XEN_9PFS_RING_ORDER +
312 gnttab_end_foreign_access(priv->rings[i].ref, 0, 0);
313 free_page((unsigned long)priv->rings[i].intf);
320 static int xen_9pfs_front_remove(struct xenbus_device *dev)
322 struct xen_9pfs_front_priv *priv = dev_get_drvdata(&dev->dev);
324 dev_set_drvdata(&dev->dev, NULL);
325 xen_9pfs_front_free(priv);
329 static int xen_9pfs_front_alloc_dataring(struct xenbus_device *dev,
330 struct xen_9pfs_dataring *ring)
336 init_waitqueue_head(&ring->wq);
337 spin_lock_init(&ring->lock);
338 INIT_WORK(&ring->work, p9_xen_response);
340 ring->intf = (struct xen_9pfs_data_intf *)get_zeroed_page(GFP_KERNEL);
343 ret = gnttab_grant_foreign_access(dev->otherend_id,
344 virt_to_gfn(ring->intf), 0);
348 bytes = alloc_pages_exact(1UL << (XEN_9PFS_RING_ORDER + XEN_PAGE_SHIFT),
349 GFP_KERNEL | __GFP_ZERO);
354 for (; i < (1 << XEN_9PFS_RING_ORDER); i++) {
355 ret = gnttab_grant_foreign_access(
356 dev->otherend_id, virt_to_gfn(bytes) + i, 0);
359 ring->intf->ref[i] = ret;
361 ring->intf->ring_order = XEN_9PFS_RING_ORDER;
362 ring->data.in = bytes;
363 ring->data.out = bytes + XEN_9PFS_RING_SIZE;
365 ret = xenbus_alloc_evtchn(dev, &ring->evtchn);
368 ring->irq = bind_evtchn_to_irqhandler(ring->evtchn,
369 xen_9pfs_front_event_handler,
370 0, "xen_9pfs-frontend", ring);
374 xenbus_free_evtchn(dev, ring->evtchn);
378 for (i--; i >= 0; i--)
379 gnttab_end_foreign_access(ring->intf->ref[i], 0, 0);
380 free_pages_exact(bytes, 1UL << (XEN_9PFS_RING_ORDER + XEN_PAGE_SHIFT));
382 gnttab_end_foreign_access(ring->ref, 0, 0);
383 free_page((unsigned long)ring->intf);
387 static int xen_9pfs_front_init(struct xenbus_device *dev)
390 struct xenbus_transaction xbt;
391 struct xen_9pfs_front_priv *priv = dev_get_drvdata(&dev->dev);
393 unsigned int max_rings, max_ring_order, len = 0;
395 versions = xenbus_read(XBT_NIL, dev->otherend, "versions", &len);
396 if (IS_ERR(versions))
397 return PTR_ERR(versions);
398 for (v = versions; *v; v++) {
399 if (simple_strtoul(v, &v, 10) == 1) {
409 max_rings = xenbus_read_unsigned(dev->otherend, "max-rings", 0);
410 if (max_rings < XEN_9PFS_NUM_RINGS)
412 max_ring_order = xenbus_read_unsigned(dev->otherend,
413 "max-ring-page-order", 0);
414 if (max_ring_order < XEN_9PFS_RING_ORDER)
417 priv->num_rings = XEN_9PFS_NUM_RINGS;
418 priv->rings = kcalloc(priv->num_rings, sizeof(*priv->rings),
425 for (i = 0; i < priv->num_rings; i++) {
426 priv->rings[i].priv = priv;
427 ret = xen_9pfs_front_alloc_dataring(dev, &priv->rings[i]);
433 ret = xenbus_transaction_start(&xbt);
435 xenbus_dev_fatal(dev, ret, "starting transaction");
438 ret = xenbus_printf(xbt, dev->nodename, "version", "%u", 1);
441 ret = xenbus_printf(xbt, dev->nodename, "num-rings", "%u",
445 for (i = 0; i < priv->num_rings; i++) {
448 BUILD_BUG_ON(XEN_9PFS_NUM_RINGS > 9);
449 sprintf(str, "ring-ref%u", i);
450 ret = xenbus_printf(xbt, dev->nodename, str, "%d",
455 sprintf(str, "event-channel-%u", i);
456 ret = xenbus_printf(xbt, dev->nodename, str, "%u",
457 priv->rings[i].evtchn);
461 priv->tag = xenbus_read(xbt, dev->nodename, "tag", NULL);
462 if (IS_ERR(priv->tag)) {
463 ret = PTR_ERR(priv->tag);
466 ret = xenbus_transaction_end(xbt, 0);
470 xenbus_dev_fatal(dev, ret, "completing transaction");
477 xenbus_transaction_end(xbt, 1);
478 xenbus_dev_fatal(dev, ret, "writing xenstore");
480 xen_9pfs_front_free(priv);
484 static int xen_9pfs_front_probe(struct xenbus_device *dev,
485 const struct xenbus_device_id *id)
487 struct xen_9pfs_front_priv *priv = NULL;
489 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
494 dev_set_drvdata(&dev->dev, priv);
496 write_lock(&xen_9pfs_lock);
497 list_add_tail(&priv->list, &xen_9pfs_devs);
498 write_unlock(&xen_9pfs_lock);
503 static int xen_9pfs_front_resume(struct xenbus_device *dev)
505 dev_warn(&dev->dev, "suspsend/resume unsupported\n");
509 static void xen_9pfs_front_changed(struct xenbus_device *dev,
510 enum xenbus_state backend_state)
512 switch (backend_state) {
513 case XenbusStateReconfiguring:
514 case XenbusStateReconfigured:
515 case XenbusStateInitialising:
516 case XenbusStateInitialised:
517 case XenbusStateUnknown:
520 case XenbusStateInitWait:
521 if (!xen_9pfs_front_init(dev))
522 xenbus_switch_state(dev, XenbusStateInitialised);
525 case XenbusStateConnected:
526 xenbus_switch_state(dev, XenbusStateConnected);
529 case XenbusStateClosed:
530 if (dev->state == XenbusStateClosed)
532 /* Missed the backend's CLOSING state -- fallthrough */
533 case XenbusStateClosing:
534 xenbus_frontend_closed(dev);
539 static struct xenbus_driver xen_9pfs_front_driver = {
540 .ids = xen_9pfs_front_ids,
541 .probe = xen_9pfs_front_probe,
542 .remove = xen_9pfs_front_remove,
543 .resume = xen_9pfs_front_resume,
544 .otherend_changed = xen_9pfs_front_changed,
547 static int p9_trans_xen_init(void)
554 pr_info("Initialising Xen transport for 9pfs\n");
556 v9fs_register_trans(&p9_xen_trans);
557 rc = xenbus_register_frontend(&xen_9pfs_front_driver);
559 v9fs_unregister_trans(&p9_xen_trans);
563 module_init(p9_trans_xen_init);
565 static void p9_trans_xen_exit(void)
567 v9fs_unregister_trans(&p9_xen_trans);
568 return xenbus_unregister_driver(&xen_9pfs_front_driver);
570 module_exit(p9_trans_xen_exit);