virtio_ring.c 14 KB

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