blob: c3f1a9e33cef0852a947295fefcb765ffed80a88 [file] [log] [blame]
Hank Janssen3e7ee492009-07-13 16:02:34 -07001/*
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>
K. Y. Srinivasanb2a5a582011-05-10 07:55:30 -070021 * K. Y. Srinivasan <kys@microsoft.com>
Hank Janssen3e7ee492009-07-13 16:02:34 -070022 *
23 */
Hank Janssen0a466182011-03-29 13:58:47 -070024#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Hank Janssen3e7ee492009-07-13 16:02:34 -070025
Greg Kroah-Hartmana0086dc2009-08-17 17:22:08 -070026#include <linux/kernel.h>
27#include <linux/mm.h>
Greg Kroah-Hartman46a97192011-10-04 12:29:52 -070028#include <linux/hyperv.h>
K. Y. Srinivasan011a7c32014-02-01 19:02:20 -080029#include <linux/uio.h>
Vitaly Kuznetsov9988ce62016-09-02 05:58:20 -070030#include <linux/vmalloc.h>
31#include <linux/slab.h>
K. Y. Srinivasan3f335ea2011-05-12 19:34:15 -070032
K. Y. Srinivasan0f2a6612011-05-12 19:34:28 -070033#include "hyperv_vmbus.h"
Hank Janssen3e7ee492009-07-13 16:02:34 -070034
stephen hemmingerf3dd3f42017-02-27 10:26:48 -080035#define VMBUS_PKT_TRAILER 8
36
K. Y. Srinivasan98fa8cf2012-12-01 06:46:36 -080037/*
38 * When we write to the ring buffer, check if the host needs to
39 * be signaled. Here is the details of this protocol:
40 *
41 * 1. The host guarantees that while it is draining the
42 * ring buffer, it will set the interrupt_mask to
43 * indicate it does not need to be interrupted when
44 * new data is placed.
45 *
46 * 2. The host guarantees that it will completely drain
47 * the ring buffer before exiting the read loop. Further,
48 * once the ring buffer is empty, it will clear the
49 * interrupt_mask and re-check to see if new data has
50 * arrived.
K. Y. Srinivasan1f6ee4e2016-11-06 13:14:17 -080051 *
52 * KYS: Oct. 30, 2016:
53 * It looks like Windows hosts have logic to deal with DOS attacks that
54 * can be triggered if it receives interrupts when it is not expecting
55 * the interrupt. The host expects interrupts only when the ring
56 * transitions from empty to non-empty (or full to non full on the guest
57 * to host ring).
58 * So, base the signaling decision solely on the ring state until the
59 * host logic is fixed.
K. Y. Srinivasan98fa8cf2012-12-01 06:46:36 -080060 */
61
Stephen Hemmingerb103a562017-02-05 17:20:32 -070062static void hv_signal_on_write(u32 old_write, struct vmbus_channel *channel)
K. Y. Srinivasan98fa8cf2012-12-01 06:46:36 -080063{
K. Y. Srinivasan1f6ee4e2016-11-06 13:14:17 -080064 struct hv_ring_buffer_info *rbi = &channel->outbound;
65
K. Y. Srinivasandcd0eec2016-04-02 17:59:48 -070066 virt_mb();
K. Y. Srinivasand45faae2016-04-02 17:59:47 -070067 if (READ_ONCE(rbi->ring_buffer->interrupt_mask))
K. Y. Srinivasan1f6ee4e2016-11-06 13:14:17 -080068 return;
K. Y. Srinivasan98fa8cf2012-12-01 06:46:36 -080069
Jason Wange91e84f2013-06-20 12:58:57 +080070 /* check interrupt_mask before read_index */
K. Y. Srinivasandcd0eec2016-04-02 17:59:48 -070071 virt_rmb();
K. Y. Srinivasan98fa8cf2012-12-01 06:46:36 -080072 /*
73 * This is the only case we need to signal when the
74 * ring transitions from being empty to non-empty.
75 */
K. Y. Srinivasand45faae2016-04-02 17:59:47 -070076 if (old_write == READ_ONCE(rbi->ring_buffer->read_index))
K. Y. Srinivasan1f6ee4e2016-11-06 13:14:17 -080077 vmbus_setevent(channel);
K. Y. Srinivasan98fa8cf2012-12-01 06:46:36 -080078
K. Y. Srinivasan1f6ee4e2016-11-06 13:14:17 -080079 return;
K. Y. Srinivasan98fa8cf2012-12-01 06:46:36 -080080}
81
Vitaly Kuznetsov822f18d2015-12-14 19:01:57 -080082/* Get the next write location for the specified ring buffer. */
Greg Kroah-Hartman4d643112009-07-14 15:09:36 -070083static inline u32
K. Y. Srinivasan2b8a9122011-05-10 07:55:29 -070084hv_get_next_write_location(struct hv_ring_buffer_info *ring_info)
Hank Janssen3e7ee492009-07-13 16:02:34 -070085{
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -080086 u32 next = ring_info->ring_buffer->write_index;
Hank Janssen3e7ee492009-07-13 16:02:34 -070087
Hank Janssen3e7ee492009-07-13 16:02:34 -070088 return next;
89}
90
Vitaly Kuznetsov822f18d2015-12-14 19:01:57 -080091/* Set the next write location for the specified ring buffer. */
Hank Janssen3e7ee492009-07-13 16:02:34 -070092static inline void
K. Y. Srinivasan2b8a9122011-05-10 07:55:29 -070093hv_set_next_write_location(struct hv_ring_buffer_info *ring_info,
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -080094 u32 next_write_location)
Hank Janssen3e7ee492009-07-13 16:02:34 -070095{
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -080096 ring_info->ring_buffer->write_index = next_write_location;
Hank Janssen3e7ee492009-07-13 16:02:34 -070097}
98
Vitaly Kuznetsov822f18d2015-12-14 19:01:57 -080099/* Get the next read location for the specified ring buffer. */
Greg Kroah-Hartman4d643112009-07-14 15:09:36 -0700100static inline u32
Stephen Hemmingere4165a02017-02-11 23:02:24 -0700101hv_get_next_read_location(const struct hv_ring_buffer_info *ring_info)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700102{
Stephen Hemmingere4165a02017-02-11 23:02:24 -0700103 return ring_info->ring_buffer->read_index;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700104}
105
K. Y. Srinivasanb2a5a582011-05-10 07:55:30 -0700106/*
K. Y. Srinivasanb2a5a582011-05-10 07:55:30 -0700107 * Get the next read location + offset for the specified ring buffer.
Vitaly Kuznetsov822f18d2015-12-14 19:01:57 -0800108 * This allows the caller to skip.
K. Y. Srinivasanb2a5a582011-05-10 07:55:30 -0700109 */
Greg Kroah-Hartman4d643112009-07-14 15:09:36 -0700110static inline u32
Stephen Hemmingere4165a02017-02-11 23:02:24 -0700111hv_get_next_readlocation_withoffset(const struct hv_ring_buffer_info *ring_info,
112 u32 offset)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700113{
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800114 u32 next = ring_info->ring_buffer->read_index;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700115
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800116 next += offset;
Stephen Hemminger8d12f882017-02-11 23:02:25 -0700117 if (next >= ring_info->ring_datasize)
118 next -= ring_info->ring_datasize;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700119
120 return next;
121}
122
Vitaly Kuznetsov822f18d2015-12-14 19:01:57 -0800123/* Set the next read location for the specified ring buffer. */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700124static inline void
K. Y. Srinivasan2b8a9122011-05-10 07:55:29 -0700125hv_set_next_read_location(struct hv_ring_buffer_info *ring_info,
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800126 u32 next_read_location)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700127{
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800128 ring_info->ring_buffer->read_index = next_read_location;
K. Y. Srinivasanab028db2016-04-02 17:59:51 -0700129 ring_info->priv_read_index = next_read_location;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700130}
131
Vitaly Kuznetsov822f18d2015-12-14 19:01:57 -0800132/* Get the size of the ring buffer. */
Greg Kroah-Hartman4d643112009-07-14 15:09:36 -0700133static inline u32
Stephen Hemmingere4165a02017-02-11 23:02:24 -0700134hv_get_ring_buffersize(const struct hv_ring_buffer_info *ring_info)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700135{
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800136 return ring_info->ring_datasize;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700137}
138
Vitaly Kuznetsov822f18d2015-12-14 19:01:57 -0800139/* Get the read and write indices as u64 of the specified ring buffer. */
Greg Kroah-Hartman59471432009-07-14 15:10:26 -0700140static inline u64
K. Y. Srinivasan2b8a9122011-05-10 07:55:29 -0700141hv_get_ring_bufferindices(struct hv_ring_buffer_info *ring_info)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700142{
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800143 return (u64)ring_info->ring_buffer->write_index << 32;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700144}
145
K. Y. Srinivasan8f1136a2011-05-10 07:55:31 -0700146/*
K. Y. Srinivasan8f1136a2011-05-10 07:55:31 -0700147 * Helper routine to copy to source from ring buffer.
148 * Assume there is enough room. Handles wrap-around in src case only!!
K. Y. Srinivasan8f1136a2011-05-10 07:55:31 -0700149 */
150static u32 hv_copyfrom_ringbuffer(
Stephen Hemmingere4165a02017-02-11 23:02:24 -0700151 const struct hv_ring_buffer_info *ring_info,
K. Y. Srinivasan8f1136a2011-05-10 07:55:31 -0700152 void *dest,
153 u32 destlen,
154 u32 start_read_offset)
155{
156 void *ring_buffer = hv_get_ring_buffer(ring_info);
157 u32 ring_buffer_size = hv_get_ring_buffersize(ring_info);
158
Vitaly Kuznetsovf24f0b42016-09-02 05:58:21 -0700159 memcpy(dest, ring_buffer + start_read_offset, destlen);
K. Y. Srinivasan8f1136a2011-05-10 07:55:31 -0700160
161 start_read_offset += destlen;
Stephen Hemminger8d12f882017-02-11 23:02:25 -0700162 if (start_read_offset >= ring_buffer_size)
163 start_read_offset -= ring_buffer_size;
K. Y. Srinivasan8f1136a2011-05-10 07:55:31 -0700164
165 return start_read_offset;
166}
167
168
K. Y. Srinivasan75815782011-05-10 07:55:32 -0700169/*
K. Y. Srinivasan75815782011-05-10 07:55:32 -0700170 * Helper routine to copy from source to ring buffer.
171 * Assume there is enough room. Handles wrap-around in dest case only!!
K. Y. Srinivasan75815782011-05-10 07:55:32 -0700172 */
173static u32 hv_copyto_ringbuffer(
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800174 struct hv_ring_buffer_info *ring_info,
175 u32 start_write_offset,
Stephen Hemmingere4165a02017-02-11 23:02:24 -0700176 const void *src,
K. Y. Srinivasan75815782011-05-10 07:55:32 -0700177 u32 srclen)
178{
179 void *ring_buffer = hv_get_ring_buffer(ring_info);
180 u32 ring_buffer_size = hv_get_ring_buffersize(ring_info);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700181
Vitaly Kuznetsovf24f0b42016-09-02 05:58:21 -0700182 memcpy(ring_buffer + start_write_offset, src, srclen);
K. Y. Srinivasan75815782011-05-10 07:55:32 -0700183
184 start_write_offset += srclen;
Stephen Hemminger8d12f882017-02-11 23:02:25 -0700185 if (start_write_offset >= ring_buffer_size)
186 start_write_offset -= ring_buffer_size;
K. Y. Srinivasan75815782011-05-10 07:55:32 -0700187
188 return start_write_offset;
189}
Hank Janssen3e7ee492009-07-13 16:02:34 -0700190
Vitaly Kuznetsov822f18d2015-12-14 19:01:57 -0800191/* Get various debug metrics for the specified ring buffer. */
Stephen Hemmingere4165a02017-02-11 23:02:24 -0700192void hv_ringbuffer_get_debuginfo(const struct hv_ring_buffer_info *ring_info,
193 struct hv_ring_buffer_debug_info *debug_info)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700194{
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800195 u32 bytes_avail_towrite;
196 u32 bytes_avail_toread;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700197
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800198 if (ring_info->ring_buffer) {
K. Y. Srinivasan2b8a9122011-05-10 07:55:29 -0700199 hv_get_ringbuffer_availbytes(ring_info,
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800200 &bytes_avail_toread,
201 &bytes_avail_towrite);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700202
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800203 debug_info->bytes_avail_toread = bytes_avail_toread;
204 debug_info->bytes_avail_towrite = bytes_avail_towrite;
Haiyang Zhang82f8bd42010-11-08 14:04:45 -0800205 debug_info->current_read_index =
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800206 ring_info->ring_buffer->read_index;
Haiyang Zhang82f8bd42010-11-08 14:04:45 -0800207 debug_info->current_write_index =
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800208 ring_info->ring_buffer->write_index;
Haiyang Zhang82f8bd42010-11-08 14:04:45 -0800209 debug_info->current_interrupt_mask =
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800210 ring_info->ring_buffer->interrupt_mask;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700211 }
212}
213
Vitaly Kuznetsov822f18d2015-12-14 19:01:57 -0800214/* Initialize the ring buffer. */
K. Y. Srinivasan72a95cb2011-05-10 07:55:21 -0700215int hv_ringbuffer_init(struct hv_ring_buffer_info *ring_info,
Vitaly Kuznetsov9988ce62016-09-02 05:58:20 -0700216 struct page *pages, u32 page_cnt)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700217{
Vitaly Kuznetsov9988ce62016-09-02 05:58:20 -0700218 int i;
219 struct page **pages_wraparound;
220
221 BUILD_BUG_ON((sizeof(struct hv_ring_buffer) != PAGE_SIZE));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700222
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800223 memset(ring_info, 0, sizeof(struct hv_ring_buffer_info));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700224
Vitaly Kuznetsov9988ce62016-09-02 05:58:20 -0700225 /*
226 * First page holds struct hv_ring_buffer, do wraparound mapping for
227 * the rest.
228 */
229 pages_wraparound = kzalloc(sizeof(struct page *) * (page_cnt * 2 - 1),
230 GFP_KERNEL);
231 if (!pages_wraparound)
232 return -ENOMEM;
233
234 pages_wraparound[0] = pages;
235 for (i = 0; i < 2 * (page_cnt - 1); i++)
236 pages_wraparound[i + 1] = &pages[i % (page_cnt - 1) + 1];
237
238 ring_info->ring_buffer = (struct hv_ring_buffer *)
239 vmap(pages_wraparound, page_cnt * 2 - 1, VM_MAP, PAGE_KERNEL);
240
241 kfree(pages_wraparound);
242
243
244 if (!ring_info->ring_buffer)
245 return -ENOMEM;
246
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800247 ring_info->ring_buffer->read_index =
248 ring_info->ring_buffer->write_index = 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700249
Vitaly Kuznetsov822f18d2015-12-14 19:01:57 -0800250 /* Set the feature bit for enabling flow control. */
K. Y. Srinivasan046c7912014-09-05 17:29:12 -0700251 ring_info->ring_buffer->feature_bits.value = 1;
252
Vitaly Kuznetsov9988ce62016-09-02 05:58:20 -0700253 ring_info->ring_size = page_cnt << PAGE_SHIFT;
254 ring_info->ring_datasize = ring_info->ring_size -
255 sizeof(struct hv_ring_buffer);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700256
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800257 spin_lock_init(&ring_info->ring_lock);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700258
259 return 0;
260}
261
Vitaly Kuznetsov822f18d2015-12-14 19:01:57 -0800262/* Cleanup the ring buffer. */
K. Y. Srinivasan2dba6882011-05-10 07:55:22 -0700263void hv_ringbuffer_cleanup(struct hv_ring_buffer_info *ring_info)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700264{
Vitaly Kuznetsov9988ce62016-09-02 05:58:20 -0700265 vunmap(ring_info->ring_buffer);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700266}
267
Vitaly Kuznetsov822f18d2015-12-14 19:01:57 -0800268/* Write to the ring buffer. */
K. Y. Srinivasan1f6ee4e2016-11-06 13:14:17 -0800269int hv_ringbuffer_write(struct vmbus_channel *channel,
Stephen Hemmingere4165a02017-02-11 23:02:24 -0700270 const struct kvec *kv_list, u32 kv_count)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700271{
C. Bartlett4408f532010-02-03 15:34:27 +0000272 int i = 0;
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800273 u32 bytes_avail_towrite;
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800274 u32 totalbytes_towrite = 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700275
K. Y. Srinivasan66a60542011-05-10 07:55:33 -0700276 u32 next_write_location;
K. Y. Srinivasan98fa8cf2012-12-01 06:46:36 -0800277 u32 old_write;
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800278 u64 prev_indices = 0;
K. Y. Srinivasanfe760e42016-01-27 22:29:45 -0800279 unsigned long flags = 0;
K. Y. Srinivasan1f6ee4e2016-11-06 13:14:17 -0800280 struct hv_ring_buffer_info *outring_info = &channel->outbound;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700281
K. Y. Srinivasane7e97dd2016-12-07 01:16:28 -0800282 if (channel->rescind)
283 return -ENODEV;
284
K. Y. Srinivasan011a7c32014-02-01 19:02:20 -0800285 for (i = 0; i < kv_count; i++)
286 totalbytes_towrite += kv_list[i].iov_len;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700287
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800288 totalbytes_towrite += sizeof(u64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700289
Stephen Hemminger5529eaf2017-02-11 23:02:22 -0700290 spin_lock_irqsave(&outring_info->ring_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700291
K. Y. Srinivasana6341f02016-04-02 17:59:46 -0700292 bytes_avail_towrite = hv_get_bytes_to_write(outring_info);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700293
Vitaly Kuznetsov822f18d2015-12-14 19:01:57 -0800294 /*
295 * If there is only room for the packet, assume it is full.
296 * Otherwise, the next time around, we think the ring buffer
297 * is empty since the read index == write index.
298 */
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800299 if (bytes_avail_towrite <= totalbytes_towrite) {
Stephen Hemminger5529eaf2017-02-11 23:02:22 -0700300 spin_unlock_irqrestore(&outring_info->ring_lock, flags);
K. Y. Srinivasand2598f02011-08-25 09:48:58 -0700301 return -EAGAIN;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700302 }
303
Bill Pemberton454f18a2009-07-27 16:47:24 -0400304 /* Write to the ring buffer */
K. Y. Srinivasan2b8a9122011-05-10 07:55:29 -0700305 next_write_location = hv_get_next_write_location(outring_info);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700306
K. Y. Srinivasan98fa8cf2012-12-01 06:46:36 -0800307 old_write = next_write_location;
308
K. Y. Srinivasan011a7c32014-02-01 19:02:20 -0800309 for (i = 0; i < kv_count; i++) {
K. Y. Srinivasan2b8a9122011-05-10 07:55:29 -0700310 next_write_location = hv_copyto_ringbuffer(outring_info,
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800311 next_write_location,
K. Y. Srinivasan011a7c32014-02-01 19:02:20 -0800312 kv_list[i].iov_base,
313 kv_list[i].iov_len);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700314 }
315
Bill Pemberton454f18a2009-07-27 16:47:24 -0400316 /* Set previous packet start */
K. Y. Srinivasan2b8a9122011-05-10 07:55:29 -0700317 prev_indices = hv_get_ring_bufferindices(outring_info);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700318
K. Y. Srinivasan2b8a9122011-05-10 07:55:29 -0700319 next_write_location = hv_copyto_ringbuffer(outring_info,
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800320 next_write_location,
321 &prev_indices,
Nicolas Palixb219b3f2009-07-30 17:37:23 +0200322 sizeof(u64));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700323
K. Y. Srinivasan98fa8cf2012-12-01 06:46:36 -0800324 /* Issue a full memory barrier before updating the write index */
K. Y. Srinivasandcd0eec2016-04-02 17:59:48 -0700325 virt_mb();
Hank Janssen3e7ee492009-07-13 16:02:34 -0700326
Bill Pemberton454f18a2009-07-27 16:47:24 -0400327 /* Now, update the write location */
K. Y. Srinivasan2b8a9122011-05-10 07:55:29 -0700328 hv_set_next_write_location(outring_info, next_write_location);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700329
Hank Janssen3e7ee492009-07-13 16:02:34 -0700330
Stephen Hemminger5529eaf2017-02-11 23:02:22 -0700331 spin_unlock_irqrestore(&outring_info->ring_lock, flags);
K. Y. Srinivasan98fa8cf2012-12-01 06:46:36 -0800332
Stephen Hemmingerb103a562017-02-05 17:20:32 -0700333 hv_signal_on_write(old_write, channel);
K. Y. Srinivasane7e97dd2016-12-07 01:16:28 -0800334
335 if (channel->rescind)
336 return -ENODEV;
337
Hank Janssen3e7ee492009-07-13 16:02:34 -0700338 return 0;
339}
340
stephen hemmingerf3dd3f42017-02-27 10:26:48 -0800341static inline void
342init_cached_read_index(struct hv_ring_buffer_info *rbi)
343{
344 rbi->cached_read_index = rbi->ring_buffer->read_index;
345}
346
K. Y. Srinivasan33725922016-11-06 13:14:18 -0800347int hv_ringbuffer_read(struct vmbus_channel *channel,
Vitaly Kuznetsov940b68e2015-12-14 19:02:01 -0800348 void *buffer, u32 buflen, u32 *buffer_actual_len,
K. Y. Srinivasan33725922016-11-06 13:14:18 -0800349 u64 *requestid, bool raw)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700350{
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800351 u32 bytes_avail_toread;
352 u32 next_read_location = 0;
353 u64 prev_indices = 0;
Vitaly Kuznetsov940b68e2015-12-14 19:02:01 -0800354 struct vmpacket_descriptor desc;
355 u32 offset;
356 u32 packetlen;
357 int ret = 0;
K. Y. Srinivasan33725922016-11-06 13:14:18 -0800358 struct hv_ring_buffer_info *inring_info = &channel->inbound;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700359
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800360 if (buflen <= 0)
Bill Pembertona16e1482010-05-05 15:27:50 -0400361 return -EINVAL;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700362
Hank Janssen3e7ee492009-07-13 16:02:34 -0700363
Vitaly Kuznetsov940b68e2015-12-14 19:02:01 -0800364 *buffer_actual_len = 0;
365 *requestid = 0;
366
K. Y. Srinivasana6341f02016-04-02 17:59:46 -0700367 bytes_avail_toread = hv_get_bytes_to_read(inring_info);
Bill Pemberton454f18a2009-07-27 16:47:24 -0400368 /* Make sure there is something to read */
Vitaly Kuznetsov940b68e2015-12-14 19:02:01 -0800369 if (bytes_avail_toread < sizeof(desc)) {
370 /*
371 * No error is set when there is even no header, drivers are
372 * supposed to analyze buffer_actual_len.
373 */
K. Y. Srinivasan3eba9a72016-01-27 22:29:44 -0800374 return ret;
Vitaly Kuznetsov940b68e2015-12-14 19:02:01 -0800375 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700376
stephen hemmingerf3dd3f42017-02-27 10:26:48 -0800377 init_cached_read_index(inring_info);
378
Vitaly Kuznetsov940b68e2015-12-14 19:02:01 -0800379 next_read_location = hv_get_next_read_location(inring_info);
380 next_read_location = hv_copyfrom_ringbuffer(inring_info, &desc,
381 sizeof(desc),
382 next_read_location);
383
384 offset = raw ? 0 : (desc.offset8 << 3);
385 packetlen = (desc.len8 << 3) - offset;
386 *buffer_actual_len = packetlen;
387 *requestid = desc.trans_id;
388
K. Y. Srinivasan3eba9a72016-01-27 22:29:44 -0800389 if (bytes_avail_toread < packetlen + offset)
390 return -EAGAIN;
Vitaly Kuznetsov940b68e2015-12-14 19:02:01 -0800391
K. Y. Srinivasan3eba9a72016-01-27 22:29:44 -0800392 if (packetlen > buflen)
393 return -ENOBUFS;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700394
Haiyang Zhang1ac58642010-11-08 14:04:47 -0800395 next_read_location =
K. Y. Srinivasan2b8a9122011-05-10 07:55:29 -0700396 hv_get_next_readlocation_withoffset(inring_info, offset);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700397
K. Y. Srinivasan2b8a9122011-05-10 07:55:29 -0700398 next_read_location = hv_copyfrom_ringbuffer(inring_info,
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800399 buffer,
Vitaly Kuznetsov940b68e2015-12-14 19:02:01 -0800400 packetlen,
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800401 next_read_location);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700402
K. Y. Srinivasan2b8a9122011-05-10 07:55:29 -0700403 next_read_location = hv_copyfrom_ringbuffer(inring_info,
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800404 &prev_indices,
C. Bartlett4408f532010-02-03 15:34:27 +0000405 sizeof(u64),
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800406 next_read_location);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700407
Vitaly Kuznetsov822f18d2015-12-14 19:01:57 -0800408 /*
409 * Make sure all reads are done before we update the read index since
410 * the writer may start writing to the read area once the read index
411 * is updated.
412 */
K. Y. Srinivasandcd0eec2016-04-02 17:59:48 -0700413 virt_mb();
Hank Janssen3e7ee492009-07-13 16:02:34 -0700414
Bill Pemberton454f18a2009-07-27 16:47:24 -0400415 /* Update the read index */
K. Y. Srinivasan2b8a9122011-05-10 07:55:29 -0700416 hv_set_next_read_location(inring_info, next_read_location);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700417
K. Y. Srinivasan33725922016-11-06 13:14:18 -0800418 hv_signal_on_read(channel);
K. Y. Srinivasanc2b8e522012-12-01 06:46:57 -0800419
Vitaly Kuznetsov940b68e2015-12-14 19:02:01 -0800420 return ret;
Vitaly Kuznetsovb5f53dd2015-12-14 19:01:59 -0800421}
stephen hemmingerf3dd3f42017-02-27 10:26:48 -0800422
423/*
424 * Determine number of bytes available in ring buffer after
425 * the current iterator (priv_read_index) location.
426 *
427 * This is similar to hv_get_bytes_to_read but with private
428 * read index instead.
429 */
430static u32 hv_pkt_iter_avail(const struct hv_ring_buffer_info *rbi)
431{
432 u32 priv_read_loc = rbi->priv_read_index;
433 u32 write_loc = READ_ONCE(rbi->ring_buffer->write_index);
434
435 if (write_loc >= priv_read_loc)
436 return write_loc - priv_read_loc;
437 else
438 return (rbi->ring_datasize - priv_read_loc) + write_loc;
439}
440
441/*
442 * Get first vmbus packet from ring buffer after read_index
443 *
444 * If ring buffer is empty, returns NULL and no other action needed.
445 */
446struct vmpacket_descriptor *hv_pkt_iter_first(struct vmbus_channel *channel)
447{
448 struct hv_ring_buffer_info *rbi = &channel->inbound;
449
450 /* set state for later hv_signal_on_read() */
451 init_cached_read_index(rbi);
452
453 if (hv_pkt_iter_avail(rbi) < sizeof(struct vmpacket_descriptor))
454 return NULL;
455
456 return hv_get_ring_buffer(rbi) + rbi->priv_read_index;
457}
458EXPORT_SYMBOL_GPL(hv_pkt_iter_first);
459
460/*
461 * Get next vmbus packet from ring buffer.
462 *
463 * Advances the current location (priv_read_index) and checks for more
464 * data. If the end of the ring buffer is reached, then return NULL.
465 */
466struct vmpacket_descriptor *
467__hv_pkt_iter_next(struct vmbus_channel *channel,
468 const struct vmpacket_descriptor *desc)
469{
470 struct hv_ring_buffer_info *rbi = &channel->inbound;
471 u32 packetlen = desc->len8 << 3;
472 u32 dsize = rbi->ring_datasize;
473
474 /* bump offset to next potential packet */
475 rbi->priv_read_index += packetlen + VMBUS_PKT_TRAILER;
476 if (rbi->priv_read_index >= dsize)
477 rbi->priv_read_index -= dsize;
478
479 /* more data? */
480 if (hv_pkt_iter_avail(rbi) < sizeof(struct vmpacket_descriptor))
481 return NULL;
482 else
483 return hv_get_ring_buffer(rbi) + rbi->priv_read_index;
484}
485EXPORT_SYMBOL_GPL(__hv_pkt_iter_next);
486
487/*
488 * Update host ring buffer after iterating over packets.
489 */
490void hv_pkt_iter_close(struct vmbus_channel *channel)
491{
492 struct hv_ring_buffer_info *rbi = &channel->inbound;
493
494 /*
495 * Make sure all reads are done before we update the read index since
496 * the writer may start writing to the read area once the read index
497 * is updated.
498 */
499 virt_rmb();
500 rbi->ring_buffer->read_index = rbi->priv_read_index;
501
502 hv_signal_on_read(channel);
503}
504EXPORT_SYMBOL_GPL(hv_pkt_iter_close);