Staging: hv: rename Connection.c to connection.c
[linux-2.6.git] / drivers / staging / hv / RingBuffer.c
1 /*
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
24 #include <linux/kernel.h>
25 #include <linux/mm.h>
26 #include "osd.h"
27 #include "logging.h"
28 #include "RingBuffer.h"
29
30
31 /* #defines */
32
33
34 /* Amount of space to write to */
35 #define BYTES_AVAIL_TO_WRITE(r, w, z) ((w) >= (r)) ? ((z) - ((w) - (r))) : ((r) - (w))
36
37
38 /*++
39
40 Name:
41         GetRingBufferAvailBytes()
42
43 Description:
44         Get number of bytes available to read and to write to
45         for the specified ring buffer
46
47 --*/
48 static inline void
49 GetRingBufferAvailBytes(RING_BUFFER_INFO *rbi, u32 *read, u32 *write)
50 {
51         u32 read_loc, write_loc;
52
53         /* Capture the read/write indices before they changed */
54         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
63 Name:
64         GetNextWriteLocation()
65
66 Description:
67         Get the next write location for the specified ring buffer
68
69 --*/
70 static inline u32
71 GetNextWriteLocation(RING_BUFFER_INFO *RingInfo)
72 {
73         u32 next = RingInfo->RingBuffer->WriteIndex;
74
75         /* ASSERT(next < RingInfo->RingDataSize); */
76
77         return next;
78 }
79
80 /*++
81
82 Name:
83         SetNextWriteLocation()
84
85 Description:
86         Set the next write location for the specified ring buffer
87
88 --*/
89 static inline void
90 SetNextWriteLocation(RING_BUFFER_INFO *RingInfo, u32 NextWriteLocation)
91 {
92         RingInfo->RingBuffer->WriteIndex = NextWriteLocation;
93 }
94
95 /*++
96
97 Name:
98         GetNextReadLocation()
99
100 Description:
101         Get the next read location for the specified ring buffer
102
103 --*/
104 static inline u32
105 GetNextReadLocation(RING_BUFFER_INFO *RingInfo)
106 {
107         u32 next = RingInfo->RingBuffer->ReadIndex;
108
109         /* ASSERT(next < RingInfo->RingDataSize); */
110
111         return next;
112 }
113
114 /*++
115
116 Name:
117         GetNextReadLocationWithOffset()
118
119 Description:
120         Get the next read location + offset for the specified ring buffer.
121         This allows the caller to skip
122
123 --*/
124 static inline u32
125 GetNextReadLocationWithOffset(RING_BUFFER_INFO *RingInfo, u32 Offset)
126 {
127         u32 next = RingInfo->RingBuffer->ReadIndex;
128
129         /* ASSERT(next < RingInfo->RingDataSize); */
130         next += Offset;
131         next %= RingInfo->RingDataSize;
132
133         return next;
134 }
135
136 /*++
137
138 Name:
139         SetNextReadLocation()
140
141 Description:
142         Set the next read location for the specified ring buffer
143
144 --*/
145 static inline void
146 SetNextReadLocation(RING_BUFFER_INFO *RingInfo, u32 NextReadLocation)
147 {
148         RingInfo->RingBuffer->ReadIndex = NextReadLocation;
149 }
150
151
152 /*++
153
154 Name:
155         GetRingBuffer()
156
157 Description:
158         Get the start of the ring buffer
159
160 --*/
161 static inline void *
162 GetRingBuffer(RING_BUFFER_INFO *RingInfo)
163 {
164         return (void *)RingInfo->RingBuffer->Buffer;
165 }
166
167
168 /*++
169
170 Name:
171         GetRingBufferSize()
172
173 Description:
174         Get the size of the ring buffer
175
176 --*/
177 static inline u32
178 GetRingBufferSize(RING_BUFFER_INFO *RingInfo)
179 {
180         return RingInfo->RingDataSize;
181 }
182
183 /*++
184
185 Name:
186         GetRingBufferIndices()
187
188 Description:
189         Get the read and write indices as u64 of the specified ring buffer
190
191 --*/
192 static inline u64
193 GetRingBufferIndices(RING_BUFFER_INFO *RingInfo)
194 {
195         return ((u64)RingInfo->RingBuffer->WriteIndex << 32)
196         || RingInfo->RingBuffer->ReadIndex;
197 }
198
199
200 /*++
201
202 Name:
203         DumpRingInfo()
204
205 Description:
206         Dump out to console the ring buffer info
207
208 --*/
209 void DumpRingInfo(RING_BUFFER_INFO *RingInfo, char *Prefix)
210 {
211         u32 bytesAvailToWrite;
212         u32 bytesAvailToRead;
213
214         GetRingBufferAvailBytes(RingInfo,
215         &bytesAvailToRead,
216         &bytesAvailToWrite);
217
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);
229 }
230
231
232 /* Internal routines */
233
234 static u32
235 CopyToRingBuffer(
236         RING_BUFFER_INFO        *RingInfo,
237         u32                             StartWriteOffset,
238         void                            *Src,
239         u32                             SrcLen);
240
241 static u32
242 CopyFromRingBuffer(
243         RING_BUFFER_INFO        *RingInfo,
244         void                            *Dest,
245         u32                             DestLen,
246         u32                             StartReadOffset);
247
248
249
250 /*++
251
252 Name:
253         RingBufferGetDebugInfo()
254
255 Description:
256         Get various debug metrics for the specified ring buffer
257
258 --*/
259 void RingBufferGetDebugInfo(RING_BUFFER_INFO *RingInfo,
260                             RING_BUFFER_DEBUG_INFO *DebugInfo)
261 {
262         u32 bytesAvailToWrite;
263         u32 bytesAvailToRead;
264
265         if (RingInfo->RingBuffer) {
266                 GetRingBufferAvailBytes(RingInfo,
267                                         &bytesAvailToRead,
268                                         &bytesAvailToWrite);
269
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;
275         }
276 }
277
278
279 /*++
280
281 Name:
282         GetRingBufferInterruptMask()
283
284 Description:
285         Get the interrupt mask for the specified ring buffer
286
287 --*/
288 u32 GetRingBufferInterruptMask(RING_BUFFER_INFO *rbi)
289 {
290         return rbi->RingBuffer->InterruptMask;
291 }
292
293 /*++
294
295 Name:
296         RingBufferInit()
297
298 Description:
299         Initialize the ring buffer
300
301 --*/
302 int RingBufferInit(RING_BUFFER_INFO *RingInfo, void *Buffer, u32 BufferLen)
303 {
304         if (sizeof(RING_BUFFER) != PAGE_SIZE)
305                 return -EINVAL;
306
307         memset(RingInfo, 0, sizeof(RING_BUFFER_INFO));
308
309         RingInfo->RingBuffer = (RING_BUFFER *)Buffer;
310         RingInfo->RingBuffer->ReadIndex = RingInfo->RingBuffer->WriteIndex = 0;
311
312         RingInfo->RingSize = BufferLen;
313         RingInfo->RingDataSize = BufferLen - sizeof(RING_BUFFER);
314
315         spin_lock_init(&RingInfo->ring_lock);
316
317         return 0;
318 }
319
320 /*++
321
322 Name:
323         RingBufferCleanup()
324
325 Description:
326         Cleanup the ring buffer
327
328 --*/
329 void RingBufferCleanup(RING_BUFFER_INFO *RingInfo)
330 {
331 }
332
333 /*++
334
335 Name:
336         RingBufferWrite()
337
338 Description:
339         Write to the ring buffer
340
341 --*/
342 int RingBufferWrite(RING_BUFFER_INFO *OutRingInfo,
343                     struct scatterlist *sglist, u32 sgcount)
344 {
345         int i = 0;
346         u32 byteAvailToWrite;
347         u32 byteAvailToRead;
348         u32 totalBytesToWrite = 0;
349
350         struct scatterlist *sg;
351         volatile u32 nextWriteLocation;
352         u64 prevIndices = 0;
353         unsigned long flags;
354
355         DPRINT_ENTER(VMBUS);
356
357         for_each_sg(sglist, sg, sgcount, i)
358         {
359                 totalBytesToWrite += sg->length;
360         }
361
362         totalBytesToWrite += sizeof(u64);
363
364         spin_lock_irqsave(&OutRingInfo->ring_lock, flags);
365
366         GetRingBufferAvailBytes(OutRingInfo,
367                                 &byteAvailToRead,
368                                 &byteAvailToWrite);
369
370         DPRINT_DBG(VMBUS, "Writing %u bytes...", totalBytesToWrite);
371
372         /* DumpRingInfo(OutRingInfo, "BEFORE "); */
373
374         /* If there is only room for the packet, assume it is full. */
375         /* Otherwise, the next time around, we think the ring buffer */
376         /* is empty since the read index == write index */
377         if (byteAvailToWrite <= totalBytesToWrite) {
378                 DPRINT_DBG(VMBUS,
379                         "No more space left on outbound ring buffer "
380                         "(needed %u, avail %u)",
381                         totalBytesToWrite,
382                         byteAvailToWrite);
383
384                 spin_unlock_irqrestore(&OutRingInfo->ring_lock, flags);
385
386                 DPRINT_EXIT(VMBUS);
387
388                 return -1;
389         }
390
391         /* Write to the ring buffer */
392         nextWriteLocation = GetNextWriteLocation(OutRingInfo);
393
394         for_each_sg(sglist, sg, sgcount, i)
395         {
396                 nextWriteLocation = CopyToRingBuffer(OutRingInfo,
397                                                      nextWriteLocation,
398                                                      sg_virt(sg),
399                                                      sg->length);
400         }
401
402         /* Set previous packet start */
403         prevIndices = GetRingBufferIndices(OutRingInfo);
404
405         nextWriteLocation = CopyToRingBuffer(OutRingInfo,
406                                              nextWriteLocation,
407                                              &prevIndices,
408                                              sizeof(u64));
409
410         /* Make sure we flush all writes before updating the writeIndex */
411         mb();
412
413         /* Now, update the write location */
414         SetNextWriteLocation(OutRingInfo, nextWriteLocation);
415
416         /* DumpRingInfo(OutRingInfo, "AFTER "); */
417
418         spin_unlock_irqrestore(&OutRingInfo->ring_lock, flags);
419
420         DPRINT_EXIT(VMBUS);
421
422         return 0;
423 }
424
425
426 /*++
427
428 Name:
429         RingBufferPeek()
430
431 Description:
432         Read without advancing the read index
433
434 --*/
435 int RingBufferPeek(RING_BUFFER_INFO *InRingInfo, void *Buffer, u32 BufferLen)
436 {
437         u32 bytesAvailToWrite;
438         u32 bytesAvailToRead;
439         u32 nextReadLocation = 0;
440         unsigned long flags;
441
442         spin_lock_irqsave(&InRingInfo->ring_lock, flags);
443
444         GetRingBufferAvailBytes(InRingInfo,
445                                 &bytesAvailToRead,
446                                 &bytesAvailToWrite);
447
448         /* Make sure there is something to read */
449         if (bytesAvailToRead < BufferLen) {
450                 /* DPRINT_DBG(VMBUS,
451                         "got callback but not enough to read "
452                         "<avail to read %d read size %d>!!",
453                         bytesAvailToRead,
454                         BufferLen); */
455
456                 spin_unlock_irqrestore(&InRingInfo->ring_lock, flags);
457
458                 return -1;
459         }
460
461         /* Convert to byte offset */
462         nextReadLocation = GetNextReadLocation(InRingInfo);
463
464         nextReadLocation = CopyFromRingBuffer(InRingInfo,
465                                                 Buffer,
466                                                 BufferLen,
467                                                 nextReadLocation);
468
469         spin_unlock_irqrestore(&InRingInfo->ring_lock, flags);
470
471         return 0;
472 }
473
474
475 /*++
476
477 Name:
478         RingBufferRead()
479
480 Description:
481         Read and advance the read index
482
483 --*/
484 int RingBufferRead(RING_BUFFER_INFO *InRingInfo, void *Buffer,
485                    u32 BufferLen, u32 Offset)
486 {
487         u32 bytesAvailToWrite;
488         u32 bytesAvailToRead;
489         u32 nextReadLocation = 0;
490         u64 prevIndices = 0;
491         unsigned long flags;
492
493         if (BufferLen <= 0)
494                 return -EINVAL;
495
496         spin_lock_irqsave(&InRingInfo->ring_lock, flags);
497
498         GetRingBufferAvailBytes(InRingInfo,
499                                 &bytesAvailToRead,
500                                 &bytesAvailToWrite);
501
502         DPRINT_DBG(VMBUS, "Reading %u bytes...", BufferLen);
503
504         /* DumpRingInfo(InRingInfo, "BEFORE "); */
505
506         /* Make sure there is something to read */
507         if (bytesAvailToRead < BufferLen) {
508                 DPRINT_DBG(VMBUS,
509                         "got callback but not enough to read "
510                         "<avail to read %d read size %d>!!",
511                         bytesAvailToRead,
512                         BufferLen);
513
514                 spin_unlock_irqrestore(&InRingInfo->ring_lock, flags);
515
516                 return -1;
517         }
518
519         nextReadLocation = GetNextReadLocationWithOffset(InRingInfo, Offset);
520
521         nextReadLocation = CopyFromRingBuffer(InRingInfo,
522                                                 Buffer,
523                                                 BufferLen,
524                                                 nextReadLocation);
525
526         nextReadLocation = CopyFromRingBuffer(InRingInfo,
527                                                 &prevIndices,
528                                                 sizeof(u64),
529                                                 nextReadLocation);
530
531         /* Make sure all reads are done before we update the read index since */
532         /* the writer may start writing to the read area once the read index */
533         /*is updated */
534         mb();
535
536         /* Update the read index */
537         SetNextReadLocation(InRingInfo, nextReadLocation);
538
539         /* DumpRingInfo(InRingInfo, "AFTER "); */
540
541         spin_unlock_irqrestore(&InRingInfo->ring_lock, flags);
542
543         return 0;
544 }
545
546
547 /*++
548
549 Name:
550         CopyToRingBuffer()
551
552 Description:
553         Helper routine to copy from source to ring buffer.
554         Assume there is enough room. Handles wrap-around in dest case only!!
555
556 --*/
557 static u32
558 CopyToRingBuffer(
559         RING_BUFFER_INFO        *RingInfo,
560         u32                             StartWriteOffset,
561         void                            *Src,
562         u32                             SrcLen)
563 {
564         void *ringBuffer = GetRingBuffer(RingInfo);
565         u32 ringBufferSize = GetRingBufferSize(RingInfo);
566         u32 fragLen;
567
568         /* wrap-around detected! */
569         if (SrcLen > ringBufferSize - StartWriteOffset) {
570                 DPRINT_DBG(VMBUS, "wrap-around detected!");
571
572                 fragLen = ringBufferSize - StartWriteOffset;
573                 memcpy(ringBuffer + StartWriteOffset, Src, fragLen);
574                 memcpy(ringBuffer, Src + fragLen, SrcLen - fragLen);
575         } else
576                 memcpy(ringBuffer + StartWriteOffset, Src, SrcLen);
577
578         StartWriteOffset += SrcLen;
579         StartWriteOffset %= ringBufferSize;
580
581         return StartWriteOffset;
582 }
583
584
585 /*++
586
587 Name:
588         CopyFromRingBuffer()
589
590 Description:
591         Helper routine to copy to source from ring buffer.
592         Assume there is enough room. Handles wrap-around in src case only!!
593
594 --*/
595 static u32
596 CopyFromRingBuffer(
597         RING_BUFFER_INFO        *RingInfo,
598         void                            *Dest,
599         u32                             DestLen,
600         u32                             StartReadOffset)
601 {
602         void *ringBuffer = GetRingBuffer(RingInfo);
603         u32 ringBufferSize = GetRingBufferSize(RingInfo);
604
605         u32 fragLen;
606
607         /* wrap-around detected at the src */
608         if (DestLen > ringBufferSize - StartReadOffset) {
609                 DPRINT_DBG(VMBUS, "src wrap-around detected!");
610
611                 fragLen = ringBufferSize - StartReadOffset;
612
613                 memcpy(Dest, ringBuffer + StartReadOffset, fragLen);
614                 memcpy(Dest + fragLen, ringBuffer, DestLen - fragLen);
615         } else
616
617                 memcpy(Dest, ringBuffer + StartReadOffset, DestLen);
618
619
620         StartReadOffset += DestLen;
621         StartReadOffset %= ringBufferSize;
622
623         return StartReadOffset;
624 }
625
626
627 /* eof */