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
->RingBuffer
->ReadIndex
;
55 write_loc
= rbi
->RingBuffer
->WriteIndex
;
57 *write
= BYTES_AVAIL_TO_WRITE(read_loc
, write_loc
, rbi
->RingDataSize
);
58 *read
= rbi
->RingDataSize
- *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
->RingBuffer
->WriteIndex
;
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
->RingBuffer
->WriteIndex
= NextWriteLocation
;
102 Get the next read location for the specified ring buffer
106 GetNextReadLocation(struct hv_ring_buffer_info
*RingInfo
)
108 u32 next
= RingInfo
->RingBuffer
->ReadIndex
;
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
->RingBuffer
->ReadIndex
;
130 /* ASSERT(next < RingInfo->RingDataSize); */
132 next
%= RingInfo
->RingDataSize
;
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
->RingBuffer
->ReadIndex
= NextReadLocation
;
159 Get the start of the ring buffer
163 GetRingBuffer(struct hv_ring_buffer_info
*RingInfo
)
165 return (void *)RingInfo
->RingBuffer
->Buffer
;
175 Get the size of the ring buffer
179 GetRingBufferSize(struct hv_ring_buffer_info
*RingInfo
)
181 return RingInfo
->RingDataSize
;
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
->RingBuffer
->WriteIndex
<< 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
->RingBuffer
->Buffer
,
227 RingInfo
->RingBuffer
->ReadIndex
,
228 RingInfo
->RingBuffer
->WriteIndex
);
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
->RingBuffer
) {
266 GetRingBufferAvailBytes(RingInfo
,
270 debug_info
->BytesAvailToRead
= bytesAvailToRead
;
271 debug_info
->BytesAvailToWrite
= bytesAvailToWrite
;
272 debug_info
->CurrentReadIndex
= RingInfo
->RingBuffer
->ReadIndex
;
273 debug_info
->CurrentWriteIndex
= RingInfo
->RingBuffer
->WriteIndex
;
274 debug_info
->CurrentInterruptMask
= RingInfo
->RingBuffer
->InterruptMask
;
282 GetRingBufferInterruptMask()
285 Get the interrupt mask for the specified ring buffer
288 u32
GetRingBufferInterruptMask(struct hv_ring_buffer_info
*rbi
)
290 return rbi
->RingBuffer
->InterruptMask
;
299 Initialize the ring buffer
302 int RingBufferInit(struct hv_ring_buffer_info
*RingInfo
, void *Buffer
, u32 BufferLen
)
304 if (sizeof(struct hv_ring_buffer
) != PAGE_SIZE
)
307 memset(RingInfo
, 0, sizeof(struct hv_ring_buffer_info
));
309 RingInfo
->RingBuffer
= (struct hv_ring_buffer
*)Buffer
;
310 RingInfo
->RingBuffer
->ReadIndex
= RingInfo
->RingBuffer
->WriteIndex
= 0;
312 RingInfo
->RingSize
= BufferLen
;
313 RingInfo
->RingDataSize
= BufferLen
- sizeof(struct hv_ring_buffer
);
315 spin_lock_init(&RingInfo
->ring_lock
);
326 Cleanup the ring buffer
329 void RingBufferCleanup(struct hv_ring_buffer_info
*RingInfo
)
339 Write to the ring buffer
342 int RingBufferWrite(struct hv_ring_buffer_info
*OutRingInfo
,
343 struct scatterlist
*sglist
, u32 sgcount
)
346 u32 byteAvailToWrite
;
348 u32 totalBytesToWrite
= 0;
350 struct scatterlist
*sg
;
351 volatile u32 nextWriteLocation
;
355 for_each_sg(sglist
, sg
, sgcount
, i
)
357 totalBytesToWrite
+= sg
->length
;
360 totalBytesToWrite
+= sizeof(u64
);
362 spin_lock_irqsave(&OutRingInfo
->ring_lock
, flags
);
364 GetRingBufferAvailBytes(OutRingInfo
,
368 DPRINT_DBG(VMBUS
, "Writing %u bytes...", totalBytesToWrite
);
370 /* DumpRingInfo(OutRingInfo, "BEFORE "); */
372 /* If there is only room for the packet, assume it is full. */
373 /* Otherwise, the next time around, we think the ring buffer */
374 /* is empty since the read index == write index */
375 if (byteAvailToWrite
<= totalBytesToWrite
) {
377 "No more space left on outbound ring buffer "
378 "(needed %u, avail %u)",
382 spin_unlock_irqrestore(&OutRingInfo
->ring_lock
, flags
);
386 /* Write to the ring buffer */
387 nextWriteLocation
= GetNextWriteLocation(OutRingInfo
);
389 for_each_sg(sglist
, sg
, sgcount
, i
)
391 nextWriteLocation
= CopyToRingBuffer(OutRingInfo
,
397 /* Set previous packet start */
398 prevIndices
= GetRingBufferIndices(OutRingInfo
);
400 nextWriteLocation
= CopyToRingBuffer(OutRingInfo
,
405 /* Make sure we flush all writes before updating the writeIndex */
408 /* Now, update the write location */
409 SetNextWriteLocation(OutRingInfo
, nextWriteLocation
);
411 /* DumpRingInfo(OutRingInfo, "AFTER "); */
413 spin_unlock_irqrestore(&OutRingInfo
->ring_lock
, flags
);
424 Read without advancing the read index
427 int RingBufferPeek(struct hv_ring_buffer_info
*InRingInfo
, void *Buffer
, u32 BufferLen
)
429 u32 bytesAvailToWrite
;
430 u32 bytesAvailToRead
;
431 u32 nextReadLocation
= 0;
434 spin_lock_irqsave(&InRingInfo
->ring_lock
, flags
);
436 GetRingBufferAvailBytes(InRingInfo
,
440 /* Make sure there is something to read */
441 if (bytesAvailToRead
< BufferLen
) {
443 "got callback but not enough to read "
444 "<avail to read %d read size %d>!!",
448 spin_unlock_irqrestore(&InRingInfo
->ring_lock
, flags
);
453 /* Convert to byte offset */
454 nextReadLocation
= GetNextReadLocation(InRingInfo
);
456 nextReadLocation
= CopyFromRingBuffer(InRingInfo
,
461 spin_unlock_irqrestore(&InRingInfo
->ring_lock
, flags
);
473 Read and advance the read index
476 int RingBufferRead(struct hv_ring_buffer_info
*InRingInfo
, void *Buffer
,
477 u32 BufferLen
, u32 Offset
)
479 u32 bytesAvailToWrite
;
480 u32 bytesAvailToRead
;
481 u32 nextReadLocation
= 0;
488 spin_lock_irqsave(&InRingInfo
->ring_lock
, flags
);
490 GetRingBufferAvailBytes(InRingInfo
,
494 DPRINT_DBG(VMBUS
, "Reading %u bytes...", BufferLen
);
496 /* DumpRingInfo(InRingInfo, "BEFORE "); */
498 /* Make sure there is something to read */
499 if (bytesAvailToRead
< BufferLen
) {
501 "got callback but not enough to read "
502 "<avail to read %d read size %d>!!",
506 spin_unlock_irqrestore(&InRingInfo
->ring_lock
, flags
);
511 nextReadLocation
= GetNextReadLocationWithOffset(InRingInfo
, Offset
);
513 nextReadLocation
= CopyFromRingBuffer(InRingInfo
,
518 nextReadLocation
= CopyFromRingBuffer(InRingInfo
,
523 /* Make sure all reads are done before we update the read index since */
524 /* the writer may start writing to the read area once the read index */
528 /* Update the read index */
529 SetNextReadLocation(InRingInfo
, nextReadLocation
);
531 /* DumpRingInfo(InRingInfo, "AFTER "); */
533 spin_unlock_irqrestore(&InRingInfo
->ring_lock
, flags
);
545 Helper routine to copy from source to ring buffer.
546 Assume there is enough room. Handles wrap-around in dest case only!!
551 struct hv_ring_buffer_info
*RingInfo
,
552 u32 StartWriteOffset
,
556 void *ringBuffer
= GetRingBuffer(RingInfo
);
557 u32 ringBufferSize
= GetRingBufferSize(RingInfo
);
560 /* wrap-around detected! */
561 if (SrcLen
> ringBufferSize
- StartWriteOffset
) {
562 DPRINT_DBG(VMBUS
, "wrap-around detected!");
564 fragLen
= ringBufferSize
- StartWriteOffset
;
565 memcpy(ringBuffer
+ StartWriteOffset
, Src
, fragLen
);
566 memcpy(ringBuffer
, Src
+ fragLen
, SrcLen
- fragLen
);
568 memcpy(ringBuffer
+ StartWriteOffset
, Src
, SrcLen
);
570 StartWriteOffset
+= SrcLen
;
571 StartWriteOffset
%= ringBufferSize
;
573 return StartWriteOffset
;
583 Helper routine to copy to source from ring buffer.
584 Assume there is enough room. Handles wrap-around in src case only!!
589 struct hv_ring_buffer_info
*RingInfo
,
594 void *ringBuffer
= GetRingBuffer(RingInfo
);
595 u32 ringBufferSize
= GetRingBufferSize(RingInfo
);
599 /* wrap-around detected at the src */
600 if (DestLen
> ringBufferSize
- StartReadOffset
) {
601 DPRINT_DBG(VMBUS
, "src wrap-around detected!");
603 fragLen
= ringBufferSize
- StartReadOffset
;
605 memcpy(Dest
, ringBuffer
+ StartReadOffset
, fragLen
);
606 memcpy(Dest
+ fragLen
, ringBuffer
, DestLen
- fragLen
);
609 memcpy(Dest
, ringBuffer
+ StartReadOffset
, DestLen
);
612 StartReadOffset
+= DestLen
;
613 StartReadOffset
%= ringBufferSize
;
615 return StartReadOffset
;