virtio_ring.c 8.1 KB

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