trans_virtio.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /*
  2. * The Virtio 9p transport driver
  3. *
  4. * This is a block based transport driver based on the lguest block driver
  5. * code.
  6. *
  7. * Copyright (C) 2007, 2008 Eric Van Hensbergen, IBM Corporation
  8. *
  9. * Based on virtio console driver
  10. * Copyright (C) 2006, 2007 Rusty Russell, IBM Corporation
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License version 2
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to:
  23. * Free Software Foundation
  24. * 51 Franklin Street, Fifth Floor
  25. * Boston, MA 02111-1301 USA
  26. *
  27. */
  28. #include <linux/in.h>
  29. #include <linux/module.h>
  30. #include <linux/net.h>
  31. #include <linux/ipv6.h>
  32. #include <linux/errno.h>
  33. #include <linux/kernel.h>
  34. #include <linux/un.h>
  35. #include <linux/uaccess.h>
  36. #include <linux/inet.h>
  37. #include <linux/idr.h>
  38. #include <linux/file.h>
  39. #include <net/9p/9p.h>
  40. #include <linux/parser.h>
  41. #include <net/9p/client.h>
  42. #include <net/9p/transport.h>
  43. #include <linux/scatterlist.h>
  44. #include <linux/virtio.h>
  45. #include <linux/virtio_9p.h>
  46. #define VIRTQUEUE_NUM 128
  47. /* a single mutex to manage channel initialization and attachment */
  48. static DEFINE_MUTEX(virtio_9p_lock);
  49. /* global which tracks highest initialized channel */
  50. static int chan_index;
  51. /**
  52. * struct virtio_chan - per-instance transport information
  53. * @initialized: whether the channel is initialized
  54. * @inuse: whether the channel is in use
  55. * @lock: protects multiple elements within this structure
  56. * @vdev: virtio dev associated with this channel
  57. * @vq: virtio queue associated with this channel
  58. * @tagpool: accounting for tag ids (and request slots)
  59. * @reqs: array of request slots
  60. * @max_tag: current number of request_slots allocated
  61. * @sg: scatter gather list which is used to pack a request (protected?)
  62. *
  63. * We keep all per-channel information in a structure.
  64. * This structure is allocated within the devices dev->mem space.
  65. * A pointer to the structure will get put in the transport private.
  66. *
  67. */
  68. static struct virtio_chan {
  69. bool initialized;
  70. bool inuse;
  71. spinlock_t lock;
  72. struct p9_client *client;
  73. struct virtio_device *vdev;
  74. struct virtqueue *vq;
  75. /* Scatterlist: can be too big for stack. */
  76. struct scatterlist sg[VIRTQUEUE_NUM];
  77. } channels[MAX_9P_CHAN];
  78. /* How many bytes left in this page. */
  79. static unsigned int rest_of_page(void *data)
  80. {
  81. return PAGE_SIZE - ((unsigned long)data % PAGE_SIZE);
  82. }
  83. /**
  84. * p9_virtio_close - reclaim resources of a channel
  85. * @trans: transport state
  86. *
  87. * This reclaims a channel by freeing its resources and
  88. * reseting its inuse flag.
  89. *
  90. */
  91. static void p9_virtio_close(struct p9_client *client)
  92. {
  93. struct virtio_chan *chan = client->trans;
  94. mutex_lock(&virtio_9p_lock);
  95. chan->inuse = false;
  96. mutex_unlock(&virtio_9p_lock);
  97. }
  98. /**
  99. * req_done - callback which signals activity from the server
  100. * @vq: virtio queue activity was received on
  101. *
  102. * This notifies us that the server has triggered some activity
  103. * on the virtio channel - most likely a response to request we
  104. * sent. Figure out which requests now have responses and wake up
  105. * those threads.
  106. *
  107. * Bugs: could do with some additional sanity checking, but appears to work.
  108. *
  109. */
  110. static void req_done(struct virtqueue *vq)
  111. {
  112. struct virtio_chan *chan = vq->vdev->priv;
  113. struct p9_fcall *rc;
  114. unsigned int len;
  115. struct p9_req_t *req;
  116. P9_DPRINTK(P9_DEBUG_TRANS, ": request done\n");
  117. while ((rc = chan->vq->vq_ops->get_buf(chan->vq, &len)) != NULL) {
  118. P9_DPRINTK(P9_DEBUG_TRANS, ": rc %p\n", rc);
  119. P9_DPRINTK(P9_DEBUG_TRANS, ": lookup tag %d\n", rc->tag);
  120. req = p9_tag_lookup(chan->client, rc->tag);
  121. p9_client_cb(chan->client, req);
  122. }
  123. }
  124. /**
  125. * pack_sg_list - pack a scatter gather list from a linear buffer
  126. * @sg: scatter/gather list to pack into
  127. * @start: which segment of the sg_list to start at
  128. * @limit: maximum segment to pack data to
  129. * @data: data to pack into scatter/gather list
  130. * @count: amount of data to pack into the scatter/gather list
  131. *
  132. * sg_lists have multiple segments of various sizes. This will pack
  133. * arbitrary data into an existing scatter gather list, segmenting the
  134. * data as necessary within constraints.
  135. *
  136. */
  137. static int
  138. pack_sg_list(struct scatterlist *sg, int start, int limit, char *data,
  139. int count)
  140. {
  141. int s;
  142. int index = start;
  143. while (count) {
  144. s = rest_of_page(data);
  145. if (s > count)
  146. s = count;
  147. sg_set_buf(&sg[index++], data, s);
  148. count -= s;
  149. data += s;
  150. BUG_ON(index > limit);
  151. }
  152. return index-start;
  153. }
  154. /* We don't currently allow canceling of virtio requests */
  155. static int p9_virtio_cancel(struct p9_client *client, struct p9_req_t *req)
  156. {
  157. return 1;
  158. }
  159. /**
  160. * p9_virtio_request - issue a request
  161. * @t: transport state
  162. * @tc: &p9_fcall request to transmit
  163. * @rc: &p9_fcall to put reponse into
  164. *
  165. */
  166. static int
  167. p9_virtio_request(struct p9_client *client, struct p9_req_t *req)
  168. {
  169. int in, out;
  170. struct virtio_chan *chan = client->trans;
  171. char *rdata = (char *)req->rc+sizeof(struct p9_fcall);
  172. P9_DPRINTK(P9_DEBUG_TRANS, "9p debug: virtio request\n");
  173. out = pack_sg_list(chan->sg, 0, VIRTQUEUE_NUM, req->tc->sdata,
  174. req->tc->size);
  175. in = pack_sg_list(chan->sg, out, VIRTQUEUE_NUM-out, rdata,
  176. client->msize);
  177. req->status = REQ_STATUS_SENT;
  178. if (chan->vq->vq_ops->add_buf(chan->vq, chan->sg, out, in, req->tc)) {
  179. P9_DPRINTK(P9_DEBUG_TRANS,
  180. "9p debug: virtio rpc add_buf returned failure");
  181. return -EIO;
  182. }
  183. chan->vq->vq_ops->kick(chan->vq);
  184. P9_DPRINTK(P9_DEBUG_TRANS, "9p debug: virtio request kicked\n");
  185. return 0;
  186. }
  187. /**
  188. * p9_virtio_probe - probe for existence of 9P virtio channels
  189. * @vdev: virtio device to probe
  190. *
  191. * This probes for existing virtio channels. At present only
  192. * a single channel is in use, so in the future more work may need
  193. * to be done here.
  194. *
  195. */
  196. static int p9_virtio_probe(struct virtio_device *vdev)
  197. {
  198. int err;
  199. struct virtio_chan *chan;
  200. int index;
  201. mutex_lock(&virtio_9p_lock);
  202. index = chan_index++;
  203. chan = &channels[index];
  204. mutex_unlock(&virtio_9p_lock);
  205. if (chan_index > MAX_9P_CHAN) {
  206. printk(KERN_ERR "9p: virtio: Maximum channels exceeded\n");
  207. BUG();
  208. err = -ENOMEM;
  209. goto fail;
  210. }
  211. chan->vdev = vdev;
  212. /* We expect one virtqueue, for requests. */
  213. chan->vq = vdev->config->find_vq(vdev, 0, req_done);
  214. if (IS_ERR(chan->vq)) {
  215. err = PTR_ERR(chan->vq);
  216. goto out_free_vq;
  217. }
  218. chan->vq->vdev->priv = chan;
  219. spin_lock_init(&chan->lock);
  220. sg_init_table(chan->sg, VIRTQUEUE_NUM);
  221. chan->inuse = false;
  222. chan->initialized = true;
  223. return 0;
  224. out_free_vq:
  225. vdev->config->del_vq(chan->vq);
  226. fail:
  227. mutex_lock(&virtio_9p_lock);
  228. chan_index--;
  229. mutex_unlock(&virtio_9p_lock);
  230. return err;
  231. }
  232. /**
  233. * p9_virtio_create - allocate a new virtio channel
  234. * @client: client instance invoking this transport
  235. * @devname: string identifying the channel to connect to (unused)
  236. * @args: args passed from sys_mount() for per-transport options (unused)
  237. *
  238. * This sets up a transport channel for 9p communication. Right now
  239. * we only match the first available channel, but eventually we couldlook up
  240. * alternate channels by matching devname versus a virtio_config entry.
  241. * We use a simple reference count mechanism to ensure that only a single
  242. * mount has a channel open at a time.
  243. *
  244. * Bugs: doesn't allow identification of a specific channel
  245. * to allocate, channels are allocated sequentially. This was
  246. * a pragmatic decision to get things rolling, but ideally some
  247. * way of identifying the channel to attach to would be nice
  248. * if we are going to support multiple channels.
  249. *
  250. */
  251. static int
  252. p9_virtio_create(struct p9_client *client, const char *devname, char *args)
  253. {
  254. struct virtio_chan *chan = channels;
  255. int index = 0;
  256. mutex_lock(&virtio_9p_lock);
  257. while (index < MAX_9P_CHAN) {
  258. if (chan->initialized && !chan->inuse) {
  259. chan->inuse = true;
  260. break;
  261. } else {
  262. index++;
  263. chan = &channels[index];
  264. }
  265. }
  266. mutex_unlock(&virtio_9p_lock);
  267. if (index >= MAX_9P_CHAN) {
  268. printk(KERN_ERR "9p: no channels available\n");
  269. return -ENODEV;
  270. }
  271. client->trans = (void *)chan;
  272. chan->client = client;
  273. return 0;
  274. }
  275. /**
  276. * p9_virtio_remove - clean up resources associated with a virtio device
  277. * @vdev: virtio device to remove
  278. *
  279. */
  280. static void p9_virtio_remove(struct virtio_device *vdev)
  281. {
  282. struct virtio_chan *chan = vdev->priv;
  283. BUG_ON(chan->inuse);
  284. if (chan->initialized) {
  285. vdev->config->del_vq(chan->vq);
  286. chan->initialized = false;
  287. }
  288. }
  289. #define VIRTIO_ID_9P 9
  290. static struct virtio_device_id id_table[] = {
  291. { VIRTIO_ID_9P, VIRTIO_DEV_ANY_ID },
  292. { 0 },
  293. };
  294. /* The standard "struct lguest_driver": */
  295. static struct virtio_driver p9_virtio_drv = {
  296. .driver.name = KBUILD_MODNAME,
  297. .driver.owner = THIS_MODULE,
  298. .id_table = id_table,
  299. .probe = p9_virtio_probe,
  300. .remove = p9_virtio_remove,
  301. };
  302. static struct p9_trans_module p9_virtio_trans = {
  303. .name = "virtio",
  304. .create = p9_virtio_create,
  305. .close = p9_virtio_close,
  306. .request = p9_virtio_request,
  307. .cancel = p9_virtio_cancel,
  308. .maxsize = PAGE_SIZE*16,
  309. .def = 0,
  310. .owner = THIS_MODULE,
  311. };
  312. /* The standard init function */
  313. static int __init p9_virtio_init(void)
  314. {
  315. int count;
  316. for (count = 0; count < MAX_9P_CHAN; count++)
  317. channels[count].initialized = false;
  318. v9fs_register_trans(&p9_virtio_trans);
  319. return register_virtio_driver(&p9_virtio_drv);
  320. }
  321. static void __exit p9_virtio_cleanup(void)
  322. {
  323. unregister_virtio_driver(&p9_virtio_drv);
  324. v9fs_unregister_trans(&p9_virtio_trans);
  325. }
  326. module_init(p9_virtio_init);
  327. module_exit(p9_virtio_cleanup);
  328. MODULE_DEVICE_TABLE(virtio, id_table);
  329. MODULE_AUTHOR("Eric Van Hensbergen <ericvh@gmail.com>");
  330. MODULE_DESCRIPTION("Virtio 9p Transport");
  331. MODULE_LICENSE("GPL");