trans_virtio.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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_ids.h>
  46. #include <linux/virtio_9p.h>
  47. #define VIRTQUEUE_NUM 128
  48. /* a single mutex to manage channel initialization and attachment */
  49. static DEFINE_MUTEX(virtio_9p_lock);
  50. /* global which tracks highest initialized channel */
  51. static int chan_index;
  52. /**
  53. * struct virtio_chan - per-instance transport information
  54. * @initialized: whether the channel is initialized
  55. * @inuse: whether the channel is in use
  56. * @lock: protects multiple elements within this structure
  57. * @client: client instance
  58. * @vdev: virtio dev associated with this channel
  59. * @vq: virtio queue associated with this channel
  60. * @sg: scatter gather list which is used to pack a request (protected?)
  61. *
  62. * We keep all per-channel information in a structure.
  63. * This structure is allocated within the devices dev->mem space.
  64. * A pointer to the structure will get put in the transport private.
  65. *
  66. */
  67. static struct virtio_chan {
  68. bool initialized;
  69. bool inuse;
  70. spinlock_t lock;
  71. struct p9_client *client;
  72. struct virtio_device *vdev;
  73. struct virtqueue *vq;
  74. /* Scatterlist: can be too big for stack. */
  75. struct scatterlist sg[VIRTQUEUE_NUM];
  76. } channels[MAX_9P_CHAN];
  77. /* How many bytes left in this page. */
  78. static unsigned int rest_of_page(void *data)
  79. {
  80. return PAGE_SIZE - ((unsigned long)data % PAGE_SIZE);
  81. }
  82. /**
  83. * p9_virtio_close - reclaim resources of a channel
  84. * @client: client instance
  85. *
  86. * This reclaims a channel by freeing its resources and
  87. * reseting its inuse flag.
  88. *
  89. */
  90. static void p9_virtio_close(struct p9_client *client)
  91. {
  92. struct virtio_chan *chan = client->trans;
  93. mutex_lock(&virtio_9p_lock);
  94. chan->inuse = false;
  95. mutex_unlock(&virtio_9p_lock);
  96. }
  97. /**
  98. * req_done - callback which signals activity from the server
  99. * @vq: virtio queue activity was received on
  100. *
  101. * This notifies us that the server has triggered some activity
  102. * on the virtio channel - most likely a response to request we
  103. * sent. Figure out which requests now have responses and wake up
  104. * those threads.
  105. *
  106. * Bugs: could do with some additional sanity checking, but appears to work.
  107. *
  108. */
  109. static void req_done(struct virtqueue *vq)
  110. {
  111. struct virtio_chan *chan = vq->vdev->priv;
  112. struct p9_fcall *rc;
  113. unsigned int len;
  114. struct p9_req_t *req;
  115. P9_DPRINTK(P9_DEBUG_TRANS, ": request done\n");
  116. while ((rc = chan->vq->vq_ops->get_buf(chan->vq, &len)) != NULL) {
  117. P9_DPRINTK(P9_DEBUG_TRANS, ": rc %p\n", rc);
  118. P9_DPRINTK(P9_DEBUG_TRANS, ": lookup tag %d\n", rc->tag);
  119. req = p9_tag_lookup(chan->client, rc->tag);
  120. req->status = REQ_STATUS_RCVD;
  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. * @client: client instance issuing the request
  162. * @req: request to be issued
  163. *
  164. */
  165. static int
  166. p9_virtio_request(struct p9_client *client, struct p9_req_t *req)
  167. {
  168. int in, out;
  169. struct virtio_chan *chan = client->trans;
  170. char *rdata = (char *)req->rc+sizeof(struct p9_fcall);
  171. P9_DPRINTK(P9_DEBUG_TRANS, "9p debug: virtio request\n");
  172. out = pack_sg_list(chan->sg, 0, VIRTQUEUE_NUM, req->tc->sdata,
  173. req->tc->size);
  174. in = pack_sg_list(chan->sg, out, VIRTQUEUE_NUM-out, rdata,
  175. client->msize);
  176. req->status = REQ_STATUS_SENT;
  177. if (chan->vq->vq_ops->add_buf(chan->vq, chan->sg, out, in, req->tc) < 0) {
  178. P9_DPRINTK(P9_DEBUG_TRANS,
  179. "9p debug: virtio rpc add_buf returned failure");
  180. return -EIO;
  181. }
  182. chan->vq->vq_ops->kick(chan->vq);
  183. P9_DPRINTK(P9_DEBUG_TRANS, "9p debug: virtio request kicked\n");
  184. return 0;
  185. }
  186. /**
  187. * p9_virtio_probe - probe for existence of 9P virtio channels
  188. * @vdev: virtio device to probe
  189. *
  190. * This probes for existing virtio channels. At present only
  191. * a single channel is in use, so in the future more work may need
  192. * to be done here.
  193. *
  194. */
  195. static int p9_virtio_probe(struct virtio_device *vdev)
  196. {
  197. int err;
  198. struct virtio_chan *chan;
  199. int index;
  200. mutex_lock(&virtio_9p_lock);
  201. index = chan_index++;
  202. chan = &channels[index];
  203. mutex_unlock(&virtio_9p_lock);
  204. if (chan_index > MAX_9P_CHAN) {
  205. printk(KERN_ERR "9p: virtio: Maximum channels exceeded\n");
  206. BUG();
  207. err = -ENOMEM;
  208. goto fail;
  209. }
  210. chan->vdev = vdev;
  211. /* We expect one virtqueue, for requests. */
  212. chan->vq = virtio_find_single_vq(vdev, req_done, "requests");
  213. if (IS_ERR(chan->vq)) {
  214. err = PTR_ERR(chan->vq);
  215. goto out_free_vq;
  216. }
  217. chan->vq->vdev->priv = chan;
  218. spin_lock_init(&chan->lock);
  219. sg_init_table(chan->sg, VIRTQUEUE_NUM);
  220. chan->inuse = false;
  221. chan->initialized = true;
  222. return 0;
  223. out_free_vq:
  224. vdev->config->del_vqs(vdev);
  225. fail:
  226. mutex_lock(&virtio_9p_lock);
  227. chan_index--;
  228. mutex_unlock(&virtio_9p_lock);
  229. return err;
  230. }
  231. /**
  232. * p9_virtio_create - allocate a new virtio channel
  233. * @client: client instance invoking this transport
  234. * @devname: string identifying the channel to connect to (unused)
  235. * @args: args passed from sys_mount() for per-transport options (unused)
  236. *
  237. * This sets up a transport channel for 9p communication. Right now
  238. * we only match the first available channel, but eventually we couldlook up
  239. * alternate channels by matching devname versus a virtio_config entry.
  240. * We use a simple reference count mechanism to ensure that only a single
  241. * mount has a channel open at a time.
  242. *
  243. * Bugs: doesn't allow identification of a specific channel
  244. * to allocate, channels are allocated sequentially. This was
  245. * a pragmatic decision to get things rolling, but ideally some
  246. * way of identifying the channel to attach to would be nice
  247. * if we are going to support multiple channels.
  248. *
  249. */
  250. static int
  251. p9_virtio_create(struct p9_client *client, const char *devname, char *args)
  252. {
  253. struct virtio_chan *chan = channels;
  254. int index = 0;
  255. mutex_lock(&virtio_9p_lock);
  256. while (index < MAX_9P_CHAN) {
  257. if (chan->initialized && !chan->inuse) {
  258. chan->inuse = true;
  259. break;
  260. } else {
  261. index++;
  262. chan = &channels[index];
  263. }
  264. }
  265. mutex_unlock(&virtio_9p_lock);
  266. if (index >= MAX_9P_CHAN) {
  267. printk(KERN_ERR "9p: no channels available\n");
  268. return -ENODEV;
  269. }
  270. client->trans = (void *)chan;
  271. chan->client = client;
  272. return 0;
  273. }
  274. /**
  275. * p9_virtio_remove - clean up resources associated with a virtio device
  276. * @vdev: virtio device to remove
  277. *
  278. */
  279. static void p9_virtio_remove(struct virtio_device *vdev)
  280. {
  281. struct virtio_chan *chan = vdev->priv;
  282. BUG_ON(chan->inuse);
  283. if (chan->initialized) {
  284. vdev->config->del_vqs(vdev);
  285. chan->initialized = false;
  286. }
  287. }
  288. static struct virtio_device_id id_table[] = {
  289. { VIRTIO_ID_9P, VIRTIO_DEV_ANY_ID },
  290. { 0 },
  291. };
  292. /* The standard "struct lguest_driver": */
  293. static struct virtio_driver p9_virtio_drv = {
  294. .driver.name = KBUILD_MODNAME,
  295. .driver.owner = THIS_MODULE,
  296. .id_table = id_table,
  297. .probe = p9_virtio_probe,
  298. .remove = p9_virtio_remove,
  299. };
  300. static struct p9_trans_module p9_virtio_trans = {
  301. .name = "virtio",
  302. .create = p9_virtio_create,
  303. .close = p9_virtio_close,
  304. .request = p9_virtio_request,
  305. .cancel = p9_virtio_cancel,
  306. .maxsize = PAGE_SIZE*16,
  307. .def = 0,
  308. .owner = THIS_MODULE,
  309. };
  310. /* The standard init function */
  311. static int __init p9_virtio_init(void)
  312. {
  313. int count;
  314. for (count = 0; count < MAX_9P_CHAN; count++)
  315. channels[count].initialized = false;
  316. v9fs_register_trans(&p9_virtio_trans);
  317. return register_virtio_driver(&p9_virtio_drv);
  318. }
  319. static void __exit p9_virtio_cleanup(void)
  320. {
  321. unregister_virtio_driver(&p9_virtio_drv);
  322. v9fs_unregister_trans(&p9_virtio_trans);
  323. }
  324. module_init(p9_virtio_init);
  325. module_exit(p9_virtio_cleanup);
  326. MODULE_DEVICE_TABLE(virtio, id_table);
  327. MODULE_AUTHOR("Eric Van Hensbergen <ericvh@gmail.com>");
  328. MODULE_DESCRIPTION("Virtio 9p Transport");
  329. MODULE_LICENSE("GPL");