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