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