xenbus_comms.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. #include <linux/wait.h>
  33. #include <linux/interrupt.h>
  34. #include <linux/sched.h>
  35. #include <linux/err.h>
  36. #include <xen/xenbus.h>
  37. #include <asm/xen/hypervisor.h>
  38. #include <xen/events.h>
  39. #include <xen/page.h>
  40. #include "xenbus_comms.h"
  41. static int xenbus_irq;
  42. static DECLARE_WORK(probe_work, xenbus_probe);
  43. static DECLARE_WAIT_QUEUE_HEAD(xb_waitq);
  44. static irqreturn_t wake_waiting(int irq, void *unused)
  45. {
  46. if (unlikely(xenstored_ready == 0)) {
  47. xenstored_ready = 1;
  48. schedule_work(&probe_work);
  49. }
  50. wake_up(&xb_waitq);
  51. return IRQ_HANDLED;
  52. }
  53. static int check_indexes(XENSTORE_RING_IDX cons, XENSTORE_RING_IDX prod)
  54. {
  55. return ((prod - cons) <= XENSTORE_RING_SIZE);
  56. }
  57. static void *get_output_chunk(XENSTORE_RING_IDX cons,
  58. XENSTORE_RING_IDX prod,
  59. char *buf, uint32_t *len)
  60. {
  61. *len = XENSTORE_RING_SIZE - MASK_XENSTORE_IDX(prod);
  62. if ((XENSTORE_RING_SIZE - (prod - cons)) < *len)
  63. *len = XENSTORE_RING_SIZE - (prod - cons);
  64. return buf + MASK_XENSTORE_IDX(prod);
  65. }
  66. static const void *get_input_chunk(XENSTORE_RING_IDX cons,
  67. XENSTORE_RING_IDX prod,
  68. const char *buf, uint32_t *len)
  69. {
  70. *len = XENSTORE_RING_SIZE - MASK_XENSTORE_IDX(cons);
  71. if ((prod - cons) < *len)
  72. *len = prod - cons;
  73. return buf + MASK_XENSTORE_IDX(cons);
  74. }
  75. /**
  76. * xb_write - low level write
  77. * @data: buffer to send
  78. * @len: length of buffer
  79. *
  80. * Returns 0 on success, error otherwise.
  81. */
  82. int xb_write(const void *data, unsigned len)
  83. {
  84. struct xenstore_domain_interface *intf = xen_store_interface;
  85. XENSTORE_RING_IDX cons, prod;
  86. int rc;
  87. while (len != 0) {
  88. void *dst;
  89. unsigned int avail;
  90. rc = wait_event_interruptible(
  91. xb_waitq,
  92. (intf->req_prod - intf->req_cons) !=
  93. XENSTORE_RING_SIZE);
  94. if (rc < 0)
  95. return rc;
  96. /* Read indexes, then verify. */
  97. cons = intf->req_cons;
  98. prod = intf->req_prod;
  99. if (!check_indexes(cons, prod)) {
  100. intf->req_cons = intf->req_prod = 0;
  101. return -EIO;
  102. }
  103. dst = get_output_chunk(cons, prod, intf->req, &avail);
  104. if (avail == 0)
  105. continue;
  106. if (avail > len)
  107. avail = len;
  108. /* Must write data /after/ reading the consumer index. */
  109. mb();
  110. memcpy(dst, data, avail);
  111. data += avail;
  112. len -= avail;
  113. /* Other side must not see new producer until data is there. */
  114. wmb();
  115. intf->req_prod += avail;
  116. /* Implies mb(): other side will see the updated producer. */
  117. notify_remote_via_evtchn(xen_store_evtchn);
  118. }
  119. return 0;
  120. }
  121. int xb_data_to_read(void)
  122. {
  123. struct xenstore_domain_interface *intf = xen_store_interface;
  124. return (intf->rsp_cons != intf->rsp_prod);
  125. }
  126. int xb_wait_for_data_to_read(void)
  127. {
  128. return wait_event_interruptible(xb_waitq, xb_data_to_read());
  129. }
  130. int xb_read(void *data, unsigned len)
  131. {
  132. struct xenstore_domain_interface *intf = xen_store_interface;
  133. XENSTORE_RING_IDX cons, prod;
  134. int rc;
  135. while (len != 0) {
  136. unsigned int avail;
  137. const char *src;
  138. rc = xb_wait_for_data_to_read();
  139. if (rc < 0)
  140. return rc;
  141. /* Read indexes, then verify. */
  142. cons = intf->rsp_cons;
  143. prod = intf->rsp_prod;
  144. if (!check_indexes(cons, prod)) {
  145. intf->rsp_cons = intf->rsp_prod = 0;
  146. return -EIO;
  147. }
  148. src = get_input_chunk(cons, prod, intf->rsp, &avail);
  149. if (avail == 0)
  150. continue;
  151. if (avail > len)
  152. avail = len;
  153. /* Must read data /after/ reading the producer index. */
  154. rmb();
  155. memcpy(data, src, avail);
  156. data += avail;
  157. len -= avail;
  158. /* Other side must not see free space until we've copied out */
  159. mb();
  160. intf->rsp_cons += avail;
  161. pr_debug("Finished read of %i bytes (%i to go)\n", avail, len);
  162. /* Implies mb(): other side will see the updated consumer. */
  163. notify_remote_via_evtchn(xen_store_evtchn);
  164. }
  165. return 0;
  166. }
  167. /**
  168. * xb_init_comms - Set up interrupt handler off store event channel.
  169. */
  170. int xb_init_comms(void)
  171. {
  172. struct xenstore_domain_interface *intf = xen_store_interface;
  173. if (intf->req_prod != intf->req_cons)
  174. printk(KERN_ERR "XENBUS request ring is not quiescent "
  175. "(%08x:%08x)!\n", intf->req_cons, intf->req_prod);
  176. if (intf->rsp_prod != intf->rsp_cons) {
  177. printk(KERN_WARNING "XENBUS response ring is not quiescent "
  178. "(%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. printk(KERN_ERR "XENBUS request irq failed %i\n", err);
  193. return err;
  194. }
  195. xenbus_irq = err;
  196. }
  197. return 0;
  198. }