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>
24 #include <linux/kernel.h>
28 #include "ring_buffer.h"
34 /* Amount of space to write to */
35 #define BYTES_AVAIL_TO_WRITE(r, w, z) ((w) >= (r)) ? ((z) - ((w) - (r))) : ((r) - (w))
41 get_ringbuffer_availbytes()
44 Get number of bytes available to read and to write to
45 for the specified ring buffer
49 get_ringbuffer_availbytes(struct hv_ring_buffer_info *rbi,
50 u32 *read, u32 *write)
52 u32 read_loc, write_loc;
54 /* Capture the read/write indices before they changed */
55 read_loc = rbi->ring_buffer->read_index;
56 write_loc = rbi->ring_buffer->write_index;
58 *write = BYTES_AVAIL_TO_WRITE(read_loc, write_loc, rbi->ring_datasize);
59 *read = rbi->ring_datasize - *write;
65 get_next_write_location()
68 Get the next write location for the specified ring buffer
72 get_next_write_location(struct hv_ring_buffer_info *ring_info)
74 u32 next = ring_info->ring_buffer->write_index;
76 /* ASSERT(next < ring_info->RingDataSize); */
84 set_next_write_location()
87 Set the next write location for the specified ring buffer
91 set_next_write_location(struct hv_ring_buffer_info *ring_info,
92 u32 next_write_location)
94 ring_info->ring_buffer->write_index = next_write_location;
100 get_next_read_location()
103 Get the next read location for the specified ring buffer
107 get_next_read_location(struct hv_ring_buffer_info *ring_info)
109 u32 next = ring_info->ring_buffer->read_index;
111 /* ASSERT(next < ring_info->RingDataSize); */
119 get_next_readlocation_withoffset()
122 Get the next read location + offset for the specified ring buffer.
123 This allows the caller to skip
127 get_next_readlocation_withoffset(struct hv_ring_buffer_info *ring_info,
130 u32 next = ring_info->ring_buffer->read_index;
132 /* ASSERT(next < ring_info->RingDataSize); */
134 next %= ring_info->ring_datasize;
142 set_next_read_location()
145 Set the next read location for the specified ring buffer
149 set_next_read_location(struct hv_ring_buffer_info *ring_info,
150 u32 next_read_location)
152 ring_info->ring_buffer->read_index = next_read_location;
162 Get the start of the ring buffer
166 get_ring_buffer(struct hv_ring_buffer_info *ring_info)
168 return (void *)ring_info->ring_buffer->buffer;
175 get_ring_buffersize()
178 Get the size of the ring buffer
182 get_ring_buffersize(struct hv_ring_buffer_info *ring_info)
184 return ring_info->ring_datasize;
190 get_ring_bufferindices()
193 Get the read and write indices as u64 of the specified ring buffer
197 get_ring_bufferindices(struct hv_ring_buffer_info *ring_info)
199 return (u64)ring_info->ring_buffer->write_index << 32;
209 Dump out to console the ring buffer info
212 void dump_ring_info(struct hv_ring_buffer_info *ring_info, char *prefix)
214 u32 bytes_avail_towrite;
215 u32 bytes_avail_toread;
217 get_ringbuffer_availbytes(ring_info,
219 &bytes_avail_towrite);
223 "%s <<ringinfo %p buffer %p avail write %u "
224 "avail read %u read idx %u write idx %u>>",
227 ring_info->ring_buffer->buffer,
230 ring_info->ring_buffer->read_index,
231 ring_info->ring_buffer->write_index);
235 /* Internal routines */
239 struct hv_ring_buffer_info *ring_info,
240 u32 start_write_offset,
246 struct hv_ring_buffer_info *ring_info,
249 u32 start_read_offset);
256 ringbuffer_get_debuginfo()
259 Get various debug metrics for the specified ring buffer
262 void ringbuffer_get_debuginfo(struct hv_ring_buffer_info *ring_info,
263 struct hv_ring_buffer_debug_info *debug_info)
265 u32 bytes_avail_towrite;
266 u32 bytes_avail_toread;
268 if (ring_info->ring_buffer) {
269 get_ringbuffer_availbytes(ring_info,
271 &bytes_avail_towrite);
273 debug_info->bytes_avail_toread = bytes_avail_toread;
274 debug_info->bytes_avail_towrite = bytes_avail_towrite;
275 debug_info->current_read_index =
276 ring_info->ring_buffer->read_index;
277 debug_info->current_write_index =
278 ring_info->ring_buffer->write_index;
279 debug_info->current_interrupt_mask =
280 ring_info->ring_buffer->interrupt_mask;
288 get_ringbuffer_interrupt_mask()
291 Get the interrupt mask for the specified ring buffer
294 u32 get_ringbuffer_interrupt_mask(struct hv_ring_buffer_info *rbi)
296 return rbi->ring_buffer->interrupt_mask;
305 Initialize the ring buffer
308 int ringbuffer_init(struct hv_ring_buffer_info *ring_info,
309 void *buffer, u32 buflen)
311 if (sizeof(struct hv_ring_buffer) != PAGE_SIZE)
314 memset(ring_info, 0, sizeof(struct hv_ring_buffer_info));
316 ring_info->ring_buffer = (struct hv_ring_buffer *)buffer;
317 ring_info->ring_buffer->read_index =
318 ring_info->ring_buffer->write_index = 0;
320 ring_info->ring_size = buflen;
321 ring_info->ring_datasize = buflen - sizeof(struct hv_ring_buffer);
323 spin_lock_init(&ring_info->ring_lock);
334 Cleanup the ring buffer
337 void ringbuffer_cleanup(struct hv_ring_buffer_info *ring_info)
347 Write to the ring buffer
350 int ringbuffer_write(struct hv_ring_buffer_info *outring_info,
351 struct scatterlist *sglist, u32 sgcount)
354 u32 bytes_avail_towrite;
355 u32 bytes_avail_toread;
356 u32 totalbytes_towrite = 0;
358 struct scatterlist *sg;
359 volatile u32 next_write_location;
360 u64 prev_indices = 0;
363 for_each_sg(sglist, sg, sgcount, i)
365 totalbytes_towrite += sg->length;
368 totalbytes_towrite += sizeof(u64);
370 spin_lock_irqsave(&outring_info->ring_lock, flags);
372 get_ringbuffer_availbytes(outring_info,
374 &bytes_avail_towrite);
376 DPRINT_DBG(VMBUS, "Writing %u bytes...", totalbytes_towrite);
378 /* Dumpring_info(Outring_info, "BEFORE "); */
380 /* If there is only room for the packet, assume it is full. */
381 /* Otherwise, the next time around, we think the ring buffer */
382 /* is empty since the read index == write index */
383 if (bytes_avail_towrite <= totalbytes_towrite) {
385 "No more space left on outbound ring buffer "
386 "(needed %u, avail %u)",
388 bytes_avail_towrite);
390 spin_unlock_irqrestore(&outring_info->ring_lock, flags);
394 /* Write to the ring buffer */
395 next_write_location = get_next_write_location(outring_info);
397 for_each_sg(sglist, sg, sgcount, i)
399 next_write_location = copyto_ringbuffer(outring_info,
405 /* Set previous packet start */
406 prev_indices = get_ring_bufferindices(outring_info);
408 next_write_location = copyto_ringbuffer(outring_info,
413 /* Make sure we flush all writes before updating the writeIndex */
416 /* Now, update the write location */
417 set_next_write_location(outring_info, next_write_location);
419 /* Dumpring_info(Outring_info, "AFTER "); */
421 spin_unlock_irqrestore(&outring_info->ring_lock, flags);
432 Read without advancing the read index
435 int ringbuffer_peek(struct hv_ring_buffer_info *Inring_info,
436 void *Buffer, u32 buflen)
438 u32 bytes_avail_towrite;
439 u32 bytes_avail_toread;
440 u32 next_read_location = 0;
443 spin_lock_irqsave(&Inring_info->ring_lock, flags);
445 get_ringbuffer_availbytes(Inring_info,
447 &bytes_avail_towrite);
449 /* Make sure there is something to read */
450 if (bytes_avail_toread < buflen) {
452 "got callback but not enough to read "
453 "<avail to read %d read size %d>!!",
457 spin_unlock_irqrestore(&Inring_info->ring_lock, flags);
462 /* Convert to byte offset */
463 next_read_location = get_next_read_location(Inring_info);
465 next_read_location = copyfrom_ringbuffer(Inring_info,
470 spin_unlock_irqrestore(&Inring_info->ring_lock, flags);
482 Read and advance the read index
485 int ringbuffer_read(struct hv_ring_buffer_info *inring_info, void *buffer,
486 u32 buflen, u32 offset)
488 u32 bytes_avail_towrite;
489 u32 bytes_avail_toread;
490 u32 next_read_location = 0;
491 u64 prev_indices = 0;
497 spin_lock_irqsave(&inring_info->ring_lock, flags);
499 get_ringbuffer_availbytes(inring_info,
501 &bytes_avail_towrite);
503 DPRINT_DBG(VMBUS, "Reading %u bytes...", buflen);
505 /* Dumpring_info(Inring_info, "BEFORE "); */
507 /* Make sure there is something to read */
508 if (bytes_avail_toread < buflen) {
510 "got callback but not enough to read "
511 "<avail to read %d read size %d>!!",
515 spin_unlock_irqrestore(&inring_info->ring_lock, flags);
521 get_next_readlocation_withoffset(inring_info, offset);
523 next_read_location = copyfrom_ringbuffer(inring_info,
528 next_read_location = copyfrom_ringbuffer(inring_info,
533 /* Make sure all reads are done before we update the read index since */
534 /* the writer may start writing to the read area once the read index */
538 /* Update the read index */
539 set_next_read_location(inring_info, next_read_location);
541 /* Dumpring_info(Inring_info, "AFTER "); */
543 spin_unlock_irqrestore(&inring_info->ring_lock, flags);
555 Helper routine to copy from source to ring buffer.
556 Assume there is enough room. Handles wrap-around in dest case only!!
561 struct hv_ring_buffer_info *ring_info,
562 u32 start_write_offset,
566 void *ring_buffer = get_ring_buffer(ring_info);
567 u32 ring_buffer_size = get_ring_buffersize(ring_info);
570 /* wrap-around detected! */
571 if (srclen > ring_buffer_size - start_write_offset) {
572 DPRINT_DBG(VMBUS, "wrap-around detected!");
574 frag_len = ring_buffer_size - start_write_offset;
575 memcpy(ring_buffer + start_write_offset, src, frag_len);
576 memcpy(ring_buffer, src + frag_len, srclen - frag_len);
578 memcpy(ring_buffer + start_write_offset, src, srclen);
580 start_write_offset += srclen;
581 start_write_offset %= ring_buffer_size;
583 return start_write_offset;
590 copyfrom_ringbuffer()
593 Helper routine to copy to source from ring buffer.
594 Assume there is enough room. Handles wrap-around in src case only!!
599 struct hv_ring_buffer_info *ring_info,
602 u32 start_read_offset)
604 void *ring_buffer = get_ring_buffer(ring_info);
605 u32 ring_buffer_size = get_ring_buffersize(ring_info);
609 /* wrap-around detected at the src */
610 if (destlen > ring_buffer_size - start_read_offset) {
611 DPRINT_DBG(VMBUS, "src wrap-around detected!");
613 frag_len = ring_buffer_size - start_read_offset;
615 memcpy(dest, ring_buffer + start_read_offset, frag_len);
616 memcpy(dest + frag_len, ring_buffer, destlen - frag_len);
619 memcpy(dest, ring_buffer + start_read_offset, destlen);
622 start_read_offset += destlen;
623 start_read_offset %= ring_buffer_size;
625 return start_read_offset;