ring_buffer.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  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 "hyperv_vmbus.h"
  29. void hv_begin_read(struct hv_ring_buffer_info *rbi)
  30. {
  31. rbi->ring_buffer->interrupt_mask = 1;
  32. smp_mb();
  33. }
  34. u32 hv_end_read(struct hv_ring_buffer_info *rbi)
  35. {
  36. u32 read;
  37. u32 write;
  38. rbi->ring_buffer->interrupt_mask = 0;
  39. smp_mb();
  40. /*
  41. * Now check to see if the ring buffer is still empty.
  42. * If it is not, we raced and we need to process new
  43. * incoming messages.
  44. */
  45. hv_get_ringbuffer_availbytes(rbi, &read, &write);
  46. return read;
  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. {
  65. if (rbi->ring_buffer->interrupt_mask)
  66. return false;
  67. /*
  68. * This is the only case we need to signal when the
  69. * ring transitions from being empty to non-empty.
  70. */
  71. if (old_write == rbi->ring_buffer->read_index)
  72. return true;
  73. return false;
  74. }
  75. /*
  76. * To optimize the flow management on the send-side,
  77. * when the sender is blocked because of lack of
  78. * sufficient space in the ring buffer, potential the
  79. * consumer of the ring buffer can signal the producer.
  80. * This is controlled by the following parameters:
  81. *
  82. * 1. pending_send_sz: This is the size in bytes that the
  83. * producer is trying to send.
  84. * 2. The feature bit feat_pending_send_sz set to indicate if
  85. * the consumer of the ring will signal when the ring
  86. * state transitions from being full to a state where
  87. * there is room for the producer to send the pending packet.
  88. */
  89. static bool hv_need_to_signal_on_read(u32 old_rd,
  90. struct hv_ring_buffer_info *rbi)
  91. {
  92. u32 prev_write_sz;
  93. u32 cur_write_sz;
  94. u32 r_size;
  95. u32 write_loc = rbi->ring_buffer->write_index;
  96. u32 read_loc = rbi->ring_buffer->read_index;
  97. u32 pending_sz = rbi->ring_buffer->pending_send_sz;
  98. /*
  99. * If the other end is not blocked on write don't bother.
  100. */
  101. if (pending_sz == 0)
  102. return false;
  103. r_size = rbi->ring_datasize;
  104. cur_write_sz = write_loc >= read_loc ? r_size - (write_loc - read_loc) :
  105. read_loc - write_loc;
  106. prev_write_sz = write_loc >= old_rd ? r_size - (write_loc - old_rd) :
  107. old_rd - write_loc;
  108. if ((prev_write_sz < pending_sz) && (cur_write_sz >= pending_sz))
  109. return true;
  110. return false;
  111. }
  112. /*
  113. * hv_get_next_write_location()
  114. *
  115. * Get the next write location for the specified ring buffer
  116. *
  117. */
  118. static inline u32
  119. hv_get_next_write_location(struct hv_ring_buffer_info *ring_info)
  120. {
  121. u32 next = ring_info->ring_buffer->write_index;
  122. return next;
  123. }
  124. /*
  125. * hv_set_next_write_location()
  126. *
  127. * Set the next write location for the specified ring buffer
  128. *
  129. */
  130. static inline void
  131. hv_set_next_write_location(struct hv_ring_buffer_info *ring_info,
  132. u32 next_write_location)
  133. {
  134. ring_info->ring_buffer->write_index = next_write_location;
  135. }
  136. /*
  137. * hv_get_next_read_location()
  138. *
  139. * Get the next read location for the specified ring buffer
  140. */
  141. static inline u32
  142. hv_get_next_read_location(struct hv_ring_buffer_info *ring_info)
  143. {
  144. u32 next = ring_info->ring_buffer->read_index;
  145. return next;
  146. }
  147. /*
  148. * hv_get_next_readlocation_withoffset()
  149. *
  150. * Get the next read location + offset for the specified ring buffer.
  151. * This allows the caller to skip
  152. */
  153. static inline u32
  154. hv_get_next_readlocation_withoffset(struct hv_ring_buffer_info *ring_info,
  155. u32 offset)
  156. {
  157. u32 next = ring_info->ring_buffer->read_index;
  158. next += offset;
  159. next %= ring_info->ring_datasize;
  160. return next;
  161. }
  162. /*
  163. *
  164. * hv_set_next_read_location()
  165. *
  166. * Set the next read location for the specified ring buffer
  167. *
  168. */
  169. static inline void
  170. hv_set_next_read_location(struct hv_ring_buffer_info *ring_info,
  171. u32 next_read_location)
  172. {
  173. ring_info->ring_buffer->read_index = next_read_location;
  174. }
  175. /*
  176. *
  177. * hv_get_ring_buffer()
  178. *
  179. * Get the start of the ring buffer
  180. */
  181. static inline void *
  182. hv_get_ring_buffer(struct hv_ring_buffer_info *ring_info)
  183. {
  184. return (void *)ring_info->ring_buffer->buffer;
  185. }
  186. /*
  187. *
  188. * hv_get_ring_buffersize()
  189. *
  190. * Get the size of the ring buffer
  191. */
  192. static inline u32
  193. hv_get_ring_buffersize(struct hv_ring_buffer_info *ring_info)
  194. {
  195. return ring_info->ring_datasize;
  196. }
  197. /*
  198. *
  199. * hv_get_ring_bufferindices()
  200. *
  201. * Get the read and write indices as u64 of the specified ring buffer
  202. *
  203. */
  204. static inline u64
  205. hv_get_ring_bufferindices(struct hv_ring_buffer_info *ring_info)
  206. {
  207. return (u64)ring_info->ring_buffer->write_index << 32;
  208. }
  209. /*
  210. *
  211. * hv_copyfrom_ringbuffer()
  212. *
  213. * Helper routine to copy to source from ring buffer.
  214. * Assume there is enough room. Handles wrap-around in src case only!!
  215. *
  216. */
  217. static u32 hv_copyfrom_ringbuffer(
  218. struct hv_ring_buffer_info *ring_info,
  219. void *dest,
  220. u32 destlen,
  221. u32 start_read_offset)
  222. {
  223. void *ring_buffer = hv_get_ring_buffer(ring_info);
  224. u32 ring_buffer_size = hv_get_ring_buffersize(ring_info);
  225. u32 frag_len;
  226. /* wrap-around detected at the src */
  227. if (destlen > ring_buffer_size - start_read_offset) {
  228. frag_len = ring_buffer_size - start_read_offset;
  229. memcpy(dest, ring_buffer + start_read_offset, frag_len);
  230. memcpy(dest + frag_len, ring_buffer, destlen - frag_len);
  231. } else
  232. memcpy(dest, ring_buffer + start_read_offset, destlen);
  233. start_read_offset += destlen;
  234. start_read_offset %= ring_buffer_size;
  235. return start_read_offset;
  236. }
  237. /*
  238. *
  239. * hv_copyto_ringbuffer()
  240. *
  241. * Helper routine to copy from source to ring buffer.
  242. * Assume there is enough room. Handles wrap-around in dest case only!!
  243. *
  244. */
  245. static u32 hv_copyto_ringbuffer(
  246. struct hv_ring_buffer_info *ring_info,
  247. u32 start_write_offset,
  248. void *src,
  249. u32 srclen)
  250. {
  251. void *ring_buffer = hv_get_ring_buffer(ring_info);
  252. u32 ring_buffer_size = hv_get_ring_buffersize(ring_info);
  253. u32 frag_len;
  254. /* wrap-around detected! */
  255. if (srclen > ring_buffer_size - start_write_offset) {
  256. frag_len = ring_buffer_size - start_write_offset;
  257. memcpy(ring_buffer + start_write_offset, src, frag_len);
  258. memcpy(ring_buffer, src + frag_len, srclen - frag_len);
  259. } else
  260. memcpy(ring_buffer + start_write_offset, src, srclen);
  261. start_write_offset += srclen;
  262. start_write_offset %= ring_buffer_size;
  263. return start_write_offset;
  264. }
  265. /*
  266. *
  267. * hv_ringbuffer_get_debuginfo()
  268. *
  269. * Get various debug metrics for the specified ring buffer
  270. *
  271. */
  272. void hv_ringbuffer_get_debuginfo(struct hv_ring_buffer_info *ring_info,
  273. struct hv_ring_buffer_debug_info *debug_info)
  274. {
  275. u32 bytes_avail_towrite;
  276. u32 bytes_avail_toread;
  277. if (ring_info->ring_buffer) {
  278. hv_get_ringbuffer_availbytes(ring_info,
  279. &bytes_avail_toread,
  280. &bytes_avail_towrite);
  281. debug_info->bytes_avail_toread = bytes_avail_toread;
  282. debug_info->bytes_avail_towrite = bytes_avail_towrite;
  283. debug_info->current_read_index =
  284. ring_info->ring_buffer->read_index;
  285. debug_info->current_write_index =
  286. ring_info->ring_buffer->write_index;
  287. debug_info->current_interrupt_mask =
  288. ring_info->ring_buffer->interrupt_mask;
  289. }
  290. }
  291. /*
  292. *
  293. * hv_ringbuffer_init()
  294. *
  295. *Initialize the ring buffer
  296. *
  297. */
  298. int hv_ringbuffer_init(struct hv_ring_buffer_info *ring_info,
  299. void *buffer, u32 buflen)
  300. {
  301. if (sizeof(struct hv_ring_buffer) != PAGE_SIZE)
  302. return -EINVAL;
  303. memset(ring_info, 0, sizeof(struct hv_ring_buffer_info));
  304. ring_info->ring_buffer = (struct hv_ring_buffer *)buffer;
  305. ring_info->ring_buffer->read_index =
  306. ring_info->ring_buffer->write_index = 0;
  307. ring_info->ring_size = buflen;
  308. ring_info->ring_datasize = buflen - sizeof(struct hv_ring_buffer);
  309. spin_lock_init(&ring_info->ring_lock);
  310. return 0;
  311. }
  312. /*
  313. *
  314. * hv_ringbuffer_cleanup()
  315. *
  316. * Cleanup the ring buffer
  317. *
  318. */
  319. void hv_ringbuffer_cleanup(struct hv_ring_buffer_info *ring_info)
  320. {
  321. }
  322. /*
  323. *
  324. * hv_ringbuffer_write()
  325. *
  326. * Write to the ring buffer
  327. *
  328. */
  329. int hv_ringbuffer_write(struct hv_ring_buffer_info *outring_info,
  330. struct scatterlist *sglist, u32 sgcount, bool *signal)
  331. {
  332. int i = 0;
  333. u32 bytes_avail_towrite;
  334. u32 bytes_avail_toread;
  335. u32 totalbytes_towrite = 0;
  336. struct scatterlist *sg;
  337. u32 next_write_location;
  338. u32 old_write;
  339. u64 prev_indices = 0;
  340. unsigned long flags;
  341. for_each_sg(sglist, sg, sgcount, i)
  342. {
  343. totalbytes_towrite += sg->length;
  344. }
  345. totalbytes_towrite += sizeof(u64);
  346. spin_lock_irqsave(&outring_info->ring_lock, flags);
  347. hv_get_ringbuffer_availbytes(outring_info,
  348. &bytes_avail_toread,
  349. &bytes_avail_towrite);
  350. /* If there is only room for the packet, assume it is full. */
  351. /* Otherwise, the next time around, we think the ring buffer */
  352. /* is empty since the read index == write index */
  353. if (bytes_avail_towrite <= totalbytes_towrite) {
  354. spin_unlock_irqrestore(&outring_info->ring_lock, flags);
  355. return -EAGAIN;
  356. }
  357. /* Write to the ring buffer */
  358. next_write_location = hv_get_next_write_location(outring_info);
  359. old_write = next_write_location;
  360. for_each_sg(sglist, sg, sgcount, i)
  361. {
  362. next_write_location = hv_copyto_ringbuffer(outring_info,
  363. next_write_location,
  364. sg_virt(sg),
  365. sg->length);
  366. }
  367. /* Set previous packet start */
  368. prev_indices = hv_get_ring_bufferindices(outring_info);
  369. next_write_location = hv_copyto_ringbuffer(outring_info,
  370. next_write_location,
  371. &prev_indices,
  372. sizeof(u64));
  373. /* Issue a full memory barrier before updating the write index */
  374. smp_mb();
  375. /* Now, update the write location */
  376. hv_set_next_write_location(outring_info, next_write_location);
  377. spin_unlock_irqrestore(&outring_info->ring_lock, flags);
  378. *signal = hv_need_to_signal(old_write, outring_info);
  379. return 0;
  380. }
  381. /*
  382. *
  383. * hv_ringbuffer_peek()
  384. *
  385. * Read without advancing the read index
  386. *
  387. */
  388. int hv_ringbuffer_peek(struct hv_ring_buffer_info *Inring_info,
  389. void *Buffer, u32 buflen)
  390. {
  391. u32 bytes_avail_towrite;
  392. u32 bytes_avail_toread;
  393. u32 next_read_location = 0;
  394. unsigned long flags;
  395. spin_lock_irqsave(&Inring_info->ring_lock, flags);
  396. hv_get_ringbuffer_availbytes(Inring_info,
  397. &bytes_avail_toread,
  398. &bytes_avail_towrite);
  399. /* Make sure there is something to read */
  400. if (bytes_avail_toread < buflen) {
  401. spin_unlock_irqrestore(&Inring_info->ring_lock, flags);
  402. return -EAGAIN;
  403. }
  404. /* Convert to byte offset */
  405. next_read_location = hv_get_next_read_location(Inring_info);
  406. next_read_location = hv_copyfrom_ringbuffer(Inring_info,
  407. Buffer,
  408. buflen,
  409. next_read_location);
  410. spin_unlock_irqrestore(&Inring_info->ring_lock, flags);
  411. return 0;
  412. }
  413. /*
  414. *
  415. * hv_ringbuffer_read()
  416. *
  417. * Read and advance the read index
  418. *
  419. */
  420. int hv_ringbuffer_read(struct hv_ring_buffer_info *inring_info, void *buffer,
  421. u32 buflen, u32 offset, bool *signal)
  422. {
  423. u32 bytes_avail_towrite;
  424. u32 bytes_avail_toread;
  425. u32 next_read_location = 0;
  426. u64 prev_indices = 0;
  427. unsigned long flags;
  428. u32 old_read;
  429. if (buflen <= 0)
  430. return -EINVAL;
  431. spin_lock_irqsave(&inring_info->ring_lock, flags);
  432. hv_get_ringbuffer_availbytes(inring_info,
  433. &bytes_avail_toread,
  434. &bytes_avail_towrite);
  435. old_read = bytes_avail_toread;
  436. /* Make sure there is something to read */
  437. if (bytes_avail_toread < buflen) {
  438. spin_unlock_irqrestore(&inring_info->ring_lock, flags);
  439. return -EAGAIN;
  440. }
  441. next_read_location =
  442. hv_get_next_readlocation_withoffset(inring_info, offset);
  443. next_read_location = hv_copyfrom_ringbuffer(inring_info,
  444. buffer,
  445. buflen,
  446. next_read_location);
  447. next_read_location = hv_copyfrom_ringbuffer(inring_info,
  448. &prev_indices,
  449. sizeof(u64),
  450. next_read_location);
  451. /* Make sure all reads are done before we update the read index since */
  452. /* the writer may start writing to the read area once the read index */
  453. /*is updated */
  454. smp_mb();
  455. /* Update the read index */
  456. hv_set_next_read_location(inring_info, next_read_location);
  457. spin_unlock_irqrestore(&inring_info->ring_lock, flags);
  458. *signal = hv_need_to_signal_on_read(old_read, inring_info);
  459. return 0;
  460. }