xref: /linux-6.15/drivers/hv/ring_buffer.c (revision bb862397)
13b20eb23SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
246a97191SGreg Kroah-Hartman /*
346a97191SGreg Kroah-Hartman  *
446a97191SGreg Kroah-Hartman  * Copyright (c) 2009, Microsoft Corporation.
546a97191SGreg Kroah-Hartman  *
646a97191SGreg Kroah-Hartman  * Authors:
746a97191SGreg Kroah-Hartman  *   Haiyang Zhang <[email protected]>
846a97191SGreg Kroah-Hartman  *   Hank Janssen  <[email protected]>
946a97191SGreg Kroah-Hartman  *   K. Y. Srinivasan <[email protected]>
1046a97191SGreg Kroah-Hartman  */
1146a97191SGreg Kroah-Hartman #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
1246a97191SGreg Kroah-Hartman 
1346a97191SGreg Kroah-Hartman #include <linux/kernel.h>
1446a97191SGreg Kroah-Hartman #include <linux/mm.h>
1546a97191SGreg Kroah-Hartman #include <linux/hyperv.h>
16011a7c3cSK. Y. Srinivasan #include <linux/uio.h>
179988ce68SVitaly Kuznetsov #include <linux/vmalloc.h>
189988ce68SVitaly Kuznetsov #include <linux/slab.h>
198dd45f2aSStephen Hemminger #include <linux/prefetch.h>
209a879772STianyu Lan #include <linux/io.h>
219a879772STianyu Lan #include <asm/mshyperv.h>
2246a97191SGreg Kroah-Hartman 
2346a97191SGreg Kroah-Hartman #include "hyperv_vmbus.h"
2446a97191SGreg Kroah-Hartman 
25f3dd3f47Sstephen hemminger #define VMBUS_PKT_TRAILER	8
26f3dd3f47Sstephen hemminger 
2798fa8cf4SK. Y. Srinivasan /*
2898fa8cf4SK. Y. Srinivasan  * When we write to the ring buffer, check if the host needs to
2998fa8cf4SK. Y. Srinivasan  * be signaled. Here is the details of this protocol:
3098fa8cf4SK. Y. Srinivasan  *
3198fa8cf4SK. Y. Srinivasan  *	1. The host guarantees that while it is draining the
3298fa8cf4SK. Y. Srinivasan  *	   ring buffer, it will set the interrupt_mask to
3398fa8cf4SK. Y. Srinivasan  *	   indicate it does not need to be interrupted when
3498fa8cf4SK. Y. Srinivasan  *	   new data is placed.
3598fa8cf4SK. Y. Srinivasan  *
3698fa8cf4SK. Y. Srinivasan  *	2. The host guarantees that it will completely drain
3798fa8cf4SK. Y. Srinivasan  *	   the ring buffer before exiting the read loop. Further,
3898fa8cf4SK. Y. Srinivasan  *	   once the ring buffer is empty, it will clear the
3998fa8cf4SK. Y. Srinivasan  *	   interrupt_mask and re-check to see if new data has
4098fa8cf4SK. Y. Srinivasan  *	   arrived.
411f6ee4e7SK. Y. Srinivasan  *
421f6ee4e7SK. Y. Srinivasan  * KYS: Oct. 30, 2016:
431f6ee4e7SK. Y. Srinivasan  * It looks like Windows hosts have logic to deal with DOS attacks that
441f6ee4e7SK. Y. Srinivasan  * can be triggered if it receives interrupts when it is not expecting
451f6ee4e7SK. Y. Srinivasan  * the interrupt. The host expects interrupts only when the ring
461f6ee4e7SK. Y. Srinivasan  * transitions from empty to non-empty (or full to non full on the guest
471f6ee4e7SK. Y. Srinivasan  * to host ring).
481f6ee4e7SK. Y. Srinivasan  * So, base the signaling decision solely on the ring state until the
491f6ee4e7SK. Y. Srinivasan  * host logic is fixed.
5098fa8cf4SK. Y. Srinivasan  */
5198fa8cf4SK. Y. Srinivasan 
hv_signal_on_write(u32 old_write,struct vmbus_channel * channel)52b103a56fSStephen Hemminger static void hv_signal_on_write(u32 old_write, struct vmbus_channel *channel)
5398fa8cf4SK. Y. Srinivasan {
541f6ee4e7SK. Y. Srinivasan 	struct hv_ring_buffer_info *rbi = &channel->outbound;
551f6ee4e7SK. Y. Srinivasan 
56dcd0eecaSK. Y. Srinivasan 	virt_mb();
57d45faaeeSK. Y. Srinivasan 	if (READ_ONCE(rbi->ring_buffer->interrupt_mask))
581f6ee4e7SK. Y. Srinivasan 		return;
5998fa8cf4SK. Y. Srinivasan 
60e91e84faSJason Wang 	/* check interrupt_mask before read_index */
61dcd0eecaSK. Y. Srinivasan 	virt_rmb();
6298fa8cf4SK. Y. Srinivasan 	/*
6398fa8cf4SK. Y. Srinivasan 	 * This is the only case we need to signal when the
6498fa8cf4SK. Y. Srinivasan 	 * ring transitions from being empty to non-empty.
6598fa8cf4SK. Y. Srinivasan 	 */
66396ae57eSKimberly Brown 	if (old_write == READ_ONCE(rbi->ring_buffer->read_index)) {
67396ae57eSKimberly Brown 		++channel->intr_out_empty;
681f6ee4e7SK. Y. Srinivasan 		vmbus_setevent(channel);
6998fa8cf4SK. Y. Srinivasan 	}
70396ae57eSKimberly Brown }
7198fa8cf4SK. Y. Srinivasan 
72822f18d4SVitaly Kuznetsov /* Get the next write location for the specified ring buffer. */
7346a97191SGreg Kroah-Hartman static inline u32
hv_get_next_write_location(struct hv_ring_buffer_info * ring_info)7446a97191SGreg Kroah-Hartman hv_get_next_write_location(struct hv_ring_buffer_info *ring_info)
7546a97191SGreg Kroah-Hartman {
7646a97191SGreg Kroah-Hartman 	u32 next = ring_info->ring_buffer->write_index;
7746a97191SGreg Kroah-Hartman 
7846a97191SGreg Kroah-Hartman 	return next;
7946a97191SGreg Kroah-Hartman }
8046a97191SGreg Kroah-Hartman 
81822f18d4SVitaly Kuznetsov /* Set the next write location for the specified ring buffer. */
8246a97191SGreg Kroah-Hartman static inline void
hv_set_next_write_location(struct hv_ring_buffer_info * ring_info,u32 next_write_location)8346a97191SGreg Kroah-Hartman hv_set_next_write_location(struct hv_ring_buffer_info *ring_info,
8446a97191SGreg Kroah-Hartman 		     u32 next_write_location)
8546a97191SGreg Kroah-Hartman {
8646a97191SGreg Kroah-Hartman 	ring_info->ring_buffer->write_index = next_write_location;
8746a97191SGreg Kroah-Hartman }
8846a97191SGreg Kroah-Hartman 
89822f18d4SVitaly Kuznetsov /* Get the size of the ring buffer. */
9046a97191SGreg Kroah-Hartman static inline u32
hv_get_ring_buffersize(const struct hv_ring_buffer_info * ring_info)91e4165a0fSStephen Hemminger hv_get_ring_buffersize(const struct hv_ring_buffer_info *ring_info)
9246a97191SGreg Kroah-Hartman {
9346a97191SGreg Kroah-Hartman 	return ring_info->ring_datasize;
9446a97191SGreg Kroah-Hartman }
9546a97191SGreg Kroah-Hartman 
96822f18d4SVitaly Kuznetsov /* Get the read and write indices as u64 of the specified ring buffer. */
9746a97191SGreg Kroah-Hartman static inline u64
hv_get_ring_bufferindices(struct hv_ring_buffer_info * ring_info)9846a97191SGreg Kroah-Hartman hv_get_ring_bufferindices(struct hv_ring_buffer_info *ring_info)
9946a97191SGreg Kroah-Hartman {
10046a97191SGreg Kroah-Hartman 	return (u64)ring_info->ring_buffer->write_index << 32;
10146a97191SGreg Kroah-Hartman }
10246a97191SGreg Kroah-Hartman 
10346a97191SGreg Kroah-Hartman /*
10446a97191SGreg Kroah-Hartman  * Helper routine to copy from source to ring buffer.
10546a97191SGreg Kroah-Hartman  * Assume there is enough room. Handles wrap-around in dest case only!!
10646a97191SGreg Kroah-Hartman  */
hv_copyto_ringbuffer(struct hv_ring_buffer_info * ring_info,u32 start_write_offset,const void * src,u32 srclen)10746a97191SGreg Kroah-Hartman static u32 hv_copyto_ringbuffer(
10846a97191SGreg Kroah-Hartman 	struct hv_ring_buffer_info	*ring_info,
10946a97191SGreg Kroah-Hartman 	u32				start_write_offset,
110e4165a0fSStephen Hemminger 	const void			*src,
11146a97191SGreg Kroah-Hartman 	u32				srclen)
11246a97191SGreg Kroah-Hartman {
11346a97191SGreg Kroah-Hartman 	void *ring_buffer = hv_get_ring_buffer(ring_info);
11446a97191SGreg Kroah-Hartman 	u32 ring_buffer_size = hv_get_ring_buffersize(ring_info);
11546a97191SGreg Kroah-Hartman 
11646a97191SGreg Kroah-Hartman 	memcpy(ring_buffer + start_write_offset, src, srclen);
11746a97191SGreg Kroah-Hartman 
11846a97191SGreg Kroah-Hartman 	start_write_offset += srclen;
1198d12f882SStephen Hemminger 	if (start_write_offset >= ring_buffer_size)
1208d12f882SStephen Hemminger 		start_write_offset -= ring_buffer_size;
12146a97191SGreg Kroah-Hartman 
12246a97191SGreg Kroah-Hartman 	return start_write_offset;
12346a97191SGreg Kroah-Hartman }
12446a97191SGreg Kroah-Hartman 
1250487426fSStephen Hemminger /*
1260487426fSStephen Hemminger  *
1270487426fSStephen Hemminger  * hv_get_ringbuffer_availbytes()
1280487426fSStephen Hemminger  *
1290487426fSStephen Hemminger  * Get number of bytes available to read and to write to
1300487426fSStephen Hemminger  * for the specified ring buffer
1310487426fSStephen Hemminger  */
1320487426fSStephen Hemminger static void
hv_get_ringbuffer_availbytes(const struct hv_ring_buffer_info * rbi,u32 * read,u32 * write)1330487426fSStephen Hemminger hv_get_ringbuffer_availbytes(const struct hv_ring_buffer_info *rbi,
1340487426fSStephen Hemminger 			     u32 *read, u32 *write)
1350487426fSStephen Hemminger {
1360487426fSStephen Hemminger 	u32 read_loc, write_loc, dsize;
1370487426fSStephen Hemminger 
1380487426fSStephen Hemminger 	/* Capture the read/write indices before they changed */
1390487426fSStephen Hemminger 	read_loc = READ_ONCE(rbi->ring_buffer->read_index);
1400487426fSStephen Hemminger 	write_loc = READ_ONCE(rbi->ring_buffer->write_index);
1410487426fSStephen Hemminger 	dsize = rbi->ring_datasize;
1420487426fSStephen Hemminger 
1430487426fSStephen Hemminger 	*write = write_loc >= read_loc ? dsize - (write_loc - read_loc) :
1440487426fSStephen Hemminger 		read_loc - write_loc;
1450487426fSStephen Hemminger 	*read = dsize - *write;
1460487426fSStephen Hemminger }
1470487426fSStephen Hemminger 
148822f18d4SVitaly Kuznetsov /* 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)14914948e39SKimberly Brown int hv_ringbuffer_get_debuginfo(struct hv_ring_buffer_info *ring_info,
15046a97191SGreg Kroah-Hartman 				struct hv_ring_buffer_debug_info *debug_info)
15146a97191SGreg Kroah-Hartman {
15246a97191SGreg Kroah-Hartman 	u32 bytes_avail_towrite;
15346a97191SGreg Kroah-Hartman 	u32 bytes_avail_toread;
15446a97191SGreg Kroah-Hartman 
15514948e39SKimberly Brown 	mutex_lock(&ring_info->ring_buffer_mutex);
15614948e39SKimberly Brown 
15714948e39SKimberly Brown 	if (!ring_info->ring_buffer) {
15814948e39SKimberly Brown 		mutex_unlock(&ring_info->ring_buffer_mutex);
159ba50bf1cSDexuan Cui 		return -EINVAL;
16014948e39SKimberly Brown 	}
161ba50bf1cSDexuan Cui 
16246a97191SGreg Kroah-Hartman 	hv_get_ringbuffer_availbytes(ring_info,
16346a97191SGreg Kroah-Hartman 				     &bytes_avail_toread,
16446a97191SGreg Kroah-Hartman 				     &bytes_avail_towrite);
16546a97191SGreg Kroah-Hartman 	debug_info->bytes_avail_toread = bytes_avail_toread;
16646a97191SGreg Kroah-Hartman 	debug_info->bytes_avail_towrite = bytes_avail_towrite;
167ba50bf1cSDexuan Cui 	debug_info->current_read_index = ring_info->ring_buffer->read_index;
168ba50bf1cSDexuan Cui 	debug_info->current_write_index = ring_info->ring_buffer->write_index;
169ba50bf1cSDexuan Cui 	debug_info->current_interrupt_mask
170ba50bf1cSDexuan Cui 		= ring_info->ring_buffer->interrupt_mask;
17114948e39SKimberly Brown 	mutex_unlock(&ring_info->ring_buffer_mutex);
17214948e39SKimberly Brown 
173ba50bf1cSDexuan Cui 	return 0;
17446a97191SGreg Kroah-Hartman }
1754827ee1dSStephen Hemminger EXPORT_SYMBOL_GPL(hv_ringbuffer_get_debuginfo);
17646a97191SGreg Kroah-Hartman 
17714948e39SKimberly Brown /* Initialize a channel's ring buffer info mutex locks */
hv_ringbuffer_pre_init(struct vmbus_channel * channel)17814948e39SKimberly Brown void hv_ringbuffer_pre_init(struct vmbus_channel *channel)
17914948e39SKimberly Brown {
18014948e39SKimberly Brown 	mutex_init(&channel->inbound.ring_buffer_mutex);
18114948e39SKimberly Brown 	mutex_init(&channel->outbound.ring_buffer_mutex);
18214948e39SKimberly Brown }
18314948e39SKimberly Brown 
184822f18d4SVitaly Kuznetsov /* Initialize the ring buffer. */
hv_ringbuffer_init(struct hv_ring_buffer_info * ring_info,struct page * pages,u32 page_cnt,u32 max_pkt_size)18546a97191SGreg Kroah-Hartman int hv_ringbuffer_init(struct hv_ring_buffer_info *ring_info,
186adae1e93SAndres Beltran 		       struct page *pages, u32 page_cnt, u32 max_pkt_size)
18746a97191SGreg Kroah-Hartman {
1889988ce68SVitaly Kuznetsov 	struct page **pages_wraparound;
1899a879772STianyu Lan 	int i;
1909988ce68SVitaly Kuznetsov 
1919988ce68SVitaly Kuznetsov 	BUILD_BUG_ON((sizeof(struct hv_ring_buffer) != PAGE_SIZE));
19246a97191SGreg Kroah-Hartman 
1939988ce68SVitaly Kuznetsov 	/*
1949988ce68SVitaly Kuznetsov 	 * First page holds struct hv_ring_buffer, do wraparound mapping for
1959988ce68SVitaly Kuznetsov 	 * the rest.
1969988ce68SVitaly Kuznetsov 	 */
1979a879772STianyu Lan 	pages_wraparound = kcalloc(page_cnt * 2 - 1,
1989a879772STianyu Lan 				   sizeof(struct page *),
1999a879772STianyu Lan 				   GFP_KERNEL);
20001ccca3cSLv Ruyi 	if (!pages_wraparound)
20101ccca3cSLv Ruyi 		return -ENOMEM;
2029a879772STianyu Lan 
2039a879772STianyu Lan 	pages_wraparound[0] = pages;
2049a879772STianyu Lan 	for (i = 0; i < 2 * (page_cnt - 1); i++)
2059a879772STianyu Lan 		pages_wraparound[i + 1] =
2069a879772STianyu Lan 			&pages[i % (page_cnt - 1) + 1];
2079a879772STianyu Lan 
2089a879772STianyu Lan 	ring_info->ring_buffer = (struct hv_ring_buffer *)
2099a879772STianyu Lan 		vmap(pages_wraparound, page_cnt * 2 - 1, VM_MAP,
210*bb862397SMichael Kelley 			pgprot_decrypted(PAGE_KERNEL));
2119a879772STianyu Lan 
2129a879772STianyu Lan 	kfree(pages_wraparound);
2139a879772STianyu Lan 	if (!ring_info->ring_buffer)
2149a879772STianyu Lan 		return -ENOMEM;
2159a879772STianyu Lan 
216*bb862397SMichael Kelley 	/*
217*bb862397SMichael Kelley 	 * Ensure the header page is zero'ed since
218*bb862397SMichael Kelley 	 * encryption status may have changed.
219*bb862397SMichael Kelley 	 */
220*bb862397SMichael Kelley 	memset(ring_info->ring_buffer, 0, HV_HYP_PAGE_SIZE);
2219a879772STianyu Lan 
22246a97191SGreg Kroah-Hartman 	ring_info->ring_buffer->read_index =
22346a97191SGreg Kroah-Hartman 		ring_info->ring_buffer->write_index = 0;
22446a97191SGreg Kroah-Hartman 
225822f18d4SVitaly Kuznetsov 	/* Set the feature bit for enabling flow control. */
226046c7911SK. Y. Srinivasan 	ring_info->ring_buffer->feature_bits.value = 1;
227046c7911SK. Y. Srinivasan 
2289988ce68SVitaly Kuznetsov 	ring_info->ring_size = page_cnt << PAGE_SHIFT;
22963273cb4SLong Li 	ring_info->ring_size_div10_reciprocal =
23063273cb4SLong Li 		reciprocal_value(ring_info->ring_size / 10);
2319988ce68SVitaly Kuznetsov 	ring_info->ring_datasize = ring_info->ring_size -
2329988ce68SVitaly Kuznetsov 		sizeof(struct hv_ring_buffer);
2334713eb7bSKimberly Brown 	ring_info->priv_read_index = 0;
23446a97191SGreg Kroah-Hartman 
235adae1e93SAndres Beltran 	/* Initialize buffer that holds copies of incoming packets */
236adae1e93SAndres Beltran 	if (max_pkt_size) {
237adae1e93SAndres Beltran 		ring_info->pkt_buffer = kzalloc(max_pkt_size, GFP_KERNEL);
238adae1e93SAndres Beltran 		if (!ring_info->pkt_buffer)
239adae1e93SAndres Beltran 			return -ENOMEM;
240adae1e93SAndres Beltran 		ring_info->pkt_buffer_size = max_pkt_size;
241adae1e93SAndres Beltran 	}
242adae1e93SAndres Beltran 
24346a97191SGreg Kroah-Hartman 	spin_lock_init(&ring_info->ring_lock);
24446a97191SGreg Kroah-Hartman 
24546a97191SGreg Kroah-Hartman 	return 0;
24646a97191SGreg Kroah-Hartman }
24746a97191SGreg Kroah-Hartman 
248822f18d4SVitaly Kuznetsov /* Cleanup the ring buffer. */
hv_ringbuffer_cleanup(struct hv_ring_buffer_info * ring_info)24946a97191SGreg Kroah-Hartman void hv_ringbuffer_cleanup(struct hv_ring_buffer_info *ring_info)
25046a97191SGreg Kroah-Hartman {
25114948e39SKimberly Brown 	mutex_lock(&ring_info->ring_buffer_mutex);
2529988ce68SVitaly Kuznetsov 	vunmap(ring_info->ring_buffer);
253ae6935edSStephen Hemminger 	ring_info->ring_buffer = NULL;
25414948e39SKimberly Brown 	mutex_unlock(&ring_info->ring_buffer_mutex);
255adae1e93SAndres Beltran 
256adae1e93SAndres Beltran 	kfree(ring_info->pkt_buffer);
257f1940d4eSVitaly Kuznetsov 	ring_info->pkt_buffer = NULL;
258adae1e93SAndres Beltran 	ring_info->pkt_buffer_size = 0;
25946a97191SGreg Kroah-Hartman }
26046a97191SGreg Kroah-Hartman 
2611d044ca0SGuilherme G. Piccoli /*
2621d044ca0SGuilherme G. Piccoli  * Check if the ring buffer spinlock is available to take or not; used on
2631d044ca0SGuilherme G. Piccoli  * atomic contexts, like panic path (see the Hyper-V framebuffer driver).
2641d044ca0SGuilherme G. Piccoli  */
2651d044ca0SGuilherme G. Piccoli 
hv_ringbuffer_spinlock_busy(struct vmbus_channel * channel)2661d044ca0SGuilherme G. Piccoli bool hv_ringbuffer_spinlock_busy(struct vmbus_channel *channel)
2671d044ca0SGuilherme G. Piccoli {
2681d044ca0SGuilherme G. Piccoli 	struct hv_ring_buffer_info *rinfo = &channel->outbound;
2691d044ca0SGuilherme G. Piccoli 
2701d044ca0SGuilherme G. Piccoli 	return spin_is_locked(&rinfo->ring_lock);
2711d044ca0SGuilherme G. Piccoli }
2721d044ca0SGuilherme G. Piccoli EXPORT_SYMBOL_GPL(hv_ringbuffer_spinlock_busy);
2731d044ca0SGuilherme G. Piccoli 
274822f18d4SVitaly Kuznetsov /* Write to the ring buffer. */
hv_ringbuffer_write(struct vmbus_channel * channel,const struct kvec * kv_list,u32 kv_count,u64 requestid,u64 * trans_id)2751f6ee4e7SK. Y. Srinivasan int hv_ringbuffer_write(struct vmbus_channel *channel,
276e8b7db38SAndres Beltran 			const struct kvec *kv_list, u32 kv_count,
277b03afa57SAndrea Parri (Microsoft) 			u64 requestid, u64 *trans_id)
27846a97191SGreg Kroah-Hartman {
2792c616a8bSStephen Hemminger 	int i;
28046a97191SGreg Kroah-Hartman 	u32 bytes_avail_towrite;
2812c616a8bSStephen Hemminger 	u32 totalbytes_towrite = sizeof(u64);
28246a97191SGreg Kroah-Hartman 	u32 next_write_location;
28398fa8cf4SK. Y. Srinivasan 	u32 old_write;
2842c616a8bSStephen Hemminger 	u64 prev_indices;
2852c616a8bSStephen Hemminger 	unsigned long flags;
2861f6ee4e7SK. Y. Srinivasan 	struct hv_ring_buffer_info *outring_info = &channel->outbound;
287e8b7db38SAndres Beltran 	struct vmpacket_descriptor *desc = kv_list[0].iov_base;
288b03afa57SAndrea Parri (Microsoft) 	u64 __trans_id, rqst_id = VMBUS_NO_RQSTOR;
28946a97191SGreg Kroah-Hartman 
290e7e97dd8SK. Y. Srinivasan 	if (channel->rescind)
291e7e97dd8SK. Y. Srinivasan 		return -ENODEV;
292e7e97dd8SK. Y. Srinivasan 
293011a7c3cSK. Y. Srinivasan 	for (i = 0; i < kv_count; i++)
294011a7c3cSK. Y. Srinivasan 		totalbytes_towrite += kv_list[i].iov_len;
29546a97191SGreg Kroah-Hartman 
29646a97191SGreg Kroah-Hartman 	spin_lock_irqsave(&outring_info->ring_lock, flags);
29746a97191SGreg Kroah-Hartman 
298a6341f00SK. Y. Srinivasan 	bytes_avail_towrite = hv_get_bytes_to_write(outring_info);
29946a97191SGreg Kroah-Hartman 
300822f18d4SVitaly Kuznetsov 	/*
301822f18d4SVitaly Kuznetsov 	 * If there is only room for the packet, assume it is full.
302822f18d4SVitaly Kuznetsov 	 * Otherwise, the next time around, we think the ring buffer
303822f18d4SVitaly Kuznetsov 	 * is empty since the read index == write index.
304822f18d4SVitaly Kuznetsov 	 */
30546a97191SGreg Kroah-Hartman 	if (bytes_avail_towrite <= totalbytes_towrite) {
306396ae57eSKimberly Brown 		++channel->out_full_total;
307396ae57eSKimberly Brown 
308396ae57eSKimberly Brown 		if (!channel->out_full_flag) {
309396ae57eSKimberly Brown 			++channel->out_full_first;
310396ae57eSKimberly Brown 			channel->out_full_flag = true;
311396ae57eSKimberly Brown 		}
312396ae57eSKimberly Brown 
31346a97191SGreg Kroah-Hartman 		spin_unlock_irqrestore(&outring_info->ring_lock, flags);
31446a97191SGreg Kroah-Hartman 		return -EAGAIN;
31546a97191SGreg Kroah-Hartman 	}
31646a97191SGreg Kroah-Hartman 
317396ae57eSKimberly Brown 	channel->out_full_flag = false;
318396ae57eSKimberly Brown 
31946a97191SGreg Kroah-Hartman 	/* Write to the ring buffer */
32046a97191SGreg Kroah-Hartman 	next_write_location = hv_get_next_write_location(outring_info);
32146a97191SGreg Kroah-Hartman 
32298fa8cf4SK. Y. Srinivasan 	old_write = next_write_location;
32398fa8cf4SK. Y. Srinivasan 
324011a7c3cSK. Y. Srinivasan 	for (i = 0; i < kv_count; i++) {
32546a97191SGreg Kroah-Hartman 		next_write_location = hv_copyto_ringbuffer(outring_info,
32646a97191SGreg Kroah-Hartman 						     next_write_location,
327011a7c3cSK. Y. Srinivasan 						     kv_list[i].iov_base,
328011a7c3cSK. Y. Srinivasan 						     kv_list[i].iov_len);
32946a97191SGreg Kroah-Hartman 	}
33046a97191SGreg Kroah-Hartman 
331e8b7db38SAndres Beltran 	/*
332e8b7db38SAndres Beltran 	 * Allocate the request ID after the data has been copied into the
333e8b7db38SAndres Beltran 	 * ring buffer.  Once this request ID is allocated, the completion
334e8b7db38SAndres Beltran 	 * path could find the data and free it.
335e8b7db38SAndres Beltran 	 */
336e8b7db38SAndres Beltran 
337e8b7db38SAndres Beltran 	if (desc->flags == VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED) {
338bf5fd8caSAndrea Parri (Microsoft) 		if (channel->next_request_id_callback != NULL) {
339bf5fd8caSAndrea Parri (Microsoft) 			rqst_id = channel->next_request_id_callback(channel, requestid);
340e8b7db38SAndres Beltran 			if (rqst_id == VMBUS_RQST_ERROR) {
341e8b7db38SAndres Beltran 				spin_unlock_irqrestore(&outring_info->ring_lock, flags);
342e8b7db38SAndres Beltran 				return -EAGAIN;
343e8b7db38SAndres Beltran 			}
344e8b7db38SAndres Beltran 		}
345bf5fd8caSAndrea Parri (Microsoft) 	}
346e8b7db38SAndres Beltran 	desc = hv_get_ring_buffer(outring_info) + old_write;
347b03afa57SAndrea Parri (Microsoft) 	__trans_id = (rqst_id == VMBUS_NO_RQSTOR) ? requestid : rqst_id;
348b03afa57SAndrea Parri (Microsoft) 	/*
349b03afa57SAndrea Parri (Microsoft) 	 * Ensure the compiler doesn't generate code that reads the value of
350b03afa57SAndrea Parri (Microsoft) 	 * the transaction ID from the ring buffer, which is shared with the
351b03afa57SAndrea Parri (Microsoft) 	 * Hyper-V host and subject to being changed at any time.
352b03afa57SAndrea Parri (Microsoft) 	 */
353b03afa57SAndrea Parri (Microsoft) 	WRITE_ONCE(desc->trans_id, __trans_id);
354b03afa57SAndrea Parri (Microsoft) 	if (trans_id)
355b03afa57SAndrea Parri (Microsoft) 		*trans_id = __trans_id;
356e8b7db38SAndres Beltran 
35746a97191SGreg Kroah-Hartman 	/* Set previous packet start */
35846a97191SGreg Kroah-Hartman 	prev_indices = hv_get_ring_bufferindices(outring_info);
35946a97191SGreg Kroah-Hartman 
36046a97191SGreg Kroah-Hartman 	next_write_location = hv_copyto_ringbuffer(outring_info,
36146a97191SGreg Kroah-Hartman 					     next_write_location,
36246a97191SGreg Kroah-Hartman 					     &prev_indices,
36346a97191SGreg Kroah-Hartman 					     sizeof(u64));
36446a97191SGreg Kroah-Hartman 
36598fa8cf4SK. Y. Srinivasan 	/* Issue a full memory barrier before updating the write index */
366dcd0eecaSK. Y. Srinivasan 	virt_mb();
36746a97191SGreg Kroah-Hartman 
36846a97191SGreg Kroah-Hartman 	/* Now, update the write location */
36946a97191SGreg Kroah-Hartman 	hv_set_next_write_location(outring_info, next_write_location);
37046a97191SGreg Kroah-Hartman 
37146a97191SGreg Kroah-Hartman 
37246a97191SGreg Kroah-Hartman 	spin_unlock_irqrestore(&outring_info->ring_lock, flags);
37398fa8cf4SK. Y. Srinivasan 
374b103a56fSStephen Hemminger 	hv_signal_on_write(old_write, channel);
375e7e97dd8SK. Y. Srinivasan 
376e8b7db38SAndres Beltran 	if (channel->rescind) {
377e8b7db38SAndres Beltran 		if (rqst_id != VMBUS_NO_RQSTOR) {
378e8b7db38SAndres Beltran 			/* Reclaim request ID to avoid leak of IDs */
379bf5fd8caSAndrea Parri (Microsoft) 			if (channel->request_addr_callback != NULL)
380bf5fd8caSAndrea Parri (Microsoft) 				channel->request_addr_callback(channel, rqst_id);
381e8b7db38SAndres Beltran 		}
382e7e97dd8SK. Y. Srinivasan 		return -ENODEV;
383e8b7db38SAndres Beltran 	}
384e7e97dd8SK. Y. Srinivasan 
38546a97191SGreg Kroah-Hartman 	return 0;
38646a97191SGreg Kroah-Hartman }
38746a97191SGreg Kroah-Hartman 
hv_ringbuffer_read(struct vmbus_channel * channel,void * buffer,u32 buflen,u32 * buffer_actual_len,u64 * requestid,bool raw)3883372592aSK. Y. Srinivasan int hv_ringbuffer_read(struct vmbus_channel *channel,
389940b68e2SVitaly Kuznetsov 		       void *buffer, u32 buflen, u32 *buffer_actual_len,
3903372592aSK. Y. Srinivasan 		       u64 *requestid, bool raw)
39146a97191SGreg Kroah-Hartman {
3924226ff69SStephen Hemminger 	struct vmpacket_descriptor *desc;
3934226ff69SStephen Hemminger 	u32 packetlen, offset;
39446a97191SGreg Kroah-Hartman 
3954226ff69SStephen Hemminger 	if (unlikely(buflen == 0))
39646a97191SGreg Kroah-Hartman 		return -EINVAL;
39746a97191SGreg Kroah-Hartman 
398940b68e2SVitaly Kuznetsov 	*buffer_actual_len = 0;
399940b68e2SVitaly Kuznetsov 	*requestid = 0;
400940b68e2SVitaly Kuznetsov 
40146a97191SGreg Kroah-Hartman 	/* Make sure there is something to read */
4024226ff69SStephen Hemminger 	desc = hv_pkt_iter_first(channel);
4034226ff69SStephen Hemminger 	if (desc == NULL) {
404940b68e2SVitaly Kuznetsov 		/*
405940b68e2SVitaly Kuznetsov 		 * No error is set when there is even no header, drivers are
406940b68e2SVitaly Kuznetsov 		 * supposed to analyze buffer_actual_len.
407940b68e2SVitaly Kuznetsov 		 */
40842dd2715SStephen Hemminger 		return 0;
409940b68e2SVitaly Kuznetsov 	}
41046a97191SGreg Kroah-Hartman 
4114226ff69SStephen Hemminger 	offset = raw ? 0 : (desc->offset8 << 3);
4124226ff69SStephen Hemminger 	packetlen = (desc->len8 << 3) - offset;
413940b68e2SVitaly Kuznetsov 	*buffer_actual_len = packetlen;
4144226ff69SStephen Hemminger 	*requestid = desc->trans_id;
415940b68e2SVitaly Kuznetsov 
4164226ff69SStephen Hemminger 	if (unlikely(packetlen > buflen))
4173eba9a77SK. Y. Srinivasan 		return -ENOBUFS;
41846a97191SGreg Kroah-Hartman 
4194226ff69SStephen Hemminger 	/* since ring is double mapped, only one copy is necessary */
4204226ff69SStephen Hemminger 	memcpy(buffer, (const char *)desc + offset, packetlen);
42146a97191SGreg Kroah-Hartman 
4224226ff69SStephen Hemminger 	/* Advance ring index to next packet descriptor */
4231c9de08fSAndrea Parri (Microsoft) 	__hv_pkt_iter_next(channel, desc);
42446a97191SGreg Kroah-Hartman 
4254226ff69SStephen Hemminger 	/* Notify host of update */
4264226ff69SStephen Hemminger 	hv_pkt_iter_close(channel);
427c2b8e520SK. Y. Srinivasan 
42842dd2715SStephen Hemminger 	return 0;
429b5f53ddeSVitaly Kuznetsov }
430f3dd3f47Sstephen hemminger 
431f3dd3f47Sstephen hemminger /*
432f3dd3f47Sstephen hemminger  * Determine number of bytes available in ring buffer after
433f3dd3f47Sstephen hemminger  * the current iterator (priv_read_index) location.
434f3dd3f47Sstephen hemminger  *
435f3dd3f47Sstephen hemminger  * This is similar to hv_get_bytes_to_read but with private
436f3dd3f47Sstephen hemminger  * read index instead.
437f3dd3f47Sstephen hemminger  */
hv_pkt_iter_avail(const struct hv_ring_buffer_info * rbi)438f3dd3f47Sstephen hemminger static u32 hv_pkt_iter_avail(const struct hv_ring_buffer_info *rbi)
439f3dd3f47Sstephen hemminger {
440f3dd3f47Sstephen hemminger 	u32 priv_read_loc = rbi->priv_read_index;
441b6cae15bSMichael Kelley 	u32 write_loc;
442b6cae15bSMichael Kelley 
443b6cae15bSMichael Kelley 	/*
444b6cae15bSMichael Kelley 	 * The Hyper-V host writes the packet data, then uses
445b6cae15bSMichael Kelley 	 * store_release() to update the write_index.  Use load_acquire()
446b6cae15bSMichael Kelley 	 * here to prevent loads of the packet data from being re-ordered
447b6cae15bSMichael Kelley 	 * before the read of the write_index and potentially getting
448b6cae15bSMichael Kelley 	 * stale data.
449b6cae15bSMichael Kelley 	 */
450b6cae15bSMichael Kelley 	write_loc = virt_load_acquire(&rbi->ring_buffer->write_index);
451f3dd3f47Sstephen hemminger 
452f3dd3f47Sstephen hemminger 	if (write_loc >= priv_read_loc)
453f3dd3f47Sstephen hemminger 		return write_loc - priv_read_loc;
454f3dd3f47Sstephen hemminger 	else
455f3dd3f47Sstephen hemminger 		return (rbi->ring_datasize - priv_read_loc) + write_loc;
456f3dd3f47Sstephen hemminger }
457f3dd3f47Sstephen hemminger 
458f3dd3f47Sstephen hemminger /*
459f3dd3f47Sstephen hemminger  * Get first vmbus packet from ring buffer after read_index
460f3dd3f47Sstephen hemminger  *
461f3dd3f47Sstephen hemminger  * If ring buffer is empty, returns NULL and no other action needed.
462f3dd3f47Sstephen hemminger  */
hv_pkt_iter_first(struct vmbus_channel * channel)463f3dd3f47Sstephen hemminger struct vmpacket_descriptor *hv_pkt_iter_first(struct vmbus_channel *channel)
464f3dd3f47Sstephen hemminger {
465f3dd3f47Sstephen hemminger 	struct hv_ring_buffer_info *rbi = &channel->inbound;
466adae1e93SAndres Beltran 	struct vmpacket_descriptor *desc, *desc_copy;
467adae1e93SAndres Beltran 	u32 bytes_avail, pkt_len, pkt_offset;
468f3dd3f47Sstephen hemminger 
4691c9de08fSAndrea Parri (Microsoft) 	hv_debug_delay_test(channel, MESSAGE_DELAY);
470f3dd3f47Sstephen hemminger 
4711c9de08fSAndrea Parri (Microsoft) 	bytes_avail = hv_pkt_iter_avail(rbi);
4721c9de08fSAndrea Parri (Microsoft) 	if (bytes_avail < sizeof(struct vmpacket_descriptor))
4731c9de08fSAndrea Parri (Microsoft) 		return NULL;
4741c9de08fSAndrea Parri (Microsoft) 	bytes_avail = min(rbi->pkt_buffer_size, bytes_avail);
4751c9de08fSAndrea Parri (Microsoft) 
4761c9de08fSAndrea Parri (Microsoft) 	desc = (struct vmpacket_descriptor *)(hv_get_ring_buffer(rbi) + rbi->priv_read_index);
47715e1674dSStephen Hemminger 
478adae1e93SAndres Beltran 	/*
479adae1e93SAndres Beltran 	 * Ensure the compiler does not use references to incoming Hyper-V values (which
480adae1e93SAndres Beltran 	 * could change at any moment) when reading local variables later in the code
481adae1e93SAndres Beltran 	 */
482adae1e93SAndres Beltran 	pkt_len = READ_ONCE(desc->len8) << 3;
483adae1e93SAndres Beltran 	pkt_offset = READ_ONCE(desc->offset8) << 3;
484adae1e93SAndres Beltran 
485adae1e93SAndres Beltran 	/*
486adae1e93SAndres Beltran 	 * If pkt_len is invalid, set it to the smaller of hv_pkt_iter_avail() and
487adae1e93SAndres Beltran 	 * rbi->pkt_buffer_size
488adae1e93SAndres Beltran 	 */
489adae1e93SAndres Beltran 	if (pkt_len < sizeof(struct vmpacket_descriptor) || pkt_len > bytes_avail)
490adae1e93SAndres Beltran 		pkt_len = bytes_avail;
491adae1e93SAndres Beltran 
492adae1e93SAndres Beltran 	/*
493adae1e93SAndres Beltran 	 * If pkt_offset is invalid, arbitrarily set it to
494adae1e93SAndres Beltran 	 * the size of vmpacket_descriptor
495adae1e93SAndres Beltran 	 */
496adae1e93SAndres Beltran 	if (pkt_offset < sizeof(struct vmpacket_descriptor) || pkt_offset > pkt_len)
497adae1e93SAndres Beltran 		pkt_offset = sizeof(struct vmpacket_descriptor);
498adae1e93SAndres Beltran 
499adae1e93SAndres Beltran 	/* Copy the Hyper-V packet out of the ring buffer */
500adae1e93SAndres Beltran 	desc_copy = (struct vmpacket_descriptor *)rbi->pkt_buffer;
501adae1e93SAndres Beltran 	memcpy(desc_copy, desc, pkt_len);
502adae1e93SAndres Beltran 
503adae1e93SAndres Beltran 	/*
504adae1e93SAndres Beltran 	 * Hyper-V could still change len8 and offset8 after the earlier read.
505adae1e93SAndres Beltran 	 * Ensure that desc_copy has legal values for len8 and offset8 that
506adae1e93SAndres Beltran 	 * are consistent with the copy we just made
507adae1e93SAndres Beltran 	 */
508adae1e93SAndres Beltran 	desc_copy->len8 = pkt_len >> 3;
509adae1e93SAndres Beltran 	desc_copy->offset8 = pkt_offset >> 3;
510adae1e93SAndres Beltran 
511adae1e93SAndres Beltran 	return desc_copy;
512f3dd3f47Sstephen hemminger }
513f3dd3f47Sstephen hemminger EXPORT_SYMBOL_GPL(hv_pkt_iter_first);
514f3dd3f47Sstephen hemminger 
515f3dd3f47Sstephen hemminger /*
516f3dd3f47Sstephen hemminger  * Get next vmbus packet from ring buffer.
517f3dd3f47Sstephen hemminger  *
518f3dd3f47Sstephen hemminger  * Advances the current location (priv_read_index) and checks for more
519f3dd3f47Sstephen hemminger  * data. If the end of the ring buffer is reached, then return NULL.
520f3dd3f47Sstephen hemminger  */
521f3dd3f47Sstephen hemminger struct vmpacket_descriptor *
__hv_pkt_iter_next(struct vmbus_channel * channel,const struct vmpacket_descriptor * desc)522f3dd3f47Sstephen hemminger __hv_pkt_iter_next(struct vmbus_channel *channel,
5231c9de08fSAndrea Parri (Microsoft) 		   const struct vmpacket_descriptor *desc)
524f3dd3f47Sstephen hemminger {
525f3dd3f47Sstephen hemminger 	struct hv_ring_buffer_info *rbi = &channel->inbound;
526f3dd3f47Sstephen hemminger 	u32 packetlen = desc->len8 << 3;
527f3dd3f47Sstephen hemminger 	u32 dsize = rbi->ring_datasize;
528f3dd3f47Sstephen hemminger 
529af9ca6f9SBranden Bonaby 	hv_debug_delay_test(channel, MESSAGE_DELAY);
530f3dd3f47Sstephen hemminger 	/* bump offset to next potential packet */
531f3dd3f47Sstephen hemminger 	rbi->priv_read_index += packetlen + VMBUS_PKT_TRAILER;
532f3dd3f47Sstephen hemminger 	if (rbi->priv_read_index >= dsize)
533f3dd3f47Sstephen hemminger 		rbi->priv_read_index -= dsize;
534f3dd3f47Sstephen hemminger 
535f3dd3f47Sstephen hemminger 	/* more data? */
5361c9de08fSAndrea Parri (Microsoft) 	return hv_pkt_iter_first(channel);
537f3dd3f47Sstephen hemminger }
538f3dd3f47Sstephen hemminger EXPORT_SYMBOL_GPL(__hv_pkt_iter_next);
539f3dd3f47Sstephen hemminger 
540655296c8SMichael Kelley /* 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)541655296c8SMichael Kelley static u32 hv_pkt_iter_bytes_read(const struct hv_ring_buffer_info *rbi,
542655296c8SMichael Kelley 					u32 start_read_index)
543655296c8SMichael Kelley {
544655296c8SMichael Kelley 	if (rbi->priv_read_index >= start_read_index)
545655296c8SMichael Kelley 		return rbi->priv_read_index - start_read_index;
546655296c8SMichael Kelley 	else
547655296c8SMichael Kelley 		return rbi->ring_datasize - start_read_index +
548655296c8SMichael Kelley 			rbi->priv_read_index;
549655296c8SMichael Kelley }
550655296c8SMichael Kelley 
551f3dd3f47Sstephen hemminger /*
55271b38245SMichael Kelley  * Update host ring buffer after iterating over packets. If the host has
55371b38245SMichael Kelley  * stopped queuing new entries because it found the ring buffer full, and
55471b38245SMichael Kelley  * sufficient space is being freed up, signal the host. But be careful to
55571b38245SMichael Kelley  * only signal the host when necessary, both for performance reasons and
55671b38245SMichael Kelley  * because Hyper-V protects itself by throttling guests that signal
55771b38245SMichael Kelley  * inappropriately.
55871b38245SMichael Kelley  *
55971b38245SMichael Kelley  * Determining when to signal is tricky. There are three key data inputs
56071b38245SMichael Kelley  * that must be handled in this order to avoid race conditions:
56171b38245SMichael Kelley  *
56271b38245SMichael Kelley  * 1. Update the read_index
56371b38245SMichael Kelley  * 2. Read the pending_send_sz
56471b38245SMichael Kelley  * 3. Read the current write_index
56571b38245SMichael Kelley  *
56671b38245SMichael Kelley  * The interrupt_mask is not used to determine when to signal. The
56771b38245SMichael Kelley  * interrupt_mask is used only on the guest->host ring buffer when
56871b38245SMichael Kelley  * sending requests to the host. The host does not use it on the host->
56971b38245SMichael Kelley  * guest ring buffer to indicate whether it should be signaled.
570f3dd3f47Sstephen hemminger  */
hv_pkt_iter_close(struct vmbus_channel * channel)571f3dd3f47Sstephen hemminger void hv_pkt_iter_close(struct vmbus_channel *channel)
572f3dd3f47Sstephen hemminger {
573f3dd3f47Sstephen hemminger 	struct hv_ring_buffer_info *rbi = &channel->inbound;
574655296c8SMichael Kelley 	u32 curr_write_sz, pending_sz, bytes_read, start_read_index;
575f3dd3f47Sstephen hemminger 
576f3dd3f47Sstephen hemminger 	/*
577f3dd3f47Sstephen hemminger 	 * Make sure all reads are done before we update the read index since
578f3dd3f47Sstephen hemminger 	 * the writer may start writing to the read area once the read index
579f3dd3f47Sstephen hemminger 	 * is updated.
580f3dd3f47Sstephen hemminger 	 */
581f3dd3f47Sstephen hemminger 	virt_rmb();
582655296c8SMichael Kelley 	start_read_index = rbi->ring_buffer->read_index;
583f3dd3f47Sstephen hemminger 	rbi->ring_buffer->read_index = rbi->priv_read_index;
584f3dd3f47Sstephen hemminger 
58571b38245SMichael Kelley 	/*
58671b38245SMichael Kelley 	 * Older versions of Hyper-V (before WS2102 and Win8) do not
58771b38245SMichael Kelley 	 * implement pending_send_sz and simply poll if the host->guest
58871b38245SMichael Kelley 	 * ring buffer is full.  No signaling is needed or expected.
58971b38245SMichael Kelley 	 */
590655296c8SMichael Kelley 	if (!rbi->ring_buffer->feature_bits.feat_pending_send_sz)
591655296c8SMichael Kelley 		return;
592655296c8SMichael Kelley 
5938dd45f2aSStephen Hemminger 	/*
5948dd45f2aSStephen Hemminger 	 * Issue a full memory barrier before making the signaling decision.
59571b38245SMichael Kelley 	 * If reading pending_send_sz were to be reordered and happen
59671b38245SMichael Kelley 	 * before we commit the new read_index, a race could occur.  If the
59771b38245SMichael Kelley 	 * host were to set the pending_send_sz after we have sampled
59871b38245SMichael Kelley 	 * pending_send_sz, and the ring buffer blocks before we commit the
5998dd45f2aSStephen Hemminger 	 * read index, we could miss sending the interrupt. Issue a full
6008dd45f2aSStephen Hemminger 	 * memory barrier to address this.
6018dd45f2aSStephen Hemminger 	 */
6028dd45f2aSStephen Hemminger 	virt_mb();
6038dd45f2aSStephen Hemminger 
60471b38245SMichael Kelley 	/*
60571b38245SMichael Kelley 	 * If the pending_send_sz is zero, then the ring buffer is not
60671b38245SMichael Kelley 	 * blocked and there is no need to signal.  This is far by the
60771b38245SMichael Kelley 	 * most common case, so exit quickly for best performance.
60871b38245SMichael Kelley 	 */
609655296c8SMichael Kelley 	pending_sz = READ_ONCE(rbi->ring_buffer->pending_send_sz);
610655296c8SMichael Kelley 	if (!pending_sz)
6118dd45f2aSStephen Hemminger 		return;
6128dd45f2aSStephen Hemminger 
613655296c8SMichael Kelley 	/*
614655296c8SMichael Kelley 	 * Ensure the read of write_index in hv_get_bytes_to_write()
615655296c8SMichael Kelley 	 * happens after the read of pending_send_sz.
616655296c8SMichael Kelley 	 */
617655296c8SMichael Kelley 	virt_rmb();
618655296c8SMichael Kelley 	curr_write_sz = hv_get_bytes_to_write(rbi);
619655296c8SMichael Kelley 	bytes_read = hv_pkt_iter_bytes_read(rbi, start_read_index);
62003bad714SStephen Hemminger 
62103bad714SStephen Hemminger 	/*
62271b38245SMichael Kelley 	 * We want to signal the host only if we're transitioning
62371b38245SMichael Kelley 	 * from a "not enough free space" state to a "enough free
62471b38245SMichael Kelley 	 * space" state.  For example, it's possible that this function
62571b38245SMichael Kelley 	 * could run and free up enough space to signal the host, and then
62671b38245SMichael Kelley 	 * run again and free up additional space before the host has a
62771b38245SMichael Kelley 	 * chance to clear the pending_send_sz.  The 2nd invocation would
62871b38245SMichael Kelley 	 * be a null transition from "enough free space" to "enough free
62971b38245SMichael Kelley 	 * space", which doesn't warrant a signal.
63071b38245SMichael Kelley 	 *
63171b38245SMichael Kelley 	 * Exactly filling the ring buffer is treated as "not enough
63271b38245SMichael Kelley 	 * space". The ring buffer always must have at least one byte
63371b38245SMichael Kelley 	 * empty so the empty and full conditions are distinguishable.
63471b38245SMichael Kelley 	 * hv_get_bytes_to_write() doesn't fully tell the truth in
63571b38245SMichael Kelley 	 * this regard.
63671b38245SMichael Kelley 	 *
63771b38245SMichael Kelley 	 * So first check if we were in the "enough free space" state
63871b38245SMichael Kelley 	 * before we began the iteration. If so, the host was not
63971b38245SMichael Kelley 	 * blocked, and there's no need to signal.
64003bad714SStephen Hemminger 	 */
641655296c8SMichael Kelley 	if (curr_write_sz - bytes_read > pending_sz)
64203bad714SStephen Hemminger 		return;
64303bad714SStephen Hemminger 
64471b38245SMichael Kelley 	/*
64571b38245SMichael Kelley 	 * Similarly, if the new state is "not enough space", then
64671b38245SMichael Kelley 	 * there's no need to signal.
64771b38245SMichael Kelley 	 */
648655296c8SMichael Kelley 	if (curr_write_sz <= pending_sz)
6498dd45f2aSStephen Hemminger 		return;
6508dd45f2aSStephen Hemminger 
651396ae57eSKimberly Brown 	++channel->intr_in_full;
6528dd45f2aSStephen Hemminger 	vmbus_setevent(channel);
653f3dd3f47Sstephen hemminger }
654f3dd3f47Sstephen hemminger EXPORT_SYMBOL_GPL(hv_pkt_iter_close);
655