xref: /illumos-gate/usr/src/uts/sun4v/sys/vnet_gen.h (revision 4abb96737d15cd2d6530b0aa7b8404ec911ad940)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #ifndef _VNET_GEN_H
28 #define	_VNET_GEN_H
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35 
36 #define	VGEN_SUCCESS		(0)	/* successful return */
37 #define	VGEN_FAILURE		(-1)	/* unsuccessful return */
38 
39 #define	VGEN_NUM_VER		1	/* max # of vgen versions */
40 
41 #define	VGEN_LOCAL	1	/* local ldc end-point */
42 #define	VGEN_PEER	2	/* peer ldc end-point */
43 
44 /* vgen_t flags */
45 #define	VGEN_STOPPED		0x0
46 #define	VGEN_STARTED		0x1
47 
48 #define	KMEM_FREE(_p)		kmem_free((_p), sizeof (*(_p)))
49 
50 #define	VGEN_INIT_MCTAB_SIZE	16	/* initial size of multicast table */
51 
52 #define	READ_ENTER(x)	rw_enter(x, RW_READER)
53 #define	WRITE_ENTER(x)	rw_enter(x, RW_WRITER)
54 #define	RW_EXIT(x)	rw_exit(x)
55 
56 /* channel flags */
57 #define	CHANNEL_ATTACHED	0x1
58 #define	CHANNEL_STARTED		0x2
59 
60 /* transmit return values */
61 #define	VGEN_TX_SUCCESS		0	/* transmit success */
62 #define	VGEN_TX_FAILURE		1	/* transmit failure */
63 #define	VGEN_TX_NORESOURCES	2	/* out of tbufs/txds */
64 
65 /* private descriptor flags */
66 #define	VGEN_PRIV_DESC_FREE	0x0	/* desc is available */
67 #define	VGEN_PRIV_DESC_BUSY	0x1	/* desc in use */
68 
69 #define	LDC_TO_VNET(ldcp)  ((ldcp)->portp->vgenp->vnetp)
70 #define	LDC_TO_VGEN(ldcp)  ((ldcp)->portp->vgenp)
71 
72 /* get the address of next tbuf */
73 #define	NEXTTBUF(ldcp, tbufp)	(((tbufp) + 1) == (ldcp)->tbufendp    \
74 		? (ldcp)->tbufp : ((tbufp) + 1))
75 
76 /* increment recv index */
77 #define	INCR_RXI(i, ldcp)	\
78 		((i) = (((i) + 1) & ((ldcp)->num_rxds - 1)))
79 
80 /* decrement recv index */
81 #define	DECR_RXI(i, ldcp)	\
82 		((i) = (((i) - 1) & ((ldcp)->num_rxds - 1)))
83 
84 /* increment tx index */
85 #define	INCR_TXI(i, ldcp)	\
86 		((i) = (((i) + 1) & ((ldcp)->num_txds - 1)))
87 
88 /* decrement tx index */
89 #define	DECR_TXI(i, ldcp)	\
90 		((i) = (((i) - 1) & ((ldcp)->num_txds - 1)))
91 
92 /* bounds check rx index */
93 #define	CHECK_RXI(i, ldcp)	\
94 		(((i) >= 0) && ((i) < (ldcp)->num_rxds))
95 
96 /* bounds check tx index */
97 #define	CHECK_TXI(i, ldcp)	\
98 		(((i) >= 0) && ((i) < (ldcp)->num_txds))
99 
100 /* private descriptor */
101 typedef struct vgen_priv_desc {
102 	uint64_t		flags;		/* flag bits */
103 	vnet_public_desc_t	*descp;		/* associated public desc */
104 	ldc_mem_handle_t	memhandle;	/* mem handle for data */
105 	mblk_t			*mp;		/* associated packet */
106 	uint64_t		datap;		/* mp->b_rptr */
107 	uint64_t		datalen;	/* total actual datalen */
108 	uint64_t		seqnum;		/* sequence number of pkt */
109 	uint64_t		ncookies;	/* num ldc_mem_cookies */
110 	ldc_mem_cookie_t	memcookie[MAX_COOKIES];	/* data cookies */
111 } vgen_private_desc_t;
112 
113 /*
114  * Handshake parameters (per vio_mailbox.h) of each ldc end point, used
115  * during handshake negotiation.
116  */
117 typedef struct vgen_handshake_params {
118 	/* version specific params */
119 	uint32_t	ver_major:16,
120 			ver_minor:16;		/* major, minor version */
121 	uint8_t		dev_class;		/* device class */
122 
123 	/* attributes specific params */
124 	uint64_t		mtu;		/* max transfer unit size */
125 	uint64_t		addr;		/* address of the device */
126 	uint8_t			addr_type;	/* type of address */
127 	uint8_t			xfer_mode;	/* SHM or PKT */
128 	uint16_t		ack_freq;	/* dring data ack freq */
129 
130 	/* descriptor ring params */
131 	uint32_t		num_desc;	/* # of descriptors in ring */
132 	uint32_t		desc_size;	/* size of descriptor */
133 	ldc_mem_cookie_t	dring_cookie;	/* desc ring cookie */
134 	uint32_t		num_dcookies;	/* # of dring cookies */
135 	uint64_t		dring_ident;	/* ident=0 for INFO msg */
136 	boolean_t		dring_ready;   /* dring ready flag */
137 } vgen_hparams_t;
138 
139 /* version info */
140 typedef struct vgen_ver {
141 	uint32_t	ver_major:16,
142 			ver_minor:16;
143 } vgen_ver_t;
144 
145 typedef struct vgen_stats {
146 
147 	/* Link Input/Output stats */
148 	uint64_t	ipackets;
149 	uint64_t	ierrors;
150 	uint64_t	opackets;
151 	uint64_t	oerrors;
152 #if 0
153 	uint64_t	collisions;
154 #endif
155 
156 	/* MIB II variables */
157 	uint64_t	rbytes;		/* # bytes received */
158 	uint64_t	obytes;		/* # bytes transmitted */
159 	uint32_t	multircv;	/* # multicast packets received */
160 	uint32_t	multixmt;	/* # multicast packets for xmit */
161 	uint32_t	brdcstrcv;	/* # broadcast packets received */
162 	uint32_t	brdcstxmt;	/* # broadcast packets for xmit */
163 	uint32_t	norcvbuf;	/* # rcv packets discarded */
164 	uint32_t	noxmtbuf;	/* # xmit packets discarded */
165 
166 	/* Tx Statistics */
167 	uint32_t	tx_no_desc;
168 	uint32_t	tx_allocb_fail;
169 
170 	/* Rx Statistics */
171 	uint32_t	rx_no_desc;
172 	uint32_t	rx_allocb_fail;
173 	uint32_t	rx_lost_pkts;
174 
175 	/* Callback statistics */
176 	uint32_t	callbacks;
177 	uint32_t	dring_data_acks;
178 
179 } vgen_stats_t;
180 
181 typedef struct vgen_kstats {
182 	/*
183 	 * Link Input/Output stats
184 	 */
185 	kstat_named_t	ipackets;
186 	kstat_named_t	ipackets64;
187 	kstat_named_t	ierrors;
188 	kstat_named_t	opackets;
189 	kstat_named_t	opackets64;
190 	kstat_named_t	oerrors;
191 #if 0
192 	kstat_named_t	collisions;
193 #endif
194 	/*
195 	 * required by kstat for MIB II objects(RFC 1213)
196 	 */
197 	kstat_named_t	rbytes; 	/* MIB - ifInOctets */
198 	kstat_named_t	rbytes64;
199 	kstat_named_t	obytes; 	/* MIB - ifOutOctets */
200 	kstat_named_t	obytes64;
201 	kstat_named_t	multircv; 	/* MIB - ifInNUcastPkts */
202 	kstat_named_t	multixmt; 	/* MIB - ifOutNUcastPkts */
203 	kstat_named_t	brdcstrcv;	/* MIB - ifInNUcastPkts */
204 	kstat_named_t	brdcstxmt;	/* MIB - ifOutNUcastPkts */
205 	kstat_named_t	norcvbuf; 	/* MIB - ifInDiscards */
206 	kstat_named_t	noxmtbuf; 	/* MIB - ifOutDiscards */
207 
208 	/* Tx Statistics */
209 	kstat_named_t	tx_no_desc;
210 	kstat_named_t	tx_allocb_fail;
211 
212 	/* Rx Statistics */
213 	kstat_named_t	rx_no_desc;
214 	kstat_named_t	rx_allocb_fail;
215 	kstat_named_t	rx_lost_pkts;
216 
217 	/* Callback statistics */
218 	kstat_named_t	callbacks;
219 	kstat_named_t	dring_data_acks;
220 
221 } vgen_kstats_t;
222 
223 /* Channel information associated with a vgen-port */
224 typedef struct vgen_ldc {
225 
226 	struct vgen_ldc		*nextp;		/* next ldc in the list */
227 	struct vgen_port	*portp;		/* associated port */
228 
229 	/*
230 	 * Locks:
231 	 * locking hierarchy when more than one lock is held concurrently:
232 	 * cblock > txlock > tclock.
233 	 */
234 	kmutex_t		cblock;		/* sync callback processing */
235 	kmutex_t		txlock;		/* sync transmits */
236 	kmutex_t		tclock;		/* tx reclaim lock */
237 
238 	/* channel info from ldc layer */
239 	uint64_t		ldc_id;		/* channel number */
240 	uint64_t		ldc_handle;	/* channel handle */
241 	ldc_status_t		ldc_status;	/* channel status */
242 
243 	/* handshake info */
244 	vgen_ver_t		vgen_versions[VGEN_NUM_VER]; /* versions */
245 	int			hphase;		/* handshake phase */
246 	int			hstate;		/* handshake state bits */
247 	uint32_t		local_sid;	/* local session id */
248 	uint32_t		peer_sid;	/* session id of peer */
249 	vgen_hparams_t		local_hparams;	/* local handshake params */
250 	vgen_hparams_t		peer_hparams;	/* peer's handshake params */
251 	timeout_id_t		htid;		/* handshake wd timeout id */
252 
253 	/* transmit and receive descriptor ring info */
254 	ldc_dring_handle_t	tx_dhandle;	/* tx descriptor ring handle */
255 	ldc_mem_cookie_t	tx_dcookie;	/* tx descriptor ring cookie */
256 	ldc_dring_handle_t	rx_dhandle;	/* mapped rx dhandle */
257 	ldc_mem_cookie_t	rx_dcookie;	/* rx descriptor ring cookie */
258 	vnet_public_desc_t	*txdp;		/* transmit frame descriptors */
259 	vnet_public_desc_t	*txdendp;	/* txd ring end */
260 	vgen_private_desc_t	*tbufp;		/* associated tx resources */
261 	vgen_private_desc_t	*tbufendp;	/* tbuf ring end */
262 	vgen_private_desc_t	*next_tbufp;	/* next free tbuf */
263 	vgen_private_desc_t	*cur_tbufp;	/* next reclaim tbuf */
264 	uint64_t		next_txseq;	/* next tx sequence number */
265 	uint32_t		num_txdcookies;	/* # of tx dring cookies */
266 	uint32_t		num_rxdcookies;	/* # of rx dring cookies */
267 	uint32_t		next_txi;	/* next tx descriptor index */
268 	uint32_t		num_txds;	/* number of tx descriptors */
269 	uint32_t		reclaim_lowat;	/* lowat for tx reclaim */
270 	uint32_t		reclaim_hiwat;	/* hiwat for tx reclaim */
271 	clock_t			reclaim_lbolt;	/* time of last tx reclaim */
272 	timeout_id_t		wd_tid;		/* tx watchdog timeout id */
273 	vnet_public_desc_t	*rxdp;		/* receive frame descriptors */
274 	uint64_t		next_rxseq;	/* next expected recv seqnum */
275 	uint32_t		next_rxi;	/* next expected recv index */
276 	uint32_t		num_rxds;	/* number of rx descriptors */
277 
278 	/* misc */
279 	uint32_t		flags;		/* flags */
280 	boolean_t		need_resched;	/* reschedule tx */
281 	boolean_t		need_ldc_reset; /* ldc_reset needed */
282 	boolean_t		need_mcast_sync; /* sync mcast table with vsw */
283 	uint32_t		hretries;	/* handshake retry count */
284 
285 	/* channel statistics */
286 	vgen_stats_t		*statsp;	/* channel statistics */
287 	kstat_t			*ksp;		/* channel kstats */
288 
289 } vgen_ldc_t;
290 
291 /* Channel list structure */
292 typedef struct vgen_ldclist_s {
293 	vgen_ldc_t	*headp;		/* head of the list */
294 	krwlock_t	rwlock;		/* sync access to the list */
295 	int		num_ldcs;	/* number of channels in the list */
296 } vgen_ldclist_t;
297 
298 /* port information  structure */
299 typedef struct vgen_port {
300 	struct vgen_port	*nextp;		/* next port in the list */
301 	struct vgen		*vgenp;		/* associated vgen_t */
302 	int			port_num;	/* port number */
303 	vgen_ldclist_t		ldclist;	/* list of ldcs for this port */
304 	struct ether_addr	macaddr;	/* mac address of peer */
305 } vgen_port_t;
306 
307 /* port list structure */
308 typedef struct vgen_portlist {
309 	vgen_port_t	*headp;		/* head of ports */
310 	vgen_port_t	*tailp;		/* tail */
311 	krwlock_t	rwlock;		/* sync access to the port list */
312 } vgen_portlist_t;
313 
314 /* vgen instance information  */
315 typedef struct vgen {
316 	void			*vnetp;		/* associated vnet instance */
317 	dev_info_t		*vnetdip;	/* dip of vnet */
318 	void			*vnetmacp;	/* mac_t of vnet */
319 	uint8_t			macaddr[ETHERADDRL];	/* mac addr of vnet */
320 	mac_resource_handle_t	mrh;		/* handle for mac_rx() */
321 	kmutex_t		lock;		/* synchornize ops */
322 	int			flags;		/* flags */
323 	vgen_portlist_t		vgenports;	/* Port List */
324 	mdeg_node_spec_t	*mdeg_parentp;
325 	mdeg_handle_t		mdeg_hdl;
326 	vgen_port_t		*vsw_portp;	/* port connected to vsw */
327 	mac_t			vgenmac;	/* vgen mac ops */
328 	struct ether_addr	*mctab;		/* multicast addr table */
329 	uint32_t		mcsize;		/* allocated size of mctab */
330 	uint32_t		mccount;	/* # of valid addrs in mctab */
331 } vgen_t;
332 
333 #ifdef __cplusplus
334 }
335 #endif
336 
337 #endif	/* _VNET_GEN_H */
338