virtio_ring.c 20 KB

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