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