tty/serial: atmel_serial: whitespace and braces modifications
[zen-stable.git] / drivers / staging / hv / ring_buffer.c
blob42f76728429a0676dcd0cd3a82d2e10b55ee2fd0
1 /*
3 * Copyright (c) 2009, Microsoft Corporation.
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.
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.
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.
18 * Authors:
19 * Haiyang Zhang <haiyangz@microsoft.com>
20 * Hank Janssen <hjanssen@microsoft.com>
21 * K. Y. Srinivasan <kys@microsoft.com>
24 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
26 #include <linux/kernel.h>
27 #include <linux/mm.h>
29 #include "hyperv.h"
30 #include "hyperv_vmbus.h"
33 /* #defines */
36 /* Amount of space to write to */
37 #define BYTES_AVAIL_TO_WRITE(r, w, z) ((w) >= (r)) ? ((z) - ((w) - (r))) : ((r) - (w))
42 * hv_get_ringbuffer_availbytes()
44 * Get number of bytes available to read and to write to
45 * for the specified ring buffer
47 static inline void
48 hv_get_ringbuffer_availbytes(struct hv_ring_buffer_info *rbi,
49 u32 *read, u32 *write)
51 u32 read_loc, write_loc;
53 smp_read_barrier_depends();
55 /* Capture the read/write indices before they changed */
56 read_loc = rbi->ring_buffer->read_index;
57 write_loc = rbi->ring_buffer->write_index;
59 *write = BYTES_AVAIL_TO_WRITE(read_loc, write_loc, rbi->ring_datasize);
60 *read = rbi->ring_datasize - *write;
64 * hv_get_next_write_location()
66 * Get the next write location for the specified ring buffer
69 static inline u32
70 hv_get_next_write_location(struct hv_ring_buffer_info *ring_info)
72 u32 next = ring_info->ring_buffer->write_index;
74 return next;
78 * hv_set_next_write_location()
80 * Set the next write location for the specified ring buffer
83 static inline void
84 hv_set_next_write_location(struct hv_ring_buffer_info *ring_info,
85 u32 next_write_location)
87 ring_info->ring_buffer->write_index = next_write_location;
91 * hv_get_next_read_location()
93 * Get the next read location for the specified ring buffer
95 static inline u32
96 hv_get_next_read_location(struct hv_ring_buffer_info *ring_info)
98 u32 next = ring_info->ring_buffer->read_index;
100 return next;
104 * hv_get_next_readlocation_withoffset()
106 * Get the next read location + offset for the specified ring buffer.
107 * This allows the caller to skip
109 static inline u32
110 hv_get_next_readlocation_withoffset(struct hv_ring_buffer_info *ring_info,
111 u32 offset)
113 u32 next = ring_info->ring_buffer->read_index;
115 next += offset;
116 next %= ring_info->ring_datasize;
118 return next;
123 * hv_set_next_read_location()
125 * Set the next read location for the specified ring buffer
128 static inline void
129 hv_set_next_read_location(struct hv_ring_buffer_info *ring_info,
130 u32 next_read_location)
132 ring_info->ring_buffer->read_index = next_read_location;
138 * hv_get_ring_buffer()
140 * Get the start of the ring buffer
142 static inline void *
143 hv_get_ring_buffer(struct hv_ring_buffer_info *ring_info)
145 return (void *)ring_info->ring_buffer->buffer;
151 * hv_get_ring_buffersize()
153 * Get the size of the ring buffer
155 static inline u32
156 hv_get_ring_buffersize(struct hv_ring_buffer_info *ring_info)
158 return ring_info->ring_datasize;
163 * hv_get_ring_bufferindices()
165 * Get the read and write indices as u64 of the specified ring buffer
168 static inline u64
169 hv_get_ring_bufferindices(struct hv_ring_buffer_info *ring_info)
171 return (u64)ring_info->ring_buffer->write_index << 32;
177 * hv_dump_ring_info()
179 * Dump out to console the ring buffer info
182 void hv_dump_ring_info(struct hv_ring_buffer_info *ring_info, char *prefix)
184 u32 bytes_avail_towrite;
185 u32 bytes_avail_toread;
187 hv_get_ringbuffer_availbytes(ring_info,
188 &bytes_avail_toread,
189 &bytes_avail_towrite);
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>>",
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);
207 * hv_copyfrom_ringbuffer()
209 * Helper routine to copy to source from ring buffer.
210 * Assume there is enough room. Handles wrap-around in src case only!!
213 static u32 hv_copyfrom_ringbuffer(
214 struct hv_ring_buffer_info *ring_info,
215 void *dest,
216 u32 destlen,
217 u32 start_read_offset)
219 void *ring_buffer = hv_get_ring_buffer(ring_info);
220 u32 ring_buffer_size = hv_get_ring_buffersize(ring_info);
222 u32 frag_len;
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;
228 memcpy(dest, ring_buffer + start_read_offset, frag_len);
229 memcpy(dest + frag_len, ring_buffer, destlen - frag_len);
230 } else
232 memcpy(dest, ring_buffer + start_read_offset, destlen);
235 start_read_offset += destlen;
236 start_read_offset %= ring_buffer_size;
238 return start_read_offset;
244 * hv_copyto_ringbuffer()
246 * Helper routine to copy from source to ring buffer.
247 * Assume there is enough room. Handles wrap-around in dest case only!!
250 static u32 hv_copyto_ringbuffer(
251 struct hv_ring_buffer_info *ring_info,
252 u32 start_write_offset,
253 void *src,
254 u32 srclen)
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;
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);
268 start_write_offset += srclen;
269 start_write_offset %= ring_buffer_size;
271 return start_write_offset;
276 * hv_ringbuffer_get_debuginfo()
278 * Get various debug metrics for the specified ring buffer
281 void hv_ringbuffer_get_debuginfo(struct hv_ring_buffer_info *ring_info,
282 struct hv_ring_buffer_debug_info *debug_info)
284 u32 bytes_avail_towrite;
285 u32 bytes_avail_toread;
287 if (ring_info->ring_buffer) {
288 hv_get_ringbuffer_availbytes(ring_info,
289 &bytes_avail_toread,
290 &bytes_avail_towrite);
292 debug_info->bytes_avail_toread = bytes_avail_toread;
293 debug_info->bytes_avail_towrite = bytes_avail_towrite;
294 debug_info->current_read_index =
295 ring_info->ring_buffer->read_index;
296 debug_info->current_write_index =
297 ring_info->ring_buffer->write_index;
298 debug_info->current_interrupt_mask =
299 ring_info->ring_buffer->interrupt_mask;
306 * hv_get_ringbuffer_interrupt_mask()
308 * Get the interrupt mask for the specified ring buffer
311 u32 hv_get_ringbuffer_interrupt_mask(struct hv_ring_buffer_info *rbi)
313 return rbi->ring_buffer->interrupt_mask;
318 * hv_ringbuffer_init()
320 *Initialize the ring buffer
323 int hv_ringbuffer_init(struct hv_ring_buffer_info *ring_info,
324 void *buffer, u32 buflen)
326 if (sizeof(struct hv_ring_buffer) != PAGE_SIZE)
327 return -EINVAL;
329 memset(ring_info, 0, sizeof(struct hv_ring_buffer_info));
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;
335 ring_info->ring_size = buflen;
336 ring_info->ring_datasize = buflen - sizeof(struct hv_ring_buffer);
338 spin_lock_init(&ring_info->ring_lock);
340 return 0;
345 * hv_ringbuffer_cleanup()
347 * Cleanup the ring buffer
350 void hv_ringbuffer_cleanup(struct hv_ring_buffer_info *ring_info)
356 * hv_ringbuffer_write()
358 * Write to the ring buffer
361 int hv_ringbuffer_write(struct hv_ring_buffer_info *outring_info,
362 struct scatterlist *sglist, u32 sgcount)
364 int i = 0;
365 u32 bytes_avail_towrite;
366 u32 bytes_avail_toread;
367 u32 totalbytes_towrite = 0;
369 struct scatterlist *sg;
370 u32 next_write_location;
371 u64 prev_indices = 0;
372 unsigned long flags;
374 for_each_sg(sglist, sg, sgcount, i)
376 totalbytes_towrite += sg->length;
379 totalbytes_towrite += sizeof(u64);
381 spin_lock_irqsave(&outring_info->ring_lock, flags);
383 hv_get_ringbuffer_availbytes(outring_info,
384 &bytes_avail_toread,
385 &bytes_avail_towrite);
388 /* If there is only room for the packet, assume it is full. */
389 /* Otherwise, the next time around, we think the ring buffer */
390 /* is empty since the read index == write index */
391 if (bytes_avail_towrite <= totalbytes_towrite) {
392 spin_unlock_irqrestore(&outring_info->ring_lock, flags);
393 return -1;
396 /* Write to the ring buffer */
397 next_write_location = hv_get_next_write_location(outring_info);
399 for_each_sg(sglist, sg, sgcount, i)
401 next_write_location = hv_copyto_ringbuffer(outring_info,
402 next_write_location,
403 sg_virt(sg),
404 sg->length);
407 /* Set previous packet start */
408 prev_indices = hv_get_ring_bufferindices(outring_info);
410 next_write_location = hv_copyto_ringbuffer(outring_info,
411 next_write_location,
412 &prev_indices,
413 sizeof(u64));
415 /* Make sure we flush all writes before updating the writeIndex */
416 smp_wmb();
418 /* Now, update the write location */
419 hv_set_next_write_location(outring_info, next_write_location);
422 spin_unlock_irqrestore(&outring_info->ring_lock, flags);
423 return 0;
429 * hv_ringbuffer_peek()
431 * Read without advancing the read index
434 int hv_ringbuffer_peek(struct hv_ring_buffer_info *Inring_info,
435 void *Buffer, u32 buflen)
437 u32 bytes_avail_towrite;
438 u32 bytes_avail_toread;
439 u32 next_read_location = 0;
440 unsigned long flags;
442 spin_lock_irqsave(&Inring_info->ring_lock, flags);
444 hv_get_ringbuffer_availbytes(Inring_info,
445 &bytes_avail_toread,
446 &bytes_avail_towrite);
448 /* Make sure there is something to read */
449 if (bytes_avail_toread < buflen) {
451 spin_unlock_irqrestore(&Inring_info->ring_lock, flags);
453 return -1;
456 /* Convert to byte offset */
457 next_read_location = hv_get_next_read_location(Inring_info);
459 next_read_location = hv_copyfrom_ringbuffer(Inring_info,
460 Buffer,
461 buflen,
462 next_read_location);
464 spin_unlock_irqrestore(&Inring_info->ring_lock, flags);
466 return 0;
472 * hv_ringbuffer_read()
474 * Read and advance the read index
477 int hv_ringbuffer_read(struct hv_ring_buffer_info *inring_info, void *buffer,
478 u32 buflen, u32 offset)
480 u32 bytes_avail_towrite;
481 u32 bytes_avail_toread;
482 u32 next_read_location = 0;
483 u64 prev_indices = 0;
484 unsigned long flags;
486 if (buflen <= 0)
487 return -EINVAL;
489 spin_lock_irqsave(&inring_info->ring_lock, flags);
491 hv_get_ringbuffer_availbytes(inring_info,
492 &bytes_avail_toread,
493 &bytes_avail_towrite);
495 /* Make sure there is something to read */
496 if (bytes_avail_toread < buflen) {
497 spin_unlock_irqrestore(&inring_info->ring_lock, flags);
499 return -1;
502 next_read_location =
503 hv_get_next_readlocation_withoffset(inring_info, offset);
505 next_read_location = hv_copyfrom_ringbuffer(inring_info,
506 buffer,
507 buflen,
508 next_read_location);
510 next_read_location = hv_copyfrom_ringbuffer(inring_info,
511 &prev_indices,
512 sizeof(u64),
513 next_read_location);
515 /* Make sure all reads are done before we update the read index since */
516 /* the writer may start writing to the read area once the read index */
517 /*is updated */
518 smp_mb();
520 /* Update the read index */
521 hv_set_next_read_location(inring_info, next_read_location);
523 spin_unlock_irqrestore(&inring_info->ring_lock, flags);
525 return 0;