blob: 8af25a097d75ae2a189d5e5aa25c1b226c849dc7 [file] [log] [blame]
Hank Janssen3e7ee492009-07-13 16:02:34 -07001/*
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>
K. Y. Srinivasanb2a5a582011-05-10 07:55:30 -070021 * K. Y. Srinivasan <kys@microsoft.com>
Hank Janssen3e7ee492009-07-13 16:02:34 -070022 *
23 */
Hank Janssen0a466182011-03-29 13:58:47 -070024#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Hank Janssen3e7ee492009-07-13 16:02:34 -070025
Greg Kroah-Hartmana0086dc2009-08-17 17:22:08 -070026#include <linux/kernel.h>
27#include <linux/mm.h>
Greg Kroah-Hartman46a97192011-10-04 12:29:52 -070028#include <linux/hyperv.h>
K. Y. Srinivasan3f335ea2011-05-12 19:34:15 -070029
K. Y. Srinivasan0f2a6612011-05-12 19:34:28 -070030#include "hyperv_vmbus.h"
Hank Janssen3e7ee492009-07-13 16:02:34 -070031
Hank Janssen3e7ee492009-07-13 16:02:34 -070032
Bill Pemberton454f18a2009-07-27 16:47:24 -040033/* #defines */
34
35
36/* Amount of space to write to */
K. Y. Srinivasanb737b2e2011-08-27 11:31:42 -070037#define BYTES_AVAIL_TO_WRITE(r, w, z) \
38 ((w) >= (r)) ? ((z) - ((w) - (r))) : ((r) - (w))
Hank Janssen3e7ee492009-07-13 16:02:34 -070039
40
K. Y. Srinivasanb2a5a582011-05-10 07:55:30 -070041/*
42 *
43 * hv_get_ringbuffer_availbytes()
44 *
45 * Get number of bytes available to read and to write to
46 * for the specified ring buffer
47 */
Hank Janssen3e7ee492009-07-13 16:02:34 -070048static inline void
K. Y. Srinivasan2b8a9122011-05-10 07:55:29 -070049hv_get_ringbuffer_availbytes(struct hv_ring_buffer_info *rbi,
Haiyang Zhang1ac58642010-11-08 14:04:47 -080050 u32 *read, u32 *write)
Hank Janssen3e7ee492009-07-13 16:02:34 -070051{
C. Bartlett4408f532010-02-03 15:34:27 +000052 u32 read_loc, write_loc;
Hank Janssen3e7ee492009-07-13 16:02:34 -070053
K. Y. Srinivasandf2a4a72011-06-06 15:49:52 -070054 smp_read_barrier_depends();
55
Bill Pemberton454f18a2009-07-27 16:47:24 -040056 /* Capture the read/write indices before they changed */
Haiyang Zhang82f8bd42010-11-08 14:04:45 -080057 read_loc = rbi->ring_buffer->read_index;
58 write_loc = rbi->ring_buffer->write_index;
Hank Janssen3e7ee492009-07-13 16:02:34 -070059
Haiyang Zhang82f8bd42010-11-08 14:04:45 -080060 *write = BYTES_AVAIL_TO_WRITE(read_loc, write_loc, rbi->ring_datasize);
61 *read = rbi->ring_datasize - *write;
Hank Janssen3e7ee492009-07-13 16:02:34 -070062}
63
K. Y. Srinivasanb2a5a582011-05-10 07:55:30 -070064/*
65 * hv_get_next_write_location()
66 *
67 * Get the next write location for the specified ring buffer
68 *
69 */
Greg Kroah-Hartman4d643112009-07-14 15:09:36 -070070static inline u32
K. Y. Srinivasan2b8a9122011-05-10 07:55:29 -070071hv_get_next_write_location(struct hv_ring_buffer_info *ring_info)
Hank Janssen3e7ee492009-07-13 16:02:34 -070072{
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -080073 u32 next = ring_info->ring_buffer->write_index;
Hank Janssen3e7ee492009-07-13 16:02:34 -070074
Hank Janssen3e7ee492009-07-13 16:02:34 -070075 return next;
76}
77
K. Y. Srinivasanb2a5a582011-05-10 07:55:30 -070078/*
79 * hv_set_next_write_location()
80 *
81 * Set the next write location for the specified ring buffer
82 *
83 */
Hank Janssen3e7ee492009-07-13 16:02:34 -070084static inline void
K. Y. Srinivasan2b8a9122011-05-10 07:55:29 -070085hv_set_next_write_location(struct hv_ring_buffer_info *ring_info,
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -080086 u32 next_write_location)
Hank Janssen3e7ee492009-07-13 16:02:34 -070087{
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -080088 ring_info->ring_buffer->write_index = next_write_location;
Hank Janssen3e7ee492009-07-13 16:02:34 -070089}
90
K. Y. Srinivasanb2a5a582011-05-10 07:55:30 -070091/*
92 * hv_get_next_read_location()
93 *
94 * Get the next read location for the specified ring buffer
95 */
Greg Kroah-Hartman4d643112009-07-14 15:09:36 -070096static inline u32
K. Y. Srinivasan2b8a9122011-05-10 07:55:29 -070097hv_get_next_read_location(struct hv_ring_buffer_info *ring_info)
Hank Janssen3e7ee492009-07-13 16:02:34 -070098{
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -080099 u32 next = ring_info->ring_buffer->read_index;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700100
Hank Janssen3e7ee492009-07-13 16:02:34 -0700101 return next;
102}
103
K. Y. Srinivasanb2a5a582011-05-10 07:55:30 -0700104/*
105 * hv_get_next_readlocation_withoffset()
106 *
107 * Get the next read location + offset for the specified ring buffer.
108 * This allows the caller to skip
109 */
Greg Kroah-Hartman4d643112009-07-14 15:09:36 -0700110static inline u32
K. Y. Srinivasan2b8a9122011-05-10 07:55:29 -0700111hv_get_next_readlocation_withoffset(struct hv_ring_buffer_info *ring_info,
Haiyang Zhang1ac58642010-11-08 14:04:47 -0800112 u32 offset)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700113{
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800114 u32 next = ring_info->ring_buffer->read_index;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700115
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800116 next += offset;
117 next %= ring_info->ring_datasize;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700118
119 return next;
120}
121
K. Y. Srinivasanb2a5a582011-05-10 07:55:30 -0700122/*
123 *
124 * hv_set_next_read_location()
125 *
126 * Set the next read location for the specified ring buffer
127 *
128 */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700129static inline void
K. Y. Srinivasan2b8a9122011-05-10 07:55:29 -0700130hv_set_next_read_location(struct hv_ring_buffer_info *ring_info,
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800131 u32 next_read_location)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700132{
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800133 ring_info->ring_buffer->read_index = next_read_location;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700134}
135
136
K. Y. Srinivasanb2a5a582011-05-10 07:55:30 -0700137/*
138 *
139 * hv_get_ring_buffer()
140 *
141 * Get the start of the ring buffer
142 */
Greg Kroah-Hartman8282c402009-07-14 15:06:28 -0700143static inline void *
K. Y. Srinivasan2b8a9122011-05-10 07:55:29 -0700144hv_get_ring_buffer(struct hv_ring_buffer_info *ring_info)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700145{
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800146 return (void *)ring_info->ring_buffer->buffer;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700147}
148
149
K. Y. Srinivasanb2a5a582011-05-10 07:55:30 -0700150/*
151 *
152 * hv_get_ring_buffersize()
153 *
154 * Get the size of the ring buffer
155 */
Greg Kroah-Hartman4d643112009-07-14 15:09:36 -0700156static inline u32
K. Y. Srinivasan2b8a9122011-05-10 07:55:29 -0700157hv_get_ring_buffersize(struct hv_ring_buffer_info *ring_info)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700158{
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800159 return ring_info->ring_datasize;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700160}
161
K. Y. Srinivasanb2a5a582011-05-10 07:55:30 -0700162/*
163 *
164 * hv_get_ring_bufferindices()
165 *
166 * Get the read and write indices as u64 of the specified ring buffer
167 *
168 */
Greg Kroah-Hartman59471432009-07-14 15:10:26 -0700169static inline u64
K. Y. Srinivasan2b8a9122011-05-10 07:55:29 -0700170hv_get_ring_bufferindices(struct hv_ring_buffer_info *ring_info)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700171{
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800172 return (u64)ring_info->ring_buffer->write_index << 32;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700173}
174
K. Y. Srinivasan8f1136a2011-05-10 07:55:31 -0700175/*
176 *
177 * hv_copyfrom_ringbuffer()
178 *
179 * Helper routine to copy to source from ring buffer.
180 * Assume there is enough room. Handles wrap-around in src case only!!
181 *
182 */
183static u32 hv_copyfrom_ringbuffer(
184 struct hv_ring_buffer_info *ring_info,
185 void *dest,
186 u32 destlen,
187 u32 start_read_offset)
188{
189 void *ring_buffer = hv_get_ring_buffer(ring_info);
190 u32 ring_buffer_size = hv_get_ring_buffersize(ring_info);
191
192 u32 frag_len;
193
194 /* wrap-around detected at the src */
195 if (destlen > ring_buffer_size - start_read_offset) {
196 frag_len = ring_buffer_size - start_read_offset;
197
198 memcpy(dest, ring_buffer + start_read_offset, frag_len);
199 memcpy(dest + frag_len, ring_buffer, destlen - frag_len);
200 } else
201
202 memcpy(dest, ring_buffer + start_read_offset, destlen);
203
204
205 start_read_offset += destlen;
206 start_read_offset %= ring_buffer_size;
207
208 return start_read_offset;
209}
210
211
K. Y. Srinivasan75815782011-05-10 07:55:32 -0700212/*
213 *
214 * hv_copyto_ringbuffer()
215 *
216 * Helper routine to copy from source to ring buffer.
217 * Assume there is enough room. Handles wrap-around in dest case only!!
218 *
219 */
220static u32 hv_copyto_ringbuffer(
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800221 struct hv_ring_buffer_info *ring_info,
222 u32 start_write_offset,
223 void *src,
K. Y. Srinivasan75815782011-05-10 07:55:32 -0700224 u32 srclen)
225{
226 void *ring_buffer = hv_get_ring_buffer(ring_info);
227 u32 ring_buffer_size = hv_get_ring_buffersize(ring_info);
228 u32 frag_len;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700229
K. Y. Srinivasan75815782011-05-10 07:55:32 -0700230 /* wrap-around detected! */
231 if (srclen > ring_buffer_size - start_write_offset) {
232 frag_len = ring_buffer_size - start_write_offset;
233 memcpy(ring_buffer + start_write_offset, src, frag_len);
234 memcpy(ring_buffer, src + frag_len, srclen - frag_len);
235 } else
236 memcpy(ring_buffer + start_write_offset, src, srclen);
237
238 start_write_offset += srclen;
239 start_write_offset %= ring_buffer_size;
240
241 return start_write_offset;
242}
Hank Janssen3e7ee492009-07-13 16:02:34 -0700243
K. Y. Srinivasanb2a5a582011-05-10 07:55:30 -0700244/*
245 *
246 * hv_ringbuffer_get_debuginfo()
247 *
248 * Get various debug metrics for the specified ring buffer
249 *
250 */
K. Y. Srinivasana75b61d2011-05-10 07:55:28 -0700251void hv_ringbuffer_get_debuginfo(struct hv_ring_buffer_info *ring_info,
Greg Kroah-Hartman80682b72010-07-27 11:37:32 -0700252 struct hv_ring_buffer_debug_info *debug_info)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700253{
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800254 u32 bytes_avail_towrite;
255 u32 bytes_avail_toread;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700256
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800257 if (ring_info->ring_buffer) {
K. Y. Srinivasan2b8a9122011-05-10 07:55:29 -0700258 hv_get_ringbuffer_availbytes(ring_info,
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800259 &bytes_avail_toread,
260 &bytes_avail_towrite);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700261
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800262 debug_info->bytes_avail_toread = bytes_avail_toread;
263 debug_info->bytes_avail_towrite = bytes_avail_towrite;
Haiyang Zhang82f8bd42010-11-08 14:04:45 -0800264 debug_info->current_read_index =
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800265 ring_info->ring_buffer->read_index;
Haiyang Zhang82f8bd42010-11-08 14:04:45 -0800266 debug_info->current_write_index =
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800267 ring_info->ring_buffer->write_index;
Haiyang Zhang82f8bd42010-11-08 14:04:45 -0800268 debug_info->current_interrupt_mask =
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800269 ring_info->ring_buffer->interrupt_mask;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700270 }
271}
272
273
K. Y. Srinivasanb2a5a582011-05-10 07:55:30 -0700274/*
275 *
276 * hv_get_ringbuffer_interrupt_mask()
277 *
278 * Get the interrupt mask for the specified ring buffer
279 *
280 */
K. Y. Srinivasandecc49d2011-05-10 07:55:26 -0700281u32 hv_get_ringbuffer_interrupt_mask(struct hv_ring_buffer_info *rbi)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700282{
Haiyang Zhang82f8bd42010-11-08 14:04:45 -0800283 return rbi->ring_buffer->interrupt_mask;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700284}
285
K. Y. Srinivasanb2a5a582011-05-10 07:55:30 -0700286/*
287 *
288 * hv_ringbuffer_init()
289 *
290 *Initialize the ring buffer
291 *
292 */
K. Y. Srinivasan72a95cb2011-05-10 07:55:21 -0700293int hv_ringbuffer_init(struct hv_ring_buffer_info *ring_info,
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800294 void *buffer, u32 buflen)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700295{
Greg Kroah-Hartman4a1b3ac2010-07-27 11:47:08 -0700296 if (sizeof(struct hv_ring_buffer) != PAGE_SIZE)
Bill Pemberton3324fb42010-05-05 15:27:49 -0400297 return -EINVAL;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700298
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800299 memset(ring_info, 0, sizeof(struct hv_ring_buffer_info));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700300
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800301 ring_info->ring_buffer = (struct hv_ring_buffer *)buffer;
302 ring_info->ring_buffer->read_index =
303 ring_info->ring_buffer->write_index = 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700304
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800305 ring_info->ring_size = buflen;
306 ring_info->ring_datasize = buflen - sizeof(struct hv_ring_buffer);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700307
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800308 spin_lock_init(&ring_info->ring_lock);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700309
310 return 0;
311}
312
K. Y. Srinivasanb2a5a582011-05-10 07:55:30 -0700313/*
314 *
315 * hv_ringbuffer_cleanup()
316 *
317 * Cleanup the ring buffer
318 *
319 */
K. Y. Srinivasan2dba6882011-05-10 07:55:22 -0700320void hv_ringbuffer_cleanup(struct hv_ring_buffer_info *ring_info)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700321{
Hank Janssen3e7ee492009-07-13 16:02:34 -0700322}
323
K. Y. Srinivasanb2a5a582011-05-10 07:55:30 -0700324/*
325 *
326 * hv_ringbuffer_write()
327 *
328 * Write to the ring buffer
329 *
330 */
K. Y. Srinivasan633c4dc2011-05-10 07:55:23 -0700331int hv_ringbuffer_write(struct hv_ring_buffer_info *outring_info,
Greg Kroah-Hartman3523a802009-08-17 17:22:08 -0700332 struct scatterlist *sglist, u32 sgcount)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700333{
C. Bartlett4408f532010-02-03 15:34:27 +0000334 int i = 0;
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800335 u32 bytes_avail_towrite;
336 u32 bytes_avail_toread;
337 u32 totalbytes_towrite = 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700338
Nicolas Palixb219b3f2009-07-30 17:37:23 +0200339 struct scatterlist *sg;
K. Y. Srinivasan66a60542011-05-10 07:55:33 -0700340 u32 next_write_location;
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800341 u64 prev_indices = 0;
Greg Kroah-Hartmana98f96e2009-07-15 14:55:14 -0700342 unsigned long flags;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700343
Nicolas Palixb219b3f2009-07-30 17:37:23 +0200344 for_each_sg(sglist, sg, sgcount, i)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700345 {
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800346 totalbytes_towrite += sg->length;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700347 }
348
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800349 totalbytes_towrite += sizeof(u64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700350
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800351 spin_lock_irqsave(&outring_info->ring_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700352
K. Y. Srinivasan2b8a9122011-05-10 07:55:29 -0700353 hv_get_ringbuffer_availbytes(outring_info,
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800354 &bytes_avail_toread,
355 &bytes_avail_towrite);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700356
Hank Janssen3e7ee492009-07-13 16:02:34 -0700357
C. Bartlett4408f532010-02-03 15:34:27 +0000358 /* If there is only room for the packet, assume it is full. */
359 /* Otherwise, the next time around, we think the ring buffer */
Bill Pemberton454f18a2009-07-27 16:47:24 -0400360 /* is empty since the read index == write index */
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800361 if (bytes_avail_towrite <= totalbytes_towrite) {
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800362 spin_unlock_irqrestore(&outring_info->ring_lock, flags);
K. Y. Srinivasand2598f02011-08-25 09:48:58 -0700363 return -EAGAIN;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700364 }
365
Bill Pemberton454f18a2009-07-27 16:47:24 -0400366 /* Write to the ring buffer */
K. Y. Srinivasan2b8a9122011-05-10 07:55:29 -0700367 next_write_location = hv_get_next_write_location(outring_info);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700368
Nicolas Palixb219b3f2009-07-30 17:37:23 +0200369 for_each_sg(sglist, sg, sgcount, i)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700370 {
K. Y. Srinivasan2b8a9122011-05-10 07:55:29 -0700371 next_write_location = hv_copyto_ringbuffer(outring_info,
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800372 next_write_location,
Nicolas Palixb219b3f2009-07-30 17:37:23 +0200373 sg_virt(sg),
374 sg->length);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700375 }
376
Bill Pemberton454f18a2009-07-27 16:47:24 -0400377 /* Set previous packet start */
K. Y. Srinivasan2b8a9122011-05-10 07:55:29 -0700378 prev_indices = hv_get_ring_bufferindices(outring_info);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700379
K. Y. Srinivasan2b8a9122011-05-10 07:55:29 -0700380 next_write_location = hv_copyto_ringbuffer(outring_info,
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800381 next_write_location,
382 &prev_indices,
Nicolas Palixb219b3f2009-07-30 17:37:23 +0200383 sizeof(u64));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700384
Bill Pemberton454f18a2009-07-27 16:47:24 -0400385 /* Make sure we flush all writes before updating the writeIndex */
K. Y. Srinivasane690b5a2011-06-06 15:49:53 -0700386 smp_wmb();
Hank Janssen3e7ee492009-07-13 16:02:34 -0700387
Bill Pemberton454f18a2009-07-27 16:47:24 -0400388 /* Now, update the write location */
K. Y. Srinivasan2b8a9122011-05-10 07:55:29 -0700389 hv_set_next_write_location(outring_info, next_write_location);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700390
Hank Janssen3e7ee492009-07-13 16:02:34 -0700391
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800392 spin_unlock_irqrestore(&outring_info->ring_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700393 return 0;
394}
395
396
K. Y. Srinivasanb2a5a582011-05-10 07:55:30 -0700397/*
398 *
399 * hv_ringbuffer_peek()
400 *
401 * Read without advancing the read index
402 *
403 */
K. Y. Srinivasana89186c2011-05-10 07:55:24 -0700404int hv_ringbuffer_peek(struct hv_ring_buffer_info *Inring_info,
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800405 void *Buffer, u32 buflen)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700406{
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800407 u32 bytes_avail_towrite;
408 u32 bytes_avail_toread;
409 u32 next_read_location = 0;
Greg Kroah-Hartmana98f96e2009-07-15 14:55:14 -0700410 unsigned long flags;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700411
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800412 spin_lock_irqsave(&Inring_info->ring_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700413
K. Y. Srinivasan2b8a9122011-05-10 07:55:29 -0700414 hv_get_ringbuffer_availbytes(Inring_info,
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800415 &bytes_avail_toread,
416 &bytes_avail_towrite);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700417
Bill Pemberton454f18a2009-07-27 16:47:24 -0400418 /* Make sure there is something to read */
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800419 if (bytes_avail_toread < buflen) {
Hank Janssen3e7ee492009-07-13 16:02:34 -0700420
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800421 spin_unlock_irqrestore(&Inring_info->ring_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700422
K. Y. Srinivasand2598f02011-08-25 09:48:58 -0700423 return -EAGAIN;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700424 }
425
Bill Pemberton454f18a2009-07-27 16:47:24 -0400426 /* Convert to byte offset */
K. Y. Srinivasan2b8a9122011-05-10 07:55:29 -0700427 next_read_location = hv_get_next_read_location(Inring_info);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700428
K. Y. Srinivasan2b8a9122011-05-10 07:55:29 -0700429 next_read_location = hv_copyfrom_ringbuffer(Inring_info,
C. Bartlett4408f532010-02-03 15:34:27 +0000430 Buffer,
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800431 buflen,
432 next_read_location);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700433
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800434 spin_unlock_irqrestore(&Inring_info->ring_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700435
436 return 0;
437}
438
439
K. Y. Srinivasanb2a5a582011-05-10 07:55:30 -0700440/*
441 *
442 * hv_ringbuffer_read()
443 *
444 * Read and advance the read index
445 *
446 */
K. Y. Srinivasan38397c82011-05-10 07:55:25 -0700447int hv_ringbuffer_read(struct hv_ring_buffer_info *inring_info, void *buffer,
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800448 u32 buflen, u32 offset)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700449{
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800450 u32 bytes_avail_towrite;
451 u32 bytes_avail_toread;
452 u32 next_read_location = 0;
453 u64 prev_indices = 0;
Greg Kroah-Hartmana98f96e2009-07-15 14:55:14 -0700454 unsigned long flags;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700455
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800456 if (buflen <= 0)
Bill Pembertona16e1482010-05-05 15:27:50 -0400457 return -EINVAL;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700458
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800459 spin_lock_irqsave(&inring_info->ring_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700460
K. Y. Srinivasan2b8a9122011-05-10 07:55:29 -0700461 hv_get_ringbuffer_availbytes(inring_info,
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800462 &bytes_avail_toread,
463 &bytes_avail_towrite);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700464
Bill Pemberton454f18a2009-07-27 16:47:24 -0400465 /* Make sure there is something to read */
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800466 if (bytes_avail_toread < buflen) {
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800467 spin_unlock_irqrestore(&inring_info->ring_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700468
K. Y. Srinivasand2598f02011-08-25 09:48:58 -0700469 return -EAGAIN;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700470 }
471
Haiyang Zhang1ac58642010-11-08 14:04:47 -0800472 next_read_location =
K. Y. Srinivasan2b8a9122011-05-10 07:55:29 -0700473 hv_get_next_readlocation_withoffset(inring_info, offset);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700474
K. Y. Srinivasan2b8a9122011-05-10 07:55:29 -0700475 next_read_location = hv_copyfrom_ringbuffer(inring_info,
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800476 buffer,
477 buflen,
478 next_read_location);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700479
K. Y. Srinivasan2b8a9122011-05-10 07:55:29 -0700480 next_read_location = hv_copyfrom_ringbuffer(inring_info,
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800481 &prev_indices,
C. Bartlett4408f532010-02-03 15:34:27 +0000482 sizeof(u64),
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800483 next_read_location);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700484
Bill Pemberton454f18a2009-07-27 16:47:24 -0400485 /* Make sure all reads are done before we update the read index since */
C. Bartlett4408f532010-02-03 15:34:27 +0000486 /* the writer may start writing to the read area once the read index */
487 /*is updated */
K. Y. Srinivasanef0d5b22011-06-06 15:49:51 -0700488 smp_mb();
Hank Janssen3e7ee492009-07-13 16:02:34 -0700489
Bill Pemberton454f18a2009-07-27 16:47:24 -0400490 /* Update the read index */
K. Y. Srinivasan2b8a9122011-05-10 07:55:29 -0700491 hv_set_next_read_location(inring_info, next_read_location);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700492
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800493 spin_unlock_irqrestore(&inring_info->ring_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700494
495 return 0;
496}