virtio_ring.c 14 KB

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