Drivers: hv: Optimize signaling in the read path
[deliverable/linux.git] / drivers / hv / ring_buffer.c
CommitLineData
3e7ee490
HJ
1/*
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>
b2a5a585 21 * K. Y. Srinivasan <kys@microsoft.com>
3e7ee490
HJ
22 *
23 */
0a46618d 24#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
3e7ee490 25
a0086dc5
GKH
26#include <linux/kernel.h>
27#include <linux/mm.h>
46a97191 28#include <linux/hyperv.h>
3f335ea2 29
0f2a6619 30#include "hyperv_vmbus.h"
3e7ee490 31
6fdf3b21
S
32void hv_begin_read(struct hv_ring_buffer_info *rbi)
33{
34 rbi->ring_buffer->interrupt_mask = 1;
35 smp_mb();
36}
37
38u32 hv_end_read(struct hv_ring_buffer_info *rbi)
39{
40 u32 read;
41 u32 write;
42
43 rbi->ring_buffer->interrupt_mask = 0;
44 smp_mb();
45
46 /*
47 * Now check to see if the ring buffer is still empty.
48 * If it is not, we raced and we need to process new
49 * incoming messages.
50 */
51 hv_get_ringbuffer_availbytes(rbi, &read, &write);
52
53 return read;
54}
55
3e7ee490 56
b2a5a585
S
57/*
58 * hv_get_next_write_location()
59 *
60 * Get the next write location for the specified ring buffer
61 *
62 */
4d643114 63static inline u32
2b8a912e 64hv_get_next_write_location(struct hv_ring_buffer_info *ring_info)
3e7ee490 65{
fc8c72eb 66 u32 next = ring_info->ring_buffer->write_index;
3e7ee490 67
3e7ee490
HJ
68 return next;
69}
70
b2a5a585
S
71/*
72 * hv_set_next_write_location()
73 *
74 * Set the next write location for the specified ring buffer
75 *
76 */
3e7ee490 77static inline void
2b8a912e 78hv_set_next_write_location(struct hv_ring_buffer_info *ring_info,
fc8c72eb 79 u32 next_write_location)
3e7ee490 80{
fc8c72eb 81 ring_info->ring_buffer->write_index = next_write_location;
3e7ee490
HJ
82}
83
b2a5a585
S
84/*
85 * hv_get_next_read_location()
86 *
87 * Get the next read location for the specified ring buffer
88 */
4d643114 89static inline u32
2b8a912e 90hv_get_next_read_location(struct hv_ring_buffer_info *ring_info)
3e7ee490 91{
fc8c72eb 92 u32 next = ring_info->ring_buffer->read_index;
3e7ee490 93
3e7ee490
HJ
94 return next;
95}
96
b2a5a585
S
97/*
98 * hv_get_next_readlocation_withoffset()
99 *
100 * Get the next read location + offset for the specified ring buffer.
101 * This allows the caller to skip
102 */
4d643114 103static inline u32
2b8a912e 104hv_get_next_readlocation_withoffset(struct hv_ring_buffer_info *ring_info,
1ac58644 105 u32 offset)
3e7ee490 106{
fc8c72eb 107 u32 next = ring_info->ring_buffer->read_index;
3e7ee490 108
fc8c72eb
HZ
109 next += offset;
110 next %= ring_info->ring_datasize;
3e7ee490
HJ
111
112 return next;
113}
114
b2a5a585
S
115/*
116 *
117 * hv_set_next_read_location()
118 *
119 * Set the next read location for the specified ring buffer
120 *
121 */
3e7ee490 122static inline void
2b8a912e 123hv_set_next_read_location(struct hv_ring_buffer_info *ring_info,
fc8c72eb 124 u32 next_read_location)
3e7ee490 125{
fc8c72eb 126 ring_info->ring_buffer->read_index = next_read_location;
3e7ee490
HJ
127}
128
129
b2a5a585
S
130/*
131 *
132 * hv_get_ring_buffer()
133 *
134 * Get the start of the ring buffer
135 */
8282c400 136static inline void *
2b8a912e 137hv_get_ring_buffer(struct hv_ring_buffer_info *ring_info)
3e7ee490 138{
fc8c72eb 139 return (void *)ring_info->ring_buffer->buffer;
3e7ee490
HJ
140}
141
142
b2a5a585
S
143/*
144 *
145 * hv_get_ring_buffersize()
146 *
147 * Get the size of the ring buffer
148 */
4d643114 149static inline u32
2b8a912e 150hv_get_ring_buffersize(struct hv_ring_buffer_info *ring_info)
3e7ee490 151{
fc8c72eb 152 return ring_info->ring_datasize;
3e7ee490
HJ
153}
154
b2a5a585
S
155/*
156 *
157 * hv_get_ring_bufferindices()
158 *
159 * Get the read and write indices as u64 of the specified ring buffer
160 *
161 */
59471438 162static inline u64
2b8a912e 163hv_get_ring_bufferindices(struct hv_ring_buffer_info *ring_info)
3e7ee490 164{
fc8c72eb 165 return (u64)ring_info->ring_buffer->write_index << 32;
3e7ee490
HJ
166}
167
8f1136ae
S
168/*
169 *
170 * hv_copyfrom_ringbuffer()
171 *
172 * Helper routine to copy to source from ring buffer.
173 * Assume there is enough room. Handles wrap-around in src case only!!
174 *
175 */
176static u32 hv_copyfrom_ringbuffer(
177 struct hv_ring_buffer_info *ring_info,
178 void *dest,
179 u32 destlen,
180 u32 start_read_offset)
181{
182 void *ring_buffer = hv_get_ring_buffer(ring_info);
183 u32 ring_buffer_size = hv_get_ring_buffersize(ring_info);
184
185 u32 frag_len;
186
187 /* wrap-around detected at the src */
188 if (destlen > ring_buffer_size - start_read_offset) {
189 frag_len = ring_buffer_size - start_read_offset;
190
191 memcpy(dest, ring_buffer + start_read_offset, frag_len);
192 memcpy(dest + frag_len, ring_buffer, destlen - frag_len);
193 } else
194
195 memcpy(dest, ring_buffer + start_read_offset, destlen);
196
197
198 start_read_offset += destlen;
199 start_read_offset %= ring_buffer_size;
200
201 return start_read_offset;
202}
203
204
7581578d
S
205/*
206 *
207 * hv_copyto_ringbuffer()
208 *
209 * Helper routine to copy from source to ring buffer.
210 * Assume there is enough room. Handles wrap-around in dest case only!!
211 *
212 */
213static u32 hv_copyto_ringbuffer(
fc8c72eb
HZ
214 struct hv_ring_buffer_info *ring_info,
215 u32 start_write_offset,
216 void *src,
7581578d
S
217 u32 srclen)
218{
219 void *ring_buffer = hv_get_ring_buffer(ring_info);
220 u32 ring_buffer_size = hv_get_ring_buffersize(ring_info);
221 u32 frag_len;
222
223 /* wrap-around detected! */
224 if (srclen > ring_buffer_size - start_write_offset) {
225 frag_len = ring_buffer_size - start_write_offset;
226 memcpy(ring_buffer + start_write_offset, src, frag_len);
227 memcpy(ring_buffer, src + frag_len, srclen - frag_len);
228 } else
229 memcpy(ring_buffer + start_write_offset, src, srclen);
3e7ee490 230
7581578d
S
231 start_write_offset += srclen;
232 start_write_offset %= ring_buffer_size;
233
234 return start_write_offset;
235}
3e7ee490 236
b2a5a585
S
237/*
238 *
239 * hv_ringbuffer_get_debuginfo()
240 *
241 * Get various debug metrics for the specified ring buffer
242 *
243 */
a75b61d5 244void hv_ringbuffer_get_debuginfo(struct hv_ring_buffer_info *ring_info,
80682b7a 245 struct hv_ring_buffer_debug_info *debug_info)
3e7ee490 246{
fc8c72eb
HZ
247 u32 bytes_avail_towrite;
248 u32 bytes_avail_toread;
3e7ee490 249
fc8c72eb 250 if (ring_info->ring_buffer) {
2b8a912e 251 hv_get_ringbuffer_availbytes(ring_info,
fc8c72eb
HZ
252 &bytes_avail_toread,
253 &bytes_avail_towrite);
3e7ee490 254
fc8c72eb
HZ
255 debug_info->bytes_avail_toread = bytes_avail_toread;
256 debug_info->bytes_avail_towrite = bytes_avail_towrite;
82f8bd40 257 debug_info->current_read_index =
fc8c72eb 258 ring_info->ring_buffer->read_index;
82f8bd40 259 debug_info->current_write_index =
fc8c72eb 260 ring_info->ring_buffer->write_index;
82f8bd40 261 debug_info->current_interrupt_mask =
fc8c72eb 262 ring_info->ring_buffer->interrupt_mask;
3e7ee490
HJ
263 }
264}
265
266
b2a5a585
S
267/*
268 *
269 * hv_get_ringbuffer_interrupt_mask()
270 *
271 * Get the interrupt mask for the specified ring buffer
272 *
273 */
decc49da 274u32 hv_get_ringbuffer_interrupt_mask(struct hv_ring_buffer_info *rbi)
3e7ee490 275{
82f8bd40 276 return rbi->ring_buffer->interrupt_mask;
3e7ee490
HJ
277}
278
b2a5a585
S
279/*
280 *
281 * hv_ringbuffer_init()
282 *
283 *Initialize the ring buffer
284 *
285 */
72a95cbc 286int hv_ringbuffer_init(struct hv_ring_buffer_info *ring_info,
fc8c72eb 287 void *buffer, u32 buflen)
3e7ee490 288{
4a1b3acc 289 if (sizeof(struct hv_ring_buffer) != PAGE_SIZE)
3324fb40 290 return -EINVAL;
3e7ee490 291
fc8c72eb 292 memset(ring_info, 0, sizeof(struct hv_ring_buffer_info));
3e7ee490 293
fc8c72eb
HZ
294 ring_info->ring_buffer = (struct hv_ring_buffer *)buffer;
295 ring_info->ring_buffer->read_index =
296 ring_info->ring_buffer->write_index = 0;
3e7ee490 297
fc8c72eb
HZ
298 ring_info->ring_size = buflen;
299 ring_info->ring_datasize = buflen - sizeof(struct hv_ring_buffer);
3e7ee490 300
fc8c72eb 301 spin_lock_init(&ring_info->ring_lock);
3e7ee490
HJ
302
303 return 0;
304}
305
b2a5a585
S
306/*
307 *
308 * hv_ringbuffer_cleanup()
309 *
310 * Cleanup the ring buffer
311 *
312 */
2dba688b 313void hv_ringbuffer_cleanup(struct hv_ring_buffer_info *ring_info)
3e7ee490 314{
3e7ee490
HJ
315}
316
b2a5a585
S
317/*
318 *
319 * hv_ringbuffer_write()
320 *
321 * Write to the ring buffer
322 *
323 */
633c4dce 324int hv_ringbuffer_write(struct hv_ring_buffer_info *outring_info,
3523a805 325 struct scatterlist *sglist, u32 sgcount)
3e7ee490 326{
4408f531 327 int i = 0;
fc8c72eb
HZ
328 u32 bytes_avail_towrite;
329 u32 bytes_avail_toread;
330 u32 totalbytes_towrite = 0;
3e7ee490 331
b219b3f7 332 struct scatterlist *sg;
66a60543 333 u32 next_write_location;
fc8c72eb 334 u64 prev_indices = 0;
a98f96ee 335 unsigned long flags;
3e7ee490 336
b219b3f7 337 for_each_sg(sglist, sg, sgcount, i)
3e7ee490 338 {
fc8c72eb 339 totalbytes_towrite += sg->length;
3e7ee490
HJ
340 }
341
fc8c72eb 342 totalbytes_towrite += sizeof(u64);
3e7ee490 343
fc8c72eb 344 spin_lock_irqsave(&outring_info->ring_lock, flags);
3e7ee490 345
2b8a912e 346 hv_get_ringbuffer_availbytes(outring_info,
fc8c72eb
HZ
347 &bytes_avail_toread,
348 &bytes_avail_towrite);
3e7ee490 349
3e7ee490 350
4408f531
B
351 /* If there is only room for the packet, assume it is full. */
352 /* Otherwise, the next time around, we think the ring buffer */
454f18a9 353 /* is empty since the read index == write index */
fc8c72eb 354 if (bytes_avail_towrite <= totalbytes_towrite) {
fc8c72eb 355 spin_unlock_irqrestore(&outring_info->ring_lock, flags);
d2598f01 356 return -EAGAIN;
3e7ee490
HJ
357 }
358
454f18a9 359 /* Write to the ring buffer */
2b8a912e 360 next_write_location = hv_get_next_write_location(outring_info);
3e7ee490 361
b219b3f7 362 for_each_sg(sglist, sg, sgcount, i)
3e7ee490 363 {
2b8a912e 364 next_write_location = hv_copyto_ringbuffer(outring_info,
fc8c72eb 365 next_write_location,
b219b3f7
NP
366 sg_virt(sg),
367 sg->length);
3e7ee490
HJ
368 }
369
454f18a9 370 /* Set previous packet start */
2b8a912e 371 prev_indices = hv_get_ring_bufferindices(outring_info);
3e7ee490 372
2b8a912e 373 next_write_location = hv_copyto_ringbuffer(outring_info,
fc8c72eb
HZ
374 next_write_location,
375 &prev_indices,
b219b3f7 376 sizeof(u64));
3e7ee490 377
454f18a9 378 /* Make sure we flush all writes before updating the writeIndex */
e690b5a9 379 smp_wmb();
3e7ee490 380
454f18a9 381 /* Now, update the write location */
2b8a912e 382 hv_set_next_write_location(outring_info, next_write_location);
3e7ee490 383
3e7ee490 384
fc8c72eb 385 spin_unlock_irqrestore(&outring_info->ring_lock, flags);
3e7ee490
HJ
386 return 0;
387}
388
389
b2a5a585
S
390/*
391 *
392 * hv_ringbuffer_peek()
393 *
394 * Read without advancing the read index
395 *
396 */
a89186c2 397int hv_ringbuffer_peek(struct hv_ring_buffer_info *Inring_info,
fc8c72eb 398 void *Buffer, u32 buflen)
3e7ee490 399{
fc8c72eb
HZ
400 u32 bytes_avail_towrite;
401 u32 bytes_avail_toread;
402 u32 next_read_location = 0;
a98f96ee 403 unsigned long flags;
3e7ee490 404
fc8c72eb 405 spin_lock_irqsave(&Inring_info->ring_lock, flags);
3e7ee490 406
2b8a912e 407 hv_get_ringbuffer_availbytes(Inring_info,
fc8c72eb
HZ
408 &bytes_avail_toread,
409 &bytes_avail_towrite);
3e7ee490 410
454f18a9 411 /* Make sure there is something to read */
fc8c72eb 412 if (bytes_avail_toread < buflen) {
3e7ee490 413
fc8c72eb 414 spin_unlock_irqrestore(&Inring_info->ring_lock, flags);
3e7ee490 415
d2598f01 416 return -EAGAIN;
3e7ee490
HJ
417 }
418
454f18a9 419 /* Convert to byte offset */
2b8a912e 420 next_read_location = hv_get_next_read_location(Inring_info);
3e7ee490 421
2b8a912e 422 next_read_location = hv_copyfrom_ringbuffer(Inring_info,
4408f531 423 Buffer,
fc8c72eb
HZ
424 buflen,
425 next_read_location);
3e7ee490 426
fc8c72eb 427 spin_unlock_irqrestore(&Inring_info->ring_lock, flags);
3e7ee490
HJ
428
429 return 0;
430}
431
432
b2a5a585
S
433/*
434 *
435 * hv_ringbuffer_read()
436 *
437 * Read and advance the read index
438 *
439 */
38397c8a 440int hv_ringbuffer_read(struct hv_ring_buffer_info *inring_info, void *buffer,
fc8c72eb 441 u32 buflen, u32 offset)
3e7ee490 442{
fc8c72eb
HZ
443 u32 bytes_avail_towrite;
444 u32 bytes_avail_toread;
445 u32 next_read_location = 0;
446 u64 prev_indices = 0;
a98f96ee 447 unsigned long flags;
3e7ee490 448
fc8c72eb 449 if (buflen <= 0)
a16e1485 450 return -EINVAL;
3e7ee490 451
fc8c72eb 452 spin_lock_irqsave(&inring_info->ring_lock, flags);
3e7ee490 453
2b8a912e 454 hv_get_ringbuffer_availbytes(inring_info,
fc8c72eb
HZ
455 &bytes_avail_toread,
456 &bytes_avail_towrite);
3e7ee490 457
454f18a9 458 /* Make sure there is something to read */
fc8c72eb 459 if (bytes_avail_toread < buflen) {
fc8c72eb 460 spin_unlock_irqrestore(&inring_info->ring_lock, flags);
3e7ee490 461
d2598f01 462 return -EAGAIN;
3e7ee490
HJ
463 }
464
1ac58644 465 next_read_location =
2b8a912e 466 hv_get_next_readlocation_withoffset(inring_info, offset);
3e7ee490 467
2b8a912e 468 next_read_location = hv_copyfrom_ringbuffer(inring_info,
fc8c72eb
HZ
469 buffer,
470 buflen,
471 next_read_location);
3e7ee490 472
2b8a912e 473 next_read_location = hv_copyfrom_ringbuffer(inring_info,
fc8c72eb 474 &prev_indices,
4408f531 475 sizeof(u64),
fc8c72eb 476 next_read_location);
3e7ee490 477
454f18a9 478 /* Make sure all reads are done before we update the read index since */
4408f531
B
479 /* the writer may start writing to the read area once the read index */
480 /*is updated */
ef0d5b23 481 smp_mb();
3e7ee490 482
454f18a9 483 /* Update the read index */
2b8a912e 484 hv_set_next_read_location(inring_info, next_read_location);
3e7ee490 485
fc8c72eb 486 spin_unlock_irqrestore(&inring_info->ring_lock, flags);
3e7ee490
HJ
487
488 return 0;
489}
This page took 0.449577 seconds and 5 git commands to generate.