xref: /titanic_41/usr/src/uts/sun4v/sys/vdsk_common.h (revision cb6207858a9fcc2feaee22e626912fba281ac969)
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	_VDSK_COMMON_H
28 #define	_VDSK_COMMON_H
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35 
36 /*
37  * This header file contains the private LDoms Virtual Disk (vDisk) definitions
38  * common to both the server (vds) and the client (vdc)
39  */
40 
41 #include <sys/efi_partition.h>
42 #include <sys/machparam.h>
43 #include <sys/vtoc.h>
44 
45 #include <sys/ldc.h>
46 #include <sys/vio_common.h>
47 #include <sys/vio_mailbox.h>
48 
49 /*
50  * vDisk definitions
51  */
52 
53 /*
54  * The number of Descriptor Ring entries
55  *
56  * Constraints:
57  * 	- overall DRing size must be greater than 8K (MMU_PAGESIZE)
58  *	- overall DRing size should be 8K aligned (desirable but not enforced)
59  *	- DRing entry must be 8 byte aligned
60  */
61 #define	VD_DRING_LEN		512
62 
63 /*
64  *
65  */
66 #define	VD_DRING_ENTRY_SZ	(sizeof (vd_dring_entry_t) + 		\
67 		(sizeof (ldc_mem_cookie_t) * (VD_MAX_COOKIES - 1)))
68 
69 /*
70  * The maximum block size we can transmit using one Descriptor Ring entry
71  *
72  * Currently no FS uses more than 128K and it doesn't look like they
73  * will either as there is no perf gain to be had by larger values.
74  * ( see ZFS comment at definition of SPA_MAXBLOCKSIZE ).
75  *
76  * We choose 256K to give us some headroom.
77  */
78 #define	VD_MAX_BLOCK_SIZE	(256 * 1024)
79 
80 #define	VD_MAX_COOKIES		((VD_MAX_BLOCK_SIZE / PAGESIZE) + 1)
81 #define	VD_USEC_TIMEOUT		20000
82 #define	VD_LDC_IDS_PROP		"ldc-ids"
83 #define	VD_LDC_MTU		256
84 
85 /*
86  * Flags used by ioctl routines to indicate if a copyin/copyout is needed
87  */
88 #define	VD_COPYOUT		0x1
89 #define	VD_COPYIN		0x2
90 
91 /*
92  * vDisk operations on physical devices
93  */
94 #define	VD_OP_BREAD		0x01	/* Block Read */
95 #define	VD_OP_BWRITE		0x02	/* Block Write */
96 #define	VD_OP_FLUSH		0x03	/* Flush disk write cache contents */
97 #define	VD_OP_GET_WCE		0x04	/* Get disk W$ status */
98 #define	VD_OP_SET_WCE		0x05	/* Enable/Disable disk W$ */
99 #define	VD_OP_GET_VTOC		0x06	/* Get VTOC */
100 #define	VD_OP_SET_VTOC		0x07	/* Set VTOC */
101 #define	VD_OP_GET_DISKGEOM	0x08	/* Get disk geometry */
102 #define	VD_OP_SET_DISKGEOM	0x09	/* Set disk geometry */
103 #define	VD_OP_SCSICMD		0x0a	/* SCSI control command */
104 #define	VD_OP_GET_DEVID		0x0b	/* Get device id */
105 #define	VD_OP_GET_EFI 		0x0c	/* Get EFI */
106 #define	VD_OP_SET_EFI 		0x0d	/* Set EFI */
107 #define	VD_OP_MASK		0xFF	/* mask of all possible operations */
108 #define	VD_OP_COUNT		13	/* Number of operations */
109 
110 /*
111  * EFI disks do not have a slice 7. Actually that slice is used to represent
112  * the whole disk.
113  */
114 #define	VD_EFI_WD_SLICE	7
115 
116 /*
117  * Definitions of the various ways vds can export disk support to vdc.
118  */
119 typedef enum vd_disk_type {
120 	VD_DISK_TYPE_UNK = 0,		/* Unknown device type */
121 	VD_DISK_TYPE_SLICE,		/* slice in block device */
122 	VD_DISK_TYPE_DISK		/* entire disk (slice 2) */
123 } vd_disk_type_t;
124 
125 /*
126  * Definitions of the various disk label that vDisk supports.
127  */
128 typedef enum vd_disk_label {
129 	VD_DISK_LABEL_UNK = 0,		/* Unknown disk label */
130 	VD_DISK_LABEL_VTOC,		/* VTOC disk label */
131 	VD_DISK_LABEL_EFI		/* EFI disk label */
132 } vd_disk_label_t;
133 
134 /*
135  * vDisk Descriptor payload
136  */
137 typedef struct vd_dring_payload {
138 	uint64_t	req_id;		/* The request ID being processed */
139 	uint8_t		operation;	/* operation for server to perform */
140 	uint8_t		slice;		/* The disk slice being accessed */
141 	uint16_t	resv1;		/* padding */
142 	uint32_t	status;		/* "errno" of server operation */
143 	uint64_t	addr;		/* LP64	diskaddr_t (block I/O) */
144 	uint64_t	nbytes;		/* LP64 size_t */
145 	uint32_t	ncookies;	/* Number of cookies used */
146 	uint32_t	resv2;		/* padding */
147 
148 	ldc_mem_cookie_t	cookie[1];	/* variable sized array */
149 } vd_dring_payload_t;
150 
151 
152 /*
153  * vDisk Descriptor entry
154  */
155 typedef struct vd_dring_entry {
156 	vio_dring_entry_hdr_t		hdr;		/* common header */
157 	vd_dring_payload_t		payload;	/* disk specific data */
158 } vd_dring_entry_t;
159 
160 
161 /*
162  * vDisk control operation structures
163  */
164 
165 /*
166  * vDisk geometry definition (VD_OP_GET_DISKGEOM and VD_OP_SET_DISKGEOM)
167  */
168 typedef struct vd_geom {
169 	uint16_t	ncyl;		/* number of data cylinders */
170 	uint16_t	acyl;		/* number of alternate cylinders */
171 	uint16_t	bcyl;		/* cyl offset for fixed head area */
172 	uint16_t	nhead;		/* number of heads */
173 	uint16_t	nsect;		/* number of data sectors per track */
174 	uint16_t	intrlv;		/* interleave factor */
175 	uint16_t	apc;		/* alternates per cyl (SCSI only) */
176 	uint16_t	rpm;		/* revolutions per minute */
177 	uint16_t	pcyl;		/* number of physical cylinders */
178 	uint16_t	write_reinstruct;	/* # sectors to skip, writes */
179 	uint16_t	read_reinstruct;	/* # sectors to skip, reads */
180 } vd_geom_t;
181 
182 
183 /*
184  * vDisk partition definition
185  */
186 typedef struct vd_partition {
187 	uint16_t	id_tag;		/* ID tag of partition */
188 	uint16_t	perm;		/* permission flags for partition */
189 	uint32_t	reserved;	/* padding */
190 	uint64_t	start;		/* block number of partition start */
191 	uint64_t	nblocks;	/* number of blocks in partition */
192 } vd_partition_t;
193 
194 /*
195  * vDisk VTOC definition (VD_OP_GET_VTOC and VD_OP_SET_VTOC)
196  */
197 #define	VD_VOLNAME_LEN		8	/* length of volume_name field */
198 #define	VD_ASCIILABEL_LEN	128	/* length of ascii_label field */
199 typedef struct vd_vtoc {
200 	char		volume_name[VD_VOLNAME_LEN];	/* volume name */
201 	uint16_t	sector_size;		/* sector size in bytes */
202 	uint16_t	num_partitions;		/* number of partitions */
203 	char		ascii_label[VD_ASCIILABEL_LEN];	/* ASCII label */
204 	vd_partition_t	partition[V_NUMPAR];	/* partition headers */
205 } vd_vtoc_t;
206 
207 
208 /*
209  * vDisk EFI definition (VD_OP_GET_EFI and VD_OP_SET_EFI)
210  */
211 typedef struct vd_efi {
212 	uint64_t	lba;		/* lba of the request */
213 	uint64_t	length;		/* length of data */
214 	char		data[1];	/* data of the request */
215 } vd_efi_t;
216 
217 
218 /*
219  * vDisk DEVID definition (VD_OP_GET_DEVID)
220  */
221 #define	VD_DEVID_SIZE(l)	(sizeof (vd_devid_t) - 1 + l)
222 #define	VD_DEVID_DEFAULT_LEN	128
223 
224 typedef struct vd_devid {
225 	uint16_t	reserved;	/* padding */
226 	uint16_t	type;		/* type of device id */
227 	uint32_t	length;		/* length the device id */
228 	char		id[1];		/* device id */
229 } vd_devid_t;
230 
231 /*
232  * Copy the contents of a vd_geom_t to the contents of a dk_geom struct
233  */
234 #define	VD_GEOM2DK_GEOM(vd_geom, dk_geom)				\
235 {									\
236 	bzero((dk_geom), sizeof (*(dk_geom)));				\
237 	(dk_geom)->dkg_ncyl		= (vd_geom)->ncyl;		\
238 	(dk_geom)->dkg_acyl		= (vd_geom)->acyl;		\
239 	(dk_geom)->dkg_bcyl		= (vd_geom)->bcyl;		\
240 	(dk_geom)->dkg_nhead		= (vd_geom)->nhead;		\
241 	(dk_geom)->dkg_nsect		= (vd_geom)->nsect;		\
242 	(dk_geom)->dkg_intrlv		= (vd_geom)->intrlv;		\
243 	(dk_geom)->dkg_apc		= (vd_geom)->apc;		\
244 	(dk_geom)->dkg_rpm		= (vd_geom)->rpm;		\
245 	(dk_geom)->dkg_pcyl		= (vd_geom)->pcyl;		\
246 	(dk_geom)->dkg_write_reinstruct	= (vd_geom)->write_reinstruct;	\
247 	(dk_geom)->dkg_read_reinstruct	= (vd_geom)->read_reinstruct;	\
248 }
249 
250 /*
251  * Copy the contents of a vd_vtoc_t to the contents of a vtoc struct
252  */
253 #define	VD_VTOC2VTOC(vd_vtoc, vtoc)					\
254 {									\
255 	bzero((vtoc), sizeof (*(vtoc)));				\
256 	bcopy((vd_vtoc)->volume_name, (vtoc)->v_volume,			\
257 	    MIN(sizeof ((vd_vtoc)->volume_name),			\
258 		sizeof ((vtoc)->v_volume)));				\
259 	bcopy((vd_vtoc)->ascii_label, (vtoc)->v_asciilabel,		\
260 	    MIN(sizeof ((vd_vtoc)->ascii_label),			\
261 		sizeof ((vtoc)->v_asciilabel)));			\
262 	(vtoc)->v_sanity	= VTOC_SANE;				\
263 	(vtoc)->v_version	= V_VERSION;				\
264 	(vtoc)->v_sectorsz	= (vd_vtoc)->sector_size;		\
265 	(vtoc)->v_nparts	= (vd_vtoc)->num_partitions;		\
266 	for (int i = 0; i < (vd_vtoc)->num_partitions; i++) {		\
267 		(vtoc)->v_part[i].p_tag	= (vd_vtoc)->partition[i].id_tag; \
268 		(vtoc)->v_part[i].p_flag = (vd_vtoc)->partition[i].perm; \
269 		(vtoc)->v_part[i].p_start = (vd_vtoc)->partition[i].start; \
270 		(vtoc)->v_part[i].p_size = (vd_vtoc)->partition[i].nblocks; \
271 	}								\
272 }
273 
274 /*
275  * Copy the contents of a dk_geom struct to the contents of a vd_geom_t
276  */
277 #define	DK_GEOM2VD_GEOM(dk_geom, vd_geom)				\
278 {									\
279 	bzero((vd_geom), sizeof (*(vd_geom)));				\
280 	(vd_geom)->ncyl			= (dk_geom)->dkg_ncyl;		\
281 	(vd_geom)->acyl			= (dk_geom)->dkg_acyl;		\
282 	(vd_geom)->bcyl			= (dk_geom)->dkg_bcyl;		\
283 	(vd_geom)->nhead		= (dk_geom)->dkg_nhead;		\
284 	(vd_geom)->nsect		= (dk_geom)->dkg_nsect;		\
285 	(vd_geom)->intrlv		= (dk_geom)->dkg_intrlv;	\
286 	(vd_geom)->apc			= (dk_geom)->dkg_apc;		\
287 	(vd_geom)->rpm			= (dk_geom)->dkg_rpm;		\
288 	(vd_geom)->pcyl			= (dk_geom)->dkg_pcyl;		\
289 	(vd_geom)->write_reinstruct	= (dk_geom)->dkg_write_reinstruct; \
290 	(vd_geom)->read_reinstruct	= (dk_geom)->dkg_read_reinstruct; \
291 }
292 
293 /*
294  * Copy the contents of a vtoc struct to the contents of a vd_vtoc_t
295  */
296 #define	VTOC2VD_VTOC(vtoc, vd_vtoc)					\
297 {									\
298 	bzero((vd_vtoc), sizeof (*(vd_vtoc)));				\
299 	bcopy((vtoc)->v_volume, (vd_vtoc)->volume_name,			\
300 	    MIN(sizeof ((vtoc)->v_volume),				\
301 		sizeof ((vd_vtoc)->volume_name)));			\
302 	bcopy((vtoc)->v_asciilabel, (vd_vtoc)->ascii_label,		\
303 	    MIN(sizeof ((vtoc)->v_asciilabel),				\
304 		sizeof ((vd_vtoc)->ascii_label)));			\
305 	(vd_vtoc)->sector_size			= (vtoc)->v_sectorsz;	\
306 	(vd_vtoc)->num_partitions		= (vtoc)->v_nparts;	\
307 	for (int i = 0; i < (vtoc)->v_nparts; i++) {			\
308 		(vd_vtoc)->partition[i].id_tag	= (vtoc)->v_part[i].p_tag; \
309 		(vd_vtoc)->partition[i].perm	= (vtoc)->v_part[i].p_flag; \
310 		(vd_vtoc)->partition[i].start	= (vtoc)->v_part[i].p_start; \
311 		(vd_vtoc)->partition[i].nblocks	= (vtoc)->v_part[i].p_size; \
312 	}								\
313 }
314 
315 /*
316  * Copy the contents of a vd_efi_t to the contents of a dk_efi_t.
317  * Note that (dk_efi)->dki_data and (vd_efi)->data should be correctly
318  * initialized prior to using this macro.
319  */
320 #define	VD_EFI2DK_EFI(vd_efi, dk_efi)					\
321 {									\
322 	(dk_efi)->dki_lba	= (vd_efi)->lba;			\
323 	(dk_efi)->dki_length	= (vd_efi)->length;			\
324 	bcopy((vd_efi)->data, (dk_efi)->dki_data, (dk_efi)->dki_length); \
325 }
326 
327 /*
328  * Copy the contents of dk_efi_t to the contents of vd_efi_t.
329  * Note that (dk_efi)->dki_data and (vd_efi)->data should be correctly
330  * initialized prior to using this macro.
331  */
332 #define	DK_EFI2VD_EFI(dk_efi, vd_efi)					\
333 {									\
334 	(vd_efi)->lba		= (dk_efi)->dki_lba;			\
335 	(vd_efi)->length	= (dk_efi)->dki_length;			\
336 	bcopy((dk_efi)->dki_data, (vd_efi)->data, (vd_efi)->length);	\
337 }
338 
339 /*
340  * Hooks for EFI support
341  */
342 
343 /*
344  * The EFI alloc_and_read() function will use some ioctls to get EFI data
345  * but the device reference we will use is different depending if the command
346  * is issued from the vDisk server side (vds) or from the vDisk client side
347  * (vdc). From the server side (vds), we will have a layered device reference
348  * (ldi_handle_t) while on the client side (vdc) we will have a regular device
349  * reference (dev_t).
350  */
351 #ifdef _SUN4V_VDS
352 int vds_efi_alloc_and_read(ldi_handle_t dev, struct dk_gpt **vtoc,
353     size_t *vtoc_len);
354 #else
355 void vdc_efi_init(int (*func)(dev_t, int, caddr_t, int));
356 void vdc_efi_fini(void);
357 int vdc_efi_alloc_and_read(dev_t dev, struct dk_gpt **vtoc,
358     size_t *vtoc_len);
359 #endif
360 
361 void vd_efi_free(struct dk_gpt *ptr, size_t length);
362 void vd_efi_to_vtoc(struct dk_gpt *efi, struct vtoc *vtoc);
363 
364 #ifdef	__cplusplus
365 }
366 #endif
367 
368 #endif	/* _VDSK_COMMON_H */
369