Commit | Line | Data |
---|---|---|
0a8a69dd RR |
1 | /* Virtio ring implementation. |
2 | * | |
3 | * Copyright 2007 Rusty Russell IBM Corporation | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation; either version 2 of the License, or | |
8 | * (at your option) any later version. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program; if not, write to the Free Software | |
17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
18 | */ | |
19 | #include <linux/virtio.h> | |
20 | #include <linux/virtio_ring.h> | |
e34f8725 | 21 | #include <linux/virtio_config.h> |
0a8a69dd | 22 | #include <linux/device.h> |
5a0e3ad6 | 23 | #include <linux/slab.h> |
b5a2c4f1 | 24 | #include <linux/module.h> |
e93300b1 | 25 | #include <linux/hrtimer.h> |
6abb2dd9 | 26 | #include <linux/kmemleak.h> |
0a8a69dd RR |
27 | |
28 | #ifdef DEBUG | |
29 | /* For development, we want to crash whenever the ring is screwed. */ | |
9499f5e7 RR |
30 | #define BAD_RING(_vq, fmt, args...) \ |
31 | do { \ | |
32 | dev_err(&(_vq)->vq.vdev->dev, \ | |
33 | "%s:"fmt, (_vq)->vq.name, ##args); \ | |
34 | BUG(); \ | |
35 | } while (0) | |
c5f841f1 RR |
36 | /* Caller is supposed to guarantee no reentry. */ |
37 | #define START_USE(_vq) \ | |
38 | do { \ | |
39 | if ((_vq)->in_use) \ | |
9499f5e7 RR |
40 | panic("%s:in_use = %i\n", \ |
41 | (_vq)->vq.name, (_vq)->in_use); \ | |
c5f841f1 | 42 | (_vq)->in_use = __LINE__; \ |
9499f5e7 | 43 | } while (0) |
3a35ce7d | 44 | #define END_USE(_vq) \ |
97a545ab | 45 | do { BUG_ON(!(_vq)->in_use); (_vq)->in_use = 0; } while(0) |
0a8a69dd | 46 | #else |
9499f5e7 RR |
47 | #define BAD_RING(_vq, fmt, args...) \ |
48 | do { \ | |
49 | dev_err(&_vq->vq.vdev->dev, \ | |
50 | "%s:"fmt, (_vq)->vq.name, ##args); \ | |
51 | (_vq)->broken = true; \ | |
52 | } while (0) | |
0a8a69dd RR |
53 | #define START_USE(vq) |
54 | #define END_USE(vq) | |
55 | #endif | |
56 | ||
43b4f721 | 57 | struct vring_virtqueue { |
0a8a69dd RR |
58 | struct virtqueue vq; |
59 | ||
60 | /* Actual memory layout for this queue */ | |
61 | struct vring vring; | |
62 | ||
7b21e34f RR |
63 | /* Can we use weak barriers? */ |
64 | bool weak_barriers; | |
65 | ||
0a8a69dd RR |
66 | /* Other side has made a mess, don't try any more. */ |
67 | bool broken; | |
68 | ||
9fa29b9d MM |
69 | /* Host supports indirect buffers */ |
70 | bool indirect; | |
71 | ||
a5c262c5 MT |
72 | /* Host publishes avail event idx */ |
73 | bool event; | |
74 | ||
0a8a69dd RR |
75 | /* Head of free buffer list. */ |
76 | unsigned int free_head; | |
77 | /* Number we've added since last sync. */ | |
78 | unsigned int num_added; | |
79 | ||
80 | /* Last used index we've seen. */ | |
1bc4953e | 81 | u16 last_used_idx; |
0a8a69dd RR |
82 | |
83 | /* How to notify other side. FIXME: commonalize hcalls! */ | |
46f9c2b9 | 84 | bool (*notify)(struct virtqueue *vq); |
0a8a69dd RR |
85 | |
86 | #ifdef DEBUG | |
87 | /* They're supposed to lock for us. */ | |
88 | unsigned int in_use; | |
e93300b1 RR |
89 | |
90 | /* Figure out if their kicks are too delayed. */ | |
91 | bool last_add_time_valid; | |
92 | ktime_t last_add_time; | |
0a8a69dd RR |
93 | #endif |
94 | ||
95 | /* Tokens for callbacks. */ | |
96 | void *data[]; | |
97 | }; | |
98 | ||
99 | #define to_vvq(_vq) container_of(_vq, struct vring_virtqueue, vq) | |
100 | ||
00e6f3d9 MT |
101 | static struct vring_desc *alloc_indirect(struct virtqueue *_vq, |
102 | unsigned int total_sg, gfp_t gfp) | |
9fa29b9d MM |
103 | { |
104 | struct vring_desc *desc; | |
b25bd251 | 105 | unsigned int i; |
9fa29b9d | 106 | |
b92b1b89 WD |
107 | /* |
108 | * We require lowmem mappings for the descriptors because | |
109 | * otherwise virt_to_phys will give us bogus addresses in the | |
110 | * virtqueue. | |
111 | */ | |
112 | gfp &= ~(__GFP_HIGHMEM | __GFP_HIGH); | |
113 | ||
13816c76 | 114 | desc = kmalloc(total_sg * sizeof(struct vring_desc), gfp); |
9fa29b9d | 115 | if (!desc) |
b25bd251 | 116 | return NULL; |
9fa29b9d | 117 | |
b25bd251 | 118 | for (i = 0; i < total_sg; i++) |
00e6f3d9 | 119 | desc[i].next = cpu_to_virtio16(_vq->vdev, i + 1); |
b25bd251 | 120 | return desc; |
9fa29b9d MM |
121 | } |
122 | ||
13816c76 RR |
123 | static inline int virtqueue_add(struct virtqueue *_vq, |
124 | struct scatterlist *sgs[], | |
eeebf9b1 | 125 | unsigned int total_sg, |
13816c76 RR |
126 | unsigned int out_sgs, |
127 | unsigned int in_sgs, | |
128 | void *data, | |
129 | gfp_t gfp) | |
0a8a69dd RR |
130 | { |
131 | struct vring_virtqueue *vq = to_vvq(_vq); | |
13816c76 | 132 | struct scatterlist *sg; |
b25bd251 RR |
133 | struct vring_desc *desc; |
134 | unsigned int i, n, avail, descs_used, uninitialized_var(prev); | |
1fe9b6fe | 135 | int head; |
b25bd251 | 136 | bool indirect; |
0a8a69dd | 137 | |
9fa29b9d MM |
138 | START_USE(vq); |
139 | ||
0a8a69dd | 140 | BUG_ON(data == NULL); |
9fa29b9d | 141 | |
70670444 RR |
142 | if (unlikely(vq->broken)) { |
143 | END_USE(vq); | |
144 | return -EIO; | |
145 | } | |
146 | ||
e93300b1 RR |
147 | #ifdef DEBUG |
148 | { | |
149 | ktime_t now = ktime_get(); | |
150 | ||
151 | /* No kick or get, with .1 second between? Warn. */ | |
152 | if (vq->last_add_time_valid) | |
153 | WARN_ON(ktime_to_ms(ktime_sub(now, vq->last_add_time)) | |
154 | > 100); | |
155 | vq->last_add_time = now; | |
156 | vq->last_add_time_valid = true; | |
157 | } | |
158 | #endif | |
159 | ||
b25bd251 RR |
160 | BUG_ON(total_sg > vq->vring.num); |
161 | BUG_ON(total_sg == 0); | |
162 | ||
163 | head = vq->free_head; | |
164 | ||
9fa29b9d MM |
165 | /* If the host supports indirect descriptor tables, and we have multiple |
166 | * buffers, then go indirect. FIXME: tune this threshold */ | |
b25bd251 | 167 | if (vq->indirect && total_sg > 1 && vq->vq.num_free) |
00e6f3d9 | 168 | desc = alloc_indirect(_vq, total_sg, gfp); |
b25bd251 RR |
169 | else |
170 | desc = NULL; | |
171 | ||
172 | if (desc) { | |
173 | /* Use a single buffer which doesn't continue */ | |
00e6f3d9 MT |
174 | vq->vring.desc[head].flags = cpu_to_virtio16(_vq->vdev, VRING_DESC_F_INDIRECT); |
175 | vq->vring.desc[head].addr = cpu_to_virtio64(_vq->vdev, virt_to_phys(desc)); | |
b25bd251 RR |
176 | /* avoid kmemleak false positive (hidden by virt_to_phys) */ |
177 | kmemleak_ignore(desc); | |
00e6f3d9 | 178 | vq->vring.desc[head].len = cpu_to_virtio32(_vq->vdev, total_sg * sizeof(struct vring_desc)); |
b25bd251 RR |
179 | |
180 | /* Set up rest to use this indirect table. */ | |
181 | i = 0; | |
182 | descs_used = 1; | |
183 | indirect = true; | |
184 | } else { | |
185 | desc = vq->vring.desc; | |
186 | i = head; | |
187 | descs_used = total_sg; | |
188 | indirect = false; | |
9fa29b9d MM |
189 | } |
190 | ||
b25bd251 | 191 | if (vq->vq.num_free < descs_used) { |
0a8a69dd | 192 | pr_debug("Can't add buf len %i - avail = %i\n", |
b25bd251 | 193 | descs_used, vq->vq.num_free); |
44653eae RR |
194 | /* FIXME: for historical reasons, we force a notify here if |
195 | * there are outgoing parts to the buffer. Presumably the | |
196 | * host should service the ring ASAP. */ | |
13816c76 | 197 | if (out_sgs) |
44653eae | 198 | vq->notify(&vq->vq); |
0a8a69dd RR |
199 | END_USE(vq); |
200 | return -ENOSPC; | |
201 | } | |
202 | ||
203 | /* We're about to use some buffers from the free list. */ | |
b25bd251 | 204 | vq->vq.num_free -= descs_used; |
13816c76 | 205 | |
13816c76 | 206 | for (n = 0; n < out_sgs; n++) { |
eeebf9b1 | 207 | for (sg = sgs[n]; sg; sg = sg_next(sg)) { |
00e6f3d9 MT |
208 | desc[i].flags = cpu_to_virtio16(_vq->vdev, VRING_DESC_F_NEXT); |
209 | desc[i].addr = cpu_to_virtio64(_vq->vdev, sg_phys(sg)); | |
210 | desc[i].len = cpu_to_virtio32(_vq->vdev, sg->length); | |
13816c76 | 211 | prev = i; |
00e6f3d9 | 212 | i = virtio16_to_cpu(_vq->vdev, desc[i].next); |
13816c76 | 213 | } |
0a8a69dd | 214 | } |
13816c76 | 215 | for (; n < (out_sgs + in_sgs); n++) { |
eeebf9b1 | 216 | for (sg = sgs[n]; sg; sg = sg_next(sg)) { |
00e6f3d9 MT |
217 | desc[i].flags = cpu_to_virtio16(_vq->vdev, VRING_DESC_F_NEXT | VRING_DESC_F_WRITE); |
218 | desc[i].addr = cpu_to_virtio64(_vq->vdev, sg_phys(sg)); | |
219 | desc[i].len = cpu_to_virtio32(_vq->vdev, sg->length); | |
13816c76 | 220 | prev = i; |
00e6f3d9 | 221 | i = virtio16_to_cpu(_vq->vdev, desc[i].next); |
13816c76 | 222 | } |
0a8a69dd RR |
223 | } |
224 | /* Last one doesn't continue. */ | |
00e6f3d9 | 225 | desc[prev].flags &= cpu_to_virtio16(_vq->vdev, ~VRING_DESC_F_NEXT); |
0a8a69dd RR |
226 | |
227 | /* Update free pointer */ | |
b25bd251 | 228 | if (indirect) |
00e6f3d9 | 229 | vq->free_head = virtio16_to_cpu(_vq->vdev, vq->vring.desc[head].next); |
b25bd251 RR |
230 | else |
231 | vq->free_head = i; | |
0a8a69dd RR |
232 | |
233 | /* Set token. */ | |
234 | vq->data[head] = data; | |
235 | ||
236 | /* Put entry in available array (but don't update avail->idx until they | |
3b720b8c | 237 | * do sync). */ |
00e6f3d9 MT |
238 | avail = virtio16_to_cpu(_vq->vdev, vq->vring.avail->idx) & (vq->vring.num - 1); |
239 | vq->vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head); | |
0a8a69dd | 240 | |
ee7cd898 RR |
241 | /* Descriptors and available array need to be set before we expose the |
242 | * new available array entries. */ | |
a9a0fef7 | 243 | virtio_wmb(vq->weak_barriers); |
00e6f3d9 | 244 | vq->vring.avail->idx = cpu_to_virtio16(_vq->vdev, virtio16_to_cpu(_vq->vdev, vq->vring.avail->idx) + 1); |
ee7cd898 RR |
245 | vq->num_added++; |
246 | ||
5e05bf58 TH |
247 | pr_debug("Added buffer head %i to %p\n", head, vq); |
248 | END_USE(vq); | |
249 | ||
ee7cd898 RR |
250 | /* This is very unlikely, but theoretically possible. Kick |
251 | * just in case. */ | |
252 | if (unlikely(vq->num_added == (1 << 16) - 1)) | |
253 | virtqueue_kick(_vq); | |
254 | ||
98e8c6bc | 255 | return 0; |
0a8a69dd | 256 | } |
13816c76 | 257 | |
13816c76 RR |
258 | /** |
259 | * virtqueue_add_sgs - expose buffers to other end | |
260 | * @vq: the struct virtqueue we're talking about. | |
261 | * @sgs: array of terminated scatterlists. | |
262 | * @out_num: the number of scatterlists readable by other side | |
263 | * @in_num: the number of scatterlists which are writable (after readable ones) | |
264 | * @data: the token identifying the buffer. | |
265 | * @gfp: how to do memory allocations (if necessary). | |
266 | * | |
267 | * Caller must ensure we don't call this with other virtqueue operations | |
268 | * at the same time (except where noted). | |
269 | * | |
70670444 | 270 | * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO). |
13816c76 RR |
271 | */ |
272 | int virtqueue_add_sgs(struct virtqueue *_vq, | |
273 | struct scatterlist *sgs[], | |
274 | unsigned int out_sgs, | |
275 | unsigned int in_sgs, | |
276 | void *data, | |
277 | gfp_t gfp) | |
278 | { | |
eeebf9b1 | 279 | unsigned int i, total_sg = 0; |
13816c76 RR |
280 | |
281 | /* Count them first. */ | |
eeebf9b1 | 282 | for (i = 0; i < out_sgs + in_sgs; i++) { |
13816c76 RR |
283 | struct scatterlist *sg; |
284 | for (sg = sgs[i]; sg; sg = sg_next(sg)) | |
eeebf9b1 | 285 | total_sg++; |
13816c76 | 286 | } |
eeebf9b1 | 287 | return virtqueue_add(_vq, sgs, total_sg, out_sgs, in_sgs, data, gfp); |
13816c76 RR |
288 | } |
289 | EXPORT_SYMBOL_GPL(virtqueue_add_sgs); | |
290 | ||
282edb36 RR |
291 | /** |
292 | * virtqueue_add_outbuf - expose output buffers to other end | |
293 | * @vq: the struct virtqueue we're talking about. | |
eeebf9b1 RR |
294 | * @sg: scatterlist (must be well-formed and terminated!) |
295 | * @num: the number of entries in @sg readable by other side | |
282edb36 RR |
296 | * @data: the token identifying the buffer. |
297 | * @gfp: how to do memory allocations (if necessary). | |
298 | * | |
299 | * Caller must ensure we don't call this with other virtqueue operations | |
300 | * at the same time (except where noted). | |
301 | * | |
70670444 | 302 | * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO). |
282edb36 RR |
303 | */ |
304 | int virtqueue_add_outbuf(struct virtqueue *vq, | |
eeebf9b1 | 305 | struct scatterlist *sg, unsigned int num, |
282edb36 RR |
306 | void *data, |
307 | gfp_t gfp) | |
308 | { | |
eeebf9b1 | 309 | return virtqueue_add(vq, &sg, num, 1, 0, data, gfp); |
282edb36 RR |
310 | } |
311 | EXPORT_SYMBOL_GPL(virtqueue_add_outbuf); | |
312 | ||
313 | /** | |
314 | * virtqueue_add_inbuf - expose input buffers to other end | |
315 | * @vq: the struct virtqueue we're talking about. | |
eeebf9b1 RR |
316 | * @sg: scatterlist (must be well-formed and terminated!) |
317 | * @num: the number of entries in @sg writable by other side | |
282edb36 RR |
318 | * @data: the token identifying the buffer. |
319 | * @gfp: how to do memory allocations (if necessary). | |
320 | * | |
321 | * Caller must ensure we don't call this with other virtqueue operations | |
322 | * at the same time (except where noted). | |
323 | * | |
70670444 | 324 | * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO). |
282edb36 RR |
325 | */ |
326 | int virtqueue_add_inbuf(struct virtqueue *vq, | |
eeebf9b1 | 327 | struct scatterlist *sg, unsigned int num, |
282edb36 RR |
328 | void *data, |
329 | gfp_t gfp) | |
330 | { | |
eeebf9b1 | 331 | return virtqueue_add(vq, &sg, num, 0, 1, data, gfp); |
282edb36 RR |
332 | } |
333 | EXPORT_SYMBOL_GPL(virtqueue_add_inbuf); | |
334 | ||
5dfc1762 | 335 | /** |
41f0377f | 336 | * virtqueue_kick_prepare - first half of split virtqueue_kick call. |
5dfc1762 RR |
337 | * @vq: the struct virtqueue |
338 | * | |
41f0377f RR |
339 | * Instead of virtqueue_kick(), you can do: |
340 | * if (virtqueue_kick_prepare(vq)) | |
341 | * virtqueue_notify(vq); | |
5dfc1762 | 342 | * |
41f0377f RR |
343 | * This is sometimes useful because the virtqueue_kick_prepare() needs |
344 | * to be serialized, but the actual virtqueue_notify() call does not. | |
5dfc1762 | 345 | */ |
41f0377f | 346 | bool virtqueue_kick_prepare(struct virtqueue *_vq) |
0a8a69dd RR |
347 | { |
348 | struct vring_virtqueue *vq = to_vvq(_vq); | |
a5c262c5 | 349 | u16 new, old; |
41f0377f RR |
350 | bool needs_kick; |
351 | ||
0a8a69dd | 352 | START_USE(vq); |
a72caae2 JW |
353 | /* We need to expose available array entries before checking avail |
354 | * event. */ | |
a9a0fef7 | 355 | virtio_mb(vq->weak_barriers); |
0a8a69dd | 356 | |
00e6f3d9 MT |
357 | old = virtio16_to_cpu(_vq->vdev, vq->vring.avail->idx) - vq->num_added; |
358 | new = virtio16_to_cpu(_vq->vdev, vq->vring.avail->idx); | |
0a8a69dd RR |
359 | vq->num_added = 0; |
360 | ||
e93300b1 RR |
361 | #ifdef DEBUG |
362 | if (vq->last_add_time_valid) { | |
363 | WARN_ON(ktime_to_ms(ktime_sub(ktime_get(), | |
364 | vq->last_add_time)) > 100); | |
365 | } | |
366 | vq->last_add_time_valid = false; | |
367 | #endif | |
368 | ||
41f0377f | 369 | if (vq->event) { |
00e6f3d9 | 370 | needs_kick = vring_need_event(virtio16_to_cpu(_vq->vdev, vring_avail_event(&vq->vring)), |
41f0377f RR |
371 | new, old); |
372 | } else { | |
00e6f3d9 | 373 | needs_kick = !(vq->vring.used->flags & cpu_to_virtio16(_vq->vdev, VRING_USED_F_NO_NOTIFY)); |
41f0377f | 374 | } |
0a8a69dd | 375 | END_USE(vq); |
41f0377f RR |
376 | return needs_kick; |
377 | } | |
378 | EXPORT_SYMBOL_GPL(virtqueue_kick_prepare); | |
379 | ||
380 | /** | |
381 | * virtqueue_notify - second half of split virtqueue_kick call. | |
382 | * @vq: the struct virtqueue | |
383 | * | |
384 | * This does not need to be serialized. | |
5b1bf7cb HG |
385 | * |
386 | * Returns false if host notify failed or queue is broken, otherwise true. | |
41f0377f | 387 | */ |
5b1bf7cb | 388 | bool virtqueue_notify(struct virtqueue *_vq) |
41f0377f RR |
389 | { |
390 | struct vring_virtqueue *vq = to_vvq(_vq); | |
391 | ||
5b1bf7cb HG |
392 | if (unlikely(vq->broken)) |
393 | return false; | |
394 | ||
41f0377f | 395 | /* Prod other side to tell it about changes. */ |
2342d6a6 | 396 | if (!vq->notify(_vq)) { |
5b1bf7cb HG |
397 | vq->broken = true; |
398 | return false; | |
399 | } | |
400 | return true; | |
41f0377f RR |
401 | } |
402 | EXPORT_SYMBOL_GPL(virtqueue_notify); | |
403 | ||
404 | /** | |
405 | * virtqueue_kick - update after add_buf | |
406 | * @vq: the struct virtqueue | |
407 | * | |
b3087e48 | 408 | * After one or more virtqueue_add_* calls, invoke this to kick |
41f0377f RR |
409 | * the other side. |
410 | * | |
411 | * Caller must ensure we don't call this with other virtqueue | |
412 | * operations at the same time (except where noted). | |
5b1bf7cb HG |
413 | * |
414 | * Returns false if kick failed, otherwise true. | |
41f0377f | 415 | */ |
5b1bf7cb | 416 | bool virtqueue_kick(struct virtqueue *vq) |
41f0377f RR |
417 | { |
418 | if (virtqueue_kick_prepare(vq)) | |
5b1bf7cb HG |
419 | return virtqueue_notify(vq); |
420 | return true; | |
0a8a69dd | 421 | } |
7c5e9ed0 | 422 | EXPORT_SYMBOL_GPL(virtqueue_kick); |
0a8a69dd RR |
423 | |
424 | static void detach_buf(struct vring_virtqueue *vq, unsigned int head) | |
425 | { | |
426 | unsigned int i; | |
427 | ||
428 | /* Clear data ptr. */ | |
429 | vq->data[head] = NULL; | |
430 | ||
431 | /* Put back on free list: find end */ | |
432 | i = head; | |
9fa29b9d MM |
433 | |
434 | /* Free the indirect table */ | |
00e6f3d9 MT |
435 | if (vq->vring.desc[i].flags & cpu_to_virtio16(vq->vq.vdev, VRING_DESC_F_INDIRECT)) |
436 | kfree(phys_to_virt(virtio64_to_cpu(vq->vq.vdev, vq->vring.desc[i].addr))); | |
9fa29b9d | 437 | |
00e6f3d9 MT |
438 | while (vq->vring.desc[i].flags & cpu_to_virtio16(vq->vq.vdev, VRING_DESC_F_NEXT)) { |
439 | i = virtio16_to_cpu(vq->vq.vdev, vq->vring.desc[i].next); | |
06ca287d | 440 | vq->vq.num_free++; |
0a8a69dd RR |
441 | } |
442 | ||
00e6f3d9 | 443 | vq->vring.desc[i].next = cpu_to_virtio16(vq->vq.vdev, vq->free_head); |
0a8a69dd RR |
444 | vq->free_head = head; |
445 | /* Plus final descriptor */ | |
06ca287d | 446 | vq->vq.num_free++; |
0a8a69dd RR |
447 | } |
448 | ||
0a8a69dd RR |
449 | static inline bool more_used(const struct vring_virtqueue *vq) |
450 | { | |
00e6f3d9 | 451 | return vq->last_used_idx != virtio16_to_cpu(vq->vq.vdev, vq->vring.used->idx); |
0a8a69dd RR |
452 | } |
453 | ||
5dfc1762 RR |
454 | /** |
455 | * virtqueue_get_buf - get the next used buffer | |
456 | * @vq: the struct virtqueue we're talking about. | |
457 | * @len: the length written into the buffer | |
458 | * | |
459 | * If the driver wrote data into the buffer, @len will be set to the | |
460 | * amount written. This means you don't need to clear the buffer | |
461 | * beforehand to ensure there's no data leakage in the case of short | |
462 | * writes. | |
463 | * | |
464 | * Caller must ensure we don't call this with other virtqueue | |
465 | * operations at the same time (except where noted). | |
466 | * | |
467 | * Returns NULL if there are no used buffers, or the "data" token | |
b3087e48 | 468 | * handed to virtqueue_add_*(). |
5dfc1762 | 469 | */ |
7c5e9ed0 | 470 | void *virtqueue_get_buf(struct virtqueue *_vq, unsigned int *len) |
0a8a69dd RR |
471 | { |
472 | struct vring_virtqueue *vq = to_vvq(_vq); | |
473 | void *ret; | |
474 | unsigned int i; | |
3b720b8c | 475 | u16 last_used; |
0a8a69dd RR |
476 | |
477 | START_USE(vq); | |
478 | ||
5ef82752 RR |
479 | if (unlikely(vq->broken)) { |
480 | END_USE(vq); | |
481 | return NULL; | |
482 | } | |
483 | ||
0a8a69dd RR |
484 | if (!more_used(vq)) { |
485 | pr_debug("No more buffers in queue\n"); | |
486 | END_USE(vq); | |
487 | return NULL; | |
488 | } | |
489 | ||
2d61ba95 | 490 | /* Only get used array entries after they have been exposed by host. */ |
a9a0fef7 | 491 | virtio_rmb(vq->weak_barriers); |
2d61ba95 | 492 | |
3b720b8c | 493 | last_used = (vq->last_used_idx & (vq->vring.num - 1)); |
00e6f3d9 MT |
494 | i = virtio32_to_cpu(_vq->vdev, vq->vring.used->ring[last_used].id); |
495 | *len = virtio32_to_cpu(_vq->vdev, vq->vring.used->ring[last_used].len); | |
0a8a69dd RR |
496 | |
497 | if (unlikely(i >= vq->vring.num)) { | |
498 | BAD_RING(vq, "id %u out of range\n", i); | |
499 | return NULL; | |
500 | } | |
501 | if (unlikely(!vq->data[i])) { | |
502 | BAD_RING(vq, "id %u is not a head!\n", i); | |
503 | return NULL; | |
504 | } | |
505 | ||
506 | /* detach_buf clears data, so grab it now. */ | |
507 | ret = vq->data[i]; | |
508 | detach_buf(vq, i); | |
509 | vq->last_used_idx++; | |
a5c262c5 MT |
510 | /* If we expect an interrupt for the next entry, tell host |
511 | * by writing event index and flush out the write before | |
512 | * the read in the next get_buf call. */ | |
00e6f3d9 MT |
513 | if (!(vq->vring.avail->flags & cpu_to_virtio16(_vq->vdev, VRING_AVAIL_F_NO_INTERRUPT))) { |
514 | vring_used_event(&vq->vring) = cpu_to_virtio16(_vq->vdev, vq->last_used_idx); | |
a9a0fef7 | 515 | virtio_mb(vq->weak_barriers); |
a5c262c5 MT |
516 | } |
517 | ||
e93300b1 RR |
518 | #ifdef DEBUG |
519 | vq->last_add_time_valid = false; | |
520 | #endif | |
521 | ||
0a8a69dd RR |
522 | END_USE(vq); |
523 | return ret; | |
524 | } | |
7c5e9ed0 | 525 | EXPORT_SYMBOL_GPL(virtqueue_get_buf); |
0a8a69dd | 526 | |
5dfc1762 RR |
527 | /** |
528 | * virtqueue_disable_cb - disable callbacks | |
529 | * @vq: the struct virtqueue we're talking about. | |
530 | * | |
531 | * Note that this is not necessarily synchronous, hence unreliable and only | |
532 | * useful as an optimization. | |
533 | * | |
534 | * Unlike other operations, this need not be serialized. | |
535 | */ | |
7c5e9ed0 | 536 | void virtqueue_disable_cb(struct virtqueue *_vq) |
18445c4d RR |
537 | { |
538 | struct vring_virtqueue *vq = to_vvq(_vq); | |
539 | ||
00e6f3d9 | 540 | vq->vring.avail->flags |= cpu_to_virtio16(_vq->vdev, VRING_AVAIL_F_NO_INTERRUPT); |
18445c4d | 541 | } |
7c5e9ed0 | 542 | EXPORT_SYMBOL_GPL(virtqueue_disable_cb); |
18445c4d | 543 | |
5dfc1762 | 544 | /** |
cc229884 | 545 | * virtqueue_enable_cb_prepare - restart callbacks after disable_cb |
5dfc1762 RR |
546 | * @vq: the struct virtqueue we're talking about. |
547 | * | |
cc229884 MT |
548 | * This re-enables callbacks; it returns current queue state |
549 | * in an opaque unsigned value. This value should be later tested by | |
550 | * virtqueue_poll, to detect a possible race between the driver checking for | |
551 | * more work, and enabling callbacks. | |
5dfc1762 RR |
552 | * |
553 | * Caller must ensure we don't call this with other virtqueue | |
554 | * operations at the same time (except where noted). | |
555 | */ | |
cc229884 | 556 | unsigned virtqueue_enable_cb_prepare(struct virtqueue *_vq) |
0a8a69dd RR |
557 | { |
558 | struct vring_virtqueue *vq = to_vvq(_vq); | |
cc229884 | 559 | u16 last_used_idx; |
0a8a69dd RR |
560 | |
561 | START_USE(vq); | |
0a8a69dd RR |
562 | |
563 | /* We optimistically turn back on interrupts, then check if there was | |
564 | * more to do. */ | |
a5c262c5 MT |
565 | /* Depending on the VIRTIO_RING_F_EVENT_IDX feature, we need to |
566 | * either clear the flags bit or point the event index at the next | |
567 | * entry. Always do both to keep code simple. */ | |
00e6f3d9 MT |
568 | vq->vring.avail->flags &= cpu_to_virtio16(_vq->vdev, ~VRING_AVAIL_F_NO_INTERRUPT); |
569 | vring_used_event(&vq->vring) = cpu_to_virtio16(_vq->vdev, last_used_idx = vq->last_used_idx); | |
cc229884 MT |
570 | END_USE(vq); |
571 | return last_used_idx; | |
572 | } | |
573 | EXPORT_SYMBOL_GPL(virtqueue_enable_cb_prepare); | |
574 | ||
575 | /** | |
576 | * virtqueue_poll - query pending used buffers | |
577 | * @vq: the struct virtqueue we're talking about. | |
578 | * @last_used_idx: virtqueue state (from call to virtqueue_enable_cb_prepare). | |
579 | * | |
580 | * Returns "true" if there are pending used buffers in the queue. | |
581 | * | |
582 | * This does not need to be serialized. | |
583 | */ | |
584 | bool virtqueue_poll(struct virtqueue *_vq, unsigned last_used_idx) | |
585 | { | |
586 | struct vring_virtqueue *vq = to_vvq(_vq); | |
587 | ||
a9a0fef7 | 588 | virtio_mb(vq->weak_barriers); |
00e6f3d9 | 589 | return (u16)last_used_idx != virtio16_to_cpu(_vq->vdev, vq->vring.used->idx); |
cc229884 MT |
590 | } |
591 | EXPORT_SYMBOL_GPL(virtqueue_poll); | |
0a8a69dd | 592 | |
cc229884 MT |
593 | /** |
594 | * virtqueue_enable_cb - restart callbacks after disable_cb. | |
595 | * @vq: the struct virtqueue we're talking about. | |
596 | * | |
597 | * This re-enables callbacks; it returns "false" if there are pending | |
598 | * buffers in the queue, to detect a possible race between the driver | |
599 | * checking for more work, and enabling callbacks. | |
600 | * | |
601 | * Caller must ensure we don't call this with other virtqueue | |
602 | * operations at the same time (except where noted). | |
603 | */ | |
604 | bool virtqueue_enable_cb(struct virtqueue *_vq) | |
605 | { | |
606 | unsigned last_used_idx = virtqueue_enable_cb_prepare(_vq); | |
607 | return !virtqueue_poll(_vq, last_used_idx); | |
0a8a69dd | 608 | } |
7c5e9ed0 | 609 | EXPORT_SYMBOL_GPL(virtqueue_enable_cb); |
0a8a69dd | 610 | |
5dfc1762 RR |
611 | /** |
612 | * virtqueue_enable_cb_delayed - restart callbacks after disable_cb. | |
613 | * @vq: the struct virtqueue we're talking about. | |
614 | * | |
615 | * This re-enables callbacks but hints to the other side to delay | |
616 | * interrupts until most of the available buffers have been processed; | |
617 | * it returns "false" if there are many pending buffers in the queue, | |
618 | * to detect a possible race between the driver checking for more work, | |
619 | * and enabling callbacks. | |
620 | * | |
621 | * Caller must ensure we don't call this with other virtqueue | |
622 | * operations at the same time (except where noted). | |
623 | */ | |
7ab358c2 MT |
624 | bool virtqueue_enable_cb_delayed(struct virtqueue *_vq) |
625 | { | |
626 | struct vring_virtqueue *vq = to_vvq(_vq); | |
627 | u16 bufs; | |
628 | ||
629 | START_USE(vq); | |
630 | ||
631 | /* We optimistically turn back on interrupts, then check if there was | |
632 | * more to do. */ | |
633 | /* Depending on the VIRTIO_RING_F_USED_EVENT_IDX feature, we need to | |
634 | * either clear the flags bit or point the event index at the next | |
635 | * entry. Always do both to keep code simple. */ | |
00e6f3d9 | 636 | vq->vring.avail->flags &= cpu_to_virtio16(_vq->vdev, ~VRING_AVAIL_F_NO_INTERRUPT); |
7ab358c2 | 637 | /* TODO: tune this threshold */ |
00e6f3d9 MT |
638 | bufs = (u16)(virtio16_to_cpu(_vq->vdev, vq->vring.avail->idx) - vq->last_used_idx) * 3 / 4; |
639 | vring_used_event(&vq->vring) = cpu_to_virtio16(_vq->vdev, vq->last_used_idx + bufs); | |
a9a0fef7 | 640 | virtio_mb(vq->weak_barriers); |
00e6f3d9 | 641 | if (unlikely((u16)(virtio16_to_cpu(_vq->vdev, vq->vring.used->idx) - vq->last_used_idx) > bufs)) { |
7ab358c2 MT |
642 | END_USE(vq); |
643 | return false; | |
644 | } | |
645 | ||
646 | END_USE(vq); | |
647 | return true; | |
648 | } | |
649 | EXPORT_SYMBOL_GPL(virtqueue_enable_cb_delayed); | |
650 | ||
5dfc1762 RR |
651 | /** |
652 | * virtqueue_detach_unused_buf - detach first unused buffer | |
653 | * @vq: the struct virtqueue we're talking about. | |
654 | * | |
b3087e48 | 655 | * Returns NULL or the "data" token handed to virtqueue_add_*(). |
5dfc1762 RR |
656 | * This is not valid on an active queue; it is useful only for device |
657 | * shutdown. | |
658 | */ | |
7c5e9ed0 | 659 | void *virtqueue_detach_unused_buf(struct virtqueue *_vq) |
c021eac4 SM |
660 | { |
661 | struct vring_virtqueue *vq = to_vvq(_vq); | |
662 | unsigned int i; | |
663 | void *buf; | |
664 | ||
665 | START_USE(vq); | |
666 | ||
667 | for (i = 0; i < vq->vring.num; i++) { | |
668 | if (!vq->data[i]) | |
669 | continue; | |
670 | /* detach_buf clears data, so grab it now. */ | |
671 | buf = vq->data[i]; | |
672 | detach_buf(vq, i); | |
00e6f3d9 | 673 | vq->vring.avail->idx = cpu_to_virtio16(_vq->vdev, virtio16_to_cpu(_vq->vdev, vq->vring.avail->idx) - 1); |
c021eac4 SM |
674 | END_USE(vq); |
675 | return buf; | |
676 | } | |
677 | /* That should have freed everything. */ | |
06ca287d | 678 | BUG_ON(vq->vq.num_free != vq->vring.num); |
c021eac4 SM |
679 | |
680 | END_USE(vq); | |
681 | return NULL; | |
682 | } | |
7c5e9ed0 | 683 | EXPORT_SYMBOL_GPL(virtqueue_detach_unused_buf); |
c021eac4 | 684 | |
0a8a69dd RR |
685 | irqreturn_t vring_interrupt(int irq, void *_vq) |
686 | { | |
687 | struct vring_virtqueue *vq = to_vvq(_vq); | |
688 | ||
689 | if (!more_used(vq)) { | |
690 | pr_debug("virtqueue interrupt with no work for %p\n", vq); | |
691 | return IRQ_NONE; | |
692 | } | |
693 | ||
694 | if (unlikely(vq->broken)) | |
695 | return IRQ_HANDLED; | |
696 | ||
697 | pr_debug("virtqueue callback for %p (%p)\n", vq, vq->vq.callback); | |
18445c4d RR |
698 | if (vq->vq.callback) |
699 | vq->vq.callback(&vq->vq); | |
0a8a69dd RR |
700 | |
701 | return IRQ_HANDLED; | |
702 | } | |
c6fd4701 | 703 | EXPORT_SYMBOL_GPL(vring_interrupt); |
0a8a69dd | 704 | |
17bb6d40 JW |
705 | struct virtqueue *vring_new_virtqueue(unsigned int index, |
706 | unsigned int num, | |
87c7d57c | 707 | unsigned int vring_align, |
0a8a69dd | 708 | struct virtio_device *vdev, |
7b21e34f | 709 | bool weak_barriers, |
0a8a69dd | 710 | void *pages, |
46f9c2b9 | 711 | bool (*notify)(struct virtqueue *), |
9499f5e7 RR |
712 | void (*callback)(struct virtqueue *), |
713 | const char *name) | |
0a8a69dd RR |
714 | { |
715 | struct vring_virtqueue *vq; | |
716 | unsigned int i; | |
717 | ||
42b36cc0 RR |
718 | /* We assume num is a power of 2. */ |
719 | if (num & (num - 1)) { | |
720 | dev_warn(&vdev->dev, "Bad virtqueue length %u\n", num); | |
721 | return NULL; | |
722 | } | |
723 | ||
0a8a69dd RR |
724 | vq = kmalloc(sizeof(*vq) + sizeof(void *)*num, GFP_KERNEL); |
725 | if (!vq) | |
726 | return NULL; | |
727 | ||
87c7d57c | 728 | vring_init(&vq->vring, num, pages, vring_align); |
0a8a69dd RR |
729 | vq->vq.callback = callback; |
730 | vq->vq.vdev = vdev; | |
9499f5e7 | 731 | vq->vq.name = name; |
06ca287d RR |
732 | vq->vq.num_free = num; |
733 | vq->vq.index = index; | |
0a8a69dd | 734 | vq->notify = notify; |
7b21e34f | 735 | vq->weak_barriers = weak_barriers; |
0a8a69dd RR |
736 | vq->broken = false; |
737 | vq->last_used_idx = 0; | |
738 | vq->num_added = 0; | |
9499f5e7 | 739 | list_add_tail(&vq->vq.list, &vdev->vqs); |
0a8a69dd RR |
740 | #ifdef DEBUG |
741 | vq->in_use = false; | |
e93300b1 | 742 | vq->last_add_time_valid = false; |
0a8a69dd RR |
743 | #endif |
744 | ||
9fa29b9d | 745 | vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC); |
a5c262c5 | 746 | vq->event = virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX); |
9fa29b9d | 747 | |
0a8a69dd RR |
748 | /* No callback? Tell other side not to bother us. */ |
749 | if (!callback) | |
00e6f3d9 | 750 | vq->vring.avail->flags |= cpu_to_virtio16(vdev, VRING_AVAIL_F_NO_INTERRUPT); |
0a8a69dd RR |
751 | |
752 | /* Put everything in free lists. */ | |
0a8a69dd | 753 | vq->free_head = 0; |
3b870624 | 754 | for (i = 0; i < num-1; i++) { |
00e6f3d9 | 755 | vq->vring.desc[i].next = cpu_to_virtio16(vdev, i + 1); |
3b870624 AS |
756 | vq->data[i] = NULL; |
757 | } | |
758 | vq->data[i] = NULL; | |
0a8a69dd RR |
759 | |
760 | return &vq->vq; | |
761 | } | |
c6fd4701 | 762 | EXPORT_SYMBOL_GPL(vring_new_virtqueue); |
0a8a69dd RR |
763 | |
764 | void vring_del_virtqueue(struct virtqueue *vq) | |
765 | { | |
9499f5e7 | 766 | list_del(&vq->list); |
0a8a69dd RR |
767 | kfree(to_vvq(vq)); |
768 | } | |
c6fd4701 | 769 | EXPORT_SYMBOL_GPL(vring_del_virtqueue); |
0a8a69dd | 770 | |
e34f8725 RR |
771 | /* Manipulates transport-specific feature bits. */ |
772 | void vring_transport_features(struct virtio_device *vdev) | |
773 | { | |
774 | unsigned int i; | |
775 | ||
776 | for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++) { | |
777 | switch (i) { | |
9fa29b9d MM |
778 | case VIRTIO_RING_F_INDIRECT_DESC: |
779 | break; | |
a5c262c5 MT |
780 | case VIRTIO_RING_F_EVENT_IDX: |
781 | break; | |
747ae34a MT |
782 | case VIRTIO_F_VERSION_1: |
783 | break; | |
e34f8725 RR |
784 | default: |
785 | /* We don't understand this bit. */ | |
e16e12be | 786 | __virtio_clear_bit(vdev, i); |
e34f8725 RR |
787 | } |
788 | } | |
789 | } | |
790 | EXPORT_SYMBOL_GPL(vring_transport_features); | |
791 | ||
5dfc1762 RR |
792 | /** |
793 | * virtqueue_get_vring_size - return the size of the virtqueue's vring | |
794 | * @vq: the struct virtqueue containing the vring of interest. | |
795 | * | |
796 | * Returns the size of the vring. This is mainly used for boasting to | |
797 | * userspace. Unlike other operations, this need not be serialized. | |
798 | */ | |
8f9f4668 RJ |
799 | unsigned int virtqueue_get_vring_size(struct virtqueue *_vq) |
800 | { | |
801 | ||
802 | struct vring_virtqueue *vq = to_vvq(_vq); | |
803 | ||
804 | return vq->vring.num; | |
805 | } | |
806 | EXPORT_SYMBOL_GPL(virtqueue_get_vring_size); | |
807 | ||
b3b32c94 HG |
808 | bool virtqueue_is_broken(struct virtqueue *_vq) |
809 | { | |
810 | struct vring_virtqueue *vq = to_vvq(_vq); | |
811 | ||
812 | return vq->broken; | |
813 | } | |
814 | EXPORT_SYMBOL_GPL(virtqueue_is_broken); | |
815 | ||
e2dcdfe9 RR |
816 | /* |
817 | * This should prevent the device from being used, allowing drivers to | |
818 | * recover. You may need to grab appropriate locks to flush. | |
819 | */ | |
820 | void virtio_break_device(struct virtio_device *dev) | |
821 | { | |
822 | struct virtqueue *_vq; | |
823 | ||
824 | list_for_each_entry(_vq, &dev->vqs, list) { | |
825 | struct vring_virtqueue *vq = to_vvq(_vq); | |
826 | vq->broken = true; | |
827 | } | |
828 | } | |
829 | EXPORT_SYMBOL_GPL(virtio_break_device); | |
830 | ||
89062652 CH |
831 | void *virtqueue_get_avail(struct virtqueue *_vq) |
832 | { | |
833 | struct vring_virtqueue *vq = to_vvq(_vq); | |
834 | ||
835 | return vq->vring.avail; | |
836 | } | |
837 | EXPORT_SYMBOL_GPL(virtqueue_get_avail); | |
838 | ||
839 | void *virtqueue_get_used(struct virtqueue *_vq) | |
840 | { | |
841 | struct vring_virtqueue *vq = to_vvq(_vq); | |
842 | ||
843 | return vq->vring.used; | |
844 | } | |
845 | EXPORT_SYMBOL_GPL(virtqueue_get_used); | |
846 | ||
c6fd4701 | 847 | MODULE_LICENSE("GPL"); |