virtio_ring.c 18 KB

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