| Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 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> | 
| Rusty Russell | e34f872 | 2008-07-25 12:06:13 -0500 | [diff] [blame] | 21 | #include <linux/virtio_config.h> | 
| Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 22 | #include <linux/device.h> | 
| Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 23 | #include <linux/slab.h> | 
| Paul Gortmaker | b5a2c4f | 2011-07-03 16:20:30 -0400 | [diff] [blame] | 24 | #include <linux/module.h> | 
| Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 25 |  | 
| Michael S. Tsirkin | d57ed95 | 2010-01-28 00:42:23 +0200 | [diff] [blame] | 26 | /* 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 Russell | 7b21e34 | 2012-01-12 15:44:42 +1030 | [diff] [blame] | 31 |  * 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. Tsirkin | d57ed95 | 2010-01-28 00:42:23 +0200 | [diff] [blame] | 38 | #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 Russell | 7b21e34 | 2012-01-12 15:44:42 +1030 | [diff] [blame] | 42 | #define virtio_mb(vq) mb() | 
 | 43 | #define virtio_rmb(vq) rmb() | 
 | 44 | #define virtio_wmb(vq) wmb() | 
| Michael S. Tsirkin | d57ed95 | 2010-01-28 00:42:23 +0200 | [diff] [blame] | 45 | #endif | 
 | 46 |  | 
| Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 47 | #ifdef DEBUG | 
 | 48 | /* For development, we want to crash whenever the ring is screwed. */ | 
| Rusty Russell | 9499f5e | 2009-06-12 22:16:35 -0600 | [diff] [blame] | 49 | #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 Russell | c5f841f | 2009-03-30 21:55:22 -0600 | [diff] [blame] | 55 | /* Caller is supposed to guarantee no reentry. */ | 
 | 56 | #define START_USE(_vq)						\ | 
 | 57 | 	do {							\ | 
 | 58 | 		if ((_vq)->in_use)				\ | 
| Rusty Russell | 9499f5e | 2009-06-12 22:16:35 -0600 | [diff] [blame] | 59 | 			panic("%s:in_use = %i\n",		\ | 
 | 60 | 			      (_vq)->vq.name, (_vq)->in_use);	\ | 
| Rusty Russell | c5f841f | 2009-03-30 21:55:22 -0600 | [diff] [blame] | 61 | 		(_vq)->in_use = __LINE__;			\ | 
| Rusty Russell | 9499f5e | 2009-06-12 22:16:35 -0600 | [diff] [blame] | 62 | 	} while (0) | 
| Roel Kluin | 3a35ce7 | 2009-01-22 16:42:57 +0100 | [diff] [blame] | 63 | #define END_USE(_vq) \ | 
| Rusty Russell | 97a545a | 2010-02-24 14:22:22 -0600 | [diff] [blame] | 64 | 	do { BUG_ON(!(_vq)->in_use); (_vq)->in_use = 0; } while(0) | 
| Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 65 | #else | 
| Rusty Russell | 9499f5e | 2009-06-12 22:16:35 -0600 | [diff] [blame] | 66 | #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 Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 72 | #define START_USE(vq) | 
 | 73 | #define END_USE(vq) | 
 | 74 | #endif | 
 | 75 |  | 
 | 76 | struct vring_virtqueue | 
 | 77 | { | 
 | 78 | 	struct virtqueue vq; | 
 | 79 |  | 
 | 80 | 	/* Actual memory layout for this queue */ | 
 | 81 | 	struct vring vring; | 
 | 82 |  | 
| Rusty Russell | 7b21e34 | 2012-01-12 15:44:42 +1030 | [diff] [blame] | 83 | 	/* Can we use weak barriers? */ | 
 | 84 | 	bool weak_barriers; | 
 | 85 |  | 
| Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 86 | 	/* Other side has made a mess, don't try any more. */ | 
 | 87 | 	bool broken; | 
 | 88 |  | 
| Mark McLoughlin | 9fa29b9d | 2009-05-11 18:11:45 +0100 | [diff] [blame] | 89 | 	/* Host supports indirect buffers */ | 
 | 90 | 	bool indirect; | 
 | 91 |  | 
| Michael S. Tsirkin | a5c262c | 2011-05-20 02:10:44 +0300 | [diff] [blame] | 92 | 	/* Host publishes avail event idx */ | 
 | 93 | 	bool event; | 
 | 94 |  | 
| Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 95 | 	/* 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 Liguori | 1bc4953 | 2007-11-07 15:49:24 -0600 | [diff] [blame] | 103 | 	u16 last_used_idx; | 
| Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 104 |  | 
 | 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 McLoughlin | 9fa29b9d | 2009-05-11 18:11:45 +0100 | [diff] [blame] | 119 | /* Set up an indirect table of descriptors and add it to the queue. */ | 
 | 120 | static int vring_add_indirect(struct vring_virtqueue *vq, | 
 | 121 | 			      struct scatterlist sg[], | 
 | 122 | 			      unsigned int out, | 
| Michael S. Tsirkin | bbd603e | 2010-04-29 17:26:37 +0300 | [diff] [blame] | 123 | 			      unsigned int in, | 
 | 124 | 			      gfp_t gfp) | 
| Mark McLoughlin | 9fa29b9d | 2009-05-11 18:11:45 +0100 | [diff] [blame] | 125 | { | 
 | 126 | 	struct vring_desc *desc; | 
 | 127 | 	unsigned head; | 
 | 128 | 	int i; | 
 | 129 |  | 
| Michael S. Tsirkin | bbd603e | 2010-04-29 17:26:37 +0300 | [diff] [blame] | 130 | 	desc = kmalloc((out + in) * sizeof(struct vring_desc), gfp); | 
| Mark McLoughlin | 9fa29b9d | 2009-05-11 18:11:45 +0100 | [diff] [blame] | 131 | 	if (!desc) | 
| Michael S. Tsirkin | 686d363 | 2010-06-10 18:16:11 +0300 | [diff] [blame] | 132 | 		return -ENOMEM; | 
| Mark McLoughlin | 9fa29b9d | 2009-05-11 18:11:45 +0100 | [diff] [blame] | 133 |  | 
 | 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 |  | 
| Michael S. Tsirkin | bbd603e | 2010-04-29 17:26:37 +0300 | [diff] [blame] | 169 | int virtqueue_add_buf_gfp(struct virtqueue *_vq, | 
 | 170 | 			  struct scatterlist sg[], | 
 | 171 | 			  unsigned int out, | 
 | 172 | 			  unsigned int in, | 
 | 173 | 			  void *data, | 
 | 174 | 			  gfp_t gfp) | 
| Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 175 | { | 
 | 176 | 	struct vring_virtqueue *vq = to_vvq(_vq); | 
| Michael S. Tsirkin | 1fe9b6f | 2010-07-26 16:55:30 +0930 | [diff] [blame] | 177 | 	unsigned int i, avail, uninitialized_var(prev); | 
 | 178 | 	int head; | 
| Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 179 |  | 
| Mark McLoughlin | 9fa29b9d | 2009-05-11 18:11:45 +0100 | [diff] [blame] | 180 | 	START_USE(vq); | 
 | 181 |  | 
| Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 182 | 	BUG_ON(data == NULL); | 
| Mark McLoughlin | 9fa29b9d | 2009-05-11 18:11:45 +0100 | [diff] [blame] | 183 |  | 
 | 184 | 	/* If the host supports indirect descriptor tables, and we have multiple | 
 | 185 | 	 * buffers, then go indirect. FIXME: tune this threshold */ | 
 | 186 | 	if (vq->indirect && (out + in) > 1 && vq->num_free) { | 
| Michael S. Tsirkin | bbd603e | 2010-04-29 17:26:37 +0300 | [diff] [blame] | 187 | 		head = vring_add_indirect(vq, sg, out, in, gfp); | 
| Michael S. Tsirkin | 1fe9b6f | 2010-07-26 16:55:30 +0930 | [diff] [blame] | 188 | 		if (likely(head >= 0)) | 
| Mark McLoughlin | 9fa29b9d | 2009-05-11 18:11:45 +0100 | [diff] [blame] | 189 | 			goto add_head; | 
 | 190 | 	} | 
 | 191 |  | 
| Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 192 | 	BUG_ON(out + in > vq->vring.num); | 
 | 193 | 	BUG_ON(out + in == 0); | 
 | 194 |  | 
| Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 195 | 	if (vq->num_free < out + in) { | 
 | 196 | 		pr_debug("Can't add buf len %i - avail = %i\n", | 
 | 197 | 			 out + in, vq->num_free); | 
| Rusty Russell | 44653ea | 2008-07-25 12:06:04 -0500 | [diff] [blame] | 198 | 		/* FIXME: for historical reasons, we force a notify here if | 
 | 199 | 		 * there are outgoing parts to the buffer.  Presumably the | 
 | 200 | 		 * host should service the ring ASAP. */ | 
 | 201 | 		if (out) | 
 | 202 | 			vq->notify(&vq->vq); | 
| Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 203 | 		END_USE(vq); | 
 | 204 | 		return -ENOSPC; | 
 | 205 | 	} | 
 | 206 |  | 
 | 207 | 	/* We're about to use some buffers from the free list. */ | 
 | 208 | 	vq->num_free -= out + in; | 
 | 209 |  | 
 | 210 | 	head = vq->free_head; | 
 | 211 | 	for (i = vq->free_head; out; i = vq->vring.desc[i].next, out--) { | 
 | 212 | 		vq->vring.desc[i].flags = VRING_DESC_F_NEXT; | 
| Rusty Russell | 15f9c89 | 2008-02-04 23:50:05 -0500 | [diff] [blame] | 213 | 		vq->vring.desc[i].addr = sg_phys(sg); | 
| Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 214 | 		vq->vring.desc[i].len = sg->length; | 
 | 215 | 		prev = i; | 
 | 216 | 		sg++; | 
 | 217 | 	} | 
 | 218 | 	for (; in; i = vq->vring.desc[i].next, in--) { | 
 | 219 | 		vq->vring.desc[i].flags = VRING_DESC_F_NEXT|VRING_DESC_F_WRITE; | 
| Rusty Russell | 15f9c89 | 2008-02-04 23:50:05 -0500 | [diff] [blame] | 220 | 		vq->vring.desc[i].addr = sg_phys(sg); | 
| Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 221 | 		vq->vring.desc[i].len = sg->length; | 
 | 222 | 		prev = i; | 
 | 223 | 		sg++; | 
 | 224 | 	} | 
 | 225 | 	/* Last one doesn't continue. */ | 
 | 226 | 	vq->vring.desc[prev].flags &= ~VRING_DESC_F_NEXT; | 
 | 227 |  | 
 | 228 | 	/* Update free pointer */ | 
 | 229 | 	vq->free_head = i; | 
 | 230 |  | 
| Mark McLoughlin | 9fa29b9d | 2009-05-11 18:11:45 +0100 | [diff] [blame] | 231 | add_head: | 
| Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 232 | 	/* Set token. */ | 
 | 233 | 	vq->data[head] = data; | 
 | 234 |  | 
 | 235 | 	/* Put entry in available array (but don't update avail->idx until they | 
 | 236 | 	 * do sync).  FIXME: avoid modulus here? */ | 
 | 237 | 	avail = (vq->vring.avail->idx + vq->num_added++) % vq->vring.num; | 
 | 238 | 	vq->vring.avail->ring[avail] = head; | 
 | 239 |  | 
 | 240 | 	pr_debug("Added buffer head %i to %p\n", head, vq); | 
 | 241 | 	END_USE(vq); | 
| Rusty Russell | 3c1b27d | 2009-09-23 22:26:31 -0600 | [diff] [blame] | 242 |  | 
| Rusty Russell | 3c1b27d | 2009-09-23 22:26:31 -0600 | [diff] [blame] | 243 | 	return vq->num_free; | 
| Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 244 | } | 
| Michael S. Tsirkin | bbd603e | 2010-04-29 17:26:37 +0300 | [diff] [blame] | 245 | EXPORT_SYMBOL_GPL(virtqueue_add_buf_gfp); | 
| Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 246 |  | 
| Michael S. Tsirkin | 7c5e9ed | 2010-04-12 16:19:07 +0300 | [diff] [blame] | 247 | void virtqueue_kick(struct virtqueue *_vq) | 
| Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 248 | { | 
 | 249 | 	struct vring_virtqueue *vq = to_vvq(_vq); | 
| Michael S. Tsirkin | a5c262c | 2011-05-20 02:10:44 +0300 | [diff] [blame] | 250 | 	u16 new, old; | 
| Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 251 | 	START_USE(vq); | 
 | 252 | 	/* Descriptors and available array need to be set before we expose the | 
 | 253 | 	 * new available array entries. */ | 
| Rusty Russell | 7b21e34 | 2012-01-12 15:44:42 +1030 | [diff] [blame] | 254 | 	virtio_wmb(vq); | 
| Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 255 |  | 
| Michael S. Tsirkin | a5c262c | 2011-05-20 02:10:44 +0300 | [diff] [blame] | 256 | 	old = vq->vring.avail->idx; | 
 | 257 | 	new = vq->vring.avail->idx = old + vq->num_added; | 
| Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 258 | 	vq->num_added = 0; | 
 | 259 |  | 
 | 260 | 	/* Need to update avail index before checking if we should notify */ | 
| Rusty Russell | 7b21e34 | 2012-01-12 15:44:42 +1030 | [diff] [blame] | 261 | 	virtio_mb(vq); | 
| Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 262 |  | 
| Michael S. Tsirkin | a5c262c | 2011-05-20 02:10:44 +0300 | [diff] [blame] | 263 | 	if (vq->event ? | 
 | 264 | 	    vring_need_event(vring_avail_event(&vq->vring), new, old) : | 
 | 265 | 	    !(vq->vring.used->flags & VRING_USED_F_NO_NOTIFY)) | 
| Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 266 | 		/* Prod other side to tell it about changes. */ | 
 | 267 | 		vq->notify(&vq->vq); | 
 | 268 |  | 
 | 269 | 	END_USE(vq); | 
 | 270 | } | 
| Michael S. Tsirkin | 7c5e9ed | 2010-04-12 16:19:07 +0300 | [diff] [blame] | 271 | EXPORT_SYMBOL_GPL(virtqueue_kick); | 
| Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 272 |  | 
 | 273 | static void detach_buf(struct vring_virtqueue *vq, unsigned int head) | 
 | 274 | { | 
 | 275 | 	unsigned int i; | 
 | 276 |  | 
 | 277 | 	/* Clear data ptr. */ | 
 | 278 | 	vq->data[head] = NULL; | 
 | 279 |  | 
 | 280 | 	/* Put back on free list: find end */ | 
 | 281 | 	i = head; | 
| Mark McLoughlin | 9fa29b9d | 2009-05-11 18:11:45 +0100 | [diff] [blame] | 282 |  | 
 | 283 | 	/* Free the indirect table */ | 
 | 284 | 	if (vq->vring.desc[i].flags & VRING_DESC_F_INDIRECT) | 
 | 285 | 		kfree(phys_to_virt(vq->vring.desc[i].addr)); | 
 | 286 |  | 
| Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 287 | 	while (vq->vring.desc[i].flags & VRING_DESC_F_NEXT) { | 
 | 288 | 		i = vq->vring.desc[i].next; | 
 | 289 | 		vq->num_free++; | 
 | 290 | 	} | 
 | 291 |  | 
 | 292 | 	vq->vring.desc[i].next = vq->free_head; | 
 | 293 | 	vq->free_head = head; | 
 | 294 | 	/* Plus final descriptor */ | 
 | 295 | 	vq->num_free++; | 
 | 296 | } | 
 | 297 |  | 
| Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 298 | static inline bool more_used(const struct vring_virtqueue *vq) | 
 | 299 | { | 
 | 300 | 	return vq->last_used_idx != vq->vring.used->idx; | 
 | 301 | } | 
 | 302 |  | 
| Michael S. Tsirkin | 7c5e9ed | 2010-04-12 16:19:07 +0300 | [diff] [blame] | 303 | void *virtqueue_get_buf(struct virtqueue *_vq, unsigned int *len) | 
| Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 304 | { | 
 | 305 | 	struct vring_virtqueue *vq = to_vvq(_vq); | 
 | 306 | 	void *ret; | 
 | 307 | 	unsigned int i; | 
 | 308 |  | 
 | 309 | 	START_USE(vq); | 
 | 310 |  | 
| Rusty Russell | 5ef8275 | 2008-05-02 21:50:43 -0500 | [diff] [blame] | 311 | 	if (unlikely(vq->broken)) { | 
 | 312 | 		END_USE(vq); | 
 | 313 | 		return NULL; | 
 | 314 | 	} | 
 | 315 |  | 
| Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 316 | 	if (!more_used(vq)) { | 
 | 317 | 		pr_debug("No more buffers in queue\n"); | 
 | 318 | 		END_USE(vq); | 
 | 319 | 		return NULL; | 
 | 320 | 	} | 
 | 321 |  | 
| Michael S. Tsirkin | 2d61ba9 | 2009-10-25 15:28:53 +0200 | [diff] [blame] | 322 | 	/* Only get used array entries after they have been exposed by host. */ | 
| Rusty Russell | 7b21e34 | 2012-01-12 15:44:42 +1030 | [diff] [blame] | 323 | 	virtio_rmb(vq); | 
| Michael S. Tsirkin | 2d61ba9 | 2009-10-25 15:28:53 +0200 | [diff] [blame] | 324 |  | 
| Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 325 | 	i = vq->vring.used->ring[vq->last_used_idx%vq->vring.num].id; | 
 | 326 | 	*len = vq->vring.used->ring[vq->last_used_idx%vq->vring.num].len; | 
 | 327 |  | 
 | 328 | 	if (unlikely(i >= vq->vring.num)) { | 
 | 329 | 		BAD_RING(vq, "id %u out of range\n", i); | 
 | 330 | 		return NULL; | 
 | 331 | 	} | 
 | 332 | 	if (unlikely(!vq->data[i])) { | 
 | 333 | 		BAD_RING(vq, "id %u is not a head!\n", i); | 
 | 334 | 		return NULL; | 
 | 335 | 	} | 
 | 336 |  | 
 | 337 | 	/* detach_buf clears data, so grab it now. */ | 
 | 338 | 	ret = vq->data[i]; | 
 | 339 | 	detach_buf(vq, i); | 
 | 340 | 	vq->last_used_idx++; | 
| Michael S. Tsirkin | a5c262c | 2011-05-20 02:10:44 +0300 | [diff] [blame] | 341 | 	/* If we expect an interrupt for the next entry, tell host | 
 | 342 | 	 * by writing event index and flush out the write before | 
 | 343 | 	 * the read in the next get_buf call. */ | 
 | 344 | 	if (!(vq->vring.avail->flags & VRING_AVAIL_F_NO_INTERRUPT)) { | 
 | 345 | 		vring_used_event(&vq->vring) = vq->last_used_idx; | 
| Rusty Russell | 7b21e34 | 2012-01-12 15:44:42 +1030 | [diff] [blame] | 346 | 		virtio_mb(vq); | 
| Michael S. Tsirkin | a5c262c | 2011-05-20 02:10:44 +0300 | [diff] [blame] | 347 | 	} | 
 | 348 |  | 
| Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 349 | 	END_USE(vq); | 
 | 350 | 	return ret; | 
 | 351 | } | 
| Michael S. Tsirkin | 7c5e9ed | 2010-04-12 16:19:07 +0300 | [diff] [blame] | 352 | EXPORT_SYMBOL_GPL(virtqueue_get_buf); | 
| Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 353 |  | 
| Michael S. Tsirkin | 7c5e9ed | 2010-04-12 16:19:07 +0300 | [diff] [blame] | 354 | void virtqueue_disable_cb(struct virtqueue *_vq) | 
| Rusty Russell | 18445c4 | 2008-02-04 23:49:57 -0500 | [diff] [blame] | 355 | { | 
 | 356 | 	struct vring_virtqueue *vq = to_vvq(_vq); | 
 | 357 |  | 
| Rusty Russell | 18445c4 | 2008-02-04 23:49:57 -0500 | [diff] [blame] | 358 | 	vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT; | 
| Rusty Russell | 18445c4 | 2008-02-04 23:49:57 -0500 | [diff] [blame] | 359 | } | 
| Michael S. Tsirkin | 7c5e9ed | 2010-04-12 16:19:07 +0300 | [diff] [blame] | 360 | EXPORT_SYMBOL_GPL(virtqueue_disable_cb); | 
| Rusty Russell | 18445c4 | 2008-02-04 23:49:57 -0500 | [diff] [blame] | 361 |  | 
| Michael S. Tsirkin | 7c5e9ed | 2010-04-12 16:19:07 +0300 | [diff] [blame] | 362 | bool virtqueue_enable_cb(struct virtqueue *_vq) | 
| Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 363 | { | 
 | 364 | 	struct vring_virtqueue *vq = to_vvq(_vq); | 
 | 365 |  | 
 | 366 | 	START_USE(vq); | 
| Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 367 |  | 
 | 368 | 	/* We optimistically turn back on interrupts, then check if there was | 
 | 369 | 	 * more to do. */ | 
| Michael S. Tsirkin | a5c262c | 2011-05-20 02:10:44 +0300 | [diff] [blame] | 370 | 	/* Depending on the VIRTIO_RING_F_EVENT_IDX feature, we need to | 
 | 371 | 	 * either clear the flags bit or point the event index at the next | 
 | 372 | 	 * entry. Always do both to keep code simple. */ | 
| Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 373 | 	vq->vring.avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT; | 
| Michael S. Tsirkin | a5c262c | 2011-05-20 02:10:44 +0300 | [diff] [blame] | 374 | 	vring_used_event(&vq->vring) = vq->last_used_idx; | 
| Rusty Russell | 7b21e34 | 2012-01-12 15:44:42 +1030 | [diff] [blame] | 375 | 	virtio_mb(vq); | 
| Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 376 | 	if (unlikely(more_used(vq))) { | 
| Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 377 | 		END_USE(vq); | 
 | 378 | 		return false; | 
 | 379 | 	} | 
 | 380 |  | 
 | 381 | 	END_USE(vq); | 
 | 382 | 	return true; | 
 | 383 | } | 
| Michael S. Tsirkin | 7c5e9ed | 2010-04-12 16:19:07 +0300 | [diff] [blame] | 384 | EXPORT_SYMBOL_GPL(virtqueue_enable_cb); | 
| Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 385 |  | 
| Michael S. Tsirkin | 7ab358c | 2011-05-20 02:11:14 +0300 | [diff] [blame] | 386 | bool virtqueue_enable_cb_delayed(struct virtqueue *_vq) | 
 | 387 | { | 
 | 388 | 	struct vring_virtqueue *vq = to_vvq(_vq); | 
 | 389 | 	u16 bufs; | 
 | 390 |  | 
 | 391 | 	START_USE(vq); | 
 | 392 |  | 
 | 393 | 	/* We optimistically turn back on interrupts, then check if there was | 
 | 394 | 	 * more to do. */ | 
 | 395 | 	/* Depending on the VIRTIO_RING_F_USED_EVENT_IDX feature, we need to | 
 | 396 | 	 * either clear the flags bit or point the event index at the next | 
 | 397 | 	 * entry. Always do both to keep code simple. */ | 
 | 398 | 	vq->vring.avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT; | 
 | 399 | 	/* TODO: tune this threshold */ | 
 | 400 | 	bufs = (u16)(vq->vring.avail->idx - vq->last_used_idx) * 3 / 4; | 
 | 401 | 	vring_used_event(&vq->vring) = vq->last_used_idx + bufs; | 
| Rusty Russell | 7b21e34 | 2012-01-12 15:44:42 +1030 | [diff] [blame] | 402 | 	virtio_mb(vq); | 
| Michael S. Tsirkin | 7ab358c | 2011-05-20 02:11:14 +0300 | [diff] [blame] | 403 | 	if (unlikely((u16)(vq->vring.used->idx - vq->last_used_idx) > bufs)) { | 
 | 404 | 		END_USE(vq); | 
 | 405 | 		return false; | 
 | 406 | 	} | 
 | 407 |  | 
 | 408 | 	END_USE(vq); | 
 | 409 | 	return true; | 
 | 410 | } | 
 | 411 | EXPORT_SYMBOL_GPL(virtqueue_enable_cb_delayed); | 
 | 412 |  | 
| Michael S. Tsirkin | 7c5e9ed | 2010-04-12 16:19:07 +0300 | [diff] [blame] | 413 | void *virtqueue_detach_unused_buf(struct virtqueue *_vq) | 
| Shirley Ma | c021eac | 2010-01-18 19:15:23 +0530 | [diff] [blame] | 414 | { | 
 | 415 | 	struct vring_virtqueue *vq = to_vvq(_vq); | 
 | 416 | 	unsigned int i; | 
 | 417 | 	void *buf; | 
 | 418 |  | 
 | 419 | 	START_USE(vq); | 
 | 420 |  | 
 | 421 | 	for (i = 0; i < vq->vring.num; i++) { | 
 | 422 | 		if (!vq->data[i]) | 
 | 423 | 			continue; | 
 | 424 | 		/* detach_buf clears data, so grab it now. */ | 
 | 425 | 		buf = vq->data[i]; | 
 | 426 | 		detach_buf(vq, i); | 
| Amit Shah | b3258ff | 2011-03-16 19:12:10 +0530 | [diff] [blame] | 427 | 		vq->vring.avail->idx--; | 
| Shirley Ma | c021eac | 2010-01-18 19:15:23 +0530 | [diff] [blame] | 428 | 		END_USE(vq); | 
 | 429 | 		return buf; | 
 | 430 | 	} | 
 | 431 | 	/* That should have freed everything. */ | 
 | 432 | 	BUG_ON(vq->num_free != vq->vring.num); | 
 | 433 |  | 
 | 434 | 	END_USE(vq); | 
 | 435 | 	return NULL; | 
 | 436 | } | 
| Michael S. Tsirkin | 7c5e9ed | 2010-04-12 16:19:07 +0300 | [diff] [blame] | 437 | EXPORT_SYMBOL_GPL(virtqueue_detach_unused_buf); | 
| Shirley Ma | c021eac | 2010-01-18 19:15:23 +0530 | [diff] [blame] | 438 |  | 
| Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 439 | irqreturn_t vring_interrupt(int irq, void *_vq) | 
 | 440 | { | 
 | 441 | 	struct vring_virtqueue *vq = to_vvq(_vq); | 
 | 442 |  | 
 | 443 | 	if (!more_used(vq)) { | 
 | 444 | 		pr_debug("virtqueue interrupt with no work for %p\n", vq); | 
 | 445 | 		return IRQ_NONE; | 
 | 446 | 	} | 
 | 447 |  | 
 | 448 | 	if (unlikely(vq->broken)) | 
 | 449 | 		return IRQ_HANDLED; | 
 | 450 |  | 
 | 451 | 	pr_debug("virtqueue callback for %p (%p)\n", vq, vq->vq.callback); | 
| Rusty Russell | 18445c4 | 2008-02-04 23:49:57 -0500 | [diff] [blame] | 452 | 	if (vq->vq.callback) | 
 | 453 | 		vq->vq.callback(&vq->vq); | 
| Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 454 |  | 
 | 455 | 	return IRQ_HANDLED; | 
 | 456 | } | 
| Rusty Russell | c6fd470 | 2008-02-04 23:50:05 -0500 | [diff] [blame] | 457 | EXPORT_SYMBOL_GPL(vring_interrupt); | 
| Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 458 |  | 
| Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 459 | struct virtqueue *vring_new_virtqueue(unsigned int num, | 
| Rusty Russell | 87c7d57 | 2008-12-30 09:26:03 -0600 | [diff] [blame] | 460 | 				      unsigned int vring_align, | 
| Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 461 | 				      struct virtio_device *vdev, | 
| Rusty Russell | 7b21e34 | 2012-01-12 15:44:42 +1030 | [diff] [blame] | 462 | 				      bool weak_barriers, | 
| Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 463 | 				      void *pages, | 
 | 464 | 				      void (*notify)(struct virtqueue *), | 
| Rusty Russell | 9499f5e | 2009-06-12 22:16:35 -0600 | [diff] [blame] | 465 | 				      void (*callback)(struct virtqueue *), | 
 | 466 | 				      const char *name) | 
| Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 467 | { | 
 | 468 | 	struct vring_virtqueue *vq; | 
 | 469 | 	unsigned int i; | 
 | 470 |  | 
| Rusty Russell | 42b36cc | 2007-11-12 13:39:18 +1100 | [diff] [blame] | 471 | 	/* We assume num is a power of 2. */ | 
 | 472 | 	if (num & (num - 1)) { | 
 | 473 | 		dev_warn(&vdev->dev, "Bad virtqueue length %u\n", num); | 
 | 474 | 		return NULL; | 
 | 475 | 	} | 
 | 476 |  | 
| Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 477 | 	vq = kmalloc(sizeof(*vq) + sizeof(void *)*num, GFP_KERNEL); | 
 | 478 | 	if (!vq) | 
 | 479 | 		return NULL; | 
 | 480 |  | 
| Rusty Russell | 87c7d57 | 2008-12-30 09:26:03 -0600 | [diff] [blame] | 481 | 	vring_init(&vq->vring, num, pages, vring_align); | 
| Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 482 | 	vq->vq.callback = callback; | 
 | 483 | 	vq->vq.vdev = vdev; | 
| Rusty Russell | 9499f5e | 2009-06-12 22:16:35 -0600 | [diff] [blame] | 484 | 	vq->vq.name = name; | 
| Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 485 | 	vq->notify = notify; | 
| Rusty Russell | 7b21e34 | 2012-01-12 15:44:42 +1030 | [diff] [blame] | 486 | 	vq->weak_barriers = weak_barriers; | 
| Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 487 | 	vq->broken = false; | 
 | 488 | 	vq->last_used_idx = 0; | 
 | 489 | 	vq->num_added = 0; | 
| Rusty Russell | 9499f5e | 2009-06-12 22:16:35 -0600 | [diff] [blame] | 490 | 	list_add_tail(&vq->vq.list, &vdev->vqs); | 
| Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 491 | #ifdef DEBUG | 
 | 492 | 	vq->in_use = false; | 
 | 493 | #endif | 
 | 494 |  | 
| Mark McLoughlin | 9fa29b9d | 2009-05-11 18:11:45 +0100 | [diff] [blame] | 495 | 	vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC); | 
| Michael S. Tsirkin | a5c262c | 2011-05-20 02:10:44 +0300 | [diff] [blame] | 496 | 	vq->event = virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX); | 
| Mark McLoughlin | 9fa29b9d | 2009-05-11 18:11:45 +0100 | [diff] [blame] | 497 |  | 
| Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 498 | 	/* No callback?  Tell other side not to bother us. */ | 
 | 499 | 	if (!callback) | 
 | 500 | 		vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT; | 
 | 501 |  | 
 | 502 | 	/* Put everything in free lists. */ | 
 | 503 | 	vq->num_free = num; | 
 | 504 | 	vq->free_head = 0; | 
| Amit Shah | 3b87062 | 2010-02-12 10:32:14 +0530 | [diff] [blame] | 505 | 	for (i = 0; i < num-1; i++) { | 
| Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 506 | 		vq->vring.desc[i].next = i+1; | 
| Amit Shah | 3b87062 | 2010-02-12 10:32:14 +0530 | [diff] [blame] | 507 | 		vq->data[i] = NULL; | 
 | 508 | 	} | 
 | 509 | 	vq->data[i] = NULL; | 
| Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 510 |  | 
 | 511 | 	return &vq->vq; | 
 | 512 | } | 
| Rusty Russell | c6fd470 | 2008-02-04 23:50:05 -0500 | [diff] [blame] | 513 | EXPORT_SYMBOL_GPL(vring_new_virtqueue); | 
| Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 514 |  | 
 | 515 | void vring_del_virtqueue(struct virtqueue *vq) | 
 | 516 | { | 
| Rusty Russell | 9499f5e | 2009-06-12 22:16:35 -0600 | [diff] [blame] | 517 | 	list_del(&vq->list); | 
| Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 518 | 	kfree(to_vvq(vq)); | 
 | 519 | } | 
| Rusty Russell | c6fd470 | 2008-02-04 23:50:05 -0500 | [diff] [blame] | 520 | EXPORT_SYMBOL_GPL(vring_del_virtqueue); | 
| Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 521 |  | 
| Rusty Russell | e34f872 | 2008-07-25 12:06:13 -0500 | [diff] [blame] | 522 | /* Manipulates transport-specific feature bits. */ | 
 | 523 | void vring_transport_features(struct virtio_device *vdev) | 
 | 524 | { | 
 | 525 | 	unsigned int i; | 
 | 526 |  | 
 | 527 | 	for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++) { | 
 | 528 | 		switch (i) { | 
| Mark McLoughlin | 9fa29b9d | 2009-05-11 18:11:45 +0100 | [diff] [blame] | 529 | 		case VIRTIO_RING_F_INDIRECT_DESC: | 
 | 530 | 			break; | 
| Michael S. Tsirkin | a5c262c | 2011-05-20 02:10:44 +0300 | [diff] [blame] | 531 | 		case VIRTIO_RING_F_EVENT_IDX: | 
 | 532 | 			break; | 
| Rusty Russell | e34f872 | 2008-07-25 12:06:13 -0500 | [diff] [blame] | 533 | 		default: | 
 | 534 | 			/* We don't understand this bit. */ | 
 | 535 | 			clear_bit(i, vdev->features); | 
 | 536 | 		} | 
 | 537 | 	} | 
 | 538 | } | 
 | 539 | EXPORT_SYMBOL_GPL(vring_transport_features); | 
 | 540 |  | 
| Rick Jones | 8f9f466 | 2011-10-19 08:10:59 +0000 | [diff] [blame] | 541 | /* return the size of the vring within the virtqueue */ | 
 | 542 | unsigned int virtqueue_get_vring_size(struct virtqueue *_vq) | 
 | 543 | { | 
 | 544 |  | 
 | 545 | 	struct vring_virtqueue *vq = to_vvq(_vq); | 
 | 546 |  | 
 | 547 | 	return vq->vring.num; | 
 | 548 | } | 
 | 549 | EXPORT_SYMBOL_GPL(virtqueue_get_vring_size); | 
 | 550 |  | 
| Rusty Russell | c6fd470 | 2008-02-04 23:50:05 -0500 | [diff] [blame] | 551 | MODULE_LICENSE("GPL"); |