Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / net / 9p / trans_virtio.c
blob713783d0542c4ab4df99f24b6975c2c8756612e7
1 /*
2 * The Guest 9p transport driver
4 * This is a block based transport driver based on the lguest block driver
5 * code.
7 */
8 /*
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
31 #include <linux/in.h>
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>
37 #include <linux/un.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
63 struct p9_req_t {
64 int status;
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 */
76 spinlock_t lock;
78 struct virtio_device *vdev;
79 struct virtqueue *vq;
81 struct p9_idpool *tagpool;
82 struct p9_req_t *reqs;
83 int max_tag;
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 */
94 tag++;
96 while (tag >= c->max_tag) {
97 int old_max = c->max_tag;
98 int count;
100 if (c->max_tag)
101 c->max_tag *= 2;
102 else
103 c->max_tag = P9_INIT_MAXTAG;
105 c->reqs = krealloc(c->reqs, sizeof(struct p9_req_t)*c->max_tag,
106 GFP_ATOMIC);
107 if (!c->reqs) {
108 printk(KERN_ERR "Couldn't grow tag array\n");
109 BUG();
111 for (count = old_max; count < c->max_tag; count++) {
112 c->reqs[count].status = REQ_STATUS_IDLE;
113 <<<<<<< HEAD:net/9p/trans_virtio.c
114 c->reqs[count].wq = kmalloc(sizeof(wait_queue_t),
115 =======
116 c->reqs[count].wq = kmalloc(sizeof(wait_queue_head_t),
117 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:net/9p/trans_virtio.c
118 GFP_ATOMIC);
119 if (!c->reqs[count].wq) {
120 printk(KERN_ERR "Couldn't grow tag array\n");
121 BUG();
123 init_waitqueue_head(c->reqs[count].wq);
127 return &c->reqs[tag];
131 /* How many bytes left in this page. */
132 static unsigned int rest_of_page(void *data)
134 return PAGE_SIZE - ((unsigned long)data % PAGE_SIZE);
137 static void p9_virtio_close(struct p9_trans *trans)
139 struct virtio_chan *chan = trans->priv;
140 int count;
141 unsigned int flags;
143 spin_lock_irqsave(&chan->lock, flags);
144 p9_idpool_destroy(chan->tagpool);
145 for (count = 0; count < chan->max_tag; count++)
146 kfree(chan->reqs[count].wq);
147 kfree(chan->reqs);
148 chan->max_tag = 0;
149 spin_unlock_irqrestore(&chan->lock, flags);
151 down(&virtio_9p_lock);
152 chan->inuse = false;
153 up(&virtio_9p_lock);
155 kfree(trans);
158 static void req_done(struct virtqueue *vq)
160 struct virtio_chan *chan = vq->vdev->priv;
161 struct p9_fcall *rc;
162 unsigned int len;
163 unsigned long flags;
164 struct p9_req_t *req;
166 spin_lock_irqsave(&chan->lock, flags);
167 while ((rc = chan->vq->vq_ops->get_buf(chan->vq, &len)) != NULL) {
168 req = p9_lookup_tag(chan, rc->tag);
169 req->status = REQ_STATUS_RCVD;
170 wake_up(req->wq);
172 /* In case queue is stopped waiting for more buffers. */
173 spin_unlock_irqrestore(&chan->lock, flags);
176 static int
177 pack_sg_list(struct scatterlist *sg, int start, int limit, char *data,
178 int count)
180 int s;
181 int index = start;
183 while (count) {
184 s = rest_of_page(data);
185 if (s > count)
186 s = count;
187 sg_set_buf(&sg[index++], data, s);
188 count -= s;
189 data += s;
190 <<<<<<< HEAD:net/9p/trans_virtio.c
191 if (index > limit)
192 BUG();
193 =======
194 BUG_ON(index > limit);
195 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:net/9p/trans_virtio.c
198 return index-start;
201 static int
202 p9_virtio_rpc(struct p9_trans *t, struct p9_fcall *tc, struct p9_fcall **rc)
204 int in, out;
205 int n, err, size;
206 struct virtio_chan *chan = t->priv;
207 char *rdata;
208 struct p9_req_t *req;
209 unsigned long flags;
211 if (*rc == NULL) {
212 *rc = kmalloc(sizeof(struct p9_fcall) + t->msize, GFP_KERNEL);
213 if (!*rc)
214 return -ENOMEM;
217 rdata = (char *)*rc+sizeof(struct p9_fcall);
219 n = P9_NOTAG;
220 if (tc->id != P9_TVERSION) {
221 n = p9_idpool_get(chan->tagpool);
222 if (n < 0)
223 return -ENOMEM;
226 spin_lock_irqsave(&chan->lock, flags);
227 req = p9_lookup_tag(chan, n);
228 spin_unlock_irqrestore(&chan->lock, flags);
230 p9_set_tag(tc, n);
232 P9_DPRINTK(P9_DEBUG_TRANS, "9p debug: virtio rpc tag %d\n", n);
234 out = pack_sg_list(chan->sg, 0, VIRTQUEUE_NUM, tc->sdata, tc->size);
235 in = pack_sg_list(chan->sg, out, VIRTQUEUE_NUM-out, rdata, t->msize);
237 req->status = REQ_STATUS_SENT;
239 if (chan->vq->vq_ops->add_buf(chan->vq, chan->sg, out, in, tc)) {
240 P9_DPRINTK(P9_DEBUG_TRANS,
241 "9p debug: virtio rpc add_buf returned failure");
242 return -EIO;
245 chan->vq->vq_ops->kick(chan->vq);
247 wait_event(*req->wq, req->status == REQ_STATUS_RCVD);
249 size = le32_to_cpu(*(__le32 *) rdata);
251 err = p9_deserialize_fcall(rdata, size, *rc, t->extended);
252 if (err < 0) {
253 P9_DPRINTK(P9_DEBUG_TRANS,
254 "9p debug: virtio rpc deserialize returned %d\n", err);
255 return err;
258 #ifdef CONFIG_NET_9P_DEBUG
259 if ((p9_debug_level&P9_DEBUG_FCALL) == P9_DEBUG_FCALL) {
260 char buf[150];
262 p9_printfcall(buf, sizeof(buf), *rc, t->extended);
263 printk(KERN_NOTICE ">>> %p %s\n", t, buf);
265 #endif
267 if (n != P9_NOTAG && p9_idpool_check(n, chan->tagpool))
268 p9_idpool_put(n, chan->tagpool);
270 req->status = REQ_STATUS_IDLE;
272 return 0;
275 static int p9_virtio_probe(struct virtio_device *vdev)
277 int err;
278 struct virtio_chan *chan;
279 int index;
281 down(&virtio_9p_lock);
282 index = chan_index++;
283 chan = &channels[index];
284 up(&virtio_9p_lock);
286 if (chan_index > MAX_9P_CHAN) {
287 printk(KERN_ERR "9p: virtio: Maximum channels exceeded\n");
288 BUG();
289 err = -ENOMEM;
290 goto fail;
293 chan->vdev = vdev;
295 /* We expect one virtqueue, for requests. */
296 chan->vq = vdev->config->find_vq(vdev, 0, req_done);
297 if (IS_ERR(chan->vq)) {
298 err = PTR_ERR(chan->vq);
299 goto out_free_vq;
301 chan->vq->vdev->priv = chan;
302 spin_lock_init(&chan->lock);
304 sg_init_table(chan->sg, VIRTQUEUE_NUM);
306 chan->inuse = false;
307 chan->initialized = true;
308 return 0;
310 out_free_vq:
311 vdev->config->del_vq(chan->vq);
312 fail:
313 down(&virtio_9p_lock);
314 chan_index--;
315 up(&virtio_9p_lock);
316 return err;
319 /* This sets up a transport channel for 9p communication. Right now
320 * we only match the first available channel, but eventually we couldlook up
321 * alternate channels by matching devname versus a virtio_config entry.
322 * We use a simple reference count mechanism to ensure that only a single
323 * mount has a channel open at a time. */
324 static struct p9_trans *
325 p9_virtio_create(const char *devname, char *args, int msize,
326 unsigned char extended)
328 struct p9_trans *trans;
329 struct virtio_chan *chan = channels;
330 int index = 0;
332 down(&virtio_9p_lock);
333 while (index < MAX_9P_CHAN) {
334 if (chan->initialized && !chan->inuse) {
335 chan->inuse = true;
336 break;
337 } else {
338 index++;
339 chan = &channels[index];
342 up(&virtio_9p_lock);
344 if (index >= MAX_9P_CHAN) {
345 printk(KERN_ERR "9p: no channels available\n");
346 return ERR_PTR(-ENODEV);
349 chan->tagpool = p9_idpool_create();
350 if (IS_ERR(chan->tagpool)) {
351 printk(KERN_ERR "9p: couldn't allocate tagpool\n");
352 return ERR_PTR(-ENOMEM);
354 p9_idpool_get(chan->tagpool); /* reserve tag 0 */
355 chan->max_tag = 0;
356 chan->reqs = NULL;
358 trans = kmalloc(sizeof(struct p9_trans), GFP_KERNEL);
359 if (!trans) {
360 printk(KERN_ERR "9p: couldn't allocate transport\n");
361 return ERR_PTR(-ENOMEM);
363 trans->extended = extended;
364 trans->msize = msize;
365 trans->close = p9_virtio_close;
366 trans->rpc = p9_virtio_rpc;
367 trans->priv = chan;
369 return trans;
372 static void p9_virtio_remove(struct virtio_device *vdev)
374 struct virtio_chan *chan = vdev->priv;
376 BUG_ON(chan->inuse);
378 if (chan->initialized) {
379 vdev->config->del_vq(chan->vq);
380 chan->initialized = false;
384 #define VIRTIO_ID_9P 9
386 static struct virtio_device_id id_table[] = {
387 { VIRTIO_ID_9P, VIRTIO_DEV_ANY_ID },
388 { 0 },
391 /* The standard "struct lguest_driver": */
392 static struct virtio_driver p9_virtio_drv = {
393 .driver.name = KBUILD_MODNAME,
394 .driver.owner = THIS_MODULE,
395 .id_table = id_table,
396 .probe = p9_virtio_probe,
397 .remove = p9_virtio_remove,
400 static struct p9_trans_module p9_virtio_trans = {
401 .name = "virtio",
402 .create = p9_virtio_create,
403 .maxsize = PAGE_SIZE*16,
404 .def = 0,
407 /* The standard init function */
408 static int __init p9_virtio_init(void)
410 int count;
412 for (count = 0; count < MAX_9P_CHAN; count++)
413 channels[count].initialized = false;
415 v9fs_register_trans(&p9_virtio_trans);
416 return register_virtio_driver(&p9_virtio_drv);
419 static void __exit p9_virtio_cleanup(void)
421 unregister_virtio_driver(&p9_virtio_drv);
424 module_init(p9_virtio_init);
425 module_exit(p9_virtio_cleanup);
427 MODULE_DEVICE_TABLE(virtio, id_table);
428 MODULE_AUTHOR("Eric Van Hensbergen <ericvh@gmail.com>");
429 MODULE_DESCRIPTION("Virtio 9p Transport");
430 MODULE_LICENSE("GPL");