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
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.
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>
30 #include "hyperv_vmbus.h"
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
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
70 hv_get_next_write_location(struct hv_ring_buffer_info *ring_info)
72 u32 next = ring_info->ring_buffer->write_index;
78 * hv_set_next_write_location()
80 * Set the next write location for the specified ring buffer
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
96 hv_get_next_read_location(struct hv_ring_buffer_info *ring_info)
98 u32 next = ring_info->ring_buffer->read_index;
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
110 hv_get_next_readlocation_withoffset(struct hv_ring_buffer_info *ring_info,
113 u32 next = ring_info->ring_buffer->read_index;
116 next %= ring_info->ring_datasize;
123 * hv_set_next_read_location()
125 * Set the next read location for the specified ring buffer
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
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
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
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,
189 &bytes_avail_towrite);
193 "%s <<ringinfo %p buffer %p avail write %u "
194 "avail read %u read idx %u write idx %u>>",
197 ring_info->ring_buffer->buffer,
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,
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);
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);
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,
256 void *ring_buffer = hv_get_ring_buffer(ring_info);
257 u32 ring_buffer_size = hv_get_ring_buffersize(ring_info);
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);
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,
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)
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);
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)
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;
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,
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);
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,
407 /* Set previous packet start */
408 prev_indices = hv_get_ring_bufferindices(outring_info);
410 next_write_location = hv_copyto_ringbuffer(outring_info,
415 /* Make sure we flush all writes before updating the writeIndex */
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);
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;
442 spin_lock_irqsave(&Inring_info->ring_lock, flags);
444 hv_get_ringbuffer_availbytes(Inring_info,
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);
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,
464 spin_unlock_irqrestore(&Inring_info->ring_lock, flags);
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;
489 spin_lock_irqsave(&inring_info->ring_lock, flags);
491 hv_get_ringbuffer_availbytes(inring_info,
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);
503 hv_get_next_readlocation_withoffset(inring_info, offset);
505 next_read_location = hv_copyfrom_ringbuffer(inring_info,
510 next_read_location = hv_copyfrom_ringbuffer(inring_info,
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 */
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);