Merge 'pci/enumeration' into loongarch-next
[linux-block.git] / net / 9p / trans_xen.c
CommitLineData
024b7d6a 1// SPDX-License-Identifier: GPL-2.0-only
868eb122
SS
2/*
3 * linux/fs/9p/trans_xen
4 *
5 * Xen transport layer.
6 *
7 * Copyright (C) 2017 by Stefano Stabellini <stefano@aporeto.com>
868eb122
SS
8 */
9
10#include <xen/events.h>
11#include <xen/grant_table.h>
12#include <xen/xen.h>
13#include <xen/xenbus.h>
14#include <xen/interface/io/9pfs.h>
15
16#include <linux/module.h>
71ebd719 17#include <linux/spinlock.h>
868eb122
SS
18#include <net/9p/9p.h>
19#include <net/9p/client.h>
20#include <net/9p/transport.h>
21
71ebd719 22#define XEN_9PFS_NUM_RINGS 2
36f99675
SS
23#define XEN_9PFS_RING_ORDER 9
24#define XEN_9PFS_RING_SIZE(ring) XEN_FLEX_RING_SIZE(ring->intf->ring_order)
71ebd719
SS
25
26struct xen_9pfs_header {
27 uint32_t size;
28 uint8_t id;
29 uint16_t tag;
30
31 /* uint8_t sdata[]; */
32} __attribute__((packed));
33
34/* One per ring, more than one per 9pfs share */
35struct xen_9pfs_dataring {
36 struct xen_9pfs_front_priv *priv;
37
38 struct xen_9pfs_data_intf *intf;
39 grant_ref_t ref;
40 int evtchn;
41 int irq;
42 /* protect a ring from concurrent accesses */
43 spinlock_t lock;
44
45 struct xen_9pfs_data data;
46 wait_queue_head_t wq;
47 struct work_struct work;
48};
49
50/* One per 9pfs share */
51struct xen_9pfs_front_priv {
52 struct list_head list;
53 struct xenbus_device *dev;
54 char *tag;
55 struct p9_client *client;
56
57 int num_rings;
58 struct xen_9pfs_dataring *rings;
59};
60
61static LIST_HEAD(xen_9pfs_devs);
62static DEFINE_RWLOCK(xen_9pfs_lock);
63
f023f18d 64/* We don't currently allow canceling of requests */
868eb122
SS
65static int p9_xen_cancel(struct p9_client *client, struct p9_req_t *req)
66{
f023f18d 67 return 1;
868eb122
SS
68}
69
70static int p9_xen_create(struct p9_client *client, const char *addr, char *args)
71{
f023f18d
SS
72 struct xen_9pfs_front_priv *priv;
73
10aa1452
TB
74 if (addr == NULL)
75 return -EINVAL;
76
f023f18d
SS
77 read_lock(&xen_9pfs_lock);
78 list_for_each_entry(priv, &xen_9pfs_devs, list) {
79 if (!strcmp(priv->tag, addr)) {
80 priv->client = client;
81 read_unlock(&xen_9pfs_lock);
82 return 0;
83 }
84 }
85 read_unlock(&xen_9pfs_lock);
86 return -EINVAL;
868eb122
SS
87}
88
89static void p9_xen_close(struct p9_client *client)
90{
f023f18d
SS
91 struct xen_9pfs_front_priv *priv;
92
93 read_lock(&xen_9pfs_lock);
94 list_for_each_entry(priv, &xen_9pfs_devs, list) {
95 if (priv->client == client) {
96 priv->client = NULL;
97 read_unlock(&xen_9pfs_lock);
98 return;
99 }
100 }
101 read_unlock(&xen_9pfs_lock);
102}
103
104static bool p9_xen_write_todo(struct xen_9pfs_dataring *ring, RING_IDX size)
105{
106 RING_IDX cons, prod;
107
108 cons = ring->intf->out_cons;
109 prod = ring->intf->out_prod;
110 virt_mb();
111
36f99675
SS
112 return XEN_9PFS_RING_SIZE(ring) -
113 xen_9pfs_queued(prod, cons, XEN_9PFS_RING_SIZE(ring)) >= size;
868eb122
SS
114}
115
116static int p9_xen_request(struct p9_client *client, struct p9_req_t *p9_req)
117{
732b33d0 118 struct xen_9pfs_front_priv *priv;
f023f18d
SS
119 RING_IDX cons, prod, masked_cons, masked_prod;
120 unsigned long flags;
523adb6c 121 u32 size = p9_req->tc.size;
f023f18d
SS
122 struct xen_9pfs_dataring *ring;
123 int num;
124
125 read_lock(&xen_9pfs_lock);
126 list_for_each_entry(priv, &xen_9pfs_devs, list) {
127 if (priv->client == client)
128 break;
129 }
130 read_unlock(&xen_9pfs_lock);
732b33d0 131 if (list_entry_is_head(priv, &xen_9pfs_devs, list))
f023f18d
SS
132 return -EINVAL;
133
523adb6c 134 num = p9_req->tc.tag % priv->num_rings;
f023f18d
SS
135 ring = &priv->rings[num];
136
137again:
9523feac
TT
138 while (wait_event_killable(ring->wq,
139 p9_xen_write_todo(ring, size)) != 0)
f023f18d
SS
140 ;
141
142 spin_lock_irqsave(&ring->lock, flags);
143 cons = ring->intf->out_cons;
144 prod = ring->intf->out_prod;
145 virt_mb();
146
36f99675
SS
147 if (XEN_9PFS_RING_SIZE(ring) -
148 xen_9pfs_queued(prod, cons, XEN_9PFS_RING_SIZE(ring)) < size) {
f023f18d
SS
149 spin_unlock_irqrestore(&ring->lock, flags);
150 goto again;
151 }
152
36f99675
SS
153 masked_prod = xen_9pfs_mask(prod, XEN_9PFS_RING_SIZE(ring));
154 masked_cons = xen_9pfs_mask(cons, XEN_9PFS_RING_SIZE(ring));
f023f18d 155
523adb6c 156 xen_9pfs_write_packet(ring->data.out, p9_req->tc.sdata, size,
36f99675
SS
157 &masked_prod, masked_cons,
158 XEN_9PFS_RING_SIZE(ring));
f023f18d 159
1a4f69ef 160 WRITE_ONCE(p9_req->status, REQ_STATUS_SENT);
f023f18d
SS
161 virt_wmb(); /* write ring before updating pointer */
162 prod += size;
163 ring->intf->out_prod = prod;
164 spin_unlock_irqrestore(&ring->lock, flags);
165 notify_remote_via_irq(ring->irq);
8b11ff09 166 p9_req_put(client, p9_req);
f023f18d 167
868eb122
SS
168 return 0;
169}
170
71ebd719
SS
171static void p9_xen_response(struct work_struct *work)
172{
f66c72be
SS
173 struct xen_9pfs_front_priv *priv;
174 struct xen_9pfs_dataring *ring;
175 RING_IDX cons, prod, masked_cons, masked_prod;
176 struct xen_9pfs_header h;
177 struct p9_req_t *req;
178 int status;
179
180 ring = container_of(work, struct xen_9pfs_dataring, work);
181 priv = ring->priv;
182
183 while (1) {
184 cons = ring->intf->in_cons;
185 prod = ring->intf->in_prod;
186 virt_rmb();
187
36f99675 188 if (xen_9pfs_queued(prod, cons, XEN_9PFS_RING_SIZE(ring)) <
f66c72be
SS
189 sizeof(h)) {
190 notify_remote_via_irq(ring->irq);
191 return;
192 }
193
36f99675
SS
194 masked_prod = xen_9pfs_mask(prod, XEN_9PFS_RING_SIZE(ring));
195 masked_cons = xen_9pfs_mask(cons, XEN_9PFS_RING_SIZE(ring));
f66c72be
SS
196
197 /* First, read just the header */
198 xen_9pfs_read_packet(&h, ring->data.in, sizeof(h),
199 masked_prod, &masked_cons,
36f99675 200 XEN_9PFS_RING_SIZE(ring));
f66c72be
SS
201
202 req = p9_tag_lookup(priv->client, h.tag);
203 if (!req || req->status != REQ_STATUS_SENT) {
204 dev_warn(&priv->dev->dev, "Wrong req tag=%x\n", h.tag);
205 cons += h.size;
206 virt_mb();
207 ring->intf->in_cons = cons;
208 continue;
209 }
210
391c18cf
DM
211 if (h.size > req->rc.capacity) {
212 dev_warn(&priv->dev->dev,
213 "requested packet size too big: %d for tag %d with capacity %zd\n",
214 h.size, h.tag, req->rc.capacity);
1a4f69ef 215 WRITE_ONCE(req->status, REQ_STATUS_ERROR);
391c18cf
DM
216 goto recv_error;
217 }
218
f15e006b
DM
219 req->rc.size = h.size;
220 req->rc.id = h.id;
221 req->rc.tag = h.tag;
523adb6c 222 req->rc.offset = 0;
f66c72be 223
36f99675 224 masked_cons = xen_9pfs_mask(cons, XEN_9PFS_RING_SIZE(ring));
f66c72be 225 /* Then, read the whole packet (including the header) */
523adb6c 226 xen_9pfs_read_packet(req->rc.sdata, ring->data.in, h.size,
f66c72be 227 masked_prod, &masked_cons,
36f99675 228 XEN_9PFS_RING_SIZE(ring));
f66c72be 229
391c18cf 230recv_error:
f66c72be
SS
231 virt_mb();
232 cons += h.size;
233 ring->intf->in_cons = cons;
234
235 status = (req->status != REQ_STATUS_ERROR) ?
236 REQ_STATUS_RCVD : REQ_STATUS_ERROR;
237
238 p9_client_cb(priv->client, req, status);
239 }
71ebd719
SS
240}
241
242static irqreturn_t xen_9pfs_front_event_handler(int irq, void *r)
243{
244 struct xen_9pfs_dataring *ring = r;
245
246 if (!ring || !ring->priv->client) {
247 /* ignore spurious interrupt */
248 return IRQ_HANDLED;
249 }
250
251 wake_up_interruptible(&ring->wq);
252 schedule_work(&ring->work);
253
254 return IRQ_HANDLED;
255}
256
868eb122
SS
257static struct p9_trans_module p9_xen_trans = {
258 .name = "xen",
36f99675 259 .maxsize = 1 << (XEN_9PFS_RING_ORDER + XEN_PAGE_SHIFT - 2),
01d205d9 260 .pooled_rbuffers = false,
868eb122
SS
261 .def = 1,
262 .create = p9_xen_create,
263 .close = p9_xen_close,
264 .request = p9_xen_request,
265 .cancel = p9_xen_cancel,
266 .owner = THIS_MODULE,
267};
268
269static const struct xenbus_device_id xen_9pfs_front_ids[] = {
270 { "9pfs" },
271 { "" }
272};
273
71ebd719
SS
274static void xen_9pfs_front_free(struct xen_9pfs_front_priv *priv)
275{
276 int i, j;
277
278 write_lock(&xen_9pfs_lock);
279 list_del(&priv->list);
280 write_unlock(&xen_9pfs_lock);
281
282 for (i = 0; i < priv->num_rings; i++) {
283 if (!priv->rings[i].intf)
284 break;
285 if (priv->rings[i].irq > 0)
286 unbind_from_irqhandler(priv->rings[i].irq, priv->dev);
287 if (priv->rings[i].data.in) {
36f99675
SS
288 for (j = 0;
289 j < (1 << priv->rings[i].intf->ring_order);
290 j++) {
71ebd719
SS
291 grant_ref_t ref;
292
293 ref = priv->rings[i].intf->ref[j];
49f8b459 294 gnttab_end_foreign_access(ref, NULL);
71ebd719 295 }
5cadd4bb
JG
296 free_pages_exact(priv->rings[i].data.in,
297 1UL << (priv->rings[i].intf->ring_order +
298 XEN_PAGE_SHIFT));
71ebd719 299 }
49f8b459 300 gnttab_end_foreign_access(priv->rings[i].ref, NULL);
71ebd719
SS
301 free_page((unsigned long)priv->rings[i].intf);
302 }
303 kfree(priv->rings);
304 kfree(priv->tag);
305 kfree(priv);
306}
307
7cffcade 308static void xen_9pfs_front_remove(struct xenbus_device *dev)
868eb122 309{
71ebd719
SS
310 struct xen_9pfs_front_priv *priv = dev_get_drvdata(&dev->dev);
311
312 dev_set_drvdata(&dev->dev, NULL);
313 xen_9pfs_front_free(priv);
868eb122
SS
314}
315
71ebd719 316static int xen_9pfs_front_alloc_dataring(struct xenbus_device *dev,
36f99675
SS
317 struct xen_9pfs_dataring *ring,
318 unsigned int order)
71ebd719
SS
319{
320 int i = 0;
321 int ret = -ENOMEM;
322 void *bytes = NULL;
323
324 init_waitqueue_head(&ring->wq);
325 spin_lock_init(&ring->lock);
326 INIT_WORK(&ring->work, p9_xen_response);
327
328 ring->intf = (struct xen_9pfs_data_intf *)get_zeroed_page(GFP_KERNEL);
329 if (!ring->intf)
330 return ret;
331 ret = gnttab_grant_foreign_access(dev->otherend_id,
332 virt_to_gfn(ring->intf), 0);
333 if (ret < 0)
334 goto out;
335 ring->ref = ret;
5cadd4bb
JG
336 bytes = alloc_pages_exact(1UL << (order + XEN_PAGE_SHIFT),
337 GFP_KERNEL | __GFP_ZERO);
71ebd719
SS
338 if (!bytes) {
339 ret = -ENOMEM;
340 goto out;
341 }
36f99675 342 for (; i < (1 << order); i++) {
71ebd719
SS
343 ret = gnttab_grant_foreign_access(
344 dev->otherend_id, virt_to_gfn(bytes) + i, 0);
345 if (ret < 0)
346 goto out;
347 ring->intf->ref[i] = ret;
348 }
36f99675 349 ring->intf->ring_order = order;
71ebd719 350 ring->data.in = bytes;
36f99675 351 ring->data.out = bytes + XEN_FLEX_RING_SIZE(order);
71ebd719
SS
352
353 ret = xenbus_alloc_evtchn(dev, &ring->evtchn);
354 if (ret)
355 goto out;
356 ring->irq = bind_evtchn_to_irqhandler(ring->evtchn,
357 xen_9pfs_front_event_handler,
358 0, "xen_9pfs-frontend", ring);
359 if (ring->irq >= 0)
360 return 0;
361
362 xenbus_free_evtchn(dev, ring->evtchn);
363 ret = ring->irq;
364out:
365 if (bytes) {
366 for (i--; i >= 0; i--)
49f8b459 367 gnttab_end_foreign_access(ring->intf->ref[i], NULL);
5cadd4bb 368 free_pages_exact(bytes, 1UL << (order + XEN_PAGE_SHIFT));
71ebd719 369 }
49f8b459 370 gnttab_end_foreign_access(ring->ref, NULL);
71ebd719
SS
371 free_page((unsigned long)ring->intf);
372 return ret;
373}
374
868eb122
SS
375static int xen_9pfs_front_probe(struct xenbus_device *dev,
376 const struct xenbus_device_id *id)
377{
71ebd719
SS
378 int ret, i;
379 struct xenbus_transaction xbt;
380 struct xen_9pfs_front_priv *priv = NULL;
381 char *versions;
31d47266 382 unsigned int max_rings, max_ring_order, len = 0;
71ebd719
SS
383
384 versions = xenbus_read(XBT_NIL, dev->otherend, "versions", &len);
2f9ad0ac
DM
385 if (IS_ERR(versions))
386 return PTR_ERR(versions);
71ebd719
SS
387 if (strcmp(versions, "1")) {
388 kfree(versions);
389 return -EINVAL;
390 }
391 kfree(versions);
392 max_rings = xenbus_read_unsigned(dev->otherend, "max-rings", 0);
393 if (max_rings < XEN_9PFS_NUM_RINGS)
394 return -EINVAL;
395 max_ring_order = xenbus_read_unsigned(dev->otherend,
396 "max-ring-page-order", 0);
36f99675
SS
397 if (max_ring_order > XEN_9PFS_RING_ORDER)
398 max_ring_order = XEN_9PFS_RING_ORDER;
399 if (p9_xen_trans.maxsize > XEN_FLEX_RING_SIZE(max_ring_order))
400 p9_xen_trans.maxsize = XEN_FLEX_RING_SIZE(max_ring_order) / 2;
71ebd719
SS
401
402 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
403 if (!priv)
404 return -ENOMEM;
405
406 priv->dev = dev;
407 priv->num_rings = XEN_9PFS_NUM_RINGS;
408 priv->rings = kcalloc(priv->num_rings, sizeof(*priv->rings),
409 GFP_KERNEL);
410 if (!priv->rings) {
411 kfree(priv);
412 return -ENOMEM;
413 }
414
415 for (i = 0; i < priv->num_rings; i++) {
416 priv->rings[i].priv = priv;
36f99675
SS
417 ret = xen_9pfs_front_alloc_dataring(dev, &priv->rings[i],
418 max_ring_order);
71ebd719
SS
419 if (ret < 0)
420 goto error;
421 }
422
423 again:
424 ret = xenbus_transaction_start(&xbt);
425 if (ret) {
426 xenbus_dev_fatal(dev, ret, "starting transaction");
427 goto error;
428 }
429 ret = xenbus_printf(xbt, dev->nodename, "version", "%u", 1);
430 if (ret)
431 goto error_xenbus;
432 ret = xenbus_printf(xbt, dev->nodename, "num-rings", "%u",
433 priv->num_rings);
434 if (ret)
435 goto error_xenbus;
436 for (i = 0; i < priv->num_rings; i++) {
437 char str[16];
438
439 BUILD_BUG_ON(XEN_9PFS_NUM_RINGS > 9);
316a1bef 440 sprintf(str, "ring-ref%d", i);
71ebd719
SS
441 ret = xenbus_printf(xbt, dev->nodename, str, "%d",
442 priv->rings[i].ref);
443 if (ret)
444 goto error_xenbus;
445
316a1bef 446 sprintf(str, "event-channel-%d", i);
71ebd719
SS
447 ret = xenbus_printf(xbt, dev->nodename, str, "%u",
448 priv->rings[i].evtchn);
449 if (ret)
450 goto error_xenbus;
451 }
452 priv->tag = xenbus_read(xbt, dev->nodename, "tag", NULL);
14e3995e
WY
453 if (IS_ERR(priv->tag)) {
454 ret = PTR_ERR(priv->tag);
71ebd719
SS
455 goto error_xenbus;
456 }
457 ret = xenbus_transaction_end(xbt, 0);
458 if (ret) {
459 if (ret == -EAGAIN)
460 goto again;
461 xenbus_dev_fatal(dev, ret, "completing transaction");
462 goto error;
463 }
464
465 write_lock(&xen_9pfs_lock);
466 list_add_tail(&priv->list, &xen_9pfs_devs);
467 write_unlock(&xen_9pfs_lock);
468 dev_set_drvdata(&dev->dev, priv);
469 xenbus_switch_state(dev, XenbusStateInitialised);
470
868eb122 471 return 0;
71ebd719
SS
472
473 error_xenbus:
474 xenbus_transaction_end(xbt, 1);
475 xenbus_dev_fatal(dev, ret, "writing xenstore");
476 error:
477 dev_set_drvdata(&dev->dev, NULL);
478 xen_9pfs_front_free(priv);
479 return ret;
868eb122
SS
480}
481
482static int xen_9pfs_front_resume(struct xenbus_device *dev)
483{
680a2846 484 dev_warn(&dev->dev, "suspend/resume unsupported\n");
868eb122
SS
485 return 0;
486}
487
488static void xen_9pfs_front_changed(struct xenbus_device *dev,
489 enum xenbus_state backend_state)
490{
71ebd719
SS
491 switch (backend_state) {
492 case XenbusStateReconfiguring:
493 case XenbusStateReconfigured:
494 case XenbusStateInitialising:
495 case XenbusStateInitialised:
496 case XenbusStateUnknown:
497 break;
498
499 case XenbusStateInitWait:
500 break;
501
502 case XenbusStateConnected:
503 xenbus_switch_state(dev, XenbusStateConnected);
504 break;
505
506 case XenbusStateClosed:
507 if (dev->state == XenbusStateClosed)
508 break;
df561f66 509 fallthrough; /* Missed the backend's CLOSING state */
71ebd719
SS
510 case XenbusStateClosing:
511 xenbus_frontend_closed(dev);
512 break;
513 }
868eb122
SS
514}
515
516static struct xenbus_driver xen_9pfs_front_driver = {
517 .ids = xen_9pfs_front_ids,
518 .probe = xen_9pfs_front_probe,
519 .remove = xen_9pfs_front_remove,
520 .resume = xen_9pfs_front_resume,
521 .otherend_changed = xen_9pfs_front_changed,
522};
523
0664c63a 524static int __init p9_trans_xen_init(void)
868eb122 525{
80a316ff
Y
526 int rc;
527
868eb122
SS
528 if (!xen_domain())
529 return -ENODEV;
530
531 pr_info("Initialising Xen transport for 9pfs\n");
532
533 v9fs_register_trans(&p9_xen_trans);
80a316ff
Y
534 rc = xenbus_register_frontend(&xen_9pfs_front_driver);
535 if (rc)
536 v9fs_unregister_trans(&p9_xen_trans);
537
538 return rc;
868eb122
SS
539}
540module_init(p9_trans_xen_init);
4cd82a5b 541MODULE_ALIAS_9P("xen");
868eb122 542
0664c63a 543static void __exit p9_trans_xen_exit(void)
868eb122
SS
544{
545 v9fs_unregister_trans(&p9_xen_trans);
546 return xenbus_unregister_driver(&xen_9pfs_front_driver);
547}
548module_exit(p9_trans_xen_exit);
d542296a 549
99aa673e 550MODULE_ALIAS("xen:9pfs");
d542296a
SH
551MODULE_AUTHOR("Stefano Stabellini <stefano@aporeto.com>");
552MODULE_DESCRIPTION("Xen Transport for 9P");
553MODULE_LICENSE("GPL");