GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / staging / hv / ring_buffer.c
blobd78c569ac94a2aa7ae4ba6b2325dbf6d2b93c45c
1 /*
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
12 * more details.
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.
18 * Authors:
19 * Haiyang Zhang <haiyangz@microsoft.com>
20 * Hank Janssen <hjanssen@microsoft.com>
24 #include <linux/kernel.h>
25 #include <linux/mm.h>
26 #include "osd.h"
27 #include "logging.h"
28 #include "ring_buffer.h"
31 /* #defines */
34 /* Amount of space to write to */
35 #define BYTES_AVAIL_TO_WRITE(r, w, z) ((w) >= (r)) ? ((z) - ((w) - (r))) : ((r) - (w))
38 /*++
40 Name:
41 GetRingBufferAvailBytes()
43 Description:
44 Get number of bytes available to read and to write to
45 for the specified ring buffer
47 --*/
48 static inline void
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;
61 /*++
63 Name:
64 GetNextWriteLocation()
66 Description:
67 Get the next write location for the specified ring buffer
69 --*/
70 static inline u32
71 GetNextWriteLocation(struct hv_ring_buffer_info *RingInfo)
73 u32 next = RingInfo->RingBuffer->WriteIndex;
75 /* ASSERT(next < RingInfo->RingDataSize); */
77 return next;
80 /*++
82 Name:
83 SetNextWriteLocation()
85 Description:
86 Set the next write location for the specified ring buffer
88 --*/
89 static inline void
90 SetNextWriteLocation(struct hv_ring_buffer_info *RingInfo,
91 u32 NextWriteLocation)
93 RingInfo->RingBuffer->WriteIndex = NextWriteLocation;
96 /*++
98 Name:
99 GetNextReadLocation()
101 Description:
102 Get the next read location for the specified ring buffer
104 --*/
105 static inline u32
106 GetNextReadLocation(struct hv_ring_buffer_info *RingInfo)
108 u32 next = RingInfo->RingBuffer->ReadIndex;
110 /* ASSERT(next < RingInfo->RingDataSize); */
112 return next;
115 /*++
117 Name:
118 GetNextReadLocationWithOffset()
120 Description:
121 Get the next read location + offset for the specified ring buffer.
122 This allows the caller to skip
124 --*/
125 static inline u32
126 GetNextReadLocationWithOffset(struct hv_ring_buffer_info *RingInfo, u32 Offset)
128 u32 next = RingInfo->RingBuffer->ReadIndex;
130 /* ASSERT(next < RingInfo->RingDataSize); */
131 next += Offset;
132 next %= RingInfo->RingDataSize;
134 return next;
137 /*++
139 Name:
140 SetNextReadLocation()
142 Description:
143 Set the next read location for the specified ring buffer
145 --*/
146 static inline void
147 SetNextReadLocation(struct hv_ring_buffer_info *RingInfo, u32 NextReadLocation)
149 RingInfo->RingBuffer->ReadIndex = NextReadLocation;
153 /*++
155 Name:
156 GetRingBuffer()
158 Description:
159 Get the start of the ring buffer
161 --*/
162 static inline void *
163 GetRingBuffer(struct hv_ring_buffer_info *RingInfo)
165 return (void *)RingInfo->RingBuffer->Buffer;
169 /*++
171 Name:
172 GetRingBufferSize()
174 Description:
175 Get the size of the ring buffer
177 --*/
178 static inline u32
179 GetRingBufferSize(struct hv_ring_buffer_info *RingInfo)
181 return RingInfo->RingDataSize;
184 /*++
186 Name:
187 GetRingBufferIndices()
189 Description:
190 Get the read and write indices as u64 of the specified ring buffer
192 --*/
193 static inline u64
194 GetRingBufferIndices(struct hv_ring_buffer_info *RingInfo)
196 return (u64)RingInfo->RingBuffer->WriteIndex << 32;
200 /*++
202 Name:
203 DumpRingInfo()
205 Description:
206 Dump out to console the ring buffer info
208 --*/
209 void DumpRingInfo(struct hv_ring_buffer_info *RingInfo, char *Prefix)
211 u32 bytesAvailToWrite;
212 u32 bytesAvailToRead;
214 GetRingBufferAvailBytes(RingInfo,
215 &bytesAvailToRead,
216 &bytesAvailToWrite);
218 DPRINT(VMBUS,
219 DEBUG_RING_LVL,
220 "%s <<ringinfo %p buffer %p avail write %u "
221 "avail read %u read idx %u write idx %u>>",
222 Prefix,
223 RingInfo,
224 RingInfo->RingBuffer->Buffer,
225 bytesAvailToWrite,
226 bytesAvailToRead,
227 RingInfo->RingBuffer->ReadIndex,
228 RingInfo->RingBuffer->WriteIndex);
232 /* Internal routines */
234 static u32
235 CopyToRingBuffer(
236 struct hv_ring_buffer_info *RingInfo,
237 u32 StartWriteOffset,
238 void *Src,
239 u32 SrcLen);
241 static u32
242 CopyFromRingBuffer(
243 struct hv_ring_buffer_info *RingInfo,
244 void *Dest,
245 u32 DestLen,
246 u32 StartReadOffset);
250 /*++
252 Name:
253 RingBufferGetDebugInfo()
255 Description:
256 Get various debug metrics for the specified ring buffer
258 --*/
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,
267 &bytesAvailToRead,
268 &bytesAvailToWrite);
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;
279 /*++
281 Name:
282 GetRingBufferInterruptMask()
284 Description:
285 Get the interrupt mask for the specified ring buffer
287 --*/
288 u32 GetRingBufferInterruptMask(struct hv_ring_buffer_info *rbi)
290 return rbi->RingBuffer->InterruptMask;
293 /*++
295 Name:
296 RingBufferInit()
298 Description:
299 Initialize the ring buffer
301 --*/
302 int RingBufferInit(struct hv_ring_buffer_info *RingInfo, void *Buffer, u32 BufferLen)
304 if (sizeof(struct hv_ring_buffer) != PAGE_SIZE)
305 return -EINVAL;
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);
317 return 0;
320 /*++
322 Name:
323 RingBufferCleanup()
325 Description:
326 Cleanup the ring buffer
328 --*/
329 void RingBufferCleanup(struct hv_ring_buffer_info *RingInfo)
333 /*++
335 Name:
336 RingBufferWrite()
338 Description:
339 Write to the ring buffer
341 --*/
342 int RingBufferWrite(struct hv_ring_buffer_info *OutRingInfo,
343 struct scatterlist *sglist, u32 sgcount)
345 int i = 0;
346 u32 byteAvailToWrite;
347 u32 byteAvailToRead;
348 u32 totalBytesToWrite = 0;
350 struct scatterlist *sg;
351 volatile u32 nextWriteLocation;
352 u64 prevIndices = 0;
353 unsigned long flags;
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,
365 &byteAvailToRead,
366 &byteAvailToWrite);
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) {
376 DPRINT_DBG(VMBUS,
377 "No more space left on outbound ring buffer "
378 "(needed %u, avail %u)",
379 totalBytesToWrite,
380 byteAvailToWrite);
382 spin_unlock_irqrestore(&OutRingInfo->ring_lock, flags);
383 return -1;
386 /* Write to the ring buffer */
387 nextWriteLocation = GetNextWriteLocation(OutRingInfo);
389 for_each_sg(sglist, sg, sgcount, i)
391 nextWriteLocation = CopyToRingBuffer(OutRingInfo,
392 nextWriteLocation,
393 sg_virt(sg),
394 sg->length);
397 /* Set previous packet start */
398 prevIndices = GetRingBufferIndices(OutRingInfo);
400 nextWriteLocation = CopyToRingBuffer(OutRingInfo,
401 nextWriteLocation,
402 &prevIndices,
403 sizeof(u64));
405 /* Make sure we flush all writes before updating the writeIndex */
406 mb();
408 /* Now, update the write location */
409 SetNextWriteLocation(OutRingInfo, nextWriteLocation);
411 /* DumpRingInfo(OutRingInfo, "AFTER "); */
413 spin_unlock_irqrestore(&OutRingInfo->ring_lock, flags);
414 return 0;
418 /*++
420 Name:
421 RingBufferPeek()
423 Description:
424 Read without advancing the read index
426 --*/
427 int RingBufferPeek(struct hv_ring_buffer_info *InRingInfo, void *Buffer, u32 BufferLen)
429 u32 bytesAvailToWrite;
430 u32 bytesAvailToRead;
431 u32 nextReadLocation = 0;
432 unsigned long flags;
434 spin_lock_irqsave(&InRingInfo->ring_lock, flags);
436 GetRingBufferAvailBytes(InRingInfo,
437 &bytesAvailToRead,
438 &bytesAvailToWrite);
440 /* Make sure there is something to read */
441 if (bytesAvailToRead < BufferLen) {
442 /* DPRINT_DBG(VMBUS,
443 "got callback but not enough to read "
444 "<avail to read %d read size %d>!!",
445 bytesAvailToRead,
446 BufferLen); */
448 spin_unlock_irqrestore(&InRingInfo->ring_lock, flags);
450 return -1;
453 /* Convert to byte offset */
454 nextReadLocation = GetNextReadLocation(InRingInfo);
456 nextReadLocation = CopyFromRingBuffer(InRingInfo,
457 Buffer,
458 BufferLen,
459 nextReadLocation);
461 spin_unlock_irqrestore(&InRingInfo->ring_lock, flags);
463 return 0;
467 /*++
469 Name:
470 RingBufferRead()
472 Description:
473 Read and advance the read index
475 --*/
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;
482 u64 prevIndices = 0;
483 unsigned long flags;
485 if (BufferLen <= 0)
486 return -EINVAL;
488 spin_lock_irqsave(&InRingInfo->ring_lock, flags);
490 GetRingBufferAvailBytes(InRingInfo,
491 &bytesAvailToRead,
492 &bytesAvailToWrite);
494 DPRINT_DBG(VMBUS, "Reading %u bytes...", BufferLen);
496 /* DumpRingInfo(InRingInfo, "BEFORE "); */
498 /* Make sure there is something to read */
499 if (bytesAvailToRead < BufferLen) {
500 DPRINT_DBG(VMBUS,
501 "got callback but not enough to read "
502 "<avail to read %d read size %d>!!",
503 bytesAvailToRead,
504 BufferLen);
506 spin_unlock_irqrestore(&InRingInfo->ring_lock, flags);
508 return -1;
511 nextReadLocation = GetNextReadLocationWithOffset(InRingInfo, Offset);
513 nextReadLocation = CopyFromRingBuffer(InRingInfo,
514 Buffer,
515 BufferLen,
516 nextReadLocation);
518 nextReadLocation = CopyFromRingBuffer(InRingInfo,
519 &prevIndices,
520 sizeof(u64),
521 nextReadLocation);
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 */
525 /*is updated */
526 mb();
528 /* Update the read index */
529 SetNextReadLocation(InRingInfo, nextReadLocation);
531 /* DumpRingInfo(InRingInfo, "AFTER "); */
533 spin_unlock_irqrestore(&InRingInfo->ring_lock, flags);
535 return 0;
539 /*++
541 Name:
542 CopyToRingBuffer()
544 Description:
545 Helper routine to copy from source to ring buffer.
546 Assume there is enough room. Handles wrap-around in dest case only!!
548 --*/
549 static u32
550 CopyToRingBuffer(
551 struct hv_ring_buffer_info *RingInfo,
552 u32 StartWriteOffset,
553 void *Src,
554 u32 SrcLen)
556 void *ringBuffer = GetRingBuffer(RingInfo);
557 u32 ringBufferSize = GetRingBufferSize(RingInfo);
558 u32 fragLen;
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);
567 } else
568 memcpy(ringBuffer + StartWriteOffset, Src, SrcLen);
570 StartWriteOffset += SrcLen;
571 StartWriteOffset %= ringBufferSize;
573 return StartWriteOffset;
577 /*++
579 Name:
580 CopyFromRingBuffer()
582 Description:
583 Helper routine to copy to source from ring buffer.
584 Assume there is enough room. Handles wrap-around in src case only!!
586 --*/
587 static u32
588 CopyFromRingBuffer(
589 struct hv_ring_buffer_info *RingInfo,
590 void *Dest,
591 u32 DestLen,
592 u32 StartReadOffset)
594 void *ringBuffer = GetRingBuffer(RingInfo);
595 u32 ringBufferSize = GetRingBufferSize(RingInfo);
597 u32 fragLen;
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);
607 } else
609 memcpy(Dest, ringBuffer + StartReadOffset, DestLen);
612 StartReadOffset += DestLen;
613 StartReadOffset %= ringBufferSize;
615 return StartReadOffset;
619 /* eof */