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