xref: /illumos-gate/usr/src/uts/common/io/vioif/vioif.h (revision d4221574)
1f8296c60SJoshua M. Clulow /*
2f8296c60SJoshua M. Clulow  * This file and its contents are supplied under the terms of the
3f8296c60SJoshua M. Clulow  * Common Development and Distribution License ("CDDL"), version 1.0.
4f8296c60SJoshua M. Clulow  * You may only use this file in accordance with the terms of version
5f8296c60SJoshua M. Clulow  * 1.0 of the CDDL.
6f8296c60SJoshua M. Clulow  *
7f8296c60SJoshua M. Clulow  * A full copy of the text of the CDDL should have accompanied this
8f8296c60SJoshua M. Clulow  * source.  A copy of the CDDL is also available via the Internet at
9f8296c60SJoshua M. Clulow  * http://www.illumos.org/license/CDDL.
10f8296c60SJoshua M. Clulow  */
11f8296c60SJoshua M. Clulow 
12f8296c60SJoshua M. Clulow /*
1335d41f28SJason King  * Copyright 2021 Joyent, Inc.
14f8296c60SJoshua M. Clulow  */
15f8296c60SJoshua M. Clulow 
16f8296c60SJoshua M. Clulow /*
17f8296c60SJoshua M. Clulow  * VIRTIO NETWORK DRIVER
18f8296c60SJoshua M. Clulow  */
19f8296c60SJoshua M. Clulow 
20f8296c60SJoshua M. Clulow #ifndef _VIOIF_H
21f8296c60SJoshua M. Clulow #define	_VIOIF_H
22f8296c60SJoshua M. Clulow 
23f8296c60SJoshua M. Clulow #include "virtio.h"
24f8296c60SJoshua M. Clulow 
25f8296c60SJoshua M. Clulow #ifdef __cplusplus
26f8296c60SJoshua M. Clulow extern "C" {
27f8296c60SJoshua M. Clulow #endif
28f8296c60SJoshua M. Clulow 
29f8296c60SJoshua M. Clulow /*
30f8296c60SJoshua M. Clulow  * VIRTIO NETWORK CONFIGURATION REGISTERS
31f8296c60SJoshua M. Clulow  *
32f8296c60SJoshua M. Clulow  * These are offsets into the device-specific configuration space available
33f8296c60SJoshua M. Clulow  * through the virtio_dev_*() family of functions.
34f8296c60SJoshua M. Clulow  */
35f8296c60SJoshua M. Clulow #define	VIRTIO_NET_CONFIG_MAC		0x00	/* 48 R/W */
36f8296c60SJoshua M. Clulow #define	VIRTIO_NET_CONFIG_STATUS	0x06	/* 16 R   */
37f8296c60SJoshua M. Clulow #define	VIRTIO_NET_CONFIG_MAX_VQ_PAIRS	0x08	/* 16 R   */
38f8296c60SJoshua M. Clulow #define	VIRTIO_NET_CONFIG_MTU		0x0A	/* 16 R   */
39f8296c60SJoshua M. Clulow 
40f8296c60SJoshua M. Clulow /*
41f8296c60SJoshua M. Clulow  * VIRTIO NETWORK VIRTQUEUES
42f8296c60SJoshua M. Clulow  *
43f8296c60SJoshua M. Clulow  * Note that the control queue is only present if VIRTIO_NET_F_CTRL_VQ is
44f8296c60SJoshua M. Clulow  * negotiated with the device.
45f8296c60SJoshua M. Clulow  */
46f8296c60SJoshua M. Clulow #define	VIRTIO_NET_VIRTQ_RX		0
47f8296c60SJoshua M. Clulow #define	VIRTIO_NET_VIRTQ_TX		1
48f8296c60SJoshua M. Clulow #define	VIRTIO_NET_VIRTQ_CONTROL	2
49f8296c60SJoshua M. Clulow 
50f8296c60SJoshua M. Clulow /*
51f8296c60SJoshua M. Clulow  * VIRTIO NETWORK FEATURE BITS
52f8296c60SJoshua M. Clulow  */
53f8296c60SJoshua M. Clulow 
54f8296c60SJoshua M. Clulow /*
55f8296c60SJoshua M. Clulow  * CSUM, GUEST_CSUM:
56f8296c60SJoshua M. Clulow  *	Partial checksum support.  These features signal that the device will
57f8296c60SJoshua M. Clulow  *	accept packets with partial checksums (CSUM), and that the driver will
58f8296c60SJoshua M. Clulow  *	accept packets with partial checksums (GUEST_CSUM).  These features
59f8296c60SJoshua M. Clulow  *	combine the use of the VIRTIO_NET_HDR_F_NEEDS_CSUM flag, and the
60f8296c60SJoshua M. Clulow  *	"csum_start" and "csum_offset" fields, in the virtio net header.
61f8296c60SJoshua M. Clulow  */
62f8296c60SJoshua M. Clulow #define	VIRTIO_NET_F_CSUM		(1ULL << 0)
63f8296c60SJoshua M. Clulow #define	VIRTIO_NET_F_GUEST_CSUM		(1ULL << 1)
64f8296c60SJoshua M. Clulow 
65f8296c60SJoshua M. Clulow /*
66f8296c60SJoshua M. Clulow  * MTU:
67f8296c60SJoshua M. Clulow  *	The device offers a maximum MTU value at VIRTIO_NET_CONFIG_MTU.  If
68f8296c60SJoshua M. Clulow  *	this is not negotiated, we allow the largest possible MTU that our
69f8296c60SJoshua M. Clulow  *	buffer allocations support in case jumbo frames are tacitly supported
70f8296c60SJoshua M. Clulow  *	by the device.  The default MTU is always 1500.
71f8296c60SJoshua M. Clulow  */
72f8296c60SJoshua M. Clulow #define	VIRTIO_NET_F_MTU		(1ULL << 3)
73f8296c60SJoshua M. Clulow 
74f8296c60SJoshua M. Clulow /*
75f8296c60SJoshua M. Clulow  * MAC:
76f8296c60SJoshua M. Clulow  *	The device has an assigned primary MAC address.  If this feature bit is
77f8296c60SJoshua M. Clulow  *	not set, the driver must provide a locally assigned MAC address.  See
78f8296c60SJoshua M. Clulow  *	IEEE 802, "48-bit universal LAN MAC addresses" for more details on
79f8296c60SJoshua M. Clulow  *	assignment.
80f8296c60SJoshua M. Clulow  */
81f8296c60SJoshua M. Clulow #define	VIRTIO_NET_F_MAC		(1ULL << 5)
82f8296c60SJoshua M. Clulow 
83f8296c60SJoshua M. Clulow /*
84f8296c60SJoshua M. Clulow  * GUEST_TSO4, GUEST_TSO6, GUEST_UFO:
85f8296c60SJoshua M. Clulow  *	Inbound segmentation offload support.  These features depend on having
86f8296c60SJoshua M. Clulow  *	VIRTIO_NET_F_GUEST_CSUM and signal that the driver can accept large
87f8296c60SJoshua M. Clulow  *	combined TCP (v4 or v6) packets, or reassembled UDP fragments.
88f8296c60SJoshua M. Clulow  */
89f8296c60SJoshua M. Clulow #define	VIRTIO_NET_F_GUEST_TSO4		(1ULL << 7)
90f8296c60SJoshua M. Clulow #define	VIRTIO_NET_F_GUEST_TSO6		(1ULL << 8)
91f8296c60SJoshua M. Clulow #define	VIRTIO_NET_F_GUEST_UFO		(1ULL << 10)
92f8296c60SJoshua M. Clulow 
93f8296c60SJoshua M. Clulow /*
94f8296c60SJoshua M. Clulow  * GUEST_ECN:
95f8296c60SJoshua M. Clulow  *	Depends on either VIRTIO_NET_F_GUEST_TSO4 or VIRTIO_NET_F_GUEST_TSO6.
96f8296c60SJoshua M. Clulow  *	This feature means the driver will look for the VIRTIO_NET_HDR_GSO_ECN
97f8296c60SJoshua M. Clulow  *	bit in the "gso_type" of the virtio net header.  This bit tells the
98f8296c60SJoshua M. Clulow  *	driver that the Explicit Congestion Notification (ECN) bit was set in
99f8296c60SJoshua M. Clulow  *	the original TCP packets.
100f8296c60SJoshua M. Clulow  */
101f8296c60SJoshua M. Clulow #define	VIRTIO_NET_F_GUEST_ECN		(1ULL << 9)
102f8296c60SJoshua M. Clulow 
103f8296c60SJoshua M. Clulow /*
104f8296c60SJoshua M. Clulow  * HOST_TSO4, HOST_TSO6, HOST_UFO:
105f8296c60SJoshua M. Clulow  *	Outbound segmentation offload support.  These features depend on having
106f8296c60SJoshua M. Clulow  *	VIRTIO_NET_F_CSUM and signal that the device will accept large combined
107f8296c60SJoshua M. Clulow  *	TCP (v4 or v6) packets that require segmentation offload, or large
108f8296c60SJoshua M. Clulow  *	combined UDP packets that require fragmentation offload.
109f8296c60SJoshua M. Clulow  */
110f8296c60SJoshua M. Clulow #define	VIRTIO_NET_F_HOST_TSO4		(1ULL << 11)
111f8296c60SJoshua M. Clulow #define	VIRTIO_NET_F_HOST_TSO6		(1ULL << 12)
112f8296c60SJoshua M. Clulow #define	VIRTIO_NET_F_HOST_UFO		(1ULL << 14)
113f8296c60SJoshua M. Clulow 
114f8296c60SJoshua M. Clulow /*
115f8296c60SJoshua M. Clulow  * HOST_ECN:
116f8296c60SJoshua M. Clulow  *	Depends on either VIRTIO_NET_F_HOST_TSO4 or VIRTIO_NET_F_HOST_TSO6.
117f8296c60SJoshua M. Clulow  *	This features means the device will accept packets that both require
118f8296c60SJoshua M. Clulow  *	segmentation offload and have the Explicit Congestion Notification
119f8296c60SJoshua M. Clulow  *	(ECN) bit set.  If this feature is not present, the device must not
120f8296c60SJoshua M. Clulow  *	send large segments that require ECN to be set.
121f8296c60SJoshua M. Clulow  */
122f8296c60SJoshua M. Clulow #define	VIRTIO_NET_F_HOST_ECN		(1ULL << 13)
123f8296c60SJoshua M. Clulow 
124f8296c60SJoshua M. Clulow /*
125f8296c60SJoshua M. Clulow  * GSO:
126f8296c60SJoshua M. Clulow  *	The GSO feature is, in theory, the combination of HOST_TSO4, HOST_TSO6,
127f8296c60SJoshua M. Clulow  *	and HOST_ECN.  This is only useful for legacy devices; newer devices
128f8296c60SJoshua M. Clulow  *	should be using the more specific bits above.
129f8296c60SJoshua M. Clulow  */
130f8296c60SJoshua M. Clulow #define	VIRTIO_NET_F_GSO		(1ULL << 6)
131f8296c60SJoshua M. Clulow 
132f8296c60SJoshua M. Clulow /*
133f8296c60SJoshua M. Clulow  * MRG_RXBUF:
134f8296c60SJoshua M. Clulow  *	This feature allows the receipt of large packets without needing to
135f8296c60SJoshua M. Clulow  *	allocate large buffers.  The "virtio_net_hdr" will include an extra
136f8296c60SJoshua M. Clulow  *	value: the number of buffers to gang together.
137f8296c60SJoshua M. Clulow  */
138f8296c60SJoshua M. Clulow #define	VIRTIO_NET_F_MRG_RXBUF		(1ULL << 15)
139f8296c60SJoshua M. Clulow 
140f8296c60SJoshua M. Clulow /*
141f8296c60SJoshua M. Clulow  * STATUS:
142f8296c60SJoshua M. Clulow  *	The VIRTIO_NET_CONFIG_STATUS configuration register is available, which
143f8296c60SJoshua M. Clulow  *	allows the driver to read the link state from the device.
144f8296c60SJoshua M. Clulow  */
145f8296c60SJoshua M. Clulow #define	VIRTIO_NET_F_STATUS		(1ULL << 16)
146f8296c60SJoshua M. Clulow 
147f8296c60SJoshua M. Clulow /*
148f8296c60SJoshua M. Clulow  * CTRL_VQ, CTRL_RX, CTRL_VLAN:
149f8296c60SJoshua M. Clulow  *	These features signal that the device exposes the control queue
150f8296c60SJoshua M. Clulow  *	(VIRTIO_NET_VIRTQ_CONTROL), in the case of CTRL_VQ; and that the
151f8296c60SJoshua M. Clulow  *	control queue supports extra commands (CTRL_RX, CTRL_VLAN).
152f8296c60SJoshua M. Clulow  */
153f8296c60SJoshua M. Clulow #define	VIRTIO_NET_F_CTRL_VQ		(1ULL << 17)
154f8296c60SJoshua M. Clulow #define	VIRTIO_NET_F_CTRL_RX		(1ULL << 18)
155f8296c60SJoshua M. Clulow #define	VIRTIO_NET_F_CTRL_VLAN		(1ULL << 19)
156f8296c60SJoshua M. Clulow #define	VIRTIO_NET_F_CTRL_RX_EXTRA	(1ULL << 20)
157f8296c60SJoshua M. Clulow 
158f8296c60SJoshua M. Clulow /*
159f8296c60SJoshua M. Clulow  * These features are supported by the driver and we will request them from the
160f8296c60SJoshua M. Clulow  * device.  Note that we do not currently request GUEST_CSUM, as the driver
161f8296c60SJoshua M. Clulow  * does not presently support receiving frames with any offload features from
162f8296c60SJoshua M. Clulow  * the device.
163f8296c60SJoshua M. Clulow  */
164f8296c60SJoshua M. Clulow #define	VIRTIO_NET_WANTED_FEATURES	(VIRTIO_NET_F_CSUM |		\
165f8296c60SJoshua M. Clulow 					VIRTIO_NET_F_GSO |		\
166f8296c60SJoshua M. Clulow 					VIRTIO_NET_F_HOST_TSO4 |	\
16762366fbbSRobert Mustacchi 					VIRTIO_NET_F_HOST_TSO6 |	\
168f8296c60SJoshua M. Clulow 					VIRTIO_NET_F_HOST_ECN |		\
169f8296c60SJoshua M. Clulow 					VIRTIO_NET_F_MAC |		\
17035d41f28SJason King 					VIRTIO_NET_F_MTU |		\
17135d41f28SJason King 					VIRTIO_NET_F_CTRL_VQ |		\
17235d41f28SJason King 					VIRTIO_NET_F_CTRL_RX)
173f8296c60SJoshua M. Clulow 
174f8296c60SJoshua M. Clulow /*
175f8296c60SJoshua M. Clulow  * VIRTIO NETWORK HEADER
176f8296c60SJoshua M. Clulow  *
177f8296c60SJoshua M. Clulow  * This structure appears at the start of each transmit or receive packet
178f8296c60SJoshua M. Clulow  * buffer.
179f8296c60SJoshua M. Clulow  */
180f8296c60SJoshua M. Clulow struct virtio_net_hdr {
181f8296c60SJoshua M. Clulow 	uint8_t				vnh_flags;
182f8296c60SJoshua M. Clulow 	uint8_t				vnh_gso_type;
183f8296c60SJoshua M. Clulow 	uint16_t			vnh_hdr_len;
184f8296c60SJoshua M. Clulow 	uint16_t			vnh_gso_size;
185f8296c60SJoshua M. Clulow 	uint16_t			vnh_csum_start;
186f8296c60SJoshua M. Clulow 	uint16_t			vnh_csum_offset;
187f8296c60SJoshua M. Clulow } __packed;
188f8296c60SJoshua M. Clulow 
189f8296c60SJoshua M. Clulow /*
190f8296c60SJoshua M. Clulow  * VIRTIO NETWORK HEADER: FLAGS (vnh_flags)
191f8296c60SJoshua M. Clulow  */
192f8296c60SJoshua M. Clulow #define	VIRTIO_NET_HDR_F_NEEDS_CSUM	0x01
193f8296c60SJoshua M. Clulow 
194f8296c60SJoshua M. Clulow /*
195f8296c60SJoshua M. Clulow  * VIRTIO NETWORK HEADER: OFFLOAD OPTIONS (vnh_gso_type)
196f8296c60SJoshua M. Clulow  *
197f8296c60SJoshua M. Clulow  * Each of these is an offload type, except for the ECN value which is
198f8296c60SJoshua M. Clulow  * logically OR-ed with one of the other types.
199f8296c60SJoshua M. Clulow  */
200f8296c60SJoshua M. Clulow #define	VIRTIO_NET_HDR_GSO_NONE		0
201f8296c60SJoshua M. Clulow #define	VIRTIO_NET_HDR_GSO_TCPV4	1
202f8296c60SJoshua M. Clulow #define	VIRTIO_NET_HDR_GSO_UDP		3
203f8296c60SJoshua M. Clulow #define	VIRTIO_NET_HDR_GSO_TCPV6	4
204f8296c60SJoshua M. Clulow #define	VIRTIO_NET_HDR_GSO_ECN		0x80
205f8296c60SJoshua M. Clulow 
20635d41f28SJason King /*
20735d41f28SJason King  * VIRTIO CONTROL VIRTQUEUE HEADER
20835d41f28SJason King  *
20935d41f28SJason King  * This structure appears at the start of each control virtqueue request.
21035d41f28SJason King  */
21135d41f28SJason King struct virtio_net_ctrlq_hdr {
21235d41f28SJason King 	uint8_t		vnch_class;
21335d41f28SJason King 	uint8_t		vnch_command;
21435d41f28SJason King } __packed;
21535d41f28SJason King 
21635d41f28SJason King /*
217*d4221574SAndy Fiddaman  * Control Queue Classes
21835d41f28SJason King  */
21935d41f28SJason King #define	VIRTIO_NET_CTRL_RX		0
22035d41f28SJason King 
22135d41f28SJason King /*
22235d41f28SJason King  * CTRL_RX commands
22335d41f28SJason King  */
22435d41f28SJason King #define	VIRTIO_NET_CTRL_RX_PROMISC	0
22535d41f28SJason King #define	VIRTIO_NET_CTRL_RX_ALLMULTI	1
22635d41f28SJason King #define	VIRTIO_NET_CTRL_RX_ALLUNI	2
22735d41f28SJason King #define	VIRTIO_NET_CTRL_RX_NOMULTI	3
22835d41f28SJason King #define	VIRTIO_NET_CTRL_RX_NOUNI	4
22935d41f28SJason King #define	VIRTIO_NET_CTRL_RX_NOBCAST	5
23035d41f28SJason King 
23135d41f28SJason King /*
23235d41f28SJason King  * Control queue ack values
23335d41f28SJason King  */
23435d41f28SJason King #define	VIRTIO_NET_CQ_OK		0
23535d41f28SJason King #define	VIRTIO_NET_CQ_ERR		1
23635d41f28SJason King 
237f8296c60SJoshua M. Clulow 
238f8296c60SJoshua M. Clulow /*
239f8296c60SJoshua M. Clulow  * DRIVER PARAMETERS
240f8296c60SJoshua M. Clulow  */
241f8296c60SJoshua M. Clulow 
242f8296c60SJoshua M. Clulow /*
243f8296c60SJoshua M. Clulow  * At attach, we allocate a fixed pool of buffers for receipt and transmission
244f8296c60SJoshua M. Clulow  * of frames.  The maximum number of buffers of each type that we will allocate
245f8296c60SJoshua M. Clulow  * is specified here.  If the ring size is smaller than this number, we will
246f8296c60SJoshua M. Clulow  * use the ring size instead.
247f8296c60SJoshua M. Clulow  */
248f8296c60SJoshua M. Clulow #define	VIRTIO_NET_TX_BUFS		256
249f8296c60SJoshua M. Clulow #define	VIRTIO_NET_RX_BUFS		256
250f8296c60SJoshua M. Clulow 
25135d41f28SJason King /*
25235d41f28SJason King  * Initially, only use a single buf for control queue requests (when
25335d41f28SJason King  * present). If this becomes a bottleneck, we can simply increase this
25435d41f28SJason King  * value as necessary.
25535d41f28SJason King  */
25635d41f28SJason King #define	VIRTIO_NET_CTRL_BUFS		1
25735d41f28SJason King 
258f8296c60SJoshua M. Clulow /*
259f8296c60SJoshua M. Clulow  * The virtio net header and the first buffer segment share the same DMA
260f8296c60SJoshua M. Clulow  * allocation.  We round up the virtio header size to a multiple of 4 and add 2
261f8296c60SJoshua M. Clulow  * bytes so that the IP header, which starts immediately after the 14 or 18
262f8296c60SJoshua M. Clulow  * byte Ethernet header, is then correctly aligned:
263f8296c60SJoshua M. Clulow  *
264f8296c60SJoshua M. Clulow  *   0                10      16   18                              32/36
265f8296c60SJoshua M. Clulow  *   | virtio_net_hdr | %4==0 | +2 | Ethernet header (14/18 bytes) | IPv4 ...
266f8296c60SJoshua M. Clulow  *
267f8296c60SJoshua M. Clulow  * Note that for this to work correctly, the DMA allocation must also be 4 byte
268f8296c60SJoshua M. Clulow  * aligned.
269f8296c60SJoshua M. Clulow  */
270f8296c60SJoshua M. Clulow #define	VIOIF_HEADER_ALIGN		4
271f8296c60SJoshua M. Clulow #define	VIOIF_HEADER_SKIP		(P2ROUNDUP( \
272f8296c60SJoshua M. Clulow 					    sizeof (struct virtio_net_hdr), \
273f8296c60SJoshua M. Clulow 					    VIOIF_HEADER_ALIGN) + 2)
274f8296c60SJoshua M. Clulow 
275f8296c60SJoshua M. Clulow /*
276f8296c60SJoshua M. Clulow  * Given we are not negotiating VIRTIO_NET_F_MRG_RXBUF, the specification says
277f8296c60SJoshua M. Clulow  * we must be able to accept a 1514 byte packet, or if any segmentation offload
278f8296c60SJoshua M. Clulow  * features have been negotiated a 65550 byte packet.  To keep things simple,
279f8296c60SJoshua M. Clulow  * we'll assume segmentation offload is possible in most cases.  In addition to
280f8296c60SJoshua M. Clulow  * the packet payload, we need to account for the Ethernet header and the
281f8296c60SJoshua M. Clulow  * virtio_net_hdr.
282f8296c60SJoshua M. Clulow  */
283f8296c60SJoshua M. Clulow #define	VIOIF_RX_DATA_SIZE		65550
284f8296c60SJoshua M. Clulow #define	VIOIF_RX_BUF_SIZE		(VIOIF_RX_DATA_SIZE + \
285f8296c60SJoshua M. Clulow 					    sizeof (struct ether_header) + \
286f8296c60SJoshua M. Clulow 					    VIOIF_HEADER_SKIP)
287f8296c60SJoshua M. Clulow 
288f8296c60SJoshua M. Clulow /*
289f8296c60SJoshua M. Clulow  * If we assume that a large allocation will probably have mostly 4K page sized
290f8296c60SJoshua M. Clulow  * cookies, 64 segments allows us 256KB for a single frame.  We're in control
291f8296c60SJoshua M. Clulow  * of the allocation we use for receive buffers, so this value only has an
292f8296c60SJoshua M. Clulow  * impact on the length of chain we're able to create for external transmit
293f8296c60SJoshua M. Clulow  * buffer mappings.
294f8296c60SJoshua M. Clulow  */
295f8296c60SJoshua M. Clulow #define	VIOIF_MAX_SEGS			64
296f8296c60SJoshua M. Clulow 
297f8296c60SJoshua M. Clulow /*
298f8296c60SJoshua M. Clulow  * We pre-allocate a reasonably large buffer to copy small packets
299f8296c60SJoshua M. Clulow  * there. Bigger packets are mapped, packets with multiple
300f8296c60SJoshua M. Clulow  * cookies are mapped as indirect buffers.
301f8296c60SJoshua M. Clulow  */
302f8296c60SJoshua M. Clulow #define	VIOIF_TX_INLINE_SIZE		(2 * 1024)
303f8296c60SJoshua M. Clulow 
30435d41f28SJason King /*
30535d41f28SJason King  * Control queue messages are very small. This is a rather arbitrary small
30635d41f28SJason King  * bufer size that should be sufficiently large for any control queue
30735d41f28SJason King  * messages we will send.
30835d41f28SJason King  */
30935d41f28SJason King #define	VIOIF_CTRL_SIZE			256
310f8296c60SJoshua M. Clulow 
311f8296c60SJoshua M. Clulow /*
312f8296c60SJoshua M. Clulow  * TYPE DEFINITIONS
313f8296c60SJoshua M. Clulow  */
314f8296c60SJoshua M. Clulow 
315f8296c60SJoshua M. Clulow typedef struct vioif vioif_t;
316f8296c60SJoshua M. Clulow 
317f8296c60SJoshua M. Clulow /*
318f8296c60SJoshua M. Clulow  * Receive buffers are allocated in advance as a combination of DMA memory and
319f8296c60SJoshua M. Clulow  * a descriptor chain.  Receive buffers can be loaned to the networking stack
320f8296c60SJoshua M. Clulow  * to avoid copying, and this object contains the free routine to pass to
321f8296c60SJoshua M. Clulow  * desballoc().
322f8296c60SJoshua M. Clulow  *
323f8296c60SJoshua M. Clulow  * When receive buffers are not in use, they are linked into the per-instance
324f8296c60SJoshua M. Clulow  * free list, "vif_rxbufs" via "rb_link".  Under normal conditions, we expect
325f8296c60SJoshua M. Clulow  * the free list to be empty much of the time; most buffers will be in the ring
326f8296c60SJoshua M. Clulow  * or on loan.
327f8296c60SJoshua M. Clulow  */
328f8296c60SJoshua M. Clulow typedef struct vioif_rxbuf {
329f8296c60SJoshua M. Clulow 	vioif_t				*rb_vioif;
330f8296c60SJoshua M. Clulow 	frtn_t				rb_frtn;
331f8296c60SJoshua M. Clulow 
332f8296c60SJoshua M. Clulow 	virtio_dma_t			*rb_dma;
333f8296c60SJoshua M. Clulow 	virtio_chain_t			*rb_chain;
334f8296c60SJoshua M. Clulow 
335f8296c60SJoshua M. Clulow 	list_node_t			rb_link;
336f8296c60SJoshua M. Clulow } vioif_rxbuf_t;
337f8296c60SJoshua M. Clulow 
33835d41f28SJason King typedef struct vioif_ctrlbuf {
33935d41f28SJason King 	vioif_t				*cb_vioif;
34035d41f28SJason King 
34135d41f28SJason King 	virtio_dma_t			*cb_dma;
34235d41f28SJason King 	virtio_chain_t			*cb_chain;
34335d41f28SJason King 
34435d41f28SJason King 	list_node_t			cb_link;
34535d41f28SJason King } vioif_ctrlbuf_t;
34635d41f28SJason King 
347f8296c60SJoshua M. Clulow /*
348f8296c60SJoshua M. Clulow  * Transmit buffers are also allocated in advance.  DMA memory is allocated for
349f8296c60SJoshua M. Clulow  * the virtio net header, and to hold small packets.  Larger packets are mapped
350f8296c60SJoshua M. Clulow  * from storage loaned to the driver by the network stack.
351f8296c60SJoshua M. Clulow  *
352f8296c60SJoshua M. Clulow  * When transmit buffers are not in use, they are linked into the per-instance
353f8296c60SJoshua M. Clulow  * free list, "vif_txbufs" via "tb_link".
354f8296c60SJoshua M. Clulow  */
355f8296c60SJoshua M. Clulow typedef struct vioif_txbuf {
356f8296c60SJoshua M. Clulow 	mblk_t				*tb_mp;
357f8296c60SJoshua M. Clulow 
358f8296c60SJoshua M. Clulow 	/*
359f8296c60SJoshua M. Clulow 	 * Inline buffer space (VIOIF_TX_INLINE_SIZE) for storage of the virtio
360f8296c60SJoshua M. Clulow 	 * net header, and to hold copied (rather than mapped) packet data.
361f8296c60SJoshua M. Clulow 	 */
362f8296c60SJoshua M. Clulow 	virtio_dma_t			*tb_dma;
363f8296c60SJoshua M. Clulow 	virtio_chain_t			*tb_chain;
364f8296c60SJoshua M. Clulow 
365f8296c60SJoshua M. Clulow 	/*
366f8296c60SJoshua M. Clulow 	 * External buffer mapping.  The capacity is fixed at allocation time,
367f8296c60SJoshua M. Clulow 	 * and "tb_ndmaext" tracks the current number of mappings.
368f8296c60SJoshua M. Clulow 	 */
369f8296c60SJoshua M. Clulow 	virtio_dma_t			**tb_dmaext;
370f8296c60SJoshua M. Clulow 	uint_t				tb_dmaext_capacity;
371f8296c60SJoshua M. Clulow 	uint_t				tb_ndmaext;
372f8296c60SJoshua M. Clulow 
373f8296c60SJoshua M. Clulow 	list_node_t			tb_link;
374f8296c60SJoshua M. Clulow } vioif_txbuf_t;
375f8296c60SJoshua M. Clulow 
376f8296c60SJoshua M. Clulow typedef enum vioif_runstate {
377f8296c60SJoshua M. Clulow 	VIOIF_RUNSTATE_STOPPED = 1,
378f8296c60SJoshua M. Clulow 	VIOIF_RUNSTATE_STOPPING,
379f8296c60SJoshua M. Clulow 	VIOIF_RUNSTATE_RUNNING
380f8296c60SJoshua M. Clulow } vioif_runstate_t;
381f8296c60SJoshua M. Clulow 
382f8296c60SJoshua M. Clulow /*
383f8296c60SJoshua M. Clulow  * Per-instance driver object.
384f8296c60SJoshua M. Clulow  */
385f8296c60SJoshua M. Clulow struct vioif {
386f8296c60SJoshua M. Clulow 	dev_info_t			*vif_dip;
387f8296c60SJoshua M. Clulow 	virtio_t			*vif_virtio;
388f8296c60SJoshua M. Clulow 
389f8296c60SJoshua M. Clulow 	kmutex_t			vif_mutex;
390f8296c60SJoshua M. Clulow 
391f8296c60SJoshua M. Clulow 	/*
392f8296c60SJoshua M. Clulow 	 * The NIC is considered RUNNING between the mc_start(9E) and
393f8296c60SJoshua M. Clulow 	 * mc_stop(9E) calls.  Otherwise it is STOPPING (while draining
394f8296c60SJoshua M. Clulow 	 * resources) then STOPPED.  When not RUNNING, we will drop incoming
395f8296c60SJoshua M. Clulow 	 * frames and refuse to insert more receive buffers into the receive
396f8296c60SJoshua M. Clulow 	 * queue.
397f8296c60SJoshua M. Clulow 	 */
398f8296c60SJoshua M. Clulow 	vioif_runstate_t		vif_runstate;
399f8296c60SJoshua M. Clulow 
400f8296c60SJoshua M. Clulow 	mac_handle_t			vif_mac_handle;
401f8296c60SJoshua M. Clulow 
402f8296c60SJoshua M. Clulow 	virtio_queue_t			*vif_rx_vq;
403f8296c60SJoshua M. Clulow 	virtio_queue_t			*vif_tx_vq;
40435d41f28SJason King 	virtio_queue_t			*vif_ctrl_vq;
405f8296c60SJoshua M. Clulow 
406f8296c60SJoshua M. Clulow 	/* TX virtqueue management resources */
407f8296c60SJoshua M. Clulow 	boolean_t			vif_tx_corked;
408f8296c60SJoshua M. Clulow 	boolean_t			vif_tx_drain;
409f8296c60SJoshua M. Clulow 	timeout_id_t			vif_tx_reclaim_tid;
410f8296c60SJoshua M. Clulow 
411f8296c60SJoshua M. Clulow 	/*
412f8296c60SJoshua M. Clulow 	 * Configured offload features:
413f8296c60SJoshua M. Clulow 	 */
414f8296c60SJoshua M. Clulow 	unsigned int			vif_tx_csum:1;
415f8296c60SJoshua M. Clulow 	unsigned int			vif_tx_tso4:1;
41662366fbbSRobert Mustacchi 	unsigned int			vif_tx_tso6:1;
417f8296c60SJoshua M. Clulow 
418f8296c60SJoshua M. Clulow 	/*
419f8296c60SJoshua M. Clulow 	 * For debugging, it is useful to know whether the MAC address we
420f8296c60SJoshua M. Clulow 	 * are using came from the host (via VIRTIO_NET_CONFIG_MAC) or
421f8296c60SJoshua M. Clulow 	 * was otherwise generated or set from within the guest.
422f8296c60SJoshua M. Clulow 	 */
423f8296c60SJoshua M. Clulow 	unsigned int			vif_mac_from_host:1;
424f8296c60SJoshua M. Clulow 
42535d41f28SJason King 	unsigned int			vif_has_ctrlq:1;
42635d41f28SJason King 	unsigned int			vif_has_ctrlq_rx:1;
42735d41f28SJason King 
428f8296c60SJoshua M. Clulow 	uint_t				vif_mtu;
429f8296c60SJoshua M. Clulow 	uint_t				vif_mtu_max;
430f8296c60SJoshua M. Clulow 	uint8_t				vif_mac[ETHERADDRL];
431f8296c60SJoshua M. Clulow 
432f8296c60SJoshua M. Clulow 	/*
433f8296c60SJoshua M. Clulow 	 * Receive buffer free list and accounting:
434f8296c60SJoshua M. Clulow 	 */
435f8296c60SJoshua M. Clulow 	list_t				vif_rxbufs;
436f8296c60SJoshua M. Clulow 	uint_t				vif_nrxbufs_alloc;
437f8296c60SJoshua M. Clulow 	uint_t				vif_nrxbufs_onloan;
438f8296c60SJoshua M. Clulow 	uint_t				vif_nrxbufs_onloan_max;
439f8296c60SJoshua M. Clulow 	uint_t				vif_rxbufs_capacity;
440f8296c60SJoshua M. Clulow 	vioif_rxbuf_t			*vif_rxbufs_mem;
441f8296c60SJoshua M. Clulow 
442f8296c60SJoshua M. Clulow 	/*
443f8296c60SJoshua M. Clulow 	 * Transmit buffer free list and accounting:
444f8296c60SJoshua M. Clulow 	 */
445f8296c60SJoshua M. Clulow 	list_t				vif_txbufs;
446f8296c60SJoshua M. Clulow 	uint_t				vif_ntxbufs_alloc;
447f8296c60SJoshua M. Clulow 	uint_t				vif_txbufs_capacity;
448f8296c60SJoshua M. Clulow 	vioif_txbuf_t			*vif_txbufs_mem;
449f8296c60SJoshua M. Clulow 
450f8296c60SJoshua M. Clulow 	/*
451f8296c60SJoshua M. Clulow 	 * These copy size thresholds are exposed as private MAC properties so
452f8296c60SJoshua M. Clulow 	 * that they can be tuned without rebooting.
453f8296c60SJoshua M. Clulow 	 */
454f8296c60SJoshua M. Clulow 	uint_t				vif_rxcopy_thresh;
455f8296c60SJoshua M. Clulow 	uint_t				vif_txcopy_thresh;
456f8296c60SJoshua M. Clulow 
45735d41f28SJason King 	list_t				vif_ctrlbufs;
45835d41f28SJason King 	uint_t				vif_nctrlbufs_alloc;
45935d41f28SJason King 	uint_t				vif_ctrlbufs_capacity;
46035d41f28SJason King 	vioif_ctrlbuf_t			*vif_ctrlbufs_mem;
46135d41f28SJason King 
462f8296c60SJoshua M. Clulow 	/*
463f8296c60SJoshua M. Clulow 	 * Statistics visible through mac:
464f8296c60SJoshua M. Clulow 	 */
465f8296c60SJoshua M. Clulow 	uint64_t			vif_ipackets;
466f8296c60SJoshua M. Clulow 	uint64_t			vif_opackets;
467f8296c60SJoshua M. Clulow 	uint64_t			vif_rbytes;
468f8296c60SJoshua M. Clulow 	uint64_t			vif_obytes;
469f8296c60SJoshua M. Clulow 	uint64_t			vif_brdcstxmt;
470f8296c60SJoshua M. Clulow 	uint64_t			vif_brdcstrcv;
471f8296c60SJoshua M. Clulow 	uint64_t			vif_multixmt;
472f8296c60SJoshua M. Clulow 	uint64_t			vif_multircv;
473f8296c60SJoshua M. Clulow 	uint64_t			vif_norecvbuf;
474f8296c60SJoshua M. Clulow 	uint64_t			vif_notxbuf;
475f8296c60SJoshua M. Clulow 	uint64_t			vif_ierrors;
476f8296c60SJoshua M. Clulow 	uint64_t			vif_oerrors;
477f8296c60SJoshua M. Clulow 
478f8296c60SJoshua M. Clulow 	/*
479f8296c60SJoshua M. Clulow 	 * Internal debugging statistics:
480f8296c60SJoshua M. Clulow 	 */
481f8296c60SJoshua M. Clulow 	uint64_t			vif_rxfail_dma_handle;
482f8296c60SJoshua M. Clulow 	uint64_t			vif_rxfail_dma_buffer;
483f8296c60SJoshua M. Clulow 	uint64_t			vif_rxfail_dma_bind;
484f8296c60SJoshua M. Clulow 	uint64_t			vif_rxfail_chain_undersize;
485f8296c60SJoshua M. Clulow 	uint64_t			vif_rxfail_no_descriptors;
486f8296c60SJoshua M. Clulow 	uint64_t			vif_txfail_dma_handle;
487f8296c60SJoshua M. Clulow 	uint64_t			vif_txfail_dma_bind;
488f8296c60SJoshua M. Clulow 	uint64_t			vif_txfail_indirect_limit;
489f8296c60SJoshua M. Clulow 
490f8296c60SJoshua M. Clulow 	uint64_t			vif_stat_tx_reclaim;
49135d41f28SJason King 
49235d41f28SJason King 	uint64_t			vif_noctrlbuf;
49335d41f28SJason King 	uint64_t			vif_ctrlbuf_toosmall;
494f8296c60SJoshua M. Clulow };
495f8296c60SJoshua M. Clulow 
496f8296c60SJoshua M. Clulow #ifdef __cplusplus
497f8296c60SJoshua M. Clulow }
498f8296c60SJoshua M. Clulow #endif
499f8296c60SJoshua M. Clulow 
500f8296c60SJoshua M. Clulow #endif /* _VIOIF_H */
501