Commit | Line | Data |
---|---|---|
fd534e9b | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
0a8a69dd RR |
2 | /* Virtio ring implementation. |
3 | * | |
4 | * Copyright 2007 Rusty Russell IBM Corporation | |
0a8a69dd RR |
5 | */ |
6 | #include <linux/virtio.h> | |
7 | #include <linux/virtio_ring.h> | |
e34f8725 | 8 | #include <linux/virtio_config.h> |
0a8a69dd | 9 | #include <linux/device.h> |
5a0e3ad6 | 10 | #include <linux/slab.h> |
b5a2c4f1 | 11 | #include <linux/module.h> |
e93300b1 | 12 | #include <linux/hrtimer.h> |
780bc790 | 13 | #include <linux/dma-mapping.h> |
88938359 | 14 | #include <linux/kmsan.h> |
f8ce7263 | 15 | #include <linux/spinlock.h> |
78fe3987 | 16 | #include <xen/xen.h> |
0a8a69dd RR |
17 | |
18 | #ifdef DEBUG | |
19 | /* For development, we want to crash whenever the ring is screwed. */ | |
9499f5e7 RR |
20 | #define BAD_RING(_vq, fmt, args...) \ |
21 | do { \ | |
22 | dev_err(&(_vq)->vq.vdev->dev, \ | |
23 | "%s:"fmt, (_vq)->vq.name, ##args); \ | |
24 | BUG(); \ | |
25 | } while (0) | |
c5f841f1 RR |
26 | /* Caller is supposed to guarantee no reentry. */ |
27 | #define START_USE(_vq) \ | |
28 | do { \ | |
29 | if ((_vq)->in_use) \ | |
9499f5e7 RR |
30 | panic("%s:in_use = %i\n", \ |
31 | (_vq)->vq.name, (_vq)->in_use); \ | |
c5f841f1 | 32 | (_vq)->in_use = __LINE__; \ |
9499f5e7 | 33 | } while (0) |
3a35ce7d | 34 | #define END_USE(_vq) \ |
97a545ab | 35 | do { BUG_ON(!(_vq)->in_use); (_vq)->in_use = 0; } while(0) |
4d6a105e TB |
36 | #define LAST_ADD_TIME_UPDATE(_vq) \ |
37 | do { \ | |
38 | ktime_t now = ktime_get(); \ | |
39 | \ | |
40 | /* No kick or get, with .1 second between? Warn. */ \ | |
41 | if ((_vq)->last_add_time_valid) \ | |
42 | WARN_ON(ktime_to_ms(ktime_sub(now, \ | |
43 | (_vq)->last_add_time)) > 100); \ | |
44 | (_vq)->last_add_time = now; \ | |
45 | (_vq)->last_add_time_valid = true; \ | |
46 | } while (0) | |
47 | #define LAST_ADD_TIME_CHECK(_vq) \ | |
48 | do { \ | |
49 | if ((_vq)->last_add_time_valid) { \ | |
50 | WARN_ON(ktime_to_ms(ktime_sub(ktime_get(), \ | |
51 | (_vq)->last_add_time)) > 100); \ | |
52 | } \ | |
53 | } while (0) | |
54 | #define LAST_ADD_TIME_INVALID(_vq) \ | |
55 | ((_vq)->last_add_time_valid = false) | |
0a8a69dd | 56 | #else |
9499f5e7 RR |
57 | #define BAD_RING(_vq, fmt, args...) \ |
58 | do { \ | |
59 | dev_err(&_vq->vq.vdev->dev, \ | |
60 | "%s:"fmt, (_vq)->vq.name, ##args); \ | |
61 | (_vq)->broken = true; \ | |
62 | } while (0) | |
0a8a69dd RR |
63 | #define START_USE(vq) |
64 | #define END_USE(vq) | |
4d6a105e TB |
65 | #define LAST_ADD_TIME_UPDATE(vq) |
66 | #define LAST_ADD_TIME_CHECK(vq) | |
67 | #define LAST_ADD_TIME_INVALID(vq) | |
0a8a69dd RR |
68 | #endif |
69 | ||
cbeedb72 | 70 | struct vring_desc_state_split { |
780bc790 | 71 | void *data; /* Data for callback. */ |
bc2b4c34 XZ |
72 | |
73 | /* Indirect desc table and extra table, if any. These two will be | |
74 | * allocated together. So we won't stress more to the memory allocator. | |
75 | */ | |
76 | struct vring_desc *indir_desc; | |
780bc790 AL |
77 | }; |
78 | ||
1ce9e605 TB |
79 | struct vring_desc_state_packed { |
80 | void *data; /* Data for callback. */ | |
aaa78984 XZ |
81 | |
82 | /* Indirect desc table and extra table, if any. These two will be | |
83 | * allocated together. So we won't stress more to the memory allocator. | |
84 | */ | |
85 | struct vring_packed_desc *indir_desc; | |
1ce9e605 | 86 | u16 num; /* Descriptor list length. */ |
1ce9e605 TB |
87 | u16 last; /* The last desc state in a list. */ |
88 | }; | |
89 | ||
1f28750f | 90 | struct vring_desc_extra { |
ef5c366f JW |
91 | dma_addr_t addr; /* Descriptor DMA addr. */ |
92 | u32 len; /* Descriptor length. */ | |
1ce9e605 | 93 | u16 flags; /* Descriptor flags. */ |
aeef9b47 | 94 | u16 next; /* The next desc state in a list. */ |
1ce9e605 TB |
95 | }; |
96 | ||
d76136e4 XZ |
97 | struct vring_virtqueue_split { |
98 | /* Actual memory layout for this queue. */ | |
99 | struct vring vring; | |
100 | ||
101 | /* Last written value to avail->flags */ | |
102 | u16 avail_flags_shadow; | |
103 | ||
104 | /* | |
105 | * Last written value to avail->idx in | |
106 | * guest byte order. | |
107 | */ | |
108 | u16 avail_idx_shadow; | |
109 | ||
110 | /* Per-descriptor state. */ | |
111 | struct vring_desc_state_split *desc_state; | |
112 | struct vring_desc_extra *desc_extra; | |
113 | ||
114 | /* DMA address and size information */ | |
115 | dma_addr_t queue_dma_addr; | |
116 | size_t queue_size_in_bytes; | |
af36b16f XZ |
117 | |
118 | /* | |
119 | * The parameters for creating vrings are reserved for creating new | |
120 | * vring. | |
121 | */ | |
122 | u32 vring_align; | |
123 | bool may_reduce_num; | |
d76136e4 XZ |
124 | }; |
125 | ||
126 | struct vring_virtqueue_packed { | |
127 | /* Actual memory layout for this queue. */ | |
128 | struct { | |
129 | unsigned int num; | |
130 | struct vring_packed_desc *desc; | |
131 | struct vring_packed_desc_event *driver; | |
132 | struct vring_packed_desc_event *device; | |
133 | } vring; | |
134 | ||
135 | /* Driver ring wrap counter. */ | |
136 | bool avail_wrap_counter; | |
137 | ||
138 | /* Avail used flags. */ | |
139 | u16 avail_used_flags; | |
140 | ||
141 | /* Index of the next avail descriptor. */ | |
142 | u16 next_avail_idx; | |
143 | ||
144 | /* | |
145 | * Last written value to driver->flags in | |
146 | * guest byte order. | |
147 | */ | |
148 | u16 event_flags_shadow; | |
149 | ||
150 | /* Per-descriptor state. */ | |
151 | struct vring_desc_state_packed *desc_state; | |
152 | struct vring_desc_extra *desc_extra; | |
153 | ||
154 | /* DMA address and size information */ | |
155 | dma_addr_t ring_dma_addr; | |
156 | dma_addr_t driver_event_dma_addr; | |
157 | dma_addr_t device_event_dma_addr; | |
158 | size_t ring_size_in_bytes; | |
159 | size_t event_size_in_bytes; | |
160 | }; | |
161 | ||
43b4f721 | 162 | struct vring_virtqueue { |
0a8a69dd RR |
163 | struct virtqueue vq; |
164 | ||
1ce9e605 TB |
165 | /* Is this a packed ring? */ |
166 | bool packed_ring; | |
167 | ||
fb3fba6b TB |
168 | /* Is DMA API used? */ |
169 | bool use_dma_api; | |
170 | ||
7b21e34f RR |
171 | /* Can we use weak barriers? */ |
172 | bool weak_barriers; | |
173 | ||
0a8a69dd RR |
174 | /* Other side has made a mess, don't try any more. */ |
175 | bool broken; | |
176 | ||
9fa29b9d MM |
177 | /* Host supports indirect buffers */ |
178 | bool indirect; | |
179 | ||
a5c262c5 MT |
180 | /* Host publishes avail event idx */ |
181 | bool event; | |
182 | ||
0a8a69dd RR |
183 | /* Head of free buffer list. */ |
184 | unsigned int free_head; | |
185 | /* Number we've added since last sync. */ | |
186 | unsigned int num_added; | |
187 | ||
a7722890 | 188 | /* Last used index we've seen. |
189 | * for split ring, it just contains last used index | |
190 | * for packed ring: | |
191 | * bits up to VRING_PACKED_EVENT_F_WRAP_CTR include the last used index. | |
192 | * bits from VRING_PACKED_EVENT_F_WRAP_CTR include the used wrap counter. | |
193 | */ | |
1bc4953e | 194 | u16 last_used_idx; |
0a8a69dd | 195 | |
8d622d21 MT |
196 | /* Hint for event idx: already triggered no need to disable. */ |
197 | bool event_triggered; | |
198 | ||
1ce9e605 TB |
199 | union { |
200 | /* Available for split ring */ | |
d76136e4 | 201 | struct vring_virtqueue_split split; |
e593bf97 | 202 | |
1ce9e605 | 203 | /* Available for packed ring */ |
d76136e4 | 204 | struct vring_virtqueue_packed packed; |
1ce9e605 | 205 | }; |
f277ec42 | 206 | |
0a8a69dd | 207 | /* How to notify other side. FIXME: commonalize hcalls! */ |
46f9c2b9 | 208 | bool (*notify)(struct virtqueue *vq); |
0a8a69dd | 209 | |
2a2d1382 AL |
210 | /* DMA, allocation, and size information */ |
211 | bool we_own_ring; | |
2a2d1382 | 212 | |
2713ea3c JW |
213 | /* Device used for doing DMA */ |
214 | struct device *dma_dev; | |
215 | ||
0a8a69dd RR |
216 | #ifdef DEBUG |
217 | /* They're supposed to lock for us. */ | |
218 | unsigned int in_use; | |
e93300b1 RR |
219 | |
220 | /* Figure out if their kicks are too delayed. */ | |
221 | bool last_add_time_valid; | |
222 | ktime_t last_add_time; | |
0a8a69dd | 223 | #endif |
0a8a69dd RR |
224 | }; |
225 | ||
a2b36c8d | 226 | static struct vring_desc_extra *vring_alloc_desc_extra(unsigned int num); |
6fea20e5 | 227 | static void vring_free(struct virtqueue *_vq); |
e6f633e5 TB |
228 | |
229 | /* | |
230 | * Helpers. | |
231 | */ | |
232 | ||
4b6ec919 | 233 | #define to_vvq(_vq) container_of_const(_vq, struct vring_virtqueue, vq) |
0a8a69dd | 234 | |
4b6ec919 | 235 | static bool virtqueue_use_indirect(const struct vring_virtqueue *vq, |
1adbd6b2 | 236 | unsigned int total_sg) |
2f18c2d1 | 237 | { |
2f18c2d1 TB |
238 | /* |
239 | * If the host supports indirect descriptor tables, and we have multiple | |
240 | * buffers, then go indirect. FIXME: tune this threshold | |
241 | */ | |
242 | return (vq->indirect && total_sg > 1 && vq->vq.num_free); | |
243 | } | |
244 | ||
d26c96c8 | 245 | /* |
1a937693 MT |
246 | * Modern virtio devices have feature bits to specify whether they need a |
247 | * quirk and bypass the IOMMU. If not there, just use the DMA API. | |
248 | * | |
249 | * If there, the interaction between virtio and DMA API is messy. | |
d26c96c8 AL |
250 | * |
251 | * On most systems with virtio, physical addresses match bus addresses, | |
252 | * and it doesn't particularly matter whether we use the DMA API. | |
253 | * | |
254 | * On some systems, including Xen and any system with a physical device | |
255 | * that speaks virtio behind a physical IOMMU, we must use the DMA API | |
256 | * for virtio DMA to work at all. | |
257 | * | |
258 | * On other systems, including SPARC and PPC64, virtio-pci devices are | |
259 | * enumerated as though they are behind an IOMMU, but the virtio host | |
260 | * ignores the IOMMU, so we must either pretend that the IOMMU isn't | |
261 | * there or somehow map everything as the identity. | |
262 | * | |
263 | * For the time being, we preserve historic behavior and bypass the DMA | |
264 | * API. | |
1a937693 MT |
265 | * |
266 | * TODO: install a per-device DMA ops structure that does the right thing | |
267 | * taking into account all the above quirks, and use the DMA API | |
268 | * unconditionally on data path. | |
d26c96c8 AL |
269 | */ |
270 | ||
4b6ec919 | 271 | static bool vring_use_dma_api(const struct virtio_device *vdev) |
d26c96c8 | 272 | { |
24b6842a | 273 | if (!virtio_has_dma_quirk(vdev)) |
1a937693 MT |
274 | return true; |
275 | ||
276 | /* Otherwise, we are left to guess. */ | |
78fe3987 AL |
277 | /* |
278 | * In theory, it's possible to have a buggy QEMU-supposed | |
279 | * emulated Q35 IOMMU and Xen enabled at the same time. On | |
280 | * such a configuration, virtio has never worked and will | |
281 | * not work without an even larger kludge. Instead, enable | |
282 | * the DMA API if we're a Xen guest, which at least allows | |
283 | * all of the sensible Xen configurations to work correctly. | |
284 | */ | |
285 | if (xen_domain()) | |
286 | return true; | |
287 | ||
d26c96c8 AL |
288 | return false; |
289 | } | |
290 | ||
c7e1b422 XZ |
291 | static bool vring_need_unmap_buffer(const struct vring_virtqueue *vring, |
292 | const struct vring_desc_extra *extra) | |
9f19c084 | 293 | { |
c7e1b422 | 294 | return vring->use_dma_api && (extra->addr != DMA_MAPPING_ERROR); |
9f19c084 XZ |
295 | } |
296 | ||
4b6ec919 | 297 | size_t virtio_max_dma_size(const struct virtio_device *vdev) |
e6d6dd6c JR |
298 | { |
299 | size_t max_segment_size = SIZE_MAX; | |
300 | ||
301 | if (vring_use_dma_api(vdev)) | |
817fc978 | 302 | max_segment_size = dma_max_mapping_size(vdev->dev.parent); |
e6d6dd6c JR |
303 | |
304 | return max_segment_size; | |
305 | } | |
306 | EXPORT_SYMBOL_GPL(virtio_max_dma_size); | |
307 | ||
d79dca75 | 308 | static void *vring_alloc_queue(struct virtio_device *vdev, size_t size, |
2713ea3c JW |
309 | dma_addr_t *dma_handle, gfp_t flag, |
310 | struct device *dma_dev) | |
d79dca75 TB |
311 | { |
312 | if (vring_use_dma_api(vdev)) { | |
2713ea3c | 313 | return dma_alloc_coherent(dma_dev, size, |
d79dca75 TB |
314 | dma_handle, flag); |
315 | } else { | |
316 | void *queue = alloc_pages_exact(PAGE_ALIGN(size), flag); | |
317 | ||
318 | if (queue) { | |
319 | phys_addr_t phys_addr = virt_to_phys(queue); | |
320 | *dma_handle = (dma_addr_t)phys_addr; | |
321 | ||
322 | /* | |
323 | * Sanity check: make sure we dind't truncate | |
324 | * the address. The only arches I can find that | |
325 | * have 64-bit phys_addr_t but 32-bit dma_addr_t | |
326 | * are certain non-highmem MIPS and x86 | |
327 | * configurations, but these configurations | |
328 | * should never allocate physical pages above 32 | |
329 | * bits, so this is fine. Just in case, throw a | |
330 | * warning and abort if we end up with an | |
331 | * unrepresentable address. | |
332 | */ | |
333 | if (WARN_ON_ONCE(*dma_handle != phys_addr)) { | |
334 | free_pages_exact(queue, PAGE_ALIGN(size)); | |
335 | return NULL; | |
336 | } | |
337 | } | |
338 | return queue; | |
339 | } | |
340 | } | |
341 | ||
342 | static void vring_free_queue(struct virtio_device *vdev, size_t size, | |
2713ea3c JW |
343 | void *queue, dma_addr_t dma_handle, |
344 | struct device *dma_dev) | |
d79dca75 TB |
345 | { |
346 | if (vring_use_dma_api(vdev)) | |
2713ea3c | 347 | dma_free_coherent(dma_dev, size, queue, dma_handle); |
d79dca75 TB |
348 | else |
349 | free_pages_exact(queue, PAGE_ALIGN(size)); | |
350 | } | |
351 | ||
780bc790 AL |
352 | /* |
353 | * The DMA ops on various arches are rather gnarly right now, and | |
354 | * making all of the arch DMA ops work on the vring device itself | |
2713ea3c | 355 | * is a mess. |
780bc790 | 356 | */ |
1adbd6b2 | 357 | static struct device *vring_dma_dev(const struct vring_virtqueue *vq) |
780bc790 | 358 | { |
2713ea3c | 359 | return vq->dma_dev; |
780bc790 AL |
360 | } |
361 | ||
362 | /* Map one sg entry. */ | |
0e27fa6d | 363 | static int vring_map_one_sg(const struct vring_virtqueue *vq, struct scatterlist *sg, |
c7e1b422 XZ |
364 | enum dma_data_direction direction, dma_addr_t *addr, |
365 | u32 *len, bool premapped) | |
780bc790 | 366 | { |
c7e1b422 | 367 | if (premapped) { |
d7344a2f | 368 | *addr = sg_dma_address(sg); |
c7e1b422 | 369 | *len = sg_dma_len(sg); |
d7344a2f XZ |
370 | return 0; |
371 | } | |
372 | ||
c7e1b422 XZ |
373 | *len = sg->length; |
374 | ||
88938359 AP |
375 | if (!vq->use_dma_api) { |
376 | /* | |
377 | * If DMA is not used, KMSAN doesn't know that the scatterlist | |
378 | * is initialized by the hardware. Explicitly check/unpoison it | |
379 | * depending on the direction. | |
380 | */ | |
381 | kmsan_handle_dma(sg_page(sg), sg->offset, sg->length, direction); | |
0e27fa6d XZ |
382 | *addr = (dma_addr_t)sg_phys(sg); |
383 | return 0; | |
88938359 | 384 | } |
780bc790 AL |
385 | |
386 | /* | |
387 | * We can't use dma_map_sg, because we don't use scatterlists in | |
388 | * the way it expects (we don't guarantee that the scatterlist | |
389 | * will exist for the lifetime of the mapping). | |
390 | */ | |
0e27fa6d | 391 | *addr = dma_map_page(vring_dma_dev(vq), |
780bc790 AL |
392 | sg_page(sg), sg->offset, sg->length, |
393 | direction); | |
0e27fa6d XZ |
394 | |
395 | if (dma_mapping_error(vring_dma_dev(vq), *addr)) | |
396 | return -ENOMEM; | |
397 | ||
398 | return 0; | |
780bc790 AL |
399 | } |
400 | ||
401 | static dma_addr_t vring_map_single(const struct vring_virtqueue *vq, | |
402 | void *cpu_addr, size_t size, | |
403 | enum dma_data_direction direction) | |
404 | { | |
fb3fba6b | 405 | if (!vq->use_dma_api) |
780bc790 AL |
406 | return (dma_addr_t)virt_to_phys(cpu_addr); |
407 | ||
408 | return dma_map_single(vring_dma_dev(vq), | |
409 | cpu_addr, size, direction); | |
410 | } | |
411 | ||
e6f633e5 TB |
412 | static int vring_mapping_error(const struct vring_virtqueue *vq, |
413 | dma_addr_t addr) | |
414 | { | |
fb3fba6b | 415 | if (!vq->use_dma_api) |
e6f633e5 TB |
416 | return 0; |
417 | ||
418 | return dma_mapping_error(vring_dma_dev(vq), addr); | |
419 | } | |
420 | ||
3a897128 XZ |
421 | static void virtqueue_init(struct vring_virtqueue *vq, u32 num) |
422 | { | |
423 | vq->vq.num_free = num; | |
424 | ||
425 | if (vq->packed_ring) | |
426 | vq->last_used_idx = 0 | (1 << VRING_PACKED_EVENT_F_WRAP_CTR); | |
427 | else | |
428 | vq->last_used_idx = 0; | |
429 | ||
430 | vq->event_triggered = false; | |
431 | vq->num_added = 0; | |
432 | ||
433 | #ifdef DEBUG | |
434 | vq->in_use = false; | |
435 | vq->last_add_time_valid = false; | |
436 | #endif | |
437 | } | |
438 | ||
e6f633e5 TB |
439 | |
440 | /* | |
441 | * Split ring specific functions - *_split(). | |
442 | */ | |
443 | ||
72b5e895 | 444 | static unsigned int vring_unmap_one_split(const struct vring_virtqueue *vq, |
bc2b4c34 | 445 | struct vring_desc_extra *extra) |
72b5e895 | 446 | { |
72b5e895 JW |
447 | u16 flags; |
448 | ||
bc2b4c34 | 449 | flags = extra->flags; |
72b5e895 JW |
450 | |
451 | if (flags & VRING_DESC_F_INDIRECT) { | |
b319940f XZ |
452 | if (!vq->use_dma_api) |
453 | goto out; | |
454 | ||
72b5e895 | 455 | dma_unmap_single(vring_dma_dev(vq), |
bc2b4c34 XZ |
456 | extra->addr, |
457 | extra->len, | |
72b5e895 JW |
458 | (flags & VRING_DESC_F_WRITE) ? |
459 | DMA_FROM_DEVICE : DMA_TO_DEVICE); | |
460 | } else { | |
c7e1b422 | 461 | if (!vring_need_unmap_buffer(vq, extra)) |
b319940f XZ |
462 | goto out; |
463 | ||
72b5e895 | 464 | dma_unmap_page(vring_dma_dev(vq), |
bc2b4c34 XZ |
465 | extra->addr, |
466 | extra->len, | |
72b5e895 JW |
467 | (flags & VRING_DESC_F_WRITE) ? |
468 | DMA_FROM_DEVICE : DMA_TO_DEVICE); | |
469 | } | |
470 | ||
471 | out: | |
bc2b4c34 | 472 | return extra->next; |
72b5e895 JW |
473 | } |
474 | ||
138fd251 TB |
475 | static struct vring_desc *alloc_indirect_split(struct virtqueue *_vq, |
476 | unsigned int total_sg, | |
477 | gfp_t gfp) | |
9fa29b9d | 478 | { |
bc2b4c34 | 479 | struct vring_desc_extra *extra; |
9fa29b9d | 480 | struct vring_desc *desc; |
bc2b4c34 | 481 | unsigned int i, size; |
9fa29b9d | 482 | |
b92b1b89 WD |
483 | /* |
484 | * We require lowmem mappings for the descriptors because | |
485 | * otherwise virt_to_phys will give us bogus addresses in the | |
486 | * virtqueue. | |
487 | */ | |
82107539 | 488 | gfp &= ~__GFP_HIGHMEM; |
b92b1b89 | 489 | |
bc2b4c34 XZ |
490 | size = sizeof(*desc) * total_sg + sizeof(*extra) * total_sg; |
491 | ||
492 | desc = kmalloc(size, gfp); | |
9fa29b9d | 493 | if (!desc) |
b25bd251 | 494 | return NULL; |
9fa29b9d | 495 | |
bc2b4c34 XZ |
496 | extra = (struct vring_desc_extra *)&desc[total_sg]; |
497 | ||
b25bd251 | 498 | for (i = 0; i < total_sg; i++) |
bc2b4c34 XZ |
499 | extra[i].next = i + 1; |
500 | ||
b25bd251 | 501 | return desc; |
9fa29b9d MM |
502 | } |
503 | ||
fe4c3862 JW |
504 | static inline unsigned int virtqueue_add_desc_split(struct virtqueue *vq, |
505 | struct vring_desc *desc, | |
bc2b4c34 | 506 | struct vring_desc_extra *extra, |
fe4c3862 JW |
507 | unsigned int i, |
508 | dma_addr_t addr, | |
509 | unsigned int len, | |
c7e1b422 | 510 | u16 flags, bool premapped) |
fe4c3862 | 511 | { |
72b5e895 JW |
512 | u16 next; |
513 | ||
fe4c3862 JW |
514 | desc[i].flags = cpu_to_virtio16(vq->vdev, flags); |
515 | desc[i].addr = cpu_to_virtio64(vq->vdev, addr); | |
516 | desc[i].len = cpu_to_virtio32(vq->vdev, len); | |
517 | ||
c7e1b422 | 518 | extra[i].addr = premapped ? DMA_MAPPING_ERROR : addr; |
bc2b4c34 XZ |
519 | extra[i].len = len; |
520 | extra[i].flags = flags; | |
72b5e895 | 521 | |
bc2b4c34 XZ |
522 | next = extra[i].next; |
523 | ||
524 | desc[i].next = cpu_to_virtio16(vq->vdev, next); | |
72b5e895 JW |
525 | |
526 | return next; | |
fe4c3862 JW |
527 | } |
528 | ||
138fd251 TB |
529 | static inline int virtqueue_add_split(struct virtqueue *_vq, |
530 | struct scatterlist *sgs[], | |
531 | unsigned int total_sg, | |
532 | unsigned int out_sgs, | |
533 | unsigned int in_sgs, | |
534 | void *data, | |
535 | void *ctx, | |
c7e1b422 | 536 | bool premapped, |
138fd251 | 537 | gfp_t gfp) |
0a8a69dd RR |
538 | { |
539 | struct vring_virtqueue *vq = to_vvq(_vq); | |
bc2b4c34 | 540 | struct vring_desc_extra *extra; |
13816c76 | 541 | struct scatterlist *sg; |
b25bd251 | 542 | struct vring_desc *desc; |
3f649ab7 | 543 | unsigned int i, n, avail, descs_used, prev, err_idx; |
1fe9b6fe | 544 | int head; |
b25bd251 | 545 | bool indirect; |
0a8a69dd | 546 | |
9fa29b9d MM |
547 | START_USE(vq); |
548 | ||
0a8a69dd | 549 | BUG_ON(data == NULL); |
5a08b04f | 550 | BUG_ON(ctx && vq->indirect); |
9fa29b9d | 551 | |
70670444 RR |
552 | if (unlikely(vq->broken)) { |
553 | END_USE(vq); | |
554 | return -EIO; | |
555 | } | |
556 | ||
4d6a105e | 557 | LAST_ADD_TIME_UPDATE(vq); |
e93300b1 | 558 | |
b25bd251 RR |
559 | BUG_ON(total_sg == 0); |
560 | ||
561 | head = vq->free_head; | |
562 | ||
35c51e09 | 563 | if (virtqueue_use_indirect(vq, total_sg)) |
138fd251 | 564 | desc = alloc_indirect_split(_vq, total_sg, gfp); |
44ed8089 | 565 | else { |
b25bd251 | 566 | desc = NULL; |
e593bf97 | 567 | WARN_ON_ONCE(total_sg > vq->split.vring.num && !vq->indirect); |
44ed8089 | 568 | } |
b25bd251 RR |
569 | |
570 | if (desc) { | |
571 | /* Use a single buffer which doesn't continue */ | |
780bc790 | 572 | indirect = true; |
b25bd251 RR |
573 | /* Set up rest to use this indirect table. */ |
574 | i = 0; | |
575 | descs_used = 1; | |
bc2b4c34 | 576 | extra = (struct vring_desc_extra *)&desc[total_sg]; |
b25bd251 | 577 | } else { |
780bc790 | 578 | indirect = false; |
e593bf97 | 579 | desc = vq->split.vring.desc; |
bc2b4c34 | 580 | extra = vq->split.desc_extra; |
b25bd251 RR |
581 | i = head; |
582 | descs_used = total_sg; | |
9fa29b9d MM |
583 | } |
584 | ||
b4b4ff73 | 585 | if (unlikely(vq->vq.num_free < descs_used)) { |
0a8a69dd | 586 | pr_debug("Can't add buf len %i - avail = %i\n", |
b25bd251 | 587 | descs_used, vq->vq.num_free); |
44653eae RR |
588 | /* FIXME: for historical reasons, we force a notify here if |
589 | * there are outgoing parts to the buffer. Presumably the | |
590 | * host should service the ring ASAP. */ | |
13816c76 | 591 | if (out_sgs) |
44653eae | 592 | vq->notify(&vq->vq); |
58625edf WY |
593 | if (indirect) |
594 | kfree(desc); | |
0a8a69dd RR |
595 | END_USE(vq); |
596 | return -ENOSPC; | |
597 | } | |
598 | ||
13816c76 | 599 | for (n = 0; n < out_sgs; n++) { |
eeebf9b1 | 600 | for (sg = sgs[n]; sg; sg = sg_next(sg)) { |
0e27fa6d | 601 | dma_addr_t addr; |
c7e1b422 | 602 | u32 len; |
0e27fa6d | 603 | |
c7e1b422 | 604 | if (vring_map_one_sg(vq, sg, DMA_TO_DEVICE, &addr, &len, premapped)) |
780bc790 AL |
605 | goto unmap_release; |
606 | ||
13816c76 | 607 | prev = i; |
72b5e895 JW |
608 | /* Note that we trust indirect descriptor |
609 | * table since it use stream DMA mapping. | |
610 | */ | |
c7e1b422 XZ |
611 | i = virtqueue_add_desc_split(_vq, desc, extra, i, addr, len, |
612 | VRING_DESC_F_NEXT, | |
613 | premapped); | |
13816c76 | 614 | } |
0a8a69dd | 615 | } |
13816c76 | 616 | for (; n < (out_sgs + in_sgs); n++) { |
eeebf9b1 | 617 | for (sg = sgs[n]; sg; sg = sg_next(sg)) { |
0e27fa6d | 618 | dma_addr_t addr; |
c7e1b422 | 619 | u32 len; |
0e27fa6d | 620 | |
c7e1b422 | 621 | if (vring_map_one_sg(vq, sg, DMA_FROM_DEVICE, &addr, &len, premapped)) |
780bc790 AL |
622 | goto unmap_release; |
623 | ||
13816c76 | 624 | prev = i; |
72b5e895 JW |
625 | /* Note that we trust indirect descriptor |
626 | * table since it use stream DMA mapping. | |
627 | */ | |
c7e1b422 | 628 | i = virtqueue_add_desc_split(_vq, desc, extra, i, addr, len, |
fe4c3862 | 629 | VRING_DESC_F_NEXT | |
c7e1b422 XZ |
630 | VRING_DESC_F_WRITE, |
631 | premapped); | |
13816c76 | 632 | } |
0a8a69dd RR |
633 | } |
634 | /* Last one doesn't continue. */ | |
00e6f3d9 | 635 | desc[prev].flags &= cpu_to_virtio16(_vq->vdev, ~VRING_DESC_F_NEXT); |
c7e1b422 | 636 | if (!indirect && vring_need_unmap_buffer(vq, &extra[prev])) |
890d3356 | 637 | vq->split.desc_extra[prev & (vq->split.vring.num - 1)].flags &= |
72b5e895 | 638 | ~VRING_DESC_F_NEXT; |
0a8a69dd | 639 | |
780bc790 AL |
640 | if (indirect) { |
641 | /* Now that the indirect table is filled in, map it. */ | |
642 | dma_addr_t addr = vring_map_single( | |
643 | vq, desc, total_sg * sizeof(struct vring_desc), | |
644 | DMA_TO_DEVICE); | |
c7e1b422 | 645 | if (vring_mapping_error(vq, addr)) |
780bc790 AL |
646 | goto unmap_release; |
647 | ||
fe4c3862 | 648 | virtqueue_add_desc_split(_vq, vq->split.vring.desc, |
bc2b4c34 | 649 | vq->split.desc_extra, |
fe4c3862 JW |
650 | head, addr, |
651 | total_sg * sizeof(struct vring_desc), | |
c7e1b422 | 652 | VRING_DESC_F_INDIRECT, false); |
780bc790 AL |
653 | } |
654 | ||
655 | /* We're using some buffers from the free list. */ | |
656 | vq->vq.num_free -= descs_used; | |
657 | ||
0a8a69dd | 658 | /* Update free pointer */ |
b25bd251 | 659 | if (indirect) |
72b5e895 | 660 | vq->free_head = vq->split.desc_extra[head].next; |
b25bd251 RR |
661 | else |
662 | vq->free_head = i; | |
0a8a69dd | 663 | |
780bc790 | 664 | /* Store token and indirect buffer state. */ |
cbeedb72 | 665 | vq->split.desc_state[head].data = data; |
780bc790 | 666 | if (indirect) |
cbeedb72 | 667 | vq->split.desc_state[head].indir_desc = desc; |
87646a34 | 668 | else |
cbeedb72 | 669 | vq->split.desc_state[head].indir_desc = ctx; |
0a8a69dd RR |
670 | |
671 | /* Put entry in available array (but don't update avail->idx until they | |
3b720b8c | 672 | * do sync). */ |
e593bf97 TB |
673 | avail = vq->split.avail_idx_shadow & (vq->split.vring.num - 1); |
674 | vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head); | |
0a8a69dd | 675 | |
ee7cd898 RR |
676 | /* Descriptors and available array need to be set before we expose the |
677 | * new available array entries. */ | |
a9a0fef7 | 678 | virtio_wmb(vq->weak_barriers); |
e593bf97 TB |
679 | vq->split.avail_idx_shadow++; |
680 | vq->split.vring.avail->idx = cpu_to_virtio16(_vq->vdev, | |
681 | vq->split.avail_idx_shadow); | |
ee7cd898 RR |
682 | vq->num_added++; |
683 | ||
5e05bf58 TH |
684 | pr_debug("Added buffer head %i to %p\n", head, vq); |
685 | END_USE(vq); | |
686 | ||
ee7cd898 RR |
687 | /* This is very unlikely, but theoretically possible. Kick |
688 | * just in case. */ | |
689 | if (unlikely(vq->num_added == (1 << 16) - 1)) | |
690 | virtqueue_kick(_vq); | |
691 | ||
98e8c6bc | 692 | return 0; |
780bc790 AL |
693 | |
694 | unmap_release: | |
695 | err_idx = i; | |
cf8f1696 ML |
696 | |
697 | if (indirect) | |
698 | i = 0; | |
699 | else | |
700 | i = head; | |
780bc790 AL |
701 | |
702 | for (n = 0; n < total_sg; n++) { | |
703 | if (i == err_idx) | |
704 | break; | |
bc2b4c34 XZ |
705 | |
706 | i = vring_unmap_one_split(vq, &extra[i]); | |
780bc790 AL |
707 | } |
708 | ||
780bc790 AL |
709 | if (indirect) |
710 | kfree(desc); | |
711 | ||
3cc36f6e | 712 | END_USE(vq); |
f7728002 | 713 | return -ENOMEM; |
0a8a69dd | 714 | } |
13816c76 | 715 | |
138fd251 | 716 | static bool virtqueue_kick_prepare_split(struct virtqueue *_vq) |
0a8a69dd RR |
717 | { |
718 | struct vring_virtqueue *vq = to_vvq(_vq); | |
a5c262c5 | 719 | u16 new, old; |
41f0377f RR |
720 | bool needs_kick; |
721 | ||
0a8a69dd | 722 | START_USE(vq); |
a72caae2 JW |
723 | /* We need to expose available array entries before checking avail |
724 | * event. */ | |
a9a0fef7 | 725 | virtio_mb(vq->weak_barriers); |
0a8a69dd | 726 | |
e593bf97 TB |
727 | old = vq->split.avail_idx_shadow - vq->num_added; |
728 | new = vq->split.avail_idx_shadow; | |
0a8a69dd RR |
729 | vq->num_added = 0; |
730 | ||
4d6a105e TB |
731 | LAST_ADD_TIME_CHECK(vq); |
732 | LAST_ADD_TIME_INVALID(vq); | |
e93300b1 | 733 | |
41f0377f | 734 | if (vq->event) { |
e593bf97 TB |
735 | needs_kick = vring_need_event(virtio16_to_cpu(_vq->vdev, |
736 | vring_avail_event(&vq->split.vring)), | |
41f0377f RR |
737 | new, old); |
738 | } else { | |
e593bf97 TB |
739 | needs_kick = !(vq->split.vring.used->flags & |
740 | cpu_to_virtio16(_vq->vdev, | |
741 | VRING_USED_F_NO_NOTIFY)); | |
41f0377f | 742 | } |
0a8a69dd | 743 | END_USE(vq); |
41f0377f RR |
744 | return needs_kick; |
745 | } | |
138fd251 | 746 | |
138fd251 TB |
747 | static void detach_buf_split(struct vring_virtqueue *vq, unsigned int head, |
748 | void **ctx) | |
0a8a69dd | 749 | { |
bc2b4c34 | 750 | struct vring_desc_extra *extra; |
780bc790 | 751 | unsigned int i, j; |
c60923cb | 752 | __virtio16 nextflag = cpu_to_virtio16(vq->vq.vdev, VRING_DESC_F_NEXT); |
0a8a69dd RR |
753 | |
754 | /* Clear data ptr. */ | |
cbeedb72 | 755 | vq->split.desc_state[head].data = NULL; |
0a8a69dd | 756 | |
bc2b4c34 XZ |
757 | extra = vq->split.desc_extra; |
758 | ||
780bc790 | 759 | /* Put back on free list: unmap first-level descriptors and find end */ |
0a8a69dd | 760 | i = head; |
9fa29b9d | 761 | |
e593bf97 | 762 | while (vq->split.vring.desc[i].flags & nextflag) { |
bc2b4c34 | 763 | vring_unmap_one_split(vq, &extra[i]); |
72b5e895 | 764 | i = vq->split.desc_extra[i].next; |
06ca287d | 765 | vq->vq.num_free++; |
0a8a69dd RR |
766 | } |
767 | ||
bc2b4c34 | 768 | vring_unmap_one_split(vq, &extra[i]); |
72b5e895 | 769 | vq->split.desc_extra[i].next = vq->free_head; |
0a8a69dd | 770 | vq->free_head = head; |
780bc790 | 771 | |
0a8a69dd | 772 | /* Plus final descriptor */ |
06ca287d | 773 | vq->vq.num_free++; |
780bc790 | 774 | |
5a08b04f | 775 | if (vq->indirect) { |
cbeedb72 TB |
776 | struct vring_desc *indir_desc = |
777 | vq->split.desc_state[head].indir_desc; | |
bc2b4c34 | 778 | u32 len, num; |
5a08b04f MT |
779 | |
780 | /* Free the indirect table, if any, now that it's unmapped. */ | |
781 | if (!indir_desc) | |
782 | return; | |
72b5e895 | 783 | len = vq->split.desc_extra[head].len; |
780bc790 | 784 | |
72b5e895 JW |
785 | BUG_ON(!(vq->split.desc_extra[head].flags & |
786 | VRING_DESC_F_INDIRECT)); | |
780bc790 AL |
787 | BUG_ON(len == 0 || len % sizeof(struct vring_desc)); |
788 | ||
bc2b4c34 XZ |
789 | num = len / sizeof(struct vring_desc); |
790 | ||
791 | extra = (struct vring_desc_extra *)&indir_desc[num]; | |
792 | ||
c7e1b422 | 793 | if (vq->use_dma_api) { |
bc2b4c34 XZ |
794 | for (j = 0; j < num; j++) |
795 | vring_unmap_one_split(vq, &extra[j]); | |
610c708b | 796 | } |
780bc790 | 797 | |
5a08b04f | 798 | kfree(indir_desc); |
cbeedb72 | 799 | vq->split.desc_state[head].indir_desc = NULL; |
5a08b04f | 800 | } else if (ctx) { |
cbeedb72 | 801 | *ctx = vq->split.desc_state[head].indir_desc; |
780bc790 | 802 | } |
0a8a69dd RR |
803 | } |
804 | ||
1adbd6b2 | 805 | static bool more_used_split(const struct vring_virtqueue *vq) |
0a8a69dd | 806 | { |
e593bf97 TB |
807 | return vq->last_used_idx != virtio16_to_cpu(vq->vq.vdev, |
808 | vq->split.vring.used->idx); | |
0a8a69dd RR |
809 | } |
810 | ||
138fd251 TB |
811 | static void *virtqueue_get_buf_ctx_split(struct virtqueue *_vq, |
812 | unsigned int *len, | |
813 | void **ctx) | |
0a8a69dd RR |
814 | { |
815 | struct vring_virtqueue *vq = to_vvq(_vq); | |
816 | void *ret; | |
817 | unsigned int i; | |
3b720b8c | 818 | u16 last_used; |
0a8a69dd RR |
819 | |
820 | START_USE(vq); | |
821 | ||
5ef82752 RR |
822 | if (unlikely(vq->broken)) { |
823 | END_USE(vq); | |
824 | return NULL; | |
825 | } | |
826 | ||
138fd251 | 827 | if (!more_used_split(vq)) { |
0a8a69dd RR |
828 | pr_debug("No more buffers in queue\n"); |
829 | END_USE(vq); | |
830 | return NULL; | |
831 | } | |
832 | ||
2d61ba95 | 833 | /* Only get used array entries after they have been exposed by host. */ |
a9a0fef7 | 834 | virtio_rmb(vq->weak_barriers); |
2d61ba95 | 835 | |
e593bf97 TB |
836 | last_used = (vq->last_used_idx & (vq->split.vring.num - 1)); |
837 | i = virtio32_to_cpu(_vq->vdev, | |
838 | vq->split.vring.used->ring[last_used].id); | |
839 | *len = virtio32_to_cpu(_vq->vdev, | |
840 | vq->split.vring.used->ring[last_used].len); | |
0a8a69dd | 841 | |
e593bf97 | 842 | if (unlikely(i >= vq->split.vring.num)) { |
0a8a69dd RR |
843 | BAD_RING(vq, "id %u out of range\n", i); |
844 | return NULL; | |
845 | } | |
cbeedb72 | 846 | if (unlikely(!vq->split.desc_state[i].data)) { |
0a8a69dd RR |
847 | BAD_RING(vq, "id %u is not a head!\n", i); |
848 | return NULL; | |
849 | } | |
850 | ||
138fd251 | 851 | /* detach_buf_split clears data, so grab it now. */ |
cbeedb72 | 852 | ret = vq->split.desc_state[i].data; |
138fd251 | 853 | detach_buf_split(vq, i, ctx); |
0a8a69dd | 854 | vq->last_used_idx++; |
a5c262c5 MT |
855 | /* If we expect an interrupt for the next entry, tell host |
856 | * by writing event index and flush out the write before | |
857 | * the read in the next get_buf call. */ | |
e593bf97 | 858 | if (!(vq->split.avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT)) |
788e5b3a | 859 | virtio_store_mb(vq->weak_barriers, |
e593bf97 | 860 | &vring_used_event(&vq->split.vring), |
788e5b3a | 861 | cpu_to_virtio16(_vq->vdev, vq->last_used_idx)); |
a5c262c5 | 862 | |
4d6a105e | 863 | LAST_ADD_TIME_INVALID(vq); |
e93300b1 | 864 | |
0a8a69dd RR |
865 | END_USE(vq); |
866 | return ret; | |
867 | } | |
138fd251 | 868 | |
138fd251 | 869 | static void virtqueue_disable_cb_split(struct virtqueue *_vq) |
18445c4d RR |
870 | { |
871 | struct vring_virtqueue *vq = to_vvq(_vq); | |
872 | ||
e593bf97 TB |
873 | if (!(vq->split.avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT)) { |
874 | vq->split.avail_flags_shadow |= VRING_AVAIL_F_NO_INTERRUPT; | |
6c0b057c AH |
875 | |
876 | /* | |
877 | * If device triggered an event already it won't trigger one again: | |
878 | * no need to disable. | |
879 | */ | |
880 | if (vq->event_triggered) | |
881 | return; | |
882 | ||
8d622d21 MT |
883 | if (vq->event) |
884 | /* TODO: this is a hack. Figure out a cleaner value to write. */ | |
885 | vring_used_event(&vq->split.vring) = 0x0; | |
886 | else | |
e593bf97 TB |
887 | vq->split.vring.avail->flags = |
888 | cpu_to_virtio16(_vq->vdev, | |
889 | vq->split.avail_flags_shadow); | |
f277ec42 | 890 | } |
18445c4d RR |
891 | } |
892 | ||
31532340 | 893 | static unsigned int virtqueue_enable_cb_prepare_split(struct virtqueue *_vq) |
0a8a69dd RR |
894 | { |
895 | struct vring_virtqueue *vq = to_vvq(_vq); | |
cc229884 | 896 | u16 last_used_idx; |
0a8a69dd RR |
897 | |
898 | START_USE(vq); | |
0a8a69dd RR |
899 | |
900 | /* We optimistically turn back on interrupts, then check if there was | |
901 | * more to do. */ | |
a5c262c5 MT |
902 | /* Depending on the VIRTIO_RING_F_EVENT_IDX feature, we need to |
903 | * either clear the flags bit or point the event index at the next | |
904 | * entry. Always do both to keep code simple. */ | |
e593bf97 TB |
905 | if (vq->split.avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT) { |
906 | vq->split.avail_flags_shadow &= ~VRING_AVAIL_F_NO_INTERRUPT; | |
0ea1e4a6 | 907 | if (!vq->event) |
e593bf97 TB |
908 | vq->split.vring.avail->flags = |
909 | cpu_to_virtio16(_vq->vdev, | |
910 | vq->split.avail_flags_shadow); | |
f277ec42 | 911 | } |
e593bf97 TB |
912 | vring_used_event(&vq->split.vring) = cpu_to_virtio16(_vq->vdev, |
913 | last_used_idx = vq->last_used_idx); | |
cc229884 MT |
914 | END_USE(vq); |
915 | return last_used_idx; | |
916 | } | |
138fd251 | 917 | |
31532340 | 918 | static bool virtqueue_poll_split(struct virtqueue *_vq, unsigned int last_used_idx) |
138fd251 TB |
919 | { |
920 | struct vring_virtqueue *vq = to_vvq(_vq); | |
921 | ||
922 | return (u16)last_used_idx != virtio16_to_cpu(_vq->vdev, | |
e593bf97 | 923 | vq->split.vring.used->idx); |
138fd251 TB |
924 | } |
925 | ||
138fd251 | 926 | static bool virtqueue_enable_cb_delayed_split(struct virtqueue *_vq) |
7ab358c2 MT |
927 | { |
928 | struct vring_virtqueue *vq = to_vvq(_vq); | |
929 | u16 bufs; | |
930 | ||
931 | START_USE(vq); | |
932 | ||
933 | /* We optimistically turn back on interrupts, then check if there was | |
934 | * more to do. */ | |
935 | /* Depending on the VIRTIO_RING_F_USED_EVENT_IDX feature, we need to | |
936 | * either clear the flags bit or point the event index at the next | |
0ea1e4a6 | 937 | * entry. Always update the event index to keep code simple. */ |
e593bf97 TB |
938 | if (vq->split.avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT) { |
939 | vq->split.avail_flags_shadow &= ~VRING_AVAIL_F_NO_INTERRUPT; | |
0ea1e4a6 | 940 | if (!vq->event) |
e593bf97 TB |
941 | vq->split.vring.avail->flags = |
942 | cpu_to_virtio16(_vq->vdev, | |
943 | vq->split.avail_flags_shadow); | |
f277ec42 | 944 | } |
7ab358c2 | 945 | /* TODO: tune this threshold */ |
e593bf97 | 946 | bufs = (u16)(vq->split.avail_idx_shadow - vq->last_used_idx) * 3 / 4; |
788e5b3a MT |
947 | |
948 | virtio_store_mb(vq->weak_barriers, | |
e593bf97 | 949 | &vring_used_event(&vq->split.vring), |
788e5b3a MT |
950 | cpu_to_virtio16(_vq->vdev, vq->last_used_idx + bufs)); |
951 | ||
e593bf97 TB |
952 | if (unlikely((u16)(virtio16_to_cpu(_vq->vdev, vq->split.vring.used->idx) |
953 | - vq->last_used_idx) > bufs)) { | |
7ab358c2 MT |
954 | END_USE(vq); |
955 | return false; | |
956 | } | |
957 | ||
958 | END_USE(vq); | |
959 | return true; | |
960 | } | |
7ab358c2 | 961 | |
138fd251 | 962 | static void *virtqueue_detach_unused_buf_split(struct virtqueue *_vq) |
c021eac4 SM |
963 | { |
964 | struct vring_virtqueue *vq = to_vvq(_vq); | |
965 | unsigned int i; | |
966 | void *buf; | |
967 | ||
968 | START_USE(vq); | |
969 | ||
e593bf97 | 970 | for (i = 0; i < vq->split.vring.num; i++) { |
cbeedb72 | 971 | if (!vq->split.desc_state[i].data) |
c021eac4 | 972 | continue; |
138fd251 | 973 | /* detach_buf_split clears data, so grab it now. */ |
cbeedb72 | 974 | buf = vq->split.desc_state[i].data; |
138fd251 | 975 | detach_buf_split(vq, i, NULL); |
e593bf97 TB |
976 | vq->split.avail_idx_shadow--; |
977 | vq->split.vring.avail->idx = cpu_to_virtio16(_vq->vdev, | |
978 | vq->split.avail_idx_shadow); | |
c021eac4 SM |
979 | END_USE(vq); |
980 | return buf; | |
981 | } | |
982 | /* That should have freed everything. */ | |
e593bf97 | 983 | BUG_ON(vq->vq.num_free != vq->split.vring.num); |
c021eac4 SM |
984 | |
985 | END_USE(vq); | |
986 | return NULL; | |
987 | } | |
138fd251 | 988 | |
198fa7be XZ |
989 | static void virtqueue_vring_init_split(struct vring_virtqueue_split *vring_split, |
990 | struct vring_virtqueue *vq) | |
991 | { | |
992 | struct virtio_device *vdev; | |
993 | ||
994 | vdev = vq->vq.vdev; | |
995 | ||
996 | vring_split->avail_flags_shadow = 0; | |
997 | vring_split->avail_idx_shadow = 0; | |
998 | ||
999 | /* No callback? Tell other side not to bother us. */ | |
1000 | if (!vq->vq.callback) { | |
1001 | vring_split->avail_flags_shadow |= VRING_AVAIL_F_NO_INTERRUPT; | |
1002 | if (!vq->event) | |
1003 | vring_split->vring.avail->flags = cpu_to_virtio16(vdev, | |
1004 | vring_split->avail_flags_shadow); | |
1005 | } | |
1006 | } | |
1007 | ||
e5175b41 XZ |
1008 | static void virtqueue_reinit_split(struct vring_virtqueue *vq) |
1009 | { | |
1010 | int num; | |
1011 | ||
1012 | num = vq->split.vring.num; | |
1013 | ||
1014 | vq->split.vring.avail->flags = 0; | |
1015 | vq->split.vring.avail->idx = 0; | |
1016 | ||
1017 | /* reset avail event */ | |
1018 | vq->split.vring.avail->ring[num] = 0; | |
1019 | ||
1020 | vq->split.vring.used->flags = 0; | |
1021 | vq->split.vring.used->idx = 0; | |
1022 | ||
1023 | /* reset used event */ | |
1024 | *(__virtio16 *)&(vq->split.vring.used->ring[num]) = 0; | |
1025 | ||
1026 | virtqueue_init(vq, num); | |
1027 | ||
1028 | virtqueue_vring_init_split(&vq->split, vq); | |
1029 | } | |
1030 | ||
e1d6a423 XZ |
1031 | static void virtqueue_vring_attach_split(struct vring_virtqueue *vq, |
1032 | struct vring_virtqueue_split *vring_split) | |
1033 | { | |
1034 | vq->split = *vring_split; | |
1035 | ||
1036 | /* Put everything in free lists. */ | |
1037 | vq->free_head = 0; | |
1038 | } | |
1039 | ||
a2b36c8d XZ |
1040 | static int vring_alloc_state_extra_split(struct vring_virtqueue_split *vring_split) |
1041 | { | |
1042 | struct vring_desc_state_split *state; | |
1043 | struct vring_desc_extra *extra; | |
1044 | u32 num = vring_split->vring.num; | |
1045 | ||
1046 | state = kmalloc_array(num, sizeof(struct vring_desc_state_split), GFP_KERNEL); | |
1047 | if (!state) | |
1048 | goto err_state; | |
1049 | ||
1050 | extra = vring_alloc_desc_extra(num); | |
1051 | if (!extra) | |
1052 | goto err_extra; | |
1053 | ||
1054 | memset(state, 0, num * sizeof(struct vring_desc_state_split)); | |
1055 | ||
1056 | vring_split->desc_state = state; | |
1057 | vring_split->desc_extra = extra; | |
1058 | return 0; | |
1059 | ||
1060 | err_extra: | |
1061 | kfree(state); | |
1062 | err_state: | |
1063 | return -ENOMEM; | |
1064 | } | |
1065 | ||
89f05d94 | 1066 | static void vring_free_split(struct vring_virtqueue_split *vring_split, |
2713ea3c | 1067 | struct virtio_device *vdev, struct device *dma_dev) |
89f05d94 XZ |
1068 | { |
1069 | vring_free_queue(vdev, vring_split->queue_size_in_bytes, | |
1070 | vring_split->vring.desc, | |
2713ea3c JW |
1071 | vring_split->queue_dma_addr, |
1072 | dma_dev); | |
89f05d94 XZ |
1073 | |
1074 | kfree(vring_split->desc_state); | |
1075 | kfree(vring_split->desc_extra); | |
1076 | } | |
1077 | ||
c2d87fe6 XZ |
1078 | static int vring_alloc_queue_split(struct vring_virtqueue_split *vring_split, |
1079 | struct virtio_device *vdev, | |
1080 | u32 num, | |
1081 | unsigned int vring_align, | |
2713ea3c JW |
1082 | bool may_reduce_num, |
1083 | struct device *dma_dev) | |
d79dca75 | 1084 | { |
d79dca75 TB |
1085 | void *queue = NULL; |
1086 | dma_addr_t dma_addr; | |
d79dca75 TB |
1087 | |
1088 | /* We assume num is a power of 2. */ | |
b9d978a8 | 1089 | if (!is_power_of_2(num)) { |
d79dca75 | 1090 | dev_warn(&vdev->dev, "Bad virtqueue length %u\n", num); |
c2d87fe6 | 1091 | return -EINVAL; |
d79dca75 TB |
1092 | } |
1093 | ||
1094 | /* TODO: allocate each queue chunk individually */ | |
1095 | for (; num && vring_size(num, vring_align) > PAGE_SIZE; num /= 2) { | |
1096 | queue = vring_alloc_queue(vdev, vring_size(num, vring_align), | |
1097 | &dma_addr, | |
2713ea3c JW |
1098 | GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO, |
1099 | dma_dev); | |
d79dca75 TB |
1100 | if (queue) |
1101 | break; | |
cf94db21 | 1102 | if (!may_reduce_num) |
c2d87fe6 | 1103 | return -ENOMEM; |
d79dca75 TB |
1104 | } |
1105 | ||
1106 | if (!num) | |
c2d87fe6 | 1107 | return -ENOMEM; |
d79dca75 TB |
1108 | |
1109 | if (!queue) { | |
1110 | /* Try to get a single page. You are my only hope! */ | |
1111 | queue = vring_alloc_queue(vdev, vring_size(num, vring_align), | |
2713ea3c JW |
1112 | &dma_addr, GFP_KERNEL | __GFP_ZERO, |
1113 | dma_dev); | |
d79dca75 TB |
1114 | } |
1115 | if (!queue) | |
c2d87fe6 XZ |
1116 | return -ENOMEM; |
1117 | ||
1118 | vring_init(&vring_split->vring, num, queue, vring_align); | |
1119 | ||
1120 | vring_split->queue_dma_addr = dma_addr; | |
1121 | vring_split->queue_size_in_bytes = vring_size(num, vring_align); | |
d79dca75 | 1122 | |
af36b16f XZ |
1123 | vring_split->vring_align = vring_align; |
1124 | vring_split->may_reduce_num = may_reduce_num; | |
1125 | ||
c2d87fe6 XZ |
1126 | return 0; |
1127 | } | |
1128 | ||
a49c26f7 WH |
1129 | static struct virtqueue *__vring_new_virtqueue_split(unsigned int index, |
1130 | struct vring_virtqueue_split *vring_split, | |
1131 | struct virtio_device *vdev, | |
1132 | bool weak_barriers, | |
1133 | bool context, | |
1134 | bool (*notify)(struct virtqueue *), | |
1135 | void (*callback)(struct virtqueue *), | |
1136 | const char *name, | |
1137 | struct device *dma_dev) | |
1138 | { | |
1139 | struct vring_virtqueue *vq; | |
1140 | int err; | |
1141 | ||
1142 | vq = kmalloc(sizeof(*vq), GFP_KERNEL); | |
1143 | if (!vq) | |
1144 | return NULL; | |
1145 | ||
1146 | vq->packed_ring = false; | |
1147 | vq->vq.callback = callback; | |
1148 | vq->vq.vdev = vdev; | |
1149 | vq->vq.name = name; | |
1150 | vq->vq.index = index; | |
1151 | vq->vq.reset = false; | |
1152 | vq->we_own_ring = false; | |
1153 | vq->notify = notify; | |
1154 | vq->weak_barriers = weak_barriers; | |
1155 | #ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION | |
1156 | vq->broken = true; | |
1157 | #else | |
1158 | vq->broken = false; | |
1159 | #endif | |
1160 | vq->dma_dev = dma_dev; | |
1161 | vq->use_dma_api = vring_use_dma_api(vdev); | |
a49c26f7 WH |
1162 | |
1163 | vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC) && | |
1164 | !context; | |
1165 | vq->event = virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX); | |
1166 | ||
1167 | if (virtio_has_feature(vdev, VIRTIO_F_ORDER_PLATFORM)) | |
1168 | vq->weak_barriers = false; | |
1169 | ||
1170 | err = vring_alloc_state_extra_split(vring_split); | |
1171 | if (err) { | |
1172 | kfree(vq); | |
1173 | return NULL; | |
1174 | } | |
1175 | ||
1176 | virtqueue_vring_init_split(vring_split, vq); | |
1177 | ||
1178 | virtqueue_init(vq, vring_split->vring.num); | |
1179 | virtqueue_vring_attach_split(vq, vring_split); | |
1180 | ||
1181 | spin_lock(&vdev->vqs_list_lock); | |
1182 | list_add_tail(&vq->vq.list, &vdev->vqs); | |
1183 | spin_unlock(&vdev->vqs_list_lock); | |
1184 | return &vq->vq; | |
1185 | } | |
1186 | ||
c2d87fe6 XZ |
1187 | static struct virtqueue *vring_create_virtqueue_split( |
1188 | unsigned int index, | |
1189 | unsigned int num, | |
1190 | unsigned int vring_align, | |
1191 | struct virtio_device *vdev, | |
1192 | bool weak_barriers, | |
1193 | bool may_reduce_num, | |
1194 | bool context, | |
1195 | bool (*notify)(struct virtqueue *), | |
1196 | void (*callback)(struct virtqueue *), | |
2713ea3c JW |
1197 | const char *name, |
1198 | struct device *dma_dev) | |
c2d87fe6 XZ |
1199 | { |
1200 | struct vring_virtqueue_split vring_split = {}; | |
1201 | struct virtqueue *vq; | |
1202 | int err; | |
1203 | ||
1204 | err = vring_alloc_queue_split(&vring_split, vdev, num, vring_align, | |
2713ea3c | 1205 | may_reduce_num, dma_dev); |
c2d87fe6 XZ |
1206 | if (err) |
1207 | return NULL; | |
d79dca75 | 1208 | |
a49c26f7 | 1209 | vq = __vring_new_virtqueue_split(index, &vring_split, vdev, weak_barriers, |
2713ea3c | 1210 | context, notify, callback, name, dma_dev); |
d79dca75 | 1211 | if (!vq) { |
2713ea3c | 1212 | vring_free_split(&vring_split, vdev, dma_dev); |
d79dca75 TB |
1213 | return NULL; |
1214 | } | |
1215 | ||
d79dca75 TB |
1216 | to_vvq(vq)->we_own_ring = true; |
1217 | ||
1218 | return vq; | |
1219 | } | |
1220 | ||
6fea20e5 XZ |
1221 | static int virtqueue_resize_split(struct virtqueue *_vq, u32 num) |
1222 | { | |
1223 | struct vring_virtqueue_split vring_split = {}; | |
1224 | struct vring_virtqueue *vq = to_vvq(_vq); | |
1225 | struct virtio_device *vdev = _vq->vdev; | |
1226 | int err; | |
1227 | ||
1228 | err = vring_alloc_queue_split(&vring_split, vdev, num, | |
1229 | vq->split.vring_align, | |
2713ea3c JW |
1230 | vq->split.may_reduce_num, |
1231 | vring_dma_dev(vq)); | |
6fea20e5 XZ |
1232 | if (err) |
1233 | goto err; | |
1234 | ||
1235 | err = vring_alloc_state_extra_split(&vring_split); | |
1236 | if (err) | |
1237 | goto err_state_extra; | |
1238 | ||
1239 | vring_free(&vq->vq); | |
1240 | ||
1241 | virtqueue_vring_init_split(&vring_split, vq); | |
1242 | ||
1243 | virtqueue_init(vq, vring_split.vring.num); | |
1244 | virtqueue_vring_attach_split(vq, &vring_split); | |
1245 | ||
1246 | return 0; | |
1247 | ||
1248 | err_state_extra: | |
2713ea3c | 1249 | vring_free_split(&vring_split, vdev, vring_dma_dev(vq)); |
6fea20e5 XZ |
1250 | err: |
1251 | virtqueue_reinit_split(vq); | |
1252 | return -ENOMEM; | |
1253 | } | |
1254 | ||
e6f633e5 | 1255 | |
1ce9e605 TB |
1256 | /* |
1257 | * Packed ring specific functions - *_packed(). | |
1258 | */ | |
1adbd6b2 | 1259 | static bool packed_used_wrap_counter(u16 last_used_idx) |
a7722890 | 1260 | { |
1261 | return !!(last_used_idx & (1 << VRING_PACKED_EVENT_F_WRAP_CTR)); | |
1262 | } | |
1263 | ||
1adbd6b2 | 1264 | static u16 packed_last_used(u16 last_used_idx) |
a7722890 | 1265 | { |
1266 | return last_used_idx & ~(-(1 << VRING_PACKED_EVENT_F_WRAP_CTR)); | |
1267 | } | |
1ce9e605 | 1268 | |
d80dc15b | 1269 | static void vring_unmap_extra_packed(const struct vring_virtqueue *vq, |
4b6ec919 | 1270 | const struct vring_desc_extra *extra) |
1ce9e605 TB |
1271 | { |
1272 | u16 flags; | |
1273 | ||
d80dc15b | 1274 | flags = extra->flags; |
1ce9e605 TB |
1275 | |
1276 | if (flags & VRING_DESC_F_INDIRECT) { | |
b319940f XZ |
1277 | if (!vq->use_dma_api) |
1278 | return; | |
1279 | ||
1ce9e605 | 1280 | dma_unmap_single(vring_dma_dev(vq), |
d80dc15b | 1281 | extra->addr, extra->len, |
1ce9e605 TB |
1282 | (flags & VRING_DESC_F_WRITE) ? |
1283 | DMA_FROM_DEVICE : DMA_TO_DEVICE); | |
1284 | } else { | |
c7e1b422 | 1285 | if (!vring_need_unmap_buffer(vq, extra)) |
b319940f XZ |
1286 | return; |
1287 | ||
1ce9e605 | 1288 | dma_unmap_page(vring_dma_dev(vq), |
d80dc15b | 1289 | extra->addr, extra->len, |
1ce9e605 TB |
1290 | (flags & VRING_DESC_F_WRITE) ? |
1291 | DMA_FROM_DEVICE : DMA_TO_DEVICE); | |
1292 | } | |
1293 | } | |
1294 | ||
1ce9e605 TB |
1295 | static struct vring_packed_desc *alloc_indirect_packed(unsigned int total_sg, |
1296 | gfp_t gfp) | |
1297 | { | |
aaa78984 | 1298 | struct vring_desc_extra *extra; |
1ce9e605 | 1299 | struct vring_packed_desc *desc; |
aaa78984 | 1300 | int i, size; |
1ce9e605 TB |
1301 | |
1302 | /* | |
1303 | * We require lowmem mappings for the descriptors because | |
1304 | * otherwise virt_to_phys will give us bogus addresses in the | |
1305 | * virtqueue. | |
1306 | */ | |
1307 | gfp &= ~__GFP_HIGHMEM; | |
1308 | ||
aaa78984 XZ |
1309 | size = (sizeof(*desc) + sizeof(*extra)) * total_sg; |
1310 | ||
1311 | desc = kmalloc(size, gfp); | |
1312 | if (!desc) | |
1313 | return NULL; | |
1314 | ||
1315 | extra = (struct vring_desc_extra *)&desc[total_sg]; | |
1316 | ||
1317 | for (i = 0; i < total_sg; i++) | |
1318 | extra[i].next = i + 1; | |
1ce9e605 TB |
1319 | |
1320 | return desc; | |
1321 | } | |
1322 | ||
1323 | static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq, | |
8d7670f3 XZ |
1324 | struct scatterlist *sgs[], |
1325 | unsigned int total_sg, | |
1326 | unsigned int out_sgs, | |
1327 | unsigned int in_sgs, | |
1328 | void *data, | |
c7e1b422 | 1329 | bool premapped, |
8d7670f3 | 1330 | gfp_t gfp) |
1ce9e605 | 1331 | { |
aaa78984 | 1332 | struct vring_desc_extra *extra; |
1ce9e605 TB |
1333 | struct vring_packed_desc *desc; |
1334 | struct scatterlist *sg; | |
c7e1b422 | 1335 | unsigned int i, n, err_idx, len; |
1ce9e605 TB |
1336 | u16 head, id; |
1337 | dma_addr_t addr; | |
1338 | ||
1339 | head = vq->packed.next_avail_idx; | |
1340 | desc = alloc_indirect_packed(total_sg, gfp); | |
fc6d70f4 XZ |
1341 | if (!desc) |
1342 | return -ENOMEM; | |
1ce9e605 | 1343 | |
aaa78984 XZ |
1344 | extra = (struct vring_desc_extra *)&desc[total_sg]; |
1345 | ||
1ce9e605 TB |
1346 | if (unlikely(vq->vq.num_free < 1)) { |
1347 | pr_debug("Can't add buf len 1 - avail = 0\n"); | |
df0bfe75 | 1348 | kfree(desc); |
1ce9e605 TB |
1349 | END_USE(vq); |
1350 | return -ENOSPC; | |
1351 | } | |
1352 | ||
1353 | i = 0; | |
1354 | id = vq->free_head; | |
1355 | BUG_ON(id == vq->packed.vring.num); | |
1356 | ||
1357 | for (n = 0; n < out_sgs + in_sgs; n++) { | |
1358 | for (sg = sgs[n]; sg; sg = sg_next(sg)) { | |
0e27fa6d | 1359 | if (vring_map_one_sg(vq, sg, n < out_sgs ? |
c7e1b422 XZ |
1360 | DMA_TO_DEVICE : DMA_FROM_DEVICE, |
1361 | &addr, &len, premapped)) | |
1ce9e605 TB |
1362 | goto unmap_release; |
1363 | ||
1364 | desc[i].flags = cpu_to_le16(n < out_sgs ? | |
1365 | 0 : VRING_DESC_F_WRITE); | |
1366 | desc[i].addr = cpu_to_le64(addr); | |
c7e1b422 | 1367 | desc[i].len = cpu_to_le32(len); |
aaa78984 XZ |
1368 | |
1369 | if (unlikely(vq->use_dma_api)) { | |
c7e1b422 XZ |
1370 | extra[i].addr = premapped ? DMA_MAPPING_ERROR : addr; |
1371 | extra[i].len = len; | |
aaa78984 XZ |
1372 | extra[i].flags = n < out_sgs ? 0 : VRING_DESC_F_WRITE; |
1373 | } | |
1374 | ||
1ce9e605 TB |
1375 | i++; |
1376 | } | |
1377 | } | |
1378 | ||
1379 | /* Now that the indirect table is filled in, map it. */ | |
1380 | addr = vring_map_single(vq, desc, | |
1381 | total_sg * sizeof(struct vring_packed_desc), | |
1382 | DMA_TO_DEVICE); | |
c7e1b422 | 1383 | if (vring_mapping_error(vq, addr)) |
1ce9e605 TB |
1384 | goto unmap_release; |
1385 | ||
1386 | vq->packed.vring.desc[head].addr = cpu_to_le64(addr); | |
1387 | vq->packed.vring.desc[head].len = cpu_to_le32(total_sg * | |
1388 | sizeof(struct vring_packed_desc)); | |
1389 | vq->packed.vring.desc[head].id = cpu_to_le16(id); | |
1390 | ||
d5c0ed17 | 1391 | if (vq->use_dma_api) { |
1ce9e605 TB |
1392 | vq->packed.desc_extra[id].addr = addr; |
1393 | vq->packed.desc_extra[id].len = total_sg * | |
1394 | sizeof(struct vring_packed_desc); | |
1395 | vq->packed.desc_extra[id].flags = VRING_DESC_F_INDIRECT | | |
1396 | vq->packed.avail_used_flags; | |
1397 | } | |
1398 | ||
1399 | /* | |
1400 | * A driver MUST NOT make the first descriptor in the list | |
1401 | * available before all subsequent descriptors comprising | |
1402 | * the list are made available. | |
1403 | */ | |
1404 | virtio_wmb(vq->weak_barriers); | |
1405 | vq->packed.vring.desc[head].flags = cpu_to_le16(VRING_DESC_F_INDIRECT | | |
1406 | vq->packed.avail_used_flags); | |
1407 | ||
1408 | /* We're using some buffers from the free list. */ | |
1409 | vq->vq.num_free -= 1; | |
1410 | ||
1411 | /* Update free pointer */ | |
1412 | n = head + 1; | |
1413 | if (n >= vq->packed.vring.num) { | |
1414 | n = 0; | |
1415 | vq->packed.avail_wrap_counter ^= 1; | |
1416 | vq->packed.avail_used_flags ^= | |
1417 | 1 << VRING_PACKED_DESC_F_AVAIL | | |
1418 | 1 << VRING_PACKED_DESC_F_USED; | |
1419 | } | |
1420 | vq->packed.next_avail_idx = n; | |
aeef9b47 | 1421 | vq->free_head = vq->packed.desc_extra[id].next; |
1ce9e605 TB |
1422 | |
1423 | /* Store token and indirect buffer state. */ | |
1424 | vq->packed.desc_state[id].num = 1; | |
1425 | vq->packed.desc_state[id].data = data; | |
1426 | vq->packed.desc_state[id].indir_desc = desc; | |
1427 | vq->packed.desc_state[id].last = id; | |
1428 | ||
1429 | vq->num_added += 1; | |
1430 | ||
1431 | pr_debug("Added buffer head %i to %p\n", head, vq); | |
1432 | END_USE(vq); | |
1433 | ||
1434 | return 0; | |
1435 | ||
1436 | unmap_release: | |
1437 | err_idx = i; | |
1438 | ||
1439 | for (i = 0; i < err_idx; i++) | |
aaa78984 | 1440 | vring_unmap_extra_packed(vq, &extra[i]); |
1ce9e605 TB |
1441 | |
1442 | kfree(desc); | |
1443 | ||
1444 | END_USE(vq); | |
f7728002 | 1445 | return -ENOMEM; |
1ce9e605 TB |
1446 | } |
1447 | ||
1448 | static inline int virtqueue_add_packed(struct virtqueue *_vq, | |
1449 | struct scatterlist *sgs[], | |
1450 | unsigned int total_sg, | |
1451 | unsigned int out_sgs, | |
1452 | unsigned int in_sgs, | |
1453 | void *data, | |
1454 | void *ctx, | |
c7e1b422 | 1455 | bool premapped, |
1ce9e605 TB |
1456 | gfp_t gfp) |
1457 | { | |
1458 | struct vring_virtqueue *vq = to_vvq(_vq); | |
1459 | struct vring_packed_desc *desc; | |
1460 | struct scatterlist *sg; | |
c7e1b422 | 1461 | unsigned int i, n, c, descs_used, err_idx, len; |
3f649ab7 KC |
1462 | __le16 head_flags, flags; |
1463 | u16 head, id, prev, curr, avail_used_flags; | |
fc6d70f4 | 1464 | int err; |
1ce9e605 TB |
1465 | |
1466 | START_USE(vq); | |
1467 | ||
1468 | BUG_ON(data == NULL); | |
1469 | BUG_ON(ctx && vq->indirect); | |
1470 | ||
1471 | if (unlikely(vq->broken)) { | |
1472 | END_USE(vq); | |
1473 | return -EIO; | |
1474 | } | |
1475 | ||
1476 | LAST_ADD_TIME_UPDATE(vq); | |
1477 | ||
1478 | BUG_ON(total_sg == 0); | |
1479 | ||
35c51e09 | 1480 | if (virtqueue_use_indirect(vq, total_sg)) { |
fc6d70f4 | 1481 | err = virtqueue_add_indirect_packed(vq, sgs, total_sg, out_sgs, |
c7e1b422 | 1482 | in_sgs, data, premapped, gfp); |
1861ba62 MT |
1483 | if (err != -ENOMEM) { |
1484 | END_USE(vq); | |
fc6d70f4 | 1485 | return err; |
1861ba62 | 1486 | } |
fc6d70f4 XZ |
1487 | |
1488 | /* fall back on direct */ | |
1489 | } | |
1ce9e605 TB |
1490 | |
1491 | head = vq->packed.next_avail_idx; | |
1492 | avail_used_flags = vq->packed.avail_used_flags; | |
1493 | ||
1494 | WARN_ON_ONCE(total_sg > vq->packed.vring.num && !vq->indirect); | |
1495 | ||
1496 | desc = vq->packed.vring.desc; | |
1497 | i = head; | |
1498 | descs_used = total_sg; | |
1499 | ||
1500 | if (unlikely(vq->vq.num_free < descs_used)) { | |
1501 | pr_debug("Can't add buf len %i - avail = %i\n", | |
1502 | descs_used, vq->vq.num_free); | |
1503 | END_USE(vq); | |
1504 | return -ENOSPC; | |
1505 | } | |
1506 | ||
1507 | id = vq->free_head; | |
1508 | BUG_ON(id == vq->packed.vring.num); | |
1509 | ||
1510 | curr = id; | |
1511 | c = 0; | |
1512 | for (n = 0; n < out_sgs + in_sgs; n++) { | |
1513 | for (sg = sgs[n]; sg; sg = sg_next(sg)) { | |
0e27fa6d XZ |
1514 | dma_addr_t addr; |
1515 | ||
1516 | if (vring_map_one_sg(vq, sg, n < out_sgs ? | |
c7e1b422 XZ |
1517 | DMA_TO_DEVICE : DMA_FROM_DEVICE, |
1518 | &addr, &len, premapped)) | |
1ce9e605 TB |
1519 | goto unmap_release; |
1520 | ||
1521 | flags = cpu_to_le16(vq->packed.avail_used_flags | | |
1522 | (++c == total_sg ? 0 : VRING_DESC_F_NEXT) | | |
1523 | (n < out_sgs ? 0 : VRING_DESC_F_WRITE)); | |
1524 | if (i == head) | |
1525 | head_flags = flags; | |
1526 | else | |
1527 | desc[i].flags = flags; | |
1528 | ||
1529 | desc[i].addr = cpu_to_le64(addr); | |
c7e1b422 | 1530 | desc[i].len = cpu_to_le32(len); |
1ce9e605 TB |
1531 | desc[i].id = cpu_to_le16(id); |
1532 | ||
d5c0ed17 | 1533 | if (unlikely(vq->use_dma_api)) { |
c7e1b422 XZ |
1534 | vq->packed.desc_extra[curr].addr = premapped ? |
1535 | DMA_MAPPING_ERROR : addr; | |
1536 | vq->packed.desc_extra[curr].len = len; | |
1ce9e605 TB |
1537 | vq->packed.desc_extra[curr].flags = |
1538 | le16_to_cpu(flags); | |
1539 | } | |
1540 | prev = curr; | |
aeef9b47 | 1541 | curr = vq->packed.desc_extra[curr].next; |
1ce9e605 TB |
1542 | |
1543 | if ((unlikely(++i >= vq->packed.vring.num))) { | |
1544 | i = 0; | |
1545 | vq->packed.avail_used_flags ^= | |
1546 | 1 << VRING_PACKED_DESC_F_AVAIL | | |
1547 | 1 << VRING_PACKED_DESC_F_USED; | |
1548 | } | |
1549 | } | |
1550 | } | |
1551 | ||
1acfe2c1 | 1552 | if (i <= head) |
1ce9e605 TB |
1553 | vq->packed.avail_wrap_counter ^= 1; |
1554 | ||
1555 | /* We're using some buffers from the free list. */ | |
1556 | vq->vq.num_free -= descs_used; | |
1557 | ||
1558 | /* Update free pointer */ | |
1559 | vq->packed.next_avail_idx = i; | |
1560 | vq->free_head = curr; | |
1561 | ||
1562 | /* Store token. */ | |
1563 | vq->packed.desc_state[id].num = descs_used; | |
1564 | vq->packed.desc_state[id].data = data; | |
1565 | vq->packed.desc_state[id].indir_desc = ctx; | |
1566 | vq->packed.desc_state[id].last = prev; | |
1567 | ||
1568 | /* | |
1569 | * A driver MUST NOT make the first descriptor in the list | |
1570 | * available before all subsequent descriptors comprising | |
1571 | * the list are made available. | |
1572 | */ | |
1573 | virtio_wmb(vq->weak_barriers); | |
1574 | vq->packed.vring.desc[head].flags = head_flags; | |
1575 | vq->num_added += descs_used; | |
1576 | ||
1577 | pr_debug("Added buffer head %i to %p\n", head, vq); | |
1578 | END_USE(vq); | |
1579 | ||
1580 | return 0; | |
1581 | ||
1582 | unmap_release: | |
1583 | err_idx = i; | |
1584 | i = head; | |
44593865 | 1585 | curr = vq->free_head; |
1ce9e605 TB |
1586 | |
1587 | vq->packed.avail_used_flags = avail_used_flags; | |
1588 | ||
1589 | for (n = 0; n < total_sg; n++) { | |
1590 | if (i == err_idx) | |
1591 | break; | |
d80dc15b | 1592 | vring_unmap_extra_packed(vq, &vq->packed.desc_extra[curr]); |
44593865 | 1593 | curr = vq->packed.desc_extra[curr].next; |
1ce9e605 TB |
1594 | i++; |
1595 | if (i >= vq->packed.vring.num) | |
1596 | i = 0; | |
1597 | } | |
1598 | ||
1599 | END_USE(vq); | |
1600 | return -EIO; | |
1601 | } | |
1602 | ||
1603 | static bool virtqueue_kick_prepare_packed(struct virtqueue *_vq) | |
1604 | { | |
1605 | struct vring_virtqueue *vq = to_vvq(_vq); | |
f51f9826 | 1606 | u16 new, old, off_wrap, flags, wrap_counter, event_idx; |
1ce9e605 TB |
1607 | bool needs_kick; |
1608 | union { | |
1609 | struct { | |
1610 | __le16 off_wrap; | |
1611 | __le16 flags; | |
1612 | }; | |
1613 | u32 u32; | |
1614 | } snapshot; | |
1615 | ||
1616 | START_USE(vq); | |
1617 | ||
1618 | /* | |
1619 | * We need to expose the new flags value before checking notification | |
1620 | * suppressions. | |
1621 | */ | |
1622 | virtio_mb(vq->weak_barriers); | |
1623 | ||
f51f9826 TB |
1624 | old = vq->packed.next_avail_idx - vq->num_added; |
1625 | new = vq->packed.next_avail_idx; | |
1ce9e605 TB |
1626 | vq->num_added = 0; |
1627 | ||
1628 | snapshot.u32 = *(u32 *)vq->packed.vring.device; | |
1629 | flags = le16_to_cpu(snapshot.flags); | |
1630 | ||
1631 | LAST_ADD_TIME_CHECK(vq); | |
1632 | LAST_ADD_TIME_INVALID(vq); | |
1633 | ||
f51f9826 TB |
1634 | if (flags != VRING_PACKED_EVENT_FLAG_DESC) { |
1635 | needs_kick = (flags != VRING_PACKED_EVENT_FLAG_DISABLE); | |
1636 | goto out; | |
1637 | } | |
1638 | ||
1639 | off_wrap = le16_to_cpu(snapshot.off_wrap); | |
1640 | ||
1641 | wrap_counter = off_wrap >> VRING_PACKED_EVENT_F_WRAP_CTR; | |
1642 | event_idx = off_wrap & ~(1 << VRING_PACKED_EVENT_F_WRAP_CTR); | |
1643 | if (wrap_counter != vq->packed.avail_wrap_counter) | |
1644 | event_idx -= vq->packed.vring.num; | |
1645 | ||
1646 | needs_kick = vring_need_event(event_idx, new, old); | |
1647 | out: | |
1ce9e605 TB |
1648 | END_USE(vq); |
1649 | return needs_kick; | |
1650 | } | |
1651 | ||
1652 | static void detach_buf_packed(struct vring_virtqueue *vq, | |
1653 | unsigned int id, void **ctx) | |
1654 | { | |
1655 | struct vring_desc_state_packed *state = NULL; | |
1656 | struct vring_packed_desc *desc; | |
1657 | unsigned int i, curr; | |
1658 | ||
1659 | state = &vq->packed.desc_state[id]; | |
1660 | ||
1661 | /* Clear data ptr. */ | |
1662 | state->data = NULL; | |
1663 | ||
aeef9b47 | 1664 | vq->packed.desc_extra[state->last].next = vq->free_head; |
1ce9e605 TB |
1665 | vq->free_head = id; |
1666 | vq->vq.num_free += state->num; | |
1667 | ||
d5c0ed17 | 1668 | if (unlikely(vq->use_dma_api)) { |
1ce9e605 TB |
1669 | curr = id; |
1670 | for (i = 0; i < state->num; i++) { | |
d80dc15b XZ |
1671 | vring_unmap_extra_packed(vq, |
1672 | &vq->packed.desc_extra[curr]); | |
aeef9b47 | 1673 | curr = vq->packed.desc_extra[curr].next; |
1ce9e605 TB |
1674 | } |
1675 | } | |
1676 | ||
1677 | if (vq->indirect) { | |
aaa78984 XZ |
1678 | struct vring_desc_extra *extra; |
1679 | u32 len, num; | |
1ce9e605 TB |
1680 | |
1681 | /* Free the indirect table, if any, now that it's unmapped. */ | |
1682 | desc = state->indir_desc; | |
1683 | if (!desc) | |
1684 | return; | |
1685 | ||
c7e1b422 | 1686 | if (vq->use_dma_api) { |
1ce9e605 | 1687 | len = vq->packed.desc_extra[id].len; |
aaa78984 XZ |
1688 | num = len / sizeof(struct vring_packed_desc); |
1689 | ||
1690 | extra = (struct vring_desc_extra *)&desc[num]; | |
1691 | ||
1692 | for (i = 0; i < num; i++) | |
1693 | vring_unmap_extra_packed(vq, &extra[i]); | |
1ce9e605 TB |
1694 | } |
1695 | kfree(desc); | |
1696 | state->indir_desc = NULL; | |
1697 | } else if (ctx) { | |
1698 | *ctx = state->indir_desc; | |
1699 | } | |
1700 | } | |
1701 | ||
1702 | static inline bool is_used_desc_packed(const struct vring_virtqueue *vq, | |
1703 | u16 idx, bool used_wrap_counter) | |
1704 | { | |
1705 | bool avail, used; | |
1706 | u16 flags; | |
1707 | ||
1708 | flags = le16_to_cpu(vq->packed.vring.desc[idx].flags); | |
1709 | avail = !!(flags & (1 << VRING_PACKED_DESC_F_AVAIL)); | |
1710 | used = !!(flags & (1 << VRING_PACKED_DESC_F_USED)); | |
1711 | ||
1712 | return avail == used && used == used_wrap_counter; | |
1713 | } | |
1714 | ||
1adbd6b2 | 1715 | static bool more_used_packed(const struct vring_virtqueue *vq) |
1ce9e605 | 1716 | { |
a7722890 | 1717 | u16 last_used; |
1718 | u16 last_used_idx; | |
1719 | bool used_wrap_counter; | |
1720 | ||
1721 | last_used_idx = READ_ONCE(vq->last_used_idx); | |
1722 | last_used = packed_last_used(last_used_idx); | |
1723 | used_wrap_counter = packed_used_wrap_counter(last_used_idx); | |
1724 | return is_used_desc_packed(vq, last_used, used_wrap_counter); | |
1ce9e605 TB |
1725 | } |
1726 | ||
1727 | static void *virtqueue_get_buf_ctx_packed(struct virtqueue *_vq, | |
1728 | unsigned int *len, | |
1729 | void **ctx) | |
1730 | { | |
1731 | struct vring_virtqueue *vq = to_vvq(_vq); | |
a7722890 | 1732 | u16 last_used, id, last_used_idx; |
1733 | bool used_wrap_counter; | |
1ce9e605 TB |
1734 | void *ret; |
1735 | ||
1736 | START_USE(vq); | |
1737 | ||
1738 | if (unlikely(vq->broken)) { | |
1739 | END_USE(vq); | |
1740 | return NULL; | |
1741 | } | |
1742 | ||
1743 | if (!more_used_packed(vq)) { | |
1744 | pr_debug("No more buffers in queue\n"); | |
1745 | END_USE(vq); | |
1746 | return NULL; | |
1747 | } | |
1748 | ||
1749 | /* Only get used elements after they have been exposed by host. */ | |
1750 | virtio_rmb(vq->weak_barriers); | |
1751 | ||
a7722890 | 1752 | last_used_idx = READ_ONCE(vq->last_used_idx); |
1753 | used_wrap_counter = packed_used_wrap_counter(last_used_idx); | |
1754 | last_used = packed_last_used(last_used_idx); | |
1ce9e605 TB |
1755 | id = le16_to_cpu(vq->packed.vring.desc[last_used].id); |
1756 | *len = le32_to_cpu(vq->packed.vring.desc[last_used].len); | |
1757 | ||
1758 | if (unlikely(id >= vq->packed.vring.num)) { | |
1759 | BAD_RING(vq, "id %u out of range\n", id); | |
1760 | return NULL; | |
1761 | } | |
1762 | if (unlikely(!vq->packed.desc_state[id].data)) { | |
1763 | BAD_RING(vq, "id %u is not a head!\n", id); | |
1764 | return NULL; | |
1765 | } | |
1766 | ||
1767 | /* detach_buf_packed clears data, so grab it now. */ | |
1768 | ret = vq->packed.desc_state[id].data; | |
1769 | detach_buf_packed(vq, id, ctx); | |
1770 | ||
a7722890 | 1771 | last_used += vq->packed.desc_state[id].num; |
1772 | if (unlikely(last_used >= vq->packed.vring.num)) { | |
1773 | last_used -= vq->packed.vring.num; | |
1774 | used_wrap_counter ^= 1; | |
1ce9e605 TB |
1775 | } |
1776 | ||
a7722890 | 1777 | last_used = (last_used | (used_wrap_counter << VRING_PACKED_EVENT_F_WRAP_CTR)); |
1778 | WRITE_ONCE(vq->last_used_idx, last_used); | |
1779 | ||
f51f9826 TB |
1780 | /* |
1781 | * If we expect an interrupt for the next entry, tell host | |
1782 | * by writing event index and flush out the write before | |
1783 | * the read in the next get_buf call. | |
1784 | */ | |
1785 | if (vq->packed.event_flags_shadow == VRING_PACKED_EVENT_FLAG_DESC) | |
1786 | virtio_store_mb(vq->weak_barriers, | |
1787 | &vq->packed.vring.driver->off_wrap, | |
a7722890 | 1788 | cpu_to_le16(vq->last_used_idx)); |
f51f9826 | 1789 | |
1ce9e605 TB |
1790 | LAST_ADD_TIME_INVALID(vq); |
1791 | ||
1792 | END_USE(vq); | |
1793 | return ret; | |
1794 | } | |
1795 | ||
1796 | static void virtqueue_disable_cb_packed(struct virtqueue *_vq) | |
1797 | { | |
1798 | struct vring_virtqueue *vq = to_vvq(_vq); | |
1799 | ||
1800 | if (vq->packed.event_flags_shadow != VRING_PACKED_EVENT_FLAG_DISABLE) { | |
1801 | vq->packed.event_flags_shadow = VRING_PACKED_EVENT_FLAG_DISABLE; | |
6c0b057c AH |
1802 | |
1803 | /* | |
1804 | * If device triggered an event already it won't trigger one again: | |
1805 | * no need to disable. | |
1806 | */ | |
1807 | if (vq->event_triggered) | |
1808 | return; | |
1809 | ||
1ce9e605 TB |
1810 | vq->packed.vring.driver->flags = |
1811 | cpu_to_le16(vq->packed.event_flags_shadow); | |
1812 | } | |
1813 | } | |
1814 | ||
31532340 | 1815 | static unsigned int virtqueue_enable_cb_prepare_packed(struct virtqueue *_vq) |
1ce9e605 TB |
1816 | { |
1817 | struct vring_virtqueue *vq = to_vvq(_vq); | |
1818 | ||
1819 | START_USE(vq); | |
1820 | ||
1821 | /* | |
1822 | * We optimistically turn back on interrupts, then check if there was | |
1823 | * more to do. | |
1824 | */ | |
1825 | ||
f51f9826 TB |
1826 | if (vq->event) { |
1827 | vq->packed.vring.driver->off_wrap = | |
a7722890 | 1828 | cpu_to_le16(vq->last_used_idx); |
f51f9826 TB |
1829 | /* |
1830 | * We need to update event offset and event wrap | |
1831 | * counter first before updating event flags. | |
1832 | */ | |
1833 | virtio_wmb(vq->weak_barriers); | |
1834 | } | |
1835 | ||
1ce9e605 | 1836 | if (vq->packed.event_flags_shadow == VRING_PACKED_EVENT_FLAG_DISABLE) { |
f51f9826 TB |
1837 | vq->packed.event_flags_shadow = vq->event ? |
1838 | VRING_PACKED_EVENT_FLAG_DESC : | |
1839 | VRING_PACKED_EVENT_FLAG_ENABLE; | |
1ce9e605 TB |
1840 | vq->packed.vring.driver->flags = |
1841 | cpu_to_le16(vq->packed.event_flags_shadow); | |
1842 | } | |
1843 | ||
1844 | END_USE(vq); | |
a7722890 | 1845 | return vq->last_used_idx; |
1ce9e605 TB |
1846 | } |
1847 | ||
1848 | static bool virtqueue_poll_packed(struct virtqueue *_vq, u16 off_wrap) | |
1849 | { | |
1850 | struct vring_virtqueue *vq = to_vvq(_vq); | |
1851 | bool wrap_counter; | |
1852 | u16 used_idx; | |
1853 | ||
1854 | wrap_counter = off_wrap >> VRING_PACKED_EVENT_F_WRAP_CTR; | |
1855 | used_idx = off_wrap & ~(1 << VRING_PACKED_EVENT_F_WRAP_CTR); | |
1856 | ||
1857 | return is_used_desc_packed(vq, used_idx, wrap_counter); | |
1858 | } | |
1859 | ||
1860 | static bool virtqueue_enable_cb_delayed_packed(struct virtqueue *_vq) | |
1861 | { | |
1862 | struct vring_virtqueue *vq = to_vvq(_vq); | |
a7722890 | 1863 | u16 used_idx, wrap_counter, last_used_idx; |
f51f9826 | 1864 | u16 bufs; |
1ce9e605 TB |
1865 | |
1866 | START_USE(vq); | |
1867 | ||
1868 | /* | |
1869 | * We optimistically turn back on interrupts, then check if there was | |
1870 | * more to do. | |
1871 | */ | |
1872 | ||
f51f9826 TB |
1873 | if (vq->event) { |
1874 | /* TODO: tune this threshold */ | |
1875 | bufs = (vq->packed.vring.num - vq->vq.num_free) * 3 / 4; | |
a7722890 | 1876 | last_used_idx = READ_ONCE(vq->last_used_idx); |
1877 | wrap_counter = packed_used_wrap_counter(last_used_idx); | |
f51f9826 | 1878 | |
a7722890 | 1879 | used_idx = packed_last_used(last_used_idx) + bufs; |
f51f9826 TB |
1880 | if (used_idx >= vq->packed.vring.num) { |
1881 | used_idx -= vq->packed.vring.num; | |
1882 | wrap_counter ^= 1; | |
1883 | } | |
1884 | ||
1885 | vq->packed.vring.driver->off_wrap = cpu_to_le16(used_idx | | |
1886 | (wrap_counter << VRING_PACKED_EVENT_F_WRAP_CTR)); | |
1887 | ||
1888 | /* | |
1889 | * We need to update event offset and event wrap | |
1890 | * counter first before updating event flags. | |
1891 | */ | |
1892 | virtio_wmb(vq->weak_barriers); | |
f51f9826 | 1893 | } |
1ce9e605 TB |
1894 | |
1895 | if (vq->packed.event_flags_shadow == VRING_PACKED_EVENT_FLAG_DISABLE) { | |
f51f9826 TB |
1896 | vq->packed.event_flags_shadow = vq->event ? |
1897 | VRING_PACKED_EVENT_FLAG_DESC : | |
1898 | VRING_PACKED_EVENT_FLAG_ENABLE; | |
1ce9e605 TB |
1899 | vq->packed.vring.driver->flags = |
1900 | cpu_to_le16(vq->packed.event_flags_shadow); | |
1901 | } | |
1902 | ||
1903 | /* | |
1904 | * We need to update event suppression structure first | |
1905 | * before re-checking for more used buffers. | |
1906 | */ | |
1907 | virtio_mb(vq->weak_barriers); | |
1908 | ||
a7722890 | 1909 | last_used_idx = READ_ONCE(vq->last_used_idx); |
1910 | wrap_counter = packed_used_wrap_counter(last_used_idx); | |
1911 | used_idx = packed_last_used(last_used_idx); | |
1912 | if (is_used_desc_packed(vq, used_idx, wrap_counter)) { | |
1ce9e605 TB |
1913 | END_USE(vq); |
1914 | return false; | |
1915 | } | |
1916 | ||
1917 | END_USE(vq); | |
1918 | return true; | |
1919 | } | |
1920 | ||
1921 | static void *virtqueue_detach_unused_buf_packed(struct virtqueue *_vq) | |
1922 | { | |
1923 | struct vring_virtqueue *vq = to_vvq(_vq); | |
1924 | unsigned int i; | |
1925 | void *buf; | |
1926 | ||
1927 | START_USE(vq); | |
1928 | ||
1929 | for (i = 0; i < vq->packed.vring.num; i++) { | |
1930 | if (!vq->packed.desc_state[i].data) | |
1931 | continue; | |
1932 | /* detach_buf clears data, so grab it now. */ | |
1933 | buf = vq->packed.desc_state[i].data; | |
1934 | detach_buf_packed(vq, i, NULL); | |
1935 | END_USE(vq); | |
1936 | return buf; | |
1937 | } | |
1938 | /* That should have freed everything. */ | |
1939 | BUG_ON(vq->vq.num_free != vq->packed.vring.num); | |
1940 | ||
1941 | END_USE(vq); | |
1942 | return NULL; | |
1943 | } | |
1944 | ||
96ef18a2 | 1945 | static struct vring_desc_extra *vring_alloc_desc_extra(unsigned int num) |
5a222421 JW |
1946 | { |
1947 | struct vring_desc_extra *desc_extra; | |
1948 | unsigned int i; | |
1949 | ||
1950 | desc_extra = kmalloc_array(num, sizeof(struct vring_desc_extra), | |
1951 | GFP_KERNEL); | |
1952 | if (!desc_extra) | |
1953 | return NULL; | |
1954 | ||
1955 | memset(desc_extra, 0, num * sizeof(struct vring_desc_extra)); | |
1956 | ||
1957 | for (i = 0; i < num - 1; i++) | |
1958 | desc_extra[i].next = i + 1; | |
1959 | ||
1960 | return desc_extra; | |
1961 | } | |
1962 | ||
6356f8bb | 1963 | static void vring_free_packed(struct vring_virtqueue_packed *vring_packed, |
2713ea3c JW |
1964 | struct virtio_device *vdev, |
1965 | struct device *dma_dev) | |
6356f8bb XZ |
1966 | { |
1967 | if (vring_packed->vring.desc) | |
1968 | vring_free_queue(vdev, vring_packed->ring_size_in_bytes, | |
1969 | vring_packed->vring.desc, | |
2713ea3c JW |
1970 | vring_packed->ring_dma_addr, |
1971 | dma_dev); | |
6356f8bb XZ |
1972 | |
1973 | if (vring_packed->vring.driver) | |
1974 | vring_free_queue(vdev, vring_packed->event_size_in_bytes, | |
1975 | vring_packed->vring.driver, | |
2713ea3c JW |
1976 | vring_packed->driver_event_dma_addr, |
1977 | dma_dev); | |
6356f8bb XZ |
1978 | |
1979 | if (vring_packed->vring.device) | |
1980 | vring_free_queue(vdev, vring_packed->event_size_in_bytes, | |
1981 | vring_packed->vring.device, | |
2713ea3c JW |
1982 | vring_packed->device_event_dma_addr, |
1983 | dma_dev); | |
6356f8bb XZ |
1984 | |
1985 | kfree(vring_packed->desc_state); | |
1986 | kfree(vring_packed->desc_extra); | |
1987 | } | |
1988 | ||
6b60b9c0 XZ |
1989 | static int vring_alloc_queue_packed(struct vring_virtqueue_packed *vring_packed, |
1990 | struct virtio_device *vdev, | |
2713ea3c | 1991 | u32 num, struct device *dma_dev) |
1ce9e605 | 1992 | { |
1ce9e605 TB |
1993 | struct vring_packed_desc *ring; |
1994 | struct vring_packed_desc_event *driver, *device; | |
1995 | dma_addr_t ring_dma_addr, driver_event_dma_addr, device_event_dma_addr; | |
1996 | size_t ring_size_in_bytes, event_size_in_bytes; | |
1ce9e605 TB |
1997 | |
1998 | ring_size_in_bytes = num * sizeof(struct vring_packed_desc); | |
1999 | ||
2000 | ring = vring_alloc_queue(vdev, ring_size_in_bytes, | |
2001 | &ring_dma_addr, | |
2713ea3c JW |
2002 | GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO, |
2003 | dma_dev); | |
1ce9e605 | 2004 | if (!ring) |
6b60b9c0 XZ |
2005 | goto err; |
2006 | ||
2007 | vring_packed->vring.desc = ring; | |
2008 | vring_packed->ring_dma_addr = ring_dma_addr; | |
2009 | vring_packed->ring_size_in_bytes = ring_size_in_bytes; | |
1ce9e605 TB |
2010 | |
2011 | event_size_in_bytes = sizeof(struct vring_packed_desc_event); | |
2012 | ||
2013 | driver = vring_alloc_queue(vdev, event_size_in_bytes, | |
2014 | &driver_event_dma_addr, | |
2713ea3c JW |
2015 | GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO, |
2016 | dma_dev); | |
1ce9e605 | 2017 | if (!driver) |
6b60b9c0 XZ |
2018 | goto err; |
2019 | ||
2020 | vring_packed->vring.driver = driver; | |
2021 | vring_packed->event_size_in_bytes = event_size_in_bytes; | |
2022 | vring_packed->driver_event_dma_addr = driver_event_dma_addr; | |
1ce9e605 TB |
2023 | |
2024 | device = vring_alloc_queue(vdev, event_size_in_bytes, | |
2025 | &device_event_dma_addr, | |
2713ea3c JW |
2026 | GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO, |
2027 | dma_dev); | |
1ce9e605 | 2028 | if (!device) |
6b60b9c0 XZ |
2029 | goto err; |
2030 | ||
2031 | vring_packed->vring.device = device; | |
2032 | vring_packed->device_event_dma_addr = device_event_dma_addr; | |
2033 | ||
2034 | vring_packed->vring.num = num; | |
2035 | ||
2036 | return 0; | |
2037 | ||
2038 | err: | |
2713ea3c | 2039 | vring_free_packed(vring_packed, vdev, dma_dev); |
6b60b9c0 XZ |
2040 | return -ENOMEM; |
2041 | } | |
2042 | ||
ef3167cf XZ |
2043 | static int vring_alloc_state_extra_packed(struct vring_virtqueue_packed *vring_packed) |
2044 | { | |
2045 | struct vring_desc_state_packed *state; | |
2046 | struct vring_desc_extra *extra; | |
2047 | u32 num = vring_packed->vring.num; | |
2048 | ||
2049 | state = kmalloc_array(num, sizeof(struct vring_desc_state_packed), GFP_KERNEL); | |
2050 | if (!state) | |
2051 | goto err_desc_state; | |
2052 | ||
2053 | memset(state, 0, num * sizeof(struct vring_desc_state_packed)); | |
2054 | ||
2055 | extra = vring_alloc_desc_extra(num); | |
2056 | if (!extra) | |
2057 | goto err_desc_extra; | |
2058 | ||
2059 | vring_packed->desc_state = state; | |
2060 | vring_packed->desc_extra = extra; | |
2061 | ||
2062 | return 0; | |
2063 | ||
2064 | err_desc_extra: | |
2065 | kfree(state); | |
2066 | err_desc_state: | |
2067 | return -ENOMEM; | |
2068 | } | |
2069 | ||
1a107c87 XZ |
2070 | static void virtqueue_vring_init_packed(struct vring_virtqueue_packed *vring_packed, |
2071 | bool callback) | |
2072 | { | |
2073 | vring_packed->next_avail_idx = 0; | |
2074 | vring_packed->avail_wrap_counter = 1; | |
2075 | vring_packed->event_flags_shadow = 0; | |
2076 | vring_packed->avail_used_flags = 1 << VRING_PACKED_DESC_F_AVAIL; | |
2077 | ||
2078 | /* No callback? Tell other side not to bother us. */ | |
2079 | if (!callback) { | |
2080 | vring_packed->event_flags_shadow = VRING_PACKED_EVENT_FLAG_DISABLE; | |
2081 | vring_packed->vring.driver->flags = | |
2082 | cpu_to_le16(vring_packed->event_flags_shadow); | |
2083 | } | |
2084 | } | |
2085 | ||
51d649f1 XZ |
2086 | static void virtqueue_vring_attach_packed(struct vring_virtqueue *vq, |
2087 | struct vring_virtqueue_packed *vring_packed) | |
2088 | { | |
2089 | vq->packed = *vring_packed; | |
2090 | ||
2091 | /* Put everything in free lists. */ | |
2092 | vq->free_head = 0; | |
2093 | } | |
2094 | ||
56775e14 XZ |
2095 | static void virtqueue_reinit_packed(struct vring_virtqueue *vq) |
2096 | { | |
2097 | memset(vq->packed.vring.device, 0, vq->packed.event_size_in_bytes); | |
2098 | memset(vq->packed.vring.driver, 0, vq->packed.event_size_in_bytes); | |
2099 | ||
2100 | /* we need to reset the desc.flags. For more, see is_used_desc_packed() */ | |
2101 | memset(vq->packed.vring.desc, 0, vq->packed.ring_size_in_bytes); | |
2102 | ||
2103 | virtqueue_init(vq, vq->packed.vring.num); | |
2104 | virtqueue_vring_init_packed(&vq->packed, !!vq->vq.callback); | |
2105 | } | |
2106 | ||
a49c26f7 WH |
2107 | static struct virtqueue *__vring_new_virtqueue_packed(unsigned int index, |
2108 | struct vring_virtqueue_packed *vring_packed, | |
2109 | struct virtio_device *vdev, | |
2110 | bool weak_barriers, | |
2111 | bool context, | |
2112 | bool (*notify)(struct virtqueue *), | |
2113 | void (*callback)(struct virtqueue *), | |
2114 | const char *name, | |
2115 | struct device *dma_dev) | |
6b60b9c0 | 2116 | { |
6b60b9c0 | 2117 | struct vring_virtqueue *vq; |
ef3167cf | 2118 | int err; |
6b60b9c0 | 2119 | |
1ce9e605 TB |
2120 | vq = kmalloc(sizeof(*vq), GFP_KERNEL); |
2121 | if (!vq) | |
a49c26f7 | 2122 | return NULL; |
1ce9e605 TB |
2123 | |
2124 | vq->vq.callback = callback; | |
2125 | vq->vq.vdev = vdev; | |
2126 | vq->vq.name = name; | |
1ce9e605 | 2127 | vq->vq.index = index; |
4913e854 | 2128 | vq->vq.reset = false; |
a49c26f7 | 2129 | vq->we_own_ring = false; |
1ce9e605 TB |
2130 | vq->notify = notify; |
2131 | vq->weak_barriers = weak_barriers; | |
c346dae4 | 2132 | #ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION |
8b4ec69d | 2133 | vq->broken = true; |
c346dae4 JW |
2134 | #else |
2135 | vq->broken = false; | |
2136 | #endif | |
1ce9e605 | 2137 | vq->packed_ring = true; |
2713ea3c | 2138 | vq->dma_dev = dma_dev; |
1ce9e605 | 2139 | vq->use_dma_api = vring_use_dma_api(vdev); |
1ce9e605 TB |
2140 | |
2141 | vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC) && | |
2142 | !context; | |
2143 | vq->event = virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX); | |
2144 | ||
45383fb0 TB |
2145 | if (virtio_has_feature(vdev, VIRTIO_F_ORDER_PLATFORM)) |
2146 | vq->weak_barriers = false; | |
2147 | ||
a49c26f7 WH |
2148 | err = vring_alloc_state_extra_packed(vring_packed); |
2149 | if (err) { | |
2150 | kfree(vq); | |
2151 | return NULL; | |
2152 | } | |
1ce9e605 | 2153 | |
a49c26f7 | 2154 | virtqueue_vring_init_packed(vring_packed, !!callback); |
1ce9e605 | 2155 | |
a49c26f7 WH |
2156 | virtqueue_init(vq, vring_packed->vring.num); |
2157 | virtqueue_vring_attach_packed(vq, vring_packed); | |
3a897128 | 2158 | |
0e566c8f | 2159 | spin_lock(&vdev->vqs_list_lock); |
e152d8af | 2160 | list_add_tail(&vq->vq.list, &vdev->vqs); |
0e566c8f | 2161 | spin_unlock(&vdev->vqs_list_lock); |
1ce9e605 | 2162 | return &vq->vq; |
a49c26f7 | 2163 | } |
1ce9e605 | 2164 | |
a49c26f7 WH |
2165 | static struct virtqueue *vring_create_virtqueue_packed( |
2166 | unsigned int index, | |
2167 | unsigned int num, | |
2168 | unsigned int vring_align, | |
2169 | struct virtio_device *vdev, | |
2170 | bool weak_barriers, | |
2171 | bool may_reduce_num, | |
2172 | bool context, | |
2173 | bool (*notify)(struct virtqueue *), | |
2174 | void (*callback)(struct virtqueue *), | |
2175 | const char *name, | |
2176 | struct device *dma_dev) | |
2177 | { | |
2178 | struct vring_virtqueue_packed vring_packed = {}; | |
2179 | struct virtqueue *vq; | |
2180 | ||
2181 | if (vring_alloc_queue_packed(&vring_packed, vdev, num, dma_dev)) | |
2182 | return NULL; | |
2183 | ||
2184 | vq = __vring_new_virtqueue_packed(index, &vring_packed, vdev, weak_barriers, | |
2185 | context, notify, callback, name, dma_dev); | |
2186 | if (!vq) { | |
2187 | vring_free_packed(&vring_packed, vdev, dma_dev); | |
2188 | return NULL; | |
2189 | } | |
2190 | ||
2191 | to_vvq(vq)->we_own_ring = true; | |
2192 | ||
2193 | return vq; | |
1ce9e605 TB |
2194 | } |
2195 | ||
947f9fcf XZ |
2196 | static int virtqueue_resize_packed(struct virtqueue *_vq, u32 num) |
2197 | { | |
2198 | struct vring_virtqueue_packed vring_packed = {}; | |
2199 | struct vring_virtqueue *vq = to_vvq(_vq); | |
2200 | struct virtio_device *vdev = _vq->vdev; | |
2201 | int err; | |
2202 | ||
2713ea3c | 2203 | if (vring_alloc_queue_packed(&vring_packed, vdev, num, vring_dma_dev(vq))) |
947f9fcf XZ |
2204 | goto err_ring; |
2205 | ||
2206 | err = vring_alloc_state_extra_packed(&vring_packed); | |
2207 | if (err) | |
2208 | goto err_state_extra; | |
2209 | ||
2210 | vring_free(&vq->vq); | |
2211 | ||
2212 | virtqueue_vring_init_packed(&vring_packed, !!vq->vq.callback); | |
2213 | ||
2214 | virtqueue_init(vq, vring_packed.vring.num); | |
2215 | virtqueue_vring_attach_packed(vq, &vring_packed); | |
2216 | ||
2217 | return 0; | |
2218 | ||
2219 | err_state_extra: | |
2713ea3c | 2220 | vring_free_packed(&vring_packed, vdev, vring_dma_dev(vq)); |
947f9fcf XZ |
2221 | err_ring: |
2222 | virtqueue_reinit_packed(vq); | |
2223 | return -ENOMEM; | |
2224 | } | |
2225 | ||
ad48d53b XZ |
2226 | static int virtqueue_disable_and_recycle(struct virtqueue *_vq, |
2227 | void (*recycle)(struct virtqueue *vq, void *buf)) | |
2228 | { | |
2229 | struct vring_virtqueue *vq = to_vvq(_vq); | |
2230 | struct virtio_device *vdev = vq->vq.vdev; | |
2231 | void *buf; | |
2232 | int err; | |
2233 | ||
2234 | if (!vq->we_own_ring) | |
2235 | return -EPERM; | |
2236 | ||
2237 | if (!vdev->config->disable_vq_and_reset) | |
2238 | return -ENOENT; | |
2239 | ||
2240 | if (!vdev->config->enable_vq_after_reset) | |
2241 | return -ENOENT; | |
2242 | ||
2243 | err = vdev->config->disable_vq_and_reset(_vq); | |
2244 | if (err) | |
2245 | return err; | |
2246 | ||
2247 | while ((buf = virtqueue_detach_unused_buf(_vq)) != NULL) | |
2248 | recycle(_vq, buf); | |
2249 | ||
2250 | return 0; | |
2251 | } | |
2252 | ||
2253 | static int virtqueue_enable_after_reset(struct virtqueue *_vq) | |
2254 | { | |
2255 | struct vring_virtqueue *vq = to_vvq(_vq); | |
2256 | struct virtio_device *vdev = vq->vq.vdev; | |
2257 | ||
2258 | if (vdev->config->enable_vq_after_reset(_vq)) | |
2259 | return -EBUSY; | |
2260 | ||
2261 | return 0; | |
2262 | } | |
1ce9e605 | 2263 | |
e6f633e5 TB |
2264 | /* |
2265 | * Generic functions and exported symbols. | |
2266 | */ | |
2267 | ||
2268 | static inline int virtqueue_add(struct virtqueue *_vq, | |
2269 | struct scatterlist *sgs[], | |
2270 | unsigned int total_sg, | |
2271 | unsigned int out_sgs, | |
2272 | unsigned int in_sgs, | |
2273 | void *data, | |
2274 | void *ctx, | |
c7e1b422 | 2275 | bool premapped, |
e6f633e5 TB |
2276 | gfp_t gfp) |
2277 | { | |
1ce9e605 TB |
2278 | struct vring_virtqueue *vq = to_vvq(_vq); |
2279 | ||
2280 | return vq->packed_ring ? virtqueue_add_packed(_vq, sgs, total_sg, | |
c7e1b422 | 2281 | out_sgs, in_sgs, data, ctx, premapped, gfp) : |
1ce9e605 | 2282 | virtqueue_add_split(_vq, sgs, total_sg, |
c7e1b422 | 2283 | out_sgs, in_sgs, data, ctx, premapped, gfp); |
e6f633e5 TB |
2284 | } |
2285 | ||
2286 | /** | |
2287 | * virtqueue_add_sgs - expose buffers to other end | |
a5581206 | 2288 | * @_vq: the struct virtqueue we're talking about. |
e6f633e5 | 2289 | * @sgs: array of terminated scatterlists. |
a5581206 JB |
2290 | * @out_sgs: the number of scatterlists readable by other side |
2291 | * @in_sgs: the number of scatterlists which are writable (after readable ones) | |
e6f633e5 TB |
2292 | * @data: the token identifying the buffer. |
2293 | * @gfp: how to do memory allocations (if necessary). | |
2294 | * | |
2295 | * Caller must ensure we don't call this with other virtqueue operations | |
2296 | * at the same time (except where noted). | |
2297 | * | |
2298 | * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO). | |
2299 | */ | |
2300 | int virtqueue_add_sgs(struct virtqueue *_vq, | |
2301 | struct scatterlist *sgs[], | |
2302 | unsigned int out_sgs, | |
2303 | unsigned int in_sgs, | |
2304 | void *data, | |
2305 | gfp_t gfp) | |
2306 | { | |
2307 | unsigned int i, total_sg = 0; | |
2308 | ||
2309 | /* Count them first. */ | |
2310 | for (i = 0; i < out_sgs + in_sgs; i++) { | |
2311 | struct scatterlist *sg; | |
2312 | ||
2313 | for (sg = sgs[i]; sg; sg = sg_next(sg)) | |
2314 | total_sg++; | |
2315 | } | |
2316 | return virtqueue_add(_vq, sgs, total_sg, out_sgs, in_sgs, | |
c7e1b422 | 2317 | data, NULL, false, gfp); |
e6f633e5 TB |
2318 | } |
2319 | EXPORT_SYMBOL_GPL(virtqueue_add_sgs); | |
2320 | ||
2321 | /** | |
2322 | * virtqueue_add_outbuf - expose output buffers to other end | |
2323 | * @vq: the struct virtqueue we're talking about. | |
2324 | * @sg: scatterlist (must be well-formed and terminated!) | |
2325 | * @num: the number of entries in @sg readable by other side | |
2326 | * @data: the token identifying the buffer. | |
2327 | * @gfp: how to do memory allocations (if necessary). | |
2328 | * | |
2329 | * Caller must ensure we don't call this with other virtqueue operations | |
2330 | * at the same time (except where noted). | |
2331 | * | |
2332 | * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO). | |
2333 | */ | |
2334 | int virtqueue_add_outbuf(struct virtqueue *vq, | |
2335 | struct scatterlist *sg, unsigned int num, | |
2336 | void *data, | |
2337 | gfp_t gfp) | |
2338 | { | |
c7e1b422 | 2339 | return virtqueue_add(vq, &sg, num, 1, 0, data, NULL, false, gfp); |
e6f633e5 TB |
2340 | } |
2341 | EXPORT_SYMBOL_GPL(virtqueue_add_outbuf); | |
2342 | ||
3ef66af3 XZ |
2343 | /** |
2344 | * virtqueue_add_outbuf_premapped - expose output buffers to other end | |
2345 | * @vq: the struct virtqueue we're talking about. | |
2346 | * @sg: scatterlist (must be well-formed and terminated!) | |
2347 | * @num: the number of entries in @sg readable by other side | |
2348 | * @data: the token identifying the buffer. | |
2349 | * @gfp: how to do memory allocations (if necessary). | |
2350 | * | |
2351 | * Caller must ensure we don't call this with other virtqueue operations | |
2352 | * at the same time (except where noted). | |
2353 | * | |
2354 | * Return: | |
2355 | * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO). | |
2356 | */ | |
2357 | int virtqueue_add_outbuf_premapped(struct virtqueue *vq, | |
2358 | struct scatterlist *sg, unsigned int num, | |
2359 | void *data, | |
2360 | gfp_t gfp) | |
2361 | { | |
2362 | return virtqueue_add(vq, &sg, num, 1, 0, data, NULL, true, gfp); | |
2363 | } | |
2364 | EXPORT_SYMBOL_GPL(virtqueue_add_outbuf_premapped); | |
2365 | ||
e6f633e5 TB |
2366 | /** |
2367 | * virtqueue_add_inbuf - expose input buffers to other end | |
2368 | * @vq: the struct virtqueue we're talking about. | |
2369 | * @sg: scatterlist (must be well-formed and terminated!) | |
2370 | * @num: the number of entries in @sg writable by other side | |
2371 | * @data: the token identifying the buffer. | |
2372 | * @gfp: how to do memory allocations (if necessary). | |
2373 | * | |
2374 | * Caller must ensure we don't call this with other virtqueue operations | |
2375 | * at the same time (except where noted). | |
2376 | * | |
2377 | * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO). | |
2378 | */ | |
2379 | int virtqueue_add_inbuf(struct virtqueue *vq, | |
2380 | struct scatterlist *sg, unsigned int num, | |
2381 | void *data, | |
2382 | gfp_t gfp) | |
2383 | { | |
c7e1b422 | 2384 | return virtqueue_add(vq, &sg, num, 0, 1, data, NULL, false, gfp); |
e6f633e5 TB |
2385 | } |
2386 | EXPORT_SYMBOL_GPL(virtqueue_add_inbuf); | |
2387 | ||
2388 | /** | |
2389 | * virtqueue_add_inbuf_ctx - expose input buffers to other end | |
2390 | * @vq: the struct virtqueue we're talking about. | |
2391 | * @sg: scatterlist (must be well-formed and terminated!) | |
2392 | * @num: the number of entries in @sg writable by other side | |
2393 | * @data: the token identifying the buffer. | |
2394 | * @ctx: extra context for the token | |
2395 | * @gfp: how to do memory allocations (if necessary). | |
2396 | * | |
2397 | * Caller must ensure we don't call this with other virtqueue operations | |
2398 | * at the same time (except where noted). | |
2399 | * | |
2400 | * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO). | |
2401 | */ | |
2402 | int virtqueue_add_inbuf_ctx(struct virtqueue *vq, | |
2403 | struct scatterlist *sg, unsigned int num, | |
2404 | void *data, | |
2405 | void *ctx, | |
2406 | gfp_t gfp) | |
2407 | { | |
c7e1b422 | 2408 | return virtqueue_add(vq, &sg, num, 0, 1, data, ctx, false, gfp); |
e6f633e5 TB |
2409 | } |
2410 | EXPORT_SYMBOL_GPL(virtqueue_add_inbuf_ctx); | |
2411 | ||
3ef66af3 XZ |
2412 | /** |
2413 | * virtqueue_add_inbuf_premapped - expose input buffers to other end | |
2414 | * @vq: the struct virtqueue we're talking about. | |
2415 | * @sg: scatterlist (must be well-formed and terminated!) | |
2416 | * @num: the number of entries in @sg writable by other side | |
2417 | * @data: the token identifying the buffer. | |
2418 | * @ctx: extra context for the token | |
2419 | * @gfp: how to do memory allocations (if necessary). | |
2420 | * | |
2421 | * Caller must ensure we don't call this with other virtqueue operations | |
2422 | * at the same time (except where noted). | |
2423 | * | |
2424 | * Return: | |
2425 | * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO). | |
2426 | */ | |
2427 | int virtqueue_add_inbuf_premapped(struct virtqueue *vq, | |
2428 | struct scatterlist *sg, unsigned int num, | |
2429 | void *data, | |
2430 | void *ctx, | |
2431 | gfp_t gfp) | |
2432 | { | |
2433 | return virtqueue_add(vq, &sg, num, 0, 1, data, ctx, true, gfp); | |
2434 | } | |
2435 | EXPORT_SYMBOL_GPL(virtqueue_add_inbuf_premapped); | |
2436 | ||
2df64759 XZ |
2437 | /** |
2438 | * virtqueue_dma_dev - get the dma dev | |
2439 | * @_vq: the struct virtqueue we're talking about. | |
2440 | * | |
2441 | * Returns the dma dev. That can been used for dma api. | |
2442 | */ | |
2443 | struct device *virtqueue_dma_dev(struct virtqueue *_vq) | |
2444 | { | |
2445 | struct vring_virtqueue *vq = to_vvq(_vq); | |
2446 | ||
2447 | if (vq->use_dma_api) | |
2448 | return vring_dma_dev(vq); | |
2449 | else | |
2450 | return NULL; | |
2451 | } | |
2452 | EXPORT_SYMBOL_GPL(virtqueue_dma_dev); | |
2453 | ||
e6f633e5 TB |
2454 | /** |
2455 | * virtqueue_kick_prepare - first half of split virtqueue_kick call. | |
a5581206 | 2456 | * @_vq: the struct virtqueue |
e6f633e5 TB |
2457 | * |
2458 | * Instead of virtqueue_kick(), you can do: | |
2459 | * if (virtqueue_kick_prepare(vq)) | |
2460 | * virtqueue_notify(vq); | |
2461 | * | |
2462 | * This is sometimes useful because the virtqueue_kick_prepare() needs | |
2463 | * to be serialized, but the actual virtqueue_notify() call does not. | |
2464 | */ | |
2465 | bool virtqueue_kick_prepare(struct virtqueue *_vq) | |
2466 | { | |
1ce9e605 TB |
2467 | struct vring_virtqueue *vq = to_vvq(_vq); |
2468 | ||
2469 | return vq->packed_ring ? virtqueue_kick_prepare_packed(_vq) : | |
2470 | virtqueue_kick_prepare_split(_vq); | |
e6f633e5 TB |
2471 | } |
2472 | EXPORT_SYMBOL_GPL(virtqueue_kick_prepare); | |
2473 | ||
2474 | /** | |
2475 | * virtqueue_notify - second half of split virtqueue_kick call. | |
a5581206 | 2476 | * @_vq: the struct virtqueue |
e6f633e5 TB |
2477 | * |
2478 | * This does not need to be serialized. | |
2479 | * | |
2480 | * Returns false if host notify failed or queue is broken, otherwise true. | |
2481 | */ | |
2482 | bool virtqueue_notify(struct virtqueue *_vq) | |
2483 | { | |
2484 | struct vring_virtqueue *vq = to_vvq(_vq); | |
2485 | ||
2486 | if (unlikely(vq->broken)) | |
2487 | return false; | |
2488 | ||
2489 | /* Prod other side to tell it about changes. */ | |
2490 | if (!vq->notify(_vq)) { | |
2491 | vq->broken = true; | |
2492 | return false; | |
2493 | } | |
2494 | return true; | |
2495 | } | |
2496 | EXPORT_SYMBOL_GPL(virtqueue_notify); | |
2497 | ||
2498 | /** | |
2499 | * virtqueue_kick - update after add_buf | |
2500 | * @vq: the struct virtqueue | |
2501 | * | |
2502 | * After one or more virtqueue_add_* calls, invoke this to kick | |
2503 | * the other side. | |
2504 | * | |
2505 | * Caller must ensure we don't call this with other virtqueue | |
2506 | * operations at the same time (except where noted). | |
2507 | * | |
2508 | * Returns false if kick failed, otherwise true. | |
2509 | */ | |
2510 | bool virtqueue_kick(struct virtqueue *vq) | |
2511 | { | |
2512 | if (virtqueue_kick_prepare(vq)) | |
2513 | return virtqueue_notify(vq); | |
2514 | return true; | |
2515 | } | |
2516 | EXPORT_SYMBOL_GPL(virtqueue_kick); | |
2517 | ||
2518 | /** | |
31c11db6 | 2519 | * virtqueue_get_buf_ctx - get the next used buffer |
a5581206 | 2520 | * @_vq: the struct virtqueue we're talking about. |
e6f633e5 | 2521 | * @len: the length written into the buffer |
a5581206 | 2522 | * @ctx: extra context for the token |
e6f633e5 TB |
2523 | * |
2524 | * If the device wrote data into the buffer, @len will be set to the | |
2525 | * amount written. This means you don't need to clear the buffer | |
2526 | * beforehand to ensure there's no data leakage in the case of short | |
2527 | * writes. | |
2528 | * | |
2529 | * Caller must ensure we don't call this with other virtqueue | |
2530 | * operations at the same time (except where noted). | |
2531 | * | |
2532 | * Returns NULL if there are no used buffers, or the "data" token | |
2533 | * handed to virtqueue_add_*(). | |
2534 | */ | |
2535 | void *virtqueue_get_buf_ctx(struct virtqueue *_vq, unsigned int *len, | |
2536 | void **ctx) | |
2537 | { | |
1ce9e605 TB |
2538 | struct vring_virtqueue *vq = to_vvq(_vq); |
2539 | ||
2540 | return vq->packed_ring ? virtqueue_get_buf_ctx_packed(_vq, len, ctx) : | |
2541 | virtqueue_get_buf_ctx_split(_vq, len, ctx); | |
e6f633e5 TB |
2542 | } |
2543 | EXPORT_SYMBOL_GPL(virtqueue_get_buf_ctx); | |
2544 | ||
2545 | void *virtqueue_get_buf(struct virtqueue *_vq, unsigned int *len) | |
2546 | { | |
2547 | return virtqueue_get_buf_ctx(_vq, len, NULL); | |
2548 | } | |
2549 | EXPORT_SYMBOL_GPL(virtqueue_get_buf); | |
e6f633e5 TB |
2550 | /** |
2551 | * virtqueue_disable_cb - disable callbacks | |
a5581206 | 2552 | * @_vq: the struct virtqueue we're talking about. |
e6f633e5 TB |
2553 | * |
2554 | * Note that this is not necessarily synchronous, hence unreliable and only | |
2555 | * useful as an optimization. | |
2556 | * | |
2557 | * Unlike other operations, this need not be serialized. | |
2558 | */ | |
2559 | void virtqueue_disable_cb(struct virtqueue *_vq) | |
2560 | { | |
1ce9e605 TB |
2561 | struct vring_virtqueue *vq = to_vvq(_vq); |
2562 | ||
2563 | if (vq->packed_ring) | |
2564 | virtqueue_disable_cb_packed(_vq); | |
2565 | else | |
2566 | virtqueue_disable_cb_split(_vq); | |
e6f633e5 TB |
2567 | } |
2568 | EXPORT_SYMBOL_GPL(virtqueue_disable_cb); | |
2569 | ||
2570 | /** | |
2571 | * virtqueue_enable_cb_prepare - restart callbacks after disable_cb | |
a5581206 | 2572 | * @_vq: the struct virtqueue we're talking about. |
e6f633e5 TB |
2573 | * |
2574 | * This re-enables callbacks; it returns current queue state | |
2575 | * in an opaque unsigned value. This value should be later tested by | |
2576 | * virtqueue_poll, to detect a possible race between the driver checking for | |
2577 | * more work, and enabling callbacks. | |
2578 | * | |
2579 | * Caller must ensure we don't call this with other virtqueue | |
2580 | * operations at the same time (except where noted). | |
2581 | */ | |
31532340 | 2582 | unsigned int virtqueue_enable_cb_prepare(struct virtqueue *_vq) |
e6f633e5 | 2583 | { |
1ce9e605 TB |
2584 | struct vring_virtqueue *vq = to_vvq(_vq); |
2585 | ||
8d622d21 MT |
2586 | if (vq->event_triggered) |
2587 | vq->event_triggered = false; | |
2588 | ||
1ce9e605 TB |
2589 | return vq->packed_ring ? virtqueue_enable_cb_prepare_packed(_vq) : |
2590 | virtqueue_enable_cb_prepare_split(_vq); | |
e6f633e5 TB |
2591 | } |
2592 | EXPORT_SYMBOL_GPL(virtqueue_enable_cb_prepare); | |
2593 | ||
2594 | /** | |
2595 | * virtqueue_poll - query pending used buffers | |
a5581206 | 2596 | * @_vq: the struct virtqueue we're talking about. |
e6f633e5 TB |
2597 | * @last_used_idx: virtqueue state (from call to virtqueue_enable_cb_prepare). |
2598 | * | |
2599 | * Returns "true" if there are pending used buffers in the queue. | |
2600 | * | |
2601 | * This does not need to be serialized. | |
2602 | */ | |
31532340 | 2603 | bool virtqueue_poll(struct virtqueue *_vq, unsigned int last_used_idx) |
e6f633e5 TB |
2604 | { |
2605 | struct vring_virtqueue *vq = to_vvq(_vq); | |
2606 | ||
481a0d74 MW |
2607 | if (unlikely(vq->broken)) |
2608 | return false; | |
2609 | ||
e6f633e5 | 2610 | virtio_mb(vq->weak_barriers); |
1ce9e605 TB |
2611 | return vq->packed_ring ? virtqueue_poll_packed(_vq, last_used_idx) : |
2612 | virtqueue_poll_split(_vq, last_used_idx); | |
e6f633e5 TB |
2613 | } |
2614 | EXPORT_SYMBOL_GPL(virtqueue_poll); | |
2615 | ||
2616 | /** | |
2617 | * virtqueue_enable_cb - restart callbacks after disable_cb. | |
a5581206 | 2618 | * @_vq: the struct virtqueue we're talking about. |
e6f633e5 TB |
2619 | * |
2620 | * This re-enables callbacks; it returns "false" if there are pending | |
2621 | * buffers in the queue, to detect a possible race between the driver | |
2622 | * checking for more work, and enabling callbacks. | |
2623 | * | |
2624 | * Caller must ensure we don't call this with other virtqueue | |
2625 | * operations at the same time (except where noted). | |
2626 | */ | |
2627 | bool virtqueue_enable_cb(struct virtqueue *_vq) | |
2628 | { | |
31532340 | 2629 | unsigned int last_used_idx = virtqueue_enable_cb_prepare(_vq); |
e6f633e5 TB |
2630 | |
2631 | return !virtqueue_poll(_vq, last_used_idx); | |
2632 | } | |
2633 | EXPORT_SYMBOL_GPL(virtqueue_enable_cb); | |
2634 | ||
2635 | /** | |
2636 | * virtqueue_enable_cb_delayed - restart callbacks after disable_cb. | |
a5581206 | 2637 | * @_vq: the struct virtqueue we're talking about. |
e6f633e5 TB |
2638 | * |
2639 | * This re-enables callbacks but hints to the other side to delay | |
2640 | * interrupts until most of the available buffers have been processed; | |
2641 | * it returns "false" if there are many pending buffers in the queue, | |
2642 | * to detect a possible race between the driver checking for more work, | |
2643 | * and enabling callbacks. | |
2644 | * | |
2645 | * Caller must ensure we don't call this with other virtqueue | |
2646 | * operations at the same time (except where noted). | |
2647 | */ | |
2648 | bool virtqueue_enable_cb_delayed(struct virtqueue *_vq) | |
2649 | { | |
1ce9e605 TB |
2650 | struct vring_virtqueue *vq = to_vvq(_vq); |
2651 | ||
8d622d21 | 2652 | if (vq->event_triggered) |
2e2f925f | 2653 | data_race(vq->event_triggered = false); |
8d622d21 | 2654 | |
1ce9e605 TB |
2655 | return vq->packed_ring ? virtqueue_enable_cb_delayed_packed(_vq) : |
2656 | virtqueue_enable_cb_delayed_split(_vq); | |
e6f633e5 TB |
2657 | } |
2658 | EXPORT_SYMBOL_GPL(virtqueue_enable_cb_delayed); | |
2659 | ||
138fd251 TB |
2660 | /** |
2661 | * virtqueue_detach_unused_buf - detach first unused buffer | |
a5581206 | 2662 | * @_vq: the struct virtqueue we're talking about. |
138fd251 TB |
2663 | * |
2664 | * Returns NULL or the "data" token handed to virtqueue_add_*(). | |
a62eecb3 XZ |
2665 | * This is not valid on an active queue; it is useful for device |
2666 | * shutdown or the reset queue. | |
138fd251 TB |
2667 | */ |
2668 | void *virtqueue_detach_unused_buf(struct virtqueue *_vq) | |
2669 | { | |
1ce9e605 TB |
2670 | struct vring_virtqueue *vq = to_vvq(_vq); |
2671 | ||
2672 | return vq->packed_ring ? virtqueue_detach_unused_buf_packed(_vq) : | |
2673 | virtqueue_detach_unused_buf_split(_vq); | |
138fd251 | 2674 | } |
7c5e9ed0 | 2675 | EXPORT_SYMBOL_GPL(virtqueue_detach_unused_buf); |
c021eac4 | 2676 | |
138fd251 TB |
2677 | static inline bool more_used(const struct vring_virtqueue *vq) |
2678 | { | |
1ce9e605 | 2679 | return vq->packed_ring ? more_used_packed(vq) : more_used_split(vq); |
138fd251 TB |
2680 | } |
2681 | ||
5c669c4a RC |
2682 | /** |
2683 | * vring_interrupt - notify a virtqueue on an interrupt | |
2684 | * @irq: the IRQ number (ignored) | |
2685 | * @_vq: the struct virtqueue to notify | |
2686 | * | |
2687 | * Calls the callback function of @_vq to process the virtqueue | |
2688 | * notification. | |
2689 | */ | |
0a8a69dd RR |
2690 | irqreturn_t vring_interrupt(int irq, void *_vq) |
2691 | { | |
2692 | struct vring_virtqueue *vq = to_vvq(_vq); | |
2693 | ||
2694 | if (!more_used(vq)) { | |
2695 | pr_debug("virtqueue interrupt with no work for %p\n", vq); | |
2696 | return IRQ_NONE; | |
2697 | } | |
2698 | ||
8b4ec69d | 2699 | if (unlikely(vq->broken)) { |
c346dae4 | 2700 | #ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION |
8b4ec69d JW |
2701 | dev_warn_once(&vq->vq.vdev->dev, |
2702 | "virtio vring IRQ raised before DRIVER_OK"); | |
2703 | return IRQ_NONE; | |
c346dae4 JW |
2704 | #else |
2705 | return IRQ_HANDLED; | |
2706 | #endif | |
8b4ec69d | 2707 | } |
0a8a69dd | 2708 | |
8d622d21 MT |
2709 | /* Just a hint for performance: so it's ok that this can be racy! */ |
2710 | if (vq->event) | |
83c334ed | 2711 | data_race(vq->event_triggered = true); |
8d622d21 | 2712 | |
0a8a69dd | 2713 | pr_debug("virtqueue callback for %p (%p)\n", vq, vq->vq.callback); |
18445c4d RR |
2714 | if (vq->vq.callback) |
2715 | vq->vq.callback(&vq->vq); | |
0a8a69dd RR |
2716 | |
2717 | return IRQ_HANDLED; | |
2718 | } | |
c6fd4701 | 2719 | EXPORT_SYMBOL_GPL(vring_interrupt); |
0a8a69dd | 2720 | |
2a2d1382 AL |
2721 | struct virtqueue *vring_create_virtqueue( |
2722 | unsigned int index, | |
2723 | unsigned int num, | |
2724 | unsigned int vring_align, | |
2725 | struct virtio_device *vdev, | |
2726 | bool weak_barriers, | |
2727 | bool may_reduce_num, | |
f94682dd | 2728 | bool context, |
2a2d1382 AL |
2729 | bool (*notify)(struct virtqueue *), |
2730 | void (*callback)(struct virtqueue *), | |
2731 | const char *name) | |
2732 | { | |
1ce9e605 TB |
2733 | |
2734 | if (virtio_has_feature(vdev, VIRTIO_F_RING_PACKED)) | |
2735 | return vring_create_virtqueue_packed(index, num, vring_align, | |
2736 | vdev, weak_barriers, may_reduce_num, | |
2713ea3c | 2737 | context, notify, callback, name, vdev->dev.parent); |
1ce9e605 | 2738 | |
d79dca75 TB |
2739 | return vring_create_virtqueue_split(index, num, vring_align, |
2740 | vdev, weak_barriers, may_reduce_num, | |
2713ea3c | 2741 | context, notify, callback, name, vdev->dev.parent); |
2a2d1382 AL |
2742 | } |
2743 | EXPORT_SYMBOL_GPL(vring_create_virtqueue); | |
2744 | ||
2713ea3c JW |
2745 | struct virtqueue *vring_create_virtqueue_dma( |
2746 | unsigned int index, | |
2747 | unsigned int num, | |
2748 | unsigned int vring_align, | |
2749 | struct virtio_device *vdev, | |
2750 | bool weak_barriers, | |
2751 | bool may_reduce_num, | |
2752 | bool context, | |
2753 | bool (*notify)(struct virtqueue *), | |
2754 | void (*callback)(struct virtqueue *), | |
2755 | const char *name, | |
2756 | struct device *dma_dev) | |
2757 | { | |
2758 | ||
2759 | if (virtio_has_feature(vdev, VIRTIO_F_RING_PACKED)) | |
2760 | return vring_create_virtqueue_packed(index, num, vring_align, | |
2761 | vdev, weak_barriers, may_reduce_num, | |
2762 | context, notify, callback, name, dma_dev); | |
2763 | ||
2764 | return vring_create_virtqueue_split(index, num, vring_align, | |
2765 | vdev, weak_barriers, may_reduce_num, | |
2766 | context, notify, callback, name, dma_dev); | |
2767 | } | |
2768 | EXPORT_SYMBOL_GPL(vring_create_virtqueue_dma); | |
2769 | ||
c790e8e1 XZ |
2770 | /** |
2771 | * virtqueue_resize - resize the vring of vq | |
2772 | * @_vq: the struct virtqueue we're talking about. | |
2773 | * @num: new ring num | |
4d09f240 | 2774 | * @recycle: callback to recycle unused buffers |
8d6712c8 | 2775 | * @recycle_done: callback to be invoked when recycle for all unused buffers done |
c790e8e1 XZ |
2776 | * |
2777 | * When it is really necessary to create a new vring, it will set the current vq | |
2778 | * into the reset state. Then call the passed callback to recycle the buffer | |
2779 | * that is no longer used. Only after the new vring is successfully created, the | |
2780 | * old vring will be released. | |
2781 | * | |
2782 | * Caller must ensure we don't call this with other virtqueue operations | |
2783 | * at the same time (except where noted). | |
2784 | * | |
2785 | * Returns zero or a negative error. | |
2786 | * 0: success. | |
2787 | * -ENOMEM: Failed to allocate a new ring, fall back to the original ring size. | |
2788 | * vq can still work normally | |
2789 | * -EBUSY: Failed to sync with device, vq may not work properly | |
2790 | * -ENOENT: Transport or device not supported | |
2791 | * -E2BIG/-EINVAL: num error | |
2792 | * -EPERM: Operation not permitted | |
2793 | * | |
2794 | */ | |
2795 | int virtqueue_resize(struct virtqueue *_vq, u32 num, | |
8d6712c8 KD |
2796 | void (*recycle)(struct virtqueue *vq, void *buf), |
2797 | void (*recycle_done)(struct virtqueue *vq)) | |
c790e8e1 XZ |
2798 | { |
2799 | struct vring_virtqueue *vq = to_vvq(_vq); | |
45ebc7e6 | 2800 | int err, err_reset; |
c790e8e1 | 2801 | |
c790e8e1 XZ |
2802 | if (num > vq->vq.num_max) |
2803 | return -E2BIG; | |
2804 | ||
2805 | if (!num) | |
2806 | return -EINVAL; | |
2807 | ||
2808 | if ((vq->packed_ring ? vq->packed.vring.num : vq->split.vring.num) == num) | |
2809 | return 0; | |
2810 | ||
ad48d53b | 2811 | err = virtqueue_disable_and_recycle(_vq, recycle); |
c790e8e1 XZ |
2812 | if (err) |
2813 | return err; | |
8d6712c8 KD |
2814 | if (recycle_done) |
2815 | recycle_done(_vq); | |
c790e8e1 | 2816 | |
c790e8e1 XZ |
2817 | if (vq->packed_ring) |
2818 | err = virtqueue_resize_packed(_vq, num); | |
2819 | else | |
2820 | err = virtqueue_resize_split(_vq, num); | |
2821 | ||
45ebc7e6 LV |
2822 | err_reset = virtqueue_enable_after_reset(_vq); |
2823 | if (err_reset) | |
2824 | return err_reset; | |
2825 | ||
2826 | return err; | |
c790e8e1 XZ |
2827 | } |
2828 | EXPORT_SYMBOL_GPL(virtqueue_resize); | |
2829 | ||
ba3e0c47 XZ |
2830 | /** |
2831 | * virtqueue_reset - detach and recycle all unused buffers | |
2832 | * @_vq: the struct virtqueue we're talking about. | |
2833 | * @recycle: callback to recycle unused buffers | |
8d2da07c | 2834 | * @recycle_done: callback to be invoked when recycle for all unused buffers done |
ba3e0c47 XZ |
2835 | * |
2836 | * Caller must ensure we don't call this with other virtqueue operations | |
2837 | * at the same time (except where noted). | |
2838 | * | |
2839 | * Returns zero or a negative error. | |
2840 | * 0: success. | |
2841 | * -EBUSY: Failed to sync with device, vq may not work properly | |
2842 | * -ENOENT: Transport or device not supported | |
2843 | * -EPERM: Operation not permitted | |
2844 | */ | |
2845 | int virtqueue_reset(struct virtqueue *_vq, | |
8d2da07c KD |
2846 | void (*recycle)(struct virtqueue *vq, void *buf), |
2847 | void (*recycle_done)(struct virtqueue *vq)) | |
ba3e0c47 XZ |
2848 | { |
2849 | struct vring_virtqueue *vq = to_vvq(_vq); | |
2850 | int err; | |
2851 | ||
2852 | err = virtqueue_disable_and_recycle(_vq, recycle); | |
2853 | if (err) | |
2854 | return err; | |
8d2da07c KD |
2855 | if (recycle_done) |
2856 | recycle_done(_vq); | |
ba3e0c47 XZ |
2857 | |
2858 | if (vq->packed_ring) | |
2859 | virtqueue_reinit_packed(vq); | |
2860 | else | |
2861 | virtqueue_reinit_split(vq); | |
2862 | ||
2863 | return virtqueue_enable_after_reset(_vq); | |
2864 | } | |
2865 | EXPORT_SYMBOL_GPL(virtqueue_reset); | |
2866 | ||
2a2d1382 AL |
2867 | struct virtqueue *vring_new_virtqueue(unsigned int index, |
2868 | unsigned int num, | |
2869 | unsigned int vring_align, | |
2870 | struct virtio_device *vdev, | |
2871 | bool weak_barriers, | |
f94682dd | 2872 | bool context, |
2a2d1382 AL |
2873 | void *pages, |
2874 | bool (*notify)(struct virtqueue *vq), | |
2875 | void (*callback)(struct virtqueue *vq), | |
2876 | const char *name) | |
2877 | { | |
cd4c812a | 2878 | struct vring_virtqueue_split vring_split = {}; |
1ce9e605 | 2879 | |
a49c26f7 WH |
2880 | if (virtio_has_feature(vdev, VIRTIO_F_RING_PACKED)) { |
2881 | struct vring_virtqueue_packed vring_packed = {}; | |
2882 | ||
2883 | vring_packed.vring.num = num; | |
2884 | vring_packed.vring.desc = pages; | |
2885 | return __vring_new_virtqueue_packed(index, &vring_packed, | |
2886 | vdev, weak_barriers, | |
2887 | context, notify, callback, | |
2888 | name, vdev->dev.parent); | |
2889 | } | |
1ce9e605 | 2890 | |
cd4c812a | 2891 | vring_init(&vring_split.vring, num, pages, vring_align); |
a49c26f7 | 2892 | return __vring_new_virtqueue_split(index, &vring_split, vdev, weak_barriers, |
2713ea3c JW |
2893 | context, notify, callback, name, |
2894 | vdev->dev.parent); | |
2a2d1382 | 2895 | } |
c6fd4701 | 2896 | EXPORT_SYMBOL_GPL(vring_new_virtqueue); |
0a8a69dd | 2897 | |
3ea19e32 | 2898 | static void vring_free(struct virtqueue *_vq) |
0a8a69dd | 2899 | { |
2a2d1382 AL |
2900 | struct vring_virtqueue *vq = to_vvq(_vq); |
2901 | ||
2902 | if (vq->we_own_ring) { | |
1ce9e605 TB |
2903 | if (vq->packed_ring) { |
2904 | vring_free_queue(vq->vq.vdev, | |
2905 | vq->packed.ring_size_in_bytes, | |
2906 | vq->packed.vring.desc, | |
2713ea3c JW |
2907 | vq->packed.ring_dma_addr, |
2908 | vring_dma_dev(vq)); | |
1ce9e605 TB |
2909 | |
2910 | vring_free_queue(vq->vq.vdev, | |
2911 | vq->packed.event_size_in_bytes, | |
2912 | vq->packed.vring.driver, | |
2713ea3c JW |
2913 | vq->packed.driver_event_dma_addr, |
2914 | vring_dma_dev(vq)); | |
1ce9e605 TB |
2915 | |
2916 | vring_free_queue(vq->vq.vdev, | |
2917 | vq->packed.event_size_in_bytes, | |
2918 | vq->packed.vring.device, | |
2713ea3c JW |
2919 | vq->packed.device_event_dma_addr, |
2920 | vring_dma_dev(vq)); | |
1ce9e605 TB |
2921 | |
2922 | kfree(vq->packed.desc_state); | |
2923 | kfree(vq->packed.desc_extra); | |
2924 | } else { | |
2925 | vring_free_queue(vq->vq.vdev, | |
2926 | vq->split.queue_size_in_bytes, | |
2927 | vq->split.vring.desc, | |
2713ea3c JW |
2928 | vq->split.queue_dma_addr, |
2929 | vring_dma_dev(vq)); | |
1ce9e605 | 2930 | } |
2a2d1382 | 2931 | } |
72b5e895 | 2932 | if (!vq->packed_ring) { |
f13f09a1 | 2933 | kfree(vq->split.desc_state); |
72b5e895 JW |
2934 | kfree(vq->split.desc_extra); |
2935 | } | |
3ea19e32 XZ |
2936 | } |
2937 | ||
2938 | void vring_del_virtqueue(struct virtqueue *_vq) | |
2939 | { | |
2940 | struct vring_virtqueue *vq = to_vvq(_vq); | |
2941 | ||
2942 | spin_lock(&vq->vq.vdev->vqs_list_lock); | |
2943 | list_del(&_vq->list); | |
2944 | spin_unlock(&vq->vq.vdev->vqs_list_lock); | |
2945 | ||
2946 | vring_free(_vq); | |
2947 | ||
2a2d1382 | 2948 | kfree(vq); |
0a8a69dd | 2949 | } |
c6fd4701 | 2950 | EXPORT_SYMBOL_GPL(vring_del_virtqueue); |
0a8a69dd | 2951 | |
af8ececd VP |
2952 | u32 vring_notification_data(struct virtqueue *_vq) |
2953 | { | |
2954 | struct vring_virtqueue *vq = to_vvq(_vq); | |
2955 | u16 next; | |
2956 | ||
2957 | if (vq->packed_ring) | |
2958 | next = (vq->packed.next_avail_idx & | |
2959 | ~(-(1 << VRING_PACKED_EVENT_F_WRAP_CTR))) | | |
2960 | vq->packed.avail_wrap_counter << | |
2961 | VRING_PACKED_EVENT_F_WRAP_CTR; | |
2962 | else | |
2963 | next = vq->split.avail_idx_shadow; | |
2964 | ||
2965 | return next << 16 | _vq->index; | |
2966 | } | |
2967 | EXPORT_SYMBOL_GPL(vring_notification_data); | |
2968 | ||
e34f8725 RR |
2969 | /* Manipulates transport-specific feature bits. */ |
2970 | void vring_transport_features(struct virtio_device *vdev) | |
2971 | { | |
2972 | unsigned int i; | |
2973 | ||
2974 | for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++) { | |
2975 | switch (i) { | |
9fa29b9d MM |
2976 | case VIRTIO_RING_F_INDIRECT_DESC: |
2977 | break; | |
a5c262c5 MT |
2978 | case VIRTIO_RING_F_EVENT_IDX: |
2979 | break; | |
747ae34a MT |
2980 | case VIRTIO_F_VERSION_1: |
2981 | break; | |
321bd212 | 2982 | case VIRTIO_F_ACCESS_PLATFORM: |
1a937693 | 2983 | break; |
f959a128 TB |
2984 | case VIRTIO_F_RING_PACKED: |
2985 | break; | |
45383fb0 TB |
2986 | case VIRTIO_F_ORDER_PLATFORM: |
2987 | break; | |
af8ececd VP |
2988 | case VIRTIO_F_NOTIFICATION_DATA: |
2989 | break; | |
e34f8725 RR |
2990 | default: |
2991 | /* We don't understand this bit. */ | |
e16e12be | 2992 | __virtio_clear_bit(vdev, i); |
e34f8725 RR |
2993 | } |
2994 | } | |
2995 | } | |
2996 | EXPORT_SYMBOL_GPL(vring_transport_features); | |
2997 | ||
5dfc1762 RR |
2998 | /** |
2999 | * virtqueue_get_vring_size - return the size of the virtqueue's vring | |
a5581206 | 3000 | * @_vq: the struct virtqueue containing the vring of interest. |
5dfc1762 RR |
3001 | * |
3002 | * Returns the size of the vring. This is mainly used for boasting to | |
3003 | * userspace. Unlike other operations, this need not be serialized. | |
3004 | */ | |
4b6ec919 | 3005 | unsigned int virtqueue_get_vring_size(const struct virtqueue *_vq) |
8f9f4668 RJ |
3006 | { |
3007 | ||
4b6ec919 | 3008 | const struct vring_virtqueue *vq = to_vvq(_vq); |
8f9f4668 | 3009 | |
1ce9e605 | 3010 | return vq->packed_ring ? vq->packed.vring.num : vq->split.vring.num; |
8f9f4668 RJ |
3011 | } |
3012 | EXPORT_SYMBOL_GPL(virtqueue_get_vring_size); | |
3013 | ||
32510631 XZ |
3014 | /* |
3015 | * This function should only be called by the core, not directly by the driver. | |
3016 | */ | |
3017 | void __virtqueue_break(struct virtqueue *_vq) | |
3018 | { | |
3019 | struct vring_virtqueue *vq = to_vvq(_vq); | |
3020 | ||
3021 | /* Pairs with READ_ONCE() in virtqueue_is_broken(). */ | |
3022 | WRITE_ONCE(vq->broken, true); | |
3023 | } | |
3024 | EXPORT_SYMBOL_GPL(__virtqueue_break); | |
3025 | ||
3026 | /* | |
3027 | * This function should only be called by the core, not directly by the driver. | |
3028 | */ | |
3029 | void __virtqueue_unbreak(struct virtqueue *_vq) | |
3030 | { | |
3031 | struct vring_virtqueue *vq = to_vvq(_vq); | |
3032 | ||
3033 | /* Pairs with READ_ONCE() in virtqueue_is_broken(). */ | |
3034 | WRITE_ONCE(vq->broken, false); | |
3035 | } | |
3036 | EXPORT_SYMBOL_GPL(__virtqueue_unbreak); | |
3037 | ||
4b6ec919 | 3038 | bool virtqueue_is_broken(const struct virtqueue *_vq) |
b3b32c94 | 3039 | { |
4b6ec919 | 3040 | const struct vring_virtqueue *vq = to_vvq(_vq); |
b3b32c94 | 3041 | |
60f07798 | 3042 | return READ_ONCE(vq->broken); |
b3b32c94 HG |
3043 | } |
3044 | EXPORT_SYMBOL_GPL(virtqueue_is_broken); | |
3045 | ||
e2dcdfe9 RR |
3046 | /* |
3047 | * This should prevent the device from being used, allowing drivers to | |
3048 | * recover. You may need to grab appropriate locks to flush. | |
3049 | */ | |
3050 | void virtio_break_device(struct virtio_device *dev) | |
3051 | { | |
3052 | struct virtqueue *_vq; | |
3053 | ||
0e566c8f | 3054 | spin_lock(&dev->vqs_list_lock); |
e2dcdfe9 RR |
3055 | list_for_each_entry(_vq, &dev->vqs, list) { |
3056 | struct vring_virtqueue *vq = to_vvq(_vq); | |
60f07798 PP |
3057 | |
3058 | /* Pairs with READ_ONCE() in virtqueue_is_broken(). */ | |
3059 | WRITE_ONCE(vq->broken, true); | |
e2dcdfe9 | 3060 | } |
0e566c8f | 3061 | spin_unlock(&dev->vqs_list_lock); |
e2dcdfe9 RR |
3062 | } |
3063 | EXPORT_SYMBOL_GPL(virtio_break_device); | |
3064 | ||
be83f04d JW |
3065 | /* |
3066 | * This should allow the device to be used by the driver. You may | |
3067 | * need to grab appropriate locks to flush the write to | |
3068 | * vq->broken. This should only be used in some specific case e.g | |
3069 | * (probing and restoring). This function should only be called by the | |
3070 | * core, not directly by the driver. | |
3071 | */ | |
3072 | void __virtio_unbreak_device(struct virtio_device *dev) | |
3073 | { | |
3074 | struct virtqueue *_vq; | |
3075 | ||
3076 | spin_lock(&dev->vqs_list_lock); | |
3077 | list_for_each_entry(_vq, &dev->vqs, list) { | |
3078 | struct vring_virtqueue *vq = to_vvq(_vq); | |
3079 | ||
3080 | /* Pairs with READ_ONCE() in virtqueue_is_broken(). */ | |
3081 | WRITE_ONCE(vq->broken, false); | |
3082 | } | |
3083 | spin_unlock(&dev->vqs_list_lock); | |
3084 | } | |
3085 | EXPORT_SYMBOL_GPL(__virtio_unbreak_device); | |
3086 | ||
4b6ec919 | 3087 | dma_addr_t virtqueue_get_desc_addr(const struct virtqueue *_vq) |
89062652 | 3088 | { |
4b6ec919 | 3089 | const struct vring_virtqueue *vq = to_vvq(_vq); |
89062652 | 3090 | |
2a2d1382 AL |
3091 | BUG_ON(!vq->we_own_ring); |
3092 | ||
1ce9e605 TB |
3093 | if (vq->packed_ring) |
3094 | return vq->packed.ring_dma_addr; | |
3095 | ||
d79dca75 | 3096 | return vq->split.queue_dma_addr; |
89062652 | 3097 | } |
2a2d1382 | 3098 | EXPORT_SYMBOL_GPL(virtqueue_get_desc_addr); |
89062652 | 3099 | |
4b6ec919 | 3100 | dma_addr_t virtqueue_get_avail_addr(const struct virtqueue *_vq) |
89062652 | 3101 | { |
4b6ec919 | 3102 | const struct vring_virtqueue *vq = to_vvq(_vq); |
89062652 | 3103 | |
2a2d1382 AL |
3104 | BUG_ON(!vq->we_own_ring); |
3105 | ||
1ce9e605 TB |
3106 | if (vq->packed_ring) |
3107 | return vq->packed.driver_event_dma_addr; | |
3108 | ||
d79dca75 | 3109 | return vq->split.queue_dma_addr + |
e593bf97 | 3110 | ((char *)vq->split.vring.avail - (char *)vq->split.vring.desc); |
2a2d1382 AL |
3111 | } |
3112 | EXPORT_SYMBOL_GPL(virtqueue_get_avail_addr); | |
3113 | ||
4b6ec919 | 3114 | dma_addr_t virtqueue_get_used_addr(const struct virtqueue *_vq) |
2a2d1382 | 3115 | { |
4b6ec919 | 3116 | const struct vring_virtqueue *vq = to_vvq(_vq); |
2a2d1382 AL |
3117 | |
3118 | BUG_ON(!vq->we_own_ring); | |
3119 | ||
1ce9e605 TB |
3120 | if (vq->packed_ring) |
3121 | return vq->packed.device_event_dma_addr; | |
3122 | ||
d79dca75 | 3123 | return vq->split.queue_dma_addr + |
e593bf97 | 3124 | ((char *)vq->split.vring.used - (char *)vq->split.vring.desc); |
2a2d1382 AL |
3125 | } |
3126 | EXPORT_SYMBOL_GPL(virtqueue_get_used_addr); | |
3127 | ||
1ce9e605 | 3128 | /* Only available for split ring */ |
4b6ec919 | 3129 | const struct vring *virtqueue_get_vring(const struct virtqueue *vq) |
2a2d1382 | 3130 | { |
e593bf97 | 3131 | return &to_vvq(vq)->split.vring; |
89062652 | 3132 | } |
2a2d1382 | 3133 | EXPORT_SYMBOL_GPL(virtqueue_get_vring); |
89062652 | 3134 | |
b6253b4e XZ |
3135 | /** |
3136 | * virtqueue_dma_map_single_attrs - map DMA for _vq | |
3137 | * @_vq: the struct virtqueue we're talking about. | |
3138 | * @ptr: the pointer of the buffer to do dma | |
3139 | * @size: the size of the buffer to do dma | |
3140 | * @dir: DMA direction | |
3141 | * @attrs: DMA Attrs | |
3142 | * | |
3143 | * The caller calls this to do dma mapping in advance. The DMA address can be | |
3144 | * passed to this _vq when it is in pre-mapped mode. | |
3145 | * | |
3146 | * return DMA address. Caller should check that by virtqueue_dma_mapping_error(). | |
3147 | */ | |
3148 | dma_addr_t virtqueue_dma_map_single_attrs(struct virtqueue *_vq, void *ptr, | |
3149 | size_t size, | |
3150 | enum dma_data_direction dir, | |
3151 | unsigned long attrs) | |
3152 | { | |
3153 | struct vring_virtqueue *vq = to_vvq(_vq); | |
3154 | ||
840b2d39 XZ |
3155 | if (!vq->use_dma_api) { |
3156 | kmsan_handle_dma(virt_to_page(ptr), offset_in_page(ptr), size, dir); | |
b6253b4e | 3157 | return (dma_addr_t)virt_to_phys(ptr); |
840b2d39 | 3158 | } |
b6253b4e XZ |
3159 | |
3160 | return dma_map_single_attrs(vring_dma_dev(vq), ptr, size, dir, attrs); | |
3161 | } | |
3162 | EXPORT_SYMBOL_GPL(virtqueue_dma_map_single_attrs); | |
3163 | ||
3164 | /** | |
3165 | * virtqueue_dma_unmap_single_attrs - unmap DMA for _vq | |
3166 | * @_vq: the struct virtqueue we're talking about. | |
3167 | * @addr: the dma address to unmap | |
3168 | * @size: the size of the buffer | |
3169 | * @dir: DMA direction | |
3170 | * @attrs: DMA Attrs | |
3171 | * | |
3172 | * Unmap the address that is mapped by the virtqueue_dma_map_* APIs. | |
3173 | * | |
3174 | */ | |
3175 | void virtqueue_dma_unmap_single_attrs(struct virtqueue *_vq, dma_addr_t addr, | |
3176 | size_t size, enum dma_data_direction dir, | |
3177 | unsigned long attrs) | |
3178 | { | |
3179 | struct vring_virtqueue *vq = to_vvq(_vq); | |
3180 | ||
3181 | if (!vq->use_dma_api) | |
3182 | return; | |
3183 | ||
3184 | dma_unmap_single_attrs(vring_dma_dev(vq), addr, size, dir, attrs); | |
3185 | } | |
3186 | EXPORT_SYMBOL_GPL(virtqueue_dma_unmap_single_attrs); | |
3187 | ||
3188 | /** | |
3189 | * virtqueue_dma_mapping_error - check dma address | |
3190 | * @_vq: the struct virtqueue we're talking about. | |
3191 | * @addr: DMA address | |
3192 | * | |
3193 | * Returns 0 means dma valid. Other means invalid dma address. | |
3194 | */ | |
3195 | int virtqueue_dma_mapping_error(struct virtqueue *_vq, dma_addr_t addr) | |
3196 | { | |
3197 | struct vring_virtqueue *vq = to_vvq(_vq); | |
3198 | ||
3199 | if (!vq->use_dma_api) | |
3200 | return 0; | |
3201 | ||
3202 | return dma_mapping_error(vring_dma_dev(vq), addr); | |
3203 | } | |
3204 | EXPORT_SYMBOL_GPL(virtqueue_dma_mapping_error); | |
3205 | ||
8bd2f710 XZ |
3206 | /** |
3207 | * virtqueue_dma_need_sync - check a dma address needs sync | |
3208 | * @_vq: the struct virtqueue we're talking about. | |
3209 | * @addr: DMA address | |
3210 | * | |
3211 | * Check if the dma address mapped by the virtqueue_dma_map_* APIs needs to be | |
3212 | * synchronized | |
3213 | * | |
3214 | * return bool | |
3215 | */ | |
3216 | bool virtqueue_dma_need_sync(struct virtqueue *_vq, dma_addr_t addr) | |
3217 | { | |
3218 | struct vring_virtqueue *vq = to_vvq(_vq); | |
3219 | ||
3220 | if (!vq->use_dma_api) | |
3221 | return false; | |
3222 | ||
3223 | return dma_need_sync(vring_dma_dev(vq), addr); | |
3224 | } | |
3225 | EXPORT_SYMBOL_GPL(virtqueue_dma_need_sync); | |
3226 | ||
3227 | /** | |
3228 | * virtqueue_dma_sync_single_range_for_cpu - dma sync for cpu | |
3229 | * @_vq: the struct virtqueue we're talking about. | |
3230 | * @addr: DMA address | |
3231 | * @offset: DMA address offset | |
3232 | * @size: buf size for sync | |
3233 | * @dir: DMA direction | |
3234 | * | |
3235 | * Before calling this function, use virtqueue_dma_need_sync() to confirm that | |
3236 | * the DMA address really needs to be synchronized | |
3237 | * | |
3238 | */ | |
3239 | void virtqueue_dma_sync_single_range_for_cpu(struct virtqueue *_vq, | |
3240 | dma_addr_t addr, | |
3241 | unsigned long offset, size_t size, | |
3242 | enum dma_data_direction dir) | |
3243 | { | |
3244 | struct vring_virtqueue *vq = to_vvq(_vq); | |
3245 | struct device *dev = vring_dma_dev(vq); | |
3246 | ||
3247 | if (!vq->use_dma_api) | |
3248 | return; | |
3249 | ||
1f475cd5 | 3250 | dma_sync_single_range_for_cpu(dev, addr, offset, size, dir); |
8bd2f710 XZ |
3251 | } |
3252 | EXPORT_SYMBOL_GPL(virtqueue_dma_sync_single_range_for_cpu); | |
3253 | ||
3254 | /** | |
3255 | * virtqueue_dma_sync_single_range_for_device - dma sync for device | |
3256 | * @_vq: the struct virtqueue we're talking about. | |
3257 | * @addr: DMA address | |
3258 | * @offset: DMA address offset | |
3259 | * @size: buf size for sync | |
3260 | * @dir: DMA direction | |
3261 | * | |
3262 | * Before calling this function, use virtqueue_dma_need_sync() to confirm that | |
3263 | * the DMA address really needs to be synchronized | |
3264 | */ | |
3265 | void virtqueue_dma_sync_single_range_for_device(struct virtqueue *_vq, | |
3266 | dma_addr_t addr, | |
3267 | unsigned long offset, size_t size, | |
3268 | enum dma_data_direction dir) | |
3269 | { | |
3270 | struct vring_virtqueue *vq = to_vvq(_vq); | |
3271 | struct device *dev = vring_dma_dev(vq); | |
3272 | ||
3273 | if (!vq->use_dma_api) | |
3274 | return; | |
3275 | ||
1f475cd5 | 3276 | dma_sync_single_range_for_device(dev, addr, offset, size, dir); |
8bd2f710 XZ |
3277 | } |
3278 | EXPORT_SYMBOL_GPL(virtqueue_dma_sync_single_range_for_device); | |
3279 | ||
ab0727f3 | 3280 | MODULE_DESCRIPTION("Virtio ring implementation"); |
c6fd4701 | 3281 | MODULE_LICENSE("GPL"); |