xref: /openbmc/linux/net/9p/trans_virtio.c (revision e0d6cb9cd3a3ac8a3b8e5b22b83c4f8619786f22)
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);
167a01a9840SVenkateswararao Jujjuri (JV) 		req->status = REQ_STATUS_RCVD;
16891b8534fSEric Van Hensbergen 		p9_client_cb(chan->client, req);
169e2735b77SEric Van Hensbergen 	}
170b530cc79SEric Van Hensbergen }
171b530cc79SEric Van Hensbergen 
172ee443996SEric Van Hensbergen /**
173ee443996SEric Van Hensbergen  * pack_sg_list - pack a scatter gather list from a linear buffer
174ee443996SEric Van Hensbergen  * @sg: scatter/gather list to pack into
175ee443996SEric Van Hensbergen  * @start: which segment of the sg_list to start at
176ee443996SEric Van Hensbergen  * @limit: maximum segment to pack data to
177ee443996SEric Van Hensbergen  * @data: data to pack into scatter/gather list
178ee443996SEric Van Hensbergen  * @count: amount of data to pack into the scatter/gather list
179ee443996SEric Van Hensbergen  *
180ee443996SEric Van Hensbergen  * sg_lists have multiple segments of various sizes.  This will pack
181ee443996SEric Van Hensbergen  * arbitrary data into an existing scatter gather list, segmenting the
182ee443996SEric Van Hensbergen  * data as necessary within constraints.
183ee443996SEric Van Hensbergen  *
184ee443996SEric Van Hensbergen  */
185ee443996SEric Van Hensbergen 
186abfa034eSAneesh Kumar K.V static int pack_sg_list(struct scatterlist *sg, int start,
187abfa034eSAneesh Kumar K.V 			int limit, char *data, int count)
188e2735b77SEric Van Hensbergen {
189e2735b77SEric Van Hensbergen 	int s;
190e2735b77SEric Van Hensbergen 	int index = start;
191e2735b77SEric Van Hensbergen 
192e2735b77SEric Van Hensbergen 	while (count) {
193e2735b77SEric Van Hensbergen 		s = rest_of_page(data);
194e2735b77SEric Van Hensbergen 		if (s > count)
195e2735b77SEric Van Hensbergen 			s = count;
1965fcb08beSSasha Levin 		BUG_ON(index > limit);
1970b36f1adSRusty Russell 		/* Make sure we don't terminate early. */
1980b36f1adSRusty Russell 		sg_unmark_end(&sg[index]);
199e2735b77SEric Van Hensbergen 		sg_set_buf(&sg[index++], data, s);
200e2735b77SEric Van Hensbergen 		count -= s;
201e2735b77SEric Van Hensbergen 		data += s;
202e2735b77SEric Van Hensbergen 	}
2030b36f1adSRusty Russell 	if (index-start)
2040b36f1adSRusty Russell 		sg_mark_end(&sg[index - 1]);
205e2735b77SEric Van Hensbergen 	return index-start;
206e2735b77SEric Van Hensbergen }
207e2735b77SEric Van Hensbergen 
20891b8534fSEric Van Hensbergen /* We don't currently allow canceling of virtio requests */
20991b8534fSEric Van Hensbergen static int p9_virtio_cancel(struct p9_client *client, struct p9_req_t *req)
21091b8534fSEric Van Hensbergen {
21191b8534fSEric Van Hensbergen 	return 1;
21291b8534fSEric Van Hensbergen }
21391b8534fSEric Van Hensbergen 
214ee443996SEric Van Hensbergen /**
2154038866dSVenkateswararao Jujjuri (JV)  * pack_sg_list_p - Just like pack_sg_list. Instead of taking a buffer,
2164038866dSVenkateswararao Jujjuri (JV)  * this takes a list of pages.
2174038866dSVenkateswararao Jujjuri (JV)  * @sg: scatter/gather list to pack into
2184038866dSVenkateswararao Jujjuri (JV)  * @start: which segment of the sg_list to start at
2192c53040fSBen Hutchings  * @pdata: a list of pages to add into sg.
220abfa034eSAneesh Kumar K.V  * @nr_pages: number of pages to pack into the scatter/gather list
221abfa034eSAneesh Kumar K.V  * @data: data to pack into scatter/gather list
2224038866dSVenkateswararao Jujjuri (JV)  * @count: amount of data to pack into the scatter/gather list
2234038866dSVenkateswararao Jujjuri (JV)  */
2244038866dSVenkateswararao Jujjuri (JV) static int
225abfa034eSAneesh Kumar K.V pack_sg_list_p(struct scatterlist *sg, int start, int limit,
226abfa034eSAneesh Kumar K.V 	       struct page **pdata, int nr_pages, char *data, int count)
2274038866dSVenkateswararao Jujjuri (JV) {
228abfa034eSAneesh Kumar K.V 	int i = 0, s;
229abfa034eSAneesh Kumar K.V 	int data_off;
2304038866dSVenkateswararao Jujjuri (JV) 	int index = start;
2314038866dSVenkateswararao Jujjuri (JV) 
232abfa034eSAneesh Kumar K.V 	BUG_ON(nr_pages > (limit - start));
233abfa034eSAneesh Kumar K.V 	/*
234abfa034eSAneesh Kumar K.V 	 * if the first page doesn't start at
235abfa034eSAneesh Kumar K.V 	 * page boundary find the offset
236abfa034eSAneesh Kumar K.V 	 */
237abfa034eSAneesh Kumar K.V 	data_off = offset_in_page(data);
238abfa034eSAneesh Kumar K.V 	while (nr_pages) {
239abfa034eSAneesh Kumar K.V 		s = rest_of_page(data);
240abfa034eSAneesh Kumar K.V 		if (s > count)
241abfa034eSAneesh Kumar K.V 			s = count;
2420b36f1adSRusty Russell 		/* Make sure we don't terminate early. */
2430b36f1adSRusty Russell 		sg_unmark_end(&sg[index]);
244abfa034eSAneesh Kumar K.V 		sg_set_page(&sg[index++], pdata[i++], s, data_off);
245abfa034eSAneesh Kumar K.V 		data_off = 0;
246abfa034eSAneesh Kumar K.V 		data += s;
2474038866dSVenkateswararao Jujjuri (JV) 		count -= s;
248abfa034eSAneesh Kumar K.V 		nr_pages--;
2494038866dSVenkateswararao Jujjuri (JV) 	}
2500b36f1adSRusty Russell 
2510b36f1adSRusty Russell 	if (index-start)
2520b36f1adSRusty Russell 		sg_mark_end(&sg[index - 1]);
2534038866dSVenkateswararao Jujjuri (JV) 	return index - start;
2544038866dSVenkateswararao Jujjuri (JV) }
2554038866dSVenkateswararao Jujjuri (JV) 
2564038866dSVenkateswararao Jujjuri (JV) /**
25791b8534fSEric Van Hensbergen  * p9_virtio_request - issue a request
2580e15597eSAbhishek Kulkarni  * @client: client instance issuing the request
2590e15597eSAbhishek Kulkarni  * @req: request to be issued
260ee443996SEric Van Hensbergen  *
261ee443996SEric Van Hensbergen  */
262ee443996SEric Van Hensbergen 
263e2735b77SEric Van Hensbergen static int
26491b8534fSEric Van Hensbergen p9_virtio_request(struct p9_client *client, struct p9_req_t *req)
265e2735b77SEric Van Hensbergen {
266abfa034eSAneesh Kumar K.V 	int err;
2670b36f1adSRusty Russell 	int in, out, out_sgs, in_sgs;
268419b3956SVenkateswararao Jujjuri (JV) 	unsigned long flags;
269abfa034eSAneesh Kumar K.V 	struct virtio_chan *chan = client->trans;
2700b36f1adSRusty Russell 	struct scatterlist *sgs[2];
271e2735b77SEric Van Hensbergen 
2725d385153SJoe Perches 	p9_debug(P9_DEBUG_TRANS, "9p debug: virtio request\n");
273e2735b77SEric Van Hensbergen 
274419b3956SVenkateswararao Jujjuri (JV) 	req->status = REQ_STATUS_SENT;
275abfa034eSAneesh Kumar K.V req_retry:
276419b3956SVenkateswararao Jujjuri (JV) 	spin_lock_irqsave(&chan->lock, flags);
2774038866dSVenkateswararao Jujjuri (JV) 
2780b36f1adSRusty Russell 	out_sgs = in_sgs = 0;
2794038866dSVenkateswararao Jujjuri (JV) 	/* Handle out VirtIO ring buffers */
280abfa034eSAneesh Kumar K.V 	out = pack_sg_list(chan->sg, 0,
281abfa034eSAneesh Kumar K.V 			   VIRTQUEUE_NUM, req->tc->sdata, req->tc->size);
2820b36f1adSRusty Russell 	if (out)
2830b36f1adSRusty Russell 		sgs[out_sgs++] = chan->sg;
2844038866dSVenkateswararao Jujjuri (JV) 
285abfa034eSAneesh Kumar K.V 	in = pack_sg_list(chan->sg, out,
286abfa034eSAneesh Kumar K.V 			  VIRTQUEUE_NUM, req->rc->sdata, req->rc->capacity);
2870b36f1adSRusty Russell 	if (in)
2880b36f1adSRusty Russell 		sgs[out_sgs + in_sgs++] = chan->sg + out;
289e2735b77SEric Van Hensbergen 
2900b36f1adSRusty Russell 	err = virtqueue_add_sgs(chan->vq, sgs, out_sgs, in_sgs, req->tc,
291f96fde41SRusty Russell 				GFP_ATOMIC);
292419b3956SVenkateswararao Jujjuri (JV) 	if (err < 0) {
29352f44e0dSVenkateswararao Jujjuri (JV) 		if (err == -ENOSPC) {
29452f44e0dSVenkateswararao Jujjuri (JV) 			chan->ring_bufs_avail = 0;
29552f44e0dSVenkateswararao Jujjuri (JV) 			spin_unlock_irqrestore(&chan->lock, flags);
29652f44e0dSVenkateswararao Jujjuri (JV) 			err = wait_event_interruptible(*chan->vc_wq,
29752f44e0dSVenkateswararao Jujjuri (JV) 							chan->ring_bufs_avail);
29852f44e0dSVenkateswararao Jujjuri (JV) 			if (err  == -ERESTARTSYS)
29952f44e0dSVenkateswararao Jujjuri (JV) 				return err;
30052f44e0dSVenkateswararao Jujjuri (JV) 
3015d385153SJoe Perches 			p9_debug(P9_DEBUG_TRANS, "Retry virtio request\n");
302abfa034eSAneesh Kumar K.V 			goto req_retry;
303abfa034eSAneesh Kumar K.V 		} else {
304abfa034eSAneesh Kumar K.V 			spin_unlock_irqrestore(&chan->lock, flags);
3055d385153SJoe Perches 			p9_debug(P9_DEBUG_TRANS,
3060b36f1adSRusty Russell 				 "virtio rpc add_sgs returned failure\n");
307abfa034eSAneesh Kumar K.V 			return -EIO;
308abfa034eSAneesh Kumar K.V 		}
309abfa034eSAneesh Kumar K.V 	}
310abfa034eSAneesh Kumar K.V 	virtqueue_kick(chan->vq);
311abfa034eSAneesh Kumar K.V 	spin_unlock_irqrestore(&chan->lock, flags);
312abfa034eSAneesh Kumar K.V 
3135d385153SJoe Perches 	p9_debug(P9_DEBUG_TRANS, "virtio request kicked\n");
314abfa034eSAneesh Kumar K.V 	return 0;
315abfa034eSAneesh Kumar K.V }
316abfa034eSAneesh Kumar K.V 
317abfa034eSAneesh Kumar K.V static int p9_get_mapped_pages(struct virtio_chan *chan,
318abfa034eSAneesh Kumar K.V 			       struct page **pages, char *data,
319abfa034eSAneesh Kumar K.V 			       int nr_pages, int write, int kern_buf)
320abfa034eSAneesh Kumar K.V {
321abfa034eSAneesh Kumar K.V 	int err;
322abfa034eSAneesh Kumar K.V 	if (!kern_buf) {
323abfa034eSAneesh Kumar K.V 		/*
324abfa034eSAneesh Kumar K.V 		 * We allow only p9_max_pages pinned. We wait for the
325abfa034eSAneesh Kumar K.V 		 * Other zc request to finish here
326abfa034eSAneesh Kumar K.V 		 */
327abfa034eSAneesh Kumar K.V 		if (atomic_read(&vp_pinned) >= chan->p9_max_pages) {
328abfa034eSAneesh Kumar K.V 			err = wait_event_interruptible(vp_wq,
329abfa034eSAneesh Kumar K.V 			      (atomic_read(&vp_pinned) < chan->p9_max_pages));
330abfa034eSAneesh Kumar K.V 			if (err == -ERESTARTSYS)
331abfa034eSAneesh Kumar K.V 				return err;
332abfa034eSAneesh Kumar K.V 		}
333abfa034eSAneesh Kumar K.V 		err = p9_payload_gup(data, &nr_pages, pages, write);
334abfa034eSAneesh Kumar K.V 		if (err < 0)
335abfa034eSAneesh Kumar K.V 			return err;
336abfa034eSAneesh Kumar K.V 		atomic_add(nr_pages, &vp_pinned);
337abfa034eSAneesh Kumar K.V 	} else {
338abfa034eSAneesh Kumar K.V 		/* kernel buffer, no need to pin pages */
339abfa034eSAneesh Kumar K.V 		int s, index = 0;
340abfa034eSAneesh Kumar K.V 		int count = nr_pages;
341abfa034eSAneesh Kumar K.V 		while (nr_pages) {
342abfa034eSAneesh Kumar K.V 			s = rest_of_page(data);
343b9cdc88dSWill Deacon 			pages[index++] = kmap_to_page(data);
344abfa034eSAneesh Kumar K.V 			data += s;
345abfa034eSAneesh Kumar K.V 			nr_pages--;
346abfa034eSAneesh Kumar K.V 		}
347abfa034eSAneesh Kumar K.V 		nr_pages = count;
348abfa034eSAneesh Kumar K.V 	}
349abfa034eSAneesh Kumar K.V 	return nr_pages;
350abfa034eSAneesh Kumar K.V }
351abfa034eSAneesh Kumar K.V 
352abfa034eSAneesh Kumar K.V /**
353abfa034eSAneesh Kumar K.V  * p9_virtio_zc_request - issue a zero copy request
354abfa034eSAneesh Kumar K.V  * @client: client instance issuing the request
355abfa034eSAneesh Kumar K.V  * @req: request to be issued
356abfa034eSAneesh Kumar K.V  * @uidata: user bffer that should be ued for zero copy read
357abfa034eSAneesh Kumar K.V  * @uodata: user buffer that shoud be user for zero copy write
358abfa034eSAneesh Kumar K.V  * @inlen: read buffer size
359abfa034eSAneesh Kumar K.V  * @olen: write buffer size
360abfa034eSAneesh Kumar K.V  * @hdrlen: reader header size, This is the size of response protocol data
361abfa034eSAneesh Kumar K.V  *
362abfa034eSAneesh Kumar K.V  */
363abfa034eSAneesh Kumar K.V static int
364abfa034eSAneesh Kumar K.V p9_virtio_zc_request(struct p9_client *client, struct p9_req_t *req,
365abfa034eSAneesh Kumar K.V 		     char *uidata, char *uodata, int inlen,
366abfa034eSAneesh Kumar K.V 		     int outlen, int in_hdr_len, int kern_buf)
367abfa034eSAneesh Kumar K.V {
3680b36f1adSRusty Russell 	int in, out, err, out_sgs, in_sgs;
369abfa034eSAneesh Kumar K.V 	unsigned long flags;
370abfa034eSAneesh Kumar K.V 	int in_nr_pages = 0, out_nr_pages = 0;
371abfa034eSAneesh Kumar K.V 	struct page **in_pages = NULL, **out_pages = NULL;
372abfa034eSAneesh Kumar K.V 	struct virtio_chan *chan = client->trans;
3730b36f1adSRusty Russell 	struct scatterlist *sgs[4];
374abfa034eSAneesh Kumar K.V 
3755d385153SJoe Perches 	p9_debug(P9_DEBUG_TRANS, "virtio request\n");
376abfa034eSAneesh Kumar K.V 
377abfa034eSAneesh Kumar K.V 	if (uodata) {
378abfa034eSAneesh Kumar K.V 		out_nr_pages = p9_nr_pages(uodata, outlen);
379abfa034eSAneesh Kumar K.V 		out_pages = kmalloc(sizeof(struct page *) * out_nr_pages,
380abfa034eSAneesh Kumar K.V 				    GFP_NOFS);
381abfa034eSAneesh Kumar K.V 		if (!out_pages) {
382abfa034eSAneesh Kumar K.V 			err = -ENOMEM;
383abfa034eSAneesh Kumar K.V 			goto err_out;
384abfa034eSAneesh Kumar K.V 		}
385abfa034eSAneesh Kumar K.V 		out_nr_pages = p9_get_mapped_pages(chan, out_pages, uodata,
386abfa034eSAneesh Kumar K.V 						   out_nr_pages, 0, kern_buf);
387abfa034eSAneesh Kumar K.V 		if (out_nr_pages < 0) {
388abfa034eSAneesh Kumar K.V 			err = out_nr_pages;
389abfa034eSAneesh Kumar K.V 			kfree(out_pages);
390abfa034eSAneesh Kumar K.V 			out_pages = NULL;
391abfa034eSAneesh Kumar K.V 			goto err_out;
392abfa034eSAneesh Kumar K.V 		}
393abfa034eSAneesh Kumar K.V 	}
394abfa034eSAneesh Kumar K.V 	if (uidata) {
395abfa034eSAneesh Kumar K.V 		in_nr_pages = p9_nr_pages(uidata, inlen);
396abfa034eSAneesh Kumar K.V 		in_pages = kmalloc(sizeof(struct page *) * in_nr_pages,
397abfa034eSAneesh Kumar K.V 				   GFP_NOFS);
398abfa034eSAneesh Kumar K.V 		if (!in_pages) {
399abfa034eSAneesh Kumar K.V 			err = -ENOMEM;
400abfa034eSAneesh Kumar K.V 			goto err_out;
401abfa034eSAneesh Kumar K.V 		}
402abfa034eSAneesh Kumar K.V 		in_nr_pages = p9_get_mapped_pages(chan, in_pages, uidata,
403abfa034eSAneesh Kumar K.V 						  in_nr_pages, 1, kern_buf);
404abfa034eSAneesh Kumar K.V 		if (in_nr_pages < 0) {
405abfa034eSAneesh Kumar K.V 			err = in_nr_pages;
406abfa034eSAneesh Kumar K.V 			kfree(in_pages);
407abfa034eSAneesh Kumar K.V 			in_pages = NULL;
408abfa034eSAneesh Kumar K.V 			goto err_out;
409abfa034eSAneesh Kumar K.V 		}
410abfa034eSAneesh Kumar K.V 	}
411abfa034eSAneesh Kumar K.V 	req->status = REQ_STATUS_SENT;
412abfa034eSAneesh Kumar K.V req_retry_pinned:
413abfa034eSAneesh Kumar K.V 	spin_lock_irqsave(&chan->lock, flags);
4140b36f1adSRusty Russell 
4150b36f1adSRusty Russell 	out_sgs = in_sgs = 0;
4160b36f1adSRusty Russell 
417abfa034eSAneesh Kumar K.V 	/* out data */
418abfa034eSAneesh Kumar K.V 	out = pack_sg_list(chan->sg, 0,
419abfa034eSAneesh Kumar K.V 			   VIRTQUEUE_NUM, req->tc->sdata, req->tc->size);
420abfa034eSAneesh Kumar K.V 
4210b36f1adSRusty Russell 	if (out)
4220b36f1adSRusty Russell 		sgs[out_sgs++] = chan->sg;
4230b36f1adSRusty Russell 
4240b36f1adSRusty Russell 	if (out_pages) {
4250b36f1adSRusty Russell 		sgs[out_sgs++] = chan->sg + out;
426abfa034eSAneesh Kumar K.V 		out += pack_sg_list_p(chan->sg, out, VIRTQUEUE_NUM,
427abfa034eSAneesh Kumar K.V 				      out_pages, out_nr_pages, uodata, outlen);
4280b36f1adSRusty Russell 	}
4290b36f1adSRusty Russell 
430abfa034eSAneesh Kumar K.V 	/*
431abfa034eSAneesh Kumar K.V 	 * Take care of in data
432abfa034eSAneesh Kumar K.V 	 * For example TREAD have 11.
433abfa034eSAneesh Kumar K.V 	 * 11 is the read/write header = PDU Header(7) + IO Size (4).
434abfa034eSAneesh Kumar K.V 	 * Arrange in such a way that server places header in the
435abfa034eSAneesh Kumar K.V 	 * alloced memory and payload onto the user buffer.
436abfa034eSAneesh Kumar K.V 	 */
437abfa034eSAneesh Kumar K.V 	in = pack_sg_list(chan->sg, out,
438abfa034eSAneesh Kumar K.V 			  VIRTQUEUE_NUM, req->rc->sdata, in_hdr_len);
4390b36f1adSRusty Russell 	if (in)
4400b36f1adSRusty Russell 		sgs[out_sgs + in_sgs++] = chan->sg + out;
4410b36f1adSRusty Russell 
4420b36f1adSRusty Russell 	if (in_pages) {
4430b36f1adSRusty Russell 		sgs[out_sgs + in_sgs++] = chan->sg + out + in;
444abfa034eSAneesh Kumar K.V 		in += pack_sg_list_p(chan->sg, out + in, VIRTQUEUE_NUM,
445abfa034eSAneesh Kumar K.V 				     in_pages, in_nr_pages, uidata, inlen);
4460b36f1adSRusty Russell 	}
447abfa034eSAneesh Kumar K.V 
4480b36f1adSRusty Russell 	BUG_ON(out_sgs + in_sgs > ARRAY_SIZE(sgs));
4490b36f1adSRusty Russell 	err = virtqueue_add_sgs(chan->vq, sgs, out_sgs, in_sgs, req->tc,
450f96fde41SRusty Russell 				GFP_ATOMIC);
451abfa034eSAneesh Kumar K.V 	if (err < 0) {
452abfa034eSAneesh Kumar K.V 		if (err == -ENOSPC) {
453abfa034eSAneesh Kumar K.V 			chan->ring_bufs_avail = 0;
454abfa034eSAneesh Kumar K.V 			spin_unlock_irqrestore(&chan->lock, flags);
455abfa034eSAneesh Kumar K.V 			err = wait_event_interruptible(*chan->vc_wq,
456abfa034eSAneesh Kumar K.V 						       chan->ring_bufs_avail);
457abfa034eSAneesh Kumar K.V 			if (err  == -ERESTARTSYS)
458abfa034eSAneesh Kumar K.V 				goto err_out;
459abfa034eSAneesh Kumar K.V 
4605d385153SJoe Perches 			p9_debug(P9_DEBUG_TRANS, "Retry virtio request\n");
461316ad550SVenkateswararao Jujjuri (JV) 			goto req_retry_pinned;
46252f44e0dSVenkateswararao Jujjuri (JV) 		} else {
463419b3956SVenkateswararao Jujjuri (JV) 			spin_unlock_irqrestore(&chan->lock, flags);
4645d385153SJoe Perches 			p9_debug(P9_DEBUG_TRANS,
4650b36f1adSRusty Russell 				 "virtio rpc add_sgs returned failure\n");
466abfa034eSAneesh Kumar K.V 			err = -EIO;
467abfa034eSAneesh Kumar K.V 			goto err_out;
468e2735b77SEric Van Hensbergen 		}
46952f44e0dSVenkateswararao Jujjuri (JV) 	}
470dc3f5e68SMichael S. Tsirkin 	virtqueue_kick(chan->vq);
471419b3956SVenkateswararao Jujjuri (JV) 	spin_unlock_irqrestore(&chan->lock, flags);
4725d385153SJoe Perches 	p9_debug(P9_DEBUG_TRANS, "virtio request kicked\n");
473abfa034eSAneesh Kumar K.V 	err = wait_event_interruptible(*req->wq,
474abfa034eSAneesh Kumar K.V 				       req->status >= REQ_STATUS_RCVD);
475abfa034eSAneesh Kumar K.V 	/*
476abfa034eSAneesh Kumar K.V 	 * Non kernel buffers are pinned, unpin them
477abfa034eSAneesh Kumar K.V 	 */
478abfa034eSAneesh Kumar K.V err_out:
479abfa034eSAneesh Kumar K.V 	if (!kern_buf) {
480abfa034eSAneesh Kumar K.V 		if (in_pages) {
481abfa034eSAneesh Kumar K.V 			p9_release_pages(in_pages, in_nr_pages);
482abfa034eSAneesh Kumar K.V 			atomic_sub(in_nr_pages, &vp_pinned);
483abfa034eSAneesh Kumar K.V 		}
484abfa034eSAneesh Kumar K.V 		if (out_pages) {
485abfa034eSAneesh Kumar K.V 			p9_release_pages(out_pages, out_nr_pages);
486abfa034eSAneesh Kumar K.V 			atomic_sub(out_nr_pages, &vp_pinned);
487abfa034eSAneesh Kumar K.V 		}
488abfa034eSAneesh Kumar K.V 		/* wakeup anybody waiting for slots to pin pages */
489abfa034eSAneesh Kumar K.V 		wake_up(&vp_wq);
490abfa034eSAneesh Kumar K.V 	}
491abfa034eSAneesh Kumar K.V 	kfree(in_pages);
492abfa034eSAneesh Kumar K.V 	kfree(out_pages);
493abfa034eSAneesh Kumar K.V 	return err;
494e2735b77SEric Van Hensbergen }
495e2735b77SEric Van Hensbergen 
49686c84373SAneesh Kumar K.V static ssize_t p9_mount_tag_show(struct device *dev,
49786c84373SAneesh Kumar K.V 				struct device_attribute *attr, char *buf)
49886c84373SAneesh Kumar K.V {
49986c84373SAneesh Kumar K.V 	struct virtio_chan *chan;
50086c84373SAneesh Kumar K.V 	struct virtio_device *vdev;
50186c84373SAneesh Kumar K.V 
50286c84373SAneesh Kumar K.V 	vdev = dev_to_virtio(dev);
50386c84373SAneesh Kumar K.V 	chan = vdev->priv;
50486c84373SAneesh Kumar K.V 
50586c84373SAneesh Kumar K.V 	return snprintf(buf, chan->tag_len + 1, "%s", chan->tag);
50686c84373SAneesh Kumar K.V }
50786c84373SAneesh Kumar K.V 
50886c84373SAneesh Kumar K.V static DEVICE_ATTR(mount_tag, 0444, p9_mount_tag_show, NULL);
50986c84373SAneesh Kumar K.V 
510ee443996SEric Van Hensbergen /**
511ee443996SEric Van Hensbergen  * p9_virtio_probe - probe for existence of 9P virtio channels
512ee443996SEric Van Hensbergen  * @vdev: virtio device to probe
513ee443996SEric Van Hensbergen  *
51437c1209dSAneesh Kumar K.V  * This probes for existing virtio channels.
515ee443996SEric Van Hensbergen  *
516ee443996SEric Van Hensbergen  */
517ee443996SEric Van Hensbergen 
518e2735b77SEric Van Hensbergen static int p9_virtio_probe(struct virtio_device *vdev)
519b530cc79SEric Van Hensbergen {
52097ee9b02SAneesh Kumar K.V 	__u16 tag_len;
52197ee9b02SAneesh Kumar K.V 	char *tag;
522b530cc79SEric Van Hensbergen 	int err;
523b530cc79SEric Van Hensbergen 	struct virtio_chan *chan;
524b530cc79SEric Van Hensbergen 
52537c1209dSAneesh Kumar K.V 	chan = kmalloc(sizeof(struct virtio_chan), GFP_KERNEL);
52637c1209dSAneesh Kumar K.V 	if (!chan) {
5275d385153SJoe Perches 		pr_err("Failed to allocate virtio 9P channel\n");
528b530cc79SEric Van Hensbergen 		err = -ENOMEM;
529b530cc79SEric Van Hensbergen 		goto fail;
530b530cc79SEric Van Hensbergen 	}
531b530cc79SEric Van Hensbergen 
532e2735b77SEric Van Hensbergen 	chan->vdev = vdev;
533e2735b77SEric Van Hensbergen 
534e2735b77SEric Van Hensbergen 	/* We expect one virtqueue, for requests. */
535d2a7dddaSMichael S. Tsirkin 	chan->vq = virtio_find_single_vq(vdev, req_done, "requests");
536e2735b77SEric Van Hensbergen 	if (IS_ERR(chan->vq)) {
537e2735b77SEric Van Hensbergen 		err = PTR_ERR(chan->vq);
538e2735b77SEric Van Hensbergen 		goto out_free_vq;
539b530cc79SEric Van Hensbergen 	}
540e2735b77SEric Van Hensbergen 	chan->vq->vdev->priv = chan;
541e2735b77SEric Van Hensbergen 	spin_lock_init(&chan->lock);
542b530cc79SEric Van Hensbergen 
543e2735b77SEric Van Hensbergen 	sg_init_table(chan->sg, VIRTQUEUE_NUM);
544b530cc79SEric Van Hensbergen 
545b530cc79SEric Van Hensbergen 	chan->inuse = false;
54697ee9b02SAneesh Kumar K.V 	if (virtio_has_feature(vdev, VIRTIO_9P_MOUNT_TAG)) {
54797ee9b02SAneesh Kumar K.V 		vdev->config->get(vdev,
54897ee9b02SAneesh Kumar K.V 				offsetof(struct virtio_9p_config, tag_len),
54997ee9b02SAneesh Kumar K.V 				&tag_len, sizeof(tag_len));
55097ee9b02SAneesh Kumar K.V 	} else {
55197ee9b02SAneesh Kumar K.V 		err = -EINVAL;
55297ee9b02SAneesh Kumar K.V 		goto out_free_vq;
55397ee9b02SAneesh Kumar K.V 	}
55497ee9b02SAneesh Kumar K.V 	tag = kmalloc(tag_len, GFP_KERNEL);
55597ee9b02SAneesh Kumar K.V 	if (!tag) {
55697ee9b02SAneesh Kumar K.V 		err = -ENOMEM;
55797ee9b02SAneesh Kumar K.V 		goto out_free_vq;
55897ee9b02SAneesh Kumar K.V 	}
55997ee9b02SAneesh Kumar K.V 	vdev->config->get(vdev, offsetof(struct virtio_9p_config, tag),
56097ee9b02SAneesh Kumar K.V 			tag, tag_len);
56197ee9b02SAneesh Kumar K.V 	chan->tag = tag;
56297ee9b02SAneesh Kumar K.V 	chan->tag_len = tag_len;
56386c84373SAneesh Kumar K.V 	err = sysfs_create_file(&(vdev->dev.kobj), &dev_attr_mount_tag.attr);
56486c84373SAneesh Kumar K.V 	if (err) {
56552f44e0dSVenkateswararao Jujjuri (JV) 		goto out_free_tag;
56686c84373SAneesh Kumar K.V 	}
56752f44e0dSVenkateswararao Jujjuri (JV) 	chan->vc_wq = kmalloc(sizeof(wait_queue_head_t), GFP_KERNEL);
56852f44e0dSVenkateswararao Jujjuri (JV) 	if (!chan->vc_wq) {
56952f44e0dSVenkateswararao Jujjuri (JV) 		err = -ENOMEM;
57052f44e0dSVenkateswararao Jujjuri (JV) 		goto out_free_tag;
57152f44e0dSVenkateswararao Jujjuri (JV) 	}
57252f44e0dSVenkateswararao Jujjuri (JV) 	init_waitqueue_head(chan->vc_wq);
57352f44e0dSVenkateswararao Jujjuri (JV) 	chan->ring_bufs_avail = 1;
57468da9ba4SVenkateswararao Jujjuri (JV) 	/* Ceiling limit to avoid denial of service attacks */
57568da9ba4SVenkateswararao Jujjuri (JV) 	chan->p9_max_pages = nr_free_buffer_pages()/4;
57652f44e0dSVenkateswararao Jujjuri (JV) 
57737c1209dSAneesh Kumar K.V 	mutex_lock(&virtio_9p_lock);
57837c1209dSAneesh Kumar K.V 	list_add_tail(&chan->chan_list, &virtio_chan_list);
57937c1209dSAneesh Kumar K.V 	mutex_unlock(&virtio_9p_lock);
580*e0d6cb9cSMichael Marineau 
581*e0d6cb9cSMichael Marineau 	/* Let udev rules use the new mount_tag attribute. */
582*e0d6cb9cSMichael Marineau 	kobject_uevent(&(vdev->dev.kobj), KOBJ_CHANGE);
583*e0d6cb9cSMichael Marineau 
584b530cc79SEric Van Hensbergen 	return 0;
585b530cc79SEric Van Hensbergen 
58652f44e0dSVenkateswararao Jujjuri (JV) out_free_tag:
58752f44e0dSVenkateswararao Jujjuri (JV) 	kfree(tag);
588e2735b77SEric Van Hensbergen out_free_vq:
589d2a7dddaSMichael S. Tsirkin 	vdev->config->del_vqs(vdev);
59037c1209dSAneesh Kumar K.V 	kfree(chan);
591b530cc79SEric Van Hensbergen fail:
592b530cc79SEric Van Hensbergen 	return err;
593b530cc79SEric Van Hensbergen }
594b530cc79SEric Van Hensbergen 
595ee443996SEric Van Hensbergen 
596ee443996SEric Van Hensbergen /**
597ee443996SEric Van Hensbergen  * p9_virtio_create - allocate a new virtio channel
5988b81ef58SEric Van Hensbergen  * @client: client instance invoking this transport
599ee443996SEric Van Hensbergen  * @devname: string identifying the channel to connect to (unused)
600ee443996SEric Van Hensbergen  * @args: args passed from sys_mount() for per-transport options (unused)
601ee443996SEric Van Hensbergen  *
602ee443996SEric Van Hensbergen  * This sets up a transport channel for 9p communication.  Right now
603b530cc79SEric Van Hensbergen  * we only match the first available channel, but eventually we couldlook up
604b530cc79SEric Van Hensbergen  * alternate channels by matching devname versus a virtio_config entry.
605b530cc79SEric Van Hensbergen  * We use a simple reference count mechanism to ensure that only a single
606ee443996SEric Van Hensbergen  * mount has a channel open at a time.
607ee443996SEric Van Hensbergen  *
608ee443996SEric Van Hensbergen  */
609ee443996SEric Van Hensbergen 
6108b81ef58SEric Van Hensbergen static int
6118b81ef58SEric Van Hensbergen p9_virtio_create(struct p9_client *client, const char *devname, char *args)
612b530cc79SEric Van Hensbergen {
61337c1209dSAneesh Kumar K.V 	struct virtio_chan *chan;
614c1a7c226SAneesh Kumar K.V 	int ret = -ENOENT;
61537c1209dSAneesh Kumar K.V 	int found = 0;
616b530cc79SEric Van Hensbergen 
617c1549497SJosef 'Jeff' Sipek 	mutex_lock(&virtio_9p_lock);
61837c1209dSAneesh Kumar K.V 	list_for_each_entry(chan, &virtio_chan_list, chan_list) {
6190b20406cSSven Eckelmann 		if (!strncmp(devname, chan->tag, chan->tag_len) &&
6200b20406cSSven Eckelmann 		    strlen(devname) == chan->tag_len) {
621f75580c4SAneesh Kumar K.V 			if (!chan->inuse) {
622b530cc79SEric Van Hensbergen 				chan->inuse = true;
62337c1209dSAneesh Kumar K.V 				found = 1;
624b530cc79SEric Van Hensbergen 				break;
625f75580c4SAneesh Kumar K.V 			}
626c1a7c226SAneesh Kumar K.V 			ret = -EBUSY;
627f75580c4SAneesh Kumar K.V 		}
628b530cc79SEric Van Hensbergen 	}
629c1549497SJosef 'Jeff' Sipek 	mutex_unlock(&virtio_9p_lock);
630b530cc79SEric Van Hensbergen 
63137c1209dSAneesh Kumar K.V 	if (!found) {
6325d385153SJoe Perches 		pr_err("no channels available\n");
633c1a7c226SAneesh Kumar K.V 		return ret;
634b530cc79SEric Van Hensbergen 	}
635b530cc79SEric Van Hensbergen 
6368b81ef58SEric Van Hensbergen 	client->trans = (void *)chan;
637562ada61SEric Van Hensbergen 	client->status = Connected;
638fea511a6SEric Van Hensbergen 	chan->client = client;
639b530cc79SEric Van Hensbergen 
6408b81ef58SEric Van Hensbergen 	return 0;
641b530cc79SEric Van Hensbergen }
642b530cc79SEric Van Hensbergen 
643ee443996SEric Van Hensbergen /**
644ee443996SEric Van Hensbergen  * p9_virtio_remove - clean up resources associated with a virtio device
645ee443996SEric Van Hensbergen  * @vdev: virtio device to remove
646ee443996SEric Van Hensbergen  *
647ee443996SEric Van Hensbergen  */
648ee443996SEric Van Hensbergen 
649f3933545SEric Van Hensbergen static void p9_virtio_remove(struct virtio_device *vdev)
650f3933545SEric Van Hensbergen {
651f3933545SEric Van Hensbergen 	struct virtio_chan *chan = vdev->priv;
652f3933545SEric Van Hensbergen 
653991ad9ecSSasha Levin 	if (chan->inuse)
654991ad9ecSSasha Levin 		p9_virtio_close(chan->client);
655d2a7dddaSMichael S. Tsirkin 	vdev->config->del_vqs(vdev);
65637c1209dSAneesh Kumar K.V 
65737c1209dSAneesh Kumar K.V 	mutex_lock(&virtio_9p_lock);
65837c1209dSAneesh Kumar K.V 	list_del(&chan->chan_list);
65937c1209dSAneesh Kumar K.V 	mutex_unlock(&virtio_9p_lock);
66086c84373SAneesh Kumar K.V 	sysfs_remove_file(&(vdev->dev.kobj), &dev_attr_mount_tag.attr);
661*e0d6cb9cSMichael Marineau 	kobject_uevent(&(vdev->dev.kobj), KOBJ_CHANGE);
66297ee9b02SAneesh Kumar K.V 	kfree(chan->tag);
66352f44e0dSVenkateswararao Jujjuri (JV) 	kfree(chan->vc_wq);
66437c1209dSAneesh Kumar K.V 	kfree(chan);
66537c1209dSAneesh Kumar K.V 
666f3933545SEric Van Hensbergen }
667f3933545SEric Van Hensbergen 
668b530cc79SEric Van Hensbergen static struct virtio_device_id id_table[] = {
669b530cc79SEric Van Hensbergen 	{ VIRTIO_ID_9P, VIRTIO_DEV_ANY_ID },
670b530cc79SEric Van Hensbergen 	{ 0 },
671b530cc79SEric Van Hensbergen };
672b530cc79SEric Van Hensbergen 
67397ee9b02SAneesh Kumar K.V static unsigned int features[] = {
67497ee9b02SAneesh Kumar K.V 	VIRTIO_9P_MOUNT_TAG,
67597ee9b02SAneesh Kumar K.V };
67697ee9b02SAneesh Kumar K.V 
677b530cc79SEric Van Hensbergen /* The standard "struct lguest_driver": */
678b530cc79SEric Van Hensbergen static struct virtio_driver p9_virtio_drv = {
67997ee9b02SAneesh Kumar K.V 	.feature_table  = features,
68097ee9b02SAneesh Kumar K.V 	.feature_table_size = ARRAY_SIZE(features),
681b530cc79SEric Van Hensbergen 	.driver.name    = KBUILD_MODNAME,
682b530cc79SEric Van Hensbergen 	.driver.owner	= THIS_MODULE,
683b530cc79SEric Van Hensbergen 	.id_table	= id_table,
684b530cc79SEric Van Hensbergen 	.probe		= p9_virtio_probe,
685f3933545SEric Van Hensbergen 	.remove		= p9_virtio_remove,
686b530cc79SEric Van Hensbergen };
687b530cc79SEric Van Hensbergen 
688b530cc79SEric Van Hensbergen static struct p9_trans_module p9_virtio_trans = {
689b530cc79SEric Van Hensbergen 	.name = "virtio",
690b530cc79SEric Van Hensbergen 	.create = p9_virtio_create,
6918b81ef58SEric Van Hensbergen 	.close = p9_virtio_close,
69291b8534fSEric Van Hensbergen 	.request = p9_virtio_request,
693dc893e19SArnd Bergmann 	.zc_request = p9_virtio_zc_request,
69491b8534fSEric Van Hensbergen 	.cancel = p9_virtio_cancel,
695b49d8b5dSAneesh Kumar K.V 	/*
696b49d8b5dSAneesh Kumar K.V 	 * We leave one entry for input and one entry for response
697b49d8b5dSAneesh Kumar K.V 	 * headers. We also skip one more entry to accomodate, address
698b49d8b5dSAneesh Kumar K.V 	 * that are not at page boundary, that can result in an extra
699b49d8b5dSAneesh Kumar K.V 	 * page in zero copy.
700b49d8b5dSAneesh Kumar K.V 	 */
701b49d8b5dSAneesh Kumar K.V 	.maxsize = PAGE_SIZE * (VIRTQUEUE_NUM - 3),
702b530cc79SEric Van Hensbergen 	.def = 0,
70372029fe8STejun Heo 	.owner = THIS_MODULE,
704b530cc79SEric Van Hensbergen };
705b530cc79SEric Van Hensbergen 
706b530cc79SEric Van Hensbergen /* The standard init function */
707b530cc79SEric Van Hensbergen static int __init p9_virtio_init(void)
708b530cc79SEric Van Hensbergen {
70937c1209dSAneesh Kumar K.V 	INIT_LIST_HEAD(&virtio_chan_list);
710b530cc79SEric Van Hensbergen 
711b530cc79SEric Van Hensbergen 	v9fs_register_trans(&p9_virtio_trans);
712b530cc79SEric Van Hensbergen 	return register_virtio_driver(&p9_virtio_drv);
713b530cc79SEric Van Hensbergen }
714b530cc79SEric Van Hensbergen 
715f3933545SEric Van Hensbergen static void __exit p9_virtio_cleanup(void)
716f3933545SEric Van Hensbergen {
717f3933545SEric Van Hensbergen 	unregister_virtio_driver(&p9_virtio_drv);
71872029fe8STejun Heo 	v9fs_unregister_trans(&p9_virtio_trans);
719f3933545SEric Van Hensbergen }
720f3933545SEric Van Hensbergen 
721b530cc79SEric Van Hensbergen module_init(p9_virtio_init);
722f3933545SEric Van Hensbergen module_exit(p9_virtio_cleanup);
723b530cc79SEric Van Hensbergen 
724b530cc79SEric Van Hensbergen MODULE_DEVICE_TABLE(virtio, id_table);
725b530cc79SEric Van Hensbergen MODULE_AUTHOR("Eric Van Hensbergen <ericvh@gmail.com>");
726b530cc79SEric Van Hensbergen MODULE_DESCRIPTION("Virtio 9p Transport");
727b530cc79SEric Van Hensbergen MODULE_LICENSE("GPL");
728