virtio_ring.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839
  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_buf - expose buffer to other end
  256. * @vq: the struct virtqueue we're talking about.
  257. * @sg: the description of the buffer(s).
  258. * @out_num: the number of sg readable by other side
  259. * @in_num: the number of sg 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_buf(struct virtqueue *_vq,
  269. struct scatterlist sg[],
  270. unsigned int out,
  271. unsigned int in,
  272. void *data,
  273. gfp_t gfp)
  274. {
  275. struct scatterlist *sgs[2];
  276. sgs[0] = sg;
  277. sgs[1] = sg + out;
  278. return virtqueue_add(_vq, sgs, sg_next_arr,
  279. out, in, out ? 1 : 0, in ? 1 : 0, data, gfp);
  280. }
  281. EXPORT_SYMBOL_GPL(virtqueue_add_buf);
  282. /**
  283. * virtqueue_add_sgs - expose buffers to other end
  284. * @vq: the struct virtqueue we're talking about.
  285. * @sgs: array of terminated scatterlists.
  286. * @out_num: the number of scatterlists readable by other side
  287. * @in_num: the number of scatterlists which are writable (after readable ones)
  288. * @data: the token identifying the buffer.
  289. * @gfp: how to do memory allocations (if necessary).
  290. *
  291. * Caller must ensure we don't call this with other virtqueue operations
  292. * at the same time (except where noted).
  293. *
  294. * Returns zero or a negative error (ie. ENOSPC, ENOMEM).
  295. */
  296. int virtqueue_add_sgs(struct virtqueue *_vq,
  297. struct scatterlist *sgs[],
  298. unsigned int out_sgs,
  299. unsigned int in_sgs,
  300. void *data,
  301. gfp_t gfp)
  302. {
  303. unsigned int i, total_out, total_in;
  304. /* Count them first. */
  305. for (i = total_out = total_in = 0; i < out_sgs; i++) {
  306. struct scatterlist *sg;
  307. for (sg = sgs[i]; sg; sg = sg_next(sg))
  308. total_out++;
  309. }
  310. for (; i < out_sgs + in_sgs; i++) {
  311. struct scatterlist *sg;
  312. for (sg = sgs[i]; sg; sg = sg_next(sg))
  313. total_in++;
  314. }
  315. return virtqueue_add(_vq, sgs, sg_next_chained,
  316. total_out, total_in, out_sgs, in_sgs, data, gfp);
  317. }
  318. EXPORT_SYMBOL_GPL(virtqueue_add_sgs);
  319. /**
  320. * virtqueue_add_outbuf - expose output 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 readable 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).
  331. */
  332. int virtqueue_add_outbuf(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, num, 0, 1, 0, data, gfp);
  338. }
  339. EXPORT_SYMBOL_GPL(virtqueue_add_outbuf);
  340. /**
  341. * virtqueue_add_inbuf - expose input buffers to other end
  342. * @vq: the struct virtqueue we're talking about.
  343. * @sgs: array of scatterlists (need not be terminated!)
  344. * @num: the number of scatterlists writable by other side
  345. * @data: the token identifying the buffer.
  346. * @gfp: how to do memory allocations (if necessary).
  347. *
  348. * Caller must ensure we don't call this with other virtqueue operations
  349. * at the same time (except where noted).
  350. *
  351. * Returns zero or a negative error (ie. ENOSPC, ENOMEM).
  352. */
  353. int virtqueue_add_inbuf(struct virtqueue *vq,
  354. struct scatterlist sg[], unsigned int num,
  355. void *data,
  356. gfp_t gfp)
  357. {
  358. return virtqueue_add(vq, &sg, sg_next_arr, 0, num, 0, 1, data, gfp);
  359. }
  360. EXPORT_SYMBOL_GPL(virtqueue_add_inbuf);
  361. /**
  362. * virtqueue_kick_prepare - first half of split virtqueue_kick call.
  363. * @vq: the struct virtqueue
  364. *
  365. * Instead of virtqueue_kick(), you can do:
  366. * if (virtqueue_kick_prepare(vq))
  367. * virtqueue_notify(vq);
  368. *
  369. * This is sometimes useful because the virtqueue_kick_prepare() needs
  370. * to be serialized, but the actual virtqueue_notify() call does not.
  371. */
  372. bool virtqueue_kick_prepare(struct virtqueue *_vq)
  373. {
  374. struct vring_virtqueue *vq = to_vvq(_vq);
  375. u16 new, old;
  376. bool needs_kick;
  377. START_USE(vq);
  378. /* We need to expose available array entries before checking avail
  379. * event. */
  380. virtio_mb(vq->weak_barriers);
  381. old = vq->vring.avail->idx - vq->num_added;
  382. new = vq->vring.avail->idx;
  383. vq->num_added = 0;
  384. #ifdef DEBUG
  385. if (vq->last_add_time_valid) {
  386. WARN_ON(ktime_to_ms(ktime_sub(ktime_get(),
  387. vq->last_add_time)) > 100);
  388. }
  389. vq->last_add_time_valid = false;
  390. #endif
  391. if (vq->event) {
  392. needs_kick = vring_need_event(vring_avail_event(&vq->vring),
  393. new, old);
  394. } else {
  395. needs_kick = !(vq->vring.used->flags & VRING_USED_F_NO_NOTIFY);
  396. }
  397. END_USE(vq);
  398. return needs_kick;
  399. }
  400. EXPORT_SYMBOL_GPL(virtqueue_kick_prepare);
  401. /**
  402. * virtqueue_notify - second half of split virtqueue_kick call.
  403. * @vq: the struct virtqueue
  404. *
  405. * This does not need to be serialized.
  406. */
  407. void virtqueue_notify(struct virtqueue *_vq)
  408. {
  409. struct vring_virtqueue *vq = to_vvq(_vq);
  410. /* Prod other side to tell it about changes. */
  411. vq->notify(_vq);
  412. }
  413. EXPORT_SYMBOL_GPL(virtqueue_notify);
  414. /**
  415. * virtqueue_kick - update after add_buf
  416. * @vq: the struct virtqueue
  417. *
  418. * After one or more virtqueue_add_buf calls, invoke this to kick
  419. * the other side.
  420. *
  421. * Caller must ensure we don't call this with other virtqueue
  422. * operations at the same time (except where noted).
  423. */
  424. void virtqueue_kick(struct virtqueue *vq)
  425. {
  426. if (virtqueue_kick_prepare(vq))
  427. virtqueue_notify(vq);
  428. }
  429. EXPORT_SYMBOL_GPL(virtqueue_kick);
  430. static void detach_buf(struct vring_virtqueue *vq, unsigned int head)
  431. {
  432. unsigned int i;
  433. /* Clear data ptr. */
  434. vq->data[head] = NULL;
  435. /* Put back on free list: find end */
  436. i = head;
  437. /* Free the indirect table */
  438. if (vq->vring.desc[i].flags & VRING_DESC_F_INDIRECT)
  439. kfree(phys_to_virt(vq->vring.desc[i].addr));
  440. while (vq->vring.desc[i].flags & VRING_DESC_F_NEXT) {
  441. i = vq->vring.desc[i].next;
  442. vq->vq.num_free++;
  443. }
  444. vq->vring.desc[i].next = vq->free_head;
  445. vq->free_head = head;
  446. /* Plus final descriptor */
  447. vq->vq.num_free++;
  448. }
  449. static inline bool more_used(const struct vring_virtqueue *vq)
  450. {
  451. return vq->last_used_idx != vq->vring.used->idx;
  452. }
  453. /**
  454. * virtqueue_get_buf - get the next used buffer
  455. * @vq: the struct virtqueue we're talking about.
  456. * @len: the length written into the buffer
  457. *
  458. * If the driver wrote data into the buffer, @len will be set to the
  459. * amount written. This means you don't need to clear the buffer
  460. * beforehand to ensure there's no data leakage in the case of short
  461. * writes.
  462. *
  463. * Caller must ensure we don't call this with other virtqueue
  464. * operations at the same time (except where noted).
  465. *
  466. * Returns NULL if there are no used buffers, or the "data" token
  467. * handed to virtqueue_add_buf().
  468. */
  469. void *virtqueue_get_buf(struct virtqueue *_vq, unsigned int *len)
  470. {
  471. struct vring_virtqueue *vq = to_vvq(_vq);
  472. void *ret;
  473. unsigned int i;
  474. u16 last_used;
  475. START_USE(vq);
  476. if (unlikely(vq->broken)) {
  477. END_USE(vq);
  478. return NULL;
  479. }
  480. if (!more_used(vq)) {
  481. pr_debug("No more buffers in queue\n");
  482. END_USE(vq);
  483. return NULL;
  484. }
  485. /* Only get used array entries after they have been exposed by host. */
  486. virtio_rmb(vq->weak_barriers);
  487. last_used = (vq->last_used_idx & (vq->vring.num - 1));
  488. i = vq->vring.used->ring[last_used].id;
  489. *len = vq->vring.used->ring[last_used].len;
  490. if (unlikely(i >= vq->vring.num)) {
  491. BAD_RING(vq, "id %u out of range\n", i);
  492. return NULL;
  493. }
  494. if (unlikely(!vq->data[i])) {
  495. BAD_RING(vq, "id %u is not a head!\n", i);
  496. return NULL;
  497. }
  498. /* detach_buf clears data, so grab it now. */
  499. ret = vq->data[i];
  500. detach_buf(vq, i);
  501. vq->last_used_idx++;
  502. /* If we expect an interrupt for the next entry, tell host
  503. * by writing event index and flush out the write before
  504. * the read in the next get_buf call. */
  505. if (!(vq->vring.avail->flags & VRING_AVAIL_F_NO_INTERRUPT)) {
  506. vring_used_event(&vq->vring) = vq->last_used_idx;
  507. virtio_mb(vq->weak_barriers);
  508. }
  509. #ifdef DEBUG
  510. vq->last_add_time_valid = false;
  511. #endif
  512. END_USE(vq);
  513. return ret;
  514. }
  515. EXPORT_SYMBOL_GPL(virtqueue_get_buf);
  516. /**
  517. * virtqueue_disable_cb - disable callbacks
  518. * @vq: the struct virtqueue we're talking about.
  519. *
  520. * Note that this is not necessarily synchronous, hence unreliable and only
  521. * useful as an optimization.
  522. *
  523. * Unlike other operations, this need not be serialized.
  524. */
  525. void virtqueue_disable_cb(struct virtqueue *_vq)
  526. {
  527. struct vring_virtqueue *vq = to_vvq(_vq);
  528. vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
  529. }
  530. EXPORT_SYMBOL_GPL(virtqueue_disable_cb);
  531. /**
  532. * virtqueue_enable_cb - restart callbacks after disable_cb.
  533. * @vq: the struct virtqueue we're talking about.
  534. *
  535. * This re-enables callbacks; it returns "false" if there are pending
  536. * buffers in the queue, to detect a possible race between the driver
  537. * checking for more work, and enabling callbacks.
  538. *
  539. * Caller must ensure we don't call this with other virtqueue
  540. * operations at the same time (except where noted).
  541. */
  542. bool virtqueue_enable_cb(struct virtqueue *_vq)
  543. {
  544. struct vring_virtqueue *vq = to_vvq(_vq);
  545. START_USE(vq);
  546. /* We optimistically turn back on interrupts, then check if there was
  547. * more to do. */
  548. /* Depending on the VIRTIO_RING_F_EVENT_IDX feature, we need to
  549. * either clear the flags bit or point the event index at the next
  550. * entry. Always do both to keep code simple. */
  551. vq->vring.avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT;
  552. vring_used_event(&vq->vring) = vq->last_used_idx;
  553. virtio_mb(vq->weak_barriers);
  554. if (unlikely(more_used(vq))) {
  555. END_USE(vq);
  556. return false;
  557. }
  558. END_USE(vq);
  559. return true;
  560. }
  561. EXPORT_SYMBOL_GPL(virtqueue_enable_cb);
  562. /**
  563. * virtqueue_enable_cb_delayed - restart callbacks after disable_cb.
  564. * @vq: the struct virtqueue we're talking about.
  565. *
  566. * This re-enables callbacks but hints to the other side to delay
  567. * interrupts until most of the available buffers have been processed;
  568. * it returns "false" if there are many pending buffers in the queue,
  569. * to detect a possible race between the driver checking for more work,
  570. * and enabling callbacks.
  571. *
  572. * Caller must ensure we don't call this with other virtqueue
  573. * operations at the same time (except where noted).
  574. */
  575. bool virtqueue_enable_cb_delayed(struct virtqueue *_vq)
  576. {
  577. struct vring_virtqueue *vq = to_vvq(_vq);
  578. u16 bufs;
  579. START_USE(vq);
  580. /* We optimistically turn back on interrupts, then check if there was
  581. * more to do. */
  582. /* Depending on the VIRTIO_RING_F_USED_EVENT_IDX feature, we need to
  583. * either clear the flags bit or point the event index at the next
  584. * entry. Always do both to keep code simple. */
  585. vq->vring.avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT;
  586. /* TODO: tune this threshold */
  587. bufs = (u16)(vq->vring.avail->idx - vq->last_used_idx) * 3 / 4;
  588. vring_used_event(&vq->vring) = vq->last_used_idx + bufs;
  589. virtio_mb(vq->weak_barriers);
  590. if (unlikely((u16)(vq->vring.used->idx - vq->last_used_idx) > bufs)) {
  591. END_USE(vq);
  592. return false;
  593. }
  594. END_USE(vq);
  595. return true;
  596. }
  597. EXPORT_SYMBOL_GPL(virtqueue_enable_cb_delayed);
  598. /**
  599. * virtqueue_detach_unused_buf - detach first unused buffer
  600. * @vq: the struct virtqueue we're talking about.
  601. *
  602. * Returns NULL or the "data" token handed to virtqueue_add_buf().
  603. * This is not valid on an active queue; it is useful only for device
  604. * shutdown.
  605. */
  606. void *virtqueue_detach_unused_buf(struct virtqueue *_vq)
  607. {
  608. struct vring_virtqueue *vq = to_vvq(_vq);
  609. unsigned int i;
  610. void *buf;
  611. START_USE(vq);
  612. for (i = 0; i < vq->vring.num; i++) {
  613. if (!vq->data[i])
  614. continue;
  615. /* detach_buf clears data, so grab it now. */
  616. buf = vq->data[i];
  617. detach_buf(vq, i);
  618. vq->vring.avail->idx--;
  619. END_USE(vq);
  620. return buf;
  621. }
  622. /* That should have freed everything. */
  623. BUG_ON(vq->vq.num_free != vq->vring.num);
  624. END_USE(vq);
  625. return NULL;
  626. }
  627. EXPORT_SYMBOL_GPL(virtqueue_detach_unused_buf);
  628. irqreturn_t vring_interrupt(int irq, void *_vq)
  629. {
  630. struct vring_virtqueue *vq = to_vvq(_vq);
  631. if (!more_used(vq)) {
  632. pr_debug("virtqueue interrupt with no work for %p\n", vq);
  633. return IRQ_NONE;
  634. }
  635. if (unlikely(vq->broken))
  636. return IRQ_HANDLED;
  637. pr_debug("virtqueue callback for %p (%p)\n", vq, vq->vq.callback);
  638. if (vq->vq.callback)
  639. vq->vq.callback(&vq->vq);
  640. return IRQ_HANDLED;
  641. }
  642. EXPORT_SYMBOL_GPL(vring_interrupt);
  643. struct virtqueue *vring_new_virtqueue(unsigned int index,
  644. unsigned int num,
  645. unsigned int vring_align,
  646. struct virtio_device *vdev,
  647. bool weak_barriers,
  648. void *pages,
  649. void (*notify)(struct virtqueue *),
  650. void (*callback)(struct virtqueue *),
  651. const char *name)
  652. {
  653. struct vring_virtqueue *vq;
  654. unsigned int i;
  655. /* We assume num is a power of 2. */
  656. if (num & (num - 1)) {
  657. dev_warn(&vdev->dev, "Bad virtqueue length %u\n", num);
  658. return NULL;
  659. }
  660. vq = kmalloc(sizeof(*vq) + sizeof(void *)*num, GFP_KERNEL);
  661. if (!vq)
  662. return NULL;
  663. vring_init(&vq->vring, num, pages, vring_align);
  664. vq->vq.callback = callback;
  665. vq->vq.vdev = vdev;
  666. vq->vq.name = name;
  667. vq->vq.num_free = num;
  668. vq->vq.index = index;
  669. vq->notify = notify;
  670. vq->weak_barriers = weak_barriers;
  671. vq->broken = false;
  672. vq->last_used_idx = 0;
  673. vq->num_added = 0;
  674. list_add_tail(&vq->vq.list, &vdev->vqs);
  675. #ifdef DEBUG
  676. vq->in_use = false;
  677. vq->last_add_time_valid = false;
  678. #endif
  679. vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC);
  680. vq->event = virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX);
  681. /* No callback? Tell other side not to bother us. */
  682. if (!callback)
  683. vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
  684. /* Put everything in free lists. */
  685. vq->free_head = 0;
  686. for (i = 0; i < num-1; i++) {
  687. vq->vring.desc[i].next = i+1;
  688. vq->data[i] = NULL;
  689. }
  690. vq->data[i] = NULL;
  691. return &vq->vq;
  692. }
  693. EXPORT_SYMBOL_GPL(vring_new_virtqueue);
  694. void vring_del_virtqueue(struct virtqueue *vq)
  695. {
  696. list_del(&vq->list);
  697. kfree(to_vvq(vq));
  698. }
  699. EXPORT_SYMBOL_GPL(vring_del_virtqueue);
  700. /* Manipulates transport-specific feature bits. */
  701. void vring_transport_features(struct virtio_device *vdev)
  702. {
  703. unsigned int i;
  704. for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++) {
  705. switch (i) {
  706. case VIRTIO_RING_F_INDIRECT_DESC:
  707. break;
  708. case VIRTIO_RING_F_EVENT_IDX:
  709. break;
  710. default:
  711. /* We don't understand this bit. */
  712. clear_bit(i, vdev->features);
  713. }
  714. }
  715. }
  716. EXPORT_SYMBOL_GPL(vring_transport_features);
  717. /**
  718. * virtqueue_get_vring_size - return the size of the virtqueue's vring
  719. * @vq: the struct virtqueue containing the vring of interest.
  720. *
  721. * Returns the size of the vring. This is mainly used for boasting to
  722. * userspace. Unlike other operations, this need not be serialized.
  723. */
  724. unsigned int virtqueue_get_vring_size(struct virtqueue *_vq)
  725. {
  726. struct vring_virtqueue *vq = to_vvq(_vq);
  727. return vq->vring.num;
  728. }
  729. EXPORT_SYMBOL_GPL(virtqueue_get_vring_size);
  730. MODULE_LICENSE("GPL");