Skip to content

Commit

Permalink
---
Browse files Browse the repository at this point in the history
yaml
---
r: 186594
b: refs/heads/master
c: 4408f53
h: refs/heads/master
v: v3
  • Loading branch information
C. Bartlett authored and Greg Kroah-Hartman committed Mar 4, 2010
1 parent 3686780 commit ec64ff2
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 68 deletions.
2 changes: 1 addition & 1 deletion [refs]
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
---
refs/heads/master: a73e6b7c508f610eeb315c443d9ac79041124af0
refs/heads/master: 4408f5319b6c8784120e0784df315b87e8a5dfef
153 changes: 86 additions & 67 deletions trunk/drivers/staging/hv/RingBuffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
static inline void
GetRingBufferAvailBytes(RING_BUFFER_INFO *rbi, u32 *read, u32 *write)
{
u32 read_loc,write_loc;
u32 read_loc, write_loc;

/* Capture the read/write indices before they changed */
read_loc = rbi->RingBuffer->ReadIndex;
Expand All @@ -68,7 +68,7 @@ GetRingBufferAvailBytes(RING_BUFFER_INFO *rbi, u32 *read, u32 *write)
--*/
static inline u32
GetNextWriteLocation(RING_BUFFER_INFO* RingInfo)
GetNextWriteLocation(RING_BUFFER_INFO *RingInfo)
{
u32 next = RingInfo->RingBuffer->WriteIndex;

Expand All @@ -87,7 +87,7 @@ GetNextWriteLocation(RING_BUFFER_INFO* RingInfo)
--*/
static inline void
SetNextWriteLocation(RING_BUFFER_INFO* RingInfo, u32 NextWriteLocation)
SetNextWriteLocation(RING_BUFFER_INFO *RingInfo, u32 NextWriteLocation)
{
RingInfo->RingBuffer->WriteIndex = NextWriteLocation;
}
Expand All @@ -102,7 +102,7 @@ SetNextWriteLocation(RING_BUFFER_INFO* RingInfo, u32 NextWriteLocation)
--*/
static inline u32
GetNextReadLocation(RING_BUFFER_INFO* RingInfo)
GetNextReadLocation(RING_BUFFER_INFO *RingInfo)
{
u32 next = RingInfo->RingBuffer->ReadIndex;

Expand All @@ -122,7 +122,7 @@ GetNextReadLocation(RING_BUFFER_INFO* RingInfo)
--*/
static inline u32
GetNextReadLocationWithOffset(RING_BUFFER_INFO* RingInfo, u32 Offset)
GetNextReadLocationWithOffset(RING_BUFFER_INFO *RingInfo, u32 Offset)
{
u32 next = RingInfo->RingBuffer->ReadIndex;

Expand All @@ -143,7 +143,7 @@ GetNextReadLocationWithOffset(RING_BUFFER_INFO* RingInfo, u32 Offset)
--*/
static inline void
SetNextReadLocation(RING_BUFFER_INFO* RingInfo, u32 NextReadLocation)
SetNextReadLocation(RING_BUFFER_INFO *RingInfo, u32 NextReadLocation)
{
RingInfo->RingBuffer->ReadIndex = NextReadLocation;
}
Expand All @@ -159,7 +159,7 @@ SetNextReadLocation(RING_BUFFER_INFO* RingInfo, u32 NextReadLocation)
--*/
static inline void *
GetRingBuffer(RING_BUFFER_INFO* RingInfo)
GetRingBuffer(RING_BUFFER_INFO *RingInfo)
{
return (void *)RingInfo->RingBuffer->Buffer;
}
Expand All @@ -175,7 +175,7 @@ GetRingBuffer(RING_BUFFER_INFO* RingInfo)
--*/
static inline u32
GetRingBufferSize(RING_BUFFER_INFO* RingInfo)
GetRingBufferSize(RING_BUFFER_INFO *RingInfo)
{
return RingInfo->RingDataSize;
}
Expand All @@ -190,9 +190,10 @@ GetRingBufferSize(RING_BUFFER_INFO* RingInfo)
--*/
static inline u64
GetRingBufferIndices(RING_BUFFER_INFO* RingInfo)
GetRingBufferIndices(RING_BUFFER_INFO *RingInfo)
{
return ((u64)RingInfo->RingBuffer->WriteIndex << 32) || RingInfo->RingBuffer->ReadIndex;
return ((u64)RingInfo->RingBuffer->WriteIndex << 32)
|| RingInfo->RingBuffer->ReadIndex;
}


Expand All @@ -210,9 +211,14 @@ void DumpRingInfo(RING_BUFFER_INFO *RingInfo, char *Prefix)
u32 bytesAvailToWrite;
u32 bytesAvailToRead;

GetRingBufferAvailBytes(RingInfo, &bytesAvailToRead, &bytesAvailToWrite);
GetRingBufferAvailBytes(RingInfo,
&bytesAvailToRead,
&bytesAvailToWrite);

DPRINT(VMBUS, DEBUG_RING_LVL, "%s <<ringinfo %p buffer %p avail write %u avail read %u read idx %u write idx %u>>",
DPRINT(VMBUS,
DEBUG_RING_LVL,
"%s <<ringinfo %p buffer %p avail write %u "
"avail read %u read idx %u write idx %u>>",
Prefix,
RingInfo,
RingInfo->RingBuffer->Buffer,
Expand All @@ -229,13 +235,13 @@ static u32
CopyToRingBuffer(
RING_BUFFER_INFO *RingInfo,
u32 StartWriteOffset,
void * Src,
void *Src,
u32 SrcLen);

static u32
CopyFromRingBuffer(
RING_BUFFER_INFO *RingInfo,
void * Dest,
void *Dest,
u32 DestLen,
u32 StartReadOffset);

Expand All @@ -256,15 +262,15 @@ void RingBufferGetDebugInfo(RING_BUFFER_INFO *RingInfo,
u32 bytesAvailToWrite;
u32 bytesAvailToRead;

if (RingInfo->RingBuffer)
{
GetRingBufferAvailBytes(RingInfo, &bytesAvailToRead, &bytesAvailToWrite);
if (RingInfo->RingBuffer) {
GetRingBufferAvailBytes(RingInfo,
&bytesAvailToRead,
&bytesAvailToWrite);

DebugInfo->BytesAvailToRead = bytesAvailToRead;
DebugInfo->BytesAvailToWrite = bytesAvailToWrite;
DebugInfo->CurrentReadIndex = RingInfo->RingBuffer->ReadIndex;
DebugInfo->CurrentWriteIndex = RingInfo->RingBuffer->WriteIndex;

DebugInfo->CurrentInterruptMask = RingInfo->RingBuffer->InterruptMask;
}
}
Expand Down Expand Up @@ -299,7 +305,7 @@ int RingBufferInit(RING_BUFFER_INFO *RingInfo, void *Buffer, u32 BufferLen)

memset(RingInfo, 0, sizeof(RING_BUFFER_INFO));

RingInfo->RingBuffer = (RING_BUFFER*)Buffer;
RingInfo->RingBuffer = (RING_BUFFER *)Buffer;
RingInfo->RingBuffer->ReadIndex = RingInfo->RingBuffer->WriteIndex = 0;

RingInfo->RingSize = BufferLen;
Expand All @@ -319,7 +325,7 @@ int RingBufferInit(RING_BUFFER_INFO *RingInfo, void *Buffer, u32 BufferLen)
Cleanup the ring buffer
--*/
void RingBufferCleanup(RING_BUFFER_INFO* RingInfo)
void RingBufferCleanup(RING_BUFFER_INFO *RingInfo)
{
}

Expand All @@ -335,14 +341,14 @@ void RingBufferCleanup(RING_BUFFER_INFO* RingInfo)
int RingBufferWrite(RING_BUFFER_INFO *OutRingInfo,
struct scatterlist *sglist, u32 sgcount)
{
int i=0;
int i = 0;
u32 byteAvailToWrite;
u32 byteAvailToRead;
u32 totalBytesToWrite=0;
u32 totalBytesToWrite = 0;

struct scatterlist *sg;
volatile u32 nextWriteLocation;
u64 prevIndices=0;
u64 prevIndices = 0;
unsigned long flags;

DPRINT_ENTER(VMBUS);
Expand All @@ -356,17 +362,23 @@ int RingBufferWrite(RING_BUFFER_INFO *OutRingInfo,

spin_lock_irqsave(&OutRingInfo->ring_lock, flags);

GetRingBufferAvailBytes(OutRingInfo, &byteAvailToRead, &byteAvailToWrite);
GetRingBufferAvailBytes(OutRingInfo,
&byteAvailToRead,
&byteAvailToWrite);

DPRINT_DBG(VMBUS, "Writing %u bytes...", totalBytesToWrite);

/* DumpRingInfo(OutRingInfo, "BEFORE "); */

/* If there is only room for the packet, assume it is full. Otherwise, the next time around, we think the ring buffer */
/* If there is only room for the packet, assume it is full. */
/* Otherwise, the next time around, we think the ring buffer */
/* is empty since the read index == write index */
if (byteAvailToWrite <= totalBytesToWrite)
{
DPRINT_DBG(VMBUS, "No more space left on outbound ring buffer (needed %u, avail %u)", totalBytesToWrite, byteAvailToWrite);
if (byteAvailToWrite <= totalBytesToWrite) {
DPRINT_DBG(VMBUS,
"No more space left on outbound ring buffer "
"(needed %u, avail %u)",
totalBytesToWrite,
byteAvailToWrite);

spin_unlock_irqrestore(&OutRingInfo->ring_lock, flags);

Expand Down Expand Up @@ -423,17 +435,22 @@ int RingBufferPeek(RING_BUFFER_INFO *InRingInfo, void *Buffer, u32 BufferLen)
{
u32 bytesAvailToWrite;
u32 bytesAvailToRead;
u32 nextReadLocation=0;
u32 nextReadLocation = 0;
unsigned long flags;

spin_lock_irqsave(&InRingInfo->ring_lock, flags);

GetRingBufferAvailBytes(InRingInfo, &bytesAvailToRead, &bytesAvailToWrite);
GetRingBufferAvailBytes(InRingInfo,
&bytesAvailToRead,
&bytesAvailToWrite);

/* Make sure there is something to read */
if (bytesAvailToRead < BufferLen )
{
/* DPRINT_DBG(VMBUS, "got callback but not enough to read <avail to read %d read size %d>!!", bytesAvailToRead, BufferLen); */
if (bytesAvailToRead < BufferLen) {
/* DPRINT_DBG(VMBUS,
"got callback but not enough to read "
"<avail to read %d read size %d>!!",
bytesAvailToRead,
BufferLen); */

spin_unlock_irqrestore(&InRingInfo->ring_lock, flags);

Expand All @@ -444,9 +461,9 @@ int RingBufferPeek(RING_BUFFER_INFO *InRingInfo, void *Buffer, u32 BufferLen)
nextReadLocation = GetNextReadLocation(InRingInfo);

nextReadLocation = CopyFromRingBuffer(InRingInfo,
Buffer,
BufferLen,
nextReadLocation);
Buffer,
BufferLen,
nextReadLocation);

spin_unlock_irqrestore(&InRingInfo->ring_lock, flags);

Expand All @@ -468,24 +485,29 @@ int RingBufferRead(RING_BUFFER_INFO *InRingInfo, void *Buffer,
{
u32 bytesAvailToWrite;
u32 bytesAvailToRead;
u32 nextReadLocation=0;
u64 prevIndices=0;
u32 nextReadLocation = 0;
u64 prevIndices = 0;
unsigned long flags;

ASSERT(BufferLen > 0);

spin_lock_irqsave(&InRingInfo->ring_lock, flags);

GetRingBufferAvailBytes(InRingInfo, &bytesAvailToRead, &bytesAvailToWrite);
GetRingBufferAvailBytes(InRingInfo,
&bytesAvailToRead,
&bytesAvailToWrite);

DPRINT_DBG(VMBUS, "Reading %u bytes...", BufferLen);

/* DumpRingInfo(InRingInfo, "BEFORE "); */

/* Make sure there is something to read */
if (bytesAvailToRead < BufferLen )
{
DPRINT_DBG(VMBUS, "got callback but not enough to read <avail to read %d read size %d>!!", bytesAvailToRead, BufferLen);
if (bytesAvailToRead < BufferLen) {
DPRINT_DBG(VMBUS,
"got callback but not enough to read "
"<avail to read %d read size %d>!!",
bytesAvailToRead,
BufferLen);

spin_unlock_irqrestore(&InRingInfo->ring_lock, flags);

Expand All @@ -495,17 +517,18 @@ int RingBufferRead(RING_BUFFER_INFO *InRingInfo, void *Buffer,
nextReadLocation = GetNextReadLocationWithOffset(InRingInfo, Offset);

nextReadLocation = CopyFromRingBuffer(InRingInfo,
Buffer,
BufferLen,
nextReadLocation);
Buffer,
BufferLen,
nextReadLocation);

nextReadLocation = CopyFromRingBuffer(InRingInfo,
&prevIndices,
sizeof(u64),
nextReadLocation);
&prevIndices,
sizeof(u64),
nextReadLocation);

/* Make sure all reads are done before we update the read index since */
/* the writer may start writing to the read area once the read index is updated */
/* the writer may start writing to the read area once the read index */
/*is updated */
mb();

/* Update the read index */
Expand Down Expand Up @@ -533,25 +556,22 @@ static u32
CopyToRingBuffer(
RING_BUFFER_INFO *RingInfo,
u32 StartWriteOffset,
void * Src,
void *Src,
u32 SrcLen)
{
void * ringBuffer=GetRingBuffer(RingInfo);
u32 ringBufferSize=GetRingBufferSize(RingInfo);
void *ringBuffer = GetRingBuffer(RingInfo);
u32 ringBufferSize = GetRingBufferSize(RingInfo);
u32 fragLen;

if (SrcLen > ringBufferSize - StartWriteOffset) /* wrap-around detected! */
{
/* wrap-around detected! */
if (SrcLen > ringBufferSize - StartWriteOffset) {
DPRINT_DBG(VMBUS, "wrap-around detected!");

fragLen = ringBufferSize - StartWriteOffset;
memcpy(ringBuffer + StartWriteOffset, Src, fragLen);
memcpy(ringBuffer, Src + fragLen, SrcLen - fragLen);
}
else
{
} else
memcpy(ringBuffer + StartWriteOffset, Src, SrcLen);
}

StartWriteOffset += SrcLen;
StartWriteOffset %= ringBufferSize;
Expand All @@ -573,28 +593,27 @@ CopyToRingBuffer(
static u32
CopyFromRingBuffer(
RING_BUFFER_INFO *RingInfo,
void * Dest,
void *Dest,
u32 DestLen,
u32 StartReadOffset)
{
void * ringBuffer=GetRingBuffer(RingInfo);
u32 ringBufferSize=GetRingBufferSize(RingInfo);
void *ringBuffer = GetRingBuffer(RingInfo);
u32 ringBufferSize = GetRingBufferSize(RingInfo);

u32 fragLen;

if (DestLen > ringBufferSize - StartReadOffset) /* wrap-around detected at the src */
{
/* wrap-around detected at the src */
if (DestLen > ringBufferSize - StartReadOffset) {
DPRINT_DBG(VMBUS, "src wrap-around detected!");

fragLen = ringBufferSize - StartReadOffset;

memcpy(Dest, ringBuffer + StartReadOffset, fragLen);
memcpy(Dest + fragLen, ringBuffer, DestLen - fragLen);
}
else
{
} else

memcpy(Dest, ringBuffer + StartReadOffset, DestLen);
}


StartReadOffset += DestLen;
StartReadOffset %= ringBufferSize;
Expand Down

0 comments on commit ec64ff2

Please sign in to comment.