xref: /openbmc/linux/net/9p/trans_virtio.c (revision 523adb6cc10b48655c0abe556505240741425b49)
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 
295d385153SJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
305d385153SJoe Perches 
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>
42b9cdc88dSWill Deacon #include <linux/highmem.h>
435a0e3ad6STejun Heo #include <linux/slab.h>
44b530cc79SEric Van Hensbergen #include <net/9p/9p.h>
45b530cc79SEric Van Hensbergen #include <linux/parser.h>
468b81ef58SEric Van Hensbergen #include <net/9p/client.h>
47b530cc79SEric Van Hensbergen #include <net/9p/transport.h>
48b530cc79SEric Van Hensbergen #include <linux/scatterlist.h>
4968da9ba4SVenkateswararao Jujjuri (JV) #include <linux/swap.h>
50b530cc79SEric Van Hensbergen #include <linux/virtio.h>
51b530cc79SEric Van Hensbergen #include <linux/virtio_9p.h>
524038866dSVenkateswararao Jujjuri (JV) #include "trans_common.h"
53b530cc79SEric Van Hensbergen 
54e2735b77SEric Van Hensbergen #define VIRTQUEUE_NUM	128
55e2735b77SEric Van Hensbergen 
56b530cc79SEric Van Hensbergen /* a single mutex to manage channel initialization and attachment */
57c1549497SJosef 'Jeff' Sipek static DEFINE_MUTEX(virtio_9p_lock);
5868da9ba4SVenkateswararao Jujjuri (JV) static DECLARE_WAIT_QUEUE_HEAD(vp_wq);
5968da9ba4SVenkateswararao Jujjuri (JV) static atomic_t vp_pinned = ATOMIC_INIT(0);
60b530cc79SEric Van Hensbergen 
61ee443996SEric Van Hensbergen /**
62ee443996SEric Van Hensbergen  * struct virtio_chan - per-instance transport information
63ee443996SEric Van Hensbergen  * @inuse: whether the channel is in use
64ee443996SEric Van Hensbergen  * @lock: protects multiple elements within this structure
650e15597eSAbhishek Kulkarni  * @client: client instance
66ee443996SEric Van Hensbergen  * @vdev: virtio dev associated with this channel
67ee443996SEric Van Hensbergen  * @vq: virtio queue associated with this channel
68ee443996SEric Van Hensbergen  * @sg: scatter gather list which is used to pack a request (protected?)
69ee443996SEric Van Hensbergen  *
70ee443996SEric Van Hensbergen  * We keep all per-channel information in a structure.
71b530cc79SEric Van Hensbergen  * This structure is allocated within the devices dev->mem space.
72b530cc79SEric Van Hensbergen  * A pointer to the structure will get put in the transport private.
73ee443996SEric Van Hensbergen  *
74b530cc79SEric Van Hensbergen  */
75ee443996SEric Van Hensbergen 
7637c1209dSAneesh Kumar K.V struct virtio_chan {
77ee443996SEric Van Hensbergen 	bool inuse;
78b530cc79SEric Van Hensbergen 
79e2735b77SEric Van Hensbergen 	spinlock_t lock;
80e2735b77SEric Van Hensbergen 
81fea511a6SEric Van Hensbergen 	struct p9_client *client;
82b530cc79SEric Van Hensbergen 	struct virtio_device *vdev;
83e2735b77SEric Van Hensbergen 	struct virtqueue *vq;
8452f44e0dSVenkateswararao Jujjuri (JV) 	int ring_bufs_avail;
8552f44e0dSVenkateswararao Jujjuri (JV) 	wait_queue_head_t *vc_wq;
8668da9ba4SVenkateswararao Jujjuri (JV) 	/* This is global limit. Since we don't have a global structure,
8768da9ba4SVenkateswararao Jujjuri (JV) 	 * will be placing it in each channel.
8868da9ba4SVenkateswararao Jujjuri (JV) 	 */
897293bfbaSZhang Yanfei 	unsigned long p9_max_pages;
90e2735b77SEric Van Hensbergen 	/* Scatterlist: can be too big for stack. */
91e2735b77SEric Van Hensbergen 	struct scatterlist sg[VIRTQUEUE_NUM];
9297ee9b02SAneesh Kumar K.V 	/*
93edcd9d97Spiaojun 	 * tag name to identify a mount null terminated
9497ee9b02SAneesh Kumar K.V 	 */
9597ee9b02SAneesh Kumar K.V 	char *tag;
9697ee9b02SAneesh Kumar K.V 
9737c1209dSAneesh Kumar K.V 	struct list_head chan_list;
9837c1209dSAneesh Kumar K.V };
9937c1209dSAneesh Kumar K.V 
10037c1209dSAneesh Kumar K.V static struct list_head virtio_chan_list;
101b530cc79SEric Van Hensbergen 
102b530cc79SEric Van Hensbergen /* How many bytes left in this page. */
103b530cc79SEric Van Hensbergen static unsigned int rest_of_page(void *data)
104b530cc79SEric Van Hensbergen {
105222e4adeSAl Viro 	return PAGE_SIZE - offset_in_page(data);
106b530cc79SEric Van Hensbergen }
107b530cc79SEric Van Hensbergen 
108ee443996SEric Van Hensbergen /**
109ee443996SEric Van Hensbergen  * p9_virtio_close - reclaim resources of a channel
1100e15597eSAbhishek Kulkarni  * @client: client instance
111ee443996SEric Van Hensbergen  *
112ee443996SEric Van Hensbergen  * This reclaims a channel by freeing its resources and
113ee443996SEric Van Hensbergen  * reseting its inuse flag.
114ee443996SEric Van Hensbergen  *
115ee443996SEric Van Hensbergen  */
116ee443996SEric Van Hensbergen 
1178b81ef58SEric Van Hensbergen static void p9_virtio_close(struct p9_client *client)
118b530cc79SEric Van Hensbergen {
1198b81ef58SEric Van Hensbergen 	struct virtio_chan *chan = client->trans;
120b530cc79SEric Van Hensbergen 
121c1549497SJosef 'Jeff' Sipek 	mutex_lock(&virtio_9p_lock);
122fb786100SAneesh Kumar K.V 	if (chan)
123b530cc79SEric Van Hensbergen 		chan->inuse = false;
124c1549497SJosef 'Jeff' Sipek 	mutex_unlock(&virtio_9p_lock);
125b530cc79SEric Van Hensbergen }
126b530cc79SEric Van Hensbergen 
127ee443996SEric Van Hensbergen /**
128ee443996SEric Van Hensbergen  * req_done - callback which signals activity from the server
129ee443996SEric Van Hensbergen  * @vq: virtio queue activity was received on
130ee443996SEric Van Hensbergen  *
131ee443996SEric Van Hensbergen  * This notifies us that the server has triggered some activity
132ee443996SEric Van Hensbergen  * on the virtio channel - most likely a response to request we
133ee443996SEric Van Hensbergen  * sent.  Figure out which requests now have responses and wake up
134ee443996SEric Van Hensbergen  * those threads.
135ee443996SEric Van Hensbergen  *
136ee443996SEric Van Hensbergen  * Bugs: could do with some additional sanity checking, but appears to work.
137ee443996SEric Van Hensbergen  *
138ee443996SEric Van Hensbergen  */
139ee443996SEric Van Hensbergen 
140e2735b77SEric Van Hensbergen static void req_done(struct virtqueue *vq)
141b530cc79SEric Van Hensbergen {
142e2735b77SEric Van Hensbergen 	struct virtio_chan *chan = vq->vdev->priv;
143e2735b77SEric Van Hensbergen 	unsigned int len;
144e2735b77SEric Van Hensbergen 	struct p9_req_t *req;
14531934da8Sjiangyiwen 	bool need_wakeup = false;
146419b3956SVenkateswararao Jujjuri (JV) 	unsigned long flags;
147b530cc79SEric Van Hensbergen 
1485d385153SJoe Perches 	p9_debug(P9_DEBUG_TRANS, ": request done\n");
14991b8534fSEric Van Hensbergen 
150419b3956SVenkateswararao Jujjuri (JV) 	spin_lock_irqsave(&chan->lock, flags);
15131934da8Sjiangyiwen 	while ((req = virtqueue_get_buf(chan->vq, &len)) != NULL) {
15231934da8Sjiangyiwen 		if (!chan->ring_bufs_avail) {
15352f44e0dSVenkateswararao Jujjuri (JV) 			chan->ring_bufs_avail = 1;
15431934da8Sjiangyiwen 			need_wakeup = true;
15531934da8Sjiangyiwen 		}
15631934da8Sjiangyiwen 
157f984579aSTomas Bortoli 		if (len) {
158*523adb6cSDominique Martinet 			req->rc.size = len;
1592b6e72edSDominique Martinet 			p9_client_cb(chan->client, req, REQ_STATUS_RCVD);
160e2735b77SEric Van Hensbergen 		}
161f984579aSTomas Bortoli 	}
16231934da8Sjiangyiwen 	spin_unlock_irqrestore(&chan->lock, flags);
16331934da8Sjiangyiwen 	/* Wakeup if anyone waiting for VirtIO ring space. */
16431934da8Sjiangyiwen 	if (need_wakeup)
16531934da8Sjiangyiwen 		wake_up(chan->vc_wq);
166b530cc79SEric Van Hensbergen }
167b530cc79SEric Van Hensbergen 
168ee443996SEric Van Hensbergen /**
169ee443996SEric Van Hensbergen  * pack_sg_list - pack a scatter gather list from a linear buffer
170ee443996SEric Van Hensbergen  * @sg: scatter/gather list to pack into
171ee443996SEric Van Hensbergen  * @start: which segment of the sg_list to start at
172ee443996SEric Van Hensbergen  * @limit: maximum segment to pack data to
173ee443996SEric Van Hensbergen  * @data: data to pack into scatter/gather list
174ee443996SEric Van Hensbergen  * @count: amount of data to pack into the scatter/gather list
175ee443996SEric Van Hensbergen  *
176ee443996SEric Van Hensbergen  * sg_lists have multiple segments of various sizes.  This will pack
177ee443996SEric Van Hensbergen  * arbitrary data into an existing scatter gather list, segmenting the
178ee443996SEric Van Hensbergen  * data as necessary within constraints.
179ee443996SEric Van Hensbergen  *
180ee443996SEric Van Hensbergen  */
181ee443996SEric Van Hensbergen 
182abfa034eSAneesh Kumar K.V static int pack_sg_list(struct scatterlist *sg, int start,
183abfa034eSAneesh Kumar K.V 			int limit, char *data, int count)
184e2735b77SEric Van Hensbergen {
185e2735b77SEric Van Hensbergen 	int s;
186e2735b77SEric Van Hensbergen 	int index = start;
187e2735b77SEric Van Hensbergen 
188e2735b77SEric Van Hensbergen 	while (count) {
189e2735b77SEric Van Hensbergen 		s = rest_of_page(data);
190e2735b77SEric Van Hensbergen 		if (s > count)
191e2735b77SEric Van Hensbergen 			s = count;
19223cba9cbSjiangyiwen 		BUG_ON(index >= limit);
1930b36f1adSRusty Russell 		/* Make sure we don't terminate early. */
1940b36f1adSRusty Russell 		sg_unmark_end(&sg[index]);
195e2735b77SEric Van Hensbergen 		sg_set_buf(&sg[index++], data, s);
196e2735b77SEric Van Hensbergen 		count -= s;
197e2735b77SEric Van Hensbergen 		data += s;
198e2735b77SEric Van Hensbergen 	}
1990b36f1adSRusty Russell 	if (index-start)
2000b36f1adSRusty Russell 		sg_mark_end(&sg[index - 1]);
201e2735b77SEric Van Hensbergen 	return index-start;
202e2735b77SEric Van Hensbergen }
203e2735b77SEric Van Hensbergen 
20491b8534fSEric Van Hensbergen /* We don't currently allow canceling of virtio requests */
20591b8534fSEric Van Hensbergen static int p9_virtio_cancel(struct p9_client *client, struct p9_req_t *req)
20691b8534fSEric Van Hensbergen {
20791b8534fSEric Van Hensbergen 	return 1;
20891b8534fSEric Van Hensbergen }
20991b8534fSEric Van Hensbergen 
210ee443996SEric Van Hensbergen /**
2114038866dSVenkateswararao Jujjuri (JV)  * pack_sg_list_p - Just like pack_sg_list. Instead of taking a buffer,
2124038866dSVenkateswararao Jujjuri (JV)  * this takes a list of pages.
2134038866dSVenkateswararao Jujjuri (JV)  * @sg: scatter/gather list to pack into
2144038866dSVenkateswararao Jujjuri (JV)  * @start: which segment of the sg_list to start at
2152c53040fSBen Hutchings  * @pdata: a list of pages to add into sg.
216abfa034eSAneesh Kumar K.V  * @nr_pages: number of pages to pack into the scatter/gather list
2174f3b35c1SAl Viro  * @offs: amount of data in the beginning of first page _not_ to pack
2184038866dSVenkateswararao Jujjuri (JV)  * @count: amount of data to pack into the scatter/gather list
2194038866dSVenkateswararao Jujjuri (JV)  */
2204038866dSVenkateswararao Jujjuri (JV) static int
221abfa034eSAneesh Kumar K.V pack_sg_list_p(struct scatterlist *sg, int start, int limit,
2224f3b35c1SAl Viro 	       struct page **pdata, int nr_pages, size_t offs, int count)
2234038866dSVenkateswararao Jujjuri (JV) {
224abfa034eSAneesh Kumar K.V 	int i = 0, s;
2254f3b35c1SAl Viro 	int data_off = offs;
2264038866dSVenkateswararao Jujjuri (JV) 	int index = start;
2274038866dSVenkateswararao Jujjuri (JV) 
228abfa034eSAneesh Kumar K.V 	BUG_ON(nr_pages > (limit - start));
229abfa034eSAneesh Kumar K.V 	/*
230abfa034eSAneesh Kumar K.V 	 * if the first page doesn't start at
231abfa034eSAneesh Kumar K.V 	 * page boundary find the offset
232abfa034eSAneesh Kumar K.V 	 */
233abfa034eSAneesh Kumar K.V 	while (nr_pages) {
2344f3b35c1SAl Viro 		s = PAGE_SIZE - data_off;
235abfa034eSAneesh Kumar K.V 		if (s > count)
236abfa034eSAneesh Kumar K.V 			s = count;
23723cba9cbSjiangyiwen 		BUG_ON(index >= limit);
2380b36f1adSRusty Russell 		/* Make sure we don't terminate early. */
2390b36f1adSRusty Russell 		sg_unmark_end(&sg[index]);
240abfa034eSAneesh Kumar K.V 		sg_set_page(&sg[index++], pdata[i++], s, data_off);
241abfa034eSAneesh Kumar K.V 		data_off = 0;
2424038866dSVenkateswararao Jujjuri (JV) 		count -= s;
243abfa034eSAneesh Kumar K.V 		nr_pages--;
2444038866dSVenkateswararao Jujjuri (JV) 	}
2450b36f1adSRusty Russell 
2460b36f1adSRusty Russell 	if (index-start)
2470b36f1adSRusty Russell 		sg_mark_end(&sg[index - 1]);
2484038866dSVenkateswararao Jujjuri (JV) 	return index - start;
2494038866dSVenkateswararao Jujjuri (JV) }
2504038866dSVenkateswararao Jujjuri (JV) 
2514038866dSVenkateswararao Jujjuri (JV) /**
25291b8534fSEric Van Hensbergen  * p9_virtio_request - issue a request
2530e15597eSAbhishek Kulkarni  * @client: client instance issuing the request
2540e15597eSAbhishek Kulkarni  * @req: request to be issued
255ee443996SEric Van Hensbergen  *
256ee443996SEric Van Hensbergen  */
257ee443996SEric Van Hensbergen 
258e2735b77SEric Van Hensbergen static int
25991b8534fSEric Van Hensbergen p9_virtio_request(struct p9_client *client, struct p9_req_t *req)
260e2735b77SEric Van Hensbergen {
261abfa034eSAneesh Kumar K.V 	int err;
2620b36f1adSRusty Russell 	int in, out, out_sgs, in_sgs;
263419b3956SVenkateswararao Jujjuri (JV) 	unsigned long flags;
264abfa034eSAneesh Kumar K.V 	struct virtio_chan *chan = client->trans;
2650b36f1adSRusty Russell 	struct scatterlist *sgs[2];
266e2735b77SEric Van Hensbergen 
2675d385153SJoe Perches 	p9_debug(P9_DEBUG_TRANS, "9p debug: virtio request\n");
268e2735b77SEric Van Hensbergen 
269419b3956SVenkateswararao Jujjuri (JV) 	req->status = REQ_STATUS_SENT;
270abfa034eSAneesh Kumar K.V req_retry:
271419b3956SVenkateswararao Jujjuri (JV) 	spin_lock_irqsave(&chan->lock, flags);
2724038866dSVenkateswararao Jujjuri (JV) 
2730b36f1adSRusty Russell 	out_sgs = in_sgs = 0;
2744038866dSVenkateswararao Jujjuri (JV) 	/* Handle out VirtIO ring buffers */
275abfa034eSAneesh Kumar K.V 	out = pack_sg_list(chan->sg, 0,
276*523adb6cSDominique Martinet 			   VIRTQUEUE_NUM, req->tc.sdata, req->tc.size);
2770b36f1adSRusty Russell 	if (out)
2780b36f1adSRusty Russell 		sgs[out_sgs++] = chan->sg;
2794038866dSVenkateswararao Jujjuri (JV) 
280abfa034eSAneesh Kumar K.V 	in = pack_sg_list(chan->sg, out,
281*523adb6cSDominique Martinet 			  VIRTQUEUE_NUM, req->rc.sdata, req->rc.capacity);
2820b36f1adSRusty Russell 	if (in)
2830b36f1adSRusty Russell 		sgs[out_sgs + in_sgs++] = chan->sg + out;
284e2735b77SEric Van Hensbergen 
285474fe9f7SAl Viro 	err = virtqueue_add_sgs(chan->vq, sgs, out_sgs, in_sgs, req,
286f96fde41SRusty Russell 				GFP_ATOMIC);
287419b3956SVenkateswararao Jujjuri (JV) 	if (err < 0) {
28852f44e0dSVenkateswararao Jujjuri (JV) 		if (err == -ENOSPC) {
28952f44e0dSVenkateswararao Jujjuri (JV) 			chan->ring_bufs_avail = 0;
29052f44e0dSVenkateswararao Jujjuri (JV) 			spin_unlock_irqrestore(&chan->lock, flags);
2919523feacSTuomas Tynkkynen 			err = wait_event_killable(*chan->vc_wq,
29252f44e0dSVenkateswararao Jujjuri (JV) 						  chan->ring_bufs_avail);
29352f44e0dSVenkateswararao Jujjuri (JV) 			if (err  == -ERESTARTSYS)
29452f44e0dSVenkateswararao Jujjuri (JV) 				return err;
29552f44e0dSVenkateswararao Jujjuri (JV) 
2965d385153SJoe Perches 			p9_debug(P9_DEBUG_TRANS, "Retry virtio request\n");
297abfa034eSAneesh Kumar K.V 			goto req_retry;
298abfa034eSAneesh Kumar K.V 		} else {
299abfa034eSAneesh Kumar K.V 			spin_unlock_irqrestore(&chan->lock, flags);
3005d385153SJoe Perches 			p9_debug(P9_DEBUG_TRANS,
3010b36f1adSRusty Russell 				 "virtio rpc add_sgs returned failure\n");
302abfa034eSAneesh Kumar K.V 			return -EIO;
303abfa034eSAneesh Kumar K.V 		}
304abfa034eSAneesh Kumar K.V 	}
305abfa034eSAneesh Kumar K.V 	virtqueue_kick(chan->vq);
306abfa034eSAneesh Kumar K.V 	spin_unlock_irqrestore(&chan->lock, flags);
307abfa034eSAneesh Kumar K.V 
3085d385153SJoe Perches 	p9_debug(P9_DEBUG_TRANS, "virtio request kicked\n");
309abfa034eSAneesh Kumar K.V 	return 0;
310abfa034eSAneesh Kumar K.V }
311abfa034eSAneesh Kumar K.V 
312abfa034eSAneesh Kumar K.V static int p9_get_mapped_pages(struct virtio_chan *chan,
3134f3b35c1SAl Viro 			       struct page ***pages,
3144f3b35c1SAl Viro 			       struct iov_iter *data,
3154f3b35c1SAl Viro 			       int count,
3164f3b35c1SAl Viro 			       size_t *offs,
3174f3b35c1SAl Viro 			       int *need_drop)
318abfa034eSAneesh Kumar K.V {
3194f3b35c1SAl Viro 	int nr_pages;
320abfa034eSAneesh Kumar K.V 	int err;
3214f3b35c1SAl Viro 
3224f3b35c1SAl Viro 	if (!iov_iter_count(data))
3234f3b35c1SAl Viro 		return 0;
3244f3b35c1SAl Viro 
3254f3b35c1SAl Viro 	if (!(data->type & ITER_KVEC)) {
3264f3b35c1SAl Viro 		int n;
327abfa034eSAneesh Kumar K.V 		/*
328abfa034eSAneesh Kumar K.V 		 * We allow only p9_max_pages pinned. We wait for the
329abfa034eSAneesh Kumar K.V 		 * Other zc request to finish here
330abfa034eSAneesh Kumar K.V 		 */
331abfa034eSAneesh Kumar K.V 		if (atomic_read(&vp_pinned) >= chan->p9_max_pages) {
3329523feacSTuomas Tynkkynen 			err = wait_event_killable(vp_wq,
333abfa034eSAneesh Kumar K.V 			      (atomic_read(&vp_pinned) < chan->p9_max_pages));
334abfa034eSAneesh Kumar K.V 			if (err == -ERESTARTSYS)
335abfa034eSAneesh Kumar K.V 				return err;
336abfa034eSAneesh Kumar K.V 		}
3374f3b35c1SAl Viro 		n = iov_iter_get_pages_alloc(data, pages, count, offs);
3384f3b35c1SAl Viro 		if (n < 0)
3394f3b35c1SAl Viro 			return n;
3404f3b35c1SAl Viro 		*need_drop = 1;
3414f3b35c1SAl Viro 		nr_pages = DIV_ROUND_UP(n + *offs, PAGE_SIZE);
342abfa034eSAneesh Kumar K.V 		atomic_add(nr_pages, &vp_pinned);
3434f3b35c1SAl Viro 		return n;
344abfa034eSAneesh Kumar K.V 	} else {
345abfa034eSAneesh Kumar K.V 		/* kernel buffer, no need to pin pages */
3464f3b35c1SAl Viro 		int index;
3474f3b35c1SAl Viro 		size_t len;
3484f3b35c1SAl Viro 		void *p;
3494f3b35c1SAl Viro 
3504f3b35c1SAl Viro 		/* we'd already checked that it's non-empty */
3514f3b35c1SAl Viro 		while (1) {
3524f3b35c1SAl Viro 			len = iov_iter_single_seg_count(data);
3534f3b35c1SAl Viro 			if (likely(len)) {
3544f3b35c1SAl Viro 				p = data->kvec->iov_base + data->iov_offset;
3554f3b35c1SAl Viro 				break;
3564f3b35c1SAl Viro 			}
3574f3b35c1SAl Viro 			iov_iter_advance(data, 0);
3584f3b35c1SAl Viro 		}
3594f3b35c1SAl Viro 		if (len > count)
3604f3b35c1SAl Viro 			len = count;
3614f3b35c1SAl Viro 
3624f3b35c1SAl Viro 		nr_pages = DIV_ROUND_UP((unsigned long)p + len, PAGE_SIZE) -
3634f3b35c1SAl Viro 			   (unsigned long)p / PAGE_SIZE;
3644f3b35c1SAl Viro 
3656da2ec56SKees Cook 		*pages = kmalloc_array(nr_pages, sizeof(struct page *),
3666da2ec56SKees Cook 				       GFP_NOFS);
3674f3b35c1SAl Viro 		if (!*pages)
3684f3b35c1SAl Viro 			return -ENOMEM;
3694f3b35c1SAl Viro 
3704f3b35c1SAl Viro 		*need_drop = 0;
371222e4adeSAl Viro 		p -= (*offs = offset_in_page(p));
3724f3b35c1SAl Viro 		for (index = 0; index < nr_pages; index++) {
3734f3b35c1SAl Viro 			if (is_vmalloc_addr(p))
3744f3b35c1SAl Viro 				(*pages)[index] = vmalloc_to_page(p);
375b6f52ae2SRichard Yao 			else
3764f3b35c1SAl Viro 				(*pages)[index] = kmap_to_page(p);
3774f3b35c1SAl Viro 			p += PAGE_SIZE;
378abfa034eSAneesh Kumar K.V 		}
3794f3b35c1SAl Viro 		return len;
380abfa034eSAneesh Kumar K.V 	}
381abfa034eSAneesh Kumar K.V }
382abfa034eSAneesh Kumar K.V 
383abfa034eSAneesh Kumar K.V /**
384abfa034eSAneesh Kumar K.V  * p9_virtio_zc_request - issue a zero copy request
385abfa034eSAneesh Kumar K.V  * @client: client instance issuing the request
386abfa034eSAneesh Kumar K.V  * @req: request to be issued
387c7ebbae7Spiaojun  * @uidata: user buffer that should be used for zero copy read
388c7ebbae7Spiaojun  * @uodata: user buffer that should be used for zero copy write
389abfa034eSAneesh Kumar K.V  * @inlen: read buffer size
3904a026da9SSun Lianwen  * @outlen: write buffer size
3914a026da9SSun Lianwen  * @in_hdr_len: reader header size, This is the size of response protocol data
392abfa034eSAneesh Kumar K.V  *
393abfa034eSAneesh Kumar K.V  */
394abfa034eSAneesh Kumar K.V static int
395abfa034eSAneesh Kumar K.V p9_virtio_zc_request(struct p9_client *client, struct p9_req_t *req,
3964f3b35c1SAl Viro 		     struct iov_iter *uidata, struct iov_iter *uodata,
3974f3b35c1SAl Viro 		     int inlen, int outlen, int in_hdr_len)
398abfa034eSAneesh Kumar K.V {
3990b36f1adSRusty Russell 	int in, out, err, out_sgs, in_sgs;
400abfa034eSAneesh Kumar K.V 	unsigned long flags;
401abfa034eSAneesh Kumar K.V 	int in_nr_pages = 0, out_nr_pages = 0;
402abfa034eSAneesh Kumar K.V 	struct page **in_pages = NULL, **out_pages = NULL;
403abfa034eSAneesh Kumar K.V 	struct virtio_chan *chan = client->trans;
4040b36f1adSRusty Russell 	struct scatterlist *sgs[4];
4054f3b35c1SAl Viro 	size_t offs;
4064f3b35c1SAl Viro 	int need_drop = 0;
407abfa034eSAneesh Kumar K.V 
4085d385153SJoe Perches 	p9_debug(P9_DEBUG_TRANS, "virtio request\n");
409abfa034eSAneesh Kumar K.V 
410abfa034eSAneesh Kumar K.V 	if (uodata) {
411d28c756cSChirantan Ekbote 		__le32 sz;
4124f3b35c1SAl Viro 		int n = p9_get_mapped_pages(chan, &out_pages, uodata,
4134f3b35c1SAl Viro 					    outlen, &offs, &need_drop);
4144f3b35c1SAl Viro 		if (n < 0)
4154f3b35c1SAl Viro 			return n;
4164f3b35c1SAl Viro 		out_nr_pages = DIV_ROUND_UP(n + offs, PAGE_SIZE);
4174f3b35c1SAl Viro 		if (n != outlen) {
4184f3b35c1SAl Viro 			__le32 v = cpu_to_le32(n);
419*523adb6cSDominique Martinet 			memcpy(&req->tc.sdata[req->tc.size - 4], &v, 4);
4204f3b35c1SAl Viro 			outlen = n;
421abfa034eSAneesh Kumar K.V 		}
422d28c756cSChirantan Ekbote 		/* The size field of the message must include the length of the
423d28c756cSChirantan Ekbote 		 * header and the length of the data.  We didn't actually know
424d28c756cSChirantan Ekbote 		 * the length of the data until this point so add it in now.
425d28c756cSChirantan Ekbote 		 */
426*523adb6cSDominique Martinet 		sz = cpu_to_le32(req->tc.size + outlen);
427*523adb6cSDominique Martinet 		memcpy(&req->tc.sdata[0], &sz, sizeof(sz));
4284f3b35c1SAl Viro 	} else if (uidata) {
4294f3b35c1SAl Viro 		int n = p9_get_mapped_pages(chan, &in_pages, uidata,
4304f3b35c1SAl Viro 					    inlen, &offs, &need_drop);
4314f3b35c1SAl Viro 		if (n < 0)
4324f3b35c1SAl Viro 			return n;
4334f3b35c1SAl Viro 		in_nr_pages = DIV_ROUND_UP(n + offs, PAGE_SIZE);
4344f3b35c1SAl Viro 		if (n != inlen) {
4354f3b35c1SAl Viro 			__le32 v = cpu_to_le32(n);
436*523adb6cSDominique Martinet 			memcpy(&req->tc.sdata[req->tc.size - 4], &v, 4);
4374f3b35c1SAl Viro 			inlen = n;
438abfa034eSAneesh Kumar K.V 		}
439abfa034eSAneesh Kumar K.V 	}
440abfa034eSAneesh Kumar K.V 	req->status = REQ_STATUS_SENT;
441abfa034eSAneesh Kumar K.V req_retry_pinned:
442abfa034eSAneesh Kumar K.V 	spin_lock_irqsave(&chan->lock, flags);
4430b36f1adSRusty Russell 
4440b36f1adSRusty Russell 	out_sgs = in_sgs = 0;
4450b36f1adSRusty Russell 
446abfa034eSAneesh Kumar K.V 	/* out data */
447abfa034eSAneesh Kumar K.V 	out = pack_sg_list(chan->sg, 0,
448*523adb6cSDominique Martinet 			   VIRTQUEUE_NUM, req->tc.sdata, req->tc.size);
449abfa034eSAneesh Kumar K.V 
4500b36f1adSRusty Russell 	if (out)
4510b36f1adSRusty Russell 		sgs[out_sgs++] = chan->sg;
4520b36f1adSRusty Russell 
4530b36f1adSRusty Russell 	if (out_pages) {
4540b36f1adSRusty Russell 		sgs[out_sgs++] = chan->sg + out;
455abfa034eSAneesh Kumar K.V 		out += pack_sg_list_p(chan->sg, out, VIRTQUEUE_NUM,
4564f3b35c1SAl Viro 				      out_pages, out_nr_pages, offs, outlen);
4570b36f1adSRusty Russell 	}
4580b36f1adSRusty Russell 
459abfa034eSAneesh Kumar K.V 	/*
460abfa034eSAneesh Kumar K.V 	 * Take care of in data
461abfa034eSAneesh Kumar K.V 	 * For example TREAD have 11.
462abfa034eSAneesh Kumar K.V 	 * 11 is the read/write header = PDU Header(7) + IO Size (4).
463abfa034eSAneesh Kumar K.V 	 * Arrange in such a way that server places header in the
464abfa034eSAneesh Kumar K.V 	 * alloced memory and payload onto the user buffer.
465abfa034eSAneesh Kumar K.V 	 */
466abfa034eSAneesh Kumar K.V 	in = pack_sg_list(chan->sg, out,
467*523adb6cSDominique Martinet 			  VIRTQUEUE_NUM, req->rc.sdata, in_hdr_len);
4680b36f1adSRusty Russell 	if (in)
4690b36f1adSRusty Russell 		sgs[out_sgs + in_sgs++] = chan->sg + out;
4700b36f1adSRusty Russell 
4710b36f1adSRusty Russell 	if (in_pages) {
4720b36f1adSRusty Russell 		sgs[out_sgs + in_sgs++] = chan->sg + out + in;
473abfa034eSAneesh Kumar K.V 		in += pack_sg_list_p(chan->sg, out + in, VIRTQUEUE_NUM,
4744f3b35c1SAl Viro 				     in_pages, in_nr_pages, offs, inlen);
4750b36f1adSRusty Russell 	}
476abfa034eSAneesh Kumar K.V 
4770b36f1adSRusty Russell 	BUG_ON(out_sgs + in_sgs > ARRAY_SIZE(sgs));
478474fe9f7SAl Viro 	err = virtqueue_add_sgs(chan->vq, sgs, out_sgs, in_sgs, req,
479f96fde41SRusty Russell 				GFP_ATOMIC);
480abfa034eSAneesh Kumar K.V 	if (err < 0) {
481abfa034eSAneesh Kumar K.V 		if (err == -ENOSPC) {
482abfa034eSAneesh Kumar K.V 			chan->ring_bufs_avail = 0;
483abfa034eSAneesh Kumar K.V 			spin_unlock_irqrestore(&chan->lock, flags);
4849523feacSTuomas Tynkkynen 			err = wait_event_killable(*chan->vc_wq,
485abfa034eSAneesh Kumar K.V 						  chan->ring_bufs_avail);
486abfa034eSAneesh Kumar K.V 			if (err  == -ERESTARTSYS)
487abfa034eSAneesh Kumar K.V 				goto err_out;
488abfa034eSAneesh Kumar K.V 
4895d385153SJoe Perches 			p9_debug(P9_DEBUG_TRANS, "Retry virtio request\n");
490316ad550SVenkateswararao Jujjuri (JV) 			goto req_retry_pinned;
49152f44e0dSVenkateswararao Jujjuri (JV) 		} else {
492419b3956SVenkateswararao Jujjuri (JV) 			spin_unlock_irqrestore(&chan->lock, flags);
4935d385153SJoe Perches 			p9_debug(P9_DEBUG_TRANS,
4940b36f1adSRusty Russell 				 "virtio rpc add_sgs returned failure\n");
495abfa034eSAneesh Kumar K.V 			err = -EIO;
496abfa034eSAneesh Kumar K.V 			goto err_out;
497e2735b77SEric Van Hensbergen 		}
49852f44e0dSVenkateswararao Jujjuri (JV) 	}
499dc3f5e68SMichael S. Tsirkin 	virtqueue_kick(chan->vq);
500419b3956SVenkateswararao Jujjuri (JV) 	spin_unlock_irqrestore(&chan->lock, flags);
5015d385153SJoe Perches 	p9_debug(P9_DEBUG_TRANS, "virtio request kicked\n");
5022557d0c5SMatthew Wilcox 	err = wait_event_killable(req->wq, req->status >= REQ_STATUS_RCVD);
503abfa034eSAneesh Kumar K.V 	/*
504abfa034eSAneesh Kumar K.V 	 * Non kernel buffers are pinned, unpin them
505abfa034eSAneesh Kumar K.V 	 */
506abfa034eSAneesh Kumar K.V err_out:
5074f3b35c1SAl Viro 	if (need_drop) {
508abfa034eSAneesh Kumar K.V 		if (in_pages) {
509abfa034eSAneesh Kumar K.V 			p9_release_pages(in_pages, in_nr_pages);
510abfa034eSAneesh Kumar K.V 			atomic_sub(in_nr_pages, &vp_pinned);
511abfa034eSAneesh Kumar K.V 		}
512abfa034eSAneesh Kumar K.V 		if (out_pages) {
513abfa034eSAneesh Kumar K.V 			p9_release_pages(out_pages, out_nr_pages);
514abfa034eSAneesh Kumar K.V 			atomic_sub(out_nr_pages, &vp_pinned);
515abfa034eSAneesh Kumar K.V 		}
516abfa034eSAneesh Kumar K.V 		/* wakeup anybody waiting for slots to pin pages */
517abfa034eSAneesh Kumar K.V 		wake_up(&vp_wq);
518abfa034eSAneesh Kumar K.V 	}
5191b8553c0SVegard Nossum 	kvfree(in_pages);
5201b8553c0SVegard Nossum 	kvfree(out_pages);
521abfa034eSAneesh Kumar K.V 	return err;
522e2735b77SEric Van Hensbergen }
523e2735b77SEric Van Hensbergen 
52486c84373SAneesh Kumar K.V static ssize_t p9_mount_tag_show(struct device *dev,
52586c84373SAneesh Kumar K.V 				struct device_attribute *attr, char *buf)
52686c84373SAneesh Kumar K.V {
52786c84373SAneesh Kumar K.V 	struct virtio_chan *chan;
52886c84373SAneesh Kumar K.V 	struct virtio_device *vdev;
529edcd9d97Spiaojun 	int tag_len;
53086c84373SAneesh Kumar K.V 
53186c84373SAneesh Kumar K.V 	vdev = dev_to_virtio(dev);
53286c84373SAneesh Kumar K.V 	chan = vdev->priv;
533edcd9d97Spiaojun 	tag_len = strlen(chan->tag);
53486c84373SAneesh Kumar K.V 
535edcd9d97Spiaojun 	memcpy(buf, chan->tag, tag_len + 1);
536179a5bc4SAndrey Ryabinin 
537edcd9d97Spiaojun 	return tag_len + 1;
53886c84373SAneesh Kumar K.V }
53986c84373SAneesh Kumar K.V 
54086c84373SAneesh Kumar K.V static DEVICE_ATTR(mount_tag, 0444, p9_mount_tag_show, NULL);
54186c84373SAneesh Kumar K.V 
542ee443996SEric Van Hensbergen /**
543ee443996SEric Van Hensbergen  * p9_virtio_probe - probe for existence of 9P virtio channels
544ee443996SEric Van Hensbergen  * @vdev: virtio device to probe
545ee443996SEric Van Hensbergen  *
54637c1209dSAneesh Kumar K.V  * This probes for existing virtio channels.
547ee443996SEric Van Hensbergen  *
548ee443996SEric Van Hensbergen  */
549ee443996SEric Van Hensbergen 
550e2735b77SEric Van Hensbergen static int p9_virtio_probe(struct virtio_device *vdev)
551b530cc79SEric Van Hensbergen {
55297ee9b02SAneesh Kumar K.V 	__u16 tag_len;
55397ee9b02SAneesh Kumar K.V 	char *tag;
554b530cc79SEric Van Hensbergen 	int err;
555b530cc79SEric Van Hensbergen 	struct virtio_chan *chan;
556b530cc79SEric Van Hensbergen 
5577754f53eSMichael S. Tsirkin 	if (!vdev->config->get) {
5587754f53eSMichael S. Tsirkin 		dev_err(&vdev->dev, "%s failure: config access disabled\n",
5597754f53eSMichael S. Tsirkin 			__func__);
5607754f53eSMichael S. Tsirkin 		return -EINVAL;
5617754f53eSMichael S. Tsirkin 	}
5627754f53eSMichael S. Tsirkin 
56337c1209dSAneesh Kumar K.V 	chan = kmalloc(sizeof(struct virtio_chan), GFP_KERNEL);
56437c1209dSAneesh Kumar K.V 	if (!chan) {
5655d385153SJoe Perches 		pr_err("Failed to allocate virtio 9P channel\n");
566b530cc79SEric Van Hensbergen 		err = -ENOMEM;
567b530cc79SEric Van Hensbergen 		goto fail;
568b530cc79SEric Van Hensbergen 	}
569b530cc79SEric Van Hensbergen 
570e2735b77SEric Van Hensbergen 	chan->vdev = vdev;
571e2735b77SEric Van Hensbergen 
572e2735b77SEric Van Hensbergen 	/* We expect one virtqueue, for requests. */
573d2a7dddaSMichael S. Tsirkin 	chan->vq = virtio_find_single_vq(vdev, req_done, "requests");
574e2735b77SEric Van Hensbergen 	if (IS_ERR(chan->vq)) {
575e2735b77SEric Van Hensbergen 		err = PTR_ERR(chan->vq);
57692aef467SJean-Philippe Brucker 		goto out_free_chan;
577b530cc79SEric Van Hensbergen 	}
578e2735b77SEric Van Hensbergen 	chan->vq->vdev->priv = chan;
579e2735b77SEric Van Hensbergen 	spin_lock_init(&chan->lock);
580b530cc79SEric Van Hensbergen 
581e2735b77SEric Van Hensbergen 	sg_init_table(chan->sg, VIRTQUEUE_NUM);
582b530cc79SEric Van Hensbergen 
583b530cc79SEric Van Hensbergen 	chan->inuse = false;
58497ee9b02SAneesh Kumar K.V 	if (virtio_has_feature(vdev, VIRTIO_9P_MOUNT_TAG)) {
585855e0c52SRusty Russell 		virtio_cread(vdev, struct virtio_9p_config, tag_len, &tag_len);
58697ee9b02SAneesh Kumar K.V 	} else {
58797ee9b02SAneesh Kumar K.V 		err = -EINVAL;
58897ee9b02SAneesh Kumar K.V 		goto out_free_vq;
58997ee9b02SAneesh Kumar K.V 	}
590edcd9d97Spiaojun 	tag = kzalloc(tag_len + 1, GFP_KERNEL);
59197ee9b02SAneesh Kumar K.V 	if (!tag) {
59297ee9b02SAneesh Kumar K.V 		err = -ENOMEM;
59397ee9b02SAneesh Kumar K.V 		goto out_free_vq;
59497ee9b02SAneesh Kumar K.V 	}
595855e0c52SRusty Russell 
596855e0c52SRusty Russell 	virtio_cread_bytes(vdev, offsetof(struct virtio_9p_config, tag),
59797ee9b02SAneesh Kumar K.V 			   tag, tag_len);
59897ee9b02SAneesh Kumar K.V 	chan->tag = tag;
59986c84373SAneesh Kumar K.V 	err = sysfs_create_file(&(vdev->dev.kobj), &dev_attr_mount_tag.attr);
60086c84373SAneesh Kumar K.V 	if (err) {
60152f44e0dSVenkateswararao Jujjuri (JV) 		goto out_free_tag;
60286c84373SAneesh Kumar K.V 	}
60352f44e0dSVenkateswararao Jujjuri (JV) 	chan->vc_wq = kmalloc(sizeof(wait_queue_head_t), GFP_KERNEL);
60452f44e0dSVenkateswararao Jujjuri (JV) 	if (!chan->vc_wq) {
60552f44e0dSVenkateswararao Jujjuri (JV) 		err = -ENOMEM;
60652f44e0dSVenkateswararao Jujjuri (JV) 		goto out_free_tag;
60752f44e0dSVenkateswararao Jujjuri (JV) 	}
60852f44e0dSVenkateswararao Jujjuri (JV) 	init_waitqueue_head(chan->vc_wq);
60952f44e0dSVenkateswararao Jujjuri (JV) 	chan->ring_bufs_avail = 1;
61068da9ba4SVenkateswararao Jujjuri (JV) 	/* Ceiling limit to avoid denial of service attacks */
61168da9ba4SVenkateswararao Jujjuri (JV) 	chan->p9_max_pages = nr_free_buffer_pages()/4;
61252f44e0dSVenkateswararao Jujjuri (JV) 
61364b4cc39SMichael S. Tsirkin 	virtio_device_ready(vdev);
61464b4cc39SMichael S. Tsirkin 
61537c1209dSAneesh Kumar K.V 	mutex_lock(&virtio_9p_lock);
61637c1209dSAneesh Kumar K.V 	list_add_tail(&chan->chan_list, &virtio_chan_list);
61737c1209dSAneesh Kumar K.V 	mutex_unlock(&virtio_9p_lock);
618e0d6cb9cSMichael Marineau 
619e0d6cb9cSMichael Marineau 	/* Let udev rules use the new mount_tag attribute. */
620e0d6cb9cSMichael Marineau 	kobject_uevent(&(vdev->dev.kobj), KOBJ_CHANGE);
621e0d6cb9cSMichael Marineau 
622b530cc79SEric Van Hensbergen 	return 0;
623b530cc79SEric Van Hensbergen 
62452f44e0dSVenkateswararao Jujjuri (JV) out_free_tag:
62552f44e0dSVenkateswararao Jujjuri (JV) 	kfree(tag);
626e2735b77SEric Van Hensbergen out_free_vq:
627d2a7dddaSMichael S. Tsirkin 	vdev->config->del_vqs(vdev);
62892aef467SJean-Philippe Brucker out_free_chan:
62937c1209dSAneesh Kumar K.V 	kfree(chan);
630b530cc79SEric Van Hensbergen fail:
631b530cc79SEric Van Hensbergen 	return err;
632b530cc79SEric Van Hensbergen }
633b530cc79SEric Van Hensbergen 
634ee443996SEric Van Hensbergen 
635ee443996SEric Van Hensbergen /**
636ee443996SEric Van Hensbergen  * p9_virtio_create - allocate a new virtio channel
6378b81ef58SEric Van Hensbergen  * @client: client instance invoking this transport
638ee443996SEric Van Hensbergen  * @devname: string identifying the channel to connect to (unused)
639ee443996SEric Van Hensbergen  * @args: args passed from sys_mount() for per-transport options (unused)
640ee443996SEric Van Hensbergen  *
641ee443996SEric Van Hensbergen  * This sets up a transport channel for 9p communication.  Right now
642b530cc79SEric Van Hensbergen  * we only match the first available channel, but eventually we couldlook up
643b530cc79SEric Van Hensbergen  * alternate channels by matching devname versus a virtio_config entry.
644b530cc79SEric Van Hensbergen  * We use a simple reference count mechanism to ensure that only a single
645ee443996SEric Van Hensbergen  * mount has a channel open at a time.
646ee443996SEric Van Hensbergen  *
647ee443996SEric Van Hensbergen  */
648ee443996SEric Van Hensbergen 
6498b81ef58SEric Van Hensbergen static int
6508b81ef58SEric Van Hensbergen p9_virtio_create(struct p9_client *client, const char *devname, char *args)
651b530cc79SEric Van Hensbergen {
65237c1209dSAneesh Kumar K.V 	struct virtio_chan *chan;
653c1a7c226SAneesh Kumar K.V 	int ret = -ENOENT;
65437c1209dSAneesh Kumar K.V 	int found = 0;
655b530cc79SEric Van Hensbergen 
65610aa1452STomas Bortoli 	if (devname == NULL)
65710aa1452STomas Bortoli 		return -EINVAL;
65810aa1452STomas Bortoli 
659c1549497SJosef 'Jeff' Sipek 	mutex_lock(&virtio_9p_lock);
66037c1209dSAneesh Kumar K.V 	list_for_each_entry(chan, &virtio_chan_list, chan_list) {
661edcd9d97Spiaojun 		if (!strcmp(devname, chan->tag)) {
662f75580c4SAneesh Kumar K.V 			if (!chan->inuse) {
663b530cc79SEric Van Hensbergen 				chan->inuse = true;
66437c1209dSAneesh Kumar K.V 				found = 1;
665b530cc79SEric Van Hensbergen 				break;
666f75580c4SAneesh Kumar K.V 			}
667c1a7c226SAneesh Kumar K.V 			ret = -EBUSY;
668f75580c4SAneesh Kumar K.V 		}
669b530cc79SEric Van Hensbergen 	}
670c1549497SJosef 'Jeff' Sipek 	mutex_unlock(&virtio_9p_lock);
671b530cc79SEric Van Hensbergen 
67237c1209dSAneesh Kumar K.V 	if (!found) {
673c7c72c5aSAneesh Kumar K.V 		pr_err("no channels available for device %s\n", devname);
674c1a7c226SAneesh Kumar K.V 		return ret;
675b530cc79SEric Van Hensbergen 	}
676b530cc79SEric Van Hensbergen 
6778b81ef58SEric Van Hensbergen 	client->trans = (void *)chan;
678562ada61SEric Van Hensbergen 	client->status = Connected;
679fea511a6SEric Van Hensbergen 	chan->client = client;
680b530cc79SEric Van Hensbergen 
6818b81ef58SEric Van Hensbergen 	return 0;
682b530cc79SEric Van Hensbergen }
683b530cc79SEric Van Hensbergen 
684ee443996SEric Van Hensbergen /**
685ee443996SEric Van Hensbergen  * p9_virtio_remove - clean up resources associated with a virtio device
686ee443996SEric Van Hensbergen  * @vdev: virtio device to remove
687ee443996SEric Van Hensbergen  *
688ee443996SEric Van Hensbergen  */
689ee443996SEric Van Hensbergen 
690f3933545SEric Van Hensbergen static void p9_virtio_remove(struct virtio_device *vdev)
691f3933545SEric Van Hensbergen {
692f3933545SEric Van Hensbergen 	struct virtio_chan *chan = vdev->priv;
6938051a2a5SMichael S. Tsirkin 	unsigned long warning_time;
69437c1209dSAneesh Kumar K.V 
69537c1209dSAneesh Kumar K.V 	mutex_lock(&virtio_9p_lock);
6968051a2a5SMichael S. Tsirkin 
6978051a2a5SMichael S. Tsirkin 	/* Remove self from list so we don't get new users. */
69837c1209dSAneesh Kumar K.V 	list_del(&chan->chan_list);
6998051a2a5SMichael S. Tsirkin 	warning_time = jiffies;
7008051a2a5SMichael S. Tsirkin 
7018051a2a5SMichael S. Tsirkin 	/* Wait for existing users to close. */
7028051a2a5SMichael S. Tsirkin 	while (chan->inuse) {
70337c1209dSAneesh Kumar K.V 		mutex_unlock(&virtio_9p_lock);
7048051a2a5SMichael S. Tsirkin 		msleep(250);
7058051a2a5SMichael S. Tsirkin 		if (time_after(jiffies, warning_time + 10 * HZ)) {
7068051a2a5SMichael S. Tsirkin 			dev_emerg(&vdev->dev,
7078051a2a5SMichael S. Tsirkin 				  "p9_virtio_remove: waiting for device in use.\n");
7088051a2a5SMichael S. Tsirkin 			warning_time = jiffies;
7098051a2a5SMichael S. Tsirkin 		}
7108051a2a5SMichael S. Tsirkin 		mutex_lock(&virtio_9p_lock);
7118051a2a5SMichael S. Tsirkin 	}
7128051a2a5SMichael S. Tsirkin 
7138051a2a5SMichael S. Tsirkin 	mutex_unlock(&virtio_9p_lock);
7148051a2a5SMichael S. Tsirkin 
715ea52bf8eSPierre Morel 	vdev->config->reset(vdev);
7168051a2a5SMichael S. Tsirkin 	vdev->config->del_vqs(vdev);
7178051a2a5SMichael S. Tsirkin 
71886c84373SAneesh Kumar K.V 	sysfs_remove_file(&(vdev->dev.kobj), &dev_attr_mount_tag.attr);
719e0d6cb9cSMichael Marineau 	kobject_uevent(&(vdev->dev.kobj), KOBJ_CHANGE);
72097ee9b02SAneesh Kumar K.V 	kfree(chan->tag);
72152f44e0dSVenkateswararao Jujjuri (JV) 	kfree(chan->vc_wq);
72237c1209dSAneesh Kumar K.V 	kfree(chan);
72337c1209dSAneesh Kumar K.V 
724f3933545SEric Van Hensbergen }
725f3933545SEric Van Hensbergen 
726b530cc79SEric Van Hensbergen static struct virtio_device_id id_table[] = {
727b530cc79SEric Van Hensbergen 	{ VIRTIO_ID_9P, VIRTIO_DEV_ANY_ID },
728b530cc79SEric Van Hensbergen 	{ 0 },
729b530cc79SEric Van Hensbergen };
730b530cc79SEric Van Hensbergen 
73197ee9b02SAneesh Kumar K.V static unsigned int features[] = {
73297ee9b02SAneesh Kumar K.V 	VIRTIO_9P_MOUNT_TAG,
73397ee9b02SAneesh Kumar K.V };
73497ee9b02SAneesh Kumar K.V 
735b530cc79SEric Van Hensbergen /* The standard "struct lguest_driver": */
736b530cc79SEric Van Hensbergen static struct virtio_driver p9_virtio_drv = {
73797ee9b02SAneesh Kumar K.V 	.feature_table  = features,
73897ee9b02SAneesh Kumar K.V 	.feature_table_size = ARRAY_SIZE(features),
739b530cc79SEric Van Hensbergen 	.driver.name    = KBUILD_MODNAME,
740b530cc79SEric Van Hensbergen 	.driver.owner	= THIS_MODULE,
741b530cc79SEric Van Hensbergen 	.id_table	= id_table,
742b530cc79SEric Van Hensbergen 	.probe		= p9_virtio_probe,
743f3933545SEric Van Hensbergen 	.remove		= p9_virtio_remove,
744b530cc79SEric Van Hensbergen };
745b530cc79SEric Van Hensbergen 
746b530cc79SEric Van Hensbergen static struct p9_trans_module p9_virtio_trans = {
747b530cc79SEric Van Hensbergen 	.name = "virtio",
748b530cc79SEric Van Hensbergen 	.create = p9_virtio_create,
7498b81ef58SEric Van Hensbergen 	.close = p9_virtio_close,
75091b8534fSEric Van Hensbergen 	.request = p9_virtio_request,
751dc893e19SArnd Bergmann 	.zc_request = p9_virtio_zc_request,
75291b8534fSEric Van Hensbergen 	.cancel = p9_virtio_cancel,
753b49d8b5dSAneesh Kumar K.V 	/*
754b49d8b5dSAneesh Kumar K.V 	 * We leave one entry for input and one entry for response
755b49d8b5dSAneesh Kumar K.V 	 * headers. We also skip one more entry to accomodate, address
756b49d8b5dSAneesh Kumar K.V 	 * that are not at page boundary, that can result in an extra
757b49d8b5dSAneesh Kumar K.V 	 * page in zero copy.
758b49d8b5dSAneesh Kumar K.V 	 */
759b49d8b5dSAneesh Kumar K.V 	.maxsize = PAGE_SIZE * (VIRTQUEUE_NUM - 3),
760f94741fdSEric Van Hensbergen 	.def = 1,
76172029fe8STejun Heo 	.owner = THIS_MODULE,
762b530cc79SEric Van Hensbergen };
763b530cc79SEric Van Hensbergen 
764b530cc79SEric Van Hensbergen /* The standard init function */
765b530cc79SEric Van Hensbergen static int __init p9_virtio_init(void)
766b530cc79SEric Van Hensbergen {
76737c1209dSAneesh Kumar K.V 	INIT_LIST_HEAD(&virtio_chan_list);
768b530cc79SEric Van Hensbergen 
769b530cc79SEric Van Hensbergen 	v9fs_register_trans(&p9_virtio_trans);
770b530cc79SEric Van Hensbergen 	return register_virtio_driver(&p9_virtio_drv);
771b530cc79SEric Van Hensbergen }
772b530cc79SEric Van Hensbergen 
773f3933545SEric Van Hensbergen static void __exit p9_virtio_cleanup(void)
774f3933545SEric Van Hensbergen {
775f3933545SEric Van Hensbergen 	unregister_virtio_driver(&p9_virtio_drv);
77672029fe8STejun Heo 	v9fs_unregister_trans(&p9_virtio_trans);
777f3933545SEric Van Hensbergen }
778f3933545SEric Van Hensbergen 
779b530cc79SEric Van Hensbergen module_init(p9_virtio_init);
780f3933545SEric Van Hensbergen module_exit(p9_virtio_cleanup);
781b530cc79SEric Van Hensbergen 
782b530cc79SEric Van Hensbergen MODULE_DEVICE_TABLE(virtio, id_table);
783b530cc79SEric Van Hensbergen MODULE_AUTHOR("Eric Van Hensbergen <ericvh@gmail.com>");
784b530cc79SEric Van Hensbergen MODULE_DESCRIPTION("Virtio 9p Transport");
785b530cc79SEric Van Hensbergen MODULE_LICENSE("GPL");
786