blob: 71e16b53e9c1856460d9feb2943be6e415a67079 [file] [log] [blame]
Thomas Gleixnerfd534e92019-05-23 11:14:39 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Rusty Russell0a8a69d2007-10-22 11:03:40 +10002/* Virtio ring implementation.
3 *
4 * Copyright 2007 Rusty Russell IBM Corporation
Rusty Russell0a8a69d2007-10-22 11:03:40 +10005 */
6#include <linux/virtio.h>
7#include <linux/virtio_ring.h>
Rusty Russelle34f8722008-07-25 12:06:13 -05008#include <linux/virtio_config.h>
Rusty Russell0a8a69d2007-10-22 11:03:40 +10009#include <linux/device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Paul Gortmakerb5a2c4f2011-07-03 16:20:30 -040011#include <linux/module.h>
Rusty Russelle93300b2012-01-12 15:44:43 +103012#include <linux/hrtimer.h>
Andy Lutomirski780bc792016-02-02 21:46:36 -080013#include <linux/dma-mapping.h>
Andy Lutomirski78fe3982016-02-02 21:46:40 -080014#include <xen/xen.h>
Rusty Russell0a8a69d2007-10-22 11:03:40 +100015
16#ifdef DEBUG
17/* For development, we want to crash whenever the ring is screwed. */
Rusty Russell9499f5e2009-06-12 22:16:35 -060018#define BAD_RING(_vq, fmt, args...) \
19 do { \
20 dev_err(&(_vq)->vq.vdev->dev, \
21 "%s:"fmt, (_vq)->vq.name, ##args); \
22 BUG(); \
23 } while (0)
Rusty Russellc5f841f2009-03-30 21:55:22 -060024/* Caller is supposed to guarantee no reentry. */
25#define START_USE(_vq) \
26 do { \
27 if ((_vq)->in_use) \
Rusty Russell9499f5e2009-06-12 22:16:35 -060028 panic("%s:in_use = %i\n", \
29 (_vq)->vq.name, (_vq)->in_use); \
Rusty Russellc5f841f2009-03-30 21:55:22 -060030 (_vq)->in_use = __LINE__; \
Rusty Russell9499f5e2009-06-12 22:16:35 -060031 } while (0)
Roel Kluin3a35ce72009-01-22 16:42:57 +010032#define END_USE(_vq) \
Rusty Russell97a545a2010-02-24 14:22:22 -060033 do { BUG_ON(!(_vq)->in_use); (_vq)->in_use = 0; } while(0)
Tiwei Bie4d6a1052018-11-21 18:03:22 +080034#define LAST_ADD_TIME_UPDATE(_vq) \
35 do { \
36 ktime_t now = ktime_get(); \
37 \
38 /* No kick or get, with .1 second between? Warn. */ \
39 if ((_vq)->last_add_time_valid) \
40 WARN_ON(ktime_to_ms(ktime_sub(now, \
41 (_vq)->last_add_time)) > 100); \
42 (_vq)->last_add_time = now; \
43 (_vq)->last_add_time_valid = true; \
44 } while (0)
45#define LAST_ADD_TIME_CHECK(_vq) \
46 do { \
47 if ((_vq)->last_add_time_valid) { \
48 WARN_ON(ktime_to_ms(ktime_sub(ktime_get(), \
49 (_vq)->last_add_time)) > 100); \
50 } \
51 } while (0)
52#define LAST_ADD_TIME_INVALID(_vq) \
53 ((_vq)->last_add_time_valid = false)
Rusty Russell0a8a69d2007-10-22 11:03:40 +100054#else
Rusty Russell9499f5e2009-06-12 22:16:35 -060055#define BAD_RING(_vq, fmt, args...) \
56 do { \
57 dev_err(&_vq->vq.vdev->dev, \
58 "%s:"fmt, (_vq)->vq.name, ##args); \
59 (_vq)->broken = true; \
60 } while (0)
Rusty Russell0a8a69d2007-10-22 11:03:40 +100061#define START_USE(vq)
62#define END_USE(vq)
Tiwei Bie4d6a1052018-11-21 18:03:22 +080063#define LAST_ADD_TIME_UPDATE(vq)
64#define LAST_ADD_TIME_CHECK(vq)
65#define LAST_ADD_TIME_INVALID(vq)
Rusty Russell0a8a69d2007-10-22 11:03:40 +100066#endif
67
Tiwei Biecbeedb72018-11-21 18:03:24 +080068struct vring_desc_state_split {
Andy Lutomirski780bc792016-02-02 21:46:36 -080069 void *data; /* Data for callback. */
70 struct vring_desc *indir_desc; /* Indirect descriptor, if any. */
71};
72
Tiwei Bie1ce9e602018-11-21 18:03:27 +080073struct vring_desc_state_packed {
74 void *data; /* Data for callback. */
75 struct vring_packed_desc *indir_desc; /* Indirect descriptor, if any. */
76 u16 num; /* Descriptor list length. */
77 u16 next; /* The next desc state in a list. */
78 u16 last; /* The last desc state in a list. */
79};
80
81struct vring_desc_extra_packed {
82 dma_addr_t addr; /* Buffer DMA addr. */
83 u32 len; /* Buffer length. */
84 u16 flags; /* Descriptor flags. */
85};
86
Michael S. Tsirkin43b4f722015-01-15 13:33:31 +020087struct vring_virtqueue {
Rusty Russell0a8a69d2007-10-22 11:03:40 +100088 struct virtqueue vq;
89
Tiwei Bie1ce9e602018-11-21 18:03:27 +080090 /* Is this a packed ring? */
91 bool packed_ring;
92
Tiwei Biefb3fba62018-11-21 18:03:26 +080093 /* Is DMA API used? */
94 bool use_dma_api;
95
Rusty Russell7b21e342012-01-12 15:44:42 +103096 /* Can we use weak barriers? */
97 bool weak_barriers;
98
Rusty Russell0a8a69d2007-10-22 11:03:40 +100099 /* Other side has made a mess, don't try any more. */
100 bool broken;
101
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100102 /* Host supports indirect buffers */
103 bool indirect;
104
Michael S. Tsirkina5c262c2011-05-20 02:10:44 +0300105 /* Host publishes avail event idx */
106 bool event;
107
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000108 /* Head of free buffer list. */
109 unsigned int free_head;
110 /* Number we've added since last sync. */
111 unsigned int num_added;
112
113 /* Last used index we've seen. */
Anthony Liguori1bc49532007-11-07 15:49:24 -0600114 u16 last_used_idx;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000115
Tiwei Bie1ce9e602018-11-21 18:03:27 +0800116 union {
117 /* Available for split ring */
118 struct {
119 /* Actual memory layout for this queue. */
120 struct vring vring;
Venkatesh Srinivasf277ec42015-11-10 16:21:07 -0800121
Tiwei Bie1ce9e602018-11-21 18:03:27 +0800122 /* Last written value to avail->flags */
123 u16 avail_flags_shadow;
Tiwei Biee593bf92018-11-21 18:03:21 +0800124
Tiwei Bie1ce9e602018-11-21 18:03:27 +0800125 /*
126 * Last written value to avail->idx in
127 * guest byte order.
128 */
129 u16 avail_idx_shadow;
Tiwei Biecbeedb72018-11-21 18:03:24 +0800130
Tiwei Bie1ce9e602018-11-21 18:03:27 +0800131 /* Per-descriptor state. */
132 struct vring_desc_state_split *desc_state;
Tiwei Bied79dca72018-11-21 18:03:25 +0800133
Tiwei Bie1ce9e602018-11-21 18:03:27 +0800134 /* DMA address and size information */
135 dma_addr_t queue_dma_addr;
136 size_t queue_size_in_bytes;
137 } split;
138
139 /* Available for packed ring */
140 struct {
141 /* Actual memory layout for this queue. */
Michael S. Tsirkin9c0644e2019-02-01 17:13:57 -0500142 struct {
143 unsigned int num;
144 struct vring_packed_desc *desc;
145 struct vring_packed_desc_event *driver;
146 struct vring_packed_desc_event *device;
147 } vring;
Tiwei Bie1ce9e602018-11-21 18:03:27 +0800148
149 /* Driver ring wrap counter. */
150 bool avail_wrap_counter;
151
152 /* Device ring wrap counter. */
153 bool used_wrap_counter;
154
155 /* Avail used flags. */
156 u16 avail_used_flags;
157
158 /* Index of the next avail descriptor. */
159 u16 next_avail_idx;
160
161 /*
162 * Last written value to driver->flags in
163 * guest byte order.
164 */
165 u16 event_flags_shadow;
166
167 /* Per-descriptor state. */
168 struct vring_desc_state_packed *desc_state;
169 struct vring_desc_extra_packed *desc_extra;
170
171 /* DMA address and size information */
172 dma_addr_t ring_dma_addr;
173 dma_addr_t driver_event_dma_addr;
174 dma_addr_t device_event_dma_addr;
175 size_t ring_size_in_bytes;
176 size_t event_size_in_bytes;
177 } packed;
178 };
Venkatesh Srinivasf277ec42015-11-10 16:21:07 -0800179
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000180 /* How to notify other side. FIXME: commonalize hcalls! */
Heinz Graalfs46f9c2b2013-10-29 09:38:50 +1030181 bool (*notify)(struct virtqueue *vq);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000182
Andy Lutomirski2a2d1382016-02-02 21:46:37 -0800183 /* DMA, allocation, and size information */
184 bool we_own_ring;
Andy Lutomirski2a2d1382016-02-02 21:46:37 -0800185
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000186#ifdef DEBUG
187 /* They're supposed to lock for us. */
188 unsigned int in_use;
Rusty Russelle93300b2012-01-12 15:44:43 +1030189
190 /* Figure out if their kicks are too delayed. */
191 bool last_add_time_valid;
192 ktime_t last_add_time;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000193#endif
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000194};
195
Tiwei Biee6f633e2018-11-21 18:03:20 +0800196
197/*
198 * Helpers.
199 */
200
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000201#define to_vvq(_vq) container_of(_vq, struct vring_virtqueue, vq)
202
Tiwei Bie2f18c2d2018-11-21 18:03:23 +0800203static inline bool virtqueue_use_indirect(struct virtqueue *_vq,
204 unsigned int total_sg)
205{
206 struct vring_virtqueue *vq = to_vvq(_vq);
207
208 /*
209 * If the host supports indirect descriptor tables, and we have multiple
210 * buffers, then go indirect. FIXME: tune this threshold
211 */
212 return (vq->indirect && total_sg > 1 && vq->vq.num_free);
213}
214
Andy Lutomirskid26c96c2016-02-02 21:46:35 -0800215/*
Michael S. Tsirkin1a937692016-04-18 12:58:14 +0300216 * Modern virtio devices have feature bits to specify whether they need a
217 * quirk and bypass the IOMMU. If not there, just use the DMA API.
218 *
219 * If there, the interaction between virtio and DMA API is messy.
Andy Lutomirskid26c96c2016-02-02 21:46:35 -0800220 *
221 * On most systems with virtio, physical addresses match bus addresses,
222 * and it doesn't particularly matter whether we use the DMA API.
223 *
224 * On some systems, including Xen and any system with a physical device
225 * that speaks virtio behind a physical IOMMU, we must use the DMA API
226 * for virtio DMA to work at all.
227 *
228 * On other systems, including SPARC and PPC64, virtio-pci devices are
229 * enumerated as though they are behind an IOMMU, but the virtio host
230 * ignores the IOMMU, so we must either pretend that the IOMMU isn't
231 * there or somehow map everything as the identity.
232 *
233 * For the time being, we preserve historic behavior and bypass the DMA
234 * API.
Michael S. Tsirkin1a937692016-04-18 12:58:14 +0300235 *
236 * TODO: install a per-device DMA ops structure that does the right thing
237 * taking into account all the above quirks, and use the DMA API
238 * unconditionally on data path.
Andy Lutomirskid26c96c2016-02-02 21:46:35 -0800239 */
240
241static bool vring_use_dma_api(struct virtio_device *vdev)
242{
Michael S. Tsirkin24b68422020-06-24 19:17:04 -0400243 if (!virtio_has_dma_quirk(vdev))
Michael S. Tsirkin1a937692016-04-18 12:58:14 +0300244 return true;
245
246 /* Otherwise, we are left to guess. */
Andy Lutomirski78fe3982016-02-02 21:46:40 -0800247 /*
248 * In theory, it's possible to have a buggy QEMU-supposed
249 * emulated Q35 IOMMU and Xen enabled at the same time. On
250 * such a configuration, virtio has never worked and will
251 * not work without an even larger kludge. Instead, enable
252 * the DMA API if we're a Xen guest, which at least allows
253 * all of the sensible Xen configurations to work correctly.
254 */
255 if (xen_domain())
256 return true;
257
Andy Lutomirskid26c96c2016-02-02 21:46:35 -0800258 return false;
259}
260
Joerg Roedele6d6dd62019-02-07 12:59:16 +0100261size_t virtio_max_dma_size(struct virtio_device *vdev)
262{
263 size_t max_segment_size = SIZE_MAX;
264
265 if (vring_use_dma_api(vdev))
266 max_segment_size = dma_max_mapping_size(&vdev->dev);
267
268 return max_segment_size;
269}
270EXPORT_SYMBOL_GPL(virtio_max_dma_size);
271
Tiwei Bied79dca72018-11-21 18:03:25 +0800272static void *vring_alloc_queue(struct virtio_device *vdev, size_t size,
273 dma_addr_t *dma_handle, gfp_t flag)
274{
275 if (vring_use_dma_api(vdev)) {
276 return dma_alloc_coherent(vdev->dev.parent, size,
277 dma_handle, flag);
278 } else {
279 void *queue = alloc_pages_exact(PAGE_ALIGN(size), flag);
280
281 if (queue) {
282 phys_addr_t phys_addr = virt_to_phys(queue);
283 *dma_handle = (dma_addr_t)phys_addr;
284
285 /*
286 * Sanity check: make sure we dind't truncate
287 * the address. The only arches I can find that
288 * have 64-bit phys_addr_t but 32-bit dma_addr_t
289 * are certain non-highmem MIPS and x86
290 * configurations, but these configurations
291 * should never allocate physical pages above 32
292 * bits, so this is fine. Just in case, throw a
293 * warning and abort if we end up with an
294 * unrepresentable address.
295 */
296 if (WARN_ON_ONCE(*dma_handle != phys_addr)) {
297 free_pages_exact(queue, PAGE_ALIGN(size));
298 return NULL;
299 }
300 }
301 return queue;
302 }
303}
304
305static void vring_free_queue(struct virtio_device *vdev, size_t size,
306 void *queue, dma_addr_t dma_handle)
307{
308 if (vring_use_dma_api(vdev))
309 dma_free_coherent(vdev->dev.parent, size, queue, dma_handle);
310 else
311 free_pages_exact(queue, PAGE_ALIGN(size));
312}
313
Andy Lutomirski780bc792016-02-02 21:46:36 -0800314/*
315 * The DMA ops on various arches are rather gnarly right now, and
316 * making all of the arch DMA ops work on the vring device itself
317 * is a mess. For now, we use the parent device for DMA ops.
318 */
Michael S. Tsirkin75bfa812016-10-31 00:38:21 +0200319static inline struct device *vring_dma_dev(const struct vring_virtqueue *vq)
Andy Lutomirski780bc792016-02-02 21:46:36 -0800320{
321 return vq->vq.vdev->dev.parent;
322}
323
324/* Map one sg entry. */
325static dma_addr_t vring_map_one_sg(const struct vring_virtqueue *vq,
326 struct scatterlist *sg,
327 enum dma_data_direction direction)
328{
Tiwei Biefb3fba62018-11-21 18:03:26 +0800329 if (!vq->use_dma_api)
Andy Lutomirski780bc792016-02-02 21:46:36 -0800330 return (dma_addr_t)sg_phys(sg);
331
332 /*
333 * We can't use dma_map_sg, because we don't use scatterlists in
334 * the way it expects (we don't guarantee that the scatterlist
335 * will exist for the lifetime of the mapping).
336 */
337 return dma_map_page(vring_dma_dev(vq),
338 sg_page(sg), sg->offset, sg->length,
339 direction);
340}
341
342static dma_addr_t vring_map_single(const struct vring_virtqueue *vq,
343 void *cpu_addr, size_t size,
344 enum dma_data_direction direction)
345{
Tiwei Biefb3fba62018-11-21 18:03:26 +0800346 if (!vq->use_dma_api)
Andy Lutomirski780bc792016-02-02 21:46:36 -0800347 return (dma_addr_t)virt_to_phys(cpu_addr);
348
349 return dma_map_single(vring_dma_dev(vq),
350 cpu_addr, size, direction);
351}
352
Tiwei Biee6f633e2018-11-21 18:03:20 +0800353static int vring_mapping_error(const struct vring_virtqueue *vq,
354 dma_addr_t addr)
355{
Tiwei Biefb3fba62018-11-21 18:03:26 +0800356 if (!vq->use_dma_api)
Tiwei Biee6f633e2018-11-21 18:03:20 +0800357 return 0;
358
359 return dma_mapping_error(vring_dma_dev(vq), addr);
360}
361
362
363/*
364 * Split ring specific functions - *_split().
365 */
366
Tiwei Bie138fd252018-11-21 18:03:19 +0800367static void vring_unmap_one_split(const struct vring_virtqueue *vq,
368 struct vring_desc *desc)
Andy Lutomirski780bc792016-02-02 21:46:36 -0800369{
370 u16 flags;
371
Tiwei Biefb3fba62018-11-21 18:03:26 +0800372 if (!vq->use_dma_api)
Andy Lutomirski780bc792016-02-02 21:46:36 -0800373 return;
374
375 flags = virtio16_to_cpu(vq->vq.vdev, desc->flags);
376
377 if (flags & VRING_DESC_F_INDIRECT) {
378 dma_unmap_single(vring_dma_dev(vq),
379 virtio64_to_cpu(vq->vq.vdev, desc->addr),
380 virtio32_to_cpu(vq->vq.vdev, desc->len),
381 (flags & VRING_DESC_F_WRITE) ?
382 DMA_FROM_DEVICE : DMA_TO_DEVICE);
383 } else {
384 dma_unmap_page(vring_dma_dev(vq),
385 virtio64_to_cpu(vq->vq.vdev, desc->addr),
386 virtio32_to_cpu(vq->vq.vdev, desc->len),
387 (flags & VRING_DESC_F_WRITE) ?
388 DMA_FROM_DEVICE : DMA_TO_DEVICE);
389 }
390}
391
Tiwei Bie138fd252018-11-21 18:03:19 +0800392static struct vring_desc *alloc_indirect_split(struct virtqueue *_vq,
393 unsigned int total_sg,
394 gfp_t gfp)
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100395{
396 struct vring_desc *desc;
Rusty Russellb25bd252014-09-11 10:17:38 +0930397 unsigned int i;
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100398
Will Deaconb92b1b82012-10-19 14:03:33 +0100399 /*
400 * We require lowmem mappings for the descriptors because
401 * otherwise virt_to_phys will give us bogus addresses in the
402 * virtqueue.
403 */
Michal Hocko82107532015-12-01 15:32:49 +0100404 gfp &= ~__GFP_HIGHMEM;
Will Deaconb92b1b82012-10-19 14:03:33 +0100405
Kees Cook6da2ec52018-06-12 13:55:00 -0700406 desc = kmalloc_array(total_sg, sizeof(struct vring_desc), gfp);
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100407 if (!desc)
Rusty Russellb25bd252014-09-11 10:17:38 +0930408 return NULL;
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100409
Rusty Russellb25bd252014-09-11 10:17:38 +0930410 for (i = 0; i < total_sg; i++)
Michael S. Tsirkin00e6f3d2014-10-22 15:42:09 +0300411 desc[i].next = cpu_to_virtio16(_vq->vdev, i + 1);
Rusty Russellb25bd252014-09-11 10:17:38 +0930412 return desc;
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100413}
414
Tiwei Bie138fd252018-11-21 18:03:19 +0800415static inline int virtqueue_add_split(struct virtqueue *_vq,
416 struct scatterlist *sgs[],
417 unsigned int total_sg,
418 unsigned int out_sgs,
419 unsigned int in_sgs,
420 void *data,
421 void *ctx,
422 gfp_t gfp)
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000423{
424 struct vring_virtqueue *vq = to_vvq(_vq);
Rusty Russell13816c72013-03-20 15:37:09 +1030425 struct scatterlist *sg;
Rusty Russellb25bd252014-09-11 10:17:38 +0930426 struct vring_desc *desc;
Kees Cook3f649ab2020-06-03 13:09:38 -0700427 unsigned int i, n, avail, descs_used, prev, err_idx;
Michael S. Tsirkin1fe9b6f2010-07-26 16:55:30 +0930428 int head;
Rusty Russellb25bd252014-09-11 10:17:38 +0930429 bool indirect;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000430
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100431 START_USE(vq);
432
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000433 BUG_ON(data == NULL);
Michael S. Tsirkin5a08b042017-02-07 06:15:13 +0200434 BUG_ON(ctx && vq->indirect);
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100435
Rusty Russell70670442014-03-13 11:23:40 +1030436 if (unlikely(vq->broken)) {
437 END_USE(vq);
438 return -EIO;
439 }
440
Tiwei Bie4d6a1052018-11-21 18:03:22 +0800441 LAST_ADD_TIME_UPDATE(vq);
Rusty Russelle93300b2012-01-12 15:44:43 +1030442
Rusty Russell13816c72013-03-20 15:37:09 +1030443 BUG_ON(total_sg == 0);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000444
Rusty Russellb25bd252014-09-11 10:17:38 +0930445 head = vq->free_head;
446
Tiwei Bie2f18c2d2018-11-21 18:03:23 +0800447 if (virtqueue_use_indirect(_vq, total_sg))
Tiwei Bie138fd252018-11-21 18:03:19 +0800448 desc = alloc_indirect_split(_vq, total_sg, gfp);
Richard W.M. Jones44ed8082017-08-10 17:56:51 +0100449 else {
Rusty Russellb25bd252014-09-11 10:17:38 +0930450 desc = NULL;
Tiwei Biee593bf92018-11-21 18:03:21 +0800451 WARN_ON_ONCE(total_sg > vq->split.vring.num && !vq->indirect);
Richard W.M. Jones44ed8082017-08-10 17:56:51 +0100452 }
Rusty Russellb25bd252014-09-11 10:17:38 +0930453
454 if (desc) {
455 /* Use a single buffer which doesn't continue */
Andy Lutomirski780bc792016-02-02 21:46:36 -0800456 indirect = true;
Rusty Russellb25bd252014-09-11 10:17:38 +0930457 /* Set up rest to use this indirect table. */
458 i = 0;
459 descs_used = 1;
Rusty Russellb25bd252014-09-11 10:17:38 +0930460 } else {
Andy Lutomirski780bc792016-02-02 21:46:36 -0800461 indirect = false;
Tiwei Biee593bf92018-11-21 18:03:21 +0800462 desc = vq->split.vring.desc;
Rusty Russellb25bd252014-09-11 10:17:38 +0930463 i = head;
464 descs_used = total_sg;
Rusty Russellb25bd252014-09-11 10:17:38 +0930465 }
466
467 if (vq->vq.num_free < descs_used) {
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000468 pr_debug("Can't add buf len %i - avail = %i\n",
Rusty Russellb25bd252014-09-11 10:17:38 +0930469 descs_used, vq->vq.num_free);
Rusty Russell44653ea2008-07-25 12:06:04 -0500470 /* FIXME: for historical reasons, we force a notify here if
471 * there are outgoing parts to the buffer. Presumably the
472 * host should service the ring ASAP. */
Rusty Russell13816c72013-03-20 15:37:09 +1030473 if (out_sgs)
Rusty Russell44653ea2008-07-25 12:06:04 -0500474 vq->notify(&vq->vq);
Wei Yongjun58625ed2016-08-02 14:16:31 +0000475 if (indirect)
476 kfree(desc);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000477 END_USE(vq);
478 return -ENOSPC;
479 }
480
Rusty Russell13816c72013-03-20 15:37:09 +1030481 for (n = 0; n < out_sgs; n++) {
Rusty Russelleeebf9b2014-09-11 10:17:37 +0930482 for (sg = sgs[n]; sg; sg = sg_next(sg)) {
Andy Lutomirski780bc792016-02-02 21:46:36 -0800483 dma_addr_t addr = vring_map_one_sg(vq, sg, DMA_TO_DEVICE);
484 if (vring_mapping_error(vq, addr))
485 goto unmap_release;
486
Michael S. Tsirkin00e6f3d2014-10-22 15:42:09 +0300487 desc[i].flags = cpu_to_virtio16(_vq->vdev, VRING_DESC_F_NEXT);
Andy Lutomirski780bc792016-02-02 21:46:36 -0800488 desc[i].addr = cpu_to_virtio64(_vq->vdev, addr);
Michael S. Tsirkin00e6f3d2014-10-22 15:42:09 +0300489 desc[i].len = cpu_to_virtio32(_vq->vdev, sg->length);
Rusty Russell13816c72013-03-20 15:37:09 +1030490 prev = i;
Michael S. Tsirkin00e6f3d2014-10-22 15:42:09 +0300491 i = virtio16_to_cpu(_vq->vdev, desc[i].next);
Rusty Russell13816c72013-03-20 15:37:09 +1030492 }
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000493 }
Rusty Russell13816c72013-03-20 15:37:09 +1030494 for (; n < (out_sgs + in_sgs); n++) {
Rusty Russelleeebf9b2014-09-11 10:17:37 +0930495 for (sg = sgs[n]; sg; sg = sg_next(sg)) {
Andy Lutomirski780bc792016-02-02 21:46:36 -0800496 dma_addr_t addr = vring_map_one_sg(vq, sg, DMA_FROM_DEVICE);
497 if (vring_mapping_error(vq, addr))
498 goto unmap_release;
499
Michael S. Tsirkin00e6f3d2014-10-22 15:42:09 +0300500 desc[i].flags = cpu_to_virtio16(_vq->vdev, VRING_DESC_F_NEXT | VRING_DESC_F_WRITE);
Andy Lutomirski780bc792016-02-02 21:46:36 -0800501 desc[i].addr = cpu_to_virtio64(_vq->vdev, addr);
Michael S. Tsirkin00e6f3d2014-10-22 15:42:09 +0300502 desc[i].len = cpu_to_virtio32(_vq->vdev, sg->length);
Rusty Russell13816c72013-03-20 15:37:09 +1030503 prev = i;
Michael S. Tsirkin00e6f3d2014-10-22 15:42:09 +0300504 i = virtio16_to_cpu(_vq->vdev, desc[i].next);
Rusty Russell13816c72013-03-20 15:37:09 +1030505 }
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000506 }
507 /* Last one doesn't continue. */
Michael S. Tsirkin00e6f3d2014-10-22 15:42:09 +0300508 desc[prev].flags &= cpu_to_virtio16(_vq->vdev, ~VRING_DESC_F_NEXT);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000509
Andy Lutomirski780bc792016-02-02 21:46:36 -0800510 if (indirect) {
511 /* Now that the indirect table is filled in, map it. */
512 dma_addr_t addr = vring_map_single(
513 vq, desc, total_sg * sizeof(struct vring_desc),
514 DMA_TO_DEVICE);
515 if (vring_mapping_error(vq, addr))
516 goto unmap_release;
517
Tiwei Biee593bf92018-11-21 18:03:21 +0800518 vq->split.vring.desc[head].flags = cpu_to_virtio16(_vq->vdev,
519 VRING_DESC_F_INDIRECT);
520 vq->split.vring.desc[head].addr = cpu_to_virtio64(_vq->vdev,
521 addr);
Andy Lutomirski780bc792016-02-02 21:46:36 -0800522
Tiwei Biee593bf92018-11-21 18:03:21 +0800523 vq->split.vring.desc[head].len = cpu_to_virtio32(_vq->vdev,
524 total_sg * sizeof(struct vring_desc));
Andy Lutomirski780bc792016-02-02 21:46:36 -0800525 }
526
527 /* We're using some buffers from the free list. */
528 vq->vq.num_free -= descs_used;
529
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000530 /* Update free pointer */
Rusty Russellb25bd252014-09-11 10:17:38 +0930531 if (indirect)
Tiwei Biee593bf92018-11-21 18:03:21 +0800532 vq->free_head = virtio16_to_cpu(_vq->vdev,
533 vq->split.vring.desc[head].next);
Rusty Russellb25bd252014-09-11 10:17:38 +0930534 else
535 vq->free_head = i;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000536
Andy Lutomirski780bc792016-02-02 21:46:36 -0800537 /* Store token and indirect buffer state. */
Tiwei Biecbeedb72018-11-21 18:03:24 +0800538 vq->split.desc_state[head].data = data;
Andy Lutomirski780bc792016-02-02 21:46:36 -0800539 if (indirect)
Tiwei Biecbeedb72018-11-21 18:03:24 +0800540 vq->split.desc_state[head].indir_desc = desc;
Jason Wang87646a32017-07-19 16:54:45 +0800541 else
Tiwei Biecbeedb72018-11-21 18:03:24 +0800542 vq->split.desc_state[head].indir_desc = ctx;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000543
544 /* Put entry in available array (but don't update avail->idx until they
Rusty Russell3b720b82012-01-12 15:44:43 +1030545 * do sync). */
Tiwei Biee593bf92018-11-21 18:03:21 +0800546 avail = vq->split.avail_idx_shadow & (vq->split.vring.num - 1);
547 vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000548
Rusty Russellee7cd892012-01-12 15:44:43 +1030549 /* Descriptors and available array need to be set before we expose the
550 * new available array entries. */
Rusty Russella9a0fef2013-03-18 13:22:19 +1030551 virtio_wmb(vq->weak_barriers);
Tiwei Biee593bf92018-11-21 18:03:21 +0800552 vq->split.avail_idx_shadow++;
553 vq->split.vring.avail->idx = cpu_to_virtio16(_vq->vdev,
554 vq->split.avail_idx_shadow);
Rusty Russellee7cd892012-01-12 15:44:43 +1030555 vq->num_added++;
556
Tetsuo Handa5e05bf52015-02-11 15:01:13 +1030557 pr_debug("Added buffer head %i to %p\n", head, vq);
558 END_USE(vq);
559
Rusty Russellee7cd892012-01-12 15:44:43 +1030560 /* This is very unlikely, but theoretically possible. Kick
561 * just in case. */
562 if (unlikely(vq->num_added == (1 << 16) - 1))
563 virtqueue_kick(_vq);
564
Rusty Russell98e8c6b2012-10-16 23:56:15 +1030565 return 0;
Andy Lutomirski780bc792016-02-02 21:46:36 -0800566
567unmap_release:
568 err_idx = i;
Matthias Langecf8f1692019-09-06 16:59:01 +0200569
570 if (indirect)
571 i = 0;
572 else
573 i = head;
Andy Lutomirski780bc792016-02-02 21:46:36 -0800574
575 for (n = 0; n < total_sg; n++) {
576 if (i == err_idx)
577 break;
Tiwei Bie138fd252018-11-21 18:03:19 +0800578 vring_unmap_one_split(vq, &desc[i]);
Matthias Langecf8f1692019-09-06 16:59:01 +0200579 i = virtio16_to_cpu(_vq->vdev, desc[i].next);
Andy Lutomirski780bc792016-02-02 21:46:36 -0800580 }
581
Andy Lutomirski780bc792016-02-02 21:46:36 -0800582 if (indirect)
583 kfree(desc);
584
Michael S. Tsirkin3cc36f62016-08-03 07:18:51 +0300585 END_USE(vq);
Halil Pasicf7728002019-11-14 13:46:46 +0100586 return -ENOMEM;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000587}
Rusty Russell13816c72013-03-20 15:37:09 +1030588
Tiwei Bie138fd252018-11-21 18:03:19 +0800589static bool virtqueue_kick_prepare_split(struct virtqueue *_vq)
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000590{
591 struct vring_virtqueue *vq = to_vvq(_vq);
Michael S. Tsirkina5c262c2011-05-20 02:10:44 +0300592 u16 new, old;
Rusty Russell41f03772012-01-12 15:44:43 +1030593 bool needs_kick;
594
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000595 START_USE(vq);
Jason Wanga72caae2012-01-20 16:17:08 +0800596 /* We need to expose available array entries before checking avail
597 * event. */
Rusty Russella9a0fef2013-03-18 13:22:19 +1030598 virtio_mb(vq->weak_barriers);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000599
Tiwei Biee593bf92018-11-21 18:03:21 +0800600 old = vq->split.avail_idx_shadow - vq->num_added;
601 new = vq->split.avail_idx_shadow;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000602 vq->num_added = 0;
603
Tiwei Bie4d6a1052018-11-21 18:03:22 +0800604 LAST_ADD_TIME_CHECK(vq);
605 LAST_ADD_TIME_INVALID(vq);
Rusty Russelle93300b2012-01-12 15:44:43 +1030606
Rusty Russell41f03772012-01-12 15:44:43 +1030607 if (vq->event) {
Tiwei Biee593bf92018-11-21 18:03:21 +0800608 needs_kick = vring_need_event(virtio16_to_cpu(_vq->vdev,
609 vring_avail_event(&vq->split.vring)),
Rusty Russell41f03772012-01-12 15:44:43 +1030610 new, old);
611 } else {
Tiwei Biee593bf92018-11-21 18:03:21 +0800612 needs_kick = !(vq->split.vring.used->flags &
613 cpu_to_virtio16(_vq->vdev,
614 VRING_USED_F_NO_NOTIFY));
Rusty Russell41f03772012-01-12 15:44:43 +1030615 }
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000616 END_USE(vq);
Rusty Russell41f03772012-01-12 15:44:43 +1030617 return needs_kick;
618}
Tiwei Bie138fd252018-11-21 18:03:19 +0800619
Tiwei Bie138fd252018-11-21 18:03:19 +0800620static void detach_buf_split(struct vring_virtqueue *vq, unsigned int head,
621 void **ctx)
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000622{
Andy Lutomirski780bc792016-02-02 21:46:36 -0800623 unsigned int i, j;
Gongleic60923c2016-11-22 13:51:50 +0800624 __virtio16 nextflag = cpu_to_virtio16(vq->vq.vdev, VRING_DESC_F_NEXT);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000625
626 /* Clear data ptr. */
Tiwei Biecbeedb72018-11-21 18:03:24 +0800627 vq->split.desc_state[head].data = NULL;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000628
Andy Lutomirski780bc792016-02-02 21:46:36 -0800629 /* Put back on free list: unmap first-level descriptors and find end */
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000630 i = head;
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100631
Tiwei Biee593bf92018-11-21 18:03:21 +0800632 while (vq->split.vring.desc[i].flags & nextflag) {
633 vring_unmap_one_split(vq, &vq->split.vring.desc[i]);
634 i = virtio16_to_cpu(vq->vq.vdev, vq->split.vring.desc[i].next);
Rusty Russell06ca2872012-10-16 23:56:14 +1030635 vq->vq.num_free++;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000636 }
637
Tiwei Biee593bf92018-11-21 18:03:21 +0800638 vring_unmap_one_split(vq, &vq->split.vring.desc[i]);
639 vq->split.vring.desc[i].next = cpu_to_virtio16(vq->vq.vdev,
640 vq->free_head);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000641 vq->free_head = head;
Andy Lutomirski780bc792016-02-02 21:46:36 -0800642
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000643 /* Plus final descriptor */
Rusty Russell06ca2872012-10-16 23:56:14 +1030644 vq->vq.num_free++;
Andy Lutomirski780bc792016-02-02 21:46:36 -0800645
Michael S. Tsirkin5a08b042017-02-07 06:15:13 +0200646 if (vq->indirect) {
Tiwei Biecbeedb72018-11-21 18:03:24 +0800647 struct vring_desc *indir_desc =
648 vq->split.desc_state[head].indir_desc;
Michael S. Tsirkin5a08b042017-02-07 06:15:13 +0200649 u32 len;
650
651 /* Free the indirect table, if any, now that it's unmapped. */
652 if (!indir_desc)
653 return;
654
Tiwei Biee593bf92018-11-21 18:03:21 +0800655 len = virtio32_to_cpu(vq->vq.vdev,
656 vq->split.vring.desc[head].len);
Andy Lutomirski780bc792016-02-02 21:46:36 -0800657
Tiwei Biee593bf92018-11-21 18:03:21 +0800658 BUG_ON(!(vq->split.vring.desc[head].flags &
Andy Lutomirski780bc792016-02-02 21:46:36 -0800659 cpu_to_virtio16(vq->vq.vdev, VRING_DESC_F_INDIRECT)));
660 BUG_ON(len == 0 || len % sizeof(struct vring_desc));
661
662 for (j = 0; j < len / sizeof(struct vring_desc); j++)
Tiwei Bie138fd252018-11-21 18:03:19 +0800663 vring_unmap_one_split(vq, &indir_desc[j]);
Andy Lutomirski780bc792016-02-02 21:46:36 -0800664
Michael S. Tsirkin5a08b042017-02-07 06:15:13 +0200665 kfree(indir_desc);
Tiwei Biecbeedb72018-11-21 18:03:24 +0800666 vq->split.desc_state[head].indir_desc = NULL;
Michael S. Tsirkin5a08b042017-02-07 06:15:13 +0200667 } else if (ctx) {
Tiwei Biecbeedb72018-11-21 18:03:24 +0800668 *ctx = vq->split.desc_state[head].indir_desc;
Andy Lutomirski780bc792016-02-02 21:46:36 -0800669 }
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000670}
671
Tiwei Bie138fd252018-11-21 18:03:19 +0800672static inline bool more_used_split(const struct vring_virtqueue *vq)
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000673{
Tiwei Biee593bf92018-11-21 18:03:21 +0800674 return vq->last_used_idx != virtio16_to_cpu(vq->vq.vdev,
675 vq->split.vring.used->idx);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000676}
677
Tiwei Bie138fd252018-11-21 18:03:19 +0800678static void *virtqueue_get_buf_ctx_split(struct virtqueue *_vq,
679 unsigned int *len,
680 void **ctx)
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000681{
682 struct vring_virtqueue *vq = to_vvq(_vq);
683 void *ret;
684 unsigned int i;
Rusty Russell3b720b82012-01-12 15:44:43 +1030685 u16 last_used;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000686
687 START_USE(vq);
688
Rusty Russell5ef82752008-05-02 21:50:43 -0500689 if (unlikely(vq->broken)) {
690 END_USE(vq);
691 return NULL;
692 }
693
Tiwei Bie138fd252018-11-21 18:03:19 +0800694 if (!more_used_split(vq)) {
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000695 pr_debug("No more buffers in queue\n");
696 END_USE(vq);
697 return NULL;
698 }
699
Michael S. Tsirkin2d61ba92009-10-25 15:28:53 +0200700 /* Only get used array entries after they have been exposed by host. */
Rusty Russella9a0fef2013-03-18 13:22:19 +1030701 virtio_rmb(vq->weak_barriers);
Michael S. Tsirkin2d61ba92009-10-25 15:28:53 +0200702
Tiwei Biee593bf92018-11-21 18:03:21 +0800703 last_used = (vq->last_used_idx & (vq->split.vring.num - 1));
704 i = virtio32_to_cpu(_vq->vdev,
705 vq->split.vring.used->ring[last_used].id);
706 *len = virtio32_to_cpu(_vq->vdev,
707 vq->split.vring.used->ring[last_used].len);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000708
Tiwei Biee593bf92018-11-21 18:03:21 +0800709 if (unlikely(i >= vq->split.vring.num)) {
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000710 BAD_RING(vq, "id %u out of range\n", i);
711 return NULL;
712 }
Tiwei Biecbeedb72018-11-21 18:03:24 +0800713 if (unlikely(!vq->split.desc_state[i].data)) {
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000714 BAD_RING(vq, "id %u is not a head!\n", i);
715 return NULL;
716 }
717
Tiwei Bie138fd252018-11-21 18:03:19 +0800718 /* detach_buf_split clears data, so grab it now. */
Tiwei Biecbeedb72018-11-21 18:03:24 +0800719 ret = vq->split.desc_state[i].data;
Tiwei Bie138fd252018-11-21 18:03:19 +0800720 detach_buf_split(vq, i, ctx);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000721 vq->last_used_idx++;
Michael S. Tsirkina5c262c2011-05-20 02:10:44 +0300722 /* If we expect an interrupt for the next entry, tell host
723 * by writing event index and flush out the write before
724 * the read in the next get_buf call. */
Tiwei Biee593bf92018-11-21 18:03:21 +0800725 if (!(vq->split.avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT))
Michael S. Tsirkin788e5b32015-12-17 12:20:39 +0200726 virtio_store_mb(vq->weak_barriers,
Tiwei Biee593bf92018-11-21 18:03:21 +0800727 &vring_used_event(&vq->split.vring),
Michael S. Tsirkin788e5b32015-12-17 12:20:39 +0200728 cpu_to_virtio16(_vq->vdev, vq->last_used_idx));
Michael S. Tsirkina5c262c2011-05-20 02:10:44 +0300729
Tiwei Bie4d6a1052018-11-21 18:03:22 +0800730 LAST_ADD_TIME_INVALID(vq);
Rusty Russelle93300b2012-01-12 15:44:43 +1030731
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000732 END_USE(vq);
733 return ret;
734}
Tiwei Bie138fd252018-11-21 18:03:19 +0800735
Tiwei Bie138fd252018-11-21 18:03:19 +0800736static void virtqueue_disable_cb_split(struct virtqueue *_vq)
737{
738 struct vring_virtqueue *vq = to_vvq(_vq);
739
Tiwei Biee593bf92018-11-21 18:03:21 +0800740 if (!(vq->split.avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT)) {
741 vq->split.avail_flags_shadow |= VRING_AVAIL_F_NO_INTERRUPT;
Tiwei Bie138fd252018-11-21 18:03:19 +0800742 if (!vq->event)
Tiwei Biee593bf92018-11-21 18:03:21 +0800743 vq->split.vring.avail->flags =
744 cpu_to_virtio16(_vq->vdev,
745 vq->split.avail_flags_shadow);
Tiwei Bie138fd252018-11-21 18:03:19 +0800746 }
747}
748
Tiwei Bie138fd252018-11-21 18:03:19 +0800749static unsigned virtqueue_enable_cb_prepare_split(struct virtqueue *_vq)
Michael S. Tsirkincc229882013-07-09 13:19:18 +0300750{
751 struct vring_virtqueue *vq = to_vvq(_vq);
752 u16 last_used_idx;
753
754 START_USE(vq);
755
756 /* We optimistically turn back on interrupts, then check if there was
757 * more to do. */
758 /* Depending on the VIRTIO_RING_F_EVENT_IDX feature, we need to
759 * either clear the flags bit or point the event index at the next
760 * entry. Always do both to keep code simple. */
Tiwei Biee593bf92018-11-21 18:03:21 +0800761 if (vq->split.avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT) {
762 vq->split.avail_flags_shadow &= ~VRING_AVAIL_F_NO_INTERRUPT;
Ladi Prosek0ea1e4a2016-08-31 14:00:04 +0200763 if (!vq->event)
Tiwei Biee593bf92018-11-21 18:03:21 +0800764 vq->split.vring.avail->flags =
765 cpu_to_virtio16(_vq->vdev,
766 vq->split.avail_flags_shadow);
Venkatesh Srinivasf277ec42015-11-10 16:21:07 -0800767 }
Tiwei Biee593bf92018-11-21 18:03:21 +0800768 vring_used_event(&vq->split.vring) = cpu_to_virtio16(_vq->vdev,
769 last_used_idx = vq->last_used_idx);
Michael S. Tsirkincc229882013-07-09 13:19:18 +0300770 END_USE(vq);
771 return last_used_idx;
772}
Tiwei Bie138fd252018-11-21 18:03:19 +0800773
Tiwei Bie138fd252018-11-21 18:03:19 +0800774static bool virtqueue_poll_split(struct virtqueue *_vq, unsigned last_used_idx)
775{
776 struct vring_virtqueue *vq = to_vvq(_vq);
777
778 return (u16)last_used_idx != virtio16_to_cpu(_vq->vdev,
Tiwei Biee593bf92018-11-21 18:03:21 +0800779 vq->split.vring.used->idx);
Tiwei Bie138fd252018-11-21 18:03:19 +0800780}
781
Tiwei Bie138fd252018-11-21 18:03:19 +0800782static bool virtqueue_enable_cb_delayed_split(struct virtqueue *_vq)
Michael S. Tsirkin7ab358c2011-05-20 02:11:14 +0300783{
784 struct vring_virtqueue *vq = to_vvq(_vq);
785 u16 bufs;
786
787 START_USE(vq);
788
789 /* We optimistically turn back on interrupts, then check if there was
790 * more to do. */
791 /* Depending on the VIRTIO_RING_F_USED_EVENT_IDX feature, we need to
792 * either clear the flags bit or point the event index at the next
Ladi Prosek0ea1e4a2016-08-31 14:00:04 +0200793 * entry. Always update the event index to keep code simple. */
Tiwei Biee593bf92018-11-21 18:03:21 +0800794 if (vq->split.avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT) {
795 vq->split.avail_flags_shadow &= ~VRING_AVAIL_F_NO_INTERRUPT;
Ladi Prosek0ea1e4a2016-08-31 14:00:04 +0200796 if (!vq->event)
Tiwei Biee593bf92018-11-21 18:03:21 +0800797 vq->split.vring.avail->flags =
798 cpu_to_virtio16(_vq->vdev,
799 vq->split.avail_flags_shadow);
Venkatesh Srinivasf277ec42015-11-10 16:21:07 -0800800 }
Michael S. Tsirkin7ab358c2011-05-20 02:11:14 +0300801 /* TODO: tune this threshold */
Tiwei Biee593bf92018-11-21 18:03:21 +0800802 bufs = (u16)(vq->split.avail_idx_shadow - vq->last_used_idx) * 3 / 4;
Michael S. Tsirkin788e5b32015-12-17 12:20:39 +0200803
804 virtio_store_mb(vq->weak_barriers,
Tiwei Biee593bf92018-11-21 18:03:21 +0800805 &vring_used_event(&vq->split.vring),
Michael S. Tsirkin788e5b32015-12-17 12:20:39 +0200806 cpu_to_virtio16(_vq->vdev, vq->last_used_idx + bufs));
807
Tiwei Biee593bf92018-11-21 18:03:21 +0800808 if (unlikely((u16)(virtio16_to_cpu(_vq->vdev, vq->split.vring.used->idx)
809 - vq->last_used_idx) > bufs)) {
Michael S. Tsirkin7ab358c2011-05-20 02:11:14 +0300810 END_USE(vq);
811 return false;
812 }
813
814 END_USE(vq);
815 return true;
816}
Michael S. Tsirkin7ab358c2011-05-20 02:11:14 +0300817
Tiwei Bie138fd252018-11-21 18:03:19 +0800818static void *virtqueue_detach_unused_buf_split(struct virtqueue *_vq)
Shirley Mac021eac2010-01-18 19:15:23 +0530819{
820 struct vring_virtqueue *vq = to_vvq(_vq);
821 unsigned int i;
822 void *buf;
823
824 START_USE(vq);
825
Tiwei Biee593bf92018-11-21 18:03:21 +0800826 for (i = 0; i < vq->split.vring.num; i++) {
Tiwei Biecbeedb72018-11-21 18:03:24 +0800827 if (!vq->split.desc_state[i].data)
Shirley Mac021eac2010-01-18 19:15:23 +0530828 continue;
Tiwei Bie138fd252018-11-21 18:03:19 +0800829 /* detach_buf_split clears data, so grab it now. */
Tiwei Biecbeedb72018-11-21 18:03:24 +0800830 buf = vq->split.desc_state[i].data;
Tiwei Bie138fd252018-11-21 18:03:19 +0800831 detach_buf_split(vq, i, NULL);
Tiwei Biee593bf92018-11-21 18:03:21 +0800832 vq->split.avail_idx_shadow--;
833 vq->split.vring.avail->idx = cpu_to_virtio16(_vq->vdev,
834 vq->split.avail_idx_shadow);
Shirley Mac021eac2010-01-18 19:15:23 +0530835 END_USE(vq);
836 return buf;
837 }
838 /* That should have freed everything. */
Tiwei Biee593bf92018-11-21 18:03:21 +0800839 BUG_ON(vq->vq.num_free != vq->split.vring.num);
Shirley Mac021eac2010-01-18 19:15:23 +0530840
841 END_USE(vq);
842 return NULL;
843}
Tiwei Bie138fd252018-11-21 18:03:19 +0800844
Tiwei Bied79dca72018-11-21 18:03:25 +0800845static struct virtqueue *vring_create_virtqueue_split(
846 unsigned int index,
847 unsigned int num,
848 unsigned int vring_align,
849 struct virtio_device *vdev,
850 bool weak_barriers,
851 bool may_reduce_num,
852 bool context,
853 bool (*notify)(struct virtqueue *),
854 void (*callback)(struct virtqueue *),
855 const char *name)
856{
857 struct virtqueue *vq;
858 void *queue = NULL;
859 dma_addr_t dma_addr;
860 size_t queue_size_in_bytes;
861 struct vring vring;
862
863 /* We assume num is a power of 2. */
864 if (num & (num - 1)) {
865 dev_warn(&vdev->dev, "Bad virtqueue length %u\n", num);
866 return NULL;
867 }
868
869 /* TODO: allocate each queue chunk individually */
870 for (; num && vring_size(num, vring_align) > PAGE_SIZE; num /= 2) {
871 queue = vring_alloc_queue(vdev, vring_size(num, vring_align),
872 &dma_addr,
873 GFP_KERNEL|__GFP_NOWARN|__GFP_ZERO);
874 if (queue)
875 break;
Cornelia Huckcf94db22019-04-08 14:33:22 +0200876 if (!may_reduce_num)
877 return NULL;
Tiwei Bied79dca72018-11-21 18:03:25 +0800878 }
879
880 if (!num)
881 return NULL;
882
883 if (!queue) {
884 /* Try to get a single page. You are my only hope! */
885 queue = vring_alloc_queue(vdev, vring_size(num, vring_align),
886 &dma_addr, GFP_KERNEL|__GFP_ZERO);
887 }
888 if (!queue)
889 return NULL;
890
891 queue_size_in_bytes = vring_size(num, vring_align);
892 vring_init(&vring, num, queue, vring_align);
893
894 vq = __vring_new_virtqueue(index, vring, vdev, weak_barriers, context,
895 notify, callback, name);
896 if (!vq) {
897 vring_free_queue(vdev, queue_size_in_bytes, queue,
898 dma_addr);
899 return NULL;
900 }
901
902 to_vvq(vq)->split.queue_dma_addr = dma_addr;
903 to_vvq(vq)->split.queue_size_in_bytes = queue_size_in_bytes;
904 to_vvq(vq)->we_own_ring = true;
905
906 return vq;
907}
908
Tiwei Biee6f633e2018-11-21 18:03:20 +0800909
910/*
Tiwei Bie1ce9e602018-11-21 18:03:27 +0800911 * Packed ring specific functions - *_packed().
912 */
913
914static void vring_unmap_state_packed(const struct vring_virtqueue *vq,
915 struct vring_desc_extra_packed *state)
916{
917 u16 flags;
918
919 if (!vq->use_dma_api)
920 return;
921
922 flags = state->flags;
923
924 if (flags & VRING_DESC_F_INDIRECT) {
925 dma_unmap_single(vring_dma_dev(vq),
926 state->addr, state->len,
927 (flags & VRING_DESC_F_WRITE) ?
928 DMA_FROM_DEVICE : DMA_TO_DEVICE);
929 } else {
930 dma_unmap_page(vring_dma_dev(vq),
931 state->addr, state->len,
932 (flags & VRING_DESC_F_WRITE) ?
933 DMA_FROM_DEVICE : DMA_TO_DEVICE);
934 }
935}
936
937static void vring_unmap_desc_packed(const struct vring_virtqueue *vq,
938 struct vring_packed_desc *desc)
939{
940 u16 flags;
941
942 if (!vq->use_dma_api)
943 return;
944
945 flags = le16_to_cpu(desc->flags);
946
947 if (flags & VRING_DESC_F_INDIRECT) {
948 dma_unmap_single(vring_dma_dev(vq),
949 le64_to_cpu(desc->addr),
950 le32_to_cpu(desc->len),
951 (flags & VRING_DESC_F_WRITE) ?
952 DMA_FROM_DEVICE : DMA_TO_DEVICE);
953 } else {
954 dma_unmap_page(vring_dma_dev(vq),
955 le64_to_cpu(desc->addr),
956 le32_to_cpu(desc->len),
957 (flags & VRING_DESC_F_WRITE) ?
958 DMA_FROM_DEVICE : DMA_TO_DEVICE);
959 }
960}
961
962static struct vring_packed_desc *alloc_indirect_packed(unsigned int total_sg,
963 gfp_t gfp)
964{
965 struct vring_packed_desc *desc;
966
967 /*
968 * We require lowmem mappings for the descriptors because
969 * otherwise virt_to_phys will give us bogus addresses in the
970 * virtqueue.
971 */
972 gfp &= ~__GFP_HIGHMEM;
973
974 desc = kmalloc_array(total_sg, sizeof(struct vring_packed_desc), gfp);
975
976 return desc;
977}
978
979static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq,
980 struct scatterlist *sgs[],
981 unsigned int total_sg,
982 unsigned int out_sgs,
983 unsigned int in_sgs,
984 void *data,
985 gfp_t gfp)
986{
987 struct vring_packed_desc *desc;
988 struct scatterlist *sg;
989 unsigned int i, n, err_idx;
990 u16 head, id;
991 dma_addr_t addr;
992
993 head = vq->packed.next_avail_idx;
994 desc = alloc_indirect_packed(total_sg, gfp);
995
996 if (unlikely(vq->vq.num_free < 1)) {
997 pr_debug("Can't add buf len 1 - avail = 0\n");
YueHaibingdf0bfe72019-03-12 15:06:53 +0800998 kfree(desc);
Tiwei Bie1ce9e602018-11-21 18:03:27 +0800999 END_USE(vq);
1000 return -ENOSPC;
1001 }
1002
1003 i = 0;
1004 id = vq->free_head;
1005 BUG_ON(id == vq->packed.vring.num);
1006
1007 for (n = 0; n < out_sgs + in_sgs; n++) {
1008 for (sg = sgs[n]; sg; sg = sg_next(sg)) {
1009 addr = vring_map_one_sg(vq, sg, n < out_sgs ?
1010 DMA_TO_DEVICE : DMA_FROM_DEVICE);
1011 if (vring_mapping_error(vq, addr))
1012 goto unmap_release;
1013
1014 desc[i].flags = cpu_to_le16(n < out_sgs ?
1015 0 : VRING_DESC_F_WRITE);
1016 desc[i].addr = cpu_to_le64(addr);
1017 desc[i].len = cpu_to_le32(sg->length);
1018 i++;
1019 }
1020 }
1021
1022 /* Now that the indirect table is filled in, map it. */
1023 addr = vring_map_single(vq, desc,
1024 total_sg * sizeof(struct vring_packed_desc),
1025 DMA_TO_DEVICE);
1026 if (vring_mapping_error(vq, addr))
1027 goto unmap_release;
1028
1029 vq->packed.vring.desc[head].addr = cpu_to_le64(addr);
1030 vq->packed.vring.desc[head].len = cpu_to_le32(total_sg *
1031 sizeof(struct vring_packed_desc));
1032 vq->packed.vring.desc[head].id = cpu_to_le16(id);
1033
1034 if (vq->use_dma_api) {
1035 vq->packed.desc_extra[id].addr = addr;
1036 vq->packed.desc_extra[id].len = total_sg *
1037 sizeof(struct vring_packed_desc);
1038 vq->packed.desc_extra[id].flags = VRING_DESC_F_INDIRECT |
1039 vq->packed.avail_used_flags;
1040 }
1041
1042 /*
1043 * A driver MUST NOT make the first descriptor in the list
1044 * available before all subsequent descriptors comprising
1045 * the list are made available.
1046 */
1047 virtio_wmb(vq->weak_barriers);
1048 vq->packed.vring.desc[head].flags = cpu_to_le16(VRING_DESC_F_INDIRECT |
1049 vq->packed.avail_used_flags);
1050
1051 /* We're using some buffers from the free list. */
1052 vq->vq.num_free -= 1;
1053
1054 /* Update free pointer */
1055 n = head + 1;
1056 if (n >= vq->packed.vring.num) {
1057 n = 0;
1058 vq->packed.avail_wrap_counter ^= 1;
1059 vq->packed.avail_used_flags ^=
1060 1 << VRING_PACKED_DESC_F_AVAIL |
1061 1 << VRING_PACKED_DESC_F_USED;
1062 }
1063 vq->packed.next_avail_idx = n;
1064 vq->free_head = vq->packed.desc_state[id].next;
1065
1066 /* Store token and indirect buffer state. */
1067 vq->packed.desc_state[id].num = 1;
1068 vq->packed.desc_state[id].data = data;
1069 vq->packed.desc_state[id].indir_desc = desc;
1070 vq->packed.desc_state[id].last = id;
1071
1072 vq->num_added += 1;
1073
1074 pr_debug("Added buffer head %i to %p\n", head, vq);
1075 END_USE(vq);
1076
1077 return 0;
1078
1079unmap_release:
1080 err_idx = i;
1081
1082 for (i = 0; i < err_idx; i++)
1083 vring_unmap_desc_packed(vq, &desc[i]);
1084
1085 kfree(desc);
1086
1087 END_USE(vq);
Halil Pasicf7728002019-11-14 13:46:46 +01001088 return -ENOMEM;
Tiwei Bie1ce9e602018-11-21 18:03:27 +08001089}
1090
1091static inline int virtqueue_add_packed(struct virtqueue *_vq,
1092 struct scatterlist *sgs[],
1093 unsigned int total_sg,
1094 unsigned int out_sgs,
1095 unsigned int in_sgs,
1096 void *data,
1097 void *ctx,
1098 gfp_t gfp)
1099{
1100 struct vring_virtqueue *vq = to_vvq(_vq);
1101 struct vring_packed_desc *desc;
1102 struct scatterlist *sg;
1103 unsigned int i, n, c, descs_used, err_idx;
Kees Cook3f649ab2020-06-03 13:09:38 -07001104 __le16 head_flags, flags;
1105 u16 head, id, prev, curr, avail_used_flags;
Tiwei Bie1ce9e602018-11-21 18:03:27 +08001106
1107 START_USE(vq);
1108
1109 BUG_ON(data == NULL);
1110 BUG_ON(ctx && vq->indirect);
1111
1112 if (unlikely(vq->broken)) {
1113 END_USE(vq);
1114 return -EIO;
1115 }
1116
1117 LAST_ADD_TIME_UPDATE(vq);
1118
1119 BUG_ON(total_sg == 0);
1120
1121 if (virtqueue_use_indirect(_vq, total_sg))
1122 return virtqueue_add_indirect_packed(vq, sgs, total_sg,
1123 out_sgs, in_sgs, data, gfp);
1124
1125 head = vq->packed.next_avail_idx;
1126 avail_used_flags = vq->packed.avail_used_flags;
1127
1128 WARN_ON_ONCE(total_sg > vq->packed.vring.num && !vq->indirect);
1129
1130 desc = vq->packed.vring.desc;
1131 i = head;
1132 descs_used = total_sg;
1133
1134 if (unlikely(vq->vq.num_free < descs_used)) {
1135 pr_debug("Can't add buf len %i - avail = %i\n",
1136 descs_used, vq->vq.num_free);
1137 END_USE(vq);
1138 return -ENOSPC;
1139 }
1140
1141 id = vq->free_head;
1142 BUG_ON(id == vq->packed.vring.num);
1143
1144 curr = id;
1145 c = 0;
1146 for (n = 0; n < out_sgs + in_sgs; n++) {
1147 for (sg = sgs[n]; sg; sg = sg_next(sg)) {
1148 dma_addr_t addr = vring_map_one_sg(vq, sg, n < out_sgs ?
1149 DMA_TO_DEVICE : DMA_FROM_DEVICE);
1150 if (vring_mapping_error(vq, addr))
1151 goto unmap_release;
1152
1153 flags = cpu_to_le16(vq->packed.avail_used_flags |
1154 (++c == total_sg ? 0 : VRING_DESC_F_NEXT) |
1155 (n < out_sgs ? 0 : VRING_DESC_F_WRITE));
1156 if (i == head)
1157 head_flags = flags;
1158 else
1159 desc[i].flags = flags;
1160
1161 desc[i].addr = cpu_to_le64(addr);
1162 desc[i].len = cpu_to_le32(sg->length);
1163 desc[i].id = cpu_to_le16(id);
1164
1165 if (unlikely(vq->use_dma_api)) {
1166 vq->packed.desc_extra[curr].addr = addr;
1167 vq->packed.desc_extra[curr].len = sg->length;
1168 vq->packed.desc_extra[curr].flags =
1169 le16_to_cpu(flags);
1170 }
1171 prev = curr;
1172 curr = vq->packed.desc_state[curr].next;
1173
1174 if ((unlikely(++i >= vq->packed.vring.num))) {
1175 i = 0;
1176 vq->packed.avail_used_flags ^=
1177 1 << VRING_PACKED_DESC_F_AVAIL |
1178 1 << VRING_PACKED_DESC_F_USED;
1179 }
1180 }
1181 }
1182
1183 if (i < head)
1184 vq->packed.avail_wrap_counter ^= 1;
1185
1186 /* We're using some buffers from the free list. */
1187 vq->vq.num_free -= descs_used;
1188
1189 /* Update free pointer */
1190 vq->packed.next_avail_idx = i;
1191 vq->free_head = curr;
1192
1193 /* Store token. */
1194 vq->packed.desc_state[id].num = descs_used;
1195 vq->packed.desc_state[id].data = data;
1196 vq->packed.desc_state[id].indir_desc = ctx;
1197 vq->packed.desc_state[id].last = prev;
1198
1199 /*
1200 * A driver MUST NOT make the first descriptor in the list
1201 * available before all subsequent descriptors comprising
1202 * the list are made available.
1203 */
1204 virtio_wmb(vq->weak_barriers);
1205 vq->packed.vring.desc[head].flags = head_flags;
1206 vq->num_added += descs_used;
1207
1208 pr_debug("Added buffer head %i to %p\n", head, vq);
1209 END_USE(vq);
1210
1211 return 0;
1212
1213unmap_release:
1214 err_idx = i;
1215 i = head;
1216
1217 vq->packed.avail_used_flags = avail_used_flags;
1218
1219 for (n = 0; n < total_sg; n++) {
1220 if (i == err_idx)
1221 break;
1222 vring_unmap_desc_packed(vq, &desc[i]);
1223 i++;
1224 if (i >= vq->packed.vring.num)
1225 i = 0;
1226 }
1227
1228 END_USE(vq);
1229 return -EIO;
1230}
1231
1232static bool virtqueue_kick_prepare_packed(struct virtqueue *_vq)
1233{
1234 struct vring_virtqueue *vq = to_vvq(_vq);
Tiwei Bief51f9822018-11-21 18:03:28 +08001235 u16 new, old, off_wrap, flags, wrap_counter, event_idx;
Tiwei Bie1ce9e602018-11-21 18:03:27 +08001236 bool needs_kick;
1237 union {
1238 struct {
1239 __le16 off_wrap;
1240 __le16 flags;
1241 };
1242 u32 u32;
1243 } snapshot;
1244
1245 START_USE(vq);
1246
1247 /*
1248 * We need to expose the new flags value before checking notification
1249 * suppressions.
1250 */
1251 virtio_mb(vq->weak_barriers);
1252
Tiwei Bief51f9822018-11-21 18:03:28 +08001253 old = vq->packed.next_avail_idx - vq->num_added;
1254 new = vq->packed.next_avail_idx;
Tiwei Bie1ce9e602018-11-21 18:03:27 +08001255 vq->num_added = 0;
1256
1257 snapshot.u32 = *(u32 *)vq->packed.vring.device;
1258 flags = le16_to_cpu(snapshot.flags);
1259
1260 LAST_ADD_TIME_CHECK(vq);
1261 LAST_ADD_TIME_INVALID(vq);
1262
Tiwei Bief51f9822018-11-21 18:03:28 +08001263 if (flags != VRING_PACKED_EVENT_FLAG_DESC) {
1264 needs_kick = (flags != VRING_PACKED_EVENT_FLAG_DISABLE);
1265 goto out;
1266 }
1267
1268 off_wrap = le16_to_cpu(snapshot.off_wrap);
1269
1270 wrap_counter = off_wrap >> VRING_PACKED_EVENT_F_WRAP_CTR;
1271 event_idx = off_wrap & ~(1 << VRING_PACKED_EVENT_F_WRAP_CTR);
1272 if (wrap_counter != vq->packed.avail_wrap_counter)
1273 event_idx -= vq->packed.vring.num;
1274
1275 needs_kick = vring_need_event(event_idx, new, old);
1276out:
Tiwei Bie1ce9e602018-11-21 18:03:27 +08001277 END_USE(vq);
1278 return needs_kick;
1279}
1280
1281static void detach_buf_packed(struct vring_virtqueue *vq,
1282 unsigned int id, void **ctx)
1283{
1284 struct vring_desc_state_packed *state = NULL;
1285 struct vring_packed_desc *desc;
1286 unsigned int i, curr;
1287
1288 state = &vq->packed.desc_state[id];
1289
1290 /* Clear data ptr. */
1291 state->data = NULL;
1292
1293 vq->packed.desc_state[state->last].next = vq->free_head;
1294 vq->free_head = id;
1295 vq->vq.num_free += state->num;
1296
1297 if (unlikely(vq->use_dma_api)) {
1298 curr = id;
1299 for (i = 0; i < state->num; i++) {
1300 vring_unmap_state_packed(vq,
1301 &vq->packed.desc_extra[curr]);
1302 curr = vq->packed.desc_state[curr].next;
1303 }
1304 }
1305
1306 if (vq->indirect) {
1307 u32 len;
1308
1309 /* Free the indirect table, if any, now that it's unmapped. */
1310 desc = state->indir_desc;
1311 if (!desc)
1312 return;
1313
1314 if (vq->use_dma_api) {
1315 len = vq->packed.desc_extra[id].len;
1316 for (i = 0; i < len / sizeof(struct vring_packed_desc);
1317 i++)
1318 vring_unmap_desc_packed(vq, &desc[i]);
1319 }
1320 kfree(desc);
1321 state->indir_desc = NULL;
1322 } else if (ctx) {
1323 *ctx = state->indir_desc;
1324 }
1325}
1326
1327static inline bool is_used_desc_packed(const struct vring_virtqueue *vq,
1328 u16 idx, bool used_wrap_counter)
1329{
1330 bool avail, used;
1331 u16 flags;
1332
1333 flags = le16_to_cpu(vq->packed.vring.desc[idx].flags);
1334 avail = !!(flags & (1 << VRING_PACKED_DESC_F_AVAIL));
1335 used = !!(flags & (1 << VRING_PACKED_DESC_F_USED));
1336
1337 return avail == used && used == used_wrap_counter;
1338}
1339
1340static inline bool more_used_packed(const struct vring_virtqueue *vq)
1341{
1342 return is_used_desc_packed(vq, vq->last_used_idx,
1343 vq->packed.used_wrap_counter);
1344}
1345
1346static void *virtqueue_get_buf_ctx_packed(struct virtqueue *_vq,
1347 unsigned int *len,
1348 void **ctx)
1349{
1350 struct vring_virtqueue *vq = to_vvq(_vq);
1351 u16 last_used, id;
1352 void *ret;
1353
1354 START_USE(vq);
1355
1356 if (unlikely(vq->broken)) {
1357 END_USE(vq);
1358 return NULL;
1359 }
1360
1361 if (!more_used_packed(vq)) {
1362 pr_debug("No more buffers in queue\n");
1363 END_USE(vq);
1364 return NULL;
1365 }
1366
1367 /* Only get used elements after they have been exposed by host. */
1368 virtio_rmb(vq->weak_barriers);
1369
1370 last_used = vq->last_used_idx;
1371 id = le16_to_cpu(vq->packed.vring.desc[last_used].id);
1372 *len = le32_to_cpu(vq->packed.vring.desc[last_used].len);
1373
1374 if (unlikely(id >= vq->packed.vring.num)) {
1375 BAD_RING(vq, "id %u out of range\n", id);
1376 return NULL;
1377 }
1378 if (unlikely(!vq->packed.desc_state[id].data)) {
1379 BAD_RING(vq, "id %u is not a head!\n", id);
1380 return NULL;
1381 }
1382
1383 /* detach_buf_packed clears data, so grab it now. */
1384 ret = vq->packed.desc_state[id].data;
1385 detach_buf_packed(vq, id, ctx);
1386
1387 vq->last_used_idx += vq->packed.desc_state[id].num;
1388 if (unlikely(vq->last_used_idx >= vq->packed.vring.num)) {
1389 vq->last_used_idx -= vq->packed.vring.num;
1390 vq->packed.used_wrap_counter ^= 1;
1391 }
1392
Tiwei Bief51f9822018-11-21 18:03:28 +08001393 /*
1394 * If we expect an interrupt for the next entry, tell host
1395 * by writing event index and flush out the write before
1396 * the read in the next get_buf call.
1397 */
1398 if (vq->packed.event_flags_shadow == VRING_PACKED_EVENT_FLAG_DESC)
1399 virtio_store_mb(vq->weak_barriers,
1400 &vq->packed.vring.driver->off_wrap,
1401 cpu_to_le16(vq->last_used_idx |
1402 (vq->packed.used_wrap_counter <<
1403 VRING_PACKED_EVENT_F_WRAP_CTR)));
1404
Tiwei Bie1ce9e602018-11-21 18:03:27 +08001405 LAST_ADD_TIME_INVALID(vq);
1406
1407 END_USE(vq);
1408 return ret;
1409}
1410
1411static void virtqueue_disable_cb_packed(struct virtqueue *_vq)
1412{
1413 struct vring_virtqueue *vq = to_vvq(_vq);
1414
1415 if (vq->packed.event_flags_shadow != VRING_PACKED_EVENT_FLAG_DISABLE) {
1416 vq->packed.event_flags_shadow = VRING_PACKED_EVENT_FLAG_DISABLE;
1417 vq->packed.vring.driver->flags =
1418 cpu_to_le16(vq->packed.event_flags_shadow);
1419 }
1420}
1421
1422static unsigned virtqueue_enable_cb_prepare_packed(struct virtqueue *_vq)
1423{
1424 struct vring_virtqueue *vq = to_vvq(_vq);
1425
1426 START_USE(vq);
1427
1428 /*
1429 * We optimistically turn back on interrupts, then check if there was
1430 * more to do.
1431 */
1432
Tiwei Bief51f9822018-11-21 18:03:28 +08001433 if (vq->event) {
1434 vq->packed.vring.driver->off_wrap =
1435 cpu_to_le16(vq->last_used_idx |
1436 (vq->packed.used_wrap_counter <<
1437 VRING_PACKED_EVENT_F_WRAP_CTR));
1438 /*
1439 * We need to update event offset and event wrap
1440 * counter first before updating event flags.
1441 */
1442 virtio_wmb(vq->weak_barriers);
1443 }
1444
Tiwei Bie1ce9e602018-11-21 18:03:27 +08001445 if (vq->packed.event_flags_shadow == VRING_PACKED_EVENT_FLAG_DISABLE) {
Tiwei Bief51f9822018-11-21 18:03:28 +08001446 vq->packed.event_flags_shadow = vq->event ?
1447 VRING_PACKED_EVENT_FLAG_DESC :
1448 VRING_PACKED_EVENT_FLAG_ENABLE;
Tiwei Bie1ce9e602018-11-21 18:03:27 +08001449 vq->packed.vring.driver->flags =
1450 cpu_to_le16(vq->packed.event_flags_shadow);
1451 }
1452
1453 END_USE(vq);
1454 return vq->last_used_idx | ((u16)vq->packed.used_wrap_counter <<
1455 VRING_PACKED_EVENT_F_WRAP_CTR);
1456}
1457
1458static bool virtqueue_poll_packed(struct virtqueue *_vq, u16 off_wrap)
1459{
1460 struct vring_virtqueue *vq = to_vvq(_vq);
1461 bool wrap_counter;
1462 u16 used_idx;
1463
1464 wrap_counter = off_wrap >> VRING_PACKED_EVENT_F_WRAP_CTR;
1465 used_idx = off_wrap & ~(1 << VRING_PACKED_EVENT_F_WRAP_CTR);
1466
1467 return is_used_desc_packed(vq, used_idx, wrap_counter);
1468}
1469
1470static bool virtqueue_enable_cb_delayed_packed(struct virtqueue *_vq)
1471{
1472 struct vring_virtqueue *vq = to_vvq(_vq);
1473 u16 used_idx, wrap_counter;
Tiwei Bief51f9822018-11-21 18:03:28 +08001474 u16 bufs;
Tiwei Bie1ce9e602018-11-21 18:03:27 +08001475
1476 START_USE(vq);
1477
1478 /*
1479 * We optimistically turn back on interrupts, then check if there was
1480 * more to do.
1481 */
1482
Tiwei Bief51f9822018-11-21 18:03:28 +08001483 if (vq->event) {
1484 /* TODO: tune this threshold */
1485 bufs = (vq->packed.vring.num - vq->vq.num_free) * 3 / 4;
1486 wrap_counter = vq->packed.used_wrap_counter;
1487
1488 used_idx = vq->last_used_idx + bufs;
1489 if (used_idx >= vq->packed.vring.num) {
1490 used_idx -= vq->packed.vring.num;
1491 wrap_counter ^= 1;
1492 }
1493
1494 vq->packed.vring.driver->off_wrap = cpu_to_le16(used_idx |
1495 (wrap_counter << VRING_PACKED_EVENT_F_WRAP_CTR));
1496
1497 /*
1498 * We need to update event offset and event wrap
1499 * counter first before updating event flags.
1500 */
1501 virtio_wmb(vq->weak_barriers);
Tiwei Bief51f9822018-11-21 18:03:28 +08001502 }
Tiwei Bie1ce9e602018-11-21 18:03:27 +08001503
1504 if (vq->packed.event_flags_shadow == VRING_PACKED_EVENT_FLAG_DISABLE) {
Tiwei Bief51f9822018-11-21 18:03:28 +08001505 vq->packed.event_flags_shadow = vq->event ?
1506 VRING_PACKED_EVENT_FLAG_DESC :
1507 VRING_PACKED_EVENT_FLAG_ENABLE;
Tiwei Bie1ce9e602018-11-21 18:03:27 +08001508 vq->packed.vring.driver->flags =
1509 cpu_to_le16(vq->packed.event_flags_shadow);
1510 }
1511
1512 /*
1513 * We need to update event suppression structure first
1514 * before re-checking for more used buffers.
1515 */
1516 virtio_mb(vq->weak_barriers);
1517
Marvin Liu40ce7912019-10-22 01:10:04 +08001518 if (is_used_desc_packed(vq,
1519 vq->last_used_idx,
1520 vq->packed.used_wrap_counter)) {
Tiwei Bie1ce9e602018-11-21 18:03:27 +08001521 END_USE(vq);
1522 return false;
1523 }
1524
1525 END_USE(vq);
1526 return true;
1527}
1528
1529static void *virtqueue_detach_unused_buf_packed(struct virtqueue *_vq)
1530{
1531 struct vring_virtqueue *vq = to_vvq(_vq);
1532 unsigned int i;
1533 void *buf;
1534
1535 START_USE(vq);
1536
1537 for (i = 0; i < vq->packed.vring.num; i++) {
1538 if (!vq->packed.desc_state[i].data)
1539 continue;
1540 /* detach_buf clears data, so grab it now. */
1541 buf = vq->packed.desc_state[i].data;
1542 detach_buf_packed(vq, i, NULL);
1543 END_USE(vq);
1544 return buf;
1545 }
1546 /* That should have freed everything. */
1547 BUG_ON(vq->vq.num_free != vq->packed.vring.num);
1548
1549 END_USE(vq);
1550 return NULL;
1551}
1552
1553static struct virtqueue *vring_create_virtqueue_packed(
1554 unsigned int index,
1555 unsigned int num,
1556 unsigned int vring_align,
1557 struct virtio_device *vdev,
1558 bool weak_barriers,
1559 bool may_reduce_num,
1560 bool context,
1561 bool (*notify)(struct virtqueue *),
1562 void (*callback)(struct virtqueue *),
1563 const char *name)
1564{
1565 struct vring_virtqueue *vq;
1566 struct vring_packed_desc *ring;
1567 struct vring_packed_desc_event *driver, *device;
1568 dma_addr_t ring_dma_addr, driver_event_dma_addr, device_event_dma_addr;
1569 size_t ring_size_in_bytes, event_size_in_bytes;
1570 unsigned int i;
1571
1572 ring_size_in_bytes = num * sizeof(struct vring_packed_desc);
1573
1574 ring = vring_alloc_queue(vdev, ring_size_in_bytes,
1575 &ring_dma_addr,
1576 GFP_KERNEL|__GFP_NOWARN|__GFP_ZERO);
1577 if (!ring)
1578 goto err_ring;
1579
1580 event_size_in_bytes = sizeof(struct vring_packed_desc_event);
1581
1582 driver = vring_alloc_queue(vdev, event_size_in_bytes,
1583 &driver_event_dma_addr,
1584 GFP_KERNEL|__GFP_NOWARN|__GFP_ZERO);
1585 if (!driver)
1586 goto err_driver;
1587
1588 device = vring_alloc_queue(vdev, event_size_in_bytes,
1589 &device_event_dma_addr,
1590 GFP_KERNEL|__GFP_NOWARN|__GFP_ZERO);
1591 if (!device)
1592 goto err_device;
1593
1594 vq = kmalloc(sizeof(*vq), GFP_KERNEL);
1595 if (!vq)
1596 goto err_vq;
1597
1598 vq->vq.callback = callback;
1599 vq->vq.vdev = vdev;
1600 vq->vq.name = name;
1601 vq->vq.num_free = num;
1602 vq->vq.index = index;
1603 vq->we_own_ring = true;
1604 vq->notify = notify;
1605 vq->weak_barriers = weak_barriers;
1606 vq->broken = false;
1607 vq->last_used_idx = 0;
1608 vq->num_added = 0;
1609 vq->packed_ring = true;
1610 vq->use_dma_api = vring_use_dma_api(vdev);
Tiwei Bie1ce9e602018-11-21 18:03:27 +08001611#ifdef DEBUG
1612 vq->in_use = false;
1613 vq->last_add_time_valid = false;
1614#endif
1615
1616 vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC) &&
1617 !context;
1618 vq->event = virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX);
1619
Tiwei Bie45383fb2019-01-23 17:50:26 +08001620 if (virtio_has_feature(vdev, VIRTIO_F_ORDER_PLATFORM))
1621 vq->weak_barriers = false;
1622
Tiwei Bie1ce9e602018-11-21 18:03:27 +08001623 vq->packed.ring_dma_addr = ring_dma_addr;
1624 vq->packed.driver_event_dma_addr = driver_event_dma_addr;
1625 vq->packed.device_event_dma_addr = device_event_dma_addr;
1626
1627 vq->packed.ring_size_in_bytes = ring_size_in_bytes;
1628 vq->packed.event_size_in_bytes = event_size_in_bytes;
1629
1630 vq->packed.vring.num = num;
1631 vq->packed.vring.desc = ring;
1632 vq->packed.vring.driver = driver;
1633 vq->packed.vring.device = device;
1634
1635 vq->packed.next_avail_idx = 0;
1636 vq->packed.avail_wrap_counter = 1;
1637 vq->packed.used_wrap_counter = 1;
1638 vq->packed.event_flags_shadow = 0;
1639 vq->packed.avail_used_flags = 1 << VRING_PACKED_DESC_F_AVAIL;
1640
1641 vq->packed.desc_state = kmalloc_array(num,
1642 sizeof(struct vring_desc_state_packed),
1643 GFP_KERNEL);
1644 if (!vq->packed.desc_state)
1645 goto err_desc_state;
1646
1647 memset(vq->packed.desc_state, 0,
1648 num * sizeof(struct vring_desc_state_packed));
1649
1650 /* Put everything in free lists. */
1651 vq->free_head = 0;
1652 for (i = 0; i < num-1; i++)
1653 vq->packed.desc_state[i].next = i + 1;
1654
1655 vq->packed.desc_extra = kmalloc_array(num,
1656 sizeof(struct vring_desc_extra_packed),
1657 GFP_KERNEL);
1658 if (!vq->packed.desc_extra)
1659 goto err_desc_extra;
1660
1661 memset(vq->packed.desc_extra, 0,
1662 num * sizeof(struct vring_desc_extra_packed));
1663
1664 /* No callback? Tell other side not to bother us. */
1665 if (!callback) {
1666 vq->packed.event_flags_shadow = VRING_PACKED_EVENT_FLAG_DISABLE;
1667 vq->packed.vring.driver->flags =
1668 cpu_to_le16(vq->packed.event_flags_shadow);
1669 }
1670
Dan Carpentere152d8a2020-12-04 17:23:36 +03001671 list_add_tail(&vq->vq.list, &vdev->vqs);
Tiwei Bie1ce9e602018-11-21 18:03:27 +08001672 return &vq->vq;
1673
1674err_desc_extra:
1675 kfree(vq->packed.desc_state);
1676err_desc_state:
1677 kfree(vq);
1678err_vq:
Dan Carpenterae93d8e2020-12-04 17:23:00 +03001679 vring_free_queue(vdev, event_size_in_bytes, device, device_event_dma_addr);
Tiwei Bie1ce9e602018-11-21 18:03:27 +08001680err_device:
Dan Carpenterae93d8e2020-12-04 17:23:00 +03001681 vring_free_queue(vdev, event_size_in_bytes, driver, driver_event_dma_addr);
Tiwei Bie1ce9e602018-11-21 18:03:27 +08001682err_driver:
1683 vring_free_queue(vdev, ring_size_in_bytes, ring, ring_dma_addr);
1684err_ring:
1685 return NULL;
1686}
1687
1688
1689/*
Tiwei Biee6f633e2018-11-21 18:03:20 +08001690 * Generic functions and exported symbols.
1691 */
1692
1693static inline int virtqueue_add(struct virtqueue *_vq,
1694 struct scatterlist *sgs[],
1695 unsigned int total_sg,
1696 unsigned int out_sgs,
1697 unsigned int in_sgs,
1698 void *data,
1699 void *ctx,
1700 gfp_t gfp)
1701{
Tiwei Bie1ce9e602018-11-21 18:03:27 +08001702 struct vring_virtqueue *vq = to_vvq(_vq);
1703
1704 return vq->packed_ring ? virtqueue_add_packed(_vq, sgs, total_sg,
1705 out_sgs, in_sgs, data, ctx, gfp) :
1706 virtqueue_add_split(_vq, sgs, total_sg,
1707 out_sgs, in_sgs, data, ctx, gfp);
Tiwei Biee6f633e2018-11-21 18:03:20 +08001708}
1709
1710/**
1711 * virtqueue_add_sgs - expose buffers to other end
Jiang Biaoa5581202019-04-23 18:25:12 +08001712 * @_vq: the struct virtqueue we're talking about.
Tiwei Biee6f633e2018-11-21 18:03:20 +08001713 * @sgs: array of terminated scatterlists.
Jiang Biaoa5581202019-04-23 18:25:12 +08001714 * @out_sgs: the number of scatterlists readable by other side
1715 * @in_sgs: the number of scatterlists which are writable (after readable ones)
Tiwei Biee6f633e2018-11-21 18:03:20 +08001716 * @data: the token identifying the buffer.
1717 * @gfp: how to do memory allocations (if necessary).
1718 *
1719 * Caller must ensure we don't call this with other virtqueue operations
1720 * at the same time (except where noted).
1721 *
1722 * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO).
1723 */
1724int virtqueue_add_sgs(struct virtqueue *_vq,
1725 struct scatterlist *sgs[],
1726 unsigned int out_sgs,
1727 unsigned int in_sgs,
1728 void *data,
1729 gfp_t gfp)
1730{
1731 unsigned int i, total_sg = 0;
1732
1733 /* Count them first. */
1734 for (i = 0; i < out_sgs + in_sgs; i++) {
1735 struct scatterlist *sg;
1736
1737 for (sg = sgs[i]; sg; sg = sg_next(sg))
1738 total_sg++;
1739 }
1740 return virtqueue_add(_vq, sgs, total_sg, out_sgs, in_sgs,
1741 data, NULL, gfp);
1742}
1743EXPORT_SYMBOL_GPL(virtqueue_add_sgs);
1744
1745/**
1746 * virtqueue_add_outbuf - expose output buffers to other end
1747 * @vq: the struct virtqueue we're talking about.
1748 * @sg: scatterlist (must be well-formed and terminated!)
1749 * @num: the number of entries in @sg readable by other side
1750 * @data: the token identifying the buffer.
1751 * @gfp: how to do memory allocations (if necessary).
1752 *
1753 * Caller must ensure we don't call this with other virtqueue operations
1754 * at the same time (except where noted).
1755 *
1756 * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO).
1757 */
1758int virtqueue_add_outbuf(struct virtqueue *vq,
1759 struct scatterlist *sg, unsigned int num,
1760 void *data,
1761 gfp_t gfp)
1762{
1763 return virtqueue_add(vq, &sg, num, 1, 0, data, NULL, gfp);
1764}
1765EXPORT_SYMBOL_GPL(virtqueue_add_outbuf);
1766
1767/**
1768 * virtqueue_add_inbuf - expose input buffers to other end
1769 * @vq: the struct virtqueue we're talking about.
1770 * @sg: scatterlist (must be well-formed and terminated!)
1771 * @num: the number of entries in @sg writable by other side
1772 * @data: the token identifying the buffer.
1773 * @gfp: how to do memory allocations (if necessary).
1774 *
1775 * Caller must ensure we don't call this with other virtqueue operations
1776 * at the same time (except where noted).
1777 *
1778 * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO).
1779 */
1780int virtqueue_add_inbuf(struct virtqueue *vq,
1781 struct scatterlist *sg, unsigned int num,
1782 void *data,
1783 gfp_t gfp)
1784{
1785 return virtqueue_add(vq, &sg, num, 0, 1, data, NULL, gfp);
1786}
1787EXPORT_SYMBOL_GPL(virtqueue_add_inbuf);
1788
1789/**
1790 * virtqueue_add_inbuf_ctx - expose input buffers to other end
1791 * @vq: the struct virtqueue we're talking about.
1792 * @sg: scatterlist (must be well-formed and terminated!)
1793 * @num: the number of entries in @sg writable by other side
1794 * @data: the token identifying the buffer.
1795 * @ctx: extra context for the token
1796 * @gfp: how to do memory allocations (if necessary).
1797 *
1798 * Caller must ensure we don't call this with other virtqueue operations
1799 * at the same time (except where noted).
1800 *
1801 * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO).
1802 */
1803int virtqueue_add_inbuf_ctx(struct virtqueue *vq,
1804 struct scatterlist *sg, unsigned int num,
1805 void *data,
1806 void *ctx,
1807 gfp_t gfp)
1808{
1809 return virtqueue_add(vq, &sg, num, 0, 1, data, ctx, gfp);
1810}
1811EXPORT_SYMBOL_GPL(virtqueue_add_inbuf_ctx);
1812
1813/**
1814 * virtqueue_kick_prepare - first half of split virtqueue_kick call.
Jiang Biaoa5581202019-04-23 18:25:12 +08001815 * @_vq: the struct virtqueue
Tiwei Biee6f633e2018-11-21 18:03:20 +08001816 *
1817 * Instead of virtqueue_kick(), you can do:
1818 * if (virtqueue_kick_prepare(vq))
1819 * virtqueue_notify(vq);
1820 *
1821 * This is sometimes useful because the virtqueue_kick_prepare() needs
1822 * to be serialized, but the actual virtqueue_notify() call does not.
1823 */
1824bool virtqueue_kick_prepare(struct virtqueue *_vq)
1825{
Tiwei Bie1ce9e602018-11-21 18:03:27 +08001826 struct vring_virtqueue *vq = to_vvq(_vq);
1827
1828 return vq->packed_ring ? virtqueue_kick_prepare_packed(_vq) :
1829 virtqueue_kick_prepare_split(_vq);
Tiwei Biee6f633e2018-11-21 18:03:20 +08001830}
1831EXPORT_SYMBOL_GPL(virtqueue_kick_prepare);
1832
1833/**
1834 * virtqueue_notify - second half of split virtqueue_kick call.
Jiang Biaoa5581202019-04-23 18:25:12 +08001835 * @_vq: the struct virtqueue
Tiwei Biee6f633e2018-11-21 18:03:20 +08001836 *
1837 * This does not need to be serialized.
1838 *
1839 * Returns false if host notify failed or queue is broken, otherwise true.
1840 */
1841bool virtqueue_notify(struct virtqueue *_vq)
1842{
1843 struct vring_virtqueue *vq = to_vvq(_vq);
1844
1845 if (unlikely(vq->broken))
1846 return false;
1847
1848 /* Prod other side to tell it about changes. */
1849 if (!vq->notify(_vq)) {
1850 vq->broken = true;
1851 return false;
1852 }
1853 return true;
1854}
1855EXPORT_SYMBOL_GPL(virtqueue_notify);
1856
1857/**
1858 * virtqueue_kick - update after add_buf
1859 * @vq: the struct virtqueue
1860 *
1861 * After one or more virtqueue_add_* calls, invoke this to kick
1862 * the other side.
1863 *
1864 * Caller must ensure we don't call this with other virtqueue
1865 * operations at the same time (except where noted).
1866 *
1867 * Returns false if kick failed, otherwise true.
1868 */
1869bool virtqueue_kick(struct virtqueue *vq)
1870{
1871 if (virtqueue_kick_prepare(vq))
1872 return virtqueue_notify(vq);
1873 return true;
1874}
1875EXPORT_SYMBOL_GPL(virtqueue_kick);
1876
1877/**
1878 * virtqueue_get_buf - get the next used buffer
Jiang Biaoa5581202019-04-23 18:25:12 +08001879 * @_vq: the struct virtqueue we're talking about.
Tiwei Biee6f633e2018-11-21 18:03:20 +08001880 * @len: the length written into the buffer
Jiang Biaoa5581202019-04-23 18:25:12 +08001881 * @ctx: extra context for the token
Tiwei Biee6f633e2018-11-21 18:03:20 +08001882 *
1883 * If the device wrote data into the buffer, @len will be set to the
1884 * amount written. This means you don't need to clear the buffer
1885 * beforehand to ensure there's no data leakage in the case of short
1886 * writes.
1887 *
1888 * Caller must ensure we don't call this with other virtqueue
1889 * operations at the same time (except where noted).
1890 *
1891 * Returns NULL if there are no used buffers, or the "data" token
1892 * handed to virtqueue_add_*().
1893 */
1894void *virtqueue_get_buf_ctx(struct virtqueue *_vq, unsigned int *len,
1895 void **ctx)
1896{
Tiwei Bie1ce9e602018-11-21 18:03:27 +08001897 struct vring_virtqueue *vq = to_vvq(_vq);
1898
1899 return vq->packed_ring ? virtqueue_get_buf_ctx_packed(_vq, len, ctx) :
1900 virtqueue_get_buf_ctx_split(_vq, len, ctx);
Tiwei Biee6f633e2018-11-21 18:03:20 +08001901}
1902EXPORT_SYMBOL_GPL(virtqueue_get_buf_ctx);
1903
1904void *virtqueue_get_buf(struct virtqueue *_vq, unsigned int *len)
1905{
1906 return virtqueue_get_buf_ctx(_vq, len, NULL);
1907}
1908EXPORT_SYMBOL_GPL(virtqueue_get_buf);
Tiwei Biee6f633e2018-11-21 18:03:20 +08001909/**
1910 * virtqueue_disable_cb - disable callbacks
Jiang Biaoa5581202019-04-23 18:25:12 +08001911 * @_vq: the struct virtqueue we're talking about.
Tiwei Biee6f633e2018-11-21 18:03:20 +08001912 *
1913 * Note that this is not necessarily synchronous, hence unreliable and only
1914 * useful as an optimization.
1915 *
1916 * Unlike other operations, this need not be serialized.
1917 */
1918void virtqueue_disable_cb(struct virtqueue *_vq)
1919{
Tiwei Bie1ce9e602018-11-21 18:03:27 +08001920 struct vring_virtqueue *vq = to_vvq(_vq);
1921
1922 if (vq->packed_ring)
1923 virtqueue_disable_cb_packed(_vq);
1924 else
1925 virtqueue_disable_cb_split(_vq);
Tiwei Biee6f633e2018-11-21 18:03:20 +08001926}
1927EXPORT_SYMBOL_GPL(virtqueue_disable_cb);
1928
1929/**
1930 * virtqueue_enable_cb_prepare - restart callbacks after disable_cb
Jiang Biaoa5581202019-04-23 18:25:12 +08001931 * @_vq: the struct virtqueue we're talking about.
Tiwei Biee6f633e2018-11-21 18:03:20 +08001932 *
1933 * This re-enables callbacks; it returns current queue state
1934 * in an opaque unsigned value. This value should be later tested by
1935 * virtqueue_poll, to detect a possible race between the driver checking for
1936 * more work, and enabling callbacks.
1937 *
1938 * Caller must ensure we don't call this with other virtqueue
1939 * operations at the same time (except where noted).
1940 */
1941unsigned virtqueue_enable_cb_prepare(struct virtqueue *_vq)
1942{
Tiwei Bie1ce9e602018-11-21 18:03:27 +08001943 struct vring_virtqueue *vq = to_vvq(_vq);
1944
1945 return vq->packed_ring ? virtqueue_enable_cb_prepare_packed(_vq) :
1946 virtqueue_enable_cb_prepare_split(_vq);
Tiwei Biee6f633e2018-11-21 18:03:20 +08001947}
1948EXPORT_SYMBOL_GPL(virtqueue_enable_cb_prepare);
1949
1950/**
1951 * virtqueue_poll - query pending used buffers
Jiang Biaoa5581202019-04-23 18:25:12 +08001952 * @_vq: the struct virtqueue we're talking about.
Tiwei Biee6f633e2018-11-21 18:03:20 +08001953 * @last_used_idx: virtqueue state (from call to virtqueue_enable_cb_prepare).
1954 *
1955 * Returns "true" if there are pending used buffers in the queue.
1956 *
1957 * This does not need to be serialized.
1958 */
1959bool virtqueue_poll(struct virtqueue *_vq, unsigned last_used_idx)
1960{
1961 struct vring_virtqueue *vq = to_vvq(_vq);
1962
Mao Wenan481a0d72020-08-02 15:44:09 +08001963 if (unlikely(vq->broken))
1964 return false;
1965
Tiwei Biee6f633e2018-11-21 18:03:20 +08001966 virtio_mb(vq->weak_barriers);
Tiwei Bie1ce9e602018-11-21 18:03:27 +08001967 return vq->packed_ring ? virtqueue_poll_packed(_vq, last_used_idx) :
1968 virtqueue_poll_split(_vq, last_used_idx);
Tiwei Biee6f633e2018-11-21 18:03:20 +08001969}
1970EXPORT_SYMBOL_GPL(virtqueue_poll);
1971
1972/**
1973 * virtqueue_enable_cb - restart callbacks after disable_cb.
Jiang Biaoa5581202019-04-23 18:25:12 +08001974 * @_vq: the struct virtqueue we're talking about.
Tiwei Biee6f633e2018-11-21 18:03:20 +08001975 *
1976 * This re-enables callbacks; it returns "false" if there are pending
1977 * buffers in the queue, to detect a possible race between the driver
1978 * checking for more work, and enabling callbacks.
1979 *
1980 * Caller must ensure we don't call this with other virtqueue
1981 * operations at the same time (except where noted).
1982 */
1983bool virtqueue_enable_cb(struct virtqueue *_vq)
1984{
1985 unsigned last_used_idx = virtqueue_enable_cb_prepare(_vq);
1986
1987 return !virtqueue_poll(_vq, last_used_idx);
1988}
1989EXPORT_SYMBOL_GPL(virtqueue_enable_cb);
1990
1991/**
1992 * virtqueue_enable_cb_delayed - restart callbacks after disable_cb.
Jiang Biaoa5581202019-04-23 18:25:12 +08001993 * @_vq: the struct virtqueue we're talking about.
Tiwei Biee6f633e2018-11-21 18:03:20 +08001994 *
1995 * This re-enables callbacks but hints to the other side to delay
1996 * interrupts until most of the available buffers have been processed;
1997 * it returns "false" if there are many pending buffers in the queue,
1998 * to detect a possible race between the driver checking for more work,
1999 * and enabling callbacks.
2000 *
2001 * Caller must ensure we don't call this with other virtqueue
2002 * operations at the same time (except where noted).
2003 */
2004bool virtqueue_enable_cb_delayed(struct virtqueue *_vq)
2005{
Tiwei Bie1ce9e602018-11-21 18:03:27 +08002006 struct vring_virtqueue *vq = to_vvq(_vq);
2007
2008 return vq->packed_ring ? virtqueue_enable_cb_delayed_packed(_vq) :
2009 virtqueue_enable_cb_delayed_split(_vq);
Tiwei Biee6f633e2018-11-21 18:03:20 +08002010}
2011EXPORT_SYMBOL_GPL(virtqueue_enable_cb_delayed);
2012
Tiwei Bie138fd252018-11-21 18:03:19 +08002013/**
2014 * virtqueue_detach_unused_buf - detach first unused buffer
Jiang Biaoa5581202019-04-23 18:25:12 +08002015 * @_vq: the struct virtqueue we're talking about.
Tiwei Bie138fd252018-11-21 18:03:19 +08002016 *
2017 * Returns NULL or the "data" token handed to virtqueue_add_*().
2018 * This is not valid on an active queue; it is useful only for device
2019 * shutdown.
2020 */
2021void *virtqueue_detach_unused_buf(struct virtqueue *_vq)
2022{
Tiwei Bie1ce9e602018-11-21 18:03:27 +08002023 struct vring_virtqueue *vq = to_vvq(_vq);
2024
2025 return vq->packed_ring ? virtqueue_detach_unused_buf_packed(_vq) :
2026 virtqueue_detach_unused_buf_split(_vq);
Tiwei Bie138fd252018-11-21 18:03:19 +08002027}
Michael S. Tsirkin7c5e9ed2010-04-12 16:19:07 +03002028EXPORT_SYMBOL_GPL(virtqueue_detach_unused_buf);
Shirley Mac021eac2010-01-18 19:15:23 +05302029
Tiwei Bie138fd252018-11-21 18:03:19 +08002030static inline bool more_used(const struct vring_virtqueue *vq)
2031{
Tiwei Bie1ce9e602018-11-21 18:03:27 +08002032 return vq->packed_ring ? more_used_packed(vq) : more_used_split(vq);
Tiwei Bie138fd252018-11-21 18:03:19 +08002033}
2034
Rusty Russell0a8a69d2007-10-22 11:03:40 +10002035irqreturn_t vring_interrupt(int irq, void *_vq)
2036{
2037 struct vring_virtqueue *vq = to_vvq(_vq);
2038
2039 if (!more_used(vq)) {
2040 pr_debug("virtqueue interrupt with no work for %p\n", vq);
2041 return IRQ_NONE;
2042 }
2043
2044 if (unlikely(vq->broken))
2045 return IRQ_HANDLED;
2046
2047 pr_debug("virtqueue callback for %p (%p)\n", vq, vq->vq.callback);
Rusty Russell18445c42008-02-04 23:49:57 -05002048 if (vq->vq.callback)
2049 vq->vq.callback(&vq->vq);
Rusty Russell0a8a69d2007-10-22 11:03:40 +10002050
2051 return IRQ_HANDLED;
2052}
Rusty Russellc6fd4702008-02-04 23:50:05 -05002053EXPORT_SYMBOL_GPL(vring_interrupt);
Rusty Russell0a8a69d2007-10-22 11:03:40 +10002054
Tiwei Bie1ce9e602018-11-21 18:03:27 +08002055/* Only available for split ring */
Andy Lutomirski2a2d1382016-02-02 21:46:37 -08002056struct virtqueue *__vring_new_virtqueue(unsigned int index,
2057 struct vring vring,
2058 struct virtio_device *vdev,
2059 bool weak_barriers,
Michael S. Tsirkinf94682d2017-03-06 18:32:29 +02002060 bool context,
Andy Lutomirski2a2d1382016-02-02 21:46:37 -08002061 bool (*notify)(struct virtqueue *),
2062 void (*callback)(struct virtqueue *),
2063 const char *name)
Rusty Russell0a8a69d2007-10-22 11:03:40 +10002064{
Rusty Russell0a8a69d2007-10-22 11:03:40 +10002065 unsigned int i;
Andy Lutomirski2a2d1382016-02-02 21:46:37 -08002066 struct vring_virtqueue *vq;
Rusty Russell0a8a69d2007-10-22 11:03:40 +10002067
Tiwei Bie1ce9e602018-11-21 18:03:27 +08002068 if (virtio_has_feature(vdev, VIRTIO_F_RING_PACKED))
2069 return NULL;
2070
Tiwei Biecbeedb72018-11-21 18:03:24 +08002071 vq = kmalloc(sizeof(*vq), GFP_KERNEL);
Rusty Russell0a8a69d2007-10-22 11:03:40 +10002072 if (!vq)
2073 return NULL;
2074
Tiwei Bie1ce9e602018-11-21 18:03:27 +08002075 vq->packed_ring = false;
Rusty Russell0a8a69d2007-10-22 11:03:40 +10002076 vq->vq.callback = callback;
2077 vq->vq.vdev = vdev;
Rusty Russell9499f5e2009-06-12 22:16:35 -06002078 vq->vq.name = name;
Andy Lutomirski2a2d1382016-02-02 21:46:37 -08002079 vq->vq.num_free = vring.num;
Rusty Russell06ca2872012-10-16 23:56:14 +10302080 vq->vq.index = index;
Andy Lutomirski2a2d1382016-02-02 21:46:37 -08002081 vq->we_own_ring = false;
Rusty Russell0a8a69d2007-10-22 11:03:40 +10002082 vq->notify = notify;
Rusty Russell7b21e342012-01-12 15:44:42 +10302083 vq->weak_barriers = weak_barriers;
Rusty Russell0a8a69d2007-10-22 11:03:40 +10002084 vq->broken = false;
2085 vq->last_used_idx = 0;
2086 vq->num_added = 0;
Tiwei Biefb3fba62018-11-21 18:03:26 +08002087 vq->use_dma_api = vring_use_dma_api(vdev);
Rusty Russell0a8a69d2007-10-22 11:03:40 +10002088#ifdef DEBUG
2089 vq->in_use = false;
Rusty Russelle93300b2012-01-12 15:44:43 +10302090 vq->last_add_time_valid = false;
Rusty Russell0a8a69d2007-10-22 11:03:40 +10002091#endif
2092
Michael S. Tsirkin5a08b042017-02-07 06:15:13 +02002093 vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC) &&
2094 !context;
Michael S. Tsirkina5c262c2011-05-20 02:10:44 +03002095 vq->event = virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX);
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +01002096
Tiwei Bie45383fb2019-01-23 17:50:26 +08002097 if (virtio_has_feature(vdev, VIRTIO_F_ORDER_PLATFORM))
2098 vq->weak_barriers = false;
2099
Tiwei Bied79dca72018-11-21 18:03:25 +08002100 vq->split.queue_dma_addr = 0;
2101 vq->split.queue_size_in_bytes = 0;
2102
Tiwei Biee593bf92018-11-21 18:03:21 +08002103 vq->split.vring = vring;
2104 vq->split.avail_flags_shadow = 0;
2105 vq->split.avail_idx_shadow = 0;
2106
Rusty Russell0a8a69d2007-10-22 11:03:40 +10002107 /* No callback? Tell other side not to bother us. */
Venkatesh Srinivasf277ec42015-11-10 16:21:07 -08002108 if (!callback) {
Tiwei Biee593bf92018-11-21 18:03:21 +08002109 vq->split.avail_flags_shadow |= VRING_AVAIL_F_NO_INTERRUPT;
Ladi Prosek0ea1e4a2016-08-31 14:00:04 +02002110 if (!vq->event)
Tiwei Biee593bf92018-11-21 18:03:21 +08002111 vq->split.vring.avail->flags = cpu_to_virtio16(vdev,
2112 vq->split.avail_flags_shadow);
Venkatesh Srinivasf277ec42015-11-10 16:21:07 -08002113 }
Rusty Russell0a8a69d2007-10-22 11:03:40 +10002114
Tiwei Biecbeedb72018-11-21 18:03:24 +08002115 vq->split.desc_state = kmalloc_array(vring.num,
2116 sizeof(struct vring_desc_state_split), GFP_KERNEL);
2117 if (!vq->split.desc_state) {
2118 kfree(vq);
2119 return NULL;
2120 }
2121
Rusty Russell0a8a69d2007-10-22 11:03:40 +10002122 /* Put everything in free lists. */
Rusty Russell0a8a69d2007-10-22 11:03:40 +10002123 vq->free_head = 0;
Andy Lutomirski2a2d1382016-02-02 21:46:37 -08002124 for (i = 0; i < vring.num-1; i++)
Tiwei Biee593bf92018-11-21 18:03:21 +08002125 vq->split.vring.desc[i].next = cpu_to_virtio16(vdev, i + 1);
Tiwei Biecbeedb72018-11-21 18:03:24 +08002126 memset(vq->split.desc_state, 0, vring.num *
2127 sizeof(struct vring_desc_state_split));
Rusty Russell0a8a69d2007-10-22 11:03:40 +10002128
Dan Carpentere152d8a2020-12-04 17:23:36 +03002129 list_add_tail(&vq->vq.list, &vdev->vqs);
Rusty Russell0a8a69d2007-10-22 11:03:40 +10002130 return &vq->vq;
2131}
Andy Lutomirski2a2d1382016-02-02 21:46:37 -08002132EXPORT_SYMBOL_GPL(__vring_new_virtqueue);
2133
Andy Lutomirski2a2d1382016-02-02 21:46:37 -08002134struct virtqueue *vring_create_virtqueue(
2135 unsigned int index,
2136 unsigned int num,
2137 unsigned int vring_align,
2138 struct virtio_device *vdev,
2139 bool weak_barriers,
2140 bool may_reduce_num,
Michael S. Tsirkinf94682d2017-03-06 18:32:29 +02002141 bool context,
Andy Lutomirski2a2d1382016-02-02 21:46:37 -08002142 bool (*notify)(struct virtqueue *),
2143 void (*callback)(struct virtqueue *),
2144 const char *name)
2145{
Tiwei Bie1ce9e602018-11-21 18:03:27 +08002146
2147 if (virtio_has_feature(vdev, VIRTIO_F_RING_PACKED))
2148 return vring_create_virtqueue_packed(index, num, vring_align,
2149 vdev, weak_barriers, may_reduce_num,
2150 context, notify, callback, name);
2151
Tiwei Bied79dca72018-11-21 18:03:25 +08002152 return vring_create_virtqueue_split(index, num, vring_align,
2153 vdev, weak_barriers, may_reduce_num,
2154 context, notify, callback, name);
Andy Lutomirski2a2d1382016-02-02 21:46:37 -08002155}
2156EXPORT_SYMBOL_GPL(vring_create_virtqueue);
2157
Tiwei Bie1ce9e602018-11-21 18:03:27 +08002158/* Only available for split ring */
Andy Lutomirski2a2d1382016-02-02 21:46:37 -08002159struct virtqueue *vring_new_virtqueue(unsigned int index,
2160 unsigned int num,
2161 unsigned int vring_align,
2162 struct virtio_device *vdev,
2163 bool weak_barriers,
Michael S. Tsirkinf94682d2017-03-06 18:32:29 +02002164 bool context,
Andy Lutomirski2a2d1382016-02-02 21:46:37 -08002165 void *pages,
2166 bool (*notify)(struct virtqueue *vq),
2167 void (*callback)(struct virtqueue *vq),
2168 const char *name)
2169{
2170 struct vring vring;
Tiwei Bie1ce9e602018-11-21 18:03:27 +08002171
2172 if (virtio_has_feature(vdev, VIRTIO_F_RING_PACKED))
2173 return NULL;
2174
Andy Lutomirski2a2d1382016-02-02 21:46:37 -08002175 vring_init(&vring, num, pages, vring_align);
Michael S. Tsirkinf94682d2017-03-06 18:32:29 +02002176 return __vring_new_virtqueue(index, vring, vdev, weak_barriers, context,
Andy Lutomirski2a2d1382016-02-02 21:46:37 -08002177 notify, callback, name);
2178}
Rusty Russellc6fd4702008-02-04 23:50:05 -05002179EXPORT_SYMBOL_GPL(vring_new_virtqueue);
Rusty Russell0a8a69d2007-10-22 11:03:40 +10002180
Andy Lutomirski2a2d1382016-02-02 21:46:37 -08002181void vring_del_virtqueue(struct virtqueue *_vq)
Rusty Russell0a8a69d2007-10-22 11:03:40 +10002182{
Andy Lutomirski2a2d1382016-02-02 21:46:37 -08002183 struct vring_virtqueue *vq = to_vvq(_vq);
2184
2185 if (vq->we_own_ring) {
Tiwei Bie1ce9e602018-11-21 18:03:27 +08002186 if (vq->packed_ring) {
2187 vring_free_queue(vq->vq.vdev,
2188 vq->packed.ring_size_in_bytes,
2189 vq->packed.vring.desc,
2190 vq->packed.ring_dma_addr);
2191
2192 vring_free_queue(vq->vq.vdev,
2193 vq->packed.event_size_in_bytes,
2194 vq->packed.vring.driver,
2195 vq->packed.driver_event_dma_addr);
2196
2197 vring_free_queue(vq->vq.vdev,
2198 vq->packed.event_size_in_bytes,
2199 vq->packed.vring.device,
2200 vq->packed.device_event_dma_addr);
2201
2202 kfree(vq->packed.desc_state);
2203 kfree(vq->packed.desc_extra);
2204 } else {
2205 vring_free_queue(vq->vq.vdev,
2206 vq->split.queue_size_in_bytes,
2207 vq->split.vring.desc,
2208 vq->split.queue_dma_addr);
Tiwei Bie1ce9e602018-11-21 18:03:27 +08002209 }
Andy Lutomirski2a2d1382016-02-02 21:46:37 -08002210 }
Suman Annaf13f09a2020-02-24 15:26:43 -06002211 if (!vq->packed_ring)
2212 kfree(vq->split.desc_state);
Andy Lutomirski2a2d1382016-02-02 21:46:37 -08002213 list_del(&_vq->list);
2214 kfree(vq);
Rusty Russell0a8a69d2007-10-22 11:03:40 +10002215}
Rusty Russellc6fd4702008-02-04 23:50:05 -05002216EXPORT_SYMBOL_GPL(vring_del_virtqueue);
Rusty Russell0a8a69d2007-10-22 11:03:40 +10002217
Rusty Russelle34f8722008-07-25 12:06:13 -05002218/* Manipulates transport-specific feature bits. */
2219void vring_transport_features(struct virtio_device *vdev)
2220{
2221 unsigned int i;
2222
2223 for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++) {
2224 switch (i) {
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +01002225 case VIRTIO_RING_F_INDIRECT_DESC:
2226 break;
Michael S. Tsirkina5c262c2011-05-20 02:10:44 +03002227 case VIRTIO_RING_F_EVENT_IDX:
2228 break;
Michael S. Tsirkin747ae342014-12-01 15:52:40 +02002229 case VIRTIO_F_VERSION_1:
2230 break;
Michael S. Tsirkin321bd212020-06-24 18:24:33 -04002231 case VIRTIO_F_ACCESS_PLATFORM:
Michael S. Tsirkin1a937692016-04-18 12:58:14 +03002232 break;
Tiwei Bief959a122018-11-21 18:03:30 +08002233 case VIRTIO_F_RING_PACKED:
2234 break;
Tiwei Bie45383fb2019-01-23 17:50:26 +08002235 case VIRTIO_F_ORDER_PLATFORM:
2236 break;
Rusty Russelle34f8722008-07-25 12:06:13 -05002237 default:
2238 /* We don't understand this bit. */
Michael S. Tsirkine16e12b2014-10-07 16:39:42 +02002239 __virtio_clear_bit(vdev, i);
Rusty Russelle34f8722008-07-25 12:06:13 -05002240 }
2241 }
2242}
2243EXPORT_SYMBOL_GPL(vring_transport_features);
2244
Rusty Russell5dfc1762012-01-12 15:44:42 +10302245/**
2246 * virtqueue_get_vring_size - return the size of the virtqueue's vring
Jiang Biaoa5581202019-04-23 18:25:12 +08002247 * @_vq: the struct virtqueue containing the vring of interest.
Rusty Russell5dfc1762012-01-12 15:44:42 +10302248 *
2249 * Returns the size of the vring. This is mainly used for boasting to
2250 * userspace. Unlike other operations, this need not be serialized.
2251 */
Rick Jones8f9f4662011-10-19 08:10:59 +00002252unsigned int virtqueue_get_vring_size(struct virtqueue *_vq)
2253{
2254
2255 struct vring_virtqueue *vq = to_vvq(_vq);
2256
Tiwei Bie1ce9e602018-11-21 18:03:27 +08002257 return vq->packed_ring ? vq->packed.vring.num : vq->split.vring.num;
Rick Jones8f9f4662011-10-19 08:10:59 +00002258}
2259EXPORT_SYMBOL_GPL(virtqueue_get_vring_size);
2260
Heinz Graalfsb3b32c92013-10-29 09:40:19 +10302261bool virtqueue_is_broken(struct virtqueue *_vq)
2262{
2263 struct vring_virtqueue *vq = to_vvq(_vq);
2264
2265 return vq->broken;
2266}
2267EXPORT_SYMBOL_GPL(virtqueue_is_broken);
2268
Rusty Russelle2dcdfe2014-04-28 11:15:08 +09302269/*
2270 * This should prevent the device from being used, allowing drivers to
2271 * recover. You may need to grab appropriate locks to flush.
2272 */
2273void virtio_break_device(struct virtio_device *dev)
2274{
2275 struct virtqueue *_vq;
2276
2277 list_for_each_entry(_vq, &dev->vqs, list) {
2278 struct vring_virtqueue *vq = to_vvq(_vq);
2279 vq->broken = true;
2280 }
2281}
2282EXPORT_SYMBOL_GPL(virtio_break_device);
2283
Andy Lutomirski2a2d1382016-02-02 21:46:37 -08002284dma_addr_t virtqueue_get_desc_addr(struct virtqueue *_vq)
Cornelia Huck89062652014-10-07 16:39:47 +02002285{
2286 struct vring_virtqueue *vq = to_vvq(_vq);
2287
Andy Lutomirski2a2d1382016-02-02 21:46:37 -08002288 BUG_ON(!vq->we_own_ring);
Cornelia Huck89062652014-10-07 16:39:47 +02002289
Tiwei Bie1ce9e602018-11-21 18:03:27 +08002290 if (vq->packed_ring)
2291 return vq->packed.ring_dma_addr;
2292
Tiwei Bied79dca72018-11-21 18:03:25 +08002293 return vq->split.queue_dma_addr;
Andy Lutomirski2a2d1382016-02-02 21:46:37 -08002294}
2295EXPORT_SYMBOL_GPL(virtqueue_get_desc_addr);
2296
2297dma_addr_t virtqueue_get_avail_addr(struct virtqueue *_vq)
Cornelia Huck89062652014-10-07 16:39:47 +02002298{
2299 struct vring_virtqueue *vq = to_vvq(_vq);
2300
Andy Lutomirski2a2d1382016-02-02 21:46:37 -08002301 BUG_ON(!vq->we_own_ring);
2302
Tiwei Bie1ce9e602018-11-21 18:03:27 +08002303 if (vq->packed_ring)
2304 return vq->packed.driver_event_dma_addr;
2305
Tiwei Bied79dca72018-11-21 18:03:25 +08002306 return vq->split.queue_dma_addr +
Tiwei Biee593bf92018-11-21 18:03:21 +08002307 ((char *)vq->split.vring.avail - (char *)vq->split.vring.desc);
Cornelia Huck89062652014-10-07 16:39:47 +02002308}
Andy Lutomirski2a2d1382016-02-02 21:46:37 -08002309EXPORT_SYMBOL_GPL(virtqueue_get_avail_addr);
2310
2311dma_addr_t virtqueue_get_used_addr(struct virtqueue *_vq)
2312{
2313 struct vring_virtqueue *vq = to_vvq(_vq);
2314
2315 BUG_ON(!vq->we_own_ring);
2316
Tiwei Bie1ce9e602018-11-21 18:03:27 +08002317 if (vq->packed_ring)
2318 return vq->packed.device_event_dma_addr;
2319
Tiwei Bied79dca72018-11-21 18:03:25 +08002320 return vq->split.queue_dma_addr +
Tiwei Biee593bf92018-11-21 18:03:21 +08002321 ((char *)vq->split.vring.used - (char *)vq->split.vring.desc);
Andy Lutomirski2a2d1382016-02-02 21:46:37 -08002322}
2323EXPORT_SYMBOL_GPL(virtqueue_get_used_addr);
2324
Tiwei Bie1ce9e602018-11-21 18:03:27 +08002325/* Only available for split ring */
Andy Lutomirski2a2d1382016-02-02 21:46:37 -08002326const struct vring *virtqueue_get_vring(struct virtqueue *vq)
2327{
Tiwei Biee593bf92018-11-21 18:03:21 +08002328 return &to_vvq(vq)->split.vring;
Andy Lutomirski2a2d1382016-02-02 21:46:37 -08002329}
2330EXPORT_SYMBOL_GPL(virtqueue_get_vring);
Cornelia Huck89062652014-10-07 16:39:47 +02002331
Rusty Russellc6fd4702008-02-04 23:50:05 -05002332MODULE_LICENSE("GPL");