Staging: hv: Get rid of the forward declaration of hv_copyfrom_ringbuffer
[deliverable/linux.git] / drivers / staging / hv / ring_buffer.c
CommitLineData
3e7ee490
HJ
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>
b2a5a585 21 * K. Y. Srinivasan <kys@microsoft.com>
3e7ee490
HJ
22 *
23 */
0a46618d 24#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
3e7ee490 25
a0086dc5
GKH
26#include <linux/kernel.h>
27#include <linux/mm.h>
645954c5 28#include "logging.h"
8f078ca6 29#include "ring_buffer.h"
3e7ee490 30
3e7ee490 31
454f18a9
BP
32/* #defines */
33
34
35/* Amount of space to write to */
0686e4f4 36#define BYTES_AVAIL_TO_WRITE(r, w, z) ((w) >= (r)) ? ((z) - ((w) - (r))) : ((r) - (w))
3e7ee490
HJ
37
38
b2a5a585
S
39/*
40 *
41 * hv_get_ringbuffer_availbytes()
42 *
43 * Get number of bytes available to read and to write to
44 * for the specified ring buffer
45 */
3e7ee490 46static inline void
2b8a912e 47hv_get_ringbuffer_availbytes(struct hv_ring_buffer_info *rbi,
1ac58644 48 u32 *read, u32 *write)
3e7ee490 49{
4408f531 50 u32 read_loc, write_loc;
3e7ee490 51
454f18a9 52 /* Capture the read/write indices before they changed */
82f8bd40
HZ
53 read_loc = rbi->ring_buffer->read_index;
54 write_loc = rbi->ring_buffer->write_index;
3e7ee490 55
82f8bd40
HZ
56 *write = BYTES_AVAIL_TO_WRITE(read_loc, write_loc, rbi->ring_datasize);
57 *read = rbi->ring_datasize - *write;
3e7ee490
HJ
58}
59
b2a5a585
S
60/*
61 * hv_get_next_write_location()
62 *
63 * Get the next write location for the specified ring buffer
64 *
65 */
4d643114 66static inline u32
2b8a912e 67hv_get_next_write_location(struct hv_ring_buffer_info *ring_info)
3e7ee490 68{
fc8c72eb 69 u32 next = ring_info->ring_buffer->write_index;
3e7ee490 70
3e7ee490
HJ
71 return next;
72}
73
b2a5a585
S
74/*
75 * hv_set_next_write_location()
76 *
77 * Set the next write location for the specified ring buffer
78 *
79 */
3e7ee490 80static inline void
2b8a912e 81hv_set_next_write_location(struct hv_ring_buffer_info *ring_info,
fc8c72eb 82 u32 next_write_location)
3e7ee490 83{
fc8c72eb 84 ring_info->ring_buffer->write_index = next_write_location;
3e7ee490
HJ
85}
86
b2a5a585
S
87/*
88 * hv_get_next_read_location()
89 *
90 * Get the next read location for the specified ring buffer
91 */
4d643114 92static inline u32
2b8a912e 93hv_get_next_read_location(struct hv_ring_buffer_info *ring_info)
3e7ee490 94{
fc8c72eb 95 u32 next = ring_info->ring_buffer->read_index;
3e7ee490 96
3e7ee490
HJ
97 return next;
98}
99
b2a5a585
S
100/*
101 * hv_get_next_readlocation_withoffset()
102 *
103 * Get the next read location + offset for the specified ring buffer.
104 * This allows the caller to skip
105 */
4d643114 106static inline u32
2b8a912e 107hv_get_next_readlocation_withoffset(struct hv_ring_buffer_info *ring_info,
1ac58644 108 u32 offset)
3e7ee490 109{
fc8c72eb 110 u32 next = ring_info->ring_buffer->read_index;
3e7ee490 111
fc8c72eb
HZ
112 next += offset;
113 next %= ring_info->ring_datasize;
3e7ee490
HJ
114
115 return next;
116}
117
b2a5a585
S
118/*
119 *
120 * hv_set_next_read_location()
121 *
122 * Set the next read location for the specified ring buffer
123 *
124 */
3e7ee490 125static inline void
2b8a912e 126hv_set_next_read_location(struct hv_ring_buffer_info *ring_info,
fc8c72eb 127 u32 next_read_location)
3e7ee490 128{
fc8c72eb 129 ring_info->ring_buffer->read_index = next_read_location;
3e7ee490
HJ
130}
131
132
b2a5a585
S
133/*
134 *
135 * hv_get_ring_buffer()
136 *
137 * Get the start of the ring buffer
138 */
8282c400 139static inline void *
2b8a912e 140hv_get_ring_buffer(struct hv_ring_buffer_info *ring_info)
3e7ee490 141{
fc8c72eb 142 return (void *)ring_info->ring_buffer->buffer;
3e7ee490
HJ
143}
144
145
b2a5a585
S
146/*
147 *
148 * hv_get_ring_buffersize()
149 *
150 * Get the size of the ring buffer
151 */
4d643114 152static inline u32
2b8a912e 153hv_get_ring_buffersize(struct hv_ring_buffer_info *ring_info)
3e7ee490 154{
fc8c72eb 155 return ring_info->ring_datasize;
3e7ee490
HJ
156}
157
b2a5a585
S
158/*
159 *
160 * hv_get_ring_bufferindices()
161 *
162 * Get the read and write indices as u64 of the specified ring buffer
163 *
164 */
59471438 165static inline u64
2b8a912e 166hv_get_ring_bufferindices(struct hv_ring_buffer_info *ring_info)
3e7ee490 167{
fc8c72eb 168 return (u64)ring_info->ring_buffer->write_index << 32;
3e7ee490
HJ
169}
170
171
b2a5a585
S
172/*
173 *
174 * hv_dump_ring_info()
175 *
176 * Dump out to console the ring buffer info
177 *
178 */
549bf93f 179void hv_dump_ring_info(struct hv_ring_buffer_info *ring_info, char *prefix)
3e7ee490 180{
fc8c72eb
HZ
181 u32 bytes_avail_towrite;
182 u32 bytes_avail_toread;
3e7ee490 183
2b8a912e 184 hv_get_ringbuffer_availbytes(ring_info,
fc8c72eb
HZ
185 &bytes_avail_toread,
186 &bytes_avail_towrite);
3e7ee490 187
4408f531
B
188 DPRINT(VMBUS,
189 DEBUG_RING_LVL,
190 "%s <<ringinfo %p buffer %p avail write %u "
191 "avail read %u read idx %u write idx %u>>",
fc8c72eb
HZ
192 prefix,
193 ring_info,
194 ring_info->ring_buffer->buffer,
195 bytes_avail_towrite,
196 bytes_avail_toread,
197 ring_info->ring_buffer->read_index,
198 ring_info->ring_buffer->write_index);
3e7ee490
HJ
199}
200
454f18a9 201
8f1136ae
S
202/*
203 *
204 * hv_copyfrom_ringbuffer()
205 *
206 * Helper routine to copy to source from ring buffer.
207 * Assume there is enough room. Handles wrap-around in src case only!!
208 *
209 */
210static u32 hv_copyfrom_ringbuffer(
211 struct hv_ring_buffer_info *ring_info,
212 void *dest,
213 u32 destlen,
214 u32 start_read_offset)
215{
216 void *ring_buffer = hv_get_ring_buffer(ring_info);
217 u32 ring_buffer_size = hv_get_ring_buffersize(ring_info);
218
219 u32 frag_len;
220
221 /* wrap-around detected at the src */
222 if (destlen > ring_buffer_size - start_read_offset) {
223 frag_len = ring_buffer_size - start_read_offset;
224
225 memcpy(dest, ring_buffer + start_read_offset, frag_len);
226 memcpy(dest + frag_len, ring_buffer, destlen - frag_len);
227 } else
228
229 memcpy(dest, ring_buffer + start_read_offset, destlen);
230
231
232 start_read_offset += destlen;
233 start_read_offset %= ring_buffer_size;
234
235 return start_read_offset;
236}
237
238
454f18a9 239
4d643114 240static u32
2b8a912e 241hv_copyto_ringbuffer(
fc8c72eb
HZ
242 struct hv_ring_buffer_info *ring_info,
243 u32 start_write_offset,
244 void *src,
245 u32 srclen);
3e7ee490 246
3e7ee490 247
b2a5a585
S
248/*
249 *
250 * hv_ringbuffer_get_debuginfo()
251 *
252 * Get various debug metrics for the specified ring buffer
253 *
254 */
a75b61d5 255void hv_ringbuffer_get_debuginfo(struct hv_ring_buffer_info *ring_info,
80682b7a 256 struct hv_ring_buffer_debug_info *debug_info)
3e7ee490 257{
fc8c72eb
HZ
258 u32 bytes_avail_towrite;
259 u32 bytes_avail_toread;
3e7ee490 260
fc8c72eb 261 if (ring_info->ring_buffer) {
2b8a912e 262 hv_get_ringbuffer_availbytes(ring_info,
fc8c72eb
HZ
263 &bytes_avail_toread,
264 &bytes_avail_towrite);
3e7ee490 265
fc8c72eb
HZ
266 debug_info->bytes_avail_toread = bytes_avail_toread;
267 debug_info->bytes_avail_towrite = bytes_avail_towrite;
82f8bd40 268 debug_info->current_read_index =
fc8c72eb 269 ring_info->ring_buffer->read_index;
82f8bd40 270 debug_info->current_write_index =
fc8c72eb 271 ring_info->ring_buffer->write_index;
82f8bd40 272 debug_info->current_interrupt_mask =
fc8c72eb 273 ring_info->ring_buffer->interrupt_mask;
3e7ee490
HJ
274 }
275}
276
277
b2a5a585
S
278/*
279 *
280 * hv_get_ringbuffer_interrupt_mask()
281 *
282 * Get the interrupt mask for the specified ring buffer
283 *
284 */
decc49da 285u32 hv_get_ringbuffer_interrupt_mask(struct hv_ring_buffer_info *rbi)
3e7ee490 286{
82f8bd40 287 return rbi->ring_buffer->interrupt_mask;
3e7ee490
HJ
288}
289
b2a5a585
S
290/*
291 *
292 * hv_ringbuffer_init()
293 *
294 *Initialize the ring buffer
295 *
296 */
72a95cbc 297int hv_ringbuffer_init(struct hv_ring_buffer_info *ring_info,
fc8c72eb 298 void *buffer, u32 buflen)
3e7ee490 299{
4a1b3acc 300 if (sizeof(struct hv_ring_buffer) != PAGE_SIZE)
3324fb40 301 return -EINVAL;
3e7ee490 302
fc8c72eb 303 memset(ring_info, 0, sizeof(struct hv_ring_buffer_info));
3e7ee490 304
fc8c72eb
HZ
305 ring_info->ring_buffer = (struct hv_ring_buffer *)buffer;
306 ring_info->ring_buffer->read_index =
307 ring_info->ring_buffer->write_index = 0;
3e7ee490 308
fc8c72eb
HZ
309 ring_info->ring_size = buflen;
310 ring_info->ring_datasize = buflen - sizeof(struct hv_ring_buffer);
3e7ee490 311
fc8c72eb 312 spin_lock_init(&ring_info->ring_lock);
3e7ee490
HJ
313
314 return 0;
315}
316
b2a5a585
S
317/*
318 *
319 * hv_ringbuffer_cleanup()
320 *
321 * Cleanup the ring buffer
322 *
323 */
2dba688b 324void hv_ringbuffer_cleanup(struct hv_ring_buffer_info *ring_info)
3e7ee490 325{
3e7ee490
HJ
326}
327
b2a5a585
S
328/*
329 *
330 * hv_ringbuffer_write()
331 *
332 * Write to the ring buffer
333 *
334 */
633c4dce 335int hv_ringbuffer_write(struct hv_ring_buffer_info *outring_info,
3523a805 336 struct scatterlist *sglist, u32 sgcount)
3e7ee490 337{
4408f531 338 int i = 0;
fc8c72eb
HZ
339 u32 bytes_avail_towrite;
340 u32 bytes_avail_toread;
341 u32 totalbytes_towrite = 0;
3e7ee490 342
b219b3f7 343 struct scatterlist *sg;
fc8c72eb
HZ
344 volatile u32 next_write_location;
345 u64 prev_indices = 0;
a98f96ee 346 unsigned long flags;
3e7ee490 347
b219b3f7 348 for_each_sg(sglist, sg, sgcount, i)
3e7ee490 349 {
fc8c72eb 350 totalbytes_towrite += sg->length;
3e7ee490
HJ
351 }
352
fc8c72eb 353 totalbytes_towrite += sizeof(u64);
3e7ee490 354
fc8c72eb 355 spin_lock_irqsave(&outring_info->ring_lock, flags);
3e7ee490 356
2b8a912e 357 hv_get_ringbuffer_availbytes(outring_info,
fc8c72eb
HZ
358 &bytes_avail_toread,
359 &bytes_avail_towrite);
3e7ee490 360
3e7ee490 361
4408f531
B
362 /* If there is only room for the packet, assume it is full. */
363 /* Otherwise, the next time around, we think the ring buffer */
454f18a9 364 /* is empty since the read index == write index */
fc8c72eb 365 if (bytes_avail_towrite <= totalbytes_towrite) {
fc8c72eb 366 spin_unlock_irqrestore(&outring_info->ring_lock, flags);
3e7ee490
HJ
367 return -1;
368 }
369
454f18a9 370 /* Write to the ring buffer */
2b8a912e 371 next_write_location = hv_get_next_write_location(outring_info);
3e7ee490 372
b219b3f7 373 for_each_sg(sglist, sg, sgcount, i)
3e7ee490 374 {
2b8a912e 375 next_write_location = hv_copyto_ringbuffer(outring_info,
fc8c72eb 376 next_write_location,
b219b3f7
NP
377 sg_virt(sg),
378 sg->length);
3e7ee490
HJ
379 }
380
454f18a9 381 /* Set previous packet start */
2b8a912e 382 prev_indices = hv_get_ring_bufferindices(outring_info);
3e7ee490 383
2b8a912e 384 next_write_location = hv_copyto_ringbuffer(outring_info,
fc8c72eb
HZ
385 next_write_location,
386 &prev_indices,
b219b3f7 387 sizeof(u64));
3e7ee490 388
454f18a9 389 /* Make sure we flush all writes before updating the writeIndex */
28b6ca9c 390 mb();
3e7ee490 391
454f18a9 392 /* Now, update the write location */
2b8a912e 393 hv_set_next_write_location(outring_info, next_write_location);
3e7ee490 394
3e7ee490 395
fc8c72eb 396 spin_unlock_irqrestore(&outring_info->ring_lock, flags);
3e7ee490
HJ
397 return 0;
398}
399
400
b2a5a585
S
401/*
402 *
403 * hv_ringbuffer_peek()
404 *
405 * Read without advancing the read index
406 *
407 */
a89186c2 408int hv_ringbuffer_peek(struct hv_ring_buffer_info *Inring_info,
fc8c72eb 409 void *Buffer, u32 buflen)
3e7ee490 410{
fc8c72eb
HZ
411 u32 bytes_avail_towrite;
412 u32 bytes_avail_toread;
413 u32 next_read_location = 0;
a98f96ee 414 unsigned long flags;
3e7ee490 415
fc8c72eb 416 spin_lock_irqsave(&Inring_info->ring_lock, flags);
3e7ee490 417
2b8a912e 418 hv_get_ringbuffer_availbytes(Inring_info,
fc8c72eb
HZ
419 &bytes_avail_toread,
420 &bytes_avail_towrite);
3e7ee490 421
454f18a9 422 /* Make sure there is something to read */
fc8c72eb 423 if (bytes_avail_toread < buflen) {
3e7ee490 424
fc8c72eb 425 spin_unlock_irqrestore(&Inring_info->ring_lock, flags);
3e7ee490
HJ
426
427 return -1;
428 }
429
454f18a9 430 /* Convert to byte offset */
2b8a912e 431 next_read_location = hv_get_next_read_location(Inring_info);
3e7ee490 432
2b8a912e 433 next_read_location = hv_copyfrom_ringbuffer(Inring_info,
4408f531 434 Buffer,
fc8c72eb
HZ
435 buflen,
436 next_read_location);
3e7ee490 437
fc8c72eb 438 spin_unlock_irqrestore(&Inring_info->ring_lock, flags);
3e7ee490
HJ
439
440 return 0;
441}
442
443
b2a5a585
S
444/*
445 *
446 * hv_ringbuffer_read()
447 *
448 * Read and advance the read index
449 *
450 */
38397c8a 451int hv_ringbuffer_read(struct hv_ring_buffer_info *inring_info, void *buffer,
fc8c72eb 452 u32 buflen, u32 offset)
3e7ee490 453{
fc8c72eb
HZ
454 u32 bytes_avail_towrite;
455 u32 bytes_avail_toread;
456 u32 next_read_location = 0;
457 u64 prev_indices = 0;
a98f96ee 458 unsigned long flags;
3e7ee490 459
fc8c72eb 460 if (buflen <= 0)
a16e1485 461 return -EINVAL;
3e7ee490 462
fc8c72eb 463 spin_lock_irqsave(&inring_info->ring_lock, flags);
3e7ee490 464
2b8a912e 465 hv_get_ringbuffer_availbytes(inring_info,
fc8c72eb
HZ
466 &bytes_avail_toread,
467 &bytes_avail_towrite);
3e7ee490 468
454f18a9 469 /* Make sure there is something to read */
fc8c72eb 470 if (bytes_avail_toread < buflen) {
fc8c72eb 471 spin_unlock_irqrestore(&inring_info->ring_lock, flags);
3e7ee490
HJ
472
473 return -1;
474 }
475
1ac58644 476 next_read_location =
2b8a912e 477 hv_get_next_readlocation_withoffset(inring_info, offset);
3e7ee490 478
2b8a912e 479 next_read_location = hv_copyfrom_ringbuffer(inring_info,
fc8c72eb
HZ
480 buffer,
481 buflen,
482 next_read_location);
3e7ee490 483
2b8a912e 484 next_read_location = hv_copyfrom_ringbuffer(inring_info,
fc8c72eb 485 &prev_indices,
4408f531 486 sizeof(u64),
fc8c72eb 487 next_read_location);
3e7ee490 488
454f18a9 489 /* Make sure all reads are done before we update the read index since */
4408f531
B
490 /* the writer may start writing to the read area once the read index */
491 /*is updated */
28b6ca9c 492 mb();
3e7ee490 493
454f18a9 494 /* Update the read index */
2b8a912e 495 hv_set_next_read_location(inring_info, next_read_location);
3e7ee490 496
fc8c72eb 497 spin_unlock_irqrestore(&inring_info->ring_lock, flags);
3e7ee490
HJ
498
499 return 0;
500}
501
502
b2a5a585
S
503/*
504 *
505 * hv_copyto_ringbuffer()
506 *
507 * Helper routine to copy from source to ring buffer.
508 * Assume there is enough room. Handles wrap-around in dest case only!!
509 *
510 */
bd1de709 511static u32
2b8a912e 512hv_copyto_ringbuffer(
fc8c72eb
HZ
513 struct hv_ring_buffer_info *ring_info,
514 u32 start_write_offset,
515 void *src,
516 u32 srclen)
3e7ee490 517{
2b8a912e
S
518 void *ring_buffer = hv_get_ring_buffer(ring_info);
519 u32 ring_buffer_size = hv_get_ring_buffersize(ring_info);
fc8c72eb 520 u32 frag_len;
3e7ee490 521
4408f531 522 /* wrap-around detected! */
fc8c72eb 523 if (srclen > ring_buffer_size - start_write_offset) {
fc8c72eb
HZ
524 frag_len = ring_buffer_size - start_write_offset;
525 memcpy(ring_buffer + start_write_offset, src, frag_len);
526 memcpy(ring_buffer, src + frag_len, srclen - frag_len);
4408f531 527 } else
fc8c72eb 528 memcpy(ring_buffer + start_write_offset, src, srclen);
3e7ee490 529
fc8c72eb
HZ
530 start_write_offset += srclen;
531 start_write_offset %= ring_buffer_size;
3e7ee490 532
fc8c72eb 533 return start_write_offset;
3e7ee490
HJ
534}
535
This page took 0.213114 seconds and 5 git commands to generate.