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 "RingBuffer.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(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(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(RING_BUFFER_INFO *RingInfo, u32 NextWriteLocation)
92 RingInfo->RingBuffer->WriteIndex = NextWriteLocation;
101 Get the next read location for the specified ring buffer
105 GetNextReadLocation(RING_BUFFER_INFO *RingInfo)
107 u32 next = RingInfo->RingBuffer->ReadIndex;
109 ASSERT(next < RingInfo->RingDataSize);
117 GetNextReadLocationWithOffset()
120 Get the next read location + offset for the specified ring buffer.
121 This allows the caller to skip
125 GetNextReadLocationWithOffset(RING_BUFFER_INFO *RingInfo, u32 Offset)
127 u32 next = RingInfo->RingBuffer->ReadIndex;
129 ASSERT(next < RingInfo->RingDataSize);
131 next %= RingInfo->RingDataSize;
139 SetNextReadLocation()
142 Set the next read location for the specified ring buffer
146 SetNextReadLocation(RING_BUFFER_INFO *RingInfo, u32 NextReadLocation)
148 RingInfo->RingBuffer->ReadIndex = NextReadLocation;
158 Get the start of the ring buffer
162 GetRingBuffer(RING_BUFFER_INFO *RingInfo)
164 return (void *)RingInfo->RingBuffer->Buffer;
174 Get the size of the ring buffer
178 GetRingBufferSize(RING_BUFFER_INFO *RingInfo)
180 return RingInfo->RingDataSize;
186 GetRingBufferIndices()
189 Get the read and write indices as u64 of the specified ring buffer
193 GetRingBufferIndices(RING_BUFFER_INFO *RingInfo)
195 return ((u64)RingInfo->RingBuffer->WriteIndex << 32)
196 || RingInfo->RingBuffer->ReadIndex;
206 Dump out to console the ring buffer info
209 void DumpRingInfo(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 RING_BUFFER_INFO *RingInfo,
237 u32 StartWriteOffset,
243 RING_BUFFER_INFO *RingInfo,
246 u32 StartReadOffset);
253 RingBufferGetDebugInfo()
256 Get various debug metrics for the specified ring buffer
259 void RingBufferGetDebugInfo(RING_BUFFER_INFO *RingInfo,
260 RING_BUFFER_DEBUG_INFO *DebugInfo)
262 u32 bytesAvailToWrite;
263 u32 bytesAvailToRead;
265 if (RingInfo->RingBuffer) {
266 GetRingBufferAvailBytes(RingInfo,
270 DebugInfo->BytesAvailToRead = bytesAvailToRead;
271 DebugInfo->BytesAvailToWrite = bytesAvailToWrite;
272 DebugInfo->CurrentReadIndex = RingInfo->RingBuffer->ReadIndex;
273 DebugInfo->CurrentWriteIndex = RingInfo->RingBuffer->WriteIndex;
274 DebugInfo->CurrentInterruptMask = RingInfo->RingBuffer->InterruptMask;
282 GetRingBufferInterruptMask()
285 Get the interrupt mask for the specified ring buffer
288 u32 GetRingBufferInterruptMask(RING_BUFFER_INFO *rbi)
290 return rbi->RingBuffer->InterruptMask;
299 Initialize the ring buffer
302 int RingBufferInit(RING_BUFFER_INFO *RingInfo, void *Buffer, u32 BufferLen)
304 ASSERT(sizeof(RING_BUFFER) == PAGE_SIZE);
306 memset(RingInfo, 0, sizeof(RING_BUFFER_INFO));
308 RingInfo->RingBuffer = (RING_BUFFER *)Buffer;
309 RingInfo->RingBuffer->ReadIndex = RingInfo->RingBuffer->WriteIndex = 0;
311 RingInfo->RingSize = BufferLen;
312 RingInfo->RingDataSize = BufferLen - sizeof(RING_BUFFER);
314 spin_lock_init(&RingInfo->ring_lock);
325 Cleanup the ring buffer
328 void RingBufferCleanup(RING_BUFFER_INFO *RingInfo)
338 Write to the ring buffer
341 int RingBufferWrite(RING_BUFFER_INFO *OutRingInfo,
342 struct scatterlist *sglist, u32 sgcount)
345 u32 byteAvailToWrite;
347 u32 totalBytesToWrite = 0;
349 struct scatterlist *sg;
350 volatile u32 nextWriteLocation;
356 for_each_sg(sglist, sg, sgcount, i)
358 totalBytesToWrite += sg->length;
361 totalBytesToWrite += sizeof(u64);
363 spin_lock_irqsave(&OutRingInfo->ring_lock, flags);
365 GetRingBufferAvailBytes(OutRingInfo,
369 DPRINT_DBG(VMBUS, "Writing %u bytes...", totalBytesToWrite);
371 /* DumpRingInfo(OutRingInfo, "BEFORE "); */
373 /* If there is only room for the packet, assume it is full. */
374 /* Otherwise, the next time around, we think the ring buffer */
375 /* is empty since the read index == write index */
376 if (byteAvailToWrite <= totalBytesToWrite) {
378 "No more space left on outbound ring buffer "
379 "(needed %u, avail %u)",
383 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);
431 Read without advancing the read index
434 int RingBufferPeek(RING_BUFFER_INFO *InRingInfo, void *Buffer, u32 BufferLen)
436 u32 bytesAvailToWrite;
437 u32 bytesAvailToRead;
438 u32 nextReadLocation = 0;
441 spin_lock_irqsave(&InRingInfo->ring_lock, flags);
443 GetRingBufferAvailBytes(InRingInfo,
447 /* Make sure there is something to read */
448 if (bytesAvailToRead < BufferLen) {
450 "got callback but not enough to read "
451 "<avail to read %d read size %d>!!",
455 spin_unlock_irqrestore(&InRingInfo->ring_lock, flags);
460 /* Convert to byte offset */
461 nextReadLocation = GetNextReadLocation(InRingInfo);
463 nextReadLocation = CopyFromRingBuffer(InRingInfo,
468 spin_unlock_irqrestore(&InRingInfo->ring_lock, flags);
480 Read and advance the read index
483 int RingBufferRead(RING_BUFFER_INFO *InRingInfo, void *Buffer,
484 u32 BufferLen, u32 Offset)
486 u32 bytesAvailToWrite;
487 u32 bytesAvailToRead;
488 u32 nextReadLocation = 0;
492 ASSERT(BufferLen > 0);
494 spin_lock_irqsave(&InRingInfo->ring_lock, flags);
496 GetRingBufferAvailBytes(InRingInfo,
500 DPRINT_DBG(VMBUS, "Reading %u bytes...", BufferLen);
502 /* DumpRingInfo(InRingInfo, "BEFORE "); */
504 /* Make sure there is something to read */
505 if (bytesAvailToRead < BufferLen) {
507 "got callback but not enough to read "
508 "<avail to read %d read size %d>!!",
512 spin_unlock_irqrestore(&InRingInfo->ring_lock, flags);
517 nextReadLocation = GetNextReadLocationWithOffset(InRingInfo, Offset);
519 nextReadLocation = CopyFromRingBuffer(InRingInfo,
524 nextReadLocation = CopyFromRingBuffer(InRingInfo,
529 /* Make sure all reads are done before we update the read index since */
530 /* the writer may start writing to the read area once the read index */
534 /* Update the read index */
535 SetNextReadLocation(InRingInfo, nextReadLocation);
537 /* DumpRingInfo(InRingInfo, "AFTER "); */
539 spin_unlock_irqrestore(&InRingInfo->ring_lock, flags);
551 Helper routine to copy from source to ring buffer.
552 Assume there is enough room. Handles wrap-around in dest case only!!
557 RING_BUFFER_INFO *RingInfo,
558 u32 StartWriteOffset,
562 void *ringBuffer = GetRingBuffer(RingInfo);
563 u32 ringBufferSize = GetRingBufferSize(RingInfo);
566 /* wrap-around detected! */
567 if (SrcLen > ringBufferSize - StartWriteOffset) {
568 DPRINT_DBG(VMBUS, "wrap-around detected!");
570 fragLen = ringBufferSize - StartWriteOffset;
571 memcpy(ringBuffer + StartWriteOffset, Src, fragLen);
572 memcpy(ringBuffer, Src + fragLen, SrcLen - fragLen);
574 memcpy(ringBuffer + StartWriteOffset, Src, SrcLen);
576 StartWriteOffset += SrcLen;
577 StartWriteOffset %= ringBufferSize;
579 return StartWriteOffset;
589 Helper routine to copy to source from ring buffer.
590 Assume there is enough room. Handles wrap-around in src case only!!
595 RING_BUFFER_INFO *RingInfo,
600 void *ringBuffer = GetRingBuffer(RingInfo);
601 u32 ringBufferSize = GetRingBufferSize(RingInfo);
605 /* wrap-around detected at the src */
606 if (DestLen > ringBufferSize - StartReadOffset) {
607 DPRINT_DBG(VMBUS, "src wrap-around detected!");
609 fragLen = ringBufferSize - StartReadOffset;
611 memcpy(Dest, ringBuffer + StartReadOffset, fragLen);
612 memcpy(Dest + fragLen, ringBuffer, DestLen - fragLen);
615 memcpy(Dest, ringBuffer + StartReadOffset, DestLen);
618 StartReadOffset += DestLen;
619 StartReadOffset %= ringBufferSize;
621 return StartReadOffset;