trans_xen.c 14 KB

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