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