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