3 * Copyright (c) 2009, Microsoft Corporation.
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.
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
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.
19 * Haiyang Zhang <haiyangz@microsoft.com>
20 * Hank Janssen <hjanssen@microsoft.com>
21 * K. Y. Srinivasan <kys@microsoft.com>
24 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
26 #include <linux/kernel.h>
30 #include "hyperv_vmbus.h"
36 /* Amount of space to write to */
37 #define BYTES_AVAIL_TO_WRITE(r, w, z) ((w) >= (r)) ? ((z) - ((w) - (r))) : ((r) - (w))
42 * hv_get_ringbuffer_availbytes()
44 * Get number of bytes available to read and to write to
45 * for the specified ring buffer
48 hv_get_ringbuffer_availbytes(struct hv_ring_buffer_info
*rbi
,
49 u32
*read
, u32
*write
)
51 u32 read_loc
, write_loc
;
53 /* Capture the read/write indices before they changed */
54 read_loc
= rbi
->ring_buffer
->read_index
;
55 write_loc
= rbi
->ring_buffer
->write_index
;
57 *write
= BYTES_AVAIL_TO_WRITE(read_loc
, write_loc
, rbi
->ring_datasize
);
58 *read
= rbi
->ring_datasize
- *write
;
62 * hv_get_next_write_location()
64 * Get the next write location for the specified ring buffer
68 hv_get_next_write_location(struct hv_ring_buffer_info
*ring_info
)
70 u32 next
= ring_info
->ring_buffer
->write_index
;
76 * hv_set_next_write_location()
78 * Set the next write location for the specified ring buffer
82 hv_set_next_write_location(struct hv_ring_buffer_info
*ring_info
,
83 u32 next_write_location
)
85 ring_info
->ring_buffer
->write_index
= next_write_location
;
89 * hv_get_next_read_location()
91 * Get the next read location for the specified ring buffer
94 hv_get_next_read_location(struct hv_ring_buffer_info
*ring_info
)
96 u32 next
= ring_info
->ring_buffer
->read_index
;
102 * hv_get_next_readlocation_withoffset()
104 * Get the next read location + offset for the specified ring buffer.
105 * This allows the caller to skip
108 hv_get_next_readlocation_withoffset(struct hv_ring_buffer_info
*ring_info
,
111 u32 next
= ring_info
->ring_buffer
->read_index
;
114 next
%= ring_info
->ring_datasize
;
121 * hv_set_next_read_location()
123 * Set the next read location for the specified ring buffer
127 hv_set_next_read_location(struct hv_ring_buffer_info
*ring_info
,
128 u32 next_read_location
)
130 ring_info
->ring_buffer
->read_index
= next_read_location
;
136 * hv_get_ring_buffer()
138 * Get the start of the ring buffer
141 hv_get_ring_buffer(struct hv_ring_buffer_info
*ring_info
)
143 return (void *)ring_info
->ring_buffer
->buffer
;
149 * hv_get_ring_buffersize()
151 * Get the size of the ring buffer
154 hv_get_ring_buffersize(struct hv_ring_buffer_info
*ring_info
)
156 return ring_info
->ring_datasize
;
161 * hv_get_ring_bufferindices()
163 * Get the read and write indices as u64 of the specified ring buffer
167 hv_get_ring_bufferindices(struct hv_ring_buffer_info
*ring_info
)
169 return (u64
)ring_info
->ring_buffer
->write_index
<< 32;
175 * hv_dump_ring_info()
177 * Dump out to console the ring buffer info
180 void hv_dump_ring_info(struct hv_ring_buffer_info
*ring_info
, char *prefix
)
182 u32 bytes_avail_towrite
;
183 u32 bytes_avail_toread
;
185 hv_get_ringbuffer_availbytes(ring_info
,
187 &bytes_avail_towrite
);
191 "%s <<ringinfo %p buffer %p avail write %u "
192 "avail read %u read idx %u write idx %u>>",
195 ring_info
->ring_buffer
->buffer
,
198 ring_info
->ring_buffer
->read_index
,
199 ring_info
->ring_buffer
->write_index
);
205 * hv_copyfrom_ringbuffer()
207 * Helper routine to copy to source from ring buffer.
208 * Assume there is enough room. Handles wrap-around in src case only!!
211 static u32
hv_copyfrom_ringbuffer(
212 struct hv_ring_buffer_info
*ring_info
,
215 u32 start_read_offset
)
217 void *ring_buffer
= hv_get_ring_buffer(ring_info
);
218 u32 ring_buffer_size
= hv_get_ring_buffersize(ring_info
);
222 /* wrap-around detected at the src */
223 if (destlen
> ring_buffer_size
- start_read_offset
) {
224 frag_len
= ring_buffer_size
- start_read_offset
;
226 memcpy(dest
, ring_buffer
+ start_read_offset
, frag_len
);
227 memcpy(dest
+ frag_len
, ring_buffer
, destlen
- frag_len
);
230 memcpy(dest
, ring_buffer
+ start_read_offset
, destlen
);
233 start_read_offset
+= destlen
;
234 start_read_offset
%= ring_buffer_size
;
236 return start_read_offset
;
242 * hv_copyto_ringbuffer()
244 * Helper routine to copy from source to ring buffer.
245 * Assume there is enough room. Handles wrap-around in dest case only!!
248 static u32
hv_copyto_ringbuffer(
249 struct hv_ring_buffer_info
*ring_info
,
250 u32 start_write_offset
,
254 void *ring_buffer
= hv_get_ring_buffer(ring_info
);
255 u32 ring_buffer_size
= hv_get_ring_buffersize(ring_info
);
258 /* wrap-around detected! */
259 if (srclen
> ring_buffer_size
- start_write_offset
) {
260 frag_len
= ring_buffer_size
- start_write_offset
;
261 memcpy(ring_buffer
+ start_write_offset
, src
, frag_len
);
262 memcpy(ring_buffer
, src
+ frag_len
, srclen
- frag_len
);
264 memcpy(ring_buffer
+ start_write_offset
, src
, srclen
);
266 start_write_offset
+= srclen
;
267 start_write_offset
%= ring_buffer_size
;
269 return start_write_offset
;
274 * hv_ringbuffer_get_debuginfo()
276 * Get various debug metrics for the specified ring buffer
279 void hv_ringbuffer_get_debuginfo(struct hv_ring_buffer_info
*ring_info
,
280 struct hv_ring_buffer_debug_info
*debug_info
)
282 u32 bytes_avail_towrite
;
283 u32 bytes_avail_toread
;
285 if (ring_info
->ring_buffer
) {
286 hv_get_ringbuffer_availbytes(ring_info
,
288 &bytes_avail_towrite
);
290 debug_info
->bytes_avail_toread
= bytes_avail_toread
;
291 debug_info
->bytes_avail_towrite
= bytes_avail_towrite
;
292 debug_info
->current_read_index
=
293 ring_info
->ring_buffer
->read_index
;
294 debug_info
->current_write_index
=
295 ring_info
->ring_buffer
->write_index
;
296 debug_info
->current_interrupt_mask
=
297 ring_info
->ring_buffer
->interrupt_mask
;
304 * hv_get_ringbuffer_interrupt_mask()
306 * Get the interrupt mask for the specified ring buffer
309 u32
hv_get_ringbuffer_interrupt_mask(struct hv_ring_buffer_info
*rbi
)
311 return rbi
->ring_buffer
->interrupt_mask
;
316 * hv_ringbuffer_init()
318 *Initialize the ring buffer
321 int hv_ringbuffer_init(struct hv_ring_buffer_info
*ring_info
,
322 void *buffer
, u32 buflen
)
324 if (sizeof(struct hv_ring_buffer
) != PAGE_SIZE
)
327 memset(ring_info
, 0, sizeof(struct hv_ring_buffer_info
));
329 ring_info
->ring_buffer
= (struct hv_ring_buffer
*)buffer
;
330 ring_info
->ring_buffer
->read_index
=
331 ring_info
->ring_buffer
->write_index
= 0;
333 ring_info
->ring_size
= buflen
;
334 ring_info
->ring_datasize
= buflen
- sizeof(struct hv_ring_buffer
);
336 spin_lock_init(&ring_info
->ring_lock
);
343 * hv_ringbuffer_cleanup()
345 * Cleanup the ring buffer
348 void hv_ringbuffer_cleanup(struct hv_ring_buffer_info
*ring_info
)
354 * hv_ringbuffer_write()
356 * Write to the ring buffer
359 int hv_ringbuffer_write(struct hv_ring_buffer_info
*outring_info
,
360 struct scatterlist
*sglist
, u32 sgcount
)
363 u32 bytes_avail_towrite
;
364 u32 bytes_avail_toread
;
365 u32 totalbytes_towrite
= 0;
367 struct scatterlist
*sg
;
368 u32 next_write_location
;
369 u64 prev_indices
= 0;
372 for_each_sg(sglist
, sg
, sgcount
, i
)
374 totalbytes_towrite
+= sg
->length
;
377 totalbytes_towrite
+= sizeof(u64
);
379 spin_lock_irqsave(&outring_info
->ring_lock
, flags
);
381 hv_get_ringbuffer_availbytes(outring_info
,
383 &bytes_avail_towrite
);
386 /* If there is only room for the packet, assume it is full. */
387 /* Otherwise, the next time around, we think the ring buffer */
388 /* is empty since the read index == write index */
389 if (bytes_avail_towrite
<= totalbytes_towrite
) {
390 spin_unlock_irqrestore(&outring_info
->ring_lock
, flags
);
394 /* Write to the ring buffer */
395 next_write_location
= hv_get_next_write_location(outring_info
);
397 for_each_sg(sglist
, sg
, sgcount
, i
)
399 next_write_location
= hv_copyto_ringbuffer(outring_info
,
405 /* Set previous packet start */
406 prev_indices
= hv_get_ring_bufferindices(outring_info
);
408 next_write_location
= hv_copyto_ringbuffer(outring_info
,
413 /* Make sure we flush all writes before updating the writeIndex */
416 /* Now, update the write location */
417 hv_set_next_write_location(outring_info
, next_write_location
);
420 spin_unlock_irqrestore(&outring_info
->ring_lock
, flags
);
427 * hv_ringbuffer_peek()
429 * Read without advancing the read index
432 int hv_ringbuffer_peek(struct hv_ring_buffer_info
*Inring_info
,
433 void *Buffer
, u32 buflen
)
435 u32 bytes_avail_towrite
;
436 u32 bytes_avail_toread
;
437 u32 next_read_location
= 0;
440 spin_lock_irqsave(&Inring_info
->ring_lock
, flags
);
442 hv_get_ringbuffer_availbytes(Inring_info
,
444 &bytes_avail_towrite
);
446 /* Make sure there is something to read */
447 if (bytes_avail_toread
< buflen
) {
449 spin_unlock_irqrestore(&Inring_info
->ring_lock
, flags
);
454 /* Convert to byte offset */
455 next_read_location
= hv_get_next_read_location(Inring_info
);
457 next_read_location
= hv_copyfrom_ringbuffer(Inring_info
,
462 spin_unlock_irqrestore(&Inring_info
->ring_lock
, flags
);
470 * hv_ringbuffer_read()
472 * Read and advance the read index
475 int hv_ringbuffer_read(struct hv_ring_buffer_info
*inring_info
, void *buffer
,
476 u32 buflen
, u32 offset
)
478 u32 bytes_avail_towrite
;
479 u32 bytes_avail_toread
;
480 u32 next_read_location
= 0;
481 u64 prev_indices
= 0;
487 spin_lock_irqsave(&inring_info
->ring_lock
, flags
);
489 hv_get_ringbuffer_availbytes(inring_info
,
491 &bytes_avail_towrite
);
493 /* Make sure there is something to read */
494 if (bytes_avail_toread
< buflen
) {
495 spin_unlock_irqrestore(&inring_info
->ring_lock
, flags
);
501 hv_get_next_readlocation_withoffset(inring_info
, offset
);
503 next_read_location
= hv_copyfrom_ringbuffer(inring_info
,
508 next_read_location
= hv_copyfrom_ringbuffer(inring_info
,
513 /* Make sure all reads are done before we update the read index since */
514 /* the writer may start writing to the read area once the read index */
518 /* Update the read index */
519 hv_set_next_read_location(inring_info
, next_read_location
);
521 spin_unlock_irqrestore(&inring_info
->ring_lock
, flags
);