blob: 10a7c0205440dc4c7e15c2df3e2727b27e9f9114 [file] [log] [blame]
Rusty Russell0a8a69d2007-10-22 11:03:40 +10001/* Virtio ring implementation.
2 *
3 * Copyright 2007 Rusty Russell IBM Corporation
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19#include <linux/virtio.h>
20#include <linux/virtio_ring.h>
Rusty Russelle34f8722008-07-25 12:06:13 -050021#include <linux/virtio_config.h>
Rusty Russell0a8a69d2007-10-22 11:03:40 +100022#include <linux/device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Paul Gortmakerb5a2c4f2011-07-03 16:20:30 -040024#include <linux/module.h>
Rusty Russelle93300b2012-01-12 15:44:43 +103025#include <linux/hrtimer.h>
Joel Stanley6abb2dd2014-02-13 15:03:46 +103026#include <linux/kmemleak.h>
Rusty Russell0a8a69d2007-10-22 11:03:40 +100027
28#ifdef DEBUG
29/* For development, we want to crash whenever the ring is screwed. */
Rusty Russell9499f5e2009-06-12 22:16:35 -060030#define BAD_RING(_vq, fmt, args...) \
31 do { \
32 dev_err(&(_vq)->vq.vdev->dev, \
33 "%s:"fmt, (_vq)->vq.name, ##args); \
34 BUG(); \
35 } while (0)
Rusty Russellc5f841f2009-03-30 21:55:22 -060036/* Caller is supposed to guarantee no reentry. */
37#define START_USE(_vq) \
38 do { \
39 if ((_vq)->in_use) \
Rusty Russell9499f5e2009-06-12 22:16:35 -060040 panic("%s:in_use = %i\n", \
41 (_vq)->vq.name, (_vq)->in_use); \
Rusty Russellc5f841f2009-03-30 21:55:22 -060042 (_vq)->in_use = __LINE__; \
Rusty Russell9499f5e2009-06-12 22:16:35 -060043 } while (0)
Roel Kluin3a35ce72009-01-22 16:42:57 +010044#define END_USE(_vq) \
Rusty Russell97a545a2010-02-24 14:22:22 -060045 do { BUG_ON(!(_vq)->in_use); (_vq)->in_use = 0; } while(0)
Rusty Russell0a8a69d2007-10-22 11:03:40 +100046#else
Rusty Russell9499f5e2009-06-12 22:16:35 -060047#define BAD_RING(_vq, fmt, args...) \
48 do { \
49 dev_err(&_vq->vq.vdev->dev, \
50 "%s:"fmt, (_vq)->vq.name, ##args); \
51 (_vq)->broken = true; \
52 } while (0)
Rusty Russell0a8a69d2007-10-22 11:03:40 +100053#define START_USE(vq)
54#define END_USE(vq)
55#endif
56
57struct vring_virtqueue
58{
59 struct virtqueue vq;
60
61 /* Actual memory layout for this queue */
62 struct vring vring;
63
Rusty Russell7b21e342012-01-12 15:44:42 +103064 /* Can we use weak barriers? */
65 bool weak_barriers;
66
Rusty Russell0a8a69d2007-10-22 11:03:40 +100067 /* Other side has made a mess, don't try any more. */
68 bool broken;
69
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +010070 /* Host supports indirect buffers */
71 bool indirect;
72
Michael S. Tsirkina5c262c2011-05-20 02:10:44 +030073 /* Host publishes avail event idx */
74 bool event;
75
Rusty Russell0a8a69d2007-10-22 11:03:40 +100076 /* Head of free buffer list. */
77 unsigned int free_head;
78 /* Number we've added since last sync. */
79 unsigned int num_added;
80
81 /* Last used index we've seen. */
Anthony Liguori1bc49532007-11-07 15:49:24 -060082 u16 last_used_idx;
Rusty Russell0a8a69d2007-10-22 11:03:40 +100083
84 /* How to notify other side. FIXME: commonalize hcalls! */
Heinz Graalfs46f9c2b2013-10-29 09:38:50 +103085 bool (*notify)(struct virtqueue *vq);
Rusty Russell0a8a69d2007-10-22 11:03:40 +100086
87#ifdef DEBUG
88 /* They're supposed to lock for us. */
89 unsigned int in_use;
Rusty Russelle93300b2012-01-12 15:44:43 +103090
91 /* Figure out if their kicks are too delayed. */
92 bool last_add_time_valid;
93 ktime_t last_add_time;
Rusty Russell0a8a69d2007-10-22 11:03:40 +100094#endif
95
96 /* Tokens for callbacks. */
97 void *data[];
98};
99
100#define to_vvq(_vq) container_of(_vq, struct vring_virtqueue, vq)
101
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100102/* Set up an indirect table of descriptors and add it to the queue. */
Rusty Russell13816c72013-03-20 15:37:09 +1030103static inline int vring_add_indirect(struct vring_virtqueue *vq,
104 struct scatterlist *sgs[],
Rusty Russell13816c72013-03-20 15:37:09 +1030105 unsigned int total_sg,
Rusty Russell13816c72013-03-20 15:37:09 +1030106 unsigned int out_sgs,
107 unsigned int in_sgs,
108 gfp_t gfp)
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100109{
110 struct vring_desc *desc;
111 unsigned head;
Rusty Russell13816c72013-03-20 15:37:09 +1030112 struct scatterlist *sg;
113 int i, n;
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100114
Will Deaconb92b1b82012-10-19 14:03:33 +0100115 /*
116 * We require lowmem mappings for the descriptors because
117 * otherwise virt_to_phys will give us bogus addresses in the
118 * virtqueue.
119 */
120 gfp &= ~(__GFP_HIGHMEM | __GFP_HIGH);
121
Rusty Russell13816c72013-03-20 15:37:09 +1030122 desc = kmalloc(total_sg * sizeof(struct vring_desc), gfp);
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100123 if (!desc)
Michael S. Tsirkin686d3632010-06-10 18:16:11 +0300124 return -ENOMEM;
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100125
Rusty Russell13816c72013-03-20 15:37:09 +1030126 /* Transfer entries from the sg lists into the indirect page */
127 i = 0;
128 for (n = 0; n < out_sgs; n++) {
Rusty Russelleeebf9b2014-09-11 10:17:37 +0930129 for (sg = sgs[n]; sg; sg = sg_next(sg)) {
Rusty Russell13816c72013-03-20 15:37:09 +1030130 desc[i].flags = VRING_DESC_F_NEXT;
131 desc[i].addr = sg_phys(sg);
132 desc[i].len = sg->length;
133 desc[i].next = i+1;
134 i++;
135 }
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100136 }
Rusty Russell13816c72013-03-20 15:37:09 +1030137 for (; n < (out_sgs + in_sgs); n++) {
Rusty Russelleeebf9b2014-09-11 10:17:37 +0930138 for (sg = sgs[n]; sg; sg = sg_next(sg)) {
Rusty Russell13816c72013-03-20 15:37:09 +1030139 desc[i].flags = VRING_DESC_F_NEXT|VRING_DESC_F_WRITE;
140 desc[i].addr = sg_phys(sg);
141 desc[i].len = sg->length;
142 desc[i].next = i+1;
143 i++;
144 }
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100145 }
Rusty Russell13816c72013-03-20 15:37:09 +1030146 BUG_ON(i != total_sg);
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100147
148 /* Last one doesn't continue. */
149 desc[i-1].flags &= ~VRING_DESC_F_NEXT;
150 desc[i-1].next = 0;
151
152 /* We're about to use a buffer */
Rusty Russell06ca2872012-10-16 23:56:14 +1030153 vq->vq.num_free--;
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100154
155 /* Use a single buffer which doesn't continue */
156 head = vq->free_head;
157 vq->vring.desc[head].flags = VRING_DESC_F_INDIRECT;
158 vq->vring.desc[head].addr = virt_to_phys(desc);
Rusty Russellbb478d82013-10-14 18:08:45 +1030159 /* kmemleak gives a false positive, as it's hidden by virt_to_phys */
160 kmemleak_ignore(desc);
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100161 vq->vring.desc[head].len = i * sizeof(struct vring_desc);
162
163 /* Update free pointer */
164 vq->free_head = vq->vring.desc[head].next;
165
166 return head;
167}
168
Rusty Russell13816c72013-03-20 15:37:09 +1030169static inline int virtqueue_add(struct virtqueue *_vq,
170 struct scatterlist *sgs[],
Rusty Russelleeebf9b2014-09-11 10:17:37 +0930171 unsigned int total_sg,
Rusty Russell13816c72013-03-20 15:37:09 +1030172 unsigned int out_sgs,
173 unsigned int in_sgs,
174 void *data,
175 gfp_t gfp)
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000176{
177 struct vring_virtqueue *vq = to_vvq(_vq);
Rusty Russell13816c72013-03-20 15:37:09 +1030178 struct scatterlist *sg;
Rusty Russelleeebf9b2014-09-11 10:17:37 +0930179 unsigned int i, n, avail, uninitialized_var(prev);
Michael S. Tsirkin1fe9b6f2010-07-26 16:55:30 +0930180 int head;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000181
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100182 START_USE(vq);
183
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000184 BUG_ON(data == NULL);
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100185
Rusty Russell70670444c22014-03-13 11:23:40 +1030186 if (unlikely(vq->broken)) {
187 END_USE(vq);
188 return -EIO;
189 }
190
Rusty Russelle93300b2012-01-12 15:44:43 +1030191#ifdef DEBUG
192 {
193 ktime_t now = ktime_get();
194
195 /* No kick or get, with .1 second between? Warn. */
196 if (vq->last_add_time_valid)
197 WARN_ON(ktime_to_ms(ktime_sub(now, vq->last_add_time))
198 > 100);
199 vq->last_add_time = now;
200 vq->last_add_time_valid = true;
201 }
202#endif
203
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100204 /* If the host supports indirect descriptor tables, and we have multiple
205 * buffers, then go indirect. FIXME: tune this threshold */
Rusty Russell13816c72013-03-20 15:37:09 +1030206 if (vq->indirect && total_sg > 1 && vq->vq.num_free) {
Rusty Russelleeebf9b2014-09-11 10:17:37 +0930207 head = vring_add_indirect(vq, sgs, total_sg,
Rusty Russell13816c72013-03-20 15:37:09 +1030208 out_sgs, in_sgs, gfp);
Michael S. Tsirkin1fe9b6f2010-07-26 16:55:30 +0930209 if (likely(head >= 0))
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100210 goto add_head;
211 }
212
Rusty Russell13816c72013-03-20 15:37:09 +1030213 BUG_ON(total_sg > vq->vring.num);
214 BUG_ON(total_sg == 0);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000215
Rusty Russell13816c72013-03-20 15:37:09 +1030216 if (vq->vq.num_free < total_sg) {
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000217 pr_debug("Can't add buf len %i - avail = %i\n",
Rusty Russell13816c72013-03-20 15:37:09 +1030218 total_sg, vq->vq.num_free);
Rusty Russell44653ea2008-07-25 12:06:04 -0500219 /* FIXME: for historical reasons, we force a notify here if
220 * there are outgoing parts to the buffer. Presumably the
221 * host should service the ring ASAP. */
Rusty Russell13816c72013-03-20 15:37:09 +1030222 if (out_sgs)
Rusty Russell44653ea2008-07-25 12:06:04 -0500223 vq->notify(&vq->vq);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000224 END_USE(vq);
225 return -ENOSPC;
226 }
227
228 /* We're about to use some buffers from the free list. */
Rusty Russell13816c72013-03-20 15:37:09 +1030229 vq->vq.num_free -= total_sg;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000230
Rusty Russell13816c72013-03-20 15:37:09 +1030231 head = i = vq->free_head;
232 for (n = 0; n < out_sgs; n++) {
Rusty Russelleeebf9b2014-09-11 10:17:37 +0930233 for (sg = sgs[n]; sg; sg = sg_next(sg)) {
Rusty Russell13816c72013-03-20 15:37:09 +1030234 vq->vring.desc[i].flags = VRING_DESC_F_NEXT;
235 vq->vring.desc[i].addr = sg_phys(sg);
236 vq->vring.desc[i].len = sg->length;
237 prev = i;
238 i = vq->vring.desc[i].next;
239 }
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000240 }
Rusty Russell13816c72013-03-20 15:37:09 +1030241 for (; n < (out_sgs + in_sgs); n++) {
Rusty Russelleeebf9b2014-09-11 10:17:37 +0930242 for (sg = sgs[n]; sg; sg = sg_next(sg)) {
Rusty Russell13816c72013-03-20 15:37:09 +1030243 vq->vring.desc[i].flags = VRING_DESC_F_NEXT|VRING_DESC_F_WRITE;
244 vq->vring.desc[i].addr = sg_phys(sg);
245 vq->vring.desc[i].len = sg->length;
246 prev = i;
247 i = vq->vring.desc[i].next;
248 }
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000249 }
250 /* Last one doesn't continue. */
251 vq->vring.desc[prev].flags &= ~VRING_DESC_F_NEXT;
252
253 /* Update free pointer */
254 vq->free_head = i;
255
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100256add_head:
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000257 /* Set token. */
258 vq->data[head] = data;
259
260 /* Put entry in available array (but don't update avail->idx until they
Rusty Russell3b720b82012-01-12 15:44:43 +1030261 * do sync). */
Rusty Russellee7cd892012-01-12 15:44:43 +1030262 avail = (vq->vring.avail->idx & (vq->vring.num-1));
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000263 vq->vring.avail->ring[avail] = head;
264
Rusty Russellee7cd892012-01-12 15:44:43 +1030265 /* Descriptors and available array need to be set before we expose the
266 * new available array entries. */
Rusty Russella9a0fef2013-03-18 13:22:19 +1030267 virtio_wmb(vq->weak_barriers);
Rusty Russellee7cd892012-01-12 15:44:43 +1030268 vq->vring.avail->idx++;
269 vq->num_added++;
270
271 /* This is very unlikely, but theoretically possible. Kick
272 * just in case. */
273 if (unlikely(vq->num_added == (1 << 16) - 1))
274 virtqueue_kick(_vq);
275
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000276 pr_debug("Added buffer head %i to %p\n", head, vq);
277 END_USE(vq);
Rusty Russell3c1b27d2009-09-23 22:26:31 -0600278
Rusty Russell98e8c6b2012-10-16 23:56:15 +1030279 return 0;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000280}
Rusty Russell13816c72013-03-20 15:37:09 +1030281
282/**
Rusty Russell13816c72013-03-20 15:37:09 +1030283 * virtqueue_add_sgs - expose buffers to other end
284 * @vq: the struct virtqueue we're talking about.
285 * @sgs: array of terminated scatterlists.
286 * @out_num: the number of scatterlists readable by other side
287 * @in_num: the number of scatterlists which are writable (after readable ones)
288 * @data: the token identifying the buffer.
289 * @gfp: how to do memory allocations (if necessary).
290 *
291 * Caller must ensure we don't call this with other virtqueue operations
292 * at the same time (except where noted).
293 *
Rusty Russell70670444c22014-03-13 11:23:40 +1030294 * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO).
Rusty Russell13816c72013-03-20 15:37:09 +1030295 */
296int virtqueue_add_sgs(struct virtqueue *_vq,
297 struct scatterlist *sgs[],
298 unsigned int out_sgs,
299 unsigned int in_sgs,
300 void *data,
301 gfp_t gfp)
302{
Rusty Russelleeebf9b2014-09-11 10:17:37 +0930303 unsigned int i, total_sg = 0;
Rusty Russell13816c72013-03-20 15:37:09 +1030304
305 /* Count them first. */
Rusty Russelleeebf9b2014-09-11 10:17:37 +0930306 for (i = 0; i < out_sgs + in_sgs; i++) {
Rusty Russell13816c72013-03-20 15:37:09 +1030307 struct scatterlist *sg;
308 for (sg = sgs[i]; sg; sg = sg_next(sg))
Rusty Russelleeebf9b2014-09-11 10:17:37 +0930309 total_sg++;
Rusty Russell13816c72013-03-20 15:37:09 +1030310 }
Rusty Russelleeebf9b2014-09-11 10:17:37 +0930311 return virtqueue_add(_vq, sgs, total_sg, out_sgs, in_sgs, data, gfp);
Rusty Russell13816c72013-03-20 15:37:09 +1030312}
313EXPORT_SYMBOL_GPL(virtqueue_add_sgs);
314
315/**
Rusty Russell282edb32013-03-20 15:44:26 +1030316 * virtqueue_add_outbuf - expose output buffers to other end
317 * @vq: the struct virtqueue we're talking about.
Rusty Russelleeebf9b2014-09-11 10:17:37 +0930318 * @sg: scatterlist (must be well-formed and terminated!)
319 * @num: the number of entries in @sg readable by other side
Rusty Russell282edb32013-03-20 15:44:26 +1030320 * @data: the token identifying the buffer.
321 * @gfp: how to do memory allocations (if necessary).
322 *
323 * Caller must ensure we don't call this with other virtqueue operations
324 * at the same time (except where noted).
325 *
Rusty Russell70670444c22014-03-13 11:23:40 +1030326 * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO).
Rusty Russell282edb32013-03-20 15:44:26 +1030327 */
328int virtqueue_add_outbuf(struct virtqueue *vq,
Rusty Russelleeebf9b2014-09-11 10:17:37 +0930329 struct scatterlist *sg, unsigned int num,
Rusty Russell282edb32013-03-20 15:44:26 +1030330 void *data,
331 gfp_t gfp)
332{
Rusty Russelleeebf9b2014-09-11 10:17:37 +0930333 return virtqueue_add(vq, &sg, num, 1, 0, data, gfp);
Rusty Russell282edb32013-03-20 15:44:26 +1030334}
335EXPORT_SYMBOL_GPL(virtqueue_add_outbuf);
336
337/**
338 * virtqueue_add_inbuf - expose input buffers to other end
339 * @vq: the struct virtqueue we're talking about.
Rusty Russelleeebf9b2014-09-11 10:17:37 +0930340 * @sg: scatterlist (must be well-formed and terminated!)
341 * @num: the number of entries in @sg writable by other side
Rusty Russell282edb32013-03-20 15:44:26 +1030342 * @data: the token identifying the buffer.
343 * @gfp: how to do memory allocations (if necessary).
344 *
345 * Caller must ensure we don't call this with other virtqueue operations
346 * at the same time (except where noted).
347 *
Rusty Russell70670444c22014-03-13 11:23:40 +1030348 * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO).
Rusty Russell282edb32013-03-20 15:44:26 +1030349 */
350int virtqueue_add_inbuf(struct virtqueue *vq,
Rusty Russelleeebf9b2014-09-11 10:17:37 +0930351 struct scatterlist *sg, unsigned int num,
Rusty Russell282edb32013-03-20 15:44:26 +1030352 void *data,
353 gfp_t gfp)
354{
Rusty Russelleeebf9b2014-09-11 10:17:37 +0930355 return virtqueue_add(vq, &sg, num, 0, 1, data, gfp);
Rusty Russell282edb32013-03-20 15:44:26 +1030356}
357EXPORT_SYMBOL_GPL(virtqueue_add_inbuf);
358
359/**
Rusty Russell41f03772012-01-12 15:44:43 +1030360 * virtqueue_kick_prepare - first half of split virtqueue_kick call.
Rusty Russell5dfc1762012-01-12 15:44:42 +1030361 * @vq: the struct virtqueue
362 *
Rusty Russell41f03772012-01-12 15:44:43 +1030363 * Instead of virtqueue_kick(), you can do:
364 * if (virtqueue_kick_prepare(vq))
365 * virtqueue_notify(vq);
Rusty Russell5dfc1762012-01-12 15:44:42 +1030366 *
Rusty Russell41f03772012-01-12 15:44:43 +1030367 * This is sometimes useful because the virtqueue_kick_prepare() needs
368 * to be serialized, but the actual virtqueue_notify() call does not.
Rusty Russell5dfc1762012-01-12 15:44:42 +1030369 */
Rusty Russell41f03772012-01-12 15:44:43 +1030370bool virtqueue_kick_prepare(struct virtqueue *_vq)
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000371{
372 struct vring_virtqueue *vq = to_vvq(_vq);
Michael S. Tsirkina5c262c2011-05-20 02:10:44 +0300373 u16 new, old;
Rusty Russell41f03772012-01-12 15:44:43 +1030374 bool needs_kick;
375
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000376 START_USE(vq);
Jason Wanga72caae2012-01-20 16:17:08 +0800377 /* We need to expose available array entries before checking avail
378 * event. */
Rusty Russella9a0fef2013-03-18 13:22:19 +1030379 virtio_mb(vq->weak_barriers);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000380
Rusty Russellee7cd892012-01-12 15:44:43 +1030381 old = vq->vring.avail->idx - vq->num_added;
382 new = vq->vring.avail->idx;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000383 vq->num_added = 0;
384
Rusty Russelle93300b2012-01-12 15:44:43 +1030385#ifdef DEBUG
386 if (vq->last_add_time_valid) {
387 WARN_ON(ktime_to_ms(ktime_sub(ktime_get(),
388 vq->last_add_time)) > 100);
389 }
390 vq->last_add_time_valid = false;
391#endif
392
Rusty Russell41f03772012-01-12 15:44:43 +1030393 if (vq->event) {
394 needs_kick = vring_need_event(vring_avail_event(&vq->vring),
395 new, old);
396 } else {
397 needs_kick = !(vq->vring.used->flags & VRING_USED_F_NO_NOTIFY);
398 }
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000399 END_USE(vq);
Rusty Russell41f03772012-01-12 15:44:43 +1030400 return needs_kick;
401}
402EXPORT_SYMBOL_GPL(virtqueue_kick_prepare);
403
404/**
405 * virtqueue_notify - second half of split virtqueue_kick call.
406 * @vq: the struct virtqueue
407 *
408 * This does not need to be serialized.
Heinz Graalfs5b1bf7c2013-10-29 09:39:48 +1030409 *
410 * Returns false if host notify failed or queue is broken, otherwise true.
Rusty Russell41f03772012-01-12 15:44:43 +1030411 */
Heinz Graalfs5b1bf7c2013-10-29 09:39:48 +1030412bool virtqueue_notify(struct virtqueue *_vq)
Rusty Russell41f03772012-01-12 15:44:43 +1030413{
414 struct vring_virtqueue *vq = to_vvq(_vq);
415
Heinz Graalfs5b1bf7c2013-10-29 09:39:48 +1030416 if (unlikely(vq->broken))
417 return false;
418
Rusty Russell41f03772012-01-12 15:44:43 +1030419 /* Prod other side to tell it about changes. */
Heinz Graalfs2342d6a2013-11-05 21:20:27 +1030420 if (!vq->notify(_vq)) {
Heinz Graalfs5b1bf7c2013-10-29 09:39:48 +1030421 vq->broken = true;
422 return false;
423 }
424 return true;
Rusty Russell41f03772012-01-12 15:44:43 +1030425}
426EXPORT_SYMBOL_GPL(virtqueue_notify);
427
428/**
429 * virtqueue_kick - update after add_buf
430 * @vq: the struct virtqueue
431 *
Rusty Russellb3087e42013-05-20 12:15:44 +0930432 * After one or more virtqueue_add_* calls, invoke this to kick
Rusty Russell41f03772012-01-12 15:44:43 +1030433 * the other side.
434 *
435 * Caller must ensure we don't call this with other virtqueue
436 * operations at the same time (except where noted).
Heinz Graalfs5b1bf7c2013-10-29 09:39:48 +1030437 *
438 * Returns false if kick failed, otherwise true.
Rusty Russell41f03772012-01-12 15:44:43 +1030439 */
Heinz Graalfs5b1bf7c2013-10-29 09:39:48 +1030440bool virtqueue_kick(struct virtqueue *vq)
Rusty Russell41f03772012-01-12 15:44:43 +1030441{
442 if (virtqueue_kick_prepare(vq))
Heinz Graalfs5b1bf7c2013-10-29 09:39:48 +1030443 return virtqueue_notify(vq);
444 return true;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000445}
Michael S. Tsirkin7c5e9ed2010-04-12 16:19:07 +0300446EXPORT_SYMBOL_GPL(virtqueue_kick);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000447
448static void detach_buf(struct vring_virtqueue *vq, unsigned int head)
449{
450 unsigned int i;
451
452 /* Clear data ptr. */
453 vq->data[head] = NULL;
454
455 /* Put back on free list: find end */
456 i = head;
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100457
458 /* Free the indirect table */
459 if (vq->vring.desc[i].flags & VRING_DESC_F_INDIRECT)
460 kfree(phys_to_virt(vq->vring.desc[i].addr));
461
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000462 while (vq->vring.desc[i].flags & VRING_DESC_F_NEXT) {
463 i = vq->vring.desc[i].next;
Rusty Russell06ca2872012-10-16 23:56:14 +1030464 vq->vq.num_free++;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000465 }
466
467 vq->vring.desc[i].next = vq->free_head;
468 vq->free_head = head;
469 /* Plus final descriptor */
Rusty Russell06ca2872012-10-16 23:56:14 +1030470 vq->vq.num_free++;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000471}
472
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000473static inline bool more_used(const struct vring_virtqueue *vq)
474{
475 return vq->last_used_idx != vq->vring.used->idx;
476}
477
Rusty Russell5dfc1762012-01-12 15:44:42 +1030478/**
479 * virtqueue_get_buf - get the next used buffer
480 * @vq: the struct virtqueue we're talking about.
481 * @len: the length written into the buffer
482 *
483 * If the driver wrote data into the buffer, @len will be set to the
484 * amount written. This means you don't need to clear the buffer
485 * beforehand to ensure there's no data leakage in the case of short
486 * writes.
487 *
488 * Caller must ensure we don't call this with other virtqueue
489 * operations at the same time (except where noted).
490 *
491 * Returns NULL if there are no used buffers, or the "data" token
Rusty Russellb3087e42013-05-20 12:15:44 +0930492 * handed to virtqueue_add_*().
Rusty Russell5dfc1762012-01-12 15:44:42 +1030493 */
Michael S. Tsirkin7c5e9ed2010-04-12 16:19:07 +0300494void *virtqueue_get_buf(struct virtqueue *_vq, unsigned int *len)
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000495{
496 struct vring_virtqueue *vq = to_vvq(_vq);
497 void *ret;
498 unsigned int i;
Rusty Russell3b720b82012-01-12 15:44:43 +1030499 u16 last_used;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000500
501 START_USE(vq);
502
Rusty Russell5ef82752008-05-02 21:50:43 -0500503 if (unlikely(vq->broken)) {
504 END_USE(vq);
505 return NULL;
506 }
507
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000508 if (!more_used(vq)) {
509 pr_debug("No more buffers in queue\n");
510 END_USE(vq);
511 return NULL;
512 }
513
Michael S. Tsirkin2d61ba92009-10-25 15:28:53 +0200514 /* Only get used array entries after they have been exposed by host. */
Rusty Russella9a0fef2013-03-18 13:22:19 +1030515 virtio_rmb(vq->weak_barriers);
Michael S. Tsirkin2d61ba92009-10-25 15:28:53 +0200516
Rusty Russell3b720b82012-01-12 15:44:43 +1030517 last_used = (vq->last_used_idx & (vq->vring.num - 1));
518 i = vq->vring.used->ring[last_used].id;
519 *len = vq->vring.used->ring[last_used].len;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000520
521 if (unlikely(i >= vq->vring.num)) {
522 BAD_RING(vq, "id %u out of range\n", i);
523 return NULL;
524 }
525 if (unlikely(!vq->data[i])) {
526 BAD_RING(vq, "id %u is not a head!\n", i);
527 return NULL;
528 }
529
530 /* detach_buf clears data, so grab it now. */
531 ret = vq->data[i];
532 detach_buf(vq, i);
533 vq->last_used_idx++;
Michael S. Tsirkina5c262c2011-05-20 02:10:44 +0300534 /* If we expect an interrupt for the next entry, tell host
535 * by writing event index and flush out the write before
536 * the read in the next get_buf call. */
537 if (!(vq->vring.avail->flags & VRING_AVAIL_F_NO_INTERRUPT)) {
538 vring_used_event(&vq->vring) = vq->last_used_idx;
Rusty Russella9a0fef2013-03-18 13:22:19 +1030539 virtio_mb(vq->weak_barriers);
Michael S. Tsirkina5c262c2011-05-20 02:10:44 +0300540 }
541
Rusty Russelle93300b2012-01-12 15:44:43 +1030542#ifdef DEBUG
543 vq->last_add_time_valid = false;
544#endif
545
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000546 END_USE(vq);
547 return ret;
548}
Michael S. Tsirkin7c5e9ed2010-04-12 16:19:07 +0300549EXPORT_SYMBOL_GPL(virtqueue_get_buf);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000550
Rusty Russell5dfc1762012-01-12 15:44:42 +1030551/**
552 * virtqueue_disable_cb - disable callbacks
553 * @vq: the struct virtqueue we're talking about.
554 *
555 * Note that this is not necessarily synchronous, hence unreliable and only
556 * useful as an optimization.
557 *
558 * Unlike other operations, this need not be serialized.
559 */
Michael S. Tsirkin7c5e9ed2010-04-12 16:19:07 +0300560void virtqueue_disable_cb(struct virtqueue *_vq)
Rusty Russell18445c42008-02-04 23:49:57 -0500561{
562 struct vring_virtqueue *vq = to_vvq(_vq);
563
Rusty Russell18445c42008-02-04 23:49:57 -0500564 vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
Rusty Russell18445c42008-02-04 23:49:57 -0500565}
Michael S. Tsirkin7c5e9ed2010-04-12 16:19:07 +0300566EXPORT_SYMBOL_GPL(virtqueue_disable_cb);
Rusty Russell18445c42008-02-04 23:49:57 -0500567
Rusty Russell5dfc1762012-01-12 15:44:42 +1030568/**
Michael S. Tsirkincc229882013-07-09 13:19:18 +0300569 * virtqueue_enable_cb_prepare - restart callbacks after disable_cb
570 * @vq: the struct virtqueue we're talking about.
571 *
572 * This re-enables callbacks; it returns current queue state
573 * in an opaque unsigned value. This value should be later tested by
574 * virtqueue_poll, to detect a possible race between the driver checking for
575 * more work, and enabling callbacks.
576 *
577 * Caller must ensure we don't call this with other virtqueue
578 * operations at the same time (except where noted).
579 */
580unsigned virtqueue_enable_cb_prepare(struct virtqueue *_vq)
581{
582 struct vring_virtqueue *vq = to_vvq(_vq);
583 u16 last_used_idx;
584
585 START_USE(vq);
586
587 /* We optimistically turn back on interrupts, then check if there was
588 * more to do. */
589 /* Depending on the VIRTIO_RING_F_EVENT_IDX feature, we need to
590 * either clear the flags bit or point the event index at the next
591 * entry. Always do both to keep code simple. */
592 vq->vring.avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT;
593 vring_used_event(&vq->vring) = last_used_idx = vq->last_used_idx;
594 END_USE(vq);
595 return last_used_idx;
596}
597EXPORT_SYMBOL_GPL(virtqueue_enable_cb_prepare);
598
599/**
600 * virtqueue_poll - query pending used buffers
601 * @vq: the struct virtqueue we're talking about.
602 * @last_used_idx: virtqueue state (from call to virtqueue_enable_cb_prepare).
603 *
604 * Returns "true" if there are pending used buffers in the queue.
605 *
606 * This does not need to be serialized.
607 */
608bool virtqueue_poll(struct virtqueue *_vq, unsigned last_used_idx)
609{
610 struct vring_virtqueue *vq = to_vvq(_vq);
611
612 virtio_mb(vq->weak_barriers);
613 return (u16)last_used_idx != vq->vring.used->idx;
614}
615EXPORT_SYMBOL_GPL(virtqueue_poll);
616
617/**
Rusty Russell5dfc1762012-01-12 15:44:42 +1030618 * virtqueue_enable_cb - restart callbacks after disable_cb.
619 * @vq: the struct virtqueue we're talking about.
620 *
621 * This re-enables callbacks; it returns "false" if there are pending
622 * buffers in the queue, to detect a possible race between the driver
623 * checking for more work, and enabling callbacks.
624 *
625 * Caller must ensure we don't call this with other virtqueue
626 * operations at the same time (except where noted).
627 */
Michael S. Tsirkin7c5e9ed2010-04-12 16:19:07 +0300628bool virtqueue_enable_cb(struct virtqueue *_vq)
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000629{
Michael S. Tsirkincc229882013-07-09 13:19:18 +0300630 unsigned last_used_idx = virtqueue_enable_cb_prepare(_vq);
631 return !virtqueue_poll(_vq, last_used_idx);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000632}
Michael S. Tsirkin7c5e9ed2010-04-12 16:19:07 +0300633EXPORT_SYMBOL_GPL(virtqueue_enable_cb);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000634
Rusty Russell5dfc1762012-01-12 15:44:42 +1030635/**
636 * virtqueue_enable_cb_delayed - restart callbacks after disable_cb.
637 * @vq: the struct virtqueue we're talking about.
638 *
639 * This re-enables callbacks but hints to the other side to delay
640 * interrupts until most of the available buffers have been processed;
641 * it returns "false" if there are many pending buffers in the queue,
642 * to detect a possible race between the driver checking for more work,
643 * and enabling callbacks.
644 *
645 * Caller must ensure we don't call this with other virtqueue
646 * operations at the same time (except where noted).
647 */
Michael S. Tsirkin7ab358c2011-05-20 02:11:14 +0300648bool virtqueue_enable_cb_delayed(struct virtqueue *_vq)
649{
650 struct vring_virtqueue *vq = to_vvq(_vq);
651 u16 bufs;
652
653 START_USE(vq);
654
655 /* We optimistically turn back on interrupts, then check if there was
656 * more to do. */
657 /* Depending on the VIRTIO_RING_F_USED_EVENT_IDX feature, we need to
658 * either clear the flags bit or point the event index at the next
659 * entry. Always do both to keep code simple. */
660 vq->vring.avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT;
661 /* TODO: tune this threshold */
662 bufs = (u16)(vq->vring.avail->idx - vq->last_used_idx) * 3 / 4;
663 vring_used_event(&vq->vring) = vq->last_used_idx + bufs;
Rusty Russella9a0fef2013-03-18 13:22:19 +1030664 virtio_mb(vq->weak_barriers);
Michael S. Tsirkin7ab358c2011-05-20 02:11:14 +0300665 if (unlikely((u16)(vq->vring.used->idx - vq->last_used_idx) > bufs)) {
666 END_USE(vq);
667 return false;
668 }
669
670 END_USE(vq);
671 return true;
672}
673EXPORT_SYMBOL_GPL(virtqueue_enable_cb_delayed);
674
Rusty Russell5dfc1762012-01-12 15:44:42 +1030675/**
676 * virtqueue_detach_unused_buf - detach first unused buffer
677 * @vq: the struct virtqueue we're talking about.
678 *
Rusty Russellb3087e42013-05-20 12:15:44 +0930679 * Returns NULL or the "data" token handed to virtqueue_add_*().
Rusty Russell5dfc1762012-01-12 15:44:42 +1030680 * This is not valid on an active queue; it is useful only for device
681 * shutdown.
682 */
Michael S. Tsirkin7c5e9ed2010-04-12 16:19:07 +0300683void *virtqueue_detach_unused_buf(struct virtqueue *_vq)
Shirley Mac021eac2010-01-18 19:15:23 +0530684{
685 struct vring_virtqueue *vq = to_vvq(_vq);
686 unsigned int i;
687 void *buf;
688
689 START_USE(vq);
690
691 for (i = 0; i < vq->vring.num; i++) {
692 if (!vq->data[i])
693 continue;
694 /* detach_buf clears data, so grab it now. */
695 buf = vq->data[i];
696 detach_buf(vq, i);
Amit Shahb3258ff2011-03-16 19:12:10 +0530697 vq->vring.avail->idx--;
Shirley Mac021eac2010-01-18 19:15:23 +0530698 END_USE(vq);
699 return buf;
700 }
701 /* That should have freed everything. */
Rusty Russell06ca2872012-10-16 23:56:14 +1030702 BUG_ON(vq->vq.num_free != vq->vring.num);
Shirley Mac021eac2010-01-18 19:15:23 +0530703
704 END_USE(vq);
705 return NULL;
706}
Michael S. Tsirkin7c5e9ed2010-04-12 16:19:07 +0300707EXPORT_SYMBOL_GPL(virtqueue_detach_unused_buf);
Shirley Mac021eac2010-01-18 19:15:23 +0530708
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000709irqreturn_t vring_interrupt(int irq, void *_vq)
710{
711 struct vring_virtqueue *vq = to_vvq(_vq);
712
713 if (!more_used(vq)) {
714 pr_debug("virtqueue interrupt with no work for %p\n", vq);
715 return IRQ_NONE;
716 }
717
718 if (unlikely(vq->broken))
719 return IRQ_HANDLED;
720
721 pr_debug("virtqueue callback for %p (%p)\n", vq, vq->vq.callback);
Rusty Russell18445c42008-02-04 23:49:57 -0500722 if (vq->vq.callback)
723 vq->vq.callback(&vq->vq);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000724
725 return IRQ_HANDLED;
726}
Rusty Russellc6fd4702008-02-04 23:50:05 -0500727EXPORT_SYMBOL_GPL(vring_interrupt);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000728
Jason Wang17bb6d42012-08-28 13:54:13 +0200729struct virtqueue *vring_new_virtqueue(unsigned int index,
730 unsigned int num,
Rusty Russell87c7d572008-12-30 09:26:03 -0600731 unsigned int vring_align,
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000732 struct virtio_device *vdev,
Rusty Russell7b21e342012-01-12 15:44:42 +1030733 bool weak_barriers,
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000734 void *pages,
Heinz Graalfs46f9c2b2013-10-29 09:38:50 +1030735 bool (*notify)(struct virtqueue *),
Rusty Russell9499f5e2009-06-12 22:16:35 -0600736 void (*callback)(struct virtqueue *),
737 const char *name)
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000738{
739 struct vring_virtqueue *vq;
740 unsigned int i;
741
Rusty Russell42b36cc2007-11-12 13:39:18 +1100742 /* We assume num is a power of 2. */
743 if (num & (num - 1)) {
744 dev_warn(&vdev->dev, "Bad virtqueue length %u\n", num);
745 return NULL;
746 }
747
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000748 vq = kmalloc(sizeof(*vq) + sizeof(void *)*num, GFP_KERNEL);
749 if (!vq)
750 return NULL;
751
Rusty Russell87c7d572008-12-30 09:26:03 -0600752 vring_init(&vq->vring, num, pages, vring_align);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000753 vq->vq.callback = callback;
754 vq->vq.vdev = vdev;
Rusty Russell9499f5e2009-06-12 22:16:35 -0600755 vq->vq.name = name;
Rusty Russell06ca2872012-10-16 23:56:14 +1030756 vq->vq.num_free = num;
757 vq->vq.index = index;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000758 vq->notify = notify;
Rusty Russell7b21e342012-01-12 15:44:42 +1030759 vq->weak_barriers = weak_barriers;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000760 vq->broken = false;
761 vq->last_used_idx = 0;
762 vq->num_added = 0;
Rusty Russell9499f5e2009-06-12 22:16:35 -0600763 list_add_tail(&vq->vq.list, &vdev->vqs);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000764#ifdef DEBUG
765 vq->in_use = false;
Rusty Russelle93300b2012-01-12 15:44:43 +1030766 vq->last_add_time_valid = false;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000767#endif
768
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100769 vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC);
Michael S. Tsirkina5c262c2011-05-20 02:10:44 +0300770 vq->event = virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX);
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100771
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000772 /* No callback? Tell other side not to bother us. */
773 if (!callback)
774 vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
775
776 /* Put everything in free lists. */
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000777 vq->free_head = 0;
Amit Shah3b870622010-02-12 10:32:14 +0530778 for (i = 0; i < num-1; i++) {
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000779 vq->vring.desc[i].next = i+1;
Amit Shah3b870622010-02-12 10:32:14 +0530780 vq->data[i] = NULL;
781 }
782 vq->data[i] = NULL;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000783
784 return &vq->vq;
785}
Rusty Russellc6fd4702008-02-04 23:50:05 -0500786EXPORT_SYMBOL_GPL(vring_new_virtqueue);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000787
788void vring_del_virtqueue(struct virtqueue *vq)
789{
Rusty Russell9499f5e2009-06-12 22:16:35 -0600790 list_del(&vq->list);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000791 kfree(to_vvq(vq));
792}
Rusty Russellc6fd4702008-02-04 23:50:05 -0500793EXPORT_SYMBOL_GPL(vring_del_virtqueue);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000794
Rusty Russelle34f8722008-07-25 12:06:13 -0500795/* Manipulates transport-specific feature bits. */
796void vring_transport_features(struct virtio_device *vdev)
797{
798 unsigned int i;
799
800 for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++) {
801 switch (i) {
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100802 case VIRTIO_RING_F_INDIRECT_DESC:
803 break;
Michael S. Tsirkina5c262c2011-05-20 02:10:44 +0300804 case VIRTIO_RING_F_EVENT_IDX:
805 break;
Rusty Russelle34f8722008-07-25 12:06:13 -0500806 default:
807 /* We don't understand this bit. */
808 clear_bit(i, vdev->features);
809 }
810 }
811}
812EXPORT_SYMBOL_GPL(vring_transport_features);
813
Rusty Russell5dfc1762012-01-12 15:44:42 +1030814/**
815 * virtqueue_get_vring_size - return the size of the virtqueue's vring
816 * @vq: the struct virtqueue containing the vring of interest.
817 *
818 * Returns the size of the vring. This is mainly used for boasting to
819 * userspace. Unlike other operations, this need not be serialized.
820 */
Rick Jones8f9f4662011-10-19 08:10:59 +0000821unsigned int virtqueue_get_vring_size(struct virtqueue *_vq)
822{
823
824 struct vring_virtqueue *vq = to_vvq(_vq);
825
826 return vq->vring.num;
827}
828EXPORT_SYMBOL_GPL(virtqueue_get_vring_size);
829
Heinz Graalfsb3b32c92013-10-29 09:40:19 +1030830bool virtqueue_is_broken(struct virtqueue *_vq)
831{
832 struct vring_virtqueue *vq = to_vvq(_vq);
833
834 return vq->broken;
835}
836EXPORT_SYMBOL_GPL(virtqueue_is_broken);
837
Rusty Russelle2dcdfe2014-04-28 11:15:08 +0930838/*
839 * This should prevent the device from being used, allowing drivers to
840 * recover. You may need to grab appropriate locks to flush.
841 */
842void virtio_break_device(struct virtio_device *dev)
843{
844 struct virtqueue *_vq;
845
846 list_for_each_entry(_vq, &dev->vqs, list) {
847 struct vring_virtqueue *vq = to_vvq(_vq);
848 vq->broken = true;
849 }
850}
851EXPORT_SYMBOL_GPL(virtio_break_device);
852
Rusty Russellc6fd4702008-02-04 23:50:05 -0500853MODULE_LICENSE("GPL");