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>
24 #include <linux/kernel.h>
28 #include "ring_buffer.h"
34 /* Amount of space to write to */
35 #define BYTES_AVAIL_TO_WRITE(r, w, z) ((w) >= (r)) ? ((z) - ((w) - (r))) : ((r) - (w))
41 GetRingBufferAvailBytes()
44 Get number of bytes available to read and to write to
45 for the specified ring buffer
49 GetRingBufferAvailBytes(struct hv_ring_buffer_info
*rbi
, 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
;
64 GetNextWriteLocation()
67 Get the next write location for the specified ring buffer
71 GetNextWriteLocation(struct hv_ring_buffer_info
*RingInfo
)
73 u32 next
= RingInfo
->ring_buffer
->write_index
;
75 /* ASSERT(next < RingInfo->RingDataSize); */
83 SetNextWriteLocation()
86 Set the next write location for the specified ring buffer
90 SetNextWriteLocation(struct hv_ring_buffer_info
*RingInfo
,
91 u32 NextWriteLocation
)
93 RingInfo
->ring_buffer
->write_index
= NextWriteLocation
;
102 Get the next read location for the specified ring buffer
106 GetNextReadLocation(struct hv_ring_buffer_info
*RingInfo
)
108 u32 next
= RingInfo
->ring_buffer
->read_index
;
110 /* ASSERT(next < RingInfo->RingDataSize); */
118 GetNextReadLocationWithOffset()
121 Get the next read location + offset for the specified ring buffer.
122 This allows the caller to skip
126 GetNextReadLocationWithOffset(struct hv_ring_buffer_info
*RingInfo
, u32 Offset
)
128 u32 next
= RingInfo
->ring_buffer
->read_index
;
130 /* ASSERT(next < RingInfo->RingDataSize); */
132 next
%= RingInfo
->ring_datasize
;
140 SetNextReadLocation()
143 Set the next read location for the specified ring buffer
147 SetNextReadLocation(struct hv_ring_buffer_info
*RingInfo
, u32 NextReadLocation
)
149 RingInfo
->ring_buffer
->read_index
= NextReadLocation
;
159 Get the start of the ring buffer
163 GetRingBuffer(struct hv_ring_buffer_info
*RingInfo
)
165 return (void *)RingInfo
->ring_buffer
->buffer
;
175 Get the size of the ring buffer
179 GetRingBufferSize(struct hv_ring_buffer_info
*RingInfo
)
181 return RingInfo
->ring_datasize
;
187 GetRingBufferIndices()
190 Get the read and write indices as u64 of the specified ring buffer
194 GetRingBufferIndices(struct hv_ring_buffer_info
*RingInfo
)
196 return (u64
)RingInfo
->ring_buffer
->write_index
<< 32;
206 Dump out to console the ring buffer info
209 void DumpRingInfo(struct hv_ring_buffer_info
*RingInfo
, char *Prefix
)
211 u32 bytesAvailToWrite
;
212 u32 bytesAvailToRead
;
214 GetRingBufferAvailBytes(RingInfo
,
220 "%s <<ringinfo %p buffer %p avail write %u "
221 "avail read %u read idx %u write idx %u>>",
224 RingInfo
->ring_buffer
->buffer
,
227 RingInfo
->ring_buffer
->read_index
,
228 RingInfo
->ring_buffer
->write_index
);
232 /* Internal routines */
236 struct hv_ring_buffer_info
*RingInfo
,
237 u32 StartWriteOffset
,
243 struct hv_ring_buffer_info
*RingInfo
,
246 u32 StartReadOffset
);
253 RingBufferGetDebugInfo()
256 Get various debug metrics for the specified ring buffer
259 void RingBufferGetDebugInfo(struct hv_ring_buffer_info
*RingInfo
,
260 struct hv_ring_buffer_debug_info
*debug_info
)
262 u32 bytesAvailToWrite
;
263 u32 bytesAvailToRead
;
265 if (RingInfo
->ring_buffer
) {
266 GetRingBufferAvailBytes(RingInfo
,
270 debug_info
->bytes_avail_toread
= bytesAvailToRead
;
271 debug_info
->bytes_avail_towrite
= bytesAvailToWrite
;
272 debug_info
->current_read_index
=
273 RingInfo
->ring_buffer
->read_index
;
274 debug_info
->current_write_index
=
275 RingInfo
->ring_buffer
->write_index
;
276 debug_info
->current_interrupt_mask
=
277 RingInfo
->ring_buffer
->interrupt_mask
;
285 GetRingBufferInterruptMask()
288 Get the interrupt mask for the specified ring buffer
291 u32
GetRingBufferInterruptMask(struct hv_ring_buffer_info
*rbi
)
293 return rbi
->ring_buffer
->interrupt_mask
;
302 Initialize the ring buffer
305 int RingBufferInit(struct hv_ring_buffer_info
*RingInfo
, void *Buffer
, u32 BufferLen
)
307 if (sizeof(struct hv_ring_buffer
) != PAGE_SIZE
)
310 memset(RingInfo
, 0, sizeof(struct hv_ring_buffer_info
));
312 RingInfo
->ring_buffer
= (struct hv_ring_buffer
*)Buffer
;
313 RingInfo
->ring_buffer
->read_index
=
314 RingInfo
->ring_buffer
->write_index
= 0;
316 RingInfo
->ring_size
= BufferLen
;
317 RingInfo
->ring_datasize
= BufferLen
- sizeof(struct hv_ring_buffer
);
319 spin_lock_init(&RingInfo
->ring_lock
);
330 Cleanup the ring buffer
333 void RingBufferCleanup(struct hv_ring_buffer_info
*RingInfo
)
343 Write to the ring buffer
346 int RingBufferWrite(struct hv_ring_buffer_info
*OutRingInfo
,
347 struct scatterlist
*sglist
, u32 sgcount
)
350 u32 byteAvailToWrite
;
352 u32 totalBytesToWrite
= 0;
354 struct scatterlist
*sg
;
355 volatile u32 nextWriteLocation
;
359 for_each_sg(sglist
, sg
, sgcount
, i
)
361 totalBytesToWrite
+= sg
->length
;
364 totalBytesToWrite
+= sizeof(u64
);
366 spin_lock_irqsave(&OutRingInfo
->ring_lock
, flags
);
368 GetRingBufferAvailBytes(OutRingInfo
,
372 DPRINT_DBG(VMBUS
, "Writing %u bytes...", totalBytesToWrite
);
374 /* DumpRingInfo(OutRingInfo, "BEFORE "); */
376 /* If there is only room for the packet, assume it is full. */
377 /* Otherwise, the next time around, we think the ring buffer */
378 /* is empty since the read index == write index */
379 if (byteAvailToWrite
<= totalBytesToWrite
) {
381 "No more space left on outbound ring buffer "
382 "(needed %u, avail %u)",
386 spin_unlock_irqrestore(&OutRingInfo
->ring_lock
, flags
);
390 /* Write to the ring buffer */
391 nextWriteLocation
= GetNextWriteLocation(OutRingInfo
);
393 for_each_sg(sglist
, sg
, sgcount
, i
)
395 nextWriteLocation
= CopyToRingBuffer(OutRingInfo
,
401 /* Set previous packet start */
402 prevIndices
= GetRingBufferIndices(OutRingInfo
);
404 nextWriteLocation
= CopyToRingBuffer(OutRingInfo
,
409 /* Make sure we flush all writes before updating the writeIndex */
412 /* Now, update the write location */
413 SetNextWriteLocation(OutRingInfo
, nextWriteLocation
);
415 /* DumpRingInfo(OutRingInfo, "AFTER "); */
417 spin_unlock_irqrestore(&OutRingInfo
->ring_lock
, flags
);
428 Read without advancing the read index
431 int RingBufferPeek(struct hv_ring_buffer_info
*InRingInfo
, void *Buffer
, u32 BufferLen
)
433 u32 bytesAvailToWrite
;
434 u32 bytesAvailToRead
;
435 u32 nextReadLocation
= 0;
438 spin_lock_irqsave(&InRingInfo
->ring_lock
, flags
);
440 GetRingBufferAvailBytes(InRingInfo
,
444 /* Make sure there is something to read */
445 if (bytesAvailToRead
< BufferLen
) {
447 "got callback but not enough to read "
448 "<avail to read %d read size %d>!!",
452 spin_unlock_irqrestore(&InRingInfo
->ring_lock
, flags
);
457 /* Convert to byte offset */
458 nextReadLocation
= GetNextReadLocation(InRingInfo
);
460 nextReadLocation
= CopyFromRingBuffer(InRingInfo
,
465 spin_unlock_irqrestore(&InRingInfo
->ring_lock
, flags
);
477 Read and advance the read index
480 int RingBufferRead(struct hv_ring_buffer_info
*InRingInfo
, void *Buffer
,
481 u32 BufferLen
, u32 Offset
)
483 u32 bytesAvailToWrite
;
484 u32 bytesAvailToRead
;
485 u32 nextReadLocation
= 0;
492 spin_lock_irqsave(&InRingInfo
->ring_lock
, flags
);
494 GetRingBufferAvailBytes(InRingInfo
,
498 DPRINT_DBG(VMBUS
, "Reading %u bytes...", BufferLen
);
500 /* DumpRingInfo(InRingInfo, "BEFORE "); */
502 /* Make sure there is something to read */
503 if (bytesAvailToRead
< BufferLen
) {
505 "got callback but not enough to read "
506 "<avail to read %d read size %d>!!",
510 spin_unlock_irqrestore(&InRingInfo
->ring_lock
, flags
);
515 nextReadLocation
= GetNextReadLocationWithOffset(InRingInfo
, Offset
);
517 nextReadLocation
= CopyFromRingBuffer(InRingInfo
,
522 nextReadLocation
= CopyFromRingBuffer(InRingInfo
,
527 /* Make sure all reads are done before we update the read index since */
528 /* the writer may start writing to the read area once the read index */
532 /* Update the read index */
533 SetNextReadLocation(InRingInfo
, nextReadLocation
);
535 /* DumpRingInfo(InRingInfo, "AFTER "); */
537 spin_unlock_irqrestore(&InRingInfo
->ring_lock
, flags
);
549 Helper routine to copy from source to ring buffer.
550 Assume there is enough room. Handles wrap-around in dest case only!!
555 struct hv_ring_buffer_info
*RingInfo
,
556 u32 StartWriteOffset
,
560 void *ringBuffer
= GetRingBuffer(RingInfo
);
561 u32 ringBufferSize
= GetRingBufferSize(RingInfo
);
564 /* wrap-around detected! */
565 if (SrcLen
> ringBufferSize
- StartWriteOffset
) {
566 DPRINT_DBG(VMBUS
, "wrap-around detected!");
568 fragLen
= ringBufferSize
- StartWriteOffset
;
569 memcpy(ringBuffer
+ StartWriteOffset
, Src
, fragLen
);
570 memcpy(ringBuffer
, Src
+ fragLen
, SrcLen
- fragLen
);
572 memcpy(ringBuffer
+ StartWriteOffset
, Src
, SrcLen
);
574 StartWriteOffset
+= SrcLen
;
575 StartWriteOffset
%= ringBufferSize
;
577 return StartWriteOffset
;
587 Helper routine to copy to source from ring buffer.
588 Assume there is enough room. Handles wrap-around in src case only!!
593 struct hv_ring_buffer_info
*RingInfo
,
598 void *ringBuffer
= GetRingBuffer(RingInfo
);
599 u32 ringBufferSize
= GetRingBufferSize(RingInfo
);
603 /* wrap-around detected at the src */
604 if (DestLen
> ringBufferSize
- StartReadOffset
) {
605 DPRINT_DBG(VMBUS
, "src wrap-around detected!");
607 fragLen
= ringBufferSize
- StartReadOffset
;
609 memcpy(Dest
, ringBuffer
+ StartReadOffset
, fragLen
);
610 memcpy(Dest
+ fragLen
, ringBuffer
, DestLen
- fragLen
);
613 memcpy(Dest
, ringBuffer
+ StartReadOffset
, DestLen
);
616 StartReadOffset
+= DestLen
;
617 StartReadOffset
%= ringBufferSize
;
619 return StartReadOffset
;