Index: head/sys/dev/hyperv/include/hyperv.h
===================================================================
--- head/sys/dev/hyperv/include/hyperv.h	(revision 302708)
+++ head/sys/dev/hyperv/include/hyperv.h	(revision 302709)
@@ -1,719 +1,720 @@
 /*-
  * Copyright (c) 2009-2012,2016 Microsoft Corp.
  * Copyright (c) 2012 NetApp Inc.
  * Copyright (c) 2012 Citrix Inc.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
  * are met:
  * 1. Redistributions of source code must retain the above copyright
  *    notice unmodified, this list of conditions, and the following
  *    disclaimer.
  * 2. Redistributions in binary form must reproduce the above copyright
  *    notice, this list of conditions and the following disclaimer in the
  *    documentation and/or other materials provided with the distribution.
  *
  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *
  * $FreeBSD$
  */
 
 /**
  * HyperV definitions for messages that are sent between instances of the
  * Channel Management Library in separate partitions, or in some cases,
  * back to itself.
  */
 
 #ifndef __HYPERV_H__
 #define __HYPERV_H__
 
 #include <sys/param.h>
 #include <sys/mbuf.h>
 #include <sys/queue.h>
 #include <sys/malloc.h>
 #include <sys/kthread.h>
 #include <sys/taskqueue.h>
 #include <sys/systm.h>
 #include <sys/lock.h>
 #include <sys/sema.h>
 #include <sys/smp.h>
 #include <sys/mutex.h>
 #include <sys/bus.h>
 #include <sys/sysctl.h>
 #include <vm/vm.h>
 #include <vm/vm_param.h>
 #include <vm/pmap.h>
 
 #include <amd64/include/xen/synch_bitops.h>
 #include <amd64/include/atomic.h>
 #include <dev/hyperv/include/hyperv_busdma.h>
 
 typedef uint8_t	hv_bool_uint8_t;
 
 #define HV_S_OK			0x00000000
 #define HV_E_FAIL		0x80004005
 #define HV_ERROR_NOT_SUPPORTED	0x80070032
 #define HV_ERROR_MACHINE_LOCKED	0x800704F7
 
 /*
  * VMBUS version is 32 bit, upper 16 bit for major_number and lower
  * 16 bit for minor_number.
  *
  * 0.13  --  Windows Server 2008
  * 1.1   --  Windows 7
  * 2.4   --  Windows 8
  * 3.0   --  Windows 8.1
  */
 #define VMBUS_VERSION_WS2008		((0 << 16) | (13))
 #define VMBUS_VERSION_WIN7		((1 << 16) | (1))
 #define VMBUS_VERSION_WIN8		((2 << 16) | (4))
 #define VMBUS_VERSION_WIN8_1		((3 << 16) | (0))
 
 #define VMBUS_VERSION_MAJOR(ver)	(((uint32_t)(ver)) >> 16)
 #define VMBUS_VERSION_MINOR(ver)	(((uint32_t)(ver)) & 0xffff)
 
 /*
  * Make maximum size of pipe payload of 16K
  */
 
 #define HV_MAX_PIPE_DATA_PAYLOAD	(sizeof(BYTE) * 16384)
 
 /*
  * Define pipe_mode values
  */
 
 #define HV_VMBUS_PIPE_TYPE_BYTE		0x00000000
 #define HV_VMBUS_PIPE_TYPE_MESSAGE	0x00000004
 
 /*
  * The size of the user defined data buffer for non-pipe offers
  */
 
 #define HV_MAX_USER_DEFINED_BYTES	120
 
 /*
  *  The size of the user defined data buffer for pipe offers
  */
 
 #define HV_MAX_PIPE_USER_DEFINED_BYTES	116
 
 
 #define HV_MAX_PAGE_BUFFER_COUNT	32
 #define HV_MAX_MULTIPAGE_BUFFER_COUNT	32
 
 #define HV_ALIGN_UP(value, align)					\
 		(((value) & (align-1)) ?				\
 		    (((value) + (align-1)) & ~(align-1) ) : (value))
 
 #define HV_ALIGN_DOWN(value, align) ( (value) & ~(align-1) )
 
 #define HV_NUM_PAGES_SPANNED(addr, len)					\
 		((HV_ALIGN_UP(addr+len, PAGE_SIZE) -			\
 		    HV_ALIGN_DOWN(addr, PAGE_SIZE)) >> PAGE_SHIFT )
 
 typedef struct hv_guid {
 	uint8_t data[16];
 } __packed hv_guid;
 
 #define HYPERV_GUID_STRLEN	40
 
 int	hyperv_guid2str(const struct hv_guid *, char *, size_t);
 
 #define HV_NIC_GUID							\
 	.data = {0x63, 0x51, 0x61, 0xF8, 0x3E, 0xDF, 0xc5, 0x46,	\
 		0x91, 0x3F, 0xF2, 0xD2, 0xF9, 0x65, 0xED, 0x0E}
 
 #define HV_IDE_GUID							\
 	.data = {0x32, 0x26, 0x41, 0x32, 0xcb, 0x86, 0xa2, 0x44,	\
 		 0x9b, 0x5c, 0x50, 0xd1, 0x41, 0x73, 0x54, 0xf5}
 
 #define HV_SCSI_GUID							\
 	.data = {0xd9, 0x63, 0x61, 0xba, 0xa1, 0x04, 0x29, 0x4d,	\
 		 0xb6, 0x05, 0x72, 0xe2, 0xff, 0xb1, 0xdc, 0x7f}
 
 /*
  * At the center of the Channel Management library is
  * the Channel Offer. This struct contains the
  * fundamental information about an offer.
  */
 
 typedef struct hv_vmbus_channel_offer {
 	hv_guid		interface_type;
 	hv_guid		interface_instance;
 	uint64_t	interrupt_latency_in_100ns_units;
 	uint32_t	interface_revision;
 	uint32_t	server_context_area_size; /* in bytes */
 	uint16_t	channel_flags;
 	uint16_t	mmio_megabytes;		  /* in bytes * 1024 * 1024 */
 	union
 	{
         /*
          * Non-pipes: The user has HV_MAX_USER_DEFINED_BYTES bytes.
          */
 		struct {
 			uint8_t	user_defined[HV_MAX_USER_DEFINED_BYTES];
 		} __packed standard;
 
         /*
          * Pipes: The following structure is an integrated pipe protocol, which
          *        is implemented on top of standard user-defined data. pipe
          *        clients  have HV_MAX_PIPE_USER_DEFINED_BYTES left for their
          *        own use.
          */
 		struct {
 			uint32_t	pipe_mode;
 			uint8_t	user_defined[HV_MAX_PIPE_USER_DEFINED_BYTES];
 		} __packed pipe;
 	} u;
 
 	/*
 	 * Sub_channel_index, newly added in Win8.
 	 */
 	uint16_t	sub_channel_index;
 	uint16_t	padding;
 
 } __packed hv_vmbus_channel_offer;
 
 typedef struct {
 	uint16_t type;
 	uint16_t data_offset8;
 	uint16_t length8;
 	uint16_t flags;
 	uint64_t transaction_id;
 } __packed hv_vm_packet_descriptor;
 
 typedef uint32_t hv_previous_packet_offset;
 
 typedef struct {
 	hv_previous_packet_offset	previous_packet_start_offset;
 	hv_vm_packet_descriptor		descriptor;
 } __packed hv_vm_packet_header;
 
 typedef struct {
 	uint32_t byte_count;
 	uint32_t byte_offset;
 } __packed hv_vm_transfer_page;
 
 typedef struct {
 	hv_vm_packet_descriptor	d;
 	uint16_t		transfer_page_set_id;
 	hv_bool_uint8_t		sender_owns_set;
 	uint8_t			reserved;
 	uint32_t		range_count;
 	hv_vm_transfer_page	ranges[1];
 } __packed hv_vm_transfer_page_packet_header;
 
 typedef struct {
 	hv_vm_packet_descriptor	d;
 	uint32_t		gpadl;
 	uint32_t		reserved;
 } __packed hv_vm_gpadl_packet_header;
 
 typedef struct {
 	hv_vm_packet_descriptor	d;
 	uint32_t		gpadl;
 	uint16_t		transfer_page_set_id;
 	uint16_t		reserved;
 } __packed hv_vm_add_remove_transfer_page_set;
 
 /*
  * This structure defines a range in guest
  * physical space that can be made
  * to look virtually contiguous.
  */
 
 typedef struct {
 	uint32_t byte_count;
 	uint32_t byte_offset;
 	uint64_t pfn_array[0];
 } __packed hv_gpa_range;
 
 /*
  * This is the format for an Establish Gpadl packet, which contains a handle
  * by which this GPADL will be known and a set of GPA ranges associated with
  * it.  This can be converted to a MDL by the guest OS.  If there are multiple
  * GPA ranges, then the resulting MDL will be "chained," representing multiple
  * VA ranges.
  */
 
 typedef struct {
 	hv_vm_packet_descriptor	d;
 	uint32_t		gpadl;
 	uint32_t		range_count;
 	hv_gpa_range		range[1];
 } __packed hv_vm_establish_gpadl;
 
 /*
  * This is the format for a Teardown Gpadl packet, which indicates that the
  * GPADL handle in the Establish Gpadl packet will never be referenced again.
  */
 
 typedef struct {
 	hv_vm_packet_descriptor	d;
 	uint32_t		gpadl;
 				/* for alignment to a 8-byte boundary */
 	uint32_t		reserved;
 } __packed hv_vm_teardown_gpadl;
 
 /*
  * This is the format for a GPA-Direct packet, which contains a set of GPA
  * ranges, in addition to commands and/or data.
  */
 
 typedef struct {
 	hv_vm_packet_descriptor	d;
 	uint32_t		reserved;
 	uint32_t		range_count;
 	hv_gpa_range		range[1];
 } __packed hv_vm_data_gpa_direct;
 
 /*
  * This is the format for a Additional data Packet.
  */
 typedef struct {
 	hv_vm_packet_descriptor	d;
 	uint64_t		total_bytes;
 	uint32_t		byte_offset;
 	uint32_t		byte_count;
 	uint8_t			data[1];
 } __packed hv_vm_additional_data;
 
 typedef union {
 	hv_vm_packet_descriptor             simple_header;
 	hv_vm_transfer_page_packet_header   transfer_page_header;
 	hv_vm_gpadl_packet_header           gpadl_header;
 	hv_vm_add_remove_transfer_page_set  add_remove_transfer_page_header;
 	hv_vm_establish_gpadl               establish_gpadl_header;
 	hv_vm_teardown_gpadl                teardown_gpadl_header;
 	hv_vm_data_gpa_direct               data_gpa_direct_header;
 } __packed hv_vm_packet_largest_possible_header;
 
 typedef enum {
 	HV_VMBUS_PACKET_TYPE_INVALID				= 0x0,
 	HV_VMBUS_PACKET_TYPES_SYNCH				= 0x1,
 	HV_VMBUS_PACKET_TYPE_ADD_TRANSFER_PAGE_SET		= 0x2,
 	HV_VMBUS_PACKET_TYPE_REMOVE_TRANSFER_PAGE_SET		= 0x3,
 	HV_VMBUS_PACKET_TYPE_ESTABLISH_GPADL			= 0x4,
 	HV_VMBUS_PACKET_TYPE_TEAR_DOWN_GPADL			= 0x5,
 	HV_VMBUS_PACKET_TYPE_DATA_IN_BAND			= 0x6,
 	HV_VMBUS_PACKET_TYPE_DATA_USING_TRANSFER_PAGES		= 0x7,
 	HV_VMBUS_PACKET_TYPE_DATA_USING_GPADL			= 0x8,
 	HV_VMBUS_PACKET_TYPE_DATA_USING_GPA_DIRECT		= 0x9,
 	HV_VMBUS_PACKET_TYPE_CANCEL_REQUEST			= 0xa,
 	HV_VMBUS_PACKET_TYPE_COMPLETION				= 0xb,
 	HV_VMBUS_PACKET_TYPE_DATA_USING_ADDITIONAL_PACKETS	= 0xc,
 	HV_VMBUS_PACKET_TYPE_ADDITIONAL_DATA = 0xd
 } hv_vmbus_packet_type;
 
 #define HV_VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED    1
 
 /*
  * Version 1 messages
  */
 typedef enum {
 	HV_CHANNEL_MESSAGE_INVALID			= 0,
 	HV_CHANNEL_MESSAGE_OFFER_CHANNEL		= 1,
 	HV_CHANNEL_MESSAGE_RESCIND_CHANNEL_OFFER	= 2,
 	HV_CHANNEL_MESSAGE_REQUEST_OFFERS		= 3,
 	HV_CHANNEL_MESSAGE_ALL_OFFERS_DELIVERED		= 4,
 	HV_CHANNEL_MESSAGE_OPEN_CHANNEL			= 5,
 	HV_CHANNEL_MESSAGE_OPEN_CHANNEL_RESULT		= 6,
 	HV_CHANNEL_MESSAGE_CLOSE_CHANNEL		= 7,
 	HV_CHANNEL_MESSAGEL_GPADL_HEADER		= 8,
 	HV_CHANNEL_MESSAGE_GPADL_BODY			= 9,
 	HV_CHANNEL_MESSAGE_GPADL_CREATED		= 10,
 	HV_CHANNEL_MESSAGE_GPADL_TEARDOWN		= 11,
 	HV_CHANNEL_MESSAGE_GPADL_TORNDOWN		= 12,
 	HV_CHANNEL_MESSAGE_REL_ID_RELEASED		= 13,
 	HV_CHANNEL_MESSAGE_INITIATED_CONTACT		= 14,
 	HV_CHANNEL_MESSAGE_VERSION_RESPONSE		= 15,
 	HV_CHANNEL_MESSAGE_UNLOAD			= 16,
 	HV_CHANNEL_MESSAGE_COUNT
 } hv_vmbus_channel_msg_type;
 
 typedef struct {
 	hv_vmbus_channel_msg_type	message_type;
 	uint32_t			padding;
 } __packed hv_vmbus_channel_msg_header;
 
 /*
  * Query VMBus Version parameters
  */
 typedef struct {
 	hv_vmbus_channel_msg_header	header;
 	uint32_t			version;
 } __packed hv_vmbus_channel_query_vmbus_version;
 
 /*
  * Channel Offer parameters
  */
 typedef struct {
 	hv_vmbus_channel_msg_header	header;
 	hv_vmbus_channel_offer		offer;
 	uint32_t			child_rel_id;
 	uint8_t				monitor_id;
 	/*
 	 * This field has been split into a bit field on Win7
 	 * and higher.
 	 */
 	uint8_t				monitor_allocated:1;
 	uint8_t				reserved:7;
 	/*
 	 * Following fields were added in win7 and higher.
 	 * Make sure to check the version before accessing these fields.
 	 *
 	 * If "is_dedicated_interrupt" is set, we must not set the
 	 * associated bit in the channel bitmap while sending the
 	 * interrupt to the host.
 	 *
 	 * connection_id is used in signaling the host.
 	 */
 	uint16_t			is_dedicated_interrupt:1;
 	uint16_t			reserved1:15;
 	uint32_t			connection_id;
 } __packed hv_vmbus_channel_offer_channel;
 
 /*
  * Rescind Offer parameters
  */
 typedef struct
 {
     hv_vmbus_channel_msg_header	header;
     uint32_t			child_rel_id;
 } __packed hv_vmbus_channel_rescind_offer;
 
 typedef struct {
 	hv_vmbus_channel_msg_header	header;
 	uint32_t			child_rel_id;
 } __packed hv_vmbus_channel_relid_released;
 
 #define HW_MACADDR_LEN	6
 
 /*
  * Common defines for Hyper-V ICs
  */
 #define HV_ICMSGTYPE_NEGOTIATE		0
 #define HV_ICMSGTYPE_HEARTBEAT		1
 #define HV_ICMSGTYPE_KVPEXCHANGE	2
 #define HV_ICMSGTYPE_SHUTDOWN		3
 #define HV_ICMSGTYPE_TIMESYNC		4
 #define HV_ICMSGTYPE_VSS		5
 
 #define HV_ICMSGHDRFLAG_TRANSACTION	1
 #define HV_ICMSGHDRFLAG_REQUEST		2
 #define HV_ICMSGHDRFLAG_RESPONSE	4
 
 typedef struct hv_vmbus_pipe_hdr {
 	uint32_t flags;
 	uint32_t msgsize;
 } __packed hv_vmbus_pipe_hdr;
 
 typedef struct hv_vmbus_ic_version {
 	uint16_t major;
 	uint16_t minor;
 } __packed hv_vmbus_ic_version;
 
 typedef struct hv_vmbus_icmsg_hdr {
 	hv_vmbus_ic_version	icverframe;
 	uint16_t		icmsgtype;
 	hv_vmbus_ic_version	icvermsg;
 	uint16_t		icmsgsize;
 	uint32_t		status;
 	uint8_t			ictransaction_id;
 	uint8_t			icflags;
 	uint8_t			reserved[2];
 } __packed hv_vmbus_icmsg_hdr;
 
 typedef struct hv_vmbus_icmsg_negotiate {
 	uint16_t		icframe_vercnt;
 	uint16_t		icmsg_vercnt;
 	uint32_t		reserved;
 	hv_vmbus_ic_version	icversion_data[1]; /* any size array */
 } __packed hv_vmbus_icmsg_negotiate;
 
 typedef struct hv_vmbus_shutdown_msg_data {
 	uint32_t		reason_code;
 	uint32_t		timeout_seconds;
 	uint32_t 		flags;
 	uint8_t			display_message[2048];
 } __packed hv_vmbus_shutdown_msg_data;
 
 typedef struct hv_vmbus_heartbeat_msg_data {
 	uint64_t 		seq_num;
 	uint32_t 		reserved[8];
 } __packed hv_vmbus_heartbeat_msg_data;
 
 typedef struct {
 	/*
 	 * offset in bytes from the start of ring data below
 	 */
 	volatile uint32_t       write_index;
 	/*
 	 * offset in bytes from the start of ring data below
 	 */
 	volatile uint32_t       read_index;
 	/*
 	 * NOTE: The interrupt_mask field is used only for channels, but
 	 * vmbus connection also uses this data structure
 	 */
 	volatile uint32_t       interrupt_mask;
 	/* pad it to PAGE_SIZE so that data starts on a page */
 	uint8_t                 reserved[4084];
 
 	/*
 	 * WARNING: Ring data starts here
 	 *  !!! DO NOT place any fields below this !!!
 	 */
 	uint8_t			buffer[0];	/* doubles as interrupt mask */
 } __packed hv_vmbus_ring_buffer;
 
 typedef struct {
 	int		length;
 	int		offset;
 	uint64_t	pfn;
 } __packed hv_vmbus_page_buffer;
 
 typedef struct {
 	int		length;
 	int		offset;
 	uint64_t	pfn_array[HV_MAX_MULTIPAGE_BUFFER_COUNT];
 } __packed hv_vmbus_multipage_buffer;
 
 typedef struct {
 	hv_vmbus_ring_buffer*	ring_buffer;
 	struct mtx		ring_lock;
 	uint32_t		ring_data_size;	/* ring_size */
 } hv_vmbus_ring_buffer_info;
 
 typedef void (*hv_vmbus_pfn_channel_callback)(void *context);
 
 typedef enum {
 	HV_CHANNEL_OFFER_STATE,
 	HV_CHANNEL_OPENING_STATE,
 	HV_CHANNEL_OPEN_STATE,
 	HV_CHANNEL_OPENED_STATE,
 	HV_CHANNEL_CLOSING_NONDESTRUCTIVE_STATE,
 } hv_vmbus_channel_state;
 
 /*
  *  Connection identifier type
  */
 typedef union {
 	uint32_t		as_uint32_t;
 	struct {
 		uint32_t	id:24;
 		uint32_t	reserved:8;
 	} u;
 
 } __packed hv_vmbus_connection_id;
 
 typedef struct hv_vmbus_channel {
 	device_t			ch_dev;
 	struct vmbus_softc		*vmbus_sc;
 	hv_vmbus_channel_state		state;
 	uint32_t			ch_flags;	/* VMBUS_CHAN_FLAG_ */
 	uint32_t			ch_id;		/* channel id */
 	/*
 	 * These are based on the offer_msg.monitor_id.
 	 * Save it here for easy access.
 	 */
 	uint8_t				monitor_group;
 	uint8_t				monitor_bit;
 
 	uint32_t			ring_buffer_gpadl_handle;
 	/*
 	 * Allocated memory for ring buffer
 	 */
 	void*				ring_buffer_pages;
 	unsigned long			ring_buffer_size;
 	uint32_t			ring_buffer_page_count;
 	/*
 	 * send to parent
 	 */
 	hv_vmbus_ring_buffer_info	outbound;
 	/*
 	 * receive from parent
 	 */
 	hv_vmbus_ring_buffer_info	inbound;
 
 	struct taskqueue *		rxq;
 	struct task			channel_task;
 	hv_vmbus_pfn_channel_callback	on_channel_callback;
 	void*				channel_callback_context;
 
-	/*
-	 * If batched_reading is set to "true", mask the interrupt
-	 * and read until the channel is empty.
-	 * If batched_reading is set to "false", the channel is not
-	 * going to perform batched reading.
-	 *
-	 * Batched reading is enabled by default; specific
-	 * drivers that don't want this behavior can turn it off.
-	 */
-	boolean_t			batched_reading;
-
 	struct hypercall_sigevt_in	*ch_sigevt;
 	struct hyperv_dma		ch_sigevt_dma;
 
 	/*
 	 * From Win8, this field specifies the target virtual process
 	 * on which to deliver the interrupt from the host to guest.
 	 * Before Win8, all channel interrupts would only be
 	 * delivered on cpu 0. Setting this value to 0 would preserve
 	 * the earlier behavior.
 	 */
 	uint32_t			target_vcpu;
 	/* The corresponding CPUID in the guest */
 	uint32_t			target_cpu;
 
 	/*
 	 * Support for multi-channels.
 	 * The initial offer is considered the primary channel and this
 	 * offer message will indicate if the host supports multi-channels.
 	 * The guest is free to ask for multi-channels to be offerred and can
 	 * open these multi-channels as a normal "primary" channel. However,
 	 * all multi-channels will have the same type and instance guids as the
 	 * primary channel. Requests sent on a given channel will result in a
 	 * response on the same channel.
 	 */
 
 	struct mtx			sc_lock;
 
 	/*
 	 * Link list of all the multi-channels if this is a primary channel
 	 */
 	TAILQ_HEAD(, hv_vmbus_channel)	sc_list_anchor;
 	TAILQ_ENTRY(hv_vmbus_channel)	sc_list_entry;
 	int				subchan_cnt;
 
 	/*
 	 * The primary channel this sub-channle belongs to.
 	 * This will be NULL for the primary channel.
 	 */
 	struct hv_vmbus_channel		*primary_channel;
 
 	/*
 	 * Driver private data
 	 */
 	void				*hv_chan_priv1;
 	void				*hv_chan_priv2;
 	void				*hv_chan_priv3;
 
 	struct task			ch_detach_task;
 	TAILQ_ENTRY(hv_vmbus_channel)	ch_link;
 	uint32_t			ch_subidx;	/* subchan index */
 
 	struct hv_guid			ch_guid_type;
 	struct hv_guid			ch_guid_inst;
 
 	struct sysctl_ctx_list		ch_sysctl_ctx;
 } hv_vmbus_channel;
 
 #define HV_VMBUS_CHAN_ISPRIMARY(chan)	((chan)->primary_channel == NULL)
 
 #define VMBUS_CHAN_FLAG_HASMNF		0x0001
+/*
+ * If this flag is set, this channel's interrupt will be masked in ISR,
+ * and the RX bufring will be drained before this channel's interrupt is
+ * unmasked.
+ *
+ * This flag is turned on by default.  Drivers can turn it off according
+ * to their own requirement.
+ */
+#define VMBUS_CHAN_FLAG_BATCHREAD	0x0002
 
 static inline void
-hv_set_channel_read_state(hv_vmbus_channel* channel, boolean_t state)
+hv_set_channel_read_state(hv_vmbus_channel* channel, boolean_t on)
 {
-	channel->batched_reading = state;
+	if (!on)
+		channel->ch_flags &= ~VMBUS_CHAN_FLAG_BATCHREAD;
+	else
+		channel->ch_flags |= VMBUS_CHAN_FLAG_BATCHREAD;
 }
 
 int		hv_vmbus_channel_recv_packet(
 				hv_vmbus_channel*	channel,
 				void*			buffer,
 				uint32_t		buffer_len,
 				uint32_t*		buffer_actual_len,
 				uint64_t*		request_id);
 
 int		hv_vmbus_channel_recv_packet_raw(
 				hv_vmbus_channel*	channel,
 				void*			buffer,
 				uint32_t		buffer_len,
 				uint32_t*		buffer_actual_len,
 				uint64_t*		request_id);
 
 int		hv_vmbus_channel_open(
 				hv_vmbus_channel*	channel,
 				uint32_t		send_ring_buffer_size,
 				uint32_t		recv_ring_buffer_size,
 				void*			user_data,
 				uint32_t		user_data_len,
 				hv_vmbus_pfn_channel_callback
 							pfn_on_channel_callback,
 				void*			context);
 
 void		hv_vmbus_channel_close(hv_vmbus_channel *channel);
 
 int		hv_vmbus_channel_send_packet(
 				hv_vmbus_channel*	channel,
 				void*			buffer,
 				uint32_t		buffer_len,
 				uint64_t		request_id,
 				hv_vmbus_packet_type	type,
 				uint32_t		flags);
 
 int		hv_vmbus_channel_send_packet_pagebuffer(
 				hv_vmbus_channel*	channel,
 				hv_vmbus_page_buffer	page_buffers[],
 				uint32_t		page_count,
 				void*			buffer,
 				uint32_t		buffer_len,
 				uint64_t		request_id);
 
 int		hv_vmbus_channel_send_packet_multipagebuffer(
 				hv_vmbus_channel*	    channel,
 				hv_vmbus_multipage_buffer*  multi_page_buffer,
 				void*			    buffer,
 				uint32_t		    buffer_len,
 				uint64_t		    request_id);
 
 int		hv_vmbus_channel_establish_gpadl(
 				hv_vmbus_channel*	channel,
 				/* must be phys and virt contiguous */
 				void*			contig_buffer,
 				/*  page-size multiple	*/
 				uint32_t		size,
 				uint32_t*		gpadl_handle);
 
 int		hv_vmbus_channel_teardown_gpdal(
 				hv_vmbus_channel*	channel,
 				uint32_t		gpadl_handle);
 
 struct hv_vmbus_channel* vmbus_select_outgoing_channel(struct hv_vmbus_channel *promary);
 
 void		vmbus_channel_cpu_set(struct hv_vmbus_channel *chan, int cpu);
 void		vmbus_channel_cpu_rr(struct hv_vmbus_channel *chan);
 struct hv_vmbus_channel **
 		vmbus_get_subchan(struct hv_vmbus_channel *pri_chan, int subchan_cnt);
 void		vmbus_rel_subchan(struct hv_vmbus_channel **subchan, int subchan_cnt);
 void		vmbus_drain_subchan(struct hv_vmbus_channel *pri_chan);
 
 /**
  * @brief Get physical address from virtual
  */
 static inline unsigned long
 hv_get_phys_addr(void *virt)
 {
 	unsigned long ret;
 	ret = (vtophys(virt) | ((vm_offset_t) virt & PAGE_MASK));
 	return (ret);
 }
 
 static __inline struct hv_vmbus_channel *
 vmbus_get_channel(device_t dev)
 {
 	return device_get_ivars(dev);
 }
 
 #endif  /* __HYPERV_H__ */
Index: head/sys/dev/hyperv/vmbus/hv_channel.c
===================================================================
--- head/sys/dev/hyperv/vmbus/hv_channel.c	(revision 302708)
+++ head/sys/dev/hyperv/vmbus/hv_channel.c	(revision 302709)
@@ -1,979 +1,981 @@
 /*-
  * Copyright (c) 2009-2012,2016 Microsoft Corp.
  * Copyright (c) 2012 NetApp Inc.
  * Copyright (c) 2012 Citrix Inc.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
  * are met:
  * 1. Redistributions of source code must retain the above copyright
  *    notice unmodified, this list of conditions, and the following
  *    disclaimer.
  * 2. Redistributions in binary form must reproduce the above copyright
  *    notice, this list of conditions and the following disclaimer in the
  *    documentation and/or other materials provided with the distribution.
  *
  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
 #include <sys/cdefs.h>
 __FBSDID("$FreeBSD$");
 
 #include <sys/param.h>
 #include <sys/kernel.h>
 #include <sys/malloc.h>
 #include <sys/systm.h>
 #include <sys/mbuf.h>
 #include <sys/lock.h>
 #include <sys/mutex.h>
 #include <sys/sysctl.h>
 
 #include <machine/atomic.h>
 #include <machine/bus.h>
 
 #include <vm/vm.h>
 #include <vm/vm_param.h>
 #include <vm/pmap.h>
 
 #include <dev/hyperv/vmbus/hv_vmbus_priv.h>
 #include <dev/hyperv/vmbus/hyperv_var.h>
 #include <dev/hyperv/vmbus/vmbus_reg.h>
 #include <dev/hyperv/vmbus/vmbus_var.h>
 
 static void 	vmbus_channel_set_event(hv_vmbus_channel* channel);
 static void	VmbusProcessChannelEvent(void* channel, int pending);
 static void	vmbus_chan_update_evtflagcnt(struct vmbus_softc *,
 		    const struct hv_vmbus_channel *);
 
 /**
  *  @brief Trigger an event notification on the specified channel
  */
 static void
 vmbus_channel_set_event(hv_vmbus_channel *channel)
 {
 	struct vmbus_softc *sc = channel->vmbus_sc;
 	uint32_t chanid = channel->ch_id;
 
 	atomic_set_long(&sc->vmbus_tx_evtflags[chanid >> VMBUS_EVTFLAG_SHIFT],
 	    1UL << (chanid & VMBUS_EVTFLAG_MASK));
 
 	if (channel->ch_flags & VMBUS_CHAN_FLAG_HASMNF) {
 		hv_vmbus_monitor_page *monitor_page;
 
 		monitor_page = sc->vmbus_mnf2;
 		synch_set_bit(channel->monitor_bit,
 			(uint32_t *)&monitor_page->
 				trigger_group[channel->monitor_group].u.pending);
 	} else {
 		hypercall_signal_event(channel->ch_sigevt_dma.hv_paddr);
 	}
 
 }
 
 static int
 vmbus_channel_sysctl_monalloc(SYSCTL_HANDLER_ARGS)
 {
 	struct hv_vmbus_channel *chan = arg1;
 	int alloc = 0;
 
 	if (chan->ch_flags & VMBUS_CHAN_FLAG_HASMNF)
 		alloc = 1;
 	return sysctl_handle_int(oidp, &alloc, 0, req);
 }
 
 static void
 vmbus_channel_sysctl_create(hv_vmbus_channel* channel)
 {
 	device_t dev;
 	struct sysctl_oid *devch_sysctl;
 	struct sysctl_oid *devch_id_sysctl, *devch_sub_sysctl;
 	struct sysctl_oid *devch_id_in_sysctl, *devch_id_out_sysctl;
 	struct sysctl_ctx_list *ctx;
 	uint32_t ch_id;
 	uint16_t sub_ch_id;
 	char name[16];
 	
 	hv_vmbus_channel* primary_ch = channel->primary_channel;
 
 	if (primary_ch == NULL) {
 		dev = channel->ch_dev;
 		ch_id = channel->ch_id;
 	} else {
 		dev = primary_ch->ch_dev;
 		ch_id = primary_ch->ch_id;
 		sub_ch_id = channel->ch_subidx;
 	}
 	ctx = &channel->ch_sysctl_ctx;
 	sysctl_ctx_init(ctx);
 	/* This creates dev.DEVNAME.DEVUNIT.channel tree */
 	devch_sysctl = SYSCTL_ADD_NODE(ctx,
 		    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
 		    OID_AUTO, "channel", CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "");
 	/* This creates dev.DEVNAME.DEVUNIT.channel.CHANID tree */
 	snprintf(name, sizeof(name), "%d", ch_id);
 	devch_id_sysctl = SYSCTL_ADD_NODE(ctx,
 	    	    SYSCTL_CHILDREN(devch_sysctl),
 	    	    OID_AUTO, name, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "");
 
 	if (primary_ch != NULL) {
 		devch_sub_sysctl = SYSCTL_ADD_NODE(ctx,
 			SYSCTL_CHILDREN(devch_id_sysctl),
 			OID_AUTO, "sub", CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "");
 		snprintf(name, sizeof(name), "%d", sub_ch_id);
 		devch_id_sysctl = SYSCTL_ADD_NODE(ctx,
 			SYSCTL_CHILDREN(devch_sub_sysctl),
 			OID_AUTO, name, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "");
 
 		SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(devch_id_sysctl),
 		    OID_AUTO, "chanid", CTLFLAG_RD,
 		    &channel->ch_id, 0, "channel id");
 	}
 	SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(devch_id_sysctl), OID_AUTO,
 	    "cpu", CTLFLAG_RD, &channel->target_cpu, 0, "owner CPU id");
 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(devch_id_sysctl), OID_AUTO,
 	    "monitor_allocated", CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
 	    channel, 0, vmbus_channel_sysctl_monalloc, "I",
 	    "is monitor allocated to this channel");
 
 	devch_id_in_sysctl = SYSCTL_ADD_NODE(ctx,
                     SYSCTL_CHILDREN(devch_id_sysctl),
                     OID_AUTO,
 		    "in",
 		    CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "");
 	devch_id_out_sysctl = SYSCTL_ADD_NODE(ctx,
                     SYSCTL_CHILDREN(devch_id_sysctl),
                     OID_AUTO,
 		    "out",
 		    CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "");
 	hv_ring_buffer_stat(ctx,
 		SYSCTL_CHILDREN(devch_id_in_sysctl),
 		&(channel->inbound),
 		"inbound ring buffer stats");
 	hv_ring_buffer_stat(ctx,
 		SYSCTL_CHILDREN(devch_id_out_sysctl),
 		&(channel->outbound),
 		"outbound ring buffer stats");
 }
 
 /**
  * @brief Open the specified channel
  */
 int
 hv_vmbus_channel_open(
 	hv_vmbus_channel*		new_channel,
 	uint32_t			send_ring_buffer_size,
 	uint32_t			recv_ring_buffer_size,
 	void*				user_data,
 	uint32_t			user_data_len,
 	hv_vmbus_pfn_channel_callback	pfn_on_channel_callback,
 	void* 				context)
 {
 	struct vmbus_softc *sc = new_channel->vmbus_sc;
 	const struct vmbus_chanmsg_chopen_resp *resp;
 	const struct vmbus_message *msg;
 	struct vmbus_chanmsg_chopen *req;
 	struct vmbus_msghc *mh;
 	uint32_t status;
 	int ret = 0;
 	void *in, *out;
 
 	if (user_data_len > VMBUS_CHANMSG_CHOPEN_UDATA_SIZE) {
 		device_printf(sc->vmbus_dev,
 		    "invalid udata len %u for chan%u\n",
 		    user_data_len, new_channel->ch_id);
 		return EINVAL;
 	}
 
 	mtx_lock(&new_channel->sc_lock);
 	if (new_channel->state == HV_CHANNEL_OPEN_STATE) {
 	    new_channel->state = HV_CHANNEL_OPENING_STATE;
 	} else {
 	    mtx_unlock(&new_channel->sc_lock);
 	    if(bootverbose)
 		printf("VMBUS: Trying to open channel <%p> which in "
 		    "%d state.\n", new_channel, new_channel->state);
 	    return (EINVAL);
 	}
 	mtx_unlock(&new_channel->sc_lock);
 
 	new_channel->on_channel_callback = pfn_on_channel_callback;
 	new_channel->channel_callback_context = context;
 
 	vmbus_chan_update_evtflagcnt(sc, new_channel);
 
 	new_channel->rxq = VMBUS_PCPU_GET(new_channel->vmbus_sc, event_tq,
 	    new_channel->target_cpu);
 	TASK_INIT(&new_channel->channel_task, 0, VmbusProcessChannelEvent, new_channel);
 
 	/* Allocate the ring buffer */
 	out = contigmalloc((send_ring_buffer_size + recv_ring_buffer_size),
 	    M_DEVBUF, M_ZERO, 0UL, BUS_SPACE_MAXADDR, PAGE_SIZE, 0);
 	KASSERT(out != NULL,
 	    ("Error VMBUS: contigmalloc failed to allocate Ring Buffer!"));
 	if (out == NULL)
 		return (ENOMEM);
 
 	in = ((uint8_t *) out + send_ring_buffer_size);
 
 	new_channel->ring_buffer_pages = out;
 	new_channel->ring_buffer_page_count = (send_ring_buffer_size +
 	    recv_ring_buffer_size) >> PAGE_SHIFT;
 	new_channel->ring_buffer_size = send_ring_buffer_size +
 	    recv_ring_buffer_size;
 
 	hv_vmbus_ring_buffer_init(
 		&new_channel->outbound,
 		out,
 		send_ring_buffer_size);
 
 	hv_vmbus_ring_buffer_init(
 		&new_channel->inbound,
 		in,
 		recv_ring_buffer_size);
 
 	/* Create sysctl tree for this channel */
 	vmbus_channel_sysctl_create(new_channel);
 
 	/**
 	 * Establish the gpadl for the ring buffer
 	 */
 	new_channel->ring_buffer_gpadl_handle = 0;
 
 	ret = hv_vmbus_channel_establish_gpadl(new_channel,
 		new_channel->outbound.ring_buffer,
 		send_ring_buffer_size + recv_ring_buffer_size,
 		&new_channel->ring_buffer_gpadl_handle);
 
 	/*
 	 * Open channel w/ the bufring GPADL on the target CPU.
 	 */
 	mh = vmbus_msghc_get(sc, sizeof(*req));
 	if (mh == NULL) {
 		device_printf(sc->vmbus_dev,
 		    "can not get msg hypercall for chopen(chan%u)\n",
 		    new_channel->ch_id);
 		return ENXIO;
 	}
 
 	req = vmbus_msghc_dataptr(mh);
 	req->chm_hdr.chm_type = VMBUS_CHANMSG_TYPE_CHOPEN;
 	req->chm_chanid = new_channel->ch_id;
 	req->chm_openid = new_channel->ch_id;
 	req->chm_gpadl = new_channel->ring_buffer_gpadl_handle;
 	req->chm_vcpuid = new_channel->target_vcpu;
 	req->chm_rxbr_pgofs = send_ring_buffer_size >> PAGE_SHIFT;
 	if (user_data_len)
 		memcpy(req->chm_udata, user_data, user_data_len);
 
 	ret = vmbus_msghc_exec(sc, mh);
 	if (ret != 0) {
 		device_printf(sc->vmbus_dev,
 		    "chopen(chan%u) msg hypercall exec failed: %d\n",
 		    new_channel->ch_id, ret);
 		vmbus_msghc_put(sc, mh);
 		return ret;
 	}
 
 	msg = vmbus_msghc_wait_result(sc, mh);
 	resp = (const struct vmbus_chanmsg_chopen_resp *)msg->msg_data;
 	status = resp->chm_status;
 
 	vmbus_msghc_put(sc, mh);
 
 	if (status == 0) {
 		new_channel->state = HV_CHANNEL_OPENED_STATE;
 		if (bootverbose) {
 			device_printf(sc->vmbus_dev, "chan%u opened\n",
 			    new_channel->ch_id);
 		}
 	} else {
 		device_printf(sc->vmbus_dev, "failed to open chan%u\n",
 		    new_channel->ch_id);
 		ret = ENXIO;
 	}
 	return (ret);
 }
 
 /**
  * @brief Establish a GPADL for the specified buffer
  */
 int
 hv_vmbus_channel_establish_gpadl(struct hv_vmbus_channel *channel,
     void *contig_buffer, uint32_t size, uint32_t *gpadl0)
 {
 	struct vmbus_softc *sc = channel->vmbus_sc;
 	struct vmbus_msghc *mh;
 	struct vmbus_chanmsg_gpadl_conn *req;
 	const struct vmbus_message *msg;
 	size_t reqsz;
 	uint32_t gpadl, status;
 	int page_count, range_len, i, cnt, error;
 	uint64_t page_id, paddr;
 
 	/*
 	 * Preliminary checks.
 	 */
 
 	KASSERT((size & PAGE_MASK) == 0,
 	    ("invalid GPA size %u, not multiple page size", size));
 	page_count = size >> PAGE_SHIFT;
 
 	paddr = hv_get_phys_addr(contig_buffer);
 	KASSERT((paddr & PAGE_MASK) == 0,
 	    ("GPA is not page aligned %jx", (uintmax_t)paddr));
 	page_id = paddr >> PAGE_SHIFT;
 
 	range_len = __offsetof(struct vmbus_gpa_range, gpa_page[page_count]);
 	/*
 	 * We don't support multiple GPA ranges.
 	 */
 	if (range_len > UINT16_MAX) {
 		device_printf(sc->vmbus_dev, "GPA too large, %d pages\n",
 		    page_count);
 		return EOPNOTSUPP;
 	}
 
 	/*
 	 * Allocate GPADL id.
 	 */
 	gpadl = vmbus_gpadl_alloc(sc);
 	*gpadl0 = gpadl;
 
 	/*
 	 * Connect this GPADL to the target channel.
 	 *
 	 * NOTE:
 	 * Since each message can only hold small set of page
 	 * addresses, several messages may be required to
 	 * complete the connection.
 	 */
 	if (page_count > VMBUS_CHANMSG_GPADL_CONN_PGMAX)
 		cnt = VMBUS_CHANMSG_GPADL_CONN_PGMAX;
 	else
 		cnt = page_count;
 	page_count -= cnt;
 
 	reqsz = __offsetof(struct vmbus_chanmsg_gpadl_conn,
 	    chm_range.gpa_page[cnt]);
 	mh = vmbus_msghc_get(sc, reqsz);
 	if (mh == NULL) {
 		device_printf(sc->vmbus_dev,
 		    "can not get msg hypercall for gpadl->chan%u\n",
 		    channel->ch_id);
 		return EIO;
 	}
 
 	req = vmbus_msghc_dataptr(mh);
 	req->chm_hdr.chm_type = VMBUS_CHANMSG_TYPE_GPADL_CONN;
 	req->chm_chanid = channel->ch_id;
 	req->chm_gpadl = gpadl;
 	req->chm_range_len = range_len;
 	req->chm_range_cnt = 1;
 	req->chm_range.gpa_len = size;
 	req->chm_range.gpa_ofs = 0;
 	for (i = 0; i < cnt; ++i)
 		req->chm_range.gpa_page[i] = page_id++;
 
 	error = vmbus_msghc_exec(sc, mh);
 	if (error) {
 		device_printf(sc->vmbus_dev,
 		    "gpadl->chan%u msg hypercall exec failed: %d\n",
 		    channel->ch_id, error);
 		vmbus_msghc_put(sc, mh);
 		return error;
 	}
 
 	while (page_count > 0) {
 		struct vmbus_chanmsg_gpadl_subconn *subreq;
 
 		if (page_count > VMBUS_CHANMSG_GPADL_SUBCONN_PGMAX)
 			cnt = VMBUS_CHANMSG_GPADL_SUBCONN_PGMAX;
 		else
 			cnt = page_count;
 		page_count -= cnt;
 
 		reqsz = __offsetof(struct vmbus_chanmsg_gpadl_subconn,
 		    chm_gpa_page[cnt]);
 		vmbus_msghc_reset(mh, reqsz);
 
 		subreq = vmbus_msghc_dataptr(mh);
 		subreq->chm_hdr.chm_type = VMBUS_CHANMSG_TYPE_GPADL_SUBCONN;
 		subreq->chm_gpadl = gpadl;
 		for (i = 0; i < cnt; ++i)
 			subreq->chm_gpa_page[i] = page_id++;
 
 		vmbus_msghc_exec_noresult(mh);
 	}
 	KASSERT(page_count == 0, ("invalid page count %d", page_count));
 
 	msg = vmbus_msghc_wait_result(sc, mh);
 	status = ((const struct vmbus_chanmsg_gpadl_connresp *)
 	    msg->msg_data)->chm_status;
 
 	vmbus_msghc_put(sc, mh);
 
 	if (status != 0) {
 		device_printf(sc->vmbus_dev, "gpadl->chan%u failed: "
 		    "status %u\n", channel->ch_id, status);
 		return EIO;
 	} else {
 		if (bootverbose) {
 			device_printf(sc->vmbus_dev, "gpadl->chan%u "
 			    "succeeded\n", channel->ch_id);
 		}
 	}
 	return 0;
 }
 
 /*
  * Disconnect the GPA from the target channel
  */
 int
 hv_vmbus_channel_teardown_gpdal(struct hv_vmbus_channel *chan, uint32_t gpadl)
 {
 	struct vmbus_softc *sc = chan->vmbus_sc;
 	struct vmbus_msghc *mh;
 	struct vmbus_chanmsg_gpadl_disconn *req;
 	int error;
 
 	mh = vmbus_msghc_get(sc, sizeof(*req));
 	if (mh == NULL) {
 		device_printf(sc->vmbus_dev,
 		    "can not get msg hypercall for gpa x->chan%u\n",
 		    chan->ch_id);
 		return EBUSY;
 	}
 
 	req = vmbus_msghc_dataptr(mh);
 	req->chm_hdr.chm_type = VMBUS_CHANMSG_TYPE_GPADL_DISCONN;
 	req->chm_chanid = chan->ch_id;
 	req->chm_gpadl = gpadl;
 
 	error = vmbus_msghc_exec(sc, mh);
 	if (error) {
 		device_printf(sc->vmbus_dev,
 		    "gpa x->chan%u msg hypercall exec failed: %d\n",
 		    chan->ch_id, error);
 		vmbus_msghc_put(sc, mh);
 		return error;
 	}
 
 	vmbus_msghc_wait_result(sc, mh);
 	/* Discard result; no useful information */
 	vmbus_msghc_put(sc, mh);
 
 	return 0;
 }
 
 static void
 hv_vmbus_channel_close_internal(hv_vmbus_channel *channel)
 {
 	struct vmbus_softc *sc = channel->vmbus_sc;
 	struct vmbus_msghc *mh;
 	struct vmbus_chanmsg_chclose *req;
 	struct taskqueue *rxq = channel->rxq;
 	int error;
 
 	channel->state = HV_CHANNEL_OPEN_STATE;
 	sysctl_ctx_free(&channel->ch_sysctl_ctx);
 
 	/*
 	 * set rxq to NULL to avoid more requests be scheduled
 	 */
 	channel->rxq = NULL;
 	taskqueue_drain(rxq, &channel->channel_task);
 	channel->on_channel_callback = NULL;
 
 	/**
 	 * Send a closing message
 	 */
 
 	mh = vmbus_msghc_get(sc, sizeof(*req));
 	if (mh == NULL) {
 		device_printf(sc->vmbus_dev,
 		    "can not get msg hypercall for chclose(chan%u)\n",
 		    channel->ch_id);
 		return;
 	}
 
 	req = vmbus_msghc_dataptr(mh);
 	req->chm_hdr.chm_type = VMBUS_CHANMSG_TYPE_CHCLOSE;
 	req->chm_chanid = channel->ch_id;
 
 	error = vmbus_msghc_exec_noresult(mh);
 	vmbus_msghc_put(sc, mh);
 
 	if (error) {
 		device_printf(sc->vmbus_dev,
 		    "chclose(chan%u) msg hypercall exec failed: %d\n",
 		    channel->ch_id, error);
 		return;
 	} else if (bootverbose) {
 		device_printf(sc->vmbus_dev, "close chan%u\n",
 		    channel->ch_id);
 	}
 
 	/* Tear down the gpadl for the channel's ring buffer */
 	if (channel->ring_buffer_gpadl_handle) {
 		hv_vmbus_channel_teardown_gpdal(channel,
 			channel->ring_buffer_gpadl_handle);
 	}
 
 	/* TODO: Send a msg to release the childRelId */
 
 	/* cleanup the ring buffers for this channel */
 	hv_ring_buffer_cleanup(&channel->outbound);
 	hv_ring_buffer_cleanup(&channel->inbound);
 
 	contigfree(channel->ring_buffer_pages, channel->ring_buffer_size,
 	    M_DEVBUF);
 }
 
 /**
  * @brief Close the specified channel
  */
 void
 hv_vmbus_channel_close(hv_vmbus_channel *channel)
 {
 	hv_vmbus_channel*	sub_channel;
 
 	if (channel->primary_channel != NULL) {
 		/*
 		 * We only close multi-channels when the primary is
 		 * closed.
 		 */
 		return;
 	}
 
 	/*
 	 * Close all multi-channels first.
 	 */
 	TAILQ_FOREACH(sub_channel, &channel->sc_list_anchor,
 	    sc_list_entry) {
 		if (sub_channel->state != HV_CHANNEL_OPENED_STATE)
 			continue;
 		hv_vmbus_channel_close_internal(sub_channel);
 	}
 	/*
 	 * Then close the primary channel.
 	 */
 	hv_vmbus_channel_close_internal(channel);
 }
 
 /**
  * @brief Send the specified buffer on the given channel
  */
 int
 hv_vmbus_channel_send_packet(
 	hv_vmbus_channel*	channel,
 	void*			buffer,
 	uint32_t		buffer_len,
 	uint64_t		request_id,
 	hv_vmbus_packet_type	type,
 	uint32_t		flags)
 {
 	int			ret = 0;
 	hv_vm_packet_descriptor	desc;
 	uint32_t		packet_len;
 	uint64_t		aligned_data;
 	uint32_t		packet_len_aligned;
 	boolean_t		need_sig;
 	hv_vmbus_sg_buffer_list	buffer_list[3];
 
 	packet_len = sizeof(hv_vm_packet_descriptor) + buffer_len;
 	packet_len_aligned = HV_ALIGN_UP(packet_len, sizeof(uint64_t));
 	aligned_data = 0;
 
 	/* Setup the descriptor */
 	desc.type = type;   /* HV_VMBUS_PACKET_TYPE_DATA_IN_BAND;             */
 	desc.flags = flags; /* HV_VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED */
 			    /* in 8-bytes granularity */
 	desc.data_offset8 = sizeof(hv_vm_packet_descriptor) >> 3;
 	desc.length8 = (uint16_t) (packet_len_aligned >> 3);
 	desc.transaction_id = request_id;
 
 	buffer_list[0].data = &desc;
 	buffer_list[0].length = sizeof(hv_vm_packet_descriptor);
 
 	buffer_list[1].data = buffer;
 	buffer_list[1].length = buffer_len;
 
 	buffer_list[2].data = &aligned_data;
 	buffer_list[2].length = packet_len_aligned - packet_len;
 
 	ret = hv_ring_buffer_write(&channel->outbound, buffer_list, 3,
 	    &need_sig);
 
 	/* TODO: We should determine if this is optional */
 	if (ret == 0 && need_sig) {
 		vmbus_channel_set_event(channel);
 	}
 
 	return (ret);
 }
 
 /**
  * @brief Send a range of single-page buffer packets using
  * a GPADL Direct packet type
  */
 int
 hv_vmbus_channel_send_packet_pagebuffer(
 	hv_vmbus_channel*	channel,
 	hv_vmbus_page_buffer	page_buffers[],
 	uint32_t		page_count,
 	void*			buffer,
 	uint32_t		buffer_len,
 	uint64_t		request_id)
 {
 
 	int					ret = 0;
 	boolean_t				need_sig;
 	uint32_t				packet_len;
 	uint32_t				page_buflen;
 	uint32_t				packetLen_aligned;
 	hv_vmbus_sg_buffer_list			buffer_list[4];
 	hv_vmbus_channel_packet_page_buffer	desc;
 	uint32_t				descSize;
 	uint64_t				alignedData = 0;
 
 	if (page_count > HV_MAX_PAGE_BUFFER_COUNT)
 		return (EINVAL);
 
 	/*
 	 * Adjust the size down since hv_vmbus_channel_packet_page_buffer
 	 *  is the largest size we support
 	 */
 	descSize = __offsetof(hv_vmbus_channel_packet_page_buffer, range);
 	page_buflen = sizeof(hv_vmbus_page_buffer) * page_count;
 	packet_len = descSize + page_buflen + buffer_len;
 	packetLen_aligned = HV_ALIGN_UP(packet_len, sizeof(uint64_t));
 
 	/* Setup the descriptor */
 	desc.type = HV_VMBUS_PACKET_TYPE_DATA_USING_GPA_DIRECT;
 	desc.flags = HV_VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
 	/* in 8-bytes granularity */
 	desc.data_offset8 = (descSize + page_buflen) >> 3;
 	desc.length8 = (uint16_t) (packetLen_aligned >> 3);
 	desc.transaction_id = request_id;
 	desc.range_count = page_count;
 
 	buffer_list[0].data = &desc;
 	buffer_list[0].length = descSize;
 
 	buffer_list[1].data = page_buffers;
 	buffer_list[1].length = page_buflen;
 
 	buffer_list[2].data = buffer;
 	buffer_list[2].length = buffer_len;
 
 	buffer_list[3].data = &alignedData;
 	buffer_list[3].length = packetLen_aligned - packet_len;
 
 	ret = hv_ring_buffer_write(&channel->outbound, buffer_list, 4,
 	    &need_sig);
 
 	/* TODO: We should determine if this is optional */
 	if (ret == 0 && need_sig) {
 		vmbus_channel_set_event(channel);
 	}
 
 	return (ret);
 }
 
 /**
  * @brief Send a multi-page buffer packet using a GPADL Direct packet type
  */
 int
 hv_vmbus_channel_send_packet_multipagebuffer(
 	hv_vmbus_channel*		channel,
 	hv_vmbus_multipage_buffer*	multi_page_buffer,
 	void*				buffer,
 	uint32_t			buffer_len,
 	uint64_t			request_id)
 {
 
 	int			ret = 0;
 	uint32_t		desc_size;
 	boolean_t		need_sig;
 	uint32_t		packet_len;
 	uint32_t		packet_len_aligned;
 	uint32_t		pfn_count;
 	uint64_t		aligned_data = 0;
 	hv_vmbus_sg_buffer_list	buffer_list[3];
 	hv_vmbus_channel_packet_multipage_buffer desc;
 
 	pfn_count =
 	    HV_NUM_PAGES_SPANNED(
 		    multi_page_buffer->offset,
 		    multi_page_buffer->length);
 
 	if ((pfn_count == 0) || (pfn_count > HV_MAX_MULTIPAGE_BUFFER_COUNT))
 	    return (EINVAL);
 	/*
 	 * Adjust the size down since hv_vmbus_channel_packet_multipage_buffer
 	 * is the largest size we support
 	 */
 	desc_size =
 	    sizeof(hv_vmbus_channel_packet_multipage_buffer) -
 		    ((HV_MAX_MULTIPAGE_BUFFER_COUNT - pfn_count) *
 			sizeof(uint64_t));
 	packet_len = desc_size + buffer_len;
 	packet_len_aligned = HV_ALIGN_UP(packet_len, sizeof(uint64_t));
 
 	/*
 	 * Setup the descriptor
 	 */
 	desc.type = HV_VMBUS_PACKET_TYPE_DATA_USING_GPA_DIRECT;
 	desc.flags = HV_VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
 	desc.data_offset8 = desc_size >> 3; /* in 8-bytes granularity */
 	desc.length8 = (uint16_t) (packet_len_aligned >> 3);
 	desc.transaction_id = request_id;
 	desc.range_count = 1;
 
 	desc.range.length = multi_page_buffer->length;
 	desc.range.offset = multi_page_buffer->offset;
 
 	memcpy(desc.range.pfn_array, multi_page_buffer->pfn_array,
 		pfn_count * sizeof(uint64_t));
 
 	buffer_list[0].data = &desc;
 	buffer_list[0].length = desc_size;
 
 	buffer_list[1].data = buffer;
 	buffer_list[1].length = buffer_len;
 
 	buffer_list[2].data = &aligned_data;
 	buffer_list[2].length = packet_len_aligned - packet_len;
 
 	ret = hv_ring_buffer_write(&channel->outbound, buffer_list, 3,
 	    &need_sig);
 
 	/* TODO: We should determine if this is optional */
 	if (ret == 0 && need_sig) {
 	    vmbus_channel_set_event(channel);
 	}
 
 	return (ret);
 }
 
 /**
  * @brief Retrieve the user packet on the specified channel
  */
 int
 hv_vmbus_channel_recv_packet(
 	hv_vmbus_channel*	channel,
 	void*			Buffer,
 	uint32_t		buffer_len,
 	uint32_t*		buffer_actual_len,
 	uint64_t*		request_id)
 {
 	int			ret;
 	uint32_t		user_len;
 	uint32_t		packet_len;
 	hv_vm_packet_descriptor	desc;
 
 	*buffer_actual_len = 0;
 	*request_id = 0;
 
 	ret = hv_ring_buffer_peek(&channel->inbound, &desc,
 		sizeof(hv_vm_packet_descriptor));
 	if (ret != 0)
 		return (0);
 
 	packet_len = desc.length8 << 3;
 	user_len = packet_len - (desc.data_offset8 << 3);
 
 	*buffer_actual_len = user_len;
 
 	if (user_len > buffer_len)
 		return (EINVAL);
 
 	*request_id = desc.transaction_id;
 
 	/* Copy over the packet to the user buffer */
 	ret = hv_ring_buffer_read(&channel->inbound, Buffer, user_len,
 		(desc.data_offset8 << 3));
 
 	return (0);
 }
 
 /**
  * @brief Retrieve the raw packet on the specified channel
  */
 int
 hv_vmbus_channel_recv_packet_raw(
 	hv_vmbus_channel*	channel,
 	void*			buffer,
 	uint32_t		buffer_len,
 	uint32_t*		buffer_actual_len,
 	uint64_t*		request_id)
 {
 	int		ret;
 	uint32_t	packetLen;
 	hv_vm_packet_descriptor	desc;
 
 	*buffer_actual_len = 0;
 	*request_id = 0;
 
 	ret = hv_ring_buffer_peek(
 		&channel->inbound, &desc,
 		sizeof(hv_vm_packet_descriptor));
 
 	if (ret != 0)
 	    return (0);
 
 	packetLen = desc.length8 << 3;
 	*buffer_actual_len = packetLen;
 
 	if (packetLen > buffer_len)
 	    return (ENOBUFS);
 
 	*request_id = desc.transaction_id;
 
 	/* Copy over the entire packet to the user buffer */
 	ret = hv_ring_buffer_read(&channel->inbound, buffer, packetLen, 0);
 
 	return (0);
 }
 
 
 /**
  * Process a channel event notification
  */
 static void
 VmbusProcessChannelEvent(void* context, int pending)
 {
 	void* arg;
 	uint32_t bytes_to_read;
 	hv_vmbus_channel* channel = (hv_vmbus_channel*)context;
-	boolean_t is_batched_reading;
+	bool is_batched_reading = false;
 
+	if (channel->ch_flags & VMBUS_CHAN_FLAG_BATCHREAD)
+		is_batched_reading = true;
+
 	if (channel->on_channel_callback != NULL) {
 		arg = channel->channel_callback_context;
-		is_batched_reading = channel->batched_reading;
 		/*
 		 * Optimize host to guest signaling by ensuring:
 		 * 1. While reading the channel, we disable interrupts from
 		 *    host.
 		 * 2. Ensure that we process all posted messages from the host
 		 *    before returning from this callback.
 		 * 3. Once we return, enable signaling from the host. Once this
 		 *    state is set we check to see if additional packets are
 		 *    available to read. In this case we repeat the process.
 		 */
 		do {
 			if (is_batched_reading)
 				hv_ring_buffer_read_begin(&channel->inbound);
 
 			channel->on_channel_callback(arg);
 
 			if (is_batched_reading)
 				bytes_to_read =
 				    hv_ring_buffer_read_end(&channel->inbound);
 			else
 				bytes_to_read = 0;
 		} while (is_batched_reading && (bytes_to_read != 0));
 	}
 }
 
 static __inline void
 vmbus_event_flags_proc(struct vmbus_softc *sc, volatile u_long *event_flags,
     int flag_cnt)
 {
 	int f;
 
 	for (f = 0; f < flag_cnt; ++f) {
 		uint32_t rel_id_base;
 		u_long flags;
 		int bit;
 
 		if (event_flags[f] == 0)
 			continue;
 
 		flags = atomic_swap_long(&event_flags[f], 0);
 		rel_id_base = f << VMBUS_EVTFLAG_SHIFT;
 
 		while ((bit = ffsl(flags)) != 0) {
 			struct hv_vmbus_channel *channel;
 			uint32_t rel_id;
 
 			--bit;	/* NOTE: ffsl is 1-based */
 			flags &= ~(1UL << bit);
 
 			rel_id = rel_id_base + bit;
 			channel = sc->vmbus_chmap[rel_id];
 
 			/* if channel is closed or closing */
 			if (channel == NULL || channel->rxq == NULL)
 				continue;
 
-			if (channel->batched_reading)
+			if (channel->ch_flags & VMBUS_CHAN_FLAG_BATCHREAD)
 				hv_ring_buffer_read_begin(&channel->inbound);
 			taskqueue_enqueue(channel->rxq, &channel->channel_task);
 		}
 	}
 }
 
 void
 vmbus_event_proc(struct vmbus_softc *sc, int cpu)
 {
 	struct vmbus_evtflags *eventf;
 
 	/*
 	 * On Host with Win8 or above, the event page can be checked directly
 	 * to get the id of the channel that has the pending interrupt.
 	 */
 	eventf = VMBUS_PCPU_GET(sc, event_flags, cpu) + VMBUS_SINT_MESSAGE;
 	vmbus_event_flags_proc(sc, eventf->evt_flags,
 	    VMBUS_PCPU_GET(sc, event_flags_cnt, cpu));
 }
 
 void
 vmbus_event_proc_compat(struct vmbus_softc *sc, int cpu)
 {
 	struct vmbus_evtflags *eventf;
 
 	eventf = VMBUS_PCPU_GET(sc, event_flags, cpu) + VMBUS_SINT_MESSAGE;
 	if (atomic_testandclear_long(&eventf->evt_flags[0], 0)) {
 		vmbus_event_flags_proc(sc, sc->vmbus_rx_evtflags,
 		    VMBUS_CHAN_MAX_COMPAT >> VMBUS_EVTFLAG_SHIFT);
 	}
 }
 
 static void
 vmbus_chan_update_evtflagcnt(struct vmbus_softc *sc,
     const struct hv_vmbus_channel *chan)
 {
 	volatile int *flag_cnt_ptr;
 	int flag_cnt;
 
 	flag_cnt = (chan->ch_id / VMBUS_EVTFLAG_LEN) + 1;
 	flag_cnt_ptr = VMBUS_PCPU_PTR(sc, event_flags_cnt, chan->target_cpu);
 
 	for (;;) {
 		int old_flag_cnt;
 
 		old_flag_cnt = *flag_cnt_ptr;
 		if (old_flag_cnt >= flag_cnt)
 			break;
 		if (atomic_cmpset_int(flag_cnt_ptr, old_flag_cnt, flag_cnt)) {
 			if (bootverbose) {
 				device_printf(sc->vmbus_dev,
 				    "channel%u update cpu%d flag_cnt to %d\n",
 				    chan->ch_id,
 				    chan->target_cpu, flag_cnt);
 			}
 			break;
 		}
 	}
 }
Index: head/sys/dev/hyperv/vmbus/hv_channel_mgmt.c
===================================================================
--- head/sys/dev/hyperv/vmbus/hv_channel_mgmt.c	(revision 302708)
+++ head/sys/dev/hyperv/vmbus/hv_channel_mgmt.c	(revision 302709)
@@ -1,572 +1,569 @@
 /*-
  * Copyright (c) 2009-2012,2016 Microsoft Corp.
  * Copyright (c) 2012 NetApp Inc.
  * Copyright (c) 2012 Citrix Inc.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
  * are met:
  * 1. Redistributions of source code must retain the above copyright
  *    notice unmodified, this list of conditions, and the following
  *    disclaimer.
  * 2. Redistributions in binary form must reproduce the above copyright
  *    notice, this list of conditions and the following disclaimer in the
  *    documentation and/or other materials provided with the distribution.
  *
  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
 #include <sys/param.h>
 #include <sys/kernel.h>
 #include <sys/lock.h>
 #include <sys/mbuf.h>
 #include <sys/mutex.h>
 
 #include <dev/hyperv/include/hyperv_busdma.h>
 #include <dev/hyperv/vmbus/hv_vmbus_priv.h>
 #include <dev/hyperv/vmbus/vmbus_reg.h>
 #include <dev/hyperv/vmbus/vmbus_var.h>
 
 typedef void	(*vmbus_chanmsg_proc_t)
 		(struct vmbus_softc *, const struct vmbus_message *);
 
 static struct hv_vmbus_channel *hv_vmbus_allocate_channel(struct vmbus_softc *);
 static void	vmbus_channel_on_offer_internal(struct vmbus_softc *,
 		    const hv_vmbus_channel_offer_channel *offer);
 static void	vmbus_chan_detach_task(void *, int);
 
 static void	vmbus_channel_on_offer(struct vmbus_softc *,
 		    const struct vmbus_message *);
 static void	vmbus_channel_on_offer_rescind(struct vmbus_softc *,
 		    const struct vmbus_message *);
 static void	vmbus_channel_on_offers_delivered(struct vmbus_softc *,
 		    const struct vmbus_message *);
 
 /**
  * Channel message dispatch table
  */
 static const vmbus_chanmsg_proc_t
 vmbus_chanmsg_process[HV_CHANNEL_MESSAGE_COUNT] = {
 	[HV_CHANNEL_MESSAGE_OFFER_CHANNEL] =
 		vmbus_channel_on_offer,
 	[HV_CHANNEL_MESSAGE_RESCIND_CHANNEL_OFFER] =
 		vmbus_channel_on_offer_rescind,
 	[HV_CHANNEL_MESSAGE_ALL_OFFERS_DELIVERED] =
 		vmbus_channel_on_offers_delivered,
 	[HV_CHANNEL_MESSAGE_OPEN_CHANNEL_RESULT] =
 		vmbus_msghc_wakeup,
 	[HV_CHANNEL_MESSAGE_GPADL_CREATED] =
 		vmbus_msghc_wakeup,
 	[HV_CHANNEL_MESSAGE_GPADL_TORNDOWN] =
 		vmbus_msghc_wakeup,
 	[HV_CHANNEL_MESSAGE_VERSION_RESPONSE] =
 		vmbus_msghc_wakeup
 };
 
 /**
  * @brief Allocate and initialize a vmbus channel object
  */
 static struct hv_vmbus_channel *
 hv_vmbus_allocate_channel(struct vmbus_softc *sc)
 {
 	struct hv_vmbus_channel *channel;
 
 	channel = malloc(sizeof(*channel), M_DEVBUF, M_WAITOK | M_ZERO);
 	channel->vmbus_sc = sc;
 
 	mtx_init(&channel->sc_lock, "vmbus multi channel", NULL, MTX_DEF);
 	TAILQ_INIT(&channel->sc_list_anchor);
 	TASK_INIT(&channel->ch_detach_task, 0, vmbus_chan_detach_task, channel);
 
 	return (channel);
 }
 
 /**
  * @brief Release the resources used by the vmbus channel object
  */
 void
 hv_vmbus_free_vmbus_channel(hv_vmbus_channel* channel)
 {
 	mtx_destroy(&channel->sc_lock);
 	free(channel, M_DEVBUF);
 }
 
 /**
  * @brief Process the offer by creating a channel/device
  * associated with this offer
  */
 static void
 vmbus_channel_process_offer(hv_vmbus_channel *new_channel)
 {
 	struct vmbus_softc *sc = new_channel->vmbus_sc;
 	hv_vmbus_channel*	channel;
 	uint32_t                relid;
 
 	relid = new_channel->ch_id;
 	/*
 	 * Make sure this is a new offer
 	 */
 	mtx_lock(&sc->vmbus_chlist_lock);
 	if (relid == 0) {
 		/*
 		 * XXX channel0 will not be processed; skip it.
 		 */
 		printf("VMBUS: got channel0 offer\n");
 	} else {
 		sc->vmbus_chmap[relid] = new_channel;
 	}
 
 	TAILQ_FOREACH(channel, &sc->vmbus_chlist, ch_link) {
 		if (memcmp(&channel->ch_guid_type, &new_channel->ch_guid_type,
 		    sizeof(hv_guid)) == 0 &&
 		    memcmp(&channel->ch_guid_inst, &new_channel->ch_guid_inst,
 		    sizeof(hv_guid)) == 0)
 			break;
 	}
 
 	if (channel == NULL) {
 		/* Install the new primary channel */
 		TAILQ_INSERT_TAIL(&sc->vmbus_chlist, new_channel, ch_link);
 	}
 	mtx_unlock(&sc->vmbus_chlist_lock);
 
 	if (bootverbose) {
 		char logstr[64];
 
 		logstr[0] = '\0';
 		if (channel != NULL) {
 			snprintf(logstr, sizeof(logstr), ", primary chan%u",
 			    channel->ch_id);
 		}
 		device_printf(sc->vmbus_dev, "chan%u subchanid%u offer%s\n",
 		    new_channel->ch_id,
 		    new_channel->ch_subidx, logstr);
 	}
 
 	if (channel != NULL) {
 		/*
 		 * Check if this is a sub channel.
 		 */
 		if (new_channel->ch_subidx != 0) {
 			/*
 			 * It is a sub channel offer, process it.
 			 */
 			new_channel->primary_channel = channel;
 			new_channel->ch_dev = channel->ch_dev;
 			mtx_lock(&channel->sc_lock);
 			TAILQ_INSERT_TAIL(&channel->sc_list_anchor,
 			    new_channel, sc_list_entry);
 			mtx_unlock(&channel->sc_lock);
 
 			/*
 			 * Insert the new channel to the end of the global
 			 * channel list.
 			 *
 			 * NOTE:
 			 * The new sub-channel MUST be inserted AFTER it's
 			 * primary channel, so that the primary channel will
 			 * be found in the above loop for its baby siblings.
 			 */
 			mtx_lock(&sc->vmbus_chlist_lock);
 			TAILQ_INSERT_TAIL(&sc->vmbus_chlist, new_channel,
 			    ch_link);
 			mtx_unlock(&sc->vmbus_chlist_lock);
 
 			new_channel->state = HV_CHANNEL_OPEN_STATE;
 
 			/*
 			 * Bump up sub-channel count and notify anyone that is
 			 * interested in this sub-channel, after this sub-channel
 			 * is setup.
 			 */
 			mtx_lock(&channel->sc_lock);
 			channel->subchan_cnt++;
 			mtx_unlock(&channel->sc_lock);
 			wakeup(channel);
 
 			return;
 		}
 
 		printf("VMBUS: duplicated primary channel%u\n",
 		    new_channel->ch_id);
 		hv_vmbus_free_vmbus_channel(new_channel);
 		return;
 	}
 
 	new_channel->state = HV_CHANNEL_OPEN_STATE;
 
 	/*
 	 * Add the new device to the bus. This will kick off device-driver
 	 * binding which eventually invokes the device driver's AddDevice()
 	 * method.
 	 *
 	 * NOTE:
 	 * Error is ignored here; don't have much to do if error really
 	 * happens.
 	 */
 	hv_vmbus_child_device_register(new_channel);
 }
 
 void
 vmbus_channel_cpu_set(struct hv_vmbus_channel *chan, int cpu)
 {
 	KASSERT(cpu >= 0 && cpu < mp_ncpus, ("invalid cpu %d", cpu));
 
 	if (chan->vmbus_sc->vmbus_version == VMBUS_VERSION_WS2008 ||
 	    chan->vmbus_sc->vmbus_version == VMBUS_VERSION_WIN7) {
 		/* Only cpu0 is supported */
 		cpu = 0;
 	}
 
 	chan->target_cpu = cpu;
 	chan->target_vcpu = VMBUS_PCPU_GET(chan->vmbus_sc, vcpuid, cpu);
 
 	if (bootverbose) {
 		printf("vmbus_chan%u: assigned to cpu%u [vcpu%u]\n",
 		    chan->ch_id,
 		    chan->target_cpu, chan->target_vcpu);
 	}
 }
 
 void
 vmbus_channel_cpu_rr(struct hv_vmbus_channel *chan)
 {
 	static uint32_t vmbus_chan_nextcpu;
 	int cpu;
 
 	cpu = atomic_fetchadd_int(&vmbus_chan_nextcpu, 1) % mp_ncpus;
 	vmbus_channel_cpu_set(chan, cpu);
 }
 
 static void
 vmbus_channel_select_defcpu(struct hv_vmbus_channel *chan)
 {
 	/*
 	 * By default, pin the channel to cpu0.  Devices having
 	 * special channel-cpu mapping requirement should call
 	 * vmbus_channel_cpu_{set,rr}().
 	 */
 	vmbus_channel_cpu_set(chan, 0);
 }
 
 /**
  * @brief Handler for channel offers from Hyper-V/Azure
  *
  * Handler for channel offers from vmbus in parent partition.
  */
 static void
 vmbus_channel_on_offer(struct vmbus_softc *sc, const struct vmbus_message *msg)
 {
 	const hv_vmbus_channel_offer_channel *offer;
 
 	/* New channel is offered by vmbus */
 	vmbus_scan_newchan(sc);
 
 	offer = (const hv_vmbus_channel_offer_channel *)msg->msg_data;
 	vmbus_channel_on_offer_internal(sc, offer);
 }
 
 static void
 vmbus_channel_on_offer_internal(struct vmbus_softc *sc,
     const hv_vmbus_channel_offer_channel *offer)
 {
 	hv_vmbus_channel* new_channel;
 
-	/* Allocate the channel object and save this offer */
+	/*
+	 * Allocate the channel object and save this offer
+	 */
 	new_channel = hv_vmbus_allocate_channel(sc);
 	new_channel->ch_id = offer->child_rel_id;
 	new_channel->ch_subidx = offer->offer.sub_channel_index;
-	if (offer->monitor_allocated)
-		new_channel->ch_flags |= VMBUS_CHAN_FLAG_HASMNF;
 	new_channel->ch_guid_type = offer->offer.interface_type;
 	new_channel->ch_guid_inst = offer->offer.interface_instance;
 
-	/*
-	 * By default we setup state to enable batched
-	 * reading. A specific service can choose to
-	 * disable this prior to opening the channel.
-	 */
-	new_channel->batched_reading = TRUE;
+	/* Batch reading is on by default */
+	new_channel->ch_flags |= VMBUS_CHAN_FLAG_BATCHREAD;
+	if (offer->monitor_allocated)
+		new_channel->ch_flags |= VMBUS_CHAN_FLAG_HASMNF;
 
 	new_channel->ch_sigevt = hyperv_dmamem_alloc(
 	    bus_get_dma_tag(sc->vmbus_dev),
 	    HYPERCALL_SIGEVTIN_ALIGN, 0, sizeof(struct hypercall_sigevt_in),
 	    &new_channel->ch_sigevt_dma, BUS_DMA_WAITOK | BUS_DMA_ZERO);
 	if (new_channel->ch_sigevt == NULL) {
 		device_printf(sc->vmbus_dev, "sigevt alloc failed\n");
 		/* XXX */
 		mtx_destroy(&new_channel->sc_lock);
 		free(new_channel, M_DEVBUF);
 		return;
 	}
 	new_channel->ch_sigevt->hc_connid = VMBUS_CONNID_EVENT;
-
 	if (sc->vmbus_version != VMBUS_VERSION_WS2008)
 		new_channel->ch_sigevt->hc_connid = offer->connection_id;
 
 	new_channel->monitor_group = (uint8_t) offer->monitor_id / 32;
 	new_channel->monitor_bit = (uint8_t) offer->monitor_id % 32;
 
 	/* Select default cpu for this channel. */
 	vmbus_channel_select_defcpu(new_channel);
 
 	vmbus_channel_process_offer(new_channel);
 }
 
 /**
  * @brief Rescind offer handler.
  *
  * We queue a work item to process this offer
  * synchronously.
  *
  * XXX pretty broken; need rework.
  */
 static void
 vmbus_channel_on_offer_rescind(struct vmbus_softc *sc,
     const struct vmbus_message *msg)
 {
 	const hv_vmbus_channel_rescind_offer *rescind;
 	hv_vmbus_channel*		channel;
 
 	rescind = (const hv_vmbus_channel_rescind_offer *)msg->msg_data;
 	if (bootverbose) {
 		device_printf(sc->vmbus_dev, "chan%u rescind\n",
 		    rescind->child_rel_id);
 	}
 
 	channel = sc->vmbus_chmap[rescind->child_rel_id];
 	if (channel == NULL)
 	    return;
 	sc->vmbus_chmap[rescind->child_rel_id] = NULL;
 
 	taskqueue_enqueue(taskqueue_thread, &channel->ch_detach_task);
 }
 
 static void
 vmbus_chan_detach_task(void *xchan, int pending __unused)
 {
 	struct hv_vmbus_channel *chan = xchan;
 
 	if (HV_VMBUS_CHAN_ISPRIMARY(chan)) {
 		/* Only primary channel owns the device */
 		hv_vmbus_child_device_unregister(chan);
 		/* NOTE: DO NOT free primary channel for now */
 	} else {
 		struct vmbus_softc *sc = chan->vmbus_sc;
 		struct hv_vmbus_channel *pri_chan = chan->primary_channel;
 		struct vmbus_chanmsg_chfree *req;
 		struct vmbus_msghc *mh;
 		int error;
 
 		mh = vmbus_msghc_get(sc, sizeof(*req));
 		if (mh == NULL) {
 			device_printf(sc->vmbus_dev,
 			    "can not get msg hypercall for chfree(chan%u)\n",
 			    chan->ch_id);
 			goto remove;
 		}
 
 		req = vmbus_msghc_dataptr(mh);
 		req->chm_hdr.chm_type = VMBUS_CHANMSG_TYPE_CHFREE;
 		req->chm_chanid = chan->ch_id;
 
 		error = vmbus_msghc_exec_noresult(mh);
 		vmbus_msghc_put(sc, mh);
 
 		if (error) {
 			device_printf(sc->vmbus_dev,
 			    "chfree(chan%u) failed: %d",
 			    chan->ch_id, error);
 			/* NOTE: Move on! */
 		} else {
 			if (bootverbose) {
 				device_printf(sc->vmbus_dev, "chan%u freed\n",
 				    chan->ch_id);
 			}
 		}
 remove:
 		mtx_lock(&sc->vmbus_chlist_lock);
 		TAILQ_REMOVE(&sc->vmbus_chlist, chan, ch_link);
 		mtx_unlock(&sc->vmbus_chlist_lock);
 
 		mtx_lock(&pri_chan->sc_lock);
 		TAILQ_REMOVE(&pri_chan->sc_list_anchor, chan, sc_list_entry);
 		KASSERT(pri_chan->subchan_cnt > 0,
 		    ("invalid subchan_cnt %d", pri_chan->subchan_cnt));
 		pri_chan->subchan_cnt--;
 		mtx_unlock(&pri_chan->sc_lock);
 		wakeup(pri_chan);
 
 		hv_vmbus_free_vmbus_channel(chan);
 	}
 }
 
 /**
  *
  * @brief Invoked when all offers have been delivered.
  */
 static void
 vmbus_channel_on_offers_delivered(struct vmbus_softc *sc,
     const struct vmbus_message *msg __unused)
 {
 
 	/* No more new channels for the channel request. */
 	vmbus_scan_done(sc);
 }
 
 /**
  * @brief Release channels that are unattached/unconnected (i.e., no drivers associated)
  */
 void
 hv_vmbus_release_unattached_channels(struct vmbus_softc *sc)
 {
 	hv_vmbus_channel *channel;
 
 	mtx_lock(&sc->vmbus_chlist_lock);
 
 	while (!TAILQ_EMPTY(&sc->vmbus_chlist)) {
 	    channel = TAILQ_FIRST(&sc->vmbus_chlist);
 	    TAILQ_REMOVE(&sc->vmbus_chlist, channel, ch_link);
 
 	    if (HV_VMBUS_CHAN_ISPRIMARY(channel)) {
 		/* Only primary channel owns the device */
 		hv_vmbus_child_device_unregister(channel);
 	    }
 	    hv_vmbus_free_vmbus_channel(channel);
 	}
 	bzero(sc->vmbus_chmap,
 	    sizeof(struct hv_vmbus_channel *) * VMBUS_CHAN_MAX);
 
 	mtx_unlock(&sc->vmbus_chlist_lock);
 }
 
 /**
  * @brief Select the best outgoing channel
  * 
  * The channel whose vcpu binding is closest to the currect vcpu will
  * be selected.
  * If no multi-channel, always select primary channel
  * 
  * @param primary - primary channel
  */
 struct hv_vmbus_channel *
 vmbus_select_outgoing_channel(struct hv_vmbus_channel *primary)
 {
 	hv_vmbus_channel *new_channel = NULL;
 	hv_vmbus_channel *outgoing_channel = primary;
 	int old_cpu_distance = 0;
 	int new_cpu_distance = 0;
 	int cur_vcpu = 0;
 	int smp_pro_id = PCPU_GET(cpuid);
 
 	if (TAILQ_EMPTY(&primary->sc_list_anchor)) {
 		return outgoing_channel;
 	}
 
 	if (smp_pro_id >= MAXCPU) {
 		return outgoing_channel;
 	}
 
 	cur_vcpu = VMBUS_PCPU_GET(primary->vmbus_sc, vcpuid, smp_pro_id);
 	
 	TAILQ_FOREACH(new_channel, &primary->sc_list_anchor, sc_list_entry) {
 		if (new_channel->state != HV_CHANNEL_OPENED_STATE){
 			continue;
 		}
 
 		if (new_channel->target_vcpu == cur_vcpu){
 			return new_channel;
 		}
 
 		old_cpu_distance = ((outgoing_channel->target_vcpu > cur_vcpu) ?
 		    (outgoing_channel->target_vcpu - cur_vcpu) :
 		    (cur_vcpu - outgoing_channel->target_vcpu));
 
 		new_cpu_distance = ((new_channel->target_vcpu > cur_vcpu) ?
 		    (new_channel->target_vcpu - cur_vcpu) :
 		    (cur_vcpu - new_channel->target_vcpu));
 
 		if (old_cpu_distance < new_cpu_distance) {
 			continue;
 		}
 
 		outgoing_channel = new_channel;
 	}
 
 	return(outgoing_channel);
 }
 
 struct hv_vmbus_channel **
 vmbus_get_subchan(struct hv_vmbus_channel *pri_chan, int subchan_cnt)
 {
 	struct hv_vmbus_channel **ret, *chan;
 	int i;
 
 	ret = malloc(subchan_cnt * sizeof(struct hv_vmbus_channel *), M_TEMP,
 	    M_WAITOK);
 
 	mtx_lock(&pri_chan->sc_lock);
 
 	while (pri_chan->subchan_cnt < subchan_cnt)
 		mtx_sleep(pri_chan, &pri_chan->sc_lock, 0, "subch", 0);
 
 	i = 0;
 	TAILQ_FOREACH(chan, &pri_chan->sc_list_anchor, sc_list_entry) {
 		/* TODO: refcnt chan */
 		ret[i] = chan;
 
 		++i;
 		if (i == subchan_cnt)
 			break;
 	}
 	KASSERT(i == subchan_cnt, ("invalid subchan count %d, should be %d",
 	    pri_chan->subchan_cnt, subchan_cnt));
 
 	mtx_unlock(&pri_chan->sc_lock);
 
 	return ret;
 }
 
 void
 vmbus_rel_subchan(struct hv_vmbus_channel **subchan, int subchan_cnt __unused)
 {
 
 	free(subchan, M_TEMP);
 }
 
 void
 vmbus_drain_subchan(struct hv_vmbus_channel *pri_chan)
 {
 	mtx_lock(&pri_chan->sc_lock);
 	while (pri_chan->subchan_cnt > 0)
 		mtx_sleep(pri_chan, &pri_chan->sc_lock, 0, "dsubch", 0);
 	mtx_unlock(&pri_chan->sc_lock);
 }
 
 void
 vmbus_chan_msgproc(struct vmbus_softc *sc, const struct vmbus_message *msg)
 {
 	vmbus_chanmsg_proc_t msg_proc;
 	uint32_t msg_type;
 
 	msg_type = ((const struct vmbus_chanmsg_hdr *)msg->msg_data)->chm_type;
 	if (msg_type >= HV_CHANNEL_MESSAGE_COUNT) {
 		device_printf(sc->vmbus_dev, "unknown message type 0x%x\n",
 		    msg_type);
 		return;
 	}
 
 	msg_proc = vmbus_chanmsg_process[msg_type];
 	if (msg_proc != NULL)
 		msg_proc(sc, msg);
 }