virtio_ring.c 12 KB

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