virtio: explicit enable_cb/disable_cb rather than callback return.
[safe/jmp/linux-2.6] / net / 9p / trans_virtio.c
1 /*
2  * The Guest 9p transport driver
3  *
4  * This is a trivial pipe-based transport driver based on the lguest console
5  * code: we use lguest's DMA mechanism to send bytes out, and register a
6  * DMA buffer to receive bytes in.  It is assumed to be present and available
7  * from the very beginning of boot.
8  *
9  * This may be have been done by just instaniating another HVC console,
10  * but HVC's blocksize of 16 bytes is annoying and painful to performance.
11  *
12  * A more efficient transport could be built based on the virtio block driver
13  * but it requires some changes in the 9p transport model (which are in
14  * progress)
15  *
16  */
17 /*
18  *  Copyright (C) 2007 Eric Van Hensbergen, IBM Corporation
19  *
20  *  Based on virtio console driver
21  *  Copyright (C) 2006, 2007 Rusty Russell, IBM Corporation
22  *
23  *  This program is free software; you can redistribute it and/or modify
24  *  it under the terms of the GNU General Public License version 2
25  *  as published by the Free Software Foundation.
26  *
27  *  This program is distributed in the hope that it will be useful,
28  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
29  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30  *  GNU General Public License for more details.
31  *
32  *  You should have received a copy of the GNU General Public License
33  *  along with this program; if not, write to:
34  *  Free Software Foundation
35  *  51 Franklin Street, Fifth Floor
36  *  Boston, MA  02111-1301  USA
37  *
38  */
39
40 #include <linux/in.h>
41 #include <linux/module.h>
42 #include <linux/net.h>
43 #include <linux/ipv6.h>
44 #include <linux/errno.h>
45 #include <linux/kernel.h>
46 #include <linux/un.h>
47 #include <linux/uaccess.h>
48 #include <linux/inet.h>
49 #include <linux/idr.h>
50 #include <linux/file.h>
51 #include <net/9p/9p.h>
52 #include <linux/parser.h>
53 #include <net/9p/transport.h>
54 #include <linux/scatterlist.h>
55 #include <linux/virtio.h>
56 #include <linux/virtio_9p.h>
57
58 /* a single mutex to manage channel initialization and attachment */
59 static DECLARE_MUTEX(virtio_9p_lock);
60 /* global which tracks highest initialized channel */
61 static int chan_index;
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 static struct virtio_chan {
68         bool initialized;               /* channel is initialized */
69         bool inuse;                     /* channel is in use */
70
71         struct virtqueue *in_vq, *out_vq;
72         struct virtio_device *vdev;
73
74         /* This is our input buffer, and how much data is left in it. */
75         unsigned int in_len;
76         char *in, *inbuf;
77
78         wait_queue_head_t wq;           /* waitq for buffer */
79 } channels[MAX_9P_CHAN];
80
81 /* How many bytes left in this page. */
82 static unsigned int rest_of_page(void *data)
83 {
84         return PAGE_SIZE - ((unsigned long)data % PAGE_SIZE);
85 }
86
87 static int p9_virtio_write(struct p9_trans *trans, void *buf, int count)
88 {
89         struct virtio_chan *chan = (struct virtio_chan *) trans->priv;
90         struct virtqueue *out_vq = chan->out_vq;
91         struct scatterlist sg[1];
92         unsigned int len;
93
94         P9_DPRINTK(P9_DEBUG_TRANS, "9p debug: virtio write (%d)\n", count);
95
96         /* keep it simple - make sure we don't overflow a page */
97         if (rest_of_page(buf) < count)
98                 count = rest_of_page(buf);
99
100         sg_init_one(sg, buf, count);
101
102         /* add_buf wants a token to identify this buffer: we hand it any
103          * non-NULL pointer, since there's only ever one buffer. */
104         if (out_vq->vq_ops->add_buf(out_vq, sg, 1, 0, (void *)1) == 0) {
105                 /* Tell Host to go! */
106                 out_vq->vq_ops->kick(out_vq);
107                 /* Chill out until it's done with the buffer. */
108                 while (!out_vq->vq_ops->get_buf(out_vq, &len))
109                         cpu_relax();
110         }
111
112         P9_DPRINTK(P9_DEBUG_TRANS, "9p debug: virtio wrote (%d)\n", count);
113
114         /* We're expected to return the amount of data we wrote: all of it. */
115         return count;
116 }
117
118 /* Create a scatter-gather list representing our input buffer and put it in the
119  * queue. */
120 static void add_inbuf(struct virtio_chan *chan)
121 {
122         struct scatterlist sg[1];
123
124         sg_init_one(sg, chan->inbuf, PAGE_SIZE);
125
126         /* We should always be able to add one buffer to an empty queue. */
127         if (chan->in_vq->vq_ops->add_buf(chan->in_vq, sg, 0, 1, chan->inbuf))
128                 BUG();
129         chan->in_vq->vq_ops->kick(chan->in_vq);
130 }
131
132 static int p9_virtio_read(struct p9_trans *trans, void *buf, int count)
133 {
134         struct virtio_chan *chan = (struct virtio_chan *) trans->priv;
135         struct virtqueue *in_vq = chan->in_vq;
136
137         P9_DPRINTK(P9_DEBUG_TRANS, "9p debug: virtio read (%d)\n", count);
138
139         /* If we don't have an input queue yet, we can't get input. */
140         BUG_ON(!in_vq);
141
142         /* No buffer?  Try to get one. */
143         if (!chan->in_len) {
144                 chan->in = in_vq->vq_ops->get_buf(in_vq, &chan->in_len);
145                 if (!chan->in)
146                         return 0;
147         }
148
149         /* You want more than we have to give?  Well, try wanting less! */
150         if (chan->in_len < count)
151                 count = chan->in_len;
152
153         /* Copy across to their buffer and increment offset. */
154         memcpy(buf, chan->in, count);
155         chan->in += count;
156         chan->in_len -= count;
157
158         /* Finished?  Re-register buffer so Host will use it again. */
159         if (chan->in_len == 0)
160                 add_inbuf(chan);
161
162         P9_DPRINTK(P9_DEBUG_TRANS, "9p debug: virtio finished read (%d)\n",
163                                                                         count);
164
165         return count;
166 }
167
168 /* The poll function is used by 9p transports to determine if there
169  * is there is activity available on a particular channel.  In our case
170  * we use it to wait for a callback from the input routines.
171  */
172 static unsigned int
173 p9_virtio_poll(struct p9_trans *trans, struct poll_table_struct *pt)
174 {
175         struct virtio_chan *chan = (struct virtio_chan *)trans->priv;
176         struct virtqueue *in_vq = chan->in_vq;
177         int ret = POLLOUT; /* we can always handle more output */
178
179         poll_wait(NULL, &chan->wq, pt);
180
181         /* No buffer?  Try to get one. */
182         if (!chan->in_len)
183                 chan->in = in_vq->vq_ops->get_buf(in_vq, &chan->in_len);
184
185         if (chan->in_len)
186                 ret |= POLLIN;
187
188         return ret;
189 }
190
191 static void p9_virtio_close(struct p9_trans *trans)
192 {
193         struct virtio_chan *chan = trans->priv;
194
195         down(&virtio_9p_lock);
196         chan->inuse = false;
197         up(&virtio_9p_lock);
198
199         kfree(trans);
200 }
201
202 static void p9_virtio_intr(struct virtqueue *q)
203 {
204         struct virtio_chan *chan = q->vdev->priv;
205
206         P9_DPRINTK(P9_DEBUG_TRANS, "9p poll_wakeup: %p\n", &chan->wq);
207         wake_up_interruptible(&chan->wq);
208 }
209
210 static int p9_virtio_probe(struct virtio_device *dev)
211 {
212         int err;
213         struct virtio_chan *chan;
214         int index;
215
216         down(&virtio_9p_lock);
217         index = chan_index++;
218         chan = &channels[index];
219         up(&virtio_9p_lock);
220
221         if (chan_index > MAX_9P_CHAN) {
222                 printk(KERN_ERR "9p: virtio: Maximum channels exceeded\n");
223                 BUG();
224         }
225
226         chan->vdev = dev;
227
228         /* This is the scratch page we use to receive console input */
229         chan->inbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
230         if (!chan->inbuf) {
231                 err = -ENOMEM;
232                 goto fail;
233         }
234
235         /* Find the input queue. */
236         dev->priv = chan;
237         chan->in_vq = dev->config->find_vq(dev, 0, p9_virtio_intr);
238         if (IS_ERR(chan->in_vq)) {
239                 err = PTR_ERR(chan->in_vq);
240                 goto free;
241         }
242
243         chan->out_vq = dev->config->find_vq(dev, 1, NULL);
244         if (IS_ERR(chan->out_vq)) {
245                 err = PTR_ERR(chan->out_vq);
246                 goto free_in_vq;
247         }
248
249         init_waitqueue_head(&chan->wq);
250
251         /* Register the input buffer the first time. */
252         add_inbuf(chan);
253         chan->inuse = false;
254         chan->initialized = true;
255
256         return 0;
257
258 free_in_vq:
259         dev->config->del_vq(chan->in_vq);
260 free:
261         kfree(chan->inbuf);
262 fail:
263         down(&virtio_9p_lock);
264         chan_index--;
265         up(&virtio_9p_lock);
266         return err;
267 }
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 static struct p9_trans *p9_virtio_create(const char *devname, char *args)
275 {
276         struct p9_trans *trans;
277         int index = 0;
278         struct virtio_chan *chan = channels;
279
280         down(&virtio_9p_lock);
281         while (index < MAX_9P_CHAN) {
282                 if (chan->initialized && !chan->inuse) {
283                         chan->inuse = true;
284                         break;
285                 } else {
286                         index++;
287                         chan = &channels[index];
288                 }
289         }
290         up(&virtio_9p_lock);
291
292         if (index >= MAX_9P_CHAN) {
293                 printk(KERN_ERR "9p: virtio: couldn't find a free channel\n");
294                 return NULL;
295         }
296
297         trans = kmalloc(sizeof(struct p9_trans), GFP_KERNEL);
298         if (!trans) {
299                 printk(KERN_ERR "9p: couldn't allocate transport\n");
300                 return ERR_PTR(-ENOMEM);
301         }
302
303         trans->write = p9_virtio_write;
304         trans->read = p9_virtio_read;
305         trans->close = p9_virtio_close;
306         trans->poll = p9_virtio_poll;
307         trans->priv = chan;
308
309         return trans;
310 }
311
312 #define VIRTIO_ID_9P 9
313
314 static struct virtio_device_id id_table[] = {
315         { VIRTIO_ID_9P, VIRTIO_DEV_ANY_ID },
316         { 0 },
317 };
318
319 /* The standard "struct lguest_driver": */
320 static struct virtio_driver p9_virtio_drv = {
321         .driver.name =  KBUILD_MODNAME,
322         .driver.owner = THIS_MODULE,
323         .id_table =     id_table,
324         .probe =        p9_virtio_probe,
325 };
326
327 static struct p9_trans_module p9_virtio_trans = {
328         .name = "virtio",
329         .create = p9_virtio_create,
330         .maxsize = PAGE_SIZE,
331         .def = 0,
332 };
333
334 /* The standard init function */
335 static int __init p9_virtio_init(void)
336 {
337         int count;
338
339         for (count = 0; count < MAX_9P_CHAN; count++)
340                 channels[count].initialized = false;
341
342         v9fs_register_trans(&p9_virtio_trans);
343         return register_virtio_driver(&p9_virtio_drv);
344 }
345
346 module_init(p9_virtio_init);
347
348 MODULE_DEVICE_TABLE(virtio, id_table);
349 MODULE_AUTHOR("Eric Van Hensbergen <ericvh@gmail.com>");
350 MODULE_DESCRIPTION("Virtio 9p Transport");
351 MODULE_LICENSE("GPL");