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