virtio_ring.c 20 KB

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