virtio_ring.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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. /* If we're indirect, we can fit many (assuming not OOM). */
  201. if (vq->indirect)
  202. return vq->num_free ? vq->vring.num : 0;
  203. return vq->num_free;
  204. }
  205. EXPORT_SYMBOL_GPL(virtqueue_add_buf_gfp);
  206. void virtqueue_kick(struct virtqueue *_vq)
  207. {
  208. struct vring_virtqueue *vq = to_vvq(_vq);
  209. START_USE(vq);
  210. /* Descriptors and available array need to be set before we expose the
  211. * new available array entries. */
  212. virtio_wmb();
  213. vq->vring.avail->idx += vq->num_added;
  214. vq->num_added = 0;
  215. /* Need to update avail index before checking if we should notify */
  216. virtio_mb();
  217. if (!(vq->vring.used->flags & VRING_USED_F_NO_NOTIFY))
  218. /* Prod other side to tell it about changes. */
  219. vq->notify(&vq->vq);
  220. END_USE(vq);
  221. }
  222. EXPORT_SYMBOL_GPL(virtqueue_kick);
  223. static void detach_buf(struct vring_virtqueue *vq, unsigned int head)
  224. {
  225. unsigned int i;
  226. /* Clear data ptr. */
  227. vq->data[head] = NULL;
  228. /* Put back on free list: find end */
  229. i = head;
  230. /* Free the indirect table */
  231. if (vq->vring.desc[i].flags & VRING_DESC_F_INDIRECT)
  232. kfree(phys_to_virt(vq->vring.desc[i].addr));
  233. while (vq->vring.desc[i].flags & VRING_DESC_F_NEXT) {
  234. i = vq->vring.desc[i].next;
  235. vq->num_free++;
  236. }
  237. vq->vring.desc[i].next = vq->free_head;
  238. vq->free_head = head;
  239. /* Plus final descriptor */
  240. vq->num_free++;
  241. }
  242. static inline bool more_used(const struct vring_virtqueue *vq)
  243. {
  244. return vq->last_used_idx != vq->vring.used->idx;
  245. }
  246. void *virtqueue_get_buf(struct virtqueue *_vq, unsigned int *len)
  247. {
  248. struct vring_virtqueue *vq = to_vvq(_vq);
  249. void *ret;
  250. unsigned int i;
  251. START_USE(vq);
  252. if (unlikely(vq->broken)) {
  253. END_USE(vq);
  254. return NULL;
  255. }
  256. if (!more_used(vq)) {
  257. pr_debug("No more buffers in queue\n");
  258. END_USE(vq);
  259. return NULL;
  260. }
  261. /* Only get used array entries after they have been exposed by host. */
  262. virtio_rmb();
  263. i = vq->vring.used->ring[vq->last_used_idx%vq->vring.num].id;
  264. *len = vq->vring.used->ring[vq->last_used_idx%vq->vring.num].len;
  265. if (unlikely(i >= vq->vring.num)) {
  266. BAD_RING(vq, "id %u out of range\n", i);
  267. return NULL;
  268. }
  269. if (unlikely(!vq->data[i])) {
  270. BAD_RING(vq, "id %u is not a head!\n", i);
  271. return NULL;
  272. }
  273. /* detach_buf clears data, so grab it now. */
  274. ret = vq->data[i];
  275. detach_buf(vq, i);
  276. vq->last_used_idx++;
  277. END_USE(vq);
  278. return ret;
  279. }
  280. EXPORT_SYMBOL_GPL(virtqueue_get_buf);
  281. void virtqueue_disable_cb(struct virtqueue *_vq)
  282. {
  283. struct vring_virtqueue *vq = to_vvq(_vq);
  284. vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
  285. }
  286. EXPORT_SYMBOL_GPL(virtqueue_disable_cb);
  287. bool virtqueue_enable_cb(struct virtqueue *_vq)
  288. {
  289. struct vring_virtqueue *vq = to_vvq(_vq);
  290. START_USE(vq);
  291. /* We optimistically turn back on interrupts, then check if there was
  292. * more to do. */
  293. vq->vring.avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT;
  294. virtio_mb();
  295. if (unlikely(more_used(vq))) {
  296. END_USE(vq);
  297. return false;
  298. }
  299. END_USE(vq);
  300. return true;
  301. }
  302. EXPORT_SYMBOL_GPL(virtqueue_enable_cb);
  303. void *virtqueue_detach_unused_buf(struct virtqueue *_vq)
  304. {
  305. struct vring_virtqueue *vq = to_vvq(_vq);
  306. unsigned int i;
  307. void *buf;
  308. START_USE(vq);
  309. for (i = 0; i < vq->vring.num; i++) {
  310. if (!vq->data[i])
  311. continue;
  312. /* detach_buf clears data, so grab it now. */
  313. buf = vq->data[i];
  314. detach_buf(vq, i);
  315. END_USE(vq);
  316. return buf;
  317. }
  318. /* That should have freed everything. */
  319. BUG_ON(vq->num_free != vq->vring.num);
  320. END_USE(vq);
  321. return NULL;
  322. }
  323. EXPORT_SYMBOL_GPL(virtqueue_detach_unused_buf);
  324. irqreturn_t vring_interrupt(int irq, void *_vq)
  325. {
  326. struct vring_virtqueue *vq = to_vvq(_vq);
  327. if (!more_used(vq)) {
  328. pr_debug("virtqueue interrupt with no work for %p\n", vq);
  329. return IRQ_NONE;
  330. }
  331. if (unlikely(vq->broken))
  332. return IRQ_HANDLED;
  333. pr_debug("virtqueue callback for %p (%p)\n", vq, vq->vq.callback);
  334. if (vq->vq.callback)
  335. vq->vq.callback(&vq->vq);
  336. return IRQ_HANDLED;
  337. }
  338. EXPORT_SYMBOL_GPL(vring_interrupt);
  339. struct virtqueue *vring_new_virtqueue(unsigned int num,
  340. unsigned int vring_align,
  341. struct virtio_device *vdev,
  342. void *pages,
  343. void (*notify)(struct virtqueue *),
  344. void (*callback)(struct virtqueue *),
  345. const char *name)
  346. {
  347. struct vring_virtqueue *vq;
  348. unsigned int i;
  349. /* We assume num is a power of 2. */
  350. if (num & (num - 1)) {
  351. dev_warn(&vdev->dev, "Bad virtqueue length %u\n", num);
  352. return NULL;
  353. }
  354. vq = kmalloc(sizeof(*vq) + sizeof(void *)*num, GFP_KERNEL);
  355. if (!vq)
  356. return NULL;
  357. vring_init(&vq->vring, num, pages, vring_align);
  358. vq->vq.callback = callback;
  359. vq->vq.vdev = vdev;
  360. vq->vq.name = name;
  361. vq->notify = notify;
  362. vq->broken = false;
  363. vq->last_used_idx = 0;
  364. vq->num_added = 0;
  365. list_add_tail(&vq->vq.list, &vdev->vqs);
  366. #ifdef DEBUG
  367. vq->in_use = false;
  368. #endif
  369. vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC);
  370. /* No callback? Tell other side not to bother us. */
  371. if (!callback)
  372. vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
  373. /* Put everything in free lists. */
  374. vq->num_free = num;
  375. vq->free_head = 0;
  376. for (i = 0; i < num-1; i++) {
  377. vq->vring.desc[i].next = i+1;
  378. vq->data[i] = NULL;
  379. }
  380. vq->data[i] = NULL;
  381. return &vq->vq;
  382. }
  383. EXPORT_SYMBOL_GPL(vring_new_virtqueue);
  384. void vring_del_virtqueue(struct virtqueue *vq)
  385. {
  386. list_del(&vq->list);
  387. kfree(to_vvq(vq));
  388. }
  389. EXPORT_SYMBOL_GPL(vring_del_virtqueue);
  390. /* Manipulates transport-specific feature bits. */
  391. void vring_transport_features(struct virtio_device *vdev)
  392. {
  393. unsigned int i;
  394. for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++) {
  395. switch (i) {
  396. case VIRTIO_RING_F_INDIRECT_DESC:
  397. break;
  398. default:
  399. /* We don't understand this bit. */
  400. clear_bit(i, vdev->features);
  401. }
  402. }
  403. }
  404. EXPORT_SYMBOL_GPL(vring_transport_features);
  405. MODULE_LICENSE("GPL");