virtio_ring.c 23 KB

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