virtio_ring.c 33 KB

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