xref: /illumos-gate/usr/src/uts/sun4v/sys/vdc.h (revision 208e825d0597a017edee1b095c64040043c0c673)
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	_VDC_H
28 #define	_VDC_H
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 /*
33  * Virtual disk client implementation definitions
34  */
35 
36 #include <sys/sysmacros.h>
37 #include <sys/note.h>
38 
39 #include <sys/ldc.h>
40 #include <sys/vio_mailbox.h>
41 #include <sys/vdsk_mailbox.h>
42 #include <sys/vdsk_common.h>
43 
44 #ifdef	__cplusplus
45 extern "C" {
46 #endif
47 
48 #define	VDC_DRIVER_NAME		"vdc"
49 
50 /*
51  * Bit-field values to indicate if parts of the vdc driver are initialised.
52  */
53 #define	VDC_SOFT_STATE	0x0001
54 #define	VDC_LOCKS	0x0002
55 #define	VDC_MINOR	0x0004
56 #define	VDC_THREAD	0x0008
57 #define	VDC_LDC		0x0010
58 #define	VDC_LDC_INIT	0x0020
59 #define	VDC_LDC_CB	0x0040
60 #define	VDC_LDC_OPEN	0x0080
61 #define	VDC_DRING_INIT	0x0100	/* The DRing was created */
62 #define	VDC_DRING_BOUND	0x0200	/* The DRing was bound to an LDC channel */
63 #define	VDC_DRING_LOCAL	0x0400	/* The local private DRing was allocated */
64 #define	VDC_DRING_ENTRY	0x0800	/* At least one DRing entry was initialised */
65 #define	VDC_DRING	(VDC_DRING_INIT | VDC_DRING_BOUND |	\
66 				VDC_DRING_LOCAL | VDC_DRING_ENTRY)
67 #define	VDC_HANDSHAKE	0x1000	/* Indicates if a handshake is in progress */
68 #define	VDC_HANDSHAKE_STOP	0x2000	/* stop further handshakes */
69 
70 /*
71  * Bit-field values to indicate status of local DRing entry
72  *
73  * The lowest 8 bits are reserved for the DRing state.
74  */
75 #define	VDC_ALLOC_HANDLE	0x10
76 
77 /*
78  * Definitions of strings to be used to create device node properties.
79  * (vdc uses the capitalised versions of these properties as they are 64-bit)
80  */
81 #define	VDC_NBLOCKS_PROP_NAME		"Nblocks"
82 #define	VDC_SIZE_PROP_NAME		"Size"
83 
84 /*
85  * Definitions of MD nodes/properties.
86  */
87 #define	VDC_MD_CHAN_NAME		"channel-endpoint"
88 #define	VDC_MD_VDEV_NAME		"virtual-device"
89 #define	VDC_MD_DISK_NAME		"disk"
90 #define	VDC_MD_CFG_HDL			"cfg-handle"
91 #define	VDC_ID_PROP			"id"
92 
93 /*
94  * Scheme to store the instance number and the slice number in the minor number.
95  * (Uses the same format and definitions as the sd(7D) driver)
96  */
97 #define	VD_MAKE_DEV(instance, minor)	((instance << SDUNIT_SHIFT) | minor)
98 
99 /*
100  * variables controlling how long to wait before timing out and how many
101  * retries to attempt before giving up when communicating with vds.
102  */
103 #define	VDC_RETRIES	10
104 
105 #define	VDC_USEC_TIMEOUT_MIN	(30 * MICROSEC)		/* 30 sec */
106 
107 #define	VD_GET_TIMEOUT_HZ(mul)	\
108 	(ddi_get_lbolt() + (vdc_hz_timeout * MAX(1, mul)))
109 
110 /*
111  * Macros to manipulate Descriptor Ring variables in the soft state
112  * structure.
113  */
114 #define	VDC_GET_NEXT_REQ_ID(vdc)	((vdc->req_id)++)
115 
116 #define	VDC_GET_DRING_ENTRY_PTR(vdc, idx)	\
117 		(vd_dring_entry_t *)(vdc->dring_mem_info.vaddr +	\
118 			(idx * vdc->dring_entry_size))
119 
120 #define	VDC_MARK_DRING_ENTRY_FREE(vdc, idx)			\
121 	{ \
122 		vd_dring_entry_t *dep = NULL;				\
123 		ASSERT(vdc != NULL);					\
124 		ASSERT((idx >= 0) && (idx < VD_DRING_LEN));		\
125 		ASSERT(vdc->dring_mem_info.vaddr != NULL);		\
126 		dep = (vd_dring_entry_t *)(vdc->dring_mem_info.vaddr +	\
127 			(idx * vdc->dring_entry_size));			\
128 		ASSERT(dep != NULL);					\
129 		dep->hdr.dstate = VIO_DESC_FREE;			\
130 	}
131 
132 /* Initialise the Session ID and Sequence Num in the DRing msg */
133 #define	VDC_INIT_DRING_DATA_MSG_IDS(dmsg, vdc)		\
134 		ASSERT(vdc != NULL);			\
135 		dmsg.tag.vio_sid = vdc->session_id;	\
136 		dmsg.seq_num = ++(vdc->seq_num);
137 
138 /*
139  * The states the message processing thread can be in.
140  */
141 typedef enum vdc_thr_state {
142 	VDC_THR_RUNNING,	/* thread is running & ready to process */
143 	VDC_THR_STOP,		/* The detach func signals the thread to stop */
144 	VDC_THR_DONE		/* Thread has exited */
145 } vdc_thr_state_t;
146 
147 /*
148  * Local Descriptor Ring entry
149  *
150  * vdc creates a Local (private) descriptor ring the same size as the
151  * public descriptor ring it exports to vds.
152  */
153 typedef struct vdc_local_desc {
154 	kmutex_t		lock;		/* protects all fields */
155 	kcondvar_t		cv;		/* indicate processing done */
156 	int			flags;		/* Dring entry state, etc */
157 	int			operation;	/* VD_OP_xxx to be performed */
158 	caddr_t			addr;		/* addr passed in by consumer */
159 	caddr_t			align_addr;	/* used if addr non-aligned */
160 	struct buf 		*buf;		/* buf passed to strategy() */
161 	ldc_mem_handle_t	desc_mhdl;	/* Mem handle of buf */
162 	vd_dring_entry_t	*dep;		/* public Dring Entry Pointer */
163 } vdc_local_desc_t;
164 
165 /*
166  * vdc soft state structure
167  */
168 typedef struct vdc {
169 	kmutex_t	attach_lock;	/* used by CV which waits in attach */
170 	kcondvar_t	attach_cv;	/* signal when attach can finish */
171 
172 	kmutex_t	lock;		/* protects next 2 sections of vars */
173 	kcondvar_t	cv;		/* signal when upper layers can send */
174 
175 	dev_info_t	*dip;		/* device info pointer */
176 	int		instance;	/* driver instance number */
177 	int		initialized;	/* keeps track of what's init'ed */
178 	int		open;		/* count of outstanding opens */
179 	int		dkio_flush_pending;	/* # outstanding DKIO flushes */
180 
181 	uint64_t	session_id;	/* common ID sent with all messages */
182 	uint64_t	seq_num;	/* most recent sequence num generated */
183 	uint64_t	seq_num_reply;	/* Last seq num ACK/NACK'ed by vds */
184 	uint64_t	req_id;		/* Most recent Request ID generated */
185 	vd_state_t	state;		/* Current handshake state */
186 	vd_disk_type_t	vdisk_type;	/* type of device/disk being imported */
187 	uint64_t	vdisk_size;	/* device size in bytes */
188 	uint64_t	max_xfer_sz;	/* maximum block size of a descriptor */
189 	uint64_t	block_size;	/* device block size used */
190 	struct dk_cinfo	*cinfo;		/* structure to store DKIOCINFO data */
191 	struct dk_minfo	*minfo;		/* structure for DKIOCGMEDIAINFO data */
192 	struct vtoc	*vtoc;		/* structure to store VTOC data */
193 
194 	/*
195 	 * The mutex 'msg_proc_lock' protects the following group of fields.
196 	 *
197 	 * The callback function checks to see if LDC triggered it due to
198 	 * there being data available and the callback will signal to
199 	 * the message processing thread waiting on 'msg_proc_cv'.
200 	 */
201 	kmutex_t		msg_proc_lock;
202 	kcondvar_t		msg_proc_cv;
203 	boolean_t		msg_pending;
204 	vdc_thr_state_t		msg_proc_thr_state;
205 	kthread_t		*msg_proc_thr_id;
206 
207 	/*
208 	 * The mutex 'dring_lock'  protects the following group of fields.
209 	 */
210 	kmutex_t		dring_lock;
211 	ldc_mem_info_t		dring_mem_info;
212 	uint_t			dring_curr_idx;
213 	uint32_t		dring_len;
214 	uint32_t		dring_cookie_count;
215 	uint32_t		dring_entry_size;
216 	ldc_mem_cookie_t	*dring_cookie;
217 	uint64_t		dring_ident;
218 
219 	vdc_local_desc_t	*local_dring;
220 
221 	uint64_t		ldc_id;
222 	ldc_status_t		ldc_state;
223 	ldc_handle_t		ldc_handle;
224 	ldc_dring_handle_t	ldc_dring_hdl;
225 } vdc_t;
226 
227 /*
228  * Debugging macros
229  */
230 #ifdef DEBUG
231 extern int	vdc_msglevel;
232 
233 #define	PR0 if (vdc_msglevel > 0)	\
234 		vdc_msg
235 
236 #define	PR1 if (vdc_msglevel > 1)	\
237 		vdc_msg
238 
239 #define	PR2 if (vdc_msglevel > 2)	\
240 		vdc_msg
241 
242 #define	VDC_DUMP_DRING_MSG(dmsgp)					\
243 		vdc_msg("sq:%d start:%d end:%d ident:%x\n",		\
244 			dmsgp->seq_num, dmsgp->start_idx,		\
245 			dmsgp->end_idx, dmsgp->dring_ident);
246 
247 #else	/* !DEBUG */
248 #define	PR0(...)
249 #define	PR1(...)
250 #define	PR2(...)
251 
252 #define	VDC_DUMP_DRING_MSG(dmsgp)
253 
254 #endif	/* !DEBUG */
255 
256 #ifdef	__cplusplus
257 }
258 #endif
259 
260 #endif	/* _VDC_H */
261