blob: ac8cb69147c4c00c94357161a6dd988d0d117e98 [file] [log] [blame]
Hank Janssen3e7ee492009-07-13 16:02:34 -07001/*
2 *
3 * Copyright (c) 2009, Microsoft Corporation.
4 *
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.
8 *
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.
13 *
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.
17 *
18 * Authors:
19 * Haiyang Zhang <haiyangz@microsoft.com>
20 * Hank Janssen <hjanssen@microsoft.com>
21 *
22 */
23
Greg Kroah-Hartmana0086dc2009-08-17 17:22:08 -070024#include <linux/kernel.h>
25#include <linux/mm.h>
Greg Kroah-Hartman4983b392009-08-19 16:14:47 -070026#include "osd.h"
Greg Kroah-Hartman645954c2009-08-28 16:22:59 -070027#include "logging.h"
Greg Kroah-Hartman8f078ca2010-05-05 22:22:35 -070028#include "ring_buffer.h"
Hank Janssen3e7ee492009-07-13 16:02:34 -070029
Hank Janssen3e7ee492009-07-13 16:02:34 -070030
Bill Pemberton454f18a2009-07-27 16:47:24 -040031/* #defines */
32
33
34/* Amount of space to write to */
Lars Lindley0686e4f2010-03-11 23:51:23 +010035#define BYTES_AVAIL_TO_WRITE(r, w, z) ((w) >= (r)) ? ((z) - ((w) - (r))) : ((r) - (w))
Hank Janssen3e7ee492009-07-13 16:02:34 -070036
37
38/*++
39
40Name:
41 GetRingBufferAvailBytes()
42
43Description:
44 Get number of bytes available to read and to write to
45 for the specified ring buffer
46
47--*/
48static inline void
Greg Kroah-Hartman4d643112009-07-14 15:09:36 -070049GetRingBufferAvailBytes(RING_BUFFER_INFO *rbi, u32 *read, u32 *write)
Hank Janssen3e7ee492009-07-13 16:02:34 -070050{
C. Bartlett4408f532010-02-03 15:34:27 +000051 u32 read_loc, write_loc;
Hank Janssen3e7ee492009-07-13 16:02:34 -070052
Bill Pemberton454f18a2009-07-27 16:47:24 -040053 /* Capture the read/write indices before they changed */
Hank Janssen3e7ee492009-07-13 16:02:34 -070054 read_loc = rbi->RingBuffer->ReadIndex;
55 write_loc = rbi->RingBuffer->WriteIndex;
56
57 *write = BYTES_AVAIL_TO_WRITE(read_loc, write_loc, rbi->RingDataSize);
58 *read = rbi->RingDataSize - *write;
59}
60
61/*++
62
63Name:
64 GetNextWriteLocation()
65
66Description:
67 Get the next write location for the specified ring buffer
68
69--*/
Greg Kroah-Hartman4d643112009-07-14 15:09:36 -070070static inline u32
C. Bartlett4408f532010-02-03 15:34:27 +000071GetNextWriteLocation(RING_BUFFER_INFO *RingInfo)
Hank Janssen3e7ee492009-07-13 16:02:34 -070072{
Greg Kroah-Hartman4d643112009-07-14 15:09:36 -070073 u32 next = RingInfo->RingBuffer->WriteIndex;
Hank Janssen3e7ee492009-07-13 16:02:34 -070074
Bill Pemberton1bbdd7a2010-05-05 15:27:48 -040075 /* ASSERT(next < RingInfo->RingDataSize); */
Hank Janssen3e7ee492009-07-13 16:02:34 -070076
77 return next;
78}
79
80/*++
81
82Name:
83 SetNextWriteLocation()
84
85Description:
86 Set the next write location for the specified ring buffer
87
88--*/
89static inline void
C. Bartlett4408f532010-02-03 15:34:27 +000090SetNextWriteLocation(RING_BUFFER_INFO *RingInfo, u32 NextWriteLocation)
Hank Janssen3e7ee492009-07-13 16:02:34 -070091{
92 RingInfo->RingBuffer->WriteIndex = NextWriteLocation;
93}
94
95/*++
96
97Name:
98 GetNextReadLocation()
99
100Description:
101 Get the next read location for the specified ring buffer
102
103--*/
Greg Kroah-Hartman4d643112009-07-14 15:09:36 -0700104static inline u32
C. Bartlett4408f532010-02-03 15:34:27 +0000105GetNextReadLocation(RING_BUFFER_INFO *RingInfo)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700106{
Greg Kroah-Hartman4d643112009-07-14 15:09:36 -0700107 u32 next = RingInfo->RingBuffer->ReadIndex;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700108
Bill Pemberton1bbdd7a2010-05-05 15:27:48 -0400109 /* ASSERT(next < RingInfo->RingDataSize); */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700110
111 return next;
112}
113
114/*++
115
116Name:
117 GetNextReadLocationWithOffset()
118
119Description:
120 Get the next read location + offset for the specified ring buffer.
121 This allows the caller to skip
122
123--*/
Greg Kroah-Hartman4d643112009-07-14 15:09:36 -0700124static inline u32
C. Bartlett4408f532010-02-03 15:34:27 +0000125GetNextReadLocationWithOffset(RING_BUFFER_INFO *RingInfo, u32 Offset)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700126{
Greg Kroah-Hartman4d643112009-07-14 15:09:36 -0700127 u32 next = RingInfo->RingBuffer->ReadIndex;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700128
Bill Pemberton1bbdd7a2010-05-05 15:27:48 -0400129 /* ASSERT(next < RingInfo->RingDataSize); */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700130 next += Offset;
131 next %= RingInfo->RingDataSize;
132
133 return next;
134}
135
136/*++
137
138Name:
139 SetNextReadLocation()
140
141Description:
142 Set the next read location for the specified ring buffer
143
144--*/
145static inline void
C. Bartlett4408f532010-02-03 15:34:27 +0000146SetNextReadLocation(RING_BUFFER_INFO *RingInfo, u32 NextReadLocation)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700147{
148 RingInfo->RingBuffer->ReadIndex = NextReadLocation;
149}
150
151
152/*++
153
154Name:
155 GetRingBuffer()
156
157Description:
158 Get the start of the ring buffer
159
160--*/
Greg Kroah-Hartman8282c402009-07-14 15:06:28 -0700161static inline void *
C. Bartlett4408f532010-02-03 15:34:27 +0000162GetRingBuffer(RING_BUFFER_INFO *RingInfo)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700163{
Greg Kroah-Hartman8282c402009-07-14 15:06:28 -0700164 return (void *)RingInfo->RingBuffer->Buffer;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700165}
166
167
168/*++
169
170Name:
171 GetRingBufferSize()
172
173Description:
174 Get the size of the ring buffer
175
176--*/
Greg Kroah-Hartman4d643112009-07-14 15:09:36 -0700177static inline u32
C. Bartlett4408f532010-02-03 15:34:27 +0000178GetRingBufferSize(RING_BUFFER_INFO *RingInfo)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700179{
180 return RingInfo->RingDataSize;
181}
182
183/*++
184
185Name:
186 GetRingBufferIndices()
187
188Description:
Greg Kroah-Hartman59471432009-07-14 15:10:26 -0700189 Get the read and write indices as u64 of the specified ring buffer
Hank Janssen3e7ee492009-07-13 16:02:34 -0700190
191--*/
Greg Kroah-Hartman59471432009-07-14 15:10:26 -0700192static inline u64
C. Bartlett4408f532010-02-03 15:34:27 +0000193GetRingBufferIndices(RING_BUFFER_INFO *RingInfo)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700194{
C. Bartlett4408f532010-02-03 15:34:27 +0000195 return ((u64)RingInfo->RingBuffer->WriteIndex << 32)
196 || RingInfo->RingBuffer->ReadIndex;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700197}
198
199
200/*++
201
202Name:
203 DumpRingInfo()
204
205Description:
206 Dump out to console the ring buffer info
207
208--*/
Greg Kroah-Hartman3523a802009-08-17 17:22:08 -0700209void DumpRingInfo(RING_BUFFER_INFO *RingInfo, char *Prefix)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700210{
Greg Kroah-Hartman4d643112009-07-14 15:09:36 -0700211 u32 bytesAvailToWrite;
212 u32 bytesAvailToRead;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700213
C. Bartlett4408f532010-02-03 15:34:27 +0000214 GetRingBufferAvailBytes(RingInfo,
215 &bytesAvailToRead,
216 &bytesAvailToWrite);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700217
C. Bartlett4408f532010-02-03 15:34:27 +0000218 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>>",
Hank Janssen3e7ee492009-07-13 16:02:34 -0700222 Prefix,
223 RingInfo,
224 RingInfo->RingBuffer->Buffer,
225 bytesAvailToWrite,
226 bytesAvailToRead,
227 RingInfo->RingBuffer->ReadIndex,
228 RingInfo->RingBuffer->WriteIndex);
229}
230
Bill Pemberton454f18a2009-07-27 16:47:24 -0400231
232/* Internal routines */
233
Greg Kroah-Hartman4d643112009-07-14 15:09:36 -0700234static u32
Hank Janssen3e7ee492009-07-13 16:02:34 -0700235CopyToRingBuffer(
236 RING_BUFFER_INFO *RingInfo,
Greg Kroah-Hartman4d643112009-07-14 15:09:36 -0700237 u32 StartWriteOffset,
C. Bartlett4408f532010-02-03 15:34:27 +0000238 void *Src,
Greg Kroah-Hartman4d643112009-07-14 15:09:36 -0700239 u32 SrcLen);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700240
Greg Kroah-Hartman4d643112009-07-14 15:09:36 -0700241static u32
Hank Janssen3e7ee492009-07-13 16:02:34 -0700242CopyFromRingBuffer(
243 RING_BUFFER_INFO *RingInfo,
C. Bartlett4408f532010-02-03 15:34:27 +0000244 void *Dest,
Greg Kroah-Hartman4d643112009-07-14 15:09:36 -0700245 u32 DestLen,
246 u32 StartReadOffset);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700247
248
249
250/*++
251
252Name:
253 RingBufferGetDebugInfo()
254
255Description:
256 Get various debug metrics for the specified ring buffer
257
258--*/
Greg Kroah-Hartman3523a802009-08-17 17:22:08 -0700259void RingBufferGetDebugInfo(RING_BUFFER_INFO *RingInfo,
Greg Kroah-Hartman80682b72010-07-27 11:37:32 -0700260 struct hv_ring_buffer_debug_info *debug_info)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700261{
Greg Kroah-Hartman4d643112009-07-14 15:09:36 -0700262 u32 bytesAvailToWrite;
263 u32 bytesAvailToRead;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700264
C. Bartlett4408f532010-02-03 15:34:27 +0000265 if (RingInfo->RingBuffer) {
266 GetRingBufferAvailBytes(RingInfo,
267 &bytesAvailToRead,
268 &bytesAvailToWrite);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700269
Greg Kroah-Hartman80682b72010-07-27 11:37:32 -0700270 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;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700275 }
276}
277
278
279/*++
280
281Name:
282 GetRingBufferInterruptMask()
283
284Description:
285 Get the interrupt mask for the specified ring buffer
286
287--*/
Greg Kroah-Hartman3523a802009-08-17 17:22:08 -0700288u32 GetRingBufferInterruptMask(RING_BUFFER_INFO *rbi)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700289{
290 return rbi->RingBuffer->InterruptMask;
291}
292
293/*++
294
295Name:
296 RingBufferInit()
297
298Description:
299 Initialize the ring buffer
300
301--*/
Greg Kroah-Hartman3523a802009-08-17 17:22:08 -0700302int RingBufferInit(RING_BUFFER_INFO *RingInfo, void *Buffer, u32 BufferLen)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700303{
Bill Pemberton3324fb42010-05-05 15:27:49 -0400304 if (sizeof(RING_BUFFER) != PAGE_SIZE)
305 return -EINVAL;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700306
307 memset(RingInfo, 0, sizeof(RING_BUFFER_INFO));
308
C. Bartlett4408f532010-02-03 15:34:27 +0000309 RingInfo->RingBuffer = (RING_BUFFER *)Buffer;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700310 RingInfo->RingBuffer->ReadIndex = RingInfo->RingBuffer->WriteIndex = 0;
311
312 RingInfo->RingSize = BufferLen;
313 RingInfo->RingDataSize = BufferLen - sizeof(RING_BUFFER);
314
Greg Kroah-Hartmana98f96e2009-07-15 14:55:14 -0700315 spin_lock_init(&RingInfo->ring_lock);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700316
317 return 0;
318}
319
320/*++
321
322Name:
323 RingBufferCleanup()
324
325Description:
326 Cleanup the ring buffer
327
328--*/
C. Bartlett4408f532010-02-03 15:34:27 +0000329void RingBufferCleanup(RING_BUFFER_INFO *RingInfo)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700330{
Hank Janssen3e7ee492009-07-13 16:02:34 -0700331}
332
333/*++
334
335Name:
336 RingBufferWrite()
337
338Description:
339 Write to the ring buffer
340
341--*/
Greg Kroah-Hartman3523a802009-08-17 17:22:08 -0700342int RingBufferWrite(RING_BUFFER_INFO *OutRingInfo,
343 struct scatterlist *sglist, u32 sgcount)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700344{
C. Bartlett4408f532010-02-03 15:34:27 +0000345 int i = 0;
Greg Kroah-Hartman4d643112009-07-14 15:09:36 -0700346 u32 byteAvailToWrite;
347 u32 byteAvailToRead;
C. Bartlett4408f532010-02-03 15:34:27 +0000348 u32 totalBytesToWrite = 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700349
Nicolas Palixb219b3f2009-07-30 17:37:23 +0200350 struct scatterlist *sg;
Greg Kroah-Hartman4d643112009-07-14 15:09:36 -0700351 volatile u32 nextWriteLocation;
C. Bartlett4408f532010-02-03 15:34:27 +0000352 u64 prevIndices = 0;
Greg Kroah-Hartmana98f96e2009-07-15 14:55:14 -0700353 unsigned long flags;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700354
Nicolas Palixb219b3f2009-07-30 17:37:23 +0200355 for_each_sg(sglist, sg, sgcount, i)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700356 {
Nicolas Palixb219b3f2009-07-30 17:37:23 +0200357 totalBytesToWrite += sg->length;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700358 }
359
Greg Kroah-Hartman59471432009-07-14 15:10:26 -0700360 totalBytesToWrite += sizeof(u64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700361
Greg Kroah-Hartmana98f96e2009-07-15 14:55:14 -0700362 spin_lock_irqsave(&OutRingInfo->ring_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700363
C. Bartlett4408f532010-02-03 15:34:27 +0000364 GetRingBufferAvailBytes(OutRingInfo,
365 &byteAvailToRead,
366 &byteAvailToWrite);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700367
368 DPRINT_DBG(VMBUS, "Writing %u bytes...", totalBytesToWrite);
369
Bill Pemberton454f18a2009-07-27 16:47:24 -0400370 /* DumpRingInfo(OutRingInfo, "BEFORE "); */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700371
C. Bartlett4408f532010-02-03 15:34:27 +0000372 /* If there is only room for the packet, assume it is full. */
373 /* Otherwise, the next time around, we think the ring buffer */
Bill Pemberton454f18a2009-07-27 16:47:24 -0400374 /* is empty since the read index == write index */
C. Bartlett4408f532010-02-03 15:34:27 +0000375 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);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700381
Greg Kroah-Hartmana98f96e2009-07-15 14:55:14 -0700382 spin_unlock_irqrestore(&OutRingInfo->ring_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700383 return -1;
384 }
385
Bill Pemberton454f18a2009-07-27 16:47:24 -0400386 /* Write to the ring buffer */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700387 nextWriteLocation = GetNextWriteLocation(OutRingInfo);
388
Nicolas Palixb219b3f2009-07-30 17:37:23 +0200389 for_each_sg(sglist, sg, sgcount, i)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700390 {
Nicolas Palixb219b3f2009-07-30 17:37:23 +0200391 nextWriteLocation = CopyToRingBuffer(OutRingInfo,
392 nextWriteLocation,
393 sg_virt(sg),
394 sg->length);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700395 }
396
Bill Pemberton454f18a2009-07-27 16:47:24 -0400397 /* Set previous packet start */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700398 prevIndices = GetRingBufferIndices(OutRingInfo);
399
400 nextWriteLocation = CopyToRingBuffer(OutRingInfo,
Nicolas Palixb219b3f2009-07-30 17:37:23 +0200401 nextWriteLocation,
402 &prevIndices,
403 sizeof(u64));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700404
Bill Pemberton454f18a2009-07-27 16:47:24 -0400405 /* Make sure we flush all writes before updating the writeIndex */
Greg Kroah-Hartman28b6ca92009-07-16 12:34:20 -0700406 mb();
Hank Janssen3e7ee492009-07-13 16:02:34 -0700407
Bill Pemberton454f18a2009-07-27 16:47:24 -0400408 /* Now, update the write location */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700409 SetNextWriteLocation(OutRingInfo, nextWriteLocation);
410
Bill Pemberton454f18a2009-07-27 16:47:24 -0400411 /* DumpRingInfo(OutRingInfo, "AFTER "); */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700412
Greg Kroah-Hartmana98f96e2009-07-15 14:55:14 -0700413 spin_unlock_irqrestore(&OutRingInfo->ring_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700414 return 0;
415}
416
417
418/*++
419
420Name:
421 RingBufferPeek()
422
423Description:
424 Read without advancing the read index
425
426--*/
Greg Kroah-Hartman3523a802009-08-17 17:22:08 -0700427int RingBufferPeek(RING_BUFFER_INFO *InRingInfo, void *Buffer, u32 BufferLen)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700428{
Greg Kroah-Hartman4d643112009-07-14 15:09:36 -0700429 u32 bytesAvailToWrite;
430 u32 bytesAvailToRead;
C. Bartlett4408f532010-02-03 15:34:27 +0000431 u32 nextReadLocation = 0;
Greg Kroah-Hartmana98f96e2009-07-15 14:55:14 -0700432 unsigned long flags;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700433
Greg Kroah-Hartmana98f96e2009-07-15 14:55:14 -0700434 spin_lock_irqsave(&InRingInfo->ring_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700435
C. Bartlett4408f532010-02-03 15:34:27 +0000436 GetRingBufferAvailBytes(InRingInfo,
437 &bytesAvailToRead,
438 &bytesAvailToWrite);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700439
Bill Pemberton454f18a2009-07-27 16:47:24 -0400440 /* Make sure there is something to read */
C. Bartlett4408f532010-02-03 15:34:27 +0000441 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); */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700447
Greg Kroah-Hartmana98f96e2009-07-15 14:55:14 -0700448 spin_unlock_irqrestore(&InRingInfo->ring_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700449
450 return -1;
451 }
452
Bill Pemberton454f18a2009-07-27 16:47:24 -0400453 /* Convert to byte offset */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700454 nextReadLocation = GetNextReadLocation(InRingInfo);
455
456 nextReadLocation = CopyFromRingBuffer(InRingInfo,
C. Bartlett4408f532010-02-03 15:34:27 +0000457 Buffer,
458 BufferLen,
459 nextReadLocation);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700460
Greg Kroah-Hartmana98f96e2009-07-15 14:55:14 -0700461 spin_unlock_irqrestore(&InRingInfo->ring_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700462
463 return 0;
464}
465
466
467/*++
468
469Name:
470 RingBufferRead()
471
472Description:
473 Read and advance the read index
474
475--*/
Greg Kroah-Hartman3523a802009-08-17 17:22:08 -0700476int RingBufferRead(RING_BUFFER_INFO *InRingInfo, void *Buffer,
477 u32 BufferLen, u32 Offset)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700478{
Greg Kroah-Hartman4d643112009-07-14 15:09:36 -0700479 u32 bytesAvailToWrite;
480 u32 bytesAvailToRead;
C. Bartlett4408f532010-02-03 15:34:27 +0000481 u32 nextReadLocation = 0;
482 u64 prevIndices = 0;
Greg Kroah-Hartmana98f96e2009-07-15 14:55:14 -0700483 unsigned long flags;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700484
Bill Pembertona16e1482010-05-05 15:27:50 -0400485 if (BufferLen <= 0)
486 return -EINVAL;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700487
Greg Kroah-Hartmana98f96e2009-07-15 14:55:14 -0700488 spin_lock_irqsave(&InRingInfo->ring_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700489
C. Bartlett4408f532010-02-03 15:34:27 +0000490 GetRingBufferAvailBytes(InRingInfo,
491 &bytesAvailToRead,
492 &bytesAvailToWrite);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700493
494 DPRINT_DBG(VMBUS, "Reading %u bytes...", BufferLen);
495
Bill Pemberton454f18a2009-07-27 16:47:24 -0400496 /* DumpRingInfo(InRingInfo, "BEFORE "); */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700497
Bill Pemberton454f18a2009-07-27 16:47:24 -0400498 /* Make sure there is something to read */
C. Bartlett4408f532010-02-03 15:34:27 +0000499 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);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700505
Greg Kroah-Hartmana98f96e2009-07-15 14:55:14 -0700506 spin_unlock_irqrestore(&InRingInfo->ring_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700507
508 return -1;
509 }
510
511 nextReadLocation = GetNextReadLocationWithOffset(InRingInfo, Offset);
512
513 nextReadLocation = CopyFromRingBuffer(InRingInfo,
C. Bartlett4408f532010-02-03 15:34:27 +0000514 Buffer,
515 BufferLen,
516 nextReadLocation);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700517
518 nextReadLocation = CopyFromRingBuffer(InRingInfo,
C. Bartlett4408f532010-02-03 15:34:27 +0000519 &prevIndices,
520 sizeof(u64),
521 nextReadLocation);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700522
Bill Pemberton454f18a2009-07-27 16:47:24 -0400523 /* Make sure all reads are done before we update the read index since */
C. Bartlett4408f532010-02-03 15:34:27 +0000524 /* the writer may start writing to the read area once the read index */
525 /*is updated */
Greg Kroah-Hartman28b6ca92009-07-16 12:34:20 -0700526 mb();
Hank Janssen3e7ee492009-07-13 16:02:34 -0700527
Bill Pemberton454f18a2009-07-27 16:47:24 -0400528 /* Update the read index */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700529 SetNextReadLocation(InRingInfo, nextReadLocation);
530
Bill Pemberton454f18a2009-07-27 16:47:24 -0400531 /* DumpRingInfo(InRingInfo, "AFTER "); */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700532
Greg Kroah-Hartmana98f96e2009-07-15 14:55:14 -0700533 spin_unlock_irqrestore(&InRingInfo->ring_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700534
535 return 0;
536}
537
538
539/*++
540
541Name:
542 CopyToRingBuffer()
543
544Description:
545 Helper routine to copy from source to ring buffer.
546 Assume there is enough room. Handles wrap-around in dest case only!!
547
548--*/
Greg Kroah-Hartmanbd1de702009-07-29 09:04:51 -0700549static u32
Hank Janssen3e7ee492009-07-13 16:02:34 -0700550CopyToRingBuffer(
551 RING_BUFFER_INFO *RingInfo,
Greg Kroah-Hartman4d643112009-07-14 15:09:36 -0700552 u32 StartWriteOffset,
C. Bartlett4408f532010-02-03 15:34:27 +0000553 void *Src,
Greg Kroah-Hartman4d643112009-07-14 15:09:36 -0700554 u32 SrcLen)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700555{
C. Bartlett4408f532010-02-03 15:34:27 +0000556 void *ringBuffer = GetRingBuffer(RingInfo);
557 u32 ringBufferSize = GetRingBufferSize(RingInfo);
Greg Kroah-Hartman4d643112009-07-14 15:09:36 -0700558 u32 fragLen;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700559
C. Bartlett4408f532010-02-03 15:34:27 +0000560 /* wrap-around detected! */
561 if (SrcLen > ringBufferSize - StartWriteOffset) {
Hank Janssen3e7ee492009-07-13 16:02:34 -0700562 DPRINT_DBG(VMBUS, "wrap-around detected!");
563
564 fragLen = ringBufferSize - StartWriteOffset;
565 memcpy(ringBuffer + StartWriteOffset, Src, fragLen);
566 memcpy(ringBuffer, Src + fragLen, SrcLen - fragLen);
C. Bartlett4408f532010-02-03 15:34:27 +0000567 } else
Hank Janssen3e7ee492009-07-13 16:02:34 -0700568 memcpy(ringBuffer + StartWriteOffset, Src, SrcLen);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700569
570 StartWriteOffset += SrcLen;
571 StartWriteOffset %= ringBufferSize;
572
573 return StartWriteOffset;
574}
575
576
577/*++
578
579Name:
580 CopyFromRingBuffer()
581
582Description:
583 Helper routine to copy to source from ring buffer.
584 Assume there is enough room. Handles wrap-around in src case only!!
585
586--*/
Greg Kroah-Hartmanbd1de702009-07-29 09:04:51 -0700587static u32
Hank Janssen3e7ee492009-07-13 16:02:34 -0700588CopyFromRingBuffer(
589 RING_BUFFER_INFO *RingInfo,
C. Bartlett4408f532010-02-03 15:34:27 +0000590 void *Dest,
Greg Kroah-Hartman4d643112009-07-14 15:09:36 -0700591 u32 DestLen,
592 u32 StartReadOffset)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700593{
C. Bartlett4408f532010-02-03 15:34:27 +0000594 void *ringBuffer = GetRingBuffer(RingInfo);
595 u32 ringBufferSize = GetRingBufferSize(RingInfo);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700596
Greg Kroah-Hartman4d643112009-07-14 15:09:36 -0700597 u32 fragLen;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700598
C. Bartlett4408f532010-02-03 15:34:27 +0000599 /* wrap-around detected at the src */
600 if (DestLen > ringBufferSize - StartReadOffset) {
Hank Janssen3e7ee492009-07-13 16:02:34 -0700601 DPRINT_DBG(VMBUS, "src wrap-around detected!");
602
603 fragLen = ringBufferSize - StartReadOffset;
604
605 memcpy(Dest, ringBuffer + StartReadOffset, fragLen);
606 memcpy(Dest + fragLen, ringBuffer, DestLen - fragLen);
C. Bartlett4408f532010-02-03 15:34:27 +0000607 } else
608
Hank Janssen3e7ee492009-07-13 16:02:34 -0700609 memcpy(Dest, ringBuffer + StartReadOffset, DestLen);
C. Bartlett4408f532010-02-03 15:34:27 +0000610
Hank Janssen3e7ee492009-07-13 16:02:34 -0700611
612 StartReadOffset += DestLen;
613 StartReadOffset %= ringBufferSize;
614
615 return StartReadOffset;
616}
617
618
Bill Pemberton454f18a2009-07-27 16:47:24 -0400619/* eof */