trans_virtio.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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. unsigned long flags;
  116. struct p9_req_t *req;
  117. spin_lock_irqsave(&chan->lock, flags);
  118. while ((rc = chan->vq->vq_ops->get_buf(chan->vq, &len)) != NULL) {
  119. req = p9_tag_lookup(chan->client, rc->tag);
  120. req->status = REQ_STATUS_RCVD;
  121. wake_up(req->wq);
  122. }
  123. /* In case queue is stopped waiting for more buffers. */
  124. spin_unlock_irqrestore(&chan->lock, flags);
  125. }
  126. /**
  127. * pack_sg_list - pack a scatter gather list from a linear buffer
  128. * @sg: scatter/gather list to pack into
  129. * @start: which segment of the sg_list to start at
  130. * @limit: maximum segment to pack data to
  131. * @data: data to pack into scatter/gather list
  132. * @count: amount of data to pack into the scatter/gather list
  133. *
  134. * sg_lists have multiple segments of various sizes. This will pack
  135. * arbitrary data into an existing scatter gather list, segmenting the
  136. * data as necessary within constraints.
  137. *
  138. */
  139. static int
  140. pack_sg_list(struct scatterlist *sg, int start, int limit, char *data,
  141. int count)
  142. {
  143. int s;
  144. int index = start;
  145. while (count) {
  146. s = rest_of_page(data);
  147. if (s > count)
  148. s = count;
  149. sg_set_buf(&sg[index++], data, s);
  150. count -= s;
  151. data += s;
  152. BUG_ON(index > limit);
  153. }
  154. return index-start;
  155. }
  156. /**
  157. * p9_virtio_rpc - issue a request and wait for a response
  158. * @t: transport state
  159. * @tc: &p9_fcall request to transmit
  160. * @rc: &p9_fcall to put reponse into
  161. *
  162. */
  163. static int
  164. p9_virtio_rpc(struct p9_client *c, struct p9_fcall *tc, struct p9_fcall **rc)
  165. {
  166. int in, out;
  167. int n, err, size;
  168. struct virtio_chan *chan = c->trans;
  169. char *rdata;
  170. struct p9_req_t *req;
  171. unsigned long flags;
  172. if (*rc == NULL) {
  173. *rc = kmalloc(sizeof(struct p9_fcall) + c->msize, GFP_KERNEL);
  174. if (!*rc)
  175. return -ENOMEM;
  176. }
  177. rdata = (char *)*rc+sizeof(struct p9_fcall);
  178. n = P9_NOTAG;
  179. if (tc->id != P9_TVERSION) {
  180. n = p9_idpool_get(c->tagpool);
  181. if (n < 0)
  182. return -ENOMEM;
  183. }
  184. spin_lock_irqsave(&chan->lock, flags);
  185. req = p9_tag_alloc(c, n);
  186. spin_unlock_irqrestore(&chan->lock, flags);
  187. p9_set_tag(tc, n);
  188. P9_DPRINTK(P9_DEBUG_TRANS, "9p debug: virtio rpc tag %d\n", n);
  189. out = pack_sg_list(chan->sg, 0, VIRTQUEUE_NUM, tc->sdata, tc->size);
  190. in = pack_sg_list(chan->sg, out, VIRTQUEUE_NUM-out, rdata, c->msize);
  191. req->status = REQ_STATUS_SENT;
  192. if (chan->vq->vq_ops->add_buf(chan->vq, chan->sg, out, in, tc)) {
  193. P9_DPRINTK(P9_DEBUG_TRANS,
  194. "9p debug: virtio rpc add_buf returned failure");
  195. return -EIO;
  196. }
  197. chan->vq->vq_ops->kick(chan->vq);
  198. wait_event(*req->wq, req->status == REQ_STATUS_RCVD);
  199. size = le32_to_cpu(*(__le32 *) rdata);
  200. err = p9_deserialize_fcall(rdata, size, *rc, c->dotu);
  201. if (err < 0) {
  202. P9_DPRINTK(P9_DEBUG_TRANS,
  203. "9p debug: virtio rpc deserialize returned %d\n", err);
  204. return err;
  205. }
  206. #ifdef CONFIG_NET_9P_DEBUG
  207. if ((p9_debug_level&P9_DEBUG_FCALL) == P9_DEBUG_FCALL) {
  208. char buf[150];
  209. p9_printfcall(buf, sizeof(buf), *rc, c->dotu);
  210. printk(KERN_NOTICE ">>> %p %s\n", c, buf);
  211. }
  212. #endif
  213. if (n != P9_NOTAG && p9_idpool_check(n, c->tagpool))
  214. p9_idpool_put(n, c->tagpool);
  215. req->status = REQ_STATUS_IDLE;
  216. return 0;
  217. }
  218. /**
  219. * p9_virtio_probe - probe for existence of 9P virtio channels
  220. * @vdev: virtio device to probe
  221. *
  222. * This probes for existing virtio channels. At present only
  223. * a single channel is in use, so in the future more work may need
  224. * to be done here.
  225. *
  226. */
  227. static int p9_virtio_probe(struct virtio_device *vdev)
  228. {
  229. int err;
  230. struct virtio_chan *chan;
  231. int index;
  232. mutex_lock(&virtio_9p_lock);
  233. index = chan_index++;
  234. chan = &channels[index];
  235. mutex_unlock(&virtio_9p_lock);
  236. if (chan_index > MAX_9P_CHAN) {
  237. printk(KERN_ERR "9p: virtio: Maximum channels exceeded\n");
  238. BUG();
  239. err = -ENOMEM;
  240. goto fail;
  241. }
  242. chan->vdev = vdev;
  243. /* We expect one virtqueue, for requests. */
  244. chan->vq = vdev->config->find_vq(vdev, 0, req_done);
  245. if (IS_ERR(chan->vq)) {
  246. err = PTR_ERR(chan->vq);
  247. goto out_free_vq;
  248. }
  249. chan->vq->vdev->priv = chan;
  250. spin_lock_init(&chan->lock);
  251. sg_init_table(chan->sg, VIRTQUEUE_NUM);
  252. chan->inuse = false;
  253. chan->initialized = true;
  254. return 0;
  255. out_free_vq:
  256. vdev->config->del_vq(chan->vq);
  257. fail:
  258. mutex_lock(&virtio_9p_lock);
  259. chan_index--;
  260. mutex_unlock(&virtio_9p_lock);
  261. return err;
  262. }
  263. /**
  264. * p9_virtio_create - allocate a new virtio channel
  265. * @client: client instance invoking this transport
  266. * @devname: string identifying the channel to connect to (unused)
  267. * @args: args passed from sys_mount() for per-transport options (unused)
  268. *
  269. * This sets up a transport channel for 9p communication. Right now
  270. * we only match the first available channel, but eventually we couldlook up
  271. * alternate channels by matching devname versus a virtio_config entry.
  272. * We use a simple reference count mechanism to ensure that only a single
  273. * mount has a channel open at a time.
  274. *
  275. * Bugs: doesn't allow identification of a specific channel
  276. * to allocate, channels are allocated sequentially. This was
  277. * a pragmatic decision to get things rolling, but ideally some
  278. * way of identifying the channel to attach to would be nice
  279. * if we are going to support multiple channels.
  280. *
  281. */
  282. static int
  283. p9_virtio_create(struct p9_client *client, const char *devname, char *args)
  284. {
  285. struct virtio_chan *chan = channels;
  286. int index = 0;
  287. mutex_lock(&virtio_9p_lock);
  288. while (index < MAX_9P_CHAN) {
  289. if (chan->initialized && !chan->inuse) {
  290. chan->inuse = true;
  291. break;
  292. } else {
  293. index++;
  294. chan = &channels[index];
  295. }
  296. }
  297. mutex_unlock(&virtio_9p_lock);
  298. if (index >= MAX_9P_CHAN) {
  299. printk(KERN_ERR "9p: no channels available\n");
  300. return -ENODEV;
  301. }
  302. client->trans = (void *)chan;
  303. chan->client = client;
  304. return 0;
  305. }
  306. /**
  307. * p9_virtio_remove - clean up resources associated with a virtio device
  308. * @vdev: virtio device to remove
  309. *
  310. */
  311. static void p9_virtio_remove(struct virtio_device *vdev)
  312. {
  313. struct virtio_chan *chan = vdev->priv;
  314. BUG_ON(chan->inuse);
  315. if (chan->initialized) {
  316. vdev->config->del_vq(chan->vq);
  317. chan->initialized = false;
  318. }
  319. }
  320. #define VIRTIO_ID_9P 9
  321. static struct virtio_device_id id_table[] = {
  322. { VIRTIO_ID_9P, VIRTIO_DEV_ANY_ID },
  323. { 0 },
  324. };
  325. /* The standard "struct lguest_driver": */
  326. static struct virtio_driver p9_virtio_drv = {
  327. .driver.name = KBUILD_MODNAME,
  328. .driver.owner = THIS_MODULE,
  329. .id_table = id_table,
  330. .probe = p9_virtio_probe,
  331. .remove = p9_virtio_remove,
  332. };
  333. static struct p9_trans_module p9_virtio_trans = {
  334. .name = "virtio",
  335. .create = p9_virtio_create,
  336. .close = p9_virtio_close,
  337. .rpc = p9_virtio_rpc,
  338. .maxsize = PAGE_SIZE*16,
  339. .def = 0,
  340. .owner = THIS_MODULE,
  341. };
  342. /* The standard init function */
  343. static int __init p9_virtio_init(void)
  344. {
  345. int count;
  346. for (count = 0; count < MAX_9P_CHAN; count++)
  347. channels[count].initialized = false;
  348. v9fs_register_trans(&p9_virtio_trans);
  349. return register_virtio_driver(&p9_virtio_drv);
  350. }
  351. static void __exit p9_virtio_cleanup(void)
  352. {
  353. unregister_virtio_driver(&p9_virtio_drv);
  354. v9fs_unregister_trans(&p9_virtio_trans);
  355. }
  356. module_init(p9_virtio_init);
  357. module_exit(p9_virtio_cleanup);
  358. MODULE_DEVICE_TABLE(virtio, id_table);
  359. MODULE_AUTHOR("Eric Van Hensbergen <ericvh@gmail.com>");
  360. MODULE_DESCRIPTION("Virtio 9p Transport");
  361. MODULE_LICENSE("GPL");