xref: /onnv-gate/usr/src/uts/common/sys/ramdisk.h (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #ifndef	_SYS_RAMDISK_H
28*0Sstevel@tonic-gate #define	_SYS_RAMDISK_H
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
31*0Sstevel@tonic-gate 
32*0Sstevel@tonic-gate #include <sys/types.h>
33*0Sstevel@tonic-gate #include <sys/time.h>
34*0Sstevel@tonic-gate #include <sys/vtoc.h>
35*0Sstevel@tonic-gate #include <sys/dkio.h>
36*0Sstevel@tonic-gate #include <sys/vnode.h>
37*0Sstevel@tonic-gate 
38*0Sstevel@tonic-gate #ifdef	__cplusplus
39*0Sstevel@tonic-gate extern "C" {
40*0Sstevel@tonic-gate #endif
41*0Sstevel@tonic-gate 
42*0Sstevel@tonic-gate /*
43*0Sstevel@tonic-gate  * /dev names:
44*0Sstevel@tonic-gate  *	/dev/ramdiskctl		- control device
45*0Sstevel@tonic-gate  *	/dev/ramdisk/<name>	- block devices
46*0Sstevel@tonic-gate  *	/dev/rramdisk/<name>	- character devices
47*0Sstevel@tonic-gate  */
48*0Sstevel@tonic-gate #define	RD_DRIVER_NAME		"ramdisk"
49*0Sstevel@tonic-gate #define	RD_BLOCK_NAME		RD_DRIVER_NAME
50*0Sstevel@tonic-gate #define	RD_CHAR_NAME		"r" RD_DRIVER_NAME
51*0Sstevel@tonic-gate 
52*0Sstevel@tonic-gate #define	RD_CTL_NODE		"ctl"
53*0Sstevel@tonic-gate #define	RD_CTL_NAME		RD_DRIVER_NAME RD_CTL_NODE
54*0Sstevel@tonic-gate 
55*0Sstevel@tonic-gate /*
56*0Sstevel@tonic-gate  * Minor number 0 is reserved for the controlling device.  All other ramdisks
57*0Sstevel@tonic-gate  * are assigned minor numbers 1..rd_max_disks.  The minor number is used as
58*0Sstevel@tonic-gate  * an index into the 'rd_devstate' structures.
59*0Sstevel@tonic-gate  */
60*0Sstevel@tonic-gate #define	RD_CTL_MINOR		0
61*0Sstevel@tonic-gate 
62*0Sstevel@tonic-gate /*
63*0Sstevel@tonic-gate  * Maximum number of ramdisks supported by this driver.
64*0Sstevel@tonic-gate  */
65*0Sstevel@tonic-gate #define	RD_MAX_DISKS		1024
66*0Sstevel@tonic-gate 
67*0Sstevel@tonic-gate /*
68*0Sstevel@tonic-gate  * Properties exported by the driver.
69*0Sstevel@tonic-gate  */
70*0Sstevel@tonic-gate #define	NBLOCKS_PROP_NAME	"Nblocks"
71*0Sstevel@tonic-gate #define	SIZE_PROP_NAME		"Size"
72*0Sstevel@tonic-gate 
73*0Sstevel@tonic-gate /*
74*0Sstevel@tonic-gate  * Strip any "ramdisk-" prefix from the name of OBP-created ramdisks.
75*0Sstevel@tonic-gate  */
76*0Sstevel@tonic-gate #define	RD_OBP_PFXSTR		"ramdisk-"
77*0Sstevel@tonic-gate #define	RD_OBP_PFXLEN		(sizeof (RD_OBP_PFXSTR) - 1)
78*0Sstevel@tonic-gate 
79*0Sstevel@tonic-gate #define	RD_STRIP_PREFIX(newname, oldname) \
80*0Sstevel@tonic-gate 	{ \
81*0Sstevel@tonic-gate 		char	*onm = oldname; \
82*0Sstevel@tonic-gate 		newname = strncmp(onm, RD_OBP_PFXSTR, RD_OBP_PFXLEN) == 0 ? \
83*0Sstevel@tonic-gate 		    (onm + RD_OBP_PFXLEN) : onm; \
84*0Sstevel@tonic-gate 	}
85*0Sstevel@tonic-gate 
86*0Sstevel@tonic-gate /*
87*0Sstevel@tonic-gate  * Strip any ",raw" suffix from the name of pseudo ramdisk devices.
88*0Sstevel@tonic-gate  */
89*0Sstevel@tonic-gate #define	RD_STRIP_SUFFIX(name) \
90*0Sstevel@tonic-gate 	{ \
91*0Sstevel@tonic-gate 		char	*str = strstr((name), ",raw"); \
92*0Sstevel@tonic-gate 		if (str != NULL) \
93*0Sstevel@tonic-gate 			*str = '\0'; \
94*0Sstevel@tonic-gate 	}
95*0Sstevel@tonic-gate 
96*0Sstevel@tonic-gate /*
97*0Sstevel@tonic-gate  * Interface between the ramdisk(7D) driver and ramdiskadm(1M).  Use is:
98*0Sstevel@tonic-gate  *
99*0Sstevel@tonic-gate  *	fd = open("/dev/ramdiskctl", O_RDWR | O_EXCL);
100*0Sstevel@tonic-gate  *
101*0Sstevel@tonic-gate  * 'ramdiskctl' must be opened exclusively. Access is controlled by permissions
102*0Sstevel@tonic-gate  * on the device, which is 0644 by default.  Write-access is required for the
103*0Sstevel@tonic-gate  * allocation and deletion of ramdisks, but only read-access is required for
104*0Sstevel@tonic-gate  * the remaining ioctls which simply return information.
105*0Sstevel@tonic-gate  *
106*0Sstevel@tonic-gate  * ioctl usage:
107*0Sstevel@tonic-gate  *
108*0Sstevel@tonic-gate  *	struct rd_ioctl ri;
109*0Sstevel@tonic-gate  *
110*0Sstevel@tonic-gate  *	strlcpy(ri.ri_name, "somediskname", sizeof (ri.ri_name));
111*0Sstevel@tonic-gate  *	ri.ri_size = somedisksize;
112*0Sstevel@tonic-gate  *	ioctl(fd, RD_CREATE_DISK, &ri);
113*0Sstevel@tonic-gate  *
114*0Sstevel@tonic-gate  *	strlcpy(ri.ri_name, "somediskname", sizeof (ri.ri_name));
115*0Sstevel@tonic-gate  *	ioctl(fd, RD_DELETE_DISK, &ri);
116*0Sstevel@tonic-gate  *
117*0Sstevel@tonic-gate  * (only ramdisks created using the RD_CREATE_DISK ioctl can be deleted
118*0Sstevel@tonic-gate  *  by the RD_DELETE_DISK ioctl).
119*0Sstevel@tonic-gate  *
120*0Sstevel@tonic-gate  * Note that these ioctls are completely private, and only for the use of
121*0Sstevel@tonic-gate  * ramdiskadm(1M).
122*0Sstevel@tonic-gate  */
123*0Sstevel@tonic-gate #define	RD_IOC_BASE		(('R' << 16) | ('D' << 8))
124*0Sstevel@tonic-gate 
125*0Sstevel@tonic-gate #define	RD_CREATE_DISK		(RD_IOC_BASE | 0x01)
126*0Sstevel@tonic-gate #define	RD_DELETE_DISK		(RD_IOC_BASE | 0x02)
127*0Sstevel@tonic-gate 
128*0Sstevel@tonic-gate #define	RD_NAME_LEN		32	/* Max length of ramdisk name */
129*0Sstevel@tonic-gate #define	RD_NAME_PAD		7	/* Pad ri_name to 8-bytes */
130*0Sstevel@tonic-gate 
131*0Sstevel@tonic-gate struct rd_ioctl {
132*0Sstevel@tonic-gate 	char		ri_name[RD_NAME_LEN + 1];
133*0Sstevel@tonic-gate 	char		_ri_pad[RD_NAME_PAD];
134*0Sstevel@tonic-gate 	uint64_t	ri_size;
135*0Sstevel@tonic-gate };
136*0Sstevel@tonic-gate 
137*0Sstevel@tonic-gate #if defined(_KERNEL)
138*0Sstevel@tonic-gate 
139*0Sstevel@tonic-gate /*
140*0Sstevel@tonic-gate  * We limit the maximum number of active ramdisk devices to 32, tuneable
141*0Sstevel@tonic-gate  * up to a maximum of 1023.  Minor 0 is always reserved for the controlling
142*0Sstevel@tonic-gate  * device.  You can change this by setting a value for 'max_disks' in
143*0Sstevel@tonic-gate  * ramdisk.conf.
144*0Sstevel@tonic-gate  */
145*0Sstevel@tonic-gate #define	RD_DFLT_DISKS	32
146*0Sstevel@tonic-gate 
147*0Sstevel@tonic-gate /*
148*0Sstevel@tonic-gate  * The maximum amount of memory that can be consumed before starving the
149*0Sstevel@tonic-gate  * kernel depends loosely on the number of cpus, the speed of those cpus,
150*0Sstevel@tonic-gate  * and other hardware characteristics, and is thus highly machine-dependent.
151*0Sstevel@tonic-gate  * The default value of 'rd_percent_physmem' is 25% of physical memory,
152*0Sstevel@tonic-gate  * but this can be changed by setting a value for 'percent_physmem' in
153*0Sstevel@tonic-gate  * ramdisk.conf.
154*0Sstevel@tonic-gate  */
155*0Sstevel@tonic-gate #define	RD_DEFAULT_PERCENT_PHYSMEM	25
156*0Sstevel@tonic-gate 
157*0Sstevel@tonic-gate /*
158*0Sstevel@tonic-gate  * Maximum size of a physical transfer?
159*0Sstevel@tonic-gate  */
160*0Sstevel@tonic-gate #define	RD_DEFAULT_MAXPHYS	(63 * 1024)	/* '126b' */
161*0Sstevel@tonic-gate 
162*0Sstevel@tonic-gate /*
163*0Sstevel@tonic-gate  * A real OBP-created ramdisk consists of one or more physical address
164*0Sstevel@tonic-gate  * ranges; these are described by the 'existing' property, whose value
165*0Sstevel@tonic-gate  * is a (corresponding) number of {phys,size} pairs.
166*0Sstevel@tonic-gate  */
167*0Sstevel@tonic-gate #define	RD_EXISTING_PROP_NAME	"existing"
168*0Sstevel@tonic-gate 
169*0Sstevel@tonic-gate typedef struct {
170*0Sstevel@tonic-gate 	uint64_t	phys;			/* Phys addr of range */
171*0Sstevel@tonic-gate 	uint64_t	size;			/* Size of range */
172*0Sstevel@tonic-gate } rd_existing_t;
173*0Sstevel@tonic-gate 
174*0Sstevel@tonic-gate 
175*0Sstevel@tonic-gate #define	RD_WINDOW_NOT_MAPPED	1	/* Valid window is on page boundary */
176*0Sstevel@tonic-gate 
177*0Sstevel@tonic-gate /*
178*0Sstevel@tonic-gate  * The entire state of each ramdisk device.  The rd_dip field will reference
179*0Sstevel@tonic-gate  * the actual devinfo for real OBP-created ramdisks, or the generic devinfo
180*0Sstevel@tonic-gate  * 'rd_dip' for pseudo ramdisk devices.
181*0Sstevel@tonic-gate  */
182*0Sstevel@tonic-gate typedef struct rd_devstate {
183*0Sstevel@tonic-gate 	kmutex_t	rd_device_lock;		/* Per device lock */
184*0Sstevel@tonic-gate 	char		rd_name[RD_NAME_LEN + 1];
185*0Sstevel@tonic-gate 	dev_info_t	*rd_dip;		/* My devinfo handle */
186*0Sstevel@tonic-gate 	minor_t		rd_minor;		/* Full minor number */
187*0Sstevel@tonic-gate 	size_t		rd_size;		/* Size in bytes */
188*0Sstevel@tonic-gate 	/*
189*0Sstevel@tonic-gate 	 * {rd_nexisting, rd_existing} and {rd_npages, rd_ppa} are
190*0Sstevel@tonic-gate 	 * mutually exclusive; the former describe an OBP-created
191*0Sstevel@tonic-gate 	 * ramdisk, the latter a 'pseudo' ramdisk.
192*0Sstevel@tonic-gate 	 */
193*0Sstevel@tonic-gate 	uint_t		rd_nexisting;		/* # 'existing' structs */
194*0Sstevel@tonic-gate 	rd_existing_t	*rd_existing;
195*0Sstevel@tonic-gate 	pgcnt_t		rd_npages;		/* # physical pages */
196*0Sstevel@tonic-gate 	page_t		**rd_ppa;
197*0Sstevel@tonic-gate 	/*
198*0Sstevel@tonic-gate 	 * Fields describing a virtual window onto the physical ramdisk,
199*0Sstevel@tonic-gate 	 * giving the offset within the ramdisk of the window, its size,
200*0Sstevel@tonic-gate 	 * and its virtual address (in the kernel heap).
201*0Sstevel@tonic-gate 	 */
202*0Sstevel@tonic-gate 	offset_t	rd_window_base;
203*0Sstevel@tonic-gate 	uint64_t	rd_window_size;
204*0Sstevel@tonic-gate 	caddr_t		rd_window_virt;
205*0Sstevel@tonic-gate 	/*
206*0Sstevel@tonic-gate 	 * Fields to count opens/closes of the ramdisk.
207*0Sstevel@tonic-gate 	 */
208*0Sstevel@tonic-gate 	uint32_t	rd_blk_open;
209*0Sstevel@tonic-gate 	uint32_t	rd_chr_open;
210*0Sstevel@tonic-gate 	uint32_t	rd_lyr_open_cnt;
211*0Sstevel@tonic-gate 	/*
212*0Sstevel@tonic-gate 	 * Fields to maintain a faked geometry of the disk.
213*0Sstevel@tonic-gate 	 */
214*0Sstevel@tonic-gate 	struct dk_geom	rd_dkg;
215*0Sstevel@tonic-gate 	struct vtoc	rd_vtoc;
216*0Sstevel@tonic-gate 	struct dk_cinfo	rd_ci;
217*0Sstevel@tonic-gate 	/*
218*0Sstevel@tonic-gate 	 * Kstat stuff.
219*0Sstevel@tonic-gate 	 */
220*0Sstevel@tonic-gate 	kmutex_t	rd_kstat_lock;
221*0Sstevel@tonic-gate 	kstat_t		*rd_kstat;
222*0Sstevel@tonic-gate } rd_devstate_t;
223*0Sstevel@tonic-gate 
224*0Sstevel@tonic-gate extern int	is_pseudo_device(dev_info_t *);
225*0Sstevel@tonic-gate 
226*0Sstevel@tonic-gate #endif
227*0Sstevel@tonic-gate 
228*0Sstevel@tonic-gate #ifdef	__cplusplus
229*0Sstevel@tonic-gate }
230*0Sstevel@tonic-gate #endif
231*0Sstevel@tonic-gate 
232*0Sstevel@tonic-gate #endif	/* _SYS_RAMDISK_H */
233