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