blob: 36bb6a613728397309e64ca2031cbf96bb95cced [file] [log] [blame]
Rusty Russell0a8a69d2007-10-22 11:03:40 +10001/* Virtio ring implementation.
2 *
3 * Copyright 2007 Rusty Russell IBM Corporation
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19#include <linux/virtio.h>
20#include <linux/virtio_ring.h>
Rusty Russelle34f8722008-07-25 12:06:13 -050021#include <linux/virtio_config.h>
Rusty Russell0a8a69d2007-10-22 11:03:40 +100022#include <linux/device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Paul Gortmakerb5a2c4f2011-07-03 16:20:30 -040024#include <linux/module.h>
Rusty Russell0a8a69d2007-10-22 11:03:40 +100025
Michael S. Tsirkind57ed952010-01-28 00:42:23 +020026/* virtio guest is communicating with a virtual "device" that actually runs on
27 * a host processor. Memory barriers are used to control SMP effects. */
28#ifdef CONFIG_SMP
29/* Where possible, use SMP barriers which are more lightweight than mandatory
30 * barriers, because mandatory barriers control MMIO effects on accesses
Rusty Russell7b21e342012-01-12 15:44:42 +103031 * through relaxed memory I/O windows (which virtio-pci does not use). */
32#define virtio_mb(vq) \
33 do { if ((vq)->weak_barriers) smp_mb(); else mb(); } while(0)
34#define virtio_rmb(vq) \
35 do { if ((vq)->weak_barriers) smp_rmb(); else rmb(); } while(0)
36#define virtio_wmb(vq) \
37 do { if ((vq)->weak_barriers) smp_rmb(); else rmb(); } while(0)
Michael S. Tsirkind57ed952010-01-28 00:42:23 +020038#else
39/* We must force memory ordering even if guest is UP since host could be
40 * running on another CPU, but SMP barriers are defined to barrier() in that
41 * configuration. So fall back to mandatory barriers instead. */
Rusty Russell7b21e342012-01-12 15:44:42 +103042#define virtio_mb(vq) mb()
43#define virtio_rmb(vq) rmb()
44#define virtio_wmb(vq) wmb()
Michael S. Tsirkind57ed952010-01-28 00:42:23 +020045#endif
46
Rusty Russell0a8a69d2007-10-22 11:03:40 +100047#ifdef DEBUG
48/* For development, we want to crash whenever the ring is screwed. */
Rusty Russell9499f5e2009-06-12 22:16:35 -060049#define BAD_RING(_vq, fmt, args...) \
50 do { \
51 dev_err(&(_vq)->vq.vdev->dev, \
52 "%s:"fmt, (_vq)->vq.name, ##args); \
53 BUG(); \
54 } while (0)
Rusty Russellc5f841f2009-03-30 21:55:22 -060055/* Caller is supposed to guarantee no reentry. */
56#define START_USE(_vq) \
57 do { \
58 if ((_vq)->in_use) \
Rusty Russell9499f5e2009-06-12 22:16:35 -060059 panic("%s:in_use = %i\n", \
60 (_vq)->vq.name, (_vq)->in_use); \
Rusty Russellc5f841f2009-03-30 21:55:22 -060061 (_vq)->in_use = __LINE__; \
Rusty Russell9499f5e2009-06-12 22:16:35 -060062 } while (0)
Roel Kluin3a35ce72009-01-22 16:42:57 +010063#define END_USE(_vq) \
Rusty Russell97a545a2010-02-24 14:22:22 -060064 do { BUG_ON(!(_vq)->in_use); (_vq)->in_use = 0; } while(0)
Rusty Russell0a8a69d2007-10-22 11:03:40 +100065#else
Rusty Russell9499f5e2009-06-12 22:16:35 -060066#define BAD_RING(_vq, fmt, args...) \
67 do { \
68 dev_err(&_vq->vq.vdev->dev, \
69 "%s:"fmt, (_vq)->vq.name, ##args); \
70 (_vq)->broken = true; \
71 } while (0)
Rusty Russell0a8a69d2007-10-22 11:03:40 +100072#define START_USE(vq)
73#define END_USE(vq)
74#endif
75
76struct vring_virtqueue
77{
78 struct virtqueue vq;
79
80 /* Actual memory layout for this queue */
81 struct vring vring;
82
Rusty Russell7b21e342012-01-12 15:44:42 +103083 /* Can we use weak barriers? */
84 bool weak_barriers;
85
Rusty Russell0a8a69d2007-10-22 11:03:40 +100086 /* Other side has made a mess, don't try any more. */
87 bool broken;
88
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +010089 /* Host supports indirect buffers */
90 bool indirect;
91
Michael S. Tsirkina5c262c2011-05-20 02:10:44 +030092 /* Host publishes avail event idx */
93 bool event;
94
Rusty Russell0a8a69d2007-10-22 11:03:40 +100095 /* Number of free buffers */
96 unsigned int num_free;
97 /* Head of free buffer list. */
98 unsigned int free_head;
99 /* Number we've added since last sync. */
100 unsigned int num_added;
101
102 /* Last used index we've seen. */
Anthony Liguori1bc49532007-11-07 15:49:24 -0600103 u16 last_used_idx;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000104
105 /* How to notify other side. FIXME: commonalize hcalls! */
106 void (*notify)(struct virtqueue *vq);
107
108#ifdef DEBUG
109 /* They're supposed to lock for us. */
110 unsigned int in_use;
111#endif
112
113 /* Tokens for callbacks. */
114 void *data[];
115};
116
117#define to_vvq(_vq) container_of(_vq, struct vring_virtqueue, vq)
118
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100119/* Set up an indirect table of descriptors and add it to the queue. */
120static int vring_add_indirect(struct vring_virtqueue *vq,
121 struct scatterlist sg[],
122 unsigned int out,
Michael S. Tsirkinbbd603e2010-04-29 17:26:37 +0300123 unsigned int in,
124 gfp_t gfp)
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100125{
126 struct vring_desc *desc;
127 unsigned head;
128 int i;
129
Michael S. Tsirkinbbd603e2010-04-29 17:26:37 +0300130 desc = kmalloc((out + in) * sizeof(struct vring_desc), gfp);
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100131 if (!desc)
Michael S. Tsirkin686d3632010-06-10 18:16:11 +0300132 return -ENOMEM;
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100133
134 /* Transfer entries from the sg list into the indirect page */
135 for (i = 0; i < out; i++) {
136 desc[i].flags = VRING_DESC_F_NEXT;
137 desc[i].addr = sg_phys(sg);
138 desc[i].len = sg->length;
139 desc[i].next = i+1;
140 sg++;
141 }
142 for (; i < (out + in); i++) {
143 desc[i].flags = VRING_DESC_F_NEXT|VRING_DESC_F_WRITE;
144 desc[i].addr = sg_phys(sg);
145 desc[i].len = sg->length;
146 desc[i].next = i+1;
147 sg++;
148 }
149
150 /* Last one doesn't continue. */
151 desc[i-1].flags &= ~VRING_DESC_F_NEXT;
152 desc[i-1].next = 0;
153
154 /* We're about to use a buffer */
155 vq->num_free--;
156
157 /* Use a single buffer which doesn't continue */
158 head = vq->free_head;
159 vq->vring.desc[head].flags = VRING_DESC_F_INDIRECT;
160 vq->vring.desc[head].addr = virt_to_phys(desc);
161 vq->vring.desc[head].len = i * sizeof(struct vring_desc);
162
163 /* Update free pointer */
164 vq->free_head = vq->vring.desc[head].next;
165
166 return head;
167}
168
Rusty Russell5dfc1762012-01-12 15:44:42 +1030169/**
Rusty Russellf96fde42012-01-12 15:44:42 +1030170 * virtqueue_add_buf - expose buffer to other end
Rusty Russell5dfc1762012-01-12 15:44:42 +1030171 * @vq: the struct virtqueue we're talking about.
172 * @sg: the description of the buffer(s).
173 * @out_num: the number of sg readable by other side
174 * @in_num: the number of sg which are writable (after readable ones)
175 * @data: the token identifying the buffer.
176 * @gfp: how to do memory allocations (if necessary).
177 *
178 * Caller must ensure we don't call this with other virtqueue operations
179 * at the same time (except where noted).
180 *
181 * Returns remaining capacity of queue or a negative error
182 * (ie. ENOSPC). Note that it only really makes sense to treat all
183 * positive return values as "available": indirect buffers mean that
184 * we can put an entire sg[] array inside a single queue entry.
185 */
Rusty Russellf96fde42012-01-12 15:44:42 +1030186int virtqueue_add_buf(struct virtqueue *_vq,
187 struct scatterlist sg[],
188 unsigned int out,
189 unsigned int in,
190 void *data,
191 gfp_t gfp)
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000192{
193 struct vring_virtqueue *vq = to_vvq(_vq);
Michael S. Tsirkin1fe9b6f2010-07-26 16:55:30 +0930194 unsigned int i, avail, uninitialized_var(prev);
195 int head;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000196
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100197 START_USE(vq);
198
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000199 BUG_ON(data == NULL);
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100200
201 /* If the host supports indirect descriptor tables, and we have multiple
202 * buffers, then go indirect. FIXME: tune this threshold */
203 if (vq->indirect && (out + in) > 1 && vq->num_free) {
Michael S. Tsirkinbbd603e2010-04-29 17:26:37 +0300204 head = vring_add_indirect(vq, sg, out, in, gfp);
Michael S. Tsirkin1fe9b6f2010-07-26 16:55:30 +0930205 if (likely(head >= 0))
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100206 goto add_head;
207 }
208
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000209 BUG_ON(out + in > vq->vring.num);
210 BUG_ON(out + in == 0);
211
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000212 if (vq->num_free < out + in) {
213 pr_debug("Can't add buf len %i - avail = %i\n",
214 out + in, vq->num_free);
Rusty Russell44653ea2008-07-25 12:06:04 -0500215 /* FIXME: for historical reasons, we force a notify here if
216 * there are outgoing parts to the buffer. Presumably the
217 * host should service the ring ASAP. */
218 if (out)
219 vq->notify(&vq->vq);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000220 END_USE(vq);
221 return -ENOSPC;
222 }
223
224 /* We're about to use some buffers from the free list. */
225 vq->num_free -= out + in;
226
227 head = vq->free_head;
228 for (i = vq->free_head; out; i = vq->vring.desc[i].next, out--) {
229 vq->vring.desc[i].flags = VRING_DESC_F_NEXT;
Rusty Russell15f9c892008-02-04 23:50:05 -0500230 vq->vring.desc[i].addr = sg_phys(sg);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000231 vq->vring.desc[i].len = sg->length;
232 prev = i;
233 sg++;
234 }
235 for (; in; i = vq->vring.desc[i].next, in--) {
236 vq->vring.desc[i].flags = VRING_DESC_F_NEXT|VRING_DESC_F_WRITE;
Rusty Russell15f9c892008-02-04 23:50:05 -0500237 vq->vring.desc[i].addr = sg_phys(sg);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000238 vq->vring.desc[i].len = sg->length;
239 prev = i;
240 sg++;
241 }
242 /* Last one doesn't continue. */
243 vq->vring.desc[prev].flags &= ~VRING_DESC_F_NEXT;
244
245 /* Update free pointer */
246 vq->free_head = i;
247
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100248add_head:
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000249 /* Set token. */
250 vq->data[head] = data;
251
252 /* Put entry in available array (but don't update avail->idx until they
Rusty Russell3b720b82012-01-12 15:44:43 +1030253 * do sync). */
Rusty Russellee7cd892012-01-12 15:44:43 +1030254 avail = (vq->vring.avail->idx & (vq->vring.num-1));
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000255 vq->vring.avail->ring[avail] = head;
256
Rusty Russellee7cd892012-01-12 15:44:43 +1030257 /* Descriptors and available array need to be set before we expose the
258 * new available array entries. */
259 virtio_wmb(vq);
260 vq->vring.avail->idx++;
261 vq->num_added++;
262
263 /* This is very unlikely, but theoretically possible. Kick
264 * just in case. */
265 if (unlikely(vq->num_added == (1 << 16) - 1))
266 virtqueue_kick(_vq);
267
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000268 pr_debug("Added buffer head %i to %p\n", head, vq);
269 END_USE(vq);
Rusty Russell3c1b27d2009-09-23 22:26:31 -0600270
Rusty Russell3c1b27d2009-09-23 22:26:31 -0600271 return vq->num_free;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000272}
Rusty Russellf96fde42012-01-12 15:44:42 +1030273EXPORT_SYMBOL_GPL(virtqueue_add_buf);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000274
Rusty Russell5dfc1762012-01-12 15:44:42 +1030275/**
Rusty Russell41f03772012-01-12 15:44:43 +1030276 * virtqueue_kick_prepare - first half of split virtqueue_kick call.
Rusty Russell5dfc1762012-01-12 15:44:42 +1030277 * @vq: the struct virtqueue
278 *
Rusty Russell41f03772012-01-12 15:44:43 +1030279 * Instead of virtqueue_kick(), you can do:
280 * if (virtqueue_kick_prepare(vq))
281 * virtqueue_notify(vq);
Rusty Russell5dfc1762012-01-12 15:44:42 +1030282 *
Rusty Russell41f03772012-01-12 15:44:43 +1030283 * This is sometimes useful because the virtqueue_kick_prepare() needs
284 * to be serialized, but the actual virtqueue_notify() call does not.
Rusty Russell5dfc1762012-01-12 15:44:42 +1030285 */
Rusty Russell41f03772012-01-12 15:44:43 +1030286bool virtqueue_kick_prepare(struct virtqueue *_vq)
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000287{
288 struct vring_virtqueue *vq = to_vvq(_vq);
Michael S. Tsirkina5c262c2011-05-20 02:10:44 +0300289 u16 new, old;
Rusty Russell41f03772012-01-12 15:44:43 +1030290 bool needs_kick;
291
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000292 START_USE(vq);
293 /* Descriptors and available array need to be set before we expose the
294 * new available array entries. */
Rusty Russell7b21e342012-01-12 15:44:42 +1030295 virtio_wmb(vq);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000296
Rusty Russellee7cd892012-01-12 15:44:43 +1030297 old = vq->vring.avail->idx - vq->num_added;
298 new = vq->vring.avail->idx;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000299 vq->num_added = 0;
300
Rusty Russell41f03772012-01-12 15:44:43 +1030301 if (vq->event) {
302 needs_kick = vring_need_event(vring_avail_event(&vq->vring),
303 new, old);
304 } else {
305 needs_kick = !(vq->vring.used->flags & VRING_USED_F_NO_NOTIFY);
306 }
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000307 END_USE(vq);
Rusty Russell41f03772012-01-12 15:44:43 +1030308 return needs_kick;
309}
310EXPORT_SYMBOL_GPL(virtqueue_kick_prepare);
311
312/**
313 * virtqueue_notify - second half of split virtqueue_kick call.
314 * @vq: the struct virtqueue
315 *
316 * This does not need to be serialized.
317 */
318void virtqueue_notify(struct virtqueue *_vq)
319{
320 struct vring_virtqueue *vq = to_vvq(_vq);
321
322 /* Prod other side to tell it about changes. */
323 vq->notify(_vq);
324}
325EXPORT_SYMBOL_GPL(virtqueue_notify);
326
327/**
328 * virtqueue_kick - update after add_buf
329 * @vq: the struct virtqueue
330 *
331 * After one or more virtqueue_add_buf calls, invoke this to kick
332 * the other side.
333 *
334 * Caller must ensure we don't call this with other virtqueue
335 * operations at the same time (except where noted).
336 */
337void virtqueue_kick(struct virtqueue *vq)
338{
339 if (virtqueue_kick_prepare(vq))
340 virtqueue_notify(vq);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000341}
Michael S. Tsirkin7c5e9ed2010-04-12 16:19:07 +0300342EXPORT_SYMBOL_GPL(virtqueue_kick);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000343
344static void detach_buf(struct vring_virtqueue *vq, unsigned int head)
345{
346 unsigned int i;
347
348 /* Clear data ptr. */
349 vq->data[head] = NULL;
350
351 /* Put back on free list: find end */
352 i = head;
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100353
354 /* Free the indirect table */
355 if (vq->vring.desc[i].flags & VRING_DESC_F_INDIRECT)
356 kfree(phys_to_virt(vq->vring.desc[i].addr));
357
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000358 while (vq->vring.desc[i].flags & VRING_DESC_F_NEXT) {
359 i = vq->vring.desc[i].next;
360 vq->num_free++;
361 }
362
363 vq->vring.desc[i].next = vq->free_head;
364 vq->free_head = head;
365 /* Plus final descriptor */
366 vq->num_free++;
367}
368
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000369static inline bool more_used(const struct vring_virtqueue *vq)
370{
371 return vq->last_used_idx != vq->vring.used->idx;
372}
373
Rusty Russell5dfc1762012-01-12 15:44:42 +1030374/**
375 * virtqueue_get_buf - get the next used buffer
376 * @vq: the struct virtqueue we're talking about.
377 * @len: the length written into the buffer
378 *
379 * If the driver wrote data into the buffer, @len will be set to the
380 * amount written. This means you don't need to clear the buffer
381 * beforehand to ensure there's no data leakage in the case of short
382 * writes.
383 *
384 * Caller must ensure we don't call this with other virtqueue
385 * operations at the same time (except where noted).
386 *
387 * Returns NULL if there are no used buffers, or the "data" token
Rusty Russellf96fde42012-01-12 15:44:42 +1030388 * handed to virtqueue_add_buf().
Rusty Russell5dfc1762012-01-12 15:44:42 +1030389 */
Michael S. Tsirkin7c5e9ed2010-04-12 16:19:07 +0300390void *virtqueue_get_buf(struct virtqueue *_vq, unsigned int *len)
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000391{
392 struct vring_virtqueue *vq = to_vvq(_vq);
393 void *ret;
394 unsigned int i;
Rusty Russell3b720b82012-01-12 15:44:43 +1030395 u16 last_used;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000396
397 START_USE(vq);
398
Rusty Russell5ef82752008-05-02 21:50:43 -0500399 if (unlikely(vq->broken)) {
400 END_USE(vq);
401 return NULL;
402 }
403
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000404 if (!more_used(vq)) {
405 pr_debug("No more buffers in queue\n");
406 END_USE(vq);
407 return NULL;
408 }
409
Michael S. Tsirkin2d61ba92009-10-25 15:28:53 +0200410 /* Only get used array entries after they have been exposed by host. */
Rusty Russell7b21e342012-01-12 15:44:42 +1030411 virtio_rmb(vq);
Michael S. Tsirkin2d61ba92009-10-25 15:28:53 +0200412
Rusty Russell3b720b82012-01-12 15:44:43 +1030413 last_used = (vq->last_used_idx & (vq->vring.num - 1));
414 i = vq->vring.used->ring[last_used].id;
415 *len = vq->vring.used->ring[last_used].len;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000416
417 if (unlikely(i >= vq->vring.num)) {
418 BAD_RING(vq, "id %u out of range\n", i);
419 return NULL;
420 }
421 if (unlikely(!vq->data[i])) {
422 BAD_RING(vq, "id %u is not a head!\n", i);
423 return NULL;
424 }
425
426 /* detach_buf clears data, so grab it now. */
427 ret = vq->data[i];
428 detach_buf(vq, i);
429 vq->last_used_idx++;
Michael S. Tsirkina5c262c2011-05-20 02:10:44 +0300430 /* If we expect an interrupt for the next entry, tell host
431 * by writing event index and flush out the write before
432 * the read in the next get_buf call. */
433 if (!(vq->vring.avail->flags & VRING_AVAIL_F_NO_INTERRUPT)) {
434 vring_used_event(&vq->vring) = vq->last_used_idx;
Rusty Russell7b21e342012-01-12 15:44:42 +1030435 virtio_mb(vq);
Michael S. Tsirkina5c262c2011-05-20 02:10:44 +0300436 }
437
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000438 END_USE(vq);
439 return ret;
440}
Michael S. Tsirkin7c5e9ed2010-04-12 16:19:07 +0300441EXPORT_SYMBOL_GPL(virtqueue_get_buf);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000442
Rusty Russell5dfc1762012-01-12 15:44:42 +1030443/**
444 * virtqueue_disable_cb - disable callbacks
445 * @vq: the struct virtqueue we're talking about.
446 *
447 * Note that this is not necessarily synchronous, hence unreliable and only
448 * useful as an optimization.
449 *
450 * Unlike other operations, this need not be serialized.
451 */
Michael S. Tsirkin7c5e9ed2010-04-12 16:19:07 +0300452void virtqueue_disable_cb(struct virtqueue *_vq)
Rusty Russell18445c42008-02-04 23:49:57 -0500453{
454 struct vring_virtqueue *vq = to_vvq(_vq);
455
Rusty Russell18445c42008-02-04 23:49:57 -0500456 vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
Rusty Russell18445c42008-02-04 23:49:57 -0500457}
Michael S. Tsirkin7c5e9ed2010-04-12 16:19:07 +0300458EXPORT_SYMBOL_GPL(virtqueue_disable_cb);
Rusty Russell18445c42008-02-04 23:49:57 -0500459
Rusty Russell5dfc1762012-01-12 15:44:42 +1030460/**
461 * virtqueue_enable_cb - restart callbacks after disable_cb.
462 * @vq: the struct virtqueue we're talking about.
463 *
464 * This re-enables callbacks; it returns "false" if there are pending
465 * buffers in the queue, to detect a possible race between the driver
466 * checking for more work, and enabling callbacks.
467 *
468 * Caller must ensure we don't call this with other virtqueue
469 * operations at the same time (except where noted).
470 */
Michael S. Tsirkin7c5e9ed2010-04-12 16:19:07 +0300471bool virtqueue_enable_cb(struct virtqueue *_vq)
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000472{
473 struct vring_virtqueue *vq = to_vvq(_vq);
474
475 START_USE(vq);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000476
477 /* We optimistically turn back on interrupts, then check if there was
478 * more to do. */
Michael S. Tsirkina5c262c2011-05-20 02:10:44 +0300479 /* Depending on the VIRTIO_RING_F_EVENT_IDX feature, we need to
480 * either clear the flags bit or point the event index at the next
481 * entry. Always do both to keep code simple. */
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000482 vq->vring.avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT;
Michael S. Tsirkina5c262c2011-05-20 02:10:44 +0300483 vring_used_event(&vq->vring) = vq->last_used_idx;
Rusty Russell7b21e342012-01-12 15:44:42 +1030484 virtio_mb(vq);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000485 if (unlikely(more_used(vq))) {
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000486 END_USE(vq);
487 return false;
488 }
489
490 END_USE(vq);
491 return true;
492}
Michael S. Tsirkin7c5e9ed2010-04-12 16:19:07 +0300493EXPORT_SYMBOL_GPL(virtqueue_enable_cb);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000494
Rusty Russell5dfc1762012-01-12 15:44:42 +1030495/**
496 * virtqueue_enable_cb_delayed - restart callbacks after disable_cb.
497 * @vq: the struct virtqueue we're talking about.
498 *
499 * This re-enables callbacks but hints to the other side to delay
500 * interrupts until most of the available buffers have been processed;
501 * it returns "false" if there are many pending buffers in the queue,
502 * to detect a possible race between the driver checking for more work,
503 * and enabling callbacks.
504 *
505 * Caller must ensure we don't call this with other virtqueue
506 * operations at the same time (except where noted).
507 */
Michael S. Tsirkin7ab358c2011-05-20 02:11:14 +0300508bool virtqueue_enable_cb_delayed(struct virtqueue *_vq)
509{
510 struct vring_virtqueue *vq = to_vvq(_vq);
511 u16 bufs;
512
513 START_USE(vq);
514
515 /* We optimistically turn back on interrupts, then check if there was
516 * more to do. */
517 /* Depending on the VIRTIO_RING_F_USED_EVENT_IDX feature, we need to
518 * either clear the flags bit or point the event index at the next
519 * entry. Always do both to keep code simple. */
520 vq->vring.avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT;
521 /* TODO: tune this threshold */
522 bufs = (u16)(vq->vring.avail->idx - vq->last_used_idx) * 3 / 4;
523 vring_used_event(&vq->vring) = vq->last_used_idx + bufs;
Rusty Russell7b21e342012-01-12 15:44:42 +1030524 virtio_mb(vq);
Michael S. Tsirkin7ab358c2011-05-20 02:11:14 +0300525 if (unlikely((u16)(vq->vring.used->idx - vq->last_used_idx) > bufs)) {
526 END_USE(vq);
527 return false;
528 }
529
530 END_USE(vq);
531 return true;
532}
533EXPORT_SYMBOL_GPL(virtqueue_enable_cb_delayed);
534
Rusty Russell5dfc1762012-01-12 15:44:42 +1030535/**
536 * virtqueue_detach_unused_buf - detach first unused buffer
537 * @vq: the struct virtqueue we're talking about.
538 *
Rusty Russellf96fde42012-01-12 15:44:42 +1030539 * Returns NULL or the "data" token handed to virtqueue_add_buf().
Rusty Russell5dfc1762012-01-12 15:44:42 +1030540 * This is not valid on an active queue; it is useful only for device
541 * shutdown.
542 */
Michael S. Tsirkin7c5e9ed2010-04-12 16:19:07 +0300543void *virtqueue_detach_unused_buf(struct virtqueue *_vq)
Shirley Mac021eac2010-01-18 19:15:23 +0530544{
545 struct vring_virtqueue *vq = to_vvq(_vq);
546 unsigned int i;
547 void *buf;
548
549 START_USE(vq);
550
551 for (i = 0; i < vq->vring.num; i++) {
552 if (!vq->data[i])
553 continue;
554 /* detach_buf clears data, so grab it now. */
555 buf = vq->data[i];
556 detach_buf(vq, i);
Amit Shahb3258ff2011-03-16 19:12:10 +0530557 vq->vring.avail->idx--;
Shirley Mac021eac2010-01-18 19:15:23 +0530558 END_USE(vq);
559 return buf;
560 }
561 /* That should have freed everything. */
562 BUG_ON(vq->num_free != vq->vring.num);
563
564 END_USE(vq);
565 return NULL;
566}
Michael S. Tsirkin7c5e9ed2010-04-12 16:19:07 +0300567EXPORT_SYMBOL_GPL(virtqueue_detach_unused_buf);
Shirley Mac021eac2010-01-18 19:15:23 +0530568
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000569irqreturn_t vring_interrupt(int irq, void *_vq)
570{
571 struct vring_virtqueue *vq = to_vvq(_vq);
572
573 if (!more_used(vq)) {
574 pr_debug("virtqueue interrupt with no work for %p\n", vq);
575 return IRQ_NONE;
576 }
577
578 if (unlikely(vq->broken))
579 return IRQ_HANDLED;
580
581 pr_debug("virtqueue callback for %p (%p)\n", vq, vq->vq.callback);
Rusty Russell18445c42008-02-04 23:49:57 -0500582 if (vq->vq.callback)
583 vq->vq.callback(&vq->vq);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000584
585 return IRQ_HANDLED;
586}
Rusty Russellc6fd4702008-02-04 23:50:05 -0500587EXPORT_SYMBOL_GPL(vring_interrupt);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000588
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000589struct virtqueue *vring_new_virtqueue(unsigned int num,
Rusty Russell87c7d572008-12-30 09:26:03 -0600590 unsigned int vring_align,
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000591 struct virtio_device *vdev,
Rusty Russell7b21e342012-01-12 15:44:42 +1030592 bool weak_barriers,
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000593 void *pages,
594 void (*notify)(struct virtqueue *),
Rusty Russell9499f5e2009-06-12 22:16:35 -0600595 void (*callback)(struct virtqueue *),
596 const char *name)
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000597{
598 struct vring_virtqueue *vq;
599 unsigned int i;
600
Rusty Russell42b36cc2007-11-12 13:39:18 +1100601 /* We assume num is a power of 2. */
602 if (num & (num - 1)) {
603 dev_warn(&vdev->dev, "Bad virtqueue length %u\n", num);
604 return NULL;
605 }
606
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000607 vq = kmalloc(sizeof(*vq) + sizeof(void *)*num, GFP_KERNEL);
608 if (!vq)
609 return NULL;
610
Rusty Russell87c7d572008-12-30 09:26:03 -0600611 vring_init(&vq->vring, num, pages, vring_align);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000612 vq->vq.callback = callback;
613 vq->vq.vdev = vdev;
Rusty Russell9499f5e2009-06-12 22:16:35 -0600614 vq->vq.name = name;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000615 vq->notify = notify;
Rusty Russell7b21e342012-01-12 15:44:42 +1030616 vq->weak_barriers = weak_barriers;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000617 vq->broken = false;
618 vq->last_used_idx = 0;
619 vq->num_added = 0;
Rusty Russell9499f5e2009-06-12 22:16:35 -0600620 list_add_tail(&vq->vq.list, &vdev->vqs);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000621#ifdef DEBUG
622 vq->in_use = false;
623#endif
624
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100625 vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC);
Michael S. Tsirkina5c262c2011-05-20 02:10:44 +0300626 vq->event = virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX);
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100627
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000628 /* No callback? Tell other side not to bother us. */
629 if (!callback)
630 vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
631
632 /* Put everything in free lists. */
633 vq->num_free = num;
634 vq->free_head = 0;
Amit Shah3b870622010-02-12 10:32:14 +0530635 for (i = 0; i < num-1; i++) {
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000636 vq->vring.desc[i].next = i+1;
Amit Shah3b870622010-02-12 10:32:14 +0530637 vq->data[i] = NULL;
638 }
639 vq->data[i] = NULL;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000640
641 return &vq->vq;
642}
Rusty Russellc6fd4702008-02-04 23:50:05 -0500643EXPORT_SYMBOL_GPL(vring_new_virtqueue);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000644
645void vring_del_virtqueue(struct virtqueue *vq)
646{
Rusty Russell9499f5e2009-06-12 22:16:35 -0600647 list_del(&vq->list);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000648 kfree(to_vvq(vq));
649}
Rusty Russellc6fd4702008-02-04 23:50:05 -0500650EXPORT_SYMBOL_GPL(vring_del_virtqueue);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000651
Rusty Russelle34f8722008-07-25 12:06:13 -0500652/* Manipulates transport-specific feature bits. */
653void vring_transport_features(struct virtio_device *vdev)
654{
655 unsigned int i;
656
657 for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++) {
658 switch (i) {
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100659 case VIRTIO_RING_F_INDIRECT_DESC:
660 break;
Michael S. Tsirkina5c262c2011-05-20 02:10:44 +0300661 case VIRTIO_RING_F_EVENT_IDX:
662 break;
Rusty Russelle34f8722008-07-25 12:06:13 -0500663 default:
664 /* We don't understand this bit. */
665 clear_bit(i, vdev->features);
666 }
667 }
668}
669EXPORT_SYMBOL_GPL(vring_transport_features);
670
Rusty Russell5dfc1762012-01-12 15:44:42 +1030671/**
672 * virtqueue_get_vring_size - return the size of the virtqueue's vring
673 * @vq: the struct virtqueue containing the vring of interest.
674 *
675 * Returns the size of the vring. This is mainly used for boasting to
676 * userspace. Unlike other operations, this need not be serialized.
677 */
Rick Jones8f9f4662011-10-19 08:10:59 +0000678unsigned int virtqueue_get_vring_size(struct virtqueue *_vq)
679{
680
681 struct vring_virtqueue *vq = to_vvq(_vq);
682
683 return vq->vring.num;
684}
685EXPORT_SYMBOL_GPL(virtqueue_get_vring_size);
686
Rusty Russellc6fd4702008-02-04 23:50:05 -0500687MODULE_LICENSE("GPL");