staging: hv: Convert camel cased struct fields in ring_buffer.h to lower cases
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / staging / hv / ring_buffer.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 "ring_buffer.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(struct hv_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->ring_buffer->read_index;
55 write_loc = rbi->ring_buffer->write_index;
56
57 *write = BYTES_AVAIL_TO_WRITE(read_loc, write_loc, rbi->ring_datasize);
58 *read = rbi->ring_datasize - *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(struct hv_ring_buffer_info *RingInfo)
72 {
73 u32 next = RingInfo->ring_buffer->write_index;
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(struct hv_ring_buffer_info *RingInfo,
91 u32 NextWriteLocation)
92 {
93 RingInfo->ring_buffer->write_index = NextWriteLocation;
94 }
95
96 /*++
97
98 Name:
99 GetNextReadLocation()
100
101 Description:
102 Get the next read location for the specified ring buffer
103
104 --*/
105 static inline u32
106 GetNextReadLocation(struct hv_ring_buffer_info *RingInfo)
107 {
108 u32 next = RingInfo->ring_buffer->read_index;
109
110 /* ASSERT(next < RingInfo->RingDataSize); */
111
112 return next;
113 }
114
115 /*++
116
117 Name:
118 GetNextReadLocationWithOffset()
119
120 Description:
121 Get the next read location + offset for the specified ring buffer.
122 This allows the caller to skip
123
124 --*/
125 static inline u32
126 GetNextReadLocationWithOffset(struct hv_ring_buffer_info *RingInfo, u32 Offset)
127 {
128 u32 next = RingInfo->ring_buffer->read_index;
129
130 /* ASSERT(next < RingInfo->RingDataSize); */
131 next += Offset;
132 next %= RingInfo->ring_datasize;
133
134 return next;
135 }
136
137 /*++
138
139 Name:
140 SetNextReadLocation()
141
142 Description:
143 Set the next read location for the specified ring buffer
144
145 --*/
146 static inline void
147 SetNextReadLocation(struct hv_ring_buffer_info *RingInfo, u32 NextReadLocation)
148 {
149 RingInfo->ring_buffer->read_index = NextReadLocation;
150 }
151
152
153 /*++
154
155 Name:
156 GetRingBuffer()
157
158 Description:
159 Get the start of the ring buffer
160
161 --*/
162 static inline void *
163 GetRingBuffer(struct hv_ring_buffer_info *RingInfo)
164 {
165 return (void *)RingInfo->ring_buffer->buffer;
166 }
167
168
169 /*++
170
171 Name:
172 GetRingBufferSize()
173
174 Description:
175 Get the size of the ring buffer
176
177 --*/
178 static inline u32
179 GetRingBufferSize(struct hv_ring_buffer_info *RingInfo)
180 {
181 return RingInfo->ring_datasize;
182 }
183
184 /*++
185
186 Name:
187 GetRingBufferIndices()
188
189 Description:
190 Get the read and write indices as u64 of the specified ring buffer
191
192 --*/
193 static inline u64
194 GetRingBufferIndices(struct hv_ring_buffer_info *RingInfo)
195 {
196 return (u64)RingInfo->ring_buffer->write_index << 32;
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(struct hv_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->ring_buffer->buffer,
225 bytesAvailToWrite,
226 bytesAvailToRead,
227 RingInfo->ring_buffer->read_index,
228 RingInfo->ring_buffer->write_index);
229 }
230
231
232 /* Internal routines */
233
234 static u32
235 CopyToRingBuffer(
236 struct hv_ring_buffer_info *RingInfo,
237 u32 StartWriteOffset,
238 void *Src,
239 u32 SrcLen);
240
241 static u32
242 CopyFromRingBuffer(
243 struct hv_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(struct hv_ring_buffer_info *RingInfo,
260 struct hv_ring_buffer_debug_info *debug_info)
261 {
262 u32 bytesAvailToWrite;
263 u32 bytesAvailToRead;
264
265 if (RingInfo->ring_buffer) {
266 GetRingBufferAvailBytes(RingInfo,
267 &bytesAvailToRead,
268 &bytesAvailToWrite);
269
270 debug_info->bytes_avail_toread = bytesAvailToRead;
271 debug_info->bytes_avail_towrite = bytesAvailToWrite;
272 debug_info->current_read_index =
273 RingInfo->ring_buffer->read_index;
274 debug_info->current_write_index =
275 RingInfo->ring_buffer->write_index;
276 debug_info->current_interrupt_mask =
277 RingInfo->ring_buffer->interrupt_mask;
278 }
279 }
280
281
282 /*++
283
284 Name:
285 GetRingBufferInterruptMask()
286
287 Description:
288 Get the interrupt mask for the specified ring buffer
289
290 --*/
291 u32 GetRingBufferInterruptMask(struct hv_ring_buffer_info *rbi)
292 {
293 return rbi->ring_buffer->interrupt_mask;
294 }
295
296 /*++
297
298 Name:
299 RingBufferInit()
300
301 Description:
302 Initialize the ring buffer
303
304 --*/
305 int RingBufferInit(struct hv_ring_buffer_info *RingInfo, void *Buffer, u32 BufferLen)
306 {
307 if (sizeof(struct hv_ring_buffer) != PAGE_SIZE)
308 return -EINVAL;
309
310 memset(RingInfo, 0, sizeof(struct hv_ring_buffer_info));
311
312 RingInfo->ring_buffer = (struct hv_ring_buffer *)Buffer;
313 RingInfo->ring_buffer->read_index =
314 RingInfo->ring_buffer->write_index = 0;
315
316 RingInfo->ring_size = BufferLen;
317 RingInfo->ring_datasize = BufferLen - sizeof(struct hv_ring_buffer);
318
319 spin_lock_init(&RingInfo->ring_lock);
320
321 return 0;
322 }
323
324 /*++
325
326 Name:
327 RingBufferCleanup()
328
329 Description:
330 Cleanup the ring buffer
331
332 --*/
333 void RingBufferCleanup(struct hv_ring_buffer_info *RingInfo)
334 {
335 }
336
337 /*++
338
339 Name:
340 RingBufferWrite()
341
342 Description:
343 Write to the ring buffer
344
345 --*/
346 int RingBufferWrite(struct hv_ring_buffer_info *OutRingInfo,
347 struct scatterlist *sglist, u32 sgcount)
348 {
349 int i = 0;
350 u32 byteAvailToWrite;
351 u32 byteAvailToRead;
352 u32 totalBytesToWrite = 0;
353
354 struct scatterlist *sg;
355 volatile u32 nextWriteLocation;
356 u64 prevIndices = 0;
357 unsigned long flags;
358
359 for_each_sg(sglist, sg, sgcount, i)
360 {
361 totalBytesToWrite += sg->length;
362 }
363
364 totalBytesToWrite += sizeof(u64);
365
366 spin_lock_irqsave(&OutRingInfo->ring_lock, flags);
367
368 GetRingBufferAvailBytes(OutRingInfo,
369 &byteAvailToRead,
370 &byteAvailToWrite);
371
372 DPRINT_DBG(VMBUS, "Writing %u bytes...", totalBytesToWrite);
373
374 /* DumpRingInfo(OutRingInfo, "BEFORE "); */
375
376 /* If there is only room for the packet, assume it is full. */
377 /* Otherwise, the next time around, we think the ring buffer */
378 /* is empty since the read index == write index */
379 if (byteAvailToWrite <= totalBytesToWrite) {
380 DPRINT_DBG(VMBUS,
381 "No more space left on outbound ring buffer "
382 "(needed %u, avail %u)",
383 totalBytesToWrite,
384 byteAvailToWrite);
385
386 spin_unlock_irqrestore(&OutRingInfo->ring_lock, flags);
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 return 0;
419 }
420
421
422 /*++
423
424 Name:
425 RingBufferPeek()
426
427 Description:
428 Read without advancing the read index
429
430 --*/
431 int RingBufferPeek(struct hv_ring_buffer_info *InRingInfo, void *Buffer, u32 BufferLen)
432 {
433 u32 bytesAvailToWrite;
434 u32 bytesAvailToRead;
435 u32 nextReadLocation = 0;
436 unsigned long flags;
437
438 spin_lock_irqsave(&InRingInfo->ring_lock, flags);
439
440 GetRingBufferAvailBytes(InRingInfo,
441 &bytesAvailToRead,
442 &bytesAvailToWrite);
443
444 /* Make sure there is something to read */
445 if (bytesAvailToRead < BufferLen) {
446 /* DPRINT_DBG(VMBUS,
447 "got callback but not enough to read "
448 "<avail to read %d read size %d>!!",
449 bytesAvailToRead,
450 BufferLen); */
451
452 spin_unlock_irqrestore(&InRingInfo->ring_lock, flags);
453
454 return -1;
455 }
456
457 /* Convert to byte offset */
458 nextReadLocation = GetNextReadLocation(InRingInfo);
459
460 nextReadLocation = CopyFromRingBuffer(InRingInfo,
461 Buffer,
462 BufferLen,
463 nextReadLocation);
464
465 spin_unlock_irqrestore(&InRingInfo->ring_lock, flags);
466
467 return 0;
468 }
469
470
471 /*++
472
473 Name:
474 RingBufferRead()
475
476 Description:
477 Read and advance the read index
478
479 --*/
480 int RingBufferRead(struct hv_ring_buffer_info *InRingInfo, void *Buffer,
481 u32 BufferLen, u32 Offset)
482 {
483 u32 bytesAvailToWrite;
484 u32 bytesAvailToRead;
485 u32 nextReadLocation = 0;
486 u64 prevIndices = 0;
487 unsigned long flags;
488
489 if (BufferLen <= 0)
490 return -EINVAL;
491
492 spin_lock_irqsave(&InRingInfo->ring_lock, flags);
493
494 GetRingBufferAvailBytes(InRingInfo,
495 &bytesAvailToRead,
496 &bytesAvailToWrite);
497
498 DPRINT_DBG(VMBUS, "Reading %u bytes...", BufferLen);
499
500 /* DumpRingInfo(InRingInfo, "BEFORE "); */
501
502 /* Make sure there is something to read */
503 if (bytesAvailToRead < BufferLen) {
504 DPRINT_DBG(VMBUS,
505 "got callback but not enough to read "
506 "<avail to read %d read size %d>!!",
507 bytesAvailToRead,
508 BufferLen);
509
510 spin_unlock_irqrestore(&InRingInfo->ring_lock, flags);
511
512 return -1;
513 }
514
515 nextReadLocation = GetNextReadLocationWithOffset(InRingInfo, Offset);
516
517 nextReadLocation = CopyFromRingBuffer(InRingInfo,
518 Buffer,
519 BufferLen,
520 nextReadLocation);
521
522 nextReadLocation = CopyFromRingBuffer(InRingInfo,
523 &prevIndices,
524 sizeof(u64),
525 nextReadLocation);
526
527 /* Make sure all reads are done before we update the read index since */
528 /* the writer may start writing to the read area once the read index */
529 /*is updated */
530 mb();
531
532 /* Update the read index */
533 SetNextReadLocation(InRingInfo, nextReadLocation);
534
535 /* DumpRingInfo(InRingInfo, "AFTER "); */
536
537 spin_unlock_irqrestore(&InRingInfo->ring_lock, flags);
538
539 return 0;
540 }
541
542
543 /*++
544
545 Name:
546 CopyToRingBuffer()
547
548 Description:
549 Helper routine to copy from source to ring buffer.
550 Assume there is enough room. Handles wrap-around in dest case only!!
551
552 --*/
553 static u32
554 CopyToRingBuffer(
555 struct hv_ring_buffer_info *RingInfo,
556 u32 StartWriteOffset,
557 void *Src,
558 u32 SrcLen)
559 {
560 void *ringBuffer = GetRingBuffer(RingInfo);
561 u32 ringBufferSize = GetRingBufferSize(RingInfo);
562 u32 fragLen;
563
564 /* wrap-around detected! */
565 if (SrcLen > ringBufferSize - StartWriteOffset) {
566 DPRINT_DBG(VMBUS, "wrap-around detected!");
567
568 fragLen = ringBufferSize - StartWriteOffset;
569 memcpy(ringBuffer + StartWriteOffset, Src, fragLen);
570 memcpy(ringBuffer, Src + fragLen, SrcLen - fragLen);
571 } else
572 memcpy(ringBuffer + StartWriteOffset, Src, SrcLen);
573
574 StartWriteOffset += SrcLen;
575 StartWriteOffset %= ringBufferSize;
576
577 return StartWriteOffset;
578 }
579
580
581 /*++
582
583 Name:
584 CopyFromRingBuffer()
585
586 Description:
587 Helper routine to copy to source from ring buffer.
588 Assume there is enough room. Handles wrap-around in src case only!!
589
590 --*/
591 static u32
592 CopyFromRingBuffer(
593 struct hv_ring_buffer_info *RingInfo,
594 void *Dest,
595 u32 DestLen,
596 u32 StartReadOffset)
597 {
598 void *ringBuffer = GetRingBuffer(RingInfo);
599 u32 ringBufferSize = GetRingBufferSize(RingInfo);
600
601 u32 fragLen;
602
603 /* wrap-around detected at the src */
604 if (DestLen > ringBufferSize - StartReadOffset) {
605 DPRINT_DBG(VMBUS, "src wrap-around detected!");
606
607 fragLen = ringBufferSize - StartReadOffset;
608
609 memcpy(Dest, ringBuffer + StartReadOffset, fragLen);
610 memcpy(Dest + fragLen, ringBuffer, DestLen - fragLen);
611 } else
612
613 memcpy(Dest, ringBuffer + StartReadOffset, DestLen);
614
615
616 StartReadOffset += DestLen;
617 StartReadOffset %= ringBufferSize;
618
619 return StartReadOffset;
620 }
621
622
623 /* eof */