xref: /titanic_44/usr/src/uts/common/io/ramdisk.c (revision 986fd29a0dc13f7608ef7f508f6e700bd7bc2720)
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 /*
22e4c8a2faSblakej  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #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 	/*
291e4c8a2faSblakej 	 * Since availrmem_initial is a long, this won't overflow.
2927c478bd9Sstevel@tonic-gate 	 */
293e4c8a2faSblakej 	rd_max_physmem = (availrmem_initial * rd_percent_physmem) / 100;
2947c478bd9Sstevel@tonic-gate }
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate /*
297e4c8a2faSblakej  * Allocate enough physical pages to hold "npages" pages.  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 page_t **
3027c478bd9Sstevel@tonic-gate rd_phys_alloc(pgcnt_t npages)
3037c478bd9Sstevel@tonic-gate {
3047c478bd9Sstevel@tonic-gate 	page_t		*pp, **ppa;
305e4c8a2faSblakej 	spgcnt_t	i;
306e4c8a2faSblakej 	size_t		ppalen;
3077c478bd9Sstevel@tonic-gate 	struct seg	kseg;
308e4c8a2faSblakej 	caddr_t		addr;		/* For coloring */
3097c478bd9Sstevel@tonic-gate 
310e4c8a2faSblakej 	if (rd_tot_physmem + npages > rd_max_physmem)
311e4c8a2faSblakej 		return (NULL);
312e4c8a2faSblakej 
313e4c8a2faSblakej 	if (!page_resv(npages, KM_NOSLEEP))
314e4c8a2faSblakej 		return (NULL);
315e4c8a2faSblakej 
316e4c8a2faSblakej 	if (!page_create_wait(npages, 0)) {
317e4c8a2faSblakej 		page_unresv(npages);
3187c478bd9Sstevel@tonic-gate 		return (NULL);
3197c478bd9Sstevel@tonic-gate 	}
3207c478bd9Sstevel@tonic-gate 
321e4c8a2faSblakej 	ppalen = npages * sizeof (struct page_t *);
322e4c8a2faSblakej 	ppa = kmem_zalloc(ppalen, KM_NOSLEEP);
323e4c8a2faSblakej 	if (ppa == NULL) {
324e4c8a2faSblakej 		page_create_putback(npages);
325e4c8a2faSblakej 		page_unresv(npages);
326e4c8a2faSblakej 		return (NULL);
3277c478bd9Sstevel@tonic-gate 	}
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate 	kseg.s_as = &kas;
330e4c8a2faSblakej 	for (i = 0, addr = NULL; i < npages; ++i, addr += PAGESIZE) {
331e4c8a2faSblakej 		pp = page_get_freelist(&kvp, 0, &kseg, addr, PAGESIZE, 0, NULL);
332e4c8a2faSblakej 		if (pp == NULL) {
333e4c8a2faSblakej 			pp = page_get_cachelist(&kvp, 0, &kseg, addr, 0, NULL);
334e4c8a2faSblakej 			if (pp == NULL)
3357c478bd9Sstevel@tonic-gate 				goto out;
336e4c8a2faSblakej 			if (!PP_ISAGED(pp))
3377c478bd9Sstevel@tonic-gate 				page_hashout(pp, NULL);
3387c478bd9Sstevel@tonic-gate 		}
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 		PP_CLRFREE(pp);
3417c478bd9Sstevel@tonic-gate 		PP_CLRAGED(pp);
3427c478bd9Sstevel@tonic-gate 		ppa[i] = pp;
3437c478bd9Sstevel@tonic-gate 	}
344e4c8a2faSblakej 
345e4c8a2faSblakej 	for (i = 0; i < npages; i++)
346e4c8a2faSblakej 		page_downgrade(ppa[i]);
3477c478bd9Sstevel@tonic-gate 	rd_tot_physmem += npages;
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 	return (ppa);
350e4c8a2faSblakej 
3517c478bd9Sstevel@tonic-gate out:
352e4c8a2faSblakej 	ASSERT(i < npages);
353e4c8a2faSblakej 	page_create_putback(npages - i);
354e4c8a2faSblakej 	while (--i >= 0)
3557c478bd9Sstevel@tonic-gate 		page_free(ppa[i], 0);
3567c478bd9Sstevel@tonic-gate 	kmem_free(ppa, ppalen);
3577c478bd9Sstevel@tonic-gate 	page_unresv(npages);
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate 	return (NULL);
3607c478bd9Sstevel@tonic-gate }
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate /*
3637c478bd9Sstevel@tonic-gate  * Free physical pages previously allocated via rd_phys_alloc(); note that
3647c478bd9Sstevel@tonic-gate  * this function may block as it has to wait until it can exclusively lock
3657c478bd9Sstevel@tonic-gate  * all the pages first.
3667c478bd9Sstevel@tonic-gate  */
3677c478bd9Sstevel@tonic-gate static void
3687c478bd9Sstevel@tonic-gate rd_phys_free(page_t **ppa, pgcnt_t npages)
3697c478bd9Sstevel@tonic-gate {
3707c478bd9Sstevel@tonic-gate 	pgcnt_t	i;
3717c478bd9Sstevel@tonic-gate 	size_t	ppalen = npages * sizeof (struct page_t *);
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 	for (i = 0; i < npages; ++i) {
3747c478bd9Sstevel@tonic-gate 		if (! page_tryupgrade(ppa[i])) {
3757c478bd9Sstevel@tonic-gate 			page_unlock(ppa[i]);
3767c478bd9Sstevel@tonic-gate 			while (! page_lock(ppa[i], SE_EXCL, NULL, P_RECLAIM))
3777c478bd9Sstevel@tonic-gate 				;
3787c478bd9Sstevel@tonic-gate 		}
3797c478bd9Sstevel@tonic-gate 		page_free(ppa[i], 0);
3807c478bd9Sstevel@tonic-gate 	}
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 	kmem_free(ppa, ppalen);
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 	page_unresv(npages);
3857c478bd9Sstevel@tonic-gate 	rd_tot_physmem -= npages;
3867c478bd9Sstevel@tonic-gate }
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate /*
3897c478bd9Sstevel@tonic-gate  * Remove a window mapping (if present).
3907c478bd9Sstevel@tonic-gate  */
3917c478bd9Sstevel@tonic-gate static void
3927c478bd9Sstevel@tonic-gate rd_unmap_window(rd_devstate_t *rsp)
3937c478bd9Sstevel@tonic-gate {
394*986fd29aSsetje 	ASSERT(rsp->rd_window_obp == 0);
3957c478bd9Sstevel@tonic-gate 	if (rsp->rd_window_base != RD_WINDOW_NOT_MAPPED) {
3967c478bd9Sstevel@tonic-gate 		hat_unload(kas.a_hat, rsp->rd_window_virt, rsp->rd_window_size,
3977c478bd9Sstevel@tonic-gate 		    HAT_UNLOAD_UNLOCK);
3987c478bd9Sstevel@tonic-gate 	}
3997c478bd9Sstevel@tonic-gate }
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate /*
4027c478bd9Sstevel@tonic-gate  * Map a portion of the ramdisk into the virtual window.
4037c478bd9Sstevel@tonic-gate  */
4047c478bd9Sstevel@tonic-gate static void
4057c478bd9Sstevel@tonic-gate rd_map_window(rd_devstate_t *rsp, off_t offset)
4067c478bd9Sstevel@tonic-gate {
4077c478bd9Sstevel@tonic-gate 	pgcnt_t	offpgs = btop(offset);
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate 	if (rsp->rd_window_base != RD_WINDOW_NOT_MAPPED) {
4107c478bd9Sstevel@tonic-gate 		/*
4117c478bd9Sstevel@tonic-gate 		 * Already mapped; is offset within our window?
4127c478bd9Sstevel@tonic-gate 		 */
4137c478bd9Sstevel@tonic-gate 		if (offset >= rsp->rd_window_base &&
4147c478bd9Sstevel@tonic-gate 		    offset < rsp->rd_window_base + rsp->rd_window_size) {
4157c478bd9Sstevel@tonic-gate 			return;
4167c478bd9Sstevel@tonic-gate 		}
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 		/*
4197c478bd9Sstevel@tonic-gate 		 * No, we need to re-map; toss the old mapping.
4207c478bd9Sstevel@tonic-gate 		 */
4217c478bd9Sstevel@tonic-gate 		rd_unmap_window(rsp);
4227c478bd9Sstevel@tonic-gate 	}
4237c478bd9Sstevel@tonic-gate 	rsp->rd_window_base = ptob(offpgs);
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate 	/*
4267c478bd9Sstevel@tonic-gate 	 * Different algorithms depending on whether this is a real
4277c478bd9Sstevel@tonic-gate 	 * OBP-created ramdisk, or a pseudo ramdisk.
4287c478bd9Sstevel@tonic-gate 	 */
4297c478bd9Sstevel@tonic-gate 	if (rsp->rd_dip == rd_dip) {
4307c478bd9Sstevel@tonic-gate 		pgcnt_t	pi, lastpi;
4317c478bd9Sstevel@tonic-gate 		caddr_t	vaddr;
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate 		/*
4347c478bd9Sstevel@tonic-gate 		 * Find the range of pages which should be mapped.
4357c478bd9Sstevel@tonic-gate 		 */
4367c478bd9Sstevel@tonic-gate 		pi = offpgs;
4377c478bd9Sstevel@tonic-gate 		lastpi = pi + btopr(rsp->rd_window_size);
4387c478bd9Sstevel@tonic-gate 		if (lastpi > rsp->rd_npages) {
4397c478bd9Sstevel@tonic-gate 			lastpi = rsp->rd_npages;
4407c478bd9Sstevel@tonic-gate 		}
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate 		/*
4437c478bd9Sstevel@tonic-gate 		 * Load the mapping.
4447c478bd9Sstevel@tonic-gate 		 */
4457c478bd9Sstevel@tonic-gate 		vaddr = rsp->rd_window_virt;
4467c478bd9Sstevel@tonic-gate 		for (; pi < lastpi; ++pi) {
4477c478bd9Sstevel@tonic-gate 			hat_memload(kas.a_hat, vaddr, rsp->rd_ppa[pi],
4487c478bd9Sstevel@tonic-gate 			    (PROT_READ | PROT_WRITE) | HAT_NOSYNC,
4497c478bd9Sstevel@tonic-gate 			    HAT_LOAD_LOCK);
4507c478bd9Sstevel@tonic-gate 			vaddr += ptob(1);
4517c478bd9Sstevel@tonic-gate 		}
4527c478bd9Sstevel@tonic-gate 	} else {
4537c478bd9Sstevel@tonic-gate 		uint_t	i;
4547c478bd9Sstevel@tonic-gate 		pfn_t	pfn;
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate 		/*
4577c478bd9Sstevel@tonic-gate 		 * Real OBP-created ramdisk: locate the physical range which
4587c478bd9Sstevel@tonic-gate 		 * contains this offset.
4597c478bd9Sstevel@tonic-gate 		 */
4607c478bd9Sstevel@tonic-gate 		for (i = 0; i < rsp->rd_nexisting; ++i) {
4617c478bd9Sstevel@tonic-gate 			if (offset < rsp->rd_existing[i].size) {
4627c478bd9Sstevel@tonic-gate 				break;
4637c478bd9Sstevel@tonic-gate 			}
4647c478bd9Sstevel@tonic-gate 			offset -= rsp->rd_existing[i].size;
4657c478bd9Sstevel@tonic-gate 		}
4667c478bd9Sstevel@tonic-gate 		ASSERT(i < rsp->rd_nexisting);
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate 		/*
4697c478bd9Sstevel@tonic-gate 		 * Load the mapping.
4707c478bd9Sstevel@tonic-gate 		 */
4717c478bd9Sstevel@tonic-gate 		pfn = btop(rsp->rd_existing[i].phys + offset);
4727c478bd9Sstevel@tonic-gate 		hat_devload(kas.a_hat, rsp->rd_window_virt, rsp->rd_window_size,
4737c478bd9Sstevel@tonic-gate 		    pfn, (PROT_READ | PROT_WRITE),
4747c478bd9Sstevel@tonic-gate 		    HAT_LOAD_NOCONSIST | HAT_LOAD_LOCK);
4757c478bd9Sstevel@tonic-gate 	}
4767c478bd9Sstevel@tonic-gate }
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate /*
4797c478bd9Sstevel@tonic-gate  * Fakes up a disk geometry, and one big partition, based on the size
4807c478bd9Sstevel@tonic-gate  * of the file. This is needed because we allow newfs'ing the device,
4817c478bd9Sstevel@tonic-gate  * and newfs will do several disk ioctls to figure out the geometry and
4827c478bd9Sstevel@tonic-gate  * partition information. It uses that information to determine the parameters
48363360950Smp204432  * to pass to mkfs. Geometry is pretty much irrelevant these days, but we
4847c478bd9Sstevel@tonic-gate  * have to support it.
4857c478bd9Sstevel@tonic-gate  *
4867c478bd9Sstevel@tonic-gate  * Stolen from lofi.c - should maybe split out common code sometime.
4877c478bd9Sstevel@tonic-gate  */
4887c478bd9Sstevel@tonic-gate static void
4897c478bd9Sstevel@tonic-gate rd_fake_disk_geometry(rd_devstate_t *rsp)
4907c478bd9Sstevel@tonic-gate {
4917c478bd9Sstevel@tonic-gate 	/* dk_geom - see dkio(7I) */
4927c478bd9Sstevel@tonic-gate 	/*
4937c478bd9Sstevel@tonic-gate 	 * dkg_ncyl _could_ be set to one here (one big cylinder with gobs
4947c478bd9Sstevel@tonic-gate 	 * of sectors), but that breaks programs like fdisk which want to
4957c478bd9Sstevel@tonic-gate 	 * partition a disk by cylinder. With one cylinder, you can't create
4967c478bd9Sstevel@tonic-gate 	 * an fdisk partition and put pcfs on it for testing (hard to pick
4977c478bd9Sstevel@tonic-gate 	 * a number between one and one).
4987c478bd9Sstevel@tonic-gate 	 *
4997c478bd9Sstevel@tonic-gate 	 * The cheezy floppy test is an attempt to not have too few cylinders
5007c478bd9Sstevel@tonic-gate 	 * for a small file, or so many on a big file that you waste space
5017c478bd9Sstevel@tonic-gate 	 * for backup superblocks or cylinder group structures.
5027c478bd9Sstevel@tonic-gate 	 */
5037c478bd9Sstevel@tonic-gate 	if (rsp->rd_size < (2 * 1024 * 1024)) /* floppy? */
5047c478bd9Sstevel@tonic-gate 		rsp->rd_dkg.dkg_ncyl = rsp->rd_size / (100 * 1024);
5057c478bd9Sstevel@tonic-gate 	else
5067c478bd9Sstevel@tonic-gate 		rsp->rd_dkg.dkg_ncyl = rsp->rd_size / (300 * 1024);
5077c478bd9Sstevel@tonic-gate 	/* in case file file is < 100k */
5087c478bd9Sstevel@tonic-gate 	if (rsp->rd_dkg.dkg_ncyl == 0)
5097c478bd9Sstevel@tonic-gate 		rsp->rd_dkg.dkg_ncyl = 1;
5107c478bd9Sstevel@tonic-gate 	rsp->rd_dkg.dkg_acyl = 0;
5117c478bd9Sstevel@tonic-gate 	rsp->rd_dkg.dkg_bcyl = 0;
5127c478bd9Sstevel@tonic-gate 	rsp->rd_dkg.dkg_nhead = 1;
5137c478bd9Sstevel@tonic-gate 	rsp->rd_dkg.dkg_obs1 = 0;
5147c478bd9Sstevel@tonic-gate 	rsp->rd_dkg.dkg_intrlv = 0;
5157c478bd9Sstevel@tonic-gate 	rsp->rd_dkg.dkg_obs2 = 0;
5167c478bd9Sstevel@tonic-gate 	rsp->rd_dkg.dkg_obs3 = 0;
5177c478bd9Sstevel@tonic-gate 	rsp->rd_dkg.dkg_apc = 0;
5187c478bd9Sstevel@tonic-gate 	rsp->rd_dkg.dkg_rpm = 7200;
5197c478bd9Sstevel@tonic-gate 	rsp->rd_dkg.dkg_pcyl = rsp->rd_dkg.dkg_ncyl + rsp->rd_dkg.dkg_acyl;
5207c478bd9Sstevel@tonic-gate 	rsp->rd_dkg.dkg_nsect = rsp->rd_size /
5217c478bd9Sstevel@tonic-gate 	    (DEV_BSIZE * rsp->rd_dkg.dkg_ncyl);
5227c478bd9Sstevel@tonic-gate 	rsp->rd_dkg.dkg_write_reinstruct = 0;
5237c478bd9Sstevel@tonic-gate 	rsp->rd_dkg.dkg_read_reinstruct = 0;
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 	/* vtoc - see dkio(7I) */
5267c478bd9Sstevel@tonic-gate 	bzero(&rsp->rd_vtoc, sizeof (struct vtoc));
5277c478bd9Sstevel@tonic-gate 	rsp->rd_vtoc.v_sanity = VTOC_SANE;
5287c478bd9Sstevel@tonic-gate 	rsp->rd_vtoc.v_version = V_VERSION;
5297c478bd9Sstevel@tonic-gate 	bcopy(RD_DRIVER_NAME, rsp->rd_vtoc.v_volume, 7);
5307c478bd9Sstevel@tonic-gate 	rsp->rd_vtoc.v_sectorsz = DEV_BSIZE;
5317c478bd9Sstevel@tonic-gate 	rsp->rd_vtoc.v_nparts = 1;
5327c478bd9Sstevel@tonic-gate 	rsp->rd_vtoc.v_part[0].p_tag = V_UNASSIGNED;
5337c478bd9Sstevel@tonic-gate 	rsp->rd_vtoc.v_part[0].p_flag = V_UNMNT;
5347c478bd9Sstevel@tonic-gate 	rsp->rd_vtoc.v_part[0].p_start = (daddr_t)0;
5357c478bd9Sstevel@tonic-gate 	/*
5367c478bd9Sstevel@tonic-gate 	 * The partition size cannot just be the number of sectors, because
5377c478bd9Sstevel@tonic-gate 	 * that might not end on a cylinder boundary. And if that's the case,
5387c478bd9Sstevel@tonic-gate 	 * newfs/mkfs will print a scary warning. So just figure the size
5397c478bd9Sstevel@tonic-gate 	 * based on the number of cylinders and sectors/cylinder.
5407c478bd9Sstevel@tonic-gate 	 */
5417c478bd9Sstevel@tonic-gate 	rsp->rd_vtoc.v_part[0].p_size = rsp->rd_dkg.dkg_pcyl *
5427c478bd9Sstevel@tonic-gate 	    rsp->rd_dkg.dkg_nsect * rsp->rd_dkg.dkg_nhead;
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate 	/* dk_cinfo - see dkio(7I) */
5457c478bd9Sstevel@tonic-gate 	bzero(&rsp->rd_ci, sizeof (struct dk_cinfo));
5467c478bd9Sstevel@tonic-gate 	(void) strcpy(rsp->rd_ci.dki_cname, RD_DRIVER_NAME);
5477c478bd9Sstevel@tonic-gate 	rsp->rd_ci.dki_ctype = DKC_MD;
5487c478bd9Sstevel@tonic-gate 	rsp->rd_ci.dki_flags = 0;
5497c478bd9Sstevel@tonic-gate 	rsp->rd_ci.dki_cnum = 0;
5507c478bd9Sstevel@tonic-gate 	rsp->rd_ci.dki_addr = 0;
5517c478bd9Sstevel@tonic-gate 	rsp->rd_ci.dki_space = 0;
5527c478bd9Sstevel@tonic-gate 	rsp->rd_ci.dki_prio = 0;
5537c478bd9Sstevel@tonic-gate 	rsp->rd_ci.dki_vec = 0;
5547c478bd9Sstevel@tonic-gate 	(void) strcpy(rsp->rd_ci.dki_dname, RD_DRIVER_NAME);
5557c478bd9Sstevel@tonic-gate 	rsp->rd_ci.dki_unit = 0;
5567c478bd9Sstevel@tonic-gate 	rsp->rd_ci.dki_slave = 0;
5577c478bd9Sstevel@tonic-gate 	rsp->rd_ci.dki_partition = 0;
5587c478bd9Sstevel@tonic-gate 	/*
5597c478bd9Sstevel@tonic-gate 	 * newfs uses this to set maxcontig. Must not be < 16, or it
5607c478bd9Sstevel@tonic-gate 	 * will be 0 when newfs multiplies it by DEV_BSIZE and divides
5617c478bd9Sstevel@tonic-gate 	 * it by the block size. Then tunefs doesn't work because
5627c478bd9Sstevel@tonic-gate 	 * maxcontig is 0.
5637c478bd9Sstevel@tonic-gate 	 */
5647c478bd9Sstevel@tonic-gate 	rsp->rd_ci.dki_maxtransfer = 16;
5657c478bd9Sstevel@tonic-gate }
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate /*
5687c478bd9Sstevel@tonic-gate  * Deallocate resources (virtual and physical, device nodes, structures)
5697c478bd9Sstevel@tonic-gate  * from a ramdisk.
5707c478bd9Sstevel@tonic-gate  */
5717c478bd9Sstevel@tonic-gate static void
5727c478bd9Sstevel@tonic-gate rd_dealloc_resources(rd_devstate_t *rsp)
5737c478bd9Sstevel@tonic-gate {
5747c478bd9Sstevel@tonic-gate 	dev_info_t	*dip = rsp->rd_dip;
5757c478bd9Sstevel@tonic-gate 	char		namebuf[RD_NAME_LEN + 5];
5767c478bd9Sstevel@tonic-gate 	dev_t		fulldev;
5777c478bd9Sstevel@tonic-gate 
578*986fd29aSsetje 	if (rsp->rd_window_obp == 0 && rsp->rd_window_virt != NULL) {
5797c478bd9Sstevel@tonic-gate 		if (rsp->rd_window_base != RD_WINDOW_NOT_MAPPED) {
5807c478bd9Sstevel@tonic-gate 			rd_unmap_window(rsp);
5817c478bd9Sstevel@tonic-gate 		}
5827c478bd9Sstevel@tonic-gate 		vmem_free(heap_arena, rsp->rd_window_virt, rsp->rd_window_size);
5837c478bd9Sstevel@tonic-gate 	}
5847c478bd9Sstevel@tonic-gate 	mutex_destroy(&rsp->rd_device_lock);
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate 	if (rsp->rd_existing) {
5877c478bd9Sstevel@tonic-gate 		ddi_prop_free(rsp->rd_existing);
5887c478bd9Sstevel@tonic-gate 	}
5897c478bd9Sstevel@tonic-gate 	if (rsp->rd_ppa != NULL) {
5907c478bd9Sstevel@tonic-gate 		rd_phys_free(rsp->rd_ppa, rsp->rd_npages);
5917c478bd9Sstevel@tonic-gate 	}
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate 	/*
5947c478bd9Sstevel@tonic-gate 	 * Remove the block and raw device nodes.
5957c478bd9Sstevel@tonic-gate 	 */
5967c478bd9Sstevel@tonic-gate 	if (dip == rd_dip) {
5977c478bd9Sstevel@tonic-gate 		(void) snprintf(namebuf, sizeof (namebuf), "%s",
5987c478bd9Sstevel@tonic-gate 		    rsp->rd_name);
5997c478bd9Sstevel@tonic-gate 		ddi_remove_minor_node(dip, namebuf);
6007c478bd9Sstevel@tonic-gate 		(void) snprintf(namebuf, sizeof (namebuf), "%s,raw",
6017c478bd9Sstevel@tonic-gate 		    rsp->rd_name);
6027c478bd9Sstevel@tonic-gate 		ddi_remove_minor_node(dip, namebuf);
6037c478bd9Sstevel@tonic-gate 	} else {
6047c478bd9Sstevel@tonic-gate 		ddi_remove_minor_node(dip, "a");
6057c478bd9Sstevel@tonic-gate 		ddi_remove_minor_node(dip, "a,raw");
6067c478bd9Sstevel@tonic-gate 	}
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate 	/*
6097c478bd9Sstevel@tonic-gate 	 * Remove the "Size" and "Nblocks" properties.
6107c478bd9Sstevel@tonic-gate 	 */
6117c478bd9Sstevel@tonic-gate 	fulldev = makedevice(ddi_driver_major(dip), rsp->rd_minor);
6127c478bd9Sstevel@tonic-gate 	(void) ddi_prop_remove(fulldev, dip, SIZE_PROP_NAME);
6137c478bd9Sstevel@tonic-gate 	(void) ddi_prop_remove(fulldev, dip, NBLOCKS_PROP_NAME);
6147c478bd9Sstevel@tonic-gate 
6157c478bd9Sstevel@tonic-gate 	if (rsp->rd_kstat) {
6167c478bd9Sstevel@tonic-gate 		kstat_delete(rsp->rd_kstat);
6177c478bd9Sstevel@tonic-gate 		mutex_destroy(&rsp->rd_kstat_lock);
6187c478bd9Sstevel@tonic-gate 	}
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate 	ddi_soft_state_free(rd_statep, rsp->rd_minor);
6217c478bd9Sstevel@tonic-gate }
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate /*
6247c478bd9Sstevel@tonic-gate  * Allocate resources (virtual and physical, device nodes, structures)
6257c478bd9Sstevel@tonic-gate  * to a ramdisk.
6267c478bd9Sstevel@tonic-gate  */
6277c478bd9Sstevel@tonic-gate static rd_devstate_t *
628*986fd29aSsetje rd_alloc_resources(char *name, uint_t addr, size_t size, dev_info_t *dip)
6297c478bd9Sstevel@tonic-gate {
6307c478bd9Sstevel@tonic-gate 	minor_t		minor;
6317c478bd9Sstevel@tonic-gate 	rd_devstate_t	*rsp;
6327c478bd9Sstevel@tonic-gate 	char		namebuf[RD_NAME_LEN + 5];
6337c478bd9Sstevel@tonic-gate 	dev_t		fulldev;
6347c478bd9Sstevel@tonic-gate 	int64_t		Nblocks_prop_val;
6357c478bd9Sstevel@tonic-gate 	int64_t		Size_prop_val;
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate 	minor = rd_find_free_minor();
6387c478bd9Sstevel@tonic-gate 	if (ddi_soft_state_zalloc(rd_statep, minor) == DDI_FAILURE) {
6397c478bd9Sstevel@tonic-gate 		return (NULL);
6407c478bd9Sstevel@tonic-gate 	}
6417c478bd9Sstevel@tonic-gate 	rsp = ddi_get_soft_state(rd_statep, minor);
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate 	(void) strcpy(rsp->rd_name, name);
6447c478bd9Sstevel@tonic-gate 	rsp->rd_dip = dip;
6457c478bd9Sstevel@tonic-gate 	rsp->rd_minor = minor;
6467c478bd9Sstevel@tonic-gate 	rsp->rd_size = size;
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate 	/*
6497c478bd9Sstevel@tonic-gate 	 * Allocate virtual window onto ramdisk.
6507c478bd9Sstevel@tonic-gate 	 */
6517c478bd9Sstevel@tonic-gate 	mutex_init(&rsp->rd_device_lock, NULL, MUTEX_DRIVER, NULL);
652*986fd29aSsetje 	if (addr == 0) {
653*986fd29aSsetje 		rsp->rd_window_obp = 0;
6547c478bd9Sstevel@tonic-gate 		rsp->rd_window_base = RD_WINDOW_NOT_MAPPED;
6557c478bd9Sstevel@tonic-gate 		rsp->rd_window_size = PAGESIZE;
6567c478bd9Sstevel@tonic-gate 		rsp->rd_window_virt = vmem_alloc(heap_arena,
6577c478bd9Sstevel@tonic-gate 		    rsp->rd_window_size, VM_SLEEP);
6587c478bd9Sstevel@tonic-gate 		if (rsp->rd_window_virt == NULL) {
6597c478bd9Sstevel@tonic-gate 			goto create_failed;
6607c478bd9Sstevel@tonic-gate 		}
661*986fd29aSsetje 	} else {
662*986fd29aSsetje 		rsp->rd_window_obp = 1;
663*986fd29aSsetje 		rsp->rd_window_base = 0;
664*986fd29aSsetje 		rsp->rd_window_size = size;
665*986fd29aSsetje 		rsp->rd_window_virt = (caddr_t)((ulong_t)addr);
666*986fd29aSsetje 	}
6677c478bd9Sstevel@tonic-gate 
6687c478bd9Sstevel@tonic-gate 	/*
6697c478bd9Sstevel@tonic-gate 	 * Allocate physical memory for non-OBP ramdisks.
6707c478bd9Sstevel@tonic-gate 	 * Create pseudo block and raw device nodes.
6717c478bd9Sstevel@tonic-gate 	 */
6727c478bd9Sstevel@tonic-gate 	if (dip == rd_dip) {
6737c478bd9Sstevel@tonic-gate 		rsp->rd_npages = btopr(size);
6747c478bd9Sstevel@tonic-gate 		rsp->rd_ppa = rd_phys_alloc(rsp->rd_npages);
6757c478bd9Sstevel@tonic-gate 		if (rsp->rd_ppa == NULL) {
6767c478bd9Sstevel@tonic-gate 			goto create_failed;
6777c478bd9Sstevel@tonic-gate 		}
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate 		/*
6807c478bd9Sstevel@tonic-gate 		 * For non-OBP ramdisks the device nodes are:
6817c478bd9Sstevel@tonic-gate 		 *
6827c478bd9Sstevel@tonic-gate 		 *	/devices/pseudo/ramdisk@0:<diskname>
6837c478bd9Sstevel@tonic-gate 		 *	/devices/pseudo/ramdisk@0:<diskname>,raw
6847c478bd9Sstevel@tonic-gate 		 */
6857c478bd9Sstevel@tonic-gate 		(void) snprintf(namebuf, sizeof (namebuf), "%s",
6867c478bd9Sstevel@tonic-gate 		    rsp->rd_name);
6877c478bd9Sstevel@tonic-gate 		if (ddi_create_minor_node(dip, namebuf, S_IFBLK, minor,
6887c478bd9Sstevel@tonic-gate 		    DDI_PSEUDO, 0) == DDI_FAILURE) {
6897c478bd9Sstevel@tonic-gate 			goto create_failed;
6907c478bd9Sstevel@tonic-gate 		}
6917c478bd9Sstevel@tonic-gate 		(void) snprintf(namebuf, sizeof (namebuf), "%s,raw",
6927c478bd9Sstevel@tonic-gate 		    rsp->rd_name);
6937c478bd9Sstevel@tonic-gate 		if (ddi_create_minor_node(dip, namebuf, S_IFCHR, minor,
6947c478bd9Sstevel@tonic-gate 		    DDI_PSEUDO, 0) == DDI_FAILURE) {
6957c478bd9Sstevel@tonic-gate 			goto create_failed;
6967c478bd9Sstevel@tonic-gate 		}
6977c478bd9Sstevel@tonic-gate 	} else {
6987c478bd9Sstevel@tonic-gate 		/*
6997c478bd9Sstevel@tonic-gate 		 * For OBP-created ramdisks the device nodes are:
7007c478bd9Sstevel@tonic-gate 		 *
7017c478bd9Sstevel@tonic-gate 		 *	/devices/ramdisk-<diskname>:a
7027c478bd9Sstevel@tonic-gate 		 *	/devices/ramdisk-<diskname>:a,raw
7037c478bd9Sstevel@tonic-gate 		 */
7047c478bd9Sstevel@tonic-gate 		if (ddi_create_minor_node(dip, "a", S_IFBLK, minor,
7057c478bd9Sstevel@tonic-gate 		    DDI_PSEUDO, 0) == DDI_FAILURE) {
7067c478bd9Sstevel@tonic-gate 			goto create_failed;
7077c478bd9Sstevel@tonic-gate 		}
7087c478bd9Sstevel@tonic-gate 		if (ddi_create_minor_node(dip, "a,raw", S_IFCHR, minor,
7097c478bd9Sstevel@tonic-gate 		    DDI_PSEUDO, 0) == DDI_FAILURE) {
7107c478bd9Sstevel@tonic-gate 			goto create_failed;
7117c478bd9Sstevel@tonic-gate 		}
7127c478bd9Sstevel@tonic-gate 	}
7137c478bd9Sstevel@tonic-gate 
7147c478bd9Sstevel@tonic-gate 	/*
7157c478bd9Sstevel@tonic-gate 	 * Create the "Size" and "Nblocks" properties.
7167c478bd9Sstevel@tonic-gate 	 */
7177c478bd9Sstevel@tonic-gate 	fulldev = makedevice(ddi_driver_major(dip), minor);
7187c478bd9Sstevel@tonic-gate 	Size_prop_val = size;
7197c478bd9Sstevel@tonic-gate 	if ((ddi_prop_update_int64(fulldev, dip,
7207c478bd9Sstevel@tonic-gate 	    SIZE_PROP_NAME, Size_prop_val)) != DDI_PROP_SUCCESS) {
7217c478bd9Sstevel@tonic-gate 		goto create_failed;
7227c478bd9Sstevel@tonic-gate 	}
7237c478bd9Sstevel@tonic-gate 	Nblocks_prop_val = size / DEV_BSIZE;
7247c478bd9Sstevel@tonic-gate 	if ((ddi_prop_update_int64(fulldev, dip,
7257c478bd9Sstevel@tonic-gate 	    NBLOCKS_PROP_NAME, Nblocks_prop_val)) != DDI_PROP_SUCCESS) {
7267c478bd9Sstevel@tonic-gate 		goto create_failed;
7277c478bd9Sstevel@tonic-gate 	}
7287c478bd9Sstevel@tonic-gate 
7297c478bd9Sstevel@tonic-gate 	/*
7307c478bd9Sstevel@tonic-gate 	 * Allocate kstat stuff.
7317c478bd9Sstevel@tonic-gate 	 */
7327c478bd9Sstevel@tonic-gate 	rsp->rd_kstat = kstat_create(RD_DRIVER_NAME, minor, NULL,
7337c478bd9Sstevel@tonic-gate 	    "disk", KSTAT_TYPE_IO, 1, 0);
7347c478bd9Sstevel@tonic-gate 	if (rsp->rd_kstat) {
7357c478bd9Sstevel@tonic-gate 		mutex_init(&rsp->rd_kstat_lock, NULL,
7367c478bd9Sstevel@tonic-gate 		    MUTEX_DRIVER, NULL);
7377c478bd9Sstevel@tonic-gate 		rsp->rd_kstat->ks_lock = &rsp->rd_kstat_lock;
7387c478bd9Sstevel@tonic-gate 		kstat_install(rsp->rd_kstat);
7397c478bd9Sstevel@tonic-gate 	}
7407c478bd9Sstevel@tonic-gate 
7417c478bd9Sstevel@tonic-gate 	rd_fake_disk_geometry(rsp);
7427c478bd9Sstevel@tonic-gate 
7437c478bd9Sstevel@tonic-gate 	return (rsp);
7447c478bd9Sstevel@tonic-gate 
7457c478bd9Sstevel@tonic-gate create_failed:
7467c478bd9Sstevel@tonic-gate 	/*
7477c478bd9Sstevel@tonic-gate 	 * Cleanup.
7487c478bd9Sstevel@tonic-gate 	 */
7497c478bd9Sstevel@tonic-gate 	rd_dealloc_resources(rsp);
7507c478bd9Sstevel@tonic-gate 
7517c478bd9Sstevel@tonic-gate 	return (NULL);
7527c478bd9Sstevel@tonic-gate }
7537c478bd9Sstevel@tonic-gate 
7547c478bd9Sstevel@tonic-gate /*
7557c478bd9Sstevel@tonic-gate  * Undo what we did in rd_attach, freeing resources and removing things which
7567c478bd9Sstevel@tonic-gate  * we installed.  The system framework guarantees we are not active with this
7577c478bd9Sstevel@tonic-gate  * devinfo node in any other entry points at this time.
7587c478bd9Sstevel@tonic-gate  */
7597c478bd9Sstevel@tonic-gate static int
7607c478bd9Sstevel@tonic-gate rd_common_detach(dev_info_t *dip)
7617c478bd9Sstevel@tonic-gate {
7627c478bd9Sstevel@tonic-gate 	if (dip == rd_dip) {
7637c478bd9Sstevel@tonic-gate 		/*
7647c478bd9Sstevel@tonic-gate 		 * Pseudo node: can't detach if any pseudo ramdisks exist.
7657c478bd9Sstevel@tonic-gate 		 */
7667c478bd9Sstevel@tonic-gate 		if (rd_is_busy()) {
7677c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
7687c478bd9Sstevel@tonic-gate 		}
7697c478bd9Sstevel@tonic-gate 		ddi_soft_state_free(rd_statep, RD_CTL_MINOR);
7707c478bd9Sstevel@tonic-gate 		rd_dip = NULL;
7717c478bd9Sstevel@tonic-gate 	} else {
7727c478bd9Sstevel@tonic-gate 		/*
7737c478bd9Sstevel@tonic-gate 		 * A 'real' ramdisk; find the state and free resources.
7747c478bd9Sstevel@tonic-gate 		 */
7757c478bd9Sstevel@tonic-gate 		rd_devstate_t	*rsp;
7767c478bd9Sstevel@tonic-gate 
7777c478bd9Sstevel@tonic-gate 		if ((rsp = rd_find_dip_state(dip)) != NULL) {
7787c478bd9Sstevel@tonic-gate 			rd_dealloc_resources(rsp);
7797c478bd9Sstevel@tonic-gate 		}
7807c478bd9Sstevel@tonic-gate 	}
7817c478bd9Sstevel@tonic-gate 	ddi_remove_minor_node(dip, NULL);
7827c478bd9Sstevel@tonic-gate 
7837c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
7847c478bd9Sstevel@tonic-gate }
7857c478bd9Sstevel@tonic-gate 
7867c478bd9Sstevel@tonic-gate static int
7877c478bd9Sstevel@tonic-gate rd_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
7887c478bd9Sstevel@tonic-gate {
7897c478bd9Sstevel@tonic-gate 	char		*name;
7907c478bd9Sstevel@tonic-gate 	rd_existing_t	*ep = NULL;
791*986fd29aSsetje 	uint_t		obpaddr = 0, nep, i;
7927c478bd9Sstevel@tonic-gate 	size_t		size = 0;
7937c478bd9Sstevel@tonic-gate 	rd_devstate_t	*rsp;
7947c478bd9Sstevel@tonic-gate 
7957c478bd9Sstevel@tonic-gate 	switch (cmd) {
7967c478bd9Sstevel@tonic-gate 
7977c478bd9Sstevel@tonic-gate 	case DDI_ATTACH:
7987c478bd9Sstevel@tonic-gate 		mutex_enter(&rd_lock);
7997c478bd9Sstevel@tonic-gate 
8007c478bd9Sstevel@tonic-gate 		/*
8017c478bd9Sstevel@tonic-gate 		 * For pseudo ramdisk devinfo set up state 0 and :ctl device;
8027c478bd9Sstevel@tonic-gate 		 * else it's an OBP-created ramdisk.
8037c478bd9Sstevel@tonic-gate 		 */
8047c478bd9Sstevel@tonic-gate 		if (is_pseudo_device(dip)) {
8057c478bd9Sstevel@tonic-gate 			rd_dip = dip;
8067c478bd9Sstevel@tonic-gate 			rd_init_tuneables();
8077c478bd9Sstevel@tonic-gate 
8087c478bd9Sstevel@tonic-gate 			/*
8097c478bd9Sstevel@tonic-gate 			 * The zeroth minor is reserved for the ramdisk
8107c478bd9Sstevel@tonic-gate 			 * 'control' device.
8117c478bd9Sstevel@tonic-gate 			 */
8127c478bd9Sstevel@tonic-gate 			if (ddi_soft_state_zalloc(rd_statep, RD_CTL_MINOR) ==
8137c478bd9Sstevel@tonic-gate 			    DDI_FAILURE) {
8147c478bd9Sstevel@tonic-gate 				goto attach_failed;
8157c478bd9Sstevel@tonic-gate 			}
8167c478bd9Sstevel@tonic-gate 			rsp = ddi_get_soft_state(rd_statep, RD_CTL_MINOR);
8177c478bd9Sstevel@tonic-gate 			rsp->rd_dip = dip;
8187c478bd9Sstevel@tonic-gate 
8197c478bd9Sstevel@tonic-gate 			if (ddi_create_minor_node(dip, RD_CTL_NODE,
8207c478bd9Sstevel@tonic-gate 			    S_IFCHR, 0, DDI_PSEUDO, NULL) == DDI_FAILURE) {
8217c478bd9Sstevel@tonic-gate 				goto attach_failed;
8227c478bd9Sstevel@tonic-gate 			}
8237c478bd9Sstevel@tonic-gate 		} else {
8247c478bd9Sstevel@tonic-gate 			RD_STRIP_PREFIX(name, ddi_node_name(dip));
8257c478bd9Sstevel@tonic-gate 
8267c478bd9Sstevel@tonic-gate 			if (strlen(name) > RD_NAME_LEN) {
8277c478bd9Sstevel@tonic-gate 				cmn_err(CE_CONT,
8287c478bd9Sstevel@tonic-gate 				    "%s: name too long - ignoring\n", name);
8297c478bd9Sstevel@tonic-gate 				goto attach_failed;
8307c478bd9Sstevel@tonic-gate 			}
8317c478bd9Sstevel@tonic-gate 
8327c478bd9Sstevel@tonic-gate 			/*
8337c478bd9Sstevel@tonic-gate 			 * An OBP-created ramdisk must have an 'existing'
8347c478bd9Sstevel@tonic-gate 			 * property; get and check it.
8357c478bd9Sstevel@tonic-gate 			 */
8367c478bd9Sstevel@tonic-gate 			if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, dip,
837*986fd29aSsetje 			    DDI_PROP_DONTPASS, OBP_EXISTING_PROP_NAME,
838*986fd29aSsetje 			    (uchar_t **)&ep, &nep) == DDI_SUCCESS) {
839*986fd29aSsetje 
8407c478bd9Sstevel@tonic-gate 				if (nep == 0 || (nep % sizeof (*ep)) != 0) {
8417c478bd9Sstevel@tonic-gate 					cmn_err(CE_CONT,
842*986fd29aSsetje 					    "%s: " OBP_EXISTING_PROP_NAME
8437c478bd9Sstevel@tonic-gate 					    " illegal size\n", name);
8447c478bd9Sstevel@tonic-gate 					goto attach_failed;
8457c478bd9Sstevel@tonic-gate 				}
8467c478bd9Sstevel@tonic-gate 				nep /= sizeof (*ep);
8477c478bd9Sstevel@tonic-gate 
8487c478bd9Sstevel@tonic-gate 				/*
8497c478bd9Sstevel@tonic-gate 				 * Calculate the size of the ramdisk.
8507c478bd9Sstevel@tonic-gate 				 */
8517c478bd9Sstevel@tonic-gate 				for (i = 0; i < nep; ++i) {
8527c478bd9Sstevel@tonic-gate 					size += ep[i].size;
8537c478bd9Sstevel@tonic-gate 				}
854*986fd29aSsetje 			} else if ((obpaddr = ddi_prop_get_int(DDI_DEV_T_ANY,
855*986fd29aSsetje 			    dip, DDI_PROP_DONTPASS, OBP_ADDRESS_PROP_NAME,
856*986fd29aSsetje 			    0)) != 0)  {
857*986fd29aSsetje 
858*986fd29aSsetje 				size = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
859*986fd29aSsetje 				    DDI_PROP_DONTPASS, OBP_SIZE_PROP_NAME, 0);
860*986fd29aSsetje 			} else {
861*986fd29aSsetje 				cmn_err(CE_CONT, "%s: missing OBP properties\n",
862*986fd29aSsetje 				    name);
863*986fd29aSsetje 				goto attach_failed;
864*986fd29aSsetje 			}
8657c478bd9Sstevel@tonic-gate 
8667c478bd9Sstevel@tonic-gate 			/*
8677c478bd9Sstevel@tonic-gate 			 * Allocate driver resources for the ramdisk.
8687c478bd9Sstevel@tonic-gate 			 */
869*986fd29aSsetje 			if ((rsp = rd_alloc_resources(name, obpaddr, size,
8707c478bd9Sstevel@tonic-gate 			    dip)) == NULL) {
8717c478bd9Sstevel@tonic-gate 				goto attach_failed;
8727c478bd9Sstevel@tonic-gate 			}
8737c478bd9Sstevel@tonic-gate 
8747c478bd9Sstevel@tonic-gate 			rsp->rd_existing = ep;
8757c478bd9Sstevel@tonic-gate 			rsp->rd_nexisting = nep;
8767c478bd9Sstevel@tonic-gate 		}
8777c478bd9Sstevel@tonic-gate 
8787c478bd9Sstevel@tonic-gate 		mutex_exit(&rd_lock);
8797c478bd9Sstevel@tonic-gate 
8807c478bd9Sstevel@tonic-gate 		ddi_report_dev(dip);
8817c478bd9Sstevel@tonic-gate 
8827c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
8837c478bd9Sstevel@tonic-gate 
8847c478bd9Sstevel@tonic-gate 	case DDI_RESUME:
8857c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
8867c478bd9Sstevel@tonic-gate 
8877c478bd9Sstevel@tonic-gate 	default:
8887c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
8897c478bd9Sstevel@tonic-gate 	}
8907c478bd9Sstevel@tonic-gate 
8917c478bd9Sstevel@tonic-gate attach_failed:
8927c478bd9Sstevel@tonic-gate 	/*
8937c478bd9Sstevel@tonic-gate 	 * Use our common detach routine to unallocate any stuff which
8947c478bd9Sstevel@tonic-gate 	 * was allocated above.
8957c478bd9Sstevel@tonic-gate 	 */
8967c478bd9Sstevel@tonic-gate 	(void) rd_common_detach(dip);
8977c478bd9Sstevel@tonic-gate 	mutex_exit(&rd_lock);
8987c478bd9Sstevel@tonic-gate 
8997c478bd9Sstevel@tonic-gate 	if (ep != NULL) {
9007c478bd9Sstevel@tonic-gate 		ddi_prop_free(ep);
9017c478bd9Sstevel@tonic-gate 	}
9027c478bd9Sstevel@tonic-gate 	return (DDI_FAILURE);
9037c478bd9Sstevel@tonic-gate }
9047c478bd9Sstevel@tonic-gate 
9057c478bd9Sstevel@tonic-gate static int
9067c478bd9Sstevel@tonic-gate rd_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
9077c478bd9Sstevel@tonic-gate {
9087c478bd9Sstevel@tonic-gate 	int	e;
9097c478bd9Sstevel@tonic-gate 
9107c478bd9Sstevel@tonic-gate 	switch (cmd) {
9117c478bd9Sstevel@tonic-gate 
9127c478bd9Sstevel@tonic-gate 	case DDI_DETACH:
9137c478bd9Sstevel@tonic-gate 		mutex_enter(&rd_lock);
9147c478bd9Sstevel@tonic-gate 		e = rd_common_detach(dip);
9157c478bd9Sstevel@tonic-gate 		mutex_exit(&rd_lock);
9167c478bd9Sstevel@tonic-gate 
9177c478bd9Sstevel@tonic-gate 		return (e);
9187c478bd9Sstevel@tonic-gate 
9197c478bd9Sstevel@tonic-gate 	case DDI_SUSPEND:
9207c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
9217c478bd9Sstevel@tonic-gate 
9227c478bd9Sstevel@tonic-gate 	default:
9237c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
9247c478bd9Sstevel@tonic-gate 	}
9257c478bd9Sstevel@tonic-gate }
9267c478bd9Sstevel@tonic-gate 
9277c478bd9Sstevel@tonic-gate /*ARGSUSED*/
9287c478bd9Sstevel@tonic-gate static int
9297c478bd9Sstevel@tonic-gate rd_getinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
9307c478bd9Sstevel@tonic-gate {
9317c478bd9Sstevel@tonic-gate 	rd_devstate_t	*rsp;
9327c478bd9Sstevel@tonic-gate 
9337c478bd9Sstevel@tonic-gate 	switch (infocmd) {
9347c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
9357c478bd9Sstevel@tonic-gate 		if ((rsp = ddi_get_soft_state(rd_statep,
9367c478bd9Sstevel@tonic-gate 		    getminor((dev_t)arg))) != NULL) {
9377c478bd9Sstevel@tonic-gate 			*result = rsp->rd_dip;
9387c478bd9Sstevel@tonic-gate 			return (DDI_SUCCESS);
9397c478bd9Sstevel@tonic-gate 		}
9407c478bd9Sstevel@tonic-gate 		*result = NULL;
9417c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
9427c478bd9Sstevel@tonic-gate 
9437c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
9447c478bd9Sstevel@tonic-gate 		if ((rsp = ddi_get_soft_state(rd_statep,
9457c478bd9Sstevel@tonic-gate 		    getminor((dev_t)arg))) != NULL) {
9467c478bd9Sstevel@tonic-gate 			*result = (void *)(uintptr_t)
9477c478bd9Sstevel@tonic-gate 			    ddi_get_instance(rsp->rd_dip);
9487c478bd9Sstevel@tonic-gate 			return (DDI_SUCCESS);
9497c478bd9Sstevel@tonic-gate 		}
9507c478bd9Sstevel@tonic-gate 		*result = NULL;
9517c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
9527c478bd9Sstevel@tonic-gate 
9537c478bd9Sstevel@tonic-gate 	default:
9547c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
9557c478bd9Sstevel@tonic-gate 	}
9567c478bd9Sstevel@tonic-gate }
9577c478bd9Sstevel@tonic-gate 
9587c478bd9Sstevel@tonic-gate /*ARGSUSED3*/
9597c478bd9Sstevel@tonic-gate static int
9607c478bd9Sstevel@tonic-gate rd_open(dev_t *devp, int flag, int otyp, cred_t *credp)
9617c478bd9Sstevel@tonic-gate {
9627c478bd9Sstevel@tonic-gate 	minor_t		minor;
9637c478bd9Sstevel@tonic-gate 	rd_devstate_t	*rsp;
9647c478bd9Sstevel@tonic-gate 
9657c478bd9Sstevel@tonic-gate 	mutex_enter(&rd_lock);
9667c478bd9Sstevel@tonic-gate 
9677c478bd9Sstevel@tonic-gate 	minor = getminor(*devp);
9687c478bd9Sstevel@tonic-gate 	if (minor == RD_CTL_MINOR) {
9697c478bd9Sstevel@tonic-gate 		/*
9707c478bd9Sstevel@tonic-gate 		 * Master control device; must be opened exclusively.
9717c478bd9Sstevel@tonic-gate 		 */
9727c478bd9Sstevel@tonic-gate 		if ((flag & FEXCL) != FEXCL || otyp != OTYP_CHR) {
9737c478bd9Sstevel@tonic-gate 			mutex_exit(&rd_lock);
9747c478bd9Sstevel@tonic-gate 			return (EINVAL);
9757c478bd9Sstevel@tonic-gate 		}
9767c478bd9Sstevel@tonic-gate 
9777c478bd9Sstevel@tonic-gate 		rsp = ddi_get_soft_state(rd_statep, RD_CTL_MINOR);
9787c478bd9Sstevel@tonic-gate 		if (rsp == NULL) {
9797c478bd9Sstevel@tonic-gate 			mutex_exit(&rd_lock);
9807c478bd9Sstevel@tonic-gate 			return (ENXIO);
9817c478bd9Sstevel@tonic-gate 		}
9827c478bd9Sstevel@tonic-gate 
9837c478bd9Sstevel@tonic-gate 		if (rd_is_open(rsp)) {
9847c478bd9Sstevel@tonic-gate 			mutex_exit(&rd_lock);
9857c478bd9Sstevel@tonic-gate 			return (EBUSY);
9867c478bd9Sstevel@tonic-gate 		}
9877c478bd9Sstevel@tonic-gate 		(void) rd_opened(rsp, OTYP_CHR);
9887c478bd9Sstevel@tonic-gate 
9897c478bd9Sstevel@tonic-gate 		mutex_exit(&rd_lock);
9907c478bd9Sstevel@tonic-gate 
9917c478bd9Sstevel@tonic-gate 		return (0);
9927c478bd9Sstevel@tonic-gate 	}
9937c478bd9Sstevel@tonic-gate 
9947c478bd9Sstevel@tonic-gate 	rsp = ddi_get_soft_state(rd_statep, minor);
9957c478bd9Sstevel@tonic-gate 	if (rsp == NULL) {
9967c478bd9Sstevel@tonic-gate 		mutex_exit(&rd_lock);
9977c478bd9Sstevel@tonic-gate 		return (ENXIO);
9987c478bd9Sstevel@tonic-gate 	}
9997c478bd9Sstevel@tonic-gate 
10007c478bd9Sstevel@tonic-gate 	if (rd_opened(rsp, otyp) == -1) {
10017c478bd9Sstevel@tonic-gate 		mutex_exit(&rd_lock);
10027c478bd9Sstevel@tonic-gate 		return (EINVAL);
10037c478bd9Sstevel@tonic-gate 	}
10047c478bd9Sstevel@tonic-gate 
10057c478bd9Sstevel@tonic-gate 	mutex_exit(&rd_lock);
10067c478bd9Sstevel@tonic-gate 	return (0);
10077c478bd9Sstevel@tonic-gate }
10087c478bd9Sstevel@tonic-gate 
10097c478bd9Sstevel@tonic-gate /*ARGSUSED*/
10107c478bd9Sstevel@tonic-gate static int
10117c478bd9Sstevel@tonic-gate rd_close(dev_t dev, int flag, int otyp, struct cred *credp)
10127c478bd9Sstevel@tonic-gate {
10137c478bd9Sstevel@tonic-gate 	minor_t		minor;
10147c478bd9Sstevel@tonic-gate 	rd_devstate_t	*rsp;
10157c478bd9Sstevel@tonic-gate 
10167c478bd9Sstevel@tonic-gate 	mutex_enter(&rd_lock);
10177c478bd9Sstevel@tonic-gate 
10187c478bd9Sstevel@tonic-gate 	minor = getminor(dev);
10197c478bd9Sstevel@tonic-gate 
10207c478bd9Sstevel@tonic-gate 	rsp = ddi_get_soft_state(rd_statep, minor);
10217c478bd9Sstevel@tonic-gate 	if (rsp == NULL) {
10227c478bd9Sstevel@tonic-gate 		mutex_exit(&rd_lock);
10237c478bd9Sstevel@tonic-gate 		return (EINVAL);
10247c478bd9Sstevel@tonic-gate 	}
10257c478bd9Sstevel@tonic-gate 
10267c478bd9Sstevel@tonic-gate 	rd_closed(rsp, otyp);
10277c478bd9Sstevel@tonic-gate 
10287c478bd9Sstevel@tonic-gate 	mutex_exit(&rd_lock);
10297c478bd9Sstevel@tonic-gate 
10307c478bd9Sstevel@tonic-gate 	return (0);
10317c478bd9Sstevel@tonic-gate }
10327c478bd9Sstevel@tonic-gate 
10337c478bd9Sstevel@tonic-gate static void
10347c478bd9Sstevel@tonic-gate rd_minphys(struct buf *bp)
10357c478bd9Sstevel@tonic-gate {
10367c478bd9Sstevel@tonic-gate 	if (bp->b_bcount > rd_maxphys) {
10377c478bd9Sstevel@tonic-gate 		bp->b_bcount = rd_maxphys;
10387c478bd9Sstevel@tonic-gate 	}
10397c478bd9Sstevel@tonic-gate }
10407c478bd9Sstevel@tonic-gate 
10417c478bd9Sstevel@tonic-gate static void
10427c478bd9Sstevel@tonic-gate rd_rw(rd_devstate_t *rsp, struct buf *bp, offset_t offset, size_t nbytes)
10437c478bd9Sstevel@tonic-gate {
10447c478bd9Sstevel@tonic-gate 	int	reading = bp->b_flags & B_READ;
10457c478bd9Sstevel@tonic-gate 	caddr_t	buf_addr;
10467c478bd9Sstevel@tonic-gate 
10477c478bd9Sstevel@tonic-gate 	bp_mapin(bp);
10487c478bd9Sstevel@tonic-gate 	buf_addr = bp->b_un.b_addr;
10497c478bd9Sstevel@tonic-gate 
10507c478bd9Sstevel@tonic-gate 	while (nbytes > 0) {
10517c478bd9Sstevel@tonic-gate 		offset_t	off_in_window;
10527c478bd9Sstevel@tonic-gate 		size_t		rem_in_window, copy_bytes;
10537c478bd9Sstevel@tonic-gate 		caddr_t		raddr;
10547c478bd9Sstevel@tonic-gate 
10557c478bd9Sstevel@tonic-gate 		mutex_enter(&rsp->rd_device_lock);
10567c478bd9Sstevel@tonic-gate 		rd_map_window(rsp, offset);
10577c478bd9Sstevel@tonic-gate 
10587c478bd9Sstevel@tonic-gate 		off_in_window = offset - rsp->rd_window_base;
10597c478bd9Sstevel@tonic-gate 		rem_in_window = rsp->rd_window_size - off_in_window;
10607c478bd9Sstevel@tonic-gate 
10617c478bd9Sstevel@tonic-gate 		raddr = rsp->rd_window_virt + off_in_window;
10627c478bd9Sstevel@tonic-gate 		copy_bytes = MIN(nbytes, rem_in_window);
10637c478bd9Sstevel@tonic-gate 
10647c478bd9Sstevel@tonic-gate 		if (reading) {
10657c478bd9Sstevel@tonic-gate 			(void) bcopy(raddr, buf_addr, copy_bytes);
10667c478bd9Sstevel@tonic-gate 		} else {
10677c478bd9Sstevel@tonic-gate 			(void) bcopy(buf_addr, raddr, copy_bytes);
10687c478bd9Sstevel@tonic-gate 		}
10697c478bd9Sstevel@tonic-gate 		mutex_exit(&rsp->rd_device_lock);
10707c478bd9Sstevel@tonic-gate 
10717c478bd9Sstevel@tonic-gate 		offset   += copy_bytes;
10727c478bd9Sstevel@tonic-gate 		buf_addr += copy_bytes;
10737c478bd9Sstevel@tonic-gate 		nbytes   -= copy_bytes;
10747c478bd9Sstevel@tonic-gate 	}
10757c478bd9Sstevel@tonic-gate }
10767c478bd9Sstevel@tonic-gate 
10777c478bd9Sstevel@tonic-gate static int
10787c478bd9Sstevel@tonic-gate rd_strategy(struct buf *bp)
10797c478bd9Sstevel@tonic-gate {
10807c478bd9Sstevel@tonic-gate 	rd_devstate_t	*rsp;
10817c478bd9Sstevel@tonic-gate 	offset_t	offset;
10827c478bd9Sstevel@tonic-gate 
10837c478bd9Sstevel@tonic-gate 	rsp = ddi_get_soft_state(rd_statep, getminor(bp->b_edev));
10847c478bd9Sstevel@tonic-gate 	offset = bp->b_blkno * DEV_BSIZE;
10857c478bd9Sstevel@tonic-gate 
10867c478bd9Sstevel@tonic-gate 	if (rsp == NULL) {
10877c478bd9Sstevel@tonic-gate 		bp->b_error = ENXIO;
10887c478bd9Sstevel@tonic-gate 		bp->b_flags |= B_ERROR;
10897c478bd9Sstevel@tonic-gate 	} else if (offset >= rsp->rd_size) {
10907c478bd9Sstevel@tonic-gate 		bp->b_error = EINVAL;
10917c478bd9Sstevel@tonic-gate 		bp->b_flags |= B_ERROR;
10927c478bd9Sstevel@tonic-gate 	} else {
10937c478bd9Sstevel@tonic-gate 		size_t	nbytes;
10947c478bd9Sstevel@tonic-gate 
10957c478bd9Sstevel@tonic-gate 		if (rsp->rd_kstat) {
10967c478bd9Sstevel@tonic-gate 			mutex_enter(rsp->rd_kstat->ks_lock);
10977c478bd9Sstevel@tonic-gate 			kstat_runq_enter(KSTAT_IO_PTR(rsp->rd_kstat));
10987c478bd9Sstevel@tonic-gate 			mutex_exit(rsp->rd_kstat->ks_lock);
10997c478bd9Sstevel@tonic-gate 		}
11007c478bd9Sstevel@tonic-gate 
11017c478bd9Sstevel@tonic-gate 		nbytes = min(bp->b_bcount, rsp->rd_size - offset);
11027c478bd9Sstevel@tonic-gate 
11037c478bd9Sstevel@tonic-gate 		rd_rw(rsp, bp, offset, nbytes);
11047c478bd9Sstevel@tonic-gate 
11057c478bd9Sstevel@tonic-gate 		bp->b_resid = bp->b_bcount - nbytes;
11067c478bd9Sstevel@tonic-gate 
11077c478bd9Sstevel@tonic-gate 		if (rsp->rd_kstat) {
11087c478bd9Sstevel@tonic-gate 			kstat_io_t *kioptr;
11097c478bd9Sstevel@tonic-gate 
11107c478bd9Sstevel@tonic-gate 			mutex_enter(rsp->rd_kstat->ks_lock);
11117c478bd9Sstevel@tonic-gate 			kioptr = KSTAT_IO_PTR(rsp->rd_kstat);
11127c478bd9Sstevel@tonic-gate 			if (bp->b_flags & B_READ) {
11137c478bd9Sstevel@tonic-gate 				kioptr->nread += nbytes;
11147c478bd9Sstevel@tonic-gate 				kioptr->reads++;
11157c478bd9Sstevel@tonic-gate 			} else {
11167c478bd9Sstevel@tonic-gate 				kioptr->nwritten += nbytes;
11177c478bd9Sstevel@tonic-gate 				kioptr->writes++;
11187c478bd9Sstevel@tonic-gate 			}
11197c478bd9Sstevel@tonic-gate 			kstat_runq_exit(kioptr);
11207c478bd9Sstevel@tonic-gate 			mutex_exit(rsp->rd_kstat->ks_lock);
11217c478bd9Sstevel@tonic-gate 		}
11227c478bd9Sstevel@tonic-gate 	}
11237c478bd9Sstevel@tonic-gate 
11247c478bd9Sstevel@tonic-gate 	biodone(bp);
11257c478bd9Sstevel@tonic-gate 	return (0);
11267c478bd9Sstevel@tonic-gate }
11277c478bd9Sstevel@tonic-gate 
11287c478bd9Sstevel@tonic-gate /*ARGSUSED*/
11297c478bd9Sstevel@tonic-gate static int
11307c478bd9Sstevel@tonic-gate rd_read(dev_t dev, struct uio *uiop, cred_t *credp)
11317c478bd9Sstevel@tonic-gate {
11327c478bd9Sstevel@tonic-gate 	rd_devstate_t	*rsp;
11337c478bd9Sstevel@tonic-gate 
11347c478bd9Sstevel@tonic-gate 	rsp = ddi_get_soft_state(rd_statep, getminor(dev));
11357c478bd9Sstevel@tonic-gate 
11367c478bd9Sstevel@tonic-gate 	if (uiop->uio_offset >= rsp->rd_size)
11377c478bd9Sstevel@tonic-gate 		return (EINVAL);
11387c478bd9Sstevel@tonic-gate 
11397c478bd9Sstevel@tonic-gate 	return (physio(rd_strategy, NULL, dev, B_READ, rd_minphys, uiop));
11407c478bd9Sstevel@tonic-gate }
11417c478bd9Sstevel@tonic-gate 
11427c478bd9Sstevel@tonic-gate /*ARGSUSED*/
11437c478bd9Sstevel@tonic-gate static int
11447c478bd9Sstevel@tonic-gate rd_write(dev_t dev, register struct uio *uiop, cred_t *credp)
11457c478bd9Sstevel@tonic-gate {
11467c478bd9Sstevel@tonic-gate 	rd_devstate_t	*rsp;
11477c478bd9Sstevel@tonic-gate 
11487c478bd9Sstevel@tonic-gate 	rsp = ddi_get_soft_state(rd_statep, getminor(dev));
11497c478bd9Sstevel@tonic-gate 
11507c478bd9Sstevel@tonic-gate 	if (uiop->uio_offset >= rsp->rd_size)
11517c478bd9Sstevel@tonic-gate 		return (EINVAL);
11527c478bd9Sstevel@tonic-gate 
11537c478bd9Sstevel@tonic-gate 	return (physio(rd_strategy, NULL, dev, B_WRITE, rd_minphys, uiop));
11547c478bd9Sstevel@tonic-gate }
11557c478bd9Sstevel@tonic-gate 
11567c478bd9Sstevel@tonic-gate /*ARGSUSED*/
11577c478bd9Sstevel@tonic-gate static int
11587c478bd9Sstevel@tonic-gate rd_create_disk(dev_t dev, struct rd_ioctl *urip, int mode, int *rvalp)
11597c478bd9Sstevel@tonic-gate {
11607c478bd9Sstevel@tonic-gate 	struct rd_ioctl	kri;
11617c478bd9Sstevel@tonic-gate 	size_t		size;
11627c478bd9Sstevel@tonic-gate 	rd_devstate_t	*rsp;
11637c478bd9Sstevel@tonic-gate 
11647c478bd9Sstevel@tonic-gate 	if (ddi_copyin(urip, &kri, sizeof (kri), mode) == -1) {
11657c478bd9Sstevel@tonic-gate 		return (EFAULT);
11667c478bd9Sstevel@tonic-gate 	}
11677c478bd9Sstevel@tonic-gate 
11687c478bd9Sstevel@tonic-gate 	kri.ri_name[RD_NAME_LEN] = '\0';
11697c478bd9Sstevel@tonic-gate 
11707c478bd9Sstevel@tonic-gate 	size = kri.ri_size;
11717c478bd9Sstevel@tonic-gate 	if (size == 0) {
11727c478bd9Sstevel@tonic-gate 		return (EINVAL);
11737c478bd9Sstevel@tonic-gate 	}
11747c478bd9Sstevel@tonic-gate 	size = ptob(btopr(size));
11757c478bd9Sstevel@tonic-gate 
11767c478bd9Sstevel@tonic-gate 	mutex_enter(&rd_lock);
11777c478bd9Sstevel@tonic-gate 
11787c478bd9Sstevel@tonic-gate 	if (rd_find_named_disk(kri.ri_name) != NULL) {
11797c478bd9Sstevel@tonic-gate 		mutex_exit(&rd_lock);
11807c478bd9Sstevel@tonic-gate 		return (EEXIST);
11817c478bd9Sstevel@tonic-gate 	}
11827c478bd9Sstevel@tonic-gate 
1183*986fd29aSsetje 	rsp = rd_alloc_resources(kri.ri_name, 0, size, rd_dip);
11847c478bd9Sstevel@tonic-gate 	if (rsp == NULL) {
11857c478bd9Sstevel@tonic-gate 		mutex_exit(&rd_lock);
11867c478bd9Sstevel@tonic-gate 		return (EAGAIN);
11877c478bd9Sstevel@tonic-gate 	}
11887c478bd9Sstevel@tonic-gate 
11897c478bd9Sstevel@tonic-gate 	mutex_exit(&rd_lock);
11907c478bd9Sstevel@tonic-gate 
11917c478bd9Sstevel@tonic-gate 	return (ddi_copyout(&kri, urip, sizeof (kri), mode) == -1 ? EFAULT : 0);
11927c478bd9Sstevel@tonic-gate }
11937c478bd9Sstevel@tonic-gate 
11947c478bd9Sstevel@tonic-gate /*ARGSUSED*/
11957c478bd9Sstevel@tonic-gate static int
11967c478bd9Sstevel@tonic-gate rd_delete_disk(dev_t dev, struct rd_ioctl *urip, int mode)
11977c478bd9Sstevel@tonic-gate {
11987c478bd9Sstevel@tonic-gate 	struct rd_ioctl	kri;
11997c478bd9Sstevel@tonic-gate 	rd_devstate_t	*rsp;
12007c478bd9Sstevel@tonic-gate 
12017c478bd9Sstevel@tonic-gate 	if (ddi_copyin(urip, &kri, sizeof (kri), mode) == -1) {
12027c478bd9Sstevel@tonic-gate 		return (EFAULT);
12037c478bd9Sstevel@tonic-gate 	}
12047c478bd9Sstevel@tonic-gate 
12057c478bd9Sstevel@tonic-gate 	kri.ri_name[RD_NAME_LEN] = '\0';
12067c478bd9Sstevel@tonic-gate 
12077c478bd9Sstevel@tonic-gate 	mutex_enter(&rd_lock);
12087c478bd9Sstevel@tonic-gate 
12097c478bd9Sstevel@tonic-gate 	rsp = rd_find_named_disk(kri.ri_name);
12107c478bd9Sstevel@tonic-gate 	if (rsp == NULL || rsp->rd_dip != rd_dip) {
12117c478bd9Sstevel@tonic-gate 		mutex_exit(&rd_lock);
12127c478bd9Sstevel@tonic-gate 		return (EINVAL);
12137c478bd9Sstevel@tonic-gate 	}
12147c478bd9Sstevel@tonic-gate 	if (rd_is_open(rsp)) {
12157c478bd9Sstevel@tonic-gate 		mutex_exit(&rd_lock);
12167c478bd9Sstevel@tonic-gate 		return (EBUSY);
12177c478bd9Sstevel@tonic-gate 	}
12187c478bd9Sstevel@tonic-gate 
12197c478bd9Sstevel@tonic-gate 	rd_dealloc_resources(rsp);
12207c478bd9Sstevel@tonic-gate 
12217c478bd9Sstevel@tonic-gate 	mutex_exit(&rd_lock);
12227c478bd9Sstevel@tonic-gate 
12237c478bd9Sstevel@tonic-gate 	return (0);
12247c478bd9Sstevel@tonic-gate }
12257c478bd9Sstevel@tonic-gate 
12267c478bd9Sstevel@tonic-gate /*ARGSUSED*/
12277c478bd9Sstevel@tonic-gate static int
12287c478bd9Sstevel@tonic-gate rd_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp, int *rvalp)
12297c478bd9Sstevel@tonic-gate {
12307c478bd9Sstevel@tonic-gate 	minor_t		minor;
12317c478bd9Sstevel@tonic-gate 	int		error;
12327c478bd9Sstevel@tonic-gate 	enum dkio_state	dkstate;
12337c478bd9Sstevel@tonic-gate 	rd_devstate_t	*rsp;
12347c478bd9Sstevel@tonic-gate 
12357c478bd9Sstevel@tonic-gate 	minor = getminor(dev);
12367c478bd9Sstevel@tonic-gate 
12377c478bd9Sstevel@tonic-gate 	/*
12387c478bd9Sstevel@tonic-gate 	 * Ramdisk ioctls only apply to the master device.
12397c478bd9Sstevel@tonic-gate 	 */
12407c478bd9Sstevel@tonic-gate 	if (minor == RD_CTL_MINOR) {
12417c478bd9Sstevel@tonic-gate 		struct rd_ioctl *rip = (struct rd_ioctl *)arg;
12427c478bd9Sstevel@tonic-gate 
12437c478bd9Sstevel@tonic-gate 		/*
12447c478bd9Sstevel@tonic-gate 		 * The query commands only need read-access - i.e., normal
12457c478bd9Sstevel@tonic-gate 		 * users are allowed to do those on the controlling device
12467c478bd9Sstevel@tonic-gate 		 * as long as they can open it read-only.
12477c478bd9Sstevel@tonic-gate 		 */
12487c478bd9Sstevel@tonic-gate 		switch (cmd) {
12497c478bd9Sstevel@tonic-gate 		case RD_CREATE_DISK:
12507c478bd9Sstevel@tonic-gate 			if ((mode & FWRITE) == 0)
12517c478bd9Sstevel@tonic-gate 				return (EPERM);
12527c478bd9Sstevel@tonic-gate 			return (rd_create_disk(dev, rip, mode, rvalp));
12537c478bd9Sstevel@tonic-gate 
12547c478bd9Sstevel@tonic-gate 		case RD_DELETE_DISK:
12557c478bd9Sstevel@tonic-gate 			if ((mode & FWRITE) == 0)
12567c478bd9Sstevel@tonic-gate 				return (EPERM);
12577c478bd9Sstevel@tonic-gate 			return (rd_delete_disk(dev, rip, mode));
12587c478bd9Sstevel@tonic-gate 
12597c478bd9Sstevel@tonic-gate 		default:
12607c478bd9Sstevel@tonic-gate 			return (EINVAL);
12617c478bd9Sstevel@tonic-gate 		}
12627c478bd9Sstevel@tonic-gate 	}
12637c478bd9Sstevel@tonic-gate 
12647c478bd9Sstevel@tonic-gate 	rsp = ddi_get_soft_state(rd_statep, minor);
12657c478bd9Sstevel@tonic-gate 	if (rsp == NULL) {
12667c478bd9Sstevel@tonic-gate 		return (ENXIO);
12677c478bd9Sstevel@tonic-gate 	}
12687c478bd9Sstevel@tonic-gate 
12697c478bd9Sstevel@tonic-gate 	/*
12707c478bd9Sstevel@tonic-gate 	 * These are for faking out utilities like newfs.
12717c478bd9Sstevel@tonic-gate 	 */
12727c478bd9Sstevel@tonic-gate 	switch (cmd) {
12737c478bd9Sstevel@tonic-gate 	case DKIOCGVTOC:
12747c478bd9Sstevel@tonic-gate 		switch (ddi_model_convert_from(mode & FMODELS)) {
12757c478bd9Sstevel@tonic-gate 		case DDI_MODEL_ILP32: {
12767c478bd9Sstevel@tonic-gate 			struct vtoc32 vtoc32;
12777c478bd9Sstevel@tonic-gate 
12787c478bd9Sstevel@tonic-gate 			vtoctovtoc32(rsp->rd_vtoc, vtoc32);
12797c478bd9Sstevel@tonic-gate 			if (ddi_copyout(&vtoc32, (void *)arg,
12807c478bd9Sstevel@tonic-gate 			    sizeof (struct vtoc32), mode))
12817c478bd9Sstevel@tonic-gate 				return (EFAULT);
12827c478bd9Sstevel@tonic-gate 			}
12837c478bd9Sstevel@tonic-gate 			break;
12847c478bd9Sstevel@tonic-gate 
12857c478bd9Sstevel@tonic-gate 		case DDI_MODEL_NONE:
12867c478bd9Sstevel@tonic-gate 			if (ddi_copyout(&rsp->rd_vtoc, (void *)arg,
12877c478bd9Sstevel@tonic-gate 			    sizeof (struct vtoc), mode))
12887c478bd9Sstevel@tonic-gate 				return (EFAULT);
12897c478bd9Sstevel@tonic-gate 			break;
12907c478bd9Sstevel@tonic-gate 		}
12917c478bd9Sstevel@tonic-gate 		return (0);
12927c478bd9Sstevel@tonic-gate 	case DKIOCINFO:
12937c478bd9Sstevel@tonic-gate 		error = ddi_copyout(&rsp->rd_ci, (void *)arg,
12947c478bd9Sstevel@tonic-gate 		    sizeof (struct dk_cinfo), mode);
12957c478bd9Sstevel@tonic-gate 		if (error)
12967c478bd9Sstevel@tonic-gate 			return (EFAULT);
12977c478bd9Sstevel@tonic-gate 		return (0);
12987c478bd9Sstevel@tonic-gate 	case DKIOCG_VIRTGEOM:
12997c478bd9Sstevel@tonic-gate 	case DKIOCG_PHYGEOM:
13007c478bd9Sstevel@tonic-gate 	case DKIOCGGEOM:
13017c478bd9Sstevel@tonic-gate 		error = ddi_copyout(&rsp->rd_dkg, (void *)arg,
13027c478bd9Sstevel@tonic-gate 		    sizeof (struct dk_geom), mode);
13037c478bd9Sstevel@tonic-gate 		if (error)
13047c478bd9Sstevel@tonic-gate 			return (EFAULT);
13057c478bd9Sstevel@tonic-gate 		return (0);
13067c478bd9Sstevel@tonic-gate 	case DKIOCSTATE:
13077c478bd9Sstevel@tonic-gate 		/* the file is always there */
13087c478bd9Sstevel@tonic-gate 		dkstate = DKIO_INSERTED;
13097c478bd9Sstevel@tonic-gate 		error = ddi_copyout(&dkstate, (void *)arg,
13107c478bd9Sstevel@tonic-gate 		    sizeof (enum dkio_state), mode);
13117c478bd9Sstevel@tonic-gate 		if (error)
13127c478bd9Sstevel@tonic-gate 			return (EFAULT);
13137c478bd9Sstevel@tonic-gate 		return (0);
13147c478bd9Sstevel@tonic-gate 	default:
13157c478bd9Sstevel@tonic-gate 		return (ENOTTY);
13167c478bd9Sstevel@tonic-gate 	}
13177c478bd9Sstevel@tonic-gate }
13187c478bd9Sstevel@tonic-gate 
13197c478bd9Sstevel@tonic-gate 
13207c478bd9Sstevel@tonic-gate static struct cb_ops rd_cb_ops = {
13217c478bd9Sstevel@tonic-gate 	rd_open,
13227c478bd9Sstevel@tonic-gate 	rd_close,
13237c478bd9Sstevel@tonic-gate 	rd_strategy,
13247c478bd9Sstevel@tonic-gate 	nodev,
13257c478bd9Sstevel@tonic-gate 	nodev,		/* dump */
13267c478bd9Sstevel@tonic-gate 	rd_read,
13277c478bd9Sstevel@tonic-gate 	rd_write,
13287c478bd9Sstevel@tonic-gate 	rd_ioctl,
13297c478bd9Sstevel@tonic-gate 	nodev,		/* devmap */
13307c478bd9Sstevel@tonic-gate 	nodev,		/* mmap */
13317c478bd9Sstevel@tonic-gate 	nodev,		/* segmap */
13327c478bd9Sstevel@tonic-gate 	nochpoll,	/* poll */
13337c478bd9Sstevel@tonic-gate 	ddi_prop_op,
13347c478bd9Sstevel@tonic-gate 	NULL,
13357c478bd9Sstevel@tonic-gate 	D_NEW | D_MP
13367c478bd9Sstevel@tonic-gate };
13377c478bd9Sstevel@tonic-gate 
13387c478bd9Sstevel@tonic-gate static struct dev_ops rd_ops = {
13397c478bd9Sstevel@tonic-gate 	DEVO_REV,
13407c478bd9Sstevel@tonic-gate 	0,
13417c478bd9Sstevel@tonic-gate 	rd_getinfo,
13427c478bd9Sstevel@tonic-gate 	nulldev,	/* identify */
13437c478bd9Sstevel@tonic-gate 	nulldev,	/* probe */
13447c478bd9Sstevel@tonic-gate 	rd_attach,
13457c478bd9Sstevel@tonic-gate 	rd_detach,
13467c478bd9Sstevel@tonic-gate 	nodev,		/* reset */
13477c478bd9Sstevel@tonic-gate 	&rd_cb_ops,
13487c478bd9Sstevel@tonic-gate 	(struct bus_ops *)0
13497c478bd9Sstevel@tonic-gate };
13507c478bd9Sstevel@tonic-gate 
13517c478bd9Sstevel@tonic-gate 
13527c478bd9Sstevel@tonic-gate extern struct mod_ops mod_driverops;
13537c478bd9Sstevel@tonic-gate 
13547c478bd9Sstevel@tonic-gate static struct modldrv modldrv = {
13557c478bd9Sstevel@tonic-gate 	&mod_driverops,
1356*986fd29aSsetje 	"ramdisk driver",
13577c478bd9Sstevel@tonic-gate 	&rd_ops
13587c478bd9Sstevel@tonic-gate };
13597c478bd9Sstevel@tonic-gate 
13607c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
13617c478bd9Sstevel@tonic-gate 	MODREV_1,
13627c478bd9Sstevel@tonic-gate 	&modldrv,
13637c478bd9Sstevel@tonic-gate 	0
13647c478bd9Sstevel@tonic-gate };
13657c478bd9Sstevel@tonic-gate 
13667c478bd9Sstevel@tonic-gate int
13677c478bd9Sstevel@tonic-gate _init(void)
13687c478bd9Sstevel@tonic-gate {
13697c478bd9Sstevel@tonic-gate 	int e;
13707c478bd9Sstevel@tonic-gate 
13717c478bd9Sstevel@tonic-gate 	if ((e = ddi_soft_state_init(&rd_statep,
13727c478bd9Sstevel@tonic-gate 	    sizeof (rd_devstate_t), 0)) != 0) {
13737c478bd9Sstevel@tonic-gate 		return (e);
13747c478bd9Sstevel@tonic-gate 	}
13757c478bd9Sstevel@tonic-gate 
13767c478bd9Sstevel@tonic-gate 	mutex_init(&rd_lock, NULL, MUTEX_DRIVER, NULL);
13777c478bd9Sstevel@tonic-gate 
13787c478bd9Sstevel@tonic-gate 	if ((e = mod_install(&modlinkage)) != 0)  {
13797c478bd9Sstevel@tonic-gate 		mutex_destroy(&rd_lock);
13807c478bd9Sstevel@tonic-gate 		ddi_soft_state_fini(&rd_statep);
13817c478bd9Sstevel@tonic-gate 	}
13827c478bd9Sstevel@tonic-gate 
13837c478bd9Sstevel@tonic-gate 	return (e);
13847c478bd9Sstevel@tonic-gate }
13857c478bd9Sstevel@tonic-gate 
13867c478bd9Sstevel@tonic-gate int
13877c478bd9Sstevel@tonic-gate _fini(void)
13887c478bd9Sstevel@tonic-gate {
13897c478bd9Sstevel@tonic-gate 	int e;
13907c478bd9Sstevel@tonic-gate 
13917c478bd9Sstevel@tonic-gate 	if ((e = mod_remove(&modlinkage)) != 0)  {
13927c478bd9Sstevel@tonic-gate 		return (e);
13937c478bd9Sstevel@tonic-gate 	}
13947c478bd9Sstevel@tonic-gate 
13957c478bd9Sstevel@tonic-gate 	ddi_soft_state_fini(&rd_statep);
13967c478bd9Sstevel@tonic-gate 	mutex_destroy(&rd_lock);
13977c478bd9Sstevel@tonic-gate 
13987c478bd9Sstevel@tonic-gate 	return (e);
13997c478bd9Sstevel@tonic-gate }
14007c478bd9Sstevel@tonic-gate 
14017c478bd9Sstevel@tonic-gate int
14027c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
14037c478bd9Sstevel@tonic-gate {
14047c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
14057c478bd9Sstevel@tonic-gate }
1406