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