xref: /openbmc/linux/net/9p/trans_virtio.c (revision 4cd82a5bb0f68236f67b1678bc9e6348a42241ed)
11f327613SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2b530cc79SEric Van Hensbergen /*
3fea511a6SEric Van Hensbergen  * The Virtio 9p transport driver
4b530cc79SEric Van Hensbergen  *
5e2735b77SEric Van Hensbergen  * This is a block based transport driver based on the lguest block driver
6e2735b77SEric Van Hensbergen  * code.
7b530cc79SEric Van Hensbergen  *
8fea511a6SEric Van Hensbergen  *  Copyright (C) 2007, 2008 Eric Van Hensbergen, IBM Corporation
9b530cc79SEric Van Hensbergen  *
10b530cc79SEric Van Hensbergen  *  Based on virtio console driver
11b530cc79SEric Van Hensbergen  *  Copyright (C) 2006, 2007 Rusty Russell, IBM Corporation
12b530cc79SEric Van Hensbergen  */
13b530cc79SEric Van Hensbergen 
145d385153SJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
155d385153SJoe Perches 
16b530cc79SEric Van Hensbergen #include <linux/in.h>
17b530cc79SEric Van Hensbergen #include <linux/module.h>
18b530cc79SEric Van Hensbergen #include <linux/net.h>
19b530cc79SEric Van Hensbergen #include <linux/ipv6.h>
20b530cc79SEric Van Hensbergen #include <linux/errno.h>
21b530cc79SEric Van Hensbergen #include <linux/kernel.h>
22b530cc79SEric Van Hensbergen #include <linux/un.h>
23b530cc79SEric Van Hensbergen #include <linux/uaccess.h>
24b530cc79SEric Van Hensbergen #include <linux/inet.h>
25b530cc79SEric Van Hensbergen #include <linux/idr.h>
26b530cc79SEric Van Hensbergen #include <linux/file.h>
27b9cdc88dSWill Deacon #include <linux/highmem.h>
285a0e3ad6STejun Heo #include <linux/slab.h>
29b530cc79SEric Van Hensbergen #include <net/9p/9p.h>
30b530cc79SEric Van Hensbergen #include <linux/parser.h>
318b81ef58SEric Van Hensbergen #include <net/9p/client.h>
32b530cc79SEric Van Hensbergen #include <net/9p/transport.h>
33b530cc79SEric Van Hensbergen #include <linux/scatterlist.h>
3468da9ba4SVenkateswararao Jujjuri (JV) #include <linux/swap.h>
35b530cc79SEric Van Hensbergen #include <linux/virtio.h>
36b530cc79SEric Van Hensbergen #include <linux/virtio_9p.h>
374038866dSVenkateswararao Jujjuri (JV) #include "trans_common.h"
38b530cc79SEric Van Hensbergen 
39e2735b77SEric Van Hensbergen #define VIRTQUEUE_NUM	128
40e2735b77SEric Van Hensbergen 
41b530cc79SEric Van Hensbergen /* a single mutex to manage channel initialization and attachment */
42c1549497SJosef 'Jeff' Sipek static DEFINE_MUTEX(virtio_9p_lock);
4368da9ba4SVenkateswararao Jujjuri (JV) static DECLARE_WAIT_QUEUE_HEAD(vp_wq);
4468da9ba4SVenkateswararao Jujjuri (JV) static atomic_t vp_pinned = ATOMIC_INIT(0);
45b530cc79SEric Van Hensbergen 
46ee443996SEric Van Hensbergen /**
47ee443996SEric Van Hensbergen  * struct virtio_chan - per-instance transport information
48ee443996SEric Van Hensbergen  * @inuse: whether the channel is in use
49ee443996SEric Van Hensbergen  * @lock: protects multiple elements within this structure
500e15597eSAbhishek Kulkarni  * @client: client instance
51ee443996SEric Van Hensbergen  * @vdev: virtio dev associated with this channel
52ee443996SEric Van Hensbergen  * @vq: virtio queue associated with this channel
53760b3d61SAndrew Lunn  * @ring_bufs_avail: flag to indicate there is some available in the ring buf
54760b3d61SAndrew Lunn  * @vc_wq: wait queue for waiting for thing to be added to ring buf
55760b3d61SAndrew Lunn  * @p9_max_pages: maximum number of pinned pages
56ee443996SEric Van Hensbergen  * @sg: scatter gather list which is used to pack a request (protected?)
57760b3d61SAndrew Lunn  * @chan_list: linked list of channels
58ee443996SEric Van Hensbergen  *
59ee443996SEric Van Hensbergen  * We keep all per-channel information in a structure.
60b530cc79SEric Van Hensbergen  * This structure is allocated within the devices dev->mem space.
61b530cc79SEric Van Hensbergen  * A pointer to the structure will get put in the transport private.
62ee443996SEric Van Hensbergen  *
63b530cc79SEric Van Hensbergen  */
64ee443996SEric Van Hensbergen 
6537c1209dSAneesh Kumar K.V struct virtio_chan {
66ee443996SEric Van Hensbergen 	bool inuse;
67b530cc79SEric Van Hensbergen 
68e2735b77SEric Van Hensbergen 	spinlock_t lock;
69e2735b77SEric Van Hensbergen 
70fea511a6SEric Van Hensbergen 	struct p9_client *client;
71b530cc79SEric Van Hensbergen 	struct virtio_device *vdev;
72e2735b77SEric Van Hensbergen 	struct virtqueue *vq;
7352f44e0dSVenkateswararao Jujjuri (JV) 	int ring_bufs_avail;
7452f44e0dSVenkateswararao Jujjuri (JV) 	wait_queue_head_t *vc_wq;
7568da9ba4SVenkateswararao Jujjuri (JV) 	/* This is global limit. Since we don't have a global structure,
7668da9ba4SVenkateswararao Jujjuri (JV) 	 * will be placing it in each channel.
7768da9ba4SVenkateswararao Jujjuri (JV) 	 */
787293bfbaSZhang Yanfei 	unsigned long p9_max_pages;
79e2735b77SEric Van Hensbergen 	/* Scatterlist: can be too big for stack. */
80e2735b77SEric Van Hensbergen 	struct scatterlist sg[VIRTQUEUE_NUM];
81760b3d61SAndrew Lunn 	/**
82760b3d61SAndrew Lunn 	 * @tag: name to identify a mount null terminated
8397ee9b02SAneesh Kumar K.V 	 */
8497ee9b02SAneesh Kumar K.V 	char *tag;
8597ee9b02SAneesh Kumar K.V 
8637c1209dSAneesh Kumar K.V 	struct list_head chan_list;
8737c1209dSAneesh Kumar K.V };
8837c1209dSAneesh Kumar K.V 
8937c1209dSAneesh Kumar K.V static struct list_head virtio_chan_list;
90b530cc79SEric Van Hensbergen 
91b530cc79SEric Van Hensbergen /* How many bytes left in this page. */
92b530cc79SEric Van Hensbergen static unsigned int rest_of_page(void *data)
93b530cc79SEric Van Hensbergen {
94222e4adeSAl Viro 	return PAGE_SIZE - offset_in_page(data);
95b530cc79SEric Van Hensbergen }
96b530cc79SEric Van Hensbergen 
97ee443996SEric Van Hensbergen /**
98ee443996SEric Van Hensbergen  * p9_virtio_close - reclaim resources of a channel
990e15597eSAbhishek Kulkarni  * @client: client instance
100ee443996SEric Van Hensbergen  *
101ee443996SEric Van Hensbergen  * This reclaims a channel by freeing its resources and
1028ab1784dSZheng Yongjun  * resetting its inuse flag.
103ee443996SEric Van Hensbergen  *
104ee443996SEric Van Hensbergen  */
105ee443996SEric Van Hensbergen 
1068b81ef58SEric Van Hensbergen static void p9_virtio_close(struct p9_client *client)
107b530cc79SEric Van Hensbergen {
1088b81ef58SEric Van Hensbergen 	struct virtio_chan *chan = client->trans;
109b530cc79SEric Van Hensbergen 
110c1549497SJosef 'Jeff' Sipek 	mutex_lock(&virtio_9p_lock);
111fb786100SAneesh Kumar K.V 	if (chan)
112b530cc79SEric Van Hensbergen 		chan->inuse = false;
113c1549497SJosef 'Jeff' Sipek 	mutex_unlock(&virtio_9p_lock);
114b530cc79SEric Van Hensbergen }
115b530cc79SEric Van Hensbergen 
116ee443996SEric Van Hensbergen /**
117ee443996SEric Van Hensbergen  * req_done - callback which signals activity from the server
118ee443996SEric Van Hensbergen  * @vq: virtio queue activity was received on
119ee443996SEric Van Hensbergen  *
120ee443996SEric Van Hensbergen  * This notifies us that the server has triggered some activity
121ee443996SEric Van Hensbergen  * on the virtio channel - most likely a response to request we
122ee443996SEric Van Hensbergen  * sent.  Figure out which requests now have responses and wake up
123ee443996SEric Van Hensbergen  * those threads.
124ee443996SEric Van Hensbergen  *
125ee443996SEric Van Hensbergen  * Bugs: could do with some additional sanity checking, but appears to work.
126ee443996SEric Van Hensbergen  *
127ee443996SEric Van Hensbergen  */
128ee443996SEric Van Hensbergen 
129e2735b77SEric Van Hensbergen static void req_done(struct virtqueue *vq)
130b530cc79SEric Van Hensbergen {
131e2735b77SEric Van Hensbergen 	struct virtio_chan *chan = vq->vdev->priv;
132e2735b77SEric Van Hensbergen 	unsigned int len;
133e2735b77SEric Van Hensbergen 	struct p9_req_t *req;
13431934da8Sjiangyiwen 	bool need_wakeup = false;
135419b3956SVenkateswararao Jujjuri (JV) 	unsigned long flags;
136b530cc79SEric Van Hensbergen 
1375d385153SJoe Perches 	p9_debug(P9_DEBUG_TRANS, ": request done\n");
13891b8534fSEric Van Hensbergen 
139419b3956SVenkateswararao Jujjuri (JV) 	spin_lock_irqsave(&chan->lock, flags);
14031934da8Sjiangyiwen 	while ((req = virtqueue_get_buf(chan->vq, &len)) != NULL) {
14131934da8Sjiangyiwen 		if (!chan->ring_bufs_avail) {
14252f44e0dSVenkateswararao Jujjuri (JV) 			chan->ring_bufs_avail = 1;
14331934da8Sjiangyiwen 			need_wakeup = true;
14431934da8Sjiangyiwen 		}
14531934da8Sjiangyiwen 
146f984579aSTomas Bortoli 		if (len) {
147523adb6cSDominique Martinet 			req->rc.size = len;
1482b6e72edSDominique Martinet 			p9_client_cb(chan->client, req, REQ_STATUS_RCVD);
149e2735b77SEric Van Hensbergen 		}
150f984579aSTomas Bortoli 	}
15131934da8Sjiangyiwen 	spin_unlock_irqrestore(&chan->lock, flags);
15231934da8Sjiangyiwen 	/* Wakeup if anyone waiting for VirtIO ring space. */
15331934da8Sjiangyiwen 	if (need_wakeup)
15431934da8Sjiangyiwen 		wake_up(chan->vc_wq);
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 
171abfa034eSAneesh Kumar K.V static int pack_sg_list(struct scatterlist *sg, int start,
172abfa034eSAneesh Kumar K.V 			int limit, char *data, int count)
173e2735b77SEric Van Hensbergen {
174e2735b77SEric Van Hensbergen 	int s;
175e2735b77SEric Van Hensbergen 	int index = start;
176e2735b77SEric Van Hensbergen 
177e2735b77SEric Van Hensbergen 	while (count) {
178e2735b77SEric Van Hensbergen 		s = rest_of_page(data);
179e2735b77SEric Van Hensbergen 		if (s > count)
180e2735b77SEric Van Hensbergen 			s = count;
18123cba9cbSjiangyiwen 		BUG_ON(index >= limit);
1820b36f1adSRusty Russell 		/* Make sure we don't terminate early. */
1830b36f1adSRusty Russell 		sg_unmark_end(&sg[index]);
184e2735b77SEric Van Hensbergen 		sg_set_buf(&sg[index++], data, s);
185e2735b77SEric Van Hensbergen 		count -= s;
186e2735b77SEric Van Hensbergen 		data += s;
187e2735b77SEric Van Hensbergen 	}
1880b36f1adSRusty Russell 	if (index-start)
1890b36f1adSRusty Russell 		sg_mark_end(&sg[index - 1]);
190e2735b77SEric Van Hensbergen 	return index-start;
191e2735b77SEric Van Hensbergen }
192e2735b77SEric Van Hensbergen 
19391b8534fSEric Van Hensbergen /* We don't currently allow canceling of virtio requests */
19491b8534fSEric Van Hensbergen static int p9_virtio_cancel(struct p9_client *client, struct p9_req_t *req)
19591b8534fSEric Van Hensbergen {
19691b8534fSEric Van Hensbergen 	return 1;
19791b8534fSEric Van Hensbergen }
19891b8534fSEric Van Hensbergen 
199728356deSTomas Bortoli /* Reply won't come, so drop req ref */
200728356deSTomas Bortoli static int p9_virtio_cancelled(struct p9_client *client, struct p9_req_t *req)
201728356deSTomas Bortoli {
202728356deSTomas Bortoli 	p9_req_put(req);
203728356deSTomas Bortoli 	return 0;
204728356deSTomas Bortoli }
205728356deSTomas Bortoli 
206ee443996SEric Van Hensbergen /**
2074038866dSVenkateswararao Jujjuri (JV)  * pack_sg_list_p - Just like pack_sg_list. Instead of taking a buffer,
2084038866dSVenkateswararao Jujjuri (JV)  * this takes a list of pages.
2094038866dSVenkateswararao Jujjuri (JV)  * @sg: scatter/gather list to pack into
2104038866dSVenkateswararao Jujjuri (JV)  * @start: which segment of the sg_list to start at
211760b3d61SAndrew Lunn  * @limit: maximum number of pages in sg list.
2122c53040fSBen Hutchings  * @pdata: a list of pages to add into sg.
213abfa034eSAneesh Kumar K.V  * @nr_pages: number of pages to pack into the scatter/gather list
2144f3b35c1SAl Viro  * @offs: amount of data in the beginning of first page _not_ to pack
2154038866dSVenkateswararao Jujjuri (JV)  * @count: amount of data to pack into the scatter/gather list
2164038866dSVenkateswararao Jujjuri (JV)  */
2174038866dSVenkateswararao Jujjuri (JV) static int
218abfa034eSAneesh Kumar K.V pack_sg_list_p(struct scatterlist *sg, int start, int limit,
2194f3b35c1SAl Viro 	       struct page **pdata, int nr_pages, size_t offs, int count)
2204038866dSVenkateswararao Jujjuri (JV) {
221abfa034eSAneesh Kumar K.V 	int i = 0, s;
2224f3b35c1SAl Viro 	int data_off = offs;
2234038866dSVenkateswararao Jujjuri (JV) 	int index = start;
2244038866dSVenkateswararao Jujjuri (JV) 
225abfa034eSAneesh Kumar K.V 	BUG_ON(nr_pages > (limit - start));
226abfa034eSAneesh Kumar K.V 	/*
227abfa034eSAneesh Kumar K.V 	 * if the first page doesn't start at
228abfa034eSAneesh Kumar K.V 	 * page boundary find the offset
229abfa034eSAneesh Kumar K.V 	 */
230abfa034eSAneesh Kumar K.V 	while (nr_pages) {
2314f3b35c1SAl Viro 		s = PAGE_SIZE - data_off;
232abfa034eSAneesh Kumar K.V 		if (s > count)
233abfa034eSAneesh Kumar K.V 			s = count;
23423cba9cbSjiangyiwen 		BUG_ON(index >= limit);
2350b36f1adSRusty Russell 		/* Make sure we don't terminate early. */
2360b36f1adSRusty Russell 		sg_unmark_end(&sg[index]);
237abfa034eSAneesh Kumar K.V 		sg_set_page(&sg[index++], pdata[i++], s, data_off);
238abfa034eSAneesh Kumar K.V 		data_off = 0;
2394038866dSVenkateswararao Jujjuri (JV) 		count -= s;
240abfa034eSAneesh Kumar K.V 		nr_pages--;
2414038866dSVenkateswararao Jujjuri (JV) 	}
2420b36f1adSRusty Russell 
2430b36f1adSRusty Russell 	if (index-start)
2440b36f1adSRusty Russell 		sg_mark_end(&sg[index - 1]);
2454038866dSVenkateswararao Jujjuri (JV) 	return index - start;
2464038866dSVenkateswararao Jujjuri (JV) }
2474038866dSVenkateswararao Jujjuri (JV) 
2484038866dSVenkateswararao Jujjuri (JV) /**
24991b8534fSEric Van Hensbergen  * p9_virtio_request - issue a request
2500e15597eSAbhishek Kulkarni  * @client: client instance issuing the request
2510e15597eSAbhishek Kulkarni  * @req: request to be issued
252ee443996SEric Van Hensbergen  *
253ee443996SEric Van Hensbergen  */
254ee443996SEric Van Hensbergen 
255e2735b77SEric Van Hensbergen static int
25691b8534fSEric Van Hensbergen p9_virtio_request(struct p9_client *client, struct p9_req_t *req)
257e2735b77SEric Van Hensbergen {
258abfa034eSAneesh Kumar K.V 	int err;
2590b36f1adSRusty Russell 	int in, out, out_sgs, in_sgs;
260419b3956SVenkateswararao Jujjuri (JV) 	unsigned long flags;
261abfa034eSAneesh Kumar K.V 	struct virtio_chan *chan = client->trans;
2620b36f1adSRusty Russell 	struct scatterlist *sgs[2];
263e2735b77SEric Van Hensbergen 
2645d385153SJoe Perches 	p9_debug(P9_DEBUG_TRANS, "9p debug: virtio request\n");
265e2735b77SEric Van Hensbergen 
266419b3956SVenkateswararao Jujjuri (JV) 	req->status = REQ_STATUS_SENT;
267abfa034eSAneesh Kumar K.V req_retry:
268419b3956SVenkateswararao Jujjuri (JV) 	spin_lock_irqsave(&chan->lock, flags);
2694038866dSVenkateswararao Jujjuri (JV) 
2700b36f1adSRusty Russell 	out_sgs = in_sgs = 0;
2714038866dSVenkateswararao Jujjuri (JV) 	/* Handle out VirtIO ring buffers */
272abfa034eSAneesh Kumar K.V 	out = pack_sg_list(chan->sg, 0,
273523adb6cSDominique Martinet 			   VIRTQUEUE_NUM, req->tc.sdata, req->tc.size);
2740b36f1adSRusty Russell 	if (out)
2750b36f1adSRusty Russell 		sgs[out_sgs++] = chan->sg;
2764038866dSVenkateswararao Jujjuri (JV) 
277abfa034eSAneesh Kumar K.V 	in = pack_sg_list(chan->sg, out,
278523adb6cSDominique Martinet 			  VIRTQUEUE_NUM, req->rc.sdata, req->rc.capacity);
2790b36f1adSRusty Russell 	if (in)
2800b36f1adSRusty Russell 		sgs[out_sgs + in_sgs++] = chan->sg + out;
281e2735b77SEric Van Hensbergen 
282474fe9f7SAl Viro 	err = virtqueue_add_sgs(chan->vq, sgs, out_sgs, in_sgs, req,
283f96fde41SRusty Russell 				GFP_ATOMIC);
284419b3956SVenkateswararao Jujjuri (JV) 	if (err < 0) {
28552f44e0dSVenkateswararao Jujjuri (JV) 		if (err == -ENOSPC) {
28652f44e0dSVenkateswararao Jujjuri (JV) 			chan->ring_bufs_avail = 0;
28752f44e0dSVenkateswararao Jujjuri (JV) 			spin_unlock_irqrestore(&chan->lock, flags);
2889523feacSTuomas Tynkkynen 			err = wait_event_killable(*chan->vc_wq,
28952f44e0dSVenkateswararao Jujjuri (JV) 						  chan->ring_bufs_avail);
29052f44e0dSVenkateswararao Jujjuri (JV) 			if (err  == -ERESTARTSYS)
29152f44e0dSVenkateswararao Jujjuri (JV) 				return err;
29252f44e0dSVenkateswararao Jujjuri (JV) 
2935d385153SJoe Perches 			p9_debug(P9_DEBUG_TRANS, "Retry virtio request\n");
294abfa034eSAneesh Kumar K.V 			goto req_retry;
295abfa034eSAneesh Kumar K.V 		} else {
296abfa034eSAneesh Kumar K.V 			spin_unlock_irqrestore(&chan->lock, flags);
2975d385153SJoe Perches 			p9_debug(P9_DEBUG_TRANS,
2980b36f1adSRusty Russell 				 "virtio rpc add_sgs returned failure\n");
299abfa034eSAneesh Kumar K.V 			return -EIO;
300abfa034eSAneesh Kumar K.V 		}
301abfa034eSAneesh Kumar K.V 	}
302abfa034eSAneesh Kumar K.V 	virtqueue_kick(chan->vq);
303abfa034eSAneesh Kumar K.V 	spin_unlock_irqrestore(&chan->lock, flags);
304abfa034eSAneesh Kumar K.V 
3055d385153SJoe Perches 	p9_debug(P9_DEBUG_TRANS, "virtio request kicked\n");
306abfa034eSAneesh Kumar K.V 	return 0;
307abfa034eSAneesh Kumar K.V }
308abfa034eSAneesh Kumar K.V 
309abfa034eSAneesh Kumar K.V static int p9_get_mapped_pages(struct virtio_chan *chan,
3104f3b35c1SAl Viro 			       struct page ***pages,
3114f3b35c1SAl Viro 			       struct iov_iter *data,
3124f3b35c1SAl Viro 			       int count,
3134f3b35c1SAl Viro 			       size_t *offs,
3144f3b35c1SAl Viro 			       int *need_drop)
315abfa034eSAneesh Kumar K.V {
3164f3b35c1SAl Viro 	int nr_pages;
317abfa034eSAneesh Kumar K.V 	int err;
3184f3b35c1SAl Viro 
3194f3b35c1SAl Viro 	if (!iov_iter_count(data))
3204f3b35c1SAl Viro 		return 0;
3214f3b35c1SAl Viro 
3222cbfdf4dSMarc Zyngier 	if (!iov_iter_is_kvec(data)) {
3234f3b35c1SAl Viro 		int n;
324abfa034eSAneesh Kumar K.V 		/*
325abfa034eSAneesh Kumar K.V 		 * We allow only p9_max_pages pinned. We wait for the
326abfa034eSAneesh Kumar K.V 		 * Other zc request to finish here
327abfa034eSAneesh Kumar K.V 		 */
328abfa034eSAneesh Kumar K.V 		if (atomic_read(&vp_pinned) >= chan->p9_max_pages) {
3299523feacSTuomas Tynkkynen 			err = wait_event_killable(vp_wq,
330abfa034eSAneesh Kumar K.V 			      (atomic_read(&vp_pinned) < chan->p9_max_pages));
331abfa034eSAneesh Kumar K.V 			if (err == -ERESTARTSYS)
332abfa034eSAneesh Kumar K.V 				return err;
333abfa034eSAneesh Kumar K.V 		}
3344f3b35c1SAl Viro 		n = iov_iter_get_pages_alloc(data, pages, count, offs);
3354f3b35c1SAl Viro 		if (n < 0)
3364f3b35c1SAl Viro 			return n;
3374f3b35c1SAl Viro 		*need_drop = 1;
3384f3b35c1SAl Viro 		nr_pages = DIV_ROUND_UP(n + *offs, PAGE_SIZE);
339abfa034eSAneesh Kumar K.V 		atomic_add(nr_pages, &vp_pinned);
3404f3b35c1SAl Viro 		return n;
341abfa034eSAneesh Kumar K.V 	} else {
342abfa034eSAneesh Kumar K.V 		/* kernel buffer, no need to pin pages */
3434f3b35c1SAl Viro 		int index;
3444f3b35c1SAl Viro 		size_t len;
3454f3b35c1SAl Viro 		void *p;
3464f3b35c1SAl Viro 
3474f3b35c1SAl Viro 		/* we'd already checked that it's non-empty */
3484f3b35c1SAl Viro 		while (1) {
3494f3b35c1SAl Viro 			len = iov_iter_single_seg_count(data);
3504f3b35c1SAl Viro 			if (likely(len)) {
3514f3b35c1SAl Viro 				p = data->kvec->iov_base + data->iov_offset;
3524f3b35c1SAl Viro 				break;
3534f3b35c1SAl Viro 			}
3544f3b35c1SAl Viro 			iov_iter_advance(data, 0);
3554f3b35c1SAl Viro 		}
3564f3b35c1SAl Viro 		if (len > count)
3574f3b35c1SAl Viro 			len = count;
3584f3b35c1SAl Viro 
3594f3b35c1SAl Viro 		nr_pages = DIV_ROUND_UP((unsigned long)p + len, PAGE_SIZE) -
3604f3b35c1SAl Viro 			   (unsigned long)p / PAGE_SIZE;
3614f3b35c1SAl Viro 
3626da2ec56SKees Cook 		*pages = kmalloc_array(nr_pages, sizeof(struct page *),
3636da2ec56SKees Cook 				       GFP_NOFS);
3644f3b35c1SAl Viro 		if (!*pages)
3654f3b35c1SAl Viro 			return -ENOMEM;
3664f3b35c1SAl Viro 
3674f3b35c1SAl Viro 		*need_drop = 0;
368222e4adeSAl Viro 		p -= (*offs = offset_in_page(p));
3694f3b35c1SAl Viro 		for (index = 0; index < nr_pages; index++) {
3704f3b35c1SAl Viro 			if (is_vmalloc_addr(p))
3714f3b35c1SAl Viro 				(*pages)[index] = vmalloc_to_page(p);
372b6f52ae2SRichard Yao 			else
3734f3b35c1SAl Viro 				(*pages)[index] = kmap_to_page(p);
3744f3b35c1SAl Viro 			p += PAGE_SIZE;
375abfa034eSAneesh Kumar K.V 		}
3764f3b35c1SAl Viro 		return len;
377abfa034eSAneesh Kumar K.V 	}
378abfa034eSAneesh Kumar K.V }
379abfa034eSAneesh Kumar K.V 
380abfa034eSAneesh Kumar K.V /**
381abfa034eSAneesh Kumar K.V  * p9_virtio_zc_request - issue a zero copy request
382abfa034eSAneesh Kumar K.V  * @client: client instance issuing the request
383abfa034eSAneesh Kumar K.V  * @req: request to be issued
384c7ebbae7Spiaojun  * @uidata: user buffer that should be used for zero copy read
385c7ebbae7Spiaojun  * @uodata: user buffer that should be used for zero copy write
386abfa034eSAneesh Kumar K.V  * @inlen: read buffer size
3874a026da9SSun Lianwen  * @outlen: write buffer size
3884a026da9SSun Lianwen  * @in_hdr_len: reader header size, This is the size of response protocol data
389abfa034eSAneesh Kumar K.V  *
390abfa034eSAneesh Kumar K.V  */
391abfa034eSAneesh Kumar K.V static int
392abfa034eSAneesh Kumar K.V p9_virtio_zc_request(struct p9_client *client, struct p9_req_t *req,
3934f3b35c1SAl Viro 		     struct iov_iter *uidata, struct iov_iter *uodata,
3944f3b35c1SAl Viro 		     int inlen, int outlen, int in_hdr_len)
395abfa034eSAneesh Kumar K.V {
3960b36f1adSRusty Russell 	int in, out, err, out_sgs, in_sgs;
397abfa034eSAneesh Kumar K.V 	unsigned long flags;
398abfa034eSAneesh Kumar K.V 	int in_nr_pages = 0, out_nr_pages = 0;
399abfa034eSAneesh Kumar K.V 	struct page **in_pages = NULL, **out_pages = NULL;
400abfa034eSAneesh Kumar K.V 	struct virtio_chan *chan = client->trans;
4010b36f1adSRusty Russell 	struct scatterlist *sgs[4];
4024f3b35c1SAl Viro 	size_t offs;
4034f3b35c1SAl Viro 	int need_drop = 0;
404728356deSTomas Bortoli 	int kicked = 0;
405abfa034eSAneesh Kumar K.V 
4065d385153SJoe Perches 	p9_debug(P9_DEBUG_TRANS, "virtio request\n");
407abfa034eSAneesh Kumar K.V 
408abfa034eSAneesh Kumar K.V 	if (uodata) {
409d28c756cSChirantan Ekbote 		__le32 sz;
4104f3b35c1SAl Viro 		int n = p9_get_mapped_pages(chan, &out_pages, uodata,
4114f3b35c1SAl Viro 					    outlen, &offs, &need_drop);
412728356deSTomas Bortoli 		if (n < 0) {
413728356deSTomas Bortoli 			err = n;
414728356deSTomas Bortoli 			goto err_out;
415728356deSTomas Bortoli 		}
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);
419523adb6cSDominique 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 		 */
426523adb6cSDominique Martinet 		sz = cpu_to_le32(req->tc.size + outlen);
427523adb6cSDominique 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);
431728356deSTomas Bortoli 		if (n < 0) {
432728356deSTomas Bortoli 			err = n;
433728356deSTomas Bortoli 			goto err_out;
434728356deSTomas Bortoli 		}
4354f3b35c1SAl Viro 		in_nr_pages = DIV_ROUND_UP(n + offs, PAGE_SIZE);
4364f3b35c1SAl Viro 		if (n != inlen) {
4374f3b35c1SAl Viro 			__le32 v = cpu_to_le32(n);
438523adb6cSDominique Martinet 			memcpy(&req->tc.sdata[req->tc.size - 4], &v, 4);
4394f3b35c1SAl Viro 			inlen = n;
440abfa034eSAneesh Kumar K.V 		}
441abfa034eSAneesh Kumar K.V 	}
442abfa034eSAneesh Kumar K.V 	req->status = REQ_STATUS_SENT;
443abfa034eSAneesh Kumar K.V req_retry_pinned:
444abfa034eSAneesh Kumar K.V 	spin_lock_irqsave(&chan->lock, flags);
4450b36f1adSRusty Russell 
4460b36f1adSRusty Russell 	out_sgs = in_sgs = 0;
4470b36f1adSRusty Russell 
448abfa034eSAneesh Kumar K.V 	/* out data */
449abfa034eSAneesh Kumar K.V 	out = pack_sg_list(chan->sg, 0,
450523adb6cSDominique Martinet 			   VIRTQUEUE_NUM, req->tc.sdata, req->tc.size);
451abfa034eSAneesh Kumar K.V 
4520b36f1adSRusty Russell 	if (out)
4530b36f1adSRusty Russell 		sgs[out_sgs++] = chan->sg;
4540b36f1adSRusty Russell 
4550b36f1adSRusty Russell 	if (out_pages) {
4560b36f1adSRusty Russell 		sgs[out_sgs++] = chan->sg + out;
457abfa034eSAneesh Kumar K.V 		out += pack_sg_list_p(chan->sg, out, VIRTQUEUE_NUM,
4584f3b35c1SAl Viro 				      out_pages, out_nr_pages, offs, outlen);
4590b36f1adSRusty Russell 	}
4600b36f1adSRusty Russell 
461abfa034eSAneesh Kumar K.V 	/*
462abfa034eSAneesh Kumar K.V 	 * Take care of in data
463abfa034eSAneesh Kumar K.V 	 * For example TREAD have 11.
464abfa034eSAneesh Kumar K.V 	 * 11 is the read/write header = PDU Header(7) + IO Size (4).
465abfa034eSAneesh Kumar K.V 	 * Arrange in such a way that server places header in the
4668ab1784dSZheng Yongjun 	 * allocated memory and payload onto the user buffer.
467abfa034eSAneesh Kumar K.V 	 */
468abfa034eSAneesh Kumar K.V 	in = pack_sg_list(chan->sg, out,
469523adb6cSDominique Martinet 			  VIRTQUEUE_NUM, req->rc.sdata, in_hdr_len);
4700b36f1adSRusty Russell 	if (in)
4710b36f1adSRusty Russell 		sgs[out_sgs + in_sgs++] = chan->sg + out;
4720b36f1adSRusty Russell 
4730b36f1adSRusty Russell 	if (in_pages) {
4740b36f1adSRusty Russell 		sgs[out_sgs + in_sgs++] = chan->sg + out + in;
475abfa034eSAneesh Kumar K.V 		in += pack_sg_list_p(chan->sg, out + in, VIRTQUEUE_NUM,
4764f3b35c1SAl Viro 				     in_pages, in_nr_pages, offs, inlen);
4770b36f1adSRusty Russell 	}
478abfa034eSAneesh Kumar K.V 
4790b36f1adSRusty Russell 	BUG_ON(out_sgs + in_sgs > ARRAY_SIZE(sgs));
480474fe9f7SAl Viro 	err = virtqueue_add_sgs(chan->vq, sgs, out_sgs, in_sgs, req,
481f96fde41SRusty Russell 				GFP_ATOMIC);
482abfa034eSAneesh Kumar K.V 	if (err < 0) {
483abfa034eSAneesh Kumar K.V 		if (err == -ENOSPC) {
484abfa034eSAneesh Kumar K.V 			chan->ring_bufs_avail = 0;
485abfa034eSAneesh Kumar K.V 			spin_unlock_irqrestore(&chan->lock, flags);
4869523feacSTuomas Tynkkynen 			err = wait_event_killable(*chan->vc_wq,
487abfa034eSAneesh Kumar K.V 						  chan->ring_bufs_avail);
488abfa034eSAneesh Kumar K.V 			if (err  == -ERESTARTSYS)
489abfa034eSAneesh Kumar K.V 				goto err_out;
490abfa034eSAneesh Kumar K.V 
4915d385153SJoe Perches 			p9_debug(P9_DEBUG_TRANS, "Retry virtio request\n");
492316ad550SVenkateswararao Jujjuri (JV) 			goto req_retry_pinned;
49352f44e0dSVenkateswararao Jujjuri (JV) 		} else {
494419b3956SVenkateswararao Jujjuri (JV) 			spin_unlock_irqrestore(&chan->lock, flags);
4955d385153SJoe Perches 			p9_debug(P9_DEBUG_TRANS,
4960b36f1adSRusty Russell 				 "virtio rpc add_sgs returned failure\n");
497abfa034eSAneesh Kumar K.V 			err = -EIO;
498abfa034eSAneesh Kumar K.V 			goto err_out;
499e2735b77SEric Van Hensbergen 		}
50052f44e0dSVenkateswararao Jujjuri (JV) 	}
501dc3f5e68SMichael S. Tsirkin 	virtqueue_kick(chan->vq);
502419b3956SVenkateswararao Jujjuri (JV) 	spin_unlock_irqrestore(&chan->lock, flags);
503728356deSTomas Bortoli 	kicked = 1;
5045d385153SJoe Perches 	p9_debug(P9_DEBUG_TRANS, "virtio request kicked\n");
5052557d0c5SMatthew Wilcox 	err = wait_event_killable(req->wq, req->status >= REQ_STATUS_RCVD);
506abfa034eSAneesh Kumar K.V 	/*
507abfa034eSAneesh Kumar K.V 	 * Non kernel buffers are pinned, unpin them
508abfa034eSAneesh Kumar K.V 	 */
509abfa034eSAneesh Kumar K.V err_out:
5104f3b35c1SAl Viro 	if (need_drop) {
511abfa034eSAneesh Kumar K.V 		if (in_pages) {
512abfa034eSAneesh Kumar K.V 			p9_release_pages(in_pages, in_nr_pages);
513abfa034eSAneesh Kumar K.V 			atomic_sub(in_nr_pages, &vp_pinned);
514abfa034eSAneesh Kumar K.V 		}
515abfa034eSAneesh Kumar K.V 		if (out_pages) {
516abfa034eSAneesh Kumar K.V 			p9_release_pages(out_pages, out_nr_pages);
517abfa034eSAneesh Kumar K.V 			atomic_sub(out_nr_pages, &vp_pinned);
518abfa034eSAneesh Kumar K.V 		}
519abfa034eSAneesh Kumar K.V 		/* wakeup anybody waiting for slots to pin pages */
520abfa034eSAneesh Kumar K.V 		wake_up(&vp_wq);
521abfa034eSAneesh Kumar K.V 	}
5221b8553c0SVegard Nossum 	kvfree(in_pages);
5231b8553c0SVegard Nossum 	kvfree(out_pages);
524728356deSTomas Bortoli 	if (!kicked) {
525728356deSTomas Bortoli 		/* reply won't come */
526728356deSTomas Bortoli 		p9_req_put(req);
527728356deSTomas Bortoli 	}
528abfa034eSAneesh Kumar K.V 	return err;
529e2735b77SEric Van Hensbergen }
530e2735b77SEric Van Hensbergen 
53186c84373SAneesh Kumar K.V static ssize_t p9_mount_tag_show(struct device *dev,
53286c84373SAneesh Kumar K.V 				struct device_attribute *attr, char *buf)
53386c84373SAneesh Kumar K.V {
53486c84373SAneesh Kumar K.V 	struct virtio_chan *chan;
53586c84373SAneesh Kumar K.V 	struct virtio_device *vdev;
536edcd9d97Spiaojun 	int tag_len;
53786c84373SAneesh Kumar K.V 
53886c84373SAneesh Kumar K.V 	vdev = dev_to_virtio(dev);
53986c84373SAneesh Kumar K.V 	chan = vdev->priv;
540edcd9d97Spiaojun 	tag_len = strlen(chan->tag);
54186c84373SAneesh Kumar K.V 
542edcd9d97Spiaojun 	memcpy(buf, chan->tag, tag_len + 1);
543179a5bc4SAndrey Ryabinin 
544edcd9d97Spiaojun 	return tag_len + 1;
54586c84373SAneesh Kumar K.V }
54686c84373SAneesh Kumar K.V 
54786c84373SAneesh Kumar K.V static DEVICE_ATTR(mount_tag, 0444, p9_mount_tag_show, NULL);
54886c84373SAneesh Kumar K.V 
549ee443996SEric Van Hensbergen /**
550ee443996SEric Van Hensbergen  * p9_virtio_probe - probe for existence of 9P virtio channels
551ee443996SEric Van Hensbergen  * @vdev: virtio device to probe
552ee443996SEric Van Hensbergen  *
55337c1209dSAneesh Kumar K.V  * This probes for existing virtio channels.
554ee443996SEric Van Hensbergen  *
555ee443996SEric Van Hensbergen  */
556ee443996SEric Van Hensbergen 
557e2735b77SEric Van Hensbergen static int p9_virtio_probe(struct virtio_device *vdev)
558b530cc79SEric Van Hensbergen {
55997ee9b02SAneesh Kumar K.V 	__u16 tag_len;
56097ee9b02SAneesh Kumar K.V 	char *tag;
561b530cc79SEric Van Hensbergen 	int err;
562b530cc79SEric Van Hensbergen 	struct virtio_chan *chan;
563b530cc79SEric Van Hensbergen 
5647754f53eSMichael S. Tsirkin 	if (!vdev->config->get) {
5657754f53eSMichael S. Tsirkin 		dev_err(&vdev->dev, "%s failure: config access disabled\n",
5667754f53eSMichael S. Tsirkin 			__func__);
5677754f53eSMichael S. Tsirkin 		return -EINVAL;
5687754f53eSMichael S. Tsirkin 	}
5697754f53eSMichael S. Tsirkin 
57037c1209dSAneesh Kumar K.V 	chan = kmalloc(sizeof(struct virtio_chan), GFP_KERNEL);
57137c1209dSAneesh Kumar K.V 	if (!chan) {
5725d385153SJoe Perches 		pr_err("Failed to allocate virtio 9P channel\n");
573b530cc79SEric Van Hensbergen 		err = -ENOMEM;
574b530cc79SEric Van Hensbergen 		goto fail;
575b530cc79SEric Van Hensbergen 	}
576b530cc79SEric Van Hensbergen 
577e2735b77SEric Van Hensbergen 	chan->vdev = vdev;
578e2735b77SEric Van Hensbergen 
579e2735b77SEric Van Hensbergen 	/* We expect one virtqueue, for requests. */
580d2a7dddaSMichael S. Tsirkin 	chan->vq = virtio_find_single_vq(vdev, req_done, "requests");
581e2735b77SEric Van Hensbergen 	if (IS_ERR(chan->vq)) {
582e2735b77SEric Van Hensbergen 		err = PTR_ERR(chan->vq);
58392aef467SJean-Philippe Brucker 		goto out_free_chan;
584b530cc79SEric Van Hensbergen 	}
585e2735b77SEric Van Hensbergen 	chan->vq->vdev->priv = chan;
586e2735b77SEric Van Hensbergen 	spin_lock_init(&chan->lock);
587b530cc79SEric Van Hensbergen 
588e2735b77SEric Van Hensbergen 	sg_init_table(chan->sg, VIRTQUEUE_NUM);
589b530cc79SEric Van Hensbergen 
590b530cc79SEric Van Hensbergen 	chan->inuse = false;
59197ee9b02SAneesh Kumar K.V 	if (virtio_has_feature(vdev, VIRTIO_9P_MOUNT_TAG)) {
592855e0c52SRusty Russell 		virtio_cread(vdev, struct virtio_9p_config, tag_len, &tag_len);
59397ee9b02SAneesh Kumar K.V 	} else {
59497ee9b02SAneesh Kumar K.V 		err = -EINVAL;
59597ee9b02SAneesh Kumar K.V 		goto out_free_vq;
59697ee9b02SAneesh Kumar K.V 	}
597edcd9d97Spiaojun 	tag = kzalloc(tag_len + 1, GFP_KERNEL);
59897ee9b02SAneesh Kumar K.V 	if (!tag) {
59997ee9b02SAneesh Kumar K.V 		err = -ENOMEM;
60097ee9b02SAneesh Kumar K.V 		goto out_free_vq;
60197ee9b02SAneesh Kumar K.V 	}
602855e0c52SRusty Russell 
603855e0c52SRusty Russell 	virtio_cread_bytes(vdev, offsetof(struct virtio_9p_config, tag),
60497ee9b02SAneesh Kumar K.V 			   tag, tag_len);
60597ee9b02SAneesh Kumar K.V 	chan->tag = tag;
60686c84373SAneesh Kumar K.V 	err = sysfs_create_file(&(vdev->dev.kobj), &dev_attr_mount_tag.attr);
60786c84373SAneesh Kumar K.V 	if (err) {
60852f44e0dSVenkateswararao Jujjuri (JV) 		goto out_free_tag;
60986c84373SAneesh Kumar K.V 	}
61052f44e0dSVenkateswararao Jujjuri (JV) 	chan->vc_wq = kmalloc(sizeof(wait_queue_head_t), GFP_KERNEL);
61152f44e0dSVenkateswararao Jujjuri (JV) 	if (!chan->vc_wq) {
61252f44e0dSVenkateswararao Jujjuri (JV) 		err = -ENOMEM;
613f997ea3bSXie Yongji 		goto out_remove_file;
61452f44e0dSVenkateswararao Jujjuri (JV) 	}
61552f44e0dSVenkateswararao Jujjuri (JV) 	init_waitqueue_head(chan->vc_wq);
61652f44e0dSVenkateswararao Jujjuri (JV) 	chan->ring_bufs_avail = 1;
61768da9ba4SVenkateswararao Jujjuri (JV) 	/* Ceiling limit to avoid denial of service attacks */
61868da9ba4SVenkateswararao Jujjuri (JV) 	chan->p9_max_pages = nr_free_buffer_pages()/4;
61952f44e0dSVenkateswararao Jujjuri (JV) 
62064b4cc39SMichael S. Tsirkin 	virtio_device_ready(vdev);
62164b4cc39SMichael S. Tsirkin 
62237c1209dSAneesh Kumar K.V 	mutex_lock(&virtio_9p_lock);
62337c1209dSAneesh Kumar K.V 	list_add_tail(&chan->chan_list, &virtio_chan_list);
62437c1209dSAneesh Kumar K.V 	mutex_unlock(&virtio_9p_lock);
625e0d6cb9cSMichael Marineau 
626e0d6cb9cSMichael Marineau 	/* Let udev rules use the new mount_tag attribute. */
627e0d6cb9cSMichael Marineau 	kobject_uevent(&(vdev->dev.kobj), KOBJ_CHANGE);
628e0d6cb9cSMichael Marineau 
629b530cc79SEric Van Hensbergen 	return 0;
630b530cc79SEric Van Hensbergen 
631f997ea3bSXie Yongji out_remove_file:
632f997ea3bSXie Yongji 	sysfs_remove_file(&vdev->dev.kobj, &dev_attr_mount_tag.attr);
63352f44e0dSVenkateswararao Jujjuri (JV) out_free_tag:
63452f44e0dSVenkateswararao Jujjuri (JV) 	kfree(tag);
635e2735b77SEric Van Hensbergen out_free_vq:
636d2a7dddaSMichael S. Tsirkin 	vdev->config->del_vqs(vdev);
63792aef467SJean-Philippe Brucker out_free_chan:
63837c1209dSAneesh Kumar K.V 	kfree(chan);
639b530cc79SEric Van Hensbergen fail:
640b530cc79SEric Van Hensbergen 	return err;
641b530cc79SEric Van Hensbergen }
642b530cc79SEric Van Hensbergen 
643ee443996SEric Van Hensbergen 
644ee443996SEric Van Hensbergen /**
645ee443996SEric Van Hensbergen  * p9_virtio_create - allocate a new virtio channel
6468b81ef58SEric Van Hensbergen  * @client: client instance invoking this transport
647ee443996SEric Van Hensbergen  * @devname: string identifying the channel to connect to (unused)
648ee443996SEric Van Hensbergen  * @args: args passed from sys_mount() for per-transport options (unused)
649ee443996SEric Van Hensbergen  *
650ee443996SEric Van Hensbergen  * This sets up a transport channel for 9p communication.  Right now
651b530cc79SEric Van Hensbergen  * we only match the first available channel, but eventually we couldlook up
652b530cc79SEric Van Hensbergen  * alternate channels by matching devname versus a virtio_config entry.
653b530cc79SEric Van Hensbergen  * We use a simple reference count mechanism to ensure that only a single
654ee443996SEric Van Hensbergen  * mount has a channel open at a time.
655ee443996SEric Van Hensbergen  *
656ee443996SEric Van Hensbergen  */
657ee443996SEric Van Hensbergen 
6588b81ef58SEric Van Hensbergen static int
6598b81ef58SEric Van Hensbergen p9_virtio_create(struct p9_client *client, const char *devname, char *args)
660b530cc79SEric Van Hensbergen {
66137c1209dSAneesh Kumar K.V 	struct virtio_chan *chan;
662c1a7c226SAneesh Kumar K.V 	int ret = -ENOENT;
66337c1209dSAneesh Kumar K.V 	int found = 0;
664b530cc79SEric Van Hensbergen 
66510aa1452STomas Bortoli 	if (devname == NULL)
66610aa1452STomas Bortoli 		return -EINVAL;
66710aa1452STomas Bortoli 
668c1549497SJosef 'Jeff' Sipek 	mutex_lock(&virtio_9p_lock);
66937c1209dSAneesh Kumar K.V 	list_for_each_entry(chan, &virtio_chan_list, chan_list) {
670edcd9d97Spiaojun 		if (!strcmp(devname, chan->tag)) {
671f75580c4SAneesh Kumar K.V 			if (!chan->inuse) {
672b530cc79SEric Van Hensbergen 				chan->inuse = true;
67337c1209dSAneesh Kumar K.V 				found = 1;
674b530cc79SEric Van Hensbergen 				break;
675f75580c4SAneesh Kumar K.V 			}
676c1a7c226SAneesh Kumar K.V 			ret = -EBUSY;
677f75580c4SAneesh Kumar K.V 		}
678b530cc79SEric Van Hensbergen 	}
679c1549497SJosef 'Jeff' Sipek 	mutex_unlock(&virtio_9p_lock);
680b530cc79SEric Van Hensbergen 
68137c1209dSAneesh Kumar K.V 	if (!found) {
682c7c72c5aSAneesh Kumar K.V 		pr_err("no channels available for device %s\n", devname);
683c1a7c226SAneesh Kumar K.V 		return ret;
684b530cc79SEric Van Hensbergen 	}
685b530cc79SEric Van Hensbergen 
6868b81ef58SEric Van Hensbergen 	client->trans = (void *)chan;
687562ada61SEric Van Hensbergen 	client->status = Connected;
688fea511a6SEric Van Hensbergen 	chan->client = client;
689b530cc79SEric Van Hensbergen 
6908b81ef58SEric Van Hensbergen 	return 0;
691b530cc79SEric Van Hensbergen }
692b530cc79SEric Van Hensbergen 
693ee443996SEric Van Hensbergen /**
694ee443996SEric Van Hensbergen  * p9_virtio_remove - clean up resources associated with a virtio device
695ee443996SEric Van Hensbergen  * @vdev: virtio device to remove
696ee443996SEric Van Hensbergen  *
697ee443996SEric Van Hensbergen  */
698ee443996SEric Van Hensbergen 
699f3933545SEric Van Hensbergen static void p9_virtio_remove(struct virtio_device *vdev)
700f3933545SEric Van Hensbergen {
701f3933545SEric Van Hensbergen 	struct virtio_chan *chan = vdev->priv;
7028051a2a5SMichael S. Tsirkin 	unsigned long warning_time;
70337c1209dSAneesh Kumar K.V 
70437c1209dSAneesh Kumar K.V 	mutex_lock(&virtio_9p_lock);
7058051a2a5SMichael S. Tsirkin 
7068051a2a5SMichael S. Tsirkin 	/* Remove self from list so we don't get new users. */
70737c1209dSAneesh Kumar K.V 	list_del(&chan->chan_list);
7088051a2a5SMichael S. Tsirkin 	warning_time = jiffies;
7098051a2a5SMichael S. Tsirkin 
7108051a2a5SMichael S. Tsirkin 	/* Wait for existing users to close. */
7118051a2a5SMichael S. Tsirkin 	while (chan->inuse) {
71237c1209dSAneesh Kumar K.V 		mutex_unlock(&virtio_9p_lock);
7138051a2a5SMichael S. Tsirkin 		msleep(250);
7148051a2a5SMichael S. Tsirkin 		if (time_after(jiffies, warning_time + 10 * HZ)) {
7158051a2a5SMichael S. Tsirkin 			dev_emerg(&vdev->dev,
7168051a2a5SMichael S. Tsirkin 				  "p9_virtio_remove: waiting for device in use.\n");
7178051a2a5SMichael S. Tsirkin 			warning_time = jiffies;
7188051a2a5SMichael S. Tsirkin 		}
7198051a2a5SMichael S. Tsirkin 		mutex_lock(&virtio_9p_lock);
7208051a2a5SMichael S. Tsirkin 	}
7218051a2a5SMichael S. Tsirkin 
7228051a2a5SMichael S. Tsirkin 	mutex_unlock(&virtio_9p_lock);
7238051a2a5SMichael S. Tsirkin 
724ea52bf8eSPierre Morel 	vdev->config->reset(vdev);
7258051a2a5SMichael S. Tsirkin 	vdev->config->del_vqs(vdev);
7268051a2a5SMichael S. Tsirkin 
72786c84373SAneesh Kumar K.V 	sysfs_remove_file(&(vdev->dev.kobj), &dev_attr_mount_tag.attr);
728e0d6cb9cSMichael Marineau 	kobject_uevent(&(vdev->dev.kobj), KOBJ_CHANGE);
72997ee9b02SAneesh Kumar K.V 	kfree(chan->tag);
73052f44e0dSVenkateswararao Jujjuri (JV) 	kfree(chan->vc_wq);
73137c1209dSAneesh Kumar K.V 	kfree(chan);
73237c1209dSAneesh Kumar K.V 
733f3933545SEric Van Hensbergen }
734f3933545SEric Van Hensbergen 
735b530cc79SEric Van Hensbergen static struct virtio_device_id id_table[] = {
736b530cc79SEric Van Hensbergen 	{ VIRTIO_ID_9P, VIRTIO_DEV_ANY_ID },
737b530cc79SEric Van Hensbergen 	{ 0 },
738b530cc79SEric Van Hensbergen };
739b530cc79SEric Van Hensbergen 
74097ee9b02SAneesh Kumar K.V static unsigned int features[] = {
74197ee9b02SAneesh Kumar K.V 	VIRTIO_9P_MOUNT_TAG,
74297ee9b02SAneesh Kumar K.V };
74397ee9b02SAneesh Kumar K.V 
744b530cc79SEric Van Hensbergen /* The standard "struct lguest_driver": */
745b530cc79SEric Van Hensbergen static struct virtio_driver p9_virtio_drv = {
74697ee9b02SAneesh Kumar K.V 	.feature_table  = features,
74797ee9b02SAneesh Kumar K.V 	.feature_table_size = ARRAY_SIZE(features),
748b530cc79SEric Van Hensbergen 	.driver.name    = KBUILD_MODNAME,
749b530cc79SEric Van Hensbergen 	.driver.owner	= THIS_MODULE,
750b530cc79SEric Van Hensbergen 	.id_table	= id_table,
751b530cc79SEric Van Hensbergen 	.probe		= p9_virtio_probe,
752f3933545SEric Van Hensbergen 	.remove		= p9_virtio_remove,
753b530cc79SEric Van Hensbergen };
754b530cc79SEric Van Hensbergen 
755b530cc79SEric Van Hensbergen static struct p9_trans_module p9_virtio_trans = {
756b530cc79SEric Van Hensbergen 	.name = "virtio",
757b530cc79SEric Van Hensbergen 	.create = p9_virtio_create,
7588b81ef58SEric Van Hensbergen 	.close = p9_virtio_close,
75991b8534fSEric Van Hensbergen 	.request = p9_virtio_request,
760dc893e19SArnd Bergmann 	.zc_request = p9_virtio_zc_request,
76191b8534fSEric Van Hensbergen 	.cancel = p9_virtio_cancel,
762728356deSTomas Bortoli 	.cancelled = p9_virtio_cancelled,
763b49d8b5dSAneesh Kumar K.V 	/*
764b49d8b5dSAneesh Kumar K.V 	 * We leave one entry for input and one entry for response
7658ab1784dSZheng Yongjun 	 * headers. We also skip one more entry to accommodate, address
766b49d8b5dSAneesh Kumar K.V 	 * that are not at page boundary, that can result in an extra
767b49d8b5dSAneesh Kumar K.V 	 * page in zero copy.
768b49d8b5dSAneesh Kumar K.V 	 */
769b49d8b5dSAneesh Kumar K.V 	.maxsize = PAGE_SIZE * (VIRTQUEUE_NUM - 3),
770f94741fdSEric Van Hensbergen 	.def = 1,
77172029fe8STejun Heo 	.owner = THIS_MODULE,
772b530cc79SEric Van Hensbergen };
773b530cc79SEric Van Hensbergen 
774b530cc79SEric Van Hensbergen /* The standard init function */
775b530cc79SEric Van Hensbergen static int __init p9_virtio_init(void)
776b530cc79SEric Van Hensbergen {
777d4548543SYueHaibing 	int rc;
778d4548543SYueHaibing 
77937c1209dSAneesh Kumar K.V 	INIT_LIST_HEAD(&virtio_chan_list);
780b530cc79SEric Van Hensbergen 
781b530cc79SEric Van Hensbergen 	v9fs_register_trans(&p9_virtio_trans);
782d4548543SYueHaibing 	rc = register_virtio_driver(&p9_virtio_drv);
783d4548543SYueHaibing 	if (rc)
784d4548543SYueHaibing 		v9fs_unregister_trans(&p9_virtio_trans);
785d4548543SYueHaibing 
786d4548543SYueHaibing 	return rc;
787b530cc79SEric Van Hensbergen }
788b530cc79SEric Van Hensbergen 
789f3933545SEric Van Hensbergen static void __exit p9_virtio_cleanup(void)
790f3933545SEric Van Hensbergen {
791f3933545SEric Van Hensbergen 	unregister_virtio_driver(&p9_virtio_drv);
79272029fe8STejun Heo 	v9fs_unregister_trans(&p9_virtio_trans);
793f3933545SEric Van Hensbergen }
794f3933545SEric Van Hensbergen 
795b530cc79SEric Van Hensbergen module_init(p9_virtio_init);
796f3933545SEric Van Hensbergen module_exit(p9_virtio_cleanup);
797*4cd82a5bSThomas Weißschuh MODULE_ALIAS_9P("virtio");
798b530cc79SEric Van Hensbergen 
799b530cc79SEric Van Hensbergen MODULE_DEVICE_TABLE(virtio, id_table);
800b530cc79SEric Van Hensbergen MODULE_AUTHOR("Eric Van Hensbergen <ericvh@gmail.com>");
801b530cc79SEric Van Hensbergen MODULE_DESCRIPTION("Virtio 9p Transport");
802b530cc79SEric Van Hensbergen MODULE_LICENSE("GPL");
803