xref: /titanic_52/usr/src/uts/sun4v/sys/vnet_gen.h (revision a9fb0ae8cfc9d2ebdf5f7cded8945fbb649ba24e)
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_TX_DBLK_SZ		2048	/* tx 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		seqnum;		/* sequence number of pkt */
111 	uint64_t		ncookies;	/* num ldc_mem_cookies */
112 	ldc_mem_cookie_t	memcookie[MAX_COOKIES];	/* data cookies */
113 } vgen_private_desc_t;
114 
115 /*
116  * Handshake parameters (per vio_mailbox.h) of each ldc end point, used
117  * during handshake negotiation.
118  */
119 typedef struct vgen_handshake_params {
120 	/* version specific params */
121 	uint32_t	ver_major:16,
122 			ver_minor:16;		/* major, minor version */
123 	uint8_t		dev_class;		/* device class */
124 
125 	/* attributes specific params */
126 	uint64_t		mtu;		/* max transfer unit size */
127 	uint64_t		addr;		/* address of the device */
128 	uint8_t			addr_type;	/* type of address */
129 	uint8_t			xfer_mode;	/* SHM or PKT */
130 	uint16_t		ack_freq;	/* dring data ack freq */
131 
132 	/* descriptor ring params */
133 	uint32_t		num_desc;	/* # of descriptors in ring */
134 	uint32_t		desc_size;	/* size of descriptor */
135 	ldc_mem_cookie_t	dring_cookie;	/* desc ring cookie */
136 	uint32_t		num_dcookies;	/* # of dring cookies */
137 	uint64_t		dring_ident;	/* ident=0 for INFO msg */
138 	boolean_t		dring_ready;   /* dring ready flag */
139 } vgen_hparams_t;
140 
141 /* version info */
142 typedef struct vgen_ver {
143 	uint32_t	ver_major:16,
144 			ver_minor:16;
145 } vgen_ver_t;
146 
147 typedef struct vgen_stats {
148 
149 	/* Link Input/Output stats */
150 	uint64_t	ipackets;
151 	uint64_t	ierrors;
152 	uint64_t	opackets;
153 	uint64_t	oerrors;
154 #if 0
155 	uint64_t	collisions;
156 #endif
157 
158 	/* MIB II variables */
159 	uint64_t	rbytes;		/* # bytes received */
160 	uint64_t	obytes;		/* # bytes transmitted */
161 	uint32_t	multircv;	/* # multicast packets received */
162 	uint32_t	multixmt;	/* # multicast packets for xmit */
163 	uint32_t	brdcstrcv;	/* # broadcast packets received */
164 	uint32_t	brdcstxmt;	/* # broadcast packets for xmit */
165 	uint32_t	norcvbuf;	/* # rcv packets discarded */
166 	uint32_t	noxmtbuf;	/* # xmit packets discarded */
167 
168 	/* Tx Statistics */
169 	uint32_t	tx_no_desc;
170 	uint32_t	tx_allocb_fail;
171 
172 	/* Rx Statistics */
173 	uint32_t	rx_no_desc;
174 	uint32_t	rx_allocb_fail;
175 	uint32_t	rx_lost_pkts;
176 
177 	/* Callback statistics */
178 	uint32_t	callbacks;
179 	uint32_t	dring_data_acks;
180 
181 } vgen_stats_t;
182 
183 typedef struct vgen_kstats {
184 	/*
185 	 * Link Input/Output stats
186 	 */
187 	kstat_named_t	ipackets;
188 	kstat_named_t	ipackets64;
189 	kstat_named_t	ierrors;
190 	kstat_named_t	opackets;
191 	kstat_named_t	opackets64;
192 	kstat_named_t	oerrors;
193 #if 0
194 	kstat_named_t	collisions;
195 #endif
196 	/*
197 	 * required by kstat for MIB II objects(RFC 1213)
198 	 */
199 	kstat_named_t	rbytes; 	/* MIB - ifInOctets */
200 	kstat_named_t	rbytes64;
201 	kstat_named_t	obytes; 	/* MIB - ifOutOctets */
202 	kstat_named_t	obytes64;
203 	kstat_named_t	multircv; 	/* MIB - ifInNUcastPkts */
204 	kstat_named_t	multixmt; 	/* MIB - ifOutNUcastPkts */
205 	kstat_named_t	brdcstrcv;	/* MIB - ifInNUcastPkts */
206 	kstat_named_t	brdcstxmt;	/* MIB - ifOutNUcastPkts */
207 	kstat_named_t	norcvbuf; 	/* MIB - ifInDiscards */
208 	kstat_named_t	noxmtbuf; 	/* MIB - ifOutDiscards */
209 
210 	/* Tx Statistics */
211 	kstat_named_t	tx_no_desc;
212 	kstat_named_t	tx_allocb_fail;
213 
214 	/* Rx Statistics */
215 	kstat_named_t	rx_no_desc;
216 	kstat_named_t	rx_allocb_fail;
217 	kstat_named_t	rx_lost_pkts;
218 
219 	/* Callback statistics */
220 	kstat_named_t	callbacks;
221 	kstat_named_t	dring_data_acks;
222 
223 } vgen_kstats_t;
224 
225 /* Channel information associated with a vgen-port */
226 typedef struct vgen_ldc {
227 
228 	struct vgen_ldc		*nextp;		/* next ldc in the list */
229 	struct vgen_port	*portp;		/* associated port */
230 
231 	/*
232 	 * Locks:
233 	 * locking hierarchy when more than one lock is held concurrently:
234 	 * cblock > txlock > tclock.
235 	 */
236 	kmutex_t		cblock;		/* sync callback processing */
237 	kmutex_t		txlock;		/* sync transmits */
238 	kmutex_t		tclock;		/* tx reclaim lock */
239 
240 	/* channel info from ldc layer */
241 	uint64_t		ldc_id;		/* channel number */
242 	uint64_t		ldc_handle;	/* channel handle */
243 	ldc_status_t		ldc_status;	/* channel status */
244 
245 	/* handshake info */
246 	vgen_ver_t		vgen_versions[VGEN_NUM_VER]; /* versions */
247 	int			hphase;		/* handshake phase */
248 	int			hstate;		/* handshake state bits */
249 	uint32_t		local_sid;	/* local session id */
250 	uint32_t		peer_sid;	/* session id of peer */
251 	vgen_hparams_t		local_hparams;	/* local handshake params */
252 	vgen_hparams_t		peer_hparams;	/* peer's handshake params */
253 	timeout_id_t		htid;		/* handshake wd timeout id */
254 
255 	/* transmit and receive descriptor ring info */
256 	ldc_dring_handle_t	tx_dhandle;	/* tx descriptor ring handle */
257 	ldc_mem_cookie_t	tx_dcookie;	/* tx descriptor ring cookie */
258 	ldc_dring_handle_t	rx_dhandle;	/* mapped rx dhandle */
259 	ldc_mem_cookie_t	rx_dcookie;	/* rx descriptor ring cookie */
260 	vnet_public_desc_t	*txdp;		/* transmit frame descriptors */
261 	vnet_public_desc_t	*txdendp;	/* txd ring end */
262 	vgen_private_desc_t	*tbufp;		/* associated tx resources */
263 	vgen_private_desc_t	*tbufendp;	/* tbuf ring end */
264 	vgen_private_desc_t	*next_tbufp;	/* next free tbuf */
265 	vgen_private_desc_t	*cur_tbufp;	/* next reclaim tbuf */
266 	uint64_t		next_txseq;	/* next tx sequence number */
267 	uint32_t		num_txdcookies;	/* # of tx dring cookies */
268 	uint32_t		num_rxdcookies;	/* # of rx dring cookies */
269 	uint32_t		next_txi;	/* next tx descriptor index */
270 	uint32_t		num_txds;	/* number of tx descriptors */
271 	uint32_t		reclaim_lowat;	/* lowat for tx reclaim */
272 	uint32_t		reclaim_hiwat;	/* hiwat for tx reclaim */
273 	clock_t			reclaim_lbolt;	/* time of last tx reclaim */
274 	timeout_id_t		wd_tid;		/* tx watchdog timeout id */
275 	vnet_public_desc_t	*rxdp;		/* receive frame descriptors */
276 	uint64_t		next_rxseq;	/* next expected recv seqnum */
277 	uint32_t		next_rxi;	/* next expected recv index */
278 	uint32_t		num_rxds;	/* number of rx descriptors */
279 	caddr_t			tx_datap;	/* prealloc'd tx data area */
280 
281 	/* misc */
282 	uint32_t		flags;		/* flags */
283 	boolean_t		need_resched;	/* reschedule tx */
284 	boolean_t		need_ldc_reset; /* ldc_reset needed */
285 	boolean_t		need_mcast_sync; /* sync mcast table with vsw */
286 	uint32_t		hretries;	/* handshake retry count */
287 
288 	/* channel statistics */
289 	vgen_stats_t		*statsp;	/* channel statistics */
290 	kstat_t			*ksp;		/* channel kstats */
291 
292 } vgen_ldc_t;
293 
294 /* Channel list structure */
295 typedef struct vgen_ldclist_s {
296 	vgen_ldc_t	*headp;		/* head of the list */
297 	krwlock_t	rwlock;		/* sync access to the list */
298 	int		num_ldcs;	/* number of channels in the list */
299 } vgen_ldclist_t;
300 
301 /* port information  structure */
302 typedef struct vgen_port {
303 	struct vgen_port	*nextp;		/* next port in the list */
304 	struct vgen		*vgenp;		/* associated vgen_t */
305 	int			port_num;	/* port number */
306 	vgen_ldclist_t		ldclist;	/* list of ldcs for this port */
307 	struct ether_addr	macaddr;	/* mac address of peer */
308 } vgen_port_t;
309 
310 /* port list structure */
311 typedef struct vgen_portlist {
312 	vgen_port_t	*headp;		/* head of ports */
313 	vgen_port_t	*tailp;		/* tail */
314 	krwlock_t	rwlock;		/* sync access to the port list */
315 } vgen_portlist_t;
316 
317 /* vgen instance information  */
318 typedef struct vgen {
319 	void			*vnetp;		/* associated vnet instance */
320 	dev_info_t		*vnetdip;	/* dip of vnet */
321 	void			*vnetmacp;	/* mac_t of vnet */
322 	uint8_t			macaddr[ETHERADDRL];	/* mac addr of vnet */
323 	mac_resource_handle_t	mrh;		/* handle for mac_rx() */
324 	kmutex_t		lock;		/* synchornize ops */
325 	int			flags;		/* flags */
326 	vgen_portlist_t		vgenports;	/* Port List */
327 	mdeg_node_spec_t	*mdeg_parentp;
328 	mdeg_handle_t		mdeg_hdl;
329 	vgen_port_t		*vsw_portp;	/* port connected to vsw */
330 	mac_t			vgenmac;	/* vgen mac ops */
331 	struct ether_addr	*mctab;		/* multicast addr table */
332 	uint32_t		mcsize;		/* allocated size of mctab */
333 	uint32_t		mccount;	/* # of valid addrs in mctab */
334 } vgen_t;
335 
336 #ifdef __cplusplus
337 }
338 #endif
339 
340 #endif	/* _VNET_GEN_H */
341