virtio_ring.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. /* Caller is supposed to guarantee no reentry. */
  28. #define START_USE(_vq) \
  29. do { \
  30. if ((_vq)->in_use) \
  31. panic("in_use = %i\n", (_vq)->in_use); \
  32. (_vq)->in_use = __LINE__; \
  33. mb(); \
  34. } while(0)
  35. #define END_USE(_vq) \
  36. do { BUG_ON(!(_vq)->in_use); (_vq)->in_use = 0; mb(); } while(0)
  37. #else
  38. #define BAD_RING(_vq, fmt...) \
  39. do { dev_err(&_vq->vq.vdev->dev, fmt); (_vq)->broken = true; } while(0)
  40. #define START_USE(vq)
  41. #define END_USE(vq)
  42. #endif
  43. struct vring_virtqueue
  44. {
  45. struct virtqueue vq;
  46. /* Actual memory layout for this queue */
  47. struct vring vring;
  48. /* Other side has made a mess, don't try any more. */
  49. bool broken;
  50. /* Number of free buffers */
  51. unsigned int num_free;
  52. /* Head of free buffer list. */
  53. unsigned int free_head;
  54. /* Number we've added since last sync. */
  55. unsigned int num_added;
  56. /* Last used index we've seen. */
  57. u16 last_used_idx;
  58. /* How to notify other side. FIXME: commonalize hcalls! */
  59. void (*notify)(struct virtqueue *vq);
  60. #ifdef DEBUG
  61. /* They're supposed to lock for us. */
  62. unsigned int in_use;
  63. #endif
  64. /* Tokens for callbacks. */
  65. void *data[];
  66. };
  67. #define to_vvq(_vq) container_of(_vq, struct vring_virtqueue, vq)
  68. static int vring_add_buf(struct virtqueue *_vq,
  69. struct scatterlist sg[],
  70. unsigned int out,
  71. unsigned int in,
  72. void *data)
  73. {
  74. struct vring_virtqueue *vq = to_vvq(_vq);
  75. unsigned int i, avail, head, uninitialized_var(prev);
  76. BUG_ON(data == NULL);
  77. BUG_ON(out + in > vq->vring.num);
  78. BUG_ON(out + in == 0);
  79. START_USE(vq);
  80. if (vq->num_free < out + in) {
  81. pr_debug("Can't add buf len %i - avail = %i\n",
  82. out + in, vq->num_free);
  83. /* FIXME: for historical reasons, we force a notify here if
  84. * there are outgoing parts to the buffer. Presumably the
  85. * host should service the ring ASAP. */
  86. if (out)
  87. vq->notify(&vq->vq);
  88. END_USE(vq);
  89. return -ENOSPC;
  90. }
  91. /* We're about to use some buffers from the free list. */
  92. vq->num_free -= out + in;
  93. head = vq->free_head;
  94. for (i = vq->free_head; out; i = vq->vring.desc[i].next, out--) {
  95. vq->vring.desc[i].flags = VRING_DESC_F_NEXT;
  96. vq->vring.desc[i].addr = sg_phys(sg);
  97. vq->vring.desc[i].len = sg->length;
  98. prev = i;
  99. sg++;
  100. }
  101. for (; in; i = vq->vring.desc[i].next, in--) {
  102. vq->vring.desc[i].flags = VRING_DESC_F_NEXT|VRING_DESC_F_WRITE;
  103. vq->vring.desc[i].addr = sg_phys(sg);
  104. vq->vring.desc[i].len = sg->length;
  105. prev = i;
  106. sg++;
  107. }
  108. /* Last one doesn't continue. */
  109. vq->vring.desc[prev].flags &= ~VRING_DESC_F_NEXT;
  110. /* Update free pointer */
  111. vq->free_head = i;
  112. /* Set token. */
  113. vq->data[head] = data;
  114. /* Put entry in available array (but don't update avail->idx until they
  115. * do sync). FIXME: avoid modulus here? */
  116. avail = (vq->vring.avail->idx + vq->num_added++) % vq->vring.num;
  117. vq->vring.avail->ring[avail] = head;
  118. pr_debug("Added buffer head %i to %p\n", head, vq);
  119. END_USE(vq);
  120. return 0;
  121. }
  122. static void vring_kick(struct virtqueue *_vq)
  123. {
  124. struct vring_virtqueue *vq = to_vvq(_vq);
  125. START_USE(vq);
  126. /* Descriptors and available array need to be set before we expose the
  127. * new available array entries. */
  128. wmb();
  129. vq->vring.avail->idx += vq->num_added;
  130. vq->num_added = 0;
  131. /* Need to update avail index before checking if we should notify */
  132. mb();
  133. if (!(vq->vring.used->flags & VRING_USED_F_NO_NOTIFY))
  134. /* Prod other side to tell it about changes. */
  135. vq->notify(&vq->vq);
  136. END_USE(vq);
  137. }
  138. static void detach_buf(struct vring_virtqueue *vq, unsigned int head)
  139. {
  140. unsigned int i;
  141. /* Clear data ptr. */
  142. vq->data[head] = NULL;
  143. /* Put back on free list: find end */
  144. i = head;
  145. while (vq->vring.desc[i].flags & VRING_DESC_F_NEXT) {
  146. i = vq->vring.desc[i].next;
  147. vq->num_free++;
  148. }
  149. vq->vring.desc[i].next = vq->free_head;
  150. vq->free_head = head;
  151. /* Plus final descriptor */
  152. vq->num_free++;
  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 (unlikely(vq->broken)) {
  165. END_USE(vq);
  166. return NULL;
  167. }
  168. if (!more_used(vq)) {
  169. pr_debug("No more buffers in queue\n");
  170. END_USE(vq);
  171. return NULL;
  172. }
  173. i = vq->vring.used->ring[vq->last_used_idx%vq->vring.num].id;
  174. *len = vq->vring.used->ring[vq->last_used_idx%vq->vring.num].len;
  175. if (unlikely(i >= vq->vring.num)) {
  176. BAD_RING(vq, "id %u out of range\n", i);
  177. return NULL;
  178. }
  179. if (unlikely(!vq->data[i])) {
  180. BAD_RING(vq, "id %u is not a head!\n", i);
  181. return NULL;
  182. }
  183. /* detach_buf clears data, so grab it now. */
  184. ret = vq->data[i];
  185. detach_buf(vq, i);
  186. vq->last_used_idx++;
  187. END_USE(vq);
  188. return ret;
  189. }
  190. static void vring_disable_cb(struct virtqueue *_vq)
  191. {
  192. struct vring_virtqueue *vq = to_vvq(_vq);
  193. vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
  194. }
  195. static bool vring_enable_cb(struct virtqueue *_vq)
  196. {
  197. struct vring_virtqueue *vq = to_vvq(_vq);
  198. START_USE(vq);
  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. END_USE(vq);
  205. return false;
  206. }
  207. END_USE(vq);
  208. return true;
  209. }
  210. irqreturn_t vring_interrupt(int irq, void *_vq)
  211. {
  212. struct vring_virtqueue *vq = to_vvq(_vq);
  213. if (!more_used(vq)) {
  214. pr_debug("virtqueue interrupt with no work for %p\n", vq);
  215. return IRQ_NONE;
  216. }
  217. if (unlikely(vq->broken))
  218. return IRQ_HANDLED;
  219. pr_debug("virtqueue callback for %p (%p)\n", vq, vq->vq.callback);
  220. if (vq->vq.callback)
  221. vq->vq.callback(&vq->vq);
  222. return IRQ_HANDLED;
  223. }
  224. EXPORT_SYMBOL_GPL(vring_interrupt);
  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. };
  232. struct virtqueue *vring_new_virtqueue(unsigned int num,
  233. unsigned int vring_align,
  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, vring_align);
  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. EXPORT_SYMBOL_GPL(vring_new_virtqueue);
  271. void vring_del_virtqueue(struct virtqueue *vq)
  272. {
  273. kfree(to_vvq(vq));
  274. }
  275. EXPORT_SYMBOL_GPL(vring_del_virtqueue);
  276. /* Manipulates transport-specific feature bits. */
  277. void vring_transport_features(struct virtio_device *vdev)
  278. {
  279. unsigned int i;
  280. for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++) {
  281. switch (i) {
  282. default:
  283. /* We don't understand this bit. */
  284. clear_bit(i, vdev->features);
  285. }
  286. }
  287. }
  288. EXPORT_SYMBOL_GPL(vring_transport_features);
  289. MODULE_LICENSE("GPL");