trans_virtio.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /*
  2. * The Guest 9p transport driver
  3. *
  4. * This is a trivial pipe-based transport driver based on the lguest console
  5. * code: we use lguest's DMA mechanism to send bytes out, and register a
  6. * DMA buffer to receive bytes in. It is assumed to be present and available
  7. * from the very beginning of boot.
  8. *
  9. * This may be have been done by just instaniating another HVC console,
  10. * but HVC's blocksize of 16 bytes is annoying and painful to performance.
  11. *
  12. * A more efficient transport could be built based on the virtio block driver
  13. * but it requires some changes in the 9p transport model (which are in
  14. * progress)
  15. *
  16. */
  17. /*
  18. * Copyright (C) 2007 Eric Van Hensbergen, IBM Corporation
  19. *
  20. * Based on virtio console driver
  21. * Copyright (C) 2006, 2007 Rusty Russell, IBM Corporation
  22. *
  23. * This program is free software; you can redistribute it and/or modify
  24. * it under the terms of the GNU General Public License version 2
  25. * as published by the Free Software Foundation.
  26. *
  27. * This program is distributed in the hope that it will be useful,
  28. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  29. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  30. * GNU General Public License for more details.
  31. *
  32. * You should have received a copy of the GNU General Public License
  33. * along with this program; if not, write to:
  34. * Free Software Foundation
  35. * 51 Franklin Street, Fifth Floor
  36. * Boston, MA 02111-1301 USA
  37. *
  38. */
  39. #include <linux/in.h>
  40. #include <linux/module.h>
  41. #include <linux/net.h>
  42. #include <linux/ipv6.h>
  43. #include <linux/errno.h>
  44. #include <linux/kernel.h>
  45. #include <linux/un.h>
  46. #include <linux/uaccess.h>
  47. #include <linux/inet.h>
  48. #include <linux/idr.h>
  49. #include <linux/file.h>
  50. #include <net/9p/9p.h>
  51. #include <linux/parser.h>
  52. #include <net/9p/transport.h>
  53. #include <linux/scatterlist.h>
  54. #include <linux/virtio.h>
  55. #include <linux/virtio_9p.h>
  56. /* a single mutex to manage channel initialization and attachment */
  57. static DECLARE_MUTEX(virtio_9p_lock);
  58. /* global which tracks highest initialized channel */
  59. static int chan_index;
  60. /* We keep all per-channel information in a structure.
  61. * This structure is allocated within the devices dev->mem space.
  62. * A pointer to the structure will get put in the transport private.
  63. */
  64. static struct virtio_chan {
  65. bool initialized; /* channel is initialized */
  66. bool inuse; /* channel is in use */
  67. struct virtqueue *in_vq, *out_vq;
  68. struct virtio_device *vdev;
  69. /* This is our input buffer, and how much data is left in it. */
  70. unsigned int in_len;
  71. char *in, *inbuf;
  72. wait_queue_head_t wq; /* waitq for buffer */
  73. } channels[MAX_9P_CHAN];
  74. /* How many bytes left in this page. */
  75. static unsigned int rest_of_page(void *data)
  76. {
  77. return PAGE_SIZE - ((unsigned long)data % PAGE_SIZE);
  78. }
  79. static int p9_virtio_write(struct p9_trans *trans, void *buf, int count)
  80. {
  81. struct virtio_chan *chan = (struct virtio_chan *) trans->priv;
  82. struct virtqueue *out_vq = chan->out_vq;
  83. struct scatterlist sg[1];
  84. unsigned int len;
  85. P9_DPRINTK(P9_DEBUG_TRANS, "9p debug: virtio write (%d)\n", count);
  86. /* keep it simple - make sure we don't overflow a page */
  87. if (rest_of_page(buf) < count)
  88. count = rest_of_page(buf);
  89. sg_init_one(sg, buf, count);
  90. /* add_buf wants a token to identify this buffer: we hand it any
  91. * non-NULL pointer, since there's only ever one buffer. */
  92. if (out_vq->vq_ops->add_buf(out_vq, sg, 1, 0, (void *)1) == 0) {
  93. /* Tell Host to go! */
  94. out_vq->vq_ops->kick(out_vq);
  95. /* Chill out until it's done with the buffer. */
  96. while (!out_vq->vq_ops->get_buf(out_vq, &len))
  97. cpu_relax();
  98. }
  99. P9_DPRINTK(P9_DEBUG_TRANS, "9p debug: virtio wrote (%d)\n", count);
  100. /* We're expected to return the amount of data we wrote: all of it. */
  101. return count;
  102. }
  103. /* Create a scatter-gather list representing our input buffer and put it in the
  104. * queue. */
  105. static void add_inbuf(struct virtio_chan *chan)
  106. {
  107. struct scatterlist sg[1];
  108. sg_init_one(sg, chan->inbuf, PAGE_SIZE);
  109. /* We should always be able to add one buffer to an empty queue. */
  110. if (chan->in_vq->vq_ops->add_buf(chan->in_vq, sg, 0, 1, chan->inbuf))
  111. BUG();
  112. chan->in_vq->vq_ops->kick(chan->in_vq);
  113. }
  114. static int p9_virtio_read(struct p9_trans *trans, void *buf, int count)
  115. {
  116. struct virtio_chan *chan = (struct virtio_chan *) trans->priv;
  117. struct virtqueue *in_vq = chan->in_vq;
  118. P9_DPRINTK(P9_DEBUG_TRANS, "9p debug: virtio read (%d)\n", count);
  119. /* If we don't have an input queue yet, we can't get input. */
  120. BUG_ON(!in_vq);
  121. /* No buffer? Try to get one. */
  122. if (!chan->in_len) {
  123. chan->in = in_vq->vq_ops->get_buf(in_vq, &chan->in_len);
  124. if (!chan->in)
  125. return 0;
  126. }
  127. /* You want more than we have to give? Well, try wanting less! */
  128. if (chan->in_len < count)
  129. count = chan->in_len;
  130. /* Copy across to their buffer and increment offset. */
  131. memcpy(buf, chan->in, count);
  132. chan->in += count;
  133. chan->in_len -= count;
  134. /* Finished? Re-register buffer so Host will use it again. */
  135. if (chan->in_len == 0)
  136. add_inbuf(chan);
  137. P9_DPRINTK(P9_DEBUG_TRANS, "9p debug: virtio finished read (%d)\n",
  138. count);
  139. return count;
  140. }
  141. /* The poll function is used by 9p transports to determine if there
  142. * is there is activity available on a particular channel. In our case
  143. * we use it to wait for a callback from the input routines.
  144. */
  145. static unsigned int
  146. p9_virtio_poll(struct p9_trans *trans, struct poll_table_struct *pt)
  147. {
  148. struct virtio_chan *chan = (struct virtio_chan *)trans->priv;
  149. struct virtqueue *in_vq = chan->in_vq;
  150. int ret = POLLOUT; /* we can always handle more output */
  151. poll_wait(NULL, &chan->wq, pt);
  152. /* No buffer? Try to get one. */
  153. if (!chan->in_len)
  154. chan->in = in_vq->vq_ops->get_buf(in_vq, &chan->in_len);
  155. if (chan->in_len)
  156. ret |= POLLIN;
  157. return ret;
  158. }
  159. static void p9_virtio_close(struct p9_trans *trans)
  160. {
  161. struct virtio_chan *chan = trans->priv;
  162. down(&virtio_9p_lock);
  163. chan->inuse = false;
  164. up(&virtio_9p_lock);
  165. kfree(trans);
  166. }
  167. static bool p9_virtio_intr(struct virtqueue *q)
  168. {
  169. struct virtio_chan *chan = q->vdev->priv;
  170. P9_DPRINTK(P9_DEBUG_TRANS, "9p poll_wakeup: %p\n", &chan->wq);
  171. wake_up_interruptible(&chan->wq);
  172. return true;
  173. }
  174. static int p9_virtio_probe(struct virtio_device *dev)
  175. {
  176. int err;
  177. struct virtio_chan *chan;
  178. int index;
  179. down(&virtio_9p_lock);
  180. index = chan_index++;
  181. chan = &channels[index];
  182. up(&virtio_9p_lock);
  183. if (chan_index > MAX_9P_CHAN) {
  184. printk(KERN_ERR "9p: virtio: Maximum channels exceeded\n");
  185. BUG();
  186. }
  187. chan->vdev = dev;
  188. /* This is the scratch page we use to receive console input */
  189. chan->inbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  190. if (!chan->inbuf) {
  191. err = -ENOMEM;
  192. goto fail;
  193. }
  194. /* Find the input queue. */
  195. dev->priv = chan;
  196. chan->in_vq = dev->config->find_vq(dev, p9_virtio_intr);
  197. if (IS_ERR(chan->in_vq)) {
  198. err = PTR_ERR(chan->in_vq);
  199. goto free;
  200. }
  201. chan->out_vq = dev->config->find_vq(dev, NULL);
  202. if (IS_ERR(chan->out_vq)) {
  203. err = PTR_ERR(chan->out_vq);
  204. goto free_in_vq;
  205. }
  206. init_waitqueue_head(&chan->wq);
  207. /* Register the input buffer the first time. */
  208. add_inbuf(chan);
  209. chan->inuse = false;
  210. chan->initialized = true;
  211. return 0;
  212. free_in_vq:
  213. dev->config->del_vq(chan->in_vq);
  214. free:
  215. kfree(chan->inbuf);
  216. fail:
  217. down(&virtio_9p_lock);
  218. chan_index--;
  219. up(&virtio_9p_lock);
  220. return err;
  221. }
  222. /* This sets up a transport channel for 9p communication. Right now
  223. * we only match the first available channel, but eventually we couldlook up
  224. * alternate channels by matching devname versus a virtio_config entry.
  225. * We use a simple reference count mechanism to ensure that only a single
  226. * mount has a channel open at a time. */
  227. static struct p9_trans *p9_virtio_create(const char *devname, char *args)
  228. {
  229. struct p9_trans *trans;
  230. int index = 0;
  231. struct virtio_chan *chan = channels;
  232. down(&virtio_9p_lock);
  233. while (index < MAX_9P_CHAN) {
  234. if (chan->initialized && !chan->inuse) {
  235. chan->inuse = true;
  236. break;
  237. } else {
  238. index++;
  239. chan = &channels[index];
  240. }
  241. }
  242. up(&virtio_9p_lock);
  243. if (index >= MAX_9P_CHAN) {
  244. printk(KERN_ERR "9p: virtio: couldn't find a free channel\n");
  245. return NULL;
  246. }
  247. trans = kmalloc(sizeof(struct p9_trans), GFP_KERNEL);
  248. if (!trans) {
  249. printk(KERN_ERR "9p: couldn't allocate transport\n");
  250. return ERR_PTR(-ENOMEM);
  251. }
  252. trans->write = p9_virtio_write;
  253. trans->read = p9_virtio_read;
  254. trans->close = p9_virtio_close;
  255. trans->poll = p9_virtio_poll;
  256. trans->priv = chan;
  257. return trans;
  258. }
  259. #define VIRTIO_ID_9P 9
  260. static struct virtio_device_id id_table[] = {
  261. { VIRTIO_ID_9P, VIRTIO_DEV_ANY_ID },
  262. { 0 },
  263. };
  264. /* The standard "struct lguest_driver": */
  265. static struct virtio_driver p9_virtio_drv = {
  266. .driver.name = KBUILD_MODNAME,
  267. .driver.owner = THIS_MODULE,
  268. .id_table = id_table,
  269. .probe = p9_virtio_probe,
  270. };
  271. static struct p9_trans_module p9_virtio_trans = {
  272. .name = "virtio",
  273. .create = p9_virtio_create,
  274. .maxsize = PAGE_SIZE,
  275. .def = 0,
  276. };
  277. /* The standard init function */
  278. static int __init p9_virtio_init(void)
  279. {
  280. int count;
  281. for (count = 0; count < MAX_9P_CHAN; count++)
  282. channels[count].initialized = false;
  283. v9fs_register_trans(&p9_virtio_trans);
  284. return register_virtio_driver(&p9_virtio_drv);
  285. }
  286. module_init(p9_virtio_init);
  287. MODULE_DEVICE_TABLE(virtio, id_table);
  288. MODULE_AUTHOR("Eric Van Hensbergen <ericvh@gmail.com>");
  289. MODULE_DESCRIPTION("Virtio 9p Transport");
  290. MODULE_LICENSE("GPL");