blob: e46d08107a50d855bed2b9e14878a720a23f5381 [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>
Andy Lutomirski780bc792016-02-02 21:46:36 -080027#include <linux/dma-mapping.h>
Rusty Russell0a8a69d2007-10-22 11:03:40 +100028
29#ifdef DEBUG
30/* For development, we want to crash whenever the ring is screwed. */
Rusty Russell9499f5e2009-06-12 22:16:35 -060031#define BAD_RING(_vq, fmt, args...) \
32 do { \
33 dev_err(&(_vq)->vq.vdev->dev, \
34 "%s:"fmt, (_vq)->vq.name, ##args); \
35 BUG(); \
36 } while (0)
Rusty Russellc5f841f2009-03-30 21:55:22 -060037/* Caller is supposed to guarantee no reentry. */
38#define START_USE(_vq) \
39 do { \
40 if ((_vq)->in_use) \
Rusty Russell9499f5e2009-06-12 22:16:35 -060041 panic("%s:in_use = %i\n", \
42 (_vq)->vq.name, (_vq)->in_use); \
Rusty Russellc5f841f2009-03-30 21:55:22 -060043 (_vq)->in_use = __LINE__; \
Rusty Russell9499f5e2009-06-12 22:16:35 -060044 } while (0)
Roel Kluin3a35ce72009-01-22 16:42:57 +010045#define END_USE(_vq) \
Rusty Russell97a545a2010-02-24 14:22:22 -060046 do { BUG_ON(!(_vq)->in_use); (_vq)->in_use = 0; } while(0)
Rusty Russell0a8a69d2007-10-22 11:03:40 +100047#else
Rusty Russell9499f5e2009-06-12 22:16:35 -060048#define BAD_RING(_vq, fmt, args...) \
49 do { \
50 dev_err(&_vq->vq.vdev->dev, \
51 "%s:"fmt, (_vq)->vq.name, ##args); \
52 (_vq)->broken = true; \
53 } while (0)
Rusty Russell0a8a69d2007-10-22 11:03:40 +100054#define START_USE(vq)
55#define END_USE(vq)
56#endif
57
Andy Lutomirski780bc792016-02-02 21:46:36 -080058struct vring_desc_state {
59 void *data; /* Data for callback. */
60 struct vring_desc *indir_desc; /* Indirect descriptor, if any. */
61};
62
Michael S. Tsirkin43b4f722015-01-15 13:33:31 +020063struct vring_virtqueue {
Rusty Russell0a8a69d2007-10-22 11:03:40 +100064 struct virtqueue vq;
65
66 /* Actual memory layout for this queue */
67 struct vring vring;
68
Rusty Russell7b21e342012-01-12 15:44:42 +103069 /* Can we use weak barriers? */
70 bool weak_barriers;
71
Rusty Russell0a8a69d2007-10-22 11:03:40 +100072 /* Other side has made a mess, don't try any more. */
73 bool broken;
74
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +010075 /* Host supports indirect buffers */
76 bool indirect;
77
Michael S. Tsirkina5c262c2011-05-20 02:10:44 +030078 /* Host publishes avail event idx */
79 bool event;
80
Rusty Russell0a8a69d2007-10-22 11:03:40 +100081 /* Head of free buffer list. */
82 unsigned int free_head;
83 /* Number we've added since last sync. */
84 unsigned int num_added;
85
86 /* Last used index we've seen. */
Anthony Liguori1bc49532007-11-07 15:49:24 -060087 u16 last_used_idx;
Rusty Russell0a8a69d2007-10-22 11:03:40 +100088
Venkatesh Srinivasf277ec42015-11-10 16:21:07 -080089 /* Last written value to avail->flags */
90 u16 avail_flags_shadow;
91
92 /* Last written value to avail->idx in guest byte order */
93 u16 avail_idx_shadow;
94
Rusty Russell0a8a69d2007-10-22 11:03:40 +100095 /* How to notify other side. FIXME: commonalize hcalls! */
Heinz Graalfs46f9c2b2013-10-29 09:38:50 +103096 bool (*notify)(struct virtqueue *vq);
Rusty Russell0a8a69d2007-10-22 11:03:40 +100097
Andy Lutomirski2a2d1382016-02-02 21:46:37 -080098 /* DMA, allocation, and size information */
99 bool we_own_ring;
100 size_t queue_size_in_bytes;
101 dma_addr_t queue_dma_addr;
102
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000103#ifdef DEBUG
104 /* They're supposed to lock for us. */
105 unsigned int in_use;
Rusty Russelle93300b2012-01-12 15:44:43 +1030106
107 /* Figure out if their kicks are too delayed. */
108 bool last_add_time_valid;
109 ktime_t last_add_time;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000110#endif
111
Andy Lutomirski780bc792016-02-02 21:46:36 -0800112 /* Per-descriptor state. */
113 struct vring_desc_state desc_state[];
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000114};
115
116#define to_vvq(_vq) container_of(_vq, struct vring_virtqueue, vq)
117
Andy Lutomirskid26c96c2016-02-02 21:46:35 -0800118/*
119 * The interaction between virtio and a possible IOMMU is a mess.
120 *
121 * On most systems with virtio, physical addresses match bus addresses,
122 * and it doesn't particularly matter whether we use the DMA API.
123 *
124 * On some systems, including Xen and any system with a physical device
125 * that speaks virtio behind a physical IOMMU, we must use the DMA API
126 * for virtio DMA to work at all.
127 *
128 * On other systems, including SPARC and PPC64, virtio-pci devices are
129 * enumerated as though they are behind an IOMMU, but the virtio host
130 * ignores the IOMMU, so we must either pretend that the IOMMU isn't
131 * there or somehow map everything as the identity.
132 *
133 * For the time being, we preserve historic behavior and bypass the DMA
134 * API.
135 */
136
137static bool vring_use_dma_api(struct virtio_device *vdev)
138{
139 return false;
140}
141
Andy Lutomirski780bc792016-02-02 21:46:36 -0800142/*
143 * The DMA ops on various arches are rather gnarly right now, and
144 * making all of the arch DMA ops work on the vring device itself
145 * is a mess. For now, we use the parent device for DMA ops.
146 */
147struct device *vring_dma_dev(const struct vring_virtqueue *vq)
148{
149 return vq->vq.vdev->dev.parent;
150}
151
152/* Map one sg entry. */
153static dma_addr_t vring_map_one_sg(const struct vring_virtqueue *vq,
154 struct scatterlist *sg,
155 enum dma_data_direction direction)
156{
157 if (!vring_use_dma_api(vq->vq.vdev))
158 return (dma_addr_t)sg_phys(sg);
159
160 /*
161 * We can't use dma_map_sg, because we don't use scatterlists in
162 * the way it expects (we don't guarantee that the scatterlist
163 * will exist for the lifetime of the mapping).
164 */
165 return dma_map_page(vring_dma_dev(vq),
166 sg_page(sg), sg->offset, sg->length,
167 direction);
168}
169
170static dma_addr_t vring_map_single(const struct vring_virtqueue *vq,
171 void *cpu_addr, size_t size,
172 enum dma_data_direction direction)
173{
174 if (!vring_use_dma_api(vq->vq.vdev))
175 return (dma_addr_t)virt_to_phys(cpu_addr);
176
177 return dma_map_single(vring_dma_dev(vq),
178 cpu_addr, size, direction);
179}
180
181static void vring_unmap_one(const struct vring_virtqueue *vq,
182 struct vring_desc *desc)
183{
184 u16 flags;
185
186 if (!vring_use_dma_api(vq->vq.vdev))
187 return;
188
189 flags = virtio16_to_cpu(vq->vq.vdev, desc->flags);
190
191 if (flags & VRING_DESC_F_INDIRECT) {
192 dma_unmap_single(vring_dma_dev(vq),
193 virtio64_to_cpu(vq->vq.vdev, desc->addr),
194 virtio32_to_cpu(vq->vq.vdev, desc->len),
195 (flags & VRING_DESC_F_WRITE) ?
196 DMA_FROM_DEVICE : DMA_TO_DEVICE);
197 } else {
198 dma_unmap_page(vring_dma_dev(vq),
199 virtio64_to_cpu(vq->vq.vdev, desc->addr),
200 virtio32_to_cpu(vq->vq.vdev, desc->len),
201 (flags & VRING_DESC_F_WRITE) ?
202 DMA_FROM_DEVICE : DMA_TO_DEVICE);
203 }
204}
205
206static int vring_mapping_error(const struct vring_virtqueue *vq,
207 dma_addr_t addr)
208{
209 if (!vring_use_dma_api(vq->vq.vdev))
210 return 0;
211
212 return dma_mapping_error(vring_dma_dev(vq), addr);
213}
214
Michael S. Tsirkin00e6f3d2014-10-22 15:42:09 +0300215static struct vring_desc *alloc_indirect(struct virtqueue *_vq,
216 unsigned int total_sg, gfp_t gfp)
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100217{
218 struct vring_desc *desc;
Rusty Russellb25bd252014-09-11 10:17:38 +0930219 unsigned int i;
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100220
Will Deaconb92b1b82012-10-19 14:03:33 +0100221 /*
222 * We require lowmem mappings for the descriptors because
223 * otherwise virt_to_phys will give us bogus addresses in the
224 * virtqueue.
225 */
Michal Hocko82107532015-12-01 15:32:49 +0100226 gfp &= ~__GFP_HIGHMEM;
Will Deaconb92b1b82012-10-19 14:03:33 +0100227
Rusty Russell13816c72013-03-20 15:37:09 +1030228 desc = kmalloc(total_sg * sizeof(struct vring_desc), gfp);
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100229 if (!desc)
Rusty Russellb25bd252014-09-11 10:17:38 +0930230 return NULL;
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100231
Rusty Russellb25bd252014-09-11 10:17:38 +0930232 for (i = 0; i < total_sg; i++)
Michael S. Tsirkin00e6f3d2014-10-22 15:42:09 +0300233 desc[i].next = cpu_to_virtio16(_vq->vdev, i + 1);
Rusty Russellb25bd252014-09-11 10:17:38 +0930234 return desc;
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100235}
236
Rusty Russell13816c72013-03-20 15:37:09 +1030237static inline int virtqueue_add(struct virtqueue *_vq,
238 struct scatterlist *sgs[],
Rusty Russelleeebf9b2014-09-11 10:17:37 +0930239 unsigned int total_sg,
Rusty Russell13816c72013-03-20 15:37:09 +1030240 unsigned int out_sgs,
241 unsigned int in_sgs,
242 void *data,
243 gfp_t gfp)
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000244{
245 struct vring_virtqueue *vq = to_vvq(_vq);
Rusty Russell13816c72013-03-20 15:37:09 +1030246 struct scatterlist *sg;
Rusty Russellb25bd252014-09-11 10:17:38 +0930247 struct vring_desc *desc;
Andy Lutomirski780bc792016-02-02 21:46:36 -0800248 unsigned int i, n, avail, descs_used, uninitialized_var(prev), err_idx;
Michael S. Tsirkin1fe9b6f2010-07-26 16:55:30 +0930249 int head;
Rusty Russellb25bd252014-09-11 10:17:38 +0930250 bool indirect;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000251
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100252 START_USE(vq);
253
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000254 BUG_ON(data == NULL);
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100255
Rusty Russell70670444c22014-03-13 11:23:40 +1030256 if (unlikely(vq->broken)) {
257 END_USE(vq);
258 return -EIO;
259 }
260
Rusty Russelle93300b2012-01-12 15:44:43 +1030261#ifdef DEBUG
262 {
263 ktime_t now = ktime_get();
264
265 /* No kick or get, with .1 second between? Warn. */
266 if (vq->last_add_time_valid)
267 WARN_ON(ktime_to_ms(ktime_sub(now, vq->last_add_time))
268 > 100);
269 vq->last_add_time = now;
270 vq->last_add_time_valid = true;
271 }
272#endif
273
Rusty Russell13816c72013-03-20 15:37:09 +1030274 BUG_ON(total_sg > vq->vring.num);
275 BUG_ON(total_sg == 0);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000276
Rusty Russellb25bd252014-09-11 10:17:38 +0930277 head = vq->free_head;
278
279 /* If the host supports indirect descriptor tables, and we have multiple
280 * buffers, then go indirect. FIXME: tune this threshold */
281 if (vq->indirect && total_sg > 1 && vq->vq.num_free)
Michael S. Tsirkin00e6f3d2014-10-22 15:42:09 +0300282 desc = alloc_indirect(_vq, total_sg, gfp);
Rusty Russellb25bd252014-09-11 10:17:38 +0930283 else
284 desc = NULL;
285
286 if (desc) {
287 /* Use a single buffer which doesn't continue */
Andy Lutomirski780bc792016-02-02 21:46:36 -0800288 indirect = true;
Rusty Russellb25bd252014-09-11 10:17:38 +0930289 /* Set up rest to use this indirect table. */
290 i = 0;
291 descs_used = 1;
Rusty Russellb25bd252014-09-11 10:17:38 +0930292 } else {
Andy Lutomirski780bc792016-02-02 21:46:36 -0800293 indirect = false;
Rusty Russellb25bd252014-09-11 10:17:38 +0930294 desc = vq->vring.desc;
295 i = head;
296 descs_used = total_sg;
Rusty Russellb25bd252014-09-11 10:17:38 +0930297 }
298
299 if (vq->vq.num_free < descs_used) {
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000300 pr_debug("Can't add buf len %i - avail = %i\n",
Rusty Russellb25bd252014-09-11 10:17:38 +0930301 descs_used, vq->vq.num_free);
Rusty Russell44653ea2008-07-25 12:06:04 -0500302 /* FIXME: for historical reasons, we force a notify here if
303 * there are outgoing parts to the buffer. Presumably the
304 * host should service the ring ASAP. */
Rusty Russell13816c72013-03-20 15:37:09 +1030305 if (out_sgs)
Rusty Russell44653ea2008-07-25 12:06:04 -0500306 vq->notify(&vq->vq);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000307 END_USE(vq);
308 return -ENOSPC;
309 }
310
Rusty Russell13816c72013-03-20 15:37:09 +1030311 for (n = 0; n < out_sgs; n++) {
Rusty Russelleeebf9b2014-09-11 10:17:37 +0930312 for (sg = sgs[n]; sg; sg = sg_next(sg)) {
Andy Lutomirski780bc792016-02-02 21:46:36 -0800313 dma_addr_t addr = vring_map_one_sg(vq, sg, DMA_TO_DEVICE);
314 if (vring_mapping_error(vq, addr))
315 goto unmap_release;
316
Michael S. Tsirkin00e6f3d2014-10-22 15:42:09 +0300317 desc[i].flags = cpu_to_virtio16(_vq->vdev, VRING_DESC_F_NEXT);
Andy Lutomirski780bc792016-02-02 21:46:36 -0800318 desc[i].addr = cpu_to_virtio64(_vq->vdev, addr);
Michael S. Tsirkin00e6f3d2014-10-22 15:42:09 +0300319 desc[i].len = cpu_to_virtio32(_vq->vdev, sg->length);
Rusty Russell13816c72013-03-20 15:37:09 +1030320 prev = i;
Michael S. Tsirkin00e6f3d2014-10-22 15:42:09 +0300321 i = virtio16_to_cpu(_vq->vdev, desc[i].next);
Rusty Russell13816c72013-03-20 15:37:09 +1030322 }
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000323 }
Rusty Russell13816c72013-03-20 15:37:09 +1030324 for (; n < (out_sgs + in_sgs); n++) {
Rusty Russelleeebf9b2014-09-11 10:17:37 +0930325 for (sg = sgs[n]; sg; sg = sg_next(sg)) {
Andy Lutomirski780bc792016-02-02 21:46:36 -0800326 dma_addr_t addr = vring_map_one_sg(vq, sg, DMA_FROM_DEVICE);
327 if (vring_mapping_error(vq, addr))
328 goto unmap_release;
329
Michael S. Tsirkin00e6f3d2014-10-22 15:42:09 +0300330 desc[i].flags = cpu_to_virtio16(_vq->vdev, VRING_DESC_F_NEXT | VRING_DESC_F_WRITE);
Andy Lutomirski780bc792016-02-02 21:46:36 -0800331 desc[i].addr = cpu_to_virtio64(_vq->vdev, addr);
Michael S. Tsirkin00e6f3d2014-10-22 15:42:09 +0300332 desc[i].len = cpu_to_virtio32(_vq->vdev, sg->length);
Rusty Russell13816c72013-03-20 15:37:09 +1030333 prev = i;
Michael S. Tsirkin00e6f3d2014-10-22 15:42:09 +0300334 i = virtio16_to_cpu(_vq->vdev, desc[i].next);
Rusty Russell13816c72013-03-20 15:37:09 +1030335 }
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000336 }
337 /* Last one doesn't continue. */
Michael S. Tsirkin00e6f3d2014-10-22 15:42:09 +0300338 desc[prev].flags &= cpu_to_virtio16(_vq->vdev, ~VRING_DESC_F_NEXT);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000339
Andy Lutomirski780bc792016-02-02 21:46:36 -0800340 if (indirect) {
341 /* Now that the indirect table is filled in, map it. */
342 dma_addr_t addr = vring_map_single(
343 vq, desc, total_sg * sizeof(struct vring_desc),
344 DMA_TO_DEVICE);
345 if (vring_mapping_error(vq, addr))
346 goto unmap_release;
347
348 vq->vring.desc[head].flags = cpu_to_virtio16(_vq->vdev, VRING_DESC_F_INDIRECT);
349 vq->vring.desc[head].addr = cpu_to_virtio64(_vq->vdev, addr);
350
351 vq->vring.desc[head].len = cpu_to_virtio32(_vq->vdev, total_sg * sizeof(struct vring_desc));
352 }
353
354 /* We're using some buffers from the free list. */
355 vq->vq.num_free -= descs_used;
356
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000357 /* Update free pointer */
Rusty Russellb25bd252014-09-11 10:17:38 +0930358 if (indirect)
Michael S. Tsirkin00e6f3d2014-10-22 15:42:09 +0300359 vq->free_head = virtio16_to_cpu(_vq->vdev, vq->vring.desc[head].next);
Rusty Russellb25bd252014-09-11 10:17:38 +0930360 else
361 vq->free_head = i;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000362
Andy Lutomirski780bc792016-02-02 21:46:36 -0800363 /* Store token and indirect buffer state. */
364 vq->desc_state[head].data = data;
365 if (indirect)
366 vq->desc_state[head].indir_desc = desc;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000367
368 /* Put entry in available array (but don't update avail->idx until they
Rusty Russell3b720b82012-01-12 15:44:43 +1030369 * do sync). */
Venkatesh Srinivasf277ec42015-11-10 16:21:07 -0800370 avail = vq->avail_idx_shadow & (vq->vring.num - 1);
Michael S. Tsirkin00e6f3d2014-10-22 15:42:09 +0300371 vq->vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000372
Rusty Russellee7cd892012-01-12 15:44:43 +1030373 /* Descriptors and available array need to be set before we expose the
374 * new available array entries. */
Rusty Russella9a0fef2013-03-18 13:22:19 +1030375 virtio_wmb(vq->weak_barriers);
Venkatesh Srinivasf277ec42015-11-10 16:21:07 -0800376 vq->avail_idx_shadow++;
377 vq->vring.avail->idx = cpu_to_virtio16(_vq->vdev, vq->avail_idx_shadow);
Rusty Russellee7cd892012-01-12 15:44:43 +1030378 vq->num_added++;
379
Tetsuo Handa5e05bf52015-02-11 15:01:13 +1030380 pr_debug("Added buffer head %i to %p\n", head, vq);
381 END_USE(vq);
382
Rusty Russellee7cd892012-01-12 15:44:43 +1030383 /* This is very unlikely, but theoretically possible. Kick
384 * just in case. */
385 if (unlikely(vq->num_added == (1 << 16) - 1))
386 virtqueue_kick(_vq);
387
Rusty Russell98e8c6b2012-10-16 23:56:15 +1030388 return 0;
Andy Lutomirski780bc792016-02-02 21:46:36 -0800389
390unmap_release:
391 err_idx = i;
392 i = head;
393
394 for (n = 0; n < total_sg; n++) {
395 if (i == err_idx)
396 break;
397 vring_unmap_one(vq, &desc[i]);
398 i = vq->vring.desc[i].next;
399 }
400
401 vq->vq.num_free += total_sg;
402
403 if (indirect)
404 kfree(desc);
405
406 return -EIO;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000407}
Rusty Russell13816c72013-03-20 15:37:09 +1030408
409/**
Rusty Russell13816c72013-03-20 15:37:09 +1030410 * virtqueue_add_sgs - expose buffers to other end
411 * @vq: the struct virtqueue we're talking about.
412 * @sgs: array of terminated scatterlists.
413 * @out_num: the number of scatterlists readable by other side
414 * @in_num: the number of scatterlists which are writable (after readable ones)
415 * @data: the token identifying the buffer.
416 * @gfp: how to do memory allocations (if necessary).
417 *
418 * Caller must ensure we don't call this with other virtqueue operations
419 * at the same time (except where noted).
420 *
Rusty Russell70670444c22014-03-13 11:23:40 +1030421 * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO).
Rusty Russell13816c72013-03-20 15:37:09 +1030422 */
423int virtqueue_add_sgs(struct virtqueue *_vq,
424 struct scatterlist *sgs[],
425 unsigned int out_sgs,
426 unsigned int in_sgs,
427 void *data,
428 gfp_t gfp)
429{
Rusty Russelleeebf9b2014-09-11 10:17:37 +0930430 unsigned int i, total_sg = 0;
Rusty Russell13816c72013-03-20 15:37:09 +1030431
432 /* Count them first. */
Rusty Russelleeebf9b2014-09-11 10:17:37 +0930433 for (i = 0; i < out_sgs + in_sgs; i++) {
Rusty Russell13816c72013-03-20 15:37:09 +1030434 struct scatterlist *sg;
435 for (sg = sgs[i]; sg; sg = sg_next(sg))
Rusty Russelleeebf9b2014-09-11 10:17:37 +0930436 total_sg++;
Rusty Russell13816c72013-03-20 15:37:09 +1030437 }
Rusty Russelleeebf9b2014-09-11 10:17:37 +0930438 return virtqueue_add(_vq, sgs, total_sg, out_sgs, in_sgs, data, gfp);
Rusty Russell13816c72013-03-20 15:37:09 +1030439}
440EXPORT_SYMBOL_GPL(virtqueue_add_sgs);
441
442/**
Rusty Russell282edb32013-03-20 15:44:26 +1030443 * virtqueue_add_outbuf - expose output buffers to other end
444 * @vq: the struct virtqueue we're talking about.
Rusty Russelleeebf9b2014-09-11 10:17:37 +0930445 * @sg: scatterlist (must be well-formed and terminated!)
446 * @num: the number of entries in @sg readable by other side
Rusty Russell282edb32013-03-20 15:44:26 +1030447 * @data: the token identifying the buffer.
448 * @gfp: how to do memory allocations (if necessary).
449 *
450 * Caller must ensure we don't call this with other virtqueue operations
451 * at the same time (except where noted).
452 *
Rusty Russell70670444c22014-03-13 11:23:40 +1030453 * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO).
Rusty Russell282edb32013-03-20 15:44:26 +1030454 */
455int virtqueue_add_outbuf(struct virtqueue *vq,
Rusty Russelleeebf9b2014-09-11 10:17:37 +0930456 struct scatterlist *sg, unsigned int num,
Rusty Russell282edb32013-03-20 15:44:26 +1030457 void *data,
458 gfp_t gfp)
459{
Rusty Russelleeebf9b2014-09-11 10:17:37 +0930460 return virtqueue_add(vq, &sg, num, 1, 0, data, gfp);
Rusty Russell282edb32013-03-20 15:44:26 +1030461}
462EXPORT_SYMBOL_GPL(virtqueue_add_outbuf);
463
464/**
465 * virtqueue_add_inbuf - expose input buffers to other end
466 * @vq: the struct virtqueue we're talking about.
Rusty Russelleeebf9b2014-09-11 10:17:37 +0930467 * @sg: scatterlist (must be well-formed and terminated!)
468 * @num: the number of entries in @sg writable by other side
Rusty Russell282edb32013-03-20 15:44:26 +1030469 * @data: the token identifying the buffer.
470 * @gfp: how to do memory allocations (if necessary).
471 *
472 * Caller must ensure we don't call this with other virtqueue operations
473 * at the same time (except where noted).
474 *
Rusty Russell70670444c22014-03-13 11:23:40 +1030475 * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO).
Rusty Russell282edb32013-03-20 15:44:26 +1030476 */
477int virtqueue_add_inbuf(struct virtqueue *vq,
Rusty Russelleeebf9b2014-09-11 10:17:37 +0930478 struct scatterlist *sg, unsigned int num,
Rusty Russell282edb32013-03-20 15:44:26 +1030479 void *data,
480 gfp_t gfp)
481{
Rusty Russelleeebf9b2014-09-11 10:17:37 +0930482 return virtqueue_add(vq, &sg, num, 0, 1, data, gfp);
Rusty Russell282edb32013-03-20 15:44:26 +1030483}
484EXPORT_SYMBOL_GPL(virtqueue_add_inbuf);
485
486/**
Rusty Russell41f03772012-01-12 15:44:43 +1030487 * virtqueue_kick_prepare - first half of split virtqueue_kick call.
Rusty Russell5dfc1762012-01-12 15:44:42 +1030488 * @vq: the struct virtqueue
489 *
Rusty Russell41f03772012-01-12 15:44:43 +1030490 * Instead of virtqueue_kick(), you can do:
491 * if (virtqueue_kick_prepare(vq))
492 * virtqueue_notify(vq);
Rusty Russell5dfc1762012-01-12 15:44:42 +1030493 *
Rusty Russell41f03772012-01-12 15:44:43 +1030494 * This is sometimes useful because the virtqueue_kick_prepare() needs
495 * to be serialized, but the actual virtqueue_notify() call does not.
Rusty Russell5dfc1762012-01-12 15:44:42 +1030496 */
Rusty Russell41f03772012-01-12 15:44:43 +1030497bool virtqueue_kick_prepare(struct virtqueue *_vq)
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000498{
499 struct vring_virtqueue *vq = to_vvq(_vq);
Michael S. Tsirkina5c262c2011-05-20 02:10:44 +0300500 u16 new, old;
Rusty Russell41f03772012-01-12 15:44:43 +1030501 bool needs_kick;
502
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000503 START_USE(vq);
Jason Wanga72caae2012-01-20 16:17:08 +0800504 /* We need to expose available array entries before checking avail
505 * event. */
Rusty Russella9a0fef2013-03-18 13:22:19 +1030506 virtio_mb(vq->weak_barriers);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000507
Venkatesh Srinivasf277ec42015-11-10 16:21:07 -0800508 old = vq->avail_idx_shadow - vq->num_added;
509 new = vq->avail_idx_shadow;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000510 vq->num_added = 0;
511
Rusty Russelle93300b2012-01-12 15:44:43 +1030512#ifdef DEBUG
513 if (vq->last_add_time_valid) {
514 WARN_ON(ktime_to_ms(ktime_sub(ktime_get(),
515 vq->last_add_time)) > 100);
516 }
517 vq->last_add_time_valid = false;
518#endif
519
Rusty Russell41f03772012-01-12 15:44:43 +1030520 if (vq->event) {
Michael S. Tsirkin00e6f3d2014-10-22 15:42:09 +0300521 needs_kick = vring_need_event(virtio16_to_cpu(_vq->vdev, vring_avail_event(&vq->vring)),
Rusty Russell41f03772012-01-12 15:44:43 +1030522 new, old);
523 } else {
Michael S. Tsirkin00e6f3d2014-10-22 15:42:09 +0300524 needs_kick = !(vq->vring.used->flags & cpu_to_virtio16(_vq->vdev, VRING_USED_F_NO_NOTIFY));
Rusty Russell41f03772012-01-12 15:44:43 +1030525 }
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000526 END_USE(vq);
Rusty Russell41f03772012-01-12 15:44:43 +1030527 return needs_kick;
528}
529EXPORT_SYMBOL_GPL(virtqueue_kick_prepare);
530
531/**
532 * virtqueue_notify - second half of split virtqueue_kick call.
533 * @vq: the struct virtqueue
534 *
535 * This does not need to be serialized.
Heinz Graalfs5b1bf7c2013-10-29 09:39:48 +1030536 *
537 * Returns false if host notify failed or queue is broken, otherwise true.
Rusty Russell41f03772012-01-12 15:44:43 +1030538 */
Heinz Graalfs5b1bf7c2013-10-29 09:39:48 +1030539bool virtqueue_notify(struct virtqueue *_vq)
Rusty Russell41f03772012-01-12 15:44:43 +1030540{
541 struct vring_virtqueue *vq = to_vvq(_vq);
542
Heinz Graalfs5b1bf7c2013-10-29 09:39:48 +1030543 if (unlikely(vq->broken))
544 return false;
545
Rusty Russell41f03772012-01-12 15:44:43 +1030546 /* Prod other side to tell it about changes. */
Heinz Graalfs2342d6a2013-11-05 21:20:27 +1030547 if (!vq->notify(_vq)) {
Heinz Graalfs5b1bf7c2013-10-29 09:39:48 +1030548 vq->broken = true;
549 return false;
550 }
551 return true;
Rusty Russell41f03772012-01-12 15:44:43 +1030552}
553EXPORT_SYMBOL_GPL(virtqueue_notify);
554
555/**
556 * virtqueue_kick - update after add_buf
557 * @vq: the struct virtqueue
558 *
Rusty Russellb3087e42013-05-20 12:15:44 +0930559 * After one or more virtqueue_add_* calls, invoke this to kick
Rusty Russell41f03772012-01-12 15:44:43 +1030560 * the other side.
561 *
562 * Caller must ensure we don't call this with other virtqueue
563 * operations at the same time (except where noted).
Heinz Graalfs5b1bf7c2013-10-29 09:39:48 +1030564 *
565 * Returns false if kick failed, otherwise true.
Rusty Russell41f03772012-01-12 15:44:43 +1030566 */
Heinz Graalfs5b1bf7c2013-10-29 09:39:48 +1030567bool virtqueue_kick(struct virtqueue *vq)
Rusty Russell41f03772012-01-12 15:44:43 +1030568{
569 if (virtqueue_kick_prepare(vq))
Heinz Graalfs5b1bf7c2013-10-29 09:39:48 +1030570 return virtqueue_notify(vq);
571 return true;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000572}
Michael S. Tsirkin7c5e9ed2010-04-12 16:19:07 +0300573EXPORT_SYMBOL_GPL(virtqueue_kick);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000574
575static void detach_buf(struct vring_virtqueue *vq, unsigned int head)
576{
Andy Lutomirski780bc792016-02-02 21:46:36 -0800577 unsigned int i, j;
578 u16 nextflag = cpu_to_virtio16(vq->vq.vdev, VRING_DESC_F_NEXT);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000579
580 /* Clear data ptr. */
Andy Lutomirski780bc792016-02-02 21:46:36 -0800581 vq->desc_state[head].data = NULL;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000582
Andy Lutomirski780bc792016-02-02 21:46:36 -0800583 /* Put back on free list: unmap first-level descriptors and find end */
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000584 i = head;
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100585
Andy Lutomirski780bc792016-02-02 21:46:36 -0800586 while (vq->vring.desc[i].flags & nextflag) {
587 vring_unmap_one(vq, &vq->vring.desc[i]);
Michael S. Tsirkin00e6f3d2014-10-22 15:42:09 +0300588 i = virtio16_to_cpu(vq->vq.vdev, vq->vring.desc[i].next);
Rusty Russell06ca2872012-10-16 23:56:14 +1030589 vq->vq.num_free++;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000590 }
591
Andy Lutomirski780bc792016-02-02 21:46:36 -0800592 vring_unmap_one(vq, &vq->vring.desc[i]);
Michael S. Tsirkin00e6f3d2014-10-22 15:42:09 +0300593 vq->vring.desc[i].next = cpu_to_virtio16(vq->vq.vdev, vq->free_head);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000594 vq->free_head = head;
Andy Lutomirski780bc792016-02-02 21:46:36 -0800595
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000596 /* Plus final descriptor */
Rusty Russell06ca2872012-10-16 23:56:14 +1030597 vq->vq.num_free++;
Andy Lutomirski780bc792016-02-02 21:46:36 -0800598
599 /* Free the indirect table, if any, now that it's unmapped. */
600 if (vq->desc_state[head].indir_desc) {
601 struct vring_desc *indir_desc = vq->desc_state[head].indir_desc;
602 u32 len = virtio32_to_cpu(vq->vq.vdev, vq->vring.desc[head].len);
603
604 BUG_ON(!(vq->vring.desc[head].flags &
605 cpu_to_virtio16(vq->vq.vdev, VRING_DESC_F_INDIRECT)));
606 BUG_ON(len == 0 || len % sizeof(struct vring_desc));
607
608 for (j = 0; j < len / sizeof(struct vring_desc); j++)
609 vring_unmap_one(vq, &indir_desc[j]);
610
611 kfree(vq->desc_state[head].indir_desc);
612 vq->desc_state[head].indir_desc = NULL;
613 }
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000614}
615
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000616static inline bool more_used(const struct vring_virtqueue *vq)
617{
Michael S. Tsirkin00e6f3d2014-10-22 15:42:09 +0300618 return vq->last_used_idx != virtio16_to_cpu(vq->vq.vdev, vq->vring.used->idx);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000619}
620
Rusty Russell5dfc1762012-01-12 15:44:42 +1030621/**
622 * virtqueue_get_buf - get the next used buffer
623 * @vq: the struct virtqueue we're talking about.
624 * @len: the length written into the buffer
625 *
626 * If the driver wrote data into the buffer, @len will be set to the
627 * amount written. This means you don't need to clear the buffer
628 * beforehand to ensure there's no data leakage in the case of short
629 * writes.
630 *
631 * Caller must ensure we don't call this with other virtqueue
632 * operations at the same time (except where noted).
633 *
634 * Returns NULL if there are no used buffers, or the "data" token
Rusty Russellb3087e42013-05-20 12:15:44 +0930635 * handed to virtqueue_add_*().
Rusty Russell5dfc1762012-01-12 15:44:42 +1030636 */
Michael S. Tsirkin7c5e9ed2010-04-12 16:19:07 +0300637void *virtqueue_get_buf(struct virtqueue *_vq, unsigned int *len)
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000638{
639 struct vring_virtqueue *vq = to_vvq(_vq);
640 void *ret;
641 unsigned int i;
Rusty Russell3b720b82012-01-12 15:44:43 +1030642 u16 last_used;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000643
644 START_USE(vq);
645
Rusty Russell5ef82752008-05-02 21:50:43 -0500646 if (unlikely(vq->broken)) {
647 END_USE(vq);
648 return NULL;
649 }
650
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000651 if (!more_used(vq)) {
652 pr_debug("No more buffers in queue\n");
653 END_USE(vq);
654 return NULL;
655 }
656
Michael S. Tsirkin2d61ba92009-10-25 15:28:53 +0200657 /* Only get used array entries after they have been exposed by host. */
Rusty Russella9a0fef2013-03-18 13:22:19 +1030658 virtio_rmb(vq->weak_barriers);
Michael S. Tsirkin2d61ba92009-10-25 15:28:53 +0200659
Rusty Russell3b720b82012-01-12 15:44:43 +1030660 last_used = (vq->last_used_idx & (vq->vring.num - 1));
Michael S. Tsirkin00e6f3d2014-10-22 15:42:09 +0300661 i = virtio32_to_cpu(_vq->vdev, vq->vring.used->ring[last_used].id);
662 *len = virtio32_to_cpu(_vq->vdev, vq->vring.used->ring[last_used].len);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000663
664 if (unlikely(i >= vq->vring.num)) {
665 BAD_RING(vq, "id %u out of range\n", i);
666 return NULL;
667 }
Andy Lutomirski780bc792016-02-02 21:46:36 -0800668 if (unlikely(!vq->desc_state[i].data)) {
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000669 BAD_RING(vq, "id %u is not a head!\n", i);
670 return NULL;
671 }
672
673 /* detach_buf clears data, so grab it now. */
Andy Lutomirski780bc792016-02-02 21:46:36 -0800674 ret = vq->desc_state[i].data;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000675 detach_buf(vq, i);
676 vq->last_used_idx++;
Michael S. Tsirkina5c262c2011-05-20 02:10:44 +0300677 /* If we expect an interrupt for the next entry, tell host
678 * by writing event index and flush out the write before
679 * the read in the next get_buf call. */
Michael S. Tsirkin788e5b32015-12-17 12:20:39 +0200680 if (!(vq->avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT))
681 virtio_store_mb(vq->weak_barriers,
682 &vring_used_event(&vq->vring),
683 cpu_to_virtio16(_vq->vdev, vq->last_used_idx));
Michael S. Tsirkina5c262c2011-05-20 02:10:44 +0300684
Rusty Russelle93300b2012-01-12 15:44:43 +1030685#ifdef DEBUG
686 vq->last_add_time_valid = false;
687#endif
688
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000689 END_USE(vq);
690 return ret;
691}
Michael S. Tsirkin7c5e9ed2010-04-12 16:19:07 +0300692EXPORT_SYMBOL_GPL(virtqueue_get_buf);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000693
Rusty Russell5dfc1762012-01-12 15:44:42 +1030694/**
695 * virtqueue_disable_cb - disable callbacks
696 * @vq: the struct virtqueue we're talking about.
697 *
698 * Note that this is not necessarily synchronous, hence unreliable and only
699 * useful as an optimization.
700 *
701 * Unlike other operations, this need not be serialized.
702 */
Michael S. Tsirkin7c5e9ed2010-04-12 16:19:07 +0300703void virtqueue_disable_cb(struct virtqueue *_vq)
Rusty Russell18445c42008-02-04 23:49:57 -0500704{
705 struct vring_virtqueue *vq = to_vvq(_vq);
706
Venkatesh Srinivasf277ec42015-11-10 16:21:07 -0800707 if (!(vq->avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT)) {
708 vq->avail_flags_shadow |= VRING_AVAIL_F_NO_INTERRUPT;
709 vq->vring.avail->flags = cpu_to_virtio16(_vq->vdev, vq->avail_flags_shadow);
710 }
711
Rusty Russell18445c42008-02-04 23:49:57 -0500712}
Michael S. Tsirkin7c5e9ed2010-04-12 16:19:07 +0300713EXPORT_SYMBOL_GPL(virtqueue_disable_cb);
Rusty Russell18445c42008-02-04 23:49:57 -0500714
Rusty Russell5dfc1762012-01-12 15:44:42 +1030715/**
Michael S. Tsirkincc229882013-07-09 13:19:18 +0300716 * virtqueue_enable_cb_prepare - restart callbacks after disable_cb
717 * @vq: the struct virtqueue we're talking about.
718 *
719 * This re-enables callbacks; it returns current queue state
720 * in an opaque unsigned value. This value should be later tested by
721 * virtqueue_poll, to detect a possible race between the driver checking for
722 * more work, and enabling callbacks.
723 *
724 * Caller must ensure we don't call this with other virtqueue
725 * operations at the same time (except where noted).
726 */
727unsigned virtqueue_enable_cb_prepare(struct virtqueue *_vq)
728{
729 struct vring_virtqueue *vq = to_vvq(_vq);
730 u16 last_used_idx;
731
732 START_USE(vq);
733
734 /* We optimistically turn back on interrupts, then check if there was
735 * more to do. */
736 /* Depending on the VIRTIO_RING_F_EVENT_IDX feature, we need to
737 * either clear the flags bit or point the event index at the next
738 * entry. Always do both to keep code simple. */
Venkatesh Srinivasf277ec42015-11-10 16:21:07 -0800739 if (vq->avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT) {
740 vq->avail_flags_shadow &= ~VRING_AVAIL_F_NO_INTERRUPT;
741 vq->vring.avail->flags = cpu_to_virtio16(_vq->vdev, vq->avail_flags_shadow);
742 }
Michael S. Tsirkin00e6f3d2014-10-22 15:42:09 +0300743 vring_used_event(&vq->vring) = cpu_to_virtio16(_vq->vdev, last_used_idx = vq->last_used_idx);
Michael S. Tsirkincc229882013-07-09 13:19:18 +0300744 END_USE(vq);
745 return last_used_idx;
746}
747EXPORT_SYMBOL_GPL(virtqueue_enable_cb_prepare);
748
749/**
750 * virtqueue_poll - query pending used buffers
751 * @vq: the struct virtqueue we're talking about.
752 * @last_used_idx: virtqueue state (from call to virtqueue_enable_cb_prepare).
753 *
754 * Returns "true" if there are pending used buffers in the queue.
755 *
756 * This does not need to be serialized.
757 */
758bool virtqueue_poll(struct virtqueue *_vq, unsigned last_used_idx)
759{
760 struct vring_virtqueue *vq = to_vvq(_vq);
761
762 virtio_mb(vq->weak_barriers);
Michael S. Tsirkin00e6f3d2014-10-22 15:42:09 +0300763 return (u16)last_used_idx != virtio16_to_cpu(_vq->vdev, vq->vring.used->idx);
Michael S. Tsirkincc229882013-07-09 13:19:18 +0300764}
765EXPORT_SYMBOL_GPL(virtqueue_poll);
766
767/**
Rusty Russell5dfc1762012-01-12 15:44:42 +1030768 * virtqueue_enable_cb - restart callbacks after disable_cb.
769 * @vq: the struct virtqueue we're talking about.
770 *
771 * This re-enables callbacks; it returns "false" if there are pending
772 * buffers in the queue, to detect a possible race between the driver
773 * checking for more work, and enabling callbacks.
774 *
775 * Caller must ensure we don't call this with other virtqueue
776 * operations at the same time (except where noted).
777 */
Michael S. Tsirkin7c5e9ed2010-04-12 16:19:07 +0300778bool virtqueue_enable_cb(struct virtqueue *_vq)
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000779{
Michael S. Tsirkincc229882013-07-09 13:19:18 +0300780 unsigned last_used_idx = virtqueue_enable_cb_prepare(_vq);
781 return !virtqueue_poll(_vq, last_used_idx);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000782}
Michael S. Tsirkin7c5e9ed2010-04-12 16:19:07 +0300783EXPORT_SYMBOL_GPL(virtqueue_enable_cb);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000784
Rusty Russell5dfc1762012-01-12 15:44:42 +1030785/**
786 * virtqueue_enable_cb_delayed - restart callbacks after disable_cb.
787 * @vq: the struct virtqueue we're talking about.
788 *
789 * This re-enables callbacks but hints to the other side to delay
790 * interrupts until most of the available buffers have been processed;
791 * it returns "false" if there are many pending buffers in the queue,
792 * to detect a possible race between the driver checking for more work,
793 * and enabling callbacks.
794 *
795 * Caller must ensure we don't call this with other virtqueue
796 * operations at the same time (except where noted).
797 */
Michael S. Tsirkin7ab358c2011-05-20 02:11:14 +0300798bool virtqueue_enable_cb_delayed(struct virtqueue *_vq)
799{
800 struct vring_virtqueue *vq = to_vvq(_vq);
801 u16 bufs;
802
803 START_USE(vq);
804
805 /* We optimistically turn back on interrupts, then check if there was
806 * more to do. */
807 /* Depending on the VIRTIO_RING_F_USED_EVENT_IDX feature, we need to
808 * either clear the flags bit or point the event index at the next
809 * entry. Always do both to keep code simple. */
Venkatesh Srinivasf277ec42015-11-10 16:21:07 -0800810 if (vq->avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT) {
811 vq->avail_flags_shadow &= ~VRING_AVAIL_F_NO_INTERRUPT;
812 vq->vring.avail->flags = cpu_to_virtio16(_vq->vdev, vq->avail_flags_shadow);
813 }
Michael S. Tsirkin7ab358c2011-05-20 02:11:14 +0300814 /* TODO: tune this threshold */
Venkatesh Srinivasf277ec42015-11-10 16:21:07 -0800815 bufs = (u16)(vq->avail_idx_shadow - vq->last_used_idx) * 3 / 4;
Michael S. Tsirkin788e5b32015-12-17 12:20:39 +0200816
817 virtio_store_mb(vq->weak_barriers,
818 &vring_used_event(&vq->vring),
819 cpu_to_virtio16(_vq->vdev, vq->last_used_idx + bufs));
820
Michael S. Tsirkin00e6f3d2014-10-22 15:42:09 +0300821 if (unlikely((u16)(virtio16_to_cpu(_vq->vdev, vq->vring.used->idx) - vq->last_used_idx) > bufs)) {
Michael S. Tsirkin7ab358c2011-05-20 02:11:14 +0300822 END_USE(vq);
823 return false;
824 }
825
826 END_USE(vq);
827 return true;
828}
829EXPORT_SYMBOL_GPL(virtqueue_enable_cb_delayed);
830
Rusty Russell5dfc1762012-01-12 15:44:42 +1030831/**
832 * virtqueue_detach_unused_buf - detach first unused buffer
833 * @vq: the struct virtqueue we're talking about.
834 *
Rusty Russellb3087e42013-05-20 12:15:44 +0930835 * Returns NULL or the "data" token handed to virtqueue_add_*().
Rusty Russell5dfc1762012-01-12 15:44:42 +1030836 * This is not valid on an active queue; it is useful only for device
837 * shutdown.
838 */
Michael S. Tsirkin7c5e9ed2010-04-12 16:19:07 +0300839void *virtqueue_detach_unused_buf(struct virtqueue *_vq)
Shirley Mac021eac2010-01-18 19:15:23 +0530840{
841 struct vring_virtqueue *vq = to_vvq(_vq);
842 unsigned int i;
843 void *buf;
844
845 START_USE(vq);
846
847 for (i = 0; i < vq->vring.num; i++) {
Andy Lutomirski780bc792016-02-02 21:46:36 -0800848 if (!vq->desc_state[i].data)
Shirley Mac021eac2010-01-18 19:15:23 +0530849 continue;
850 /* detach_buf clears data, so grab it now. */
Andy Lutomirski780bc792016-02-02 21:46:36 -0800851 buf = vq->desc_state[i].data;
Shirley Mac021eac2010-01-18 19:15:23 +0530852 detach_buf(vq, i);
Venkatesh Srinivasf277ec42015-11-10 16:21:07 -0800853 vq->avail_idx_shadow--;
854 vq->vring.avail->idx = cpu_to_virtio16(_vq->vdev, vq->avail_idx_shadow);
Shirley Mac021eac2010-01-18 19:15:23 +0530855 END_USE(vq);
856 return buf;
857 }
858 /* That should have freed everything. */
Rusty Russell06ca2872012-10-16 23:56:14 +1030859 BUG_ON(vq->vq.num_free != vq->vring.num);
Shirley Mac021eac2010-01-18 19:15:23 +0530860
861 END_USE(vq);
862 return NULL;
863}
Michael S. Tsirkin7c5e9ed2010-04-12 16:19:07 +0300864EXPORT_SYMBOL_GPL(virtqueue_detach_unused_buf);
Shirley Mac021eac2010-01-18 19:15:23 +0530865
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000866irqreturn_t vring_interrupt(int irq, void *_vq)
867{
868 struct vring_virtqueue *vq = to_vvq(_vq);
869
870 if (!more_used(vq)) {
871 pr_debug("virtqueue interrupt with no work for %p\n", vq);
872 return IRQ_NONE;
873 }
874
875 if (unlikely(vq->broken))
876 return IRQ_HANDLED;
877
878 pr_debug("virtqueue callback for %p (%p)\n", vq, vq->vq.callback);
Rusty Russell18445c42008-02-04 23:49:57 -0500879 if (vq->vq.callback)
880 vq->vq.callback(&vq->vq);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000881
882 return IRQ_HANDLED;
883}
Rusty Russellc6fd4702008-02-04 23:50:05 -0500884EXPORT_SYMBOL_GPL(vring_interrupt);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000885
Andy Lutomirski2a2d1382016-02-02 21:46:37 -0800886struct virtqueue *__vring_new_virtqueue(unsigned int index,
887 struct vring vring,
888 struct virtio_device *vdev,
889 bool weak_barriers,
890 bool (*notify)(struct virtqueue *),
891 void (*callback)(struct virtqueue *),
892 const char *name)
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000893{
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000894 unsigned int i;
Andy Lutomirski2a2d1382016-02-02 21:46:37 -0800895 struct vring_virtqueue *vq;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000896
Andy Lutomirski2a2d1382016-02-02 21:46:37 -0800897 vq = kmalloc(sizeof(*vq) + vring.num * sizeof(struct vring_desc_state),
Andy Lutomirski780bc792016-02-02 21:46:36 -0800898 GFP_KERNEL);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000899 if (!vq)
900 return NULL;
901
Andy Lutomirski2a2d1382016-02-02 21:46:37 -0800902 vq->vring = vring;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000903 vq->vq.callback = callback;
904 vq->vq.vdev = vdev;
Rusty Russell9499f5e2009-06-12 22:16:35 -0600905 vq->vq.name = name;
Andy Lutomirski2a2d1382016-02-02 21:46:37 -0800906 vq->vq.num_free = vring.num;
Rusty Russell06ca2872012-10-16 23:56:14 +1030907 vq->vq.index = index;
Andy Lutomirski2a2d1382016-02-02 21:46:37 -0800908 vq->we_own_ring = false;
909 vq->queue_dma_addr = 0;
910 vq->queue_size_in_bytes = 0;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000911 vq->notify = notify;
Rusty Russell7b21e342012-01-12 15:44:42 +1030912 vq->weak_barriers = weak_barriers;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000913 vq->broken = false;
914 vq->last_used_idx = 0;
Venkatesh Srinivasf277ec42015-11-10 16:21:07 -0800915 vq->avail_flags_shadow = 0;
916 vq->avail_idx_shadow = 0;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000917 vq->num_added = 0;
Rusty Russell9499f5e2009-06-12 22:16:35 -0600918 list_add_tail(&vq->vq.list, &vdev->vqs);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000919#ifdef DEBUG
920 vq->in_use = false;
Rusty Russelle93300b2012-01-12 15:44:43 +1030921 vq->last_add_time_valid = false;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000922#endif
923
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100924 vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC);
Michael S. Tsirkina5c262c2011-05-20 02:10:44 +0300925 vq->event = virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX);
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100926
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000927 /* No callback? Tell other side not to bother us. */
Venkatesh Srinivasf277ec42015-11-10 16:21:07 -0800928 if (!callback) {
929 vq->avail_flags_shadow |= VRING_AVAIL_F_NO_INTERRUPT;
930 vq->vring.avail->flags = cpu_to_virtio16(vdev, vq->avail_flags_shadow);
931 }
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000932
933 /* Put everything in free lists. */
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000934 vq->free_head = 0;
Andy Lutomirski2a2d1382016-02-02 21:46:37 -0800935 for (i = 0; i < vring.num-1; i++)
Michael S. Tsirkin00e6f3d2014-10-22 15:42:09 +0300936 vq->vring.desc[i].next = cpu_to_virtio16(vdev, i + 1);
Andy Lutomirski2a2d1382016-02-02 21:46:37 -0800937 memset(vq->desc_state, 0, vring.num * sizeof(struct vring_desc_state));
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000938
939 return &vq->vq;
940}
Andy Lutomirski2a2d1382016-02-02 21:46:37 -0800941EXPORT_SYMBOL_GPL(__vring_new_virtqueue);
942
943static void *vring_alloc_queue(struct virtio_device *vdev, size_t size,
944 dma_addr_t *dma_handle, gfp_t flag)
945{
946 if (vring_use_dma_api(vdev)) {
947 return dma_alloc_coherent(vdev->dev.parent, size,
948 dma_handle, flag);
949 } else {
950 void *queue = alloc_pages_exact(PAGE_ALIGN(size), flag);
951 if (queue) {
952 phys_addr_t phys_addr = virt_to_phys(queue);
953 *dma_handle = (dma_addr_t)phys_addr;
954
955 /*
956 * Sanity check: make sure we dind't truncate
957 * the address. The only arches I can find that
958 * have 64-bit phys_addr_t but 32-bit dma_addr_t
959 * are certain non-highmem MIPS and x86
960 * configurations, but these configurations
961 * should never allocate physical pages above 32
962 * bits, so this is fine. Just in case, throw a
963 * warning and abort if we end up with an
964 * unrepresentable address.
965 */
966 if (WARN_ON_ONCE(*dma_handle != phys_addr)) {
967 free_pages_exact(queue, PAGE_ALIGN(size));
968 return NULL;
969 }
970 }
971 return queue;
972 }
973}
974
975static void vring_free_queue(struct virtio_device *vdev, size_t size,
976 void *queue, dma_addr_t dma_handle)
977{
978 if (vring_use_dma_api(vdev)) {
979 dma_free_coherent(vdev->dev.parent, size, queue, dma_handle);
980 } else {
981 free_pages_exact(queue, PAGE_ALIGN(size));
982 }
983}
984
985struct virtqueue *vring_create_virtqueue(
986 unsigned int index,
987 unsigned int num,
988 unsigned int vring_align,
989 struct virtio_device *vdev,
990 bool weak_barriers,
991 bool may_reduce_num,
992 bool (*notify)(struct virtqueue *),
993 void (*callback)(struct virtqueue *),
994 const char *name)
995{
996 struct virtqueue *vq;
997 void *queue;
998 dma_addr_t dma_addr;
999 size_t queue_size_in_bytes;
1000 struct vring vring;
1001
1002 /* We assume num is a power of 2. */
1003 if (num & (num - 1)) {
1004 dev_warn(&vdev->dev, "Bad virtqueue length %u\n", num);
1005 return NULL;
1006 }
1007
1008 /* TODO: allocate each queue chunk individually */
1009 for (; num && vring_size(num, vring_align) > PAGE_SIZE; num /= 2) {
1010 queue = vring_alloc_queue(vdev, vring_size(num, vring_align),
1011 &dma_addr,
1012 GFP_KERNEL|__GFP_NOWARN|__GFP_ZERO);
1013 if (queue)
1014 break;
1015 }
1016
1017 if (!num)
1018 return NULL;
1019
1020 if (!queue) {
1021 /* Try to get a single page. You are my only hope! */
1022 queue = vring_alloc_queue(vdev, vring_size(num, vring_align),
1023 &dma_addr, GFP_KERNEL|__GFP_ZERO);
1024 }
1025 if (!queue)
1026 return NULL;
1027
1028 queue_size_in_bytes = vring_size(num, vring_align);
1029 vring_init(&vring, num, queue, vring_align);
1030
1031 vq = __vring_new_virtqueue(index, vring, vdev, weak_barriers,
1032 notify, callback, name);
1033 if (!vq) {
1034 vring_free_queue(vdev, queue_size_in_bytes, queue,
1035 dma_addr);
1036 return NULL;
1037 }
1038
1039 to_vvq(vq)->queue_dma_addr = dma_addr;
1040 to_vvq(vq)->queue_size_in_bytes = queue_size_in_bytes;
1041 to_vvq(vq)->we_own_ring = true;
1042
1043 return vq;
1044}
1045EXPORT_SYMBOL_GPL(vring_create_virtqueue);
1046
1047struct virtqueue *vring_new_virtqueue(unsigned int index,
1048 unsigned int num,
1049 unsigned int vring_align,
1050 struct virtio_device *vdev,
1051 bool weak_barriers,
1052 void *pages,
1053 bool (*notify)(struct virtqueue *vq),
1054 void (*callback)(struct virtqueue *vq),
1055 const char *name)
1056{
1057 struct vring vring;
1058 vring_init(&vring, num, pages, vring_align);
1059 return __vring_new_virtqueue(index, vring, vdev, weak_barriers,
1060 notify, callback, name);
1061}
Rusty Russellc6fd4702008-02-04 23:50:05 -05001062EXPORT_SYMBOL_GPL(vring_new_virtqueue);
Rusty Russell0a8a69d2007-10-22 11:03:40 +10001063
Andy Lutomirski2a2d1382016-02-02 21:46:37 -08001064void vring_del_virtqueue(struct virtqueue *_vq)
Rusty Russell0a8a69d2007-10-22 11:03:40 +10001065{
Andy Lutomirski2a2d1382016-02-02 21:46:37 -08001066 struct vring_virtqueue *vq = to_vvq(_vq);
1067
1068 if (vq->we_own_ring) {
1069 vring_free_queue(vq->vq.vdev, vq->queue_size_in_bytes,
1070 vq->vring.desc, vq->queue_dma_addr);
1071 }
1072 list_del(&_vq->list);
1073 kfree(vq);
Rusty Russell0a8a69d2007-10-22 11:03:40 +10001074}
Rusty Russellc6fd4702008-02-04 23:50:05 -05001075EXPORT_SYMBOL_GPL(vring_del_virtqueue);
Rusty Russell0a8a69d2007-10-22 11:03:40 +10001076
Rusty Russelle34f8722008-07-25 12:06:13 -05001077/* Manipulates transport-specific feature bits. */
1078void vring_transport_features(struct virtio_device *vdev)
1079{
1080 unsigned int i;
1081
1082 for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++) {
1083 switch (i) {
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +01001084 case VIRTIO_RING_F_INDIRECT_DESC:
1085 break;
Michael S. Tsirkina5c262c2011-05-20 02:10:44 +03001086 case VIRTIO_RING_F_EVENT_IDX:
1087 break;
Michael S. Tsirkin747ae342014-12-01 15:52:40 +02001088 case VIRTIO_F_VERSION_1:
1089 break;
Rusty Russelle34f8722008-07-25 12:06:13 -05001090 default:
1091 /* We don't understand this bit. */
Michael S. Tsirkine16e12b2014-10-07 16:39:42 +02001092 __virtio_clear_bit(vdev, i);
Rusty Russelle34f8722008-07-25 12:06:13 -05001093 }
1094 }
1095}
1096EXPORT_SYMBOL_GPL(vring_transport_features);
1097
Rusty Russell5dfc1762012-01-12 15:44:42 +10301098/**
1099 * virtqueue_get_vring_size - return the size of the virtqueue's vring
1100 * @vq: the struct virtqueue containing the vring of interest.
1101 *
1102 * Returns the size of the vring. This is mainly used for boasting to
1103 * userspace. Unlike other operations, this need not be serialized.
1104 */
Rick Jones8f9f4662011-10-19 08:10:59 +00001105unsigned int virtqueue_get_vring_size(struct virtqueue *_vq)
1106{
1107
1108 struct vring_virtqueue *vq = to_vvq(_vq);
1109
1110 return vq->vring.num;
1111}
1112EXPORT_SYMBOL_GPL(virtqueue_get_vring_size);
1113
Heinz Graalfsb3b32c92013-10-29 09:40:19 +10301114bool virtqueue_is_broken(struct virtqueue *_vq)
1115{
1116 struct vring_virtqueue *vq = to_vvq(_vq);
1117
1118 return vq->broken;
1119}
1120EXPORT_SYMBOL_GPL(virtqueue_is_broken);
1121
Rusty Russelle2dcdfe2014-04-28 11:15:08 +09301122/*
1123 * This should prevent the device from being used, allowing drivers to
1124 * recover. You may need to grab appropriate locks to flush.
1125 */
1126void virtio_break_device(struct virtio_device *dev)
1127{
1128 struct virtqueue *_vq;
1129
1130 list_for_each_entry(_vq, &dev->vqs, list) {
1131 struct vring_virtqueue *vq = to_vvq(_vq);
1132 vq->broken = true;
1133 }
1134}
1135EXPORT_SYMBOL_GPL(virtio_break_device);
1136
Andy Lutomirski2a2d1382016-02-02 21:46:37 -08001137dma_addr_t virtqueue_get_desc_addr(struct virtqueue *_vq)
Cornelia Huck89062652014-10-07 16:39:47 +02001138{
1139 struct vring_virtqueue *vq = to_vvq(_vq);
1140
Andy Lutomirski2a2d1382016-02-02 21:46:37 -08001141 BUG_ON(!vq->we_own_ring);
Cornelia Huck89062652014-10-07 16:39:47 +02001142
Andy Lutomirski2a2d1382016-02-02 21:46:37 -08001143 return vq->queue_dma_addr;
1144}
1145EXPORT_SYMBOL_GPL(virtqueue_get_desc_addr);
1146
1147dma_addr_t virtqueue_get_avail_addr(struct virtqueue *_vq)
Cornelia Huck89062652014-10-07 16:39:47 +02001148{
1149 struct vring_virtqueue *vq = to_vvq(_vq);
1150
Andy Lutomirski2a2d1382016-02-02 21:46:37 -08001151 BUG_ON(!vq->we_own_ring);
1152
1153 return vq->queue_dma_addr +
1154 ((char *)vq->vring.avail - (char *)vq->vring.desc);
Cornelia Huck89062652014-10-07 16:39:47 +02001155}
Andy Lutomirski2a2d1382016-02-02 21:46:37 -08001156EXPORT_SYMBOL_GPL(virtqueue_get_avail_addr);
1157
1158dma_addr_t virtqueue_get_used_addr(struct virtqueue *_vq)
1159{
1160 struct vring_virtqueue *vq = to_vvq(_vq);
1161
1162 BUG_ON(!vq->we_own_ring);
1163
1164 return vq->queue_dma_addr +
1165 ((char *)vq->vring.used - (char *)vq->vring.desc);
1166}
1167EXPORT_SYMBOL_GPL(virtqueue_get_used_addr);
1168
1169const struct vring *virtqueue_get_vring(struct virtqueue *vq)
1170{
1171 return &to_vvq(vq)->vring;
1172}
1173EXPORT_SYMBOL_GPL(virtqueue_get_vring);
Cornelia Huck89062652014-10-07 16:39:47 +02001174
Rusty Russellc6fd4702008-02-04 23:50:05 -05001175MODULE_LICENSE("GPL");