trans_xen.c 14 KB

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