vmbus_chan.c revision 302633
1/*-
2 * Copyright (c) 2009-2012,2016 Microsoft Corp.
3 * Copyright (c) 2012 NetApp Inc.
4 * Copyright (c) 2012 Citrix Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice unmodified, this list of conditions, and the following
12 *    disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <sys/cdefs.h>
30__FBSDID("$FreeBSD: head/sys/dev/hyperv/vmbus/hv_channel.c 302633 2016-07-12 08:28:51Z sephe $");
31
32#include <sys/param.h>
33#include <sys/kernel.h>
34#include <sys/malloc.h>
35#include <sys/systm.h>
36#include <sys/mbuf.h>
37#include <sys/lock.h>
38#include <sys/mutex.h>
39#include <sys/sysctl.h>
40
41#include <machine/atomic.h>
42#include <machine/bus.h>
43
44#include <vm/vm.h>
45#include <vm/vm_param.h>
46#include <vm/pmap.h>
47
48#include <dev/hyperv/vmbus/hv_vmbus_priv.h>
49#include <dev/hyperv/vmbus/hyperv_var.h>
50#include <dev/hyperv/vmbus/vmbus_reg.h>
51#include <dev/hyperv/vmbus/vmbus_var.h>
52
53static void 	vmbus_channel_set_event(hv_vmbus_channel* channel);
54static void	VmbusProcessChannelEvent(void* channel, int pending);
55
56/**
57 *  @brief Trigger an event notification on the specified channel
58 */
59static void
60vmbus_channel_set_event(hv_vmbus_channel *channel)
61{
62	struct vmbus_softc *sc = channel->vmbus_sc;
63	uint32_t chanid = channel->offer_msg.child_rel_id;
64
65	atomic_set_long(&sc->vmbus_tx_evtflags[chanid >> VMBUS_EVTFLAG_SHIFT],
66	    1UL << (chanid & VMBUS_EVTFLAG_MASK));
67
68	if (channel->offer_msg.monitor_allocated) {
69		hv_vmbus_monitor_page *monitor_page;
70
71		monitor_page = sc->vmbus_mnf2;
72		synch_set_bit(channel->monitor_bit,
73			(uint32_t *)&monitor_page->
74				trigger_group[channel->monitor_group].u.pending);
75	} else {
76		hypercall_signal_event(channel->ch_sigevt_dma.hv_paddr);
77	}
78
79}
80
81static int
82vmbus_channel_sysctl_monalloc(SYSCTL_HANDLER_ARGS)
83{
84	struct hv_vmbus_channel *chan = arg1;
85	int alloc = 0;
86
87	if (chan->offer_msg.monitor_allocated)
88		alloc = 1;
89	return sysctl_handle_int(oidp, &alloc, 0, req);
90}
91
92static void
93vmbus_channel_sysctl_create(hv_vmbus_channel* channel)
94{
95	device_t dev;
96	struct sysctl_oid *devch_sysctl;
97	struct sysctl_oid *devch_id_sysctl, *devch_sub_sysctl;
98	struct sysctl_oid *devch_id_in_sysctl, *devch_id_out_sysctl;
99	struct sysctl_ctx_list *ctx;
100	uint32_t ch_id;
101	uint16_t sub_ch_id;
102	char name[16];
103
104	hv_vmbus_channel* primary_ch = channel->primary_channel;
105
106	if (primary_ch == NULL) {
107		dev = channel->device->device;
108		ch_id = channel->offer_msg.child_rel_id;
109	} else {
110		dev = primary_ch->device->device;
111		ch_id = primary_ch->offer_msg.child_rel_id;
112		sub_ch_id = channel->offer_msg.offer.sub_channel_index;
113	}
114	ctx = &channel->ch_sysctl_ctx;
115	sysctl_ctx_init(ctx);
116	/* This creates dev.DEVNAME.DEVUNIT.channel tree */
117	devch_sysctl = SYSCTL_ADD_NODE(ctx,
118		    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
119		    OID_AUTO, "channel", CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "");
120	/* This creates dev.DEVNAME.DEVUNIT.channel.CHANID tree */
121	snprintf(name, sizeof(name), "%d", ch_id);
122	devch_id_sysctl = SYSCTL_ADD_NODE(ctx,
123	    	    SYSCTL_CHILDREN(devch_sysctl),
124	    	    OID_AUTO, name, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "");
125
126	if (primary_ch != NULL) {
127		devch_sub_sysctl = SYSCTL_ADD_NODE(ctx,
128			SYSCTL_CHILDREN(devch_id_sysctl),
129			OID_AUTO, "sub", CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "");
130		snprintf(name, sizeof(name), "%d", sub_ch_id);
131		devch_id_sysctl = SYSCTL_ADD_NODE(ctx,
132			SYSCTL_CHILDREN(devch_sub_sysctl),
133			OID_AUTO, name, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "");
134
135		SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(devch_id_sysctl),
136		    OID_AUTO, "chanid", CTLFLAG_RD,
137		    &channel->offer_msg.child_rel_id, 0, "channel id");
138	}
139	SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(devch_id_sysctl), OID_AUTO,
140	    "cpu", CTLFLAG_RD, &channel->target_cpu, 0, "owner CPU id");
141	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(devch_id_sysctl), OID_AUTO,
142	    "monitor_allocated", CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
143	    channel, 0, vmbus_channel_sysctl_monalloc, "I",
144	    "is monitor allocated to this channel");
145
146	devch_id_in_sysctl = SYSCTL_ADD_NODE(ctx,
147                    SYSCTL_CHILDREN(devch_id_sysctl),
148                    OID_AUTO,
149		    "in",
150		    CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "");
151	devch_id_out_sysctl = SYSCTL_ADD_NODE(ctx,
152                    SYSCTL_CHILDREN(devch_id_sysctl),
153                    OID_AUTO,
154		    "out",
155		    CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "");
156	hv_ring_buffer_stat(ctx,
157		SYSCTL_CHILDREN(devch_id_in_sysctl),
158		&(channel->inbound),
159		"inbound ring buffer stats");
160	hv_ring_buffer_stat(ctx,
161		SYSCTL_CHILDREN(devch_id_out_sysctl),
162		&(channel->outbound),
163		"outbound ring buffer stats");
164}
165
166/**
167 * @brief Open the specified channel
168 */
169int
170hv_vmbus_channel_open(
171	hv_vmbus_channel*		new_channel,
172	uint32_t			send_ring_buffer_size,
173	uint32_t			recv_ring_buffer_size,
174	void*				user_data,
175	uint32_t			user_data_len,
176	hv_vmbus_pfn_channel_callback	pfn_on_channel_callback,
177	void* 				context)
178{
179	struct vmbus_softc *sc = new_channel->vmbus_sc;
180	const struct vmbus_chanmsg_chopen_resp *resp;
181	const struct vmbus_message *msg;
182	struct vmbus_chanmsg_chopen *req;
183	struct vmbus_msghc *mh;
184	uint32_t status;
185	int ret = 0;
186	void *in, *out;
187
188	if (user_data_len > VMBUS_CHANMSG_CHOPEN_UDATA_SIZE) {
189		device_printf(sc->vmbus_dev,
190		    "invalid udata len %u for chan%u\n",
191		    user_data_len, new_channel->offer_msg.child_rel_id);
192		return EINVAL;
193	}
194
195	mtx_lock(&new_channel->sc_lock);
196	if (new_channel->state == HV_CHANNEL_OPEN_STATE) {
197	    new_channel->state = HV_CHANNEL_OPENING_STATE;
198	} else {
199	    mtx_unlock(&new_channel->sc_lock);
200	    if(bootverbose)
201		printf("VMBUS: Trying to open channel <%p> which in "
202		    "%d state.\n", new_channel, new_channel->state);
203	    return (EINVAL);
204	}
205	mtx_unlock(&new_channel->sc_lock);
206
207	new_channel->on_channel_callback = pfn_on_channel_callback;
208	new_channel->channel_callback_context = context;
209
210	vmbus_on_channel_open(new_channel);
211
212	new_channel->rxq = VMBUS_PCPU_GET(new_channel->vmbus_sc, event_tq,
213	    new_channel->target_cpu);
214	TASK_INIT(&new_channel->channel_task, 0, VmbusProcessChannelEvent, new_channel);
215
216	/* Allocate the ring buffer */
217	out = contigmalloc((send_ring_buffer_size + recv_ring_buffer_size),
218	    M_DEVBUF, M_ZERO, 0UL, BUS_SPACE_MAXADDR, PAGE_SIZE, 0);
219	KASSERT(out != NULL,
220	    ("Error VMBUS: contigmalloc failed to allocate Ring Buffer!"));
221	if (out == NULL)
222		return (ENOMEM);
223
224	in = ((uint8_t *) out + send_ring_buffer_size);
225
226	new_channel->ring_buffer_pages = out;
227	new_channel->ring_buffer_page_count = (send_ring_buffer_size +
228	    recv_ring_buffer_size) >> PAGE_SHIFT;
229	new_channel->ring_buffer_size = send_ring_buffer_size +
230	    recv_ring_buffer_size;
231
232	hv_vmbus_ring_buffer_init(
233		&new_channel->outbound,
234		out,
235		send_ring_buffer_size);
236
237	hv_vmbus_ring_buffer_init(
238		&new_channel->inbound,
239		in,
240		recv_ring_buffer_size);
241
242	/* Create sysctl tree for this channel */
243	vmbus_channel_sysctl_create(new_channel);
244
245	/**
246	 * Establish the gpadl for the ring buffer
247	 */
248	new_channel->ring_buffer_gpadl_handle = 0;
249
250	ret = hv_vmbus_channel_establish_gpadl(new_channel,
251		new_channel->outbound.ring_buffer,
252		send_ring_buffer_size + recv_ring_buffer_size,
253		&new_channel->ring_buffer_gpadl_handle);
254
255	/*
256	 * Open channel w/ the bufring GPADL on the target CPU.
257	 */
258	mh = vmbus_msghc_get(sc, sizeof(*req));
259	if (mh == NULL) {
260		device_printf(sc->vmbus_dev,
261		    "can not get msg hypercall for chopen(chan%u)\n",
262		    new_channel->offer_msg.child_rel_id);
263		return ENXIO;
264	}
265
266	req = vmbus_msghc_dataptr(mh);
267	req->chm_hdr.chm_type = VMBUS_CHANMSG_TYPE_CHOPEN;
268	req->chm_chanid = new_channel->offer_msg.child_rel_id;
269	req->chm_openid = new_channel->offer_msg.child_rel_id;
270	req->chm_gpadl = new_channel->ring_buffer_gpadl_handle;
271	req->chm_vcpuid = new_channel->target_vcpu;
272	req->chm_rxbr_pgofs = send_ring_buffer_size >> PAGE_SHIFT;
273	if (user_data_len)
274		memcpy(req->chm_udata, user_data, user_data_len);
275
276	ret = vmbus_msghc_exec(sc, mh);
277	if (ret != 0) {
278		device_printf(sc->vmbus_dev,
279		    "chopen(chan%u) msg hypercall exec failed: %d\n",
280		    new_channel->offer_msg.child_rel_id, ret);
281		vmbus_msghc_put(sc, mh);
282		return ret;
283	}
284
285	msg = vmbus_msghc_wait_result(sc, mh);
286	resp = (const struct vmbus_chanmsg_chopen_resp *)msg->msg_data;
287	status = resp->chm_status;
288
289	vmbus_msghc_put(sc, mh);
290
291	if (status == 0) {
292		new_channel->state = HV_CHANNEL_OPENED_STATE;
293		if (bootverbose) {
294			device_printf(sc->vmbus_dev, "chan%u opened\n",
295			    new_channel->offer_msg.child_rel_id);
296		}
297	} else {
298		device_printf(sc->vmbus_dev, "failed to open chan%u\n",
299		    new_channel->offer_msg.child_rel_id);
300		ret = ENXIO;
301	}
302	return (ret);
303}
304
305/**
306 * @brief Establish a GPADL for the specified buffer
307 */
308int
309hv_vmbus_channel_establish_gpadl(struct hv_vmbus_channel *channel,
310    void *contig_buffer, uint32_t size, uint32_t *gpadl0)
311{
312	struct vmbus_softc *sc = channel->vmbus_sc;
313	struct vmbus_msghc *mh;
314	struct vmbus_chanmsg_gpadl_conn *req;
315	const struct vmbus_message *msg;
316	size_t reqsz;
317	uint32_t gpadl, status;
318	int page_count, range_len, i, cnt, error;
319	uint64_t page_id, paddr;
320
321	/*
322	 * Preliminary checks.
323	 */
324
325	KASSERT((size & PAGE_MASK) == 0,
326	    ("invalid GPA size %u, not multiple page size", size));
327	page_count = size >> PAGE_SHIFT;
328
329	paddr = hv_get_phys_addr(contig_buffer);
330	KASSERT((paddr & PAGE_MASK) == 0,
331	    ("GPA is not page aligned %jx", (uintmax_t)paddr));
332	page_id = paddr >> PAGE_SHIFT;
333
334	range_len = __offsetof(struct vmbus_gpa_range, gpa_page[page_count]);
335	/*
336	 * We don't support multiple GPA ranges.
337	 */
338	if (range_len > UINT16_MAX) {
339		device_printf(sc->vmbus_dev, "GPA too large, %d pages\n",
340		    page_count);
341		return EOPNOTSUPP;
342	}
343
344	/*
345	 * Allocate GPADL id.
346	 */
347	gpadl = vmbus_gpadl_alloc(sc);
348	*gpadl0 = gpadl;
349
350	/*
351	 * Connect this GPADL to the target channel.
352	 *
353	 * NOTE:
354	 * Since each message can only hold small set of page
355	 * addresses, several messages may be required to
356	 * complete the connection.
357	 */
358	if (page_count > VMBUS_CHANMSG_GPADL_CONN_PGMAX)
359		cnt = VMBUS_CHANMSG_GPADL_CONN_PGMAX;
360	else
361		cnt = page_count;
362	page_count -= cnt;
363
364	reqsz = __offsetof(struct vmbus_chanmsg_gpadl_conn,
365	    chm_range.gpa_page[cnt]);
366	mh = vmbus_msghc_get(sc, reqsz);
367	if (mh == NULL) {
368		device_printf(sc->vmbus_dev,
369		    "can not get msg hypercall for gpadl->chan%u\n",
370		    channel->offer_msg.child_rel_id);
371		return EIO;
372	}
373
374	req = vmbus_msghc_dataptr(mh);
375	req->chm_hdr.chm_type = VMBUS_CHANMSG_TYPE_GPADL_CONN;
376	req->chm_chanid = channel->offer_msg.child_rel_id;
377	req->chm_gpadl = gpadl;
378	req->chm_range_len = range_len;
379	req->chm_range_cnt = 1;
380	req->chm_range.gpa_len = size;
381	req->chm_range.gpa_ofs = 0;
382	for (i = 0; i < cnt; ++i)
383		req->chm_range.gpa_page[i] = page_id++;
384
385	error = vmbus_msghc_exec(sc, mh);
386	if (error) {
387		device_printf(sc->vmbus_dev,
388		    "gpadl->chan%u msg hypercall exec failed: %d\n",
389		    channel->offer_msg.child_rel_id, error);
390		vmbus_msghc_put(sc, mh);
391		return error;
392	}
393
394	while (page_count > 0) {
395		struct vmbus_chanmsg_gpadl_subconn *subreq;
396
397		if (page_count > VMBUS_CHANMSG_GPADL_SUBCONN_PGMAX)
398			cnt = VMBUS_CHANMSG_GPADL_SUBCONN_PGMAX;
399		else
400			cnt = page_count;
401		page_count -= cnt;
402
403		reqsz = __offsetof(struct vmbus_chanmsg_gpadl_subconn,
404		    chm_gpa_page[cnt]);
405		vmbus_msghc_reset(mh, reqsz);
406
407		subreq = vmbus_msghc_dataptr(mh);
408		subreq->chm_hdr.chm_type = VMBUS_CHANMSG_TYPE_GPADL_SUBCONN;
409		subreq->chm_gpadl = gpadl;
410		for (i = 0; i < cnt; ++i)
411			subreq->chm_gpa_page[i] = page_id++;
412
413		vmbus_msghc_exec_noresult(mh);
414	}
415	KASSERT(page_count == 0, ("invalid page count %d", page_count));
416
417	msg = vmbus_msghc_wait_result(sc, mh);
418	status = ((const struct vmbus_chanmsg_gpadl_connresp *)
419	    msg->msg_data)->chm_status;
420
421	vmbus_msghc_put(sc, mh);
422
423	if (status != 0) {
424		device_printf(sc->vmbus_dev, "gpadl->chan%u failed: "
425		    "status %u\n", channel->offer_msg.child_rel_id, status);
426		return EIO;
427	} else {
428		if (bootverbose) {
429			device_printf(sc->vmbus_dev, "gpadl->chan%u "
430			    "succeeded\n", channel->offer_msg.child_rel_id);
431		}
432	}
433	return 0;
434}
435
436/*
437 * Disconnect the GPA from the target channel
438 */
439int
440hv_vmbus_channel_teardown_gpdal(struct hv_vmbus_channel *chan, uint32_t gpadl)
441{
442	struct vmbus_softc *sc = chan->vmbus_sc;
443	struct vmbus_msghc *mh;
444	struct vmbus_chanmsg_gpadl_disconn *req;
445	int error;
446
447	mh = vmbus_msghc_get(sc, sizeof(*req));
448	if (mh == NULL) {
449		device_printf(sc->vmbus_dev,
450		    "can not get msg hypercall for gpa x->chan%u\n",
451		    chan->offer_msg.child_rel_id);
452		return EBUSY;
453	}
454
455	req = vmbus_msghc_dataptr(mh);
456	req->chm_hdr.chm_type = VMBUS_CHANMSG_TYPE_GPADL_DISCONN;
457	req->chm_chanid = chan->offer_msg.child_rel_id;
458	req->chm_gpadl = gpadl;
459
460	error = vmbus_msghc_exec(sc, mh);
461	if (error) {
462		device_printf(sc->vmbus_dev,
463		    "gpa x->chan%u msg hypercall exec failed: %d\n",
464		    chan->offer_msg.child_rel_id, error);
465		vmbus_msghc_put(sc, mh);
466		return error;
467	}
468
469	vmbus_msghc_wait_result(sc, mh);
470	/* Discard result; no useful information */
471	vmbus_msghc_put(sc, mh);
472
473	return 0;
474}
475
476static void
477hv_vmbus_channel_close_internal(hv_vmbus_channel *channel)
478{
479	struct vmbus_softc *sc = channel->vmbus_sc;
480	struct vmbus_msghc *mh;
481	struct vmbus_chanmsg_chclose *req;
482	struct taskqueue *rxq = channel->rxq;
483	int error;
484
485	channel->state = HV_CHANNEL_OPEN_STATE;
486	sysctl_ctx_free(&channel->ch_sysctl_ctx);
487
488	/*
489	 * set rxq to NULL to avoid more requests be scheduled
490	 */
491	channel->rxq = NULL;
492	taskqueue_drain(rxq, &channel->channel_task);
493	channel->on_channel_callback = NULL;
494
495	/**
496	 * Send a closing message
497	 */
498
499	mh = vmbus_msghc_get(sc, sizeof(*req));
500	if (mh == NULL) {
501		device_printf(sc->vmbus_dev,
502		    "can not get msg hypercall for chclose(chan%u)\n",
503		    channel->offer_msg.child_rel_id);
504		return;
505	}
506
507	req = vmbus_msghc_dataptr(mh);
508	req->chm_hdr.chm_type = VMBUS_CHANMSG_TYPE_CHCLOSE;
509	req->chm_chanid = channel->offer_msg.child_rel_id;
510
511	error = vmbus_msghc_exec_noresult(mh);
512	vmbus_msghc_put(sc, mh);
513
514	if (error) {
515		device_printf(sc->vmbus_dev,
516		    "chclose(chan%u) msg hypercall exec failed: %d\n",
517		    channel->offer_msg.child_rel_id, error);
518		return;
519	} else if (bootverbose) {
520		device_printf(sc->vmbus_dev, "close chan%u\n",
521		    channel->offer_msg.child_rel_id);
522	}
523
524	/* Tear down the gpadl for the channel's ring buffer */
525	if (channel->ring_buffer_gpadl_handle) {
526		hv_vmbus_channel_teardown_gpdal(channel,
527			channel->ring_buffer_gpadl_handle);
528	}
529
530	/* TODO: Send a msg to release the childRelId */
531
532	/* cleanup the ring buffers for this channel */
533	hv_ring_buffer_cleanup(&channel->outbound);
534	hv_ring_buffer_cleanup(&channel->inbound);
535
536	contigfree(channel->ring_buffer_pages, channel->ring_buffer_size,
537	    M_DEVBUF);
538}
539
540/**
541 * @brief Close the specified channel
542 */
543void
544hv_vmbus_channel_close(hv_vmbus_channel *channel)
545{
546	hv_vmbus_channel*	sub_channel;
547
548	if (channel->primary_channel != NULL) {
549		/*
550		 * We only close multi-channels when the primary is
551		 * closed.
552		 */
553		return;
554	}
555
556	/*
557	 * Close all multi-channels first.
558	 */
559	TAILQ_FOREACH(sub_channel, &channel->sc_list_anchor,
560	    sc_list_entry) {
561		if (sub_channel->state != HV_CHANNEL_OPENED_STATE)
562			continue;
563		hv_vmbus_channel_close_internal(sub_channel);
564	}
565	/*
566	 * Then close the primary channel.
567	 */
568	hv_vmbus_channel_close_internal(channel);
569}
570
571/**
572 * @brief Send the specified buffer on the given channel
573 */
574int
575hv_vmbus_channel_send_packet(
576	hv_vmbus_channel*	channel,
577	void*			buffer,
578	uint32_t		buffer_len,
579	uint64_t		request_id,
580	hv_vmbus_packet_type	type,
581	uint32_t		flags)
582{
583	int			ret = 0;
584	hv_vm_packet_descriptor	desc;
585	uint32_t		packet_len;
586	uint64_t		aligned_data;
587	uint32_t		packet_len_aligned;
588	boolean_t		need_sig;
589	hv_vmbus_sg_buffer_list	buffer_list[3];
590
591	packet_len = sizeof(hv_vm_packet_descriptor) + buffer_len;
592	packet_len_aligned = HV_ALIGN_UP(packet_len, sizeof(uint64_t));
593	aligned_data = 0;
594
595	/* Setup the descriptor */
596	desc.type = type;   /* HV_VMBUS_PACKET_TYPE_DATA_IN_BAND;             */
597	desc.flags = flags; /* HV_VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED */
598			    /* in 8-bytes granularity */
599	desc.data_offset8 = sizeof(hv_vm_packet_descriptor) >> 3;
600	desc.length8 = (uint16_t) (packet_len_aligned >> 3);
601	desc.transaction_id = request_id;
602
603	buffer_list[0].data = &desc;
604	buffer_list[0].length = sizeof(hv_vm_packet_descriptor);
605
606	buffer_list[1].data = buffer;
607	buffer_list[1].length = buffer_len;
608
609	buffer_list[2].data = &aligned_data;
610	buffer_list[2].length = packet_len_aligned - packet_len;
611
612	ret = hv_ring_buffer_write(&channel->outbound, buffer_list, 3,
613	    &need_sig);
614
615	/* TODO: We should determine if this is optional */
616	if (ret == 0 && need_sig) {
617		vmbus_channel_set_event(channel);
618	}
619
620	return (ret);
621}
622
623/**
624 * @brief Send a range of single-page buffer packets using
625 * a GPADL Direct packet type
626 */
627int
628hv_vmbus_channel_send_packet_pagebuffer(
629	hv_vmbus_channel*	channel,
630	hv_vmbus_page_buffer	page_buffers[],
631	uint32_t		page_count,
632	void*			buffer,
633	uint32_t		buffer_len,
634	uint64_t		request_id)
635{
636
637	int					ret = 0;
638	boolean_t				need_sig;
639	uint32_t				packet_len;
640	uint32_t				page_buflen;
641	uint32_t				packetLen_aligned;
642	hv_vmbus_sg_buffer_list			buffer_list[4];
643	hv_vmbus_channel_packet_page_buffer	desc;
644	uint32_t				descSize;
645	uint64_t				alignedData = 0;
646
647	if (page_count > HV_MAX_PAGE_BUFFER_COUNT)
648		return (EINVAL);
649
650	/*
651	 * Adjust the size down since hv_vmbus_channel_packet_page_buffer
652	 *  is the largest size we support
653	 */
654	descSize = __offsetof(hv_vmbus_channel_packet_page_buffer, range);
655	page_buflen = sizeof(hv_vmbus_page_buffer) * page_count;
656	packet_len = descSize + page_buflen + buffer_len;
657	packetLen_aligned = HV_ALIGN_UP(packet_len, sizeof(uint64_t));
658
659	/* Setup the descriptor */
660	desc.type = HV_VMBUS_PACKET_TYPE_DATA_USING_GPA_DIRECT;
661	desc.flags = HV_VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
662	/* in 8-bytes granularity */
663	desc.data_offset8 = (descSize + page_buflen) >> 3;
664	desc.length8 = (uint16_t) (packetLen_aligned >> 3);
665	desc.transaction_id = request_id;
666	desc.range_count = page_count;
667
668	buffer_list[0].data = &desc;
669	buffer_list[0].length = descSize;
670
671	buffer_list[1].data = page_buffers;
672	buffer_list[1].length = page_buflen;
673
674	buffer_list[2].data = buffer;
675	buffer_list[2].length = buffer_len;
676
677	buffer_list[3].data = &alignedData;
678	buffer_list[3].length = packetLen_aligned - packet_len;
679
680	ret = hv_ring_buffer_write(&channel->outbound, buffer_list, 4,
681	    &need_sig);
682
683	/* TODO: We should determine if this is optional */
684	if (ret == 0 && need_sig) {
685		vmbus_channel_set_event(channel);
686	}
687
688	return (ret);
689}
690
691/**
692 * @brief Send a multi-page buffer packet using a GPADL Direct packet type
693 */
694int
695hv_vmbus_channel_send_packet_multipagebuffer(
696	hv_vmbus_channel*		channel,
697	hv_vmbus_multipage_buffer*	multi_page_buffer,
698	void*				buffer,
699	uint32_t			buffer_len,
700	uint64_t			request_id)
701{
702
703	int			ret = 0;
704	uint32_t		desc_size;
705	boolean_t		need_sig;
706	uint32_t		packet_len;
707	uint32_t		packet_len_aligned;
708	uint32_t		pfn_count;
709	uint64_t		aligned_data = 0;
710	hv_vmbus_sg_buffer_list	buffer_list[3];
711	hv_vmbus_channel_packet_multipage_buffer desc;
712
713	pfn_count =
714	    HV_NUM_PAGES_SPANNED(
715		    multi_page_buffer->offset,
716		    multi_page_buffer->length);
717
718	if ((pfn_count == 0) || (pfn_count > HV_MAX_MULTIPAGE_BUFFER_COUNT))
719	    return (EINVAL);
720	/*
721	 * Adjust the size down since hv_vmbus_channel_packet_multipage_buffer
722	 * is the largest size we support
723	 */
724	desc_size =
725	    sizeof(hv_vmbus_channel_packet_multipage_buffer) -
726		    ((HV_MAX_MULTIPAGE_BUFFER_COUNT - pfn_count) *
727			sizeof(uint64_t));
728	packet_len = desc_size + buffer_len;
729	packet_len_aligned = HV_ALIGN_UP(packet_len, sizeof(uint64_t));
730
731	/*
732	 * Setup the descriptor
733	 */
734	desc.type = HV_VMBUS_PACKET_TYPE_DATA_USING_GPA_DIRECT;
735	desc.flags = HV_VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
736	desc.data_offset8 = desc_size >> 3; /* in 8-bytes granularity */
737	desc.length8 = (uint16_t) (packet_len_aligned >> 3);
738	desc.transaction_id = request_id;
739	desc.range_count = 1;
740
741	desc.range.length = multi_page_buffer->length;
742	desc.range.offset = multi_page_buffer->offset;
743
744	memcpy(desc.range.pfn_array, multi_page_buffer->pfn_array,
745		pfn_count * sizeof(uint64_t));
746
747	buffer_list[0].data = &desc;
748	buffer_list[0].length = desc_size;
749
750	buffer_list[1].data = buffer;
751	buffer_list[1].length = buffer_len;
752
753	buffer_list[2].data = &aligned_data;
754	buffer_list[2].length = packet_len_aligned - packet_len;
755
756	ret = hv_ring_buffer_write(&channel->outbound, buffer_list, 3,
757	    &need_sig);
758
759	/* TODO: We should determine if this is optional */
760	if (ret == 0 && need_sig) {
761	    vmbus_channel_set_event(channel);
762	}
763
764	return (ret);
765}
766
767/**
768 * @brief Retrieve the user packet on the specified channel
769 */
770int
771hv_vmbus_channel_recv_packet(
772	hv_vmbus_channel*	channel,
773	void*			Buffer,
774	uint32_t		buffer_len,
775	uint32_t*		buffer_actual_len,
776	uint64_t*		request_id)
777{
778	int			ret;
779	uint32_t		user_len;
780	uint32_t		packet_len;
781	hv_vm_packet_descriptor	desc;
782
783	*buffer_actual_len = 0;
784	*request_id = 0;
785
786	ret = hv_ring_buffer_peek(&channel->inbound, &desc,
787		sizeof(hv_vm_packet_descriptor));
788	if (ret != 0)
789		return (0);
790
791	packet_len = desc.length8 << 3;
792	user_len = packet_len - (desc.data_offset8 << 3);
793
794	*buffer_actual_len = user_len;
795
796	if (user_len > buffer_len)
797		return (EINVAL);
798
799	*request_id = desc.transaction_id;
800
801	/* Copy over the packet to the user buffer */
802	ret = hv_ring_buffer_read(&channel->inbound, Buffer, user_len,
803		(desc.data_offset8 << 3));
804
805	return (0);
806}
807
808/**
809 * @brief Retrieve the raw packet on the specified channel
810 */
811int
812hv_vmbus_channel_recv_packet_raw(
813	hv_vmbus_channel*	channel,
814	void*			buffer,
815	uint32_t		buffer_len,
816	uint32_t*		buffer_actual_len,
817	uint64_t*		request_id)
818{
819	int		ret;
820	uint32_t	packetLen;
821	hv_vm_packet_descriptor	desc;
822
823	*buffer_actual_len = 0;
824	*request_id = 0;
825
826	ret = hv_ring_buffer_peek(
827		&channel->inbound, &desc,
828		sizeof(hv_vm_packet_descriptor));
829
830	if (ret != 0)
831	    return (0);
832
833	packetLen = desc.length8 << 3;
834	*buffer_actual_len = packetLen;
835
836	if (packetLen > buffer_len)
837	    return (ENOBUFS);
838
839	*request_id = desc.transaction_id;
840
841	/* Copy over the entire packet to the user buffer */
842	ret = hv_ring_buffer_read(&channel->inbound, buffer, packetLen, 0);
843
844	return (0);
845}
846
847
848/**
849 * Process a channel event notification
850 */
851static void
852VmbusProcessChannelEvent(void* context, int pending)
853{
854	void* arg;
855	uint32_t bytes_to_read;
856	hv_vmbus_channel* channel = (hv_vmbus_channel*)context;
857	boolean_t is_batched_reading;
858
859	if (channel->on_channel_callback != NULL) {
860		arg = channel->channel_callback_context;
861		is_batched_reading = channel->batched_reading;
862		/*
863		 * Optimize host to guest signaling by ensuring:
864		 * 1. While reading the channel, we disable interrupts from
865		 *    host.
866		 * 2. Ensure that we process all posted messages from the host
867		 *    before returning from this callback.
868		 * 3. Once we return, enable signaling from the host. Once this
869		 *    state is set we check to see if additional packets are
870		 *    available to read. In this case we repeat the process.
871		 */
872		do {
873			if (is_batched_reading)
874				hv_ring_buffer_read_begin(&channel->inbound);
875
876			channel->on_channel_callback(arg);
877
878			if (is_batched_reading)
879				bytes_to_read =
880				    hv_ring_buffer_read_end(&channel->inbound);
881			else
882				bytes_to_read = 0;
883		} while (is_batched_reading && (bytes_to_read != 0));
884	}
885}
886