virtio_ring.c 19 KB

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