virtio_ring.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  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. /* Host publishes avail event idx */
  79. bool event;
  80. /* Number of free buffers */
  81. unsigned int num_free;
  82. /* Head of free buffer list. */
  83. unsigned int free_head;
  84. /* Number we've added since last sync. */
  85. unsigned int num_added;
  86. /* Last used index we've seen. */
  87. u16 last_used_idx;
  88. /* How to notify other side. FIXME: commonalize hcalls! */
  89. void (*notify)(struct virtqueue *vq);
  90. #ifdef DEBUG
  91. /* They're supposed to lock for us. */
  92. unsigned int in_use;
  93. #endif
  94. /* Tokens for callbacks. */
  95. void *data[];
  96. };
  97. #define to_vvq(_vq) container_of(_vq, struct vring_virtqueue, vq)
  98. /* Set up an indirect table of descriptors and add it to the queue. */
  99. static int vring_add_indirect(struct vring_virtqueue *vq,
  100. struct scatterlist sg[],
  101. unsigned int out,
  102. unsigned int in,
  103. gfp_t gfp)
  104. {
  105. struct vring_desc *desc;
  106. unsigned head;
  107. int i;
  108. desc = kmalloc((out + in) * sizeof(struct vring_desc), gfp);
  109. if (!desc)
  110. return -ENOMEM;
  111. /* Transfer entries from the sg list into the indirect page */
  112. for (i = 0; i < out; i++) {
  113. desc[i].flags = VRING_DESC_F_NEXT;
  114. desc[i].addr = sg_phys(sg);
  115. desc[i].len = sg->length;
  116. desc[i].next = i+1;
  117. sg++;
  118. }
  119. for (; i < (out + in); i++) {
  120. desc[i].flags = VRING_DESC_F_NEXT|VRING_DESC_F_WRITE;
  121. desc[i].addr = sg_phys(sg);
  122. desc[i].len = sg->length;
  123. desc[i].next = i+1;
  124. sg++;
  125. }
  126. /* Last one doesn't continue. */
  127. desc[i-1].flags &= ~VRING_DESC_F_NEXT;
  128. desc[i-1].next = 0;
  129. /* We're about to use a buffer */
  130. vq->num_free--;
  131. /* Use a single buffer which doesn't continue */
  132. head = vq->free_head;
  133. vq->vring.desc[head].flags = VRING_DESC_F_INDIRECT;
  134. vq->vring.desc[head].addr = virt_to_phys(desc);
  135. vq->vring.desc[head].len = i * sizeof(struct vring_desc);
  136. /* Update free pointer */
  137. vq->free_head = vq->vring.desc[head].next;
  138. return head;
  139. }
  140. int virtqueue_add_buf_gfp(struct virtqueue *_vq,
  141. struct scatterlist sg[],
  142. unsigned int out,
  143. unsigned int in,
  144. void *data,
  145. gfp_t gfp)
  146. {
  147. struct vring_virtqueue *vq = to_vvq(_vq);
  148. unsigned int i, avail, uninitialized_var(prev);
  149. int head;
  150. START_USE(vq);
  151. BUG_ON(data == NULL);
  152. /* If the host supports indirect descriptor tables, and we have multiple
  153. * buffers, then go indirect. FIXME: tune this threshold */
  154. if (vq->indirect && (out + in) > 1 && vq->num_free) {
  155. head = vring_add_indirect(vq, sg, out, in, gfp);
  156. if (likely(head >= 0))
  157. goto add_head;
  158. }
  159. BUG_ON(out + in > vq->vring.num);
  160. BUG_ON(out + in == 0);
  161. if (vq->num_free < out + in) {
  162. pr_debug("Can't add buf len %i - avail = %i\n",
  163. out + in, vq->num_free);
  164. /* FIXME: for historical reasons, we force a notify here if
  165. * there are outgoing parts to the buffer. Presumably the
  166. * host should service the ring ASAP. */
  167. if (out)
  168. vq->notify(&vq->vq);
  169. END_USE(vq);
  170. return -ENOSPC;
  171. }
  172. /* We're about to use some buffers from the free list. */
  173. vq->num_free -= out + in;
  174. head = vq->free_head;
  175. for (i = vq->free_head; out; i = vq->vring.desc[i].next, out--) {
  176. vq->vring.desc[i].flags = VRING_DESC_F_NEXT;
  177. vq->vring.desc[i].addr = sg_phys(sg);
  178. vq->vring.desc[i].len = sg->length;
  179. prev = i;
  180. sg++;
  181. }
  182. for (; in; i = vq->vring.desc[i].next, in--) {
  183. vq->vring.desc[i].flags = VRING_DESC_F_NEXT|VRING_DESC_F_WRITE;
  184. vq->vring.desc[i].addr = sg_phys(sg);
  185. vq->vring.desc[i].len = sg->length;
  186. prev = i;
  187. sg++;
  188. }
  189. /* Last one doesn't continue. */
  190. vq->vring.desc[prev].flags &= ~VRING_DESC_F_NEXT;
  191. /* Update free pointer */
  192. vq->free_head = i;
  193. add_head:
  194. /* Set token. */
  195. vq->data[head] = data;
  196. /* Put entry in available array (but don't update avail->idx until they
  197. * do sync). FIXME: avoid modulus here? */
  198. avail = (vq->vring.avail->idx + vq->num_added++) % vq->vring.num;
  199. vq->vring.avail->ring[avail] = head;
  200. pr_debug("Added buffer head %i to %p\n", head, vq);
  201. END_USE(vq);
  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. u16 new, old;
  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. old = vq->vring.avail->idx;
  214. new = vq->vring.avail->idx = old + vq->num_added;
  215. vq->num_added = 0;
  216. /* Need to update avail index before checking if we should notify */
  217. virtio_mb();
  218. if (vq->event ?
  219. vring_need_event(vring_avail_event(&vq->vring), new, old) :
  220. !(vq->vring.used->flags & VRING_USED_F_NO_NOTIFY))
  221. /* Prod other side to tell it about changes. */
  222. vq->notify(&vq->vq);
  223. END_USE(vq);
  224. }
  225. EXPORT_SYMBOL_GPL(virtqueue_kick);
  226. static void detach_buf(struct vring_virtqueue *vq, unsigned int head)
  227. {
  228. unsigned int i;
  229. /* Clear data ptr. */
  230. vq->data[head] = NULL;
  231. /* Put back on free list: find end */
  232. i = head;
  233. /* Free the indirect table */
  234. if (vq->vring.desc[i].flags & VRING_DESC_F_INDIRECT)
  235. kfree(phys_to_virt(vq->vring.desc[i].addr));
  236. while (vq->vring.desc[i].flags & VRING_DESC_F_NEXT) {
  237. i = vq->vring.desc[i].next;
  238. vq->num_free++;
  239. }
  240. vq->vring.desc[i].next = vq->free_head;
  241. vq->free_head = head;
  242. /* Plus final descriptor */
  243. vq->num_free++;
  244. }
  245. static inline bool more_used(const struct vring_virtqueue *vq)
  246. {
  247. return vq->last_used_idx != vq->vring.used->idx;
  248. }
  249. void *virtqueue_get_buf(struct virtqueue *_vq, unsigned int *len)
  250. {
  251. struct vring_virtqueue *vq = to_vvq(_vq);
  252. void *ret;
  253. unsigned int i;
  254. START_USE(vq);
  255. if (unlikely(vq->broken)) {
  256. END_USE(vq);
  257. return NULL;
  258. }
  259. if (!more_used(vq)) {
  260. pr_debug("No more buffers in queue\n");
  261. END_USE(vq);
  262. return NULL;
  263. }
  264. /* Only get used array entries after they have been exposed by host. */
  265. virtio_rmb();
  266. i = vq->vring.used->ring[vq->last_used_idx%vq->vring.num].id;
  267. *len = vq->vring.used->ring[vq->last_used_idx%vq->vring.num].len;
  268. if (unlikely(i >= vq->vring.num)) {
  269. BAD_RING(vq, "id %u out of range\n", i);
  270. return NULL;
  271. }
  272. if (unlikely(!vq->data[i])) {
  273. BAD_RING(vq, "id %u is not a head!\n", i);
  274. return NULL;
  275. }
  276. /* detach_buf clears data, so grab it now. */
  277. ret = vq->data[i];
  278. detach_buf(vq, i);
  279. vq->last_used_idx++;
  280. /* If we expect an interrupt for the next entry, tell host
  281. * by writing event index and flush out the write before
  282. * the read in the next get_buf call. */
  283. if (!(vq->vring.avail->flags & VRING_AVAIL_F_NO_INTERRUPT)) {
  284. vring_used_event(&vq->vring) = vq->last_used_idx;
  285. virtio_mb();
  286. }
  287. END_USE(vq);
  288. return ret;
  289. }
  290. EXPORT_SYMBOL_GPL(virtqueue_get_buf);
  291. void virtqueue_disable_cb(struct virtqueue *_vq)
  292. {
  293. struct vring_virtqueue *vq = to_vvq(_vq);
  294. vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
  295. }
  296. EXPORT_SYMBOL_GPL(virtqueue_disable_cb);
  297. bool virtqueue_enable_cb(struct virtqueue *_vq)
  298. {
  299. struct vring_virtqueue *vq = to_vvq(_vq);
  300. START_USE(vq);
  301. /* We optimistically turn back on interrupts, then check if there was
  302. * more to do. */
  303. /* Depending on the VIRTIO_RING_F_EVENT_IDX feature, we need to
  304. * either clear the flags bit or point the event index at the next
  305. * entry. Always do both to keep code simple. */
  306. vq->vring.avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT;
  307. vring_used_event(&vq->vring) = vq->last_used_idx;
  308. virtio_mb();
  309. if (unlikely(more_used(vq))) {
  310. END_USE(vq);
  311. return false;
  312. }
  313. END_USE(vq);
  314. return true;
  315. }
  316. EXPORT_SYMBOL_GPL(virtqueue_enable_cb);
  317. bool virtqueue_enable_cb_delayed(struct virtqueue *_vq)
  318. {
  319. struct vring_virtqueue *vq = to_vvq(_vq);
  320. u16 bufs;
  321. START_USE(vq);
  322. /* We optimistically turn back on interrupts, then check if there was
  323. * more to do. */
  324. /* Depending on the VIRTIO_RING_F_USED_EVENT_IDX feature, we need to
  325. * either clear the flags bit or point the event index at the next
  326. * entry. Always do both to keep code simple. */
  327. vq->vring.avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT;
  328. /* TODO: tune this threshold */
  329. bufs = (u16)(vq->vring.avail->idx - vq->last_used_idx) * 3 / 4;
  330. vring_used_event(&vq->vring) = vq->last_used_idx + bufs;
  331. virtio_mb();
  332. if (unlikely((u16)(vq->vring.used->idx - vq->last_used_idx) > bufs)) {
  333. END_USE(vq);
  334. return false;
  335. }
  336. END_USE(vq);
  337. return true;
  338. }
  339. EXPORT_SYMBOL_GPL(virtqueue_enable_cb_delayed);
  340. void *virtqueue_detach_unused_buf(struct virtqueue *_vq)
  341. {
  342. struct vring_virtqueue *vq = to_vvq(_vq);
  343. unsigned int i;
  344. void *buf;
  345. START_USE(vq);
  346. for (i = 0; i < vq->vring.num; i++) {
  347. if (!vq->data[i])
  348. continue;
  349. /* detach_buf clears data, so grab it now. */
  350. buf = vq->data[i];
  351. detach_buf(vq, i);
  352. vq->vring.avail->idx--;
  353. END_USE(vq);
  354. return buf;
  355. }
  356. /* That should have freed everything. */
  357. BUG_ON(vq->num_free != vq->vring.num);
  358. END_USE(vq);
  359. return NULL;
  360. }
  361. EXPORT_SYMBOL_GPL(virtqueue_detach_unused_buf);
  362. irqreturn_t vring_interrupt(int irq, void *_vq)
  363. {
  364. struct vring_virtqueue *vq = to_vvq(_vq);
  365. if (!more_used(vq)) {
  366. pr_debug("virtqueue interrupt with no work for %p\n", vq);
  367. return IRQ_NONE;
  368. }
  369. if (unlikely(vq->broken))
  370. return IRQ_HANDLED;
  371. pr_debug("virtqueue callback for %p (%p)\n", vq, vq->vq.callback);
  372. if (vq->vq.callback)
  373. vq->vq.callback(&vq->vq);
  374. return IRQ_HANDLED;
  375. }
  376. EXPORT_SYMBOL_GPL(vring_interrupt);
  377. struct virtqueue *vring_new_virtqueue(unsigned int num,
  378. unsigned int vring_align,
  379. struct virtio_device *vdev,
  380. void *pages,
  381. void (*notify)(struct virtqueue *),
  382. void (*callback)(struct virtqueue *),
  383. const char *name)
  384. {
  385. struct vring_virtqueue *vq;
  386. unsigned int i;
  387. /* We assume num is a power of 2. */
  388. if (num & (num - 1)) {
  389. dev_warn(&vdev->dev, "Bad virtqueue length %u\n", num);
  390. return NULL;
  391. }
  392. vq = kmalloc(sizeof(*vq) + sizeof(void *)*num, GFP_KERNEL);
  393. if (!vq)
  394. return NULL;
  395. vring_init(&vq->vring, num, pages, vring_align);
  396. vq->vq.callback = callback;
  397. vq->vq.vdev = vdev;
  398. vq->vq.name = name;
  399. vq->notify = notify;
  400. vq->broken = false;
  401. vq->last_used_idx = 0;
  402. vq->num_added = 0;
  403. list_add_tail(&vq->vq.list, &vdev->vqs);
  404. #ifdef DEBUG
  405. vq->in_use = false;
  406. #endif
  407. vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC);
  408. vq->event = virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX);
  409. /* No callback? Tell other side not to bother us. */
  410. if (!callback)
  411. vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
  412. /* Put everything in free lists. */
  413. vq->num_free = num;
  414. vq->free_head = 0;
  415. for (i = 0; i < num-1; i++) {
  416. vq->vring.desc[i].next = i+1;
  417. vq->data[i] = NULL;
  418. }
  419. vq->data[i] = NULL;
  420. return &vq->vq;
  421. }
  422. EXPORT_SYMBOL_GPL(vring_new_virtqueue);
  423. void vring_del_virtqueue(struct virtqueue *vq)
  424. {
  425. list_del(&vq->list);
  426. kfree(to_vvq(vq));
  427. }
  428. EXPORT_SYMBOL_GPL(vring_del_virtqueue);
  429. /* Manipulates transport-specific feature bits. */
  430. void vring_transport_features(struct virtio_device *vdev)
  431. {
  432. unsigned int i;
  433. for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++) {
  434. switch (i) {
  435. case VIRTIO_RING_F_INDIRECT_DESC:
  436. break;
  437. case VIRTIO_RING_F_EVENT_IDX:
  438. break;
  439. default:
  440. /* We don't understand this bit. */
  441. clear_bit(i, vdev->features);
  442. }
  443. }
  444. }
  445. EXPORT_SYMBOL_GPL(vring_transport_features);
  446. MODULE_LICENSE("GPL");