blob: 0117b9fb8480d4a51830ff0e59a591a6ffeb2105 [file] [log] [blame]
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -05001/*
2 * The Guest 9p transport driver
3 *
Eric Van Hensbergene2735b72008-02-06 19:25:58 -06004 * This is a block based transport driver based on the lguest block driver
5 * code.
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -05006 *
7 */
8/*
9 * Copyright (C) 2007 Eric Van Hensbergen, IBM Corporation
10 *
11 * Based on virtio console driver
12 * Copyright (C) 2006, 2007 Rusty Russell, IBM Corporation
13 *
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.
17 *
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.
22 *
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
28 *
29 */
30
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>
48
Eric Van Hensbergene2735b72008-02-06 19:25:58 -060049#define VIRTQUEUE_NUM 128
50
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -050051/* a single mutex to manage channel initialization and attachment */
52static DECLARE_MUTEX(virtio_9p_lock);
53/* global which tracks highest initialized channel */
54static int chan_index;
55
Eric Van Hensbergene2735b72008-02-06 19:25:58 -060056#define P9_INIT_MAXTAG 16
57
58#define REQ_STATUS_IDLE 0
59#define REQ_STATUS_SENT 1
60#define REQ_STATUS_RCVD 2
61#define REQ_STATUS_FLSH 3
62
63struct p9_req_t {
64 int status;
65 wait_queue_head_t *wq;
66};
67
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -050068/* 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.
71 */
72static struct virtio_chan {
73 bool initialized; /* channel is initialized */
74 bool inuse; /* channel is in use */
75
Eric Van Hensbergene2735b72008-02-06 19:25:58 -060076 spinlock_t lock;
77
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -050078 struct virtio_device *vdev;
Eric Van Hensbergene2735b72008-02-06 19:25:58 -060079 struct virtqueue *vq;
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -050080
Eric Van Hensbergene2735b72008-02-06 19:25:58 -060081 struct p9_idpool *tagpool;
82 struct p9_req_t *reqs;
83 int max_tag;
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -050084
Eric Van Hensbergene2735b72008-02-06 19:25:58 -060085 /* Scatterlist: can be too big for stack. */
86 struct scatterlist sg[VIRTQUEUE_NUM];
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -050087} channels[MAX_9P_CHAN];
88
Eric Van Hensbergene2735b72008-02-06 19:25:58 -060089/* Lookup requests by tag */
90static struct p9_req_t *p9_lookup_tag(struct virtio_chan *c, u16 tag)
91{
92 /* This looks up the original request by tag so we know which
93 * buffer to read the data into */
94 tag++;
95
96 while (tag >= c->max_tag) {
97 int old_max = c->max_tag;
98 int count;
99
100 if (c->max_tag)
101 c->max_tag *= 2;
102 else
103 c->max_tag = P9_INIT_MAXTAG;
104
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();
110 }
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),
114 GFP_ATOMIC);
115 if (!c->reqs[count].wq) {
116 printk(KERN_ERR "Couldn't grow tag array\n");
117 BUG();
118 }
119 init_waitqueue_head(c->reqs[count].wq);
120 }
121 }
122
123 return &c->reqs[tag];
124}
125
126
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500127/* How many bytes left in this page. */
128static unsigned int rest_of_page(void *data)
129{
130 return PAGE_SIZE - ((unsigned long)data % PAGE_SIZE);
131}
132
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500133static void p9_virtio_close(struct p9_trans *trans)
134{
135 struct virtio_chan *chan = trans->priv;
Eric Van Hensbergene2735b72008-02-06 19:25:58 -0600136 int count;
137 unsigned int flags;
138
139 spin_lock_irqsave(&chan->lock, flags);
140 p9_idpool_destroy(chan->tagpool);
141 for (count = 0; count < chan->max_tag; count++)
142 kfree(chan->reqs[count].wq);
143 kfree(chan->reqs);
144 chan->max_tag = 0;
145 spin_unlock_irqrestore(&chan->lock, flags);
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500146
147 down(&virtio_9p_lock);
148 chan->inuse = false;
149 up(&virtio_9p_lock);
150
151 kfree(trans);
152}
153
Eric Van Hensbergene2735b72008-02-06 19:25:58 -0600154static void req_done(struct virtqueue *vq)
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500155{
Eric Van Hensbergene2735b72008-02-06 19:25:58 -0600156 struct virtio_chan *chan = vq->vdev->priv;
157 struct p9_fcall *rc;
158 unsigned int len;
159 unsigned long flags;
160 struct p9_req_t *req;
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500161
Eric Van Hensbergene2735b72008-02-06 19:25:58 -0600162 spin_lock_irqsave(&chan->lock, flags);
163 while ((rc = chan->vq->vq_ops->get_buf(chan->vq, &len)) != NULL) {
164 req = p9_lookup_tag(chan, rc->tag);
165 req->status = REQ_STATUS_RCVD;
166 wake_up(req->wq);
167 }
168 /* In case queue is stopped waiting for more buffers. */
169 spin_unlock_irqrestore(&chan->lock, flags);
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500170}
171
Eric Van Hensbergene2735b72008-02-06 19:25:58 -0600172static int
173pack_sg_list(struct scatterlist *sg, int start, int limit, char *data,
174 int count)
175{
176 int s;
177 int index = start;
178
179 while (count) {
180 s = rest_of_page(data);
181 if (s > count)
182 s = count;
183 sg_set_buf(&sg[index++], data, s);
184 count -= s;
185 data += s;
186 if (index > limit)
187 BUG();
188 }
189
190 return index-start;
191}
192
193static int
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600194p9_virtio_rpc(struct p9_trans *t, struct p9_fcall *tc, struct p9_fcall **rc)
Eric Van Hensbergene2735b72008-02-06 19:25:58 -0600195{
196 int in, out;
197 int n, err, size;
198 struct virtio_chan *chan = t->priv;
199 char *rdata;
200 struct p9_req_t *req;
201 unsigned long flags;
202
203 if (*rc == NULL) {
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600204 *rc = kmalloc(sizeof(struct p9_fcall) + t->msize, GFP_KERNEL);
Eric Van Hensbergene2735b72008-02-06 19:25:58 -0600205 if (!*rc)
206 return -ENOMEM;
207 }
208
209 rdata = (char *)*rc+sizeof(struct p9_fcall);
210
Eric Van Hensbergene2735b72008-02-06 19:25:58 -0600211 n = P9_NOTAG;
212 if (tc->id != P9_TVERSION) {
213 n = p9_idpool_get(chan->tagpool);
214 if (n < 0)
215 return -ENOMEM;
216 }
217
Eric Van Hensbergen7c7d90f2008-02-06 19:25:07 -0600218 spin_lock_irqsave(&chan->lock, flags);
Eric Van Hensbergene2735b72008-02-06 19:25:58 -0600219 req = p9_lookup_tag(chan, n);
220 spin_unlock_irqrestore(&chan->lock, flags);
221
222 p9_set_tag(tc, n);
223
224 P9_DPRINTK(P9_DEBUG_TRANS, "9p debug: virtio rpc tag %d\n", n);
225
226 out = pack_sg_list(chan->sg, 0, VIRTQUEUE_NUM, tc->sdata, tc->size);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600227 in = pack_sg_list(chan->sg, out, VIRTQUEUE_NUM-out, rdata, t->msize);
Eric Van Hensbergene2735b72008-02-06 19:25:58 -0600228
229 req->status = REQ_STATUS_SENT;
230
231 if (chan->vq->vq_ops->add_buf(chan->vq, chan->sg, out, in, tc)) {
232 P9_DPRINTK(P9_DEBUG_TRANS,
233 "9p debug: virtio rpc add_buf returned failure");
234 return -EIO;
235 }
236
237 chan->vq->vq_ops->kick(chan->vq);
238
239 wait_event(*req->wq, req->status == REQ_STATUS_RCVD);
240
241 size = le32_to_cpu(*(__le32 *) rdata);
242
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600243 err = p9_deserialize_fcall(rdata, size, *rc, t->extended);
Eric Van Hensbergene2735b72008-02-06 19:25:58 -0600244 if (err < 0) {
245 P9_DPRINTK(P9_DEBUG_TRANS,
246 "9p debug: virtio rpc deserialize returned %d\n", err);
247 return err;
248 }
249
250#ifdef CONFIG_NET_9P_DEBUG
251 if ((p9_debug_level&P9_DEBUG_FCALL) == P9_DEBUG_FCALL) {
252 char buf[150];
253
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600254 p9_printfcall(buf, sizeof(buf), *rc, t->extended);
Eric Van Hensbergene2735b72008-02-06 19:25:58 -0600255 printk(KERN_NOTICE ">>> %p %s\n", t, buf);
256 }
257#endif
258
259 if (n != P9_NOTAG && p9_idpool_check(n, chan->tagpool))
260 p9_idpool_put(n, chan->tagpool);
261
262 req->status = REQ_STATUS_IDLE;
263
264 return 0;
265}
266
267static int p9_virtio_probe(struct virtio_device *vdev)
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500268{
269 int err;
270 struct virtio_chan *chan;
271 int index;
272
273 down(&virtio_9p_lock);
274 index = chan_index++;
275 chan = &channels[index];
276 up(&virtio_9p_lock);
277
278 if (chan_index > MAX_9P_CHAN) {
279 printk(KERN_ERR "9p: virtio: Maximum channels exceeded\n");
280 BUG();
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500281 err = -ENOMEM;
282 goto fail;
283 }
284
Eric Van Hensbergene2735b72008-02-06 19:25:58 -0600285 chan->vdev = vdev;
286
287 /* We expect one virtqueue, for requests. */
288 chan->vq = vdev->config->find_vq(vdev, 0, req_done);
289 if (IS_ERR(chan->vq)) {
290 err = PTR_ERR(chan->vq);
291 goto out_free_vq;
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500292 }
Eric Van Hensbergene2735b72008-02-06 19:25:58 -0600293 chan->vq->vdev->priv = chan;
294 spin_lock_init(&chan->lock);
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500295
Eric Van Hensbergene2735b72008-02-06 19:25:58 -0600296 sg_init_table(chan->sg, VIRTQUEUE_NUM);
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500297
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500298 chan->inuse = false;
299 chan->initialized = true;
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500300 return 0;
301
Eric Van Hensbergene2735b72008-02-06 19:25:58 -0600302out_free_vq:
303 vdev->config->del_vq(chan->vq);
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500304fail:
305 down(&virtio_9p_lock);
306 chan_index--;
307 up(&virtio_9p_lock);
308 return err;
309}
310
311/* This sets up a transport channel for 9p communication. Right now
312 * we only match the first available channel, but eventually we couldlook up
313 * alternate channels by matching devname versus a virtio_config entry.
314 * We use a simple reference count mechanism to ensure that only a single
315 * mount has a channel open at a time. */
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600316static struct p9_trans *
317p9_virtio_create(const char *devname, char *args, int msize,
318 unsigned char extended)
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500319{
320 struct p9_trans *trans;
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500321 struct virtio_chan *chan = channels;
Eric Van Hensbergene2735b72008-02-06 19:25:58 -0600322 int index = 0;
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500323
324 down(&virtio_9p_lock);
325 while (index < MAX_9P_CHAN) {
326 if (chan->initialized && !chan->inuse) {
327 chan->inuse = true;
328 break;
329 } else {
330 index++;
331 chan = &channels[index];
332 }
333 }
334 up(&virtio_9p_lock);
335
336 if (index >= MAX_9P_CHAN) {
Eric Van Hensbergene2735b72008-02-06 19:25:58 -0600337 printk(KERN_ERR "9p: no channels available\n");
338 return ERR_PTR(-ENODEV);
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500339 }
340
Eric Van Hensbergene2735b72008-02-06 19:25:58 -0600341 chan->tagpool = p9_idpool_create();
342 if (IS_ERR(chan->tagpool)) {
343 printk(KERN_ERR "9p: couldn't allocate tagpool\n");
344 return ERR_PTR(-ENOMEM);
345 }
346 p9_idpool_get(chan->tagpool); /* reserve tag 0 */
347 chan->max_tag = 0;
348 chan->reqs = NULL;
349
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500350 trans = kmalloc(sizeof(struct p9_trans), GFP_KERNEL);
351 if (!trans) {
352 printk(KERN_ERR "9p: couldn't allocate transport\n");
353 return ERR_PTR(-ENOMEM);
354 }
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600355 trans->extended = extended;
356 trans->msize = msize;
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500357 trans->close = p9_virtio_close;
Eric Van Hensbergene2735b72008-02-06 19:25:58 -0600358 trans->rpc = p9_virtio_rpc;
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500359 trans->priv = chan;
360
361 return trans;
362}
363
Eric Van Hensbergenf3933542008-02-06 19:25:04 -0600364static void p9_virtio_remove(struct virtio_device *vdev)
365{
366 struct virtio_chan *chan = vdev->priv;
367
368 BUG_ON(chan->inuse);
369
370 if (chan->initialized) {
371 vdev->config->del_vq(chan->vq);
372 chan->initialized = false;
373 }
374}
375
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500376#define VIRTIO_ID_9P 9
377
378static struct virtio_device_id id_table[] = {
379 { VIRTIO_ID_9P, VIRTIO_DEV_ANY_ID },
380 { 0 },
381};
382
383/* The standard "struct lguest_driver": */
384static struct virtio_driver p9_virtio_drv = {
385 .driver.name = KBUILD_MODNAME,
386 .driver.owner = THIS_MODULE,
387 .id_table = id_table,
388 .probe = p9_virtio_probe,
Eric Van Hensbergenf3933542008-02-06 19:25:04 -0600389 .remove = p9_virtio_remove,
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500390};
391
392static struct p9_trans_module p9_virtio_trans = {
393 .name = "virtio",
394 .create = p9_virtio_create,
Eric Van Hensbergene2735b72008-02-06 19:25:58 -0600395 .maxsize = PAGE_SIZE*16,
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500396 .def = 0,
397};
398
399/* The standard init function */
400static int __init p9_virtio_init(void)
401{
402 int count;
403
404 for (count = 0; count < MAX_9P_CHAN; count++)
405 channels[count].initialized = false;
406
407 v9fs_register_trans(&p9_virtio_trans);
408 return register_virtio_driver(&p9_virtio_drv);
409}
410
Eric Van Hensbergenf3933542008-02-06 19:25:04 -0600411static void __exit p9_virtio_cleanup(void)
412{
413 unregister_virtio_driver(&p9_virtio_drv);
414}
415
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500416module_init(p9_virtio_init);
Eric Van Hensbergenf3933542008-02-06 19:25:04 -0600417module_exit(p9_virtio_cleanup);
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500418
419MODULE_DEVICE_TABLE(virtio, id_table);
420MODULE_AUTHOR("Eric Van Hensbergen <ericvh@gmail.com>");
421MODULE_DESCRIPTION("Virtio 9p Transport");
422MODULE_LICENSE("GPL");