trans_xen.c 14 KB

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