xref: /titanic_44/usr/src/uts/sun4v/sys/vdc.h (revision 67e3a03ed4a2813074d36330f062ed6e593a4937)
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 2008 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_DRING_INIT	0x0010	/* The DRing was created */
58 #define	VDC_DRING_BOUND	0x0020	/* The DRing was bound to an LDC channel */
59 #define	VDC_DRING_LOCAL	0x0040	/* The local private DRing was allocated */
60 #define	VDC_DRING_ENTRY	0x0080	/* At least one DRing entry was initialised */
61 #define	VDC_DRING	(VDC_DRING_INIT | VDC_DRING_BOUND |	\
62 				VDC_DRING_LOCAL | VDC_DRING_ENTRY)
63 #define	VDC_HANDSHAKE	0x0100	/* Indicates if a handshake is in progress */
64 #define	VDC_HANDSHAKE_STOP	0x0200	/* stop further handshakes */
65 
66 /*
67  * Definitions of strings to be used to create device node properties.
68  * (vdc uses the capitalised versions of these properties as they are 64-bit)
69  */
70 #define	VDC_NBLOCKS_PROP_NAME		"Nblocks"
71 #define	VDC_SIZE_PROP_NAME		"Size"
72 
73 /*
74  * Definitions of MD nodes/properties.
75  */
76 #define	VDC_MD_CHAN_NAME		"channel-endpoint"
77 #define	VDC_MD_VDEV_NAME		"virtual-device"
78 #define	VDC_MD_PORT_NAME		"virtual-device-port"
79 #define	VDC_MD_DISK_NAME		"disk"
80 #define	VDC_MD_CFG_HDL			"cfg-handle"
81 #define	VDC_MD_TIMEOUT			"vdc-timeout"
82 #define	VDC_MD_ID			"id"
83 
84 /*
85  * Definition of actions to be carried out when processing the sequence ID
86  * of a message received from the vDisk server. The function verifying the
87  * sequence number checks the 'seq_num_xxx' fields in the soft state and
88  * returns whether the message should be processed (VDC_SEQ_NUM_TODO) or
89  * whether it was it was previously processed (VDC_SEQ_NUM_SKIP).
90  */
91 #define	VDC_SEQ_NUM_INVALID		-1	/* Error */
92 #define	VDC_SEQ_NUM_SKIP		0	/* Request already processed */
93 #define	VDC_SEQ_NUM_TODO		1	/* Request needs processing */
94 
95 /*
96  * Macros to get UNIT and PART number
97  */
98 #define	VDCUNIT_SHIFT	3
99 #define	VDCPART_MASK	7
100 
101 #define	VDCUNIT(dev)	(getminor((dev)) >> VDCUNIT_SHIFT)
102 #define	VDCPART(dev)	(getminor((dev)) &  VDCPART_MASK)
103 
104 /*
105  * Scheme to store the instance number and the slice number in the minor number.
106  * (NOTE: Uses the same format and definitions as the sd(7D) driver)
107  */
108 #define	VD_MAKE_DEV(instance, minor)	((instance << VDCUNIT_SHIFT) | minor)
109 
110 /*
111  * variables controlling how long to wait before timing out and how many
112  * retries to attempt before giving up when communicating with vds.
113  *
114  * These values need to be sufficiently large so that a guest can survive
115  * the reboot of the service domain.
116  */
117 #define	VDC_RETRIES	10
118 
119 #define	VDC_USEC_TIMEOUT_MIN	(30 * MICROSEC)		/* 30 sec */
120 
121 /*
122  * This macro returns the number of Hz that the vdc driver should wait before
123  * a timeout is triggered. The 'timeout' parameter specifiecs the wait
124  * time in Hz. The 'mul' parameter allows for a multiplier to be
125  * specified allowing for a backoff to be implemented (e.g. using the
126  * retry number as a multiplier) where the wait time will get longer if
127  * there is no response on the previous retry.
128  */
129 #define	VD_GET_TIMEOUT_HZ(timeout, mul)	\
130 	(ddi_get_lbolt() + ((timeout) * MAX(1, (mul))))
131 
132 /*
133  * Macros to manipulate Descriptor Ring variables in the soft state
134  * structure.
135  */
136 #define	VDC_GET_NEXT_REQ_ID(vdc)	((vdc)->req_id++)
137 
138 #define	VDC_GET_DRING_ENTRY_PTR(vdc, idx)	\
139 		(vd_dring_entry_t *)(uintptr_t)((vdc)->dring_mem_info.vaddr + \
140 			(idx * (vdc)->dring_entry_size))
141 
142 #define	VDC_MARK_DRING_ENTRY_FREE(vdc, idx)			\
143 	{ \
144 		vd_dring_entry_t *dep = NULL;				\
145 		ASSERT(vdc != NULL);					\
146 		ASSERT(idx < vdc->dring_len);		\
147 		ASSERT(vdc->dring_mem_info.vaddr != NULL);		\
148 		dep = (vd_dring_entry_t *)(uintptr_t)			\
149 			(vdc->dring_mem_info.vaddr +	\
150 			(idx * vdc->dring_entry_size));			\
151 		ASSERT(dep != NULL);					\
152 		dep->hdr.dstate = VIO_DESC_FREE;			\
153 	}
154 
155 /* Initialise the Session ID and Sequence Num in the DRing msg */
156 #define	VDC_INIT_DRING_DATA_MSG_IDS(dmsg, vdc)		\
157 		ASSERT(vdc != NULL);			\
158 		dmsg.tag.vio_sid = vdc->session_id;	\
159 		dmsg.seq_num = vdc->seq_num;
160 
161 /*
162  * The states that the read thread can be in.
163  */
164 typedef enum vdc_rd_state {
165 	VDC_READ_IDLE,			/* idling - conn is not up */
166 	VDC_READ_WAITING,		/* waiting for data */
167 	VDC_READ_PENDING,		/* pending data avail for read */
168 	VDC_READ_RESET			/* channel was reset - stop reads */
169 } vdc_rd_state_t;
170 
171 /*
172  * The states that the vdc-vds connection can be in.
173  */
174 typedef enum vdc_state {
175 	VDC_STATE_INIT,			/* device is initialized */
176 	VDC_STATE_INIT_WAITING,		/* waiting for ldc connection */
177 	VDC_STATE_NEGOTIATE,		/* doing handshake negotiation */
178 	VDC_STATE_HANDLE_PENDING,	/* handle requests in backup dring */
179 	VDC_STATE_RUNNING,		/* running and accepting requests */
180 	VDC_STATE_DETACH,		/* detaching */
181 	VDC_STATE_RESETTING		/* resetting connection with vds */
182 } vdc_state_t;
183 
184 /*
185  * The states that the vdc instance can be in.
186  */
187 typedef enum vdc_lc_state {
188 	VDC_LC_ATTACHING,	/* driver is attaching */
189 	VDC_LC_ONLINE,		/* driver is attached and online */
190 	VDC_LC_DETACHING	/* driver is detaching */
191 } vdc_lc_state_t;
192 
193 /*
194  * Local Descriptor Ring entry
195  *
196  * vdc creates a Local (private) descriptor ring the same size as the
197  * public descriptor ring it exports to vds.
198  */
199 
200 typedef enum {
201 	VIO_read_dir,		/* read data from server */
202 	VIO_write_dir,		/* write data to server */
203 	VIO_both_dir		/* transfer both in and out in same buffer */
204 } vio_desc_direction_t;
205 
206 typedef enum {
207 	CB_STRATEGY,		/* non-blocking strategy call */
208 	CB_SYNC			/* synchronous operation */
209 } vio_cb_type_t;
210 
211 typedef struct vdc_local_desc {
212 	boolean_t		is_free;	/* local state - inuse or not */
213 
214 	int			operation;	/* VD_OP_xxx to be performed */
215 	caddr_t			addr;		/* addr passed in by consumer */
216 	int			slice;
217 	diskaddr_t		offset;		/* disk offset */
218 	size_t			nbytes;
219 	vio_cb_type_t		cb_type;	/* operation type blk/nonblk */
220 	void			*cb_arg;	/* buf passed to strategy() */
221 	vio_desc_direction_t	dir;		/* direction of transfer */
222 
223 	caddr_t			align_addr;	/* used if addr non-aligned */
224 	ldc_mem_handle_t	desc_mhdl;	/* Mem handle of buf */
225 	vd_dring_entry_t	*dep;		/* public Dring Entry Pointer */
226 
227 } vdc_local_desc_t;
228 
229 /*
230  * I/O queue used by failfast
231  */
232 typedef struct vdc_io {
233 	struct vdc_io	*vio_next;	/* next pending I/O in the queue */
234 	struct buf	*vio_buf;	/* buf for CB_STRATEGY I/O */
235 	clock_t		vio_qtime;	/* time the I/O was queued */
236 } vdc_io_t;
237 
238 /*
239  * Per vDisk server channel states
240  */
241 #define	VDC_LDC_INIT	0x0001
242 #define	VDC_LDC_CB	0x0002
243 #define	VDC_LDC_OPEN	0x0004
244 #define	VDC_LDC		(VDC_LDC_INIT | VDC_LDC_CB | VDC_LDC_OPEN)
245 
246 /*
247  * vDisk server information
248  */
249 typedef struct vdc_server {
250 	struct vdc_server	*next;			/* Next server */
251 	struct vdc		*vdcp;			/* Ptr to vdc struct */
252 	uint64_t		id;			/* Server port id */
253 	uint64_t		state;			/* Server state */
254 	uint64_t		ldc_id;			/* Server LDC id */
255 	ldc_handle_t		ldc_handle;		/* Server LDC handle */
256 	ldc_status_t		ldc_state;		/* Server LDC state */
257 	uint64_t		ctimeout;		/* conn tmout (secs) */
258 } vdc_server_t;
259 
260 /*
261  * vdc soft state structure
262  */
263 typedef struct vdc {
264 
265 	kmutex_t	lock;		/* protects next 2 sections of vars */
266 	kcondvar_t	running_cv;	/* signal when upper layers can send */
267 	kcondvar_t	initwait_cv;	/* signal when ldc conn is up */
268 	kcondvar_t	dring_free_cv;	/* signal when desc is avail */
269 	kcondvar_t	membind_cv;	/* signal when mem can be bound */
270 	boolean_t	self_reset;
271 
272 	int		initialized;	/* keeps track of what's init'ed */
273 	vdc_lc_state_t	lifecycle;	/* Current state of the vdc instance */
274 
275 	int		hshake_cnt;	/* number of failed handshakes */
276 	uint8_t		open[OTYPCNT];	/* mask of opened slices */
277 	uint8_t		open_excl;	/* mask of exclusively opened slices */
278 	ulong_t		open_lyr[V_NUMPAR]; /* number of layered opens */
279 	int		dkio_flush_pending; /* # outstanding DKIO flushes */
280 	int		validate_pending; /* # outstanding validate request */
281 	vd_disk_label_t vdisk_label; 	/* label type of device/disk imported */
282 	struct vtoc	*vtoc;		/* structure to store VTOC data */
283 	struct dk_geom	*geom;		/* structure to store geometry data */
284 	vd_slice_t	slice[V_NUMPAR]; /* logical partitions */
285 
286 	kthread_t	*msg_proc_thr;	/* main msg processing thread */
287 
288 	kmutex_t	read_lock;	/* lock to protect read */
289 	kcondvar_t	read_cv;	/* cv to wait for READ events */
290 	vdc_rd_state_t	read_state;	/* current read state */
291 
292 	uint32_t	sync_op_cnt;	/* num of active sync operations */
293 	boolean_t	sync_op_pending; /* sync operation is pending */
294 	boolean_t	sync_op_blocked; /* blocked waiting to do sync op */
295 	uint32_t	sync_op_status;	/* status of sync operation */
296 	kcondvar_t	sync_pending_cv; /* cv wait for sync op to finish */
297 	kcondvar_t	sync_blocked_cv; /* cv wait for other syncs to finish */
298 
299 	uint64_t	session_id;	/* common ID sent with all messages */
300 	uint64_t	seq_num;	/* most recent sequence num generated */
301 	uint64_t	seq_num_reply;	/* Last seq num ACK/NACK'ed by vds */
302 	uint64_t	req_id;		/* Most recent Request ID generated */
303 	uint64_t	req_id_proc;	/* Last request ID processed by vdc */
304 	vdc_state_t	state;		/* Current disk client-server state */
305 
306 	dev_info_t	*dip;		/* device info pointer */
307 	int		instance;	/* driver instance number */
308 
309 	vio_ver_t	ver;		/* version number agreed with server */
310 	vd_disk_type_t	vdisk_type;	/* type of device/disk being imported */
311 	uint32_t	vdisk_media;	/* physical media type of vDisk */
312 	uint64_t	vdisk_size;	/* device size in blocks */
313 	uint64_t	max_xfer_sz;	/* maximum block size of a descriptor */
314 	uint64_t	block_size;	/* device block size used */
315 	uint64_t	operations;	/* bitmask of ops. server supports */
316 	struct dk_cinfo	*cinfo;		/* structure to store DKIOCINFO data */
317 	struct dk_minfo	*minfo;		/* structure for DKIOCGMEDIAINFO data */
318 	ddi_devid_t	devid;		/* device id */
319 	boolean_t	ctimeout_reached; /* connection timeout has expired */
320 
321 	/*
322 	 * The ownership fields are protected by the lock mutex. The
323 	 * ownership_lock mutex is used to serialize ownership operations;
324 	 * it should be acquired before the lock mutex.
325 	 */
326 	kmutex_t	ownership_lock;		/* serialize ownership ops */
327 	int		ownership;		/* ownership status flags */
328 	kthread_t	*ownership_thread;	/* ownership thread */
329 	kcondvar_t	ownership_cv;		/* cv for ownership update */
330 
331 	/*
332 	 * The failfast fields are protected by the lock mutex.
333 	 */
334 	kthread_t	*failfast_thread;	/* failfast thread */
335 	clock_t		failfast_interval;	/* interval in microsecs */
336 	kcondvar_t	failfast_cv;		/* cv for failfast update */
337 	kcondvar_t	failfast_io_cv;		/* cv wait for I/O to finish */
338 	vdc_io_t	*failfast_io_queue;	/* failfast io queue */
339 
340 	/*
341 	 * kstats used to store I/O statistics consumed by iostat(1M).
342 	 * These are protected by the lock mutex.
343 	 */
344 	kstat_t		*io_stats;
345 	kstat_t		*err_stats;
346 
347 	ldc_dring_handle_t	dring_hdl;		/* dring handle */
348 	ldc_mem_info_t		dring_mem_info;		/* dring information */
349 	uint_t			dring_curr_idx;		/* current index */
350 	uint32_t		dring_len;		/* dring length */
351 	uint32_t		dring_max_cookies;	/* dring max cookies */
352 	uint32_t		dring_cookie_count;	/* num cookies */
353 	uint32_t		dring_entry_size;	/* descriptor size */
354 	ldc_mem_cookie_t 	*dring_cookie;		/* dring cookies */
355 	uint64_t		dring_ident;		/* dring ident */
356 
357 	uint64_t		threads_pending; 	/* num of threads */
358 
359 	vdc_local_desc_t	*local_dring;		/* local dring */
360 	vdc_local_desc_t	*local_dring_backup;	/* local dring backup */
361 	int			local_dring_backup_tail; /* backup dring tail */
362 	int			local_dring_backup_len;	/* backup dring len */
363 
364 	int			num_servers;		/* no. of servers */
365 	vdc_server_t		*server_list;		/* vdisk server list */
366 	vdc_server_t		*curr_server;		/* curr vdisk server */
367 } vdc_t;
368 
369 /*
370  * Ownership status flags
371  */
372 #define	VDC_OWNERSHIP_NONE	0x00 /* no ownership wanted */
373 #define	VDC_OWNERSHIP_WANTED	0x01 /* ownership is wanted */
374 #define	VDC_OWNERSHIP_GRANTED	0x02 /* ownership has been granted */
375 #define	VDC_OWNERSHIP_RESET	0x04 /* ownership has been reset */
376 
377 /*
378  * Reservation conflict panic message
379  */
380 #define	VDC_RESV_CONFLICT_FMT_STR	"Reservation Conflict\nDisk: "
381 #define	VDC_RESV_CONFLICT_FMT_LEN 	(sizeof (VDC_RESV_CONFLICT_FMT_STR))
382 
383 /*
384  * Debugging macros
385  */
386 #ifdef DEBUG
387 extern int	vdc_msglevel;
388 extern uint64_t	vdc_matchinst;
389 
390 #define	DMSG(_vdc, err_level, format, ...)				\
391 	do {								\
392 		if (vdc_msglevel > err_level &&				\
393 		(vdc_matchinst & (1ull << (_vdc)->instance)))		\
394 			cmn_err(CE_CONT, "?[%d,t@%p] %s: "format,	\
395 			(_vdc)->instance, (void *)curthread,		\
396 			__func__, __VA_ARGS__);				\
397 		_NOTE(CONSTANTCONDITION)				\
398 	} while (0);
399 
400 #define	DMSGX(err_level, format, ...)					\
401 	do {								\
402 		if (vdc_msglevel > err_level)				\
403 			cmn_err(CE_CONT, "?%s: "format, __func__, __VA_ARGS__);\
404 		_NOTE(CONSTANTCONDITION)				\
405 	} while (0);
406 
407 #define	VDC_DUMP_DRING_MSG(dmsgp)					\
408 		DMSGX(0, "sq:%lu start:%d end:%d ident:%lu\n",		\
409 			dmsgp->seq_num, dmsgp->start_idx,		\
410 			dmsgp->end_idx, dmsgp->dring_ident);
411 
412 #else	/* !DEBUG */
413 #define	DMSG(err_level, ...)
414 #define	DMSGX(err_level, format, ...)
415 #define	VDC_DUMP_DRING_MSG(dmsgp)
416 
417 #endif	/* !DEBUG */
418 
419 #ifdef	__cplusplus
420 }
421 #endif
422 
423 #endif	/* _VDC_H */
424