blob: 7233c88f01b8366547467215dc7c8492f03daacb [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
K. Y. Srinivasanb2a5a582011-05-10 07:55:30 -070033/*
34 * hv_get_next_write_location()
35 *
36 * Get the next write location for the specified ring buffer
37 *
38 */
Greg Kroah-Hartman4d643112009-07-14 15:09:36 -070039static inline u32
K. Y. Srinivasan2b8a9122011-05-10 07:55:29 -070040hv_get_next_write_location(struct hv_ring_buffer_info *ring_info)
Hank Janssen3e7ee492009-07-13 16:02:34 -070041{
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -080042 u32 next = ring_info->ring_buffer->write_index;
Hank Janssen3e7ee492009-07-13 16:02:34 -070043
Hank Janssen3e7ee492009-07-13 16:02:34 -070044 return next;
45}
46
K. Y. Srinivasanb2a5a582011-05-10 07:55:30 -070047/*
48 * hv_set_next_write_location()
49 *
50 * Set the next write location for the specified ring buffer
51 *
52 */
Hank Janssen3e7ee492009-07-13 16:02:34 -070053static inline void
K. Y. Srinivasan2b8a9122011-05-10 07:55:29 -070054hv_set_next_write_location(struct hv_ring_buffer_info *ring_info,
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -080055 u32 next_write_location)
Hank Janssen3e7ee492009-07-13 16:02:34 -070056{
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -080057 ring_info->ring_buffer->write_index = next_write_location;
Hank Janssen3e7ee492009-07-13 16:02:34 -070058}
59
K. Y. Srinivasanb2a5a582011-05-10 07:55:30 -070060/*
61 * hv_get_next_read_location()
62 *
63 * Get the next read location for the specified ring buffer
64 */
Greg Kroah-Hartman4d643112009-07-14 15:09:36 -070065static inline u32
K. Y. Srinivasan2b8a9122011-05-10 07:55:29 -070066hv_get_next_read_location(struct hv_ring_buffer_info *ring_info)
Hank Janssen3e7ee492009-07-13 16:02:34 -070067{
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -080068 u32 next = ring_info->ring_buffer->read_index;
Hank Janssen3e7ee492009-07-13 16:02:34 -070069
Hank Janssen3e7ee492009-07-13 16:02:34 -070070 return next;
71}
72
K. Y. Srinivasanb2a5a582011-05-10 07:55:30 -070073/*
74 * hv_get_next_readlocation_withoffset()
75 *
76 * Get the next read location + offset for the specified ring buffer.
77 * This allows the caller to skip
78 */
Greg Kroah-Hartman4d643112009-07-14 15:09:36 -070079static inline u32
K. Y. Srinivasan2b8a9122011-05-10 07:55:29 -070080hv_get_next_readlocation_withoffset(struct hv_ring_buffer_info *ring_info,
Haiyang Zhang1ac58642010-11-08 14:04:47 -080081 u32 offset)
Hank Janssen3e7ee492009-07-13 16:02:34 -070082{
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -080083 u32 next = ring_info->ring_buffer->read_index;
Hank Janssen3e7ee492009-07-13 16:02:34 -070084
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -080085 next += offset;
86 next %= ring_info->ring_datasize;
Hank Janssen3e7ee492009-07-13 16:02:34 -070087
88 return next;
89}
90
K. Y. Srinivasanb2a5a582011-05-10 07:55:30 -070091/*
92 *
93 * hv_set_next_read_location()
94 *
95 * Set the next read location for the specified ring buffer
96 *
97 */
Hank Janssen3e7ee492009-07-13 16:02:34 -070098static inline void
K. Y. Srinivasan2b8a9122011-05-10 07:55:29 -070099hv_set_next_read_location(struct hv_ring_buffer_info *ring_info,
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800100 u32 next_read_location)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700101{
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800102 ring_info->ring_buffer->read_index = next_read_location;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700103}
104
105
K. Y. Srinivasanb2a5a582011-05-10 07:55:30 -0700106/*
107 *
108 * hv_get_ring_buffer()
109 *
110 * Get the start of the ring buffer
111 */
Greg Kroah-Hartman8282c402009-07-14 15:06:28 -0700112static inline void *
K. Y. Srinivasan2b8a9122011-05-10 07:55:29 -0700113hv_get_ring_buffer(struct hv_ring_buffer_info *ring_info)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700114{
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800115 return (void *)ring_info->ring_buffer->buffer;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700116}
117
118
K. Y. Srinivasanb2a5a582011-05-10 07:55:30 -0700119/*
120 *
121 * hv_get_ring_buffersize()
122 *
123 * Get the size of the ring buffer
124 */
Greg Kroah-Hartman4d643112009-07-14 15:09:36 -0700125static inline u32
K. Y. Srinivasan2b8a9122011-05-10 07:55:29 -0700126hv_get_ring_buffersize(struct hv_ring_buffer_info *ring_info)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700127{
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800128 return ring_info->ring_datasize;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700129}
130
K. Y. Srinivasanb2a5a582011-05-10 07:55:30 -0700131/*
132 *
133 * hv_get_ring_bufferindices()
134 *
135 * Get the read and write indices as u64 of the specified ring buffer
136 *
137 */
Greg Kroah-Hartman59471432009-07-14 15:10:26 -0700138static inline u64
K. Y. Srinivasan2b8a9122011-05-10 07:55:29 -0700139hv_get_ring_bufferindices(struct hv_ring_buffer_info *ring_info)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700140{
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800141 return (u64)ring_info->ring_buffer->write_index << 32;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700142}
143
K. Y. Srinivasan8f1136a2011-05-10 07:55:31 -0700144/*
145 *
146 * hv_copyfrom_ringbuffer()
147 *
148 * Helper routine to copy to source from ring buffer.
149 * Assume there is enough room. Handles wrap-around in src case only!!
150 *
151 */
152static u32 hv_copyfrom_ringbuffer(
153 struct hv_ring_buffer_info *ring_info,
154 void *dest,
155 u32 destlen,
156 u32 start_read_offset)
157{
158 void *ring_buffer = hv_get_ring_buffer(ring_info);
159 u32 ring_buffer_size = hv_get_ring_buffersize(ring_info);
160
161 u32 frag_len;
162
163 /* wrap-around detected at the src */
164 if (destlen > ring_buffer_size - start_read_offset) {
165 frag_len = ring_buffer_size - start_read_offset;
166
167 memcpy(dest, ring_buffer + start_read_offset, frag_len);
168 memcpy(dest + frag_len, ring_buffer, destlen - frag_len);
169 } else
170
171 memcpy(dest, ring_buffer + start_read_offset, destlen);
172
173
174 start_read_offset += destlen;
175 start_read_offset %= ring_buffer_size;
176
177 return start_read_offset;
178}
179
180
K. Y. Srinivasan75815782011-05-10 07:55:32 -0700181/*
182 *
183 * hv_copyto_ringbuffer()
184 *
185 * Helper routine to copy from source to ring buffer.
186 * Assume there is enough room. Handles wrap-around in dest case only!!
187 *
188 */
189static u32 hv_copyto_ringbuffer(
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800190 struct hv_ring_buffer_info *ring_info,
191 u32 start_write_offset,
192 void *src,
K. Y. Srinivasan75815782011-05-10 07:55:32 -0700193 u32 srclen)
194{
195 void *ring_buffer = hv_get_ring_buffer(ring_info);
196 u32 ring_buffer_size = hv_get_ring_buffersize(ring_info);
197 u32 frag_len;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700198
K. Y. Srinivasan75815782011-05-10 07:55:32 -0700199 /* wrap-around detected! */
200 if (srclen > ring_buffer_size - start_write_offset) {
201 frag_len = ring_buffer_size - start_write_offset;
202 memcpy(ring_buffer + start_write_offset, src, frag_len);
203 memcpy(ring_buffer, src + frag_len, srclen - frag_len);
204 } else
205 memcpy(ring_buffer + start_write_offset, src, srclen);
206
207 start_write_offset += srclen;
208 start_write_offset %= ring_buffer_size;
209
210 return start_write_offset;
211}
Hank Janssen3e7ee492009-07-13 16:02:34 -0700212
K. Y. Srinivasanb2a5a582011-05-10 07:55:30 -0700213/*
214 *
215 * hv_ringbuffer_get_debuginfo()
216 *
217 * Get various debug metrics for the specified ring buffer
218 *
219 */
K. Y. Srinivasana75b61d2011-05-10 07:55:28 -0700220void hv_ringbuffer_get_debuginfo(struct hv_ring_buffer_info *ring_info,
Greg Kroah-Hartman80682b72010-07-27 11:37:32 -0700221 struct hv_ring_buffer_debug_info *debug_info)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700222{
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800223 u32 bytes_avail_towrite;
224 u32 bytes_avail_toread;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700225
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800226 if (ring_info->ring_buffer) {
K. Y. Srinivasan2b8a9122011-05-10 07:55:29 -0700227 hv_get_ringbuffer_availbytes(ring_info,
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800228 &bytes_avail_toread,
229 &bytes_avail_towrite);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700230
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800231 debug_info->bytes_avail_toread = bytes_avail_toread;
232 debug_info->bytes_avail_towrite = bytes_avail_towrite;
Haiyang Zhang82f8bd42010-11-08 14:04:45 -0800233 debug_info->current_read_index =
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800234 ring_info->ring_buffer->read_index;
Haiyang Zhang82f8bd42010-11-08 14:04:45 -0800235 debug_info->current_write_index =
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800236 ring_info->ring_buffer->write_index;
Haiyang Zhang82f8bd42010-11-08 14:04:45 -0800237 debug_info->current_interrupt_mask =
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800238 ring_info->ring_buffer->interrupt_mask;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700239 }
240}
241
242
K. Y. Srinivasanb2a5a582011-05-10 07:55:30 -0700243/*
244 *
245 * hv_get_ringbuffer_interrupt_mask()
246 *
247 * Get the interrupt mask for the specified ring buffer
248 *
249 */
K. Y. Srinivasandecc49d2011-05-10 07:55:26 -0700250u32 hv_get_ringbuffer_interrupt_mask(struct hv_ring_buffer_info *rbi)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700251{
Haiyang Zhang82f8bd42010-11-08 14:04:45 -0800252 return rbi->ring_buffer->interrupt_mask;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700253}
254
K. Y. Srinivasanb2a5a582011-05-10 07:55:30 -0700255/*
256 *
257 * hv_ringbuffer_init()
258 *
259 *Initialize the ring buffer
260 *
261 */
K. Y. Srinivasan72a95cb2011-05-10 07:55:21 -0700262int hv_ringbuffer_init(struct hv_ring_buffer_info *ring_info,
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800263 void *buffer, u32 buflen)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700264{
Greg Kroah-Hartman4a1b3ac2010-07-27 11:47:08 -0700265 if (sizeof(struct hv_ring_buffer) != PAGE_SIZE)
Bill Pemberton3324fb42010-05-05 15:27:49 -0400266 return -EINVAL;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700267
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800268 memset(ring_info, 0, sizeof(struct hv_ring_buffer_info));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700269
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800270 ring_info->ring_buffer = (struct hv_ring_buffer *)buffer;
271 ring_info->ring_buffer->read_index =
272 ring_info->ring_buffer->write_index = 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700273
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800274 ring_info->ring_size = buflen;
275 ring_info->ring_datasize = buflen - sizeof(struct hv_ring_buffer);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700276
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800277 spin_lock_init(&ring_info->ring_lock);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700278
279 return 0;
280}
281
K. Y. Srinivasanb2a5a582011-05-10 07:55:30 -0700282/*
283 *
284 * hv_ringbuffer_cleanup()
285 *
286 * Cleanup the ring buffer
287 *
288 */
K. Y. Srinivasan2dba6882011-05-10 07:55:22 -0700289void hv_ringbuffer_cleanup(struct hv_ring_buffer_info *ring_info)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700290{
Hank Janssen3e7ee492009-07-13 16:02:34 -0700291}
292
K. Y. Srinivasanb2a5a582011-05-10 07:55:30 -0700293/*
294 *
295 * hv_ringbuffer_write()
296 *
297 * Write to the ring buffer
298 *
299 */
K. Y. Srinivasan633c4dc2011-05-10 07:55:23 -0700300int hv_ringbuffer_write(struct hv_ring_buffer_info *outring_info,
Greg Kroah-Hartman3523a802009-08-17 17:22:08 -0700301 struct scatterlist *sglist, u32 sgcount)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700302{
C. Bartlett4408f532010-02-03 15:34:27 +0000303 int i = 0;
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800304 u32 bytes_avail_towrite;
305 u32 bytes_avail_toread;
306 u32 totalbytes_towrite = 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700307
Nicolas Palixb219b3f2009-07-30 17:37:23 +0200308 struct scatterlist *sg;
K. Y. Srinivasan66a60542011-05-10 07:55:33 -0700309 u32 next_write_location;
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800310 u64 prev_indices = 0;
Greg Kroah-Hartmana98f96e2009-07-15 14:55:14 -0700311 unsigned long flags;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700312
Nicolas Palixb219b3f2009-07-30 17:37:23 +0200313 for_each_sg(sglist, sg, sgcount, i)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700314 {
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800315 totalbytes_towrite += sg->length;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700316 }
317
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800318 totalbytes_towrite += sizeof(u64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700319
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800320 spin_lock_irqsave(&outring_info->ring_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700321
K. Y. Srinivasan2b8a9122011-05-10 07:55:29 -0700322 hv_get_ringbuffer_availbytes(outring_info,
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800323 &bytes_avail_toread,
324 &bytes_avail_towrite);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700325
Hank Janssen3e7ee492009-07-13 16:02:34 -0700326
C. Bartlett4408f532010-02-03 15:34:27 +0000327 /* If there is only room for the packet, assume it is full. */
328 /* Otherwise, the next time around, we think the ring buffer */
Bill Pemberton454f18a2009-07-27 16:47:24 -0400329 /* is empty since the read index == write index */
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800330 if (bytes_avail_towrite <= totalbytes_towrite) {
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800331 spin_unlock_irqrestore(&outring_info->ring_lock, flags);
K. Y. Srinivasand2598f02011-08-25 09:48:58 -0700332 return -EAGAIN;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700333 }
334
Bill Pemberton454f18a2009-07-27 16:47:24 -0400335 /* Write to the ring buffer */
K. Y. Srinivasan2b8a9122011-05-10 07:55:29 -0700336 next_write_location = hv_get_next_write_location(outring_info);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700337
Nicolas Palixb219b3f2009-07-30 17:37:23 +0200338 for_each_sg(sglist, sg, sgcount, i)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700339 {
K. Y. Srinivasan2b8a9122011-05-10 07:55:29 -0700340 next_write_location = hv_copyto_ringbuffer(outring_info,
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800341 next_write_location,
Nicolas Palixb219b3f2009-07-30 17:37:23 +0200342 sg_virt(sg),
343 sg->length);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700344 }
345
Bill Pemberton454f18a2009-07-27 16:47:24 -0400346 /* Set previous packet start */
K. Y. Srinivasan2b8a9122011-05-10 07:55:29 -0700347 prev_indices = hv_get_ring_bufferindices(outring_info);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700348
K. Y. Srinivasan2b8a9122011-05-10 07:55:29 -0700349 next_write_location = hv_copyto_ringbuffer(outring_info,
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800350 next_write_location,
351 &prev_indices,
Nicolas Palixb219b3f2009-07-30 17:37:23 +0200352 sizeof(u64));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700353
Bill Pemberton454f18a2009-07-27 16:47:24 -0400354 /* Make sure we flush all writes before updating the writeIndex */
K. Y. Srinivasane690b5a2011-06-06 15:49:53 -0700355 smp_wmb();
Hank Janssen3e7ee492009-07-13 16:02:34 -0700356
Bill Pemberton454f18a2009-07-27 16:47:24 -0400357 /* Now, update the write location */
K. Y. Srinivasan2b8a9122011-05-10 07:55:29 -0700358 hv_set_next_write_location(outring_info, next_write_location);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700359
Hank Janssen3e7ee492009-07-13 16:02:34 -0700360
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800361 spin_unlock_irqrestore(&outring_info->ring_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700362 return 0;
363}
364
365
K. Y. Srinivasanb2a5a582011-05-10 07:55:30 -0700366/*
367 *
368 * hv_ringbuffer_peek()
369 *
370 * Read without advancing the read index
371 *
372 */
K. Y. Srinivasana89186c2011-05-10 07:55:24 -0700373int hv_ringbuffer_peek(struct hv_ring_buffer_info *Inring_info,
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800374 void *Buffer, u32 buflen)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700375{
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800376 u32 bytes_avail_towrite;
377 u32 bytes_avail_toread;
378 u32 next_read_location = 0;
Greg Kroah-Hartmana98f96e2009-07-15 14:55:14 -0700379 unsigned long flags;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700380
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800381 spin_lock_irqsave(&Inring_info->ring_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700382
K. Y. Srinivasan2b8a9122011-05-10 07:55:29 -0700383 hv_get_ringbuffer_availbytes(Inring_info,
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800384 &bytes_avail_toread,
385 &bytes_avail_towrite);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700386
Bill Pemberton454f18a2009-07-27 16:47:24 -0400387 /* Make sure there is something to read */
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800388 if (bytes_avail_toread < buflen) {
Hank Janssen3e7ee492009-07-13 16:02:34 -0700389
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800390 spin_unlock_irqrestore(&Inring_info->ring_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700391
K. Y. Srinivasand2598f02011-08-25 09:48:58 -0700392 return -EAGAIN;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700393 }
394
Bill Pemberton454f18a2009-07-27 16:47:24 -0400395 /* Convert to byte offset */
K. Y. Srinivasan2b8a9122011-05-10 07:55:29 -0700396 next_read_location = hv_get_next_read_location(Inring_info);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700397
K. Y. Srinivasan2b8a9122011-05-10 07:55:29 -0700398 next_read_location = hv_copyfrom_ringbuffer(Inring_info,
C. Bartlett4408f532010-02-03 15:34:27 +0000399 Buffer,
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800400 buflen,
401 next_read_location);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700402
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800403 spin_unlock_irqrestore(&Inring_info->ring_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700404
405 return 0;
406}
407
408
K. Y. Srinivasanb2a5a582011-05-10 07:55:30 -0700409/*
410 *
411 * hv_ringbuffer_read()
412 *
413 * Read and advance the read index
414 *
415 */
K. Y. Srinivasan38397c82011-05-10 07:55:25 -0700416int hv_ringbuffer_read(struct hv_ring_buffer_info *inring_info, void *buffer,
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800417 u32 buflen, u32 offset)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700418{
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800419 u32 bytes_avail_towrite;
420 u32 bytes_avail_toread;
421 u32 next_read_location = 0;
422 u64 prev_indices = 0;
Greg Kroah-Hartmana98f96e2009-07-15 14:55:14 -0700423 unsigned long flags;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700424
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800425 if (buflen <= 0)
Bill Pembertona16e1482010-05-05 15:27:50 -0400426 return -EINVAL;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700427
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800428 spin_lock_irqsave(&inring_info->ring_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700429
K. Y. Srinivasan2b8a9122011-05-10 07:55:29 -0700430 hv_get_ringbuffer_availbytes(inring_info,
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800431 &bytes_avail_toread,
432 &bytes_avail_towrite);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700433
Bill Pemberton454f18a2009-07-27 16:47:24 -0400434 /* Make sure there is something to read */
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800435 if (bytes_avail_toread < buflen) {
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800436 spin_unlock_irqrestore(&inring_info->ring_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700437
K. Y. Srinivasand2598f02011-08-25 09:48:58 -0700438 return -EAGAIN;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700439 }
440
Haiyang Zhang1ac58642010-11-08 14:04:47 -0800441 next_read_location =
K. Y. Srinivasan2b8a9122011-05-10 07:55:29 -0700442 hv_get_next_readlocation_withoffset(inring_info, offset);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700443
K. Y. Srinivasan2b8a9122011-05-10 07:55:29 -0700444 next_read_location = hv_copyfrom_ringbuffer(inring_info,
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800445 buffer,
446 buflen,
447 next_read_location);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700448
K. Y. Srinivasan2b8a9122011-05-10 07:55:29 -0700449 next_read_location = hv_copyfrom_ringbuffer(inring_info,
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800450 &prev_indices,
C. Bartlett4408f532010-02-03 15:34:27 +0000451 sizeof(u64),
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800452 next_read_location);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700453
Bill Pemberton454f18a2009-07-27 16:47:24 -0400454 /* Make sure all reads are done before we update the read index since */
C. Bartlett4408f532010-02-03 15:34:27 +0000455 /* the writer may start writing to the read area once the read index */
456 /*is updated */
K. Y. Srinivasanef0d5b22011-06-06 15:49:51 -0700457 smp_mb();
Hank Janssen3e7ee492009-07-13 16:02:34 -0700458
Bill Pemberton454f18a2009-07-27 16:47:24 -0400459 /* Update the read index */
K. Y. Srinivasan2b8a9122011-05-10 07:55:29 -0700460 hv_set_next_read_location(inring_info, next_read_location);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700461
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800462 spin_unlock_irqrestore(&inring_info->ring_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700463
464 return 0;
465}