xref: /openbmc/linux/net/9p/trans_virtio.c (revision 4f3b35c157e43107cc7e1f1aa06694e8b22e10bb)
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  * @initialized: whether the channel is initialized
64ee443996SEric Van Hensbergen  * @inuse: whether the channel is in use
65ee443996SEric Van Hensbergen  * @lock: protects multiple elements within this structure
660e15597eSAbhishek Kulkarni  * @client: client instance
67ee443996SEric Van Hensbergen  * @vdev: virtio dev associated with this channel
68ee443996SEric Van Hensbergen  * @vq: virtio queue associated with this channel
69ee443996SEric Van Hensbergen  * @sg: scatter gather list which is used to pack a request (protected?)
70ee443996SEric Van Hensbergen  *
71ee443996SEric Van Hensbergen  * We keep all per-channel information in a structure.
72b530cc79SEric Van Hensbergen  * This structure is allocated within the devices dev->mem space.
73b530cc79SEric Van Hensbergen  * A pointer to the structure will get put in the transport private.
74ee443996SEric Van Hensbergen  *
75b530cc79SEric Van Hensbergen  */
76ee443996SEric Van Hensbergen 
7737c1209dSAneesh Kumar K.V struct virtio_chan {
78ee443996SEric Van Hensbergen 	bool inuse;
79b530cc79SEric Van Hensbergen 
80e2735b77SEric Van Hensbergen 	spinlock_t lock;
81e2735b77SEric Van Hensbergen 
82fea511a6SEric Van Hensbergen 	struct p9_client *client;
83b530cc79SEric Van Hensbergen 	struct virtio_device *vdev;
84e2735b77SEric Van Hensbergen 	struct virtqueue *vq;
8552f44e0dSVenkateswararao Jujjuri (JV) 	int ring_bufs_avail;
8652f44e0dSVenkateswararao Jujjuri (JV) 	wait_queue_head_t *vc_wq;
8768da9ba4SVenkateswararao Jujjuri (JV) 	/* This is global limit. Since we don't have a global structure,
8868da9ba4SVenkateswararao Jujjuri (JV) 	 * will be placing it in each channel.
8968da9ba4SVenkateswararao Jujjuri (JV) 	 */
907293bfbaSZhang Yanfei 	unsigned long p9_max_pages;
91e2735b77SEric Van Hensbergen 	/* Scatterlist: can be too big for stack. */
92e2735b77SEric Van Hensbergen 	struct scatterlist sg[VIRTQUEUE_NUM];
9337c1209dSAneesh Kumar K.V 
9497ee9b02SAneesh Kumar K.V 	int tag_len;
9597ee9b02SAneesh Kumar K.V 	/*
9697ee9b02SAneesh Kumar K.V 	 * tag name to identify a mount Non-null terminated
9797ee9b02SAneesh Kumar K.V 	 */
9897ee9b02SAneesh Kumar K.V 	char *tag;
9997ee9b02SAneesh Kumar K.V 
10037c1209dSAneesh Kumar K.V 	struct list_head chan_list;
10137c1209dSAneesh Kumar K.V };
10237c1209dSAneesh Kumar K.V 
10337c1209dSAneesh Kumar K.V static struct list_head virtio_chan_list;
104b530cc79SEric Van Hensbergen 
105b530cc79SEric Van Hensbergen /* How many bytes left in this page. */
106b530cc79SEric Van Hensbergen static unsigned int rest_of_page(void *data)
107b530cc79SEric Van Hensbergen {
108b530cc79SEric Van Hensbergen 	return PAGE_SIZE - ((unsigned long)data % PAGE_SIZE);
109b530cc79SEric Van Hensbergen }
110b530cc79SEric Van Hensbergen 
111ee443996SEric Van Hensbergen /**
112ee443996SEric Van Hensbergen  * p9_virtio_close - reclaim resources of a channel
1130e15597eSAbhishek Kulkarni  * @client: client instance
114ee443996SEric Van Hensbergen  *
115ee443996SEric Van Hensbergen  * This reclaims a channel by freeing its resources and
116ee443996SEric Van Hensbergen  * reseting its inuse flag.
117ee443996SEric Van Hensbergen  *
118ee443996SEric Van Hensbergen  */
119ee443996SEric Van Hensbergen 
1208b81ef58SEric Van Hensbergen static void p9_virtio_close(struct p9_client *client)
121b530cc79SEric Van Hensbergen {
1228b81ef58SEric Van Hensbergen 	struct virtio_chan *chan = client->trans;
123b530cc79SEric Van Hensbergen 
124c1549497SJosef 'Jeff' Sipek 	mutex_lock(&virtio_9p_lock);
125fb786100SAneesh Kumar K.V 	if (chan)
126b530cc79SEric Van Hensbergen 		chan->inuse = false;
127c1549497SJosef 'Jeff' Sipek 	mutex_unlock(&virtio_9p_lock);
128b530cc79SEric Van Hensbergen }
129b530cc79SEric Van Hensbergen 
130ee443996SEric Van Hensbergen /**
131ee443996SEric Van Hensbergen  * req_done - callback which signals activity from the server
132ee443996SEric Van Hensbergen  * @vq: virtio queue activity was received on
133ee443996SEric Van Hensbergen  *
134ee443996SEric Van Hensbergen  * This notifies us that the server has triggered some activity
135ee443996SEric Van Hensbergen  * on the virtio channel - most likely a response to request we
136ee443996SEric Van Hensbergen  * sent.  Figure out which requests now have responses and wake up
137ee443996SEric Van Hensbergen  * those threads.
138ee443996SEric Van Hensbergen  *
139ee443996SEric Van Hensbergen  * Bugs: could do with some additional sanity checking, but appears to work.
140ee443996SEric Van Hensbergen  *
141ee443996SEric Van Hensbergen  */
142ee443996SEric Van Hensbergen 
143e2735b77SEric Van Hensbergen static void req_done(struct virtqueue *vq)
144b530cc79SEric Van Hensbergen {
145e2735b77SEric Van Hensbergen 	struct virtio_chan *chan = vq->vdev->priv;
146e2735b77SEric Van Hensbergen 	struct p9_fcall *rc;
147e2735b77SEric Van Hensbergen 	unsigned int len;
148e2735b77SEric Van Hensbergen 	struct p9_req_t *req;
149419b3956SVenkateswararao Jujjuri (JV) 	unsigned long flags;
150b530cc79SEric Van Hensbergen 
1515d385153SJoe Perches 	p9_debug(P9_DEBUG_TRANS, ": request done\n");
15291b8534fSEric Van Hensbergen 
153a01a9840SVenkateswararao Jujjuri (JV) 	while (1) {
154419b3956SVenkateswararao Jujjuri (JV) 		spin_lock_irqsave(&chan->lock, flags);
155419b3956SVenkateswararao Jujjuri (JV) 		rc = virtqueue_get_buf(chan->vq, &len);
156a01a9840SVenkateswararao Jujjuri (JV) 		if (rc == NULL) {
157a01a9840SVenkateswararao Jujjuri (JV) 			spin_unlock_irqrestore(&chan->lock, flags);
158a01a9840SVenkateswararao Jujjuri (JV) 			break;
159a01a9840SVenkateswararao Jujjuri (JV) 		}
16052f44e0dSVenkateswararao Jujjuri (JV) 		chan->ring_bufs_avail = 1;
16152f44e0dSVenkateswararao Jujjuri (JV) 		spin_unlock_irqrestore(&chan->lock, flags);
16253bda3e5SVenkateswararao Jujjuri (JV) 		/* Wakeup if anyone waiting for VirtIO ring space. */
16353bda3e5SVenkateswararao Jujjuri (JV) 		wake_up(chan->vc_wq);
1645d385153SJoe Perches 		p9_debug(P9_DEBUG_TRANS, ": rc %p\n", rc);
1655d385153SJoe Perches 		p9_debug(P9_DEBUG_TRANS, ": lookup tag %d\n", rc->tag);
166fea511a6SEric Van Hensbergen 		req = p9_tag_lookup(chan->client, rc->tag);
1672b6e72edSDominique Martinet 		p9_client_cb(chan->client, req, REQ_STATUS_RCVD);
168e2735b77SEric Van Hensbergen 	}
169b530cc79SEric Van Hensbergen }
170b530cc79SEric Van Hensbergen 
171ee443996SEric Van Hensbergen /**
172ee443996SEric Van Hensbergen  * pack_sg_list - pack a scatter gather list from a linear buffer
173ee443996SEric Van Hensbergen  * @sg: scatter/gather list to pack into
174ee443996SEric Van Hensbergen  * @start: which segment of the sg_list to start at
175ee443996SEric Van Hensbergen  * @limit: maximum segment to pack data to
176ee443996SEric Van Hensbergen  * @data: data to pack into scatter/gather list
177ee443996SEric Van Hensbergen  * @count: amount of data to pack into the scatter/gather list
178ee443996SEric Van Hensbergen  *
179ee443996SEric Van Hensbergen  * sg_lists have multiple segments of various sizes.  This will pack
180ee443996SEric Van Hensbergen  * arbitrary data into an existing scatter gather list, segmenting the
181ee443996SEric Van Hensbergen  * data as necessary within constraints.
182ee443996SEric Van Hensbergen  *
183ee443996SEric Van Hensbergen  */
184ee443996SEric Van Hensbergen 
185abfa034eSAneesh Kumar K.V static int pack_sg_list(struct scatterlist *sg, int start,
186abfa034eSAneesh Kumar K.V 			int limit, char *data, int count)
187e2735b77SEric Van Hensbergen {
188e2735b77SEric Van Hensbergen 	int s;
189e2735b77SEric Van Hensbergen 	int index = start;
190e2735b77SEric Van Hensbergen 
191e2735b77SEric Van Hensbergen 	while (count) {
192e2735b77SEric Van Hensbergen 		s = rest_of_page(data);
193e2735b77SEric Van Hensbergen 		if (s > count)
194e2735b77SEric Van Hensbergen 			s = count;
1955fcb08beSSasha Levin 		BUG_ON(index > limit);
1960b36f1adSRusty Russell 		/* Make sure we don't terminate early. */
1970b36f1adSRusty Russell 		sg_unmark_end(&sg[index]);
198e2735b77SEric Van Hensbergen 		sg_set_buf(&sg[index++], data, s);
199e2735b77SEric Van Hensbergen 		count -= s;
200e2735b77SEric Van Hensbergen 		data += s;
201e2735b77SEric Van Hensbergen 	}
2020b36f1adSRusty Russell 	if (index-start)
2030b36f1adSRusty Russell 		sg_mark_end(&sg[index - 1]);
204e2735b77SEric Van Hensbergen 	return index-start;
205e2735b77SEric Van Hensbergen }
206e2735b77SEric Van Hensbergen 
20791b8534fSEric Van Hensbergen /* We don't currently allow canceling of virtio requests */
20891b8534fSEric Van Hensbergen static int p9_virtio_cancel(struct p9_client *client, struct p9_req_t *req)
20991b8534fSEric Van Hensbergen {
21091b8534fSEric Van Hensbergen 	return 1;
21191b8534fSEric Van Hensbergen }
21291b8534fSEric Van Hensbergen 
213ee443996SEric Van Hensbergen /**
2144038866dSVenkateswararao Jujjuri (JV)  * pack_sg_list_p - Just like pack_sg_list. Instead of taking a buffer,
2154038866dSVenkateswararao Jujjuri (JV)  * this takes a list of pages.
2164038866dSVenkateswararao Jujjuri (JV)  * @sg: scatter/gather list to pack into
2174038866dSVenkateswararao Jujjuri (JV)  * @start: which segment of the sg_list to start at
2182c53040fSBen Hutchings  * @pdata: a list of pages to add into sg.
219abfa034eSAneesh Kumar K.V  * @nr_pages: number of pages to pack into the scatter/gather list
220*4f3b35c1SAl Viro  * @offs: amount of data in the beginning of first page _not_ to pack
2214038866dSVenkateswararao Jujjuri (JV)  * @count: amount of data to pack into the scatter/gather list
2224038866dSVenkateswararao Jujjuri (JV)  */
2234038866dSVenkateswararao Jujjuri (JV) static int
224abfa034eSAneesh Kumar K.V pack_sg_list_p(struct scatterlist *sg, int start, int limit,
225*4f3b35c1SAl Viro 	       struct page **pdata, int nr_pages, size_t offs, int count)
2264038866dSVenkateswararao Jujjuri (JV) {
227abfa034eSAneesh Kumar K.V 	int i = 0, s;
228*4f3b35c1SAl Viro 	int data_off = offs;
2294038866dSVenkateswararao Jujjuri (JV) 	int index = start;
2304038866dSVenkateswararao Jujjuri (JV) 
231abfa034eSAneesh Kumar K.V 	BUG_ON(nr_pages > (limit - start));
232abfa034eSAneesh Kumar K.V 	/*
233abfa034eSAneesh Kumar K.V 	 * if the first page doesn't start at
234abfa034eSAneesh Kumar K.V 	 * page boundary find the offset
235abfa034eSAneesh Kumar K.V 	 */
236abfa034eSAneesh Kumar K.V 	while (nr_pages) {
237*4f3b35c1SAl Viro 		s = PAGE_SIZE - data_off;
238abfa034eSAneesh Kumar K.V 		if (s > count)
239abfa034eSAneesh Kumar K.V 			s = count;
2400b36f1adSRusty Russell 		/* Make sure we don't terminate early. */
2410b36f1adSRusty Russell 		sg_unmark_end(&sg[index]);
242abfa034eSAneesh Kumar K.V 		sg_set_page(&sg[index++], pdata[i++], s, data_off);
243abfa034eSAneesh Kumar K.V 		data_off = 0;
2444038866dSVenkateswararao Jujjuri (JV) 		count -= s;
245abfa034eSAneesh Kumar K.V 		nr_pages--;
2464038866dSVenkateswararao Jujjuri (JV) 	}
2470b36f1adSRusty Russell 
2480b36f1adSRusty Russell 	if (index-start)
2490b36f1adSRusty Russell 		sg_mark_end(&sg[index - 1]);
2504038866dSVenkateswararao Jujjuri (JV) 	return index - start;
2514038866dSVenkateswararao Jujjuri (JV) }
2524038866dSVenkateswararao Jujjuri (JV) 
2534038866dSVenkateswararao Jujjuri (JV) /**
25491b8534fSEric Van Hensbergen  * p9_virtio_request - issue a request
2550e15597eSAbhishek Kulkarni  * @client: client instance issuing the request
2560e15597eSAbhishek Kulkarni  * @req: request to be issued
257ee443996SEric Van Hensbergen  *
258ee443996SEric Van Hensbergen  */
259ee443996SEric Van Hensbergen 
260e2735b77SEric Van Hensbergen static int
26191b8534fSEric Van Hensbergen p9_virtio_request(struct p9_client *client, struct p9_req_t *req)
262e2735b77SEric Van Hensbergen {
263abfa034eSAneesh Kumar K.V 	int err;
2640b36f1adSRusty Russell 	int in, out, out_sgs, in_sgs;
265419b3956SVenkateswararao Jujjuri (JV) 	unsigned long flags;
266abfa034eSAneesh Kumar K.V 	struct virtio_chan *chan = client->trans;
2670b36f1adSRusty Russell 	struct scatterlist *sgs[2];
268e2735b77SEric Van Hensbergen 
2695d385153SJoe Perches 	p9_debug(P9_DEBUG_TRANS, "9p debug: virtio request\n");
270e2735b77SEric Van Hensbergen 
271419b3956SVenkateswararao Jujjuri (JV) 	req->status = REQ_STATUS_SENT;
272abfa034eSAneesh Kumar K.V req_retry:
273419b3956SVenkateswararao Jujjuri (JV) 	spin_lock_irqsave(&chan->lock, flags);
2744038866dSVenkateswararao Jujjuri (JV) 
2750b36f1adSRusty Russell 	out_sgs = in_sgs = 0;
2764038866dSVenkateswararao Jujjuri (JV) 	/* Handle out VirtIO ring buffers */
277abfa034eSAneesh Kumar K.V 	out = pack_sg_list(chan->sg, 0,
278abfa034eSAneesh Kumar K.V 			   VIRTQUEUE_NUM, req->tc->sdata, req->tc->size);
2790b36f1adSRusty Russell 	if (out)
2800b36f1adSRusty Russell 		sgs[out_sgs++] = chan->sg;
2814038866dSVenkateswararao Jujjuri (JV) 
282abfa034eSAneesh Kumar K.V 	in = pack_sg_list(chan->sg, out,
283abfa034eSAneesh Kumar K.V 			  VIRTQUEUE_NUM, req->rc->sdata, req->rc->capacity);
2840b36f1adSRusty Russell 	if (in)
2850b36f1adSRusty Russell 		sgs[out_sgs + in_sgs++] = chan->sg + out;
286e2735b77SEric Van Hensbergen 
2870b36f1adSRusty Russell 	err = virtqueue_add_sgs(chan->vq, sgs, out_sgs, in_sgs, req->tc,
288f96fde41SRusty Russell 				GFP_ATOMIC);
289419b3956SVenkateswararao Jujjuri (JV) 	if (err < 0) {
29052f44e0dSVenkateswararao Jujjuri (JV) 		if (err == -ENOSPC) {
29152f44e0dSVenkateswararao Jujjuri (JV) 			chan->ring_bufs_avail = 0;
29252f44e0dSVenkateswararao Jujjuri (JV) 			spin_unlock_irqrestore(&chan->lock, flags);
29352f44e0dSVenkateswararao Jujjuri (JV) 			err = wait_event_interruptible(*chan->vc_wq,
29452f44e0dSVenkateswararao Jujjuri (JV) 							chan->ring_bufs_avail);
29552f44e0dSVenkateswararao Jujjuri (JV) 			if (err  == -ERESTARTSYS)
29652f44e0dSVenkateswararao Jujjuri (JV) 				return err;
29752f44e0dSVenkateswararao Jujjuri (JV) 
2985d385153SJoe Perches 			p9_debug(P9_DEBUG_TRANS, "Retry virtio request\n");
299abfa034eSAneesh Kumar K.V 			goto req_retry;
300abfa034eSAneesh Kumar K.V 		} else {
301abfa034eSAneesh Kumar K.V 			spin_unlock_irqrestore(&chan->lock, flags);
3025d385153SJoe Perches 			p9_debug(P9_DEBUG_TRANS,
3030b36f1adSRusty Russell 				 "virtio rpc add_sgs returned failure\n");
304abfa034eSAneesh Kumar K.V 			return -EIO;
305abfa034eSAneesh Kumar K.V 		}
306abfa034eSAneesh Kumar K.V 	}
307abfa034eSAneesh Kumar K.V 	virtqueue_kick(chan->vq);
308abfa034eSAneesh Kumar K.V 	spin_unlock_irqrestore(&chan->lock, flags);
309abfa034eSAneesh Kumar K.V 
3105d385153SJoe Perches 	p9_debug(P9_DEBUG_TRANS, "virtio request kicked\n");
311abfa034eSAneesh Kumar K.V 	return 0;
312abfa034eSAneesh Kumar K.V }
313abfa034eSAneesh Kumar K.V 
314abfa034eSAneesh Kumar K.V static int p9_get_mapped_pages(struct virtio_chan *chan,
315*4f3b35c1SAl Viro 			       struct page ***pages,
316*4f3b35c1SAl Viro 			       struct iov_iter *data,
317*4f3b35c1SAl Viro 			       int count,
318*4f3b35c1SAl Viro 			       size_t *offs,
319*4f3b35c1SAl Viro 			       int *need_drop)
320abfa034eSAneesh Kumar K.V {
321*4f3b35c1SAl Viro 	int nr_pages;
322abfa034eSAneesh Kumar K.V 	int err;
323*4f3b35c1SAl Viro 
324*4f3b35c1SAl Viro 	if (!iov_iter_count(data))
325*4f3b35c1SAl Viro 		return 0;
326*4f3b35c1SAl Viro 
327*4f3b35c1SAl Viro 	if (!(data->type & ITER_KVEC)) {
328*4f3b35c1SAl Viro 		int n;
329abfa034eSAneesh Kumar K.V 		/*
330abfa034eSAneesh Kumar K.V 		 * We allow only p9_max_pages pinned. We wait for the
331abfa034eSAneesh Kumar K.V 		 * Other zc request to finish here
332abfa034eSAneesh Kumar K.V 		 */
333abfa034eSAneesh Kumar K.V 		if (atomic_read(&vp_pinned) >= chan->p9_max_pages) {
334abfa034eSAneesh Kumar K.V 			err = wait_event_interruptible(vp_wq,
335abfa034eSAneesh Kumar K.V 			      (atomic_read(&vp_pinned) < chan->p9_max_pages));
336abfa034eSAneesh Kumar K.V 			if (err == -ERESTARTSYS)
337abfa034eSAneesh Kumar K.V 				return err;
338abfa034eSAneesh Kumar K.V 		}
339*4f3b35c1SAl Viro 		n = iov_iter_get_pages_alloc(data, pages, count, offs);
340*4f3b35c1SAl Viro 		if (n < 0)
341*4f3b35c1SAl Viro 			return n;
342*4f3b35c1SAl Viro 		*need_drop = 1;
343*4f3b35c1SAl Viro 		nr_pages = DIV_ROUND_UP(n + *offs, PAGE_SIZE);
344abfa034eSAneesh Kumar K.V 		atomic_add(nr_pages, &vp_pinned);
345*4f3b35c1SAl Viro 		return n;
346abfa034eSAneesh Kumar K.V 	} else {
347abfa034eSAneesh Kumar K.V 		/* kernel buffer, no need to pin pages */
348*4f3b35c1SAl Viro 		int index;
349*4f3b35c1SAl Viro 		size_t len;
350*4f3b35c1SAl Viro 		void *p;
351*4f3b35c1SAl Viro 
352*4f3b35c1SAl Viro 		/* we'd already checked that it's non-empty */
353*4f3b35c1SAl Viro 		while (1) {
354*4f3b35c1SAl Viro 			len = iov_iter_single_seg_count(data);
355*4f3b35c1SAl Viro 			if (likely(len)) {
356*4f3b35c1SAl Viro 				p = data->kvec->iov_base + data->iov_offset;
357*4f3b35c1SAl Viro 				break;
358*4f3b35c1SAl Viro 			}
359*4f3b35c1SAl Viro 			iov_iter_advance(data, 0);
360*4f3b35c1SAl Viro 		}
361*4f3b35c1SAl Viro 		if (len > count)
362*4f3b35c1SAl Viro 			len = count;
363*4f3b35c1SAl Viro 
364*4f3b35c1SAl Viro 		nr_pages = DIV_ROUND_UP((unsigned long)p + len, PAGE_SIZE) -
365*4f3b35c1SAl Viro 			   (unsigned long)p / PAGE_SIZE;
366*4f3b35c1SAl Viro 
367*4f3b35c1SAl Viro 		*pages = kmalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
368*4f3b35c1SAl Viro 		if (!*pages)
369*4f3b35c1SAl Viro 			return -ENOMEM;
370*4f3b35c1SAl Viro 
371*4f3b35c1SAl Viro 		*need_drop = 0;
372*4f3b35c1SAl Viro 		p -= (*offs = (unsigned long)p % PAGE_SIZE);
373*4f3b35c1SAl Viro 		for (index = 0; index < nr_pages; index++) {
374*4f3b35c1SAl Viro 			if (is_vmalloc_addr(p))
375*4f3b35c1SAl Viro 				(*pages)[index] = vmalloc_to_page(p);
376b6f52ae2SRichard Yao 			else
377*4f3b35c1SAl Viro 				(*pages)[index] = kmap_to_page(p);
378*4f3b35c1SAl Viro 			p += PAGE_SIZE;
379abfa034eSAneesh Kumar K.V 		}
380*4f3b35c1SAl Viro 		return len;
381abfa034eSAneesh Kumar K.V 	}
382abfa034eSAneesh Kumar K.V }
383abfa034eSAneesh Kumar K.V 
384abfa034eSAneesh Kumar K.V /**
385abfa034eSAneesh Kumar K.V  * p9_virtio_zc_request - issue a zero copy request
386abfa034eSAneesh Kumar K.V  * @client: client instance issuing the request
387abfa034eSAneesh Kumar K.V  * @req: request to be issued
388abfa034eSAneesh Kumar K.V  * @uidata: user bffer that should be ued for zero copy read
389abfa034eSAneesh Kumar K.V  * @uodata: user buffer that shoud be user for zero copy write
390abfa034eSAneesh Kumar K.V  * @inlen: read buffer size
391abfa034eSAneesh Kumar K.V  * @olen: write buffer size
392abfa034eSAneesh Kumar K.V  * @hdrlen: reader header size, This is the size of response protocol data
393abfa034eSAneesh Kumar K.V  *
394abfa034eSAneesh Kumar K.V  */
395abfa034eSAneesh Kumar K.V static int
396abfa034eSAneesh Kumar K.V p9_virtio_zc_request(struct p9_client *client, struct p9_req_t *req,
397*4f3b35c1SAl Viro 		     struct iov_iter *uidata, struct iov_iter *uodata,
398*4f3b35c1SAl Viro 		     int inlen, int outlen, int in_hdr_len)
399abfa034eSAneesh Kumar K.V {
4000b36f1adSRusty Russell 	int in, out, err, out_sgs, in_sgs;
401abfa034eSAneesh Kumar K.V 	unsigned long flags;
402abfa034eSAneesh Kumar K.V 	int in_nr_pages = 0, out_nr_pages = 0;
403abfa034eSAneesh Kumar K.V 	struct page **in_pages = NULL, **out_pages = NULL;
404abfa034eSAneesh Kumar K.V 	struct virtio_chan *chan = client->trans;
4050b36f1adSRusty Russell 	struct scatterlist *sgs[4];
406*4f3b35c1SAl Viro 	size_t offs;
407*4f3b35c1SAl Viro 	int need_drop = 0;
408abfa034eSAneesh Kumar K.V 
4095d385153SJoe Perches 	p9_debug(P9_DEBUG_TRANS, "virtio request\n");
410abfa034eSAneesh Kumar K.V 
411abfa034eSAneesh Kumar K.V 	if (uodata) {
412*4f3b35c1SAl Viro 		int n = p9_get_mapped_pages(chan, &out_pages, uodata,
413*4f3b35c1SAl Viro 					    outlen, &offs, &need_drop);
414*4f3b35c1SAl Viro 		if (n < 0)
415*4f3b35c1SAl Viro 			return n;
416*4f3b35c1SAl Viro 		out_nr_pages = DIV_ROUND_UP(n + offs, PAGE_SIZE);
417*4f3b35c1SAl Viro 		if (n != outlen) {
418*4f3b35c1SAl Viro 			__le32 v = cpu_to_le32(n);
419*4f3b35c1SAl Viro 			memcpy(&req->tc->sdata[req->tc->size - 4], &v, 4);
420*4f3b35c1SAl Viro 			outlen = n;
421abfa034eSAneesh Kumar K.V 		}
422*4f3b35c1SAl Viro 	} else if (uidata) {
423*4f3b35c1SAl Viro 		int n = p9_get_mapped_pages(chan, &in_pages, uidata,
424*4f3b35c1SAl Viro 					    inlen, &offs, &need_drop);
425*4f3b35c1SAl Viro 		if (n < 0)
426*4f3b35c1SAl Viro 			return n;
427*4f3b35c1SAl Viro 		in_nr_pages = DIV_ROUND_UP(n + offs, PAGE_SIZE);
428*4f3b35c1SAl Viro 		if (n != inlen) {
429*4f3b35c1SAl Viro 			__le32 v = cpu_to_le32(n);
430*4f3b35c1SAl Viro 			memcpy(&req->tc->sdata[req->tc->size - 4], &v, 4);
431*4f3b35c1SAl Viro 			inlen = n;
432abfa034eSAneesh Kumar K.V 		}
433abfa034eSAneesh Kumar K.V 	}
434abfa034eSAneesh Kumar K.V 	req->status = REQ_STATUS_SENT;
435abfa034eSAneesh Kumar K.V req_retry_pinned:
436abfa034eSAneesh Kumar K.V 	spin_lock_irqsave(&chan->lock, flags);
4370b36f1adSRusty Russell 
4380b36f1adSRusty Russell 	out_sgs = in_sgs = 0;
4390b36f1adSRusty Russell 
440abfa034eSAneesh Kumar K.V 	/* out data */
441abfa034eSAneesh Kumar K.V 	out = pack_sg_list(chan->sg, 0,
442abfa034eSAneesh Kumar K.V 			   VIRTQUEUE_NUM, req->tc->sdata, req->tc->size);
443abfa034eSAneesh Kumar K.V 
4440b36f1adSRusty Russell 	if (out)
4450b36f1adSRusty Russell 		sgs[out_sgs++] = chan->sg;
4460b36f1adSRusty Russell 
4470b36f1adSRusty Russell 	if (out_pages) {
4480b36f1adSRusty Russell 		sgs[out_sgs++] = chan->sg + out;
449abfa034eSAneesh Kumar K.V 		out += pack_sg_list_p(chan->sg, out, VIRTQUEUE_NUM,
450*4f3b35c1SAl Viro 				      out_pages, out_nr_pages, offs, outlen);
4510b36f1adSRusty Russell 	}
4520b36f1adSRusty Russell 
453abfa034eSAneesh Kumar K.V 	/*
454abfa034eSAneesh Kumar K.V 	 * Take care of in data
455abfa034eSAneesh Kumar K.V 	 * For example TREAD have 11.
456abfa034eSAneesh Kumar K.V 	 * 11 is the read/write header = PDU Header(7) + IO Size (4).
457abfa034eSAneesh Kumar K.V 	 * Arrange in such a way that server places header in the
458abfa034eSAneesh Kumar K.V 	 * alloced memory and payload onto the user buffer.
459abfa034eSAneesh Kumar K.V 	 */
460abfa034eSAneesh Kumar K.V 	in = pack_sg_list(chan->sg, out,
461abfa034eSAneesh Kumar K.V 			  VIRTQUEUE_NUM, req->rc->sdata, in_hdr_len);
4620b36f1adSRusty Russell 	if (in)
4630b36f1adSRusty Russell 		sgs[out_sgs + in_sgs++] = chan->sg + out;
4640b36f1adSRusty Russell 
4650b36f1adSRusty Russell 	if (in_pages) {
4660b36f1adSRusty Russell 		sgs[out_sgs + in_sgs++] = chan->sg + out + in;
467abfa034eSAneesh Kumar K.V 		in += pack_sg_list_p(chan->sg, out + in, VIRTQUEUE_NUM,
468*4f3b35c1SAl Viro 				     in_pages, in_nr_pages, offs, inlen);
4690b36f1adSRusty Russell 	}
470abfa034eSAneesh Kumar K.V 
4710b36f1adSRusty Russell 	BUG_ON(out_sgs + in_sgs > ARRAY_SIZE(sgs));
4720b36f1adSRusty Russell 	err = virtqueue_add_sgs(chan->vq, sgs, out_sgs, in_sgs, req->tc,
473f96fde41SRusty Russell 				GFP_ATOMIC);
474abfa034eSAneesh Kumar K.V 	if (err < 0) {
475abfa034eSAneesh Kumar K.V 		if (err == -ENOSPC) {
476abfa034eSAneesh Kumar K.V 			chan->ring_bufs_avail = 0;
477abfa034eSAneesh Kumar K.V 			spin_unlock_irqrestore(&chan->lock, flags);
478abfa034eSAneesh Kumar K.V 			err = wait_event_interruptible(*chan->vc_wq,
479abfa034eSAneesh Kumar K.V 						       chan->ring_bufs_avail);
480abfa034eSAneesh Kumar K.V 			if (err  == -ERESTARTSYS)
481abfa034eSAneesh Kumar K.V 				goto err_out;
482abfa034eSAneesh Kumar K.V 
4835d385153SJoe Perches 			p9_debug(P9_DEBUG_TRANS, "Retry virtio request\n");
484316ad550SVenkateswararao Jujjuri (JV) 			goto req_retry_pinned;
48552f44e0dSVenkateswararao Jujjuri (JV) 		} else {
486419b3956SVenkateswararao Jujjuri (JV) 			spin_unlock_irqrestore(&chan->lock, flags);
4875d385153SJoe Perches 			p9_debug(P9_DEBUG_TRANS,
4880b36f1adSRusty Russell 				 "virtio rpc add_sgs returned failure\n");
489abfa034eSAneesh Kumar K.V 			err = -EIO;
490abfa034eSAneesh Kumar K.V 			goto err_out;
491e2735b77SEric Van Hensbergen 		}
49252f44e0dSVenkateswararao Jujjuri (JV) 	}
493dc3f5e68SMichael S. Tsirkin 	virtqueue_kick(chan->vq);
494419b3956SVenkateswararao Jujjuri (JV) 	spin_unlock_irqrestore(&chan->lock, flags);
4955d385153SJoe Perches 	p9_debug(P9_DEBUG_TRANS, "virtio request kicked\n");
496abfa034eSAneesh Kumar K.V 	err = wait_event_interruptible(*req->wq,
497abfa034eSAneesh Kumar K.V 				       req->status >= REQ_STATUS_RCVD);
498abfa034eSAneesh Kumar K.V 	/*
499abfa034eSAneesh Kumar K.V 	 * Non kernel buffers are pinned, unpin them
500abfa034eSAneesh Kumar K.V 	 */
501abfa034eSAneesh Kumar K.V err_out:
502*4f3b35c1SAl Viro 	if (need_drop) {
503abfa034eSAneesh Kumar K.V 		if (in_pages) {
504abfa034eSAneesh Kumar K.V 			p9_release_pages(in_pages, in_nr_pages);
505abfa034eSAneesh Kumar K.V 			atomic_sub(in_nr_pages, &vp_pinned);
506abfa034eSAneesh Kumar K.V 		}
507abfa034eSAneesh Kumar K.V 		if (out_pages) {
508abfa034eSAneesh Kumar K.V 			p9_release_pages(out_pages, out_nr_pages);
509abfa034eSAneesh Kumar K.V 			atomic_sub(out_nr_pages, &vp_pinned);
510abfa034eSAneesh Kumar K.V 		}
511abfa034eSAneesh Kumar K.V 		/* wakeup anybody waiting for slots to pin pages */
512abfa034eSAneesh Kumar K.V 		wake_up(&vp_wq);
513abfa034eSAneesh Kumar K.V 	}
514abfa034eSAneesh Kumar K.V 	kfree(in_pages);
515abfa034eSAneesh Kumar K.V 	kfree(out_pages);
516abfa034eSAneesh Kumar K.V 	return err;
517e2735b77SEric Van Hensbergen }
518e2735b77SEric Van Hensbergen 
51986c84373SAneesh Kumar K.V static ssize_t p9_mount_tag_show(struct device *dev,
52086c84373SAneesh Kumar K.V 				struct device_attribute *attr, char *buf)
52186c84373SAneesh Kumar K.V {
52286c84373SAneesh Kumar K.V 	struct virtio_chan *chan;
52386c84373SAneesh Kumar K.V 	struct virtio_device *vdev;
52486c84373SAneesh Kumar K.V 
52586c84373SAneesh Kumar K.V 	vdev = dev_to_virtio(dev);
52686c84373SAneesh Kumar K.V 	chan = vdev->priv;
52786c84373SAneesh Kumar K.V 
52886c84373SAneesh Kumar K.V 	return snprintf(buf, chan->tag_len + 1, "%s", chan->tag);
52986c84373SAneesh Kumar K.V }
53086c84373SAneesh Kumar K.V 
53186c84373SAneesh Kumar K.V static DEVICE_ATTR(mount_tag, 0444, p9_mount_tag_show, NULL);
53286c84373SAneesh Kumar K.V 
533ee443996SEric Van Hensbergen /**
534ee443996SEric Van Hensbergen  * p9_virtio_probe - probe for existence of 9P virtio channels
535ee443996SEric Van Hensbergen  * @vdev: virtio device to probe
536ee443996SEric Van Hensbergen  *
53737c1209dSAneesh Kumar K.V  * This probes for existing virtio channels.
538ee443996SEric Van Hensbergen  *
539ee443996SEric Van Hensbergen  */
540ee443996SEric Van Hensbergen 
541e2735b77SEric Van Hensbergen static int p9_virtio_probe(struct virtio_device *vdev)
542b530cc79SEric Van Hensbergen {
54397ee9b02SAneesh Kumar K.V 	__u16 tag_len;
54497ee9b02SAneesh Kumar K.V 	char *tag;
545b530cc79SEric Van Hensbergen 	int err;
546b530cc79SEric Van Hensbergen 	struct virtio_chan *chan;
547b530cc79SEric Van Hensbergen 
5487754f53eSMichael S. Tsirkin 	if (!vdev->config->get) {
5497754f53eSMichael S. Tsirkin 		dev_err(&vdev->dev, "%s failure: config access disabled\n",
5507754f53eSMichael S. Tsirkin 			__func__);
5517754f53eSMichael S. Tsirkin 		return -EINVAL;
5527754f53eSMichael S. Tsirkin 	}
5537754f53eSMichael S. Tsirkin 
55437c1209dSAneesh Kumar K.V 	chan = kmalloc(sizeof(struct virtio_chan), GFP_KERNEL);
55537c1209dSAneesh Kumar K.V 	if (!chan) {
5565d385153SJoe Perches 		pr_err("Failed to allocate virtio 9P channel\n");
557b530cc79SEric Van Hensbergen 		err = -ENOMEM;
558b530cc79SEric Van Hensbergen 		goto fail;
559b530cc79SEric Van Hensbergen 	}
560b530cc79SEric Van Hensbergen 
561e2735b77SEric Van Hensbergen 	chan->vdev = vdev;
562e2735b77SEric Van Hensbergen 
563e2735b77SEric Van Hensbergen 	/* We expect one virtqueue, for requests. */
564d2a7dddaSMichael S. Tsirkin 	chan->vq = virtio_find_single_vq(vdev, req_done, "requests");
565e2735b77SEric Van Hensbergen 	if (IS_ERR(chan->vq)) {
566e2735b77SEric Van Hensbergen 		err = PTR_ERR(chan->vq);
567e2735b77SEric Van Hensbergen 		goto out_free_vq;
568b530cc79SEric Van Hensbergen 	}
569e2735b77SEric Van Hensbergen 	chan->vq->vdev->priv = chan;
570e2735b77SEric Van Hensbergen 	spin_lock_init(&chan->lock);
571b530cc79SEric Van Hensbergen 
572e2735b77SEric Van Hensbergen 	sg_init_table(chan->sg, VIRTQUEUE_NUM);
573b530cc79SEric Van Hensbergen 
574b530cc79SEric Van Hensbergen 	chan->inuse = false;
57597ee9b02SAneesh Kumar K.V 	if (virtio_has_feature(vdev, VIRTIO_9P_MOUNT_TAG)) {
576855e0c52SRusty Russell 		virtio_cread(vdev, struct virtio_9p_config, tag_len, &tag_len);
57797ee9b02SAneesh Kumar K.V 	} else {
57897ee9b02SAneesh Kumar K.V 		err = -EINVAL;
57997ee9b02SAneesh Kumar K.V 		goto out_free_vq;
58097ee9b02SAneesh Kumar K.V 	}
58197ee9b02SAneesh Kumar K.V 	tag = kmalloc(tag_len, GFP_KERNEL);
58297ee9b02SAneesh Kumar K.V 	if (!tag) {
58397ee9b02SAneesh Kumar K.V 		err = -ENOMEM;
58497ee9b02SAneesh Kumar K.V 		goto out_free_vq;
58597ee9b02SAneesh Kumar K.V 	}
586855e0c52SRusty Russell 
587855e0c52SRusty Russell 	virtio_cread_bytes(vdev, offsetof(struct virtio_9p_config, tag),
58897ee9b02SAneesh Kumar K.V 			   tag, tag_len);
58997ee9b02SAneesh Kumar K.V 	chan->tag = tag;
59097ee9b02SAneesh Kumar K.V 	chan->tag_len = tag_len;
59186c84373SAneesh Kumar K.V 	err = sysfs_create_file(&(vdev->dev.kobj), &dev_attr_mount_tag.attr);
59286c84373SAneesh Kumar K.V 	if (err) {
59352f44e0dSVenkateswararao Jujjuri (JV) 		goto out_free_tag;
59486c84373SAneesh Kumar K.V 	}
59552f44e0dSVenkateswararao Jujjuri (JV) 	chan->vc_wq = kmalloc(sizeof(wait_queue_head_t), GFP_KERNEL);
59652f44e0dSVenkateswararao Jujjuri (JV) 	if (!chan->vc_wq) {
59752f44e0dSVenkateswararao Jujjuri (JV) 		err = -ENOMEM;
59852f44e0dSVenkateswararao Jujjuri (JV) 		goto out_free_tag;
59952f44e0dSVenkateswararao Jujjuri (JV) 	}
60052f44e0dSVenkateswararao Jujjuri (JV) 	init_waitqueue_head(chan->vc_wq);
60152f44e0dSVenkateswararao Jujjuri (JV) 	chan->ring_bufs_avail = 1;
60268da9ba4SVenkateswararao Jujjuri (JV) 	/* Ceiling limit to avoid denial of service attacks */
60368da9ba4SVenkateswararao Jujjuri (JV) 	chan->p9_max_pages = nr_free_buffer_pages()/4;
60452f44e0dSVenkateswararao Jujjuri (JV) 
60564b4cc39SMichael S. Tsirkin 	virtio_device_ready(vdev);
60664b4cc39SMichael S. Tsirkin 
60737c1209dSAneesh Kumar K.V 	mutex_lock(&virtio_9p_lock);
60837c1209dSAneesh Kumar K.V 	list_add_tail(&chan->chan_list, &virtio_chan_list);
60937c1209dSAneesh Kumar K.V 	mutex_unlock(&virtio_9p_lock);
610e0d6cb9cSMichael Marineau 
611e0d6cb9cSMichael Marineau 	/* Let udev rules use the new mount_tag attribute. */
612e0d6cb9cSMichael Marineau 	kobject_uevent(&(vdev->dev.kobj), KOBJ_CHANGE);
613e0d6cb9cSMichael Marineau 
614b530cc79SEric Van Hensbergen 	return 0;
615b530cc79SEric Van Hensbergen 
61652f44e0dSVenkateswararao Jujjuri (JV) out_free_tag:
61752f44e0dSVenkateswararao Jujjuri (JV) 	kfree(tag);
618e2735b77SEric Van Hensbergen out_free_vq:
619d2a7dddaSMichael S. Tsirkin 	vdev->config->del_vqs(vdev);
62037c1209dSAneesh Kumar K.V 	kfree(chan);
621b530cc79SEric Van Hensbergen fail:
622b530cc79SEric Van Hensbergen 	return err;
623b530cc79SEric Van Hensbergen }
624b530cc79SEric Van Hensbergen 
625ee443996SEric Van Hensbergen 
626ee443996SEric Van Hensbergen /**
627ee443996SEric Van Hensbergen  * p9_virtio_create - allocate a new virtio channel
6288b81ef58SEric Van Hensbergen  * @client: client instance invoking this transport
629ee443996SEric Van Hensbergen  * @devname: string identifying the channel to connect to (unused)
630ee443996SEric Van Hensbergen  * @args: args passed from sys_mount() for per-transport options (unused)
631ee443996SEric Van Hensbergen  *
632ee443996SEric Van Hensbergen  * This sets up a transport channel for 9p communication.  Right now
633b530cc79SEric Van Hensbergen  * we only match the first available channel, but eventually we couldlook up
634b530cc79SEric Van Hensbergen  * alternate channels by matching devname versus a virtio_config entry.
635b530cc79SEric Van Hensbergen  * We use a simple reference count mechanism to ensure that only a single
636ee443996SEric Van Hensbergen  * mount has a channel open at a time.
637ee443996SEric Van Hensbergen  *
638ee443996SEric Van Hensbergen  */
639ee443996SEric Van Hensbergen 
6408b81ef58SEric Van Hensbergen static int
6418b81ef58SEric Van Hensbergen p9_virtio_create(struct p9_client *client, const char *devname, char *args)
642b530cc79SEric Van Hensbergen {
64337c1209dSAneesh Kumar K.V 	struct virtio_chan *chan;
644c1a7c226SAneesh Kumar K.V 	int ret = -ENOENT;
64537c1209dSAneesh Kumar K.V 	int found = 0;
646b530cc79SEric Van Hensbergen 
647c1549497SJosef 'Jeff' Sipek 	mutex_lock(&virtio_9p_lock);
64837c1209dSAneesh Kumar K.V 	list_for_each_entry(chan, &virtio_chan_list, chan_list) {
6490b20406cSSven Eckelmann 		if (!strncmp(devname, chan->tag, chan->tag_len) &&
6500b20406cSSven Eckelmann 		    strlen(devname) == chan->tag_len) {
651f75580c4SAneesh Kumar K.V 			if (!chan->inuse) {
652b530cc79SEric Van Hensbergen 				chan->inuse = true;
65337c1209dSAneesh Kumar K.V 				found = 1;
654b530cc79SEric Van Hensbergen 				break;
655f75580c4SAneesh Kumar K.V 			}
656c1a7c226SAneesh Kumar K.V 			ret = -EBUSY;
657f75580c4SAneesh Kumar K.V 		}
658b530cc79SEric Van Hensbergen 	}
659c1549497SJosef 'Jeff' Sipek 	mutex_unlock(&virtio_9p_lock);
660b530cc79SEric Van Hensbergen 
66137c1209dSAneesh Kumar K.V 	if (!found) {
6625d385153SJoe Perches 		pr_err("no channels available\n");
663c1a7c226SAneesh Kumar K.V 		return ret;
664b530cc79SEric Van Hensbergen 	}
665b530cc79SEric Van Hensbergen 
6668b81ef58SEric Van Hensbergen 	client->trans = (void *)chan;
667562ada61SEric Van Hensbergen 	client->status = Connected;
668fea511a6SEric Van Hensbergen 	chan->client = client;
669b530cc79SEric Van Hensbergen 
6708b81ef58SEric Van Hensbergen 	return 0;
671b530cc79SEric Van Hensbergen }
672b530cc79SEric Van Hensbergen 
673ee443996SEric Van Hensbergen /**
674ee443996SEric Van Hensbergen  * p9_virtio_remove - clean up resources associated with a virtio device
675ee443996SEric Van Hensbergen  * @vdev: virtio device to remove
676ee443996SEric Van Hensbergen  *
677ee443996SEric Van Hensbergen  */
678ee443996SEric Van Hensbergen 
679f3933545SEric Van Hensbergen static void p9_virtio_remove(struct virtio_device *vdev)
680f3933545SEric Van Hensbergen {
681f3933545SEric Van Hensbergen 	struct virtio_chan *chan = vdev->priv;
6828051a2a5SMichael S. Tsirkin 	unsigned long warning_time;
68337c1209dSAneesh Kumar K.V 
68437c1209dSAneesh Kumar K.V 	mutex_lock(&virtio_9p_lock);
6858051a2a5SMichael S. Tsirkin 
6868051a2a5SMichael S. Tsirkin 	/* Remove self from list so we don't get new users. */
68737c1209dSAneesh Kumar K.V 	list_del(&chan->chan_list);
6888051a2a5SMichael S. Tsirkin 	warning_time = jiffies;
6898051a2a5SMichael S. Tsirkin 
6908051a2a5SMichael S. Tsirkin 	/* Wait for existing users to close. */
6918051a2a5SMichael S. Tsirkin 	while (chan->inuse) {
69237c1209dSAneesh Kumar K.V 		mutex_unlock(&virtio_9p_lock);
6938051a2a5SMichael S. Tsirkin 		msleep(250);
6948051a2a5SMichael S. Tsirkin 		if (time_after(jiffies, warning_time + 10 * HZ)) {
6958051a2a5SMichael S. Tsirkin 			dev_emerg(&vdev->dev,
6968051a2a5SMichael S. Tsirkin 				  "p9_virtio_remove: waiting for device in use.\n");
6978051a2a5SMichael S. Tsirkin 			warning_time = jiffies;
6988051a2a5SMichael S. Tsirkin 		}
6998051a2a5SMichael S. Tsirkin 		mutex_lock(&virtio_9p_lock);
7008051a2a5SMichael S. Tsirkin 	}
7018051a2a5SMichael S. Tsirkin 
7028051a2a5SMichael S. Tsirkin 	mutex_unlock(&virtio_9p_lock);
7038051a2a5SMichael S. Tsirkin 
7048051a2a5SMichael S. Tsirkin 	vdev->config->del_vqs(vdev);
7058051a2a5SMichael S. Tsirkin 
70686c84373SAneesh Kumar K.V 	sysfs_remove_file(&(vdev->dev.kobj), &dev_attr_mount_tag.attr);
707e0d6cb9cSMichael Marineau 	kobject_uevent(&(vdev->dev.kobj), KOBJ_CHANGE);
70897ee9b02SAneesh Kumar K.V 	kfree(chan->tag);
70952f44e0dSVenkateswararao Jujjuri (JV) 	kfree(chan->vc_wq);
71037c1209dSAneesh Kumar K.V 	kfree(chan);
71137c1209dSAneesh Kumar K.V 
712f3933545SEric Van Hensbergen }
713f3933545SEric Van Hensbergen 
714b530cc79SEric Van Hensbergen static struct virtio_device_id id_table[] = {
715b530cc79SEric Van Hensbergen 	{ VIRTIO_ID_9P, VIRTIO_DEV_ANY_ID },
716b530cc79SEric Van Hensbergen 	{ 0 },
717b530cc79SEric Van Hensbergen };
718b530cc79SEric Van Hensbergen 
71997ee9b02SAneesh Kumar K.V static unsigned int features[] = {
72097ee9b02SAneesh Kumar K.V 	VIRTIO_9P_MOUNT_TAG,
72197ee9b02SAneesh Kumar K.V };
72297ee9b02SAneesh Kumar K.V 
723b530cc79SEric Van Hensbergen /* The standard "struct lguest_driver": */
724b530cc79SEric Van Hensbergen static struct virtio_driver p9_virtio_drv = {
72597ee9b02SAneesh Kumar K.V 	.feature_table  = features,
72697ee9b02SAneesh Kumar K.V 	.feature_table_size = ARRAY_SIZE(features),
727b530cc79SEric Van Hensbergen 	.driver.name    = KBUILD_MODNAME,
728b530cc79SEric Van Hensbergen 	.driver.owner	= THIS_MODULE,
729b530cc79SEric Van Hensbergen 	.id_table	= id_table,
730b530cc79SEric Van Hensbergen 	.probe		= p9_virtio_probe,
731f3933545SEric Van Hensbergen 	.remove		= p9_virtio_remove,
732b530cc79SEric Van Hensbergen };
733b530cc79SEric Van Hensbergen 
734b530cc79SEric Van Hensbergen static struct p9_trans_module p9_virtio_trans = {
735b530cc79SEric Van Hensbergen 	.name = "virtio",
736b530cc79SEric Van Hensbergen 	.create = p9_virtio_create,
7378b81ef58SEric Van Hensbergen 	.close = p9_virtio_close,
73891b8534fSEric Van Hensbergen 	.request = p9_virtio_request,
739dc893e19SArnd Bergmann 	.zc_request = p9_virtio_zc_request,
74091b8534fSEric Van Hensbergen 	.cancel = p9_virtio_cancel,
741b49d8b5dSAneesh Kumar K.V 	/*
742b49d8b5dSAneesh Kumar K.V 	 * We leave one entry for input and one entry for response
743b49d8b5dSAneesh Kumar K.V 	 * headers. We also skip one more entry to accomodate, address
744b49d8b5dSAneesh Kumar K.V 	 * that are not at page boundary, that can result in an extra
745b49d8b5dSAneesh Kumar K.V 	 * page in zero copy.
746b49d8b5dSAneesh Kumar K.V 	 */
747b49d8b5dSAneesh Kumar K.V 	.maxsize = PAGE_SIZE * (VIRTQUEUE_NUM - 3),
748f94741fdSEric Van Hensbergen 	.def = 1,
74972029fe8STejun Heo 	.owner = THIS_MODULE,
750b530cc79SEric Van Hensbergen };
751b530cc79SEric Van Hensbergen 
752b530cc79SEric Van Hensbergen /* The standard init function */
753b530cc79SEric Van Hensbergen static int __init p9_virtio_init(void)
754b530cc79SEric Van Hensbergen {
75537c1209dSAneesh Kumar K.V 	INIT_LIST_HEAD(&virtio_chan_list);
756b530cc79SEric Van Hensbergen 
757b530cc79SEric Van Hensbergen 	v9fs_register_trans(&p9_virtio_trans);
758b530cc79SEric Van Hensbergen 	return register_virtio_driver(&p9_virtio_drv);
759b530cc79SEric Van Hensbergen }
760b530cc79SEric Van Hensbergen 
761f3933545SEric Van Hensbergen static void __exit p9_virtio_cleanup(void)
762f3933545SEric Van Hensbergen {
763f3933545SEric Van Hensbergen 	unregister_virtio_driver(&p9_virtio_drv);
76472029fe8STejun Heo 	v9fs_unregister_trans(&p9_virtio_trans);
765f3933545SEric Van Hensbergen }
766f3933545SEric Van Hensbergen 
767b530cc79SEric Van Hensbergen module_init(p9_virtio_init);
768f3933545SEric Van Hensbergen module_exit(p9_virtio_cleanup);
769b530cc79SEric Van Hensbergen 
770b530cc79SEric Van Hensbergen MODULE_DEVICE_TABLE(virtio, id_table);
771b530cc79SEric Van Hensbergen MODULE_AUTHOR("Eric Van Hensbergen <ericvh@gmail.com>");
772b530cc79SEric Van Hensbergen MODULE_DESCRIPTION("Virtio 9p Transport");
773b530cc79SEric Van Hensbergen MODULE_LICENSE("GPL");
774