xref: /titanic_50/usr/src/uts/sun4v/sys/vdc.h (revision 9512fe850e98fdd448c638ca63fdd92a8a510255)
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  * Definitions of strings to be used to create device node properties.
72  * (vdc uses the capitalised versions of these properties as they are 64-bit)
73  */
74 #define	VDC_NBLOCKS_PROP_NAME		"Nblocks"
75 #define	VDC_SIZE_PROP_NAME		"Size"
76 
77 /*
78  * Definitions of MD nodes/properties.
79  */
80 #define	VDC_MD_CHAN_NAME		"channel-endpoint"
81 #define	VDC_MD_VDEV_NAME		"virtual-device"
82 #define	VDC_MD_DISK_NAME		"disk"
83 #define	VDC_MD_CFG_HDL			"cfg-handle"
84 #define	VDC_ID_PROP			"id"
85 
86 /*
87  * Definition of actions to be carried out when processing the sequence ID
88  * of a message received from the vDisk server. The function verifying the
89  * sequence number checks the 'seq_num_xxx' fields in the soft state and
90  * returns whether the message should be processed (VDC_SEQ_NUM_TODO) or
91  * whether it was it was previously processed (VDC_SEQ_NUM_SKIP).
92  */
93 #define	VDC_SEQ_NUM_INVALID		-1	/* Error */
94 #define	VDC_SEQ_NUM_SKIP		0	/* Request already processed */
95 #define	VDC_SEQ_NUM_TODO		1	/* Request needs processing */
96 
97 /*
98  * Scheme to store the instance number and the slice number in the minor number.
99  * (Uses the same format and definitions as the sd(7D) driver)
100  */
101 #define	VD_MAKE_DEV(instance, minor)	((instance << SDUNIT_SHIFT) | minor)
102 
103 /*
104  * variables controlling how long to wait before timing out and how many
105  * retries to attempt before giving up when communicating with vds.
106  *
107  * These values need to be sufficiently large so that a guest can survive
108  * the reboot of the service domain.
109  */
110 #define	VDC_RETRIES	10
111 
112 #define	VDC_USEC_TIMEOUT_MIN	(30 * MICROSEC)		/* 30 sec */
113 
114 /*
115  * This macro returns the number of Hz that the vdc driver should wait before
116  * a timeout is triggered. The 'timeout' parameter specifiecs the wait
117  * time in Hz. The 'mul' parameter allows for a multiplier to be
118  * specified allowing for a backoff to be implemented (e.g. using the
119  * retry number as a multiplier) where the wait time will get longer if
120  * there is no response on the previous retry.
121  */
122 #define	VD_GET_TIMEOUT_HZ(timeout, mul)	\
123 	(ddi_get_lbolt() + ((timeout) * MAX(1, (mul))))
124 
125 /*
126  * Macros to manipulate Descriptor Ring variables in the soft state
127  * structure.
128  */
129 #define	VDC_GET_NEXT_REQ_ID(vdc)	((vdc)->req_id++)
130 
131 #define	VDC_GET_DRING_ENTRY_PTR(vdc, idx)	\
132 		(vd_dring_entry_t *)((vdc)->dring_mem_info.vaddr +	\
133 			(idx * (vdc)->dring_entry_size))
134 
135 #define	VDC_MARK_DRING_ENTRY_FREE(vdc, idx)			\
136 	{ \
137 		vd_dring_entry_t *dep = NULL;				\
138 		ASSERT(vdc != NULL);					\
139 		ASSERT((idx >= 0) && (idx < vdc->dring_len));		\
140 		ASSERT(vdc->dring_mem_info.vaddr != NULL);		\
141 		dep = (vd_dring_entry_t *)(vdc->dring_mem_info.vaddr +	\
142 			(idx * vdc->dring_entry_size));			\
143 		ASSERT(dep != NULL);					\
144 		dep->hdr.dstate = VIO_DESC_FREE;			\
145 	}
146 
147 /* Initialise the Session ID and Sequence Num in the DRing msg */
148 #define	VDC_INIT_DRING_DATA_MSG_IDS(dmsg, vdc)		\
149 		ASSERT(vdc != NULL);			\
150 		dmsg.tag.vio_sid = vdc->session_id;	\
151 		dmsg.seq_num = vdc->seq_num;
152 
153 /*
154  * The states the message processing thread can be in.
155  */
156 typedef enum vdc_thr_state {
157 	VDC_THR_RUNNING,	/* thread is running & ready to process */
158 	VDC_THR_STOP,		/* The detach func signals the thread to stop */
159 	VDC_THR_DONE		/* Thread has exited */
160 } vdc_thr_state_t;
161 
162 /*
163  * Local Descriptor Ring entry
164  *
165  * vdc creates a Local (private) descriptor ring the same size as the
166  * public descriptor ring it exports to vds.
167  */
168 typedef struct vdc_local_desc {
169 	kmutex_t		lock;		/* protects all fields */
170 	kcondvar_t		cv;		/* indicate processing done */
171 	int			flags;		/* Dring entry state, etc */
172 	int			operation;	/* VD_OP_xxx to be performed */
173 	caddr_t			addr;		/* addr passed in by consumer */
174 	caddr_t			align_addr;	/* used if addr non-aligned */
175 	struct buf 		*buf;		/* buf passed to strategy() */
176 	ldc_mem_handle_t	desc_mhdl;	/* Mem handle of buf */
177 	vd_dring_entry_t	*dep;		/* public Dring Entry Pointer */
178 } vdc_local_desc_t;
179 
180 /*
181  * vdc soft state structure
182  */
183 typedef struct vdc {
184 	kmutex_t	attach_lock;	/* used by CV which waits in attach */
185 	kcondvar_t	attach_cv;	/* signal when attach can finish */
186 
187 	kmutex_t	lock;		/* protects next 2 sections of vars */
188 	kcondvar_t	cv;		/* signal when upper layers can send */
189 
190 	int		initialized;	/* keeps track of what's init'ed */
191 	int		hshake_cnt;	/* number of failed handshakes */
192 	int		open;		/* count of outstanding opens */
193 	int		dkio_flush_pending;	/* # outstanding DKIO flushes */
194 
195 	uint64_t	session_id;	/* common ID sent with all messages */
196 	uint64_t	seq_num;	/* most recent sequence num generated */
197 	uint64_t	seq_num_reply;	/* Last seq num ACK/NACK'ed by vds */
198 	uint64_t	req_id;		/* Most recent Request ID generated */
199 	uint64_t	req_id_proc;	/* Last request ID processed by vdc */
200 	vd_state_t	state;		/* Current handshake state */
201 
202 	dev_info_t	*dip;		/* device info pointer */
203 	int		instance;	/* driver instance number */
204 	vio_ver_t	ver;		/* version number agreed with server */
205 	vd_disk_type_t	vdisk_type;	/* type of device/disk being imported */
206 	vd_disk_label_t vdisk_label; 	/* label type of device/disk imported */
207 	uint64_t	vdisk_size;	/* device size in blocks */
208 	uint64_t	max_xfer_sz;	/* maximum block size of a descriptor */
209 	uint64_t	block_size;	/* device block size used */
210 	struct dk_label	*label;		/* structure to store disk label */
211 	struct dk_cinfo	*cinfo;		/* structure to store DKIOCINFO data */
212 	struct dk_minfo	*minfo;		/* structure for DKIOCGMEDIAINFO data */
213 	struct vtoc	*vtoc;		/* structure to store VTOC data */
214 	ddi_devid_t	devid;		/* device id */
215 
216 	/*
217 	 * The mutex 'msg_proc_lock' protects the following group of fields.
218 	 *
219 	 * The callback function checks to see if LDC triggered it due to
220 	 * there being data available and the callback will signal to
221 	 * the message processing thread waiting on 'msg_proc_cv'.
222 	 */
223 	kmutex_t		msg_proc_lock;
224 	kcondvar_t		msg_proc_cv;
225 	boolean_t		msg_pending;
226 	vdc_thr_state_t		msg_proc_thr_state;
227 	kthread_t		*msg_proc_thr_id;
228 
229 	/*
230 	 * The mutex 'dring_lock'  protects the following group of fields.
231 	 */
232 	kmutex_t		dring_lock;
233 	ldc_mem_info_t		dring_mem_info;
234 	uint_t			dring_curr_idx;
235 	uint_t			dring_proc_idx;
236 	uint32_t		dring_len;
237 	uint32_t		dring_max_cookies;
238 	uint32_t		dring_cookie_count;
239 	uint32_t		dring_entry_size;
240 	boolean_t		dring_notify_server;
241 	ldc_mem_cookie_t	*dring_cookie;
242 	uint64_t		dring_ident;
243 
244 	vdc_local_desc_t	*local_dring;
245 
246 	uint64_t		ldc_id;
247 	ldc_status_t		ldc_state;
248 	ldc_handle_t		ldc_handle;
249 	ldc_dring_handle_t	ldc_dring_hdl;
250 } vdc_t;
251 
252 /*
253  * Debugging macros
254  */
255 #ifdef DEBUG
256 extern int	vdc_msglevel;
257 
258 #define	DMSG(err_level, format, ...)					\
259 	do {								\
260 		if (vdc_msglevel > err_level)				\
261 			cmn_err(CE_CONT, "?%s"format, __func__, __VA_ARGS__);\
262 		_NOTE(CONSTANTCONDITION)				\
263 	} while (0);
264 
265 #define	VDC_DUMP_DRING_MSG(dmsgp)					\
266 		DMSG(0, "sq:%lu start:%d end:%d ident:%lu\n",		\
267 			dmsgp->seq_num, dmsgp->start_idx,		\
268 			dmsgp->end_idx, dmsgp->dring_ident);
269 
270 #else	/* !DEBUG */
271 #define	DMSG(err_level, ...)
272 #define	VDC_DUMP_DRING_MSG(dmsgp)
273 
274 #endif	/* !DEBUG */
275 
276 #ifdef	__cplusplus
277 }
278 #endif
279 
280 #endif	/* _VDC_H */
281