virtio_ring.c 19 KB

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