xref: /titanic_41/usr/src/uts/sun4v/sys/vdc.h (revision ba2e4443695ee6a6f420a35cd4fc3d3346d22932)
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  * Scheme to store the instance number and the slice number in the minor number.
88  * (Uses the same format and definitions as the sd(7D) driver)
89  */
90 #define	VD_MAKE_DEV(instance, minor)	((instance << SDUNIT_SHIFT) | minor)
91 
92 /*
93  * variables controlling how long to wait before timing out and how many
94  * retries to attempt before giving up when communicating with vds.
95  */
96 #define	VDC_RETRIES	3
97 
98 #define	VDC_USEC_TIMEOUT_MIN	(30 * MICROSEC)		/* 30 sec */
99 
100 #define	VD_GET_TIMEOUT_HZ(mul)	\
101 	(ddi_get_lbolt() + (vdc_hz_timeout * MAX(1, mul)))
102 
103 /*
104  * Macros to manipulate Descriptor Ring variables in the soft state
105  * structure.
106  */
107 #define	VDC_GET_NEXT_REQ_ID(vdc)	((vdc->req_id)++)
108 
109 #define	VDC_GET_DRING_ENTRY_PTR(vdc, idx)	\
110 		(vd_dring_entry_t *)(vdc->dring_mem_info.vaddr +	\
111 			(idx * vdc->dring_entry_size))
112 
113 #define	VDC_MARK_DRING_ENTRY_FREE(vdc, idx)			\
114 	{ \
115 		vd_dring_entry_t *dep = NULL;				\
116 		ASSERT(vdc != NULL);					\
117 		ASSERT((idx >= 0) && (idx < VD_DRING_LEN));		\
118 		ASSERT(vdc->dring_mem_info.vaddr != NULL);		\
119 		dep = (vd_dring_entry_t *)(vdc->dring_mem_info.vaddr +	\
120 			(idx * vdc->dring_entry_size));			\
121 		ASSERT(dep != NULL);					\
122 		dep->hdr.dstate = VIO_DESC_FREE;			\
123 	}
124 
125 /* Initialise the Session ID and Sequence Num in the DRing msg */
126 #define	VDC_INIT_DRING_DATA_MSG_IDS(dmsg, vdc)		\
127 		ASSERT(vdc != NULL);			\
128 		dmsg.tag.vio_sid = vdc->session_id;	\
129 		dmsg.seq_num = vdc->seq_num;
130 
131 /*
132  * The states the message processing thread can be in.
133  */
134 typedef enum vdc_thr_state {
135 	VDC_THR_RUNNING,	/* thread is running & ready to process */
136 	VDC_THR_STOP,		/* The detach func signals the thread to stop */
137 	VDC_THR_DONE		/* Thread has exited */
138 } vdc_thr_state_t;
139 
140 /*
141  * Local Descriptor Ring entry
142  *
143  * vdc creates a Local (private) descriptor ring the same size as the
144  * public descriptor ring it exports to vds.
145  */
146 typedef struct vdc_local_desc {
147 	kmutex_t		lock;		/* protects all fields */
148 	kcondvar_t		cv;		/* indicate processing done */
149 	int			flags;		/* Dring entry state, etc */
150 	int			operation;	/* VD_OP_xxx to be performed */
151 	caddr_t			addr;		/* addr passed in by consumer */
152 	caddr_t			align_addr;	/* used if addr non-aligned */
153 	struct buf 		*buf;		/* buf passed to strategy() */
154 	ldc_mem_handle_t	desc_mhdl;	/* Mem handle of buf */
155 	vd_dring_entry_t	*dep;		/* public Dring Entry Pointer */
156 } vdc_local_desc_t;
157 
158 /*
159  * vdc soft state structure
160  */
161 typedef struct vdc {
162 	kmutex_t	attach_lock;	/* used by CV which waits in attach */
163 	kcondvar_t	attach_cv;	/* signal when attach can finish */
164 
165 	kmutex_t	lock;		/* protects next 2 sections of vars */
166 	kcondvar_t	cv;		/* signal when upper layers can send */
167 
168 	dev_info_t	*dip;		/* device info pointer */
169 	int		instance;	/* driver instance number */
170 	int		initialized;	/* keeps track of what's init'ed */
171 	int		hshake_cnt;	/* number of failed handshakes */
172 	int		open;		/* count of outstanding opens */
173 	int		dkio_flush_pending;	/* # outstanding DKIO flushes */
174 
175 	vio_ver_t	ver;		/* version number agreed with server */
176 	uint64_t	session_id;	/* common ID sent with all messages */
177 	uint64_t	seq_num;	/* most recent sequence num generated */
178 	uint64_t	seq_num_reply;	/* Last seq num ACK/NACK'ed by vds */
179 	uint64_t	req_id;		/* Most recent Request ID generated */
180 	vd_state_t	state;		/* Current handshake state */
181 	vd_disk_type_t	vdisk_type;	/* type of device/disk being imported */
182 	uint64_t	vdisk_size;	/* device size in bytes */
183 	uint64_t	max_xfer_sz;	/* maximum block size of a descriptor */
184 	uint64_t	block_size;	/* device block size used */
185 	struct dk_label	*label;		/* structure to store disk label */
186 	struct dk_cinfo	*cinfo;		/* structure to store DKIOCINFO data */
187 	struct dk_minfo	*minfo;		/* structure for DKIOCGMEDIAINFO data */
188 	struct vtoc	*vtoc;		/* structure to store VTOC data */
189 
190 	/*
191 	 * The mutex 'msg_proc_lock' protects the following group of fields.
192 	 *
193 	 * The callback function checks to see if LDC triggered it due to
194 	 * there being data available and the callback will signal to
195 	 * the message processing thread waiting on 'msg_proc_cv'.
196 	 */
197 	kmutex_t		msg_proc_lock;
198 	kcondvar_t		msg_proc_cv;
199 	boolean_t		msg_pending;
200 	vdc_thr_state_t		msg_proc_thr_state;
201 	kthread_t		*msg_proc_thr_id;
202 
203 	/*
204 	 * The mutex 'dring_lock'  protects the following group of fields.
205 	 */
206 	kmutex_t		dring_lock;
207 	ldc_mem_info_t		dring_mem_info;
208 	uint_t			dring_curr_idx;
209 	uint32_t		dring_len;
210 	uint32_t		dring_cookie_count;
211 	uint32_t		dring_entry_size;
212 	ldc_mem_cookie_t	*dring_cookie;
213 	uint64_t		dring_ident;
214 
215 	vdc_local_desc_t	*local_dring;
216 
217 	uint64_t		ldc_id;
218 	ldc_status_t		ldc_state;
219 	ldc_handle_t		ldc_handle;
220 	ldc_dring_handle_t	ldc_dring_hdl;
221 } vdc_t;
222 
223 /*
224  * Debugging macros
225  */
226 #ifdef DEBUG
227 extern int	vdc_msglevel;
228 
229 #define	PR0 if (vdc_msglevel > 0)	\
230 		vdc_msg
231 
232 #define	PR1 if (vdc_msglevel > 1)	\
233 		vdc_msg
234 
235 #define	PR2 if (vdc_msglevel > 2)	\
236 		vdc_msg
237 
238 #define	VDC_DUMP_DRING_MSG(dmsgp)					\
239 		vdc_msg("sq:%d start:%d end:%d ident:%x\n",		\
240 			dmsgp->seq_num, dmsgp->start_idx,		\
241 			dmsgp->end_idx, dmsgp->dring_ident);
242 
243 #else	/* !DEBUG */
244 #define	PR0(...)
245 #define	PR1(...)
246 #define	PR2(...)
247 
248 #define	VDC_DUMP_DRING_MSG(dmsgp)
249 
250 #endif	/* !DEBUG */
251 
252 #ifdef	__cplusplus
253 }
254 #endif
255 
256 #endif	/* _VDC_H */
257