blob: 50e071444a5c5c319b8c253d4e8ee8cb87e1145f [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>
Stephen Hemminger8dd45f22017-06-25 12:30:26 -070032#include <linux/prefetch.h>
K. Y. Srinivasan3f335ea2011-05-12 19:34:15 -070033
K. Y. Srinivasan0f2a6612011-05-12 19:34:28 -070034#include "hyperv_vmbus.h"
Hank Janssen3e7ee492009-07-13 16:02:34 -070035
stephen hemmingerf3dd3f42017-02-27 10:26:48 -080036#define VMBUS_PKT_TRAILER 8
37
K. Y. Srinivasan98fa8cf2012-12-01 06:46:36 -080038/*
39 * When we write to the ring buffer, check if the host needs to
40 * be signaled. Here is the details of this protocol:
41 *
42 * 1. The host guarantees that while it is draining the
43 * ring buffer, it will set the interrupt_mask to
44 * indicate it does not need to be interrupted when
45 * new data is placed.
46 *
47 * 2. The host guarantees that it will completely drain
48 * the ring buffer before exiting the read loop. Further,
49 * once the ring buffer is empty, it will clear the
50 * interrupt_mask and re-check to see if new data has
51 * arrived.
K. Y. Srinivasan1f6ee4e2016-11-06 13:14:17 -080052 *
53 * KYS: Oct. 30, 2016:
54 * It looks like Windows hosts have logic to deal with DOS attacks that
55 * can be triggered if it receives interrupts when it is not expecting
56 * the interrupt. The host expects interrupts only when the ring
57 * transitions from empty to non-empty (or full to non full on the guest
58 * to host ring).
59 * So, base the signaling decision solely on the ring state until the
60 * host logic is fixed.
K. Y. Srinivasan98fa8cf2012-12-01 06:46:36 -080061 */
62
Stephen Hemmingerb103a562017-02-05 17:20:32 -070063static void hv_signal_on_write(u32 old_write, struct vmbus_channel *channel)
K. Y. Srinivasan98fa8cf2012-12-01 06:46:36 -080064{
K. Y. Srinivasan1f6ee4e2016-11-06 13:14:17 -080065 struct hv_ring_buffer_info *rbi = &channel->outbound;
66
K. Y. Srinivasandcd0eec2016-04-02 17:59:48 -070067 virt_mb();
K. Y. Srinivasand45faae2016-04-02 17:59:47 -070068 if (READ_ONCE(rbi->ring_buffer->interrupt_mask))
K. Y. Srinivasan1f6ee4e2016-11-06 13:14:17 -080069 return;
K. Y. Srinivasan98fa8cf2012-12-01 06:46:36 -080070
Jason Wange91e84f2013-06-20 12:58:57 +080071 /* check interrupt_mask before read_index */
K. Y. Srinivasandcd0eec2016-04-02 17:59:48 -070072 virt_rmb();
K. Y. Srinivasan98fa8cf2012-12-01 06:46:36 -080073 /*
74 * This is the only case we need to signal when the
75 * ring transitions from being empty to non-empty.
76 */
K. Y. Srinivasand45faae2016-04-02 17:59:47 -070077 if (old_write == READ_ONCE(rbi->ring_buffer->read_index))
K. Y. Srinivasan1f6ee4e2016-11-06 13:14:17 -080078 vmbus_setevent(channel);
K. Y. Srinivasan98fa8cf2012-12-01 06:46:36 -080079}
80
Vitaly Kuznetsov822f18d2015-12-14 19:01:57 -080081/* Get the next write location for the specified ring buffer. */
Greg Kroah-Hartman4d643112009-07-14 15:09:36 -070082static inline u32
K. Y. Srinivasan2b8a9122011-05-10 07:55:29 -070083hv_get_next_write_location(struct hv_ring_buffer_info *ring_info)
Hank Janssen3e7ee492009-07-13 16:02:34 -070084{
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -080085 u32 next = ring_info->ring_buffer->write_index;
Hank Janssen3e7ee492009-07-13 16:02:34 -070086
Hank Janssen3e7ee492009-07-13 16:02:34 -070087 return next;
88}
89
Vitaly Kuznetsov822f18d2015-12-14 19:01:57 -080090/* Set the next write location for the specified ring buffer. */
Hank Janssen3e7ee492009-07-13 16:02:34 -070091static inline void
K. Y. Srinivasan2b8a9122011-05-10 07:55:29 -070092hv_set_next_write_location(struct hv_ring_buffer_info *ring_info,
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -080093 u32 next_write_location)
Hank Janssen3e7ee492009-07-13 16:02:34 -070094{
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -080095 ring_info->ring_buffer->write_index = next_write_location;
Hank Janssen3e7ee492009-07-13 16:02:34 -070096}
97
Vitaly Kuznetsov822f18d2015-12-14 19:01:57 -080098/* Set the next read location for the specified ring buffer. */
Hank Janssen3e7ee492009-07-13 16:02:34 -070099static inline void
K. Y. Srinivasan2b8a9122011-05-10 07:55:29 -0700100hv_set_next_read_location(struct hv_ring_buffer_info *ring_info,
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800101 u32 next_read_location)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700102{
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800103 ring_info->ring_buffer->read_index = next_read_location;
K. Y. Srinivasanab028db2016-04-02 17:59:51 -0700104 ring_info->priv_read_index = next_read_location;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700105}
106
Vitaly Kuznetsov822f18d2015-12-14 19:01:57 -0800107/* Get the size of the ring buffer. */
Greg Kroah-Hartman4d643112009-07-14 15:09:36 -0700108static inline u32
Stephen Hemmingere4165a02017-02-11 23:02:24 -0700109hv_get_ring_buffersize(const struct hv_ring_buffer_info *ring_info)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700110{
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800111 return ring_info->ring_datasize;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700112}
113
Vitaly Kuznetsov822f18d2015-12-14 19:01:57 -0800114/* Get the read and write indices as u64 of the specified ring buffer. */
Greg Kroah-Hartman59471432009-07-14 15:10:26 -0700115static inline u64
K. Y. Srinivasan2b8a9122011-05-10 07:55:29 -0700116hv_get_ring_bufferindices(struct hv_ring_buffer_info *ring_info)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700117{
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800118 return (u64)ring_info->ring_buffer->write_index << 32;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700119}
120
K. Y. Srinivasan8f1136a2011-05-10 07:55:31 -0700121/*
K. Y. Srinivasan75815782011-05-10 07:55:32 -0700122 * Helper routine to copy from source to ring buffer.
123 * Assume there is enough room. Handles wrap-around in dest case only!!
K. Y. Srinivasan75815782011-05-10 07:55:32 -0700124 */
125static u32 hv_copyto_ringbuffer(
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800126 struct hv_ring_buffer_info *ring_info,
127 u32 start_write_offset,
Stephen Hemmingere4165a02017-02-11 23:02:24 -0700128 const void *src,
K. Y. Srinivasan75815782011-05-10 07:55:32 -0700129 u32 srclen)
130{
131 void *ring_buffer = hv_get_ring_buffer(ring_info);
132 u32 ring_buffer_size = hv_get_ring_buffersize(ring_info);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700133
Vitaly Kuznetsovf24f0b42016-09-02 05:58:21 -0700134 memcpy(ring_buffer + start_write_offset, src, srclen);
K. Y. Srinivasan75815782011-05-10 07:55:32 -0700135
136 start_write_offset += srclen;
Stephen Hemminger8d12f882017-02-11 23:02:25 -0700137 if (start_write_offset >= ring_buffer_size)
138 start_write_offset -= ring_buffer_size;
K. Y. Srinivasan75815782011-05-10 07:55:32 -0700139
140 return start_write_offset;
141}
Hank Janssen3e7ee492009-07-13 16:02:34 -0700142
Stephen Hemminger04874262017-12-01 11:01:49 -0800143/*
144 *
145 * hv_get_ringbuffer_availbytes()
146 *
147 * Get number of bytes available to read and to write to
148 * for the specified ring buffer
149 */
150static void
151hv_get_ringbuffer_availbytes(const struct hv_ring_buffer_info *rbi,
152 u32 *read, u32 *write)
153{
154 u32 read_loc, write_loc, dsize;
155
156 /* Capture the read/write indices before they changed */
157 read_loc = READ_ONCE(rbi->ring_buffer->read_index);
158 write_loc = READ_ONCE(rbi->ring_buffer->write_index);
159 dsize = rbi->ring_datasize;
160
161 *write = write_loc >= read_loc ? dsize - (write_loc - read_loc) :
162 read_loc - write_loc;
163 *read = dsize - *write;
164}
165
Vitaly Kuznetsov822f18d2015-12-14 19:01:57 -0800166/* Get various debug metrics for the specified ring buffer. */
Stephen Hemmingere4165a02017-02-11 23:02:24 -0700167void hv_ringbuffer_get_debuginfo(const struct hv_ring_buffer_info *ring_info,
168 struct hv_ring_buffer_debug_info *debug_info)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700169{
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800170 u32 bytes_avail_towrite;
171 u32 bytes_avail_toread;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700172
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800173 if (ring_info->ring_buffer) {
K. Y. Srinivasan2b8a9122011-05-10 07:55:29 -0700174 hv_get_ringbuffer_availbytes(ring_info,
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800175 &bytes_avail_toread,
176 &bytes_avail_towrite);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700177
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800178 debug_info->bytes_avail_toread = bytes_avail_toread;
179 debug_info->bytes_avail_towrite = bytes_avail_towrite;
Haiyang Zhang82f8bd42010-11-08 14:04:45 -0800180 debug_info->current_read_index =
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800181 ring_info->ring_buffer->read_index;
Haiyang Zhang82f8bd42010-11-08 14:04:45 -0800182 debug_info->current_write_index =
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800183 ring_info->ring_buffer->write_index;
Haiyang Zhang82f8bd42010-11-08 14:04:45 -0800184 debug_info->current_interrupt_mask =
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800185 ring_info->ring_buffer->interrupt_mask;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700186 }
187}
Stephen Hemminger4827ee12017-03-04 18:27:18 -0700188EXPORT_SYMBOL_GPL(hv_ringbuffer_get_debuginfo);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700189
Vitaly Kuznetsov822f18d2015-12-14 19:01:57 -0800190/* Initialize the ring buffer. */
K. Y. Srinivasan72a95cb2011-05-10 07:55:21 -0700191int hv_ringbuffer_init(struct hv_ring_buffer_info *ring_info,
Vitaly Kuznetsov9988ce62016-09-02 05:58:20 -0700192 struct page *pages, u32 page_cnt)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700193{
Vitaly Kuznetsov9988ce62016-09-02 05:58:20 -0700194 int i;
195 struct page **pages_wraparound;
196
197 BUILD_BUG_ON((sizeof(struct hv_ring_buffer) != PAGE_SIZE));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700198
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800199 memset(ring_info, 0, sizeof(struct hv_ring_buffer_info));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700200
Vitaly Kuznetsov9988ce62016-09-02 05:58:20 -0700201 /*
202 * First page holds struct hv_ring_buffer, do wraparound mapping for
203 * the rest.
204 */
205 pages_wraparound = kzalloc(sizeof(struct page *) * (page_cnt * 2 - 1),
206 GFP_KERNEL);
207 if (!pages_wraparound)
208 return -ENOMEM;
209
210 pages_wraparound[0] = pages;
211 for (i = 0; i < 2 * (page_cnt - 1); i++)
212 pages_wraparound[i + 1] = &pages[i % (page_cnt - 1) + 1];
213
214 ring_info->ring_buffer = (struct hv_ring_buffer *)
215 vmap(pages_wraparound, page_cnt * 2 - 1, VM_MAP, PAGE_KERNEL);
216
217 kfree(pages_wraparound);
218
219
220 if (!ring_info->ring_buffer)
221 return -ENOMEM;
222
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800223 ring_info->ring_buffer->read_index =
224 ring_info->ring_buffer->write_index = 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700225
Vitaly Kuznetsov822f18d2015-12-14 19:01:57 -0800226 /* Set the feature bit for enabling flow control. */
K. Y. Srinivasan046c7912014-09-05 17:29:12 -0700227 ring_info->ring_buffer->feature_bits.value = 1;
228
Vitaly Kuznetsov9988ce62016-09-02 05:58:20 -0700229 ring_info->ring_size = page_cnt << PAGE_SHIFT;
230 ring_info->ring_datasize = ring_info->ring_size -
231 sizeof(struct hv_ring_buffer);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700232
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800233 spin_lock_init(&ring_info->ring_lock);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700234
235 return 0;
236}
237
Vitaly Kuznetsov822f18d2015-12-14 19:01:57 -0800238/* Cleanup the ring buffer. */
K. Y. Srinivasan2dba6882011-05-10 07:55:22 -0700239void hv_ringbuffer_cleanup(struct hv_ring_buffer_info *ring_info)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700240{
Vitaly Kuznetsov9988ce62016-09-02 05:58:20 -0700241 vunmap(ring_info->ring_buffer);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700242}
243
Vitaly Kuznetsov822f18d2015-12-14 19:01:57 -0800244/* Write to the ring buffer. */
K. Y. Srinivasan1f6ee4e2016-11-06 13:14:17 -0800245int hv_ringbuffer_write(struct vmbus_channel *channel,
Stephen Hemmingere4165a02017-02-11 23:02:24 -0700246 const struct kvec *kv_list, u32 kv_count)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700247{
Stephen Hemminger2c616a82017-03-04 18:27:13 -0700248 int i;
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800249 u32 bytes_avail_towrite;
Stephen Hemminger2c616a82017-03-04 18:27:13 -0700250 u32 totalbytes_towrite = sizeof(u64);
K. Y. Srinivasan66a60542011-05-10 07:55:33 -0700251 u32 next_write_location;
K. Y. Srinivasan98fa8cf2012-12-01 06:46:36 -0800252 u32 old_write;
Stephen Hemminger2c616a82017-03-04 18:27:13 -0700253 u64 prev_indices;
254 unsigned long flags;
K. Y. Srinivasan1f6ee4e2016-11-06 13:14:17 -0800255 struct hv_ring_buffer_info *outring_info = &channel->outbound;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700256
K. Y. Srinivasane7e97dd2016-12-07 01:16:28 -0800257 if (channel->rescind)
258 return -ENODEV;
259
K. Y. Srinivasan011a7c32014-02-01 19:02:20 -0800260 for (i = 0; i < kv_count; i++)
261 totalbytes_towrite += kv_list[i].iov_len;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700262
Stephen Hemminger5529eaf2017-02-11 23:02:22 -0700263 spin_lock_irqsave(&outring_info->ring_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700264
K. Y. Srinivasana6341f02016-04-02 17:59:46 -0700265 bytes_avail_towrite = hv_get_bytes_to_write(outring_info);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700266
Vitaly Kuznetsov822f18d2015-12-14 19:01:57 -0800267 /*
268 * If there is only room for the packet, assume it is full.
269 * Otherwise, the next time around, we think the ring buffer
270 * is empty since the read index == write index.
271 */
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800272 if (bytes_avail_towrite <= totalbytes_towrite) {
Stephen Hemminger5529eaf2017-02-11 23:02:22 -0700273 spin_unlock_irqrestore(&outring_info->ring_lock, flags);
K. Y. Srinivasand2598f02011-08-25 09:48:58 -0700274 return -EAGAIN;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700275 }
276
Bill Pemberton454f18a2009-07-27 16:47:24 -0400277 /* Write to the ring buffer */
K. Y. Srinivasan2b8a9122011-05-10 07:55:29 -0700278 next_write_location = hv_get_next_write_location(outring_info);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700279
K. Y. Srinivasan98fa8cf2012-12-01 06:46:36 -0800280 old_write = next_write_location;
281
K. Y. Srinivasan011a7c32014-02-01 19:02:20 -0800282 for (i = 0; i < kv_count; i++) {
K. Y. Srinivasan2b8a9122011-05-10 07:55:29 -0700283 next_write_location = hv_copyto_ringbuffer(outring_info,
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800284 next_write_location,
K. Y. Srinivasan011a7c32014-02-01 19:02:20 -0800285 kv_list[i].iov_base,
286 kv_list[i].iov_len);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700287 }
288
Bill Pemberton454f18a2009-07-27 16:47:24 -0400289 /* Set previous packet start */
K. Y. Srinivasan2b8a9122011-05-10 07:55:29 -0700290 prev_indices = hv_get_ring_bufferindices(outring_info);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700291
K. Y. Srinivasan2b8a9122011-05-10 07:55:29 -0700292 next_write_location = hv_copyto_ringbuffer(outring_info,
Haiyang Zhangfc8c72e2010-11-08 14:04:46 -0800293 next_write_location,
294 &prev_indices,
Nicolas Palixb219b3f2009-07-30 17:37:23 +0200295 sizeof(u64));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700296
K. Y. Srinivasan98fa8cf2012-12-01 06:46:36 -0800297 /* Issue a full memory barrier before updating the write index */
K. Y. Srinivasandcd0eec2016-04-02 17:59:48 -0700298 virt_mb();
Hank Janssen3e7ee492009-07-13 16:02:34 -0700299
Bill Pemberton454f18a2009-07-27 16:47:24 -0400300 /* Now, update the write location */
K. Y. Srinivasan2b8a9122011-05-10 07:55:29 -0700301 hv_set_next_write_location(outring_info, next_write_location);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700302
Hank Janssen3e7ee492009-07-13 16:02:34 -0700303
Stephen Hemminger5529eaf2017-02-11 23:02:22 -0700304 spin_unlock_irqrestore(&outring_info->ring_lock, flags);
K. Y. Srinivasan98fa8cf2012-12-01 06:46:36 -0800305
Stephen Hemmingerb103a562017-02-05 17:20:32 -0700306 hv_signal_on_write(old_write, channel);
K. Y. Srinivasane7e97dd2016-12-07 01:16:28 -0800307
308 if (channel->rescind)
309 return -ENODEV;
310
Hank Janssen3e7ee492009-07-13 16:02:34 -0700311 return 0;
312}
313
K. Y. Srinivasan33725922016-11-06 13:14:18 -0800314int hv_ringbuffer_read(struct vmbus_channel *channel,
Vitaly Kuznetsov940b68e2015-12-14 19:02:01 -0800315 void *buffer, u32 buflen, u32 *buffer_actual_len,
K. Y. Srinivasan33725922016-11-06 13:14:18 -0800316 u64 *requestid, bool raw)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700317{
Stephen Hemminger4226ff62017-06-25 12:30:24 -0700318 struct vmpacket_descriptor *desc;
319 u32 packetlen, offset;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700320
Stephen Hemminger4226ff62017-06-25 12:30:24 -0700321 if (unlikely(buflen == 0))
Bill Pembertona16e1482010-05-05 15:27:50 -0400322 return -EINVAL;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700323
Vitaly Kuznetsov940b68e2015-12-14 19:02:01 -0800324 *buffer_actual_len = 0;
325 *requestid = 0;
326
Bill Pemberton454f18a2009-07-27 16:47:24 -0400327 /* Make sure there is something to read */
Stephen Hemminger4226ff62017-06-25 12:30:24 -0700328 desc = hv_pkt_iter_first(channel);
329 if (desc == NULL) {
Vitaly Kuznetsov940b68e2015-12-14 19:02:01 -0800330 /*
331 * No error is set when there is even no header, drivers are
332 * supposed to analyze buffer_actual_len.
333 */
Stephen Hemminger42dd2712017-03-04 18:27:15 -0700334 return 0;
Vitaly Kuznetsov940b68e2015-12-14 19:02:01 -0800335 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700336
Stephen Hemminger4226ff62017-06-25 12:30:24 -0700337 offset = raw ? 0 : (desc->offset8 << 3);
338 packetlen = (desc->len8 << 3) - offset;
Vitaly Kuznetsov940b68e2015-12-14 19:02:01 -0800339 *buffer_actual_len = packetlen;
Stephen Hemminger4226ff62017-06-25 12:30:24 -0700340 *requestid = desc->trans_id;
Vitaly Kuznetsov940b68e2015-12-14 19:02:01 -0800341
Stephen Hemminger4226ff62017-06-25 12:30:24 -0700342 if (unlikely(packetlen > buflen))
K. Y. Srinivasan3eba9a72016-01-27 22:29:44 -0800343 return -ENOBUFS;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700344
Stephen Hemminger4226ff62017-06-25 12:30:24 -0700345 /* since ring is double mapped, only one copy is necessary */
346 memcpy(buffer, (const char *)desc + offset, packetlen);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700347
Stephen Hemminger4226ff62017-06-25 12:30:24 -0700348 /* Advance ring index to next packet descriptor */
349 __hv_pkt_iter_next(channel, desc);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700350
Stephen Hemminger4226ff62017-06-25 12:30:24 -0700351 /* Notify host of update */
352 hv_pkt_iter_close(channel);
K. Y. Srinivasanc2b8e522012-12-01 06:46:57 -0800353
Stephen Hemminger42dd2712017-03-04 18:27:15 -0700354 return 0;
Vitaly Kuznetsovb5f53dd2015-12-14 19:01:59 -0800355}
stephen hemmingerf3dd3f42017-02-27 10:26:48 -0800356
357/*
358 * Determine number of bytes available in ring buffer after
359 * the current iterator (priv_read_index) location.
360 *
361 * This is similar to hv_get_bytes_to_read but with private
362 * read index instead.
363 */
364static u32 hv_pkt_iter_avail(const struct hv_ring_buffer_info *rbi)
365{
366 u32 priv_read_loc = rbi->priv_read_index;
367 u32 write_loc = READ_ONCE(rbi->ring_buffer->write_index);
368
369 if (write_loc >= priv_read_loc)
370 return write_loc - priv_read_loc;
371 else
372 return (rbi->ring_datasize - priv_read_loc) + write_loc;
373}
374
375/*
376 * Get first vmbus packet from ring buffer after read_index
377 *
378 * If ring buffer is empty, returns NULL and no other action needed.
379 */
380struct vmpacket_descriptor *hv_pkt_iter_first(struct vmbus_channel *channel)
381{
382 struct hv_ring_buffer_info *rbi = &channel->inbound;
Stephen Hemminger15e16742017-06-25 12:30:29 -0700383 struct vmpacket_descriptor *desc;
stephen hemmingerf3dd3f42017-02-27 10:26:48 -0800384
stephen hemmingerf3dd3f42017-02-27 10:26:48 -0800385 if (hv_pkt_iter_avail(rbi) < sizeof(struct vmpacket_descriptor))
386 return NULL;
387
Stephen Hemminger15e16742017-06-25 12:30:29 -0700388 desc = hv_get_ring_buffer(rbi) + rbi->priv_read_index;
389 if (desc)
390 prefetch((char *)desc + (desc->len8 << 3));
391
392 return desc;
stephen hemmingerf3dd3f42017-02-27 10:26:48 -0800393}
394EXPORT_SYMBOL_GPL(hv_pkt_iter_first);
395
396/*
397 * Get next vmbus packet from ring buffer.
398 *
399 * Advances the current location (priv_read_index) and checks for more
400 * data. If the end of the ring buffer is reached, then return NULL.
401 */
402struct vmpacket_descriptor *
403__hv_pkt_iter_next(struct vmbus_channel *channel,
404 const struct vmpacket_descriptor *desc)
405{
406 struct hv_ring_buffer_info *rbi = &channel->inbound;
407 u32 packetlen = desc->len8 << 3;
408 u32 dsize = rbi->ring_datasize;
409
410 /* bump offset to next potential packet */
411 rbi->priv_read_index += packetlen + VMBUS_PKT_TRAILER;
412 if (rbi->priv_read_index >= dsize)
413 rbi->priv_read_index -= dsize;
414
415 /* more data? */
Stephen Hemminger05d00bc2017-06-25 12:30:27 -0700416 return hv_pkt_iter_first(channel);
stephen hemmingerf3dd3f42017-02-27 10:26:48 -0800417}
418EXPORT_SYMBOL_GPL(__hv_pkt_iter_next);
419
420/*
421 * Update host ring buffer after iterating over packets.
422 */
423void hv_pkt_iter_close(struct vmbus_channel *channel)
424{
425 struct hv_ring_buffer_info *rbi = &channel->inbound;
Stephen Hemminger05d00bc2017-06-25 12:30:27 -0700426 u32 orig_write_sz = hv_get_bytes_to_write(rbi);
stephen hemmingerf3dd3f42017-02-27 10:26:48 -0800427
428 /*
429 * Make sure all reads are done before we update the read index since
430 * the writer may start writing to the read area once the read index
431 * is updated.
432 */
433 virt_rmb();
434 rbi->ring_buffer->read_index = rbi->priv_read_index;
435
Stephen Hemminger8dd45f22017-06-25 12:30:26 -0700436 /*
437 * Issue a full memory barrier before making the signaling decision.
438 * Here is the reason for having this barrier:
439 * If the reading of the pend_sz (in this function)
440 * were to be reordered and read before we commit the new read
441 * index (in the calling function) we could
442 * have a problem. If the host were to set the pending_sz after we
443 * have sampled pending_sz and go to sleep before we commit the
444 * read index, we could miss sending the interrupt. Issue a full
445 * memory barrier to address this.
446 */
447 virt_mb();
448
Stephen Hemminger03bad712017-06-25 12:30:28 -0700449 /* If host has disabled notifications then skip */
450 if (rbi->ring_buffer->interrupt_mask)
Stephen Hemminger8dd45f22017-06-25 12:30:26 -0700451 return;
452
Stephen Hemminger03bad712017-06-25 12:30:28 -0700453 if (rbi->ring_buffer->feature_bits.feat_pending_send_sz) {
454 u32 pending_sz = READ_ONCE(rbi->ring_buffer->pending_send_sz);
Stephen Hemminger8dd45f22017-06-25 12:30:26 -0700455
Stephen Hemminger03bad712017-06-25 12:30:28 -0700456 /*
457 * If there was space before we began iteration,
458 * then host was not blocked. Also handles case where
459 * pending_sz is zero then host has nothing pending
460 * and does not need to be signaled.
461 */
462 if (orig_write_sz > pending_sz)
463 return;
464
465 /* If pending write will not fit, don't give false hope. */
466 if (hv_get_bytes_to_write(rbi) < pending_sz)
467 return;
468 }
469
470 vmbus_setevent(channel);
stephen hemmingerf3dd3f42017-02-27 10:26:48 -0800471}
472EXPORT_SYMBOL_GPL(hv_pkt_iter_close);