blob: 7e6fd7517016b9a1608e59f15c1b4b2af9e2349f [file] [log] [blame]
Rusty Russell0a8a69d2007-10-22 11:03:40 +10001/* Virtio ring implementation.
2 *
3 * Copyright 2007 Rusty Russell IBM Corporation
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19#include <linux/virtio.h>
20#include <linux/virtio_ring.h>
Rusty Russelle34f8722008-07-25 12:06:13 -050021#include <linux/virtio_config.h>
Rusty Russell0a8a69d2007-10-22 11:03:40 +100022#include <linux/device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Paul Gortmakerb5a2c4f2011-07-03 16:20:30 -040024#include <linux/module.h>
Rusty Russelle93300b2012-01-12 15:44:43 +103025#include <linux/hrtimer.h>
Rusty Russell0a8a69d2007-10-22 11:03:40 +100026
Michael S. Tsirkind57ed952010-01-28 00:42:23 +020027/* virtio guest is communicating with a virtual "device" that actually runs on
28 * a host processor. Memory barriers are used to control SMP effects. */
29#ifdef CONFIG_SMP
30/* Where possible, use SMP barriers which are more lightweight than mandatory
31 * barriers, because mandatory barriers control MMIO effects on accesses
Rusty Russell7b21e342012-01-12 15:44:42 +103032 * through relaxed memory I/O windows (which virtio-pci does not use). */
33#define virtio_mb(vq) \
34 do { if ((vq)->weak_barriers) smp_mb(); else mb(); } while(0)
35#define virtio_rmb(vq) \
36 do { if ((vq)->weak_barriers) smp_rmb(); else rmb(); } while(0)
37#define virtio_wmb(vq) \
Jason Wang4dbc5d92012-01-20 16:16:59 +080038 do { if ((vq)->weak_barriers) smp_wmb(); else wmb(); } while(0)
Michael S. Tsirkind57ed952010-01-28 00:42:23 +020039#else
40/* We must force memory ordering even if guest is UP since host could be
41 * running on another CPU, but SMP barriers are defined to barrier() in that
42 * configuration. So fall back to mandatory barriers instead. */
Rusty Russell7b21e342012-01-12 15:44:42 +103043#define virtio_mb(vq) mb()
44#define virtio_rmb(vq) rmb()
45#define virtio_wmb(vq) wmb()
Michael S. Tsirkind57ed952010-01-28 00:42:23 +020046#endif
47
Rusty Russell0a8a69d2007-10-22 11:03:40 +100048#ifdef DEBUG
49/* For development, we want to crash whenever the ring is screwed. */
Rusty Russell9499f5e2009-06-12 22:16:35 -060050#define BAD_RING(_vq, fmt, args...) \
51 do { \
52 dev_err(&(_vq)->vq.vdev->dev, \
53 "%s:"fmt, (_vq)->vq.name, ##args); \
54 BUG(); \
55 } while (0)
Rusty Russellc5f841f2009-03-30 21:55:22 -060056/* Caller is supposed to guarantee no reentry. */
57#define START_USE(_vq) \
58 do { \
59 if ((_vq)->in_use) \
Rusty Russell9499f5e2009-06-12 22:16:35 -060060 panic("%s:in_use = %i\n", \
61 (_vq)->vq.name, (_vq)->in_use); \
Rusty Russellc5f841f2009-03-30 21:55:22 -060062 (_vq)->in_use = __LINE__; \
Rusty Russell9499f5e2009-06-12 22:16:35 -060063 } while (0)
Roel Kluin3a35ce72009-01-22 16:42:57 +010064#define END_USE(_vq) \
Rusty Russell97a545a2010-02-24 14:22:22 -060065 do { BUG_ON(!(_vq)->in_use); (_vq)->in_use = 0; } while(0)
Rusty Russell0a8a69d2007-10-22 11:03:40 +100066#else
Rusty Russell9499f5e2009-06-12 22:16:35 -060067#define BAD_RING(_vq, fmt, args...) \
68 do { \
69 dev_err(&_vq->vq.vdev->dev, \
70 "%s:"fmt, (_vq)->vq.name, ##args); \
71 (_vq)->broken = true; \
72 } while (0)
Rusty Russell0a8a69d2007-10-22 11:03:40 +100073#define START_USE(vq)
74#define END_USE(vq)
75#endif
76
77struct vring_virtqueue
78{
79 struct virtqueue vq;
80
81 /* Actual memory layout for this queue */
82 struct vring vring;
83
Rusty Russell7b21e342012-01-12 15:44:42 +103084 /* Can we use weak barriers? */
85 bool weak_barriers;
86
Rusty Russell0a8a69d2007-10-22 11:03:40 +100087 /* Other side has made a mess, don't try any more. */
88 bool broken;
89
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +010090 /* Host supports indirect buffers */
91 bool indirect;
92
Michael S. Tsirkina5c262c2011-05-20 02:10:44 +030093 /* Host publishes avail event idx */
94 bool event;
95
Rusty Russell0a8a69d2007-10-22 11:03:40 +100096 /* Number of free buffers */
97 unsigned int num_free;
98 /* Head of free buffer list. */
99 unsigned int free_head;
100 /* Number we've added since last sync. */
101 unsigned int num_added;
102
103 /* Last used index we've seen. */
Anthony Liguori1bc49532007-11-07 15:49:24 -0600104 u16 last_used_idx;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000105
106 /* How to notify other side. FIXME: commonalize hcalls! */
107 void (*notify)(struct virtqueue *vq);
108
109#ifdef DEBUG
110 /* They're supposed to lock for us. */
111 unsigned int in_use;
Rusty Russelle93300b2012-01-12 15:44:43 +1030112
113 /* Figure out if their kicks are too delayed. */
114 bool last_add_time_valid;
115 ktime_t last_add_time;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000116#endif
117
118 /* Tokens for callbacks. */
119 void *data[];
120};
121
122#define to_vvq(_vq) container_of(_vq, struct vring_virtqueue, vq)
123
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100124/* Set up an indirect table of descriptors and add it to the queue. */
125static int vring_add_indirect(struct vring_virtqueue *vq,
126 struct scatterlist sg[],
127 unsigned int out,
Michael S. Tsirkinbbd603e2010-04-29 17:26:37 +0300128 unsigned int in,
129 gfp_t gfp)
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100130{
131 struct vring_desc *desc;
132 unsigned head;
133 int i;
134
Michael S. Tsirkinbbd603e2010-04-29 17:26:37 +0300135 desc = kmalloc((out + in) * sizeof(struct vring_desc), gfp);
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100136 if (!desc)
Michael S. Tsirkin686d3632010-06-10 18:16:11 +0300137 return -ENOMEM;
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100138
139 /* Transfer entries from the sg list into the indirect page */
140 for (i = 0; i < out; i++) {
141 desc[i].flags = VRING_DESC_F_NEXT;
142 desc[i].addr = sg_phys(sg);
143 desc[i].len = sg->length;
144 desc[i].next = i+1;
145 sg++;
146 }
147 for (; i < (out + in); i++) {
148 desc[i].flags = VRING_DESC_F_NEXT|VRING_DESC_F_WRITE;
149 desc[i].addr = sg_phys(sg);
150 desc[i].len = sg->length;
151 desc[i].next = i+1;
152 sg++;
153 }
154
155 /* Last one doesn't continue. */
156 desc[i-1].flags &= ~VRING_DESC_F_NEXT;
157 desc[i-1].next = 0;
158
159 /* We're about to use a buffer */
160 vq->num_free--;
161
162 /* Use a single buffer which doesn't continue */
163 head = vq->free_head;
164 vq->vring.desc[head].flags = VRING_DESC_F_INDIRECT;
165 vq->vring.desc[head].addr = virt_to_phys(desc);
166 vq->vring.desc[head].len = i * sizeof(struct vring_desc);
167
168 /* Update free pointer */
169 vq->free_head = vq->vring.desc[head].next;
170
171 return head;
172}
173
Rusty Russell5dfc1762012-01-12 15:44:42 +1030174/**
Yatin Manerkarc89a2d02012-06-12 15:27:41 -0700175 * vring_add_buf - expose buffer to other end
Rusty Russell5dfc1762012-01-12 15:44:42 +1030176 * @vq: the struct virtqueue we're talking about.
177 * @sg: the description of the buffer(s).
178 * @out_num: the number of sg readable by other side
179 * @in_num: the number of sg which are writable (after readable ones)
180 * @data: the token identifying the buffer.
181 * @gfp: how to do memory allocations (if necessary).
182 *
183 * Caller must ensure we don't call this with other virtqueue operations
184 * at the same time (except where noted).
185 *
186 * Returns remaining capacity of queue or a negative error
187 * (ie. ENOSPC). Note that it only really makes sense to treat all
188 * positive return values as "available": indirect buffers mean that
189 * we can put an entire sg[] array inside a single queue entry.
190 */
Yatin Manerkarc89a2d02012-06-12 15:27:41 -0700191static int vring_add_buf(struct virtqueue *_vq,
Rusty Russellf96fde42012-01-12 15:44:42 +1030192 struct scatterlist sg[],
193 unsigned int out,
194 unsigned int in,
195 void *data,
196 gfp_t gfp)
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000197{
198 struct vring_virtqueue *vq = to_vvq(_vq);
Michael S. Tsirkin1fe9b6f2010-07-26 16:55:30 +0930199 unsigned int i, avail, uninitialized_var(prev);
200 int head;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000201
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100202 START_USE(vq);
203
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000204 BUG_ON(data == NULL);
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100205
Rusty Russelle93300b2012-01-12 15:44:43 +1030206#ifdef DEBUG
207 {
208 ktime_t now = ktime_get();
209
210 /* No kick or get, with .1 second between? Warn. */
211 if (vq->last_add_time_valid)
212 WARN_ON(ktime_to_ms(ktime_sub(now, vq->last_add_time))
213 > 100);
214 vq->last_add_time = now;
215 vq->last_add_time_valid = true;
216 }
217#endif
218
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100219 /* If the host supports indirect descriptor tables, and we have multiple
220 * buffers, then go indirect. FIXME: tune this threshold */
221 if (vq->indirect && (out + in) > 1 && vq->num_free) {
Michael S. Tsirkinbbd603e2010-04-29 17:26:37 +0300222 head = vring_add_indirect(vq, sg, out, in, gfp);
Michael S. Tsirkin1fe9b6f2010-07-26 16:55:30 +0930223 if (likely(head >= 0))
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100224 goto add_head;
225 }
226
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000227 BUG_ON(out + in > vq->vring.num);
228 BUG_ON(out + in == 0);
229
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000230 if (vq->num_free < out + in) {
231 pr_debug("Can't add buf len %i - avail = %i\n",
232 out + in, vq->num_free);
Rusty Russell44653ea2008-07-25 12:06:04 -0500233 /* FIXME: for historical reasons, we force a notify here if
234 * there are outgoing parts to the buffer. Presumably the
235 * host should service the ring ASAP. */
236 if (out)
237 vq->notify(&vq->vq);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000238 END_USE(vq);
239 return -ENOSPC;
240 }
241
242 /* We're about to use some buffers from the free list. */
243 vq->num_free -= out + in;
244
245 head = vq->free_head;
246 for (i = vq->free_head; out; i = vq->vring.desc[i].next, out--) {
247 vq->vring.desc[i].flags = VRING_DESC_F_NEXT;
Rusty Russell15f9c892008-02-04 23:50:05 -0500248 vq->vring.desc[i].addr = sg_phys(sg);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000249 vq->vring.desc[i].len = sg->length;
250 prev = i;
251 sg++;
252 }
253 for (; in; i = vq->vring.desc[i].next, in--) {
254 vq->vring.desc[i].flags = VRING_DESC_F_NEXT|VRING_DESC_F_WRITE;
Rusty Russell15f9c892008-02-04 23:50:05 -0500255 vq->vring.desc[i].addr = sg_phys(sg);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000256 vq->vring.desc[i].len = sg->length;
257 prev = i;
258 sg++;
259 }
260 /* Last one doesn't continue. */
261 vq->vring.desc[prev].flags &= ~VRING_DESC_F_NEXT;
262
263 /* Update free pointer */
264 vq->free_head = i;
265
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100266add_head:
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000267 /* Set token. */
268 vq->data[head] = data;
269
270 /* Put entry in available array (but don't update avail->idx until they
Rusty Russell3b720b82012-01-12 15:44:43 +1030271 * do sync). */
Rusty Russellee7cd892012-01-12 15:44:43 +1030272 avail = (vq->vring.avail->idx & (vq->vring.num-1));
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000273 vq->vring.avail->ring[avail] = head;
274
Rusty Russellee7cd892012-01-12 15:44:43 +1030275 /* Descriptors and available array need to be set before we expose the
276 * new available array entries. */
277 virtio_wmb(vq);
278 vq->vring.avail->idx++;
279 vq->num_added++;
280
281 /* This is very unlikely, but theoretically possible. Kick
282 * just in case. */
283 if (unlikely(vq->num_added == (1 << 16) - 1))
284 virtqueue_kick(_vq);
285
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000286 pr_debug("Added buffer head %i to %p\n", head, vq);
287 END_USE(vq);
Rusty Russell3c1b27d2009-09-23 22:26:31 -0600288
Rusty Russell3c1b27d2009-09-23 22:26:31 -0600289 return vq->num_free;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000290}
291
Rusty Russell5dfc1762012-01-12 15:44:42 +1030292/**
Yatin Manerkarc89a2d02012-06-12 15:27:41 -0700293 * vring_kick_prepare - first half of split vring_kick call.
Rusty Russell5dfc1762012-01-12 15:44:42 +1030294 * @vq: the struct virtqueue
295 *
Yatin Manerkarc89a2d02012-06-12 15:27:41 -0700296 * Instead of vring_kick(), you can do:
297 * if (vring_kick_prepare(vq))
298 * vring_kick_notify(vq);
Rusty Russell5dfc1762012-01-12 15:44:42 +1030299 *
Yatin Manerkarc89a2d02012-06-12 15:27:41 -0700300 * This is sometimes useful because the vring_kick_prepare() needs
301 * to be serialized, but the actual vring_kick_notify() call does not.
Rusty Russell5dfc1762012-01-12 15:44:42 +1030302 */
Yatin Manerkarc89a2d02012-06-12 15:27:41 -0700303static bool vring_kick_prepare(struct virtqueue *_vq)
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000304{
305 struct vring_virtqueue *vq = to_vvq(_vq);
Michael S. Tsirkina5c262c2011-05-20 02:10:44 +0300306 u16 new, old;
Rusty Russell41f03772012-01-12 15:44:43 +1030307 bool needs_kick;
308
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000309 START_USE(vq);
Jason Wanga72caae2012-01-20 16:17:08 +0800310 /* We need to expose available array entries before checking avail
311 * event. */
312 virtio_mb(vq);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000313
Rusty Russellee7cd892012-01-12 15:44:43 +1030314 old = vq->vring.avail->idx - vq->num_added;
315 new = vq->vring.avail->idx;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000316 vq->num_added = 0;
317
Rusty Russelle93300b2012-01-12 15:44:43 +1030318#ifdef DEBUG
319 if (vq->last_add_time_valid) {
320 WARN_ON(ktime_to_ms(ktime_sub(ktime_get(),
321 vq->last_add_time)) > 100);
322 }
323 vq->last_add_time_valid = false;
324#endif
325
Rusty Russell41f03772012-01-12 15:44:43 +1030326 if (vq->event) {
327 needs_kick = vring_need_event(vring_avail_event(&vq->vring),
328 new, old);
329 } else {
330 needs_kick = !(vq->vring.used->flags & VRING_USED_F_NO_NOTIFY);
331 }
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000332 END_USE(vq);
Rusty Russell41f03772012-01-12 15:44:43 +1030333 return needs_kick;
334}
Rusty Russell41f03772012-01-12 15:44:43 +1030335
336/**
Yatin Manerkarc89a2d02012-06-12 15:27:41 -0700337 * vring_kick_notify - second half of split virtqueue_kick call.
Rusty Russell41f03772012-01-12 15:44:43 +1030338 * @vq: the struct virtqueue
339 *
340 * This does not need to be serialized.
341 */
Yatin Manerkarc89a2d02012-06-12 15:27:41 -0700342static void vring_kick_notify(struct virtqueue *_vq)
Rusty Russell41f03772012-01-12 15:44:43 +1030343{
344 struct vring_virtqueue *vq = to_vvq(_vq);
345
346 /* Prod other side to tell it about changes. */
347 vq->notify(_vq);
348}
Rusty Russell41f03772012-01-12 15:44:43 +1030349
350/**
Yatin Manerkarc89a2d02012-06-12 15:27:41 -0700351 * vring_kick - update after add_buf
Rusty Russell41f03772012-01-12 15:44:43 +1030352 * @vq: the struct virtqueue
353 *
Yatin Manerkarc89a2d02012-06-12 15:27:41 -0700354 * After one or more vring_add_buf calls, invoke this to kick
Rusty Russell41f03772012-01-12 15:44:43 +1030355 * the other side.
356 *
357 * Caller must ensure we don't call this with other virtqueue
358 * operations at the same time (except where noted).
359 */
Yatin Manerkarc89a2d02012-06-12 15:27:41 -0700360static void vring_kick(struct virtqueue *vq)
Rusty Russell41f03772012-01-12 15:44:43 +1030361{
Yatin Manerkarc89a2d02012-06-12 15:27:41 -0700362 if (vring_kick_prepare(vq))
363 vring_kick_notify(vq);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000364}
365
366static void detach_buf(struct vring_virtqueue *vq, unsigned int head)
367{
368 unsigned int i;
369
370 /* Clear data ptr. */
371 vq->data[head] = NULL;
372
373 /* Put back on free list: find end */
374 i = head;
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100375
376 /* Free the indirect table */
377 if (vq->vring.desc[i].flags & VRING_DESC_F_INDIRECT)
378 kfree(phys_to_virt(vq->vring.desc[i].addr));
379
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000380 while (vq->vring.desc[i].flags & VRING_DESC_F_NEXT) {
381 i = vq->vring.desc[i].next;
382 vq->num_free++;
383 }
384
385 vq->vring.desc[i].next = vq->free_head;
386 vq->free_head = head;
387 /* Plus final descriptor */
388 vq->num_free++;
389}
390
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000391static inline bool more_used(const struct vring_virtqueue *vq)
392{
393 return vq->last_used_idx != vq->vring.used->idx;
394}
395
Rusty Russell5dfc1762012-01-12 15:44:42 +1030396/**
Yatin Manerkarc89a2d02012-06-12 15:27:41 -0700397 * vring_get_buf - get the next used buffer
Rusty Russell5dfc1762012-01-12 15:44:42 +1030398 * @vq: the struct virtqueue we're talking about.
399 * @len: the length written into the buffer
400 *
401 * If the driver wrote data into the buffer, @len will be set to the
402 * amount written. This means you don't need to clear the buffer
403 * beforehand to ensure there's no data leakage in the case of short
404 * writes.
405 *
406 * Caller must ensure we don't call this with other virtqueue
407 * operations at the same time (except where noted).
408 *
409 * Returns NULL if there are no used buffers, or the "data" token
Yatin Manerkarc89a2d02012-06-12 15:27:41 -0700410 * handed to vring_add_buf().
Rusty Russell5dfc1762012-01-12 15:44:42 +1030411 */
Yatin Manerkarc89a2d02012-06-12 15:27:41 -0700412static void *vring_get_buf(struct virtqueue *_vq, unsigned int *len)
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000413{
414 struct vring_virtqueue *vq = to_vvq(_vq);
415 void *ret;
416 unsigned int i;
Rusty Russell3b720b82012-01-12 15:44:43 +1030417 u16 last_used;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000418
419 START_USE(vq);
420
Rusty Russell5ef82752008-05-02 21:50:43 -0500421 if (unlikely(vq->broken)) {
422 END_USE(vq);
423 return NULL;
424 }
425
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000426 if (!more_used(vq)) {
427 pr_debug("No more buffers in queue\n");
428 END_USE(vq);
429 return NULL;
430 }
431
Michael S. Tsirkin2d61ba92009-10-25 15:28:53 +0200432 /* Only get used array entries after they have been exposed by host. */
Rusty Russell7b21e342012-01-12 15:44:42 +1030433 virtio_rmb(vq);
Michael S. Tsirkin2d61ba92009-10-25 15:28:53 +0200434
Rusty Russell3b720b82012-01-12 15:44:43 +1030435 last_used = (vq->last_used_idx & (vq->vring.num - 1));
436 i = vq->vring.used->ring[last_used].id;
437 *len = vq->vring.used->ring[last_used].len;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000438
439 if (unlikely(i >= vq->vring.num)) {
440 BAD_RING(vq, "id %u out of range\n", i);
441 return NULL;
442 }
443 if (unlikely(!vq->data[i])) {
444 BAD_RING(vq, "id %u is not a head!\n", i);
445 return NULL;
446 }
447
448 /* detach_buf clears data, so grab it now. */
449 ret = vq->data[i];
450 detach_buf(vq, i);
451 vq->last_used_idx++;
Michael S. Tsirkina5c262c2011-05-20 02:10:44 +0300452 /* If we expect an interrupt for the next entry, tell host
453 * by writing event index and flush out the write before
454 * the read in the next get_buf call. */
455 if (!(vq->vring.avail->flags & VRING_AVAIL_F_NO_INTERRUPT)) {
456 vring_used_event(&vq->vring) = vq->last_used_idx;
Rusty Russell7b21e342012-01-12 15:44:42 +1030457 virtio_mb(vq);
Michael S. Tsirkina5c262c2011-05-20 02:10:44 +0300458 }
459
Rusty Russelle93300b2012-01-12 15:44:43 +1030460#ifdef DEBUG
461 vq->last_add_time_valid = false;
462#endif
463
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000464 END_USE(vq);
465 return ret;
466}
467
Rusty Russell5dfc1762012-01-12 15:44:42 +1030468/**
Yatin Manerkarc89a2d02012-06-12 15:27:41 -0700469 * vring_disable_cb - disable callbacks
Rusty Russell5dfc1762012-01-12 15:44:42 +1030470 * @vq: the struct virtqueue we're talking about.
471 *
472 * Note that this is not necessarily synchronous, hence unreliable and only
473 * useful as an optimization.
474 *
475 * Unlike other operations, this need not be serialized.
476 */
Yatin Manerkarc89a2d02012-06-12 15:27:41 -0700477static void vring_disable_cb(struct virtqueue *_vq)
Rusty Russell18445c42008-02-04 23:49:57 -0500478{
479 struct vring_virtqueue *vq = to_vvq(_vq);
480
Rusty Russell18445c42008-02-04 23:49:57 -0500481 vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
Rusty Russell18445c42008-02-04 23:49:57 -0500482}
483
Rusty Russell5dfc1762012-01-12 15:44:42 +1030484/**
Yatin Manerkarc89a2d02012-06-12 15:27:41 -0700485 * vring_enable_cb - restart callbacks after disable_cb.
Rusty Russell5dfc1762012-01-12 15:44:42 +1030486 * @vq: the struct virtqueue we're talking about.
487 *
488 * This re-enables callbacks; it returns "false" if there are pending
489 * buffers in the queue, to detect a possible race between the driver
490 * checking for more work, and enabling callbacks.
491 *
492 * Caller must ensure we don't call this with other virtqueue
493 * operations at the same time (except where noted).
494 */
Yatin Manerkarc89a2d02012-06-12 15:27:41 -0700495static bool vring_enable_cb(struct virtqueue *_vq)
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000496{
497 struct vring_virtqueue *vq = to_vvq(_vq);
498
499 START_USE(vq);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000500
501 /* We optimistically turn back on interrupts, then check if there was
502 * more to do. */
Michael S. Tsirkina5c262c2011-05-20 02:10:44 +0300503 /* Depending on the VIRTIO_RING_F_EVENT_IDX feature, we need to
504 * either clear the flags bit or point the event index at the next
505 * entry. Always do both to keep code simple. */
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000506 vq->vring.avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT;
Michael S. Tsirkina5c262c2011-05-20 02:10:44 +0300507 vring_used_event(&vq->vring) = vq->last_used_idx;
Rusty Russell7b21e342012-01-12 15:44:42 +1030508 virtio_mb(vq);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000509 if (unlikely(more_used(vq))) {
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000510 END_USE(vq);
511 return false;
512 }
513
514 END_USE(vq);
515 return true;
516}
517
Rusty Russell5dfc1762012-01-12 15:44:42 +1030518/**
Yatin Manerkarc89a2d02012-06-12 15:27:41 -0700519 * vring_enable_cb_delayed - restart callbacks after disable_cb.
Rusty Russell5dfc1762012-01-12 15:44:42 +1030520 * @vq: the struct virtqueue we're talking about.
521 *
522 * This re-enables callbacks but hints to the other side to delay
523 * interrupts until most of the available buffers have been processed;
524 * it returns "false" if there are many pending buffers in the queue,
525 * to detect a possible race between the driver checking for more work,
526 * and enabling callbacks.
527 *
528 * Caller must ensure we don't call this with other virtqueue
529 * operations at the same time (except where noted).
530 */
Yatin Manerkarc89a2d02012-06-12 15:27:41 -0700531static bool vring_enable_cb_delayed(struct virtqueue *_vq)
Michael S. Tsirkin7ab358c2011-05-20 02:11:14 +0300532{
533 struct vring_virtqueue *vq = to_vvq(_vq);
534 u16 bufs;
535
536 START_USE(vq);
537
538 /* We optimistically turn back on interrupts, then check if there was
539 * more to do. */
540 /* Depending on the VIRTIO_RING_F_USED_EVENT_IDX feature, we need to
541 * either clear the flags bit or point the event index at the next
542 * entry. Always do both to keep code simple. */
543 vq->vring.avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT;
544 /* TODO: tune this threshold */
545 bufs = (u16)(vq->vring.avail->idx - vq->last_used_idx) * 3 / 4;
546 vring_used_event(&vq->vring) = vq->last_used_idx + bufs;
Rusty Russell7b21e342012-01-12 15:44:42 +1030547 virtio_mb(vq);
Michael S. Tsirkin7ab358c2011-05-20 02:11:14 +0300548 if (unlikely((u16)(vq->vring.used->idx - vq->last_used_idx) > bufs)) {
549 END_USE(vq);
550 return false;
551 }
552
553 END_USE(vq);
554 return true;
555}
Michael S. Tsirkin7ab358c2011-05-20 02:11:14 +0300556
Rusty Russell5dfc1762012-01-12 15:44:42 +1030557/**
Yatin Manerkarc89a2d02012-06-12 15:27:41 -0700558 * vring_detach_unused_buf - detach first unused buffer
Rusty Russell5dfc1762012-01-12 15:44:42 +1030559 * @vq: the struct virtqueue we're talking about.
560 *
Yatin Manerkarc89a2d02012-06-12 15:27:41 -0700561 * Returns NULL or the "data" token handed to vring_add_buf().
Rusty Russell5dfc1762012-01-12 15:44:42 +1030562 * This is not valid on an active queue; it is useful only for device
563 * shutdown.
564 */
Yatin Manerkarc89a2d02012-06-12 15:27:41 -0700565static void *vring_detach_unused_buf(struct virtqueue *_vq)
Shirley Mac021eac2010-01-18 19:15:23 +0530566{
567 struct vring_virtqueue *vq = to_vvq(_vq);
568 unsigned int i;
569 void *buf;
570
571 START_USE(vq);
572
573 for (i = 0; i < vq->vring.num; i++) {
574 if (!vq->data[i])
575 continue;
576 /* detach_buf clears data, so grab it now. */
577 buf = vq->data[i];
578 detach_buf(vq, i);
Amit Shahb3258ff2011-03-16 19:12:10 +0530579 vq->vring.avail->idx--;
Shirley Mac021eac2010-01-18 19:15:23 +0530580 END_USE(vq);
581 return buf;
582 }
583 /* That should have freed everything. */
584 BUG_ON(vq->num_free != vq->vring.num);
585
586 END_USE(vq);
587 return NULL;
588}
589
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000590irqreturn_t vring_interrupt(int irq, void *_vq)
591{
592 struct vring_virtqueue *vq = to_vvq(_vq);
593
594 if (!more_used(vq)) {
595 pr_debug("virtqueue interrupt with no work for %p\n", vq);
596 return IRQ_NONE;
597 }
598
599 if (unlikely(vq->broken))
600 return IRQ_HANDLED;
601
602 pr_debug("virtqueue callback for %p (%p)\n", vq, vq->vq.callback);
Rusty Russell18445c42008-02-04 23:49:57 -0500603 if (vq->vq.callback)
604 vq->vq.callback(&vq->vq);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000605
606 return IRQ_HANDLED;
607}
Rusty Russellc6fd4702008-02-04 23:50:05 -0500608EXPORT_SYMBOL_GPL(vring_interrupt);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000609
Yatin Manerkarc89a2d02012-06-12 15:27:41 -0700610/**
611 * get_vring_size - return the size of the virtqueue's vring
612 * @vq: the struct virtqueue containing the vring of interest.
613 *
614 * Returns the size of the vring. This is mainly used for boasting to
615 * userspace. Unlike other operations, this need not be serialized.
616 */
617static unsigned int get_vring_size(struct virtqueue *_vq)
618{
619
620 struct vring_virtqueue *vq = to_vvq(_vq);
621
622 return vq->vring.num;
623}
624
625static struct virtqueue_ops vring_vq_ops = {
626 .add_buf = vring_add_buf,
627 .get_buf = vring_get_buf,
628 .kick = vring_kick,
629 .kick_prepare = vring_kick_prepare,
630 .kick_notify = vring_kick_notify,
631 .disable_cb = vring_disable_cb,
632 .enable_cb = vring_enable_cb,
633 .enable_cb_delayed = vring_enable_cb_delayed,
634 .detach_unused_buf = vring_detach_unused_buf,
635 .get_impl_size = get_vring_size,
636};
637
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000638struct virtqueue *vring_new_virtqueue(unsigned int num,
Rusty Russell87c7d572008-12-30 09:26:03 -0600639 unsigned int vring_align,
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000640 struct virtio_device *vdev,
Rusty Russell7b21e342012-01-12 15:44:42 +1030641 bool weak_barriers,
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000642 void *pages,
643 void (*notify)(struct virtqueue *),
Rusty Russell9499f5e2009-06-12 22:16:35 -0600644 void (*callback)(struct virtqueue *),
645 const char *name)
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000646{
647 struct vring_virtqueue *vq;
648 unsigned int i;
649
Rusty Russell42b36cc2007-11-12 13:39:18 +1100650 /* We assume num is a power of 2. */
651 if (num & (num - 1)) {
652 dev_warn(&vdev->dev, "Bad virtqueue length %u\n", num);
653 return NULL;
654 }
655
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000656 vq = kmalloc(sizeof(*vq) + sizeof(void *)*num, GFP_KERNEL);
657 if (!vq)
658 return NULL;
659
Rusty Russell87c7d572008-12-30 09:26:03 -0600660 vring_init(&vq->vring, num, pages, vring_align);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000661 vq->vq.callback = callback;
662 vq->vq.vdev = vdev;
Yatin Manerkarc89a2d02012-06-12 15:27:41 -0700663 vq->vq.vq_ops = &vring_vq_ops;
Rusty Russell9499f5e2009-06-12 22:16:35 -0600664 vq->vq.name = name;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000665 vq->notify = notify;
Rusty Russell7b21e342012-01-12 15:44:42 +1030666 vq->weak_barriers = weak_barriers;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000667 vq->broken = false;
668 vq->last_used_idx = 0;
669 vq->num_added = 0;
Rusty Russell9499f5e2009-06-12 22:16:35 -0600670 list_add_tail(&vq->vq.list, &vdev->vqs);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000671#ifdef DEBUG
672 vq->in_use = false;
Rusty Russelle93300b2012-01-12 15:44:43 +1030673 vq->last_add_time_valid = false;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000674#endif
675
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100676 vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC);
Michael S. Tsirkina5c262c2011-05-20 02:10:44 +0300677 vq->event = virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX);
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100678
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000679 /* No callback? Tell other side not to bother us. */
680 if (!callback)
681 vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
682
683 /* Put everything in free lists. */
684 vq->num_free = num;
685 vq->free_head = 0;
Amit Shah3b870622010-02-12 10:32:14 +0530686 for (i = 0; i < num-1; i++) {
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000687 vq->vring.desc[i].next = i+1;
Amit Shah3b870622010-02-12 10:32:14 +0530688 vq->data[i] = NULL;
689 }
690 vq->data[i] = NULL;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000691
692 return &vq->vq;
693}
Rusty Russellc6fd4702008-02-04 23:50:05 -0500694EXPORT_SYMBOL_GPL(vring_new_virtqueue);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000695
696void vring_del_virtqueue(struct virtqueue *vq)
697{
Rusty Russell9499f5e2009-06-12 22:16:35 -0600698 list_del(&vq->list);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000699 kfree(to_vvq(vq));
700}
Rusty Russellc6fd4702008-02-04 23:50:05 -0500701EXPORT_SYMBOL_GPL(vring_del_virtqueue);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000702
Rusty Russelle34f8722008-07-25 12:06:13 -0500703/* Manipulates transport-specific feature bits. */
704void vring_transport_features(struct virtio_device *vdev)
705{
706 unsigned int i;
707
708 for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++) {
709 switch (i) {
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100710 case VIRTIO_RING_F_INDIRECT_DESC:
711 break;
Michael S. Tsirkina5c262c2011-05-20 02:10:44 +0300712 case VIRTIO_RING_F_EVENT_IDX:
713 break;
Rusty Russelle34f8722008-07-25 12:06:13 -0500714 default:
715 /* We don't understand this bit. */
716 clear_bit(i, vdev->features);
717 }
718 }
719}
720EXPORT_SYMBOL_GPL(vring_transport_features);
721
Rusty Russellc6fd4702008-02-04 23:50:05 -0500722MODULE_LICENSE("GPL");