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