xref: /titanic_50/usr/src/uts/common/io/ramdisk.c (revision 63360950109af2ce85a962ca61f40b8782f11100)
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
518c2aff7Sartem  * Common Development and Distribution License (the "License").
618c2aff7Sartem  * 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 /*
2218c2aff7Sartem  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * Ramdisk device driver.
307c478bd9Sstevel@tonic-gate  *
317c478bd9Sstevel@tonic-gate  * There are two types of ramdisk: 'real' OBP-created ramdisks, and 'pseudo'
327c478bd9Sstevel@tonic-gate  * ramdisks created at runtime with no corresponding OBP device node.  The
337c478bd9Sstevel@tonic-gate  * ramdisk(7D) driver is capable of dealing with both, and with the creation
347c478bd9Sstevel@tonic-gate  * and deletion of 'pseudo' ramdisks.
357c478bd9Sstevel@tonic-gate  *
367c478bd9Sstevel@tonic-gate  * Every ramdisk has a single 'state' structure which maintains data for
377c478bd9Sstevel@tonic-gate  * that ramdisk, and is assigned a single minor number.  The bottom 10-bits
387c478bd9Sstevel@tonic-gate  * of the minor number index the state structures; the top 8-bits give a
397c478bd9Sstevel@tonic-gate  * 'real OBP disk' number, i.e. they are zero for 'pseudo' ramdisks.  Thus
407c478bd9Sstevel@tonic-gate  * it is possible to distinguish 'real' from 'pseudo' ramdisks using the
417c478bd9Sstevel@tonic-gate  * top 8-bits of the minor number.
427c478bd9Sstevel@tonic-gate  *
437c478bd9Sstevel@tonic-gate  * Each OBP-created ramdisk has its own node in the device tree with an
447c478bd9Sstevel@tonic-gate  * "existing" property which describes the one-or-more physical address ranges
457c478bd9Sstevel@tonic-gate  * assigned to the ramdisk.  All 'pseudo' ramdisks share a common devinfo
467c478bd9Sstevel@tonic-gate  * structure.
477c478bd9Sstevel@tonic-gate  *
487c478bd9Sstevel@tonic-gate  * A single character device node is used by ramdiskadm(1M) to communicate
497c478bd9Sstevel@tonic-gate  * with the ramdisk driver, with minor number 0:
507c478bd9Sstevel@tonic-gate  *
517c478bd9Sstevel@tonic-gate  *	/dev/ramdiskctl -> /devices/pseudo/ramdisk@0:ctl
527c478bd9Sstevel@tonic-gate  *
537c478bd9Sstevel@tonic-gate  * For consistent access, block and raw device nodes are created for *every*
547c478bd9Sstevel@tonic-gate  * ramdisk.  For 'pseudo' ramdisks:
557c478bd9Sstevel@tonic-gate  *
567c478bd9Sstevel@tonic-gate  *	/dev/ramdisk/<diskname>  -> /devices/pseudo/ramdisk@0:<diskname>
577c478bd9Sstevel@tonic-gate  *	/dev/rramdisk/<diskname> -> /devices/pseudo/ramdisk@0:<diskname>,raw
587c478bd9Sstevel@tonic-gate  *
597c478bd9Sstevel@tonic-gate  * For OBP-created ramdisks:
607c478bd9Sstevel@tonic-gate  *
617c478bd9Sstevel@tonic-gate  *	/dev/ramdisk/<diskname>  -> /devices/ramdisk-<diskname>:a
627c478bd9Sstevel@tonic-gate  *	/dev/ramdisk/<diskname>  -> /devices/ramdisk-<diskname>:a,raw
637c478bd9Sstevel@tonic-gate  *
647c478bd9Sstevel@tonic-gate  * This allows the transition from the standalone to the kernel to proceed
657c478bd9Sstevel@tonic-gate  * when booting from a ramdisk, and for the installation to correctly identify
667c478bd9Sstevel@tonic-gate  * the root device.
677c478bd9Sstevel@tonic-gate  */
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate #include <sys/types.h>
707c478bd9Sstevel@tonic-gate #include <sys/param.h>
717c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
727c478bd9Sstevel@tonic-gate #include <sys/errno.h>
737c478bd9Sstevel@tonic-gate #include <sys/uio.h>
747c478bd9Sstevel@tonic-gate #include <sys/buf.h>
757c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
767c478bd9Sstevel@tonic-gate #include <sys/open.h>
777c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
787c478bd9Sstevel@tonic-gate #include <sys/poll.h>
797c478bd9Sstevel@tonic-gate #include <sys/conf.h>
807c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
817c478bd9Sstevel@tonic-gate #include <sys/stat.h>
827c478bd9Sstevel@tonic-gate #include <sys/file.h>
837c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
847c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
857c478bd9Sstevel@tonic-gate #include <sys/ramdisk.h>
867c478bd9Sstevel@tonic-gate #include <vm/seg_kmem.h>
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate /*
897c478bd9Sstevel@tonic-gate  * An opaque handle where information about our set of ramdisk devices lives.
907c478bd9Sstevel@tonic-gate  */
917c478bd9Sstevel@tonic-gate static void	*rd_statep;
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate /*
947c478bd9Sstevel@tonic-gate  * Pointer to devinfo for the 'pseudo' ramdisks.  Real OBP-created ramdisks
957c478bd9Sstevel@tonic-gate  * get their own individual devinfo.
967c478bd9Sstevel@tonic-gate  */
977c478bd9Sstevel@tonic-gate static dev_info_t *rd_dip = NULL;
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate /*
1007c478bd9Sstevel@tonic-gate  * Global state lock.
1017c478bd9Sstevel@tonic-gate  */
1027c478bd9Sstevel@tonic-gate static kmutex_t	rd_lock;
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate /*
1057c478bd9Sstevel@tonic-gate  * Maximum number of ramdisks supported by this driver.
1067c478bd9Sstevel@tonic-gate  */
1077c478bd9Sstevel@tonic-gate static uint32_t	rd_max_disks = RD_DFLT_DISKS;
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate /*
1107c478bd9Sstevel@tonic-gate  * Percentage of physical memory which can be assigned to pseudo ramdisks,
1117c478bd9Sstevel@tonic-gate  * what that equates to in pages, and how many pages are currently assigned.
1127c478bd9Sstevel@tonic-gate  */
1137c478bd9Sstevel@tonic-gate static uint_t	rd_percent_physmem = RD_DEFAULT_PERCENT_PHYSMEM;
1147c478bd9Sstevel@tonic-gate static pgcnt_t	rd_max_physmem;
1157c478bd9Sstevel@tonic-gate static pgcnt_t	rd_tot_physmem;
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate static uint_t	rd_maxphys = RD_DEFAULT_MAXPHYS;
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate /*
1207c478bd9Sstevel@tonic-gate  * Is the driver busy, i.e. are there any pseudo ramdisk devices in existence?
1217c478bd9Sstevel@tonic-gate  */
1227c478bd9Sstevel@tonic-gate static int
1237c478bd9Sstevel@tonic-gate rd_is_busy(void)
1247c478bd9Sstevel@tonic-gate {
1257c478bd9Sstevel@tonic-gate 	minor_t	minor;
1267c478bd9Sstevel@tonic-gate 	rd_devstate_t	*rsp;
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&rd_lock));
1297c478bd9Sstevel@tonic-gate 	for (minor = 1; minor <= rd_max_disks; ++minor) {
1307c478bd9Sstevel@tonic-gate 		if ((rsp = ddi_get_soft_state(rd_statep, minor)) != NULL &&
1317c478bd9Sstevel@tonic-gate 		    rsp->rd_dip == rd_dip) {
1327c478bd9Sstevel@tonic-gate 			return (EBUSY);
1337c478bd9Sstevel@tonic-gate 		}
1347c478bd9Sstevel@tonic-gate 	}
1357c478bd9Sstevel@tonic-gate 	return (0);
1367c478bd9Sstevel@tonic-gate }
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate /*
1397c478bd9Sstevel@tonic-gate  * Find the first free minor number; returns zero if there isn't one.
1407c478bd9Sstevel@tonic-gate  */
1417c478bd9Sstevel@tonic-gate static minor_t
1427c478bd9Sstevel@tonic-gate rd_find_free_minor(void)
1437c478bd9Sstevel@tonic-gate {
1447c478bd9Sstevel@tonic-gate 	minor_t	minor;
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&rd_lock));
1477c478bd9Sstevel@tonic-gate 	for (minor = 1; minor <= rd_max_disks; ++minor) {
1487c478bd9Sstevel@tonic-gate 		if (ddi_get_soft_state(rd_statep, minor) == NULL) {
1497c478bd9Sstevel@tonic-gate 			return (minor);
1507c478bd9Sstevel@tonic-gate 		}
1517c478bd9Sstevel@tonic-gate 	}
1527c478bd9Sstevel@tonic-gate 	return (0);
1537c478bd9Sstevel@tonic-gate }
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate /*
1567c478bd9Sstevel@tonic-gate  * Locate the rd_devstate for the named ramdisk; returns NULL if not found.
1577c478bd9Sstevel@tonic-gate  * Each ramdisk is identified uniquely by name, i.e. an OBP-created ramdisk
1587c478bd9Sstevel@tonic-gate  * cannot have the same name as a pseudo ramdisk.
1597c478bd9Sstevel@tonic-gate  */
1607c478bd9Sstevel@tonic-gate static rd_devstate_t *
1617c478bd9Sstevel@tonic-gate rd_find_named_disk(char *name)
1627c478bd9Sstevel@tonic-gate {
1637c478bd9Sstevel@tonic-gate 	minor_t		minor;
1647c478bd9Sstevel@tonic-gate 	rd_devstate_t	*rsp;
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&rd_lock));
1677c478bd9Sstevel@tonic-gate 	for (minor = 1; minor <= rd_max_disks; ++minor) {
1687c478bd9Sstevel@tonic-gate 		if ((rsp = ddi_get_soft_state(rd_statep, minor)) != NULL &&
1697c478bd9Sstevel@tonic-gate 		    strcmp(rsp->rd_name, name) == 0) {
1707c478bd9Sstevel@tonic-gate 			return (rsp);
1717c478bd9Sstevel@tonic-gate 		}
1727c478bd9Sstevel@tonic-gate 	}
1737c478bd9Sstevel@tonic-gate 	return (NULL);
1747c478bd9Sstevel@tonic-gate }
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate /*
1777c478bd9Sstevel@tonic-gate  * Locate the rd_devstate for the real OBP-created ramdisk whose devinfo
1787c478bd9Sstevel@tonic-gate  * is referenced by 'dip'; returns NULL if not found (shouldn't happen).
1797c478bd9Sstevel@tonic-gate  */
1807c478bd9Sstevel@tonic-gate static rd_devstate_t *
1817c478bd9Sstevel@tonic-gate rd_find_dip_state(dev_info_t *dip)
1827c478bd9Sstevel@tonic-gate {
1837c478bd9Sstevel@tonic-gate 	minor_t		minor;
1847c478bd9Sstevel@tonic-gate 	rd_devstate_t	*rsp;
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&rd_lock));
1877c478bd9Sstevel@tonic-gate 	for (minor = 1; minor <= rd_max_disks; ++minor) {
1887c478bd9Sstevel@tonic-gate 		if ((rsp = ddi_get_soft_state(rd_statep, minor)) != NULL &&
1897c478bd9Sstevel@tonic-gate 		    rsp->rd_dip == dip) {
1907c478bd9Sstevel@tonic-gate 			return (rsp);
1917c478bd9Sstevel@tonic-gate 		}
1927c478bd9Sstevel@tonic-gate 	}
1937c478bd9Sstevel@tonic-gate 	return (NULL);
1947c478bd9Sstevel@tonic-gate }
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate /*
1977c478bd9Sstevel@tonic-gate  * Is the ramdisk open?
1987c478bd9Sstevel@tonic-gate  */
1997c478bd9Sstevel@tonic-gate static int
2007c478bd9Sstevel@tonic-gate rd_is_open(rd_devstate_t *rsp)
2017c478bd9Sstevel@tonic-gate {
2027c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&rd_lock));
2037c478bd9Sstevel@tonic-gate 	return (rsp->rd_chr_open || rsp->rd_blk_open || rsp->rd_lyr_open_cnt);
2047c478bd9Sstevel@tonic-gate }
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate /*
2077c478bd9Sstevel@tonic-gate  * Mark the ramdisk open.
2087c478bd9Sstevel@tonic-gate  */
2097c478bd9Sstevel@tonic-gate static int
2107c478bd9Sstevel@tonic-gate rd_opened(rd_devstate_t *rsp, int otyp)
2117c478bd9Sstevel@tonic-gate {
2127c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&rd_lock));
2137c478bd9Sstevel@tonic-gate 	switch (otyp) {
2147c478bd9Sstevel@tonic-gate 	case OTYP_CHR:
2157c478bd9Sstevel@tonic-gate 		rsp->rd_chr_open = 1;
2167c478bd9Sstevel@tonic-gate 		break;
2177c478bd9Sstevel@tonic-gate 	case OTYP_BLK:
2187c478bd9Sstevel@tonic-gate 		rsp->rd_blk_open = 1;
2197c478bd9Sstevel@tonic-gate 		break;
2207c478bd9Sstevel@tonic-gate 	case OTYP_LYR:
2217c478bd9Sstevel@tonic-gate 		rsp->rd_lyr_open_cnt++;
2227c478bd9Sstevel@tonic-gate 		break;
2237c478bd9Sstevel@tonic-gate 	default:
2247c478bd9Sstevel@tonic-gate 		return (-1);
2257c478bd9Sstevel@tonic-gate 	}
2267c478bd9Sstevel@tonic-gate 	return (0);
2277c478bd9Sstevel@tonic-gate }
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate /*
2307c478bd9Sstevel@tonic-gate  * Mark the ramdisk closed.
2317c478bd9Sstevel@tonic-gate  */
2327c478bd9Sstevel@tonic-gate static void
2337c478bd9Sstevel@tonic-gate rd_closed(rd_devstate_t *rsp, int otyp)
2347c478bd9Sstevel@tonic-gate {
2357c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&rd_lock));
2367c478bd9Sstevel@tonic-gate 	switch (otyp) {
2377c478bd9Sstevel@tonic-gate 	case OTYP_CHR:
2387c478bd9Sstevel@tonic-gate 		rsp->rd_chr_open = 0;
2397c478bd9Sstevel@tonic-gate 		break;
2407c478bd9Sstevel@tonic-gate 	case OTYP_BLK:
2417c478bd9Sstevel@tonic-gate 		rsp->rd_blk_open = 0;
2427c478bd9Sstevel@tonic-gate 		break;
2437c478bd9Sstevel@tonic-gate 	case OTYP_LYR:
2447c478bd9Sstevel@tonic-gate 		rsp->rd_lyr_open_cnt--;
2457c478bd9Sstevel@tonic-gate 		break;
2467c478bd9Sstevel@tonic-gate 	default:
2477c478bd9Sstevel@tonic-gate 		break;
2487c478bd9Sstevel@tonic-gate 	}
2497c478bd9Sstevel@tonic-gate }
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate static void
2527c478bd9Sstevel@tonic-gate rd_init_tuneables(void)
2537c478bd9Sstevel@tonic-gate {
2547c478bd9Sstevel@tonic-gate 	char	*prop, *p;
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 	/*
2577c478bd9Sstevel@tonic-gate 	 * Ensure sanity of 'rd_max_disks', which may be tuned in ramdisk.conf.
2587c478bd9Sstevel@tonic-gate 	 */
2597c478bd9Sstevel@tonic-gate 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, rd_dip, 0,
2607c478bd9Sstevel@tonic-gate 	    "max_disks", &prop) == DDI_PROP_SUCCESS) {
2617c478bd9Sstevel@tonic-gate 		p = prop;
2627c478bd9Sstevel@tonic-gate 		rd_max_disks = (uint32_t)stoi(&p);
2637c478bd9Sstevel@tonic-gate 		ddi_prop_free(prop);
2647c478bd9Sstevel@tonic-gate 	}
2657c478bd9Sstevel@tonic-gate 	if (rd_max_disks >= RD_MAX_DISKS) {
2667c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "ramdisk: rd_max_disks (%u) too big;"
2677c478bd9Sstevel@tonic-gate 		    " using default (%u).", rd_max_disks, RD_MAX_DISKS - 1);
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate 		rd_max_disks = RD_MAX_DISKS - 1;
2707c478bd9Sstevel@tonic-gate 	}
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 	/*
2737c478bd9Sstevel@tonic-gate 	 * Ensure sanity of 'rd_percent_physmem', which may be tuned
2747c478bd9Sstevel@tonic-gate 	 * in ramdisk.conf.
2757c478bd9Sstevel@tonic-gate 	 */
2767c478bd9Sstevel@tonic-gate 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, rd_dip, 0,
2777c478bd9Sstevel@tonic-gate 	    "percent_physmem", &prop) == DDI_PROP_SUCCESS) {
2787c478bd9Sstevel@tonic-gate 		p = prop;
2797c478bd9Sstevel@tonic-gate 		rd_percent_physmem = (uint_t)stoi(&p);
2807c478bd9Sstevel@tonic-gate 		ddi_prop_free(prop);
2817c478bd9Sstevel@tonic-gate 	}
2827c478bd9Sstevel@tonic-gate 	if (rd_percent_physmem >= 100) {
2837c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "ramdisk: rd_percent_physmem (%u) >= 100;"
2847c478bd9Sstevel@tonic-gate 		    " using default (%u%%).", rd_percent_physmem,
2857c478bd9Sstevel@tonic-gate 		    RD_DEFAULT_PERCENT_PHYSMEM);
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 		rd_percent_physmem = RD_DEFAULT_PERCENT_PHYSMEM;
2887c478bd9Sstevel@tonic-gate 	}
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 	/*
2917c478bd9Sstevel@tonic-gate 	 * Since availrmem is in pages (and is a long), this won't overflow.
2927c478bd9Sstevel@tonic-gate 	 */
2937c478bd9Sstevel@tonic-gate 	rd_max_physmem = (availrmem * rd_percent_physmem) / 100;
2947c478bd9Sstevel@tonic-gate }
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate /*
2977c478bd9Sstevel@tonic-gate  * Allocate enough physical pages to hold `size' bytes.  Returns an
2987c478bd9Sstevel@tonic-gate  * array of page_t * pointers that can later be mapped in or out via
2997c478bd9Sstevel@tonic-gate  * rd_{un}map_window() but is otherwise opaque, or NULL on failure.
3007c478bd9Sstevel@tonic-gate  *
3017c478bd9Sstevel@tonic-gate  * This code stolen from the NCA driver.
3027c478bd9Sstevel@tonic-gate  */
3037c478bd9Sstevel@tonic-gate page_t **
3047c478bd9Sstevel@tonic-gate rd_phys_alloc(pgcnt_t npages)
3057c478bd9Sstevel@tonic-gate {
3067c478bd9Sstevel@tonic-gate 	page_t		*pp, **ppa;
3077c478bd9Sstevel@tonic-gate 	pgcnt_t		i;
3087c478bd9Sstevel@tonic-gate 	size_t		ppalen =  npages * sizeof (struct page_t *);
3097c478bd9Sstevel@tonic-gate 	struct seg	kseg;
3107c478bd9Sstevel@tonic-gate 	char		*addr;		/* For the purposes of coloring */
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate 	if (rd_tot_physmem + npages > rd_max_physmem) {
3137c478bd9Sstevel@tonic-gate 		return (NULL);
3147c478bd9Sstevel@tonic-gate 	}
3157c478bd9Sstevel@tonic-gate 	ppa = kmem_zalloc(ppalen, KM_SLEEP);
3167c478bd9Sstevel@tonic-gate 	(void) page_resv(npages, KM_SLEEP);
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 	for (i = 0, addr = NULL; i < npages; ++i, addr += PAGESIZE) {
3197c478bd9Sstevel@tonic-gate 		if (!page_create_wait(1, KM_SLEEP)) {
3207c478bd9Sstevel@tonic-gate 			goto out;
3217c478bd9Sstevel@tonic-gate 		}
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 		kseg.s_as = &kas;
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate 		if ((pp = page_get_freelist(&kvp, 0, &kseg, addr, PAGESIZE,
3267c478bd9Sstevel@tonic-gate 		    KM_SLEEP, NULL)) == NULL) {
3277c478bd9Sstevel@tonic-gate 			if ((pp = page_get_cachelist(&kvp, 0, &kseg, addr,
3287c478bd9Sstevel@tonic-gate 			    KM_SLEEP, NULL)) == NULL) {
3297c478bd9Sstevel@tonic-gate 				goto out;
3307c478bd9Sstevel@tonic-gate 			}
3317c478bd9Sstevel@tonic-gate 			if (PP_ISAGED(pp) == 0) {
3327c478bd9Sstevel@tonic-gate 				page_hashout(pp, NULL);
3337c478bd9Sstevel@tonic-gate 			}
3347c478bd9Sstevel@tonic-gate 		}
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate 		PP_CLRFREE(pp);
3377c478bd9Sstevel@tonic-gate 		PP_CLRAGED(pp);
3387c478bd9Sstevel@tonic-gate 		ppa[i] = pp;
3397c478bd9Sstevel@tonic-gate 		page_downgrade(pp);
3407c478bd9Sstevel@tonic-gate 	}
3417c478bd9Sstevel@tonic-gate 	rd_tot_physmem += npages;
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 	return (ppa);
3447c478bd9Sstevel@tonic-gate out:
3457c478bd9Sstevel@tonic-gate 	for (i = 0; ppa[i] != NULL && i < npages; ++i) {
3467c478bd9Sstevel@tonic-gate 		page_free(ppa[i], 0);
3477c478bd9Sstevel@tonic-gate 	}
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 	page_create_putback(i);
3507c478bd9Sstevel@tonic-gate 	kmem_free(ppa, ppalen);
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate 	page_unresv(npages);
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 	return (NULL);
3557c478bd9Sstevel@tonic-gate }
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate /*
3587c478bd9Sstevel@tonic-gate  * Free physical pages previously allocated via rd_phys_alloc(); note that
3597c478bd9Sstevel@tonic-gate  * this function may block as it has to wait until it can exclusively lock
3607c478bd9Sstevel@tonic-gate  * all the pages first.
3617c478bd9Sstevel@tonic-gate  */
3627c478bd9Sstevel@tonic-gate static void
3637c478bd9Sstevel@tonic-gate rd_phys_free(page_t **ppa, pgcnt_t npages)
3647c478bd9Sstevel@tonic-gate {
3657c478bd9Sstevel@tonic-gate 	pgcnt_t	i;
3667c478bd9Sstevel@tonic-gate 	size_t	ppalen = npages * sizeof (struct page_t *);
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 	for (i = 0; i < npages; ++i) {
3697c478bd9Sstevel@tonic-gate 		if (! page_tryupgrade(ppa[i])) {
3707c478bd9Sstevel@tonic-gate 			page_unlock(ppa[i]);
3717c478bd9Sstevel@tonic-gate 			while (! page_lock(ppa[i], SE_EXCL, NULL, P_RECLAIM))
3727c478bd9Sstevel@tonic-gate 				;
3737c478bd9Sstevel@tonic-gate 		}
3747c478bd9Sstevel@tonic-gate 		page_free(ppa[i], 0);
3757c478bd9Sstevel@tonic-gate 	}
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate 	kmem_free(ppa, ppalen);
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 	page_unresv(npages);
3807c478bd9Sstevel@tonic-gate 	rd_tot_physmem -= npages;
3817c478bd9Sstevel@tonic-gate }
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate /*
3847c478bd9Sstevel@tonic-gate  * Remove a window mapping (if present).
3857c478bd9Sstevel@tonic-gate  */
3867c478bd9Sstevel@tonic-gate static void
3877c478bd9Sstevel@tonic-gate rd_unmap_window(rd_devstate_t *rsp)
3887c478bd9Sstevel@tonic-gate {
3897c478bd9Sstevel@tonic-gate 	if (rsp->rd_window_base != RD_WINDOW_NOT_MAPPED) {
3907c478bd9Sstevel@tonic-gate 		hat_unload(kas.a_hat, rsp->rd_window_virt, rsp->rd_window_size,
3917c478bd9Sstevel@tonic-gate 		    HAT_UNLOAD_UNLOCK);
3927c478bd9Sstevel@tonic-gate 	}
3937c478bd9Sstevel@tonic-gate }
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate /*
3967c478bd9Sstevel@tonic-gate  * Map a portion of the ramdisk into the virtual window.
3977c478bd9Sstevel@tonic-gate  */
3987c478bd9Sstevel@tonic-gate static void
3997c478bd9Sstevel@tonic-gate rd_map_window(rd_devstate_t *rsp, off_t offset)
4007c478bd9Sstevel@tonic-gate {
4017c478bd9Sstevel@tonic-gate 	pgcnt_t	offpgs = btop(offset);
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 	if (rsp->rd_window_base != RD_WINDOW_NOT_MAPPED) {
4047c478bd9Sstevel@tonic-gate 		/*
4057c478bd9Sstevel@tonic-gate 		 * Already mapped; is offset within our window?
4067c478bd9Sstevel@tonic-gate 		 */
4077c478bd9Sstevel@tonic-gate 		if (offset >= rsp->rd_window_base &&
4087c478bd9Sstevel@tonic-gate 		    offset < rsp->rd_window_base + rsp->rd_window_size) {
4097c478bd9Sstevel@tonic-gate 			return;
4107c478bd9Sstevel@tonic-gate 		}
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate 		/*
4137c478bd9Sstevel@tonic-gate 		 * No, we need to re-map; toss the old mapping.
4147c478bd9Sstevel@tonic-gate 		 */
4157c478bd9Sstevel@tonic-gate 		rd_unmap_window(rsp);
4167c478bd9Sstevel@tonic-gate 	}
4177c478bd9Sstevel@tonic-gate 	rsp->rd_window_base = ptob(offpgs);
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate 	/*
4207c478bd9Sstevel@tonic-gate 	 * Different algorithms depending on whether this is a real
4217c478bd9Sstevel@tonic-gate 	 * OBP-created ramdisk, or a pseudo ramdisk.
4227c478bd9Sstevel@tonic-gate 	 */
4237c478bd9Sstevel@tonic-gate 	if (rsp->rd_dip == rd_dip) {
4247c478bd9Sstevel@tonic-gate 		pgcnt_t	pi, lastpi;
4257c478bd9Sstevel@tonic-gate 		caddr_t	vaddr;
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate 		/*
4287c478bd9Sstevel@tonic-gate 		 * Find the range of pages which should be mapped.
4297c478bd9Sstevel@tonic-gate 		 */
4307c478bd9Sstevel@tonic-gate 		pi = offpgs;
4317c478bd9Sstevel@tonic-gate 		lastpi = pi + btopr(rsp->rd_window_size);
4327c478bd9Sstevel@tonic-gate 		if (lastpi > rsp->rd_npages) {
4337c478bd9Sstevel@tonic-gate 			lastpi = rsp->rd_npages;
4347c478bd9Sstevel@tonic-gate 		}
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate 		/*
4377c478bd9Sstevel@tonic-gate 		 * Load the mapping.
4387c478bd9Sstevel@tonic-gate 		 */
4397c478bd9Sstevel@tonic-gate 		vaddr = rsp->rd_window_virt;
4407c478bd9Sstevel@tonic-gate 		for (; pi < lastpi; ++pi) {
4417c478bd9Sstevel@tonic-gate 			hat_memload(kas.a_hat, vaddr, rsp->rd_ppa[pi],
4427c478bd9Sstevel@tonic-gate 			    (PROT_READ | PROT_WRITE) | HAT_NOSYNC,
4437c478bd9Sstevel@tonic-gate 			    HAT_LOAD_LOCK);
4447c478bd9Sstevel@tonic-gate 			vaddr += ptob(1);
4457c478bd9Sstevel@tonic-gate 		}
4467c478bd9Sstevel@tonic-gate 	} else {
4477c478bd9Sstevel@tonic-gate 		uint_t	i;
4487c478bd9Sstevel@tonic-gate 		pfn_t	pfn;
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate 		/*
4517c478bd9Sstevel@tonic-gate 		 * Real OBP-created ramdisk: locate the physical range which
4527c478bd9Sstevel@tonic-gate 		 * contains this offset.
4537c478bd9Sstevel@tonic-gate 		 */
4547c478bd9Sstevel@tonic-gate 		for (i = 0; i < rsp->rd_nexisting; ++i) {
4557c478bd9Sstevel@tonic-gate 			if (offset < rsp->rd_existing[i].size) {
4567c478bd9Sstevel@tonic-gate 				break;
4577c478bd9Sstevel@tonic-gate 			}
4587c478bd9Sstevel@tonic-gate 			offset -= rsp->rd_existing[i].size;
4597c478bd9Sstevel@tonic-gate 		}
4607c478bd9Sstevel@tonic-gate 		ASSERT(i < rsp->rd_nexisting);
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate 		/*
4637c478bd9Sstevel@tonic-gate 		 * Load the mapping.
4647c478bd9Sstevel@tonic-gate 		 */
4657c478bd9Sstevel@tonic-gate 		pfn = btop(rsp->rd_existing[i].phys + offset);
4667c478bd9Sstevel@tonic-gate 		hat_devload(kas.a_hat, rsp->rd_window_virt, rsp->rd_window_size,
4677c478bd9Sstevel@tonic-gate 		    pfn, (PROT_READ | PROT_WRITE),
4687c478bd9Sstevel@tonic-gate 		    HAT_LOAD_NOCONSIST | HAT_LOAD_LOCK);
4697c478bd9Sstevel@tonic-gate 	}
4707c478bd9Sstevel@tonic-gate }
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate /*
4737c478bd9Sstevel@tonic-gate  * Fakes up a disk geometry, and one big partition, based on the size
4747c478bd9Sstevel@tonic-gate  * of the file. This is needed because we allow newfs'ing the device,
4757c478bd9Sstevel@tonic-gate  * and newfs will do several disk ioctls to figure out the geometry and
4767c478bd9Sstevel@tonic-gate  * partition information. It uses that information to determine the parameters
477*63360950Smp204432  * to pass to mkfs. Geometry is pretty much irrelevant these days, but we
4787c478bd9Sstevel@tonic-gate  * have to support it.
4797c478bd9Sstevel@tonic-gate  *
4807c478bd9Sstevel@tonic-gate  * Stolen from lofi.c - should maybe split out common code sometime.
4817c478bd9Sstevel@tonic-gate  */
4827c478bd9Sstevel@tonic-gate static void
4837c478bd9Sstevel@tonic-gate rd_fake_disk_geometry(rd_devstate_t *rsp)
4847c478bd9Sstevel@tonic-gate {
4857c478bd9Sstevel@tonic-gate 	/* dk_geom - see dkio(7I) */
4867c478bd9Sstevel@tonic-gate 	/*
4877c478bd9Sstevel@tonic-gate 	 * dkg_ncyl _could_ be set to one here (one big cylinder with gobs
4887c478bd9Sstevel@tonic-gate 	 * of sectors), but that breaks programs like fdisk which want to
4897c478bd9Sstevel@tonic-gate 	 * partition a disk by cylinder. With one cylinder, you can't create
4907c478bd9Sstevel@tonic-gate 	 * an fdisk partition and put pcfs on it for testing (hard to pick
4917c478bd9Sstevel@tonic-gate 	 * a number between one and one).
4927c478bd9Sstevel@tonic-gate 	 *
4937c478bd9Sstevel@tonic-gate 	 * The cheezy floppy test is an attempt to not have too few cylinders
4947c478bd9Sstevel@tonic-gate 	 * for a small file, or so many on a big file that you waste space
4957c478bd9Sstevel@tonic-gate 	 * for backup superblocks or cylinder group structures.
4967c478bd9Sstevel@tonic-gate 	 */
4977c478bd9Sstevel@tonic-gate 	if (rsp->rd_size < (2 * 1024 * 1024)) /* floppy? */
4987c478bd9Sstevel@tonic-gate 		rsp->rd_dkg.dkg_ncyl = rsp->rd_size / (100 * 1024);
4997c478bd9Sstevel@tonic-gate 	else
5007c478bd9Sstevel@tonic-gate 		rsp->rd_dkg.dkg_ncyl = rsp->rd_size / (300 * 1024);
5017c478bd9Sstevel@tonic-gate 	/* in case file file is < 100k */
5027c478bd9Sstevel@tonic-gate 	if (rsp->rd_dkg.dkg_ncyl == 0)
5037c478bd9Sstevel@tonic-gate 		rsp->rd_dkg.dkg_ncyl = 1;
5047c478bd9Sstevel@tonic-gate 	rsp->rd_dkg.dkg_acyl = 0;
5057c478bd9Sstevel@tonic-gate 	rsp->rd_dkg.dkg_bcyl = 0;
5067c478bd9Sstevel@tonic-gate 	rsp->rd_dkg.dkg_nhead = 1;
5077c478bd9Sstevel@tonic-gate 	rsp->rd_dkg.dkg_obs1 = 0;
5087c478bd9Sstevel@tonic-gate 	rsp->rd_dkg.dkg_intrlv = 0;
5097c478bd9Sstevel@tonic-gate 	rsp->rd_dkg.dkg_obs2 = 0;
5107c478bd9Sstevel@tonic-gate 	rsp->rd_dkg.dkg_obs3 = 0;
5117c478bd9Sstevel@tonic-gate 	rsp->rd_dkg.dkg_apc = 0;
5127c478bd9Sstevel@tonic-gate 	rsp->rd_dkg.dkg_rpm = 7200;
5137c478bd9Sstevel@tonic-gate 	rsp->rd_dkg.dkg_pcyl = rsp->rd_dkg.dkg_ncyl + rsp->rd_dkg.dkg_acyl;
5147c478bd9Sstevel@tonic-gate 	rsp->rd_dkg.dkg_nsect = rsp->rd_size /
5157c478bd9Sstevel@tonic-gate 	    (DEV_BSIZE * rsp->rd_dkg.dkg_ncyl);
5167c478bd9Sstevel@tonic-gate 	rsp->rd_dkg.dkg_write_reinstruct = 0;
5177c478bd9Sstevel@tonic-gate 	rsp->rd_dkg.dkg_read_reinstruct = 0;
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate 	/* vtoc - see dkio(7I) */
5207c478bd9Sstevel@tonic-gate 	bzero(&rsp->rd_vtoc, sizeof (struct vtoc));
5217c478bd9Sstevel@tonic-gate 	rsp->rd_vtoc.v_sanity = VTOC_SANE;
5227c478bd9Sstevel@tonic-gate 	rsp->rd_vtoc.v_version = V_VERSION;
5237c478bd9Sstevel@tonic-gate 	bcopy(RD_DRIVER_NAME, rsp->rd_vtoc.v_volume, 7);
5247c478bd9Sstevel@tonic-gate 	rsp->rd_vtoc.v_sectorsz = DEV_BSIZE;
5257c478bd9Sstevel@tonic-gate 	rsp->rd_vtoc.v_nparts = 1;
5267c478bd9Sstevel@tonic-gate 	rsp->rd_vtoc.v_part[0].p_tag = V_UNASSIGNED;
5277c478bd9Sstevel@tonic-gate 	rsp->rd_vtoc.v_part[0].p_flag = V_UNMNT;
5287c478bd9Sstevel@tonic-gate 	rsp->rd_vtoc.v_part[0].p_start = (daddr_t)0;
5297c478bd9Sstevel@tonic-gate 	/*
5307c478bd9Sstevel@tonic-gate 	 * The partition size cannot just be the number of sectors, because
5317c478bd9Sstevel@tonic-gate 	 * that might not end on a cylinder boundary. And if that's the case,
5327c478bd9Sstevel@tonic-gate 	 * newfs/mkfs will print a scary warning. So just figure the size
5337c478bd9Sstevel@tonic-gate 	 * based on the number of cylinders and sectors/cylinder.
5347c478bd9Sstevel@tonic-gate 	 */
5357c478bd9Sstevel@tonic-gate 	rsp->rd_vtoc.v_part[0].p_size = rsp->rd_dkg.dkg_pcyl *
5367c478bd9Sstevel@tonic-gate 	    rsp->rd_dkg.dkg_nsect * rsp->rd_dkg.dkg_nhead;
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate 	/* dk_cinfo - see dkio(7I) */
5397c478bd9Sstevel@tonic-gate 	bzero(&rsp->rd_ci, sizeof (struct dk_cinfo));
5407c478bd9Sstevel@tonic-gate 	(void) strcpy(rsp->rd_ci.dki_cname, RD_DRIVER_NAME);
5417c478bd9Sstevel@tonic-gate 	rsp->rd_ci.dki_ctype = DKC_MD;
5427c478bd9Sstevel@tonic-gate 	rsp->rd_ci.dki_flags = 0;
5437c478bd9Sstevel@tonic-gate 	rsp->rd_ci.dki_cnum = 0;
5447c478bd9Sstevel@tonic-gate 	rsp->rd_ci.dki_addr = 0;
5457c478bd9Sstevel@tonic-gate 	rsp->rd_ci.dki_space = 0;
5467c478bd9Sstevel@tonic-gate 	rsp->rd_ci.dki_prio = 0;
5477c478bd9Sstevel@tonic-gate 	rsp->rd_ci.dki_vec = 0;
5487c478bd9Sstevel@tonic-gate 	(void) strcpy(rsp->rd_ci.dki_dname, RD_DRIVER_NAME);
5497c478bd9Sstevel@tonic-gate 	rsp->rd_ci.dki_unit = 0;
5507c478bd9Sstevel@tonic-gate 	rsp->rd_ci.dki_slave = 0;
5517c478bd9Sstevel@tonic-gate 	rsp->rd_ci.dki_partition = 0;
5527c478bd9Sstevel@tonic-gate 	/*
5537c478bd9Sstevel@tonic-gate 	 * newfs uses this to set maxcontig. Must not be < 16, or it
5547c478bd9Sstevel@tonic-gate 	 * will be 0 when newfs multiplies it by DEV_BSIZE and divides
5557c478bd9Sstevel@tonic-gate 	 * it by the block size. Then tunefs doesn't work because
5567c478bd9Sstevel@tonic-gate 	 * maxcontig is 0.
5577c478bd9Sstevel@tonic-gate 	 */
5587c478bd9Sstevel@tonic-gate 	rsp->rd_ci.dki_maxtransfer = 16;
5597c478bd9Sstevel@tonic-gate }
5607c478bd9Sstevel@tonic-gate 
5617c478bd9Sstevel@tonic-gate /*
5627c478bd9Sstevel@tonic-gate  * Deallocate resources (virtual and physical, device nodes, structures)
5637c478bd9Sstevel@tonic-gate  * from a ramdisk.
5647c478bd9Sstevel@tonic-gate  */
5657c478bd9Sstevel@tonic-gate static void
5667c478bd9Sstevel@tonic-gate rd_dealloc_resources(rd_devstate_t *rsp)
5677c478bd9Sstevel@tonic-gate {
5687c478bd9Sstevel@tonic-gate 	dev_info_t	*dip = rsp->rd_dip;
5697c478bd9Sstevel@tonic-gate 	char		namebuf[RD_NAME_LEN + 5];
5707c478bd9Sstevel@tonic-gate 	dev_t		fulldev;
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate 	if (rsp->rd_window_virt != NULL) {
5737c478bd9Sstevel@tonic-gate 		if (rsp->rd_window_base != RD_WINDOW_NOT_MAPPED) {
5747c478bd9Sstevel@tonic-gate 			rd_unmap_window(rsp);
5757c478bd9Sstevel@tonic-gate 		}
5767c478bd9Sstevel@tonic-gate 		vmem_free(heap_arena, rsp->rd_window_virt, rsp->rd_window_size);
5777c478bd9Sstevel@tonic-gate 	}
5787c478bd9Sstevel@tonic-gate 	mutex_destroy(&rsp->rd_device_lock);
5797c478bd9Sstevel@tonic-gate 
5807c478bd9Sstevel@tonic-gate 	if (rsp->rd_existing) {
5817c478bd9Sstevel@tonic-gate 		ddi_prop_free(rsp->rd_existing);
5827c478bd9Sstevel@tonic-gate 	}
5837c478bd9Sstevel@tonic-gate 	if (rsp->rd_ppa != NULL) {
5847c478bd9Sstevel@tonic-gate 		rd_phys_free(rsp->rd_ppa, rsp->rd_npages);
5857c478bd9Sstevel@tonic-gate 	}
5867c478bd9Sstevel@tonic-gate 
5877c478bd9Sstevel@tonic-gate 	/*
5887c478bd9Sstevel@tonic-gate 	 * Remove the block and raw device nodes.
5897c478bd9Sstevel@tonic-gate 	 */
5907c478bd9Sstevel@tonic-gate 	if (dip == rd_dip) {
5917c478bd9Sstevel@tonic-gate 		(void) snprintf(namebuf, sizeof (namebuf), "%s",
5927c478bd9Sstevel@tonic-gate 		    rsp->rd_name);
5937c478bd9Sstevel@tonic-gate 		ddi_remove_minor_node(dip, namebuf);
5947c478bd9Sstevel@tonic-gate 		(void) snprintf(namebuf, sizeof (namebuf), "%s,raw",
5957c478bd9Sstevel@tonic-gate 		    rsp->rd_name);
5967c478bd9Sstevel@tonic-gate 		ddi_remove_minor_node(dip, namebuf);
5977c478bd9Sstevel@tonic-gate 	} else {
5987c478bd9Sstevel@tonic-gate 		ddi_remove_minor_node(dip, "a");
5997c478bd9Sstevel@tonic-gate 		ddi_remove_minor_node(dip, "a,raw");
6007c478bd9Sstevel@tonic-gate 	}
6017c478bd9Sstevel@tonic-gate 
6027c478bd9Sstevel@tonic-gate 	/*
6037c478bd9Sstevel@tonic-gate 	 * Remove the "Size" and "Nblocks" properties.
6047c478bd9Sstevel@tonic-gate 	 */
6057c478bd9Sstevel@tonic-gate 	fulldev = makedevice(ddi_driver_major(dip), rsp->rd_minor);
6067c478bd9Sstevel@tonic-gate 	(void) ddi_prop_remove(fulldev, dip, SIZE_PROP_NAME);
6077c478bd9Sstevel@tonic-gate 	(void) ddi_prop_remove(fulldev, dip, NBLOCKS_PROP_NAME);
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate 	if (rsp->rd_kstat) {
6107c478bd9Sstevel@tonic-gate 		kstat_delete(rsp->rd_kstat);
6117c478bd9Sstevel@tonic-gate 		mutex_destroy(&rsp->rd_kstat_lock);
6127c478bd9Sstevel@tonic-gate 	}
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate 	ddi_soft_state_free(rd_statep, rsp->rd_minor);
6157c478bd9Sstevel@tonic-gate }
6167c478bd9Sstevel@tonic-gate 
6177c478bd9Sstevel@tonic-gate /*
6187c478bd9Sstevel@tonic-gate  * Allocate resources (virtual and physical, device nodes, structures)
6197c478bd9Sstevel@tonic-gate  * to a ramdisk.
6207c478bd9Sstevel@tonic-gate  */
6217c478bd9Sstevel@tonic-gate static rd_devstate_t *
6227c478bd9Sstevel@tonic-gate rd_alloc_resources(char *name, size_t size, dev_info_t *dip)
6237c478bd9Sstevel@tonic-gate {
6247c478bd9Sstevel@tonic-gate 	minor_t		minor;
6257c478bd9Sstevel@tonic-gate 	rd_devstate_t	*rsp;
6267c478bd9Sstevel@tonic-gate 	char		namebuf[RD_NAME_LEN + 5];
6277c478bd9Sstevel@tonic-gate 	dev_t		fulldev;
6287c478bd9Sstevel@tonic-gate 	int64_t		Nblocks_prop_val;
6297c478bd9Sstevel@tonic-gate 	int64_t		Size_prop_val;
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate 	minor = rd_find_free_minor();
6327c478bd9Sstevel@tonic-gate 	if (ddi_soft_state_zalloc(rd_statep, minor) == DDI_FAILURE) {
6337c478bd9Sstevel@tonic-gate 		return (NULL);
6347c478bd9Sstevel@tonic-gate 	}
6357c478bd9Sstevel@tonic-gate 	rsp = ddi_get_soft_state(rd_statep, minor);
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate 	(void) strcpy(rsp->rd_name, name);
6387c478bd9Sstevel@tonic-gate 	rsp->rd_dip = dip;
6397c478bd9Sstevel@tonic-gate 	rsp->rd_minor = minor;
6407c478bd9Sstevel@tonic-gate 	rsp->rd_size = size;
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate 	/*
6437c478bd9Sstevel@tonic-gate 	 * Allocate virtual window onto ramdisk.
6447c478bd9Sstevel@tonic-gate 	 */
6457c478bd9Sstevel@tonic-gate 	mutex_init(&rsp->rd_device_lock, NULL, MUTEX_DRIVER, NULL);
6467c478bd9Sstevel@tonic-gate 	rsp->rd_window_base = RD_WINDOW_NOT_MAPPED;
6477c478bd9Sstevel@tonic-gate 	rsp->rd_window_size = PAGESIZE;
6487c478bd9Sstevel@tonic-gate 	rsp->rd_window_virt = vmem_alloc(heap_arena,
6497c478bd9Sstevel@tonic-gate 	    rsp->rd_window_size, VM_SLEEP);
6507c478bd9Sstevel@tonic-gate 	if (rsp->rd_window_virt == NULL) {
6517c478bd9Sstevel@tonic-gate 		goto create_failed;
6527c478bd9Sstevel@tonic-gate 	}
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate 	/*
6557c478bd9Sstevel@tonic-gate 	 * Allocate physical memory for non-OBP ramdisks.
6567c478bd9Sstevel@tonic-gate 	 * Create pseudo block and raw device nodes.
6577c478bd9Sstevel@tonic-gate 	 */
6587c478bd9Sstevel@tonic-gate 	if (dip == rd_dip) {
6597c478bd9Sstevel@tonic-gate 		rsp->rd_npages = btopr(size);
6607c478bd9Sstevel@tonic-gate 		rsp->rd_ppa = rd_phys_alloc(rsp->rd_npages);
6617c478bd9Sstevel@tonic-gate 		if (rsp->rd_ppa == NULL) {
6627c478bd9Sstevel@tonic-gate 			goto create_failed;
6637c478bd9Sstevel@tonic-gate 		}
6647c478bd9Sstevel@tonic-gate 
6657c478bd9Sstevel@tonic-gate 		/*
6667c478bd9Sstevel@tonic-gate 		 * For non-OBP ramdisks the device nodes are:
6677c478bd9Sstevel@tonic-gate 		 *
6687c478bd9Sstevel@tonic-gate 		 *	/devices/pseudo/ramdisk@0:<diskname>
6697c478bd9Sstevel@tonic-gate 		 *	/devices/pseudo/ramdisk@0:<diskname>,raw
6707c478bd9Sstevel@tonic-gate 		 */
6717c478bd9Sstevel@tonic-gate 		(void) snprintf(namebuf, sizeof (namebuf), "%s",
6727c478bd9Sstevel@tonic-gate 		    rsp->rd_name);
6737c478bd9Sstevel@tonic-gate 		if (ddi_create_minor_node(dip, namebuf, S_IFBLK, minor,
6747c478bd9Sstevel@tonic-gate 		    DDI_PSEUDO, 0) == DDI_FAILURE) {
6757c478bd9Sstevel@tonic-gate 			goto create_failed;
6767c478bd9Sstevel@tonic-gate 		}
6777c478bd9Sstevel@tonic-gate 		(void) snprintf(namebuf, sizeof (namebuf), "%s,raw",
6787c478bd9Sstevel@tonic-gate 		    rsp->rd_name);
6797c478bd9Sstevel@tonic-gate 		if (ddi_create_minor_node(dip, namebuf, S_IFCHR, minor,
6807c478bd9Sstevel@tonic-gate 		    DDI_PSEUDO, 0) == DDI_FAILURE) {
6817c478bd9Sstevel@tonic-gate 			goto create_failed;
6827c478bd9Sstevel@tonic-gate 		}
6837c478bd9Sstevel@tonic-gate 	} else {
6847c478bd9Sstevel@tonic-gate 		/*
6857c478bd9Sstevel@tonic-gate 		 * For OBP-created ramdisks the device nodes are:
6867c478bd9Sstevel@tonic-gate 		 *
6877c478bd9Sstevel@tonic-gate 		 *	/devices/ramdisk-<diskname>:a
6887c478bd9Sstevel@tonic-gate 		 *	/devices/ramdisk-<diskname>:a,raw
6897c478bd9Sstevel@tonic-gate 		 */
6907c478bd9Sstevel@tonic-gate 		if (ddi_create_minor_node(dip, "a", S_IFBLK, minor,
6917c478bd9Sstevel@tonic-gate 		    DDI_PSEUDO, 0) == DDI_FAILURE) {
6927c478bd9Sstevel@tonic-gate 			goto create_failed;
6937c478bd9Sstevel@tonic-gate 		}
6947c478bd9Sstevel@tonic-gate 		if (ddi_create_minor_node(dip, "a,raw", S_IFCHR, minor,
6957c478bd9Sstevel@tonic-gate 		    DDI_PSEUDO, 0) == DDI_FAILURE) {
6967c478bd9Sstevel@tonic-gate 			goto create_failed;
6977c478bd9Sstevel@tonic-gate 		}
6987c478bd9Sstevel@tonic-gate 	}
6997c478bd9Sstevel@tonic-gate 
7007c478bd9Sstevel@tonic-gate 	/*
7017c478bd9Sstevel@tonic-gate 	 * Create the "Size" and "Nblocks" properties.
7027c478bd9Sstevel@tonic-gate 	 */
7037c478bd9Sstevel@tonic-gate 	fulldev = makedevice(ddi_driver_major(dip), minor);
7047c478bd9Sstevel@tonic-gate 	Size_prop_val = size;
7057c478bd9Sstevel@tonic-gate 	if ((ddi_prop_update_int64(fulldev, dip,
7067c478bd9Sstevel@tonic-gate 	    SIZE_PROP_NAME, Size_prop_val)) != DDI_PROP_SUCCESS) {
7077c478bd9Sstevel@tonic-gate 		goto create_failed;
7087c478bd9Sstevel@tonic-gate 	}
7097c478bd9Sstevel@tonic-gate 	Nblocks_prop_val = size / DEV_BSIZE;
7107c478bd9Sstevel@tonic-gate 	if ((ddi_prop_update_int64(fulldev, dip,
7117c478bd9Sstevel@tonic-gate 	    NBLOCKS_PROP_NAME, Nblocks_prop_val)) != DDI_PROP_SUCCESS) {
7127c478bd9Sstevel@tonic-gate 		goto create_failed;
7137c478bd9Sstevel@tonic-gate 	}
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate 	/*
7167c478bd9Sstevel@tonic-gate 	 * Allocate kstat stuff.
7177c478bd9Sstevel@tonic-gate 	 */
7187c478bd9Sstevel@tonic-gate 	rsp->rd_kstat = kstat_create(RD_DRIVER_NAME, minor, NULL,
7197c478bd9Sstevel@tonic-gate 					"disk", KSTAT_TYPE_IO, 1, 0);
7207c478bd9Sstevel@tonic-gate 	if (rsp->rd_kstat) {
7217c478bd9Sstevel@tonic-gate 		mutex_init(&rsp->rd_kstat_lock, NULL,
7227c478bd9Sstevel@tonic-gate 		    MUTEX_DRIVER, NULL);
7237c478bd9Sstevel@tonic-gate 		rsp->rd_kstat->ks_lock = &rsp->rd_kstat_lock;
7247c478bd9Sstevel@tonic-gate 		kstat_install(rsp->rd_kstat);
7257c478bd9Sstevel@tonic-gate 	}
7267c478bd9Sstevel@tonic-gate 
7277c478bd9Sstevel@tonic-gate 	rd_fake_disk_geometry(rsp);
7287c478bd9Sstevel@tonic-gate 
7297c478bd9Sstevel@tonic-gate 	return (rsp);
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate create_failed:
7327c478bd9Sstevel@tonic-gate 	/*
7337c478bd9Sstevel@tonic-gate 	 * Cleanup.
7347c478bd9Sstevel@tonic-gate 	 */
7357c478bd9Sstevel@tonic-gate 	rd_dealloc_resources(rsp);
7367c478bd9Sstevel@tonic-gate 
7377c478bd9Sstevel@tonic-gate 	return (NULL);
7387c478bd9Sstevel@tonic-gate }
7397c478bd9Sstevel@tonic-gate 
7407c478bd9Sstevel@tonic-gate /*
7417c478bd9Sstevel@tonic-gate  * Undo what we did in rd_attach, freeing resources and removing things which
7427c478bd9Sstevel@tonic-gate  * we installed.  The system framework guarantees we are not active with this
7437c478bd9Sstevel@tonic-gate  * devinfo node in any other entry points at this time.
7447c478bd9Sstevel@tonic-gate  */
7457c478bd9Sstevel@tonic-gate static int
7467c478bd9Sstevel@tonic-gate rd_common_detach(dev_info_t *dip)
7477c478bd9Sstevel@tonic-gate {
7487c478bd9Sstevel@tonic-gate 	if (dip == rd_dip) {
7497c478bd9Sstevel@tonic-gate 		/*
7507c478bd9Sstevel@tonic-gate 		 * Pseudo node: can't detach if any pseudo ramdisks exist.
7517c478bd9Sstevel@tonic-gate 		 */
7527c478bd9Sstevel@tonic-gate 		if (rd_is_busy()) {
7537c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
7547c478bd9Sstevel@tonic-gate 		}
7557c478bd9Sstevel@tonic-gate 		ddi_soft_state_free(rd_statep, RD_CTL_MINOR);
7567c478bd9Sstevel@tonic-gate 		rd_dip = NULL;
7577c478bd9Sstevel@tonic-gate 	} else {
7587c478bd9Sstevel@tonic-gate 		/*
7597c478bd9Sstevel@tonic-gate 		 * A 'real' ramdisk; find the state and free resources.
7607c478bd9Sstevel@tonic-gate 		 */
7617c478bd9Sstevel@tonic-gate 		rd_devstate_t	*rsp;
7627c478bd9Sstevel@tonic-gate 
7637c478bd9Sstevel@tonic-gate 		if ((rsp = rd_find_dip_state(dip)) != NULL) {
7647c478bd9Sstevel@tonic-gate 			rd_dealloc_resources(rsp);
7657c478bd9Sstevel@tonic-gate 		}
7667c478bd9Sstevel@tonic-gate 	}
7677c478bd9Sstevel@tonic-gate 	ddi_remove_minor_node(dip, NULL);
7687c478bd9Sstevel@tonic-gate 
7697c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
7707c478bd9Sstevel@tonic-gate }
7717c478bd9Sstevel@tonic-gate 
7727c478bd9Sstevel@tonic-gate static int
7737c478bd9Sstevel@tonic-gate rd_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
7747c478bd9Sstevel@tonic-gate {
7757c478bd9Sstevel@tonic-gate 	char		*name;
7767c478bd9Sstevel@tonic-gate 	rd_existing_t	*ep = NULL;
7777c478bd9Sstevel@tonic-gate 	uint_t		nep, i;
7787c478bd9Sstevel@tonic-gate 	size_t		size = 0;
7797c478bd9Sstevel@tonic-gate 	rd_devstate_t	*rsp;
7807c478bd9Sstevel@tonic-gate 
7817c478bd9Sstevel@tonic-gate 	switch (cmd) {
7827c478bd9Sstevel@tonic-gate 
7837c478bd9Sstevel@tonic-gate 	case DDI_ATTACH:
7847c478bd9Sstevel@tonic-gate 		mutex_enter(&rd_lock);
7857c478bd9Sstevel@tonic-gate 
7867c478bd9Sstevel@tonic-gate 		/*
7877c478bd9Sstevel@tonic-gate 		 * For pseudo ramdisk devinfo set up state 0 and :ctl device;
7887c478bd9Sstevel@tonic-gate 		 * else it's an OBP-created ramdisk.
7897c478bd9Sstevel@tonic-gate 		 */
7907c478bd9Sstevel@tonic-gate 		if (is_pseudo_device(dip)) {
7917c478bd9Sstevel@tonic-gate 			rd_dip = dip;
7927c478bd9Sstevel@tonic-gate 			rd_init_tuneables();
7937c478bd9Sstevel@tonic-gate 
7947c478bd9Sstevel@tonic-gate 			/*
7957c478bd9Sstevel@tonic-gate 			 * The zeroth minor is reserved for the ramdisk
7967c478bd9Sstevel@tonic-gate 			 * 'control' device.
7977c478bd9Sstevel@tonic-gate 			 */
7987c478bd9Sstevel@tonic-gate 			if (ddi_soft_state_zalloc(rd_statep, RD_CTL_MINOR) ==
7997c478bd9Sstevel@tonic-gate 			    DDI_FAILURE) {
8007c478bd9Sstevel@tonic-gate 				goto attach_failed;
8017c478bd9Sstevel@tonic-gate 			}
8027c478bd9Sstevel@tonic-gate 			rsp = ddi_get_soft_state(rd_statep, RD_CTL_MINOR);
8037c478bd9Sstevel@tonic-gate 			rsp->rd_dip = dip;
8047c478bd9Sstevel@tonic-gate 
8057c478bd9Sstevel@tonic-gate 			if (ddi_create_minor_node(dip, RD_CTL_NODE,
8067c478bd9Sstevel@tonic-gate 			    S_IFCHR, 0, DDI_PSEUDO, NULL) == DDI_FAILURE) {
8077c478bd9Sstevel@tonic-gate 				goto attach_failed;
8087c478bd9Sstevel@tonic-gate 			}
8097c478bd9Sstevel@tonic-gate 		} else {
8107c478bd9Sstevel@tonic-gate 			RD_STRIP_PREFIX(name, ddi_node_name(dip));
8117c478bd9Sstevel@tonic-gate 
8127c478bd9Sstevel@tonic-gate 			if (strlen(name) > RD_NAME_LEN) {
8137c478bd9Sstevel@tonic-gate 				cmn_err(CE_CONT,
8147c478bd9Sstevel@tonic-gate 				    "%s: name too long - ignoring\n", name);
8157c478bd9Sstevel@tonic-gate 				goto attach_failed;
8167c478bd9Sstevel@tonic-gate 			}
8177c478bd9Sstevel@tonic-gate 
8187c478bd9Sstevel@tonic-gate 			/*
8197c478bd9Sstevel@tonic-gate 			 * An OBP-created ramdisk must have an 'existing'
8207c478bd9Sstevel@tonic-gate 			 * property; get and check it.
8217c478bd9Sstevel@tonic-gate 			 */
8227c478bd9Sstevel@tonic-gate 			if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, dip,
8237c478bd9Sstevel@tonic-gate 			    DDI_PROP_DONTPASS, RD_EXISTING_PROP_NAME,
8247c478bd9Sstevel@tonic-gate 			    (uchar_t **)&ep, &nep) != DDI_SUCCESS) {
8257c478bd9Sstevel@tonic-gate 				cmn_err(CE_CONT,
8267c478bd9Sstevel@tonic-gate 				    "%s: " RD_EXISTING_PROP_NAME
8277c478bd9Sstevel@tonic-gate 				    " property missing\n", name);
8287c478bd9Sstevel@tonic-gate 				goto attach_failed;
8297c478bd9Sstevel@tonic-gate 			}
8307c478bd9Sstevel@tonic-gate 			if (nep == 0 || (nep % sizeof (*ep)) != 0) {
8317c478bd9Sstevel@tonic-gate 				cmn_err(CE_CONT,
8327c478bd9Sstevel@tonic-gate 				    "%s: " RD_EXISTING_PROP_NAME
8337c478bd9Sstevel@tonic-gate 				    " illegal size\n", name);
8347c478bd9Sstevel@tonic-gate 				goto attach_failed;
8357c478bd9Sstevel@tonic-gate 			}
8367c478bd9Sstevel@tonic-gate 			nep /= sizeof (*ep);
8377c478bd9Sstevel@tonic-gate 
8387c478bd9Sstevel@tonic-gate 			/*
8397c478bd9Sstevel@tonic-gate 			 * Calculate the size of the ramdisk.
8407c478bd9Sstevel@tonic-gate 			 */
8417c478bd9Sstevel@tonic-gate 			for (i = 0; i < nep; ++i) {
8427c478bd9Sstevel@tonic-gate 				size += ep[i].size;
8437c478bd9Sstevel@tonic-gate 			}
8447c478bd9Sstevel@tonic-gate 
8457c478bd9Sstevel@tonic-gate 			/*
8467c478bd9Sstevel@tonic-gate 			 * Allocate driver resources for the ramdisk.
8477c478bd9Sstevel@tonic-gate 			 */
8487c478bd9Sstevel@tonic-gate 			if ((rsp = rd_alloc_resources(name, size,
8497c478bd9Sstevel@tonic-gate 			    dip)) == NULL) {
8507c478bd9Sstevel@tonic-gate 				goto attach_failed;
8517c478bd9Sstevel@tonic-gate 			}
8527c478bd9Sstevel@tonic-gate 
8537c478bd9Sstevel@tonic-gate 			rsp->rd_existing = ep;
8547c478bd9Sstevel@tonic-gate 			rsp->rd_nexisting = nep;
8557c478bd9Sstevel@tonic-gate 		}
8567c478bd9Sstevel@tonic-gate 
8577c478bd9Sstevel@tonic-gate 		mutex_exit(&rd_lock);
8587c478bd9Sstevel@tonic-gate 
8597c478bd9Sstevel@tonic-gate 		ddi_report_dev(dip);
8607c478bd9Sstevel@tonic-gate 
8617c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
8627c478bd9Sstevel@tonic-gate 
8637c478bd9Sstevel@tonic-gate 	case DDI_RESUME:
8647c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
8657c478bd9Sstevel@tonic-gate 
8667c478bd9Sstevel@tonic-gate 	default:
8677c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
8687c478bd9Sstevel@tonic-gate 	}
8697c478bd9Sstevel@tonic-gate 
8707c478bd9Sstevel@tonic-gate attach_failed:
8717c478bd9Sstevel@tonic-gate 	/*
8727c478bd9Sstevel@tonic-gate 	 * Use our common detach routine to unallocate any stuff which
8737c478bd9Sstevel@tonic-gate 	 * was allocated above.
8747c478bd9Sstevel@tonic-gate 	 */
8757c478bd9Sstevel@tonic-gate 	(void) rd_common_detach(dip);
8767c478bd9Sstevel@tonic-gate 	mutex_exit(&rd_lock);
8777c478bd9Sstevel@tonic-gate 
8787c478bd9Sstevel@tonic-gate 	if (ep != NULL) {
8797c478bd9Sstevel@tonic-gate 		ddi_prop_free(ep);
8807c478bd9Sstevel@tonic-gate 	}
8817c478bd9Sstevel@tonic-gate 	return (DDI_FAILURE);
8827c478bd9Sstevel@tonic-gate }
8837c478bd9Sstevel@tonic-gate 
8847c478bd9Sstevel@tonic-gate static int
8857c478bd9Sstevel@tonic-gate rd_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
8867c478bd9Sstevel@tonic-gate {
8877c478bd9Sstevel@tonic-gate 	int	e;
8887c478bd9Sstevel@tonic-gate 
8897c478bd9Sstevel@tonic-gate 	switch (cmd) {
8907c478bd9Sstevel@tonic-gate 
8917c478bd9Sstevel@tonic-gate 	case DDI_DETACH:
8927c478bd9Sstevel@tonic-gate 		mutex_enter(&rd_lock);
8937c478bd9Sstevel@tonic-gate 		e = rd_common_detach(dip);
8947c478bd9Sstevel@tonic-gate 		mutex_exit(&rd_lock);
8957c478bd9Sstevel@tonic-gate 
8967c478bd9Sstevel@tonic-gate 		return (e);
8977c478bd9Sstevel@tonic-gate 
8987c478bd9Sstevel@tonic-gate 	case DDI_SUSPEND:
8997c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
9007c478bd9Sstevel@tonic-gate 
9017c478bd9Sstevel@tonic-gate 	default:
9027c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
9037c478bd9Sstevel@tonic-gate 	}
9047c478bd9Sstevel@tonic-gate }
9057c478bd9Sstevel@tonic-gate 
9067c478bd9Sstevel@tonic-gate /*ARGSUSED*/
9077c478bd9Sstevel@tonic-gate static int
9087c478bd9Sstevel@tonic-gate rd_getinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
9097c478bd9Sstevel@tonic-gate {
9107c478bd9Sstevel@tonic-gate 	rd_devstate_t	*rsp;
9117c478bd9Sstevel@tonic-gate 
9127c478bd9Sstevel@tonic-gate 	switch (infocmd) {
9137c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
9147c478bd9Sstevel@tonic-gate 		if ((rsp = ddi_get_soft_state(rd_statep,
9157c478bd9Sstevel@tonic-gate 		    getminor((dev_t)arg))) != NULL) {
9167c478bd9Sstevel@tonic-gate 			*result = rsp->rd_dip;
9177c478bd9Sstevel@tonic-gate 			return (DDI_SUCCESS);
9187c478bd9Sstevel@tonic-gate 		}
9197c478bd9Sstevel@tonic-gate 		*result = NULL;
9207c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
9217c478bd9Sstevel@tonic-gate 
9227c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
9237c478bd9Sstevel@tonic-gate 		if ((rsp = ddi_get_soft_state(rd_statep,
9247c478bd9Sstevel@tonic-gate 		    getminor((dev_t)arg))) != NULL) {
9257c478bd9Sstevel@tonic-gate 			*result = (void *)(uintptr_t)
9267c478bd9Sstevel@tonic-gate 			    ddi_get_instance(rsp->rd_dip);
9277c478bd9Sstevel@tonic-gate 			return (DDI_SUCCESS);
9287c478bd9Sstevel@tonic-gate 		}
9297c478bd9Sstevel@tonic-gate 		*result = NULL;
9307c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
9317c478bd9Sstevel@tonic-gate 
9327c478bd9Sstevel@tonic-gate 	default:
9337c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
9347c478bd9Sstevel@tonic-gate 	}
9357c478bd9Sstevel@tonic-gate }
9367c478bd9Sstevel@tonic-gate 
9377c478bd9Sstevel@tonic-gate /*ARGSUSED3*/
9387c478bd9Sstevel@tonic-gate static int
9397c478bd9Sstevel@tonic-gate rd_open(dev_t *devp, int flag, int otyp, cred_t *credp)
9407c478bd9Sstevel@tonic-gate {
9417c478bd9Sstevel@tonic-gate 	minor_t		minor;
9427c478bd9Sstevel@tonic-gate 	rd_devstate_t	*rsp;
9437c478bd9Sstevel@tonic-gate 
9447c478bd9Sstevel@tonic-gate 	mutex_enter(&rd_lock);
9457c478bd9Sstevel@tonic-gate 
9467c478bd9Sstevel@tonic-gate 	minor = getminor(*devp);
9477c478bd9Sstevel@tonic-gate 	if (minor == RD_CTL_MINOR) {
9487c478bd9Sstevel@tonic-gate 		/*
9497c478bd9Sstevel@tonic-gate 		 * Master control device; must be opened exclusively.
9507c478bd9Sstevel@tonic-gate 		 */
9517c478bd9Sstevel@tonic-gate 		if ((flag & FEXCL) != FEXCL || otyp != OTYP_CHR) {
9527c478bd9Sstevel@tonic-gate 			mutex_exit(&rd_lock);
9537c478bd9Sstevel@tonic-gate 			return (EINVAL);
9547c478bd9Sstevel@tonic-gate 		}
9557c478bd9Sstevel@tonic-gate 
9567c478bd9Sstevel@tonic-gate 		rsp = ddi_get_soft_state(rd_statep, RD_CTL_MINOR);
9577c478bd9Sstevel@tonic-gate 		if (rsp == NULL) {
9587c478bd9Sstevel@tonic-gate 			mutex_exit(&rd_lock);
9597c478bd9Sstevel@tonic-gate 			return (ENXIO);
9607c478bd9Sstevel@tonic-gate 		}
9617c478bd9Sstevel@tonic-gate 
9627c478bd9Sstevel@tonic-gate 		if (rd_is_open(rsp)) {
9637c478bd9Sstevel@tonic-gate 			mutex_exit(&rd_lock);
9647c478bd9Sstevel@tonic-gate 			return (EBUSY);
9657c478bd9Sstevel@tonic-gate 		}
9667c478bd9Sstevel@tonic-gate 		(void) rd_opened(rsp, OTYP_CHR);
9677c478bd9Sstevel@tonic-gate 
9687c478bd9Sstevel@tonic-gate 		mutex_exit(&rd_lock);
9697c478bd9Sstevel@tonic-gate 
9707c478bd9Sstevel@tonic-gate 		return (0);
9717c478bd9Sstevel@tonic-gate 	}
9727c478bd9Sstevel@tonic-gate 
9737c478bd9Sstevel@tonic-gate 	rsp = ddi_get_soft_state(rd_statep, minor);
9747c478bd9Sstevel@tonic-gate 	if (rsp == NULL) {
9757c478bd9Sstevel@tonic-gate 		mutex_exit(&rd_lock);
9767c478bd9Sstevel@tonic-gate 		return (ENXIO);
9777c478bd9Sstevel@tonic-gate 	}
9787c478bd9Sstevel@tonic-gate 
9797c478bd9Sstevel@tonic-gate 	if (rd_opened(rsp, otyp) == -1) {
9807c478bd9Sstevel@tonic-gate 		mutex_exit(&rd_lock);
9817c478bd9Sstevel@tonic-gate 		return (EINVAL);
9827c478bd9Sstevel@tonic-gate 	}
9837c478bd9Sstevel@tonic-gate 
9847c478bd9Sstevel@tonic-gate 	mutex_exit(&rd_lock);
9857c478bd9Sstevel@tonic-gate 	return (0);
9867c478bd9Sstevel@tonic-gate }
9877c478bd9Sstevel@tonic-gate 
9887c478bd9Sstevel@tonic-gate /*ARGSUSED*/
9897c478bd9Sstevel@tonic-gate static int
9907c478bd9Sstevel@tonic-gate rd_close(dev_t dev, int flag, int otyp, struct cred *credp)
9917c478bd9Sstevel@tonic-gate {
9927c478bd9Sstevel@tonic-gate 	minor_t		minor;
9937c478bd9Sstevel@tonic-gate 	rd_devstate_t	*rsp;
9947c478bd9Sstevel@tonic-gate 
9957c478bd9Sstevel@tonic-gate 	mutex_enter(&rd_lock);
9967c478bd9Sstevel@tonic-gate 
9977c478bd9Sstevel@tonic-gate 	minor = getminor(dev);
9987c478bd9Sstevel@tonic-gate 
9997c478bd9Sstevel@tonic-gate 	rsp = ddi_get_soft_state(rd_statep, minor);
10007c478bd9Sstevel@tonic-gate 	if (rsp == NULL) {
10017c478bd9Sstevel@tonic-gate 		mutex_exit(&rd_lock);
10027c478bd9Sstevel@tonic-gate 		return (EINVAL);
10037c478bd9Sstevel@tonic-gate 	}
10047c478bd9Sstevel@tonic-gate 
10057c478bd9Sstevel@tonic-gate 	rd_closed(rsp, otyp);
10067c478bd9Sstevel@tonic-gate 
10077c478bd9Sstevel@tonic-gate 	mutex_exit(&rd_lock);
10087c478bd9Sstevel@tonic-gate 
10097c478bd9Sstevel@tonic-gate 	return (0);
10107c478bd9Sstevel@tonic-gate }
10117c478bd9Sstevel@tonic-gate 
10127c478bd9Sstevel@tonic-gate static void
10137c478bd9Sstevel@tonic-gate rd_minphys(struct buf *bp)
10147c478bd9Sstevel@tonic-gate {
10157c478bd9Sstevel@tonic-gate 	if (bp->b_bcount > rd_maxphys) {
10167c478bd9Sstevel@tonic-gate 		bp->b_bcount = rd_maxphys;
10177c478bd9Sstevel@tonic-gate 	}
10187c478bd9Sstevel@tonic-gate }
10197c478bd9Sstevel@tonic-gate 
10207c478bd9Sstevel@tonic-gate static void
10217c478bd9Sstevel@tonic-gate rd_rw(rd_devstate_t *rsp, struct buf *bp, offset_t offset, size_t nbytes)
10227c478bd9Sstevel@tonic-gate {
10237c478bd9Sstevel@tonic-gate 	int	reading = bp->b_flags & B_READ;
10247c478bd9Sstevel@tonic-gate 	caddr_t	buf_addr;
10257c478bd9Sstevel@tonic-gate 
10267c478bd9Sstevel@tonic-gate 	bp_mapin(bp);
10277c478bd9Sstevel@tonic-gate 	buf_addr = bp->b_un.b_addr;
10287c478bd9Sstevel@tonic-gate 
10297c478bd9Sstevel@tonic-gate 	while (nbytes > 0) {
10307c478bd9Sstevel@tonic-gate 		offset_t	off_in_window;
10317c478bd9Sstevel@tonic-gate 		size_t		rem_in_window, copy_bytes;
10327c478bd9Sstevel@tonic-gate 		caddr_t		raddr;
10337c478bd9Sstevel@tonic-gate 
10347c478bd9Sstevel@tonic-gate 		mutex_enter(&rsp->rd_device_lock);
10357c478bd9Sstevel@tonic-gate 		rd_map_window(rsp, offset);
10367c478bd9Sstevel@tonic-gate 
10377c478bd9Sstevel@tonic-gate 		off_in_window = offset - rsp->rd_window_base;
10387c478bd9Sstevel@tonic-gate 		rem_in_window = rsp->rd_window_size - off_in_window;
10397c478bd9Sstevel@tonic-gate 
10407c478bd9Sstevel@tonic-gate 		raddr = rsp->rd_window_virt + off_in_window;
10417c478bd9Sstevel@tonic-gate 		copy_bytes = MIN(nbytes, rem_in_window);
10427c478bd9Sstevel@tonic-gate 
10437c478bd9Sstevel@tonic-gate 		if (reading) {
10447c478bd9Sstevel@tonic-gate 			(void) bcopy(raddr, buf_addr, copy_bytes);
10457c478bd9Sstevel@tonic-gate 		} else {
10467c478bd9Sstevel@tonic-gate 			(void) bcopy(buf_addr, raddr, copy_bytes);
10477c478bd9Sstevel@tonic-gate 		}
10487c478bd9Sstevel@tonic-gate 		mutex_exit(&rsp->rd_device_lock);
10497c478bd9Sstevel@tonic-gate 
10507c478bd9Sstevel@tonic-gate 		offset   += copy_bytes;
10517c478bd9Sstevel@tonic-gate 		buf_addr += copy_bytes;
10527c478bd9Sstevel@tonic-gate 		nbytes   -= copy_bytes;
10537c478bd9Sstevel@tonic-gate 	}
10547c478bd9Sstevel@tonic-gate }
10557c478bd9Sstevel@tonic-gate 
10567c478bd9Sstevel@tonic-gate static int
10577c478bd9Sstevel@tonic-gate rd_strategy(struct buf *bp)
10587c478bd9Sstevel@tonic-gate {
10597c478bd9Sstevel@tonic-gate 	rd_devstate_t	*rsp;
10607c478bd9Sstevel@tonic-gate 	offset_t	offset;
10617c478bd9Sstevel@tonic-gate 
10627c478bd9Sstevel@tonic-gate 	rsp = ddi_get_soft_state(rd_statep, getminor(bp->b_edev));
10637c478bd9Sstevel@tonic-gate 	offset = bp->b_blkno * DEV_BSIZE;
10647c478bd9Sstevel@tonic-gate 
10657c478bd9Sstevel@tonic-gate 	if (rsp == NULL) {
10667c478bd9Sstevel@tonic-gate 		bp->b_error = ENXIO;
10677c478bd9Sstevel@tonic-gate 		bp->b_flags |= B_ERROR;
10687c478bd9Sstevel@tonic-gate 	} else if (offset >= rsp->rd_size) {
10697c478bd9Sstevel@tonic-gate 		bp->b_error = EINVAL;
10707c478bd9Sstevel@tonic-gate 		bp->b_flags |= B_ERROR;
10717c478bd9Sstevel@tonic-gate 	} else {
10727c478bd9Sstevel@tonic-gate 		size_t	nbytes;
10737c478bd9Sstevel@tonic-gate 
10747c478bd9Sstevel@tonic-gate 		if (rsp->rd_kstat) {
10757c478bd9Sstevel@tonic-gate 			mutex_enter(rsp->rd_kstat->ks_lock);
10767c478bd9Sstevel@tonic-gate 			kstat_runq_enter(KSTAT_IO_PTR(rsp->rd_kstat));
10777c478bd9Sstevel@tonic-gate 			mutex_exit(rsp->rd_kstat->ks_lock);
10787c478bd9Sstevel@tonic-gate 		}
10797c478bd9Sstevel@tonic-gate 
10807c478bd9Sstevel@tonic-gate 		nbytes = min(bp->b_bcount, rsp->rd_size - offset);
10817c478bd9Sstevel@tonic-gate 
10827c478bd9Sstevel@tonic-gate 		rd_rw(rsp, bp, offset, nbytes);
10837c478bd9Sstevel@tonic-gate 
10847c478bd9Sstevel@tonic-gate 		bp->b_resid = bp->b_bcount - nbytes;
10857c478bd9Sstevel@tonic-gate 
10867c478bd9Sstevel@tonic-gate 		if (rsp->rd_kstat) {
10877c478bd9Sstevel@tonic-gate 			kstat_io_t *kioptr;
10887c478bd9Sstevel@tonic-gate 
10897c478bd9Sstevel@tonic-gate 			mutex_enter(rsp->rd_kstat->ks_lock);
10907c478bd9Sstevel@tonic-gate 			kioptr = KSTAT_IO_PTR(rsp->rd_kstat);
10917c478bd9Sstevel@tonic-gate 			if (bp->b_flags & B_READ) {
10927c478bd9Sstevel@tonic-gate 				kioptr->nread += nbytes;
10937c478bd9Sstevel@tonic-gate 				kioptr->reads++;
10947c478bd9Sstevel@tonic-gate 			} else {
10957c478bd9Sstevel@tonic-gate 				kioptr->nwritten += nbytes;
10967c478bd9Sstevel@tonic-gate 				kioptr->writes++;
10977c478bd9Sstevel@tonic-gate 			}
10987c478bd9Sstevel@tonic-gate 			kstat_runq_exit(kioptr);
10997c478bd9Sstevel@tonic-gate 			mutex_exit(rsp->rd_kstat->ks_lock);
11007c478bd9Sstevel@tonic-gate 		}
11017c478bd9Sstevel@tonic-gate 	}
11027c478bd9Sstevel@tonic-gate 
11037c478bd9Sstevel@tonic-gate 	biodone(bp);
11047c478bd9Sstevel@tonic-gate 	return (0);
11057c478bd9Sstevel@tonic-gate }
11067c478bd9Sstevel@tonic-gate 
11077c478bd9Sstevel@tonic-gate /*ARGSUSED*/
11087c478bd9Sstevel@tonic-gate static int
11097c478bd9Sstevel@tonic-gate rd_read(dev_t dev, struct uio *uiop, cred_t *credp)
11107c478bd9Sstevel@tonic-gate {
11117c478bd9Sstevel@tonic-gate 	rd_devstate_t	*rsp;
11127c478bd9Sstevel@tonic-gate 
11137c478bd9Sstevel@tonic-gate 	rsp = ddi_get_soft_state(rd_statep, getminor(dev));
11147c478bd9Sstevel@tonic-gate 
11157c478bd9Sstevel@tonic-gate 	if (uiop->uio_offset >= rsp->rd_size)
11167c478bd9Sstevel@tonic-gate 		return (EINVAL);
11177c478bd9Sstevel@tonic-gate 
11187c478bd9Sstevel@tonic-gate 	return (physio(rd_strategy, NULL, dev, B_READ, rd_minphys, uiop));
11197c478bd9Sstevel@tonic-gate }
11207c478bd9Sstevel@tonic-gate 
11217c478bd9Sstevel@tonic-gate /*ARGSUSED*/
11227c478bd9Sstevel@tonic-gate static int
11237c478bd9Sstevel@tonic-gate rd_write(dev_t dev, register struct uio *uiop, cred_t *credp)
11247c478bd9Sstevel@tonic-gate {
11257c478bd9Sstevel@tonic-gate 	rd_devstate_t	*rsp;
11267c478bd9Sstevel@tonic-gate 
11277c478bd9Sstevel@tonic-gate 	rsp = ddi_get_soft_state(rd_statep, getminor(dev));
11287c478bd9Sstevel@tonic-gate 
11297c478bd9Sstevel@tonic-gate 	if (uiop->uio_offset >= rsp->rd_size)
11307c478bd9Sstevel@tonic-gate 		return (EINVAL);
11317c478bd9Sstevel@tonic-gate 
11327c478bd9Sstevel@tonic-gate 	return (physio(rd_strategy, NULL, dev, B_WRITE, rd_minphys, uiop));
11337c478bd9Sstevel@tonic-gate }
11347c478bd9Sstevel@tonic-gate 
11357c478bd9Sstevel@tonic-gate /*ARGSUSED*/
11367c478bd9Sstevel@tonic-gate static int
11377c478bd9Sstevel@tonic-gate rd_create_disk(dev_t dev, struct rd_ioctl *urip, int mode, int *rvalp)
11387c478bd9Sstevel@tonic-gate {
11397c478bd9Sstevel@tonic-gate 	struct rd_ioctl	kri;
11407c478bd9Sstevel@tonic-gate 	size_t		size;
11417c478bd9Sstevel@tonic-gate 	rd_devstate_t	*rsp;
11427c478bd9Sstevel@tonic-gate 
11437c478bd9Sstevel@tonic-gate 	if (ddi_copyin(urip, &kri, sizeof (kri), mode) == -1) {
11447c478bd9Sstevel@tonic-gate 		return (EFAULT);
11457c478bd9Sstevel@tonic-gate 	}
11467c478bd9Sstevel@tonic-gate 
11477c478bd9Sstevel@tonic-gate 	kri.ri_name[RD_NAME_LEN] = '\0';
11487c478bd9Sstevel@tonic-gate 
11497c478bd9Sstevel@tonic-gate 	size = kri.ri_size;
11507c478bd9Sstevel@tonic-gate 	if (size == 0) {
11517c478bd9Sstevel@tonic-gate 		return (EINVAL);
11527c478bd9Sstevel@tonic-gate 	}
11537c478bd9Sstevel@tonic-gate 	size = ptob(btopr(size));
11547c478bd9Sstevel@tonic-gate 
11557c478bd9Sstevel@tonic-gate 	mutex_enter(&rd_lock);
11567c478bd9Sstevel@tonic-gate 
11577c478bd9Sstevel@tonic-gate 	if (rd_find_named_disk(kri.ri_name) != NULL) {
11587c478bd9Sstevel@tonic-gate 		mutex_exit(&rd_lock);
11597c478bd9Sstevel@tonic-gate 		return (EEXIST);
11607c478bd9Sstevel@tonic-gate 	}
11617c478bd9Sstevel@tonic-gate 
11627c478bd9Sstevel@tonic-gate 	rsp = rd_alloc_resources(kri.ri_name, size, rd_dip);
11637c478bd9Sstevel@tonic-gate 	if (rsp == NULL) {
11647c478bd9Sstevel@tonic-gate 		mutex_exit(&rd_lock);
11657c478bd9Sstevel@tonic-gate 		return (EAGAIN);
11667c478bd9Sstevel@tonic-gate 	}
11677c478bd9Sstevel@tonic-gate 
11687c478bd9Sstevel@tonic-gate 	mutex_exit(&rd_lock);
11697c478bd9Sstevel@tonic-gate 
11707c478bd9Sstevel@tonic-gate 	return (ddi_copyout(&kri, urip, sizeof (kri), mode) == -1 ? EFAULT : 0);
11717c478bd9Sstevel@tonic-gate }
11727c478bd9Sstevel@tonic-gate 
11737c478bd9Sstevel@tonic-gate /*ARGSUSED*/
11747c478bd9Sstevel@tonic-gate static int
11757c478bd9Sstevel@tonic-gate rd_delete_disk(dev_t dev, struct rd_ioctl *urip, int mode)
11767c478bd9Sstevel@tonic-gate {
11777c478bd9Sstevel@tonic-gate 	struct rd_ioctl	kri;
11787c478bd9Sstevel@tonic-gate 	rd_devstate_t	*rsp;
11797c478bd9Sstevel@tonic-gate 
11807c478bd9Sstevel@tonic-gate 	if (ddi_copyin(urip, &kri, sizeof (kri), mode) == -1) {
11817c478bd9Sstevel@tonic-gate 		return (EFAULT);
11827c478bd9Sstevel@tonic-gate 	}
11837c478bd9Sstevel@tonic-gate 
11847c478bd9Sstevel@tonic-gate 	kri.ri_name[RD_NAME_LEN] = '\0';
11857c478bd9Sstevel@tonic-gate 
11867c478bd9Sstevel@tonic-gate 	mutex_enter(&rd_lock);
11877c478bd9Sstevel@tonic-gate 
11887c478bd9Sstevel@tonic-gate 	rsp = rd_find_named_disk(kri.ri_name);
11897c478bd9Sstevel@tonic-gate 	if (rsp == NULL || rsp->rd_dip != rd_dip) {
11907c478bd9Sstevel@tonic-gate 		mutex_exit(&rd_lock);
11917c478bd9Sstevel@tonic-gate 		return (EINVAL);
11927c478bd9Sstevel@tonic-gate 	}
11937c478bd9Sstevel@tonic-gate 	if (rd_is_open(rsp)) {
11947c478bd9Sstevel@tonic-gate 		mutex_exit(&rd_lock);
11957c478bd9Sstevel@tonic-gate 		return (EBUSY);
11967c478bd9Sstevel@tonic-gate 	}
11977c478bd9Sstevel@tonic-gate 
11987c478bd9Sstevel@tonic-gate 	rd_dealloc_resources(rsp);
11997c478bd9Sstevel@tonic-gate 
12007c478bd9Sstevel@tonic-gate 	mutex_exit(&rd_lock);
12017c478bd9Sstevel@tonic-gate 
12027c478bd9Sstevel@tonic-gate 	return (0);
12037c478bd9Sstevel@tonic-gate }
12047c478bd9Sstevel@tonic-gate 
12057c478bd9Sstevel@tonic-gate /*ARGSUSED*/
12067c478bd9Sstevel@tonic-gate static int
12077c478bd9Sstevel@tonic-gate rd_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp, int *rvalp)
12087c478bd9Sstevel@tonic-gate {
12097c478bd9Sstevel@tonic-gate 	minor_t		minor;
12107c478bd9Sstevel@tonic-gate 	int		error;
12117c478bd9Sstevel@tonic-gate 	enum dkio_state	dkstate;
12127c478bd9Sstevel@tonic-gate 	rd_devstate_t	*rsp;
12137c478bd9Sstevel@tonic-gate 
12147c478bd9Sstevel@tonic-gate 	minor = getminor(dev);
12157c478bd9Sstevel@tonic-gate 
12167c478bd9Sstevel@tonic-gate 	/*
12177c478bd9Sstevel@tonic-gate 	 * Ramdisk ioctls only apply to the master device.
12187c478bd9Sstevel@tonic-gate 	 */
12197c478bd9Sstevel@tonic-gate 	if (minor == RD_CTL_MINOR) {
12207c478bd9Sstevel@tonic-gate 		struct rd_ioctl *rip = (struct rd_ioctl *)arg;
12217c478bd9Sstevel@tonic-gate 
12227c478bd9Sstevel@tonic-gate 		/*
12237c478bd9Sstevel@tonic-gate 		 * The query commands only need read-access - i.e., normal
12247c478bd9Sstevel@tonic-gate 		 * users are allowed to do those on the controlling device
12257c478bd9Sstevel@tonic-gate 		 * as long as they can open it read-only.
12267c478bd9Sstevel@tonic-gate 		 */
12277c478bd9Sstevel@tonic-gate 		switch (cmd) {
12287c478bd9Sstevel@tonic-gate 		case RD_CREATE_DISK:
12297c478bd9Sstevel@tonic-gate 			if ((mode & FWRITE) == 0)
12307c478bd9Sstevel@tonic-gate 				return (EPERM);
12317c478bd9Sstevel@tonic-gate 			return (rd_create_disk(dev, rip, mode, rvalp));
12327c478bd9Sstevel@tonic-gate 
12337c478bd9Sstevel@tonic-gate 		case RD_DELETE_DISK:
12347c478bd9Sstevel@tonic-gate 			if ((mode & FWRITE) == 0)
12357c478bd9Sstevel@tonic-gate 				return (EPERM);
12367c478bd9Sstevel@tonic-gate 			return (rd_delete_disk(dev, rip, mode));
12377c478bd9Sstevel@tonic-gate 
12387c478bd9Sstevel@tonic-gate 		default:
12397c478bd9Sstevel@tonic-gate 			return (EINVAL);
12407c478bd9Sstevel@tonic-gate 		}
12417c478bd9Sstevel@tonic-gate 	}
12427c478bd9Sstevel@tonic-gate 
12437c478bd9Sstevel@tonic-gate 	rsp = ddi_get_soft_state(rd_statep, minor);
12447c478bd9Sstevel@tonic-gate 	if (rsp == NULL) {
12457c478bd9Sstevel@tonic-gate 		return (ENXIO);
12467c478bd9Sstevel@tonic-gate 	}
12477c478bd9Sstevel@tonic-gate 
12487c478bd9Sstevel@tonic-gate 	/*
12497c478bd9Sstevel@tonic-gate 	 * These are for faking out utilities like newfs.
12507c478bd9Sstevel@tonic-gate 	 */
12517c478bd9Sstevel@tonic-gate 	switch (cmd) {
12527c478bd9Sstevel@tonic-gate 	case DKIOCGVTOC:
12537c478bd9Sstevel@tonic-gate 		switch (ddi_model_convert_from(mode & FMODELS)) {
12547c478bd9Sstevel@tonic-gate 		case DDI_MODEL_ILP32: {
12557c478bd9Sstevel@tonic-gate 			struct vtoc32 vtoc32;
12567c478bd9Sstevel@tonic-gate 
12577c478bd9Sstevel@tonic-gate 			vtoctovtoc32(rsp->rd_vtoc, vtoc32);
12587c478bd9Sstevel@tonic-gate 			if (ddi_copyout(&vtoc32, (void *)arg,
12597c478bd9Sstevel@tonic-gate 			    sizeof (struct vtoc32), mode))
12607c478bd9Sstevel@tonic-gate 				return (EFAULT);
12617c478bd9Sstevel@tonic-gate 			}
12627c478bd9Sstevel@tonic-gate 			break;
12637c478bd9Sstevel@tonic-gate 
12647c478bd9Sstevel@tonic-gate 		case DDI_MODEL_NONE:
12657c478bd9Sstevel@tonic-gate 			if (ddi_copyout(&rsp->rd_vtoc, (void *)arg,
12667c478bd9Sstevel@tonic-gate 			    sizeof (struct vtoc), mode))
12677c478bd9Sstevel@tonic-gate 				return (EFAULT);
12687c478bd9Sstevel@tonic-gate 			break;
12697c478bd9Sstevel@tonic-gate 		}
12707c478bd9Sstevel@tonic-gate 		return (0);
12717c478bd9Sstevel@tonic-gate 	case DKIOCINFO:
12727c478bd9Sstevel@tonic-gate 		error = ddi_copyout(&rsp->rd_ci, (void *)arg,
12737c478bd9Sstevel@tonic-gate 		    sizeof (struct dk_cinfo), mode);
12747c478bd9Sstevel@tonic-gate 		if (error)
12757c478bd9Sstevel@tonic-gate 			return (EFAULT);
12767c478bd9Sstevel@tonic-gate 		return (0);
12777c478bd9Sstevel@tonic-gate 	case DKIOCG_VIRTGEOM:
12787c478bd9Sstevel@tonic-gate 	case DKIOCG_PHYGEOM:
12797c478bd9Sstevel@tonic-gate 	case DKIOCGGEOM:
12807c478bd9Sstevel@tonic-gate 		error = ddi_copyout(&rsp->rd_dkg, (void *)arg,
12817c478bd9Sstevel@tonic-gate 		    sizeof (struct dk_geom), mode);
12827c478bd9Sstevel@tonic-gate 		if (error)
12837c478bd9Sstevel@tonic-gate 			return (EFAULT);
12847c478bd9Sstevel@tonic-gate 		return (0);
12857c478bd9Sstevel@tonic-gate 	case DKIOCSTATE:
12867c478bd9Sstevel@tonic-gate 		/* the file is always there */
12877c478bd9Sstevel@tonic-gate 		dkstate = DKIO_INSERTED;
12887c478bd9Sstevel@tonic-gate 		error = ddi_copyout(&dkstate, (void *)arg,
12897c478bd9Sstevel@tonic-gate 		    sizeof (enum dkio_state), mode);
12907c478bd9Sstevel@tonic-gate 		if (error)
12917c478bd9Sstevel@tonic-gate 			return (EFAULT);
12927c478bd9Sstevel@tonic-gate 		return (0);
12937c478bd9Sstevel@tonic-gate 	default:
12947c478bd9Sstevel@tonic-gate 		return (ENOTTY);
12957c478bd9Sstevel@tonic-gate 	}
12967c478bd9Sstevel@tonic-gate }
12977c478bd9Sstevel@tonic-gate 
12987c478bd9Sstevel@tonic-gate 
12997c478bd9Sstevel@tonic-gate static struct cb_ops rd_cb_ops = {
13007c478bd9Sstevel@tonic-gate 	rd_open,
13017c478bd9Sstevel@tonic-gate 	rd_close,
13027c478bd9Sstevel@tonic-gate 	rd_strategy,
13037c478bd9Sstevel@tonic-gate 	nodev,
13047c478bd9Sstevel@tonic-gate 	nodev,		/* dump */
13057c478bd9Sstevel@tonic-gate 	rd_read,
13067c478bd9Sstevel@tonic-gate 	rd_write,
13077c478bd9Sstevel@tonic-gate 	rd_ioctl,
13087c478bd9Sstevel@tonic-gate 	nodev,		/* devmap */
13097c478bd9Sstevel@tonic-gate 	nodev,		/* mmap */
13107c478bd9Sstevel@tonic-gate 	nodev,		/* segmap */
13117c478bd9Sstevel@tonic-gate 	nochpoll,	/* poll */
13127c478bd9Sstevel@tonic-gate 	ddi_prop_op,
13137c478bd9Sstevel@tonic-gate 	NULL,
13147c478bd9Sstevel@tonic-gate 	D_NEW | D_MP
13157c478bd9Sstevel@tonic-gate };
13167c478bd9Sstevel@tonic-gate 
13177c478bd9Sstevel@tonic-gate static struct dev_ops rd_ops = {
13187c478bd9Sstevel@tonic-gate 	DEVO_REV,
13197c478bd9Sstevel@tonic-gate 	0,
13207c478bd9Sstevel@tonic-gate 	rd_getinfo,
13217c478bd9Sstevel@tonic-gate 	nulldev,	/* identify */
13227c478bd9Sstevel@tonic-gate 	nulldev,	/* probe */
13237c478bd9Sstevel@tonic-gate 	rd_attach,
13247c478bd9Sstevel@tonic-gate 	rd_detach,
13257c478bd9Sstevel@tonic-gate 	nodev,		/* reset */
13267c478bd9Sstevel@tonic-gate 	&rd_cb_ops,
13277c478bd9Sstevel@tonic-gate 	(struct bus_ops *)0
13287c478bd9Sstevel@tonic-gate };
13297c478bd9Sstevel@tonic-gate 
13307c478bd9Sstevel@tonic-gate 
13317c478bd9Sstevel@tonic-gate extern struct mod_ops mod_driverops;
13327c478bd9Sstevel@tonic-gate 
13337c478bd9Sstevel@tonic-gate static struct modldrv modldrv = {
13347c478bd9Sstevel@tonic-gate 	&mod_driverops,
13357c478bd9Sstevel@tonic-gate 	"ramdisk driver v%I%",
13367c478bd9Sstevel@tonic-gate 	&rd_ops
13377c478bd9Sstevel@tonic-gate };
13387c478bd9Sstevel@tonic-gate 
13397c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
13407c478bd9Sstevel@tonic-gate 	MODREV_1,
13417c478bd9Sstevel@tonic-gate 	&modldrv,
13427c478bd9Sstevel@tonic-gate 	0
13437c478bd9Sstevel@tonic-gate };
13447c478bd9Sstevel@tonic-gate 
13457c478bd9Sstevel@tonic-gate int
13467c478bd9Sstevel@tonic-gate _init(void)
13477c478bd9Sstevel@tonic-gate {
13487c478bd9Sstevel@tonic-gate 	int e;
13497c478bd9Sstevel@tonic-gate 
13507c478bd9Sstevel@tonic-gate 	if ((e = ddi_soft_state_init(&rd_statep,
13517c478bd9Sstevel@tonic-gate 	    sizeof (rd_devstate_t), 0)) != 0) {
13527c478bd9Sstevel@tonic-gate 		return (e);
13537c478bd9Sstevel@tonic-gate 	}
13547c478bd9Sstevel@tonic-gate 
13557c478bd9Sstevel@tonic-gate 	mutex_init(&rd_lock, NULL, MUTEX_DRIVER, NULL);
13567c478bd9Sstevel@tonic-gate 
13577c478bd9Sstevel@tonic-gate 	if ((e = mod_install(&modlinkage)) != 0)  {
13587c478bd9Sstevel@tonic-gate 		mutex_destroy(&rd_lock);
13597c478bd9Sstevel@tonic-gate 		ddi_soft_state_fini(&rd_statep);
13607c478bd9Sstevel@tonic-gate 	}
13617c478bd9Sstevel@tonic-gate 
13627c478bd9Sstevel@tonic-gate 	return (e);
13637c478bd9Sstevel@tonic-gate }
13647c478bd9Sstevel@tonic-gate 
13657c478bd9Sstevel@tonic-gate int
13667c478bd9Sstevel@tonic-gate _fini(void)
13677c478bd9Sstevel@tonic-gate {
13687c478bd9Sstevel@tonic-gate 	int e;
13697c478bd9Sstevel@tonic-gate 
13707c478bd9Sstevel@tonic-gate 	if ((e = mod_remove(&modlinkage)) != 0)  {
13717c478bd9Sstevel@tonic-gate 		return (e);
13727c478bd9Sstevel@tonic-gate 	}
13737c478bd9Sstevel@tonic-gate 
13747c478bd9Sstevel@tonic-gate 	ddi_soft_state_fini(&rd_statep);
13757c478bd9Sstevel@tonic-gate 	mutex_destroy(&rd_lock);
13767c478bd9Sstevel@tonic-gate 
13777c478bd9Sstevel@tonic-gate 	return (e);
13787c478bd9Sstevel@tonic-gate }
13797c478bd9Sstevel@tonic-gate 
13807c478bd9Sstevel@tonic-gate int
13817c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
13827c478bd9Sstevel@tonic-gate {
13837c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
13847c478bd9Sstevel@tonic-gate }
1385