xref: /openbmc/linux/net/9p/trans_virtio.c (revision e2735b7720320b68590ca2b32b78ca91213931b2)
1b530cc79SEric Van Hensbergen /*
2b530cc79SEric Van Hensbergen  * The Guest 9p transport driver
3b530cc79SEric Van Hensbergen  *
4*e2735b77SEric Van Hensbergen  * This is a block based transport driver based on the lguest block driver
5*e2735b77SEric Van Hensbergen  * code.
6b530cc79SEric Van Hensbergen  *
7b530cc79SEric Van Hensbergen  */
8b530cc79SEric Van Hensbergen /*
9b530cc79SEric Van Hensbergen  *  Copyright (C) 2007 Eric Van Hensbergen, IBM Corporation
10b530cc79SEric Van Hensbergen  *
11b530cc79SEric Van Hensbergen  *  Based on virtio console driver
12b530cc79SEric Van Hensbergen  *  Copyright (C) 2006, 2007 Rusty Russell, IBM Corporation
13b530cc79SEric Van Hensbergen  *
14b530cc79SEric Van Hensbergen  *  This program is free software; you can redistribute it and/or modify
15b530cc79SEric Van Hensbergen  *  it under the terms of the GNU General Public License version 2
16b530cc79SEric Van Hensbergen  *  as published by the Free Software Foundation.
17b530cc79SEric Van Hensbergen  *
18b530cc79SEric Van Hensbergen  *  This program is distributed in the hope that it will be useful,
19b530cc79SEric Van Hensbergen  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
20b530cc79SEric Van Hensbergen  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21b530cc79SEric Van Hensbergen  *  GNU General Public License for more details.
22b530cc79SEric Van Hensbergen  *
23b530cc79SEric Van Hensbergen  *  You should have received a copy of the GNU General Public License
24b530cc79SEric Van Hensbergen  *  along with this program; if not, write to:
25b530cc79SEric Van Hensbergen  *  Free Software Foundation
26b530cc79SEric Van Hensbergen  *  51 Franklin Street, Fifth Floor
27b530cc79SEric Van Hensbergen  *  Boston, MA  02111-1301  USA
28b530cc79SEric Van Hensbergen  *
29b530cc79SEric Van Hensbergen  */
30b530cc79SEric Van Hensbergen 
31b530cc79SEric Van Hensbergen #include <linux/in.h>
32b530cc79SEric Van Hensbergen #include <linux/module.h>
33b530cc79SEric Van Hensbergen #include <linux/net.h>
34b530cc79SEric Van Hensbergen #include <linux/ipv6.h>
35b530cc79SEric Van Hensbergen #include <linux/errno.h>
36b530cc79SEric Van Hensbergen #include <linux/kernel.h>
37b530cc79SEric Van Hensbergen #include <linux/un.h>
38b530cc79SEric Van Hensbergen #include <linux/uaccess.h>
39b530cc79SEric Van Hensbergen #include <linux/inet.h>
40b530cc79SEric Van Hensbergen #include <linux/idr.h>
41b530cc79SEric Van Hensbergen #include <linux/file.h>
42b530cc79SEric Van Hensbergen #include <net/9p/9p.h>
43b530cc79SEric Van Hensbergen #include <linux/parser.h>
44b530cc79SEric Van Hensbergen #include <net/9p/transport.h>
45b530cc79SEric Van Hensbergen #include <linux/scatterlist.h>
46b530cc79SEric Van Hensbergen #include <linux/virtio.h>
47b530cc79SEric Van Hensbergen #include <linux/virtio_9p.h>
48b530cc79SEric Van Hensbergen 
49*e2735b77SEric Van Hensbergen #define VIRTQUEUE_NUM	128
50*e2735b77SEric Van Hensbergen 
51b530cc79SEric Van Hensbergen /* a single mutex to manage channel initialization and attachment */
52b530cc79SEric Van Hensbergen static DECLARE_MUTEX(virtio_9p_lock);
53b530cc79SEric Van Hensbergen /* global which tracks highest initialized channel */
54b530cc79SEric Van Hensbergen static int chan_index;
55b530cc79SEric Van Hensbergen 
56*e2735b77SEric Van Hensbergen #define P9_INIT_MAXTAG	16
57*e2735b77SEric Van Hensbergen 
58*e2735b77SEric Van Hensbergen #define REQ_STATUS_IDLE	0
59*e2735b77SEric Van Hensbergen #define REQ_STATUS_SENT 1
60*e2735b77SEric Van Hensbergen #define REQ_STATUS_RCVD 2
61*e2735b77SEric Van Hensbergen #define REQ_STATUS_FLSH 3
62*e2735b77SEric Van Hensbergen 
63*e2735b77SEric Van Hensbergen struct p9_req_t {
64*e2735b77SEric Van Hensbergen 	int status;
65*e2735b77SEric Van Hensbergen 	wait_queue_head_t *wq;
66*e2735b77SEric Van Hensbergen };
67*e2735b77SEric Van Hensbergen 
68b530cc79SEric Van Hensbergen /* We keep all per-channel information in a structure.
69b530cc79SEric Van Hensbergen  * This structure is allocated within the devices dev->mem space.
70b530cc79SEric Van Hensbergen  * A pointer to the structure will get put in the transport private.
71b530cc79SEric Van Hensbergen  */
72b530cc79SEric Van Hensbergen static struct virtio_chan {
73b530cc79SEric Van Hensbergen 	bool initialized;		/* channel is initialized */
74b530cc79SEric Van Hensbergen 	bool inuse;			/* channel is in use */
75b530cc79SEric Van Hensbergen 
76*e2735b77SEric Van Hensbergen 	spinlock_t lock;
77*e2735b77SEric Van Hensbergen 
78b530cc79SEric Van Hensbergen 	struct virtio_device *vdev;
79*e2735b77SEric Van Hensbergen 	struct virtqueue *vq;
80b530cc79SEric Van Hensbergen 
81*e2735b77SEric Van Hensbergen 	struct p9_idpool *tagpool;
82*e2735b77SEric Van Hensbergen 	struct p9_req_t *reqs;
83*e2735b77SEric Van Hensbergen 	int max_tag;
84b530cc79SEric Van Hensbergen 
85*e2735b77SEric Van Hensbergen 	/* Scatterlist: can be too big for stack. */
86*e2735b77SEric Van Hensbergen 	struct scatterlist sg[VIRTQUEUE_NUM];
87b530cc79SEric Van Hensbergen } channels[MAX_9P_CHAN];
88b530cc79SEric Van Hensbergen 
89*e2735b77SEric Van Hensbergen /* Lookup requests by tag */
90*e2735b77SEric Van Hensbergen static struct p9_req_t *p9_lookup_tag(struct virtio_chan *c, u16 tag)
91*e2735b77SEric Van Hensbergen {
92*e2735b77SEric Van Hensbergen 	/* This looks up the original request by tag so we know which
93*e2735b77SEric Van Hensbergen 	 * buffer to read the data into */
94*e2735b77SEric Van Hensbergen 	tag++;
95*e2735b77SEric Van Hensbergen 
96*e2735b77SEric Van Hensbergen 	while (tag >= c->max_tag) {
97*e2735b77SEric Van Hensbergen 		int old_max = c->max_tag;
98*e2735b77SEric Van Hensbergen 		int count;
99*e2735b77SEric Van Hensbergen 
100*e2735b77SEric Van Hensbergen 		if (c->max_tag)
101*e2735b77SEric Van Hensbergen 			c->max_tag *= 2;
102*e2735b77SEric Van Hensbergen 		else
103*e2735b77SEric Van Hensbergen 			c->max_tag = P9_INIT_MAXTAG;
104*e2735b77SEric Van Hensbergen 
105*e2735b77SEric Van Hensbergen 		c->reqs = krealloc(c->reqs, sizeof(struct p9_req_t)*c->max_tag,
106*e2735b77SEric Van Hensbergen 								GFP_ATOMIC);
107*e2735b77SEric Van Hensbergen 		if (!c->reqs) {
108*e2735b77SEric Van Hensbergen 			printk(KERN_ERR "Couldn't grow tag array\n");
109*e2735b77SEric Van Hensbergen 			BUG();
110*e2735b77SEric Van Hensbergen 		}
111*e2735b77SEric Van Hensbergen 		for (count = old_max; count < c->max_tag; count++) {
112*e2735b77SEric Van Hensbergen 			c->reqs[count].status = REQ_STATUS_IDLE;
113*e2735b77SEric Van Hensbergen 			c->reqs[count].wq = kmalloc(sizeof(wait_queue_t),
114*e2735b77SEric Van Hensbergen 								GFP_ATOMIC);
115*e2735b77SEric Van Hensbergen 			if (!c->reqs[count].wq) {
116*e2735b77SEric Van Hensbergen 				printk(KERN_ERR "Couldn't grow tag array\n");
117*e2735b77SEric Van Hensbergen 				BUG();
118*e2735b77SEric Van Hensbergen 			}
119*e2735b77SEric Van Hensbergen 			init_waitqueue_head(c->reqs[count].wq);
120*e2735b77SEric Van Hensbergen 		}
121*e2735b77SEric Van Hensbergen 	}
122*e2735b77SEric Van Hensbergen 
123*e2735b77SEric Van Hensbergen 	return &c->reqs[tag];
124*e2735b77SEric Van Hensbergen }
125*e2735b77SEric Van Hensbergen 
126*e2735b77SEric Van Hensbergen 
127b530cc79SEric Van Hensbergen /* How many bytes left in this page. */
128b530cc79SEric Van Hensbergen static unsigned int rest_of_page(void *data)
129b530cc79SEric Van Hensbergen {
130b530cc79SEric Van Hensbergen 	return PAGE_SIZE - ((unsigned long)data % PAGE_SIZE);
131b530cc79SEric Van Hensbergen }
132b530cc79SEric Van Hensbergen 
133b530cc79SEric Van Hensbergen static int p9_virtio_write(struct p9_trans *trans, void *buf, int count)
134b530cc79SEric Van Hensbergen {
135*e2735b77SEric Van Hensbergen 	/* Only use the rpc mechanism for now */
136b530cc79SEric Van Hensbergen 	return count;
137b530cc79SEric Van Hensbergen }
138b530cc79SEric Van Hensbergen 
139b530cc79SEric Van Hensbergen static int p9_virtio_read(struct p9_trans *trans, void *buf, int count)
140b530cc79SEric Van Hensbergen {
141*e2735b77SEric Van Hensbergen 	/* Only use the rpc mechanism for now */
142b530cc79SEric Van Hensbergen 	return 0;
143b530cc79SEric Van Hensbergen }
144b530cc79SEric Van Hensbergen 
145b530cc79SEric Van Hensbergen /* The poll function is used by 9p transports to determine if there
146b530cc79SEric Van Hensbergen  * is there is activity available on a particular channel.  In our case
147b530cc79SEric Van Hensbergen  * we use it to wait for a callback from the input routines.
148b530cc79SEric Van Hensbergen  */
149b530cc79SEric Van Hensbergen static unsigned int
150b530cc79SEric Van Hensbergen p9_virtio_poll(struct p9_trans *trans, struct poll_table_struct *pt)
151b530cc79SEric Van Hensbergen {
152*e2735b77SEric Van Hensbergen 	/* Only use the rpc mechanism for now */
153*e2735b77SEric Van Hensbergen 	return 0;
154b530cc79SEric Van Hensbergen }
155b530cc79SEric Van Hensbergen 
156b530cc79SEric Van Hensbergen static void p9_virtio_close(struct p9_trans *trans)
157b530cc79SEric Van Hensbergen {
158b530cc79SEric Van Hensbergen 	struct virtio_chan *chan = trans->priv;
159*e2735b77SEric Van Hensbergen 	int count;
160*e2735b77SEric Van Hensbergen 	unsigned int flags;
161*e2735b77SEric Van Hensbergen 
162*e2735b77SEric Van Hensbergen 	spin_lock_irqsave(&chan->lock, flags);
163*e2735b77SEric Van Hensbergen 	p9_idpool_destroy(chan->tagpool);
164*e2735b77SEric Van Hensbergen 	for (count = 0; count < chan->max_tag; count++)
165*e2735b77SEric Van Hensbergen 		kfree(chan->reqs[count].wq);
166*e2735b77SEric Van Hensbergen 	kfree(chan->reqs);
167*e2735b77SEric Van Hensbergen 	chan->max_tag = 0;
168*e2735b77SEric Van Hensbergen 	spin_unlock_irqrestore(&chan->lock, flags);
169b530cc79SEric Van Hensbergen 
170b530cc79SEric Van Hensbergen 	down(&virtio_9p_lock);
171b530cc79SEric Van Hensbergen 	chan->inuse = false;
172b530cc79SEric Van Hensbergen 	up(&virtio_9p_lock);
173b530cc79SEric Van Hensbergen 
174b530cc79SEric Van Hensbergen 	kfree(trans);
175b530cc79SEric Van Hensbergen }
176b530cc79SEric Van Hensbergen 
177*e2735b77SEric Van Hensbergen static void req_done(struct virtqueue *vq)
178b530cc79SEric Van Hensbergen {
179*e2735b77SEric Van Hensbergen 	struct virtio_chan *chan = vq->vdev->priv;
180*e2735b77SEric Van Hensbergen 	struct p9_fcall *rc;
181*e2735b77SEric Van Hensbergen 	unsigned int len;
182*e2735b77SEric Van Hensbergen 	unsigned long flags;
183*e2735b77SEric Van Hensbergen 	struct p9_req_t *req;
184b530cc79SEric Van Hensbergen 
185*e2735b77SEric Van Hensbergen 	spin_lock_irqsave(&chan->lock, flags);
186*e2735b77SEric Van Hensbergen 	while ((rc = chan->vq->vq_ops->get_buf(chan->vq, &len)) != NULL) {
187*e2735b77SEric Van Hensbergen 		req = p9_lookup_tag(chan, rc->tag);
188*e2735b77SEric Van Hensbergen 		req->status = REQ_STATUS_RCVD;
189*e2735b77SEric Van Hensbergen 		wake_up(req->wq);
190*e2735b77SEric Van Hensbergen 	}
191*e2735b77SEric Van Hensbergen 	/* In case queue is stopped waiting for more buffers. */
192*e2735b77SEric Van Hensbergen 	spin_unlock_irqrestore(&chan->lock, flags);
193b530cc79SEric Van Hensbergen }
194b530cc79SEric Van Hensbergen 
195*e2735b77SEric Van Hensbergen static int
196*e2735b77SEric Van Hensbergen pack_sg_list(struct scatterlist *sg, int start, int limit, char *data,
197*e2735b77SEric Van Hensbergen 								int count)
198*e2735b77SEric Van Hensbergen {
199*e2735b77SEric Van Hensbergen 	int s;
200*e2735b77SEric Van Hensbergen 	int index = start;
201*e2735b77SEric Van Hensbergen 
202*e2735b77SEric Van Hensbergen 	while (count) {
203*e2735b77SEric Van Hensbergen 		s = rest_of_page(data);
204*e2735b77SEric Van Hensbergen 		if (s > count)
205*e2735b77SEric Van Hensbergen 			s = count;
206*e2735b77SEric Van Hensbergen 		sg_set_buf(&sg[index++], data, s);
207*e2735b77SEric Van Hensbergen 		count -= s;
208*e2735b77SEric Van Hensbergen 		data += s;
209*e2735b77SEric Van Hensbergen 		if (index > limit)
210*e2735b77SEric Van Hensbergen 			BUG();
211*e2735b77SEric Van Hensbergen 	}
212*e2735b77SEric Van Hensbergen 
213*e2735b77SEric Van Hensbergen 	return index-start;
214*e2735b77SEric Van Hensbergen }
215*e2735b77SEric Van Hensbergen 
216*e2735b77SEric Van Hensbergen static int
217*e2735b77SEric Van Hensbergen p9_virtio_rpc(struct p9_trans *t, struct p9_fcall *tc, struct p9_fcall **rc,
218*e2735b77SEric Van Hensbergen 		int msize, int dotu)
219*e2735b77SEric Van Hensbergen {
220*e2735b77SEric Van Hensbergen 	int in, out;
221*e2735b77SEric Van Hensbergen 	int n, err, size;
222*e2735b77SEric Van Hensbergen 	struct virtio_chan *chan = t->priv;
223*e2735b77SEric Van Hensbergen 	char *rdata;
224*e2735b77SEric Van Hensbergen 	struct p9_req_t *req;
225*e2735b77SEric Van Hensbergen 	unsigned long flags;
226*e2735b77SEric Van Hensbergen 
227*e2735b77SEric Van Hensbergen 	if (*rc == NULL) {
228*e2735b77SEric Van Hensbergen 		*rc = kmalloc(sizeof(struct p9_fcall) + msize, GFP_KERNEL);
229*e2735b77SEric Van Hensbergen 		if (!*rc)
230*e2735b77SEric Van Hensbergen 			return -ENOMEM;
231*e2735b77SEric Van Hensbergen 	}
232*e2735b77SEric Van Hensbergen 
233*e2735b77SEric Van Hensbergen 	rdata = (char *)*rc+sizeof(struct p9_fcall);
234*e2735b77SEric Van Hensbergen 
235*e2735b77SEric Van Hensbergen 	spin_lock_irqsave(&chan->lock, flags);
236*e2735b77SEric Van Hensbergen 	n = P9_NOTAG;
237*e2735b77SEric Van Hensbergen 	if (tc->id != P9_TVERSION) {
238*e2735b77SEric Van Hensbergen 		n = p9_idpool_get(chan->tagpool);
239*e2735b77SEric Van Hensbergen 		if (n < 0)
240*e2735b77SEric Van Hensbergen 			return -ENOMEM;
241*e2735b77SEric Van Hensbergen 	}
242*e2735b77SEric Van Hensbergen 
243*e2735b77SEric Van Hensbergen 	req = p9_lookup_tag(chan, n);
244*e2735b77SEric Van Hensbergen 	spin_unlock_irqrestore(&chan->lock, flags);
245*e2735b77SEric Van Hensbergen 
246*e2735b77SEric Van Hensbergen 	p9_set_tag(tc, n);
247*e2735b77SEric Van Hensbergen 
248*e2735b77SEric Van Hensbergen 	P9_DPRINTK(P9_DEBUG_TRANS, "9p debug: virtio rpc tag %d\n", n);
249*e2735b77SEric Van Hensbergen 
250*e2735b77SEric Van Hensbergen 	out = pack_sg_list(chan->sg, 0, VIRTQUEUE_NUM, tc->sdata, tc->size);
251*e2735b77SEric Van Hensbergen 	in = pack_sg_list(chan->sg, out, VIRTQUEUE_NUM-out, rdata, msize);
252*e2735b77SEric Van Hensbergen 
253*e2735b77SEric Van Hensbergen 	req->status = REQ_STATUS_SENT;
254*e2735b77SEric Van Hensbergen 
255*e2735b77SEric Van Hensbergen 	if (chan->vq->vq_ops->add_buf(chan->vq, chan->sg, out, in, tc)) {
256*e2735b77SEric Van Hensbergen 		P9_DPRINTK(P9_DEBUG_TRANS,
257*e2735b77SEric Van Hensbergen 			"9p debug: virtio rpc add_buf returned failure");
258*e2735b77SEric Van Hensbergen 		return -EIO;
259*e2735b77SEric Van Hensbergen 	}
260*e2735b77SEric Van Hensbergen 
261*e2735b77SEric Van Hensbergen 	chan->vq->vq_ops->kick(chan->vq);
262*e2735b77SEric Van Hensbergen 
263*e2735b77SEric Van Hensbergen 	wait_event(*req->wq, req->status == REQ_STATUS_RCVD);
264*e2735b77SEric Van Hensbergen 
265*e2735b77SEric Van Hensbergen 	size = le32_to_cpu(*(__le32 *) rdata);
266*e2735b77SEric Van Hensbergen 
267*e2735b77SEric Van Hensbergen 	err = p9_deserialize_fcall(rdata, size, *rc, dotu);
268*e2735b77SEric Van Hensbergen 	if (err < 0) {
269*e2735b77SEric Van Hensbergen 		P9_DPRINTK(P9_DEBUG_TRANS,
270*e2735b77SEric Van Hensbergen 			"9p debug: virtio rpc deserialize returned %d\n", err);
271*e2735b77SEric Van Hensbergen 		return err;
272*e2735b77SEric Van Hensbergen 	}
273*e2735b77SEric Van Hensbergen 
274*e2735b77SEric Van Hensbergen #ifdef CONFIG_NET_9P_DEBUG
275*e2735b77SEric Van Hensbergen 	if ((p9_debug_level&P9_DEBUG_FCALL) == P9_DEBUG_FCALL) {
276*e2735b77SEric Van Hensbergen 		char buf[150];
277*e2735b77SEric Van Hensbergen 
278*e2735b77SEric Van Hensbergen 		p9_printfcall(buf, sizeof(buf), *rc, dotu);
279*e2735b77SEric Van Hensbergen 		printk(KERN_NOTICE ">>> %p %s\n", t, buf);
280*e2735b77SEric Van Hensbergen 	}
281*e2735b77SEric Van Hensbergen #endif
282*e2735b77SEric Van Hensbergen 
283*e2735b77SEric Van Hensbergen 	if (n != P9_NOTAG && p9_idpool_check(n, chan->tagpool))
284*e2735b77SEric Van Hensbergen 		p9_idpool_put(n, chan->tagpool);
285*e2735b77SEric Van Hensbergen 
286*e2735b77SEric Van Hensbergen 	req->status = REQ_STATUS_IDLE;
287*e2735b77SEric Van Hensbergen 
288*e2735b77SEric Van Hensbergen 	return 0;
289*e2735b77SEric Van Hensbergen }
290*e2735b77SEric Van Hensbergen 
291*e2735b77SEric Van Hensbergen static int p9_virtio_probe(struct virtio_device *vdev)
292b530cc79SEric Van Hensbergen {
293b530cc79SEric Van Hensbergen 	int err;
294b530cc79SEric Van Hensbergen 	struct virtio_chan *chan;
295b530cc79SEric Van Hensbergen 	int index;
296b530cc79SEric Van Hensbergen 
297b530cc79SEric Van Hensbergen 	down(&virtio_9p_lock);
298b530cc79SEric Van Hensbergen 	index = chan_index++;
299b530cc79SEric Van Hensbergen 	chan = &channels[index];
300b530cc79SEric Van Hensbergen 	up(&virtio_9p_lock);
301b530cc79SEric Van Hensbergen 
302b530cc79SEric Van Hensbergen 	if (chan_index > MAX_9P_CHAN) {
303b530cc79SEric Van Hensbergen 		printk(KERN_ERR "9p: virtio: Maximum channels exceeded\n");
304b530cc79SEric Van Hensbergen 		BUG();
305b530cc79SEric Van Hensbergen 		err = -ENOMEM;
306b530cc79SEric Van Hensbergen 		goto fail;
307b530cc79SEric Van Hensbergen 	}
308b530cc79SEric Van Hensbergen 
309*e2735b77SEric Van Hensbergen 	chan->vdev = vdev;
310*e2735b77SEric Van Hensbergen 
311*e2735b77SEric Van Hensbergen 	/* We expect one virtqueue, for requests. */
312*e2735b77SEric Van Hensbergen 	chan->vq = vdev->config->find_vq(vdev, 0, req_done);
313*e2735b77SEric Van Hensbergen 	if (IS_ERR(chan->vq)) {
314*e2735b77SEric Van Hensbergen 		err = PTR_ERR(chan->vq);
315*e2735b77SEric Van Hensbergen 		goto out_free_vq;
316b530cc79SEric Van Hensbergen 	}
317*e2735b77SEric Van Hensbergen 	chan->vq->vdev->priv = chan;
318*e2735b77SEric Van Hensbergen 	spin_lock_init(&chan->lock);
319b530cc79SEric Van Hensbergen 
320*e2735b77SEric Van Hensbergen 	sg_init_table(chan->sg, VIRTQUEUE_NUM);
321b530cc79SEric Van Hensbergen 
322b530cc79SEric Van Hensbergen 	chan->inuse = false;
323b530cc79SEric Van Hensbergen 	chan->initialized = true;
324b530cc79SEric Van Hensbergen 	return 0;
325b530cc79SEric Van Hensbergen 
326*e2735b77SEric Van Hensbergen out_free_vq:
327*e2735b77SEric Van Hensbergen 	vdev->config->del_vq(chan->vq);
328b530cc79SEric Van Hensbergen fail:
329b530cc79SEric Van Hensbergen 	down(&virtio_9p_lock);
330b530cc79SEric Van Hensbergen 	chan_index--;
331b530cc79SEric Van Hensbergen 	up(&virtio_9p_lock);
332b530cc79SEric Van Hensbergen 	return err;
333b530cc79SEric Van Hensbergen }
334b530cc79SEric Van Hensbergen 
335b530cc79SEric Van Hensbergen /* This sets up a transport channel for 9p communication.  Right now
336b530cc79SEric Van Hensbergen  * we only match the first available channel, but eventually we couldlook up
337b530cc79SEric Van Hensbergen  * alternate channels by matching devname versus a virtio_config entry.
338b530cc79SEric Van Hensbergen  * We use a simple reference count mechanism to ensure that only a single
339b530cc79SEric Van Hensbergen  * mount has a channel open at a time. */
340b530cc79SEric Van Hensbergen static struct p9_trans *p9_virtio_create(const char *devname, char *args)
341b530cc79SEric Van Hensbergen {
342b530cc79SEric Van Hensbergen 	struct p9_trans *trans;
343b530cc79SEric Van Hensbergen 	struct virtio_chan *chan = channels;
344*e2735b77SEric Van Hensbergen 	int index = 0;
345b530cc79SEric Van Hensbergen 
346b530cc79SEric Van Hensbergen 	down(&virtio_9p_lock);
347b530cc79SEric Van Hensbergen 	while (index < MAX_9P_CHAN) {
348b530cc79SEric Van Hensbergen 		if (chan->initialized && !chan->inuse) {
349b530cc79SEric Van Hensbergen 			chan->inuse = true;
350b530cc79SEric Van Hensbergen 			break;
351b530cc79SEric Van Hensbergen 		} else {
352b530cc79SEric Van Hensbergen 			index++;
353b530cc79SEric Van Hensbergen 			chan = &channels[index];
354b530cc79SEric Van Hensbergen 		}
355b530cc79SEric Van Hensbergen 	}
356b530cc79SEric Van Hensbergen 	up(&virtio_9p_lock);
357b530cc79SEric Van Hensbergen 
358b530cc79SEric Van Hensbergen 	if (index >= MAX_9P_CHAN) {
359*e2735b77SEric Van Hensbergen 		printk(KERN_ERR "9p: no channels available\n");
360*e2735b77SEric Van Hensbergen 		return ERR_PTR(-ENODEV);
361b530cc79SEric Van Hensbergen 	}
362b530cc79SEric Van Hensbergen 
363*e2735b77SEric Van Hensbergen 	chan->tagpool = p9_idpool_create();
364*e2735b77SEric Van Hensbergen 	if (IS_ERR(chan->tagpool)) {
365*e2735b77SEric Van Hensbergen 		printk(KERN_ERR "9p: couldn't allocate tagpool\n");
366*e2735b77SEric Van Hensbergen 		return ERR_PTR(-ENOMEM);
367*e2735b77SEric Van Hensbergen 	}
368*e2735b77SEric Van Hensbergen 	p9_idpool_get(chan->tagpool); /* reserve tag 0 */
369*e2735b77SEric Van Hensbergen 	chan->max_tag = 0;
370*e2735b77SEric Van Hensbergen 	chan->reqs = NULL;
371*e2735b77SEric Van Hensbergen 
372b530cc79SEric Van Hensbergen 	trans = kmalloc(sizeof(struct p9_trans), GFP_KERNEL);
373b530cc79SEric Van Hensbergen 	if (!trans) {
374b530cc79SEric Van Hensbergen 		printk(KERN_ERR "9p: couldn't allocate transport\n");
375b530cc79SEric Van Hensbergen 		return ERR_PTR(-ENOMEM);
376b530cc79SEric Van Hensbergen 	}
377b530cc79SEric Van Hensbergen 
378b530cc79SEric Van Hensbergen 	trans->write = p9_virtio_write;
379b530cc79SEric Van Hensbergen 	trans->read = p9_virtio_read;
380b530cc79SEric Van Hensbergen 	trans->close = p9_virtio_close;
381b530cc79SEric Van Hensbergen 	trans->poll = p9_virtio_poll;
382*e2735b77SEric Van Hensbergen 	trans->rpc = p9_virtio_rpc;
383b530cc79SEric Van Hensbergen 	trans->priv = chan;
384b530cc79SEric Van Hensbergen 
385b530cc79SEric Van Hensbergen 	return trans;
386b530cc79SEric Van Hensbergen }
387b530cc79SEric Van Hensbergen 
388b530cc79SEric Van Hensbergen #define VIRTIO_ID_9P 9
389b530cc79SEric Van Hensbergen 
390b530cc79SEric Van Hensbergen static struct virtio_device_id id_table[] = {
391b530cc79SEric Van Hensbergen 	{ VIRTIO_ID_9P, VIRTIO_DEV_ANY_ID },
392b530cc79SEric Van Hensbergen 	{ 0 },
393b530cc79SEric Van Hensbergen };
394b530cc79SEric Van Hensbergen 
395b530cc79SEric Van Hensbergen /* The standard "struct lguest_driver": */
396b530cc79SEric Van Hensbergen static struct virtio_driver p9_virtio_drv = {
397b530cc79SEric Van Hensbergen 	.driver.name = 	KBUILD_MODNAME,
398b530cc79SEric Van Hensbergen 	.driver.owner = THIS_MODULE,
399b530cc79SEric Van Hensbergen 	.id_table =	id_table,
400b530cc79SEric Van Hensbergen 	.probe = 	p9_virtio_probe,
401b530cc79SEric Van Hensbergen };
402b530cc79SEric Van Hensbergen 
403b530cc79SEric Van Hensbergen static struct p9_trans_module p9_virtio_trans = {
404b530cc79SEric Van Hensbergen 	.name = "virtio",
405b530cc79SEric Van Hensbergen 	.create = p9_virtio_create,
406*e2735b77SEric Van Hensbergen 	.maxsize = PAGE_SIZE*16,
407b530cc79SEric Van Hensbergen 	.def = 0,
408b530cc79SEric Van Hensbergen };
409b530cc79SEric Van Hensbergen 
410b530cc79SEric Van Hensbergen /* The standard init function */
411b530cc79SEric Van Hensbergen static int __init p9_virtio_init(void)
412b530cc79SEric Van Hensbergen {
413b530cc79SEric Van Hensbergen 	int count;
414b530cc79SEric Van Hensbergen 
415b530cc79SEric Van Hensbergen 	for (count = 0; count < MAX_9P_CHAN; count++)
416b530cc79SEric Van Hensbergen 		channels[count].initialized = false;
417b530cc79SEric Van Hensbergen 
418b530cc79SEric Van Hensbergen 	v9fs_register_trans(&p9_virtio_trans);
419b530cc79SEric Van Hensbergen 	return register_virtio_driver(&p9_virtio_drv);
420b530cc79SEric Van Hensbergen }
421b530cc79SEric Van Hensbergen 
422b530cc79SEric Van Hensbergen module_init(p9_virtio_init);
423b530cc79SEric Van Hensbergen 
424b530cc79SEric Van Hensbergen MODULE_DEVICE_TABLE(virtio, id_table);
425b530cc79SEric Van Hensbergen MODULE_AUTHOR("Eric Van Hensbergen <ericvh@gmail.com>");
426b530cc79SEric Van Hensbergen MODULE_DESCRIPTION("Virtio 9p Transport");
427b530cc79SEric Van Hensbergen MODULE_LICENSE("GPL");
428