xref: /OK3568_Linux_fs/kernel/drivers/hv/ring_buffer.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * Copyright (c) 2009, Microsoft Corporation.
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  * Authors:
7*4882a593Smuzhiyun  *   Haiyang Zhang <haiyangz@microsoft.com>
8*4882a593Smuzhiyun  *   Hank Janssen  <hjanssen@microsoft.com>
9*4882a593Smuzhiyun  *   K. Y. Srinivasan <kys@microsoft.com>
10*4882a593Smuzhiyun  */
11*4882a593Smuzhiyun #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12*4882a593Smuzhiyun 
13*4882a593Smuzhiyun #include <linux/kernel.h>
14*4882a593Smuzhiyun #include <linux/mm.h>
15*4882a593Smuzhiyun #include <linux/hyperv.h>
16*4882a593Smuzhiyun #include <linux/uio.h>
17*4882a593Smuzhiyun #include <linux/vmalloc.h>
18*4882a593Smuzhiyun #include <linux/slab.h>
19*4882a593Smuzhiyun #include <linux/prefetch.h>
20*4882a593Smuzhiyun 
21*4882a593Smuzhiyun #include "hyperv_vmbus.h"
22*4882a593Smuzhiyun 
23*4882a593Smuzhiyun #define VMBUS_PKT_TRAILER	8
24*4882a593Smuzhiyun 
25*4882a593Smuzhiyun /*
26*4882a593Smuzhiyun  * When we write to the ring buffer, check if the host needs to
27*4882a593Smuzhiyun  * be signaled. Here is the details of this protocol:
28*4882a593Smuzhiyun  *
29*4882a593Smuzhiyun  *	1. The host guarantees that while it is draining the
30*4882a593Smuzhiyun  *	   ring buffer, it will set the interrupt_mask to
31*4882a593Smuzhiyun  *	   indicate it does not need to be interrupted when
32*4882a593Smuzhiyun  *	   new data is placed.
33*4882a593Smuzhiyun  *
34*4882a593Smuzhiyun  *	2. The host guarantees that it will completely drain
35*4882a593Smuzhiyun  *	   the ring buffer before exiting the read loop. Further,
36*4882a593Smuzhiyun  *	   once the ring buffer is empty, it will clear the
37*4882a593Smuzhiyun  *	   interrupt_mask and re-check to see if new data has
38*4882a593Smuzhiyun  *	   arrived.
39*4882a593Smuzhiyun  *
40*4882a593Smuzhiyun  * KYS: Oct. 30, 2016:
41*4882a593Smuzhiyun  * It looks like Windows hosts have logic to deal with DOS attacks that
42*4882a593Smuzhiyun  * can be triggered if it receives interrupts when it is not expecting
43*4882a593Smuzhiyun  * the interrupt. The host expects interrupts only when the ring
44*4882a593Smuzhiyun  * transitions from empty to non-empty (or full to non full on the guest
45*4882a593Smuzhiyun  * to host ring).
46*4882a593Smuzhiyun  * So, base the signaling decision solely on the ring state until the
47*4882a593Smuzhiyun  * host logic is fixed.
48*4882a593Smuzhiyun  */
49*4882a593Smuzhiyun 
hv_signal_on_write(u32 old_write,struct vmbus_channel * channel)50*4882a593Smuzhiyun static void hv_signal_on_write(u32 old_write, struct vmbus_channel *channel)
51*4882a593Smuzhiyun {
52*4882a593Smuzhiyun 	struct hv_ring_buffer_info *rbi = &channel->outbound;
53*4882a593Smuzhiyun 
54*4882a593Smuzhiyun 	virt_mb();
55*4882a593Smuzhiyun 	if (READ_ONCE(rbi->ring_buffer->interrupt_mask))
56*4882a593Smuzhiyun 		return;
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun 	/* check interrupt_mask before read_index */
59*4882a593Smuzhiyun 	virt_rmb();
60*4882a593Smuzhiyun 	/*
61*4882a593Smuzhiyun 	 * This is the only case we need to signal when the
62*4882a593Smuzhiyun 	 * ring transitions from being empty to non-empty.
63*4882a593Smuzhiyun 	 */
64*4882a593Smuzhiyun 	if (old_write == READ_ONCE(rbi->ring_buffer->read_index)) {
65*4882a593Smuzhiyun 		++channel->intr_out_empty;
66*4882a593Smuzhiyun 		vmbus_setevent(channel);
67*4882a593Smuzhiyun 	}
68*4882a593Smuzhiyun }
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun /* Get the next write location for the specified ring buffer. */
71*4882a593Smuzhiyun static inline u32
hv_get_next_write_location(struct hv_ring_buffer_info * ring_info)72*4882a593Smuzhiyun hv_get_next_write_location(struct hv_ring_buffer_info *ring_info)
73*4882a593Smuzhiyun {
74*4882a593Smuzhiyun 	u32 next = ring_info->ring_buffer->write_index;
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun 	return next;
77*4882a593Smuzhiyun }
78*4882a593Smuzhiyun 
79*4882a593Smuzhiyun /* Set the next write location for the specified ring buffer. */
80*4882a593Smuzhiyun static inline void
hv_set_next_write_location(struct hv_ring_buffer_info * ring_info,u32 next_write_location)81*4882a593Smuzhiyun hv_set_next_write_location(struct hv_ring_buffer_info *ring_info,
82*4882a593Smuzhiyun 		     u32 next_write_location)
83*4882a593Smuzhiyun {
84*4882a593Smuzhiyun 	ring_info->ring_buffer->write_index = next_write_location;
85*4882a593Smuzhiyun }
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun /* Set the next read location for the specified ring buffer. */
88*4882a593Smuzhiyun static inline void
hv_set_next_read_location(struct hv_ring_buffer_info * ring_info,u32 next_read_location)89*4882a593Smuzhiyun hv_set_next_read_location(struct hv_ring_buffer_info *ring_info,
90*4882a593Smuzhiyun 		    u32 next_read_location)
91*4882a593Smuzhiyun {
92*4882a593Smuzhiyun 	ring_info->ring_buffer->read_index = next_read_location;
93*4882a593Smuzhiyun 	ring_info->priv_read_index = next_read_location;
94*4882a593Smuzhiyun }
95*4882a593Smuzhiyun 
96*4882a593Smuzhiyun /* Get the size of the ring buffer. */
97*4882a593Smuzhiyun static inline u32
hv_get_ring_buffersize(const struct hv_ring_buffer_info * ring_info)98*4882a593Smuzhiyun hv_get_ring_buffersize(const struct hv_ring_buffer_info *ring_info)
99*4882a593Smuzhiyun {
100*4882a593Smuzhiyun 	return ring_info->ring_datasize;
101*4882a593Smuzhiyun }
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun /* Get the read and write indices as u64 of the specified ring buffer. */
104*4882a593Smuzhiyun static inline u64
hv_get_ring_bufferindices(struct hv_ring_buffer_info * ring_info)105*4882a593Smuzhiyun hv_get_ring_bufferindices(struct hv_ring_buffer_info *ring_info)
106*4882a593Smuzhiyun {
107*4882a593Smuzhiyun 	return (u64)ring_info->ring_buffer->write_index << 32;
108*4882a593Smuzhiyun }
109*4882a593Smuzhiyun 
110*4882a593Smuzhiyun /*
111*4882a593Smuzhiyun  * Helper routine to copy from source to ring buffer.
112*4882a593Smuzhiyun  * Assume there is enough room. Handles wrap-around in dest case only!!
113*4882a593Smuzhiyun  */
hv_copyto_ringbuffer(struct hv_ring_buffer_info * ring_info,u32 start_write_offset,const void * src,u32 srclen)114*4882a593Smuzhiyun static u32 hv_copyto_ringbuffer(
115*4882a593Smuzhiyun 	struct hv_ring_buffer_info	*ring_info,
116*4882a593Smuzhiyun 	u32				start_write_offset,
117*4882a593Smuzhiyun 	const void			*src,
118*4882a593Smuzhiyun 	u32				srclen)
119*4882a593Smuzhiyun {
120*4882a593Smuzhiyun 	void *ring_buffer = hv_get_ring_buffer(ring_info);
121*4882a593Smuzhiyun 	u32 ring_buffer_size = hv_get_ring_buffersize(ring_info);
122*4882a593Smuzhiyun 
123*4882a593Smuzhiyun 	memcpy(ring_buffer + start_write_offset, src, srclen);
124*4882a593Smuzhiyun 
125*4882a593Smuzhiyun 	start_write_offset += srclen;
126*4882a593Smuzhiyun 	if (start_write_offset >= ring_buffer_size)
127*4882a593Smuzhiyun 		start_write_offset -= ring_buffer_size;
128*4882a593Smuzhiyun 
129*4882a593Smuzhiyun 	return start_write_offset;
130*4882a593Smuzhiyun }
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun /*
133*4882a593Smuzhiyun  *
134*4882a593Smuzhiyun  * hv_get_ringbuffer_availbytes()
135*4882a593Smuzhiyun  *
136*4882a593Smuzhiyun  * Get number of bytes available to read and to write to
137*4882a593Smuzhiyun  * for the specified ring buffer
138*4882a593Smuzhiyun  */
139*4882a593Smuzhiyun static void
hv_get_ringbuffer_availbytes(const struct hv_ring_buffer_info * rbi,u32 * read,u32 * write)140*4882a593Smuzhiyun hv_get_ringbuffer_availbytes(const struct hv_ring_buffer_info *rbi,
141*4882a593Smuzhiyun 			     u32 *read, u32 *write)
142*4882a593Smuzhiyun {
143*4882a593Smuzhiyun 	u32 read_loc, write_loc, dsize;
144*4882a593Smuzhiyun 
145*4882a593Smuzhiyun 	/* Capture the read/write indices before they changed */
146*4882a593Smuzhiyun 	read_loc = READ_ONCE(rbi->ring_buffer->read_index);
147*4882a593Smuzhiyun 	write_loc = READ_ONCE(rbi->ring_buffer->write_index);
148*4882a593Smuzhiyun 	dsize = rbi->ring_datasize;
149*4882a593Smuzhiyun 
150*4882a593Smuzhiyun 	*write = write_loc >= read_loc ? dsize - (write_loc - read_loc) :
151*4882a593Smuzhiyun 		read_loc - write_loc;
152*4882a593Smuzhiyun 	*read = dsize - *write;
153*4882a593Smuzhiyun }
154*4882a593Smuzhiyun 
155*4882a593Smuzhiyun /* Get various debug metrics for the specified ring buffer. */
hv_ringbuffer_get_debuginfo(struct hv_ring_buffer_info * ring_info,struct hv_ring_buffer_debug_info * debug_info)156*4882a593Smuzhiyun int hv_ringbuffer_get_debuginfo(struct hv_ring_buffer_info *ring_info,
157*4882a593Smuzhiyun 				struct hv_ring_buffer_debug_info *debug_info)
158*4882a593Smuzhiyun {
159*4882a593Smuzhiyun 	u32 bytes_avail_towrite;
160*4882a593Smuzhiyun 	u32 bytes_avail_toread;
161*4882a593Smuzhiyun 
162*4882a593Smuzhiyun 	mutex_lock(&ring_info->ring_buffer_mutex);
163*4882a593Smuzhiyun 
164*4882a593Smuzhiyun 	if (!ring_info->ring_buffer) {
165*4882a593Smuzhiyun 		mutex_unlock(&ring_info->ring_buffer_mutex);
166*4882a593Smuzhiyun 		return -EINVAL;
167*4882a593Smuzhiyun 	}
168*4882a593Smuzhiyun 
169*4882a593Smuzhiyun 	hv_get_ringbuffer_availbytes(ring_info,
170*4882a593Smuzhiyun 				     &bytes_avail_toread,
171*4882a593Smuzhiyun 				     &bytes_avail_towrite);
172*4882a593Smuzhiyun 	debug_info->bytes_avail_toread = bytes_avail_toread;
173*4882a593Smuzhiyun 	debug_info->bytes_avail_towrite = bytes_avail_towrite;
174*4882a593Smuzhiyun 	debug_info->current_read_index = ring_info->ring_buffer->read_index;
175*4882a593Smuzhiyun 	debug_info->current_write_index = ring_info->ring_buffer->write_index;
176*4882a593Smuzhiyun 	debug_info->current_interrupt_mask
177*4882a593Smuzhiyun 		= ring_info->ring_buffer->interrupt_mask;
178*4882a593Smuzhiyun 	mutex_unlock(&ring_info->ring_buffer_mutex);
179*4882a593Smuzhiyun 
180*4882a593Smuzhiyun 	return 0;
181*4882a593Smuzhiyun }
182*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(hv_ringbuffer_get_debuginfo);
183*4882a593Smuzhiyun 
184*4882a593Smuzhiyun /* Initialize a channel's ring buffer info mutex locks */
hv_ringbuffer_pre_init(struct vmbus_channel * channel)185*4882a593Smuzhiyun void hv_ringbuffer_pre_init(struct vmbus_channel *channel)
186*4882a593Smuzhiyun {
187*4882a593Smuzhiyun 	mutex_init(&channel->inbound.ring_buffer_mutex);
188*4882a593Smuzhiyun 	mutex_init(&channel->outbound.ring_buffer_mutex);
189*4882a593Smuzhiyun }
190*4882a593Smuzhiyun 
191*4882a593Smuzhiyun /* Initialize the ring buffer. */
hv_ringbuffer_init(struct hv_ring_buffer_info * ring_info,struct page * pages,u32 page_cnt)192*4882a593Smuzhiyun int hv_ringbuffer_init(struct hv_ring_buffer_info *ring_info,
193*4882a593Smuzhiyun 		       struct page *pages, u32 page_cnt)
194*4882a593Smuzhiyun {
195*4882a593Smuzhiyun 	int i;
196*4882a593Smuzhiyun 	struct page **pages_wraparound;
197*4882a593Smuzhiyun 
198*4882a593Smuzhiyun 	BUILD_BUG_ON((sizeof(struct hv_ring_buffer) != PAGE_SIZE));
199*4882a593Smuzhiyun 
200*4882a593Smuzhiyun 	/*
201*4882a593Smuzhiyun 	 * First page holds struct hv_ring_buffer, do wraparound mapping for
202*4882a593Smuzhiyun 	 * the rest.
203*4882a593Smuzhiyun 	 */
204*4882a593Smuzhiyun 	pages_wraparound = kcalloc(page_cnt * 2 - 1, sizeof(struct page *),
205*4882a593Smuzhiyun 				   GFP_KERNEL);
206*4882a593Smuzhiyun 	if (!pages_wraparound)
207*4882a593Smuzhiyun 		return -ENOMEM;
208*4882a593Smuzhiyun 
209*4882a593Smuzhiyun 	pages_wraparound[0] = pages;
210*4882a593Smuzhiyun 	for (i = 0; i < 2 * (page_cnt - 1); i++)
211*4882a593Smuzhiyun 		pages_wraparound[i + 1] = &pages[i % (page_cnt - 1) + 1];
212*4882a593Smuzhiyun 
213*4882a593Smuzhiyun 	ring_info->ring_buffer = (struct hv_ring_buffer *)
214*4882a593Smuzhiyun 		vmap(pages_wraparound, page_cnt * 2 - 1, VM_MAP, PAGE_KERNEL);
215*4882a593Smuzhiyun 
216*4882a593Smuzhiyun 	kfree(pages_wraparound);
217*4882a593Smuzhiyun 
218*4882a593Smuzhiyun 
219*4882a593Smuzhiyun 	if (!ring_info->ring_buffer)
220*4882a593Smuzhiyun 		return -ENOMEM;
221*4882a593Smuzhiyun 
222*4882a593Smuzhiyun 	ring_info->ring_buffer->read_index =
223*4882a593Smuzhiyun 		ring_info->ring_buffer->write_index = 0;
224*4882a593Smuzhiyun 
225*4882a593Smuzhiyun 	/* Set the feature bit for enabling flow control. */
226*4882a593Smuzhiyun 	ring_info->ring_buffer->feature_bits.value = 1;
227*4882a593Smuzhiyun 
228*4882a593Smuzhiyun 	ring_info->ring_size = page_cnt << PAGE_SHIFT;
229*4882a593Smuzhiyun 	ring_info->ring_size_div10_reciprocal =
230*4882a593Smuzhiyun 		reciprocal_value(ring_info->ring_size / 10);
231*4882a593Smuzhiyun 	ring_info->ring_datasize = ring_info->ring_size -
232*4882a593Smuzhiyun 		sizeof(struct hv_ring_buffer);
233*4882a593Smuzhiyun 	ring_info->priv_read_index = 0;
234*4882a593Smuzhiyun 
235*4882a593Smuzhiyun 	spin_lock_init(&ring_info->ring_lock);
236*4882a593Smuzhiyun 
237*4882a593Smuzhiyun 	return 0;
238*4882a593Smuzhiyun }
239*4882a593Smuzhiyun 
240*4882a593Smuzhiyun /* Cleanup the ring buffer. */
hv_ringbuffer_cleanup(struct hv_ring_buffer_info * ring_info)241*4882a593Smuzhiyun void hv_ringbuffer_cleanup(struct hv_ring_buffer_info *ring_info)
242*4882a593Smuzhiyun {
243*4882a593Smuzhiyun 	mutex_lock(&ring_info->ring_buffer_mutex);
244*4882a593Smuzhiyun 	vunmap(ring_info->ring_buffer);
245*4882a593Smuzhiyun 	ring_info->ring_buffer = NULL;
246*4882a593Smuzhiyun 	mutex_unlock(&ring_info->ring_buffer_mutex);
247*4882a593Smuzhiyun }
248*4882a593Smuzhiyun 
249*4882a593Smuzhiyun /* Write to the ring buffer. */
hv_ringbuffer_write(struct vmbus_channel * channel,const struct kvec * kv_list,u32 kv_count)250*4882a593Smuzhiyun int hv_ringbuffer_write(struct vmbus_channel *channel,
251*4882a593Smuzhiyun 			const struct kvec *kv_list, u32 kv_count)
252*4882a593Smuzhiyun {
253*4882a593Smuzhiyun 	int i;
254*4882a593Smuzhiyun 	u32 bytes_avail_towrite;
255*4882a593Smuzhiyun 	u32 totalbytes_towrite = sizeof(u64);
256*4882a593Smuzhiyun 	u32 next_write_location;
257*4882a593Smuzhiyun 	u32 old_write;
258*4882a593Smuzhiyun 	u64 prev_indices;
259*4882a593Smuzhiyun 	unsigned long flags;
260*4882a593Smuzhiyun 	struct hv_ring_buffer_info *outring_info = &channel->outbound;
261*4882a593Smuzhiyun 
262*4882a593Smuzhiyun 	if (channel->rescind)
263*4882a593Smuzhiyun 		return -ENODEV;
264*4882a593Smuzhiyun 
265*4882a593Smuzhiyun 	for (i = 0; i < kv_count; i++)
266*4882a593Smuzhiyun 		totalbytes_towrite += kv_list[i].iov_len;
267*4882a593Smuzhiyun 
268*4882a593Smuzhiyun 	spin_lock_irqsave(&outring_info->ring_lock, flags);
269*4882a593Smuzhiyun 
270*4882a593Smuzhiyun 	bytes_avail_towrite = hv_get_bytes_to_write(outring_info);
271*4882a593Smuzhiyun 
272*4882a593Smuzhiyun 	/*
273*4882a593Smuzhiyun 	 * If there is only room for the packet, assume it is full.
274*4882a593Smuzhiyun 	 * Otherwise, the next time around, we think the ring buffer
275*4882a593Smuzhiyun 	 * is empty since the read index == write index.
276*4882a593Smuzhiyun 	 */
277*4882a593Smuzhiyun 	if (bytes_avail_towrite <= totalbytes_towrite) {
278*4882a593Smuzhiyun 		++channel->out_full_total;
279*4882a593Smuzhiyun 
280*4882a593Smuzhiyun 		if (!channel->out_full_flag) {
281*4882a593Smuzhiyun 			++channel->out_full_first;
282*4882a593Smuzhiyun 			channel->out_full_flag = true;
283*4882a593Smuzhiyun 		}
284*4882a593Smuzhiyun 
285*4882a593Smuzhiyun 		spin_unlock_irqrestore(&outring_info->ring_lock, flags);
286*4882a593Smuzhiyun 		return -EAGAIN;
287*4882a593Smuzhiyun 	}
288*4882a593Smuzhiyun 
289*4882a593Smuzhiyun 	channel->out_full_flag = false;
290*4882a593Smuzhiyun 
291*4882a593Smuzhiyun 	/* Write to the ring buffer */
292*4882a593Smuzhiyun 	next_write_location = hv_get_next_write_location(outring_info);
293*4882a593Smuzhiyun 
294*4882a593Smuzhiyun 	old_write = next_write_location;
295*4882a593Smuzhiyun 
296*4882a593Smuzhiyun 	for (i = 0; i < kv_count; i++) {
297*4882a593Smuzhiyun 		next_write_location = hv_copyto_ringbuffer(outring_info,
298*4882a593Smuzhiyun 						     next_write_location,
299*4882a593Smuzhiyun 						     kv_list[i].iov_base,
300*4882a593Smuzhiyun 						     kv_list[i].iov_len);
301*4882a593Smuzhiyun 	}
302*4882a593Smuzhiyun 
303*4882a593Smuzhiyun 	/* Set previous packet start */
304*4882a593Smuzhiyun 	prev_indices = hv_get_ring_bufferindices(outring_info);
305*4882a593Smuzhiyun 
306*4882a593Smuzhiyun 	next_write_location = hv_copyto_ringbuffer(outring_info,
307*4882a593Smuzhiyun 					     next_write_location,
308*4882a593Smuzhiyun 					     &prev_indices,
309*4882a593Smuzhiyun 					     sizeof(u64));
310*4882a593Smuzhiyun 
311*4882a593Smuzhiyun 	/* Issue a full memory barrier before updating the write index */
312*4882a593Smuzhiyun 	virt_mb();
313*4882a593Smuzhiyun 
314*4882a593Smuzhiyun 	/* Now, update the write location */
315*4882a593Smuzhiyun 	hv_set_next_write_location(outring_info, next_write_location);
316*4882a593Smuzhiyun 
317*4882a593Smuzhiyun 
318*4882a593Smuzhiyun 	spin_unlock_irqrestore(&outring_info->ring_lock, flags);
319*4882a593Smuzhiyun 
320*4882a593Smuzhiyun 	hv_signal_on_write(old_write, channel);
321*4882a593Smuzhiyun 
322*4882a593Smuzhiyun 	if (channel->rescind)
323*4882a593Smuzhiyun 		return -ENODEV;
324*4882a593Smuzhiyun 
325*4882a593Smuzhiyun 	return 0;
326*4882a593Smuzhiyun }
327*4882a593Smuzhiyun 
hv_ringbuffer_read(struct vmbus_channel * channel,void * buffer,u32 buflen,u32 * buffer_actual_len,u64 * requestid,bool raw)328*4882a593Smuzhiyun int hv_ringbuffer_read(struct vmbus_channel *channel,
329*4882a593Smuzhiyun 		       void *buffer, u32 buflen, u32 *buffer_actual_len,
330*4882a593Smuzhiyun 		       u64 *requestid, bool raw)
331*4882a593Smuzhiyun {
332*4882a593Smuzhiyun 	struct vmpacket_descriptor *desc;
333*4882a593Smuzhiyun 	u32 packetlen, offset;
334*4882a593Smuzhiyun 
335*4882a593Smuzhiyun 	if (unlikely(buflen == 0))
336*4882a593Smuzhiyun 		return -EINVAL;
337*4882a593Smuzhiyun 
338*4882a593Smuzhiyun 	*buffer_actual_len = 0;
339*4882a593Smuzhiyun 	*requestid = 0;
340*4882a593Smuzhiyun 
341*4882a593Smuzhiyun 	/* Make sure there is something to read */
342*4882a593Smuzhiyun 	desc = hv_pkt_iter_first(channel);
343*4882a593Smuzhiyun 	if (desc == NULL) {
344*4882a593Smuzhiyun 		/*
345*4882a593Smuzhiyun 		 * No error is set when there is even no header, drivers are
346*4882a593Smuzhiyun 		 * supposed to analyze buffer_actual_len.
347*4882a593Smuzhiyun 		 */
348*4882a593Smuzhiyun 		return 0;
349*4882a593Smuzhiyun 	}
350*4882a593Smuzhiyun 
351*4882a593Smuzhiyun 	offset = raw ? 0 : (desc->offset8 << 3);
352*4882a593Smuzhiyun 	packetlen = (desc->len8 << 3) - offset;
353*4882a593Smuzhiyun 	*buffer_actual_len = packetlen;
354*4882a593Smuzhiyun 	*requestid = desc->trans_id;
355*4882a593Smuzhiyun 
356*4882a593Smuzhiyun 	if (unlikely(packetlen > buflen))
357*4882a593Smuzhiyun 		return -ENOBUFS;
358*4882a593Smuzhiyun 
359*4882a593Smuzhiyun 	/* since ring is double mapped, only one copy is necessary */
360*4882a593Smuzhiyun 	memcpy(buffer, (const char *)desc + offset, packetlen);
361*4882a593Smuzhiyun 
362*4882a593Smuzhiyun 	/* Advance ring index to next packet descriptor */
363*4882a593Smuzhiyun 	__hv_pkt_iter_next(channel, desc);
364*4882a593Smuzhiyun 
365*4882a593Smuzhiyun 	/* Notify host of update */
366*4882a593Smuzhiyun 	hv_pkt_iter_close(channel);
367*4882a593Smuzhiyun 
368*4882a593Smuzhiyun 	return 0;
369*4882a593Smuzhiyun }
370*4882a593Smuzhiyun 
371*4882a593Smuzhiyun /*
372*4882a593Smuzhiyun  * Determine number of bytes available in ring buffer after
373*4882a593Smuzhiyun  * the current iterator (priv_read_index) location.
374*4882a593Smuzhiyun  *
375*4882a593Smuzhiyun  * This is similar to hv_get_bytes_to_read but with private
376*4882a593Smuzhiyun  * read index instead.
377*4882a593Smuzhiyun  */
hv_pkt_iter_avail(const struct hv_ring_buffer_info * rbi)378*4882a593Smuzhiyun static u32 hv_pkt_iter_avail(const struct hv_ring_buffer_info *rbi)
379*4882a593Smuzhiyun {
380*4882a593Smuzhiyun 	u32 priv_read_loc = rbi->priv_read_index;
381*4882a593Smuzhiyun 	u32 write_loc;
382*4882a593Smuzhiyun 
383*4882a593Smuzhiyun 	/*
384*4882a593Smuzhiyun 	 * The Hyper-V host writes the packet data, then uses
385*4882a593Smuzhiyun 	 * store_release() to update the write_index.  Use load_acquire()
386*4882a593Smuzhiyun 	 * here to prevent loads of the packet data from being re-ordered
387*4882a593Smuzhiyun 	 * before the read of the write_index and potentially getting
388*4882a593Smuzhiyun 	 * stale data.
389*4882a593Smuzhiyun 	 */
390*4882a593Smuzhiyun 	write_loc = virt_load_acquire(&rbi->ring_buffer->write_index);
391*4882a593Smuzhiyun 
392*4882a593Smuzhiyun 	if (write_loc >= priv_read_loc)
393*4882a593Smuzhiyun 		return write_loc - priv_read_loc;
394*4882a593Smuzhiyun 	else
395*4882a593Smuzhiyun 		return (rbi->ring_datasize - priv_read_loc) + write_loc;
396*4882a593Smuzhiyun }
397*4882a593Smuzhiyun 
398*4882a593Smuzhiyun /*
399*4882a593Smuzhiyun  * Get first vmbus packet from ring buffer after read_index
400*4882a593Smuzhiyun  *
401*4882a593Smuzhiyun  * If ring buffer is empty, returns NULL and no other action needed.
402*4882a593Smuzhiyun  */
hv_pkt_iter_first(struct vmbus_channel * channel)403*4882a593Smuzhiyun struct vmpacket_descriptor *hv_pkt_iter_first(struct vmbus_channel *channel)
404*4882a593Smuzhiyun {
405*4882a593Smuzhiyun 	struct hv_ring_buffer_info *rbi = &channel->inbound;
406*4882a593Smuzhiyun 	struct vmpacket_descriptor *desc;
407*4882a593Smuzhiyun 
408*4882a593Smuzhiyun 	hv_debug_delay_test(channel, MESSAGE_DELAY);
409*4882a593Smuzhiyun 	if (hv_pkt_iter_avail(rbi) < sizeof(struct vmpacket_descriptor))
410*4882a593Smuzhiyun 		return NULL;
411*4882a593Smuzhiyun 
412*4882a593Smuzhiyun 	desc = hv_get_ring_buffer(rbi) + rbi->priv_read_index;
413*4882a593Smuzhiyun 	if (desc)
414*4882a593Smuzhiyun 		prefetch((char *)desc + (desc->len8 << 3));
415*4882a593Smuzhiyun 
416*4882a593Smuzhiyun 	return desc;
417*4882a593Smuzhiyun }
418*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(hv_pkt_iter_first);
419*4882a593Smuzhiyun 
420*4882a593Smuzhiyun /*
421*4882a593Smuzhiyun  * Get next vmbus packet from ring buffer.
422*4882a593Smuzhiyun  *
423*4882a593Smuzhiyun  * Advances the current location (priv_read_index) and checks for more
424*4882a593Smuzhiyun  * data. If the end of the ring buffer is reached, then return NULL.
425*4882a593Smuzhiyun  */
426*4882a593Smuzhiyun struct vmpacket_descriptor *
__hv_pkt_iter_next(struct vmbus_channel * channel,const struct vmpacket_descriptor * desc)427*4882a593Smuzhiyun __hv_pkt_iter_next(struct vmbus_channel *channel,
428*4882a593Smuzhiyun 		   const struct vmpacket_descriptor *desc)
429*4882a593Smuzhiyun {
430*4882a593Smuzhiyun 	struct hv_ring_buffer_info *rbi = &channel->inbound;
431*4882a593Smuzhiyun 	u32 packetlen = desc->len8 << 3;
432*4882a593Smuzhiyun 	u32 dsize = rbi->ring_datasize;
433*4882a593Smuzhiyun 
434*4882a593Smuzhiyun 	hv_debug_delay_test(channel, MESSAGE_DELAY);
435*4882a593Smuzhiyun 	/* bump offset to next potential packet */
436*4882a593Smuzhiyun 	rbi->priv_read_index += packetlen + VMBUS_PKT_TRAILER;
437*4882a593Smuzhiyun 	if (rbi->priv_read_index >= dsize)
438*4882a593Smuzhiyun 		rbi->priv_read_index -= dsize;
439*4882a593Smuzhiyun 
440*4882a593Smuzhiyun 	/* more data? */
441*4882a593Smuzhiyun 	return hv_pkt_iter_first(channel);
442*4882a593Smuzhiyun }
443*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(__hv_pkt_iter_next);
444*4882a593Smuzhiyun 
445*4882a593Smuzhiyun /* How many bytes were read in this iterator cycle */
hv_pkt_iter_bytes_read(const struct hv_ring_buffer_info * rbi,u32 start_read_index)446*4882a593Smuzhiyun static u32 hv_pkt_iter_bytes_read(const struct hv_ring_buffer_info *rbi,
447*4882a593Smuzhiyun 					u32 start_read_index)
448*4882a593Smuzhiyun {
449*4882a593Smuzhiyun 	if (rbi->priv_read_index >= start_read_index)
450*4882a593Smuzhiyun 		return rbi->priv_read_index - start_read_index;
451*4882a593Smuzhiyun 	else
452*4882a593Smuzhiyun 		return rbi->ring_datasize - start_read_index +
453*4882a593Smuzhiyun 			rbi->priv_read_index;
454*4882a593Smuzhiyun }
455*4882a593Smuzhiyun 
456*4882a593Smuzhiyun /*
457*4882a593Smuzhiyun  * Update host ring buffer after iterating over packets. If the host has
458*4882a593Smuzhiyun  * stopped queuing new entries because it found the ring buffer full, and
459*4882a593Smuzhiyun  * sufficient space is being freed up, signal the host. But be careful to
460*4882a593Smuzhiyun  * only signal the host when necessary, both for performance reasons and
461*4882a593Smuzhiyun  * because Hyper-V protects itself by throttling guests that signal
462*4882a593Smuzhiyun  * inappropriately.
463*4882a593Smuzhiyun  *
464*4882a593Smuzhiyun  * Determining when to signal is tricky. There are three key data inputs
465*4882a593Smuzhiyun  * that must be handled in this order to avoid race conditions:
466*4882a593Smuzhiyun  *
467*4882a593Smuzhiyun  * 1. Update the read_index
468*4882a593Smuzhiyun  * 2. Read the pending_send_sz
469*4882a593Smuzhiyun  * 3. Read the current write_index
470*4882a593Smuzhiyun  *
471*4882a593Smuzhiyun  * The interrupt_mask is not used to determine when to signal. The
472*4882a593Smuzhiyun  * interrupt_mask is used only on the guest->host ring buffer when
473*4882a593Smuzhiyun  * sending requests to the host. The host does not use it on the host->
474*4882a593Smuzhiyun  * guest ring buffer to indicate whether it should be signaled.
475*4882a593Smuzhiyun  */
hv_pkt_iter_close(struct vmbus_channel * channel)476*4882a593Smuzhiyun void hv_pkt_iter_close(struct vmbus_channel *channel)
477*4882a593Smuzhiyun {
478*4882a593Smuzhiyun 	struct hv_ring_buffer_info *rbi = &channel->inbound;
479*4882a593Smuzhiyun 	u32 curr_write_sz, pending_sz, bytes_read, start_read_index;
480*4882a593Smuzhiyun 
481*4882a593Smuzhiyun 	/*
482*4882a593Smuzhiyun 	 * Make sure all reads are done before we update the read index since
483*4882a593Smuzhiyun 	 * the writer may start writing to the read area once the read index
484*4882a593Smuzhiyun 	 * is updated.
485*4882a593Smuzhiyun 	 */
486*4882a593Smuzhiyun 	virt_rmb();
487*4882a593Smuzhiyun 	start_read_index = rbi->ring_buffer->read_index;
488*4882a593Smuzhiyun 	rbi->ring_buffer->read_index = rbi->priv_read_index;
489*4882a593Smuzhiyun 
490*4882a593Smuzhiyun 	/*
491*4882a593Smuzhiyun 	 * Older versions of Hyper-V (before WS2102 and Win8) do not
492*4882a593Smuzhiyun 	 * implement pending_send_sz and simply poll if the host->guest
493*4882a593Smuzhiyun 	 * ring buffer is full.  No signaling is needed or expected.
494*4882a593Smuzhiyun 	 */
495*4882a593Smuzhiyun 	if (!rbi->ring_buffer->feature_bits.feat_pending_send_sz)
496*4882a593Smuzhiyun 		return;
497*4882a593Smuzhiyun 
498*4882a593Smuzhiyun 	/*
499*4882a593Smuzhiyun 	 * Issue a full memory barrier before making the signaling decision.
500*4882a593Smuzhiyun 	 * If reading pending_send_sz were to be reordered and happen
501*4882a593Smuzhiyun 	 * before we commit the new read_index, a race could occur.  If the
502*4882a593Smuzhiyun 	 * host were to set the pending_send_sz after we have sampled
503*4882a593Smuzhiyun 	 * pending_send_sz, and the ring buffer blocks before we commit the
504*4882a593Smuzhiyun 	 * read index, we could miss sending the interrupt. Issue a full
505*4882a593Smuzhiyun 	 * memory barrier to address this.
506*4882a593Smuzhiyun 	 */
507*4882a593Smuzhiyun 	virt_mb();
508*4882a593Smuzhiyun 
509*4882a593Smuzhiyun 	/*
510*4882a593Smuzhiyun 	 * If the pending_send_sz is zero, then the ring buffer is not
511*4882a593Smuzhiyun 	 * blocked and there is no need to signal.  This is far by the
512*4882a593Smuzhiyun 	 * most common case, so exit quickly for best performance.
513*4882a593Smuzhiyun 	 */
514*4882a593Smuzhiyun 	pending_sz = READ_ONCE(rbi->ring_buffer->pending_send_sz);
515*4882a593Smuzhiyun 	if (!pending_sz)
516*4882a593Smuzhiyun 		return;
517*4882a593Smuzhiyun 
518*4882a593Smuzhiyun 	/*
519*4882a593Smuzhiyun 	 * Ensure the read of write_index in hv_get_bytes_to_write()
520*4882a593Smuzhiyun 	 * happens after the read of pending_send_sz.
521*4882a593Smuzhiyun 	 */
522*4882a593Smuzhiyun 	virt_rmb();
523*4882a593Smuzhiyun 	curr_write_sz = hv_get_bytes_to_write(rbi);
524*4882a593Smuzhiyun 	bytes_read = hv_pkt_iter_bytes_read(rbi, start_read_index);
525*4882a593Smuzhiyun 
526*4882a593Smuzhiyun 	/*
527*4882a593Smuzhiyun 	 * We want to signal the host only if we're transitioning
528*4882a593Smuzhiyun 	 * from a "not enough free space" state to a "enough free
529*4882a593Smuzhiyun 	 * space" state.  For example, it's possible that this function
530*4882a593Smuzhiyun 	 * could run and free up enough space to signal the host, and then
531*4882a593Smuzhiyun 	 * run again and free up additional space before the host has a
532*4882a593Smuzhiyun 	 * chance to clear the pending_send_sz.  The 2nd invocation would
533*4882a593Smuzhiyun 	 * be a null transition from "enough free space" to "enough free
534*4882a593Smuzhiyun 	 * space", which doesn't warrant a signal.
535*4882a593Smuzhiyun 	 *
536*4882a593Smuzhiyun 	 * Exactly filling the ring buffer is treated as "not enough
537*4882a593Smuzhiyun 	 * space". The ring buffer always must have at least one byte
538*4882a593Smuzhiyun 	 * empty so the empty and full conditions are distinguishable.
539*4882a593Smuzhiyun 	 * hv_get_bytes_to_write() doesn't fully tell the truth in
540*4882a593Smuzhiyun 	 * this regard.
541*4882a593Smuzhiyun 	 *
542*4882a593Smuzhiyun 	 * So first check if we were in the "enough free space" state
543*4882a593Smuzhiyun 	 * before we began the iteration. If so, the host was not
544*4882a593Smuzhiyun 	 * blocked, and there's no need to signal.
545*4882a593Smuzhiyun 	 */
546*4882a593Smuzhiyun 	if (curr_write_sz - bytes_read > pending_sz)
547*4882a593Smuzhiyun 		return;
548*4882a593Smuzhiyun 
549*4882a593Smuzhiyun 	/*
550*4882a593Smuzhiyun 	 * Similarly, if the new state is "not enough space", then
551*4882a593Smuzhiyun 	 * there's no need to signal.
552*4882a593Smuzhiyun 	 */
553*4882a593Smuzhiyun 	if (curr_write_sz <= pending_sz)
554*4882a593Smuzhiyun 		return;
555*4882a593Smuzhiyun 
556*4882a593Smuzhiyun 	++channel->intr_in_full;
557*4882a593Smuzhiyun 	vmbus_setevent(channel);
558*4882a593Smuzhiyun }
559*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(hv_pkt_iter_close);
560