xref: /openbmc/linux/net/9p/trans_virtio.c (revision b9cdc88df8e63e81c723b82c286fc97f5d0dc325)
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>
42*b9cdc88dSWill 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) 	 */
9068da9ba4SVenkateswararao Jujjuri (JV) 	int 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);
197e2735b77SEric Van Hensbergen 		sg_set_buf(&sg[index++], data, s);
198e2735b77SEric Van Hensbergen 		count -= s;
199e2735b77SEric Van Hensbergen 		data += s;
200e2735b77SEric Van Hensbergen 	}
201e2735b77SEric Van Hensbergen 
202e2735b77SEric Van Hensbergen 	return index-start;
203e2735b77SEric Van Hensbergen }
204e2735b77SEric Van Hensbergen 
20591b8534fSEric Van Hensbergen /* We don't currently allow canceling of virtio requests */
20691b8534fSEric Van Hensbergen static int p9_virtio_cancel(struct p9_client *client, struct p9_req_t *req)
20791b8534fSEric Van Hensbergen {
20891b8534fSEric Van Hensbergen 	return 1;
20991b8534fSEric Van Hensbergen }
21091b8534fSEric Van Hensbergen 
211ee443996SEric Van Hensbergen /**
2124038866dSVenkateswararao Jujjuri (JV)  * pack_sg_list_p - Just like pack_sg_list. Instead of taking a buffer,
2134038866dSVenkateswararao Jujjuri (JV)  * this takes a list of pages.
2144038866dSVenkateswararao Jujjuri (JV)  * @sg: scatter/gather list to pack into
2154038866dSVenkateswararao Jujjuri (JV)  * @start: which segment of the sg_list to start at
2162c53040fSBen Hutchings  * @pdata: a list of pages to add into sg.
217abfa034eSAneesh Kumar K.V  * @nr_pages: number of pages to pack into the scatter/gather list
218abfa034eSAneesh Kumar K.V  * @data: data to pack into scatter/gather list
2194038866dSVenkateswararao Jujjuri (JV)  * @count: amount of data to pack into the scatter/gather list
2204038866dSVenkateswararao Jujjuri (JV)  */
2214038866dSVenkateswararao Jujjuri (JV) static int
222abfa034eSAneesh Kumar K.V pack_sg_list_p(struct scatterlist *sg, int start, int limit,
223abfa034eSAneesh Kumar K.V 	       struct page **pdata, int nr_pages, char *data, int count)
2244038866dSVenkateswararao Jujjuri (JV) {
225abfa034eSAneesh Kumar K.V 	int i = 0, s;
226abfa034eSAneesh Kumar K.V 	int data_off;
2274038866dSVenkateswararao Jujjuri (JV) 	int index = start;
2284038866dSVenkateswararao Jujjuri (JV) 
229abfa034eSAneesh Kumar K.V 	BUG_ON(nr_pages > (limit - start));
230abfa034eSAneesh Kumar K.V 	/*
231abfa034eSAneesh Kumar K.V 	 * if the first page doesn't start at
232abfa034eSAneesh Kumar K.V 	 * page boundary find the offset
233abfa034eSAneesh Kumar K.V 	 */
234abfa034eSAneesh Kumar K.V 	data_off = offset_in_page(data);
235abfa034eSAneesh Kumar K.V 	while (nr_pages) {
236abfa034eSAneesh Kumar K.V 		s = rest_of_page(data);
237abfa034eSAneesh Kumar K.V 		if (s > count)
238abfa034eSAneesh Kumar K.V 			s = count;
239abfa034eSAneesh Kumar K.V 		sg_set_page(&sg[index++], pdata[i++], s, data_off);
240abfa034eSAneesh Kumar K.V 		data_off = 0;
241abfa034eSAneesh Kumar K.V 		data += s;
2424038866dSVenkateswararao Jujjuri (JV) 		count -= s;
243abfa034eSAneesh Kumar K.V 		nr_pages--;
2444038866dSVenkateswararao Jujjuri (JV) 	}
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;
259abfa034eSAneesh Kumar K.V 	int in, out;
260419b3956SVenkateswararao Jujjuri (JV) 	unsigned long flags;
261abfa034eSAneesh Kumar K.V 	struct virtio_chan *chan = client->trans;
262e2735b77SEric Van Hensbergen 
2635d385153SJoe Perches 	p9_debug(P9_DEBUG_TRANS, "9p debug: virtio request\n");
264e2735b77SEric Van Hensbergen 
265419b3956SVenkateswararao Jujjuri (JV) 	req->status = REQ_STATUS_SENT;
266abfa034eSAneesh Kumar K.V req_retry:
267419b3956SVenkateswararao Jujjuri (JV) 	spin_lock_irqsave(&chan->lock, flags);
2684038866dSVenkateswararao Jujjuri (JV) 
2694038866dSVenkateswararao Jujjuri (JV) 	/* Handle out VirtIO ring buffers */
270abfa034eSAneesh Kumar K.V 	out = pack_sg_list(chan->sg, 0,
271abfa034eSAneesh Kumar K.V 			   VIRTQUEUE_NUM, req->tc->sdata, req->tc->size);
2724038866dSVenkateswararao Jujjuri (JV) 
273abfa034eSAneesh Kumar K.V 	in = pack_sg_list(chan->sg, out,
274abfa034eSAneesh Kumar K.V 			  VIRTQUEUE_NUM, req->rc->sdata, req->rc->capacity);
275e2735b77SEric Van Hensbergen 
276f96fde41SRusty Russell 	err = virtqueue_add_buf(chan->vq, chan->sg, out, in, req->tc,
277f96fde41SRusty Russell 				GFP_ATOMIC);
278419b3956SVenkateswararao Jujjuri (JV) 	if (err < 0) {
27952f44e0dSVenkateswararao Jujjuri (JV) 		if (err == -ENOSPC) {
28052f44e0dSVenkateswararao Jujjuri (JV) 			chan->ring_bufs_avail = 0;
28152f44e0dSVenkateswararao Jujjuri (JV) 			spin_unlock_irqrestore(&chan->lock, flags);
28252f44e0dSVenkateswararao Jujjuri (JV) 			err = wait_event_interruptible(*chan->vc_wq,
28352f44e0dSVenkateswararao Jujjuri (JV) 							chan->ring_bufs_avail);
28452f44e0dSVenkateswararao Jujjuri (JV) 			if (err  == -ERESTARTSYS)
28552f44e0dSVenkateswararao Jujjuri (JV) 				return err;
28652f44e0dSVenkateswararao Jujjuri (JV) 
2875d385153SJoe Perches 			p9_debug(P9_DEBUG_TRANS, "Retry virtio request\n");
288abfa034eSAneesh Kumar K.V 			goto req_retry;
289abfa034eSAneesh Kumar K.V 		} else {
290abfa034eSAneesh Kumar K.V 			spin_unlock_irqrestore(&chan->lock, flags);
2915d385153SJoe Perches 			p9_debug(P9_DEBUG_TRANS,
2925d385153SJoe Perches 				 "virtio rpc add_buf returned failure\n");
293abfa034eSAneesh Kumar K.V 			return -EIO;
294abfa034eSAneesh Kumar K.V 		}
295abfa034eSAneesh Kumar K.V 	}
296abfa034eSAneesh Kumar K.V 	virtqueue_kick(chan->vq);
297abfa034eSAneesh Kumar K.V 	spin_unlock_irqrestore(&chan->lock, flags);
298abfa034eSAneesh Kumar K.V 
2995d385153SJoe Perches 	p9_debug(P9_DEBUG_TRANS, "virtio request kicked\n");
300abfa034eSAneesh Kumar K.V 	return 0;
301abfa034eSAneesh Kumar K.V }
302abfa034eSAneesh Kumar K.V 
303abfa034eSAneesh Kumar K.V static int p9_get_mapped_pages(struct virtio_chan *chan,
304abfa034eSAneesh Kumar K.V 			       struct page **pages, char *data,
305abfa034eSAneesh Kumar K.V 			       int nr_pages, int write, int kern_buf)
306abfa034eSAneesh Kumar K.V {
307abfa034eSAneesh Kumar K.V 	int err;
308abfa034eSAneesh Kumar K.V 	if (!kern_buf) {
309abfa034eSAneesh Kumar K.V 		/*
310abfa034eSAneesh Kumar K.V 		 * We allow only p9_max_pages pinned. We wait for the
311abfa034eSAneesh Kumar K.V 		 * Other zc request to finish here
312abfa034eSAneesh Kumar K.V 		 */
313abfa034eSAneesh Kumar K.V 		if (atomic_read(&vp_pinned) >= chan->p9_max_pages) {
314abfa034eSAneesh Kumar K.V 			err = wait_event_interruptible(vp_wq,
315abfa034eSAneesh Kumar K.V 			      (atomic_read(&vp_pinned) < chan->p9_max_pages));
316abfa034eSAneesh Kumar K.V 			if (err == -ERESTARTSYS)
317abfa034eSAneesh Kumar K.V 				return err;
318abfa034eSAneesh Kumar K.V 		}
319abfa034eSAneesh Kumar K.V 		err = p9_payload_gup(data, &nr_pages, pages, write);
320abfa034eSAneesh Kumar K.V 		if (err < 0)
321abfa034eSAneesh Kumar K.V 			return err;
322abfa034eSAneesh Kumar K.V 		atomic_add(nr_pages, &vp_pinned);
323abfa034eSAneesh Kumar K.V 	} else {
324abfa034eSAneesh Kumar K.V 		/* kernel buffer, no need to pin pages */
325abfa034eSAneesh Kumar K.V 		int s, index = 0;
326abfa034eSAneesh Kumar K.V 		int count = nr_pages;
327abfa034eSAneesh Kumar K.V 		while (nr_pages) {
328abfa034eSAneesh Kumar K.V 			s = rest_of_page(data);
329*b9cdc88dSWill Deacon 			pages[index++] = kmap_to_page(data);
330abfa034eSAneesh Kumar K.V 			data += s;
331abfa034eSAneesh Kumar K.V 			nr_pages--;
332abfa034eSAneesh Kumar K.V 		}
333abfa034eSAneesh Kumar K.V 		nr_pages = count;
334abfa034eSAneesh Kumar K.V 	}
335abfa034eSAneesh Kumar K.V 	return nr_pages;
336abfa034eSAneesh Kumar K.V }
337abfa034eSAneesh Kumar K.V 
338abfa034eSAneesh Kumar K.V /**
339abfa034eSAneesh Kumar K.V  * p9_virtio_zc_request - issue a zero copy request
340abfa034eSAneesh Kumar K.V  * @client: client instance issuing the request
341abfa034eSAneesh Kumar K.V  * @req: request to be issued
342abfa034eSAneesh Kumar K.V  * @uidata: user bffer that should be ued for zero copy read
343abfa034eSAneesh Kumar K.V  * @uodata: user buffer that shoud be user for zero copy write
344abfa034eSAneesh Kumar K.V  * @inlen: read buffer size
345abfa034eSAneesh Kumar K.V  * @olen: write buffer size
346abfa034eSAneesh Kumar K.V  * @hdrlen: reader header size, This is the size of response protocol data
347abfa034eSAneesh Kumar K.V  *
348abfa034eSAneesh Kumar K.V  */
349abfa034eSAneesh Kumar K.V static int
350abfa034eSAneesh Kumar K.V p9_virtio_zc_request(struct p9_client *client, struct p9_req_t *req,
351abfa034eSAneesh Kumar K.V 		     char *uidata, char *uodata, int inlen,
352abfa034eSAneesh Kumar K.V 		     int outlen, int in_hdr_len, int kern_buf)
353abfa034eSAneesh Kumar K.V {
354abfa034eSAneesh Kumar K.V 	int in, out, err;
355abfa034eSAneesh Kumar K.V 	unsigned long flags;
356abfa034eSAneesh Kumar K.V 	int in_nr_pages = 0, out_nr_pages = 0;
357abfa034eSAneesh Kumar K.V 	struct page **in_pages = NULL, **out_pages = NULL;
358abfa034eSAneesh Kumar K.V 	struct virtio_chan *chan = client->trans;
359abfa034eSAneesh Kumar K.V 
3605d385153SJoe Perches 	p9_debug(P9_DEBUG_TRANS, "virtio request\n");
361abfa034eSAneesh Kumar K.V 
362abfa034eSAneesh Kumar K.V 	if (uodata) {
363abfa034eSAneesh Kumar K.V 		out_nr_pages = p9_nr_pages(uodata, outlen);
364abfa034eSAneesh Kumar K.V 		out_pages = kmalloc(sizeof(struct page *) * out_nr_pages,
365abfa034eSAneesh Kumar K.V 				    GFP_NOFS);
366abfa034eSAneesh Kumar K.V 		if (!out_pages) {
367abfa034eSAneesh Kumar K.V 			err = -ENOMEM;
368abfa034eSAneesh Kumar K.V 			goto err_out;
369abfa034eSAneesh Kumar K.V 		}
370abfa034eSAneesh Kumar K.V 		out_nr_pages = p9_get_mapped_pages(chan, out_pages, uodata,
371abfa034eSAneesh Kumar K.V 						   out_nr_pages, 0, kern_buf);
372abfa034eSAneesh Kumar K.V 		if (out_nr_pages < 0) {
373abfa034eSAneesh Kumar K.V 			err = out_nr_pages;
374abfa034eSAneesh Kumar K.V 			kfree(out_pages);
375abfa034eSAneesh Kumar K.V 			out_pages = NULL;
376abfa034eSAneesh Kumar K.V 			goto err_out;
377abfa034eSAneesh Kumar K.V 		}
378abfa034eSAneesh Kumar K.V 	}
379abfa034eSAneesh Kumar K.V 	if (uidata) {
380abfa034eSAneesh Kumar K.V 		in_nr_pages = p9_nr_pages(uidata, inlen);
381abfa034eSAneesh Kumar K.V 		in_pages = kmalloc(sizeof(struct page *) * in_nr_pages,
382abfa034eSAneesh Kumar K.V 				   GFP_NOFS);
383abfa034eSAneesh Kumar K.V 		if (!in_pages) {
384abfa034eSAneesh Kumar K.V 			err = -ENOMEM;
385abfa034eSAneesh Kumar K.V 			goto err_out;
386abfa034eSAneesh Kumar K.V 		}
387abfa034eSAneesh Kumar K.V 		in_nr_pages = p9_get_mapped_pages(chan, in_pages, uidata,
388abfa034eSAneesh Kumar K.V 						  in_nr_pages, 1, kern_buf);
389abfa034eSAneesh Kumar K.V 		if (in_nr_pages < 0) {
390abfa034eSAneesh Kumar K.V 			err = in_nr_pages;
391abfa034eSAneesh Kumar K.V 			kfree(in_pages);
392abfa034eSAneesh Kumar K.V 			in_pages = NULL;
393abfa034eSAneesh Kumar K.V 			goto err_out;
394abfa034eSAneesh Kumar K.V 		}
395abfa034eSAneesh Kumar K.V 	}
396abfa034eSAneesh Kumar K.V 	req->status = REQ_STATUS_SENT;
397abfa034eSAneesh Kumar K.V req_retry_pinned:
398abfa034eSAneesh Kumar K.V 	spin_lock_irqsave(&chan->lock, flags);
399abfa034eSAneesh Kumar K.V 	/* out data */
400abfa034eSAneesh Kumar K.V 	out = pack_sg_list(chan->sg, 0,
401abfa034eSAneesh Kumar K.V 			   VIRTQUEUE_NUM, req->tc->sdata, req->tc->size);
402abfa034eSAneesh Kumar K.V 
403abfa034eSAneesh Kumar K.V 	if (out_pages)
404abfa034eSAneesh Kumar K.V 		out += pack_sg_list_p(chan->sg, out, VIRTQUEUE_NUM,
405abfa034eSAneesh Kumar K.V 				      out_pages, out_nr_pages, uodata, outlen);
406abfa034eSAneesh Kumar K.V 	/*
407abfa034eSAneesh Kumar K.V 	 * Take care of in data
408abfa034eSAneesh Kumar K.V 	 * For example TREAD have 11.
409abfa034eSAneesh Kumar K.V 	 * 11 is the read/write header = PDU Header(7) + IO Size (4).
410abfa034eSAneesh Kumar K.V 	 * Arrange in such a way that server places header in the
411abfa034eSAneesh Kumar K.V 	 * alloced memory and payload onto the user buffer.
412abfa034eSAneesh Kumar K.V 	 */
413abfa034eSAneesh Kumar K.V 	in = pack_sg_list(chan->sg, out,
414abfa034eSAneesh Kumar K.V 			  VIRTQUEUE_NUM, req->rc->sdata, in_hdr_len);
415abfa034eSAneesh Kumar K.V 	if (in_pages)
416abfa034eSAneesh Kumar K.V 		in += pack_sg_list_p(chan->sg, out + in, VIRTQUEUE_NUM,
417abfa034eSAneesh Kumar K.V 				     in_pages, in_nr_pages, uidata, inlen);
418abfa034eSAneesh Kumar K.V 
419f96fde41SRusty Russell 	err = virtqueue_add_buf(chan->vq, chan->sg, out, in, req->tc,
420f96fde41SRusty Russell 				GFP_ATOMIC);
421abfa034eSAneesh Kumar K.V 	if (err < 0) {
422abfa034eSAneesh Kumar K.V 		if (err == -ENOSPC) {
423abfa034eSAneesh Kumar K.V 			chan->ring_bufs_avail = 0;
424abfa034eSAneesh Kumar K.V 			spin_unlock_irqrestore(&chan->lock, flags);
425abfa034eSAneesh Kumar K.V 			err = wait_event_interruptible(*chan->vc_wq,
426abfa034eSAneesh Kumar K.V 						       chan->ring_bufs_avail);
427abfa034eSAneesh Kumar K.V 			if (err  == -ERESTARTSYS)
428abfa034eSAneesh Kumar K.V 				goto err_out;
429abfa034eSAneesh Kumar K.V 
4305d385153SJoe Perches 			p9_debug(P9_DEBUG_TRANS, "Retry virtio request\n");
431316ad550SVenkateswararao Jujjuri (JV) 			goto req_retry_pinned;
43252f44e0dSVenkateswararao Jujjuri (JV) 		} else {
433419b3956SVenkateswararao Jujjuri (JV) 			spin_unlock_irqrestore(&chan->lock, flags);
4345d385153SJoe Perches 			p9_debug(P9_DEBUG_TRANS,
4355d385153SJoe Perches 				 "virtio rpc add_buf returned failure\n");
436abfa034eSAneesh Kumar K.V 			err = -EIO;
437abfa034eSAneesh Kumar K.V 			goto err_out;
438e2735b77SEric Van Hensbergen 		}
43952f44e0dSVenkateswararao Jujjuri (JV) 	}
440dc3f5e68SMichael S. Tsirkin 	virtqueue_kick(chan->vq);
441419b3956SVenkateswararao Jujjuri (JV) 	spin_unlock_irqrestore(&chan->lock, flags);
4425d385153SJoe Perches 	p9_debug(P9_DEBUG_TRANS, "virtio request kicked\n");
443abfa034eSAneesh Kumar K.V 	err = wait_event_interruptible(*req->wq,
444abfa034eSAneesh Kumar K.V 				       req->status >= REQ_STATUS_RCVD);
445abfa034eSAneesh Kumar K.V 	/*
446abfa034eSAneesh Kumar K.V 	 * Non kernel buffers are pinned, unpin them
447abfa034eSAneesh Kumar K.V 	 */
448abfa034eSAneesh Kumar K.V err_out:
449abfa034eSAneesh Kumar K.V 	if (!kern_buf) {
450abfa034eSAneesh Kumar K.V 		if (in_pages) {
451abfa034eSAneesh Kumar K.V 			p9_release_pages(in_pages, in_nr_pages);
452abfa034eSAneesh Kumar K.V 			atomic_sub(in_nr_pages, &vp_pinned);
453abfa034eSAneesh Kumar K.V 		}
454abfa034eSAneesh Kumar K.V 		if (out_pages) {
455abfa034eSAneesh Kumar K.V 			p9_release_pages(out_pages, out_nr_pages);
456abfa034eSAneesh Kumar K.V 			atomic_sub(out_nr_pages, &vp_pinned);
457abfa034eSAneesh Kumar K.V 		}
458abfa034eSAneesh Kumar K.V 		/* wakeup anybody waiting for slots to pin pages */
459abfa034eSAneesh Kumar K.V 		wake_up(&vp_wq);
460abfa034eSAneesh Kumar K.V 	}
461abfa034eSAneesh Kumar K.V 	kfree(in_pages);
462abfa034eSAneesh Kumar K.V 	kfree(out_pages);
463abfa034eSAneesh Kumar K.V 	return err;
464e2735b77SEric Van Hensbergen }
465e2735b77SEric Van Hensbergen 
46686c84373SAneesh Kumar K.V static ssize_t p9_mount_tag_show(struct device *dev,
46786c84373SAneesh Kumar K.V 				struct device_attribute *attr, char *buf)
46886c84373SAneesh Kumar K.V {
46986c84373SAneesh Kumar K.V 	struct virtio_chan *chan;
47086c84373SAneesh Kumar K.V 	struct virtio_device *vdev;
47186c84373SAneesh Kumar K.V 
47286c84373SAneesh Kumar K.V 	vdev = dev_to_virtio(dev);
47386c84373SAneesh Kumar K.V 	chan = vdev->priv;
47486c84373SAneesh Kumar K.V 
47586c84373SAneesh Kumar K.V 	return snprintf(buf, chan->tag_len + 1, "%s", chan->tag);
47686c84373SAneesh Kumar K.V }
47786c84373SAneesh Kumar K.V 
47886c84373SAneesh Kumar K.V static DEVICE_ATTR(mount_tag, 0444, p9_mount_tag_show, NULL);
47986c84373SAneesh Kumar K.V 
480ee443996SEric Van Hensbergen /**
481ee443996SEric Van Hensbergen  * p9_virtio_probe - probe for existence of 9P virtio channels
482ee443996SEric Van Hensbergen  * @vdev: virtio device to probe
483ee443996SEric Van Hensbergen  *
48437c1209dSAneesh Kumar K.V  * This probes for existing virtio channels.
485ee443996SEric Van Hensbergen  *
486ee443996SEric Van Hensbergen  */
487ee443996SEric Van Hensbergen 
488e2735b77SEric Van Hensbergen static int p9_virtio_probe(struct virtio_device *vdev)
489b530cc79SEric Van Hensbergen {
49097ee9b02SAneesh Kumar K.V 	__u16 tag_len;
49197ee9b02SAneesh Kumar K.V 	char *tag;
492b530cc79SEric Van Hensbergen 	int err;
493b530cc79SEric Van Hensbergen 	struct virtio_chan *chan;
494b530cc79SEric Van Hensbergen 
49537c1209dSAneesh Kumar K.V 	chan = kmalloc(sizeof(struct virtio_chan), GFP_KERNEL);
49637c1209dSAneesh Kumar K.V 	if (!chan) {
4975d385153SJoe Perches 		pr_err("Failed to allocate virtio 9P channel\n");
498b530cc79SEric Van Hensbergen 		err = -ENOMEM;
499b530cc79SEric Van Hensbergen 		goto fail;
500b530cc79SEric Van Hensbergen 	}
501b530cc79SEric Van Hensbergen 
502e2735b77SEric Van Hensbergen 	chan->vdev = vdev;
503e2735b77SEric Van Hensbergen 
504e2735b77SEric Van Hensbergen 	/* We expect one virtqueue, for requests. */
505d2a7dddaSMichael S. Tsirkin 	chan->vq = virtio_find_single_vq(vdev, req_done, "requests");
506e2735b77SEric Van Hensbergen 	if (IS_ERR(chan->vq)) {
507e2735b77SEric Van Hensbergen 		err = PTR_ERR(chan->vq);
508e2735b77SEric Van Hensbergen 		goto out_free_vq;
509b530cc79SEric Van Hensbergen 	}
510e2735b77SEric Van Hensbergen 	chan->vq->vdev->priv = chan;
511e2735b77SEric Van Hensbergen 	spin_lock_init(&chan->lock);
512b530cc79SEric Van Hensbergen 
513e2735b77SEric Van Hensbergen 	sg_init_table(chan->sg, VIRTQUEUE_NUM);
514b530cc79SEric Van Hensbergen 
515b530cc79SEric Van Hensbergen 	chan->inuse = false;
51697ee9b02SAneesh Kumar K.V 	if (virtio_has_feature(vdev, VIRTIO_9P_MOUNT_TAG)) {
51797ee9b02SAneesh Kumar K.V 		vdev->config->get(vdev,
51897ee9b02SAneesh Kumar K.V 				offsetof(struct virtio_9p_config, tag_len),
51997ee9b02SAneesh Kumar K.V 				&tag_len, sizeof(tag_len));
52097ee9b02SAneesh Kumar K.V 	} else {
52197ee9b02SAneesh Kumar K.V 		err = -EINVAL;
52297ee9b02SAneesh Kumar K.V 		goto out_free_vq;
52397ee9b02SAneesh Kumar K.V 	}
52497ee9b02SAneesh Kumar K.V 	tag = kmalloc(tag_len, GFP_KERNEL);
52597ee9b02SAneesh Kumar K.V 	if (!tag) {
52697ee9b02SAneesh Kumar K.V 		err = -ENOMEM;
52797ee9b02SAneesh Kumar K.V 		goto out_free_vq;
52897ee9b02SAneesh Kumar K.V 	}
52997ee9b02SAneesh Kumar K.V 	vdev->config->get(vdev, offsetof(struct virtio_9p_config, tag),
53097ee9b02SAneesh Kumar K.V 			tag, tag_len);
53197ee9b02SAneesh Kumar K.V 	chan->tag = tag;
53297ee9b02SAneesh Kumar K.V 	chan->tag_len = tag_len;
53386c84373SAneesh Kumar K.V 	err = sysfs_create_file(&(vdev->dev.kobj), &dev_attr_mount_tag.attr);
53486c84373SAneesh Kumar K.V 	if (err) {
53552f44e0dSVenkateswararao Jujjuri (JV) 		goto out_free_tag;
53686c84373SAneesh Kumar K.V 	}
53752f44e0dSVenkateswararao Jujjuri (JV) 	chan->vc_wq = kmalloc(sizeof(wait_queue_head_t), GFP_KERNEL);
53852f44e0dSVenkateswararao Jujjuri (JV) 	if (!chan->vc_wq) {
53952f44e0dSVenkateswararao Jujjuri (JV) 		err = -ENOMEM;
54052f44e0dSVenkateswararao Jujjuri (JV) 		goto out_free_tag;
54152f44e0dSVenkateswararao Jujjuri (JV) 	}
54252f44e0dSVenkateswararao Jujjuri (JV) 	init_waitqueue_head(chan->vc_wq);
54352f44e0dSVenkateswararao Jujjuri (JV) 	chan->ring_bufs_avail = 1;
54468da9ba4SVenkateswararao Jujjuri (JV) 	/* Ceiling limit to avoid denial of service attacks */
54568da9ba4SVenkateswararao Jujjuri (JV) 	chan->p9_max_pages = nr_free_buffer_pages()/4;
54652f44e0dSVenkateswararao Jujjuri (JV) 
54737c1209dSAneesh Kumar K.V 	mutex_lock(&virtio_9p_lock);
54837c1209dSAneesh Kumar K.V 	list_add_tail(&chan->chan_list, &virtio_chan_list);
54937c1209dSAneesh Kumar K.V 	mutex_unlock(&virtio_9p_lock);
550b530cc79SEric Van Hensbergen 	return 0;
551b530cc79SEric Van Hensbergen 
55252f44e0dSVenkateswararao Jujjuri (JV) out_free_tag:
55352f44e0dSVenkateswararao Jujjuri (JV) 	kfree(tag);
554e2735b77SEric Van Hensbergen out_free_vq:
555d2a7dddaSMichael S. Tsirkin 	vdev->config->del_vqs(vdev);
55637c1209dSAneesh Kumar K.V 	kfree(chan);
557b530cc79SEric Van Hensbergen fail:
558b530cc79SEric Van Hensbergen 	return err;
559b530cc79SEric Van Hensbergen }
560b530cc79SEric Van Hensbergen 
561ee443996SEric Van Hensbergen 
562ee443996SEric Van Hensbergen /**
563ee443996SEric Van Hensbergen  * p9_virtio_create - allocate a new virtio channel
5648b81ef58SEric Van Hensbergen  * @client: client instance invoking this transport
565ee443996SEric Van Hensbergen  * @devname: string identifying the channel to connect to (unused)
566ee443996SEric Van Hensbergen  * @args: args passed from sys_mount() for per-transport options (unused)
567ee443996SEric Van Hensbergen  *
568ee443996SEric Van Hensbergen  * This sets up a transport channel for 9p communication.  Right now
569b530cc79SEric Van Hensbergen  * we only match the first available channel, but eventually we couldlook up
570b530cc79SEric Van Hensbergen  * alternate channels by matching devname versus a virtio_config entry.
571b530cc79SEric Van Hensbergen  * We use a simple reference count mechanism to ensure that only a single
572ee443996SEric Van Hensbergen  * mount has a channel open at a time.
573ee443996SEric Van Hensbergen  *
574ee443996SEric Van Hensbergen  */
575ee443996SEric Van Hensbergen 
5768b81ef58SEric Van Hensbergen static int
5778b81ef58SEric Van Hensbergen p9_virtio_create(struct p9_client *client, const char *devname, char *args)
578b530cc79SEric Van Hensbergen {
57937c1209dSAneesh Kumar K.V 	struct virtio_chan *chan;
580c1a7c226SAneesh Kumar K.V 	int ret = -ENOENT;
58137c1209dSAneesh Kumar K.V 	int found = 0;
582b530cc79SEric Van Hensbergen 
583c1549497SJosef 'Jeff' Sipek 	mutex_lock(&virtio_9p_lock);
58437c1209dSAneesh Kumar K.V 	list_for_each_entry(chan, &virtio_chan_list, chan_list) {
5850b20406cSSven Eckelmann 		if (!strncmp(devname, chan->tag, chan->tag_len) &&
5860b20406cSSven Eckelmann 		    strlen(devname) == chan->tag_len) {
587f75580c4SAneesh Kumar K.V 			if (!chan->inuse) {
588b530cc79SEric Van Hensbergen 				chan->inuse = true;
58937c1209dSAneesh Kumar K.V 				found = 1;
590b530cc79SEric Van Hensbergen 				break;
591f75580c4SAneesh Kumar K.V 			}
592c1a7c226SAneesh Kumar K.V 			ret = -EBUSY;
593f75580c4SAneesh Kumar K.V 		}
594b530cc79SEric Van Hensbergen 	}
595c1549497SJosef 'Jeff' Sipek 	mutex_unlock(&virtio_9p_lock);
596b530cc79SEric Van Hensbergen 
59737c1209dSAneesh Kumar K.V 	if (!found) {
5985d385153SJoe Perches 		pr_err("no channels available\n");
599c1a7c226SAneesh Kumar K.V 		return ret;
600b530cc79SEric Van Hensbergen 	}
601b530cc79SEric Van Hensbergen 
6028b81ef58SEric Van Hensbergen 	client->trans = (void *)chan;
603562ada61SEric Van Hensbergen 	client->status = Connected;
604fea511a6SEric Van Hensbergen 	chan->client = client;
605b530cc79SEric Van Hensbergen 
6068b81ef58SEric Van Hensbergen 	return 0;
607b530cc79SEric Van Hensbergen }
608b530cc79SEric Van Hensbergen 
609ee443996SEric Van Hensbergen /**
610ee443996SEric Van Hensbergen  * p9_virtio_remove - clean up resources associated with a virtio device
611ee443996SEric Van Hensbergen  * @vdev: virtio device to remove
612ee443996SEric Van Hensbergen  *
613ee443996SEric Van Hensbergen  */
614ee443996SEric Van Hensbergen 
615f3933545SEric Van Hensbergen static void p9_virtio_remove(struct virtio_device *vdev)
616f3933545SEric Van Hensbergen {
617f3933545SEric Van Hensbergen 	struct virtio_chan *chan = vdev->priv;
618f3933545SEric Van Hensbergen 
619991ad9ecSSasha Levin 	if (chan->inuse)
620991ad9ecSSasha Levin 		p9_virtio_close(chan->client);
621d2a7dddaSMichael S. Tsirkin 	vdev->config->del_vqs(vdev);
62237c1209dSAneesh Kumar K.V 
62337c1209dSAneesh Kumar K.V 	mutex_lock(&virtio_9p_lock);
62437c1209dSAneesh Kumar K.V 	list_del(&chan->chan_list);
62537c1209dSAneesh Kumar K.V 	mutex_unlock(&virtio_9p_lock);
62686c84373SAneesh Kumar K.V 	sysfs_remove_file(&(vdev->dev.kobj), &dev_attr_mount_tag.attr);
62797ee9b02SAneesh Kumar K.V 	kfree(chan->tag);
62852f44e0dSVenkateswararao Jujjuri (JV) 	kfree(chan->vc_wq);
62937c1209dSAneesh Kumar K.V 	kfree(chan);
63037c1209dSAneesh Kumar K.V 
631f3933545SEric Van Hensbergen }
632f3933545SEric Van Hensbergen 
633b530cc79SEric Van Hensbergen static struct virtio_device_id id_table[] = {
634b530cc79SEric Van Hensbergen 	{ VIRTIO_ID_9P, VIRTIO_DEV_ANY_ID },
635b530cc79SEric Van Hensbergen 	{ 0 },
636b530cc79SEric Van Hensbergen };
637b530cc79SEric Van Hensbergen 
63897ee9b02SAneesh Kumar K.V static unsigned int features[] = {
63997ee9b02SAneesh Kumar K.V 	VIRTIO_9P_MOUNT_TAG,
64097ee9b02SAneesh Kumar K.V };
64197ee9b02SAneesh Kumar K.V 
642b530cc79SEric Van Hensbergen /* The standard "struct lguest_driver": */
643b530cc79SEric Van Hensbergen static struct virtio_driver p9_virtio_drv = {
64497ee9b02SAneesh Kumar K.V 	.feature_table  = features,
64597ee9b02SAneesh Kumar K.V 	.feature_table_size = ARRAY_SIZE(features),
646b530cc79SEric Van Hensbergen 	.driver.name    = KBUILD_MODNAME,
647b530cc79SEric Van Hensbergen 	.driver.owner	= THIS_MODULE,
648b530cc79SEric Van Hensbergen 	.id_table	= id_table,
649b530cc79SEric Van Hensbergen 	.probe		= p9_virtio_probe,
650f3933545SEric Van Hensbergen 	.remove		= p9_virtio_remove,
651b530cc79SEric Van Hensbergen };
652b530cc79SEric Van Hensbergen 
653b530cc79SEric Van Hensbergen static struct p9_trans_module p9_virtio_trans = {
654b530cc79SEric Van Hensbergen 	.name = "virtio",
655b530cc79SEric Van Hensbergen 	.create = p9_virtio_create,
6568b81ef58SEric Van Hensbergen 	.close = p9_virtio_close,
65791b8534fSEric Van Hensbergen 	.request = p9_virtio_request,
658abfa034eSAneesh Kumar K.V 	.zc_request = p9_virtio_zc_request,
65991b8534fSEric Van Hensbergen 	.cancel = p9_virtio_cancel,
660b49d8b5dSAneesh Kumar K.V 	/*
661b49d8b5dSAneesh Kumar K.V 	 * We leave one entry for input and one entry for response
662b49d8b5dSAneesh Kumar K.V 	 * headers. We also skip one more entry to accomodate, address
663b49d8b5dSAneesh Kumar K.V 	 * that are not at page boundary, that can result in an extra
664b49d8b5dSAneesh Kumar K.V 	 * page in zero copy.
665b49d8b5dSAneesh Kumar K.V 	 */
666b49d8b5dSAneesh Kumar K.V 	.maxsize = PAGE_SIZE * (VIRTQUEUE_NUM - 3),
667b530cc79SEric Van Hensbergen 	.def = 0,
66872029fe8STejun Heo 	.owner = THIS_MODULE,
669b530cc79SEric Van Hensbergen };
670b530cc79SEric Van Hensbergen 
671b530cc79SEric Van Hensbergen /* The standard init function */
672b530cc79SEric Van Hensbergen static int __init p9_virtio_init(void)
673b530cc79SEric Van Hensbergen {
67437c1209dSAneesh Kumar K.V 	INIT_LIST_HEAD(&virtio_chan_list);
675b530cc79SEric Van Hensbergen 
676b530cc79SEric Van Hensbergen 	v9fs_register_trans(&p9_virtio_trans);
677b530cc79SEric Van Hensbergen 	return register_virtio_driver(&p9_virtio_drv);
678b530cc79SEric Van Hensbergen }
679b530cc79SEric Van Hensbergen 
680f3933545SEric Van Hensbergen static void __exit p9_virtio_cleanup(void)
681f3933545SEric Van Hensbergen {
682f3933545SEric Van Hensbergen 	unregister_virtio_driver(&p9_virtio_drv);
68372029fe8STejun Heo 	v9fs_unregister_trans(&p9_virtio_trans);
684f3933545SEric Van Hensbergen }
685f3933545SEric Van Hensbergen 
686b530cc79SEric Van Hensbergen module_init(p9_virtio_init);
687f3933545SEric Van Hensbergen module_exit(p9_virtio_cleanup);
688b530cc79SEric Van Hensbergen 
689b530cc79SEric Van Hensbergen MODULE_DEVICE_TABLE(virtio, id_table);
690b530cc79SEric Van Hensbergen MODULE_AUTHOR("Eric Van Hensbergen <ericvh@gmail.com>");
691b530cc79SEric Van Hensbergen MODULE_DESCRIPTION("Virtio 9p Transport");
692b530cc79SEric Van Hensbergen MODULE_LICENSE("GPL");
693