trans_xen.c 14 KB

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