virtio_ring.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259
  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. if (indirect)
  367. kfree(desc);
  368. END_USE(vq);
  369. return -ENOMEM;
  370. }
  371. /**
  372. * virtqueue_add_sgs - expose buffers to other end
  373. * @vq: the struct virtqueue we're talking about.
  374. * @sgs: array of terminated scatterlists.
  375. * @out_num: the number of scatterlists readable by other side
  376. * @in_num: the number of scatterlists which are writable (after readable ones)
  377. * @data: the token identifying the buffer.
  378. * @gfp: how to do memory allocations (if necessary).
  379. *
  380. * Caller must ensure we don't call this with other virtqueue operations
  381. * at the same time (except where noted).
  382. *
  383. * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO).
  384. */
  385. int virtqueue_add_sgs(struct virtqueue *_vq,
  386. struct scatterlist *sgs[],
  387. unsigned int out_sgs,
  388. unsigned int in_sgs,
  389. void *data,
  390. gfp_t gfp)
  391. {
  392. unsigned int i, total_sg = 0;
  393. /* Count them first. */
  394. for (i = 0; i < out_sgs + in_sgs; i++) {
  395. struct scatterlist *sg;
  396. for (sg = sgs[i]; sg; sg = sg_next(sg))
  397. total_sg++;
  398. }
  399. return virtqueue_add(_vq, sgs, total_sg, out_sgs, in_sgs,
  400. data, NULL, gfp);
  401. }
  402. EXPORT_SYMBOL_GPL(virtqueue_add_sgs);
  403. /**
  404. * virtqueue_add_outbuf - expose output buffers to other end
  405. * @vq: the struct virtqueue we're talking about.
  406. * @sg: scatterlist (must be well-formed and terminated!)
  407. * @num: the number of entries in @sg readable by other side
  408. * @data: the token identifying the buffer.
  409. * @gfp: how to do memory allocations (if necessary).
  410. *
  411. * Caller must ensure we don't call this with other virtqueue operations
  412. * at the same time (except where noted).
  413. *
  414. * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO).
  415. */
  416. int virtqueue_add_outbuf(struct virtqueue *vq,
  417. struct scatterlist *sg, unsigned int num,
  418. void *data,
  419. gfp_t gfp)
  420. {
  421. return virtqueue_add(vq, &sg, num, 1, 0, data, NULL, gfp);
  422. }
  423. EXPORT_SYMBOL_GPL(virtqueue_add_outbuf);
  424. /**
  425. * virtqueue_add_inbuf - expose input buffers to other end
  426. * @vq: the struct virtqueue we're talking about.
  427. * @sg: scatterlist (must be well-formed and terminated!)
  428. * @num: the number of entries in @sg writable by other side
  429. * @data: the token identifying the buffer.
  430. * @gfp: how to do memory allocations (if necessary).
  431. *
  432. * Caller must ensure we don't call this with other virtqueue operations
  433. * at the same time (except where noted).
  434. *
  435. * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO).
  436. */
  437. int virtqueue_add_inbuf(struct virtqueue *vq,
  438. struct scatterlist *sg, unsigned int num,
  439. void *data,
  440. gfp_t gfp)
  441. {
  442. return virtqueue_add(vq, &sg, num, 0, 1, data, NULL, gfp);
  443. }
  444. EXPORT_SYMBOL_GPL(virtqueue_add_inbuf);
  445. /**
  446. * virtqueue_add_inbuf_ctx - expose input buffers to other end
  447. * @vq: the struct virtqueue we're talking about.
  448. * @sg: scatterlist (must be well-formed and terminated!)
  449. * @num: the number of entries in @sg writable by other side
  450. * @data: the token identifying the buffer.
  451. * @ctx: extra context for the token
  452. * @gfp: how to do memory allocations (if necessary).
  453. *
  454. * Caller must ensure we don't call this with other virtqueue operations
  455. * at the same time (except where noted).
  456. *
  457. * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO).
  458. */
  459. int virtqueue_add_inbuf_ctx(struct virtqueue *vq,
  460. struct scatterlist *sg, unsigned int num,
  461. void *data,
  462. void *ctx,
  463. gfp_t gfp)
  464. {
  465. return virtqueue_add(vq, &sg, num, 0, 1, data, ctx, gfp);
  466. }
  467. EXPORT_SYMBOL_GPL(virtqueue_add_inbuf_ctx);
  468. /**
  469. * virtqueue_kick_prepare - first half of split virtqueue_kick call.
  470. * @vq: the struct virtqueue
  471. *
  472. * Instead of virtqueue_kick(), you can do:
  473. * if (virtqueue_kick_prepare(vq))
  474. * virtqueue_notify(vq);
  475. *
  476. * This is sometimes useful because the virtqueue_kick_prepare() needs
  477. * to be serialized, but the actual virtqueue_notify() call does not.
  478. */
  479. bool virtqueue_kick_prepare(struct virtqueue *_vq)
  480. {
  481. struct vring_virtqueue *vq = to_vvq(_vq);
  482. u16 new, old;
  483. bool needs_kick;
  484. START_USE(vq);
  485. /* We need to expose available array entries before checking avail
  486. * event. */
  487. virtio_mb(vq->weak_barriers);
  488. old = vq->avail_idx_shadow - vq->num_added;
  489. new = vq->avail_idx_shadow;
  490. vq->num_added = 0;
  491. #ifdef DEBUG
  492. if (vq->last_add_time_valid) {
  493. WARN_ON(ktime_to_ms(ktime_sub(ktime_get(),
  494. vq->last_add_time)) > 100);
  495. }
  496. vq->last_add_time_valid = false;
  497. #endif
  498. if (vq->event) {
  499. needs_kick = vring_need_event(virtio16_to_cpu(_vq->vdev, vring_avail_event(&vq->vring)),
  500. new, old);
  501. } else {
  502. needs_kick = !(vq->vring.used->flags & cpu_to_virtio16(_vq->vdev, VRING_USED_F_NO_NOTIFY));
  503. }
  504. END_USE(vq);
  505. return needs_kick;
  506. }
  507. EXPORT_SYMBOL_GPL(virtqueue_kick_prepare);
  508. /**
  509. * virtqueue_notify - second half of split virtqueue_kick call.
  510. * @vq: the struct virtqueue
  511. *
  512. * This does not need to be serialized.
  513. *
  514. * Returns false if host notify failed or queue is broken, otherwise true.
  515. */
  516. bool virtqueue_notify(struct virtqueue *_vq)
  517. {
  518. struct vring_virtqueue *vq = to_vvq(_vq);
  519. if (unlikely(vq->broken))
  520. return false;
  521. /* Prod other side to tell it about changes. */
  522. if (!vq->notify(_vq)) {
  523. vq->broken = true;
  524. return false;
  525. }
  526. return true;
  527. }
  528. EXPORT_SYMBOL_GPL(virtqueue_notify);
  529. /**
  530. * virtqueue_kick - update after add_buf
  531. * @vq: the struct virtqueue
  532. *
  533. * After one or more virtqueue_add_* calls, invoke this to kick
  534. * the other side.
  535. *
  536. * Caller must ensure we don't call this with other virtqueue
  537. * operations at the same time (except where noted).
  538. *
  539. * Returns false if kick failed, otherwise true.
  540. */
  541. bool virtqueue_kick(struct virtqueue *vq)
  542. {
  543. if (virtqueue_kick_prepare(vq))
  544. return virtqueue_notify(vq);
  545. return true;
  546. }
  547. EXPORT_SYMBOL_GPL(virtqueue_kick);
  548. static void detach_buf(struct vring_virtqueue *vq, unsigned int head,
  549. void **ctx)
  550. {
  551. unsigned int i, j;
  552. __virtio16 nextflag = cpu_to_virtio16(vq->vq.vdev, VRING_DESC_F_NEXT);
  553. /* Clear data ptr. */
  554. vq->desc_state[head].data = NULL;
  555. /* Put back on free list: unmap first-level descriptors and find end */
  556. i = head;
  557. while (vq->vring.desc[i].flags & nextflag) {
  558. vring_unmap_one(vq, &vq->vring.desc[i]);
  559. i = virtio16_to_cpu(vq->vq.vdev, vq->vring.desc[i].next);
  560. vq->vq.num_free++;
  561. }
  562. vring_unmap_one(vq, &vq->vring.desc[i]);
  563. vq->vring.desc[i].next = cpu_to_virtio16(vq->vq.vdev, vq->free_head);
  564. vq->free_head = head;
  565. /* Plus final descriptor */
  566. vq->vq.num_free++;
  567. if (vq->indirect) {
  568. struct vring_desc *indir_desc = vq->desc_state[head].indir_desc;
  569. u32 len;
  570. /* Free the indirect table, if any, now that it's unmapped. */
  571. if (!indir_desc)
  572. return;
  573. len = virtio32_to_cpu(vq->vq.vdev, vq->vring.desc[head].len);
  574. BUG_ON(!(vq->vring.desc[head].flags &
  575. cpu_to_virtio16(vq->vq.vdev, VRING_DESC_F_INDIRECT)));
  576. BUG_ON(len == 0 || len % sizeof(struct vring_desc));
  577. for (j = 0; j < len / sizeof(struct vring_desc); j++)
  578. vring_unmap_one(vq, &indir_desc[j]);
  579. kfree(indir_desc);
  580. vq->desc_state[head].indir_desc = NULL;
  581. } else if (ctx) {
  582. *ctx = vq->desc_state[head].indir_desc;
  583. }
  584. }
  585. static inline bool more_used(const struct vring_virtqueue *vq)
  586. {
  587. return vq->last_used_idx != virtio16_to_cpu(vq->vq.vdev, vq->vring.used->idx);
  588. }
  589. /**
  590. * virtqueue_get_buf - get the next used buffer
  591. * @vq: the struct virtqueue we're talking about.
  592. * @len: the length written into the buffer
  593. *
  594. * If the device wrote data into the buffer, @len will be set to the
  595. * amount written. This means you don't need to clear the buffer
  596. * beforehand to ensure there's no data leakage in the case of short
  597. * writes.
  598. *
  599. * Caller must ensure we don't call this with other virtqueue
  600. * operations at the same time (except where noted).
  601. *
  602. * Returns NULL if there are no used buffers, or the "data" token
  603. * handed to virtqueue_add_*().
  604. */
  605. void *virtqueue_get_buf_ctx(struct virtqueue *_vq, unsigned int *len,
  606. void **ctx)
  607. {
  608. struct vring_virtqueue *vq = to_vvq(_vq);
  609. void *ret;
  610. unsigned int i;
  611. u16 last_used;
  612. START_USE(vq);
  613. if (unlikely(vq->broken)) {
  614. END_USE(vq);
  615. return NULL;
  616. }
  617. if (!more_used(vq)) {
  618. pr_debug("No more buffers in queue\n");
  619. END_USE(vq);
  620. return NULL;
  621. }
  622. /* Only get used array entries after they have been exposed by host. */
  623. virtio_rmb(vq->weak_barriers);
  624. last_used = (vq->last_used_idx & (vq->vring.num - 1));
  625. i = virtio32_to_cpu(_vq->vdev, vq->vring.used->ring[last_used].id);
  626. *len = virtio32_to_cpu(_vq->vdev, vq->vring.used->ring[last_used].len);
  627. if (unlikely(i >= vq->vring.num)) {
  628. BAD_RING(vq, "id %u out of range\n", i);
  629. return NULL;
  630. }
  631. if (unlikely(!vq->desc_state[i].data)) {
  632. BAD_RING(vq, "id %u is not a head!\n", i);
  633. return NULL;
  634. }
  635. /* detach_buf clears data, so grab it now. */
  636. ret = vq->desc_state[i].data;
  637. detach_buf(vq, i, ctx);
  638. vq->last_used_idx++;
  639. /* If we expect an interrupt for the next entry, tell host
  640. * by writing event index and flush out the write before
  641. * the read in the next get_buf call. */
  642. if (!(vq->avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT))
  643. virtio_store_mb(vq->weak_barriers,
  644. &vring_used_event(&vq->vring),
  645. cpu_to_virtio16(_vq->vdev, vq->last_used_idx));
  646. #ifdef DEBUG
  647. vq->last_add_time_valid = false;
  648. #endif
  649. END_USE(vq);
  650. return ret;
  651. }
  652. EXPORT_SYMBOL_GPL(virtqueue_get_buf_ctx);
  653. void *virtqueue_get_buf(struct virtqueue *_vq, unsigned int *len)
  654. {
  655. return virtqueue_get_buf_ctx(_vq, len, NULL);
  656. }
  657. EXPORT_SYMBOL_GPL(virtqueue_get_buf);
  658. /**
  659. * virtqueue_disable_cb - disable callbacks
  660. * @vq: the struct virtqueue we're talking about.
  661. *
  662. * Note that this is not necessarily synchronous, hence unreliable and only
  663. * useful as an optimization.
  664. *
  665. * Unlike other operations, this need not be serialized.
  666. */
  667. void virtqueue_disable_cb(struct virtqueue *_vq)
  668. {
  669. struct vring_virtqueue *vq = to_vvq(_vq);
  670. if (!(vq->avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT)) {
  671. vq->avail_flags_shadow |= VRING_AVAIL_F_NO_INTERRUPT;
  672. if (!vq->event)
  673. vq->vring.avail->flags = cpu_to_virtio16(_vq->vdev, vq->avail_flags_shadow);
  674. }
  675. }
  676. EXPORT_SYMBOL_GPL(virtqueue_disable_cb);
  677. /**
  678. * virtqueue_enable_cb_prepare - restart callbacks after disable_cb
  679. * @vq: the struct virtqueue we're talking about.
  680. *
  681. * This re-enables callbacks; it returns current queue state
  682. * in an opaque unsigned value. This value should be later tested by
  683. * virtqueue_poll, to detect a possible race between the driver checking for
  684. * more work, and enabling callbacks.
  685. *
  686. * Caller must ensure we don't call this with other virtqueue
  687. * operations at the same time (except where noted).
  688. */
  689. unsigned virtqueue_enable_cb_prepare(struct virtqueue *_vq)
  690. {
  691. struct vring_virtqueue *vq = to_vvq(_vq);
  692. u16 last_used_idx;
  693. START_USE(vq);
  694. /* We optimistically turn back on interrupts, then check if there was
  695. * more to do. */
  696. /* Depending on the VIRTIO_RING_F_EVENT_IDX feature, we need to
  697. * either clear the flags bit or point the event index at the next
  698. * entry. Always do both to keep code simple. */
  699. if (vq->avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT) {
  700. vq->avail_flags_shadow &= ~VRING_AVAIL_F_NO_INTERRUPT;
  701. if (!vq->event)
  702. vq->vring.avail->flags = cpu_to_virtio16(_vq->vdev, vq->avail_flags_shadow);
  703. }
  704. vring_used_event(&vq->vring) = cpu_to_virtio16(_vq->vdev, last_used_idx = vq->last_used_idx);
  705. END_USE(vq);
  706. return last_used_idx;
  707. }
  708. EXPORT_SYMBOL_GPL(virtqueue_enable_cb_prepare);
  709. /**
  710. * virtqueue_poll - query pending used buffers
  711. * @vq: the struct virtqueue we're talking about.
  712. * @last_used_idx: virtqueue state (from call to virtqueue_enable_cb_prepare).
  713. *
  714. * Returns "true" if there are pending used buffers in the queue.
  715. *
  716. * This does not need to be serialized.
  717. */
  718. bool virtqueue_poll(struct virtqueue *_vq, unsigned last_used_idx)
  719. {
  720. struct vring_virtqueue *vq = to_vvq(_vq);
  721. if (unlikely(vq->broken))
  722. return false;
  723. virtio_mb(vq->weak_barriers);
  724. return (u16)last_used_idx != virtio16_to_cpu(_vq->vdev, vq->vring.used->idx);
  725. }
  726. EXPORT_SYMBOL_GPL(virtqueue_poll);
  727. /**
  728. * virtqueue_enable_cb - restart callbacks after disable_cb.
  729. * @vq: the struct virtqueue we're talking about.
  730. *
  731. * This re-enables callbacks; it returns "false" if there are pending
  732. * buffers in the queue, to detect a possible race between the driver
  733. * checking for more work, and enabling callbacks.
  734. *
  735. * Caller must ensure we don't call this with other virtqueue
  736. * operations at the same time (except where noted).
  737. */
  738. bool virtqueue_enable_cb(struct virtqueue *_vq)
  739. {
  740. unsigned last_used_idx = virtqueue_enable_cb_prepare(_vq);
  741. return !virtqueue_poll(_vq, last_used_idx);
  742. }
  743. EXPORT_SYMBOL_GPL(virtqueue_enable_cb);
  744. /**
  745. * virtqueue_enable_cb_delayed - restart callbacks after disable_cb.
  746. * @vq: the struct virtqueue we're talking about.
  747. *
  748. * This re-enables callbacks but hints to the other side to delay
  749. * interrupts until most of the available buffers have been processed;
  750. * it returns "false" if there are many pending buffers in the queue,
  751. * to detect a possible race between the driver checking for more work,
  752. * and enabling callbacks.
  753. *
  754. * Caller must ensure we don't call this with other virtqueue
  755. * operations at the same time (except where noted).
  756. */
  757. bool virtqueue_enable_cb_delayed(struct virtqueue *_vq)
  758. {
  759. struct vring_virtqueue *vq = to_vvq(_vq);
  760. u16 bufs;
  761. START_USE(vq);
  762. /* We optimistically turn back on interrupts, then check if there was
  763. * more to do. */
  764. /* Depending on the VIRTIO_RING_F_USED_EVENT_IDX feature, we need to
  765. * either clear the flags bit or point the event index at the next
  766. * entry. Always update the event index to keep code simple. */
  767. if (vq->avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT) {
  768. vq->avail_flags_shadow &= ~VRING_AVAIL_F_NO_INTERRUPT;
  769. if (!vq->event)
  770. vq->vring.avail->flags = cpu_to_virtio16(_vq->vdev, vq->avail_flags_shadow);
  771. }
  772. /* TODO: tune this threshold */
  773. bufs = (u16)(vq->avail_idx_shadow - vq->last_used_idx) * 3 / 4;
  774. virtio_store_mb(vq->weak_barriers,
  775. &vring_used_event(&vq->vring),
  776. cpu_to_virtio16(_vq->vdev, vq->last_used_idx + bufs));
  777. if (unlikely((u16)(virtio16_to_cpu(_vq->vdev, vq->vring.used->idx) - vq->last_used_idx) > bufs)) {
  778. END_USE(vq);
  779. return false;
  780. }
  781. END_USE(vq);
  782. return true;
  783. }
  784. EXPORT_SYMBOL_GPL(virtqueue_enable_cb_delayed);
  785. /**
  786. * virtqueue_detach_unused_buf - detach first unused buffer
  787. * @vq: the struct virtqueue we're talking about.
  788. *
  789. * Returns NULL or the "data" token handed to virtqueue_add_*().
  790. * This is not valid on an active queue; it is useful only for device
  791. * shutdown.
  792. */
  793. void *virtqueue_detach_unused_buf(struct virtqueue *_vq)
  794. {
  795. struct vring_virtqueue *vq = to_vvq(_vq);
  796. unsigned int i;
  797. void *buf;
  798. START_USE(vq);
  799. for (i = 0; i < vq->vring.num; i++) {
  800. if (!vq->desc_state[i].data)
  801. continue;
  802. /* detach_buf clears data, so grab it now. */
  803. buf = vq->desc_state[i].data;
  804. detach_buf(vq, i, NULL);
  805. vq->avail_idx_shadow--;
  806. vq->vring.avail->idx = cpu_to_virtio16(_vq->vdev, vq->avail_idx_shadow);
  807. END_USE(vq);
  808. return buf;
  809. }
  810. /* That should have freed everything. */
  811. BUG_ON(vq->vq.num_free != vq->vring.num);
  812. END_USE(vq);
  813. return NULL;
  814. }
  815. EXPORT_SYMBOL_GPL(virtqueue_detach_unused_buf);
  816. irqreturn_t vring_interrupt(int irq, void *_vq)
  817. {
  818. struct vring_virtqueue *vq = to_vvq(_vq);
  819. if (!more_used(vq)) {
  820. pr_debug("virtqueue interrupt with no work for %p\n", vq);
  821. return IRQ_NONE;
  822. }
  823. if (unlikely(vq->broken))
  824. return IRQ_HANDLED;
  825. pr_debug("virtqueue callback for %p (%p)\n", vq, vq->vq.callback);
  826. if (vq->vq.callback)
  827. vq->vq.callback(&vq->vq);
  828. return IRQ_HANDLED;
  829. }
  830. EXPORT_SYMBOL_GPL(vring_interrupt);
  831. struct virtqueue *__vring_new_virtqueue(unsigned int index,
  832. struct vring vring,
  833. struct virtio_device *vdev,
  834. bool weak_barriers,
  835. bool context,
  836. bool (*notify)(struct virtqueue *),
  837. void (*callback)(struct virtqueue *),
  838. const char *name)
  839. {
  840. unsigned int i;
  841. struct vring_virtqueue *vq;
  842. vq = kmalloc(sizeof(*vq) + vring.num * sizeof(struct vring_desc_state),
  843. GFP_KERNEL);
  844. if (!vq)
  845. return NULL;
  846. vq->vring = vring;
  847. vq->vq.callback = callback;
  848. vq->vq.vdev = vdev;
  849. vq->vq.name = name;
  850. vq->vq.num_free = vring.num;
  851. vq->vq.index = index;
  852. vq->we_own_ring = false;
  853. vq->queue_dma_addr = 0;
  854. vq->queue_size_in_bytes = 0;
  855. vq->notify = notify;
  856. vq->weak_barriers = weak_barriers;
  857. vq->broken = false;
  858. vq->last_used_idx = 0;
  859. vq->avail_flags_shadow = 0;
  860. vq->avail_idx_shadow = 0;
  861. vq->num_added = 0;
  862. list_add_tail(&vq->vq.list, &vdev->vqs);
  863. #ifdef DEBUG
  864. vq->in_use = false;
  865. vq->last_add_time_valid = false;
  866. #endif
  867. vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC) &&
  868. !context;
  869. vq->event = virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX);
  870. /* No callback? Tell other side not to bother us. */
  871. if (!callback) {
  872. vq->avail_flags_shadow |= VRING_AVAIL_F_NO_INTERRUPT;
  873. if (!vq->event)
  874. vq->vring.avail->flags = cpu_to_virtio16(vdev, vq->avail_flags_shadow);
  875. }
  876. /* Put everything in free lists. */
  877. vq->free_head = 0;
  878. for (i = 0; i < vring.num-1; i++)
  879. vq->vring.desc[i].next = cpu_to_virtio16(vdev, i + 1);
  880. memset(vq->desc_state, 0, vring.num * sizeof(struct vring_desc_state));
  881. return &vq->vq;
  882. }
  883. EXPORT_SYMBOL_GPL(__vring_new_virtqueue);
  884. static void *vring_alloc_queue(struct virtio_device *vdev, size_t size,
  885. dma_addr_t *dma_handle, gfp_t flag)
  886. {
  887. if (vring_use_dma_api(vdev)) {
  888. return dma_alloc_coherent(vdev->dev.parent, size,
  889. dma_handle, flag);
  890. } else {
  891. void *queue = alloc_pages_exact(PAGE_ALIGN(size), flag);
  892. if (queue) {
  893. phys_addr_t phys_addr = virt_to_phys(queue);
  894. *dma_handle = (dma_addr_t)phys_addr;
  895. /*
  896. * Sanity check: make sure we dind't truncate
  897. * the address. The only arches I can find that
  898. * have 64-bit phys_addr_t but 32-bit dma_addr_t
  899. * are certain non-highmem MIPS and x86
  900. * configurations, but these configurations
  901. * should never allocate physical pages above 32
  902. * bits, so this is fine. Just in case, throw a
  903. * warning and abort if we end up with an
  904. * unrepresentable address.
  905. */
  906. if (WARN_ON_ONCE(*dma_handle != phys_addr)) {
  907. free_pages_exact(queue, PAGE_ALIGN(size));
  908. return NULL;
  909. }
  910. }
  911. return queue;
  912. }
  913. }
  914. static void vring_free_queue(struct virtio_device *vdev, size_t size,
  915. void *queue, dma_addr_t dma_handle)
  916. {
  917. if (vring_use_dma_api(vdev)) {
  918. dma_free_coherent(vdev->dev.parent, size, queue, dma_handle);
  919. } else {
  920. free_pages_exact(queue, PAGE_ALIGN(size));
  921. }
  922. }
  923. struct virtqueue *vring_create_virtqueue(
  924. unsigned int index,
  925. unsigned int num,
  926. unsigned int vring_align,
  927. struct virtio_device *vdev,
  928. bool weak_barriers,
  929. bool may_reduce_num,
  930. bool context,
  931. bool (*notify)(struct virtqueue *),
  932. void (*callback)(struct virtqueue *),
  933. const char *name)
  934. {
  935. struct virtqueue *vq;
  936. void *queue = NULL;
  937. dma_addr_t dma_addr;
  938. size_t queue_size_in_bytes;
  939. struct vring vring;
  940. /* We assume num is a power of 2. */
  941. if (num & (num - 1)) {
  942. dev_warn(&vdev->dev, "Bad virtqueue length %u\n", num);
  943. return NULL;
  944. }
  945. /* TODO: allocate each queue chunk individually */
  946. for (; num && vring_size(num, vring_align) > PAGE_SIZE; num /= 2) {
  947. queue = vring_alloc_queue(vdev, vring_size(num, vring_align),
  948. &dma_addr,
  949. GFP_KERNEL|__GFP_NOWARN|__GFP_ZERO);
  950. if (queue)
  951. break;
  952. if (!may_reduce_num)
  953. return NULL;
  954. }
  955. if (!num)
  956. return NULL;
  957. if (!queue) {
  958. /* Try to get a single page. You are my only hope! */
  959. queue = vring_alloc_queue(vdev, vring_size(num, vring_align),
  960. &dma_addr, GFP_KERNEL|__GFP_ZERO);
  961. }
  962. if (!queue)
  963. return NULL;
  964. queue_size_in_bytes = vring_size(num, vring_align);
  965. vring_init(&vring, num, queue, vring_align);
  966. vq = __vring_new_virtqueue(index, vring, vdev, weak_barriers, context,
  967. notify, callback, name);
  968. if (!vq) {
  969. vring_free_queue(vdev, queue_size_in_bytes, queue,
  970. dma_addr);
  971. return NULL;
  972. }
  973. to_vvq(vq)->queue_dma_addr = dma_addr;
  974. to_vvq(vq)->queue_size_in_bytes = queue_size_in_bytes;
  975. to_vvq(vq)->we_own_ring = true;
  976. return vq;
  977. }
  978. EXPORT_SYMBOL_GPL(vring_create_virtqueue);
  979. struct virtqueue *vring_new_virtqueue(unsigned int index,
  980. unsigned int num,
  981. unsigned int vring_align,
  982. struct virtio_device *vdev,
  983. bool weak_barriers,
  984. bool context,
  985. void *pages,
  986. bool (*notify)(struct virtqueue *vq),
  987. void (*callback)(struct virtqueue *vq),
  988. const char *name)
  989. {
  990. struct vring vring;
  991. vring_init(&vring, num, pages, vring_align);
  992. return __vring_new_virtqueue(index, vring, vdev, weak_barriers, context,
  993. notify, callback, name);
  994. }
  995. EXPORT_SYMBOL_GPL(vring_new_virtqueue);
  996. void vring_del_virtqueue(struct virtqueue *_vq)
  997. {
  998. struct vring_virtqueue *vq = to_vvq(_vq);
  999. if (vq->we_own_ring) {
  1000. vring_free_queue(vq->vq.vdev, vq->queue_size_in_bytes,
  1001. vq->vring.desc, vq->queue_dma_addr);
  1002. }
  1003. list_del(&_vq->list);
  1004. kfree(vq);
  1005. }
  1006. EXPORT_SYMBOL_GPL(vring_del_virtqueue);
  1007. /* Manipulates transport-specific feature bits. */
  1008. void vring_transport_features(struct virtio_device *vdev)
  1009. {
  1010. unsigned int i;
  1011. for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++) {
  1012. switch (i) {
  1013. case VIRTIO_RING_F_INDIRECT_DESC:
  1014. break;
  1015. case VIRTIO_RING_F_EVENT_IDX:
  1016. break;
  1017. case VIRTIO_F_VERSION_1:
  1018. break;
  1019. case VIRTIO_F_IOMMU_PLATFORM:
  1020. break;
  1021. default:
  1022. /* We don't understand this bit. */
  1023. __virtio_clear_bit(vdev, i);
  1024. }
  1025. }
  1026. }
  1027. EXPORT_SYMBOL_GPL(vring_transport_features);
  1028. /**
  1029. * virtqueue_get_vring_size - return the size of the virtqueue's vring
  1030. * @vq: the struct virtqueue containing the vring of interest.
  1031. *
  1032. * Returns the size of the vring. This is mainly used for boasting to
  1033. * userspace. Unlike other operations, this need not be serialized.
  1034. */
  1035. unsigned int virtqueue_get_vring_size(struct virtqueue *_vq)
  1036. {
  1037. struct vring_virtqueue *vq = to_vvq(_vq);
  1038. return vq->vring.num;
  1039. }
  1040. EXPORT_SYMBOL_GPL(virtqueue_get_vring_size);
  1041. bool virtqueue_is_broken(struct virtqueue *_vq)
  1042. {
  1043. struct vring_virtqueue *vq = to_vvq(_vq);
  1044. return vq->broken;
  1045. }
  1046. EXPORT_SYMBOL_GPL(virtqueue_is_broken);
  1047. /*
  1048. * This should prevent the device from being used, allowing drivers to
  1049. * recover. You may need to grab appropriate locks to flush.
  1050. */
  1051. void virtio_break_device(struct virtio_device *dev)
  1052. {
  1053. struct virtqueue *_vq;
  1054. list_for_each_entry(_vq, &dev->vqs, list) {
  1055. struct vring_virtqueue *vq = to_vvq(_vq);
  1056. vq->broken = true;
  1057. }
  1058. }
  1059. EXPORT_SYMBOL_GPL(virtio_break_device);
  1060. dma_addr_t virtqueue_get_desc_addr(struct virtqueue *_vq)
  1061. {
  1062. struct vring_virtqueue *vq = to_vvq(_vq);
  1063. BUG_ON(!vq->we_own_ring);
  1064. return vq->queue_dma_addr;
  1065. }
  1066. EXPORT_SYMBOL_GPL(virtqueue_get_desc_addr);
  1067. dma_addr_t virtqueue_get_avail_addr(struct virtqueue *_vq)
  1068. {
  1069. struct vring_virtqueue *vq = to_vvq(_vq);
  1070. BUG_ON(!vq->we_own_ring);
  1071. return vq->queue_dma_addr +
  1072. ((char *)vq->vring.avail - (char *)vq->vring.desc);
  1073. }
  1074. EXPORT_SYMBOL_GPL(virtqueue_get_avail_addr);
  1075. dma_addr_t virtqueue_get_used_addr(struct virtqueue *_vq)
  1076. {
  1077. struct vring_virtqueue *vq = to_vvq(_vq);
  1078. BUG_ON(!vq->we_own_ring);
  1079. return vq->queue_dma_addr +
  1080. ((char *)vq->vring.used - (char *)vq->vring.desc);
  1081. }
  1082. EXPORT_SYMBOL_GPL(virtqueue_get_used_addr);
  1083. const struct vring *virtqueue_get_vring(struct virtqueue *vq)
  1084. {
  1085. return &to_vvq(vq)->vring;
  1086. }
  1087. EXPORT_SYMBOL_GPL(virtqueue_get_vring);
  1088. MODULE_LICENSE("GPL");