vmbus.h revision 308517
1/*-
2 * Copyright (c) 2016 Microsoft Corp.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice unmodified, this list of conditions, and the following
10 *    disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 *
26 * $FreeBSD: stable/11/sys/dev/hyperv/include/vmbus.h 308517 2016-11-11 08:43:34Z sephe $
27 */
28
29#ifndef _VMBUS_H_
30#define _VMBUS_H_
31
32#include <sys/param.h>
33#include <sys/bus.h>
34
35/*
36 * VMBUS version is 32 bit, upper 16 bit for major_number and lower
37 * 16 bit for minor_number.
38 *
39 * 0.13  --  Windows Server 2008
40 * 1.1   --  Windows 7
41 * 2.4   --  Windows 8
42 * 3.0   --  Windows 8.1
43 */
44#define VMBUS_VERSION_WS2008		((0 << 16) | (13))
45#define VMBUS_VERSION_WIN7		((1 << 16) | (1))
46#define VMBUS_VERSION_WIN8		((2 << 16) | (4))
47#define VMBUS_VERSION_WIN8_1		((3 << 16) | (0))
48
49#define VMBUS_VERSION_MAJOR(ver)	(((uint32_t)(ver)) >> 16)
50#define VMBUS_VERSION_MINOR(ver)	(((uint32_t)(ver)) & 0xffff)
51
52/*
53 * GPA stuffs.
54 */
55struct vmbus_gpa_range {
56	uint32_t	gpa_len;
57	uint32_t	gpa_ofs;
58	uint64_t	gpa_page[0];
59} __packed;
60
61/* This is actually vmbus_gpa_range.gpa_page[1] */
62struct vmbus_gpa {
63	uint32_t	gpa_len;
64	uint32_t	gpa_ofs;
65	uint64_t	gpa_page;
66} __packed;
67
68#define VMBUS_CHANPKT_SIZE_SHIFT	3
69
70#define VMBUS_CHANPKT_GETLEN(pktlen)	\
71	(((int)(pktlen)) << VMBUS_CHANPKT_SIZE_SHIFT)
72
73struct vmbus_chanpkt_hdr {
74	uint16_t	cph_type;	/* VMBUS_CHANPKT_TYPE_ */
75	uint16_t	cph_hlen;	/* header len, in 8 bytes */
76	uint16_t	cph_tlen;	/* total len, in 8 bytes */
77	uint16_t	cph_flags;	/* VMBUS_CHANPKT_FLAG_ */
78	uint64_t	cph_xactid;
79} __packed;
80
81#define VMBUS_CHANPKT_TYPE_INBAND	0x0006
82#define VMBUS_CHANPKT_TYPE_RXBUF	0x0007
83#define VMBUS_CHANPKT_TYPE_GPA		0x0009
84#define VMBUS_CHANPKT_TYPE_COMP		0x000b
85
86#define VMBUS_CHANPKT_FLAG_NONE		0
87#define VMBUS_CHANPKT_FLAG_RC		0x0001	/* report completion */
88
89#define VMBUS_CHANPKT_CONST_DATA(pkt)		\
90	(const void *)((const uint8_t *)(pkt) +	\
91	VMBUS_CHANPKT_GETLEN((pkt)->cph_hlen))
92
93/* Include padding */
94#define VMBUS_CHANPKT_DATALEN(pkt)		\
95	(VMBUS_CHANPKT_GETLEN((pkt)->cph_tlen) -\
96	 VMBUS_CHANPKT_GETLEN((pkt)->cph_hlen))
97
98struct vmbus_rxbuf_desc {
99	uint32_t	rb_len;
100	uint32_t	rb_ofs;
101} __packed;
102
103struct vmbus_chanpkt_rxbuf {
104	struct vmbus_chanpkt_hdr cp_hdr;
105	uint16_t	cp_rxbuf_id;
106	uint16_t	cp_rsvd;
107	uint32_t	cp_rxbuf_cnt;
108	struct vmbus_rxbuf_desc cp_rxbuf[];
109} __packed;
110
111struct vmbus_chan_br {
112	void		*cbr;
113	bus_addr_t	cbr_paddr;
114	int		cbr_txsz;
115	int		cbr_rxsz;
116};
117
118struct vmbus_channel;
119struct hyperv_guid;
120struct task;
121struct taskqueue;
122
123typedef void	(*vmbus_chan_callback_t)(struct vmbus_channel *, void *);
124
125static __inline struct vmbus_channel *
126vmbus_get_channel(device_t dev)
127{
128	return device_get_ivars(dev);
129}
130
131int		vmbus_chan_open(struct vmbus_channel *chan,
132		    int txbr_size, int rxbr_size, const void *udata, int udlen,
133		    vmbus_chan_callback_t cb, void *cbarg);
134int		vmbus_chan_open_br(struct vmbus_channel *chan,
135		    const struct vmbus_chan_br *cbr, const void *udata,
136		    int udlen, vmbus_chan_callback_t cb, void *cbarg);
137void		vmbus_chan_close(struct vmbus_channel *chan);
138void		vmbus_chan_intr_drain(struct vmbus_channel *chan);
139void		vmbus_chan_run_task(struct vmbus_channel *chan,
140		    struct task *task);
141
142int		vmbus_chan_gpadl_connect(struct vmbus_channel *chan,
143		    bus_addr_t paddr, int size, uint32_t *gpadl);
144int		vmbus_chan_gpadl_disconnect(struct vmbus_channel *chan,
145		    uint32_t gpadl);
146
147void		vmbus_chan_cpu_set(struct vmbus_channel *chan, int cpu);
148void		vmbus_chan_cpu_rr(struct vmbus_channel *chan);
149struct vmbus_channel *
150		vmbus_chan_cpu2chan(struct vmbus_channel *chan, int cpu);
151void		vmbus_chan_set_readbatch(struct vmbus_channel *chan, bool on);
152
153struct vmbus_channel **
154		vmbus_subchan_get(struct vmbus_channel *pri_chan,
155		    int subchan_cnt);
156void		vmbus_subchan_rel(struct vmbus_channel **subchan,
157		    int subchan_cnt);
158void		vmbus_subchan_drain(struct vmbus_channel *pri_chan);
159
160int		vmbus_chan_recv(struct vmbus_channel *chan, void *data, int *dlen,
161		    uint64_t *xactid);
162int		vmbus_chan_recv_pkt(struct vmbus_channel *chan,
163		    struct vmbus_chanpkt_hdr *pkt, int *pktlen);
164
165int		vmbus_chan_send(struct vmbus_channel *chan, uint16_t type,
166		    uint16_t flags, void *data, int dlen, uint64_t xactid);
167int		vmbus_chan_send_sglist(struct vmbus_channel *chan,
168		    struct vmbus_gpa sg[], int sglen, void *data, int dlen,
169		    uint64_t xactid);
170int		vmbus_chan_send_prplist(struct vmbus_channel *chan,
171		    struct vmbus_gpa_range *prp, int prp_cnt, void *data,
172		    int dlen, uint64_t xactid);
173
174uint32_t	vmbus_chan_id(const struct vmbus_channel *chan);
175uint32_t	vmbus_chan_subidx(const struct vmbus_channel *chan);
176bool		vmbus_chan_is_primary(const struct vmbus_channel *chan);
177const struct hyperv_guid *
178		vmbus_chan_guid_inst(const struct vmbus_channel *chan);
179int		vmbus_chan_prplist_nelem(int br_size, int prpcnt_max,
180		    int dlen_max);
181bool		vmbus_chan_rx_empty(const struct vmbus_channel *chan);
182bool		vmbus_chan_tx_empty(const struct vmbus_channel *chan);
183struct taskqueue *
184		vmbus_chan_mgmt_tq(const struct vmbus_channel *chan);
185
186#endif	/* !_VMBUS_H_ */
187