Staging: hv: Get rid of the forward declaration of hv_copyto_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
7581578d
S
239/*
240 *
241 * hv_copyto_ringbuffer()
242 *
243 * Helper routine to copy from source to ring buffer.
244 * Assume there is enough room. Handles wrap-around in dest case only!!
245 *
246 */
247static u32 hv_copyto_ringbuffer(
fc8c72eb
HZ
248 struct hv_ring_buffer_info *ring_info,
249 u32 start_write_offset,
250 void *src,
7581578d
S
251 u32 srclen)
252{
253 void *ring_buffer = hv_get_ring_buffer(ring_info);
254 u32 ring_buffer_size = hv_get_ring_buffersize(ring_info);
255 u32 frag_len;
256
257 /* wrap-around detected! */
258 if (srclen > ring_buffer_size - start_write_offset) {
259 frag_len = ring_buffer_size - start_write_offset;
260 memcpy(ring_buffer + start_write_offset, src, frag_len);
261 memcpy(ring_buffer, src + frag_len, srclen - frag_len);
262 } else
263 memcpy(ring_buffer + start_write_offset, src, srclen);
3e7ee490 264
7581578d
S
265 start_write_offset += srclen;
266 start_write_offset %= ring_buffer_size;
267
268 return start_write_offset;
269}
3e7ee490 270
b2a5a585
S
271/*
272 *
273 * hv_ringbuffer_get_debuginfo()
274 *
275 * Get various debug metrics for the specified ring buffer
276 *
277 */
a75b61d5 278void hv_ringbuffer_get_debuginfo(struct hv_ring_buffer_info *ring_info,
80682b7a 279 struct hv_ring_buffer_debug_info *debug_info)
3e7ee490 280{
fc8c72eb
HZ
281 u32 bytes_avail_towrite;
282 u32 bytes_avail_toread;
3e7ee490 283
fc8c72eb 284 if (ring_info->ring_buffer) {
2b8a912e 285 hv_get_ringbuffer_availbytes(ring_info,
fc8c72eb
HZ
286 &bytes_avail_toread,
287 &bytes_avail_towrite);
3e7ee490 288
fc8c72eb
HZ
289 debug_info->bytes_avail_toread = bytes_avail_toread;
290 debug_info->bytes_avail_towrite = bytes_avail_towrite;
82f8bd40 291 debug_info->current_read_index =
fc8c72eb 292 ring_info->ring_buffer->read_index;
82f8bd40 293 debug_info->current_write_index =
fc8c72eb 294 ring_info->ring_buffer->write_index;
82f8bd40 295 debug_info->current_interrupt_mask =
fc8c72eb 296 ring_info->ring_buffer->interrupt_mask;
3e7ee490
HJ
297 }
298}
299
300
b2a5a585
S
301/*
302 *
303 * hv_get_ringbuffer_interrupt_mask()
304 *
305 * Get the interrupt mask for the specified ring buffer
306 *
307 */
decc49da 308u32 hv_get_ringbuffer_interrupt_mask(struct hv_ring_buffer_info *rbi)
3e7ee490 309{
82f8bd40 310 return rbi->ring_buffer->interrupt_mask;
3e7ee490
HJ
311}
312
b2a5a585
S
313/*
314 *
315 * hv_ringbuffer_init()
316 *
317 *Initialize the ring buffer
318 *
319 */
72a95cbc 320int hv_ringbuffer_init(struct hv_ring_buffer_info *ring_info,
fc8c72eb 321 void *buffer, u32 buflen)
3e7ee490 322{
4a1b3acc 323 if (sizeof(struct hv_ring_buffer) != PAGE_SIZE)
3324fb40 324 return -EINVAL;
3e7ee490 325
fc8c72eb 326 memset(ring_info, 0, sizeof(struct hv_ring_buffer_info));
3e7ee490 327
fc8c72eb
HZ
328 ring_info->ring_buffer = (struct hv_ring_buffer *)buffer;
329 ring_info->ring_buffer->read_index =
330 ring_info->ring_buffer->write_index = 0;
3e7ee490 331
fc8c72eb
HZ
332 ring_info->ring_size = buflen;
333 ring_info->ring_datasize = buflen - sizeof(struct hv_ring_buffer);
3e7ee490 334
fc8c72eb 335 spin_lock_init(&ring_info->ring_lock);
3e7ee490
HJ
336
337 return 0;
338}
339
b2a5a585
S
340/*
341 *
342 * hv_ringbuffer_cleanup()
343 *
344 * Cleanup the ring buffer
345 *
346 */
2dba688b 347void hv_ringbuffer_cleanup(struct hv_ring_buffer_info *ring_info)
3e7ee490 348{
3e7ee490
HJ
349}
350
b2a5a585
S
351/*
352 *
353 * hv_ringbuffer_write()
354 *
355 * Write to the ring buffer
356 *
357 */
633c4dce 358int hv_ringbuffer_write(struct hv_ring_buffer_info *outring_info,
3523a805 359 struct scatterlist *sglist, u32 sgcount)
3e7ee490 360{
4408f531 361 int i = 0;
fc8c72eb
HZ
362 u32 bytes_avail_towrite;
363 u32 bytes_avail_toread;
364 u32 totalbytes_towrite = 0;
3e7ee490 365
b219b3f7 366 struct scatterlist *sg;
fc8c72eb
HZ
367 volatile u32 next_write_location;
368 u64 prev_indices = 0;
a98f96ee 369 unsigned long flags;
3e7ee490 370
b219b3f7 371 for_each_sg(sglist, sg, sgcount, i)
3e7ee490 372 {
fc8c72eb 373 totalbytes_towrite += sg->length;
3e7ee490
HJ
374 }
375
fc8c72eb 376 totalbytes_towrite += sizeof(u64);
3e7ee490 377
fc8c72eb 378 spin_lock_irqsave(&outring_info->ring_lock, flags);
3e7ee490 379
2b8a912e 380 hv_get_ringbuffer_availbytes(outring_info,
fc8c72eb
HZ
381 &bytes_avail_toread,
382 &bytes_avail_towrite);
3e7ee490 383
3e7ee490 384
4408f531
B
385 /* If there is only room for the packet, assume it is full. */
386 /* Otherwise, the next time around, we think the ring buffer */
454f18a9 387 /* is empty since the read index == write index */
fc8c72eb 388 if (bytes_avail_towrite <= totalbytes_towrite) {
fc8c72eb 389 spin_unlock_irqrestore(&outring_info->ring_lock, flags);
3e7ee490
HJ
390 return -1;
391 }
392
454f18a9 393 /* Write to the ring buffer */
2b8a912e 394 next_write_location = hv_get_next_write_location(outring_info);
3e7ee490 395
b219b3f7 396 for_each_sg(sglist, sg, sgcount, i)
3e7ee490 397 {
2b8a912e 398 next_write_location = hv_copyto_ringbuffer(outring_info,
fc8c72eb 399 next_write_location,
b219b3f7
NP
400 sg_virt(sg),
401 sg->length);
3e7ee490
HJ
402 }
403
454f18a9 404 /* Set previous packet start */
2b8a912e 405 prev_indices = hv_get_ring_bufferindices(outring_info);
3e7ee490 406
2b8a912e 407 next_write_location = hv_copyto_ringbuffer(outring_info,
fc8c72eb
HZ
408 next_write_location,
409 &prev_indices,
b219b3f7 410 sizeof(u64));
3e7ee490 411
454f18a9 412 /* Make sure we flush all writes before updating the writeIndex */
28b6ca9c 413 mb();
3e7ee490 414
454f18a9 415 /* Now, update the write location */
2b8a912e 416 hv_set_next_write_location(outring_info, next_write_location);
3e7ee490 417
3e7ee490 418
fc8c72eb 419 spin_unlock_irqrestore(&outring_info->ring_lock, flags);
3e7ee490
HJ
420 return 0;
421}
422
423
b2a5a585
S
424/*
425 *
426 * hv_ringbuffer_peek()
427 *
428 * Read without advancing the read index
429 *
430 */
a89186c2 431int hv_ringbuffer_peek(struct hv_ring_buffer_info *Inring_info,
fc8c72eb 432 void *Buffer, u32 buflen)
3e7ee490 433{
fc8c72eb
HZ
434 u32 bytes_avail_towrite;
435 u32 bytes_avail_toread;
436 u32 next_read_location = 0;
a98f96ee 437 unsigned long flags;
3e7ee490 438
fc8c72eb 439 spin_lock_irqsave(&Inring_info->ring_lock, flags);
3e7ee490 440
2b8a912e 441 hv_get_ringbuffer_availbytes(Inring_info,
fc8c72eb
HZ
442 &bytes_avail_toread,
443 &bytes_avail_towrite);
3e7ee490 444
454f18a9 445 /* Make sure there is something to read */
fc8c72eb 446 if (bytes_avail_toread < buflen) {
3e7ee490 447
fc8c72eb 448 spin_unlock_irqrestore(&Inring_info->ring_lock, flags);
3e7ee490
HJ
449
450 return -1;
451 }
452
454f18a9 453 /* Convert to byte offset */
2b8a912e 454 next_read_location = hv_get_next_read_location(Inring_info);
3e7ee490 455
2b8a912e 456 next_read_location = hv_copyfrom_ringbuffer(Inring_info,
4408f531 457 Buffer,
fc8c72eb
HZ
458 buflen,
459 next_read_location);
3e7ee490 460
fc8c72eb 461 spin_unlock_irqrestore(&Inring_info->ring_lock, flags);
3e7ee490
HJ
462
463 return 0;
464}
465
466
b2a5a585
S
467/*
468 *
469 * hv_ringbuffer_read()
470 *
471 * Read and advance the read index
472 *
473 */
38397c8a 474int hv_ringbuffer_read(struct hv_ring_buffer_info *inring_info, void *buffer,
fc8c72eb 475 u32 buflen, u32 offset)
3e7ee490 476{
fc8c72eb
HZ
477 u32 bytes_avail_towrite;
478 u32 bytes_avail_toread;
479 u32 next_read_location = 0;
480 u64 prev_indices = 0;
a98f96ee 481 unsigned long flags;
3e7ee490 482
fc8c72eb 483 if (buflen <= 0)
a16e1485 484 return -EINVAL;
3e7ee490 485
fc8c72eb 486 spin_lock_irqsave(&inring_info->ring_lock, flags);
3e7ee490 487
2b8a912e 488 hv_get_ringbuffer_availbytes(inring_info,
fc8c72eb
HZ
489 &bytes_avail_toread,
490 &bytes_avail_towrite);
3e7ee490 491
454f18a9 492 /* Make sure there is something to read */
fc8c72eb 493 if (bytes_avail_toread < buflen) {
fc8c72eb 494 spin_unlock_irqrestore(&inring_info->ring_lock, flags);
3e7ee490
HJ
495
496 return -1;
497 }
498
1ac58644 499 next_read_location =
2b8a912e 500 hv_get_next_readlocation_withoffset(inring_info, offset);
3e7ee490 501
2b8a912e 502 next_read_location = hv_copyfrom_ringbuffer(inring_info,
fc8c72eb
HZ
503 buffer,
504 buflen,
505 next_read_location);
3e7ee490 506
2b8a912e 507 next_read_location = hv_copyfrom_ringbuffer(inring_info,
fc8c72eb 508 &prev_indices,
4408f531 509 sizeof(u64),
fc8c72eb 510 next_read_location);
3e7ee490 511
454f18a9 512 /* Make sure all reads are done before we update the read index since */
4408f531
B
513 /* the writer may start writing to the read area once the read index */
514 /*is updated */
28b6ca9c 515 mb();
3e7ee490 516
454f18a9 517 /* Update the read index */
2b8a912e 518 hv_set_next_read_location(inring_info, next_read_location);
3e7ee490 519
fc8c72eb 520 spin_unlock_irqrestore(&inring_info->ring_lock, flags);
3e7ee490
HJ
521
522 return 0;
523}
This page took 0.239808 seconds and 5 git commands to generate.