virtio_ring.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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. /* Host supports indirect buffers */
  60. bool indirect;
  61. /* Number of free buffers */
  62. unsigned int num_free;
  63. /* Head of free buffer list. */
  64. unsigned int free_head;
  65. /* Number we've added since last sync. */
  66. unsigned int num_added;
  67. /* Last used index we've seen. */
  68. u16 last_used_idx;
  69. /* How to notify other side. FIXME: commonalize hcalls! */
  70. void (*notify)(struct virtqueue *vq);
  71. #ifdef DEBUG
  72. /* They're supposed to lock for us. */
  73. unsigned int in_use;
  74. #endif
  75. /* Tokens for callbacks. */
  76. void *data[];
  77. };
  78. #define to_vvq(_vq) container_of(_vq, struct vring_virtqueue, vq)
  79. /* Set up an indirect table of descriptors and add it to the queue. */
  80. static int vring_add_indirect(struct vring_virtqueue *vq,
  81. struct scatterlist sg[],
  82. unsigned int out,
  83. unsigned int in)
  84. {
  85. struct vring_desc *desc;
  86. unsigned head;
  87. int i;
  88. desc = kmalloc((out + in) * sizeof(struct vring_desc), GFP_ATOMIC);
  89. if (!desc)
  90. return vq->vring.num;
  91. /* Transfer entries from the sg list into the indirect page */
  92. for (i = 0; i < out; i++) {
  93. desc[i].flags = VRING_DESC_F_NEXT;
  94. desc[i].addr = sg_phys(sg);
  95. desc[i].len = sg->length;
  96. desc[i].next = i+1;
  97. sg++;
  98. }
  99. for (; i < (out + in); i++) {
  100. desc[i].flags = VRING_DESC_F_NEXT|VRING_DESC_F_WRITE;
  101. desc[i].addr = sg_phys(sg);
  102. desc[i].len = sg->length;
  103. desc[i].next = i+1;
  104. sg++;
  105. }
  106. /* Last one doesn't continue. */
  107. desc[i-1].flags &= ~VRING_DESC_F_NEXT;
  108. desc[i-1].next = 0;
  109. /* We're about to use a buffer */
  110. vq->num_free--;
  111. /* Use a single buffer which doesn't continue */
  112. head = vq->free_head;
  113. vq->vring.desc[head].flags = VRING_DESC_F_INDIRECT;
  114. vq->vring.desc[head].addr = virt_to_phys(desc);
  115. vq->vring.desc[head].len = i * sizeof(struct vring_desc);
  116. /* Update free pointer */
  117. vq->free_head = vq->vring.desc[head].next;
  118. return head;
  119. }
  120. static int vring_add_buf(struct virtqueue *_vq,
  121. struct scatterlist sg[],
  122. unsigned int out,
  123. unsigned int in,
  124. void *data)
  125. {
  126. struct vring_virtqueue *vq = to_vvq(_vq);
  127. unsigned int i, avail, head, uninitialized_var(prev);
  128. START_USE(vq);
  129. BUG_ON(data == NULL);
  130. /* If the host supports indirect descriptor tables, and we have multiple
  131. * buffers, then go indirect. FIXME: tune this threshold */
  132. if (vq->indirect && (out + in) > 1 && vq->num_free) {
  133. head = vring_add_indirect(vq, sg, out, in);
  134. if (head != vq->vring.num)
  135. goto add_head;
  136. }
  137. BUG_ON(out + in > vq->vring.num);
  138. BUG_ON(out + in == 0);
  139. if (vq->num_free < out + in) {
  140. pr_debug("Can't add buf len %i - avail = %i\n",
  141. out + in, vq->num_free);
  142. /* FIXME: for historical reasons, we force a notify here if
  143. * there are outgoing parts to the buffer. Presumably the
  144. * host should service the ring ASAP. */
  145. if (out)
  146. vq->notify(&vq->vq);
  147. END_USE(vq);
  148. return -ENOSPC;
  149. }
  150. /* We're about to use some buffers from the free list. */
  151. vq->num_free -= out + in;
  152. head = vq->free_head;
  153. for (i = vq->free_head; out; i = vq->vring.desc[i].next, out--) {
  154. vq->vring.desc[i].flags = VRING_DESC_F_NEXT;
  155. vq->vring.desc[i].addr = sg_phys(sg);
  156. vq->vring.desc[i].len = sg->length;
  157. prev = i;
  158. sg++;
  159. }
  160. for (; in; i = vq->vring.desc[i].next, in--) {
  161. vq->vring.desc[i].flags = VRING_DESC_F_NEXT|VRING_DESC_F_WRITE;
  162. vq->vring.desc[i].addr = sg_phys(sg);
  163. vq->vring.desc[i].len = sg->length;
  164. prev = i;
  165. sg++;
  166. }
  167. /* Last one doesn't continue. */
  168. vq->vring.desc[prev].flags &= ~VRING_DESC_F_NEXT;
  169. /* Update free pointer */
  170. vq->free_head = i;
  171. add_head:
  172. /* Set token. */
  173. vq->data[head] = data;
  174. /* Put entry in available array (but don't update avail->idx until they
  175. * do sync). FIXME: avoid modulus here? */
  176. avail = (vq->vring.avail->idx + vq->num_added++) % vq->vring.num;
  177. vq->vring.avail->ring[avail] = head;
  178. pr_debug("Added buffer head %i to %p\n", head, vq);
  179. END_USE(vq);
  180. /* If we're indirect, we can fit many (assuming not OOM). */
  181. if (vq->indirect)
  182. return vq->num_free ? vq->vring.num : 0;
  183. return vq->num_free;
  184. }
  185. static void vring_kick(struct virtqueue *_vq)
  186. {
  187. struct vring_virtqueue *vq = to_vvq(_vq);
  188. START_USE(vq);
  189. /* Descriptors and available array need to be set before we expose the
  190. * new available array entries. */
  191. wmb();
  192. vq->vring.avail->idx += vq->num_added;
  193. vq->num_added = 0;
  194. /* Need to update avail index before checking if we should notify */
  195. mb();
  196. if (!(vq->vring.used->flags & VRING_USED_F_NO_NOTIFY))
  197. /* Prod other side to tell it about changes. */
  198. vq->notify(&vq->vq);
  199. END_USE(vq);
  200. }
  201. static void detach_buf(struct vring_virtqueue *vq, unsigned int head)
  202. {
  203. unsigned int i;
  204. /* Clear data ptr. */
  205. vq->data[head] = NULL;
  206. /* Put back on free list: find end */
  207. i = head;
  208. /* Free the indirect table */
  209. if (vq->vring.desc[i].flags & VRING_DESC_F_INDIRECT)
  210. kfree(phys_to_virt(vq->vring.desc[i].addr));
  211. while (vq->vring.desc[i].flags & VRING_DESC_F_NEXT) {
  212. i = vq->vring.desc[i].next;
  213. vq->num_free++;
  214. }
  215. vq->vring.desc[i].next = vq->free_head;
  216. vq->free_head = head;
  217. /* Plus final descriptor */
  218. vq->num_free++;
  219. }
  220. static inline bool more_used(const struct vring_virtqueue *vq)
  221. {
  222. return vq->last_used_idx != vq->vring.used->idx;
  223. }
  224. static void *vring_get_buf(struct virtqueue *_vq, unsigned int *len)
  225. {
  226. struct vring_virtqueue *vq = to_vvq(_vq);
  227. void *ret;
  228. unsigned int i;
  229. START_USE(vq);
  230. if (unlikely(vq->broken)) {
  231. END_USE(vq);
  232. return NULL;
  233. }
  234. if (!more_used(vq)) {
  235. pr_debug("No more buffers in queue\n");
  236. END_USE(vq);
  237. return NULL;
  238. }
  239. /* Only get used array entries after they have been exposed by host. */
  240. rmb();
  241. i = vq->vring.used->ring[vq->last_used_idx%vq->vring.num].id;
  242. *len = vq->vring.used->ring[vq->last_used_idx%vq->vring.num].len;
  243. if (unlikely(i >= vq->vring.num)) {
  244. BAD_RING(vq, "id %u out of range\n", i);
  245. return NULL;
  246. }
  247. if (unlikely(!vq->data[i])) {
  248. BAD_RING(vq, "id %u is not a head!\n", i);
  249. return NULL;
  250. }
  251. /* detach_buf clears data, so grab it now. */
  252. ret = vq->data[i];
  253. detach_buf(vq, i);
  254. vq->last_used_idx++;
  255. END_USE(vq);
  256. return ret;
  257. }
  258. static void vring_disable_cb(struct virtqueue *_vq)
  259. {
  260. struct vring_virtqueue *vq = to_vvq(_vq);
  261. vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
  262. }
  263. static bool vring_enable_cb(struct virtqueue *_vq)
  264. {
  265. struct vring_virtqueue *vq = to_vvq(_vq);
  266. START_USE(vq);
  267. /* We optimistically turn back on interrupts, then check if there was
  268. * more to do. */
  269. vq->vring.avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT;
  270. mb();
  271. if (unlikely(more_used(vq))) {
  272. END_USE(vq);
  273. return false;
  274. }
  275. END_USE(vq);
  276. return true;
  277. }
  278. irqreturn_t vring_interrupt(int irq, void *_vq)
  279. {
  280. struct vring_virtqueue *vq = to_vvq(_vq);
  281. if (!more_used(vq)) {
  282. pr_debug("virtqueue interrupt with no work for %p\n", vq);
  283. return IRQ_NONE;
  284. }
  285. if (unlikely(vq->broken))
  286. return IRQ_HANDLED;
  287. pr_debug("virtqueue callback for %p (%p)\n", vq, vq->vq.callback);
  288. if (vq->vq.callback)
  289. vq->vq.callback(&vq->vq);
  290. return IRQ_HANDLED;
  291. }
  292. EXPORT_SYMBOL_GPL(vring_interrupt);
  293. static struct virtqueue_ops vring_vq_ops = {
  294. .add_buf = vring_add_buf,
  295. .get_buf = vring_get_buf,
  296. .kick = vring_kick,
  297. .disable_cb = vring_disable_cb,
  298. .enable_cb = vring_enable_cb,
  299. };
  300. struct virtqueue *vring_new_virtqueue(unsigned int num,
  301. unsigned int vring_align,
  302. struct virtio_device *vdev,
  303. void *pages,
  304. void (*notify)(struct virtqueue *),
  305. void (*callback)(struct virtqueue *),
  306. const char *name)
  307. {
  308. struct vring_virtqueue *vq;
  309. unsigned int i;
  310. /* We assume num is a power of 2. */
  311. if (num & (num - 1)) {
  312. dev_warn(&vdev->dev, "Bad virtqueue length %u\n", num);
  313. return NULL;
  314. }
  315. vq = kmalloc(sizeof(*vq) + sizeof(void *)*num, GFP_KERNEL);
  316. if (!vq)
  317. return NULL;
  318. vring_init(&vq->vring, num, pages, vring_align);
  319. vq->vq.callback = callback;
  320. vq->vq.vdev = vdev;
  321. vq->vq.vq_ops = &vring_vq_ops;
  322. vq->vq.name = name;
  323. vq->notify = notify;
  324. vq->broken = false;
  325. vq->last_used_idx = 0;
  326. vq->num_added = 0;
  327. list_add_tail(&vq->vq.list, &vdev->vqs);
  328. #ifdef DEBUG
  329. vq->in_use = false;
  330. #endif
  331. vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC);
  332. /* No callback? Tell other side not to bother us. */
  333. if (!callback)
  334. vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
  335. /* Put everything in free lists. */
  336. vq->num_free = num;
  337. vq->free_head = 0;
  338. for (i = 0; i < num-1; i++)
  339. vq->vring.desc[i].next = i+1;
  340. return &vq->vq;
  341. }
  342. EXPORT_SYMBOL_GPL(vring_new_virtqueue);
  343. void vring_del_virtqueue(struct virtqueue *vq)
  344. {
  345. list_del(&vq->list);
  346. kfree(to_vvq(vq));
  347. }
  348. EXPORT_SYMBOL_GPL(vring_del_virtqueue);
  349. /* Manipulates transport-specific feature bits. */
  350. void vring_transport_features(struct virtio_device *vdev)
  351. {
  352. unsigned int i;
  353. for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++) {
  354. switch (i) {
  355. case VIRTIO_RING_F_INDIRECT_DESC:
  356. break;
  357. default:
  358. /* We don't understand this bit. */
  359. clear_bit(i, vdev->features);
  360. }
  361. }
  362. }
  363. EXPORT_SYMBOL_GPL(vring_transport_features);
  364. MODULE_LICENSE("GPL");