xref: /onnv-gate/usr/src/uts/common/io/ramdisk.c (revision 9956:1705cb23ec4b)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
52912Sartem  * Common Development and Distribution License (the "License").
62912Sartem  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*9956SWilliam.Roche@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*
280Sstevel@tonic-gate  * Ramdisk device driver.
290Sstevel@tonic-gate  *
300Sstevel@tonic-gate  * There are two types of ramdisk: 'real' OBP-created ramdisks, and 'pseudo'
310Sstevel@tonic-gate  * ramdisks created at runtime with no corresponding OBP device node.  The
320Sstevel@tonic-gate  * ramdisk(7D) driver is capable of dealing with both, and with the creation
330Sstevel@tonic-gate  * and deletion of 'pseudo' ramdisks.
340Sstevel@tonic-gate  *
350Sstevel@tonic-gate  * Every ramdisk has a single 'state' structure which maintains data for
360Sstevel@tonic-gate  * that ramdisk, and is assigned a single minor number.  The bottom 10-bits
370Sstevel@tonic-gate  * of the minor number index the state structures; the top 8-bits give a
380Sstevel@tonic-gate  * 'real OBP disk' number, i.e. they are zero for 'pseudo' ramdisks.  Thus
390Sstevel@tonic-gate  * it is possible to distinguish 'real' from 'pseudo' ramdisks using the
400Sstevel@tonic-gate  * top 8-bits of the minor number.
410Sstevel@tonic-gate  *
420Sstevel@tonic-gate  * Each OBP-created ramdisk has its own node in the device tree with an
430Sstevel@tonic-gate  * "existing" property which describes the one-or-more physical address ranges
440Sstevel@tonic-gate  * assigned to the ramdisk.  All 'pseudo' ramdisks share a common devinfo
450Sstevel@tonic-gate  * structure.
460Sstevel@tonic-gate  *
470Sstevel@tonic-gate  * A single character device node is used by ramdiskadm(1M) to communicate
480Sstevel@tonic-gate  * with the ramdisk driver, with minor number 0:
490Sstevel@tonic-gate  *
500Sstevel@tonic-gate  *	/dev/ramdiskctl -> /devices/pseudo/ramdisk@0:ctl
510Sstevel@tonic-gate  *
520Sstevel@tonic-gate  * For consistent access, block and raw device nodes are created for *every*
530Sstevel@tonic-gate  * ramdisk.  For 'pseudo' ramdisks:
540Sstevel@tonic-gate  *
550Sstevel@tonic-gate  *	/dev/ramdisk/<diskname>  -> /devices/pseudo/ramdisk@0:<diskname>
560Sstevel@tonic-gate  *	/dev/rramdisk/<diskname> -> /devices/pseudo/ramdisk@0:<diskname>,raw
570Sstevel@tonic-gate  *
580Sstevel@tonic-gate  * For OBP-created ramdisks:
590Sstevel@tonic-gate  *
600Sstevel@tonic-gate  *	/dev/ramdisk/<diskname>  -> /devices/ramdisk-<diskname>:a
610Sstevel@tonic-gate  *	/dev/ramdisk/<diskname>  -> /devices/ramdisk-<diskname>:a,raw
620Sstevel@tonic-gate  *
630Sstevel@tonic-gate  * This allows the transition from the standalone to the kernel to proceed
640Sstevel@tonic-gate  * when booting from a ramdisk, and for the installation to correctly identify
650Sstevel@tonic-gate  * the root device.
660Sstevel@tonic-gate  */
670Sstevel@tonic-gate 
680Sstevel@tonic-gate #include <sys/types.h>
690Sstevel@tonic-gate #include <sys/param.h>
700Sstevel@tonic-gate #include <sys/sysmacros.h>
710Sstevel@tonic-gate #include <sys/errno.h>
720Sstevel@tonic-gate #include <sys/uio.h>
730Sstevel@tonic-gate #include <sys/buf.h>
740Sstevel@tonic-gate #include <sys/modctl.h>
750Sstevel@tonic-gate #include <sys/open.h>
760Sstevel@tonic-gate #include <sys/kmem.h>
770Sstevel@tonic-gate #include <sys/poll.h>
780Sstevel@tonic-gate #include <sys/conf.h>
790Sstevel@tonic-gate #include <sys/cmn_err.h>
800Sstevel@tonic-gate #include <sys/stat.h>
810Sstevel@tonic-gate #include <sys/file.h>
820Sstevel@tonic-gate #include <sys/ddi.h>
830Sstevel@tonic-gate #include <sys/sunddi.h>
840Sstevel@tonic-gate #include <sys/ramdisk.h>
850Sstevel@tonic-gate #include <vm/seg_kmem.h>
860Sstevel@tonic-gate 
870Sstevel@tonic-gate /*
88*9956SWilliam.Roche@Sun.COM  * Flag to disable the use of real ramdisks (in the OBP - on Sparc) when
89*9956SWilliam.Roche@Sun.COM  * the associated memory is no longer available - set in the bootops section.
90*9956SWilliam.Roche@Sun.COM  */
91*9956SWilliam.Roche@Sun.COM #ifdef __sparc
92*9956SWilliam.Roche@Sun.COM extern int bootops_obp_ramdisk_disabled;
93*9956SWilliam.Roche@Sun.COM #endif /* __sparc */
94*9956SWilliam.Roche@Sun.COM 
95*9956SWilliam.Roche@Sun.COM /*
960Sstevel@tonic-gate  * An opaque handle where information about our set of ramdisk devices lives.
970Sstevel@tonic-gate  */
980Sstevel@tonic-gate static void	*rd_statep;
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate /*
1010Sstevel@tonic-gate  * Pointer to devinfo for the 'pseudo' ramdisks.  Real OBP-created ramdisks
1020Sstevel@tonic-gate  * get their own individual devinfo.
1030Sstevel@tonic-gate  */
1040Sstevel@tonic-gate static dev_info_t *rd_dip = NULL;
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate /*
1070Sstevel@tonic-gate  * Global state lock.
1080Sstevel@tonic-gate  */
1090Sstevel@tonic-gate static kmutex_t	rd_lock;
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate /*
1120Sstevel@tonic-gate  * Maximum number of ramdisks supported by this driver.
1130Sstevel@tonic-gate  */
1140Sstevel@tonic-gate static uint32_t	rd_max_disks = RD_DFLT_DISKS;
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate /*
1170Sstevel@tonic-gate  * Percentage of physical memory which can be assigned to pseudo ramdisks,
1180Sstevel@tonic-gate  * what that equates to in pages, and how many pages are currently assigned.
1190Sstevel@tonic-gate  */
1200Sstevel@tonic-gate static uint_t	rd_percent_physmem = RD_DEFAULT_PERCENT_PHYSMEM;
1210Sstevel@tonic-gate static pgcnt_t	rd_max_physmem;
1220Sstevel@tonic-gate static pgcnt_t	rd_tot_physmem;
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate static uint_t	rd_maxphys = RD_DEFAULT_MAXPHYS;
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate /*
1270Sstevel@tonic-gate  * Is the driver busy, i.e. are there any pseudo ramdisk devices in existence?
1280Sstevel@tonic-gate  */
1290Sstevel@tonic-gate static int
rd_is_busy(void)1300Sstevel@tonic-gate rd_is_busy(void)
1310Sstevel@tonic-gate {
1320Sstevel@tonic-gate 	minor_t	minor;
1330Sstevel@tonic-gate 	rd_devstate_t	*rsp;
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate 	ASSERT(mutex_owned(&rd_lock));
1360Sstevel@tonic-gate 	for (minor = 1; minor <= rd_max_disks; ++minor) {
1370Sstevel@tonic-gate 		if ((rsp = ddi_get_soft_state(rd_statep, minor)) != NULL &&
1380Sstevel@tonic-gate 		    rsp->rd_dip == rd_dip) {
1390Sstevel@tonic-gate 			return (EBUSY);
1400Sstevel@tonic-gate 		}
1410Sstevel@tonic-gate 	}
1420Sstevel@tonic-gate 	return (0);
1430Sstevel@tonic-gate }
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate /*
1460Sstevel@tonic-gate  * Find the first free minor number; returns zero if there isn't one.
1470Sstevel@tonic-gate  */
1480Sstevel@tonic-gate static minor_t
rd_find_free_minor(void)1490Sstevel@tonic-gate rd_find_free_minor(void)
1500Sstevel@tonic-gate {
1510Sstevel@tonic-gate 	minor_t	minor;
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate 	ASSERT(mutex_owned(&rd_lock));
1540Sstevel@tonic-gate 	for (minor = 1; minor <= rd_max_disks; ++minor) {
1550Sstevel@tonic-gate 		if (ddi_get_soft_state(rd_statep, minor) == NULL) {
1560Sstevel@tonic-gate 			return (minor);
1570Sstevel@tonic-gate 		}
1580Sstevel@tonic-gate 	}
1590Sstevel@tonic-gate 	return (0);
1600Sstevel@tonic-gate }
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate /*
1630Sstevel@tonic-gate  * Locate the rd_devstate for the named ramdisk; returns NULL if not found.
1640Sstevel@tonic-gate  * Each ramdisk is identified uniquely by name, i.e. an OBP-created ramdisk
1650Sstevel@tonic-gate  * cannot have the same name as a pseudo ramdisk.
1660Sstevel@tonic-gate  */
1670Sstevel@tonic-gate static rd_devstate_t *
rd_find_named_disk(char * name)1680Sstevel@tonic-gate rd_find_named_disk(char *name)
1690Sstevel@tonic-gate {
1700Sstevel@tonic-gate 	minor_t		minor;
1710Sstevel@tonic-gate 	rd_devstate_t	*rsp;
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate 	ASSERT(mutex_owned(&rd_lock));
1740Sstevel@tonic-gate 	for (minor = 1; minor <= rd_max_disks; ++minor) {
1750Sstevel@tonic-gate 		if ((rsp = ddi_get_soft_state(rd_statep, minor)) != NULL &&
1760Sstevel@tonic-gate 		    strcmp(rsp->rd_name, name) == 0) {
1770Sstevel@tonic-gate 			return (rsp);
1780Sstevel@tonic-gate 		}
1790Sstevel@tonic-gate 	}
1800Sstevel@tonic-gate 	return (NULL);
1810Sstevel@tonic-gate }
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate /*
1840Sstevel@tonic-gate  * Locate the rd_devstate for the real OBP-created ramdisk whose devinfo
1850Sstevel@tonic-gate  * is referenced by 'dip'; returns NULL if not found (shouldn't happen).
1860Sstevel@tonic-gate  */
1870Sstevel@tonic-gate static rd_devstate_t *
rd_find_dip_state(dev_info_t * dip)1880Sstevel@tonic-gate rd_find_dip_state(dev_info_t *dip)
1890Sstevel@tonic-gate {
1900Sstevel@tonic-gate 	minor_t		minor;
1910Sstevel@tonic-gate 	rd_devstate_t	*rsp;
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate 	ASSERT(mutex_owned(&rd_lock));
1940Sstevel@tonic-gate 	for (minor = 1; minor <= rd_max_disks; ++minor) {
1950Sstevel@tonic-gate 		if ((rsp = ddi_get_soft_state(rd_statep, minor)) != NULL &&
1960Sstevel@tonic-gate 		    rsp->rd_dip == dip) {
1970Sstevel@tonic-gate 			return (rsp);
1980Sstevel@tonic-gate 		}
1990Sstevel@tonic-gate 	}
2000Sstevel@tonic-gate 	return (NULL);
2010Sstevel@tonic-gate }
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate /*
2040Sstevel@tonic-gate  * Is the ramdisk open?
2050Sstevel@tonic-gate  */
2060Sstevel@tonic-gate static int
rd_is_open(rd_devstate_t * rsp)2070Sstevel@tonic-gate rd_is_open(rd_devstate_t *rsp)
2080Sstevel@tonic-gate {
2090Sstevel@tonic-gate 	ASSERT(mutex_owned(&rd_lock));
2100Sstevel@tonic-gate 	return (rsp->rd_chr_open || rsp->rd_blk_open || rsp->rd_lyr_open_cnt);
2110Sstevel@tonic-gate }
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate /*
2140Sstevel@tonic-gate  * Mark the ramdisk open.
2150Sstevel@tonic-gate  */
2160Sstevel@tonic-gate static int
rd_opened(rd_devstate_t * rsp,int otyp)2170Sstevel@tonic-gate rd_opened(rd_devstate_t *rsp, int otyp)
2180Sstevel@tonic-gate {
2190Sstevel@tonic-gate 	ASSERT(mutex_owned(&rd_lock));
2200Sstevel@tonic-gate 	switch (otyp) {
2210Sstevel@tonic-gate 	case OTYP_CHR:
2220Sstevel@tonic-gate 		rsp->rd_chr_open = 1;
2230Sstevel@tonic-gate 		break;
2240Sstevel@tonic-gate 	case OTYP_BLK:
2250Sstevel@tonic-gate 		rsp->rd_blk_open = 1;
2260Sstevel@tonic-gate 		break;
2270Sstevel@tonic-gate 	case OTYP_LYR:
2280Sstevel@tonic-gate 		rsp->rd_lyr_open_cnt++;
2290Sstevel@tonic-gate 		break;
2300Sstevel@tonic-gate 	default:
2310Sstevel@tonic-gate 		return (-1);
2320Sstevel@tonic-gate 	}
2330Sstevel@tonic-gate 	return (0);
2340Sstevel@tonic-gate }
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate /*
2370Sstevel@tonic-gate  * Mark the ramdisk closed.
2380Sstevel@tonic-gate  */
2390Sstevel@tonic-gate static void
rd_closed(rd_devstate_t * rsp,int otyp)2400Sstevel@tonic-gate rd_closed(rd_devstate_t *rsp, int otyp)
2410Sstevel@tonic-gate {
2420Sstevel@tonic-gate 	ASSERT(mutex_owned(&rd_lock));
2430Sstevel@tonic-gate 	switch (otyp) {
2440Sstevel@tonic-gate 	case OTYP_CHR:
2450Sstevel@tonic-gate 		rsp->rd_chr_open = 0;
2460Sstevel@tonic-gate 		break;
2470Sstevel@tonic-gate 	case OTYP_BLK:
2480Sstevel@tonic-gate 		rsp->rd_blk_open = 0;
2490Sstevel@tonic-gate 		break;
2500Sstevel@tonic-gate 	case OTYP_LYR:
2510Sstevel@tonic-gate 		rsp->rd_lyr_open_cnt--;
2520Sstevel@tonic-gate 		break;
2530Sstevel@tonic-gate 	default:
2540Sstevel@tonic-gate 		break;
2550Sstevel@tonic-gate 	}
2560Sstevel@tonic-gate }
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate static void
rd_init_tuneables(void)2590Sstevel@tonic-gate rd_init_tuneables(void)
2600Sstevel@tonic-gate {
2610Sstevel@tonic-gate 	char	*prop, *p;
2620Sstevel@tonic-gate 
2630Sstevel@tonic-gate 	/*
2640Sstevel@tonic-gate 	 * Ensure sanity of 'rd_max_disks', which may be tuned in ramdisk.conf.
2650Sstevel@tonic-gate 	 */
2660Sstevel@tonic-gate 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, rd_dip, 0,
2670Sstevel@tonic-gate 	    "max_disks", &prop) == DDI_PROP_SUCCESS) {
2680Sstevel@tonic-gate 		p = prop;
2690Sstevel@tonic-gate 		rd_max_disks = (uint32_t)stoi(&p);
2700Sstevel@tonic-gate 		ddi_prop_free(prop);
2710Sstevel@tonic-gate 	}
2720Sstevel@tonic-gate 	if (rd_max_disks >= RD_MAX_DISKS) {
2730Sstevel@tonic-gate 		cmn_err(CE_WARN, "ramdisk: rd_max_disks (%u) too big;"
2740Sstevel@tonic-gate 		    " using default (%u).", rd_max_disks, RD_MAX_DISKS - 1);
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate 		rd_max_disks = RD_MAX_DISKS - 1;
2770Sstevel@tonic-gate 	}
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate 	/*
2800Sstevel@tonic-gate 	 * Ensure sanity of 'rd_percent_physmem', which may be tuned
2810Sstevel@tonic-gate 	 * in ramdisk.conf.
2820Sstevel@tonic-gate 	 */
2830Sstevel@tonic-gate 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, rd_dip, 0,
2840Sstevel@tonic-gate 	    "percent_physmem", &prop) == DDI_PROP_SUCCESS) {
2850Sstevel@tonic-gate 		p = prop;
2860Sstevel@tonic-gate 		rd_percent_physmem = (uint_t)stoi(&p);
2870Sstevel@tonic-gate 		ddi_prop_free(prop);
2880Sstevel@tonic-gate 	}
2890Sstevel@tonic-gate 	if (rd_percent_physmem >= 100) {
2900Sstevel@tonic-gate 		cmn_err(CE_WARN, "ramdisk: rd_percent_physmem (%u) >= 100;"
2910Sstevel@tonic-gate 		    " using default (%u%%).", rd_percent_physmem,
2920Sstevel@tonic-gate 		    RD_DEFAULT_PERCENT_PHYSMEM);
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate 		rd_percent_physmem = RD_DEFAULT_PERCENT_PHYSMEM;
2950Sstevel@tonic-gate 	}
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate 	/*
2984363Sblakej 	 * Since availrmem_initial is a long, this won't overflow.
2990Sstevel@tonic-gate 	 */
3004363Sblakej 	rd_max_physmem = (availrmem_initial * rd_percent_physmem) / 100;
3010Sstevel@tonic-gate }
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate /*
3044363Sblakej  * Allocate enough physical pages to hold "npages" pages.  Returns an
3050Sstevel@tonic-gate  * array of page_t * pointers that can later be mapped in or out via
3060Sstevel@tonic-gate  * rd_{un}map_window() but is otherwise opaque, or NULL on failure.
3070Sstevel@tonic-gate  */
3080Sstevel@tonic-gate page_t **
rd_phys_alloc(pgcnt_t npages)3090Sstevel@tonic-gate rd_phys_alloc(pgcnt_t npages)
3100Sstevel@tonic-gate {
3110Sstevel@tonic-gate 	page_t		*pp, **ppa;
3124363Sblakej 	spgcnt_t	i;
3134363Sblakej 	size_t		ppalen;
3140Sstevel@tonic-gate 	struct seg	kseg;
3154363Sblakej 	caddr_t		addr;		/* For coloring */
3164363Sblakej 
3174363Sblakej 	if (rd_tot_physmem + npages > rd_max_physmem)
3184363Sblakej 		return (NULL);
3190Sstevel@tonic-gate 
3204363Sblakej 	if (!page_resv(npages, KM_NOSLEEP))
3214363Sblakej 		return (NULL);
3224363Sblakej 
3234363Sblakej 	if (!page_create_wait(npages, 0)) {
3244363Sblakej 		page_unresv(npages);
3250Sstevel@tonic-gate 		return (NULL);
3260Sstevel@tonic-gate 	}
3270Sstevel@tonic-gate 
3284363Sblakej 	ppalen = npages * sizeof (struct page_t *);
3294363Sblakej 	ppa = kmem_zalloc(ppalen, KM_NOSLEEP);
3304363Sblakej 	if (ppa == NULL) {
3314363Sblakej 		page_create_putback(npages);
3324363Sblakej 		page_unresv(npages);
3334363Sblakej 		return (NULL);
3344363Sblakej 	}
3350Sstevel@tonic-gate 
3364363Sblakej 	kseg.s_as = &kas;
3374363Sblakej 	for (i = 0, addr = NULL; i < npages; ++i, addr += PAGESIZE) {
3384363Sblakej 		pp = page_get_freelist(&kvp, 0, &kseg, addr, PAGESIZE, 0, NULL);
3394363Sblakej 		if (pp == NULL) {
3404363Sblakej 			pp = page_get_cachelist(&kvp, 0, &kseg, addr, 0, NULL);
3414363Sblakej 			if (pp == NULL)
3420Sstevel@tonic-gate 				goto out;
3434363Sblakej 			if (!PP_ISAGED(pp))
3440Sstevel@tonic-gate 				page_hashout(pp, NULL);
3450Sstevel@tonic-gate 		}
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate 		PP_CLRFREE(pp);
3480Sstevel@tonic-gate 		PP_CLRAGED(pp);
3490Sstevel@tonic-gate 		ppa[i] = pp;
3500Sstevel@tonic-gate 	}
3514363Sblakej 
3524363Sblakej 	for (i = 0; i < npages; i++)
3534363Sblakej 		page_downgrade(ppa[i]);
3540Sstevel@tonic-gate 	rd_tot_physmem += npages;
3550Sstevel@tonic-gate 
3560Sstevel@tonic-gate 	return (ppa);
3574363Sblakej 
3580Sstevel@tonic-gate out:
3594363Sblakej 	ASSERT(i < npages);
3604363Sblakej 	page_create_putback(npages - i);
3614363Sblakej 	while (--i >= 0)
3620Sstevel@tonic-gate 		page_free(ppa[i], 0);
3630Sstevel@tonic-gate 	kmem_free(ppa, ppalen);
3640Sstevel@tonic-gate 	page_unresv(npages);
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate 	return (NULL);
3670Sstevel@tonic-gate }
3680Sstevel@tonic-gate 
3690Sstevel@tonic-gate /*
3700Sstevel@tonic-gate  * Free physical pages previously allocated via rd_phys_alloc(); note that
3710Sstevel@tonic-gate  * this function may block as it has to wait until it can exclusively lock
3720Sstevel@tonic-gate  * all the pages first.
3730Sstevel@tonic-gate  */
3740Sstevel@tonic-gate static void
rd_phys_free(page_t ** ppa,pgcnt_t npages)3750Sstevel@tonic-gate rd_phys_free(page_t **ppa, pgcnt_t npages)
3760Sstevel@tonic-gate {
3770Sstevel@tonic-gate 	pgcnt_t	i;
3780Sstevel@tonic-gate 	size_t	ppalen = npages * sizeof (struct page_t *);
3790Sstevel@tonic-gate 
3800Sstevel@tonic-gate 	for (i = 0; i < npages; ++i) {
3810Sstevel@tonic-gate 		if (! page_tryupgrade(ppa[i])) {
3820Sstevel@tonic-gate 			page_unlock(ppa[i]);
3830Sstevel@tonic-gate 			while (! page_lock(ppa[i], SE_EXCL, NULL, P_RECLAIM))
3840Sstevel@tonic-gate 				;
3850Sstevel@tonic-gate 		}
3860Sstevel@tonic-gate 		page_free(ppa[i], 0);
3870Sstevel@tonic-gate 	}
3880Sstevel@tonic-gate 
3890Sstevel@tonic-gate 	kmem_free(ppa, ppalen);
3900Sstevel@tonic-gate 
3910Sstevel@tonic-gate 	page_unresv(npages);
3920Sstevel@tonic-gate 	rd_tot_physmem -= npages;
3930Sstevel@tonic-gate }
3940Sstevel@tonic-gate 
3950Sstevel@tonic-gate /*
3960Sstevel@tonic-gate  * Remove a window mapping (if present).
3970Sstevel@tonic-gate  */
3980Sstevel@tonic-gate static void
rd_unmap_window(rd_devstate_t * rsp)3990Sstevel@tonic-gate rd_unmap_window(rd_devstate_t *rsp)
4000Sstevel@tonic-gate {
4015648Ssetje 	ASSERT(rsp->rd_window_obp == 0);
4020Sstevel@tonic-gate 	if (rsp->rd_window_base != RD_WINDOW_NOT_MAPPED) {
4030Sstevel@tonic-gate 		hat_unload(kas.a_hat, rsp->rd_window_virt, rsp->rd_window_size,
4040Sstevel@tonic-gate 		    HAT_UNLOAD_UNLOCK);
4050Sstevel@tonic-gate 	}
4060Sstevel@tonic-gate }
4070Sstevel@tonic-gate 
4080Sstevel@tonic-gate /*
4090Sstevel@tonic-gate  * Map a portion of the ramdisk into the virtual window.
4100Sstevel@tonic-gate  */
4110Sstevel@tonic-gate static void
rd_map_window(rd_devstate_t * rsp,off_t offset)4120Sstevel@tonic-gate rd_map_window(rd_devstate_t *rsp, off_t offset)
4130Sstevel@tonic-gate {
4140Sstevel@tonic-gate 	pgcnt_t	offpgs = btop(offset);
4150Sstevel@tonic-gate 
4160Sstevel@tonic-gate 	if (rsp->rd_window_base != RD_WINDOW_NOT_MAPPED) {
4170Sstevel@tonic-gate 		/*
4180Sstevel@tonic-gate 		 * Already mapped; is offset within our window?
4190Sstevel@tonic-gate 		 */
4200Sstevel@tonic-gate 		if (offset >= rsp->rd_window_base &&
4210Sstevel@tonic-gate 		    offset < rsp->rd_window_base + rsp->rd_window_size) {
4220Sstevel@tonic-gate 			return;
4230Sstevel@tonic-gate 		}
4240Sstevel@tonic-gate 
4250Sstevel@tonic-gate 		/*
4260Sstevel@tonic-gate 		 * No, we need to re-map; toss the old mapping.
4270Sstevel@tonic-gate 		 */
4280Sstevel@tonic-gate 		rd_unmap_window(rsp);
4290Sstevel@tonic-gate 	}
4300Sstevel@tonic-gate 	rsp->rd_window_base = ptob(offpgs);
4310Sstevel@tonic-gate 
4320Sstevel@tonic-gate 	/*
4330Sstevel@tonic-gate 	 * Different algorithms depending on whether this is a real
4340Sstevel@tonic-gate 	 * OBP-created ramdisk, or a pseudo ramdisk.
4350Sstevel@tonic-gate 	 */
4360Sstevel@tonic-gate 	if (rsp->rd_dip == rd_dip) {
4370Sstevel@tonic-gate 		pgcnt_t	pi, lastpi;
4380Sstevel@tonic-gate 		caddr_t	vaddr;
4390Sstevel@tonic-gate 
4400Sstevel@tonic-gate 		/*
4410Sstevel@tonic-gate 		 * Find the range of pages which should be mapped.
4420Sstevel@tonic-gate 		 */
4430Sstevel@tonic-gate 		pi = offpgs;
4440Sstevel@tonic-gate 		lastpi = pi + btopr(rsp->rd_window_size);
4450Sstevel@tonic-gate 		if (lastpi > rsp->rd_npages) {
4460Sstevel@tonic-gate 			lastpi = rsp->rd_npages;
4470Sstevel@tonic-gate 		}
4480Sstevel@tonic-gate 
4490Sstevel@tonic-gate 		/*
4500Sstevel@tonic-gate 		 * Load the mapping.
4510Sstevel@tonic-gate 		 */
4520Sstevel@tonic-gate 		vaddr = rsp->rd_window_virt;
4530Sstevel@tonic-gate 		for (; pi < lastpi; ++pi) {
4540Sstevel@tonic-gate 			hat_memload(kas.a_hat, vaddr, rsp->rd_ppa[pi],
4550Sstevel@tonic-gate 			    (PROT_READ | PROT_WRITE) | HAT_NOSYNC,
4560Sstevel@tonic-gate 			    HAT_LOAD_LOCK);
4570Sstevel@tonic-gate 			vaddr += ptob(1);
4580Sstevel@tonic-gate 		}
4590Sstevel@tonic-gate 	} else {
4600Sstevel@tonic-gate 		uint_t	i;
4610Sstevel@tonic-gate 		pfn_t	pfn;
4620Sstevel@tonic-gate 
4630Sstevel@tonic-gate 		/*
4640Sstevel@tonic-gate 		 * Real OBP-created ramdisk: locate the physical range which
4650Sstevel@tonic-gate 		 * contains this offset.
4660Sstevel@tonic-gate 		 */
4670Sstevel@tonic-gate 		for (i = 0; i < rsp->rd_nexisting; ++i) {
4680Sstevel@tonic-gate 			if (offset < rsp->rd_existing[i].size) {
4690Sstevel@tonic-gate 				break;
4700Sstevel@tonic-gate 			}
4710Sstevel@tonic-gate 			offset -= rsp->rd_existing[i].size;
4720Sstevel@tonic-gate 		}
4730Sstevel@tonic-gate 		ASSERT(i < rsp->rd_nexisting);
4740Sstevel@tonic-gate 
4750Sstevel@tonic-gate 		/*
4760Sstevel@tonic-gate 		 * Load the mapping.
4770Sstevel@tonic-gate 		 */
4780Sstevel@tonic-gate 		pfn = btop(rsp->rd_existing[i].phys + offset);
4790Sstevel@tonic-gate 		hat_devload(kas.a_hat, rsp->rd_window_virt, rsp->rd_window_size,
4800Sstevel@tonic-gate 		    pfn, (PROT_READ | PROT_WRITE),
4810Sstevel@tonic-gate 		    HAT_LOAD_NOCONSIST | HAT_LOAD_LOCK);
4820Sstevel@tonic-gate 	}
4830Sstevel@tonic-gate }
4840Sstevel@tonic-gate 
4850Sstevel@tonic-gate /*
4860Sstevel@tonic-gate  * Fakes up a disk geometry, and one big partition, based on the size
4870Sstevel@tonic-gate  * of the file. This is needed because we allow newfs'ing the device,
4880Sstevel@tonic-gate  * and newfs will do several disk ioctls to figure out the geometry and
4890Sstevel@tonic-gate  * partition information. It uses that information to determine the parameters
4903517Smp204432  * to pass to mkfs. Geometry is pretty much irrelevant these days, but we
4910Sstevel@tonic-gate  * have to support it.
4920Sstevel@tonic-gate  *
4930Sstevel@tonic-gate  * Stolen from lofi.c - should maybe split out common code sometime.
4940Sstevel@tonic-gate  */
4950Sstevel@tonic-gate static void
rd_fake_disk_geometry(rd_devstate_t * rsp)4960Sstevel@tonic-gate rd_fake_disk_geometry(rd_devstate_t *rsp)
4970Sstevel@tonic-gate {
4980Sstevel@tonic-gate 	/* dk_geom - see dkio(7I) */
4990Sstevel@tonic-gate 	/*
5000Sstevel@tonic-gate 	 * dkg_ncyl _could_ be set to one here (one big cylinder with gobs
5010Sstevel@tonic-gate 	 * of sectors), but that breaks programs like fdisk which want to
5020Sstevel@tonic-gate 	 * partition a disk by cylinder. With one cylinder, you can't create
5030Sstevel@tonic-gate 	 * an fdisk partition and put pcfs on it for testing (hard to pick
5040Sstevel@tonic-gate 	 * a number between one and one).
5050Sstevel@tonic-gate 	 *
5060Sstevel@tonic-gate 	 * The cheezy floppy test is an attempt to not have too few cylinders
5070Sstevel@tonic-gate 	 * for a small file, or so many on a big file that you waste space
5080Sstevel@tonic-gate 	 * for backup superblocks or cylinder group structures.
5090Sstevel@tonic-gate 	 */
5100Sstevel@tonic-gate 	if (rsp->rd_size < (2 * 1024 * 1024)) /* floppy? */
5110Sstevel@tonic-gate 		rsp->rd_dkg.dkg_ncyl = rsp->rd_size / (100 * 1024);
5120Sstevel@tonic-gate 	else
5130Sstevel@tonic-gate 		rsp->rd_dkg.dkg_ncyl = rsp->rd_size / (300 * 1024);
5140Sstevel@tonic-gate 	/* in case file file is < 100k */
5150Sstevel@tonic-gate 	if (rsp->rd_dkg.dkg_ncyl == 0)
5160Sstevel@tonic-gate 		rsp->rd_dkg.dkg_ncyl = 1;
5170Sstevel@tonic-gate 	rsp->rd_dkg.dkg_acyl = 0;
5180Sstevel@tonic-gate 	rsp->rd_dkg.dkg_bcyl = 0;
5190Sstevel@tonic-gate 	rsp->rd_dkg.dkg_nhead = 1;
5200Sstevel@tonic-gate 	rsp->rd_dkg.dkg_obs1 = 0;
5210Sstevel@tonic-gate 	rsp->rd_dkg.dkg_intrlv = 0;
5220Sstevel@tonic-gate 	rsp->rd_dkg.dkg_obs2 = 0;
5230Sstevel@tonic-gate 	rsp->rd_dkg.dkg_obs3 = 0;
5240Sstevel@tonic-gate 	rsp->rd_dkg.dkg_apc = 0;
5250Sstevel@tonic-gate 	rsp->rd_dkg.dkg_rpm = 7200;
5260Sstevel@tonic-gate 	rsp->rd_dkg.dkg_pcyl = rsp->rd_dkg.dkg_ncyl + rsp->rd_dkg.dkg_acyl;
5270Sstevel@tonic-gate 	rsp->rd_dkg.dkg_nsect = rsp->rd_size /
5280Sstevel@tonic-gate 	    (DEV_BSIZE * rsp->rd_dkg.dkg_ncyl);
5290Sstevel@tonic-gate 	rsp->rd_dkg.dkg_write_reinstruct = 0;
5300Sstevel@tonic-gate 	rsp->rd_dkg.dkg_read_reinstruct = 0;
5310Sstevel@tonic-gate 
5320Sstevel@tonic-gate 	/* vtoc - see dkio(7I) */
5330Sstevel@tonic-gate 	bzero(&rsp->rd_vtoc, sizeof (struct vtoc));
5340Sstevel@tonic-gate 	rsp->rd_vtoc.v_sanity = VTOC_SANE;
5350Sstevel@tonic-gate 	rsp->rd_vtoc.v_version = V_VERSION;
5360Sstevel@tonic-gate 	bcopy(RD_DRIVER_NAME, rsp->rd_vtoc.v_volume, 7);
5370Sstevel@tonic-gate 	rsp->rd_vtoc.v_sectorsz = DEV_BSIZE;
5380Sstevel@tonic-gate 	rsp->rd_vtoc.v_nparts = 1;
5390Sstevel@tonic-gate 	rsp->rd_vtoc.v_part[0].p_tag = V_UNASSIGNED;
5400Sstevel@tonic-gate 	rsp->rd_vtoc.v_part[0].p_flag = V_UNMNT;
5410Sstevel@tonic-gate 	rsp->rd_vtoc.v_part[0].p_start = (daddr_t)0;
5420Sstevel@tonic-gate 	/*
5430Sstevel@tonic-gate 	 * The partition size cannot just be the number of sectors, because
5440Sstevel@tonic-gate 	 * that might not end on a cylinder boundary. And if that's the case,
5450Sstevel@tonic-gate 	 * newfs/mkfs will print a scary warning. So just figure the size
5460Sstevel@tonic-gate 	 * based on the number of cylinders and sectors/cylinder.
5470Sstevel@tonic-gate 	 */
5480Sstevel@tonic-gate 	rsp->rd_vtoc.v_part[0].p_size = rsp->rd_dkg.dkg_pcyl *
5490Sstevel@tonic-gate 	    rsp->rd_dkg.dkg_nsect * rsp->rd_dkg.dkg_nhead;
5500Sstevel@tonic-gate 
5510Sstevel@tonic-gate 	/* dk_cinfo - see dkio(7I) */
5520Sstevel@tonic-gate 	bzero(&rsp->rd_ci, sizeof (struct dk_cinfo));
5530Sstevel@tonic-gate 	(void) strcpy(rsp->rd_ci.dki_cname, RD_DRIVER_NAME);
5540Sstevel@tonic-gate 	rsp->rd_ci.dki_ctype = DKC_MD;
5550Sstevel@tonic-gate 	rsp->rd_ci.dki_flags = 0;
5560Sstevel@tonic-gate 	rsp->rd_ci.dki_cnum = 0;
5570Sstevel@tonic-gate 	rsp->rd_ci.dki_addr = 0;
5580Sstevel@tonic-gate 	rsp->rd_ci.dki_space = 0;
5590Sstevel@tonic-gate 	rsp->rd_ci.dki_prio = 0;
5600Sstevel@tonic-gate 	rsp->rd_ci.dki_vec = 0;
5610Sstevel@tonic-gate 	(void) strcpy(rsp->rd_ci.dki_dname, RD_DRIVER_NAME);
5620Sstevel@tonic-gate 	rsp->rd_ci.dki_unit = 0;
5630Sstevel@tonic-gate 	rsp->rd_ci.dki_slave = 0;
5640Sstevel@tonic-gate 	rsp->rd_ci.dki_partition = 0;
5650Sstevel@tonic-gate 	/*
5660Sstevel@tonic-gate 	 * newfs uses this to set maxcontig. Must not be < 16, or it
5670Sstevel@tonic-gate 	 * will be 0 when newfs multiplies it by DEV_BSIZE and divides
5680Sstevel@tonic-gate 	 * it by the block size. Then tunefs doesn't work because
5690Sstevel@tonic-gate 	 * maxcontig is 0.
5700Sstevel@tonic-gate 	 */
5710Sstevel@tonic-gate 	rsp->rd_ci.dki_maxtransfer = 16;
5720Sstevel@tonic-gate }
5730Sstevel@tonic-gate 
5740Sstevel@tonic-gate /*
5750Sstevel@tonic-gate  * Deallocate resources (virtual and physical, device nodes, structures)
5760Sstevel@tonic-gate  * from a ramdisk.
5770Sstevel@tonic-gate  */
5780Sstevel@tonic-gate static void
rd_dealloc_resources(rd_devstate_t * rsp)5790Sstevel@tonic-gate rd_dealloc_resources(rd_devstate_t *rsp)
5800Sstevel@tonic-gate {
5810Sstevel@tonic-gate 	dev_info_t	*dip = rsp->rd_dip;
5820Sstevel@tonic-gate 	char		namebuf[RD_NAME_LEN + 5];
5830Sstevel@tonic-gate 	dev_t		fulldev;
5840Sstevel@tonic-gate 
5855648Ssetje 	if (rsp->rd_window_obp == 0 && rsp->rd_window_virt != NULL) {
5860Sstevel@tonic-gate 		if (rsp->rd_window_base != RD_WINDOW_NOT_MAPPED) {
5870Sstevel@tonic-gate 			rd_unmap_window(rsp);
5880Sstevel@tonic-gate 		}
5890Sstevel@tonic-gate 		vmem_free(heap_arena, rsp->rd_window_virt, rsp->rd_window_size);
5900Sstevel@tonic-gate 	}
5910Sstevel@tonic-gate 	mutex_destroy(&rsp->rd_device_lock);
5920Sstevel@tonic-gate 
5930Sstevel@tonic-gate 	if (rsp->rd_existing) {
5940Sstevel@tonic-gate 		ddi_prop_free(rsp->rd_existing);
5950Sstevel@tonic-gate 	}
5960Sstevel@tonic-gate 	if (rsp->rd_ppa != NULL) {
5970Sstevel@tonic-gate 		rd_phys_free(rsp->rd_ppa, rsp->rd_npages);
5980Sstevel@tonic-gate 	}
5990Sstevel@tonic-gate 
6000Sstevel@tonic-gate 	/*
6010Sstevel@tonic-gate 	 * Remove the block and raw device nodes.
6020Sstevel@tonic-gate 	 */
6030Sstevel@tonic-gate 	if (dip == rd_dip) {
6040Sstevel@tonic-gate 		(void) snprintf(namebuf, sizeof (namebuf), "%s",
6050Sstevel@tonic-gate 		    rsp->rd_name);
6060Sstevel@tonic-gate 		ddi_remove_minor_node(dip, namebuf);
6070Sstevel@tonic-gate 		(void) snprintf(namebuf, sizeof (namebuf), "%s,raw",
6080Sstevel@tonic-gate 		    rsp->rd_name);
6090Sstevel@tonic-gate 		ddi_remove_minor_node(dip, namebuf);
6100Sstevel@tonic-gate 	} else {
6110Sstevel@tonic-gate 		ddi_remove_minor_node(dip, "a");
6120Sstevel@tonic-gate 		ddi_remove_minor_node(dip, "a,raw");
6130Sstevel@tonic-gate 	}
6140Sstevel@tonic-gate 
6150Sstevel@tonic-gate 	/*
6160Sstevel@tonic-gate 	 * Remove the "Size" and "Nblocks" properties.
6170Sstevel@tonic-gate 	 */
6180Sstevel@tonic-gate 	fulldev = makedevice(ddi_driver_major(dip), rsp->rd_minor);
6190Sstevel@tonic-gate 	(void) ddi_prop_remove(fulldev, dip, SIZE_PROP_NAME);
6200Sstevel@tonic-gate 	(void) ddi_prop_remove(fulldev, dip, NBLOCKS_PROP_NAME);
6210Sstevel@tonic-gate 
6220Sstevel@tonic-gate 	if (rsp->rd_kstat) {
6230Sstevel@tonic-gate 		kstat_delete(rsp->rd_kstat);
6240Sstevel@tonic-gate 		mutex_destroy(&rsp->rd_kstat_lock);
6250Sstevel@tonic-gate 	}
6260Sstevel@tonic-gate 
6270Sstevel@tonic-gate 	ddi_soft_state_free(rd_statep, rsp->rd_minor);
6280Sstevel@tonic-gate }
6290Sstevel@tonic-gate 
6300Sstevel@tonic-gate /*
6310Sstevel@tonic-gate  * Allocate resources (virtual and physical, device nodes, structures)
6320Sstevel@tonic-gate  * to a ramdisk.
6330Sstevel@tonic-gate  */
6340Sstevel@tonic-gate static rd_devstate_t *
rd_alloc_resources(char * name,uint_t addr,size_t size,dev_info_t * dip)6355648Ssetje rd_alloc_resources(char *name, uint_t addr, size_t size, dev_info_t *dip)
6360Sstevel@tonic-gate {
6370Sstevel@tonic-gate 	minor_t		minor;
6380Sstevel@tonic-gate 	rd_devstate_t	*rsp;
6390Sstevel@tonic-gate 	char		namebuf[RD_NAME_LEN + 5];
6400Sstevel@tonic-gate 	dev_t		fulldev;
6410Sstevel@tonic-gate 	int64_t		Nblocks_prop_val;
6420Sstevel@tonic-gate 	int64_t		Size_prop_val;
6430Sstevel@tonic-gate 
6440Sstevel@tonic-gate 	minor = rd_find_free_minor();
6450Sstevel@tonic-gate 	if (ddi_soft_state_zalloc(rd_statep, minor) == DDI_FAILURE) {
6460Sstevel@tonic-gate 		return (NULL);
6470Sstevel@tonic-gate 	}
6480Sstevel@tonic-gate 	rsp = ddi_get_soft_state(rd_statep, minor);
6490Sstevel@tonic-gate 
6500Sstevel@tonic-gate 	(void) strcpy(rsp->rd_name, name);
6510Sstevel@tonic-gate 	rsp->rd_dip = dip;
6520Sstevel@tonic-gate 	rsp->rd_minor = minor;
6530Sstevel@tonic-gate 	rsp->rd_size = size;
6540Sstevel@tonic-gate 
6550Sstevel@tonic-gate 	/*
6560Sstevel@tonic-gate 	 * Allocate virtual window onto ramdisk.
6570Sstevel@tonic-gate 	 */
6580Sstevel@tonic-gate 	mutex_init(&rsp->rd_device_lock, NULL, MUTEX_DRIVER, NULL);
6595648Ssetje 	if (addr == 0) {
6605648Ssetje 		rsp->rd_window_obp = 0;
6615648Ssetje 		rsp->rd_window_base = RD_WINDOW_NOT_MAPPED;
6625648Ssetje 		rsp->rd_window_size = PAGESIZE;
6635648Ssetje 		rsp->rd_window_virt = vmem_alloc(heap_arena,
6645648Ssetje 		    rsp->rd_window_size, VM_SLEEP);
6655648Ssetje 		if (rsp->rd_window_virt == NULL) {
6665648Ssetje 			goto create_failed;
6675648Ssetje 		}
6685648Ssetje 	} else {
6695648Ssetje 		rsp->rd_window_obp = 1;
6705648Ssetje 		rsp->rd_window_base = 0;
6715648Ssetje 		rsp->rd_window_size = size;
6725648Ssetje 		rsp->rd_window_virt = (caddr_t)((ulong_t)addr);
6730Sstevel@tonic-gate 	}
6740Sstevel@tonic-gate 
6750Sstevel@tonic-gate 	/*
6760Sstevel@tonic-gate 	 * Allocate physical memory for non-OBP ramdisks.
6770Sstevel@tonic-gate 	 * Create pseudo block and raw device nodes.
6780Sstevel@tonic-gate 	 */
6790Sstevel@tonic-gate 	if (dip == rd_dip) {
6800Sstevel@tonic-gate 		rsp->rd_npages = btopr(size);
6810Sstevel@tonic-gate 		rsp->rd_ppa = rd_phys_alloc(rsp->rd_npages);
6820Sstevel@tonic-gate 		if (rsp->rd_ppa == NULL) {
6830Sstevel@tonic-gate 			goto create_failed;
6840Sstevel@tonic-gate 		}
6850Sstevel@tonic-gate 
6860Sstevel@tonic-gate 		/*
6870Sstevel@tonic-gate 		 * For non-OBP ramdisks the device nodes are:
6880Sstevel@tonic-gate 		 *
6890Sstevel@tonic-gate 		 *	/devices/pseudo/ramdisk@0:<diskname>
6900Sstevel@tonic-gate 		 *	/devices/pseudo/ramdisk@0:<diskname>,raw
6910Sstevel@tonic-gate 		 */
6920Sstevel@tonic-gate 		(void) snprintf(namebuf, sizeof (namebuf), "%s",
6930Sstevel@tonic-gate 		    rsp->rd_name);
6940Sstevel@tonic-gate 		if (ddi_create_minor_node(dip, namebuf, S_IFBLK, minor,
6950Sstevel@tonic-gate 		    DDI_PSEUDO, 0) == DDI_FAILURE) {
6960Sstevel@tonic-gate 			goto create_failed;
6970Sstevel@tonic-gate 		}
6980Sstevel@tonic-gate 		(void) snprintf(namebuf, sizeof (namebuf), "%s,raw",
6990Sstevel@tonic-gate 		    rsp->rd_name);
7000Sstevel@tonic-gate 		if (ddi_create_minor_node(dip, namebuf, S_IFCHR, minor,
7010Sstevel@tonic-gate 		    DDI_PSEUDO, 0) == DDI_FAILURE) {
7020Sstevel@tonic-gate 			goto create_failed;
7030Sstevel@tonic-gate 		}
7040Sstevel@tonic-gate 	} else {
7050Sstevel@tonic-gate 		/*
7060Sstevel@tonic-gate 		 * For OBP-created ramdisks the device nodes are:
7070Sstevel@tonic-gate 		 *
7080Sstevel@tonic-gate 		 *	/devices/ramdisk-<diskname>:a
7090Sstevel@tonic-gate 		 *	/devices/ramdisk-<diskname>:a,raw
7100Sstevel@tonic-gate 		 */
7110Sstevel@tonic-gate 		if (ddi_create_minor_node(dip, "a", S_IFBLK, minor,
7120Sstevel@tonic-gate 		    DDI_PSEUDO, 0) == DDI_FAILURE) {
7130Sstevel@tonic-gate 			goto create_failed;
7140Sstevel@tonic-gate 		}
7150Sstevel@tonic-gate 		if (ddi_create_minor_node(dip, "a,raw", S_IFCHR, minor,
7160Sstevel@tonic-gate 		    DDI_PSEUDO, 0) == DDI_FAILURE) {
7170Sstevel@tonic-gate 			goto create_failed;
7180Sstevel@tonic-gate 		}
7190Sstevel@tonic-gate 	}
7200Sstevel@tonic-gate 
7210Sstevel@tonic-gate 	/*
7220Sstevel@tonic-gate 	 * Create the "Size" and "Nblocks" properties.
7230Sstevel@tonic-gate 	 */
7240Sstevel@tonic-gate 	fulldev = makedevice(ddi_driver_major(dip), minor);
7250Sstevel@tonic-gate 	Size_prop_val = size;
7260Sstevel@tonic-gate 	if ((ddi_prop_update_int64(fulldev, dip,
7270Sstevel@tonic-gate 	    SIZE_PROP_NAME, Size_prop_val)) != DDI_PROP_SUCCESS) {
7280Sstevel@tonic-gate 		goto create_failed;
7290Sstevel@tonic-gate 	}
7300Sstevel@tonic-gate 	Nblocks_prop_val = size / DEV_BSIZE;
7310Sstevel@tonic-gate 	if ((ddi_prop_update_int64(fulldev, dip,
7320Sstevel@tonic-gate 	    NBLOCKS_PROP_NAME, Nblocks_prop_val)) != DDI_PROP_SUCCESS) {
7330Sstevel@tonic-gate 		goto create_failed;
7340Sstevel@tonic-gate 	}
7350Sstevel@tonic-gate 
7360Sstevel@tonic-gate 	/*
7370Sstevel@tonic-gate 	 * Allocate kstat stuff.
7380Sstevel@tonic-gate 	 */
7390Sstevel@tonic-gate 	rsp->rd_kstat = kstat_create(RD_DRIVER_NAME, minor, NULL,
7404363Sblakej 	    "disk", KSTAT_TYPE_IO, 1, 0);
7410Sstevel@tonic-gate 	if (rsp->rd_kstat) {
7420Sstevel@tonic-gate 		mutex_init(&rsp->rd_kstat_lock, NULL,
7430Sstevel@tonic-gate 		    MUTEX_DRIVER, NULL);
7440Sstevel@tonic-gate 		rsp->rd_kstat->ks_lock = &rsp->rd_kstat_lock;
7450Sstevel@tonic-gate 		kstat_install(rsp->rd_kstat);
7460Sstevel@tonic-gate 	}
7470Sstevel@tonic-gate 
7480Sstevel@tonic-gate 	rd_fake_disk_geometry(rsp);
7490Sstevel@tonic-gate 
7500Sstevel@tonic-gate 	return (rsp);
7510Sstevel@tonic-gate 
7520Sstevel@tonic-gate create_failed:
7530Sstevel@tonic-gate 	/*
7540Sstevel@tonic-gate 	 * Cleanup.
7550Sstevel@tonic-gate 	 */
7560Sstevel@tonic-gate 	rd_dealloc_resources(rsp);
7570Sstevel@tonic-gate 
7580Sstevel@tonic-gate 	return (NULL);
7590Sstevel@tonic-gate }
7600Sstevel@tonic-gate 
7610Sstevel@tonic-gate /*
7620Sstevel@tonic-gate  * Undo what we did in rd_attach, freeing resources and removing things which
7630Sstevel@tonic-gate  * we installed.  The system framework guarantees we are not active with this
7640Sstevel@tonic-gate  * devinfo node in any other entry points at this time.
7650Sstevel@tonic-gate  */
7660Sstevel@tonic-gate static int
rd_common_detach(dev_info_t * dip)7670Sstevel@tonic-gate rd_common_detach(dev_info_t *dip)
7680Sstevel@tonic-gate {
7690Sstevel@tonic-gate 	if (dip == rd_dip) {
7700Sstevel@tonic-gate 		/*
7710Sstevel@tonic-gate 		 * Pseudo node: can't detach if any pseudo ramdisks exist.
7720Sstevel@tonic-gate 		 */
7730Sstevel@tonic-gate 		if (rd_is_busy()) {
7740Sstevel@tonic-gate 			return (DDI_FAILURE);
7750Sstevel@tonic-gate 		}
7760Sstevel@tonic-gate 		ddi_soft_state_free(rd_statep, RD_CTL_MINOR);
7770Sstevel@tonic-gate 		rd_dip = NULL;
7780Sstevel@tonic-gate 	} else {
7790Sstevel@tonic-gate 		/*
7800Sstevel@tonic-gate 		 * A 'real' ramdisk; find the state and free resources.
7810Sstevel@tonic-gate 		 */
7820Sstevel@tonic-gate 		rd_devstate_t	*rsp;
7830Sstevel@tonic-gate 
7840Sstevel@tonic-gate 		if ((rsp = rd_find_dip_state(dip)) != NULL) {
7850Sstevel@tonic-gate 			rd_dealloc_resources(rsp);
7860Sstevel@tonic-gate 		}
7870Sstevel@tonic-gate 	}
7880Sstevel@tonic-gate 	ddi_remove_minor_node(dip, NULL);
7890Sstevel@tonic-gate 
7900Sstevel@tonic-gate 	return (DDI_SUCCESS);
7910Sstevel@tonic-gate }
7920Sstevel@tonic-gate 
7930Sstevel@tonic-gate static int
rd_attach(dev_info_t * dip,ddi_attach_cmd_t cmd)7940Sstevel@tonic-gate rd_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
7950Sstevel@tonic-gate {
7960Sstevel@tonic-gate 	char		*name;
7970Sstevel@tonic-gate 	rd_existing_t	*ep = NULL;
7985648Ssetje 	uint_t		obpaddr = 0, nep, i;
7990Sstevel@tonic-gate 	size_t		size = 0;
8000Sstevel@tonic-gate 	rd_devstate_t	*rsp;
8010Sstevel@tonic-gate 
8020Sstevel@tonic-gate 	switch (cmd) {
8030Sstevel@tonic-gate 
8040Sstevel@tonic-gate 	case DDI_ATTACH:
8050Sstevel@tonic-gate 		mutex_enter(&rd_lock);
8060Sstevel@tonic-gate 
8070Sstevel@tonic-gate 		/*
8080Sstevel@tonic-gate 		 * For pseudo ramdisk devinfo set up state 0 and :ctl device;
8090Sstevel@tonic-gate 		 * else it's an OBP-created ramdisk.
8100Sstevel@tonic-gate 		 */
8110Sstevel@tonic-gate 		if (is_pseudo_device(dip)) {
8120Sstevel@tonic-gate 			rd_dip = dip;
8130Sstevel@tonic-gate 			rd_init_tuneables();
8140Sstevel@tonic-gate 
8150Sstevel@tonic-gate 			/*
8160Sstevel@tonic-gate 			 * The zeroth minor is reserved for the ramdisk
8170Sstevel@tonic-gate 			 * 'control' device.
8180Sstevel@tonic-gate 			 */
8190Sstevel@tonic-gate 			if (ddi_soft_state_zalloc(rd_statep, RD_CTL_MINOR) ==
8200Sstevel@tonic-gate 			    DDI_FAILURE) {
8210Sstevel@tonic-gate 				goto attach_failed;
8220Sstevel@tonic-gate 			}
8230Sstevel@tonic-gate 			rsp = ddi_get_soft_state(rd_statep, RD_CTL_MINOR);
8240Sstevel@tonic-gate 			rsp->rd_dip = dip;
8250Sstevel@tonic-gate 
8260Sstevel@tonic-gate 			if (ddi_create_minor_node(dip, RD_CTL_NODE,
8270Sstevel@tonic-gate 			    S_IFCHR, 0, DDI_PSEUDO, NULL) == DDI_FAILURE) {
8280Sstevel@tonic-gate 				goto attach_failed;
8290Sstevel@tonic-gate 			}
8300Sstevel@tonic-gate 		} else {
831*9956SWilliam.Roche@Sun.COM #ifdef __sparc
832*9956SWilliam.Roche@Sun.COM 			if (bootops_obp_ramdisk_disabled)
833*9956SWilliam.Roche@Sun.COM 				goto attach_failed;
834*9956SWilliam.Roche@Sun.COM #endif /* __sparc */
835*9956SWilliam.Roche@Sun.COM 
8360Sstevel@tonic-gate 			RD_STRIP_PREFIX(name, ddi_node_name(dip));
8370Sstevel@tonic-gate 
8380Sstevel@tonic-gate 			if (strlen(name) > RD_NAME_LEN) {
8390Sstevel@tonic-gate 				cmn_err(CE_CONT,
8400Sstevel@tonic-gate 				    "%s: name too long - ignoring\n", name);
8410Sstevel@tonic-gate 				goto attach_failed;
8420Sstevel@tonic-gate 			}
8430Sstevel@tonic-gate 
8440Sstevel@tonic-gate 			/*
8450Sstevel@tonic-gate 			 * An OBP-created ramdisk must have an 'existing'
8460Sstevel@tonic-gate 			 * property; get and check it.
8470Sstevel@tonic-gate 			 */
8480Sstevel@tonic-gate 			if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, dip,
8495648Ssetje 			    DDI_PROP_DONTPASS, OBP_EXISTING_PROP_NAME,
8505648Ssetje 			    (uchar_t **)&ep, &nep) == DDI_SUCCESS) {
8515648Ssetje 
8525648Ssetje 				if (nep == 0 || (nep % sizeof (*ep)) != 0) {
8535648Ssetje 					cmn_err(CE_CONT,
8545648Ssetje 					    "%s: " OBP_EXISTING_PROP_NAME
8555648Ssetje 					    " illegal size\n", name);
8565648Ssetje 					goto attach_failed;
8575648Ssetje 				}
8585648Ssetje 				nep /= sizeof (*ep);
8595648Ssetje 
8605648Ssetje 				/*
8615648Ssetje 				 * Calculate the size of the ramdisk.
8625648Ssetje 				 */
8635648Ssetje 				for (i = 0; i < nep; ++i) {
8645648Ssetje 					size += ep[i].size;
8655648Ssetje 				}
8665648Ssetje 			} else if ((obpaddr = ddi_prop_get_int(DDI_DEV_T_ANY,
8675648Ssetje 			    dip, DDI_PROP_DONTPASS, OBP_ADDRESS_PROP_NAME,
8685648Ssetje 			    0)) != 0)  {
8695648Ssetje 
8705648Ssetje 				size = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
8715648Ssetje 				    DDI_PROP_DONTPASS, OBP_SIZE_PROP_NAME, 0);
8725648Ssetje 			} else {
8735648Ssetje 				cmn_err(CE_CONT, "%s: missing OBP properties\n",
8745648Ssetje 				    name);
8750Sstevel@tonic-gate 				goto attach_failed;
8760Sstevel@tonic-gate 			}
8770Sstevel@tonic-gate 
8780Sstevel@tonic-gate 			/*
8790Sstevel@tonic-gate 			 * Allocate driver resources for the ramdisk.
8800Sstevel@tonic-gate 			 */
8815648Ssetje 			if ((rsp = rd_alloc_resources(name, obpaddr, size,
8820Sstevel@tonic-gate 			    dip)) == NULL) {
8830Sstevel@tonic-gate 				goto attach_failed;
8840Sstevel@tonic-gate 			}
8850Sstevel@tonic-gate 
8860Sstevel@tonic-gate 			rsp->rd_existing = ep;
8870Sstevel@tonic-gate 			rsp->rd_nexisting = nep;
8880Sstevel@tonic-gate 		}
8890Sstevel@tonic-gate 
8900Sstevel@tonic-gate 		mutex_exit(&rd_lock);
8910Sstevel@tonic-gate 
8920Sstevel@tonic-gate 		ddi_report_dev(dip);
8930Sstevel@tonic-gate 
8940Sstevel@tonic-gate 		return (DDI_SUCCESS);
8950Sstevel@tonic-gate 
8960Sstevel@tonic-gate 	case DDI_RESUME:
8970Sstevel@tonic-gate 		return (DDI_SUCCESS);
8980Sstevel@tonic-gate 
8990Sstevel@tonic-gate 	default:
9000Sstevel@tonic-gate 		return (DDI_FAILURE);
9010Sstevel@tonic-gate 	}
9020Sstevel@tonic-gate 
9030Sstevel@tonic-gate attach_failed:
9040Sstevel@tonic-gate 	/*
9050Sstevel@tonic-gate 	 * Use our common detach routine to unallocate any stuff which
9060Sstevel@tonic-gate 	 * was allocated above.
9070Sstevel@tonic-gate 	 */
9080Sstevel@tonic-gate 	(void) rd_common_detach(dip);
9090Sstevel@tonic-gate 	mutex_exit(&rd_lock);
9100Sstevel@tonic-gate 
9110Sstevel@tonic-gate 	if (ep != NULL) {
9120Sstevel@tonic-gate 		ddi_prop_free(ep);
9130Sstevel@tonic-gate 	}
9140Sstevel@tonic-gate 	return (DDI_FAILURE);
9150Sstevel@tonic-gate }
9160Sstevel@tonic-gate 
9170Sstevel@tonic-gate static int
rd_detach(dev_info_t * dip,ddi_detach_cmd_t cmd)9180Sstevel@tonic-gate rd_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
9190Sstevel@tonic-gate {
9200Sstevel@tonic-gate 	int	e;
9210Sstevel@tonic-gate 
9220Sstevel@tonic-gate 	switch (cmd) {
9230Sstevel@tonic-gate 
9240Sstevel@tonic-gate 	case DDI_DETACH:
9250Sstevel@tonic-gate 		mutex_enter(&rd_lock);
9260Sstevel@tonic-gate 		e = rd_common_detach(dip);
9270Sstevel@tonic-gate 		mutex_exit(&rd_lock);
9280Sstevel@tonic-gate 
9290Sstevel@tonic-gate 		return (e);
9300Sstevel@tonic-gate 
9310Sstevel@tonic-gate 	case DDI_SUSPEND:
9320Sstevel@tonic-gate 		return (DDI_SUCCESS);
9330Sstevel@tonic-gate 
9340Sstevel@tonic-gate 	default:
9350Sstevel@tonic-gate 		return (DDI_FAILURE);
9360Sstevel@tonic-gate 	}
9370Sstevel@tonic-gate }
9380Sstevel@tonic-gate 
9390Sstevel@tonic-gate /*ARGSUSED*/
9400Sstevel@tonic-gate static int
rd_getinfo(dev_info_t * dip,ddi_info_cmd_t infocmd,void * arg,void ** result)9410Sstevel@tonic-gate rd_getinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
9420Sstevel@tonic-gate {
9430Sstevel@tonic-gate 	rd_devstate_t	*rsp;
9440Sstevel@tonic-gate 
9450Sstevel@tonic-gate 	switch (infocmd) {
9460Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
9470Sstevel@tonic-gate 		if ((rsp = ddi_get_soft_state(rd_statep,
9480Sstevel@tonic-gate 		    getminor((dev_t)arg))) != NULL) {
9490Sstevel@tonic-gate 			*result = rsp->rd_dip;
9500Sstevel@tonic-gate 			return (DDI_SUCCESS);
9510Sstevel@tonic-gate 		}
9520Sstevel@tonic-gate 		*result = NULL;
9530Sstevel@tonic-gate 		return (DDI_FAILURE);
9540Sstevel@tonic-gate 
9550Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
9560Sstevel@tonic-gate 		if ((rsp = ddi_get_soft_state(rd_statep,
9570Sstevel@tonic-gate 		    getminor((dev_t)arg))) != NULL) {
9580Sstevel@tonic-gate 			*result = (void *)(uintptr_t)
9590Sstevel@tonic-gate 			    ddi_get_instance(rsp->rd_dip);
9600Sstevel@tonic-gate 			return (DDI_SUCCESS);
9610Sstevel@tonic-gate 		}
9620Sstevel@tonic-gate 		*result = NULL;
9630Sstevel@tonic-gate 		return (DDI_FAILURE);
9640Sstevel@tonic-gate 
9650Sstevel@tonic-gate 	default:
9660Sstevel@tonic-gate 		return (DDI_FAILURE);
9670Sstevel@tonic-gate 	}
9680Sstevel@tonic-gate }
9690Sstevel@tonic-gate 
9700Sstevel@tonic-gate /*ARGSUSED3*/
9710Sstevel@tonic-gate static int
rd_open(dev_t * devp,int flag,int otyp,cred_t * credp)9720Sstevel@tonic-gate rd_open(dev_t *devp, int flag, int otyp, cred_t *credp)
9730Sstevel@tonic-gate {
9740Sstevel@tonic-gate 	minor_t		minor;
9750Sstevel@tonic-gate 	rd_devstate_t	*rsp;
9760Sstevel@tonic-gate 
9770Sstevel@tonic-gate 	mutex_enter(&rd_lock);
9780Sstevel@tonic-gate 
9790Sstevel@tonic-gate 	minor = getminor(*devp);
9800Sstevel@tonic-gate 	if (minor == RD_CTL_MINOR) {
9810Sstevel@tonic-gate 		/*
9820Sstevel@tonic-gate 		 * Master control device; must be opened exclusively.
9830Sstevel@tonic-gate 		 */
9840Sstevel@tonic-gate 		if ((flag & FEXCL) != FEXCL || otyp != OTYP_CHR) {
9850Sstevel@tonic-gate 			mutex_exit(&rd_lock);
9860Sstevel@tonic-gate 			return (EINVAL);
9870Sstevel@tonic-gate 		}
9880Sstevel@tonic-gate 
9890Sstevel@tonic-gate 		rsp = ddi_get_soft_state(rd_statep, RD_CTL_MINOR);
9900Sstevel@tonic-gate 		if (rsp == NULL) {
9910Sstevel@tonic-gate 			mutex_exit(&rd_lock);
9920Sstevel@tonic-gate 			return (ENXIO);
9930Sstevel@tonic-gate 		}
9940Sstevel@tonic-gate 
9950Sstevel@tonic-gate 		if (rd_is_open(rsp)) {
9960Sstevel@tonic-gate 			mutex_exit(&rd_lock);
9970Sstevel@tonic-gate 			return (EBUSY);
9980Sstevel@tonic-gate 		}
9990Sstevel@tonic-gate 		(void) rd_opened(rsp, OTYP_CHR);
10000Sstevel@tonic-gate 
10010Sstevel@tonic-gate 		mutex_exit(&rd_lock);
10020Sstevel@tonic-gate 
10030Sstevel@tonic-gate 		return (0);
10040Sstevel@tonic-gate 	}
10050Sstevel@tonic-gate 
10060Sstevel@tonic-gate 	rsp = ddi_get_soft_state(rd_statep, minor);
10070Sstevel@tonic-gate 	if (rsp == NULL) {
10080Sstevel@tonic-gate 		mutex_exit(&rd_lock);
10090Sstevel@tonic-gate 		return (ENXIO);
10100Sstevel@tonic-gate 	}
10110Sstevel@tonic-gate 
10120Sstevel@tonic-gate 	if (rd_opened(rsp, otyp) == -1) {
10130Sstevel@tonic-gate 		mutex_exit(&rd_lock);
10140Sstevel@tonic-gate 		return (EINVAL);
10150Sstevel@tonic-gate 	}
10160Sstevel@tonic-gate 
10170Sstevel@tonic-gate 	mutex_exit(&rd_lock);
10180Sstevel@tonic-gate 	return (0);
10190Sstevel@tonic-gate }
10200Sstevel@tonic-gate 
10210Sstevel@tonic-gate /*ARGSUSED*/
10220Sstevel@tonic-gate static int
rd_close(dev_t dev,int flag,int otyp,struct cred * credp)10230Sstevel@tonic-gate rd_close(dev_t dev, int flag, int otyp, struct cred *credp)
10240Sstevel@tonic-gate {
10250Sstevel@tonic-gate 	minor_t		minor;
10260Sstevel@tonic-gate 	rd_devstate_t	*rsp;
10270Sstevel@tonic-gate 
10280Sstevel@tonic-gate 	mutex_enter(&rd_lock);
10290Sstevel@tonic-gate 
10300Sstevel@tonic-gate 	minor = getminor(dev);
10310Sstevel@tonic-gate 
10320Sstevel@tonic-gate 	rsp = ddi_get_soft_state(rd_statep, minor);
10330Sstevel@tonic-gate 	if (rsp == NULL) {
10340Sstevel@tonic-gate 		mutex_exit(&rd_lock);
10350Sstevel@tonic-gate 		return (EINVAL);
10360Sstevel@tonic-gate 	}
10370Sstevel@tonic-gate 
10380Sstevel@tonic-gate 	rd_closed(rsp, otyp);
10390Sstevel@tonic-gate 
10400Sstevel@tonic-gate 	mutex_exit(&rd_lock);
10410Sstevel@tonic-gate 
10420Sstevel@tonic-gate 	return (0);
10430Sstevel@tonic-gate }
10440Sstevel@tonic-gate 
10450Sstevel@tonic-gate static void
rd_minphys(struct buf * bp)10460Sstevel@tonic-gate rd_minphys(struct buf *bp)
10470Sstevel@tonic-gate {
10480Sstevel@tonic-gate 	if (bp->b_bcount > rd_maxphys) {
10490Sstevel@tonic-gate 		bp->b_bcount = rd_maxphys;
10500Sstevel@tonic-gate 	}
10510Sstevel@tonic-gate }
10520Sstevel@tonic-gate 
10530Sstevel@tonic-gate static void
rd_rw(rd_devstate_t * rsp,struct buf * bp,offset_t offset,size_t nbytes)10540Sstevel@tonic-gate rd_rw(rd_devstate_t *rsp, struct buf *bp, offset_t offset, size_t nbytes)
10550Sstevel@tonic-gate {
10560Sstevel@tonic-gate 	int	reading = bp->b_flags & B_READ;
10570Sstevel@tonic-gate 	caddr_t	buf_addr;
10580Sstevel@tonic-gate 
10590Sstevel@tonic-gate 	bp_mapin(bp);
10600Sstevel@tonic-gate 	buf_addr = bp->b_un.b_addr;
10610Sstevel@tonic-gate 
10620Sstevel@tonic-gate 	while (nbytes > 0) {
10630Sstevel@tonic-gate 		offset_t	off_in_window;
10640Sstevel@tonic-gate 		size_t		rem_in_window, copy_bytes;
10650Sstevel@tonic-gate 		caddr_t		raddr;
10660Sstevel@tonic-gate 
10670Sstevel@tonic-gate 		mutex_enter(&rsp->rd_device_lock);
10680Sstevel@tonic-gate 		rd_map_window(rsp, offset);
10690Sstevel@tonic-gate 
10700Sstevel@tonic-gate 		off_in_window = offset - rsp->rd_window_base;
10710Sstevel@tonic-gate 		rem_in_window = rsp->rd_window_size - off_in_window;
10720Sstevel@tonic-gate 
10730Sstevel@tonic-gate 		raddr = rsp->rd_window_virt + off_in_window;
10740Sstevel@tonic-gate 		copy_bytes = MIN(nbytes, rem_in_window);
10750Sstevel@tonic-gate 
10760Sstevel@tonic-gate 		if (reading) {
10770Sstevel@tonic-gate 			(void) bcopy(raddr, buf_addr, copy_bytes);
10780Sstevel@tonic-gate 		} else {
10790Sstevel@tonic-gate 			(void) bcopy(buf_addr, raddr, copy_bytes);
10800Sstevel@tonic-gate 		}
10810Sstevel@tonic-gate 		mutex_exit(&rsp->rd_device_lock);
10820Sstevel@tonic-gate 
10830Sstevel@tonic-gate 		offset   += copy_bytes;
10840Sstevel@tonic-gate 		buf_addr += copy_bytes;
10850Sstevel@tonic-gate 		nbytes   -= copy_bytes;
10860Sstevel@tonic-gate 	}
10870Sstevel@tonic-gate }
10880Sstevel@tonic-gate 
1089*9956SWilliam.Roche@Sun.COM /*
1090*9956SWilliam.Roche@Sun.COM  * On Sparc, this function deals with both pseudo ramdisks and OBP ramdisks.
1091*9956SWilliam.Roche@Sun.COM  * In the case where we freed the "bootarchive" ramdisk in bop_free_archive(),
1092*9956SWilliam.Roche@Sun.COM  * we stop allowing access to the OBP ramdisks. To do so, we set the
1093*9956SWilliam.Roche@Sun.COM  * bootops_obp_ramdisk_disabled flag to true, and we check if the operation
1094*9956SWilliam.Roche@Sun.COM  * is for an OBP ramdisk. In this case we indicate an ENXIO error.
1095*9956SWilliam.Roche@Sun.COM  */
10960Sstevel@tonic-gate static int
rd_strategy(struct buf * bp)10970Sstevel@tonic-gate rd_strategy(struct buf *bp)
10980Sstevel@tonic-gate {
10990Sstevel@tonic-gate 	rd_devstate_t	*rsp;
11000Sstevel@tonic-gate 	offset_t	offset;
11010Sstevel@tonic-gate 
11020Sstevel@tonic-gate 	rsp = ddi_get_soft_state(rd_statep, getminor(bp->b_edev));
11030Sstevel@tonic-gate 	offset = bp->b_blkno * DEV_BSIZE;
11040Sstevel@tonic-gate 
1105*9956SWilliam.Roche@Sun.COM #ifdef __sparc
1106*9956SWilliam.Roche@Sun.COM 	if (rsp == NULL ||
1107*9956SWilliam.Roche@Sun.COM 	    (bootops_obp_ramdisk_disabled &&
1108*9956SWilliam.Roche@Sun.COM 	    (rsp->rd_dip != rd_dip || rd_dip == NULL))) { /* OBP ramdisk */
1109*9956SWilliam.Roche@Sun.COM #else /* __sparc */
11100Sstevel@tonic-gate 	if (rsp == NULL) {
1111*9956SWilliam.Roche@Sun.COM #endif /* __sparc */
11120Sstevel@tonic-gate 		bp->b_error = ENXIO;
11130Sstevel@tonic-gate 		bp->b_flags |= B_ERROR;
11140Sstevel@tonic-gate 	} else if (offset >= rsp->rd_size) {
11150Sstevel@tonic-gate 		bp->b_error = EINVAL;
11160Sstevel@tonic-gate 		bp->b_flags |= B_ERROR;
11170Sstevel@tonic-gate 	} else {
11180Sstevel@tonic-gate 		size_t	nbytes;
11190Sstevel@tonic-gate 
11200Sstevel@tonic-gate 		if (rsp->rd_kstat) {
11210Sstevel@tonic-gate 			mutex_enter(rsp->rd_kstat->ks_lock);
11220Sstevel@tonic-gate 			kstat_runq_enter(KSTAT_IO_PTR(rsp->rd_kstat));
11230Sstevel@tonic-gate 			mutex_exit(rsp->rd_kstat->ks_lock);
11240Sstevel@tonic-gate 		}
11250Sstevel@tonic-gate 
11260Sstevel@tonic-gate 		nbytes = min(bp->b_bcount, rsp->rd_size - offset);
11270Sstevel@tonic-gate 
11280Sstevel@tonic-gate 		rd_rw(rsp, bp, offset, nbytes);
11290Sstevel@tonic-gate 
11300Sstevel@tonic-gate 		bp->b_resid = bp->b_bcount - nbytes;
11310Sstevel@tonic-gate 
11320Sstevel@tonic-gate 		if (rsp->rd_kstat) {
11330Sstevel@tonic-gate 			kstat_io_t *kioptr;
11340Sstevel@tonic-gate 
11350Sstevel@tonic-gate 			mutex_enter(rsp->rd_kstat->ks_lock);
11360Sstevel@tonic-gate 			kioptr = KSTAT_IO_PTR(rsp->rd_kstat);
11370Sstevel@tonic-gate 			if (bp->b_flags & B_READ) {
11380Sstevel@tonic-gate 				kioptr->nread += nbytes;
11390Sstevel@tonic-gate 				kioptr->reads++;
11400Sstevel@tonic-gate 			} else {
11410Sstevel@tonic-gate 				kioptr->nwritten += nbytes;
11420Sstevel@tonic-gate 				kioptr->writes++;
11430Sstevel@tonic-gate 			}
11440Sstevel@tonic-gate 			kstat_runq_exit(kioptr);
11450Sstevel@tonic-gate 			mutex_exit(rsp->rd_kstat->ks_lock);
11460Sstevel@tonic-gate 		}
11470Sstevel@tonic-gate 	}
11480Sstevel@tonic-gate 
11490Sstevel@tonic-gate 	biodone(bp);
11500Sstevel@tonic-gate 	return (0);
11510Sstevel@tonic-gate }
11520Sstevel@tonic-gate 
11530Sstevel@tonic-gate /*ARGSUSED*/
11540Sstevel@tonic-gate static int
11550Sstevel@tonic-gate rd_read(dev_t dev, struct uio *uiop, cred_t *credp)
11560Sstevel@tonic-gate {
11570Sstevel@tonic-gate 	rd_devstate_t	*rsp;
11580Sstevel@tonic-gate 
11590Sstevel@tonic-gate 	rsp = ddi_get_soft_state(rd_statep, getminor(dev));
11600Sstevel@tonic-gate 
11610Sstevel@tonic-gate 	if (uiop->uio_offset >= rsp->rd_size)
11620Sstevel@tonic-gate 		return (EINVAL);
11630Sstevel@tonic-gate 
11640Sstevel@tonic-gate 	return (physio(rd_strategy, NULL, dev, B_READ, rd_minphys, uiop));
11650Sstevel@tonic-gate }
11660Sstevel@tonic-gate 
11670Sstevel@tonic-gate /*ARGSUSED*/
11680Sstevel@tonic-gate static int
11690Sstevel@tonic-gate rd_write(dev_t dev, register struct uio *uiop, cred_t *credp)
11700Sstevel@tonic-gate {
11710Sstevel@tonic-gate 	rd_devstate_t	*rsp;
11720Sstevel@tonic-gate 
11730Sstevel@tonic-gate 	rsp = ddi_get_soft_state(rd_statep, getminor(dev));
11740Sstevel@tonic-gate 
11750Sstevel@tonic-gate 	if (uiop->uio_offset >= rsp->rd_size)
11760Sstevel@tonic-gate 		return (EINVAL);
11770Sstevel@tonic-gate 
11780Sstevel@tonic-gate 	return (physio(rd_strategy, NULL, dev, B_WRITE, rd_minphys, uiop));
11790Sstevel@tonic-gate }
11800Sstevel@tonic-gate 
11810Sstevel@tonic-gate /*ARGSUSED*/
11820Sstevel@tonic-gate static int
11830Sstevel@tonic-gate rd_create_disk(dev_t dev, struct rd_ioctl *urip, int mode, int *rvalp)
11840Sstevel@tonic-gate {
11850Sstevel@tonic-gate 	struct rd_ioctl	kri;
11860Sstevel@tonic-gate 	size_t		size;
11870Sstevel@tonic-gate 	rd_devstate_t	*rsp;
11880Sstevel@tonic-gate 
11890Sstevel@tonic-gate 	if (ddi_copyin(urip, &kri, sizeof (kri), mode) == -1) {
11900Sstevel@tonic-gate 		return (EFAULT);
11910Sstevel@tonic-gate 	}
11920Sstevel@tonic-gate 
11930Sstevel@tonic-gate 	kri.ri_name[RD_NAME_LEN] = '\0';
11940Sstevel@tonic-gate 
11950Sstevel@tonic-gate 	size = kri.ri_size;
11960Sstevel@tonic-gate 	if (size == 0) {
11970Sstevel@tonic-gate 		return (EINVAL);
11980Sstevel@tonic-gate 	}
11990Sstevel@tonic-gate 	size = ptob(btopr(size));
12000Sstevel@tonic-gate 
12010Sstevel@tonic-gate 	mutex_enter(&rd_lock);
12020Sstevel@tonic-gate 
12030Sstevel@tonic-gate 	if (rd_find_named_disk(kri.ri_name) != NULL) {
12040Sstevel@tonic-gate 		mutex_exit(&rd_lock);
12050Sstevel@tonic-gate 		return (EEXIST);
12060Sstevel@tonic-gate 	}
12070Sstevel@tonic-gate 
12085648Ssetje 	rsp = rd_alloc_resources(kri.ri_name, 0, size, rd_dip);
12090Sstevel@tonic-gate 	if (rsp == NULL) {
12100Sstevel@tonic-gate 		mutex_exit(&rd_lock);
12110Sstevel@tonic-gate 		return (EAGAIN);
12120Sstevel@tonic-gate 	}
12130Sstevel@tonic-gate 
12140Sstevel@tonic-gate 	mutex_exit(&rd_lock);
12150Sstevel@tonic-gate 
12160Sstevel@tonic-gate 	return (ddi_copyout(&kri, urip, sizeof (kri), mode) == -1 ? EFAULT : 0);
12170Sstevel@tonic-gate }
12180Sstevel@tonic-gate 
12190Sstevel@tonic-gate /*ARGSUSED*/
12200Sstevel@tonic-gate static int
12210Sstevel@tonic-gate rd_delete_disk(dev_t dev, struct rd_ioctl *urip, int mode)
12220Sstevel@tonic-gate {
12230Sstevel@tonic-gate 	struct rd_ioctl	kri;
12240Sstevel@tonic-gate 	rd_devstate_t	*rsp;
12250Sstevel@tonic-gate 
12260Sstevel@tonic-gate 	if (ddi_copyin(urip, &kri, sizeof (kri), mode) == -1) {
12270Sstevel@tonic-gate 		return (EFAULT);
12280Sstevel@tonic-gate 	}
12290Sstevel@tonic-gate 
12300Sstevel@tonic-gate 	kri.ri_name[RD_NAME_LEN] = '\0';
12310Sstevel@tonic-gate 
12320Sstevel@tonic-gate 	mutex_enter(&rd_lock);
12330Sstevel@tonic-gate 
12340Sstevel@tonic-gate 	rsp = rd_find_named_disk(kri.ri_name);
12350Sstevel@tonic-gate 	if (rsp == NULL || rsp->rd_dip != rd_dip) {
12360Sstevel@tonic-gate 		mutex_exit(&rd_lock);
12370Sstevel@tonic-gate 		return (EINVAL);
12380Sstevel@tonic-gate 	}
12390Sstevel@tonic-gate 	if (rd_is_open(rsp)) {
12400Sstevel@tonic-gate 		mutex_exit(&rd_lock);
12410Sstevel@tonic-gate 		return (EBUSY);
12420Sstevel@tonic-gate 	}
12430Sstevel@tonic-gate 
12440Sstevel@tonic-gate 	rd_dealloc_resources(rsp);
12450Sstevel@tonic-gate 
12460Sstevel@tonic-gate 	mutex_exit(&rd_lock);
12470Sstevel@tonic-gate 
12480Sstevel@tonic-gate 	return (0);
12490Sstevel@tonic-gate }
12500Sstevel@tonic-gate 
12510Sstevel@tonic-gate /*ARGSUSED*/
12520Sstevel@tonic-gate static int
12530Sstevel@tonic-gate rd_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp, int *rvalp)
12540Sstevel@tonic-gate {
12550Sstevel@tonic-gate 	minor_t		minor;
12560Sstevel@tonic-gate 	int		error;
12570Sstevel@tonic-gate 	enum dkio_state	dkstate;
12580Sstevel@tonic-gate 	rd_devstate_t	*rsp;
12590Sstevel@tonic-gate 
12600Sstevel@tonic-gate 	minor = getminor(dev);
12610Sstevel@tonic-gate 
12620Sstevel@tonic-gate 	/*
12630Sstevel@tonic-gate 	 * Ramdisk ioctls only apply to the master device.
12640Sstevel@tonic-gate 	 */
12650Sstevel@tonic-gate 	if (minor == RD_CTL_MINOR) {
12660Sstevel@tonic-gate 		struct rd_ioctl *rip = (struct rd_ioctl *)arg;
12670Sstevel@tonic-gate 
12680Sstevel@tonic-gate 		/*
12690Sstevel@tonic-gate 		 * The query commands only need read-access - i.e., normal
12700Sstevel@tonic-gate 		 * users are allowed to do those on the controlling device
12710Sstevel@tonic-gate 		 * as long as they can open it read-only.
12720Sstevel@tonic-gate 		 */
12730Sstevel@tonic-gate 		switch (cmd) {
12740Sstevel@tonic-gate 		case RD_CREATE_DISK:
12750Sstevel@tonic-gate 			if ((mode & FWRITE) == 0)
12760Sstevel@tonic-gate 				return (EPERM);
12770Sstevel@tonic-gate 			return (rd_create_disk(dev, rip, mode, rvalp));
12780Sstevel@tonic-gate 
12790Sstevel@tonic-gate 		case RD_DELETE_DISK:
12800Sstevel@tonic-gate 			if ((mode & FWRITE) == 0)
12810Sstevel@tonic-gate 				return (EPERM);
12820Sstevel@tonic-gate 			return (rd_delete_disk(dev, rip, mode));
12830Sstevel@tonic-gate 
12840Sstevel@tonic-gate 		default:
12850Sstevel@tonic-gate 			return (EINVAL);
12860Sstevel@tonic-gate 		}
12870Sstevel@tonic-gate 	}
12880Sstevel@tonic-gate 
12890Sstevel@tonic-gate 	rsp = ddi_get_soft_state(rd_statep, minor);
12900Sstevel@tonic-gate 	if (rsp == NULL) {
12910Sstevel@tonic-gate 		return (ENXIO);
12920Sstevel@tonic-gate 	}
12930Sstevel@tonic-gate 
12940Sstevel@tonic-gate 	/*
12950Sstevel@tonic-gate 	 * These are for faking out utilities like newfs.
12960Sstevel@tonic-gate 	 */
12970Sstevel@tonic-gate 	switch (cmd) {
12980Sstevel@tonic-gate 	case DKIOCGVTOC:
12990Sstevel@tonic-gate 		switch (ddi_model_convert_from(mode & FMODELS)) {
13000Sstevel@tonic-gate 		case DDI_MODEL_ILP32: {
13010Sstevel@tonic-gate 			struct vtoc32 vtoc32;
13020Sstevel@tonic-gate 
13030Sstevel@tonic-gate 			vtoctovtoc32(rsp->rd_vtoc, vtoc32);
13040Sstevel@tonic-gate 			if (ddi_copyout(&vtoc32, (void *)arg,
13050Sstevel@tonic-gate 			    sizeof (struct vtoc32), mode))
13060Sstevel@tonic-gate 				return (EFAULT);
13070Sstevel@tonic-gate 			}
13080Sstevel@tonic-gate 			break;
13090Sstevel@tonic-gate 
13100Sstevel@tonic-gate 		case DDI_MODEL_NONE:
13110Sstevel@tonic-gate 			if (ddi_copyout(&rsp->rd_vtoc, (void *)arg,
13120Sstevel@tonic-gate 			    sizeof (struct vtoc), mode))
13130Sstevel@tonic-gate 				return (EFAULT);
13140Sstevel@tonic-gate 			break;
13150Sstevel@tonic-gate 		}
13160Sstevel@tonic-gate 		return (0);
13170Sstevel@tonic-gate 	case DKIOCINFO:
13180Sstevel@tonic-gate 		error = ddi_copyout(&rsp->rd_ci, (void *)arg,
13190Sstevel@tonic-gate 		    sizeof (struct dk_cinfo), mode);
13200Sstevel@tonic-gate 		if (error)
13210Sstevel@tonic-gate 			return (EFAULT);
13220Sstevel@tonic-gate 		return (0);
13230Sstevel@tonic-gate 	case DKIOCG_VIRTGEOM:
13240Sstevel@tonic-gate 	case DKIOCG_PHYGEOM:
13250Sstevel@tonic-gate 	case DKIOCGGEOM:
13260Sstevel@tonic-gate 		error = ddi_copyout(&rsp->rd_dkg, (void *)arg,
13270Sstevel@tonic-gate 		    sizeof (struct dk_geom), mode);
13280Sstevel@tonic-gate 		if (error)
13290Sstevel@tonic-gate 			return (EFAULT);
13300Sstevel@tonic-gate 		return (0);
13310Sstevel@tonic-gate 	case DKIOCSTATE:
13320Sstevel@tonic-gate 		/* the file is always there */
13330Sstevel@tonic-gate 		dkstate = DKIO_INSERTED;
13340Sstevel@tonic-gate 		error = ddi_copyout(&dkstate, (void *)arg,
13350Sstevel@tonic-gate 		    sizeof (enum dkio_state), mode);
13360Sstevel@tonic-gate 		if (error)
13370Sstevel@tonic-gate 			return (EFAULT);
13380Sstevel@tonic-gate 		return (0);
13390Sstevel@tonic-gate 	default:
13400Sstevel@tonic-gate 		return (ENOTTY);
13410Sstevel@tonic-gate 	}
13420Sstevel@tonic-gate }
13430Sstevel@tonic-gate 
13440Sstevel@tonic-gate 
13450Sstevel@tonic-gate static struct cb_ops rd_cb_ops = {
13460Sstevel@tonic-gate 	rd_open,
13470Sstevel@tonic-gate 	rd_close,
13480Sstevel@tonic-gate 	rd_strategy,
13490Sstevel@tonic-gate 	nodev,
13500Sstevel@tonic-gate 	nodev,		/* dump */
13510Sstevel@tonic-gate 	rd_read,
13520Sstevel@tonic-gate 	rd_write,
13530Sstevel@tonic-gate 	rd_ioctl,
13540Sstevel@tonic-gate 	nodev,		/* devmap */
13550Sstevel@tonic-gate 	nodev,		/* mmap */
13560Sstevel@tonic-gate 	nodev,		/* segmap */
13570Sstevel@tonic-gate 	nochpoll,	/* poll */
13580Sstevel@tonic-gate 	ddi_prop_op,
13590Sstevel@tonic-gate 	NULL,
13600Sstevel@tonic-gate 	D_NEW | D_MP
13610Sstevel@tonic-gate };
13620Sstevel@tonic-gate 
13630Sstevel@tonic-gate static struct dev_ops rd_ops = {
13640Sstevel@tonic-gate 	DEVO_REV,
13650Sstevel@tonic-gate 	0,
13660Sstevel@tonic-gate 	rd_getinfo,
13670Sstevel@tonic-gate 	nulldev,	/* identify */
13680Sstevel@tonic-gate 	nulldev,	/* probe */
13690Sstevel@tonic-gate 	rd_attach,
13700Sstevel@tonic-gate 	rd_detach,
13710Sstevel@tonic-gate 	nodev,		/* reset */
13720Sstevel@tonic-gate 	&rd_cb_ops,
13737656SSherry.Moore@Sun.COM 	(struct bus_ops *)0,
13747656SSherry.Moore@Sun.COM 	NULL,
13757656SSherry.Moore@Sun.COM 	ddi_quiesce_not_needed,		/* quiesce */
13760Sstevel@tonic-gate };
13770Sstevel@tonic-gate 
13780Sstevel@tonic-gate 
13790Sstevel@tonic-gate extern struct mod_ops mod_driverops;
13800Sstevel@tonic-gate 
13810Sstevel@tonic-gate static struct modldrv modldrv = {
13820Sstevel@tonic-gate 	&mod_driverops,
13835648Ssetje 	"ramdisk driver",
13840Sstevel@tonic-gate 	&rd_ops
13850Sstevel@tonic-gate };
13860Sstevel@tonic-gate 
13870Sstevel@tonic-gate static struct modlinkage modlinkage = {
13880Sstevel@tonic-gate 	MODREV_1,
13890Sstevel@tonic-gate 	&modldrv,
13900Sstevel@tonic-gate 	0
13910Sstevel@tonic-gate };
13920Sstevel@tonic-gate 
13930Sstevel@tonic-gate int
13940Sstevel@tonic-gate _init(void)
13950Sstevel@tonic-gate {
13960Sstevel@tonic-gate 	int e;
13970Sstevel@tonic-gate 
13980Sstevel@tonic-gate 	if ((e = ddi_soft_state_init(&rd_statep,
13990Sstevel@tonic-gate 	    sizeof (rd_devstate_t), 0)) != 0) {
14000Sstevel@tonic-gate 		return (e);
14010Sstevel@tonic-gate 	}
14020Sstevel@tonic-gate 
14030Sstevel@tonic-gate 	mutex_init(&rd_lock, NULL, MUTEX_DRIVER, NULL);
14040Sstevel@tonic-gate 
14050Sstevel@tonic-gate 	if ((e = mod_install(&modlinkage)) != 0)  {
14060Sstevel@tonic-gate 		mutex_destroy(&rd_lock);
14070Sstevel@tonic-gate 		ddi_soft_state_fini(&rd_statep);
14080Sstevel@tonic-gate 	}
14090Sstevel@tonic-gate 
14100Sstevel@tonic-gate 	return (e);
14110Sstevel@tonic-gate }
14120Sstevel@tonic-gate 
14130Sstevel@tonic-gate int
14140Sstevel@tonic-gate _fini(void)
14150Sstevel@tonic-gate {
14160Sstevel@tonic-gate 	int e;
14170Sstevel@tonic-gate 
14180Sstevel@tonic-gate 	if ((e = mod_remove(&modlinkage)) != 0)  {
14190Sstevel@tonic-gate 		return (e);
14200Sstevel@tonic-gate 	}
14210Sstevel@tonic-gate 
14220Sstevel@tonic-gate 	ddi_soft_state_fini(&rd_statep);
14230Sstevel@tonic-gate 	mutex_destroy(&rd_lock);
14240Sstevel@tonic-gate 
14250Sstevel@tonic-gate 	return (e);
14260Sstevel@tonic-gate }
14270Sstevel@tonic-gate 
14280Sstevel@tonic-gate int
14290Sstevel@tonic-gate _info(struct modinfo *modinfop)
14300Sstevel@tonic-gate {
14310Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
14320Sstevel@tonic-gate }
1433