2 * The Guest 9p transport driver
4 * This is a block based transport driver based on the lguest block driver
9 * Copyright (C) 2007 Eric Van Hensbergen, IBM Corporation
11 * Based on virtio console driver
12 * Copyright (C) 2006, 2007 Rusty Russell, IBM Corporation
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License version 2
16 * as published by the Free Software Foundation.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to:
25 * Free Software Foundation
26 * 51 Franklin Street, Fifth Floor
27 * Boston, MA 02111-1301 USA
32 #include <linux/module.h>
33 #include <linux/net.h>
34 #include <linux/ipv6.h>
35 #include <linux/errno.h>
36 #include <linux/kernel.h>
38 #include <linux/uaccess.h>
39 #include <linux/inet.h>
40 #include <linux/idr.h>
41 #include <linux/file.h>
42 #include <net/9p/9p.h>
43 #include <linux/parser.h>
44 #include <net/9p/transport.h>
45 #include <linux/scatterlist.h>
46 #include <linux/virtio.h>
47 #include <linux/virtio_9p.h>
49 #define VIRTQUEUE_NUM 128
51 /* a single mutex to manage channel initialization and attachment */
52 static DECLARE_MUTEX(virtio_9p_lock);
53 /* global which tracks highest initialized channel */
54 static int chan_index;
56 #define P9_INIT_MAXTAG 16
58 #define REQ_STATUS_IDLE 0
59 #define REQ_STATUS_SENT 1
60 #define REQ_STATUS_RCVD 2
61 #define REQ_STATUS_FLSH 3
65 wait_queue_head_t *wq;
68 /* We keep all per-channel information in a structure.
69 * This structure is allocated within the devices dev->mem space.
70 * A pointer to the structure will get put in the transport private.
72 static struct virtio_chan {
73 bool initialized; /* channel is initialized */
74 bool inuse; /* channel is in use */
78 struct virtio_device *vdev;
81 struct p9_idpool *tagpool;
82 struct p9_req_t *reqs;
85 /* Scatterlist: can be too big for stack. */
86 struct scatterlist sg[VIRTQUEUE_NUM];
87 } channels[MAX_9P_CHAN];
89 /* Lookup requests by tag */
90 static struct p9_req_t *p9_lookup_tag(struct virtio_chan *c, u16 tag)
92 /* This looks up the original request by tag so we know which
93 * buffer to read the data into */
96 while (tag >= c->max_tag) {
97 int old_max = c->max_tag;
103 c->max_tag = P9_INIT_MAXTAG;
105 c->reqs = krealloc(c->reqs, sizeof(struct p9_req_t)*c->max_tag,
108 printk(KERN_ERR "Couldn't grow tag array\n");
111 for (count = old_max; count < c->max_tag; count++) {
112 c->reqs[count].status = REQ_STATUS_IDLE;
113 c->reqs[count].wq = kmalloc(sizeof(wait_queue_t),
115 if (!c->reqs[count].wq) {
116 printk(KERN_ERR "Couldn't grow tag array\n");
119 init_waitqueue_head(c->reqs[count].wq);
123 return &c->reqs[tag];
127 /* How many bytes left in this page. */
128 static unsigned int rest_of_page(void *data)
130 return PAGE_SIZE - ((unsigned long)data % PAGE_SIZE);
133 static int p9_virtio_write(struct p9_trans *trans, void *buf, int count)
135 /* Only use the rpc mechanism for now */
139 static int p9_virtio_read(struct p9_trans *trans, void *buf, int count)
141 /* Only use the rpc mechanism for now */
145 /* The poll function is used by 9p transports to determine if there
146 * is there is activity available on a particular channel. In our case
147 * we use it to wait for a callback from the input routines.
150 p9_virtio_poll(struct p9_trans *trans, struct poll_table_struct *pt)
152 /* Only use the rpc mechanism for now */
156 static void p9_virtio_close(struct p9_trans *trans)
158 struct virtio_chan *chan = trans->priv;
162 spin_lock_irqsave(&chan->lock, flags);
163 p9_idpool_destroy(chan->tagpool);
164 for (count = 0; count < chan->max_tag; count++)
165 kfree(chan->reqs[count].wq);
168 spin_unlock_irqrestore(&chan->lock, flags);
170 down(&virtio_9p_lock);
177 static void req_done(struct virtqueue *vq)
179 struct virtio_chan *chan = vq->vdev->priv;
183 struct p9_req_t *req;
185 spin_lock_irqsave(&chan->lock, flags);
186 while ((rc = chan->vq->vq_ops->get_buf(chan->vq, &len)) != NULL) {
187 req = p9_lookup_tag(chan, rc->tag);
188 req->status = REQ_STATUS_RCVD;
191 /* In case queue is stopped waiting for more buffers. */
192 spin_unlock_irqrestore(&chan->lock, flags);
196 pack_sg_list(struct scatterlist *sg, int start, int limit, char *data,
203 s = rest_of_page(data);
206 sg_set_buf(&sg[index++], data, s);
217 p9_virtio_rpc(struct p9_trans *t, struct p9_fcall *tc, struct p9_fcall **rc,
222 struct virtio_chan *chan = t->priv;
224 struct p9_req_t *req;
228 *rc = kmalloc(sizeof(struct p9_fcall) + msize, GFP_KERNEL);
233 rdata = (char *)*rc+sizeof(struct p9_fcall);
236 if (tc->id != P9_TVERSION) {
237 n = p9_idpool_get(chan->tagpool);
242 spin_lock_irqsave(&chan->lock, flags);
243 req = p9_lookup_tag(chan, n);
244 spin_unlock_irqrestore(&chan->lock, flags);
248 P9_DPRINTK(P9_DEBUG_TRANS, "9p debug: virtio rpc tag %d\n", n);
250 out = pack_sg_list(chan->sg, 0, VIRTQUEUE_NUM, tc->sdata, tc->size);
251 in = pack_sg_list(chan->sg, out, VIRTQUEUE_NUM-out, rdata, msize);
253 req->status = REQ_STATUS_SENT;
255 if (chan->vq->vq_ops->add_buf(chan->vq, chan->sg, out, in, tc)) {
256 P9_DPRINTK(P9_DEBUG_TRANS,
257 "9p debug: virtio rpc add_buf returned failure");
261 chan->vq->vq_ops->kick(chan->vq);
263 wait_event(*req->wq, req->status == REQ_STATUS_RCVD);
265 size = le32_to_cpu(*(__le32 *) rdata);
267 err = p9_deserialize_fcall(rdata, size, *rc, dotu);
269 P9_DPRINTK(P9_DEBUG_TRANS,
270 "9p debug: virtio rpc deserialize returned %d\n", err);
274 #ifdef CONFIG_NET_9P_DEBUG
275 if ((p9_debug_level&P9_DEBUG_FCALL) == P9_DEBUG_FCALL) {
278 p9_printfcall(buf, sizeof(buf), *rc, dotu);
279 printk(KERN_NOTICE ">>> %p %s\n", t, buf);
283 if (n != P9_NOTAG && p9_idpool_check(n, chan->tagpool))
284 p9_idpool_put(n, chan->tagpool);
286 req->status = REQ_STATUS_IDLE;
291 static int p9_virtio_probe(struct virtio_device *vdev)
294 struct virtio_chan *chan;
297 down(&virtio_9p_lock);
298 index = chan_index++;
299 chan = &channels[index];
302 if (chan_index > MAX_9P_CHAN) {
303 printk(KERN_ERR "9p: virtio: Maximum channels exceeded\n");
311 /* We expect one virtqueue, for requests. */
312 chan->vq = vdev->config->find_vq(vdev, 0, req_done);
313 if (IS_ERR(chan->vq)) {
314 err = PTR_ERR(chan->vq);
317 chan->vq->vdev->priv = chan;
318 spin_lock_init(&chan->lock);
320 sg_init_table(chan->sg, VIRTQUEUE_NUM);
323 chan->initialized = true;
327 vdev->config->del_vq(chan->vq);
329 down(&virtio_9p_lock);
335 /* This sets up a transport channel for 9p communication. Right now
336 * we only match the first available channel, but eventually we couldlook up
337 * alternate channels by matching devname versus a virtio_config entry.
338 * We use a simple reference count mechanism to ensure that only a single
339 * mount has a channel open at a time. */
340 static struct p9_trans *p9_virtio_create(const char *devname, char *args)
342 struct p9_trans *trans;
343 struct virtio_chan *chan = channels;
346 down(&virtio_9p_lock);
347 while (index < MAX_9P_CHAN) {
348 if (chan->initialized && !chan->inuse) {
353 chan = &channels[index];
358 if (index >= MAX_9P_CHAN) {
359 printk(KERN_ERR "9p: no channels available\n");
360 return ERR_PTR(-ENODEV);
363 chan->tagpool = p9_idpool_create();
364 if (IS_ERR(chan->tagpool)) {
365 printk(KERN_ERR "9p: couldn't allocate tagpool\n");
366 return ERR_PTR(-ENOMEM);
368 p9_idpool_get(chan->tagpool); /* reserve tag 0 */
372 trans = kmalloc(sizeof(struct p9_trans), GFP_KERNEL);
374 printk(KERN_ERR "9p: couldn't allocate transport\n");
375 return ERR_PTR(-ENOMEM);
378 trans->write = p9_virtio_write;
379 trans->read = p9_virtio_read;
380 trans->close = p9_virtio_close;
381 trans->poll = p9_virtio_poll;
382 trans->rpc = p9_virtio_rpc;
388 static void p9_virtio_remove(struct virtio_device *vdev)
390 struct virtio_chan *chan = vdev->priv;
394 if (chan->initialized) {
395 vdev->config->del_vq(chan->vq);
396 chan->initialized = false;
400 #define VIRTIO_ID_9P 9
402 static struct virtio_device_id id_table[] = {
403 { VIRTIO_ID_9P, VIRTIO_DEV_ANY_ID },
407 /* The standard "struct lguest_driver": */
408 static struct virtio_driver p9_virtio_drv = {
409 .driver.name = KBUILD_MODNAME,
410 .driver.owner = THIS_MODULE,
411 .id_table = id_table,
412 .probe = p9_virtio_probe,
413 .remove = p9_virtio_remove,
416 static struct p9_trans_module p9_virtio_trans = {
418 .create = p9_virtio_create,
419 .maxsize = PAGE_SIZE*16,
423 /* The standard init function */
424 static int __init p9_virtio_init(void)
428 for (count = 0; count < MAX_9P_CHAN; count++)
429 channels[count].initialized = false;
431 v9fs_register_trans(&p9_virtio_trans);
432 return register_virtio_driver(&p9_virtio_drv);
435 static void __exit p9_virtio_cleanup(void)
437 unregister_virtio_driver(&p9_virtio_drv);
440 module_init(p9_virtio_init);
441 module_exit(p9_virtio_cleanup);
443 MODULE_DEVICE_TABLE(virtio, id_table);
444 MODULE_AUTHOR("Eric Van Hensbergen <ericvh@gmail.com>");
445 MODULE_DESCRIPTION("Virtio 9p Transport");
446 MODULE_LICENSE("GPL");