xref: /openbmc/linux/net/9p/trans_virtio.c (revision 419b39561e698d73a42f8010655d22e0134486da)
1b530cc79SEric Van Hensbergen /*
2fea511a6SEric Van Hensbergen  * The Virtio 9p transport driver
3b530cc79SEric Van Hensbergen  *
4e2735b77SEric Van Hensbergen  * This is a block based transport driver based on the lguest block driver
5e2735b77SEric Van Hensbergen  * code.
6b530cc79SEric Van Hensbergen  *
7fea511a6SEric Van Hensbergen  *  Copyright (C) 2007, 2008 Eric Van Hensbergen, IBM Corporation
8b530cc79SEric Van Hensbergen  *
9b530cc79SEric Van Hensbergen  *  Based on virtio console driver
10b530cc79SEric Van Hensbergen  *  Copyright (C) 2006, 2007 Rusty Russell, IBM Corporation
11b530cc79SEric Van Hensbergen  *
12b530cc79SEric Van Hensbergen  *  This program is free software; you can redistribute it and/or modify
13b530cc79SEric Van Hensbergen  *  it under the terms of the GNU General Public License version 2
14b530cc79SEric Van Hensbergen  *  as published by the Free Software Foundation.
15b530cc79SEric Van Hensbergen  *
16b530cc79SEric Van Hensbergen  *  This program is distributed in the hope that it will be useful,
17b530cc79SEric Van Hensbergen  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
18b530cc79SEric Van Hensbergen  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19b530cc79SEric Van Hensbergen  *  GNU General Public License for more details.
20b530cc79SEric Van Hensbergen  *
21b530cc79SEric Van Hensbergen  *  You should have received a copy of the GNU General Public License
22b530cc79SEric Van Hensbergen  *  along with this program; if not, write to:
23b530cc79SEric Van Hensbergen  *  Free Software Foundation
24b530cc79SEric Van Hensbergen  *  51 Franklin Street, Fifth Floor
25b530cc79SEric Van Hensbergen  *  Boston, MA  02111-1301  USA
26b530cc79SEric Van Hensbergen  *
27b530cc79SEric Van Hensbergen  */
28b530cc79SEric Van Hensbergen 
29b530cc79SEric Van Hensbergen #include <linux/in.h>
30b530cc79SEric Van Hensbergen #include <linux/module.h>
31b530cc79SEric Van Hensbergen #include <linux/net.h>
32b530cc79SEric Van Hensbergen #include <linux/ipv6.h>
33b530cc79SEric Van Hensbergen #include <linux/errno.h>
34b530cc79SEric Van Hensbergen #include <linux/kernel.h>
35b530cc79SEric Van Hensbergen #include <linux/un.h>
36b530cc79SEric Van Hensbergen #include <linux/uaccess.h>
37b530cc79SEric Van Hensbergen #include <linux/inet.h>
38b530cc79SEric Van Hensbergen #include <linux/idr.h>
39b530cc79SEric Van Hensbergen #include <linux/file.h>
405a0e3ad6STejun Heo #include <linux/slab.h>
41b530cc79SEric Van Hensbergen #include <net/9p/9p.h>
42b530cc79SEric Van Hensbergen #include <linux/parser.h>
438b81ef58SEric Van Hensbergen #include <net/9p/client.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 
49e2735b77SEric Van Hensbergen #define VIRTQUEUE_NUM	128
50e2735b77SEric Van Hensbergen 
51b530cc79SEric Van Hensbergen /* a single mutex to manage channel initialization and attachment */
52c1549497SJosef 'Jeff' Sipek static DEFINE_MUTEX(virtio_9p_lock);
53b530cc79SEric Van Hensbergen 
54ee443996SEric Van Hensbergen /**
55ee443996SEric Van Hensbergen  * struct virtio_chan - per-instance transport information
56ee443996SEric Van Hensbergen  * @initialized: whether the channel is initialized
57ee443996SEric Van Hensbergen  * @inuse: whether the channel is in use
58ee443996SEric Van Hensbergen  * @lock: protects multiple elements within this structure
590e15597eSAbhishek Kulkarni  * @client: client instance
60ee443996SEric Van Hensbergen  * @vdev: virtio dev associated with this channel
61ee443996SEric Van Hensbergen  * @vq: virtio queue associated with this channel
62ee443996SEric Van Hensbergen  * @sg: scatter gather list which is used to pack a request (protected?)
63ee443996SEric Van Hensbergen  *
64ee443996SEric Van Hensbergen  * We keep all per-channel information in a structure.
65b530cc79SEric Van Hensbergen  * This structure is allocated within the devices dev->mem space.
66b530cc79SEric Van Hensbergen  * A pointer to the structure will get put in the transport private.
67ee443996SEric Van Hensbergen  *
68b530cc79SEric Van Hensbergen  */
69ee443996SEric Van Hensbergen 
7037c1209dSAneesh Kumar K.V struct virtio_chan {
71ee443996SEric Van Hensbergen 	bool inuse;
72b530cc79SEric Van Hensbergen 
73e2735b77SEric Van Hensbergen 	spinlock_t lock;
74e2735b77SEric Van Hensbergen 
75fea511a6SEric Van Hensbergen 	struct p9_client *client;
76b530cc79SEric Van Hensbergen 	struct virtio_device *vdev;
77e2735b77SEric Van Hensbergen 	struct virtqueue *vq;
78b530cc79SEric Van Hensbergen 
79e2735b77SEric Van Hensbergen 	/* Scatterlist: can be too big for stack. */
80e2735b77SEric Van Hensbergen 	struct scatterlist sg[VIRTQUEUE_NUM];
8137c1209dSAneesh Kumar K.V 
8297ee9b02SAneesh Kumar K.V 	int tag_len;
8397ee9b02SAneesh Kumar K.V 	/*
8497ee9b02SAneesh Kumar K.V 	 * tag name to identify a mount Non-null terminated
8597ee9b02SAneesh Kumar K.V 	 */
8697ee9b02SAneesh Kumar K.V 	char *tag;
8797ee9b02SAneesh Kumar K.V 
8837c1209dSAneesh Kumar K.V 	struct list_head chan_list;
8937c1209dSAneesh Kumar K.V };
9037c1209dSAneesh Kumar K.V 
9137c1209dSAneesh Kumar K.V static struct list_head virtio_chan_list;
92b530cc79SEric Van Hensbergen 
93b530cc79SEric Van Hensbergen /* How many bytes left in this page. */
94b530cc79SEric Van Hensbergen static unsigned int rest_of_page(void *data)
95b530cc79SEric Van Hensbergen {
96b530cc79SEric Van Hensbergen 	return PAGE_SIZE - ((unsigned long)data % PAGE_SIZE);
97b530cc79SEric Van Hensbergen }
98b530cc79SEric Van Hensbergen 
99ee443996SEric Van Hensbergen /**
100ee443996SEric Van Hensbergen  * p9_virtio_close - reclaim resources of a channel
1010e15597eSAbhishek Kulkarni  * @client: client instance
102ee443996SEric Van Hensbergen  *
103ee443996SEric Van Hensbergen  * This reclaims a channel by freeing its resources and
104ee443996SEric Van Hensbergen  * reseting its inuse flag.
105ee443996SEric Van Hensbergen  *
106ee443996SEric Van Hensbergen  */
107ee443996SEric Van Hensbergen 
1088b81ef58SEric Van Hensbergen static void p9_virtio_close(struct p9_client *client)
109b530cc79SEric Van Hensbergen {
1108b81ef58SEric Van Hensbergen 	struct virtio_chan *chan = client->trans;
111b530cc79SEric Van Hensbergen 
112c1549497SJosef 'Jeff' Sipek 	mutex_lock(&virtio_9p_lock);
113fb786100SAneesh Kumar K.V 	if (chan)
114b530cc79SEric Van Hensbergen 		chan->inuse = false;
115c1549497SJosef 'Jeff' Sipek 	mutex_unlock(&virtio_9p_lock);
116b530cc79SEric Van Hensbergen }
117b530cc79SEric Van Hensbergen 
118ee443996SEric Van Hensbergen /**
119ee443996SEric Van Hensbergen  * req_done - callback which signals activity from the server
120ee443996SEric Van Hensbergen  * @vq: virtio queue activity was received on
121ee443996SEric Van Hensbergen  *
122ee443996SEric Van Hensbergen  * This notifies us that the server has triggered some activity
123ee443996SEric Van Hensbergen  * on the virtio channel - most likely a response to request we
124ee443996SEric Van Hensbergen  * sent.  Figure out which requests now have responses and wake up
125ee443996SEric Van Hensbergen  * those threads.
126ee443996SEric Van Hensbergen  *
127ee443996SEric Van Hensbergen  * Bugs: could do with some additional sanity checking, but appears to work.
128ee443996SEric Van Hensbergen  *
129ee443996SEric Van Hensbergen  */
130ee443996SEric Van Hensbergen 
131e2735b77SEric Van Hensbergen static void req_done(struct virtqueue *vq)
132b530cc79SEric Van Hensbergen {
133e2735b77SEric Van Hensbergen 	struct virtio_chan *chan = vq->vdev->priv;
134e2735b77SEric Van Hensbergen 	struct p9_fcall *rc;
135e2735b77SEric Van Hensbergen 	unsigned int len;
136e2735b77SEric Van Hensbergen 	struct p9_req_t *req;
137*419b3956SVenkateswararao Jujjuri (JV) 	unsigned long flags;
138b530cc79SEric Van Hensbergen 
13991b8534fSEric Van Hensbergen 	P9_DPRINTK(P9_DEBUG_TRANS, ": request done\n");
14091b8534fSEric Van Hensbergen 
141*419b3956SVenkateswararao Jujjuri (JV) 	do {
142*419b3956SVenkateswararao Jujjuri (JV) 		spin_lock_irqsave(&chan->lock, flags);
143*419b3956SVenkateswararao Jujjuri (JV) 		rc = virtqueue_get_buf(chan->vq, &len);
144*419b3956SVenkateswararao Jujjuri (JV) 		spin_unlock_irqrestore(&chan->lock, flags);
145*419b3956SVenkateswararao Jujjuri (JV) 
146*419b3956SVenkateswararao Jujjuri (JV) 		if (rc != NULL) {
14791b8534fSEric Van Hensbergen 			P9_DPRINTK(P9_DEBUG_TRANS, ": rc %p\n", rc);
148*419b3956SVenkateswararao Jujjuri (JV) 			P9_DPRINTK(P9_DEBUG_TRANS, ": lookup tag %d\n",
149*419b3956SVenkateswararao Jujjuri (JV) 					rc->tag);
150fea511a6SEric Van Hensbergen 			req = p9_tag_lookup(chan->client, rc->tag);
1511bab88b2SLatchesar Ionkov 			req->status = REQ_STATUS_RCVD;
15291b8534fSEric Van Hensbergen 			p9_client_cb(chan->client, req);
153e2735b77SEric Van Hensbergen 		}
154*419b3956SVenkateswararao Jujjuri (JV) 	} while (rc != NULL);
155b530cc79SEric Van Hensbergen }
156b530cc79SEric Van Hensbergen 
157ee443996SEric Van Hensbergen /**
158ee443996SEric Van Hensbergen  * pack_sg_list - pack a scatter gather list from a linear buffer
159ee443996SEric Van Hensbergen  * @sg: scatter/gather list to pack into
160ee443996SEric Van Hensbergen  * @start: which segment of the sg_list to start at
161ee443996SEric Van Hensbergen  * @limit: maximum segment to pack data to
162ee443996SEric Van Hensbergen  * @data: data to pack into scatter/gather list
163ee443996SEric Van Hensbergen  * @count: amount of data to pack into the scatter/gather list
164ee443996SEric Van Hensbergen  *
165ee443996SEric Van Hensbergen  * sg_lists have multiple segments of various sizes.  This will pack
166ee443996SEric Van Hensbergen  * arbitrary data into an existing scatter gather list, segmenting the
167ee443996SEric Van Hensbergen  * data as necessary within constraints.
168ee443996SEric Van Hensbergen  *
169ee443996SEric Van Hensbergen  */
170ee443996SEric Van Hensbergen 
171e2735b77SEric Van Hensbergen static int
172e2735b77SEric Van Hensbergen pack_sg_list(struct scatterlist *sg, int start, int limit, char *data,
173e2735b77SEric Van Hensbergen 								int count)
174e2735b77SEric Van Hensbergen {
175e2735b77SEric Van Hensbergen 	int s;
176e2735b77SEric Van Hensbergen 	int index = start;
177e2735b77SEric Van Hensbergen 
178e2735b77SEric Van Hensbergen 	while (count) {
179e2735b77SEric Van Hensbergen 		s = rest_of_page(data);
180e2735b77SEric Van Hensbergen 		if (s > count)
181e2735b77SEric Van Hensbergen 			s = count;
182e2735b77SEric Van Hensbergen 		sg_set_buf(&sg[index++], data, s);
183e2735b77SEric Van Hensbergen 		count -= s;
184e2735b77SEric Van Hensbergen 		data += s;
185d6584f3aSJulia Lawall 		BUG_ON(index > limit);
186e2735b77SEric Van Hensbergen 	}
187e2735b77SEric Van Hensbergen 
188e2735b77SEric Van Hensbergen 	return index-start;
189e2735b77SEric Van Hensbergen }
190e2735b77SEric Van Hensbergen 
19191b8534fSEric Van Hensbergen /* We don't currently allow canceling of virtio requests */
19291b8534fSEric Van Hensbergen static int p9_virtio_cancel(struct p9_client *client, struct p9_req_t *req)
19391b8534fSEric Van Hensbergen {
19491b8534fSEric Van Hensbergen 	return 1;
19591b8534fSEric Van Hensbergen }
19691b8534fSEric Van Hensbergen 
197ee443996SEric Van Hensbergen /**
19891b8534fSEric Van Hensbergen  * p9_virtio_request - issue a request
1990e15597eSAbhishek Kulkarni  * @client: client instance issuing the request
2000e15597eSAbhishek Kulkarni  * @req: request to be issued
201ee443996SEric Van Hensbergen  *
202ee443996SEric Van Hensbergen  */
203ee443996SEric Van Hensbergen 
204e2735b77SEric Van Hensbergen static int
20591b8534fSEric Van Hensbergen p9_virtio_request(struct p9_client *client, struct p9_req_t *req)
206e2735b77SEric Van Hensbergen {
207e2735b77SEric Van Hensbergen 	int in, out;
20891b8534fSEric Van Hensbergen 	struct virtio_chan *chan = client->trans;
20991b8534fSEric Van Hensbergen 	char *rdata = (char *)req->rc+sizeof(struct p9_fcall);
210*419b3956SVenkateswararao Jujjuri (JV) 	unsigned long flags;
211*419b3956SVenkateswararao Jujjuri (JV) 	int err;
212e2735b77SEric Van Hensbergen 
21391b8534fSEric Van Hensbergen 	P9_DPRINTK(P9_DEBUG_TRANS, "9p debug: virtio request\n");
214e2735b77SEric Van Hensbergen 
215*419b3956SVenkateswararao Jujjuri (JV) 	req->status = REQ_STATUS_SENT;
216*419b3956SVenkateswararao Jujjuri (JV) 
217*419b3956SVenkateswararao Jujjuri (JV) 	spin_lock_irqsave(&chan->lock, flags);
21891b8534fSEric Van Hensbergen 	out = pack_sg_list(chan->sg, 0, VIRTQUEUE_NUM, req->tc->sdata,
21991b8534fSEric Van Hensbergen 								req->tc->size);
22091b8534fSEric Van Hensbergen 	in = pack_sg_list(chan->sg, out, VIRTQUEUE_NUM-out, rdata,
22191b8534fSEric Van Hensbergen 								client->msize);
222e2735b77SEric Van Hensbergen 
223*419b3956SVenkateswararao Jujjuri (JV) 	err = virtqueue_add_buf(chan->vq, chan->sg, out, in, req->tc);
224*419b3956SVenkateswararao Jujjuri (JV) 	if (err < 0) {
225*419b3956SVenkateswararao Jujjuri (JV) 		spin_unlock_irqrestore(&chan->lock, flags);
226e2735b77SEric Van Hensbergen 		P9_DPRINTK(P9_DEBUG_TRANS,
227e2735b77SEric Van Hensbergen 			"9p debug: virtio rpc add_buf returned failure");
228e2735b77SEric Van Hensbergen 		return -EIO;
229e2735b77SEric Van Hensbergen 	}
230e2735b77SEric Van Hensbergen 
231dc3f5e68SMichael S. Tsirkin 	virtqueue_kick(chan->vq);
232*419b3956SVenkateswararao Jujjuri (JV) 	spin_unlock_irqrestore(&chan->lock, flags);
233e2735b77SEric Van Hensbergen 
23491b8534fSEric Van Hensbergen 	P9_DPRINTK(P9_DEBUG_TRANS, "9p debug: virtio request kicked\n");
235e2735b77SEric Van Hensbergen 	return 0;
236e2735b77SEric Van Hensbergen }
237e2735b77SEric Van Hensbergen 
23886c84373SAneesh Kumar K.V static ssize_t p9_mount_tag_show(struct device *dev,
23986c84373SAneesh Kumar K.V 				struct device_attribute *attr, char *buf)
24086c84373SAneesh Kumar K.V {
24186c84373SAneesh Kumar K.V 	struct virtio_chan *chan;
24286c84373SAneesh Kumar K.V 	struct virtio_device *vdev;
24386c84373SAneesh Kumar K.V 
24486c84373SAneesh Kumar K.V 	vdev = dev_to_virtio(dev);
24586c84373SAneesh Kumar K.V 	chan = vdev->priv;
24686c84373SAneesh Kumar K.V 
24786c84373SAneesh Kumar K.V 	return snprintf(buf, chan->tag_len + 1, "%s", chan->tag);
24886c84373SAneesh Kumar K.V }
24986c84373SAneesh Kumar K.V 
25086c84373SAneesh Kumar K.V static DEVICE_ATTR(mount_tag, 0444, p9_mount_tag_show, NULL);
25186c84373SAneesh Kumar K.V 
252ee443996SEric Van Hensbergen /**
253ee443996SEric Van Hensbergen  * p9_virtio_probe - probe for existence of 9P virtio channels
254ee443996SEric Van Hensbergen  * @vdev: virtio device to probe
255ee443996SEric Van Hensbergen  *
25637c1209dSAneesh Kumar K.V  * This probes for existing virtio channels.
257ee443996SEric Van Hensbergen  *
258ee443996SEric Van Hensbergen  */
259ee443996SEric Van Hensbergen 
260e2735b77SEric Van Hensbergen static int p9_virtio_probe(struct virtio_device *vdev)
261b530cc79SEric Van Hensbergen {
26297ee9b02SAneesh Kumar K.V 	__u16 tag_len;
26397ee9b02SAneesh Kumar K.V 	char *tag;
264b530cc79SEric Van Hensbergen 	int err;
265b530cc79SEric Van Hensbergen 	struct virtio_chan *chan;
266b530cc79SEric Van Hensbergen 
26737c1209dSAneesh Kumar K.V 	chan = kmalloc(sizeof(struct virtio_chan), GFP_KERNEL);
26837c1209dSAneesh Kumar K.V 	if (!chan) {
26937c1209dSAneesh Kumar K.V 		printk(KERN_ERR "9p: Failed to allocate virtio 9P channel\n");
270b530cc79SEric Van Hensbergen 		err = -ENOMEM;
271b530cc79SEric Van Hensbergen 		goto fail;
272b530cc79SEric Van Hensbergen 	}
273b530cc79SEric Van Hensbergen 
274e2735b77SEric Van Hensbergen 	chan->vdev = vdev;
275e2735b77SEric Van Hensbergen 
276e2735b77SEric Van Hensbergen 	/* We expect one virtqueue, for requests. */
277d2a7dddaSMichael S. Tsirkin 	chan->vq = virtio_find_single_vq(vdev, req_done, "requests");
278e2735b77SEric Van Hensbergen 	if (IS_ERR(chan->vq)) {
279e2735b77SEric Van Hensbergen 		err = PTR_ERR(chan->vq);
280e2735b77SEric Van Hensbergen 		goto out_free_vq;
281b530cc79SEric Van Hensbergen 	}
282e2735b77SEric Van Hensbergen 	chan->vq->vdev->priv = chan;
283e2735b77SEric Van Hensbergen 	spin_lock_init(&chan->lock);
284b530cc79SEric Van Hensbergen 
285e2735b77SEric Van Hensbergen 	sg_init_table(chan->sg, VIRTQUEUE_NUM);
286b530cc79SEric Van Hensbergen 
287b530cc79SEric Van Hensbergen 	chan->inuse = false;
28897ee9b02SAneesh Kumar K.V 	if (virtio_has_feature(vdev, VIRTIO_9P_MOUNT_TAG)) {
28997ee9b02SAneesh Kumar K.V 		vdev->config->get(vdev,
29097ee9b02SAneesh Kumar K.V 				offsetof(struct virtio_9p_config, tag_len),
29197ee9b02SAneesh Kumar K.V 				&tag_len, sizeof(tag_len));
29297ee9b02SAneesh Kumar K.V 	} else {
29397ee9b02SAneesh Kumar K.V 		err = -EINVAL;
29497ee9b02SAneesh Kumar K.V 		goto out_free_vq;
29597ee9b02SAneesh Kumar K.V 	}
29697ee9b02SAneesh Kumar K.V 	tag = kmalloc(tag_len, GFP_KERNEL);
29797ee9b02SAneesh Kumar K.V 	if (!tag) {
29897ee9b02SAneesh Kumar K.V 		err = -ENOMEM;
29997ee9b02SAneesh Kumar K.V 		goto out_free_vq;
30097ee9b02SAneesh Kumar K.V 	}
30197ee9b02SAneesh Kumar K.V 	vdev->config->get(vdev, offsetof(struct virtio_9p_config, tag),
30297ee9b02SAneesh Kumar K.V 			tag, tag_len);
30397ee9b02SAneesh Kumar K.V 	chan->tag = tag;
30497ee9b02SAneesh Kumar K.V 	chan->tag_len = tag_len;
30586c84373SAneesh Kumar K.V 	err = sysfs_create_file(&(vdev->dev.kobj), &dev_attr_mount_tag.attr);
30686c84373SAneesh Kumar K.V 	if (err) {
30786c84373SAneesh Kumar K.V 		kfree(tag);
30886c84373SAneesh Kumar K.V 		goto out_free_vq;
30986c84373SAneesh Kumar K.V 	}
31037c1209dSAneesh Kumar K.V 	mutex_lock(&virtio_9p_lock);
31137c1209dSAneesh Kumar K.V 	list_add_tail(&chan->chan_list, &virtio_chan_list);
31237c1209dSAneesh Kumar K.V 	mutex_unlock(&virtio_9p_lock);
313b530cc79SEric Van Hensbergen 	return 0;
314b530cc79SEric Van Hensbergen 
315e2735b77SEric Van Hensbergen out_free_vq:
316d2a7dddaSMichael S. Tsirkin 	vdev->config->del_vqs(vdev);
31737c1209dSAneesh Kumar K.V 	kfree(chan);
318b530cc79SEric Van Hensbergen fail:
319b530cc79SEric Van Hensbergen 	return err;
320b530cc79SEric Van Hensbergen }
321b530cc79SEric Van Hensbergen 
322ee443996SEric Van Hensbergen 
323ee443996SEric Van Hensbergen /**
324ee443996SEric Van Hensbergen  * p9_virtio_create - allocate a new virtio channel
3258b81ef58SEric Van Hensbergen  * @client: client instance invoking this transport
326ee443996SEric Van Hensbergen  * @devname: string identifying the channel to connect to (unused)
327ee443996SEric Van Hensbergen  * @args: args passed from sys_mount() for per-transport options (unused)
328ee443996SEric Van Hensbergen  *
329ee443996SEric Van Hensbergen  * This sets up a transport channel for 9p communication.  Right now
330b530cc79SEric Van Hensbergen  * we only match the first available channel, but eventually we couldlook up
331b530cc79SEric Van Hensbergen  * alternate channels by matching devname versus a virtio_config entry.
332b530cc79SEric Van Hensbergen  * We use a simple reference count mechanism to ensure that only a single
333ee443996SEric Van Hensbergen  * mount has a channel open at a time.
334ee443996SEric Van Hensbergen  *
335ee443996SEric Van Hensbergen  */
336ee443996SEric Van Hensbergen 
3378b81ef58SEric Van Hensbergen static int
3388b81ef58SEric Van Hensbergen p9_virtio_create(struct p9_client *client, const char *devname, char *args)
339b530cc79SEric Van Hensbergen {
34037c1209dSAneesh Kumar K.V 	struct virtio_chan *chan;
341c1a7c226SAneesh Kumar K.V 	int ret = -ENOENT;
34237c1209dSAneesh Kumar K.V 	int found = 0;
343b530cc79SEric Van Hensbergen 
344c1549497SJosef 'Jeff' Sipek 	mutex_lock(&virtio_9p_lock);
34537c1209dSAneesh Kumar K.V 	list_for_each_entry(chan, &virtio_chan_list, chan_list) {
3460b20406cSSven Eckelmann 		if (!strncmp(devname, chan->tag, chan->tag_len) &&
3470b20406cSSven Eckelmann 		    strlen(devname) == chan->tag_len) {
348f75580c4SAneesh Kumar K.V 			if (!chan->inuse) {
349b530cc79SEric Van Hensbergen 				chan->inuse = true;
35037c1209dSAneesh Kumar K.V 				found = 1;
351b530cc79SEric Van Hensbergen 				break;
352f75580c4SAneesh Kumar K.V 			}
353c1a7c226SAneesh Kumar K.V 			ret = -EBUSY;
354f75580c4SAneesh Kumar K.V 		}
355b530cc79SEric Van Hensbergen 	}
356c1549497SJosef 'Jeff' Sipek 	mutex_unlock(&virtio_9p_lock);
357b530cc79SEric Van Hensbergen 
35837c1209dSAneesh Kumar K.V 	if (!found) {
359e2735b77SEric Van Hensbergen 		printk(KERN_ERR "9p: no channels available\n");
360c1a7c226SAneesh Kumar K.V 		return ret;
361b530cc79SEric Van Hensbergen 	}
362b530cc79SEric Van Hensbergen 
3638b81ef58SEric Van Hensbergen 	client->trans = (void *)chan;
364562ada61SEric Van Hensbergen 	client->status = Connected;
365fea511a6SEric Van Hensbergen 	chan->client = client;
366b530cc79SEric Van Hensbergen 
3678b81ef58SEric Van Hensbergen 	return 0;
368b530cc79SEric Van Hensbergen }
369b530cc79SEric Van Hensbergen 
370ee443996SEric Van Hensbergen /**
371ee443996SEric Van Hensbergen  * p9_virtio_remove - clean up resources associated with a virtio device
372ee443996SEric Van Hensbergen  * @vdev: virtio device to remove
373ee443996SEric Van Hensbergen  *
374ee443996SEric Van Hensbergen  */
375ee443996SEric Van Hensbergen 
376f3933545SEric Van Hensbergen static void p9_virtio_remove(struct virtio_device *vdev)
377f3933545SEric Van Hensbergen {
378f3933545SEric Van Hensbergen 	struct virtio_chan *chan = vdev->priv;
379f3933545SEric Van Hensbergen 
380f3933545SEric Van Hensbergen 	BUG_ON(chan->inuse);
381d2a7dddaSMichael S. Tsirkin 	vdev->config->del_vqs(vdev);
38237c1209dSAneesh Kumar K.V 
38337c1209dSAneesh Kumar K.V 	mutex_lock(&virtio_9p_lock);
38437c1209dSAneesh Kumar K.V 	list_del(&chan->chan_list);
38537c1209dSAneesh Kumar K.V 	mutex_unlock(&virtio_9p_lock);
38686c84373SAneesh Kumar K.V 	sysfs_remove_file(&(vdev->dev.kobj), &dev_attr_mount_tag.attr);
38797ee9b02SAneesh Kumar K.V 	kfree(chan->tag);
38837c1209dSAneesh Kumar K.V 	kfree(chan);
38937c1209dSAneesh Kumar K.V 
390f3933545SEric Van Hensbergen }
391f3933545SEric Van Hensbergen 
392b530cc79SEric Van Hensbergen static struct virtio_device_id id_table[] = {
393b530cc79SEric Van Hensbergen 	{ VIRTIO_ID_9P, VIRTIO_DEV_ANY_ID },
394b530cc79SEric Van Hensbergen 	{ 0 },
395b530cc79SEric Van Hensbergen };
396b530cc79SEric Van Hensbergen 
39797ee9b02SAneesh Kumar K.V static unsigned int features[] = {
39897ee9b02SAneesh Kumar K.V 	VIRTIO_9P_MOUNT_TAG,
39997ee9b02SAneesh Kumar K.V };
40097ee9b02SAneesh Kumar K.V 
401b530cc79SEric Van Hensbergen /* The standard "struct lguest_driver": */
402b530cc79SEric Van Hensbergen static struct virtio_driver p9_virtio_drv = {
40397ee9b02SAneesh Kumar K.V 	.feature_table  = features,
40497ee9b02SAneesh Kumar K.V 	.feature_table_size = ARRAY_SIZE(features),
405b530cc79SEric Van Hensbergen 	.driver.name    = KBUILD_MODNAME,
406b530cc79SEric Van Hensbergen 	.driver.owner	= THIS_MODULE,
407b530cc79SEric Van Hensbergen 	.id_table	= id_table,
408b530cc79SEric Van Hensbergen 	.probe		= p9_virtio_probe,
409f3933545SEric Van Hensbergen 	.remove		= p9_virtio_remove,
410b530cc79SEric Van Hensbergen };
411b530cc79SEric Van Hensbergen 
412b530cc79SEric Van Hensbergen static struct p9_trans_module p9_virtio_trans = {
413b530cc79SEric Van Hensbergen 	.name = "virtio",
414b530cc79SEric Van Hensbergen 	.create = p9_virtio_create,
4158b81ef58SEric Van Hensbergen 	.close = p9_virtio_close,
41691b8534fSEric Van Hensbergen 	.request = p9_virtio_request,
41791b8534fSEric Van Hensbergen 	.cancel = p9_virtio_cancel,
418e2735b77SEric Van Hensbergen 	.maxsize = PAGE_SIZE*16,
419b530cc79SEric Van Hensbergen 	.def = 0,
42072029fe8STejun Heo 	.owner = THIS_MODULE,
421b530cc79SEric Van Hensbergen };
422b530cc79SEric Van Hensbergen 
423b530cc79SEric Van Hensbergen /* The standard init function */
424b530cc79SEric Van Hensbergen static int __init p9_virtio_init(void)
425b530cc79SEric Van Hensbergen {
42637c1209dSAneesh Kumar K.V 	INIT_LIST_HEAD(&virtio_chan_list);
427b530cc79SEric Van Hensbergen 
428b530cc79SEric Van Hensbergen 	v9fs_register_trans(&p9_virtio_trans);
429b530cc79SEric Van Hensbergen 	return register_virtio_driver(&p9_virtio_drv);
430b530cc79SEric Van Hensbergen }
431b530cc79SEric Van Hensbergen 
432f3933545SEric Van Hensbergen static void __exit p9_virtio_cleanup(void)
433f3933545SEric Van Hensbergen {
434f3933545SEric Van Hensbergen 	unregister_virtio_driver(&p9_virtio_drv);
43572029fe8STejun Heo 	v9fs_unregister_trans(&p9_virtio_trans);
436f3933545SEric Van Hensbergen }
437f3933545SEric Van Hensbergen 
438b530cc79SEric Van Hensbergen module_init(p9_virtio_init);
439f3933545SEric Van Hensbergen module_exit(p9_virtio_cleanup);
440b530cc79SEric Van Hensbergen 
441b530cc79SEric Van Hensbergen MODULE_DEVICE_TABLE(virtio, id_table);
442b530cc79SEric Van Hensbergen MODULE_AUTHOR("Eric Van Hensbergen <ericvh@gmail.com>");
443b530cc79SEric Van Hensbergen MODULE_DESCRIPTION("Virtio 9p Transport");
444b530cc79SEric Van Hensbergen MODULE_LICENSE("GPL");
445