trans_xen.c 14 KB

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