virtio_ring.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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. {
  102. struct vring_desc *desc;
  103. unsigned head;
  104. int i;
  105. desc = kmalloc((out + in) * sizeof(struct vring_desc), GFP_ATOMIC);
  106. if (!desc)
  107. return vq->vring.num;
  108. /* Transfer entries from the sg list into the indirect page */
  109. for (i = 0; i < out; i++) {
  110. desc[i].flags = VRING_DESC_F_NEXT;
  111. desc[i].addr = sg_phys(sg);
  112. desc[i].len = sg->length;
  113. desc[i].next = i+1;
  114. sg++;
  115. }
  116. for (; i < (out + in); i++) {
  117. desc[i].flags = VRING_DESC_F_NEXT|VRING_DESC_F_WRITE;
  118. desc[i].addr = sg_phys(sg);
  119. desc[i].len = sg->length;
  120. desc[i].next = i+1;
  121. sg++;
  122. }
  123. /* Last one doesn't continue. */
  124. desc[i-1].flags &= ~VRING_DESC_F_NEXT;
  125. desc[i-1].next = 0;
  126. /* We're about to use a buffer */
  127. vq->num_free--;
  128. /* Use a single buffer which doesn't continue */
  129. head = vq->free_head;
  130. vq->vring.desc[head].flags = VRING_DESC_F_INDIRECT;
  131. vq->vring.desc[head].addr = virt_to_phys(desc);
  132. vq->vring.desc[head].len = i * sizeof(struct vring_desc);
  133. /* Update free pointer */
  134. vq->free_head = vq->vring.desc[head].next;
  135. return head;
  136. }
  137. static int vring_add_buf(struct virtqueue *_vq,
  138. struct scatterlist sg[],
  139. unsigned int out,
  140. unsigned int in,
  141. void *data)
  142. {
  143. struct vring_virtqueue *vq = to_vvq(_vq);
  144. unsigned int i, avail, head, uninitialized_var(prev);
  145. START_USE(vq);
  146. BUG_ON(data == NULL);
  147. /* If the host supports indirect descriptor tables, and we have multiple
  148. * buffers, then go indirect. FIXME: tune this threshold */
  149. if (vq->indirect && (out + in) > 1 && vq->num_free) {
  150. head = vring_add_indirect(vq, sg, out, in);
  151. if (head != vq->vring.num)
  152. goto add_head;
  153. }
  154. BUG_ON(out + in > vq->vring.num);
  155. BUG_ON(out + in == 0);
  156. if (vq->num_free < out + in) {
  157. pr_debug("Can't add buf len %i - avail = %i\n",
  158. out + in, vq->num_free);
  159. /* FIXME: for historical reasons, we force a notify here if
  160. * there are outgoing parts to the buffer. Presumably the
  161. * host should service the ring ASAP. */
  162. if (out)
  163. vq->notify(&vq->vq);
  164. END_USE(vq);
  165. return -ENOSPC;
  166. }
  167. /* We're about to use some buffers from the free list. */
  168. vq->num_free -= out + in;
  169. head = vq->free_head;
  170. for (i = vq->free_head; out; i = vq->vring.desc[i].next, out--) {
  171. vq->vring.desc[i].flags = VRING_DESC_F_NEXT;
  172. vq->vring.desc[i].addr = sg_phys(sg);
  173. vq->vring.desc[i].len = sg->length;
  174. prev = i;
  175. sg++;
  176. }
  177. for (; in; i = vq->vring.desc[i].next, in--) {
  178. vq->vring.desc[i].flags = VRING_DESC_F_NEXT|VRING_DESC_F_WRITE;
  179. vq->vring.desc[i].addr = sg_phys(sg);
  180. vq->vring.desc[i].len = sg->length;
  181. prev = i;
  182. sg++;
  183. }
  184. /* Last one doesn't continue. */
  185. vq->vring.desc[prev].flags &= ~VRING_DESC_F_NEXT;
  186. /* Update free pointer */
  187. vq->free_head = i;
  188. add_head:
  189. /* Set token. */
  190. vq->data[head] = data;
  191. /* Put entry in available array (but don't update avail->idx until they
  192. * do sync). FIXME: avoid modulus here? */
  193. avail = (vq->vring.avail->idx + vq->num_added++) % vq->vring.num;
  194. vq->vring.avail->ring[avail] = head;
  195. pr_debug("Added buffer head %i to %p\n", head, vq);
  196. END_USE(vq);
  197. /* If we're indirect, we can fit many (assuming not OOM). */
  198. if (vq->indirect)
  199. return vq->num_free ? vq->vring.num : 0;
  200. return vq->num_free;
  201. }
  202. static void vring_kick(struct virtqueue *_vq)
  203. {
  204. struct vring_virtqueue *vq = to_vvq(_vq);
  205. START_USE(vq);
  206. /* Descriptors and available array need to be set before we expose the
  207. * new available array entries. */
  208. virtio_wmb();
  209. vq->vring.avail->idx += vq->num_added;
  210. vq->num_added = 0;
  211. /* Need to update avail index before checking if we should notify */
  212. virtio_mb();
  213. if (!(vq->vring.used->flags & VRING_USED_F_NO_NOTIFY))
  214. /* Prod other side to tell it about changes. */
  215. vq->notify(&vq->vq);
  216. END_USE(vq);
  217. }
  218. static void detach_buf(struct vring_virtqueue *vq, unsigned int head)
  219. {
  220. unsigned int i;
  221. /* Clear data ptr. */
  222. vq->data[head] = NULL;
  223. /* Put back on free list: find end */
  224. i = head;
  225. /* Free the indirect table */
  226. if (vq->vring.desc[i].flags & VRING_DESC_F_INDIRECT)
  227. kfree(phys_to_virt(vq->vring.desc[i].addr));
  228. while (vq->vring.desc[i].flags & VRING_DESC_F_NEXT) {
  229. i = vq->vring.desc[i].next;
  230. vq->num_free++;
  231. }
  232. vq->vring.desc[i].next = vq->free_head;
  233. vq->free_head = head;
  234. /* Plus final descriptor */
  235. vq->num_free++;
  236. }
  237. static inline bool more_used(const struct vring_virtqueue *vq)
  238. {
  239. return vq->last_used_idx != vq->vring.used->idx;
  240. }
  241. static void *vring_get_buf(struct virtqueue *_vq, unsigned int *len)
  242. {
  243. struct vring_virtqueue *vq = to_vvq(_vq);
  244. void *ret;
  245. unsigned int i;
  246. START_USE(vq);
  247. if (unlikely(vq->broken)) {
  248. END_USE(vq);
  249. return NULL;
  250. }
  251. if (!more_used(vq)) {
  252. pr_debug("No more buffers in queue\n");
  253. END_USE(vq);
  254. return NULL;
  255. }
  256. /* Only get used array entries after they have been exposed by host. */
  257. virtio_rmb();
  258. i = vq->vring.used->ring[vq->last_used_idx%vq->vring.num].id;
  259. *len = vq->vring.used->ring[vq->last_used_idx%vq->vring.num].len;
  260. if (unlikely(i >= vq->vring.num)) {
  261. BAD_RING(vq, "id %u out of range\n", i);
  262. return NULL;
  263. }
  264. if (unlikely(!vq->data[i])) {
  265. BAD_RING(vq, "id %u is not a head!\n", i);
  266. return NULL;
  267. }
  268. /* detach_buf clears data, so grab it now. */
  269. ret = vq->data[i];
  270. detach_buf(vq, i);
  271. vq->last_used_idx++;
  272. END_USE(vq);
  273. return ret;
  274. }
  275. static void vring_disable_cb(struct virtqueue *_vq)
  276. {
  277. struct vring_virtqueue *vq = to_vvq(_vq);
  278. vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
  279. }
  280. static bool vring_enable_cb(struct virtqueue *_vq)
  281. {
  282. struct vring_virtqueue *vq = to_vvq(_vq);
  283. START_USE(vq);
  284. /* We optimistically turn back on interrupts, then check if there was
  285. * more to do. */
  286. vq->vring.avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT;
  287. virtio_mb();
  288. if (unlikely(more_used(vq))) {
  289. END_USE(vq);
  290. return false;
  291. }
  292. END_USE(vq);
  293. return true;
  294. }
  295. static void *vring_detach_unused_buf(struct virtqueue *_vq)
  296. {
  297. struct vring_virtqueue *vq = to_vvq(_vq);
  298. unsigned int i;
  299. void *buf;
  300. START_USE(vq);
  301. for (i = 0; i < vq->vring.num; i++) {
  302. if (!vq->data[i])
  303. continue;
  304. /* detach_buf clears data, so grab it now. */
  305. buf = vq->data[i];
  306. detach_buf(vq, i);
  307. END_USE(vq);
  308. return buf;
  309. }
  310. /* That should have freed everything. */
  311. BUG_ON(vq->num_free != vq->vring.num);
  312. END_USE(vq);
  313. return NULL;
  314. }
  315. irqreturn_t vring_interrupt(int irq, void *_vq)
  316. {
  317. struct vring_virtqueue *vq = to_vvq(_vq);
  318. if (!more_used(vq)) {
  319. pr_debug("virtqueue interrupt with no work for %p\n", vq);
  320. return IRQ_NONE;
  321. }
  322. if (unlikely(vq->broken))
  323. return IRQ_HANDLED;
  324. pr_debug("virtqueue callback for %p (%p)\n", vq, vq->vq.callback);
  325. if (vq->vq.callback)
  326. vq->vq.callback(&vq->vq);
  327. return IRQ_HANDLED;
  328. }
  329. EXPORT_SYMBOL_GPL(vring_interrupt);
  330. static struct virtqueue_ops vring_vq_ops = {
  331. .add_buf = vring_add_buf,
  332. .get_buf = vring_get_buf,
  333. .kick = vring_kick,
  334. .disable_cb = vring_disable_cb,
  335. .enable_cb = vring_enable_cb,
  336. .detach_unused_buf = vring_detach_unused_buf,
  337. };
  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.vq_ops = &vring_vq_ops;
  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");