virtio_ring.c 12 KB

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