xref: /illumos-gate/usr/src/uts/sun4v/sys/vnet_gen.h (revision c7e4935f5b755b4bbeaec416f1ad24337aeac7a4)
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 #define	VGEN_DBLK_SZ		2048	/* data buffer size */
73 #define	VGEN_LDC_UP_DELAY	100	/* usec delay between ldc_up retries */
74 
75 /* get the address of next tbuf */
76 #define	NEXTTBUF(ldcp, tbufp)	(((tbufp) + 1) == (ldcp)->tbufendp    \
77 		? (ldcp)->tbufp : ((tbufp) + 1))
78 
79 /* increment recv index */
80 #define	INCR_RXI(i, ldcp)	\
81 		((i) = (((i) + 1) & ((ldcp)->num_rxds - 1)))
82 
83 /* decrement recv index */
84 #define	DECR_RXI(i, ldcp)	\
85 		((i) = (((i) - 1) & ((ldcp)->num_rxds - 1)))
86 
87 /* increment tx index */
88 #define	INCR_TXI(i, ldcp)	\
89 		((i) = (((i) + 1) & ((ldcp)->num_txds - 1)))
90 
91 /* decrement tx index */
92 #define	DECR_TXI(i, ldcp)	\
93 		((i) = (((i) - 1) & ((ldcp)->num_txds - 1)))
94 
95 /* bounds check rx index */
96 #define	CHECK_RXI(i, ldcp)	\
97 		(((i) >= 0) && ((i) < (ldcp)->num_rxds))
98 
99 /* bounds check tx index */
100 #define	CHECK_TXI(i, ldcp)	\
101 		(((i) >= 0) && ((i) < (ldcp)->num_txds))
102 
103 /* private descriptor */
104 typedef struct vgen_priv_desc {
105 	uint64_t		flags;		/* flag bits */
106 	vnet_public_desc_t	*descp;		/* associated public desc */
107 	ldc_mem_handle_t	memhandle;	/* mem handle for data */
108 	caddr_t			datap;		/* prealloc'd tx data buffer */
109 	uint64_t		datalen;	/* total actual datalen */
110 	uint64_t		ncookies;	/* num ldc_mem_cookies */
111 	ldc_mem_cookie_t	memcookie[MAX_COOKIES];	/* data cookies */
112 } vgen_private_desc_t;
113 
114 /*
115  * Handshake parameters (per vio_mailbox.h) of each ldc end point, used
116  * during handshake negotiation.
117  */
118 typedef struct vgen_handshake_params {
119 	/* version specific params */
120 	uint32_t	ver_major:16,
121 			ver_minor:16;		/* major, minor version */
122 	uint8_t		dev_class;		/* device class */
123 
124 	/* attributes specific params */
125 	uint64_t		mtu;		/* max transfer unit size */
126 	uint64_t		addr;		/* address of the device */
127 	uint8_t			addr_type;	/* type of address */
128 	uint8_t			xfer_mode;	/* SHM or PKT */
129 	uint16_t		ack_freq;	/* dring data ack freq */
130 
131 	/* descriptor ring params */
132 	uint32_t		num_desc;	/* # of descriptors in ring */
133 	uint32_t		desc_size;	/* size of descriptor */
134 	ldc_mem_cookie_t	dring_cookie;	/* desc ring cookie */
135 	uint32_t		num_dcookies;	/* # of dring cookies */
136 	uint64_t		dring_ident;	/* ident=0 for INFO msg */
137 	boolean_t		dring_ready;   /* dring ready flag */
138 } vgen_hparams_t;
139 
140 /* version info */
141 typedef struct vgen_ver {
142 	uint32_t	ver_major:16,
143 			ver_minor:16;
144 } vgen_ver_t;
145 
146 typedef struct vgen_stats {
147 
148 	/* Link Input/Output stats */
149 	uint64_t	ipackets;	/* # rx packets */
150 	uint64_t	ierrors;	/* # rx error */
151 	uint64_t	opackets;	/* # tx packets */
152 	uint64_t	oerrors;	/* # tx error */
153 
154 	/* MIB II variables */
155 	uint64_t	rbytes;		/* # bytes received */
156 	uint64_t	obytes;		/* # bytes transmitted */
157 	uint32_t	multircv;	/* # multicast packets received */
158 	uint32_t	multixmt;	/* # multicast packets for xmit */
159 	uint32_t	brdcstrcv;	/* # broadcast packets received */
160 	uint32_t	brdcstxmt;	/* # broadcast packets for xmit */
161 	uint32_t	norcvbuf;	/* # rcv packets discarded */
162 	uint32_t	noxmtbuf;	/* # xmit packets discarded */
163 
164 	/* Tx Statistics */
165 	uint32_t	tx_no_desc;	/* # out of transmit descriptors */
166 
167 	/* Rx Statistics */
168 	uint32_t	rx_allocb_fail;	/* # rx buf allocb() failures */
169 	uint32_t	rx_vio_allocb_fail; /* # vio_allocb() failures */
170 	uint32_t	rx_lost_pkts;	/* # rx lost packets */
171 
172 	/* Callback statistics */
173 	uint32_t	callbacks;		/* # callbacks */
174 	uint32_t	dring_data_acks;	/* # dring data acks recvd  */
175 	uint32_t	dring_stopped_acks;	/* # dring stopped acks recvd */
176 	uint32_t	dring_data_msgs;	/* # dring data msgs sent */
177 
178 } vgen_stats_t;
179 
180 typedef struct vgen_kstats {
181 	/*
182 	 * Link Input/Output stats
183 	 */
184 	kstat_named_t	ipackets;
185 	kstat_named_t	ipackets64;
186 	kstat_named_t	ierrors;
187 	kstat_named_t	opackets;
188 	kstat_named_t	opackets64;
189 	kstat_named_t	oerrors;
190 
191 	/*
192 	 * required by kstat for MIB II objects(RFC 1213)
193 	 */
194 	kstat_named_t	rbytes; 	/* MIB - ifInOctets */
195 	kstat_named_t	rbytes64;
196 	kstat_named_t	obytes; 	/* MIB - ifOutOctets */
197 	kstat_named_t	obytes64;
198 	kstat_named_t	multircv; 	/* MIB - ifInNUcastPkts */
199 	kstat_named_t	multixmt; 	/* MIB - ifOutNUcastPkts */
200 	kstat_named_t	brdcstrcv;	/* MIB - ifInNUcastPkts */
201 	kstat_named_t	brdcstxmt;	/* MIB - ifOutNUcastPkts */
202 	kstat_named_t	norcvbuf; 	/* MIB - ifInDiscards */
203 	kstat_named_t	noxmtbuf; 	/* MIB - ifOutDiscards */
204 
205 	/* Tx Statistics */
206 	kstat_named_t	tx_no_desc;	/* # out of transmit descriptors */
207 
208 	/* Rx Statistics */
209 	kstat_named_t	rx_allocb_fail;	/* # rx buf allocb failures */
210 	kstat_named_t	rx_vio_allocb_fail; /* # vio_allocb() failures */
211 	kstat_named_t	rx_lost_pkts;	/* # rx lost packets */
212 
213 	/* Callback statistics */
214 	kstat_named_t	callbacks;		/* # callbacks */
215 	kstat_named_t	dring_data_acks;	/* # dring data acks recvd  */
216 	kstat_named_t	dring_stopped_acks;	/* # dring stopped acks recvd */
217 	kstat_named_t	dring_data_msgs;	/* # dring data msgs sent */
218 
219 } vgen_kstats_t;
220 
221 /* Channel information associated with a vgen-port */
222 typedef struct vgen_ldc {
223 
224 	struct vgen_ldc		*nextp;		/* next ldc in the list */
225 	struct vgen_port	*portp;		/* associated port */
226 
227 	/*
228 	 * Locks:
229 	 * locking hierarchy when more than one lock is held concurrently:
230 	 * cblock > txlock > tclock.
231 	 */
232 	kmutex_t		cblock;		/* sync callback processing */
233 	kmutex_t		txlock;		/* sync transmits */
234 	kmutex_t		tclock;		/* tx reclaim lock */
235 
236 	/* channel info from ldc layer */
237 	uint64_t		ldc_id;		/* channel number */
238 	uint64_t		ldc_handle;	/* channel handle */
239 	ldc_status_t		ldc_status;	/* channel status */
240 
241 	/* handshake info */
242 	vgen_ver_t		vgen_versions[VGEN_NUM_VER]; /* versions */
243 	int			hphase;		/* handshake phase */
244 	int			hstate;		/* handshake state bits */
245 	uint32_t		local_sid;	/* local session id */
246 	uint32_t		peer_sid;	/* session id of peer */
247 	vgen_hparams_t		local_hparams;	/* local handshake params */
248 	vgen_hparams_t		peer_hparams;	/* peer's handshake params */
249 	timeout_id_t		htid;		/* handshake wd timeout id */
250 
251 	/* transmit and receive descriptor ring info */
252 	ldc_dring_handle_t	tx_dhandle;	/* tx descriptor ring handle */
253 	ldc_mem_cookie_t	tx_dcookie;	/* tx descriptor ring cookie */
254 	ldc_dring_handle_t	rx_dhandle;	/* mapped rx dhandle */
255 	ldc_mem_cookie_t	rx_dcookie;	/* rx descriptor ring cookie */
256 	vnet_public_desc_t	*txdp;		/* transmit frame descriptors */
257 	vnet_public_desc_t	*txdendp;	/* txd ring end */
258 	vgen_private_desc_t	*tbufp;		/* associated tx resources */
259 	vgen_private_desc_t	*tbufendp;	/* tbuf ring end */
260 	vgen_private_desc_t	*next_tbufp;	/* next free tbuf */
261 	vgen_private_desc_t	*cur_tbufp;	/* next reclaim tbuf */
262 	uint64_t		next_txseq;	/* next tx sequence number */
263 	uint32_t		num_txdcookies;	/* # of tx dring cookies */
264 	uint32_t		num_rxdcookies;	/* # of rx dring cookies */
265 	uint32_t		next_txi;	/* next tx descriptor index */
266 	uint32_t		num_txds;	/* number of tx descriptors */
267 	clock_t			reclaim_lbolt;	/* time of last tx reclaim */
268 	timeout_id_t		wd_tid;		/* tx watchdog timeout id */
269 	vnet_public_desc_t	*rxdp;		/* receive frame descriptors */
270 	uint64_t		next_rxseq;	/* next expected recv seqnum */
271 	uint32_t		next_rxi;	/* next expected recv index */
272 	uint32_t		num_rxds;	/* number of rx descriptors */
273 	caddr_t			tx_datap;	/* prealloc'd tx data area */
274 	vio_mblk_pool_t		*rmp;		/* rx mblk pool */
275 	uint32_t		num_rbufs;	/* number of rx bufs */
276 
277 	/* misc */
278 	uint32_t		flags;		/* flags */
279 	boolean_t		need_resched;	/* reschedule tx */
280 	boolean_t		need_ldc_reset; /* ldc_reset needed */
281 	boolean_t		need_mcast_sync; /* sync mcast table with vsw */
282 	uint32_t		hretries;	/* handshake retry count */
283 	boolean_t		resched_peer;	/* send tx msg to peer */
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 	uint8_t			macaddr[ETHERADDRL];	/* mac addr of vnet */
319 	kmutex_t		lock;		/* synchornize ops */
320 	int			flags;		/* flags */
321 	vgen_portlist_t		vgenports;	/* Port List */
322 	mdeg_node_spec_t	*mdeg_parentp;
323 	mdeg_handle_t		mdeg_hdl;
324 	vgen_port_t		*vsw_portp;	/* port connected to vsw */
325 	mac_register_t		*macp;		/* vgen mac ops */
326 	struct ether_addr	*mctab;		/* multicast addr table */
327 	uint32_t		mcsize;		/* allocated size of mctab */
328 	uint32_t		mccount;	/* # of valid addrs in mctab */
329 	vio_mblk_pool_t		*rmp;		/* rx mblk pools to be freed */
330 } vgen_t;
331 
332 #ifdef __cplusplus
333 }
334 #endif
335 
336 #endif	/* _VNET_GEN_H */
337