virtio_ring.c 23 KB

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