virtio_ring.c 19 KB

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