virtio_ring.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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. return 0;
  181. }
  182. static void vring_kick(struct virtqueue *_vq)
  183. {
  184. struct vring_virtqueue *vq = to_vvq(_vq);
  185. START_USE(vq);
  186. /* Descriptors and available array need to be set before we expose the
  187. * new available array entries. */
  188. wmb();
  189. vq->vring.avail->idx += vq->num_added;
  190. vq->num_added = 0;
  191. /* Need to update avail index before checking if we should notify */
  192. mb();
  193. if (!(vq->vring.used->flags & VRING_USED_F_NO_NOTIFY))
  194. /* Prod other side to tell it about changes. */
  195. vq->notify(&vq->vq);
  196. END_USE(vq);
  197. }
  198. static void detach_buf(struct vring_virtqueue *vq, unsigned int head)
  199. {
  200. unsigned int i;
  201. /* Clear data ptr. */
  202. vq->data[head] = NULL;
  203. /* Put back on free list: find end */
  204. i = head;
  205. /* Free the indirect table */
  206. if (vq->vring.desc[i].flags & VRING_DESC_F_INDIRECT)
  207. kfree(phys_to_virt(vq->vring.desc[i].addr));
  208. while (vq->vring.desc[i].flags & VRING_DESC_F_NEXT) {
  209. i = vq->vring.desc[i].next;
  210. vq->num_free++;
  211. }
  212. vq->vring.desc[i].next = vq->free_head;
  213. vq->free_head = head;
  214. /* Plus final descriptor */
  215. vq->num_free++;
  216. }
  217. static inline bool more_used(const struct vring_virtqueue *vq)
  218. {
  219. return vq->last_used_idx != vq->vring.used->idx;
  220. }
  221. static void *vring_get_buf(struct virtqueue *_vq, unsigned int *len)
  222. {
  223. struct vring_virtqueue *vq = to_vvq(_vq);
  224. void *ret;
  225. unsigned int i;
  226. START_USE(vq);
  227. if (unlikely(vq->broken)) {
  228. END_USE(vq);
  229. return NULL;
  230. }
  231. if (!more_used(vq)) {
  232. pr_debug("No more buffers in queue\n");
  233. END_USE(vq);
  234. return NULL;
  235. }
  236. i = vq->vring.used->ring[vq->last_used_idx%vq->vring.num].id;
  237. *len = vq->vring.used->ring[vq->last_used_idx%vq->vring.num].len;
  238. if (unlikely(i >= vq->vring.num)) {
  239. BAD_RING(vq, "id %u out of range\n", i);
  240. return NULL;
  241. }
  242. if (unlikely(!vq->data[i])) {
  243. BAD_RING(vq, "id %u is not a head!\n", i);
  244. return NULL;
  245. }
  246. /* detach_buf clears data, so grab it now. */
  247. ret = vq->data[i];
  248. detach_buf(vq, i);
  249. vq->last_used_idx++;
  250. END_USE(vq);
  251. return ret;
  252. }
  253. static void vring_disable_cb(struct virtqueue *_vq)
  254. {
  255. struct vring_virtqueue *vq = to_vvq(_vq);
  256. vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
  257. }
  258. static bool vring_enable_cb(struct virtqueue *_vq)
  259. {
  260. struct vring_virtqueue *vq = to_vvq(_vq);
  261. START_USE(vq);
  262. /* We optimistically turn back on interrupts, then check if there was
  263. * more to do. */
  264. vq->vring.avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT;
  265. mb();
  266. if (unlikely(more_used(vq))) {
  267. END_USE(vq);
  268. return false;
  269. }
  270. END_USE(vq);
  271. return true;
  272. }
  273. irqreturn_t vring_interrupt(int irq, void *_vq)
  274. {
  275. struct vring_virtqueue *vq = to_vvq(_vq);
  276. if (!more_used(vq)) {
  277. pr_debug("virtqueue interrupt with no work for %p\n", vq);
  278. return IRQ_NONE;
  279. }
  280. if (unlikely(vq->broken))
  281. return IRQ_HANDLED;
  282. pr_debug("virtqueue callback for %p (%p)\n", vq, vq->vq.callback);
  283. if (vq->vq.callback)
  284. vq->vq.callback(&vq->vq);
  285. return IRQ_HANDLED;
  286. }
  287. EXPORT_SYMBOL_GPL(vring_interrupt);
  288. static struct virtqueue_ops vring_vq_ops = {
  289. .add_buf = vring_add_buf,
  290. .get_buf = vring_get_buf,
  291. .kick = vring_kick,
  292. .disable_cb = vring_disable_cb,
  293. .enable_cb = vring_enable_cb,
  294. };
  295. struct virtqueue *vring_new_virtqueue(unsigned int num,
  296. unsigned int vring_align,
  297. struct virtio_device *vdev,
  298. void *pages,
  299. void (*notify)(struct virtqueue *),
  300. void (*callback)(struct virtqueue *),
  301. const char *name)
  302. {
  303. struct vring_virtqueue *vq;
  304. unsigned int i;
  305. /* We assume num is a power of 2. */
  306. if (num & (num - 1)) {
  307. dev_warn(&vdev->dev, "Bad virtqueue length %u\n", num);
  308. return NULL;
  309. }
  310. vq = kmalloc(sizeof(*vq) + sizeof(void *)*num, GFP_KERNEL);
  311. if (!vq)
  312. return NULL;
  313. vring_init(&vq->vring, num, pages, vring_align);
  314. vq->vq.callback = callback;
  315. vq->vq.vdev = vdev;
  316. vq->vq.vq_ops = &vring_vq_ops;
  317. vq->vq.name = name;
  318. vq->notify = notify;
  319. vq->broken = false;
  320. vq->last_used_idx = 0;
  321. vq->num_added = 0;
  322. list_add_tail(&vq->vq.list, &vdev->vqs);
  323. #ifdef DEBUG
  324. vq->in_use = false;
  325. #endif
  326. vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC);
  327. /* No callback? Tell other side not to bother us. */
  328. if (!callback)
  329. vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
  330. /* Put everything in free lists. */
  331. vq->num_free = num;
  332. vq->free_head = 0;
  333. for (i = 0; i < num-1; i++)
  334. vq->vring.desc[i].next = i+1;
  335. return &vq->vq;
  336. }
  337. EXPORT_SYMBOL_GPL(vring_new_virtqueue);
  338. void vring_del_virtqueue(struct virtqueue *vq)
  339. {
  340. list_del(&vq->list);
  341. kfree(to_vvq(vq));
  342. }
  343. EXPORT_SYMBOL_GPL(vring_del_virtqueue);
  344. /* Manipulates transport-specific feature bits. */
  345. void vring_transport_features(struct virtio_device *vdev)
  346. {
  347. unsigned int i;
  348. for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++) {
  349. switch (i) {
  350. case VIRTIO_RING_F_INDIRECT_DESC:
  351. break;
  352. default:
  353. /* We don't understand this bit. */
  354. clear_bit(i, vdev->features);
  355. }
  356. }
  357. }
  358. EXPORT_SYMBOL_GPL(vring_transport_features);
  359. MODULE_LICENSE("GPL");