virtio_ring.c 22 KB

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