]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/hv/ring_buffer.c
vmbus: cleanup header file style
[mirror_ubuntu-artful-kernel.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>
011a7c3c 29#include <linux/uio.h>
9988ce68
VK
30#include <linux/vmalloc.h>
31#include <linux/slab.h>
3f335ea2 32
0f2a6619 33#include "hyperv_vmbus.h"
3e7ee490 34
98fa8cf4
S
35/*
36 * When we write to the ring buffer, check if the host needs to
37 * be signaled. Here is the details of this protocol:
38 *
39 * 1. The host guarantees that while it is draining the
40 * ring buffer, it will set the interrupt_mask to
41 * indicate it does not need to be interrupted when
42 * new data is placed.
43 *
44 * 2. The host guarantees that it will completely drain
45 * the ring buffer before exiting the read loop. Further,
46 * once the ring buffer is empty, it will clear the
47 * interrupt_mask and re-check to see if new data has
48 * arrived.
1f6ee4e7
S
49 *
50 * KYS: Oct. 30, 2016:
51 * It looks like Windows hosts have logic to deal with DOS attacks that
52 * can be triggered if it receives interrupts when it is not expecting
53 * the interrupt. The host expects interrupts only when the ring
54 * transitions from empty to non-empty (or full to non full on the guest
55 * to host ring).
56 * So, base the signaling decision solely on the ring state until the
57 * host logic is fixed.
98fa8cf4
S
58 */
59
b103a56f 60static void hv_signal_on_write(u32 old_write, struct vmbus_channel *channel)
98fa8cf4 61{
1f6ee4e7
S
62 struct hv_ring_buffer_info *rbi = &channel->outbound;
63
dcd0eeca 64 virt_mb();
d45faaee 65 if (READ_ONCE(rbi->ring_buffer->interrupt_mask))
1f6ee4e7 66 return;
98fa8cf4 67
e91e84fa 68 /* check interrupt_mask before read_index */
dcd0eeca 69 virt_rmb();
98fa8cf4
S
70 /*
71 * This is the only case we need to signal when the
72 * ring transitions from being empty to non-empty.
73 */
d45faaee 74 if (old_write == READ_ONCE(rbi->ring_buffer->read_index))
1f6ee4e7 75 vmbus_setevent(channel);
98fa8cf4
S
76}
77
822f18d4 78/* Get the next write location for the specified ring buffer. */
4d643114 79static inline u32
2b8a912e 80hv_get_next_write_location(struct hv_ring_buffer_info *ring_info)
3e7ee490 81{
fc8c72eb 82 u32 next = ring_info->ring_buffer->write_index;
3e7ee490 83
3e7ee490
HJ
84 return next;
85}
86
822f18d4 87/* Set the next write location for the specified ring buffer. */
3e7ee490 88static inline void
2b8a912e 89hv_set_next_write_location(struct hv_ring_buffer_info *ring_info,
fc8c72eb 90 u32 next_write_location)
3e7ee490 91{
fc8c72eb 92 ring_info->ring_buffer->write_index = next_write_location;
3e7ee490
HJ
93}
94
822f18d4 95/* Get the next read location for the specified ring buffer. */
4d643114 96static inline u32
e4165a0f 97hv_get_next_read_location(const struct hv_ring_buffer_info *ring_info)
3e7ee490 98{
e4165a0f 99 return ring_info->ring_buffer->read_index;
3e7ee490
HJ
100}
101
b2a5a585 102/*
b2a5a585 103 * Get the next read location + offset for the specified ring buffer.
822f18d4 104 * This allows the caller to skip.
b2a5a585 105 */
4d643114 106static inline u32
e4165a0f
SH
107hv_get_next_readlocation_withoffset(const struct hv_ring_buffer_info *ring_info,
108 u32 offset)
3e7ee490 109{
fc8c72eb 110 u32 next = ring_info->ring_buffer->read_index;
3e7ee490 111
fc8c72eb 112 next += offset;
8d12f882
SH
113 if (next >= ring_info->ring_datasize)
114 next -= ring_info->ring_datasize;
3e7ee490
HJ
115
116 return next;
117}
118
822f18d4 119/* Set the next read location for the specified ring buffer. */
3e7ee490 120static inline void
2b8a912e 121hv_set_next_read_location(struct hv_ring_buffer_info *ring_info,
fc8c72eb 122 u32 next_read_location)
3e7ee490 123{
fc8c72eb 124 ring_info->ring_buffer->read_index = next_read_location;
ab028db4 125 ring_info->priv_read_index = next_read_location;
3e7ee490
HJ
126}
127
822f18d4 128/* Get the size of the ring buffer. */
4d643114 129static inline u32
e4165a0f 130hv_get_ring_buffersize(const struct hv_ring_buffer_info *ring_info)
3e7ee490 131{
fc8c72eb 132 return ring_info->ring_datasize;
3e7ee490
HJ
133}
134
822f18d4 135/* Get the read and write indices as u64 of the specified ring buffer. */
59471438 136static inline u64
2b8a912e 137hv_get_ring_bufferindices(struct hv_ring_buffer_info *ring_info)
3e7ee490 138{
fc8c72eb 139 return (u64)ring_info->ring_buffer->write_index << 32;
3e7ee490
HJ
140}
141
8f1136ae 142/*
8f1136ae
S
143 * Helper routine to copy to source from ring buffer.
144 * Assume there is enough room. Handles wrap-around in src case only!!
8f1136ae
S
145 */
146static u32 hv_copyfrom_ringbuffer(
e4165a0f 147 const struct hv_ring_buffer_info *ring_info,
8f1136ae
S
148 void *dest,
149 u32 destlen,
150 u32 start_read_offset)
151{
152 void *ring_buffer = hv_get_ring_buffer(ring_info);
153 u32 ring_buffer_size = hv_get_ring_buffersize(ring_info);
154
f24f0b49 155 memcpy(dest, ring_buffer + start_read_offset, destlen);
8f1136ae
S
156
157 start_read_offset += destlen;
8d12f882
SH
158 if (start_read_offset >= ring_buffer_size)
159 start_read_offset -= ring_buffer_size;
8f1136ae
S
160
161 return start_read_offset;
162}
163
164
7581578d 165/*
7581578d
S
166 * Helper routine to copy from source to ring buffer.
167 * Assume there is enough room. Handles wrap-around in dest case only!!
7581578d
S
168 */
169static u32 hv_copyto_ringbuffer(
fc8c72eb
HZ
170 struct hv_ring_buffer_info *ring_info,
171 u32 start_write_offset,
e4165a0f 172 const void *src,
7581578d
S
173 u32 srclen)
174{
175 void *ring_buffer = hv_get_ring_buffer(ring_info);
176 u32 ring_buffer_size = hv_get_ring_buffersize(ring_info);
f24f0b49
VK
177
178 memcpy(ring_buffer + start_write_offset, src, srclen);
3e7ee490 179
7581578d 180 start_write_offset += srclen;
8d12f882
SH
181 if (start_write_offset >= ring_buffer_size)
182 start_write_offset -= ring_buffer_size;
7581578d
S
183
184 return start_write_offset;
185}
3e7ee490 186
822f18d4 187/* Get various debug metrics for the specified ring buffer. */
e4165a0f
SH
188void hv_ringbuffer_get_debuginfo(const struct hv_ring_buffer_info *ring_info,
189 struct hv_ring_buffer_debug_info *debug_info)
3e7ee490 190{
fc8c72eb
HZ
191 u32 bytes_avail_towrite;
192 u32 bytes_avail_toread;
3e7ee490 193
fc8c72eb 194 if (ring_info->ring_buffer) {
2b8a912e 195 hv_get_ringbuffer_availbytes(ring_info,
fc8c72eb
HZ
196 &bytes_avail_toread,
197 &bytes_avail_towrite);
3e7ee490 198
fc8c72eb
HZ
199 debug_info->bytes_avail_toread = bytes_avail_toread;
200 debug_info->bytes_avail_towrite = bytes_avail_towrite;
82f8bd40 201 debug_info->current_read_index =
fc8c72eb 202 ring_info->ring_buffer->read_index;
82f8bd40 203 debug_info->current_write_index =
fc8c72eb 204 ring_info->ring_buffer->write_index;
82f8bd40 205 debug_info->current_interrupt_mask =
fc8c72eb 206 ring_info->ring_buffer->interrupt_mask;
3e7ee490
HJ
207 }
208}
209
822f18d4 210/* Initialize the ring buffer. */
72a95cbc 211int hv_ringbuffer_init(struct hv_ring_buffer_info *ring_info,
9988ce68 212 struct page *pages, u32 page_cnt)
3e7ee490 213{
9988ce68
VK
214 int i;
215 struct page **pages_wraparound;
216
217 BUILD_BUG_ON((sizeof(struct hv_ring_buffer) != PAGE_SIZE));
3e7ee490 218
fc8c72eb 219 memset(ring_info, 0, sizeof(struct hv_ring_buffer_info));
3e7ee490 220
9988ce68
VK
221 /*
222 * First page holds struct hv_ring_buffer, do wraparound mapping for
223 * the rest.
224 */
225 pages_wraparound = kzalloc(sizeof(struct page *) * (page_cnt * 2 - 1),
226 GFP_KERNEL);
227 if (!pages_wraparound)
228 return -ENOMEM;
229
230 pages_wraparound[0] = pages;
231 for (i = 0; i < 2 * (page_cnt - 1); i++)
232 pages_wraparound[i + 1] = &pages[i % (page_cnt - 1) + 1];
233
234 ring_info->ring_buffer = (struct hv_ring_buffer *)
235 vmap(pages_wraparound, page_cnt * 2 - 1, VM_MAP, PAGE_KERNEL);
236
237 kfree(pages_wraparound);
238
239
240 if (!ring_info->ring_buffer)
241 return -ENOMEM;
242
fc8c72eb
HZ
243 ring_info->ring_buffer->read_index =
244 ring_info->ring_buffer->write_index = 0;
3e7ee490 245
822f18d4 246 /* Set the feature bit for enabling flow control. */
046c7911
S
247 ring_info->ring_buffer->feature_bits.value = 1;
248
9988ce68
VK
249 ring_info->ring_size = page_cnt << PAGE_SHIFT;
250 ring_info->ring_datasize = ring_info->ring_size -
251 sizeof(struct hv_ring_buffer);
3e7ee490 252
fc8c72eb 253 spin_lock_init(&ring_info->ring_lock);
3e7ee490
HJ
254
255 return 0;
256}
257
822f18d4 258/* Cleanup the ring buffer. */
2dba688b 259void hv_ringbuffer_cleanup(struct hv_ring_buffer_info *ring_info)
3e7ee490 260{
9988ce68 261 vunmap(ring_info->ring_buffer);
3e7ee490
HJ
262}
263
822f18d4 264/* Write to the ring buffer. */
1f6ee4e7 265int hv_ringbuffer_write(struct vmbus_channel *channel,
e4165a0f 266 const struct kvec *kv_list, u32 kv_count)
3e7ee490 267{
2c616a8b 268 int i;
fc8c72eb 269 u32 bytes_avail_towrite;
2c616a8b 270 u32 totalbytes_towrite = sizeof(u64);
66a60543 271 u32 next_write_location;
98fa8cf4 272 u32 old_write;
2c616a8b
SH
273 u64 prev_indices;
274 unsigned long flags;
1f6ee4e7 275 struct hv_ring_buffer_info *outring_info = &channel->outbound;
3e7ee490 276
e7e97dd8
S
277 if (channel->rescind)
278 return -ENODEV;
279
011a7c3c
S
280 for (i = 0; i < kv_count; i++)
281 totalbytes_towrite += kv_list[i].iov_len;
3e7ee490 282
5529eaf6 283 spin_lock_irqsave(&outring_info->ring_lock, flags);
3e7ee490 284
a6341f00 285 bytes_avail_towrite = hv_get_bytes_to_write(outring_info);
3e7ee490 286
822f18d4
VK
287 /*
288 * If there is only room for the packet, assume it is full.
289 * Otherwise, the next time around, we think the ring buffer
290 * is empty since the read index == write index.
291 */
fc8c72eb 292 if (bytes_avail_towrite <= totalbytes_towrite) {
5529eaf6 293 spin_unlock_irqrestore(&outring_info->ring_lock, flags);
d2598f01 294 return -EAGAIN;
3e7ee490
HJ
295 }
296
454f18a9 297 /* Write to the ring buffer */
2b8a912e 298 next_write_location = hv_get_next_write_location(outring_info);
3e7ee490 299
98fa8cf4
S
300 old_write = next_write_location;
301
011a7c3c 302 for (i = 0; i < kv_count; i++) {
2b8a912e 303 next_write_location = hv_copyto_ringbuffer(outring_info,
fc8c72eb 304 next_write_location,
011a7c3c
S
305 kv_list[i].iov_base,
306 kv_list[i].iov_len);
3e7ee490
HJ
307 }
308
454f18a9 309 /* Set previous packet start */
2b8a912e 310 prev_indices = hv_get_ring_bufferindices(outring_info);
3e7ee490 311
2b8a912e 312 next_write_location = hv_copyto_ringbuffer(outring_info,
fc8c72eb
HZ
313 next_write_location,
314 &prev_indices,
b219b3f7 315 sizeof(u64));
3e7ee490 316
98fa8cf4 317 /* Issue a full memory barrier before updating the write index */
dcd0eeca 318 virt_mb();
3e7ee490 319
454f18a9 320 /* Now, update the write location */
2b8a912e 321 hv_set_next_write_location(outring_info, next_write_location);
3e7ee490 322
3e7ee490 323
5529eaf6 324 spin_unlock_irqrestore(&outring_info->ring_lock, flags);
98fa8cf4 325
b103a56f 326 hv_signal_on_write(old_write, channel);
e7e97dd8
S
327
328 if (channel->rescind)
329 return -ENODEV;
330
3e7ee490
HJ
331 return 0;
332}
333
3372592a 334int hv_ringbuffer_read(struct vmbus_channel *channel,
940b68e2 335 void *buffer, u32 buflen, u32 *buffer_actual_len,
3372592a 336 u64 *requestid, bool raw)
3e7ee490 337{
fc8c72eb 338 u32 bytes_avail_toread;
2c616a8b 339 u32 next_read_location;
fc8c72eb 340 u64 prev_indices = 0;
940b68e2
VK
341 struct vmpacket_descriptor desc;
342 u32 offset;
343 u32 packetlen;
3372592a 344 struct hv_ring_buffer_info *inring_info = &channel->inbound;
3e7ee490 345
fc8c72eb 346 if (buflen <= 0)
a16e1485 347 return -EINVAL;
3e7ee490 348
940b68e2
VK
349 *buffer_actual_len = 0;
350 *requestid = 0;
351
a6341f00 352 bytes_avail_toread = hv_get_bytes_to_read(inring_info);
454f18a9 353 /* Make sure there is something to read */
940b68e2
VK
354 if (bytes_avail_toread < sizeof(desc)) {
355 /*
356 * No error is set when there is even no header, drivers are
357 * supposed to analyze buffer_actual_len.
358 */
42dd2715 359 return 0;
940b68e2 360 }
3e7ee490 361
433e19cf 362 init_cached_read_index(channel);
940b68e2
VK
363 next_read_location = hv_get_next_read_location(inring_info);
364 next_read_location = hv_copyfrom_ringbuffer(inring_info, &desc,
365 sizeof(desc),
366 next_read_location);
367
368 offset = raw ? 0 : (desc.offset8 << 3);
369 packetlen = (desc.len8 << 3) - offset;
370 *buffer_actual_len = packetlen;
371 *requestid = desc.trans_id;
372
3eba9a77
S
373 if (bytes_avail_toread < packetlen + offset)
374 return -EAGAIN;
940b68e2 375
3eba9a77
S
376 if (packetlen > buflen)
377 return -ENOBUFS;
3e7ee490 378
1ac58644 379 next_read_location =
2b8a912e 380 hv_get_next_readlocation_withoffset(inring_info, offset);
3e7ee490 381
2b8a912e 382 next_read_location = hv_copyfrom_ringbuffer(inring_info,
fc8c72eb 383 buffer,
940b68e2 384 packetlen,
fc8c72eb 385 next_read_location);
3e7ee490 386
2b8a912e 387 next_read_location = hv_copyfrom_ringbuffer(inring_info,
fc8c72eb 388 &prev_indices,
4408f531 389 sizeof(u64),
fc8c72eb 390 next_read_location);
3e7ee490 391
822f18d4
VK
392 /*
393 * Make sure all reads are done before we update the read index since
394 * the writer may start writing to the read area once the read index
395 * is updated.
396 */
dcd0eeca 397 virt_mb();
3e7ee490 398
454f18a9 399 /* Update the read index */
2b8a912e 400 hv_set_next_read_location(inring_info, next_read_location);
3e7ee490 401
3372592a 402 hv_signal_on_read(channel);
c2b8e520 403
42dd2715 404 return 0;
b5f53dde 405}