virtio_ring.c 23 KB

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