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>
21 #include <linux/virtio_config.h>
22 #include <linux/device.h>
23 #include <linux/slab.h>
24 #include <linux/module.h>
25 #include <linux/hrtimer.h>
26 
27 /* 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
32  * 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) \
38 	do { if ((vq)->weak_barriers) smp_wmb(); else wmb(); } while(0)
39 #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. */
43 #define virtio_mb(vq) mb()
44 #define virtio_rmb(vq) rmb()
45 #define virtio_wmb(vq) wmb()
46 #endif
47 
48 #ifdef DEBUG
49 /* For development, we want to crash whenever the ring is screwed. */
50 #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)
56 /* Caller is supposed to guarantee no reentry. */
57 #define START_USE(_vq)						\
58 	do {							\
59 		if ((_vq)->in_use)				\
60 			panic("%s:in_use = %i\n",		\
61 			      (_vq)->vq.name, (_vq)->in_use);	\
62 		(_vq)->in_use = __LINE__;			\
63 	} while (0)
64 #define END_USE(_vq) \
65 	do { BUG_ON(!(_vq)->in_use); (_vq)->in_use = 0; } while(0)
66 #else
67 #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)
73 #define START_USE(vq)
74 #define END_USE(vq)
75 #endif
76 
77 struct vring_virtqueue
78 {
79 	struct virtqueue vq;
80 
81 	/* Actual memory layout for this queue */
82 	struct vring vring;
83 
84 	/* Can we use weak barriers? */
85 	bool weak_barriers;
86 
87 	/* Other side has made a mess, don't try any more. */
88 	bool broken;
89 
90 	/* Host supports indirect buffers */
91 	bool indirect;
92 
93 	/* Host publishes avail event idx */
94 	bool event;
95 
96 	/* 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. */
104 	u16 last_used_idx;
105 
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;
112 
113 	/* Figure out if their kicks are too delayed. */
114 	bool last_add_time_valid;
115 	ktime_t last_add_time;
116 #endif
117 
118 	/* Tokens for callbacks. */
119 	void *data[];
120 };
121 
122 #define to_vvq(_vq) container_of(_vq, struct vring_virtqueue, vq)
123 
124 /* Set up an indirect table of descriptors and add it to the queue. */
vring_add_indirect(struct vring_virtqueue * vq,struct scatterlist sg[],unsigned int out,unsigned int in,gfp_t gfp)125 static int vring_add_indirect(struct vring_virtqueue *vq,
126 			      struct scatterlist sg[],
127 			      unsigned int out,
128 			      unsigned int in,
129 			      gfp_t gfp)
130 {
131 	struct vring_desc *desc;
132 	unsigned head;
133 	int i;
134 
135 	/*
136 	 * We require lowmem mappings for the descriptors because
137 	 * otherwise virt_to_phys will give us bogus addresses in the
138 	 * virtqueue.
139 	 */
140 	gfp &= ~(__GFP_HIGHMEM | __GFP_HIGH);
141 
142 	desc = kmalloc((out + in) * sizeof(struct vring_desc), gfp);
143 	if (!desc)
144 		return -ENOMEM;
145 
146 	/* Transfer entries from the sg list into the indirect page */
147 	for (i = 0; i < out; i++) {
148 		desc[i].flags = VRING_DESC_F_NEXT;
149 		desc[i].addr = sg_phys(sg);
150 		desc[i].len = sg->length;
151 		desc[i].next = i+1;
152 		sg++;
153 	}
154 	for (; i < (out + in); i++) {
155 		desc[i].flags = VRING_DESC_F_NEXT|VRING_DESC_F_WRITE;
156 		desc[i].addr = sg_phys(sg);
157 		desc[i].len = sg->length;
158 		desc[i].next = i+1;
159 		sg++;
160 	}
161 
162 	/* Last one doesn't continue. */
163 	desc[i-1].flags &= ~VRING_DESC_F_NEXT;
164 	desc[i-1].next = 0;
165 
166 	/* We're about to use a buffer */
167 	vq->num_free--;
168 
169 	/* Use a single buffer which doesn't continue */
170 	head = vq->free_head;
171 	vq->vring.desc[head].flags = VRING_DESC_F_INDIRECT;
172 	vq->vring.desc[head].addr = virt_to_phys(desc);
173 	vq->vring.desc[head].len = i * sizeof(struct vring_desc);
174 
175 	/* Update free pointer */
176 	vq->free_head = vq->vring.desc[head].next;
177 
178 	return head;
179 }
180 
181 /**
182  * virtqueue_add_buf - expose buffer to other end
183  * @vq: the struct virtqueue we're talking about.
184  * @sg: the description of the buffer(s).
185  * @out_num: the number of sg readable by other side
186  * @in_num: the number of sg which are writable (after readable ones)
187  * @data: the token identifying the buffer.
188  * @gfp: how to do memory allocations (if necessary).
189  *
190  * Caller must ensure we don't call this with other virtqueue operations
191  * at the same time (except where noted).
192  *
193  * Returns remaining capacity of queue or a negative error
194  * (ie. ENOSPC).  Note that it only really makes sense to treat all
195  * positive return values as "available": indirect buffers mean that
196  * we can put an entire sg[] array inside a single queue entry.
197  */
virtqueue_add_buf(struct virtqueue * _vq,struct scatterlist sg[],unsigned int out,unsigned int in,void * data,gfp_t gfp)198 int virtqueue_add_buf(struct virtqueue *_vq,
199 		      struct scatterlist sg[],
200 		      unsigned int out,
201 		      unsigned int in,
202 		      void *data,
203 		      gfp_t gfp)
204 {
205 	struct vring_virtqueue *vq = to_vvq(_vq);
206 	unsigned int i, avail, uninitialized_var(prev);
207 	int head;
208 
209 	START_USE(vq);
210 
211 	BUG_ON(data == NULL);
212 
213 #ifdef DEBUG
214 	{
215 		ktime_t now = ktime_get();
216 
217 		/* No kick or get, with .1 second between?  Warn. */
218 		if (vq->last_add_time_valid)
219 			WARN_ON(ktime_to_ms(ktime_sub(now, vq->last_add_time))
220 					    > 100);
221 		vq->last_add_time = now;
222 		vq->last_add_time_valid = true;
223 	}
224 #endif
225 
226 	/* If the host supports indirect descriptor tables, and we have multiple
227 	 * buffers, then go indirect. FIXME: tune this threshold */
228 	if (vq->indirect && (out + in) > 1 && vq->num_free) {
229 		head = vring_add_indirect(vq, sg, out, in, gfp);
230 		if (likely(head >= 0))
231 			goto add_head;
232 	}
233 
234 	BUG_ON(out + in > vq->vring.num);
235 	BUG_ON(out + in == 0);
236 
237 	if (vq->num_free < out + in) {
238 		pr_debug("Can't add buf len %i - avail = %i\n",
239 			 out + in, vq->num_free);
240 		/* FIXME: for historical reasons, we force a notify here if
241 		 * there are outgoing parts to the buffer.  Presumably the
242 		 * host should service the ring ASAP. */
243 		if (out)
244 			vq->notify(&vq->vq);
245 		END_USE(vq);
246 		return -ENOSPC;
247 	}
248 
249 	/* We're about to use some buffers from the free list. */
250 	vq->num_free -= out + in;
251 
252 	head = vq->free_head;
253 	for (i = vq->free_head; out; i = vq->vring.desc[i].next, out--) {
254 		vq->vring.desc[i].flags = VRING_DESC_F_NEXT;
255 		vq->vring.desc[i].addr = sg_phys(sg);
256 		vq->vring.desc[i].len = sg->length;
257 		prev = i;
258 		sg++;
259 	}
260 	for (; in; i = vq->vring.desc[i].next, in--) {
261 		vq->vring.desc[i].flags = VRING_DESC_F_NEXT|VRING_DESC_F_WRITE;
262 		vq->vring.desc[i].addr = sg_phys(sg);
263 		vq->vring.desc[i].len = sg->length;
264 		prev = i;
265 		sg++;
266 	}
267 	/* Last one doesn't continue. */
268 	vq->vring.desc[prev].flags &= ~VRING_DESC_F_NEXT;
269 
270 	/* Update free pointer */
271 	vq->free_head = i;
272 
273 add_head:
274 	/* Set token. */
275 	vq->data[head] = data;
276 
277 	/* Put entry in available array (but don't update avail->idx until they
278 	 * do sync). */
279 	avail = (vq->vring.avail->idx & (vq->vring.num-1));
280 	vq->vring.avail->ring[avail] = head;
281 
282 	/* Descriptors and available array need to be set before we expose the
283 	 * new available array entries. */
284 	virtio_wmb(vq);
285 	vq->vring.avail->idx++;
286 	vq->num_added++;
287 
288 	/* This is very unlikely, but theoretically possible.  Kick
289 	 * just in case. */
290 	if (unlikely(vq->num_added == (1 << 16) - 1))
291 		virtqueue_kick(_vq);
292 
293 	pr_debug("Added buffer head %i to %p\n", head, vq);
294 	END_USE(vq);
295 
296 	return vq->num_free;
297 }
298 EXPORT_SYMBOL_GPL(virtqueue_add_buf);
299 
300 /**
301  * virtqueue_kick_prepare - first half of split virtqueue_kick call.
302  * @vq: the struct virtqueue
303  *
304  * Instead of virtqueue_kick(), you can do:
305  *	if (virtqueue_kick_prepare(vq))
306  *		virtqueue_notify(vq);
307  *
308  * This is sometimes useful because the virtqueue_kick_prepare() needs
309  * to be serialized, but the actual virtqueue_notify() call does not.
310  */
virtqueue_kick_prepare(struct virtqueue * _vq)311 bool virtqueue_kick_prepare(struct virtqueue *_vq)
312 {
313 	struct vring_virtqueue *vq = to_vvq(_vq);
314 	u16 new, old;
315 	bool needs_kick;
316 
317 	START_USE(vq);
318 	/* We need to expose available array entries before checking avail
319 	 * event. */
320 	virtio_mb(vq);
321 
322 	old = vq->vring.avail->idx - vq->num_added;
323 	new = vq->vring.avail->idx;
324 	vq->num_added = 0;
325 
326 #ifdef DEBUG
327 	if (vq->last_add_time_valid) {
328 		WARN_ON(ktime_to_ms(ktime_sub(ktime_get(),
329 					      vq->last_add_time)) > 100);
330 	}
331 	vq->last_add_time_valid = false;
332 #endif
333 
334 	if (vq->event) {
335 		needs_kick = vring_need_event(vring_avail_event(&vq->vring),
336 					      new, old);
337 	} else {
338 		needs_kick = !(vq->vring.used->flags & VRING_USED_F_NO_NOTIFY);
339 	}
340 	END_USE(vq);
341 	return needs_kick;
342 }
343 EXPORT_SYMBOL_GPL(virtqueue_kick_prepare);
344 
345 /**
346  * virtqueue_notify - second half of split virtqueue_kick call.
347  * @vq: the struct virtqueue
348  *
349  * This does not need to be serialized.
350  */
virtqueue_notify(struct virtqueue * _vq)351 void virtqueue_notify(struct virtqueue *_vq)
352 {
353 	struct vring_virtqueue *vq = to_vvq(_vq);
354 
355 	/* Prod other side to tell it about changes. */
356 	vq->notify(_vq);
357 }
358 EXPORT_SYMBOL_GPL(virtqueue_notify);
359 
360 /**
361  * virtqueue_kick - update after add_buf
362  * @vq: the struct virtqueue
363  *
364  * After one or more virtqueue_add_buf calls, invoke this to kick
365  * the other side.
366  *
367  * Caller must ensure we don't call this with other virtqueue
368  * operations at the same time (except where noted).
369  */
virtqueue_kick(struct virtqueue * vq)370 void virtqueue_kick(struct virtqueue *vq)
371 {
372 	if (virtqueue_kick_prepare(vq))
373 		virtqueue_notify(vq);
374 }
375 EXPORT_SYMBOL_GPL(virtqueue_kick);
376 
detach_buf(struct vring_virtqueue * vq,unsigned int head)377 static void detach_buf(struct vring_virtqueue *vq, unsigned int head)
378 {
379 	unsigned int i;
380 
381 	/* Clear data ptr. */
382 	vq->data[head] = NULL;
383 
384 	/* Put back on free list: find end */
385 	i = head;
386 
387 	/* Free the indirect table */
388 	if (vq->vring.desc[i].flags & VRING_DESC_F_INDIRECT)
389 		kfree(phys_to_virt(vq->vring.desc[i].addr));
390 
391 	while (vq->vring.desc[i].flags & VRING_DESC_F_NEXT) {
392 		i = vq->vring.desc[i].next;
393 		vq->num_free++;
394 	}
395 
396 	vq->vring.desc[i].next = vq->free_head;
397 	vq->free_head = head;
398 	/* Plus final descriptor */
399 	vq->num_free++;
400 }
401 
more_used(const struct vring_virtqueue * vq)402 static inline bool more_used(const struct vring_virtqueue *vq)
403 {
404 	return vq->last_used_idx != vq->vring.used->idx;
405 }
406 
407 /**
408  * virtqueue_get_buf - get the next used buffer
409  * @vq: the struct virtqueue we're talking about.
410  * @len: the length written into the buffer
411  *
412  * If the driver wrote data into the buffer, @len will be set to the
413  * amount written.  This means you don't need to clear the buffer
414  * beforehand to ensure there's no data leakage in the case of short
415  * writes.
416  *
417  * Caller must ensure we don't call this with other virtqueue
418  * operations at the same time (except where noted).
419  *
420  * Returns NULL if there are no used buffers, or the "data" token
421  * handed to virtqueue_add_buf().
422  */
virtqueue_get_buf(struct virtqueue * _vq,unsigned int * len)423 void *virtqueue_get_buf(struct virtqueue *_vq, unsigned int *len)
424 {
425 	struct vring_virtqueue *vq = to_vvq(_vq);
426 	void *ret;
427 	unsigned int i;
428 	u16 last_used;
429 
430 	START_USE(vq);
431 
432 	if (unlikely(vq->broken)) {
433 		END_USE(vq);
434 		return NULL;
435 	}
436 
437 	if (!more_used(vq)) {
438 		pr_debug("No more buffers in queue\n");
439 		END_USE(vq);
440 		return NULL;
441 	}
442 
443 	/* Only get used array entries after they have been exposed by host. */
444 	virtio_rmb(vq);
445 
446 	last_used = (vq->last_used_idx & (vq->vring.num - 1));
447 	i = vq->vring.used->ring[last_used].id;
448 	*len = vq->vring.used->ring[last_used].len;
449 
450 	if (unlikely(i >= vq->vring.num)) {
451 		BAD_RING(vq, "id %u out of range\n", i);
452 		return NULL;
453 	}
454 	if (unlikely(!vq->data[i])) {
455 		BAD_RING(vq, "id %u is not a head!\n", i);
456 		return NULL;
457 	}
458 
459 	/* detach_buf clears data, so grab it now. */
460 	ret = vq->data[i];
461 	detach_buf(vq, i);
462 	vq->last_used_idx++;
463 	/* If we expect an interrupt for the next entry, tell host
464 	 * by writing event index and flush out the write before
465 	 * the read in the next get_buf call. */
466 	if (!(vq->vring.avail->flags & VRING_AVAIL_F_NO_INTERRUPT)) {
467 		vring_used_event(&vq->vring) = vq->last_used_idx;
468 		virtio_mb(vq);
469 	}
470 
471 #ifdef DEBUG
472 	vq->last_add_time_valid = false;
473 #endif
474 
475 	END_USE(vq);
476 	return ret;
477 }
478 EXPORT_SYMBOL_GPL(virtqueue_get_buf);
479 
480 /**
481  * virtqueue_disable_cb - disable callbacks
482  * @vq: the struct virtqueue we're talking about.
483  *
484  * Note that this is not necessarily synchronous, hence unreliable and only
485  * useful as an optimization.
486  *
487  * Unlike other operations, this need not be serialized.
488  */
virtqueue_disable_cb(struct virtqueue * _vq)489 void virtqueue_disable_cb(struct virtqueue *_vq)
490 {
491 	struct vring_virtqueue *vq = to_vvq(_vq);
492 
493 	vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
494 }
495 EXPORT_SYMBOL_GPL(virtqueue_disable_cb);
496 
497 /**
498  * virtqueue_enable_cb - restart callbacks after disable_cb.
499  * @vq: the struct virtqueue we're talking about.
500  *
501  * This re-enables callbacks; it returns current queue state
502  * in an opaque unsigned value. This value should be later tested by
503  * virtqueue_poll, to detect a possible race between the driver checking for
504  * more work, and enabling callbacks.
505  *
506  * Caller must ensure we don't call this with other virtqueue
507  * operations at the same time (except where noted).
508  */
virtqueue_enable_cb_prepare(struct virtqueue * _vq)509 unsigned virtqueue_enable_cb_prepare(struct virtqueue *_vq)
510 {
511 	struct vring_virtqueue *vq = to_vvq(_vq);
512 	u16 last_used_idx;
513 
514 	START_USE(vq);
515 
516 	/* We optimistically turn back on interrupts, then check if there was
517 	 * more to do. */
518 	/* Depending on the VIRTIO_RING_F_EVENT_IDX feature, we need to
519 	 * either clear the flags bit or point the event index at the next
520 	 * entry. Always do both to keep code simple. */
521 	vq->vring.avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT;
522 	vring_used_event(&vq->vring) = last_used_idx = vq->last_used_idx;
523 	END_USE(vq);
524 	return last_used_idx;
525 }
526 EXPORT_SYMBOL_GPL(virtqueue_enable_cb_prepare);
527 
528 /**
529  * virtqueue_poll - query pending used buffers
530  * @vq: the struct virtqueue we're talking about.
531  * @last_used_idx: virtqueue state (from call to virtqueue_enable_cb_prepare).
532  *
533  * Returns "true" if there are pending used buffers in the queue.
534  *
535  * This does not need to be serialized.
536  */
virtqueue_poll(struct virtqueue * _vq,unsigned last_used_idx)537 bool virtqueue_poll(struct virtqueue *_vq, unsigned last_used_idx)
538 {
539 	struct vring_virtqueue *vq = to_vvq(_vq);
540 
541 	virtio_mb(vq);
542 	return (u16)last_used_idx != vq->vring.used->idx;
543 }
544 EXPORT_SYMBOL_GPL(virtqueue_poll);
545 
546 /**
547  * virtqueue_enable_cb - restart callbacks after disable_cb.
548  * @vq: the struct virtqueue we're talking about.
549  *
550  * This re-enables callbacks; it returns "false" if there are pending
551  * buffers in the queue, to detect a possible race between the driver
552  * checking for more work, and enabling callbacks.
553  *
554  * Caller must ensure we don't call this with other virtqueue
555  * operations at the same time (except where noted).
556  */
virtqueue_enable_cb(struct virtqueue * _vq)557 bool virtqueue_enable_cb(struct virtqueue *_vq)
558 {
559 	unsigned last_used_idx = virtqueue_enable_cb_prepare(_vq);
560 	return !virtqueue_poll(_vq, last_used_idx);
561 }
562 EXPORT_SYMBOL_GPL(virtqueue_enable_cb);
563 
564 /**
565  * virtqueue_enable_cb_delayed - restart callbacks after disable_cb.
566  * @vq: the struct virtqueue we're talking about.
567  *
568  * This re-enables callbacks but hints to the other side to delay
569  * interrupts until most of the available buffers have been processed;
570  * it returns "false" if there are many pending buffers in the queue,
571  * to detect a possible race between the driver checking for more work,
572  * and enabling callbacks.
573  *
574  * Caller must ensure we don't call this with other virtqueue
575  * operations at the same time (except where noted).
576  */
virtqueue_enable_cb_delayed(struct virtqueue * _vq)577 bool virtqueue_enable_cb_delayed(struct virtqueue *_vq)
578 {
579 	struct vring_virtqueue *vq = to_vvq(_vq);
580 	u16 bufs;
581 
582 	START_USE(vq);
583 
584 	/* We optimistically turn back on interrupts, then check if there was
585 	 * more to do. */
586 	/* Depending on the VIRTIO_RING_F_USED_EVENT_IDX feature, we need to
587 	 * either clear the flags bit or point the event index at the next
588 	 * entry. Always do both to keep code simple. */
589 	vq->vring.avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT;
590 	/* TODO: tune this threshold */
591 	bufs = (u16)(vq->vring.avail->idx - vq->last_used_idx) * 3 / 4;
592 	vring_used_event(&vq->vring) = vq->last_used_idx + bufs;
593 	virtio_mb(vq);
594 	if (unlikely((u16)(vq->vring.used->idx - vq->last_used_idx) > bufs)) {
595 		END_USE(vq);
596 		return false;
597 	}
598 
599 	END_USE(vq);
600 	return true;
601 }
602 EXPORT_SYMBOL_GPL(virtqueue_enable_cb_delayed);
603 
604 /**
605  * virtqueue_detach_unused_buf - detach first unused buffer
606  * @vq: the struct virtqueue we're talking about.
607  *
608  * Returns NULL or the "data" token handed to virtqueue_add_buf().
609  * This is not valid on an active queue; it is useful only for device
610  * shutdown.
611  */
virtqueue_detach_unused_buf(struct virtqueue * _vq)612 void *virtqueue_detach_unused_buf(struct virtqueue *_vq)
613 {
614 	struct vring_virtqueue *vq = to_vvq(_vq);
615 	unsigned int i;
616 	void *buf;
617 
618 	START_USE(vq);
619 
620 	for (i = 0; i < vq->vring.num; i++) {
621 		if (!vq->data[i])
622 			continue;
623 		/* detach_buf clears data, so grab it now. */
624 		buf = vq->data[i];
625 		detach_buf(vq, i);
626 		vq->vring.avail->idx--;
627 		END_USE(vq);
628 		return buf;
629 	}
630 	/* That should have freed everything. */
631 	BUG_ON(vq->num_free != vq->vring.num);
632 
633 	END_USE(vq);
634 	return NULL;
635 }
636 EXPORT_SYMBOL_GPL(virtqueue_detach_unused_buf);
637 
vring_interrupt(int irq,void * _vq)638 irqreturn_t vring_interrupt(int irq, void *_vq)
639 {
640 	struct vring_virtqueue *vq = to_vvq(_vq);
641 
642 	if (!more_used(vq)) {
643 		pr_debug("virtqueue interrupt with no work for %p\n", vq);
644 		return IRQ_NONE;
645 	}
646 
647 	if (unlikely(vq->broken))
648 		return IRQ_HANDLED;
649 
650 	pr_debug("virtqueue callback for %p (%p)\n", vq, vq->vq.callback);
651 	if (vq->vq.callback)
652 		vq->vq.callback(&vq->vq);
653 
654 	return IRQ_HANDLED;
655 }
656 EXPORT_SYMBOL_GPL(vring_interrupt);
657 
vring_new_virtqueue(unsigned int num,unsigned int vring_align,struct virtio_device * vdev,bool weak_barriers,void * pages,void (* notify)(struct virtqueue *),void (* callback)(struct virtqueue *),const char * name)658 struct virtqueue *vring_new_virtqueue(unsigned int num,
659 				      unsigned int vring_align,
660 				      struct virtio_device *vdev,
661 				      bool weak_barriers,
662 				      void *pages,
663 				      void (*notify)(struct virtqueue *),
664 				      void (*callback)(struct virtqueue *),
665 				      const char *name)
666 {
667 	struct vring_virtqueue *vq;
668 	unsigned int i;
669 
670 	/* We assume num is a power of 2. */
671 	if (num & (num - 1)) {
672 		dev_warn(&vdev->dev, "Bad virtqueue length %u\n", num);
673 		return NULL;
674 	}
675 
676 	vq = kmalloc(sizeof(*vq) + sizeof(void *)*num, GFP_KERNEL);
677 	if (!vq)
678 		return NULL;
679 
680 	vring_init(&vq->vring, num, pages, vring_align);
681 	vq->vq.callback = callback;
682 	vq->vq.vdev = vdev;
683 	vq->vq.name = name;
684 	vq->notify = notify;
685 	vq->weak_barriers = weak_barriers;
686 	vq->broken = false;
687 	vq->last_used_idx = 0;
688 	vq->num_added = 0;
689 	list_add_tail(&vq->vq.list, &vdev->vqs);
690 #ifdef DEBUG
691 	vq->in_use = false;
692 	vq->last_add_time_valid = false;
693 #endif
694 
695 	vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC);
696 	vq->event = virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX);
697 
698 	/* No callback?  Tell other side not to bother us. */
699 	if (!callback)
700 		vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
701 
702 	/* Put everything in free lists. */
703 	vq->num_free = num;
704 	vq->free_head = 0;
705 	for (i = 0; i < num-1; i++) {
706 		vq->vring.desc[i].next = i+1;
707 		vq->data[i] = NULL;
708 	}
709 	vq->data[i] = NULL;
710 
711 	return &vq->vq;
712 }
713 EXPORT_SYMBOL_GPL(vring_new_virtqueue);
714 
vring_del_virtqueue(struct virtqueue * vq)715 void vring_del_virtqueue(struct virtqueue *vq)
716 {
717 	list_del(&vq->list);
718 	kfree(to_vvq(vq));
719 }
720 EXPORT_SYMBOL_GPL(vring_del_virtqueue);
721 
722 /* Manipulates transport-specific feature bits. */
vring_transport_features(struct virtio_device * vdev)723 void vring_transport_features(struct virtio_device *vdev)
724 {
725 	unsigned int i;
726 
727 	for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++) {
728 		switch (i) {
729 		case VIRTIO_RING_F_INDIRECT_DESC:
730 			break;
731 		case VIRTIO_RING_F_EVENT_IDX:
732 			break;
733 		default:
734 			/* We don't understand this bit. */
735 			clear_bit(i, vdev->features);
736 		}
737 	}
738 }
739 EXPORT_SYMBOL_GPL(vring_transport_features);
740 
741 /**
742  * virtqueue_get_vring_size - return the size of the virtqueue's vring
743  * @vq: the struct virtqueue containing the vring of interest.
744  *
745  * Returns the size of the vring.  This is mainly used for boasting to
746  * userspace.  Unlike other operations, this need not be serialized.
747  */
virtqueue_get_vring_size(struct virtqueue * _vq)748 unsigned int virtqueue_get_vring_size(struct virtqueue *_vq)
749 {
750 
751 	struct vring_virtqueue *vq = to_vvq(_vq);
752 
753 	return vq->vring.num;
754 }
755 EXPORT_SYMBOL_GPL(virtqueue_get_vring_size);
756 
757 MODULE_LICENSE("GPL");
758