xenbus_comms.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /******************************************************************************
  2. * xenbus_comms.c
  3. *
  4. * Low level code to talks to Xen Store: ringbuffer and event channel.
  5. *
  6. * Copyright (C) 2005 Rusty Russell, IBM Corporation
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License version 2
  10. * as published by the Free Software Foundation; or, when distributed
  11. * separately from the Linux kernel or incorporated into other
  12. * software packages, subject to the following license:
  13. *
  14. * Permission is hereby granted, free of charge, to any person obtaining a copy
  15. * of this source file (the "Software"), to deal in the Software without
  16. * restriction, including without limitation the rights to use, copy, modify,
  17. * merge, publish, distribute, sublicense, and/or sell copies of the Software,
  18. * and to permit persons to whom the Software is furnished to do so, subject to
  19. * the following conditions:
  20. *
  21. * The above copyright notice and this permission notice shall be included in
  22. * all copies or substantial portions of the Software.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  25. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  26. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  27. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  28. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  29. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  30. * IN THE SOFTWARE.
  31. */
  32. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  33. #include <linux/wait.h>
  34. #include <linux/interrupt.h>
  35. #include <linux/sched.h>
  36. #include <linux/err.h>
  37. #include <xen/xenbus.h>
  38. #include <asm/xen/hypervisor.h>
  39. #include <xen/events.h>
  40. #include <xen/page.h>
  41. #include "xenbus_comms.h"
  42. static int xenbus_irq;
  43. static DECLARE_WORK(probe_work, xenbus_probe);
  44. static DECLARE_WAIT_QUEUE_HEAD(xb_waitq);
  45. static irqreturn_t wake_waiting(int irq, void *unused)
  46. {
  47. if (unlikely(xenstored_ready == 0)) {
  48. xenstored_ready = 1;
  49. schedule_work(&probe_work);
  50. }
  51. wake_up(&xb_waitq);
  52. return IRQ_HANDLED;
  53. }
  54. static int check_indexes(XENSTORE_RING_IDX cons, XENSTORE_RING_IDX prod)
  55. {
  56. return ((prod - cons) <= XENSTORE_RING_SIZE);
  57. }
  58. static void *get_output_chunk(XENSTORE_RING_IDX cons,
  59. XENSTORE_RING_IDX prod,
  60. char *buf, uint32_t *len)
  61. {
  62. *len = XENSTORE_RING_SIZE - MASK_XENSTORE_IDX(prod);
  63. if ((XENSTORE_RING_SIZE - (prod - cons)) < *len)
  64. *len = XENSTORE_RING_SIZE - (prod - cons);
  65. return buf + MASK_XENSTORE_IDX(prod);
  66. }
  67. static const void *get_input_chunk(XENSTORE_RING_IDX cons,
  68. XENSTORE_RING_IDX prod,
  69. const char *buf, uint32_t *len)
  70. {
  71. *len = XENSTORE_RING_SIZE - MASK_XENSTORE_IDX(cons);
  72. if ((prod - cons) < *len)
  73. *len = prod - cons;
  74. return buf + MASK_XENSTORE_IDX(cons);
  75. }
  76. /**
  77. * xb_write - low level write
  78. * @data: buffer to send
  79. * @len: length of buffer
  80. *
  81. * Returns 0 on success, error otherwise.
  82. */
  83. int xb_write(const void *data, unsigned len)
  84. {
  85. struct xenstore_domain_interface *intf = xen_store_interface;
  86. XENSTORE_RING_IDX cons, prod;
  87. int rc;
  88. while (len != 0) {
  89. void *dst;
  90. unsigned int avail;
  91. rc = wait_event_interruptible(
  92. xb_waitq,
  93. (intf->req_prod - intf->req_cons) !=
  94. XENSTORE_RING_SIZE);
  95. if (rc < 0)
  96. return rc;
  97. /* Read indexes, then verify. */
  98. cons = intf->req_cons;
  99. prod = intf->req_prod;
  100. if (!check_indexes(cons, prod)) {
  101. intf->req_cons = intf->req_prod = 0;
  102. return -EIO;
  103. }
  104. dst = get_output_chunk(cons, prod, intf->req, &avail);
  105. if (avail == 0)
  106. continue;
  107. if (avail > len)
  108. avail = len;
  109. /* Must write data /after/ reading the consumer index. */
  110. mb();
  111. memcpy(dst, data, avail);
  112. data += avail;
  113. len -= avail;
  114. /* Other side must not see new producer until data is there. */
  115. wmb();
  116. intf->req_prod += avail;
  117. /* Implies mb(): other side will see the updated producer. */
  118. notify_remote_via_evtchn(xen_store_evtchn);
  119. }
  120. return 0;
  121. }
  122. int xb_data_to_read(void)
  123. {
  124. struct xenstore_domain_interface *intf = xen_store_interface;
  125. return (intf->rsp_cons != intf->rsp_prod);
  126. }
  127. int xb_wait_for_data_to_read(void)
  128. {
  129. return wait_event_interruptible(xb_waitq, xb_data_to_read());
  130. }
  131. int xb_read(void *data, unsigned len)
  132. {
  133. struct xenstore_domain_interface *intf = xen_store_interface;
  134. XENSTORE_RING_IDX cons, prod;
  135. int rc;
  136. while (len != 0) {
  137. unsigned int avail;
  138. const char *src;
  139. rc = xb_wait_for_data_to_read();
  140. if (rc < 0)
  141. return rc;
  142. /* Read indexes, then verify. */
  143. cons = intf->rsp_cons;
  144. prod = intf->rsp_prod;
  145. if (!check_indexes(cons, prod)) {
  146. intf->rsp_cons = intf->rsp_prod = 0;
  147. return -EIO;
  148. }
  149. src = get_input_chunk(cons, prod, intf->rsp, &avail);
  150. if (avail == 0)
  151. continue;
  152. if (avail > len)
  153. avail = len;
  154. /* Must read data /after/ reading the producer index. */
  155. rmb();
  156. memcpy(data, src, avail);
  157. data += avail;
  158. len -= avail;
  159. /* Other side must not see free space until we've copied out */
  160. mb();
  161. intf->rsp_cons += avail;
  162. pr_debug("Finished read of %i bytes (%i to go)\n", avail, len);
  163. /* Implies mb(): other side will see the updated consumer. */
  164. notify_remote_via_evtchn(xen_store_evtchn);
  165. }
  166. return 0;
  167. }
  168. /**
  169. * xb_init_comms - Set up interrupt handler off store event channel.
  170. */
  171. int xb_init_comms(void)
  172. {
  173. struct xenstore_domain_interface *intf = xen_store_interface;
  174. if (intf->req_prod != intf->req_cons)
  175. pr_err("request ring is not quiescent (%08x:%08x)!\n",
  176. intf->req_cons, intf->req_prod);
  177. if (intf->rsp_prod != intf->rsp_cons) {
  178. pr_warn("response ring is not quiescent (%08x:%08x): fixing up\n",
  179. intf->rsp_cons, intf->rsp_prod);
  180. /* breaks kdump */
  181. if (!reset_devices)
  182. intf->rsp_cons = intf->rsp_prod;
  183. }
  184. if (xenbus_irq) {
  185. /* Already have an irq; assume we're resuming */
  186. rebind_evtchn_irq(xen_store_evtchn, xenbus_irq);
  187. } else {
  188. int err;
  189. err = bind_evtchn_to_irqhandler(xen_store_evtchn, wake_waiting,
  190. 0, "xenbus", &xb_waitq);
  191. if (err < 0) {
  192. pr_err("request irq failed %i\n", err);
  193. return err;
  194. }
  195. xenbus_irq = err;
  196. }
  197. return 0;
  198. }
  199. void xb_deinit_comms(void)
  200. {
  201. unbind_from_irqhandler(xenbus_irq, &xb_waitq);
  202. xenbus_irq = 0;
  203. }