virtio_ring.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. #ifdef DEBUG
  24. /* For development, we want to crash whenever the ring is screwed. */
  25. #define BAD_RING(vq, fmt...) \
  26. do { dev_err(&vq->vq.vdev->dev, fmt); BUG(); } while(0)
  27. #define START_USE(vq) \
  28. do { if ((vq)->in_use) panic("in_use = %i\n", (vq)->in_use); (vq)->in_use = __LINE__; mb(); } while(0)
  29. #define END_USE(vq) \
  30. do { BUG_ON(!(vq)->in_use); (vq)->in_use = 0; mb(); } while(0)
  31. #else
  32. #define BAD_RING(vq, fmt...) \
  33. do { dev_err(&vq->vq.vdev->dev, fmt); (vq)->broken = true; } while(0)
  34. #define START_USE(vq)
  35. #define END_USE(vq)
  36. #endif
  37. struct vring_virtqueue
  38. {
  39. struct virtqueue vq;
  40. /* Actual memory layout for this queue */
  41. struct vring vring;
  42. /* Other side has made a mess, don't try any more. */
  43. bool broken;
  44. /* Number of free buffers */
  45. unsigned int num_free;
  46. /* Head of free buffer list. */
  47. unsigned int free_head;
  48. /* Number we've added since last sync. */
  49. unsigned int num_added;
  50. /* Last used index we've seen. */
  51. u16 last_used_idx;
  52. /* How to notify other side. FIXME: commonalize hcalls! */
  53. void (*notify)(struct virtqueue *vq);
  54. #ifdef DEBUG
  55. /* They're supposed to lock for us. */
  56. unsigned int in_use;
  57. #endif
  58. /* Tokens for callbacks. */
  59. void *data[];
  60. };
  61. #define to_vvq(_vq) container_of(_vq, struct vring_virtqueue, vq)
  62. static int vring_add_buf(struct virtqueue *_vq,
  63. struct scatterlist sg[],
  64. unsigned int out,
  65. unsigned int in,
  66. void *data)
  67. {
  68. struct vring_virtqueue *vq = to_vvq(_vq);
  69. unsigned int i, avail, head, uninitialized_var(prev);
  70. BUG_ON(data == NULL);
  71. BUG_ON(out + in > vq->vring.num);
  72. BUG_ON(out + in == 0);
  73. START_USE(vq);
  74. if (vq->num_free < out + in) {
  75. pr_debug("Can't add buf len %i - avail = %i\n",
  76. out + in, vq->num_free);
  77. /* FIXME: for historical reasons, we force a notify here if
  78. * there are outgoing parts to the buffer. Presumably the
  79. * host should service the ring ASAP. */
  80. if (out)
  81. vq->notify(&vq->vq);
  82. END_USE(vq);
  83. return -ENOSPC;
  84. }
  85. /* We're about to use some buffers from the free list. */
  86. vq->num_free -= out + in;
  87. head = vq->free_head;
  88. for (i = vq->free_head; out; i = vq->vring.desc[i].next, out--) {
  89. vq->vring.desc[i].flags = VRING_DESC_F_NEXT;
  90. vq->vring.desc[i].addr = sg_phys(sg);
  91. vq->vring.desc[i].len = sg->length;
  92. prev = i;
  93. sg++;
  94. }
  95. for (; in; i = vq->vring.desc[i].next, in--) {
  96. vq->vring.desc[i].flags = VRING_DESC_F_NEXT|VRING_DESC_F_WRITE;
  97. vq->vring.desc[i].addr = sg_phys(sg);
  98. vq->vring.desc[i].len = sg->length;
  99. prev = i;
  100. sg++;
  101. }
  102. /* Last one doesn't continue. */
  103. vq->vring.desc[prev].flags &= ~VRING_DESC_F_NEXT;
  104. /* Update free pointer */
  105. vq->free_head = i;
  106. /* Set token. */
  107. vq->data[head] = data;
  108. /* Put entry in available array (but don't update avail->idx until they
  109. * do sync). FIXME: avoid modulus here? */
  110. avail = (vq->vring.avail->idx + vq->num_added++) % vq->vring.num;
  111. vq->vring.avail->ring[avail] = head;
  112. pr_debug("Added buffer head %i to %p\n", head, vq);
  113. END_USE(vq);
  114. return 0;
  115. }
  116. static void vring_kick(struct virtqueue *_vq)
  117. {
  118. struct vring_virtqueue *vq = to_vvq(_vq);
  119. START_USE(vq);
  120. /* Descriptors and available array need to be set before we expose the
  121. * new available array entries. */
  122. wmb();
  123. vq->vring.avail->idx += vq->num_added;
  124. vq->num_added = 0;
  125. /* Need to update avail index before checking if we should notify */
  126. mb();
  127. if (!(vq->vring.used->flags & VRING_USED_F_NO_NOTIFY))
  128. /* Prod other side to tell it about changes. */
  129. vq->notify(&vq->vq);
  130. END_USE(vq);
  131. }
  132. static void detach_buf(struct vring_virtqueue *vq, unsigned int head)
  133. {
  134. unsigned int i;
  135. /* Clear data ptr. */
  136. vq->data[head] = NULL;
  137. /* Put back on free list: find end */
  138. i = head;
  139. while (vq->vring.desc[i].flags & VRING_DESC_F_NEXT) {
  140. i = vq->vring.desc[i].next;
  141. vq->num_free++;
  142. }
  143. vq->vring.desc[i].next = vq->free_head;
  144. vq->free_head = head;
  145. /* Plus final descriptor */
  146. vq->num_free++;
  147. }
  148. static inline bool more_used(const struct vring_virtqueue *vq)
  149. {
  150. return vq->last_used_idx != vq->vring.used->idx;
  151. }
  152. static void *vring_get_buf(struct virtqueue *_vq, unsigned int *len)
  153. {
  154. struct vring_virtqueue *vq = to_vvq(_vq);
  155. void *ret;
  156. unsigned int i;
  157. START_USE(vq);
  158. if (unlikely(vq->broken)) {
  159. END_USE(vq);
  160. return NULL;
  161. }
  162. if (!more_used(vq)) {
  163. pr_debug("No more buffers in queue\n");
  164. END_USE(vq);
  165. return NULL;
  166. }
  167. i = vq->vring.used->ring[vq->last_used_idx%vq->vring.num].id;
  168. *len = vq->vring.used->ring[vq->last_used_idx%vq->vring.num].len;
  169. if (unlikely(i >= vq->vring.num)) {
  170. BAD_RING(vq, "id %u out of range\n", i);
  171. return NULL;
  172. }
  173. if (unlikely(!vq->data[i])) {
  174. BAD_RING(vq, "id %u is not a head!\n", i);
  175. return NULL;
  176. }
  177. /* detach_buf clears data, so grab it now. */
  178. ret = vq->data[i];
  179. detach_buf(vq, i);
  180. vq->last_used_idx++;
  181. END_USE(vq);
  182. return ret;
  183. }
  184. static void vring_disable_cb(struct virtqueue *_vq)
  185. {
  186. struct vring_virtqueue *vq = to_vvq(_vq);
  187. vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
  188. }
  189. static bool vring_enable_cb(struct virtqueue *_vq)
  190. {
  191. struct vring_virtqueue *vq = to_vvq(_vq);
  192. START_USE(vq);
  193. /* We optimistically turn back on interrupts, then check if there was
  194. * more to do. */
  195. vq->vring.avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT;
  196. mb();
  197. if (unlikely(more_used(vq))) {
  198. END_USE(vq);
  199. return false;
  200. }
  201. END_USE(vq);
  202. return true;
  203. }
  204. irqreturn_t vring_interrupt(int irq, void *_vq)
  205. {
  206. struct vring_virtqueue *vq = to_vvq(_vq);
  207. if (!more_used(vq)) {
  208. pr_debug("virtqueue interrupt with no work for %p\n", vq);
  209. return IRQ_NONE;
  210. }
  211. if (unlikely(vq->broken))
  212. return IRQ_HANDLED;
  213. pr_debug("virtqueue callback for %p (%p)\n", vq, vq->vq.callback);
  214. if (vq->vq.callback)
  215. vq->vq.callback(&vq->vq);
  216. return IRQ_HANDLED;
  217. }
  218. EXPORT_SYMBOL_GPL(vring_interrupt);
  219. static struct virtqueue_ops vring_vq_ops = {
  220. .add_buf = vring_add_buf,
  221. .get_buf = vring_get_buf,
  222. .kick = vring_kick,
  223. .disable_cb = vring_disable_cb,
  224. .enable_cb = vring_enable_cb,
  225. };
  226. struct virtqueue *vring_new_virtqueue(unsigned int num,
  227. struct virtio_device *vdev,
  228. void *pages,
  229. void (*notify)(struct virtqueue *),
  230. void (*callback)(struct virtqueue *))
  231. {
  232. struct vring_virtqueue *vq;
  233. unsigned int i;
  234. /* We assume num is a power of 2. */
  235. if (num & (num - 1)) {
  236. dev_warn(&vdev->dev, "Bad virtqueue length %u\n", num);
  237. return NULL;
  238. }
  239. vq = kmalloc(sizeof(*vq) + sizeof(void *)*num, GFP_KERNEL);
  240. if (!vq)
  241. return NULL;
  242. vring_init(&vq->vring, num, pages, PAGE_SIZE);
  243. vq->vq.callback = callback;
  244. vq->vq.vdev = vdev;
  245. vq->vq.vq_ops = &vring_vq_ops;
  246. vq->notify = notify;
  247. vq->broken = false;
  248. vq->last_used_idx = 0;
  249. vq->num_added = 0;
  250. #ifdef DEBUG
  251. vq->in_use = false;
  252. #endif
  253. /* No callback? Tell other side not to bother us. */
  254. if (!callback)
  255. vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
  256. /* Put everything in free lists. */
  257. vq->num_free = num;
  258. vq->free_head = 0;
  259. for (i = 0; i < num-1; i++)
  260. vq->vring.desc[i].next = i+1;
  261. return &vq->vq;
  262. }
  263. EXPORT_SYMBOL_GPL(vring_new_virtqueue);
  264. void vring_del_virtqueue(struct virtqueue *vq)
  265. {
  266. kfree(to_vvq(vq));
  267. }
  268. EXPORT_SYMBOL_GPL(vring_del_virtqueue);
  269. /* Manipulates transport-specific feature bits. */
  270. void vring_transport_features(struct virtio_device *vdev)
  271. {
  272. unsigned int i;
  273. for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++) {
  274. switch (i) {
  275. default:
  276. /* We don't understand this bit. */
  277. clear_bit(i, vdev->features);
  278. }
  279. }
  280. }
  281. EXPORT_SYMBOL_GPL(vring_transport_features);
  282. MODULE_LICENSE("GPL");