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