ring_buffer.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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 "hyperv_vmbus.h"
  30. void hv_begin_read(struct hv_ring_buffer_info *rbi)
  31. {
  32. rbi->ring_buffer->interrupt_mask = 1;
  33. virt_mb();
  34. }
  35. u32 hv_end_read(struct hv_ring_buffer_info *rbi)
  36. {
  37. rbi->ring_buffer->interrupt_mask = 0;
  38. virt_mb();
  39. /*
  40. * Now check to see if the ring buffer is still empty.
  41. * If it is not, we raced and we need to process new
  42. * incoming messages.
  43. */
  44. return hv_get_bytes_to_read(rbi);
  45. }
  46. /*
  47. * When we write to the ring buffer, check if the host needs to
  48. * be signaled. Here is the details of this protocol:
  49. *
  50. * 1. The host guarantees that while it is draining the
  51. * ring buffer, it will set the interrupt_mask to
  52. * indicate it does not need to be interrupted when
  53. * new data is placed.
  54. *
  55. * 2. The host guarantees that it will completely drain
  56. * the ring buffer before exiting the read loop. Further,
  57. * once the ring buffer is empty, it will clear the
  58. * interrupt_mask and re-check to see if new data has
  59. * arrived.
  60. */
  61. static bool hv_need_to_signal(u32 old_write, struct hv_ring_buffer_info *rbi)
  62. {
  63. virt_mb();
  64. if (READ_ONCE(rbi->ring_buffer->interrupt_mask))
  65. return false;
  66. /* check interrupt_mask before read_index */
  67. virt_rmb();
  68. /*
  69. * This is the only case we need to signal when the
  70. * ring transitions from being empty to non-empty.
  71. */
  72. if (old_write == READ_ONCE(rbi->ring_buffer->read_index))
  73. return true;
  74. return false;
  75. }
  76. /* Get the next write location for the specified ring buffer. */
  77. static inline u32
  78. hv_get_next_write_location(struct hv_ring_buffer_info *ring_info)
  79. {
  80. u32 next = ring_info->ring_buffer->write_index;
  81. return next;
  82. }
  83. /* Set the next write location for the specified ring buffer. */
  84. static inline void
  85. hv_set_next_write_location(struct hv_ring_buffer_info *ring_info,
  86. u32 next_write_location)
  87. {
  88. ring_info->ring_buffer->write_index = next_write_location;
  89. }
  90. /* Get the next read location for the specified ring buffer. */
  91. static inline u32
  92. hv_get_next_read_location(struct hv_ring_buffer_info *ring_info)
  93. {
  94. u32 next = ring_info->ring_buffer->read_index;
  95. return next;
  96. }
  97. /*
  98. * Get the next read location + offset for the specified ring buffer.
  99. * This allows the caller to skip.
  100. */
  101. static inline u32
  102. hv_get_next_readlocation_withoffset(struct hv_ring_buffer_info *ring_info,
  103. u32 offset)
  104. {
  105. u32 next = ring_info->ring_buffer->read_index;
  106. next += offset;
  107. next %= ring_info->ring_datasize;
  108. return next;
  109. }
  110. /* Set the next read location for the specified ring buffer. */
  111. static inline void
  112. hv_set_next_read_location(struct hv_ring_buffer_info *ring_info,
  113. u32 next_read_location)
  114. {
  115. ring_info->ring_buffer->read_index = next_read_location;
  116. ring_info->priv_read_index = next_read_location;
  117. }
  118. /* Get the size of the ring buffer. */
  119. static inline u32
  120. hv_get_ring_buffersize(struct hv_ring_buffer_info *ring_info)
  121. {
  122. return ring_info->ring_datasize;
  123. }
  124. /* Get the read and write indices as u64 of the specified ring buffer. */
  125. static inline u64
  126. hv_get_ring_bufferindices(struct hv_ring_buffer_info *ring_info)
  127. {
  128. return (u64)ring_info->ring_buffer->write_index << 32;
  129. }
  130. /*
  131. * Helper routine to copy to source from ring buffer.
  132. * Assume there is enough room. Handles wrap-around in src case only!!
  133. */
  134. static u32 hv_copyfrom_ringbuffer(
  135. struct hv_ring_buffer_info *ring_info,
  136. void *dest,
  137. u32 destlen,
  138. u32 start_read_offset)
  139. {
  140. void *ring_buffer = hv_get_ring_buffer(ring_info);
  141. u32 ring_buffer_size = hv_get_ring_buffersize(ring_info);
  142. u32 frag_len;
  143. /* wrap-around detected at the src */
  144. if (destlen > ring_buffer_size - start_read_offset) {
  145. frag_len = ring_buffer_size - start_read_offset;
  146. memcpy(dest, ring_buffer + start_read_offset, frag_len);
  147. memcpy(dest + frag_len, ring_buffer, destlen - frag_len);
  148. } else
  149. memcpy(dest, ring_buffer + start_read_offset, destlen);
  150. start_read_offset += destlen;
  151. start_read_offset %= ring_buffer_size;
  152. return start_read_offset;
  153. }
  154. /*
  155. * Helper routine to copy from source to ring buffer.
  156. * Assume there is enough room. Handles wrap-around in dest case only!!
  157. */
  158. static u32 hv_copyto_ringbuffer(
  159. struct hv_ring_buffer_info *ring_info,
  160. u32 start_write_offset,
  161. void *src,
  162. u32 srclen)
  163. {
  164. void *ring_buffer = hv_get_ring_buffer(ring_info);
  165. u32 ring_buffer_size = hv_get_ring_buffersize(ring_info);
  166. u32 frag_len;
  167. /* wrap-around detected! */
  168. if (srclen > ring_buffer_size - start_write_offset) {
  169. frag_len = ring_buffer_size - start_write_offset;
  170. memcpy(ring_buffer + start_write_offset, src, frag_len);
  171. memcpy(ring_buffer, src + frag_len, srclen - frag_len);
  172. } else
  173. memcpy(ring_buffer + start_write_offset, src, srclen);
  174. start_write_offset += srclen;
  175. start_write_offset %= ring_buffer_size;
  176. return start_write_offset;
  177. }
  178. /* Get various debug metrics for the specified ring buffer. */
  179. void hv_ringbuffer_get_debuginfo(struct hv_ring_buffer_info *ring_info,
  180. struct hv_ring_buffer_debug_info *debug_info)
  181. {
  182. u32 bytes_avail_towrite;
  183. u32 bytes_avail_toread;
  184. if (ring_info->ring_buffer) {
  185. hv_get_ringbuffer_availbytes(ring_info,
  186. &bytes_avail_toread,
  187. &bytes_avail_towrite);
  188. debug_info->bytes_avail_toread = bytes_avail_toread;
  189. debug_info->bytes_avail_towrite = bytes_avail_towrite;
  190. debug_info->current_read_index =
  191. ring_info->ring_buffer->read_index;
  192. debug_info->current_write_index =
  193. ring_info->ring_buffer->write_index;
  194. debug_info->current_interrupt_mask =
  195. ring_info->ring_buffer->interrupt_mask;
  196. }
  197. }
  198. /* Initialize the ring buffer. */
  199. int hv_ringbuffer_init(struct hv_ring_buffer_info *ring_info,
  200. void *buffer, u32 buflen)
  201. {
  202. if (sizeof(struct hv_ring_buffer) != PAGE_SIZE)
  203. return -EINVAL;
  204. memset(ring_info, 0, sizeof(struct hv_ring_buffer_info));
  205. ring_info->ring_buffer = (struct hv_ring_buffer *)buffer;
  206. ring_info->ring_buffer->read_index =
  207. ring_info->ring_buffer->write_index = 0;
  208. /* Set the feature bit for enabling flow control. */
  209. ring_info->ring_buffer->feature_bits.value = 1;
  210. ring_info->ring_size = buflen;
  211. ring_info->ring_datasize = buflen - sizeof(struct hv_ring_buffer);
  212. spin_lock_init(&ring_info->ring_lock);
  213. return 0;
  214. }
  215. /* Cleanup the ring buffer. */
  216. void hv_ringbuffer_cleanup(struct hv_ring_buffer_info *ring_info)
  217. {
  218. }
  219. /* Write to the ring buffer. */
  220. int hv_ringbuffer_write(struct hv_ring_buffer_info *outring_info,
  221. struct kvec *kv_list, u32 kv_count, bool *signal, bool lock)
  222. {
  223. int i = 0;
  224. u32 bytes_avail_towrite;
  225. u32 totalbytes_towrite = 0;
  226. u32 next_write_location;
  227. u32 old_write;
  228. u64 prev_indices = 0;
  229. unsigned long flags = 0;
  230. for (i = 0; i < kv_count; i++)
  231. totalbytes_towrite += kv_list[i].iov_len;
  232. totalbytes_towrite += sizeof(u64);
  233. if (lock)
  234. spin_lock_irqsave(&outring_info->ring_lock, flags);
  235. bytes_avail_towrite = hv_get_bytes_to_write(outring_info);
  236. /*
  237. * If there is only room for the packet, assume it is full.
  238. * Otherwise, the next time around, we think the ring buffer
  239. * is empty since the read index == write index.
  240. */
  241. if (bytes_avail_towrite <= totalbytes_towrite) {
  242. if (lock)
  243. spin_unlock_irqrestore(&outring_info->ring_lock, flags);
  244. return -EAGAIN;
  245. }
  246. /* Write to the ring buffer */
  247. next_write_location = hv_get_next_write_location(outring_info);
  248. old_write = next_write_location;
  249. for (i = 0; i < kv_count; i++) {
  250. next_write_location = hv_copyto_ringbuffer(outring_info,
  251. next_write_location,
  252. kv_list[i].iov_base,
  253. kv_list[i].iov_len);
  254. }
  255. /* Set previous packet start */
  256. prev_indices = hv_get_ring_bufferindices(outring_info);
  257. next_write_location = hv_copyto_ringbuffer(outring_info,
  258. next_write_location,
  259. &prev_indices,
  260. sizeof(u64));
  261. /* Issue a full memory barrier before updating the write index */
  262. virt_mb();
  263. /* Now, update the write location */
  264. hv_set_next_write_location(outring_info, next_write_location);
  265. if (lock)
  266. spin_unlock_irqrestore(&outring_info->ring_lock, flags);
  267. *signal = hv_need_to_signal(old_write, outring_info);
  268. return 0;
  269. }
  270. int hv_ringbuffer_read(struct hv_ring_buffer_info *inring_info,
  271. void *buffer, u32 buflen, u32 *buffer_actual_len,
  272. u64 *requestid, bool *signal, bool raw)
  273. {
  274. u32 bytes_avail_toread;
  275. u32 next_read_location = 0;
  276. u64 prev_indices = 0;
  277. struct vmpacket_descriptor desc;
  278. u32 offset;
  279. u32 packetlen;
  280. int ret = 0;
  281. if (buflen <= 0)
  282. return -EINVAL;
  283. *buffer_actual_len = 0;
  284. *requestid = 0;
  285. bytes_avail_toread = hv_get_bytes_to_read(inring_info);
  286. /* Make sure there is something to read */
  287. if (bytes_avail_toread < sizeof(desc)) {
  288. /*
  289. * No error is set when there is even no header, drivers are
  290. * supposed to analyze buffer_actual_len.
  291. */
  292. return ret;
  293. }
  294. next_read_location = hv_get_next_read_location(inring_info);
  295. next_read_location = hv_copyfrom_ringbuffer(inring_info, &desc,
  296. sizeof(desc),
  297. next_read_location);
  298. offset = raw ? 0 : (desc.offset8 << 3);
  299. packetlen = (desc.len8 << 3) - offset;
  300. *buffer_actual_len = packetlen;
  301. *requestid = desc.trans_id;
  302. if (bytes_avail_toread < packetlen + offset)
  303. return -EAGAIN;
  304. if (packetlen > buflen)
  305. return -ENOBUFS;
  306. next_read_location =
  307. hv_get_next_readlocation_withoffset(inring_info, offset);
  308. next_read_location = hv_copyfrom_ringbuffer(inring_info,
  309. buffer,
  310. packetlen,
  311. next_read_location);
  312. next_read_location = hv_copyfrom_ringbuffer(inring_info,
  313. &prev_indices,
  314. sizeof(u64),
  315. next_read_location);
  316. /*
  317. * Make sure all reads are done before we update the read index since
  318. * the writer may start writing to the read area once the read index
  319. * is updated.
  320. */
  321. virt_mb();
  322. /* Update the read index */
  323. hv_set_next_read_location(inring_info, next_read_location);
  324. *signal = hv_need_to_signal_on_read(inring_info);
  325. return ret;
  326. }