Added patch headers.
[linux-flexiantxendom0-3.2.10.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         ASSERT(sizeof(RING_BUFFER) == PAGE_SIZE);
305
306         memset(RingInfo, 0, sizeof(RING_BUFFER_INFO));
307
308         RingInfo->RingBuffer = (RING_BUFFER *)Buffer;
309         RingInfo->RingBuffer->ReadIndex = RingInfo->RingBuffer->WriteIndex = 0;
310
311         RingInfo->RingSize = BufferLen;
312         RingInfo->RingDataSize = BufferLen - sizeof(RING_BUFFER);
313
314         spin_lock_init(&RingInfo->ring_lock);
315
316         return 0;
317 }
318
319 /*++
320
321 Name:
322         RingBufferCleanup()
323
324 Description:
325         Cleanup the ring buffer
326
327 --*/
328 void RingBufferCleanup(RING_BUFFER_INFO *RingInfo)
329 {
330 }
331
332 /*++
333
334 Name:
335         RingBufferWrite()
336
337 Description:
338         Write to the ring buffer
339
340 --*/
341 int RingBufferWrite(RING_BUFFER_INFO *OutRingInfo,
342                     struct scatterlist *sglist, u32 sgcount)
343 {
344         int i = 0;
345         u32 byteAvailToWrite;
346         u32 byteAvailToRead;
347         u32 totalBytesToWrite = 0;
348
349         struct scatterlist *sg;
350         volatile u32 nextWriteLocation;
351         u64 prevIndices = 0;
352         unsigned long flags;
353
354         DPRINT_ENTER(VMBUS);
355
356         for_each_sg(sglist, sg, sgcount, i)
357         {
358                 totalBytesToWrite += sg->length;
359         }
360
361         totalBytesToWrite += sizeof(u64);
362
363         spin_lock_irqsave(&OutRingInfo->ring_lock, flags);
364
365         GetRingBufferAvailBytes(OutRingInfo,
366                                 &byteAvailToRead,
367                                 &byteAvailToWrite);
368
369         DPRINT_DBG(VMBUS, "Writing %u bytes...", totalBytesToWrite);
370
371         /* DumpRingInfo(OutRingInfo, "BEFORE "); */
372
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) {
377                 DPRINT_DBG(VMBUS,
378                         "No more space left on outbound ring buffer "
379                         "(needed %u, avail %u)",
380                         totalBytesToWrite,
381                         byteAvailToWrite);
382
383                 spin_unlock_irqrestore(&OutRingInfo->ring_lock, flags);
384
385                 DPRINT_EXIT(VMBUS);
386
387                 return -1;
388         }
389
390         /* Write to the ring buffer */
391         nextWriteLocation = GetNextWriteLocation(OutRingInfo);
392
393         for_each_sg(sglist, sg, sgcount, i)
394         {
395                 nextWriteLocation = CopyToRingBuffer(OutRingInfo,
396                                                      nextWriteLocation,
397                                                      sg_virt(sg),
398                                                      sg->length);
399         }
400
401         /* Set previous packet start */
402         prevIndices = GetRingBufferIndices(OutRingInfo);
403
404         nextWriteLocation = CopyToRingBuffer(OutRingInfo,
405                                              nextWriteLocation,
406                                              &prevIndices,
407                                              sizeof(u64));
408
409         /* Make sure we flush all writes before updating the writeIndex */
410         mb();
411
412         /* Now, update the write location */
413         SetNextWriteLocation(OutRingInfo, nextWriteLocation);
414
415         /* DumpRingInfo(OutRingInfo, "AFTER "); */
416
417         spin_unlock_irqrestore(&OutRingInfo->ring_lock, flags);
418
419         DPRINT_EXIT(VMBUS);
420
421         return 0;
422 }
423
424
425 /*++
426
427 Name:
428         RingBufferPeek()
429
430 Description:
431         Read without advancing the read index
432
433 --*/
434 int RingBufferPeek(RING_BUFFER_INFO *InRingInfo, void *Buffer, u32 BufferLen)
435 {
436         u32 bytesAvailToWrite;
437         u32 bytesAvailToRead;
438         u32 nextReadLocation = 0;
439         unsigned long flags;
440
441         spin_lock_irqsave(&InRingInfo->ring_lock, flags);
442
443         GetRingBufferAvailBytes(InRingInfo,
444                                 &bytesAvailToRead,
445                                 &bytesAvailToWrite);
446
447         /* Make sure there is something to read */
448         if (bytesAvailToRead < BufferLen) {
449                 /* DPRINT_DBG(VMBUS,
450                         "got callback but not enough to read "
451                         "<avail to read %d read size %d>!!",
452                         bytesAvailToRead,
453                         BufferLen); */
454
455                 spin_unlock_irqrestore(&InRingInfo->ring_lock, flags);
456
457                 return -1;
458         }
459
460         /* Convert to byte offset */
461         nextReadLocation = GetNextReadLocation(InRingInfo);
462
463         nextReadLocation = CopyFromRingBuffer(InRingInfo,
464                                                 Buffer,
465                                                 BufferLen,
466                                                 nextReadLocation);
467
468         spin_unlock_irqrestore(&InRingInfo->ring_lock, flags);
469
470         return 0;
471 }
472
473
474 /*++
475
476 Name:
477         RingBufferRead()
478
479 Description:
480         Read and advance the read index
481
482 --*/
483 int RingBufferRead(RING_BUFFER_INFO *InRingInfo, void *Buffer,
484                    u32 BufferLen, u32 Offset)
485 {
486         u32 bytesAvailToWrite;
487         u32 bytesAvailToRead;
488         u32 nextReadLocation = 0;
489         u64 prevIndices = 0;
490         unsigned long flags;
491
492         ASSERT(BufferLen > 0);
493
494         spin_lock_irqsave(&InRingInfo->ring_lock, flags);
495
496         GetRingBufferAvailBytes(InRingInfo,
497                                 &bytesAvailToRead,
498                                 &bytesAvailToWrite);
499
500         DPRINT_DBG(VMBUS, "Reading %u bytes...", BufferLen);
501
502         /* DumpRingInfo(InRingInfo, "BEFORE "); */
503
504         /* Make sure there is something to read */
505         if (bytesAvailToRead < BufferLen) {
506                 DPRINT_DBG(VMBUS,
507                         "got callback but not enough to read "
508                         "<avail to read %d read size %d>!!",
509                         bytesAvailToRead,
510                         BufferLen);
511
512                 spin_unlock_irqrestore(&InRingInfo->ring_lock, flags);
513
514                 return -1;
515         }
516
517         nextReadLocation = GetNextReadLocationWithOffset(InRingInfo, Offset);
518
519         nextReadLocation = CopyFromRingBuffer(InRingInfo,
520                                                 Buffer,
521                                                 BufferLen,
522                                                 nextReadLocation);
523
524         nextReadLocation = CopyFromRingBuffer(InRingInfo,
525                                                 &prevIndices,
526                                                 sizeof(u64),
527                                                 nextReadLocation);
528
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 */
531         /*is updated */
532         mb();
533
534         /* Update the read index */
535         SetNextReadLocation(InRingInfo, nextReadLocation);
536
537         /* DumpRingInfo(InRingInfo, "AFTER "); */
538
539         spin_unlock_irqrestore(&InRingInfo->ring_lock, flags);
540
541         return 0;
542 }
543
544
545 /*++
546
547 Name:
548         CopyToRingBuffer()
549
550 Description:
551         Helper routine to copy from source to ring buffer.
552         Assume there is enough room. Handles wrap-around in dest case only!!
553
554 --*/
555 static u32
556 CopyToRingBuffer(
557         RING_BUFFER_INFO        *RingInfo,
558         u32                             StartWriteOffset,
559         void                            *Src,
560         u32                             SrcLen)
561 {
562         void *ringBuffer = GetRingBuffer(RingInfo);
563         u32 ringBufferSize = GetRingBufferSize(RingInfo);
564         u32 fragLen;
565
566         /* wrap-around detected! */
567         if (SrcLen > ringBufferSize - StartWriteOffset) {
568                 DPRINT_DBG(VMBUS, "wrap-around detected!");
569
570                 fragLen = ringBufferSize - StartWriteOffset;
571                 memcpy(ringBuffer + StartWriteOffset, Src, fragLen);
572                 memcpy(ringBuffer, Src + fragLen, SrcLen - fragLen);
573         } else
574                 memcpy(ringBuffer + StartWriteOffset, Src, SrcLen);
575
576         StartWriteOffset += SrcLen;
577         StartWriteOffset %= ringBufferSize;
578
579         return StartWriteOffset;
580 }
581
582
583 /*++
584
585 Name:
586         CopyFromRingBuffer()
587
588 Description:
589         Helper routine to copy to source from ring buffer.
590         Assume there is enough room. Handles wrap-around in src case only!!
591
592 --*/
593 static u32
594 CopyFromRingBuffer(
595         RING_BUFFER_INFO        *RingInfo,
596         void                            *Dest,
597         u32                             DestLen,
598         u32                             StartReadOffset)
599 {
600         void *ringBuffer = GetRingBuffer(RingInfo);
601         u32 ringBufferSize = GetRingBufferSize(RingInfo);
602
603         u32 fragLen;
604
605         /* wrap-around detected at the src */
606         if (DestLen > ringBufferSize - StartReadOffset) {
607                 DPRINT_DBG(VMBUS, "src wrap-around detected!");
608
609                 fragLen = ringBufferSize - StartReadOffset;
610
611                 memcpy(Dest, ringBuffer + StartReadOffset, fragLen);
612                 memcpy(Dest + fragLen, ringBuffer, DestLen - fragLen);
613         } else
614
615                 memcpy(Dest, ringBuffer + StartReadOffset, DestLen);
616
617
618         StartReadOffset += DestLen;
619         StartReadOffset %= ringBufferSize;
620
621         return StartReadOffset;
622 }
623
624
625 /* eof */