virtio_ring.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  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->num_added++) & (vq->vring.num-1));
  222. vq->vring.avail->ring[avail] = head;
  223. pr_debug("Added buffer head %i to %p\n", head, vq);
  224. END_USE(vq);
  225. return vq->num_free;
  226. }
  227. EXPORT_SYMBOL_GPL(virtqueue_add_buf);
  228. /**
  229. * virtqueue_kick_prepare - first half of split virtqueue_kick call.
  230. * @vq: the struct virtqueue
  231. *
  232. * Instead of virtqueue_kick(), you can do:
  233. * if (virtqueue_kick_prepare(vq))
  234. * virtqueue_notify(vq);
  235. *
  236. * This is sometimes useful because the virtqueue_kick_prepare() needs
  237. * to be serialized, but the actual virtqueue_notify() call does not.
  238. */
  239. bool virtqueue_kick_prepare(struct virtqueue *_vq)
  240. {
  241. struct vring_virtqueue *vq = to_vvq(_vq);
  242. u16 new, old;
  243. bool needs_kick;
  244. START_USE(vq);
  245. /* Descriptors and available array need to be set before we expose the
  246. * new available array entries. */
  247. virtio_wmb(vq);
  248. old = vq->vring.avail->idx;
  249. new = vq->vring.avail->idx = old + vq->num_added;
  250. vq->num_added = 0;
  251. /* Need to update avail index before checking if we should notify */
  252. virtio_mb(vq);
  253. if (vq->event) {
  254. needs_kick = vring_need_event(vring_avail_event(&vq->vring),
  255. new, old);
  256. } else {
  257. needs_kick = !(vq->vring.used->flags & VRING_USED_F_NO_NOTIFY);
  258. }
  259. END_USE(vq);
  260. return needs_kick;
  261. }
  262. EXPORT_SYMBOL_GPL(virtqueue_kick_prepare);
  263. /**
  264. * virtqueue_notify - second half of split virtqueue_kick call.
  265. * @vq: the struct virtqueue
  266. *
  267. * This does not need to be serialized.
  268. */
  269. void virtqueue_notify(struct virtqueue *_vq)
  270. {
  271. struct vring_virtqueue *vq = to_vvq(_vq);
  272. /* Prod other side to tell it about changes. */
  273. vq->notify(_vq);
  274. }
  275. EXPORT_SYMBOL_GPL(virtqueue_notify);
  276. /**
  277. * virtqueue_kick - update after add_buf
  278. * @vq: the struct virtqueue
  279. *
  280. * After one or more virtqueue_add_buf calls, invoke this to kick
  281. * the other side.
  282. *
  283. * Caller must ensure we don't call this with other virtqueue
  284. * operations at the same time (except where noted).
  285. */
  286. void virtqueue_kick(struct virtqueue *vq)
  287. {
  288. if (virtqueue_kick_prepare(vq))
  289. virtqueue_notify(vq);
  290. }
  291. EXPORT_SYMBOL_GPL(virtqueue_kick);
  292. static void detach_buf(struct vring_virtqueue *vq, unsigned int head)
  293. {
  294. unsigned int i;
  295. /* Clear data ptr. */
  296. vq->data[head] = NULL;
  297. /* Put back on free list: find end */
  298. i = head;
  299. /* Free the indirect table */
  300. if (vq->vring.desc[i].flags & VRING_DESC_F_INDIRECT)
  301. kfree(phys_to_virt(vq->vring.desc[i].addr));
  302. while (vq->vring.desc[i].flags & VRING_DESC_F_NEXT) {
  303. i = vq->vring.desc[i].next;
  304. vq->num_free++;
  305. }
  306. vq->vring.desc[i].next = vq->free_head;
  307. vq->free_head = head;
  308. /* Plus final descriptor */
  309. vq->num_free++;
  310. }
  311. static inline bool more_used(const struct vring_virtqueue *vq)
  312. {
  313. return vq->last_used_idx != vq->vring.used->idx;
  314. }
  315. /**
  316. * virtqueue_get_buf - get the next used buffer
  317. * @vq: the struct virtqueue we're talking about.
  318. * @len: the length written into the buffer
  319. *
  320. * If the driver wrote data into the buffer, @len will be set to the
  321. * amount written. This means you don't need to clear the buffer
  322. * beforehand to ensure there's no data leakage in the case of short
  323. * writes.
  324. *
  325. * Caller must ensure we don't call this with other virtqueue
  326. * operations at the same time (except where noted).
  327. *
  328. * Returns NULL if there are no used buffers, or the "data" token
  329. * handed to virtqueue_add_buf().
  330. */
  331. void *virtqueue_get_buf(struct virtqueue *_vq, unsigned int *len)
  332. {
  333. struct vring_virtqueue *vq = to_vvq(_vq);
  334. void *ret;
  335. unsigned int i;
  336. u16 last_used;
  337. START_USE(vq);
  338. if (unlikely(vq->broken)) {
  339. END_USE(vq);
  340. return NULL;
  341. }
  342. if (!more_used(vq)) {
  343. pr_debug("No more buffers in queue\n");
  344. END_USE(vq);
  345. return NULL;
  346. }
  347. /* Only get used array entries after they have been exposed by host. */
  348. virtio_rmb(vq);
  349. last_used = (vq->last_used_idx & (vq->vring.num - 1));
  350. i = vq->vring.used->ring[last_used].id;
  351. *len = vq->vring.used->ring[last_used].len;
  352. if (unlikely(i >= vq->vring.num)) {
  353. BAD_RING(vq, "id %u out of range\n", i);
  354. return NULL;
  355. }
  356. if (unlikely(!vq->data[i])) {
  357. BAD_RING(vq, "id %u is not a head!\n", i);
  358. return NULL;
  359. }
  360. /* detach_buf clears data, so grab it now. */
  361. ret = vq->data[i];
  362. detach_buf(vq, i);
  363. vq->last_used_idx++;
  364. /* If we expect an interrupt for the next entry, tell host
  365. * by writing event index and flush out the write before
  366. * the read in the next get_buf call. */
  367. if (!(vq->vring.avail->flags & VRING_AVAIL_F_NO_INTERRUPT)) {
  368. vring_used_event(&vq->vring) = vq->last_used_idx;
  369. virtio_mb(vq);
  370. }
  371. END_USE(vq);
  372. return ret;
  373. }
  374. EXPORT_SYMBOL_GPL(virtqueue_get_buf);
  375. /**
  376. * virtqueue_disable_cb - disable callbacks
  377. * @vq: the struct virtqueue we're talking about.
  378. *
  379. * Note that this is not necessarily synchronous, hence unreliable and only
  380. * useful as an optimization.
  381. *
  382. * Unlike other operations, this need not be serialized.
  383. */
  384. void virtqueue_disable_cb(struct virtqueue *_vq)
  385. {
  386. struct vring_virtqueue *vq = to_vvq(_vq);
  387. vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
  388. }
  389. EXPORT_SYMBOL_GPL(virtqueue_disable_cb);
  390. /**
  391. * virtqueue_enable_cb - restart callbacks after disable_cb.
  392. * @vq: the struct virtqueue we're talking about.
  393. *
  394. * This re-enables callbacks; it returns "false" if there are pending
  395. * buffers in the queue, to detect a possible race between the driver
  396. * checking for more work, and enabling callbacks.
  397. *
  398. * Caller must ensure we don't call this with other virtqueue
  399. * operations at the same time (except where noted).
  400. */
  401. bool virtqueue_enable_cb(struct virtqueue *_vq)
  402. {
  403. struct vring_virtqueue *vq = to_vvq(_vq);
  404. START_USE(vq);
  405. /* We optimistically turn back on interrupts, then check if there was
  406. * more to do. */
  407. /* Depending on the VIRTIO_RING_F_EVENT_IDX feature, we need to
  408. * either clear the flags bit or point the event index at the next
  409. * entry. Always do both to keep code simple. */
  410. vq->vring.avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT;
  411. vring_used_event(&vq->vring) = vq->last_used_idx;
  412. virtio_mb(vq);
  413. if (unlikely(more_used(vq))) {
  414. END_USE(vq);
  415. return false;
  416. }
  417. END_USE(vq);
  418. return true;
  419. }
  420. EXPORT_SYMBOL_GPL(virtqueue_enable_cb);
  421. /**
  422. * virtqueue_enable_cb_delayed - restart callbacks after disable_cb.
  423. * @vq: the struct virtqueue we're talking about.
  424. *
  425. * This re-enables callbacks but hints to the other side to delay
  426. * interrupts until most of the available buffers have been processed;
  427. * it returns "false" if there are many pending buffers in the queue,
  428. * to detect a possible race between the driver checking for more work,
  429. * and enabling callbacks.
  430. *
  431. * Caller must ensure we don't call this with other virtqueue
  432. * operations at the same time (except where noted).
  433. */
  434. bool virtqueue_enable_cb_delayed(struct virtqueue *_vq)
  435. {
  436. struct vring_virtqueue *vq = to_vvq(_vq);
  437. u16 bufs;
  438. START_USE(vq);
  439. /* We optimistically turn back on interrupts, then check if there was
  440. * more to do. */
  441. /* Depending on the VIRTIO_RING_F_USED_EVENT_IDX feature, we need to
  442. * either clear the flags bit or point the event index at the next
  443. * entry. Always do both to keep code simple. */
  444. vq->vring.avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT;
  445. /* TODO: tune this threshold */
  446. bufs = (u16)(vq->vring.avail->idx - vq->last_used_idx) * 3 / 4;
  447. vring_used_event(&vq->vring) = vq->last_used_idx + bufs;
  448. virtio_mb(vq);
  449. if (unlikely((u16)(vq->vring.used->idx - vq->last_used_idx) > bufs)) {
  450. END_USE(vq);
  451. return false;
  452. }
  453. END_USE(vq);
  454. return true;
  455. }
  456. EXPORT_SYMBOL_GPL(virtqueue_enable_cb_delayed);
  457. /**
  458. * virtqueue_detach_unused_buf - detach first unused buffer
  459. * @vq: the struct virtqueue we're talking about.
  460. *
  461. * Returns NULL or the "data" token handed to virtqueue_add_buf().
  462. * This is not valid on an active queue; it is useful only for device
  463. * shutdown.
  464. */
  465. void *virtqueue_detach_unused_buf(struct virtqueue *_vq)
  466. {
  467. struct vring_virtqueue *vq = to_vvq(_vq);
  468. unsigned int i;
  469. void *buf;
  470. START_USE(vq);
  471. for (i = 0; i < vq->vring.num; i++) {
  472. if (!vq->data[i])
  473. continue;
  474. /* detach_buf clears data, so grab it now. */
  475. buf = vq->data[i];
  476. detach_buf(vq, i);
  477. vq->vring.avail->idx--;
  478. END_USE(vq);
  479. return buf;
  480. }
  481. /* That should have freed everything. */
  482. BUG_ON(vq->num_free != vq->vring.num);
  483. END_USE(vq);
  484. return NULL;
  485. }
  486. EXPORT_SYMBOL_GPL(virtqueue_detach_unused_buf);
  487. irqreturn_t vring_interrupt(int irq, void *_vq)
  488. {
  489. struct vring_virtqueue *vq = to_vvq(_vq);
  490. if (!more_used(vq)) {
  491. pr_debug("virtqueue interrupt with no work for %p\n", vq);
  492. return IRQ_NONE;
  493. }
  494. if (unlikely(vq->broken))
  495. return IRQ_HANDLED;
  496. pr_debug("virtqueue callback for %p (%p)\n", vq, vq->vq.callback);
  497. if (vq->vq.callback)
  498. vq->vq.callback(&vq->vq);
  499. return IRQ_HANDLED;
  500. }
  501. EXPORT_SYMBOL_GPL(vring_interrupt);
  502. struct virtqueue *vring_new_virtqueue(unsigned int num,
  503. unsigned int vring_align,
  504. struct virtio_device *vdev,
  505. bool weak_barriers,
  506. void *pages,
  507. void (*notify)(struct virtqueue *),
  508. void (*callback)(struct virtqueue *),
  509. const char *name)
  510. {
  511. struct vring_virtqueue *vq;
  512. unsigned int i;
  513. /* We assume num is a power of 2. */
  514. if (num & (num - 1)) {
  515. dev_warn(&vdev->dev, "Bad virtqueue length %u\n", num);
  516. return NULL;
  517. }
  518. vq = kmalloc(sizeof(*vq) + sizeof(void *)*num, GFP_KERNEL);
  519. if (!vq)
  520. return NULL;
  521. vring_init(&vq->vring, num, pages, vring_align);
  522. vq->vq.callback = callback;
  523. vq->vq.vdev = vdev;
  524. vq->vq.name = name;
  525. vq->notify = notify;
  526. vq->weak_barriers = weak_barriers;
  527. vq->broken = false;
  528. vq->last_used_idx = 0;
  529. vq->num_added = 0;
  530. list_add_tail(&vq->vq.list, &vdev->vqs);
  531. #ifdef DEBUG
  532. vq->in_use = false;
  533. #endif
  534. vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC);
  535. vq->event = virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX);
  536. /* No callback? Tell other side not to bother us. */
  537. if (!callback)
  538. vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
  539. /* Put everything in free lists. */
  540. vq->num_free = num;
  541. vq->free_head = 0;
  542. for (i = 0; i < num-1; i++) {
  543. vq->vring.desc[i].next = i+1;
  544. vq->data[i] = NULL;
  545. }
  546. vq->data[i] = NULL;
  547. return &vq->vq;
  548. }
  549. EXPORT_SYMBOL_GPL(vring_new_virtqueue);
  550. void vring_del_virtqueue(struct virtqueue *vq)
  551. {
  552. list_del(&vq->list);
  553. kfree(to_vvq(vq));
  554. }
  555. EXPORT_SYMBOL_GPL(vring_del_virtqueue);
  556. /* Manipulates transport-specific feature bits. */
  557. void vring_transport_features(struct virtio_device *vdev)
  558. {
  559. unsigned int i;
  560. for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++) {
  561. switch (i) {
  562. case VIRTIO_RING_F_INDIRECT_DESC:
  563. break;
  564. case VIRTIO_RING_F_EVENT_IDX:
  565. break;
  566. default:
  567. /* We don't understand this bit. */
  568. clear_bit(i, vdev->features);
  569. }
  570. }
  571. }
  572. EXPORT_SYMBOL_GPL(vring_transport_features);
  573. /**
  574. * virtqueue_get_vring_size - return the size of the virtqueue's vring
  575. * @vq: the struct virtqueue containing the vring of interest.
  576. *
  577. * Returns the size of the vring. This is mainly used for boasting to
  578. * userspace. Unlike other operations, this need not be serialized.
  579. */
  580. unsigned int virtqueue_get_vring_size(struct virtqueue *_vq)
  581. {
  582. struct vring_virtqueue *vq = to_vvq(_vq);
  583. return vq->vring.num;
  584. }
  585. EXPORT_SYMBOL_GPL(virtqueue_get_vring_size);
  586. MODULE_LICENSE("GPL");