ring_buffer.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. /*
  2. *
  3. * Copyright (c) 2009, Microsoft Corporation.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  16. * Place - Suite 330, Boston, MA 02111-1307 USA.
  17. *
  18. * Authors:
  19. * Haiyang Zhang <haiyangz@microsoft.com>
  20. * Hank Janssen <hjanssen@microsoft.com>
  21. * K. Y. Srinivasan <kys@microsoft.com>
  22. *
  23. */
  24. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  25. #include <linux/kernel.h>
  26. #include <linux/mm.h>
  27. #include <linux/hyperv.h>
  28. #include <linux/uio.h>
  29. #include <linux/vmalloc.h>
  30. #include <linux/slab.h>
  31. #include "hyperv_vmbus.h"
  32. void hv_begin_read(struct hv_ring_buffer_info *rbi)
  33. {
  34. rbi->ring_buffer->interrupt_mask = 1;
  35. virt_mb();
  36. }
  37. u32 hv_end_read(struct hv_ring_buffer_info *rbi)
  38. {
  39. rbi->ring_buffer->interrupt_mask = 0;
  40. virt_mb();
  41. /*
  42. * Now check to see if the ring buffer is still empty.
  43. * If it is not, we raced and we need to process new
  44. * incoming messages.
  45. */
  46. return hv_get_bytes_to_read(rbi);
  47. }
  48. /*
  49. * When we write to the ring buffer, check if the host needs to
  50. * be signaled. Here is the details of this protocol:
  51. *
  52. * 1. The host guarantees that while it is draining the
  53. * ring buffer, it will set the interrupt_mask to
  54. * indicate it does not need to be interrupted when
  55. * new data is placed.
  56. *
  57. * 2. The host guarantees that it will completely drain
  58. * the ring buffer before exiting the read loop. Further,
  59. * once the ring buffer is empty, it will clear the
  60. * interrupt_mask and re-check to see if new data has
  61. * arrived.
  62. */
  63. static bool hv_need_to_signal(u32 old_write, struct hv_ring_buffer_info *rbi,
  64. enum hv_signal_policy policy)
  65. {
  66. virt_mb();
  67. if (READ_ONCE(rbi->ring_buffer->interrupt_mask))
  68. return false;
  69. /*
  70. * When the client wants to control signaling,
  71. * we only honour the host interrupt mask.
  72. */
  73. if (policy == HV_SIGNAL_POLICY_EXPLICIT)
  74. return true;
  75. /* check interrupt_mask before read_index */
  76. virt_rmb();
  77. /*
  78. * This is the only case we need to signal when the
  79. * ring transitions from being empty to non-empty.
  80. */
  81. if (old_write == READ_ONCE(rbi->ring_buffer->read_index))
  82. return true;
  83. return false;
  84. }
  85. /* Get the next write location for the specified ring buffer. */
  86. static inline u32
  87. hv_get_next_write_location(struct hv_ring_buffer_info *ring_info)
  88. {
  89. u32 next = ring_info->ring_buffer->write_index;
  90. return next;
  91. }
  92. /* Set the next write location for the specified ring buffer. */
  93. static inline void
  94. hv_set_next_write_location(struct hv_ring_buffer_info *ring_info,
  95. u32 next_write_location)
  96. {
  97. ring_info->ring_buffer->write_index = next_write_location;
  98. }
  99. /* Get the next read location for the specified ring buffer. */
  100. static inline u32
  101. hv_get_next_read_location(struct hv_ring_buffer_info *ring_info)
  102. {
  103. u32 next = ring_info->ring_buffer->read_index;
  104. return next;
  105. }
  106. /*
  107. * Get the next read location + offset for the specified ring buffer.
  108. * This allows the caller to skip.
  109. */
  110. static inline u32
  111. hv_get_next_readlocation_withoffset(struct hv_ring_buffer_info *ring_info,
  112. u32 offset)
  113. {
  114. u32 next = ring_info->ring_buffer->read_index;
  115. next += offset;
  116. next %= ring_info->ring_datasize;
  117. return next;
  118. }
  119. /* Set the next read location for the specified ring buffer. */
  120. static inline void
  121. hv_set_next_read_location(struct hv_ring_buffer_info *ring_info,
  122. u32 next_read_location)
  123. {
  124. ring_info->ring_buffer->read_index = next_read_location;
  125. ring_info->priv_read_index = next_read_location;
  126. }
  127. /* Get the size of the ring buffer. */
  128. static inline u32
  129. hv_get_ring_buffersize(struct hv_ring_buffer_info *ring_info)
  130. {
  131. return ring_info->ring_datasize;
  132. }
  133. /* Get the read and write indices as u64 of the specified ring buffer. */
  134. static inline u64
  135. hv_get_ring_bufferindices(struct hv_ring_buffer_info *ring_info)
  136. {
  137. return (u64)ring_info->ring_buffer->write_index << 32;
  138. }
  139. /*
  140. * Helper routine to copy to source from ring buffer.
  141. * Assume there is enough room. Handles wrap-around in src case only!!
  142. */
  143. static u32 hv_copyfrom_ringbuffer(
  144. struct hv_ring_buffer_info *ring_info,
  145. void *dest,
  146. u32 destlen,
  147. u32 start_read_offset)
  148. {
  149. void *ring_buffer = hv_get_ring_buffer(ring_info);
  150. u32 ring_buffer_size = hv_get_ring_buffersize(ring_info);
  151. memcpy(dest, ring_buffer + start_read_offset, destlen);
  152. start_read_offset += destlen;
  153. start_read_offset %= ring_buffer_size;
  154. return start_read_offset;
  155. }
  156. /*
  157. * Helper routine to copy from source to ring buffer.
  158. * Assume there is enough room. Handles wrap-around in dest case only!!
  159. */
  160. static u32 hv_copyto_ringbuffer(
  161. struct hv_ring_buffer_info *ring_info,
  162. u32 start_write_offset,
  163. void *src,
  164. u32 srclen)
  165. {
  166. void *ring_buffer = hv_get_ring_buffer(ring_info);
  167. u32 ring_buffer_size = hv_get_ring_buffersize(ring_info);
  168. memcpy(ring_buffer + start_write_offset, src, srclen);
  169. start_write_offset += srclen;
  170. start_write_offset %= ring_buffer_size;
  171. return start_write_offset;
  172. }
  173. /* Get various debug metrics for the specified ring buffer. */
  174. void hv_ringbuffer_get_debuginfo(struct hv_ring_buffer_info *ring_info,
  175. struct hv_ring_buffer_debug_info *debug_info)
  176. {
  177. u32 bytes_avail_towrite;
  178. u32 bytes_avail_toread;
  179. if (ring_info->ring_buffer) {
  180. hv_get_ringbuffer_availbytes(ring_info,
  181. &bytes_avail_toread,
  182. &bytes_avail_towrite);
  183. debug_info->bytes_avail_toread = bytes_avail_toread;
  184. debug_info->bytes_avail_towrite = bytes_avail_towrite;
  185. debug_info->current_read_index =
  186. ring_info->ring_buffer->read_index;
  187. debug_info->current_write_index =
  188. ring_info->ring_buffer->write_index;
  189. debug_info->current_interrupt_mask =
  190. ring_info->ring_buffer->interrupt_mask;
  191. }
  192. }
  193. /* Initialize the ring buffer. */
  194. int hv_ringbuffer_init(struct hv_ring_buffer_info *ring_info,
  195. struct page *pages, u32 page_cnt)
  196. {
  197. int i;
  198. struct page **pages_wraparound;
  199. BUILD_BUG_ON((sizeof(struct hv_ring_buffer) != PAGE_SIZE));
  200. memset(ring_info, 0, sizeof(struct hv_ring_buffer_info));
  201. /*
  202. * First page holds struct hv_ring_buffer, do wraparound mapping for
  203. * the rest.
  204. */
  205. pages_wraparound = kzalloc(sizeof(struct page *) * (page_cnt * 2 - 1),
  206. GFP_KERNEL);
  207. if (!pages_wraparound)
  208. return -ENOMEM;
  209. pages_wraparound[0] = pages;
  210. for (i = 0; i < 2 * (page_cnt - 1); i++)
  211. pages_wraparound[i + 1] = &pages[i % (page_cnt - 1) + 1];
  212. ring_info->ring_buffer = (struct hv_ring_buffer *)
  213. vmap(pages_wraparound, page_cnt * 2 - 1, VM_MAP, PAGE_KERNEL);
  214. kfree(pages_wraparound);
  215. if (!ring_info->ring_buffer)
  216. return -ENOMEM;
  217. ring_info->ring_buffer->read_index =
  218. ring_info->ring_buffer->write_index = 0;
  219. /* Set the feature bit for enabling flow control. */
  220. ring_info->ring_buffer->feature_bits.value = 1;
  221. ring_info->ring_size = page_cnt << PAGE_SHIFT;
  222. ring_info->ring_datasize = ring_info->ring_size -
  223. sizeof(struct hv_ring_buffer);
  224. spin_lock_init(&ring_info->ring_lock);
  225. return 0;
  226. }
  227. /* Cleanup the ring buffer. */
  228. void hv_ringbuffer_cleanup(struct hv_ring_buffer_info *ring_info)
  229. {
  230. vunmap(ring_info->ring_buffer);
  231. }
  232. /* Write to the ring buffer. */
  233. int hv_ringbuffer_write(struct hv_ring_buffer_info *outring_info,
  234. struct kvec *kv_list, u32 kv_count, bool *signal, bool lock,
  235. enum hv_signal_policy policy)
  236. {
  237. int i = 0;
  238. u32 bytes_avail_towrite;
  239. u32 totalbytes_towrite = 0;
  240. u32 next_write_location;
  241. u32 old_write;
  242. u64 prev_indices = 0;
  243. unsigned long flags = 0;
  244. for (i = 0; i < kv_count; i++)
  245. totalbytes_towrite += kv_list[i].iov_len;
  246. totalbytes_towrite += sizeof(u64);
  247. if (lock)
  248. spin_lock_irqsave(&outring_info->ring_lock, flags);
  249. bytes_avail_towrite = hv_get_bytes_to_write(outring_info);
  250. /*
  251. * If there is only room for the packet, assume it is full.
  252. * Otherwise, the next time around, we think the ring buffer
  253. * is empty since the read index == write index.
  254. */
  255. if (bytes_avail_towrite <= totalbytes_towrite) {
  256. if (lock)
  257. spin_unlock_irqrestore(&outring_info->ring_lock, flags);
  258. return -EAGAIN;
  259. }
  260. /* Write to the ring buffer */
  261. next_write_location = hv_get_next_write_location(outring_info);
  262. old_write = next_write_location;
  263. for (i = 0; i < kv_count; i++) {
  264. next_write_location = hv_copyto_ringbuffer(outring_info,
  265. next_write_location,
  266. kv_list[i].iov_base,
  267. kv_list[i].iov_len);
  268. }
  269. /* Set previous packet start */
  270. prev_indices = hv_get_ring_bufferindices(outring_info);
  271. next_write_location = hv_copyto_ringbuffer(outring_info,
  272. next_write_location,
  273. &prev_indices,
  274. sizeof(u64));
  275. /* Issue a full memory barrier before updating the write index */
  276. virt_mb();
  277. /* Now, update the write location */
  278. hv_set_next_write_location(outring_info, next_write_location);
  279. if (lock)
  280. spin_unlock_irqrestore(&outring_info->ring_lock, flags);
  281. *signal = hv_need_to_signal(old_write, outring_info, policy);
  282. return 0;
  283. }
  284. int hv_ringbuffer_read(struct hv_ring_buffer_info *inring_info,
  285. void *buffer, u32 buflen, u32 *buffer_actual_len,
  286. u64 *requestid, bool *signal, bool raw)
  287. {
  288. u32 bytes_avail_toread;
  289. u32 next_read_location = 0;
  290. u64 prev_indices = 0;
  291. struct vmpacket_descriptor desc;
  292. u32 offset;
  293. u32 packetlen;
  294. int ret = 0;
  295. if (buflen <= 0)
  296. return -EINVAL;
  297. *buffer_actual_len = 0;
  298. *requestid = 0;
  299. bytes_avail_toread = hv_get_bytes_to_read(inring_info);
  300. /* Make sure there is something to read */
  301. if (bytes_avail_toread < sizeof(desc)) {
  302. /*
  303. * No error is set when there is even no header, drivers are
  304. * supposed to analyze buffer_actual_len.
  305. */
  306. return ret;
  307. }
  308. next_read_location = hv_get_next_read_location(inring_info);
  309. next_read_location = hv_copyfrom_ringbuffer(inring_info, &desc,
  310. sizeof(desc),
  311. next_read_location);
  312. offset = raw ? 0 : (desc.offset8 << 3);
  313. packetlen = (desc.len8 << 3) - offset;
  314. *buffer_actual_len = packetlen;
  315. *requestid = desc.trans_id;
  316. if (bytes_avail_toread < packetlen + offset)
  317. return -EAGAIN;
  318. if (packetlen > buflen)
  319. return -ENOBUFS;
  320. next_read_location =
  321. hv_get_next_readlocation_withoffset(inring_info, offset);
  322. next_read_location = hv_copyfrom_ringbuffer(inring_info,
  323. buffer,
  324. packetlen,
  325. next_read_location);
  326. next_read_location = hv_copyfrom_ringbuffer(inring_info,
  327. &prev_indices,
  328. sizeof(u64),
  329. next_read_location);
  330. /*
  331. * Make sure all reads are done before we update the read index since
  332. * the writer may start writing to the read area once the read index
  333. * is updated.
  334. */
  335. virt_mb();
  336. /* Update the read index */
  337. hv_set_next_read_location(inring_info, next_read_location);
  338. *signal = hv_need_to_signal_on_read(inring_info);
  339. return ret;
  340. }