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