xref: /illumos-gate/usr/src/uts/common/os/sunddi.c (revision 4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6)
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
54ab75253Smrj  * Common Development and Distribution License (the "License").
64ab75253Smrj  * 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  */
214ab75253Smrj 
227c478bd9Sstevel@tonic-gate /*
235febcb4aSScott Carter, SD IOSW  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <sys/note.h>
287c478bd9Sstevel@tonic-gate #include <sys/types.h>
297c478bd9Sstevel@tonic-gate #include <sys/param.h>
307c478bd9Sstevel@tonic-gate #include <sys/systm.h>
317c478bd9Sstevel@tonic-gate #include <sys/buf.h>
327c478bd9Sstevel@tonic-gate #include <sys/uio.h>
337c478bd9Sstevel@tonic-gate #include <sys/cred.h>
347c478bd9Sstevel@tonic-gate #include <sys/poll.h>
357c478bd9Sstevel@tonic-gate #include <sys/mman.h>
367c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
377c478bd9Sstevel@tonic-gate #include <sys/model.h>
387c478bd9Sstevel@tonic-gate #include <sys/file.h>
397c478bd9Sstevel@tonic-gate #include <sys/proc.h>
407c478bd9Sstevel@tonic-gate #include <sys/open.h>
417c478bd9Sstevel@tonic-gate #include <sys/user.h>
427c478bd9Sstevel@tonic-gate #include <sys/t_lock.h>
437c478bd9Sstevel@tonic-gate #include <sys/vm.h>
447c478bd9Sstevel@tonic-gate #include <sys/stat.h>
457c478bd9Sstevel@tonic-gate #include <vm/hat.h>
467c478bd9Sstevel@tonic-gate #include <vm/seg.h>
477c478bd9Sstevel@tonic-gate #include <vm/seg_vn.h>
487c478bd9Sstevel@tonic-gate #include <vm/seg_dev.h>
497c478bd9Sstevel@tonic-gate #include <vm/as.h>
507c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
517c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
527c478bd9Sstevel@tonic-gate #include <sys/debug.h>
537c478bd9Sstevel@tonic-gate #include <sys/autoconf.h>
547c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
557c478bd9Sstevel@tonic-gate #include <sys/esunddi.h>
567c478bd9Sstevel@tonic-gate #include <sys/sunndi.h>
577c478bd9Sstevel@tonic-gate #include <sys/kstat.h>
587c478bd9Sstevel@tonic-gate #include <sys/conf.h>
597c478bd9Sstevel@tonic-gate #include <sys/ddi_impldefs.h>	/* include implementation structure defs */
607c478bd9Sstevel@tonic-gate #include <sys/ndi_impldefs.h>	/* include prototypes */
61dd4eeefdSeota #include <sys/ddi_timer.h>
627c478bd9Sstevel@tonic-gate #include <sys/hwconf.h>
637c478bd9Sstevel@tonic-gate #include <sys/pathname.h>
647c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
657c478bd9Sstevel@tonic-gate #include <sys/epm.h>
667c478bd9Sstevel@tonic-gate #include <sys/devctl.h>
677c478bd9Sstevel@tonic-gate #include <sys/callb.h>
687c478bd9Sstevel@tonic-gate #include <sys/cladm.h>
697c478bd9Sstevel@tonic-gate #include <sys/sysevent.h>
707c478bd9Sstevel@tonic-gate #include <sys/dacf_impl.h>
717c478bd9Sstevel@tonic-gate #include <sys/ddidevmap.h>
727c478bd9Sstevel@tonic-gate #include <sys/bootconf.h>
737c478bd9Sstevel@tonic-gate #include <sys/disp.h>
747c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
757c478bd9Sstevel@tonic-gate #include <sys/promif.h>
767c478bd9Sstevel@tonic-gate #include <sys/instance.h>
777c478bd9Sstevel@tonic-gate #include <sys/sysevent/eventdefs.h>
787c478bd9Sstevel@tonic-gate #include <sys/task.h>
797c478bd9Sstevel@tonic-gate #include <sys/project.h>
807c478bd9Sstevel@tonic-gate #include <sys/taskq.h>
817c478bd9Sstevel@tonic-gate #include <sys/devpolicy.h>
827c478bd9Sstevel@tonic-gate #include <sys/ctype.h>
837c478bd9Sstevel@tonic-gate #include <net/if.h>
84c6939658Ssl #include <sys/rctl.h>
855679c89fSjv #include <sys/zone.h>
86*4c06356bSdh #include <sys/ddi.h>
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate extern	pri_t	minclsyspri;
897c478bd9Sstevel@tonic-gate 
90c6939658Ssl extern	rctl_hndl_t rc_project_locked_mem;
91c6939658Ssl extern	rctl_hndl_t rc_zone_locked_mem;
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate #ifdef DEBUG
947c478bd9Sstevel@tonic-gate static int sunddi_debug = 0;
957c478bd9Sstevel@tonic-gate #endif /* DEBUG */
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate /* ddi_umem_unlock miscellaneous */
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate static	void	i_ddi_umem_unlock_thread_start(void);
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate static	kmutex_t	ddi_umem_unlock_mutex; /* unlock list mutex */
1027c478bd9Sstevel@tonic-gate static	kcondvar_t	ddi_umem_unlock_cv; /* unlock list block/unblock */
1037c478bd9Sstevel@tonic-gate static	kthread_t	*ddi_umem_unlock_thread;
1047c478bd9Sstevel@tonic-gate /*
1057c478bd9Sstevel@tonic-gate  * The ddi_umem_unlock FIFO list.  NULL head pointer indicates empty list.
1067c478bd9Sstevel@tonic-gate  */
1077c478bd9Sstevel@tonic-gate static	struct	ddi_umem_cookie *ddi_umem_unlock_head = NULL;
1087c478bd9Sstevel@tonic-gate static	struct	ddi_umem_cookie *ddi_umem_unlock_tail = NULL;
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate /*
1117c478bd9Sstevel@tonic-gate  * DDI(Sun) Function and flag definitions:
1127c478bd9Sstevel@tonic-gate  */
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate #if defined(__x86)
1157c478bd9Sstevel@tonic-gate /*
1167c478bd9Sstevel@tonic-gate  * Used to indicate which entries were chosen from a range.
1177c478bd9Sstevel@tonic-gate  */
1187c478bd9Sstevel@tonic-gate char	*chosen_reg = "chosen-reg";
1197c478bd9Sstevel@tonic-gate #endif
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate /*
1227c478bd9Sstevel@tonic-gate  * Function used to ring system console bell
1237c478bd9Sstevel@tonic-gate  */
1247c478bd9Sstevel@tonic-gate void (*ddi_console_bell_func)(clock_t duration);
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate /*
1277c478bd9Sstevel@tonic-gate  * Creating register mappings and handling interrupts:
1287c478bd9Sstevel@tonic-gate  */
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate /*
1317c478bd9Sstevel@tonic-gate  * Generic ddi_map: Call parent to fulfill request...
1327c478bd9Sstevel@tonic-gate  */
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate int
1357c478bd9Sstevel@tonic-gate ddi_map(dev_info_t *dp, ddi_map_req_t *mp, off_t offset,
1367c478bd9Sstevel@tonic-gate     off_t len, caddr_t *addrp)
1377c478bd9Sstevel@tonic-gate {
1387c478bd9Sstevel@tonic-gate 	dev_info_t *pdip;
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	ASSERT(dp);
1417c478bd9Sstevel@tonic-gate 	pdip = (dev_info_t *)DEVI(dp)->devi_parent;
1427c478bd9Sstevel@tonic-gate 	return ((DEVI(pdip)->devi_ops->devo_bus_ops->bus_map)(pdip,
1437c478bd9Sstevel@tonic-gate 	    dp, mp, offset, len, addrp));
1447c478bd9Sstevel@tonic-gate }
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate /*
1477c478bd9Sstevel@tonic-gate  * ddi_apply_range: (Called by nexi only.)
1487c478bd9Sstevel@tonic-gate  * Apply ranges in parent node dp, to child regspec rp...
1497c478bd9Sstevel@tonic-gate  */
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate int
1527c478bd9Sstevel@tonic-gate ddi_apply_range(dev_info_t *dp, dev_info_t *rdip, struct regspec *rp)
1537c478bd9Sstevel@tonic-gate {
1547c478bd9Sstevel@tonic-gate 	return (i_ddi_apply_range(dp, rdip, rp));
1557c478bd9Sstevel@tonic-gate }
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate int
1587c478bd9Sstevel@tonic-gate ddi_map_regs(dev_info_t *dip, uint_t rnumber, caddr_t *kaddrp, off_t offset,
1597c478bd9Sstevel@tonic-gate     off_t len)
1607c478bd9Sstevel@tonic-gate {
1617c478bd9Sstevel@tonic-gate 	ddi_map_req_t mr;
1627c478bd9Sstevel@tonic-gate #if defined(__x86)
1637c478bd9Sstevel@tonic-gate 	struct {
1647c478bd9Sstevel@tonic-gate 		int	bus;
1657c478bd9Sstevel@tonic-gate 		int	addr;
1667c478bd9Sstevel@tonic-gate 		int	size;
1677c478bd9Sstevel@tonic-gate 	} reg, *reglist;
1687c478bd9Sstevel@tonic-gate 	uint_t	length;
1697c478bd9Sstevel@tonic-gate 	int	rc;
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 	/*
1727c478bd9Sstevel@tonic-gate 	 * get the 'registers' or the 'reg' property.
1737c478bd9Sstevel@tonic-gate 	 * We look up the reg property as an array of
1747c478bd9Sstevel@tonic-gate 	 * int's.
1757c478bd9Sstevel@tonic-gate 	 */
1767c478bd9Sstevel@tonic-gate 	rc = ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dip,
1777c478bd9Sstevel@tonic-gate 	    DDI_PROP_DONTPASS, "registers", (int **)&reglist, &length);
1787c478bd9Sstevel@tonic-gate 	if (rc != DDI_PROP_SUCCESS)
1797c478bd9Sstevel@tonic-gate 		rc = ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dip,
1807c478bd9Sstevel@tonic-gate 		    DDI_PROP_DONTPASS, "reg", (int **)&reglist, &length);
1817c478bd9Sstevel@tonic-gate 	if (rc == DDI_PROP_SUCCESS) {
1827c478bd9Sstevel@tonic-gate 		/*
1837c478bd9Sstevel@tonic-gate 		 * point to the required entry.
1847c478bd9Sstevel@tonic-gate 		 */
1857c478bd9Sstevel@tonic-gate 		reg = reglist[rnumber];
1867c478bd9Sstevel@tonic-gate 		reg.addr += offset;
1877c478bd9Sstevel@tonic-gate 		if (len != 0)
1887c478bd9Sstevel@tonic-gate 			reg.size = len;
1897c478bd9Sstevel@tonic-gate 		/*
1907c478bd9Sstevel@tonic-gate 		 * make a new property containing ONLY the required tuple.
1917c478bd9Sstevel@tonic-gate 		 */
1927c478bd9Sstevel@tonic-gate 		if (ddi_prop_update_int_array(DDI_DEV_T_NONE, dip,
1937c478bd9Sstevel@tonic-gate 		    chosen_reg, (int *)&reg, (sizeof (reg)/sizeof (int)))
1947c478bd9Sstevel@tonic-gate 		    != DDI_PROP_SUCCESS) {
1957c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s%d: cannot create '%s' "
1967c478bd9Sstevel@tonic-gate 			    "property", DEVI(dip)->devi_name,
1977c478bd9Sstevel@tonic-gate 			    DEVI(dip)->devi_instance, chosen_reg);
1987c478bd9Sstevel@tonic-gate 		}
1997c478bd9Sstevel@tonic-gate 		/*
2007c478bd9Sstevel@tonic-gate 		 * free the memory allocated by
2017c478bd9Sstevel@tonic-gate 		 * ddi_prop_lookup_int_array ().
2027c478bd9Sstevel@tonic-gate 		 */
2037c478bd9Sstevel@tonic-gate 		ddi_prop_free((void *)reglist);
2047c478bd9Sstevel@tonic-gate 	}
2057c478bd9Sstevel@tonic-gate #endif
2067c478bd9Sstevel@tonic-gate 	mr.map_op = DDI_MO_MAP_LOCKED;
2077c478bd9Sstevel@tonic-gate 	mr.map_type = DDI_MT_RNUMBER;
2087c478bd9Sstevel@tonic-gate 	mr.map_obj.rnumber = rnumber;
2097c478bd9Sstevel@tonic-gate 	mr.map_prot = PROT_READ | PROT_WRITE;
2107c478bd9Sstevel@tonic-gate 	mr.map_flags = DDI_MF_KERNEL_MAPPING;
2117c478bd9Sstevel@tonic-gate 	mr.map_handlep = NULL;
2127c478bd9Sstevel@tonic-gate 	mr.map_vers = DDI_MAP_VERSION;
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 	/*
2157c478bd9Sstevel@tonic-gate 	 * Call my parent to map in my regs.
2167c478bd9Sstevel@tonic-gate 	 */
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 	return (ddi_map(dip, &mr, offset, len, kaddrp));
2197c478bd9Sstevel@tonic-gate }
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate void
2227c478bd9Sstevel@tonic-gate ddi_unmap_regs(dev_info_t *dip, uint_t rnumber, caddr_t *kaddrp, off_t offset,
2237c478bd9Sstevel@tonic-gate     off_t len)
2247c478bd9Sstevel@tonic-gate {
2257c478bd9Sstevel@tonic-gate 	ddi_map_req_t mr;
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 	mr.map_op = DDI_MO_UNMAP;
2287c478bd9Sstevel@tonic-gate 	mr.map_type = DDI_MT_RNUMBER;
2297c478bd9Sstevel@tonic-gate 	mr.map_flags = DDI_MF_KERNEL_MAPPING;
2307c478bd9Sstevel@tonic-gate 	mr.map_prot = PROT_READ | PROT_WRITE;	/* who cares? */
2317c478bd9Sstevel@tonic-gate 	mr.map_obj.rnumber = rnumber;
2327c478bd9Sstevel@tonic-gate 	mr.map_handlep = NULL;
2337c478bd9Sstevel@tonic-gate 	mr.map_vers = DDI_MAP_VERSION;
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 	/*
2367c478bd9Sstevel@tonic-gate 	 * Call my parent to unmap my regs.
2377c478bd9Sstevel@tonic-gate 	 */
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 	(void) ddi_map(dip, &mr, offset, len, kaddrp);
2407c478bd9Sstevel@tonic-gate 	*kaddrp = (caddr_t)0;
2417c478bd9Sstevel@tonic-gate #if defined(__x86)
2427c478bd9Sstevel@tonic-gate 	(void) ddi_prop_remove(DDI_DEV_T_NONE, dip, chosen_reg);
2437c478bd9Sstevel@tonic-gate #endif
2447c478bd9Sstevel@tonic-gate }
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate int
2477c478bd9Sstevel@tonic-gate ddi_bus_map(dev_info_t *dip, dev_info_t *rdip, ddi_map_req_t *mp,
2487c478bd9Sstevel@tonic-gate 	off_t offset, off_t len, caddr_t *vaddrp)
2497c478bd9Sstevel@tonic-gate {
2507c478bd9Sstevel@tonic-gate 	return (i_ddi_bus_map(dip, rdip, mp, offset, len, vaddrp));
2517c478bd9Sstevel@tonic-gate }
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate /*
2547c478bd9Sstevel@tonic-gate  * nullbusmap:	The/DDI default bus_map entry point for nexi
2557c478bd9Sstevel@tonic-gate  *		not conforming to the reg/range paradigm (i.e. scsi, etc.)
2567c478bd9Sstevel@tonic-gate  *		with no HAT/MMU layer to be programmed at this level.
2577c478bd9Sstevel@tonic-gate  *
2587c478bd9Sstevel@tonic-gate  *		If the call is to map by rnumber, return an error,
2597c478bd9Sstevel@tonic-gate  *		otherwise pass anything else up the tree to my parent.
2607c478bd9Sstevel@tonic-gate  */
2617c478bd9Sstevel@tonic-gate int
2627c478bd9Sstevel@tonic-gate nullbusmap(dev_info_t *dip, dev_info_t *rdip, ddi_map_req_t *mp,
2637c478bd9Sstevel@tonic-gate 	off_t offset, off_t len, caddr_t *vaddrp)
2647c478bd9Sstevel@tonic-gate {
2657c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(rdip))
2667c478bd9Sstevel@tonic-gate 	if (mp->map_type == DDI_MT_RNUMBER)
2677c478bd9Sstevel@tonic-gate 		return (DDI_ME_UNSUPPORTED);
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate 	return (ddi_map(dip, mp, offset, len, vaddrp));
2707c478bd9Sstevel@tonic-gate }
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate /*
2737c478bd9Sstevel@tonic-gate  * ddi_rnumber_to_regspec: Not for use by leaf drivers.
2747c478bd9Sstevel@tonic-gate  *			   Only for use by nexi using the reg/range paradigm.
2757c478bd9Sstevel@tonic-gate  */
2767c478bd9Sstevel@tonic-gate struct regspec *
2777c478bd9Sstevel@tonic-gate ddi_rnumber_to_regspec(dev_info_t *dip, int rnumber)
2787c478bd9Sstevel@tonic-gate {
2797c478bd9Sstevel@tonic-gate 	return (i_ddi_rnumber_to_regspec(dip, rnumber));
2807c478bd9Sstevel@tonic-gate }
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate /*
2847c478bd9Sstevel@tonic-gate  * Note that we allow the dip to be nil because we may be called
2857c478bd9Sstevel@tonic-gate  * prior even to the instantiation of the devinfo tree itself - all
2867c478bd9Sstevel@tonic-gate  * regular leaf and nexus drivers should always use a non-nil dip!
2877c478bd9Sstevel@tonic-gate  *
2887c478bd9Sstevel@tonic-gate  * We treat peek in a somewhat cavalier fashion .. assuming that we'll
2897c478bd9Sstevel@tonic-gate  * simply get a synchronous fault as soon as we touch a missing address.
2907c478bd9Sstevel@tonic-gate  *
2917c478bd9Sstevel@tonic-gate  * Poke is rather more carefully handled because we might poke to a write
2927c478bd9Sstevel@tonic-gate  * buffer, "succeed", then only find some time later that we got an
2937c478bd9Sstevel@tonic-gate  * asynchronous fault that indicated that the address we were writing to
2947c478bd9Sstevel@tonic-gate  * was not really backed by hardware.
2957c478bd9Sstevel@tonic-gate  */
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate static int
2987c478bd9Sstevel@tonic-gate i_ddi_peekpoke(dev_info_t *devi, ddi_ctl_enum_t cmd, size_t size,
2997c478bd9Sstevel@tonic-gate     void *addr, void *value_p)
3007c478bd9Sstevel@tonic-gate {
3017c478bd9Sstevel@tonic-gate 	union {
3027c478bd9Sstevel@tonic-gate 		uint64_t	u64;
3037c478bd9Sstevel@tonic-gate 		uint32_t	u32;
3047c478bd9Sstevel@tonic-gate 		uint16_t	u16;
3057c478bd9Sstevel@tonic-gate 		uint8_t		u8;
3067c478bd9Sstevel@tonic-gate 	} peekpoke_value;
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 	peekpoke_ctlops_t peekpoke_args;
3097c478bd9Sstevel@tonic-gate 	uint64_t dummy_result;
3107c478bd9Sstevel@tonic-gate 	int rval;
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate 	/* Note: size is assumed to be correct;  it is not checked. */
3137c478bd9Sstevel@tonic-gate 	peekpoke_args.size = size;
314abdbd06dSagiri 	peekpoke_args.dev_addr = (uintptr_t)addr;
3157c478bd9Sstevel@tonic-gate 	peekpoke_args.handle = NULL;
3167c478bd9Sstevel@tonic-gate 	peekpoke_args.repcount = 1;
3177c478bd9Sstevel@tonic-gate 	peekpoke_args.flags = 0;
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 	if (cmd == DDI_CTLOPS_POKE) {
3207c478bd9Sstevel@tonic-gate 		switch (size) {
3217c478bd9Sstevel@tonic-gate 		case sizeof (uint8_t):
3227c478bd9Sstevel@tonic-gate 			peekpoke_value.u8 = *(uint8_t *)value_p;
3237c478bd9Sstevel@tonic-gate 			break;
3247c478bd9Sstevel@tonic-gate 		case sizeof (uint16_t):
3257c478bd9Sstevel@tonic-gate 			peekpoke_value.u16 = *(uint16_t *)value_p;
3267c478bd9Sstevel@tonic-gate 			break;
3277c478bd9Sstevel@tonic-gate 		case sizeof (uint32_t):
3287c478bd9Sstevel@tonic-gate 			peekpoke_value.u32 = *(uint32_t *)value_p;
3297c478bd9Sstevel@tonic-gate 			break;
3307c478bd9Sstevel@tonic-gate 		case sizeof (uint64_t):
3317c478bd9Sstevel@tonic-gate 			peekpoke_value.u64 = *(uint64_t *)value_p;
3327c478bd9Sstevel@tonic-gate 			break;
3337c478bd9Sstevel@tonic-gate 		}
3347c478bd9Sstevel@tonic-gate 	}
3357c478bd9Sstevel@tonic-gate 
336abdbd06dSagiri 	peekpoke_args.host_addr = (uintptr_t)&peekpoke_value.u64;
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate 	if (devi != NULL)
3397c478bd9Sstevel@tonic-gate 		rval = ddi_ctlops(devi, devi, cmd, &peekpoke_args,
3407c478bd9Sstevel@tonic-gate 		    &dummy_result);
3417c478bd9Sstevel@tonic-gate 	else
3427c478bd9Sstevel@tonic-gate 		rval = peekpoke_mem(cmd, &peekpoke_args);
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 	/*
3457c478bd9Sstevel@tonic-gate 	 * A NULL value_p is permitted by ddi_peek(9F); discard the result.
3467c478bd9Sstevel@tonic-gate 	 */
3477c478bd9Sstevel@tonic-gate 	if ((cmd == DDI_CTLOPS_PEEK) & (value_p != NULL)) {
3487c478bd9Sstevel@tonic-gate 		switch (size) {
3497c478bd9Sstevel@tonic-gate 		case sizeof (uint8_t):
3507c478bd9Sstevel@tonic-gate 			*(uint8_t *)value_p = peekpoke_value.u8;
3517c478bd9Sstevel@tonic-gate 			break;
3527c478bd9Sstevel@tonic-gate 		case sizeof (uint16_t):
3537c478bd9Sstevel@tonic-gate 			*(uint16_t *)value_p = peekpoke_value.u16;
3547c478bd9Sstevel@tonic-gate 			break;
3557c478bd9Sstevel@tonic-gate 		case sizeof (uint32_t):
3567c478bd9Sstevel@tonic-gate 			*(uint32_t *)value_p = peekpoke_value.u32;
3577c478bd9Sstevel@tonic-gate 			break;
3587c478bd9Sstevel@tonic-gate 		case sizeof (uint64_t):
3597c478bd9Sstevel@tonic-gate 			*(uint64_t *)value_p = peekpoke_value.u64;
3607c478bd9Sstevel@tonic-gate 			break;
3617c478bd9Sstevel@tonic-gate 		}
3627c478bd9Sstevel@tonic-gate 	}
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 	return (rval);
3657c478bd9Sstevel@tonic-gate }
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate /*
3687c478bd9Sstevel@tonic-gate  * Keep ddi_peek() and ddi_poke() in case 3rd parties are calling this.
3697c478bd9Sstevel@tonic-gate  * they shouldn't be, but the 9f manpage kind of pseudo exposes it.
3707c478bd9Sstevel@tonic-gate  */
3717c478bd9Sstevel@tonic-gate int
3727c478bd9Sstevel@tonic-gate ddi_peek(dev_info_t *devi, size_t size, void *addr, void *value_p)
3737c478bd9Sstevel@tonic-gate {
3747c478bd9Sstevel@tonic-gate 	switch (size) {
3757c478bd9Sstevel@tonic-gate 	case sizeof (uint8_t):
3767c478bd9Sstevel@tonic-gate 	case sizeof (uint16_t):
3777c478bd9Sstevel@tonic-gate 	case sizeof (uint32_t):
3787c478bd9Sstevel@tonic-gate 	case sizeof (uint64_t):
3797c478bd9Sstevel@tonic-gate 		break;
3807c478bd9Sstevel@tonic-gate 	default:
3817c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
3827c478bd9Sstevel@tonic-gate 	}
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 	return (i_ddi_peekpoke(devi, DDI_CTLOPS_PEEK, size, addr, value_p));
3857c478bd9Sstevel@tonic-gate }
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate int
3887c478bd9Sstevel@tonic-gate ddi_poke(dev_info_t *devi, size_t size, void *addr, void *value_p)
3897c478bd9Sstevel@tonic-gate {
3907c478bd9Sstevel@tonic-gate 	switch (size) {
3917c478bd9Sstevel@tonic-gate 	case sizeof (uint8_t):
3927c478bd9Sstevel@tonic-gate 	case sizeof (uint16_t):
3937c478bd9Sstevel@tonic-gate 	case sizeof (uint32_t):
3947c478bd9Sstevel@tonic-gate 	case sizeof (uint64_t):
3957c478bd9Sstevel@tonic-gate 		break;
3967c478bd9Sstevel@tonic-gate 	default:
3977c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
3987c478bd9Sstevel@tonic-gate 	}
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate 	return (i_ddi_peekpoke(devi, DDI_CTLOPS_POKE, size, addr, value_p));
4017c478bd9Sstevel@tonic-gate }
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate int
4047c478bd9Sstevel@tonic-gate ddi_peek8(dev_info_t *dip, int8_t *addr, int8_t *val_p)
4057c478bd9Sstevel@tonic-gate {
4067c478bd9Sstevel@tonic-gate 	return (i_ddi_peekpoke(dip, DDI_CTLOPS_PEEK, sizeof (*val_p), addr,
4077c478bd9Sstevel@tonic-gate 	    val_p));
4087c478bd9Sstevel@tonic-gate }
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate int
4117c478bd9Sstevel@tonic-gate ddi_peek16(dev_info_t *dip, int16_t *addr, int16_t *val_p)
4127c478bd9Sstevel@tonic-gate {
4137c478bd9Sstevel@tonic-gate 	return (i_ddi_peekpoke(dip, DDI_CTLOPS_PEEK, sizeof (*val_p), addr,
4147c478bd9Sstevel@tonic-gate 	    val_p));
4157c478bd9Sstevel@tonic-gate }
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate int
4187c478bd9Sstevel@tonic-gate ddi_peek32(dev_info_t *dip, int32_t *addr, int32_t *val_p)
4197c478bd9Sstevel@tonic-gate {
4207c478bd9Sstevel@tonic-gate 	return (i_ddi_peekpoke(dip, DDI_CTLOPS_PEEK, sizeof (*val_p), addr,
4217c478bd9Sstevel@tonic-gate 	    val_p));
4227c478bd9Sstevel@tonic-gate }
4237c478bd9Sstevel@tonic-gate 
4247c478bd9Sstevel@tonic-gate int
4257c478bd9Sstevel@tonic-gate ddi_peek64(dev_info_t *dip, int64_t *addr, int64_t *val_p)
4264ab75253Smrj {
4274ab75253Smrj 	return (i_ddi_peekpoke(dip, DDI_CTLOPS_PEEK, sizeof (*val_p), addr,
4284ab75253Smrj 	    val_p));
4294ab75253Smrj }
4304ab75253Smrj 
4314ab75253Smrj 
4324ab75253Smrj /*
4334ab75253Smrj  * We need to separate the old interfaces from the new ones and leave them
4344ab75253Smrj  * in here for a while. Previous versions of the OS defined the new interfaces
4354ab75253Smrj  * to the old interfaces. This way we can fix things up so that we can
4364ab75253Smrj  * eventually remove these interfaces.
4374ab75253Smrj  * e.g. A 3rd party module/driver using ddi_peek8 and built against S10
4384ab75253Smrj  * or earlier will actually have a reference to ddi_peekc in the binary.
4394ab75253Smrj  */
4404ab75253Smrj #ifdef _ILP32
4414ab75253Smrj int
4424ab75253Smrj ddi_peekc(dev_info_t *dip, int8_t *addr, int8_t *val_p)
4434ab75253Smrj {
4444ab75253Smrj 	return (i_ddi_peekpoke(dip, DDI_CTLOPS_PEEK, sizeof (*val_p), addr,
4454ab75253Smrj 	    val_p));
4464ab75253Smrj }
4474ab75253Smrj 
4484ab75253Smrj int
4494ab75253Smrj ddi_peeks(dev_info_t *dip, int16_t *addr, int16_t *val_p)
4504ab75253Smrj {
4514ab75253Smrj 	return (i_ddi_peekpoke(dip, DDI_CTLOPS_PEEK, sizeof (*val_p), addr,
4524ab75253Smrj 	    val_p));
4534ab75253Smrj }
4544ab75253Smrj 
4554ab75253Smrj int
4564ab75253Smrj ddi_peekl(dev_info_t *dip, int32_t *addr, int32_t *val_p)
4574ab75253Smrj {
4584ab75253Smrj 	return (i_ddi_peekpoke(dip, DDI_CTLOPS_PEEK, sizeof (*val_p), addr,
4594ab75253Smrj 	    val_p));
4604ab75253Smrj }
4614ab75253Smrj 
4627c478bd9Sstevel@tonic-gate int
4637c478bd9Sstevel@tonic-gate ddi_peekd(dev_info_t *dip, int64_t *addr, int64_t *val_p)
4647c478bd9Sstevel@tonic-gate {
4657c478bd9Sstevel@tonic-gate 	return (i_ddi_peekpoke(dip, DDI_CTLOPS_PEEK, sizeof (*val_p), addr,
4667c478bd9Sstevel@tonic-gate 	    val_p));
4677c478bd9Sstevel@tonic-gate }
4684ab75253Smrj #endif /* _ILP32 */
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate int
4717c478bd9Sstevel@tonic-gate ddi_poke8(dev_info_t *dip, int8_t *addr, int8_t val)
4727c478bd9Sstevel@tonic-gate {
4737c478bd9Sstevel@tonic-gate 	return (i_ddi_peekpoke(dip, DDI_CTLOPS_POKE, sizeof (val), addr, &val));
4747c478bd9Sstevel@tonic-gate }
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate int
4777c478bd9Sstevel@tonic-gate ddi_poke16(dev_info_t *dip, int16_t *addr, int16_t val)
4787c478bd9Sstevel@tonic-gate {
4797c478bd9Sstevel@tonic-gate 	return (i_ddi_peekpoke(dip, DDI_CTLOPS_POKE, sizeof (val), addr, &val));
4807c478bd9Sstevel@tonic-gate }
4817c478bd9Sstevel@tonic-gate 
4827c478bd9Sstevel@tonic-gate int
4837c478bd9Sstevel@tonic-gate ddi_poke32(dev_info_t *dip, int32_t *addr, int32_t val)
4847c478bd9Sstevel@tonic-gate {
4857c478bd9Sstevel@tonic-gate 	return (i_ddi_peekpoke(dip, DDI_CTLOPS_POKE, sizeof (val), addr, &val));
4867c478bd9Sstevel@tonic-gate }
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate int
4897c478bd9Sstevel@tonic-gate ddi_poke64(dev_info_t *dip, int64_t *addr, int64_t val)
4904ab75253Smrj {
4914ab75253Smrj 	return (i_ddi_peekpoke(dip, DDI_CTLOPS_POKE, sizeof (val), addr, &val));
4924ab75253Smrj }
4934ab75253Smrj 
4944ab75253Smrj /*
4954ab75253Smrj  * We need to separate the old interfaces from the new ones and leave them
4964ab75253Smrj  * in here for a while. Previous versions of the OS defined the new interfaces
4974ab75253Smrj  * to the old interfaces. This way we can fix things up so that we can
4984ab75253Smrj  * eventually remove these interfaces.
4994ab75253Smrj  * e.g. A 3rd party module/driver using ddi_poke8 and built against S10
5004ab75253Smrj  * or earlier will actually have a reference to ddi_pokec in the binary.
5014ab75253Smrj  */
5024ab75253Smrj #ifdef _ILP32
5034ab75253Smrj int
5044ab75253Smrj ddi_pokec(dev_info_t *dip, int8_t *addr, int8_t val)
5054ab75253Smrj {
5064ab75253Smrj 	return (i_ddi_peekpoke(dip, DDI_CTLOPS_POKE, sizeof (val), addr, &val));
5074ab75253Smrj }
5084ab75253Smrj 
5094ab75253Smrj int
5104ab75253Smrj ddi_pokes(dev_info_t *dip, int16_t *addr, int16_t val)
5114ab75253Smrj {
5124ab75253Smrj 	return (i_ddi_peekpoke(dip, DDI_CTLOPS_POKE, sizeof (val), addr, &val));
5134ab75253Smrj }
5144ab75253Smrj 
5154ab75253Smrj int
5164ab75253Smrj ddi_pokel(dev_info_t *dip, int32_t *addr, int32_t val)
5174ab75253Smrj {
5184ab75253Smrj 	return (i_ddi_peekpoke(dip, DDI_CTLOPS_POKE, sizeof (val), addr, &val));
5194ab75253Smrj }
5204ab75253Smrj 
5217c478bd9Sstevel@tonic-gate int
5227c478bd9Sstevel@tonic-gate ddi_poked(dev_info_t *dip, int64_t *addr, int64_t val)
5237c478bd9Sstevel@tonic-gate {
5247c478bd9Sstevel@tonic-gate 	return (i_ddi_peekpoke(dip, DDI_CTLOPS_POKE, sizeof (val), addr, &val));
5257c478bd9Sstevel@tonic-gate }
5264ab75253Smrj #endif /* _ILP32 */
5277c478bd9Sstevel@tonic-gate 
5287c478bd9Sstevel@tonic-gate /*
5297c478bd9Sstevel@tonic-gate  * ddi_peekpokeio() is used primarily by the mem drivers for moving
5307c478bd9Sstevel@tonic-gate  * data to and from uio structures via peek and poke.  Note that we
5317c478bd9Sstevel@tonic-gate  * use "internal" routines ddi_peek and ddi_poke to make this go
5327c478bd9Sstevel@tonic-gate  * slightly faster, avoiding the call overhead ..
5337c478bd9Sstevel@tonic-gate  */
5347c478bd9Sstevel@tonic-gate int
5357c478bd9Sstevel@tonic-gate ddi_peekpokeio(dev_info_t *devi, struct uio *uio, enum uio_rw rw,
5367c478bd9Sstevel@tonic-gate     caddr_t addr, size_t len, uint_t xfersize)
5377c478bd9Sstevel@tonic-gate {
5387c478bd9Sstevel@tonic-gate 	int64_t	ibuffer;
5397c478bd9Sstevel@tonic-gate 	int8_t w8;
5407c478bd9Sstevel@tonic-gate 	size_t sz;
5417c478bd9Sstevel@tonic-gate 	int o;
5427c478bd9Sstevel@tonic-gate 
5437c478bd9Sstevel@tonic-gate 	if (xfersize > sizeof (long))
5447c478bd9Sstevel@tonic-gate 		xfersize = sizeof (long);
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate 	while (len != 0) {
5477c478bd9Sstevel@tonic-gate 		if ((len | (uintptr_t)addr) & 1) {
5487c478bd9Sstevel@tonic-gate 			sz = sizeof (int8_t);
5497c478bd9Sstevel@tonic-gate 			if (rw == UIO_WRITE) {
5507c478bd9Sstevel@tonic-gate 				if ((o = uwritec(uio)) == -1)
5517c478bd9Sstevel@tonic-gate 					return (DDI_FAILURE);
5527c478bd9Sstevel@tonic-gate 				if (ddi_poke8(devi, (int8_t *)addr,
5537c478bd9Sstevel@tonic-gate 				    (int8_t)o) != DDI_SUCCESS)
5547c478bd9Sstevel@tonic-gate 					return (DDI_FAILURE);
5557c478bd9Sstevel@tonic-gate 			} else {
5567c478bd9Sstevel@tonic-gate 				if (i_ddi_peekpoke(devi, DDI_CTLOPS_PEEK, sz,
5577c478bd9Sstevel@tonic-gate 				    (int8_t *)addr, &w8) != DDI_SUCCESS)
5587c478bd9Sstevel@tonic-gate 					return (DDI_FAILURE);
5597c478bd9Sstevel@tonic-gate 				if (ureadc(w8, uio))
5607c478bd9Sstevel@tonic-gate 					return (DDI_FAILURE);
5617c478bd9Sstevel@tonic-gate 			}
5627c478bd9Sstevel@tonic-gate 		} else {
5637c478bd9Sstevel@tonic-gate 			switch (xfersize) {
5647c478bd9Sstevel@tonic-gate 			case sizeof (int64_t):
5657c478bd9Sstevel@tonic-gate 				if (((len | (uintptr_t)addr) &
5667c478bd9Sstevel@tonic-gate 				    (sizeof (int64_t) - 1)) == 0) {
5677c478bd9Sstevel@tonic-gate 					sz = xfersize;
5687c478bd9Sstevel@tonic-gate 					break;
5697c478bd9Sstevel@tonic-gate 				}
5707c478bd9Sstevel@tonic-gate 				/*FALLTHROUGH*/
5717c478bd9Sstevel@tonic-gate 			case sizeof (int32_t):
5727c478bd9Sstevel@tonic-gate 				if (((len | (uintptr_t)addr) &
5737c478bd9Sstevel@tonic-gate 				    (sizeof (int32_t) - 1)) == 0) {
5747c478bd9Sstevel@tonic-gate 					sz = xfersize;
5757c478bd9Sstevel@tonic-gate 					break;
5767c478bd9Sstevel@tonic-gate 				}
5777c478bd9Sstevel@tonic-gate 				/*FALLTHROUGH*/
5787c478bd9Sstevel@tonic-gate 			default:
5797c478bd9Sstevel@tonic-gate 				/*
5807c478bd9Sstevel@tonic-gate 				 * This still assumes that we might have an
5817c478bd9Sstevel@tonic-gate 				 * I/O bus out there that permits 16-bit
5827c478bd9Sstevel@tonic-gate 				 * transfers (and that it would be upset by
5837c478bd9Sstevel@tonic-gate 				 * 32-bit transfers from such locations).
5847c478bd9Sstevel@tonic-gate 				 */
5857c478bd9Sstevel@tonic-gate 				sz = sizeof (int16_t);
5867c478bd9Sstevel@tonic-gate 				break;
5877c478bd9Sstevel@tonic-gate 			}
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate 			if (rw == UIO_READ) {
5907c478bd9Sstevel@tonic-gate 				if (i_ddi_peekpoke(devi, DDI_CTLOPS_PEEK, sz,
5917c478bd9Sstevel@tonic-gate 				    addr, &ibuffer) != DDI_SUCCESS)
5927c478bd9Sstevel@tonic-gate 					return (DDI_FAILURE);
5937c478bd9Sstevel@tonic-gate 			}
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate 			if (uiomove(&ibuffer, sz, rw, uio))
5967c478bd9Sstevel@tonic-gate 				return (DDI_FAILURE);
5977c478bd9Sstevel@tonic-gate 
5987c478bd9Sstevel@tonic-gate 			if (rw == UIO_WRITE) {
5997c478bd9Sstevel@tonic-gate 				if (i_ddi_peekpoke(devi, DDI_CTLOPS_POKE, sz,
6007c478bd9Sstevel@tonic-gate 				    addr, &ibuffer) != DDI_SUCCESS)
6017c478bd9Sstevel@tonic-gate 					return (DDI_FAILURE);
6027c478bd9Sstevel@tonic-gate 			}
6037c478bd9Sstevel@tonic-gate 		}
6047c478bd9Sstevel@tonic-gate 		addr += sz;
6057c478bd9Sstevel@tonic-gate 		len -= sz;
6067c478bd9Sstevel@tonic-gate 	}
6077c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
6087c478bd9Sstevel@tonic-gate }
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate /*
6117c478bd9Sstevel@tonic-gate  * These routines are used by drivers that do layered ioctls
6127c478bd9Sstevel@tonic-gate  * On sparc, they're implemented in assembler to avoid spilling
6137c478bd9Sstevel@tonic-gate  * register windows in the common (copyin) case ..
6147c478bd9Sstevel@tonic-gate  */
6157c478bd9Sstevel@tonic-gate #if !defined(__sparc)
6167c478bd9Sstevel@tonic-gate int
6177c478bd9Sstevel@tonic-gate ddi_copyin(const void *buf, void *kernbuf, size_t size, int flags)
6187c478bd9Sstevel@tonic-gate {
6197c478bd9Sstevel@tonic-gate 	if (flags & FKIOCTL)
6207c478bd9Sstevel@tonic-gate 		return (kcopy(buf, kernbuf, size) ? -1 : 0);
6217c478bd9Sstevel@tonic-gate 	return (copyin(buf, kernbuf, size));
6227c478bd9Sstevel@tonic-gate }
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate int
6257c478bd9Sstevel@tonic-gate ddi_copyout(const void *buf, void *kernbuf, size_t size, int flags)
6267c478bd9Sstevel@tonic-gate {
6277c478bd9Sstevel@tonic-gate 	if (flags & FKIOCTL)
6287c478bd9Sstevel@tonic-gate 		return (kcopy(buf, kernbuf, size) ? -1 : 0);
6297c478bd9Sstevel@tonic-gate 	return (copyout(buf, kernbuf, size));
6307c478bd9Sstevel@tonic-gate }
6317c478bd9Sstevel@tonic-gate #endif	/* !__sparc */
6327c478bd9Sstevel@tonic-gate 
6337c478bd9Sstevel@tonic-gate /*
6347c478bd9Sstevel@tonic-gate  * Conversions in nexus pagesize units.  We don't duplicate the
6357c478bd9Sstevel@tonic-gate  * 'nil dip' semantics of peek/poke because btopr/btop/ptob are DDI/DKI
6367c478bd9Sstevel@tonic-gate  * routines anyway.
6377c478bd9Sstevel@tonic-gate  */
6387c478bd9Sstevel@tonic-gate unsigned long
6397c478bd9Sstevel@tonic-gate ddi_btop(dev_info_t *dip, unsigned long bytes)
6407c478bd9Sstevel@tonic-gate {
6417c478bd9Sstevel@tonic-gate 	unsigned long pages;
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate 	(void) ddi_ctlops(dip, dip, DDI_CTLOPS_BTOP, &bytes, &pages);
6447c478bd9Sstevel@tonic-gate 	return (pages);
6457c478bd9Sstevel@tonic-gate }
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate unsigned long
6487c478bd9Sstevel@tonic-gate ddi_btopr(dev_info_t *dip, unsigned long bytes)
6497c478bd9Sstevel@tonic-gate {
6507c478bd9Sstevel@tonic-gate 	unsigned long pages;
6517c478bd9Sstevel@tonic-gate 
6527c478bd9Sstevel@tonic-gate 	(void) ddi_ctlops(dip, dip, DDI_CTLOPS_BTOPR, &bytes, &pages);
6537c478bd9Sstevel@tonic-gate 	return (pages);
6547c478bd9Sstevel@tonic-gate }
6557c478bd9Sstevel@tonic-gate 
6567c478bd9Sstevel@tonic-gate unsigned long
6577c478bd9Sstevel@tonic-gate ddi_ptob(dev_info_t *dip, unsigned long pages)
6587c478bd9Sstevel@tonic-gate {
6597c478bd9Sstevel@tonic-gate 	unsigned long bytes;
6607c478bd9Sstevel@tonic-gate 
6617c478bd9Sstevel@tonic-gate 	(void) ddi_ctlops(dip, dip, DDI_CTLOPS_PTOB, &pages, &bytes);
6627c478bd9Sstevel@tonic-gate 	return (bytes);
6637c478bd9Sstevel@tonic-gate }
6647c478bd9Sstevel@tonic-gate 
6657c478bd9Sstevel@tonic-gate unsigned int
6667c478bd9Sstevel@tonic-gate ddi_enter_critical(void)
6677c478bd9Sstevel@tonic-gate {
6687c478bd9Sstevel@tonic-gate 	return ((uint_t)spl7());
6697c478bd9Sstevel@tonic-gate }
6707c478bd9Sstevel@tonic-gate 
6717c478bd9Sstevel@tonic-gate void
6727c478bd9Sstevel@tonic-gate ddi_exit_critical(unsigned int spl)
6737c478bd9Sstevel@tonic-gate {
6747c478bd9Sstevel@tonic-gate 	splx((int)spl);
6757c478bd9Sstevel@tonic-gate }
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate /*
6787c478bd9Sstevel@tonic-gate  * Nexus ctlops punter
6797c478bd9Sstevel@tonic-gate  */
6807c478bd9Sstevel@tonic-gate 
6817c478bd9Sstevel@tonic-gate #if !defined(__sparc)
6827c478bd9Sstevel@tonic-gate /*
6837c478bd9Sstevel@tonic-gate  * Request bus_ctl parent to handle a bus_ctl request
6847c478bd9Sstevel@tonic-gate  *
6857c478bd9Sstevel@tonic-gate  * (The sparc version is in sparc_ddi.s)
6867c478bd9Sstevel@tonic-gate  */
6877c478bd9Sstevel@tonic-gate int
6887c478bd9Sstevel@tonic-gate ddi_ctlops(dev_info_t *d, dev_info_t *r, ddi_ctl_enum_t op, void *a, void *v)
6897c478bd9Sstevel@tonic-gate {
6907c478bd9Sstevel@tonic-gate 	int (*fp)();
6917c478bd9Sstevel@tonic-gate 
6927c478bd9Sstevel@tonic-gate 	if (!d || !r)
6937c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
6947c478bd9Sstevel@tonic-gate 
6957c478bd9Sstevel@tonic-gate 	if ((d = (dev_info_t *)DEVI(d)->devi_bus_ctl) == NULL)
6967c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
6977c478bd9Sstevel@tonic-gate 
6987c478bd9Sstevel@tonic-gate 	fp = DEVI(d)->devi_ops->devo_bus_ops->bus_ctl;
6997c478bd9Sstevel@tonic-gate 	return ((*fp)(d, r, op, a, v));
7007c478bd9Sstevel@tonic-gate }
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate #endif
7037c478bd9Sstevel@tonic-gate 
7047c478bd9Sstevel@tonic-gate /*
7057c478bd9Sstevel@tonic-gate  * DMA/DVMA setup
7067c478bd9Sstevel@tonic-gate  */
7077c478bd9Sstevel@tonic-gate 
7087c478bd9Sstevel@tonic-gate #if defined(__sparc)
7097c478bd9Sstevel@tonic-gate static ddi_dma_lim_t standard_limits = {
7107c478bd9Sstevel@tonic-gate 	(uint_t)0,	/* addr_t dlim_addr_lo */
7117c478bd9Sstevel@tonic-gate 	(uint_t)-1,	/* addr_t dlim_addr_hi */
7127c478bd9Sstevel@tonic-gate 	(uint_t)-1,	/* uint_t dlim_cntr_max */
7137c478bd9Sstevel@tonic-gate 	(uint_t)1,	/* uint_t dlim_burstsizes */
7147c478bd9Sstevel@tonic-gate 	(uint_t)1,	/* uint_t dlim_minxfer */
7157c478bd9Sstevel@tonic-gate 	0		/* uint_t dlim_dmaspeed */
7167c478bd9Sstevel@tonic-gate };
7177c478bd9Sstevel@tonic-gate #elif defined(__x86)
7187c478bd9Sstevel@tonic-gate static ddi_dma_lim_t standard_limits = {
7197c478bd9Sstevel@tonic-gate 	(uint_t)0,		/* addr_t dlim_addr_lo */
7207c478bd9Sstevel@tonic-gate 	(uint_t)0xffffff,	/* addr_t dlim_addr_hi */
7217c478bd9Sstevel@tonic-gate 	(uint_t)0,		/* uint_t dlim_cntr_max */
7227c478bd9Sstevel@tonic-gate 	(uint_t)0x00000001,	/* uint_t dlim_burstsizes */
7237c478bd9Sstevel@tonic-gate 	(uint_t)DMA_UNIT_8,	/* uint_t dlim_minxfer */
7247c478bd9Sstevel@tonic-gate 	(uint_t)0,		/* uint_t dlim_dmaspeed */
7257c478bd9Sstevel@tonic-gate 	(uint_t)0x86<<24+0,	/* uint_t dlim_version */
7267c478bd9Sstevel@tonic-gate 	(uint_t)0xffff,		/* uint_t dlim_adreg_max */
7277c478bd9Sstevel@tonic-gate 	(uint_t)0xffff,		/* uint_t dlim_ctreg_max */
7287c478bd9Sstevel@tonic-gate 	(uint_t)512,		/* uint_t dlim_granular */
7297c478bd9Sstevel@tonic-gate 	(int)1,			/* int dlim_sgllen */
7307c478bd9Sstevel@tonic-gate 	(uint_t)0xffffffff	/* uint_t dlim_reqsizes */
7317c478bd9Sstevel@tonic-gate };
7327c478bd9Sstevel@tonic-gate 
7337c478bd9Sstevel@tonic-gate #endif
7347c478bd9Sstevel@tonic-gate 
7357c478bd9Sstevel@tonic-gate int
7367c478bd9Sstevel@tonic-gate ddi_dma_setup(dev_info_t *dip, struct ddi_dma_req *dmareqp,
7377c478bd9Sstevel@tonic-gate     ddi_dma_handle_t *handlep)
7387c478bd9Sstevel@tonic-gate {
7397c478bd9Sstevel@tonic-gate 	int (*funcp)() = ddi_dma_map;
7407c478bd9Sstevel@tonic-gate 	struct bus_ops *bop;
7417c478bd9Sstevel@tonic-gate #if defined(__sparc)
7427c478bd9Sstevel@tonic-gate 	auto ddi_dma_lim_t dma_lim;
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate 	if (dmareqp->dmar_limits == (ddi_dma_lim_t *)0) {
7457c478bd9Sstevel@tonic-gate 		dma_lim = standard_limits;
7467c478bd9Sstevel@tonic-gate 	} else {
7477c478bd9Sstevel@tonic-gate 		dma_lim = *dmareqp->dmar_limits;
7487c478bd9Sstevel@tonic-gate 	}
7497c478bd9Sstevel@tonic-gate 	dmareqp->dmar_limits = &dma_lim;
7507c478bd9Sstevel@tonic-gate #endif
7517c478bd9Sstevel@tonic-gate #if defined(__x86)
7527c478bd9Sstevel@tonic-gate 	if (dmareqp->dmar_limits == (ddi_dma_lim_t *)0)
7537c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
7547c478bd9Sstevel@tonic-gate #endif
7557c478bd9Sstevel@tonic-gate 
7567c478bd9Sstevel@tonic-gate 	/*
7577c478bd9Sstevel@tonic-gate 	 * Handle the case that the requester is both a leaf
7587c478bd9Sstevel@tonic-gate 	 * and a nexus driver simultaneously by calling the
7597c478bd9Sstevel@tonic-gate 	 * requester's bus_dma_map function directly instead
7607c478bd9Sstevel@tonic-gate 	 * of ddi_dma_map.
7617c478bd9Sstevel@tonic-gate 	 */
7627c478bd9Sstevel@tonic-gate 	bop = DEVI(dip)->devi_ops->devo_bus_ops;
7637c478bd9Sstevel@tonic-gate 	if (bop && bop->bus_dma_map)
7647c478bd9Sstevel@tonic-gate 		funcp = bop->bus_dma_map;
7657c478bd9Sstevel@tonic-gate 	return ((*funcp)(dip, dip, dmareqp, handlep));
7667c478bd9Sstevel@tonic-gate }
7677c478bd9Sstevel@tonic-gate 
7687c478bd9Sstevel@tonic-gate int
7697c478bd9Sstevel@tonic-gate ddi_dma_addr_setup(dev_info_t *dip, struct as *as, caddr_t addr, size_t len,
7707c478bd9Sstevel@tonic-gate     uint_t flags, int (*waitfp)(), caddr_t arg,
7717c478bd9Sstevel@tonic-gate     ddi_dma_lim_t *limits, ddi_dma_handle_t *handlep)
7727c478bd9Sstevel@tonic-gate {
7737c478bd9Sstevel@tonic-gate 	int (*funcp)() = ddi_dma_map;
7747c478bd9Sstevel@tonic-gate 	ddi_dma_lim_t dma_lim;
7757c478bd9Sstevel@tonic-gate 	struct ddi_dma_req dmareq;
7767c478bd9Sstevel@tonic-gate 	struct bus_ops *bop;
7777c478bd9Sstevel@tonic-gate 
7787c478bd9Sstevel@tonic-gate 	if (len == 0) {
7797c478bd9Sstevel@tonic-gate 		return (DDI_DMA_NOMAPPING);
7807c478bd9Sstevel@tonic-gate 	}
7817c478bd9Sstevel@tonic-gate 	if (limits == (ddi_dma_lim_t *)0) {
7827c478bd9Sstevel@tonic-gate 		dma_lim = standard_limits;
7837c478bd9Sstevel@tonic-gate 	} else {
7847c478bd9Sstevel@tonic-gate 		dma_lim = *limits;
7857c478bd9Sstevel@tonic-gate 	}
7867c478bd9Sstevel@tonic-gate 	dmareq.dmar_limits = &dma_lim;
7877c478bd9Sstevel@tonic-gate 	dmareq.dmar_flags = flags;
7887c478bd9Sstevel@tonic-gate 	dmareq.dmar_fp = waitfp;
7897c478bd9Sstevel@tonic-gate 	dmareq.dmar_arg = arg;
7907c478bd9Sstevel@tonic-gate 	dmareq.dmar_object.dmao_size = len;
7917c478bd9Sstevel@tonic-gate 	dmareq.dmar_object.dmao_type = DMA_OTYP_VADDR;
7927c478bd9Sstevel@tonic-gate 	dmareq.dmar_object.dmao_obj.virt_obj.v_as = as;
7937c478bd9Sstevel@tonic-gate 	dmareq.dmar_object.dmao_obj.virt_obj.v_addr = addr;
7947c478bd9Sstevel@tonic-gate 	dmareq.dmar_object.dmao_obj.virt_obj.v_priv = NULL;
7957c478bd9Sstevel@tonic-gate 
7967c478bd9Sstevel@tonic-gate 	/*
7977c478bd9Sstevel@tonic-gate 	 * Handle the case that the requester is both a leaf
7987c478bd9Sstevel@tonic-gate 	 * and a nexus driver simultaneously by calling the
7997c478bd9Sstevel@tonic-gate 	 * requester's bus_dma_map function directly instead
8007c478bd9Sstevel@tonic-gate 	 * of ddi_dma_map.
8017c478bd9Sstevel@tonic-gate 	 */
8027c478bd9Sstevel@tonic-gate 	bop = DEVI(dip)->devi_ops->devo_bus_ops;
8037c478bd9Sstevel@tonic-gate 	if (bop && bop->bus_dma_map)
8047c478bd9Sstevel@tonic-gate 		funcp = bop->bus_dma_map;
8057c478bd9Sstevel@tonic-gate 
8067c478bd9Sstevel@tonic-gate 	return ((*funcp)(dip, dip, &dmareq, handlep));
8077c478bd9Sstevel@tonic-gate }
8087c478bd9Sstevel@tonic-gate 
8097c478bd9Sstevel@tonic-gate int
8107c478bd9Sstevel@tonic-gate ddi_dma_buf_setup(dev_info_t *dip, struct buf *bp, uint_t flags,
8117c478bd9Sstevel@tonic-gate     int (*waitfp)(), caddr_t arg, ddi_dma_lim_t *limits,
8127c478bd9Sstevel@tonic-gate     ddi_dma_handle_t *handlep)
8137c478bd9Sstevel@tonic-gate {
8147c478bd9Sstevel@tonic-gate 	int (*funcp)() = ddi_dma_map;
8157c478bd9Sstevel@tonic-gate 	ddi_dma_lim_t dma_lim;
8167c478bd9Sstevel@tonic-gate 	struct ddi_dma_req dmareq;
8177c478bd9Sstevel@tonic-gate 	struct bus_ops *bop;
8187c478bd9Sstevel@tonic-gate 
8197c478bd9Sstevel@tonic-gate 	if (limits == (ddi_dma_lim_t *)0) {
8207c478bd9Sstevel@tonic-gate 		dma_lim = standard_limits;
8217c478bd9Sstevel@tonic-gate 	} else {
8227c478bd9Sstevel@tonic-gate 		dma_lim = *limits;
8237c478bd9Sstevel@tonic-gate 	}
8247c478bd9Sstevel@tonic-gate 	dmareq.dmar_limits = &dma_lim;
8257c478bd9Sstevel@tonic-gate 	dmareq.dmar_flags = flags;
8267c478bd9Sstevel@tonic-gate 	dmareq.dmar_fp = waitfp;
8277c478bd9Sstevel@tonic-gate 	dmareq.dmar_arg = arg;
8287c478bd9Sstevel@tonic-gate 	dmareq.dmar_object.dmao_size = (uint_t)bp->b_bcount;
8297c478bd9Sstevel@tonic-gate 
8302d3bf78eScth 	if (bp->b_flags & B_PAGEIO) {
8317c478bd9Sstevel@tonic-gate 		dmareq.dmar_object.dmao_type = DMA_OTYP_PAGES;
8327c478bd9Sstevel@tonic-gate 		dmareq.dmar_object.dmao_obj.pp_obj.pp_pp = bp->b_pages;
8337c478bd9Sstevel@tonic-gate 		dmareq.dmar_object.dmao_obj.pp_obj.pp_offset =
8347c478bd9Sstevel@tonic-gate 		    (uint_t)(((uintptr_t)bp->b_un.b_addr) & MMU_PAGEOFFSET);
8357c478bd9Sstevel@tonic-gate 	} else {
8367c478bd9Sstevel@tonic-gate 		dmareq.dmar_object.dmao_type = DMA_OTYP_BUFVADDR;
8377c478bd9Sstevel@tonic-gate 		dmareq.dmar_object.dmao_obj.virt_obj.v_addr = bp->b_un.b_addr;
8382d3bf78eScth 		if (bp->b_flags & B_SHADOW) {
8397c478bd9Sstevel@tonic-gate 			dmareq.dmar_object.dmao_obj.virt_obj.v_priv =
840184cd04cScth 			    bp->b_shadow;
8417c478bd9Sstevel@tonic-gate 		} else {
8427c478bd9Sstevel@tonic-gate 			dmareq.dmar_object.dmao_obj.virt_obj.v_priv = NULL;
8437c478bd9Sstevel@tonic-gate 		}
8447c478bd9Sstevel@tonic-gate 
8457c478bd9Sstevel@tonic-gate 		/*
8467c478bd9Sstevel@tonic-gate 		 * If the buffer has no proc pointer, or the proc
8477c478bd9Sstevel@tonic-gate 		 * struct has the kernel address space, or the buffer has
8487c478bd9Sstevel@tonic-gate 		 * been marked B_REMAPPED (meaning that it is now
8497c478bd9Sstevel@tonic-gate 		 * mapped into the kernel's address space), then
8507c478bd9Sstevel@tonic-gate 		 * the address space is kas (kernel address space).
8517c478bd9Sstevel@tonic-gate 		 */
8522d3bf78eScth 		if ((bp->b_proc == NULL) || (bp->b_proc->p_as == &kas) ||
8532d3bf78eScth 		    (bp->b_flags & B_REMAPPED)) {
8547c478bd9Sstevel@tonic-gate 			dmareq.dmar_object.dmao_obj.virt_obj.v_as = 0;
8557c478bd9Sstevel@tonic-gate 		} else {
8567c478bd9Sstevel@tonic-gate 			dmareq.dmar_object.dmao_obj.virt_obj.v_as =
8577c478bd9Sstevel@tonic-gate 			    bp->b_proc->p_as;
8587c478bd9Sstevel@tonic-gate 		}
8597c478bd9Sstevel@tonic-gate 	}
8607c478bd9Sstevel@tonic-gate 
8617c478bd9Sstevel@tonic-gate 	/*
8627c478bd9Sstevel@tonic-gate 	 * Handle the case that the requester is both a leaf
8637c478bd9Sstevel@tonic-gate 	 * and a nexus driver simultaneously by calling the
8647c478bd9Sstevel@tonic-gate 	 * requester's bus_dma_map function directly instead
8657c478bd9Sstevel@tonic-gate 	 * of ddi_dma_map.
8667c478bd9Sstevel@tonic-gate 	 */
8677c478bd9Sstevel@tonic-gate 	bop = DEVI(dip)->devi_ops->devo_bus_ops;
8687c478bd9Sstevel@tonic-gate 	if (bop && bop->bus_dma_map)
8697c478bd9Sstevel@tonic-gate 		funcp = bop->bus_dma_map;
8707c478bd9Sstevel@tonic-gate 
8717c478bd9Sstevel@tonic-gate 	return ((*funcp)(dip, dip, &dmareq, handlep));
8727c478bd9Sstevel@tonic-gate }
8737c478bd9Sstevel@tonic-gate 
8747c478bd9Sstevel@tonic-gate #if !defined(__sparc)
8757c478bd9Sstevel@tonic-gate /*
8767c478bd9Sstevel@tonic-gate  * Request bus_dma_ctl parent to fiddle with a dma request.
8777c478bd9Sstevel@tonic-gate  *
8787c478bd9Sstevel@tonic-gate  * (The sparc version is in sparc_subr.s)
8797c478bd9Sstevel@tonic-gate  */
8807c478bd9Sstevel@tonic-gate int
8817c478bd9Sstevel@tonic-gate ddi_dma_mctl(dev_info_t *dip, dev_info_t *rdip,
8827c478bd9Sstevel@tonic-gate     ddi_dma_handle_t handle, enum ddi_dma_ctlops request,
8837c478bd9Sstevel@tonic-gate     off_t *offp, size_t *lenp, caddr_t *objp, uint_t flags)
8847c478bd9Sstevel@tonic-gate {
8857c478bd9Sstevel@tonic-gate 	int (*fp)();
8867c478bd9Sstevel@tonic-gate 
887b51bbbf5SVikram Hegde 	if (dip != ddi_root_node())
888b51bbbf5SVikram Hegde 		dip = (dev_info_t *)DEVI(dip)->devi_bus_dma_ctl;
8897c478bd9Sstevel@tonic-gate 	fp = DEVI(dip)->devi_ops->devo_bus_ops->bus_dma_ctl;
8907c478bd9Sstevel@tonic-gate 	return ((*fp) (dip, rdip, handle, request, offp, lenp, objp, flags));
8917c478bd9Sstevel@tonic-gate }
8927c478bd9Sstevel@tonic-gate #endif
8937c478bd9Sstevel@tonic-gate 
8947c478bd9Sstevel@tonic-gate /*
8957c478bd9Sstevel@tonic-gate  * For all DMA control functions, call the DMA control
8967c478bd9Sstevel@tonic-gate  * routine and return status.
8977c478bd9Sstevel@tonic-gate  *
8987c478bd9Sstevel@tonic-gate  * Just plain assume that the parent is to be called.
8997c478bd9Sstevel@tonic-gate  * If a nexus driver or a thread outside the framework
9007c478bd9Sstevel@tonic-gate  * of a nexus driver or a leaf driver calls these functions,
9017c478bd9Sstevel@tonic-gate  * it is up to them to deal with the fact that the parent's
9027c478bd9Sstevel@tonic-gate  * bus_dma_ctl function will be the first one called.
9037c478bd9Sstevel@tonic-gate  */
9047c478bd9Sstevel@tonic-gate 
9057c478bd9Sstevel@tonic-gate #define	HD	((ddi_dma_impl_t *)h)->dmai_rdip
9067c478bd9Sstevel@tonic-gate 
9077c478bd9Sstevel@tonic-gate int
9087c478bd9Sstevel@tonic-gate ddi_dma_kvaddrp(ddi_dma_handle_t h, off_t off, size_t len, caddr_t *kp)
9097c478bd9Sstevel@tonic-gate {
9107c478bd9Sstevel@tonic-gate 	return (ddi_dma_mctl(HD, HD, h, DDI_DMA_KVADDR, &off, &len, kp, 0));
9117c478bd9Sstevel@tonic-gate }
9127c478bd9Sstevel@tonic-gate 
9137c478bd9Sstevel@tonic-gate int
9147c478bd9Sstevel@tonic-gate ddi_dma_htoc(ddi_dma_handle_t h, off_t o, ddi_dma_cookie_t *c)
9157c478bd9Sstevel@tonic-gate {
9167c478bd9Sstevel@tonic-gate 	return (ddi_dma_mctl(HD, HD, h, DDI_DMA_HTOC, &o, 0, (caddr_t *)c, 0));
9177c478bd9Sstevel@tonic-gate }
9187c478bd9Sstevel@tonic-gate 
9197c478bd9Sstevel@tonic-gate int
9207c478bd9Sstevel@tonic-gate ddi_dma_coff(ddi_dma_handle_t h, ddi_dma_cookie_t *c, off_t *o)
9217c478bd9Sstevel@tonic-gate {
9227c478bd9Sstevel@tonic-gate 	return (ddi_dma_mctl(HD, HD, h, DDI_DMA_COFF,
9237c478bd9Sstevel@tonic-gate 	    (off_t *)c, 0, (caddr_t *)o, 0));
9247c478bd9Sstevel@tonic-gate }
9257c478bd9Sstevel@tonic-gate 
9267c478bd9Sstevel@tonic-gate int
9277c478bd9Sstevel@tonic-gate ddi_dma_movwin(ddi_dma_handle_t h, off_t *o, size_t *l, ddi_dma_cookie_t *c)
9287c478bd9Sstevel@tonic-gate {
9297c478bd9Sstevel@tonic-gate 	return (ddi_dma_mctl(HD, HD, h, DDI_DMA_MOVWIN, o,
9307c478bd9Sstevel@tonic-gate 	    l, (caddr_t *)c, 0));
9317c478bd9Sstevel@tonic-gate }
9327c478bd9Sstevel@tonic-gate 
9337c478bd9Sstevel@tonic-gate int
9347c478bd9Sstevel@tonic-gate ddi_dma_curwin(ddi_dma_handle_t h, off_t *o, size_t *l)
9357c478bd9Sstevel@tonic-gate {
9367c478bd9Sstevel@tonic-gate 	if ((((ddi_dma_impl_t *)h)->dmai_rflags & DDI_DMA_PARTIAL) == 0)
9377c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
9387c478bd9Sstevel@tonic-gate 	return (ddi_dma_mctl(HD, HD, h, DDI_DMA_REPWIN, o, l, 0, 0));
9397c478bd9Sstevel@tonic-gate }
9407c478bd9Sstevel@tonic-gate 
9417c478bd9Sstevel@tonic-gate int
9427c478bd9Sstevel@tonic-gate ddi_dma_nextwin(ddi_dma_handle_t h, ddi_dma_win_t win,
9437c478bd9Sstevel@tonic-gate     ddi_dma_win_t *nwin)
9447c478bd9Sstevel@tonic-gate {
9457c478bd9Sstevel@tonic-gate 	return (ddi_dma_mctl(HD, HD, h, DDI_DMA_NEXTWIN, (off_t *)&win, 0,
9467c478bd9Sstevel@tonic-gate 	    (caddr_t *)nwin, 0));
9477c478bd9Sstevel@tonic-gate }
9487c478bd9Sstevel@tonic-gate 
9497c478bd9Sstevel@tonic-gate int
9507c478bd9Sstevel@tonic-gate ddi_dma_nextseg(ddi_dma_win_t win, ddi_dma_seg_t seg, ddi_dma_seg_t *nseg)
9517c478bd9Sstevel@tonic-gate {
9527c478bd9Sstevel@tonic-gate 	ddi_dma_handle_t h = (ddi_dma_handle_t)win;
9537c478bd9Sstevel@tonic-gate 
9547c478bd9Sstevel@tonic-gate 	return (ddi_dma_mctl(HD, HD, h, DDI_DMA_NEXTSEG, (off_t *)&win,
9557c478bd9Sstevel@tonic-gate 	    (size_t *)&seg, (caddr_t *)nseg, 0));
9567c478bd9Sstevel@tonic-gate }
9577c478bd9Sstevel@tonic-gate 
9587c478bd9Sstevel@tonic-gate #if (defined(__i386) && !defined(__amd64)) || defined(__sparc)
9597c478bd9Sstevel@tonic-gate /*
9607c478bd9Sstevel@tonic-gate  * This routine is Obsolete and should be removed from ALL architectures
9617c478bd9Sstevel@tonic-gate  * in a future release of Solaris.
9627c478bd9Sstevel@tonic-gate  *
9637c478bd9Sstevel@tonic-gate  * It is deliberately NOT ported to amd64; please fix the code that
9647c478bd9Sstevel@tonic-gate  * depends on this routine to use ddi_dma_nextcookie(9F).
96512f080e7Smrj  *
96612f080e7Smrj  * NOTE: even though we fixed the pointer through a 32-bit param issue (the fix
96712f080e7Smrj  * is a side effect to some other cleanup), we're still not going to support
96812f080e7Smrj  * this interface on x64.
9697c478bd9Sstevel@tonic-gate  */
9707c478bd9Sstevel@tonic-gate int
9717c478bd9Sstevel@tonic-gate ddi_dma_segtocookie(ddi_dma_seg_t seg, off_t *o, off_t *l,
9727c478bd9Sstevel@tonic-gate     ddi_dma_cookie_t *cookiep)
9737c478bd9Sstevel@tonic-gate {
9747c478bd9Sstevel@tonic-gate 	ddi_dma_handle_t h = (ddi_dma_handle_t)seg;
9757c478bd9Sstevel@tonic-gate 
9767c478bd9Sstevel@tonic-gate 	return (ddi_dma_mctl(HD, HD, h, DDI_DMA_SEGTOC, o, (size_t *)l,
9777c478bd9Sstevel@tonic-gate 	    (caddr_t *)cookiep, 0));
9787c478bd9Sstevel@tonic-gate }
9797c478bd9Sstevel@tonic-gate #endif	/* (__i386 && !__amd64) || __sparc */
9807c478bd9Sstevel@tonic-gate 
9817c478bd9Sstevel@tonic-gate #if !defined(__sparc)
9827c478bd9Sstevel@tonic-gate 
9837c478bd9Sstevel@tonic-gate /*
9847c478bd9Sstevel@tonic-gate  * The SPARC versions of these routines are done in assembler to
9857c478bd9Sstevel@tonic-gate  * save register windows, so they're in sparc_subr.s.
9867c478bd9Sstevel@tonic-gate  */
9877c478bd9Sstevel@tonic-gate 
9887c478bd9Sstevel@tonic-gate int
9897c478bd9Sstevel@tonic-gate ddi_dma_map(dev_info_t *dip, dev_info_t *rdip,
9907c478bd9Sstevel@tonic-gate 	struct ddi_dma_req *dmareqp, ddi_dma_handle_t *handlep)
9917c478bd9Sstevel@tonic-gate {
9927c478bd9Sstevel@tonic-gate 	int (*funcp)(dev_info_t *, dev_info_t *, struct ddi_dma_req *,
9937c478bd9Sstevel@tonic-gate 	    ddi_dma_handle_t *);
9947c478bd9Sstevel@tonic-gate 
995b51bbbf5SVikram Hegde 	if (dip != ddi_root_node())
996b51bbbf5SVikram Hegde 		dip = (dev_info_t *)DEVI(dip)->devi_bus_dma_map;
9977c478bd9Sstevel@tonic-gate 
998b51bbbf5SVikram Hegde 	funcp = DEVI(dip)->devi_ops->devo_bus_ops->bus_dma_map;
999b51bbbf5SVikram Hegde 	return ((*funcp)(dip, rdip, dmareqp, handlep));
10007c478bd9Sstevel@tonic-gate }
10017c478bd9Sstevel@tonic-gate 
10027c478bd9Sstevel@tonic-gate int
10037c478bd9Sstevel@tonic-gate ddi_dma_allochdl(dev_info_t *dip, dev_info_t *rdip, ddi_dma_attr_t *attr,
10047c478bd9Sstevel@tonic-gate     int (*waitfp)(caddr_t), caddr_t arg, ddi_dma_handle_t *handlep)
10057c478bd9Sstevel@tonic-gate {
10067c478bd9Sstevel@tonic-gate 	int (*funcp)(dev_info_t *, dev_info_t *, ddi_dma_attr_t *,
10077c478bd9Sstevel@tonic-gate 	    int (*)(caddr_t), caddr_t, ddi_dma_handle_t *);
10087c478bd9Sstevel@tonic-gate 
1009b51bbbf5SVikram Hegde 	if (dip != ddi_root_node())
1010b51bbbf5SVikram Hegde 		dip = (dev_info_t *)DEVI(dip)->devi_bus_dma_allochdl;
10117c478bd9Sstevel@tonic-gate 
1012b51bbbf5SVikram Hegde 	funcp = DEVI(dip)->devi_ops->devo_bus_ops->bus_dma_allochdl;
1013b51bbbf5SVikram Hegde 	return ((*funcp)(dip, rdip, attr, waitfp, arg, handlep));
10147c478bd9Sstevel@tonic-gate }
10157c478bd9Sstevel@tonic-gate 
10167c478bd9Sstevel@tonic-gate int
10177c478bd9Sstevel@tonic-gate ddi_dma_freehdl(dev_info_t *dip, dev_info_t *rdip, ddi_dma_handle_t handlep)
10187c478bd9Sstevel@tonic-gate {
10197c478bd9Sstevel@tonic-gate 	int (*funcp)(dev_info_t *, dev_info_t *, ddi_dma_handle_t);
10207c478bd9Sstevel@tonic-gate 
1021b51bbbf5SVikram Hegde 	if (dip != ddi_root_node())
1022b51bbbf5SVikram Hegde 		dip = (dev_info_t *)DEVI(dip)->devi_bus_dma_allochdl;
10237c478bd9Sstevel@tonic-gate 
1024b51bbbf5SVikram Hegde 	funcp = DEVI(dip)->devi_ops->devo_bus_ops->bus_dma_freehdl;
1025b51bbbf5SVikram Hegde 	return ((*funcp)(dip, rdip, handlep));
10267c478bd9Sstevel@tonic-gate }
10277c478bd9Sstevel@tonic-gate 
10287c478bd9Sstevel@tonic-gate int
10297c478bd9Sstevel@tonic-gate ddi_dma_bindhdl(dev_info_t *dip, dev_info_t *rdip,
10307c478bd9Sstevel@tonic-gate     ddi_dma_handle_t handle, struct ddi_dma_req *dmareq,
10317c478bd9Sstevel@tonic-gate     ddi_dma_cookie_t *cp, uint_t *ccountp)
10327c478bd9Sstevel@tonic-gate {
10337c478bd9Sstevel@tonic-gate 	int (*funcp)(dev_info_t *, dev_info_t *, ddi_dma_handle_t,
10347c478bd9Sstevel@tonic-gate 	    struct ddi_dma_req *, ddi_dma_cookie_t *, uint_t *);
10357c478bd9Sstevel@tonic-gate 
1036b51bbbf5SVikram Hegde 	if (dip != ddi_root_node())
1037b51bbbf5SVikram Hegde 		dip = (dev_info_t *)DEVI(dip)->devi_bus_dma_bindhdl;
10387c478bd9Sstevel@tonic-gate 
1039b51bbbf5SVikram Hegde 	funcp = DEVI(dip)->devi_ops->devo_bus_ops->bus_dma_bindhdl;
1040b51bbbf5SVikram Hegde 	return ((*funcp)(dip, rdip, handle, dmareq, cp, ccountp));
10417c478bd9Sstevel@tonic-gate }
10427c478bd9Sstevel@tonic-gate 
10437c478bd9Sstevel@tonic-gate int
10447c478bd9Sstevel@tonic-gate ddi_dma_unbindhdl(dev_info_t *dip, dev_info_t *rdip,
10457c478bd9Sstevel@tonic-gate     ddi_dma_handle_t handle)
10467c478bd9Sstevel@tonic-gate {
10477c478bd9Sstevel@tonic-gate 	int (*funcp)(dev_info_t *, dev_info_t *, ddi_dma_handle_t);
10487c478bd9Sstevel@tonic-gate 
1049b51bbbf5SVikram Hegde 	if (dip != ddi_root_node())
1050b51bbbf5SVikram Hegde 		dip = (dev_info_t *)DEVI(dip)->devi_bus_dma_unbindhdl;
10517c478bd9Sstevel@tonic-gate 
1052b51bbbf5SVikram Hegde 	funcp = DEVI(dip)->devi_ops->devo_bus_ops->bus_dma_unbindhdl;
1053b51bbbf5SVikram Hegde 	return ((*funcp)(dip, rdip, handle));
10547c478bd9Sstevel@tonic-gate }
10557c478bd9Sstevel@tonic-gate 
10567c478bd9Sstevel@tonic-gate 
10577c478bd9Sstevel@tonic-gate int
10587c478bd9Sstevel@tonic-gate ddi_dma_flush(dev_info_t *dip, dev_info_t *rdip,
10597c478bd9Sstevel@tonic-gate     ddi_dma_handle_t handle, off_t off, size_t len,
10607c478bd9Sstevel@tonic-gate     uint_t cache_flags)
10617c478bd9Sstevel@tonic-gate {
10627c478bd9Sstevel@tonic-gate 	int (*funcp)(dev_info_t *, dev_info_t *, ddi_dma_handle_t,
10637c478bd9Sstevel@tonic-gate 	    off_t, size_t, uint_t);
10647c478bd9Sstevel@tonic-gate 
1065b51bbbf5SVikram Hegde 	if (dip != ddi_root_node())
1066b51bbbf5SVikram Hegde 		dip = (dev_info_t *)DEVI(dip)->devi_bus_dma_flush;
10677c478bd9Sstevel@tonic-gate 
1068b51bbbf5SVikram Hegde 	funcp = DEVI(dip)->devi_ops->devo_bus_ops->bus_dma_flush;
1069b51bbbf5SVikram Hegde 	return ((*funcp)(dip, rdip, handle, off, len, cache_flags));
10707c478bd9Sstevel@tonic-gate }
10717c478bd9Sstevel@tonic-gate 
10727c478bd9Sstevel@tonic-gate int
10737c478bd9Sstevel@tonic-gate ddi_dma_win(dev_info_t *dip, dev_info_t *rdip,
10747c478bd9Sstevel@tonic-gate     ddi_dma_handle_t handle, uint_t win, off_t *offp,
10757c478bd9Sstevel@tonic-gate     size_t *lenp, ddi_dma_cookie_t *cookiep, uint_t *ccountp)
10767c478bd9Sstevel@tonic-gate {
10777c478bd9Sstevel@tonic-gate 	int (*funcp)(dev_info_t *, dev_info_t *, ddi_dma_handle_t,
10787c478bd9Sstevel@tonic-gate 	    uint_t, off_t *, size_t *, ddi_dma_cookie_t *, uint_t *);
10797c478bd9Sstevel@tonic-gate 
1080b51bbbf5SVikram Hegde 	if (dip != ddi_root_node())
1081b51bbbf5SVikram Hegde 		dip = (dev_info_t *)DEVI(dip)->devi_bus_dma_win;
10827c478bd9Sstevel@tonic-gate 
1083b51bbbf5SVikram Hegde 	funcp = DEVI(dip)->devi_ops->devo_bus_ops->bus_dma_win;
1084b51bbbf5SVikram Hegde 	return ((*funcp)(dip, rdip, handle, win, offp, lenp,
10857c478bd9Sstevel@tonic-gate 	    cookiep, ccountp));
10867c478bd9Sstevel@tonic-gate }
10877c478bd9Sstevel@tonic-gate 
10887c478bd9Sstevel@tonic-gate int
10897c478bd9Sstevel@tonic-gate ddi_dma_sync(ddi_dma_handle_t h, off_t o, size_t l, uint_t whom)
10907c478bd9Sstevel@tonic-gate {
10917c478bd9Sstevel@tonic-gate 	ddi_dma_impl_t *hp = (ddi_dma_impl_t *)h;
1092b51bbbf5SVikram Hegde 	dev_info_t *dip, *rdip;
10937c478bd9Sstevel@tonic-gate 	int (*funcp)(dev_info_t *, dev_info_t *, ddi_dma_handle_t, off_t,
1094184cd04cScth 	    size_t, uint_t);
10957c478bd9Sstevel@tonic-gate 
10967c478bd9Sstevel@tonic-gate 	/*
10977c478bd9Sstevel@tonic-gate 	 * the DMA nexus driver will set DMP_NOSYNC if the
10987c478bd9Sstevel@tonic-gate 	 * platform does not require any sync operation. For
10997c478bd9Sstevel@tonic-gate 	 * example if the memory is uncached or consistent
11007c478bd9Sstevel@tonic-gate 	 * and without any I/O write buffers involved.
11017c478bd9Sstevel@tonic-gate 	 */
11027c478bd9Sstevel@tonic-gate 	if ((hp->dmai_rflags & DMP_NOSYNC) == DMP_NOSYNC)
11037c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
11047c478bd9Sstevel@tonic-gate 
1105b51bbbf5SVikram Hegde 	dip = rdip = hp->dmai_rdip;
1106b51bbbf5SVikram Hegde 	if (dip != ddi_root_node())
1107b51bbbf5SVikram Hegde 		dip = (dev_info_t *)DEVI(dip)->devi_bus_dma_flush;
1108b51bbbf5SVikram Hegde 	funcp = DEVI(dip)->devi_ops->devo_bus_ops->bus_dma_flush;
1109b51bbbf5SVikram Hegde 	return ((*funcp)(dip, rdip, h, o, l, whom));
11107c478bd9Sstevel@tonic-gate }
11117c478bd9Sstevel@tonic-gate 
11127c478bd9Sstevel@tonic-gate int
11137c478bd9Sstevel@tonic-gate ddi_dma_unbind_handle(ddi_dma_handle_t h)
11147c478bd9Sstevel@tonic-gate {
11157c478bd9Sstevel@tonic-gate 	ddi_dma_impl_t *hp = (ddi_dma_impl_t *)h;
1116b51bbbf5SVikram Hegde 	dev_info_t *dip, *rdip;
11177c478bd9Sstevel@tonic-gate 	int (*funcp)(dev_info_t *, dev_info_t *, ddi_dma_handle_t);
11187c478bd9Sstevel@tonic-gate 
1119b51bbbf5SVikram Hegde 	dip = rdip = hp->dmai_rdip;
1120b51bbbf5SVikram Hegde 	if (dip != ddi_root_node())
1121b51bbbf5SVikram Hegde 		dip = (dev_info_t *)DEVI(dip)->devi_bus_dma_unbindhdl;
1122b51bbbf5SVikram Hegde 	funcp = DEVI(rdip)->devi_bus_dma_unbindfunc;
1123b51bbbf5SVikram Hegde 	return ((*funcp)(dip, rdip, h));
11247c478bd9Sstevel@tonic-gate }
11257c478bd9Sstevel@tonic-gate 
11267c478bd9Sstevel@tonic-gate #endif	/* !__sparc */
11277c478bd9Sstevel@tonic-gate 
11287c478bd9Sstevel@tonic-gate int
11297c478bd9Sstevel@tonic-gate ddi_dma_free(ddi_dma_handle_t h)
11307c478bd9Sstevel@tonic-gate {
11317c478bd9Sstevel@tonic-gate 	return (ddi_dma_mctl(HD, HD, h, DDI_DMA_FREE, 0, 0, 0, 0));
11327c478bd9Sstevel@tonic-gate }
11337c478bd9Sstevel@tonic-gate 
11347c478bd9Sstevel@tonic-gate int
11357c478bd9Sstevel@tonic-gate ddi_iopb_alloc(dev_info_t *dip, ddi_dma_lim_t *limp, uint_t len, caddr_t *iopbp)
11367c478bd9Sstevel@tonic-gate {
11377c478bd9Sstevel@tonic-gate 	ddi_dma_lim_t defalt;
11387c478bd9Sstevel@tonic-gate 	size_t size = len;
11397c478bd9Sstevel@tonic-gate 
11407c478bd9Sstevel@tonic-gate 	if (!limp) {
11417c478bd9Sstevel@tonic-gate 		defalt = standard_limits;
11427c478bd9Sstevel@tonic-gate 		limp = &defalt;
11437c478bd9Sstevel@tonic-gate 	}
11447c478bd9Sstevel@tonic-gate 	return (i_ddi_mem_alloc_lim(dip, limp, size, 0, 0, 0,
11457c478bd9Sstevel@tonic-gate 	    iopbp, NULL, NULL));
11467c478bd9Sstevel@tonic-gate }
11477c478bd9Sstevel@tonic-gate 
11487c478bd9Sstevel@tonic-gate void
11497c478bd9Sstevel@tonic-gate ddi_iopb_free(caddr_t iopb)
11507c478bd9Sstevel@tonic-gate {
11517b93957cSeota 	i_ddi_mem_free(iopb, NULL);
11527c478bd9Sstevel@tonic-gate }
11537c478bd9Sstevel@tonic-gate 
11547c478bd9Sstevel@tonic-gate int
11557c478bd9Sstevel@tonic-gate ddi_mem_alloc(dev_info_t *dip, ddi_dma_lim_t *limits, uint_t length,
11567c478bd9Sstevel@tonic-gate 	uint_t flags, caddr_t *kaddrp, uint_t *real_length)
11577c478bd9Sstevel@tonic-gate {
11587c478bd9Sstevel@tonic-gate 	ddi_dma_lim_t defalt;
11597c478bd9Sstevel@tonic-gate 	size_t size = length;
11607c478bd9Sstevel@tonic-gate 
11617c478bd9Sstevel@tonic-gate 	if (!limits) {
11627c478bd9Sstevel@tonic-gate 		defalt = standard_limits;
11637c478bd9Sstevel@tonic-gate 		limits = &defalt;
11647c478bd9Sstevel@tonic-gate 	}
11657c478bd9Sstevel@tonic-gate 	return (i_ddi_mem_alloc_lim(dip, limits, size, flags & 0x1,
11667c478bd9Sstevel@tonic-gate 	    1, 0, kaddrp, real_length, NULL));
11677c478bd9Sstevel@tonic-gate }
11687c478bd9Sstevel@tonic-gate 
11697c478bd9Sstevel@tonic-gate void
11707c478bd9Sstevel@tonic-gate ddi_mem_free(caddr_t kaddr)
11717c478bd9Sstevel@tonic-gate {
11727b93957cSeota 	i_ddi_mem_free(kaddr, NULL);
11737c478bd9Sstevel@tonic-gate }
11747c478bd9Sstevel@tonic-gate 
11757c478bd9Sstevel@tonic-gate /*
11767c478bd9Sstevel@tonic-gate  * DMA attributes, alignment, burst sizes, and transfer minimums
11777c478bd9Sstevel@tonic-gate  */
11787c478bd9Sstevel@tonic-gate int
11797c478bd9Sstevel@tonic-gate ddi_dma_get_attr(ddi_dma_handle_t handle, ddi_dma_attr_t *attrp)
11807c478bd9Sstevel@tonic-gate {
11817c478bd9Sstevel@tonic-gate 	ddi_dma_impl_t *dimp = (ddi_dma_impl_t *)handle;
11827c478bd9Sstevel@tonic-gate 
11837c478bd9Sstevel@tonic-gate 	if (attrp == NULL)
11847c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
11857c478bd9Sstevel@tonic-gate 	*attrp = dimp->dmai_attr;
11867c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
11877c478bd9Sstevel@tonic-gate }
11887c478bd9Sstevel@tonic-gate 
11897c478bd9Sstevel@tonic-gate int
11907c478bd9Sstevel@tonic-gate ddi_dma_burstsizes(ddi_dma_handle_t handle)
11917c478bd9Sstevel@tonic-gate {
11927c478bd9Sstevel@tonic-gate 	ddi_dma_impl_t *dimp = (ddi_dma_impl_t *)handle;
11937c478bd9Sstevel@tonic-gate 
11947c478bd9Sstevel@tonic-gate 	if (!dimp)
11957c478bd9Sstevel@tonic-gate 		return (0);
11967c478bd9Sstevel@tonic-gate 	else
11977c478bd9Sstevel@tonic-gate 		return (dimp->dmai_burstsizes);
11987c478bd9Sstevel@tonic-gate }
11997c478bd9Sstevel@tonic-gate 
12007c478bd9Sstevel@tonic-gate int
12017c478bd9Sstevel@tonic-gate ddi_dma_devalign(ddi_dma_handle_t handle, uint_t *alignment, uint_t *mineffect)
12027c478bd9Sstevel@tonic-gate {
12037c478bd9Sstevel@tonic-gate 	ddi_dma_impl_t *dimp = (ddi_dma_impl_t *)handle;
12047c478bd9Sstevel@tonic-gate 
12057c478bd9Sstevel@tonic-gate 	if (!dimp || !alignment || !mineffect)
12067c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
12077c478bd9Sstevel@tonic-gate 	if (!(dimp->dmai_rflags & DDI_DMA_SBUS_64BIT)) {
12087c478bd9Sstevel@tonic-gate 		*alignment = 1 << ddi_ffs(dimp->dmai_burstsizes);
12097c478bd9Sstevel@tonic-gate 	} else {
12107c478bd9Sstevel@tonic-gate 		if (dimp->dmai_burstsizes & 0xff0000) {
12117c478bd9Sstevel@tonic-gate 			*alignment = 1 << ddi_ffs(dimp->dmai_burstsizes >> 16);
12127c478bd9Sstevel@tonic-gate 		} else {
12137c478bd9Sstevel@tonic-gate 			*alignment = 1 << ddi_ffs(dimp->dmai_burstsizes);
12147c478bd9Sstevel@tonic-gate 		}
12157c478bd9Sstevel@tonic-gate 	}
12167c478bd9Sstevel@tonic-gate 	*mineffect = dimp->dmai_minxfer;
12177c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
12187c478bd9Sstevel@tonic-gate }
12197c478bd9Sstevel@tonic-gate 
12207c478bd9Sstevel@tonic-gate int
12217c478bd9Sstevel@tonic-gate ddi_iomin(dev_info_t *a, int i, int stream)
12227c478bd9Sstevel@tonic-gate {
12237c478bd9Sstevel@tonic-gate 	int r;
12247c478bd9Sstevel@tonic-gate 
12257c478bd9Sstevel@tonic-gate 	/*
12267c478bd9Sstevel@tonic-gate 	 * Make sure that the initial value is sane
12277c478bd9Sstevel@tonic-gate 	 */
12287c478bd9Sstevel@tonic-gate 	if (i & (i - 1))
12297c478bd9Sstevel@tonic-gate 		return (0);
12307c478bd9Sstevel@tonic-gate 	if (i == 0)
12317c478bd9Sstevel@tonic-gate 		i = (stream) ? 4 : 1;
12327c478bd9Sstevel@tonic-gate 
12337c478bd9Sstevel@tonic-gate 	r = ddi_ctlops(a, a,
12347c478bd9Sstevel@tonic-gate 	    DDI_CTLOPS_IOMIN, (void *)(uintptr_t)stream, (void *)&i);
12357c478bd9Sstevel@tonic-gate 	if (r != DDI_SUCCESS || (i & (i - 1)))
12367c478bd9Sstevel@tonic-gate 		return (0);
12377c478bd9Sstevel@tonic-gate 	return (i);
12387c478bd9Sstevel@tonic-gate }
12397c478bd9Sstevel@tonic-gate 
12407c478bd9Sstevel@tonic-gate /*
12417c478bd9Sstevel@tonic-gate  * Given two DMA attribute structures, apply the attributes
12427c478bd9Sstevel@tonic-gate  * of one to the other, following the rules of attributes
12437c478bd9Sstevel@tonic-gate  * and the wishes of the caller.
12447c478bd9Sstevel@tonic-gate  *
12457c478bd9Sstevel@tonic-gate  * The rules of DMA attribute structures are that you cannot
12467c478bd9Sstevel@tonic-gate  * make things *less* restrictive as you apply one set
12477c478bd9Sstevel@tonic-gate  * of attributes to another.
12487c478bd9Sstevel@tonic-gate  *
12497c478bd9Sstevel@tonic-gate  */
12507c478bd9Sstevel@tonic-gate void
12517c478bd9Sstevel@tonic-gate ddi_dma_attr_merge(ddi_dma_attr_t *attr, ddi_dma_attr_t *mod)
12527c478bd9Sstevel@tonic-gate {
12537c478bd9Sstevel@tonic-gate 	attr->dma_attr_addr_lo =
12547c478bd9Sstevel@tonic-gate 	    MAX(attr->dma_attr_addr_lo, mod->dma_attr_addr_lo);
12557c478bd9Sstevel@tonic-gate 	attr->dma_attr_addr_hi =
12567c478bd9Sstevel@tonic-gate 	    MIN(attr->dma_attr_addr_hi, mod->dma_attr_addr_hi);
12577c478bd9Sstevel@tonic-gate 	attr->dma_attr_count_max =
12587c478bd9Sstevel@tonic-gate 	    MIN(attr->dma_attr_count_max, mod->dma_attr_count_max);
12597c478bd9Sstevel@tonic-gate 	attr->dma_attr_align =
12607c478bd9Sstevel@tonic-gate 	    MAX(attr->dma_attr_align,  mod->dma_attr_align);
12617c478bd9Sstevel@tonic-gate 	attr->dma_attr_burstsizes =
12627c478bd9Sstevel@tonic-gate 	    (uint_t)(attr->dma_attr_burstsizes & mod->dma_attr_burstsizes);
12637c478bd9Sstevel@tonic-gate 	attr->dma_attr_minxfer =
12647c478bd9Sstevel@tonic-gate 	    maxbit(attr->dma_attr_minxfer, mod->dma_attr_minxfer);
12657c478bd9Sstevel@tonic-gate 	attr->dma_attr_maxxfer =
12667c478bd9Sstevel@tonic-gate 	    MIN(attr->dma_attr_maxxfer, mod->dma_attr_maxxfer);
12677c478bd9Sstevel@tonic-gate 	attr->dma_attr_seg = MIN(attr->dma_attr_seg, mod->dma_attr_seg);
12687c478bd9Sstevel@tonic-gate 	attr->dma_attr_sgllen = MIN((uint_t)attr->dma_attr_sgllen,
12697c478bd9Sstevel@tonic-gate 	    (uint_t)mod->dma_attr_sgllen);
12707c478bd9Sstevel@tonic-gate 	attr->dma_attr_granular =
12717c478bd9Sstevel@tonic-gate 	    MAX(attr->dma_attr_granular, mod->dma_attr_granular);
12727c478bd9Sstevel@tonic-gate }
12737c478bd9Sstevel@tonic-gate 
12747c478bd9Sstevel@tonic-gate /*
12757c478bd9Sstevel@tonic-gate  * mmap/segmap interface:
12767c478bd9Sstevel@tonic-gate  */
12777c478bd9Sstevel@tonic-gate 
12787c478bd9Sstevel@tonic-gate /*
12797c478bd9Sstevel@tonic-gate  * ddi_segmap:		setup the default segment driver. Calls the drivers
12807c478bd9Sstevel@tonic-gate  *			XXmmap routine to validate the range to be mapped.
12817c478bd9Sstevel@tonic-gate  *			Return ENXIO of the range is not valid.  Create
12827c478bd9Sstevel@tonic-gate  *			a seg_dev segment that contains all of the
12837c478bd9Sstevel@tonic-gate  *			necessary information and will reference the
12847c478bd9Sstevel@tonic-gate  *			default segment driver routines. It returns zero
12857c478bd9Sstevel@tonic-gate  *			on success or non-zero on failure.
12867c478bd9Sstevel@tonic-gate  */
12877c478bd9Sstevel@tonic-gate int
12887c478bd9Sstevel@tonic-gate ddi_segmap(dev_t dev, off_t offset, struct as *asp, caddr_t *addrp, off_t len,
12897c478bd9Sstevel@tonic-gate     uint_t prot, uint_t maxprot, uint_t flags, cred_t *credp)
12907c478bd9Sstevel@tonic-gate {
12917c478bd9Sstevel@tonic-gate 	extern int spec_segmap(dev_t, off_t, struct as *, caddr_t *,
12927c478bd9Sstevel@tonic-gate 	    off_t, uint_t, uint_t, uint_t, struct cred *);
12937c478bd9Sstevel@tonic-gate 
12947c478bd9Sstevel@tonic-gate 	return (spec_segmap(dev, offset, asp, addrp, len,
12957c478bd9Sstevel@tonic-gate 	    prot, maxprot, flags, credp));
12967c478bd9Sstevel@tonic-gate }
12977c478bd9Sstevel@tonic-gate 
12987c478bd9Sstevel@tonic-gate /*
12997c478bd9Sstevel@tonic-gate  * ddi_map_fault:	Resolve mappings at fault time.  Used by segment
13007c478bd9Sstevel@tonic-gate  *			drivers. Allows each successive parent to resolve
13017c478bd9Sstevel@tonic-gate  *			address translations and add its mappings to the
13027c478bd9Sstevel@tonic-gate  *			mapping list supplied in the page structure. It
13037c478bd9Sstevel@tonic-gate  *			returns zero on success	or non-zero on failure.
13047c478bd9Sstevel@tonic-gate  */
13057c478bd9Sstevel@tonic-gate 
13067c478bd9Sstevel@tonic-gate int
13077c478bd9Sstevel@tonic-gate ddi_map_fault(dev_info_t *dip, struct hat *hat, struct seg *seg,
13087c478bd9Sstevel@tonic-gate     caddr_t addr, struct devpage *dp, pfn_t pfn, uint_t prot, uint_t lock)
13097c478bd9Sstevel@tonic-gate {
13107c478bd9Sstevel@tonic-gate 	return (i_ddi_map_fault(dip, dip, hat, seg, addr, dp, pfn, prot, lock));
13117c478bd9Sstevel@tonic-gate }
13127c478bd9Sstevel@tonic-gate 
13137c478bd9Sstevel@tonic-gate /*
13147c478bd9Sstevel@tonic-gate  * ddi_device_mapping_check:	Called from ddi_segmap_setup.
13157c478bd9Sstevel@tonic-gate  *	Invokes platform specific DDI to determine whether attributes specified
13167c478bd9Sstevel@tonic-gate  *	in attr(9s) are	valid for the region of memory that will be made
13177c478bd9Sstevel@tonic-gate  *	available for direct access to user process via the mmap(2) system call.
13187c478bd9Sstevel@tonic-gate  */
13197c478bd9Sstevel@tonic-gate int
13207c478bd9Sstevel@tonic-gate ddi_device_mapping_check(dev_t dev, ddi_device_acc_attr_t *accattrp,
13217c478bd9Sstevel@tonic-gate     uint_t rnumber, uint_t *hat_flags)
13227c478bd9Sstevel@tonic-gate {
13237c478bd9Sstevel@tonic-gate 	ddi_acc_handle_t handle;
13247c478bd9Sstevel@tonic-gate 	ddi_map_req_t mr;
13257c478bd9Sstevel@tonic-gate 	ddi_acc_hdl_t *hp;
13267c478bd9Sstevel@tonic-gate 	int result;
13277c478bd9Sstevel@tonic-gate 	dev_info_t *dip;
13287c478bd9Sstevel@tonic-gate 
13297c478bd9Sstevel@tonic-gate 	/*
13307c478bd9Sstevel@tonic-gate 	 * we use e_ddi_hold_devi_by_dev to search for the devi.  We
13317c478bd9Sstevel@tonic-gate 	 * release it immediately since it should already be held by
13327c478bd9Sstevel@tonic-gate 	 * a devfs vnode.
13337c478bd9Sstevel@tonic-gate 	 */
13347c478bd9Sstevel@tonic-gate 	if ((dip =
13357c478bd9Sstevel@tonic-gate 	    e_ddi_hold_devi_by_dev(dev, E_DDI_HOLD_DEVI_NOATTACH)) == NULL)
13367c478bd9Sstevel@tonic-gate 		return (-1);
13377c478bd9Sstevel@tonic-gate 	ddi_release_devi(dip);		/* for e_ddi_hold_devi_by_dev() */
13387c478bd9Sstevel@tonic-gate 
13397c478bd9Sstevel@tonic-gate 	/*
13407c478bd9Sstevel@tonic-gate 	 * Allocate and initialize the common elements of data
13417c478bd9Sstevel@tonic-gate 	 * access handle.
13427c478bd9Sstevel@tonic-gate 	 */
13437c478bd9Sstevel@tonic-gate 	handle = impl_acc_hdl_alloc(KM_SLEEP, NULL);
13447c478bd9Sstevel@tonic-gate 	if (handle == NULL)
13457c478bd9Sstevel@tonic-gate 		return (-1);
13467c478bd9Sstevel@tonic-gate 
13477c478bd9Sstevel@tonic-gate 	hp = impl_acc_hdl_get(handle);
13487c478bd9Sstevel@tonic-gate 	hp->ah_vers = VERS_ACCHDL;
13497c478bd9Sstevel@tonic-gate 	hp->ah_dip = dip;
13507c478bd9Sstevel@tonic-gate 	hp->ah_rnumber = rnumber;
13517c478bd9Sstevel@tonic-gate 	hp->ah_offset = 0;
13527c478bd9Sstevel@tonic-gate 	hp->ah_len = 0;
13537c478bd9Sstevel@tonic-gate 	hp->ah_acc = *accattrp;
13547c478bd9Sstevel@tonic-gate 
13557c478bd9Sstevel@tonic-gate 	/*
13567c478bd9Sstevel@tonic-gate 	 * Set up the mapping request and call to parent.
13577c478bd9Sstevel@tonic-gate 	 */
13587c478bd9Sstevel@tonic-gate 	mr.map_op = DDI_MO_MAP_HANDLE;
13597c478bd9Sstevel@tonic-gate 	mr.map_type = DDI_MT_RNUMBER;
13607c478bd9Sstevel@tonic-gate 	mr.map_obj.rnumber = rnumber;
13617c478bd9Sstevel@tonic-gate 	mr.map_prot = PROT_READ | PROT_WRITE;
13627c478bd9Sstevel@tonic-gate 	mr.map_flags = DDI_MF_KERNEL_MAPPING;
13637c478bd9Sstevel@tonic-gate 	mr.map_handlep = hp;
13647c478bd9Sstevel@tonic-gate 	mr.map_vers = DDI_MAP_VERSION;
13657c478bd9Sstevel@tonic-gate 	result = ddi_map(dip, &mr, 0, 0, NULL);
13667c478bd9Sstevel@tonic-gate 
13677c478bd9Sstevel@tonic-gate 	/*
13687c478bd9Sstevel@tonic-gate 	 * Region must be mappable, pick up flags from the framework.
13697c478bd9Sstevel@tonic-gate 	 */
13707c478bd9Sstevel@tonic-gate 	*hat_flags = hp->ah_hat_flags;
13717c478bd9Sstevel@tonic-gate 
13727c478bd9Sstevel@tonic-gate 	impl_acc_hdl_free(handle);
13737c478bd9Sstevel@tonic-gate 
13747c478bd9Sstevel@tonic-gate 	/*
13757c478bd9Sstevel@tonic-gate 	 * check for end result.
13767c478bd9Sstevel@tonic-gate 	 */
13777c478bd9Sstevel@tonic-gate 	if (result != DDI_SUCCESS)
13787c478bd9Sstevel@tonic-gate 		return (-1);
13797c478bd9Sstevel@tonic-gate 	return (0);
13807c478bd9Sstevel@tonic-gate }
13817c478bd9Sstevel@tonic-gate 
13827c478bd9Sstevel@tonic-gate 
13837c478bd9Sstevel@tonic-gate /*
13847c478bd9Sstevel@tonic-gate  * Property functions:	 See also, ddipropdefs.h.
13857c478bd9Sstevel@tonic-gate  *
13867c478bd9Sstevel@tonic-gate  * These functions are the framework for the property functions,
13877c478bd9Sstevel@tonic-gate  * i.e. they support software defined properties.  All implementation
13887c478bd9Sstevel@tonic-gate  * specific property handling (i.e.: self-identifying devices and
13897c478bd9Sstevel@tonic-gate  * PROM defined properties are handled in the implementation specific
13907c478bd9Sstevel@tonic-gate  * functions (defined in ddi_implfuncs.h).
13917c478bd9Sstevel@tonic-gate  */
13927c478bd9Sstevel@tonic-gate 
13937c478bd9Sstevel@tonic-gate /*
13947c478bd9Sstevel@tonic-gate  * nopropop:	Shouldn't be called, right?
13957c478bd9Sstevel@tonic-gate  */
13967c478bd9Sstevel@tonic-gate int
13977c478bd9Sstevel@tonic-gate nopropop(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, int mod_flags,
13987c478bd9Sstevel@tonic-gate     char *name, caddr_t valuep, int *lengthp)
13997c478bd9Sstevel@tonic-gate {
14007c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(dev, dip, prop_op, mod_flags, name, valuep, lengthp))
14017c478bd9Sstevel@tonic-gate 	return (DDI_PROP_NOT_FOUND);
14027c478bd9Sstevel@tonic-gate }
14037c478bd9Sstevel@tonic-gate 
14047c478bd9Sstevel@tonic-gate #ifdef	DDI_PROP_DEBUG
14057c478bd9Sstevel@tonic-gate int ddi_prop_debug_flag = 0;
14067c478bd9Sstevel@tonic-gate 
14077c478bd9Sstevel@tonic-gate int
14087c478bd9Sstevel@tonic-gate ddi_prop_debug(int enable)
14097c478bd9Sstevel@tonic-gate {
14107c478bd9Sstevel@tonic-gate 	int prev = ddi_prop_debug_flag;
14117c478bd9Sstevel@tonic-gate 
14127c478bd9Sstevel@tonic-gate 	if ((enable != 0) || (prev != 0))
14137c478bd9Sstevel@tonic-gate 		printf("ddi_prop_debug: debugging %s\n",
14147c478bd9Sstevel@tonic-gate 		    enable ? "enabled" : "disabled");
14157c478bd9Sstevel@tonic-gate 	ddi_prop_debug_flag = enable;
14167c478bd9Sstevel@tonic-gate 	return (prev);
14177c478bd9Sstevel@tonic-gate }
14187c478bd9Sstevel@tonic-gate 
14197c478bd9Sstevel@tonic-gate #endif	/* DDI_PROP_DEBUG */
14207c478bd9Sstevel@tonic-gate 
14217c478bd9Sstevel@tonic-gate /*
14227c478bd9Sstevel@tonic-gate  * Search a property list for a match, if found return pointer
14237c478bd9Sstevel@tonic-gate  * to matching prop struct, else return NULL.
14247c478bd9Sstevel@tonic-gate  */
14257c478bd9Sstevel@tonic-gate 
14267c478bd9Sstevel@tonic-gate ddi_prop_t *
14277c478bd9Sstevel@tonic-gate i_ddi_prop_search(dev_t dev, char *name, uint_t flags, ddi_prop_t **list_head)
14287c478bd9Sstevel@tonic-gate {
14297c478bd9Sstevel@tonic-gate 	ddi_prop_t	*propp;
14307c478bd9Sstevel@tonic-gate 
14317c478bd9Sstevel@tonic-gate 	/*
14327c478bd9Sstevel@tonic-gate 	 * find the property in child's devinfo:
14339ac2ddeaScth 	 * Search order defined by this search function is first matching
14349ac2ddeaScth 	 * property with input dev == DDI_DEV_T_ANY matching any dev or
14359ac2ddeaScth 	 * dev == propp->prop_dev, name == propp->name, and the correct
14369ac2ddeaScth 	 * data type as specified in the flags.  If a DDI_DEV_T_NONE dev
14379ac2ddeaScth 	 * value made it this far then it implies a DDI_DEV_T_ANY search.
14387c478bd9Sstevel@tonic-gate 	 */
14399ac2ddeaScth 	if (dev == DDI_DEV_T_NONE)
14409ac2ddeaScth 		dev = DDI_DEV_T_ANY;
14417c478bd9Sstevel@tonic-gate 
14427c478bd9Sstevel@tonic-gate 	for (propp = *list_head; propp != NULL; propp = propp->prop_next)  {
14437c478bd9Sstevel@tonic-gate 
14442cd7878fScth 		if (!DDI_STRSAME(propp->prop_name, name))
14457c478bd9Sstevel@tonic-gate 			continue;
14467c478bd9Sstevel@tonic-gate 
14477c478bd9Sstevel@tonic-gate 		if ((dev != DDI_DEV_T_ANY) && (propp->prop_dev != dev))
14487c478bd9Sstevel@tonic-gate 			continue;
14497c478bd9Sstevel@tonic-gate 
14507c478bd9Sstevel@tonic-gate 		if (((propp->prop_flags & flags) & DDI_PROP_TYPE_MASK) == 0)
14517c478bd9Sstevel@tonic-gate 			continue;
14527c478bd9Sstevel@tonic-gate 
14537c478bd9Sstevel@tonic-gate 		return (propp);
14547c478bd9Sstevel@tonic-gate 	}
14557c478bd9Sstevel@tonic-gate 
14567c478bd9Sstevel@tonic-gate 	return ((ddi_prop_t *)0);
14577c478bd9Sstevel@tonic-gate }
14587c478bd9Sstevel@tonic-gate 
14597c478bd9Sstevel@tonic-gate /*
14607c478bd9Sstevel@tonic-gate  * Search for property within devnames structures
14617c478bd9Sstevel@tonic-gate  */
14627c478bd9Sstevel@tonic-gate ddi_prop_t *
14637c478bd9Sstevel@tonic-gate i_ddi_search_global_prop(dev_t dev, char *name, uint_t flags)
14647c478bd9Sstevel@tonic-gate {
14657c478bd9Sstevel@tonic-gate 	major_t		major;
14667c478bd9Sstevel@tonic-gate 	struct devnames	*dnp;
14677c478bd9Sstevel@tonic-gate 	ddi_prop_t	*propp;
14687c478bd9Sstevel@tonic-gate 
14697c478bd9Sstevel@tonic-gate 	/*
14707c478bd9Sstevel@tonic-gate 	 * Valid dev_t value is needed to index into the
14717c478bd9Sstevel@tonic-gate 	 * correct devnames entry, therefore a dev_t
14727c478bd9Sstevel@tonic-gate 	 * value of DDI_DEV_T_ANY is not appropriate.
14737c478bd9Sstevel@tonic-gate 	 */
14747c478bd9Sstevel@tonic-gate 	ASSERT(dev != DDI_DEV_T_ANY);
14757c478bd9Sstevel@tonic-gate 	if (dev == DDI_DEV_T_ANY) {
14767c478bd9Sstevel@tonic-gate 		return ((ddi_prop_t *)0);
14777c478bd9Sstevel@tonic-gate 	}
14787c478bd9Sstevel@tonic-gate 
14797c478bd9Sstevel@tonic-gate 	major = getmajor(dev);
14807c478bd9Sstevel@tonic-gate 	dnp = &(devnamesp[major]);
14817c478bd9Sstevel@tonic-gate 
14827c478bd9Sstevel@tonic-gate 	if (dnp->dn_global_prop_ptr == NULL)
14837c478bd9Sstevel@tonic-gate 		return ((ddi_prop_t *)0);
14847c478bd9Sstevel@tonic-gate 
14857c478bd9Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
14867c478bd9Sstevel@tonic-gate 
14877c478bd9Sstevel@tonic-gate 	for (propp = dnp->dn_global_prop_ptr->prop_list;
14887c478bd9Sstevel@tonic-gate 	    propp != NULL;
14897c478bd9Sstevel@tonic-gate 	    propp = (ddi_prop_t *)propp->prop_next) {
14907c478bd9Sstevel@tonic-gate 
14912cd7878fScth 		if (!DDI_STRSAME(propp->prop_name, name))
14927c478bd9Sstevel@tonic-gate 			continue;
14937c478bd9Sstevel@tonic-gate 
149465cf7c95SVikram Hegde 		if ((!(flags & DDI_PROP_ROOTNEX_GLOBAL)) &&
149565cf7c95SVikram Hegde 		    (!(flags & LDI_DEV_T_ANY)) && (propp->prop_dev != dev))
14967c478bd9Sstevel@tonic-gate 			continue;
14977c478bd9Sstevel@tonic-gate 
14987c478bd9Sstevel@tonic-gate 		if (((propp->prop_flags & flags) & DDI_PROP_TYPE_MASK) == 0)
14997c478bd9Sstevel@tonic-gate 			continue;
15007c478bd9Sstevel@tonic-gate 
15017c478bd9Sstevel@tonic-gate 		/* Property found, return it */
15027c478bd9Sstevel@tonic-gate 		UNLOCK_DEV_OPS(&dnp->dn_lock);
15037c478bd9Sstevel@tonic-gate 		return (propp);
15047c478bd9Sstevel@tonic-gate 	}
15057c478bd9Sstevel@tonic-gate 
15067c478bd9Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
15077c478bd9Sstevel@tonic-gate 	return ((ddi_prop_t *)0);
15087c478bd9Sstevel@tonic-gate }
15097c478bd9Sstevel@tonic-gate 
15107c478bd9Sstevel@tonic-gate static char prop_no_mem_msg[] = "can't allocate memory for ddi property <%s>";
15117c478bd9Sstevel@tonic-gate 
15127c478bd9Sstevel@tonic-gate /*
15137c478bd9Sstevel@tonic-gate  * ddi_prop_search_global:
15147c478bd9Sstevel@tonic-gate  *	Search the global property list within devnames
15157c478bd9Sstevel@tonic-gate  *	for the named property.  Return the encoded value.
15167c478bd9Sstevel@tonic-gate  */
15177c478bd9Sstevel@tonic-gate static int
15187c478bd9Sstevel@tonic-gate i_ddi_prop_search_global(dev_t dev, uint_t flags, char *name,
15197c478bd9Sstevel@tonic-gate     void *valuep, uint_t *lengthp)
15207c478bd9Sstevel@tonic-gate {
15217c478bd9Sstevel@tonic-gate 	ddi_prop_t	*propp;
15227c478bd9Sstevel@tonic-gate 	caddr_t		buffer;
15237c478bd9Sstevel@tonic-gate 
15247c478bd9Sstevel@tonic-gate 	propp =  i_ddi_search_global_prop(dev, name, flags);
15257c478bd9Sstevel@tonic-gate 
15267c478bd9Sstevel@tonic-gate 	/* Property NOT found, bail */
15277c478bd9Sstevel@tonic-gate 	if (propp == (ddi_prop_t *)0)
15287c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
15297c478bd9Sstevel@tonic-gate 
15307c478bd9Sstevel@tonic-gate 	if (propp->prop_flags & DDI_PROP_UNDEF_IT)
15317c478bd9Sstevel@tonic-gate 		return (DDI_PROP_UNDEFINED);
15327c478bd9Sstevel@tonic-gate 
15333b756dd3Scth 	if ((buffer = kmem_alloc(propp->prop_len,
15343b756dd3Scth 	    (flags & DDI_PROP_CANSLEEP) ? KM_SLEEP : KM_NOSLEEP)) == NULL) {
15357c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, prop_no_mem_msg, name);
15367c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NO_MEMORY);
15377c478bd9Sstevel@tonic-gate 	}
15387c478bd9Sstevel@tonic-gate 
15397c478bd9Sstevel@tonic-gate 	/*
15407c478bd9Sstevel@tonic-gate 	 * Return the encoded data
15417c478bd9Sstevel@tonic-gate 	 */
15427c478bd9Sstevel@tonic-gate 	*(caddr_t *)valuep = buffer;
15437c478bd9Sstevel@tonic-gate 	*lengthp = propp->prop_len;
15447c478bd9Sstevel@tonic-gate 	bcopy(propp->prop_val, buffer, propp->prop_len);
15457c478bd9Sstevel@tonic-gate 
15467c478bd9Sstevel@tonic-gate 	return (DDI_PROP_SUCCESS);
15477c478bd9Sstevel@tonic-gate }
15487c478bd9Sstevel@tonic-gate 
15497c478bd9Sstevel@tonic-gate /*
15507c478bd9Sstevel@tonic-gate  * ddi_prop_search_common:	Lookup and return the encoded value
15517c478bd9Sstevel@tonic-gate  */
15527c478bd9Sstevel@tonic-gate int
15537c478bd9Sstevel@tonic-gate ddi_prop_search_common(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op,
15547c478bd9Sstevel@tonic-gate     uint_t flags, char *name, void *valuep, uint_t *lengthp)
15557c478bd9Sstevel@tonic-gate {
15567c478bd9Sstevel@tonic-gate 	ddi_prop_t	*propp;
15577c478bd9Sstevel@tonic-gate 	int		i;
15587c478bd9Sstevel@tonic-gate 	caddr_t		buffer;
15597c478bd9Sstevel@tonic-gate 	caddr_t		prealloc = NULL;
15607c478bd9Sstevel@tonic-gate 	int		plength = 0;
15617c478bd9Sstevel@tonic-gate 	dev_info_t	*pdip;
15627c478bd9Sstevel@tonic-gate 	int		(*bop)();
15637c478bd9Sstevel@tonic-gate 
15647c478bd9Sstevel@tonic-gate 	/*CONSTANTCONDITION*/
15657c478bd9Sstevel@tonic-gate 	while (1)  {
15667c478bd9Sstevel@tonic-gate 
15677c478bd9Sstevel@tonic-gate 		mutex_enter(&(DEVI(dip)->devi_lock));
15687c478bd9Sstevel@tonic-gate 
15697c478bd9Sstevel@tonic-gate 
15707c478bd9Sstevel@tonic-gate 		/*
15717c478bd9Sstevel@tonic-gate 		 * find the property in child's devinfo:
15727c478bd9Sstevel@tonic-gate 		 * Search order is:
15737c478bd9Sstevel@tonic-gate 		 *	1. driver defined properties
15747c478bd9Sstevel@tonic-gate 		 *	2. system defined properties
15757c478bd9Sstevel@tonic-gate 		 *	3. driver global properties
15767c478bd9Sstevel@tonic-gate 		 *	4. boot defined properties
15777c478bd9Sstevel@tonic-gate 		 */
15787c478bd9Sstevel@tonic-gate 
15797c478bd9Sstevel@tonic-gate 		propp = i_ddi_prop_search(dev, name, flags,
15807c478bd9Sstevel@tonic-gate 		    &(DEVI(dip)->devi_drv_prop_ptr));
15817c478bd9Sstevel@tonic-gate 		if (propp == NULL)  {
15827c478bd9Sstevel@tonic-gate 			propp = i_ddi_prop_search(dev, name, flags,
15837c478bd9Sstevel@tonic-gate 			    &(DEVI(dip)->devi_sys_prop_ptr));
15847c478bd9Sstevel@tonic-gate 		}
15857c478bd9Sstevel@tonic-gate 		if ((propp == NULL) && DEVI(dip)->devi_global_prop_list) {
15867c478bd9Sstevel@tonic-gate 			propp = i_ddi_prop_search(dev, name, flags,
15877c478bd9Sstevel@tonic-gate 			    &DEVI(dip)->devi_global_prop_list->prop_list);
15887c478bd9Sstevel@tonic-gate 		}
15897c478bd9Sstevel@tonic-gate 
15907c478bd9Sstevel@tonic-gate 		if (propp == NULL)  {
15917c478bd9Sstevel@tonic-gate 			propp = i_ddi_prop_search(dev, name, flags,
15927c478bd9Sstevel@tonic-gate 			    &(DEVI(dip)->devi_hw_prop_ptr));
15937c478bd9Sstevel@tonic-gate 		}
15947c478bd9Sstevel@tonic-gate 
15957c478bd9Sstevel@tonic-gate 		/*
15967c478bd9Sstevel@tonic-gate 		 * Software property found?
15977c478bd9Sstevel@tonic-gate 		 */
15987c478bd9Sstevel@tonic-gate 		if (propp != (ddi_prop_t *)0)	{
15997c478bd9Sstevel@tonic-gate 
16007c478bd9Sstevel@tonic-gate 			/*
16017c478bd9Sstevel@tonic-gate 			 * If explicit undefine, return now.
16027c478bd9Sstevel@tonic-gate 			 */
16037c478bd9Sstevel@tonic-gate 			if (propp->prop_flags & DDI_PROP_UNDEF_IT) {
16047c478bd9Sstevel@tonic-gate 				mutex_exit(&(DEVI(dip)->devi_lock));
16057c478bd9Sstevel@tonic-gate 				if (prealloc)
16067c478bd9Sstevel@tonic-gate 					kmem_free(prealloc, plength);
16077c478bd9Sstevel@tonic-gate 				return (DDI_PROP_UNDEFINED);
16087c478bd9Sstevel@tonic-gate 			}
16097c478bd9Sstevel@tonic-gate 
16107c478bd9Sstevel@tonic-gate 			/*
16117c478bd9Sstevel@tonic-gate 			 * If we only want to know if it exists, return now
16127c478bd9Sstevel@tonic-gate 			 */
16137c478bd9Sstevel@tonic-gate 			if (prop_op == PROP_EXISTS) {
16147c478bd9Sstevel@tonic-gate 				mutex_exit(&(DEVI(dip)->devi_lock));
16157c478bd9Sstevel@tonic-gate 				ASSERT(prealloc == NULL);
16167c478bd9Sstevel@tonic-gate 				return (DDI_PROP_SUCCESS);
16177c478bd9Sstevel@tonic-gate 			}
16187c478bd9Sstevel@tonic-gate 
16197c478bd9Sstevel@tonic-gate 			/*
16207c478bd9Sstevel@tonic-gate 			 * If length only request or prop length == 0,
16217c478bd9Sstevel@tonic-gate 			 * service request and return now.
16227c478bd9Sstevel@tonic-gate 			 */
16237c478bd9Sstevel@tonic-gate 			if ((prop_op == PROP_LEN) ||(propp->prop_len == 0)) {
16247c478bd9Sstevel@tonic-gate 				*lengthp = propp->prop_len;
16257c478bd9Sstevel@tonic-gate 
16267c478bd9Sstevel@tonic-gate 				/*
16277c478bd9Sstevel@tonic-gate 				 * if prop_op is PROP_LEN_AND_VAL_ALLOC
16287c478bd9Sstevel@tonic-gate 				 * that means prop_len is 0, so set valuep
16297c478bd9Sstevel@tonic-gate 				 * also to NULL
16307c478bd9Sstevel@tonic-gate 				 */
16317c478bd9Sstevel@tonic-gate 				if (prop_op == PROP_LEN_AND_VAL_ALLOC)
16327c478bd9Sstevel@tonic-gate 					*(caddr_t *)valuep = NULL;
16337c478bd9Sstevel@tonic-gate 
16347c478bd9Sstevel@tonic-gate 				mutex_exit(&(DEVI(dip)->devi_lock));
16357c478bd9Sstevel@tonic-gate 				if (prealloc)
16367c478bd9Sstevel@tonic-gate 					kmem_free(prealloc, plength);
16377c478bd9Sstevel@tonic-gate 				return (DDI_PROP_SUCCESS);
16387c478bd9Sstevel@tonic-gate 			}
16397c478bd9Sstevel@tonic-gate 
16407c478bd9Sstevel@tonic-gate 			/*
16417c478bd9Sstevel@tonic-gate 			 * If LEN_AND_VAL_ALLOC and the request can sleep,
16427c478bd9Sstevel@tonic-gate 			 * drop the mutex, allocate the buffer, and go
16437c478bd9Sstevel@tonic-gate 			 * through the loop again.  If we already allocated
16447c478bd9Sstevel@tonic-gate 			 * the buffer, and the size of the property changed,
16457c478bd9Sstevel@tonic-gate 			 * keep trying...
16467c478bd9Sstevel@tonic-gate 			 */
16477c478bd9Sstevel@tonic-gate 			if ((prop_op == PROP_LEN_AND_VAL_ALLOC) &&
16487c478bd9Sstevel@tonic-gate 			    (flags & DDI_PROP_CANSLEEP))  {
16497c478bd9Sstevel@tonic-gate 				if (prealloc && (propp->prop_len != plength)) {
16507c478bd9Sstevel@tonic-gate 					kmem_free(prealloc, plength);
16517c478bd9Sstevel@tonic-gate 					prealloc = NULL;
16527c478bd9Sstevel@tonic-gate 				}
16537c478bd9Sstevel@tonic-gate 				if (prealloc == NULL)  {
16547c478bd9Sstevel@tonic-gate 					plength = propp->prop_len;
16557c478bd9Sstevel@tonic-gate 					mutex_exit(&(DEVI(dip)->devi_lock));
16567c478bd9Sstevel@tonic-gate 					prealloc = kmem_alloc(plength,
16577c478bd9Sstevel@tonic-gate 					    KM_SLEEP);
16587c478bd9Sstevel@tonic-gate 					continue;
16597c478bd9Sstevel@tonic-gate 				}
16607c478bd9Sstevel@tonic-gate 			}
16617c478bd9Sstevel@tonic-gate 
16627c478bd9Sstevel@tonic-gate 			/*
16637c478bd9Sstevel@tonic-gate 			 * Allocate buffer, if required.  Either way,
16647c478bd9Sstevel@tonic-gate 			 * set `buffer' variable.
16657c478bd9Sstevel@tonic-gate 			 */
16667c478bd9Sstevel@tonic-gate 			i = *lengthp;			/* Get callers length */
16677c478bd9Sstevel@tonic-gate 			*lengthp = propp->prop_len;	/* Set callers length */
16687c478bd9Sstevel@tonic-gate 
16697c478bd9Sstevel@tonic-gate 			switch (prop_op) {
16707c478bd9Sstevel@tonic-gate 
16717c478bd9Sstevel@tonic-gate 			case PROP_LEN_AND_VAL_ALLOC:
16727c478bd9Sstevel@tonic-gate 
16737c478bd9Sstevel@tonic-gate 				if (prealloc == NULL) {
16747c478bd9Sstevel@tonic-gate 					buffer = kmem_alloc(propp->prop_len,
16757c478bd9Sstevel@tonic-gate 					    KM_NOSLEEP);
16767c478bd9Sstevel@tonic-gate 				} else {
16777c478bd9Sstevel@tonic-gate 					buffer = prealloc;
16787c478bd9Sstevel@tonic-gate 				}
16797c478bd9Sstevel@tonic-gate 
16807c478bd9Sstevel@tonic-gate 				if (buffer == NULL)  {
16817c478bd9Sstevel@tonic-gate 					mutex_exit(&(DEVI(dip)->devi_lock));
16827c478bd9Sstevel@tonic-gate 					cmn_err(CE_CONT, prop_no_mem_msg, name);
16837c478bd9Sstevel@tonic-gate 					return (DDI_PROP_NO_MEMORY);
16847c478bd9Sstevel@tonic-gate 				}
16857c478bd9Sstevel@tonic-gate 				/* Set callers buf ptr */
16867c478bd9Sstevel@tonic-gate 				*(caddr_t *)valuep = buffer;
16877c478bd9Sstevel@tonic-gate 				break;
16887c478bd9Sstevel@tonic-gate 
16897c478bd9Sstevel@tonic-gate 			case PROP_LEN_AND_VAL_BUF:
16907c478bd9Sstevel@tonic-gate 
16917c478bd9Sstevel@tonic-gate 				if (propp->prop_len > (i)) {
16927c478bd9Sstevel@tonic-gate 					mutex_exit(&(DEVI(dip)->devi_lock));
16937c478bd9Sstevel@tonic-gate 					return (DDI_PROP_BUF_TOO_SMALL);
16947c478bd9Sstevel@tonic-gate 				}
16957c478bd9Sstevel@tonic-gate 
16967c478bd9Sstevel@tonic-gate 				buffer = valuep;  /* Get callers buf ptr */
16977c478bd9Sstevel@tonic-gate 				break;
16987c478bd9Sstevel@tonic-gate 
16997c478bd9Sstevel@tonic-gate 			default:
17007c478bd9Sstevel@tonic-gate 				break;
17017c478bd9Sstevel@tonic-gate 			}
17027c478bd9Sstevel@tonic-gate 
17037c478bd9Sstevel@tonic-gate 			/*
17047c478bd9Sstevel@tonic-gate 			 * Do the copy.
17057c478bd9Sstevel@tonic-gate 			 */
17067c478bd9Sstevel@tonic-gate 			bcopy(propp->prop_val, buffer, propp->prop_len);
17077c478bd9Sstevel@tonic-gate 			mutex_exit(&(DEVI(dip)->devi_lock));
17087c478bd9Sstevel@tonic-gate 			return (DDI_PROP_SUCCESS);
17097c478bd9Sstevel@tonic-gate 		}
17107c478bd9Sstevel@tonic-gate 
17117c478bd9Sstevel@tonic-gate 		mutex_exit(&(DEVI(dip)->devi_lock));
17127c478bd9Sstevel@tonic-gate 		if (prealloc)
17137c478bd9Sstevel@tonic-gate 			kmem_free(prealloc, plength);
17147c478bd9Sstevel@tonic-gate 		prealloc = NULL;
17157c478bd9Sstevel@tonic-gate 
17167c478bd9Sstevel@tonic-gate 		/*
17177c478bd9Sstevel@tonic-gate 		 * Prop not found, call parent bus_ops to deal with possible
17187c478bd9Sstevel@tonic-gate 		 * h/w layer (possible PROM defined props, etc.) and to
17197c478bd9Sstevel@tonic-gate 		 * possibly ascend the hierarchy, if allowed by flags.
17207c478bd9Sstevel@tonic-gate 		 */
17217c478bd9Sstevel@tonic-gate 		pdip = (dev_info_t *)DEVI(dip)->devi_parent;
17227c478bd9Sstevel@tonic-gate 
17237c478bd9Sstevel@tonic-gate 		/*
17247c478bd9Sstevel@tonic-gate 		 * One last call for the root driver PROM props?
17257c478bd9Sstevel@tonic-gate 		 */
17267c478bd9Sstevel@tonic-gate 		if (dip == ddi_root_node())  {
17277c478bd9Sstevel@tonic-gate 			return (ddi_bus_prop_op(dev, dip, dip, prop_op,
17287c478bd9Sstevel@tonic-gate 			    flags, name, valuep, (int *)lengthp));
17297c478bd9Sstevel@tonic-gate 		}
17307c478bd9Sstevel@tonic-gate 
17317c478bd9Sstevel@tonic-gate 		/*
17327c478bd9Sstevel@tonic-gate 		 * We may have been called to check for properties
17337c478bd9Sstevel@tonic-gate 		 * within a single devinfo node that has no parent -
17347c478bd9Sstevel@tonic-gate 		 * see make_prop()
17357c478bd9Sstevel@tonic-gate 		 */
17367c478bd9Sstevel@tonic-gate 		if (pdip == NULL) {
17377c478bd9Sstevel@tonic-gate 			ASSERT((flags &
17387c478bd9Sstevel@tonic-gate 			    (DDI_PROP_DONTPASS | DDI_PROP_NOTPROM)) ==
17397c478bd9Sstevel@tonic-gate 			    (DDI_PROP_DONTPASS | DDI_PROP_NOTPROM));
17407c478bd9Sstevel@tonic-gate 			return (DDI_PROP_NOT_FOUND);
17417c478bd9Sstevel@tonic-gate 		}
17427c478bd9Sstevel@tonic-gate 
17437c478bd9Sstevel@tonic-gate 		/*
17447c478bd9Sstevel@tonic-gate 		 * Instead of recursing, we do iterative calls up the tree.
17457c478bd9Sstevel@tonic-gate 		 * As a bit of optimization, skip the bus_op level if the
17467c478bd9Sstevel@tonic-gate 		 * node is a s/w node and if the parent's bus_prop_op function
17477c478bd9Sstevel@tonic-gate 		 * is `ddi_bus_prop_op', because we know that in this case,
17487c478bd9Sstevel@tonic-gate 		 * this function does nothing.
17497c478bd9Sstevel@tonic-gate 		 *
17507c478bd9Sstevel@tonic-gate 		 * 4225415: If the parent isn't attached, or the child
17517c478bd9Sstevel@tonic-gate 		 * hasn't been named by the parent yet, use the default
17527c478bd9Sstevel@tonic-gate 		 * ddi_bus_prop_op as a proxy for the parent.  This
17537c478bd9Sstevel@tonic-gate 		 * allows property lookups in any child/parent state to
17547c478bd9Sstevel@tonic-gate 		 * include 'prom' and inherited properties, even when
17557c478bd9Sstevel@tonic-gate 		 * there are no drivers attached to the child or parent.
17567c478bd9Sstevel@tonic-gate 		 */
17577c478bd9Sstevel@tonic-gate 
17587c478bd9Sstevel@tonic-gate 		bop = ddi_bus_prop_op;
1759737d277aScth 		if (i_ddi_devi_attached(pdip) &&
17607c478bd9Sstevel@tonic-gate 		    (i_ddi_node_state(dip) >= DS_INITIALIZED))
17617c478bd9Sstevel@tonic-gate 			bop = DEVI(pdip)->devi_ops->devo_bus_ops->bus_prop_op;
17627c478bd9Sstevel@tonic-gate 
17637c478bd9Sstevel@tonic-gate 		i = DDI_PROP_NOT_FOUND;
17647c478bd9Sstevel@tonic-gate 
17657c478bd9Sstevel@tonic-gate 		if ((bop != ddi_bus_prop_op) || ndi_dev_is_prom_node(dip)) {
17667c478bd9Sstevel@tonic-gate 			i = (*bop)(dev, pdip, dip, prop_op,
17677c478bd9Sstevel@tonic-gate 			    flags | DDI_PROP_DONTPASS,
17687c478bd9Sstevel@tonic-gate 			    name, valuep, lengthp);
17697c478bd9Sstevel@tonic-gate 		}
17707c478bd9Sstevel@tonic-gate 
17717c478bd9Sstevel@tonic-gate 		if ((flags & DDI_PROP_DONTPASS) ||
17727c478bd9Sstevel@tonic-gate 		    (i != DDI_PROP_NOT_FOUND))
17737c478bd9Sstevel@tonic-gate 			return (i);
17747c478bd9Sstevel@tonic-gate 
17757c478bd9Sstevel@tonic-gate 		dip = pdip;
17767c478bd9Sstevel@tonic-gate 	}
17777c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
17787c478bd9Sstevel@tonic-gate }
17797c478bd9Sstevel@tonic-gate 
17807c478bd9Sstevel@tonic-gate 
17817c478bd9Sstevel@tonic-gate /*
17827c478bd9Sstevel@tonic-gate  * ddi_prop_op: The basic property operator for drivers.
17837c478bd9Sstevel@tonic-gate  *
17847c478bd9Sstevel@tonic-gate  * In ddi_prop_op, the type of valuep is interpreted based on prop_op:
17857c478bd9Sstevel@tonic-gate  *
17867c478bd9Sstevel@tonic-gate  *	prop_op			valuep
17877c478bd9Sstevel@tonic-gate  *	------			------
17887c478bd9Sstevel@tonic-gate  *
17897c478bd9Sstevel@tonic-gate  *	PROP_LEN		<unused>
17907c478bd9Sstevel@tonic-gate  *
17917c478bd9Sstevel@tonic-gate  *	PROP_LEN_AND_VAL_BUF	Pointer to callers buffer
17927c478bd9Sstevel@tonic-gate  *
17937c478bd9Sstevel@tonic-gate  *	PROP_LEN_AND_VAL_ALLOC	Address of callers pointer (will be set to
17947c478bd9Sstevel@tonic-gate  *				address of allocated buffer, if successful)
17957c478bd9Sstevel@tonic-gate  */
17967c478bd9Sstevel@tonic-gate int
17977c478bd9Sstevel@tonic-gate ddi_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, int mod_flags,
17987c478bd9Sstevel@tonic-gate     char *name, caddr_t valuep, int *lengthp)
17997c478bd9Sstevel@tonic-gate {
18007c478bd9Sstevel@tonic-gate 	int	i;
18017c478bd9Sstevel@tonic-gate 
18027c478bd9Sstevel@tonic-gate 	ASSERT((mod_flags & DDI_PROP_TYPE_MASK) == 0);
18037c478bd9Sstevel@tonic-gate 
18047c478bd9Sstevel@tonic-gate 	/*
18057c478bd9Sstevel@tonic-gate 	 * If this was originally an LDI prop lookup then we bail here.
18067c478bd9Sstevel@tonic-gate 	 * The reason is that the LDI property lookup interfaces first call
18077c478bd9Sstevel@tonic-gate 	 * a drivers prop_op() entry point to allow it to override
18087c478bd9Sstevel@tonic-gate 	 * properties.  But if we've made it here, then the driver hasn't
18097c478bd9Sstevel@tonic-gate 	 * overriden any properties.  We don't want to continue with the
18107c478bd9Sstevel@tonic-gate 	 * property search here because we don't have any type inforamtion.
18117c478bd9Sstevel@tonic-gate 	 * When we return failure, the LDI interfaces will then proceed to
18127c478bd9Sstevel@tonic-gate 	 * call the typed property interfaces to look up the property.
18137c478bd9Sstevel@tonic-gate 	 */
18147c478bd9Sstevel@tonic-gate 	if (mod_flags & DDI_PROP_DYNAMIC)
18157c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
18167c478bd9Sstevel@tonic-gate 
18177c478bd9Sstevel@tonic-gate 	/*
18187c478bd9Sstevel@tonic-gate 	 * check for pre-typed property consumer asking for typed property:
18197c478bd9Sstevel@tonic-gate 	 * see e_ddi_getprop_int64.
18207c478bd9Sstevel@tonic-gate 	 */
18217c478bd9Sstevel@tonic-gate 	if (mod_flags & DDI_PROP_CONSUMER_TYPED)
18227c478bd9Sstevel@tonic-gate 		mod_flags |= DDI_PROP_TYPE_INT64;
18237c478bd9Sstevel@tonic-gate 	mod_flags |= DDI_PROP_TYPE_ANY;
18247c478bd9Sstevel@tonic-gate 
18257c478bd9Sstevel@tonic-gate 	i = ddi_prop_search_common(dev, dip, prop_op,
1826184cd04cScth 	    mod_flags, name, valuep, (uint_t *)lengthp);
18277c478bd9Sstevel@tonic-gate 	if (i == DDI_PROP_FOUND_1275)
18287c478bd9Sstevel@tonic-gate 		return (DDI_PROP_SUCCESS);
18297c478bd9Sstevel@tonic-gate 	return (i);
18307c478bd9Sstevel@tonic-gate }
18317c478bd9Sstevel@tonic-gate 
18327c478bd9Sstevel@tonic-gate /*
1833184cd04cScth  * ddi_prop_op_nblocks_blksize: The basic property operator for drivers that
1834184cd04cScth  * maintain size in number of blksize blocks.  Provides a dynamic property
1835184cd04cScth  * implementation for size oriented properties based on nblocks64 and blksize
1836184cd04cScth  * values passed in by the driver.  Fallback to ddi_prop_op if the nblocks64
1837184cd04cScth  * is too large.  This interface should not be used with a nblocks64 that
1838184cd04cScth  * represents the driver's idea of how to represent unknown, if nblocks is
1839184cd04cScth  * unknown use ddi_prop_op.
18407c478bd9Sstevel@tonic-gate  */
18417c478bd9Sstevel@tonic-gate int
1842184cd04cScth ddi_prop_op_nblocks_blksize(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op,
1843184cd04cScth     int mod_flags, char *name, caddr_t valuep, int *lengthp,
1844184cd04cScth     uint64_t nblocks64, uint_t blksize)
18457c478bd9Sstevel@tonic-gate {
18467c478bd9Sstevel@tonic-gate 	uint64_t size64;
1847184cd04cScth 	int	blkshift;
1848184cd04cScth 
1849184cd04cScth 	/* convert block size to shift value */
1850184cd04cScth 	ASSERT(BIT_ONLYONESET(blksize));
1851184cd04cScth 	blkshift = highbit(blksize) - 1;
18527c478bd9Sstevel@tonic-gate 
18537c478bd9Sstevel@tonic-gate 	/*
18547c478bd9Sstevel@tonic-gate 	 * There is no point in supporting nblocks64 values that don't have
18557c478bd9Sstevel@tonic-gate 	 * an accurate uint64_t byte count representation.
18567c478bd9Sstevel@tonic-gate 	 */
1857184cd04cScth 	if (nblocks64 >= (UINT64_MAX >> blkshift))
18587c478bd9Sstevel@tonic-gate 		return (ddi_prop_op(dev, dip, prop_op, mod_flags,
18597c478bd9Sstevel@tonic-gate 		    name, valuep, lengthp));
18607c478bd9Sstevel@tonic-gate 
1861184cd04cScth 	size64 = nblocks64 << blkshift;
1862184cd04cScth 	return (ddi_prop_op_size_blksize(dev, dip, prop_op, mod_flags,
1863184cd04cScth 	    name, valuep, lengthp, size64, blksize));
18647c478bd9Sstevel@tonic-gate }
18657c478bd9Sstevel@tonic-gate 
18667c478bd9Sstevel@tonic-gate /*
1867184cd04cScth  * ddi_prop_op_nblocks: ddi_prop_op_nblocks_blksize with DEV_BSIZE blksize.
1868184cd04cScth  */
1869184cd04cScth int
1870184cd04cScth ddi_prop_op_nblocks(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op,
1871184cd04cScth     int mod_flags, char *name, caddr_t valuep, int *lengthp, uint64_t nblocks64)
1872184cd04cScth {
1873184cd04cScth 	return (ddi_prop_op_nblocks_blksize(dev, dip, prop_op,
1874184cd04cScth 	    mod_flags, name, valuep, lengthp, nblocks64, DEV_BSIZE));
1875184cd04cScth }
1876184cd04cScth 
1877184cd04cScth /*
1878184cd04cScth  * ddi_prop_op_size_blksize: The basic property operator for block drivers that
1879184cd04cScth  * maintain size in bytes. Provides a of dynamic property implementation for
1880184cd04cScth  * size oriented properties based on size64 value and blksize passed in by the
1881184cd04cScth  * driver.  Fallback to ddi_prop_op if the size64 is too large. This interface
1882184cd04cScth  * should not be used with a size64 that represents the driver's idea of how
1883184cd04cScth  * to represent unknown, if size is unknown use ddi_prop_op.
18847c478bd9Sstevel@tonic-gate  *
18857c478bd9Sstevel@tonic-gate  * NOTE: the legacy "nblocks"/"size" properties are treated as 32-bit unsigned
18867c478bd9Sstevel@tonic-gate  * integers. While the most likely interface to request them ([bc]devi_size)
18877c478bd9Sstevel@tonic-gate  * is declared int (signed) there is no enforcement of this, which means we
18887c478bd9Sstevel@tonic-gate  * can't enforce limitations here without risking regression.
18897c478bd9Sstevel@tonic-gate  */
18907c478bd9Sstevel@tonic-gate int
1891184cd04cScth ddi_prop_op_size_blksize(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op,
1892184cd04cScth     int mod_flags, char *name, caddr_t valuep, int *lengthp, uint64_t size64,
1893184cd04cScth     uint_t blksize)
18947c478bd9Sstevel@tonic-gate {
18957c478bd9Sstevel@tonic-gate 	uint64_t nblocks64;
18967c478bd9Sstevel@tonic-gate 	int	callers_length;
18977c478bd9Sstevel@tonic-gate 	caddr_t	buffer;
1898184cd04cScth 	int	blkshift;
1899184cd04cScth 
1900b9ccdc5aScth 	/*
1901b9ccdc5aScth 	 * This is a kludge to support capture of size(9P) pure dynamic
1902b9ccdc5aScth 	 * properties in snapshots for non-cmlb code (without exposing
1903b9ccdc5aScth 	 * i_ddi_prop_dyn changes). When everyone uses cmlb, this code
1904b9ccdc5aScth 	 * should be removed.
1905b9ccdc5aScth 	 */
1906b9ccdc5aScth 	if (i_ddi_prop_dyn_driver_get(dip) == NULL) {
1907b9ccdc5aScth 		static i_ddi_prop_dyn_t prop_dyn_size[] = {
1908b9ccdc5aScth 		    {"Size",		DDI_PROP_TYPE_INT64,	S_IFCHR},
1909b9ccdc5aScth 		    {"Nblocks",		DDI_PROP_TYPE_INT64,	S_IFBLK},
1910b9ccdc5aScth 		    {NULL}
1911b9ccdc5aScth 		};
1912b9ccdc5aScth 		i_ddi_prop_dyn_driver_set(dip, prop_dyn_size);
1913b9ccdc5aScth 	}
1914b9ccdc5aScth 
1915184cd04cScth 	/* convert block size to shift value */
1916184cd04cScth 	ASSERT(BIT_ONLYONESET(blksize));
1917184cd04cScth 	blkshift = highbit(blksize) - 1;
19187c478bd9Sstevel@tonic-gate 
19197c478bd9Sstevel@tonic-gate 	/* compute DEV_BSIZE nblocks value */
1920184cd04cScth 	nblocks64 = size64 >> blkshift;
19217c478bd9Sstevel@tonic-gate 
19227c478bd9Sstevel@tonic-gate 	/* get callers length, establish length of our dynamic properties */
19237c478bd9Sstevel@tonic-gate 	callers_length = *lengthp;
19247c478bd9Sstevel@tonic-gate 
19257c478bd9Sstevel@tonic-gate 	if (strcmp(name, "Nblocks") == 0)
19267c478bd9Sstevel@tonic-gate 		*lengthp = sizeof (uint64_t);
19277c478bd9Sstevel@tonic-gate 	else if (strcmp(name, "Size") == 0)
19287c478bd9Sstevel@tonic-gate 		*lengthp = sizeof (uint64_t);
19297c478bd9Sstevel@tonic-gate 	else if ((strcmp(name, "nblocks") == 0) && (nblocks64 < UINT_MAX))
19307c478bd9Sstevel@tonic-gate 		*lengthp = sizeof (uint32_t);
19317c478bd9Sstevel@tonic-gate 	else if ((strcmp(name, "size") == 0) && (size64 < UINT_MAX))
19327c478bd9Sstevel@tonic-gate 		*lengthp = sizeof (uint32_t);
1933184cd04cScth 	else if ((strcmp(name, "blksize") == 0) && (blksize < UINT_MAX))
1934184cd04cScth 		*lengthp = sizeof (uint32_t);
19357c478bd9Sstevel@tonic-gate 	else {
19367c478bd9Sstevel@tonic-gate 		/* fallback to ddi_prop_op */
19377c478bd9Sstevel@tonic-gate 		return (ddi_prop_op(dev, dip, prop_op, mod_flags,
19387c478bd9Sstevel@tonic-gate 		    name, valuep, lengthp));
19397c478bd9Sstevel@tonic-gate 	}
19407c478bd9Sstevel@tonic-gate 
19417c478bd9Sstevel@tonic-gate 	/* service request for the length of the property */
19427c478bd9Sstevel@tonic-gate 	if (prop_op == PROP_LEN)
19437c478bd9Sstevel@tonic-gate 		return (DDI_PROP_SUCCESS);
19447c478bd9Sstevel@tonic-gate 
19457c478bd9Sstevel@tonic-gate 	switch (prop_op) {
19467c478bd9Sstevel@tonic-gate 	case PROP_LEN_AND_VAL_ALLOC:
19477c478bd9Sstevel@tonic-gate 		if ((buffer = kmem_alloc(*lengthp,
19487c478bd9Sstevel@tonic-gate 		    (mod_flags & DDI_PROP_CANSLEEP) ?
19497c478bd9Sstevel@tonic-gate 		    KM_SLEEP : KM_NOSLEEP)) == NULL)
19507c478bd9Sstevel@tonic-gate 			return (DDI_PROP_NO_MEMORY);
19517c478bd9Sstevel@tonic-gate 
19527c478bd9Sstevel@tonic-gate 		*(caddr_t *)valuep = buffer;	/* set callers buf ptr */
19537c478bd9Sstevel@tonic-gate 		break;
19547c478bd9Sstevel@tonic-gate 
19557c478bd9Sstevel@tonic-gate 	case PROP_LEN_AND_VAL_BUF:
1956b9ccdc5aScth 		/* the length of the property and the request must match */
1957b9ccdc5aScth 		if (callers_length != *lengthp)
1958b9ccdc5aScth 			return (DDI_PROP_INVAL_ARG);
1959b9ccdc5aScth 
19607c478bd9Sstevel@tonic-gate 		buffer = valuep;		/* get callers buf ptr */
19617c478bd9Sstevel@tonic-gate 		break;
19627c478bd9Sstevel@tonic-gate 
19637c478bd9Sstevel@tonic-gate 	default:
19647c478bd9Sstevel@tonic-gate 		return (DDI_PROP_INVAL_ARG);
19657c478bd9Sstevel@tonic-gate 	}
19667c478bd9Sstevel@tonic-gate 
19677c478bd9Sstevel@tonic-gate 	/* transfer the value into the buffer */
19687c478bd9Sstevel@tonic-gate 	if (strcmp(name, "Nblocks") == 0)
19697c478bd9Sstevel@tonic-gate 		*((uint64_t *)buffer) = nblocks64;
19707c478bd9Sstevel@tonic-gate 	else if (strcmp(name, "Size") == 0)
19717c478bd9Sstevel@tonic-gate 		*((uint64_t *)buffer) = size64;
19727c478bd9Sstevel@tonic-gate 	else if (strcmp(name, "nblocks") == 0)
19737c478bd9Sstevel@tonic-gate 		*((uint32_t *)buffer) = (uint32_t)nblocks64;
19747c478bd9Sstevel@tonic-gate 	else if (strcmp(name, "size") == 0)
19757c478bd9Sstevel@tonic-gate 		*((uint32_t *)buffer) = (uint32_t)size64;
1976184cd04cScth 	else if (strcmp(name, "blksize") == 0)
1977184cd04cScth 		*((uint32_t *)buffer) = (uint32_t)blksize;
19787c478bd9Sstevel@tonic-gate 	return (DDI_PROP_SUCCESS);
19797c478bd9Sstevel@tonic-gate }
19807c478bd9Sstevel@tonic-gate 
1981184cd04cScth /*
1982184cd04cScth  * ddi_prop_op_size: ddi_prop_op_size_blksize with DEV_BSIZE block size.
1983184cd04cScth  */
1984184cd04cScth int
1985184cd04cScth ddi_prop_op_size(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op,
1986184cd04cScth     int mod_flags, char *name, caddr_t valuep, int *lengthp, uint64_t size64)
1987184cd04cScth {
1988184cd04cScth 	return (ddi_prop_op_size_blksize(dev, dip, prop_op,
1989184cd04cScth 	    mod_flags, name, valuep, lengthp, size64, DEV_BSIZE));
1990184cd04cScth }
1991184cd04cScth 
19927c478bd9Sstevel@tonic-gate /*
19937c478bd9Sstevel@tonic-gate  * Variable length props...
19947c478bd9Sstevel@tonic-gate  */
19957c478bd9Sstevel@tonic-gate 
19967c478bd9Sstevel@tonic-gate /*
19977c478bd9Sstevel@tonic-gate  * ddi_getlongprop:	Get variable length property len+val into a buffer
19987c478bd9Sstevel@tonic-gate  *		allocated by property provider via kmem_alloc. Requester
19997c478bd9Sstevel@tonic-gate  *		is responsible for freeing returned property via kmem_free.
20007c478bd9Sstevel@tonic-gate  *
20017c478bd9Sstevel@tonic-gate  *	Arguments:
20027c478bd9Sstevel@tonic-gate  *
20037c478bd9Sstevel@tonic-gate  *	dev_t:	Input:	dev_t of property.
20047c478bd9Sstevel@tonic-gate  *	dip:	Input:	dev_info_t pointer of child.
20057c478bd9Sstevel@tonic-gate  *	flags:	Input:	Possible flag modifiers are:
20067c478bd9Sstevel@tonic-gate  *		DDI_PROP_DONTPASS:	Don't pass to parent if prop not found.
20077c478bd9Sstevel@tonic-gate  *		DDI_PROP_CANSLEEP:	Memory allocation may sleep.
20087c478bd9Sstevel@tonic-gate  *	name:	Input:	name of property.
20097c478bd9Sstevel@tonic-gate  *	valuep:	Output:	Addr of callers buffer pointer.
20107c478bd9Sstevel@tonic-gate  *	lengthp:Output:	*lengthp will contain prop length on exit.
20117c478bd9Sstevel@tonic-gate  *
20127c478bd9Sstevel@tonic-gate  *	Possible Returns:
20137c478bd9Sstevel@tonic-gate  *
20147c478bd9Sstevel@tonic-gate  *		DDI_PROP_SUCCESS:	Prop found and returned.
20157c478bd9Sstevel@tonic-gate  *		DDI_PROP_NOT_FOUND:	Prop not found
20167c478bd9Sstevel@tonic-gate  *		DDI_PROP_UNDEFINED:	Prop explicitly undefined.
20177c478bd9Sstevel@tonic-gate  *		DDI_PROP_NO_MEMORY:	Prop found, but unable to alloc mem.
20187c478bd9Sstevel@tonic-gate  */
20197c478bd9Sstevel@tonic-gate 
20207c478bd9Sstevel@tonic-gate int
20217c478bd9Sstevel@tonic-gate ddi_getlongprop(dev_t dev, dev_info_t *dip, int flags,
20227c478bd9Sstevel@tonic-gate     char *name, caddr_t valuep, int *lengthp)
20237c478bd9Sstevel@tonic-gate {
20247c478bd9Sstevel@tonic-gate 	return (ddi_prop_op(dev, dip, PROP_LEN_AND_VAL_ALLOC,
20257c478bd9Sstevel@tonic-gate 	    flags, name, valuep, lengthp));
20267c478bd9Sstevel@tonic-gate }
20277c478bd9Sstevel@tonic-gate 
20287c478bd9Sstevel@tonic-gate /*
20297c478bd9Sstevel@tonic-gate  *
20307c478bd9Sstevel@tonic-gate  * ddi_getlongprop_buf:		Get long prop into pre-allocated callers
20317c478bd9Sstevel@tonic-gate  *				buffer. (no memory allocation by provider).
20327c478bd9Sstevel@tonic-gate  *
20337c478bd9Sstevel@tonic-gate  *	dev_t:	Input:	dev_t of property.
20347c478bd9Sstevel@tonic-gate  *	dip:	Input:	dev_info_t pointer of child.
20357c478bd9Sstevel@tonic-gate  *	flags:	Input:	DDI_PROP_DONTPASS or NULL
20367c478bd9Sstevel@tonic-gate  *	name:	Input:	name of property
20377c478bd9Sstevel@tonic-gate  *	valuep:	Input:	ptr to callers buffer.
20387c478bd9Sstevel@tonic-gate  *	lengthp:I/O:	ptr to length of callers buffer on entry,
20397c478bd9Sstevel@tonic-gate  *			actual length of property on exit.
20407c478bd9Sstevel@tonic-gate  *
20417c478bd9Sstevel@tonic-gate  *	Possible returns:
20427c478bd9Sstevel@tonic-gate  *
20437c478bd9Sstevel@tonic-gate  *		DDI_PROP_SUCCESS	Prop found and returned
20447c478bd9Sstevel@tonic-gate  *		DDI_PROP_NOT_FOUND	Prop not found
20457c478bd9Sstevel@tonic-gate  *		DDI_PROP_UNDEFINED	Prop explicitly undefined.
20467c478bd9Sstevel@tonic-gate  *		DDI_PROP_BUF_TOO_SMALL	Prop found, callers buf too small,
20477c478bd9Sstevel@tonic-gate  *					no value returned, but actual prop
20487c478bd9Sstevel@tonic-gate  *					length returned in *lengthp
20497c478bd9Sstevel@tonic-gate  *
20507c478bd9Sstevel@tonic-gate  */
20517c478bd9Sstevel@tonic-gate 
20527c478bd9Sstevel@tonic-gate int
20537c478bd9Sstevel@tonic-gate ddi_getlongprop_buf(dev_t dev, dev_info_t *dip, int flags,
20547c478bd9Sstevel@tonic-gate     char *name, caddr_t valuep, int *lengthp)
20557c478bd9Sstevel@tonic-gate {
20567c478bd9Sstevel@tonic-gate 	return (ddi_prop_op(dev, dip, PROP_LEN_AND_VAL_BUF,
20577c478bd9Sstevel@tonic-gate 	    flags, name, valuep, lengthp));
20587c478bd9Sstevel@tonic-gate }
20597c478bd9Sstevel@tonic-gate 
20607c478bd9Sstevel@tonic-gate /*
20617c478bd9Sstevel@tonic-gate  * Integer/boolean sized props.
20627c478bd9Sstevel@tonic-gate  *
20637c478bd9Sstevel@tonic-gate  * Call is value only... returns found boolean or int sized prop value or
20647c478bd9Sstevel@tonic-gate  * defvalue if prop not found or is wrong length or is explicitly undefined.
20657c478bd9Sstevel@tonic-gate  * Only flag is DDI_PROP_DONTPASS...
20667c478bd9Sstevel@tonic-gate  *
20677c478bd9Sstevel@tonic-gate  * By convention, this interface returns boolean (0) sized properties
20687c478bd9Sstevel@tonic-gate  * as value (int)1.
20697c478bd9Sstevel@tonic-gate  *
20707c478bd9Sstevel@tonic-gate  * This never returns an error, if property not found or specifically
20717c478bd9Sstevel@tonic-gate  * undefined, the input `defvalue' is returned.
20727c478bd9Sstevel@tonic-gate  */
20737c478bd9Sstevel@tonic-gate 
20747c478bd9Sstevel@tonic-gate int
20757c478bd9Sstevel@tonic-gate ddi_getprop(dev_t dev, dev_info_t *dip, int flags, char *name, int defvalue)
20767c478bd9Sstevel@tonic-gate {
20777c478bd9Sstevel@tonic-gate 	int	propvalue = defvalue;
20787c478bd9Sstevel@tonic-gate 	int	proplength = sizeof (int);
20797c478bd9Sstevel@tonic-gate 	int	error;
20807c478bd9Sstevel@tonic-gate 
20817c478bd9Sstevel@tonic-gate 	error = ddi_prop_op(dev, dip, PROP_LEN_AND_VAL_BUF,
20827c478bd9Sstevel@tonic-gate 	    flags, name, (caddr_t)&propvalue, &proplength);
20837c478bd9Sstevel@tonic-gate 
20847c478bd9Sstevel@tonic-gate 	if ((error == DDI_PROP_SUCCESS) && (proplength == 0))
20857c478bd9Sstevel@tonic-gate 		propvalue = 1;
20867c478bd9Sstevel@tonic-gate 
20877c478bd9Sstevel@tonic-gate 	return (propvalue);
20887c478bd9Sstevel@tonic-gate }
20897c478bd9Sstevel@tonic-gate 
20907c478bd9Sstevel@tonic-gate /*
20917c478bd9Sstevel@tonic-gate  * Get prop length interface: flags are 0 or DDI_PROP_DONTPASS
20927c478bd9Sstevel@tonic-gate  * if returns DDI_PROP_SUCCESS, length returned in *lengthp.
20937c478bd9Sstevel@tonic-gate  */
20947c478bd9Sstevel@tonic-gate 
20957c478bd9Sstevel@tonic-gate int
20967c478bd9Sstevel@tonic-gate ddi_getproplen(dev_t dev, dev_info_t *dip, int flags, char *name, int *lengthp)
20977c478bd9Sstevel@tonic-gate {
20987c478bd9Sstevel@tonic-gate 	return (ddi_prop_op(dev, dip, PROP_LEN, flags, name, NULL, lengthp));
20997c478bd9Sstevel@tonic-gate }
21007c478bd9Sstevel@tonic-gate 
21017c478bd9Sstevel@tonic-gate /*
21027c478bd9Sstevel@tonic-gate  * Allocate a struct prop_driver_data, along with 'size' bytes
21037c478bd9Sstevel@tonic-gate  * for decoded property data.  This structure is freed by
21047c478bd9Sstevel@tonic-gate  * calling ddi_prop_free(9F).
21057c478bd9Sstevel@tonic-gate  */
21067c478bd9Sstevel@tonic-gate static void *
21077c478bd9Sstevel@tonic-gate ddi_prop_decode_alloc(size_t size, void (*prop_free)(struct prop_driver_data *))
21087c478bd9Sstevel@tonic-gate {
21097c478bd9Sstevel@tonic-gate 	struct prop_driver_data *pdd;
21107c478bd9Sstevel@tonic-gate 
21117c478bd9Sstevel@tonic-gate 	/*
21127c478bd9Sstevel@tonic-gate 	 * Allocate a structure with enough memory to store the decoded data.
21137c478bd9Sstevel@tonic-gate 	 */
21147c478bd9Sstevel@tonic-gate 	pdd = kmem_zalloc(sizeof (struct prop_driver_data) + size, KM_SLEEP);
21157c478bd9Sstevel@tonic-gate 	pdd->pdd_size = (sizeof (struct prop_driver_data) + size);
21167c478bd9Sstevel@tonic-gate 	pdd->pdd_prop_free = prop_free;
21177c478bd9Sstevel@tonic-gate 
21187c478bd9Sstevel@tonic-gate 	/*
21197c478bd9Sstevel@tonic-gate 	 * Return a pointer to the location to put the decoded data.
21207c478bd9Sstevel@tonic-gate 	 */
21217c478bd9Sstevel@tonic-gate 	return ((void *)((caddr_t)pdd + sizeof (struct prop_driver_data)));
21227c478bd9Sstevel@tonic-gate }
21237c478bd9Sstevel@tonic-gate 
21247c478bd9Sstevel@tonic-gate /*
21257c478bd9Sstevel@tonic-gate  * Allocated the memory needed to store the encoded data in the property
21267c478bd9Sstevel@tonic-gate  * handle.
21277c478bd9Sstevel@tonic-gate  */
21287c478bd9Sstevel@tonic-gate static int
21297c478bd9Sstevel@tonic-gate ddi_prop_encode_alloc(prop_handle_t *ph, size_t size)
21307c478bd9Sstevel@tonic-gate {
21317c478bd9Sstevel@tonic-gate 	/*
21327c478bd9Sstevel@tonic-gate 	 * If size is zero, then set data to NULL and size to 0.  This
21337c478bd9Sstevel@tonic-gate 	 * is a boolean property.
21347c478bd9Sstevel@tonic-gate 	 */
21357c478bd9Sstevel@tonic-gate 	if (size == 0) {
21367c478bd9Sstevel@tonic-gate 		ph->ph_size = 0;
21377c478bd9Sstevel@tonic-gate 		ph->ph_data = NULL;
21387c478bd9Sstevel@tonic-gate 		ph->ph_cur_pos = NULL;
21397c478bd9Sstevel@tonic-gate 		ph->ph_save_pos = NULL;
21407c478bd9Sstevel@tonic-gate 	} else {
21417c478bd9Sstevel@tonic-gate 		if (ph->ph_flags == DDI_PROP_DONTSLEEP) {
21427c478bd9Sstevel@tonic-gate 			ph->ph_data = kmem_zalloc(size, KM_NOSLEEP);
21437c478bd9Sstevel@tonic-gate 			if (ph->ph_data == NULL)
21447c478bd9Sstevel@tonic-gate 				return (DDI_PROP_NO_MEMORY);
21457c478bd9Sstevel@tonic-gate 		} else
21467c478bd9Sstevel@tonic-gate 			ph->ph_data = kmem_zalloc(size, KM_SLEEP);
21477c478bd9Sstevel@tonic-gate 		ph->ph_size = size;
21487c478bd9Sstevel@tonic-gate 		ph->ph_cur_pos = ph->ph_data;
21497c478bd9Sstevel@tonic-gate 		ph->ph_save_pos = ph->ph_data;
21507c478bd9Sstevel@tonic-gate 	}
21517c478bd9Sstevel@tonic-gate 	return (DDI_PROP_SUCCESS);
21527c478bd9Sstevel@tonic-gate }
21537c478bd9Sstevel@tonic-gate 
21547c478bd9Sstevel@tonic-gate /*
21557c478bd9Sstevel@tonic-gate  * Free the space allocated by the lookup routines.  Each lookup routine
21567c478bd9Sstevel@tonic-gate  * returns a pointer to the decoded data to the driver.  The driver then
21577c478bd9Sstevel@tonic-gate  * passes this pointer back to us.  This data actually lives in a struct
21587c478bd9Sstevel@tonic-gate  * prop_driver_data.  We use negative indexing to find the beginning of
21597c478bd9Sstevel@tonic-gate  * the structure and then free the entire structure using the size and
21607c478bd9Sstevel@tonic-gate  * the free routine stored in the structure.
21617c478bd9Sstevel@tonic-gate  */
21627c478bd9Sstevel@tonic-gate void
21637c478bd9Sstevel@tonic-gate ddi_prop_free(void *datap)
21647c478bd9Sstevel@tonic-gate {
21657c478bd9Sstevel@tonic-gate 	struct prop_driver_data *pdd;
21667c478bd9Sstevel@tonic-gate 
21677c478bd9Sstevel@tonic-gate 	/*
21687c478bd9Sstevel@tonic-gate 	 * Get the structure
21697c478bd9Sstevel@tonic-gate 	 */
21707c478bd9Sstevel@tonic-gate 	pdd = (struct prop_driver_data *)
2171184cd04cScth 	    ((caddr_t)datap - sizeof (struct prop_driver_data));
21727c478bd9Sstevel@tonic-gate 	/*
21737c478bd9Sstevel@tonic-gate 	 * Call the free routine to free it
21747c478bd9Sstevel@tonic-gate 	 */
21757c478bd9Sstevel@tonic-gate 	(*pdd->pdd_prop_free)(pdd);
21767c478bd9Sstevel@tonic-gate }
21777c478bd9Sstevel@tonic-gate 
21787c478bd9Sstevel@tonic-gate /*
21797c478bd9Sstevel@tonic-gate  * Free the data associated with an array of ints,
21807c478bd9Sstevel@tonic-gate  * allocated with ddi_prop_decode_alloc().
21817c478bd9Sstevel@tonic-gate  */
21827c478bd9Sstevel@tonic-gate static void
21837c478bd9Sstevel@tonic-gate ddi_prop_free_ints(struct prop_driver_data *pdd)
21847c478bd9Sstevel@tonic-gate {
21857c478bd9Sstevel@tonic-gate 	kmem_free(pdd, pdd->pdd_size);
21867c478bd9Sstevel@tonic-gate }
21877c478bd9Sstevel@tonic-gate 
21887c478bd9Sstevel@tonic-gate /*
21897c478bd9Sstevel@tonic-gate  * Free a single string property or a single string contained within
21907c478bd9Sstevel@tonic-gate  * the argv style return value of an array of strings.
21917c478bd9Sstevel@tonic-gate  */
21927c478bd9Sstevel@tonic-gate static void
21937c478bd9Sstevel@tonic-gate ddi_prop_free_string(struct prop_driver_data *pdd)
21947c478bd9Sstevel@tonic-gate {
21957c478bd9Sstevel@tonic-gate 	kmem_free(pdd, pdd->pdd_size);
21967c478bd9Sstevel@tonic-gate 
21977c478bd9Sstevel@tonic-gate }
21987c478bd9Sstevel@tonic-gate 
21997c478bd9Sstevel@tonic-gate /*
22007c478bd9Sstevel@tonic-gate  * Free an array of strings.
22017c478bd9Sstevel@tonic-gate  */
22027c478bd9Sstevel@tonic-gate static void
22037c478bd9Sstevel@tonic-gate ddi_prop_free_strings(struct prop_driver_data *pdd)
22047c478bd9Sstevel@tonic-gate {
22057c478bd9Sstevel@tonic-gate 	kmem_free(pdd, pdd->pdd_size);
22067c478bd9Sstevel@tonic-gate }
22077c478bd9Sstevel@tonic-gate 
22087c478bd9Sstevel@tonic-gate /*
22097c478bd9Sstevel@tonic-gate  * Free the data associated with an array of bytes.
22107c478bd9Sstevel@tonic-gate  */
22117c478bd9Sstevel@tonic-gate static void
22127c478bd9Sstevel@tonic-gate ddi_prop_free_bytes(struct prop_driver_data *pdd)
22137c478bd9Sstevel@tonic-gate {
22147c478bd9Sstevel@tonic-gate 	kmem_free(pdd, pdd->pdd_size);
22157c478bd9Sstevel@tonic-gate }
22167c478bd9Sstevel@tonic-gate 
22177c478bd9Sstevel@tonic-gate /*
22187c478bd9Sstevel@tonic-gate  * Reset the current location pointer in the property handle to the
22197c478bd9Sstevel@tonic-gate  * beginning of the data.
22207c478bd9Sstevel@tonic-gate  */
22217c478bd9Sstevel@tonic-gate void
22227c478bd9Sstevel@tonic-gate ddi_prop_reset_pos(prop_handle_t *ph)
22237c478bd9Sstevel@tonic-gate {
22247c478bd9Sstevel@tonic-gate 	ph->ph_cur_pos = ph->ph_data;
22257c478bd9Sstevel@tonic-gate 	ph->ph_save_pos = ph->ph_data;
22267c478bd9Sstevel@tonic-gate }
22277c478bd9Sstevel@tonic-gate 
22287c478bd9Sstevel@tonic-gate /*
22297c478bd9Sstevel@tonic-gate  * Restore the current location pointer in the property handle to the
22307c478bd9Sstevel@tonic-gate  * saved position.
22317c478bd9Sstevel@tonic-gate  */
22327c478bd9Sstevel@tonic-gate void
22337c478bd9Sstevel@tonic-gate ddi_prop_save_pos(prop_handle_t *ph)
22347c478bd9Sstevel@tonic-gate {
22357c478bd9Sstevel@tonic-gate 	ph->ph_save_pos = ph->ph_cur_pos;
22367c478bd9Sstevel@tonic-gate }
22377c478bd9Sstevel@tonic-gate 
22387c478bd9Sstevel@tonic-gate /*
22397c478bd9Sstevel@tonic-gate  * Save the location that the current location pointer is pointing to..
22407c478bd9Sstevel@tonic-gate  */
22417c478bd9Sstevel@tonic-gate void
22427c478bd9Sstevel@tonic-gate ddi_prop_restore_pos(prop_handle_t *ph)
22437c478bd9Sstevel@tonic-gate {
22447c478bd9Sstevel@tonic-gate 	ph->ph_cur_pos = ph->ph_save_pos;
22457c478bd9Sstevel@tonic-gate }
22467c478bd9Sstevel@tonic-gate 
22477c478bd9Sstevel@tonic-gate /*
22487c478bd9Sstevel@tonic-gate  * Property encode/decode functions
22497c478bd9Sstevel@tonic-gate  */
22507c478bd9Sstevel@tonic-gate 
22517c478bd9Sstevel@tonic-gate /*
22527c478bd9Sstevel@tonic-gate  * Decode a single integer property
22537c478bd9Sstevel@tonic-gate  */
22547c478bd9Sstevel@tonic-gate static int
22557c478bd9Sstevel@tonic-gate ddi_prop_fm_decode_int(prop_handle_t *ph, void *data, uint_t *nelements)
22567c478bd9Sstevel@tonic-gate {
22577c478bd9Sstevel@tonic-gate 	int	i;
22587c478bd9Sstevel@tonic-gate 	int	tmp;
22597c478bd9Sstevel@tonic-gate 
22607c478bd9Sstevel@tonic-gate 	/*
22617c478bd9Sstevel@tonic-gate 	 * If there is nothing to decode return an error
22627c478bd9Sstevel@tonic-gate 	 */
22637c478bd9Sstevel@tonic-gate 	if (ph->ph_size == 0)
22647c478bd9Sstevel@tonic-gate 		return (DDI_PROP_END_OF_DATA);
22657c478bd9Sstevel@tonic-gate 
22667c478bd9Sstevel@tonic-gate 	/*
22677c478bd9Sstevel@tonic-gate 	 * Decode the property as a single integer and return it
22687c478bd9Sstevel@tonic-gate 	 * in data if we were able to decode it.
22697c478bd9Sstevel@tonic-gate 	 */
22707c478bd9Sstevel@tonic-gate 	i = DDI_PROP_INT(ph, DDI_PROP_CMD_DECODE, &tmp);
22717c478bd9Sstevel@tonic-gate 	if (i < DDI_PROP_RESULT_OK) {
22727c478bd9Sstevel@tonic-gate 		switch (i) {
22737c478bd9Sstevel@tonic-gate 		case DDI_PROP_RESULT_EOF:
22747c478bd9Sstevel@tonic-gate 			return (DDI_PROP_END_OF_DATA);
22757c478bd9Sstevel@tonic-gate 
22767c478bd9Sstevel@tonic-gate 		case DDI_PROP_RESULT_ERROR:
22777c478bd9Sstevel@tonic-gate 			return (DDI_PROP_CANNOT_DECODE);
22787c478bd9Sstevel@tonic-gate 		}
22797c478bd9Sstevel@tonic-gate 	}
22807c478bd9Sstevel@tonic-gate 
22817c478bd9Sstevel@tonic-gate 	*(int *)data = tmp;
22827c478bd9Sstevel@tonic-gate 	*nelements = 1;
22837c478bd9Sstevel@tonic-gate 	return (DDI_PROP_SUCCESS);
22847c478bd9Sstevel@tonic-gate }
22857c478bd9Sstevel@tonic-gate 
22867c478bd9Sstevel@tonic-gate /*
22877c478bd9Sstevel@tonic-gate  * Decode a single 64 bit integer property
22887c478bd9Sstevel@tonic-gate  */
22897c478bd9Sstevel@tonic-gate static int
22907c478bd9Sstevel@tonic-gate ddi_prop_fm_decode_int64(prop_handle_t *ph, void *data, uint_t *nelements)
22917c478bd9Sstevel@tonic-gate {
22927c478bd9Sstevel@tonic-gate 	int	i;
22937c478bd9Sstevel@tonic-gate 	int64_t	tmp;
22947c478bd9Sstevel@tonic-gate 
22957c478bd9Sstevel@tonic-gate 	/*
22967c478bd9Sstevel@tonic-gate 	 * If there is nothing to decode return an error
22977c478bd9Sstevel@tonic-gate 	 */
22987c478bd9Sstevel@tonic-gate 	if (ph->ph_size == 0)
22997c478bd9Sstevel@tonic-gate 		return (DDI_PROP_END_OF_DATA);
23007c478bd9Sstevel@tonic-gate 
23017c478bd9Sstevel@tonic-gate 	/*
23027c478bd9Sstevel@tonic-gate 	 * Decode the property as a single integer and return it
23037c478bd9Sstevel@tonic-gate 	 * in data if we were able to decode it.
23047c478bd9Sstevel@tonic-gate 	 */
23057c478bd9Sstevel@tonic-gate 	i = DDI_PROP_INT64(ph, DDI_PROP_CMD_DECODE, &tmp);
23067c478bd9Sstevel@tonic-gate 	if (i < DDI_PROP_RESULT_OK) {
23077c478bd9Sstevel@tonic-gate 		switch (i) {
23087c478bd9Sstevel@tonic-gate 		case DDI_PROP_RESULT_EOF:
23097c478bd9Sstevel@tonic-gate 			return (DDI_PROP_END_OF_DATA);
23107c478bd9Sstevel@tonic-gate 
23117c478bd9Sstevel@tonic-gate 		case DDI_PROP_RESULT_ERROR:
23127c478bd9Sstevel@tonic-gate 			return (DDI_PROP_CANNOT_DECODE);
23137c478bd9Sstevel@tonic-gate 		}
23147c478bd9Sstevel@tonic-gate 	}
23157c478bd9Sstevel@tonic-gate 
23167c478bd9Sstevel@tonic-gate 	*(int64_t *)data = tmp;
23177c478bd9Sstevel@tonic-gate 	*nelements = 1;
23187c478bd9Sstevel@tonic-gate 	return (DDI_PROP_SUCCESS);
23197c478bd9Sstevel@tonic-gate }
23207c478bd9Sstevel@tonic-gate 
23217c478bd9Sstevel@tonic-gate /*
23227c478bd9Sstevel@tonic-gate  * Decode an array of integers property
23237c478bd9Sstevel@tonic-gate  */
23247c478bd9Sstevel@tonic-gate static int
23257c478bd9Sstevel@tonic-gate ddi_prop_fm_decode_ints(prop_handle_t *ph, void *data, uint_t *nelements)
23267c478bd9Sstevel@tonic-gate {
23277c478bd9Sstevel@tonic-gate 	int	i;
23287c478bd9Sstevel@tonic-gate 	int	cnt = 0;
23297c478bd9Sstevel@tonic-gate 	int	*tmp;
23307c478bd9Sstevel@tonic-gate 	int	*intp;
23317c478bd9Sstevel@tonic-gate 	int	n;
23327c478bd9Sstevel@tonic-gate 
23337c478bd9Sstevel@tonic-gate 	/*
23347c478bd9Sstevel@tonic-gate 	 * Figure out how many array elements there are by going through the
23357c478bd9Sstevel@tonic-gate 	 * data without decoding it first and counting.
23367c478bd9Sstevel@tonic-gate 	 */
23377c478bd9Sstevel@tonic-gate 	for (;;) {
23387c478bd9Sstevel@tonic-gate 		i = DDI_PROP_INT(ph, DDI_PROP_CMD_SKIP, NULL);
23397c478bd9Sstevel@tonic-gate 		if (i < 0)
23407c478bd9Sstevel@tonic-gate 			break;
23417c478bd9Sstevel@tonic-gate 		cnt++;
23427c478bd9Sstevel@tonic-gate 	}
23437c478bd9Sstevel@tonic-gate 
23447c478bd9Sstevel@tonic-gate 	/*
23457c478bd9Sstevel@tonic-gate 	 * If there are no elements return an error
23467c478bd9Sstevel@tonic-gate 	 */
23477c478bd9Sstevel@tonic-gate 	if (cnt == 0)
23487c478bd9Sstevel@tonic-gate 		return (DDI_PROP_END_OF_DATA);
23497c478bd9Sstevel@tonic-gate 
23507c478bd9Sstevel@tonic-gate 	/*
23517c478bd9Sstevel@tonic-gate 	 * If we cannot skip through the data, we cannot decode it
23527c478bd9Sstevel@tonic-gate 	 */
23537c478bd9Sstevel@tonic-gate 	if (i == DDI_PROP_RESULT_ERROR)
23547c478bd9Sstevel@tonic-gate 		return (DDI_PROP_CANNOT_DECODE);
23557c478bd9Sstevel@tonic-gate 
23567c478bd9Sstevel@tonic-gate 	/*
23577c478bd9Sstevel@tonic-gate 	 * Reset the data pointer to the beginning of the encoded data
23587c478bd9Sstevel@tonic-gate 	 */
23597c478bd9Sstevel@tonic-gate 	ddi_prop_reset_pos(ph);
23607c478bd9Sstevel@tonic-gate 
23617c478bd9Sstevel@tonic-gate 	/*
23627c478bd9Sstevel@tonic-gate 	 * Allocated memory to store the decoded value in.
23637c478bd9Sstevel@tonic-gate 	 */
23647c478bd9Sstevel@tonic-gate 	intp = ddi_prop_decode_alloc((cnt * sizeof (int)),
2365184cd04cScth 	    ddi_prop_free_ints);
23667c478bd9Sstevel@tonic-gate 
23677c478bd9Sstevel@tonic-gate 	/*
23687c478bd9Sstevel@tonic-gate 	 * Decode each element and place it in the space we just allocated
23697c478bd9Sstevel@tonic-gate 	 */
23707c478bd9Sstevel@tonic-gate 	tmp = intp;
23717c478bd9Sstevel@tonic-gate 	for (n = 0; n < cnt; n++, tmp++) {
23727c478bd9Sstevel@tonic-gate 		i = DDI_PROP_INT(ph, DDI_PROP_CMD_DECODE, tmp);
23737c478bd9Sstevel@tonic-gate 		if (i < DDI_PROP_RESULT_OK) {
23747c478bd9Sstevel@tonic-gate 			/*
23757c478bd9Sstevel@tonic-gate 			 * Free the space we just allocated
23767c478bd9Sstevel@tonic-gate 			 * and return an error.
23777c478bd9Sstevel@tonic-gate 			 */
23787c478bd9Sstevel@tonic-gate 			ddi_prop_free(intp);
23797c478bd9Sstevel@tonic-gate 			switch (i) {
23807c478bd9Sstevel@tonic-gate 			case DDI_PROP_RESULT_EOF:
23817c478bd9Sstevel@tonic-gate 				return (DDI_PROP_END_OF_DATA);
23827c478bd9Sstevel@tonic-gate 
23837c478bd9Sstevel@tonic-gate 			case DDI_PROP_RESULT_ERROR:
23847c478bd9Sstevel@tonic-gate 				return (DDI_PROP_CANNOT_DECODE);
23857c478bd9Sstevel@tonic-gate 			}
23867c478bd9Sstevel@tonic-gate 		}
23877c478bd9Sstevel@tonic-gate 	}
23887c478bd9Sstevel@tonic-gate 
23897c478bd9Sstevel@tonic-gate 	*nelements = cnt;
23907c478bd9Sstevel@tonic-gate 	*(int **)data = intp;
23917c478bd9Sstevel@tonic-gate 
23927c478bd9Sstevel@tonic-gate 	return (DDI_PROP_SUCCESS);
23937c478bd9Sstevel@tonic-gate }
23947c478bd9Sstevel@tonic-gate 
23957c478bd9Sstevel@tonic-gate /*
23967c478bd9Sstevel@tonic-gate  * Decode a 64 bit integer array property
23977c478bd9Sstevel@tonic-gate  */
23987c478bd9Sstevel@tonic-gate static int
23997c478bd9Sstevel@tonic-gate ddi_prop_fm_decode_int64_array(prop_handle_t *ph, void *data, uint_t *nelements)
24007c478bd9Sstevel@tonic-gate {
24017c478bd9Sstevel@tonic-gate 	int	i;
24027c478bd9Sstevel@tonic-gate 	int	n;
24037c478bd9Sstevel@tonic-gate 	int	cnt = 0;
24047c478bd9Sstevel@tonic-gate 	int64_t	*tmp;
24057c478bd9Sstevel@tonic-gate 	int64_t	*intp;
24067c478bd9Sstevel@tonic-gate 
24077c478bd9Sstevel@tonic-gate 	/*
24087c478bd9Sstevel@tonic-gate 	 * Count the number of array elements by going
24097c478bd9Sstevel@tonic-gate 	 * through the data without decoding it.
24107c478bd9Sstevel@tonic-gate 	 */
24117c478bd9Sstevel@tonic-gate 	for (;;) {
24127c478bd9Sstevel@tonic-gate 		i = DDI_PROP_INT64(ph, DDI_PROP_CMD_SKIP, NULL);
24137c478bd9Sstevel@tonic-gate 		if (i < 0)
24147c478bd9Sstevel@tonic-gate 			break;
24157c478bd9Sstevel@tonic-gate 		cnt++;
24167c478bd9Sstevel@tonic-gate 	}
24177c478bd9Sstevel@tonic-gate 
24187c478bd9Sstevel@tonic-gate 	/*
24197c478bd9Sstevel@tonic-gate 	 * If there are no elements return an error
24207c478bd9Sstevel@tonic-gate 	 */
24217c478bd9Sstevel@tonic-gate 	if (cnt == 0)
24227c478bd9Sstevel@tonic-gate 		return (DDI_PROP_END_OF_DATA);
24237c478bd9Sstevel@tonic-gate 
24247c478bd9Sstevel@tonic-gate 	/*
24257c478bd9Sstevel@tonic-gate 	 * If we cannot skip through the data, we cannot decode it
24267c478bd9Sstevel@tonic-gate 	 */
24277c478bd9Sstevel@tonic-gate 	if (i == DDI_PROP_RESULT_ERROR)
24287c478bd9Sstevel@tonic-gate 		return (DDI_PROP_CANNOT_DECODE);
24297c478bd9Sstevel@tonic-gate 
24307c478bd9Sstevel@tonic-gate 	/*
24317c478bd9Sstevel@tonic-gate 	 * Reset the data pointer to the beginning of the encoded data
24327c478bd9Sstevel@tonic-gate 	 */
24337c478bd9Sstevel@tonic-gate 	ddi_prop_reset_pos(ph);
24347c478bd9Sstevel@tonic-gate 
24357c478bd9Sstevel@tonic-gate 	/*
24367c478bd9Sstevel@tonic-gate 	 * Allocate memory to store the decoded value.
24377c478bd9Sstevel@tonic-gate 	 */
24387c478bd9Sstevel@tonic-gate 	intp = ddi_prop_decode_alloc((cnt * sizeof (int64_t)),
2439184cd04cScth 	    ddi_prop_free_ints);
24407c478bd9Sstevel@tonic-gate 
24417c478bd9Sstevel@tonic-gate 	/*
24427c478bd9Sstevel@tonic-gate 	 * Decode each element and place it in the space allocated
24437c478bd9Sstevel@tonic-gate 	 */
24447c478bd9Sstevel@tonic-gate 	tmp = intp;
24457c478bd9Sstevel@tonic-gate 	for (n = 0; n < cnt; n++, tmp++) {
24467c478bd9Sstevel@tonic-gate 		i = DDI_PROP_INT64(ph, DDI_PROP_CMD_DECODE, tmp);
24477c478bd9Sstevel@tonic-gate 		if (i < DDI_PROP_RESULT_OK) {
24487c478bd9Sstevel@tonic-gate 			/*
24497c478bd9Sstevel@tonic-gate 			 * Free the space we just allocated
24507c478bd9Sstevel@tonic-gate 			 * and return an error.
24517c478bd9Sstevel@tonic-gate 			 */
24527c478bd9Sstevel@tonic-gate 			ddi_prop_free(intp);
24537c478bd9Sstevel@tonic-gate 			switch (i) {
24547c478bd9Sstevel@tonic-gate 			case DDI_PROP_RESULT_EOF:
24557c478bd9Sstevel@tonic-gate 				return (DDI_PROP_END_OF_DATA);
24567c478bd9Sstevel@tonic-gate 
24577c478bd9Sstevel@tonic-gate 			case DDI_PROP_RESULT_ERROR:
24587c478bd9Sstevel@tonic-gate 				return (DDI_PROP_CANNOT_DECODE);
24597c478bd9Sstevel@tonic-gate 			}
24607c478bd9Sstevel@tonic-gate 		}
24617c478bd9Sstevel@tonic-gate 	}
24627c478bd9Sstevel@tonic-gate 
24637c478bd9Sstevel@tonic-gate 	*nelements = cnt;
24647c478bd9Sstevel@tonic-gate 	*(int64_t **)data = intp;
24657c478bd9Sstevel@tonic-gate 
24667c478bd9Sstevel@tonic-gate 	return (DDI_PROP_SUCCESS);
24677c478bd9Sstevel@tonic-gate }
24687c478bd9Sstevel@tonic-gate 
24697c478bd9Sstevel@tonic-gate /*
24707c478bd9Sstevel@tonic-gate  * Encode an array of integers property (Can be one element)
24717c478bd9Sstevel@tonic-gate  */
24727c478bd9Sstevel@tonic-gate int
24737c478bd9Sstevel@tonic-gate ddi_prop_fm_encode_ints(prop_handle_t *ph, void *data, uint_t nelements)
24747c478bd9Sstevel@tonic-gate {
24757c478bd9Sstevel@tonic-gate 	int	i;
24767c478bd9Sstevel@tonic-gate 	int	*tmp;
24777c478bd9Sstevel@tonic-gate 	int	cnt;
24787c478bd9Sstevel@tonic-gate 	int	size;
24797c478bd9Sstevel@tonic-gate 
24807c478bd9Sstevel@tonic-gate 	/*
24817c478bd9Sstevel@tonic-gate 	 * If there is no data, we cannot do anything
24827c478bd9Sstevel@tonic-gate 	 */
24837c478bd9Sstevel@tonic-gate 	if (nelements == 0)
24847c478bd9Sstevel@tonic-gate 		return (DDI_PROP_CANNOT_ENCODE);
24857c478bd9Sstevel@tonic-gate 
24867c478bd9Sstevel@tonic-gate 	/*
24877c478bd9Sstevel@tonic-gate 	 * Get the size of an encoded int.
24887c478bd9Sstevel@tonic-gate 	 */
24897c478bd9Sstevel@tonic-gate 	size = DDI_PROP_INT(ph, DDI_PROP_CMD_GET_ESIZE, NULL);
24907c478bd9Sstevel@tonic-gate 
24917c478bd9Sstevel@tonic-gate 	if (size < DDI_PROP_RESULT_OK) {
24927c478bd9Sstevel@tonic-gate 		switch (size) {
24937c478bd9Sstevel@tonic-gate 		case DDI_PROP_RESULT_EOF:
24947c478bd9Sstevel@tonic-gate 			return (DDI_PROP_END_OF_DATA);
24957c478bd9Sstevel@tonic-gate 
24967c478bd9Sstevel@tonic-gate 		case DDI_PROP_RESULT_ERROR:
24977c478bd9Sstevel@tonic-gate 			return (DDI_PROP_CANNOT_ENCODE);
24987c478bd9Sstevel@tonic-gate 		}
24997c478bd9Sstevel@tonic-gate 	}
25007c478bd9Sstevel@tonic-gate 
25017c478bd9Sstevel@tonic-gate 	/*
25027c478bd9Sstevel@tonic-gate 	 * Allocate space in the handle to store the encoded int.
25037c478bd9Sstevel@tonic-gate 	 */
25047c478bd9Sstevel@tonic-gate 	if (ddi_prop_encode_alloc(ph, size * nelements) !=
2505184cd04cScth 	    DDI_PROP_SUCCESS)
25067c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NO_MEMORY);
25077c478bd9Sstevel@tonic-gate 
25087c478bd9Sstevel@tonic-gate 	/*
25097c478bd9Sstevel@tonic-gate 	 * Encode the array of ints.
25107c478bd9Sstevel@tonic-gate 	 */
25117c478bd9Sstevel@tonic-gate 	tmp = (int *)data;
25127c478bd9Sstevel@tonic-gate 	for (cnt = 0; cnt < nelements; cnt++, tmp++) {
25137c478bd9Sstevel@tonic-gate 		i = DDI_PROP_INT(ph, DDI_PROP_CMD_ENCODE, tmp);
25147c478bd9Sstevel@tonic-gate 		if (i < DDI_PROP_RESULT_OK) {
25157c478bd9Sstevel@tonic-gate 			switch (i) {
25167c478bd9Sstevel@tonic-gate 			case DDI_PROP_RESULT_EOF:
25177c478bd9Sstevel@tonic-gate 				return (DDI_PROP_END_OF_DATA);
25187c478bd9Sstevel@tonic-gate 
25197c478bd9Sstevel@tonic-gate 			case DDI_PROP_RESULT_ERROR:
25207c478bd9Sstevel@tonic-gate 				return (DDI_PROP_CANNOT_ENCODE);
25217c478bd9Sstevel@tonic-gate 			}
25227c478bd9Sstevel@tonic-gate 		}
25237c478bd9Sstevel@tonic-gate 	}
25247c478bd9Sstevel@tonic-gate 
25257c478bd9Sstevel@tonic-gate 	return (DDI_PROP_SUCCESS);
25267c478bd9Sstevel@tonic-gate }
25277c478bd9Sstevel@tonic-gate 
25287c478bd9Sstevel@tonic-gate 
25297c478bd9Sstevel@tonic-gate /*
25307c478bd9Sstevel@tonic-gate  * Encode a 64 bit integer array property
25317c478bd9Sstevel@tonic-gate  */
25327c478bd9Sstevel@tonic-gate int
25337c478bd9Sstevel@tonic-gate ddi_prop_fm_encode_int64(prop_handle_t *ph, void *data, uint_t nelements)
25347c478bd9Sstevel@tonic-gate {
25357c478bd9Sstevel@tonic-gate 	int i;
25367c478bd9Sstevel@tonic-gate 	int cnt;
25377c478bd9Sstevel@tonic-gate 	int size;
25387c478bd9Sstevel@tonic-gate 	int64_t *tmp;
25397c478bd9Sstevel@tonic-gate 
25407c478bd9Sstevel@tonic-gate 	/*
25417c478bd9Sstevel@tonic-gate 	 * If there is no data, we cannot do anything
25427c478bd9Sstevel@tonic-gate 	 */
25437c478bd9Sstevel@tonic-gate 	if (nelements == 0)
25447c478bd9Sstevel@tonic-gate 		return (DDI_PROP_CANNOT_ENCODE);
25457c478bd9Sstevel@tonic-gate 
25467c478bd9Sstevel@tonic-gate 	/*
25477c478bd9Sstevel@tonic-gate 	 * Get the size of an encoded 64 bit int.
25487c478bd9Sstevel@tonic-gate 	 */
25497c478bd9Sstevel@tonic-gate 	size = DDI_PROP_INT64(ph, DDI_PROP_CMD_GET_ESIZE, NULL);
25507c478bd9Sstevel@tonic-gate 
25517c478bd9Sstevel@tonic-gate 	if (size < DDI_PROP_RESULT_OK) {
25527c478bd9Sstevel@tonic-gate 		switch (size) {
25537c478bd9Sstevel@tonic-gate 		case DDI_PROP_RESULT_EOF:
25547c478bd9Sstevel@tonic-gate 			return (DDI_PROP_END_OF_DATA);
25557c478bd9Sstevel@tonic-gate 
25567c478bd9Sstevel@tonic-gate 		case DDI_PROP_RESULT_ERROR:
25577c478bd9Sstevel@tonic-gate 			return (DDI_PROP_CANNOT_ENCODE);
25587c478bd9Sstevel@tonic-gate 		}
25597c478bd9Sstevel@tonic-gate 	}
25607c478bd9Sstevel@tonic-gate 
25617c478bd9Sstevel@tonic-gate 	/*
25627c478bd9Sstevel@tonic-gate 	 * Allocate space in the handle to store the encoded int.
25637c478bd9Sstevel@tonic-gate 	 */
25647c478bd9Sstevel@tonic-gate 	if (ddi_prop_encode_alloc(ph, size * nelements) !=
25657c478bd9Sstevel@tonic-gate 	    DDI_PROP_SUCCESS)
25667c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NO_MEMORY);
25677c478bd9Sstevel@tonic-gate 
25687c478bd9Sstevel@tonic-gate 	/*
25697c478bd9Sstevel@tonic-gate 	 * Encode the array of ints.
25707c478bd9Sstevel@tonic-gate 	 */
25717c478bd9Sstevel@tonic-gate 	tmp = (int64_t *)data;
25727c478bd9Sstevel@tonic-gate 	for (cnt = 0; cnt < nelements; cnt++, tmp++) {
25737c478bd9Sstevel@tonic-gate 		i = DDI_PROP_INT64(ph, DDI_PROP_CMD_ENCODE, tmp);
25747c478bd9Sstevel@tonic-gate 		if (i < DDI_PROP_RESULT_OK) {
25757c478bd9Sstevel@tonic-gate 			switch (i) {
25767c478bd9Sstevel@tonic-gate 			case DDI_PROP_RESULT_EOF:
25777c478bd9Sstevel@tonic-gate 				return (DDI_PROP_END_OF_DATA);
25787c478bd9Sstevel@tonic-gate 
25797c478bd9Sstevel@tonic-gate 			case DDI_PROP_RESULT_ERROR:
25807c478bd9Sstevel@tonic-gate 				return (DDI_PROP_CANNOT_ENCODE);
25817c478bd9Sstevel@tonic-gate 			}
25827c478bd9Sstevel@tonic-gate 		}
25837c478bd9Sstevel@tonic-gate 	}
25847c478bd9Sstevel@tonic-gate 
25857c478bd9Sstevel@tonic-gate 	return (DDI_PROP_SUCCESS);
25867c478bd9Sstevel@tonic-gate }
25877c478bd9Sstevel@tonic-gate 
25887c478bd9Sstevel@tonic-gate /*
25897c478bd9Sstevel@tonic-gate  * Decode a single string property
25907c478bd9Sstevel@tonic-gate  */
25917c478bd9Sstevel@tonic-gate static int
25927c478bd9Sstevel@tonic-gate ddi_prop_fm_decode_string(prop_handle_t *ph, void *data, uint_t *nelements)
25937c478bd9Sstevel@tonic-gate {
25947c478bd9Sstevel@tonic-gate 	char		*tmp;
25957c478bd9Sstevel@tonic-gate 	char		*str;
25967c478bd9Sstevel@tonic-gate 	int		i;
25977c478bd9Sstevel@tonic-gate 	int		size;
25987c478bd9Sstevel@tonic-gate 
25997c478bd9Sstevel@tonic-gate 	/*
26007c478bd9Sstevel@tonic-gate 	 * If there is nothing to decode return an error
26017c478bd9Sstevel@tonic-gate 	 */
26027c478bd9Sstevel@tonic-gate 	if (ph->ph_size == 0)
26037c478bd9Sstevel@tonic-gate 		return (DDI_PROP_END_OF_DATA);
26047c478bd9Sstevel@tonic-gate 
26057c478bd9Sstevel@tonic-gate 	/*
26067c478bd9Sstevel@tonic-gate 	 * Get the decoded size of the encoded string.
26077c478bd9Sstevel@tonic-gate 	 */
26087c478bd9Sstevel@tonic-gate 	size = DDI_PROP_STR(ph, DDI_PROP_CMD_GET_DSIZE, NULL);
26097c478bd9Sstevel@tonic-gate 	if (size < DDI_PROP_RESULT_OK) {
26107c478bd9Sstevel@tonic-gate 		switch (size) {
26117c478bd9Sstevel@tonic-gate 		case DDI_PROP_RESULT_EOF:
26127c478bd9Sstevel@tonic-gate 			return (DDI_PROP_END_OF_DATA);
26137c478bd9Sstevel@tonic-gate 
26147c478bd9Sstevel@tonic-gate 		case DDI_PROP_RESULT_ERROR:
26157c478bd9Sstevel@tonic-gate 			return (DDI_PROP_CANNOT_DECODE);
26167c478bd9Sstevel@tonic-gate 		}
26177c478bd9Sstevel@tonic-gate 	}
26187c478bd9Sstevel@tonic-gate 
26197c478bd9Sstevel@tonic-gate 	/*
26207c478bd9Sstevel@tonic-gate 	 * Allocated memory to store the decoded value in.
26217c478bd9Sstevel@tonic-gate 	 */
26227c478bd9Sstevel@tonic-gate 	str = ddi_prop_decode_alloc((size_t)size, ddi_prop_free_string);
26237c478bd9Sstevel@tonic-gate 
26247c478bd9Sstevel@tonic-gate 	ddi_prop_reset_pos(ph);
26257c478bd9Sstevel@tonic-gate 
26267c478bd9Sstevel@tonic-gate 	/*
26277c478bd9Sstevel@tonic-gate 	 * Decode the str and place it in the space we just allocated
26287c478bd9Sstevel@tonic-gate 	 */
26297c478bd9Sstevel@tonic-gate 	tmp = str;
26307c478bd9Sstevel@tonic-gate 	i = DDI_PROP_STR(ph, DDI_PROP_CMD_DECODE, tmp);
26317c478bd9Sstevel@tonic-gate 	if (i < DDI_PROP_RESULT_OK) {
26327c478bd9Sstevel@tonic-gate 		/*
26337c478bd9Sstevel@tonic-gate 		 * Free the space we just allocated
26347c478bd9Sstevel@tonic-gate 		 * and return an error.
26357c478bd9Sstevel@tonic-gate 		 */
26367c478bd9Sstevel@tonic-gate 		ddi_prop_free(str);
26377c478bd9Sstevel@tonic-gate 		switch (i) {
26387c478bd9Sstevel@tonic-gate 		case DDI_PROP_RESULT_EOF:
26397c478bd9Sstevel@tonic-gate 			return (DDI_PROP_END_OF_DATA);
26407c478bd9Sstevel@tonic-gate 
26417c478bd9Sstevel@tonic-gate 		case DDI_PROP_RESULT_ERROR:
26427c478bd9Sstevel@tonic-gate 			return (DDI_PROP_CANNOT_DECODE);
26437c478bd9Sstevel@tonic-gate 		}
26447c478bd9Sstevel@tonic-gate 	}
26457c478bd9Sstevel@tonic-gate 
26467c478bd9Sstevel@tonic-gate 	*(char **)data = str;
26477c478bd9Sstevel@tonic-gate 	*nelements = 1;
26487c478bd9Sstevel@tonic-gate 
26497c478bd9Sstevel@tonic-gate 	return (DDI_PROP_SUCCESS);
26507c478bd9Sstevel@tonic-gate }
26517c478bd9Sstevel@tonic-gate 
26527c478bd9Sstevel@tonic-gate /*
26537c478bd9Sstevel@tonic-gate  * Decode an array of strings.
26547c478bd9Sstevel@tonic-gate  */
26557c478bd9Sstevel@tonic-gate int
26567c478bd9Sstevel@tonic-gate ddi_prop_fm_decode_strings(prop_handle_t *ph, void *data, uint_t *nelements)
26577c478bd9Sstevel@tonic-gate {
26587c478bd9Sstevel@tonic-gate 	int		cnt = 0;
26597c478bd9Sstevel@tonic-gate 	char		**strs;
26607c478bd9Sstevel@tonic-gate 	char		**tmp;
26617c478bd9Sstevel@tonic-gate 	char		*ptr;
26627c478bd9Sstevel@tonic-gate 	int		i;
26637c478bd9Sstevel@tonic-gate 	int		n;
26647c478bd9Sstevel@tonic-gate 	int		size;
26657c478bd9Sstevel@tonic-gate 	size_t		nbytes;
26667c478bd9Sstevel@tonic-gate 
26677c478bd9Sstevel@tonic-gate 	/*
26687c478bd9Sstevel@tonic-gate 	 * Figure out how many array elements there are by going through the
26697c478bd9Sstevel@tonic-gate 	 * data without decoding it first and counting.
26707c478bd9Sstevel@tonic-gate 	 */
26717c478bd9Sstevel@tonic-gate 	for (;;) {
26727c478bd9Sstevel@tonic-gate 		i = DDI_PROP_STR(ph, DDI_PROP_CMD_SKIP, NULL);
26737c478bd9Sstevel@tonic-gate 		if (i < 0)
26747c478bd9Sstevel@tonic-gate 			break;
26757c478bd9Sstevel@tonic-gate 		cnt++;
26767c478bd9Sstevel@tonic-gate 	}
26777c478bd9Sstevel@tonic-gate 
26787c478bd9Sstevel@tonic-gate 	/*
26797c478bd9Sstevel@tonic-gate 	 * If there are no elements return an error
26807c478bd9Sstevel@tonic-gate 	 */
26817c478bd9Sstevel@tonic-gate 	if (cnt == 0)
26827c478bd9Sstevel@tonic-gate 		return (DDI_PROP_END_OF_DATA);
26837c478bd9Sstevel@tonic-gate 
26847c478bd9Sstevel@tonic-gate 	/*
26857c478bd9Sstevel@tonic-gate 	 * If we cannot skip through the data, we cannot decode it
26867c478bd9Sstevel@tonic-gate 	 */
26877c478bd9Sstevel@tonic-gate 	if (i == DDI_PROP_RESULT_ERROR)
26887c478bd9Sstevel@tonic-gate 		return (DDI_PROP_CANNOT_DECODE);
26897c478bd9Sstevel@tonic-gate 
26907c478bd9Sstevel@tonic-gate 	/*
26917c478bd9Sstevel@tonic-gate 	 * Reset the data pointer to the beginning of the encoded data
26927c478bd9Sstevel@tonic-gate 	 */
26937c478bd9Sstevel@tonic-gate 	ddi_prop_reset_pos(ph);
26947c478bd9Sstevel@tonic-gate 
26957c478bd9Sstevel@tonic-gate 	/*
26967c478bd9Sstevel@tonic-gate 	 * Figure out how much memory we need for the sum total
26977c478bd9Sstevel@tonic-gate 	 */
26987c478bd9Sstevel@tonic-gate 	nbytes = (cnt + 1) * sizeof (char *);
26997c478bd9Sstevel@tonic-gate 
27007c478bd9Sstevel@tonic-gate 	for (n = 0; n < cnt; n++) {
27017c478bd9Sstevel@tonic-gate 		/*
27027c478bd9Sstevel@tonic-gate 		 * Get the decoded size of the current encoded string.
27037c478bd9Sstevel@tonic-gate 		 */
27047c478bd9Sstevel@tonic-gate 		size = DDI_PROP_STR(ph, DDI_PROP_CMD_GET_DSIZE, NULL);
27057c478bd9Sstevel@tonic-gate 		if (size < DDI_PROP_RESULT_OK) {
27067c478bd9Sstevel@tonic-gate 			switch (size) {
27077c478bd9Sstevel@tonic-gate 			case DDI_PROP_RESULT_EOF:
27087c478bd9Sstevel@tonic-gate 				return (DDI_PROP_END_OF_DATA);
27097c478bd9Sstevel@tonic-gate 
27107c478bd9Sstevel@tonic-gate 			case DDI_PROP_RESULT_ERROR:
27117c478bd9Sstevel@tonic-gate 				return (DDI_PROP_CANNOT_DECODE);
27127c478bd9Sstevel@tonic-gate 			}
27137c478bd9Sstevel@tonic-gate 		}
27147c478bd9Sstevel@tonic-gate 
27157c478bd9Sstevel@tonic-gate 		nbytes += size;
27167c478bd9Sstevel@tonic-gate 	}
27177c478bd9Sstevel@tonic-gate 
27187c478bd9Sstevel@tonic-gate 	/*
27197c478bd9Sstevel@tonic-gate 	 * Allocate memory in which to store the decoded strings.
27207c478bd9Sstevel@tonic-gate 	 */
27217c478bd9Sstevel@tonic-gate 	strs = ddi_prop_decode_alloc(nbytes, ddi_prop_free_strings);
27227c478bd9Sstevel@tonic-gate 
27237c478bd9Sstevel@tonic-gate 	/*
27247c478bd9Sstevel@tonic-gate 	 * Set up pointers for each string by figuring out yet
27257c478bd9Sstevel@tonic-gate 	 * again how long each string is.
27267c478bd9Sstevel@tonic-gate 	 */
27277c478bd9Sstevel@tonic-gate 	ddi_prop_reset_pos(ph);
27287c478bd9Sstevel@tonic-gate 	ptr = (caddr_t)strs + ((cnt + 1) * sizeof (char *));
27297c478bd9Sstevel@tonic-gate 	for (tmp = strs, n = 0; n < cnt; n++, tmp++) {
27307c478bd9Sstevel@tonic-gate 		/*
27317c478bd9Sstevel@tonic-gate 		 * Get the decoded size of the current encoded string.
27327c478bd9Sstevel@tonic-gate 		 */
27337c478bd9Sstevel@tonic-gate 		size = DDI_PROP_STR(ph, DDI_PROP_CMD_GET_DSIZE, NULL);
27347c478bd9Sstevel@tonic-gate 		if (size < DDI_PROP_RESULT_OK) {
27357c478bd9Sstevel@tonic-gate 			ddi_prop_free(strs);
27367c478bd9Sstevel@tonic-gate 			switch (size) {
27377c478bd9Sstevel@tonic-gate 			case DDI_PROP_RESULT_EOF:
27387c478bd9Sstevel@tonic-gate 				return (DDI_PROP_END_OF_DATA);
27397c478bd9Sstevel@tonic-gate 
27407c478bd9Sstevel@tonic-gate 			case DDI_PROP_RESULT_ERROR:
27417c478bd9Sstevel@tonic-gate 				return (DDI_PROP_CANNOT_DECODE);
27427c478bd9Sstevel@tonic-gate 			}
27437c478bd9Sstevel@tonic-gate 		}
27447c478bd9Sstevel@tonic-gate 
27457c478bd9Sstevel@tonic-gate 		*tmp = ptr;
27467c478bd9Sstevel@tonic-gate 		ptr += size;
27477c478bd9Sstevel@tonic-gate 	}
27487c478bd9Sstevel@tonic-gate 
27497c478bd9Sstevel@tonic-gate 	/*
27507c478bd9Sstevel@tonic-gate 	 * String array is terminated by a NULL
27517c478bd9Sstevel@tonic-gate 	 */
27527c478bd9Sstevel@tonic-gate 	*tmp = NULL;
27537c478bd9Sstevel@tonic-gate 
27547c478bd9Sstevel@tonic-gate 	/*
27557c478bd9Sstevel@tonic-gate 	 * Finally, we can decode each string
27567c478bd9Sstevel@tonic-gate 	 */
27577c478bd9Sstevel@tonic-gate 	ddi_prop_reset_pos(ph);
27587c478bd9Sstevel@tonic-gate 	for (tmp = strs, n = 0; n < cnt; n++, tmp++) {
27597c478bd9Sstevel@tonic-gate 		i = DDI_PROP_STR(ph, DDI_PROP_CMD_DECODE, *tmp);
27607c478bd9Sstevel@tonic-gate 		if (i < DDI_PROP_RESULT_OK) {
27617c478bd9Sstevel@tonic-gate 			/*
27627c478bd9Sstevel@tonic-gate 			 * Free the space we just allocated
27637c478bd9Sstevel@tonic-gate 			 * and return an error
27647c478bd9Sstevel@tonic-gate 			 */
27657c478bd9Sstevel@tonic-gate 			ddi_prop_free(strs);
27667c478bd9Sstevel@tonic-gate 			switch (i) {
27677c478bd9Sstevel@tonic-gate 			case DDI_PROP_RESULT_EOF:
27687c478bd9Sstevel@tonic-gate 				return (DDI_PROP_END_OF_DATA);
27697c478bd9Sstevel@tonic-gate 
27707c478bd9Sstevel@tonic-gate 			case DDI_PROP_RESULT_ERROR:
27717c478bd9Sstevel@tonic-gate 				return (DDI_PROP_CANNOT_DECODE);
27727c478bd9Sstevel@tonic-gate 			}
27737c478bd9Sstevel@tonic-gate 		}
27747c478bd9Sstevel@tonic-gate 	}
27757c478bd9Sstevel@tonic-gate 
27767c478bd9Sstevel@tonic-gate 	*(char ***)data = strs;
27777c478bd9Sstevel@tonic-gate 	*nelements = cnt;
27787c478bd9Sstevel@tonic-gate 
27797c478bd9Sstevel@tonic-gate 	return (DDI_PROP_SUCCESS);
27807c478bd9Sstevel@tonic-gate }
27817c478bd9Sstevel@tonic-gate 
27827c478bd9Sstevel@tonic-gate /*
27837c478bd9Sstevel@tonic-gate  * Encode a string.
27847c478bd9Sstevel@tonic-gate  */
27857c478bd9Sstevel@tonic-gate int
27867c478bd9Sstevel@tonic-gate ddi_prop_fm_encode_string(prop_handle_t *ph, void *data, uint_t nelements)
27877c478bd9Sstevel@tonic-gate {
27887c478bd9Sstevel@tonic-gate 	char		**tmp;
27897c478bd9Sstevel@tonic-gate 	int		size;
27907c478bd9Sstevel@tonic-gate 	int		i;
27917c478bd9Sstevel@tonic-gate 
27927c478bd9Sstevel@tonic-gate 	/*
27937c478bd9Sstevel@tonic-gate 	 * If there is no data, we cannot do anything
27947c478bd9Sstevel@tonic-gate 	 */
27957c478bd9Sstevel@tonic-gate 	if (nelements == 0)
27967c478bd9Sstevel@tonic-gate 		return (DDI_PROP_CANNOT_ENCODE);
27977c478bd9Sstevel@tonic-gate 
27987c478bd9Sstevel@tonic-gate 	/*
27997c478bd9Sstevel@tonic-gate 	 * Get the size of the encoded string.
28007c478bd9Sstevel@tonic-gate 	 */
28017c478bd9Sstevel@tonic-gate 	tmp = (char **)data;
28027c478bd9Sstevel@tonic-gate 	size = DDI_PROP_STR(ph, DDI_PROP_CMD_GET_ESIZE, *tmp);
28037c478bd9Sstevel@tonic-gate 	if (size < DDI_PROP_RESULT_OK) {
28047c478bd9Sstevel@tonic-gate 		switch (size) {
28057c478bd9Sstevel@tonic-gate 		case DDI_PROP_RESULT_EOF:
28067c478bd9Sstevel@tonic-gate 			return (DDI_PROP_END_OF_DATA);
28077c478bd9Sstevel@tonic-gate 
28087c478bd9Sstevel@tonic-gate 		case DDI_PROP_RESULT_ERROR:
28097c478bd9Sstevel@tonic-gate 			return (DDI_PROP_CANNOT_ENCODE);
28107c478bd9Sstevel@tonic-gate 		}
28117c478bd9Sstevel@tonic-gate 	}
28127c478bd9Sstevel@tonic-gate 
28137c478bd9Sstevel@tonic-gate 	/*
28147c478bd9Sstevel@tonic-gate 	 * Allocate space in the handle to store the encoded string.
28157c478bd9Sstevel@tonic-gate 	 */
28167c478bd9Sstevel@tonic-gate 	if (ddi_prop_encode_alloc(ph, size) != DDI_PROP_SUCCESS)
28177c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NO_MEMORY);
28187c478bd9Sstevel@tonic-gate 
28197c478bd9Sstevel@tonic-gate 	ddi_prop_reset_pos(ph);
28207c478bd9Sstevel@tonic-gate 
28217c478bd9Sstevel@tonic-gate 	/*
28227c478bd9Sstevel@tonic-gate 	 * Encode the string.
28237c478bd9Sstevel@tonic-gate 	 */
28247c478bd9Sstevel@tonic-gate 	tmp = (char **)data;
28257c478bd9Sstevel@tonic-gate 	i = DDI_PROP_STR(ph, DDI_PROP_CMD_ENCODE, *tmp);
28267c478bd9Sstevel@tonic-gate 	if (i < DDI_PROP_RESULT_OK) {
28277c478bd9Sstevel@tonic-gate 		switch (i) {
28287c478bd9Sstevel@tonic-gate 		case DDI_PROP_RESULT_EOF:
28297c478bd9Sstevel@tonic-gate 			return (DDI_PROP_END_OF_DATA);
28307c478bd9Sstevel@tonic-gate 
28317c478bd9Sstevel@tonic-gate 		case DDI_PROP_RESULT_ERROR:
28327c478bd9Sstevel@tonic-gate 			return (DDI_PROP_CANNOT_ENCODE);
28337c478bd9Sstevel@tonic-gate 		}
28347c478bd9Sstevel@tonic-gate 	}
28357c478bd9Sstevel@tonic-gate 
28367c478bd9Sstevel@tonic-gate 	return (DDI_PROP_SUCCESS);
28377c478bd9Sstevel@tonic-gate }
28387c478bd9Sstevel@tonic-gate 
28397c478bd9Sstevel@tonic-gate 
28407c478bd9Sstevel@tonic-gate /*
28417c478bd9Sstevel@tonic-gate  * Encode an array of strings.
28427c478bd9Sstevel@tonic-gate  */
28437c478bd9Sstevel@tonic-gate int
28447c478bd9Sstevel@tonic-gate ddi_prop_fm_encode_strings(prop_handle_t *ph, void *data, uint_t nelements)
28457c478bd9Sstevel@tonic-gate {
28467c478bd9Sstevel@tonic-gate 	int		cnt = 0;
28477c478bd9Sstevel@tonic-gate 	char		**tmp;
28487c478bd9Sstevel@tonic-gate 	int		size;
28497c478bd9Sstevel@tonic-gate 	uint_t		total_size;
28507c478bd9Sstevel@tonic-gate 	int		i;
28517c478bd9Sstevel@tonic-gate 
28527c478bd9Sstevel@tonic-gate 	/*
28537c478bd9Sstevel@tonic-gate 	 * If there is no data, we cannot do anything
28547c478bd9Sstevel@tonic-gate 	 */
28557c478bd9Sstevel@tonic-gate 	if (nelements == 0)
28567c478bd9Sstevel@tonic-gate 		return (DDI_PROP_CANNOT_ENCODE);
28577c478bd9Sstevel@tonic-gate 
28587c478bd9Sstevel@tonic-gate 	/*
28597c478bd9Sstevel@tonic-gate 	 * Get the total size required to encode all the strings.
28607c478bd9Sstevel@tonic-gate 	 */
28617c478bd9Sstevel@tonic-gate 	total_size = 0;
28627c478bd9Sstevel@tonic-gate 	tmp = (char **)data;
28637c478bd9Sstevel@tonic-gate 	for (cnt = 0; cnt < nelements; cnt++, tmp++) {
28647c478bd9Sstevel@tonic-gate 		size = DDI_PROP_STR(ph, DDI_PROP_CMD_GET_ESIZE, *tmp);
28657c478bd9Sstevel@tonic-gate 		if (size < DDI_PROP_RESULT_OK) {
28667c478bd9Sstevel@tonic-gate 			switch (size) {
28677c478bd9Sstevel@tonic-gate 			case DDI_PROP_RESULT_EOF:
28687c478bd9Sstevel@tonic-gate 				return (DDI_PROP_END_OF_DATA);
28697c478bd9Sstevel@tonic-gate 
28707c478bd9Sstevel@tonic-gate 			case DDI_PROP_RESULT_ERROR:
28717c478bd9Sstevel@tonic-gate 				return (DDI_PROP_CANNOT_ENCODE);
28727c478bd9Sstevel@tonic-gate 			}
28737c478bd9Sstevel@tonic-gate 		}
28747c478bd9Sstevel@tonic-gate 		total_size += (uint_t)size;
28757c478bd9Sstevel@tonic-gate 	}
28767c478bd9Sstevel@tonic-gate 
28777c478bd9Sstevel@tonic-gate 	/*
28787c478bd9Sstevel@tonic-gate 	 * Allocate space in the handle to store the encoded strings.
28797c478bd9Sstevel@tonic-gate 	 */
28807c478bd9Sstevel@tonic-gate 	if (ddi_prop_encode_alloc(ph, total_size) != DDI_PROP_SUCCESS)
28817c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NO_MEMORY);
28827c478bd9Sstevel@tonic-gate 
28837c478bd9Sstevel@tonic-gate 	ddi_prop_reset_pos(ph);
28847c478bd9Sstevel@tonic-gate 
28857c478bd9Sstevel@tonic-gate 	/*
28867c478bd9Sstevel@tonic-gate 	 * Encode the array of strings.
28877c478bd9Sstevel@tonic-gate 	 */
28887c478bd9Sstevel@tonic-gate 	tmp = (char **)data;
28897c478bd9Sstevel@tonic-gate 	for (cnt = 0; cnt < nelements; cnt++, tmp++) {
28907c478bd9Sstevel@tonic-gate 		i = DDI_PROP_STR(ph, DDI_PROP_CMD_ENCODE, *tmp);
28917c478bd9Sstevel@tonic-gate 		if (i < DDI_PROP_RESULT_OK) {
28927c478bd9Sstevel@tonic-gate 			switch (i) {
28937c478bd9Sstevel@tonic-gate 			case DDI_PROP_RESULT_EOF:
28947c478bd9Sstevel@tonic-gate 				return (DDI_PROP_END_OF_DATA);
28957c478bd9Sstevel@tonic-gate 
28967c478bd9Sstevel@tonic-gate 			case DDI_PROP_RESULT_ERROR:
28977c478bd9Sstevel@tonic-gate 				return (DDI_PROP_CANNOT_ENCODE);
28987c478bd9Sstevel@tonic-gate 			}
28997c478bd9Sstevel@tonic-gate 		}
29007c478bd9Sstevel@tonic-gate 	}
29017c478bd9Sstevel@tonic-gate 
29027c478bd9Sstevel@tonic-gate 	return (DDI_PROP_SUCCESS);
29037c478bd9Sstevel@tonic-gate }
29047c478bd9Sstevel@tonic-gate 
29057c478bd9Sstevel@tonic-gate 
29067c478bd9Sstevel@tonic-gate /*
29077c478bd9Sstevel@tonic-gate  * Decode an array of bytes.
29087c478bd9Sstevel@tonic-gate  */
29097c478bd9Sstevel@tonic-gate static int
29107c478bd9Sstevel@tonic-gate ddi_prop_fm_decode_bytes(prop_handle_t *ph, void *data, uint_t *nelements)
29117c478bd9Sstevel@tonic-gate {
29127c478bd9Sstevel@tonic-gate 	uchar_t		*tmp;
29137c478bd9Sstevel@tonic-gate 	int		nbytes;
29147c478bd9Sstevel@tonic-gate 	int		i;
29157c478bd9Sstevel@tonic-gate 
29167c478bd9Sstevel@tonic-gate 	/*
29177c478bd9Sstevel@tonic-gate 	 * If there are no elements return an error
29187c478bd9Sstevel@tonic-gate 	 */
29197c478bd9Sstevel@tonic-gate 	if (ph->ph_size == 0)
29207c478bd9Sstevel@tonic-gate 		return (DDI_PROP_END_OF_DATA);
29217c478bd9Sstevel@tonic-gate 
29227c478bd9Sstevel@tonic-gate 	/*
29237c478bd9Sstevel@tonic-gate 	 * Get the size of the encoded array of bytes.
29247c478bd9Sstevel@tonic-gate 	 */
29257c478bd9Sstevel@tonic-gate 	nbytes = DDI_PROP_BYTES(ph, DDI_PROP_CMD_GET_DSIZE,
2926184cd04cScth 	    data, ph->ph_size);
29277c478bd9Sstevel@tonic-gate 	if (nbytes < DDI_PROP_RESULT_OK) {
29287c478bd9Sstevel@tonic-gate 		switch (nbytes) {
29297c478bd9Sstevel@tonic-gate 		case DDI_PROP_RESULT_EOF:
29307c478bd9Sstevel@tonic-gate 			return (DDI_PROP_END_OF_DATA);
29317c478bd9Sstevel@tonic-gate 
29327c478bd9Sstevel@tonic-gate 		case DDI_PROP_RESULT_ERROR:
29337c478bd9Sstevel@tonic-gate 			return (DDI_PROP_CANNOT_DECODE);
29347c478bd9Sstevel@tonic-gate 		}
29357c478bd9Sstevel@tonic-gate 	}
29367c478bd9Sstevel@tonic-gate 
29377c478bd9Sstevel@tonic-gate 	/*
29387c478bd9Sstevel@tonic-gate 	 * Allocated memory to store the decoded value in.
29397c478bd9Sstevel@tonic-gate 	 */
29407c478bd9Sstevel@tonic-gate 	tmp = ddi_prop_decode_alloc(nbytes, ddi_prop_free_bytes);
29417c478bd9Sstevel@tonic-gate 
29427c478bd9Sstevel@tonic-gate 	/*
29437c478bd9Sstevel@tonic-gate 	 * Decode each element and place it in the space we just allocated
29447c478bd9Sstevel@tonic-gate 	 */
29457c478bd9Sstevel@tonic-gate 	i = DDI_PROP_BYTES(ph, DDI_PROP_CMD_DECODE, tmp, nbytes);
29467c478bd9Sstevel@tonic-gate 	if (i < DDI_PROP_RESULT_OK) {
29477c478bd9Sstevel@tonic-gate 		/*
29487c478bd9Sstevel@tonic-gate 		 * Free the space we just allocated
29497c478bd9Sstevel@tonic-gate 		 * and return an error
29507c478bd9Sstevel@tonic-gate 		 */
29517c478bd9Sstevel@tonic-gate 		ddi_prop_free(tmp);
29527c478bd9Sstevel@tonic-gate 		switch (i) {
29537c478bd9Sstevel@tonic-gate 		case DDI_PROP_RESULT_EOF:
29547c478bd9Sstevel@tonic-gate 			return (DDI_PROP_END_OF_DATA);
29557c478bd9Sstevel@tonic-gate 
29567c478bd9Sstevel@tonic-gate 		case DDI_PROP_RESULT_ERROR:
29577c478bd9Sstevel@tonic-gate 			return (DDI_PROP_CANNOT_DECODE);
29587c478bd9Sstevel@tonic-gate 		}
29597c478bd9Sstevel@tonic-gate 	}
29607c478bd9Sstevel@tonic-gate 
29617c478bd9Sstevel@tonic-gate 	*(uchar_t **)data = tmp;
29627c478bd9Sstevel@tonic-gate 	*nelements = nbytes;
29637c478bd9Sstevel@tonic-gate 
29647c478bd9Sstevel@tonic-gate 	return (DDI_PROP_SUCCESS);
29657c478bd9Sstevel@tonic-gate }
29667c478bd9Sstevel@tonic-gate 
29677c478bd9Sstevel@tonic-gate /*
29687c478bd9Sstevel@tonic-gate  * Encode an array of bytes.
29697c478bd9Sstevel@tonic-gate  */
29707c478bd9Sstevel@tonic-gate int
29717c478bd9Sstevel@tonic-gate ddi_prop_fm_encode_bytes(prop_handle_t *ph, void *data, uint_t nelements)
29727c478bd9Sstevel@tonic-gate {
29737c478bd9Sstevel@tonic-gate 	int		size;
29747c478bd9Sstevel@tonic-gate 	int		i;
29757c478bd9Sstevel@tonic-gate 
29767c478bd9Sstevel@tonic-gate 	/*
29777c478bd9Sstevel@tonic-gate 	 * If there are no elements, then this is a boolean property,
29787c478bd9Sstevel@tonic-gate 	 * so just create a property handle with no data and return.
29797c478bd9Sstevel@tonic-gate 	 */
29807c478bd9Sstevel@tonic-gate 	if (nelements == 0) {
29817c478bd9Sstevel@tonic-gate 		(void) ddi_prop_encode_alloc(ph, 0);
29827c478bd9Sstevel@tonic-gate 		return (DDI_PROP_SUCCESS);
29837c478bd9Sstevel@tonic-gate 	}
29847c478bd9Sstevel@tonic-gate 
29857c478bd9Sstevel@tonic-gate 	/*
29867c478bd9Sstevel@tonic-gate 	 * Get the size of the encoded array of bytes.
29877c478bd9Sstevel@tonic-gate 	 */
29887c478bd9Sstevel@tonic-gate 	size = DDI_PROP_BYTES(ph, DDI_PROP_CMD_GET_ESIZE, (uchar_t *)data,
2989184cd04cScth 	    nelements);
29907c478bd9Sstevel@tonic-gate 	if (size < DDI_PROP_RESULT_OK) {
29917c478bd9Sstevel@tonic-gate 		switch (size) {
29927c478bd9Sstevel@tonic-gate 		case DDI_PROP_RESULT_EOF:
29937c478bd9Sstevel@tonic-gate 			return (DDI_PROP_END_OF_DATA);
29947c478bd9Sstevel@tonic-gate 
29957c478bd9Sstevel@tonic-gate 		case DDI_PROP_RESULT_ERROR:
29967c478bd9Sstevel@tonic-gate 			return (DDI_PROP_CANNOT_DECODE);
29977c478bd9Sstevel@tonic-gate 		}
29987c478bd9Sstevel@tonic-gate 	}
29997c478bd9Sstevel@tonic-gate 
30007c478bd9Sstevel@tonic-gate 	/*
30017c478bd9Sstevel@tonic-gate 	 * Allocate space in the handle to store the encoded bytes.
30027c478bd9Sstevel@tonic-gate 	 */
30037c478bd9Sstevel@tonic-gate 	if (ddi_prop_encode_alloc(ph, (uint_t)size) != DDI_PROP_SUCCESS)
30047c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NO_MEMORY);
30057c478bd9Sstevel@tonic-gate 
30067c478bd9Sstevel@tonic-gate 	/*
30077c478bd9Sstevel@tonic-gate 	 * Encode the array of bytes.
30087c478bd9Sstevel@tonic-gate 	 */
30097c478bd9Sstevel@tonic-gate 	i = DDI_PROP_BYTES(ph, DDI_PROP_CMD_ENCODE, (uchar_t *)data,
3010184cd04cScth 	    nelements);
30117c478bd9Sstevel@tonic-gate 	if (i < DDI_PROP_RESULT_OK) {
30127c478bd9Sstevel@tonic-gate 		switch (i) {
30137c478bd9Sstevel@tonic-gate 		case DDI_PROP_RESULT_EOF:
30147c478bd9Sstevel@tonic-gate 			return (DDI_PROP_END_OF_DATA);
30157c478bd9Sstevel@tonic-gate 
30167c478bd9Sstevel@tonic-gate 		case DDI_PROP_RESULT_ERROR:
30177c478bd9Sstevel@tonic-gate 			return (DDI_PROP_CANNOT_ENCODE);
30187c478bd9Sstevel@tonic-gate 		}
30197c478bd9Sstevel@tonic-gate 	}
30207c478bd9Sstevel@tonic-gate 
30217c478bd9Sstevel@tonic-gate 	return (DDI_PROP_SUCCESS);
30227c478bd9Sstevel@tonic-gate }
30237c478bd9Sstevel@tonic-gate 
30247c478bd9Sstevel@tonic-gate /*
30257c478bd9Sstevel@tonic-gate  * OBP 1275 integer, string and byte operators.
30267c478bd9Sstevel@tonic-gate  *
30277c478bd9Sstevel@tonic-gate  * DDI_PROP_CMD_DECODE:
30287c478bd9Sstevel@tonic-gate  *
30297c478bd9Sstevel@tonic-gate  *	DDI_PROP_RESULT_ERROR:		cannot decode the data
30307c478bd9Sstevel@tonic-gate  *	DDI_PROP_RESULT_EOF:		end of data
30317c478bd9Sstevel@tonic-gate  *	DDI_PROP_OK:			data was decoded
30327c478bd9Sstevel@tonic-gate  *
30337c478bd9Sstevel@tonic-gate  * DDI_PROP_CMD_ENCODE:
30347c478bd9Sstevel@tonic-gate  *
30357c478bd9Sstevel@tonic-gate  *	DDI_PROP_RESULT_ERROR:		cannot encode the data
30367c478bd9Sstevel@tonic-gate  *	DDI_PROP_RESULT_EOF:		end of data
30377c478bd9Sstevel@tonic-gate  *	DDI_PROP_OK:			data was encoded
30387c478bd9Sstevel@tonic-gate  *
30397c478bd9Sstevel@tonic-gate  * DDI_PROP_CMD_SKIP:
30407c478bd9Sstevel@tonic-gate  *
30417c478bd9Sstevel@tonic-gate  *	DDI_PROP_RESULT_ERROR:		cannot skip the data
30427c478bd9Sstevel@tonic-gate  *	DDI_PROP_RESULT_EOF:		end of data
30437c478bd9Sstevel@tonic-gate  *	DDI_PROP_OK:			data was skipped
30447c478bd9Sstevel@tonic-gate  *
30457c478bd9Sstevel@tonic-gate  * DDI_PROP_CMD_GET_ESIZE:
30467c478bd9Sstevel@tonic-gate  *
30477c478bd9Sstevel@tonic-gate  *	DDI_PROP_RESULT_ERROR:		cannot get encoded size
30487c478bd9Sstevel@tonic-gate  *	DDI_PROP_RESULT_EOF:		end of data
30497c478bd9Sstevel@tonic-gate  *	> 0:				the encoded size
30507c478bd9Sstevel@tonic-gate  *
30517c478bd9Sstevel@tonic-gate  * DDI_PROP_CMD_GET_DSIZE:
30527c478bd9Sstevel@tonic-gate  *
30537c478bd9Sstevel@tonic-gate  *	DDI_PROP_RESULT_ERROR:		cannot get decoded size
30547c478bd9Sstevel@tonic-gate  *	DDI_PROP_RESULT_EOF:		end of data
30557c478bd9Sstevel@tonic-gate  *	> 0:				the decoded size
30567c478bd9Sstevel@tonic-gate  */
30577c478bd9Sstevel@tonic-gate 
30587c478bd9Sstevel@tonic-gate /*
30597c478bd9Sstevel@tonic-gate  * OBP 1275 integer operator
30607c478bd9Sstevel@tonic-gate  *
30617c478bd9Sstevel@tonic-gate  * OBP properties are a byte stream of data, so integers may not be
30627c478bd9Sstevel@tonic-gate  * properly aligned.  Therefore we need to copy them one byte at a time.
30637c478bd9Sstevel@tonic-gate  */
30647c478bd9Sstevel@tonic-gate int
30657c478bd9Sstevel@tonic-gate ddi_prop_1275_int(prop_handle_t *ph, uint_t cmd, int *data)
30667c478bd9Sstevel@tonic-gate {
30677c478bd9Sstevel@tonic-gate 	int	i;
30687c478bd9Sstevel@tonic-gate 
30697c478bd9Sstevel@tonic-gate 	switch (cmd) {
30707c478bd9Sstevel@tonic-gate 	case DDI_PROP_CMD_DECODE:
30717c478bd9Sstevel@tonic-gate 		/*
30727c478bd9Sstevel@tonic-gate 		 * Check that there is encoded data
30737c478bd9Sstevel@tonic-gate 		 */
30747c478bd9Sstevel@tonic-gate 		if (ph->ph_cur_pos == NULL || ph->ph_size == 0)
30757c478bd9Sstevel@tonic-gate 			return (DDI_PROP_RESULT_ERROR);
30767c478bd9Sstevel@tonic-gate 		if (ph->ph_flags & PH_FROM_PROM) {
30777c478bd9Sstevel@tonic-gate 			i = MIN(ph->ph_size, PROP_1275_INT_SIZE);
30787c478bd9Sstevel@tonic-gate 			if ((int *)ph->ph_cur_pos > ((int *)ph->ph_data +
3079184cd04cScth 			    ph->ph_size - i))
30807c478bd9Sstevel@tonic-gate 				return (DDI_PROP_RESULT_ERROR);
30817c478bd9Sstevel@tonic-gate 		} else {
30827c478bd9Sstevel@tonic-gate 			if (ph->ph_size < sizeof (int) ||
3083184cd04cScth 			    ((int *)ph->ph_cur_pos > ((int *)ph->ph_data +
3084184cd04cScth 			    ph->ph_size - sizeof (int))))
3085184cd04cScth 				return (DDI_PROP_RESULT_ERROR);
30867c478bd9Sstevel@tonic-gate 		}
30877c478bd9Sstevel@tonic-gate 
30887c478bd9Sstevel@tonic-gate 		/*
30897c478bd9Sstevel@tonic-gate 		 * Copy the integer, using the implementation-specific
30907c478bd9Sstevel@tonic-gate 		 * copy function if the property is coming from the PROM.
30917c478bd9Sstevel@tonic-gate 		 */
30927c478bd9Sstevel@tonic-gate 		if (ph->ph_flags & PH_FROM_PROM) {
30937c478bd9Sstevel@tonic-gate 			*data = impl_ddi_prop_int_from_prom(
3094184cd04cScth 			    (uchar_t *)ph->ph_cur_pos,
3095184cd04cScth 			    (ph->ph_size < PROP_1275_INT_SIZE) ?
3096184cd04cScth 			    ph->ph_size : PROP_1275_INT_SIZE);
30977c478bd9Sstevel@tonic-gate 		} else {
30987c478bd9Sstevel@tonic-gate 			bcopy(ph->ph_cur_pos, data, sizeof (int));
30997c478bd9Sstevel@tonic-gate 		}
31007c478bd9Sstevel@tonic-gate 
31017c478bd9Sstevel@tonic-gate 		/*
31027c478bd9Sstevel@tonic-gate 		 * Move the current location to the start of the next
31037c478bd9Sstevel@tonic-gate 		 * bit of undecoded data.
31047c478bd9Sstevel@tonic-gate 		 */
31057c478bd9Sstevel@tonic-gate 		ph->ph_cur_pos = (uchar_t *)ph->ph_cur_pos +
3106184cd04cScth 		    PROP_1275_INT_SIZE;
31077c478bd9Sstevel@tonic-gate 		return (DDI_PROP_RESULT_OK);
31087c478bd9Sstevel@tonic-gate 
31097c478bd9Sstevel@tonic-gate 	case DDI_PROP_CMD_ENCODE:
31107c478bd9Sstevel@tonic-gate 		/*
31117c478bd9Sstevel@tonic-gate 		 * Check that there is room to encoded the data
31127c478bd9Sstevel@tonic-gate 		 */
31137c478bd9Sstevel@tonic-gate 		if (ph->ph_cur_pos == NULL || ph->ph_size == 0 ||
3114184cd04cScth 		    ph->ph_size < PROP_1275_INT_SIZE ||
3115184cd04cScth 		    ((int *)ph->ph_cur_pos > ((int *)ph->ph_data +
3116184cd04cScth 		    ph->ph_size - sizeof (int))))
31177c478bd9Sstevel@tonic-gate 			return (DDI_PROP_RESULT_ERROR);
31187c478bd9Sstevel@tonic-gate 
31197c478bd9Sstevel@tonic-gate 		/*
31207c478bd9Sstevel@tonic-gate 		 * Encode the integer into the byte stream one byte at a
31217c478bd9Sstevel@tonic-gate 		 * time.
31227c478bd9Sstevel@tonic-gate 		 */
31237c478bd9Sstevel@tonic-gate 		bcopy(data, ph->ph_cur_pos, sizeof (int));
31247c478bd9Sstevel@tonic-gate 
31257c478bd9Sstevel@tonic-gate 		/*
31267c478bd9Sstevel@tonic-gate 		 * Move the current location to the start of the next bit of
31277c478bd9Sstevel@tonic-gate 		 * space where we can store encoded data.
31287c478bd9Sstevel@tonic-gate 		 */
31297c478bd9Sstevel@tonic-gate 		ph->ph_cur_pos = (uchar_t *)ph->ph_cur_pos + PROP_1275_INT_SIZE;
31307c478bd9Sstevel@tonic-gate 		return (DDI_PROP_RESULT_OK);
31317c478bd9Sstevel@tonic-gate 
31327c478bd9Sstevel@tonic-gate 	case DDI_PROP_CMD_SKIP:
31337c478bd9Sstevel@tonic-gate 		/*
31347c478bd9Sstevel@tonic-gate 		 * Check that there is encoded data
31357c478bd9Sstevel@tonic-gate 		 */
31367c478bd9Sstevel@tonic-gate 		if (ph->ph_cur_pos == NULL || ph->ph_size == 0 ||
3137184cd04cScth 		    ph->ph_size < PROP_1275_INT_SIZE)
31387c478bd9Sstevel@tonic-gate 			return (DDI_PROP_RESULT_ERROR);
31397c478bd9Sstevel@tonic-gate 
31407c478bd9Sstevel@tonic-gate 
31417c478bd9Sstevel@tonic-gate 		if ((caddr_t)ph->ph_cur_pos ==
3142184cd04cScth 		    (caddr_t)ph->ph_data + ph->ph_size) {
31437c478bd9Sstevel@tonic-gate 			return (DDI_PROP_RESULT_EOF);
31447c478bd9Sstevel@tonic-gate 		} else if ((caddr_t)ph->ph_cur_pos >
3145184cd04cScth 		    (caddr_t)ph->ph_data + ph->ph_size) {
31467c478bd9Sstevel@tonic-gate 			return (DDI_PROP_RESULT_EOF);
31477c478bd9Sstevel@tonic-gate 		}
31487c478bd9Sstevel@tonic-gate 
31497c478bd9Sstevel@tonic-gate 		/*
31507c478bd9Sstevel@tonic-gate 		 * Move the current location to the start of the next bit of
31517c478bd9Sstevel@tonic-gate 		 * undecoded data.
31527c478bd9Sstevel@tonic-gate 		 */
31537c478bd9Sstevel@tonic-gate 		ph->ph_cur_pos = (uchar_t *)ph->ph_cur_pos + PROP_1275_INT_SIZE;
31547c478bd9Sstevel@tonic-gate 		return (DDI_PROP_RESULT_OK);
31557c478bd9Sstevel@tonic-gate 
31567c478bd9Sstevel@tonic-gate 	case DDI_PROP_CMD_GET_ESIZE:
31577c478bd9Sstevel@tonic-gate 		/*
31587c478bd9Sstevel@tonic-gate 		 * Return the size of an encoded integer on OBP
31597c478bd9Sstevel@tonic-gate 		 */
31607c478bd9Sstevel@tonic-gate 		return (PROP_1275_INT_SIZE);
31617c478bd9Sstevel@tonic-gate 
31627c478bd9Sstevel@tonic-gate 	case DDI_PROP_CMD_GET_DSIZE:
31637c478bd9Sstevel@tonic-gate 		/*
31647c478bd9Sstevel@tonic-gate 		 * Return the size of a decoded integer on the system.
31657c478bd9Sstevel@tonic-gate 		 */
31667c478bd9Sstevel@tonic-gate 		return (sizeof (int));
31677c478bd9Sstevel@tonic-gate 
31687c478bd9Sstevel@tonic-gate 	default:
31697c478bd9Sstevel@tonic-gate #ifdef DEBUG
31707c478bd9Sstevel@tonic-gate 		panic("ddi_prop_1275_int: %x impossible", cmd);
31717c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
31727c478bd9Sstevel@tonic-gate #else
31737c478bd9Sstevel@tonic-gate 		return (DDI_PROP_RESULT_ERROR);
31747c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
31757c478bd9Sstevel@tonic-gate 	}
31767c478bd9Sstevel@tonic-gate }
31777c478bd9Sstevel@tonic-gate 
31787c478bd9Sstevel@tonic-gate /*
31797c478bd9Sstevel@tonic-gate  * 64 bit integer operator.
31807c478bd9Sstevel@tonic-gate  *
31817c478bd9Sstevel@tonic-gate  * This is an extension, defined by Sun, to the 1275 integer
31827c478bd9Sstevel@tonic-gate  * operator.  This routine handles the encoding/decoding of
31837c478bd9Sstevel@tonic-gate  * 64 bit integer properties.
31847c478bd9Sstevel@tonic-gate  */
31857c478bd9Sstevel@tonic-gate int
31867c478bd9Sstevel@tonic-gate ddi_prop_int64_op(prop_handle_t *ph, uint_t cmd, int64_t *data)
31877c478bd9Sstevel@tonic-gate {
31887c478bd9Sstevel@tonic-gate 
31897c478bd9Sstevel@tonic-gate 	switch (cmd) {
31907c478bd9Sstevel@tonic-gate 	case DDI_PROP_CMD_DECODE:
31917c478bd9Sstevel@tonic-gate 		/*
31927c478bd9Sstevel@tonic-gate 		 * Check that there is encoded data
31937c478bd9Sstevel@tonic-gate 		 */
31947c478bd9Sstevel@tonic-gate 		if (ph->ph_cur_pos == NULL || ph->ph_size == 0)
31957c478bd9Sstevel@tonic-gate 			return (DDI_PROP_RESULT_ERROR);
31967c478bd9Sstevel@tonic-gate 		if (ph->ph_flags & PH_FROM_PROM) {
31977c478bd9Sstevel@tonic-gate 			return (DDI_PROP_RESULT_ERROR);
31987c478bd9Sstevel@tonic-gate 		} else {
31997c478bd9Sstevel@tonic-gate 			if (ph->ph_size < sizeof (int64_t) ||
32007c478bd9Sstevel@tonic-gate 			    ((int64_t *)ph->ph_cur_pos >
32017c478bd9Sstevel@tonic-gate 			    ((int64_t *)ph->ph_data +
32027c478bd9Sstevel@tonic-gate 			    ph->ph_size - sizeof (int64_t))))
32037c478bd9Sstevel@tonic-gate 				return (DDI_PROP_RESULT_ERROR);
32047c478bd9Sstevel@tonic-gate 		}
32057c478bd9Sstevel@tonic-gate 		/*
32067c478bd9Sstevel@tonic-gate 		 * Copy the integer, using the implementation-specific
32077c478bd9Sstevel@tonic-gate 		 * copy function if the property is coming from the PROM.
32087c478bd9Sstevel@tonic-gate 		 */
32097c478bd9Sstevel@tonic-gate 		if (ph->ph_flags & PH_FROM_PROM) {
32107c478bd9Sstevel@tonic-gate 			return (DDI_PROP_RESULT_ERROR);
32117c478bd9Sstevel@tonic-gate 		} else {
32127c478bd9Sstevel@tonic-gate 			bcopy(ph->ph_cur_pos, data, sizeof (int64_t));
32137c478bd9Sstevel@tonic-gate 		}
32147c478bd9Sstevel@tonic-gate 
32157c478bd9Sstevel@tonic-gate 		/*
32167c478bd9Sstevel@tonic-gate 		 * Move the current location to the start of the next
32177c478bd9Sstevel@tonic-gate 		 * bit of undecoded data.
32187c478bd9Sstevel@tonic-gate 		 */
32197c478bd9Sstevel@tonic-gate 		ph->ph_cur_pos = (uchar_t *)ph->ph_cur_pos +
32207c478bd9Sstevel@tonic-gate 		    sizeof (int64_t);
32217c478bd9Sstevel@tonic-gate 			return (DDI_PROP_RESULT_OK);
32227c478bd9Sstevel@tonic-gate 
32237c478bd9Sstevel@tonic-gate 	case DDI_PROP_CMD_ENCODE:
32247c478bd9Sstevel@tonic-gate 		/*
32257c478bd9Sstevel@tonic-gate 		 * Check that there is room to encoded the data
32267c478bd9Sstevel@tonic-gate 		 */
32277c478bd9Sstevel@tonic-gate 		if (ph->ph_cur_pos == NULL || ph->ph_size == 0 ||
32287c478bd9Sstevel@tonic-gate 		    ph->ph_size < sizeof (int64_t) ||
32297c478bd9Sstevel@tonic-gate 		    ((int64_t *)ph->ph_cur_pos > ((int64_t *)ph->ph_data +
32307c478bd9Sstevel@tonic-gate 		    ph->ph_size - sizeof (int64_t))))
32317c478bd9Sstevel@tonic-gate 			return (DDI_PROP_RESULT_ERROR);
32327c478bd9Sstevel@tonic-gate 
32337c478bd9Sstevel@tonic-gate 		/*
32347c478bd9Sstevel@tonic-gate 		 * Encode the integer into the byte stream one byte at a
32357c478bd9Sstevel@tonic-gate 		 * time.
32367c478bd9Sstevel@tonic-gate 		 */
32377c478bd9Sstevel@tonic-gate 		bcopy(data, ph->ph_cur_pos, sizeof (int64_t));
32387c478bd9Sstevel@tonic-gate 
32397c478bd9Sstevel@tonic-gate 		/*
32407c478bd9Sstevel@tonic-gate 		 * Move the current location to the start of the next bit of
32417c478bd9Sstevel@tonic-gate 		 * space where we can store encoded data.
32427c478bd9Sstevel@tonic-gate 		 */
32437c478bd9Sstevel@tonic-gate 		ph->ph_cur_pos = (uchar_t *)ph->ph_cur_pos +
32447c478bd9Sstevel@tonic-gate 		    sizeof (int64_t);
32457c478bd9Sstevel@tonic-gate 		return (DDI_PROP_RESULT_OK);
32467c478bd9Sstevel@tonic-gate 
32477c478bd9Sstevel@tonic-gate 	case DDI_PROP_CMD_SKIP:
32487c478bd9Sstevel@tonic-gate 		/*
32497c478bd9Sstevel@tonic-gate 		 * Check that there is encoded data
32507c478bd9Sstevel@tonic-gate 		 */
32517c478bd9Sstevel@tonic-gate 		if (ph->ph_cur_pos == NULL || ph->ph_size == 0 ||
32527c478bd9Sstevel@tonic-gate 		    ph->ph_size < sizeof (int64_t))
32537c478bd9Sstevel@tonic-gate 			return (DDI_PROP_RESULT_ERROR);
32547c478bd9Sstevel@tonic-gate 
32557c478bd9Sstevel@tonic-gate 		if ((caddr_t)ph->ph_cur_pos ==
32567c478bd9Sstevel@tonic-gate 		    (caddr_t)ph->ph_data + ph->ph_size) {
32577c478bd9Sstevel@tonic-gate 			return (DDI_PROP_RESULT_EOF);
32587c478bd9Sstevel@tonic-gate 		} else if ((caddr_t)ph->ph_cur_pos >
32597c478bd9Sstevel@tonic-gate 		    (caddr_t)ph->ph_data + ph->ph_size) {
32607c478bd9Sstevel@tonic-gate 			return (DDI_PROP_RESULT_EOF);
32617c478bd9Sstevel@tonic-gate 		}
32627c478bd9Sstevel@tonic-gate 
32637c478bd9Sstevel@tonic-gate 		/*
32647c478bd9Sstevel@tonic-gate 		 * Move the current location to the start of
32657c478bd9Sstevel@tonic-gate 		 * the next bit of undecoded data.
32667c478bd9Sstevel@tonic-gate 		 */
32677c478bd9Sstevel@tonic-gate 		ph->ph_cur_pos = (uchar_t *)ph->ph_cur_pos +
32687c478bd9Sstevel@tonic-gate 		    sizeof (int64_t);
32697c478bd9Sstevel@tonic-gate 			return (DDI_PROP_RESULT_OK);
32707c478bd9Sstevel@tonic-gate 
32717c478bd9Sstevel@tonic-gate 	case DDI_PROP_CMD_GET_ESIZE:
32727c478bd9Sstevel@tonic-gate 		/*
32737c478bd9Sstevel@tonic-gate 		 * Return the size of an encoded integer on OBP
32747c478bd9Sstevel@tonic-gate 		 */
32757c478bd9Sstevel@tonic-gate 		return (sizeof (int64_t));
32767c478bd9Sstevel@tonic-gate 
32777c478bd9Sstevel@tonic-gate 	case DDI_PROP_CMD_GET_DSIZE:
32787c478bd9Sstevel@tonic-gate 		/*
32797c478bd9Sstevel@tonic-gate 		 * Return the size of a decoded integer on the system.
32807c478bd9Sstevel@tonic-gate 		 */
32817c478bd9Sstevel@tonic-gate 		return (sizeof (int64_t));
32827c478bd9Sstevel@tonic-gate 
32837c478bd9Sstevel@tonic-gate 	default:
32847c478bd9Sstevel@tonic-gate #ifdef DEBUG
32857c478bd9Sstevel@tonic-gate 		panic("ddi_prop_int64_op: %x impossible", cmd);
32867c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
32877c478bd9Sstevel@tonic-gate #else
32887c478bd9Sstevel@tonic-gate 		return (DDI_PROP_RESULT_ERROR);
32897c478bd9Sstevel@tonic-gate #endif  /* DEBUG */
32907c478bd9Sstevel@tonic-gate 	}
32917c478bd9Sstevel@tonic-gate }
32927c478bd9Sstevel@tonic-gate 
32937c478bd9Sstevel@tonic-gate /*
32947c478bd9Sstevel@tonic-gate  * OBP 1275 string operator.
32957c478bd9Sstevel@tonic-gate  *
32967c478bd9Sstevel@tonic-gate  * OBP strings are NULL terminated.
32977c478bd9Sstevel@tonic-gate  */
32987c478bd9Sstevel@tonic-gate int
32997c478bd9Sstevel@tonic-gate ddi_prop_1275_string(prop_handle_t *ph, uint_t cmd, char *data)
33007c478bd9Sstevel@tonic-gate {
33017c478bd9Sstevel@tonic-gate 	int	n;
33027c478bd9Sstevel@tonic-gate 	char	*p;
33037c478bd9Sstevel@tonic-gate 	char	*end;
33047c478bd9Sstevel@tonic-gate 
33057c478bd9Sstevel@tonic-gate 	switch (cmd) {
33067c478bd9Sstevel@tonic-gate 	case DDI_PROP_CMD_DECODE:
33077c478bd9Sstevel@tonic-gate 		/*
33087c478bd9Sstevel@tonic-gate 		 * Check that there is encoded data
33097c478bd9Sstevel@tonic-gate 		 */
33107c478bd9Sstevel@tonic-gate 		if (ph->ph_cur_pos == NULL || ph->ph_size == 0) {
33117c478bd9Sstevel@tonic-gate 			return (DDI_PROP_RESULT_ERROR);
33127c478bd9Sstevel@tonic-gate 		}
33137c478bd9Sstevel@tonic-gate 
33147c478bd9Sstevel@tonic-gate 		/*
33159e1bcca2Scth 		 * Match DDI_PROP_CMD_GET_DSIZE logic for when to stop and
33169e1bcca2Scth 		 * how to NULL terminate result.
33177c478bd9Sstevel@tonic-gate 		 */
33189e1bcca2Scth 		p = (char *)ph->ph_cur_pos;
33199e1bcca2Scth 		end = (char *)ph->ph_data + ph->ph_size;
33209e1bcca2Scth 		if (p >= end)
33219e1bcca2Scth 			return (DDI_PROP_RESULT_EOF);
33229e1bcca2Scth 
33239e1bcca2Scth 		while (p < end) {
33249e1bcca2Scth 			*data++ = *p;
33259e1bcca2Scth 			if (*p++ == 0) {	/* NULL from OBP */
33269e1bcca2Scth 				ph->ph_cur_pos = p;
33279e1bcca2Scth 				return (DDI_PROP_RESULT_OK);
33289e1bcca2Scth 			}
33299e1bcca2Scth 		}
33307c478bd9Sstevel@tonic-gate 
33317c478bd9Sstevel@tonic-gate 		/*
33329e1bcca2Scth 		 * If OBP did not NULL terminate string, which happens
33339e1bcca2Scth 		 * (at least) for 'true'/'false' boolean values, account for
33349e1bcca2Scth 		 * the space and store null termination on decode.
33357c478bd9Sstevel@tonic-gate 		 */
33369e1bcca2Scth 		ph->ph_cur_pos = p;
33379e1bcca2Scth 		*data = 0;
33387c478bd9Sstevel@tonic-gate 		return (DDI_PROP_RESULT_OK);
33397c478bd9Sstevel@tonic-gate 
33407c478bd9Sstevel@tonic-gate 	case DDI_PROP_CMD_ENCODE:
33417c478bd9Sstevel@tonic-gate 		/*
33427c478bd9Sstevel@tonic-gate 		 * Check that there is room to encoded the data
33437c478bd9Sstevel@tonic-gate 		 */
33447c478bd9Sstevel@tonic-gate 		if (ph->ph_cur_pos == NULL || ph->ph_size == 0) {
33457c478bd9Sstevel@tonic-gate 			return (DDI_PROP_RESULT_ERROR);
33467c478bd9Sstevel@tonic-gate 		}
33477c478bd9Sstevel@tonic-gate 
33487c478bd9Sstevel@tonic-gate 		n = strlen(data) + 1;
33497c478bd9Sstevel@tonic-gate 		if ((char *)ph->ph_cur_pos > ((char *)ph->ph_data +
3350184cd04cScth 		    ph->ph_size - n)) {
33517c478bd9Sstevel@tonic-gate 			return (DDI_PROP_RESULT_ERROR);
33527c478bd9Sstevel@tonic-gate 		}
33537c478bd9Sstevel@tonic-gate 
33547c478bd9Sstevel@tonic-gate 		/*
33557c478bd9Sstevel@tonic-gate 		 * Copy the NULL terminated string
33567c478bd9Sstevel@tonic-gate 		 */
33577c478bd9Sstevel@tonic-gate 		bcopy(data, ph->ph_cur_pos, n);
33587c478bd9Sstevel@tonic-gate 
33597c478bd9Sstevel@tonic-gate 		/*
33607c478bd9Sstevel@tonic-gate 		 * Move the current location to the start of the next bit of
33617c478bd9Sstevel@tonic-gate 		 * space where we can store encoded data.
33627c478bd9Sstevel@tonic-gate 		 */
33637c478bd9Sstevel@tonic-gate 		ph->ph_cur_pos = (char *)ph->ph_cur_pos + n;
33647c478bd9Sstevel@tonic-gate 		return (DDI_PROP_RESULT_OK);
33657c478bd9Sstevel@tonic-gate 
33667c478bd9Sstevel@tonic-gate 	case DDI_PROP_CMD_SKIP:
33677c478bd9Sstevel@tonic-gate 		/*
33687c478bd9Sstevel@tonic-gate 		 * Check that there is encoded data
33697c478bd9Sstevel@tonic-gate 		 */
33707c478bd9Sstevel@tonic-gate 		if (ph->ph_cur_pos == NULL || ph->ph_size == 0) {
33717c478bd9Sstevel@tonic-gate 			return (DDI_PROP_RESULT_ERROR);
33727c478bd9Sstevel@tonic-gate 		}
33737c478bd9Sstevel@tonic-gate 
33747c478bd9Sstevel@tonic-gate 		/*
33757c478bd9Sstevel@tonic-gate 		 * Return the string length plus one for the NULL
33767c478bd9Sstevel@tonic-gate 		 * We know the size of the property, we need to
33777c478bd9Sstevel@tonic-gate 		 * ensure that the string is properly formatted,
33787c478bd9Sstevel@tonic-gate 		 * since we may be looking up random OBP data.
33797c478bd9Sstevel@tonic-gate 		 */
33807c478bd9Sstevel@tonic-gate 		p = (char *)ph->ph_cur_pos;
33817c478bd9Sstevel@tonic-gate 		end = (char *)ph->ph_data + ph->ph_size;
33829e1bcca2Scth 		if (p >= end)
33837c478bd9Sstevel@tonic-gate 			return (DDI_PROP_RESULT_EOF);
33847c478bd9Sstevel@tonic-gate 
33859e1bcca2Scth 		while (p < end) {
33869e1bcca2Scth 			if (*p++ == 0) {	/* NULL from OBP */
33877c478bd9Sstevel@tonic-gate 				ph->ph_cur_pos = p;
33887c478bd9Sstevel@tonic-gate 				return (DDI_PROP_RESULT_OK);
33897c478bd9Sstevel@tonic-gate 			}
33907c478bd9Sstevel@tonic-gate 		}
33917c478bd9Sstevel@tonic-gate 
33929e1bcca2Scth 		/*
33939e1bcca2Scth 		 * Accommodate the fact that OBP does not always NULL
33949e1bcca2Scth 		 * terminate strings.
33959e1bcca2Scth 		 */
33969e1bcca2Scth 		ph->ph_cur_pos = p;
33979e1bcca2Scth 		return (DDI_PROP_RESULT_OK);
33987c478bd9Sstevel@tonic-gate 
33997c478bd9Sstevel@tonic-gate 	case DDI_PROP_CMD_GET_ESIZE:
34007c478bd9Sstevel@tonic-gate 		/*
34017c478bd9Sstevel@tonic-gate 		 * Return the size of the encoded string on OBP.
34027c478bd9Sstevel@tonic-gate 		 */
34037c478bd9Sstevel@tonic-gate 		return (strlen(data) + 1);
34047c478bd9Sstevel@tonic-gate 
34057c478bd9Sstevel@tonic-gate 	case DDI_PROP_CMD_GET_DSIZE:
34067c478bd9Sstevel@tonic-gate 		/*
34079e1bcca2Scth 		 * Return the string length plus one for the NULL.
34087c478bd9Sstevel@tonic-gate 		 * We know the size of the property, we need to
34097c478bd9Sstevel@tonic-gate 		 * ensure that the string is properly formatted,
34107c478bd9Sstevel@tonic-gate 		 * since we may be looking up random OBP data.
34117c478bd9Sstevel@tonic-gate 		 */
34127c478bd9Sstevel@tonic-gate 		p = (char *)ph->ph_cur_pos;
34137c478bd9Sstevel@tonic-gate 		end = (char *)ph->ph_data + ph->ph_size;
34149e1bcca2Scth 		if (p >= end)
34159e1bcca2Scth 			return (DDI_PROP_RESULT_EOF);
34169e1bcca2Scth 
34177c478bd9Sstevel@tonic-gate 		for (n = 0; p < end; n++) {
34189e1bcca2Scth 			if (*p++ == 0) {	/* NULL from OBP */
34197c478bd9Sstevel@tonic-gate 				ph->ph_cur_pos = p;
34209e1bcca2Scth 				return (n + 1);
34217c478bd9Sstevel@tonic-gate 			}
34227c478bd9Sstevel@tonic-gate 		}
34239e1bcca2Scth 
34249e1bcca2Scth 		/*
34259e1bcca2Scth 		 * If OBP did not NULL terminate string, which happens for
34269e1bcca2Scth 		 * 'true'/'false' boolean values, account for the space
34279e1bcca2Scth 		 * to store null termination here.
34289e1bcca2Scth 		 */
34299e1bcca2Scth 		ph->ph_cur_pos = p;
34309e1bcca2Scth 		return (n + 1);
34317c478bd9Sstevel@tonic-gate 
34327c478bd9Sstevel@tonic-gate 	default:
34337c478bd9Sstevel@tonic-gate #ifdef DEBUG
34347c478bd9Sstevel@tonic-gate 		panic("ddi_prop_1275_string: %x impossible", cmd);
34357c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
34367c478bd9Sstevel@tonic-gate #else
34377c478bd9Sstevel@tonic-gate 		return (DDI_PROP_RESULT_ERROR);
34387c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
34397c478bd9Sstevel@tonic-gate 	}
34407c478bd9Sstevel@tonic-gate }
34417c478bd9Sstevel@tonic-gate 
34427c478bd9Sstevel@tonic-gate /*
34437c478bd9Sstevel@tonic-gate  * OBP 1275 byte operator
34447c478bd9Sstevel@tonic-gate  *
34457c478bd9Sstevel@tonic-gate  * Caller must specify the number of bytes to get.  OBP encodes bytes
34467c478bd9Sstevel@tonic-gate  * as a byte so there is a 1-to-1 translation.
34477c478bd9Sstevel@tonic-gate  */
34487c478bd9Sstevel@tonic-gate int
34497c478bd9Sstevel@tonic-gate ddi_prop_1275_bytes(prop_handle_t *ph, uint_t cmd, uchar_t *data,
34507c478bd9Sstevel@tonic-gate 	uint_t nelements)
34517c478bd9Sstevel@tonic-gate {
34527c478bd9Sstevel@tonic-gate 	switch (cmd) {
34537c478bd9Sstevel@tonic-gate 	case DDI_PROP_CMD_DECODE:
34547c478bd9Sstevel@tonic-gate 		/*
34557c478bd9Sstevel@tonic-gate 		 * Check that there is encoded data
34567c478bd9Sstevel@tonic-gate 		 */
34577c478bd9Sstevel@tonic-gate 		if (ph->ph_cur_pos == NULL || ph->ph_size == 0 ||
3458184cd04cScth 		    ph->ph_size < nelements ||
3459184cd04cScth 		    ((char *)ph->ph_cur_pos > ((char *)ph->ph_data +
3460184cd04cScth 		    ph->ph_size - nelements)))
34617c478bd9Sstevel@tonic-gate 			return (DDI_PROP_RESULT_ERROR);
34627c478bd9Sstevel@tonic-gate 
34637c478bd9Sstevel@tonic-gate 		/*
34647c478bd9Sstevel@tonic-gate 		 * Copy out the bytes
34657c478bd9Sstevel@tonic-gate 		 */
34667c478bd9Sstevel@tonic-gate 		bcopy(ph->ph_cur_pos, data, nelements);
34677c478bd9Sstevel@tonic-gate 
34687c478bd9Sstevel@tonic-gate 		/*
34697c478bd9Sstevel@tonic-gate 		 * Move the current location
34707c478bd9Sstevel@tonic-gate 		 */
34717c478bd9Sstevel@tonic-gate 		ph->ph_cur_pos = (char *)ph->ph_cur_pos + nelements;
34727c478bd9Sstevel@tonic-gate 		return (DDI_PROP_RESULT_OK);
34737c478bd9Sstevel@tonic-gate 
34747c478bd9Sstevel@tonic-gate 	case DDI_PROP_CMD_ENCODE:
34757c478bd9Sstevel@tonic-gate 		/*
34767c478bd9Sstevel@tonic-gate 		 * Check that there is room to encode the data
34777c478bd9Sstevel@tonic-gate 		 */
34787c478bd9Sstevel@tonic-gate 		if (ph->ph_cur_pos == NULL || ph->ph_size == 0 ||
3479184cd04cScth 		    ph->ph_size < nelements ||
3480184cd04cScth 		    ((char *)ph->ph_cur_pos > ((char *)ph->ph_data +
3481184cd04cScth 		    ph->ph_size - nelements)))
34827c478bd9Sstevel@tonic-gate 			return (DDI_PROP_RESULT_ERROR);
34837c478bd9Sstevel@tonic-gate 
34847c478bd9Sstevel@tonic-gate 		/*
34857c478bd9Sstevel@tonic-gate 		 * Copy in the bytes
34867c478bd9Sstevel@tonic-gate 		 */
34877c478bd9Sstevel@tonic-gate 		bcopy(data, ph->ph_cur_pos, nelements);
34887c478bd9Sstevel@tonic-gate 
34897c478bd9Sstevel@tonic-gate 		/*
34907c478bd9Sstevel@tonic-gate 		 * Move the current location to the start of the next bit of
34917c478bd9Sstevel@tonic-gate 		 * space where we can store encoded data.
34927c478bd9Sstevel@tonic-gate 		 */
34937c478bd9Sstevel@tonic-gate 		ph->ph_cur_pos = (char *)ph->ph_cur_pos + nelements;
34947c478bd9Sstevel@tonic-gate 		return (DDI_PROP_RESULT_OK);
34957c478bd9Sstevel@tonic-gate 
34967c478bd9Sstevel@tonic-gate 	case DDI_PROP_CMD_SKIP:
34977c478bd9Sstevel@tonic-gate 		/*
34987c478bd9Sstevel@tonic-gate 		 * Check that there is encoded data
34997c478bd9Sstevel@tonic-gate 		 */
35007c478bd9Sstevel@tonic-gate 		if (ph->ph_cur_pos == NULL || ph->ph_size == 0 ||
3501184cd04cScth 		    ph->ph_size < nelements)
35027c478bd9Sstevel@tonic-gate 			return (DDI_PROP_RESULT_ERROR);
35037c478bd9Sstevel@tonic-gate 
35047c478bd9Sstevel@tonic-gate 		if ((char *)ph->ph_cur_pos > ((char *)ph->ph_data +
3505184cd04cScth 		    ph->ph_size - nelements))
35067c478bd9Sstevel@tonic-gate 			return (DDI_PROP_RESULT_EOF);
35077c478bd9Sstevel@tonic-gate 
35087c478bd9Sstevel@tonic-gate 		/*
35097c478bd9Sstevel@tonic-gate 		 * Move the current location
35107c478bd9Sstevel@tonic-gate 		 */
35117c478bd9Sstevel@tonic-gate 		ph->ph_cur_pos = (char *)ph->ph_cur_pos + nelements;
35127c478bd9Sstevel@tonic-gate 		return (DDI_PROP_RESULT_OK);
35137c478bd9Sstevel@tonic-gate 
35147c478bd9Sstevel@tonic-gate 	case DDI_PROP_CMD_GET_ESIZE:
35157c478bd9Sstevel@tonic-gate 		/*
35167c478bd9Sstevel@tonic-gate 		 * The size in bytes of the encoded size is the
35177c478bd9Sstevel@tonic-gate 		 * same as the decoded size provided by the caller.
35187c478bd9Sstevel@tonic-gate 		 */
35197c478bd9Sstevel@tonic-gate 		return (nelements);
35207c478bd9Sstevel@tonic-gate 
35217c478bd9Sstevel@tonic-gate 	case DDI_PROP_CMD_GET_DSIZE:
35227c478bd9Sstevel@tonic-gate 		/*
35237c478bd9Sstevel@tonic-gate 		 * Just return the number of bytes specified by the caller.
35247c478bd9Sstevel@tonic-gate 		 */
35257c478bd9Sstevel@tonic-gate 		return (nelements);
35267c478bd9Sstevel@tonic-gate 
35277c478bd9Sstevel@tonic-gate 	default:
35287c478bd9Sstevel@tonic-gate #ifdef DEBUG
35297c478bd9Sstevel@tonic-gate 		panic("ddi_prop_1275_bytes: %x impossible", cmd);
35307c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
35317c478bd9Sstevel@tonic-gate #else
35327c478bd9Sstevel@tonic-gate 		return (DDI_PROP_RESULT_ERROR);
35337c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
35347c478bd9Sstevel@tonic-gate 	}
35357c478bd9Sstevel@tonic-gate }
35367c478bd9Sstevel@tonic-gate 
35377c478bd9Sstevel@tonic-gate /*
35387c478bd9Sstevel@tonic-gate  * Used for properties that come from the OBP, hardware configuration files,
35397c478bd9Sstevel@tonic-gate  * or that are created by calls to ddi_prop_update(9F).
35407c478bd9Sstevel@tonic-gate  */
35417c478bd9Sstevel@tonic-gate static struct prop_handle_ops prop_1275_ops = {
35427c478bd9Sstevel@tonic-gate 	ddi_prop_1275_int,
35437c478bd9Sstevel@tonic-gate 	ddi_prop_1275_string,
35447c478bd9Sstevel@tonic-gate 	ddi_prop_1275_bytes,
35457c478bd9Sstevel@tonic-gate 	ddi_prop_int64_op
35467c478bd9Sstevel@tonic-gate };
35477c478bd9Sstevel@tonic-gate 
35487c478bd9Sstevel@tonic-gate 
35497c478bd9Sstevel@tonic-gate /*
35507c478bd9Sstevel@tonic-gate  * Interface to create/modify a managed property on child's behalf...
35517c478bd9Sstevel@tonic-gate  * Flags interpreted are:
35527c478bd9Sstevel@tonic-gate  *	DDI_PROP_CANSLEEP:	Allow memory allocation to sleep.
35537c478bd9Sstevel@tonic-gate  *	DDI_PROP_SYSTEM_DEF:	Manipulate system list rather than driver list.
35547c478bd9Sstevel@tonic-gate  *
35557c478bd9Sstevel@tonic-gate  * Use same dev_t when modifying or undefining a property.
35567c478bd9Sstevel@tonic-gate  * Search for properties with DDI_DEV_T_ANY to match first named
35577c478bd9Sstevel@tonic-gate  * property on the list.
35587c478bd9Sstevel@tonic-gate  *
35597c478bd9Sstevel@tonic-gate  * Properties are stored LIFO and subsequently will match the first
35607c478bd9Sstevel@tonic-gate  * `matching' instance.
35617c478bd9Sstevel@tonic-gate  */
35627c478bd9Sstevel@tonic-gate 
35637c478bd9Sstevel@tonic-gate /*
35647c478bd9Sstevel@tonic-gate  * ddi_prop_add:	Add a software defined property
35657c478bd9Sstevel@tonic-gate  */
35667c478bd9Sstevel@tonic-gate 
35677c478bd9Sstevel@tonic-gate /*
35687c478bd9Sstevel@tonic-gate  * define to get a new ddi_prop_t.
35697c478bd9Sstevel@tonic-gate  * km_flags are KM_SLEEP or KM_NOSLEEP.
35707c478bd9Sstevel@tonic-gate  */
35717c478bd9Sstevel@tonic-gate 
35727c478bd9Sstevel@tonic-gate #define	DDI_NEW_PROP_T(km_flags)	\
35737c478bd9Sstevel@tonic-gate 	(kmem_zalloc(sizeof (ddi_prop_t), km_flags))
35747c478bd9Sstevel@tonic-gate 
35757c478bd9Sstevel@tonic-gate static int
35767c478bd9Sstevel@tonic-gate ddi_prop_add(dev_t dev, dev_info_t *dip, int flags,
35777c478bd9Sstevel@tonic-gate     char *name, caddr_t value, int length)
35787c478bd9Sstevel@tonic-gate {
35797c478bd9Sstevel@tonic-gate 	ddi_prop_t	*new_propp, *propp;
35807c478bd9Sstevel@tonic-gate 	ddi_prop_t	**list_head = &(DEVI(dip)->devi_drv_prop_ptr);
35817c478bd9Sstevel@tonic-gate 	int		km_flags = KM_NOSLEEP;
35827c478bd9Sstevel@tonic-gate 	int		name_buf_len;
35837c478bd9Sstevel@tonic-gate 
35847c478bd9Sstevel@tonic-gate 	/*
35857c478bd9Sstevel@tonic-gate 	 * If dev_t is DDI_DEV_T_ANY or name's length is zero return error.
35867c478bd9Sstevel@tonic-gate 	 */
35877c478bd9Sstevel@tonic-gate 
35887c478bd9Sstevel@tonic-gate 	if (dev == DDI_DEV_T_ANY || name == (char *)0 || strlen(name) == 0)
35897c478bd9Sstevel@tonic-gate 		return (DDI_PROP_INVAL_ARG);
35907c478bd9Sstevel@tonic-gate 
35917c478bd9Sstevel@tonic-gate 	if (flags & DDI_PROP_CANSLEEP)
35927c478bd9Sstevel@tonic-gate 		km_flags = KM_SLEEP;
35937c478bd9Sstevel@tonic-gate 
35947c478bd9Sstevel@tonic-gate 	if (flags & DDI_PROP_SYSTEM_DEF)
35957c478bd9Sstevel@tonic-gate 		list_head = &(DEVI(dip)->devi_sys_prop_ptr);
35967c478bd9Sstevel@tonic-gate 	else if (flags & DDI_PROP_HW_DEF)
35977c478bd9Sstevel@tonic-gate 		list_head = &(DEVI(dip)->devi_hw_prop_ptr);
35987c478bd9Sstevel@tonic-gate 
35997c478bd9Sstevel@tonic-gate 	if ((new_propp = DDI_NEW_PROP_T(km_flags)) == NULL)  {
36007c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, prop_no_mem_msg, name);
36017c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NO_MEMORY);
36027c478bd9Sstevel@tonic-gate 	}
36037c478bd9Sstevel@tonic-gate 
36047c478bd9Sstevel@tonic-gate 	/*
36057c478bd9Sstevel@tonic-gate 	 * If dev is major number 0, then we need to do a ddi_name_to_major
36067c478bd9Sstevel@tonic-gate 	 * to get the real major number for the device.  This needs to be
36077c478bd9Sstevel@tonic-gate 	 * done because some drivers need to call ddi_prop_create in their
36087c478bd9Sstevel@tonic-gate 	 * attach routines but they don't have a dev.  By creating the dev
36097c478bd9Sstevel@tonic-gate 	 * ourself if the major number is 0, drivers will not have to know what
36107c478bd9Sstevel@tonic-gate 	 * their major number.	They can just create a dev with major number
36117c478bd9Sstevel@tonic-gate 	 * 0 and pass it in.  For device 0, we will be doing a little extra
36127c478bd9Sstevel@tonic-gate 	 * work by recreating the same dev that we already have, but its the
36137c478bd9Sstevel@tonic-gate 	 * price you pay :-).
36147c478bd9Sstevel@tonic-gate 	 *
36157c478bd9Sstevel@tonic-gate 	 * This fixes bug #1098060.
36167c478bd9Sstevel@tonic-gate 	 */
36177c478bd9Sstevel@tonic-gate 	if (getmajor(dev) == DDI_MAJOR_T_UNKNOWN) {
36187c478bd9Sstevel@tonic-gate 		new_propp->prop_dev =
36197c478bd9Sstevel@tonic-gate 		    makedevice(ddi_name_to_major(DEVI(dip)->devi_binding_name),
36207c478bd9Sstevel@tonic-gate 		    getminor(dev));
36217c478bd9Sstevel@tonic-gate 	} else
36227c478bd9Sstevel@tonic-gate 		new_propp->prop_dev = dev;
36237c478bd9Sstevel@tonic-gate 
36247c478bd9Sstevel@tonic-gate 	/*
36257c478bd9Sstevel@tonic-gate 	 * Allocate space for property name and copy it in...
36267c478bd9Sstevel@tonic-gate 	 */
36277c478bd9Sstevel@tonic-gate 
36287c478bd9Sstevel@tonic-gate 	name_buf_len = strlen(name) + 1;
36297c478bd9Sstevel@tonic-gate 	new_propp->prop_name = kmem_alloc(name_buf_len, km_flags);
36307c478bd9Sstevel@tonic-gate 	if (new_propp->prop_name == 0)	{
36317c478bd9Sstevel@tonic-gate 		kmem_free(new_propp, sizeof (ddi_prop_t));
36327c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, prop_no_mem_msg, name);
36337c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NO_MEMORY);
36347c478bd9Sstevel@tonic-gate 	}
36357c478bd9Sstevel@tonic-gate 	bcopy(name, new_propp->prop_name, name_buf_len);
36367c478bd9Sstevel@tonic-gate 
36377c478bd9Sstevel@tonic-gate 	/*
36387c478bd9Sstevel@tonic-gate 	 * Set the property type
36397c478bd9Sstevel@tonic-gate 	 */
36407c478bd9Sstevel@tonic-gate 	new_propp->prop_flags = flags & DDI_PROP_TYPE_MASK;
36417c478bd9Sstevel@tonic-gate 
36427c478bd9Sstevel@tonic-gate 	/*
36437c478bd9Sstevel@tonic-gate 	 * Set length and value ONLY if not an explicit property undefine:
36447c478bd9Sstevel@tonic-gate 	 * NOTE: value and length are zero for explicit undefines.
36457c478bd9Sstevel@tonic-gate 	 */
36467c478bd9Sstevel@tonic-gate 
36477c478bd9Sstevel@tonic-gate 	if (flags & DDI_PROP_UNDEF_IT) {
36487c478bd9Sstevel@tonic-gate 		new_propp->prop_flags |= DDI_PROP_UNDEF_IT;
36497c478bd9Sstevel@tonic-gate 	} else {
36507c478bd9Sstevel@tonic-gate 		if ((new_propp->prop_len = length) != 0) {
36517c478bd9Sstevel@tonic-gate 			new_propp->prop_val = kmem_alloc(length, km_flags);
36527c478bd9Sstevel@tonic-gate 			if (new_propp->prop_val == 0)  {
36537c478bd9Sstevel@tonic-gate 				kmem_free(new_propp->prop_name, name_buf_len);
36547c478bd9Sstevel@tonic-gate 				kmem_free(new_propp, sizeof (ddi_prop_t));
36557c478bd9Sstevel@tonic-gate 				cmn_err(CE_CONT, prop_no_mem_msg, name);
36567c478bd9Sstevel@tonic-gate 				return (DDI_PROP_NO_MEMORY);
36577c478bd9Sstevel@tonic-gate 			}
36587c478bd9Sstevel@tonic-gate 			bcopy(value, new_propp->prop_val, length);
36597c478bd9Sstevel@tonic-gate 		}
36607c478bd9Sstevel@tonic-gate 	}
36617c478bd9Sstevel@tonic-gate 
36627c478bd9Sstevel@tonic-gate 	/*
36637c478bd9Sstevel@tonic-gate 	 * Link property into beginning of list. (Properties are LIFO order.)
36647c478bd9Sstevel@tonic-gate 	 */
36657c478bd9Sstevel@tonic-gate 
36667c478bd9Sstevel@tonic-gate 	mutex_enter(&(DEVI(dip)->devi_lock));
36677c478bd9Sstevel@tonic-gate 	propp = *list_head;
36687c478bd9Sstevel@tonic-gate 	new_propp->prop_next = propp;
36697c478bd9Sstevel@tonic-gate 	*list_head = new_propp;
36707c478bd9Sstevel@tonic-gate 	mutex_exit(&(DEVI(dip)->devi_lock));
36717c478bd9Sstevel@tonic-gate 	return (DDI_PROP_SUCCESS);
36727c478bd9Sstevel@tonic-gate }
36737c478bd9Sstevel@tonic-gate 
36747c478bd9Sstevel@tonic-gate 
36757c478bd9Sstevel@tonic-gate /*
36767c478bd9Sstevel@tonic-gate  * ddi_prop_change:	Modify a software managed property value
36777c478bd9Sstevel@tonic-gate  *
36787c478bd9Sstevel@tonic-gate  *			Set new length and value if found.
36797c478bd9Sstevel@tonic-gate  *			returns DDI_PROP_INVAL_ARG if dev is DDI_DEV_T_ANY or
36807c478bd9Sstevel@tonic-gate  *			input name is the NULL string.
36817c478bd9Sstevel@tonic-gate  *			returns DDI_PROP_NO_MEMORY if unable to allocate memory
36827c478bd9Sstevel@tonic-gate  *
36837c478bd9Sstevel@tonic-gate  *			Note: an undef can be modified to be a define,
36847c478bd9Sstevel@tonic-gate  *			(you can't go the other way.)
36857c478bd9Sstevel@tonic-gate  */
36867c478bd9Sstevel@tonic-gate 
36877c478bd9Sstevel@tonic-gate static int
36887c478bd9Sstevel@tonic-gate ddi_prop_change(dev_t dev, dev_info_t *dip, int flags,
36897c478bd9Sstevel@tonic-gate     char *name, caddr_t value, int length)
36907c478bd9Sstevel@tonic-gate {
36917c478bd9Sstevel@tonic-gate 	ddi_prop_t	*propp;
36929ac2ddeaScth 	ddi_prop_t	**ppropp;
36937c478bd9Sstevel@tonic-gate 	caddr_t		p = NULL;
36947c478bd9Sstevel@tonic-gate 
36959ac2ddeaScth 	if ((dev == DDI_DEV_T_ANY) || (name == NULL) || (strlen(name) == 0))
36967c478bd9Sstevel@tonic-gate 		return (DDI_PROP_INVAL_ARG);
36977c478bd9Sstevel@tonic-gate 
36987c478bd9Sstevel@tonic-gate 	/*
36997c478bd9Sstevel@tonic-gate 	 * Preallocate buffer, even if we don't need it...
37007c478bd9Sstevel@tonic-gate 	 */
37017c478bd9Sstevel@tonic-gate 	if (length != 0)  {
37029ac2ddeaScth 		p = kmem_alloc(length, (flags & DDI_PROP_CANSLEEP) ?
37039ac2ddeaScth 		    KM_SLEEP : KM_NOSLEEP);
37047c478bd9Sstevel@tonic-gate 		if (p == NULL)	{
37057c478bd9Sstevel@tonic-gate 			cmn_err(CE_CONT, prop_no_mem_msg, name);
37067c478bd9Sstevel@tonic-gate 			return (DDI_PROP_NO_MEMORY);
37077c478bd9Sstevel@tonic-gate 		}
37087c478bd9Sstevel@tonic-gate 	}
37097c478bd9Sstevel@tonic-gate 
37109ac2ddeaScth 	/*
37119ac2ddeaScth 	 * If the dev_t value contains DDI_MAJOR_T_UNKNOWN for the major
37129ac2ddeaScth 	 * number, a real dev_t value should be created based upon the dip's
37139ac2ddeaScth 	 * binding driver.  See ddi_prop_add...
37149ac2ddeaScth 	 */
37159ac2ddeaScth 	if (getmajor(dev) == DDI_MAJOR_T_UNKNOWN)
37169ac2ddeaScth 		dev = makedevice(
37179ac2ddeaScth 		    ddi_name_to_major(DEVI(dip)->devi_binding_name),
37189ac2ddeaScth 		    getminor(dev));
37199ac2ddeaScth 
37207c478bd9Sstevel@tonic-gate 	/*
37217c478bd9Sstevel@tonic-gate 	 * Check to see if the property exists.  If so we modify it.
37227c478bd9Sstevel@tonic-gate 	 * Else we create it by calling ddi_prop_add().
37237c478bd9Sstevel@tonic-gate 	 */
37247c478bd9Sstevel@tonic-gate 	mutex_enter(&(DEVI(dip)->devi_lock));
37259ac2ddeaScth 	ppropp = &DEVI(dip)->devi_drv_prop_ptr;
37267c478bd9Sstevel@tonic-gate 	if (flags & DDI_PROP_SYSTEM_DEF)
37279ac2ddeaScth 		ppropp = &DEVI(dip)->devi_sys_prop_ptr;
37287c478bd9Sstevel@tonic-gate 	else if (flags & DDI_PROP_HW_DEF)
37299ac2ddeaScth 		ppropp = &DEVI(dip)->devi_hw_prop_ptr;
37307c478bd9Sstevel@tonic-gate 
37319ac2ddeaScth 	if ((propp = i_ddi_prop_search(dev, name, flags, ppropp)) != NULL) {
37329ac2ddeaScth 		/*
37339ac2ddeaScth 		 * Need to reallocate buffer?  If so, do it
37349ac2ddeaScth 		 * carefully (reuse same space if new prop
37359ac2ddeaScth 		 * is same size and non-NULL sized).
37369ac2ddeaScth 		 */
37379ac2ddeaScth 		if (length != 0)
37389ac2ddeaScth 			bcopy(value, p, length);
37397c478bd9Sstevel@tonic-gate 
37409ac2ddeaScth 		if (propp->prop_len != 0)
37419ac2ddeaScth 			kmem_free(propp->prop_val, propp->prop_len);
37427c478bd9Sstevel@tonic-gate 
37439ac2ddeaScth 		propp->prop_len = length;
37449ac2ddeaScth 		propp->prop_val = p;
37459ac2ddeaScth 		propp->prop_flags &= ~DDI_PROP_UNDEF_IT;
37469ac2ddeaScth 		mutex_exit(&(DEVI(dip)->devi_lock));
37479ac2ddeaScth 		return (DDI_PROP_SUCCESS);
37487c478bd9Sstevel@tonic-gate 	}
37497c478bd9Sstevel@tonic-gate 
37507c478bd9Sstevel@tonic-gate 	mutex_exit(&(DEVI(dip)->devi_lock));
37517c478bd9Sstevel@tonic-gate 	if (length != 0)
37527c478bd9Sstevel@tonic-gate 		kmem_free(p, length);
37539ac2ddeaScth 
37547c478bd9Sstevel@tonic-gate 	return (ddi_prop_add(dev, dip, flags, name, value, length));
37557c478bd9Sstevel@tonic-gate }
37567c478bd9Sstevel@tonic-gate 
37577c478bd9Sstevel@tonic-gate /*
37587c478bd9Sstevel@tonic-gate  * Common update routine used to update and encode a property.	Creates
37597c478bd9Sstevel@tonic-gate  * a property handle, calls the property encode routine, figures out if
37607c478bd9Sstevel@tonic-gate  * the property already exists and updates if it does.	Otherwise it
37617c478bd9Sstevel@tonic-gate  * creates if it does not exist.
37627c478bd9Sstevel@tonic-gate  */
37637c478bd9Sstevel@tonic-gate int
37647c478bd9Sstevel@tonic-gate ddi_prop_update_common(dev_t match_dev, dev_info_t *dip, int flags,
37657c478bd9Sstevel@tonic-gate     char *name, void *data, uint_t nelements,
37667c478bd9Sstevel@tonic-gate     int (*prop_create)(prop_handle_t *, void *data, uint_t nelements))
37677c478bd9Sstevel@tonic-gate {
37687c478bd9Sstevel@tonic-gate 	prop_handle_t	ph;
37697c478bd9Sstevel@tonic-gate 	int		rval;
37707c478bd9Sstevel@tonic-gate 	uint_t		ourflags;
37717c478bd9Sstevel@tonic-gate 
37727c478bd9Sstevel@tonic-gate 	/*
37737c478bd9Sstevel@tonic-gate 	 * If dev_t is DDI_DEV_T_ANY or name's length is zero,
37747c478bd9Sstevel@tonic-gate 	 * return error.
37757c478bd9Sstevel@tonic-gate 	 */
37767c478bd9Sstevel@tonic-gate 	if (match_dev == DDI_DEV_T_ANY || name == NULL || strlen(name) == 0)
37777c478bd9Sstevel@tonic-gate 		return (DDI_PROP_INVAL_ARG);
37787c478bd9Sstevel@tonic-gate 
37797c478bd9Sstevel@tonic-gate 	/*
37807c478bd9Sstevel@tonic-gate 	 * Create the handle
37817c478bd9Sstevel@tonic-gate 	 */
37827c478bd9Sstevel@tonic-gate 	ph.ph_data = NULL;
37837c478bd9Sstevel@tonic-gate 	ph.ph_cur_pos = NULL;
37847c478bd9Sstevel@tonic-gate 	ph.ph_save_pos = NULL;
37857c478bd9Sstevel@tonic-gate 	ph.ph_size = 0;
37867c478bd9Sstevel@tonic-gate 	ph.ph_ops = &prop_1275_ops;
37877c478bd9Sstevel@tonic-gate 
37887c478bd9Sstevel@tonic-gate 	/*
37897c478bd9Sstevel@tonic-gate 	 * ourflags:
37907c478bd9Sstevel@tonic-gate 	 * For compatibility with the old interfaces.  The old interfaces
37917c478bd9Sstevel@tonic-gate 	 * didn't sleep by default and slept when the flag was set.  These
37927c478bd9Sstevel@tonic-gate 	 * interfaces to the opposite.	So the old interfaces now set the
37937c478bd9Sstevel@tonic-gate 	 * DDI_PROP_DONTSLEEP flag by default which tells us not to sleep.
37947c478bd9Sstevel@tonic-gate 	 *
37957c478bd9Sstevel@tonic-gate 	 * ph.ph_flags:
37967c478bd9Sstevel@tonic-gate 	 * Blocked data or unblocked data allocation
37977c478bd9Sstevel@tonic-gate 	 * for ph.ph_data in ddi_prop_encode_alloc()
37987c478bd9Sstevel@tonic-gate 	 */
37997c478bd9Sstevel@tonic-gate 	if (flags & DDI_PROP_DONTSLEEP) {
38007c478bd9Sstevel@tonic-gate 		ourflags = flags;
38017c478bd9Sstevel@tonic-gate 		ph.ph_flags = DDI_PROP_DONTSLEEP;
38027c478bd9Sstevel@tonic-gate 	} else {
38037c478bd9Sstevel@tonic-gate 		ourflags = flags | DDI_PROP_CANSLEEP;
38047c478bd9Sstevel@tonic-gate 		ph.ph_flags = DDI_PROP_CANSLEEP;
38057c478bd9Sstevel@tonic-gate 	}
38067c478bd9Sstevel@tonic-gate 
38077c478bd9Sstevel@tonic-gate 	/*
38087c478bd9Sstevel@tonic-gate 	 * Encode the data and store it in the property handle by
38097c478bd9Sstevel@tonic-gate 	 * calling the prop_encode routine.
38107c478bd9Sstevel@tonic-gate 	 */
38117c478bd9Sstevel@tonic-gate 	if ((rval = (*prop_create)(&ph, data, nelements)) !=
38127c478bd9Sstevel@tonic-gate 	    DDI_PROP_SUCCESS) {
38137c478bd9Sstevel@tonic-gate 		if (rval == DDI_PROP_NO_MEMORY)
38147c478bd9Sstevel@tonic-gate 			cmn_err(CE_CONT, prop_no_mem_msg, name);
38157c478bd9Sstevel@tonic-gate 		if (ph.ph_size != 0)
38167c478bd9Sstevel@tonic-gate 			kmem_free(ph.ph_data, ph.ph_size);
38177c478bd9Sstevel@tonic-gate 		return (rval);
38187c478bd9Sstevel@tonic-gate 	}
38197c478bd9Sstevel@tonic-gate 
38207c478bd9Sstevel@tonic-gate 	/*
38217c478bd9Sstevel@tonic-gate 	 * The old interfaces use a stacking approach to creating
38227c478bd9Sstevel@tonic-gate 	 * properties.	If we are being called from the old interfaces,
38237c478bd9Sstevel@tonic-gate 	 * the DDI_PROP_STACK_CREATE flag will be set, so we just do a
38247c478bd9Sstevel@tonic-gate 	 * create without checking.
38257c478bd9Sstevel@tonic-gate 	 */
38267c478bd9Sstevel@tonic-gate 	if (flags & DDI_PROP_STACK_CREATE) {
38277c478bd9Sstevel@tonic-gate 		rval = ddi_prop_add(match_dev, dip,
38287c478bd9Sstevel@tonic-gate 		    ourflags, name, ph.ph_data, ph.ph_size);
38297c478bd9Sstevel@tonic-gate 	} else {
38307c478bd9Sstevel@tonic-gate 		rval = ddi_prop_change(match_dev, dip,
38317c478bd9Sstevel@tonic-gate 		    ourflags, name, ph.ph_data, ph.ph_size);
38327c478bd9Sstevel@tonic-gate 	}
38337c478bd9Sstevel@tonic-gate 
38347c478bd9Sstevel@tonic-gate 	/*
38357c478bd9Sstevel@tonic-gate 	 * Free the encoded data allocated in the prop_encode routine.
38367c478bd9Sstevel@tonic-gate 	 */
38377c478bd9Sstevel@tonic-gate 	if (ph.ph_size != 0)
38387c478bd9Sstevel@tonic-gate 		kmem_free(ph.ph_data, ph.ph_size);
38397c478bd9Sstevel@tonic-gate 
38407c478bd9Sstevel@tonic-gate 	return (rval);
38417c478bd9Sstevel@tonic-gate }
38427c478bd9Sstevel@tonic-gate 
38437c478bd9Sstevel@tonic-gate 
38447c478bd9Sstevel@tonic-gate /*
38457c478bd9Sstevel@tonic-gate  * ddi_prop_create:	Define a managed property:
38467c478bd9Sstevel@tonic-gate  *			See above for details.
38477c478bd9Sstevel@tonic-gate  */
38487c478bd9Sstevel@tonic-gate 
38497c478bd9Sstevel@tonic-gate int
38507c478bd9Sstevel@tonic-gate ddi_prop_create(dev_t dev, dev_info_t *dip, int flag,
38517c478bd9Sstevel@tonic-gate     char *name, caddr_t value, int length)
38527c478bd9Sstevel@tonic-gate {
38537c478bd9Sstevel@tonic-gate 	if (!(flag & DDI_PROP_CANSLEEP)) {
38547c478bd9Sstevel@tonic-gate 		flag |= DDI_PROP_DONTSLEEP;
38557c478bd9Sstevel@tonic-gate #ifdef DDI_PROP_DEBUG
38567c478bd9Sstevel@tonic-gate 		if (length != 0)
38577c478bd9Sstevel@tonic-gate 			cmn_err(CE_NOTE, "!ddi_prop_create: interface obsolete,"
38587c478bd9Sstevel@tonic-gate 			    "use ddi_prop_update (prop = %s, node = %s%d)",
38597c478bd9Sstevel@tonic-gate 			    name, ddi_driver_name(dip), ddi_get_instance(dip));
38607c478bd9Sstevel@tonic-gate #endif /* DDI_PROP_DEBUG */
38617c478bd9Sstevel@tonic-gate 	}
38627c478bd9Sstevel@tonic-gate 	flag &= ~DDI_PROP_SYSTEM_DEF;
3863b9ccdc5aScth 	flag |= DDI_PROP_STACK_CREATE | DDI_PROP_TYPE_ANY;
3864b9ccdc5aScth 	return (ddi_prop_update_common(dev, dip, flag, name,
38657c478bd9Sstevel@tonic-gate 	    value, length, ddi_prop_fm_encode_bytes));
38667c478bd9Sstevel@tonic-gate }
38677c478bd9Sstevel@tonic-gate 
38687c478bd9Sstevel@tonic-gate int
38697c478bd9Sstevel@tonic-gate e_ddi_prop_create(dev_t dev, dev_info_t *dip, int flag,
38707c478bd9Sstevel@tonic-gate     char *name, caddr_t value, int length)
38717c478bd9Sstevel@tonic-gate {
38727c478bd9Sstevel@tonic-gate 	if (!(flag & DDI_PROP_CANSLEEP))
38737c478bd9Sstevel@tonic-gate 		flag |= DDI_PROP_DONTSLEEP;
3874b9ccdc5aScth 	flag |= DDI_PROP_SYSTEM_DEF | DDI_PROP_STACK_CREATE | DDI_PROP_TYPE_ANY;
3875b9ccdc5aScth 	return (ddi_prop_update_common(dev, dip, flag,
38767c478bd9Sstevel@tonic-gate 	    name, value, length, ddi_prop_fm_encode_bytes));
38777c478bd9Sstevel@tonic-gate }
38787c478bd9Sstevel@tonic-gate 
38797c478bd9Sstevel@tonic-gate int
38807c478bd9Sstevel@tonic-gate ddi_prop_modify(dev_t dev, dev_info_t *dip, int flag,
38817c478bd9Sstevel@tonic-gate     char *name, caddr_t value, int length)
38827c478bd9Sstevel@tonic-gate {
38837c478bd9Sstevel@tonic-gate 	ASSERT((flag & DDI_PROP_TYPE_MASK) == 0);
38847c478bd9Sstevel@tonic-gate 
38857c478bd9Sstevel@tonic-gate 	/*
38867c478bd9Sstevel@tonic-gate 	 * If dev_t is DDI_DEV_T_ANY or name's length is zero,
38877c478bd9Sstevel@tonic-gate 	 * return error.
38887c478bd9Sstevel@tonic-gate 	 */
38897c478bd9Sstevel@tonic-gate 	if (dev == DDI_DEV_T_ANY || name == NULL || strlen(name) == 0)
38907c478bd9Sstevel@tonic-gate 		return (DDI_PROP_INVAL_ARG);
38917c478bd9Sstevel@tonic-gate 
38927c478bd9Sstevel@tonic-gate 	if (!(flag & DDI_PROP_CANSLEEP))
38937c478bd9Sstevel@tonic-gate 		flag |= DDI_PROP_DONTSLEEP;
38947c478bd9Sstevel@tonic-gate 	flag &= ~DDI_PROP_SYSTEM_DEF;
38959ac2ddeaScth 	if (ddi_prop_exists(dev, dip, (flag | DDI_PROP_NOTPROM), name) == 0)
38967c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
38977c478bd9Sstevel@tonic-gate 
38987c478bd9Sstevel@tonic-gate 	return (ddi_prop_update_common(dev, dip,
38997c478bd9Sstevel@tonic-gate 	    (flag | DDI_PROP_TYPE_BYTE), name,
39007c478bd9Sstevel@tonic-gate 	    value, length, ddi_prop_fm_encode_bytes));
39017c478bd9Sstevel@tonic-gate }
39027c478bd9Sstevel@tonic-gate 
39037c478bd9Sstevel@tonic-gate int
39047c478bd9Sstevel@tonic-gate e_ddi_prop_modify(dev_t dev, dev_info_t *dip, int flag,
39057c478bd9Sstevel@tonic-gate     char *name, caddr_t value, int length)
39067c478bd9Sstevel@tonic-gate {
39077c478bd9Sstevel@tonic-gate 	ASSERT((flag & DDI_PROP_TYPE_MASK) == 0);
39087c478bd9Sstevel@tonic-gate 
39097c478bd9Sstevel@tonic-gate 	/*
39107c478bd9Sstevel@tonic-gate 	 * If dev_t is DDI_DEV_T_ANY or name's length is zero,
39117c478bd9Sstevel@tonic-gate 	 * return error.
39127c478bd9Sstevel@tonic-gate 	 */
39137c478bd9Sstevel@tonic-gate 	if (dev == DDI_DEV_T_ANY || name == NULL || strlen(name) == 0)
39147c478bd9Sstevel@tonic-gate 		return (DDI_PROP_INVAL_ARG);
39157c478bd9Sstevel@tonic-gate 
39169ac2ddeaScth 	if (ddi_prop_exists(dev, dip, (flag | DDI_PROP_SYSTEM_DEF), name) == 0)
39177c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
39187c478bd9Sstevel@tonic-gate 
39197c478bd9Sstevel@tonic-gate 	if (!(flag & DDI_PROP_CANSLEEP))
39207c478bd9Sstevel@tonic-gate 		flag |= DDI_PROP_DONTSLEEP;
39217c478bd9Sstevel@tonic-gate 	return (ddi_prop_update_common(dev, dip,
3922184cd04cScth 	    (flag | DDI_PROP_SYSTEM_DEF | DDI_PROP_TYPE_BYTE),
3923184cd04cScth 	    name, value, length, ddi_prop_fm_encode_bytes));
39247c478bd9Sstevel@tonic-gate }
39257c478bd9Sstevel@tonic-gate 
39267c478bd9Sstevel@tonic-gate 
39277c478bd9Sstevel@tonic-gate /*
39287c478bd9Sstevel@tonic-gate  * Common lookup routine used to lookup and decode a property.
39297c478bd9Sstevel@tonic-gate  * Creates a property handle, searches for the raw encoded data,
39307c478bd9Sstevel@tonic-gate  * fills in the handle, and calls the property decode functions
39317c478bd9Sstevel@tonic-gate  * passed in.
39327c478bd9Sstevel@tonic-gate  *
39337c478bd9Sstevel@tonic-gate  * This routine is not static because ddi_bus_prop_op() which lives in
39347c478bd9Sstevel@tonic-gate  * ddi_impl.c calls it.  No driver should be calling this routine.
39357c478bd9Sstevel@tonic-gate  */
39367c478bd9Sstevel@tonic-gate int
39377c478bd9Sstevel@tonic-gate ddi_prop_lookup_common(dev_t match_dev, dev_info_t *dip,
39387c478bd9Sstevel@tonic-gate     uint_t flags, char *name, void *data, uint_t *nelements,
39397c478bd9Sstevel@tonic-gate     int (*prop_decoder)(prop_handle_t *, void *data, uint_t *nelements))
39407c478bd9Sstevel@tonic-gate {
39417c478bd9Sstevel@tonic-gate 	int		rval;
39427c478bd9Sstevel@tonic-gate 	uint_t		ourflags;
39437c478bd9Sstevel@tonic-gate 	prop_handle_t	ph;
39447c478bd9Sstevel@tonic-gate 
39457c478bd9Sstevel@tonic-gate 	if ((match_dev == DDI_DEV_T_NONE) ||
39469ac2ddeaScth 	    (name == NULL) || (strlen(name) == 0))
39477c478bd9Sstevel@tonic-gate 		return (DDI_PROP_INVAL_ARG);
39487c478bd9Sstevel@tonic-gate 
39497c478bd9Sstevel@tonic-gate 	ourflags = (flags & DDI_PROP_DONTSLEEP) ? flags :
3950184cd04cScth 	    flags | DDI_PROP_CANSLEEP;
39517c478bd9Sstevel@tonic-gate 
39527c478bd9Sstevel@tonic-gate 	/*
39537c478bd9Sstevel@tonic-gate 	 * Get the encoded data
39547c478bd9Sstevel@tonic-gate 	 */
39557c478bd9Sstevel@tonic-gate 	bzero(&ph, sizeof (prop_handle_t));
39567c478bd9Sstevel@tonic-gate 
395765cf7c95SVikram Hegde 	if ((flags & DDI_UNBND_DLPI2) || (flags & DDI_PROP_ROOTNEX_GLOBAL)) {
39587c478bd9Sstevel@tonic-gate 		/*
395965cf7c95SVikram Hegde 		 * For rootnex and unbound dlpi style-2 devices, index into
39607c478bd9Sstevel@tonic-gate 		 * the devnames' array and search the global
39617c478bd9Sstevel@tonic-gate 		 * property list.
39627c478bd9Sstevel@tonic-gate 		 */
39637c478bd9Sstevel@tonic-gate 		ourflags &= ~DDI_UNBND_DLPI2;
39647c478bd9Sstevel@tonic-gate 		rval = i_ddi_prop_search_global(match_dev,
39657c478bd9Sstevel@tonic-gate 		    ourflags, name, &ph.ph_data, &ph.ph_size);
39667c478bd9Sstevel@tonic-gate 	} else {
39677c478bd9Sstevel@tonic-gate 		rval = ddi_prop_search_common(match_dev, dip,
39687c478bd9Sstevel@tonic-gate 		    PROP_LEN_AND_VAL_ALLOC, ourflags, name,
39697c478bd9Sstevel@tonic-gate 		    &ph.ph_data, &ph.ph_size);
39707c478bd9Sstevel@tonic-gate 
39717c478bd9Sstevel@tonic-gate 	}
39727c478bd9Sstevel@tonic-gate 
39737c478bd9Sstevel@tonic-gate 	if (rval != DDI_PROP_SUCCESS && rval != DDI_PROP_FOUND_1275) {
39747c478bd9Sstevel@tonic-gate 		ASSERT(ph.ph_data == NULL);
39757c478bd9Sstevel@tonic-gate 		ASSERT(ph.ph_size == 0);
39767c478bd9Sstevel@tonic-gate 		return (rval);
39777c478bd9Sstevel@tonic-gate 	}
39787c478bd9Sstevel@tonic-gate 
39797c478bd9Sstevel@tonic-gate 	/*
39807c478bd9Sstevel@tonic-gate 	 * If the encoded data came from a OBP or software
39817c478bd9Sstevel@tonic-gate 	 * use the 1275 OBP decode/encode routines.
39827c478bd9Sstevel@tonic-gate 	 */
39837c478bd9Sstevel@tonic-gate 	ph.ph_cur_pos = ph.ph_data;
39847c478bd9Sstevel@tonic-gate 	ph.ph_save_pos = ph.ph_data;
39857c478bd9Sstevel@tonic-gate 	ph.ph_ops = &prop_1275_ops;
39867c478bd9Sstevel@tonic-gate 	ph.ph_flags = (rval == DDI_PROP_FOUND_1275) ? PH_FROM_PROM : 0;
39877c478bd9Sstevel@tonic-gate 
39887c478bd9Sstevel@tonic-gate 	rval = (*prop_decoder)(&ph, data, nelements);
39897c478bd9Sstevel@tonic-gate 
39907c478bd9Sstevel@tonic-gate 	/*
39917c478bd9Sstevel@tonic-gate 	 * Free the encoded data
39927c478bd9Sstevel@tonic-gate 	 */
39937c478bd9Sstevel@tonic-gate 	if (ph.ph_size != 0)
39947c478bd9Sstevel@tonic-gate 		kmem_free(ph.ph_data, ph.ph_size);
39957c478bd9Sstevel@tonic-gate 
39967c478bd9Sstevel@tonic-gate 	return (rval);
39977c478bd9Sstevel@tonic-gate }
39987c478bd9Sstevel@tonic-gate 
39997c478bd9Sstevel@tonic-gate /*
40007c478bd9Sstevel@tonic-gate  * Lookup and return an array of composite properties.  The driver must
40017c478bd9Sstevel@tonic-gate  * provide the decode routine.
40027c478bd9Sstevel@tonic-gate  */
40037c478bd9Sstevel@tonic-gate int
40047c478bd9Sstevel@tonic-gate ddi_prop_lookup(dev_t match_dev, dev_info_t *dip,
40057c478bd9Sstevel@tonic-gate     uint_t flags, char *name, void *data, uint_t *nelements,
40067c478bd9Sstevel@tonic-gate     int (*prop_decoder)(prop_handle_t *, void *data, uint_t *nelements))
40077c478bd9Sstevel@tonic-gate {
40087c478bd9Sstevel@tonic-gate 	return (ddi_prop_lookup_common(match_dev, dip,
40097c478bd9Sstevel@tonic-gate 	    (flags | DDI_PROP_TYPE_COMPOSITE), name,
40107c478bd9Sstevel@tonic-gate 	    data, nelements, prop_decoder));
40117c478bd9Sstevel@tonic-gate }
40127c478bd9Sstevel@tonic-gate 
40137c478bd9Sstevel@tonic-gate /*
40147c478bd9Sstevel@tonic-gate  * Return 1 if a property exists (no type checking done).
40157c478bd9Sstevel@tonic-gate  * Return 0 if it does not exist.
40167c478bd9Sstevel@tonic-gate  */
40177c478bd9Sstevel@tonic-gate int
40187c478bd9Sstevel@tonic-gate ddi_prop_exists(dev_t match_dev, dev_info_t *dip, uint_t flags, char *name)
40197c478bd9Sstevel@tonic-gate {
40207c478bd9Sstevel@tonic-gate 	int	i;
40217c478bd9Sstevel@tonic-gate 	uint_t	x = 0;
40227c478bd9Sstevel@tonic-gate 
40237c478bd9Sstevel@tonic-gate 	i = ddi_prop_search_common(match_dev, dip, PROP_EXISTS,
4024184cd04cScth 	    flags | DDI_PROP_TYPE_MASK, name, NULL, &x);
40257c478bd9Sstevel@tonic-gate 	return (i == DDI_PROP_SUCCESS || i == DDI_PROP_FOUND_1275);
40267c478bd9Sstevel@tonic-gate }
40277c478bd9Sstevel@tonic-gate 
40287c478bd9Sstevel@tonic-gate 
40297c478bd9Sstevel@tonic-gate /*
40307c478bd9Sstevel@tonic-gate  * Update an array of composite properties.  The driver must
40317c478bd9Sstevel@tonic-gate  * provide the encode routine.
40327c478bd9Sstevel@tonic-gate  */
40337c478bd9Sstevel@tonic-gate int
40347c478bd9Sstevel@tonic-gate ddi_prop_update(dev_t match_dev, dev_info_t *dip,
40357c478bd9Sstevel@tonic-gate     char *name, void *data, uint_t nelements,
40367c478bd9Sstevel@tonic-gate     int (*prop_create)(prop_handle_t *, void *data, uint_t nelements))
40377c478bd9Sstevel@tonic-gate {
40387c478bd9Sstevel@tonic-gate 	return (ddi_prop_update_common(match_dev, dip, DDI_PROP_TYPE_COMPOSITE,
40397c478bd9Sstevel@tonic-gate 	    name, data, nelements, prop_create));
40407c478bd9Sstevel@tonic-gate }
40417c478bd9Sstevel@tonic-gate 
40427c478bd9Sstevel@tonic-gate /*
40437c478bd9Sstevel@tonic-gate  * Get a single integer or boolean property and return it.
40447c478bd9Sstevel@tonic-gate  * If the property does not exists, or cannot be decoded,
40457c478bd9Sstevel@tonic-gate  * then return the defvalue passed in.
40467c478bd9Sstevel@tonic-gate  *
40477c478bd9Sstevel@tonic-gate  * This routine always succeeds.
40487c478bd9Sstevel@tonic-gate  */
40497c478bd9Sstevel@tonic-gate int
40507c478bd9Sstevel@tonic-gate ddi_prop_get_int(dev_t match_dev, dev_info_t *dip, uint_t flags,
40517c478bd9Sstevel@tonic-gate     char *name, int defvalue)
40527c478bd9Sstevel@tonic-gate {
40537c478bd9Sstevel@tonic-gate 	int	data;
40547c478bd9Sstevel@tonic-gate 	uint_t	nelements;
40557c478bd9Sstevel@tonic-gate 	int	rval;
40567c478bd9Sstevel@tonic-gate 
40577c478bd9Sstevel@tonic-gate 	if (flags & ~(DDI_PROP_DONTPASS | DDI_PROP_NOTPROM |
405865cf7c95SVikram Hegde 	    LDI_DEV_T_ANY | DDI_UNBND_DLPI2 | DDI_PROP_ROOTNEX_GLOBAL)) {
40597c478bd9Sstevel@tonic-gate #ifdef DEBUG
40607c478bd9Sstevel@tonic-gate 		if (dip != NULL) {
40617c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "ddi_prop_get_int: invalid flag"
40627c478bd9Sstevel@tonic-gate 			    " 0x%x (prop = %s, node = %s%d)", flags,
40637c478bd9Sstevel@tonic-gate 			    name, ddi_driver_name(dip), ddi_get_instance(dip));
40647c478bd9Sstevel@tonic-gate 		}
40657c478bd9Sstevel@tonic-gate #endif /* DEBUG */
40667c478bd9Sstevel@tonic-gate 		flags &= DDI_PROP_DONTPASS | DDI_PROP_NOTPROM |
40677c478bd9Sstevel@tonic-gate 		    LDI_DEV_T_ANY | DDI_UNBND_DLPI2;
40687c478bd9Sstevel@tonic-gate 	}
40697c478bd9Sstevel@tonic-gate 
40707c478bd9Sstevel@tonic-gate 	if ((rval = ddi_prop_lookup_common(match_dev, dip,
40717c478bd9Sstevel@tonic-gate 	    (flags | DDI_PROP_TYPE_INT), name, &data, &nelements,
40727c478bd9Sstevel@tonic-gate 	    ddi_prop_fm_decode_int)) != DDI_PROP_SUCCESS) {
40737c478bd9Sstevel@tonic-gate 		if (rval == DDI_PROP_END_OF_DATA)
40747c478bd9Sstevel@tonic-gate 			data = 1;
40757c478bd9Sstevel@tonic-gate 		else
40767c478bd9Sstevel@tonic-gate 			data = defvalue;
40777c478bd9Sstevel@tonic-gate 	}
40787c478bd9Sstevel@tonic-gate 	return (data);
40797c478bd9Sstevel@tonic-gate }
40807c478bd9Sstevel@tonic-gate 
40817c478bd9Sstevel@tonic-gate /*
40827c478bd9Sstevel@tonic-gate  * Get a single 64 bit integer or boolean property and return it.
40837c478bd9Sstevel@tonic-gate  * If the property does not exists, or cannot be decoded,
40847c478bd9Sstevel@tonic-gate  * then return the defvalue passed in.
40857c478bd9Sstevel@tonic-gate  *
40867c478bd9Sstevel@tonic-gate  * This routine always succeeds.
40877c478bd9Sstevel@tonic-gate  */
40887c478bd9Sstevel@tonic-gate int64_t
40897c478bd9Sstevel@tonic-gate ddi_prop_get_int64(dev_t match_dev, dev_info_t *dip, uint_t flags,
40907c478bd9Sstevel@tonic-gate     char *name, int64_t defvalue)
40917c478bd9Sstevel@tonic-gate {
40927c478bd9Sstevel@tonic-gate 	int64_t	data;
40937c478bd9Sstevel@tonic-gate 	uint_t	nelements;
40947c478bd9Sstevel@tonic-gate 	int	rval;
40957c478bd9Sstevel@tonic-gate 
40967c478bd9Sstevel@tonic-gate 	if (flags & ~(DDI_PROP_DONTPASS | DDI_PROP_NOTPROM |
409765cf7c95SVikram Hegde 	    LDI_DEV_T_ANY | DDI_UNBND_DLPI2 | DDI_PROP_ROOTNEX_GLOBAL)) {
40987c478bd9Sstevel@tonic-gate #ifdef DEBUG
40997c478bd9Sstevel@tonic-gate 		if (dip != NULL) {
41007c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "ddi_prop_get_int64: invalid flag"
41017c478bd9Sstevel@tonic-gate 			    " 0x%x (prop = %s, node = %s%d)", flags,
41027c478bd9Sstevel@tonic-gate 			    name, ddi_driver_name(dip), ddi_get_instance(dip));
41037c478bd9Sstevel@tonic-gate 		}
41047c478bd9Sstevel@tonic-gate #endif /* DEBUG */
41057c478bd9Sstevel@tonic-gate 		return (DDI_PROP_INVAL_ARG);
41067c478bd9Sstevel@tonic-gate 	}
41077c478bd9Sstevel@tonic-gate 
41087c478bd9Sstevel@tonic-gate 	if ((rval = ddi_prop_lookup_common(match_dev, dip,
41097c478bd9Sstevel@tonic-gate 	    (flags | DDI_PROP_TYPE_INT64 | DDI_PROP_NOTPROM),
41107c478bd9Sstevel@tonic-gate 	    name, &data, &nelements, ddi_prop_fm_decode_int64))
41117c478bd9Sstevel@tonic-gate 	    != DDI_PROP_SUCCESS) {
41127c478bd9Sstevel@tonic-gate 		if (rval == DDI_PROP_END_OF_DATA)
41137c478bd9Sstevel@tonic-gate 			data = 1;
41147c478bd9Sstevel@tonic-gate 		else
41157c478bd9Sstevel@tonic-gate 			data = defvalue;
41167c478bd9Sstevel@tonic-gate 	}
41177c478bd9Sstevel@tonic-gate 	return (data);
41187c478bd9Sstevel@tonic-gate }
41197c478bd9Sstevel@tonic-gate 
41207c478bd9Sstevel@tonic-gate /*
41217c478bd9Sstevel@tonic-gate  * Get an array of integer property
41227c478bd9Sstevel@tonic-gate  */
41237c478bd9Sstevel@tonic-gate int
41247c478bd9Sstevel@tonic-gate ddi_prop_lookup_int_array(dev_t match_dev, dev_info_t *dip, uint_t flags,
41257c478bd9Sstevel@tonic-gate     char *name, int **data, uint_t *nelements)
41267c478bd9Sstevel@tonic-gate {
41277c478bd9Sstevel@tonic-gate 	if (flags & ~(DDI_PROP_DONTPASS | DDI_PROP_NOTPROM |
412865cf7c95SVikram Hegde 	    LDI_DEV_T_ANY | DDI_UNBND_DLPI2 | DDI_PROP_ROOTNEX_GLOBAL)) {
41297c478bd9Sstevel@tonic-gate #ifdef DEBUG
41307c478bd9Sstevel@tonic-gate 		if (dip != NULL) {
41317c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "ddi_prop_lookup_int_array: "
41327c478bd9Sstevel@tonic-gate 			    "invalid flag 0x%x (prop = %s, node = %s%d)",
41337c478bd9Sstevel@tonic-gate 			    flags, name, ddi_driver_name(dip),
41347c478bd9Sstevel@tonic-gate 			    ddi_get_instance(dip));
41357c478bd9Sstevel@tonic-gate 		}
41367c478bd9Sstevel@tonic-gate #endif /* DEBUG */
41377c478bd9Sstevel@tonic-gate 		flags &= DDI_PROP_DONTPASS | DDI_PROP_NOTPROM |
4138184cd04cScth 		    LDI_DEV_T_ANY | DDI_UNBND_DLPI2;
41397c478bd9Sstevel@tonic-gate 	}
41407c478bd9Sstevel@tonic-gate 
41417c478bd9Sstevel@tonic-gate 	return (ddi_prop_lookup_common(match_dev, dip,
41427c478bd9Sstevel@tonic-gate 	    (flags | DDI_PROP_TYPE_INT), name, data,
41437c478bd9Sstevel@tonic-gate 	    nelements, ddi_prop_fm_decode_ints));
41447c478bd9Sstevel@tonic-gate }
41457c478bd9Sstevel@tonic-gate 
41467c478bd9Sstevel@tonic-gate /*
41477c478bd9Sstevel@tonic-gate  * Get an array of 64 bit integer properties
41487c478bd9Sstevel@tonic-gate  */
41497c478bd9Sstevel@tonic-gate int
41507c478bd9Sstevel@tonic-gate ddi_prop_lookup_int64_array(dev_t match_dev, dev_info_t *dip, uint_t flags,
41517c478bd9Sstevel@tonic-gate     char *name, int64_t **data, uint_t *nelements)
41527c478bd9Sstevel@tonic-gate {
41537c478bd9Sstevel@tonic-gate 	if (flags & ~(DDI_PROP_DONTPASS | DDI_PROP_NOTPROM |
415465cf7c95SVikram Hegde 	    LDI_DEV_T_ANY | DDI_UNBND_DLPI2 | DDI_PROP_ROOTNEX_GLOBAL)) {
41557c478bd9Sstevel@tonic-gate #ifdef DEBUG
41567c478bd9Sstevel@tonic-gate 		if (dip != NULL) {
41577c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "ddi_prop_lookup_int64_array: "
41587c478bd9Sstevel@tonic-gate 			    "invalid flag 0x%x (prop = %s, node = %s%d)",
41597c478bd9Sstevel@tonic-gate 			    flags, name, ddi_driver_name(dip),
41607c478bd9Sstevel@tonic-gate 			    ddi_get_instance(dip));
41617c478bd9Sstevel@tonic-gate 		}
41627c478bd9Sstevel@tonic-gate #endif /* DEBUG */
41637c478bd9Sstevel@tonic-gate 		return (DDI_PROP_INVAL_ARG);
41647c478bd9Sstevel@tonic-gate 	}
41657c478bd9Sstevel@tonic-gate 
41667c478bd9Sstevel@tonic-gate 	return (ddi_prop_lookup_common(match_dev, dip,
41677c478bd9Sstevel@tonic-gate 	    (flags | DDI_PROP_TYPE_INT64 | DDI_PROP_NOTPROM),
41687c478bd9Sstevel@tonic-gate 	    name, data, nelements, ddi_prop_fm_decode_int64_array));
41697c478bd9Sstevel@tonic-gate }
41707c478bd9Sstevel@tonic-gate 
41717c478bd9Sstevel@tonic-gate /*
41727c478bd9Sstevel@tonic-gate  * Update a single integer property.  If the property exists on the drivers
41737c478bd9Sstevel@tonic-gate  * property list it updates, else it creates it.
41747c478bd9Sstevel@tonic-gate  */
41757c478bd9Sstevel@tonic-gate int
41767c478bd9Sstevel@tonic-gate ddi_prop_update_int(dev_t match_dev, dev_info_t *dip,
41777c478bd9Sstevel@tonic-gate     char *name, int data)
41787c478bd9Sstevel@tonic-gate {
41797c478bd9Sstevel@tonic-gate 	return (ddi_prop_update_common(match_dev, dip, DDI_PROP_TYPE_INT,
41807c478bd9Sstevel@tonic-gate 	    name, &data, 1, ddi_prop_fm_encode_ints));
41817c478bd9Sstevel@tonic-gate }
41827c478bd9Sstevel@tonic-gate 
41837c478bd9Sstevel@tonic-gate /*
41847c478bd9Sstevel@tonic-gate  * Update a single 64 bit integer property.
41857c478bd9Sstevel@tonic-gate  * Update the driver property list if it exists, else create it.
41867c478bd9Sstevel@tonic-gate  */
41877c478bd9Sstevel@tonic-gate int
41887c478bd9Sstevel@tonic-gate ddi_prop_update_int64(dev_t match_dev, dev_info_t *dip,
41897c478bd9Sstevel@tonic-gate     char *name, int64_t data)
41907c478bd9Sstevel@tonic-gate {
41917c478bd9Sstevel@tonic-gate 	return (ddi_prop_update_common(match_dev, dip, DDI_PROP_TYPE_INT64,
41927c478bd9Sstevel@tonic-gate 	    name, &data, 1, ddi_prop_fm_encode_int64));
41937c478bd9Sstevel@tonic-gate }
41947c478bd9Sstevel@tonic-gate 
41957c478bd9Sstevel@tonic-gate int
41967c478bd9Sstevel@tonic-gate e_ddi_prop_update_int(dev_t match_dev, dev_info_t *dip,
41977c478bd9Sstevel@tonic-gate     char *name, int data)
41987c478bd9Sstevel@tonic-gate {
41997c478bd9Sstevel@tonic-gate 	return (ddi_prop_update_common(match_dev, dip,
42007c478bd9Sstevel@tonic-gate 	    DDI_PROP_SYSTEM_DEF | DDI_PROP_TYPE_INT,
42017c478bd9Sstevel@tonic-gate 	    name, &data, 1, ddi_prop_fm_encode_ints));
42027c478bd9Sstevel@tonic-gate }
42037c478bd9Sstevel@tonic-gate 
42047c478bd9Sstevel@tonic-gate int
42057c478bd9Sstevel@tonic-gate e_ddi_prop_update_int64(dev_t match_dev, dev_info_t *dip,
42067c478bd9Sstevel@tonic-gate     char *name, int64_t data)
42077c478bd9Sstevel@tonic-gate {
42087c478bd9Sstevel@tonic-gate 	return (ddi_prop_update_common(match_dev, dip,
42097c478bd9Sstevel@tonic-gate 	    DDI_PROP_SYSTEM_DEF | DDI_PROP_TYPE_INT64,
42107c478bd9Sstevel@tonic-gate 	    name, &data, 1, ddi_prop_fm_encode_int64));
42117c478bd9Sstevel@tonic-gate }
42127c478bd9Sstevel@tonic-gate 
42137c478bd9Sstevel@tonic-gate /*
42147c478bd9Sstevel@tonic-gate  * Update an array of integer property.  If the property exists on the drivers
42157c478bd9Sstevel@tonic-gate  * property list it updates, else it creates it.
42167c478bd9Sstevel@tonic-gate  */
42177c478bd9Sstevel@tonic-gate int
42187c478bd9Sstevel@tonic-gate ddi_prop_update_int_array(dev_t match_dev, dev_info_t *dip,
42197c478bd9Sstevel@tonic-gate     char *name, int *data, uint_t nelements)
42207c478bd9Sstevel@tonic-gate {
42217c478bd9Sstevel@tonic-gate 	return (ddi_prop_update_common(match_dev, dip, DDI_PROP_TYPE_INT,
42227c478bd9Sstevel@tonic-gate 	    name, data, nelements, ddi_prop_fm_encode_ints));
42237c478bd9Sstevel@tonic-gate }
42247c478bd9Sstevel@tonic-gate 
42257c478bd9Sstevel@tonic-gate /*
42267c478bd9Sstevel@tonic-gate  * Update an array of 64 bit integer properties.
42277c478bd9Sstevel@tonic-gate  * Update the driver property list if it exists, else create it.
42287c478bd9Sstevel@tonic-gate  */
42297c478bd9Sstevel@tonic-gate int
42307c478bd9Sstevel@tonic-gate ddi_prop_update_int64_array(dev_t match_dev, dev_info_t *dip,
42317c478bd9Sstevel@tonic-gate     char *name, int64_t *data, uint_t nelements)
42327c478bd9Sstevel@tonic-gate {
42337c478bd9Sstevel@tonic-gate 	return (ddi_prop_update_common(match_dev, dip, DDI_PROP_TYPE_INT64,
42347c478bd9Sstevel@tonic-gate 	    name, data, nelements, ddi_prop_fm_encode_int64));
42357c478bd9Sstevel@tonic-gate }
42367c478bd9Sstevel@tonic-gate 
42377c478bd9Sstevel@tonic-gate int
42387c478bd9Sstevel@tonic-gate e_ddi_prop_update_int64_array(dev_t match_dev, dev_info_t *dip,
42397c478bd9Sstevel@tonic-gate     char *name, int64_t *data, uint_t nelements)
42407c478bd9Sstevel@tonic-gate {
42417c478bd9Sstevel@tonic-gate 	return (ddi_prop_update_common(match_dev, dip,
42427c478bd9Sstevel@tonic-gate 	    DDI_PROP_SYSTEM_DEF | DDI_PROP_TYPE_INT64,
42437c478bd9Sstevel@tonic-gate 	    name, data, nelements, ddi_prop_fm_encode_int64));
42447c478bd9Sstevel@tonic-gate }
42457c478bd9Sstevel@tonic-gate 
42467c478bd9Sstevel@tonic-gate int
42477c478bd9Sstevel@tonic-gate e_ddi_prop_update_int_array(dev_t match_dev, dev_info_t *dip,
42487c478bd9Sstevel@tonic-gate     char *name, int *data, uint_t nelements)
42497c478bd9Sstevel@tonic-gate {
42507c478bd9Sstevel@tonic-gate 	return (ddi_prop_update_common(match_dev, dip,
42517c478bd9Sstevel@tonic-gate 	    DDI_PROP_SYSTEM_DEF | DDI_PROP_TYPE_INT,
42527c478bd9Sstevel@tonic-gate 	    name, data, nelements, ddi_prop_fm_encode_ints));
42537c478bd9Sstevel@tonic-gate }
42547c478bd9Sstevel@tonic-gate 
42557c478bd9Sstevel@tonic-gate /*
42567c478bd9Sstevel@tonic-gate  * Get a single string property.
42577c478bd9Sstevel@tonic-gate  */
42587c478bd9Sstevel@tonic-gate int
42597c478bd9Sstevel@tonic-gate ddi_prop_lookup_string(dev_t match_dev, dev_info_t *dip, uint_t flags,
42607c478bd9Sstevel@tonic-gate     char *name, char **data)
42617c478bd9Sstevel@tonic-gate {
42627c478bd9Sstevel@tonic-gate 	uint_t x;
42637c478bd9Sstevel@tonic-gate 
42647c478bd9Sstevel@tonic-gate 	if (flags & ~(DDI_PROP_DONTPASS | DDI_PROP_NOTPROM |
426565cf7c95SVikram Hegde 	    LDI_DEV_T_ANY | DDI_UNBND_DLPI2 | DDI_PROP_ROOTNEX_GLOBAL)) {
42667c478bd9Sstevel@tonic-gate #ifdef DEBUG
42677c478bd9Sstevel@tonic-gate 		if (dip != NULL) {
42687c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s: invalid flag 0x%x "
42697c478bd9Sstevel@tonic-gate 			    "(prop = %s, node = %s%d); invalid bits ignored",
42707c478bd9Sstevel@tonic-gate 			    "ddi_prop_lookup_string", flags, name,
42717c478bd9Sstevel@tonic-gate 			    ddi_driver_name(dip), ddi_get_instance(dip));
42727c478bd9Sstevel@tonic-gate 		}
42737c478bd9Sstevel@tonic-gate #endif /* DEBUG */
42747c478bd9Sstevel@tonic-gate 		flags &= DDI_PROP_DONTPASS | DDI_PROP_NOTPROM |
42757c478bd9Sstevel@tonic-gate 		    LDI_DEV_T_ANY | DDI_UNBND_DLPI2;
42767c478bd9Sstevel@tonic-gate 	}
42777c478bd9Sstevel@tonic-gate 
42787c478bd9Sstevel@tonic-gate 	return (ddi_prop_lookup_common(match_dev, dip,
42797c478bd9Sstevel@tonic-gate 	    (flags | DDI_PROP_TYPE_STRING), name, data,
42807c478bd9Sstevel@tonic-gate 	    &x, ddi_prop_fm_decode_string));
42817c478bd9Sstevel@tonic-gate }
42827c478bd9Sstevel@tonic-gate 
42837c478bd9Sstevel@tonic-gate /*
42847c478bd9Sstevel@tonic-gate  * Get an array of strings property.
42857c478bd9Sstevel@tonic-gate  */
42867c478bd9Sstevel@tonic-gate int
42877c478bd9Sstevel@tonic-gate ddi_prop_lookup_string_array(dev_t match_dev, dev_info_t *dip, uint_t flags,
42887c478bd9Sstevel@tonic-gate     char *name, char ***data, uint_t *nelements)
42897c478bd9Sstevel@tonic-gate {
42907c478bd9Sstevel@tonic-gate 	if (flags & ~(DDI_PROP_DONTPASS | DDI_PROP_NOTPROM |
429165cf7c95SVikram Hegde 	    LDI_DEV_T_ANY | DDI_UNBND_DLPI2 | DDI_PROP_ROOTNEX_GLOBAL)) {
42927c478bd9Sstevel@tonic-gate #ifdef DEBUG
42937c478bd9Sstevel@tonic-gate 		if (dip != NULL) {
42947c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "ddi_prop_lookup_string_array: "
42957c478bd9Sstevel@tonic-gate 			    "invalid flag 0x%x (prop = %s, node = %s%d)",
42967c478bd9Sstevel@tonic-gate 			    flags, name, ddi_driver_name(dip),
42977c478bd9Sstevel@tonic-gate 			    ddi_get_instance(dip));
42987c478bd9Sstevel@tonic-gate 		}
42997c478bd9Sstevel@tonic-gate #endif /* DEBUG */
43007c478bd9Sstevel@tonic-gate 		flags &= DDI_PROP_DONTPASS | DDI_PROP_NOTPROM |
43017c478bd9Sstevel@tonic-gate 		    LDI_DEV_T_ANY | DDI_UNBND_DLPI2;
43027c478bd9Sstevel@tonic-gate 	}
43037c478bd9Sstevel@tonic-gate 
43047c478bd9Sstevel@tonic-gate 	return (ddi_prop_lookup_common(match_dev, dip,
43057c478bd9Sstevel@tonic-gate 	    (flags | DDI_PROP_TYPE_STRING), name, data,
43067c478bd9Sstevel@tonic-gate 	    nelements, ddi_prop_fm_decode_strings));
43077c478bd9Sstevel@tonic-gate }
43087c478bd9Sstevel@tonic-gate 
43097c478bd9Sstevel@tonic-gate /*
43107c478bd9Sstevel@tonic-gate  * Update a single string property.
43117c478bd9Sstevel@tonic-gate  */
43127c478bd9Sstevel@tonic-gate int
43137c478bd9Sstevel@tonic-gate ddi_prop_update_string(dev_t match_dev, dev_info_t *dip,
43147c478bd9Sstevel@tonic-gate     char *name, char *data)
43157c478bd9Sstevel@tonic-gate {
43167c478bd9Sstevel@tonic-gate 	return (ddi_prop_update_common(match_dev, dip,
43177c478bd9Sstevel@tonic-gate 	    DDI_PROP_TYPE_STRING, name, &data, 1,
43187c478bd9Sstevel@tonic-gate 	    ddi_prop_fm_encode_string));
43197c478bd9Sstevel@tonic-gate }
43207c478bd9Sstevel@tonic-gate 
43217c478bd9Sstevel@tonic-gate int
43227c478bd9Sstevel@tonic-gate e_ddi_prop_update_string(dev_t match_dev, dev_info_t *dip,
43237c478bd9Sstevel@tonic-gate     char *name, char *data)
43247c478bd9Sstevel@tonic-gate {
43257c478bd9Sstevel@tonic-gate 	return (ddi_prop_update_common(match_dev, dip,
43267c478bd9Sstevel@tonic-gate 	    DDI_PROP_SYSTEM_DEF | DDI_PROP_TYPE_STRING,
43277c478bd9Sstevel@tonic-gate 	    name, &data, 1, ddi_prop_fm_encode_string));
43287c478bd9Sstevel@tonic-gate }
43297c478bd9Sstevel@tonic-gate 
43307c478bd9Sstevel@tonic-gate 
43317c478bd9Sstevel@tonic-gate /*
43327c478bd9Sstevel@tonic-gate  * Update an array of strings property.
43337c478bd9Sstevel@tonic-gate  */
43347c478bd9Sstevel@tonic-gate int
43357c478bd9Sstevel@tonic-gate ddi_prop_update_string_array(dev_t match_dev, dev_info_t *dip,
43367c478bd9Sstevel@tonic-gate     char *name, char **data, uint_t nelements)
43377c478bd9Sstevel@tonic-gate {
43387c478bd9Sstevel@tonic-gate 	return (ddi_prop_update_common(match_dev, dip,
43397c478bd9Sstevel@tonic-gate 	    DDI_PROP_TYPE_STRING, name, data, nelements,
43407c478bd9Sstevel@tonic-gate 	    ddi_prop_fm_encode_strings));
43417c478bd9Sstevel@tonic-gate }
43427c478bd9Sstevel@tonic-gate 
43437c478bd9Sstevel@tonic-gate int
43447c478bd9Sstevel@tonic-gate e_ddi_prop_update_string_array(dev_t match_dev, dev_info_t *dip,
43457c478bd9Sstevel@tonic-gate     char *name, char **data, uint_t nelements)
43467c478bd9Sstevel@tonic-gate {
43477c478bd9Sstevel@tonic-gate 	return (ddi_prop_update_common(match_dev, dip,
43487c478bd9Sstevel@tonic-gate 	    DDI_PROP_SYSTEM_DEF | DDI_PROP_TYPE_STRING,
43497c478bd9Sstevel@tonic-gate 	    name, data, nelements,
43507c478bd9Sstevel@tonic-gate 	    ddi_prop_fm_encode_strings));
43517c478bd9Sstevel@tonic-gate }
43527c478bd9Sstevel@tonic-gate 
43537c478bd9Sstevel@tonic-gate 
43547c478bd9Sstevel@tonic-gate /*
43557c478bd9Sstevel@tonic-gate  * Get an array of bytes property.
43567c478bd9Sstevel@tonic-gate  */
43577c478bd9Sstevel@tonic-gate int
43587c478bd9Sstevel@tonic-gate ddi_prop_lookup_byte_array(dev_t match_dev, dev_info_t *dip, uint_t flags,
43597c478bd9Sstevel@tonic-gate     char *name, uchar_t **data, uint_t *nelements)
43607c478bd9Sstevel@tonic-gate {
43617c478bd9Sstevel@tonic-gate 	if (flags & ~(DDI_PROP_DONTPASS | DDI_PROP_NOTPROM |
436265cf7c95SVikram Hegde 	    LDI_DEV_T_ANY | DDI_UNBND_DLPI2 | DDI_PROP_ROOTNEX_GLOBAL)) {
43637c478bd9Sstevel@tonic-gate #ifdef DEBUG
43647c478bd9Sstevel@tonic-gate 		if (dip != NULL) {
43657c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "ddi_prop_lookup_byte_array: "
43667c478bd9Sstevel@tonic-gate 			    " invalid flag 0x%x (prop = %s, node = %s%d)",
43677c478bd9Sstevel@tonic-gate 			    flags, name, ddi_driver_name(dip),
43687c478bd9Sstevel@tonic-gate 			    ddi_get_instance(dip));
43697c478bd9Sstevel@tonic-gate 		}
43707c478bd9Sstevel@tonic-gate #endif /* DEBUG */
43717c478bd9Sstevel@tonic-gate 		flags &= DDI_PROP_DONTPASS | DDI_PROP_NOTPROM |
43727c478bd9Sstevel@tonic-gate 		    LDI_DEV_T_ANY | DDI_UNBND_DLPI2;
43737c478bd9Sstevel@tonic-gate 	}
43747c478bd9Sstevel@tonic-gate 
43757c478bd9Sstevel@tonic-gate 	return (ddi_prop_lookup_common(match_dev, dip,
43767c478bd9Sstevel@tonic-gate 	    (flags | DDI_PROP_TYPE_BYTE), name, data,
43777c478bd9Sstevel@tonic-gate 	    nelements, ddi_prop_fm_decode_bytes));
43787c478bd9Sstevel@tonic-gate }
43797c478bd9Sstevel@tonic-gate 
43807c478bd9Sstevel@tonic-gate /*
43817c478bd9Sstevel@tonic-gate  * Update an array of bytes property.
43827c478bd9Sstevel@tonic-gate  */
43837c478bd9Sstevel@tonic-gate int
43847c478bd9Sstevel@tonic-gate ddi_prop_update_byte_array(dev_t match_dev, dev_info_t *dip,
43857c478bd9Sstevel@tonic-gate     char *name, uchar_t *data, uint_t nelements)
43867c478bd9Sstevel@tonic-gate {
43877c478bd9Sstevel@tonic-gate 	if (nelements == 0)
43887c478bd9Sstevel@tonic-gate 		return (DDI_PROP_INVAL_ARG);
43897c478bd9Sstevel@tonic-gate 
43907c478bd9Sstevel@tonic-gate 	return (ddi_prop_update_common(match_dev, dip, DDI_PROP_TYPE_BYTE,
43917c478bd9Sstevel@tonic-gate 	    name, data, nelements, ddi_prop_fm_encode_bytes));
43927c478bd9Sstevel@tonic-gate }
43937c478bd9Sstevel@tonic-gate 
43947c478bd9Sstevel@tonic-gate 
43957c478bd9Sstevel@tonic-gate int
43967c478bd9Sstevel@tonic-gate e_ddi_prop_update_byte_array(dev_t match_dev, dev_info_t *dip,
43977c478bd9Sstevel@tonic-gate     char *name, uchar_t *data, uint_t nelements)
43987c478bd9Sstevel@tonic-gate {
43997c478bd9Sstevel@tonic-gate 	if (nelements == 0)
44007c478bd9Sstevel@tonic-gate 		return (DDI_PROP_INVAL_ARG);
44017c478bd9Sstevel@tonic-gate 
44027c478bd9Sstevel@tonic-gate 	return (ddi_prop_update_common(match_dev, dip,
44037c478bd9Sstevel@tonic-gate 	    DDI_PROP_SYSTEM_DEF | DDI_PROP_TYPE_BYTE,
44047c478bd9Sstevel@tonic-gate 	    name, data, nelements, ddi_prop_fm_encode_bytes));
44057c478bd9Sstevel@tonic-gate }
44067c478bd9Sstevel@tonic-gate 
44077c478bd9Sstevel@tonic-gate 
44087c478bd9Sstevel@tonic-gate /*
44097c478bd9Sstevel@tonic-gate  * ddi_prop_remove_common:	Undefine a managed property:
44107c478bd9Sstevel@tonic-gate  *			Input dev_t must match dev_t when defined.
44117c478bd9Sstevel@tonic-gate  *			Returns DDI_PROP_NOT_FOUND, possibly.
44127c478bd9Sstevel@tonic-gate  *			DDI_PROP_INVAL_ARG is also possible if dev is
44137c478bd9Sstevel@tonic-gate  *			DDI_DEV_T_ANY or incoming name is the NULL string.
44147c478bd9Sstevel@tonic-gate  */
44157c478bd9Sstevel@tonic-gate int
44167c478bd9Sstevel@tonic-gate ddi_prop_remove_common(dev_t dev, dev_info_t *dip, char *name, int flag)
44177c478bd9Sstevel@tonic-gate {
44187c478bd9Sstevel@tonic-gate 	ddi_prop_t	**list_head = &(DEVI(dip)->devi_drv_prop_ptr);
44197c478bd9Sstevel@tonic-gate 	ddi_prop_t	*propp;
44207c478bd9Sstevel@tonic-gate 	ddi_prop_t	*lastpropp = NULL;
44217c478bd9Sstevel@tonic-gate 
44227c478bd9Sstevel@tonic-gate 	if ((dev == DDI_DEV_T_ANY) || (name == (char *)0) ||
44237c478bd9Sstevel@tonic-gate 	    (strlen(name) == 0)) {
44247c478bd9Sstevel@tonic-gate 		return (DDI_PROP_INVAL_ARG);
44257c478bd9Sstevel@tonic-gate 	}
44267c478bd9Sstevel@tonic-gate 
44277c478bd9Sstevel@tonic-gate 	if (flag & DDI_PROP_SYSTEM_DEF)
44287c478bd9Sstevel@tonic-gate 		list_head = &(DEVI(dip)->devi_sys_prop_ptr);
44297c478bd9Sstevel@tonic-gate 	else if (flag & DDI_PROP_HW_DEF)
44307c478bd9Sstevel@tonic-gate 		list_head = &(DEVI(dip)->devi_hw_prop_ptr);
44317c478bd9Sstevel@tonic-gate 
44327c478bd9Sstevel@tonic-gate 	mutex_enter(&(DEVI(dip)->devi_lock));
44337c478bd9Sstevel@tonic-gate 
44347c478bd9Sstevel@tonic-gate 	for (propp = *list_head; propp != NULL; propp = propp->prop_next)  {
44352cd7878fScth 		if (DDI_STRSAME(propp->prop_name, name) &&
44367c478bd9Sstevel@tonic-gate 		    (dev == propp->prop_dev)) {
44377c478bd9Sstevel@tonic-gate 			/*
44387c478bd9Sstevel@tonic-gate 			 * Unlink this propp allowing for it to
44397c478bd9Sstevel@tonic-gate 			 * be first in the list:
44407c478bd9Sstevel@tonic-gate 			 */
44417c478bd9Sstevel@tonic-gate 
44427c478bd9Sstevel@tonic-gate 			if (lastpropp == NULL)
44437c478bd9Sstevel@tonic-gate 				*list_head = propp->prop_next;
44447c478bd9Sstevel@tonic-gate 			else
44457c478bd9Sstevel@tonic-gate 				lastpropp->prop_next = propp->prop_next;
44467c478bd9Sstevel@tonic-gate 
44477c478bd9Sstevel@tonic-gate 			mutex_exit(&(DEVI(dip)->devi_lock));
44487c478bd9Sstevel@tonic-gate 
44497c478bd9Sstevel@tonic-gate 			/*
44507c478bd9Sstevel@tonic-gate 			 * Free memory and return...
44517c478bd9Sstevel@tonic-gate 			 */
44527c478bd9Sstevel@tonic-gate 			kmem_free(propp->prop_name,
44537c478bd9Sstevel@tonic-gate 			    strlen(propp->prop_name) + 1);
44547c478bd9Sstevel@tonic-gate 			if (propp->prop_len != 0)
44557c478bd9Sstevel@tonic-gate 				kmem_free(propp->prop_val, propp->prop_len);
44567c478bd9Sstevel@tonic-gate 			kmem_free(propp, sizeof (ddi_prop_t));
44577c478bd9Sstevel@tonic-gate 			return (DDI_PROP_SUCCESS);
44587c478bd9Sstevel@tonic-gate 		}
44597c478bd9Sstevel@tonic-gate 		lastpropp = propp;
44607c478bd9Sstevel@tonic-gate 	}
44617c478bd9Sstevel@tonic-gate 	mutex_exit(&(DEVI(dip)->devi_lock));
44627c478bd9Sstevel@tonic-gate 	return (DDI_PROP_NOT_FOUND);
44637c478bd9Sstevel@tonic-gate }
44647c478bd9Sstevel@tonic-gate 
44657c478bd9Sstevel@tonic-gate int
44667c478bd9Sstevel@tonic-gate ddi_prop_remove(dev_t dev, dev_info_t *dip, char *name)
44677c478bd9Sstevel@tonic-gate {
44687c478bd9Sstevel@tonic-gate 	return (ddi_prop_remove_common(dev, dip, name, 0));
44697c478bd9Sstevel@tonic-gate }
44707c478bd9Sstevel@tonic-gate 
44717c478bd9Sstevel@tonic-gate int
44727c478bd9Sstevel@tonic-gate e_ddi_prop_remove(dev_t dev, dev_info_t *dip, char *name)
44737c478bd9Sstevel@tonic-gate {
44747c478bd9Sstevel@tonic-gate 	return (ddi_prop_remove_common(dev, dip, name, DDI_PROP_SYSTEM_DEF));
44757c478bd9Sstevel@tonic-gate }
44767c478bd9Sstevel@tonic-gate 
44777c478bd9Sstevel@tonic-gate /*
44787c478bd9Sstevel@tonic-gate  * e_ddi_prop_list_delete: remove a list of properties
44797c478bd9Sstevel@tonic-gate  *	Note that the caller needs to provide the required protection
44807c478bd9Sstevel@tonic-gate  *	(eg. devi_lock if these properties are still attached to a devi)
44817c478bd9Sstevel@tonic-gate  */
44827c478bd9Sstevel@tonic-gate void
44837c478bd9Sstevel@tonic-gate e_ddi_prop_list_delete(ddi_prop_t *props)
44847c478bd9Sstevel@tonic-gate {
44857c478bd9Sstevel@tonic-gate 	i_ddi_prop_list_delete(props);
44867c478bd9Sstevel@tonic-gate }
44877c478bd9Sstevel@tonic-gate 
44887c478bd9Sstevel@tonic-gate /*
44897c478bd9Sstevel@tonic-gate  * ddi_prop_remove_all_common:
44907c478bd9Sstevel@tonic-gate  *	Used before unloading a driver to remove
44917c478bd9Sstevel@tonic-gate  *	all properties. (undefines all dev_t's props.)
44927c478bd9Sstevel@tonic-gate  *	Also removes `explicitly undefined' props.
44937c478bd9Sstevel@tonic-gate  *	No errors possible.
44947c478bd9Sstevel@tonic-gate  */
44957c478bd9Sstevel@tonic-gate void
44967c478bd9Sstevel@tonic-gate ddi_prop_remove_all_common(dev_info_t *dip, int flag)
44977c478bd9Sstevel@tonic-gate {
44987c478bd9Sstevel@tonic-gate 	ddi_prop_t	**list_head;
44997c478bd9Sstevel@tonic-gate 
45007c478bd9Sstevel@tonic-gate 	mutex_enter(&(DEVI(dip)->devi_lock));
45017c478bd9Sstevel@tonic-gate 	if (flag & DDI_PROP_SYSTEM_DEF) {
45027c478bd9Sstevel@tonic-gate 		list_head = &(DEVI(dip)->devi_sys_prop_ptr);
45037c478bd9Sstevel@tonic-gate 	} else if (flag & DDI_PROP_HW_DEF) {
45047c478bd9Sstevel@tonic-gate 		list_head = &(DEVI(dip)->devi_hw_prop_ptr);
45057c478bd9Sstevel@tonic-gate 	} else {
45067c478bd9Sstevel@tonic-gate 		list_head = &(DEVI(dip)->devi_drv_prop_ptr);
45077c478bd9Sstevel@tonic-gate 	}
45087c478bd9Sstevel@tonic-gate 	i_ddi_prop_list_delete(*list_head);
45097c478bd9Sstevel@tonic-gate 	*list_head = NULL;
45107c478bd9Sstevel@tonic-gate 	mutex_exit(&(DEVI(dip)->devi_lock));
45117c478bd9Sstevel@tonic-gate }
45127c478bd9Sstevel@tonic-gate 
45137c478bd9Sstevel@tonic-gate 
45147c478bd9Sstevel@tonic-gate /*
45157c478bd9Sstevel@tonic-gate  * ddi_prop_remove_all:		Remove all driver prop definitions.
45167c478bd9Sstevel@tonic-gate  */
45177c478bd9Sstevel@tonic-gate 
45187c478bd9Sstevel@tonic-gate void
45197c478bd9Sstevel@tonic-gate ddi_prop_remove_all(dev_info_t *dip)
45207c478bd9Sstevel@tonic-gate {
4521b9ccdc5aScth 	i_ddi_prop_dyn_driver_set(dip, NULL);
45227c478bd9Sstevel@tonic-gate 	ddi_prop_remove_all_common(dip, 0);
45237c478bd9Sstevel@tonic-gate }
45247c478bd9Sstevel@tonic-gate 
45257c478bd9Sstevel@tonic-gate /*
45267c478bd9Sstevel@tonic-gate  * e_ddi_prop_remove_all:	Remove all system prop definitions.
45277c478bd9Sstevel@tonic-gate  */
45287c478bd9Sstevel@tonic-gate 
45297c478bd9Sstevel@tonic-gate void
45307c478bd9Sstevel@tonic-gate e_ddi_prop_remove_all(dev_info_t *dip)
45317c478bd9Sstevel@tonic-gate {
45327c478bd9Sstevel@tonic-gate 	ddi_prop_remove_all_common(dip, (int)DDI_PROP_SYSTEM_DEF);
45337c478bd9Sstevel@tonic-gate }
45347c478bd9Sstevel@tonic-gate 
45357c478bd9Sstevel@tonic-gate 
45367c478bd9Sstevel@tonic-gate /*
45377c478bd9Sstevel@tonic-gate  * ddi_prop_undefine:	Explicitly undefine a property.  Property
45387c478bd9Sstevel@tonic-gate  *			searches which match this property return
45397c478bd9Sstevel@tonic-gate  *			the error code DDI_PROP_UNDEFINED.
45407c478bd9Sstevel@tonic-gate  *
45417c478bd9Sstevel@tonic-gate  *			Use ddi_prop_remove to negate effect of
45427c478bd9Sstevel@tonic-gate  *			ddi_prop_undefine
45437c478bd9Sstevel@tonic-gate  *
45447c478bd9Sstevel@tonic-gate  *			See above for error returns.
45457c478bd9Sstevel@tonic-gate  */
45467c478bd9Sstevel@tonic-gate 
45477c478bd9Sstevel@tonic-gate int
45487c478bd9Sstevel@tonic-gate ddi_prop_undefine(dev_t dev, dev_info_t *dip, int flag, char *name)
45497c478bd9Sstevel@tonic-gate {
45507c478bd9Sstevel@tonic-gate 	if (!(flag & DDI_PROP_CANSLEEP))
45517c478bd9Sstevel@tonic-gate 		flag |= DDI_PROP_DONTSLEEP;
4552b9ccdc5aScth 	flag |= DDI_PROP_STACK_CREATE | DDI_PROP_UNDEF_IT | DDI_PROP_TYPE_ANY;
4553b9ccdc5aScth 	return (ddi_prop_update_common(dev, dip, flag,
4554b9ccdc5aScth 	    name, NULL, 0, ddi_prop_fm_encode_bytes));
45557c478bd9Sstevel@tonic-gate }
45567c478bd9Sstevel@tonic-gate 
45577c478bd9Sstevel@tonic-gate int
45587c478bd9Sstevel@tonic-gate e_ddi_prop_undefine(dev_t dev, dev_info_t *dip, int flag, char *name)
45597c478bd9Sstevel@tonic-gate {
45607c478bd9Sstevel@tonic-gate 	if (!(flag & DDI_PROP_CANSLEEP))
45617c478bd9Sstevel@tonic-gate 		flag |= DDI_PROP_DONTSLEEP;
4562b9ccdc5aScth 	flag |= DDI_PROP_SYSTEM_DEF | DDI_PROP_STACK_CREATE |
4563b9ccdc5aScth 	    DDI_PROP_UNDEF_IT | DDI_PROP_TYPE_ANY;
4564b9ccdc5aScth 	return (ddi_prop_update_common(dev, dip, flag,
45657c478bd9Sstevel@tonic-gate 	    name, NULL, 0, ddi_prop_fm_encode_bytes));
45667c478bd9Sstevel@tonic-gate }
45677c478bd9Sstevel@tonic-gate 
4568b9ccdc5aScth /*
4569b9ccdc5aScth  * Support for gathering dynamic properties in devinfo snapshot.
4570b9ccdc5aScth  */
4571b9ccdc5aScth void
4572b9ccdc5aScth i_ddi_prop_dyn_driver_set(dev_info_t *dip, i_ddi_prop_dyn_t *dp)
4573b9ccdc5aScth {
4574b9ccdc5aScth 	DEVI(dip)->devi_prop_dyn_driver = dp;
4575b9ccdc5aScth }
4576b9ccdc5aScth 
4577b9ccdc5aScth i_ddi_prop_dyn_t *
4578b9ccdc5aScth i_ddi_prop_dyn_driver_get(dev_info_t *dip)
4579b9ccdc5aScth {
4580b9ccdc5aScth 	return (DEVI(dip)->devi_prop_dyn_driver);
4581b9ccdc5aScth }
4582b9ccdc5aScth 
4583b9ccdc5aScth void
4584b9ccdc5aScth i_ddi_prop_dyn_parent_set(dev_info_t *dip, i_ddi_prop_dyn_t *dp)
4585b9ccdc5aScth {
4586b9ccdc5aScth 	DEVI(dip)->devi_prop_dyn_parent = dp;
4587b9ccdc5aScth }
4588b9ccdc5aScth 
4589b9ccdc5aScth i_ddi_prop_dyn_t *
4590b9ccdc5aScth i_ddi_prop_dyn_parent_get(dev_info_t *dip)
4591b9ccdc5aScth {
4592b9ccdc5aScth 	return (DEVI(dip)->devi_prop_dyn_parent);
4593b9ccdc5aScth }
4594b9ccdc5aScth 
4595b9ccdc5aScth void
4596b9ccdc5aScth i_ddi_prop_dyn_cache_invalidate(dev_info_t *dip, i_ddi_prop_dyn_t *dp)
4597b9ccdc5aScth {
4598b9ccdc5aScth 	/* for now we invalidate the entire cached snapshot */
4599b9ccdc5aScth 	if (dip && dp)
4600*4c06356bSdh 		i_ddi_di_cache_invalidate();
4601b9ccdc5aScth }
4602b9ccdc5aScth 
4603b9ccdc5aScth /* ARGSUSED */
4604b9ccdc5aScth void
4605b9ccdc5aScth ddi_prop_cache_invalidate(dev_t dev, dev_info_t *dip, char *name, int flags)
4606b9ccdc5aScth {
4607b9ccdc5aScth 	/* for now we invalidate the entire cached snapshot */
4608*4c06356bSdh 	i_ddi_di_cache_invalidate();
4609b9ccdc5aScth }
4610b9ccdc5aScth 
4611b9ccdc5aScth 
46127c478bd9Sstevel@tonic-gate /*
46137c478bd9Sstevel@tonic-gate  * Code to search hardware layer (PROM), if it exists, on behalf of child.
46147c478bd9Sstevel@tonic-gate  *
46157c478bd9Sstevel@tonic-gate  * if input dip != child_dip, then call is on behalf of child
46167c478bd9Sstevel@tonic-gate  * to search PROM, do it via ddi_prop_search_common() and ascend only
46177c478bd9Sstevel@tonic-gate  * if allowed.
46187c478bd9Sstevel@tonic-gate  *
46197c478bd9Sstevel@tonic-gate  * if input dip == ch_dip (child_dip), call is on behalf of root driver,
46207c478bd9Sstevel@tonic-gate  * to search for PROM defined props only.
46217c478bd9Sstevel@tonic-gate  *
46227c478bd9Sstevel@tonic-gate  * Note that the PROM search is done only if the requested dev
46237c478bd9Sstevel@tonic-gate  * is either DDI_DEV_T_ANY or DDI_DEV_T_NONE. PROM properties
46247c478bd9Sstevel@tonic-gate  * have no associated dev, thus are automatically associated with
46257c478bd9Sstevel@tonic-gate  * DDI_DEV_T_NONE.
46267c478bd9Sstevel@tonic-gate  *
46277c478bd9Sstevel@tonic-gate  * Modifying flag DDI_PROP_NOTPROM inhibits the search in the h/w layer.
46287c478bd9Sstevel@tonic-gate  *
46297c478bd9Sstevel@tonic-gate  * Returns DDI_PROP_FOUND_1275 if found to indicate to framework
46307c478bd9Sstevel@tonic-gate  * that the property resides in the prom.
46317c478bd9Sstevel@tonic-gate  */
46327c478bd9Sstevel@tonic-gate int
46337c478bd9Sstevel@tonic-gate impl_ddi_bus_prop_op(dev_t dev, dev_info_t *dip, dev_info_t *ch_dip,
46347c478bd9Sstevel@tonic-gate     ddi_prop_op_t prop_op, int mod_flags,
46357c478bd9Sstevel@tonic-gate     char *name, caddr_t valuep, int *lengthp)
46367c478bd9Sstevel@tonic-gate {
46377c478bd9Sstevel@tonic-gate 	int	len;
46387c478bd9Sstevel@tonic-gate 	caddr_t buffer;
46397c478bd9Sstevel@tonic-gate 
46407c478bd9Sstevel@tonic-gate 	/*
46417c478bd9Sstevel@tonic-gate 	 * If requested dev is DDI_DEV_T_NONE or DDI_DEV_T_ANY, then
46427c478bd9Sstevel@tonic-gate 	 * look in caller's PROM if it's a self identifying device...
46437c478bd9Sstevel@tonic-gate 	 *
46447c478bd9Sstevel@tonic-gate 	 * Note that this is very similar to ddi_prop_op, but we
46457c478bd9Sstevel@tonic-gate 	 * search the PROM instead of the s/w defined properties,
46467c478bd9Sstevel@tonic-gate 	 * and we are called on by the parent driver to do this for
46477c478bd9Sstevel@tonic-gate 	 * the child.
46487c478bd9Sstevel@tonic-gate 	 */
46497c478bd9Sstevel@tonic-gate 
46507c478bd9Sstevel@tonic-gate 	if (((dev == DDI_DEV_T_NONE) || (dev == DDI_DEV_T_ANY)) &&
46517c478bd9Sstevel@tonic-gate 	    ndi_dev_is_prom_node(ch_dip) &&
46527c478bd9Sstevel@tonic-gate 	    ((mod_flags & DDI_PROP_NOTPROM) == 0)) {
4653fa9e4066Sahrens 		len = prom_getproplen((pnode_t)DEVI(ch_dip)->devi_nodeid, name);
46547c478bd9Sstevel@tonic-gate 		if (len == -1) {
46557c478bd9Sstevel@tonic-gate 			return (DDI_PROP_NOT_FOUND);
46567c478bd9Sstevel@tonic-gate 		}
46577c478bd9Sstevel@tonic-gate 
46587c478bd9Sstevel@tonic-gate 		/*
46597c478bd9Sstevel@tonic-gate 		 * If exists only request, we're done
46607c478bd9Sstevel@tonic-gate 		 */
46617c478bd9Sstevel@tonic-gate 		if (prop_op == PROP_EXISTS) {
46627c478bd9Sstevel@tonic-gate 			return (DDI_PROP_FOUND_1275);
46637c478bd9Sstevel@tonic-gate 		}
46647c478bd9Sstevel@tonic-gate 
46657c478bd9Sstevel@tonic-gate 		/*
46667c478bd9Sstevel@tonic-gate 		 * If length only request or prop length == 0, get out
46677c478bd9Sstevel@tonic-gate 		 */
46687c478bd9Sstevel@tonic-gate 		if ((prop_op == PROP_LEN) || (len == 0)) {
46697c478bd9Sstevel@tonic-gate 			*lengthp = len;
46707c478bd9Sstevel@tonic-gate 			return (DDI_PROP_FOUND_1275);
46717c478bd9Sstevel@tonic-gate 		}
46727c478bd9Sstevel@tonic-gate 
46737c478bd9Sstevel@tonic-gate 		/*
46747c478bd9Sstevel@tonic-gate 		 * Allocate buffer if required... (either way `buffer'
46757c478bd9Sstevel@tonic-gate 		 * is receiving address).
46767c478bd9Sstevel@tonic-gate 		 */
46777c478bd9Sstevel@tonic-gate 
46787c478bd9Sstevel@tonic-gate 		switch (prop_op) {
46797c478bd9Sstevel@tonic-gate 
46807c478bd9Sstevel@tonic-gate 		case PROP_LEN_AND_VAL_ALLOC:
46817c478bd9Sstevel@tonic-gate 
46827c478bd9Sstevel@tonic-gate 			buffer = kmem_alloc((size_t)len,
46837c478bd9Sstevel@tonic-gate 			    mod_flags & DDI_PROP_CANSLEEP ?
46847c478bd9Sstevel@tonic-gate 			    KM_SLEEP : KM_NOSLEEP);
46857c478bd9Sstevel@tonic-gate 			if (buffer == NULL) {
46867c478bd9Sstevel@tonic-gate 				return (DDI_PROP_NO_MEMORY);
46877c478bd9Sstevel@tonic-gate 			}
46887c478bd9Sstevel@tonic-gate 			*(caddr_t *)valuep = buffer;
46897c478bd9Sstevel@tonic-gate 			break;
46907c478bd9Sstevel@tonic-gate 
46917c478bd9Sstevel@tonic-gate 		case PROP_LEN_AND_VAL_BUF:
46927c478bd9Sstevel@tonic-gate 
46937c478bd9Sstevel@tonic-gate 			if (len > (*lengthp)) {
46947c478bd9Sstevel@tonic-gate 				*lengthp = len;
46957c478bd9Sstevel@tonic-gate 				return (DDI_PROP_BUF_TOO_SMALL);
46967c478bd9Sstevel@tonic-gate 			}
46977c478bd9Sstevel@tonic-gate 
46987c478bd9Sstevel@tonic-gate 			buffer = valuep;
46997c478bd9Sstevel@tonic-gate 			break;
47007c478bd9Sstevel@tonic-gate 
47017c478bd9Sstevel@tonic-gate 		default:
47027c478bd9Sstevel@tonic-gate 			break;
47037c478bd9Sstevel@tonic-gate 		}
47047c478bd9Sstevel@tonic-gate 
47057c478bd9Sstevel@tonic-gate 		/*
47067c478bd9Sstevel@tonic-gate 		 * Call the PROM function to do the copy.
47077c478bd9Sstevel@tonic-gate 		 */
4708fa9e4066Sahrens 		(void) prom_getprop((pnode_t)DEVI(ch_dip)->devi_nodeid,
4709184cd04cScth 		    name, buffer);
47107c478bd9Sstevel@tonic-gate 
47117c478bd9Sstevel@tonic-gate 		*lengthp = len; /* return the actual length to the caller */
47127c478bd9Sstevel@tonic-gate 		(void) impl_fix_props(dip, ch_dip, name, len, buffer);
47137c478bd9Sstevel@tonic-gate 		return (DDI_PROP_FOUND_1275);
47147c478bd9Sstevel@tonic-gate 	}
47157c478bd9Sstevel@tonic-gate 
47167c478bd9Sstevel@tonic-gate 	return (DDI_PROP_NOT_FOUND);
47177c478bd9Sstevel@tonic-gate }
47187c478bd9Sstevel@tonic-gate 
47197c478bd9Sstevel@tonic-gate /*
47207c478bd9Sstevel@tonic-gate  * The ddi_bus_prop_op default bus nexus prop op function.
47217c478bd9Sstevel@tonic-gate  *
47227c478bd9Sstevel@tonic-gate  * Code to search hardware layer (PROM), if it exists,
47237c478bd9Sstevel@tonic-gate  * on behalf of child, then, if appropriate, ascend and check
47247c478bd9Sstevel@tonic-gate  * my own software defined properties...
47257c478bd9Sstevel@tonic-gate  */
47267c478bd9Sstevel@tonic-gate int
47277c478bd9Sstevel@tonic-gate ddi_bus_prop_op(dev_t dev, dev_info_t *dip, dev_info_t *ch_dip,
47287c478bd9Sstevel@tonic-gate     ddi_prop_op_t prop_op, int mod_flags,
47297c478bd9Sstevel@tonic-gate     char *name, caddr_t valuep, int *lengthp)
47307c478bd9Sstevel@tonic-gate {
47317c478bd9Sstevel@tonic-gate 	int	error;
47327c478bd9Sstevel@tonic-gate 
47337c478bd9Sstevel@tonic-gate 	error = impl_ddi_bus_prop_op(dev, dip, ch_dip, prop_op, mod_flags,
4734184cd04cScth 	    name, valuep, lengthp);
47357c478bd9Sstevel@tonic-gate 
47367c478bd9Sstevel@tonic-gate 	if (error == DDI_PROP_SUCCESS || error == DDI_PROP_FOUND_1275 ||
47377c478bd9Sstevel@tonic-gate 	    error == DDI_PROP_BUF_TOO_SMALL)
47387c478bd9Sstevel@tonic-gate 		return (error);
47397c478bd9Sstevel@tonic-gate 
47407c478bd9Sstevel@tonic-gate 	if (error == DDI_PROP_NO_MEMORY) {
47417c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, prop_no_mem_msg, name);
47427c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NO_MEMORY);
47437c478bd9Sstevel@tonic-gate 	}
47447c478bd9Sstevel@tonic-gate 
47457c478bd9Sstevel@tonic-gate 	/*
47467c478bd9Sstevel@tonic-gate 	 * Check the 'options' node as a last resort
47477c478bd9Sstevel@tonic-gate 	 */
47487c478bd9Sstevel@tonic-gate 	if ((mod_flags & DDI_PROP_DONTPASS) != 0)
47497c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
47507c478bd9Sstevel@tonic-gate 
47517c478bd9Sstevel@tonic-gate 	if (ch_dip == ddi_root_node())	{
47527c478bd9Sstevel@tonic-gate 		/*
47537c478bd9Sstevel@tonic-gate 		 * As a last resort, when we've reached
47547c478bd9Sstevel@tonic-gate 		 * the top and still haven't found the
47557c478bd9Sstevel@tonic-gate 		 * property, see if the desired property
47567c478bd9Sstevel@tonic-gate 		 * is attached to the options node.
47577c478bd9Sstevel@tonic-gate 		 *
47587c478bd9Sstevel@tonic-gate 		 * The options dip is attached right after boot.
47597c478bd9Sstevel@tonic-gate 		 */
47607c478bd9Sstevel@tonic-gate 		ASSERT(options_dip != NULL);
47617c478bd9Sstevel@tonic-gate 		/*
47627c478bd9Sstevel@tonic-gate 		 * Force the "don't pass" flag to *just* see
47637c478bd9Sstevel@tonic-gate 		 * what the options node has to offer.
47647c478bd9Sstevel@tonic-gate 		 */
47657c478bd9Sstevel@tonic-gate 		return (ddi_prop_search_common(dev, options_dip, prop_op,
47667c478bd9Sstevel@tonic-gate 		    mod_flags|DDI_PROP_DONTPASS, name, valuep,
47677c478bd9Sstevel@tonic-gate 		    (uint_t *)lengthp));
47687c478bd9Sstevel@tonic-gate 	}
47697c478bd9Sstevel@tonic-gate 
47707c478bd9Sstevel@tonic-gate 	/*
47717c478bd9Sstevel@tonic-gate 	 * Otherwise, continue search with parent's s/w defined properties...
47727c478bd9Sstevel@tonic-gate 	 * NOTE: Using `dip' in following call increments the level.
47737c478bd9Sstevel@tonic-gate 	 */
47747c478bd9Sstevel@tonic-gate 
47757c478bd9Sstevel@tonic-gate 	return (ddi_prop_search_common(dev, dip, prop_op, mod_flags,
47767c478bd9Sstevel@tonic-gate 	    name, valuep, (uint_t *)lengthp));
47777c478bd9Sstevel@tonic-gate }
47787c478bd9Sstevel@tonic-gate 
47797c478bd9Sstevel@tonic-gate /*
47807c478bd9Sstevel@tonic-gate  * External property functions used by other parts of the kernel...
47817c478bd9Sstevel@tonic-gate  */
47827c478bd9Sstevel@tonic-gate 
47837c478bd9Sstevel@tonic-gate /*
47847c478bd9Sstevel@tonic-gate  * e_ddi_getlongprop: See comments for ddi_get_longprop.
47857c478bd9Sstevel@tonic-gate  */
47867c478bd9Sstevel@tonic-gate 
47877c478bd9Sstevel@tonic-gate int
47887c478bd9Sstevel@tonic-gate e_ddi_getlongprop(dev_t dev, vtype_t type, char *name, int flags,
47897c478bd9Sstevel@tonic-gate     caddr_t valuep, int *lengthp)
47907c478bd9Sstevel@tonic-gate {
47917c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(type))
47927c478bd9Sstevel@tonic-gate 	dev_info_t *devi;
47937c478bd9Sstevel@tonic-gate 	ddi_prop_op_t prop_op = PROP_LEN_AND_VAL_ALLOC;
47947c478bd9Sstevel@tonic-gate 	int error;
47957c478bd9Sstevel@tonic-gate 
47967c478bd9Sstevel@tonic-gate 	if ((devi = e_ddi_hold_devi_by_dev(dev, 0)) == NULL)
47977c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
47987c478bd9Sstevel@tonic-gate 
47997c478bd9Sstevel@tonic-gate 	error = cdev_prop_op(dev, devi, prop_op, flags, name, valuep, lengthp);
48007c478bd9Sstevel@tonic-gate 	ddi_release_devi(devi);
48017c478bd9Sstevel@tonic-gate 	return (error);
48027c478bd9Sstevel@tonic-gate }
48037c478bd9Sstevel@tonic-gate 
48047c478bd9Sstevel@tonic-gate /*
48057c478bd9Sstevel@tonic-gate  * e_ddi_getlongprop_buf:	See comments for ddi_getlongprop_buf.
48067c478bd9Sstevel@tonic-gate  */
48077c478bd9Sstevel@tonic-gate 
48087c478bd9Sstevel@tonic-gate int
48097c478bd9Sstevel@tonic-gate e_ddi_getlongprop_buf(dev_t dev, vtype_t type, char *name, int flags,
48107c478bd9Sstevel@tonic-gate     caddr_t valuep, int *lengthp)
48117c478bd9Sstevel@tonic-gate {
48127c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(type))
48137c478bd9Sstevel@tonic-gate 	dev_info_t *devi;
48147c478bd9Sstevel@tonic-gate 	ddi_prop_op_t prop_op = PROP_LEN_AND_VAL_BUF;
48157c478bd9Sstevel@tonic-gate 	int error;
48167c478bd9Sstevel@tonic-gate 
48177c478bd9Sstevel@tonic-gate 	if ((devi = e_ddi_hold_devi_by_dev(dev, 0)) == NULL)
48187c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
48197c478bd9Sstevel@tonic-gate 
48207c478bd9Sstevel@tonic-gate 	error = cdev_prop_op(dev, devi, prop_op, flags, name, valuep, lengthp);
48217c478bd9Sstevel@tonic-gate 	ddi_release_devi(devi);
48227c478bd9Sstevel@tonic-gate 	return (error);
48237c478bd9Sstevel@tonic-gate }
48247c478bd9Sstevel@tonic-gate 
48257c478bd9Sstevel@tonic-gate /*
48267c478bd9Sstevel@tonic-gate  * e_ddi_getprop:	See comments for ddi_getprop.
48277c478bd9Sstevel@tonic-gate  */
48287c478bd9Sstevel@tonic-gate int
48297c478bd9Sstevel@tonic-gate e_ddi_getprop(dev_t dev, vtype_t type, char *name, int flags, int defvalue)
48307c478bd9Sstevel@tonic-gate {
48317c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(type))
48327c478bd9Sstevel@tonic-gate 	dev_info_t *devi;
48337c478bd9Sstevel@tonic-gate 	ddi_prop_op_t prop_op = PROP_LEN_AND_VAL_BUF;
48347c478bd9Sstevel@tonic-gate 	int	propvalue = defvalue;
48357c478bd9Sstevel@tonic-gate 	int	proplength = sizeof (int);
48367c478bd9Sstevel@tonic-gate 	int	error;
48377c478bd9Sstevel@tonic-gate 
48387c478bd9Sstevel@tonic-gate 	if ((devi = e_ddi_hold_devi_by_dev(dev, 0)) == NULL)
48397c478bd9Sstevel@tonic-gate 		return (defvalue);
48407c478bd9Sstevel@tonic-gate 
48417c478bd9Sstevel@tonic-gate 	error = cdev_prop_op(dev, devi, prop_op,
48427c478bd9Sstevel@tonic-gate 	    flags, name, (caddr_t)&propvalue, &proplength);
48437c478bd9Sstevel@tonic-gate 	ddi_release_devi(devi);
48447c478bd9Sstevel@tonic-gate 
48457c478bd9Sstevel@tonic-gate 	if ((error == DDI_PROP_SUCCESS) && (proplength == 0))
48467c478bd9Sstevel@tonic-gate 		propvalue = 1;
48477c478bd9Sstevel@tonic-gate 
48487c478bd9Sstevel@tonic-gate 	return (propvalue);
48497c478bd9Sstevel@tonic-gate }
48507c478bd9Sstevel@tonic-gate 
48517c478bd9Sstevel@tonic-gate /*
48527c478bd9Sstevel@tonic-gate  * e_ddi_getprop_int64:
48537c478bd9Sstevel@tonic-gate  *
48547c478bd9Sstevel@tonic-gate  * This is a typed interfaces, but predates typed properties. With the
48557c478bd9Sstevel@tonic-gate  * introduction of typed properties the framework tries to ensure
48567c478bd9Sstevel@tonic-gate  * consistent use of typed interfaces. This is why TYPE_INT64 is not
48577c478bd9Sstevel@tonic-gate  * part of TYPE_ANY.  E_ddi_getprop_int64 is a special case where a
48587c478bd9Sstevel@tonic-gate  * typed interface invokes legacy (non-typed) interfaces:
48597c478bd9Sstevel@tonic-gate  * cdev_prop_op(), prop_op(9E), ddi_prop_op(9F)).  In this case the
48607c478bd9Sstevel@tonic-gate  * fact that TYPE_INT64 is not part of TYPE_ANY matters.  To support
48617c478bd9Sstevel@tonic-gate  * this type of lookup as a single operation we invoke the legacy
48627c478bd9Sstevel@tonic-gate  * non-typed interfaces with the special CONSUMER_TYPED bit set. The
48637c478bd9Sstevel@tonic-gate  * framework ddi_prop_op(9F) implementation is expected to check for
48647c478bd9Sstevel@tonic-gate  * CONSUMER_TYPED and, if set, expand type bits beyond TYPE_ANY
48657c478bd9Sstevel@tonic-gate  * (currently TYPE_INT64).
48667c478bd9Sstevel@tonic-gate  */
48677c478bd9Sstevel@tonic-gate int64_t
48687c478bd9Sstevel@tonic-gate e_ddi_getprop_int64(dev_t dev, vtype_t type, char *name,
48697c478bd9Sstevel@tonic-gate     int flags, int64_t defvalue)
48707c478bd9Sstevel@tonic-gate {
48717c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(type))
48727c478bd9Sstevel@tonic-gate 	dev_info_t	*devi;
48737c478bd9Sstevel@tonic-gate 	ddi_prop_op_t	prop_op = PROP_LEN_AND_VAL_BUF;
48747c478bd9Sstevel@tonic-gate 	int64_t		propvalue = defvalue;
48757c478bd9Sstevel@tonic-gate 	int		proplength = sizeof (propvalue);
48767c478bd9Sstevel@tonic-gate 	int		error;
48777c478bd9Sstevel@tonic-gate 
48787c478bd9Sstevel@tonic-gate 	if ((devi = e_ddi_hold_devi_by_dev(dev, 0)) == NULL)
48797c478bd9Sstevel@tonic-gate 		return (defvalue);
48807c478bd9Sstevel@tonic-gate 
48817c478bd9Sstevel@tonic-gate 	error = cdev_prop_op(dev, devi, prop_op, flags |
48827c478bd9Sstevel@tonic-gate 	    DDI_PROP_CONSUMER_TYPED, name, (caddr_t)&propvalue, &proplength);
48837c478bd9Sstevel@tonic-gate 	ddi_release_devi(devi);
48847c478bd9Sstevel@tonic-gate 
48857c478bd9Sstevel@tonic-gate 	if ((error == DDI_PROP_SUCCESS) && (proplength == 0))
48867c478bd9Sstevel@tonic-gate 		propvalue = 1;
48877c478bd9Sstevel@tonic-gate 
48887c478bd9Sstevel@tonic-gate 	return (propvalue);
48897c478bd9Sstevel@tonic-gate }
48907c478bd9Sstevel@tonic-gate 
48917c478bd9Sstevel@tonic-gate /*
48927c478bd9Sstevel@tonic-gate  * e_ddi_getproplen:	See comments for ddi_getproplen.
48937c478bd9Sstevel@tonic-gate  */
48947c478bd9Sstevel@tonic-gate int
48957c478bd9Sstevel@tonic-gate e_ddi_getproplen(dev_t dev, vtype_t type, char *name, int flags, int *lengthp)
48967c478bd9Sstevel@tonic-gate {
48977c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(type))
48987c478bd9Sstevel@tonic-gate 	dev_info_t *devi;
48997c478bd9Sstevel@tonic-gate 	ddi_prop_op_t prop_op = PROP_LEN;
49007c478bd9Sstevel@tonic-gate 	int error;
49017c478bd9Sstevel@tonic-gate 
49027c478bd9Sstevel@tonic-gate 	if ((devi = e_ddi_hold_devi_by_dev(dev, 0)) == NULL)
49037c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
49047c478bd9Sstevel@tonic-gate 
49057c478bd9Sstevel@tonic-gate 	error = cdev_prop_op(dev, devi, prop_op, flags, name, NULL, lengthp);
49067c478bd9Sstevel@tonic-gate 	ddi_release_devi(devi);
49077c478bd9Sstevel@tonic-gate 	return (error);
49087c478bd9Sstevel@tonic-gate }
49097c478bd9Sstevel@tonic-gate 
49107c478bd9Sstevel@tonic-gate /*
49117c478bd9Sstevel@tonic-gate  * Routines to get at elements of the dev_info structure
49127c478bd9Sstevel@tonic-gate  */
49137c478bd9Sstevel@tonic-gate 
49147c478bd9Sstevel@tonic-gate /*
49157c478bd9Sstevel@tonic-gate  * ddi_binding_name: Return the driver binding name of the devinfo node
49167c478bd9Sstevel@tonic-gate  *		This is the name the OS used to bind the node to a driver.
49177c478bd9Sstevel@tonic-gate  */
49187c478bd9Sstevel@tonic-gate char *
49197c478bd9Sstevel@tonic-gate ddi_binding_name(dev_info_t *dip)
49207c478bd9Sstevel@tonic-gate {
49217c478bd9Sstevel@tonic-gate 	return (DEVI(dip)->devi_binding_name);
49227c478bd9Sstevel@tonic-gate }
49237c478bd9Sstevel@tonic-gate 
49247c478bd9Sstevel@tonic-gate /*
49257c478bd9Sstevel@tonic-gate  * ddi_driver_major: Return the major number of the driver that
49265c066ec2SJerry Gilliam  *	the supplied devinfo is bound to.  If not yet bound,
49275c066ec2SJerry Gilliam  *	DDI_MAJOR_T_NONE.
49285c066ec2SJerry Gilliam  *
49295c066ec2SJerry Gilliam  * When used by the driver bound to 'devi', this
49305c066ec2SJerry Gilliam  * function will reliably return the driver major number.
49315c066ec2SJerry Gilliam  * Other ways of determining the driver major number, such as
49325c066ec2SJerry Gilliam  *	major = ddi_name_to_major(ddi_get_name(devi));
49335c066ec2SJerry Gilliam  *	major = ddi_name_to_major(ddi_binding_name(devi));
49345c066ec2SJerry Gilliam  * can return a different result as the driver/alias binding
49355c066ec2SJerry Gilliam  * can change dynamically, and thus should be avoided.
49367c478bd9Sstevel@tonic-gate  */
49377c478bd9Sstevel@tonic-gate major_t
49387c478bd9Sstevel@tonic-gate ddi_driver_major(dev_info_t *devi)
49397c478bd9Sstevel@tonic-gate {
49407c478bd9Sstevel@tonic-gate 	return (DEVI(devi)->devi_major);
49417c478bd9Sstevel@tonic-gate }
49427c478bd9Sstevel@tonic-gate 
49437c478bd9Sstevel@tonic-gate /*
49447c478bd9Sstevel@tonic-gate  * ddi_driver_name: Return the normalized driver name. this is the
49457c478bd9Sstevel@tonic-gate  *		actual driver name
49467c478bd9Sstevel@tonic-gate  */
49477c478bd9Sstevel@tonic-gate const char *
49487c478bd9Sstevel@tonic-gate ddi_driver_name(dev_info_t *devi)
49497c478bd9Sstevel@tonic-gate {
49507c478bd9Sstevel@tonic-gate 	major_t major;
49517c478bd9Sstevel@tonic-gate 
4952a204de77Scth 	if ((major = ddi_driver_major(devi)) != DDI_MAJOR_T_NONE)
49537c478bd9Sstevel@tonic-gate 		return (ddi_major_to_name(major));
49547c478bd9Sstevel@tonic-gate 
49557c478bd9Sstevel@tonic-gate 	return (ddi_node_name(devi));
49567c478bd9Sstevel@tonic-gate }
49577c478bd9Sstevel@tonic-gate 
49587c478bd9Sstevel@tonic-gate /*
49597c478bd9Sstevel@tonic-gate  * i_ddi_set_binding_name:	Set binding name.
49607c478bd9Sstevel@tonic-gate  *
49617c478bd9Sstevel@tonic-gate  *	Set the binding name to the given name.
49627c478bd9Sstevel@tonic-gate  *	This routine is for use by the ddi implementation, not by drivers.
49637c478bd9Sstevel@tonic-gate  */
49647c478bd9Sstevel@tonic-gate void
49657c478bd9Sstevel@tonic-gate i_ddi_set_binding_name(dev_info_t *dip, char *name)
49667c478bd9Sstevel@tonic-gate {
49677c478bd9Sstevel@tonic-gate 	DEVI(dip)->devi_binding_name = name;
49687c478bd9Sstevel@tonic-gate 
49697c478bd9Sstevel@tonic-gate }
49707c478bd9Sstevel@tonic-gate 
49717c478bd9Sstevel@tonic-gate /*
49727c478bd9Sstevel@tonic-gate  * ddi_get_name: A synonym of ddi_binding_name() ... returns a name
49737c478bd9Sstevel@tonic-gate  * the implementation has used to bind the node to a driver.
49747c478bd9Sstevel@tonic-gate  */
49757c478bd9Sstevel@tonic-gate char *
49767c478bd9Sstevel@tonic-gate ddi_get_name(dev_info_t *dip)
49777c478bd9Sstevel@tonic-gate {
49787c478bd9Sstevel@tonic-gate 	return (DEVI(dip)->devi_binding_name);
49797c478bd9Sstevel@tonic-gate }
49807c478bd9Sstevel@tonic-gate 
49817c478bd9Sstevel@tonic-gate /*
49827c478bd9Sstevel@tonic-gate  * ddi_node_name: Return the name property of the devinfo node
49837c478bd9Sstevel@tonic-gate  *		This may differ from ddi_binding_name if the node name
49847c478bd9Sstevel@tonic-gate  *		does not define a binding to a driver (i.e. generic names).
49857c478bd9Sstevel@tonic-gate  */
49867c478bd9Sstevel@tonic-gate char *
49877c478bd9Sstevel@tonic-gate ddi_node_name(dev_info_t *dip)
49887c478bd9Sstevel@tonic-gate {
49897c478bd9Sstevel@tonic-gate 	return (DEVI(dip)->devi_node_name);
49907c478bd9Sstevel@tonic-gate }
49917c478bd9Sstevel@tonic-gate 
49927c478bd9Sstevel@tonic-gate 
49937c478bd9Sstevel@tonic-gate /*
49947c478bd9Sstevel@tonic-gate  * ddi_get_nodeid:	Get nodeid stored in dev_info structure.
49957c478bd9Sstevel@tonic-gate  */
49967c478bd9Sstevel@tonic-gate int
49977c478bd9Sstevel@tonic-gate ddi_get_nodeid(dev_info_t *dip)
49987c478bd9Sstevel@tonic-gate {
49997c478bd9Sstevel@tonic-gate 	return (DEVI(dip)->devi_nodeid);
50007c478bd9Sstevel@tonic-gate }
50017c478bd9Sstevel@tonic-gate 
50027c478bd9Sstevel@tonic-gate int
50037c478bd9Sstevel@tonic-gate ddi_get_instance(dev_info_t *dip)
50047c478bd9Sstevel@tonic-gate {
50057c478bd9Sstevel@tonic-gate 	return (DEVI(dip)->devi_instance);
50067c478bd9Sstevel@tonic-gate }
50077c478bd9Sstevel@tonic-gate 
50087c478bd9Sstevel@tonic-gate struct dev_ops *
50097c478bd9Sstevel@tonic-gate ddi_get_driver(dev_info_t *dip)
50107c478bd9Sstevel@tonic-gate {
50117c478bd9Sstevel@tonic-gate 	return (DEVI(dip)->devi_ops);
50127c478bd9Sstevel@tonic-gate }
50137c478bd9Sstevel@tonic-gate 
50147c478bd9Sstevel@tonic-gate void
50157c478bd9Sstevel@tonic-gate ddi_set_driver(dev_info_t *dip, struct dev_ops *devo)
50167c478bd9Sstevel@tonic-gate {
50177c478bd9Sstevel@tonic-gate 	DEVI(dip)->devi_ops = devo;
50187c478bd9Sstevel@tonic-gate }
50197c478bd9Sstevel@tonic-gate 
50207c478bd9Sstevel@tonic-gate /*
50217c478bd9Sstevel@tonic-gate  * ddi_set_driver_private/ddi_get_driver_private:
50227c478bd9Sstevel@tonic-gate  * Get/set device driver private data in devinfo.
50237c478bd9Sstevel@tonic-gate  */
50247c478bd9Sstevel@tonic-gate void
50257c478bd9Sstevel@tonic-gate ddi_set_driver_private(dev_info_t *dip, void *data)
50267c478bd9Sstevel@tonic-gate {
50277c478bd9Sstevel@tonic-gate 	DEVI(dip)->devi_driver_data = data;
50287c478bd9Sstevel@tonic-gate }
50297c478bd9Sstevel@tonic-gate 
50307c478bd9Sstevel@tonic-gate void *
50317c478bd9Sstevel@tonic-gate ddi_get_driver_private(dev_info_t *dip)
50327c478bd9Sstevel@tonic-gate {
50337c478bd9Sstevel@tonic-gate 	return (DEVI(dip)->devi_driver_data);
50347c478bd9Sstevel@tonic-gate }
50357c478bd9Sstevel@tonic-gate 
50367c478bd9Sstevel@tonic-gate /*
50377c478bd9Sstevel@tonic-gate  * ddi_get_parent, ddi_get_child, ddi_get_next_sibling
50387c478bd9Sstevel@tonic-gate  */
50397c478bd9Sstevel@tonic-gate 
50407c478bd9Sstevel@tonic-gate dev_info_t *
50417c478bd9Sstevel@tonic-gate ddi_get_parent(dev_info_t *dip)
50427c478bd9Sstevel@tonic-gate {
50437c478bd9Sstevel@tonic-gate 	return ((dev_info_t *)DEVI(dip)->devi_parent);
50447c478bd9Sstevel@tonic-gate }
50457c478bd9Sstevel@tonic-gate 
50467c478bd9Sstevel@tonic-gate dev_info_t *
50477c478bd9Sstevel@tonic-gate ddi_get_child(dev_info_t *dip)
50487c478bd9Sstevel@tonic-gate {
50497c478bd9Sstevel@tonic-gate 	return ((dev_info_t *)DEVI(dip)->devi_child);
50507c478bd9Sstevel@tonic-gate }
50517c478bd9Sstevel@tonic-gate 
50527c478bd9Sstevel@tonic-gate dev_info_t *
50537c478bd9Sstevel@tonic-gate ddi_get_next_sibling(dev_info_t *dip)
50547c478bd9Sstevel@tonic-gate {
50557c478bd9Sstevel@tonic-gate 	return ((dev_info_t *)DEVI(dip)->devi_sibling);
50567c478bd9Sstevel@tonic-gate }
50577c478bd9Sstevel@tonic-gate 
50587c478bd9Sstevel@tonic-gate dev_info_t *
50597c478bd9Sstevel@tonic-gate ddi_get_next(dev_info_t *dip)
50607c478bd9Sstevel@tonic-gate {
50617c478bd9Sstevel@tonic-gate 	return ((dev_info_t *)DEVI(dip)->devi_next);
50627c478bd9Sstevel@tonic-gate }
50637c478bd9Sstevel@tonic-gate 
50647c478bd9Sstevel@tonic-gate void
50657c478bd9Sstevel@tonic-gate ddi_set_next(dev_info_t *dip, dev_info_t *nextdip)
50667c478bd9Sstevel@tonic-gate {
50677c478bd9Sstevel@tonic-gate 	DEVI(dip)->devi_next = DEVI(nextdip);
50687c478bd9Sstevel@tonic-gate }
50697c478bd9Sstevel@tonic-gate 
50707c478bd9Sstevel@tonic-gate /*
50717c478bd9Sstevel@tonic-gate  * ddi_root_node:		Return root node of devinfo tree
50727c478bd9Sstevel@tonic-gate  */
50737c478bd9Sstevel@tonic-gate 
50747c478bd9Sstevel@tonic-gate dev_info_t *
50757c478bd9Sstevel@tonic-gate ddi_root_node(void)
50767c478bd9Sstevel@tonic-gate {
50777c478bd9Sstevel@tonic-gate 	extern dev_info_t *top_devinfo;
50787c478bd9Sstevel@tonic-gate 
50797c478bd9Sstevel@tonic-gate 	return (top_devinfo);
50807c478bd9Sstevel@tonic-gate }
50817c478bd9Sstevel@tonic-gate 
50827c478bd9Sstevel@tonic-gate /*
50837c478bd9Sstevel@tonic-gate  * Miscellaneous functions:
50847c478bd9Sstevel@tonic-gate  */
50857c478bd9Sstevel@tonic-gate 
50867c478bd9Sstevel@tonic-gate /*
50877c478bd9Sstevel@tonic-gate  * Implementation specific hooks
50887c478bd9Sstevel@tonic-gate  */
50897c478bd9Sstevel@tonic-gate 
50907c478bd9Sstevel@tonic-gate void
50917c478bd9Sstevel@tonic-gate ddi_report_dev(dev_info_t *d)
50927c478bd9Sstevel@tonic-gate {
50937c478bd9Sstevel@tonic-gate 	char *b;
50947c478bd9Sstevel@tonic-gate 
50957c478bd9Sstevel@tonic-gate 	(void) ddi_ctlops(d, d, DDI_CTLOPS_REPORTDEV, (void *)0, (void *)0);
50967c478bd9Sstevel@tonic-gate 
50977c478bd9Sstevel@tonic-gate 	/*
50987c478bd9Sstevel@tonic-gate 	 * If this devinfo node has cb_ops, it's implicitly accessible from
50997c478bd9Sstevel@tonic-gate 	 * userland, so we print its full name together with the instance
51007c478bd9Sstevel@tonic-gate 	 * number 'abbreviation' that the driver may use internally.
51017c478bd9Sstevel@tonic-gate 	 */
51027c478bd9Sstevel@tonic-gate 	if (DEVI(d)->devi_ops->devo_cb_ops != (struct cb_ops *)0 &&
51037c478bd9Sstevel@tonic-gate 	    (b = kmem_zalloc(MAXPATHLEN, KM_NOSLEEP))) {
51047c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "?%s%d is %s\n",
51057c478bd9Sstevel@tonic-gate 		    ddi_driver_name(d), ddi_get_instance(d),
51067c478bd9Sstevel@tonic-gate 		    ddi_pathname(d, b));
51077c478bd9Sstevel@tonic-gate 		kmem_free(b, MAXPATHLEN);
51087c478bd9Sstevel@tonic-gate 	}
51097c478bd9Sstevel@tonic-gate }
51107c478bd9Sstevel@tonic-gate 
51117c478bd9Sstevel@tonic-gate /*
51127c478bd9Sstevel@tonic-gate  * ddi_ctlops() is described in the assembler not to buy a new register
51137c478bd9Sstevel@tonic-gate  * window when it's called and can reduce cost in climbing the device tree
51147c478bd9Sstevel@tonic-gate  * without using the tail call optimization.
51157c478bd9Sstevel@tonic-gate  */
51167c478bd9Sstevel@tonic-gate int
51177c478bd9Sstevel@tonic-gate ddi_dev_regsize(dev_info_t *dev, uint_t rnumber, off_t *result)
51187c478bd9Sstevel@tonic-gate {
51197c478bd9Sstevel@tonic-gate 	int ret;
51207c478bd9Sstevel@tonic-gate 
51217c478bd9Sstevel@tonic-gate 	ret = ddi_ctlops(dev, dev, DDI_CTLOPS_REGSIZE,
51227c478bd9Sstevel@tonic-gate 	    (void *)&rnumber, (void *)result);
51237c478bd9Sstevel@tonic-gate 
51247c478bd9Sstevel@tonic-gate 	return (ret == DDI_SUCCESS ? DDI_SUCCESS : DDI_FAILURE);
51257c478bd9Sstevel@tonic-gate }
51267c478bd9Sstevel@tonic-gate 
51277c478bd9Sstevel@tonic-gate int
51287c478bd9Sstevel@tonic-gate ddi_dev_nregs(dev_info_t *dev, int *result)
51297c478bd9Sstevel@tonic-gate {
51307c478bd9Sstevel@tonic-gate 	return (ddi_ctlops(dev, dev, DDI_CTLOPS_NREGS, 0, (void *)result));
51317c478bd9Sstevel@tonic-gate }
51327c478bd9Sstevel@tonic-gate 
51337c478bd9Sstevel@tonic-gate int
51347c478bd9Sstevel@tonic-gate ddi_dev_is_sid(dev_info_t *d)
51357c478bd9Sstevel@tonic-gate {
51367c478bd9Sstevel@tonic-gate 	return (ddi_ctlops(d, d, DDI_CTLOPS_SIDDEV, (void *)0, (void *)0));
51377c478bd9Sstevel@tonic-gate }
51387c478bd9Sstevel@tonic-gate 
51397c478bd9Sstevel@tonic-gate int
51407c478bd9Sstevel@tonic-gate ddi_slaveonly(dev_info_t *d)
51417c478bd9Sstevel@tonic-gate {
51427c478bd9Sstevel@tonic-gate 	return (ddi_ctlops(d, d, DDI_CTLOPS_SLAVEONLY, (void *)0, (void *)0));
51437c478bd9Sstevel@tonic-gate }
51447c478bd9Sstevel@tonic-gate 
51457c478bd9Sstevel@tonic-gate int
51467c478bd9Sstevel@tonic-gate ddi_dev_affinity(dev_info_t *a, dev_info_t *b)
51477c478bd9Sstevel@tonic-gate {
51487c478bd9Sstevel@tonic-gate 	return (ddi_ctlops(a, a, DDI_CTLOPS_AFFINITY, (void *)b, (void *)0));
51497c478bd9Sstevel@tonic-gate }
51507c478bd9Sstevel@tonic-gate 
51517c478bd9Sstevel@tonic-gate int
51527c478bd9Sstevel@tonic-gate ddi_streams_driver(dev_info_t *dip)
51537c478bd9Sstevel@tonic-gate {
5154737d277aScth 	if (i_ddi_devi_attached(dip) &&
51557c478bd9Sstevel@tonic-gate 	    (DEVI(dip)->devi_ops->devo_cb_ops != NULL) &&
51567c478bd9Sstevel@tonic-gate 	    (DEVI(dip)->devi_ops->devo_cb_ops->cb_str != NULL))
51577c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
51587c478bd9Sstevel@tonic-gate 	return (DDI_FAILURE);
51597c478bd9Sstevel@tonic-gate }
51607c478bd9Sstevel@tonic-gate 
51617c478bd9Sstevel@tonic-gate /*
51627c478bd9Sstevel@tonic-gate  * callback free list
51637c478bd9Sstevel@tonic-gate  */
51647c478bd9Sstevel@tonic-gate 
51657c478bd9Sstevel@tonic-gate static int ncallbacks;
51667c478bd9Sstevel@tonic-gate static int nc_low = 170;
51677c478bd9Sstevel@tonic-gate static int nc_med = 512;
51687c478bd9Sstevel@tonic-gate static int nc_high = 2048;
51697c478bd9Sstevel@tonic-gate static struct ddi_callback *callbackq;
51707c478bd9Sstevel@tonic-gate static struct ddi_callback *callbackqfree;
51717c478bd9Sstevel@tonic-gate 
51727c478bd9Sstevel@tonic-gate /*
51737c478bd9Sstevel@tonic-gate  * set/run callback lists
51747c478bd9Sstevel@tonic-gate  */
51757c478bd9Sstevel@tonic-gate struct	cbstats	{
51767c478bd9Sstevel@tonic-gate 	kstat_named_t	cb_asked;
51777c478bd9Sstevel@tonic-gate 	kstat_named_t	cb_new;
51787c478bd9Sstevel@tonic-gate 	kstat_named_t	cb_run;
51797c478bd9Sstevel@tonic-gate 	kstat_named_t	cb_delete;
51807c478bd9Sstevel@tonic-gate 	kstat_named_t	cb_maxreq;
51817c478bd9Sstevel@tonic-gate 	kstat_named_t	cb_maxlist;
51827c478bd9Sstevel@tonic-gate 	kstat_named_t	cb_alloc;
51837c478bd9Sstevel@tonic-gate 	kstat_named_t	cb_runouts;
51847c478bd9Sstevel@tonic-gate 	kstat_named_t	cb_L2;
51857c478bd9Sstevel@tonic-gate 	kstat_named_t	cb_grow;
51867c478bd9Sstevel@tonic-gate } cbstats = {
51877c478bd9Sstevel@tonic-gate 	{"asked",	KSTAT_DATA_UINT32},
51887c478bd9Sstevel@tonic-gate 	{"new",		KSTAT_DATA_UINT32},
51897c478bd9Sstevel@tonic-gate 	{"run",		KSTAT_DATA_UINT32},
51907c478bd9Sstevel@tonic-gate 	{"delete",	KSTAT_DATA_UINT32},
51917c478bd9Sstevel@tonic-gate 	{"maxreq",	KSTAT_DATA_UINT32},
51927c478bd9Sstevel@tonic-gate 	{"maxlist",	KSTAT_DATA_UINT32},
51937c478bd9Sstevel@tonic-gate 	{"alloc",	KSTAT_DATA_UINT32},
51947c478bd9Sstevel@tonic-gate 	{"runouts",	KSTAT_DATA_UINT32},
51957c478bd9Sstevel@tonic-gate 	{"L2",		KSTAT_DATA_UINT32},
51967c478bd9Sstevel@tonic-gate 	{"grow",	KSTAT_DATA_UINT32},
51977c478bd9Sstevel@tonic-gate };
51987c478bd9Sstevel@tonic-gate 
51997c478bd9Sstevel@tonic-gate #define	nc_asked	cb_asked.value.ui32
52007c478bd9Sstevel@tonic-gate #define	nc_new		cb_new.value.ui32
52017c478bd9Sstevel@tonic-gate #define	nc_run		cb_run.value.ui32
52027c478bd9Sstevel@tonic-gate #define	nc_delete	cb_delete.value.ui32
52037c478bd9Sstevel@tonic-gate #define	nc_maxreq	cb_maxreq.value.ui32
52047c478bd9Sstevel@tonic-gate #define	nc_maxlist	cb_maxlist.value.ui32
52057c478bd9Sstevel@tonic-gate #define	nc_alloc	cb_alloc.value.ui32
52067c478bd9Sstevel@tonic-gate #define	nc_runouts	cb_runouts.value.ui32
52077c478bd9Sstevel@tonic-gate #define	nc_L2		cb_L2.value.ui32
52087c478bd9Sstevel@tonic-gate #define	nc_grow		cb_grow.value.ui32
52097c478bd9Sstevel@tonic-gate 
52107c478bd9Sstevel@tonic-gate static kmutex_t ddi_callback_mutex;
52117c478bd9Sstevel@tonic-gate 
52127c478bd9Sstevel@tonic-gate /*
52137c478bd9Sstevel@tonic-gate  * callbacks are handled using a L1/L2 cache. The L1 cache
52147c478bd9Sstevel@tonic-gate  * comes out of kmem_cache_alloc and can expand/shrink dynamically. If
52157c478bd9Sstevel@tonic-gate  * we can't get callbacks from the L1 cache [because pageout is doing
52167c478bd9Sstevel@tonic-gate  * I/O at the time freemem is 0], we allocate callbacks out of the
52177c478bd9Sstevel@tonic-gate  * L2 cache. The L2 cache is static and depends on the memory size.
52187c478bd9Sstevel@tonic-gate  * [We might also count the number of devices at probe time and
52197c478bd9Sstevel@tonic-gate  * allocate one structure per device and adjust for deferred attach]
52207c478bd9Sstevel@tonic-gate  */
52217c478bd9Sstevel@tonic-gate void
52227c478bd9Sstevel@tonic-gate impl_ddi_callback_init(void)
52237c478bd9Sstevel@tonic-gate {
52247c478bd9Sstevel@tonic-gate 	int	i;
52257c478bd9Sstevel@tonic-gate 	uint_t	physmegs;
52267c478bd9Sstevel@tonic-gate 	kstat_t	*ksp;
52277c478bd9Sstevel@tonic-gate 
52287c478bd9Sstevel@tonic-gate 	physmegs = physmem >> (20 - PAGESHIFT);
52297c478bd9Sstevel@tonic-gate 	if (physmegs < 48) {
52307c478bd9Sstevel@tonic-gate 		ncallbacks = nc_low;
52317c478bd9Sstevel@tonic-gate 	} else if (physmegs < 128) {
52327c478bd9Sstevel@tonic-gate 		ncallbacks = nc_med;
52337c478bd9Sstevel@tonic-gate 	} else {
52347c478bd9Sstevel@tonic-gate 		ncallbacks = nc_high;
52357c478bd9Sstevel@tonic-gate 	}
52367c478bd9Sstevel@tonic-gate 
52377c478bd9Sstevel@tonic-gate 	/*
52387c478bd9Sstevel@tonic-gate 	 * init free list
52397c478bd9Sstevel@tonic-gate 	 */
52407c478bd9Sstevel@tonic-gate 	callbackq = kmem_zalloc(
52417c478bd9Sstevel@tonic-gate 	    ncallbacks * sizeof (struct ddi_callback), KM_SLEEP);
52427c478bd9Sstevel@tonic-gate 	for (i = 0; i < ncallbacks-1; i++)
52437c478bd9Sstevel@tonic-gate 		callbackq[i].c_nfree = &callbackq[i+1];
52447c478bd9Sstevel@tonic-gate 	callbackqfree = callbackq;
52457c478bd9Sstevel@tonic-gate 
52467c478bd9Sstevel@tonic-gate 	/* init kstats */
52477c478bd9Sstevel@tonic-gate 	if (ksp = kstat_create("unix", 0, "cbstats", "misc", KSTAT_TYPE_NAMED,
52487c478bd9Sstevel@tonic-gate 	    sizeof (cbstats) / sizeof (kstat_named_t), KSTAT_FLAG_VIRTUAL)) {
52497c478bd9Sstevel@tonic-gate 		ksp->ks_data = (void *) &cbstats;
52507c478bd9Sstevel@tonic-gate 		kstat_install(ksp);
52517c478bd9Sstevel@tonic-gate 	}
52527c478bd9Sstevel@tonic-gate 
52537c478bd9Sstevel@tonic-gate }
52547c478bd9Sstevel@tonic-gate 
52557c478bd9Sstevel@tonic-gate static void
52567c478bd9Sstevel@tonic-gate callback_insert(int (*funcp)(caddr_t), caddr_t arg, uintptr_t *listid,
52577c478bd9Sstevel@tonic-gate 	int count)
52587c478bd9Sstevel@tonic-gate {
52597c478bd9Sstevel@tonic-gate 	struct ddi_callback *list, *marker, *new;
52607c478bd9Sstevel@tonic-gate 	size_t size = sizeof (struct ddi_callback);
52617c478bd9Sstevel@tonic-gate 
52627c478bd9Sstevel@tonic-gate 	list = marker = (struct ddi_callback *)*listid;
52637c478bd9Sstevel@tonic-gate 	while (list != NULL) {
52647c478bd9Sstevel@tonic-gate 		if (list->c_call == funcp && list->c_arg == arg) {
52657c478bd9Sstevel@tonic-gate 			list->c_count += count;
52667c478bd9Sstevel@tonic-gate 			return;
52677c478bd9Sstevel@tonic-gate 		}
52687c478bd9Sstevel@tonic-gate 		marker = list;
52697c478bd9Sstevel@tonic-gate 		list = list->c_nlist;
52707c478bd9Sstevel@tonic-gate 	}
52717c478bd9Sstevel@tonic-gate 	new = kmem_alloc(size, KM_NOSLEEP);
52727c478bd9Sstevel@tonic-gate 	if (new == NULL) {
52737c478bd9Sstevel@tonic-gate 		new = callbackqfree;
52747c478bd9Sstevel@tonic-gate 		if (new == NULL) {
52757c478bd9Sstevel@tonic-gate 			new = kmem_alloc_tryhard(sizeof (struct ddi_callback),
52767c478bd9Sstevel@tonic-gate 			    &size, KM_NOSLEEP | KM_PANIC);
52777c478bd9Sstevel@tonic-gate 			cbstats.nc_grow++;
52787c478bd9Sstevel@tonic-gate 		} else {
52797c478bd9Sstevel@tonic-gate 			callbackqfree = new->c_nfree;
52807c478bd9Sstevel@tonic-gate 			cbstats.nc_L2++;
52817c478bd9Sstevel@tonic-gate 		}
52827c478bd9Sstevel@tonic-gate 	}
52837c478bd9Sstevel@tonic-gate 	if (marker != NULL) {
52847c478bd9Sstevel@tonic-gate 		marker->c_nlist = new;
52857c478bd9Sstevel@tonic-gate 	} else {
52867c478bd9Sstevel@tonic-gate 		*listid = (uintptr_t)new;
52877c478bd9Sstevel@tonic-gate 	}
52887c478bd9Sstevel@tonic-gate 	new->c_size = size;
52897c478bd9Sstevel@tonic-gate 	new->c_nlist = NULL;
52907c478bd9Sstevel@tonic-gate 	new->c_call = funcp;
52917c478bd9Sstevel@tonic-gate 	new->c_arg = arg;
52927c478bd9Sstevel@tonic-gate 	new->c_count = count;
52937c478bd9Sstevel@tonic-gate 	cbstats.nc_new++;
52947c478bd9Sstevel@tonic-gate 	cbstats.nc_alloc++;
52957c478bd9Sstevel@tonic-gate 	if (cbstats.nc_alloc > cbstats.nc_maxlist)
52967c478bd9Sstevel@tonic-gate 		cbstats.nc_maxlist = cbstats.nc_alloc;
52977c478bd9Sstevel@tonic-gate }
52987c478bd9Sstevel@tonic-gate 
52997c478bd9Sstevel@tonic-gate void
53007c478bd9Sstevel@tonic-gate ddi_set_callback(int (*funcp)(caddr_t), caddr_t arg, uintptr_t *listid)
53017c478bd9Sstevel@tonic-gate {
53027c478bd9Sstevel@tonic-gate 	mutex_enter(&ddi_callback_mutex);
53037c478bd9Sstevel@tonic-gate 	cbstats.nc_asked++;
53047c478bd9Sstevel@tonic-gate 	if ((cbstats.nc_asked - cbstats.nc_run) > cbstats.nc_maxreq)
53057c478bd9Sstevel@tonic-gate 		cbstats.nc_maxreq = (cbstats.nc_asked - cbstats.nc_run);
53067c478bd9Sstevel@tonic-gate 	(void) callback_insert(funcp, arg, listid, 1);
53077c478bd9Sstevel@tonic-gate 	mutex_exit(&ddi_callback_mutex);
53087c478bd9Sstevel@tonic-gate }
53097c478bd9Sstevel@tonic-gate 
53107c478bd9Sstevel@tonic-gate static void
53117c478bd9Sstevel@tonic-gate real_callback_run(void *Queue)
53127c478bd9Sstevel@tonic-gate {
53137c478bd9Sstevel@tonic-gate 	int (*funcp)(caddr_t);
53147c478bd9Sstevel@tonic-gate 	caddr_t arg;
53157c478bd9Sstevel@tonic-gate 	int count, rval;
53167c478bd9Sstevel@tonic-gate 	uintptr_t *listid;
53177c478bd9Sstevel@tonic-gate 	struct ddi_callback *list, *marker;
53187c478bd9Sstevel@tonic-gate 	int check_pending = 1;
53197c478bd9Sstevel@tonic-gate 	int pending = 0;
53207c478bd9Sstevel@tonic-gate 
53217c478bd9Sstevel@tonic-gate 	do {
53227c478bd9Sstevel@tonic-gate 		mutex_enter(&ddi_callback_mutex);
53237c478bd9Sstevel@tonic-gate 		listid = Queue;
53247c478bd9Sstevel@tonic-gate 		list = (struct ddi_callback *)*listid;
53257c478bd9Sstevel@tonic-gate 		if (list == NULL) {
53267c478bd9Sstevel@tonic-gate 			mutex_exit(&ddi_callback_mutex);
53277c478bd9Sstevel@tonic-gate 			return;
53287c478bd9Sstevel@tonic-gate 		}
53297c478bd9Sstevel@tonic-gate 		if (check_pending) {
53307c478bd9Sstevel@tonic-gate 			marker = list;
53317c478bd9Sstevel@tonic-gate 			while (marker != NULL) {
53327c478bd9Sstevel@tonic-gate 				pending += marker->c_count;
53337c478bd9Sstevel@tonic-gate 				marker = marker->c_nlist;
53347c478bd9Sstevel@tonic-gate 			}
53357c478bd9Sstevel@tonic-gate 			check_pending = 0;
53367c478bd9Sstevel@tonic-gate 		}
53377c478bd9Sstevel@tonic-gate 		ASSERT(pending > 0);
53387c478bd9Sstevel@tonic-gate 		ASSERT(list->c_count > 0);
53397c478bd9Sstevel@tonic-gate 		funcp = list->c_call;
53407c478bd9Sstevel@tonic-gate 		arg = list->c_arg;
53417c478bd9Sstevel@tonic-gate 		count = list->c_count;
53427c478bd9Sstevel@tonic-gate 		*(uintptr_t *)Queue = (uintptr_t)list->c_nlist;
53437c478bd9Sstevel@tonic-gate 		if (list >= &callbackq[0] &&
53447c478bd9Sstevel@tonic-gate 		    list <= &callbackq[ncallbacks-1]) {
53457c478bd9Sstevel@tonic-gate 			list->c_nfree = callbackqfree;
53467c478bd9Sstevel@tonic-gate 			callbackqfree = list;
53477c478bd9Sstevel@tonic-gate 		} else
53487c478bd9Sstevel@tonic-gate 			kmem_free(list, list->c_size);
53497c478bd9Sstevel@tonic-gate 
53507c478bd9Sstevel@tonic-gate 		cbstats.nc_delete++;
53517c478bd9Sstevel@tonic-gate 		cbstats.nc_alloc--;
53527c478bd9Sstevel@tonic-gate 		mutex_exit(&ddi_callback_mutex);
53537c478bd9Sstevel@tonic-gate 
53547c478bd9Sstevel@tonic-gate 		do {
53557c478bd9Sstevel@tonic-gate 			if ((rval = (*funcp)(arg)) == 0) {
53567c478bd9Sstevel@tonic-gate 				pending -= count;
53577c478bd9Sstevel@tonic-gate 				mutex_enter(&ddi_callback_mutex);
53587c478bd9Sstevel@tonic-gate 				(void) callback_insert(funcp, arg, listid,
5359184cd04cScth 				    count);
53607c478bd9Sstevel@tonic-gate 				cbstats.nc_runouts++;
53617c478bd9Sstevel@tonic-gate 			} else {
53627c478bd9Sstevel@tonic-gate 				pending--;
53637c478bd9Sstevel@tonic-gate 				mutex_enter(&ddi_callback_mutex);
53647c478bd9Sstevel@tonic-gate 				cbstats.nc_run++;
53657c478bd9Sstevel@tonic-gate 			}
53667c478bd9Sstevel@tonic-gate 			mutex_exit(&ddi_callback_mutex);
53677c478bd9Sstevel@tonic-gate 		} while (rval != 0 && (--count > 0));
53687c478bd9Sstevel@tonic-gate 	} while (pending > 0);
53697c478bd9Sstevel@tonic-gate }
53707c478bd9Sstevel@tonic-gate 
53717c478bd9Sstevel@tonic-gate void
53727c478bd9Sstevel@tonic-gate ddi_run_callback(uintptr_t *listid)
53737c478bd9Sstevel@tonic-gate {
53747c478bd9Sstevel@tonic-gate 	softcall(real_callback_run, listid);
53757c478bd9Sstevel@tonic-gate }
53767c478bd9Sstevel@tonic-gate 
5377dd4eeefdSeota /*
5378dd4eeefdSeota  * ddi_periodic_t
5379dd4eeefdSeota  * ddi_periodic_add(void (*func)(void *), void *arg, hrtime_t interval,
5380dd4eeefdSeota  *     int level)
5381dd4eeefdSeota  *
5382dd4eeefdSeota  * INTERFACE LEVEL
5383dd4eeefdSeota  *      Solaris DDI specific (Solaris DDI)
5384dd4eeefdSeota  *
5385dd4eeefdSeota  * PARAMETERS
5386dd4eeefdSeota  *      func: the callback function
5387dd4eeefdSeota  *
5388dd4eeefdSeota  *            The callback function will be invoked. The function is invoked
5389dd4eeefdSeota  *            in kernel context if the argument level passed is the zero.
5390dd4eeefdSeota  *            Otherwise it's invoked in interrupt context at the specified
5391dd4eeefdSeota  *            level.
5392dd4eeefdSeota  *
5393dd4eeefdSeota  *       arg: the argument passed to the callback function
5394dd4eeefdSeota  *
5395dd4eeefdSeota  *  interval: interval time
5396dd4eeefdSeota  *
5397dd4eeefdSeota  *    level : callback interrupt level
5398dd4eeefdSeota  *
5399dd4eeefdSeota  *            If the value is the zero, the callback function is invoked
5400dd4eeefdSeota  *            in kernel context. If the value is more than the zero, but
5401dd4eeefdSeota  *            less than or equal to ten, the callback function is invoked in
5402dd4eeefdSeota  *            interrupt context at the specified interrupt level, which may
5403dd4eeefdSeota  *            be used for real time applications.
5404dd4eeefdSeota  *
5405dd4eeefdSeota  *            This value must be in range of 0-10, which can be a numeric
5406dd4eeefdSeota  *            number or a pre-defined macro (DDI_IPL_0, ... , DDI_IPL_10).
5407dd4eeefdSeota  *
5408dd4eeefdSeota  * DESCRIPTION
5409dd4eeefdSeota  *      ddi_periodic_add(9F) schedules the specified function to be
5410dd4eeefdSeota  *      periodically invoked in the interval time.
5411dd4eeefdSeota  *
5412dd4eeefdSeota  *      As well as timeout(9F), the exact time interval over which the function
5413dd4eeefdSeota  *      takes effect cannot be guaranteed, but the value given is a close
5414dd4eeefdSeota  *      approximation.
5415dd4eeefdSeota  *
5416dd4eeefdSeota  *      Drivers waiting on behalf of processes with real-time constraints must
5417dd4eeefdSeota  *      pass non-zero value with the level argument to ddi_periodic_add(9F).
5418dd4eeefdSeota  *
5419dd4eeefdSeota  * RETURN VALUES
5420dd4eeefdSeota  *      ddi_periodic_add(9F) returns a non-zero opaque value (ddi_periodic_t),
5421dd4eeefdSeota  *      which must be used for ddi_periodic_delete(9F) to specify the request.
5422dd4eeefdSeota  *
5423dd4eeefdSeota  * CONTEXT
5424dd4eeefdSeota  *      ddi_periodic_add(9F) can be called in user or kernel context, but
5425dd4eeefdSeota  *      it cannot be called in interrupt context, which is different from
5426dd4eeefdSeota  *      timeout(9F).
5427dd4eeefdSeota  */
5428dd4eeefdSeota ddi_periodic_t
5429dd4eeefdSeota ddi_periodic_add(void (*func)(void *), void *arg, hrtime_t interval, int level)
5430dd4eeefdSeota {
5431dd4eeefdSeota 	/*
5432dd4eeefdSeota 	 * Sanity check of the argument level.
5433dd4eeefdSeota 	 */
5434dd4eeefdSeota 	if (level < DDI_IPL_0 || level > DDI_IPL_10)
5435dd4eeefdSeota 		cmn_err(CE_PANIC,
5436dd4eeefdSeota 		    "ddi_periodic_add: invalid interrupt level (%d).", level);
5437dd4eeefdSeota 
5438dd4eeefdSeota 	/*
5439dd4eeefdSeota 	 * Sanity check of the context. ddi_periodic_add() cannot be
5440dd4eeefdSeota 	 * called in either interrupt context or high interrupt context.
5441dd4eeefdSeota 	 */
5442dd4eeefdSeota 	if (servicing_interrupt())
5443dd4eeefdSeota 		cmn_err(CE_PANIC,
5444dd4eeefdSeota 		    "ddi_periodic_add: called in (high) interrupt context.");
5445dd4eeefdSeota 
5446dd4eeefdSeota 	return ((ddi_periodic_t)i_timeout(func, arg, interval, level));
5447dd4eeefdSeota }
5448dd4eeefdSeota 
5449dd4eeefdSeota /*
5450dd4eeefdSeota  * void
5451dd4eeefdSeota  * ddi_periodic_delete(ddi_periodic_t req)
5452dd4eeefdSeota  *
5453dd4eeefdSeota  * INTERFACE LEVEL
5454dd4eeefdSeota  *     Solaris DDI specific (Solaris DDI)
5455dd4eeefdSeota  *
5456dd4eeefdSeota  * PARAMETERS
5457dd4eeefdSeota  *     req: ddi_periodic_t opaque value ddi_periodic_add(9F) returned
5458dd4eeefdSeota  *     previously.
5459dd4eeefdSeota  *
5460dd4eeefdSeota  * DESCRIPTION
5461dd4eeefdSeota  *     ddi_periodic_delete(9F) cancels the ddi_periodic_add(9F) request
5462dd4eeefdSeota  *     previously requested.
5463dd4eeefdSeota  *
5464dd4eeefdSeota  *     ddi_periodic_delete(9F) will not return until the pending request
5465dd4eeefdSeota  *     is canceled or executed.
5466dd4eeefdSeota  *
5467dd4eeefdSeota  *     As well as untimeout(9F), calling ddi_periodic_delete(9F) for a
5468dd4eeefdSeota  *     timeout which is either running on another CPU, or has already
5469dd4eeefdSeota  *     completed causes no problems. However, unlike untimeout(9F), there is
5470dd4eeefdSeota  *     no restrictions on the lock which might be held across the call to
5471dd4eeefdSeota  *     ddi_periodic_delete(9F).
5472dd4eeefdSeota  *
5473dd4eeefdSeota  *     Drivers should be structured with the understanding that the arrival of
5474dd4eeefdSeota  *     both an interrupt and a timeout for that interrupt can occasionally
5475dd4eeefdSeota  *     occur, in either order.
5476dd4eeefdSeota  *
5477dd4eeefdSeota  * CONTEXT
5478dd4eeefdSeota  *     ddi_periodic_delete(9F) can be called in user or kernel context, but
5479dd4eeefdSeota  *     it cannot be called in interrupt context, which is different from
5480dd4eeefdSeota  *     untimeout(9F).
5481dd4eeefdSeota  */
5482dd4eeefdSeota void
5483dd4eeefdSeota ddi_periodic_delete(ddi_periodic_t req)
5484dd4eeefdSeota {
5485dd4eeefdSeota 	/*
5486dd4eeefdSeota 	 * Sanity check of the context. ddi_periodic_delete() cannot be
5487dd4eeefdSeota 	 * called in either interrupt context or high interrupt context.
5488dd4eeefdSeota 	 */
5489dd4eeefdSeota 	if (servicing_interrupt())
5490dd4eeefdSeota 		cmn_err(CE_PANIC,
5491dd4eeefdSeota 		    "ddi_periodic_delete: called in (high) interrupt context.");
5492dd4eeefdSeota 
5493dd4eeefdSeota 	i_untimeout((timeout_t)req);
5494dd4eeefdSeota }
5495dd4eeefdSeota 
54967c478bd9Sstevel@tonic-gate dev_info_t *
54977c478bd9Sstevel@tonic-gate nodevinfo(dev_t dev, int otyp)
54987c478bd9Sstevel@tonic-gate {
54997c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(dev, otyp))
55007c478bd9Sstevel@tonic-gate 	return ((dev_info_t *)0);
55017c478bd9Sstevel@tonic-gate }
55027c478bd9Sstevel@tonic-gate 
55037c478bd9Sstevel@tonic-gate /*
55047c478bd9Sstevel@tonic-gate  * A driver should support its own getinfo(9E) entry point. This function
55057c478bd9Sstevel@tonic-gate  * is provided as a convenience for ON drivers that don't expect their
55067c478bd9Sstevel@tonic-gate  * getinfo(9E) entry point to be called. A driver that uses this must not
55077c478bd9Sstevel@tonic-gate  * call ddi_create_minor_node.
55087c478bd9Sstevel@tonic-gate  */
55097c478bd9Sstevel@tonic-gate int
55107c478bd9Sstevel@tonic-gate ddi_no_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
55117c478bd9Sstevel@tonic-gate {
55127c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(dip, infocmd, arg, result))
55137c478bd9Sstevel@tonic-gate 	return (DDI_FAILURE);
55147c478bd9Sstevel@tonic-gate }
55157c478bd9Sstevel@tonic-gate 
55167c478bd9Sstevel@tonic-gate /*
55177c478bd9Sstevel@tonic-gate  * A driver should support its own getinfo(9E) entry point. This function
55187c478bd9Sstevel@tonic-gate  * is provided as a convenience for ON drivers that where the minor number
55197c478bd9Sstevel@tonic-gate  * is the instance. Drivers that do not have 1:1 mapping must implement
55207c478bd9Sstevel@tonic-gate  * their own getinfo(9E) function.
55217c478bd9Sstevel@tonic-gate  */
55227c478bd9Sstevel@tonic-gate int
55237c478bd9Sstevel@tonic-gate ddi_getinfo_1to1(dev_info_t *dip, ddi_info_cmd_t infocmd,
55247c478bd9Sstevel@tonic-gate     void *arg, void **result)
55257c478bd9Sstevel@tonic-gate {
55267c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(dip))
55277c478bd9Sstevel@tonic-gate 	int	instance;
55287c478bd9Sstevel@tonic-gate 
55297c478bd9Sstevel@tonic-gate 	if (infocmd != DDI_INFO_DEVT2INSTANCE)
55307c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
55317c478bd9Sstevel@tonic-gate 
55327c478bd9Sstevel@tonic-gate 	instance = getminor((dev_t)(uintptr_t)arg);
55337c478bd9Sstevel@tonic-gate 	*result = (void *)(uintptr_t)instance;
55347c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
55357c478bd9Sstevel@tonic-gate }
55367c478bd9Sstevel@tonic-gate 
55377c478bd9Sstevel@tonic-gate int
55387c478bd9Sstevel@tonic-gate ddifail(dev_info_t *devi, ddi_attach_cmd_t cmd)
55397c478bd9Sstevel@tonic-gate {
55407c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(devi, cmd))
55417c478bd9Sstevel@tonic-gate 	return (DDI_FAILURE);
55427c478bd9Sstevel@tonic-gate }
55437c478bd9Sstevel@tonic-gate 
55447c478bd9Sstevel@tonic-gate int
55457c478bd9Sstevel@tonic-gate ddi_no_dma_map(dev_info_t *dip, dev_info_t *rdip,
55467c478bd9Sstevel@tonic-gate     struct ddi_dma_req *dmareqp, ddi_dma_handle_t *handlep)
55477c478bd9Sstevel@tonic-gate {
55487c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(dip, rdip, dmareqp, handlep))
55497c478bd9Sstevel@tonic-gate 	return (DDI_DMA_NOMAPPING);
55507c478bd9Sstevel@tonic-gate }
55517c478bd9Sstevel@tonic-gate 
55527c478bd9Sstevel@tonic-gate int
55537c478bd9Sstevel@tonic-gate ddi_no_dma_allochdl(dev_info_t *dip, dev_info_t *rdip, ddi_dma_attr_t *attr,
55547c478bd9Sstevel@tonic-gate     int (*waitfp)(caddr_t), caddr_t arg, ddi_dma_handle_t *handlep)
55557c478bd9Sstevel@tonic-gate {
55567c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(dip, rdip, attr, waitfp, arg, handlep))
55577c478bd9Sstevel@tonic-gate 	return (DDI_DMA_BADATTR);
55587c478bd9Sstevel@tonic-gate }
55597c478bd9Sstevel@tonic-gate 
55607c478bd9Sstevel@tonic-gate int
55617c478bd9Sstevel@tonic-gate ddi_no_dma_freehdl(dev_info_t *dip, dev_info_t *rdip,
55627c478bd9Sstevel@tonic-gate     ddi_dma_handle_t handle)
55637c478bd9Sstevel@tonic-gate {
55647c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(dip, rdip, handle))
55657c478bd9Sstevel@tonic-gate 	return (DDI_FAILURE);
55667c478bd9Sstevel@tonic-gate }
55677c478bd9Sstevel@tonic-gate 
55687c478bd9Sstevel@tonic-gate int
55697c478bd9Sstevel@tonic-gate ddi_no_dma_bindhdl(dev_info_t *dip, dev_info_t *rdip,
55707c478bd9Sstevel@tonic-gate     ddi_dma_handle_t handle, struct ddi_dma_req *dmareq,
55717c478bd9Sstevel@tonic-gate     ddi_dma_cookie_t *cp, uint_t *ccountp)
55727c478bd9Sstevel@tonic-gate {
55737c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(dip, rdip, handle, dmareq, cp, ccountp))
55747c478bd9Sstevel@tonic-gate 	return (DDI_DMA_NOMAPPING);
55757c478bd9Sstevel@tonic-gate }
55767c478bd9Sstevel@tonic-gate 
55777c478bd9Sstevel@tonic-gate int
55787c478bd9Sstevel@tonic-gate ddi_no_dma_unbindhdl(dev_info_t *dip, dev_info_t *rdip,
55797c478bd9Sstevel@tonic-gate     ddi_dma_handle_t handle)
55807c478bd9Sstevel@tonic-gate {
55817c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(dip, rdip, handle))
55827c478bd9Sstevel@tonic-gate 	return (DDI_FAILURE);
55837c478bd9Sstevel@tonic-gate }
55847c478bd9Sstevel@tonic-gate 
55857c478bd9Sstevel@tonic-gate int
55867c478bd9Sstevel@tonic-gate ddi_no_dma_flush(dev_info_t *dip, dev_info_t *rdip,
55877c478bd9Sstevel@tonic-gate     ddi_dma_handle_t handle, off_t off, size_t len,
55887c478bd9Sstevel@tonic-gate     uint_t cache_flags)
55897c478bd9Sstevel@tonic-gate {
55907c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(dip, rdip, handle, off, len, cache_flags))
55917c478bd9Sstevel@tonic-gate 	return (DDI_FAILURE);
55927c478bd9Sstevel@tonic-gate }
55937c478bd9Sstevel@tonic-gate 
55947c478bd9Sstevel@tonic-gate int
55957c478bd9Sstevel@tonic-gate ddi_no_dma_win(dev_info_t *dip, dev_info_t *rdip,
55967c478bd9Sstevel@tonic-gate     ddi_dma_handle_t handle, uint_t win, off_t *offp,
55977c478bd9Sstevel@tonic-gate     size_t *lenp, ddi_dma_cookie_t *cookiep, uint_t *ccountp)
55987c478bd9Sstevel@tonic-gate {
55997c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(dip, rdip, handle, win, offp, lenp, cookiep, ccountp))
56007c478bd9Sstevel@tonic-gate 	return (DDI_FAILURE);
56017c478bd9Sstevel@tonic-gate }
56027c478bd9Sstevel@tonic-gate 
56037c478bd9Sstevel@tonic-gate int
56047c478bd9Sstevel@tonic-gate ddi_no_dma_mctl(dev_info_t *dip, dev_info_t *rdip,
56057c478bd9Sstevel@tonic-gate     ddi_dma_handle_t handle, enum ddi_dma_ctlops request,
56067c478bd9Sstevel@tonic-gate     off_t *offp, size_t *lenp, caddr_t *objp, uint_t flags)
56077c478bd9Sstevel@tonic-gate {
56087c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(dip, rdip, handle, request, offp, lenp, objp, flags))
56097c478bd9Sstevel@tonic-gate 	return (DDI_FAILURE);
56107c478bd9Sstevel@tonic-gate }
56117c478bd9Sstevel@tonic-gate 
56127c478bd9Sstevel@tonic-gate void
56137c478bd9Sstevel@tonic-gate ddivoid(void)
56147c478bd9Sstevel@tonic-gate {}
56157c478bd9Sstevel@tonic-gate 
56167c478bd9Sstevel@tonic-gate int
56177c478bd9Sstevel@tonic-gate nochpoll(dev_t dev, short events, int anyyet, short *reventsp,
56187c478bd9Sstevel@tonic-gate     struct pollhead **pollhdrp)
56197c478bd9Sstevel@tonic-gate {
56207c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(dev, events, anyyet, reventsp, pollhdrp))
56217c478bd9Sstevel@tonic-gate 	return (ENXIO);
56227c478bd9Sstevel@tonic-gate }
56237c478bd9Sstevel@tonic-gate 
56247c478bd9Sstevel@tonic-gate cred_t *
56257c478bd9Sstevel@tonic-gate ddi_get_cred(void)
56267c478bd9Sstevel@tonic-gate {
56277c478bd9Sstevel@tonic-gate 	return (CRED());
56287c478bd9Sstevel@tonic-gate }
56297c478bd9Sstevel@tonic-gate 
56307c478bd9Sstevel@tonic-gate clock_t
56317c478bd9Sstevel@tonic-gate ddi_get_lbolt(void)
56327c478bd9Sstevel@tonic-gate {
56337c478bd9Sstevel@tonic-gate 	return (lbolt);
56347c478bd9Sstevel@tonic-gate }
56357c478bd9Sstevel@tonic-gate 
56367c478bd9Sstevel@tonic-gate time_t
56377c478bd9Sstevel@tonic-gate ddi_get_time(void)
56387c478bd9Sstevel@tonic-gate {
56397c478bd9Sstevel@tonic-gate 	time_t	now;
56407c478bd9Sstevel@tonic-gate 
56417c478bd9Sstevel@tonic-gate 	if ((now = gethrestime_sec()) == 0) {
56427c478bd9Sstevel@tonic-gate 		timestruc_t ts;
56437c478bd9Sstevel@tonic-gate 		mutex_enter(&tod_lock);
56447c478bd9Sstevel@tonic-gate 		ts = tod_get();
56457c478bd9Sstevel@tonic-gate 		mutex_exit(&tod_lock);
56467c478bd9Sstevel@tonic-gate 		return (ts.tv_sec);
56477c478bd9Sstevel@tonic-gate 	} else {
56487c478bd9Sstevel@tonic-gate 		return (now);
56497c478bd9Sstevel@tonic-gate 	}
56507c478bd9Sstevel@tonic-gate }
56517c478bd9Sstevel@tonic-gate 
56527c478bd9Sstevel@tonic-gate pid_t
56537c478bd9Sstevel@tonic-gate ddi_get_pid(void)
56547c478bd9Sstevel@tonic-gate {
56557c478bd9Sstevel@tonic-gate 	return (ttoproc(curthread)->p_pid);
56567c478bd9Sstevel@tonic-gate }
56577c478bd9Sstevel@tonic-gate 
56587c478bd9Sstevel@tonic-gate kt_did_t
56597c478bd9Sstevel@tonic-gate ddi_get_kt_did(void)
56607c478bd9Sstevel@tonic-gate {
56617c478bd9Sstevel@tonic-gate 	return (curthread->t_did);
56627c478bd9Sstevel@tonic-gate }
56637c478bd9Sstevel@tonic-gate 
56647c478bd9Sstevel@tonic-gate /*
56657c478bd9Sstevel@tonic-gate  * This function returns B_TRUE if the caller can reasonably expect that a call
56667c478bd9Sstevel@tonic-gate  * to cv_wait_sig(9F), cv_timedwait_sig(9F), or qwait_sig(9F) could be awakened
56677c478bd9Sstevel@tonic-gate  * by user-level signal.  If it returns B_FALSE, then the caller should use
56687c478bd9Sstevel@tonic-gate  * other means to make certain that the wait will not hang "forever."
56697c478bd9Sstevel@tonic-gate  *
56707c478bd9Sstevel@tonic-gate  * It does not check the signal mask, nor for reception of any particular
56717c478bd9Sstevel@tonic-gate  * signal.
56727c478bd9Sstevel@tonic-gate  *
56737c478bd9Sstevel@tonic-gate  * Currently, a thread can receive a signal if it's not a kernel thread and it
56747c478bd9Sstevel@tonic-gate  * is not in the middle of exit(2) tear-down.  Threads that are in that
56757c478bd9Sstevel@tonic-gate  * tear-down effectively convert cv_wait_sig to cv_wait, cv_timedwait_sig to
56767c478bd9Sstevel@tonic-gate  * cv_timedwait, and qwait_sig to qwait.
56777c478bd9Sstevel@tonic-gate  */
56787c478bd9Sstevel@tonic-gate boolean_t
56797c478bd9Sstevel@tonic-gate ddi_can_receive_sig(void)
56807c478bd9Sstevel@tonic-gate {
56817c478bd9Sstevel@tonic-gate 	proc_t *pp;
56827c478bd9Sstevel@tonic-gate 
56837c478bd9Sstevel@tonic-gate 	if (curthread->t_proc_flag & TP_LWPEXIT)
56847c478bd9Sstevel@tonic-gate 		return (B_FALSE);
56857c478bd9Sstevel@tonic-gate 	if ((pp = ttoproc(curthread)) == NULL)
56867c478bd9Sstevel@tonic-gate 		return (B_FALSE);
56877c478bd9Sstevel@tonic-gate 	return (pp->p_as != &kas);
56887c478bd9Sstevel@tonic-gate }
56897c478bd9Sstevel@tonic-gate 
56907c478bd9Sstevel@tonic-gate /*
56917c478bd9Sstevel@tonic-gate  * Swap bytes in 16-bit [half-]words
56927c478bd9Sstevel@tonic-gate  */
56937c478bd9Sstevel@tonic-gate void
56947c478bd9Sstevel@tonic-gate swab(void *src, void *dst, size_t nbytes)
56957c478bd9Sstevel@tonic-gate {
56967c478bd9Sstevel@tonic-gate 	uchar_t *pf = (uchar_t *)src;
56977c478bd9Sstevel@tonic-gate 	uchar_t *pt = (uchar_t *)dst;
56987c478bd9Sstevel@tonic-gate 	uchar_t tmp;
56997c478bd9Sstevel@tonic-gate 	int nshorts;
57007c478bd9Sstevel@tonic-gate 
57017c478bd9Sstevel@tonic-gate 	nshorts = nbytes >> 1;
57027c478bd9Sstevel@tonic-gate 
57037c478bd9Sstevel@tonic-gate 	while (--nshorts >= 0) {
57047c478bd9Sstevel@tonic-gate 		tmp = *pf++;
57057c478bd9Sstevel@tonic-gate 		*pt++ = *pf++;
57067c478bd9Sstevel@tonic-gate 		*pt++ = tmp;
57077c478bd9Sstevel@tonic-gate 	}
57087c478bd9Sstevel@tonic-gate }
57097c478bd9Sstevel@tonic-gate 
57107c478bd9Sstevel@tonic-gate static void
57117c478bd9Sstevel@tonic-gate ddi_append_minor_node(dev_info_t *ddip, struct ddi_minor_data *dmdp)
57127c478bd9Sstevel@tonic-gate {
5713b9ccdc5aScth 	int			circ;
5714b9ccdc5aScth 	struct ddi_minor_data	*dp;
57157c478bd9Sstevel@tonic-gate 
5716b9ccdc5aScth 	ndi_devi_enter(ddip, &circ);
57177c478bd9Sstevel@tonic-gate 	if ((dp = DEVI(ddip)->devi_minor) == (struct ddi_minor_data *)NULL) {
57187c478bd9Sstevel@tonic-gate 		DEVI(ddip)->devi_minor = dmdp;
57197c478bd9Sstevel@tonic-gate 	} else {
57207c478bd9Sstevel@tonic-gate 		while (dp->next != (struct ddi_minor_data *)NULL)
57217c478bd9Sstevel@tonic-gate 			dp = dp->next;
57227c478bd9Sstevel@tonic-gate 		dp->next = dmdp;
57237c478bd9Sstevel@tonic-gate 	}
5724b9ccdc5aScth 	ndi_devi_exit(ddip, circ);
57257c478bd9Sstevel@tonic-gate }
57267c478bd9Sstevel@tonic-gate 
57277c478bd9Sstevel@tonic-gate /*
57287c478bd9Sstevel@tonic-gate  * Part of the obsolete SunCluster DDI Hooks.
57297c478bd9Sstevel@tonic-gate  * Keep for binary compatibility
57307c478bd9Sstevel@tonic-gate  */
57317c478bd9Sstevel@tonic-gate minor_t
57327c478bd9Sstevel@tonic-gate ddi_getiminor(dev_t dev)
57337c478bd9Sstevel@tonic-gate {
57347c478bd9Sstevel@tonic-gate 	return (getminor(dev));
57357c478bd9Sstevel@tonic-gate }
57367c478bd9Sstevel@tonic-gate 
57377c478bd9Sstevel@tonic-gate static int
57387c478bd9Sstevel@tonic-gate i_log_devfs_minor_create(dev_info_t *dip, char *minor_name)
57397c478bd9Sstevel@tonic-gate {
57407c478bd9Sstevel@tonic-gate 	int se_flag;
57417c478bd9Sstevel@tonic-gate 	int kmem_flag;
57427c478bd9Sstevel@tonic-gate 	int se_err;
574330294554Sphitran 	char *pathname, *class_name;
57447c478bd9Sstevel@tonic-gate 	sysevent_t *ev = NULL;
57457c478bd9Sstevel@tonic-gate 	sysevent_id_t eid;
57467c478bd9Sstevel@tonic-gate 	sysevent_value_t se_val;
57477c478bd9Sstevel@tonic-gate 	sysevent_attr_list_t *ev_attr_list = NULL;
57487c478bd9Sstevel@tonic-gate 
57497c478bd9Sstevel@tonic-gate 	/* determine interrupt context */
57507c478bd9Sstevel@tonic-gate 	se_flag = (servicing_interrupt()) ? SE_NOSLEEP : SE_SLEEP;
57517c478bd9Sstevel@tonic-gate 	kmem_flag = (se_flag == SE_SLEEP) ? KM_SLEEP : KM_NOSLEEP;
57527c478bd9Sstevel@tonic-gate 
5753*4c06356bSdh 	i_ddi_di_cache_invalidate();
57547c478bd9Sstevel@tonic-gate 
57557c478bd9Sstevel@tonic-gate #ifdef DEBUG
57567c478bd9Sstevel@tonic-gate 	if ((se_flag == SE_NOSLEEP) && sunddi_debug) {
57577c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "ddi_create_minor_node: called from "
57587c478bd9Sstevel@tonic-gate 		    "interrupt level by driver %s",
57597c478bd9Sstevel@tonic-gate 		    ddi_driver_name(dip));
57607c478bd9Sstevel@tonic-gate 	}
57617c478bd9Sstevel@tonic-gate #endif /* DEBUG */
57627c478bd9Sstevel@tonic-gate 
57637c478bd9Sstevel@tonic-gate 	ev = sysevent_alloc(EC_DEVFS, ESC_DEVFS_MINOR_CREATE, EP_DDI, se_flag);
57647c478bd9Sstevel@tonic-gate 	if (ev == NULL) {
57657c478bd9Sstevel@tonic-gate 		goto fail;
57667c478bd9Sstevel@tonic-gate 	}
57677c478bd9Sstevel@tonic-gate 
57687c478bd9Sstevel@tonic-gate 	pathname = kmem_alloc(MAXPATHLEN, kmem_flag);
57697c478bd9Sstevel@tonic-gate 	if (pathname == NULL) {
57707c478bd9Sstevel@tonic-gate 		sysevent_free(ev);
57717c478bd9Sstevel@tonic-gate 		goto fail;
57727c478bd9Sstevel@tonic-gate 	}
57737c478bd9Sstevel@tonic-gate 
57747c478bd9Sstevel@tonic-gate 	(void) ddi_pathname(dip, pathname);
57757c478bd9Sstevel@tonic-gate 	ASSERT(strlen(pathname));
57767c478bd9Sstevel@tonic-gate 	se_val.value_type = SE_DATA_TYPE_STRING;
57777c478bd9Sstevel@tonic-gate 	se_val.value.sv_string = pathname;
57787c478bd9Sstevel@tonic-gate 	if (sysevent_add_attr(&ev_attr_list, DEVFS_PATHNAME,
57797c478bd9Sstevel@tonic-gate 	    &se_val, se_flag) != 0) {
57807c478bd9Sstevel@tonic-gate 		kmem_free(pathname, MAXPATHLEN);
57817c478bd9Sstevel@tonic-gate 		sysevent_free(ev);
57827c478bd9Sstevel@tonic-gate 		goto fail;
57837c478bd9Sstevel@tonic-gate 	}
57847c478bd9Sstevel@tonic-gate 	kmem_free(pathname, MAXPATHLEN);
57857c478bd9Sstevel@tonic-gate 
578630294554Sphitran 	/* add the device class attribute */
578730294554Sphitran 	if ((class_name = i_ddi_devi_class(dip)) != NULL) {
578830294554Sphitran 		se_val.value_type = SE_DATA_TYPE_STRING;
578930294554Sphitran 		se_val.value.sv_string = class_name;
579030294554Sphitran 		if (sysevent_add_attr(&ev_attr_list,
579130294554Sphitran 		    DEVFS_DEVI_CLASS, &se_val, SE_SLEEP) != 0) {
579230294554Sphitran 			sysevent_free_attr(ev_attr_list);
579330294554Sphitran 			goto fail;
579430294554Sphitran 		}
579530294554Sphitran 	}
579630294554Sphitran 
57977c478bd9Sstevel@tonic-gate 	/*
57987c478bd9Sstevel@tonic-gate 	 * allow for NULL minor names
57997c478bd9Sstevel@tonic-gate 	 */
58007c478bd9Sstevel@tonic-gate 	if (minor_name != NULL) {
58017c478bd9Sstevel@tonic-gate 		se_val.value.sv_string = minor_name;
58027c478bd9Sstevel@tonic-gate 		if (sysevent_add_attr(&ev_attr_list, DEVFS_MINOR_NAME,
58037c478bd9Sstevel@tonic-gate 		    &se_val, se_flag) != 0) {
58047c478bd9Sstevel@tonic-gate 			sysevent_free_attr(ev_attr_list);
58057c478bd9Sstevel@tonic-gate 			sysevent_free(ev);
58067c478bd9Sstevel@tonic-gate 			goto fail;
58077c478bd9Sstevel@tonic-gate 		}
58087c478bd9Sstevel@tonic-gate 	}
58097c478bd9Sstevel@tonic-gate 
58107c478bd9Sstevel@tonic-gate 	if (sysevent_attach_attributes(ev, ev_attr_list) != 0) {
58117c478bd9Sstevel@tonic-gate 		sysevent_free_attr(ev_attr_list);
58127c478bd9Sstevel@tonic-gate 		sysevent_free(ev);
58137c478bd9Sstevel@tonic-gate 		goto fail;
58147c478bd9Sstevel@tonic-gate 	}
58157c478bd9Sstevel@tonic-gate 
58167c478bd9Sstevel@tonic-gate 	if ((se_err = log_sysevent(ev, se_flag, &eid)) != 0) {
58177c478bd9Sstevel@tonic-gate 		if (se_err == SE_NO_TRANSPORT) {
58187c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "/devices or /dev may not be current "
58197c478bd9Sstevel@tonic-gate 			    "for driver %s (%s). Run devfsadm -i %s",
58207c478bd9Sstevel@tonic-gate 			    ddi_driver_name(dip), "syseventd not responding",
58217c478bd9Sstevel@tonic-gate 			    ddi_driver_name(dip));
58227c478bd9Sstevel@tonic-gate 		} else {
58237c478bd9Sstevel@tonic-gate 			sysevent_free(ev);
58247c478bd9Sstevel@tonic-gate 			goto fail;
58257c478bd9Sstevel@tonic-gate 		}
58267c478bd9Sstevel@tonic-gate 	}
58277c478bd9Sstevel@tonic-gate 
58287c478bd9Sstevel@tonic-gate 	sysevent_free(ev);
58297c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
58307c478bd9Sstevel@tonic-gate fail:
58317c478bd9Sstevel@tonic-gate 	cmn_err(CE_WARN, "/devices or /dev may not be current "
58327c478bd9Sstevel@tonic-gate 	    "for driver %s. Run devfsadm -i %s",
58337c478bd9Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_driver_name(dip));
58347c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
58357c478bd9Sstevel@tonic-gate }
58367c478bd9Sstevel@tonic-gate 
58377c478bd9Sstevel@tonic-gate /*
58387c478bd9Sstevel@tonic-gate  * failing to remove a minor node is not of interest
58397c478bd9Sstevel@tonic-gate  * therefore we do not generate an error message
58407c478bd9Sstevel@tonic-gate  */
58417c478bd9Sstevel@tonic-gate static int
58427c478bd9Sstevel@tonic-gate i_log_devfs_minor_remove(dev_info_t *dip, char *minor_name)
58437c478bd9Sstevel@tonic-gate {
584430294554Sphitran 	char *pathname, *class_name;
58457c478bd9Sstevel@tonic-gate 	sysevent_t *ev;
58467c478bd9Sstevel@tonic-gate 	sysevent_id_t eid;
58477c478bd9Sstevel@tonic-gate 	sysevent_value_t se_val;
58487c478bd9Sstevel@tonic-gate 	sysevent_attr_list_t *ev_attr_list = NULL;
58497c478bd9Sstevel@tonic-gate 
58507c478bd9Sstevel@tonic-gate 	/*
58517c478bd9Sstevel@tonic-gate 	 * only log ddi_remove_minor_node() calls outside the scope
58527c478bd9Sstevel@tonic-gate 	 * of attach/detach reconfigurations and when the dip is
58537c478bd9Sstevel@tonic-gate 	 * still initialized.
58547c478bd9Sstevel@tonic-gate 	 */
58557c478bd9Sstevel@tonic-gate 	if (DEVI_IS_ATTACHING(dip) || DEVI_IS_DETACHING(dip) ||
58567c478bd9Sstevel@tonic-gate 	    (i_ddi_node_state(dip) < DS_INITIALIZED)) {
58577c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
58587c478bd9Sstevel@tonic-gate 	}
58597c478bd9Sstevel@tonic-gate 
5860*4c06356bSdh 	i_ddi_di_cache_invalidate();
58617c478bd9Sstevel@tonic-gate 
58627c478bd9Sstevel@tonic-gate 	ev = sysevent_alloc(EC_DEVFS, ESC_DEVFS_MINOR_REMOVE, EP_DDI, SE_SLEEP);
58637c478bd9Sstevel@tonic-gate 	if (ev == NULL) {
58647c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
58657c478bd9Sstevel@tonic-gate 	}
58667c478bd9Sstevel@tonic-gate 
58677c478bd9Sstevel@tonic-gate 	pathname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
58687c478bd9Sstevel@tonic-gate 	if (pathname == NULL) {
58697c478bd9Sstevel@tonic-gate 		sysevent_free(ev);
58707c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
58717c478bd9Sstevel@tonic-gate 	}
58727c478bd9Sstevel@tonic-gate 
58737c478bd9Sstevel@tonic-gate 	(void) ddi_pathname(dip, pathname);
58747c478bd9Sstevel@tonic-gate 	ASSERT(strlen(pathname));
58757c478bd9Sstevel@tonic-gate 	se_val.value_type = SE_DATA_TYPE_STRING;
58767c478bd9Sstevel@tonic-gate 	se_val.value.sv_string = pathname;
58777c478bd9Sstevel@tonic-gate 	if (sysevent_add_attr(&ev_attr_list, DEVFS_PATHNAME,
58787c478bd9Sstevel@tonic-gate 	    &se_val, SE_SLEEP) != 0) {
58797c478bd9Sstevel@tonic-gate 		kmem_free(pathname, MAXPATHLEN);
58807c478bd9Sstevel@tonic-gate 		sysevent_free(ev);
58817c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
58827c478bd9Sstevel@tonic-gate 	}
58837c478bd9Sstevel@tonic-gate 
58847c478bd9Sstevel@tonic-gate 	kmem_free(pathname, MAXPATHLEN);
58857c478bd9Sstevel@tonic-gate 
58867c478bd9Sstevel@tonic-gate 	/*
58877c478bd9Sstevel@tonic-gate 	 * allow for NULL minor names
58887c478bd9Sstevel@tonic-gate 	 */
58897c478bd9Sstevel@tonic-gate 	if (minor_name != NULL) {
58907c478bd9Sstevel@tonic-gate 		se_val.value.sv_string = minor_name;
58917c478bd9Sstevel@tonic-gate 		if (sysevent_add_attr(&ev_attr_list, DEVFS_MINOR_NAME,
58927c478bd9Sstevel@tonic-gate 		    &se_val, SE_SLEEP) != 0) {
58937c478bd9Sstevel@tonic-gate 			sysevent_free_attr(ev_attr_list);
58947c478bd9Sstevel@tonic-gate 			goto fail;
58957c478bd9Sstevel@tonic-gate 		}
58967c478bd9Sstevel@tonic-gate 	}
58977c478bd9Sstevel@tonic-gate 
589830294554Sphitran 	if ((class_name = i_ddi_devi_class(dip)) != NULL) {
589930294554Sphitran 		/* add the device class, driver name and instance attributes */
590030294554Sphitran 
590130294554Sphitran 		se_val.value_type = SE_DATA_TYPE_STRING;
590230294554Sphitran 		se_val.value.sv_string = class_name;
590330294554Sphitran 		if (sysevent_add_attr(&ev_attr_list,
590430294554Sphitran 		    DEVFS_DEVI_CLASS, &se_val, SE_SLEEP) != 0) {
590530294554Sphitran 			sysevent_free_attr(ev_attr_list);
590630294554Sphitran 			goto fail;
590730294554Sphitran 		}
590830294554Sphitran 
590930294554Sphitran 		se_val.value_type = SE_DATA_TYPE_STRING;
591030294554Sphitran 		se_val.value.sv_string = (char *)ddi_driver_name(dip);
591130294554Sphitran 		if (sysevent_add_attr(&ev_attr_list,
591230294554Sphitran 		    DEVFS_DRIVER_NAME, &se_val, SE_SLEEP) != 0) {
591330294554Sphitran 			sysevent_free_attr(ev_attr_list);
591430294554Sphitran 			goto fail;
591530294554Sphitran 		}
591630294554Sphitran 
591730294554Sphitran 		se_val.value_type = SE_DATA_TYPE_INT32;
591830294554Sphitran 		se_val.value.sv_int32 = ddi_get_instance(dip);
591930294554Sphitran 		if (sysevent_add_attr(&ev_attr_list,
592030294554Sphitran 		    DEVFS_INSTANCE, &se_val, SE_SLEEP) != 0) {
592130294554Sphitran 			sysevent_free_attr(ev_attr_list);
592230294554Sphitran 			goto fail;
592330294554Sphitran 		}
592430294554Sphitran 
592530294554Sphitran 	}
592630294554Sphitran 
59277c478bd9Sstevel@tonic-gate 	if (sysevent_attach_attributes(ev, ev_attr_list) != 0) {
59287c478bd9Sstevel@tonic-gate 		sysevent_free_attr(ev_attr_list);
59297c478bd9Sstevel@tonic-gate 	} else {
59307c478bd9Sstevel@tonic-gate 		(void) log_sysevent(ev, SE_SLEEP, &eid);
59317c478bd9Sstevel@tonic-gate 	}
59327c478bd9Sstevel@tonic-gate fail:
59337c478bd9Sstevel@tonic-gate 	sysevent_free(ev);
59347c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
59357c478bd9Sstevel@tonic-gate }
59367c478bd9Sstevel@tonic-gate 
59377c478bd9Sstevel@tonic-gate /*
59387c478bd9Sstevel@tonic-gate  * Derive the device class of the node.
59397c478bd9Sstevel@tonic-gate  * Device class names aren't defined yet. Until this is done we use
59407c478bd9Sstevel@tonic-gate  * devfs event subclass names as device class names.
59417c478bd9Sstevel@tonic-gate  */
59427c478bd9Sstevel@tonic-gate static int
59437c478bd9Sstevel@tonic-gate derive_devi_class(dev_info_t *dip, char *node_type, int flag)
59447c478bd9Sstevel@tonic-gate {
59457c478bd9Sstevel@tonic-gate 	int rv = DDI_SUCCESS;
59467c478bd9Sstevel@tonic-gate 
59477c478bd9Sstevel@tonic-gate 	if (i_ddi_devi_class(dip) == NULL) {
59487c478bd9Sstevel@tonic-gate 		if (strncmp(node_type, DDI_NT_BLOCK,
59497c478bd9Sstevel@tonic-gate 		    sizeof (DDI_NT_BLOCK) - 1) == 0 &&
59507c478bd9Sstevel@tonic-gate 		    (node_type[sizeof (DDI_NT_BLOCK) - 1] == '\0' ||
59517c478bd9Sstevel@tonic-gate 		    node_type[sizeof (DDI_NT_BLOCK) - 1] == ':') &&
59527c478bd9Sstevel@tonic-gate 		    strcmp(node_type, DDI_NT_FD) != 0) {
59537c478bd9Sstevel@tonic-gate 
59547c478bd9Sstevel@tonic-gate 			rv = i_ddi_set_devi_class(dip, ESC_DISK, flag);
59557c478bd9Sstevel@tonic-gate 
59567c478bd9Sstevel@tonic-gate 		} else if (strncmp(node_type, DDI_NT_NET,
59577c478bd9Sstevel@tonic-gate 		    sizeof (DDI_NT_NET) - 1) == 0 &&
59587c478bd9Sstevel@tonic-gate 		    (node_type[sizeof (DDI_NT_NET) - 1] == '\0' ||
59597c478bd9Sstevel@tonic-gate 		    node_type[sizeof (DDI_NT_NET) - 1] == ':')) {
59607c478bd9Sstevel@tonic-gate 
59617c478bd9Sstevel@tonic-gate 			rv = i_ddi_set_devi_class(dip, ESC_NETWORK, flag);
5962db11e6feSjacobs 
5963db11e6feSjacobs 		} else if (strncmp(node_type, DDI_NT_PRINTER,
5964db11e6feSjacobs 		    sizeof (DDI_NT_PRINTER) - 1) == 0 &&
5965db11e6feSjacobs 		    (node_type[sizeof (DDI_NT_PRINTER) - 1] == '\0' ||
5966db11e6feSjacobs 		    node_type[sizeof (DDI_NT_PRINTER) - 1] == ':')) {
5967db11e6feSjacobs 
5968db11e6feSjacobs 			rv = i_ddi_set_devi_class(dip, ESC_PRINTER, flag);
596930294554Sphitran 
597030294554Sphitran 		} else if (strncmp(node_type, DDI_PSEUDO,
597130294554Sphitran 		    sizeof (DDI_PSEUDO) -1) == 0 &&
597230294554Sphitran 		    (strncmp(ESC_LOFI, ddi_node_name(dip),
597330294554Sphitran 		    sizeof (ESC_LOFI) -1) == 0)) {
597430294554Sphitran 			rv = i_ddi_set_devi_class(dip, ESC_LOFI, flag);
59757c478bd9Sstevel@tonic-gate 		}
59767c478bd9Sstevel@tonic-gate 	}
59777c478bd9Sstevel@tonic-gate 
59787c478bd9Sstevel@tonic-gate 	return (rv);
59797c478bd9Sstevel@tonic-gate }
59807c478bd9Sstevel@tonic-gate 
59817c478bd9Sstevel@tonic-gate /*
59827c478bd9Sstevel@tonic-gate  * Check compliance with PSARC 2003/375:
59837c478bd9Sstevel@tonic-gate  *
59847c478bd9Sstevel@tonic-gate  * The name must contain only characters a-z, A-Z, 0-9 or _ and it must not
59857c478bd9Sstevel@tonic-gate  * exceed IFNAMSIZ (16) characters in length.
59867c478bd9Sstevel@tonic-gate  */
59877c478bd9Sstevel@tonic-gate static boolean_t
59887c478bd9Sstevel@tonic-gate verify_name(char *name)
59897c478bd9Sstevel@tonic-gate {
59907c478bd9Sstevel@tonic-gate 	size_t	len = strlen(name);
59917c478bd9Sstevel@tonic-gate 	char	*cp;
59927c478bd9Sstevel@tonic-gate 
59937c478bd9Sstevel@tonic-gate 	if (len == 0 || len > IFNAMSIZ)
59947c478bd9Sstevel@tonic-gate 		return (B_FALSE);
59957c478bd9Sstevel@tonic-gate 
59967c478bd9Sstevel@tonic-gate 	for (cp = name; *cp != '\0'; cp++) {
59977c478bd9Sstevel@tonic-gate 		if (!isalnum(*cp) && *cp != '_')
59987c478bd9Sstevel@tonic-gate 			return (B_FALSE);
59997c478bd9Sstevel@tonic-gate 	}
60007c478bd9Sstevel@tonic-gate 
60017c478bd9Sstevel@tonic-gate 	return (B_TRUE);
60027c478bd9Sstevel@tonic-gate }
60037c478bd9Sstevel@tonic-gate 
60047c478bd9Sstevel@tonic-gate /*
60057c478bd9Sstevel@tonic-gate  * ddi_create_minor_common:	Create a  ddi_minor_data structure and
60067c478bd9Sstevel@tonic-gate  *				attach it to the given devinfo node.
60077c478bd9Sstevel@tonic-gate  */
60087c478bd9Sstevel@tonic-gate 
60097c478bd9Sstevel@tonic-gate int
60107c478bd9Sstevel@tonic-gate ddi_create_minor_common(dev_info_t *dip, char *name, int spec_type,
60117c478bd9Sstevel@tonic-gate     minor_t minor_num, char *node_type, int flag, ddi_minor_type mtype,
60127c478bd9Sstevel@tonic-gate     const char *read_priv, const char *write_priv, mode_t priv_mode)
60137c478bd9Sstevel@tonic-gate {
60147c478bd9Sstevel@tonic-gate 	struct ddi_minor_data *dmdp;
60157c478bd9Sstevel@tonic-gate 	major_t major;
60167c478bd9Sstevel@tonic-gate 
60177c478bd9Sstevel@tonic-gate 	if (spec_type != S_IFCHR && spec_type != S_IFBLK)
60187c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
60197c478bd9Sstevel@tonic-gate 
60207c478bd9Sstevel@tonic-gate 	if (name == NULL)
60217c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
60227c478bd9Sstevel@tonic-gate 
60237c478bd9Sstevel@tonic-gate 	/*
60247c478bd9Sstevel@tonic-gate 	 * Log a message if the minor number the driver is creating
60257c478bd9Sstevel@tonic-gate 	 * is not expressible on the on-disk filesystem (currently
60267c478bd9Sstevel@tonic-gate 	 * this is limited to 18 bits both by UFS). The device can
60277c478bd9Sstevel@tonic-gate 	 * be opened via devfs, but not by device special files created
60287c478bd9Sstevel@tonic-gate 	 * via mknod().
60297c478bd9Sstevel@tonic-gate 	 */
60307c478bd9Sstevel@tonic-gate 	if (minor_num > L_MAXMIN32) {
60317c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN,
60327c478bd9Sstevel@tonic-gate 		    "%s%d:%s minor 0x%x too big for 32-bit applications",
60337c478bd9Sstevel@tonic-gate 		    ddi_driver_name(dip), ddi_get_instance(dip),
60347c478bd9Sstevel@tonic-gate 		    name, minor_num);
60357c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
60367c478bd9Sstevel@tonic-gate 	}
60377c478bd9Sstevel@tonic-gate 
60387c478bd9Sstevel@tonic-gate 	/* dip must be bound and attached */
60397c478bd9Sstevel@tonic-gate 	major = ddi_driver_major(dip);
6040a204de77Scth 	ASSERT(major != DDI_MAJOR_T_NONE);
60417c478bd9Sstevel@tonic-gate 
60427c478bd9Sstevel@tonic-gate 	/*
60437c478bd9Sstevel@tonic-gate 	 * Default node_type to DDI_PSEUDO and issue notice in debug mode
60447c478bd9Sstevel@tonic-gate 	 */
60457c478bd9Sstevel@tonic-gate 	if (node_type == NULL) {
60467c478bd9Sstevel@tonic-gate 		node_type = DDI_PSEUDO;
60477c478bd9Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_NOTE, "!illegal node_type NULL for %s%d "
60487c478bd9Sstevel@tonic-gate 		    " minor node %s; default to DDI_PSEUDO",
60497c478bd9Sstevel@tonic-gate 		    ddi_driver_name(dip), ddi_get_instance(dip), name));
60507c478bd9Sstevel@tonic-gate 	}
60517c478bd9Sstevel@tonic-gate 
60527c478bd9Sstevel@tonic-gate 	/*
60537c478bd9Sstevel@tonic-gate 	 * If the driver is a network driver, ensure that the name falls within
60547c478bd9Sstevel@tonic-gate 	 * the interface naming constraints specified by PSARC/2003/375.
60557c478bd9Sstevel@tonic-gate 	 */
60567c478bd9Sstevel@tonic-gate 	if (strcmp(node_type, DDI_NT_NET) == 0) {
60577c478bd9Sstevel@tonic-gate 		if (!verify_name(name))
60587c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
60597c478bd9Sstevel@tonic-gate 
60607c478bd9Sstevel@tonic-gate 		if (mtype == DDM_MINOR) {
60617c478bd9Sstevel@tonic-gate 			struct devnames *dnp = &devnamesp[major];
60627c478bd9Sstevel@tonic-gate 
60637c478bd9Sstevel@tonic-gate 			/* Mark driver as a network driver */
60647c478bd9Sstevel@tonic-gate 			LOCK_DEV_OPS(&dnp->dn_lock);
60657c478bd9Sstevel@tonic-gate 			dnp->dn_flags |= DN_NETWORK_DRIVER;
60667c478bd9Sstevel@tonic-gate 			UNLOCK_DEV_OPS(&dnp->dn_lock);
60677c478bd9Sstevel@tonic-gate 		}
60687c478bd9Sstevel@tonic-gate 	}
60697c478bd9Sstevel@tonic-gate 
60707c478bd9Sstevel@tonic-gate 	if (mtype == DDM_MINOR) {
60717c478bd9Sstevel@tonic-gate 		if (derive_devi_class(dip,  node_type, KM_NOSLEEP) !=
60727c478bd9Sstevel@tonic-gate 		    DDI_SUCCESS)
60737c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
60747c478bd9Sstevel@tonic-gate 	}
60757c478bd9Sstevel@tonic-gate 
60767c478bd9Sstevel@tonic-gate 	/*
60777c478bd9Sstevel@tonic-gate 	 * Take care of minor number information for the node.
60787c478bd9Sstevel@tonic-gate 	 */
60797c478bd9Sstevel@tonic-gate 
60807c478bd9Sstevel@tonic-gate 	if ((dmdp = kmem_zalloc(sizeof (struct ddi_minor_data),
60817c478bd9Sstevel@tonic-gate 	    KM_NOSLEEP)) == NULL) {
60827c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
60837c478bd9Sstevel@tonic-gate 	}
60847c478bd9Sstevel@tonic-gate 	if ((dmdp->ddm_name = i_ddi_strdup(name, KM_NOSLEEP)) == NULL) {
60857c478bd9Sstevel@tonic-gate 		kmem_free(dmdp, sizeof (struct ddi_minor_data));
60867c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
60877c478bd9Sstevel@tonic-gate 	}
60887c478bd9Sstevel@tonic-gate 	dmdp->dip = dip;
60897c478bd9Sstevel@tonic-gate 	dmdp->ddm_dev = makedevice(major, minor_num);
60907c478bd9Sstevel@tonic-gate 	dmdp->ddm_spec_type = spec_type;
60917c478bd9Sstevel@tonic-gate 	dmdp->ddm_node_type = node_type;
60927c478bd9Sstevel@tonic-gate 	dmdp->type = mtype;
60937c478bd9Sstevel@tonic-gate 	if (flag & CLONE_DEV) {
60947c478bd9Sstevel@tonic-gate 		dmdp->type = DDM_ALIAS;
60957c478bd9Sstevel@tonic-gate 		dmdp->ddm_dev = makedevice(ddi_driver_major(clone_dip), major);
60967c478bd9Sstevel@tonic-gate 	}
60977c478bd9Sstevel@tonic-gate 	if (flag & PRIVONLY_DEV) {
60987c478bd9Sstevel@tonic-gate 		dmdp->ddm_flags |= DM_NO_FSPERM;
60997c478bd9Sstevel@tonic-gate 	}
61007c478bd9Sstevel@tonic-gate 	if (read_priv || write_priv) {
61017c478bd9Sstevel@tonic-gate 		dmdp->ddm_node_priv =
61027c478bd9Sstevel@tonic-gate 		    devpolicy_priv_by_name(read_priv, write_priv);
61037c478bd9Sstevel@tonic-gate 	}
61047c478bd9Sstevel@tonic-gate 	dmdp->ddm_priv_mode = priv_mode;
61057c478bd9Sstevel@tonic-gate 
61067c478bd9Sstevel@tonic-gate 	ddi_append_minor_node(dip, dmdp);
61077c478bd9Sstevel@tonic-gate 
61087c478bd9Sstevel@tonic-gate 	/*
61097c478bd9Sstevel@tonic-gate 	 * only log ddi_create_minor_node() calls which occur
61107c478bd9Sstevel@tonic-gate 	 * outside the scope of attach(9e)/detach(9e) reconfigurations
61117c478bd9Sstevel@tonic-gate 	 */
61127b3700d1Sszhou 	if (!(DEVI_IS_ATTACHING(dip) || DEVI_IS_DETACHING(dip)) &&
61137b3700d1Sszhou 	    mtype != DDM_INTERNAL_PATH) {
61147c478bd9Sstevel@tonic-gate 		(void) i_log_devfs_minor_create(dip, name);
61157c478bd9Sstevel@tonic-gate 	}
61167c478bd9Sstevel@tonic-gate 
61177c478bd9Sstevel@tonic-gate 	/*
61187c478bd9Sstevel@tonic-gate 	 * Check if any dacf rules match the creation of this minor node
61197c478bd9Sstevel@tonic-gate 	 */
61207c478bd9Sstevel@tonic-gate 	dacfc_match_create_minor(name, node_type, dip, dmdp, flag);
61217c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
61227c478bd9Sstevel@tonic-gate }
61237c478bd9Sstevel@tonic-gate 
61247c478bd9Sstevel@tonic-gate int
61257c478bd9Sstevel@tonic-gate ddi_create_minor_node(dev_info_t *dip, char *name, int spec_type,
61267c478bd9Sstevel@tonic-gate     minor_t minor_num, char *node_type, int flag)
61277c478bd9Sstevel@tonic-gate {
61287c478bd9Sstevel@tonic-gate 	return (ddi_create_minor_common(dip, name, spec_type, minor_num,
61297c478bd9Sstevel@tonic-gate 	    node_type, flag, DDM_MINOR, NULL, NULL, 0));
61307c478bd9Sstevel@tonic-gate }
61317c478bd9Sstevel@tonic-gate 
61327c478bd9Sstevel@tonic-gate int
61337c478bd9Sstevel@tonic-gate ddi_create_priv_minor_node(dev_info_t *dip, char *name, int spec_type,
61347c478bd9Sstevel@tonic-gate     minor_t minor_num, char *node_type, int flag,
61357c478bd9Sstevel@tonic-gate     const char *rdpriv, const char *wrpriv, mode_t priv_mode)
61367c478bd9Sstevel@tonic-gate {
61377c478bd9Sstevel@tonic-gate 	return (ddi_create_minor_common(dip, name, spec_type, minor_num,
61387c478bd9Sstevel@tonic-gate 	    node_type, flag, DDM_MINOR, rdpriv, wrpriv, priv_mode));
61397c478bd9Sstevel@tonic-gate }
61407c478bd9Sstevel@tonic-gate 
61417c478bd9Sstevel@tonic-gate int
61427c478bd9Sstevel@tonic-gate ddi_create_default_minor_node(dev_info_t *dip, char *name, int spec_type,
61437c478bd9Sstevel@tonic-gate     minor_t minor_num, char *node_type, int flag)
61447c478bd9Sstevel@tonic-gate {
61457c478bd9Sstevel@tonic-gate 	return (ddi_create_minor_common(dip, name, spec_type, minor_num,
61467c478bd9Sstevel@tonic-gate 	    node_type, flag, DDM_DEFAULT, NULL, NULL, 0));
61477c478bd9Sstevel@tonic-gate }
61487c478bd9Sstevel@tonic-gate 
61497c478bd9Sstevel@tonic-gate /*
61507c478bd9Sstevel@tonic-gate  * Internal (non-ddi) routine for drivers to export names known
61517c478bd9Sstevel@tonic-gate  * to the kernel (especially ddi_pathname_to_dev_t and friends)
61527c478bd9Sstevel@tonic-gate  * but not exported externally to /dev
61537c478bd9Sstevel@tonic-gate  */
61547c478bd9Sstevel@tonic-gate int
61557c478bd9Sstevel@tonic-gate ddi_create_internal_pathname(dev_info_t *dip, char *name, int spec_type,
61567c478bd9Sstevel@tonic-gate     minor_t minor_num)
61577c478bd9Sstevel@tonic-gate {
61587c478bd9Sstevel@tonic-gate 	return (ddi_create_minor_common(dip, name, spec_type, minor_num,
61597c478bd9Sstevel@tonic-gate 	    "internal", 0, DDM_INTERNAL_PATH, NULL, NULL, 0));
61607c478bd9Sstevel@tonic-gate }
61617c478bd9Sstevel@tonic-gate 
61627c478bd9Sstevel@tonic-gate void
61637c478bd9Sstevel@tonic-gate ddi_remove_minor_node(dev_info_t *dip, char *name)
61647c478bd9Sstevel@tonic-gate {
6165b9ccdc5aScth 	int			circ;
6166b9ccdc5aScth 	struct ddi_minor_data	*dmdp, *dmdp1;
6167b9ccdc5aScth 	struct ddi_minor_data	**dmdp_prev;
61687c478bd9Sstevel@tonic-gate 
6169b9ccdc5aScth 	ndi_devi_enter(dip, &circ);
61707c478bd9Sstevel@tonic-gate 	dmdp_prev = &DEVI(dip)->devi_minor;
61717c478bd9Sstevel@tonic-gate 	dmdp = DEVI(dip)->devi_minor;
61727c478bd9Sstevel@tonic-gate 	while (dmdp != NULL) {
61737c478bd9Sstevel@tonic-gate 		dmdp1 = dmdp->next;
61747c478bd9Sstevel@tonic-gate 		if ((name == NULL || (dmdp->ddm_name != NULL &&
61757c478bd9Sstevel@tonic-gate 		    strcmp(name, dmdp->ddm_name) == 0))) {
61767c478bd9Sstevel@tonic-gate 			if (dmdp->ddm_name != NULL) {
61777b3700d1Sszhou 				if (dmdp->type != DDM_INTERNAL_PATH)
61787b3700d1Sszhou 					(void) i_log_devfs_minor_remove(dip,
61797b3700d1Sszhou 					    dmdp->ddm_name);
61807c478bd9Sstevel@tonic-gate 				kmem_free(dmdp->ddm_name,
61817c478bd9Sstevel@tonic-gate 				    strlen(dmdp->ddm_name) + 1);
61827c478bd9Sstevel@tonic-gate 			}
61837c478bd9Sstevel@tonic-gate 			/*
61847c478bd9Sstevel@tonic-gate 			 * Release device privilege, if any.
61857c478bd9Sstevel@tonic-gate 			 * Release dacf client data associated with this minor
61867c478bd9Sstevel@tonic-gate 			 * node by storing NULL.
61877c478bd9Sstevel@tonic-gate 			 */
61887c478bd9Sstevel@tonic-gate 			if (dmdp->ddm_node_priv)
61897c478bd9Sstevel@tonic-gate 				dpfree(dmdp->ddm_node_priv);
61907c478bd9Sstevel@tonic-gate 			dacf_store_info((dacf_infohdl_t)dmdp, NULL);
61917c478bd9Sstevel@tonic-gate 			kmem_free(dmdp, sizeof (struct ddi_minor_data));
61927c478bd9Sstevel@tonic-gate 			*dmdp_prev = dmdp1;
61937c478bd9Sstevel@tonic-gate 			/*
61947c478bd9Sstevel@tonic-gate 			 * OK, we found it, so get out now -- if we drive on,
61957c478bd9Sstevel@tonic-gate 			 * we will strcmp against garbage.  See 1139209.
61967c478bd9Sstevel@tonic-gate 			 */
61977c478bd9Sstevel@tonic-gate 			if (name != NULL)
61987c478bd9Sstevel@tonic-gate 				break;
61997c478bd9Sstevel@tonic-gate 		} else {
62007c478bd9Sstevel@tonic-gate 			dmdp_prev = &dmdp->next;
62017c478bd9Sstevel@tonic-gate 		}
62027c478bd9Sstevel@tonic-gate 		dmdp = dmdp1;
62037c478bd9Sstevel@tonic-gate 	}
6204b9ccdc5aScth 	ndi_devi_exit(dip, circ);
62057c478bd9Sstevel@tonic-gate }
62067c478bd9Sstevel@tonic-gate 
62077c478bd9Sstevel@tonic-gate 
62087c478bd9Sstevel@tonic-gate int
62097c478bd9Sstevel@tonic-gate ddi_in_panic()
62107c478bd9Sstevel@tonic-gate {
62117c478bd9Sstevel@tonic-gate 	return (panicstr != NULL);
62127c478bd9Sstevel@tonic-gate }
62137c478bd9Sstevel@tonic-gate 
62147c478bd9Sstevel@tonic-gate 
62157c478bd9Sstevel@tonic-gate /*
62167c478bd9Sstevel@tonic-gate  * Find first bit set in a mask (returned counting from 1 up)
62177c478bd9Sstevel@tonic-gate  */
62187c478bd9Sstevel@tonic-gate 
62197c478bd9Sstevel@tonic-gate int
62207c478bd9Sstevel@tonic-gate ddi_ffs(long mask)
62217c478bd9Sstevel@tonic-gate {
62227c478bd9Sstevel@tonic-gate 	return (ffs(mask));
62237c478bd9Sstevel@tonic-gate }
62247c478bd9Sstevel@tonic-gate 
62257c478bd9Sstevel@tonic-gate /*
62267c478bd9Sstevel@tonic-gate  * Find last bit set. Take mask and clear
62277c478bd9Sstevel@tonic-gate  * all but the most significant bit, and
62287c478bd9Sstevel@tonic-gate  * then let ffs do the rest of the work.
62297c478bd9Sstevel@tonic-gate  *
62307c478bd9Sstevel@tonic-gate  * Algorithm courtesy of Steve Chessin.
62317c478bd9Sstevel@tonic-gate  */
62327c478bd9Sstevel@tonic-gate 
62337c478bd9Sstevel@tonic-gate int
62347c478bd9Sstevel@tonic-gate ddi_fls(long mask)
62357c478bd9Sstevel@tonic-gate {
62367c478bd9Sstevel@tonic-gate 	while (mask) {
62377c478bd9Sstevel@tonic-gate 		long nx;
62387c478bd9Sstevel@tonic-gate 
62397c478bd9Sstevel@tonic-gate 		if ((nx = (mask & (mask - 1))) == 0)
62407c478bd9Sstevel@tonic-gate 			break;
62417c478bd9Sstevel@tonic-gate 		mask = nx;
62427c478bd9Sstevel@tonic-gate 	}
62437c478bd9Sstevel@tonic-gate 	return (ffs(mask));
62447c478bd9Sstevel@tonic-gate }
62457c478bd9Sstevel@tonic-gate 
62467c478bd9Sstevel@tonic-gate /*
6247*4c06356bSdh  * The ddi_soft_state_* routines comprise generic storage management utilities
6248*4c06356bSdh  * for driver soft state structures (in "the old days," this was done with
6249*4c06356bSdh  * statically sized array - big systems and dynamic loading and unloading
6250*4c06356bSdh  * make heap allocation more attractive).
62517c478bd9Sstevel@tonic-gate  */
62527c478bd9Sstevel@tonic-gate 
62537c478bd9Sstevel@tonic-gate /*
62547c478bd9Sstevel@tonic-gate  * Allocate a set of pointers to 'n_items' objects of size 'size'
62557c478bd9Sstevel@tonic-gate  * bytes.  Each pointer is initialized to nil.
62567c478bd9Sstevel@tonic-gate  *
62577c478bd9Sstevel@tonic-gate  * The 'size' and 'n_items' values are stashed in the opaque
62587c478bd9Sstevel@tonic-gate  * handle returned to the caller.
62597c478bd9Sstevel@tonic-gate  *
62607c478bd9Sstevel@tonic-gate  * This implementation interprets 'set of pointers' to mean 'array
62617c478bd9Sstevel@tonic-gate  * of pointers' but note that nothing in the interface definition
62627c478bd9Sstevel@tonic-gate  * precludes an implementation that uses, for example, a linked list.
62637c478bd9Sstevel@tonic-gate  * However there should be a small efficiency gain from using an array
62647c478bd9Sstevel@tonic-gate  * at lookup time.
62657c478bd9Sstevel@tonic-gate  *
62667c478bd9Sstevel@tonic-gate  * NOTE	As an optimization, we make our growable array allocations in
62677c478bd9Sstevel@tonic-gate  *	powers of two (bytes), since that's how much kmem_alloc (currently)
62687c478bd9Sstevel@tonic-gate  *	gives us anyway.  It should save us some free/realloc's ..
62697c478bd9Sstevel@tonic-gate  *
62707c478bd9Sstevel@tonic-gate  *	As a further optimization, we make the growable array start out
62717c478bd9Sstevel@tonic-gate  *	with MIN_N_ITEMS in it.
62727c478bd9Sstevel@tonic-gate  */
62737c478bd9Sstevel@tonic-gate 
62747c478bd9Sstevel@tonic-gate #define	MIN_N_ITEMS	8	/* 8 void *'s == 32 bytes */
62757c478bd9Sstevel@tonic-gate 
62767c478bd9Sstevel@tonic-gate int
62777c478bd9Sstevel@tonic-gate ddi_soft_state_init(void **state_p, size_t size, size_t n_items)
62787c478bd9Sstevel@tonic-gate {
6279*4c06356bSdh 	i_ddi_soft_state	*ss;
62807c478bd9Sstevel@tonic-gate 
6281*4c06356bSdh 	if (state_p == NULL || size == 0)
62827c478bd9Sstevel@tonic-gate 		return (EINVAL);
62837c478bd9Sstevel@tonic-gate 
62847c478bd9Sstevel@tonic-gate 	ss = kmem_zalloc(sizeof (*ss), KM_SLEEP);
62857c478bd9Sstevel@tonic-gate 	mutex_init(&ss->lock, NULL, MUTEX_DRIVER, NULL);
62867c478bd9Sstevel@tonic-gate 	ss->size = size;
62877c478bd9Sstevel@tonic-gate 
62887c478bd9Sstevel@tonic-gate 	if (n_items < MIN_N_ITEMS)
62897c478bd9Sstevel@tonic-gate 		ss->n_items = MIN_N_ITEMS;
62907c478bd9Sstevel@tonic-gate 	else {
62917c478bd9Sstevel@tonic-gate 		int bitlog;
62927c478bd9Sstevel@tonic-gate 
62937c478bd9Sstevel@tonic-gate 		if ((bitlog = ddi_fls(n_items)) == ddi_ffs(n_items))
62947c478bd9Sstevel@tonic-gate 			bitlog--;
62957c478bd9Sstevel@tonic-gate 		ss->n_items = 1 << bitlog;
62967c478bd9Sstevel@tonic-gate 	}
62977c478bd9Sstevel@tonic-gate 
62987c478bd9Sstevel@tonic-gate 	ASSERT(ss->n_items >= n_items);
62997c478bd9Sstevel@tonic-gate 
63007c478bd9Sstevel@tonic-gate 	ss->array = kmem_zalloc(ss->n_items * sizeof (void *), KM_SLEEP);
63017c478bd9Sstevel@tonic-gate 
63027c478bd9Sstevel@tonic-gate 	*state_p = ss;
63037c478bd9Sstevel@tonic-gate 	return (0);
63047c478bd9Sstevel@tonic-gate }
63057c478bd9Sstevel@tonic-gate 
63067c478bd9Sstevel@tonic-gate /*
63077c478bd9Sstevel@tonic-gate  * Allocate a state structure of size 'size' to be associated
63087c478bd9Sstevel@tonic-gate  * with item 'item'.
63097c478bd9Sstevel@tonic-gate  *
63107c478bd9Sstevel@tonic-gate  * In this implementation, the array is extended to
63117c478bd9Sstevel@tonic-gate  * allow the requested offset, if needed.
63127c478bd9Sstevel@tonic-gate  */
63137c478bd9Sstevel@tonic-gate int
63147c478bd9Sstevel@tonic-gate ddi_soft_state_zalloc(void *state, int item)
63157c478bd9Sstevel@tonic-gate {
6316*4c06356bSdh 	i_ddi_soft_state	*ss = (i_ddi_soft_state *)state;
6317*4c06356bSdh 	void			**array;
6318*4c06356bSdh 	void			*new_element;
63197c478bd9Sstevel@tonic-gate 
6320*4c06356bSdh 	if ((state == NULL) || (item < 0))
63217c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
63227c478bd9Sstevel@tonic-gate 
63237c478bd9Sstevel@tonic-gate 	mutex_enter(&ss->lock);
63247c478bd9Sstevel@tonic-gate 	if (ss->size == 0) {
63257c478bd9Sstevel@tonic-gate 		mutex_exit(&ss->lock);
63267c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "ddi_soft_state_zalloc: bad handle: %s",
63277c478bd9Sstevel@tonic-gate 		    mod_containing_pc(caller()));
63287c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
63297c478bd9Sstevel@tonic-gate 	}
63307c478bd9Sstevel@tonic-gate 
63317c478bd9Sstevel@tonic-gate 	array = ss->array;	/* NULL if ss->n_items == 0 */
63327c478bd9Sstevel@tonic-gate 	ASSERT(ss->n_items != 0 && array != NULL);
63337c478bd9Sstevel@tonic-gate 
63347c478bd9Sstevel@tonic-gate 	/*
63357c478bd9Sstevel@tonic-gate 	 * refuse to tread on an existing element
63367c478bd9Sstevel@tonic-gate 	 */
63377c478bd9Sstevel@tonic-gate 	if (item < ss->n_items && array[item] != NULL) {
63387c478bd9Sstevel@tonic-gate 		mutex_exit(&ss->lock);
63397c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
63407c478bd9Sstevel@tonic-gate 	}
63417c478bd9Sstevel@tonic-gate 
63427c478bd9Sstevel@tonic-gate 	/*
63437c478bd9Sstevel@tonic-gate 	 * Allocate a new element to plug in
63447c478bd9Sstevel@tonic-gate 	 */
63457c478bd9Sstevel@tonic-gate 	new_element = kmem_zalloc(ss->size, KM_SLEEP);
63467c478bd9Sstevel@tonic-gate 
63477c478bd9Sstevel@tonic-gate 	/*
63487c478bd9Sstevel@tonic-gate 	 * Check if the array is big enough, if not, grow it.
63497c478bd9Sstevel@tonic-gate 	 */
63507c478bd9Sstevel@tonic-gate 	if (item >= ss->n_items) {
6351*4c06356bSdh 		void			**new_array;
6352*4c06356bSdh 		size_t			new_n_items;
6353*4c06356bSdh 		struct i_ddi_soft_state	*dirty;
63547c478bd9Sstevel@tonic-gate 
63557c478bd9Sstevel@tonic-gate 		/*
63567c478bd9Sstevel@tonic-gate 		 * Allocate a new array of the right length, copy
63577c478bd9Sstevel@tonic-gate 		 * all the old pointers to the new array, then
63587c478bd9Sstevel@tonic-gate 		 * if it exists at all, put the old array on the
63597c478bd9Sstevel@tonic-gate 		 * dirty list.
63607c478bd9Sstevel@tonic-gate 		 *
63617c478bd9Sstevel@tonic-gate 		 * Note that we can't kmem_free() the old array.
63627c478bd9Sstevel@tonic-gate 		 *
63637c478bd9Sstevel@tonic-gate 		 * Why -- well the 'get' operation is 'mutex-free', so we
63647c478bd9Sstevel@tonic-gate 		 * can't easily catch a suspended thread that is just about
63657c478bd9Sstevel@tonic-gate 		 * to dereference the array we just grew out of.  So we
63667c478bd9Sstevel@tonic-gate 		 * cons up a header and put it on a list of 'dirty'
63677c478bd9Sstevel@tonic-gate 		 * pointer arrays.  (Dirty in the sense that there may
63687c478bd9Sstevel@tonic-gate 		 * be suspended threads somewhere that are in the middle
63697c478bd9Sstevel@tonic-gate 		 * of referencing them).  Fortunately, we -can- garbage
63707c478bd9Sstevel@tonic-gate 		 * collect it all at ddi_soft_state_fini time.
63717c478bd9Sstevel@tonic-gate 		 */
63727c478bd9Sstevel@tonic-gate 		new_n_items = ss->n_items;
63737c478bd9Sstevel@tonic-gate 		while (new_n_items < (1 + item))
63747c478bd9Sstevel@tonic-gate 			new_n_items <<= 1;	/* double array size .. */
63757c478bd9Sstevel@tonic-gate 
63767c478bd9Sstevel@tonic-gate 		ASSERT(new_n_items >= (1 + item));	/* sanity check! */
63777c478bd9Sstevel@tonic-gate 
63787c478bd9Sstevel@tonic-gate 		new_array = kmem_zalloc(new_n_items * sizeof (void *),
63797c478bd9Sstevel@tonic-gate 		    KM_SLEEP);
63807c478bd9Sstevel@tonic-gate 		/*
63817c478bd9Sstevel@tonic-gate 		 * Copy the pointers into the new array
63827c478bd9Sstevel@tonic-gate 		 */
63837c478bd9Sstevel@tonic-gate 		bcopy(array, new_array, ss->n_items * sizeof (void *));
63847c478bd9Sstevel@tonic-gate 
63857c478bd9Sstevel@tonic-gate 		/*
63867c478bd9Sstevel@tonic-gate 		 * Save the old array on the dirty list
63877c478bd9Sstevel@tonic-gate 		 */
63887c478bd9Sstevel@tonic-gate 		dirty = kmem_zalloc(sizeof (*dirty), KM_SLEEP);
63897c478bd9Sstevel@tonic-gate 		dirty->array = ss->array;
63907c478bd9Sstevel@tonic-gate 		dirty->n_items = ss->n_items;
63917c478bd9Sstevel@tonic-gate 		dirty->next = ss->next;
63927c478bd9Sstevel@tonic-gate 		ss->next = dirty;
63937c478bd9Sstevel@tonic-gate 
63947c478bd9Sstevel@tonic-gate 		ss->array = (array = new_array);
63957c478bd9Sstevel@tonic-gate 		ss->n_items = new_n_items;
63967c478bd9Sstevel@tonic-gate 	}
63977c478bd9Sstevel@tonic-gate 
63987c478bd9Sstevel@tonic-gate 	ASSERT(array != NULL && item < ss->n_items && array[item] == NULL);
63997c478bd9Sstevel@tonic-gate 
64007c478bd9Sstevel@tonic-gate 	array[item] = new_element;
64017c478bd9Sstevel@tonic-gate 
64027c478bd9Sstevel@tonic-gate 	mutex_exit(&ss->lock);
64037c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
64047c478bd9Sstevel@tonic-gate }
64057c478bd9Sstevel@tonic-gate 
64067c478bd9Sstevel@tonic-gate /*
64077c478bd9Sstevel@tonic-gate  * Fetch a pointer to the allocated soft state structure.
64087c478bd9Sstevel@tonic-gate  *
64097c478bd9Sstevel@tonic-gate  * This is designed to be cheap.
64107c478bd9Sstevel@tonic-gate  *
64117c478bd9Sstevel@tonic-gate  * There's an argument that there should be more checking for
64127c478bd9Sstevel@tonic-gate  * nil pointers and out of bounds on the array.. but we do a lot
64137c478bd9Sstevel@tonic-gate  * of that in the alloc/free routines.
64147c478bd9Sstevel@tonic-gate  *
64157c478bd9Sstevel@tonic-gate  * An array has the convenience that we don't need to lock read-access
64167c478bd9Sstevel@tonic-gate  * to it c.f. a linked list.  However our "expanding array" strategy
64177c478bd9Sstevel@tonic-gate  * means that we should hold a readers lock on the i_ddi_soft_state
64187c478bd9Sstevel@tonic-gate  * structure.
64197c478bd9Sstevel@tonic-gate  *
64207c478bd9Sstevel@tonic-gate  * However, from a performance viewpoint, we need to do it without
64217c478bd9Sstevel@tonic-gate  * any locks at all -- this also makes it a leaf routine.  The algorithm
64227c478bd9Sstevel@tonic-gate  * is 'lock-free' because we only discard the pointer arrays at
64237c478bd9Sstevel@tonic-gate  * ddi_soft_state_fini() time.
64247c478bd9Sstevel@tonic-gate  */
64257c478bd9Sstevel@tonic-gate void *
64267c478bd9Sstevel@tonic-gate ddi_get_soft_state(void *state, int item)
64277c478bd9Sstevel@tonic-gate {
6428*4c06356bSdh 	i_ddi_soft_state	*ss = (i_ddi_soft_state *)state;
64297c478bd9Sstevel@tonic-gate 
6430*4c06356bSdh 	ASSERT((ss != NULL) && (item >= 0));
64317c478bd9Sstevel@tonic-gate 
64327c478bd9Sstevel@tonic-gate 	if (item < ss->n_items && ss->array != NULL)
64337c478bd9Sstevel@tonic-gate 		return (ss->array[item]);
64347c478bd9Sstevel@tonic-gate 	return (NULL);
64357c478bd9Sstevel@tonic-gate }
64367c478bd9Sstevel@tonic-gate 
64377c478bd9Sstevel@tonic-gate /*
64387c478bd9Sstevel@tonic-gate  * Free the state structure corresponding to 'item.'   Freeing an
64397c478bd9Sstevel@tonic-gate  * element that has either gone or was never allocated is not
64407c478bd9Sstevel@tonic-gate  * considered an error.  Note that we free the state structure, but
64417c478bd9Sstevel@tonic-gate  * we don't shrink our pointer array, or discard 'dirty' arrays,
64427c478bd9Sstevel@tonic-gate  * since even a few pointers don't really waste too much memory.
64437c478bd9Sstevel@tonic-gate  *
64447c478bd9Sstevel@tonic-gate  * Passing an item number that is out of bounds, or a null pointer will
64457c478bd9Sstevel@tonic-gate  * provoke an error message.
64467c478bd9Sstevel@tonic-gate  */
64477c478bd9Sstevel@tonic-gate void
64487c478bd9Sstevel@tonic-gate ddi_soft_state_free(void *state, int item)
64497c478bd9Sstevel@tonic-gate {
6450*4c06356bSdh 	i_ddi_soft_state	*ss = (i_ddi_soft_state *)state;
6451*4c06356bSdh 	void			**array;
6452*4c06356bSdh 	void			*element;
6453*4c06356bSdh 	static char		msg[] = "ddi_soft_state_free:";
64547c478bd9Sstevel@tonic-gate 
6455*4c06356bSdh 	if (ss == NULL) {
64567c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s null handle: %s",
64577c478bd9Sstevel@tonic-gate 		    msg, mod_containing_pc(caller()));
64587c478bd9Sstevel@tonic-gate 		return;
64597c478bd9Sstevel@tonic-gate 	}
64607c478bd9Sstevel@tonic-gate 
64617c478bd9Sstevel@tonic-gate 	element = NULL;
64627c478bd9Sstevel@tonic-gate 
64637c478bd9Sstevel@tonic-gate 	mutex_enter(&ss->lock);
64647c478bd9Sstevel@tonic-gate 
64657c478bd9Sstevel@tonic-gate 	if ((array = ss->array) == NULL || ss->size == 0) {
64667c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s bad handle: %s",
64677c478bd9Sstevel@tonic-gate 		    msg, mod_containing_pc(caller()));
64687c478bd9Sstevel@tonic-gate 	} else if (item < 0 || item >= ss->n_items) {
64697c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s item %d not in range [0..%lu]: %s",
64707c478bd9Sstevel@tonic-gate 		    msg, item, ss->n_items - 1, mod_containing_pc(caller()));
64717c478bd9Sstevel@tonic-gate 	} else if (array[item] != NULL) {
64727c478bd9Sstevel@tonic-gate 		element = array[item];
64737c478bd9Sstevel@tonic-gate 		array[item] = NULL;
64747c478bd9Sstevel@tonic-gate 	}
64757c478bd9Sstevel@tonic-gate 
64767c478bd9Sstevel@tonic-gate 	mutex_exit(&ss->lock);
64777c478bd9Sstevel@tonic-gate 
64787c478bd9Sstevel@tonic-gate 	if (element)
64797c478bd9Sstevel@tonic-gate 		kmem_free(element, ss->size);
64807c478bd9Sstevel@tonic-gate }
64817c478bd9Sstevel@tonic-gate 
64827c478bd9Sstevel@tonic-gate /*
64837c478bd9Sstevel@tonic-gate  * Free the entire set of pointers, and any
64847c478bd9Sstevel@tonic-gate  * soft state structures contained therein.
64857c478bd9Sstevel@tonic-gate  *
64867c478bd9Sstevel@tonic-gate  * Note that we don't grab the ss->lock mutex, even though
64877c478bd9Sstevel@tonic-gate  * we're inspecting the various fields of the data structure.
64887c478bd9Sstevel@tonic-gate  *
64897c478bd9Sstevel@tonic-gate  * There is an implicit assumption that this routine will
64907c478bd9Sstevel@tonic-gate  * never run concurrently with any of the above on this
64917c478bd9Sstevel@tonic-gate  * particular state structure i.e. by the time the driver
64927c478bd9Sstevel@tonic-gate  * calls this routine, there should be no other threads
64937c478bd9Sstevel@tonic-gate  * running in the driver.
64947c478bd9Sstevel@tonic-gate  */
64957c478bd9Sstevel@tonic-gate void
64967c478bd9Sstevel@tonic-gate ddi_soft_state_fini(void **state_p)
64977c478bd9Sstevel@tonic-gate {
6498*4c06356bSdh 	i_ddi_soft_state	*ss, *dirty;
6499*4c06356bSdh 	int			item;
6500*4c06356bSdh 	static char		msg[] = "ddi_soft_state_fini:";
65017c478bd9Sstevel@tonic-gate 
6502*4c06356bSdh 	if (state_p == NULL ||
6503*4c06356bSdh 	    (ss = (i_ddi_soft_state *)(*state_p)) == NULL) {
65047c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s null handle: %s",
65057c478bd9Sstevel@tonic-gate 		    msg, mod_containing_pc(caller()));
65067c478bd9Sstevel@tonic-gate 		return;
65077c478bd9Sstevel@tonic-gate 	}
65087c478bd9Sstevel@tonic-gate 
65097c478bd9Sstevel@tonic-gate 	if (ss->size == 0) {
65107c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s bad handle: %s",
65117c478bd9Sstevel@tonic-gate 		    msg, mod_containing_pc(caller()));
65127c478bd9Sstevel@tonic-gate 		return;
65137c478bd9Sstevel@tonic-gate 	}
65147c478bd9Sstevel@tonic-gate 
65157c478bd9Sstevel@tonic-gate 	if (ss->n_items > 0) {
65167c478bd9Sstevel@tonic-gate 		for (item = 0; item < ss->n_items; item++)
65177c478bd9Sstevel@tonic-gate 			ddi_soft_state_free(ss, item);
65187c478bd9Sstevel@tonic-gate 		kmem_free(ss->array, ss->n_items * sizeof (void *));
65197c478bd9Sstevel@tonic-gate 	}
65207c478bd9Sstevel@tonic-gate 
65217c478bd9Sstevel@tonic-gate 	/*
65227c478bd9Sstevel@tonic-gate 	 * Now delete any dirty arrays from previous 'grow' operations
65237c478bd9Sstevel@tonic-gate 	 */
65247c478bd9Sstevel@tonic-gate 	for (dirty = ss->next; dirty; dirty = ss->next) {
65257c478bd9Sstevel@tonic-gate 		ss->next = dirty->next;
65267c478bd9Sstevel@tonic-gate 		kmem_free(dirty->array, dirty->n_items * sizeof (void *));
65277c478bd9Sstevel@tonic-gate 		kmem_free(dirty, sizeof (*dirty));
65287c478bd9Sstevel@tonic-gate 	}
65297c478bd9Sstevel@tonic-gate 
65307c478bd9Sstevel@tonic-gate 	mutex_destroy(&ss->lock);
65317c478bd9Sstevel@tonic-gate 	kmem_free(ss, sizeof (*ss));
65327c478bd9Sstevel@tonic-gate 
65337c478bd9Sstevel@tonic-gate 	*state_p = NULL;
65347c478bd9Sstevel@tonic-gate }
65357c478bd9Sstevel@tonic-gate 
6536*4c06356bSdh #define	SS_N_ITEMS_PER_HASH	16
6537*4c06356bSdh #define	SS_MIN_HASH_SZ		16
6538*4c06356bSdh #define	SS_MAX_HASH_SZ		4096
6539*4c06356bSdh 
6540*4c06356bSdh int
6541*4c06356bSdh ddi_soft_state_bystr_init(ddi_soft_state_bystr **state_p, size_t size,
6542*4c06356bSdh     int n_items)
6543*4c06356bSdh {
6544*4c06356bSdh 	i_ddi_soft_state_bystr	*sss;
6545*4c06356bSdh 	int			hash_sz;
6546*4c06356bSdh 
6547*4c06356bSdh 	ASSERT(state_p && size && n_items);
6548*4c06356bSdh 	if ((state_p == NULL) || (size == 0) || (n_items == 0))
6549*4c06356bSdh 		return (EINVAL);
6550*4c06356bSdh 
6551*4c06356bSdh 	/* current implementation is based on hash, convert n_items to hash */
6552*4c06356bSdh 	hash_sz = n_items / SS_N_ITEMS_PER_HASH;
6553*4c06356bSdh 	if (hash_sz < SS_MIN_HASH_SZ)
6554*4c06356bSdh 		hash_sz = SS_MIN_HASH_SZ;
6555*4c06356bSdh 	else if (hash_sz > SS_MAX_HASH_SZ)
6556*4c06356bSdh 		hash_sz = SS_MAX_HASH_SZ;
6557*4c06356bSdh 
6558*4c06356bSdh 	/* allocate soft_state pool */
6559*4c06356bSdh 	sss = kmem_zalloc(sizeof (*sss), KM_SLEEP);
6560*4c06356bSdh 	sss->ss_size = size;
6561*4c06356bSdh 	sss->ss_mod_hash = mod_hash_create_strhash("soft_state_bystr",
6562*4c06356bSdh 	    hash_sz, mod_hash_null_valdtor);
6563*4c06356bSdh 	*state_p = (ddi_soft_state_bystr *)sss;
6564*4c06356bSdh 	return (0);
6565*4c06356bSdh }
6566*4c06356bSdh 
6567*4c06356bSdh int
6568*4c06356bSdh ddi_soft_state_bystr_zalloc(ddi_soft_state_bystr *state, const char *str)
6569*4c06356bSdh {
6570*4c06356bSdh 	i_ddi_soft_state_bystr	*sss = (i_ddi_soft_state_bystr *)state;
6571*4c06356bSdh 	void			*sso;
6572*4c06356bSdh 	char			*dup_str;
6573*4c06356bSdh 
6574*4c06356bSdh 	ASSERT(sss && str && sss->ss_mod_hash);
6575*4c06356bSdh 	if ((sss == NULL) || (str == NULL) || (sss->ss_mod_hash == NULL))
6576*4c06356bSdh 		return (DDI_FAILURE);
6577*4c06356bSdh 	sso = kmem_zalloc(sss->ss_size, KM_SLEEP);
6578*4c06356bSdh 	dup_str = i_ddi_strdup((char *)str, KM_SLEEP);
6579*4c06356bSdh 	if (mod_hash_insert(sss->ss_mod_hash,
6580*4c06356bSdh 	    (mod_hash_key_t)dup_str, (mod_hash_val_t)sso) == 0)
6581*4c06356bSdh 		return (DDI_SUCCESS);
6582*4c06356bSdh 
6583*4c06356bSdh 	/*
6584*4c06356bSdh 	 * The only error from an strhash insert is caused by a duplicate key.
6585*4c06356bSdh 	 * We refuse to tread on an existing elements, so free and fail.
6586*4c06356bSdh 	 */
6587*4c06356bSdh 	kmem_free(dup_str, strlen(dup_str) + 1);
6588*4c06356bSdh 	kmem_free(sso, sss->ss_size);
6589*4c06356bSdh 	return (DDI_FAILURE);
6590*4c06356bSdh }
6591*4c06356bSdh 
6592*4c06356bSdh void *
6593*4c06356bSdh ddi_soft_state_bystr_get(ddi_soft_state_bystr *state, const char *str)
6594*4c06356bSdh {
6595*4c06356bSdh 	i_ddi_soft_state_bystr	*sss = (i_ddi_soft_state_bystr *)state;
6596*4c06356bSdh 	void			*sso;
6597*4c06356bSdh 
6598*4c06356bSdh 	ASSERT(sss && str && sss->ss_mod_hash);
6599*4c06356bSdh 	if ((sss == NULL) || (str == NULL) || (sss->ss_mod_hash == NULL))
6600*4c06356bSdh 		return (NULL);
6601*4c06356bSdh 
6602*4c06356bSdh 	if (mod_hash_find(sss->ss_mod_hash,
6603*4c06356bSdh 	    (mod_hash_key_t)str, (mod_hash_val_t *)&sso) == 0)
6604*4c06356bSdh 		return (sso);
6605*4c06356bSdh 	return (NULL);
6606*4c06356bSdh }
6607*4c06356bSdh 
6608*4c06356bSdh void
6609*4c06356bSdh ddi_soft_state_bystr_free(ddi_soft_state_bystr *state, const char *str)
6610*4c06356bSdh {
6611*4c06356bSdh 	i_ddi_soft_state_bystr	*sss = (i_ddi_soft_state_bystr *)state;
6612*4c06356bSdh 	void			*sso;
6613*4c06356bSdh 
6614*4c06356bSdh 	ASSERT(sss && str && sss->ss_mod_hash);
6615*4c06356bSdh 	if ((sss == NULL) || (str == NULL) || (sss->ss_mod_hash == NULL))
6616*4c06356bSdh 		return;
6617*4c06356bSdh 
6618*4c06356bSdh 	(void) mod_hash_remove(sss->ss_mod_hash,
6619*4c06356bSdh 	    (mod_hash_key_t)str, (mod_hash_val_t *)&sso);
6620*4c06356bSdh 	kmem_free(sso, sss->ss_size);
6621*4c06356bSdh }
6622*4c06356bSdh 
6623*4c06356bSdh void
6624*4c06356bSdh ddi_soft_state_bystr_fini(ddi_soft_state_bystr **state_p)
6625*4c06356bSdh {
6626*4c06356bSdh 	i_ddi_soft_state_bystr	*sss;
6627*4c06356bSdh 
6628*4c06356bSdh 	ASSERT(state_p);
6629*4c06356bSdh 	if (state_p == NULL)
6630*4c06356bSdh 		return;
6631*4c06356bSdh 
6632*4c06356bSdh 	sss = (i_ddi_soft_state_bystr *)(*state_p);
6633*4c06356bSdh 	if (sss == NULL)
6634*4c06356bSdh 		return;
6635*4c06356bSdh 
6636*4c06356bSdh 	ASSERT(sss->ss_mod_hash);
6637*4c06356bSdh 	if (sss->ss_mod_hash) {
6638*4c06356bSdh 		mod_hash_destroy_strhash(sss->ss_mod_hash);
6639*4c06356bSdh 		sss->ss_mod_hash = NULL;
6640*4c06356bSdh 	}
6641*4c06356bSdh 
6642*4c06356bSdh 	kmem_free(sss, sizeof (*sss));
6643*4c06356bSdh 	*state_p = NULL;
6644*4c06356bSdh }
6645*4c06356bSdh 
6646*4c06356bSdh /*
6647*4c06356bSdh  * The ddi_strid_* routines provide string-to-index management utilities.
6648*4c06356bSdh  */
6649*4c06356bSdh /* allocate and initialize an strid set */
6650*4c06356bSdh int
6651*4c06356bSdh ddi_strid_init(ddi_strid **strid_p, int n_items)
6652*4c06356bSdh {
6653*4c06356bSdh 	i_ddi_strid	*ss;
6654*4c06356bSdh 	int		hash_sz;
6655*4c06356bSdh 
6656*4c06356bSdh 	if (strid_p == NULL)
6657*4c06356bSdh 		return (DDI_FAILURE);
6658*4c06356bSdh 
6659*4c06356bSdh 	/* current implementation is based on hash, convert n_items to hash */
6660*4c06356bSdh 	hash_sz = n_items / SS_N_ITEMS_PER_HASH;
6661*4c06356bSdh 	if (hash_sz < SS_MIN_HASH_SZ)
6662*4c06356bSdh 		hash_sz = SS_MIN_HASH_SZ;
6663*4c06356bSdh 	else if (hash_sz > SS_MAX_HASH_SZ)
6664*4c06356bSdh 		hash_sz = SS_MAX_HASH_SZ;
6665*4c06356bSdh 
6666*4c06356bSdh 	ss = kmem_alloc(sizeof (*ss), KM_SLEEP);
6667*4c06356bSdh 	ss->strid_space = id_space_create("strid", 1, n_items);
6668*4c06356bSdh 	ss->strid_bystr = mod_hash_create_strhash("strid_bystr", hash_sz,
6669*4c06356bSdh 	    mod_hash_null_valdtor);
6670*4c06356bSdh 	ss->strid_byid = mod_hash_create_idhash("strid_byid", hash_sz,
6671*4c06356bSdh 	    mod_hash_null_valdtor);
6672*4c06356bSdh 	*strid_p = (ddi_strid *)ss;
6673*4c06356bSdh 	return (DDI_SUCCESS);
6674*4c06356bSdh }
6675*4c06356bSdh 
6676*4c06356bSdh #define	ID_FIXED_SIZE	0x1
6677*4c06356bSdh 
6678*4c06356bSdh /* allocate an id mapping within the specified set for str, return id */
6679*4c06356bSdh static id_t
6680*4c06356bSdh i_ddi_strid_alloc(ddi_strid *strid, char *str, int flags)
6681*4c06356bSdh {
6682*4c06356bSdh 	i_ddi_strid	*ss = (i_ddi_strid *)strid;
6683*4c06356bSdh 	id_t		id;
6684*4c06356bSdh 	char		*s;
6685*4c06356bSdh 
6686*4c06356bSdh 	ASSERT(ss && str);
6687*4c06356bSdh 	if ((ss == NULL) || (str == NULL))
6688*4c06356bSdh 		return (0);
6689*4c06356bSdh 
6690*4c06356bSdh 	/*
6691*4c06356bSdh 	 * Allocate an id using VM_FIRSTFIT in order to keep allocated id
6692*4c06356bSdh 	 * range as compressed as possible.  This is important to minimize
6693*4c06356bSdh 	 * the amount of space used when the id is used as a ddi_soft_state
6694*4c06356bSdh 	 * index by the caller.
6695*4c06356bSdh 	 *
6696*4c06356bSdh 	 * If ID_FIXED_SIZE, use the _nosleep variant to fail rather
6697*4c06356bSdh 	 * than sleep in id_allocff()
6698*4c06356bSdh 	 */
6699*4c06356bSdh 	if (flags & ID_FIXED_SIZE) {
6700*4c06356bSdh 		id = id_allocff_nosleep(ss->strid_space);
6701*4c06356bSdh 		if (id == (id_t)-1)
6702*4c06356bSdh 			return (0);
6703*4c06356bSdh 	} else {
6704*4c06356bSdh 		id = id_allocff(ss->strid_space);
6705*4c06356bSdh 	}
6706*4c06356bSdh 
6707*4c06356bSdh 	/*
6708*4c06356bSdh 	 * NOTE: since we create and destroy in unison we can save space by
6709*4c06356bSdh 	 * using bystr key as the byid value.  This means destroy must occur
6710*4c06356bSdh 	 * in (byid, bystr) order.
6711*4c06356bSdh 	 */
6712*4c06356bSdh 	s = i_ddi_strdup(str, KM_SLEEP);
6713*4c06356bSdh 	if (mod_hash_insert(ss->strid_bystr, (mod_hash_key_t)s,
6714*4c06356bSdh 	    (mod_hash_val_t)(intptr_t)id) != 0) {
6715*4c06356bSdh 		ddi_strid_free(strid, id);
6716*4c06356bSdh 		return (0);
6717*4c06356bSdh 	}
6718*4c06356bSdh 	if (mod_hash_insert(ss->strid_byid, (mod_hash_key_t)(intptr_t)id,
6719*4c06356bSdh 	    (mod_hash_val_t)s) != 0) {
6720*4c06356bSdh 		ddi_strid_free(strid, id);
6721*4c06356bSdh 		return (0);
6722*4c06356bSdh 	}
6723*4c06356bSdh 
6724*4c06356bSdh 	/* NOTE: s if freed on mod_hash_destroy by mod_hash_strval_dtor */
6725*4c06356bSdh 	return (id);
6726*4c06356bSdh }
6727*4c06356bSdh 
6728*4c06356bSdh /* allocate an id mapping within the specified set for str, return id */
6729*4c06356bSdh id_t
6730*4c06356bSdh ddi_strid_alloc(ddi_strid *strid, char *str)
6731*4c06356bSdh {
6732*4c06356bSdh 	return (i_ddi_strid_alloc(strid, str, 0));
6733*4c06356bSdh }
6734*4c06356bSdh 
6735*4c06356bSdh /* allocate an id mapping within the specified set for str, return id */
6736*4c06356bSdh id_t
6737*4c06356bSdh ddi_strid_fixed_alloc(ddi_strid *strid, char *str)
6738*4c06356bSdh {
6739*4c06356bSdh 	return (i_ddi_strid_alloc(strid, str, ID_FIXED_SIZE));
6740*4c06356bSdh }
6741*4c06356bSdh 
6742*4c06356bSdh /* return the id within the specified strid given the str */
6743*4c06356bSdh id_t
6744*4c06356bSdh ddi_strid_str2id(ddi_strid *strid, char *str)
6745*4c06356bSdh {
6746*4c06356bSdh 	i_ddi_strid	*ss = (i_ddi_strid *)strid;
6747*4c06356bSdh 	id_t		id = 0;
6748*4c06356bSdh 	mod_hash_val_t	hv;
6749*4c06356bSdh 
6750*4c06356bSdh 	ASSERT(ss && str);
6751*4c06356bSdh 	if (ss && str && (mod_hash_find(ss->strid_bystr,
6752*4c06356bSdh 	    (mod_hash_key_t)str, &hv) == 0))
6753*4c06356bSdh 		id = (int)(intptr_t)hv;
6754*4c06356bSdh 	return (id);
6755*4c06356bSdh }
6756*4c06356bSdh 
6757*4c06356bSdh /* return str within the specified strid given the id */
6758*4c06356bSdh char *
6759*4c06356bSdh ddi_strid_id2str(ddi_strid *strid, id_t id)
6760*4c06356bSdh {
6761*4c06356bSdh 	i_ddi_strid	*ss = (i_ddi_strid *)strid;
6762*4c06356bSdh 	char		*str = NULL;
6763*4c06356bSdh 	mod_hash_val_t	hv;
6764*4c06356bSdh 
6765*4c06356bSdh 	ASSERT(ss && id > 0);
6766*4c06356bSdh 	if (ss && (id > 0) && (mod_hash_find(ss->strid_byid,
6767*4c06356bSdh 	    (mod_hash_key_t)(uintptr_t)id, &hv) == 0))
6768*4c06356bSdh 		str = (char *)hv;
6769*4c06356bSdh 	return (str);
6770*4c06356bSdh }
6771*4c06356bSdh 
6772*4c06356bSdh /* free the id mapping within the specified strid */
6773*4c06356bSdh void
6774*4c06356bSdh ddi_strid_free(ddi_strid *strid, id_t id)
6775*4c06356bSdh {
6776*4c06356bSdh 	i_ddi_strid	*ss = (i_ddi_strid *)strid;
6777*4c06356bSdh 	char		*str;
6778*4c06356bSdh 
6779*4c06356bSdh 	ASSERT(ss && id > 0);
6780*4c06356bSdh 	if ((ss == NULL) || (id <= 0))
6781*4c06356bSdh 		return;
6782*4c06356bSdh 
6783*4c06356bSdh 	/* bystr key is byid value: destroy order must be (byid, bystr) */
6784*4c06356bSdh 	str = ddi_strid_id2str(strid, id);
6785*4c06356bSdh 	(void) mod_hash_destroy(ss->strid_byid, (mod_hash_key_t)(uintptr_t)id);
6786*4c06356bSdh 	id_free(ss->strid_space, id);
6787*4c06356bSdh 
6788*4c06356bSdh 	if (str)
6789*4c06356bSdh 		(void) mod_hash_destroy(ss->strid_bystr, (mod_hash_key_t)str);
6790*4c06356bSdh }
6791*4c06356bSdh 
6792*4c06356bSdh /* destroy the strid set */
6793*4c06356bSdh void
6794*4c06356bSdh ddi_strid_fini(ddi_strid **strid_p)
6795*4c06356bSdh {
6796*4c06356bSdh 	i_ddi_strid	*ss;
6797*4c06356bSdh 
6798*4c06356bSdh 	ASSERT(strid_p);
6799*4c06356bSdh 	if (strid_p == NULL)
6800*4c06356bSdh 		return;
6801*4c06356bSdh 
6802*4c06356bSdh 	ss = (i_ddi_strid *)(*strid_p);
6803*4c06356bSdh 	if (ss == NULL)
6804*4c06356bSdh 		return;
6805*4c06356bSdh 
6806*4c06356bSdh 	/* bystr key is byid value: destroy order must be (byid, bystr) */
6807*4c06356bSdh 	if (ss->strid_byid)
6808*4c06356bSdh 		mod_hash_destroy_hash(ss->strid_byid);
6809*4c06356bSdh 	if (ss->strid_byid)
6810*4c06356bSdh 		mod_hash_destroy_hash(ss->strid_bystr);
6811*4c06356bSdh 	if (ss->strid_space)
6812*4c06356bSdh 		id_space_destroy(ss->strid_space);
6813*4c06356bSdh 	kmem_free(ss, sizeof (*ss));
6814*4c06356bSdh 	*strid_p = NULL;
6815*4c06356bSdh }
6816*4c06356bSdh 
68177c478bd9Sstevel@tonic-gate /*
6818fe9fe9fbScth  * This sets the devi_addr entry in the dev_info structure 'dip' to 'name'.
6819fe9fe9fbScth  * Storage is double buffered to prevent updates during devi_addr use -
6820fe9fe9fbScth  * double buffering is adaquate for reliable ddi_deviname() consumption.
6821fe9fe9fbScth  * The double buffer is not freed until dev_info structure destruction
6822fe9fe9fbScth  * (by i_ddi_free_node).
68237c478bd9Sstevel@tonic-gate  */
68247c478bd9Sstevel@tonic-gate void
68257c478bd9Sstevel@tonic-gate ddi_set_name_addr(dev_info_t *dip, char *name)
68267c478bd9Sstevel@tonic-gate {
6827fe9fe9fbScth 	char	*buf = DEVI(dip)->devi_addr_buf;
6828fe9fe9fbScth 	char	*newaddr;
68297c478bd9Sstevel@tonic-gate 
6830fe9fe9fbScth 	if (buf == NULL) {
6831fe9fe9fbScth 		buf = kmem_zalloc(2 * MAXNAMELEN, KM_SLEEP);
6832fe9fe9fbScth 		DEVI(dip)->devi_addr_buf = buf;
68337c478bd9Sstevel@tonic-gate 	}
6834fe9fe9fbScth 
6835fe9fe9fbScth 	if (name) {
6836fe9fe9fbScth 		ASSERT(strlen(name) < MAXNAMELEN);
6837fe9fe9fbScth 		newaddr = (DEVI(dip)->devi_addr == buf) ?
6838fe9fe9fbScth 		    (buf + MAXNAMELEN) : buf;
6839fe9fe9fbScth 		(void) strlcpy(newaddr, name, MAXNAMELEN);
6840fe9fe9fbScth 	} else
6841fe9fe9fbScth 		newaddr = NULL;
6842fe9fe9fbScth 
6843fe9fe9fbScth 	DEVI(dip)->devi_addr = newaddr;
68447c478bd9Sstevel@tonic-gate }
68457c478bd9Sstevel@tonic-gate 
68467c478bd9Sstevel@tonic-gate char *
68477c478bd9Sstevel@tonic-gate ddi_get_name_addr(dev_info_t *dip)
68487c478bd9Sstevel@tonic-gate {
68497c478bd9Sstevel@tonic-gate 	return (DEVI(dip)->devi_addr);
68507c478bd9Sstevel@tonic-gate }
68517c478bd9Sstevel@tonic-gate 
68527c478bd9Sstevel@tonic-gate void
68537c478bd9Sstevel@tonic-gate ddi_set_parent_data(dev_info_t *dip, void *pd)
68547c478bd9Sstevel@tonic-gate {
68557c478bd9Sstevel@tonic-gate 	DEVI(dip)->devi_parent_data = pd;
68567c478bd9Sstevel@tonic-gate }
68577c478bd9Sstevel@tonic-gate 
68587c478bd9Sstevel@tonic-gate void *
68597c478bd9Sstevel@tonic-gate ddi_get_parent_data(dev_info_t *dip)
68607c478bd9Sstevel@tonic-gate {
68617c478bd9Sstevel@tonic-gate 	return (DEVI(dip)->devi_parent_data);
68627c478bd9Sstevel@tonic-gate }
68637c478bd9Sstevel@tonic-gate 
68647c478bd9Sstevel@tonic-gate /*
68655c066ec2SJerry Gilliam  * ddi_name_to_major: returns the major number of a named module,
68665c066ec2SJerry Gilliam  * derived from the current driver alias binding.
68675c066ec2SJerry Gilliam  *
68685c066ec2SJerry Gilliam  * Caveat: drivers should avoid the use of this function, in particular
68695c066ec2SJerry Gilliam  * together with ddi_get_name/ddi_binding name, as per
68705c066ec2SJerry Gilliam  *	major = ddi_name_to_major(ddi_get_name(devi));
68715c066ec2SJerry Gilliam  * ddi_name_to_major() relies on the state of the device/alias binding,
68725c066ec2SJerry Gilliam  * which can and does change dynamically as aliases are administered
68735c066ec2SJerry Gilliam  * over time.  An attached device instance cannot rely on the major
68745c066ec2SJerry Gilliam  * number returned by ddi_name_to_major() to match its own major number.
68755c066ec2SJerry Gilliam  *
68765c066ec2SJerry Gilliam  * For driver use, ddi_driver_major() reliably returns the major number
68775c066ec2SJerry Gilliam  * for the module to which the device was bound at attach time over
68785c066ec2SJerry Gilliam  * the life of the instance.
68795c066ec2SJerry Gilliam  *	major = ddi_driver_major(dev_info_t *)
68807c478bd9Sstevel@tonic-gate  */
68817c478bd9Sstevel@tonic-gate major_t
68827c478bd9Sstevel@tonic-gate ddi_name_to_major(char *name)
68837c478bd9Sstevel@tonic-gate {
68847c478bd9Sstevel@tonic-gate 	return (mod_name_to_major(name));
68857c478bd9Sstevel@tonic-gate }
68867c478bd9Sstevel@tonic-gate 
68877c478bd9Sstevel@tonic-gate /*
68887c478bd9Sstevel@tonic-gate  * ddi_major_to_name: Returns the module name bound to a major number.
68897c478bd9Sstevel@tonic-gate  */
68907c478bd9Sstevel@tonic-gate char *
68917c478bd9Sstevel@tonic-gate ddi_major_to_name(major_t major)
68927c478bd9Sstevel@tonic-gate {
68937c478bd9Sstevel@tonic-gate 	return (mod_major_to_name(major));
68947c478bd9Sstevel@tonic-gate }
68957c478bd9Sstevel@tonic-gate 
68967c478bd9Sstevel@tonic-gate /*
68977c478bd9Sstevel@tonic-gate  * Return the name of the devinfo node pointed at by 'dip' in the buffer
68987c478bd9Sstevel@tonic-gate  * pointed at by 'name.'  A devinfo node is named as a result of calling
68997c478bd9Sstevel@tonic-gate  * ddi_initchild().
69007c478bd9Sstevel@tonic-gate  *
69017c478bd9Sstevel@tonic-gate  * Note: the driver must be held before calling this function!
69027c478bd9Sstevel@tonic-gate  */
69037c478bd9Sstevel@tonic-gate char *
69047c478bd9Sstevel@tonic-gate ddi_deviname(dev_info_t *dip, char *name)
69057c478bd9Sstevel@tonic-gate {
69067c478bd9Sstevel@tonic-gate 	char *addrname;
69077c478bd9Sstevel@tonic-gate 	char none = '\0';
69087c478bd9Sstevel@tonic-gate 
69097c478bd9Sstevel@tonic-gate 	if (dip == ddi_root_node()) {
69107c478bd9Sstevel@tonic-gate 		*name = '\0';
69117c478bd9Sstevel@tonic-gate 		return (name);
69127c478bd9Sstevel@tonic-gate 	}
69137c478bd9Sstevel@tonic-gate 
6914f4da9be0Scth 	if (i_ddi_node_state(dip) < DS_BOUND) {
69157c478bd9Sstevel@tonic-gate 		addrname = &none;
69167c478bd9Sstevel@tonic-gate 	} else {
6917f4da9be0Scth 		/*
6918f4da9be0Scth 		 * Use ddi_get_name_addr() without checking state so we get
6919f4da9be0Scth 		 * a unit-address if we are called after ddi_set_name_addr()
6920f4da9be0Scth 		 * by nexus DDI_CTL_INITCHILD code, but before completing
6921f4da9be0Scth 		 * node promotion to DS_INITIALIZED.  We currently have
6922f4da9be0Scth 		 * two situations where we are called in this state:
6923f4da9be0Scth 		 *   o  For framework processing of a path-oriented alias.
6924f4da9be0Scth 		 *   o  If a SCSA nexus driver calls ddi_devid_register()
6925f4da9be0Scth 		 *	from it's tran_tgt_init(9E) implementation.
6926f4da9be0Scth 		 */
69277c478bd9Sstevel@tonic-gate 		addrname = ddi_get_name_addr(dip);
6928f4da9be0Scth 		if (addrname == NULL)
6929f4da9be0Scth 			addrname = &none;
69307c478bd9Sstevel@tonic-gate 	}
69317c478bd9Sstevel@tonic-gate 
69327c478bd9Sstevel@tonic-gate 	if (*addrname == '\0') {
69337c478bd9Sstevel@tonic-gate 		(void) sprintf(name, "/%s", ddi_node_name(dip));
69347c478bd9Sstevel@tonic-gate 	} else {
69357c478bd9Sstevel@tonic-gate 		(void) sprintf(name, "/%s@%s", ddi_node_name(dip), addrname);
69367c478bd9Sstevel@tonic-gate 	}
69377c478bd9Sstevel@tonic-gate 
69387c478bd9Sstevel@tonic-gate 	return (name);
69397c478bd9Sstevel@tonic-gate }
69407c478bd9Sstevel@tonic-gate 
69417c478bd9Sstevel@tonic-gate /*
69427c478bd9Sstevel@tonic-gate  * Spits out the name of device node, typically name@addr, for a given node,
69437c478bd9Sstevel@tonic-gate  * using the driver name, not the nodename.
69447c478bd9Sstevel@tonic-gate  *
69457c478bd9Sstevel@tonic-gate  * Used by match_parent. Not to be used elsewhere.
69467c478bd9Sstevel@tonic-gate  */
69477c478bd9Sstevel@tonic-gate char *
69487c478bd9Sstevel@tonic-gate i_ddi_parname(dev_info_t *dip, char *name)
69497c478bd9Sstevel@tonic-gate {
69507c478bd9Sstevel@tonic-gate 	char *addrname;
69517c478bd9Sstevel@tonic-gate 
69527c478bd9Sstevel@tonic-gate 	if (dip == ddi_root_node()) {
69537c478bd9Sstevel@tonic-gate 		*name = '\0';
69547c478bd9Sstevel@tonic-gate 		return (name);
69557c478bd9Sstevel@tonic-gate 	}
69567c478bd9Sstevel@tonic-gate 
69577c478bd9Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(dip) >= DS_INITIALIZED);
69587c478bd9Sstevel@tonic-gate 
69597c478bd9Sstevel@tonic-gate 	if (*(addrname = ddi_get_name_addr(dip)) == '\0')
69607c478bd9Sstevel@tonic-gate 		(void) sprintf(name, "%s", ddi_binding_name(dip));
69617c478bd9Sstevel@tonic-gate 	else
69627c478bd9Sstevel@tonic-gate 		(void) sprintf(name, "%s@%s", ddi_binding_name(dip), addrname);
69637c478bd9Sstevel@tonic-gate 	return (name);
69647c478bd9Sstevel@tonic-gate }
69657c478bd9Sstevel@tonic-gate 
69667c478bd9Sstevel@tonic-gate static char *
69677c478bd9Sstevel@tonic-gate pathname_work(dev_info_t *dip, char *path)
69687c478bd9Sstevel@tonic-gate {
69697c478bd9Sstevel@tonic-gate 	char *bp;
69707c478bd9Sstevel@tonic-gate 
69717c478bd9Sstevel@tonic-gate 	if (dip == ddi_root_node()) {
69727c478bd9Sstevel@tonic-gate 		*path = '\0';
69737c478bd9Sstevel@tonic-gate 		return (path);
69747c478bd9Sstevel@tonic-gate 	}
69757c478bd9Sstevel@tonic-gate 	(void) pathname_work(ddi_get_parent(dip), path);
69767c478bd9Sstevel@tonic-gate 	bp = path + strlen(path);
69777c478bd9Sstevel@tonic-gate 	(void) ddi_deviname(dip, bp);
69787c478bd9Sstevel@tonic-gate 	return (path);
69797c478bd9Sstevel@tonic-gate }
69807c478bd9Sstevel@tonic-gate 
69817c478bd9Sstevel@tonic-gate char *
69827c478bd9Sstevel@tonic-gate ddi_pathname(dev_info_t *dip, char *path)
69837c478bd9Sstevel@tonic-gate {
69847c478bd9Sstevel@tonic-gate 	return (pathname_work(dip, path));
69857c478bd9Sstevel@tonic-gate }
69867c478bd9Sstevel@tonic-gate 
6987f9722deaSChris Horne char *
6988f9722deaSChris Horne ddi_pathname_minor(struct ddi_minor_data *dmdp, char *path)
6989f9722deaSChris Horne {
6990f9722deaSChris Horne 	if (dmdp->dip == NULL)
6991f9722deaSChris Horne 		*path = '\0';
6992f9722deaSChris Horne 	else {
6993f9722deaSChris Horne 		(void) ddi_pathname(dmdp->dip, path);
6994f9722deaSChris Horne 		if (dmdp->ddm_name) {
6995f9722deaSChris Horne 			(void) strcat(path, ":");
6996f9722deaSChris Horne 			(void) strcat(path, dmdp->ddm_name);
6997f9722deaSChris Horne 		}
6998f9722deaSChris Horne 	}
6999f9722deaSChris Horne 	return (path);
7000f9722deaSChris Horne }
7001f9722deaSChris Horne 
700238c67cbdSjiang wu - Sun Microsystems - Beijing China static char *
700338c67cbdSjiang wu - Sun Microsystems - Beijing China pathname_work_obp(dev_info_t *dip, char *path)
700438c67cbdSjiang wu - Sun Microsystems - Beijing China {
700538c67cbdSjiang wu - Sun Microsystems - Beijing China 	char *bp;
700638c67cbdSjiang wu - Sun Microsystems - Beijing China 	char *obp_path;
700738c67cbdSjiang wu - Sun Microsystems - Beijing China 
700838c67cbdSjiang wu - Sun Microsystems - Beijing China 	/*
700938c67cbdSjiang wu - Sun Microsystems - Beijing China 	 * look up the "obp-path" property, return the path if it exists
701038c67cbdSjiang wu - Sun Microsystems - Beijing China 	 */
701138c67cbdSjiang wu - Sun Microsystems - Beijing China 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
701238c67cbdSjiang wu - Sun Microsystems - Beijing China 	    "obp-path", &obp_path) == DDI_PROP_SUCCESS) {
701338c67cbdSjiang wu - Sun Microsystems - Beijing China 		(void) strcpy(path, obp_path);
701438c67cbdSjiang wu - Sun Microsystems - Beijing China 		ddi_prop_free(obp_path);
701538c67cbdSjiang wu - Sun Microsystems - Beijing China 		return (path);
701638c67cbdSjiang wu - Sun Microsystems - Beijing China 	}
701738c67cbdSjiang wu - Sun Microsystems - Beijing China 
701838c67cbdSjiang wu - Sun Microsystems - Beijing China 	/*
701938c67cbdSjiang wu - Sun Microsystems - Beijing China 	 * stop at root, no obp path
702038c67cbdSjiang wu - Sun Microsystems - Beijing China 	 */
702138c67cbdSjiang wu - Sun Microsystems - Beijing China 	if (dip == ddi_root_node()) {
702238c67cbdSjiang wu - Sun Microsystems - Beijing China 		return (NULL);
702338c67cbdSjiang wu - Sun Microsystems - Beijing China 	}
702438c67cbdSjiang wu - Sun Microsystems - Beijing China 
702538c67cbdSjiang wu - Sun Microsystems - Beijing China 	obp_path = pathname_work_obp(ddi_get_parent(dip), path);
702638c67cbdSjiang wu - Sun Microsystems - Beijing China 	if (obp_path == NULL)
702738c67cbdSjiang wu - Sun Microsystems - Beijing China 		return (NULL);
702838c67cbdSjiang wu - Sun Microsystems - Beijing China 
702938c67cbdSjiang wu - Sun Microsystems - Beijing China 	/*
703038c67cbdSjiang wu - Sun Microsystems - Beijing China 	 * append our component to parent's obp path
703138c67cbdSjiang wu - Sun Microsystems - Beijing China 	 */
703238c67cbdSjiang wu - Sun Microsystems - Beijing China 	bp = path + strlen(path);
703338c67cbdSjiang wu - Sun Microsystems - Beijing China 	if (*(bp - 1) != '/')
703438c67cbdSjiang wu - Sun Microsystems - Beijing China 		(void) strcat(bp++, "/");
703538c67cbdSjiang wu - Sun Microsystems - Beijing China 	(void) ddi_deviname(dip, bp);
703638c67cbdSjiang wu - Sun Microsystems - Beijing China 	return (path);
703738c67cbdSjiang wu - Sun Microsystems - Beijing China }
703838c67cbdSjiang wu - Sun Microsystems - Beijing China 
703938c67cbdSjiang wu - Sun Microsystems - Beijing China /*
704038c67cbdSjiang wu - Sun Microsystems - Beijing China  * return the 'obp-path' based path for the given node, or NULL if the node
704138c67cbdSjiang wu - Sun Microsystems - Beijing China  * does not have a different obp path. NOTE: Unlike ddi_pathname, this
704238c67cbdSjiang wu - Sun Microsystems - Beijing China  * function can't be called from interrupt context (since we need to
704338c67cbdSjiang wu - Sun Microsystems - Beijing China  * lookup a string property).
704438c67cbdSjiang wu - Sun Microsystems - Beijing China  */
704538c67cbdSjiang wu - Sun Microsystems - Beijing China char *
704638c67cbdSjiang wu - Sun Microsystems - Beijing China ddi_pathname_obp(dev_info_t *dip, char *path)
704738c67cbdSjiang wu - Sun Microsystems - Beijing China {
704838c67cbdSjiang wu - Sun Microsystems - Beijing China 	ASSERT(!servicing_interrupt());
704938c67cbdSjiang wu - Sun Microsystems - Beijing China 	if (dip == NULL || path == NULL)
705038c67cbdSjiang wu - Sun Microsystems - Beijing China 		return (NULL);
705138c67cbdSjiang wu - Sun Microsystems - Beijing China 
705238c67cbdSjiang wu - Sun Microsystems - Beijing China 	/* split work into a separate function to aid debugging */
705338c67cbdSjiang wu - Sun Microsystems - Beijing China 	return (pathname_work_obp(dip, path));
705438c67cbdSjiang wu - Sun Microsystems - Beijing China }
705538c67cbdSjiang wu - Sun Microsystems - Beijing China 
705638c67cbdSjiang wu - Sun Microsystems - Beijing China int
705738c67cbdSjiang wu - Sun Microsystems - Beijing China ddi_pathname_obp_set(dev_info_t *dip, char *component)
705838c67cbdSjiang wu - Sun Microsystems - Beijing China {
705938c67cbdSjiang wu - Sun Microsystems - Beijing China 	dev_info_t *pdip;
7060caa9369fSjiang wu - Sun Microsystems - Beijing China 	char *obp_path = NULL;
7061caa9369fSjiang wu - Sun Microsystems - Beijing China 	int rc = DDI_FAILURE;
706238c67cbdSjiang wu - Sun Microsystems - Beijing China 
706338c67cbdSjiang wu - Sun Microsystems - Beijing China 	if (dip == NULL)
706438c67cbdSjiang wu - Sun Microsystems - Beijing China 		return (DDI_FAILURE);
7065caa9369fSjiang wu - Sun Microsystems - Beijing China 
7066caa9369fSjiang wu - Sun Microsystems - Beijing China 	obp_path = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
7067caa9369fSjiang wu - Sun Microsystems - Beijing China 
706838c67cbdSjiang wu - Sun Microsystems - Beijing China 	pdip = ddi_get_parent(dip);
706938c67cbdSjiang wu - Sun Microsystems - Beijing China 
707038c67cbdSjiang wu - Sun Microsystems - Beijing China 	if (ddi_pathname_obp(pdip, obp_path) == NULL) {
707138c67cbdSjiang wu - Sun Microsystems - Beijing China 		(void) ddi_pathname(pdip, obp_path);
707238c67cbdSjiang wu - Sun Microsystems - Beijing China 	}
707338c67cbdSjiang wu - Sun Microsystems - Beijing China 
707438c67cbdSjiang wu - Sun Microsystems - Beijing China 	if (component) {
7075caa9369fSjiang wu - Sun Microsystems - Beijing China 		(void) strncat(obp_path, "/", MAXPATHLEN);
7076caa9369fSjiang wu - Sun Microsystems - Beijing China 		(void) strncat(obp_path, component, MAXPATHLEN);
707738c67cbdSjiang wu - Sun Microsystems - Beijing China 	}
7078caa9369fSjiang wu - Sun Microsystems - Beijing China 	rc = ndi_prop_update_string(DDI_DEV_T_NONE, dip, "obp-path",
7079caa9369fSjiang wu - Sun Microsystems - Beijing China 	    obp_path);
7080caa9369fSjiang wu - Sun Microsystems - Beijing China 
7081caa9369fSjiang wu - Sun Microsystems - Beijing China 	if (obp_path)
7082caa9369fSjiang wu - Sun Microsystems - Beijing China 		kmem_free(obp_path, MAXPATHLEN);
7083caa9369fSjiang wu - Sun Microsystems - Beijing China 
7084caa9369fSjiang wu - Sun Microsystems - Beijing China 	return (rc);
708538c67cbdSjiang wu - Sun Microsystems - Beijing China }
708638c67cbdSjiang wu - Sun Microsystems - Beijing China 
70877c478bd9Sstevel@tonic-gate /*
70887c478bd9Sstevel@tonic-gate  * Given a dev_t, return the pathname of the corresponding device in the
70897c478bd9Sstevel@tonic-gate  * buffer pointed at by "path."  The buffer is assumed to be large enough
70907c478bd9Sstevel@tonic-gate  * to hold the pathname of the device (MAXPATHLEN).
70917c478bd9Sstevel@tonic-gate  *
70927c478bd9Sstevel@tonic-gate  * The pathname of a device is the pathname of the devinfo node to which
70937c478bd9Sstevel@tonic-gate  * the device "belongs," concatenated with the character ':' and the name
70947c478bd9Sstevel@tonic-gate  * of the minor node corresponding to the dev_t.  If spec_type is 0 then
70957c478bd9Sstevel@tonic-gate  * just the pathname of the devinfo node is returned without driving attach
70967c478bd9Sstevel@tonic-gate  * of that node.  For a non-zero spec_type, an attach is performed and a
70977c478bd9Sstevel@tonic-gate  * search of the minor list occurs.
70987c478bd9Sstevel@tonic-gate  *
70997c478bd9Sstevel@tonic-gate  * It is possible that the path associated with the dev_t is not
71007c478bd9Sstevel@tonic-gate  * currently available in the devinfo tree.  In order to have a
71017c478bd9Sstevel@tonic-gate  * dev_t, a device must have been discovered before, which means
71027c478bd9Sstevel@tonic-gate  * that the path is always in the instance tree.  The one exception
71037c478bd9Sstevel@tonic-gate  * to this is if the dev_t is associated with a pseudo driver, in
71047c478bd9Sstevel@tonic-gate  * which case the device must exist on the pseudo branch of the
71057c478bd9Sstevel@tonic-gate  * devinfo tree as a result of parsing .conf files.
71067c478bd9Sstevel@tonic-gate  */
71077c478bd9Sstevel@tonic-gate int
71087c478bd9Sstevel@tonic-gate ddi_dev_pathname(dev_t devt, int spec_type, char *path)
71097c478bd9Sstevel@tonic-gate {
7110b9ccdc5aScth 	int		circ;
71117c478bd9Sstevel@tonic-gate 	major_t		major = getmajor(devt);
71127c478bd9Sstevel@tonic-gate 	int		instance;
71137c478bd9Sstevel@tonic-gate 	dev_info_t	*dip;
71147c478bd9Sstevel@tonic-gate 	char		*minorname;
71157c478bd9Sstevel@tonic-gate 	char		*drvname;
71167c478bd9Sstevel@tonic-gate 
71177c478bd9Sstevel@tonic-gate 	if (major >= devcnt)
71187c478bd9Sstevel@tonic-gate 		goto fail;
71197c478bd9Sstevel@tonic-gate 	if (major == clone_major) {
71207c478bd9Sstevel@tonic-gate 		/* clone has no minor nodes, manufacture the path here */
71217c478bd9Sstevel@tonic-gate 		if ((drvname = ddi_major_to_name(getminor(devt))) == NULL)
71227c478bd9Sstevel@tonic-gate 			goto fail;
71237c478bd9Sstevel@tonic-gate 
71247c478bd9Sstevel@tonic-gate 		(void) snprintf(path, MAXPATHLEN, "%s:%s", CLONE_PATH, drvname);
71257c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
71267c478bd9Sstevel@tonic-gate 	}
71277c478bd9Sstevel@tonic-gate 
71287c478bd9Sstevel@tonic-gate 	/* extract instance from devt (getinfo(9E) DDI_INFO_DEVT2INSTANCE). */
71297c478bd9Sstevel@tonic-gate 	if ((instance = dev_to_instance(devt)) == -1)
71307c478bd9Sstevel@tonic-gate 		goto fail;
71317c478bd9Sstevel@tonic-gate 
71327c478bd9Sstevel@tonic-gate 	/* reconstruct the path given the major/instance */
71337c478bd9Sstevel@tonic-gate 	if (e_ddi_majorinstance_to_path(major, instance, path) != DDI_SUCCESS)
71347c478bd9Sstevel@tonic-gate 		goto fail;
71357c478bd9Sstevel@tonic-gate 
71367c478bd9Sstevel@tonic-gate 	/* if spec_type given we must drive attach and search minor nodes */
71377c478bd9Sstevel@tonic-gate 	if ((spec_type == S_IFCHR) || (spec_type == S_IFBLK)) {
71387c478bd9Sstevel@tonic-gate 		/* attach the path so we can search minors */
71397c478bd9Sstevel@tonic-gate 		if ((dip = e_ddi_hold_devi_by_path(path, 0)) == NULL)
71407c478bd9Sstevel@tonic-gate 			goto fail;
71417c478bd9Sstevel@tonic-gate 
71427c478bd9Sstevel@tonic-gate 		/* Add minorname to path. */
7143b9ccdc5aScth 		ndi_devi_enter(dip, &circ);
71447c478bd9Sstevel@tonic-gate 		minorname = i_ddi_devtspectype_to_minorname(dip,
71457c478bd9Sstevel@tonic-gate 		    devt, spec_type);
71467c478bd9Sstevel@tonic-gate 		if (minorname) {
71477c478bd9Sstevel@tonic-gate 			(void) strcat(path, ":");
71487c478bd9Sstevel@tonic-gate 			(void) strcat(path, minorname);
71497c478bd9Sstevel@tonic-gate 		}
7150b9ccdc5aScth 		ndi_devi_exit(dip, circ);
71517c478bd9Sstevel@tonic-gate 		ddi_release_devi(dip);
71527c478bd9Sstevel@tonic-gate 		if (minorname == NULL)
71537c478bd9Sstevel@tonic-gate 			goto fail;
71547c478bd9Sstevel@tonic-gate 	}
71557c478bd9Sstevel@tonic-gate 	ASSERT(strlen(path) < MAXPATHLEN);
71567c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
71577c478bd9Sstevel@tonic-gate 
71587c478bd9Sstevel@tonic-gate fail:	*path = 0;
71597c478bd9Sstevel@tonic-gate 	return (DDI_FAILURE);
71607c478bd9Sstevel@tonic-gate }
71617c478bd9Sstevel@tonic-gate 
71627c478bd9Sstevel@tonic-gate /*
71637c478bd9Sstevel@tonic-gate  * Given a major number and an instance, return the path.
71647c478bd9Sstevel@tonic-gate  * This interface does NOT drive attach.
71657c478bd9Sstevel@tonic-gate  */
71667c478bd9Sstevel@tonic-gate int
71677c478bd9Sstevel@tonic-gate e_ddi_majorinstance_to_path(major_t major, int instance, char *path)
71687c478bd9Sstevel@tonic-gate {
716905a69601Scth 	struct devnames *dnp;
71707c478bd9Sstevel@tonic-gate 	dev_info_t	*dip;
71717c478bd9Sstevel@tonic-gate 
717205a69601Scth 	if ((major >= devcnt) || (instance == -1)) {
717305a69601Scth 		*path = 0;
717405a69601Scth 		return (DDI_FAILURE);
717505a69601Scth 	}
717605a69601Scth 
71777c478bd9Sstevel@tonic-gate 	/* look for the major/instance in the instance tree */
71787c478bd9Sstevel@tonic-gate 	if (e_ddi_instance_majorinstance_to_path(major, instance,
717905a69601Scth 	    path) == DDI_SUCCESS) {
718005a69601Scth 		ASSERT(strlen(path) < MAXPATHLEN);
718105a69601Scth 		return (DDI_SUCCESS);
718205a69601Scth 	}
718305a69601Scth 
718405a69601Scth 	/*
718505a69601Scth 	 * Not in instance tree, find the instance on the per driver list and
718605a69601Scth 	 * construct path to instance via ddi_pathname(). This is how paths
718705a69601Scth 	 * down the 'pseudo' branch are constructed.
718805a69601Scth 	 */
718905a69601Scth 	dnp = &(devnamesp[major]);
719005a69601Scth 	LOCK_DEV_OPS(&(dnp->dn_lock));
719105a69601Scth 	for (dip = dnp->dn_head; dip;
719205a69601Scth 	    dip = (dev_info_t *)DEVI(dip)->devi_next) {
719305a69601Scth 		/* Skip if instance does not match. */
719405a69601Scth 		if (DEVI(dip)->devi_instance != instance)
719505a69601Scth 			continue;
719605a69601Scth 
719705a69601Scth 		/*
719805a69601Scth 		 * An ndi_hold_devi() does not prevent DS_INITIALIZED->DS_BOUND
719905a69601Scth 		 * node demotion, so it is not an effective way of ensuring
720005a69601Scth 		 * that the ddi_pathname result has a unit-address.  Instead,
720105a69601Scth 		 * we reverify the node state after calling ddi_pathname().
720205a69601Scth 		 */
720305a69601Scth 		if (i_ddi_node_state(dip) >= DS_INITIALIZED) {
720405a69601Scth 			(void) ddi_pathname(dip, path);
720505a69601Scth 			if (i_ddi_node_state(dip) < DS_INITIALIZED)
720605a69601Scth 				continue;
720705a69601Scth 			UNLOCK_DEV_OPS(&(dnp->dn_lock));
720805a69601Scth 			ASSERT(strlen(path) < MAXPATHLEN);
720905a69601Scth 			return (DDI_SUCCESS);
72107c478bd9Sstevel@tonic-gate 		}
72117c478bd9Sstevel@tonic-gate 	}
721205a69601Scth 	UNLOCK_DEV_OPS(&(dnp->dn_lock));
72137c478bd9Sstevel@tonic-gate 
721405a69601Scth 	/* can't reconstruct the path */
721505a69601Scth 	*path = 0;
721605a69601Scth 	return (DDI_FAILURE);
721705a69601Scth }
72187c478bd9Sstevel@tonic-gate 
72197c478bd9Sstevel@tonic-gate #define	GLD_DRIVER_PPA "SUNW,gld_v0_ppa"
72207c478bd9Sstevel@tonic-gate 
72217c478bd9Sstevel@tonic-gate /*
72227c478bd9Sstevel@tonic-gate  * Given the dip for a network interface return the ppa for that interface.
72237c478bd9Sstevel@tonic-gate  *
72247c478bd9Sstevel@tonic-gate  * In all cases except GLD v0 drivers, the ppa == instance.
72257c478bd9Sstevel@tonic-gate  * In the case of GLD v0 drivers, the ppa is equal to the attach order.
72267c478bd9Sstevel@tonic-gate  * So for these drivers when the attach routine calls gld_register(),
72277c478bd9Sstevel@tonic-gate  * the GLD framework creates an integer property called "gld_driver_ppa"
72287c478bd9Sstevel@tonic-gate  * that can be queried here.
72297c478bd9Sstevel@tonic-gate  *
72307c478bd9Sstevel@tonic-gate  * The only time this function is used is when a system is booting over nfs.
72317c478bd9Sstevel@tonic-gate  * In this case the system has to resolve the pathname of the boot device
72327c478bd9Sstevel@tonic-gate  * to it's ppa.
72337c478bd9Sstevel@tonic-gate  */
72347c478bd9Sstevel@tonic-gate int
72357c478bd9Sstevel@tonic-gate i_ddi_devi_get_ppa(dev_info_t *dip)
72367c478bd9Sstevel@tonic-gate {
72377c478bd9Sstevel@tonic-gate 	return (ddi_prop_get_int(DDI_DEV_T_ANY, dip,
7238184cd04cScth 	    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
7239184cd04cScth 	    GLD_DRIVER_PPA, ddi_get_instance(dip)));
72407c478bd9Sstevel@tonic-gate }
72417c478bd9Sstevel@tonic-gate 
72427c478bd9Sstevel@tonic-gate /*
72437c478bd9Sstevel@tonic-gate  * i_ddi_devi_set_ppa() should only be called from gld_register()
72447c478bd9Sstevel@tonic-gate  * and only for GLD v0 drivers
72457c478bd9Sstevel@tonic-gate  */
72467c478bd9Sstevel@tonic-gate void
72477c478bd9Sstevel@tonic-gate i_ddi_devi_set_ppa(dev_info_t *dip, int ppa)
72487c478bd9Sstevel@tonic-gate {
72497c478bd9Sstevel@tonic-gate 	(void) e_ddi_prop_update_int(DDI_DEV_T_NONE, dip, GLD_DRIVER_PPA, ppa);
72507c478bd9Sstevel@tonic-gate }
72517c478bd9Sstevel@tonic-gate 
72527c478bd9Sstevel@tonic-gate 
72537c478bd9Sstevel@tonic-gate /*
72547c478bd9Sstevel@tonic-gate  * Private DDI Console bell functions.
72557c478bd9Sstevel@tonic-gate  */
72567c478bd9Sstevel@tonic-gate void
72577c478bd9Sstevel@tonic-gate ddi_ring_console_bell(clock_t duration)
72587c478bd9Sstevel@tonic-gate {
72597c478bd9Sstevel@tonic-gate 	if (ddi_console_bell_func != NULL)
72607c478bd9Sstevel@tonic-gate 		(*ddi_console_bell_func)(duration);
72617c478bd9Sstevel@tonic-gate }
72627c478bd9Sstevel@tonic-gate 
72637c478bd9Sstevel@tonic-gate void
72647c478bd9Sstevel@tonic-gate ddi_set_console_bell(void (*bellfunc)(clock_t duration))
72657c478bd9Sstevel@tonic-gate {
72667c478bd9Sstevel@tonic-gate 	ddi_console_bell_func = bellfunc;
72677c478bd9Sstevel@tonic-gate }
72687c478bd9Sstevel@tonic-gate 
72697c478bd9Sstevel@tonic-gate int
72707c478bd9Sstevel@tonic-gate ddi_dma_alloc_handle(dev_info_t *dip, ddi_dma_attr_t *attr,
72717c478bd9Sstevel@tonic-gate 	int (*waitfp)(caddr_t), caddr_t arg, ddi_dma_handle_t *handlep)
72727c478bd9Sstevel@tonic-gate {
72737c478bd9Sstevel@tonic-gate 	int (*funcp)() = ddi_dma_allochdl;
72747c478bd9Sstevel@tonic-gate 	ddi_dma_attr_t dma_attr;
72757c478bd9Sstevel@tonic-gate 	struct bus_ops *bop;
72767c478bd9Sstevel@tonic-gate 
72777c478bd9Sstevel@tonic-gate 	if (attr == (ddi_dma_attr_t *)0)
72787c478bd9Sstevel@tonic-gate 		return (DDI_DMA_BADATTR);
72797c478bd9Sstevel@tonic-gate 
72807c478bd9Sstevel@tonic-gate 	dma_attr = *attr;
72817c478bd9Sstevel@tonic-gate 
72827c478bd9Sstevel@tonic-gate 	bop = DEVI(dip)->devi_ops->devo_bus_ops;
72837c478bd9Sstevel@tonic-gate 	if (bop && bop->bus_dma_allochdl)
72847c478bd9Sstevel@tonic-gate 		funcp = bop->bus_dma_allochdl;
72857c478bd9Sstevel@tonic-gate 
72867c478bd9Sstevel@tonic-gate 	return ((*funcp)(dip, dip, &dma_attr, waitfp, arg, handlep));
72877c478bd9Sstevel@tonic-gate }
72887c478bd9Sstevel@tonic-gate 
72897c478bd9Sstevel@tonic-gate void
72907c478bd9Sstevel@tonic-gate ddi_dma_free_handle(ddi_dma_handle_t *handlep)
72917c478bd9Sstevel@tonic-gate {
72927c478bd9Sstevel@tonic-gate 	ddi_dma_handle_t h = *handlep;
72937c478bd9Sstevel@tonic-gate 	(void) ddi_dma_freehdl(HD, HD, h);
72947c478bd9Sstevel@tonic-gate }
72957c478bd9Sstevel@tonic-gate 
72967c478bd9Sstevel@tonic-gate static uintptr_t dma_mem_list_id = 0;
72977c478bd9Sstevel@tonic-gate 
72987c478bd9Sstevel@tonic-gate 
72997c478bd9Sstevel@tonic-gate int
73007c478bd9Sstevel@tonic-gate ddi_dma_mem_alloc(ddi_dma_handle_t handle, size_t length,
73017b93957cSeota 	ddi_device_acc_attr_t *accattrp, uint_t flags,
73027c478bd9Sstevel@tonic-gate 	int (*waitfp)(caddr_t), caddr_t arg, caddr_t *kaddrp,
73037c478bd9Sstevel@tonic-gate 	size_t *real_length, ddi_acc_handle_t *handlep)
73047c478bd9Sstevel@tonic-gate {
73057c478bd9Sstevel@tonic-gate 	ddi_dma_impl_t *hp = (ddi_dma_impl_t *)handle;
73067c478bd9Sstevel@tonic-gate 	dev_info_t *dip = hp->dmai_rdip;
73077c478bd9Sstevel@tonic-gate 	ddi_acc_hdl_t *ap;
73087c478bd9Sstevel@tonic-gate 	ddi_dma_attr_t *attrp = &hp->dmai_attr;
73097b93957cSeota 	uint_t sleepflag, xfermodes;
73107c478bd9Sstevel@tonic-gate 	int (*fp)(caddr_t);
73117c478bd9Sstevel@tonic-gate 	int rval;
73127c478bd9Sstevel@tonic-gate 
73137c478bd9Sstevel@tonic-gate 	if (waitfp == DDI_DMA_SLEEP)
73147c478bd9Sstevel@tonic-gate 		fp = (int (*)())KM_SLEEP;
73157c478bd9Sstevel@tonic-gate 	else if (waitfp == DDI_DMA_DONTWAIT)
73167c478bd9Sstevel@tonic-gate 		fp = (int (*)())KM_NOSLEEP;
73177c478bd9Sstevel@tonic-gate 	else
73187c478bd9Sstevel@tonic-gate 		fp = waitfp;
73197c478bd9Sstevel@tonic-gate 	*handlep = impl_acc_hdl_alloc(fp, arg);
73207c478bd9Sstevel@tonic-gate 	if (*handlep == NULL)
73217c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
73227c478bd9Sstevel@tonic-gate 
732383220025Seota 	/* check if the cache attributes are supported */
732483220025Seota 	if (i_ddi_check_cache_attr(flags) == B_FALSE)
73257b93957cSeota 		return (DDI_FAILURE);
73267b93957cSeota 
73277b93957cSeota 	/*
73287b93957cSeota 	 * Transfer the meaningful bits to xfermodes.
73297b93957cSeota 	 * Double-check if the 3rd party driver correctly sets the bits.
73307b93957cSeota 	 * If not, set DDI_DMA_STREAMING to keep compatibility.
73317b93957cSeota 	 */
73327b93957cSeota 	xfermodes = flags & (DDI_DMA_CONSISTENT | DDI_DMA_STREAMING);
73337b93957cSeota 	if (xfermodes == 0) {
73347b93957cSeota 		xfermodes = DDI_DMA_STREAMING;
73357b93957cSeota 	}
73367b93957cSeota 
73377c478bd9Sstevel@tonic-gate 	/*
73387c478bd9Sstevel@tonic-gate 	 * initialize the common elements of data access handle
73397c478bd9Sstevel@tonic-gate 	 */
73407c478bd9Sstevel@tonic-gate 	ap = impl_acc_hdl_get(*handlep);
73417c478bd9Sstevel@tonic-gate 	ap->ah_vers = VERS_ACCHDL;
73427c478bd9Sstevel@tonic-gate 	ap->ah_dip = dip;
73437c478bd9Sstevel@tonic-gate 	ap->ah_offset = 0;
73447c478bd9Sstevel@tonic-gate 	ap->ah_len = 0;
73457b93957cSeota 	ap->ah_xfermodes = flags;
73467c478bd9Sstevel@tonic-gate 	ap->ah_acc = *accattrp;
73477c478bd9Sstevel@tonic-gate 
73487c478bd9Sstevel@tonic-gate 	sleepflag = ((waitfp == DDI_DMA_SLEEP) ? 1 : 0);
73497c478bd9Sstevel@tonic-gate 	if (xfermodes == DDI_DMA_CONSISTENT) {
73507b93957cSeota 		rval = i_ddi_mem_alloc(dip, attrp, length, sleepflag,
73517b93957cSeota 		    flags, accattrp, kaddrp, NULL, ap);
73527c478bd9Sstevel@tonic-gate 		*real_length = length;
73537c478bd9Sstevel@tonic-gate 	} else {
73547b93957cSeota 		rval = i_ddi_mem_alloc(dip, attrp, length, sleepflag,
73557b93957cSeota 		    flags, accattrp, kaddrp, real_length, ap);
73567c478bd9Sstevel@tonic-gate 	}
73577c478bd9Sstevel@tonic-gate 	if (rval == DDI_SUCCESS) {
73587c478bd9Sstevel@tonic-gate 		ap->ah_len = (off_t)(*real_length);
73597c478bd9Sstevel@tonic-gate 		ap->ah_addr = *kaddrp;
73607c478bd9Sstevel@tonic-gate 	} else {
73617c478bd9Sstevel@tonic-gate 		impl_acc_hdl_free(*handlep);
73627c478bd9Sstevel@tonic-gate 		*handlep = (ddi_acc_handle_t)NULL;
73637c478bd9Sstevel@tonic-gate 		if (waitfp != DDI_DMA_SLEEP && waitfp != DDI_DMA_DONTWAIT) {
73647c478bd9Sstevel@tonic-gate 			ddi_set_callback(waitfp, arg, &dma_mem_list_id);
73657c478bd9Sstevel@tonic-gate 		}
73667c478bd9Sstevel@tonic-gate 		rval = DDI_FAILURE;
73677c478bd9Sstevel@tonic-gate 	}
73687c478bd9Sstevel@tonic-gate 	return (rval);
73697c478bd9Sstevel@tonic-gate }
73707c478bd9Sstevel@tonic-gate 
73717c478bd9Sstevel@tonic-gate void
73727c478bd9Sstevel@tonic-gate ddi_dma_mem_free(ddi_acc_handle_t *handlep)
73737c478bd9Sstevel@tonic-gate {
73747c478bd9Sstevel@tonic-gate 	ddi_acc_hdl_t *ap;
73757c478bd9Sstevel@tonic-gate 
73767c478bd9Sstevel@tonic-gate 	ap = impl_acc_hdl_get(*handlep);
73777c478bd9Sstevel@tonic-gate 	ASSERT(ap);
73787c478bd9Sstevel@tonic-gate 
73797b93957cSeota 	i_ddi_mem_free((caddr_t)ap->ah_addr, ap);
73807c478bd9Sstevel@tonic-gate 
73817c478bd9Sstevel@tonic-gate 	/*
73827c478bd9Sstevel@tonic-gate 	 * free the handle
73837c478bd9Sstevel@tonic-gate 	 */
73847c478bd9Sstevel@tonic-gate 	impl_acc_hdl_free(*handlep);
73857c478bd9Sstevel@tonic-gate 	*handlep = (ddi_acc_handle_t)NULL;
73867c478bd9Sstevel@tonic-gate 
73877c478bd9Sstevel@tonic-gate 	if (dma_mem_list_id != 0) {
73887c478bd9Sstevel@tonic-gate 		ddi_run_callback(&dma_mem_list_id);
73897c478bd9Sstevel@tonic-gate 	}
73907c478bd9Sstevel@tonic-gate }
73917c478bd9Sstevel@tonic-gate 
73927c478bd9Sstevel@tonic-gate int
73937c478bd9Sstevel@tonic-gate ddi_dma_buf_bind_handle(ddi_dma_handle_t handle, struct buf *bp,
73947c478bd9Sstevel@tonic-gate 	uint_t flags, int (*waitfp)(caddr_t), caddr_t arg,
73957c478bd9Sstevel@tonic-gate 	ddi_dma_cookie_t *cookiep, uint_t *ccountp)
73967c478bd9Sstevel@tonic-gate {
73977c478bd9Sstevel@tonic-gate 	ddi_dma_impl_t *hp = (ddi_dma_impl_t *)handle;
7398b51bbbf5SVikram Hegde 	dev_info_t *dip, *rdip;
73997c478bd9Sstevel@tonic-gate 	struct ddi_dma_req dmareq;
74007c478bd9Sstevel@tonic-gate 	int (*funcp)();
74017c478bd9Sstevel@tonic-gate 
74027c478bd9Sstevel@tonic-gate 	dmareq.dmar_flags = flags;
74037c478bd9Sstevel@tonic-gate 	dmareq.dmar_fp = waitfp;
74047c478bd9Sstevel@tonic-gate 	dmareq.dmar_arg = arg;
74057c478bd9Sstevel@tonic-gate 	dmareq.dmar_object.dmao_size = (uint_t)bp->b_bcount;
74067c478bd9Sstevel@tonic-gate 
74072d3bf78eScth 	if (bp->b_flags & B_PAGEIO) {
74087c478bd9Sstevel@tonic-gate 		dmareq.dmar_object.dmao_type = DMA_OTYP_PAGES;
74097c478bd9Sstevel@tonic-gate 		dmareq.dmar_object.dmao_obj.pp_obj.pp_pp = bp->b_pages;
74107c478bd9Sstevel@tonic-gate 		dmareq.dmar_object.dmao_obj.pp_obj.pp_offset =
74117c478bd9Sstevel@tonic-gate 		    (uint_t)(((uintptr_t)bp->b_un.b_addr) & MMU_PAGEOFFSET);
74127c478bd9Sstevel@tonic-gate 	} else {
74137c478bd9Sstevel@tonic-gate 		dmareq.dmar_object.dmao_obj.virt_obj.v_addr = bp->b_un.b_addr;
74142d3bf78eScth 		if (bp->b_flags & B_SHADOW) {
74157c478bd9Sstevel@tonic-gate 			dmareq.dmar_object.dmao_obj.virt_obj.v_priv =
7416184cd04cScth 			    bp->b_shadow;
74177c478bd9Sstevel@tonic-gate 			dmareq.dmar_object.dmao_type = DMA_OTYP_BUFVADDR;
74187c478bd9Sstevel@tonic-gate 		} else {
74197c478bd9Sstevel@tonic-gate 			dmareq.dmar_object.dmao_type =
7420184cd04cScth 			    (bp->b_flags & (B_PHYS | B_REMAPPED)) ?
7421184cd04cScth 			    DMA_OTYP_BUFVADDR : DMA_OTYP_VADDR;
74227c478bd9Sstevel@tonic-gate 			dmareq.dmar_object.dmao_obj.virt_obj.v_priv = NULL;
74237c478bd9Sstevel@tonic-gate 		}
74247c478bd9Sstevel@tonic-gate 
74257c478bd9Sstevel@tonic-gate 		/*
74267c478bd9Sstevel@tonic-gate 		 * If the buffer has no proc pointer, or the proc
74277c478bd9Sstevel@tonic-gate 		 * struct has the kernel address space, or the buffer has
74287c478bd9Sstevel@tonic-gate 		 * been marked B_REMAPPED (meaning that it is now
74297c478bd9Sstevel@tonic-gate 		 * mapped into the kernel's address space), then
74307c478bd9Sstevel@tonic-gate 		 * the address space is kas (kernel address space).
74317c478bd9Sstevel@tonic-gate 		 */
74322d3bf78eScth 		if ((bp->b_proc == NULL) || (bp->b_proc->p_as == &kas) ||
74332d3bf78eScth 		    (bp->b_flags & B_REMAPPED)) {
74347c478bd9Sstevel@tonic-gate 			dmareq.dmar_object.dmao_obj.virt_obj.v_as = 0;
74357c478bd9Sstevel@tonic-gate 		} else {
74367c478bd9Sstevel@tonic-gate 			dmareq.dmar_object.dmao_obj.virt_obj.v_as =
74377c478bd9Sstevel@tonic-gate 			    bp->b_proc->p_as;
74387c478bd9Sstevel@tonic-gate 		}
74397c478bd9Sstevel@tonic-gate 	}
74407c478bd9Sstevel@tonic-gate 
7441b51bbbf5SVikram Hegde 	dip = rdip = hp->dmai_rdip;
7442b51bbbf5SVikram Hegde 	if (dip != ddi_root_node())
7443b51bbbf5SVikram Hegde 		dip = (dev_info_t *)DEVI(dip)->devi_bus_dma_bindhdl;
7444b51bbbf5SVikram Hegde 	funcp = DEVI(rdip)->devi_bus_dma_bindfunc;
7445b51bbbf5SVikram Hegde 	return ((*funcp)(dip, rdip, handle, &dmareq, cookiep, ccountp));
74467c478bd9Sstevel@tonic-gate }
74477c478bd9Sstevel@tonic-gate 
74487c478bd9Sstevel@tonic-gate int
74497c478bd9Sstevel@tonic-gate ddi_dma_addr_bind_handle(ddi_dma_handle_t handle, struct as *as,
74507c478bd9Sstevel@tonic-gate 	caddr_t addr, size_t len, uint_t flags, int (*waitfp)(caddr_t),
74517c478bd9Sstevel@tonic-gate 	caddr_t arg, ddi_dma_cookie_t *cookiep, uint_t *ccountp)
74527c478bd9Sstevel@tonic-gate {
74537c478bd9Sstevel@tonic-gate 	ddi_dma_impl_t *hp = (ddi_dma_impl_t *)handle;
7454b51bbbf5SVikram Hegde 	dev_info_t *dip, *rdip;
74557c478bd9Sstevel@tonic-gate 	struct ddi_dma_req dmareq;
74567c478bd9Sstevel@tonic-gate 	int (*funcp)();
74577c478bd9Sstevel@tonic-gate 
74587c478bd9Sstevel@tonic-gate 	if (len == (uint_t)0) {
74597c478bd9Sstevel@tonic-gate 		return (DDI_DMA_NOMAPPING);
74607c478bd9Sstevel@tonic-gate 	}
74617c478bd9Sstevel@tonic-gate 	dmareq.dmar_flags = flags;
74627c478bd9Sstevel@tonic-gate 	dmareq.dmar_fp = waitfp;
74637c478bd9Sstevel@tonic-gate 	dmareq.dmar_arg = arg;
74647c478bd9Sstevel@tonic-gate 	dmareq.dmar_object.dmao_size = len;
74657c478bd9Sstevel@tonic-gate 	dmareq.dmar_object.dmao_type = DMA_OTYP_VADDR;
74667c478bd9Sstevel@tonic-gate 	dmareq.dmar_object.dmao_obj.virt_obj.v_as = as;
74677c478bd9Sstevel@tonic-gate 	dmareq.dmar_object.dmao_obj.virt_obj.v_addr = addr;
74687c478bd9Sstevel@tonic-gate 	dmareq.dmar_object.dmao_obj.virt_obj.v_priv = NULL;
74697c478bd9Sstevel@tonic-gate 
7470b51bbbf5SVikram Hegde 	dip = rdip = hp->dmai_rdip;
7471b51bbbf5SVikram Hegde 	if (dip != ddi_root_node())
7472b51bbbf5SVikram Hegde 		dip = (dev_info_t *)DEVI(dip)->devi_bus_dma_bindhdl;
7473b51bbbf5SVikram Hegde 	funcp = DEVI(rdip)->devi_bus_dma_bindfunc;
7474b51bbbf5SVikram Hegde 	return ((*funcp)(dip, rdip, handle, &dmareq, cookiep, ccountp));
74757c478bd9Sstevel@tonic-gate }
74767c478bd9Sstevel@tonic-gate 
74777c478bd9Sstevel@tonic-gate void
74787c478bd9Sstevel@tonic-gate ddi_dma_nextcookie(ddi_dma_handle_t handle, ddi_dma_cookie_t *cookiep)
74797c478bd9Sstevel@tonic-gate {
74807c478bd9Sstevel@tonic-gate 	ddi_dma_impl_t *hp = (ddi_dma_impl_t *)handle;
74817c478bd9Sstevel@tonic-gate 	ddi_dma_cookie_t *cp;
74827c478bd9Sstevel@tonic-gate 
74837c478bd9Sstevel@tonic-gate 	cp = hp->dmai_cookie;
74847c478bd9Sstevel@tonic-gate 	ASSERT(cp);
74857c478bd9Sstevel@tonic-gate 
74867c478bd9Sstevel@tonic-gate 	cookiep->dmac_notused = cp->dmac_notused;
74877c478bd9Sstevel@tonic-gate 	cookiep->dmac_type = cp->dmac_type;
74887c478bd9Sstevel@tonic-gate 	cookiep->dmac_address = cp->dmac_address;
74897c478bd9Sstevel@tonic-gate 	cookiep->dmac_size = cp->dmac_size;
74907c478bd9Sstevel@tonic-gate 	hp->dmai_cookie++;
74917c478bd9Sstevel@tonic-gate }
74927c478bd9Sstevel@tonic-gate 
74937c478bd9Sstevel@tonic-gate int
74947c478bd9Sstevel@tonic-gate ddi_dma_numwin(ddi_dma_handle_t handle, uint_t *nwinp)
74957c478bd9Sstevel@tonic-gate {
74967c478bd9Sstevel@tonic-gate 	ddi_dma_impl_t *hp = (ddi_dma_impl_t *)handle;
74977c478bd9Sstevel@tonic-gate 	if ((hp->dmai_rflags & DDI_DMA_PARTIAL) == 0) {
74987c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
74997c478bd9Sstevel@tonic-gate 	} else {
75007c478bd9Sstevel@tonic-gate 		*nwinp = hp->dmai_nwin;
75017c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
75027c478bd9Sstevel@tonic-gate 	}
75037c478bd9Sstevel@tonic-gate }
75047c478bd9Sstevel@tonic-gate 
75057c478bd9Sstevel@tonic-gate int
75067c478bd9Sstevel@tonic-gate ddi_dma_getwin(ddi_dma_handle_t h, uint_t win, off_t *offp,
75077c478bd9Sstevel@tonic-gate 	size_t *lenp, ddi_dma_cookie_t *cookiep, uint_t *ccountp)
75087c478bd9Sstevel@tonic-gate {
75097c478bd9Sstevel@tonic-gate 	int (*funcp)() = ddi_dma_win;
75107c478bd9Sstevel@tonic-gate 	struct bus_ops *bop;
75117c478bd9Sstevel@tonic-gate 
75127c478bd9Sstevel@tonic-gate 	bop = DEVI(HD)->devi_ops->devo_bus_ops;
75137c478bd9Sstevel@tonic-gate 	if (bop && bop->bus_dma_win)
75147c478bd9Sstevel@tonic-gate 		funcp = bop->bus_dma_win;
75157c478bd9Sstevel@tonic-gate 
75167c478bd9Sstevel@tonic-gate 	return ((*funcp)(HD, HD, h, win, offp, lenp, cookiep, ccountp));
75177c478bd9Sstevel@tonic-gate }
75187c478bd9Sstevel@tonic-gate 
75197c478bd9Sstevel@tonic-gate int
75207c478bd9Sstevel@tonic-gate ddi_dma_set_sbus64(ddi_dma_handle_t h, ulong_t burstsizes)
75217c478bd9Sstevel@tonic-gate {
75227c478bd9Sstevel@tonic-gate 	return (ddi_dma_mctl(HD, HD, h, DDI_DMA_SET_SBUS64, 0,
7523184cd04cScth 	    &burstsizes, 0, 0));
75247c478bd9Sstevel@tonic-gate }
75257c478bd9Sstevel@tonic-gate 
75267c478bd9Sstevel@tonic-gate int
75277c478bd9Sstevel@tonic-gate i_ddi_dma_fault_check(ddi_dma_impl_t *hp)
75287c478bd9Sstevel@tonic-gate {
75297c478bd9Sstevel@tonic-gate 	return (hp->dmai_fault);
75307c478bd9Sstevel@tonic-gate }
75317c478bd9Sstevel@tonic-gate 
75327c478bd9Sstevel@tonic-gate int
75337c478bd9Sstevel@tonic-gate ddi_check_dma_handle(ddi_dma_handle_t handle)
75347c478bd9Sstevel@tonic-gate {
75357c478bd9Sstevel@tonic-gate 	ddi_dma_impl_t *hp = (ddi_dma_impl_t *)handle;
75367c478bd9Sstevel@tonic-gate 	int (*check)(ddi_dma_impl_t *);
75377c478bd9Sstevel@tonic-gate 
75387c478bd9Sstevel@tonic-gate 	if ((check = hp->dmai_fault_check) == NULL)
75397c478bd9Sstevel@tonic-gate 		check = i_ddi_dma_fault_check;
75407c478bd9Sstevel@tonic-gate 
75417c478bd9Sstevel@tonic-gate 	return (((*check)(hp) == DDI_SUCCESS) ? DDI_SUCCESS : DDI_FAILURE);
75427c478bd9Sstevel@tonic-gate }
75437c478bd9Sstevel@tonic-gate 
75447c478bd9Sstevel@tonic-gate void
75457c478bd9Sstevel@tonic-gate i_ddi_dma_set_fault(ddi_dma_handle_t handle)
75467c478bd9Sstevel@tonic-gate {
75477c478bd9Sstevel@tonic-gate 	ddi_dma_impl_t *hp = (ddi_dma_impl_t *)handle;
75487c478bd9Sstevel@tonic-gate 	void (*notify)(ddi_dma_impl_t *);
75497c478bd9Sstevel@tonic-gate 
75507c478bd9Sstevel@tonic-gate 	if (!hp->dmai_fault) {
75517c478bd9Sstevel@tonic-gate 		hp->dmai_fault = 1;
75527c478bd9Sstevel@tonic-gate 		if ((notify = hp->dmai_fault_notify) != NULL)
75537c478bd9Sstevel@tonic-gate 			(*notify)(hp);
75547c478bd9Sstevel@tonic-gate 	}
75557c478bd9Sstevel@tonic-gate }
75567c478bd9Sstevel@tonic-gate 
75577c478bd9Sstevel@tonic-gate void
75587c478bd9Sstevel@tonic-gate i_ddi_dma_clr_fault(ddi_dma_handle_t handle)
75597c478bd9Sstevel@tonic-gate {
75607c478bd9Sstevel@tonic-gate 	ddi_dma_impl_t *hp = (ddi_dma_impl_t *)handle;
75617c478bd9Sstevel@tonic-gate 	void (*notify)(ddi_dma_impl_t *);
75627c478bd9Sstevel@tonic-gate 
75637c478bd9Sstevel@tonic-gate 	if (hp->dmai_fault) {
75647c478bd9Sstevel@tonic-gate 		hp->dmai_fault = 0;
75657c478bd9Sstevel@tonic-gate 		if ((notify = hp->dmai_fault_notify) != NULL)
75667c478bd9Sstevel@tonic-gate 			(*notify)(hp);
75677c478bd9Sstevel@tonic-gate 	}
75687c478bd9Sstevel@tonic-gate }
75697c478bd9Sstevel@tonic-gate 
75707c478bd9Sstevel@tonic-gate /*
75717c478bd9Sstevel@tonic-gate  * register mapping routines.
75727c478bd9Sstevel@tonic-gate  */
75737c478bd9Sstevel@tonic-gate int
75747c478bd9Sstevel@tonic-gate ddi_regs_map_setup(dev_info_t *dip, uint_t rnumber, caddr_t *addrp,
75757c478bd9Sstevel@tonic-gate 	offset_t offset, offset_t len, ddi_device_acc_attr_t *accattrp,
75767c478bd9Sstevel@tonic-gate 	ddi_acc_handle_t *handle)
75777c478bd9Sstevel@tonic-gate {
75787c478bd9Sstevel@tonic-gate 	ddi_map_req_t mr;
75797c478bd9Sstevel@tonic-gate 	ddi_acc_hdl_t *hp;
75807c478bd9Sstevel@tonic-gate 	int result;
75817c478bd9Sstevel@tonic-gate 
75827c478bd9Sstevel@tonic-gate 	/*
75837c478bd9Sstevel@tonic-gate 	 * Allocate and initialize the common elements of data access handle.
75847c478bd9Sstevel@tonic-gate 	 */
75857c478bd9Sstevel@tonic-gate 	*handle = impl_acc_hdl_alloc(KM_SLEEP, NULL);
75867c478bd9Sstevel@tonic-gate 	hp = impl_acc_hdl_get(*handle);
75877c478bd9Sstevel@tonic-gate 	hp->ah_vers = VERS_ACCHDL;
75887c478bd9Sstevel@tonic-gate 	hp->ah_dip = dip;
75897c478bd9Sstevel@tonic-gate 	hp->ah_rnumber = rnumber;
75907c478bd9Sstevel@tonic-gate 	hp->ah_offset = offset;
75917c478bd9Sstevel@tonic-gate 	hp->ah_len = len;
75927c478bd9Sstevel@tonic-gate 	hp->ah_acc = *accattrp;
75937c478bd9Sstevel@tonic-gate 
75947c478bd9Sstevel@tonic-gate 	/*
75957c478bd9Sstevel@tonic-gate 	 * Set up the mapping request and call to parent.
75967c478bd9Sstevel@tonic-gate 	 */
75977c478bd9Sstevel@tonic-gate 	mr.map_op = DDI_MO_MAP_LOCKED;
75987c478bd9Sstevel@tonic-gate 	mr.map_type = DDI_MT_RNUMBER;
75997c478bd9Sstevel@tonic-gate 	mr.map_obj.rnumber = rnumber;
76007c478bd9Sstevel@tonic-gate 	mr.map_prot = PROT_READ | PROT_WRITE;
76017c478bd9Sstevel@tonic-gate 	mr.map_flags = DDI_MF_KERNEL_MAPPING;
76027c478bd9Sstevel@tonic-gate 	mr.map_handlep = hp;
76037c478bd9Sstevel@tonic-gate 	mr.map_vers = DDI_MAP_VERSION;
76047c478bd9Sstevel@tonic-gate 	result = ddi_map(dip, &mr, offset, len, addrp);
76057c478bd9Sstevel@tonic-gate 
76067c478bd9Sstevel@tonic-gate 	/*
76077c478bd9Sstevel@tonic-gate 	 * check for end result
76087c478bd9Sstevel@tonic-gate 	 */
76097c478bd9Sstevel@tonic-gate 	if (result != DDI_SUCCESS) {
76107c478bd9Sstevel@tonic-gate 		impl_acc_hdl_free(*handle);
76117c478bd9Sstevel@tonic-gate 		*handle = (ddi_acc_handle_t)NULL;
76127c478bd9Sstevel@tonic-gate 	} else {
76137c478bd9Sstevel@tonic-gate 		hp->ah_addr = *addrp;
76147c478bd9Sstevel@tonic-gate 	}
76157c478bd9Sstevel@tonic-gate 
76167c478bd9Sstevel@tonic-gate 	return (result);
76177c478bd9Sstevel@tonic-gate }
76187c478bd9Sstevel@tonic-gate 
76197c478bd9Sstevel@tonic-gate void
76207c478bd9Sstevel@tonic-gate ddi_regs_map_free(ddi_acc_handle_t *handlep)
76217c478bd9Sstevel@tonic-gate {
76227c478bd9Sstevel@tonic-gate 	ddi_map_req_t mr;
76237c478bd9Sstevel@tonic-gate 	ddi_acc_hdl_t *hp;
76247c478bd9Sstevel@tonic-gate 
76257c478bd9Sstevel@tonic-gate 	hp = impl_acc_hdl_get(*handlep);
76267c478bd9Sstevel@tonic-gate 	ASSERT(hp);
76277c478bd9Sstevel@tonic-gate 
76287c478bd9Sstevel@tonic-gate 	mr.map_op = DDI_MO_UNMAP;
76297c478bd9Sstevel@tonic-gate 	mr.map_type = DDI_MT_RNUMBER;
76307c478bd9Sstevel@tonic-gate 	mr.map_obj.rnumber = hp->ah_rnumber;
76317c478bd9Sstevel@tonic-gate 	mr.map_prot = PROT_READ | PROT_WRITE;
76327c478bd9Sstevel@tonic-gate 	mr.map_flags = DDI_MF_KERNEL_MAPPING;
76337c478bd9Sstevel@tonic-gate 	mr.map_handlep = hp;
76347c478bd9Sstevel@tonic-gate 	mr.map_vers = DDI_MAP_VERSION;
76357c478bd9Sstevel@tonic-gate 
76367c478bd9Sstevel@tonic-gate 	/*
76377c478bd9Sstevel@tonic-gate 	 * Call my parent to unmap my regs.
76387c478bd9Sstevel@tonic-gate 	 */
76397c478bd9Sstevel@tonic-gate 	(void) ddi_map(hp->ah_dip, &mr, hp->ah_offset,
7640184cd04cScth 	    hp->ah_len, &hp->ah_addr);
76417c478bd9Sstevel@tonic-gate 	/*
76427c478bd9Sstevel@tonic-gate 	 * free the handle
76437c478bd9Sstevel@tonic-gate 	 */
76447c478bd9Sstevel@tonic-gate 	impl_acc_hdl_free(*handlep);
76457c478bd9Sstevel@tonic-gate 	*handlep = (ddi_acc_handle_t)NULL;
76467c478bd9Sstevel@tonic-gate }
76477c478bd9Sstevel@tonic-gate 
76487c478bd9Sstevel@tonic-gate int
76497c478bd9Sstevel@tonic-gate ddi_device_zero(ddi_acc_handle_t handle, caddr_t dev_addr, size_t bytecount,
76507c478bd9Sstevel@tonic-gate 	ssize_t dev_advcnt, uint_t dev_datasz)
76517c478bd9Sstevel@tonic-gate {
76527c478bd9Sstevel@tonic-gate 	uint8_t *b;
76537c478bd9Sstevel@tonic-gate 	uint16_t *w;
76547c478bd9Sstevel@tonic-gate 	uint32_t *l;
76557c478bd9Sstevel@tonic-gate 	uint64_t *ll;
76567c478bd9Sstevel@tonic-gate 
76577c478bd9Sstevel@tonic-gate 	/* check for total byte count is multiple of data transfer size */
76587c478bd9Sstevel@tonic-gate 	if (bytecount != ((bytecount / dev_datasz) * dev_datasz))
76597c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
76607c478bd9Sstevel@tonic-gate 
76617c478bd9Sstevel@tonic-gate 	switch (dev_datasz) {
76627c478bd9Sstevel@tonic-gate 	case DDI_DATA_SZ01_ACC:
76637c478bd9Sstevel@tonic-gate 		for (b = (uint8_t *)dev_addr;
7664184cd04cScth 		    bytecount != 0; bytecount -= 1, b += dev_advcnt)
76657c478bd9Sstevel@tonic-gate 			ddi_put8(handle, b, 0);
76667c478bd9Sstevel@tonic-gate 		break;
76677c478bd9Sstevel@tonic-gate 	case DDI_DATA_SZ02_ACC:
76687c478bd9Sstevel@tonic-gate 		for (w = (uint16_t *)dev_addr;
7669184cd04cScth 		    bytecount != 0; bytecount -= 2, w += dev_advcnt)
76707c478bd9Sstevel@tonic-gate 			ddi_put16(handle, w, 0);
76717c478bd9Sstevel@tonic-gate 		break;
76727c478bd9Sstevel@tonic-gate 	case DDI_DATA_SZ04_ACC:
76737c478bd9Sstevel@tonic-gate 		for (l = (uint32_t *)dev_addr;
7674184cd04cScth 		    bytecount != 0; bytecount -= 4, l += dev_advcnt)
76757c478bd9Sstevel@tonic-gate 			ddi_put32(handle, l, 0);
76767c478bd9Sstevel@tonic-gate 		break;
76777c478bd9Sstevel@tonic-gate 	case DDI_DATA_SZ08_ACC:
76787c478bd9Sstevel@tonic-gate 		for (ll = (uint64_t *)dev_addr;
7679184cd04cScth 		    bytecount != 0; bytecount -= 8, ll += dev_advcnt)
76807c478bd9Sstevel@tonic-gate 			ddi_put64(handle, ll, 0x0ll);
76817c478bd9Sstevel@tonic-gate 		break;
76827c478bd9Sstevel@tonic-gate 	default:
76837c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
76847c478bd9Sstevel@tonic-gate 	}
76857c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
76867c478bd9Sstevel@tonic-gate }
76877c478bd9Sstevel@tonic-gate 
76887c478bd9Sstevel@tonic-gate int
76897c478bd9Sstevel@tonic-gate ddi_device_copy(
76907c478bd9Sstevel@tonic-gate 	ddi_acc_handle_t src_handle, caddr_t src_addr, ssize_t src_advcnt,
76917c478bd9Sstevel@tonic-gate 	ddi_acc_handle_t dest_handle, caddr_t dest_addr, ssize_t dest_advcnt,
76927c478bd9Sstevel@tonic-gate 	size_t bytecount, uint_t dev_datasz)
76937c478bd9Sstevel@tonic-gate {
76947c478bd9Sstevel@tonic-gate 	uint8_t *b_src, *b_dst;
76957c478bd9Sstevel@tonic-gate 	uint16_t *w_src, *w_dst;
76967c478bd9Sstevel@tonic-gate 	uint32_t *l_src, *l_dst;
76977c478bd9Sstevel@tonic-gate 	uint64_t *ll_src, *ll_dst;
76987c478bd9Sstevel@tonic-gate 
76997c478bd9Sstevel@tonic-gate 	/* check for total byte count is multiple of data transfer size */
77007c478bd9Sstevel@tonic-gate 	if (bytecount != ((bytecount / dev_datasz) * dev_datasz))
77017c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
77027c478bd9Sstevel@tonic-gate 
77037c478bd9Sstevel@tonic-gate 	switch (dev_datasz) {
77047c478bd9Sstevel@tonic-gate 	case DDI_DATA_SZ01_ACC:
77057c478bd9Sstevel@tonic-gate 		b_src = (uint8_t *)src_addr;
77067c478bd9Sstevel@tonic-gate 		b_dst = (uint8_t *)dest_addr;
77077c478bd9Sstevel@tonic-gate 
77087c478bd9Sstevel@tonic-gate 		for (; bytecount != 0; bytecount -= 1) {
77097c478bd9Sstevel@tonic-gate 			ddi_put8(dest_handle, b_dst,
7710184cd04cScth 			    ddi_get8(src_handle, b_src));
77117c478bd9Sstevel@tonic-gate 			b_dst += dest_advcnt;
77127c478bd9Sstevel@tonic-gate 			b_src += src_advcnt;
77137c478bd9Sstevel@tonic-gate 		}
77147c478bd9Sstevel@tonic-gate 		break;
77157c478bd9Sstevel@tonic-gate 	case DDI_DATA_SZ02_ACC:
77167c478bd9Sstevel@tonic-gate 		w_src = (uint16_t *)src_addr;
77177c478bd9Sstevel@tonic-gate 		w_dst = (uint16_t *)dest_addr;
77187c478bd9Sstevel@tonic-gate 
77197c478bd9Sstevel@tonic-gate 		for (; bytecount != 0; bytecount -= 2) {
77207c478bd9Sstevel@tonic-gate 			ddi_put16(dest_handle, w_dst,
7721184cd04cScth 			    ddi_get16(src_handle, w_src));
77227c478bd9Sstevel@tonic-gate 			w_dst += dest_advcnt;
77237c478bd9Sstevel@tonic-gate 			w_src += src_advcnt;
77247c478bd9Sstevel@tonic-gate 		}
77257c478bd9Sstevel@tonic-gate 		break;
77267c478bd9Sstevel@tonic-gate 	case DDI_DATA_SZ04_ACC:
77277c478bd9Sstevel@tonic-gate 		l_src = (uint32_t *)src_addr;
77287c478bd9Sstevel@tonic-gate 		l_dst = (uint32_t *)dest_addr;
77297c478bd9Sstevel@tonic-gate 
77307c478bd9Sstevel@tonic-gate 		for (; bytecount != 0; bytecount -= 4) {
77317c478bd9Sstevel@tonic-gate 			ddi_put32(dest_handle, l_dst,
7732184cd04cScth 			    ddi_get32(src_handle, l_src));
77337c478bd9Sstevel@tonic-gate 			l_dst += dest_advcnt;
77347c478bd9Sstevel@tonic-gate 			l_src += src_advcnt;
77357c478bd9Sstevel@tonic-gate 		}
77367c478bd9Sstevel@tonic-gate 		break;
77377c478bd9Sstevel@tonic-gate 	case DDI_DATA_SZ08_ACC:
77387c478bd9Sstevel@tonic-gate 		ll_src = (uint64_t *)src_addr;
77397c478bd9Sstevel@tonic-gate 		ll_dst = (uint64_t *)dest_addr;
77407c478bd9Sstevel@tonic-gate 
77417c478bd9Sstevel@tonic-gate 		for (; bytecount != 0; bytecount -= 8) {
77427c478bd9Sstevel@tonic-gate 			ddi_put64(dest_handle, ll_dst,
7743184cd04cScth 			    ddi_get64(src_handle, ll_src));
77447c478bd9Sstevel@tonic-gate 			ll_dst += dest_advcnt;
77457c478bd9Sstevel@tonic-gate 			ll_src += src_advcnt;
77467c478bd9Sstevel@tonic-gate 		}
77477c478bd9Sstevel@tonic-gate 		break;
77487c478bd9Sstevel@tonic-gate 	default:
77497c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
77507c478bd9Sstevel@tonic-gate 	}
77517c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
77527c478bd9Sstevel@tonic-gate }
77537c478bd9Sstevel@tonic-gate 
77547c478bd9Sstevel@tonic-gate #define	swap16(value)  \
77557c478bd9Sstevel@tonic-gate 	((((value) & 0xff) << 8) | ((value) >> 8))
77567c478bd9Sstevel@tonic-gate 
77577c478bd9Sstevel@tonic-gate #define	swap32(value)	\
77587c478bd9Sstevel@tonic-gate 	(((uint32_t)swap16((uint16_t)((value) & 0xffff)) << 16) | \
77597c478bd9Sstevel@tonic-gate 	(uint32_t)swap16((uint16_t)((value) >> 16)))
77607c478bd9Sstevel@tonic-gate 
77617c478bd9Sstevel@tonic-gate #define	swap64(value)	\
77627c478bd9Sstevel@tonic-gate 	(((uint64_t)swap32((uint32_t)((value) & 0xffffffff)) \
77637c478bd9Sstevel@tonic-gate 	    << 32) | \
77647c478bd9Sstevel@tonic-gate 	(uint64_t)swap32((uint32_t)((value) >> 32)))
77657c478bd9Sstevel@tonic-gate 
77667c478bd9Sstevel@tonic-gate uint16_t
77677c478bd9Sstevel@tonic-gate ddi_swap16(uint16_t value)
77687c478bd9Sstevel@tonic-gate {
77697c478bd9Sstevel@tonic-gate 	return (swap16(value));
77707c478bd9Sstevel@tonic-gate }
77717c478bd9Sstevel@tonic-gate 
77727c478bd9Sstevel@tonic-gate uint32_t
77737c478bd9Sstevel@tonic-gate ddi_swap32(uint32_t value)
77747c478bd9Sstevel@tonic-gate {
77757c478bd9Sstevel@tonic-gate 	return (swap32(value));
77767c478bd9Sstevel@tonic-gate }
77777c478bd9Sstevel@tonic-gate 
77787c478bd9Sstevel@tonic-gate uint64_t
77797c478bd9Sstevel@tonic-gate ddi_swap64(uint64_t value)
77807c478bd9Sstevel@tonic-gate {
77817c478bd9Sstevel@tonic-gate 	return (swap64(value));
77827c478bd9Sstevel@tonic-gate }
77837c478bd9Sstevel@tonic-gate 
77847c478bd9Sstevel@tonic-gate /*
77857c478bd9Sstevel@tonic-gate  * Convert a binding name to a driver name.
77867c478bd9Sstevel@tonic-gate  * A binding name is the name used to determine the driver for a
77877c478bd9Sstevel@tonic-gate  * device - it may be either an alias for the driver or the name
77887c478bd9Sstevel@tonic-gate  * of the driver itself.
77897c478bd9Sstevel@tonic-gate  */
77907c478bd9Sstevel@tonic-gate char *
77917c478bd9Sstevel@tonic-gate i_binding_to_drv_name(char *bname)
77927c478bd9Sstevel@tonic-gate {
77937c478bd9Sstevel@tonic-gate 	major_t major_no;
77947c478bd9Sstevel@tonic-gate 
77957c478bd9Sstevel@tonic-gate 	ASSERT(bname != NULL);
77967c478bd9Sstevel@tonic-gate 
77977c478bd9Sstevel@tonic-gate 	if ((major_no = ddi_name_to_major(bname)) == -1)
77987c478bd9Sstevel@tonic-gate 		return (NULL);
77997c478bd9Sstevel@tonic-gate 	return (ddi_major_to_name(major_no));
78007c478bd9Sstevel@tonic-gate }
78017c478bd9Sstevel@tonic-gate 
78027c478bd9Sstevel@tonic-gate /*
78037c478bd9Sstevel@tonic-gate  * Search for minor name that has specified dev_t and spec_type.
78047c478bd9Sstevel@tonic-gate  * If spec_type is zero then any dev_t match works.  Since we
78057c478bd9Sstevel@tonic-gate  * are returning a pointer to the minor name string, we require the
78067c478bd9Sstevel@tonic-gate  * caller to do the locking.
78077c478bd9Sstevel@tonic-gate  */
78087c478bd9Sstevel@tonic-gate char *
78097c478bd9Sstevel@tonic-gate i_ddi_devtspectype_to_minorname(dev_info_t *dip, dev_t dev, int spec_type)
78107c478bd9Sstevel@tonic-gate {
78117c478bd9Sstevel@tonic-gate 	struct ddi_minor_data	*dmdp;
78127c478bd9Sstevel@tonic-gate 
78137c478bd9Sstevel@tonic-gate 	/*
78147c478bd9Sstevel@tonic-gate 	 * The did layered driver currently intentionally returns a
78157c478bd9Sstevel@tonic-gate 	 * devinfo ptr for an underlying sd instance based on a did
78167c478bd9Sstevel@tonic-gate 	 * dev_t. In this case it is not an error.
78177c478bd9Sstevel@tonic-gate 	 *
78187c478bd9Sstevel@tonic-gate 	 * The did layered driver is associated with Sun Cluster.
78197c478bd9Sstevel@tonic-gate 	 */
78207c478bd9Sstevel@tonic-gate 	ASSERT((ddi_driver_major(dip) == getmajor(dev)) ||
7821184cd04cScth 	    (strcmp(ddi_major_to_name(getmajor(dev)), "did") == 0));
78227c478bd9Sstevel@tonic-gate 
7823b9ccdc5aScth 	ASSERT(DEVI_BUSY_OWNED(dip));
78247c478bd9Sstevel@tonic-gate 	for (dmdp = DEVI(dip)->devi_minor; dmdp; dmdp = dmdp->next) {
78257c478bd9Sstevel@tonic-gate 		if (((dmdp->type == DDM_MINOR) ||
78267c478bd9Sstevel@tonic-gate 		    (dmdp->type == DDM_INTERNAL_PATH) ||
78277c478bd9Sstevel@tonic-gate 		    (dmdp->type == DDM_DEFAULT)) &&
78287c478bd9Sstevel@tonic-gate 		    (dmdp->ddm_dev == dev) &&
78297c478bd9Sstevel@tonic-gate 		    ((((spec_type & (S_IFCHR|S_IFBLK))) == 0) ||
78307c478bd9Sstevel@tonic-gate 		    (dmdp->ddm_spec_type == spec_type)))
78317c478bd9Sstevel@tonic-gate 			return (dmdp->ddm_name);
78327c478bd9Sstevel@tonic-gate 	}
78337c478bd9Sstevel@tonic-gate 
78347c478bd9Sstevel@tonic-gate 	return (NULL);
78357c478bd9Sstevel@tonic-gate }
78367c478bd9Sstevel@tonic-gate 
78377c478bd9Sstevel@tonic-gate /*
78387c478bd9Sstevel@tonic-gate  * Find the devt and spectype of the specified minor_name.
78397c478bd9Sstevel@tonic-gate  * Return DDI_FAILURE if minor_name not found. Since we are
78407c478bd9Sstevel@tonic-gate  * returning everything via arguments we can do the locking.
78417c478bd9Sstevel@tonic-gate  */
78427c478bd9Sstevel@tonic-gate int
78437c478bd9Sstevel@tonic-gate i_ddi_minorname_to_devtspectype(dev_info_t *dip, char *minor_name,
78447c478bd9Sstevel@tonic-gate 	dev_t *devtp, int *spectypep)
78457c478bd9Sstevel@tonic-gate {
7846b9ccdc5aScth 	int			circ;
78477c478bd9Sstevel@tonic-gate 	struct ddi_minor_data	*dmdp;
78487c478bd9Sstevel@tonic-gate 
78497c478bd9Sstevel@tonic-gate 	/* deal with clone minor nodes */
78507c478bd9Sstevel@tonic-gate 	if (dip == clone_dip) {
78517c478bd9Sstevel@tonic-gate 		major_t	major;
78527c478bd9Sstevel@tonic-gate 		/*
78537c478bd9Sstevel@tonic-gate 		 * Make sure minor_name is a STREAMS driver.
78547c478bd9Sstevel@tonic-gate 		 * We load the driver but don't attach to any instances.
78557c478bd9Sstevel@tonic-gate 		 */
78567c478bd9Sstevel@tonic-gate 
78577c478bd9Sstevel@tonic-gate 		major = ddi_name_to_major(minor_name);
7858a204de77Scth 		if (major == DDI_MAJOR_T_NONE)
78597c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
78607c478bd9Sstevel@tonic-gate 
78617c478bd9Sstevel@tonic-gate 		if (ddi_hold_driver(major) == NULL)
78627c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
78637c478bd9Sstevel@tonic-gate 
78647c478bd9Sstevel@tonic-gate 		if (STREAMSTAB(major) == NULL) {
78657c478bd9Sstevel@tonic-gate 			ddi_rele_driver(major);
78667c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
78677c478bd9Sstevel@tonic-gate 		}
78687c478bd9Sstevel@tonic-gate 		ddi_rele_driver(major);
78697c478bd9Sstevel@tonic-gate 
78707c478bd9Sstevel@tonic-gate 		if (devtp)
78717c478bd9Sstevel@tonic-gate 			*devtp = makedevice(clone_major, (minor_t)major);
78727c478bd9Sstevel@tonic-gate 
78737c478bd9Sstevel@tonic-gate 		if (spectypep)
78747c478bd9Sstevel@tonic-gate 			*spectypep = S_IFCHR;
78757c478bd9Sstevel@tonic-gate 
78767c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
78777c478bd9Sstevel@tonic-gate 	}
78787c478bd9Sstevel@tonic-gate 
7879b9ccdc5aScth 	ndi_devi_enter(dip, &circ);
78807c478bd9Sstevel@tonic-gate 	for (dmdp = DEVI(dip)->devi_minor; dmdp; dmdp = dmdp->next) {
78817c478bd9Sstevel@tonic-gate 		if (((dmdp->type != DDM_MINOR) &&
78827c478bd9Sstevel@tonic-gate 		    (dmdp->type != DDM_INTERNAL_PATH) &&
78837c478bd9Sstevel@tonic-gate 		    (dmdp->type != DDM_DEFAULT)) ||
78847c478bd9Sstevel@tonic-gate 		    strcmp(minor_name, dmdp->ddm_name))
78857c478bd9Sstevel@tonic-gate 			continue;
78867c478bd9Sstevel@tonic-gate 
78877c478bd9Sstevel@tonic-gate 		if (devtp)
78887c478bd9Sstevel@tonic-gate 			*devtp = dmdp->ddm_dev;
78897c478bd9Sstevel@tonic-gate 
78907c478bd9Sstevel@tonic-gate 		if (spectypep)
78917c478bd9Sstevel@tonic-gate 			*spectypep = dmdp->ddm_spec_type;
78927c478bd9Sstevel@tonic-gate 
7893b9ccdc5aScth 		ndi_devi_exit(dip, circ);
78947c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
78957c478bd9Sstevel@tonic-gate 	}
7896b9ccdc5aScth 	ndi_devi_exit(dip, circ);
78977c478bd9Sstevel@tonic-gate 
78987c478bd9Sstevel@tonic-gate 	return (DDI_FAILURE);
78997c478bd9Sstevel@tonic-gate }
79007c478bd9Sstevel@tonic-gate 
79017c478bd9Sstevel@tonic-gate static kmutex_t devid_gen_mutex;
79027c478bd9Sstevel@tonic-gate static short	devid_gen_number;
79037c478bd9Sstevel@tonic-gate 
79047c478bd9Sstevel@tonic-gate #ifdef DEBUG
79057c478bd9Sstevel@tonic-gate 
79067c478bd9Sstevel@tonic-gate static int	devid_register_corrupt = 0;
79077c478bd9Sstevel@tonic-gate static int	devid_register_corrupt_major = 0;
79087c478bd9Sstevel@tonic-gate static int	devid_register_corrupt_hint = 0;
79097c478bd9Sstevel@tonic-gate static int	devid_register_corrupt_hint_major = 0;
79107c478bd9Sstevel@tonic-gate 
79117c478bd9Sstevel@tonic-gate static int devid_lyr_debug = 0;
79127c478bd9Sstevel@tonic-gate 
79137c478bd9Sstevel@tonic-gate #define	DDI_DEBUG_DEVID_DEVTS(msg, ndevs, devs)		\
79147c478bd9Sstevel@tonic-gate 	if (devid_lyr_debug)					\
79157c478bd9Sstevel@tonic-gate 		ddi_debug_devid_devts(msg, ndevs, devs)
79167c478bd9Sstevel@tonic-gate 
79177c478bd9Sstevel@tonic-gate #else
79187c478bd9Sstevel@tonic-gate 
79197c478bd9Sstevel@tonic-gate #define	DDI_DEBUG_DEVID_DEVTS(msg, ndevs, devs)
79207c478bd9Sstevel@tonic-gate 
79217c478bd9Sstevel@tonic-gate #endif /* DEBUG */
79227c478bd9Sstevel@tonic-gate 
79237c478bd9Sstevel@tonic-gate 
79247c478bd9Sstevel@tonic-gate #ifdef	DEBUG
79257c478bd9Sstevel@tonic-gate 
79267c478bd9Sstevel@tonic-gate static void
79277c478bd9Sstevel@tonic-gate ddi_debug_devid_devts(char *msg, int ndevs, dev_t *devs)
79287c478bd9Sstevel@tonic-gate {
79297c478bd9Sstevel@tonic-gate 	int i;
79307c478bd9Sstevel@tonic-gate 
79317c478bd9Sstevel@tonic-gate 	cmn_err(CE_CONT, "%s:\n", msg);
79327c478bd9Sstevel@tonic-gate 	for (i = 0; i < ndevs; i++) {
79337c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "    0x%lx\n", devs[i]);
79347c478bd9Sstevel@tonic-gate 	}
79357c478bd9Sstevel@tonic-gate }
79367c478bd9Sstevel@tonic-gate 
79377c478bd9Sstevel@tonic-gate static void
79387c478bd9Sstevel@tonic-gate ddi_debug_devid_paths(char *msg, int npaths, char **paths)
79397c478bd9Sstevel@tonic-gate {
79407c478bd9Sstevel@tonic-gate 	int i;
79417c478bd9Sstevel@tonic-gate 
79427c478bd9Sstevel@tonic-gate 	cmn_err(CE_CONT, "%s:\n", msg);
79437c478bd9Sstevel@tonic-gate 	for (i = 0; i < npaths; i++) {
79447c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "    %s\n", paths[i]);
79457c478bd9Sstevel@tonic-gate 	}
79467c478bd9Sstevel@tonic-gate }
79477c478bd9Sstevel@tonic-gate 
79487c478bd9Sstevel@tonic-gate static void
79497c478bd9Sstevel@tonic-gate ddi_debug_devid_devts_per_path(char *path, int ndevs, dev_t *devs)
79507c478bd9Sstevel@tonic-gate {
79517c478bd9Sstevel@tonic-gate 	int i;
79527c478bd9Sstevel@tonic-gate 
79537c478bd9Sstevel@tonic-gate 	cmn_err(CE_CONT, "dev_ts per path %s\n", path);
79547c478bd9Sstevel@tonic-gate 	for (i = 0; i < ndevs; i++) {
79557c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "    0x%lx\n", devs[i]);
79567c478bd9Sstevel@tonic-gate 	}
79577c478bd9Sstevel@tonic-gate }
79587c478bd9Sstevel@tonic-gate 
79597c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
79607c478bd9Sstevel@tonic-gate 
79617c478bd9Sstevel@tonic-gate /*
79627c478bd9Sstevel@tonic-gate  * Register device id into DDI framework.
79637c478bd9Sstevel@tonic-gate  * Must be called when device is attached.
79647c478bd9Sstevel@tonic-gate  */
79657c478bd9Sstevel@tonic-gate static int
79667c478bd9Sstevel@tonic-gate i_ddi_devid_register(dev_info_t *dip, ddi_devid_t devid)
79677c478bd9Sstevel@tonic-gate {
79687c478bd9Sstevel@tonic-gate 	impl_devid_t	*i_devid = (impl_devid_t *)devid;
79697c478bd9Sstevel@tonic-gate 	size_t		driver_len;
79707c478bd9Sstevel@tonic-gate 	const char	*driver_name;
79717c478bd9Sstevel@tonic-gate 	char		*devid_str;
79727c478bd9Sstevel@tonic-gate 	major_t		major;
79737c478bd9Sstevel@tonic-gate 
79747c478bd9Sstevel@tonic-gate 	if ((dip == NULL) ||
7975a204de77Scth 	    ((major = ddi_driver_major(dip)) == DDI_MAJOR_T_NONE))
79767c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
79777c478bd9Sstevel@tonic-gate 
79787c478bd9Sstevel@tonic-gate 	/* verify that the devid is valid */
79797c478bd9Sstevel@tonic-gate 	if (ddi_devid_valid(devid) != DDI_SUCCESS)
79807c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
79817c478bd9Sstevel@tonic-gate 
79827c478bd9Sstevel@tonic-gate 	/* Updating driver name hint in devid */
79837c478bd9Sstevel@tonic-gate 	driver_name = ddi_driver_name(dip);
79847c478bd9Sstevel@tonic-gate 	driver_len = strlen(driver_name);
79857c478bd9Sstevel@tonic-gate 	if (driver_len > DEVID_HINT_SIZE) {
79867c478bd9Sstevel@tonic-gate 		/* Pick up last four characters of driver name */
79877c478bd9Sstevel@tonic-gate 		driver_name += driver_len - DEVID_HINT_SIZE;
79887c478bd9Sstevel@tonic-gate 		driver_len = DEVID_HINT_SIZE;
79897c478bd9Sstevel@tonic-gate 	}
79907c478bd9Sstevel@tonic-gate 	bzero(i_devid->did_driver, DEVID_HINT_SIZE);
79917c478bd9Sstevel@tonic-gate 	bcopy(driver_name, i_devid->did_driver, driver_len);
79927c478bd9Sstevel@tonic-gate 
79937c478bd9Sstevel@tonic-gate #ifdef DEBUG
79947c478bd9Sstevel@tonic-gate 	/* Corrupt the devid for testing. */
79957c478bd9Sstevel@tonic-gate 	if (devid_register_corrupt)
79967c478bd9Sstevel@tonic-gate 		i_devid->did_id[0] += devid_register_corrupt;
79977c478bd9Sstevel@tonic-gate 	if (devid_register_corrupt_major &&
79987c478bd9Sstevel@tonic-gate 	    (major == devid_register_corrupt_major))
79997c478bd9Sstevel@tonic-gate 		i_devid->did_id[0] += 1;
80007c478bd9Sstevel@tonic-gate 	if (devid_register_corrupt_hint)
80017c478bd9Sstevel@tonic-gate 		i_devid->did_driver[0] += devid_register_corrupt_hint;
80027c478bd9Sstevel@tonic-gate 	if (devid_register_corrupt_hint_major &&
80037c478bd9Sstevel@tonic-gate 	    (major == devid_register_corrupt_hint_major))
80047c478bd9Sstevel@tonic-gate 		i_devid->did_driver[0] += 1;
80057c478bd9Sstevel@tonic-gate #endif /* DEBUG */
80067c478bd9Sstevel@tonic-gate 
80077c478bd9Sstevel@tonic-gate 	/* encode the devid as a string */
80087c478bd9Sstevel@tonic-gate 	if ((devid_str = ddi_devid_str_encode(devid, NULL)) == NULL)
80097c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
80107c478bd9Sstevel@tonic-gate 
80117c478bd9Sstevel@tonic-gate 	/* add string as a string property */
80127c478bd9Sstevel@tonic-gate 	if (ndi_prop_update_string(DDI_DEV_T_NONE, dip,
80137c478bd9Sstevel@tonic-gate 	    DEVID_PROP_NAME, devid_str) != DDI_SUCCESS) {
80147c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d: devid property update failed",
8015184cd04cScth 		    ddi_driver_name(dip), ddi_get_instance(dip));
80167c478bd9Sstevel@tonic-gate 		ddi_devid_str_free(devid_str);
80177c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
80187c478bd9Sstevel@tonic-gate 	}
80197c478bd9Sstevel@tonic-gate 
8020602ca9eaScth 	/* keep pointer to devid string for interrupt context fma code */
8021602ca9eaScth 	if (DEVI(dip)->devi_devid_str)
8022602ca9eaScth 		ddi_devid_str_free(DEVI(dip)->devi_devid_str);
8023602ca9eaScth 	DEVI(dip)->devi_devid_str = devid_str;
80247c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
80257c478bd9Sstevel@tonic-gate }
80267c478bd9Sstevel@tonic-gate 
80277c478bd9Sstevel@tonic-gate int
80287c478bd9Sstevel@tonic-gate ddi_devid_register(dev_info_t *dip, ddi_devid_t devid)
80297c478bd9Sstevel@tonic-gate {
80307c478bd9Sstevel@tonic-gate 	int rval;
80317c478bd9Sstevel@tonic-gate 
80327c478bd9Sstevel@tonic-gate 	rval = i_ddi_devid_register(dip, devid);
80337c478bd9Sstevel@tonic-gate 	if (rval == DDI_SUCCESS) {
80347c478bd9Sstevel@tonic-gate 		/*
80357c478bd9Sstevel@tonic-gate 		 * Register devid in devid-to-path cache
80367c478bd9Sstevel@tonic-gate 		 */
80377c478bd9Sstevel@tonic-gate 		if (e_devid_cache_register(dip, devid) == DDI_SUCCESS) {
80387c478bd9Sstevel@tonic-gate 			mutex_enter(&DEVI(dip)->devi_lock);
80397c478bd9Sstevel@tonic-gate 			DEVI(dip)->devi_flags |= DEVI_REGISTERED_DEVID;
80407c478bd9Sstevel@tonic-gate 			mutex_exit(&DEVI(dip)->devi_lock);
80417c478bd9Sstevel@tonic-gate 		} else {
80427c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s%d: failed to cache devid",
8043184cd04cScth 			    ddi_driver_name(dip), ddi_get_instance(dip));
80447c478bd9Sstevel@tonic-gate 		}
80457c478bd9Sstevel@tonic-gate 	} else {
80467c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d: failed to register devid",
8047184cd04cScth 		    ddi_driver_name(dip), ddi_get_instance(dip));
80487c478bd9Sstevel@tonic-gate 	}
80497c478bd9Sstevel@tonic-gate 	return (rval);
80507c478bd9Sstevel@tonic-gate }
80517c478bd9Sstevel@tonic-gate 
80527c478bd9Sstevel@tonic-gate /*
80537c478bd9Sstevel@tonic-gate  * Remove (unregister) device id from DDI framework.
80547c478bd9Sstevel@tonic-gate  * Must be called when device is detached.
80557c478bd9Sstevel@tonic-gate  */
80567c478bd9Sstevel@tonic-gate static void
80577c478bd9Sstevel@tonic-gate i_ddi_devid_unregister(dev_info_t *dip)
80587c478bd9Sstevel@tonic-gate {
8059602ca9eaScth 	if (DEVI(dip)->devi_devid_str) {
8060602ca9eaScth 		ddi_devid_str_free(DEVI(dip)->devi_devid_str);
8061602ca9eaScth 		DEVI(dip)->devi_devid_str = NULL;
8062602ca9eaScth 	}
80637c478bd9Sstevel@tonic-gate 
80647c478bd9Sstevel@tonic-gate 	/* remove the devid property */
80657c478bd9Sstevel@tonic-gate 	(void) ndi_prop_remove(DDI_DEV_T_NONE, dip, DEVID_PROP_NAME);
80667c478bd9Sstevel@tonic-gate }
80677c478bd9Sstevel@tonic-gate 
80687c478bd9Sstevel@tonic-gate void
80697c478bd9Sstevel@tonic-gate ddi_devid_unregister(dev_info_t *dip)
80707c478bd9Sstevel@tonic-gate {
80717c478bd9Sstevel@tonic-gate 	mutex_enter(&DEVI(dip)->devi_lock);
80727c478bd9Sstevel@tonic-gate 	DEVI(dip)->devi_flags &= ~DEVI_REGISTERED_DEVID;
80737c478bd9Sstevel@tonic-gate 	mutex_exit(&DEVI(dip)->devi_lock);
80747c478bd9Sstevel@tonic-gate 	e_devid_cache_unregister(dip);
80757c478bd9Sstevel@tonic-gate 	i_ddi_devid_unregister(dip);
80767c478bd9Sstevel@tonic-gate }
80777c478bd9Sstevel@tonic-gate 
80787c478bd9Sstevel@tonic-gate /*
80797c478bd9Sstevel@tonic-gate  * Allocate and initialize a device id.
80807c478bd9Sstevel@tonic-gate  */
80817c478bd9Sstevel@tonic-gate int
80827c478bd9Sstevel@tonic-gate ddi_devid_init(
80837c478bd9Sstevel@tonic-gate 	dev_info_t	*dip,
80847c478bd9Sstevel@tonic-gate 	ushort_t	devid_type,
80857c478bd9Sstevel@tonic-gate 	ushort_t	nbytes,
80867c478bd9Sstevel@tonic-gate 	void		*id,
80877c478bd9Sstevel@tonic-gate 	ddi_devid_t	*ret_devid)
80887c478bd9Sstevel@tonic-gate {
80897c478bd9Sstevel@tonic-gate 	impl_devid_t	*i_devid;
80907c478bd9Sstevel@tonic-gate 	int		sz = sizeof (*i_devid) + nbytes - sizeof (char);
80917c478bd9Sstevel@tonic-gate 	int		driver_len;
80927c478bd9Sstevel@tonic-gate 	const char	*driver_name;
80937c478bd9Sstevel@tonic-gate 
80947c478bd9Sstevel@tonic-gate 	switch (devid_type) {
80957c478bd9Sstevel@tonic-gate 	case DEVID_SCSI3_WWN:
80967c478bd9Sstevel@tonic-gate 		/*FALLTHRU*/
80977c478bd9Sstevel@tonic-gate 	case DEVID_SCSI_SERIAL:
80987c478bd9Sstevel@tonic-gate 		/*FALLTHRU*/
80997c478bd9Sstevel@tonic-gate 	case DEVID_ATA_SERIAL:
81007c478bd9Sstevel@tonic-gate 		/*FALLTHRU*/
81017c478bd9Sstevel@tonic-gate 	case DEVID_ENCAP:
81027c478bd9Sstevel@tonic-gate 		if (nbytes == 0)
81037c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
81047c478bd9Sstevel@tonic-gate 		if (id == NULL)
81057c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
81067c478bd9Sstevel@tonic-gate 		break;
81077c478bd9Sstevel@tonic-gate 	case DEVID_FAB:
81087c478bd9Sstevel@tonic-gate 		if (nbytes != 0)
81097c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
81107c478bd9Sstevel@tonic-gate 		if (id != NULL)
81117c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
81127c478bd9Sstevel@tonic-gate 		nbytes = sizeof (int) +
81137c478bd9Sstevel@tonic-gate 		    sizeof (struct timeval32) + sizeof (short);
81147c478bd9Sstevel@tonic-gate 		sz += nbytes;
81157c478bd9Sstevel@tonic-gate 		break;
81167c478bd9Sstevel@tonic-gate 	default:
81177c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
81187c478bd9Sstevel@tonic-gate 	}
81197c478bd9Sstevel@tonic-gate 
81207c478bd9Sstevel@tonic-gate 	if ((i_devid = kmem_zalloc(sz, KM_SLEEP)) == NULL)
81217c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
81227c478bd9Sstevel@tonic-gate 
81237c478bd9Sstevel@tonic-gate 	i_devid->did_magic_hi = DEVID_MAGIC_MSB;
81247c478bd9Sstevel@tonic-gate 	i_devid->did_magic_lo = DEVID_MAGIC_LSB;
81257c478bd9Sstevel@tonic-gate 	i_devid->did_rev_hi = DEVID_REV_MSB;
81267c478bd9Sstevel@tonic-gate 	i_devid->did_rev_lo = DEVID_REV_LSB;
81277c478bd9Sstevel@tonic-gate 	DEVID_FORMTYPE(i_devid, devid_type);
81287c478bd9Sstevel@tonic-gate 	DEVID_FORMLEN(i_devid, nbytes);
81297c478bd9Sstevel@tonic-gate 
81307c478bd9Sstevel@tonic-gate 	/* Fill in driver name hint */
81317c478bd9Sstevel@tonic-gate 	driver_name = ddi_driver_name(dip);
81327c478bd9Sstevel@tonic-gate 	driver_len = strlen(driver_name);
81337c478bd9Sstevel@tonic-gate 	if (driver_len > DEVID_HINT_SIZE) {
81347c478bd9Sstevel@tonic-gate 		/* Pick up last four characters of driver name */
81357c478bd9Sstevel@tonic-gate 		driver_name += driver_len - DEVID_HINT_SIZE;
81367c478bd9Sstevel@tonic-gate 		driver_len = DEVID_HINT_SIZE;
81377c478bd9Sstevel@tonic-gate 	}
81387c478bd9Sstevel@tonic-gate 
81397c478bd9Sstevel@tonic-gate 	bcopy(driver_name, i_devid->did_driver, driver_len);
81407c478bd9Sstevel@tonic-gate 
81417c478bd9Sstevel@tonic-gate 	/* Fill in id field */
81427c478bd9Sstevel@tonic-gate 	if (devid_type == DEVID_FAB) {
81437c478bd9Sstevel@tonic-gate 		char		*cp;
81445679c89fSjv 		uint32_t	hostid;
81457c478bd9Sstevel@tonic-gate 		struct timeval32 timestamp32;
81467c478bd9Sstevel@tonic-gate 		int		i;
81477c478bd9Sstevel@tonic-gate 		int		*ip;
81487c478bd9Sstevel@tonic-gate 		short		gen;
81497c478bd9Sstevel@tonic-gate 
81507c478bd9Sstevel@tonic-gate 		/* increase the generation number */
81517c478bd9Sstevel@tonic-gate 		mutex_enter(&devid_gen_mutex);
81527c478bd9Sstevel@tonic-gate 		gen = devid_gen_number++;
81537c478bd9Sstevel@tonic-gate 		mutex_exit(&devid_gen_mutex);
81547c478bd9Sstevel@tonic-gate 
81557c478bd9Sstevel@tonic-gate 		cp = i_devid->did_id;
81567c478bd9Sstevel@tonic-gate 
81577c478bd9Sstevel@tonic-gate 		/* Fill in host id (big-endian byte ordering) */
81585679c89fSjv 		hostid = zone_get_hostid(NULL);
81597c478bd9Sstevel@tonic-gate 		*cp++ = hibyte(hiword(hostid));
81607c478bd9Sstevel@tonic-gate 		*cp++ = lobyte(hiword(hostid));
81617c478bd9Sstevel@tonic-gate 		*cp++ = hibyte(loword(hostid));
81627c478bd9Sstevel@tonic-gate 		*cp++ = lobyte(loword(hostid));
81637c478bd9Sstevel@tonic-gate 
81647c478bd9Sstevel@tonic-gate 		/*
81657c478bd9Sstevel@tonic-gate 		 * Fill in timestamp (big-endian byte ordering)
81667c478bd9Sstevel@tonic-gate 		 *
81677c478bd9Sstevel@tonic-gate 		 * (Note that the format may have to be changed
81687c478bd9Sstevel@tonic-gate 		 * before 2038 comes around, though it's arguably
81697c478bd9Sstevel@tonic-gate 		 * unique enough as it is..)
81707c478bd9Sstevel@tonic-gate 		 */
81717c478bd9Sstevel@tonic-gate 		uniqtime32(&timestamp32);
81727c478bd9Sstevel@tonic-gate 		ip = (int *)&timestamp32;
81737c478bd9Sstevel@tonic-gate 		for (i = 0;
81747c478bd9Sstevel@tonic-gate 		    i < sizeof (timestamp32) / sizeof (int); i++, ip++) {
81757c478bd9Sstevel@tonic-gate 			int	val;
81767c478bd9Sstevel@tonic-gate 			val = *ip;
81777c478bd9Sstevel@tonic-gate 			*cp++ = hibyte(hiword(val));
81787c478bd9Sstevel@tonic-gate 			*cp++ = lobyte(hiword(val));
81797c478bd9Sstevel@tonic-gate 			*cp++ = hibyte(loword(val));
81807c478bd9Sstevel@tonic-gate 			*cp++ = lobyte(loword(val));
81817c478bd9Sstevel@tonic-gate 		}
81827c478bd9Sstevel@tonic-gate 
81837c478bd9Sstevel@tonic-gate 		/* fill in the generation number */
81847c478bd9Sstevel@tonic-gate 		*cp++ = hibyte(gen);
81857c478bd9Sstevel@tonic-gate 		*cp++ = lobyte(gen);
81867c478bd9Sstevel@tonic-gate 	} else
81877c478bd9Sstevel@tonic-gate 		bcopy(id, i_devid->did_id, nbytes);
81887c478bd9Sstevel@tonic-gate 
81897c478bd9Sstevel@tonic-gate 	/* return device id */
81907c478bd9Sstevel@tonic-gate 	*ret_devid = (ddi_devid_t)i_devid;
81917c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
81927c478bd9Sstevel@tonic-gate }
81937c478bd9Sstevel@tonic-gate 
81948d483882Smlf int
81958d483882Smlf ddi_devid_get(dev_info_t *dip, ddi_devid_t *ret_devid)
81968d483882Smlf {
81978d483882Smlf 	return (i_ddi_devi_get_devid(DDI_DEV_T_ANY, dip, ret_devid));
81988d483882Smlf }
81998d483882Smlf 
82007c478bd9Sstevel@tonic-gate int
82017c478bd9Sstevel@tonic-gate i_ddi_devi_get_devid(dev_t dev, dev_info_t *dip, ddi_devid_t *ret_devid)
82027c478bd9Sstevel@tonic-gate {
82037c478bd9Sstevel@tonic-gate 	char		*devidstr;
82047c478bd9Sstevel@tonic-gate 
82057c478bd9Sstevel@tonic-gate 	ASSERT(dev != DDI_DEV_T_NONE);
82067c478bd9Sstevel@tonic-gate 
82077c478bd9Sstevel@tonic-gate 	/* look up the property, devt specific first */
82087c478bd9Sstevel@tonic-gate 	if (ddi_prop_lookup_string(dev, dip, DDI_PROP_DONTPASS,
82097c478bd9Sstevel@tonic-gate 	    DEVID_PROP_NAME, &devidstr) != DDI_PROP_SUCCESS) {
82107c478bd9Sstevel@tonic-gate 		if ((dev == DDI_DEV_T_ANY) ||
82117c478bd9Sstevel@tonic-gate 		    (ddi_prop_lookup_string(DDI_DEV_T_ANY, dip,
8212184cd04cScth 		    DDI_PROP_DONTPASS, DEVID_PROP_NAME, &devidstr) !=
8213184cd04cScth 		    DDI_PROP_SUCCESS)) {
8214184cd04cScth 			return (DDI_FAILURE);
82157c478bd9Sstevel@tonic-gate 		}
82167c478bd9Sstevel@tonic-gate 	}
82177c478bd9Sstevel@tonic-gate 
82187c478bd9Sstevel@tonic-gate 	/* convert to binary form */
82197c478bd9Sstevel@tonic-gate 	if (ddi_devid_str_decode(devidstr, ret_devid, NULL) == -1) {
82207c478bd9Sstevel@tonic-gate 		ddi_prop_free(devidstr);
82217c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
82227c478bd9Sstevel@tonic-gate 	}
82237c478bd9Sstevel@tonic-gate 	ddi_prop_free(devidstr);
82247c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
82257c478bd9Sstevel@tonic-gate }
82267c478bd9Sstevel@tonic-gate 
82277c478bd9Sstevel@tonic-gate /*
82287c478bd9Sstevel@tonic-gate  * Return a copy of the device id for dev_t
82297c478bd9Sstevel@tonic-gate  */
82307c478bd9Sstevel@tonic-gate int
82317c478bd9Sstevel@tonic-gate ddi_lyr_get_devid(dev_t dev, ddi_devid_t *ret_devid)
82327c478bd9Sstevel@tonic-gate {
82337c478bd9Sstevel@tonic-gate 	dev_info_t	*dip;
82347c478bd9Sstevel@tonic-gate 	int		rval;
82357c478bd9Sstevel@tonic-gate 
82367c478bd9Sstevel@tonic-gate 	/* get the dip */
82377c478bd9Sstevel@tonic-gate 	if ((dip = e_ddi_hold_devi_by_dev(dev, 0)) == NULL)
82387c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
82397c478bd9Sstevel@tonic-gate 
82407c478bd9Sstevel@tonic-gate 	rval = i_ddi_devi_get_devid(dev, dip, ret_devid);
82417c478bd9Sstevel@tonic-gate 
82427c478bd9Sstevel@tonic-gate 	ddi_release_devi(dip);		/* e_ddi_hold_devi_by_dev() */
82437c478bd9Sstevel@tonic-gate 	return (rval);
82447c478bd9Sstevel@tonic-gate }
82457c478bd9Sstevel@tonic-gate 
82467c478bd9Sstevel@tonic-gate /*
82477c478bd9Sstevel@tonic-gate  * Return a copy of the minor name for dev_t and spec_type
82487c478bd9Sstevel@tonic-gate  */
82497c478bd9Sstevel@tonic-gate int
82507c478bd9Sstevel@tonic-gate ddi_lyr_get_minor_name(dev_t dev, int spec_type, char **minor_name)
82517c478bd9Sstevel@tonic-gate {
8252b9ccdc5aScth 	char		*buf;
8253b9ccdc5aScth 	int		circ;
82547c478bd9Sstevel@tonic-gate 	dev_info_t	*dip;
82557c478bd9Sstevel@tonic-gate 	char		*nm;
8256b9ccdc5aScth 	int		rval;
82577c478bd9Sstevel@tonic-gate 
8258b9ccdc5aScth 	if ((dip = e_ddi_hold_devi_by_dev(dev, 0)) == NULL) {
8259b9ccdc5aScth 		*minor_name = NULL;
82607c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
82617c478bd9Sstevel@tonic-gate 	}
82627c478bd9Sstevel@tonic-gate 
8263b9ccdc5aScth 	/* Find the minor name and copy into max size buf */
8264b9ccdc5aScth 	buf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
8265b9ccdc5aScth 	ndi_devi_enter(dip, &circ);
8266b9ccdc5aScth 	nm = i_ddi_devtspectype_to_minorname(dip, dev, spec_type);
8267b9ccdc5aScth 	if (nm)
8268b9ccdc5aScth 		(void) strcpy(buf, nm);
8269b9ccdc5aScth 	ndi_devi_exit(dip, circ);
8270b9ccdc5aScth 	ddi_release_devi(dip);	/* e_ddi_hold_devi_by_dev() */
82717c478bd9Sstevel@tonic-gate 
8272b9ccdc5aScth 	if (nm) {
8273b9ccdc5aScth 		/* duplicate into min size buf for return result */
8274b9ccdc5aScth 		*minor_name = i_ddi_strdup(buf, KM_SLEEP);
8275b9ccdc5aScth 		rval = DDI_SUCCESS;
8276b9ccdc5aScth 	} else {
82777c478bd9Sstevel@tonic-gate 		*minor_name = NULL;
8278b9ccdc5aScth 		rval = DDI_FAILURE;
82797c478bd9Sstevel@tonic-gate 	}
82807c478bd9Sstevel@tonic-gate 
8281b9ccdc5aScth 	/* free max size buf and return */
8282b9ccdc5aScth 	kmem_free(buf, MAXNAMELEN);
8283b9ccdc5aScth 	return (rval);
82847c478bd9Sstevel@tonic-gate }
82857c478bd9Sstevel@tonic-gate 
82867c478bd9Sstevel@tonic-gate int
82877c478bd9Sstevel@tonic-gate ddi_lyr_devid_to_devlist(
82887c478bd9Sstevel@tonic-gate 	ddi_devid_t	devid,
82897c478bd9Sstevel@tonic-gate 	char		*minor_name,
82907c478bd9Sstevel@tonic-gate 	int		*retndevs,
82917c478bd9Sstevel@tonic-gate 	dev_t		**retdevs)
82927c478bd9Sstevel@tonic-gate {
82937c478bd9Sstevel@tonic-gate 	ASSERT(ddi_devid_valid(devid) == DDI_SUCCESS);
82947c478bd9Sstevel@tonic-gate 
82957c478bd9Sstevel@tonic-gate 	if (e_devid_cache_to_devt_list(devid, minor_name,
82967c478bd9Sstevel@tonic-gate 	    retndevs, retdevs) == DDI_SUCCESS) {
82977c478bd9Sstevel@tonic-gate 		ASSERT(*retndevs > 0);
82987c478bd9Sstevel@tonic-gate 		DDI_DEBUG_DEVID_DEVTS("ddi_lyr_devid_to_devlist",
8299184cd04cScth 		    *retndevs, *retdevs);
83007c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
83017c478bd9Sstevel@tonic-gate 	}
83027c478bd9Sstevel@tonic-gate 
83037c478bd9Sstevel@tonic-gate 	if (e_ddi_devid_discovery(devid) == DDI_FAILURE) {
83047c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
83057c478bd9Sstevel@tonic-gate 	}
83067c478bd9Sstevel@tonic-gate 
83077c478bd9Sstevel@tonic-gate 	if (e_devid_cache_to_devt_list(devid, minor_name,
83087c478bd9Sstevel@tonic-gate 	    retndevs, retdevs) == DDI_SUCCESS) {
83097c478bd9Sstevel@tonic-gate 		ASSERT(*retndevs > 0);
83107c478bd9Sstevel@tonic-gate 		DDI_DEBUG_DEVID_DEVTS("ddi_lyr_devid_to_devlist",
8311184cd04cScth 		    *retndevs, *retdevs);
83127c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
83137c478bd9Sstevel@tonic-gate 	}
83147c478bd9Sstevel@tonic-gate 
83157c478bd9Sstevel@tonic-gate 	return (DDI_FAILURE);
83167c478bd9Sstevel@tonic-gate }
83177c478bd9Sstevel@tonic-gate 
83187c478bd9Sstevel@tonic-gate void
83197c478bd9Sstevel@tonic-gate ddi_lyr_free_devlist(dev_t *devlist, int ndevs)
83207c478bd9Sstevel@tonic-gate {
83217c478bd9Sstevel@tonic-gate 	kmem_free(devlist, sizeof (dev_t) * ndevs);
83227c478bd9Sstevel@tonic-gate }
83237c478bd9Sstevel@tonic-gate 
83247c478bd9Sstevel@tonic-gate /*
83257c478bd9Sstevel@tonic-gate  * Note: This will need to be fixed if we ever allow processes to
83267c478bd9Sstevel@tonic-gate  * have more than one data model per exec.
83277c478bd9Sstevel@tonic-gate  */
83287c478bd9Sstevel@tonic-gate model_t
83297c478bd9Sstevel@tonic-gate ddi_mmap_get_model(void)
83307c478bd9Sstevel@tonic-gate {
83317c478bd9Sstevel@tonic-gate 	return (get_udatamodel());
83327c478bd9Sstevel@tonic-gate }
83337c478bd9Sstevel@tonic-gate 
83347c478bd9Sstevel@tonic-gate model_t
83357c478bd9Sstevel@tonic-gate ddi_model_convert_from(model_t model)
83367c478bd9Sstevel@tonic-gate {
83377c478bd9Sstevel@tonic-gate 	return ((model & DDI_MODEL_MASK) & ~DDI_MODEL_NATIVE);
83387c478bd9Sstevel@tonic-gate }
83397c478bd9Sstevel@tonic-gate 
83407c478bd9Sstevel@tonic-gate /*
83417c478bd9Sstevel@tonic-gate  * ddi interfaces managing storage and retrieval of eventcookies.
83427c478bd9Sstevel@tonic-gate  */
83437c478bd9Sstevel@tonic-gate 
83447c478bd9Sstevel@tonic-gate /*
83457c478bd9Sstevel@tonic-gate  * Invoke bus nexus driver's implementation of the
83467c478bd9Sstevel@tonic-gate  * (*bus_remove_eventcall)() interface to remove a registered
83477c478bd9Sstevel@tonic-gate  * callback handler for "event".
83487c478bd9Sstevel@tonic-gate  */
83497c478bd9Sstevel@tonic-gate int
83507c478bd9Sstevel@tonic-gate ddi_remove_event_handler(ddi_callback_id_t id)
83517c478bd9Sstevel@tonic-gate {
83527c478bd9Sstevel@tonic-gate 	ndi_event_callbacks_t *cb = (ndi_event_callbacks_t *)id;
83537c478bd9Sstevel@tonic-gate 	dev_info_t *ddip;
83547c478bd9Sstevel@tonic-gate 
83557c478bd9Sstevel@tonic-gate 	ASSERT(cb);
83567c478bd9Sstevel@tonic-gate 	if (!cb) {
83577c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
83587c478bd9Sstevel@tonic-gate 	}
83597c478bd9Sstevel@tonic-gate 
83607c478bd9Sstevel@tonic-gate 	ddip = NDI_EVENT_DDIP(cb->ndi_evtcb_cookie);
83617c478bd9Sstevel@tonic-gate 	return (ndi_busop_remove_eventcall(ddip, id));
83627c478bd9Sstevel@tonic-gate }
83637c478bd9Sstevel@tonic-gate 
83647c478bd9Sstevel@tonic-gate /*
83657c478bd9Sstevel@tonic-gate  * Invoke bus nexus driver's implementation of the
83667c478bd9Sstevel@tonic-gate  * (*bus_add_eventcall)() interface to register a callback handler
83677c478bd9Sstevel@tonic-gate  * for "event".
83687c478bd9Sstevel@tonic-gate  */
83697c478bd9Sstevel@tonic-gate int
83707c478bd9Sstevel@tonic-gate ddi_add_event_handler(dev_info_t *dip, ddi_eventcookie_t event,
83717c478bd9Sstevel@tonic-gate     void (*handler)(dev_info_t *, ddi_eventcookie_t, void *, void *),
83727c478bd9Sstevel@tonic-gate     void *arg, ddi_callback_id_t *id)
83737c478bd9Sstevel@tonic-gate {
83747c478bd9Sstevel@tonic-gate 	return (ndi_busop_add_eventcall(dip, dip, event, handler, arg, id));
83757c478bd9Sstevel@tonic-gate }
83767c478bd9Sstevel@tonic-gate 
83777c478bd9Sstevel@tonic-gate 
83787c478bd9Sstevel@tonic-gate /*
83797c478bd9Sstevel@tonic-gate  * Return a handle for event "name" by calling up the device tree
83807c478bd9Sstevel@tonic-gate  * hierarchy via  (*bus_get_eventcookie)() interface until claimed
83817c478bd9Sstevel@tonic-gate  * by a bus nexus or top of dev_info tree is reached.
83827c478bd9Sstevel@tonic-gate  */
83837c478bd9Sstevel@tonic-gate int
83847c478bd9Sstevel@tonic-gate ddi_get_eventcookie(dev_info_t *dip, char *name,
83857c478bd9Sstevel@tonic-gate     ddi_eventcookie_t *event_cookiep)
83867c478bd9Sstevel@tonic-gate {
83877c478bd9Sstevel@tonic-gate 	return (ndi_busop_get_eventcookie(dip, dip,
83887c478bd9Sstevel@tonic-gate 	    name, event_cookiep));
83897c478bd9Sstevel@tonic-gate }
83907c478bd9Sstevel@tonic-gate 
83917c478bd9Sstevel@tonic-gate /*
83927c478bd9Sstevel@tonic-gate  * This procedure is provided as the general callback function when
83937c478bd9Sstevel@tonic-gate  * umem_lockmemory calls as_add_callback for long term memory locking.
83947c478bd9Sstevel@tonic-gate  * When as_unmap, as_setprot, or as_free encounter segments which have
83957c478bd9Sstevel@tonic-gate  * locked memory, this callback will be invoked.
83967c478bd9Sstevel@tonic-gate  */
83977c478bd9Sstevel@tonic-gate void
83987c478bd9Sstevel@tonic-gate umem_lock_undo(struct as *as, void *arg, uint_t event)
83997c478bd9Sstevel@tonic-gate {
84007c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(as, event))
84017c478bd9Sstevel@tonic-gate 	struct ddi_umem_cookie *cp = (struct ddi_umem_cookie *)arg;
84027c478bd9Sstevel@tonic-gate 
84037c478bd9Sstevel@tonic-gate 	/*
84047c478bd9Sstevel@tonic-gate 	 * Call the cleanup function.  Decrement the cookie reference
84057c478bd9Sstevel@tonic-gate 	 * count, if it goes to zero, return the memory for the cookie.
84067c478bd9Sstevel@tonic-gate 	 * The i_ddi_umem_unlock for this cookie may or may not have been
84077c478bd9Sstevel@tonic-gate 	 * called already.  It is the responsibility of the caller of
84087c478bd9Sstevel@tonic-gate 	 * umem_lockmemory to handle the case of the cleanup routine
84097c478bd9Sstevel@tonic-gate 	 * being called after a ddi_umem_unlock for the cookie
84107c478bd9Sstevel@tonic-gate 	 * was called.
84117c478bd9Sstevel@tonic-gate 	 */
84127c478bd9Sstevel@tonic-gate 
84137c478bd9Sstevel@tonic-gate 	(*cp->callbacks.cbo_umem_lock_cleanup)((ddi_umem_cookie_t)cp);
84147c478bd9Sstevel@tonic-gate 
84157c478bd9Sstevel@tonic-gate 	/* remove the cookie if reference goes to zero */
84167c478bd9Sstevel@tonic-gate 	if (atomic_add_long_nv((ulong_t *)(&(cp->cook_refcnt)), -1) == 0) {
84177c478bd9Sstevel@tonic-gate 		kmem_free(cp, sizeof (struct ddi_umem_cookie));
84187c478bd9Sstevel@tonic-gate 	}
84197c478bd9Sstevel@tonic-gate }
84207c478bd9Sstevel@tonic-gate 
84217c478bd9Sstevel@tonic-gate /*
84227c478bd9Sstevel@tonic-gate  * The following two Consolidation Private routines provide generic
84237c478bd9Sstevel@tonic-gate  * interfaces to increase/decrease the amount of device-locked memory.
84247c478bd9Sstevel@tonic-gate  *
84257c478bd9Sstevel@tonic-gate  * To keep project_rele and project_hold consistent, i_ddi_decr_locked_memory()
84267c478bd9Sstevel@tonic-gate  * must be called every time i_ddi_incr_locked_memory() is called.
84277c478bd9Sstevel@tonic-gate  */
84287c478bd9Sstevel@tonic-gate int
84297c478bd9Sstevel@tonic-gate /* ARGSUSED */
8430c6939658Ssl i_ddi_incr_locked_memory(proc_t *procp, rctl_qty_t inc)
84317c478bd9Sstevel@tonic-gate {
8432c6939658Ssl 	ASSERT(procp != NULL);
8433c6939658Ssl 	mutex_enter(&procp->p_lock);
8434c6939658Ssl 	if (rctl_incr_locked_mem(procp, NULL, inc, 1)) {
8435c6939658Ssl 		mutex_exit(&procp->p_lock);
84367c478bd9Sstevel@tonic-gate 		return (ENOMEM);
84377c478bd9Sstevel@tonic-gate 	}
8438c6939658Ssl 	mutex_exit(&procp->p_lock);
84397c478bd9Sstevel@tonic-gate 	return (0);
84407c478bd9Sstevel@tonic-gate }
84417c478bd9Sstevel@tonic-gate 
84427c478bd9Sstevel@tonic-gate /*
84437c478bd9Sstevel@tonic-gate  * To keep project_rele and project_hold consistent, i_ddi_incr_locked_memory()
84447c478bd9Sstevel@tonic-gate  * must be called every time i_ddi_decr_locked_memory() is called.
84457c478bd9Sstevel@tonic-gate  */
84467c478bd9Sstevel@tonic-gate /* ARGSUSED */
84477c478bd9Sstevel@tonic-gate void
8448c6939658Ssl i_ddi_decr_locked_memory(proc_t *procp, rctl_qty_t dec)
84497c478bd9Sstevel@tonic-gate {
8450c6939658Ssl 	ASSERT(procp != NULL);
8451c6939658Ssl 	mutex_enter(&procp->p_lock);
8452c6939658Ssl 	rctl_decr_locked_mem(procp, NULL, dec, 1);
8453c6939658Ssl 	mutex_exit(&procp->p_lock);
84547c478bd9Sstevel@tonic-gate }
84557c478bd9Sstevel@tonic-gate 
84567c478bd9Sstevel@tonic-gate /*
8457c6939658Ssl  * This routine checks if the max-locked-memory resource ctl is
84587c478bd9Sstevel@tonic-gate  * exceeded, if not increments it, grabs a hold on the project.
84597c478bd9Sstevel@tonic-gate  * Returns 0 if successful otherwise returns error code
84607c478bd9Sstevel@tonic-gate  */
84617c478bd9Sstevel@tonic-gate static int
84627c478bd9Sstevel@tonic-gate umem_incr_devlockmem(struct ddi_umem_cookie *cookie)
84637c478bd9Sstevel@tonic-gate {
84647c478bd9Sstevel@tonic-gate 	proc_t		*procp;
84657c478bd9Sstevel@tonic-gate 	int		ret;
84667c478bd9Sstevel@tonic-gate 
84677c478bd9Sstevel@tonic-gate 	ASSERT(cookie);
84687c478bd9Sstevel@tonic-gate 	procp = cookie->procp;
84697c478bd9Sstevel@tonic-gate 	ASSERT(procp);
84707c478bd9Sstevel@tonic-gate 
8471c6939658Ssl 	if ((ret = i_ddi_incr_locked_memory(procp,
8472184cd04cScth 	    cookie->size)) != 0) {
84737c478bd9Sstevel@tonic-gate 		return (ret);
84747c478bd9Sstevel@tonic-gate 	}
84757c478bd9Sstevel@tonic-gate 	return (0);
84767c478bd9Sstevel@tonic-gate }
84777c478bd9Sstevel@tonic-gate 
84787c478bd9Sstevel@tonic-gate /*
8479c6939658Ssl  * Decrements the max-locked-memory resource ctl and releases
84807c478bd9Sstevel@tonic-gate  * the hold on the project that was acquired during umem_incr_devlockmem
84817c478bd9Sstevel@tonic-gate  */
84827c478bd9Sstevel@tonic-gate static void
84837c478bd9Sstevel@tonic-gate umem_decr_devlockmem(struct ddi_umem_cookie *cookie)
84847c478bd9Sstevel@tonic-gate {
8485c6939658Ssl 	proc_t		*proc;
84867c478bd9Sstevel@tonic-gate 
8487c6939658Ssl 	proc = (proc_t *)cookie->procp;
8488c6939658Ssl 	if (!proc)
84897c478bd9Sstevel@tonic-gate 		return;
84907c478bd9Sstevel@tonic-gate 
8491c6939658Ssl 	i_ddi_decr_locked_memory(proc, cookie->size);
84927c478bd9Sstevel@tonic-gate }
84937c478bd9Sstevel@tonic-gate 
84947c478bd9Sstevel@tonic-gate /*
84957c478bd9Sstevel@tonic-gate  * A consolidation private function which is essentially equivalent to
84967c478bd9Sstevel@tonic-gate  * ddi_umem_lock but with the addition of arguments ops_vector and procp.
84977c478bd9Sstevel@tonic-gate  * A call to as_add_callback is done if DDI_UMEMLOCK_LONGTERM is set, and
84987c478bd9Sstevel@tonic-gate  * the ops_vector is valid.
84997c478bd9Sstevel@tonic-gate  *
85007c478bd9Sstevel@tonic-gate  * Lock the virtual address range in the current process and create a
85017c478bd9Sstevel@tonic-gate  * ddi_umem_cookie (of type UMEM_LOCKED). This can be used to pass to
85027c478bd9Sstevel@tonic-gate  * ddi_umem_iosetup to create a buf or do devmap_umem_setup/remap to export
85037c478bd9Sstevel@tonic-gate  * to user space.
85047c478bd9Sstevel@tonic-gate  *
85057c478bd9Sstevel@tonic-gate  * Note: The resource control accounting currently uses a full charge model
85067c478bd9Sstevel@tonic-gate  * in other words attempts to lock the same/overlapping areas of memory
85077c478bd9Sstevel@tonic-gate  * will deduct the full size of the buffer from the projects running
85087c478bd9Sstevel@tonic-gate  * counter for the device locked memory.
85097c478bd9Sstevel@tonic-gate  *
85107c478bd9Sstevel@tonic-gate  * addr, size should be PAGESIZE aligned
85117c478bd9Sstevel@tonic-gate  *
85127c478bd9Sstevel@tonic-gate  * flags - DDI_UMEMLOCK_READ, DDI_UMEMLOCK_WRITE or both
85137c478bd9Sstevel@tonic-gate  *	identifies whether the locked memory will be read or written or both
85147c478bd9Sstevel@tonic-gate  *      DDI_UMEMLOCK_LONGTERM  must be set when the locking will
85157c478bd9Sstevel@tonic-gate  * be maintained for an indefinitely long period (essentially permanent),
85167c478bd9Sstevel@tonic-gate  * rather than for what would be required for a typical I/O completion.
85177c478bd9Sstevel@tonic-gate  * When DDI_UMEMLOCK_LONGTERM is set, umem_lockmemory will return EFAULT
85187c478bd9Sstevel@tonic-gate  * if the memory pertains to a regular file which is mapped MAP_SHARED.
85197c478bd9Sstevel@tonic-gate  * This is to prevent a deadlock if a file truncation is attempted after
85207c478bd9Sstevel@tonic-gate  * after the locking is done.
85217c478bd9Sstevel@tonic-gate  *
85227c478bd9Sstevel@tonic-gate  * Returns 0 on success
85237c478bd9Sstevel@tonic-gate  *	EINVAL - for invalid parameters
85247c478bd9Sstevel@tonic-gate  *	EPERM, ENOMEM and other error codes returned by as_pagelock
85257c478bd9Sstevel@tonic-gate  *	ENOMEM - is returned if the current request to lock memory exceeds
8526c6939658Ssl  *		*.max-locked-memory resource control value.
85277c478bd9Sstevel@tonic-gate  *      EFAULT - memory pertains to a regular file mapped shared and
85287c478bd9Sstevel@tonic-gate  *		and DDI_UMEMLOCK_LONGTERM flag is set
85297c478bd9Sstevel@tonic-gate  *	EAGAIN - could not start the ddi_umem_unlock list processing thread
85307c478bd9Sstevel@tonic-gate  */
85317c478bd9Sstevel@tonic-gate int
85327c478bd9Sstevel@tonic-gate umem_lockmemory(caddr_t addr, size_t len, int flags, ddi_umem_cookie_t *cookie,
85337c478bd9Sstevel@tonic-gate 		struct umem_callback_ops *ops_vector,
85347c478bd9Sstevel@tonic-gate 		proc_t *procp)
85357c478bd9Sstevel@tonic-gate {
85367c478bd9Sstevel@tonic-gate 	int	error;
85377c478bd9Sstevel@tonic-gate 	struct ddi_umem_cookie *p;
85387c478bd9Sstevel@tonic-gate 	void	(*driver_callback)() = NULL;
8539d863b343SBill Taylor 	struct as *as;
85407c478bd9Sstevel@tonic-gate 	struct seg		*seg;
85417c478bd9Sstevel@tonic-gate 	vnode_t			*vp;
85427c478bd9Sstevel@tonic-gate 
8543d863b343SBill Taylor 	/* Allow device drivers to not have to reference "curproc" */
8544d863b343SBill Taylor 	if (procp == NULL)
8545d863b343SBill Taylor 		procp = curproc;
8546d863b343SBill Taylor 	as = procp->p_as;
85477c478bd9Sstevel@tonic-gate 	*cookie = NULL;		/* in case of any error return */
85487c478bd9Sstevel@tonic-gate 
85497c478bd9Sstevel@tonic-gate 	/* These are the only three valid flags */
85507c478bd9Sstevel@tonic-gate 	if ((flags & ~(DDI_UMEMLOCK_READ | DDI_UMEMLOCK_WRITE |
85517c478bd9Sstevel@tonic-gate 	    DDI_UMEMLOCK_LONGTERM)) != 0)
85527c478bd9Sstevel@tonic-gate 		return (EINVAL);
85537c478bd9Sstevel@tonic-gate 
85547c478bd9Sstevel@tonic-gate 	/* At least one (can be both) of the two access flags must be set */
85557c478bd9Sstevel@tonic-gate 	if ((flags & (DDI_UMEMLOCK_READ | DDI_UMEMLOCK_WRITE)) == 0)
85567c478bd9Sstevel@tonic-gate 		return (EINVAL);
85577c478bd9Sstevel@tonic-gate 
85587c478bd9Sstevel@tonic-gate 	/* addr and len must be page-aligned */
85597c478bd9Sstevel@tonic-gate 	if (((uintptr_t)addr & PAGEOFFSET) != 0)
85607c478bd9Sstevel@tonic-gate 		return (EINVAL);
85617c478bd9Sstevel@tonic-gate 
85627c478bd9Sstevel@tonic-gate 	if ((len & PAGEOFFSET) != 0)
85637c478bd9Sstevel@tonic-gate 		return (EINVAL);
85647c478bd9Sstevel@tonic-gate 
85657c478bd9Sstevel@tonic-gate 	/*
85667c478bd9Sstevel@tonic-gate 	 * For longterm locking a driver callback must be specified; if
85677c478bd9Sstevel@tonic-gate 	 * not longterm then a callback is optional.
85687c478bd9Sstevel@tonic-gate 	 */
85697c478bd9Sstevel@tonic-gate 	if (ops_vector != NULL) {
85707c478bd9Sstevel@tonic-gate 		if (ops_vector->cbo_umem_callback_version !=
85717c478bd9Sstevel@tonic-gate 		    UMEM_CALLBACK_VERSION)
85727c478bd9Sstevel@tonic-gate 			return (EINVAL);
85737c478bd9Sstevel@tonic-gate 		else
85747c478bd9Sstevel@tonic-gate 			driver_callback = ops_vector->cbo_umem_lock_cleanup;
85757c478bd9Sstevel@tonic-gate 	}
85767c478bd9Sstevel@tonic-gate 	if ((driver_callback == NULL) && (flags & DDI_UMEMLOCK_LONGTERM))
85777c478bd9Sstevel@tonic-gate 		return (EINVAL);
85787c478bd9Sstevel@tonic-gate 
85797c478bd9Sstevel@tonic-gate 	/*
85807c478bd9Sstevel@tonic-gate 	 * Call i_ddi_umem_unlock_thread_start if necessary.  It will
85817c478bd9Sstevel@tonic-gate 	 * be called on first ddi_umem_lock or umem_lockmemory call.
85827c478bd9Sstevel@tonic-gate 	 */
85837c478bd9Sstevel@tonic-gate 	if (ddi_umem_unlock_thread == NULL)
85847c478bd9Sstevel@tonic-gate 		i_ddi_umem_unlock_thread_start();
85857c478bd9Sstevel@tonic-gate 
85867c478bd9Sstevel@tonic-gate 	/* Allocate memory for the cookie */
85877c478bd9Sstevel@tonic-gate 	p = kmem_zalloc(sizeof (struct ddi_umem_cookie), KM_SLEEP);
85887c478bd9Sstevel@tonic-gate 
85897c478bd9Sstevel@tonic-gate 	/* Convert the flags to seg_rw type */
85907c478bd9Sstevel@tonic-gate 	if (flags & DDI_UMEMLOCK_WRITE) {
85917c478bd9Sstevel@tonic-gate 		p->s_flags = S_WRITE;
85927c478bd9Sstevel@tonic-gate 	} else {
85937c478bd9Sstevel@tonic-gate 		p->s_flags = S_READ;
85947c478bd9Sstevel@tonic-gate 	}
85957c478bd9Sstevel@tonic-gate 
85967c478bd9Sstevel@tonic-gate 	/* Store procp in cookie for later iosetup/unlock */
85977c478bd9Sstevel@tonic-gate 	p->procp = (void *)procp;
85987c478bd9Sstevel@tonic-gate 
85997c478bd9Sstevel@tonic-gate 	/*
86007c478bd9Sstevel@tonic-gate 	 * Store the struct as pointer in cookie for later use by
86017c478bd9Sstevel@tonic-gate 	 * ddi_umem_unlock.  The proc->p_as will be stale if ddi_umem_unlock
86027c478bd9Sstevel@tonic-gate 	 * is called after relvm is called.
86037c478bd9Sstevel@tonic-gate 	 */
86047c478bd9Sstevel@tonic-gate 	p->asp = as;
86057c478bd9Sstevel@tonic-gate 
86067c478bd9Sstevel@tonic-gate 	/*
86077c478bd9Sstevel@tonic-gate 	 * The size field is needed for lockmem accounting.
86087c478bd9Sstevel@tonic-gate 	 */
86097c478bd9Sstevel@tonic-gate 	p->size = len;
86107c478bd9Sstevel@tonic-gate 
86117c478bd9Sstevel@tonic-gate 	if (umem_incr_devlockmem(p) != 0) {
86127c478bd9Sstevel@tonic-gate 		/*
86137c478bd9Sstevel@tonic-gate 		 * The requested memory cannot be locked
86147c478bd9Sstevel@tonic-gate 		 */
86157c478bd9Sstevel@tonic-gate 		kmem_free(p, sizeof (struct ddi_umem_cookie));
86167c478bd9Sstevel@tonic-gate 		*cookie = (ddi_umem_cookie_t)NULL;
86177c478bd9Sstevel@tonic-gate 		return (ENOMEM);
86187c478bd9Sstevel@tonic-gate 	}
86197c478bd9Sstevel@tonic-gate 
86207c478bd9Sstevel@tonic-gate 	/* Lock the pages corresponding to addr, len in memory */
86217c478bd9Sstevel@tonic-gate 	error = as_pagelock(as, &(p->pparray), addr, len, p->s_flags);
86227c478bd9Sstevel@tonic-gate 	if (error != 0) {
86237c478bd9Sstevel@tonic-gate 		umem_decr_devlockmem(p);
86247c478bd9Sstevel@tonic-gate 		kmem_free(p, sizeof (struct ddi_umem_cookie));
86257c478bd9Sstevel@tonic-gate 		*cookie = (ddi_umem_cookie_t)NULL;
86267c478bd9Sstevel@tonic-gate 		return (error);
86277c478bd9Sstevel@tonic-gate 	}
86287c478bd9Sstevel@tonic-gate 
86297c478bd9Sstevel@tonic-gate 	/*
86307c478bd9Sstevel@tonic-gate 	 * For longterm locking the addr must pertain to a seg_vn segment or
86317c478bd9Sstevel@tonic-gate 	 * or a seg_spt segment.
86327c478bd9Sstevel@tonic-gate 	 * If the segment pertains to a regular file, it cannot be
86337c478bd9Sstevel@tonic-gate 	 * mapped MAP_SHARED.
86347c478bd9Sstevel@tonic-gate 	 * This is to prevent a deadlock if a file truncation is attempted
86357c478bd9Sstevel@tonic-gate 	 * after the locking is done.
86367c478bd9Sstevel@tonic-gate 	 * Doing this after as_pagelock guarantees persistence of the as; if
86377c478bd9Sstevel@tonic-gate 	 * an unacceptable segment is found, the cleanup includes calling
86387c478bd9Sstevel@tonic-gate 	 * as_pageunlock before returning EFAULT.
8639d863b343SBill Taylor 	 *
8640d863b343SBill Taylor 	 * segdev is allowed here as it is already locked.  This allows
8641d863b343SBill Taylor 	 * for memory exported by drivers through mmap() (which is already
8642d863b343SBill Taylor 	 * locked) to be allowed for LONGTERM.
86437c478bd9Sstevel@tonic-gate 	 */
86447c478bd9Sstevel@tonic-gate 	if (flags & DDI_UMEMLOCK_LONGTERM) {
86457c478bd9Sstevel@tonic-gate 		extern  struct seg_ops segspt_shmops;
8646d863b343SBill Taylor 		extern	struct seg_ops segdev_ops;
86477c478bd9Sstevel@tonic-gate 		AS_LOCK_ENTER(as, &as->a_lock, RW_READER);
86487c478bd9Sstevel@tonic-gate 		for (seg = as_segat(as, addr); ; seg = AS_SEGNEXT(as, seg)) {
86497c478bd9Sstevel@tonic-gate 			if (seg == NULL || seg->s_base > addr + len)
86507c478bd9Sstevel@tonic-gate 				break;
8651d863b343SBill Taylor 			if (seg->s_ops == &segdev_ops)
8652d863b343SBill Taylor 				continue;
86537c478bd9Sstevel@tonic-gate 			if (((seg->s_ops != &segvn_ops) &&
86547c478bd9Sstevel@tonic-gate 			    (seg->s_ops != &segspt_shmops)) ||
86557c478bd9Sstevel@tonic-gate 			    ((SEGOP_GETVP(seg, addr, &vp) == 0 &&
86567c478bd9Sstevel@tonic-gate 			    vp != NULL && vp->v_type == VREG) &&
86577c478bd9Sstevel@tonic-gate 			    (SEGOP_GETTYPE(seg, addr) & MAP_SHARED))) {
86587c478bd9Sstevel@tonic-gate 				as_pageunlock(as, p->pparray,
8659184cd04cScth 				    addr, len, p->s_flags);
86607c478bd9Sstevel@tonic-gate 				AS_LOCK_EXIT(as, &as->a_lock);
86617c478bd9Sstevel@tonic-gate 				umem_decr_devlockmem(p);
86627c478bd9Sstevel@tonic-gate 				kmem_free(p, sizeof (struct ddi_umem_cookie));
86637c478bd9Sstevel@tonic-gate 				*cookie = (ddi_umem_cookie_t)NULL;
86647c478bd9Sstevel@tonic-gate 				return (EFAULT);
86657c478bd9Sstevel@tonic-gate 			}
86667c478bd9Sstevel@tonic-gate 		}
86677c478bd9Sstevel@tonic-gate 		AS_LOCK_EXIT(as, &as->a_lock);
86687c478bd9Sstevel@tonic-gate 	}
86697c478bd9Sstevel@tonic-gate 
86707c478bd9Sstevel@tonic-gate 
86717c478bd9Sstevel@tonic-gate 	/* Initialize the fields in the ddi_umem_cookie */
86727c478bd9Sstevel@tonic-gate 	p->cvaddr = addr;
86737c478bd9Sstevel@tonic-gate 	p->type = UMEM_LOCKED;
86747c478bd9Sstevel@tonic-gate 	if (driver_callback != NULL) {
86757c478bd9Sstevel@tonic-gate 		/* i_ddi_umem_unlock and umem_lock_undo may need the cookie */
86767c478bd9Sstevel@tonic-gate 		p->cook_refcnt = 2;
86777c478bd9Sstevel@tonic-gate 		p->callbacks = *ops_vector;
86787c478bd9Sstevel@tonic-gate 	} else {
86797c478bd9Sstevel@tonic-gate 		/* only i_ddi_umme_unlock needs the cookie */
86807c478bd9Sstevel@tonic-gate 		p->cook_refcnt = 1;
86817c478bd9Sstevel@tonic-gate 	}
86827c478bd9Sstevel@tonic-gate 
86837c478bd9Sstevel@tonic-gate 	*cookie = (ddi_umem_cookie_t)p;
86847c478bd9Sstevel@tonic-gate 
86857c478bd9Sstevel@tonic-gate 	/*
86867c478bd9Sstevel@tonic-gate 	 * If a driver callback was specified, add an entry to the
86877c478bd9Sstevel@tonic-gate 	 * as struct callback list. The as_pagelock above guarantees
86887c478bd9Sstevel@tonic-gate 	 * the persistence of as.
86897c478bd9Sstevel@tonic-gate 	 */
86907c478bd9Sstevel@tonic-gate 	if (driver_callback) {
86917c478bd9Sstevel@tonic-gate 		error = as_add_callback(as, umem_lock_undo, p, AS_ALL_EVENT,
8692184cd04cScth 		    addr, len, KM_SLEEP);
86937c478bd9Sstevel@tonic-gate 		if (error != 0) {
86947c478bd9Sstevel@tonic-gate 			as_pageunlock(as, p->pparray,
8695184cd04cScth 			    addr, len, p->s_flags);
86967c478bd9Sstevel@tonic-gate 			umem_decr_devlockmem(p);
86977c478bd9Sstevel@tonic-gate 			kmem_free(p, sizeof (struct ddi_umem_cookie));
86987c478bd9Sstevel@tonic-gate 			*cookie = (ddi_umem_cookie_t)NULL;
86997c478bd9Sstevel@tonic-gate 		}
87007c478bd9Sstevel@tonic-gate 	}
87017c478bd9Sstevel@tonic-gate 	return (error);
87027c478bd9Sstevel@tonic-gate }
87037c478bd9Sstevel@tonic-gate 
87047c478bd9Sstevel@tonic-gate /*
87057c478bd9Sstevel@tonic-gate  * Unlock the pages locked by ddi_umem_lock or umem_lockmemory and free
87067c478bd9Sstevel@tonic-gate  * the cookie.  Called from i_ddi_umem_unlock_thread.
87077c478bd9Sstevel@tonic-gate  */
87087c478bd9Sstevel@tonic-gate 
87097c478bd9Sstevel@tonic-gate static void
87107c478bd9Sstevel@tonic-gate i_ddi_umem_unlock(struct ddi_umem_cookie *p)
87117c478bd9Sstevel@tonic-gate {
87127c478bd9Sstevel@tonic-gate 	uint_t	rc;
87137c478bd9Sstevel@tonic-gate 
87147c478bd9Sstevel@tonic-gate 	/*
87157c478bd9Sstevel@tonic-gate 	 * There is no way to determine whether a callback to
87167c478bd9Sstevel@tonic-gate 	 * umem_lock_undo was registered via as_add_callback.
87177c478bd9Sstevel@tonic-gate 	 * (i.e. umem_lockmemory was called with DDI_MEMLOCK_LONGTERM and
87187c478bd9Sstevel@tonic-gate 	 * a valid callback function structure.)  as_delete_callback
87197c478bd9Sstevel@tonic-gate 	 * is called to delete a possible registered callback.  If the
87207c478bd9Sstevel@tonic-gate 	 * return from as_delete_callbacks is AS_CALLBACK_DELETED, it
87217c478bd9Sstevel@tonic-gate 	 * indicates that there was a callback registered, and that is was
87227c478bd9Sstevel@tonic-gate 	 * successfully deleted.  Thus, the cookie reference count
87237c478bd9Sstevel@tonic-gate 	 * will never be decremented by umem_lock_undo.  Just return the
87247c478bd9Sstevel@tonic-gate 	 * memory for the cookie, since both users of the cookie are done.
87257c478bd9Sstevel@tonic-gate 	 * A return of AS_CALLBACK_NOTFOUND indicates a callback was
87267c478bd9Sstevel@tonic-gate 	 * never registered.  A return of AS_CALLBACK_DELETE_DEFERRED
87277c478bd9Sstevel@tonic-gate 	 * indicates that callback processing is taking place and, and
87287c478bd9Sstevel@tonic-gate 	 * umem_lock_undo is, or will be, executing, and thus decrementing
87297c478bd9Sstevel@tonic-gate 	 * the cookie reference count when it is complete.
87307c478bd9Sstevel@tonic-gate 	 *
87317c478bd9Sstevel@tonic-gate 	 * This needs to be done before as_pageunlock so that the
87327c478bd9Sstevel@tonic-gate 	 * persistence of as is guaranteed because of the locked pages.
87337c478bd9Sstevel@tonic-gate 	 *
87347c478bd9Sstevel@tonic-gate 	 */
87357c478bd9Sstevel@tonic-gate 	rc = as_delete_callback(p->asp, p);
87367c478bd9Sstevel@tonic-gate 
87377c478bd9Sstevel@tonic-gate 
87387c478bd9Sstevel@tonic-gate 	/*
87397c478bd9Sstevel@tonic-gate 	 * The proc->p_as will be stale if i_ddi_umem_unlock is called
87407c478bd9Sstevel@tonic-gate 	 * after relvm is called so use p->asp.
87417c478bd9Sstevel@tonic-gate 	 */
87427c478bd9Sstevel@tonic-gate 	as_pageunlock(p->asp, p->pparray, p->cvaddr, p->size, p->s_flags);
87437c478bd9Sstevel@tonic-gate 
87447c478bd9Sstevel@tonic-gate 	/*
87457c478bd9Sstevel@tonic-gate 	 * Now that we have unlocked the memory decrement the
8746c6939658Ssl 	 * *.max-locked-memory rctl
87477c478bd9Sstevel@tonic-gate 	 */
87487c478bd9Sstevel@tonic-gate 	umem_decr_devlockmem(p);
87497c478bd9Sstevel@tonic-gate 
87507c478bd9Sstevel@tonic-gate 	if (rc == AS_CALLBACK_DELETED) {
87517c478bd9Sstevel@tonic-gate 		/* umem_lock_undo will not happen, return the cookie memory */
87527c478bd9Sstevel@tonic-gate 		ASSERT(p->cook_refcnt == 2);
87537c478bd9Sstevel@tonic-gate 		kmem_free(p, sizeof (struct ddi_umem_cookie));
87547c478bd9Sstevel@tonic-gate 	} else {
87557c478bd9Sstevel@tonic-gate 		/*
87567c478bd9Sstevel@tonic-gate 		 * umem_undo_lock may happen if as_delete_callback returned
87577c478bd9Sstevel@tonic-gate 		 * AS_CALLBACK_DELETE_DEFERRED.  In that case, decrement the
87587c478bd9Sstevel@tonic-gate 		 * reference count, atomically, and return the cookie
87597c478bd9Sstevel@tonic-gate 		 * memory if the reference count goes to zero.  The only
87607c478bd9Sstevel@tonic-gate 		 * other value for rc is AS_CALLBACK_NOTFOUND.  In that
87617c478bd9Sstevel@tonic-gate 		 * case, just return the cookie memory.
87627c478bd9Sstevel@tonic-gate 		 */
87637c478bd9Sstevel@tonic-gate 		if ((rc != AS_CALLBACK_DELETE_DEFERRED) ||
87647c478bd9Sstevel@tonic-gate 		    (atomic_add_long_nv((ulong_t *)(&(p->cook_refcnt)), -1)
87657c478bd9Sstevel@tonic-gate 		    == 0)) {
87667c478bd9Sstevel@tonic-gate 			kmem_free(p, sizeof (struct ddi_umem_cookie));
87677c478bd9Sstevel@tonic-gate 		}
87687c478bd9Sstevel@tonic-gate 	}
87697c478bd9Sstevel@tonic-gate }
87707c478bd9Sstevel@tonic-gate 
87717c478bd9Sstevel@tonic-gate /*
87727c478bd9Sstevel@tonic-gate  * i_ddi_umem_unlock_thread - deferred ddi_umem_unlock list handler.
87737c478bd9Sstevel@tonic-gate  *
87747c478bd9Sstevel@tonic-gate  * Call i_ddi_umem_unlock for entries in the ddi_umem_unlock list
87757c478bd9Sstevel@tonic-gate  * until it is empty.  Then, wait for more to be added.  This thread is awoken
87767c478bd9Sstevel@tonic-gate  * via calls to ddi_umem_unlock.
87777c478bd9Sstevel@tonic-gate  */
87787c478bd9Sstevel@tonic-gate 
87797c478bd9Sstevel@tonic-gate static void
87807c478bd9Sstevel@tonic-gate i_ddi_umem_unlock_thread(void)
87817c478bd9Sstevel@tonic-gate {
87827c478bd9Sstevel@tonic-gate 	struct ddi_umem_cookie	*ret_cookie;
87837c478bd9Sstevel@tonic-gate 	callb_cpr_t	cprinfo;
87847c478bd9Sstevel@tonic-gate 
87857c478bd9Sstevel@tonic-gate 	/* process the ddi_umem_unlock list */
87867c478bd9Sstevel@tonic-gate 	CALLB_CPR_INIT(&cprinfo, &ddi_umem_unlock_mutex,
87877c478bd9Sstevel@tonic-gate 	    callb_generic_cpr, "unlock_thread");
87887c478bd9Sstevel@tonic-gate 	for (;;) {
87897c478bd9Sstevel@tonic-gate 		mutex_enter(&ddi_umem_unlock_mutex);
87907c478bd9Sstevel@tonic-gate 		if (ddi_umem_unlock_head != NULL) {	/* list not empty */
87917c478bd9Sstevel@tonic-gate 			ret_cookie = ddi_umem_unlock_head;
87927c478bd9Sstevel@tonic-gate 			/* take if off the list */
87937c478bd9Sstevel@tonic-gate 			if ((ddi_umem_unlock_head =
87947c478bd9Sstevel@tonic-gate 			    ddi_umem_unlock_head->unl_forw) == NULL) {
87957c478bd9Sstevel@tonic-gate 				ddi_umem_unlock_tail = NULL;
87967c478bd9Sstevel@tonic-gate 			}
87977c478bd9Sstevel@tonic-gate 			mutex_exit(&ddi_umem_unlock_mutex);
87987c478bd9Sstevel@tonic-gate 			/* unlock the pages in this cookie */
87997c478bd9Sstevel@tonic-gate 			(void) i_ddi_umem_unlock(ret_cookie);
88007c478bd9Sstevel@tonic-gate 		} else {   /* list is empty, wait for next ddi_umem_unlock */
88017c478bd9Sstevel@tonic-gate 			CALLB_CPR_SAFE_BEGIN(&cprinfo);
88027c478bd9Sstevel@tonic-gate 			cv_wait(&ddi_umem_unlock_cv, &ddi_umem_unlock_mutex);
88037c478bd9Sstevel@tonic-gate 			CALLB_CPR_SAFE_END(&cprinfo, &ddi_umem_unlock_mutex);
88047c478bd9Sstevel@tonic-gate 			mutex_exit(&ddi_umem_unlock_mutex);
88057c478bd9Sstevel@tonic-gate 		}
88067c478bd9Sstevel@tonic-gate 	}
88077c478bd9Sstevel@tonic-gate 	/* ddi_umem_unlock_thread does not exit */
88087c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
88097c478bd9Sstevel@tonic-gate }
88107c478bd9Sstevel@tonic-gate 
88117c478bd9Sstevel@tonic-gate /*
88127c478bd9Sstevel@tonic-gate  * Start the thread that will process the ddi_umem_unlock list if it is
88137c478bd9Sstevel@tonic-gate  * not already started (i_ddi_umem_unlock_thread).
88147c478bd9Sstevel@tonic-gate  */
88157c478bd9Sstevel@tonic-gate static void
88167c478bd9Sstevel@tonic-gate i_ddi_umem_unlock_thread_start(void)
88177c478bd9Sstevel@tonic-gate {
88187c478bd9Sstevel@tonic-gate 	mutex_enter(&ddi_umem_unlock_mutex);
88197c478bd9Sstevel@tonic-gate 	if (ddi_umem_unlock_thread == NULL) {
88207c478bd9Sstevel@tonic-gate 		ddi_umem_unlock_thread = thread_create(NULL, 0,
88217c478bd9Sstevel@tonic-gate 		    i_ddi_umem_unlock_thread, NULL, 0, &p0,
88227c478bd9Sstevel@tonic-gate 		    TS_RUN, minclsyspri);
88237c478bd9Sstevel@tonic-gate 	}
88247c478bd9Sstevel@tonic-gate 	mutex_exit(&ddi_umem_unlock_mutex);
88257c478bd9Sstevel@tonic-gate }
88267c478bd9Sstevel@tonic-gate 
88277c478bd9Sstevel@tonic-gate /*
88287c478bd9Sstevel@tonic-gate  * Lock the virtual address range in the current process and create a
88297c478bd9Sstevel@tonic-gate  * ddi_umem_cookie (of type UMEM_LOCKED). This can be used to pass to
88307c478bd9Sstevel@tonic-gate  * ddi_umem_iosetup to create a buf or do devmap_umem_setup/remap to export
88317c478bd9Sstevel@tonic-gate  * to user space.
88327c478bd9Sstevel@tonic-gate  *
88337c478bd9Sstevel@tonic-gate  * Note: The resource control accounting currently uses a full charge model
88347c478bd9Sstevel@tonic-gate  * in other words attempts to lock the same/overlapping areas of memory
88357c478bd9Sstevel@tonic-gate  * will deduct the full size of the buffer from the projects running
88367c478bd9Sstevel@tonic-gate  * counter for the device locked memory. This applies to umem_lockmemory too.
88377c478bd9Sstevel@tonic-gate  *
88387c478bd9Sstevel@tonic-gate  * addr, size should be PAGESIZE aligned
88397c478bd9Sstevel@tonic-gate  * flags - DDI_UMEMLOCK_READ, DDI_UMEMLOCK_WRITE or both
88407c478bd9Sstevel@tonic-gate  *	identifies whether the locked memory will be read or written or both
88417c478bd9Sstevel@tonic-gate  *
88427c478bd9Sstevel@tonic-gate  * Returns 0 on success
88437c478bd9Sstevel@tonic-gate  *	EINVAL - for invalid parameters
88447c478bd9Sstevel@tonic-gate  *	EPERM, ENOMEM and other error codes returned by as_pagelock
88457c478bd9Sstevel@tonic-gate  *	ENOMEM - is returned if the current request to lock memory exceeds
8846c6939658Ssl  *		*.max-locked-memory resource control value.
88477c478bd9Sstevel@tonic-gate  *	EAGAIN - could not start the ddi_umem_unlock list processing thread
88487c478bd9Sstevel@tonic-gate  */
88497c478bd9Sstevel@tonic-gate int
88507c478bd9Sstevel@tonic-gate ddi_umem_lock(caddr_t addr, size_t len, int flags, ddi_umem_cookie_t *cookie)
88517c478bd9Sstevel@tonic-gate {
88527c478bd9Sstevel@tonic-gate 	int	error;
88537c478bd9Sstevel@tonic-gate 	struct ddi_umem_cookie *p;
88547c478bd9Sstevel@tonic-gate 
88557c478bd9Sstevel@tonic-gate 	*cookie = NULL;		/* in case of any error return */
88567c478bd9Sstevel@tonic-gate 
88577c478bd9Sstevel@tonic-gate 	/* These are the only two valid flags */
88587c478bd9Sstevel@tonic-gate 	if ((flags & ~(DDI_UMEMLOCK_READ | DDI_UMEMLOCK_WRITE)) != 0) {
88597c478bd9Sstevel@tonic-gate 		return (EINVAL);
88607c478bd9Sstevel@tonic-gate 	}
88617c478bd9Sstevel@tonic-gate 
88627c478bd9Sstevel@tonic-gate 	/* At least one of the two flags (or both) must be set */
88637c478bd9Sstevel@tonic-gate 	if ((flags & (DDI_UMEMLOCK_READ | DDI_UMEMLOCK_WRITE)) == 0) {
88647c478bd9Sstevel@tonic-gate 		return (EINVAL);
88657c478bd9Sstevel@tonic-gate 	}
88667c478bd9Sstevel@tonic-gate 
88677c478bd9Sstevel@tonic-gate 	/* addr and len must be page-aligned */
88687c478bd9Sstevel@tonic-gate 	if (((uintptr_t)addr & PAGEOFFSET) != 0) {
88697c478bd9Sstevel@tonic-gate 		return (EINVAL);
88707c478bd9Sstevel@tonic-gate 	}
88717c478bd9Sstevel@tonic-gate 
88727c478bd9Sstevel@tonic-gate 	if ((len & PAGEOFFSET) != 0) {
88737c478bd9Sstevel@tonic-gate 		return (EINVAL);
88747c478bd9Sstevel@tonic-gate 	}
88757c478bd9Sstevel@tonic-gate 
88767c478bd9Sstevel@tonic-gate 	/*
88777c478bd9Sstevel@tonic-gate 	 * Call i_ddi_umem_unlock_thread_start if necessary.  It will
88787c478bd9Sstevel@tonic-gate 	 * be called on first ddi_umem_lock or umem_lockmemory call.
88797c478bd9Sstevel@tonic-gate 	 */
88807c478bd9Sstevel@tonic-gate 	if (ddi_umem_unlock_thread == NULL)
88817c478bd9Sstevel@tonic-gate 		i_ddi_umem_unlock_thread_start();
88827c478bd9Sstevel@tonic-gate 
88837c478bd9Sstevel@tonic-gate 	/* Allocate memory for the cookie */
88847c478bd9Sstevel@tonic-gate 	p = kmem_zalloc(sizeof (struct ddi_umem_cookie), KM_SLEEP);
88857c478bd9Sstevel@tonic-gate 
88867c478bd9Sstevel@tonic-gate 	/* Convert the flags to seg_rw type */
88877c478bd9Sstevel@tonic-gate 	if (flags & DDI_UMEMLOCK_WRITE) {
88887c478bd9Sstevel@tonic-gate 		p->s_flags = S_WRITE;
88897c478bd9Sstevel@tonic-gate 	} else {
88907c478bd9Sstevel@tonic-gate 		p->s_flags = S_READ;
88917c478bd9Sstevel@tonic-gate 	}
88927c478bd9Sstevel@tonic-gate 
88937c478bd9Sstevel@tonic-gate 	/* Store curproc in cookie for later iosetup/unlock */
88947c478bd9Sstevel@tonic-gate 	p->procp = (void *)curproc;
88957c478bd9Sstevel@tonic-gate 
88967c478bd9Sstevel@tonic-gate 	/*
88977c478bd9Sstevel@tonic-gate 	 * Store the struct as pointer in cookie for later use by
88987c478bd9Sstevel@tonic-gate 	 * ddi_umem_unlock.  The proc->p_as will be stale if ddi_umem_unlock
88997c478bd9Sstevel@tonic-gate 	 * is called after relvm is called.
89007c478bd9Sstevel@tonic-gate 	 */
89017c478bd9Sstevel@tonic-gate 	p->asp = curproc->p_as;
89027c478bd9Sstevel@tonic-gate 	/*
89037c478bd9Sstevel@tonic-gate 	 * The size field is needed for lockmem accounting.
89047c478bd9Sstevel@tonic-gate 	 */
89057c478bd9Sstevel@tonic-gate 	p->size = len;
89067c478bd9Sstevel@tonic-gate 
89077c478bd9Sstevel@tonic-gate 	if (umem_incr_devlockmem(p) != 0) {
89087c478bd9Sstevel@tonic-gate 		/*
89097c478bd9Sstevel@tonic-gate 		 * The requested memory cannot be locked
89107c478bd9Sstevel@tonic-gate 		 */
89117c478bd9Sstevel@tonic-gate 		kmem_free(p, sizeof (struct ddi_umem_cookie));
89127c478bd9Sstevel@tonic-gate 		*cookie = (ddi_umem_cookie_t)NULL;
89137c478bd9Sstevel@tonic-gate 		return (ENOMEM);
89147c478bd9Sstevel@tonic-gate 	}
89157c478bd9Sstevel@tonic-gate 
89167c478bd9Sstevel@tonic-gate 	/* Lock the pages corresponding to addr, len in memory */
89177c478bd9Sstevel@tonic-gate 	error = as_pagelock(((proc_t *)p->procp)->p_as, &(p->pparray),
89187c478bd9Sstevel@tonic-gate 	    addr, len, p->s_flags);
89197c478bd9Sstevel@tonic-gate 	if (error != 0) {
89207c478bd9Sstevel@tonic-gate 		umem_decr_devlockmem(p);
89217c478bd9Sstevel@tonic-gate 		kmem_free(p, sizeof (struct ddi_umem_cookie));
89227c478bd9Sstevel@tonic-gate 		*cookie = (ddi_umem_cookie_t)NULL;
89237c478bd9Sstevel@tonic-gate 		return (error);
89247c478bd9Sstevel@tonic-gate 	}
89257c478bd9Sstevel@tonic-gate 
89267c478bd9Sstevel@tonic-gate 	/* Initialize the fields in the ddi_umem_cookie */
89277c478bd9Sstevel@tonic-gate 	p->cvaddr = addr;
89287c478bd9Sstevel@tonic-gate 	p->type = UMEM_LOCKED;
89297c478bd9Sstevel@tonic-gate 	p->cook_refcnt = 1;
89307c478bd9Sstevel@tonic-gate 
89317c478bd9Sstevel@tonic-gate 	*cookie = (ddi_umem_cookie_t)p;
89327c478bd9Sstevel@tonic-gate 	return (error);
89337c478bd9Sstevel@tonic-gate }
89347c478bd9Sstevel@tonic-gate 
89357c478bd9Sstevel@tonic-gate /*
89367c478bd9Sstevel@tonic-gate  * Add the cookie to the ddi_umem_unlock list.  Pages will be
89377c478bd9Sstevel@tonic-gate  * unlocked by i_ddi_umem_unlock_thread.
89387c478bd9Sstevel@tonic-gate  */
89397c478bd9Sstevel@tonic-gate 
89407c478bd9Sstevel@tonic-gate void
89417c478bd9Sstevel@tonic-gate ddi_umem_unlock(ddi_umem_cookie_t cookie)
89427c478bd9Sstevel@tonic-gate {
89437c478bd9Sstevel@tonic-gate 	struct ddi_umem_cookie	*p = (struct ddi_umem_cookie *)cookie;
89447c478bd9Sstevel@tonic-gate 
89457c478bd9Sstevel@tonic-gate 	ASSERT(p->type == UMEM_LOCKED);
89467c478bd9Sstevel@tonic-gate 	ASSERT(CPU_ON_INTR(CPU) == 0); /* cannot be high level */
89477c478bd9Sstevel@tonic-gate 	ASSERT(ddi_umem_unlock_thread != NULL);
89487c478bd9Sstevel@tonic-gate 
89497c478bd9Sstevel@tonic-gate 	p->unl_forw = (struct ddi_umem_cookie *)NULL;	/* end of list */
89509bbaf432Seota 	/*
89519bbaf432Seota 	 * Queue the unlock request and notify i_ddi_umem_unlock thread
89529bbaf432Seota 	 * if it's called in the interrupt context. Otherwise, unlock pages
89539bbaf432Seota 	 * immediately.
89549bbaf432Seota 	 */
89559bbaf432Seota 	if (servicing_interrupt()) {
89569bbaf432Seota 		/* queue the unlock request and notify the thread */
89579bbaf432Seota 		mutex_enter(&ddi_umem_unlock_mutex);
89589bbaf432Seota 		if (ddi_umem_unlock_head == NULL) {
89599bbaf432Seota 			ddi_umem_unlock_head = ddi_umem_unlock_tail = p;
89609bbaf432Seota 			cv_broadcast(&ddi_umem_unlock_cv);
89619bbaf432Seota 		} else {
89629bbaf432Seota 			ddi_umem_unlock_tail->unl_forw = p;
89639bbaf432Seota 			ddi_umem_unlock_tail = p;
89649bbaf432Seota 		}
89659bbaf432Seota 		mutex_exit(&ddi_umem_unlock_mutex);
89667c478bd9Sstevel@tonic-gate 	} else {
89679bbaf432Seota 		/* unlock the pages right away */
89689bbaf432Seota 		(void) i_ddi_umem_unlock(p);
89697c478bd9Sstevel@tonic-gate 	}
89707c478bd9Sstevel@tonic-gate }
89717c478bd9Sstevel@tonic-gate 
89727c478bd9Sstevel@tonic-gate /*
89737c478bd9Sstevel@tonic-gate  * Create a buf structure from a ddi_umem_cookie
89747c478bd9Sstevel@tonic-gate  * cookie - is a ddi_umem_cookie for from ddi_umem_lock and ddi_umem_alloc
89757c478bd9Sstevel@tonic-gate  *		(only UMEM_LOCKED & KMEM_NON_PAGEABLE types supported)
89767c478bd9Sstevel@tonic-gate  * off, len - identifies the portion of the memory represented by the cookie
89777c478bd9Sstevel@tonic-gate  *		that the buf points to.
89787c478bd9Sstevel@tonic-gate  *	NOTE: off, len need to follow the alignment/size restrictions of the
89797c478bd9Sstevel@tonic-gate  *		device (dev) that this buf will be passed to. Some devices
89807c478bd9Sstevel@tonic-gate  *		will accept unrestricted alignment/size, whereas others (such as
89817c478bd9Sstevel@tonic-gate  *		st) require some block-size alignment/size. It is the caller's
89827c478bd9Sstevel@tonic-gate  *		responsibility to ensure that the alignment/size restrictions
89837c478bd9Sstevel@tonic-gate  *		are met (we cannot assert as we do not know the restrictions)
89847c478bd9Sstevel@tonic-gate  *
89857c478bd9Sstevel@tonic-gate  * direction - is one of B_READ or B_WRITE and needs to be compatible with
89867c478bd9Sstevel@tonic-gate  *		the flags used in ddi_umem_lock
89877c478bd9Sstevel@tonic-gate  *
89887c478bd9Sstevel@tonic-gate  * The following three arguments are used to initialize fields in the
89897c478bd9Sstevel@tonic-gate  * buf structure and are uninterpreted by this routine.
89907c478bd9Sstevel@tonic-gate  *
89917c478bd9Sstevel@tonic-gate  * dev
89927c478bd9Sstevel@tonic-gate  * blkno
89937c478bd9Sstevel@tonic-gate  * iodone
89947c478bd9Sstevel@tonic-gate  *
89957c478bd9Sstevel@tonic-gate  * sleepflag - is one of DDI_UMEM_SLEEP or DDI_UMEM_NOSLEEP
89967c478bd9Sstevel@tonic-gate  *
89977c478bd9Sstevel@tonic-gate  * Returns a buf structure pointer on success (to be freed by freerbuf)
89987c478bd9Sstevel@tonic-gate  *	NULL on any parameter error or memory alloc failure
89997c478bd9Sstevel@tonic-gate  *
90007c478bd9Sstevel@tonic-gate  */
90017c478bd9Sstevel@tonic-gate struct buf *
90027c478bd9Sstevel@tonic-gate ddi_umem_iosetup(ddi_umem_cookie_t cookie, off_t off, size_t len,
90037c478bd9Sstevel@tonic-gate 	int direction, dev_t dev, daddr_t blkno,
90047c478bd9Sstevel@tonic-gate 	int (*iodone)(struct buf *), int sleepflag)
90057c478bd9Sstevel@tonic-gate {
90067c478bd9Sstevel@tonic-gate 	struct ddi_umem_cookie *p = (struct ddi_umem_cookie *)cookie;
90077c478bd9Sstevel@tonic-gate 	struct buf *bp;
90087c478bd9Sstevel@tonic-gate 
90097c478bd9Sstevel@tonic-gate 	/*
90107c478bd9Sstevel@tonic-gate 	 * check for valid cookie offset, len
90117c478bd9Sstevel@tonic-gate 	 */
90127c478bd9Sstevel@tonic-gate 	if ((off + len) > p->size) {
90137c478bd9Sstevel@tonic-gate 		return (NULL);
90147c478bd9Sstevel@tonic-gate 	}
90157c478bd9Sstevel@tonic-gate 
90167c478bd9Sstevel@tonic-gate 	if (len > p->size) {
90177c478bd9Sstevel@tonic-gate 		return (NULL);
90187c478bd9Sstevel@tonic-gate 	}
90197c478bd9Sstevel@tonic-gate 
90207c478bd9Sstevel@tonic-gate 	/* direction has to be one of B_READ or B_WRITE */
90217c478bd9Sstevel@tonic-gate 	if ((direction != B_READ) && (direction != B_WRITE)) {
90227c478bd9Sstevel@tonic-gate 		return (NULL);
90237c478bd9Sstevel@tonic-gate 	}
90247c478bd9Sstevel@tonic-gate 
90257c478bd9Sstevel@tonic-gate 	/* These are the only two valid sleepflags */
90267c478bd9Sstevel@tonic-gate 	if ((sleepflag != DDI_UMEM_SLEEP) && (sleepflag != DDI_UMEM_NOSLEEP)) {
90277c478bd9Sstevel@tonic-gate 		return (NULL);
90287c478bd9Sstevel@tonic-gate 	}
90297c478bd9Sstevel@tonic-gate 
90307c478bd9Sstevel@tonic-gate 	/*
90317c478bd9Sstevel@tonic-gate 	 * Only cookies of type UMEM_LOCKED and KMEM_NON_PAGEABLE are supported
90327c478bd9Sstevel@tonic-gate 	 */
90337c478bd9Sstevel@tonic-gate 	if ((p->type != UMEM_LOCKED) && (p->type != KMEM_NON_PAGEABLE)) {
90347c478bd9Sstevel@tonic-gate 		return (NULL);
90357c478bd9Sstevel@tonic-gate 	}
90367c478bd9Sstevel@tonic-gate 
90377c478bd9Sstevel@tonic-gate 	/* If type is KMEM_NON_PAGEABLE procp is NULL */
90387c478bd9Sstevel@tonic-gate 	ASSERT((p->type == KMEM_NON_PAGEABLE) ?
9039184cd04cScth 	    (p->procp == NULL) : (p->procp != NULL));
90407c478bd9Sstevel@tonic-gate 
90417c478bd9Sstevel@tonic-gate 	bp = kmem_alloc(sizeof (struct buf), sleepflag);
90427c478bd9Sstevel@tonic-gate 	if (bp == NULL) {
90437c478bd9Sstevel@tonic-gate 		return (NULL);
90447c478bd9Sstevel@tonic-gate 	}
90457c478bd9Sstevel@tonic-gate 	bioinit(bp);
90467c478bd9Sstevel@tonic-gate 
90477c478bd9Sstevel@tonic-gate 	bp->b_flags = B_BUSY | B_PHYS | direction;
90487c478bd9Sstevel@tonic-gate 	bp->b_edev = dev;
90497c478bd9Sstevel@tonic-gate 	bp->b_lblkno = blkno;
90507c478bd9Sstevel@tonic-gate 	bp->b_iodone = iodone;
90517c478bd9Sstevel@tonic-gate 	bp->b_bcount = len;
90527c478bd9Sstevel@tonic-gate 	bp->b_proc = (proc_t *)p->procp;
90537c478bd9Sstevel@tonic-gate 	ASSERT(((uintptr_t)(p->cvaddr) & PAGEOFFSET) == 0);
90547c478bd9Sstevel@tonic-gate 	bp->b_un.b_addr = (caddr_t)((uintptr_t)(p->cvaddr) + off);
90557c478bd9Sstevel@tonic-gate 	if (p->pparray != NULL) {
90567c478bd9Sstevel@tonic-gate 		bp->b_flags |= B_SHADOW;
90577c478bd9Sstevel@tonic-gate 		ASSERT(((uintptr_t)(p->cvaddr) & PAGEOFFSET) == 0);
90587c478bd9Sstevel@tonic-gate 		bp->b_shadow = p->pparray + btop(off);
90597c478bd9Sstevel@tonic-gate 	}
90607c478bd9Sstevel@tonic-gate 	return (bp);
90617c478bd9Sstevel@tonic-gate }
90627c478bd9Sstevel@tonic-gate 
90637c478bd9Sstevel@tonic-gate /*
90647c478bd9Sstevel@tonic-gate  * Fault-handling and related routines
90657c478bd9Sstevel@tonic-gate  */
90667c478bd9Sstevel@tonic-gate 
90677c478bd9Sstevel@tonic-gate ddi_devstate_t
90687c478bd9Sstevel@tonic-gate ddi_get_devstate(dev_info_t *dip)
90697c478bd9Sstevel@tonic-gate {
90707c478bd9Sstevel@tonic-gate 	if (DEVI_IS_DEVICE_OFFLINE(dip))
90717c478bd9Sstevel@tonic-gate 		return (DDI_DEVSTATE_OFFLINE);
90727c478bd9Sstevel@tonic-gate 	else if (DEVI_IS_DEVICE_DOWN(dip) || DEVI_IS_BUS_DOWN(dip))
90737c478bd9Sstevel@tonic-gate 		return (DDI_DEVSTATE_DOWN);
90747c478bd9Sstevel@tonic-gate 	else if (DEVI_IS_BUS_QUIESCED(dip))
90757c478bd9Sstevel@tonic-gate 		return (DDI_DEVSTATE_QUIESCED);
90767c478bd9Sstevel@tonic-gate 	else if (DEVI_IS_DEVICE_DEGRADED(dip))
90777c478bd9Sstevel@tonic-gate 		return (DDI_DEVSTATE_DEGRADED);
90787c478bd9Sstevel@tonic-gate 	else
90797c478bd9Sstevel@tonic-gate 		return (DDI_DEVSTATE_UP);
90807c478bd9Sstevel@tonic-gate }
90817c478bd9Sstevel@tonic-gate 
90827c478bd9Sstevel@tonic-gate void
90837c478bd9Sstevel@tonic-gate ddi_dev_report_fault(dev_info_t *dip, ddi_fault_impact_t impact,
90847c478bd9Sstevel@tonic-gate 	ddi_fault_location_t location, const char *message)
90857c478bd9Sstevel@tonic-gate {
90867c478bd9Sstevel@tonic-gate 	struct ddi_fault_event_data fd;
90877c478bd9Sstevel@tonic-gate 	ddi_eventcookie_t ec;
90887c478bd9Sstevel@tonic-gate 
90897c478bd9Sstevel@tonic-gate 	/*
90907c478bd9Sstevel@tonic-gate 	 * Assemble all the information into a fault-event-data structure
90917c478bd9Sstevel@tonic-gate 	 */
90927c478bd9Sstevel@tonic-gate 	fd.f_dip = dip;
90937c478bd9Sstevel@tonic-gate 	fd.f_impact = impact;
90947c478bd9Sstevel@tonic-gate 	fd.f_location = location;
90957c478bd9Sstevel@tonic-gate 	fd.f_message = message;
90967c478bd9Sstevel@tonic-gate 	fd.f_oldstate = ddi_get_devstate(dip);
90977c478bd9Sstevel@tonic-gate 
90987c478bd9Sstevel@tonic-gate 	/*
90997c478bd9Sstevel@tonic-gate 	 * Get eventcookie from defining parent.
91007c478bd9Sstevel@tonic-gate 	 */
91017c478bd9Sstevel@tonic-gate 	if (ddi_get_eventcookie(dip, DDI_DEVI_FAULT_EVENT, &ec) !=
91027c478bd9Sstevel@tonic-gate 	    DDI_SUCCESS)
91037c478bd9Sstevel@tonic-gate 		return;
91047c478bd9Sstevel@tonic-gate 
91057c478bd9Sstevel@tonic-gate 	(void) ndi_post_event(dip, dip, ec, &fd);
91067c478bd9Sstevel@tonic-gate }
91077c478bd9Sstevel@tonic-gate 
91087c478bd9Sstevel@tonic-gate char *
91097c478bd9Sstevel@tonic-gate i_ddi_devi_class(dev_info_t *dip)
91107c478bd9Sstevel@tonic-gate {
91117c478bd9Sstevel@tonic-gate 	return (DEVI(dip)->devi_device_class);
91127c478bd9Sstevel@tonic-gate }
91137c478bd9Sstevel@tonic-gate 
91147c478bd9Sstevel@tonic-gate int
91157c478bd9Sstevel@tonic-gate i_ddi_set_devi_class(dev_info_t *dip, char *devi_class, int flag)
91167c478bd9Sstevel@tonic-gate {
91177c478bd9Sstevel@tonic-gate 	struct dev_info *devi = DEVI(dip);
91187c478bd9Sstevel@tonic-gate 
91197c478bd9Sstevel@tonic-gate 	mutex_enter(&devi->devi_lock);
91207c478bd9Sstevel@tonic-gate 
91217c478bd9Sstevel@tonic-gate 	if (devi->devi_device_class)
91227c478bd9Sstevel@tonic-gate 		kmem_free(devi->devi_device_class,
91237c478bd9Sstevel@tonic-gate 		    strlen(devi->devi_device_class) + 1);
91247c478bd9Sstevel@tonic-gate 
91257c478bd9Sstevel@tonic-gate 	if ((devi->devi_device_class = i_ddi_strdup(devi_class, flag))
91267c478bd9Sstevel@tonic-gate 	    != NULL) {
91277c478bd9Sstevel@tonic-gate 		mutex_exit(&devi->devi_lock);
91287c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
91297c478bd9Sstevel@tonic-gate 	}
91307c478bd9Sstevel@tonic-gate 
91317c478bd9Sstevel@tonic-gate 	mutex_exit(&devi->devi_lock);
91327c478bd9Sstevel@tonic-gate 
91337c478bd9Sstevel@tonic-gate 	return (DDI_FAILURE);
91347c478bd9Sstevel@tonic-gate }
91357c478bd9Sstevel@tonic-gate 
91367c478bd9Sstevel@tonic-gate 
91377c478bd9Sstevel@tonic-gate /*
91387c478bd9Sstevel@tonic-gate  * Task Queues DDI interfaces.
91397c478bd9Sstevel@tonic-gate  */
91407c478bd9Sstevel@tonic-gate 
91417c478bd9Sstevel@tonic-gate /* ARGSUSED */
91427c478bd9Sstevel@tonic-gate ddi_taskq_t *
91437c478bd9Sstevel@tonic-gate ddi_taskq_create(dev_info_t *dip, const char *name, int nthreads,
91447c478bd9Sstevel@tonic-gate     pri_t pri, uint_t cflags)
91457c478bd9Sstevel@tonic-gate {
91467c478bd9Sstevel@tonic-gate 	char full_name[TASKQ_NAMELEN];
91477c478bd9Sstevel@tonic-gate 	const char *tq_name;
91487c478bd9Sstevel@tonic-gate 	int nodeid = 0;
91497c478bd9Sstevel@tonic-gate 
91507c478bd9Sstevel@tonic-gate 	if (dip == NULL)
91517c478bd9Sstevel@tonic-gate 		tq_name = name;
91527c478bd9Sstevel@tonic-gate 	else {
91537c478bd9Sstevel@tonic-gate 		nodeid = ddi_get_instance(dip);
91547c478bd9Sstevel@tonic-gate 
91557c478bd9Sstevel@tonic-gate 		if (name == NULL)
91567c478bd9Sstevel@tonic-gate 			name = "tq";
91577c478bd9Sstevel@tonic-gate 
91587c478bd9Sstevel@tonic-gate 		(void) snprintf(full_name, sizeof (full_name), "%s_%s",
91597c478bd9Sstevel@tonic-gate 		    ddi_driver_name(dip), name);
91607c478bd9Sstevel@tonic-gate 
91617c478bd9Sstevel@tonic-gate 		tq_name = full_name;
91627c478bd9Sstevel@tonic-gate 	}
91637c478bd9Sstevel@tonic-gate 
91647c478bd9Sstevel@tonic-gate 	return ((ddi_taskq_t *)taskq_create_instance(tq_name, nodeid, nthreads,
9165184cd04cScth 	    pri == TASKQ_DEFAULTPRI ? minclsyspri : pri,
9166184cd04cScth 	    nthreads, INT_MAX, TASKQ_PREPOPULATE));
91677c478bd9Sstevel@tonic-gate }
91687c478bd9Sstevel@tonic-gate 
91697c478bd9Sstevel@tonic-gate void
91707c478bd9Sstevel@tonic-gate ddi_taskq_destroy(ddi_taskq_t *tq)
91717c478bd9Sstevel@tonic-gate {
91727c478bd9Sstevel@tonic-gate 	taskq_destroy((taskq_t *)tq);
91737c478bd9Sstevel@tonic-gate }
91747c478bd9Sstevel@tonic-gate 
91757c478bd9Sstevel@tonic-gate int
91767c478bd9Sstevel@tonic-gate ddi_taskq_dispatch(ddi_taskq_t *tq, void (* func)(void *),
91777c478bd9Sstevel@tonic-gate     void *arg, uint_t dflags)
91787c478bd9Sstevel@tonic-gate {
91797c478bd9Sstevel@tonic-gate 	taskqid_t id = taskq_dispatch((taskq_t *)tq, func, arg,
91807c478bd9Sstevel@tonic-gate 	    dflags == DDI_SLEEP ? TQ_SLEEP : TQ_NOSLEEP);
91817c478bd9Sstevel@tonic-gate 
91827c478bd9Sstevel@tonic-gate 	return (id != 0 ? DDI_SUCCESS : DDI_FAILURE);
91837c478bd9Sstevel@tonic-gate }
91847c478bd9Sstevel@tonic-gate 
91857c478bd9Sstevel@tonic-gate void
91867c478bd9Sstevel@tonic-gate ddi_taskq_wait(ddi_taskq_t *tq)
91877c478bd9Sstevel@tonic-gate {
91887c478bd9Sstevel@tonic-gate 	taskq_wait((taskq_t *)tq);
91897c478bd9Sstevel@tonic-gate }
91907c478bd9Sstevel@tonic-gate 
91917c478bd9Sstevel@tonic-gate void
91927c478bd9Sstevel@tonic-gate ddi_taskq_suspend(ddi_taskq_t *tq)
91937c478bd9Sstevel@tonic-gate {
91947c478bd9Sstevel@tonic-gate 	taskq_suspend((taskq_t *)tq);
91957c478bd9Sstevel@tonic-gate }
91967c478bd9Sstevel@tonic-gate 
91977c478bd9Sstevel@tonic-gate boolean_t
91987c478bd9Sstevel@tonic-gate ddi_taskq_suspended(ddi_taskq_t *tq)
91997c478bd9Sstevel@tonic-gate {
92007c478bd9Sstevel@tonic-gate 	return (taskq_suspended((taskq_t *)tq));
92017c478bd9Sstevel@tonic-gate }
92027c478bd9Sstevel@tonic-gate 
92037c478bd9Sstevel@tonic-gate void
92047c478bd9Sstevel@tonic-gate ddi_taskq_resume(ddi_taskq_t *tq)
92057c478bd9Sstevel@tonic-gate {
92067c478bd9Sstevel@tonic-gate 	taskq_resume((taskq_t *)tq);
92077c478bd9Sstevel@tonic-gate }
92087c478bd9Sstevel@tonic-gate 
92097c478bd9Sstevel@tonic-gate int
92107c478bd9Sstevel@tonic-gate ddi_parse(
92117c478bd9Sstevel@tonic-gate 	const char	*ifname,
92127c478bd9Sstevel@tonic-gate 	char		*alnum,
92137c478bd9Sstevel@tonic-gate 	uint_t		*nump)
92147c478bd9Sstevel@tonic-gate {
92157c478bd9Sstevel@tonic-gate 	const char	*p;
92167c478bd9Sstevel@tonic-gate 	int		l;
92177c478bd9Sstevel@tonic-gate 	ulong_t		num;
92187c478bd9Sstevel@tonic-gate 	boolean_t	nonum = B_TRUE;
92197c478bd9Sstevel@tonic-gate 	char		c;
92207c478bd9Sstevel@tonic-gate 
92217c478bd9Sstevel@tonic-gate 	l = strlen(ifname);
92227c478bd9Sstevel@tonic-gate 	for (p = ifname + l; p != ifname; l--) {
92237c478bd9Sstevel@tonic-gate 		c = *--p;
92247c478bd9Sstevel@tonic-gate 		if (!isdigit(c)) {
92257c478bd9Sstevel@tonic-gate 			(void) strlcpy(alnum, ifname, l + 1);
92267c478bd9Sstevel@tonic-gate 			if (ddi_strtoul(p + 1, NULL, 10, &num) != 0)
92277c478bd9Sstevel@tonic-gate 				return (DDI_FAILURE);
92287c478bd9Sstevel@tonic-gate 			break;
92297c478bd9Sstevel@tonic-gate 		}
92307c478bd9Sstevel@tonic-gate 		nonum = B_FALSE;
92317c478bd9Sstevel@tonic-gate 	}
92327c478bd9Sstevel@tonic-gate 	if (l == 0 || nonum)
92337c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
92347c478bd9Sstevel@tonic-gate 
92357c478bd9Sstevel@tonic-gate 	*nump = num;
92367c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
92377c478bd9Sstevel@tonic-gate }
923819397407SSherry Moore 
923919397407SSherry Moore /*
924019397407SSherry Moore  * Default initialization function for drivers that don't need to quiesce.
924119397407SSherry Moore  */
924219397407SSherry Moore /* ARGSUSED */
924319397407SSherry Moore int
924419397407SSherry Moore ddi_quiesce_not_needed(dev_info_t *dip)
924519397407SSherry Moore {
924619397407SSherry Moore 	return (DDI_SUCCESS);
924719397407SSherry Moore }
924819397407SSherry Moore 
924919397407SSherry Moore /*
925019397407SSherry Moore  * Initialization function for drivers that should implement quiesce()
925119397407SSherry Moore  * but haven't yet.
925219397407SSherry Moore  */
925319397407SSherry Moore /* ARGSUSED */
925419397407SSherry Moore int
925519397407SSherry Moore ddi_quiesce_not_supported(dev_info_t *dip)
925619397407SSherry Moore {
925719397407SSherry Moore 	return (DDI_FAILURE);
925819397407SSherry Moore }
92595febcb4aSScott Carter, SD IOSW 
92607f0b8309SEdward Pilatowicz char *
92617f0b8309SEdward Pilatowicz ddi_strdup(const char *str, int flag)
92627f0b8309SEdward Pilatowicz {
92637f0b8309SEdward Pilatowicz 	int	n;
92647f0b8309SEdward Pilatowicz 	char	*ptr;
92657f0b8309SEdward Pilatowicz 
92667f0b8309SEdward Pilatowicz 	ASSERT(str != NULL);
92677f0b8309SEdward Pilatowicz 	ASSERT((flag == KM_SLEEP) || (flag == KM_NOSLEEP));
92687f0b8309SEdward Pilatowicz 
92697f0b8309SEdward Pilatowicz 	n = strlen(str);
92707f0b8309SEdward Pilatowicz 	if ((ptr = kmem_alloc(n + 1, flag)) == NULL)
92717f0b8309SEdward Pilatowicz 		return (NULL);
92727f0b8309SEdward Pilatowicz 	bcopy(str, ptr, n + 1);
92737f0b8309SEdward Pilatowicz 	return (ptr);
92747f0b8309SEdward Pilatowicz }
92757f0b8309SEdward Pilatowicz 
92767f0b8309SEdward Pilatowicz char *
92777f0b8309SEdward Pilatowicz strdup(const char *str)
92787f0b8309SEdward Pilatowicz {
92797f0b8309SEdward Pilatowicz 	return (ddi_strdup(str, KM_SLEEP));
92807f0b8309SEdward Pilatowicz }
92817f0b8309SEdward Pilatowicz 
92827f0b8309SEdward Pilatowicz void
92837f0b8309SEdward Pilatowicz strfree(char *str)
92847f0b8309SEdward Pilatowicz {
92857f0b8309SEdward Pilatowicz 	ASSERT(str != NULL);
92867f0b8309SEdward Pilatowicz 	kmem_free(str, strlen(str) + 1);
92877f0b8309SEdward Pilatowicz }
92887f0b8309SEdward Pilatowicz 
92895febcb4aSScott Carter, SD IOSW /*
92905febcb4aSScott Carter, SD IOSW  * Generic DDI callback interfaces.
92915febcb4aSScott Carter, SD IOSW  */
92925febcb4aSScott Carter, SD IOSW 
92935febcb4aSScott Carter, SD IOSW int
92945febcb4aSScott Carter, SD IOSW ddi_cb_register(dev_info_t *dip, ddi_cb_flags_t flags, ddi_cb_func_t cbfunc,
92955febcb4aSScott Carter, SD IOSW     void *arg1, void *arg2, ddi_cb_handle_t *ret_hdlp)
92965febcb4aSScott Carter, SD IOSW {
92975febcb4aSScott Carter, SD IOSW 	ddi_cb_t	*cbp;
92985febcb4aSScott Carter, SD IOSW 
92995febcb4aSScott Carter, SD IOSW 	ASSERT(dip != NULL);
93005febcb4aSScott Carter, SD IOSW 	ASSERT(DDI_CB_FLAG_VALID(flags));
93015febcb4aSScott Carter, SD IOSW 	ASSERT(cbfunc != NULL);
93025febcb4aSScott Carter, SD IOSW 	ASSERT(ret_hdlp != NULL);
93035febcb4aSScott Carter, SD IOSW 
93045febcb4aSScott Carter, SD IOSW 	/* Sanity check the context */
93055febcb4aSScott Carter, SD IOSW 	ASSERT(!servicing_interrupt());
93065febcb4aSScott Carter, SD IOSW 	if (servicing_interrupt())
93075febcb4aSScott Carter, SD IOSW 		return (DDI_FAILURE);
93085febcb4aSScott Carter, SD IOSW 
93095febcb4aSScott Carter, SD IOSW 	/* Validate parameters */
93105febcb4aSScott Carter, SD IOSW 	if ((dip == NULL) || !DDI_CB_FLAG_VALID(flags) ||
93115febcb4aSScott Carter, SD IOSW 	    (cbfunc == NULL) || (ret_hdlp == NULL))
93125febcb4aSScott Carter, SD IOSW 		return (DDI_EINVAL);
93135febcb4aSScott Carter, SD IOSW 
93145febcb4aSScott Carter, SD IOSW 	/* Check for previous registration */
93155febcb4aSScott Carter, SD IOSW 	if (DEVI(dip)->devi_cb_p != NULL)
93165febcb4aSScott Carter, SD IOSW 		return (DDI_EALREADY);
93175febcb4aSScott Carter, SD IOSW 
93185febcb4aSScott Carter, SD IOSW 	/* Allocate and initialize callback */
93195febcb4aSScott Carter, SD IOSW 	cbp = kmem_zalloc(sizeof (ddi_cb_t), KM_SLEEP);
93205febcb4aSScott Carter, SD IOSW 	cbp->cb_dip = dip;
93215febcb4aSScott Carter, SD IOSW 	cbp->cb_func = cbfunc;
93225febcb4aSScott Carter, SD IOSW 	cbp->cb_arg1 = arg1;
93235febcb4aSScott Carter, SD IOSW 	cbp->cb_arg2 = arg2;
93245febcb4aSScott Carter, SD IOSW 	cbp->cb_flags = flags;
93255febcb4aSScott Carter, SD IOSW 	DEVI(dip)->devi_cb_p = cbp;
93265febcb4aSScott Carter, SD IOSW 
93275febcb4aSScott Carter, SD IOSW 	/* If adding an IRM callback, notify IRM */
93285febcb4aSScott Carter, SD IOSW 	if (flags & DDI_CB_FLAG_INTR)
93295febcb4aSScott Carter, SD IOSW 		i_ddi_irm_set_cb(dip, B_TRUE);
93305febcb4aSScott Carter, SD IOSW 
93315febcb4aSScott Carter, SD IOSW 	*ret_hdlp = (ddi_cb_handle_t)&(DEVI(dip)->devi_cb_p);
93325febcb4aSScott Carter, SD IOSW 	return (DDI_SUCCESS);
93335febcb4aSScott Carter, SD IOSW }
93345febcb4aSScott Carter, SD IOSW 
93355febcb4aSScott Carter, SD IOSW int
93365febcb4aSScott Carter, SD IOSW ddi_cb_unregister(ddi_cb_handle_t hdl)
93375febcb4aSScott Carter, SD IOSW {
93385febcb4aSScott Carter, SD IOSW 	ddi_cb_t	*cbp;
93395febcb4aSScott Carter, SD IOSW 	dev_info_t	*dip;
93405febcb4aSScott Carter, SD IOSW 
93415febcb4aSScott Carter, SD IOSW 	ASSERT(hdl != NULL);
93425febcb4aSScott Carter, SD IOSW 
93435febcb4aSScott Carter, SD IOSW 	/* Sanity check the context */
93445febcb4aSScott Carter, SD IOSW 	ASSERT(!servicing_interrupt());
93455febcb4aSScott Carter, SD IOSW 	if (servicing_interrupt())
93465febcb4aSScott Carter, SD IOSW 		return (DDI_FAILURE);
93475febcb4aSScott Carter, SD IOSW 
93485febcb4aSScott Carter, SD IOSW 	/* Validate parameters */
93495febcb4aSScott Carter, SD IOSW 	if ((hdl == NULL) || ((cbp = *(ddi_cb_t **)hdl) == NULL) ||
93505febcb4aSScott Carter, SD IOSW 	    ((dip = cbp->cb_dip) == NULL))
93515febcb4aSScott Carter, SD IOSW 		return (DDI_EINVAL);
93525febcb4aSScott Carter, SD IOSW 
93535febcb4aSScott Carter, SD IOSW 	/* If removing an IRM callback, notify IRM */
93545febcb4aSScott Carter, SD IOSW 	if (cbp->cb_flags & DDI_CB_FLAG_INTR)
93555febcb4aSScott Carter, SD IOSW 		i_ddi_irm_set_cb(dip, B_FALSE);
93565febcb4aSScott Carter, SD IOSW 
93575febcb4aSScott Carter, SD IOSW 	/* Destroy the callback */
93585febcb4aSScott Carter, SD IOSW 	kmem_free(cbp, sizeof (ddi_cb_t));
93595febcb4aSScott Carter, SD IOSW 	DEVI(dip)->devi_cb_p = NULL;
93605febcb4aSScott Carter, SD IOSW 
93615febcb4aSScott Carter, SD IOSW 	return (DDI_SUCCESS);
93625febcb4aSScott Carter, SD IOSW }
9363