virtio_ring.c 11 KB

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