virtio_ring.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  1. /* Virtio ring implementation.
  2. *
  3. * Copyright 2007 Rusty Russell IBM Corporation
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. #include <linux/virtio.h>
  20. #include <linux/virtio_ring.h>
  21. #include <linux/virtio_config.h>
  22. #include <linux/device.h>
  23. #include <linux/slab.h>
  24. #include <linux/module.h>
  25. /* virtio guest is communicating with a virtual "device" that actually runs on
  26. * a host processor. Memory barriers are used to control SMP effects. */
  27. #ifdef CONFIG_SMP
  28. /* Where possible, use SMP barriers which are more lightweight than mandatory
  29. * barriers, because mandatory barriers control MMIO effects on accesses
  30. * through relaxed memory I/O windows (which virtio-pci does not use). */
  31. #define virtio_mb(vq) \
  32. do { if ((vq)->weak_barriers) smp_mb(); else mb(); } while(0)
  33. #define virtio_rmb(vq) \
  34. do { if ((vq)->weak_barriers) smp_rmb(); else rmb(); } while(0)
  35. #define virtio_wmb(vq) \
  36. do { if ((vq)->weak_barriers) smp_rmb(); else rmb(); } while(0)
  37. #else
  38. /* We must force memory ordering even if guest is UP since host could be
  39. * running on another CPU, but SMP barriers are defined to barrier() in that
  40. * configuration. So fall back to mandatory barriers instead. */
  41. #define virtio_mb(vq) mb()
  42. #define virtio_rmb(vq) rmb()
  43. #define virtio_wmb(vq) wmb()
  44. #endif
  45. #ifdef DEBUG
  46. /* For development, we want to crash whenever the ring is screwed. */
  47. #define BAD_RING(_vq, fmt, args...) \
  48. do { \
  49. dev_err(&(_vq)->vq.vdev->dev, \
  50. "%s:"fmt, (_vq)->vq.name, ##args); \
  51. BUG(); \
  52. } while (0)
  53. /* Caller is supposed to guarantee no reentry. */
  54. #define START_USE(_vq) \
  55. do { \
  56. if ((_vq)->in_use) \
  57. panic("%s:in_use = %i\n", \
  58. (_vq)->vq.name, (_vq)->in_use); \
  59. (_vq)->in_use = __LINE__; \
  60. } while (0)
  61. #define END_USE(_vq) \
  62. do { BUG_ON(!(_vq)->in_use); (_vq)->in_use = 0; } while(0)
  63. #else
  64. #define BAD_RING(_vq, fmt, args...) \
  65. do { \
  66. dev_err(&_vq->vq.vdev->dev, \
  67. "%s:"fmt, (_vq)->vq.name, ##args); \
  68. (_vq)->broken = true; \
  69. } while (0)
  70. #define START_USE(vq)
  71. #define END_USE(vq)
  72. #endif
  73. struct vring_virtqueue
  74. {
  75. struct virtqueue vq;
  76. /* Actual memory layout for this queue */
  77. struct vring vring;
  78. /* Can we use weak barriers? */
  79. bool weak_barriers;
  80. /* Other side has made a mess, don't try any more. */
  81. bool broken;
  82. /* Host supports indirect buffers */
  83. bool indirect;
  84. /* Host publishes avail event idx */
  85. bool event;
  86. /* Number of free buffers */
  87. unsigned int num_free;
  88. /* Head of free buffer list. */
  89. unsigned int free_head;
  90. /* Number we've added since last sync. */
  91. unsigned int num_added;
  92. /* Last used index we've seen. */
  93. u16 last_used_idx;
  94. /* How to notify other side. FIXME: commonalize hcalls! */
  95. void (*notify)(struct virtqueue *vq);
  96. #ifdef DEBUG
  97. /* They're supposed to lock for us. */
  98. unsigned int in_use;
  99. #endif
  100. /* Tokens for callbacks. */
  101. void *data[];
  102. };
  103. #define to_vvq(_vq) container_of(_vq, struct vring_virtqueue, vq)
  104. /* Set up an indirect table of descriptors and add it to the queue. */
  105. static int vring_add_indirect(struct vring_virtqueue *vq,
  106. struct scatterlist sg[],
  107. unsigned int out,
  108. unsigned int in,
  109. gfp_t gfp)
  110. {
  111. struct vring_desc *desc;
  112. unsigned head;
  113. int i;
  114. desc = kmalloc((out + in) * sizeof(struct vring_desc), gfp);
  115. if (!desc)
  116. return -ENOMEM;
  117. /* Transfer entries from the sg list into the indirect page */
  118. for (i = 0; i < out; i++) {
  119. desc[i].flags = VRING_DESC_F_NEXT;
  120. desc[i].addr = sg_phys(sg);
  121. desc[i].len = sg->length;
  122. desc[i].next = i+1;
  123. sg++;
  124. }
  125. for (; i < (out + in); i++) {
  126. desc[i].flags = VRING_DESC_F_NEXT|VRING_DESC_F_WRITE;
  127. desc[i].addr = sg_phys(sg);
  128. desc[i].len = sg->length;
  129. desc[i].next = i+1;
  130. sg++;
  131. }
  132. /* Last one doesn't continue. */
  133. desc[i-1].flags &= ~VRING_DESC_F_NEXT;
  134. desc[i-1].next = 0;
  135. /* We're about to use a buffer */
  136. vq->num_free--;
  137. /* Use a single buffer which doesn't continue */
  138. head = vq->free_head;
  139. vq->vring.desc[head].flags = VRING_DESC_F_INDIRECT;
  140. vq->vring.desc[head].addr = virt_to_phys(desc);
  141. vq->vring.desc[head].len = i * sizeof(struct vring_desc);
  142. /* Update free pointer */
  143. vq->free_head = vq->vring.desc[head].next;
  144. return head;
  145. }
  146. /**
  147. * virtqueue_add_buf - expose buffer to other end
  148. * @vq: the struct virtqueue we're talking about.
  149. * @sg: the description of the buffer(s).
  150. * @out_num: the number of sg readable by other side
  151. * @in_num: the number of sg which are writable (after readable ones)
  152. * @data: the token identifying the buffer.
  153. * @gfp: how to do memory allocations (if necessary).
  154. *
  155. * Caller must ensure we don't call this with other virtqueue operations
  156. * at the same time (except where noted).
  157. *
  158. * Returns remaining capacity of queue or a negative error
  159. * (ie. ENOSPC). Note that it only really makes sense to treat all
  160. * positive return values as "available": indirect buffers mean that
  161. * we can put an entire sg[] array inside a single queue entry.
  162. */
  163. int virtqueue_add_buf(struct virtqueue *_vq,
  164. struct scatterlist sg[],
  165. unsigned int out,
  166. unsigned int in,
  167. void *data,
  168. gfp_t gfp)
  169. {
  170. struct vring_virtqueue *vq = to_vvq(_vq);
  171. unsigned int i, avail, uninitialized_var(prev);
  172. int head;
  173. START_USE(vq);
  174. BUG_ON(data == NULL);
  175. /* If the host supports indirect descriptor tables, and we have multiple
  176. * buffers, then go indirect. FIXME: tune this threshold */
  177. if (vq->indirect && (out + in) > 1 && vq->num_free) {
  178. head = vring_add_indirect(vq, sg, out, in, gfp);
  179. if (likely(head >= 0))
  180. goto add_head;
  181. }
  182. BUG_ON(out + in > vq->vring.num);
  183. BUG_ON(out + in == 0);
  184. if (vq->num_free < out + in) {
  185. pr_debug("Can't add buf len %i - avail = %i\n",
  186. out + in, vq->num_free);
  187. /* FIXME: for historical reasons, we force a notify here if
  188. * there are outgoing parts to the buffer. Presumably the
  189. * host should service the ring ASAP. */
  190. if (out)
  191. vq->notify(&vq->vq);
  192. END_USE(vq);
  193. return -ENOSPC;
  194. }
  195. /* We're about to use some buffers from the free list. */
  196. vq->num_free -= out + in;
  197. head = vq->free_head;
  198. for (i = vq->free_head; out; i = vq->vring.desc[i].next, out--) {
  199. vq->vring.desc[i].flags = VRING_DESC_F_NEXT;
  200. vq->vring.desc[i].addr = sg_phys(sg);
  201. vq->vring.desc[i].len = sg->length;
  202. prev = i;
  203. sg++;
  204. }
  205. for (; in; i = vq->vring.desc[i].next, in--) {
  206. vq->vring.desc[i].flags = VRING_DESC_F_NEXT|VRING_DESC_F_WRITE;
  207. vq->vring.desc[i].addr = sg_phys(sg);
  208. vq->vring.desc[i].len = sg->length;
  209. prev = i;
  210. sg++;
  211. }
  212. /* Last one doesn't continue. */
  213. vq->vring.desc[prev].flags &= ~VRING_DESC_F_NEXT;
  214. /* Update free pointer */
  215. vq->free_head = i;
  216. add_head:
  217. /* Set token. */
  218. vq->data[head] = data;
  219. /* Put entry in available array (but don't update avail->idx until they
  220. * do sync). */
  221. avail = (vq->vring.avail->idx & (vq->vring.num-1));
  222. vq->vring.avail->ring[avail] = head;
  223. /* Descriptors and available array need to be set before we expose the
  224. * new available array entries. */
  225. virtio_wmb(vq);
  226. vq->vring.avail->idx++;
  227. vq->num_added++;
  228. /* This is very unlikely, but theoretically possible. Kick
  229. * just in case. */
  230. if (unlikely(vq->num_added == (1 << 16) - 1))
  231. virtqueue_kick(_vq);
  232. pr_debug("Added buffer head %i to %p\n", head, vq);
  233. END_USE(vq);
  234. return vq->num_free;
  235. }
  236. EXPORT_SYMBOL_GPL(virtqueue_add_buf);
  237. /**
  238. * virtqueue_kick_prepare - first half of split virtqueue_kick call.
  239. * @vq: the struct virtqueue
  240. *
  241. * Instead of virtqueue_kick(), you can do:
  242. * if (virtqueue_kick_prepare(vq))
  243. * virtqueue_notify(vq);
  244. *
  245. * This is sometimes useful because the virtqueue_kick_prepare() needs
  246. * to be serialized, but the actual virtqueue_notify() call does not.
  247. */
  248. bool virtqueue_kick_prepare(struct virtqueue *_vq)
  249. {
  250. struct vring_virtqueue *vq = to_vvq(_vq);
  251. u16 new, old;
  252. bool needs_kick;
  253. START_USE(vq);
  254. /* Descriptors and available array need to be set before we expose the
  255. * new available array entries. */
  256. virtio_wmb(vq);
  257. old = vq->vring.avail->idx - vq->num_added;
  258. new = vq->vring.avail->idx;
  259. vq->num_added = 0;
  260. if (vq->event) {
  261. needs_kick = vring_need_event(vring_avail_event(&vq->vring),
  262. new, old);
  263. } else {
  264. needs_kick = !(vq->vring.used->flags & VRING_USED_F_NO_NOTIFY);
  265. }
  266. END_USE(vq);
  267. return needs_kick;
  268. }
  269. EXPORT_SYMBOL_GPL(virtqueue_kick_prepare);
  270. /**
  271. * virtqueue_notify - second half of split virtqueue_kick call.
  272. * @vq: the struct virtqueue
  273. *
  274. * This does not need to be serialized.
  275. */
  276. void virtqueue_notify(struct virtqueue *_vq)
  277. {
  278. struct vring_virtqueue *vq = to_vvq(_vq);
  279. /* Prod other side to tell it about changes. */
  280. vq->notify(_vq);
  281. }
  282. EXPORT_SYMBOL_GPL(virtqueue_notify);
  283. /**
  284. * virtqueue_kick - update after add_buf
  285. * @vq: the struct virtqueue
  286. *
  287. * After one or more virtqueue_add_buf calls, invoke this to kick
  288. * the other side.
  289. *
  290. * Caller must ensure we don't call this with other virtqueue
  291. * operations at the same time (except where noted).
  292. */
  293. void virtqueue_kick(struct virtqueue *vq)
  294. {
  295. if (virtqueue_kick_prepare(vq))
  296. virtqueue_notify(vq);
  297. }
  298. EXPORT_SYMBOL_GPL(virtqueue_kick);
  299. static void detach_buf(struct vring_virtqueue *vq, unsigned int head)
  300. {
  301. unsigned int i;
  302. /* Clear data ptr. */
  303. vq->data[head] = NULL;
  304. /* Put back on free list: find end */
  305. i = head;
  306. /* Free the indirect table */
  307. if (vq->vring.desc[i].flags & VRING_DESC_F_INDIRECT)
  308. kfree(phys_to_virt(vq->vring.desc[i].addr));
  309. while (vq->vring.desc[i].flags & VRING_DESC_F_NEXT) {
  310. i = vq->vring.desc[i].next;
  311. vq->num_free++;
  312. }
  313. vq->vring.desc[i].next = vq->free_head;
  314. vq->free_head = head;
  315. /* Plus final descriptor */
  316. vq->num_free++;
  317. }
  318. static inline bool more_used(const struct vring_virtqueue *vq)
  319. {
  320. return vq->last_used_idx != vq->vring.used->idx;
  321. }
  322. /**
  323. * virtqueue_get_buf - get the next used buffer
  324. * @vq: the struct virtqueue we're talking about.
  325. * @len: the length written into the buffer
  326. *
  327. * If the driver wrote data into the buffer, @len will be set to the
  328. * amount written. This means you don't need to clear the buffer
  329. * beforehand to ensure there's no data leakage in the case of short
  330. * writes.
  331. *
  332. * Caller must ensure we don't call this with other virtqueue
  333. * operations at the same time (except where noted).
  334. *
  335. * Returns NULL if there are no used buffers, or the "data" token
  336. * handed to virtqueue_add_buf().
  337. */
  338. void *virtqueue_get_buf(struct virtqueue *_vq, unsigned int *len)
  339. {
  340. struct vring_virtqueue *vq = to_vvq(_vq);
  341. void *ret;
  342. unsigned int i;
  343. u16 last_used;
  344. START_USE(vq);
  345. if (unlikely(vq->broken)) {
  346. END_USE(vq);
  347. return NULL;
  348. }
  349. if (!more_used(vq)) {
  350. pr_debug("No more buffers in queue\n");
  351. END_USE(vq);
  352. return NULL;
  353. }
  354. /* Only get used array entries after they have been exposed by host. */
  355. virtio_rmb(vq);
  356. last_used = (vq->last_used_idx & (vq->vring.num - 1));
  357. i = vq->vring.used->ring[last_used].id;
  358. *len = vq->vring.used->ring[last_used].len;
  359. if (unlikely(i >= vq->vring.num)) {
  360. BAD_RING(vq, "id %u out of range\n", i);
  361. return NULL;
  362. }
  363. if (unlikely(!vq->data[i])) {
  364. BAD_RING(vq, "id %u is not a head!\n", i);
  365. return NULL;
  366. }
  367. /* detach_buf clears data, so grab it now. */
  368. ret = vq->data[i];
  369. detach_buf(vq, i);
  370. vq->last_used_idx++;
  371. /* If we expect an interrupt for the next entry, tell host
  372. * by writing event index and flush out the write before
  373. * the read in the next get_buf call. */
  374. if (!(vq->vring.avail->flags & VRING_AVAIL_F_NO_INTERRUPT)) {
  375. vring_used_event(&vq->vring) = vq->last_used_idx;
  376. virtio_mb(vq);
  377. }
  378. END_USE(vq);
  379. return ret;
  380. }
  381. EXPORT_SYMBOL_GPL(virtqueue_get_buf);
  382. /**
  383. * virtqueue_disable_cb - disable callbacks
  384. * @vq: the struct virtqueue we're talking about.
  385. *
  386. * Note that this is not necessarily synchronous, hence unreliable and only
  387. * useful as an optimization.
  388. *
  389. * Unlike other operations, this need not be serialized.
  390. */
  391. void virtqueue_disable_cb(struct virtqueue *_vq)
  392. {
  393. struct vring_virtqueue *vq = to_vvq(_vq);
  394. vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
  395. }
  396. EXPORT_SYMBOL_GPL(virtqueue_disable_cb);
  397. /**
  398. * virtqueue_enable_cb - restart callbacks after disable_cb.
  399. * @vq: the struct virtqueue we're talking about.
  400. *
  401. * This re-enables callbacks; it returns "false" if there are pending
  402. * buffers in the queue, to detect a possible race between the driver
  403. * checking for more work, and enabling callbacks.
  404. *
  405. * Caller must ensure we don't call this with other virtqueue
  406. * operations at the same time (except where noted).
  407. */
  408. bool virtqueue_enable_cb(struct virtqueue *_vq)
  409. {
  410. struct vring_virtqueue *vq = to_vvq(_vq);
  411. START_USE(vq);
  412. /* We optimistically turn back on interrupts, then check if there was
  413. * more to do. */
  414. /* Depending on the VIRTIO_RING_F_EVENT_IDX feature, we need to
  415. * either clear the flags bit or point the event index at the next
  416. * entry. Always do both to keep code simple. */
  417. vq->vring.avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT;
  418. vring_used_event(&vq->vring) = vq->last_used_idx;
  419. virtio_mb(vq);
  420. if (unlikely(more_used(vq))) {
  421. END_USE(vq);
  422. return false;
  423. }
  424. END_USE(vq);
  425. return true;
  426. }
  427. EXPORT_SYMBOL_GPL(virtqueue_enable_cb);
  428. /**
  429. * virtqueue_enable_cb_delayed - restart callbacks after disable_cb.
  430. * @vq: the struct virtqueue we're talking about.
  431. *
  432. * This re-enables callbacks but hints to the other side to delay
  433. * interrupts until most of the available buffers have been processed;
  434. * it returns "false" if there are many pending buffers in the queue,
  435. * to detect a possible race between the driver checking for more work,
  436. * and enabling callbacks.
  437. *
  438. * Caller must ensure we don't call this with other virtqueue
  439. * operations at the same time (except where noted).
  440. */
  441. bool virtqueue_enable_cb_delayed(struct virtqueue *_vq)
  442. {
  443. struct vring_virtqueue *vq = to_vvq(_vq);
  444. u16 bufs;
  445. START_USE(vq);
  446. /* We optimistically turn back on interrupts, then check if there was
  447. * more to do. */
  448. /* Depending on the VIRTIO_RING_F_USED_EVENT_IDX feature, we need to
  449. * either clear the flags bit or point the event index at the next
  450. * entry. Always do both to keep code simple. */
  451. vq->vring.avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT;
  452. /* TODO: tune this threshold */
  453. bufs = (u16)(vq->vring.avail->idx - vq->last_used_idx) * 3 / 4;
  454. vring_used_event(&vq->vring) = vq->last_used_idx + bufs;
  455. virtio_mb(vq);
  456. if (unlikely((u16)(vq->vring.used->idx - vq->last_used_idx) > bufs)) {
  457. END_USE(vq);
  458. return false;
  459. }
  460. END_USE(vq);
  461. return true;
  462. }
  463. EXPORT_SYMBOL_GPL(virtqueue_enable_cb_delayed);
  464. /**
  465. * virtqueue_detach_unused_buf - detach first unused buffer
  466. * @vq: the struct virtqueue we're talking about.
  467. *
  468. * Returns NULL or the "data" token handed to virtqueue_add_buf().
  469. * This is not valid on an active queue; it is useful only for device
  470. * shutdown.
  471. */
  472. void *virtqueue_detach_unused_buf(struct virtqueue *_vq)
  473. {
  474. struct vring_virtqueue *vq = to_vvq(_vq);
  475. unsigned int i;
  476. void *buf;
  477. START_USE(vq);
  478. for (i = 0; i < vq->vring.num; i++) {
  479. if (!vq->data[i])
  480. continue;
  481. /* detach_buf clears data, so grab it now. */
  482. buf = vq->data[i];
  483. detach_buf(vq, i);
  484. vq->vring.avail->idx--;
  485. END_USE(vq);
  486. return buf;
  487. }
  488. /* That should have freed everything. */
  489. BUG_ON(vq->num_free != vq->vring.num);
  490. END_USE(vq);
  491. return NULL;
  492. }
  493. EXPORT_SYMBOL_GPL(virtqueue_detach_unused_buf);
  494. irqreturn_t vring_interrupt(int irq, void *_vq)
  495. {
  496. struct vring_virtqueue *vq = to_vvq(_vq);
  497. if (!more_used(vq)) {
  498. pr_debug("virtqueue interrupt with no work for %p\n", vq);
  499. return IRQ_NONE;
  500. }
  501. if (unlikely(vq->broken))
  502. return IRQ_HANDLED;
  503. pr_debug("virtqueue callback for %p (%p)\n", vq, vq->vq.callback);
  504. if (vq->vq.callback)
  505. vq->vq.callback(&vq->vq);
  506. return IRQ_HANDLED;
  507. }
  508. EXPORT_SYMBOL_GPL(vring_interrupt);
  509. struct virtqueue *vring_new_virtqueue(unsigned int num,
  510. unsigned int vring_align,
  511. struct virtio_device *vdev,
  512. bool weak_barriers,
  513. void *pages,
  514. void (*notify)(struct virtqueue *),
  515. void (*callback)(struct virtqueue *),
  516. const char *name)
  517. {
  518. struct vring_virtqueue *vq;
  519. unsigned int i;
  520. /* We assume num is a power of 2. */
  521. if (num & (num - 1)) {
  522. dev_warn(&vdev->dev, "Bad virtqueue length %u\n", num);
  523. return NULL;
  524. }
  525. vq = kmalloc(sizeof(*vq) + sizeof(void *)*num, GFP_KERNEL);
  526. if (!vq)
  527. return NULL;
  528. vring_init(&vq->vring, num, pages, vring_align);
  529. vq->vq.callback = callback;
  530. vq->vq.vdev = vdev;
  531. vq->vq.name = name;
  532. vq->notify = notify;
  533. vq->weak_barriers = weak_barriers;
  534. vq->broken = false;
  535. vq->last_used_idx = 0;
  536. vq->num_added = 0;
  537. list_add_tail(&vq->vq.list, &vdev->vqs);
  538. #ifdef DEBUG
  539. vq->in_use = false;
  540. #endif
  541. vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC);
  542. vq->event = virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX);
  543. /* No callback? Tell other side not to bother us. */
  544. if (!callback)
  545. vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
  546. /* Put everything in free lists. */
  547. vq->num_free = num;
  548. vq->free_head = 0;
  549. for (i = 0; i < num-1; i++) {
  550. vq->vring.desc[i].next = i+1;
  551. vq->data[i] = NULL;
  552. }
  553. vq->data[i] = NULL;
  554. return &vq->vq;
  555. }
  556. EXPORT_SYMBOL_GPL(vring_new_virtqueue);
  557. void vring_del_virtqueue(struct virtqueue *vq)
  558. {
  559. list_del(&vq->list);
  560. kfree(to_vvq(vq));
  561. }
  562. EXPORT_SYMBOL_GPL(vring_del_virtqueue);
  563. /* Manipulates transport-specific feature bits. */
  564. void vring_transport_features(struct virtio_device *vdev)
  565. {
  566. unsigned int i;
  567. for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++) {
  568. switch (i) {
  569. case VIRTIO_RING_F_INDIRECT_DESC:
  570. break;
  571. case VIRTIO_RING_F_EVENT_IDX:
  572. break;
  573. default:
  574. /* We don't understand this bit. */
  575. clear_bit(i, vdev->features);
  576. }
  577. }
  578. }
  579. EXPORT_SYMBOL_GPL(vring_transport_features);
  580. /**
  581. * virtqueue_get_vring_size - return the size of the virtqueue's vring
  582. * @vq: the struct virtqueue containing the vring of interest.
  583. *
  584. * Returns the size of the vring. This is mainly used for boasting to
  585. * userspace. Unlike other operations, this need not be serialized.
  586. */
  587. unsigned int virtqueue_get_vring_size(struct virtqueue *_vq)
  588. {
  589. struct vring_virtqueue *vq = to_vvq(_vq);
  590. return vq->vring.num;
  591. }
  592. EXPORT_SYMBOL_GPL(virtqueue_get_vring_size);
  593. MODULE_LICENSE("GPL");