blob: a3cd90a74012be927c838dc1e9916f78538948fc [file] [log] [blame]
Thomas Gleixner1f327612019-05-28 09:57:16 -07001// SPDX-License-Identifier: GPL-2.0-only
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -05002/*
Eric Van Hensbergenfea511a2008-10-13 18:45:23 -05003 * The Virtio 9p transport driver
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -05004 *
Eric Van Hensbergene2735b72008-02-06 19:25:58 -06005 * This is a block based transport driver based on the lguest block driver
6 * code.
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -05007 *
Eric Van Hensbergenfea511a2008-10-13 18:45:23 -05008 * Copyright (C) 2007, 2008 Eric Van Hensbergen, IBM Corporation
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -05009 *
10 * Based on virtio console driver
11 * Copyright (C) 2006, 2007 Rusty Russell, IBM Corporation
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -050012 */
13
Joe Perches5d385152011-11-28 10:40:46 -080014#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -050016#include <linux/in.h>
17#include <linux/module.h>
18#include <linux/net.h>
19#include <linux/ipv6.h>
20#include <linux/errno.h>
21#include <linux/kernel.h>
22#include <linux/un.h>
23#include <linux/uaccess.h>
24#include <linux/inet.h>
25#include <linux/idr.h>
26#include <linux/file.h>
Will Deaconb9cdc882012-10-19 14:03:32 +010027#include <linux/highmem.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -050029#include <net/9p/9p.h>
30#include <linux/parser.h>
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -050031#include <net/9p/client.h>
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -050032#include <net/9p/transport.h>
33#include <linux/scatterlist.h>
Venkateswararao Jujjuri (JV)68da9ba2011-03-18 15:49:48 -070034#include <linux/swap.h>
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -050035#include <linux/virtio.h>
36#include <linux/virtio_9p.h>
Venkateswararao Jujjuri (JV)40388662011-01-28 15:22:36 -080037#include "trans_common.h"
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -050038
Eric Van Hensbergene2735b72008-02-06 19:25:58 -060039#define VIRTQUEUE_NUM 128
40
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -050041/* a single mutex to manage channel initialization and attachment */
Josef 'Jeff' Sipekc1549492008-03-07 11:39:13 -060042static DEFINE_MUTEX(virtio_9p_lock);
Venkateswararao Jujjuri (JV)68da9ba2011-03-18 15:49:48 -070043static DECLARE_WAIT_QUEUE_HEAD(vp_wq);
44static atomic_t vp_pinned = ATOMIC_INIT(0);
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -050045
Eric Van Hensbergenee443992008-03-05 07:08:09 -060046/**
47 * struct virtio_chan - per-instance transport information
Eric Van Hensbergenee443992008-03-05 07:08:09 -060048 * @inuse: whether the channel is in use
49 * @lock: protects multiple elements within this structure
Abhishek Kulkarni0e155972009-07-19 13:41:55 -060050 * @client: client instance
Eric Van Hensbergenee443992008-03-05 07:08:09 -060051 * @vdev: virtio dev associated with this channel
52 * @vq: virtio queue associated with this channel
Eric Van Hensbergenee443992008-03-05 07:08:09 -060053 * @sg: scatter gather list which is used to pack a request (protected?)
54 *
55 * We keep all per-channel information in a structure.
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -050056 * This structure is allocated within the devices dev->mem space.
57 * A pointer to the structure will get put in the transport private.
Eric Van Hensbergenee443992008-03-05 07:08:09 -060058 *
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -050059 */
Eric Van Hensbergenee443992008-03-05 07:08:09 -060060
Aneesh Kumar K.V37c1209d42010-02-15 17:27:01 +000061struct virtio_chan {
Eric Van Hensbergenee443992008-03-05 07:08:09 -060062 bool inuse;
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -050063
Eric Van Hensbergene2735b72008-02-06 19:25:58 -060064 spinlock_t lock;
65
Eric Van Hensbergenfea511a2008-10-13 18:45:23 -050066 struct p9_client *client;
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -050067 struct virtio_device *vdev;
Eric Van Hensbergene2735b72008-02-06 19:25:58 -060068 struct virtqueue *vq;
Venkateswararao Jujjuri (JV)52f44e02010-09-29 18:33:41 -070069 int ring_bufs_avail;
70 wait_queue_head_t *vc_wq;
Venkateswararao Jujjuri (JV)68da9ba2011-03-18 15:49:48 -070071 /* This is global limit. Since we don't have a global structure,
72 * will be placing it in each channel.
73 */
Zhang Yanfei7293bfb2013-02-22 16:35:49 -080074 unsigned long p9_max_pages;
Eric Van Hensbergene2735b72008-02-06 19:25:58 -060075 /* Scatterlist: can be too big for stack. */
76 struct scatterlist sg[VIRTQUEUE_NUM];
Aneesh Kumar K.V97ee9b02010-03-06 04:44:14 +000077 /*
piaojunedcd9d92018-08-03 17:22:20 +080078 * tag name to identify a mount null terminated
Aneesh Kumar K.V97ee9b02010-03-06 04:44:14 +000079 */
80 char *tag;
81
Aneesh Kumar K.V37c1209d42010-02-15 17:27:01 +000082 struct list_head chan_list;
83};
84
85static struct list_head virtio_chan_list;
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -050086
87/* How many bytes left in this page. */
88static unsigned int rest_of_page(void *data)
89{
Al Viro222e4ad2016-01-02 13:31:21 -050090 return PAGE_SIZE - offset_in_page(data);
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -050091}
92
Eric Van Hensbergenee443992008-03-05 07:08:09 -060093/**
94 * p9_virtio_close - reclaim resources of a channel
Abhishek Kulkarni0e155972009-07-19 13:41:55 -060095 * @client: client instance
Eric Van Hensbergenee443992008-03-05 07:08:09 -060096 *
97 * This reclaims a channel by freeing its resources and
98 * reseting its inuse flag.
99 *
100 */
101
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500102static void p9_virtio_close(struct p9_client *client)
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500103{
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500104 struct virtio_chan *chan = client->trans;
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500105
Josef 'Jeff' Sipekc1549492008-03-07 11:39:13 -0600106 mutex_lock(&virtio_9p_lock);
Aneesh Kumar K.Vfb786102010-02-08 11:50:32 +0000107 if (chan)
108 chan->inuse = false;
Josef 'Jeff' Sipekc1549492008-03-07 11:39:13 -0600109 mutex_unlock(&virtio_9p_lock);
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500110}
111
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600112/**
113 * req_done - callback which signals activity from the server
114 * @vq: virtio queue activity was received on
115 *
116 * This notifies us that the server has triggered some activity
117 * on the virtio channel - most likely a response to request we
118 * sent. Figure out which requests now have responses and wake up
119 * those threads.
120 *
121 * Bugs: could do with some additional sanity checking, but appears to work.
122 *
123 */
124
Eric Van Hensbergene2735b72008-02-06 19:25:58 -0600125static void req_done(struct virtqueue *vq)
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500126{
Eric Van Hensbergene2735b72008-02-06 19:25:58 -0600127 struct virtio_chan *chan = vq->vdev->priv;
Eric Van Hensbergene2735b72008-02-06 19:25:58 -0600128 unsigned int len;
Eric Van Hensbergene2735b72008-02-06 19:25:58 -0600129 struct p9_req_t *req;
jiangyiwen31934da2018-07-19 15:17:00 +0800130 bool need_wakeup = false;
Venkateswararao Jujjuri (JV)419b3952010-09-29 18:06:54 -0700131 unsigned long flags;
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500132
Joe Perches5d385152011-11-28 10:40:46 -0800133 p9_debug(P9_DEBUG_TRANS, ": request done\n");
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500134
jiangyiwen31934da2018-07-19 15:17:00 +0800135 spin_lock_irqsave(&chan->lock, flags);
136 while ((req = virtqueue_get_buf(chan->vq, &len)) != NULL) {
137 if (!chan->ring_bufs_avail) {
138 chan->ring_bufs_avail = 1;
139 need_wakeup = true;
Venkateswararao Jujjuri (JV)419b3952010-09-29 18:06:54 -0700140 }
jiangyiwen31934da2018-07-19 15:17:00 +0800141
Tomas Bortolif9845792018-07-23 17:44:04 +0200142 if (len) {
Dominique Martinet523adb62018-07-30 05:55:19 +0000143 req->rc.size = len;
Greg Kurz26d99832018-01-22 22:02:05 +0100144 p9_client_cb(chan->client, req, REQ_STATUS_RCVD);
Tomas Bortolif9845792018-07-23 17:44:04 +0200145 }
Venkateswararao Jujjuri (JV)a01a9842011-03-14 14:12:49 -0700146 }
jiangyiwen31934da2018-07-19 15:17:00 +0800147 spin_unlock_irqrestore(&chan->lock, flags);
148 /* Wakeup if anyone waiting for VirtIO ring space. */
149 if (need_wakeup)
150 wake_up(chan->vc_wq);
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500151}
152
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600153/**
154 * pack_sg_list - pack a scatter gather list from a linear buffer
155 * @sg: scatter/gather list to pack into
156 * @start: which segment of the sg_list to start at
157 * @limit: maximum segment to pack data to
158 * @data: data to pack into scatter/gather list
159 * @count: amount of data to pack into the scatter/gather list
160 *
161 * sg_lists have multiple segments of various sizes. This will pack
162 * arbitrary data into an existing scatter gather list, segmenting the
163 * data as necessary within constraints.
164 *
165 */
166
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530167static int pack_sg_list(struct scatterlist *sg, int start,
168 int limit, char *data, int count)
Eric Van Hensbergene2735b72008-02-06 19:25:58 -0600169{
170 int s;
171 int index = start;
172
173 while (count) {
174 s = rest_of_page(data);
175 if (s > count)
176 s = count;
jiangyiwen23cba9c2018-08-03 12:11:34 +0800177 BUG_ON(index >= limit);
Rusty Russell0b36f1a2013-03-20 15:44:30 +1030178 /* Make sure we don't terminate early. */
179 sg_unmark_end(&sg[index]);
Eric Van Hensbergene2735b72008-02-06 19:25:58 -0600180 sg_set_buf(&sg[index++], data, s);
181 count -= s;
182 data += s;
Eric Van Hensbergene2735b72008-02-06 19:25:58 -0600183 }
Rusty Russell0b36f1a2013-03-20 15:44:30 +1030184 if (index-start)
185 sg_mark_end(&sg[index - 1]);
Eric Van Hensbergene2735b72008-02-06 19:25:58 -0600186 return index-start;
187}
188
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500189/* We don't currently allow canceling of virtio requests */
190static int p9_virtio_cancel(struct p9_client *client, struct p9_req_t *req)
191{
192 return 1;
193}
194
Tomas Bortoli728356d2018-08-14 19:43:42 +0200195/* Reply won't come, so drop req ref */
196static int p9_virtio_cancelled(struct p9_client *client, struct p9_req_t *req)
197{
198 p9_req_put(req);
199 return 0;
200}
201
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600202/**
Venkateswararao Jujjuri (JV)40388662011-01-28 15:22:36 -0800203 * pack_sg_list_p - Just like pack_sg_list. Instead of taking a buffer,
204 * this takes a list of pages.
205 * @sg: scatter/gather list to pack into
206 * @start: which segment of the sg_list to start at
Ben Hutchings2c530402012-07-10 10:55:09 +0000207 * @pdata: a list of pages to add into sg.
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530208 * @nr_pages: number of pages to pack into the scatter/gather list
Al Viro4f3b35c2015-04-01 19:57:53 -0400209 * @offs: amount of data in the beginning of first page _not_ to pack
Venkateswararao Jujjuri (JV)40388662011-01-28 15:22:36 -0800210 * @count: amount of data to pack into the scatter/gather list
211 */
212static int
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530213pack_sg_list_p(struct scatterlist *sg, int start, int limit,
Al Viro4f3b35c2015-04-01 19:57:53 -0400214 struct page **pdata, int nr_pages, size_t offs, int count)
Venkateswararao Jujjuri (JV)40388662011-01-28 15:22:36 -0800215{
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530216 int i = 0, s;
Al Viro4f3b35c2015-04-01 19:57:53 -0400217 int data_off = offs;
Venkateswararao Jujjuri (JV)40388662011-01-28 15:22:36 -0800218 int index = start;
219
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530220 BUG_ON(nr_pages > (limit - start));
221 /*
222 * if the first page doesn't start at
223 * page boundary find the offset
224 */
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530225 while (nr_pages) {
Al Viro4f3b35c2015-04-01 19:57:53 -0400226 s = PAGE_SIZE - data_off;
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530227 if (s > count)
228 s = count;
jiangyiwen23cba9c2018-08-03 12:11:34 +0800229 BUG_ON(index >= limit);
Rusty Russell0b36f1a2013-03-20 15:44:30 +1030230 /* Make sure we don't terminate early. */
231 sg_unmark_end(&sg[index]);
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530232 sg_set_page(&sg[index++], pdata[i++], s, data_off);
233 data_off = 0;
Venkateswararao Jujjuri (JV)40388662011-01-28 15:22:36 -0800234 count -= s;
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530235 nr_pages--;
Venkateswararao Jujjuri (JV)40388662011-01-28 15:22:36 -0800236 }
Rusty Russell0b36f1a2013-03-20 15:44:30 +1030237
238 if (index-start)
239 sg_mark_end(&sg[index - 1]);
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530240 return index - start;
Venkateswararao Jujjuri (JV)40388662011-01-28 15:22:36 -0800241}
242
243/**
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500244 * p9_virtio_request - issue a request
Abhishek Kulkarni0e155972009-07-19 13:41:55 -0600245 * @client: client instance issuing the request
246 * @req: request to be issued
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600247 *
248 */
249
Eric Van Hensbergene2735b72008-02-06 19:25:58 -0600250static int
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500251p9_virtio_request(struct p9_client *client, struct p9_req_t *req)
Eric Van Hensbergene2735b72008-02-06 19:25:58 -0600252{
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530253 int err;
Rusty Russell0b36f1a2013-03-20 15:44:30 +1030254 int in, out, out_sgs, in_sgs;
Venkateswararao Jujjuri (JV)419b3952010-09-29 18:06:54 -0700255 unsigned long flags;
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530256 struct virtio_chan *chan = client->trans;
Rusty Russell0b36f1a2013-03-20 15:44:30 +1030257 struct scatterlist *sgs[2];
Eric Van Hensbergene2735b72008-02-06 19:25:58 -0600258
Joe Perches5d385152011-11-28 10:40:46 -0800259 p9_debug(P9_DEBUG_TRANS, "9p debug: virtio request\n");
Eric Van Hensbergene2735b72008-02-06 19:25:58 -0600260
Venkateswararao Jujjuri (JV)419b3952010-09-29 18:06:54 -0700261 req->status = REQ_STATUS_SENT;
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530262req_retry:
Venkateswararao Jujjuri (JV)419b3952010-09-29 18:06:54 -0700263 spin_lock_irqsave(&chan->lock, flags);
Venkateswararao Jujjuri (JV)40388662011-01-28 15:22:36 -0800264
Rusty Russell0b36f1a2013-03-20 15:44:30 +1030265 out_sgs = in_sgs = 0;
Venkateswararao Jujjuri (JV)40388662011-01-28 15:22:36 -0800266 /* Handle out VirtIO ring buffers */
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530267 out = pack_sg_list(chan->sg, 0,
Dominique Martinet523adb62018-07-30 05:55:19 +0000268 VIRTQUEUE_NUM, req->tc.sdata, req->tc.size);
Rusty Russell0b36f1a2013-03-20 15:44:30 +1030269 if (out)
270 sgs[out_sgs++] = chan->sg;
Venkateswararao Jujjuri (JV)40388662011-01-28 15:22:36 -0800271
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530272 in = pack_sg_list(chan->sg, out,
Dominique Martinet523adb62018-07-30 05:55:19 +0000273 VIRTQUEUE_NUM, req->rc.sdata, req->rc.capacity);
Rusty Russell0b36f1a2013-03-20 15:44:30 +1030274 if (in)
275 sgs[out_sgs + in_sgs++] = chan->sg + out;
Eric Van Hensbergene2735b72008-02-06 19:25:58 -0600276
Al Viro474fe9f2015-07-12 21:06:44 -0400277 err = virtqueue_add_sgs(chan->vq, sgs, out_sgs, in_sgs, req,
Rusty Russellf96fde42012-01-12 15:44:42 +1030278 GFP_ATOMIC);
Venkateswararao Jujjuri (JV)419b3952010-09-29 18:06:54 -0700279 if (err < 0) {
Venkateswararao Jujjuri (JV)52f44e02010-09-29 18:33:41 -0700280 if (err == -ENOSPC) {
281 chan->ring_bufs_avail = 0;
282 spin_unlock_irqrestore(&chan->lock, flags);
Tuomas Tynkkynen9523fea2017-09-06 17:59:08 +0300283 err = wait_event_killable(*chan->vc_wq,
284 chan->ring_bufs_avail);
Venkateswararao Jujjuri (JV)52f44e02010-09-29 18:33:41 -0700285 if (err == -ERESTARTSYS)
286 return err;
287
Joe Perches5d385152011-11-28 10:40:46 -0800288 p9_debug(P9_DEBUG_TRANS, "Retry virtio request\n");
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530289 goto req_retry;
Venkateswararao Jujjuri (JV)52f44e02010-09-29 18:33:41 -0700290 } else {
291 spin_unlock_irqrestore(&chan->lock, flags);
Joe Perches5d385152011-11-28 10:40:46 -0800292 p9_debug(P9_DEBUG_TRANS,
Rusty Russell0b36f1a2013-03-20 15:44:30 +1030293 "virtio rpc add_sgs returned failure\n");
Venkateswararao Jujjuri (JV)52f44e02010-09-29 18:33:41 -0700294 return -EIO;
295 }
Eric Van Hensbergene2735b72008-02-06 19:25:58 -0600296 }
Michael S. Tsirkindc3f5e62010-04-13 16:11:50 +0300297 virtqueue_kick(chan->vq);
Venkateswararao Jujjuri (JV)419b3952010-09-29 18:06:54 -0700298 spin_unlock_irqrestore(&chan->lock, flags);
Eric Van Hensbergene2735b72008-02-06 19:25:58 -0600299
Joe Perches5d385152011-11-28 10:40:46 -0800300 p9_debug(P9_DEBUG_TRANS, "virtio request kicked\n");
Eric Van Hensbergene2735b72008-02-06 19:25:58 -0600301 return 0;
302}
303
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530304static int p9_get_mapped_pages(struct virtio_chan *chan,
Al Viro4f3b35c2015-04-01 19:57:53 -0400305 struct page ***pages,
306 struct iov_iter *data,
307 int count,
308 size_t *offs,
309 int *need_drop)
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530310{
Al Viro4f3b35c2015-04-01 19:57:53 -0400311 int nr_pages;
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530312 int err;
Al Viro4f3b35c2015-04-01 19:57:53 -0400313
314 if (!iov_iter_count(data))
315 return 0;
316
Marc Zyngier2cbfdf42018-11-02 17:16:51 +0000317 if (!iov_iter_is_kvec(data)) {
Al Viro4f3b35c2015-04-01 19:57:53 -0400318 int n;
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530319 /*
320 * We allow only p9_max_pages pinned. We wait for the
321 * Other zc request to finish here
322 */
323 if (atomic_read(&vp_pinned) >= chan->p9_max_pages) {
Tuomas Tynkkynen9523fea2017-09-06 17:59:08 +0300324 err = wait_event_killable(vp_wq,
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530325 (atomic_read(&vp_pinned) < chan->p9_max_pages));
326 if (err == -ERESTARTSYS)
327 return err;
328 }
Al Viro4f3b35c2015-04-01 19:57:53 -0400329 n = iov_iter_get_pages_alloc(data, pages, count, offs);
330 if (n < 0)
331 return n;
332 *need_drop = 1;
333 nr_pages = DIV_ROUND_UP(n + *offs, PAGE_SIZE);
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530334 atomic_add(nr_pages, &vp_pinned);
Al Viro4f3b35c2015-04-01 19:57:53 -0400335 return n;
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530336 } else {
337 /* kernel buffer, no need to pin pages */
Al Viro4f3b35c2015-04-01 19:57:53 -0400338 int index;
339 size_t len;
340 void *p;
341
342 /* we'd already checked that it's non-empty */
343 while (1) {
344 len = iov_iter_single_seg_count(data);
345 if (likely(len)) {
346 p = data->kvec->iov_base + data->iov_offset;
347 break;
348 }
349 iov_iter_advance(data, 0);
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530350 }
Al Viro4f3b35c2015-04-01 19:57:53 -0400351 if (len > count)
352 len = count;
353
354 nr_pages = DIV_ROUND_UP((unsigned long)p + len, PAGE_SIZE) -
355 (unsigned long)p / PAGE_SIZE;
356
Kees Cook6da2ec52018-06-12 13:55:00 -0700357 *pages = kmalloc_array(nr_pages, sizeof(struct page *),
358 GFP_NOFS);
Al Viro4f3b35c2015-04-01 19:57:53 -0400359 if (!*pages)
360 return -ENOMEM;
361
362 *need_drop = 0;
Al Viro222e4ad2016-01-02 13:31:21 -0500363 p -= (*offs = offset_in_page(p));
Al Viro4f3b35c2015-04-01 19:57:53 -0400364 for (index = 0; index < nr_pages; index++) {
365 if (is_vmalloc_addr(p))
366 (*pages)[index] = vmalloc_to_page(p);
367 else
368 (*pages)[index] = kmap_to_page(p);
369 p += PAGE_SIZE;
370 }
371 return len;
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530372 }
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530373}
374
375/**
376 * p9_virtio_zc_request - issue a zero copy request
377 * @client: client instance issuing the request
378 * @req: request to be issued
piaojunc7ebbae2018-07-18 11:45:29 +0800379 * @uidata: user buffer that should be used for zero copy read
380 * @uodata: user buffer that should be used for zero copy write
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530381 * @inlen: read buffer size
Sun Lianwen4a026da2018-05-08 09:49:38 +0800382 * @outlen: write buffer size
383 * @in_hdr_len: reader header size, This is the size of response protocol data
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530384 *
385 */
386static int
387p9_virtio_zc_request(struct p9_client *client, struct p9_req_t *req,
Al Viro4f3b35c2015-04-01 19:57:53 -0400388 struct iov_iter *uidata, struct iov_iter *uodata,
389 int inlen, int outlen, int in_hdr_len)
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530390{
Rusty Russell0b36f1a2013-03-20 15:44:30 +1030391 int in, out, err, out_sgs, in_sgs;
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530392 unsigned long flags;
393 int in_nr_pages = 0, out_nr_pages = 0;
394 struct page **in_pages = NULL, **out_pages = NULL;
395 struct virtio_chan *chan = client->trans;
Rusty Russell0b36f1a2013-03-20 15:44:30 +1030396 struct scatterlist *sgs[4];
Al Viro4f3b35c2015-04-01 19:57:53 -0400397 size_t offs;
398 int need_drop = 0;
Tomas Bortoli728356d2018-08-14 19:43:42 +0200399 int kicked = 0;
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530400
Joe Perches5d385152011-11-28 10:40:46 -0800401 p9_debug(P9_DEBUG_TRANS, "virtio request\n");
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530402
403 if (uodata) {
Chirantan Ekboted28c7562018-07-16 17:35:29 -0700404 __le32 sz;
Al Viro4f3b35c2015-04-01 19:57:53 -0400405 int n = p9_get_mapped_pages(chan, &out_pages, uodata,
406 outlen, &offs, &need_drop);
Tomas Bortoli728356d2018-08-14 19:43:42 +0200407 if (n < 0) {
408 err = n;
409 goto err_out;
410 }
Al Viro4f3b35c2015-04-01 19:57:53 -0400411 out_nr_pages = DIV_ROUND_UP(n + offs, PAGE_SIZE);
412 if (n != outlen) {
413 __le32 v = cpu_to_le32(n);
Dominique Martinet523adb62018-07-30 05:55:19 +0000414 memcpy(&req->tc.sdata[req->tc.size - 4], &v, 4);
Al Viro4f3b35c2015-04-01 19:57:53 -0400415 outlen = n;
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530416 }
Chirantan Ekboted28c7562018-07-16 17:35:29 -0700417 /* The size field of the message must include the length of the
418 * header and the length of the data. We didn't actually know
419 * the length of the data until this point so add it in now.
420 */
Dominique Martinet523adb62018-07-30 05:55:19 +0000421 sz = cpu_to_le32(req->tc.size + outlen);
422 memcpy(&req->tc.sdata[0], &sz, sizeof(sz));
Al Viro4f3b35c2015-04-01 19:57:53 -0400423 } else if (uidata) {
424 int n = p9_get_mapped_pages(chan, &in_pages, uidata,
425 inlen, &offs, &need_drop);
Tomas Bortoli728356d2018-08-14 19:43:42 +0200426 if (n < 0) {
427 err = n;
428 goto err_out;
429 }
Al Viro4f3b35c2015-04-01 19:57:53 -0400430 in_nr_pages = DIV_ROUND_UP(n + offs, PAGE_SIZE);
431 if (n != inlen) {
432 __le32 v = cpu_to_le32(n);
Dominique Martinet523adb62018-07-30 05:55:19 +0000433 memcpy(&req->tc.sdata[req->tc.size - 4], &v, 4);
Al Viro4f3b35c2015-04-01 19:57:53 -0400434 inlen = n;
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530435 }
436 }
437 req->status = REQ_STATUS_SENT;
438req_retry_pinned:
439 spin_lock_irqsave(&chan->lock, flags);
Rusty Russell0b36f1a2013-03-20 15:44:30 +1030440
441 out_sgs = in_sgs = 0;
442
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530443 /* out data */
444 out = pack_sg_list(chan->sg, 0,
Dominique Martinet523adb62018-07-30 05:55:19 +0000445 VIRTQUEUE_NUM, req->tc.sdata, req->tc.size);
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530446
Rusty Russell0b36f1a2013-03-20 15:44:30 +1030447 if (out)
448 sgs[out_sgs++] = chan->sg;
449
450 if (out_pages) {
451 sgs[out_sgs++] = chan->sg + out;
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530452 out += pack_sg_list_p(chan->sg, out, VIRTQUEUE_NUM,
Al Viro4f3b35c2015-04-01 19:57:53 -0400453 out_pages, out_nr_pages, offs, outlen);
Rusty Russell0b36f1a2013-03-20 15:44:30 +1030454 }
Stephen Hemmingerc69f297d2018-07-24 12:29:10 -0700455
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530456 /*
457 * Take care of in data
458 * For example TREAD have 11.
459 * 11 is the read/write header = PDU Header(7) + IO Size (4).
460 * Arrange in such a way that server places header in the
461 * alloced memory and payload onto the user buffer.
462 */
463 in = pack_sg_list(chan->sg, out,
Dominique Martinet523adb62018-07-30 05:55:19 +0000464 VIRTQUEUE_NUM, req->rc.sdata, in_hdr_len);
Rusty Russell0b36f1a2013-03-20 15:44:30 +1030465 if (in)
466 sgs[out_sgs + in_sgs++] = chan->sg + out;
467
468 if (in_pages) {
469 sgs[out_sgs + in_sgs++] = chan->sg + out + in;
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530470 in += pack_sg_list_p(chan->sg, out + in, VIRTQUEUE_NUM,
Al Viro4f3b35c2015-04-01 19:57:53 -0400471 in_pages, in_nr_pages, offs, inlen);
Rusty Russell0b36f1a2013-03-20 15:44:30 +1030472 }
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530473
Rusty Russell0b36f1a2013-03-20 15:44:30 +1030474 BUG_ON(out_sgs + in_sgs > ARRAY_SIZE(sgs));
Al Viro474fe9f2015-07-12 21:06:44 -0400475 err = virtqueue_add_sgs(chan->vq, sgs, out_sgs, in_sgs, req,
Rusty Russellf96fde42012-01-12 15:44:42 +1030476 GFP_ATOMIC);
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530477 if (err < 0) {
478 if (err == -ENOSPC) {
479 chan->ring_bufs_avail = 0;
480 spin_unlock_irqrestore(&chan->lock, flags);
Tuomas Tynkkynen9523fea2017-09-06 17:59:08 +0300481 err = wait_event_killable(*chan->vc_wq,
482 chan->ring_bufs_avail);
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530483 if (err == -ERESTARTSYS)
484 goto err_out;
485
Joe Perches5d385152011-11-28 10:40:46 -0800486 p9_debug(P9_DEBUG_TRANS, "Retry virtio request\n");
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530487 goto req_retry_pinned;
488 } else {
489 spin_unlock_irqrestore(&chan->lock, flags);
Joe Perches5d385152011-11-28 10:40:46 -0800490 p9_debug(P9_DEBUG_TRANS,
Rusty Russell0b36f1a2013-03-20 15:44:30 +1030491 "virtio rpc add_sgs returned failure\n");
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530492 err = -EIO;
493 goto err_out;
494 }
495 }
496 virtqueue_kick(chan->vq);
497 spin_unlock_irqrestore(&chan->lock, flags);
Tomas Bortoli728356d2018-08-14 19:43:42 +0200498 kicked = 1;
Joe Perches5d385152011-11-28 10:40:46 -0800499 p9_debug(P9_DEBUG_TRANS, "virtio request kicked\n");
Matthew Wilcox2557d0c2018-07-11 14:02:23 -0700500 err = wait_event_killable(req->wq, req->status >= REQ_STATUS_RCVD);
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530501 /*
502 * Non kernel buffers are pinned, unpin them
503 */
504err_out:
Al Viro4f3b35c2015-04-01 19:57:53 -0400505 if (need_drop) {
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530506 if (in_pages) {
507 p9_release_pages(in_pages, in_nr_pages);
508 atomic_sub(in_nr_pages, &vp_pinned);
509 }
510 if (out_pages) {
511 p9_release_pages(out_pages, out_nr_pages);
512 atomic_sub(out_nr_pages, &vp_pinned);
513 }
514 /* wakeup anybody waiting for slots to pin pages */
515 wake_up(&vp_wq);
516 }
Vegard Nossum1b8553c2016-08-03 19:59:47 +0300517 kvfree(in_pages);
518 kvfree(out_pages);
Tomas Bortoli728356d2018-08-14 19:43:42 +0200519 if (!kicked) {
520 /* reply won't come */
521 p9_req_put(req);
522 }
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530523 return err;
524}
525
Aneesh Kumar K.V86c84372010-03-06 04:44:15 +0000526static ssize_t p9_mount_tag_show(struct device *dev,
527 struct device_attribute *attr, char *buf)
528{
529 struct virtio_chan *chan;
530 struct virtio_device *vdev;
piaojunedcd9d92018-08-03 17:22:20 +0800531 int tag_len;
Aneesh Kumar K.V86c84372010-03-06 04:44:15 +0000532
533 vdev = dev_to_virtio(dev);
534 chan = vdev->priv;
piaojunedcd9d92018-08-03 17:22:20 +0800535 tag_len = strlen(chan->tag);
Aneesh Kumar K.V86c84372010-03-06 04:44:15 +0000536
piaojunedcd9d92018-08-03 17:22:20 +0800537 memcpy(buf, chan->tag, tag_len + 1);
Andrey Ryabinin179a5bc2015-01-27 16:00:19 +0300538
piaojunedcd9d92018-08-03 17:22:20 +0800539 return tag_len + 1;
Aneesh Kumar K.V86c84372010-03-06 04:44:15 +0000540}
541
542static DEVICE_ATTR(mount_tag, 0444, p9_mount_tag_show, NULL);
543
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600544/**
545 * p9_virtio_probe - probe for existence of 9P virtio channels
546 * @vdev: virtio device to probe
547 *
Aneesh Kumar K.V37c1209d42010-02-15 17:27:01 +0000548 * This probes for existing virtio channels.
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600549 *
550 */
551
Eric Van Hensbergene2735b72008-02-06 19:25:58 -0600552static int p9_virtio_probe(struct virtio_device *vdev)
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500553{
Aneesh Kumar K.V97ee9b02010-03-06 04:44:14 +0000554 __u16 tag_len;
555 char *tag;
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500556 int err;
557 struct virtio_chan *chan;
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500558
Michael S. Tsirkin7754f532015-01-12 16:23:37 +0200559 if (!vdev->config->get) {
560 dev_err(&vdev->dev, "%s failure: config access disabled\n",
561 __func__);
562 return -EINVAL;
563 }
564
Aneesh Kumar K.V37c1209d42010-02-15 17:27:01 +0000565 chan = kmalloc(sizeof(struct virtio_chan), GFP_KERNEL);
566 if (!chan) {
Joe Perches5d385152011-11-28 10:40:46 -0800567 pr_err("Failed to allocate virtio 9P channel\n");
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500568 err = -ENOMEM;
569 goto fail;
570 }
571
Eric Van Hensbergene2735b72008-02-06 19:25:58 -0600572 chan->vdev = vdev;
573
574 /* We expect one virtqueue, for requests. */
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600575 chan->vq = virtio_find_single_vq(vdev, req_done, "requests");
Eric Van Hensbergene2735b72008-02-06 19:25:58 -0600576 if (IS_ERR(chan->vq)) {
577 err = PTR_ERR(chan->vq);
Jean-Philippe Brucker92aef462018-07-17 19:14:45 -0700578 goto out_free_chan;
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500579 }
Eric Van Hensbergene2735b72008-02-06 19:25:58 -0600580 chan->vq->vdev->priv = chan;
581 spin_lock_init(&chan->lock);
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500582
Eric Van Hensbergene2735b72008-02-06 19:25:58 -0600583 sg_init_table(chan->sg, VIRTQUEUE_NUM);
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500584
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500585 chan->inuse = false;
Aneesh Kumar K.V97ee9b02010-03-06 04:44:14 +0000586 if (virtio_has_feature(vdev, VIRTIO_9P_MOUNT_TAG)) {
Rusty Russell855e0c52013-10-14 18:11:51 +1030587 virtio_cread(vdev, struct virtio_9p_config, tag_len, &tag_len);
Aneesh Kumar K.V97ee9b02010-03-06 04:44:14 +0000588 } else {
589 err = -EINVAL;
590 goto out_free_vq;
591 }
piaojunedcd9d92018-08-03 17:22:20 +0800592 tag = kzalloc(tag_len + 1, GFP_KERNEL);
Aneesh Kumar K.V97ee9b02010-03-06 04:44:14 +0000593 if (!tag) {
594 err = -ENOMEM;
595 goto out_free_vq;
596 }
Rusty Russell855e0c52013-10-14 18:11:51 +1030597
598 virtio_cread_bytes(vdev, offsetof(struct virtio_9p_config, tag),
599 tag, tag_len);
Aneesh Kumar K.V97ee9b02010-03-06 04:44:14 +0000600 chan->tag = tag;
Aneesh Kumar K.V86c84372010-03-06 04:44:15 +0000601 err = sysfs_create_file(&(vdev->dev.kobj), &dev_attr_mount_tag.attr);
602 if (err) {
Venkateswararao Jujjuri (JV)52f44e02010-09-29 18:33:41 -0700603 goto out_free_tag;
Aneesh Kumar K.V86c84372010-03-06 04:44:15 +0000604 }
Venkateswararao Jujjuri (JV)52f44e02010-09-29 18:33:41 -0700605 chan->vc_wq = kmalloc(sizeof(wait_queue_head_t), GFP_KERNEL);
606 if (!chan->vc_wq) {
607 err = -ENOMEM;
608 goto out_free_tag;
609 }
610 init_waitqueue_head(chan->vc_wq);
611 chan->ring_bufs_avail = 1;
Venkateswararao Jujjuri (JV)68da9ba2011-03-18 15:49:48 -0700612 /* Ceiling limit to avoid denial of service attacks */
613 chan->p9_max_pages = nr_free_buffer_pages()/4;
Venkateswararao Jujjuri (JV)52f44e02010-09-29 18:33:41 -0700614
Michael S. Tsirkin64b4cc32014-10-15 10:22:31 +1030615 virtio_device_ready(vdev);
616
Aneesh Kumar K.V37c1209d42010-02-15 17:27:01 +0000617 mutex_lock(&virtio_9p_lock);
618 list_add_tail(&chan->chan_list, &virtio_chan_list);
619 mutex_unlock(&virtio_9p_lock);
Michael Marineaue0d6cb92013-08-11 00:53:45 -0400620
621 /* Let udev rules use the new mount_tag attribute. */
622 kobject_uevent(&(vdev->dev.kobj), KOBJ_CHANGE);
623
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500624 return 0;
625
Venkateswararao Jujjuri (JV)52f44e02010-09-29 18:33:41 -0700626out_free_tag:
627 kfree(tag);
Eric Van Hensbergene2735b72008-02-06 19:25:58 -0600628out_free_vq:
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600629 vdev->config->del_vqs(vdev);
Jean-Philippe Brucker92aef462018-07-17 19:14:45 -0700630out_free_chan:
Aneesh Kumar K.V37c1209d42010-02-15 17:27:01 +0000631 kfree(chan);
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500632fail:
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500633 return err;
634}
635
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600636
637/**
638 * p9_virtio_create - allocate a new virtio channel
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500639 * @client: client instance invoking this transport
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600640 * @devname: string identifying the channel to connect to (unused)
641 * @args: args passed from sys_mount() for per-transport options (unused)
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600642 *
643 * This sets up a transport channel for 9p communication. Right now
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500644 * we only match the first available channel, but eventually we couldlook up
645 * alternate channels by matching devname versus a virtio_config entry.
646 * We use a simple reference count mechanism to ensure that only a single
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600647 * mount has a channel open at a time.
648 *
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600649 */
650
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500651static int
652p9_virtio_create(struct p9_client *client, const char *devname, char *args)
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500653{
Aneesh Kumar K.V37c1209d42010-02-15 17:27:01 +0000654 struct virtio_chan *chan;
Aneesh Kumar K.Vc1a7c222010-02-15 17:27:02 +0000655 int ret = -ENOENT;
Aneesh Kumar K.V37c1209d42010-02-15 17:27:01 +0000656 int found = 0;
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500657
Tomas Bortoli10aa1452018-07-27 13:05:58 +0200658 if (devname == NULL)
659 return -EINVAL;
660
Josef 'Jeff' Sipekc1549492008-03-07 11:39:13 -0600661 mutex_lock(&virtio_9p_lock);
Aneesh Kumar K.V37c1209d42010-02-15 17:27:01 +0000662 list_for_each_entry(chan, &virtio_chan_list, chan_list) {
piaojunedcd9d92018-08-03 17:22:20 +0800663 if (!strcmp(devname, chan->tag)) {
Aneesh Kumar K.Vf75580c2010-02-15 17:27:00 +0000664 if (!chan->inuse) {
665 chan->inuse = true;
Aneesh Kumar K.V37c1209d42010-02-15 17:27:01 +0000666 found = 1;
Aneesh Kumar K.Vf75580c2010-02-15 17:27:00 +0000667 break;
668 }
Aneesh Kumar K.Vc1a7c222010-02-15 17:27:02 +0000669 ret = -EBUSY;
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500670 }
671 }
Josef 'Jeff' Sipekc1549492008-03-07 11:39:13 -0600672 mutex_unlock(&virtio_9p_lock);
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500673
Aneesh Kumar K.V37c1209d42010-02-15 17:27:01 +0000674 if (!found) {
Aneesh Kumar K.Vc7c72c52015-09-03 13:36:51 +0530675 pr_err("no channels available for device %s\n", devname);
Aneesh Kumar K.Vc1a7c222010-02-15 17:27:02 +0000676 return ret;
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500677 }
678
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500679 client->trans = (void *)chan;
Eric Van Hensbergen562ada62010-01-15 18:54:03 -0600680 client->status = Connected;
Eric Van Hensbergenfea511a2008-10-13 18:45:23 -0500681 chan->client = client;
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500682
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500683 return 0;
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500684}
685
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600686/**
687 * p9_virtio_remove - clean up resources associated with a virtio device
688 * @vdev: virtio device to remove
689 *
690 */
691
Eric Van Hensbergenf3933542008-02-06 19:25:04 -0600692static void p9_virtio_remove(struct virtio_device *vdev)
693{
694 struct virtio_chan *chan = vdev->priv;
Michael S. Tsirkin8051a2a2015-03-12 11:53:41 +1030695 unsigned long warning_time;
Eric Van Hensbergenf3933542008-02-06 19:25:04 -0600696
Aneesh Kumar K.V37c1209d42010-02-15 17:27:01 +0000697 mutex_lock(&virtio_9p_lock);
Michael S. Tsirkin8051a2a2015-03-12 11:53:41 +1030698
699 /* Remove self from list so we don't get new users. */
Aneesh Kumar K.V37c1209d42010-02-15 17:27:01 +0000700 list_del(&chan->chan_list);
Michael S. Tsirkin8051a2a2015-03-12 11:53:41 +1030701 warning_time = jiffies;
702
703 /* Wait for existing users to close. */
704 while (chan->inuse) {
705 mutex_unlock(&virtio_9p_lock);
706 msleep(250);
707 if (time_after(jiffies, warning_time + 10 * HZ)) {
708 dev_emerg(&vdev->dev,
709 "p9_virtio_remove: waiting for device in use.\n");
710 warning_time = jiffies;
711 }
712 mutex_lock(&virtio_9p_lock);
713 }
714
Aneesh Kumar K.V37c1209d42010-02-15 17:27:01 +0000715 mutex_unlock(&virtio_9p_lock);
Michael S. Tsirkin8051a2a2015-03-12 11:53:41 +1030716
Pierre Morelea52bf82015-07-09 14:58:26 +0200717 vdev->config->reset(vdev);
Michael S. Tsirkin8051a2a2015-03-12 11:53:41 +1030718 vdev->config->del_vqs(vdev);
719
Aneesh Kumar K.V86c84372010-03-06 04:44:15 +0000720 sysfs_remove_file(&(vdev->dev.kobj), &dev_attr_mount_tag.attr);
Michael Marineaue0d6cb92013-08-11 00:53:45 -0400721 kobject_uevent(&(vdev->dev.kobj), KOBJ_CHANGE);
Aneesh Kumar K.V97ee9b02010-03-06 04:44:14 +0000722 kfree(chan->tag);
Venkateswararao Jujjuri (JV)52f44e02010-09-29 18:33:41 -0700723 kfree(chan->vc_wq);
Aneesh Kumar K.V37c1209d42010-02-15 17:27:01 +0000724 kfree(chan);
725
Eric Van Hensbergenf3933542008-02-06 19:25:04 -0600726}
727
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500728static struct virtio_device_id id_table[] = {
729 { VIRTIO_ID_9P, VIRTIO_DEV_ANY_ID },
730 { 0 },
731};
732
Aneesh Kumar K.V97ee9b02010-03-06 04:44:14 +0000733static unsigned int features[] = {
734 VIRTIO_9P_MOUNT_TAG,
735};
736
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500737/* The standard "struct lguest_driver": */
738static struct virtio_driver p9_virtio_drv = {
Aneesh Kumar K.V97ee9b02010-03-06 04:44:14 +0000739 .feature_table = features,
740 .feature_table_size = ARRAY_SIZE(features),
741 .driver.name = KBUILD_MODNAME,
742 .driver.owner = THIS_MODULE,
743 .id_table = id_table,
744 .probe = p9_virtio_probe,
745 .remove = p9_virtio_remove,
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500746};
747
748static struct p9_trans_module p9_virtio_trans = {
749 .name = "virtio",
750 .create = p9_virtio_create,
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500751 .close = p9_virtio_close,
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500752 .request = p9_virtio_request,
Arnd Bergmanndc893e12013-03-08 12:43:31 -0800753 .zc_request = p9_virtio_zc_request,
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500754 .cancel = p9_virtio_cancel,
Tomas Bortoli728356d2018-08-14 19:43:42 +0200755 .cancelled = p9_virtio_cancelled,
Aneesh Kumar K.Vb49d8b52011-08-17 16:56:04 +0000756 /*
757 * We leave one entry for input and one entry for response
758 * headers. We also skip one more entry to accomodate, address
759 * that are not at page boundary, that can result in an extra
760 * page in zero copy.
761 */
762 .maxsize = PAGE_SIZE * (VIRTQUEUE_NUM - 3),
Eric Van Hensbergenf94741f2013-11-12 10:20:03 -0600763 .def = 1,
Tejun Heo72029fe2008-09-24 16:22:23 -0500764 .owner = THIS_MODULE,
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500765};
766
767/* The standard init function */
768static int __init p9_virtio_init(void)
769{
YueHaibingd4548542019-04-30 19:59:42 +0800770 int rc;
771
Aneesh Kumar K.V37c1209d42010-02-15 17:27:01 +0000772 INIT_LIST_HEAD(&virtio_chan_list);
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500773
774 v9fs_register_trans(&p9_virtio_trans);
YueHaibingd4548542019-04-30 19:59:42 +0800775 rc = register_virtio_driver(&p9_virtio_drv);
776 if (rc)
777 v9fs_unregister_trans(&p9_virtio_trans);
778
779 return rc;
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500780}
781
Eric Van Hensbergenf3933542008-02-06 19:25:04 -0600782static void __exit p9_virtio_cleanup(void)
783{
784 unregister_virtio_driver(&p9_virtio_drv);
Tejun Heo72029fe2008-09-24 16:22:23 -0500785 v9fs_unregister_trans(&p9_virtio_trans);
Eric Van Hensbergenf3933542008-02-06 19:25:04 -0600786}
787
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500788module_init(p9_virtio_init);
Eric Van Hensbergenf3933542008-02-06 19:25:04 -0600789module_exit(p9_virtio_cleanup);
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500790
791MODULE_DEVICE_TABLE(virtio, id_table);
792MODULE_AUTHOR("Eric Van Hensbergen <ericvh@gmail.com>");
793MODULE_DESCRIPTION("Virtio 9p Transport");
794MODULE_LICENSE("GPL");