virtio_ring.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255
  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. #include <linux/hrtimer.h>
  26. #include <linux/kmemleak.h>
  27. #include <linux/dma-mapping.h>
  28. #include <xen/xen.h>
  29. #ifdef DEBUG
  30. /* For development, we want to crash whenever the ring is screwed. */
  31. #define BAD_RING(_vq, fmt, args...) \
  32. do { \
  33. dev_err(&(_vq)->vq.vdev->dev, \
  34. "%s:"fmt, (_vq)->vq.name, ##args); \
  35. BUG(); \
  36. } while (0)
  37. /* Caller is supposed to guarantee no reentry. */
  38. #define START_USE(_vq) \
  39. do { \
  40. if ((_vq)->in_use) \
  41. panic("%s:in_use = %i\n", \
  42. (_vq)->vq.name, (_vq)->in_use); \
  43. (_vq)->in_use = __LINE__; \
  44. } while (0)
  45. #define END_USE(_vq) \
  46. do { BUG_ON(!(_vq)->in_use); (_vq)->in_use = 0; } while(0)
  47. #else
  48. #define BAD_RING(_vq, fmt, args...) \
  49. do { \
  50. dev_err(&_vq->vq.vdev->dev, \
  51. "%s:"fmt, (_vq)->vq.name, ##args); \
  52. (_vq)->broken = true; \
  53. } while (0)
  54. #define START_USE(vq)
  55. #define END_USE(vq)
  56. #endif
  57. struct vring_desc_state {
  58. void *data; /* Data for callback. */
  59. struct vring_desc *indir_desc; /* Indirect descriptor, if any. */
  60. };
  61. struct vring_virtqueue {
  62. struct virtqueue vq;
  63. /* Actual memory layout for this queue */
  64. struct vring vring;
  65. /* Can we use weak barriers? */
  66. bool weak_barriers;
  67. /* Other side has made a mess, don't try any more. */
  68. bool broken;
  69. /* Host supports indirect buffers */
  70. bool indirect;
  71. /* Host publishes avail event idx */
  72. bool event;
  73. /* Head of free buffer list. */
  74. unsigned int free_head;
  75. /* Number we've added since last sync. */
  76. unsigned int num_added;
  77. /* Last used index we've seen. */
  78. u16 last_used_idx;
  79. /* Last written value to avail->flags */
  80. u16 avail_flags_shadow;
  81. /* Last written value to avail->idx in guest byte order */
  82. u16 avail_idx_shadow;
  83. /* How to notify other side. FIXME: commonalize hcalls! */
  84. bool (*notify)(struct virtqueue *vq);
  85. /* DMA, allocation, and size information */
  86. bool we_own_ring;
  87. size_t queue_size_in_bytes;
  88. dma_addr_t queue_dma_addr;
  89. #ifdef DEBUG
  90. /* They're supposed to lock for us. */
  91. unsigned int in_use;
  92. /* Figure out if their kicks are too delayed. */
  93. bool last_add_time_valid;
  94. ktime_t last_add_time;
  95. #endif
  96. /* Per-descriptor state. */
  97. struct vring_desc_state desc_state[];
  98. };
  99. #define to_vvq(_vq) container_of(_vq, struct vring_virtqueue, vq)
  100. /*
  101. * Modern virtio devices have feature bits to specify whether they need a
  102. * quirk and bypass the IOMMU. If not there, just use the DMA API.
  103. *
  104. * If there, the interaction between virtio and DMA API is messy.
  105. *
  106. * On most systems with virtio, physical addresses match bus addresses,
  107. * and it doesn't particularly matter whether we use the DMA API.
  108. *
  109. * On some systems, including Xen and any system with a physical device
  110. * that speaks virtio behind a physical IOMMU, we must use the DMA API
  111. * for virtio DMA to work at all.
  112. *
  113. * On other systems, including SPARC and PPC64, virtio-pci devices are
  114. * enumerated as though they are behind an IOMMU, but the virtio host
  115. * ignores the IOMMU, so we must either pretend that the IOMMU isn't
  116. * there or somehow map everything as the identity.
  117. *
  118. * For the time being, we preserve historic behavior and bypass the DMA
  119. * API.
  120. *
  121. * TODO: install a per-device DMA ops structure that does the right thing
  122. * taking into account all the above quirks, and use the DMA API
  123. * unconditionally on data path.
  124. */
  125. static bool vring_use_dma_api(struct virtio_device *vdev)
  126. {
  127. if (!virtio_has_iommu_quirk(vdev))
  128. return true;
  129. /* Otherwise, we are left to guess. */
  130. /*
  131. * In theory, it's possible to have a buggy QEMU-supposed
  132. * emulated Q35 IOMMU and Xen enabled at the same time. On
  133. * such a configuration, virtio has never worked and will
  134. * not work without an even larger kludge. Instead, enable
  135. * the DMA API if we're a Xen guest, which at least allows
  136. * all of the sensible Xen configurations to work correctly.
  137. */
  138. if (xen_domain())
  139. return true;
  140. return false;
  141. }
  142. /*
  143. * The DMA ops on various arches are rather gnarly right now, and
  144. * making all of the arch DMA ops work on the vring device itself
  145. * is a mess. For now, we use the parent device for DMA ops.
  146. */
  147. static inline struct device *vring_dma_dev(const struct vring_virtqueue *vq)
  148. {
  149. return vq->vq.vdev->dev.parent;
  150. }
  151. /* Map one sg entry. */
  152. static dma_addr_t vring_map_one_sg(const struct vring_virtqueue *vq,
  153. struct scatterlist *sg,
  154. enum dma_data_direction direction)
  155. {
  156. if (!vring_use_dma_api(vq->vq.vdev))
  157. return (dma_addr_t)sg_phys(sg);
  158. /*
  159. * We can't use dma_map_sg, because we don't use scatterlists in
  160. * the way it expects (we don't guarantee that the scatterlist
  161. * will exist for the lifetime of the mapping).
  162. */
  163. return dma_map_page(vring_dma_dev(vq),
  164. sg_page(sg), sg->offset, sg->length,
  165. direction);
  166. }
  167. static dma_addr_t vring_map_single(const struct vring_virtqueue *vq,
  168. void *cpu_addr, size_t size,
  169. enum dma_data_direction direction)
  170. {
  171. if (!vring_use_dma_api(vq->vq.vdev))
  172. return (dma_addr_t)virt_to_phys(cpu_addr);
  173. return dma_map_single(vring_dma_dev(vq),
  174. cpu_addr, size, direction);
  175. }
  176. static void vring_unmap_one(const struct vring_virtqueue *vq,
  177. struct vring_desc *desc)
  178. {
  179. u16 flags;
  180. if (!vring_use_dma_api(vq->vq.vdev))
  181. return;
  182. flags = virtio16_to_cpu(vq->vq.vdev, desc->flags);
  183. if (flags & VRING_DESC_F_INDIRECT) {
  184. dma_unmap_single(vring_dma_dev(vq),
  185. virtio64_to_cpu(vq->vq.vdev, desc->addr),
  186. virtio32_to_cpu(vq->vq.vdev, desc->len),
  187. (flags & VRING_DESC_F_WRITE) ?
  188. DMA_FROM_DEVICE : DMA_TO_DEVICE);
  189. } else {
  190. dma_unmap_page(vring_dma_dev(vq),
  191. virtio64_to_cpu(vq->vq.vdev, desc->addr),
  192. virtio32_to_cpu(vq->vq.vdev, desc->len),
  193. (flags & VRING_DESC_F_WRITE) ?
  194. DMA_FROM_DEVICE : DMA_TO_DEVICE);
  195. }
  196. }
  197. static int vring_mapping_error(const struct vring_virtqueue *vq,
  198. dma_addr_t addr)
  199. {
  200. if (!vring_use_dma_api(vq->vq.vdev))
  201. return 0;
  202. return dma_mapping_error(vring_dma_dev(vq), addr);
  203. }
  204. static struct vring_desc *alloc_indirect(struct virtqueue *_vq,
  205. unsigned int total_sg, gfp_t gfp)
  206. {
  207. struct vring_desc *desc;
  208. unsigned int i;
  209. /*
  210. * We require lowmem mappings for the descriptors because
  211. * otherwise virt_to_phys will give us bogus addresses in the
  212. * virtqueue.
  213. */
  214. gfp &= ~__GFP_HIGHMEM;
  215. desc = kmalloc(total_sg * sizeof(struct vring_desc), gfp);
  216. if (!desc)
  217. return NULL;
  218. for (i = 0; i < total_sg; i++)
  219. desc[i].next = cpu_to_virtio16(_vq->vdev, i + 1);
  220. return desc;
  221. }
  222. static inline int virtqueue_add(struct virtqueue *_vq,
  223. struct scatterlist *sgs[],
  224. unsigned int total_sg,
  225. unsigned int out_sgs,
  226. unsigned int in_sgs,
  227. void *data,
  228. void *ctx,
  229. gfp_t gfp)
  230. {
  231. struct vring_virtqueue *vq = to_vvq(_vq);
  232. struct scatterlist *sg;
  233. struct vring_desc *desc;
  234. unsigned int i, n, avail, descs_used, uninitialized_var(prev), err_idx;
  235. int head;
  236. bool indirect;
  237. START_USE(vq);
  238. BUG_ON(data == NULL);
  239. BUG_ON(ctx && vq->indirect);
  240. if (unlikely(vq->broken)) {
  241. END_USE(vq);
  242. return -EIO;
  243. }
  244. #ifdef DEBUG
  245. {
  246. ktime_t now = ktime_get();
  247. /* No kick or get, with .1 second between? Warn. */
  248. if (vq->last_add_time_valid)
  249. WARN_ON(ktime_to_ms(ktime_sub(now, vq->last_add_time))
  250. > 100);
  251. vq->last_add_time = now;
  252. vq->last_add_time_valid = true;
  253. }
  254. #endif
  255. BUG_ON(total_sg == 0);
  256. head = vq->free_head;
  257. /* If the host supports indirect descriptor tables, and we have multiple
  258. * buffers, then go indirect. FIXME: tune this threshold */
  259. if (vq->indirect && total_sg > 1 && vq->vq.num_free)
  260. desc = alloc_indirect(_vq, total_sg, gfp);
  261. else {
  262. desc = NULL;
  263. WARN_ON_ONCE(total_sg > vq->vring.num && !vq->indirect);
  264. }
  265. if (desc) {
  266. /* Use a single buffer which doesn't continue */
  267. indirect = true;
  268. /* Set up rest to use this indirect table. */
  269. i = 0;
  270. descs_used = 1;
  271. } else {
  272. indirect = false;
  273. desc = vq->vring.desc;
  274. i = head;
  275. descs_used = total_sg;
  276. }
  277. if (vq->vq.num_free < descs_used) {
  278. pr_debug("Can't add buf len %i - avail = %i\n",
  279. descs_used, vq->vq.num_free);
  280. /* FIXME: for historical reasons, we force a notify here if
  281. * there are outgoing parts to the buffer. Presumably the
  282. * host should service the ring ASAP. */
  283. if (out_sgs)
  284. vq->notify(&vq->vq);
  285. if (indirect)
  286. kfree(desc);
  287. END_USE(vq);
  288. return -ENOSPC;
  289. }
  290. for (n = 0; n < out_sgs; n++) {
  291. for (sg = sgs[n]; sg; sg = sg_next(sg)) {
  292. dma_addr_t addr = vring_map_one_sg(vq, sg, DMA_TO_DEVICE);
  293. if (vring_mapping_error(vq, addr))
  294. goto unmap_release;
  295. desc[i].flags = cpu_to_virtio16(_vq->vdev, VRING_DESC_F_NEXT);
  296. desc[i].addr = cpu_to_virtio64(_vq->vdev, addr);
  297. desc[i].len = cpu_to_virtio32(_vq->vdev, sg->length);
  298. prev = i;
  299. i = virtio16_to_cpu(_vq->vdev, desc[i].next);
  300. }
  301. }
  302. for (; n < (out_sgs + in_sgs); n++) {
  303. for (sg = sgs[n]; sg; sg = sg_next(sg)) {
  304. dma_addr_t addr = vring_map_one_sg(vq, sg, DMA_FROM_DEVICE);
  305. if (vring_mapping_error(vq, addr))
  306. goto unmap_release;
  307. desc[i].flags = cpu_to_virtio16(_vq->vdev, VRING_DESC_F_NEXT | VRING_DESC_F_WRITE);
  308. desc[i].addr = cpu_to_virtio64(_vq->vdev, addr);
  309. desc[i].len = cpu_to_virtio32(_vq->vdev, sg->length);
  310. prev = i;
  311. i = virtio16_to_cpu(_vq->vdev, desc[i].next);
  312. }
  313. }
  314. /* Last one doesn't continue. */
  315. desc[prev].flags &= cpu_to_virtio16(_vq->vdev, ~VRING_DESC_F_NEXT);
  316. if (indirect) {
  317. /* Now that the indirect table is filled in, map it. */
  318. dma_addr_t addr = vring_map_single(
  319. vq, desc, total_sg * sizeof(struct vring_desc),
  320. DMA_TO_DEVICE);
  321. if (vring_mapping_error(vq, addr))
  322. goto unmap_release;
  323. vq->vring.desc[head].flags = cpu_to_virtio16(_vq->vdev, VRING_DESC_F_INDIRECT);
  324. vq->vring.desc[head].addr = cpu_to_virtio64(_vq->vdev, addr);
  325. vq->vring.desc[head].len = cpu_to_virtio32(_vq->vdev, total_sg * sizeof(struct vring_desc));
  326. }
  327. /* We're using some buffers from the free list. */
  328. vq->vq.num_free -= descs_used;
  329. /* Update free pointer */
  330. if (indirect)
  331. vq->free_head = virtio16_to_cpu(_vq->vdev, vq->vring.desc[head].next);
  332. else
  333. vq->free_head = i;
  334. /* Store token and indirect buffer state. */
  335. vq->desc_state[head].data = data;
  336. if (indirect)
  337. vq->desc_state[head].indir_desc = desc;
  338. else
  339. vq->desc_state[head].indir_desc = ctx;
  340. /* Put entry in available array (but don't update avail->idx until they
  341. * do sync). */
  342. avail = vq->avail_idx_shadow & (vq->vring.num - 1);
  343. vq->vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head);
  344. /* Descriptors and available array need to be set before we expose the
  345. * new available array entries. */
  346. virtio_wmb(vq->weak_barriers);
  347. vq->avail_idx_shadow++;
  348. vq->vring.avail->idx = cpu_to_virtio16(_vq->vdev, vq->avail_idx_shadow);
  349. vq->num_added++;
  350. pr_debug("Added buffer head %i to %p\n", head, vq);
  351. END_USE(vq);
  352. /* This is very unlikely, but theoretically possible. Kick
  353. * just in case. */
  354. if (unlikely(vq->num_added == (1 << 16) - 1))
  355. virtqueue_kick(_vq);
  356. return 0;
  357. unmap_release:
  358. err_idx = i;
  359. i = head;
  360. for (n = 0; n < total_sg; n++) {
  361. if (i == err_idx)
  362. break;
  363. vring_unmap_one(vq, &desc[i]);
  364. i = virtio16_to_cpu(_vq->vdev, vq->vring.desc[i].next);
  365. }
  366. vq->vq.num_free += total_sg;
  367. if (indirect)
  368. kfree(desc);
  369. END_USE(vq);
  370. return -EIO;
  371. }
  372. /**
  373. * virtqueue_add_sgs - expose buffers to other end
  374. * @vq: the struct virtqueue we're talking about.
  375. * @sgs: array of terminated scatterlists.
  376. * @out_num: the number of scatterlists readable by other side
  377. * @in_num: the number of scatterlists which are writable (after readable ones)
  378. * @data: the token identifying the buffer.
  379. * @gfp: how to do memory allocations (if necessary).
  380. *
  381. * Caller must ensure we don't call this with other virtqueue operations
  382. * at the same time (except where noted).
  383. *
  384. * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO).
  385. */
  386. int virtqueue_add_sgs(struct virtqueue *_vq,
  387. struct scatterlist *sgs[],
  388. unsigned int out_sgs,
  389. unsigned int in_sgs,
  390. void *data,
  391. gfp_t gfp)
  392. {
  393. unsigned int i, total_sg = 0;
  394. /* Count them first. */
  395. for (i = 0; i < out_sgs + in_sgs; i++) {
  396. struct scatterlist *sg;
  397. for (sg = sgs[i]; sg; sg = sg_next(sg))
  398. total_sg++;
  399. }
  400. return virtqueue_add(_vq, sgs, total_sg, out_sgs, in_sgs,
  401. data, NULL, gfp);
  402. }
  403. EXPORT_SYMBOL_GPL(virtqueue_add_sgs);
  404. /**
  405. * virtqueue_add_outbuf - expose output buffers to other end
  406. * @vq: the struct virtqueue we're talking about.
  407. * @sg: scatterlist (must be well-formed and terminated!)
  408. * @num: the number of entries in @sg readable by other side
  409. * @data: the token identifying the buffer.
  410. * @gfp: how to do memory allocations (if necessary).
  411. *
  412. * Caller must ensure we don't call this with other virtqueue operations
  413. * at the same time (except where noted).
  414. *
  415. * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO).
  416. */
  417. int virtqueue_add_outbuf(struct virtqueue *vq,
  418. struct scatterlist *sg, unsigned int num,
  419. void *data,
  420. gfp_t gfp)
  421. {
  422. return virtqueue_add(vq, &sg, num, 1, 0, data, NULL, gfp);
  423. }
  424. EXPORT_SYMBOL_GPL(virtqueue_add_outbuf);
  425. /**
  426. * virtqueue_add_inbuf - expose input buffers to other end
  427. * @vq: the struct virtqueue we're talking about.
  428. * @sg: scatterlist (must be well-formed and terminated!)
  429. * @num: the number of entries in @sg writable by other side
  430. * @data: the token identifying the buffer.
  431. * @gfp: how to do memory allocations (if necessary).
  432. *
  433. * Caller must ensure we don't call this with other virtqueue operations
  434. * at the same time (except where noted).
  435. *
  436. * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO).
  437. */
  438. int virtqueue_add_inbuf(struct virtqueue *vq,
  439. struct scatterlist *sg, unsigned int num,
  440. void *data,
  441. gfp_t gfp)
  442. {
  443. return virtqueue_add(vq, &sg, num, 0, 1, data, NULL, gfp);
  444. }
  445. EXPORT_SYMBOL_GPL(virtqueue_add_inbuf);
  446. /**
  447. * virtqueue_add_inbuf_ctx - expose input buffers to other end
  448. * @vq: the struct virtqueue we're talking about.
  449. * @sg: scatterlist (must be well-formed and terminated!)
  450. * @num: the number of entries in @sg writable by other side
  451. * @data: the token identifying the buffer.
  452. * @ctx: extra context for the token
  453. * @gfp: how to do memory allocations (if necessary).
  454. *
  455. * Caller must ensure we don't call this with other virtqueue operations
  456. * at the same time (except where noted).
  457. *
  458. * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO).
  459. */
  460. int virtqueue_add_inbuf_ctx(struct virtqueue *vq,
  461. struct scatterlist *sg, unsigned int num,
  462. void *data,
  463. void *ctx,
  464. gfp_t gfp)
  465. {
  466. return virtqueue_add(vq, &sg, num, 0, 1, data, ctx, gfp);
  467. }
  468. EXPORT_SYMBOL_GPL(virtqueue_add_inbuf_ctx);
  469. /**
  470. * virtqueue_kick_prepare - first half of split virtqueue_kick call.
  471. * @vq: the struct virtqueue
  472. *
  473. * Instead of virtqueue_kick(), you can do:
  474. * if (virtqueue_kick_prepare(vq))
  475. * virtqueue_notify(vq);
  476. *
  477. * This is sometimes useful because the virtqueue_kick_prepare() needs
  478. * to be serialized, but the actual virtqueue_notify() call does not.
  479. */
  480. bool virtqueue_kick_prepare(struct virtqueue *_vq)
  481. {
  482. struct vring_virtqueue *vq = to_vvq(_vq);
  483. u16 new, old;
  484. bool needs_kick;
  485. START_USE(vq);
  486. /* We need to expose available array entries before checking avail
  487. * event. */
  488. virtio_mb(vq->weak_barriers);
  489. old = vq->avail_idx_shadow - vq->num_added;
  490. new = vq->avail_idx_shadow;
  491. vq->num_added = 0;
  492. #ifdef DEBUG
  493. if (vq->last_add_time_valid) {
  494. WARN_ON(ktime_to_ms(ktime_sub(ktime_get(),
  495. vq->last_add_time)) > 100);
  496. }
  497. vq->last_add_time_valid = false;
  498. #endif
  499. if (vq->event) {
  500. needs_kick = vring_need_event(virtio16_to_cpu(_vq->vdev, vring_avail_event(&vq->vring)),
  501. new, old);
  502. } else {
  503. needs_kick = !(vq->vring.used->flags & cpu_to_virtio16(_vq->vdev, VRING_USED_F_NO_NOTIFY));
  504. }
  505. END_USE(vq);
  506. return needs_kick;
  507. }
  508. EXPORT_SYMBOL_GPL(virtqueue_kick_prepare);
  509. /**
  510. * virtqueue_notify - second half of split virtqueue_kick call.
  511. * @vq: the struct virtqueue
  512. *
  513. * This does not need to be serialized.
  514. *
  515. * Returns false if host notify failed or queue is broken, otherwise true.
  516. */
  517. bool virtqueue_notify(struct virtqueue *_vq)
  518. {
  519. struct vring_virtqueue *vq = to_vvq(_vq);
  520. if (unlikely(vq->broken))
  521. return false;
  522. /* Prod other side to tell it about changes. */
  523. if (!vq->notify(_vq)) {
  524. vq->broken = true;
  525. return false;
  526. }
  527. return true;
  528. }
  529. EXPORT_SYMBOL_GPL(virtqueue_notify);
  530. /**
  531. * virtqueue_kick - update after add_buf
  532. * @vq: the struct virtqueue
  533. *
  534. * After one or more virtqueue_add_* calls, invoke this to kick
  535. * the other side.
  536. *
  537. * Caller must ensure we don't call this with other virtqueue
  538. * operations at the same time (except where noted).
  539. *
  540. * Returns false if kick failed, otherwise true.
  541. */
  542. bool virtqueue_kick(struct virtqueue *vq)
  543. {
  544. if (virtqueue_kick_prepare(vq))
  545. return virtqueue_notify(vq);
  546. return true;
  547. }
  548. EXPORT_SYMBOL_GPL(virtqueue_kick);
  549. static void detach_buf(struct vring_virtqueue *vq, unsigned int head,
  550. void **ctx)
  551. {
  552. unsigned int i, j;
  553. __virtio16 nextflag = cpu_to_virtio16(vq->vq.vdev, VRING_DESC_F_NEXT);
  554. /* Clear data ptr. */
  555. vq->desc_state[head].data = NULL;
  556. /* Put back on free list: unmap first-level descriptors and find end */
  557. i = head;
  558. while (vq->vring.desc[i].flags & nextflag) {
  559. vring_unmap_one(vq, &vq->vring.desc[i]);
  560. i = virtio16_to_cpu(vq->vq.vdev, vq->vring.desc[i].next);
  561. vq->vq.num_free++;
  562. }
  563. vring_unmap_one(vq, &vq->vring.desc[i]);
  564. vq->vring.desc[i].next = cpu_to_virtio16(vq->vq.vdev, vq->free_head);
  565. vq->free_head = head;
  566. /* Plus final descriptor */
  567. vq->vq.num_free++;
  568. if (vq->indirect) {
  569. struct vring_desc *indir_desc = vq->desc_state[head].indir_desc;
  570. u32 len;
  571. /* Free the indirect table, if any, now that it's unmapped. */
  572. if (!indir_desc)
  573. return;
  574. len = virtio32_to_cpu(vq->vq.vdev, vq->vring.desc[head].len);
  575. BUG_ON(!(vq->vring.desc[head].flags &
  576. cpu_to_virtio16(vq->vq.vdev, VRING_DESC_F_INDIRECT)));
  577. BUG_ON(len == 0 || len % sizeof(struct vring_desc));
  578. for (j = 0; j < len / sizeof(struct vring_desc); j++)
  579. vring_unmap_one(vq, &indir_desc[j]);
  580. kfree(indir_desc);
  581. vq->desc_state[head].indir_desc = NULL;
  582. } else if (ctx) {
  583. *ctx = vq->desc_state[head].indir_desc;
  584. }
  585. }
  586. static inline bool more_used(const struct vring_virtqueue *vq)
  587. {
  588. return vq->last_used_idx != virtio16_to_cpu(vq->vq.vdev, vq->vring.used->idx);
  589. }
  590. /**
  591. * virtqueue_get_buf - get the next used buffer
  592. * @vq: the struct virtqueue we're talking about.
  593. * @len: the length written into the buffer
  594. *
  595. * If the device wrote data into the buffer, @len will be set to the
  596. * amount written. This means you don't need to clear the buffer
  597. * beforehand to ensure there's no data leakage in the case of short
  598. * writes.
  599. *
  600. * Caller must ensure we don't call this with other virtqueue
  601. * operations at the same time (except where noted).
  602. *
  603. * Returns NULL if there are no used buffers, or the "data" token
  604. * handed to virtqueue_add_*().
  605. */
  606. void *virtqueue_get_buf_ctx(struct virtqueue *_vq, unsigned int *len,
  607. void **ctx)
  608. {
  609. struct vring_virtqueue *vq = to_vvq(_vq);
  610. void *ret;
  611. unsigned int i;
  612. u16 last_used;
  613. START_USE(vq);
  614. if (unlikely(vq->broken)) {
  615. END_USE(vq);
  616. return NULL;
  617. }
  618. if (!more_used(vq)) {
  619. pr_debug("No more buffers in queue\n");
  620. END_USE(vq);
  621. return NULL;
  622. }
  623. /* Only get used array entries after they have been exposed by host. */
  624. virtio_rmb(vq->weak_barriers);
  625. last_used = (vq->last_used_idx & (vq->vring.num - 1));
  626. i = virtio32_to_cpu(_vq->vdev, vq->vring.used->ring[last_used].id);
  627. *len = virtio32_to_cpu(_vq->vdev, vq->vring.used->ring[last_used].len);
  628. if (unlikely(i >= vq->vring.num)) {
  629. BAD_RING(vq, "id %u out of range\n", i);
  630. return NULL;
  631. }
  632. if (unlikely(!vq->desc_state[i].data)) {
  633. BAD_RING(vq, "id %u is not a head!\n", i);
  634. return NULL;
  635. }
  636. /* detach_buf clears data, so grab it now. */
  637. ret = vq->desc_state[i].data;
  638. detach_buf(vq, i, ctx);
  639. vq->last_used_idx++;
  640. /* If we expect an interrupt for the next entry, tell host
  641. * by writing event index and flush out the write before
  642. * the read in the next get_buf call. */
  643. if (!(vq->avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT))
  644. virtio_store_mb(vq->weak_barriers,
  645. &vring_used_event(&vq->vring),
  646. cpu_to_virtio16(_vq->vdev, vq->last_used_idx));
  647. #ifdef DEBUG
  648. vq->last_add_time_valid = false;
  649. #endif
  650. END_USE(vq);
  651. return ret;
  652. }
  653. EXPORT_SYMBOL_GPL(virtqueue_get_buf_ctx);
  654. void *virtqueue_get_buf(struct virtqueue *_vq, unsigned int *len)
  655. {
  656. return virtqueue_get_buf_ctx(_vq, len, NULL);
  657. }
  658. EXPORT_SYMBOL_GPL(virtqueue_get_buf);
  659. /**
  660. * virtqueue_disable_cb - disable callbacks
  661. * @vq: the struct virtqueue we're talking about.
  662. *
  663. * Note that this is not necessarily synchronous, hence unreliable and only
  664. * useful as an optimization.
  665. *
  666. * Unlike other operations, this need not be serialized.
  667. */
  668. void virtqueue_disable_cb(struct virtqueue *_vq)
  669. {
  670. struct vring_virtqueue *vq = to_vvq(_vq);
  671. if (!(vq->avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT)) {
  672. vq->avail_flags_shadow |= VRING_AVAIL_F_NO_INTERRUPT;
  673. if (!vq->event)
  674. vq->vring.avail->flags = cpu_to_virtio16(_vq->vdev, vq->avail_flags_shadow);
  675. }
  676. }
  677. EXPORT_SYMBOL_GPL(virtqueue_disable_cb);
  678. /**
  679. * virtqueue_enable_cb_prepare - restart callbacks after disable_cb
  680. * @vq: the struct virtqueue we're talking about.
  681. *
  682. * This re-enables callbacks; it returns current queue state
  683. * in an opaque unsigned value. This value should be later tested by
  684. * virtqueue_poll, to detect a possible race between the driver checking for
  685. * more work, and enabling callbacks.
  686. *
  687. * Caller must ensure we don't call this with other virtqueue
  688. * operations at the same time (except where noted).
  689. */
  690. unsigned virtqueue_enable_cb_prepare(struct virtqueue *_vq)
  691. {
  692. struct vring_virtqueue *vq = to_vvq(_vq);
  693. u16 last_used_idx;
  694. START_USE(vq);
  695. /* We optimistically turn back on interrupts, then check if there was
  696. * more to do. */
  697. /* Depending on the VIRTIO_RING_F_EVENT_IDX feature, we need to
  698. * either clear the flags bit or point the event index at the next
  699. * entry. Always do both to keep code simple. */
  700. if (vq->avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT) {
  701. vq->avail_flags_shadow &= ~VRING_AVAIL_F_NO_INTERRUPT;
  702. if (!vq->event)
  703. vq->vring.avail->flags = cpu_to_virtio16(_vq->vdev, vq->avail_flags_shadow);
  704. }
  705. vring_used_event(&vq->vring) = cpu_to_virtio16(_vq->vdev, last_used_idx = vq->last_used_idx);
  706. END_USE(vq);
  707. return last_used_idx;
  708. }
  709. EXPORT_SYMBOL_GPL(virtqueue_enable_cb_prepare);
  710. /**
  711. * virtqueue_poll - query pending used buffers
  712. * @vq: the struct virtqueue we're talking about.
  713. * @last_used_idx: virtqueue state (from call to virtqueue_enable_cb_prepare).
  714. *
  715. * Returns "true" if there are pending used buffers in the queue.
  716. *
  717. * This does not need to be serialized.
  718. */
  719. bool virtqueue_poll(struct virtqueue *_vq, unsigned last_used_idx)
  720. {
  721. struct vring_virtqueue *vq = to_vvq(_vq);
  722. virtio_mb(vq->weak_barriers);
  723. return (u16)last_used_idx != virtio16_to_cpu(_vq->vdev, vq->vring.used->idx);
  724. }
  725. EXPORT_SYMBOL_GPL(virtqueue_poll);
  726. /**
  727. * virtqueue_enable_cb - restart callbacks after disable_cb.
  728. * @vq: the struct virtqueue we're talking about.
  729. *
  730. * This re-enables callbacks; it returns "false" if there are pending
  731. * buffers in the queue, to detect a possible race between the driver
  732. * checking for more work, and enabling callbacks.
  733. *
  734. * Caller must ensure we don't call this with other virtqueue
  735. * operations at the same time (except where noted).
  736. */
  737. bool virtqueue_enable_cb(struct virtqueue *_vq)
  738. {
  739. unsigned last_used_idx = virtqueue_enable_cb_prepare(_vq);
  740. return !virtqueue_poll(_vq, last_used_idx);
  741. }
  742. EXPORT_SYMBOL_GPL(virtqueue_enable_cb);
  743. /**
  744. * virtqueue_enable_cb_delayed - restart callbacks after disable_cb.
  745. * @vq: the struct virtqueue we're talking about.
  746. *
  747. * This re-enables callbacks but hints to the other side to delay
  748. * interrupts until most of the available buffers have been processed;
  749. * it returns "false" if there are many pending buffers in the queue,
  750. * to detect a possible race between the driver checking for more work,
  751. * and enabling callbacks.
  752. *
  753. * Caller must ensure we don't call this with other virtqueue
  754. * operations at the same time (except where noted).
  755. */
  756. bool virtqueue_enable_cb_delayed(struct virtqueue *_vq)
  757. {
  758. struct vring_virtqueue *vq = to_vvq(_vq);
  759. u16 bufs;
  760. START_USE(vq);
  761. /* We optimistically turn back on interrupts, then check if there was
  762. * more to do. */
  763. /* Depending on the VIRTIO_RING_F_USED_EVENT_IDX feature, we need to
  764. * either clear the flags bit or point the event index at the next
  765. * entry. Always update the event index to keep code simple. */
  766. if (vq->avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT) {
  767. vq->avail_flags_shadow &= ~VRING_AVAIL_F_NO_INTERRUPT;
  768. if (!vq->event)
  769. vq->vring.avail->flags = cpu_to_virtio16(_vq->vdev, vq->avail_flags_shadow);
  770. }
  771. /* TODO: tune this threshold */
  772. bufs = (u16)(vq->avail_idx_shadow - vq->last_used_idx) * 3 / 4;
  773. virtio_store_mb(vq->weak_barriers,
  774. &vring_used_event(&vq->vring),
  775. cpu_to_virtio16(_vq->vdev, vq->last_used_idx + bufs));
  776. if (unlikely((u16)(virtio16_to_cpu(_vq->vdev, vq->vring.used->idx) - vq->last_used_idx) > bufs)) {
  777. END_USE(vq);
  778. return false;
  779. }
  780. END_USE(vq);
  781. return true;
  782. }
  783. EXPORT_SYMBOL_GPL(virtqueue_enable_cb_delayed);
  784. /**
  785. * virtqueue_detach_unused_buf - detach first unused buffer
  786. * @vq: the struct virtqueue we're talking about.
  787. *
  788. * Returns NULL or the "data" token handed to virtqueue_add_*().
  789. * This is not valid on an active queue; it is useful only for device
  790. * shutdown.
  791. */
  792. void *virtqueue_detach_unused_buf(struct virtqueue *_vq)
  793. {
  794. struct vring_virtqueue *vq = to_vvq(_vq);
  795. unsigned int i;
  796. void *buf;
  797. START_USE(vq);
  798. for (i = 0; i < vq->vring.num; i++) {
  799. if (!vq->desc_state[i].data)
  800. continue;
  801. /* detach_buf clears data, so grab it now. */
  802. buf = vq->desc_state[i].data;
  803. detach_buf(vq, i, NULL);
  804. vq->avail_idx_shadow--;
  805. vq->vring.avail->idx = cpu_to_virtio16(_vq->vdev, vq->avail_idx_shadow);
  806. END_USE(vq);
  807. return buf;
  808. }
  809. /* That should have freed everything. */
  810. BUG_ON(vq->vq.num_free != vq->vring.num);
  811. END_USE(vq);
  812. return NULL;
  813. }
  814. EXPORT_SYMBOL_GPL(virtqueue_detach_unused_buf);
  815. irqreturn_t vring_interrupt(int irq, void *_vq)
  816. {
  817. struct vring_virtqueue *vq = to_vvq(_vq);
  818. if (!more_used(vq)) {
  819. pr_debug("virtqueue interrupt with no work for %p\n", vq);
  820. return IRQ_NONE;
  821. }
  822. if (unlikely(vq->broken))
  823. return IRQ_HANDLED;
  824. pr_debug("virtqueue callback for %p (%p)\n", vq, vq->vq.callback);
  825. if (vq->vq.callback)
  826. vq->vq.callback(&vq->vq);
  827. return IRQ_HANDLED;
  828. }
  829. EXPORT_SYMBOL_GPL(vring_interrupt);
  830. struct virtqueue *__vring_new_virtqueue(unsigned int index,
  831. struct vring vring,
  832. struct virtio_device *vdev,
  833. bool weak_barriers,
  834. bool context,
  835. bool (*notify)(struct virtqueue *),
  836. void (*callback)(struct virtqueue *),
  837. const char *name)
  838. {
  839. unsigned int i;
  840. struct vring_virtqueue *vq;
  841. vq = kmalloc(sizeof(*vq) + vring.num * sizeof(struct vring_desc_state),
  842. GFP_KERNEL);
  843. if (!vq)
  844. return NULL;
  845. vq->vring = vring;
  846. vq->vq.callback = callback;
  847. vq->vq.vdev = vdev;
  848. vq->vq.name = name;
  849. vq->vq.num_free = vring.num;
  850. vq->vq.index = index;
  851. vq->we_own_ring = false;
  852. vq->queue_dma_addr = 0;
  853. vq->queue_size_in_bytes = 0;
  854. vq->notify = notify;
  855. vq->weak_barriers = weak_barriers;
  856. vq->broken = false;
  857. vq->last_used_idx = 0;
  858. vq->avail_flags_shadow = 0;
  859. vq->avail_idx_shadow = 0;
  860. vq->num_added = 0;
  861. list_add_tail(&vq->vq.list, &vdev->vqs);
  862. #ifdef DEBUG
  863. vq->in_use = false;
  864. vq->last_add_time_valid = false;
  865. #endif
  866. vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC) &&
  867. !context;
  868. vq->event = virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX);
  869. /* No callback? Tell other side not to bother us. */
  870. if (!callback) {
  871. vq->avail_flags_shadow |= VRING_AVAIL_F_NO_INTERRUPT;
  872. if (!vq->event)
  873. vq->vring.avail->flags = cpu_to_virtio16(vdev, vq->avail_flags_shadow);
  874. }
  875. /* Put everything in free lists. */
  876. vq->free_head = 0;
  877. for (i = 0; i < vring.num-1; i++)
  878. vq->vring.desc[i].next = cpu_to_virtio16(vdev, i + 1);
  879. memset(vq->desc_state, 0, vring.num * sizeof(struct vring_desc_state));
  880. return &vq->vq;
  881. }
  882. EXPORT_SYMBOL_GPL(__vring_new_virtqueue);
  883. static void *vring_alloc_queue(struct virtio_device *vdev, size_t size,
  884. dma_addr_t *dma_handle, gfp_t flag)
  885. {
  886. if (vring_use_dma_api(vdev)) {
  887. return dma_alloc_coherent(vdev->dev.parent, size,
  888. dma_handle, flag);
  889. } else {
  890. void *queue = alloc_pages_exact(PAGE_ALIGN(size), flag);
  891. if (queue) {
  892. phys_addr_t phys_addr = virt_to_phys(queue);
  893. *dma_handle = (dma_addr_t)phys_addr;
  894. /*
  895. * Sanity check: make sure we dind't truncate
  896. * the address. The only arches I can find that
  897. * have 64-bit phys_addr_t but 32-bit dma_addr_t
  898. * are certain non-highmem MIPS and x86
  899. * configurations, but these configurations
  900. * should never allocate physical pages above 32
  901. * bits, so this is fine. Just in case, throw a
  902. * warning and abort if we end up with an
  903. * unrepresentable address.
  904. */
  905. if (WARN_ON_ONCE(*dma_handle != phys_addr)) {
  906. free_pages_exact(queue, PAGE_ALIGN(size));
  907. return NULL;
  908. }
  909. }
  910. return queue;
  911. }
  912. }
  913. static void vring_free_queue(struct virtio_device *vdev, size_t size,
  914. void *queue, dma_addr_t dma_handle)
  915. {
  916. if (vring_use_dma_api(vdev)) {
  917. dma_free_coherent(vdev->dev.parent, size, queue, dma_handle);
  918. } else {
  919. free_pages_exact(queue, PAGE_ALIGN(size));
  920. }
  921. }
  922. struct virtqueue *vring_create_virtqueue(
  923. unsigned int index,
  924. unsigned int num,
  925. unsigned int vring_align,
  926. struct virtio_device *vdev,
  927. bool weak_barriers,
  928. bool may_reduce_num,
  929. bool context,
  930. bool (*notify)(struct virtqueue *),
  931. void (*callback)(struct virtqueue *),
  932. const char *name)
  933. {
  934. struct virtqueue *vq;
  935. void *queue = NULL;
  936. dma_addr_t dma_addr;
  937. size_t queue_size_in_bytes;
  938. struct vring vring;
  939. /* We assume num is a power of 2. */
  940. if (num & (num - 1)) {
  941. dev_warn(&vdev->dev, "Bad virtqueue length %u\n", num);
  942. return NULL;
  943. }
  944. /* TODO: allocate each queue chunk individually */
  945. for (; num && vring_size(num, vring_align) > PAGE_SIZE; num /= 2) {
  946. queue = vring_alloc_queue(vdev, vring_size(num, vring_align),
  947. &dma_addr,
  948. GFP_KERNEL|__GFP_NOWARN|__GFP_ZERO);
  949. if (queue)
  950. break;
  951. }
  952. if (!num)
  953. return NULL;
  954. if (!queue) {
  955. /* Try to get a single page. You are my only hope! */
  956. queue = vring_alloc_queue(vdev, vring_size(num, vring_align),
  957. &dma_addr, GFP_KERNEL|__GFP_ZERO);
  958. }
  959. if (!queue)
  960. return NULL;
  961. queue_size_in_bytes = vring_size(num, vring_align);
  962. vring_init(&vring, num, queue, vring_align);
  963. vq = __vring_new_virtqueue(index, vring, vdev, weak_barriers, context,
  964. notify, callback, name);
  965. if (!vq) {
  966. vring_free_queue(vdev, queue_size_in_bytes, queue,
  967. dma_addr);
  968. return NULL;
  969. }
  970. to_vvq(vq)->queue_dma_addr = dma_addr;
  971. to_vvq(vq)->queue_size_in_bytes = queue_size_in_bytes;
  972. to_vvq(vq)->we_own_ring = true;
  973. return vq;
  974. }
  975. EXPORT_SYMBOL_GPL(vring_create_virtqueue);
  976. struct virtqueue *vring_new_virtqueue(unsigned int index,
  977. unsigned int num,
  978. unsigned int vring_align,
  979. struct virtio_device *vdev,
  980. bool weak_barriers,
  981. bool context,
  982. void *pages,
  983. bool (*notify)(struct virtqueue *vq),
  984. void (*callback)(struct virtqueue *vq),
  985. const char *name)
  986. {
  987. struct vring vring;
  988. vring_init(&vring, num, pages, vring_align);
  989. return __vring_new_virtqueue(index, vring, vdev, weak_barriers, context,
  990. notify, callback, name);
  991. }
  992. EXPORT_SYMBOL_GPL(vring_new_virtqueue);
  993. void vring_del_virtqueue(struct virtqueue *_vq)
  994. {
  995. struct vring_virtqueue *vq = to_vvq(_vq);
  996. if (vq->we_own_ring) {
  997. vring_free_queue(vq->vq.vdev, vq->queue_size_in_bytes,
  998. vq->vring.desc, vq->queue_dma_addr);
  999. }
  1000. list_del(&_vq->list);
  1001. kfree(vq);
  1002. }
  1003. EXPORT_SYMBOL_GPL(vring_del_virtqueue);
  1004. /* Manipulates transport-specific feature bits. */
  1005. void vring_transport_features(struct virtio_device *vdev)
  1006. {
  1007. unsigned int i;
  1008. for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++) {
  1009. switch (i) {
  1010. case VIRTIO_RING_F_INDIRECT_DESC:
  1011. break;
  1012. case VIRTIO_RING_F_EVENT_IDX:
  1013. break;
  1014. case VIRTIO_F_VERSION_1:
  1015. break;
  1016. case VIRTIO_F_IOMMU_PLATFORM:
  1017. break;
  1018. default:
  1019. /* We don't understand this bit. */
  1020. __virtio_clear_bit(vdev, i);
  1021. }
  1022. }
  1023. }
  1024. EXPORT_SYMBOL_GPL(vring_transport_features);
  1025. /**
  1026. * virtqueue_get_vring_size - return the size of the virtqueue's vring
  1027. * @vq: the struct virtqueue containing the vring of interest.
  1028. *
  1029. * Returns the size of the vring. This is mainly used for boasting to
  1030. * userspace. Unlike other operations, this need not be serialized.
  1031. */
  1032. unsigned int virtqueue_get_vring_size(struct virtqueue *_vq)
  1033. {
  1034. struct vring_virtqueue *vq = to_vvq(_vq);
  1035. return vq->vring.num;
  1036. }
  1037. EXPORT_SYMBOL_GPL(virtqueue_get_vring_size);
  1038. bool virtqueue_is_broken(struct virtqueue *_vq)
  1039. {
  1040. struct vring_virtqueue *vq = to_vvq(_vq);
  1041. return vq->broken;
  1042. }
  1043. EXPORT_SYMBOL_GPL(virtqueue_is_broken);
  1044. /*
  1045. * This should prevent the device from being used, allowing drivers to
  1046. * recover. You may need to grab appropriate locks to flush.
  1047. */
  1048. void virtio_break_device(struct virtio_device *dev)
  1049. {
  1050. struct virtqueue *_vq;
  1051. list_for_each_entry(_vq, &dev->vqs, list) {
  1052. struct vring_virtqueue *vq = to_vvq(_vq);
  1053. vq->broken = true;
  1054. }
  1055. }
  1056. EXPORT_SYMBOL_GPL(virtio_break_device);
  1057. dma_addr_t virtqueue_get_desc_addr(struct virtqueue *_vq)
  1058. {
  1059. struct vring_virtqueue *vq = to_vvq(_vq);
  1060. BUG_ON(!vq->we_own_ring);
  1061. return vq->queue_dma_addr;
  1062. }
  1063. EXPORT_SYMBOL_GPL(virtqueue_get_desc_addr);
  1064. dma_addr_t virtqueue_get_avail_addr(struct virtqueue *_vq)
  1065. {
  1066. struct vring_virtqueue *vq = to_vvq(_vq);
  1067. BUG_ON(!vq->we_own_ring);
  1068. return vq->queue_dma_addr +
  1069. ((char *)vq->vring.avail - (char *)vq->vring.desc);
  1070. }
  1071. EXPORT_SYMBOL_GPL(virtqueue_get_avail_addr);
  1072. dma_addr_t virtqueue_get_used_addr(struct virtqueue *_vq)
  1073. {
  1074. struct vring_virtqueue *vq = to_vvq(_vq);
  1075. BUG_ON(!vq->we_own_ring);
  1076. return vq->queue_dma_addr +
  1077. ((char *)vq->vring.used - (char *)vq->vring.desc);
  1078. }
  1079. EXPORT_SYMBOL_GPL(virtqueue_get_used_addr);
  1080. const struct vring *virtqueue_get_vring(struct virtqueue *vq)
  1081. {
  1082. return &to_vvq(vq)->vring;
  1083. }
  1084. EXPORT_SYMBOL_GPL(virtqueue_get_vring);
  1085. MODULE_LICENSE("GPL");