xref: /illumos-gate/usr/src/uts/sun4v/sys/vnet_gen.h (revision a194faf8907a6722dcf10ad16c6ca72c9b7bd0ba)
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 #include <sys/vgen_stats.h>
37 
38 #define	VGEN_SUCCESS		(0)	/* successful return */
39 #define	VGEN_FAILURE		(-1)	/* unsuccessful return */
40 
41 #define	VGEN_NUM_VER		1	/* max # of vgen versions */
42 
43 #define	VGEN_LOCAL	1	/* local ldc end-point */
44 #define	VGEN_PEER	2	/* peer ldc end-point */
45 
46 /* vgen_t flags */
47 #define	VGEN_STOPPED		0x0
48 #define	VGEN_STARTED		0x1
49 
50 #define	KMEM_FREE(_p)		kmem_free((_p), sizeof (*(_p)))
51 
52 #define	VGEN_INIT_MCTAB_SIZE	16	/* initial size of multicast table */
53 
54 #define	READ_ENTER(x)	rw_enter(x, RW_READER)
55 #define	WRITE_ENTER(x)	rw_enter(x, RW_WRITER)
56 #define	RW_EXIT(x)	rw_exit(x)
57 
58 /* channel flags */
59 #define	CHANNEL_ATTACHED	0x1
60 #define	CHANNEL_STARTED		0x2
61 
62 /* transmit return values */
63 #define	VGEN_TX_SUCCESS		0	/* transmit success */
64 #define	VGEN_TX_FAILURE		1	/* transmit failure */
65 #define	VGEN_TX_NORESOURCES	2	/* out of tbufs/txds */
66 
67 /* private descriptor flags */
68 #define	VGEN_PRIV_DESC_FREE	0x0	/* desc is available */
69 #define	VGEN_PRIV_DESC_BUSY	0x1	/* desc in use */
70 
71 #define	LDC_TO_VNET(ldcp)  ((ldcp)->portp->vgenp->vnetp)
72 #define	LDC_TO_VGEN(ldcp)  ((ldcp)->portp->vgenp)
73 
74 /* receive thread flags */
75 #define	VGEN_WTHR_RUNNING 	0x01	/* worker thread running */
76 #define	VGEN_WTHR_DATARCVD 	0x02	/* data received */
77 #define	VGEN_WTHR_STOP 		0x04	/* stop worker thread request */
78 
79 #define	VGEN_LDC_UP_DELAY	100	/* usec delay between ldc_up retries */
80 
81 #define	VGEN_NUM_VMPOOLS	3	/* number of vio mblk pools */
82 
83 #define	VGEN_DBLK_SZ_128	128	/* data buffer size 128 bytes */
84 #define	VGEN_DBLK_SZ_256	256	/* data buffer size 256 bytes */
85 #define	VGEN_DBLK_SZ_2048	2048	/* data buffer size 2K bytes */
86 #define	VGEN_NRBUFS		512	/* number of receive bufs */
87 
88 #define	VGEN_TXDBLK_SZ		2048	/* Tx data buffer size */
89 
90 /* get the address of next tbuf */
91 #define	NEXTTBUF(ldcp, tbufp)	(((tbufp) + 1) == (ldcp)->tbufendp    \
92 		? (ldcp)->tbufp : ((tbufp) + 1))
93 
94 /* increment recv index */
95 #define	INCR_RXI(i, ldcp)	\
96 		((i) = (((i) + 1) & ((ldcp)->num_rxds - 1)))
97 
98 /* decrement recv index */
99 #define	DECR_RXI(i, ldcp)	\
100 		((i) = (((i) - 1) & ((ldcp)->num_rxds - 1)))
101 
102 /* increment tx index */
103 #define	INCR_TXI(i, ldcp)	\
104 		((i) = (((i) + 1) & ((ldcp)->num_txds - 1)))
105 
106 /* decrement tx index */
107 #define	DECR_TXI(i, ldcp)	\
108 		((i) = (((i) - 1) & ((ldcp)->num_txds - 1)))
109 
110 /* bounds check rx index */
111 #define	CHECK_RXI(i, ldcp)	\
112 		(((i) >= 0) && ((i) < (ldcp)->num_rxds))
113 
114 /* bounds check tx index */
115 #define	CHECK_TXI(i, ldcp)	\
116 		(((i) >= 0) && ((i) < (ldcp)->num_txds))
117 
118 /* private descriptor */
119 typedef struct vgen_priv_desc {
120 	uint64_t		flags;		/* flag bits */
121 	vnet_public_desc_t	*descp;		/* associated public desc */
122 	ldc_mem_handle_t	memhandle;	/* mem handle for data */
123 	caddr_t			datap;		/* prealloc'd tx data buffer */
124 	uint64_t		datalen;	/* total actual datalen */
125 	uint64_t		ncookies;	/* num ldc_mem_cookies */
126 	ldc_mem_cookie_t	memcookie[MAX_COOKIES];	/* data cookies */
127 } vgen_private_desc_t;
128 
129 /*
130  * Handshake parameters (per vio_mailbox.h) of each ldc end point, used
131  * during handshake negotiation.
132  */
133 typedef struct vgen_handshake_params {
134 	/* version specific params */
135 	uint16_t	ver_major;		/* major version number */
136 	uint16_t	ver_minor;		/* minor version number */
137 	uint8_t		dev_class;		/* device class */
138 
139 	/* attributes specific params */
140 	uint64_t		mtu;		/* max transfer unit size */
141 	uint64_t		addr;		/* address of the device */
142 	uint8_t			addr_type;	/* type of address */
143 	uint8_t			xfer_mode;	/* SHM or PKT */
144 	uint16_t		ack_freq;	/* dring data ack freq */
145 
146 	/* descriptor ring params */
147 	uint32_t		num_desc;	/* # of descriptors in ring */
148 	uint32_t		desc_size;	/* size of descriptor */
149 	ldc_mem_cookie_t	dring_cookie;	/* desc ring cookie */
150 	uint32_t		num_dcookies;	/* # of dring cookies */
151 	uint64_t		dring_ident;	/* ident=0 for INFO msg */
152 	boolean_t		dring_ready;	/* dring ready flag */
153 } vgen_hparams_t;
154 
155 /* version info */
156 typedef struct vgen_ver {
157 	uint16_t	ver_major;		/* major version number */
158 	uint16_t	ver_minor;		/* minor version number */
159 } vgen_ver_t;
160 
161 /* Channel information associated with a vgen-port */
162 typedef struct vgen_ldc {
163 
164 	struct vgen_ldc		*nextp;		/* next ldc in the list */
165 	struct vgen_port	*portp;		/* associated port */
166 
167 	/*
168 	 * Locks:
169 	 * locking hierarchy when more than one lock is held concurrently:
170 	 * cblock > rxlock > txlock > tclock.
171 	 */
172 	kmutex_t		cblock;		/* sync callback processing */
173 	kmutex_t		txlock;		/* protect txd alloc */
174 	kmutex_t		tclock;		/* tx reclaim lock */
175 	kmutex_t		wrlock;		/* sync transmits */
176 	kmutex_t		rxlock;		/* sync reception */
177 
178 	/* channel info from ldc layer */
179 	uint64_t		ldc_id;		/* channel number */
180 	uint64_t		ldc_handle;	/* channel handle */
181 	ldc_status_t		ldc_status;	/* channel status */
182 
183 	/* handshake info */
184 	vgen_ver_t		vgen_versions[VGEN_NUM_VER]; /* versions */
185 	int			hphase;		/* handshake phase */
186 	int			hstate;		/* handshake state bits */
187 	uint32_t		local_sid;	/* local session id */
188 	uint32_t		peer_sid;	/* session id of peer */
189 	vgen_hparams_t		local_hparams;	/* local handshake params */
190 	vgen_hparams_t		peer_hparams;	/* peer's handshake params */
191 	timeout_id_t		htid;		/* handshake wd timeout id */
192 	timeout_id_t		cancel_htid;	/* cancel handshake watchdog */
193 
194 	/* transmit and receive descriptor ring info */
195 	ldc_dring_handle_t	tx_dhandle;	/* tx descriptor ring handle */
196 	ldc_mem_cookie_t	tx_dcookie;	/* tx descriptor ring cookie */
197 	ldc_dring_handle_t	rx_dhandle;	/* mapped rx dhandle */
198 	ldc_mem_cookie_t	rx_dcookie;	/* rx descriptor ring cookie */
199 	vnet_public_desc_t	*txdp;		/* transmit frame descriptors */
200 	vnet_public_desc_t	*txdendp;	/* txd ring end */
201 	vgen_private_desc_t	*tbufp;		/* associated tx resources */
202 	vgen_private_desc_t	*tbufendp;	/* tbuf ring end */
203 	vgen_private_desc_t	*next_tbufp;	/* next free tbuf */
204 	vgen_private_desc_t	*cur_tbufp;	/* next reclaim tbuf */
205 	uint64_t		next_txseq;	/* next tx sequence number */
206 	uint32_t		num_txdcookies;	/* # of tx dring cookies */
207 	uint32_t		num_rxdcookies;	/* # of rx dring cookies */
208 	uint32_t		next_txi;	/* next tx descriptor index */
209 	uint32_t		num_txds;	/* number of tx descriptors */
210 	clock_t			reclaim_lbolt;	/* time of last tx reclaim */
211 	timeout_id_t		wd_tid;		/* tx watchdog timeout id */
212 	vnet_public_desc_t	*rxdp;		/* receive frame descriptors */
213 	uint64_t		next_rxseq;	/* next expected recv seqnum */
214 	uint32_t		next_rxi;	/* next expected recv index */
215 	uint32_t		num_rxds;	/* number of rx descriptors */
216 	caddr_t			tx_datap;	/* prealloc'd tx data area */
217 	vio_multi_pool_t	vmp;		/* rx mblk pools */
218 
219 	/* misc */
220 	uint32_t		flags;		/* flags */
221 	boolean_t		need_resched;	/* reschedule tx */
222 	boolean_t		need_ldc_reset; /* ldc_reset needed */
223 	uint32_t		hretries;	/* handshake retry count */
224 	boolean_t		resched_peer;	/* send tx msg to peer */
225 	uint32_t		resched_peer_txi; /* tx index to resched peer */
226 
227 	/* receive thread field */
228 	kthread_t		*rcv_thread;	/* receive thread */
229 	uint32_t		rcv_thr_flags;	/* receive thread flags */
230 	kmutex_t		rcv_thr_lock;	/* lock for receive thread */
231 	kcondvar_t		rcv_thr_cv;	/* cond.var for recv thread */
232 	mblk_t			*rcv_mhead;	/* received mblks head */
233 	mblk_t			*rcv_mtail;	/* received mblks tail */
234 	ddi_softint_handle_t	soft_handle;	/* soft intr handle */
235 	int			soft_pri;	/* soft int priority */
236 	kmutex_t		soft_lock;	/* lock for soft intr handler */
237 
238 	/* channel statistics */
239 	vgen_stats_t		stats;		/* channel statistics */
240 	kstat_t			*ksp;		/* channel kstats */
241 
242 } vgen_ldc_t;
243 
244 /* Channel list structure */
245 typedef struct vgen_ldclist_s {
246 	vgen_ldc_t	*headp;		/* head of the list */
247 	krwlock_t	rwlock;		/* sync access to the list */
248 	int		num_ldcs;	/* number of channels in the list */
249 } vgen_ldclist_t;
250 
251 /* port information  structure */
252 typedef struct vgen_port {
253 	struct vgen_port	*nextp;		/* next port in the list */
254 	struct vgen		*vgenp;		/* associated vgen_t */
255 	int			port_num;	/* port number */
256 	vgen_ldclist_t		ldclist;	/* list of ldcs for this port */
257 	struct ether_addr	macaddr;	/* mac address of peer */
258 } vgen_port_t;
259 
260 /* port list structure */
261 typedef struct vgen_portlist {
262 	vgen_port_t	*headp;		/* head of ports */
263 	vgen_port_t	*tailp;		/* tail */
264 	krwlock_t	rwlock;		/* sync access to the port list */
265 } vgen_portlist_t;
266 
267 /* vgen instance information  */
268 typedef struct vgen {
269 	void			*vnetp;		/* associated vnet instance */
270 	dev_info_t		*vnetdip;	/* dip of vnet */
271 	uint8_t			macaddr[ETHERADDRL];	/* mac addr of vnet */
272 	kmutex_t		lock;		/* synchornize ops */
273 	int			flags;		/* flags */
274 	vgen_portlist_t		vgenports;	/* Port List */
275 	mdeg_node_spec_t	*mdeg_parentp;
276 	mdeg_handle_t		mdeg_hdl;
277 	vgen_port_t		*vsw_portp;	/* port connected to vsw */
278 	mac_register_t		*macp;		/* vgen mac ops */
279 	struct ether_addr	*mctab;		/* multicast addr table */
280 	uint32_t		mcsize;		/* allocated size of mctab */
281 	uint32_t		mccount;	/* # of valid addrs in mctab */
282 	vio_mblk_pool_t		*rmp;		/* rx mblk pools to be freed */
283 } vgen_t;
284 
285 #ifdef __cplusplus
286 }
287 #endif
288 
289 #endif	/* _VNET_GEN_H */
290