xref: /illumos-gate/usr/src/uts/common/sys/ramdisk.h (revision bbf21555)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5986fd29aSsetje  * Common Development and Distribution License (the "License").
6986fd29aSsetje  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22986fd29aSsetje  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #ifndef	_SYS_RAMDISK_H
277c478bd9Sstevel@tonic-gate #define	_SYS_RAMDISK_H
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <sys/types.h>
307c478bd9Sstevel@tonic-gate #include <sys/time.h>
317c478bd9Sstevel@tonic-gate #include <sys/vtoc.h>
327c478bd9Sstevel@tonic-gate #include <sys/dkio.h>
337c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
367c478bd9Sstevel@tonic-gate extern "C" {
377c478bd9Sstevel@tonic-gate #endif
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate /*
407c478bd9Sstevel@tonic-gate  * /dev names:
417c478bd9Sstevel@tonic-gate  *	/dev/ramdiskctl		- control device
427c478bd9Sstevel@tonic-gate  *	/dev/ramdisk/<name>	- block devices
437c478bd9Sstevel@tonic-gate  *	/dev/rramdisk/<name>	- character devices
447c478bd9Sstevel@tonic-gate  */
457c478bd9Sstevel@tonic-gate #define	RD_DRIVER_NAME		"ramdisk"
467c478bd9Sstevel@tonic-gate #define	RD_BLOCK_NAME		RD_DRIVER_NAME
477c478bd9Sstevel@tonic-gate #define	RD_CHAR_NAME		"r" RD_DRIVER_NAME
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate #define	RD_CTL_NODE		"ctl"
507c478bd9Sstevel@tonic-gate #define	RD_CTL_NAME		RD_DRIVER_NAME RD_CTL_NODE
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate /*
537c478bd9Sstevel@tonic-gate  * Minor number 0 is reserved for the controlling device.  All other ramdisks
547c478bd9Sstevel@tonic-gate  * are assigned minor numbers 1..rd_max_disks.  The minor number is used as
557c478bd9Sstevel@tonic-gate  * an index into the 'rd_devstate' structures.
567c478bd9Sstevel@tonic-gate  */
577c478bd9Sstevel@tonic-gate #define	RD_CTL_MINOR		0
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate /*
607c478bd9Sstevel@tonic-gate  * Maximum number of ramdisks supported by this driver.
617c478bd9Sstevel@tonic-gate  */
627c478bd9Sstevel@tonic-gate #define	RD_MAX_DISKS		1024
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate /*
657c478bd9Sstevel@tonic-gate  * Properties exported by the driver.
667c478bd9Sstevel@tonic-gate  */
677c478bd9Sstevel@tonic-gate #define	NBLOCKS_PROP_NAME	"Nblocks"
687c478bd9Sstevel@tonic-gate #define	SIZE_PROP_NAME		"Size"
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate /*
717c478bd9Sstevel@tonic-gate  * Strip any "ramdisk-" prefix from the name of OBP-created ramdisks.
727c478bd9Sstevel@tonic-gate  */
737c478bd9Sstevel@tonic-gate #define	RD_OBP_PFXSTR		"ramdisk-"
747c478bd9Sstevel@tonic-gate #define	RD_OBP_PFXLEN		(sizeof (RD_OBP_PFXSTR) - 1)
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate #define	RD_STRIP_PREFIX(newname, oldname) \
777c478bd9Sstevel@tonic-gate 	{ \
787c478bd9Sstevel@tonic-gate 		char	*onm = oldname; \
797c478bd9Sstevel@tonic-gate 		newname = strncmp(onm, RD_OBP_PFXSTR, RD_OBP_PFXLEN) == 0 ? \
807c478bd9Sstevel@tonic-gate 		    (onm + RD_OBP_PFXLEN) : onm; \
817c478bd9Sstevel@tonic-gate 	}
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate /*
847c478bd9Sstevel@tonic-gate  * Strip any ",raw" suffix from the name of pseudo ramdisk devices.
857c478bd9Sstevel@tonic-gate  */
867c478bd9Sstevel@tonic-gate #define	RD_STRIP_SUFFIX(name) \
877c478bd9Sstevel@tonic-gate 	{ \
887c478bd9Sstevel@tonic-gate 		char	*str = strstr((name), ",raw"); \
897c478bd9Sstevel@tonic-gate 		if (str != NULL) \
907c478bd9Sstevel@tonic-gate 			*str = '\0'; \
917c478bd9Sstevel@tonic-gate 	}
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate /*
94*bbf21555SRichard Lowe  * Interface between the ramdisk(4D) driver and ramdiskadm(8).  Use is:
957c478bd9Sstevel@tonic-gate  *
967c478bd9Sstevel@tonic-gate  *	fd = open("/dev/ramdiskctl", O_RDWR | O_EXCL);
977c478bd9Sstevel@tonic-gate  *
987c478bd9Sstevel@tonic-gate  * 'ramdiskctl' must be opened exclusively. Access is controlled by permissions
997c478bd9Sstevel@tonic-gate  * on the device, which is 0644 by default.  Write-access is required for the
1007c478bd9Sstevel@tonic-gate  * allocation and deletion of ramdisks, but only read-access is required for
1017c478bd9Sstevel@tonic-gate  * the remaining ioctls which simply return information.
1027c478bd9Sstevel@tonic-gate  *
1037c478bd9Sstevel@tonic-gate  * ioctl usage:
1047c478bd9Sstevel@tonic-gate  *
1057c478bd9Sstevel@tonic-gate  *	struct rd_ioctl ri;
1067c478bd9Sstevel@tonic-gate  *
1077c478bd9Sstevel@tonic-gate  *	strlcpy(ri.ri_name, "somediskname", sizeof (ri.ri_name));
1087c478bd9Sstevel@tonic-gate  *	ri.ri_size = somedisksize;
1097c478bd9Sstevel@tonic-gate  *	ioctl(fd, RD_CREATE_DISK, &ri);
1107c478bd9Sstevel@tonic-gate  *
1117c478bd9Sstevel@tonic-gate  *	strlcpy(ri.ri_name, "somediskname", sizeof (ri.ri_name));
1127c478bd9Sstevel@tonic-gate  *	ioctl(fd, RD_DELETE_DISK, &ri);
1137c478bd9Sstevel@tonic-gate  *
1147c478bd9Sstevel@tonic-gate  * (only ramdisks created using the RD_CREATE_DISK ioctl can be deleted
1157c478bd9Sstevel@tonic-gate  *  by the RD_DELETE_DISK ioctl).
1167c478bd9Sstevel@tonic-gate  *
1177c478bd9Sstevel@tonic-gate  * Note that these ioctls are completely private, and only for the use of
118*bbf21555SRichard Lowe  * ramdiskadm(8).
1197c478bd9Sstevel@tonic-gate  */
1207c478bd9Sstevel@tonic-gate #define	RD_IOC_BASE		(('R' << 16) | ('D' << 8))
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate #define	RD_CREATE_DISK		(RD_IOC_BASE | 0x01)
1237c478bd9Sstevel@tonic-gate #define	RD_DELETE_DISK		(RD_IOC_BASE | 0x02)
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate #define	RD_NAME_LEN		32	/* Max length of ramdisk name */
1267c478bd9Sstevel@tonic-gate #define	RD_NAME_PAD		7	/* Pad ri_name to 8-bytes */
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate struct rd_ioctl {
1297c478bd9Sstevel@tonic-gate 	char		ri_name[RD_NAME_LEN + 1];
1307c478bd9Sstevel@tonic-gate 	char		_ri_pad[RD_NAME_PAD];
1317c478bd9Sstevel@tonic-gate 	uint64_t	ri_size;
1327c478bd9Sstevel@tonic-gate };
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate #if defined(_KERNEL)
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate /*
1377c478bd9Sstevel@tonic-gate  * We limit the maximum number of active ramdisk devices to 32, tuneable
1387c478bd9Sstevel@tonic-gate  * up to a maximum of 1023.  Minor 0 is always reserved for the controlling
1397c478bd9Sstevel@tonic-gate  * device.  You can change this by setting a value for 'max_disks' in
1407c478bd9Sstevel@tonic-gate  * ramdisk.conf.
1417c478bd9Sstevel@tonic-gate  */
1427c478bd9Sstevel@tonic-gate #define	RD_DFLT_DISKS	32
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate /*
1457c478bd9Sstevel@tonic-gate  * The maximum amount of memory that can be consumed before starving the
1467c478bd9Sstevel@tonic-gate  * kernel depends loosely on the number of cpus, the speed of those cpus,
1477c478bd9Sstevel@tonic-gate  * and other hardware characteristics, and is thus highly machine-dependent.
1487c478bd9Sstevel@tonic-gate  * The default value of 'rd_percent_physmem' is 25% of physical memory,
1497c478bd9Sstevel@tonic-gate  * but this can be changed by setting a value for 'percent_physmem' in
1507c478bd9Sstevel@tonic-gate  * ramdisk.conf.
1517c478bd9Sstevel@tonic-gate  */
1527c478bd9Sstevel@tonic-gate #define	RD_DEFAULT_PERCENT_PHYSMEM	25
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate /*
1557c478bd9Sstevel@tonic-gate  * Maximum size of a physical transfer?
1567c478bd9Sstevel@tonic-gate  */
1577c478bd9Sstevel@tonic-gate #define	RD_DEFAULT_MAXPHYS	(63 * 1024)	/* '126b' */
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate /*
1607c478bd9Sstevel@tonic-gate  * A real OBP-created ramdisk consists of one or more physical address
1617c478bd9Sstevel@tonic-gate  * ranges; these are described by the 'existing' property, whose value
1627c478bd9Sstevel@tonic-gate  * is a (corresponding) number of {phys,size} pairs.
1637c478bd9Sstevel@tonic-gate  */
164986fd29aSsetje #define	OBP_EXISTING_PROP_NAME	"existing"
165986fd29aSsetje #define	OBP_ADDRESS_PROP_NAME	"address"
166986fd29aSsetje #define	OBP_SIZE_PROP_NAME	"size"
167986fd29aSsetje 
168986fd29aSsetje #define	RD_EXISTING_PROP_NAME	"existing"	/* for x86 */
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate typedef struct {
1717c478bd9Sstevel@tonic-gate 	uint64_t	phys;			/* Phys addr of range */
1727c478bd9Sstevel@tonic-gate 	uint64_t	size;			/* Size of range */
1737c478bd9Sstevel@tonic-gate } rd_existing_t;
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate #define	RD_WINDOW_NOT_MAPPED	1	/* Valid window is on page boundary */
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate /*
1797c478bd9Sstevel@tonic-gate  * The entire state of each ramdisk device.  The rd_dip field will reference
1807c478bd9Sstevel@tonic-gate  * the actual devinfo for real OBP-created ramdisks, or the generic devinfo
1817c478bd9Sstevel@tonic-gate  * 'rd_dip' for pseudo ramdisk devices.
1827c478bd9Sstevel@tonic-gate  */
1837c478bd9Sstevel@tonic-gate typedef struct rd_devstate {
1847c478bd9Sstevel@tonic-gate 	kmutex_t	rd_device_lock;		/* Per device lock */
1857c478bd9Sstevel@tonic-gate 	char		rd_name[RD_NAME_LEN + 1];
1867c478bd9Sstevel@tonic-gate 	dev_info_t	*rd_dip;		/* My devinfo handle */
1877c478bd9Sstevel@tonic-gate 	minor_t		rd_minor;		/* Full minor number */
1887c478bd9Sstevel@tonic-gate 	size_t		rd_size;		/* Size in bytes */
1897c478bd9Sstevel@tonic-gate 	/*
1907c478bd9Sstevel@tonic-gate 	 * {rd_nexisting, rd_existing} and {rd_npages, rd_ppa} are
1917c478bd9Sstevel@tonic-gate 	 * mutually exclusive; the former describe an OBP-created
1927c478bd9Sstevel@tonic-gate 	 * ramdisk, the latter a 'pseudo' ramdisk.
1937c478bd9Sstevel@tonic-gate 	 */
1947c478bd9Sstevel@tonic-gate 	uint_t		rd_nexisting;		/* # 'existing' structs */
1957c478bd9Sstevel@tonic-gate 	rd_existing_t	*rd_existing;
1967c478bd9Sstevel@tonic-gate 	pgcnt_t		rd_npages;		/* # physical pages */
1977c478bd9Sstevel@tonic-gate 	page_t		**rd_ppa;
1987c478bd9Sstevel@tonic-gate 	/*
1997c478bd9Sstevel@tonic-gate 	 * Fields describing a virtual window onto the physical ramdisk,
2007c478bd9Sstevel@tonic-gate 	 * giving the offset within the ramdisk of the window, its size,
2017c478bd9Sstevel@tonic-gate 	 * and its virtual address (in the kernel heap).
2027c478bd9Sstevel@tonic-gate 	 */
203986fd29aSsetje 	uint_t		rd_window_obp;		/* using OBP's vaddr */
2047c478bd9Sstevel@tonic-gate 	offset_t	rd_window_base;
2057c478bd9Sstevel@tonic-gate 	uint64_t	rd_window_size;
2067c478bd9Sstevel@tonic-gate 	caddr_t		rd_window_virt;
2077c478bd9Sstevel@tonic-gate 	/*
2087c478bd9Sstevel@tonic-gate 	 * Fields to count opens/closes of the ramdisk.
2097c478bd9Sstevel@tonic-gate 	 */
2107c478bd9Sstevel@tonic-gate 	uint32_t	rd_blk_open;
2117c478bd9Sstevel@tonic-gate 	uint32_t	rd_chr_open;
2127c478bd9Sstevel@tonic-gate 	uint32_t	rd_lyr_open_cnt;
2137c478bd9Sstevel@tonic-gate 	/*
2147c478bd9Sstevel@tonic-gate 	 * Fields to maintain a faked geometry of the disk.
2157c478bd9Sstevel@tonic-gate 	 */
2167c478bd9Sstevel@tonic-gate 	struct dk_geom	rd_dkg;
2177c478bd9Sstevel@tonic-gate 	struct vtoc	rd_vtoc;
2187c478bd9Sstevel@tonic-gate 	struct dk_cinfo	rd_ci;
2197c478bd9Sstevel@tonic-gate 	/*
2207c478bd9Sstevel@tonic-gate 	 * Kstat stuff.
2217c478bd9Sstevel@tonic-gate 	 */
2227c478bd9Sstevel@tonic-gate 	kmutex_t	rd_kstat_lock;
2237c478bd9Sstevel@tonic-gate 	kstat_t		*rd_kstat;
2247c478bd9Sstevel@tonic-gate } rd_devstate_t;
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate extern int	is_pseudo_device(dev_info_t *);
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate #endif
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
2317c478bd9Sstevel@tonic-gate }
2327c478bd9Sstevel@tonic-gate #endif
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate #endif	/* _SYS_RAMDISK_H */
235