virtio_ring.c 8.6 KB

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