xref: /titanic_51/usr/src/uts/common/os/sunddi.c (revision ccdeb6b6d71f3c9aa7e78b688f7b34fff109a817)
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 /*
234f1e984dSReed  * Copyright (c) 1990, 2010, Oracle and/or its affiliates. All rights reserved.
24b89e420aSGarrett D'Amore  * Copyright 2014 Garrett D'Amore <garrett@damore.org>
25*ccdeb6b6SDan McDonald  * Copyright 2023 MNX Cloud, Inc.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <sys/note.h>
297c478bd9Sstevel@tonic-gate #include <sys/types.h>
307c478bd9Sstevel@tonic-gate #include <sys/param.h>
317c478bd9Sstevel@tonic-gate #include <sys/systm.h>
327c478bd9Sstevel@tonic-gate #include <sys/buf.h>
337c478bd9Sstevel@tonic-gate #include <sys/uio.h>
347c478bd9Sstevel@tonic-gate #include <sys/cred.h>
357c478bd9Sstevel@tonic-gate #include <sys/poll.h>
367c478bd9Sstevel@tonic-gate #include <sys/mman.h>
377c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
387c478bd9Sstevel@tonic-gate #include <sys/model.h>
397c478bd9Sstevel@tonic-gate #include <sys/file.h>
407c478bd9Sstevel@tonic-gate #include <sys/proc.h>
417c478bd9Sstevel@tonic-gate #include <sys/open.h>
427c478bd9Sstevel@tonic-gate #include <sys/user.h>
437c478bd9Sstevel@tonic-gate #include <sys/t_lock.h>
447c478bd9Sstevel@tonic-gate #include <sys/vm.h>
457c478bd9Sstevel@tonic-gate #include <sys/stat.h>
467c478bd9Sstevel@tonic-gate #include <vm/hat.h>
477c478bd9Sstevel@tonic-gate #include <vm/seg.h>
487c478bd9Sstevel@tonic-gate #include <vm/seg_vn.h>
497c478bd9Sstevel@tonic-gate #include <vm/seg_dev.h>
507c478bd9Sstevel@tonic-gate #include <vm/as.h>
517c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
527c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
537c478bd9Sstevel@tonic-gate #include <sys/debug.h>
547c478bd9Sstevel@tonic-gate #include <sys/autoconf.h>
557c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
567c478bd9Sstevel@tonic-gate #include <sys/esunddi.h>
577c478bd9Sstevel@tonic-gate #include <sys/sunndi.h>
587c478bd9Sstevel@tonic-gate #include <sys/kstat.h>
597c478bd9Sstevel@tonic-gate #include <sys/conf.h>
607c478bd9Sstevel@tonic-gate #include <sys/ddi_impldefs.h>	/* include implementation structure defs */
617c478bd9Sstevel@tonic-gate #include <sys/ndi_impldefs.h>	/* include prototypes */
62a288e5a9SJoshua M. Clulow #include <sys/ddi_periodic.h>
637c478bd9Sstevel@tonic-gate #include <sys/hwconf.h>
647c478bd9Sstevel@tonic-gate #include <sys/pathname.h>
657c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
667c478bd9Sstevel@tonic-gate #include <sys/epm.h>
677c478bd9Sstevel@tonic-gate #include <sys/devctl.h>
687c478bd9Sstevel@tonic-gate #include <sys/callb.h>
697c478bd9Sstevel@tonic-gate #include <sys/cladm.h>
707c478bd9Sstevel@tonic-gate #include <sys/sysevent.h>
717c478bd9Sstevel@tonic-gate #include <sys/dacf_impl.h>
727c478bd9Sstevel@tonic-gate #include <sys/ddidevmap.h>
737c478bd9Sstevel@tonic-gate #include <sys/bootconf.h>
747c478bd9Sstevel@tonic-gate #include <sys/disp.h>
757c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
767c478bd9Sstevel@tonic-gate #include <sys/promif.h>
777c478bd9Sstevel@tonic-gate #include <sys/instance.h>
787c478bd9Sstevel@tonic-gate #include <sys/sysevent/eventdefs.h>
797c478bd9Sstevel@tonic-gate #include <sys/task.h>
807c478bd9Sstevel@tonic-gate #include <sys/project.h>
817c478bd9Sstevel@tonic-gate #include <sys/taskq.h>
827c478bd9Sstevel@tonic-gate #include <sys/devpolicy.h>
837c478bd9Sstevel@tonic-gate #include <sys/ctype.h>
847c478bd9Sstevel@tonic-gate #include <net/if.h>
85c6939658Ssl108498 #include <sys/rctl.h>
865679c89fSjv227347 #include <sys/zone.h>
87d3d50737SRafael Vanoni #include <sys/clock_impl.h>
884c06356bSdh142964 #include <sys/ddi.h>
89a3114836SGerry Liu #include <sys/modhash.h>
90a3114836SGerry Liu #include <sys/sunldi_impl.h>
91a3114836SGerry Liu #include <sys/fs/dv_node.h>
92a3114836SGerry Liu #include <sys/fs/snode.h>
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate extern	pri_t	minclsyspri;
957c478bd9Sstevel@tonic-gate 
96c6939658Ssl108498 extern	rctl_hndl_t rc_project_locked_mem;
97c6939658Ssl108498 extern	rctl_hndl_t rc_zone_locked_mem;
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate #ifdef DEBUG
1007c478bd9Sstevel@tonic-gate static int sunddi_debug = 0;
1017c478bd9Sstevel@tonic-gate #endif /* DEBUG */
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate /* ddi_umem_unlock miscellaneous */
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate static	void	i_ddi_umem_unlock_thread_start(void);
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate static	kmutex_t	ddi_umem_unlock_mutex; /* unlock list mutex */
1087c478bd9Sstevel@tonic-gate static	kcondvar_t	ddi_umem_unlock_cv; /* unlock list block/unblock */
1097c478bd9Sstevel@tonic-gate static	kthread_t	*ddi_umem_unlock_thread;
1107c478bd9Sstevel@tonic-gate /*
1117c478bd9Sstevel@tonic-gate  * The ddi_umem_unlock FIFO list.  NULL head pointer indicates empty list.
1127c478bd9Sstevel@tonic-gate  */
1137c478bd9Sstevel@tonic-gate static	struct	ddi_umem_cookie *ddi_umem_unlock_head = NULL;
1147c478bd9Sstevel@tonic-gate static	struct	ddi_umem_cookie *ddi_umem_unlock_tail = NULL;
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate /*
1177c478bd9Sstevel@tonic-gate  * DDI(Sun) Function and flag definitions:
1187c478bd9Sstevel@tonic-gate  */
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate #if defined(__x86)
1217c478bd9Sstevel@tonic-gate /*
1227c478bd9Sstevel@tonic-gate  * Used to indicate which entries were chosen from a range.
1237c478bd9Sstevel@tonic-gate  */
1247c478bd9Sstevel@tonic-gate char	*chosen_reg = "chosen-reg";
1257c478bd9Sstevel@tonic-gate #endif
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate /*
1287c478bd9Sstevel@tonic-gate  * Function used to ring system console bell
1297c478bd9Sstevel@tonic-gate  */
1307c478bd9Sstevel@tonic-gate void (*ddi_console_bell_func)(clock_t duration);
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate /*
1337c478bd9Sstevel@tonic-gate  * Creating register mappings and handling interrupts:
1347c478bd9Sstevel@tonic-gate  */
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate /*
1377c478bd9Sstevel@tonic-gate  * Generic ddi_map: Call parent to fulfill request...
1387c478bd9Sstevel@tonic-gate  */
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate int
1417c478bd9Sstevel@tonic-gate ddi_map(dev_info_t *dp, ddi_map_req_t *mp, off_t offset,
1427c478bd9Sstevel@tonic-gate     off_t len, caddr_t *addrp)
1437c478bd9Sstevel@tonic-gate {
1447c478bd9Sstevel@tonic-gate 	dev_info_t *pdip;
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 	ASSERT(dp);
1477c478bd9Sstevel@tonic-gate 	pdip = (dev_info_t *)DEVI(dp)->devi_parent;
1487c478bd9Sstevel@tonic-gate 	return ((DEVI(pdip)->devi_ops->devo_bus_ops->bus_map)(pdip,
1497c478bd9Sstevel@tonic-gate 	    dp, mp, offset, len, addrp));
1507c478bd9Sstevel@tonic-gate }
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate /*
1537c478bd9Sstevel@tonic-gate  * ddi_apply_range: (Called by nexi only.)
1547c478bd9Sstevel@tonic-gate  * Apply ranges in parent node dp, to child regspec rp...
1557c478bd9Sstevel@tonic-gate  */
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate int
1587c478bd9Sstevel@tonic-gate ddi_apply_range(dev_info_t *dp, dev_info_t *rdip, struct regspec *rp)
1597c478bd9Sstevel@tonic-gate {
1607c478bd9Sstevel@tonic-gate 	return (i_ddi_apply_range(dp, rdip, rp));
1617c478bd9Sstevel@tonic-gate }
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate int
1647c478bd9Sstevel@tonic-gate ddi_map_regs(dev_info_t *dip, uint_t rnumber, caddr_t *kaddrp, off_t offset,
1657c478bd9Sstevel@tonic-gate     off_t len)
1667c478bd9Sstevel@tonic-gate {
1677c478bd9Sstevel@tonic-gate 	ddi_map_req_t mr;
1687c478bd9Sstevel@tonic-gate #if defined(__x86)
1697c478bd9Sstevel@tonic-gate 	struct {
1707c478bd9Sstevel@tonic-gate 		int	bus;
1717c478bd9Sstevel@tonic-gate 		int	addr;
1727c478bd9Sstevel@tonic-gate 		int	size;
1737c478bd9Sstevel@tonic-gate 	} reg, *reglist;
1747c478bd9Sstevel@tonic-gate 	uint_t	length;
1757c478bd9Sstevel@tonic-gate 	int	rc;
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 	/*
1787c478bd9Sstevel@tonic-gate 	 * get the 'registers' or the 'reg' property.
1797c478bd9Sstevel@tonic-gate 	 * We look up the reg property as an array of
1807c478bd9Sstevel@tonic-gate 	 * int's.
1817c478bd9Sstevel@tonic-gate 	 */
1827c478bd9Sstevel@tonic-gate 	rc = ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dip,
1837c478bd9Sstevel@tonic-gate 	    DDI_PROP_DONTPASS, "registers", (int **)&reglist, &length);
1847c478bd9Sstevel@tonic-gate 	if (rc != DDI_PROP_SUCCESS)
1857c478bd9Sstevel@tonic-gate 		rc = ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dip,
1867c478bd9Sstevel@tonic-gate 		    DDI_PROP_DONTPASS, "reg", (int **)&reglist, &length);
1877c478bd9Sstevel@tonic-gate 	if (rc == DDI_PROP_SUCCESS) {
1887c478bd9Sstevel@tonic-gate 		/*
1897c478bd9Sstevel@tonic-gate 		 * point to the required entry.
1907c478bd9Sstevel@tonic-gate 		 */
1917c478bd9Sstevel@tonic-gate 		reg = reglist[rnumber];
1927c478bd9Sstevel@tonic-gate 		reg.addr += offset;
1937c478bd9Sstevel@tonic-gate 		if (len != 0)
1947c478bd9Sstevel@tonic-gate 			reg.size = len;
1957c478bd9Sstevel@tonic-gate 		/*
1967c478bd9Sstevel@tonic-gate 		 * make a new property containing ONLY the required tuple.
1977c478bd9Sstevel@tonic-gate 		 */
1987c478bd9Sstevel@tonic-gate 		if (ddi_prop_update_int_array(DDI_DEV_T_NONE, dip,
1997c478bd9Sstevel@tonic-gate 		    chosen_reg, (int *)&reg, (sizeof (reg)/sizeof (int)))
2007c478bd9Sstevel@tonic-gate 		    != DDI_PROP_SUCCESS) {
2017c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s%d: cannot create '%s' "
2027c478bd9Sstevel@tonic-gate 			    "property", DEVI(dip)->devi_name,
2037c478bd9Sstevel@tonic-gate 			    DEVI(dip)->devi_instance, chosen_reg);
2047c478bd9Sstevel@tonic-gate 		}
2057c478bd9Sstevel@tonic-gate 		/*
2067c478bd9Sstevel@tonic-gate 		 * free the memory allocated by
2077c478bd9Sstevel@tonic-gate 		 * ddi_prop_lookup_int_array ().
2087c478bd9Sstevel@tonic-gate 		 */
2097c478bd9Sstevel@tonic-gate 		ddi_prop_free((void *)reglist);
2107c478bd9Sstevel@tonic-gate 	}
2117c478bd9Sstevel@tonic-gate #endif
2127c478bd9Sstevel@tonic-gate 	mr.map_op = DDI_MO_MAP_LOCKED;
2137c478bd9Sstevel@tonic-gate 	mr.map_type = DDI_MT_RNUMBER;
2147c478bd9Sstevel@tonic-gate 	mr.map_obj.rnumber = rnumber;
2157c478bd9Sstevel@tonic-gate 	mr.map_prot = PROT_READ | PROT_WRITE;
2167c478bd9Sstevel@tonic-gate 	mr.map_flags = DDI_MF_KERNEL_MAPPING;
2177c478bd9Sstevel@tonic-gate 	mr.map_handlep = NULL;
2187c478bd9Sstevel@tonic-gate 	mr.map_vers = DDI_MAP_VERSION;
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 	/*
2217c478bd9Sstevel@tonic-gate 	 * Call my parent to map in my regs.
2227c478bd9Sstevel@tonic-gate 	 */
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 	return (ddi_map(dip, &mr, offset, len, kaddrp));
2257c478bd9Sstevel@tonic-gate }
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate void
2287c478bd9Sstevel@tonic-gate ddi_unmap_regs(dev_info_t *dip, uint_t rnumber, caddr_t *kaddrp, off_t offset,
2297c478bd9Sstevel@tonic-gate     off_t len)
2307c478bd9Sstevel@tonic-gate {
2317c478bd9Sstevel@tonic-gate 	ddi_map_req_t mr;
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 	mr.map_op = DDI_MO_UNMAP;
2347c478bd9Sstevel@tonic-gate 	mr.map_type = DDI_MT_RNUMBER;
2357c478bd9Sstevel@tonic-gate 	mr.map_flags = DDI_MF_KERNEL_MAPPING;
2367c478bd9Sstevel@tonic-gate 	mr.map_prot = PROT_READ | PROT_WRITE;	/* who cares? */
2377c478bd9Sstevel@tonic-gate 	mr.map_obj.rnumber = rnumber;
2387c478bd9Sstevel@tonic-gate 	mr.map_handlep = NULL;
2397c478bd9Sstevel@tonic-gate 	mr.map_vers = DDI_MAP_VERSION;
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 	/*
2427c478bd9Sstevel@tonic-gate 	 * Call my parent to unmap my regs.
2437c478bd9Sstevel@tonic-gate 	 */
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	(void) ddi_map(dip, &mr, offset, len, kaddrp);
2467c478bd9Sstevel@tonic-gate 	*kaddrp = (caddr_t)0;
2477c478bd9Sstevel@tonic-gate #if defined(__x86)
2487c478bd9Sstevel@tonic-gate 	(void) ddi_prop_remove(DDI_DEV_T_NONE, dip, chosen_reg);
2497c478bd9Sstevel@tonic-gate #endif
2507c478bd9Sstevel@tonic-gate }
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate int
2537c478bd9Sstevel@tonic-gate ddi_bus_map(dev_info_t *dip, dev_info_t *rdip, ddi_map_req_t *mp,
2547c478bd9Sstevel@tonic-gate 	off_t offset, off_t len, caddr_t *vaddrp)
2557c478bd9Sstevel@tonic-gate {
2567c478bd9Sstevel@tonic-gate 	return (i_ddi_bus_map(dip, rdip, mp, offset, len, vaddrp));
2577c478bd9Sstevel@tonic-gate }
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate /*
2607c478bd9Sstevel@tonic-gate  * nullbusmap:	The/DDI default bus_map entry point for nexi
2617c478bd9Sstevel@tonic-gate  *		not conforming to the reg/range paradigm (i.e. scsi, etc.)
2627c478bd9Sstevel@tonic-gate  *		with no HAT/MMU layer to be programmed at this level.
2637c478bd9Sstevel@tonic-gate  *
2647c478bd9Sstevel@tonic-gate  *		If the call is to map by rnumber, return an error,
2657c478bd9Sstevel@tonic-gate  *		otherwise pass anything else up the tree to my parent.
2667c478bd9Sstevel@tonic-gate  */
2677c478bd9Sstevel@tonic-gate int
2687c478bd9Sstevel@tonic-gate nullbusmap(dev_info_t *dip, dev_info_t *rdip, ddi_map_req_t *mp,
2697c478bd9Sstevel@tonic-gate 	off_t offset, off_t len, caddr_t *vaddrp)
2707c478bd9Sstevel@tonic-gate {
2717c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(rdip))
2727c478bd9Sstevel@tonic-gate 	if (mp->map_type == DDI_MT_RNUMBER)
2737c478bd9Sstevel@tonic-gate 		return (DDI_ME_UNSUPPORTED);
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 	return (ddi_map(dip, mp, offset, len, vaddrp));
2767c478bd9Sstevel@tonic-gate }
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate /*
2797c478bd9Sstevel@tonic-gate  * ddi_rnumber_to_regspec: Not for use by leaf drivers.
2807c478bd9Sstevel@tonic-gate  *			   Only for use by nexi using the reg/range paradigm.
2817c478bd9Sstevel@tonic-gate  */
2827c478bd9Sstevel@tonic-gate struct regspec *
2837c478bd9Sstevel@tonic-gate ddi_rnumber_to_regspec(dev_info_t *dip, int rnumber)
2847c478bd9Sstevel@tonic-gate {
2857c478bd9Sstevel@tonic-gate 	return (i_ddi_rnumber_to_regspec(dip, rnumber));
2867c478bd9Sstevel@tonic-gate }
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate /*
2907c478bd9Sstevel@tonic-gate  * Note that we allow the dip to be nil because we may be called
2917c478bd9Sstevel@tonic-gate  * prior even to the instantiation of the devinfo tree itself - all
2927c478bd9Sstevel@tonic-gate  * regular leaf and nexus drivers should always use a non-nil dip!
2937c478bd9Sstevel@tonic-gate  *
2947c478bd9Sstevel@tonic-gate  * We treat peek in a somewhat cavalier fashion .. assuming that we'll
2957c478bd9Sstevel@tonic-gate  * simply get a synchronous fault as soon as we touch a missing address.
2967c478bd9Sstevel@tonic-gate  *
2977c478bd9Sstevel@tonic-gate  * Poke is rather more carefully handled because we might poke to a write
2987c478bd9Sstevel@tonic-gate  * buffer, "succeed", then only find some time later that we got an
2997c478bd9Sstevel@tonic-gate  * asynchronous fault that indicated that the address we were writing to
3007c478bd9Sstevel@tonic-gate  * was not really backed by hardware.
3017c478bd9Sstevel@tonic-gate  */
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate static int
3047c478bd9Sstevel@tonic-gate i_ddi_peekpoke(dev_info_t *devi, ddi_ctl_enum_t cmd, size_t size,
3057c478bd9Sstevel@tonic-gate     void *addr, void *value_p)
3067c478bd9Sstevel@tonic-gate {
3077c478bd9Sstevel@tonic-gate 	union {
3087c478bd9Sstevel@tonic-gate 		uint64_t	u64;
3097c478bd9Sstevel@tonic-gate 		uint32_t	u32;
3107c478bd9Sstevel@tonic-gate 		uint16_t	u16;
3117c478bd9Sstevel@tonic-gate 		uint8_t		u8;
3127c478bd9Sstevel@tonic-gate 	} peekpoke_value;
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 	peekpoke_ctlops_t peekpoke_args;
3157c478bd9Sstevel@tonic-gate 	uint64_t dummy_result;
3167c478bd9Sstevel@tonic-gate 	int rval;
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 	/* Note: size is assumed to be correct;  it is not checked. */
3197c478bd9Sstevel@tonic-gate 	peekpoke_args.size = size;
320abdbd06dSagiri 	peekpoke_args.dev_addr = (uintptr_t)addr;
3217c478bd9Sstevel@tonic-gate 	peekpoke_args.handle = NULL;
3227c478bd9Sstevel@tonic-gate 	peekpoke_args.repcount = 1;
3237c478bd9Sstevel@tonic-gate 	peekpoke_args.flags = 0;
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate 	if (cmd == DDI_CTLOPS_POKE) {
3267c478bd9Sstevel@tonic-gate 		switch (size) {
3277c478bd9Sstevel@tonic-gate 		case sizeof (uint8_t):
3287c478bd9Sstevel@tonic-gate 			peekpoke_value.u8 = *(uint8_t *)value_p;
3297c478bd9Sstevel@tonic-gate 			break;
3307c478bd9Sstevel@tonic-gate 		case sizeof (uint16_t):
3317c478bd9Sstevel@tonic-gate 			peekpoke_value.u16 = *(uint16_t *)value_p;
3327c478bd9Sstevel@tonic-gate 			break;
3337c478bd9Sstevel@tonic-gate 		case sizeof (uint32_t):
3347c478bd9Sstevel@tonic-gate 			peekpoke_value.u32 = *(uint32_t *)value_p;
3357c478bd9Sstevel@tonic-gate 			break;
3367c478bd9Sstevel@tonic-gate 		case sizeof (uint64_t):
3377c478bd9Sstevel@tonic-gate 			peekpoke_value.u64 = *(uint64_t *)value_p;
3387c478bd9Sstevel@tonic-gate 			break;
3397c478bd9Sstevel@tonic-gate 		}
3407c478bd9Sstevel@tonic-gate 	}
3417c478bd9Sstevel@tonic-gate 
342abdbd06dSagiri 	peekpoke_args.host_addr = (uintptr_t)&peekpoke_value.u64;
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 	if (devi != NULL)
3457c478bd9Sstevel@tonic-gate 		rval = ddi_ctlops(devi, devi, cmd, &peekpoke_args,
3467c478bd9Sstevel@tonic-gate 		    &dummy_result);
3477c478bd9Sstevel@tonic-gate 	else
3487c478bd9Sstevel@tonic-gate 		rval = peekpoke_mem(cmd, &peekpoke_args);
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate 	/*
3517c478bd9Sstevel@tonic-gate 	 * A NULL value_p is permitted by ddi_peek(9F); discard the result.
3527c478bd9Sstevel@tonic-gate 	 */
3537c478bd9Sstevel@tonic-gate 	if ((cmd == DDI_CTLOPS_PEEK) & (value_p != NULL)) {
3547c478bd9Sstevel@tonic-gate 		switch (size) {
3557c478bd9Sstevel@tonic-gate 		case sizeof (uint8_t):
3567c478bd9Sstevel@tonic-gate 			*(uint8_t *)value_p = peekpoke_value.u8;
3577c478bd9Sstevel@tonic-gate 			break;
3587c478bd9Sstevel@tonic-gate 		case sizeof (uint16_t):
3597c478bd9Sstevel@tonic-gate 			*(uint16_t *)value_p = peekpoke_value.u16;
3607c478bd9Sstevel@tonic-gate 			break;
3617c478bd9Sstevel@tonic-gate 		case sizeof (uint32_t):
3627c478bd9Sstevel@tonic-gate 			*(uint32_t *)value_p = peekpoke_value.u32;
3637c478bd9Sstevel@tonic-gate 			break;
3647c478bd9Sstevel@tonic-gate 		case sizeof (uint64_t):
3657c478bd9Sstevel@tonic-gate 			*(uint64_t *)value_p = peekpoke_value.u64;
3667c478bd9Sstevel@tonic-gate 			break;
3677c478bd9Sstevel@tonic-gate 		}
3687c478bd9Sstevel@tonic-gate 	}
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 	return (rval);
3717c478bd9Sstevel@tonic-gate }
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate /*
3747c478bd9Sstevel@tonic-gate  * Keep ddi_peek() and ddi_poke() in case 3rd parties are calling this.
3757c478bd9Sstevel@tonic-gate  * they shouldn't be, but the 9f manpage kind of pseudo exposes it.
3767c478bd9Sstevel@tonic-gate  */
3777c478bd9Sstevel@tonic-gate int
3787c478bd9Sstevel@tonic-gate ddi_peek(dev_info_t *devi, size_t size, void *addr, void *value_p)
3797c478bd9Sstevel@tonic-gate {
3807c478bd9Sstevel@tonic-gate 	switch (size) {
3817c478bd9Sstevel@tonic-gate 	case sizeof (uint8_t):
3827c478bd9Sstevel@tonic-gate 	case sizeof (uint16_t):
3837c478bd9Sstevel@tonic-gate 	case sizeof (uint32_t):
3847c478bd9Sstevel@tonic-gate 	case sizeof (uint64_t):
3857c478bd9Sstevel@tonic-gate 		break;
3867c478bd9Sstevel@tonic-gate 	default:
3877c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
3887c478bd9Sstevel@tonic-gate 	}
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 	return (i_ddi_peekpoke(devi, DDI_CTLOPS_PEEK, size, addr, value_p));
3917c478bd9Sstevel@tonic-gate }
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate int
3947c478bd9Sstevel@tonic-gate ddi_poke(dev_info_t *devi, size_t size, void *addr, void *value_p)
3957c478bd9Sstevel@tonic-gate {
3967c478bd9Sstevel@tonic-gate 	switch (size) {
3977c478bd9Sstevel@tonic-gate 	case sizeof (uint8_t):
3987c478bd9Sstevel@tonic-gate 	case sizeof (uint16_t):
3997c478bd9Sstevel@tonic-gate 	case sizeof (uint32_t):
4007c478bd9Sstevel@tonic-gate 	case sizeof (uint64_t):
4017c478bd9Sstevel@tonic-gate 		break;
4027c478bd9Sstevel@tonic-gate 	default:
4037c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
4047c478bd9Sstevel@tonic-gate 	}
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate 	return (i_ddi_peekpoke(devi, DDI_CTLOPS_POKE, size, addr, value_p));
4077c478bd9Sstevel@tonic-gate }
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate int
4107c478bd9Sstevel@tonic-gate ddi_peek8(dev_info_t *dip, int8_t *addr, int8_t *val_p)
4117c478bd9Sstevel@tonic-gate {
4127c478bd9Sstevel@tonic-gate 	return (i_ddi_peekpoke(dip, DDI_CTLOPS_PEEK, sizeof (*val_p), addr,
4137c478bd9Sstevel@tonic-gate 	    val_p));
4147c478bd9Sstevel@tonic-gate }
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate int
4177c478bd9Sstevel@tonic-gate ddi_peek16(dev_info_t *dip, int16_t *addr, int16_t *val_p)
4187c478bd9Sstevel@tonic-gate {
4197c478bd9Sstevel@tonic-gate 	return (i_ddi_peekpoke(dip, DDI_CTLOPS_PEEK, sizeof (*val_p), addr,
4207c478bd9Sstevel@tonic-gate 	    val_p));
4217c478bd9Sstevel@tonic-gate }
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate int
4247c478bd9Sstevel@tonic-gate ddi_peek32(dev_info_t *dip, int32_t *addr, int32_t *val_p)
4257c478bd9Sstevel@tonic-gate {
4267c478bd9Sstevel@tonic-gate 	return (i_ddi_peekpoke(dip, DDI_CTLOPS_PEEK, sizeof (*val_p), addr,
4277c478bd9Sstevel@tonic-gate 	    val_p));
4287c478bd9Sstevel@tonic-gate }
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate int
4317c478bd9Sstevel@tonic-gate ddi_peek64(dev_info_t *dip, int64_t *addr, int64_t *val_p)
4327c478bd9Sstevel@tonic-gate {
4337c478bd9Sstevel@tonic-gate 	return (i_ddi_peekpoke(dip, DDI_CTLOPS_PEEK, sizeof (*val_p), addr,
4347c478bd9Sstevel@tonic-gate 	    val_p));
4357c478bd9Sstevel@tonic-gate }
4367c478bd9Sstevel@tonic-gate 
4374ab75253Smrj 
4384ab75253Smrj /*
4394ab75253Smrj  * We need to separate the old interfaces from the new ones and leave them
4404ab75253Smrj  * in here for a while. Previous versions of the OS defined the new interfaces
4414ab75253Smrj  * to the old interfaces. This way we can fix things up so that we can
4424ab75253Smrj  * eventually remove these interfaces.
4434ab75253Smrj  * e.g. A 3rd party module/driver using ddi_peek8 and built against S10
4444ab75253Smrj  * or earlier will actually have a reference to ddi_peekc in the binary.
4454ab75253Smrj  */
4464ab75253Smrj #ifdef _ILP32
4474ab75253Smrj int
4484ab75253Smrj ddi_peekc(dev_info_t *dip, int8_t *addr, int8_t *val_p)
4494ab75253Smrj {
4504ab75253Smrj 	return (i_ddi_peekpoke(dip, DDI_CTLOPS_PEEK, sizeof (*val_p), addr,
4514ab75253Smrj 	    val_p));
4524ab75253Smrj }
4534ab75253Smrj 
4544ab75253Smrj int
4554ab75253Smrj ddi_peeks(dev_info_t *dip, int16_t *addr, int16_t *val_p)
4564ab75253Smrj {
4574ab75253Smrj 	return (i_ddi_peekpoke(dip, DDI_CTLOPS_PEEK, sizeof (*val_p), addr,
4584ab75253Smrj 	    val_p));
4594ab75253Smrj }
4604ab75253Smrj 
4614ab75253Smrj int
4624ab75253Smrj ddi_peekl(dev_info_t *dip, int32_t *addr, int32_t *val_p)
4634ab75253Smrj {
4644ab75253Smrj 	return (i_ddi_peekpoke(dip, DDI_CTLOPS_PEEK, sizeof (*val_p), addr,
4654ab75253Smrj 	    val_p));
4664ab75253Smrj }
4674ab75253Smrj 
4684ab75253Smrj int
4694ab75253Smrj ddi_peekd(dev_info_t *dip, int64_t *addr, int64_t *val_p)
4704ab75253Smrj {
4714ab75253Smrj 	return (i_ddi_peekpoke(dip, DDI_CTLOPS_PEEK, sizeof (*val_p), addr,
4724ab75253Smrj 	    val_p));
4734ab75253Smrj }
4744ab75253Smrj #endif /* _ILP32 */
4754ab75253Smrj 
4767c478bd9Sstevel@tonic-gate int
4777c478bd9Sstevel@tonic-gate ddi_poke8(dev_info_t *dip, int8_t *addr, int8_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_poke16(dev_info_t *dip, int16_t *addr, int16_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_poke32(dev_info_t *dip, int32_t *addr, int32_t val)
4907c478bd9Sstevel@tonic-gate {
4917c478bd9Sstevel@tonic-gate 	return (i_ddi_peekpoke(dip, DDI_CTLOPS_POKE, sizeof (val), addr, &val));
4927c478bd9Sstevel@tonic-gate }
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate int
4957c478bd9Sstevel@tonic-gate ddi_poke64(dev_info_t *dip, int64_t *addr, int64_t val)
4967c478bd9Sstevel@tonic-gate {
4977c478bd9Sstevel@tonic-gate 	return (i_ddi_peekpoke(dip, DDI_CTLOPS_POKE, sizeof (val), addr, &val));
4987c478bd9Sstevel@tonic-gate }
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate /*
5014ab75253Smrj  * We need to separate the old interfaces from the new ones and leave them
5024ab75253Smrj  * in here for a while. Previous versions of the OS defined the new interfaces
5034ab75253Smrj  * to the old interfaces. This way we can fix things up so that we can
5044ab75253Smrj  * eventually remove these interfaces.
5054ab75253Smrj  * e.g. A 3rd party module/driver using ddi_poke8 and built against S10
5064ab75253Smrj  * or earlier will actually have a reference to ddi_pokec in the binary.
5074ab75253Smrj  */
5084ab75253Smrj #ifdef _ILP32
5094ab75253Smrj int
5104ab75253Smrj ddi_pokec(dev_info_t *dip, int8_t *addr, int8_t val)
5114ab75253Smrj {
5124ab75253Smrj 	return (i_ddi_peekpoke(dip, DDI_CTLOPS_POKE, sizeof (val), addr, &val));
5134ab75253Smrj }
5144ab75253Smrj 
5154ab75253Smrj int
5164ab75253Smrj ddi_pokes(dev_info_t *dip, int16_t *addr, int16_t val)
5174ab75253Smrj {
5184ab75253Smrj 	return (i_ddi_peekpoke(dip, DDI_CTLOPS_POKE, sizeof (val), addr, &val));
5194ab75253Smrj }
5204ab75253Smrj 
5214ab75253Smrj int
5224ab75253Smrj ddi_pokel(dev_info_t *dip, int32_t *addr, int32_t val)
5234ab75253Smrj {
5244ab75253Smrj 	return (i_ddi_peekpoke(dip, DDI_CTLOPS_POKE, sizeof (val), addr, &val));
5254ab75253Smrj }
5264ab75253Smrj 
5274ab75253Smrj int
5284ab75253Smrj ddi_poked(dev_info_t *dip, int64_t *addr, int64_t val)
5294ab75253Smrj {
5304ab75253Smrj 	return (i_ddi_peekpoke(dip, DDI_CTLOPS_POKE, sizeof (val), addr, &val));
5314ab75253Smrj }
5324ab75253Smrj #endif /* _ILP32 */
5334ab75253Smrj 
5344ab75253Smrj /*
5357c478bd9Sstevel@tonic-gate  * ddi_peekpokeio() is used primarily by the mem drivers for moving
5367c478bd9Sstevel@tonic-gate  * data to and from uio structures via peek and poke.  Note that we
5377c478bd9Sstevel@tonic-gate  * use "internal" routines ddi_peek and ddi_poke to make this go
5387c478bd9Sstevel@tonic-gate  * slightly faster, avoiding the call overhead ..
5397c478bd9Sstevel@tonic-gate  */
5407c478bd9Sstevel@tonic-gate int
5417c478bd9Sstevel@tonic-gate ddi_peekpokeio(dev_info_t *devi, struct uio *uio, enum uio_rw rw,
5427c478bd9Sstevel@tonic-gate     caddr_t addr, size_t len, uint_t xfersize)
5437c478bd9Sstevel@tonic-gate {
5447c478bd9Sstevel@tonic-gate 	int64_t	ibuffer;
5457c478bd9Sstevel@tonic-gate 	int8_t w8;
5467c478bd9Sstevel@tonic-gate 	size_t sz;
5477c478bd9Sstevel@tonic-gate 	int o;
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate 	if (xfersize > sizeof (long))
5507c478bd9Sstevel@tonic-gate 		xfersize = sizeof (long);
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate 	while (len != 0) {
5537c478bd9Sstevel@tonic-gate 		if ((len | (uintptr_t)addr) & 1) {
5547c478bd9Sstevel@tonic-gate 			sz = sizeof (int8_t);
5557c478bd9Sstevel@tonic-gate 			if (rw == UIO_WRITE) {
5567c478bd9Sstevel@tonic-gate 				if ((o = uwritec(uio)) == -1)
5577c478bd9Sstevel@tonic-gate 					return (DDI_FAILURE);
5587c478bd9Sstevel@tonic-gate 				if (ddi_poke8(devi, (int8_t *)addr,
5597c478bd9Sstevel@tonic-gate 				    (int8_t)o) != DDI_SUCCESS)
5607c478bd9Sstevel@tonic-gate 					return (DDI_FAILURE);
5617c478bd9Sstevel@tonic-gate 			} else {
5627c478bd9Sstevel@tonic-gate 				if (i_ddi_peekpoke(devi, DDI_CTLOPS_PEEK, sz,
5637c478bd9Sstevel@tonic-gate 				    (int8_t *)addr, &w8) != DDI_SUCCESS)
5647c478bd9Sstevel@tonic-gate 					return (DDI_FAILURE);
5657c478bd9Sstevel@tonic-gate 				if (ureadc(w8, uio))
5667c478bd9Sstevel@tonic-gate 					return (DDI_FAILURE);
5677c478bd9Sstevel@tonic-gate 			}
5687c478bd9Sstevel@tonic-gate 		} else {
5697c478bd9Sstevel@tonic-gate 			switch (xfersize) {
5707c478bd9Sstevel@tonic-gate 			case sizeof (int64_t):
5717c478bd9Sstevel@tonic-gate 				if (((len | (uintptr_t)addr) &
5727c478bd9Sstevel@tonic-gate 				    (sizeof (int64_t) - 1)) == 0) {
5737c478bd9Sstevel@tonic-gate 					sz = xfersize;
5747c478bd9Sstevel@tonic-gate 					break;
5757c478bd9Sstevel@tonic-gate 				}
5767c478bd9Sstevel@tonic-gate 				/*FALLTHROUGH*/
5777c478bd9Sstevel@tonic-gate 			case sizeof (int32_t):
5787c478bd9Sstevel@tonic-gate 				if (((len | (uintptr_t)addr) &
5797c478bd9Sstevel@tonic-gate 				    (sizeof (int32_t) - 1)) == 0) {
5807c478bd9Sstevel@tonic-gate 					sz = xfersize;
5817c478bd9Sstevel@tonic-gate 					break;
5827c478bd9Sstevel@tonic-gate 				}
5837c478bd9Sstevel@tonic-gate 				/*FALLTHROUGH*/
5847c478bd9Sstevel@tonic-gate 			default:
5857c478bd9Sstevel@tonic-gate 				/*
5867c478bd9Sstevel@tonic-gate 				 * This still assumes that we might have an
5877c478bd9Sstevel@tonic-gate 				 * I/O bus out there that permits 16-bit
5887c478bd9Sstevel@tonic-gate 				 * transfers (and that it would be upset by
5897c478bd9Sstevel@tonic-gate 				 * 32-bit transfers from such locations).
5907c478bd9Sstevel@tonic-gate 				 */
5917c478bd9Sstevel@tonic-gate 				sz = sizeof (int16_t);
5927c478bd9Sstevel@tonic-gate 				break;
5937c478bd9Sstevel@tonic-gate 			}
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate 			if (rw == UIO_READ) {
5967c478bd9Sstevel@tonic-gate 				if (i_ddi_peekpoke(devi, DDI_CTLOPS_PEEK, sz,
5977c478bd9Sstevel@tonic-gate 				    addr, &ibuffer) != DDI_SUCCESS)
5987c478bd9Sstevel@tonic-gate 					return (DDI_FAILURE);
5997c478bd9Sstevel@tonic-gate 			}
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate 			if (uiomove(&ibuffer, sz, rw, uio))
6027c478bd9Sstevel@tonic-gate 				return (DDI_FAILURE);
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate 			if (rw == UIO_WRITE) {
6057c478bd9Sstevel@tonic-gate 				if (i_ddi_peekpoke(devi, DDI_CTLOPS_POKE, sz,
6067c478bd9Sstevel@tonic-gate 				    addr, &ibuffer) != DDI_SUCCESS)
6077c478bd9Sstevel@tonic-gate 					return (DDI_FAILURE);
6087c478bd9Sstevel@tonic-gate 			}
6097c478bd9Sstevel@tonic-gate 		}
6107c478bd9Sstevel@tonic-gate 		addr += sz;
6117c478bd9Sstevel@tonic-gate 		len -= sz;
6127c478bd9Sstevel@tonic-gate 	}
6137c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
6147c478bd9Sstevel@tonic-gate }
6157c478bd9Sstevel@tonic-gate 
6167c478bd9Sstevel@tonic-gate /*
6177c478bd9Sstevel@tonic-gate  * These routines are used by drivers that do layered ioctls
6187c478bd9Sstevel@tonic-gate  * On sparc, they're implemented in assembler to avoid spilling
6197c478bd9Sstevel@tonic-gate  * register windows in the common (copyin) case ..
6207c478bd9Sstevel@tonic-gate  */
6217c478bd9Sstevel@tonic-gate #if !defined(__sparc)
6227c478bd9Sstevel@tonic-gate int
6237c478bd9Sstevel@tonic-gate ddi_copyin(const void *buf, void *kernbuf, size_t size, int flags)
6247c478bd9Sstevel@tonic-gate {
6257c478bd9Sstevel@tonic-gate 	if (flags & FKIOCTL)
6267c478bd9Sstevel@tonic-gate 		return (kcopy(buf, kernbuf, size) ? -1 : 0);
6277c478bd9Sstevel@tonic-gate 	return (copyin(buf, kernbuf, size));
6287c478bd9Sstevel@tonic-gate }
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate int
6317c478bd9Sstevel@tonic-gate ddi_copyout(const void *buf, void *kernbuf, size_t size, int flags)
6327c478bd9Sstevel@tonic-gate {
6337c478bd9Sstevel@tonic-gate 	if (flags & FKIOCTL)
6347c478bd9Sstevel@tonic-gate 		return (kcopy(buf, kernbuf, size) ? -1 : 0);
6357c478bd9Sstevel@tonic-gate 	return (copyout(buf, kernbuf, size));
6367c478bd9Sstevel@tonic-gate }
6377c478bd9Sstevel@tonic-gate #endif	/* !__sparc */
6387c478bd9Sstevel@tonic-gate 
6397c478bd9Sstevel@tonic-gate /*
6407c478bd9Sstevel@tonic-gate  * Conversions in nexus pagesize units.  We don't duplicate the
6417c478bd9Sstevel@tonic-gate  * 'nil dip' semantics of peek/poke because btopr/btop/ptob are DDI/DKI
6427c478bd9Sstevel@tonic-gate  * routines anyway.
6437c478bd9Sstevel@tonic-gate  */
6447c478bd9Sstevel@tonic-gate unsigned long
6457c478bd9Sstevel@tonic-gate ddi_btop(dev_info_t *dip, unsigned long bytes)
6467c478bd9Sstevel@tonic-gate {
6477c478bd9Sstevel@tonic-gate 	unsigned long pages;
6487c478bd9Sstevel@tonic-gate 
6497c478bd9Sstevel@tonic-gate 	(void) ddi_ctlops(dip, dip, DDI_CTLOPS_BTOP, &bytes, &pages);
6507c478bd9Sstevel@tonic-gate 	return (pages);
6517c478bd9Sstevel@tonic-gate }
6527c478bd9Sstevel@tonic-gate 
6537c478bd9Sstevel@tonic-gate unsigned long
6547c478bd9Sstevel@tonic-gate ddi_btopr(dev_info_t *dip, unsigned long bytes)
6557c478bd9Sstevel@tonic-gate {
6567c478bd9Sstevel@tonic-gate 	unsigned long pages;
6577c478bd9Sstevel@tonic-gate 
6587c478bd9Sstevel@tonic-gate 	(void) ddi_ctlops(dip, dip, DDI_CTLOPS_BTOPR, &bytes, &pages);
6597c478bd9Sstevel@tonic-gate 	return (pages);
6607c478bd9Sstevel@tonic-gate }
6617c478bd9Sstevel@tonic-gate 
6627c478bd9Sstevel@tonic-gate unsigned long
6637c478bd9Sstevel@tonic-gate ddi_ptob(dev_info_t *dip, unsigned long pages)
6647c478bd9Sstevel@tonic-gate {
6657c478bd9Sstevel@tonic-gate 	unsigned long bytes;
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate 	(void) ddi_ctlops(dip, dip, DDI_CTLOPS_PTOB, &pages, &bytes);
6687c478bd9Sstevel@tonic-gate 	return (bytes);
6697c478bd9Sstevel@tonic-gate }
6707c478bd9Sstevel@tonic-gate 
6717c478bd9Sstevel@tonic-gate unsigned int
6727c478bd9Sstevel@tonic-gate ddi_enter_critical(void)
6737c478bd9Sstevel@tonic-gate {
6747c478bd9Sstevel@tonic-gate 	return ((uint_t)spl7());
6757c478bd9Sstevel@tonic-gate }
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate void
6787c478bd9Sstevel@tonic-gate ddi_exit_critical(unsigned int spl)
6797c478bd9Sstevel@tonic-gate {
6807c478bd9Sstevel@tonic-gate 	splx((int)spl);
6817c478bd9Sstevel@tonic-gate }
6827c478bd9Sstevel@tonic-gate 
6837c478bd9Sstevel@tonic-gate /*
6847c478bd9Sstevel@tonic-gate  * Nexus ctlops punter
6857c478bd9Sstevel@tonic-gate  */
6867c478bd9Sstevel@tonic-gate 
6877c478bd9Sstevel@tonic-gate #if !defined(__sparc)
6887c478bd9Sstevel@tonic-gate /*
6897c478bd9Sstevel@tonic-gate  * Request bus_ctl parent to handle a bus_ctl request
6907c478bd9Sstevel@tonic-gate  *
6917c478bd9Sstevel@tonic-gate  * (The sparc version is in sparc_ddi.s)
6927c478bd9Sstevel@tonic-gate  */
6937c478bd9Sstevel@tonic-gate int
6947c478bd9Sstevel@tonic-gate ddi_ctlops(dev_info_t *d, dev_info_t *r, ddi_ctl_enum_t op, void *a, void *v)
6957c478bd9Sstevel@tonic-gate {
6967c478bd9Sstevel@tonic-gate 	int (*fp)();
6977c478bd9Sstevel@tonic-gate 
6987c478bd9Sstevel@tonic-gate 	if (!d || !r)
6997c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate 	if ((d = (dev_info_t *)DEVI(d)->devi_bus_ctl) == NULL)
7027c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
7037c478bd9Sstevel@tonic-gate 
7047c478bd9Sstevel@tonic-gate 	fp = DEVI(d)->devi_ops->devo_bus_ops->bus_ctl;
7057c478bd9Sstevel@tonic-gate 	return ((*fp)(d, r, op, a, v));
7067c478bd9Sstevel@tonic-gate }
7077c478bd9Sstevel@tonic-gate 
7087c478bd9Sstevel@tonic-gate #endif
7097c478bd9Sstevel@tonic-gate 
7107c478bd9Sstevel@tonic-gate /*
7117c478bd9Sstevel@tonic-gate  * DMA/DVMA setup
7127c478bd9Sstevel@tonic-gate  */
7137c478bd9Sstevel@tonic-gate 
7147c478bd9Sstevel@tonic-gate #if !defined(__sparc)
7157c478bd9Sstevel@tonic-gate /*
7167c478bd9Sstevel@tonic-gate  * Request bus_dma_ctl parent to fiddle with a dma request.
7177c478bd9Sstevel@tonic-gate  *
7187c478bd9Sstevel@tonic-gate  * (The sparc version is in sparc_subr.s)
7197c478bd9Sstevel@tonic-gate  */
7207c478bd9Sstevel@tonic-gate int
7217c478bd9Sstevel@tonic-gate ddi_dma_mctl(dev_info_t *dip, dev_info_t *rdip,
7227c478bd9Sstevel@tonic-gate     ddi_dma_handle_t handle, enum ddi_dma_ctlops request,
7237c478bd9Sstevel@tonic-gate     off_t *offp, size_t *lenp, caddr_t *objp, uint_t flags)
7247c478bd9Sstevel@tonic-gate {
7257c478bd9Sstevel@tonic-gate 	int (*fp)();
7267c478bd9Sstevel@tonic-gate 
727b51bbbf5SVikram Hegde 	if (dip != ddi_root_node())
7287c478bd9Sstevel@tonic-gate 		dip = (dev_info_t *)DEVI(dip)->devi_bus_dma_ctl;
7297c478bd9Sstevel@tonic-gate 	fp = DEVI(dip)->devi_ops->devo_bus_ops->bus_dma_ctl;
7307c478bd9Sstevel@tonic-gate 	return ((*fp) (dip, rdip, handle, request, offp, lenp, objp, flags));
7317c478bd9Sstevel@tonic-gate }
7327c478bd9Sstevel@tonic-gate #endif
7337c478bd9Sstevel@tonic-gate 
7347c478bd9Sstevel@tonic-gate /*
7357c478bd9Sstevel@tonic-gate  * For all DMA control functions, call the DMA control
7367c478bd9Sstevel@tonic-gate  * routine and return status.
7377c478bd9Sstevel@tonic-gate  *
7387c478bd9Sstevel@tonic-gate  * Just plain assume that the parent is to be called.
7397c478bd9Sstevel@tonic-gate  * If a nexus driver or a thread outside the framework
7407c478bd9Sstevel@tonic-gate  * of a nexus driver or a leaf driver calls these functions,
7417c478bd9Sstevel@tonic-gate  * it is up to them to deal with the fact that the parent's
7427c478bd9Sstevel@tonic-gate  * bus_dma_ctl function will be the first one called.
7437c478bd9Sstevel@tonic-gate  */
7447c478bd9Sstevel@tonic-gate 
7457c478bd9Sstevel@tonic-gate #define	HD	((ddi_dma_impl_t *)h)->dmai_rdip
7467c478bd9Sstevel@tonic-gate 
74715e1afcdSRichard Lowe /*
748cd21e7c5SGarrett D'Amore  * This routine is left in place to satisfy link dependencies
749cd21e7c5SGarrett D'Amore  * for any 3rd party nexus drivers that rely on it.  It is never
750cd21e7c5SGarrett D'Amore  * called, though.
75115e1afcdSRichard Lowe  */
752cd21e7c5SGarrett D'Amore /*ARGSUSED*/
75315e1afcdSRichard Lowe int
754cd21e7c5SGarrett D'Amore ddi_dma_map(dev_info_t *dip, dev_info_t *rdip,
755cd21e7c5SGarrett D'Amore     struct ddi_dma_req *dmareqp, ddi_dma_handle_t *handlep)
75615e1afcdSRichard Lowe {
757cd21e7c5SGarrett D'Amore 	return (DDI_FAILURE);
75815e1afcdSRichard Lowe }
75915e1afcdSRichard Lowe 
7607c478bd9Sstevel@tonic-gate #if !defined(__sparc)
7617c478bd9Sstevel@tonic-gate 
7627c478bd9Sstevel@tonic-gate /*
7637c478bd9Sstevel@tonic-gate  * The SPARC versions of these routines are done in assembler to
7647c478bd9Sstevel@tonic-gate  * save register windows, so they're in sparc_subr.s.
7657c478bd9Sstevel@tonic-gate  */
7667c478bd9Sstevel@tonic-gate 
7677c478bd9Sstevel@tonic-gate int
7687c478bd9Sstevel@tonic-gate ddi_dma_allochdl(dev_info_t *dip, dev_info_t *rdip, ddi_dma_attr_t *attr,
7697c478bd9Sstevel@tonic-gate     int (*waitfp)(caddr_t), caddr_t arg, ddi_dma_handle_t *handlep)
7707c478bd9Sstevel@tonic-gate {
7717c478bd9Sstevel@tonic-gate 	int (*funcp)(dev_info_t *, dev_info_t *, ddi_dma_attr_t *,
7727c478bd9Sstevel@tonic-gate 	    int (*)(caddr_t), caddr_t, ddi_dma_handle_t *);
7737c478bd9Sstevel@tonic-gate 
774b51bbbf5SVikram Hegde 	if (dip != ddi_root_node())
775b51bbbf5SVikram Hegde 		dip = (dev_info_t *)DEVI(dip)->devi_bus_dma_allochdl;
7767c478bd9Sstevel@tonic-gate 
777b51bbbf5SVikram Hegde 	funcp = DEVI(dip)->devi_ops->devo_bus_ops->bus_dma_allochdl;
778b51bbbf5SVikram Hegde 	return ((*funcp)(dip, rdip, attr, waitfp, arg, handlep));
7797c478bd9Sstevel@tonic-gate }
7807c478bd9Sstevel@tonic-gate 
7817c478bd9Sstevel@tonic-gate int
7827c478bd9Sstevel@tonic-gate ddi_dma_freehdl(dev_info_t *dip, dev_info_t *rdip, ddi_dma_handle_t handlep)
7837c478bd9Sstevel@tonic-gate {
7847c478bd9Sstevel@tonic-gate 	int (*funcp)(dev_info_t *, dev_info_t *, ddi_dma_handle_t);
7857c478bd9Sstevel@tonic-gate 
786b51bbbf5SVikram Hegde 	if (dip != ddi_root_node())
787b51bbbf5SVikram Hegde 		dip = (dev_info_t *)DEVI(dip)->devi_bus_dma_allochdl;
7887c478bd9Sstevel@tonic-gate 
789b51bbbf5SVikram Hegde 	funcp = DEVI(dip)->devi_ops->devo_bus_ops->bus_dma_freehdl;
790b51bbbf5SVikram Hegde 	return ((*funcp)(dip, rdip, handlep));
7917c478bd9Sstevel@tonic-gate }
7927c478bd9Sstevel@tonic-gate 
7937c478bd9Sstevel@tonic-gate int
7947c478bd9Sstevel@tonic-gate ddi_dma_bindhdl(dev_info_t *dip, dev_info_t *rdip,
7957c478bd9Sstevel@tonic-gate     ddi_dma_handle_t handle, struct ddi_dma_req *dmareq,
7967c478bd9Sstevel@tonic-gate     ddi_dma_cookie_t *cp, uint_t *ccountp)
7977c478bd9Sstevel@tonic-gate {
7987c478bd9Sstevel@tonic-gate 	int (*funcp)(dev_info_t *, dev_info_t *, ddi_dma_handle_t,
7997c478bd9Sstevel@tonic-gate 	    struct ddi_dma_req *, ddi_dma_cookie_t *, uint_t *);
8007c478bd9Sstevel@tonic-gate 
801b51bbbf5SVikram Hegde 	if (dip != ddi_root_node())
802b51bbbf5SVikram Hegde 		dip = (dev_info_t *)DEVI(dip)->devi_bus_dma_bindhdl;
8037c478bd9Sstevel@tonic-gate 
804b51bbbf5SVikram Hegde 	funcp = DEVI(dip)->devi_ops->devo_bus_ops->bus_dma_bindhdl;
805b51bbbf5SVikram Hegde 	return ((*funcp)(dip, rdip, handle, dmareq, cp, ccountp));
8067c478bd9Sstevel@tonic-gate }
8077c478bd9Sstevel@tonic-gate 
8087c478bd9Sstevel@tonic-gate int
8097c478bd9Sstevel@tonic-gate ddi_dma_unbindhdl(dev_info_t *dip, dev_info_t *rdip,
8107c478bd9Sstevel@tonic-gate     ddi_dma_handle_t handle)
8117c478bd9Sstevel@tonic-gate {
8127c478bd9Sstevel@tonic-gate 	int (*funcp)(dev_info_t *, dev_info_t *, ddi_dma_handle_t);
8137c478bd9Sstevel@tonic-gate 
814b51bbbf5SVikram Hegde 	if (dip != ddi_root_node())
815b51bbbf5SVikram Hegde 		dip = (dev_info_t *)DEVI(dip)->devi_bus_dma_unbindhdl;
8167c478bd9Sstevel@tonic-gate 
817b51bbbf5SVikram Hegde 	funcp = DEVI(dip)->devi_ops->devo_bus_ops->bus_dma_unbindhdl;
818b51bbbf5SVikram Hegde 	return ((*funcp)(dip, rdip, handle));
8197c478bd9Sstevel@tonic-gate }
8207c478bd9Sstevel@tonic-gate 
8217c478bd9Sstevel@tonic-gate 
8227c478bd9Sstevel@tonic-gate int
8237c478bd9Sstevel@tonic-gate ddi_dma_flush(dev_info_t *dip, dev_info_t *rdip,
8247c478bd9Sstevel@tonic-gate     ddi_dma_handle_t handle, off_t off, size_t len,
8257c478bd9Sstevel@tonic-gate     uint_t cache_flags)
8267c478bd9Sstevel@tonic-gate {
8277c478bd9Sstevel@tonic-gate 	int (*funcp)(dev_info_t *, dev_info_t *, ddi_dma_handle_t,
8287c478bd9Sstevel@tonic-gate 	    off_t, size_t, uint_t);
8297c478bd9Sstevel@tonic-gate 
830b51bbbf5SVikram Hegde 	if (dip != ddi_root_node())
831b51bbbf5SVikram Hegde 		dip = (dev_info_t *)DEVI(dip)->devi_bus_dma_flush;
8327c478bd9Sstevel@tonic-gate 
833b51bbbf5SVikram Hegde 	funcp = DEVI(dip)->devi_ops->devo_bus_ops->bus_dma_flush;
834b51bbbf5SVikram Hegde 	return ((*funcp)(dip, rdip, handle, off, len, cache_flags));
8357c478bd9Sstevel@tonic-gate }
8367c478bd9Sstevel@tonic-gate 
8377c478bd9Sstevel@tonic-gate int
8387c478bd9Sstevel@tonic-gate ddi_dma_win(dev_info_t *dip, dev_info_t *rdip,
8397c478bd9Sstevel@tonic-gate     ddi_dma_handle_t handle, uint_t win, off_t *offp,
8407c478bd9Sstevel@tonic-gate     size_t *lenp, ddi_dma_cookie_t *cookiep, uint_t *ccountp)
8417c478bd9Sstevel@tonic-gate {
8427c478bd9Sstevel@tonic-gate 	int (*funcp)(dev_info_t *, dev_info_t *, ddi_dma_handle_t,
8437c478bd9Sstevel@tonic-gate 	    uint_t, off_t *, size_t *, ddi_dma_cookie_t *, uint_t *);
8447c478bd9Sstevel@tonic-gate 
845b51bbbf5SVikram Hegde 	if (dip != ddi_root_node())
846b51bbbf5SVikram Hegde 		dip = (dev_info_t *)DEVI(dip)->devi_bus_dma_win;
8477c478bd9Sstevel@tonic-gate 
848b51bbbf5SVikram Hegde 	funcp = DEVI(dip)->devi_ops->devo_bus_ops->bus_dma_win;
849b51bbbf5SVikram Hegde 	return ((*funcp)(dip, rdip, handle, win, offp, lenp,
8507c478bd9Sstevel@tonic-gate 	    cookiep, ccountp));
8517c478bd9Sstevel@tonic-gate }
8527c478bd9Sstevel@tonic-gate 
8537c478bd9Sstevel@tonic-gate int
8547c478bd9Sstevel@tonic-gate ddi_dma_sync(ddi_dma_handle_t h, off_t o, size_t l, uint_t whom)
8557c478bd9Sstevel@tonic-gate {
8567c478bd9Sstevel@tonic-gate 	ddi_dma_impl_t *hp = (ddi_dma_impl_t *)h;
857b51bbbf5SVikram Hegde 	dev_info_t *dip, *rdip;
8587c478bd9Sstevel@tonic-gate 	int (*funcp)(dev_info_t *, dev_info_t *, ddi_dma_handle_t, off_t,
8597c478bd9Sstevel@tonic-gate 	    size_t, uint_t);
8607c478bd9Sstevel@tonic-gate 
8617c478bd9Sstevel@tonic-gate 	/*
8627c478bd9Sstevel@tonic-gate 	 * the DMA nexus driver will set DMP_NOSYNC if the
8637c478bd9Sstevel@tonic-gate 	 * platform does not require any sync operation. For
8647c478bd9Sstevel@tonic-gate 	 * example if the memory is uncached or consistent
8657c478bd9Sstevel@tonic-gate 	 * and without any I/O write buffers involved.
8667c478bd9Sstevel@tonic-gate 	 */
8677c478bd9Sstevel@tonic-gate 	if ((hp->dmai_rflags & DMP_NOSYNC) == DMP_NOSYNC)
8687c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
8697c478bd9Sstevel@tonic-gate 
870b51bbbf5SVikram Hegde 	dip = rdip = hp->dmai_rdip;
871b51bbbf5SVikram Hegde 	if (dip != ddi_root_node())
872b51bbbf5SVikram Hegde 		dip = (dev_info_t *)DEVI(dip)->devi_bus_dma_flush;
873b51bbbf5SVikram Hegde 	funcp = DEVI(dip)->devi_ops->devo_bus_ops->bus_dma_flush;
874b51bbbf5SVikram Hegde 	return ((*funcp)(dip, rdip, h, o, l, whom));
8757c478bd9Sstevel@tonic-gate }
8767c478bd9Sstevel@tonic-gate 
8777c478bd9Sstevel@tonic-gate int
8787c478bd9Sstevel@tonic-gate ddi_dma_unbind_handle(ddi_dma_handle_t h)
8797c478bd9Sstevel@tonic-gate {
8807c478bd9Sstevel@tonic-gate 	ddi_dma_impl_t *hp = (ddi_dma_impl_t *)h;
881b51bbbf5SVikram Hegde 	dev_info_t *dip, *rdip;
8827c478bd9Sstevel@tonic-gate 	int (*funcp)(dev_info_t *, dev_info_t *, ddi_dma_handle_t);
8837c478bd9Sstevel@tonic-gate 
884b51bbbf5SVikram Hegde 	dip = rdip = hp->dmai_rdip;
885b51bbbf5SVikram Hegde 	if (dip != ddi_root_node())
886b51bbbf5SVikram Hegde 		dip = (dev_info_t *)DEVI(dip)->devi_bus_dma_unbindhdl;
887b51bbbf5SVikram Hegde 	funcp = DEVI(rdip)->devi_bus_dma_unbindfunc;
888b51bbbf5SVikram Hegde 	return ((*funcp)(dip, rdip, h));
8897c478bd9Sstevel@tonic-gate }
8907c478bd9Sstevel@tonic-gate 
8917c478bd9Sstevel@tonic-gate #endif	/* !__sparc */
8927c478bd9Sstevel@tonic-gate 
8937c478bd9Sstevel@tonic-gate /*
894cd21e7c5SGarrett D'Amore  * DMA burst sizes, and transfer minimums
8957c478bd9Sstevel@tonic-gate  */
8967c478bd9Sstevel@tonic-gate 
8977c478bd9Sstevel@tonic-gate int
8987c478bd9Sstevel@tonic-gate ddi_dma_burstsizes(ddi_dma_handle_t handle)
8997c478bd9Sstevel@tonic-gate {
9007c478bd9Sstevel@tonic-gate 	ddi_dma_impl_t *dimp = (ddi_dma_impl_t *)handle;
9017c478bd9Sstevel@tonic-gate 
9027c478bd9Sstevel@tonic-gate 	if (!dimp)
9037c478bd9Sstevel@tonic-gate 		return (0);
9047c478bd9Sstevel@tonic-gate 	else
9057c478bd9Sstevel@tonic-gate 		return (dimp->dmai_burstsizes);
9067c478bd9Sstevel@tonic-gate }
9077c478bd9Sstevel@tonic-gate 
9087c478bd9Sstevel@tonic-gate /*
9097c478bd9Sstevel@tonic-gate  * Given two DMA attribute structures, apply the attributes
9107c478bd9Sstevel@tonic-gate  * of one to the other, following the rules of attributes
9117c478bd9Sstevel@tonic-gate  * and the wishes of the caller.
9127c478bd9Sstevel@tonic-gate  *
9137c478bd9Sstevel@tonic-gate  * The rules of DMA attribute structures are that you cannot
9147c478bd9Sstevel@tonic-gate  * make things *less* restrictive as you apply one set
9157c478bd9Sstevel@tonic-gate  * of attributes to another.
9167c478bd9Sstevel@tonic-gate  *
9177c478bd9Sstevel@tonic-gate  */
9187c478bd9Sstevel@tonic-gate void
9197c478bd9Sstevel@tonic-gate ddi_dma_attr_merge(ddi_dma_attr_t *attr, ddi_dma_attr_t *mod)
9207c478bd9Sstevel@tonic-gate {
9217c478bd9Sstevel@tonic-gate 	attr->dma_attr_addr_lo =
9227c478bd9Sstevel@tonic-gate 	    MAX(attr->dma_attr_addr_lo, mod->dma_attr_addr_lo);
9237c478bd9Sstevel@tonic-gate 	attr->dma_attr_addr_hi =
9247c478bd9Sstevel@tonic-gate 	    MIN(attr->dma_attr_addr_hi, mod->dma_attr_addr_hi);
9257c478bd9Sstevel@tonic-gate 	attr->dma_attr_count_max =
9267c478bd9Sstevel@tonic-gate 	    MIN(attr->dma_attr_count_max, mod->dma_attr_count_max);
9277c478bd9Sstevel@tonic-gate 	attr->dma_attr_align =
9287c478bd9Sstevel@tonic-gate 	    MAX(attr->dma_attr_align,  mod->dma_attr_align);
9297c478bd9Sstevel@tonic-gate 	attr->dma_attr_burstsizes =
9307c478bd9Sstevel@tonic-gate 	    (uint_t)(attr->dma_attr_burstsizes & mod->dma_attr_burstsizes);
9317c478bd9Sstevel@tonic-gate 	attr->dma_attr_minxfer =
9327c478bd9Sstevel@tonic-gate 	    maxbit(attr->dma_attr_minxfer, mod->dma_attr_minxfer);
9337c478bd9Sstevel@tonic-gate 	attr->dma_attr_maxxfer =
9347c478bd9Sstevel@tonic-gate 	    MIN(attr->dma_attr_maxxfer, mod->dma_attr_maxxfer);
9357c478bd9Sstevel@tonic-gate 	attr->dma_attr_seg = MIN(attr->dma_attr_seg, mod->dma_attr_seg);
9367c478bd9Sstevel@tonic-gate 	attr->dma_attr_sgllen = MIN((uint_t)attr->dma_attr_sgllen,
9377c478bd9Sstevel@tonic-gate 	    (uint_t)mod->dma_attr_sgllen);
9387c478bd9Sstevel@tonic-gate 	attr->dma_attr_granular =
9397c478bd9Sstevel@tonic-gate 	    MAX(attr->dma_attr_granular, mod->dma_attr_granular);
9407c478bd9Sstevel@tonic-gate }
9417c478bd9Sstevel@tonic-gate 
9427c478bd9Sstevel@tonic-gate /*
9437c478bd9Sstevel@tonic-gate  * mmap/segmap interface:
9447c478bd9Sstevel@tonic-gate  */
9457c478bd9Sstevel@tonic-gate 
9467c478bd9Sstevel@tonic-gate /*
9477c478bd9Sstevel@tonic-gate  * ddi_segmap:		setup the default segment driver. Calls the drivers
9487c478bd9Sstevel@tonic-gate  *			XXmmap routine to validate the range to be mapped.
9497c478bd9Sstevel@tonic-gate  *			Return ENXIO of the range is not valid.  Create
9507c478bd9Sstevel@tonic-gate  *			a seg_dev segment that contains all of the
9517c478bd9Sstevel@tonic-gate  *			necessary information and will reference the
9527c478bd9Sstevel@tonic-gate  *			default segment driver routines. It returns zero
9537c478bd9Sstevel@tonic-gate  *			on success or non-zero on failure.
9547c478bd9Sstevel@tonic-gate  */
9557c478bd9Sstevel@tonic-gate int
9567c478bd9Sstevel@tonic-gate ddi_segmap(dev_t dev, off_t offset, struct as *asp, caddr_t *addrp, off_t len,
9577c478bd9Sstevel@tonic-gate     uint_t prot, uint_t maxprot, uint_t flags, cred_t *credp)
9587c478bd9Sstevel@tonic-gate {
9597c478bd9Sstevel@tonic-gate 	extern int spec_segmap(dev_t, off_t, struct as *, caddr_t *,
9607c478bd9Sstevel@tonic-gate 	    off_t, uint_t, uint_t, uint_t, struct cred *);
9617c478bd9Sstevel@tonic-gate 
9627c478bd9Sstevel@tonic-gate 	return (spec_segmap(dev, offset, asp, addrp, len,
9637c478bd9Sstevel@tonic-gate 	    prot, maxprot, flags, credp));
9647c478bd9Sstevel@tonic-gate }
9657c478bd9Sstevel@tonic-gate 
9667c478bd9Sstevel@tonic-gate /*
9677c478bd9Sstevel@tonic-gate  * ddi_map_fault:	Resolve mappings at fault time.  Used by segment
9687c478bd9Sstevel@tonic-gate  *			drivers. Allows each successive parent to resolve
9697c478bd9Sstevel@tonic-gate  *			address translations and add its mappings to the
9707c478bd9Sstevel@tonic-gate  *			mapping list supplied in the page structure. It
9717c478bd9Sstevel@tonic-gate  *			returns zero on success	or non-zero on failure.
9727c478bd9Sstevel@tonic-gate  */
9737c478bd9Sstevel@tonic-gate 
9747c478bd9Sstevel@tonic-gate int
9757c478bd9Sstevel@tonic-gate ddi_map_fault(dev_info_t *dip, struct hat *hat, struct seg *seg,
9767c478bd9Sstevel@tonic-gate     caddr_t addr, struct devpage *dp, pfn_t pfn, uint_t prot, uint_t lock)
9777c478bd9Sstevel@tonic-gate {
9787c478bd9Sstevel@tonic-gate 	return (i_ddi_map_fault(dip, dip, hat, seg, addr, dp, pfn, prot, lock));
9797c478bd9Sstevel@tonic-gate }
9807c478bd9Sstevel@tonic-gate 
9817c478bd9Sstevel@tonic-gate /*
9827c478bd9Sstevel@tonic-gate  * ddi_device_mapping_check:	Called from ddi_segmap_setup.
9837c478bd9Sstevel@tonic-gate  *	Invokes platform specific DDI to determine whether attributes specified
9847c478bd9Sstevel@tonic-gate  *	in attr(9s) are	valid for the region of memory that will be made
9857c478bd9Sstevel@tonic-gate  *	available for direct access to user process via the mmap(2) system call.
9867c478bd9Sstevel@tonic-gate  */
9877c478bd9Sstevel@tonic-gate int
9887c478bd9Sstevel@tonic-gate ddi_device_mapping_check(dev_t dev, ddi_device_acc_attr_t *accattrp,
9897c478bd9Sstevel@tonic-gate     uint_t rnumber, uint_t *hat_flags)
9907c478bd9Sstevel@tonic-gate {
9917c478bd9Sstevel@tonic-gate 	ddi_acc_handle_t handle;
9927c478bd9Sstevel@tonic-gate 	ddi_map_req_t mr;
9937c478bd9Sstevel@tonic-gate 	ddi_acc_hdl_t *hp;
9947c478bd9Sstevel@tonic-gate 	int result;
9957c478bd9Sstevel@tonic-gate 	dev_info_t *dip;
9967c478bd9Sstevel@tonic-gate 
9977c478bd9Sstevel@tonic-gate 	/*
9987c478bd9Sstevel@tonic-gate 	 * we use e_ddi_hold_devi_by_dev to search for the devi.  We
9997c478bd9Sstevel@tonic-gate 	 * release it immediately since it should already be held by
10007c478bd9Sstevel@tonic-gate 	 * a devfs vnode.
10017c478bd9Sstevel@tonic-gate 	 */
10027c478bd9Sstevel@tonic-gate 	if ((dip =
10037c478bd9Sstevel@tonic-gate 	    e_ddi_hold_devi_by_dev(dev, E_DDI_HOLD_DEVI_NOATTACH)) == NULL)
10047c478bd9Sstevel@tonic-gate 		return (-1);
10057c478bd9Sstevel@tonic-gate 	ddi_release_devi(dip);		/* for e_ddi_hold_devi_by_dev() */
10067c478bd9Sstevel@tonic-gate 
10077c478bd9Sstevel@tonic-gate 	/*
10087c478bd9Sstevel@tonic-gate 	 * Allocate and initialize the common elements of data
10097c478bd9Sstevel@tonic-gate 	 * access handle.
10107c478bd9Sstevel@tonic-gate 	 */
10117c478bd9Sstevel@tonic-gate 	handle = impl_acc_hdl_alloc(KM_SLEEP, NULL);
10127c478bd9Sstevel@tonic-gate 	if (handle == NULL)
10137c478bd9Sstevel@tonic-gate 		return (-1);
10147c478bd9Sstevel@tonic-gate 
10157c478bd9Sstevel@tonic-gate 	hp = impl_acc_hdl_get(handle);
10167c478bd9Sstevel@tonic-gate 	hp->ah_vers = VERS_ACCHDL;
10177c478bd9Sstevel@tonic-gate 	hp->ah_dip = dip;
10187c478bd9Sstevel@tonic-gate 	hp->ah_rnumber = rnumber;
10197c478bd9Sstevel@tonic-gate 	hp->ah_offset = 0;
10207c478bd9Sstevel@tonic-gate 	hp->ah_len = 0;
10217c478bd9Sstevel@tonic-gate 	hp->ah_acc = *accattrp;
10227c478bd9Sstevel@tonic-gate 
10237c478bd9Sstevel@tonic-gate 	/*
10247c478bd9Sstevel@tonic-gate 	 * Set up the mapping request and call to parent.
10257c478bd9Sstevel@tonic-gate 	 */
10267c478bd9Sstevel@tonic-gate 	mr.map_op = DDI_MO_MAP_HANDLE;
10277c478bd9Sstevel@tonic-gate 	mr.map_type = DDI_MT_RNUMBER;
10287c478bd9Sstevel@tonic-gate 	mr.map_obj.rnumber = rnumber;
10297c478bd9Sstevel@tonic-gate 	mr.map_prot = PROT_READ | PROT_WRITE;
10307c478bd9Sstevel@tonic-gate 	mr.map_flags = DDI_MF_KERNEL_MAPPING;
10317c478bd9Sstevel@tonic-gate 	mr.map_handlep = hp;
10327c478bd9Sstevel@tonic-gate 	mr.map_vers = DDI_MAP_VERSION;
10337c478bd9Sstevel@tonic-gate 	result = ddi_map(dip, &mr, 0, 0, NULL);
10347c478bd9Sstevel@tonic-gate 
10357c478bd9Sstevel@tonic-gate 	/*
10367c478bd9Sstevel@tonic-gate 	 * Region must be mappable, pick up flags from the framework.
10377c478bd9Sstevel@tonic-gate 	 */
10387c478bd9Sstevel@tonic-gate 	*hat_flags = hp->ah_hat_flags;
10397c478bd9Sstevel@tonic-gate 
10407c478bd9Sstevel@tonic-gate 	impl_acc_hdl_free(handle);
10417c478bd9Sstevel@tonic-gate 
10427c478bd9Sstevel@tonic-gate 	/*
10437c478bd9Sstevel@tonic-gate 	 * check for end result.
10447c478bd9Sstevel@tonic-gate 	 */
10457c478bd9Sstevel@tonic-gate 	if (result != DDI_SUCCESS)
10467c478bd9Sstevel@tonic-gate 		return (-1);
10477c478bd9Sstevel@tonic-gate 	return (0);
10487c478bd9Sstevel@tonic-gate }
10497c478bd9Sstevel@tonic-gate 
10507c478bd9Sstevel@tonic-gate 
10517c478bd9Sstevel@tonic-gate /*
10527c478bd9Sstevel@tonic-gate  * Property functions:	 See also, ddipropdefs.h.
10537c478bd9Sstevel@tonic-gate  *
10547c478bd9Sstevel@tonic-gate  * These functions are the framework for the property functions,
10557c478bd9Sstevel@tonic-gate  * i.e. they support software defined properties.  All implementation
10567c478bd9Sstevel@tonic-gate  * specific property handling (i.e.: self-identifying devices and
10577c478bd9Sstevel@tonic-gate  * PROM defined properties are handled in the implementation specific
10587c478bd9Sstevel@tonic-gate  * functions (defined in ddi_implfuncs.h).
10597c478bd9Sstevel@tonic-gate  */
10607c478bd9Sstevel@tonic-gate 
10617c478bd9Sstevel@tonic-gate /*
10627c478bd9Sstevel@tonic-gate  * nopropop:	Shouldn't be called, right?
10637c478bd9Sstevel@tonic-gate  */
10647c478bd9Sstevel@tonic-gate int
10657c478bd9Sstevel@tonic-gate nopropop(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, int mod_flags,
10667c478bd9Sstevel@tonic-gate     char *name, caddr_t valuep, int *lengthp)
10677c478bd9Sstevel@tonic-gate {
10687c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(dev, dip, prop_op, mod_flags, name, valuep, lengthp))
10697c478bd9Sstevel@tonic-gate 	return (DDI_PROP_NOT_FOUND);
10707c478bd9Sstevel@tonic-gate }
10717c478bd9Sstevel@tonic-gate 
10727c478bd9Sstevel@tonic-gate #ifdef	DDI_PROP_DEBUG
10737c478bd9Sstevel@tonic-gate int ddi_prop_debug_flag = 0;
10747c478bd9Sstevel@tonic-gate 
10757c478bd9Sstevel@tonic-gate int
10767c478bd9Sstevel@tonic-gate ddi_prop_debug(int enable)
10777c478bd9Sstevel@tonic-gate {
10787c478bd9Sstevel@tonic-gate 	int prev = ddi_prop_debug_flag;
10797c478bd9Sstevel@tonic-gate 
10807c478bd9Sstevel@tonic-gate 	if ((enable != 0) || (prev != 0))
10817c478bd9Sstevel@tonic-gate 		printf("ddi_prop_debug: debugging %s\n",
10827c478bd9Sstevel@tonic-gate 		    enable ? "enabled" : "disabled");
10837c478bd9Sstevel@tonic-gate 	ddi_prop_debug_flag = enable;
10847c478bd9Sstevel@tonic-gate 	return (prev);
10857c478bd9Sstevel@tonic-gate }
10867c478bd9Sstevel@tonic-gate 
10877c478bd9Sstevel@tonic-gate #endif	/* DDI_PROP_DEBUG */
10887c478bd9Sstevel@tonic-gate 
10897c478bd9Sstevel@tonic-gate /*
10907c478bd9Sstevel@tonic-gate  * Search a property list for a match, if found return pointer
10917c478bd9Sstevel@tonic-gate  * to matching prop struct, else return NULL.
10927c478bd9Sstevel@tonic-gate  */
10937c478bd9Sstevel@tonic-gate 
10947c478bd9Sstevel@tonic-gate ddi_prop_t *
10957c478bd9Sstevel@tonic-gate i_ddi_prop_search(dev_t dev, char *name, uint_t flags, ddi_prop_t **list_head)
10967c478bd9Sstevel@tonic-gate {
10977c478bd9Sstevel@tonic-gate 	ddi_prop_t	*propp;
10987c478bd9Sstevel@tonic-gate 
10997c478bd9Sstevel@tonic-gate 	/*
11007c478bd9Sstevel@tonic-gate 	 * find the property in child's devinfo:
11019ac2ddeaScth 	 * Search order defined by this search function is first matching
11029ac2ddeaScth 	 * property with input dev == DDI_DEV_T_ANY matching any dev or
11039ac2ddeaScth 	 * dev == propp->prop_dev, name == propp->name, and the correct
11049ac2ddeaScth 	 * data type as specified in the flags.  If a DDI_DEV_T_NONE dev
11059ac2ddeaScth 	 * value made it this far then it implies a DDI_DEV_T_ANY search.
11067c478bd9Sstevel@tonic-gate 	 */
11079ac2ddeaScth 	if (dev == DDI_DEV_T_NONE)
11089ac2ddeaScth 		dev = DDI_DEV_T_ANY;
11097c478bd9Sstevel@tonic-gate 
11107c478bd9Sstevel@tonic-gate 	for (propp = *list_head; propp != NULL; propp = propp->prop_next)  {
11117c478bd9Sstevel@tonic-gate 
11122cd7878fScth 		if (!DDI_STRSAME(propp->prop_name, name))
11137c478bd9Sstevel@tonic-gate 			continue;
11147c478bd9Sstevel@tonic-gate 
11157c478bd9Sstevel@tonic-gate 		if ((dev != DDI_DEV_T_ANY) && (propp->prop_dev != dev))
11167c478bd9Sstevel@tonic-gate 			continue;
11177c478bd9Sstevel@tonic-gate 
11187c478bd9Sstevel@tonic-gate 		if (((propp->prop_flags & flags) & DDI_PROP_TYPE_MASK) == 0)
11197c478bd9Sstevel@tonic-gate 			continue;
11207c478bd9Sstevel@tonic-gate 
11217c478bd9Sstevel@tonic-gate 		return (propp);
11227c478bd9Sstevel@tonic-gate 	}
11237c478bd9Sstevel@tonic-gate 
11247c478bd9Sstevel@tonic-gate 	return ((ddi_prop_t *)0);
11257c478bd9Sstevel@tonic-gate }
11267c478bd9Sstevel@tonic-gate 
11277c478bd9Sstevel@tonic-gate /*
11287c478bd9Sstevel@tonic-gate  * Search for property within devnames structures
11297c478bd9Sstevel@tonic-gate  */
11307c478bd9Sstevel@tonic-gate ddi_prop_t *
11317c478bd9Sstevel@tonic-gate i_ddi_search_global_prop(dev_t dev, char *name, uint_t flags)
11327c478bd9Sstevel@tonic-gate {
11337c478bd9Sstevel@tonic-gate 	major_t		major;
11347c478bd9Sstevel@tonic-gate 	struct devnames	*dnp;
11357c478bd9Sstevel@tonic-gate 	ddi_prop_t	*propp;
11367c478bd9Sstevel@tonic-gate 
11377c478bd9Sstevel@tonic-gate 	/*
11387c478bd9Sstevel@tonic-gate 	 * Valid dev_t value is needed to index into the
11397c478bd9Sstevel@tonic-gate 	 * correct devnames entry, therefore a dev_t
11407c478bd9Sstevel@tonic-gate 	 * value of DDI_DEV_T_ANY is not appropriate.
11417c478bd9Sstevel@tonic-gate 	 */
11427c478bd9Sstevel@tonic-gate 	ASSERT(dev != DDI_DEV_T_ANY);
11437c478bd9Sstevel@tonic-gate 	if (dev == DDI_DEV_T_ANY) {
11447c478bd9Sstevel@tonic-gate 		return ((ddi_prop_t *)0);
11457c478bd9Sstevel@tonic-gate 	}
11467c478bd9Sstevel@tonic-gate 
11477c478bd9Sstevel@tonic-gate 	major = getmajor(dev);
11487c478bd9Sstevel@tonic-gate 	dnp = &(devnamesp[major]);
11497c478bd9Sstevel@tonic-gate 
11507c478bd9Sstevel@tonic-gate 	if (dnp->dn_global_prop_ptr == NULL)
11517c478bd9Sstevel@tonic-gate 		return ((ddi_prop_t *)0);
11527c478bd9Sstevel@tonic-gate 
11537c478bd9Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
11547c478bd9Sstevel@tonic-gate 
11557c478bd9Sstevel@tonic-gate 	for (propp = dnp->dn_global_prop_ptr->prop_list;
11567c478bd9Sstevel@tonic-gate 	    propp != NULL;
11577c478bd9Sstevel@tonic-gate 	    propp = (ddi_prop_t *)propp->prop_next) {
11587c478bd9Sstevel@tonic-gate 
11592cd7878fScth 		if (!DDI_STRSAME(propp->prop_name, name))
11607c478bd9Sstevel@tonic-gate 			continue;
11617c478bd9Sstevel@tonic-gate 
116265cf7c95SVikram Hegde 		if ((!(flags & DDI_PROP_ROOTNEX_GLOBAL)) &&
116365cf7c95SVikram Hegde 		    (!(flags & LDI_DEV_T_ANY)) && (propp->prop_dev != dev))
11647c478bd9Sstevel@tonic-gate 			continue;
11657c478bd9Sstevel@tonic-gate 
11667c478bd9Sstevel@tonic-gate 		if (((propp->prop_flags & flags) & DDI_PROP_TYPE_MASK) == 0)
11677c478bd9Sstevel@tonic-gate 			continue;
11687c478bd9Sstevel@tonic-gate 
11697c478bd9Sstevel@tonic-gate 		/* Property found, return it */
11707c478bd9Sstevel@tonic-gate 		UNLOCK_DEV_OPS(&dnp->dn_lock);
11717c478bd9Sstevel@tonic-gate 		return (propp);
11727c478bd9Sstevel@tonic-gate 	}
11737c478bd9Sstevel@tonic-gate 
11747c478bd9Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
11757c478bd9Sstevel@tonic-gate 	return ((ddi_prop_t *)0);
11767c478bd9Sstevel@tonic-gate }
11777c478bd9Sstevel@tonic-gate 
11787c478bd9Sstevel@tonic-gate static char prop_no_mem_msg[] = "can't allocate memory for ddi property <%s>";
11797c478bd9Sstevel@tonic-gate 
11807c478bd9Sstevel@tonic-gate /*
11817c478bd9Sstevel@tonic-gate  * ddi_prop_search_global:
11827c478bd9Sstevel@tonic-gate  *	Search the global property list within devnames
11837c478bd9Sstevel@tonic-gate  *	for the named property.  Return the encoded value.
11847c478bd9Sstevel@tonic-gate  */
11857c478bd9Sstevel@tonic-gate static int
11867c478bd9Sstevel@tonic-gate i_ddi_prop_search_global(dev_t dev, uint_t flags, char *name,
11877c478bd9Sstevel@tonic-gate     void *valuep, uint_t *lengthp)
11887c478bd9Sstevel@tonic-gate {
11897c478bd9Sstevel@tonic-gate 	ddi_prop_t	*propp;
11907c478bd9Sstevel@tonic-gate 	caddr_t		buffer;
11917c478bd9Sstevel@tonic-gate 
11927c478bd9Sstevel@tonic-gate 	propp =  i_ddi_search_global_prop(dev, name, flags);
11937c478bd9Sstevel@tonic-gate 
11947c478bd9Sstevel@tonic-gate 	/* Property NOT found, bail */
11957c478bd9Sstevel@tonic-gate 	if (propp == (ddi_prop_t *)0)
11967c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
11977c478bd9Sstevel@tonic-gate 
11987c478bd9Sstevel@tonic-gate 	if (propp->prop_flags & DDI_PROP_UNDEF_IT)
11997c478bd9Sstevel@tonic-gate 		return (DDI_PROP_UNDEFINED);
12007c478bd9Sstevel@tonic-gate 
12013b756dd3Scth 	if ((buffer = kmem_alloc(propp->prop_len,
12023b756dd3Scth 	    (flags & DDI_PROP_CANSLEEP) ? KM_SLEEP : KM_NOSLEEP)) == NULL) {
12037c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, prop_no_mem_msg, name);
12047c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NO_MEMORY);
12057c478bd9Sstevel@tonic-gate 	}
12067c478bd9Sstevel@tonic-gate 
12077c478bd9Sstevel@tonic-gate 	/*
12087c478bd9Sstevel@tonic-gate 	 * Return the encoded data
12097c478bd9Sstevel@tonic-gate 	 */
12107c478bd9Sstevel@tonic-gate 	*(caddr_t *)valuep = buffer;
12117c478bd9Sstevel@tonic-gate 	*lengthp = propp->prop_len;
12127c478bd9Sstevel@tonic-gate 	bcopy(propp->prop_val, buffer, propp->prop_len);
12137c478bd9Sstevel@tonic-gate 
12147c478bd9Sstevel@tonic-gate 	return (DDI_PROP_SUCCESS);
12157c478bd9Sstevel@tonic-gate }
12167c478bd9Sstevel@tonic-gate 
12177c478bd9Sstevel@tonic-gate /*
12187c478bd9Sstevel@tonic-gate  * ddi_prop_search_common:	Lookup and return the encoded value
12197c478bd9Sstevel@tonic-gate  */
12207c478bd9Sstevel@tonic-gate int
12217c478bd9Sstevel@tonic-gate ddi_prop_search_common(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op,
12227c478bd9Sstevel@tonic-gate     uint_t flags, char *name, void *valuep, uint_t *lengthp)
12237c478bd9Sstevel@tonic-gate {
12247c478bd9Sstevel@tonic-gate 	ddi_prop_t	*propp;
12257c478bd9Sstevel@tonic-gate 	int		i;
12267c478bd9Sstevel@tonic-gate 	caddr_t		buffer;
12277c478bd9Sstevel@tonic-gate 	caddr_t		prealloc = NULL;
12287c478bd9Sstevel@tonic-gate 	int		plength = 0;
12297c478bd9Sstevel@tonic-gate 	dev_info_t	*pdip;
12307c478bd9Sstevel@tonic-gate 	int		(*bop)();
12317c478bd9Sstevel@tonic-gate 
12327c478bd9Sstevel@tonic-gate 	/*CONSTANTCONDITION*/
12337c478bd9Sstevel@tonic-gate 	while (1)  {
12347c478bd9Sstevel@tonic-gate 
12357c478bd9Sstevel@tonic-gate 		mutex_enter(&(DEVI(dip)->devi_lock));
12367c478bd9Sstevel@tonic-gate 
12377c478bd9Sstevel@tonic-gate 
12387c478bd9Sstevel@tonic-gate 		/*
12397c478bd9Sstevel@tonic-gate 		 * find the property in child's devinfo:
12407c478bd9Sstevel@tonic-gate 		 * Search order is:
12417c478bd9Sstevel@tonic-gate 		 *	1. driver defined properties
12427c478bd9Sstevel@tonic-gate 		 *	2. system defined properties
12437c478bd9Sstevel@tonic-gate 		 *	3. driver global properties
12447c478bd9Sstevel@tonic-gate 		 *	4. boot defined properties
12457c478bd9Sstevel@tonic-gate 		 */
12467c478bd9Sstevel@tonic-gate 
12477c478bd9Sstevel@tonic-gate 		propp = i_ddi_prop_search(dev, name, flags,
12487c478bd9Sstevel@tonic-gate 		    &(DEVI(dip)->devi_drv_prop_ptr));
12497c478bd9Sstevel@tonic-gate 		if (propp == NULL)  {
12507c478bd9Sstevel@tonic-gate 			propp = i_ddi_prop_search(dev, name, flags,
12517c478bd9Sstevel@tonic-gate 			    &(DEVI(dip)->devi_sys_prop_ptr));
12527c478bd9Sstevel@tonic-gate 		}
12537c478bd9Sstevel@tonic-gate 		if ((propp == NULL) && DEVI(dip)->devi_global_prop_list) {
12547c478bd9Sstevel@tonic-gate 			propp = i_ddi_prop_search(dev, name, flags,
12557c478bd9Sstevel@tonic-gate 			    &DEVI(dip)->devi_global_prop_list->prop_list);
12567c478bd9Sstevel@tonic-gate 		}
12577c478bd9Sstevel@tonic-gate 
12587c478bd9Sstevel@tonic-gate 		if (propp == NULL)  {
12597c478bd9Sstevel@tonic-gate 			propp = i_ddi_prop_search(dev, name, flags,
12607c478bd9Sstevel@tonic-gate 			    &(DEVI(dip)->devi_hw_prop_ptr));
12617c478bd9Sstevel@tonic-gate 		}
12627c478bd9Sstevel@tonic-gate 
12637c478bd9Sstevel@tonic-gate 		/*
12647c478bd9Sstevel@tonic-gate 		 * Software property found?
12657c478bd9Sstevel@tonic-gate 		 */
12667c478bd9Sstevel@tonic-gate 		if (propp != (ddi_prop_t *)0)	{
12677c478bd9Sstevel@tonic-gate 
12687c478bd9Sstevel@tonic-gate 			/*
12697c478bd9Sstevel@tonic-gate 			 * If explicit undefine, return now.
12707c478bd9Sstevel@tonic-gate 			 */
12717c478bd9Sstevel@tonic-gate 			if (propp->prop_flags & DDI_PROP_UNDEF_IT) {
12727c478bd9Sstevel@tonic-gate 				mutex_exit(&(DEVI(dip)->devi_lock));
12737c478bd9Sstevel@tonic-gate 				if (prealloc)
12747c478bd9Sstevel@tonic-gate 					kmem_free(prealloc, plength);
12757c478bd9Sstevel@tonic-gate 				return (DDI_PROP_UNDEFINED);
12767c478bd9Sstevel@tonic-gate 			}
12777c478bd9Sstevel@tonic-gate 
12787c478bd9Sstevel@tonic-gate 			/*
12797c478bd9Sstevel@tonic-gate 			 * If we only want to know if it exists, return now
12807c478bd9Sstevel@tonic-gate 			 */
12817c478bd9Sstevel@tonic-gate 			if (prop_op == PROP_EXISTS) {
12827c478bd9Sstevel@tonic-gate 				mutex_exit(&(DEVI(dip)->devi_lock));
12837c478bd9Sstevel@tonic-gate 				ASSERT(prealloc == NULL);
12847c478bd9Sstevel@tonic-gate 				return (DDI_PROP_SUCCESS);
12857c478bd9Sstevel@tonic-gate 			}
12867c478bd9Sstevel@tonic-gate 
12877c478bd9Sstevel@tonic-gate 			/*
12887c478bd9Sstevel@tonic-gate 			 * If length only request or prop length == 0,
12897c478bd9Sstevel@tonic-gate 			 * service request and return now.
12907c478bd9Sstevel@tonic-gate 			 */
12917c478bd9Sstevel@tonic-gate 			if ((prop_op == PROP_LEN) ||(propp->prop_len == 0)) {
12927c478bd9Sstevel@tonic-gate 				*lengthp = propp->prop_len;
12937c478bd9Sstevel@tonic-gate 
12947c478bd9Sstevel@tonic-gate 				/*
12957c478bd9Sstevel@tonic-gate 				 * if prop_op is PROP_LEN_AND_VAL_ALLOC
12967c478bd9Sstevel@tonic-gate 				 * that means prop_len is 0, so set valuep
12977c478bd9Sstevel@tonic-gate 				 * also to NULL
12987c478bd9Sstevel@tonic-gate 				 */
12997c478bd9Sstevel@tonic-gate 				if (prop_op == PROP_LEN_AND_VAL_ALLOC)
13007c478bd9Sstevel@tonic-gate 					*(caddr_t *)valuep = NULL;
13017c478bd9Sstevel@tonic-gate 
13027c478bd9Sstevel@tonic-gate 				mutex_exit(&(DEVI(dip)->devi_lock));
13037c478bd9Sstevel@tonic-gate 				if (prealloc)
13047c478bd9Sstevel@tonic-gate 					kmem_free(prealloc, plength);
13057c478bd9Sstevel@tonic-gate 				return (DDI_PROP_SUCCESS);
13067c478bd9Sstevel@tonic-gate 			}
13077c478bd9Sstevel@tonic-gate 
13087c478bd9Sstevel@tonic-gate 			/*
13097c478bd9Sstevel@tonic-gate 			 * If LEN_AND_VAL_ALLOC and the request can sleep,
13107c478bd9Sstevel@tonic-gate 			 * drop the mutex, allocate the buffer, and go
13117c478bd9Sstevel@tonic-gate 			 * through the loop again.  If we already allocated
13127c478bd9Sstevel@tonic-gate 			 * the buffer, and the size of the property changed,
13137c478bd9Sstevel@tonic-gate 			 * keep trying...
13147c478bd9Sstevel@tonic-gate 			 */
13157c478bd9Sstevel@tonic-gate 			if ((prop_op == PROP_LEN_AND_VAL_ALLOC) &&
13167c478bd9Sstevel@tonic-gate 			    (flags & DDI_PROP_CANSLEEP))  {
13177c478bd9Sstevel@tonic-gate 				if (prealloc && (propp->prop_len != plength)) {
13187c478bd9Sstevel@tonic-gate 					kmem_free(prealloc, plength);
13197c478bd9Sstevel@tonic-gate 					prealloc = NULL;
13207c478bd9Sstevel@tonic-gate 				}
13217c478bd9Sstevel@tonic-gate 				if (prealloc == NULL)  {
13227c478bd9Sstevel@tonic-gate 					plength = propp->prop_len;
13237c478bd9Sstevel@tonic-gate 					mutex_exit(&(DEVI(dip)->devi_lock));
13247c478bd9Sstevel@tonic-gate 					prealloc = kmem_alloc(plength,
13257c478bd9Sstevel@tonic-gate 					    KM_SLEEP);
13267c478bd9Sstevel@tonic-gate 					continue;
13277c478bd9Sstevel@tonic-gate 				}
13287c478bd9Sstevel@tonic-gate 			}
13297c478bd9Sstevel@tonic-gate 
13307c478bd9Sstevel@tonic-gate 			/*
13317c478bd9Sstevel@tonic-gate 			 * Allocate buffer, if required.  Either way,
13327c478bd9Sstevel@tonic-gate 			 * set `buffer' variable.
13337c478bd9Sstevel@tonic-gate 			 */
13347c478bd9Sstevel@tonic-gate 			i = *lengthp;			/* Get callers length */
13357c478bd9Sstevel@tonic-gate 			*lengthp = propp->prop_len;	/* Set callers length */
13367c478bd9Sstevel@tonic-gate 
13377c478bd9Sstevel@tonic-gate 			switch (prop_op) {
13387c478bd9Sstevel@tonic-gate 
13397c478bd9Sstevel@tonic-gate 			case PROP_LEN_AND_VAL_ALLOC:
13407c478bd9Sstevel@tonic-gate 
13417c478bd9Sstevel@tonic-gate 				if (prealloc == NULL) {
13427c478bd9Sstevel@tonic-gate 					buffer = kmem_alloc(propp->prop_len,
13437c478bd9Sstevel@tonic-gate 					    KM_NOSLEEP);
13447c478bd9Sstevel@tonic-gate 				} else {
13457c478bd9Sstevel@tonic-gate 					buffer = prealloc;
13467c478bd9Sstevel@tonic-gate 				}
13477c478bd9Sstevel@tonic-gate 
13487c478bd9Sstevel@tonic-gate 				if (buffer == NULL)  {
13497c478bd9Sstevel@tonic-gate 					mutex_exit(&(DEVI(dip)->devi_lock));
13507c478bd9Sstevel@tonic-gate 					cmn_err(CE_CONT, prop_no_mem_msg, name);
13517c478bd9Sstevel@tonic-gate 					return (DDI_PROP_NO_MEMORY);
13527c478bd9Sstevel@tonic-gate 				}
13537c478bd9Sstevel@tonic-gate 				/* Set callers buf ptr */
13547c478bd9Sstevel@tonic-gate 				*(caddr_t *)valuep = buffer;
13557c478bd9Sstevel@tonic-gate 				break;
13567c478bd9Sstevel@tonic-gate 
13577c478bd9Sstevel@tonic-gate 			case PROP_LEN_AND_VAL_BUF:
13587c478bd9Sstevel@tonic-gate 
13597c478bd9Sstevel@tonic-gate 				if (propp->prop_len > (i)) {
13607c478bd9Sstevel@tonic-gate 					mutex_exit(&(DEVI(dip)->devi_lock));
13617c478bd9Sstevel@tonic-gate 					return (DDI_PROP_BUF_TOO_SMALL);
13627c478bd9Sstevel@tonic-gate 				}
13637c478bd9Sstevel@tonic-gate 
13647c478bd9Sstevel@tonic-gate 				buffer = valuep;  /* Get callers buf ptr */
13657c478bd9Sstevel@tonic-gate 				break;
13667c478bd9Sstevel@tonic-gate 
13677c478bd9Sstevel@tonic-gate 			default:
13687c478bd9Sstevel@tonic-gate 				break;
13697c478bd9Sstevel@tonic-gate 			}
13707c478bd9Sstevel@tonic-gate 
13717c478bd9Sstevel@tonic-gate 			/*
13727c478bd9Sstevel@tonic-gate 			 * Do the copy.
13737c478bd9Sstevel@tonic-gate 			 */
13747c478bd9Sstevel@tonic-gate 			bcopy(propp->prop_val, buffer, propp->prop_len);
13757c478bd9Sstevel@tonic-gate 			mutex_exit(&(DEVI(dip)->devi_lock));
13767c478bd9Sstevel@tonic-gate 			return (DDI_PROP_SUCCESS);
13777c478bd9Sstevel@tonic-gate 		}
13787c478bd9Sstevel@tonic-gate 
13797c478bd9Sstevel@tonic-gate 		mutex_exit(&(DEVI(dip)->devi_lock));
13807c478bd9Sstevel@tonic-gate 		if (prealloc)
13817c478bd9Sstevel@tonic-gate 			kmem_free(prealloc, plength);
13827c478bd9Sstevel@tonic-gate 		prealloc = NULL;
13837c478bd9Sstevel@tonic-gate 
13847c478bd9Sstevel@tonic-gate 		/*
13857c478bd9Sstevel@tonic-gate 		 * Prop not found, call parent bus_ops to deal with possible
13867c478bd9Sstevel@tonic-gate 		 * h/w layer (possible PROM defined props, etc.) and to
13877c478bd9Sstevel@tonic-gate 		 * possibly ascend the hierarchy, if allowed by flags.
13887c478bd9Sstevel@tonic-gate 		 */
13897c478bd9Sstevel@tonic-gate 		pdip = (dev_info_t *)DEVI(dip)->devi_parent;
13907c478bd9Sstevel@tonic-gate 
13917c478bd9Sstevel@tonic-gate 		/*
13927c478bd9Sstevel@tonic-gate 		 * One last call for the root driver PROM props?
13937c478bd9Sstevel@tonic-gate 		 */
13947c478bd9Sstevel@tonic-gate 		if (dip == ddi_root_node())  {
13957c478bd9Sstevel@tonic-gate 			return (ddi_bus_prop_op(dev, dip, dip, prop_op,
13967c478bd9Sstevel@tonic-gate 			    flags, name, valuep, (int *)lengthp));
13977c478bd9Sstevel@tonic-gate 		}
13987c478bd9Sstevel@tonic-gate 
13997c478bd9Sstevel@tonic-gate 		/*
14007c478bd9Sstevel@tonic-gate 		 * We may have been called to check for properties
14017c478bd9Sstevel@tonic-gate 		 * within a single devinfo node that has no parent -
14027c478bd9Sstevel@tonic-gate 		 * see make_prop()
14037c478bd9Sstevel@tonic-gate 		 */
14047c478bd9Sstevel@tonic-gate 		if (pdip == NULL) {
14057c478bd9Sstevel@tonic-gate 			ASSERT((flags &
14067c478bd9Sstevel@tonic-gate 			    (DDI_PROP_DONTPASS | DDI_PROP_NOTPROM)) ==
14077c478bd9Sstevel@tonic-gate 			    (DDI_PROP_DONTPASS | DDI_PROP_NOTPROM));
14087c478bd9Sstevel@tonic-gate 			return (DDI_PROP_NOT_FOUND);
14097c478bd9Sstevel@tonic-gate 		}
14107c478bd9Sstevel@tonic-gate 
14117c478bd9Sstevel@tonic-gate 		/*
14127c478bd9Sstevel@tonic-gate 		 * Instead of recursing, we do iterative calls up the tree.
14137c478bd9Sstevel@tonic-gate 		 * As a bit of optimization, skip the bus_op level if the
14147c478bd9Sstevel@tonic-gate 		 * node is a s/w node and if the parent's bus_prop_op function
14157c478bd9Sstevel@tonic-gate 		 * is `ddi_bus_prop_op', because we know that in this case,
14167c478bd9Sstevel@tonic-gate 		 * this function does nothing.
14177c478bd9Sstevel@tonic-gate 		 *
14187c478bd9Sstevel@tonic-gate 		 * 4225415: If the parent isn't attached, or the child
14197c478bd9Sstevel@tonic-gate 		 * hasn't been named by the parent yet, use the default
14207c478bd9Sstevel@tonic-gate 		 * ddi_bus_prop_op as a proxy for the parent.  This
14217c478bd9Sstevel@tonic-gate 		 * allows property lookups in any child/parent state to
14227c478bd9Sstevel@tonic-gate 		 * include 'prom' and inherited properties, even when
14237c478bd9Sstevel@tonic-gate 		 * there are no drivers attached to the child or parent.
14247c478bd9Sstevel@tonic-gate 		 */
14257c478bd9Sstevel@tonic-gate 
14267c478bd9Sstevel@tonic-gate 		bop = ddi_bus_prop_op;
1427737d277aScth 		if (i_ddi_devi_attached(pdip) &&
14287c478bd9Sstevel@tonic-gate 		    (i_ddi_node_state(dip) >= DS_INITIALIZED))
14297c478bd9Sstevel@tonic-gate 			bop = DEVI(pdip)->devi_ops->devo_bus_ops->bus_prop_op;
14307c478bd9Sstevel@tonic-gate 
14317c478bd9Sstevel@tonic-gate 		i = DDI_PROP_NOT_FOUND;
14327c478bd9Sstevel@tonic-gate 
14337c478bd9Sstevel@tonic-gate 		if ((bop != ddi_bus_prop_op) || ndi_dev_is_prom_node(dip)) {
14347c478bd9Sstevel@tonic-gate 			i = (*bop)(dev, pdip, dip, prop_op,
14357c478bd9Sstevel@tonic-gate 			    flags | DDI_PROP_DONTPASS,
14367c478bd9Sstevel@tonic-gate 			    name, valuep, lengthp);
14377c478bd9Sstevel@tonic-gate 		}
14387c478bd9Sstevel@tonic-gate 
14397c478bd9Sstevel@tonic-gate 		if ((flags & DDI_PROP_DONTPASS) ||
14407c478bd9Sstevel@tonic-gate 		    (i != DDI_PROP_NOT_FOUND))
14417c478bd9Sstevel@tonic-gate 			return (i);
14427c478bd9Sstevel@tonic-gate 
14437c478bd9Sstevel@tonic-gate 		dip = pdip;
14447c478bd9Sstevel@tonic-gate 	}
14457c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
14467c478bd9Sstevel@tonic-gate }
14477c478bd9Sstevel@tonic-gate 
14487c478bd9Sstevel@tonic-gate 
14497c478bd9Sstevel@tonic-gate /*
14507c478bd9Sstevel@tonic-gate  * ddi_prop_op: The basic property operator for drivers.
14517c478bd9Sstevel@tonic-gate  *
14527c478bd9Sstevel@tonic-gate  * In ddi_prop_op, the type of valuep is interpreted based on prop_op:
14537c478bd9Sstevel@tonic-gate  *
14547c478bd9Sstevel@tonic-gate  *	prop_op			valuep
14557c478bd9Sstevel@tonic-gate  *	------			------
14567c478bd9Sstevel@tonic-gate  *
14577c478bd9Sstevel@tonic-gate  *	PROP_LEN		<unused>
14587c478bd9Sstevel@tonic-gate  *
14597c478bd9Sstevel@tonic-gate  *	PROP_LEN_AND_VAL_BUF	Pointer to callers buffer
14607c478bd9Sstevel@tonic-gate  *
14617c478bd9Sstevel@tonic-gate  *	PROP_LEN_AND_VAL_ALLOC	Address of callers pointer (will be set to
14627c478bd9Sstevel@tonic-gate  *				address of allocated buffer, if successful)
14637c478bd9Sstevel@tonic-gate  */
14647c478bd9Sstevel@tonic-gate int
14657c478bd9Sstevel@tonic-gate ddi_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, int mod_flags,
14667c478bd9Sstevel@tonic-gate     char *name, caddr_t valuep, int *lengthp)
14677c478bd9Sstevel@tonic-gate {
14687c478bd9Sstevel@tonic-gate 	int	i;
14697c478bd9Sstevel@tonic-gate 
14707c478bd9Sstevel@tonic-gate 	ASSERT((mod_flags & DDI_PROP_TYPE_MASK) == 0);
14717c478bd9Sstevel@tonic-gate 
14727c478bd9Sstevel@tonic-gate 	/*
14737c478bd9Sstevel@tonic-gate 	 * If this was originally an LDI prop lookup then we bail here.
14747c478bd9Sstevel@tonic-gate 	 * The reason is that the LDI property lookup interfaces first call
14757c478bd9Sstevel@tonic-gate 	 * a drivers prop_op() entry point to allow it to override
14767c478bd9Sstevel@tonic-gate 	 * properties.  But if we've made it here, then the driver hasn't
14777c478bd9Sstevel@tonic-gate 	 * overriden any properties.  We don't want to continue with the
14787c478bd9Sstevel@tonic-gate 	 * property search here because we don't have any type inforamtion.
14797c478bd9Sstevel@tonic-gate 	 * When we return failure, the LDI interfaces will then proceed to
14807c478bd9Sstevel@tonic-gate 	 * call the typed property interfaces to look up the property.
14817c478bd9Sstevel@tonic-gate 	 */
14827c478bd9Sstevel@tonic-gate 	if (mod_flags & DDI_PROP_DYNAMIC)
14837c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
14847c478bd9Sstevel@tonic-gate 
14857c478bd9Sstevel@tonic-gate 	/*
14867c478bd9Sstevel@tonic-gate 	 * check for pre-typed property consumer asking for typed property:
14877c478bd9Sstevel@tonic-gate 	 * see e_ddi_getprop_int64.
14887c478bd9Sstevel@tonic-gate 	 */
14897c478bd9Sstevel@tonic-gate 	if (mod_flags & DDI_PROP_CONSUMER_TYPED)
14907c478bd9Sstevel@tonic-gate 		mod_flags |= DDI_PROP_TYPE_INT64;
14917c478bd9Sstevel@tonic-gate 	mod_flags |= DDI_PROP_TYPE_ANY;
14927c478bd9Sstevel@tonic-gate 
14937c478bd9Sstevel@tonic-gate 	i = ddi_prop_search_common(dev, dip, prop_op,
14947c478bd9Sstevel@tonic-gate 	    mod_flags, name, valuep, (uint_t *)lengthp);
14957c478bd9Sstevel@tonic-gate 	if (i == DDI_PROP_FOUND_1275)
14967c478bd9Sstevel@tonic-gate 		return (DDI_PROP_SUCCESS);
14977c478bd9Sstevel@tonic-gate 	return (i);
14987c478bd9Sstevel@tonic-gate }
14997c478bd9Sstevel@tonic-gate 
15007c478bd9Sstevel@tonic-gate /*
1501184cd04cScth  * ddi_prop_op_nblocks_blksize: The basic property operator for drivers that
1502184cd04cScth  * maintain size in number of blksize blocks.  Provides a dynamic property
1503184cd04cScth  * implementation for size oriented properties based on nblocks64 and blksize
1504184cd04cScth  * values passed in by the driver.  Fallback to ddi_prop_op if the nblocks64
1505184cd04cScth  * is too large.  This interface should not be used with a nblocks64 that
1506184cd04cScth  * represents the driver's idea of how to represent unknown, if nblocks is
1507184cd04cScth  * unknown use ddi_prop_op.
15087c478bd9Sstevel@tonic-gate  */
15097c478bd9Sstevel@tonic-gate int
1510184cd04cScth ddi_prop_op_nblocks_blksize(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op,
1511184cd04cScth     int mod_flags, char *name, caddr_t valuep, int *lengthp,
1512184cd04cScth     uint64_t nblocks64, uint_t blksize)
15137c478bd9Sstevel@tonic-gate {
15147c478bd9Sstevel@tonic-gate 	uint64_t size64;
1515184cd04cScth 	int	blkshift;
1516184cd04cScth 
1517184cd04cScth 	/* convert block size to shift value */
1518184cd04cScth 	ASSERT(BIT_ONLYONESET(blksize));
1519184cd04cScth 	blkshift = highbit(blksize) - 1;
15207c478bd9Sstevel@tonic-gate 
15217c478bd9Sstevel@tonic-gate 	/*
15227c478bd9Sstevel@tonic-gate 	 * There is no point in supporting nblocks64 values that don't have
15237c478bd9Sstevel@tonic-gate 	 * an accurate uint64_t byte count representation.
15247c478bd9Sstevel@tonic-gate 	 */
1525184cd04cScth 	if (nblocks64 >= (UINT64_MAX >> blkshift))
15267c478bd9Sstevel@tonic-gate 		return (ddi_prop_op(dev, dip, prop_op, mod_flags,
15277c478bd9Sstevel@tonic-gate 		    name, valuep, lengthp));
15287c478bd9Sstevel@tonic-gate 
1529184cd04cScth 	size64 = nblocks64 << blkshift;
1530184cd04cScth 	return (ddi_prop_op_size_blksize(dev, dip, prop_op, mod_flags,
1531184cd04cScth 	    name, valuep, lengthp, size64, blksize));
15327c478bd9Sstevel@tonic-gate }
15337c478bd9Sstevel@tonic-gate 
15347c478bd9Sstevel@tonic-gate /*
1535184cd04cScth  * ddi_prop_op_nblocks: ddi_prop_op_nblocks_blksize with DEV_BSIZE blksize.
1536184cd04cScth  */
1537184cd04cScth int
1538184cd04cScth ddi_prop_op_nblocks(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op,
1539184cd04cScth     int mod_flags, char *name, caddr_t valuep, int *lengthp, uint64_t nblocks64)
1540184cd04cScth {
1541184cd04cScth 	return (ddi_prop_op_nblocks_blksize(dev, dip, prop_op,
1542184cd04cScth 	    mod_flags, name, valuep, lengthp, nblocks64, DEV_BSIZE));
1543184cd04cScth }
1544184cd04cScth 
1545184cd04cScth /*
1546184cd04cScth  * ddi_prop_op_size_blksize: The basic property operator for block drivers that
1547184cd04cScth  * maintain size in bytes. Provides a of dynamic property implementation for
1548184cd04cScth  * size oriented properties based on size64 value and blksize passed in by the
1549184cd04cScth  * driver.  Fallback to ddi_prop_op if the size64 is too large. This interface
1550184cd04cScth  * should not be used with a size64 that represents the driver's idea of how
1551184cd04cScth  * to represent unknown, if size is unknown use ddi_prop_op.
15527c478bd9Sstevel@tonic-gate  *
15537c478bd9Sstevel@tonic-gate  * NOTE: the legacy "nblocks"/"size" properties are treated as 32-bit unsigned
15547c478bd9Sstevel@tonic-gate  * integers. While the most likely interface to request them ([bc]devi_size)
15557c478bd9Sstevel@tonic-gate  * is declared int (signed) there is no enforcement of this, which means we
15567c478bd9Sstevel@tonic-gate  * can't enforce limitations here without risking regression.
15577c478bd9Sstevel@tonic-gate  */
15587c478bd9Sstevel@tonic-gate int
1559184cd04cScth ddi_prop_op_size_blksize(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op,
1560184cd04cScth     int mod_flags, char *name, caddr_t valuep, int *lengthp, uint64_t size64,
1561184cd04cScth     uint_t blksize)
15627c478bd9Sstevel@tonic-gate {
15637c478bd9Sstevel@tonic-gate 	uint64_t nblocks64;
15647c478bd9Sstevel@tonic-gate 	int	callers_length;
15657c478bd9Sstevel@tonic-gate 	caddr_t	buffer;
1566184cd04cScth 	int	blkshift;
1567184cd04cScth 
1568b9ccdc5aScth 	/*
1569b9ccdc5aScth 	 * This is a kludge to support capture of size(9P) pure dynamic
1570b9ccdc5aScth 	 * properties in snapshots for non-cmlb code (without exposing
1571b9ccdc5aScth 	 * i_ddi_prop_dyn changes). When everyone uses cmlb, this code
1572b9ccdc5aScth 	 * should be removed.
1573b9ccdc5aScth 	 */
1574b9ccdc5aScth 	if (i_ddi_prop_dyn_driver_get(dip) == NULL) {
1575b9ccdc5aScth 		static i_ddi_prop_dyn_t prop_dyn_size[] = {
1576b9ccdc5aScth 		    {"Size",		DDI_PROP_TYPE_INT64,	S_IFCHR},
1577b9ccdc5aScth 		    {"Nblocks",		DDI_PROP_TYPE_INT64,	S_IFBLK},
1578b9ccdc5aScth 		    {NULL}
1579b9ccdc5aScth 		};
1580b9ccdc5aScth 		i_ddi_prop_dyn_driver_set(dip, prop_dyn_size);
1581b9ccdc5aScth 	}
1582b9ccdc5aScth 
1583184cd04cScth 	/* convert block size to shift value */
1584184cd04cScth 	ASSERT(BIT_ONLYONESET(blksize));
1585184cd04cScth 	blkshift = highbit(blksize) - 1;
15867c478bd9Sstevel@tonic-gate 
15877c478bd9Sstevel@tonic-gate 	/* compute DEV_BSIZE nblocks value */
1588184cd04cScth 	nblocks64 = size64 >> blkshift;
15897c478bd9Sstevel@tonic-gate 
15907c478bd9Sstevel@tonic-gate 	/* get callers length, establish length of our dynamic properties */
15917c478bd9Sstevel@tonic-gate 	callers_length = *lengthp;
15927c478bd9Sstevel@tonic-gate 
15937c478bd9Sstevel@tonic-gate 	if (strcmp(name, "Nblocks") == 0)
15947c478bd9Sstevel@tonic-gate 		*lengthp = sizeof (uint64_t);
15957c478bd9Sstevel@tonic-gate 	else if (strcmp(name, "Size") == 0)
15967c478bd9Sstevel@tonic-gate 		*lengthp = sizeof (uint64_t);
15977c478bd9Sstevel@tonic-gate 	else if ((strcmp(name, "nblocks") == 0) && (nblocks64 < UINT_MAX))
15987c478bd9Sstevel@tonic-gate 		*lengthp = sizeof (uint32_t);
15997c478bd9Sstevel@tonic-gate 	else if ((strcmp(name, "size") == 0) && (size64 < UINT_MAX))
16007c478bd9Sstevel@tonic-gate 		*lengthp = sizeof (uint32_t);
1601184cd04cScth 	else if ((strcmp(name, "blksize") == 0) && (blksize < UINT_MAX))
1602184cd04cScth 		*lengthp = sizeof (uint32_t);
16037c478bd9Sstevel@tonic-gate 	else {
16047c478bd9Sstevel@tonic-gate 		/* fallback to ddi_prop_op */
16057c478bd9Sstevel@tonic-gate 		return (ddi_prop_op(dev, dip, prop_op, mod_flags,
16067c478bd9Sstevel@tonic-gate 		    name, valuep, lengthp));
16077c478bd9Sstevel@tonic-gate 	}
16087c478bd9Sstevel@tonic-gate 
16097c478bd9Sstevel@tonic-gate 	/* service request for the length of the property */
16107c478bd9Sstevel@tonic-gate 	if (prop_op == PROP_LEN)
16117c478bd9Sstevel@tonic-gate 		return (DDI_PROP_SUCCESS);
16127c478bd9Sstevel@tonic-gate 
16137c478bd9Sstevel@tonic-gate 	switch (prop_op) {
16147c478bd9Sstevel@tonic-gate 	case PROP_LEN_AND_VAL_ALLOC:
16157c478bd9Sstevel@tonic-gate 		if ((buffer = kmem_alloc(*lengthp,
16167c478bd9Sstevel@tonic-gate 		    (mod_flags & DDI_PROP_CANSLEEP) ?
16177c478bd9Sstevel@tonic-gate 		    KM_SLEEP : KM_NOSLEEP)) == NULL)
16187c478bd9Sstevel@tonic-gate 			return (DDI_PROP_NO_MEMORY);
16197c478bd9Sstevel@tonic-gate 
16207c478bd9Sstevel@tonic-gate 		*(caddr_t *)valuep = buffer;	/* set callers buf ptr */
16217c478bd9Sstevel@tonic-gate 		break;
16227c478bd9Sstevel@tonic-gate 
16237c478bd9Sstevel@tonic-gate 	case PROP_LEN_AND_VAL_BUF:
1624b9ccdc5aScth 		/* the length of the property and the request must match */
1625b9ccdc5aScth 		if (callers_length != *lengthp)
1626b9ccdc5aScth 			return (DDI_PROP_INVAL_ARG);
1627b9ccdc5aScth 
16287c478bd9Sstevel@tonic-gate 		buffer = valuep;		/* get callers buf ptr */
16297c478bd9Sstevel@tonic-gate 		break;
16307c478bd9Sstevel@tonic-gate 
16317c478bd9Sstevel@tonic-gate 	default:
16327c478bd9Sstevel@tonic-gate 		return (DDI_PROP_INVAL_ARG);
16337c478bd9Sstevel@tonic-gate 	}
16347c478bd9Sstevel@tonic-gate 
16357c478bd9Sstevel@tonic-gate 	/* transfer the value into the buffer */
16367c478bd9Sstevel@tonic-gate 	if (strcmp(name, "Nblocks") == 0)
16377c478bd9Sstevel@tonic-gate 		*((uint64_t *)buffer) = nblocks64;
16387c478bd9Sstevel@tonic-gate 	else if (strcmp(name, "Size") == 0)
16397c478bd9Sstevel@tonic-gate 		*((uint64_t *)buffer) = size64;
16407c478bd9Sstevel@tonic-gate 	else if (strcmp(name, "nblocks") == 0)
16417c478bd9Sstevel@tonic-gate 		*((uint32_t *)buffer) = (uint32_t)nblocks64;
16427c478bd9Sstevel@tonic-gate 	else if (strcmp(name, "size") == 0)
16437c478bd9Sstevel@tonic-gate 		*((uint32_t *)buffer) = (uint32_t)size64;
1644184cd04cScth 	else if (strcmp(name, "blksize") == 0)
1645184cd04cScth 		*((uint32_t *)buffer) = (uint32_t)blksize;
16467c478bd9Sstevel@tonic-gate 	return (DDI_PROP_SUCCESS);
16477c478bd9Sstevel@tonic-gate }
16487c478bd9Sstevel@tonic-gate 
16497c478bd9Sstevel@tonic-gate /*
1650184cd04cScth  * ddi_prop_op_size: ddi_prop_op_size_blksize with DEV_BSIZE block size.
1651184cd04cScth  */
1652184cd04cScth int
1653184cd04cScth ddi_prop_op_size(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op,
1654184cd04cScth     int mod_flags, char *name, caddr_t valuep, int *lengthp, uint64_t size64)
1655184cd04cScth {
1656184cd04cScth 	return (ddi_prop_op_size_blksize(dev, dip, prop_op,
1657184cd04cScth 	    mod_flags, name, valuep, lengthp, size64, DEV_BSIZE));
1658184cd04cScth }
1659184cd04cScth 
1660184cd04cScth /*
16617c478bd9Sstevel@tonic-gate  * Variable length props...
16627c478bd9Sstevel@tonic-gate  */
16637c478bd9Sstevel@tonic-gate 
16647c478bd9Sstevel@tonic-gate /*
16657c478bd9Sstevel@tonic-gate  * ddi_getlongprop:	Get variable length property len+val into a buffer
16667c478bd9Sstevel@tonic-gate  *		allocated by property provider via kmem_alloc. Requester
16677c478bd9Sstevel@tonic-gate  *		is responsible for freeing returned property via kmem_free.
16687c478bd9Sstevel@tonic-gate  *
16697c478bd9Sstevel@tonic-gate  *	Arguments:
16707c478bd9Sstevel@tonic-gate  *
16717c478bd9Sstevel@tonic-gate  *	dev_t:	Input:	dev_t of property.
16727c478bd9Sstevel@tonic-gate  *	dip:	Input:	dev_info_t pointer of child.
16737c478bd9Sstevel@tonic-gate  *	flags:	Input:	Possible flag modifiers are:
16747c478bd9Sstevel@tonic-gate  *		DDI_PROP_DONTPASS:	Don't pass to parent if prop not found.
16757c478bd9Sstevel@tonic-gate  *		DDI_PROP_CANSLEEP:	Memory allocation may sleep.
16767c478bd9Sstevel@tonic-gate  *	name:	Input:	name of property.
16777c478bd9Sstevel@tonic-gate  *	valuep:	Output:	Addr of callers buffer pointer.
16787c478bd9Sstevel@tonic-gate  *	lengthp:Output:	*lengthp will contain prop length on exit.
16797c478bd9Sstevel@tonic-gate  *
16807c478bd9Sstevel@tonic-gate  *	Possible Returns:
16817c478bd9Sstevel@tonic-gate  *
16827c478bd9Sstevel@tonic-gate  *		DDI_PROP_SUCCESS:	Prop found and returned.
16837c478bd9Sstevel@tonic-gate  *		DDI_PROP_NOT_FOUND:	Prop not found
16847c478bd9Sstevel@tonic-gate  *		DDI_PROP_UNDEFINED:	Prop explicitly undefined.
16857c478bd9Sstevel@tonic-gate  *		DDI_PROP_NO_MEMORY:	Prop found, but unable to alloc mem.
16867c478bd9Sstevel@tonic-gate  */
16877c478bd9Sstevel@tonic-gate 
16887c478bd9Sstevel@tonic-gate int
16897c478bd9Sstevel@tonic-gate ddi_getlongprop(dev_t dev, dev_info_t *dip, int flags,
16907c478bd9Sstevel@tonic-gate     char *name, caddr_t valuep, int *lengthp)
16917c478bd9Sstevel@tonic-gate {
16927c478bd9Sstevel@tonic-gate 	return (ddi_prop_op(dev, dip, PROP_LEN_AND_VAL_ALLOC,
16937c478bd9Sstevel@tonic-gate 	    flags, name, valuep, lengthp));
16947c478bd9Sstevel@tonic-gate }
16957c478bd9Sstevel@tonic-gate 
16967c478bd9Sstevel@tonic-gate /*
16977c478bd9Sstevel@tonic-gate  *
16987c478bd9Sstevel@tonic-gate  * ddi_getlongprop_buf:		Get long prop into pre-allocated callers
16997c478bd9Sstevel@tonic-gate  *				buffer. (no memory allocation by provider).
17007c478bd9Sstevel@tonic-gate  *
17017c478bd9Sstevel@tonic-gate  *	dev_t:	Input:	dev_t of property.
17027c478bd9Sstevel@tonic-gate  *	dip:	Input:	dev_info_t pointer of child.
17037c478bd9Sstevel@tonic-gate  *	flags:	Input:	DDI_PROP_DONTPASS or NULL
17047c478bd9Sstevel@tonic-gate  *	name:	Input:	name of property
17057c478bd9Sstevel@tonic-gate  *	valuep:	Input:	ptr to callers buffer.
17067c478bd9Sstevel@tonic-gate  *	lengthp:I/O:	ptr to length of callers buffer on entry,
17077c478bd9Sstevel@tonic-gate  *			actual length of property on exit.
17087c478bd9Sstevel@tonic-gate  *
17097c478bd9Sstevel@tonic-gate  *	Possible returns:
17107c478bd9Sstevel@tonic-gate  *
17117c478bd9Sstevel@tonic-gate  *		DDI_PROP_SUCCESS	Prop found and returned
17127c478bd9Sstevel@tonic-gate  *		DDI_PROP_NOT_FOUND	Prop not found
17137c478bd9Sstevel@tonic-gate  *		DDI_PROP_UNDEFINED	Prop explicitly undefined.
17147c478bd9Sstevel@tonic-gate  *		DDI_PROP_BUF_TOO_SMALL	Prop found, callers buf too small,
17157c478bd9Sstevel@tonic-gate  *					no value returned, but actual prop
17167c478bd9Sstevel@tonic-gate  *					length returned in *lengthp
17177c478bd9Sstevel@tonic-gate  *
17187c478bd9Sstevel@tonic-gate  */
17197c478bd9Sstevel@tonic-gate 
17207c478bd9Sstevel@tonic-gate int
17217c478bd9Sstevel@tonic-gate ddi_getlongprop_buf(dev_t dev, dev_info_t *dip, int flags,
17227c478bd9Sstevel@tonic-gate     char *name, caddr_t valuep, int *lengthp)
17237c478bd9Sstevel@tonic-gate {
17247c478bd9Sstevel@tonic-gate 	return (ddi_prop_op(dev, dip, PROP_LEN_AND_VAL_BUF,
17257c478bd9Sstevel@tonic-gate 	    flags, name, valuep, lengthp));
17267c478bd9Sstevel@tonic-gate }
17277c478bd9Sstevel@tonic-gate 
17287c478bd9Sstevel@tonic-gate /*
17297c478bd9Sstevel@tonic-gate  * Integer/boolean sized props.
17307c478bd9Sstevel@tonic-gate  *
17317c478bd9Sstevel@tonic-gate  * Call is value only... returns found boolean or int sized prop value or
17327c478bd9Sstevel@tonic-gate  * defvalue if prop not found or is wrong length or is explicitly undefined.
17337c478bd9Sstevel@tonic-gate  * Only flag is DDI_PROP_DONTPASS...
17347c478bd9Sstevel@tonic-gate  *
17357c478bd9Sstevel@tonic-gate  * By convention, this interface returns boolean (0) sized properties
17367c478bd9Sstevel@tonic-gate  * as value (int)1.
17377c478bd9Sstevel@tonic-gate  *
17387c478bd9Sstevel@tonic-gate  * This never returns an error, if property not found or specifically
17397c478bd9Sstevel@tonic-gate  * undefined, the input `defvalue' is returned.
17407c478bd9Sstevel@tonic-gate  */
17417c478bd9Sstevel@tonic-gate 
17427c478bd9Sstevel@tonic-gate int
17437c478bd9Sstevel@tonic-gate ddi_getprop(dev_t dev, dev_info_t *dip, int flags, char *name, int defvalue)
17447c478bd9Sstevel@tonic-gate {
17457c478bd9Sstevel@tonic-gate 	int	propvalue = defvalue;
17467c478bd9Sstevel@tonic-gate 	int	proplength = sizeof (int);
17477c478bd9Sstevel@tonic-gate 	int	error;
17487c478bd9Sstevel@tonic-gate 
17497c478bd9Sstevel@tonic-gate 	error = ddi_prop_op(dev, dip, PROP_LEN_AND_VAL_BUF,
17507c478bd9Sstevel@tonic-gate 	    flags, name, (caddr_t)&propvalue, &proplength);
17517c478bd9Sstevel@tonic-gate 
17527c478bd9Sstevel@tonic-gate 	if ((error == DDI_PROP_SUCCESS) && (proplength == 0))
17537c478bd9Sstevel@tonic-gate 		propvalue = 1;
17547c478bd9Sstevel@tonic-gate 
17557c478bd9Sstevel@tonic-gate 	return (propvalue);
17567c478bd9Sstevel@tonic-gate }
17577c478bd9Sstevel@tonic-gate 
17587c478bd9Sstevel@tonic-gate /*
17597c478bd9Sstevel@tonic-gate  * Get prop length interface: flags are 0 or DDI_PROP_DONTPASS
17607c478bd9Sstevel@tonic-gate  * if returns DDI_PROP_SUCCESS, length returned in *lengthp.
17617c478bd9Sstevel@tonic-gate  */
17627c478bd9Sstevel@tonic-gate 
17637c478bd9Sstevel@tonic-gate int
17647c478bd9Sstevel@tonic-gate ddi_getproplen(dev_t dev, dev_info_t *dip, int flags, char *name, int *lengthp)
17657c478bd9Sstevel@tonic-gate {
17667c478bd9Sstevel@tonic-gate 	return (ddi_prop_op(dev, dip, PROP_LEN, flags, name, NULL, lengthp));
17677c478bd9Sstevel@tonic-gate }
17687c478bd9Sstevel@tonic-gate 
17697c478bd9Sstevel@tonic-gate /*
17707c478bd9Sstevel@tonic-gate  * Allocate a struct prop_driver_data, along with 'size' bytes
17717c478bd9Sstevel@tonic-gate  * for decoded property data.  This structure is freed by
17727c478bd9Sstevel@tonic-gate  * calling ddi_prop_free(9F).
17737c478bd9Sstevel@tonic-gate  */
17747c478bd9Sstevel@tonic-gate static void *
17757c478bd9Sstevel@tonic-gate ddi_prop_decode_alloc(size_t size, void (*prop_free)(struct prop_driver_data *))
17767c478bd9Sstevel@tonic-gate {
17777c478bd9Sstevel@tonic-gate 	struct prop_driver_data *pdd;
17787c478bd9Sstevel@tonic-gate 
17797c478bd9Sstevel@tonic-gate 	/*
17807c478bd9Sstevel@tonic-gate 	 * Allocate a structure with enough memory to store the decoded data.
17817c478bd9Sstevel@tonic-gate 	 */
17827c478bd9Sstevel@tonic-gate 	pdd = kmem_zalloc(sizeof (struct prop_driver_data) + size, KM_SLEEP);
17837c478bd9Sstevel@tonic-gate 	pdd->pdd_size = (sizeof (struct prop_driver_data) + size);
17847c478bd9Sstevel@tonic-gate 	pdd->pdd_prop_free = prop_free;
17857c478bd9Sstevel@tonic-gate 
17867c478bd9Sstevel@tonic-gate 	/*
17877c478bd9Sstevel@tonic-gate 	 * Return a pointer to the location to put the decoded data.
17887c478bd9Sstevel@tonic-gate 	 */
17897c478bd9Sstevel@tonic-gate 	return ((void *)((caddr_t)pdd + sizeof (struct prop_driver_data)));
17907c478bd9Sstevel@tonic-gate }
17917c478bd9Sstevel@tonic-gate 
17927c478bd9Sstevel@tonic-gate /*
17937c478bd9Sstevel@tonic-gate  * Allocated the memory needed to store the encoded data in the property
17947c478bd9Sstevel@tonic-gate  * handle.
17957c478bd9Sstevel@tonic-gate  */
17967c478bd9Sstevel@tonic-gate static int
17977c478bd9Sstevel@tonic-gate ddi_prop_encode_alloc(prop_handle_t *ph, size_t size)
17987c478bd9Sstevel@tonic-gate {
17997c478bd9Sstevel@tonic-gate 	/*
18007c478bd9Sstevel@tonic-gate 	 * If size is zero, then set data to NULL and size to 0.  This
18017c478bd9Sstevel@tonic-gate 	 * is a boolean property.
18027c478bd9Sstevel@tonic-gate 	 */
18037c478bd9Sstevel@tonic-gate 	if (size == 0) {
18047c478bd9Sstevel@tonic-gate 		ph->ph_size = 0;
18057c478bd9Sstevel@tonic-gate 		ph->ph_data = NULL;
18067c478bd9Sstevel@tonic-gate 		ph->ph_cur_pos = NULL;
18077c478bd9Sstevel@tonic-gate 		ph->ph_save_pos = NULL;
18087c478bd9Sstevel@tonic-gate 	} else {
18097c478bd9Sstevel@tonic-gate 		if (ph->ph_flags == DDI_PROP_DONTSLEEP) {
18107c478bd9Sstevel@tonic-gate 			ph->ph_data = kmem_zalloc(size, KM_NOSLEEP);
18117c478bd9Sstevel@tonic-gate 			if (ph->ph_data == NULL)
18127c478bd9Sstevel@tonic-gate 				return (DDI_PROP_NO_MEMORY);
18137c478bd9Sstevel@tonic-gate 		} else
18147c478bd9Sstevel@tonic-gate 			ph->ph_data = kmem_zalloc(size, KM_SLEEP);
18157c478bd9Sstevel@tonic-gate 		ph->ph_size = size;
18167c478bd9Sstevel@tonic-gate 		ph->ph_cur_pos = ph->ph_data;
18177c478bd9Sstevel@tonic-gate 		ph->ph_save_pos = ph->ph_data;
18187c478bd9Sstevel@tonic-gate 	}
18197c478bd9Sstevel@tonic-gate 	return (DDI_PROP_SUCCESS);
18207c478bd9Sstevel@tonic-gate }
18217c478bd9Sstevel@tonic-gate 
18227c478bd9Sstevel@tonic-gate /*
18237c478bd9Sstevel@tonic-gate  * Free the space allocated by the lookup routines.  Each lookup routine
18247c478bd9Sstevel@tonic-gate  * returns a pointer to the decoded data to the driver.  The driver then
18257c478bd9Sstevel@tonic-gate  * passes this pointer back to us.  This data actually lives in a struct
18267c478bd9Sstevel@tonic-gate  * prop_driver_data.  We use negative indexing to find the beginning of
18277c478bd9Sstevel@tonic-gate  * the structure and then free the entire structure using the size and
18287c478bd9Sstevel@tonic-gate  * the free routine stored in the structure.
18297c478bd9Sstevel@tonic-gate  */
18307c478bd9Sstevel@tonic-gate void
18317c478bd9Sstevel@tonic-gate ddi_prop_free(void *datap)
18327c478bd9Sstevel@tonic-gate {
18337c478bd9Sstevel@tonic-gate 	struct prop_driver_data *pdd;
18347c478bd9Sstevel@tonic-gate 
18357c478bd9Sstevel@tonic-gate 	/*
18367c478bd9Sstevel@tonic-gate 	 * Get the structure
18377c478bd9Sstevel@tonic-gate 	 */
18387c478bd9Sstevel@tonic-gate 	pdd = (struct prop_driver_data *)
18397c478bd9Sstevel@tonic-gate 	    ((caddr_t)datap - sizeof (struct prop_driver_data));
18407c478bd9Sstevel@tonic-gate 	/*
18417c478bd9Sstevel@tonic-gate 	 * Call the free routine to free it
18427c478bd9Sstevel@tonic-gate 	 */
18437c478bd9Sstevel@tonic-gate 	(*pdd->pdd_prop_free)(pdd);
18447c478bd9Sstevel@tonic-gate }
18457c478bd9Sstevel@tonic-gate 
18467c478bd9Sstevel@tonic-gate /*
18477c478bd9Sstevel@tonic-gate  * Free the data associated with an array of ints,
18487c478bd9Sstevel@tonic-gate  * allocated with ddi_prop_decode_alloc().
18497c478bd9Sstevel@tonic-gate  */
18507c478bd9Sstevel@tonic-gate static void
18517c478bd9Sstevel@tonic-gate ddi_prop_free_ints(struct prop_driver_data *pdd)
18527c478bd9Sstevel@tonic-gate {
18537c478bd9Sstevel@tonic-gate 	kmem_free(pdd, pdd->pdd_size);
18547c478bd9Sstevel@tonic-gate }
18557c478bd9Sstevel@tonic-gate 
18567c478bd9Sstevel@tonic-gate /*
18577c478bd9Sstevel@tonic-gate  * Free a single string property or a single string contained within
18587c478bd9Sstevel@tonic-gate  * the argv style return value of an array of strings.
18597c478bd9Sstevel@tonic-gate  */
18607c478bd9Sstevel@tonic-gate static void
18617c478bd9Sstevel@tonic-gate ddi_prop_free_string(struct prop_driver_data *pdd)
18627c478bd9Sstevel@tonic-gate {
18637c478bd9Sstevel@tonic-gate 	kmem_free(pdd, pdd->pdd_size);
18647c478bd9Sstevel@tonic-gate 
18657c478bd9Sstevel@tonic-gate }
18667c478bd9Sstevel@tonic-gate 
18677c478bd9Sstevel@tonic-gate /*
18687c478bd9Sstevel@tonic-gate  * Free an array of strings.
18697c478bd9Sstevel@tonic-gate  */
18707c478bd9Sstevel@tonic-gate static void
18717c478bd9Sstevel@tonic-gate ddi_prop_free_strings(struct prop_driver_data *pdd)
18727c478bd9Sstevel@tonic-gate {
18737c478bd9Sstevel@tonic-gate 	kmem_free(pdd, pdd->pdd_size);
18747c478bd9Sstevel@tonic-gate }
18757c478bd9Sstevel@tonic-gate 
18767c478bd9Sstevel@tonic-gate /*
18777c478bd9Sstevel@tonic-gate  * Free the data associated with an array of bytes.
18787c478bd9Sstevel@tonic-gate  */
18797c478bd9Sstevel@tonic-gate static void
18807c478bd9Sstevel@tonic-gate ddi_prop_free_bytes(struct prop_driver_data *pdd)
18817c478bd9Sstevel@tonic-gate {
18827c478bd9Sstevel@tonic-gate 	kmem_free(pdd, pdd->pdd_size);
18837c478bd9Sstevel@tonic-gate }
18847c478bd9Sstevel@tonic-gate 
18857c478bd9Sstevel@tonic-gate /*
18867c478bd9Sstevel@tonic-gate  * Reset the current location pointer in the property handle to the
18877c478bd9Sstevel@tonic-gate  * beginning of the data.
18887c478bd9Sstevel@tonic-gate  */
18897c478bd9Sstevel@tonic-gate void
18907c478bd9Sstevel@tonic-gate ddi_prop_reset_pos(prop_handle_t *ph)
18917c478bd9Sstevel@tonic-gate {
18927c478bd9Sstevel@tonic-gate 	ph->ph_cur_pos = ph->ph_data;
18937c478bd9Sstevel@tonic-gate 	ph->ph_save_pos = ph->ph_data;
18947c478bd9Sstevel@tonic-gate }
18957c478bd9Sstevel@tonic-gate 
18967c478bd9Sstevel@tonic-gate /*
18977c478bd9Sstevel@tonic-gate  * Restore the current location pointer in the property handle to the
18987c478bd9Sstevel@tonic-gate  * saved position.
18997c478bd9Sstevel@tonic-gate  */
19007c478bd9Sstevel@tonic-gate void
19017c478bd9Sstevel@tonic-gate ddi_prop_save_pos(prop_handle_t *ph)
19027c478bd9Sstevel@tonic-gate {
19037c478bd9Sstevel@tonic-gate 	ph->ph_save_pos = ph->ph_cur_pos;
19047c478bd9Sstevel@tonic-gate }
19057c478bd9Sstevel@tonic-gate 
19067c478bd9Sstevel@tonic-gate /*
19077c478bd9Sstevel@tonic-gate  * Save the location that the current location pointer is pointing to..
19087c478bd9Sstevel@tonic-gate  */
19097c478bd9Sstevel@tonic-gate void
19107c478bd9Sstevel@tonic-gate ddi_prop_restore_pos(prop_handle_t *ph)
19117c478bd9Sstevel@tonic-gate {
19127c478bd9Sstevel@tonic-gate 	ph->ph_cur_pos = ph->ph_save_pos;
19137c478bd9Sstevel@tonic-gate }
19147c478bd9Sstevel@tonic-gate 
19157c478bd9Sstevel@tonic-gate /*
19167c478bd9Sstevel@tonic-gate  * Property encode/decode functions
19177c478bd9Sstevel@tonic-gate  */
19187c478bd9Sstevel@tonic-gate 
19197c478bd9Sstevel@tonic-gate /*
19207c478bd9Sstevel@tonic-gate  * Decode a single integer property
19217c478bd9Sstevel@tonic-gate  */
19227c478bd9Sstevel@tonic-gate static int
19237c478bd9Sstevel@tonic-gate ddi_prop_fm_decode_int(prop_handle_t *ph, void *data, uint_t *nelements)
19247c478bd9Sstevel@tonic-gate {
19257c478bd9Sstevel@tonic-gate 	int	i;
19267c478bd9Sstevel@tonic-gate 	int	tmp;
19277c478bd9Sstevel@tonic-gate 
19287c478bd9Sstevel@tonic-gate 	/*
19297c478bd9Sstevel@tonic-gate 	 * If there is nothing to decode return an error
19307c478bd9Sstevel@tonic-gate 	 */
19317c478bd9Sstevel@tonic-gate 	if (ph->ph_size == 0)
19327c478bd9Sstevel@tonic-gate 		return (DDI_PROP_END_OF_DATA);
19337c478bd9Sstevel@tonic-gate 
19347c478bd9Sstevel@tonic-gate 	/*
19357c478bd9Sstevel@tonic-gate 	 * Decode the property as a single integer and return it
19367c478bd9Sstevel@tonic-gate 	 * in data if we were able to decode it.
19377c478bd9Sstevel@tonic-gate 	 */
19387c478bd9Sstevel@tonic-gate 	i = DDI_PROP_INT(ph, DDI_PROP_CMD_DECODE, &tmp);
19397c478bd9Sstevel@tonic-gate 	if (i < DDI_PROP_RESULT_OK) {
19407c478bd9Sstevel@tonic-gate 		switch (i) {
19417c478bd9Sstevel@tonic-gate 		case DDI_PROP_RESULT_EOF:
19427c478bd9Sstevel@tonic-gate 			return (DDI_PROP_END_OF_DATA);
19437c478bd9Sstevel@tonic-gate 
19447c478bd9Sstevel@tonic-gate 		case DDI_PROP_RESULT_ERROR:
19457c478bd9Sstevel@tonic-gate 			return (DDI_PROP_CANNOT_DECODE);
19467c478bd9Sstevel@tonic-gate 		}
19477c478bd9Sstevel@tonic-gate 	}
19487c478bd9Sstevel@tonic-gate 
19497c478bd9Sstevel@tonic-gate 	*(int *)data = tmp;
19507c478bd9Sstevel@tonic-gate 	*nelements = 1;
19517c478bd9Sstevel@tonic-gate 	return (DDI_PROP_SUCCESS);
19527c478bd9Sstevel@tonic-gate }
19537c478bd9Sstevel@tonic-gate 
19547c478bd9Sstevel@tonic-gate /*
19557c478bd9Sstevel@tonic-gate  * Decode a single 64 bit integer property
19567c478bd9Sstevel@tonic-gate  */
19577c478bd9Sstevel@tonic-gate static int
19587c478bd9Sstevel@tonic-gate ddi_prop_fm_decode_int64(prop_handle_t *ph, void *data, uint_t *nelements)
19597c478bd9Sstevel@tonic-gate {
19607c478bd9Sstevel@tonic-gate 	int	i;
19617c478bd9Sstevel@tonic-gate 	int64_t	tmp;
19627c478bd9Sstevel@tonic-gate 
19637c478bd9Sstevel@tonic-gate 	/*
19647c478bd9Sstevel@tonic-gate 	 * If there is nothing to decode return an error
19657c478bd9Sstevel@tonic-gate 	 */
19667c478bd9Sstevel@tonic-gate 	if (ph->ph_size == 0)
19677c478bd9Sstevel@tonic-gate 		return (DDI_PROP_END_OF_DATA);
19687c478bd9Sstevel@tonic-gate 
19697c478bd9Sstevel@tonic-gate 	/*
19707c478bd9Sstevel@tonic-gate 	 * Decode the property as a single integer and return it
19717c478bd9Sstevel@tonic-gate 	 * in data if we were able to decode it.
19727c478bd9Sstevel@tonic-gate 	 */
19737c478bd9Sstevel@tonic-gate 	i = DDI_PROP_INT64(ph, DDI_PROP_CMD_DECODE, &tmp);
19747c478bd9Sstevel@tonic-gate 	if (i < DDI_PROP_RESULT_OK) {
19757c478bd9Sstevel@tonic-gate 		switch (i) {
19767c478bd9Sstevel@tonic-gate 		case DDI_PROP_RESULT_EOF:
19777c478bd9Sstevel@tonic-gate 			return (DDI_PROP_END_OF_DATA);
19787c478bd9Sstevel@tonic-gate 
19797c478bd9Sstevel@tonic-gate 		case DDI_PROP_RESULT_ERROR:
19807c478bd9Sstevel@tonic-gate 			return (DDI_PROP_CANNOT_DECODE);
19817c478bd9Sstevel@tonic-gate 		}
19827c478bd9Sstevel@tonic-gate 	}
19837c478bd9Sstevel@tonic-gate 
19847c478bd9Sstevel@tonic-gate 	*(int64_t *)data = tmp;
19857c478bd9Sstevel@tonic-gate 	*nelements = 1;
19867c478bd9Sstevel@tonic-gate 	return (DDI_PROP_SUCCESS);
19877c478bd9Sstevel@tonic-gate }
19887c478bd9Sstevel@tonic-gate 
19897c478bd9Sstevel@tonic-gate /*
19907c478bd9Sstevel@tonic-gate  * Decode an array of integers property
19917c478bd9Sstevel@tonic-gate  */
19927c478bd9Sstevel@tonic-gate static int
19937c478bd9Sstevel@tonic-gate ddi_prop_fm_decode_ints(prop_handle_t *ph, void *data, uint_t *nelements)
19947c478bd9Sstevel@tonic-gate {
19957c478bd9Sstevel@tonic-gate 	int	i;
19967c478bd9Sstevel@tonic-gate 	int	cnt = 0;
19977c478bd9Sstevel@tonic-gate 	int	*tmp;
19987c478bd9Sstevel@tonic-gate 	int	*intp;
19997c478bd9Sstevel@tonic-gate 	int	n;
20007c478bd9Sstevel@tonic-gate 
20017c478bd9Sstevel@tonic-gate 	/*
20027c478bd9Sstevel@tonic-gate 	 * Figure out how many array elements there are by going through the
20037c478bd9Sstevel@tonic-gate 	 * data without decoding it first and counting.
20047c478bd9Sstevel@tonic-gate 	 */
20057c478bd9Sstevel@tonic-gate 	for (;;) {
20067c478bd9Sstevel@tonic-gate 		i = DDI_PROP_INT(ph, DDI_PROP_CMD_SKIP, NULL);
20077c478bd9Sstevel@tonic-gate 		if (i < 0)
20087c478bd9Sstevel@tonic-gate 			break;
20097c478bd9Sstevel@tonic-gate 		cnt++;
20107c478bd9Sstevel@tonic-gate 	}
20117c478bd9Sstevel@tonic-gate 
20127c478bd9Sstevel@tonic-gate 	/*
20137c478bd9Sstevel@tonic-gate 	 * If there are no elements return an error
20147c478bd9Sstevel@tonic-gate 	 */
20157c478bd9Sstevel@tonic-gate 	if (cnt == 0)
20167c478bd9Sstevel@tonic-gate 		return (DDI_PROP_END_OF_DATA);
20177c478bd9Sstevel@tonic-gate 
20187c478bd9Sstevel@tonic-gate 	/*
20197c478bd9Sstevel@tonic-gate 	 * If we cannot skip through the data, we cannot decode it
20207c478bd9Sstevel@tonic-gate 	 */
20217c478bd9Sstevel@tonic-gate 	if (i == DDI_PROP_RESULT_ERROR)
20227c478bd9Sstevel@tonic-gate 		return (DDI_PROP_CANNOT_DECODE);
20237c478bd9Sstevel@tonic-gate 
20247c478bd9Sstevel@tonic-gate 	/*
20257c478bd9Sstevel@tonic-gate 	 * Reset the data pointer to the beginning of the encoded data
20267c478bd9Sstevel@tonic-gate 	 */
20277c478bd9Sstevel@tonic-gate 	ddi_prop_reset_pos(ph);
20287c478bd9Sstevel@tonic-gate 
20297c478bd9Sstevel@tonic-gate 	/*
20307c478bd9Sstevel@tonic-gate 	 * Allocated memory to store the decoded value in.
20317c478bd9Sstevel@tonic-gate 	 */
20327c478bd9Sstevel@tonic-gate 	intp = ddi_prop_decode_alloc((cnt * sizeof (int)),
20337c478bd9Sstevel@tonic-gate 	    ddi_prop_free_ints);
20347c478bd9Sstevel@tonic-gate 
20357c478bd9Sstevel@tonic-gate 	/*
20367c478bd9Sstevel@tonic-gate 	 * Decode each element and place it in the space we just allocated
20377c478bd9Sstevel@tonic-gate 	 */
20387c478bd9Sstevel@tonic-gate 	tmp = intp;
20397c478bd9Sstevel@tonic-gate 	for (n = 0; n < cnt; n++, tmp++) {
20407c478bd9Sstevel@tonic-gate 		i = DDI_PROP_INT(ph, DDI_PROP_CMD_DECODE, tmp);
20417c478bd9Sstevel@tonic-gate 		if (i < DDI_PROP_RESULT_OK) {
20427c478bd9Sstevel@tonic-gate 			/*
20437c478bd9Sstevel@tonic-gate 			 * Free the space we just allocated
20447c478bd9Sstevel@tonic-gate 			 * and return an error.
20457c478bd9Sstevel@tonic-gate 			 */
20467c478bd9Sstevel@tonic-gate 			ddi_prop_free(intp);
20477c478bd9Sstevel@tonic-gate 			switch (i) {
20487c478bd9Sstevel@tonic-gate 			case DDI_PROP_RESULT_EOF:
20497c478bd9Sstevel@tonic-gate 				return (DDI_PROP_END_OF_DATA);
20507c478bd9Sstevel@tonic-gate 
20517c478bd9Sstevel@tonic-gate 			case DDI_PROP_RESULT_ERROR:
20527c478bd9Sstevel@tonic-gate 				return (DDI_PROP_CANNOT_DECODE);
20537c478bd9Sstevel@tonic-gate 			}
20547c478bd9Sstevel@tonic-gate 		}
20557c478bd9Sstevel@tonic-gate 	}
20567c478bd9Sstevel@tonic-gate 
20577c478bd9Sstevel@tonic-gate 	*nelements = cnt;
20587c478bd9Sstevel@tonic-gate 	*(int **)data = intp;
20597c478bd9Sstevel@tonic-gate 
20607c478bd9Sstevel@tonic-gate 	return (DDI_PROP_SUCCESS);
20617c478bd9Sstevel@tonic-gate }
20627c478bd9Sstevel@tonic-gate 
20637c478bd9Sstevel@tonic-gate /*
20647c478bd9Sstevel@tonic-gate  * Decode a 64 bit integer array property
20657c478bd9Sstevel@tonic-gate  */
20667c478bd9Sstevel@tonic-gate static int
20677c478bd9Sstevel@tonic-gate ddi_prop_fm_decode_int64_array(prop_handle_t *ph, void *data, uint_t *nelements)
20687c478bd9Sstevel@tonic-gate {
20697c478bd9Sstevel@tonic-gate 	int	i;
20707c478bd9Sstevel@tonic-gate 	int	n;
20717c478bd9Sstevel@tonic-gate 	int	cnt = 0;
20727c478bd9Sstevel@tonic-gate 	int64_t	*tmp;
20737c478bd9Sstevel@tonic-gate 	int64_t	*intp;
20747c478bd9Sstevel@tonic-gate 
20757c478bd9Sstevel@tonic-gate 	/*
20767c478bd9Sstevel@tonic-gate 	 * Count the number of array elements by going
20777c478bd9Sstevel@tonic-gate 	 * through the data without decoding it.
20787c478bd9Sstevel@tonic-gate 	 */
20797c478bd9Sstevel@tonic-gate 	for (;;) {
20807c478bd9Sstevel@tonic-gate 		i = DDI_PROP_INT64(ph, DDI_PROP_CMD_SKIP, NULL);
20817c478bd9Sstevel@tonic-gate 		if (i < 0)
20827c478bd9Sstevel@tonic-gate 			break;
20837c478bd9Sstevel@tonic-gate 		cnt++;
20847c478bd9Sstevel@tonic-gate 	}
20857c478bd9Sstevel@tonic-gate 
20867c478bd9Sstevel@tonic-gate 	/*
20877c478bd9Sstevel@tonic-gate 	 * If there are no elements return an error
20887c478bd9Sstevel@tonic-gate 	 */
20897c478bd9Sstevel@tonic-gate 	if (cnt == 0)
20907c478bd9Sstevel@tonic-gate 		return (DDI_PROP_END_OF_DATA);
20917c478bd9Sstevel@tonic-gate 
20927c478bd9Sstevel@tonic-gate 	/*
20937c478bd9Sstevel@tonic-gate 	 * If we cannot skip through the data, we cannot decode it
20947c478bd9Sstevel@tonic-gate 	 */
20957c478bd9Sstevel@tonic-gate 	if (i == DDI_PROP_RESULT_ERROR)
20967c478bd9Sstevel@tonic-gate 		return (DDI_PROP_CANNOT_DECODE);
20977c478bd9Sstevel@tonic-gate 
20987c478bd9Sstevel@tonic-gate 	/*
20997c478bd9Sstevel@tonic-gate 	 * Reset the data pointer to the beginning of the encoded data
21007c478bd9Sstevel@tonic-gate 	 */
21017c478bd9Sstevel@tonic-gate 	ddi_prop_reset_pos(ph);
21027c478bd9Sstevel@tonic-gate 
21037c478bd9Sstevel@tonic-gate 	/*
21047c478bd9Sstevel@tonic-gate 	 * Allocate memory to store the decoded value.
21057c478bd9Sstevel@tonic-gate 	 */
21067c478bd9Sstevel@tonic-gate 	intp = ddi_prop_decode_alloc((cnt * sizeof (int64_t)),
21077c478bd9Sstevel@tonic-gate 	    ddi_prop_free_ints);
21087c478bd9Sstevel@tonic-gate 
21097c478bd9Sstevel@tonic-gate 	/*
21107c478bd9Sstevel@tonic-gate 	 * Decode each element and place it in the space allocated
21117c478bd9Sstevel@tonic-gate 	 */
21127c478bd9Sstevel@tonic-gate 	tmp = intp;
21137c478bd9Sstevel@tonic-gate 	for (n = 0; n < cnt; n++, tmp++) {
21147c478bd9Sstevel@tonic-gate 		i = DDI_PROP_INT64(ph, DDI_PROP_CMD_DECODE, tmp);
21157c478bd9Sstevel@tonic-gate 		if (i < DDI_PROP_RESULT_OK) {
21167c478bd9Sstevel@tonic-gate 			/*
21177c478bd9Sstevel@tonic-gate 			 * Free the space we just allocated
21187c478bd9Sstevel@tonic-gate 			 * and return an error.
21197c478bd9Sstevel@tonic-gate 			 */
21207c478bd9Sstevel@tonic-gate 			ddi_prop_free(intp);
21217c478bd9Sstevel@tonic-gate 			switch (i) {
21227c478bd9Sstevel@tonic-gate 			case DDI_PROP_RESULT_EOF:
21237c478bd9Sstevel@tonic-gate 				return (DDI_PROP_END_OF_DATA);
21247c478bd9Sstevel@tonic-gate 
21257c478bd9Sstevel@tonic-gate 			case DDI_PROP_RESULT_ERROR:
21267c478bd9Sstevel@tonic-gate 				return (DDI_PROP_CANNOT_DECODE);
21277c478bd9Sstevel@tonic-gate 			}
21287c478bd9Sstevel@tonic-gate 		}
21297c478bd9Sstevel@tonic-gate 	}
21307c478bd9Sstevel@tonic-gate 
21317c478bd9Sstevel@tonic-gate 	*nelements = cnt;
21327c478bd9Sstevel@tonic-gate 	*(int64_t **)data = intp;
21337c478bd9Sstevel@tonic-gate 
21347c478bd9Sstevel@tonic-gate 	return (DDI_PROP_SUCCESS);
21357c478bd9Sstevel@tonic-gate }
21367c478bd9Sstevel@tonic-gate 
21377c478bd9Sstevel@tonic-gate /*
21387c478bd9Sstevel@tonic-gate  * Encode an array of integers property (Can be one element)
21397c478bd9Sstevel@tonic-gate  */
21407c478bd9Sstevel@tonic-gate int
21417c478bd9Sstevel@tonic-gate ddi_prop_fm_encode_ints(prop_handle_t *ph, void *data, uint_t nelements)
21427c478bd9Sstevel@tonic-gate {
21437c478bd9Sstevel@tonic-gate 	int	i;
21447c478bd9Sstevel@tonic-gate 	int	*tmp;
21457c478bd9Sstevel@tonic-gate 	int	cnt;
21467c478bd9Sstevel@tonic-gate 	int	size;
21477c478bd9Sstevel@tonic-gate 
21487c478bd9Sstevel@tonic-gate 	/*
21497c478bd9Sstevel@tonic-gate 	 * If there is no data, we cannot do anything
21507c478bd9Sstevel@tonic-gate 	 */
21517c478bd9Sstevel@tonic-gate 	if (nelements == 0)
21527c478bd9Sstevel@tonic-gate 		return (DDI_PROP_CANNOT_ENCODE);
21537c478bd9Sstevel@tonic-gate 
21547c478bd9Sstevel@tonic-gate 	/*
21557c478bd9Sstevel@tonic-gate 	 * Get the size of an encoded int.
21567c478bd9Sstevel@tonic-gate 	 */
21577c478bd9Sstevel@tonic-gate 	size = DDI_PROP_INT(ph, DDI_PROP_CMD_GET_ESIZE, NULL);
21587c478bd9Sstevel@tonic-gate 
21597c478bd9Sstevel@tonic-gate 	if (size < DDI_PROP_RESULT_OK) {
21607c478bd9Sstevel@tonic-gate 		switch (size) {
21617c478bd9Sstevel@tonic-gate 		case DDI_PROP_RESULT_EOF:
21627c478bd9Sstevel@tonic-gate 			return (DDI_PROP_END_OF_DATA);
21637c478bd9Sstevel@tonic-gate 
21647c478bd9Sstevel@tonic-gate 		case DDI_PROP_RESULT_ERROR:
21657c478bd9Sstevel@tonic-gate 			return (DDI_PROP_CANNOT_ENCODE);
21667c478bd9Sstevel@tonic-gate 		}
21677c478bd9Sstevel@tonic-gate 	}
21687c478bd9Sstevel@tonic-gate 
21697c478bd9Sstevel@tonic-gate 	/*
21707c478bd9Sstevel@tonic-gate 	 * Allocate space in the handle to store the encoded int.
21717c478bd9Sstevel@tonic-gate 	 */
21727c478bd9Sstevel@tonic-gate 	if (ddi_prop_encode_alloc(ph, size * nelements) !=
21737c478bd9Sstevel@tonic-gate 	    DDI_PROP_SUCCESS)
21747c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NO_MEMORY);
21757c478bd9Sstevel@tonic-gate 
21767c478bd9Sstevel@tonic-gate 	/*
21777c478bd9Sstevel@tonic-gate 	 * Encode the array of ints.
21787c478bd9Sstevel@tonic-gate 	 */
21797c478bd9Sstevel@tonic-gate 	tmp = (int *)data;
21807c478bd9Sstevel@tonic-gate 	for (cnt = 0; cnt < nelements; cnt++, tmp++) {
21817c478bd9Sstevel@tonic-gate 		i = DDI_PROP_INT(ph, DDI_PROP_CMD_ENCODE, tmp);
21827c478bd9Sstevel@tonic-gate 		if (i < DDI_PROP_RESULT_OK) {
21837c478bd9Sstevel@tonic-gate 			switch (i) {
21847c478bd9Sstevel@tonic-gate 			case DDI_PROP_RESULT_EOF:
21857c478bd9Sstevel@tonic-gate 				return (DDI_PROP_END_OF_DATA);
21867c478bd9Sstevel@tonic-gate 
21877c478bd9Sstevel@tonic-gate 			case DDI_PROP_RESULT_ERROR:
21887c478bd9Sstevel@tonic-gate 				return (DDI_PROP_CANNOT_ENCODE);
21897c478bd9Sstevel@tonic-gate 			}
21907c478bd9Sstevel@tonic-gate 		}
21917c478bd9Sstevel@tonic-gate 	}
21927c478bd9Sstevel@tonic-gate 
21937c478bd9Sstevel@tonic-gate 	return (DDI_PROP_SUCCESS);
21947c478bd9Sstevel@tonic-gate }
21957c478bd9Sstevel@tonic-gate 
21967c478bd9Sstevel@tonic-gate 
21977c478bd9Sstevel@tonic-gate /*
21987c478bd9Sstevel@tonic-gate  * Encode a 64 bit integer array property
21997c478bd9Sstevel@tonic-gate  */
22007c478bd9Sstevel@tonic-gate int
22017c478bd9Sstevel@tonic-gate ddi_prop_fm_encode_int64(prop_handle_t *ph, void *data, uint_t nelements)
22027c478bd9Sstevel@tonic-gate {
22037c478bd9Sstevel@tonic-gate 	int i;
22047c478bd9Sstevel@tonic-gate 	int cnt;
22057c478bd9Sstevel@tonic-gate 	int size;
22067c478bd9Sstevel@tonic-gate 	int64_t *tmp;
22077c478bd9Sstevel@tonic-gate 
22087c478bd9Sstevel@tonic-gate 	/*
22097c478bd9Sstevel@tonic-gate 	 * If there is no data, we cannot do anything
22107c478bd9Sstevel@tonic-gate 	 */
22117c478bd9Sstevel@tonic-gate 	if (nelements == 0)
22127c478bd9Sstevel@tonic-gate 		return (DDI_PROP_CANNOT_ENCODE);
22137c478bd9Sstevel@tonic-gate 
22147c478bd9Sstevel@tonic-gate 	/*
22157c478bd9Sstevel@tonic-gate 	 * Get the size of an encoded 64 bit int.
22167c478bd9Sstevel@tonic-gate 	 */
22177c478bd9Sstevel@tonic-gate 	size = DDI_PROP_INT64(ph, DDI_PROP_CMD_GET_ESIZE, NULL);
22187c478bd9Sstevel@tonic-gate 
22197c478bd9Sstevel@tonic-gate 	if (size < DDI_PROP_RESULT_OK) {
22207c478bd9Sstevel@tonic-gate 		switch (size) {
22217c478bd9Sstevel@tonic-gate 		case DDI_PROP_RESULT_EOF:
22227c478bd9Sstevel@tonic-gate 			return (DDI_PROP_END_OF_DATA);
22237c478bd9Sstevel@tonic-gate 
22247c478bd9Sstevel@tonic-gate 		case DDI_PROP_RESULT_ERROR:
22257c478bd9Sstevel@tonic-gate 			return (DDI_PROP_CANNOT_ENCODE);
22267c478bd9Sstevel@tonic-gate 		}
22277c478bd9Sstevel@tonic-gate 	}
22287c478bd9Sstevel@tonic-gate 
22297c478bd9Sstevel@tonic-gate 	/*
22307c478bd9Sstevel@tonic-gate 	 * Allocate space in the handle to store the encoded int.
22317c478bd9Sstevel@tonic-gate 	 */
22327c478bd9Sstevel@tonic-gate 	if (ddi_prop_encode_alloc(ph, size * nelements) !=
22337c478bd9Sstevel@tonic-gate 	    DDI_PROP_SUCCESS)
22347c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NO_MEMORY);
22357c478bd9Sstevel@tonic-gate 
22367c478bd9Sstevel@tonic-gate 	/*
22377c478bd9Sstevel@tonic-gate 	 * Encode the array of ints.
22387c478bd9Sstevel@tonic-gate 	 */
22397c478bd9Sstevel@tonic-gate 	tmp = (int64_t *)data;
22407c478bd9Sstevel@tonic-gate 	for (cnt = 0; cnt < nelements; cnt++, tmp++) {
22417c478bd9Sstevel@tonic-gate 		i = DDI_PROP_INT64(ph, DDI_PROP_CMD_ENCODE, tmp);
22427c478bd9Sstevel@tonic-gate 		if (i < DDI_PROP_RESULT_OK) {
22437c478bd9Sstevel@tonic-gate 			switch (i) {
22447c478bd9Sstevel@tonic-gate 			case DDI_PROP_RESULT_EOF:
22457c478bd9Sstevel@tonic-gate 				return (DDI_PROP_END_OF_DATA);
22467c478bd9Sstevel@tonic-gate 
22477c478bd9Sstevel@tonic-gate 			case DDI_PROP_RESULT_ERROR:
22487c478bd9Sstevel@tonic-gate 				return (DDI_PROP_CANNOT_ENCODE);
22497c478bd9Sstevel@tonic-gate 			}
22507c478bd9Sstevel@tonic-gate 		}
22517c478bd9Sstevel@tonic-gate 	}
22527c478bd9Sstevel@tonic-gate 
22537c478bd9Sstevel@tonic-gate 	return (DDI_PROP_SUCCESS);
22547c478bd9Sstevel@tonic-gate }
22557c478bd9Sstevel@tonic-gate 
22567c478bd9Sstevel@tonic-gate /*
22577c478bd9Sstevel@tonic-gate  * Decode a single string property
22587c478bd9Sstevel@tonic-gate  */
22597c478bd9Sstevel@tonic-gate static int
22607c478bd9Sstevel@tonic-gate ddi_prop_fm_decode_string(prop_handle_t *ph, void *data, uint_t *nelements)
22617c478bd9Sstevel@tonic-gate {
22627c478bd9Sstevel@tonic-gate 	char		*tmp;
22637c478bd9Sstevel@tonic-gate 	char		*str;
22647c478bd9Sstevel@tonic-gate 	int		i;
22657c478bd9Sstevel@tonic-gate 	int		size;
22667c478bd9Sstevel@tonic-gate 
22677c478bd9Sstevel@tonic-gate 	/*
22687c478bd9Sstevel@tonic-gate 	 * If there is nothing to decode return an error
22697c478bd9Sstevel@tonic-gate 	 */
22707c478bd9Sstevel@tonic-gate 	if (ph->ph_size == 0)
22717c478bd9Sstevel@tonic-gate 		return (DDI_PROP_END_OF_DATA);
22727c478bd9Sstevel@tonic-gate 
22737c478bd9Sstevel@tonic-gate 	/*
22747c478bd9Sstevel@tonic-gate 	 * Get the decoded size of the encoded string.
22757c478bd9Sstevel@tonic-gate 	 */
22767c478bd9Sstevel@tonic-gate 	size = DDI_PROP_STR(ph, DDI_PROP_CMD_GET_DSIZE, NULL);
22777c478bd9Sstevel@tonic-gate 	if (size < DDI_PROP_RESULT_OK) {
22787c478bd9Sstevel@tonic-gate 		switch (size) {
22797c478bd9Sstevel@tonic-gate 		case DDI_PROP_RESULT_EOF:
22807c478bd9Sstevel@tonic-gate 			return (DDI_PROP_END_OF_DATA);
22817c478bd9Sstevel@tonic-gate 
22827c478bd9Sstevel@tonic-gate 		case DDI_PROP_RESULT_ERROR:
22837c478bd9Sstevel@tonic-gate 			return (DDI_PROP_CANNOT_DECODE);
22847c478bd9Sstevel@tonic-gate 		}
22857c478bd9Sstevel@tonic-gate 	}
22867c478bd9Sstevel@tonic-gate 
22877c478bd9Sstevel@tonic-gate 	/*
22887c478bd9Sstevel@tonic-gate 	 * Allocated memory to store the decoded value in.
22897c478bd9Sstevel@tonic-gate 	 */
22907c478bd9Sstevel@tonic-gate 	str = ddi_prop_decode_alloc((size_t)size, ddi_prop_free_string);
22917c478bd9Sstevel@tonic-gate 
22927c478bd9Sstevel@tonic-gate 	ddi_prop_reset_pos(ph);
22937c478bd9Sstevel@tonic-gate 
22947c478bd9Sstevel@tonic-gate 	/*
22957c478bd9Sstevel@tonic-gate 	 * Decode the str and place it in the space we just allocated
22967c478bd9Sstevel@tonic-gate 	 */
22977c478bd9Sstevel@tonic-gate 	tmp = str;
22987c478bd9Sstevel@tonic-gate 	i = DDI_PROP_STR(ph, DDI_PROP_CMD_DECODE, tmp);
22997c478bd9Sstevel@tonic-gate 	if (i < DDI_PROP_RESULT_OK) {
23007c478bd9Sstevel@tonic-gate 		/*
23017c478bd9Sstevel@tonic-gate 		 * Free the space we just allocated
23027c478bd9Sstevel@tonic-gate 		 * and return an error.
23037c478bd9Sstevel@tonic-gate 		 */
23047c478bd9Sstevel@tonic-gate 		ddi_prop_free(str);
23057c478bd9Sstevel@tonic-gate 		switch (i) {
23067c478bd9Sstevel@tonic-gate 		case DDI_PROP_RESULT_EOF:
23077c478bd9Sstevel@tonic-gate 			return (DDI_PROP_END_OF_DATA);
23087c478bd9Sstevel@tonic-gate 
23097c478bd9Sstevel@tonic-gate 		case DDI_PROP_RESULT_ERROR:
23107c478bd9Sstevel@tonic-gate 			return (DDI_PROP_CANNOT_DECODE);
23117c478bd9Sstevel@tonic-gate 		}
23127c478bd9Sstevel@tonic-gate 	}
23137c478bd9Sstevel@tonic-gate 
23147c478bd9Sstevel@tonic-gate 	*(char **)data = str;
23157c478bd9Sstevel@tonic-gate 	*nelements = 1;
23167c478bd9Sstevel@tonic-gate 
23177c478bd9Sstevel@tonic-gate 	return (DDI_PROP_SUCCESS);
23187c478bd9Sstevel@tonic-gate }
23197c478bd9Sstevel@tonic-gate 
23207c478bd9Sstevel@tonic-gate /*
23217c478bd9Sstevel@tonic-gate  * Decode an array of strings.
23227c478bd9Sstevel@tonic-gate  */
23237c478bd9Sstevel@tonic-gate int
23247c478bd9Sstevel@tonic-gate ddi_prop_fm_decode_strings(prop_handle_t *ph, void *data, uint_t *nelements)
23257c478bd9Sstevel@tonic-gate {
23267c478bd9Sstevel@tonic-gate 	int		cnt = 0;
23277c478bd9Sstevel@tonic-gate 	char		**strs;
23287c478bd9Sstevel@tonic-gate 	char		**tmp;
23297c478bd9Sstevel@tonic-gate 	char		*ptr;
23307c478bd9Sstevel@tonic-gate 	int		i;
23317c478bd9Sstevel@tonic-gate 	int		n;
23327c478bd9Sstevel@tonic-gate 	int		size;
23337c478bd9Sstevel@tonic-gate 	size_t		nbytes;
23347c478bd9Sstevel@tonic-gate 
23357c478bd9Sstevel@tonic-gate 	/*
23367c478bd9Sstevel@tonic-gate 	 * Figure out how many array elements there are by going through the
23377c478bd9Sstevel@tonic-gate 	 * data without decoding it first and counting.
23387c478bd9Sstevel@tonic-gate 	 */
23397c478bd9Sstevel@tonic-gate 	for (;;) {
23407c478bd9Sstevel@tonic-gate 		i = DDI_PROP_STR(ph, DDI_PROP_CMD_SKIP, NULL);
23417c478bd9Sstevel@tonic-gate 		if (i < 0)
23427c478bd9Sstevel@tonic-gate 			break;
23437c478bd9Sstevel@tonic-gate 		cnt++;
23447c478bd9Sstevel@tonic-gate 	}
23457c478bd9Sstevel@tonic-gate 
23467c478bd9Sstevel@tonic-gate 	/*
23477c478bd9Sstevel@tonic-gate 	 * If there are no elements return an error
23487c478bd9Sstevel@tonic-gate 	 */
23497c478bd9Sstevel@tonic-gate 	if (cnt == 0)
23507c478bd9Sstevel@tonic-gate 		return (DDI_PROP_END_OF_DATA);
23517c478bd9Sstevel@tonic-gate 
23527c478bd9Sstevel@tonic-gate 	/*
23537c478bd9Sstevel@tonic-gate 	 * If we cannot skip through the data, we cannot decode it
23547c478bd9Sstevel@tonic-gate 	 */
23557c478bd9Sstevel@tonic-gate 	if (i == DDI_PROP_RESULT_ERROR)
23567c478bd9Sstevel@tonic-gate 		return (DDI_PROP_CANNOT_DECODE);
23577c478bd9Sstevel@tonic-gate 
23587c478bd9Sstevel@tonic-gate 	/*
23597c478bd9Sstevel@tonic-gate 	 * Reset the data pointer to the beginning of the encoded data
23607c478bd9Sstevel@tonic-gate 	 */
23617c478bd9Sstevel@tonic-gate 	ddi_prop_reset_pos(ph);
23627c478bd9Sstevel@tonic-gate 
23637c478bd9Sstevel@tonic-gate 	/*
23647c478bd9Sstevel@tonic-gate 	 * Figure out how much memory we need for the sum total
23657c478bd9Sstevel@tonic-gate 	 */
23667c478bd9Sstevel@tonic-gate 	nbytes = (cnt + 1) * sizeof (char *);
23677c478bd9Sstevel@tonic-gate 
23687c478bd9Sstevel@tonic-gate 	for (n = 0; n < cnt; n++) {
23697c478bd9Sstevel@tonic-gate 		/*
23707c478bd9Sstevel@tonic-gate 		 * Get the decoded size of the current encoded string.
23717c478bd9Sstevel@tonic-gate 		 */
23727c478bd9Sstevel@tonic-gate 		size = DDI_PROP_STR(ph, DDI_PROP_CMD_GET_DSIZE, NULL);
23737c478bd9Sstevel@tonic-gate 		if (size < DDI_PROP_RESULT_OK) {
23747c478bd9Sstevel@tonic-gate 			switch (size) {
23757c478bd9Sstevel@tonic-gate 			case DDI_PROP_RESULT_EOF:
23767c478bd9Sstevel@tonic-gate 				return (DDI_PROP_END_OF_DATA);
23777c478bd9Sstevel@tonic-gate 
23787c478bd9Sstevel@tonic-gate 			case DDI_PROP_RESULT_ERROR:
23797c478bd9Sstevel@tonic-gate 				return (DDI_PROP_CANNOT_DECODE);
23807c478bd9Sstevel@tonic-gate 			}
23817c478bd9Sstevel@tonic-gate 		}
23827c478bd9Sstevel@tonic-gate 
23837c478bd9Sstevel@tonic-gate 		nbytes += size;
23847c478bd9Sstevel@tonic-gate 	}
23857c478bd9Sstevel@tonic-gate 
23867c478bd9Sstevel@tonic-gate 	/*
23877c478bd9Sstevel@tonic-gate 	 * Allocate memory in which to store the decoded strings.
23887c478bd9Sstevel@tonic-gate 	 */
23897c478bd9Sstevel@tonic-gate 	strs = ddi_prop_decode_alloc(nbytes, ddi_prop_free_strings);
23907c478bd9Sstevel@tonic-gate 
23917c478bd9Sstevel@tonic-gate 	/*
23927c478bd9Sstevel@tonic-gate 	 * Set up pointers for each string by figuring out yet
23937c478bd9Sstevel@tonic-gate 	 * again how long each string is.
23947c478bd9Sstevel@tonic-gate 	 */
23957c478bd9Sstevel@tonic-gate 	ddi_prop_reset_pos(ph);
23967c478bd9Sstevel@tonic-gate 	ptr = (caddr_t)strs + ((cnt + 1) * sizeof (char *));
23977c478bd9Sstevel@tonic-gate 	for (tmp = strs, n = 0; n < cnt; n++, tmp++) {
23987c478bd9Sstevel@tonic-gate 		/*
23997c478bd9Sstevel@tonic-gate 		 * Get the decoded size of the current encoded string.
24007c478bd9Sstevel@tonic-gate 		 */
24017c478bd9Sstevel@tonic-gate 		size = DDI_PROP_STR(ph, DDI_PROP_CMD_GET_DSIZE, NULL);
24027c478bd9Sstevel@tonic-gate 		if (size < DDI_PROP_RESULT_OK) {
24037c478bd9Sstevel@tonic-gate 			ddi_prop_free(strs);
24047c478bd9Sstevel@tonic-gate 			switch (size) {
24057c478bd9Sstevel@tonic-gate 			case DDI_PROP_RESULT_EOF:
24067c478bd9Sstevel@tonic-gate 				return (DDI_PROP_END_OF_DATA);
24077c478bd9Sstevel@tonic-gate 
24087c478bd9Sstevel@tonic-gate 			case DDI_PROP_RESULT_ERROR:
24097c478bd9Sstevel@tonic-gate 				return (DDI_PROP_CANNOT_DECODE);
24107c478bd9Sstevel@tonic-gate 			}
24117c478bd9Sstevel@tonic-gate 		}
24127c478bd9Sstevel@tonic-gate 
24137c478bd9Sstevel@tonic-gate 		*tmp = ptr;
24147c478bd9Sstevel@tonic-gate 		ptr += size;
24157c478bd9Sstevel@tonic-gate 	}
24167c478bd9Sstevel@tonic-gate 
24177c478bd9Sstevel@tonic-gate 	/*
24187c478bd9Sstevel@tonic-gate 	 * String array is terminated by a NULL
24197c478bd9Sstevel@tonic-gate 	 */
24207c478bd9Sstevel@tonic-gate 	*tmp = NULL;
24217c478bd9Sstevel@tonic-gate 
24227c478bd9Sstevel@tonic-gate 	/*
24237c478bd9Sstevel@tonic-gate 	 * Finally, we can decode each string
24247c478bd9Sstevel@tonic-gate 	 */
24257c478bd9Sstevel@tonic-gate 	ddi_prop_reset_pos(ph);
24267c478bd9Sstevel@tonic-gate 	for (tmp = strs, n = 0; n < cnt; n++, tmp++) {
24277c478bd9Sstevel@tonic-gate 		i = DDI_PROP_STR(ph, DDI_PROP_CMD_DECODE, *tmp);
24287c478bd9Sstevel@tonic-gate 		if (i < DDI_PROP_RESULT_OK) {
24297c478bd9Sstevel@tonic-gate 			/*
24307c478bd9Sstevel@tonic-gate 			 * Free the space we just allocated
24317c478bd9Sstevel@tonic-gate 			 * and return an error
24327c478bd9Sstevel@tonic-gate 			 */
24337c478bd9Sstevel@tonic-gate 			ddi_prop_free(strs);
24347c478bd9Sstevel@tonic-gate 			switch (i) {
24357c478bd9Sstevel@tonic-gate 			case DDI_PROP_RESULT_EOF:
24367c478bd9Sstevel@tonic-gate 				return (DDI_PROP_END_OF_DATA);
24377c478bd9Sstevel@tonic-gate 
24387c478bd9Sstevel@tonic-gate 			case DDI_PROP_RESULT_ERROR:
24397c478bd9Sstevel@tonic-gate 				return (DDI_PROP_CANNOT_DECODE);
24407c478bd9Sstevel@tonic-gate 			}
24417c478bd9Sstevel@tonic-gate 		}
24427c478bd9Sstevel@tonic-gate 	}
24437c478bd9Sstevel@tonic-gate 
24447c478bd9Sstevel@tonic-gate 	*(char ***)data = strs;
24457c478bd9Sstevel@tonic-gate 	*nelements = cnt;
24467c478bd9Sstevel@tonic-gate 
24477c478bd9Sstevel@tonic-gate 	return (DDI_PROP_SUCCESS);
24487c478bd9Sstevel@tonic-gate }
24497c478bd9Sstevel@tonic-gate 
24507c478bd9Sstevel@tonic-gate /*
24517c478bd9Sstevel@tonic-gate  * Encode a string.
24527c478bd9Sstevel@tonic-gate  */
24537c478bd9Sstevel@tonic-gate int
24547c478bd9Sstevel@tonic-gate ddi_prop_fm_encode_string(prop_handle_t *ph, void *data, uint_t nelements)
24557c478bd9Sstevel@tonic-gate {
24567c478bd9Sstevel@tonic-gate 	char		**tmp;
24577c478bd9Sstevel@tonic-gate 	int		size;
24587c478bd9Sstevel@tonic-gate 	int		i;
24597c478bd9Sstevel@tonic-gate 
24607c478bd9Sstevel@tonic-gate 	/*
24617c478bd9Sstevel@tonic-gate 	 * If there is no data, we cannot do anything
24627c478bd9Sstevel@tonic-gate 	 */
24637c478bd9Sstevel@tonic-gate 	if (nelements == 0)
24647c478bd9Sstevel@tonic-gate 		return (DDI_PROP_CANNOT_ENCODE);
24657c478bd9Sstevel@tonic-gate 
24667c478bd9Sstevel@tonic-gate 	/*
24677c478bd9Sstevel@tonic-gate 	 * Get the size of the encoded string.
24687c478bd9Sstevel@tonic-gate 	 */
24697c478bd9Sstevel@tonic-gate 	tmp = (char **)data;
24707c478bd9Sstevel@tonic-gate 	size = DDI_PROP_STR(ph, DDI_PROP_CMD_GET_ESIZE, *tmp);
24717c478bd9Sstevel@tonic-gate 	if (size < DDI_PROP_RESULT_OK) {
24727c478bd9Sstevel@tonic-gate 		switch (size) {
24737c478bd9Sstevel@tonic-gate 		case DDI_PROP_RESULT_EOF:
24747c478bd9Sstevel@tonic-gate 			return (DDI_PROP_END_OF_DATA);
24757c478bd9Sstevel@tonic-gate 
24767c478bd9Sstevel@tonic-gate 		case DDI_PROP_RESULT_ERROR:
24777c478bd9Sstevel@tonic-gate 			return (DDI_PROP_CANNOT_ENCODE);
24787c478bd9Sstevel@tonic-gate 		}
24797c478bd9Sstevel@tonic-gate 	}
24807c478bd9Sstevel@tonic-gate 
24817c478bd9Sstevel@tonic-gate 	/*
24827c478bd9Sstevel@tonic-gate 	 * Allocate space in the handle to store the encoded string.
24837c478bd9Sstevel@tonic-gate 	 */
24847c478bd9Sstevel@tonic-gate 	if (ddi_prop_encode_alloc(ph, size) != DDI_PROP_SUCCESS)
24857c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NO_MEMORY);
24867c478bd9Sstevel@tonic-gate 
24877c478bd9Sstevel@tonic-gate 	ddi_prop_reset_pos(ph);
24887c478bd9Sstevel@tonic-gate 
24897c478bd9Sstevel@tonic-gate 	/*
24907c478bd9Sstevel@tonic-gate 	 * Encode the string.
24917c478bd9Sstevel@tonic-gate 	 */
24927c478bd9Sstevel@tonic-gate 	tmp = (char **)data;
24937c478bd9Sstevel@tonic-gate 	i = DDI_PROP_STR(ph, DDI_PROP_CMD_ENCODE, *tmp);
24947c478bd9Sstevel@tonic-gate 	if (i < DDI_PROP_RESULT_OK) {
24957c478bd9Sstevel@tonic-gate 		switch (i) {
24967c478bd9Sstevel@tonic-gate 		case DDI_PROP_RESULT_EOF:
24977c478bd9Sstevel@tonic-gate 			return (DDI_PROP_END_OF_DATA);
24987c478bd9Sstevel@tonic-gate 
24997c478bd9Sstevel@tonic-gate 		case DDI_PROP_RESULT_ERROR:
25007c478bd9Sstevel@tonic-gate 			return (DDI_PROP_CANNOT_ENCODE);
25017c478bd9Sstevel@tonic-gate 		}
25027c478bd9Sstevel@tonic-gate 	}
25037c478bd9Sstevel@tonic-gate 
25047c478bd9Sstevel@tonic-gate 	return (DDI_PROP_SUCCESS);
25057c478bd9Sstevel@tonic-gate }
25067c478bd9Sstevel@tonic-gate 
25077c478bd9Sstevel@tonic-gate 
25087c478bd9Sstevel@tonic-gate /*
25097c478bd9Sstevel@tonic-gate  * Encode an array of strings.
25107c478bd9Sstevel@tonic-gate  */
25117c478bd9Sstevel@tonic-gate int
25127c478bd9Sstevel@tonic-gate ddi_prop_fm_encode_strings(prop_handle_t *ph, void *data, uint_t nelements)
25137c478bd9Sstevel@tonic-gate {
25147c478bd9Sstevel@tonic-gate 	int		cnt = 0;
25157c478bd9Sstevel@tonic-gate 	char		**tmp;
25167c478bd9Sstevel@tonic-gate 	int		size;
25177c478bd9Sstevel@tonic-gate 	uint_t		total_size;
25187c478bd9Sstevel@tonic-gate 	int		i;
25197c478bd9Sstevel@tonic-gate 
25207c478bd9Sstevel@tonic-gate 	/*
25217c478bd9Sstevel@tonic-gate 	 * If there is no data, we cannot do anything
25227c478bd9Sstevel@tonic-gate 	 */
25237c478bd9Sstevel@tonic-gate 	if (nelements == 0)
25247c478bd9Sstevel@tonic-gate 		return (DDI_PROP_CANNOT_ENCODE);
25257c478bd9Sstevel@tonic-gate 
25267c478bd9Sstevel@tonic-gate 	/*
25277c478bd9Sstevel@tonic-gate 	 * Get the total size required to encode all the strings.
25287c478bd9Sstevel@tonic-gate 	 */
25297c478bd9Sstevel@tonic-gate 	total_size = 0;
25307c478bd9Sstevel@tonic-gate 	tmp = (char **)data;
25317c478bd9Sstevel@tonic-gate 	for (cnt = 0; cnt < nelements; cnt++, tmp++) {
25327c478bd9Sstevel@tonic-gate 		size = DDI_PROP_STR(ph, DDI_PROP_CMD_GET_ESIZE, *tmp);
25337c478bd9Sstevel@tonic-gate 		if (size < DDI_PROP_RESULT_OK) {
25347c478bd9Sstevel@tonic-gate 			switch (size) {
25357c478bd9Sstevel@tonic-gate 			case DDI_PROP_RESULT_EOF:
25367c478bd9Sstevel@tonic-gate 				return (DDI_PROP_END_OF_DATA);
25377c478bd9Sstevel@tonic-gate 
25387c478bd9Sstevel@tonic-gate 			case DDI_PROP_RESULT_ERROR:
25397c478bd9Sstevel@tonic-gate 				return (DDI_PROP_CANNOT_ENCODE);
25407c478bd9Sstevel@tonic-gate 			}
25417c478bd9Sstevel@tonic-gate 		}
25427c478bd9Sstevel@tonic-gate 		total_size += (uint_t)size;
25437c478bd9Sstevel@tonic-gate 	}
25447c478bd9Sstevel@tonic-gate 
25457c478bd9Sstevel@tonic-gate 	/*
25467c478bd9Sstevel@tonic-gate 	 * Allocate space in the handle to store the encoded strings.
25477c478bd9Sstevel@tonic-gate 	 */
25487c478bd9Sstevel@tonic-gate 	if (ddi_prop_encode_alloc(ph, total_size) != DDI_PROP_SUCCESS)
25497c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NO_MEMORY);
25507c478bd9Sstevel@tonic-gate 
25517c478bd9Sstevel@tonic-gate 	ddi_prop_reset_pos(ph);
25527c478bd9Sstevel@tonic-gate 
25537c478bd9Sstevel@tonic-gate 	/*
25547c478bd9Sstevel@tonic-gate 	 * Encode the array of strings.
25557c478bd9Sstevel@tonic-gate 	 */
25567c478bd9Sstevel@tonic-gate 	tmp = (char **)data;
25577c478bd9Sstevel@tonic-gate 	for (cnt = 0; cnt < nelements; cnt++, tmp++) {
25587c478bd9Sstevel@tonic-gate 		i = DDI_PROP_STR(ph, DDI_PROP_CMD_ENCODE, *tmp);
25597c478bd9Sstevel@tonic-gate 		if (i < DDI_PROP_RESULT_OK) {
25607c478bd9Sstevel@tonic-gate 			switch (i) {
25617c478bd9Sstevel@tonic-gate 			case DDI_PROP_RESULT_EOF:
25627c478bd9Sstevel@tonic-gate 				return (DDI_PROP_END_OF_DATA);
25637c478bd9Sstevel@tonic-gate 
25647c478bd9Sstevel@tonic-gate 			case DDI_PROP_RESULT_ERROR:
25657c478bd9Sstevel@tonic-gate 				return (DDI_PROP_CANNOT_ENCODE);
25667c478bd9Sstevel@tonic-gate 			}
25677c478bd9Sstevel@tonic-gate 		}
25687c478bd9Sstevel@tonic-gate 	}
25697c478bd9Sstevel@tonic-gate 
25707c478bd9Sstevel@tonic-gate 	return (DDI_PROP_SUCCESS);
25717c478bd9Sstevel@tonic-gate }
25727c478bd9Sstevel@tonic-gate 
25737c478bd9Sstevel@tonic-gate 
25747c478bd9Sstevel@tonic-gate /*
25757c478bd9Sstevel@tonic-gate  * Decode an array of bytes.
25767c478bd9Sstevel@tonic-gate  */
25777c478bd9Sstevel@tonic-gate static int
25787c478bd9Sstevel@tonic-gate ddi_prop_fm_decode_bytes(prop_handle_t *ph, void *data, uint_t *nelements)
25797c478bd9Sstevel@tonic-gate {
25807c478bd9Sstevel@tonic-gate 	uchar_t		*tmp;
25817c478bd9Sstevel@tonic-gate 	int		nbytes;
25827c478bd9Sstevel@tonic-gate 	int		i;
25837c478bd9Sstevel@tonic-gate 
25847c478bd9Sstevel@tonic-gate 	/*
25857c478bd9Sstevel@tonic-gate 	 * If there are no elements return an error
25867c478bd9Sstevel@tonic-gate 	 */
25877c478bd9Sstevel@tonic-gate 	if (ph->ph_size == 0)
25887c478bd9Sstevel@tonic-gate 		return (DDI_PROP_END_OF_DATA);
25897c478bd9Sstevel@tonic-gate 
25907c478bd9Sstevel@tonic-gate 	/*
25917c478bd9Sstevel@tonic-gate 	 * Get the size of the encoded array of bytes.
25927c478bd9Sstevel@tonic-gate 	 */
25937c478bd9Sstevel@tonic-gate 	nbytes = DDI_PROP_BYTES(ph, DDI_PROP_CMD_GET_DSIZE,
25947c478bd9Sstevel@tonic-gate 	    data, ph->ph_size);
25957c478bd9Sstevel@tonic-gate 	if (nbytes < DDI_PROP_RESULT_OK) {
25967c478bd9Sstevel@tonic-gate 		switch (nbytes) {
25977c478bd9Sstevel@tonic-gate 		case DDI_PROP_RESULT_EOF:
25987c478bd9Sstevel@tonic-gate 			return (DDI_PROP_END_OF_DATA);
25997c478bd9Sstevel@tonic-gate 
26007c478bd9Sstevel@tonic-gate 		case DDI_PROP_RESULT_ERROR:
26017c478bd9Sstevel@tonic-gate 			return (DDI_PROP_CANNOT_DECODE);
26027c478bd9Sstevel@tonic-gate 		}
26037c478bd9Sstevel@tonic-gate 	}
26047c478bd9Sstevel@tonic-gate 
26057c478bd9Sstevel@tonic-gate 	/*
26067c478bd9Sstevel@tonic-gate 	 * Allocated memory to store the decoded value in.
26077c478bd9Sstevel@tonic-gate 	 */
26087c478bd9Sstevel@tonic-gate 	tmp = ddi_prop_decode_alloc(nbytes, ddi_prop_free_bytes);
26097c478bd9Sstevel@tonic-gate 
26107c478bd9Sstevel@tonic-gate 	/*
26117c478bd9Sstevel@tonic-gate 	 * Decode each element and place it in the space we just allocated
26127c478bd9Sstevel@tonic-gate 	 */
26137c478bd9Sstevel@tonic-gate 	i = DDI_PROP_BYTES(ph, DDI_PROP_CMD_DECODE, tmp, nbytes);
26147c478bd9Sstevel@tonic-gate 	if (i < DDI_PROP_RESULT_OK) {
26157c478bd9Sstevel@tonic-gate 		/*
26167c478bd9Sstevel@tonic-gate 		 * Free the space we just allocated
26177c478bd9Sstevel@tonic-gate 		 * and return an error
26187c478bd9Sstevel@tonic-gate 		 */
26197c478bd9Sstevel@tonic-gate 		ddi_prop_free(tmp);
26207c478bd9Sstevel@tonic-gate 		switch (i) {
26217c478bd9Sstevel@tonic-gate 		case DDI_PROP_RESULT_EOF:
26227c478bd9Sstevel@tonic-gate 			return (DDI_PROP_END_OF_DATA);
26237c478bd9Sstevel@tonic-gate 
26247c478bd9Sstevel@tonic-gate 		case DDI_PROP_RESULT_ERROR:
26257c478bd9Sstevel@tonic-gate 			return (DDI_PROP_CANNOT_DECODE);
26267c478bd9Sstevel@tonic-gate 		}
26277c478bd9Sstevel@tonic-gate 	}
26287c478bd9Sstevel@tonic-gate 
26297c478bd9Sstevel@tonic-gate 	*(uchar_t **)data = tmp;
26307c478bd9Sstevel@tonic-gate 	*nelements = nbytes;
26317c478bd9Sstevel@tonic-gate 
26327c478bd9Sstevel@tonic-gate 	return (DDI_PROP_SUCCESS);
26337c478bd9Sstevel@tonic-gate }
26347c478bd9Sstevel@tonic-gate 
26357c478bd9Sstevel@tonic-gate /*
26367c478bd9Sstevel@tonic-gate  * Encode an array of bytes.
26377c478bd9Sstevel@tonic-gate  */
26387c478bd9Sstevel@tonic-gate int
26397c478bd9Sstevel@tonic-gate ddi_prop_fm_encode_bytes(prop_handle_t *ph, void *data, uint_t nelements)
26407c478bd9Sstevel@tonic-gate {
26417c478bd9Sstevel@tonic-gate 	int		size;
26427c478bd9Sstevel@tonic-gate 	int		i;
26437c478bd9Sstevel@tonic-gate 
26447c478bd9Sstevel@tonic-gate 	/*
26457c478bd9Sstevel@tonic-gate 	 * If there are no elements, then this is a boolean property,
26467c478bd9Sstevel@tonic-gate 	 * so just create a property handle with no data and return.
26477c478bd9Sstevel@tonic-gate 	 */
26487c478bd9Sstevel@tonic-gate 	if (nelements == 0) {
26497c478bd9Sstevel@tonic-gate 		(void) ddi_prop_encode_alloc(ph, 0);
26507c478bd9Sstevel@tonic-gate 		return (DDI_PROP_SUCCESS);
26517c478bd9Sstevel@tonic-gate 	}
26527c478bd9Sstevel@tonic-gate 
26537c478bd9Sstevel@tonic-gate 	/*
26547c478bd9Sstevel@tonic-gate 	 * Get the size of the encoded array of bytes.
26557c478bd9Sstevel@tonic-gate 	 */
26567c478bd9Sstevel@tonic-gate 	size = DDI_PROP_BYTES(ph, DDI_PROP_CMD_GET_ESIZE, (uchar_t *)data,
26577c478bd9Sstevel@tonic-gate 	    nelements);
26587c478bd9Sstevel@tonic-gate 	if (size < DDI_PROP_RESULT_OK) {
26597c478bd9Sstevel@tonic-gate 		switch (size) {
26607c478bd9Sstevel@tonic-gate 		case DDI_PROP_RESULT_EOF:
26617c478bd9Sstevel@tonic-gate 			return (DDI_PROP_END_OF_DATA);
26627c478bd9Sstevel@tonic-gate 
26637c478bd9Sstevel@tonic-gate 		case DDI_PROP_RESULT_ERROR:
26647c478bd9Sstevel@tonic-gate 			return (DDI_PROP_CANNOT_DECODE);
26657c478bd9Sstevel@tonic-gate 		}
26667c478bd9Sstevel@tonic-gate 	}
26677c478bd9Sstevel@tonic-gate 
26687c478bd9Sstevel@tonic-gate 	/*
26697c478bd9Sstevel@tonic-gate 	 * Allocate space in the handle to store the encoded bytes.
26707c478bd9Sstevel@tonic-gate 	 */
26717c478bd9Sstevel@tonic-gate 	if (ddi_prop_encode_alloc(ph, (uint_t)size) != DDI_PROP_SUCCESS)
26727c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NO_MEMORY);
26737c478bd9Sstevel@tonic-gate 
26747c478bd9Sstevel@tonic-gate 	/*
26757c478bd9Sstevel@tonic-gate 	 * Encode the array of bytes.
26767c478bd9Sstevel@tonic-gate 	 */
26777c478bd9Sstevel@tonic-gate 	i = DDI_PROP_BYTES(ph, DDI_PROP_CMD_ENCODE, (uchar_t *)data,
26787c478bd9Sstevel@tonic-gate 	    nelements);
26797c478bd9Sstevel@tonic-gate 	if (i < DDI_PROP_RESULT_OK) {
26807c478bd9Sstevel@tonic-gate 		switch (i) {
26817c478bd9Sstevel@tonic-gate 		case DDI_PROP_RESULT_EOF:
26827c478bd9Sstevel@tonic-gate 			return (DDI_PROP_END_OF_DATA);
26837c478bd9Sstevel@tonic-gate 
26847c478bd9Sstevel@tonic-gate 		case DDI_PROP_RESULT_ERROR:
26857c478bd9Sstevel@tonic-gate 			return (DDI_PROP_CANNOT_ENCODE);
26867c478bd9Sstevel@tonic-gate 		}
26877c478bd9Sstevel@tonic-gate 	}
26887c478bd9Sstevel@tonic-gate 
26897c478bd9Sstevel@tonic-gate 	return (DDI_PROP_SUCCESS);
26907c478bd9Sstevel@tonic-gate }
26917c478bd9Sstevel@tonic-gate 
26927c478bd9Sstevel@tonic-gate /*
26937c478bd9Sstevel@tonic-gate  * OBP 1275 integer, string and byte operators.
26947c478bd9Sstevel@tonic-gate  *
26957c478bd9Sstevel@tonic-gate  * DDI_PROP_CMD_DECODE:
26967c478bd9Sstevel@tonic-gate  *
26977c478bd9Sstevel@tonic-gate  *	DDI_PROP_RESULT_ERROR:		cannot decode the data
26987c478bd9Sstevel@tonic-gate  *	DDI_PROP_RESULT_EOF:		end of data
26997c478bd9Sstevel@tonic-gate  *	DDI_PROP_OK:			data was decoded
27007c478bd9Sstevel@tonic-gate  *
27017c478bd9Sstevel@tonic-gate  * DDI_PROP_CMD_ENCODE:
27027c478bd9Sstevel@tonic-gate  *
27037c478bd9Sstevel@tonic-gate  *	DDI_PROP_RESULT_ERROR:		cannot encode the data
27047c478bd9Sstevel@tonic-gate  *	DDI_PROP_RESULT_EOF:		end of data
27057c478bd9Sstevel@tonic-gate  *	DDI_PROP_OK:			data was encoded
27067c478bd9Sstevel@tonic-gate  *
27077c478bd9Sstevel@tonic-gate  * DDI_PROP_CMD_SKIP:
27087c478bd9Sstevel@tonic-gate  *
27097c478bd9Sstevel@tonic-gate  *	DDI_PROP_RESULT_ERROR:		cannot skip the data
27107c478bd9Sstevel@tonic-gate  *	DDI_PROP_RESULT_EOF:		end of data
27117c478bd9Sstevel@tonic-gate  *	DDI_PROP_OK:			data was skipped
27127c478bd9Sstevel@tonic-gate  *
27137c478bd9Sstevel@tonic-gate  * DDI_PROP_CMD_GET_ESIZE:
27147c478bd9Sstevel@tonic-gate  *
27157c478bd9Sstevel@tonic-gate  *	DDI_PROP_RESULT_ERROR:		cannot get encoded size
27167c478bd9Sstevel@tonic-gate  *	DDI_PROP_RESULT_EOF:		end of data
27177c478bd9Sstevel@tonic-gate  *	> 0:				the encoded size
27187c478bd9Sstevel@tonic-gate  *
27197c478bd9Sstevel@tonic-gate  * DDI_PROP_CMD_GET_DSIZE:
27207c478bd9Sstevel@tonic-gate  *
27217c478bd9Sstevel@tonic-gate  *	DDI_PROP_RESULT_ERROR:		cannot get decoded size
27227c478bd9Sstevel@tonic-gate  *	DDI_PROP_RESULT_EOF:		end of data
27237c478bd9Sstevel@tonic-gate  *	> 0:				the decoded size
27247c478bd9Sstevel@tonic-gate  */
27257c478bd9Sstevel@tonic-gate 
27267c478bd9Sstevel@tonic-gate /*
27277c478bd9Sstevel@tonic-gate  * OBP 1275 integer operator
27287c478bd9Sstevel@tonic-gate  *
27297c478bd9Sstevel@tonic-gate  * OBP properties are a byte stream of data, so integers may not be
27307c478bd9Sstevel@tonic-gate  * properly aligned.  Therefore we need to copy them one byte at a time.
27317c478bd9Sstevel@tonic-gate  */
27327c478bd9Sstevel@tonic-gate int
27337c478bd9Sstevel@tonic-gate ddi_prop_1275_int(prop_handle_t *ph, uint_t cmd, int *data)
27347c478bd9Sstevel@tonic-gate {
27357c478bd9Sstevel@tonic-gate 	int	i;
27367c478bd9Sstevel@tonic-gate 
27377c478bd9Sstevel@tonic-gate 	switch (cmd) {
27387c478bd9Sstevel@tonic-gate 	case DDI_PROP_CMD_DECODE:
27397c478bd9Sstevel@tonic-gate 		/*
27407c478bd9Sstevel@tonic-gate 		 * Check that there is encoded data
27417c478bd9Sstevel@tonic-gate 		 */
27427c478bd9Sstevel@tonic-gate 		if (ph->ph_cur_pos == NULL || ph->ph_size == 0)
27437c478bd9Sstevel@tonic-gate 			return (DDI_PROP_RESULT_ERROR);
27447c478bd9Sstevel@tonic-gate 		if (ph->ph_flags & PH_FROM_PROM) {
27457c478bd9Sstevel@tonic-gate 			i = MIN(ph->ph_size, PROP_1275_INT_SIZE);
27467c478bd9Sstevel@tonic-gate 			if ((int *)ph->ph_cur_pos > ((int *)ph->ph_data +
27477c478bd9Sstevel@tonic-gate 			    ph->ph_size - i))
27487c478bd9Sstevel@tonic-gate 				return (DDI_PROP_RESULT_ERROR);
27497c478bd9Sstevel@tonic-gate 		} else {
27507c478bd9Sstevel@tonic-gate 			if (ph->ph_size < sizeof (int) ||
27517c478bd9Sstevel@tonic-gate 			    ((int *)ph->ph_cur_pos > ((int *)ph->ph_data +
27527c478bd9Sstevel@tonic-gate 			    ph->ph_size - sizeof (int))))
27537c478bd9Sstevel@tonic-gate 				return (DDI_PROP_RESULT_ERROR);
27547c478bd9Sstevel@tonic-gate 		}
27557c478bd9Sstevel@tonic-gate 
27567c478bd9Sstevel@tonic-gate 		/*
27577c478bd9Sstevel@tonic-gate 		 * Copy the integer, using the implementation-specific
27587c478bd9Sstevel@tonic-gate 		 * copy function if the property is coming from the PROM.
27597c478bd9Sstevel@tonic-gate 		 */
27607c478bd9Sstevel@tonic-gate 		if (ph->ph_flags & PH_FROM_PROM) {
27617c478bd9Sstevel@tonic-gate 			*data = impl_ddi_prop_int_from_prom(
27627c478bd9Sstevel@tonic-gate 			    (uchar_t *)ph->ph_cur_pos,
27637c478bd9Sstevel@tonic-gate 			    (ph->ph_size < PROP_1275_INT_SIZE) ?
27647c478bd9Sstevel@tonic-gate 			    ph->ph_size : PROP_1275_INT_SIZE);
27657c478bd9Sstevel@tonic-gate 		} else {
27667c478bd9Sstevel@tonic-gate 			bcopy(ph->ph_cur_pos, data, sizeof (int));
27677c478bd9Sstevel@tonic-gate 		}
27687c478bd9Sstevel@tonic-gate 
27697c478bd9Sstevel@tonic-gate 		/*
27707c478bd9Sstevel@tonic-gate 		 * Move the current location to the start of the next
27717c478bd9Sstevel@tonic-gate 		 * bit of undecoded data.
27727c478bd9Sstevel@tonic-gate 		 */
27737c478bd9Sstevel@tonic-gate 		ph->ph_cur_pos = (uchar_t *)ph->ph_cur_pos +
27747c478bd9Sstevel@tonic-gate 		    PROP_1275_INT_SIZE;
27757c478bd9Sstevel@tonic-gate 		return (DDI_PROP_RESULT_OK);
27767c478bd9Sstevel@tonic-gate 
27777c478bd9Sstevel@tonic-gate 	case DDI_PROP_CMD_ENCODE:
27787c478bd9Sstevel@tonic-gate 		/*
27797c478bd9Sstevel@tonic-gate 		 * Check that there is room to encoded the data
27807c478bd9Sstevel@tonic-gate 		 */
27817c478bd9Sstevel@tonic-gate 		if (ph->ph_cur_pos == NULL || ph->ph_size == 0 ||
27827c478bd9Sstevel@tonic-gate 		    ph->ph_size < PROP_1275_INT_SIZE ||
27837c478bd9Sstevel@tonic-gate 		    ((int *)ph->ph_cur_pos > ((int *)ph->ph_data +
27847c478bd9Sstevel@tonic-gate 		    ph->ph_size - sizeof (int))))
27857c478bd9Sstevel@tonic-gate 			return (DDI_PROP_RESULT_ERROR);
27867c478bd9Sstevel@tonic-gate 
27877c478bd9Sstevel@tonic-gate 		/*
27887c478bd9Sstevel@tonic-gate 		 * Encode the integer into the byte stream one byte at a
27897c478bd9Sstevel@tonic-gate 		 * time.
27907c478bd9Sstevel@tonic-gate 		 */
27917c478bd9Sstevel@tonic-gate 		bcopy(data, ph->ph_cur_pos, sizeof (int));
27927c478bd9Sstevel@tonic-gate 
27937c478bd9Sstevel@tonic-gate 		/*
27947c478bd9Sstevel@tonic-gate 		 * Move the current location to the start of the next bit of
27957c478bd9Sstevel@tonic-gate 		 * space where we can store encoded data.
27967c478bd9Sstevel@tonic-gate 		 */
27977c478bd9Sstevel@tonic-gate 		ph->ph_cur_pos = (uchar_t *)ph->ph_cur_pos + PROP_1275_INT_SIZE;
27987c478bd9Sstevel@tonic-gate 		return (DDI_PROP_RESULT_OK);
27997c478bd9Sstevel@tonic-gate 
28007c478bd9Sstevel@tonic-gate 	case DDI_PROP_CMD_SKIP:
28017c478bd9Sstevel@tonic-gate 		/*
28027c478bd9Sstevel@tonic-gate 		 * Check that there is encoded data
28037c478bd9Sstevel@tonic-gate 		 */
28047c478bd9Sstevel@tonic-gate 		if (ph->ph_cur_pos == NULL || ph->ph_size == 0 ||
28057c478bd9Sstevel@tonic-gate 		    ph->ph_size < PROP_1275_INT_SIZE)
28067c478bd9Sstevel@tonic-gate 			return (DDI_PROP_RESULT_ERROR);
28077c478bd9Sstevel@tonic-gate 
28087c478bd9Sstevel@tonic-gate 
28097c478bd9Sstevel@tonic-gate 		if ((caddr_t)ph->ph_cur_pos ==
28107c478bd9Sstevel@tonic-gate 		    (caddr_t)ph->ph_data + ph->ph_size) {
28117c478bd9Sstevel@tonic-gate 			return (DDI_PROP_RESULT_EOF);
28127c478bd9Sstevel@tonic-gate 		} else if ((caddr_t)ph->ph_cur_pos >
28137c478bd9Sstevel@tonic-gate 		    (caddr_t)ph->ph_data + ph->ph_size) {
28147c478bd9Sstevel@tonic-gate 			return (DDI_PROP_RESULT_EOF);
28157c478bd9Sstevel@tonic-gate 		}
28167c478bd9Sstevel@tonic-gate 
28177c478bd9Sstevel@tonic-gate 		/*
28187c478bd9Sstevel@tonic-gate 		 * Move the current location to the start of the next bit of
28197c478bd9Sstevel@tonic-gate 		 * undecoded data.
28207c478bd9Sstevel@tonic-gate 		 */
28217c478bd9Sstevel@tonic-gate 		ph->ph_cur_pos = (uchar_t *)ph->ph_cur_pos + PROP_1275_INT_SIZE;
28227c478bd9Sstevel@tonic-gate 		return (DDI_PROP_RESULT_OK);
28237c478bd9Sstevel@tonic-gate 
28247c478bd9Sstevel@tonic-gate 	case DDI_PROP_CMD_GET_ESIZE:
28257c478bd9Sstevel@tonic-gate 		/*
28267c478bd9Sstevel@tonic-gate 		 * Return the size of an encoded integer on OBP
28277c478bd9Sstevel@tonic-gate 		 */
28287c478bd9Sstevel@tonic-gate 		return (PROP_1275_INT_SIZE);
28297c478bd9Sstevel@tonic-gate 
28307c478bd9Sstevel@tonic-gate 	case DDI_PROP_CMD_GET_DSIZE:
28317c478bd9Sstevel@tonic-gate 		/*
28327c478bd9Sstevel@tonic-gate 		 * Return the size of a decoded integer on the system.
28337c478bd9Sstevel@tonic-gate 		 */
28347c478bd9Sstevel@tonic-gate 		return (sizeof (int));
28357c478bd9Sstevel@tonic-gate 
28367c478bd9Sstevel@tonic-gate 	default:
28377c478bd9Sstevel@tonic-gate #ifdef DEBUG
28387c478bd9Sstevel@tonic-gate 		panic("ddi_prop_1275_int: %x impossible", cmd);
28397c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
28407c478bd9Sstevel@tonic-gate #else
28417c478bd9Sstevel@tonic-gate 		return (DDI_PROP_RESULT_ERROR);
28427c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
28437c478bd9Sstevel@tonic-gate 	}
28447c478bd9Sstevel@tonic-gate }
28457c478bd9Sstevel@tonic-gate 
28467c478bd9Sstevel@tonic-gate /*
28477c478bd9Sstevel@tonic-gate  * 64 bit integer operator.
28487c478bd9Sstevel@tonic-gate  *
28497c478bd9Sstevel@tonic-gate  * This is an extension, defined by Sun, to the 1275 integer
28507c478bd9Sstevel@tonic-gate  * operator.  This routine handles the encoding/decoding of
28517c478bd9Sstevel@tonic-gate  * 64 bit integer properties.
28527c478bd9Sstevel@tonic-gate  */
28537c478bd9Sstevel@tonic-gate int
28547c478bd9Sstevel@tonic-gate ddi_prop_int64_op(prop_handle_t *ph, uint_t cmd, int64_t *data)
28557c478bd9Sstevel@tonic-gate {
28567c478bd9Sstevel@tonic-gate 
28577c478bd9Sstevel@tonic-gate 	switch (cmd) {
28587c478bd9Sstevel@tonic-gate 	case DDI_PROP_CMD_DECODE:
28597c478bd9Sstevel@tonic-gate 		/*
28607c478bd9Sstevel@tonic-gate 		 * Check that there is encoded data
28617c478bd9Sstevel@tonic-gate 		 */
28627c478bd9Sstevel@tonic-gate 		if (ph->ph_cur_pos == NULL || ph->ph_size == 0)
28637c478bd9Sstevel@tonic-gate 			return (DDI_PROP_RESULT_ERROR);
28647c478bd9Sstevel@tonic-gate 		if (ph->ph_flags & PH_FROM_PROM) {
28657c478bd9Sstevel@tonic-gate 			return (DDI_PROP_RESULT_ERROR);
28667c478bd9Sstevel@tonic-gate 		} else {
28677c478bd9Sstevel@tonic-gate 			if (ph->ph_size < sizeof (int64_t) ||
28687c478bd9Sstevel@tonic-gate 			    ((int64_t *)ph->ph_cur_pos >
28697c478bd9Sstevel@tonic-gate 			    ((int64_t *)ph->ph_data +
28707c478bd9Sstevel@tonic-gate 			    ph->ph_size - sizeof (int64_t))))
28717c478bd9Sstevel@tonic-gate 				return (DDI_PROP_RESULT_ERROR);
28727c478bd9Sstevel@tonic-gate 		}
28737c478bd9Sstevel@tonic-gate 		/*
28747c478bd9Sstevel@tonic-gate 		 * Copy the integer, using the implementation-specific
28757c478bd9Sstevel@tonic-gate 		 * copy function if the property is coming from the PROM.
28767c478bd9Sstevel@tonic-gate 		 */
28777c478bd9Sstevel@tonic-gate 		if (ph->ph_flags & PH_FROM_PROM) {
28787c478bd9Sstevel@tonic-gate 			return (DDI_PROP_RESULT_ERROR);
28797c478bd9Sstevel@tonic-gate 		} else {
28807c478bd9Sstevel@tonic-gate 			bcopy(ph->ph_cur_pos, data, sizeof (int64_t));
28817c478bd9Sstevel@tonic-gate 		}
28827c478bd9Sstevel@tonic-gate 
28837c478bd9Sstevel@tonic-gate 		/*
28847c478bd9Sstevel@tonic-gate 		 * Move the current location to the start of the next
28857c478bd9Sstevel@tonic-gate 		 * bit of undecoded data.
28867c478bd9Sstevel@tonic-gate 		 */
28877c478bd9Sstevel@tonic-gate 		ph->ph_cur_pos = (uchar_t *)ph->ph_cur_pos +
28887c478bd9Sstevel@tonic-gate 		    sizeof (int64_t);
28897c478bd9Sstevel@tonic-gate 			return (DDI_PROP_RESULT_OK);
28907c478bd9Sstevel@tonic-gate 
28917c478bd9Sstevel@tonic-gate 	case DDI_PROP_CMD_ENCODE:
28927c478bd9Sstevel@tonic-gate 		/*
28937c478bd9Sstevel@tonic-gate 		 * Check that there is room to encoded the data
28947c478bd9Sstevel@tonic-gate 		 */
28957c478bd9Sstevel@tonic-gate 		if (ph->ph_cur_pos == NULL || ph->ph_size == 0 ||
28967c478bd9Sstevel@tonic-gate 		    ph->ph_size < sizeof (int64_t) ||
28977c478bd9Sstevel@tonic-gate 		    ((int64_t *)ph->ph_cur_pos > ((int64_t *)ph->ph_data +
28987c478bd9Sstevel@tonic-gate 		    ph->ph_size - sizeof (int64_t))))
28997c478bd9Sstevel@tonic-gate 			return (DDI_PROP_RESULT_ERROR);
29007c478bd9Sstevel@tonic-gate 
29017c478bd9Sstevel@tonic-gate 		/*
29027c478bd9Sstevel@tonic-gate 		 * Encode the integer into the byte stream one byte at a
29037c478bd9Sstevel@tonic-gate 		 * time.
29047c478bd9Sstevel@tonic-gate 		 */
29057c478bd9Sstevel@tonic-gate 		bcopy(data, ph->ph_cur_pos, sizeof (int64_t));
29067c478bd9Sstevel@tonic-gate 
29077c478bd9Sstevel@tonic-gate 		/*
29087c478bd9Sstevel@tonic-gate 		 * Move the current location to the start of the next bit of
29097c478bd9Sstevel@tonic-gate 		 * space where we can store encoded data.
29107c478bd9Sstevel@tonic-gate 		 */
29117c478bd9Sstevel@tonic-gate 		ph->ph_cur_pos = (uchar_t *)ph->ph_cur_pos +
29127c478bd9Sstevel@tonic-gate 		    sizeof (int64_t);
29137c478bd9Sstevel@tonic-gate 		return (DDI_PROP_RESULT_OK);
29147c478bd9Sstevel@tonic-gate 
29157c478bd9Sstevel@tonic-gate 	case DDI_PROP_CMD_SKIP:
29167c478bd9Sstevel@tonic-gate 		/*
29177c478bd9Sstevel@tonic-gate 		 * Check that there is encoded data
29187c478bd9Sstevel@tonic-gate 		 */
29197c478bd9Sstevel@tonic-gate 		if (ph->ph_cur_pos == NULL || ph->ph_size == 0 ||
29207c478bd9Sstevel@tonic-gate 		    ph->ph_size < sizeof (int64_t))
29217c478bd9Sstevel@tonic-gate 			return (DDI_PROP_RESULT_ERROR);
29227c478bd9Sstevel@tonic-gate 
29237c478bd9Sstevel@tonic-gate 		if ((caddr_t)ph->ph_cur_pos ==
29247c478bd9Sstevel@tonic-gate 		    (caddr_t)ph->ph_data + ph->ph_size) {
29257c478bd9Sstevel@tonic-gate 			return (DDI_PROP_RESULT_EOF);
29267c478bd9Sstevel@tonic-gate 		} else if ((caddr_t)ph->ph_cur_pos >
29277c478bd9Sstevel@tonic-gate 		    (caddr_t)ph->ph_data + ph->ph_size) {
29287c478bd9Sstevel@tonic-gate 			return (DDI_PROP_RESULT_EOF);
29297c478bd9Sstevel@tonic-gate 		}
29307c478bd9Sstevel@tonic-gate 
29317c478bd9Sstevel@tonic-gate 		/*
29327c478bd9Sstevel@tonic-gate 		 * Move the current location to the start of
29337c478bd9Sstevel@tonic-gate 		 * the next bit of undecoded data.
29347c478bd9Sstevel@tonic-gate 		 */
29357c478bd9Sstevel@tonic-gate 		ph->ph_cur_pos = (uchar_t *)ph->ph_cur_pos +
29367c478bd9Sstevel@tonic-gate 		    sizeof (int64_t);
29377c478bd9Sstevel@tonic-gate 			return (DDI_PROP_RESULT_OK);
29387c478bd9Sstevel@tonic-gate 
29397c478bd9Sstevel@tonic-gate 	case DDI_PROP_CMD_GET_ESIZE:
29407c478bd9Sstevel@tonic-gate 		/*
29417c478bd9Sstevel@tonic-gate 		 * Return the size of an encoded integer on OBP
29427c478bd9Sstevel@tonic-gate 		 */
29437c478bd9Sstevel@tonic-gate 		return (sizeof (int64_t));
29447c478bd9Sstevel@tonic-gate 
29457c478bd9Sstevel@tonic-gate 	case DDI_PROP_CMD_GET_DSIZE:
29467c478bd9Sstevel@tonic-gate 		/*
29477c478bd9Sstevel@tonic-gate 		 * Return the size of a decoded integer on the system.
29487c478bd9Sstevel@tonic-gate 		 */
29497c478bd9Sstevel@tonic-gate 		return (sizeof (int64_t));
29507c478bd9Sstevel@tonic-gate 
29517c478bd9Sstevel@tonic-gate 	default:
29527c478bd9Sstevel@tonic-gate #ifdef DEBUG
29537c478bd9Sstevel@tonic-gate 		panic("ddi_prop_int64_op: %x impossible", cmd);
29547c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
29557c478bd9Sstevel@tonic-gate #else
29567c478bd9Sstevel@tonic-gate 		return (DDI_PROP_RESULT_ERROR);
29577c478bd9Sstevel@tonic-gate #endif  /* DEBUG */
29587c478bd9Sstevel@tonic-gate 	}
29597c478bd9Sstevel@tonic-gate }
29607c478bd9Sstevel@tonic-gate 
29617c478bd9Sstevel@tonic-gate /*
29627c478bd9Sstevel@tonic-gate  * OBP 1275 string operator.
29637c478bd9Sstevel@tonic-gate  *
29647c478bd9Sstevel@tonic-gate  * OBP strings are NULL terminated.
29657c478bd9Sstevel@tonic-gate  */
29667c478bd9Sstevel@tonic-gate int
29677c478bd9Sstevel@tonic-gate ddi_prop_1275_string(prop_handle_t *ph, uint_t cmd, char *data)
29687c478bd9Sstevel@tonic-gate {
29697c478bd9Sstevel@tonic-gate 	int	n;
29707c478bd9Sstevel@tonic-gate 	char	*p;
29717c478bd9Sstevel@tonic-gate 	char	*end;
29727c478bd9Sstevel@tonic-gate 
29737c478bd9Sstevel@tonic-gate 	switch (cmd) {
29747c478bd9Sstevel@tonic-gate 	case DDI_PROP_CMD_DECODE:
29757c478bd9Sstevel@tonic-gate 		/*
29767c478bd9Sstevel@tonic-gate 		 * Check that there is encoded data
29777c478bd9Sstevel@tonic-gate 		 */
29787c478bd9Sstevel@tonic-gate 		if (ph->ph_cur_pos == NULL || ph->ph_size == 0) {
29797c478bd9Sstevel@tonic-gate 			return (DDI_PROP_RESULT_ERROR);
29807c478bd9Sstevel@tonic-gate 		}
29817c478bd9Sstevel@tonic-gate 
29829e1bcca2Scth 		/*
29839e1bcca2Scth 		 * Match DDI_PROP_CMD_GET_DSIZE logic for when to stop and
29849e1bcca2Scth 		 * how to NULL terminate result.
29859e1bcca2Scth 		 */
29869e1bcca2Scth 		p = (char *)ph->ph_cur_pos;
29879e1bcca2Scth 		end = (char *)ph->ph_data + ph->ph_size;
29889e1bcca2Scth 		if (p >= end)
29899e1bcca2Scth 			return (DDI_PROP_RESULT_EOF);
29909e1bcca2Scth 
29919e1bcca2Scth 		while (p < end) {
29929e1bcca2Scth 			*data++ = *p;
29939e1bcca2Scth 			if (*p++ == 0) {	/* NULL from OBP */
29949e1bcca2Scth 				ph->ph_cur_pos = p;
29959e1bcca2Scth 				return (DDI_PROP_RESULT_OK);
29969e1bcca2Scth 			}
29977c478bd9Sstevel@tonic-gate 		}
29987c478bd9Sstevel@tonic-gate 
29997c478bd9Sstevel@tonic-gate 		/*
30009e1bcca2Scth 		 * If OBP did not NULL terminate string, which happens
30019e1bcca2Scth 		 * (at least) for 'true'/'false' boolean values, account for
30029e1bcca2Scth 		 * the space and store null termination on decode.
30037c478bd9Sstevel@tonic-gate 		 */
30049e1bcca2Scth 		ph->ph_cur_pos = p;
30059e1bcca2Scth 		*data = 0;
30067c478bd9Sstevel@tonic-gate 		return (DDI_PROP_RESULT_OK);
30077c478bd9Sstevel@tonic-gate 
30087c478bd9Sstevel@tonic-gate 	case DDI_PROP_CMD_ENCODE:
30097c478bd9Sstevel@tonic-gate 		/*
30107c478bd9Sstevel@tonic-gate 		 * Check that there is room to encoded the data
30117c478bd9Sstevel@tonic-gate 		 */
30127c478bd9Sstevel@tonic-gate 		if (ph->ph_cur_pos == NULL || ph->ph_size == 0) {
30137c478bd9Sstevel@tonic-gate 			return (DDI_PROP_RESULT_ERROR);
30147c478bd9Sstevel@tonic-gate 		}
30157c478bd9Sstevel@tonic-gate 
30167c478bd9Sstevel@tonic-gate 		n = strlen(data) + 1;
30177c478bd9Sstevel@tonic-gate 		if ((char *)ph->ph_cur_pos > ((char *)ph->ph_data +
30187c478bd9Sstevel@tonic-gate 		    ph->ph_size - n)) {
30197c478bd9Sstevel@tonic-gate 			return (DDI_PROP_RESULT_ERROR);
30207c478bd9Sstevel@tonic-gate 		}
30217c478bd9Sstevel@tonic-gate 
30227c478bd9Sstevel@tonic-gate 		/*
30237c478bd9Sstevel@tonic-gate 		 * Copy the NULL terminated string
30247c478bd9Sstevel@tonic-gate 		 */
30257c478bd9Sstevel@tonic-gate 		bcopy(data, ph->ph_cur_pos, n);
30267c478bd9Sstevel@tonic-gate 
30277c478bd9Sstevel@tonic-gate 		/*
30287c478bd9Sstevel@tonic-gate 		 * Move the current location to the start of the next bit of
30297c478bd9Sstevel@tonic-gate 		 * space where we can store encoded data.
30307c478bd9Sstevel@tonic-gate 		 */
30317c478bd9Sstevel@tonic-gate 		ph->ph_cur_pos = (char *)ph->ph_cur_pos + n;
30327c478bd9Sstevel@tonic-gate 		return (DDI_PROP_RESULT_OK);
30337c478bd9Sstevel@tonic-gate 
30347c478bd9Sstevel@tonic-gate 	case DDI_PROP_CMD_SKIP:
30357c478bd9Sstevel@tonic-gate 		/*
30367c478bd9Sstevel@tonic-gate 		 * Check that there is encoded data
30377c478bd9Sstevel@tonic-gate 		 */
30387c478bd9Sstevel@tonic-gate 		if (ph->ph_cur_pos == NULL || ph->ph_size == 0) {
30397c478bd9Sstevel@tonic-gate 			return (DDI_PROP_RESULT_ERROR);
30407c478bd9Sstevel@tonic-gate 		}
30417c478bd9Sstevel@tonic-gate 
30427c478bd9Sstevel@tonic-gate 		/*
30437c478bd9Sstevel@tonic-gate 		 * Return the string length plus one for the NULL
30447c478bd9Sstevel@tonic-gate 		 * We know the size of the property, we need to
30457c478bd9Sstevel@tonic-gate 		 * ensure that the string is properly formatted,
30467c478bd9Sstevel@tonic-gate 		 * since we may be looking up random OBP data.
30477c478bd9Sstevel@tonic-gate 		 */
30487c478bd9Sstevel@tonic-gate 		p = (char *)ph->ph_cur_pos;
30497c478bd9Sstevel@tonic-gate 		end = (char *)ph->ph_data + ph->ph_size;
30509e1bcca2Scth 		if (p >= end)
30517c478bd9Sstevel@tonic-gate 			return (DDI_PROP_RESULT_EOF);
30527c478bd9Sstevel@tonic-gate 
30539e1bcca2Scth 		while (p < end) {
30549e1bcca2Scth 			if (*p++ == 0) {	/* NULL from OBP */
30557c478bd9Sstevel@tonic-gate 				ph->ph_cur_pos = p;
30567c478bd9Sstevel@tonic-gate 				return (DDI_PROP_RESULT_OK);
30577c478bd9Sstevel@tonic-gate 			}
30587c478bd9Sstevel@tonic-gate 		}
30597c478bd9Sstevel@tonic-gate 
30609e1bcca2Scth 		/*
30619e1bcca2Scth 		 * Accommodate the fact that OBP does not always NULL
30629e1bcca2Scth 		 * terminate strings.
30639e1bcca2Scth 		 */
30649e1bcca2Scth 		ph->ph_cur_pos = p;
30659e1bcca2Scth 		return (DDI_PROP_RESULT_OK);
30667c478bd9Sstevel@tonic-gate 
30677c478bd9Sstevel@tonic-gate 	case DDI_PROP_CMD_GET_ESIZE:
30687c478bd9Sstevel@tonic-gate 		/*
30697c478bd9Sstevel@tonic-gate 		 * Return the size of the encoded string on OBP.
30707c478bd9Sstevel@tonic-gate 		 */
30717c478bd9Sstevel@tonic-gate 		return (strlen(data) + 1);
30727c478bd9Sstevel@tonic-gate 
30737c478bd9Sstevel@tonic-gate 	case DDI_PROP_CMD_GET_DSIZE:
30747c478bd9Sstevel@tonic-gate 		/*
30759e1bcca2Scth 		 * Return the string length plus one for the NULL.
30767c478bd9Sstevel@tonic-gate 		 * We know the size of the property, we need to
30777c478bd9Sstevel@tonic-gate 		 * ensure that the string is properly formatted,
30787c478bd9Sstevel@tonic-gate 		 * since we may be looking up random OBP data.
30797c478bd9Sstevel@tonic-gate 		 */
30807c478bd9Sstevel@tonic-gate 		p = (char *)ph->ph_cur_pos;
30817c478bd9Sstevel@tonic-gate 		end = (char *)ph->ph_data + ph->ph_size;
30829e1bcca2Scth 		if (p >= end)
30839e1bcca2Scth 			return (DDI_PROP_RESULT_EOF);
30849e1bcca2Scth 
30857c478bd9Sstevel@tonic-gate 		for (n = 0; p < end; n++) {
30869e1bcca2Scth 			if (*p++ == 0) {	/* NULL from OBP */
30877c478bd9Sstevel@tonic-gate 				ph->ph_cur_pos = p;
30887c478bd9Sstevel@tonic-gate 				return (n + 1);
30897c478bd9Sstevel@tonic-gate 			}
30907c478bd9Sstevel@tonic-gate 		}
30919e1bcca2Scth 
30929e1bcca2Scth 		/*
30939e1bcca2Scth 		 * If OBP did not NULL terminate string, which happens for
30949e1bcca2Scth 		 * 'true'/'false' boolean values, account for the space
30959e1bcca2Scth 		 * to store null termination here.
30969e1bcca2Scth 		 */
30979e1bcca2Scth 		ph->ph_cur_pos = p;
30989e1bcca2Scth 		return (n + 1);
30997c478bd9Sstevel@tonic-gate 
31007c478bd9Sstevel@tonic-gate 	default:
31017c478bd9Sstevel@tonic-gate #ifdef DEBUG
31027c478bd9Sstevel@tonic-gate 		panic("ddi_prop_1275_string: %x impossible", cmd);
31037c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
31047c478bd9Sstevel@tonic-gate #else
31057c478bd9Sstevel@tonic-gate 		return (DDI_PROP_RESULT_ERROR);
31067c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
31077c478bd9Sstevel@tonic-gate 	}
31087c478bd9Sstevel@tonic-gate }
31097c478bd9Sstevel@tonic-gate 
31107c478bd9Sstevel@tonic-gate /*
31117c478bd9Sstevel@tonic-gate  * OBP 1275 byte operator
31127c478bd9Sstevel@tonic-gate  *
31137c478bd9Sstevel@tonic-gate  * Caller must specify the number of bytes to get.  OBP encodes bytes
31147c478bd9Sstevel@tonic-gate  * as a byte so there is a 1-to-1 translation.
31157c478bd9Sstevel@tonic-gate  */
31167c478bd9Sstevel@tonic-gate int
31177c478bd9Sstevel@tonic-gate ddi_prop_1275_bytes(prop_handle_t *ph, uint_t cmd, uchar_t *data,
31187c478bd9Sstevel@tonic-gate 	uint_t nelements)
31197c478bd9Sstevel@tonic-gate {
31207c478bd9Sstevel@tonic-gate 	switch (cmd) {
31217c478bd9Sstevel@tonic-gate 	case DDI_PROP_CMD_DECODE:
31227c478bd9Sstevel@tonic-gate 		/*
31237c478bd9Sstevel@tonic-gate 		 * Check that there is encoded data
31247c478bd9Sstevel@tonic-gate 		 */
31257c478bd9Sstevel@tonic-gate 		if (ph->ph_cur_pos == NULL || ph->ph_size == 0 ||
31267c478bd9Sstevel@tonic-gate 		    ph->ph_size < nelements ||
31277c478bd9Sstevel@tonic-gate 		    ((char *)ph->ph_cur_pos > ((char *)ph->ph_data +
31287c478bd9Sstevel@tonic-gate 		    ph->ph_size - nelements)))
31297c478bd9Sstevel@tonic-gate 			return (DDI_PROP_RESULT_ERROR);
31307c478bd9Sstevel@tonic-gate 
31317c478bd9Sstevel@tonic-gate 		/*
31327c478bd9Sstevel@tonic-gate 		 * Copy out the bytes
31337c478bd9Sstevel@tonic-gate 		 */
31347c478bd9Sstevel@tonic-gate 		bcopy(ph->ph_cur_pos, data, nelements);
31357c478bd9Sstevel@tonic-gate 
31367c478bd9Sstevel@tonic-gate 		/*
31377c478bd9Sstevel@tonic-gate 		 * Move the current location
31387c478bd9Sstevel@tonic-gate 		 */
31397c478bd9Sstevel@tonic-gate 		ph->ph_cur_pos = (char *)ph->ph_cur_pos + nelements;
31407c478bd9Sstevel@tonic-gate 		return (DDI_PROP_RESULT_OK);
31417c478bd9Sstevel@tonic-gate 
31427c478bd9Sstevel@tonic-gate 	case DDI_PROP_CMD_ENCODE:
31437c478bd9Sstevel@tonic-gate 		/*
31447c478bd9Sstevel@tonic-gate 		 * Check that there is room to encode the data
31457c478bd9Sstevel@tonic-gate 		 */
31467c478bd9Sstevel@tonic-gate 		if (ph->ph_cur_pos == NULL || ph->ph_size == 0 ||
31477c478bd9Sstevel@tonic-gate 		    ph->ph_size < nelements ||
31487c478bd9Sstevel@tonic-gate 		    ((char *)ph->ph_cur_pos > ((char *)ph->ph_data +
31497c478bd9Sstevel@tonic-gate 		    ph->ph_size - nelements)))
31507c478bd9Sstevel@tonic-gate 			return (DDI_PROP_RESULT_ERROR);
31517c478bd9Sstevel@tonic-gate 
31527c478bd9Sstevel@tonic-gate 		/*
31537c478bd9Sstevel@tonic-gate 		 * Copy in the bytes
31547c478bd9Sstevel@tonic-gate 		 */
31557c478bd9Sstevel@tonic-gate 		bcopy(data, ph->ph_cur_pos, nelements);
31567c478bd9Sstevel@tonic-gate 
31577c478bd9Sstevel@tonic-gate 		/*
31587c478bd9Sstevel@tonic-gate 		 * Move the current location to the start of the next bit of
31597c478bd9Sstevel@tonic-gate 		 * space where we can store encoded data.
31607c478bd9Sstevel@tonic-gate 		 */
31617c478bd9Sstevel@tonic-gate 		ph->ph_cur_pos = (char *)ph->ph_cur_pos + nelements;
31627c478bd9Sstevel@tonic-gate 		return (DDI_PROP_RESULT_OK);
31637c478bd9Sstevel@tonic-gate 
31647c478bd9Sstevel@tonic-gate 	case DDI_PROP_CMD_SKIP:
31657c478bd9Sstevel@tonic-gate 		/*
31667c478bd9Sstevel@tonic-gate 		 * Check that there is encoded data
31677c478bd9Sstevel@tonic-gate 		 */
31687c478bd9Sstevel@tonic-gate 		if (ph->ph_cur_pos == NULL || ph->ph_size == 0 ||
31697c478bd9Sstevel@tonic-gate 		    ph->ph_size < nelements)
31707c478bd9Sstevel@tonic-gate 			return (DDI_PROP_RESULT_ERROR);
31717c478bd9Sstevel@tonic-gate 
31727c478bd9Sstevel@tonic-gate 		if ((char *)ph->ph_cur_pos > ((char *)ph->ph_data +
31737c478bd9Sstevel@tonic-gate 		    ph->ph_size - nelements))
31747c478bd9Sstevel@tonic-gate 			return (DDI_PROP_RESULT_EOF);
31757c478bd9Sstevel@tonic-gate 
31767c478bd9Sstevel@tonic-gate 		/*
31777c478bd9Sstevel@tonic-gate 		 * Move the current location
31787c478bd9Sstevel@tonic-gate 		 */
31797c478bd9Sstevel@tonic-gate 		ph->ph_cur_pos = (char *)ph->ph_cur_pos + nelements;
31807c478bd9Sstevel@tonic-gate 		return (DDI_PROP_RESULT_OK);
31817c478bd9Sstevel@tonic-gate 
31827c478bd9Sstevel@tonic-gate 	case DDI_PROP_CMD_GET_ESIZE:
31837c478bd9Sstevel@tonic-gate 		/*
31847c478bd9Sstevel@tonic-gate 		 * The size in bytes of the encoded size is the
31857c478bd9Sstevel@tonic-gate 		 * same as the decoded size provided by the caller.
31867c478bd9Sstevel@tonic-gate 		 */
31877c478bd9Sstevel@tonic-gate 		return (nelements);
31887c478bd9Sstevel@tonic-gate 
31897c478bd9Sstevel@tonic-gate 	case DDI_PROP_CMD_GET_DSIZE:
31907c478bd9Sstevel@tonic-gate 		/*
31917c478bd9Sstevel@tonic-gate 		 * Just return the number of bytes specified by the caller.
31927c478bd9Sstevel@tonic-gate 		 */
31937c478bd9Sstevel@tonic-gate 		return (nelements);
31947c478bd9Sstevel@tonic-gate 
31957c478bd9Sstevel@tonic-gate 	default:
31967c478bd9Sstevel@tonic-gate #ifdef DEBUG
31977c478bd9Sstevel@tonic-gate 		panic("ddi_prop_1275_bytes: %x impossible", cmd);
31987c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
31997c478bd9Sstevel@tonic-gate #else
32007c478bd9Sstevel@tonic-gate 		return (DDI_PROP_RESULT_ERROR);
32017c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
32027c478bd9Sstevel@tonic-gate 	}
32037c478bd9Sstevel@tonic-gate }
32047c478bd9Sstevel@tonic-gate 
32057c478bd9Sstevel@tonic-gate /*
32067c478bd9Sstevel@tonic-gate  * Used for properties that come from the OBP, hardware configuration files,
32077c478bd9Sstevel@tonic-gate  * or that are created by calls to ddi_prop_update(9F).
32087c478bd9Sstevel@tonic-gate  */
32097c478bd9Sstevel@tonic-gate static struct prop_handle_ops prop_1275_ops = {
32107c478bd9Sstevel@tonic-gate 	ddi_prop_1275_int,
32117c478bd9Sstevel@tonic-gate 	ddi_prop_1275_string,
32127c478bd9Sstevel@tonic-gate 	ddi_prop_1275_bytes,
32137c478bd9Sstevel@tonic-gate 	ddi_prop_int64_op
32147c478bd9Sstevel@tonic-gate };
32157c478bd9Sstevel@tonic-gate 
32167c478bd9Sstevel@tonic-gate 
32177c478bd9Sstevel@tonic-gate /*
32187c478bd9Sstevel@tonic-gate  * Interface to create/modify a managed property on child's behalf...
32197c478bd9Sstevel@tonic-gate  * Flags interpreted are:
32207c478bd9Sstevel@tonic-gate  *	DDI_PROP_CANSLEEP:	Allow memory allocation to sleep.
32217c478bd9Sstevel@tonic-gate  *	DDI_PROP_SYSTEM_DEF:	Manipulate system list rather than driver list.
32227c478bd9Sstevel@tonic-gate  *
32237c478bd9Sstevel@tonic-gate  * Use same dev_t when modifying or undefining a property.
32247c478bd9Sstevel@tonic-gate  * Search for properties with DDI_DEV_T_ANY to match first named
32257c478bd9Sstevel@tonic-gate  * property on the list.
32267c478bd9Sstevel@tonic-gate  *
32277c478bd9Sstevel@tonic-gate  * Properties are stored LIFO and subsequently will match the first
32287c478bd9Sstevel@tonic-gate  * `matching' instance.
32297c478bd9Sstevel@tonic-gate  */
32307c478bd9Sstevel@tonic-gate 
32317c478bd9Sstevel@tonic-gate /*
32327c478bd9Sstevel@tonic-gate  * ddi_prop_add:	Add a software defined property
32337c478bd9Sstevel@tonic-gate  */
32347c478bd9Sstevel@tonic-gate 
32357c478bd9Sstevel@tonic-gate /*
32367c478bd9Sstevel@tonic-gate  * define to get a new ddi_prop_t.
32377c478bd9Sstevel@tonic-gate  * km_flags are KM_SLEEP or KM_NOSLEEP.
32387c478bd9Sstevel@tonic-gate  */
32397c478bd9Sstevel@tonic-gate 
32407c478bd9Sstevel@tonic-gate #define	DDI_NEW_PROP_T(km_flags)	\
32417c478bd9Sstevel@tonic-gate 	(kmem_zalloc(sizeof (ddi_prop_t), km_flags))
32427c478bd9Sstevel@tonic-gate 
32437c478bd9Sstevel@tonic-gate static int
32447c478bd9Sstevel@tonic-gate ddi_prop_add(dev_t dev, dev_info_t *dip, int flags,
32457c478bd9Sstevel@tonic-gate     char *name, caddr_t value, int length)
32467c478bd9Sstevel@tonic-gate {
32477c478bd9Sstevel@tonic-gate 	ddi_prop_t	*new_propp, *propp;
32487c478bd9Sstevel@tonic-gate 	ddi_prop_t	**list_head = &(DEVI(dip)->devi_drv_prop_ptr);
32497c478bd9Sstevel@tonic-gate 	int		km_flags = KM_NOSLEEP;
32507c478bd9Sstevel@tonic-gate 	int		name_buf_len;
32517c478bd9Sstevel@tonic-gate 
32527c478bd9Sstevel@tonic-gate 	/*
32537c478bd9Sstevel@tonic-gate 	 * If dev_t is DDI_DEV_T_ANY or name's length is zero return error.
32547c478bd9Sstevel@tonic-gate 	 */
32557c478bd9Sstevel@tonic-gate 
32567c478bd9Sstevel@tonic-gate 	if (dev == DDI_DEV_T_ANY || name == (char *)0 || strlen(name) == 0)
32577c478bd9Sstevel@tonic-gate 		return (DDI_PROP_INVAL_ARG);
32587c478bd9Sstevel@tonic-gate 
32597c478bd9Sstevel@tonic-gate 	if (flags & DDI_PROP_CANSLEEP)
32607c478bd9Sstevel@tonic-gate 		km_flags = KM_SLEEP;
32617c478bd9Sstevel@tonic-gate 
32627c478bd9Sstevel@tonic-gate 	if (flags & DDI_PROP_SYSTEM_DEF)
32637c478bd9Sstevel@tonic-gate 		list_head = &(DEVI(dip)->devi_sys_prop_ptr);
32647c478bd9Sstevel@tonic-gate 	else if (flags & DDI_PROP_HW_DEF)
32657c478bd9Sstevel@tonic-gate 		list_head = &(DEVI(dip)->devi_hw_prop_ptr);
32667c478bd9Sstevel@tonic-gate 
32677c478bd9Sstevel@tonic-gate 	if ((new_propp = DDI_NEW_PROP_T(km_flags)) == NULL)  {
32687c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, prop_no_mem_msg, name);
32697c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NO_MEMORY);
32707c478bd9Sstevel@tonic-gate 	}
32717c478bd9Sstevel@tonic-gate 
32727c478bd9Sstevel@tonic-gate 	/*
32737c478bd9Sstevel@tonic-gate 	 * If dev is major number 0, then we need to do a ddi_name_to_major
32747c478bd9Sstevel@tonic-gate 	 * to get the real major number for the device.  This needs to be
32757c478bd9Sstevel@tonic-gate 	 * done because some drivers need to call ddi_prop_create in their
32767c478bd9Sstevel@tonic-gate 	 * attach routines but they don't have a dev.  By creating the dev
32777c478bd9Sstevel@tonic-gate 	 * ourself if the major number is 0, drivers will not have to know what
32787c478bd9Sstevel@tonic-gate 	 * their major number.	They can just create a dev with major number
32797c478bd9Sstevel@tonic-gate 	 * 0 and pass it in.  For device 0, we will be doing a little extra
32807c478bd9Sstevel@tonic-gate 	 * work by recreating the same dev that we already have, but its the
32817c478bd9Sstevel@tonic-gate 	 * price you pay :-).
32827c478bd9Sstevel@tonic-gate 	 *
32837c478bd9Sstevel@tonic-gate 	 * This fixes bug #1098060.
32847c478bd9Sstevel@tonic-gate 	 */
32857c478bd9Sstevel@tonic-gate 	if (getmajor(dev) == DDI_MAJOR_T_UNKNOWN) {
32867c478bd9Sstevel@tonic-gate 		new_propp->prop_dev =
32877c478bd9Sstevel@tonic-gate 		    makedevice(ddi_name_to_major(DEVI(dip)->devi_binding_name),
32887c478bd9Sstevel@tonic-gate 		    getminor(dev));
32897c478bd9Sstevel@tonic-gate 	} else
32907c478bd9Sstevel@tonic-gate 		new_propp->prop_dev = dev;
32917c478bd9Sstevel@tonic-gate 
32927c478bd9Sstevel@tonic-gate 	/*
32937c478bd9Sstevel@tonic-gate 	 * Allocate space for property name and copy it in...
32947c478bd9Sstevel@tonic-gate 	 */
32957c478bd9Sstevel@tonic-gate 
32967c478bd9Sstevel@tonic-gate 	name_buf_len = strlen(name) + 1;
32977c478bd9Sstevel@tonic-gate 	new_propp->prop_name = kmem_alloc(name_buf_len, km_flags);
32987c478bd9Sstevel@tonic-gate 	if (new_propp->prop_name == 0)	{
32997c478bd9Sstevel@tonic-gate 		kmem_free(new_propp, sizeof (ddi_prop_t));
33007c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, prop_no_mem_msg, name);
33017c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NO_MEMORY);
33027c478bd9Sstevel@tonic-gate 	}
33037c478bd9Sstevel@tonic-gate 	bcopy(name, new_propp->prop_name, name_buf_len);
33047c478bd9Sstevel@tonic-gate 
33057c478bd9Sstevel@tonic-gate 	/*
33067c478bd9Sstevel@tonic-gate 	 * Set the property type
33077c478bd9Sstevel@tonic-gate 	 */
33087c478bd9Sstevel@tonic-gate 	new_propp->prop_flags = flags & DDI_PROP_TYPE_MASK;
33097c478bd9Sstevel@tonic-gate 
33107c478bd9Sstevel@tonic-gate 	/*
33117c478bd9Sstevel@tonic-gate 	 * Set length and value ONLY if not an explicit property undefine:
33127c478bd9Sstevel@tonic-gate 	 * NOTE: value and length are zero for explicit undefines.
33137c478bd9Sstevel@tonic-gate 	 */
33147c478bd9Sstevel@tonic-gate 
33157c478bd9Sstevel@tonic-gate 	if (flags & DDI_PROP_UNDEF_IT) {
33167c478bd9Sstevel@tonic-gate 		new_propp->prop_flags |= DDI_PROP_UNDEF_IT;
33177c478bd9Sstevel@tonic-gate 	} else {
33187c478bd9Sstevel@tonic-gate 		if ((new_propp->prop_len = length) != 0) {
33197c478bd9Sstevel@tonic-gate 			new_propp->prop_val = kmem_alloc(length, km_flags);
33207c478bd9Sstevel@tonic-gate 			if (new_propp->prop_val == 0)  {
33217c478bd9Sstevel@tonic-gate 				kmem_free(new_propp->prop_name, name_buf_len);
33227c478bd9Sstevel@tonic-gate 				kmem_free(new_propp, sizeof (ddi_prop_t));
33237c478bd9Sstevel@tonic-gate 				cmn_err(CE_CONT, prop_no_mem_msg, name);
33247c478bd9Sstevel@tonic-gate 				return (DDI_PROP_NO_MEMORY);
33257c478bd9Sstevel@tonic-gate 			}
33267c478bd9Sstevel@tonic-gate 			bcopy(value, new_propp->prop_val, length);
33277c478bd9Sstevel@tonic-gate 		}
33287c478bd9Sstevel@tonic-gate 	}
33297c478bd9Sstevel@tonic-gate 
33307c478bd9Sstevel@tonic-gate 	/*
33317c478bd9Sstevel@tonic-gate 	 * Link property into beginning of list. (Properties are LIFO order.)
33327c478bd9Sstevel@tonic-gate 	 */
33337c478bd9Sstevel@tonic-gate 
33347c478bd9Sstevel@tonic-gate 	mutex_enter(&(DEVI(dip)->devi_lock));
33357c478bd9Sstevel@tonic-gate 	propp = *list_head;
33367c478bd9Sstevel@tonic-gate 	new_propp->prop_next = propp;
33377c478bd9Sstevel@tonic-gate 	*list_head = new_propp;
33387c478bd9Sstevel@tonic-gate 	mutex_exit(&(DEVI(dip)->devi_lock));
33397c478bd9Sstevel@tonic-gate 	return (DDI_PROP_SUCCESS);
33407c478bd9Sstevel@tonic-gate }
33417c478bd9Sstevel@tonic-gate 
33427c478bd9Sstevel@tonic-gate 
33437c478bd9Sstevel@tonic-gate /*
33447c478bd9Sstevel@tonic-gate  * ddi_prop_change:	Modify a software managed property value
33457c478bd9Sstevel@tonic-gate  *
33467c478bd9Sstevel@tonic-gate  *			Set new length and value if found.
33477c478bd9Sstevel@tonic-gate  *			returns DDI_PROP_INVAL_ARG if dev is DDI_DEV_T_ANY or
33487c478bd9Sstevel@tonic-gate  *			input name is the NULL string.
33497c478bd9Sstevel@tonic-gate  *			returns DDI_PROP_NO_MEMORY if unable to allocate memory
33507c478bd9Sstevel@tonic-gate  *
33517c478bd9Sstevel@tonic-gate  *			Note: an undef can be modified to be a define,
33527c478bd9Sstevel@tonic-gate  *			(you can't go the other way.)
33537c478bd9Sstevel@tonic-gate  */
33547c478bd9Sstevel@tonic-gate 
33557c478bd9Sstevel@tonic-gate static int
33567c478bd9Sstevel@tonic-gate ddi_prop_change(dev_t dev, dev_info_t *dip, int flags,
33577c478bd9Sstevel@tonic-gate     char *name, caddr_t value, int length)
33587c478bd9Sstevel@tonic-gate {
33597c478bd9Sstevel@tonic-gate 	ddi_prop_t	*propp;
33609ac2ddeaScth 	ddi_prop_t	**ppropp;
33617c478bd9Sstevel@tonic-gate 	caddr_t		p = NULL;
33627c478bd9Sstevel@tonic-gate 
33639ac2ddeaScth 	if ((dev == DDI_DEV_T_ANY) || (name == NULL) || (strlen(name) == 0))
33647c478bd9Sstevel@tonic-gate 		return (DDI_PROP_INVAL_ARG);
33657c478bd9Sstevel@tonic-gate 
33667c478bd9Sstevel@tonic-gate 	/*
33677c478bd9Sstevel@tonic-gate 	 * Preallocate buffer, even if we don't need it...
33687c478bd9Sstevel@tonic-gate 	 */
33697c478bd9Sstevel@tonic-gate 	if (length != 0)  {
33709ac2ddeaScth 		p = kmem_alloc(length, (flags & DDI_PROP_CANSLEEP) ?
33719ac2ddeaScth 		    KM_SLEEP : KM_NOSLEEP);
33727c478bd9Sstevel@tonic-gate 		if (p == NULL)	{
33737c478bd9Sstevel@tonic-gate 			cmn_err(CE_CONT, prop_no_mem_msg, name);
33747c478bd9Sstevel@tonic-gate 			return (DDI_PROP_NO_MEMORY);
33757c478bd9Sstevel@tonic-gate 		}
33767c478bd9Sstevel@tonic-gate 	}
33777c478bd9Sstevel@tonic-gate 
33787c478bd9Sstevel@tonic-gate 	/*
33799ac2ddeaScth 	 * If the dev_t value contains DDI_MAJOR_T_UNKNOWN for the major
33809ac2ddeaScth 	 * number, a real dev_t value should be created based upon the dip's
33819ac2ddeaScth 	 * binding driver.  See ddi_prop_add...
33829ac2ddeaScth 	 */
33839ac2ddeaScth 	if (getmajor(dev) == DDI_MAJOR_T_UNKNOWN)
33849ac2ddeaScth 		dev = makedevice(
33859ac2ddeaScth 		    ddi_name_to_major(DEVI(dip)->devi_binding_name),
33869ac2ddeaScth 		    getminor(dev));
33879ac2ddeaScth 
33889ac2ddeaScth 	/*
33897c478bd9Sstevel@tonic-gate 	 * Check to see if the property exists.  If so we modify it.
33907c478bd9Sstevel@tonic-gate 	 * Else we create it by calling ddi_prop_add().
33917c478bd9Sstevel@tonic-gate 	 */
33927c478bd9Sstevel@tonic-gate 	mutex_enter(&(DEVI(dip)->devi_lock));
33939ac2ddeaScth 	ppropp = &DEVI(dip)->devi_drv_prop_ptr;
33947c478bd9Sstevel@tonic-gate 	if (flags & DDI_PROP_SYSTEM_DEF)
33959ac2ddeaScth 		ppropp = &DEVI(dip)->devi_sys_prop_ptr;
33967c478bd9Sstevel@tonic-gate 	else if (flags & DDI_PROP_HW_DEF)
33979ac2ddeaScth 		ppropp = &DEVI(dip)->devi_hw_prop_ptr;
33987c478bd9Sstevel@tonic-gate 
33999ac2ddeaScth 	if ((propp = i_ddi_prop_search(dev, name, flags, ppropp)) != NULL) {
34007c478bd9Sstevel@tonic-gate 		/*
34017c478bd9Sstevel@tonic-gate 		 * Need to reallocate buffer?  If so, do it
34029ac2ddeaScth 		 * carefully (reuse same space if new prop
34037c478bd9Sstevel@tonic-gate 		 * is same size and non-NULL sized).
34047c478bd9Sstevel@tonic-gate 		 */
34057c478bd9Sstevel@tonic-gate 		if (length != 0)
34067c478bd9Sstevel@tonic-gate 			bcopy(value, p, length);
34077c478bd9Sstevel@tonic-gate 
34087c478bd9Sstevel@tonic-gate 		if (propp->prop_len != 0)
34097c478bd9Sstevel@tonic-gate 			kmem_free(propp->prop_val, propp->prop_len);
34107c478bd9Sstevel@tonic-gate 
34117c478bd9Sstevel@tonic-gate 		propp->prop_len = length;
34127c478bd9Sstevel@tonic-gate 		propp->prop_val = p;
34137c478bd9Sstevel@tonic-gate 		propp->prop_flags &= ~DDI_PROP_UNDEF_IT;
34147c478bd9Sstevel@tonic-gate 		mutex_exit(&(DEVI(dip)->devi_lock));
34157c478bd9Sstevel@tonic-gate 		return (DDI_PROP_SUCCESS);
34167c478bd9Sstevel@tonic-gate 	}
34177c478bd9Sstevel@tonic-gate 
34187c478bd9Sstevel@tonic-gate 	mutex_exit(&(DEVI(dip)->devi_lock));
34197c478bd9Sstevel@tonic-gate 	if (length != 0)
34207c478bd9Sstevel@tonic-gate 		kmem_free(p, length);
34219ac2ddeaScth 
34227c478bd9Sstevel@tonic-gate 	return (ddi_prop_add(dev, dip, flags, name, value, length));
34237c478bd9Sstevel@tonic-gate }
34247c478bd9Sstevel@tonic-gate 
34257c478bd9Sstevel@tonic-gate /*
34267c478bd9Sstevel@tonic-gate  * Common update routine used to update and encode a property.	Creates
34277c478bd9Sstevel@tonic-gate  * a property handle, calls the property encode routine, figures out if
34287c478bd9Sstevel@tonic-gate  * the property already exists and updates if it does.	Otherwise it
34297c478bd9Sstevel@tonic-gate  * creates if it does not exist.
34307c478bd9Sstevel@tonic-gate  */
34317c478bd9Sstevel@tonic-gate int
34327c478bd9Sstevel@tonic-gate ddi_prop_update_common(dev_t match_dev, dev_info_t *dip, int flags,
34337c478bd9Sstevel@tonic-gate     char *name, void *data, uint_t nelements,
34347c478bd9Sstevel@tonic-gate     int (*prop_create)(prop_handle_t *, void *data, uint_t nelements))
34357c478bd9Sstevel@tonic-gate {
34367c478bd9Sstevel@tonic-gate 	prop_handle_t	ph;
34377c478bd9Sstevel@tonic-gate 	int		rval;
34387c478bd9Sstevel@tonic-gate 	uint_t		ourflags;
34397c478bd9Sstevel@tonic-gate 
34407c478bd9Sstevel@tonic-gate 	/*
34417c478bd9Sstevel@tonic-gate 	 * If dev_t is DDI_DEV_T_ANY or name's length is zero,
34427c478bd9Sstevel@tonic-gate 	 * return error.
34437c478bd9Sstevel@tonic-gate 	 */
34447c478bd9Sstevel@tonic-gate 	if (match_dev == DDI_DEV_T_ANY || name == NULL || strlen(name) == 0)
34457c478bd9Sstevel@tonic-gate 		return (DDI_PROP_INVAL_ARG);
34467c478bd9Sstevel@tonic-gate 
34477c478bd9Sstevel@tonic-gate 	/*
34487c478bd9Sstevel@tonic-gate 	 * Create the handle
34497c478bd9Sstevel@tonic-gate 	 */
34507c478bd9Sstevel@tonic-gate 	ph.ph_data = NULL;
34517c478bd9Sstevel@tonic-gate 	ph.ph_cur_pos = NULL;
34527c478bd9Sstevel@tonic-gate 	ph.ph_save_pos = NULL;
34537c478bd9Sstevel@tonic-gate 	ph.ph_size = 0;
34547c478bd9Sstevel@tonic-gate 	ph.ph_ops = &prop_1275_ops;
34557c478bd9Sstevel@tonic-gate 
34567c478bd9Sstevel@tonic-gate 	/*
34577c478bd9Sstevel@tonic-gate 	 * ourflags:
34587c478bd9Sstevel@tonic-gate 	 * For compatibility with the old interfaces.  The old interfaces
34597c478bd9Sstevel@tonic-gate 	 * didn't sleep by default and slept when the flag was set.  These
34607c478bd9Sstevel@tonic-gate 	 * interfaces to the opposite.	So the old interfaces now set the
34617c478bd9Sstevel@tonic-gate 	 * DDI_PROP_DONTSLEEP flag by default which tells us not to sleep.
34627c478bd9Sstevel@tonic-gate 	 *
34637c478bd9Sstevel@tonic-gate 	 * ph.ph_flags:
34647c478bd9Sstevel@tonic-gate 	 * Blocked data or unblocked data allocation
34657c478bd9Sstevel@tonic-gate 	 * for ph.ph_data in ddi_prop_encode_alloc()
34667c478bd9Sstevel@tonic-gate 	 */
34677c478bd9Sstevel@tonic-gate 	if (flags & DDI_PROP_DONTSLEEP) {
34687c478bd9Sstevel@tonic-gate 		ourflags = flags;
34697c478bd9Sstevel@tonic-gate 		ph.ph_flags = DDI_PROP_DONTSLEEP;
34707c478bd9Sstevel@tonic-gate 	} else {
34717c478bd9Sstevel@tonic-gate 		ourflags = flags | DDI_PROP_CANSLEEP;
34727c478bd9Sstevel@tonic-gate 		ph.ph_flags = DDI_PROP_CANSLEEP;
34737c478bd9Sstevel@tonic-gate 	}
34747c478bd9Sstevel@tonic-gate 
34757c478bd9Sstevel@tonic-gate 	/*
34767c478bd9Sstevel@tonic-gate 	 * Encode the data and store it in the property handle by
34777c478bd9Sstevel@tonic-gate 	 * calling the prop_encode routine.
34787c478bd9Sstevel@tonic-gate 	 */
34797c478bd9Sstevel@tonic-gate 	if ((rval = (*prop_create)(&ph, data, nelements)) !=
34807c478bd9Sstevel@tonic-gate 	    DDI_PROP_SUCCESS) {
34817c478bd9Sstevel@tonic-gate 		if (rval == DDI_PROP_NO_MEMORY)
34827c478bd9Sstevel@tonic-gate 			cmn_err(CE_CONT, prop_no_mem_msg, name);
34837c478bd9Sstevel@tonic-gate 		if (ph.ph_size != 0)
34847c478bd9Sstevel@tonic-gate 			kmem_free(ph.ph_data, ph.ph_size);
34857c478bd9Sstevel@tonic-gate 		return (rval);
34867c478bd9Sstevel@tonic-gate 	}
34877c478bd9Sstevel@tonic-gate 
34887c478bd9Sstevel@tonic-gate 	/*
34897c478bd9Sstevel@tonic-gate 	 * The old interfaces use a stacking approach to creating
34907c478bd9Sstevel@tonic-gate 	 * properties.	If we are being called from the old interfaces,
34917c478bd9Sstevel@tonic-gate 	 * the DDI_PROP_STACK_CREATE flag will be set, so we just do a
34927c478bd9Sstevel@tonic-gate 	 * create without checking.
34937c478bd9Sstevel@tonic-gate 	 */
34947c478bd9Sstevel@tonic-gate 	if (flags & DDI_PROP_STACK_CREATE) {
34957c478bd9Sstevel@tonic-gate 		rval = ddi_prop_add(match_dev, dip,
34967c478bd9Sstevel@tonic-gate 		    ourflags, name, ph.ph_data, ph.ph_size);
34977c478bd9Sstevel@tonic-gate 	} else {
34987c478bd9Sstevel@tonic-gate 		rval = ddi_prop_change(match_dev, dip,
34997c478bd9Sstevel@tonic-gate 		    ourflags, name, ph.ph_data, ph.ph_size);
35007c478bd9Sstevel@tonic-gate 	}
35017c478bd9Sstevel@tonic-gate 
35027c478bd9Sstevel@tonic-gate 	/*
35037c478bd9Sstevel@tonic-gate 	 * Free the encoded data allocated in the prop_encode routine.
35047c478bd9Sstevel@tonic-gate 	 */
35057c478bd9Sstevel@tonic-gate 	if (ph.ph_size != 0)
35067c478bd9Sstevel@tonic-gate 		kmem_free(ph.ph_data, ph.ph_size);
35077c478bd9Sstevel@tonic-gate 
35087c478bd9Sstevel@tonic-gate 	return (rval);
35097c478bd9Sstevel@tonic-gate }
35107c478bd9Sstevel@tonic-gate 
35117c478bd9Sstevel@tonic-gate 
35127c478bd9Sstevel@tonic-gate /*
35137c478bd9Sstevel@tonic-gate  * ddi_prop_create:	Define a managed property:
35147c478bd9Sstevel@tonic-gate  *			See above for details.
35157c478bd9Sstevel@tonic-gate  */
35167c478bd9Sstevel@tonic-gate 
35177c478bd9Sstevel@tonic-gate int
35187c478bd9Sstevel@tonic-gate ddi_prop_create(dev_t dev, dev_info_t *dip, int flag,
35197c478bd9Sstevel@tonic-gate     char *name, caddr_t value, int length)
35207c478bd9Sstevel@tonic-gate {
35217c478bd9Sstevel@tonic-gate 	if (!(flag & DDI_PROP_CANSLEEP)) {
35227c478bd9Sstevel@tonic-gate 		flag |= DDI_PROP_DONTSLEEP;
35237c478bd9Sstevel@tonic-gate #ifdef DDI_PROP_DEBUG
35247c478bd9Sstevel@tonic-gate 		if (length != 0)
35257c478bd9Sstevel@tonic-gate 			cmn_err(CE_NOTE, "!ddi_prop_create: interface obsolete,"
35267c478bd9Sstevel@tonic-gate 			    "use ddi_prop_update (prop = %s, node = %s%d)",
35277c478bd9Sstevel@tonic-gate 			    name, ddi_driver_name(dip), ddi_get_instance(dip));
35287c478bd9Sstevel@tonic-gate #endif /* DDI_PROP_DEBUG */
35297c478bd9Sstevel@tonic-gate 	}
35307c478bd9Sstevel@tonic-gate 	flag &= ~DDI_PROP_SYSTEM_DEF;
3531b9ccdc5aScth 	flag |= DDI_PROP_STACK_CREATE | DDI_PROP_TYPE_ANY;
3532b9ccdc5aScth 	return (ddi_prop_update_common(dev, dip, flag, name,
35337c478bd9Sstevel@tonic-gate 	    value, length, ddi_prop_fm_encode_bytes));
35347c478bd9Sstevel@tonic-gate }
35357c478bd9Sstevel@tonic-gate 
35367c478bd9Sstevel@tonic-gate int
35377c478bd9Sstevel@tonic-gate e_ddi_prop_create(dev_t dev, dev_info_t *dip, int flag,
35387c478bd9Sstevel@tonic-gate     char *name, caddr_t value, int length)
35397c478bd9Sstevel@tonic-gate {
35407c478bd9Sstevel@tonic-gate 	if (!(flag & DDI_PROP_CANSLEEP))
35417c478bd9Sstevel@tonic-gate 		flag |= DDI_PROP_DONTSLEEP;
3542b9ccdc5aScth 	flag |= DDI_PROP_SYSTEM_DEF | DDI_PROP_STACK_CREATE | DDI_PROP_TYPE_ANY;
3543b9ccdc5aScth 	return (ddi_prop_update_common(dev, dip, flag,
35447c478bd9Sstevel@tonic-gate 	    name, value, length, ddi_prop_fm_encode_bytes));
35457c478bd9Sstevel@tonic-gate }
35467c478bd9Sstevel@tonic-gate 
35477c478bd9Sstevel@tonic-gate int
35487c478bd9Sstevel@tonic-gate ddi_prop_modify(dev_t dev, dev_info_t *dip, int flag,
35497c478bd9Sstevel@tonic-gate     char *name, caddr_t value, int length)
35507c478bd9Sstevel@tonic-gate {
35517c478bd9Sstevel@tonic-gate 	ASSERT((flag & DDI_PROP_TYPE_MASK) == 0);
35527c478bd9Sstevel@tonic-gate 
35537c478bd9Sstevel@tonic-gate 	/*
35547c478bd9Sstevel@tonic-gate 	 * If dev_t is DDI_DEV_T_ANY or name's length is zero,
35557c478bd9Sstevel@tonic-gate 	 * return error.
35567c478bd9Sstevel@tonic-gate 	 */
35577c478bd9Sstevel@tonic-gate 	if (dev == DDI_DEV_T_ANY || name == NULL || strlen(name) == 0)
35587c478bd9Sstevel@tonic-gate 		return (DDI_PROP_INVAL_ARG);
35597c478bd9Sstevel@tonic-gate 
35607c478bd9Sstevel@tonic-gate 	if (!(flag & DDI_PROP_CANSLEEP))
35617c478bd9Sstevel@tonic-gate 		flag |= DDI_PROP_DONTSLEEP;
35627c478bd9Sstevel@tonic-gate 	flag &= ~DDI_PROP_SYSTEM_DEF;
35639ac2ddeaScth 	if (ddi_prop_exists(dev, dip, (flag | DDI_PROP_NOTPROM), name) == 0)
35647c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
35657c478bd9Sstevel@tonic-gate 
35667c478bd9Sstevel@tonic-gate 	return (ddi_prop_update_common(dev, dip,
35677c478bd9Sstevel@tonic-gate 	    (flag | DDI_PROP_TYPE_BYTE), name,
35687c478bd9Sstevel@tonic-gate 	    value, length, ddi_prop_fm_encode_bytes));
35697c478bd9Sstevel@tonic-gate }
35707c478bd9Sstevel@tonic-gate 
35717c478bd9Sstevel@tonic-gate int
35727c478bd9Sstevel@tonic-gate e_ddi_prop_modify(dev_t dev, dev_info_t *dip, int flag,
35737c478bd9Sstevel@tonic-gate     char *name, caddr_t value, int length)
35747c478bd9Sstevel@tonic-gate {
35757c478bd9Sstevel@tonic-gate 	ASSERT((flag & DDI_PROP_TYPE_MASK) == 0);
35767c478bd9Sstevel@tonic-gate 
35777c478bd9Sstevel@tonic-gate 	/*
35787c478bd9Sstevel@tonic-gate 	 * If dev_t is DDI_DEV_T_ANY or name's length is zero,
35797c478bd9Sstevel@tonic-gate 	 * return error.
35807c478bd9Sstevel@tonic-gate 	 */
35817c478bd9Sstevel@tonic-gate 	if (dev == DDI_DEV_T_ANY || name == NULL || strlen(name) == 0)
35827c478bd9Sstevel@tonic-gate 		return (DDI_PROP_INVAL_ARG);
35837c478bd9Sstevel@tonic-gate 
35849ac2ddeaScth 	if (ddi_prop_exists(dev, dip, (flag | DDI_PROP_SYSTEM_DEF), name) == 0)
35857c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
35867c478bd9Sstevel@tonic-gate 
35877c478bd9Sstevel@tonic-gate 	if (!(flag & DDI_PROP_CANSLEEP))
35887c478bd9Sstevel@tonic-gate 		flag |= DDI_PROP_DONTSLEEP;
35897c478bd9Sstevel@tonic-gate 	return (ddi_prop_update_common(dev, dip,
35907c478bd9Sstevel@tonic-gate 	    (flag | DDI_PROP_SYSTEM_DEF | DDI_PROP_TYPE_BYTE),
35917c478bd9Sstevel@tonic-gate 	    name, value, length, ddi_prop_fm_encode_bytes));
35927c478bd9Sstevel@tonic-gate }
35937c478bd9Sstevel@tonic-gate 
35947c478bd9Sstevel@tonic-gate 
35957c478bd9Sstevel@tonic-gate /*
35967c478bd9Sstevel@tonic-gate  * Common lookup routine used to lookup and decode a property.
35977c478bd9Sstevel@tonic-gate  * Creates a property handle, searches for the raw encoded data,
35987c478bd9Sstevel@tonic-gate  * fills in the handle, and calls the property decode functions
35997c478bd9Sstevel@tonic-gate  * passed in.
36007c478bd9Sstevel@tonic-gate  *
36017c478bd9Sstevel@tonic-gate  * This routine is not static because ddi_bus_prop_op() which lives in
36027c478bd9Sstevel@tonic-gate  * ddi_impl.c calls it.  No driver should be calling this routine.
36037c478bd9Sstevel@tonic-gate  */
36047c478bd9Sstevel@tonic-gate int
36057c478bd9Sstevel@tonic-gate ddi_prop_lookup_common(dev_t match_dev, dev_info_t *dip,
36067c478bd9Sstevel@tonic-gate     uint_t flags, char *name, void *data, uint_t *nelements,
36077c478bd9Sstevel@tonic-gate     int (*prop_decoder)(prop_handle_t *, void *data, uint_t *nelements))
36087c478bd9Sstevel@tonic-gate {
36097c478bd9Sstevel@tonic-gate 	int		rval;
36107c478bd9Sstevel@tonic-gate 	uint_t		ourflags;
36117c478bd9Sstevel@tonic-gate 	prop_handle_t	ph;
36127c478bd9Sstevel@tonic-gate 
36137c478bd9Sstevel@tonic-gate 	if ((match_dev == DDI_DEV_T_NONE) ||
36147c478bd9Sstevel@tonic-gate 	    (name == NULL) || (strlen(name) == 0))
36157c478bd9Sstevel@tonic-gate 		return (DDI_PROP_INVAL_ARG);
36167c478bd9Sstevel@tonic-gate 
36177c478bd9Sstevel@tonic-gate 	ourflags = (flags & DDI_PROP_DONTSLEEP) ? flags :
36187c478bd9Sstevel@tonic-gate 	    flags | DDI_PROP_CANSLEEP;
36197c478bd9Sstevel@tonic-gate 
36207c478bd9Sstevel@tonic-gate 	/*
36217c478bd9Sstevel@tonic-gate 	 * Get the encoded data
36227c478bd9Sstevel@tonic-gate 	 */
36237c478bd9Sstevel@tonic-gate 	bzero(&ph, sizeof (prop_handle_t));
36247c478bd9Sstevel@tonic-gate 
362565cf7c95SVikram Hegde 	if ((flags & DDI_UNBND_DLPI2) || (flags & DDI_PROP_ROOTNEX_GLOBAL)) {
36267c478bd9Sstevel@tonic-gate 		/*
362765cf7c95SVikram Hegde 		 * For rootnex and unbound dlpi style-2 devices, index into
36287c478bd9Sstevel@tonic-gate 		 * the devnames' array and search the global
36297c478bd9Sstevel@tonic-gate 		 * property list.
36307c478bd9Sstevel@tonic-gate 		 */
36317c478bd9Sstevel@tonic-gate 		ourflags &= ~DDI_UNBND_DLPI2;
36327c478bd9Sstevel@tonic-gate 		rval = i_ddi_prop_search_global(match_dev,
36337c478bd9Sstevel@tonic-gate 		    ourflags, name, &ph.ph_data, &ph.ph_size);
36347c478bd9Sstevel@tonic-gate 	} else {
36357c478bd9Sstevel@tonic-gate 		rval = ddi_prop_search_common(match_dev, dip,
36367c478bd9Sstevel@tonic-gate 		    PROP_LEN_AND_VAL_ALLOC, ourflags, name,
36377c478bd9Sstevel@tonic-gate 		    &ph.ph_data, &ph.ph_size);
36387c478bd9Sstevel@tonic-gate 
36397c478bd9Sstevel@tonic-gate 	}
36407c478bd9Sstevel@tonic-gate 
36417c478bd9Sstevel@tonic-gate 	if (rval != DDI_PROP_SUCCESS && rval != DDI_PROP_FOUND_1275) {
36427c478bd9Sstevel@tonic-gate 		ASSERT(ph.ph_data == NULL);
36437c478bd9Sstevel@tonic-gate 		ASSERT(ph.ph_size == 0);
36447c478bd9Sstevel@tonic-gate 		return (rval);
36457c478bd9Sstevel@tonic-gate 	}
36467c478bd9Sstevel@tonic-gate 
36477c478bd9Sstevel@tonic-gate 	/*
36487c478bd9Sstevel@tonic-gate 	 * If the encoded data came from a OBP or software
36497c478bd9Sstevel@tonic-gate 	 * use the 1275 OBP decode/encode routines.
36507c478bd9Sstevel@tonic-gate 	 */
36517c478bd9Sstevel@tonic-gate 	ph.ph_cur_pos = ph.ph_data;
36527c478bd9Sstevel@tonic-gate 	ph.ph_save_pos = ph.ph_data;
36537c478bd9Sstevel@tonic-gate 	ph.ph_ops = &prop_1275_ops;
36547c478bd9Sstevel@tonic-gate 	ph.ph_flags = (rval == DDI_PROP_FOUND_1275) ? PH_FROM_PROM : 0;
36557c478bd9Sstevel@tonic-gate 
36567c478bd9Sstevel@tonic-gate 	rval = (*prop_decoder)(&ph, data, nelements);
36577c478bd9Sstevel@tonic-gate 
36587c478bd9Sstevel@tonic-gate 	/*
36597c478bd9Sstevel@tonic-gate 	 * Free the encoded data
36607c478bd9Sstevel@tonic-gate 	 */
36617c478bd9Sstevel@tonic-gate 	if (ph.ph_size != 0)
36627c478bd9Sstevel@tonic-gate 		kmem_free(ph.ph_data, ph.ph_size);
36637c478bd9Sstevel@tonic-gate 
36647c478bd9Sstevel@tonic-gate 	return (rval);
36657c478bd9Sstevel@tonic-gate }
36667c478bd9Sstevel@tonic-gate 
36677c478bd9Sstevel@tonic-gate /*
36687c478bd9Sstevel@tonic-gate  * Lookup and return an array of composite properties.  The driver must
36697c478bd9Sstevel@tonic-gate  * provide the decode routine.
36707c478bd9Sstevel@tonic-gate  */
36717c478bd9Sstevel@tonic-gate int
36727c478bd9Sstevel@tonic-gate ddi_prop_lookup(dev_t match_dev, dev_info_t *dip,
36737c478bd9Sstevel@tonic-gate     uint_t flags, char *name, void *data, uint_t *nelements,
36747c478bd9Sstevel@tonic-gate     int (*prop_decoder)(prop_handle_t *, void *data, uint_t *nelements))
36757c478bd9Sstevel@tonic-gate {
36767c478bd9Sstevel@tonic-gate 	return (ddi_prop_lookup_common(match_dev, dip,
36777c478bd9Sstevel@tonic-gate 	    (flags | DDI_PROP_TYPE_COMPOSITE), name,
36787c478bd9Sstevel@tonic-gate 	    data, nelements, prop_decoder));
36797c478bd9Sstevel@tonic-gate }
36807c478bd9Sstevel@tonic-gate 
36817c478bd9Sstevel@tonic-gate /*
36827c478bd9Sstevel@tonic-gate  * Return 1 if a property exists (no type checking done).
36837c478bd9Sstevel@tonic-gate  * Return 0 if it does not exist.
36847c478bd9Sstevel@tonic-gate  */
36857c478bd9Sstevel@tonic-gate int
36867c478bd9Sstevel@tonic-gate ddi_prop_exists(dev_t match_dev, dev_info_t *dip, uint_t flags, char *name)
36877c478bd9Sstevel@tonic-gate {
36887c478bd9Sstevel@tonic-gate 	int	i;
36897c478bd9Sstevel@tonic-gate 	uint_t	x = 0;
36907c478bd9Sstevel@tonic-gate 
36917c478bd9Sstevel@tonic-gate 	i = ddi_prop_search_common(match_dev, dip, PROP_EXISTS,
36927c478bd9Sstevel@tonic-gate 	    flags | DDI_PROP_TYPE_MASK, name, NULL, &x);
36937c478bd9Sstevel@tonic-gate 	return (i == DDI_PROP_SUCCESS || i == DDI_PROP_FOUND_1275);
36947c478bd9Sstevel@tonic-gate }
36957c478bd9Sstevel@tonic-gate 
36967c478bd9Sstevel@tonic-gate 
36977c478bd9Sstevel@tonic-gate /*
36987c478bd9Sstevel@tonic-gate  * Update an array of composite properties.  The driver must
36997c478bd9Sstevel@tonic-gate  * provide the encode routine.
37007c478bd9Sstevel@tonic-gate  */
37017c478bd9Sstevel@tonic-gate int
37027c478bd9Sstevel@tonic-gate ddi_prop_update(dev_t match_dev, dev_info_t *dip,
37037c478bd9Sstevel@tonic-gate     char *name, void *data, uint_t nelements,
37047c478bd9Sstevel@tonic-gate     int (*prop_create)(prop_handle_t *, void *data, uint_t nelements))
37057c478bd9Sstevel@tonic-gate {
37067c478bd9Sstevel@tonic-gate 	return (ddi_prop_update_common(match_dev, dip, DDI_PROP_TYPE_COMPOSITE,
37077c478bd9Sstevel@tonic-gate 	    name, data, nelements, prop_create));
37087c478bd9Sstevel@tonic-gate }
37097c478bd9Sstevel@tonic-gate 
37107c478bd9Sstevel@tonic-gate /*
37117c478bd9Sstevel@tonic-gate  * Get a single integer or boolean property and return it.
37127c478bd9Sstevel@tonic-gate  * If the property does not exists, or cannot be decoded,
37137c478bd9Sstevel@tonic-gate  * then return the defvalue passed in.
37147c478bd9Sstevel@tonic-gate  *
37157c478bd9Sstevel@tonic-gate  * This routine always succeeds.
37167c478bd9Sstevel@tonic-gate  */
37177c478bd9Sstevel@tonic-gate int
37187c478bd9Sstevel@tonic-gate ddi_prop_get_int(dev_t match_dev, dev_info_t *dip, uint_t flags,
37197c478bd9Sstevel@tonic-gate     char *name, int defvalue)
37207c478bd9Sstevel@tonic-gate {
37217c478bd9Sstevel@tonic-gate 	int	data;
37227c478bd9Sstevel@tonic-gate 	uint_t	nelements;
37237c478bd9Sstevel@tonic-gate 	int	rval;
37247c478bd9Sstevel@tonic-gate 
37257c478bd9Sstevel@tonic-gate 	if (flags & ~(DDI_PROP_DONTPASS | DDI_PROP_NOTPROM |
372665cf7c95SVikram Hegde 	    LDI_DEV_T_ANY | DDI_UNBND_DLPI2 | DDI_PROP_ROOTNEX_GLOBAL)) {
37277c478bd9Sstevel@tonic-gate #ifdef DEBUG
37287c478bd9Sstevel@tonic-gate 		if (dip != NULL) {
37297c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "ddi_prop_get_int: invalid flag"
37307c478bd9Sstevel@tonic-gate 			    " 0x%x (prop = %s, node = %s%d)", flags,
37317c478bd9Sstevel@tonic-gate 			    name, ddi_driver_name(dip), ddi_get_instance(dip));
37327c478bd9Sstevel@tonic-gate 		}
37337c478bd9Sstevel@tonic-gate #endif /* DEBUG */
37347c478bd9Sstevel@tonic-gate 		flags &= DDI_PROP_DONTPASS | DDI_PROP_NOTPROM |
37357c478bd9Sstevel@tonic-gate 		    LDI_DEV_T_ANY | DDI_UNBND_DLPI2;
37367c478bd9Sstevel@tonic-gate 	}
37377c478bd9Sstevel@tonic-gate 
37387c478bd9Sstevel@tonic-gate 	if ((rval = ddi_prop_lookup_common(match_dev, dip,
37397c478bd9Sstevel@tonic-gate 	    (flags | DDI_PROP_TYPE_INT), name, &data, &nelements,
37407c478bd9Sstevel@tonic-gate 	    ddi_prop_fm_decode_int)) != DDI_PROP_SUCCESS) {
37417c478bd9Sstevel@tonic-gate 		if (rval == DDI_PROP_END_OF_DATA)
37427c478bd9Sstevel@tonic-gate 			data = 1;
37437c478bd9Sstevel@tonic-gate 		else
37447c478bd9Sstevel@tonic-gate 			data = defvalue;
37457c478bd9Sstevel@tonic-gate 	}
37467c478bd9Sstevel@tonic-gate 	return (data);
37477c478bd9Sstevel@tonic-gate }
37487c478bd9Sstevel@tonic-gate 
37497c478bd9Sstevel@tonic-gate /*
37507c478bd9Sstevel@tonic-gate  * Get a single 64 bit integer or boolean property and return it.
37517c478bd9Sstevel@tonic-gate  * If the property does not exists, or cannot be decoded,
37527c478bd9Sstevel@tonic-gate  * then return the defvalue passed in.
37537c478bd9Sstevel@tonic-gate  *
37547c478bd9Sstevel@tonic-gate  * This routine always succeeds.
37557c478bd9Sstevel@tonic-gate  */
37567c478bd9Sstevel@tonic-gate int64_t
37577c478bd9Sstevel@tonic-gate ddi_prop_get_int64(dev_t match_dev, dev_info_t *dip, uint_t flags,
37587c478bd9Sstevel@tonic-gate     char *name, int64_t defvalue)
37597c478bd9Sstevel@tonic-gate {
37607c478bd9Sstevel@tonic-gate 	int64_t	data;
37617c478bd9Sstevel@tonic-gate 	uint_t	nelements;
37627c478bd9Sstevel@tonic-gate 	int	rval;
37637c478bd9Sstevel@tonic-gate 
37647c478bd9Sstevel@tonic-gate 	if (flags & ~(DDI_PROP_DONTPASS | DDI_PROP_NOTPROM |
376565cf7c95SVikram Hegde 	    LDI_DEV_T_ANY | DDI_UNBND_DLPI2 | DDI_PROP_ROOTNEX_GLOBAL)) {
37667c478bd9Sstevel@tonic-gate #ifdef DEBUG
37677c478bd9Sstevel@tonic-gate 		if (dip != NULL) {
37687c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "ddi_prop_get_int64: invalid flag"
37697c478bd9Sstevel@tonic-gate 			    " 0x%x (prop = %s, node = %s%d)", flags,
37707c478bd9Sstevel@tonic-gate 			    name, ddi_driver_name(dip), ddi_get_instance(dip));
37717c478bd9Sstevel@tonic-gate 		}
37727c478bd9Sstevel@tonic-gate #endif /* DEBUG */
37737c478bd9Sstevel@tonic-gate 		return (DDI_PROP_INVAL_ARG);
37747c478bd9Sstevel@tonic-gate 	}
37757c478bd9Sstevel@tonic-gate 
37767c478bd9Sstevel@tonic-gate 	if ((rval = ddi_prop_lookup_common(match_dev, dip,
37777c478bd9Sstevel@tonic-gate 	    (flags | DDI_PROP_TYPE_INT64 | DDI_PROP_NOTPROM),
37787c478bd9Sstevel@tonic-gate 	    name, &data, &nelements, ddi_prop_fm_decode_int64))
37797c478bd9Sstevel@tonic-gate 	    != DDI_PROP_SUCCESS) {
37807c478bd9Sstevel@tonic-gate 		if (rval == DDI_PROP_END_OF_DATA)
37817c478bd9Sstevel@tonic-gate 			data = 1;
37827c478bd9Sstevel@tonic-gate 		else
37837c478bd9Sstevel@tonic-gate 			data = defvalue;
37847c478bd9Sstevel@tonic-gate 	}
37857c478bd9Sstevel@tonic-gate 	return (data);
37867c478bd9Sstevel@tonic-gate }
37877c478bd9Sstevel@tonic-gate 
37887c478bd9Sstevel@tonic-gate /*
37897c478bd9Sstevel@tonic-gate  * Get an array of integer property
37907c478bd9Sstevel@tonic-gate  */
37917c478bd9Sstevel@tonic-gate int
37927c478bd9Sstevel@tonic-gate ddi_prop_lookup_int_array(dev_t match_dev, dev_info_t *dip, uint_t flags,
37937c478bd9Sstevel@tonic-gate     char *name, int **data, uint_t *nelements)
37947c478bd9Sstevel@tonic-gate {
37957c478bd9Sstevel@tonic-gate 	if (flags & ~(DDI_PROP_DONTPASS | DDI_PROP_NOTPROM |
379665cf7c95SVikram Hegde 	    LDI_DEV_T_ANY | DDI_UNBND_DLPI2 | DDI_PROP_ROOTNEX_GLOBAL)) {
37977c478bd9Sstevel@tonic-gate #ifdef DEBUG
37987c478bd9Sstevel@tonic-gate 		if (dip != NULL) {
37997c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "ddi_prop_lookup_int_array: "
38007c478bd9Sstevel@tonic-gate 			    "invalid flag 0x%x (prop = %s, node = %s%d)",
38017c478bd9Sstevel@tonic-gate 			    flags, name, ddi_driver_name(dip),
38027c478bd9Sstevel@tonic-gate 			    ddi_get_instance(dip));
38037c478bd9Sstevel@tonic-gate 		}
38047c478bd9Sstevel@tonic-gate #endif /* DEBUG */
38057c478bd9Sstevel@tonic-gate 		flags &= DDI_PROP_DONTPASS | DDI_PROP_NOTPROM |
38067c478bd9Sstevel@tonic-gate 		    LDI_DEV_T_ANY | DDI_UNBND_DLPI2;
38077c478bd9Sstevel@tonic-gate 	}
38087c478bd9Sstevel@tonic-gate 
38097c478bd9Sstevel@tonic-gate 	return (ddi_prop_lookup_common(match_dev, dip,
38107c478bd9Sstevel@tonic-gate 	    (flags | DDI_PROP_TYPE_INT), name, data,
38117c478bd9Sstevel@tonic-gate 	    nelements, ddi_prop_fm_decode_ints));
38127c478bd9Sstevel@tonic-gate }
38137c478bd9Sstevel@tonic-gate 
38147c478bd9Sstevel@tonic-gate /*
38157c478bd9Sstevel@tonic-gate  * Get an array of 64 bit integer properties
38167c478bd9Sstevel@tonic-gate  */
38177c478bd9Sstevel@tonic-gate int
38187c478bd9Sstevel@tonic-gate ddi_prop_lookup_int64_array(dev_t match_dev, dev_info_t *dip, uint_t flags,
38197c478bd9Sstevel@tonic-gate     char *name, int64_t **data, uint_t *nelements)
38207c478bd9Sstevel@tonic-gate {
38217c478bd9Sstevel@tonic-gate 	if (flags & ~(DDI_PROP_DONTPASS | DDI_PROP_NOTPROM |
382265cf7c95SVikram Hegde 	    LDI_DEV_T_ANY | DDI_UNBND_DLPI2 | DDI_PROP_ROOTNEX_GLOBAL)) {
38237c478bd9Sstevel@tonic-gate #ifdef DEBUG
38247c478bd9Sstevel@tonic-gate 		if (dip != NULL) {
38257c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "ddi_prop_lookup_int64_array: "
38267c478bd9Sstevel@tonic-gate 			    "invalid flag 0x%x (prop = %s, node = %s%d)",
38277c478bd9Sstevel@tonic-gate 			    flags, name, ddi_driver_name(dip),
38287c478bd9Sstevel@tonic-gate 			    ddi_get_instance(dip));
38297c478bd9Sstevel@tonic-gate 		}
38307c478bd9Sstevel@tonic-gate #endif /* DEBUG */
38317c478bd9Sstevel@tonic-gate 		return (DDI_PROP_INVAL_ARG);
38327c478bd9Sstevel@tonic-gate 	}
38337c478bd9Sstevel@tonic-gate 
38347c478bd9Sstevel@tonic-gate 	return (ddi_prop_lookup_common(match_dev, dip,
38357c478bd9Sstevel@tonic-gate 	    (flags | DDI_PROP_TYPE_INT64 | DDI_PROP_NOTPROM),
38367c478bd9Sstevel@tonic-gate 	    name, data, nelements, ddi_prop_fm_decode_int64_array));
38377c478bd9Sstevel@tonic-gate }
38387c478bd9Sstevel@tonic-gate 
38397c478bd9Sstevel@tonic-gate /*
38407c478bd9Sstevel@tonic-gate  * Update a single integer property.  If the property exists on the drivers
38417c478bd9Sstevel@tonic-gate  * property list it updates, else it creates it.
38427c478bd9Sstevel@tonic-gate  */
38437c478bd9Sstevel@tonic-gate int
38447c478bd9Sstevel@tonic-gate ddi_prop_update_int(dev_t match_dev, dev_info_t *dip,
38457c478bd9Sstevel@tonic-gate     char *name, int data)
38467c478bd9Sstevel@tonic-gate {
38477c478bd9Sstevel@tonic-gate 	return (ddi_prop_update_common(match_dev, dip, DDI_PROP_TYPE_INT,
38487c478bd9Sstevel@tonic-gate 	    name, &data, 1, ddi_prop_fm_encode_ints));
38497c478bd9Sstevel@tonic-gate }
38507c478bd9Sstevel@tonic-gate 
38517c478bd9Sstevel@tonic-gate /*
38527c478bd9Sstevel@tonic-gate  * Update a single 64 bit integer property.
38537c478bd9Sstevel@tonic-gate  * Update the driver property list if it exists, else create it.
38547c478bd9Sstevel@tonic-gate  */
38557c478bd9Sstevel@tonic-gate int
38567c478bd9Sstevel@tonic-gate ddi_prop_update_int64(dev_t match_dev, dev_info_t *dip,
38577c478bd9Sstevel@tonic-gate     char *name, int64_t data)
38587c478bd9Sstevel@tonic-gate {
38597c478bd9Sstevel@tonic-gate 	return (ddi_prop_update_common(match_dev, dip, DDI_PROP_TYPE_INT64,
38607c478bd9Sstevel@tonic-gate 	    name, &data, 1, ddi_prop_fm_encode_int64));
38617c478bd9Sstevel@tonic-gate }
38627c478bd9Sstevel@tonic-gate 
38637c478bd9Sstevel@tonic-gate int
38647c478bd9Sstevel@tonic-gate e_ddi_prop_update_int(dev_t match_dev, dev_info_t *dip,
38657c478bd9Sstevel@tonic-gate     char *name, int data)
38667c478bd9Sstevel@tonic-gate {
38677c478bd9Sstevel@tonic-gate 	return (ddi_prop_update_common(match_dev, dip,
38687c478bd9Sstevel@tonic-gate 	    DDI_PROP_SYSTEM_DEF | DDI_PROP_TYPE_INT,
38697c478bd9Sstevel@tonic-gate 	    name, &data, 1, ddi_prop_fm_encode_ints));
38707c478bd9Sstevel@tonic-gate }
38717c478bd9Sstevel@tonic-gate 
38727c478bd9Sstevel@tonic-gate int
38737c478bd9Sstevel@tonic-gate e_ddi_prop_update_int64(dev_t match_dev, dev_info_t *dip,
38747c478bd9Sstevel@tonic-gate     char *name, int64_t data)
38757c478bd9Sstevel@tonic-gate {
38767c478bd9Sstevel@tonic-gate 	return (ddi_prop_update_common(match_dev, dip,
38777c478bd9Sstevel@tonic-gate 	    DDI_PROP_SYSTEM_DEF | DDI_PROP_TYPE_INT64,
38787c478bd9Sstevel@tonic-gate 	    name, &data, 1, ddi_prop_fm_encode_int64));
38797c478bd9Sstevel@tonic-gate }
38807c478bd9Sstevel@tonic-gate 
38817c478bd9Sstevel@tonic-gate /*
38827c478bd9Sstevel@tonic-gate  * Update an array of integer property.  If the property exists on the drivers
38837c478bd9Sstevel@tonic-gate  * property list it updates, else it creates it.
38847c478bd9Sstevel@tonic-gate  */
38857c478bd9Sstevel@tonic-gate int
38867c478bd9Sstevel@tonic-gate ddi_prop_update_int_array(dev_t match_dev, dev_info_t *dip,
38877c478bd9Sstevel@tonic-gate     char *name, int *data, uint_t nelements)
38887c478bd9Sstevel@tonic-gate {
38897c478bd9Sstevel@tonic-gate 	return (ddi_prop_update_common(match_dev, dip, DDI_PROP_TYPE_INT,
38907c478bd9Sstevel@tonic-gate 	    name, data, nelements, ddi_prop_fm_encode_ints));
38917c478bd9Sstevel@tonic-gate }
38927c478bd9Sstevel@tonic-gate 
38937c478bd9Sstevel@tonic-gate /*
38947c478bd9Sstevel@tonic-gate  * Update an array of 64 bit integer properties.
38957c478bd9Sstevel@tonic-gate  * Update the driver property list if it exists, else create it.
38967c478bd9Sstevel@tonic-gate  */
38977c478bd9Sstevel@tonic-gate int
38987c478bd9Sstevel@tonic-gate ddi_prop_update_int64_array(dev_t match_dev, dev_info_t *dip,
38997c478bd9Sstevel@tonic-gate     char *name, int64_t *data, uint_t nelements)
39007c478bd9Sstevel@tonic-gate {
39017c478bd9Sstevel@tonic-gate 	return (ddi_prop_update_common(match_dev, dip, DDI_PROP_TYPE_INT64,
39027c478bd9Sstevel@tonic-gate 	    name, data, nelements, ddi_prop_fm_encode_int64));
39037c478bd9Sstevel@tonic-gate }
39047c478bd9Sstevel@tonic-gate 
39057c478bd9Sstevel@tonic-gate int
39067c478bd9Sstevel@tonic-gate e_ddi_prop_update_int64_array(dev_t match_dev, dev_info_t *dip,
39077c478bd9Sstevel@tonic-gate     char *name, int64_t *data, uint_t nelements)
39087c478bd9Sstevel@tonic-gate {
39097c478bd9Sstevel@tonic-gate 	return (ddi_prop_update_common(match_dev, dip,
39107c478bd9Sstevel@tonic-gate 	    DDI_PROP_SYSTEM_DEF | DDI_PROP_TYPE_INT64,
39117c478bd9Sstevel@tonic-gate 	    name, data, nelements, ddi_prop_fm_encode_int64));
39127c478bd9Sstevel@tonic-gate }
39137c478bd9Sstevel@tonic-gate 
39147c478bd9Sstevel@tonic-gate int
39157c478bd9Sstevel@tonic-gate e_ddi_prop_update_int_array(dev_t match_dev, dev_info_t *dip,
39167c478bd9Sstevel@tonic-gate     char *name, int *data, uint_t nelements)
39177c478bd9Sstevel@tonic-gate {
39187c478bd9Sstevel@tonic-gate 	return (ddi_prop_update_common(match_dev, dip,
39197c478bd9Sstevel@tonic-gate 	    DDI_PROP_SYSTEM_DEF | DDI_PROP_TYPE_INT,
39207c478bd9Sstevel@tonic-gate 	    name, data, nelements, ddi_prop_fm_encode_ints));
39217c478bd9Sstevel@tonic-gate }
39227c478bd9Sstevel@tonic-gate 
39237c478bd9Sstevel@tonic-gate /*
39247c478bd9Sstevel@tonic-gate  * Get a single string property.
39257c478bd9Sstevel@tonic-gate  */
39267c478bd9Sstevel@tonic-gate int
39277c478bd9Sstevel@tonic-gate ddi_prop_lookup_string(dev_t match_dev, dev_info_t *dip, uint_t flags,
39287c478bd9Sstevel@tonic-gate     char *name, char **data)
39297c478bd9Sstevel@tonic-gate {
39307c478bd9Sstevel@tonic-gate 	uint_t x;
39317c478bd9Sstevel@tonic-gate 
39327c478bd9Sstevel@tonic-gate 	if (flags & ~(DDI_PROP_DONTPASS | DDI_PROP_NOTPROM |
393365cf7c95SVikram Hegde 	    LDI_DEV_T_ANY | DDI_UNBND_DLPI2 | DDI_PROP_ROOTNEX_GLOBAL)) {
39347c478bd9Sstevel@tonic-gate #ifdef DEBUG
39357c478bd9Sstevel@tonic-gate 		if (dip != NULL) {
39367c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s: invalid flag 0x%x "
39377c478bd9Sstevel@tonic-gate 			    "(prop = %s, node = %s%d); invalid bits ignored",
39387c478bd9Sstevel@tonic-gate 			    "ddi_prop_lookup_string", flags, name,
39397c478bd9Sstevel@tonic-gate 			    ddi_driver_name(dip), ddi_get_instance(dip));
39407c478bd9Sstevel@tonic-gate 		}
39417c478bd9Sstevel@tonic-gate #endif /* DEBUG */
39427c478bd9Sstevel@tonic-gate 		flags &= DDI_PROP_DONTPASS | DDI_PROP_NOTPROM |
39437c478bd9Sstevel@tonic-gate 		    LDI_DEV_T_ANY | DDI_UNBND_DLPI2;
39447c478bd9Sstevel@tonic-gate 	}
39457c478bd9Sstevel@tonic-gate 
39467c478bd9Sstevel@tonic-gate 	return (ddi_prop_lookup_common(match_dev, dip,
39477c478bd9Sstevel@tonic-gate 	    (flags | DDI_PROP_TYPE_STRING), name, data,
39487c478bd9Sstevel@tonic-gate 	    &x, ddi_prop_fm_decode_string));
39497c478bd9Sstevel@tonic-gate }
39507c478bd9Sstevel@tonic-gate 
39517c478bd9Sstevel@tonic-gate /*
39527c478bd9Sstevel@tonic-gate  * Get an array of strings property.
39537c478bd9Sstevel@tonic-gate  */
39547c478bd9Sstevel@tonic-gate int
39557c478bd9Sstevel@tonic-gate ddi_prop_lookup_string_array(dev_t match_dev, dev_info_t *dip, uint_t flags,
39567c478bd9Sstevel@tonic-gate     char *name, char ***data, uint_t *nelements)
39577c478bd9Sstevel@tonic-gate {
39587c478bd9Sstevel@tonic-gate 	if (flags & ~(DDI_PROP_DONTPASS | DDI_PROP_NOTPROM |
395965cf7c95SVikram Hegde 	    LDI_DEV_T_ANY | DDI_UNBND_DLPI2 | DDI_PROP_ROOTNEX_GLOBAL)) {
39607c478bd9Sstevel@tonic-gate #ifdef DEBUG
39617c478bd9Sstevel@tonic-gate 		if (dip != NULL) {
39627c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "ddi_prop_lookup_string_array: "
39637c478bd9Sstevel@tonic-gate 			    "invalid flag 0x%x (prop = %s, node = %s%d)",
39647c478bd9Sstevel@tonic-gate 			    flags, name, ddi_driver_name(dip),
39657c478bd9Sstevel@tonic-gate 			    ddi_get_instance(dip));
39667c478bd9Sstevel@tonic-gate 		}
39677c478bd9Sstevel@tonic-gate #endif /* DEBUG */
39687c478bd9Sstevel@tonic-gate 		flags &= DDI_PROP_DONTPASS | DDI_PROP_NOTPROM |
39697c478bd9Sstevel@tonic-gate 		    LDI_DEV_T_ANY | DDI_UNBND_DLPI2;
39707c478bd9Sstevel@tonic-gate 	}
39717c478bd9Sstevel@tonic-gate 
39727c478bd9Sstevel@tonic-gate 	return (ddi_prop_lookup_common(match_dev, dip,
39737c478bd9Sstevel@tonic-gate 	    (flags | DDI_PROP_TYPE_STRING), name, data,
39747c478bd9Sstevel@tonic-gate 	    nelements, ddi_prop_fm_decode_strings));
39757c478bd9Sstevel@tonic-gate }
39767c478bd9Sstevel@tonic-gate 
39777c478bd9Sstevel@tonic-gate /*
39787c478bd9Sstevel@tonic-gate  * Update a single string property.
39797c478bd9Sstevel@tonic-gate  */
39807c478bd9Sstevel@tonic-gate int
39817c478bd9Sstevel@tonic-gate ddi_prop_update_string(dev_t match_dev, dev_info_t *dip,
39827c478bd9Sstevel@tonic-gate     char *name, char *data)
39837c478bd9Sstevel@tonic-gate {
39847c478bd9Sstevel@tonic-gate 	return (ddi_prop_update_common(match_dev, dip,
39857c478bd9Sstevel@tonic-gate 	    DDI_PROP_TYPE_STRING, name, &data, 1,
39867c478bd9Sstevel@tonic-gate 	    ddi_prop_fm_encode_string));
39877c478bd9Sstevel@tonic-gate }
39887c478bd9Sstevel@tonic-gate 
39897c478bd9Sstevel@tonic-gate int
39907c478bd9Sstevel@tonic-gate e_ddi_prop_update_string(dev_t match_dev, dev_info_t *dip,
39917c478bd9Sstevel@tonic-gate     char *name, char *data)
39927c478bd9Sstevel@tonic-gate {
39937c478bd9Sstevel@tonic-gate 	return (ddi_prop_update_common(match_dev, dip,
39947c478bd9Sstevel@tonic-gate 	    DDI_PROP_SYSTEM_DEF | DDI_PROP_TYPE_STRING,
39957c478bd9Sstevel@tonic-gate 	    name, &data, 1, ddi_prop_fm_encode_string));
39967c478bd9Sstevel@tonic-gate }
39977c478bd9Sstevel@tonic-gate 
39987c478bd9Sstevel@tonic-gate 
39997c478bd9Sstevel@tonic-gate /*
40007c478bd9Sstevel@tonic-gate  * Update an array of strings property.
40017c478bd9Sstevel@tonic-gate  */
40027c478bd9Sstevel@tonic-gate int
40037c478bd9Sstevel@tonic-gate ddi_prop_update_string_array(dev_t match_dev, dev_info_t *dip,
40047c478bd9Sstevel@tonic-gate     char *name, char **data, uint_t nelements)
40057c478bd9Sstevel@tonic-gate {
40067c478bd9Sstevel@tonic-gate 	return (ddi_prop_update_common(match_dev, dip,
40077c478bd9Sstevel@tonic-gate 	    DDI_PROP_TYPE_STRING, name, data, nelements,
40087c478bd9Sstevel@tonic-gate 	    ddi_prop_fm_encode_strings));
40097c478bd9Sstevel@tonic-gate }
40107c478bd9Sstevel@tonic-gate 
40117c478bd9Sstevel@tonic-gate int
40127c478bd9Sstevel@tonic-gate e_ddi_prop_update_string_array(dev_t match_dev, dev_info_t *dip,
40137c478bd9Sstevel@tonic-gate     char *name, char **data, uint_t nelements)
40147c478bd9Sstevel@tonic-gate {
40157c478bd9Sstevel@tonic-gate 	return (ddi_prop_update_common(match_dev, dip,
40167c478bd9Sstevel@tonic-gate 	    DDI_PROP_SYSTEM_DEF | DDI_PROP_TYPE_STRING,
40177c478bd9Sstevel@tonic-gate 	    name, data, nelements,
40187c478bd9Sstevel@tonic-gate 	    ddi_prop_fm_encode_strings));
40197c478bd9Sstevel@tonic-gate }
40207c478bd9Sstevel@tonic-gate 
40217c478bd9Sstevel@tonic-gate 
40227c478bd9Sstevel@tonic-gate /*
40237c478bd9Sstevel@tonic-gate  * Get an array of bytes property.
40247c478bd9Sstevel@tonic-gate  */
40257c478bd9Sstevel@tonic-gate int
40267c478bd9Sstevel@tonic-gate ddi_prop_lookup_byte_array(dev_t match_dev, dev_info_t *dip, uint_t flags,
40277c478bd9Sstevel@tonic-gate     char *name, uchar_t **data, uint_t *nelements)
40287c478bd9Sstevel@tonic-gate {
40297c478bd9Sstevel@tonic-gate 	if (flags & ~(DDI_PROP_DONTPASS | DDI_PROP_NOTPROM |
403065cf7c95SVikram Hegde 	    LDI_DEV_T_ANY | DDI_UNBND_DLPI2 | DDI_PROP_ROOTNEX_GLOBAL)) {
40317c478bd9Sstevel@tonic-gate #ifdef DEBUG
40327c478bd9Sstevel@tonic-gate 		if (dip != NULL) {
40337c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "ddi_prop_lookup_byte_array: "
40347c478bd9Sstevel@tonic-gate 			    " invalid flag 0x%x (prop = %s, node = %s%d)",
40357c478bd9Sstevel@tonic-gate 			    flags, name, ddi_driver_name(dip),
40367c478bd9Sstevel@tonic-gate 			    ddi_get_instance(dip));
40377c478bd9Sstevel@tonic-gate 		}
40387c478bd9Sstevel@tonic-gate #endif /* DEBUG */
40397c478bd9Sstevel@tonic-gate 		flags &= DDI_PROP_DONTPASS | DDI_PROP_NOTPROM |
40407c478bd9Sstevel@tonic-gate 		    LDI_DEV_T_ANY | DDI_UNBND_DLPI2;
40417c478bd9Sstevel@tonic-gate 	}
40427c478bd9Sstevel@tonic-gate 
40437c478bd9Sstevel@tonic-gate 	return (ddi_prop_lookup_common(match_dev, dip,
40447c478bd9Sstevel@tonic-gate 	    (flags | DDI_PROP_TYPE_BYTE), name, data,
40457c478bd9Sstevel@tonic-gate 	    nelements, ddi_prop_fm_decode_bytes));
40467c478bd9Sstevel@tonic-gate }
40477c478bd9Sstevel@tonic-gate 
40487c478bd9Sstevel@tonic-gate /*
40497c478bd9Sstevel@tonic-gate  * Update an array of bytes property.
40507c478bd9Sstevel@tonic-gate  */
40517c478bd9Sstevel@tonic-gate int
40527c478bd9Sstevel@tonic-gate ddi_prop_update_byte_array(dev_t match_dev, dev_info_t *dip,
40537c478bd9Sstevel@tonic-gate     char *name, uchar_t *data, uint_t nelements)
40547c478bd9Sstevel@tonic-gate {
40557c478bd9Sstevel@tonic-gate 	if (nelements == 0)
40567c478bd9Sstevel@tonic-gate 		return (DDI_PROP_INVAL_ARG);
40577c478bd9Sstevel@tonic-gate 
40587c478bd9Sstevel@tonic-gate 	return (ddi_prop_update_common(match_dev, dip, DDI_PROP_TYPE_BYTE,
40597c478bd9Sstevel@tonic-gate 	    name, data, nelements, ddi_prop_fm_encode_bytes));
40607c478bd9Sstevel@tonic-gate }
40617c478bd9Sstevel@tonic-gate 
40627c478bd9Sstevel@tonic-gate 
40637c478bd9Sstevel@tonic-gate int
40647c478bd9Sstevel@tonic-gate e_ddi_prop_update_byte_array(dev_t match_dev, dev_info_t *dip,
40657c478bd9Sstevel@tonic-gate     char *name, uchar_t *data, uint_t nelements)
40667c478bd9Sstevel@tonic-gate {
40677c478bd9Sstevel@tonic-gate 	if (nelements == 0)
40687c478bd9Sstevel@tonic-gate 		return (DDI_PROP_INVAL_ARG);
40697c478bd9Sstevel@tonic-gate 
40707c478bd9Sstevel@tonic-gate 	return (ddi_prop_update_common(match_dev, dip,
40717c478bd9Sstevel@tonic-gate 	    DDI_PROP_SYSTEM_DEF | DDI_PROP_TYPE_BYTE,
40727c478bd9Sstevel@tonic-gate 	    name, data, nelements, ddi_prop_fm_encode_bytes));
40737c478bd9Sstevel@tonic-gate }
40747c478bd9Sstevel@tonic-gate 
40757c478bd9Sstevel@tonic-gate 
40767c478bd9Sstevel@tonic-gate /*
40777c478bd9Sstevel@tonic-gate  * ddi_prop_remove_common:	Undefine a managed property:
40787c478bd9Sstevel@tonic-gate  *			Input dev_t must match dev_t when defined.
40797c478bd9Sstevel@tonic-gate  *			Returns DDI_PROP_NOT_FOUND, possibly.
40807c478bd9Sstevel@tonic-gate  *			DDI_PROP_INVAL_ARG is also possible if dev is
40817c478bd9Sstevel@tonic-gate  *			DDI_DEV_T_ANY or incoming name is the NULL string.
40827c478bd9Sstevel@tonic-gate  */
40837c478bd9Sstevel@tonic-gate int
40847c478bd9Sstevel@tonic-gate ddi_prop_remove_common(dev_t dev, dev_info_t *dip, char *name, int flag)
40857c478bd9Sstevel@tonic-gate {
40867c478bd9Sstevel@tonic-gate 	ddi_prop_t	**list_head = &(DEVI(dip)->devi_drv_prop_ptr);
40877c478bd9Sstevel@tonic-gate 	ddi_prop_t	*propp;
40887c478bd9Sstevel@tonic-gate 	ddi_prop_t	*lastpropp = NULL;
40897c478bd9Sstevel@tonic-gate 
40907c478bd9Sstevel@tonic-gate 	if ((dev == DDI_DEV_T_ANY) || (name == (char *)0) ||
40917c478bd9Sstevel@tonic-gate 	    (strlen(name) == 0)) {
40927c478bd9Sstevel@tonic-gate 		return (DDI_PROP_INVAL_ARG);
40937c478bd9Sstevel@tonic-gate 	}
40947c478bd9Sstevel@tonic-gate 
40957c478bd9Sstevel@tonic-gate 	if (flag & DDI_PROP_SYSTEM_DEF)
40967c478bd9Sstevel@tonic-gate 		list_head = &(DEVI(dip)->devi_sys_prop_ptr);
40977c478bd9Sstevel@tonic-gate 	else if (flag & DDI_PROP_HW_DEF)
40987c478bd9Sstevel@tonic-gate 		list_head = &(DEVI(dip)->devi_hw_prop_ptr);
40997c478bd9Sstevel@tonic-gate 
41007c478bd9Sstevel@tonic-gate 	mutex_enter(&(DEVI(dip)->devi_lock));
41017c478bd9Sstevel@tonic-gate 
41027c478bd9Sstevel@tonic-gate 	for (propp = *list_head; propp != NULL; propp = propp->prop_next)  {
41032cd7878fScth 		if (DDI_STRSAME(propp->prop_name, name) &&
41047c478bd9Sstevel@tonic-gate 		    (dev == propp->prop_dev)) {
41057c478bd9Sstevel@tonic-gate 			/*
41067c478bd9Sstevel@tonic-gate 			 * Unlink this propp allowing for it to
41077c478bd9Sstevel@tonic-gate 			 * be first in the list:
41087c478bd9Sstevel@tonic-gate 			 */
41097c478bd9Sstevel@tonic-gate 
41107c478bd9Sstevel@tonic-gate 			if (lastpropp == NULL)
41117c478bd9Sstevel@tonic-gate 				*list_head = propp->prop_next;
41127c478bd9Sstevel@tonic-gate 			else
41137c478bd9Sstevel@tonic-gate 				lastpropp->prop_next = propp->prop_next;
41147c478bd9Sstevel@tonic-gate 
41157c478bd9Sstevel@tonic-gate 			mutex_exit(&(DEVI(dip)->devi_lock));
41167c478bd9Sstevel@tonic-gate 
41177c478bd9Sstevel@tonic-gate 			/*
41187c478bd9Sstevel@tonic-gate 			 * Free memory and return...
41197c478bd9Sstevel@tonic-gate 			 */
41207c478bd9Sstevel@tonic-gate 			kmem_free(propp->prop_name,
41217c478bd9Sstevel@tonic-gate 			    strlen(propp->prop_name) + 1);
41227c478bd9Sstevel@tonic-gate 			if (propp->prop_len != 0)
41237c478bd9Sstevel@tonic-gate 				kmem_free(propp->prop_val, propp->prop_len);
41247c478bd9Sstevel@tonic-gate 			kmem_free(propp, sizeof (ddi_prop_t));
41257c478bd9Sstevel@tonic-gate 			return (DDI_PROP_SUCCESS);
41267c478bd9Sstevel@tonic-gate 		}
41277c478bd9Sstevel@tonic-gate 		lastpropp = propp;
41287c478bd9Sstevel@tonic-gate 	}
41297c478bd9Sstevel@tonic-gate 	mutex_exit(&(DEVI(dip)->devi_lock));
41307c478bd9Sstevel@tonic-gate 	return (DDI_PROP_NOT_FOUND);
41317c478bd9Sstevel@tonic-gate }
41327c478bd9Sstevel@tonic-gate 
41337c478bd9Sstevel@tonic-gate int
41347c478bd9Sstevel@tonic-gate ddi_prop_remove(dev_t dev, dev_info_t *dip, char *name)
41357c478bd9Sstevel@tonic-gate {
41367c478bd9Sstevel@tonic-gate 	return (ddi_prop_remove_common(dev, dip, name, 0));
41377c478bd9Sstevel@tonic-gate }
41387c478bd9Sstevel@tonic-gate 
41397c478bd9Sstevel@tonic-gate int
41407c478bd9Sstevel@tonic-gate e_ddi_prop_remove(dev_t dev, dev_info_t *dip, char *name)
41417c478bd9Sstevel@tonic-gate {
41427c478bd9Sstevel@tonic-gate 	return (ddi_prop_remove_common(dev, dip, name, DDI_PROP_SYSTEM_DEF));
41437c478bd9Sstevel@tonic-gate }
41447c478bd9Sstevel@tonic-gate 
41457c478bd9Sstevel@tonic-gate /*
41467c478bd9Sstevel@tonic-gate  * e_ddi_prop_list_delete: remove a list of properties
41477c478bd9Sstevel@tonic-gate  *	Note that the caller needs to provide the required protection
41487c478bd9Sstevel@tonic-gate  *	(eg. devi_lock if these properties are still attached to a devi)
41497c478bd9Sstevel@tonic-gate  */
41507c478bd9Sstevel@tonic-gate void
41517c478bd9Sstevel@tonic-gate e_ddi_prop_list_delete(ddi_prop_t *props)
41527c478bd9Sstevel@tonic-gate {
41537c478bd9Sstevel@tonic-gate 	i_ddi_prop_list_delete(props);
41547c478bd9Sstevel@tonic-gate }
41557c478bd9Sstevel@tonic-gate 
41567c478bd9Sstevel@tonic-gate /*
41577c478bd9Sstevel@tonic-gate  * ddi_prop_remove_all_common:
41587c478bd9Sstevel@tonic-gate  *	Used before unloading a driver to remove
41597c478bd9Sstevel@tonic-gate  *	all properties. (undefines all dev_t's props.)
41607c478bd9Sstevel@tonic-gate  *	Also removes `explicitly undefined' props.
41617c478bd9Sstevel@tonic-gate  *	No errors possible.
41627c478bd9Sstevel@tonic-gate  */
41637c478bd9Sstevel@tonic-gate void
41647c478bd9Sstevel@tonic-gate ddi_prop_remove_all_common(dev_info_t *dip, int flag)
41657c478bd9Sstevel@tonic-gate {
41667c478bd9Sstevel@tonic-gate 	ddi_prop_t	**list_head;
41677c478bd9Sstevel@tonic-gate 
41687c478bd9Sstevel@tonic-gate 	mutex_enter(&(DEVI(dip)->devi_lock));
41697c478bd9Sstevel@tonic-gate 	if (flag & DDI_PROP_SYSTEM_DEF) {
41707c478bd9Sstevel@tonic-gate 		list_head = &(DEVI(dip)->devi_sys_prop_ptr);
41717c478bd9Sstevel@tonic-gate 	} else if (flag & DDI_PROP_HW_DEF) {
41727c478bd9Sstevel@tonic-gate 		list_head = &(DEVI(dip)->devi_hw_prop_ptr);
41737c478bd9Sstevel@tonic-gate 	} else {
41747c478bd9Sstevel@tonic-gate 		list_head = &(DEVI(dip)->devi_drv_prop_ptr);
41757c478bd9Sstevel@tonic-gate 	}
41767c478bd9Sstevel@tonic-gate 	i_ddi_prop_list_delete(*list_head);
41777c478bd9Sstevel@tonic-gate 	*list_head = NULL;
41787c478bd9Sstevel@tonic-gate 	mutex_exit(&(DEVI(dip)->devi_lock));
41797c478bd9Sstevel@tonic-gate }
41807c478bd9Sstevel@tonic-gate 
41817c478bd9Sstevel@tonic-gate 
41827c478bd9Sstevel@tonic-gate /*
41837c478bd9Sstevel@tonic-gate  * ddi_prop_remove_all:		Remove all driver prop definitions.
41847c478bd9Sstevel@tonic-gate  */
41857c478bd9Sstevel@tonic-gate 
41867c478bd9Sstevel@tonic-gate void
41877c478bd9Sstevel@tonic-gate ddi_prop_remove_all(dev_info_t *dip)
41887c478bd9Sstevel@tonic-gate {
4189b9ccdc5aScth 	i_ddi_prop_dyn_driver_set(dip, NULL);
41907c478bd9Sstevel@tonic-gate 	ddi_prop_remove_all_common(dip, 0);
41917c478bd9Sstevel@tonic-gate }
41927c478bd9Sstevel@tonic-gate 
41937c478bd9Sstevel@tonic-gate /*
41947c478bd9Sstevel@tonic-gate  * e_ddi_prop_remove_all:	Remove all system prop definitions.
41957c478bd9Sstevel@tonic-gate  */
41967c478bd9Sstevel@tonic-gate 
41977c478bd9Sstevel@tonic-gate void
41987c478bd9Sstevel@tonic-gate e_ddi_prop_remove_all(dev_info_t *dip)
41997c478bd9Sstevel@tonic-gate {
42007c478bd9Sstevel@tonic-gate 	ddi_prop_remove_all_common(dip, (int)DDI_PROP_SYSTEM_DEF);
42017c478bd9Sstevel@tonic-gate }
42027c478bd9Sstevel@tonic-gate 
42037c478bd9Sstevel@tonic-gate 
42047c478bd9Sstevel@tonic-gate /*
42057c478bd9Sstevel@tonic-gate  * ddi_prop_undefine:	Explicitly undefine a property.  Property
42067c478bd9Sstevel@tonic-gate  *			searches which match this property return
42077c478bd9Sstevel@tonic-gate  *			the error code DDI_PROP_UNDEFINED.
42087c478bd9Sstevel@tonic-gate  *
42097c478bd9Sstevel@tonic-gate  *			Use ddi_prop_remove to negate effect of
42107c478bd9Sstevel@tonic-gate  *			ddi_prop_undefine
42117c478bd9Sstevel@tonic-gate  *
42127c478bd9Sstevel@tonic-gate  *			See above for error returns.
42137c478bd9Sstevel@tonic-gate  */
42147c478bd9Sstevel@tonic-gate 
42157c478bd9Sstevel@tonic-gate int
42167c478bd9Sstevel@tonic-gate ddi_prop_undefine(dev_t dev, dev_info_t *dip, int flag, char *name)
42177c478bd9Sstevel@tonic-gate {
42187c478bd9Sstevel@tonic-gate 	if (!(flag & DDI_PROP_CANSLEEP))
42197c478bd9Sstevel@tonic-gate 		flag |= DDI_PROP_DONTSLEEP;
4220b9ccdc5aScth 	flag |= DDI_PROP_STACK_CREATE | DDI_PROP_UNDEF_IT | DDI_PROP_TYPE_ANY;
4221b9ccdc5aScth 	return (ddi_prop_update_common(dev, dip, flag,
4222b9ccdc5aScth 	    name, NULL, 0, ddi_prop_fm_encode_bytes));
42237c478bd9Sstevel@tonic-gate }
42247c478bd9Sstevel@tonic-gate 
42257c478bd9Sstevel@tonic-gate int
42267c478bd9Sstevel@tonic-gate e_ddi_prop_undefine(dev_t dev, dev_info_t *dip, int flag, char *name)
42277c478bd9Sstevel@tonic-gate {
42287c478bd9Sstevel@tonic-gate 	if (!(flag & DDI_PROP_CANSLEEP))
42297c478bd9Sstevel@tonic-gate 		flag |= DDI_PROP_DONTSLEEP;
4230b9ccdc5aScth 	flag |= DDI_PROP_SYSTEM_DEF | DDI_PROP_STACK_CREATE |
4231b9ccdc5aScth 	    DDI_PROP_UNDEF_IT | DDI_PROP_TYPE_ANY;
4232b9ccdc5aScth 	return (ddi_prop_update_common(dev, dip, flag,
42337c478bd9Sstevel@tonic-gate 	    name, NULL, 0, ddi_prop_fm_encode_bytes));
42347c478bd9Sstevel@tonic-gate }
42357c478bd9Sstevel@tonic-gate 
42367c478bd9Sstevel@tonic-gate /*
4237b9ccdc5aScth  * Support for gathering dynamic properties in devinfo snapshot.
4238b9ccdc5aScth  */
4239b9ccdc5aScth void
4240b9ccdc5aScth i_ddi_prop_dyn_driver_set(dev_info_t *dip, i_ddi_prop_dyn_t *dp)
4241b9ccdc5aScth {
4242b9ccdc5aScth 	DEVI(dip)->devi_prop_dyn_driver = dp;
4243b9ccdc5aScth }
4244b9ccdc5aScth 
4245b9ccdc5aScth i_ddi_prop_dyn_t *
4246b9ccdc5aScth i_ddi_prop_dyn_driver_get(dev_info_t *dip)
4247b9ccdc5aScth {
4248b9ccdc5aScth 	return (DEVI(dip)->devi_prop_dyn_driver);
4249b9ccdc5aScth }
4250b9ccdc5aScth 
4251b9ccdc5aScth void
4252b9ccdc5aScth i_ddi_prop_dyn_parent_set(dev_info_t *dip, i_ddi_prop_dyn_t *dp)
4253b9ccdc5aScth {
4254b9ccdc5aScth 	DEVI(dip)->devi_prop_dyn_parent = dp;
4255b9ccdc5aScth }
4256b9ccdc5aScth 
4257b9ccdc5aScth i_ddi_prop_dyn_t *
4258b9ccdc5aScth i_ddi_prop_dyn_parent_get(dev_info_t *dip)
4259b9ccdc5aScth {
4260b9ccdc5aScth 	return (DEVI(dip)->devi_prop_dyn_parent);
4261b9ccdc5aScth }
4262b9ccdc5aScth 
4263b9ccdc5aScth void
4264b9ccdc5aScth i_ddi_prop_dyn_cache_invalidate(dev_info_t *dip, i_ddi_prop_dyn_t *dp)
4265b9ccdc5aScth {
4266b9ccdc5aScth 	/* for now we invalidate the entire cached snapshot */
4267b9ccdc5aScth 	if (dip && dp)
42684c06356bSdh142964 		i_ddi_di_cache_invalidate();
4269b9ccdc5aScth }
4270b9ccdc5aScth 
4271b9ccdc5aScth /* ARGSUSED */
4272b9ccdc5aScth void
4273b9ccdc5aScth ddi_prop_cache_invalidate(dev_t dev, dev_info_t *dip, char *name, int flags)
4274b9ccdc5aScth {
4275b9ccdc5aScth 	/* for now we invalidate the entire cached snapshot */
42764c06356bSdh142964 	i_ddi_di_cache_invalidate();
4277b9ccdc5aScth }
4278b9ccdc5aScth 
4279b9ccdc5aScth 
4280b9ccdc5aScth /*
42817c478bd9Sstevel@tonic-gate  * Code to search hardware layer (PROM), if it exists, on behalf of child.
42827c478bd9Sstevel@tonic-gate  *
42837c478bd9Sstevel@tonic-gate  * if input dip != child_dip, then call is on behalf of child
42847c478bd9Sstevel@tonic-gate  * to search PROM, do it via ddi_prop_search_common() and ascend only
42857c478bd9Sstevel@tonic-gate  * if allowed.
42867c478bd9Sstevel@tonic-gate  *
42877c478bd9Sstevel@tonic-gate  * if input dip == ch_dip (child_dip), call is on behalf of root driver,
42887c478bd9Sstevel@tonic-gate  * to search for PROM defined props only.
42897c478bd9Sstevel@tonic-gate  *
42907c478bd9Sstevel@tonic-gate  * Note that the PROM search is done only if the requested dev
42917c478bd9Sstevel@tonic-gate  * is either DDI_DEV_T_ANY or DDI_DEV_T_NONE. PROM properties
42927c478bd9Sstevel@tonic-gate  * have no associated dev, thus are automatically associated with
42937c478bd9Sstevel@tonic-gate  * DDI_DEV_T_NONE.
42947c478bd9Sstevel@tonic-gate  *
42957c478bd9Sstevel@tonic-gate  * Modifying flag DDI_PROP_NOTPROM inhibits the search in the h/w layer.
42967c478bd9Sstevel@tonic-gate  *
42977c478bd9Sstevel@tonic-gate  * Returns DDI_PROP_FOUND_1275 if found to indicate to framework
42987c478bd9Sstevel@tonic-gate  * that the property resides in the prom.
42997c478bd9Sstevel@tonic-gate  */
43007c478bd9Sstevel@tonic-gate int
43017c478bd9Sstevel@tonic-gate impl_ddi_bus_prop_op(dev_t dev, dev_info_t *dip, dev_info_t *ch_dip,
43027c478bd9Sstevel@tonic-gate     ddi_prop_op_t prop_op, int mod_flags,
43037c478bd9Sstevel@tonic-gate     char *name, caddr_t valuep, int *lengthp)
43047c478bd9Sstevel@tonic-gate {
43057c478bd9Sstevel@tonic-gate 	int	len;
43067c478bd9Sstevel@tonic-gate 	caddr_t buffer;
43077c478bd9Sstevel@tonic-gate 
43087c478bd9Sstevel@tonic-gate 	/*
43097c478bd9Sstevel@tonic-gate 	 * If requested dev is DDI_DEV_T_NONE or DDI_DEV_T_ANY, then
43107c478bd9Sstevel@tonic-gate 	 * look in caller's PROM if it's a self identifying device...
43117c478bd9Sstevel@tonic-gate 	 *
43127c478bd9Sstevel@tonic-gate 	 * Note that this is very similar to ddi_prop_op, but we
43137c478bd9Sstevel@tonic-gate 	 * search the PROM instead of the s/w defined properties,
43147c478bd9Sstevel@tonic-gate 	 * and we are called on by the parent driver to do this for
43157c478bd9Sstevel@tonic-gate 	 * the child.
43167c478bd9Sstevel@tonic-gate 	 */
43177c478bd9Sstevel@tonic-gate 
43187c478bd9Sstevel@tonic-gate 	if (((dev == DDI_DEV_T_NONE) || (dev == DDI_DEV_T_ANY)) &&
43197c478bd9Sstevel@tonic-gate 	    ndi_dev_is_prom_node(ch_dip) &&
43207c478bd9Sstevel@tonic-gate 	    ((mod_flags & DDI_PROP_NOTPROM) == 0)) {
4321fa9e4066Sahrens 		len = prom_getproplen((pnode_t)DEVI(ch_dip)->devi_nodeid, name);
43227c478bd9Sstevel@tonic-gate 		if (len == -1) {
43237c478bd9Sstevel@tonic-gate 			return (DDI_PROP_NOT_FOUND);
43247c478bd9Sstevel@tonic-gate 		}
43257c478bd9Sstevel@tonic-gate 
43267c478bd9Sstevel@tonic-gate 		/*
43277c478bd9Sstevel@tonic-gate 		 * If exists only request, we're done
43287c478bd9Sstevel@tonic-gate 		 */
43297c478bd9Sstevel@tonic-gate 		if (prop_op == PROP_EXISTS) {
43307c478bd9Sstevel@tonic-gate 			return (DDI_PROP_FOUND_1275);
43317c478bd9Sstevel@tonic-gate 		}
43327c478bd9Sstevel@tonic-gate 
43337c478bd9Sstevel@tonic-gate 		/*
43347c478bd9Sstevel@tonic-gate 		 * If length only request or prop length == 0, get out
43357c478bd9Sstevel@tonic-gate 		 */
43367c478bd9Sstevel@tonic-gate 		if ((prop_op == PROP_LEN) || (len == 0)) {
43377c478bd9Sstevel@tonic-gate 			*lengthp = len;
43387c478bd9Sstevel@tonic-gate 			return (DDI_PROP_FOUND_1275);
43397c478bd9Sstevel@tonic-gate 		}
43407c478bd9Sstevel@tonic-gate 
43417c478bd9Sstevel@tonic-gate 		/*
43427c478bd9Sstevel@tonic-gate 		 * Allocate buffer if required... (either way `buffer'
43437c478bd9Sstevel@tonic-gate 		 * is receiving address).
43447c478bd9Sstevel@tonic-gate 		 */
43457c478bd9Sstevel@tonic-gate 
43467c478bd9Sstevel@tonic-gate 		switch (prop_op) {
43477c478bd9Sstevel@tonic-gate 
43487c478bd9Sstevel@tonic-gate 		case PROP_LEN_AND_VAL_ALLOC:
43497c478bd9Sstevel@tonic-gate 
43507c478bd9Sstevel@tonic-gate 			buffer = kmem_alloc((size_t)len,
43517c478bd9Sstevel@tonic-gate 			    mod_flags & DDI_PROP_CANSLEEP ?
43527c478bd9Sstevel@tonic-gate 			    KM_SLEEP : KM_NOSLEEP);
43537c478bd9Sstevel@tonic-gate 			if (buffer == NULL) {
43547c478bd9Sstevel@tonic-gate 				return (DDI_PROP_NO_MEMORY);
43557c478bd9Sstevel@tonic-gate 			}
43567c478bd9Sstevel@tonic-gate 			*(caddr_t *)valuep = buffer;
43577c478bd9Sstevel@tonic-gate 			break;
43587c478bd9Sstevel@tonic-gate 
43597c478bd9Sstevel@tonic-gate 		case PROP_LEN_AND_VAL_BUF:
43607c478bd9Sstevel@tonic-gate 
43617c478bd9Sstevel@tonic-gate 			if (len > (*lengthp)) {
43627c478bd9Sstevel@tonic-gate 				*lengthp = len;
43637c478bd9Sstevel@tonic-gate 				return (DDI_PROP_BUF_TOO_SMALL);
43647c478bd9Sstevel@tonic-gate 			}
43657c478bd9Sstevel@tonic-gate 
43667c478bd9Sstevel@tonic-gate 			buffer = valuep;
43677c478bd9Sstevel@tonic-gate 			break;
43687c478bd9Sstevel@tonic-gate 
43697c478bd9Sstevel@tonic-gate 		default:
43707c478bd9Sstevel@tonic-gate 			break;
43717c478bd9Sstevel@tonic-gate 		}
43727c478bd9Sstevel@tonic-gate 
43737c478bd9Sstevel@tonic-gate 		/*
43747c478bd9Sstevel@tonic-gate 		 * Call the PROM function to do the copy.
43757c478bd9Sstevel@tonic-gate 		 */
4376fa9e4066Sahrens 		(void) prom_getprop((pnode_t)DEVI(ch_dip)->devi_nodeid,
43777c478bd9Sstevel@tonic-gate 		    name, buffer);
43787c478bd9Sstevel@tonic-gate 
43797c478bd9Sstevel@tonic-gate 		*lengthp = len; /* return the actual length to the caller */
43807c478bd9Sstevel@tonic-gate 		(void) impl_fix_props(dip, ch_dip, name, len, buffer);
43817c478bd9Sstevel@tonic-gate 		return (DDI_PROP_FOUND_1275);
43827c478bd9Sstevel@tonic-gate 	}
43837c478bd9Sstevel@tonic-gate 
43847c478bd9Sstevel@tonic-gate 	return (DDI_PROP_NOT_FOUND);
43857c478bd9Sstevel@tonic-gate }
43867c478bd9Sstevel@tonic-gate 
43877c478bd9Sstevel@tonic-gate /*
43887c478bd9Sstevel@tonic-gate  * The ddi_bus_prop_op default bus nexus prop op function.
43897c478bd9Sstevel@tonic-gate  *
43907c478bd9Sstevel@tonic-gate  * Code to search hardware layer (PROM), if it exists,
43917c478bd9Sstevel@tonic-gate  * on behalf of child, then, if appropriate, ascend and check
43927c478bd9Sstevel@tonic-gate  * my own software defined properties...
43937c478bd9Sstevel@tonic-gate  */
43947c478bd9Sstevel@tonic-gate int
43957c478bd9Sstevel@tonic-gate ddi_bus_prop_op(dev_t dev, dev_info_t *dip, dev_info_t *ch_dip,
43967c478bd9Sstevel@tonic-gate     ddi_prop_op_t prop_op, int mod_flags,
43977c478bd9Sstevel@tonic-gate     char *name, caddr_t valuep, int *lengthp)
43987c478bd9Sstevel@tonic-gate {
43997c478bd9Sstevel@tonic-gate 	int	error;
44007c478bd9Sstevel@tonic-gate 
44017c478bd9Sstevel@tonic-gate 	error = impl_ddi_bus_prop_op(dev, dip, ch_dip, prop_op, mod_flags,
44027c478bd9Sstevel@tonic-gate 	    name, valuep, lengthp);
44037c478bd9Sstevel@tonic-gate 
44047c478bd9Sstevel@tonic-gate 	if (error == DDI_PROP_SUCCESS || error == DDI_PROP_FOUND_1275 ||
44057c478bd9Sstevel@tonic-gate 	    error == DDI_PROP_BUF_TOO_SMALL)
44067c478bd9Sstevel@tonic-gate 		return (error);
44077c478bd9Sstevel@tonic-gate 
44087c478bd9Sstevel@tonic-gate 	if (error == DDI_PROP_NO_MEMORY) {
44097c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, prop_no_mem_msg, name);
44107c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NO_MEMORY);
44117c478bd9Sstevel@tonic-gate 	}
44127c478bd9Sstevel@tonic-gate 
44137c478bd9Sstevel@tonic-gate 	/*
44147c478bd9Sstevel@tonic-gate 	 * Check the 'options' node as a last resort
44157c478bd9Sstevel@tonic-gate 	 */
44167c478bd9Sstevel@tonic-gate 	if ((mod_flags & DDI_PROP_DONTPASS) != 0)
44177c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
44187c478bd9Sstevel@tonic-gate 
44197c478bd9Sstevel@tonic-gate 	if (ch_dip == ddi_root_node())	{
44207c478bd9Sstevel@tonic-gate 		/*
44217c478bd9Sstevel@tonic-gate 		 * As a last resort, when we've reached
44227c478bd9Sstevel@tonic-gate 		 * the top and still haven't found the
44237c478bd9Sstevel@tonic-gate 		 * property, see if the desired property
44247c478bd9Sstevel@tonic-gate 		 * is attached to the options node.
44257c478bd9Sstevel@tonic-gate 		 *
44267c478bd9Sstevel@tonic-gate 		 * The options dip is attached right after boot.
44277c478bd9Sstevel@tonic-gate 		 */
44287c478bd9Sstevel@tonic-gate 		ASSERT(options_dip != NULL);
44297c478bd9Sstevel@tonic-gate 		/*
44307c478bd9Sstevel@tonic-gate 		 * Force the "don't pass" flag to *just* see
44317c478bd9Sstevel@tonic-gate 		 * what the options node has to offer.
44327c478bd9Sstevel@tonic-gate 		 */
44337c478bd9Sstevel@tonic-gate 		return (ddi_prop_search_common(dev, options_dip, prop_op,
44347c478bd9Sstevel@tonic-gate 		    mod_flags|DDI_PROP_DONTPASS, name, valuep,
44357c478bd9Sstevel@tonic-gate 		    (uint_t *)lengthp));
44367c478bd9Sstevel@tonic-gate 	}
44377c478bd9Sstevel@tonic-gate 
44387c478bd9Sstevel@tonic-gate 	/*
44397c478bd9Sstevel@tonic-gate 	 * Otherwise, continue search with parent's s/w defined properties...
44407c478bd9Sstevel@tonic-gate 	 * NOTE: Using `dip' in following call increments the level.
44417c478bd9Sstevel@tonic-gate 	 */
44427c478bd9Sstevel@tonic-gate 
44437c478bd9Sstevel@tonic-gate 	return (ddi_prop_search_common(dev, dip, prop_op, mod_flags,
44447c478bd9Sstevel@tonic-gate 	    name, valuep, (uint_t *)lengthp));
44457c478bd9Sstevel@tonic-gate }
44467c478bd9Sstevel@tonic-gate 
44477c478bd9Sstevel@tonic-gate /*
44487c478bd9Sstevel@tonic-gate  * External property functions used by other parts of the kernel...
44497c478bd9Sstevel@tonic-gate  */
44507c478bd9Sstevel@tonic-gate 
44517c478bd9Sstevel@tonic-gate /*
44527c478bd9Sstevel@tonic-gate  * e_ddi_getlongprop: See comments for ddi_get_longprop.
44537c478bd9Sstevel@tonic-gate  */
44547c478bd9Sstevel@tonic-gate 
44557c478bd9Sstevel@tonic-gate int
44567c478bd9Sstevel@tonic-gate e_ddi_getlongprop(dev_t dev, vtype_t type, char *name, int flags,
44577c478bd9Sstevel@tonic-gate     caddr_t valuep, int *lengthp)
44587c478bd9Sstevel@tonic-gate {
44597c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(type))
44607c478bd9Sstevel@tonic-gate 	dev_info_t *devi;
44617c478bd9Sstevel@tonic-gate 	ddi_prop_op_t prop_op = PROP_LEN_AND_VAL_ALLOC;
44627c478bd9Sstevel@tonic-gate 	int error;
44637c478bd9Sstevel@tonic-gate 
44647c478bd9Sstevel@tonic-gate 	if ((devi = e_ddi_hold_devi_by_dev(dev, 0)) == NULL)
44657c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
44667c478bd9Sstevel@tonic-gate 
44677c478bd9Sstevel@tonic-gate 	error = cdev_prop_op(dev, devi, prop_op, flags, name, valuep, lengthp);
44687c478bd9Sstevel@tonic-gate 	ddi_release_devi(devi);
44697c478bd9Sstevel@tonic-gate 	return (error);
44707c478bd9Sstevel@tonic-gate }
44717c478bd9Sstevel@tonic-gate 
44727c478bd9Sstevel@tonic-gate /*
44737c478bd9Sstevel@tonic-gate  * e_ddi_getlongprop_buf:	See comments for ddi_getlongprop_buf.
44747c478bd9Sstevel@tonic-gate  */
44757c478bd9Sstevel@tonic-gate 
44767c478bd9Sstevel@tonic-gate int
44777c478bd9Sstevel@tonic-gate e_ddi_getlongprop_buf(dev_t dev, vtype_t type, char *name, int flags,
44787c478bd9Sstevel@tonic-gate     caddr_t valuep, int *lengthp)
44797c478bd9Sstevel@tonic-gate {
44807c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(type))
44817c478bd9Sstevel@tonic-gate 	dev_info_t *devi;
44827c478bd9Sstevel@tonic-gate 	ddi_prop_op_t prop_op = PROP_LEN_AND_VAL_BUF;
44837c478bd9Sstevel@tonic-gate 	int error;
44847c478bd9Sstevel@tonic-gate 
44857c478bd9Sstevel@tonic-gate 	if ((devi = e_ddi_hold_devi_by_dev(dev, 0)) == NULL)
44867c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
44877c478bd9Sstevel@tonic-gate 
44887c478bd9Sstevel@tonic-gate 	error = cdev_prop_op(dev, devi, prop_op, flags, name, valuep, lengthp);
44897c478bd9Sstevel@tonic-gate 	ddi_release_devi(devi);
44907c478bd9Sstevel@tonic-gate 	return (error);
44917c478bd9Sstevel@tonic-gate }
44927c478bd9Sstevel@tonic-gate 
44937c478bd9Sstevel@tonic-gate /*
44947c478bd9Sstevel@tonic-gate  * e_ddi_getprop:	See comments for ddi_getprop.
44957c478bd9Sstevel@tonic-gate  */
44967c478bd9Sstevel@tonic-gate int
44977c478bd9Sstevel@tonic-gate e_ddi_getprop(dev_t dev, vtype_t type, char *name, int flags, int defvalue)
44987c478bd9Sstevel@tonic-gate {
44997c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(type))
45007c478bd9Sstevel@tonic-gate 	dev_info_t *devi;
45017c478bd9Sstevel@tonic-gate 	ddi_prop_op_t prop_op = PROP_LEN_AND_VAL_BUF;
45027c478bd9Sstevel@tonic-gate 	int	propvalue = defvalue;
45037c478bd9Sstevel@tonic-gate 	int	proplength = sizeof (int);
45047c478bd9Sstevel@tonic-gate 	int	error;
45057c478bd9Sstevel@tonic-gate 
45067c478bd9Sstevel@tonic-gate 	if ((devi = e_ddi_hold_devi_by_dev(dev, 0)) == NULL)
45077c478bd9Sstevel@tonic-gate 		return (defvalue);
45087c478bd9Sstevel@tonic-gate 
45097c478bd9Sstevel@tonic-gate 	error = cdev_prop_op(dev, devi, prop_op,
45107c478bd9Sstevel@tonic-gate 	    flags, name, (caddr_t)&propvalue, &proplength);
45117c478bd9Sstevel@tonic-gate 	ddi_release_devi(devi);
45127c478bd9Sstevel@tonic-gate 
45137c478bd9Sstevel@tonic-gate 	if ((error == DDI_PROP_SUCCESS) && (proplength == 0))
45147c478bd9Sstevel@tonic-gate 		propvalue = 1;
45157c478bd9Sstevel@tonic-gate 
45167c478bd9Sstevel@tonic-gate 	return (propvalue);
45177c478bd9Sstevel@tonic-gate }
45187c478bd9Sstevel@tonic-gate 
45197c478bd9Sstevel@tonic-gate /*
45207c478bd9Sstevel@tonic-gate  * e_ddi_getprop_int64:
45217c478bd9Sstevel@tonic-gate  *
45227c478bd9Sstevel@tonic-gate  * This is a typed interfaces, but predates typed properties. With the
45237c478bd9Sstevel@tonic-gate  * introduction of typed properties the framework tries to ensure
45247c478bd9Sstevel@tonic-gate  * consistent use of typed interfaces. This is why TYPE_INT64 is not
45257c478bd9Sstevel@tonic-gate  * part of TYPE_ANY.  E_ddi_getprop_int64 is a special case where a
45267c478bd9Sstevel@tonic-gate  * typed interface invokes legacy (non-typed) interfaces:
45277c478bd9Sstevel@tonic-gate  * cdev_prop_op(), prop_op(9E), ddi_prop_op(9F)).  In this case the
45287c478bd9Sstevel@tonic-gate  * fact that TYPE_INT64 is not part of TYPE_ANY matters.  To support
45297c478bd9Sstevel@tonic-gate  * this type of lookup as a single operation we invoke the legacy
45307c478bd9Sstevel@tonic-gate  * non-typed interfaces with the special CONSUMER_TYPED bit set. The
45317c478bd9Sstevel@tonic-gate  * framework ddi_prop_op(9F) implementation is expected to check for
45327c478bd9Sstevel@tonic-gate  * CONSUMER_TYPED and, if set, expand type bits beyond TYPE_ANY
45337c478bd9Sstevel@tonic-gate  * (currently TYPE_INT64).
45347c478bd9Sstevel@tonic-gate  */
45357c478bd9Sstevel@tonic-gate int64_t
45367c478bd9Sstevel@tonic-gate e_ddi_getprop_int64(dev_t dev, vtype_t type, char *name,
45377c478bd9Sstevel@tonic-gate     int flags, int64_t defvalue)
45387c478bd9Sstevel@tonic-gate {
45397c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(type))
45407c478bd9Sstevel@tonic-gate 	dev_info_t	*devi;
45417c478bd9Sstevel@tonic-gate 	ddi_prop_op_t	prop_op = PROP_LEN_AND_VAL_BUF;
45427c478bd9Sstevel@tonic-gate 	int64_t		propvalue = defvalue;
45437c478bd9Sstevel@tonic-gate 	int		proplength = sizeof (propvalue);
45447c478bd9Sstevel@tonic-gate 	int		error;
45457c478bd9Sstevel@tonic-gate 
45467c478bd9Sstevel@tonic-gate 	if ((devi = e_ddi_hold_devi_by_dev(dev, 0)) == NULL)
45477c478bd9Sstevel@tonic-gate 		return (defvalue);
45487c478bd9Sstevel@tonic-gate 
45497c478bd9Sstevel@tonic-gate 	error = cdev_prop_op(dev, devi, prop_op, flags |
45507c478bd9Sstevel@tonic-gate 	    DDI_PROP_CONSUMER_TYPED, name, (caddr_t)&propvalue, &proplength);
45517c478bd9Sstevel@tonic-gate 	ddi_release_devi(devi);
45527c478bd9Sstevel@tonic-gate 
45537c478bd9Sstevel@tonic-gate 	if ((error == DDI_PROP_SUCCESS) && (proplength == 0))
45547c478bd9Sstevel@tonic-gate 		propvalue = 1;
45557c478bd9Sstevel@tonic-gate 
45567c478bd9Sstevel@tonic-gate 	return (propvalue);
45577c478bd9Sstevel@tonic-gate }
45587c478bd9Sstevel@tonic-gate 
45597c478bd9Sstevel@tonic-gate /*
45607c478bd9Sstevel@tonic-gate  * e_ddi_getproplen:	See comments for ddi_getproplen.
45617c478bd9Sstevel@tonic-gate  */
45627c478bd9Sstevel@tonic-gate int
45637c478bd9Sstevel@tonic-gate e_ddi_getproplen(dev_t dev, vtype_t type, char *name, int flags, int *lengthp)
45647c478bd9Sstevel@tonic-gate {
45657c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(type))
45667c478bd9Sstevel@tonic-gate 	dev_info_t *devi;
45677c478bd9Sstevel@tonic-gate 	ddi_prop_op_t prop_op = PROP_LEN;
45687c478bd9Sstevel@tonic-gate 	int error;
45697c478bd9Sstevel@tonic-gate 
45707c478bd9Sstevel@tonic-gate 	if ((devi = e_ddi_hold_devi_by_dev(dev, 0)) == NULL)
45717c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
45727c478bd9Sstevel@tonic-gate 
45737c478bd9Sstevel@tonic-gate 	error = cdev_prop_op(dev, devi, prop_op, flags, name, NULL, lengthp);
45747c478bd9Sstevel@tonic-gate 	ddi_release_devi(devi);
45757c478bd9Sstevel@tonic-gate 	return (error);
45767c478bd9Sstevel@tonic-gate }
45777c478bd9Sstevel@tonic-gate 
45787c478bd9Sstevel@tonic-gate /*
45797c478bd9Sstevel@tonic-gate  * Routines to get at elements of the dev_info structure
45807c478bd9Sstevel@tonic-gate  */
45817c478bd9Sstevel@tonic-gate 
45827c478bd9Sstevel@tonic-gate /*
45837c478bd9Sstevel@tonic-gate  * ddi_binding_name: Return the driver binding name of the devinfo node
45847c478bd9Sstevel@tonic-gate  *		This is the name the OS used to bind the node to a driver.
45857c478bd9Sstevel@tonic-gate  */
45867c478bd9Sstevel@tonic-gate char *
45877c478bd9Sstevel@tonic-gate ddi_binding_name(dev_info_t *dip)
45887c478bd9Sstevel@tonic-gate {
45897c478bd9Sstevel@tonic-gate 	return (DEVI(dip)->devi_binding_name);
45907c478bd9Sstevel@tonic-gate }
45917c478bd9Sstevel@tonic-gate 
45927c478bd9Sstevel@tonic-gate /*
45937c478bd9Sstevel@tonic-gate  * ddi_driver_major: Return the major number of the driver that
45945c066ec2SJerry Gilliam  *	the supplied devinfo is bound to.  If not yet bound,
45955c066ec2SJerry Gilliam  *	DDI_MAJOR_T_NONE.
45965c066ec2SJerry Gilliam  *
45975c066ec2SJerry Gilliam  * When used by the driver bound to 'devi', this
45985c066ec2SJerry Gilliam  * function will reliably return the driver major number.
45995c066ec2SJerry Gilliam  * Other ways of determining the driver major number, such as
46005c066ec2SJerry Gilliam  *	major = ddi_name_to_major(ddi_get_name(devi));
46015c066ec2SJerry Gilliam  *	major = ddi_name_to_major(ddi_binding_name(devi));
46025c066ec2SJerry Gilliam  * can return a different result as the driver/alias binding
46035c066ec2SJerry Gilliam  * can change dynamically, and thus should be avoided.
46047c478bd9Sstevel@tonic-gate  */
46057c478bd9Sstevel@tonic-gate major_t
46067c478bd9Sstevel@tonic-gate ddi_driver_major(dev_info_t *devi)
46077c478bd9Sstevel@tonic-gate {
46087c478bd9Sstevel@tonic-gate 	return (DEVI(devi)->devi_major);
46097c478bd9Sstevel@tonic-gate }
46107c478bd9Sstevel@tonic-gate 
46117c478bd9Sstevel@tonic-gate /*
46127c478bd9Sstevel@tonic-gate  * ddi_driver_name: Return the normalized driver name. this is the
46137c478bd9Sstevel@tonic-gate  *		actual driver name
46147c478bd9Sstevel@tonic-gate  */
46157c478bd9Sstevel@tonic-gate const char *
46167c478bd9Sstevel@tonic-gate ddi_driver_name(dev_info_t *devi)
46177c478bd9Sstevel@tonic-gate {
46187c478bd9Sstevel@tonic-gate 	major_t major;
46197c478bd9Sstevel@tonic-gate 
4620a204de77Scth 	if ((major = ddi_driver_major(devi)) != DDI_MAJOR_T_NONE)
46217c478bd9Sstevel@tonic-gate 		return (ddi_major_to_name(major));
46227c478bd9Sstevel@tonic-gate 
46237c478bd9Sstevel@tonic-gate 	return (ddi_node_name(devi));
46247c478bd9Sstevel@tonic-gate }
46257c478bd9Sstevel@tonic-gate 
46267c478bd9Sstevel@tonic-gate /*
46277c478bd9Sstevel@tonic-gate  * i_ddi_set_binding_name:	Set binding name.
46287c478bd9Sstevel@tonic-gate  *
46297c478bd9Sstevel@tonic-gate  *	Set the binding name to the given name.
46307c478bd9Sstevel@tonic-gate  *	This routine is for use by the ddi implementation, not by drivers.
46317c478bd9Sstevel@tonic-gate  */
46327c478bd9Sstevel@tonic-gate void
46337c478bd9Sstevel@tonic-gate i_ddi_set_binding_name(dev_info_t *dip, char *name)
46347c478bd9Sstevel@tonic-gate {
46357c478bd9Sstevel@tonic-gate 	DEVI(dip)->devi_binding_name = name;
46367c478bd9Sstevel@tonic-gate 
46377c478bd9Sstevel@tonic-gate }
46387c478bd9Sstevel@tonic-gate 
46397c478bd9Sstevel@tonic-gate /*
46407c478bd9Sstevel@tonic-gate  * ddi_get_name: A synonym of ddi_binding_name() ... returns a name
46417c478bd9Sstevel@tonic-gate  * the implementation has used to bind the node to a driver.
46427c478bd9Sstevel@tonic-gate  */
46437c478bd9Sstevel@tonic-gate char *
46447c478bd9Sstevel@tonic-gate ddi_get_name(dev_info_t *dip)
46457c478bd9Sstevel@tonic-gate {
46467c478bd9Sstevel@tonic-gate 	return (DEVI(dip)->devi_binding_name);
46477c478bd9Sstevel@tonic-gate }
46487c478bd9Sstevel@tonic-gate 
46497c478bd9Sstevel@tonic-gate /*
46507c478bd9Sstevel@tonic-gate  * ddi_node_name: Return the name property of the devinfo node
46517c478bd9Sstevel@tonic-gate  *		This may differ from ddi_binding_name if the node name
46527c478bd9Sstevel@tonic-gate  *		does not define a binding to a driver (i.e. generic names).
46537c478bd9Sstevel@tonic-gate  */
46547c478bd9Sstevel@tonic-gate char *
46557c478bd9Sstevel@tonic-gate ddi_node_name(dev_info_t *dip)
46567c478bd9Sstevel@tonic-gate {
46577c478bd9Sstevel@tonic-gate 	return (DEVI(dip)->devi_node_name);
46587c478bd9Sstevel@tonic-gate }
46597c478bd9Sstevel@tonic-gate 
46607c478bd9Sstevel@tonic-gate 
46617c478bd9Sstevel@tonic-gate /*
46627c478bd9Sstevel@tonic-gate  * ddi_get_nodeid:	Get nodeid stored in dev_info structure.
46637c478bd9Sstevel@tonic-gate  */
46647c478bd9Sstevel@tonic-gate int
46657c478bd9Sstevel@tonic-gate ddi_get_nodeid(dev_info_t *dip)
46667c478bd9Sstevel@tonic-gate {
46677c478bd9Sstevel@tonic-gate 	return (DEVI(dip)->devi_nodeid);
46687c478bd9Sstevel@tonic-gate }
46697c478bd9Sstevel@tonic-gate 
46707c478bd9Sstevel@tonic-gate int
46717c478bd9Sstevel@tonic-gate ddi_get_instance(dev_info_t *dip)
46727c478bd9Sstevel@tonic-gate {
46737c478bd9Sstevel@tonic-gate 	return (DEVI(dip)->devi_instance);
46747c478bd9Sstevel@tonic-gate }
46757c478bd9Sstevel@tonic-gate 
46767c478bd9Sstevel@tonic-gate struct dev_ops *
46777c478bd9Sstevel@tonic-gate ddi_get_driver(dev_info_t *dip)
46787c478bd9Sstevel@tonic-gate {
46797c478bd9Sstevel@tonic-gate 	return (DEVI(dip)->devi_ops);
46807c478bd9Sstevel@tonic-gate }
46817c478bd9Sstevel@tonic-gate 
46827c478bd9Sstevel@tonic-gate void
46837c478bd9Sstevel@tonic-gate ddi_set_driver(dev_info_t *dip, struct dev_ops *devo)
46847c478bd9Sstevel@tonic-gate {
46857c478bd9Sstevel@tonic-gate 	DEVI(dip)->devi_ops = devo;
46867c478bd9Sstevel@tonic-gate }
46877c478bd9Sstevel@tonic-gate 
46887c478bd9Sstevel@tonic-gate /*
46897c478bd9Sstevel@tonic-gate  * ddi_set_driver_private/ddi_get_driver_private:
46907c478bd9Sstevel@tonic-gate  * Get/set device driver private data in devinfo.
46917c478bd9Sstevel@tonic-gate  */
46927c478bd9Sstevel@tonic-gate void
46937c478bd9Sstevel@tonic-gate ddi_set_driver_private(dev_info_t *dip, void *data)
46947c478bd9Sstevel@tonic-gate {
46957c478bd9Sstevel@tonic-gate 	DEVI(dip)->devi_driver_data = data;
46967c478bd9Sstevel@tonic-gate }
46977c478bd9Sstevel@tonic-gate 
46987c478bd9Sstevel@tonic-gate void *
46997c478bd9Sstevel@tonic-gate ddi_get_driver_private(dev_info_t *dip)
47007c478bd9Sstevel@tonic-gate {
47017c478bd9Sstevel@tonic-gate 	return (DEVI(dip)->devi_driver_data);
47027c478bd9Sstevel@tonic-gate }
47037c478bd9Sstevel@tonic-gate 
47047c478bd9Sstevel@tonic-gate /*
47057c478bd9Sstevel@tonic-gate  * ddi_get_parent, ddi_get_child, ddi_get_next_sibling
47067c478bd9Sstevel@tonic-gate  */
47077c478bd9Sstevel@tonic-gate 
47087c478bd9Sstevel@tonic-gate dev_info_t *
47097c478bd9Sstevel@tonic-gate ddi_get_parent(dev_info_t *dip)
47107c478bd9Sstevel@tonic-gate {
47117c478bd9Sstevel@tonic-gate 	return ((dev_info_t *)DEVI(dip)->devi_parent);
47127c478bd9Sstevel@tonic-gate }
47137c478bd9Sstevel@tonic-gate 
47147c478bd9Sstevel@tonic-gate dev_info_t *
47157c478bd9Sstevel@tonic-gate ddi_get_child(dev_info_t *dip)
47167c478bd9Sstevel@tonic-gate {
47177c478bd9Sstevel@tonic-gate 	return ((dev_info_t *)DEVI(dip)->devi_child);
47187c478bd9Sstevel@tonic-gate }
47197c478bd9Sstevel@tonic-gate 
47207c478bd9Sstevel@tonic-gate dev_info_t *
47217c478bd9Sstevel@tonic-gate ddi_get_next_sibling(dev_info_t *dip)
47227c478bd9Sstevel@tonic-gate {
47237c478bd9Sstevel@tonic-gate 	return ((dev_info_t *)DEVI(dip)->devi_sibling);
47247c478bd9Sstevel@tonic-gate }
47257c478bd9Sstevel@tonic-gate 
47267c478bd9Sstevel@tonic-gate dev_info_t *
47277c478bd9Sstevel@tonic-gate ddi_get_next(dev_info_t *dip)
47287c478bd9Sstevel@tonic-gate {
47297c478bd9Sstevel@tonic-gate 	return ((dev_info_t *)DEVI(dip)->devi_next);
47307c478bd9Sstevel@tonic-gate }
47317c478bd9Sstevel@tonic-gate 
47327c478bd9Sstevel@tonic-gate void
47337c478bd9Sstevel@tonic-gate ddi_set_next(dev_info_t *dip, dev_info_t *nextdip)
47347c478bd9Sstevel@tonic-gate {
47357c478bd9Sstevel@tonic-gate 	DEVI(dip)->devi_next = DEVI(nextdip);
47367c478bd9Sstevel@tonic-gate }
47377c478bd9Sstevel@tonic-gate 
47387c478bd9Sstevel@tonic-gate /*
47397c478bd9Sstevel@tonic-gate  * ddi_root_node:		Return root node of devinfo tree
47407c478bd9Sstevel@tonic-gate  */
47417c478bd9Sstevel@tonic-gate 
47427c478bd9Sstevel@tonic-gate dev_info_t *
47437c478bd9Sstevel@tonic-gate ddi_root_node(void)
47447c478bd9Sstevel@tonic-gate {
47457c478bd9Sstevel@tonic-gate 	extern dev_info_t *top_devinfo;
47467c478bd9Sstevel@tonic-gate 
47477c478bd9Sstevel@tonic-gate 	return (top_devinfo);
47487c478bd9Sstevel@tonic-gate }
47497c478bd9Sstevel@tonic-gate 
47507c478bd9Sstevel@tonic-gate /*
47517c478bd9Sstevel@tonic-gate  * Miscellaneous functions:
47527c478bd9Sstevel@tonic-gate  */
47537c478bd9Sstevel@tonic-gate 
47547c478bd9Sstevel@tonic-gate /*
47557c478bd9Sstevel@tonic-gate  * Implementation specific hooks
47567c478bd9Sstevel@tonic-gate  */
47577c478bd9Sstevel@tonic-gate 
47587c478bd9Sstevel@tonic-gate void
47597c478bd9Sstevel@tonic-gate ddi_report_dev(dev_info_t *d)
47607c478bd9Sstevel@tonic-gate {
47617c478bd9Sstevel@tonic-gate 	char *b;
47627c478bd9Sstevel@tonic-gate 
47637c478bd9Sstevel@tonic-gate 	(void) ddi_ctlops(d, d, DDI_CTLOPS_REPORTDEV, (void *)0, (void *)0);
47647c478bd9Sstevel@tonic-gate 
47657c478bd9Sstevel@tonic-gate 	/*
47667c478bd9Sstevel@tonic-gate 	 * If this devinfo node has cb_ops, it's implicitly accessible from
47677c478bd9Sstevel@tonic-gate 	 * userland, so we print its full name together with the instance
47687c478bd9Sstevel@tonic-gate 	 * number 'abbreviation' that the driver may use internally.
47697c478bd9Sstevel@tonic-gate 	 */
47707c478bd9Sstevel@tonic-gate 	if (DEVI(d)->devi_ops->devo_cb_ops != (struct cb_ops *)0 &&
47717c478bd9Sstevel@tonic-gate 	    (b = kmem_zalloc(MAXPATHLEN, KM_NOSLEEP))) {
47727c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "?%s%d is %s\n",
47737c478bd9Sstevel@tonic-gate 		    ddi_driver_name(d), ddi_get_instance(d),
47747c478bd9Sstevel@tonic-gate 		    ddi_pathname(d, b));
47757c478bd9Sstevel@tonic-gate 		kmem_free(b, MAXPATHLEN);
47767c478bd9Sstevel@tonic-gate 	}
47777c478bd9Sstevel@tonic-gate }
47787c478bd9Sstevel@tonic-gate 
47797c478bd9Sstevel@tonic-gate /*
47807c478bd9Sstevel@tonic-gate  * ddi_ctlops() is described in the assembler not to buy a new register
47817c478bd9Sstevel@tonic-gate  * window when it's called and can reduce cost in climbing the device tree
47827c478bd9Sstevel@tonic-gate  * without using the tail call optimization.
47837c478bd9Sstevel@tonic-gate  */
47847c478bd9Sstevel@tonic-gate int
47857c478bd9Sstevel@tonic-gate ddi_dev_regsize(dev_info_t *dev, uint_t rnumber, off_t *result)
47867c478bd9Sstevel@tonic-gate {
47877c478bd9Sstevel@tonic-gate 	int ret;
47887c478bd9Sstevel@tonic-gate 
47897c478bd9Sstevel@tonic-gate 	ret = ddi_ctlops(dev, dev, DDI_CTLOPS_REGSIZE,
47907c478bd9Sstevel@tonic-gate 	    (void *)&rnumber, (void *)result);
47917c478bd9Sstevel@tonic-gate 
47927c478bd9Sstevel@tonic-gate 	return (ret == DDI_SUCCESS ? DDI_SUCCESS : DDI_FAILURE);
47937c478bd9Sstevel@tonic-gate }
47947c478bd9Sstevel@tonic-gate 
47957c478bd9Sstevel@tonic-gate int
47967c478bd9Sstevel@tonic-gate ddi_dev_nregs(dev_info_t *dev, int *result)
47977c478bd9Sstevel@tonic-gate {
47987c478bd9Sstevel@tonic-gate 	return (ddi_ctlops(dev, dev, DDI_CTLOPS_NREGS, 0, (void *)result));
47997c478bd9Sstevel@tonic-gate }
48007c478bd9Sstevel@tonic-gate 
48017c478bd9Sstevel@tonic-gate int
48027c478bd9Sstevel@tonic-gate ddi_dev_is_sid(dev_info_t *d)
48037c478bd9Sstevel@tonic-gate {
48047c478bd9Sstevel@tonic-gate 	return (ddi_ctlops(d, d, DDI_CTLOPS_SIDDEV, (void *)0, (void *)0));
48057c478bd9Sstevel@tonic-gate }
48067c478bd9Sstevel@tonic-gate 
48077c478bd9Sstevel@tonic-gate int
48087c478bd9Sstevel@tonic-gate ddi_slaveonly(dev_info_t *d)
48097c478bd9Sstevel@tonic-gate {
48107c478bd9Sstevel@tonic-gate 	return (ddi_ctlops(d, d, DDI_CTLOPS_SLAVEONLY, (void *)0, (void *)0));
48117c478bd9Sstevel@tonic-gate }
48127c478bd9Sstevel@tonic-gate 
48137c478bd9Sstevel@tonic-gate int
48147c478bd9Sstevel@tonic-gate ddi_dev_affinity(dev_info_t *a, dev_info_t *b)
48157c478bd9Sstevel@tonic-gate {
48167c478bd9Sstevel@tonic-gate 	return (ddi_ctlops(a, a, DDI_CTLOPS_AFFINITY, (void *)b, (void *)0));
48177c478bd9Sstevel@tonic-gate }
48187c478bd9Sstevel@tonic-gate 
48197c478bd9Sstevel@tonic-gate int
48207c478bd9Sstevel@tonic-gate ddi_streams_driver(dev_info_t *dip)
48217c478bd9Sstevel@tonic-gate {
4822737d277aScth 	if (i_ddi_devi_attached(dip) &&
48237c478bd9Sstevel@tonic-gate 	    (DEVI(dip)->devi_ops->devo_cb_ops != NULL) &&
48247c478bd9Sstevel@tonic-gate 	    (DEVI(dip)->devi_ops->devo_cb_ops->cb_str != NULL))
48257c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
48267c478bd9Sstevel@tonic-gate 	return (DDI_FAILURE);
48277c478bd9Sstevel@tonic-gate }
48287c478bd9Sstevel@tonic-gate 
48297c478bd9Sstevel@tonic-gate /*
48307c478bd9Sstevel@tonic-gate  * callback free list
48317c478bd9Sstevel@tonic-gate  */
48327c478bd9Sstevel@tonic-gate 
48337c478bd9Sstevel@tonic-gate static int ncallbacks;
48347c478bd9Sstevel@tonic-gate static int nc_low = 170;
48357c478bd9Sstevel@tonic-gate static int nc_med = 512;
48367c478bd9Sstevel@tonic-gate static int nc_high = 2048;
48377c478bd9Sstevel@tonic-gate static struct ddi_callback *callbackq;
48387c478bd9Sstevel@tonic-gate static struct ddi_callback *callbackqfree;
48397c478bd9Sstevel@tonic-gate 
48407c478bd9Sstevel@tonic-gate /*
48417c478bd9Sstevel@tonic-gate  * set/run callback lists
48427c478bd9Sstevel@tonic-gate  */
48437c478bd9Sstevel@tonic-gate struct	cbstats	{
48447c478bd9Sstevel@tonic-gate 	kstat_named_t	cb_asked;
48457c478bd9Sstevel@tonic-gate 	kstat_named_t	cb_new;
48467c478bd9Sstevel@tonic-gate 	kstat_named_t	cb_run;
48477c478bd9Sstevel@tonic-gate 	kstat_named_t	cb_delete;
48487c478bd9Sstevel@tonic-gate 	kstat_named_t	cb_maxreq;
48497c478bd9Sstevel@tonic-gate 	kstat_named_t	cb_maxlist;
48507c478bd9Sstevel@tonic-gate 	kstat_named_t	cb_alloc;
48517c478bd9Sstevel@tonic-gate 	kstat_named_t	cb_runouts;
48527c478bd9Sstevel@tonic-gate 	kstat_named_t	cb_L2;
48537c478bd9Sstevel@tonic-gate 	kstat_named_t	cb_grow;
48547c478bd9Sstevel@tonic-gate } cbstats = {
48557c478bd9Sstevel@tonic-gate 	{"asked",	KSTAT_DATA_UINT32},
48567c478bd9Sstevel@tonic-gate 	{"new",		KSTAT_DATA_UINT32},
48577c478bd9Sstevel@tonic-gate 	{"run",		KSTAT_DATA_UINT32},
48587c478bd9Sstevel@tonic-gate 	{"delete",	KSTAT_DATA_UINT32},
48597c478bd9Sstevel@tonic-gate 	{"maxreq",	KSTAT_DATA_UINT32},
48607c478bd9Sstevel@tonic-gate 	{"maxlist",	KSTAT_DATA_UINT32},
48617c478bd9Sstevel@tonic-gate 	{"alloc",	KSTAT_DATA_UINT32},
48627c478bd9Sstevel@tonic-gate 	{"runouts",	KSTAT_DATA_UINT32},
48637c478bd9Sstevel@tonic-gate 	{"L2",		KSTAT_DATA_UINT32},
48647c478bd9Sstevel@tonic-gate 	{"grow",	KSTAT_DATA_UINT32},
48657c478bd9Sstevel@tonic-gate };
48667c478bd9Sstevel@tonic-gate 
48677c478bd9Sstevel@tonic-gate #define	nc_asked	cb_asked.value.ui32
48687c478bd9Sstevel@tonic-gate #define	nc_new		cb_new.value.ui32
48697c478bd9Sstevel@tonic-gate #define	nc_run		cb_run.value.ui32
48707c478bd9Sstevel@tonic-gate #define	nc_delete	cb_delete.value.ui32
48717c478bd9Sstevel@tonic-gate #define	nc_maxreq	cb_maxreq.value.ui32
48727c478bd9Sstevel@tonic-gate #define	nc_maxlist	cb_maxlist.value.ui32
48737c478bd9Sstevel@tonic-gate #define	nc_alloc	cb_alloc.value.ui32
48747c478bd9Sstevel@tonic-gate #define	nc_runouts	cb_runouts.value.ui32
48757c478bd9Sstevel@tonic-gate #define	nc_L2		cb_L2.value.ui32
48767c478bd9Sstevel@tonic-gate #define	nc_grow		cb_grow.value.ui32
48777c478bd9Sstevel@tonic-gate 
48787c478bd9Sstevel@tonic-gate static kmutex_t ddi_callback_mutex;
48797c478bd9Sstevel@tonic-gate 
48807c478bd9Sstevel@tonic-gate /*
48817c478bd9Sstevel@tonic-gate  * callbacks are handled using a L1/L2 cache. The L1 cache
48827c478bd9Sstevel@tonic-gate  * comes out of kmem_cache_alloc and can expand/shrink dynamically. If
48837c478bd9Sstevel@tonic-gate  * we can't get callbacks from the L1 cache [because pageout is doing
48847c478bd9Sstevel@tonic-gate  * I/O at the time freemem is 0], we allocate callbacks out of the
48857c478bd9Sstevel@tonic-gate  * L2 cache. The L2 cache is static and depends on the memory size.
48867c478bd9Sstevel@tonic-gate  * [We might also count the number of devices at probe time and
48877c478bd9Sstevel@tonic-gate  * allocate one structure per device and adjust for deferred attach]
48887c478bd9Sstevel@tonic-gate  */
48897c478bd9Sstevel@tonic-gate void
48907c478bd9Sstevel@tonic-gate impl_ddi_callback_init(void)
48917c478bd9Sstevel@tonic-gate {
48927c478bd9Sstevel@tonic-gate 	int	i;
48937c478bd9Sstevel@tonic-gate 	uint_t	physmegs;
48947c478bd9Sstevel@tonic-gate 	kstat_t	*ksp;
48957c478bd9Sstevel@tonic-gate 
48967c478bd9Sstevel@tonic-gate 	physmegs = physmem >> (20 - PAGESHIFT);
48977c478bd9Sstevel@tonic-gate 	if (physmegs < 48) {
48987c478bd9Sstevel@tonic-gate 		ncallbacks = nc_low;
48997c478bd9Sstevel@tonic-gate 	} else if (physmegs < 128) {
49007c478bd9Sstevel@tonic-gate 		ncallbacks = nc_med;
49017c478bd9Sstevel@tonic-gate 	} else {
49027c478bd9Sstevel@tonic-gate 		ncallbacks = nc_high;
49037c478bd9Sstevel@tonic-gate 	}
49047c478bd9Sstevel@tonic-gate 
49057c478bd9Sstevel@tonic-gate 	/*
49067c478bd9Sstevel@tonic-gate 	 * init free list
49077c478bd9Sstevel@tonic-gate 	 */
49087c478bd9Sstevel@tonic-gate 	callbackq = kmem_zalloc(
49097c478bd9Sstevel@tonic-gate 	    ncallbacks * sizeof (struct ddi_callback), KM_SLEEP);
49107c478bd9Sstevel@tonic-gate 	for (i = 0; i < ncallbacks-1; i++)
49117c478bd9Sstevel@tonic-gate 		callbackq[i].c_nfree = &callbackq[i+1];
49127c478bd9Sstevel@tonic-gate 	callbackqfree = callbackq;
49137c478bd9Sstevel@tonic-gate 
49147c478bd9Sstevel@tonic-gate 	/* init kstats */
49157c478bd9Sstevel@tonic-gate 	if (ksp = kstat_create("unix", 0, "cbstats", "misc", KSTAT_TYPE_NAMED,
49167c478bd9Sstevel@tonic-gate 	    sizeof (cbstats) / sizeof (kstat_named_t), KSTAT_FLAG_VIRTUAL)) {
49177c478bd9Sstevel@tonic-gate 		ksp->ks_data = (void *) &cbstats;
49187c478bd9Sstevel@tonic-gate 		kstat_install(ksp);
49197c478bd9Sstevel@tonic-gate 	}
49207c478bd9Sstevel@tonic-gate 
49217c478bd9Sstevel@tonic-gate }
49227c478bd9Sstevel@tonic-gate 
49237c478bd9Sstevel@tonic-gate static void
49247c478bd9Sstevel@tonic-gate callback_insert(int (*funcp)(caddr_t), caddr_t arg, uintptr_t *listid,
49257c478bd9Sstevel@tonic-gate 	int count)
49267c478bd9Sstevel@tonic-gate {
49277c478bd9Sstevel@tonic-gate 	struct ddi_callback *list, *marker, *new;
49287c478bd9Sstevel@tonic-gate 	size_t size = sizeof (struct ddi_callback);
49297c478bd9Sstevel@tonic-gate 
49307c478bd9Sstevel@tonic-gate 	list = marker = (struct ddi_callback *)*listid;
49317c478bd9Sstevel@tonic-gate 	while (list != NULL) {
49327c478bd9Sstevel@tonic-gate 		if (list->c_call == funcp && list->c_arg == arg) {
49337c478bd9Sstevel@tonic-gate 			list->c_count += count;
49347c478bd9Sstevel@tonic-gate 			return;
49357c478bd9Sstevel@tonic-gate 		}
49367c478bd9Sstevel@tonic-gate 		marker = list;
49377c478bd9Sstevel@tonic-gate 		list = list->c_nlist;
49387c478bd9Sstevel@tonic-gate 	}
49397c478bd9Sstevel@tonic-gate 	new = kmem_alloc(size, KM_NOSLEEP);
49407c478bd9Sstevel@tonic-gate 	if (new == NULL) {
49417c478bd9Sstevel@tonic-gate 		new = callbackqfree;
49427c478bd9Sstevel@tonic-gate 		if (new == NULL) {
49437c478bd9Sstevel@tonic-gate 			new = kmem_alloc_tryhard(sizeof (struct ddi_callback),
49447c478bd9Sstevel@tonic-gate 			    &size, KM_NOSLEEP | KM_PANIC);
49457c478bd9Sstevel@tonic-gate 			cbstats.nc_grow++;
49467c478bd9Sstevel@tonic-gate 		} else {
49477c478bd9Sstevel@tonic-gate 			callbackqfree = new->c_nfree;
49487c478bd9Sstevel@tonic-gate 			cbstats.nc_L2++;
49497c478bd9Sstevel@tonic-gate 		}
49507c478bd9Sstevel@tonic-gate 	}
49517c478bd9Sstevel@tonic-gate 	if (marker != NULL) {
49527c478bd9Sstevel@tonic-gate 		marker->c_nlist = new;
49537c478bd9Sstevel@tonic-gate 	} else {
49547c478bd9Sstevel@tonic-gate 		*listid = (uintptr_t)new;
49557c478bd9Sstevel@tonic-gate 	}
49567c478bd9Sstevel@tonic-gate 	new->c_size = size;
49577c478bd9Sstevel@tonic-gate 	new->c_nlist = NULL;
49587c478bd9Sstevel@tonic-gate 	new->c_call = funcp;
49597c478bd9Sstevel@tonic-gate 	new->c_arg = arg;
49607c478bd9Sstevel@tonic-gate 	new->c_count = count;
49617c478bd9Sstevel@tonic-gate 	cbstats.nc_new++;
49627c478bd9Sstevel@tonic-gate 	cbstats.nc_alloc++;
49637c478bd9Sstevel@tonic-gate 	if (cbstats.nc_alloc > cbstats.nc_maxlist)
49647c478bd9Sstevel@tonic-gate 		cbstats.nc_maxlist = cbstats.nc_alloc;
49657c478bd9Sstevel@tonic-gate }
49667c478bd9Sstevel@tonic-gate 
49677c478bd9Sstevel@tonic-gate void
49687c478bd9Sstevel@tonic-gate ddi_set_callback(int (*funcp)(caddr_t), caddr_t arg, uintptr_t *listid)
49697c478bd9Sstevel@tonic-gate {
49707c478bd9Sstevel@tonic-gate 	mutex_enter(&ddi_callback_mutex);
49717c478bd9Sstevel@tonic-gate 	cbstats.nc_asked++;
49727c478bd9Sstevel@tonic-gate 	if ((cbstats.nc_asked - cbstats.nc_run) > cbstats.nc_maxreq)
49737c478bd9Sstevel@tonic-gate 		cbstats.nc_maxreq = (cbstats.nc_asked - cbstats.nc_run);
49747c478bd9Sstevel@tonic-gate 	(void) callback_insert(funcp, arg, listid, 1);
49757c478bd9Sstevel@tonic-gate 	mutex_exit(&ddi_callback_mutex);
49767c478bd9Sstevel@tonic-gate }
49777c478bd9Sstevel@tonic-gate 
49787c478bd9Sstevel@tonic-gate static void
49797c478bd9Sstevel@tonic-gate real_callback_run(void *Queue)
49807c478bd9Sstevel@tonic-gate {
49817c478bd9Sstevel@tonic-gate 	int (*funcp)(caddr_t);
49827c478bd9Sstevel@tonic-gate 	caddr_t arg;
49837c478bd9Sstevel@tonic-gate 	int count, rval;
49847c478bd9Sstevel@tonic-gate 	uintptr_t *listid;
49857c478bd9Sstevel@tonic-gate 	struct ddi_callback *list, *marker;
49867c478bd9Sstevel@tonic-gate 	int check_pending = 1;
49877c478bd9Sstevel@tonic-gate 	int pending = 0;
49887c478bd9Sstevel@tonic-gate 
49897c478bd9Sstevel@tonic-gate 	do {
49907c478bd9Sstevel@tonic-gate 		mutex_enter(&ddi_callback_mutex);
49917c478bd9Sstevel@tonic-gate 		listid = Queue;
49927c478bd9Sstevel@tonic-gate 		list = (struct ddi_callback *)*listid;
49937c478bd9Sstevel@tonic-gate 		if (list == NULL) {
49947c478bd9Sstevel@tonic-gate 			mutex_exit(&ddi_callback_mutex);
49957c478bd9Sstevel@tonic-gate 			return;
49967c478bd9Sstevel@tonic-gate 		}
49977c478bd9Sstevel@tonic-gate 		if (check_pending) {
49987c478bd9Sstevel@tonic-gate 			marker = list;
49997c478bd9Sstevel@tonic-gate 			while (marker != NULL) {
50007c478bd9Sstevel@tonic-gate 				pending += marker->c_count;
50017c478bd9Sstevel@tonic-gate 				marker = marker->c_nlist;
50027c478bd9Sstevel@tonic-gate 			}
50037c478bd9Sstevel@tonic-gate 			check_pending = 0;
50047c478bd9Sstevel@tonic-gate 		}
50057c478bd9Sstevel@tonic-gate 		ASSERT(pending > 0);
50067c478bd9Sstevel@tonic-gate 		ASSERT(list->c_count > 0);
50077c478bd9Sstevel@tonic-gate 		funcp = list->c_call;
50087c478bd9Sstevel@tonic-gate 		arg = list->c_arg;
50097c478bd9Sstevel@tonic-gate 		count = list->c_count;
50107c478bd9Sstevel@tonic-gate 		*(uintptr_t *)Queue = (uintptr_t)list->c_nlist;
50117c478bd9Sstevel@tonic-gate 		if (list >= &callbackq[0] &&
50127c478bd9Sstevel@tonic-gate 		    list <= &callbackq[ncallbacks-1]) {
50137c478bd9Sstevel@tonic-gate 			list->c_nfree = callbackqfree;
50147c478bd9Sstevel@tonic-gate 			callbackqfree = list;
50157c478bd9Sstevel@tonic-gate 		} else
50167c478bd9Sstevel@tonic-gate 			kmem_free(list, list->c_size);
50177c478bd9Sstevel@tonic-gate 
50187c478bd9Sstevel@tonic-gate 		cbstats.nc_delete++;
50197c478bd9Sstevel@tonic-gate 		cbstats.nc_alloc--;
50207c478bd9Sstevel@tonic-gate 		mutex_exit(&ddi_callback_mutex);
50217c478bd9Sstevel@tonic-gate 
50227c478bd9Sstevel@tonic-gate 		do {
50237c478bd9Sstevel@tonic-gate 			if ((rval = (*funcp)(arg)) == 0) {
50247c478bd9Sstevel@tonic-gate 				pending -= count;
50257c478bd9Sstevel@tonic-gate 				mutex_enter(&ddi_callback_mutex);
50267c478bd9Sstevel@tonic-gate 				(void) callback_insert(funcp, arg, listid,
50277c478bd9Sstevel@tonic-gate 				    count);
50287c478bd9Sstevel@tonic-gate 				cbstats.nc_runouts++;
50297c478bd9Sstevel@tonic-gate 			} else {
50307c478bd9Sstevel@tonic-gate 				pending--;
50317c478bd9Sstevel@tonic-gate 				mutex_enter(&ddi_callback_mutex);
50327c478bd9Sstevel@tonic-gate 				cbstats.nc_run++;
50337c478bd9Sstevel@tonic-gate 			}
50347c478bd9Sstevel@tonic-gate 			mutex_exit(&ddi_callback_mutex);
50357c478bd9Sstevel@tonic-gate 		} while (rval != 0 && (--count > 0));
50367c478bd9Sstevel@tonic-gate 	} while (pending > 0);
50377c478bd9Sstevel@tonic-gate }
50387c478bd9Sstevel@tonic-gate 
50397c478bd9Sstevel@tonic-gate void
50407c478bd9Sstevel@tonic-gate ddi_run_callback(uintptr_t *listid)
50417c478bd9Sstevel@tonic-gate {
50427c478bd9Sstevel@tonic-gate 	softcall(real_callback_run, listid);
50437c478bd9Sstevel@tonic-gate }
50447c478bd9Sstevel@tonic-gate 
5045dd4eeefdSeota /*
5046dd4eeefdSeota  * ddi_periodic_t
5047dd4eeefdSeota  * ddi_periodic_add(void (*func)(void *), void *arg, hrtime_t interval,
5048dd4eeefdSeota  *     int level)
5049dd4eeefdSeota  *
5050dd4eeefdSeota  * INTERFACE LEVEL
5051dd4eeefdSeota  *      Solaris DDI specific (Solaris DDI)
5052dd4eeefdSeota  *
5053dd4eeefdSeota  * PARAMETERS
5054dd4eeefdSeota  *      func: the callback function
5055dd4eeefdSeota  *
5056dd4eeefdSeota  *            The callback function will be invoked. The function is invoked
5057dd4eeefdSeota  *            in kernel context if the argument level passed is the zero.
5058dd4eeefdSeota  *            Otherwise it's invoked in interrupt context at the specified
5059dd4eeefdSeota  *            level.
5060dd4eeefdSeota  *
5061dd4eeefdSeota  *       arg: the argument passed to the callback function
5062dd4eeefdSeota  *
5063dd4eeefdSeota  *  interval: interval time
5064dd4eeefdSeota  *
5065dd4eeefdSeota  *    level : callback interrupt level
5066dd4eeefdSeota  *
5067dd4eeefdSeota  *            If the value is the zero, the callback function is invoked
5068dd4eeefdSeota  *            in kernel context. If the value is more than the zero, but
5069dd4eeefdSeota  *            less than or equal to ten, the callback function is invoked in
5070dd4eeefdSeota  *            interrupt context at the specified interrupt level, which may
5071dd4eeefdSeota  *            be used for real time applications.
5072dd4eeefdSeota  *
5073dd4eeefdSeota  *            This value must be in range of 0-10, which can be a numeric
5074dd4eeefdSeota  *            number or a pre-defined macro (DDI_IPL_0, ... , DDI_IPL_10).
5075dd4eeefdSeota  *
5076dd4eeefdSeota  * DESCRIPTION
5077dd4eeefdSeota  *      ddi_periodic_add(9F) schedules the specified function to be
5078dd4eeefdSeota  *      periodically invoked in the interval time.
5079dd4eeefdSeota  *
5080dd4eeefdSeota  *      As well as timeout(9F), the exact time interval over which the function
5081dd4eeefdSeota  *      takes effect cannot be guaranteed, but the value given is a close
5082dd4eeefdSeota  *      approximation.
5083dd4eeefdSeota  *
5084dd4eeefdSeota  *      Drivers waiting on behalf of processes with real-time constraints must
5085dd4eeefdSeota  *      pass non-zero value with the level argument to ddi_periodic_add(9F).
5086dd4eeefdSeota  *
5087dd4eeefdSeota  * RETURN VALUES
5088dd4eeefdSeota  *      ddi_periodic_add(9F) returns a non-zero opaque value (ddi_periodic_t),
5089dd4eeefdSeota  *      which must be used for ddi_periodic_delete(9F) to specify the request.
5090dd4eeefdSeota  *
5091dd4eeefdSeota  * CONTEXT
5092dd4eeefdSeota  *      ddi_periodic_add(9F) can be called in user or kernel context, but
5093dd4eeefdSeota  *      it cannot be called in interrupt context, which is different from
5094dd4eeefdSeota  *      timeout(9F).
5095dd4eeefdSeota  */
5096dd4eeefdSeota ddi_periodic_t
5097dd4eeefdSeota ddi_periodic_add(void (*func)(void *), void *arg, hrtime_t interval, int level)
5098dd4eeefdSeota {
5099dd4eeefdSeota 	/*
5100dd4eeefdSeota 	 * Sanity check of the argument level.
5101dd4eeefdSeota 	 */
5102dd4eeefdSeota 	if (level < DDI_IPL_0 || level > DDI_IPL_10)
5103dd4eeefdSeota 		cmn_err(CE_PANIC,
5104dd4eeefdSeota 		    "ddi_periodic_add: invalid interrupt level (%d).", level);
5105dd4eeefdSeota 
5106dd4eeefdSeota 	/*
5107dd4eeefdSeota 	 * Sanity check of the context. ddi_periodic_add() cannot be
5108dd4eeefdSeota 	 * called in either interrupt context or high interrupt context.
5109dd4eeefdSeota 	 */
5110dd4eeefdSeota 	if (servicing_interrupt())
5111dd4eeefdSeota 		cmn_err(CE_PANIC,
5112dd4eeefdSeota 		    "ddi_periodic_add: called in (high) interrupt context.");
5113dd4eeefdSeota 
5114dd4eeefdSeota 	return ((ddi_periodic_t)i_timeout(func, arg, interval, level));
5115dd4eeefdSeota }
5116dd4eeefdSeota 
5117dd4eeefdSeota /*
5118dd4eeefdSeota  * void
5119dd4eeefdSeota  * ddi_periodic_delete(ddi_periodic_t req)
5120dd4eeefdSeota  *
5121dd4eeefdSeota  * INTERFACE LEVEL
5122dd4eeefdSeota  *     Solaris DDI specific (Solaris DDI)
5123dd4eeefdSeota  *
5124dd4eeefdSeota  * PARAMETERS
5125dd4eeefdSeota  *     req: ddi_periodic_t opaque value ddi_periodic_add(9F) returned
5126dd4eeefdSeota  *     previously.
5127dd4eeefdSeota  *
5128dd4eeefdSeota  * DESCRIPTION
5129dd4eeefdSeota  *     ddi_periodic_delete(9F) cancels the ddi_periodic_add(9F) request
5130dd4eeefdSeota  *     previously requested.
5131dd4eeefdSeota  *
5132dd4eeefdSeota  *     ddi_periodic_delete(9F) will not return until the pending request
5133dd4eeefdSeota  *     is canceled or executed.
5134dd4eeefdSeota  *
5135dd4eeefdSeota  *     As well as untimeout(9F), calling ddi_periodic_delete(9F) for a
5136dd4eeefdSeota  *     timeout which is either running on another CPU, or has already
5137dd4eeefdSeota  *     completed causes no problems. However, unlike untimeout(9F), there is
5138dd4eeefdSeota  *     no restrictions on the lock which might be held across the call to
5139dd4eeefdSeota  *     ddi_periodic_delete(9F).
5140dd4eeefdSeota  *
5141dd4eeefdSeota  *     Drivers should be structured with the understanding that the arrival of
5142dd4eeefdSeota  *     both an interrupt and a timeout for that interrupt can occasionally
5143dd4eeefdSeota  *     occur, in either order.
5144dd4eeefdSeota  *
5145dd4eeefdSeota  * CONTEXT
5146dd4eeefdSeota  *     ddi_periodic_delete(9F) can be called in user or kernel context, but
5147dd4eeefdSeota  *     it cannot be called in interrupt context, which is different from
5148dd4eeefdSeota  *     untimeout(9F).
5149dd4eeefdSeota  */
5150dd4eeefdSeota void
5151dd4eeefdSeota ddi_periodic_delete(ddi_periodic_t req)
5152dd4eeefdSeota {
5153dd4eeefdSeota 	/*
5154dd4eeefdSeota 	 * Sanity check of the context. ddi_periodic_delete() cannot be
5155dd4eeefdSeota 	 * called in either interrupt context or high interrupt context.
5156dd4eeefdSeota 	 */
5157dd4eeefdSeota 	if (servicing_interrupt())
5158dd4eeefdSeota 		cmn_err(CE_PANIC,
5159dd4eeefdSeota 		    "ddi_periodic_delete: called in (high) interrupt context.");
5160dd4eeefdSeota 
5161dd4eeefdSeota 	i_untimeout((timeout_t)req);
5162dd4eeefdSeota }
5163dd4eeefdSeota 
51647c478bd9Sstevel@tonic-gate dev_info_t *
51657c478bd9Sstevel@tonic-gate nodevinfo(dev_t dev, int otyp)
51667c478bd9Sstevel@tonic-gate {
51677c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(dev, otyp))
51687c478bd9Sstevel@tonic-gate 	return ((dev_info_t *)0);
51697c478bd9Sstevel@tonic-gate }
51707c478bd9Sstevel@tonic-gate 
51717c478bd9Sstevel@tonic-gate /*
51727c478bd9Sstevel@tonic-gate  * A driver should support its own getinfo(9E) entry point. This function
51737c478bd9Sstevel@tonic-gate  * is provided as a convenience for ON drivers that don't expect their
51747c478bd9Sstevel@tonic-gate  * getinfo(9E) entry point to be called. A driver that uses this must not
51757c478bd9Sstevel@tonic-gate  * call ddi_create_minor_node.
51767c478bd9Sstevel@tonic-gate  */
51777c478bd9Sstevel@tonic-gate int
51787c478bd9Sstevel@tonic-gate ddi_no_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
51797c478bd9Sstevel@tonic-gate {
51807c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(dip, infocmd, arg, result))
51817c478bd9Sstevel@tonic-gate 	return (DDI_FAILURE);
51827c478bd9Sstevel@tonic-gate }
51837c478bd9Sstevel@tonic-gate 
51847c478bd9Sstevel@tonic-gate /*
51857c478bd9Sstevel@tonic-gate  * A driver should support its own getinfo(9E) entry point. This function
51867c478bd9Sstevel@tonic-gate  * is provided as a convenience for ON drivers that where the minor number
51877c478bd9Sstevel@tonic-gate  * is the instance. Drivers that do not have 1:1 mapping must implement
51887c478bd9Sstevel@tonic-gate  * their own getinfo(9E) function.
51897c478bd9Sstevel@tonic-gate  */
51907c478bd9Sstevel@tonic-gate int
51917c478bd9Sstevel@tonic-gate ddi_getinfo_1to1(dev_info_t *dip, ddi_info_cmd_t infocmd,
51927c478bd9Sstevel@tonic-gate     void *arg, void **result)
51937c478bd9Sstevel@tonic-gate {
51947c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(dip))
51957c478bd9Sstevel@tonic-gate 	int	instance;
51967c478bd9Sstevel@tonic-gate 
51977c478bd9Sstevel@tonic-gate 	if (infocmd != DDI_INFO_DEVT2INSTANCE)
51987c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
51997c478bd9Sstevel@tonic-gate 
52007c478bd9Sstevel@tonic-gate 	instance = getminor((dev_t)(uintptr_t)arg);
52017c478bd9Sstevel@tonic-gate 	*result = (void *)(uintptr_t)instance;
52027c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
52037c478bd9Sstevel@tonic-gate }
52047c478bd9Sstevel@tonic-gate 
52057c478bd9Sstevel@tonic-gate int
52067c478bd9Sstevel@tonic-gate ddifail(dev_info_t *devi, ddi_attach_cmd_t cmd)
52077c478bd9Sstevel@tonic-gate {
52087c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(devi, cmd))
52097c478bd9Sstevel@tonic-gate 	return (DDI_FAILURE);
52107c478bd9Sstevel@tonic-gate }
52117c478bd9Sstevel@tonic-gate 
52127c478bd9Sstevel@tonic-gate int
52137c478bd9Sstevel@tonic-gate ddi_no_dma_map(dev_info_t *dip, dev_info_t *rdip,
52147c478bd9Sstevel@tonic-gate     struct ddi_dma_req *dmareqp, ddi_dma_handle_t *handlep)
52157c478bd9Sstevel@tonic-gate {
52167c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(dip, rdip, dmareqp, handlep))
52177c478bd9Sstevel@tonic-gate 	return (DDI_DMA_NOMAPPING);
52187c478bd9Sstevel@tonic-gate }
52197c478bd9Sstevel@tonic-gate 
52207c478bd9Sstevel@tonic-gate int
52217c478bd9Sstevel@tonic-gate ddi_no_dma_allochdl(dev_info_t *dip, dev_info_t *rdip, ddi_dma_attr_t *attr,
52227c478bd9Sstevel@tonic-gate     int (*waitfp)(caddr_t), caddr_t arg, ddi_dma_handle_t *handlep)
52237c478bd9Sstevel@tonic-gate {
52247c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(dip, rdip, attr, waitfp, arg, handlep))
52257c478bd9Sstevel@tonic-gate 	return (DDI_DMA_BADATTR);
52267c478bd9Sstevel@tonic-gate }
52277c478bd9Sstevel@tonic-gate 
52287c478bd9Sstevel@tonic-gate int
52297c478bd9Sstevel@tonic-gate ddi_no_dma_freehdl(dev_info_t *dip, dev_info_t *rdip,
52307c478bd9Sstevel@tonic-gate     ddi_dma_handle_t handle)
52317c478bd9Sstevel@tonic-gate {
52327c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(dip, rdip, handle))
52337c478bd9Sstevel@tonic-gate 	return (DDI_FAILURE);
52347c478bd9Sstevel@tonic-gate }
52357c478bd9Sstevel@tonic-gate 
52367c478bd9Sstevel@tonic-gate int
52377c478bd9Sstevel@tonic-gate ddi_no_dma_bindhdl(dev_info_t *dip, dev_info_t *rdip,
52387c478bd9Sstevel@tonic-gate     ddi_dma_handle_t handle, struct ddi_dma_req *dmareq,
52397c478bd9Sstevel@tonic-gate     ddi_dma_cookie_t *cp, uint_t *ccountp)
52407c478bd9Sstevel@tonic-gate {
52417c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(dip, rdip, handle, dmareq, cp, ccountp))
52427c478bd9Sstevel@tonic-gate 	return (DDI_DMA_NOMAPPING);
52437c478bd9Sstevel@tonic-gate }
52447c478bd9Sstevel@tonic-gate 
52457c478bd9Sstevel@tonic-gate int
52467c478bd9Sstevel@tonic-gate ddi_no_dma_unbindhdl(dev_info_t *dip, dev_info_t *rdip,
52477c478bd9Sstevel@tonic-gate     ddi_dma_handle_t handle)
52487c478bd9Sstevel@tonic-gate {
52497c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(dip, rdip, handle))
52507c478bd9Sstevel@tonic-gate 	return (DDI_FAILURE);
52517c478bd9Sstevel@tonic-gate }
52527c478bd9Sstevel@tonic-gate 
52537c478bd9Sstevel@tonic-gate int
52547c478bd9Sstevel@tonic-gate ddi_no_dma_flush(dev_info_t *dip, dev_info_t *rdip,
52557c478bd9Sstevel@tonic-gate     ddi_dma_handle_t handle, off_t off, size_t len,
52567c478bd9Sstevel@tonic-gate     uint_t cache_flags)
52577c478bd9Sstevel@tonic-gate {
52587c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(dip, rdip, handle, off, len, cache_flags))
52597c478bd9Sstevel@tonic-gate 	return (DDI_FAILURE);
52607c478bd9Sstevel@tonic-gate }
52617c478bd9Sstevel@tonic-gate 
52627c478bd9Sstevel@tonic-gate int
52637c478bd9Sstevel@tonic-gate ddi_no_dma_win(dev_info_t *dip, dev_info_t *rdip,
52647c478bd9Sstevel@tonic-gate     ddi_dma_handle_t handle, uint_t win, off_t *offp,
52657c478bd9Sstevel@tonic-gate     size_t *lenp, ddi_dma_cookie_t *cookiep, uint_t *ccountp)
52667c478bd9Sstevel@tonic-gate {
52677c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(dip, rdip, handle, win, offp, lenp, cookiep, ccountp))
52687c478bd9Sstevel@tonic-gate 	return (DDI_FAILURE);
52697c478bd9Sstevel@tonic-gate }
52707c478bd9Sstevel@tonic-gate 
52717c478bd9Sstevel@tonic-gate int
52727c478bd9Sstevel@tonic-gate ddi_no_dma_mctl(dev_info_t *dip, dev_info_t *rdip,
52737c478bd9Sstevel@tonic-gate     ddi_dma_handle_t handle, enum ddi_dma_ctlops request,
52747c478bd9Sstevel@tonic-gate     off_t *offp, size_t *lenp, caddr_t *objp, uint_t flags)
52757c478bd9Sstevel@tonic-gate {
52767c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(dip, rdip, handle, request, offp, lenp, objp, flags))
52777c478bd9Sstevel@tonic-gate 	return (DDI_FAILURE);
52787c478bd9Sstevel@tonic-gate }
52797c478bd9Sstevel@tonic-gate 
52807c478bd9Sstevel@tonic-gate void
52817c478bd9Sstevel@tonic-gate ddivoid(void)
52827c478bd9Sstevel@tonic-gate {}
52837c478bd9Sstevel@tonic-gate 
52847c478bd9Sstevel@tonic-gate int
52857c478bd9Sstevel@tonic-gate nochpoll(dev_t dev, short events, int anyyet, short *reventsp,
52867c478bd9Sstevel@tonic-gate     struct pollhead **pollhdrp)
52877c478bd9Sstevel@tonic-gate {
52887c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(dev, events, anyyet, reventsp, pollhdrp))
52897c478bd9Sstevel@tonic-gate 	return (ENXIO);
52907c478bd9Sstevel@tonic-gate }
52917c478bd9Sstevel@tonic-gate 
52927c478bd9Sstevel@tonic-gate cred_t *
52937c478bd9Sstevel@tonic-gate ddi_get_cred(void)
52947c478bd9Sstevel@tonic-gate {
52957c478bd9Sstevel@tonic-gate 	return (CRED());
52967c478bd9Sstevel@tonic-gate }
52977c478bd9Sstevel@tonic-gate 
52987c478bd9Sstevel@tonic-gate clock_t
52997c478bd9Sstevel@tonic-gate ddi_get_lbolt(void)
53007c478bd9Sstevel@tonic-gate {
5301d3d50737SRafael Vanoni 	return ((clock_t)lbolt_hybrid());
5302d3d50737SRafael Vanoni }
5303d3d50737SRafael Vanoni 
5304d3d50737SRafael Vanoni int64_t
5305d3d50737SRafael Vanoni ddi_get_lbolt64(void)
5306d3d50737SRafael Vanoni {
5307d3d50737SRafael Vanoni 	return (lbolt_hybrid());
53087c478bd9Sstevel@tonic-gate }
53097c478bd9Sstevel@tonic-gate 
53107c478bd9Sstevel@tonic-gate time_t
53117c478bd9Sstevel@tonic-gate ddi_get_time(void)
53127c478bd9Sstevel@tonic-gate {
53137c478bd9Sstevel@tonic-gate 	time_t	now;
53147c478bd9Sstevel@tonic-gate 
53157c478bd9Sstevel@tonic-gate 	if ((now = gethrestime_sec()) == 0) {
53167c478bd9Sstevel@tonic-gate 		timestruc_t ts;
53177c478bd9Sstevel@tonic-gate 		mutex_enter(&tod_lock);
53187c478bd9Sstevel@tonic-gate 		ts = tod_get();
53197c478bd9Sstevel@tonic-gate 		mutex_exit(&tod_lock);
53207c478bd9Sstevel@tonic-gate 		return (ts.tv_sec);
53217c478bd9Sstevel@tonic-gate 	} else {
53227c478bd9Sstevel@tonic-gate 		return (now);
53237c478bd9Sstevel@tonic-gate 	}
53247c478bd9Sstevel@tonic-gate }
53257c478bd9Sstevel@tonic-gate 
53267c478bd9Sstevel@tonic-gate pid_t
53277c478bd9Sstevel@tonic-gate ddi_get_pid(void)
53287c478bd9Sstevel@tonic-gate {
53297c478bd9Sstevel@tonic-gate 	return (ttoproc(curthread)->p_pid);
53307c478bd9Sstevel@tonic-gate }
53317c478bd9Sstevel@tonic-gate 
53327c478bd9Sstevel@tonic-gate kt_did_t
53337c478bd9Sstevel@tonic-gate ddi_get_kt_did(void)
53347c478bd9Sstevel@tonic-gate {
53357c478bd9Sstevel@tonic-gate 	return (curthread->t_did);
53367c478bd9Sstevel@tonic-gate }
53377c478bd9Sstevel@tonic-gate 
53387c478bd9Sstevel@tonic-gate /*
53397c478bd9Sstevel@tonic-gate  * This function returns B_TRUE if the caller can reasonably expect that a call
53407c478bd9Sstevel@tonic-gate  * to cv_wait_sig(9F), cv_timedwait_sig(9F), or qwait_sig(9F) could be awakened
53417c478bd9Sstevel@tonic-gate  * by user-level signal.  If it returns B_FALSE, then the caller should use
53427c478bd9Sstevel@tonic-gate  * other means to make certain that the wait will not hang "forever."
53437c478bd9Sstevel@tonic-gate  *
53447c478bd9Sstevel@tonic-gate  * It does not check the signal mask, nor for reception of any particular
53457c478bd9Sstevel@tonic-gate  * signal.
53467c478bd9Sstevel@tonic-gate  *
53477c478bd9Sstevel@tonic-gate  * Currently, a thread can receive a signal if it's not a kernel thread and it
53487c478bd9Sstevel@tonic-gate  * is not in the middle of exit(2) tear-down.  Threads that are in that
53497c478bd9Sstevel@tonic-gate  * tear-down effectively convert cv_wait_sig to cv_wait, cv_timedwait_sig to
53507c478bd9Sstevel@tonic-gate  * cv_timedwait, and qwait_sig to qwait.
53517c478bd9Sstevel@tonic-gate  */
53527c478bd9Sstevel@tonic-gate boolean_t
53537c478bd9Sstevel@tonic-gate ddi_can_receive_sig(void)
53547c478bd9Sstevel@tonic-gate {
53557c478bd9Sstevel@tonic-gate 	proc_t *pp;
53567c478bd9Sstevel@tonic-gate 
53577c478bd9Sstevel@tonic-gate 	if (curthread->t_proc_flag & TP_LWPEXIT)
53587c478bd9Sstevel@tonic-gate 		return (B_FALSE);
53597c478bd9Sstevel@tonic-gate 	if ((pp = ttoproc(curthread)) == NULL)
53607c478bd9Sstevel@tonic-gate 		return (B_FALSE);
53617c478bd9Sstevel@tonic-gate 	return (pp->p_as != &kas);
53627c478bd9Sstevel@tonic-gate }
53637c478bd9Sstevel@tonic-gate 
53647c478bd9Sstevel@tonic-gate /*
53657c478bd9Sstevel@tonic-gate  * Swap bytes in 16-bit [half-]words
53667c478bd9Sstevel@tonic-gate  */
53677c478bd9Sstevel@tonic-gate void
53687c478bd9Sstevel@tonic-gate swab(void *src, void *dst, size_t nbytes)
53697c478bd9Sstevel@tonic-gate {
53707c478bd9Sstevel@tonic-gate 	uchar_t *pf = (uchar_t *)src;
53717c478bd9Sstevel@tonic-gate 	uchar_t *pt = (uchar_t *)dst;
53727c478bd9Sstevel@tonic-gate 	uchar_t tmp;
53737c478bd9Sstevel@tonic-gate 	int nshorts;
53747c478bd9Sstevel@tonic-gate 
53757c478bd9Sstevel@tonic-gate 	nshorts = nbytes >> 1;
53767c478bd9Sstevel@tonic-gate 
53777c478bd9Sstevel@tonic-gate 	while (--nshorts >= 0) {
53787c478bd9Sstevel@tonic-gate 		tmp = *pf++;
53797c478bd9Sstevel@tonic-gate 		*pt++ = *pf++;
53807c478bd9Sstevel@tonic-gate 		*pt++ = tmp;
53817c478bd9Sstevel@tonic-gate 	}
53827c478bd9Sstevel@tonic-gate }
53837c478bd9Sstevel@tonic-gate 
53847c478bd9Sstevel@tonic-gate static void
53857c478bd9Sstevel@tonic-gate ddi_append_minor_node(dev_info_t *ddip, struct ddi_minor_data *dmdp)
53867c478bd9Sstevel@tonic-gate {
5387b9ccdc5aScth 	int			circ;
53887c478bd9Sstevel@tonic-gate 	struct ddi_minor_data	*dp;
53897c478bd9Sstevel@tonic-gate 
5390b9ccdc5aScth 	ndi_devi_enter(ddip, &circ);
53917c478bd9Sstevel@tonic-gate 	if ((dp = DEVI(ddip)->devi_minor) == (struct ddi_minor_data *)NULL) {
53927c478bd9Sstevel@tonic-gate 		DEVI(ddip)->devi_minor = dmdp;
53937c478bd9Sstevel@tonic-gate 	} else {
53947c478bd9Sstevel@tonic-gate 		while (dp->next != (struct ddi_minor_data *)NULL)
53957c478bd9Sstevel@tonic-gate 			dp = dp->next;
53967c478bd9Sstevel@tonic-gate 		dp->next = dmdp;
53977c478bd9Sstevel@tonic-gate 	}
5398b9ccdc5aScth 	ndi_devi_exit(ddip, circ);
53997c478bd9Sstevel@tonic-gate }
54007c478bd9Sstevel@tonic-gate 
54017c478bd9Sstevel@tonic-gate /*
54027c478bd9Sstevel@tonic-gate  * Part of the obsolete SunCluster DDI Hooks.
54037c478bd9Sstevel@tonic-gate  * Keep for binary compatibility
54047c478bd9Sstevel@tonic-gate  */
54057c478bd9Sstevel@tonic-gate minor_t
54067c478bd9Sstevel@tonic-gate ddi_getiminor(dev_t dev)
54077c478bd9Sstevel@tonic-gate {
54087c478bd9Sstevel@tonic-gate 	return (getminor(dev));
54097c478bd9Sstevel@tonic-gate }
54107c478bd9Sstevel@tonic-gate 
54117c478bd9Sstevel@tonic-gate static int
54127c478bd9Sstevel@tonic-gate i_log_devfs_minor_create(dev_info_t *dip, char *minor_name)
54137c478bd9Sstevel@tonic-gate {
54147c478bd9Sstevel@tonic-gate 	int se_flag;
54157c478bd9Sstevel@tonic-gate 	int kmem_flag;
54167c478bd9Sstevel@tonic-gate 	int se_err;
541730294554Sphitran 	char *pathname, *class_name;
54187c478bd9Sstevel@tonic-gate 	sysevent_t *ev = NULL;
54197c478bd9Sstevel@tonic-gate 	sysevent_id_t eid;
54207c478bd9Sstevel@tonic-gate 	sysevent_value_t se_val;
54217c478bd9Sstevel@tonic-gate 	sysevent_attr_list_t *ev_attr_list = NULL;
54227c478bd9Sstevel@tonic-gate 
54237c478bd9Sstevel@tonic-gate 	/* determine interrupt context */
54247c478bd9Sstevel@tonic-gate 	se_flag = (servicing_interrupt()) ? SE_NOSLEEP : SE_SLEEP;
54257c478bd9Sstevel@tonic-gate 	kmem_flag = (se_flag == SE_SLEEP) ? KM_SLEEP : KM_NOSLEEP;
54267c478bd9Sstevel@tonic-gate 
54274c06356bSdh142964 	i_ddi_di_cache_invalidate();
54287c478bd9Sstevel@tonic-gate 
54297c478bd9Sstevel@tonic-gate #ifdef DEBUG
54307c478bd9Sstevel@tonic-gate 	if ((se_flag == SE_NOSLEEP) && sunddi_debug) {
54317c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "ddi_create_minor_node: called from "
54327c478bd9Sstevel@tonic-gate 		    "interrupt level by driver %s",
54337c478bd9Sstevel@tonic-gate 		    ddi_driver_name(dip));
54347c478bd9Sstevel@tonic-gate 	}
54357c478bd9Sstevel@tonic-gate #endif /* DEBUG */
54367c478bd9Sstevel@tonic-gate 
54377c478bd9Sstevel@tonic-gate 	ev = sysevent_alloc(EC_DEVFS, ESC_DEVFS_MINOR_CREATE, EP_DDI, se_flag);
54387c478bd9Sstevel@tonic-gate 	if (ev == NULL) {
54397c478bd9Sstevel@tonic-gate 		goto fail;
54407c478bd9Sstevel@tonic-gate 	}
54417c478bd9Sstevel@tonic-gate 
54427c478bd9Sstevel@tonic-gate 	pathname = kmem_alloc(MAXPATHLEN, kmem_flag);
54437c478bd9Sstevel@tonic-gate 	if (pathname == NULL) {
54447c478bd9Sstevel@tonic-gate 		sysevent_free(ev);
54457c478bd9Sstevel@tonic-gate 		goto fail;
54467c478bd9Sstevel@tonic-gate 	}
54477c478bd9Sstevel@tonic-gate 
54487c478bd9Sstevel@tonic-gate 	(void) ddi_pathname(dip, pathname);
54497c478bd9Sstevel@tonic-gate 	ASSERT(strlen(pathname));
54507c478bd9Sstevel@tonic-gate 	se_val.value_type = SE_DATA_TYPE_STRING;
54517c478bd9Sstevel@tonic-gate 	se_val.value.sv_string = pathname;
54527c478bd9Sstevel@tonic-gate 	if (sysevent_add_attr(&ev_attr_list, DEVFS_PATHNAME,
54537c478bd9Sstevel@tonic-gate 	    &se_val, se_flag) != 0) {
54547c478bd9Sstevel@tonic-gate 		kmem_free(pathname, MAXPATHLEN);
54557c478bd9Sstevel@tonic-gate 		sysevent_free(ev);
54567c478bd9Sstevel@tonic-gate 		goto fail;
54577c478bd9Sstevel@tonic-gate 	}
54587c478bd9Sstevel@tonic-gate 	kmem_free(pathname, MAXPATHLEN);
54597c478bd9Sstevel@tonic-gate 
546030294554Sphitran 	/* add the device class attribute */
546130294554Sphitran 	if ((class_name = i_ddi_devi_class(dip)) != NULL) {
546230294554Sphitran 		se_val.value_type = SE_DATA_TYPE_STRING;
546330294554Sphitran 		se_val.value.sv_string = class_name;
546430294554Sphitran 		if (sysevent_add_attr(&ev_attr_list,
546530294554Sphitran 		    DEVFS_DEVI_CLASS, &se_val, SE_SLEEP) != 0) {
546630294554Sphitran 			sysevent_free_attr(ev_attr_list);
546730294554Sphitran 			goto fail;
546830294554Sphitran 		}
546930294554Sphitran 	}
547030294554Sphitran 
54717c478bd9Sstevel@tonic-gate 	/*
54727c478bd9Sstevel@tonic-gate 	 * allow for NULL minor names
54737c478bd9Sstevel@tonic-gate 	 */
54747c478bd9Sstevel@tonic-gate 	if (minor_name != NULL) {
54757c478bd9Sstevel@tonic-gate 		se_val.value.sv_string = minor_name;
54767c478bd9Sstevel@tonic-gate 		if (sysevent_add_attr(&ev_attr_list, DEVFS_MINOR_NAME,
54777c478bd9Sstevel@tonic-gate 		    &se_val, se_flag) != 0) {
54787c478bd9Sstevel@tonic-gate 			sysevent_free_attr(ev_attr_list);
54797c478bd9Sstevel@tonic-gate 			sysevent_free(ev);
54807c478bd9Sstevel@tonic-gate 			goto fail;
54817c478bd9Sstevel@tonic-gate 		}
54827c478bd9Sstevel@tonic-gate 	}
54837c478bd9Sstevel@tonic-gate 
54847c478bd9Sstevel@tonic-gate 	if (sysevent_attach_attributes(ev, ev_attr_list) != 0) {
54857c478bd9Sstevel@tonic-gate 		sysevent_free_attr(ev_attr_list);
54867c478bd9Sstevel@tonic-gate 		sysevent_free(ev);
54877c478bd9Sstevel@tonic-gate 		goto fail;
54887c478bd9Sstevel@tonic-gate 	}
54897c478bd9Sstevel@tonic-gate 
54907c478bd9Sstevel@tonic-gate 	if ((se_err = log_sysevent(ev, se_flag, &eid)) != 0) {
54917c478bd9Sstevel@tonic-gate 		if (se_err == SE_NO_TRANSPORT) {
54927c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "/devices or /dev may not be current "
54937c478bd9Sstevel@tonic-gate 			    "for driver %s (%s). Run devfsadm -i %s",
54947c478bd9Sstevel@tonic-gate 			    ddi_driver_name(dip), "syseventd not responding",
54957c478bd9Sstevel@tonic-gate 			    ddi_driver_name(dip));
54967c478bd9Sstevel@tonic-gate 		} else {
54977c478bd9Sstevel@tonic-gate 			sysevent_free(ev);
54987c478bd9Sstevel@tonic-gate 			goto fail;
54997c478bd9Sstevel@tonic-gate 		}
55007c478bd9Sstevel@tonic-gate 	}
55017c478bd9Sstevel@tonic-gate 
55027c478bd9Sstevel@tonic-gate 	sysevent_free(ev);
55037c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
55047c478bd9Sstevel@tonic-gate fail:
55057c478bd9Sstevel@tonic-gate 	cmn_err(CE_WARN, "/devices or /dev may not be current "
55067c478bd9Sstevel@tonic-gate 	    "for driver %s. Run devfsadm -i %s",
55077c478bd9Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_driver_name(dip));
55087c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
55097c478bd9Sstevel@tonic-gate }
55107c478bd9Sstevel@tonic-gate 
55117c478bd9Sstevel@tonic-gate /*
55127c478bd9Sstevel@tonic-gate  * failing to remove a minor node is not of interest
55137c478bd9Sstevel@tonic-gate  * therefore we do not generate an error message
55147c478bd9Sstevel@tonic-gate  */
55157c478bd9Sstevel@tonic-gate static int
55167c478bd9Sstevel@tonic-gate i_log_devfs_minor_remove(dev_info_t *dip, char *minor_name)
55177c478bd9Sstevel@tonic-gate {
551830294554Sphitran 	char *pathname, *class_name;
55197c478bd9Sstevel@tonic-gate 	sysevent_t *ev;
55207c478bd9Sstevel@tonic-gate 	sysevent_id_t eid;
55217c478bd9Sstevel@tonic-gate 	sysevent_value_t se_val;
55227c478bd9Sstevel@tonic-gate 	sysevent_attr_list_t *ev_attr_list = NULL;
55237c478bd9Sstevel@tonic-gate 
55247c478bd9Sstevel@tonic-gate 	/*
55257c478bd9Sstevel@tonic-gate 	 * only log ddi_remove_minor_node() calls outside the scope
55267c478bd9Sstevel@tonic-gate 	 * of attach/detach reconfigurations and when the dip is
55277c478bd9Sstevel@tonic-gate 	 * still initialized.
55287c478bd9Sstevel@tonic-gate 	 */
55297c478bd9Sstevel@tonic-gate 	if (DEVI_IS_ATTACHING(dip) || DEVI_IS_DETACHING(dip) ||
55307c478bd9Sstevel@tonic-gate 	    (i_ddi_node_state(dip) < DS_INITIALIZED)) {
55317c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
55327c478bd9Sstevel@tonic-gate 	}
55337c478bd9Sstevel@tonic-gate 
55344c06356bSdh142964 	i_ddi_di_cache_invalidate();
55357c478bd9Sstevel@tonic-gate 
55367c478bd9Sstevel@tonic-gate 	ev = sysevent_alloc(EC_DEVFS, ESC_DEVFS_MINOR_REMOVE, EP_DDI, SE_SLEEP);
55377c478bd9Sstevel@tonic-gate 	if (ev == NULL) {
55387c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
55397c478bd9Sstevel@tonic-gate 	}
55407c478bd9Sstevel@tonic-gate 
55417c478bd9Sstevel@tonic-gate 	pathname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
55427c478bd9Sstevel@tonic-gate 	if (pathname == NULL) {
55437c478bd9Sstevel@tonic-gate 		sysevent_free(ev);
55447c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
55457c478bd9Sstevel@tonic-gate 	}
55467c478bd9Sstevel@tonic-gate 
55477c478bd9Sstevel@tonic-gate 	(void) ddi_pathname(dip, pathname);
55487c478bd9Sstevel@tonic-gate 	ASSERT(strlen(pathname));
55497c478bd9Sstevel@tonic-gate 	se_val.value_type = SE_DATA_TYPE_STRING;
55507c478bd9Sstevel@tonic-gate 	se_val.value.sv_string = pathname;
55517c478bd9Sstevel@tonic-gate 	if (sysevent_add_attr(&ev_attr_list, DEVFS_PATHNAME,
55527c478bd9Sstevel@tonic-gate 	    &se_val, SE_SLEEP) != 0) {
55537c478bd9Sstevel@tonic-gate 		kmem_free(pathname, MAXPATHLEN);
55547c478bd9Sstevel@tonic-gate 		sysevent_free(ev);
55557c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
55567c478bd9Sstevel@tonic-gate 	}
55577c478bd9Sstevel@tonic-gate 
55587c478bd9Sstevel@tonic-gate 	kmem_free(pathname, MAXPATHLEN);
55597c478bd9Sstevel@tonic-gate 
55607c478bd9Sstevel@tonic-gate 	/*
55617c478bd9Sstevel@tonic-gate 	 * allow for NULL minor names
55627c478bd9Sstevel@tonic-gate 	 */
55637c478bd9Sstevel@tonic-gate 	if (minor_name != NULL) {
55647c478bd9Sstevel@tonic-gate 		se_val.value.sv_string = minor_name;
55657c478bd9Sstevel@tonic-gate 		if (sysevent_add_attr(&ev_attr_list, DEVFS_MINOR_NAME,
55667c478bd9Sstevel@tonic-gate 		    &se_val, SE_SLEEP) != 0) {
55677c478bd9Sstevel@tonic-gate 			sysevent_free_attr(ev_attr_list);
55687c478bd9Sstevel@tonic-gate 			goto fail;
55697c478bd9Sstevel@tonic-gate 		}
55707c478bd9Sstevel@tonic-gate 	}
55717c478bd9Sstevel@tonic-gate 
557230294554Sphitran 	if ((class_name = i_ddi_devi_class(dip)) != NULL) {
557330294554Sphitran 		/* add the device class, driver name and instance attributes */
557430294554Sphitran 
557530294554Sphitran 		se_val.value_type = SE_DATA_TYPE_STRING;
557630294554Sphitran 		se_val.value.sv_string = class_name;
557730294554Sphitran 		if (sysevent_add_attr(&ev_attr_list,
557830294554Sphitran 		    DEVFS_DEVI_CLASS, &se_val, SE_SLEEP) != 0) {
557930294554Sphitran 			sysevent_free_attr(ev_attr_list);
558030294554Sphitran 			goto fail;
558130294554Sphitran 		}
558230294554Sphitran 
558330294554Sphitran 		se_val.value_type = SE_DATA_TYPE_STRING;
558430294554Sphitran 		se_val.value.sv_string = (char *)ddi_driver_name(dip);
558530294554Sphitran 		if (sysevent_add_attr(&ev_attr_list,
558630294554Sphitran 		    DEVFS_DRIVER_NAME, &se_val, SE_SLEEP) != 0) {
558730294554Sphitran 			sysevent_free_attr(ev_attr_list);
558830294554Sphitran 			goto fail;
558930294554Sphitran 		}
559030294554Sphitran 
559130294554Sphitran 		se_val.value_type = SE_DATA_TYPE_INT32;
559230294554Sphitran 		se_val.value.sv_int32 = ddi_get_instance(dip);
559330294554Sphitran 		if (sysevent_add_attr(&ev_attr_list,
559430294554Sphitran 		    DEVFS_INSTANCE, &se_val, SE_SLEEP) != 0) {
559530294554Sphitran 			sysevent_free_attr(ev_attr_list);
559630294554Sphitran 			goto fail;
559730294554Sphitran 		}
559830294554Sphitran 
559930294554Sphitran 	}
560030294554Sphitran 
56017c478bd9Sstevel@tonic-gate 	if (sysevent_attach_attributes(ev, ev_attr_list) != 0) {
56027c478bd9Sstevel@tonic-gate 		sysevent_free_attr(ev_attr_list);
56037c478bd9Sstevel@tonic-gate 	} else {
56047c478bd9Sstevel@tonic-gate 		(void) log_sysevent(ev, SE_SLEEP, &eid);
56057c478bd9Sstevel@tonic-gate 	}
56067c478bd9Sstevel@tonic-gate fail:
56077c478bd9Sstevel@tonic-gate 	sysevent_free(ev);
56087c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
56097c478bd9Sstevel@tonic-gate }
56107c478bd9Sstevel@tonic-gate 
56117c478bd9Sstevel@tonic-gate /*
56127c478bd9Sstevel@tonic-gate  * Derive the device class of the node.
56137c478bd9Sstevel@tonic-gate  * Device class names aren't defined yet. Until this is done we use
56147c478bd9Sstevel@tonic-gate  * devfs event subclass names as device class names.
56157c478bd9Sstevel@tonic-gate  */
56167c478bd9Sstevel@tonic-gate static int
56177c478bd9Sstevel@tonic-gate derive_devi_class(dev_info_t *dip, char *node_type, int flag)
56187c478bd9Sstevel@tonic-gate {
56197c478bd9Sstevel@tonic-gate 	int rv = DDI_SUCCESS;
56207c478bd9Sstevel@tonic-gate 
56217c478bd9Sstevel@tonic-gate 	if (i_ddi_devi_class(dip) == NULL) {
56227c478bd9Sstevel@tonic-gate 		if (strncmp(node_type, DDI_NT_BLOCK,
56237c478bd9Sstevel@tonic-gate 		    sizeof (DDI_NT_BLOCK) - 1) == 0 &&
56247c478bd9Sstevel@tonic-gate 		    (node_type[sizeof (DDI_NT_BLOCK) - 1] == '\0' ||
56257c478bd9Sstevel@tonic-gate 		    node_type[sizeof (DDI_NT_BLOCK) - 1] == ':') &&
56267c478bd9Sstevel@tonic-gate 		    strcmp(node_type, DDI_NT_FD) != 0) {
56277c478bd9Sstevel@tonic-gate 
56287c478bd9Sstevel@tonic-gate 			rv = i_ddi_set_devi_class(dip, ESC_DISK, flag);
56297c478bd9Sstevel@tonic-gate 
56307c478bd9Sstevel@tonic-gate 		} else if (strncmp(node_type, DDI_NT_NET,
56317c478bd9Sstevel@tonic-gate 		    sizeof (DDI_NT_NET) - 1) == 0 &&
56327c478bd9Sstevel@tonic-gate 		    (node_type[sizeof (DDI_NT_NET) - 1] == '\0' ||
56337c478bd9Sstevel@tonic-gate 		    node_type[sizeof (DDI_NT_NET) - 1] == ':')) {
56347c478bd9Sstevel@tonic-gate 
56357c478bd9Sstevel@tonic-gate 			rv = i_ddi_set_devi_class(dip, ESC_NETWORK, flag);
5636db11e6feSjacobs 
5637db11e6feSjacobs 		} else if (strncmp(node_type, DDI_NT_PRINTER,
5638db11e6feSjacobs 		    sizeof (DDI_NT_PRINTER) - 1) == 0 &&
5639db11e6feSjacobs 		    (node_type[sizeof (DDI_NT_PRINTER) - 1] == '\0' ||
5640db11e6feSjacobs 		    node_type[sizeof (DDI_NT_PRINTER) - 1] == ':')) {
5641db11e6feSjacobs 
5642db11e6feSjacobs 			rv = i_ddi_set_devi_class(dip, ESC_PRINTER, flag);
564330294554Sphitran 
564430294554Sphitran 		} else if (strncmp(node_type, DDI_PSEUDO,
564530294554Sphitran 		    sizeof (DDI_PSEUDO) -1) == 0 &&
564630294554Sphitran 		    (strncmp(ESC_LOFI, ddi_node_name(dip),
564730294554Sphitran 		    sizeof (ESC_LOFI) -1) == 0)) {
564830294554Sphitran 			rv = i_ddi_set_devi_class(dip, ESC_LOFI, flag);
56497c478bd9Sstevel@tonic-gate 		}
56507c478bd9Sstevel@tonic-gate 	}
56517c478bd9Sstevel@tonic-gate 
56527c478bd9Sstevel@tonic-gate 	return (rv);
56537c478bd9Sstevel@tonic-gate }
56547c478bd9Sstevel@tonic-gate 
56557c478bd9Sstevel@tonic-gate /*
56567c478bd9Sstevel@tonic-gate  * Check compliance with PSARC 2003/375:
56577c478bd9Sstevel@tonic-gate  *
56587c478bd9Sstevel@tonic-gate  * The name must contain only characters a-z, A-Z, 0-9 or _ and it must not
56597c478bd9Sstevel@tonic-gate  * exceed IFNAMSIZ (16) characters in length.
56607c478bd9Sstevel@tonic-gate  */
56617c478bd9Sstevel@tonic-gate static boolean_t
56627c478bd9Sstevel@tonic-gate verify_name(char *name)
56637c478bd9Sstevel@tonic-gate {
56647c478bd9Sstevel@tonic-gate 	size_t	len = strlen(name);
56657c478bd9Sstevel@tonic-gate 	char	*cp;
56667c478bd9Sstevel@tonic-gate 
56677c478bd9Sstevel@tonic-gate 	if (len == 0 || len > IFNAMSIZ)
56687c478bd9Sstevel@tonic-gate 		return (B_FALSE);
56697c478bd9Sstevel@tonic-gate 
56707c478bd9Sstevel@tonic-gate 	for (cp = name; *cp != '\0'; cp++) {
56717c478bd9Sstevel@tonic-gate 		if (!isalnum(*cp) && *cp != '_')
56727c478bd9Sstevel@tonic-gate 			return (B_FALSE);
56737c478bd9Sstevel@tonic-gate 	}
56747c478bd9Sstevel@tonic-gate 
56757c478bd9Sstevel@tonic-gate 	return (B_TRUE);
56767c478bd9Sstevel@tonic-gate }
56777c478bd9Sstevel@tonic-gate 
56787c478bd9Sstevel@tonic-gate /*
56797c478bd9Sstevel@tonic-gate  * ddi_create_minor_common:	Create a  ddi_minor_data structure and
56807c478bd9Sstevel@tonic-gate  *				attach it to the given devinfo node.
56817c478bd9Sstevel@tonic-gate  */
56827c478bd9Sstevel@tonic-gate 
56837c478bd9Sstevel@tonic-gate int
56847c478bd9Sstevel@tonic-gate ddi_create_minor_common(dev_info_t *dip, char *name, int spec_type,
56857c478bd9Sstevel@tonic-gate     minor_t minor_num, char *node_type, int flag, ddi_minor_type mtype,
56867c478bd9Sstevel@tonic-gate     const char *read_priv, const char *write_priv, mode_t priv_mode)
56877c478bd9Sstevel@tonic-gate {
56887c478bd9Sstevel@tonic-gate 	struct ddi_minor_data *dmdp;
56897c478bd9Sstevel@tonic-gate 	major_t major;
56907c478bd9Sstevel@tonic-gate 
56917c478bd9Sstevel@tonic-gate 	if (spec_type != S_IFCHR && spec_type != S_IFBLK)
56927c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
56937c478bd9Sstevel@tonic-gate 
56947c478bd9Sstevel@tonic-gate 	if (name == NULL)
56957c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
56967c478bd9Sstevel@tonic-gate 
56977c478bd9Sstevel@tonic-gate 	/*
56987c478bd9Sstevel@tonic-gate 	 * Log a message if the minor number the driver is creating
56997c478bd9Sstevel@tonic-gate 	 * is not expressible on the on-disk filesystem (currently
57007c478bd9Sstevel@tonic-gate 	 * this is limited to 18 bits both by UFS). The device can
57017c478bd9Sstevel@tonic-gate 	 * be opened via devfs, but not by device special files created
57027c478bd9Sstevel@tonic-gate 	 * via mknod().
57037c478bd9Sstevel@tonic-gate 	 */
57047c478bd9Sstevel@tonic-gate 	if (minor_num > L_MAXMIN32) {
57057c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN,
57067c478bd9Sstevel@tonic-gate 		    "%s%d:%s minor 0x%x too big for 32-bit applications",
57077c478bd9Sstevel@tonic-gate 		    ddi_driver_name(dip), ddi_get_instance(dip),
57087c478bd9Sstevel@tonic-gate 		    name, minor_num);
57097c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
57107c478bd9Sstevel@tonic-gate 	}
57117c478bd9Sstevel@tonic-gate 
57127c478bd9Sstevel@tonic-gate 	/* dip must be bound and attached */
57137c478bd9Sstevel@tonic-gate 	major = ddi_driver_major(dip);
5714a204de77Scth 	ASSERT(major != DDI_MAJOR_T_NONE);
57157c478bd9Sstevel@tonic-gate 
57167c478bd9Sstevel@tonic-gate 	/*
57177c478bd9Sstevel@tonic-gate 	 * Default node_type to DDI_PSEUDO and issue notice in debug mode
57187c478bd9Sstevel@tonic-gate 	 */
57197c478bd9Sstevel@tonic-gate 	if (node_type == NULL) {
57207c478bd9Sstevel@tonic-gate 		node_type = DDI_PSEUDO;
57217c478bd9Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_NOTE, "!illegal node_type NULL for %s%d "
57227c478bd9Sstevel@tonic-gate 		    " minor node %s; default to DDI_PSEUDO",
57237c478bd9Sstevel@tonic-gate 		    ddi_driver_name(dip), ddi_get_instance(dip), name));
57247c478bd9Sstevel@tonic-gate 	}
57257c478bd9Sstevel@tonic-gate 
57267c478bd9Sstevel@tonic-gate 	/*
57277c478bd9Sstevel@tonic-gate 	 * If the driver is a network driver, ensure that the name falls within
57287c478bd9Sstevel@tonic-gate 	 * the interface naming constraints specified by PSARC/2003/375.
57297c478bd9Sstevel@tonic-gate 	 */
57307c478bd9Sstevel@tonic-gate 	if (strcmp(node_type, DDI_NT_NET) == 0) {
57317c478bd9Sstevel@tonic-gate 		if (!verify_name(name))
57327c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
57337c478bd9Sstevel@tonic-gate 
57347c478bd9Sstevel@tonic-gate 		if (mtype == DDM_MINOR) {
57357c478bd9Sstevel@tonic-gate 			struct devnames *dnp = &devnamesp[major];
57367c478bd9Sstevel@tonic-gate 
57377c478bd9Sstevel@tonic-gate 			/* Mark driver as a network driver */
57387c478bd9Sstevel@tonic-gate 			LOCK_DEV_OPS(&dnp->dn_lock);
57397c478bd9Sstevel@tonic-gate 			dnp->dn_flags |= DN_NETWORK_DRIVER;
5740095be824SCathy Zhou 
5741095be824SCathy Zhou 			/*
5742095be824SCathy Zhou 			 * If this minor node is created during the device
5743095be824SCathy Zhou 			 * attachment, this is a physical network device.
5744095be824SCathy Zhou 			 * Mark the driver as a physical network driver.
5745095be824SCathy Zhou 			 */
5746095be824SCathy Zhou 			if (DEVI_IS_ATTACHING(dip))
5747095be824SCathy Zhou 				dnp->dn_flags |= DN_NETWORK_PHYSDRIVER;
57487c478bd9Sstevel@tonic-gate 			UNLOCK_DEV_OPS(&dnp->dn_lock);
57497c478bd9Sstevel@tonic-gate 		}
57507c478bd9Sstevel@tonic-gate 	}
57517c478bd9Sstevel@tonic-gate 
57527c478bd9Sstevel@tonic-gate 	if (mtype == DDM_MINOR) {
57537c478bd9Sstevel@tonic-gate 		if (derive_devi_class(dip,  node_type, KM_NOSLEEP) !=
57547c478bd9Sstevel@tonic-gate 		    DDI_SUCCESS)
57557c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
57567c478bd9Sstevel@tonic-gate 	}
57577c478bd9Sstevel@tonic-gate 
57587c478bd9Sstevel@tonic-gate 	/*
57597c478bd9Sstevel@tonic-gate 	 * Take care of minor number information for the node.
57607c478bd9Sstevel@tonic-gate 	 */
57617c478bd9Sstevel@tonic-gate 
57627c478bd9Sstevel@tonic-gate 	if ((dmdp = kmem_zalloc(sizeof (struct ddi_minor_data),
57637c478bd9Sstevel@tonic-gate 	    KM_NOSLEEP)) == NULL) {
57647c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
57657c478bd9Sstevel@tonic-gate 	}
57667c478bd9Sstevel@tonic-gate 	if ((dmdp->ddm_name = i_ddi_strdup(name, KM_NOSLEEP)) == NULL) {
57677c478bd9Sstevel@tonic-gate 		kmem_free(dmdp, sizeof (struct ddi_minor_data));
57687c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
57697c478bd9Sstevel@tonic-gate 	}
57707c478bd9Sstevel@tonic-gate 	dmdp->dip = dip;
57717c478bd9Sstevel@tonic-gate 	dmdp->ddm_dev = makedevice(major, minor_num);
57727c478bd9Sstevel@tonic-gate 	dmdp->ddm_spec_type = spec_type;
57737c478bd9Sstevel@tonic-gate 	dmdp->ddm_node_type = node_type;
57747c478bd9Sstevel@tonic-gate 	dmdp->type = mtype;
57757c478bd9Sstevel@tonic-gate 	if (flag & CLONE_DEV) {
57767c478bd9Sstevel@tonic-gate 		dmdp->type = DDM_ALIAS;
57777c478bd9Sstevel@tonic-gate 		dmdp->ddm_dev = makedevice(ddi_driver_major(clone_dip), major);
57787c478bd9Sstevel@tonic-gate 	}
57797c478bd9Sstevel@tonic-gate 	if (flag & PRIVONLY_DEV) {
57807c478bd9Sstevel@tonic-gate 		dmdp->ddm_flags |= DM_NO_FSPERM;
57817c478bd9Sstevel@tonic-gate 	}
57827c478bd9Sstevel@tonic-gate 	if (read_priv || write_priv) {
57837c478bd9Sstevel@tonic-gate 		dmdp->ddm_node_priv =
57847c478bd9Sstevel@tonic-gate 		    devpolicy_priv_by_name(read_priv, write_priv);
57857c478bd9Sstevel@tonic-gate 	}
57867c478bd9Sstevel@tonic-gate 	dmdp->ddm_priv_mode = priv_mode;
57877c478bd9Sstevel@tonic-gate 
57887c478bd9Sstevel@tonic-gate 	ddi_append_minor_node(dip, dmdp);
57897c478bd9Sstevel@tonic-gate 
57907c478bd9Sstevel@tonic-gate 	/*
57917c478bd9Sstevel@tonic-gate 	 * only log ddi_create_minor_node() calls which occur
57927c478bd9Sstevel@tonic-gate 	 * outside the scope of attach(9e)/detach(9e) reconfigurations
57937c478bd9Sstevel@tonic-gate 	 */
57947b3700d1Sszhou 	if (!(DEVI_IS_ATTACHING(dip) || DEVI_IS_DETACHING(dip)) &&
57957b3700d1Sszhou 	    mtype != DDM_INTERNAL_PATH) {
57967c478bd9Sstevel@tonic-gate 		(void) i_log_devfs_minor_create(dip, name);
57977c478bd9Sstevel@tonic-gate 	}
57987c478bd9Sstevel@tonic-gate 
57997c478bd9Sstevel@tonic-gate 	/*
58007c478bd9Sstevel@tonic-gate 	 * Check if any dacf rules match the creation of this minor node
58017c478bd9Sstevel@tonic-gate 	 */
58027c478bd9Sstevel@tonic-gate 	dacfc_match_create_minor(name, node_type, dip, dmdp, flag);
58037c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
58047c478bd9Sstevel@tonic-gate }
58057c478bd9Sstevel@tonic-gate 
58067c478bd9Sstevel@tonic-gate int
58077c478bd9Sstevel@tonic-gate ddi_create_minor_node(dev_info_t *dip, char *name, int spec_type,
58087c478bd9Sstevel@tonic-gate     minor_t minor_num, char *node_type, int flag)
58097c478bd9Sstevel@tonic-gate {
58107c478bd9Sstevel@tonic-gate 	return (ddi_create_minor_common(dip, name, spec_type, minor_num,
58117c478bd9Sstevel@tonic-gate 	    node_type, flag, DDM_MINOR, NULL, NULL, 0));
58127c478bd9Sstevel@tonic-gate }
58137c478bd9Sstevel@tonic-gate 
58147c478bd9Sstevel@tonic-gate int
58157c478bd9Sstevel@tonic-gate ddi_create_priv_minor_node(dev_info_t *dip, char *name, int spec_type,
58167c478bd9Sstevel@tonic-gate     minor_t minor_num, char *node_type, int flag,
58177c478bd9Sstevel@tonic-gate     const char *rdpriv, const char *wrpriv, mode_t priv_mode)
58187c478bd9Sstevel@tonic-gate {
58197c478bd9Sstevel@tonic-gate 	return (ddi_create_minor_common(dip, name, spec_type, minor_num,
58207c478bd9Sstevel@tonic-gate 	    node_type, flag, DDM_MINOR, rdpriv, wrpriv, priv_mode));
58217c478bd9Sstevel@tonic-gate }
58227c478bd9Sstevel@tonic-gate 
58237c478bd9Sstevel@tonic-gate int
58247c478bd9Sstevel@tonic-gate ddi_create_default_minor_node(dev_info_t *dip, char *name, int spec_type,
58257c478bd9Sstevel@tonic-gate     minor_t minor_num, char *node_type, int flag)
58267c478bd9Sstevel@tonic-gate {
58277c478bd9Sstevel@tonic-gate 	return (ddi_create_minor_common(dip, name, spec_type, minor_num,
58287c478bd9Sstevel@tonic-gate 	    node_type, flag, DDM_DEFAULT, NULL, NULL, 0));
58297c478bd9Sstevel@tonic-gate }
58307c478bd9Sstevel@tonic-gate 
58317c478bd9Sstevel@tonic-gate /*
58327c478bd9Sstevel@tonic-gate  * Internal (non-ddi) routine for drivers to export names known
58337c478bd9Sstevel@tonic-gate  * to the kernel (especially ddi_pathname_to_dev_t and friends)
58347c478bd9Sstevel@tonic-gate  * but not exported externally to /dev
58357c478bd9Sstevel@tonic-gate  */
58367c478bd9Sstevel@tonic-gate int
58377c478bd9Sstevel@tonic-gate ddi_create_internal_pathname(dev_info_t *dip, char *name, int spec_type,
58387c478bd9Sstevel@tonic-gate     minor_t minor_num)
58397c478bd9Sstevel@tonic-gate {
58407c478bd9Sstevel@tonic-gate 	return (ddi_create_minor_common(dip, name, spec_type, minor_num,
58417c478bd9Sstevel@tonic-gate 	    "internal", 0, DDM_INTERNAL_PATH, NULL, NULL, 0));
58427c478bd9Sstevel@tonic-gate }
58437c478bd9Sstevel@tonic-gate 
58447c478bd9Sstevel@tonic-gate void
58457c478bd9Sstevel@tonic-gate ddi_remove_minor_node(dev_info_t *dip, char *name)
58467c478bd9Sstevel@tonic-gate {
5847b9ccdc5aScth 	int			circ;
58487c478bd9Sstevel@tonic-gate 	struct ddi_minor_data	*dmdp, *dmdp1;
58497c478bd9Sstevel@tonic-gate 	struct ddi_minor_data	**dmdp_prev;
58507c478bd9Sstevel@tonic-gate 
5851b9ccdc5aScth 	ndi_devi_enter(dip, &circ);
58527c478bd9Sstevel@tonic-gate 	dmdp_prev = &DEVI(dip)->devi_minor;
58537c478bd9Sstevel@tonic-gate 	dmdp = DEVI(dip)->devi_minor;
58547c478bd9Sstevel@tonic-gate 	while (dmdp != NULL) {
58557c478bd9Sstevel@tonic-gate 		dmdp1 = dmdp->next;
58567c478bd9Sstevel@tonic-gate 		if ((name == NULL || (dmdp->ddm_name != NULL &&
58577c478bd9Sstevel@tonic-gate 		    strcmp(name, dmdp->ddm_name) == 0))) {
58587c478bd9Sstevel@tonic-gate 			if (dmdp->ddm_name != NULL) {
58597b3700d1Sszhou 				if (dmdp->type != DDM_INTERNAL_PATH)
58607c478bd9Sstevel@tonic-gate 					(void) i_log_devfs_minor_remove(dip,
58617c478bd9Sstevel@tonic-gate 					    dmdp->ddm_name);
58627c478bd9Sstevel@tonic-gate 				kmem_free(dmdp->ddm_name,
58637c478bd9Sstevel@tonic-gate 				    strlen(dmdp->ddm_name) + 1);
58647c478bd9Sstevel@tonic-gate 			}
58657c478bd9Sstevel@tonic-gate 			/*
58667c478bd9Sstevel@tonic-gate 			 * Release device privilege, if any.
58677c478bd9Sstevel@tonic-gate 			 * Release dacf client data associated with this minor
58687c478bd9Sstevel@tonic-gate 			 * node by storing NULL.
58697c478bd9Sstevel@tonic-gate 			 */
58707c478bd9Sstevel@tonic-gate 			if (dmdp->ddm_node_priv)
58717c478bd9Sstevel@tonic-gate 				dpfree(dmdp->ddm_node_priv);
58727c478bd9Sstevel@tonic-gate 			dacf_store_info((dacf_infohdl_t)dmdp, NULL);
58737c478bd9Sstevel@tonic-gate 			kmem_free(dmdp, sizeof (struct ddi_minor_data));
58747c478bd9Sstevel@tonic-gate 			*dmdp_prev = dmdp1;
58757c478bd9Sstevel@tonic-gate 			/*
58767c478bd9Sstevel@tonic-gate 			 * OK, we found it, so get out now -- if we drive on,
58777c478bd9Sstevel@tonic-gate 			 * we will strcmp against garbage.  See 1139209.
58787c478bd9Sstevel@tonic-gate 			 */
58797c478bd9Sstevel@tonic-gate 			if (name != NULL)
58807c478bd9Sstevel@tonic-gate 				break;
58817c478bd9Sstevel@tonic-gate 		} else {
58827c478bd9Sstevel@tonic-gate 			dmdp_prev = &dmdp->next;
58837c478bd9Sstevel@tonic-gate 		}
58847c478bd9Sstevel@tonic-gate 		dmdp = dmdp1;
58857c478bd9Sstevel@tonic-gate 	}
5886b9ccdc5aScth 	ndi_devi_exit(dip, circ);
58877c478bd9Sstevel@tonic-gate }
58887c478bd9Sstevel@tonic-gate 
58897c478bd9Sstevel@tonic-gate 
58907c478bd9Sstevel@tonic-gate int
58917c478bd9Sstevel@tonic-gate ddi_in_panic()
58927c478bd9Sstevel@tonic-gate {
58937c478bd9Sstevel@tonic-gate 	return (panicstr != NULL);
58947c478bd9Sstevel@tonic-gate }
58957c478bd9Sstevel@tonic-gate 
58967c478bd9Sstevel@tonic-gate 
58977c478bd9Sstevel@tonic-gate /*
58987c478bd9Sstevel@tonic-gate  * Find first bit set in a mask (returned counting from 1 up)
58997c478bd9Sstevel@tonic-gate  */
59007c478bd9Sstevel@tonic-gate 
59017c478bd9Sstevel@tonic-gate int
59027c478bd9Sstevel@tonic-gate ddi_ffs(long mask)
59037c478bd9Sstevel@tonic-gate {
59047c478bd9Sstevel@tonic-gate 	return (ffs(mask));
59057c478bd9Sstevel@tonic-gate }
59067c478bd9Sstevel@tonic-gate 
59077c478bd9Sstevel@tonic-gate /*
59087c478bd9Sstevel@tonic-gate  * Find last bit set. Take mask and clear
59097c478bd9Sstevel@tonic-gate  * all but the most significant bit, and
59107c478bd9Sstevel@tonic-gate  * then let ffs do the rest of the work.
59117c478bd9Sstevel@tonic-gate  *
59127c478bd9Sstevel@tonic-gate  * Algorithm courtesy of Steve Chessin.
59137c478bd9Sstevel@tonic-gate  */
59147c478bd9Sstevel@tonic-gate 
59157c478bd9Sstevel@tonic-gate int
59167c478bd9Sstevel@tonic-gate ddi_fls(long mask)
59177c478bd9Sstevel@tonic-gate {
59187c478bd9Sstevel@tonic-gate 	while (mask) {
59197c478bd9Sstevel@tonic-gate 		long nx;
59207c478bd9Sstevel@tonic-gate 
59217c478bd9Sstevel@tonic-gate 		if ((nx = (mask & (mask - 1))) == 0)
59227c478bd9Sstevel@tonic-gate 			break;
59237c478bd9Sstevel@tonic-gate 		mask = nx;
59247c478bd9Sstevel@tonic-gate 	}
59257c478bd9Sstevel@tonic-gate 	return (ffs(mask));
59267c478bd9Sstevel@tonic-gate }
59277c478bd9Sstevel@tonic-gate 
59287c478bd9Sstevel@tonic-gate /*
59294c06356bSdh142964  * The ddi_soft_state_* routines comprise generic storage management utilities
59304c06356bSdh142964  * for driver soft state structures (in "the old days," this was done with
59314c06356bSdh142964  * statically sized array - big systems and dynamic loading and unloading
59324c06356bSdh142964  * make heap allocation more attractive).
59337c478bd9Sstevel@tonic-gate  */
59347c478bd9Sstevel@tonic-gate 
59357c478bd9Sstevel@tonic-gate /*
59367c478bd9Sstevel@tonic-gate  * Allocate a set of pointers to 'n_items' objects of size 'size'
59377c478bd9Sstevel@tonic-gate  * bytes.  Each pointer is initialized to nil.
59387c478bd9Sstevel@tonic-gate  *
59397c478bd9Sstevel@tonic-gate  * The 'size' and 'n_items' values are stashed in the opaque
59407c478bd9Sstevel@tonic-gate  * handle returned to the caller.
59417c478bd9Sstevel@tonic-gate  *
59427c478bd9Sstevel@tonic-gate  * This implementation interprets 'set of pointers' to mean 'array
59437c478bd9Sstevel@tonic-gate  * of pointers' but note that nothing in the interface definition
59447c478bd9Sstevel@tonic-gate  * precludes an implementation that uses, for example, a linked list.
59457c478bd9Sstevel@tonic-gate  * However there should be a small efficiency gain from using an array
59467c478bd9Sstevel@tonic-gate  * at lookup time.
59477c478bd9Sstevel@tonic-gate  *
59487c478bd9Sstevel@tonic-gate  * NOTE	As an optimization, we make our growable array allocations in
59497c478bd9Sstevel@tonic-gate  *	powers of two (bytes), since that's how much kmem_alloc (currently)
59507c478bd9Sstevel@tonic-gate  *	gives us anyway.  It should save us some free/realloc's ..
59517c478bd9Sstevel@tonic-gate  *
59527c478bd9Sstevel@tonic-gate  *	As a further optimization, we make the growable array start out
59537c478bd9Sstevel@tonic-gate  *	with MIN_N_ITEMS in it.
59547c478bd9Sstevel@tonic-gate  */
59557c478bd9Sstevel@tonic-gate 
59567c478bd9Sstevel@tonic-gate #define	MIN_N_ITEMS	8	/* 8 void *'s == 32 bytes */
59577c478bd9Sstevel@tonic-gate 
59587c478bd9Sstevel@tonic-gate int
59597c478bd9Sstevel@tonic-gate ddi_soft_state_init(void **state_p, size_t size, size_t n_items)
59607c478bd9Sstevel@tonic-gate {
59614c06356bSdh142964 	i_ddi_soft_state	*ss;
59627c478bd9Sstevel@tonic-gate 
59634c06356bSdh142964 	if (state_p == NULL || size == 0)
59647c478bd9Sstevel@tonic-gate 		return (EINVAL);
59657c478bd9Sstevel@tonic-gate 
59667c478bd9Sstevel@tonic-gate 	ss = kmem_zalloc(sizeof (*ss), KM_SLEEP);
59677c478bd9Sstevel@tonic-gate 	mutex_init(&ss->lock, NULL, MUTEX_DRIVER, NULL);
59687c478bd9Sstevel@tonic-gate 	ss->size = size;
59697c478bd9Sstevel@tonic-gate 
59707c478bd9Sstevel@tonic-gate 	if (n_items < MIN_N_ITEMS)
59717c478bd9Sstevel@tonic-gate 		ss->n_items = MIN_N_ITEMS;
59727c478bd9Sstevel@tonic-gate 	else {
59737c478bd9Sstevel@tonic-gate 		int bitlog;
59747c478bd9Sstevel@tonic-gate 
59757c478bd9Sstevel@tonic-gate 		if ((bitlog = ddi_fls(n_items)) == ddi_ffs(n_items))
59767c478bd9Sstevel@tonic-gate 			bitlog--;
59777c478bd9Sstevel@tonic-gate 		ss->n_items = 1 << bitlog;
59787c478bd9Sstevel@tonic-gate 	}
59797c478bd9Sstevel@tonic-gate 
59807c478bd9Sstevel@tonic-gate 	ASSERT(ss->n_items >= n_items);
59817c478bd9Sstevel@tonic-gate 
59827c478bd9Sstevel@tonic-gate 	ss->array = kmem_zalloc(ss->n_items * sizeof (void *), KM_SLEEP);
59837c478bd9Sstevel@tonic-gate 
59847c478bd9Sstevel@tonic-gate 	*state_p = ss;
59857c478bd9Sstevel@tonic-gate 	return (0);
59867c478bd9Sstevel@tonic-gate }
59877c478bd9Sstevel@tonic-gate 
59887c478bd9Sstevel@tonic-gate /*
59897c478bd9Sstevel@tonic-gate  * Allocate a state structure of size 'size' to be associated
59907c478bd9Sstevel@tonic-gate  * with item 'item'.
59917c478bd9Sstevel@tonic-gate  *
59927c478bd9Sstevel@tonic-gate  * In this implementation, the array is extended to
59937c478bd9Sstevel@tonic-gate  * allow the requested offset, if needed.
59947c478bd9Sstevel@tonic-gate  */
59957c478bd9Sstevel@tonic-gate int
59967c478bd9Sstevel@tonic-gate ddi_soft_state_zalloc(void *state, int item)
59977c478bd9Sstevel@tonic-gate {
59984c06356bSdh142964 	i_ddi_soft_state	*ss = (i_ddi_soft_state *)state;
59997c478bd9Sstevel@tonic-gate 	void			**array;
60007c478bd9Sstevel@tonic-gate 	void			*new_element;
60017c478bd9Sstevel@tonic-gate 
60024c06356bSdh142964 	if ((state == NULL) || (item < 0))
60037c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
60047c478bd9Sstevel@tonic-gate 
60057c478bd9Sstevel@tonic-gate 	mutex_enter(&ss->lock);
60067c478bd9Sstevel@tonic-gate 	if (ss->size == 0) {
60077c478bd9Sstevel@tonic-gate 		mutex_exit(&ss->lock);
60087c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "ddi_soft_state_zalloc: bad handle: %s",
60097c478bd9Sstevel@tonic-gate 		    mod_containing_pc(caller()));
60107c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
60117c478bd9Sstevel@tonic-gate 	}
60127c478bd9Sstevel@tonic-gate 
60137c478bd9Sstevel@tonic-gate 	array = ss->array;	/* NULL if ss->n_items == 0 */
60147c478bd9Sstevel@tonic-gate 	ASSERT(ss->n_items != 0 && array != NULL);
60157c478bd9Sstevel@tonic-gate 
60167c478bd9Sstevel@tonic-gate 	/*
60177c478bd9Sstevel@tonic-gate 	 * refuse to tread on an existing element
60187c478bd9Sstevel@tonic-gate 	 */
60197c478bd9Sstevel@tonic-gate 	if (item < ss->n_items && array[item] != NULL) {
60207c478bd9Sstevel@tonic-gate 		mutex_exit(&ss->lock);
60217c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
60227c478bd9Sstevel@tonic-gate 	}
60237c478bd9Sstevel@tonic-gate 
60247c478bd9Sstevel@tonic-gate 	/*
60257c478bd9Sstevel@tonic-gate 	 * Allocate a new element to plug in
60267c478bd9Sstevel@tonic-gate 	 */
60277c478bd9Sstevel@tonic-gate 	new_element = kmem_zalloc(ss->size, KM_SLEEP);
60287c478bd9Sstevel@tonic-gate 
60297c478bd9Sstevel@tonic-gate 	/*
60307c478bd9Sstevel@tonic-gate 	 * Check if the array is big enough, if not, grow it.
60317c478bd9Sstevel@tonic-gate 	 */
60327c478bd9Sstevel@tonic-gate 	if (item >= ss->n_items) {
60337c478bd9Sstevel@tonic-gate 		void			**new_array;
60347c478bd9Sstevel@tonic-gate 		size_t			new_n_items;
60357c478bd9Sstevel@tonic-gate 		struct i_ddi_soft_state	*dirty;
60367c478bd9Sstevel@tonic-gate 
60377c478bd9Sstevel@tonic-gate 		/*
60387c478bd9Sstevel@tonic-gate 		 * Allocate a new array of the right length, copy
60397c478bd9Sstevel@tonic-gate 		 * all the old pointers to the new array, then
60407c478bd9Sstevel@tonic-gate 		 * if it exists at all, put the old array on the
60417c478bd9Sstevel@tonic-gate 		 * dirty list.
60427c478bd9Sstevel@tonic-gate 		 *
60437c478bd9Sstevel@tonic-gate 		 * Note that we can't kmem_free() the old array.
60447c478bd9Sstevel@tonic-gate 		 *
60457c478bd9Sstevel@tonic-gate 		 * Why -- well the 'get' operation is 'mutex-free', so we
60467c478bd9Sstevel@tonic-gate 		 * can't easily catch a suspended thread that is just about
60477c478bd9Sstevel@tonic-gate 		 * to dereference the array we just grew out of.  So we
60487c478bd9Sstevel@tonic-gate 		 * cons up a header and put it on a list of 'dirty'
60497c478bd9Sstevel@tonic-gate 		 * pointer arrays.  (Dirty in the sense that there may
60507c478bd9Sstevel@tonic-gate 		 * be suspended threads somewhere that are in the middle
60517c478bd9Sstevel@tonic-gate 		 * of referencing them).  Fortunately, we -can- garbage
60527c478bd9Sstevel@tonic-gate 		 * collect it all at ddi_soft_state_fini time.
60537c478bd9Sstevel@tonic-gate 		 */
60547c478bd9Sstevel@tonic-gate 		new_n_items = ss->n_items;
60557c478bd9Sstevel@tonic-gate 		while (new_n_items < (1 + item))
60567c478bd9Sstevel@tonic-gate 			new_n_items <<= 1;	/* double array size .. */
60577c478bd9Sstevel@tonic-gate 
60587c478bd9Sstevel@tonic-gate 		ASSERT(new_n_items >= (1 + item));	/* sanity check! */
60597c478bd9Sstevel@tonic-gate 
60607c478bd9Sstevel@tonic-gate 		new_array = kmem_zalloc(new_n_items * sizeof (void *),
60617c478bd9Sstevel@tonic-gate 		    KM_SLEEP);
60627c478bd9Sstevel@tonic-gate 		/*
60637c478bd9Sstevel@tonic-gate 		 * Copy the pointers into the new array
60647c478bd9Sstevel@tonic-gate 		 */
60657c478bd9Sstevel@tonic-gate 		bcopy(array, new_array, ss->n_items * sizeof (void *));
60667c478bd9Sstevel@tonic-gate 
60677c478bd9Sstevel@tonic-gate 		/*
60687c478bd9Sstevel@tonic-gate 		 * Save the old array on the dirty list
60697c478bd9Sstevel@tonic-gate 		 */
60707c478bd9Sstevel@tonic-gate 		dirty = kmem_zalloc(sizeof (*dirty), KM_SLEEP);
60717c478bd9Sstevel@tonic-gate 		dirty->array = ss->array;
60727c478bd9Sstevel@tonic-gate 		dirty->n_items = ss->n_items;
60737c478bd9Sstevel@tonic-gate 		dirty->next = ss->next;
60747c478bd9Sstevel@tonic-gate 		ss->next = dirty;
60757c478bd9Sstevel@tonic-gate 
60767c478bd9Sstevel@tonic-gate 		ss->array = (array = new_array);
60777c478bd9Sstevel@tonic-gate 		ss->n_items = new_n_items;
60787c478bd9Sstevel@tonic-gate 	}
60797c478bd9Sstevel@tonic-gate 
60807c478bd9Sstevel@tonic-gate 	ASSERT(array != NULL && item < ss->n_items && array[item] == NULL);
60817c478bd9Sstevel@tonic-gate 
60827c478bd9Sstevel@tonic-gate 	array[item] = new_element;
60837c478bd9Sstevel@tonic-gate 
60847c478bd9Sstevel@tonic-gate 	mutex_exit(&ss->lock);
60857c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
60867c478bd9Sstevel@tonic-gate }
60877c478bd9Sstevel@tonic-gate 
60887c478bd9Sstevel@tonic-gate /*
60897c478bd9Sstevel@tonic-gate  * Fetch a pointer to the allocated soft state structure.
60907c478bd9Sstevel@tonic-gate  *
60917c478bd9Sstevel@tonic-gate  * This is designed to be cheap.
60927c478bd9Sstevel@tonic-gate  *
60937c478bd9Sstevel@tonic-gate  * There's an argument that there should be more checking for
60947c478bd9Sstevel@tonic-gate  * nil pointers and out of bounds on the array.. but we do a lot
60957c478bd9Sstevel@tonic-gate  * of that in the alloc/free routines.
60967c478bd9Sstevel@tonic-gate  *
60977c478bd9Sstevel@tonic-gate  * An array has the convenience that we don't need to lock read-access
60987c478bd9Sstevel@tonic-gate  * to it c.f. a linked list.  However our "expanding array" strategy
60997c478bd9Sstevel@tonic-gate  * means that we should hold a readers lock on the i_ddi_soft_state
61007c478bd9Sstevel@tonic-gate  * structure.
61017c478bd9Sstevel@tonic-gate  *
61027c478bd9Sstevel@tonic-gate  * However, from a performance viewpoint, we need to do it without
61037c478bd9Sstevel@tonic-gate  * any locks at all -- this also makes it a leaf routine.  The algorithm
61047c478bd9Sstevel@tonic-gate  * is 'lock-free' because we only discard the pointer arrays at
61057c478bd9Sstevel@tonic-gate  * ddi_soft_state_fini() time.
61067c478bd9Sstevel@tonic-gate  */
61077c478bd9Sstevel@tonic-gate void *
61087c478bd9Sstevel@tonic-gate ddi_get_soft_state(void *state, int item)
61097c478bd9Sstevel@tonic-gate {
61104c06356bSdh142964 	i_ddi_soft_state	*ss = (i_ddi_soft_state *)state;
61117c478bd9Sstevel@tonic-gate 
61124c06356bSdh142964 	ASSERT((ss != NULL) && (item >= 0));
61137c478bd9Sstevel@tonic-gate 
61147c478bd9Sstevel@tonic-gate 	if (item < ss->n_items && ss->array != NULL)
61157c478bd9Sstevel@tonic-gate 		return (ss->array[item]);
61167c478bd9Sstevel@tonic-gate 	return (NULL);
61177c478bd9Sstevel@tonic-gate }
61187c478bd9Sstevel@tonic-gate 
61197c478bd9Sstevel@tonic-gate /*
61207c478bd9Sstevel@tonic-gate  * Free the state structure corresponding to 'item.'   Freeing an
61217c478bd9Sstevel@tonic-gate  * element that has either gone or was never allocated is not
61227c478bd9Sstevel@tonic-gate  * considered an error.  Note that we free the state structure, but
61237c478bd9Sstevel@tonic-gate  * we don't shrink our pointer array, or discard 'dirty' arrays,
61247c478bd9Sstevel@tonic-gate  * since even a few pointers don't really waste too much memory.
61257c478bd9Sstevel@tonic-gate  *
61267c478bd9Sstevel@tonic-gate  * Passing an item number that is out of bounds, or a null pointer will
61277c478bd9Sstevel@tonic-gate  * provoke an error message.
61287c478bd9Sstevel@tonic-gate  */
61297c478bd9Sstevel@tonic-gate void
61307c478bd9Sstevel@tonic-gate ddi_soft_state_free(void *state, int item)
61317c478bd9Sstevel@tonic-gate {
61324c06356bSdh142964 	i_ddi_soft_state	*ss = (i_ddi_soft_state *)state;
61337c478bd9Sstevel@tonic-gate 	void			**array;
61347c478bd9Sstevel@tonic-gate 	void			*element;
61357c478bd9Sstevel@tonic-gate 	static char		msg[] = "ddi_soft_state_free:";
61367c478bd9Sstevel@tonic-gate 
61374c06356bSdh142964 	if (ss == NULL) {
61387c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s null handle: %s",
61397c478bd9Sstevel@tonic-gate 		    msg, mod_containing_pc(caller()));
61407c478bd9Sstevel@tonic-gate 		return;
61417c478bd9Sstevel@tonic-gate 	}
61427c478bd9Sstevel@tonic-gate 
61437c478bd9Sstevel@tonic-gate 	element = NULL;
61447c478bd9Sstevel@tonic-gate 
61457c478bd9Sstevel@tonic-gate 	mutex_enter(&ss->lock);
61467c478bd9Sstevel@tonic-gate 
61477c478bd9Sstevel@tonic-gate 	if ((array = ss->array) == NULL || ss->size == 0) {
61487c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s bad handle: %s",
61497c478bd9Sstevel@tonic-gate 		    msg, mod_containing_pc(caller()));
61507c478bd9Sstevel@tonic-gate 	} else if (item < 0 || item >= ss->n_items) {
61517c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s item %d not in range [0..%lu]: %s",
61527c478bd9Sstevel@tonic-gate 		    msg, item, ss->n_items - 1, mod_containing_pc(caller()));
61537c478bd9Sstevel@tonic-gate 	} else if (array[item] != NULL) {
61547c478bd9Sstevel@tonic-gate 		element = array[item];
61557c478bd9Sstevel@tonic-gate 		array[item] = NULL;
61567c478bd9Sstevel@tonic-gate 	}
61577c478bd9Sstevel@tonic-gate 
61587c478bd9Sstevel@tonic-gate 	mutex_exit(&ss->lock);
61597c478bd9Sstevel@tonic-gate 
61607c478bd9Sstevel@tonic-gate 	if (element)
61617c478bd9Sstevel@tonic-gate 		kmem_free(element, ss->size);
61627c478bd9Sstevel@tonic-gate }
61637c478bd9Sstevel@tonic-gate 
61647c478bd9Sstevel@tonic-gate /*
61657c478bd9Sstevel@tonic-gate  * Free the entire set of pointers, and any
61667c478bd9Sstevel@tonic-gate  * soft state structures contained therein.
61677c478bd9Sstevel@tonic-gate  *
61687c478bd9Sstevel@tonic-gate  * Note that we don't grab the ss->lock mutex, even though
61697c478bd9Sstevel@tonic-gate  * we're inspecting the various fields of the data structure.
61707c478bd9Sstevel@tonic-gate  *
61717c478bd9Sstevel@tonic-gate  * There is an implicit assumption that this routine will
61727c478bd9Sstevel@tonic-gate  * never run concurrently with any of the above on this
61737c478bd9Sstevel@tonic-gate  * particular state structure i.e. by the time the driver
61747c478bd9Sstevel@tonic-gate  * calls this routine, there should be no other threads
61757c478bd9Sstevel@tonic-gate  * running in the driver.
61767c478bd9Sstevel@tonic-gate  */
61777c478bd9Sstevel@tonic-gate void
61787c478bd9Sstevel@tonic-gate ddi_soft_state_fini(void **state_p)
61797c478bd9Sstevel@tonic-gate {
61804c06356bSdh142964 	i_ddi_soft_state	*ss, *dirty;
61817c478bd9Sstevel@tonic-gate 	int			item;
61827c478bd9Sstevel@tonic-gate 	static char		msg[] = "ddi_soft_state_fini:";
61837c478bd9Sstevel@tonic-gate 
61844c06356bSdh142964 	if (state_p == NULL ||
61854c06356bSdh142964 	    (ss = (i_ddi_soft_state *)(*state_p)) == NULL) {
61867c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s null handle: %s",
61877c478bd9Sstevel@tonic-gate 		    msg, mod_containing_pc(caller()));
61887c478bd9Sstevel@tonic-gate 		return;
61897c478bd9Sstevel@tonic-gate 	}
61907c478bd9Sstevel@tonic-gate 
61917c478bd9Sstevel@tonic-gate 	if (ss->size == 0) {
61927c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s bad handle: %s",
61937c478bd9Sstevel@tonic-gate 		    msg, mod_containing_pc(caller()));
61947c478bd9Sstevel@tonic-gate 		return;
61957c478bd9Sstevel@tonic-gate 	}
61967c478bd9Sstevel@tonic-gate 
61977c478bd9Sstevel@tonic-gate 	if (ss->n_items > 0) {
61987c478bd9Sstevel@tonic-gate 		for (item = 0; item < ss->n_items; item++)
61997c478bd9Sstevel@tonic-gate 			ddi_soft_state_free(ss, item);
62007c478bd9Sstevel@tonic-gate 		kmem_free(ss->array, ss->n_items * sizeof (void *));
62017c478bd9Sstevel@tonic-gate 	}
62027c478bd9Sstevel@tonic-gate 
62037c478bd9Sstevel@tonic-gate 	/*
62047c478bd9Sstevel@tonic-gate 	 * Now delete any dirty arrays from previous 'grow' operations
62057c478bd9Sstevel@tonic-gate 	 */
62067c478bd9Sstevel@tonic-gate 	for (dirty = ss->next; dirty; dirty = ss->next) {
62077c478bd9Sstevel@tonic-gate 		ss->next = dirty->next;
62087c478bd9Sstevel@tonic-gate 		kmem_free(dirty->array, dirty->n_items * sizeof (void *));
62097c478bd9Sstevel@tonic-gate 		kmem_free(dirty, sizeof (*dirty));
62107c478bd9Sstevel@tonic-gate 	}
62117c478bd9Sstevel@tonic-gate 
62127c478bd9Sstevel@tonic-gate 	mutex_destroy(&ss->lock);
62137c478bd9Sstevel@tonic-gate 	kmem_free(ss, sizeof (*ss));
62147c478bd9Sstevel@tonic-gate 
62157c478bd9Sstevel@tonic-gate 	*state_p = NULL;
62167c478bd9Sstevel@tonic-gate }
62177c478bd9Sstevel@tonic-gate 
62184c06356bSdh142964 #define	SS_N_ITEMS_PER_HASH	16
62194c06356bSdh142964 #define	SS_MIN_HASH_SZ		16
62204c06356bSdh142964 #define	SS_MAX_HASH_SZ		4096
62214c06356bSdh142964 
62224c06356bSdh142964 int
62234c06356bSdh142964 ddi_soft_state_bystr_init(ddi_soft_state_bystr **state_p, size_t size,
62244c06356bSdh142964     int n_items)
62254c06356bSdh142964 {
62264c06356bSdh142964 	i_ddi_soft_state_bystr	*sss;
62274c06356bSdh142964 	int			hash_sz;
62284c06356bSdh142964 
62294c06356bSdh142964 	ASSERT(state_p && size && n_items);
62304c06356bSdh142964 	if ((state_p == NULL) || (size == 0) || (n_items == 0))
62314c06356bSdh142964 		return (EINVAL);
62324c06356bSdh142964 
62334c06356bSdh142964 	/* current implementation is based on hash, convert n_items to hash */
62344c06356bSdh142964 	hash_sz = n_items / SS_N_ITEMS_PER_HASH;
62354c06356bSdh142964 	if (hash_sz < SS_MIN_HASH_SZ)
62364c06356bSdh142964 		hash_sz = SS_MIN_HASH_SZ;
62374c06356bSdh142964 	else if (hash_sz > SS_MAX_HASH_SZ)
62384c06356bSdh142964 		hash_sz = SS_MAX_HASH_SZ;
62394c06356bSdh142964 
62404c06356bSdh142964 	/* allocate soft_state pool */
62414c06356bSdh142964 	sss = kmem_zalloc(sizeof (*sss), KM_SLEEP);
62424c06356bSdh142964 	sss->ss_size = size;
62434c06356bSdh142964 	sss->ss_mod_hash = mod_hash_create_strhash("soft_state_bystr",
62444c06356bSdh142964 	    hash_sz, mod_hash_null_valdtor);
62454c06356bSdh142964 	*state_p = (ddi_soft_state_bystr *)sss;
62464c06356bSdh142964 	return (0);
62474c06356bSdh142964 }
62484c06356bSdh142964 
62494c06356bSdh142964 int
62504c06356bSdh142964 ddi_soft_state_bystr_zalloc(ddi_soft_state_bystr *state, const char *str)
62514c06356bSdh142964 {
62524c06356bSdh142964 	i_ddi_soft_state_bystr	*sss = (i_ddi_soft_state_bystr *)state;
62534c06356bSdh142964 	void			*sso;
62544c06356bSdh142964 	char			*dup_str;
62554c06356bSdh142964 
62564c06356bSdh142964 	ASSERT(sss && str && sss->ss_mod_hash);
62574c06356bSdh142964 	if ((sss == NULL) || (str == NULL) || (sss->ss_mod_hash == NULL))
62584c06356bSdh142964 		return (DDI_FAILURE);
62594c06356bSdh142964 	sso = kmem_zalloc(sss->ss_size, KM_SLEEP);
62604c06356bSdh142964 	dup_str = i_ddi_strdup((char *)str, KM_SLEEP);
62614c06356bSdh142964 	if (mod_hash_insert(sss->ss_mod_hash,
62624c06356bSdh142964 	    (mod_hash_key_t)dup_str, (mod_hash_val_t)sso) == 0)
62634c06356bSdh142964 		return (DDI_SUCCESS);
62644c06356bSdh142964 
62654c06356bSdh142964 	/*
62664c06356bSdh142964 	 * The only error from an strhash insert is caused by a duplicate key.
62674c06356bSdh142964 	 * We refuse to tread on an existing elements, so free and fail.
62684c06356bSdh142964 	 */
62694c06356bSdh142964 	kmem_free(dup_str, strlen(dup_str) + 1);
62704c06356bSdh142964 	kmem_free(sso, sss->ss_size);
62714c06356bSdh142964 	return (DDI_FAILURE);
62724c06356bSdh142964 }
62734c06356bSdh142964 
62744c06356bSdh142964 void *
62754c06356bSdh142964 ddi_soft_state_bystr_get(ddi_soft_state_bystr *state, const char *str)
62764c06356bSdh142964 {
62774c06356bSdh142964 	i_ddi_soft_state_bystr	*sss = (i_ddi_soft_state_bystr *)state;
62784c06356bSdh142964 	void			*sso;
62794c06356bSdh142964 
62804c06356bSdh142964 	ASSERT(sss && str && sss->ss_mod_hash);
62814c06356bSdh142964 	if ((sss == NULL) || (str == NULL) || (sss->ss_mod_hash == NULL))
62824c06356bSdh142964 		return (NULL);
62834c06356bSdh142964 
62844c06356bSdh142964 	if (mod_hash_find(sss->ss_mod_hash,
62854c06356bSdh142964 	    (mod_hash_key_t)str, (mod_hash_val_t *)&sso) == 0)
62864c06356bSdh142964 		return (sso);
62874c06356bSdh142964 	return (NULL);
62884c06356bSdh142964 }
62894c06356bSdh142964 
62904c06356bSdh142964 void
62914c06356bSdh142964 ddi_soft_state_bystr_free(ddi_soft_state_bystr *state, const char *str)
62924c06356bSdh142964 {
62934c06356bSdh142964 	i_ddi_soft_state_bystr	*sss = (i_ddi_soft_state_bystr *)state;
62944c06356bSdh142964 	void			*sso;
62954c06356bSdh142964 
62964c06356bSdh142964 	ASSERT(sss && str && sss->ss_mod_hash);
62974c06356bSdh142964 	if ((sss == NULL) || (str == NULL) || (sss->ss_mod_hash == NULL))
62984c06356bSdh142964 		return;
62994c06356bSdh142964 
63004c06356bSdh142964 	(void) mod_hash_remove(sss->ss_mod_hash,
63014c06356bSdh142964 	    (mod_hash_key_t)str, (mod_hash_val_t *)&sso);
63024c06356bSdh142964 	kmem_free(sso, sss->ss_size);
63034c06356bSdh142964 }
63044c06356bSdh142964 
63054c06356bSdh142964 void
63064c06356bSdh142964 ddi_soft_state_bystr_fini(ddi_soft_state_bystr **state_p)
63074c06356bSdh142964 {
63084c06356bSdh142964 	i_ddi_soft_state_bystr	*sss;
63094c06356bSdh142964 
63104c06356bSdh142964 	ASSERT(state_p);
63114c06356bSdh142964 	if (state_p == NULL)
63124c06356bSdh142964 		return;
63134c06356bSdh142964 
63144c06356bSdh142964 	sss = (i_ddi_soft_state_bystr *)(*state_p);
63154c06356bSdh142964 	if (sss == NULL)
63164c06356bSdh142964 		return;
63174c06356bSdh142964 
63184c06356bSdh142964 	ASSERT(sss->ss_mod_hash);
63194c06356bSdh142964 	if (sss->ss_mod_hash) {
63204c06356bSdh142964 		mod_hash_destroy_strhash(sss->ss_mod_hash);
63214c06356bSdh142964 		sss->ss_mod_hash = NULL;
63224c06356bSdh142964 	}
63234c06356bSdh142964 
63244c06356bSdh142964 	kmem_free(sss, sizeof (*sss));
63254c06356bSdh142964 	*state_p = NULL;
63264c06356bSdh142964 }
63274c06356bSdh142964 
63284c06356bSdh142964 /*
63294c06356bSdh142964  * The ddi_strid_* routines provide string-to-index management utilities.
63304c06356bSdh142964  */
63314c06356bSdh142964 /* allocate and initialize an strid set */
63324c06356bSdh142964 int
63334c06356bSdh142964 ddi_strid_init(ddi_strid **strid_p, int n_items)
63344c06356bSdh142964 {
63354c06356bSdh142964 	i_ddi_strid	*ss;
63364c06356bSdh142964 	int		hash_sz;
63374c06356bSdh142964 
63384c06356bSdh142964 	if (strid_p == NULL)
63394c06356bSdh142964 		return (DDI_FAILURE);
63404c06356bSdh142964 
63414c06356bSdh142964 	/* current implementation is based on hash, convert n_items to hash */
63424c06356bSdh142964 	hash_sz = n_items / SS_N_ITEMS_PER_HASH;
63434c06356bSdh142964 	if (hash_sz < SS_MIN_HASH_SZ)
63444c06356bSdh142964 		hash_sz = SS_MIN_HASH_SZ;
63454c06356bSdh142964 	else if (hash_sz > SS_MAX_HASH_SZ)
63464c06356bSdh142964 		hash_sz = SS_MAX_HASH_SZ;
63474c06356bSdh142964 
63484c06356bSdh142964 	ss = kmem_alloc(sizeof (*ss), KM_SLEEP);
63491b115575SJohn Danielson 	ss->strid_chunksz = n_items;
63501b115575SJohn Danielson 	ss->strid_spacesz = n_items;
63514c06356bSdh142964 	ss->strid_space = id_space_create("strid", 1, n_items);
63524c06356bSdh142964 	ss->strid_bystr = mod_hash_create_strhash("strid_bystr", hash_sz,
63534c06356bSdh142964 	    mod_hash_null_valdtor);
63544c06356bSdh142964 	ss->strid_byid = mod_hash_create_idhash("strid_byid", hash_sz,
63554c06356bSdh142964 	    mod_hash_null_valdtor);
63564c06356bSdh142964 	*strid_p = (ddi_strid *)ss;
63574c06356bSdh142964 	return (DDI_SUCCESS);
63584c06356bSdh142964 }
63594c06356bSdh142964 
63604c06356bSdh142964 /* allocate an id mapping within the specified set for str, return id */
63614c06356bSdh142964 static id_t
63621b115575SJohn Danielson i_ddi_strid_alloc(ddi_strid *strid, char *str)
63634c06356bSdh142964 {
63644c06356bSdh142964 	i_ddi_strid	*ss = (i_ddi_strid *)strid;
63654c06356bSdh142964 	id_t		id;
63664c06356bSdh142964 	char		*s;
63674c06356bSdh142964 
63684c06356bSdh142964 	ASSERT(ss && str);
63694c06356bSdh142964 	if ((ss == NULL) || (str == NULL))
63704c06356bSdh142964 		return (0);
63714c06356bSdh142964 
63724c06356bSdh142964 	/*
63734c06356bSdh142964 	 * Allocate an id using VM_FIRSTFIT in order to keep allocated id
63744c06356bSdh142964 	 * range as compressed as possible.  This is important to minimize
63754c06356bSdh142964 	 * the amount of space used when the id is used as a ddi_soft_state
63764c06356bSdh142964 	 * index by the caller.
63774c06356bSdh142964 	 *
63781b115575SJohn Danielson 	 * If the id list is exhausted, increase the size of the list
63791b115575SJohn Danielson 	 * by the chuck size specified in ddi_strid_init and reattempt
63801b115575SJohn Danielson 	 * the allocation
63814c06356bSdh142964 	 */
63821b115575SJohn Danielson 	if ((id = id_allocff_nosleep(ss->strid_space)) == (id_t)-1) {
63831b115575SJohn Danielson 		id_space_extend(ss->strid_space, ss->strid_spacesz,
63841b115575SJohn Danielson 		    ss->strid_spacesz + ss->strid_chunksz);
63851b115575SJohn Danielson 		ss->strid_spacesz += ss->strid_chunksz;
63861b115575SJohn Danielson 		if ((id = id_allocff_nosleep(ss->strid_space)) == (id_t)-1)
63874c06356bSdh142964 			return (0);
63884c06356bSdh142964 	}
63894c06356bSdh142964 
63904c06356bSdh142964 	/*
63914c06356bSdh142964 	 * NOTE: since we create and destroy in unison we can save space by
63924c06356bSdh142964 	 * using bystr key as the byid value.  This means destroy must occur
63934c06356bSdh142964 	 * in (byid, bystr) order.
63944c06356bSdh142964 	 */
63954c06356bSdh142964 	s = i_ddi_strdup(str, KM_SLEEP);
63964c06356bSdh142964 	if (mod_hash_insert(ss->strid_bystr, (mod_hash_key_t)s,
63974c06356bSdh142964 	    (mod_hash_val_t)(intptr_t)id) != 0) {
63984c06356bSdh142964 		ddi_strid_free(strid, id);
63994c06356bSdh142964 		return (0);
64004c06356bSdh142964 	}
64014c06356bSdh142964 	if (mod_hash_insert(ss->strid_byid, (mod_hash_key_t)(intptr_t)id,
64024c06356bSdh142964 	    (mod_hash_val_t)s) != 0) {
64034c06356bSdh142964 		ddi_strid_free(strid, id);
64044c06356bSdh142964 		return (0);
64054c06356bSdh142964 	}
64064c06356bSdh142964 
64074c06356bSdh142964 	/* NOTE: s if freed on mod_hash_destroy by mod_hash_strval_dtor */
64084c06356bSdh142964 	return (id);
64094c06356bSdh142964 }
64104c06356bSdh142964 
64114c06356bSdh142964 /* allocate an id mapping within the specified set for str, return id */
64124c06356bSdh142964 id_t
64134c06356bSdh142964 ddi_strid_alloc(ddi_strid *strid, char *str)
64144c06356bSdh142964 {
64151b115575SJohn Danielson 	return (i_ddi_strid_alloc(strid, str));
64164c06356bSdh142964 }
64174c06356bSdh142964 
64184c06356bSdh142964 /* return the id within the specified strid given the str */
64194c06356bSdh142964 id_t
64204c06356bSdh142964 ddi_strid_str2id(ddi_strid *strid, char *str)
64214c06356bSdh142964 {
64224c06356bSdh142964 	i_ddi_strid	*ss = (i_ddi_strid *)strid;
64234c06356bSdh142964 	id_t		id = 0;
64244c06356bSdh142964 	mod_hash_val_t	hv;
64254c06356bSdh142964 
64264c06356bSdh142964 	ASSERT(ss && str);
64274c06356bSdh142964 	if (ss && str && (mod_hash_find(ss->strid_bystr,
64284c06356bSdh142964 	    (mod_hash_key_t)str, &hv) == 0))
64294c06356bSdh142964 		id = (int)(intptr_t)hv;
64304c06356bSdh142964 	return (id);
64314c06356bSdh142964 }
64324c06356bSdh142964 
64334c06356bSdh142964 /* return str within the specified strid given the id */
64344c06356bSdh142964 char *
64354c06356bSdh142964 ddi_strid_id2str(ddi_strid *strid, id_t id)
64364c06356bSdh142964 {
64374c06356bSdh142964 	i_ddi_strid	*ss = (i_ddi_strid *)strid;
64384c06356bSdh142964 	char		*str = NULL;
64394c06356bSdh142964 	mod_hash_val_t	hv;
64404c06356bSdh142964 
64414c06356bSdh142964 	ASSERT(ss && id > 0);
64424c06356bSdh142964 	if (ss && (id > 0) && (mod_hash_find(ss->strid_byid,
64434c06356bSdh142964 	    (mod_hash_key_t)(uintptr_t)id, &hv) == 0))
64444c06356bSdh142964 		str = (char *)hv;
64454c06356bSdh142964 	return (str);
64464c06356bSdh142964 }
64474c06356bSdh142964 
64484c06356bSdh142964 /* free the id mapping within the specified strid */
64494c06356bSdh142964 void
64504c06356bSdh142964 ddi_strid_free(ddi_strid *strid, id_t id)
64514c06356bSdh142964 {
64524c06356bSdh142964 	i_ddi_strid	*ss = (i_ddi_strid *)strid;
64534c06356bSdh142964 	char		*str;
64544c06356bSdh142964 
64554c06356bSdh142964 	ASSERT(ss && id > 0);
64564c06356bSdh142964 	if ((ss == NULL) || (id <= 0))
64574c06356bSdh142964 		return;
64584c06356bSdh142964 
64594c06356bSdh142964 	/* bystr key is byid value: destroy order must be (byid, bystr) */
64604c06356bSdh142964 	str = ddi_strid_id2str(strid, id);
64614c06356bSdh142964 	(void) mod_hash_destroy(ss->strid_byid, (mod_hash_key_t)(uintptr_t)id);
64624c06356bSdh142964 	id_free(ss->strid_space, id);
64634c06356bSdh142964 
64644c06356bSdh142964 	if (str)
64654c06356bSdh142964 		(void) mod_hash_destroy(ss->strid_bystr, (mod_hash_key_t)str);
64664c06356bSdh142964 }
64674c06356bSdh142964 
64684c06356bSdh142964 /* destroy the strid set */
64694c06356bSdh142964 void
64704c06356bSdh142964 ddi_strid_fini(ddi_strid **strid_p)
64714c06356bSdh142964 {
64724c06356bSdh142964 	i_ddi_strid	*ss;
64734c06356bSdh142964 
64744c06356bSdh142964 	ASSERT(strid_p);
64754c06356bSdh142964 	if (strid_p == NULL)
64764c06356bSdh142964 		return;
64774c06356bSdh142964 
64784c06356bSdh142964 	ss = (i_ddi_strid *)(*strid_p);
64794c06356bSdh142964 	if (ss == NULL)
64804c06356bSdh142964 		return;
64814c06356bSdh142964 
64824c06356bSdh142964 	/* bystr key is byid value: destroy order must be (byid, bystr) */
64834c06356bSdh142964 	if (ss->strid_byid)
64844c06356bSdh142964 		mod_hash_destroy_hash(ss->strid_byid);
64854c06356bSdh142964 	if (ss->strid_byid)
64864c06356bSdh142964 		mod_hash_destroy_hash(ss->strid_bystr);
64874c06356bSdh142964 	if (ss->strid_space)
64884c06356bSdh142964 		id_space_destroy(ss->strid_space);
64894c06356bSdh142964 	kmem_free(ss, sizeof (*ss));
64904c06356bSdh142964 	*strid_p = NULL;
64914c06356bSdh142964 }
64924c06356bSdh142964 
64937c478bd9Sstevel@tonic-gate /*
6494fe9fe9fbScth  * This sets the devi_addr entry in the dev_info structure 'dip' to 'name'.
6495fe9fe9fbScth  * Storage is double buffered to prevent updates during devi_addr use -
6496fe9fe9fbScth  * double buffering is adaquate for reliable ddi_deviname() consumption.
6497fe9fe9fbScth  * The double buffer is not freed until dev_info structure destruction
6498fe9fe9fbScth  * (by i_ddi_free_node).
64997c478bd9Sstevel@tonic-gate  */
65007c478bd9Sstevel@tonic-gate void
65017c478bd9Sstevel@tonic-gate ddi_set_name_addr(dev_info_t *dip, char *name)
65027c478bd9Sstevel@tonic-gate {
6503fe9fe9fbScth 	char	*buf = DEVI(dip)->devi_addr_buf;
6504fe9fe9fbScth 	char	*newaddr;
65057c478bd9Sstevel@tonic-gate 
6506fe9fe9fbScth 	if (buf == NULL) {
6507fe9fe9fbScth 		buf = kmem_zalloc(2 * MAXNAMELEN, KM_SLEEP);
6508fe9fe9fbScth 		DEVI(dip)->devi_addr_buf = buf;
65097c478bd9Sstevel@tonic-gate 	}
6510fe9fe9fbScth 
6511fe9fe9fbScth 	if (name) {
6512fe9fe9fbScth 		ASSERT(strlen(name) < MAXNAMELEN);
6513fe9fe9fbScth 		newaddr = (DEVI(dip)->devi_addr == buf) ?
6514fe9fe9fbScth 		    (buf + MAXNAMELEN) : buf;
6515fe9fe9fbScth 		(void) strlcpy(newaddr, name, MAXNAMELEN);
6516fe9fe9fbScth 	} else
6517fe9fe9fbScth 		newaddr = NULL;
6518fe9fe9fbScth 
6519fe9fe9fbScth 	DEVI(dip)->devi_addr = newaddr;
65207c478bd9Sstevel@tonic-gate }
65217c478bd9Sstevel@tonic-gate 
65227c478bd9Sstevel@tonic-gate char *
65237c478bd9Sstevel@tonic-gate ddi_get_name_addr(dev_info_t *dip)
65247c478bd9Sstevel@tonic-gate {
65257c478bd9Sstevel@tonic-gate 	return (DEVI(dip)->devi_addr);
65267c478bd9Sstevel@tonic-gate }
65277c478bd9Sstevel@tonic-gate 
65287c478bd9Sstevel@tonic-gate void
65297c478bd9Sstevel@tonic-gate ddi_set_parent_data(dev_info_t *dip, void *pd)
65307c478bd9Sstevel@tonic-gate {
65317c478bd9Sstevel@tonic-gate 	DEVI(dip)->devi_parent_data = pd;
65327c478bd9Sstevel@tonic-gate }
65337c478bd9Sstevel@tonic-gate 
65347c478bd9Sstevel@tonic-gate void *
65357c478bd9Sstevel@tonic-gate ddi_get_parent_data(dev_info_t *dip)
65367c478bd9Sstevel@tonic-gate {
65377c478bd9Sstevel@tonic-gate 	return (DEVI(dip)->devi_parent_data);
65387c478bd9Sstevel@tonic-gate }
65397c478bd9Sstevel@tonic-gate 
65407c478bd9Sstevel@tonic-gate /*
65415c066ec2SJerry Gilliam  * ddi_name_to_major: returns the major number of a named module,
65425c066ec2SJerry Gilliam  * derived from the current driver alias binding.
65435c066ec2SJerry Gilliam  *
65445c066ec2SJerry Gilliam  * Caveat: drivers should avoid the use of this function, in particular
65455c066ec2SJerry Gilliam  * together with ddi_get_name/ddi_binding name, as per
65465c066ec2SJerry Gilliam  *	major = ddi_name_to_major(ddi_get_name(devi));
65475c066ec2SJerry Gilliam  * ddi_name_to_major() relies on the state of the device/alias binding,
65485c066ec2SJerry Gilliam  * which can and does change dynamically as aliases are administered
65495c066ec2SJerry Gilliam  * over time.  An attached device instance cannot rely on the major
65505c066ec2SJerry Gilliam  * number returned by ddi_name_to_major() to match its own major number.
65515c066ec2SJerry Gilliam  *
65525c066ec2SJerry Gilliam  * For driver use, ddi_driver_major() reliably returns the major number
65535c066ec2SJerry Gilliam  * for the module to which the device was bound at attach time over
65545c066ec2SJerry Gilliam  * the life of the instance.
65555c066ec2SJerry Gilliam  *	major = ddi_driver_major(dev_info_t *)
65567c478bd9Sstevel@tonic-gate  */
65577c478bd9Sstevel@tonic-gate major_t
65587c478bd9Sstevel@tonic-gate ddi_name_to_major(char *name)
65597c478bd9Sstevel@tonic-gate {
65607c478bd9Sstevel@tonic-gate 	return (mod_name_to_major(name));
65617c478bd9Sstevel@tonic-gate }
65627c478bd9Sstevel@tonic-gate 
65637c478bd9Sstevel@tonic-gate /*
65647c478bd9Sstevel@tonic-gate  * ddi_major_to_name: Returns the module name bound to a major number.
65657c478bd9Sstevel@tonic-gate  */
65667c478bd9Sstevel@tonic-gate char *
65677c478bd9Sstevel@tonic-gate ddi_major_to_name(major_t major)
65687c478bd9Sstevel@tonic-gate {
65697c478bd9Sstevel@tonic-gate 	return (mod_major_to_name(major));
65707c478bd9Sstevel@tonic-gate }
65717c478bd9Sstevel@tonic-gate 
65727c478bd9Sstevel@tonic-gate /*
65737c478bd9Sstevel@tonic-gate  * Return the name of the devinfo node pointed at by 'dip' in the buffer
65747c478bd9Sstevel@tonic-gate  * pointed at by 'name.'  A devinfo node is named as a result of calling
65757c478bd9Sstevel@tonic-gate  * ddi_initchild().
65767c478bd9Sstevel@tonic-gate  *
65777c478bd9Sstevel@tonic-gate  * Note: the driver must be held before calling this function!
65787c478bd9Sstevel@tonic-gate  */
65797c478bd9Sstevel@tonic-gate char *
65807c478bd9Sstevel@tonic-gate ddi_deviname(dev_info_t *dip, char *name)
65817c478bd9Sstevel@tonic-gate {
65827c478bd9Sstevel@tonic-gate 	char *addrname;
65837c478bd9Sstevel@tonic-gate 	char none = '\0';
65847c478bd9Sstevel@tonic-gate 
65857c478bd9Sstevel@tonic-gate 	if (dip == ddi_root_node()) {
65867c478bd9Sstevel@tonic-gate 		*name = '\0';
65877c478bd9Sstevel@tonic-gate 		return (name);
65887c478bd9Sstevel@tonic-gate 	}
65897c478bd9Sstevel@tonic-gate 
6590f4da9be0Scth 	if (i_ddi_node_state(dip) < DS_BOUND) {
65917c478bd9Sstevel@tonic-gate 		addrname = &none;
65927c478bd9Sstevel@tonic-gate 	} else {
6593f4da9be0Scth 		/*
6594f4da9be0Scth 		 * Use ddi_get_name_addr() without checking state so we get
6595f4da9be0Scth 		 * a unit-address if we are called after ddi_set_name_addr()
6596f4da9be0Scth 		 * by nexus DDI_CTL_INITCHILD code, but before completing
6597f4da9be0Scth 		 * node promotion to DS_INITIALIZED.  We currently have
6598f4da9be0Scth 		 * two situations where we are called in this state:
6599f4da9be0Scth 		 *   o  For framework processing of a path-oriented alias.
6600f4da9be0Scth 		 *   o  If a SCSA nexus driver calls ddi_devid_register()
6601f4da9be0Scth 		 *	from it's tran_tgt_init(9E) implementation.
6602f4da9be0Scth 		 */
66037c478bd9Sstevel@tonic-gate 		addrname = ddi_get_name_addr(dip);
6604f4da9be0Scth 		if (addrname == NULL)
6605f4da9be0Scth 			addrname = &none;
66067c478bd9Sstevel@tonic-gate 	}
66077c478bd9Sstevel@tonic-gate 
66087c478bd9Sstevel@tonic-gate 	if (*addrname == '\0') {
66097c478bd9Sstevel@tonic-gate 		(void) sprintf(name, "/%s", ddi_node_name(dip));
66107c478bd9Sstevel@tonic-gate 	} else {
66117c478bd9Sstevel@tonic-gate 		(void) sprintf(name, "/%s@%s", ddi_node_name(dip), addrname);
66127c478bd9Sstevel@tonic-gate 	}
66137c478bd9Sstevel@tonic-gate 
66147c478bd9Sstevel@tonic-gate 	return (name);
66157c478bd9Sstevel@tonic-gate }
66167c478bd9Sstevel@tonic-gate 
66177c478bd9Sstevel@tonic-gate /*
66187c478bd9Sstevel@tonic-gate  * Spits out the name of device node, typically name@addr, for a given node,
66197c478bd9Sstevel@tonic-gate  * using the driver name, not the nodename.
66207c478bd9Sstevel@tonic-gate  *
66217c478bd9Sstevel@tonic-gate  * Used by match_parent. Not to be used elsewhere.
66227c478bd9Sstevel@tonic-gate  */
66237c478bd9Sstevel@tonic-gate char *
66247c478bd9Sstevel@tonic-gate i_ddi_parname(dev_info_t *dip, char *name)
66257c478bd9Sstevel@tonic-gate {
66267c478bd9Sstevel@tonic-gate 	char *addrname;
66277c478bd9Sstevel@tonic-gate 
66287c478bd9Sstevel@tonic-gate 	if (dip == ddi_root_node()) {
66297c478bd9Sstevel@tonic-gate 		*name = '\0';
66307c478bd9Sstevel@tonic-gate 		return (name);
66317c478bd9Sstevel@tonic-gate 	}
66327c478bd9Sstevel@tonic-gate 
66337c478bd9Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(dip) >= DS_INITIALIZED);
66347c478bd9Sstevel@tonic-gate 
66357c478bd9Sstevel@tonic-gate 	if (*(addrname = ddi_get_name_addr(dip)) == '\0')
66367c478bd9Sstevel@tonic-gate 		(void) sprintf(name, "%s", ddi_binding_name(dip));
66377c478bd9Sstevel@tonic-gate 	else
66387c478bd9Sstevel@tonic-gate 		(void) sprintf(name, "%s@%s", ddi_binding_name(dip), addrname);
66397c478bd9Sstevel@tonic-gate 	return (name);
66407c478bd9Sstevel@tonic-gate }
66417c478bd9Sstevel@tonic-gate 
66427c478bd9Sstevel@tonic-gate static char *
66437c478bd9Sstevel@tonic-gate pathname_work(dev_info_t *dip, char *path)
66447c478bd9Sstevel@tonic-gate {
66457c478bd9Sstevel@tonic-gate 	char *bp;
66467c478bd9Sstevel@tonic-gate 
66477c478bd9Sstevel@tonic-gate 	if (dip == ddi_root_node()) {
66487c478bd9Sstevel@tonic-gate 		*path = '\0';
66497c478bd9Sstevel@tonic-gate 		return (path);
66507c478bd9Sstevel@tonic-gate 	}
66517c478bd9Sstevel@tonic-gate 	(void) pathname_work(ddi_get_parent(dip), path);
66527c478bd9Sstevel@tonic-gate 	bp = path + strlen(path);
66537c478bd9Sstevel@tonic-gate 	(void) ddi_deviname(dip, bp);
66547c478bd9Sstevel@tonic-gate 	return (path);
66557c478bd9Sstevel@tonic-gate }
66567c478bd9Sstevel@tonic-gate 
66577c478bd9Sstevel@tonic-gate char *
66587c478bd9Sstevel@tonic-gate ddi_pathname(dev_info_t *dip, char *path)
66597c478bd9Sstevel@tonic-gate {
66607c478bd9Sstevel@tonic-gate 	return (pathname_work(dip, path));
66617c478bd9Sstevel@tonic-gate }
66627c478bd9Sstevel@tonic-gate 
6663f9722deaSChris Horne char *
6664f9722deaSChris Horne ddi_pathname_minor(struct ddi_minor_data *dmdp, char *path)
6665f9722deaSChris Horne {
6666f9722deaSChris Horne 	if (dmdp->dip == NULL)
6667f9722deaSChris Horne 		*path = '\0';
6668f9722deaSChris Horne 	else {
6669f9722deaSChris Horne 		(void) ddi_pathname(dmdp->dip, path);
6670f9722deaSChris Horne 		if (dmdp->ddm_name) {
6671f9722deaSChris Horne 			(void) strcat(path, ":");
6672f9722deaSChris Horne 			(void) strcat(path, dmdp->ddm_name);
6673f9722deaSChris Horne 		}
6674f9722deaSChris Horne 	}
6675f9722deaSChris Horne 	return (path);
6676f9722deaSChris Horne }
6677f9722deaSChris Horne 
667838c67cbdSjiang wu - Sun Microsystems - Beijing China static char *
667938c67cbdSjiang wu - Sun Microsystems - Beijing China pathname_work_obp(dev_info_t *dip, char *path)
668038c67cbdSjiang wu - Sun Microsystems - Beijing China {
668138c67cbdSjiang wu - Sun Microsystems - Beijing China 	char *bp;
668238c67cbdSjiang wu - Sun Microsystems - Beijing China 	char *obp_path;
668338c67cbdSjiang wu - Sun Microsystems - Beijing China 
668438c67cbdSjiang wu - Sun Microsystems - Beijing China 	/*
668538c67cbdSjiang wu - Sun Microsystems - Beijing China 	 * look up the "obp-path" property, return the path if it exists
668638c67cbdSjiang wu - Sun Microsystems - Beijing China 	 */
668738c67cbdSjiang wu - Sun Microsystems - Beijing China 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
668838c67cbdSjiang wu - Sun Microsystems - Beijing China 	    "obp-path", &obp_path) == DDI_PROP_SUCCESS) {
668938c67cbdSjiang wu - Sun Microsystems - Beijing China 		(void) strcpy(path, obp_path);
669038c67cbdSjiang wu - Sun Microsystems - Beijing China 		ddi_prop_free(obp_path);
669138c67cbdSjiang wu - Sun Microsystems - Beijing China 		return (path);
669238c67cbdSjiang wu - Sun Microsystems - Beijing China 	}
669338c67cbdSjiang wu - Sun Microsystems - Beijing China 
669438c67cbdSjiang wu - Sun Microsystems - Beijing China 	/*
669538c67cbdSjiang wu - Sun Microsystems - Beijing China 	 * stop at root, no obp path
669638c67cbdSjiang wu - Sun Microsystems - Beijing China 	 */
669738c67cbdSjiang wu - Sun Microsystems - Beijing China 	if (dip == ddi_root_node()) {
669838c67cbdSjiang wu - Sun Microsystems - Beijing China 		return (NULL);
669938c67cbdSjiang wu - Sun Microsystems - Beijing China 	}
670038c67cbdSjiang wu - Sun Microsystems - Beijing China 
670138c67cbdSjiang wu - Sun Microsystems - Beijing China 	obp_path = pathname_work_obp(ddi_get_parent(dip), path);
670238c67cbdSjiang wu - Sun Microsystems - Beijing China 	if (obp_path == NULL)
670338c67cbdSjiang wu - Sun Microsystems - Beijing China 		return (NULL);
670438c67cbdSjiang wu - Sun Microsystems - Beijing China 
670538c67cbdSjiang wu - Sun Microsystems - Beijing China 	/*
670638c67cbdSjiang wu - Sun Microsystems - Beijing China 	 * append our component to parent's obp path
670738c67cbdSjiang wu - Sun Microsystems - Beijing China 	 */
670838c67cbdSjiang wu - Sun Microsystems - Beijing China 	bp = path + strlen(path);
670938c67cbdSjiang wu - Sun Microsystems - Beijing China 	if (*(bp - 1) != '/')
671038c67cbdSjiang wu - Sun Microsystems - Beijing China 		(void) strcat(bp++, "/");
671138c67cbdSjiang wu - Sun Microsystems - Beijing China 	(void) ddi_deviname(dip, bp);
671238c67cbdSjiang wu - Sun Microsystems - Beijing China 	return (path);
671338c67cbdSjiang wu - Sun Microsystems - Beijing China }
671438c67cbdSjiang wu - Sun Microsystems - Beijing China 
671538c67cbdSjiang wu - Sun Microsystems - Beijing China /*
671638c67cbdSjiang wu - Sun Microsystems - Beijing China  * return the 'obp-path' based path for the given node, or NULL if the node
671738c67cbdSjiang wu - Sun Microsystems - Beijing China  * does not have a different obp path. NOTE: Unlike ddi_pathname, this
671838c67cbdSjiang wu - Sun Microsystems - Beijing China  * function can't be called from interrupt context (since we need to
671938c67cbdSjiang wu - Sun Microsystems - Beijing China  * lookup a string property).
672038c67cbdSjiang wu - Sun Microsystems - Beijing China  */
672138c67cbdSjiang wu - Sun Microsystems - Beijing China char *
672238c67cbdSjiang wu - Sun Microsystems - Beijing China ddi_pathname_obp(dev_info_t *dip, char *path)
672338c67cbdSjiang wu - Sun Microsystems - Beijing China {
672438c67cbdSjiang wu - Sun Microsystems - Beijing China 	ASSERT(!servicing_interrupt());
672538c67cbdSjiang wu - Sun Microsystems - Beijing China 	if (dip == NULL || path == NULL)
672638c67cbdSjiang wu - Sun Microsystems - Beijing China 		return (NULL);
672738c67cbdSjiang wu - Sun Microsystems - Beijing China 
672838c67cbdSjiang wu - Sun Microsystems - Beijing China 	/* split work into a separate function to aid debugging */
672938c67cbdSjiang wu - Sun Microsystems - Beijing China 	return (pathname_work_obp(dip, path));
673038c67cbdSjiang wu - Sun Microsystems - Beijing China }
673138c67cbdSjiang wu - Sun Microsystems - Beijing China 
673238c67cbdSjiang wu - Sun Microsystems - Beijing China int
673338c67cbdSjiang wu - Sun Microsystems - Beijing China ddi_pathname_obp_set(dev_info_t *dip, char *component)
673438c67cbdSjiang wu - Sun Microsystems - Beijing China {
673538c67cbdSjiang wu - Sun Microsystems - Beijing China 	dev_info_t *pdip;
6736caa9369fSjiang wu - Sun Microsystems - Beijing China 	char *obp_path = NULL;
6737caa9369fSjiang wu - Sun Microsystems - Beijing China 	int rc = DDI_FAILURE;
673838c67cbdSjiang wu - Sun Microsystems - Beijing China 
673938c67cbdSjiang wu - Sun Microsystems - Beijing China 	if (dip == NULL)
674038c67cbdSjiang wu - Sun Microsystems - Beijing China 		return (DDI_FAILURE);
6741caa9369fSjiang wu - Sun Microsystems - Beijing China 
6742caa9369fSjiang wu - Sun Microsystems - Beijing China 	obp_path = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
6743caa9369fSjiang wu - Sun Microsystems - Beijing China 
674438c67cbdSjiang wu - Sun Microsystems - Beijing China 	pdip = ddi_get_parent(dip);
674538c67cbdSjiang wu - Sun Microsystems - Beijing China 
674638c67cbdSjiang wu - Sun Microsystems - Beijing China 	if (ddi_pathname_obp(pdip, obp_path) == NULL) {
674738c67cbdSjiang wu - Sun Microsystems - Beijing China 		(void) ddi_pathname(pdip, obp_path);
674838c67cbdSjiang wu - Sun Microsystems - Beijing China 	}
674938c67cbdSjiang wu - Sun Microsystems - Beijing China 
675038c67cbdSjiang wu - Sun Microsystems - Beijing China 	if (component) {
6751caa9369fSjiang wu - Sun Microsystems - Beijing China 		(void) strncat(obp_path, "/", MAXPATHLEN);
6752caa9369fSjiang wu - Sun Microsystems - Beijing China 		(void) strncat(obp_path, component, MAXPATHLEN);
675338c67cbdSjiang wu - Sun Microsystems - Beijing China 	}
6754caa9369fSjiang wu - Sun Microsystems - Beijing China 	rc = ndi_prop_update_string(DDI_DEV_T_NONE, dip, "obp-path",
6755caa9369fSjiang wu - Sun Microsystems - Beijing China 	    obp_path);
6756caa9369fSjiang wu - Sun Microsystems - Beijing China 
6757caa9369fSjiang wu - Sun Microsystems - Beijing China 	if (obp_path)
6758caa9369fSjiang wu - Sun Microsystems - Beijing China 		kmem_free(obp_path, MAXPATHLEN);
6759caa9369fSjiang wu - Sun Microsystems - Beijing China 
6760caa9369fSjiang wu - Sun Microsystems - Beijing China 	return (rc);
676138c67cbdSjiang wu - Sun Microsystems - Beijing China }
676238c67cbdSjiang wu - Sun Microsystems - Beijing China 
67637c478bd9Sstevel@tonic-gate /*
67647c478bd9Sstevel@tonic-gate  * Given a dev_t, return the pathname of the corresponding device in the
67657c478bd9Sstevel@tonic-gate  * buffer pointed at by "path."  The buffer is assumed to be large enough
67667c478bd9Sstevel@tonic-gate  * to hold the pathname of the device (MAXPATHLEN).
67677c478bd9Sstevel@tonic-gate  *
67687c478bd9Sstevel@tonic-gate  * The pathname of a device is the pathname of the devinfo node to which
67697c478bd9Sstevel@tonic-gate  * the device "belongs," concatenated with the character ':' and the name
67707c478bd9Sstevel@tonic-gate  * of the minor node corresponding to the dev_t.  If spec_type is 0 then
67717c478bd9Sstevel@tonic-gate  * just the pathname of the devinfo node is returned without driving attach
67727c478bd9Sstevel@tonic-gate  * of that node.  For a non-zero spec_type, an attach is performed and a
67737c478bd9Sstevel@tonic-gate  * search of the minor list occurs.
67747c478bd9Sstevel@tonic-gate  *
67757c478bd9Sstevel@tonic-gate  * It is possible that the path associated with the dev_t is not
67767c478bd9Sstevel@tonic-gate  * currently available in the devinfo tree.  In order to have a
67777c478bd9Sstevel@tonic-gate  * dev_t, a device must have been discovered before, which means
67787c478bd9Sstevel@tonic-gate  * that the path is always in the instance tree.  The one exception
67797c478bd9Sstevel@tonic-gate  * to this is if the dev_t is associated with a pseudo driver, in
67807c478bd9Sstevel@tonic-gate  * which case the device must exist on the pseudo branch of the
67817c478bd9Sstevel@tonic-gate  * devinfo tree as a result of parsing .conf files.
67827c478bd9Sstevel@tonic-gate  */
67837c478bd9Sstevel@tonic-gate int
67847c478bd9Sstevel@tonic-gate ddi_dev_pathname(dev_t devt, int spec_type, char *path)
67857c478bd9Sstevel@tonic-gate {
6786b9ccdc5aScth 	int		circ;
67877c478bd9Sstevel@tonic-gate 	major_t		major = getmajor(devt);
67887c478bd9Sstevel@tonic-gate 	int		instance;
67897c478bd9Sstevel@tonic-gate 	dev_info_t	*dip;
67907c478bd9Sstevel@tonic-gate 	char		*minorname;
67917c478bd9Sstevel@tonic-gate 	char		*drvname;
67927c478bd9Sstevel@tonic-gate 
67937c478bd9Sstevel@tonic-gate 	if (major >= devcnt)
67947c478bd9Sstevel@tonic-gate 		goto fail;
67957c478bd9Sstevel@tonic-gate 	if (major == clone_major) {
67967c478bd9Sstevel@tonic-gate 		/* clone has no minor nodes, manufacture the path here */
67977c478bd9Sstevel@tonic-gate 		if ((drvname = ddi_major_to_name(getminor(devt))) == NULL)
67987c478bd9Sstevel@tonic-gate 			goto fail;
67997c478bd9Sstevel@tonic-gate 
68007c478bd9Sstevel@tonic-gate 		(void) snprintf(path, MAXPATHLEN, "%s:%s", CLONE_PATH, drvname);
68017c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
68027c478bd9Sstevel@tonic-gate 	}
68037c478bd9Sstevel@tonic-gate 
68047c478bd9Sstevel@tonic-gate 	/* extract instance from devt (getinfo(9E) DDI_INFO_DEVT2INSTANCE). */
68057c478bd9Sstevel@tonic-gate 	if ((instance = dev_to_instance(devt)) == -1)
68067c478bd9Sstevel@tonic-gate 		goto fail;
68077c478bd9Sstevel@tonic-gate 
68087c478bd9Sstevel@tonic-gate 	/* reconstruct the path given the major/instance */
68097c478bd9Sstevel@tonic-gate 	if (e_ddi_majorinstance_to_path(major, instance, path) != DDI_SUCCESS)
68107c478bd9Sstevel@tonic-gate 		goto fail;
68117c478bd9Sstevel@tonic-gate 
68127c478bd9Sstevel@tonic-gate 	/* if spec_type given we must drive attach and search minor nodes */
68137c478bd9Sstevel@tonic-gate 	if ((spec_type == S_IFCHR) || (spec_type == S_IFBLK)) {
68147c478bd9Sstevel@tonic-gate 		/* attach the path so we can search minors */
68157c478bd9Sstevel@tonic-gate 		if ((dip = e_ddi_hold_devi_by_path(path, 0)) == NULL)
68167c478bd9Sstevel@tonic-gate 			goto fail;
68177c478bd9Sstevel@tonic-gate 
68187c478bd9Sstevel@tonic-gate 		/* Add minorname to path. */
6819b9ccdc5aScth 		ndi_devi_enter(dip, &circ);
68207c478bd9Sstevel@tonic-gate 		minorname = i_ddi_devtspectype_to_minorname(dip,
68217c478bd9Sstevel@tonic-gate 		    devt, spec_type);
68227c478bd9Sstevel@tonic-gate 		if (minorname) {
68237c478bd9Sstevel@tonic-gate 			(void) strcat(path, ":");
68247c478bd9Sstevel@tonic-gate 			(void) strcat(path, minorname);
68257c478bd9Sstevel@tonic-gate 		}
6826b9ccdc5aScth 		ndi_devi_exit(dip, circ);
68277c478bd9Sstevel@tonic-gate 		ddi_release_devi(dip);
68287c478bd9Sstevel@tonic-gate 		if (minorname == NULL)
68297c478bd9Sstevel@tonic-gate 			goto fail;
68307c478bd9Sstevel@tonic-gate 	}
68317c478bd9Sstevel@tonic-gate 	ASSERT(strlen(path) < MAXPATHLEN);
68327c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
68337c478bd9Sstevel@tonic-gate 
68347c478bd9Sstevel@tonic-gate fail:	*path = 0;
68357c478bd9Sstevel@tonic-gate 	return (DDI_FAILURE);
68367c478bd9Sstevel@tonic-gate }
68377c478bd9Sstevel@tonic-gate 
68387c478bd9Sstevel@tonic-gate /*
68397c478bd9Sstevel@tonic-gate  * Given a major number and an instance, return the path.
68407c478bd9Sstevel@tonic-gate  * This interface does NOT drive attach.
68417c478bd9Sstevel@tonic-gate  */
68427c478bd9Sstevel@tonic-gate int
68437c478bd9Sstevel@tonic-gate e_ddi_majorinstance_to_path(major_t major, int instance, char *path)
68447c478bd9Sstevel@tonic-gate {
684505a69601Scth 	struct devnames *dnp;
68467c478bd9Sstevel@tonic-gate 	dev_info_t	*dip;
68477c478bd9Sstevel@tonic-gate 
684805a69601Scth 	if ((major >= devcnt) || (instance == -1)) {
68497c478bd9Sstevel@tonic-gate 		*path = 0;
68507c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
68517c478bd9Sstevel@tonic-gate 	}
685205a69601Scth 
685305a69601Scth 	/* look for the major/instance in the instance tree */
685405a69601Scth 	if (e_ddi_instance_majorinstance_to_path(major, instance,
685505a69601Scth 	    path) == DDI_SUCCESS) {
68567c478bd9Sstevel@tonic-gate 		ASSERT(strlen(path) < MAXPATHLEN);
68577c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
68587c478bd9Sstevel@tonic-gate 	}
68597c478bd9Sstevel@tonic-gate 
686005a69601Scth 	/*
686105a69601Scth 	 * Not in instance tree, find the instance on the per driver list and
686205a69601Scth 	 * construct path to instance via ddi_pathname(). This is how paths
686305a69601Scth 	 * down the 'pseudo' branch are constructed.
686405a69601Scth 	 */
686505a69601Scth 	dnp = &(devnamesp[major]);
686605a69601Scth 	LOCK_DEV_OPS(&(dnp->dn_lock));
686705a69601Scth 	for (dip = dnp->dn_head; dip;
686805a69601Scth 	    dip = (dev_info_t *)DEVI(dip)->devi_next) {
686905a69601Scth 		/* Skip if instance does not match. */
687005a69601Scth 		if (DEVI(dip)->devi_instance != instance)
687105a69601Scth 			continue;
687205a69601Scth 
687305a69601Scth 		/*
687405a69601Scth 		 * An ndi_hold_devi() does not prevent DS_INITIALIZED->DS_BOUND
687505a69601Scth 		 * node demotion, so it is not an effective way of ensuring
687605a69601Scth 		 * that the ddi_pathname result has a unit-address.  Instead,
687705a69601Scth 		 * we reverify the node state after calling ddi_pathname().
687805a69601Scth 		 */
687905a69601Scth 		if (i_ddi_node_state(dip) >= DS_INITIALIZED) {
688005a69601Scth 			(void) ddi_pathname(dip, path);
688105a69601Scth 			if (i_ddi_node_state(dip) < DS_INITIALIZED)
688205a69601Scth 				continue;
688305a69601Scth 			UNLOCK_DEV_OPS(&(dnp->dn_lock));
688405a69601Scth 			ASSERT(strlen(path) < MAXPATHLEN);
688505a69601Scth 			return (DDI_SUCCESS);
688605a69601Scth 		}
688705a69601Scth 	}
688805a69601Scth 	UNLOCK_DEV_OPS(&(dnp->dn_lock));
688905a69601Scth 
689005a69601Scth 	/* can't reconstruct the path */
689105a69601Scth 	*path = 0;
689205a69601Scth 	return (DDI_FAILURE);
689305a69601Scth }
68947c478bd9Sstevel@tonic-gate 
68957c478bd9Sstevel@tonic-gate #define	GLD_DRIVER_PPA "SUNW,gld_v0_ppa"
68967c478bd9Sstevel@tonic-gate 
68977c478bd9Sstevel@tonic-gate /*
68987c478bd9Sstevel@tonic-gate  * Given the dip for a network interface return the ppa for that interface.
68997c478bd9Sstevel@tonic-gate  *
69007c478bd9Sstevel@tonic-gate  * In all cases except GLD v0 drivers, the ppa == instance.
69017c478bd9Sstevel@tonic-gate  * In the case of GLD v0 drivers, the ppa is equal to the attach order.
69027c478bd9Sstevel@tonic-gate  * So for these drivers when the attach routine calls gld_register(),
69037c478bd9Sstevel@tonic-gate  * the GLD framework creates an integer property called "gld_driver_ppa"
69047c478bd9Sstevel@tonic-gate  * that can be queried here.
69057c478bd9Sstevel@tonic-gate  *
69067c478bd9Sstevel@tonic-gate  * The only time this function is used is when a system is booting over nfs.
69077c478bd9Sstevel@tonic-gate  * In this case the system has to resolve the pathname of the boot device
69087c478bd9Sstevel@tonic-gate  * to it's ppa.
69097c478bd9Sstevel@tonic-gate  */
69107c478bd9Sstevel@tonic-gate int
69117c478bd9Sstevel@tonic-gate i_ddi_devi_get_ppa(dev_info_t *dip)
69127c478bd9Sstevel@tonic-gate {
69137c478bd9Sstevel@tonic-gate 	return (ddi_prop_get_int(DDI_DEV_T_ANY, dip,
69147c478bd9Sstevel@tonic-gate 	    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
69157c478bd9Sstevel@tonic-gate 	    GLD_DRIVER_PPA, ddi_get_instance(dip)));
69167c478bd9Sstevel@tonic-gate }
69177c478bd9Sstevel@tonic-gate 
69187c478bd9Sstevel@tonic-gate /*
69197c478bd9Sstevel@tonic-gate  * i_ddi_devi_set_ppa() should only be called from gld_register()
69207c478bd9Sstevel@tonic-gate  * and only for GLD v0 drivers
69217c478bd9Sstevel@tonic-gate  */
69227c478bd9Sstevel@tonic-gate void
69237c478bd9Sstevel@tonic-gate i_ddi_devi_set_ppa(dev_info_t *dip, int ppa)
69247c478bd9Sstevel@tonic-gate {
69257c478bd9Sstevel@tonic-gate 	(void) e_ddi_prop_update_int(DDI_DEV_T_NONE, dip, GLD_DRIVER_PPA, ppa);
69267c478bd9Sstevel@tonic-gate }
69277c478bd9Sstevel@tonic-gate 
69287c478bd9Sstevel@tonic-gate 
69297c478bd9Sstevel@tonic-gate /*
69307c478bd9Sstevel@tonic-gate  * Private DDI Console bell functions.
69317c478bd9Sstevel@tonic-gate  */
69327c478bd9Sstevel@tonic-gate void
69337c478bd9Sstevel@tonic-gate ddi_ring_console_bell(clock_t duration)
69347c478bd9Sstevel@tonic-gate {
69357c478bd9Sstevel@tonic-gate 	if (ddi_console_bell_func != NULL)
69367c478bd9Sstevel@tonic-gate 		(*ddi_console_bell_func)(duration);
69377c478bd9Sstevel@tonic-gate }
69387c478bd9Sstevel@tonic-gate 
69397c478bd9Sstevel@tonic-gate void
69407c478bd9Sstevel@tonic-gate ddi_set_console_bell(void (*bellfunc)(clock_t duration))
69417c478bd9Sstevel@tonic-gate {
69427c478bd9Sstevel@tonic-gate 	ddi_console_bell_func = bellfunc;
69437c478bd9Sstevel@tonic-gate }
69447c478bd9Sstevel@tonic-gate 
69457c478bd9Sstevel@tonic-gate int
69467c478bd9Sstevel@tonic-gate ddi_dma_alloc_handle(dev_info_t *dip, ddi_dma_attr_t *attr,
69477c478bd9Sstevel@tonic-gate 	int (*waitfp)(caddr_t), caddr_t arg, ddi_dma_handle_t *handlep)
69487c478bd9Sstevel@tonic-gate {
69497c478bd9Sstevel@tonic-gate 	int (*funcp)() = ddi_dma_allochdl;
69507c478bd9Sstevel@tonic-gate 	ddi_dma_attr_t dma_attr;
69517c478bd9Sstevel@tonic-gate 	struct bus_ops *bop;
69527c478bd9Sstevel@tonic-gate 
69537c478bd9Sstevel@tonic-gate 	if (attr == (ddi_dma_attr_t *)0)
69547c478bd9Sstevel@tonic-gate 		return (DDI_DMA_BADATTR);
69557c478bd9Sstevel@tonic-gate 
69567c478bd9Sstevel@tonic-gate 	dma_attr = *attr;
69577c478bd9Sstevel@tonic-gate 
69587c478bd9Sstevel@tonic-gate 	bop = DEVI(dip)->devi_ops->devo_bus_ops;
69597c478bd9Sstevel@tonic-gate 	if (bop && bop->bus_dma_allochdl)
69607c478bd9Sstevel@tonic-gate 		funcp = bop->bus_dma_allochdl;
69617c478bd9Sstevel@tonic-gate 
69627c478bd9Sstevel@tonic-gate 	return ((*funcp)(dip, dip, &dma_attr, waitfp, arg, handlep));
69637c478bd9Sstevel@tonic-gate }
69647c478bd9Sstevel@tonic-gate 
69657c478bd9Sstevel@tonic-gate void
69667c478bd9Sstevel@tonic-gate ddi_dma_free_handle(ddi_dma_handle_t *handlep)
69677c478bd9Sstevel@tonic-gate {
69687c478bd9Sstevel@tonic-gate 	ddi_dma_handle_t h = *handlep;
69697c478bd9Sstevel@tonic-gate 	(void) ddi_dma_freehdl(HD, HD, h);
69707c478bd9Sstevel@tonic-gate }
69717c478bd9Sstevel@tonic-gate 
69727c478bd9Sstevel@tonic-gate static uintptr_t dma_mem_list_id = 0;
69737c478bd9Sstevel@tonic-gate 
69747c478bd9Sstevel@tonic-gate 
69757c478bd9Sstevel@tonic-gate int
69767c478bd9Sstevel@tonic-gate ddi_dma_mem_alloc(ddi_dma_handle_t handle, size_t length,
69777b93957cSeota 	ddi_device_acc_attr_t *accattrp, uint_t flags,
69787c478bd9Sstevel@tonic-gate 	int (*waitfp)(caddr_t), caddr_t arg, caddr_t *kaddrp,
69797c478bd9Sstevel@tonic-gate 	size_t *real_length, ddi_acc_handle_t *handlep)
69807c478bd9Sstevel@tonic-gate {
69817c478bd9Sstevel@tonic-gate 	ddi_dma_impl_t *hp = (ddi_dma_impl_t *)handle;
69827c478bd9Sstevel@tonic-gate 	dev_info_t *dip = hp->dmai_rdip;
69837c478bd9Sstevel@tonic-gate 	ddi_acc_hdl_t *ap;
69847c478bd9Sstevel@tonic-gate 	ddi_dma_attr_t *attrp = &hp->dmai_attr;
69857b93957cSeota 	uint_t sleepflag, xfermodes;
69867c478bd9Sstevel@tonic-gate 	int (*fp)(caddr_t);
69877c478bd9Sstevel@tonic-gate 	int rval;
69887c478bd9Sstevel@tonic-gate 
69897c478bd9Sstevel@tonic-gate 	if (waitfp == DDI_DMA_SLEEP)
69907c478bd9Sstevel@tonic-gate 		fp = (int (*)())KM_SLEEP;
69917c478bd9Sstevel@tonic-gate 	else if (waitfp == DDI_DMA_DONTWAIT)
69927c478bd9Sstevel@tonic-gate 		fp = (int (*)())KM_NOSLEEP;
69937c478bd9Sstevel@tonic-gate 	else
69947c478bd9Sstevel@tonic-gate 		fp = waitfp;
69957c478bd9Sstevel@tonic-gate 	*handlep = impl_acc_hdl_alloc(fp, arg);
69967c478bd9Sstevel@tonic-gate 	if (*handlep == NULL)
69977c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
69987c478bd9Sstevel@tonic-gate 
699983220025Seota 	/* check if the cache attributes are supported */
700083220025Seota 	if (i_ddi_check_cache_attr(flags) == B_FALSE)
70017b93957cSeota 		return (DDI_FAILURE);
70027b93957cSeota 
70037b93957cSeota 	/*
70047b93957cSeota 	 * Transfer the meaningful bits to xfermodes.
70057b93957cSeota 	 * Double-check if the 3rd party driver correctly sets the bits.
70067b93957cSeota 	 * If not, set DDI_DMA_STREAMING to keep compatibility.
70077b93957cSeota 	 */
70087b93957cSeota 	xfermodes = flags & (DDI_DMA_CONSISTENT | DDI_DMA_STREAMING);
70097b93957cSeota 	if (xfermodes == 0) {
70107b93957cSeota 		xfermodes = DDI_DMA_STREAMING;
70117b93957cSeota 	}
70127b93957cSeota 
70137c478bd9Sstevel@tonic-gate 	/*
70147c478bd9Sstevel@tonic-gate 	 * initialize the common elements of data access handle
70157c478bd9Sstevel@tonic-gate 	 */
70167c478bd9Sstevel@tonic-gate 	ap = impl_acc_hdl_get(*handlep);
70177c478bd9Sstevel@tonic-gate 	ap->ah_vers = VERS_ACCHDL;
70187c478bd9Sstevel@tonic-gate 	ap->ah_dip = dip;
70197c478bd9Sstevel@tonic-gate 	ap->ah_offset = 0;
70207c478bd9Sstevel@tonic-gate 	ap->ah_len = 0;
70217b93957cSeota 	ap->ah_xfermodes = flags;
70227c478bd9Sstevel@tonic-gate 	ap->ah_acc = *accattrp;
70237c478bd9Sstevel@tonic-gate 
70247c478bd9Sstevel@tonic-gate 	sleepflag = ((waitfp == DDI_DMA_SLEEP) ? 1 : 0);
70257c478bd9Sstevel@tonic-gate 	if (xfermodes == DDI_DMA_CONSISTENT) {
70267b93957cSeota 		rval = i_ddi_mem_alloc(dip, attrp, length, sleepflag,
70277b93957cSeota 		    flags, accattrp, kaddrp, NULL, ap);
70287c478bd9Sstevel@tonic-gate 		*real_length = length;
70297c478bd9Sstevel@tonic-gate 	} else {
70307b93957cSeota 		rval = i_ddi_mem_alloc(dip, attrp, length, sleepflag,
70317b93957cSeota 		    flags, accattrp, kaddrp, real_length, ap);
70327c478bd9Sstevel@tonic-gate 	}
70337c478bd9Sstevel@tonic-gate 	if (rval == DDI_SUCCESS) {
70347c478bd9Sstevel@tonic-gate 		ap->ah_len = (off_t)(*real_length);
70357c478bd9Sstevel@tonic-gate 		ap->ah_addr = *kaddrp;
70367c478bd9Sstevel@tonic-gate 	} else {
70377c478bd9Sstevel@tonic-gate 		impl_acc_hdl_free(*handlep);
70387c478bd9Sstevel@tonic-gate 		*handlep = (ddi_acc_handle_t)NULL;
70397c478bd9Sstevel@tonic-gate 		if (waitfp != DDI_DMA_SLEEP && waitfp != DDI_DMA_DONTWAIT) {
70407c478bd9Sstevel@tonic-gate 			ddi_set_callback(waitfp, arg, &dma_mem_list_id);
70417c478bd9Sstevel@tonic-gate 		}
70427c478bd9Sstevel@tonic-gate 		rval = DDI_FAILURE;
70437c478bd9Sstevel@tonic-gate 	}
70447c478bd9Sstevel@tonic-gate 	return (rval);
70457c478bd9Sstevel@tonic-gate }
70467c478bd9Sstevel@tonic-gate 
70477c478bd9Sstevel@tonic-gate void
70487c478bd9Sstevel@tonic-gate ddi_dma_mem_free(ddi_acc_handle_t *handlep)
70497c478bd9Sstevel@tonic-gate {
70507c478bd9Sstevel@tonic-gate 	ddi_acc_hdl_t *ap;
70517c478bd9Sstevel@tonic-gate 
70527c478bd9Sstevel@tonic-gate 	ap = impl_acc_hdl_get(*handlep);
70537c478bd9Sstevel@tonic-gate 	ASSERT(ap);
70547c478bd9Sstevel@tonic-gate 
70557b93957cSeota 	i_ddi_mem_free((caddr_t)ap->ah_addr, ap);
70567c478bd9Sstevel@tonic-gate 
70577c478bd9Sstevel@tonic-gate 	/*
70587c478bd9Sstevel@tonic-gate 	 * free the handle
70597c478bd9Sstevel@tonic-gate 	 */
70607c478bd9Sstevel@tonic-gate 	impl_acc_hdl_free(*handlep);
70617c478bd9Sstevel@tonic-gate 	*handlep = (ddi_acc_handle_t)NULL;
70627c478bd9Sstevel@tonic-gate 
70637c478bd9Sstevel@tonic-gate 	if (dma_mem_list_id != 0) {
70647c478bd9Sstevel@tonic-gate 		ddi_run_callback(&dma_mem_list_id);
70657c478bd9Sstevel@tonic-gate 	}
70667c478bd9Sstevel@tonic-gate }
70677c478bd9Sstevel@tonic-gate 
70687c478bd9Sstevel@tonic-gate int
70697c478bd9Sstevel@tonic-gate ddi_dma_buf_bind_handle(ddi_dma_handle_t handle, struct buf *bp,
70707c478bd9Sstevel@tonic-gate 	uint_t flags, int (*waitfp)(caddr_t), caddr_t arg,
70717c478bd9Sstevel@tonic-gate 	ddi_dma_cookie_t *cookiep, uint_t *ccountp)
70727c478bd9Sstevel@tonic-gate {
70737c478bd9Sstevel@tonic-gate 	ddi_dma_impl_t *hp = (ddi_dma_impl_t *)handle;
7074b51bbbf5SVikram Hegde 	dev_info_t *dip, *rdip;
70757c478bd9Sstevel@tonic-gate 	struct ddi_dma_req dmareq;
70767c478bd9Sstevel@tonic-gate 	int (*funcp)();
70777c478bd9Sstevel@tonic-gate 
70787c478bd9Sstevel@tonic-gate 	dmareq.dmar_flags = flags;
70797c478bd9Sstevel@tonic-gate 	dmareq.dmar_fp = waitfp;
70807c478bd9Sstevel@tonic-gate 	dmareq.dmar_arg = arg;
70817c478bd9Sstevel@tonic-gate 	dmareq.dmar_object.dmao_size = (uint_t)bp->b_bcount;
70827c478bd9Sstevel@tonic-gate 
70832d3bf78eScth 	if (bp->b_flags & B_PAGEIO) {
70847c478bd9Sstevel@tonic-gate 		dmareq.dmar_object.dmao_type = DMA_OTYP_PAGES;
70857c478bd9Sstevel@tonic-gate 		dmareq.dmar_object.dmao_obj.pp_obj.pp_pp = bp->b_pages;
70867c478bd9Sstevel@tonic-gate 		dmareq.dmar_object.dmao_obj.pp_obj.pp_offset =
70877c478bd9Sstevel@tonic-gate 		    (uint_t)(((uintptr_t)bp->b_un.b_addr) & MMU_PAGEOFFSET);
70887c478bd9Sstevel@tonic-gate 	} else {
70897c478bd9Sstevel@tonic-gate 		dmareq.dmar_object.dmao_obj.virt_obj.v_addr = bp->b_un.b_addr;
70902d3bf78eScth 		if (bp->b_flags & B_SHADOW) {
70917c478bd9Sstevel@tonic-gate 			dmareq.dmar_object.dmao_obj.virt_obj.v_priv =
70927c478bd9Sstevel@tonic-gate 			    bp->b_shadow;
70937c478bd9Sstevel@tonic-gate 			dmareq.dmar_object.dmao_type = DMA_OTYP_BUFVADDR;
70947c478bd9Sstevel@tonic-gate 		} else {
70957c478bd9Sstevel@tonic-gate 			dmareq.dmar_object.dmao_type =
70967c478bd9Sstevel@tonic-gate 			    (bp->b_flags & (B_PHYS | B_REMAPPED)) ?
70977c478bd9Sstevel@tonic-gate 			    DMA_OTYP_BUFVADDR : DMA_OTYP_VADDR;
70987c478bd9Sstevel@tonic-gate 			dmareq.dmar_object.dmao_obj.virt_obj.v_priv = NULL;
70997c478bd9Sstevel@tonic-gate 		}
71007c478bd9Sstevel@tonic-gate 
71017c478bd9Sstevel@tonic-gate 		/*
71027c478bd9Sstevel@tonic-gate 		 * If the buffer has no proc pointer, or the proc
71037c478bd9Sstevel@tonic-gate 		 * struct has the kernel address space, or the buffer has
71047c478bd9Sstevel@tonic-gate 		 * been marked B_REMAPPED (meaning that it is now
71057c478bd9Sstevel@tonic-gate 		 * mapped into the kernel's address space), then
71067c478bd9Sstevel@tonic-gate 		 * the address space is kas (kernel address space).
71077c478bd9Sstevel@tonic-gate 		 */
71082d3bf78eScth 		if ((bp->b_proc == NULL) || (bp->b_proc->p_as == &kas) ||
71092d3bf78eScth 		    (bp->b_flags & B_REMAPPED)) {
71107c478bd9Sstevel@tonic-gate 			dmareq.dmar_object.dmao_obj.virt_obj.v_as = 0;
71117c478bd9Sstevel@tonic-gate 		} else {
71127c478bd9Sstevel@tonic-gate 			dmareq.dmar_object.dmao_obj.virt_obj.v_as =
71137c478bd9Sstevel@tonic-gate 			    bp->b_proc->p_as;
71147c478bd9Sstevel@tonic-gate 		}
71157c478bd9Sstevel@tonic-gate 	}
71167c478bd9Sstevel@tonic-gate 
7117b51bbbf5SVikram Hegde 	dip = rdip = hp->dmai_rdip;
7118b51bbbf5SVikram Hegde 	if (dip != ddi_root_node())
7119b51bbbf5SVikram Hegde 		dip = (dev_info_t *)DEVI(dip)->devi_bus_dma_bindhdl;
7120b51bbbf5SVikram Hegde 	funcp = DEVI(rdip)->devi_bus_dma_bindfunc;
7121b51bbbf5SVikram Hegde 	return ((*funcp)(dip, rdip, handle, &dmareq, cookiep, ccountp));
71227c478bd9Sstevel@tonic-gate }
71237c478bd9Sstevel@tonic-gate 
71247c478bd9Sstevel@tonic-gate int
71257c478bd9Sstevel@tonic-gate ddi_dma_addr_bind_handle(ddi_dma_handle_t handle, struct as *as,
71267c478bd9Sstevel@tonic-gate 	caddr_t addr, size_t len, uint_t flags, int (*waitfp)(caddr_t),
71277c478bd9Sstevel@tonic-gate 	caddr_t arg, ddi_dma_cookie_t *cookiep, uint_t *ccountp)
71287c478bd9Sstevel@tonic-gate {
71297c478bd9Sstevel@tonic-gate 	ddi_dma_impl_t *hp = (ddi_dma_impl_t *)handle;
7130b51bbbf5SVikram Hegde 	dev_info_t *dip, *rdip;
71317c478bd9Sstevel@tonic-gate 	struct ddi_dma_req dmareq;
71327c478bd9Sstevel@tonic-gate 	int (*funcp)();
71337c478bd9Sstevel@tonic-gate 
71347c478bd9Sstevel@tonic-gate 	if (len == (uint_t)0) {
71357c478bd9Sstevel@tonic-gate 		return (DDI_DMA_NOMAPPING);
71367c478bd9Sstevel@tonic-gate 	}
71377c478bd9Sstevel@tonic-gate 	dmareq.dmar_flags = flags;
71387c478bd9Sstevel@tonic-gate 	dmareq.dmar_fp = waitfp;
71397c478bd9Sstevel@tonic-gate 	dmareq.dmar_arg = arg;
71407c478bd9Sstevel@tonic-gate 	dmareq.dmar_object.dmao_size = len;
71417c478bd9Sstevel@tonic-gate 	dmareq.dmar_object.dmao_type = DMA_OTYP_VADDR;
71427c478bd9Sstevel@tonic-gate 	dmareq.dmar_object.dmao_obj.virt_obj.v_as = as;
71437c478bd9Sstevel@tonic-gate 	dmareq.dmar_object.dmao_obj.virt_obj.v_addr = addr;
71447c478bd9Sstevel@tonic-gate 	dmareq.dmar_object.dmao_obj.virt_obj.v_priv = NULL;
71457c478bd9Sstevel@tonic-gate 
7146b51bbbf5SVikram Hegde 	dip = rdip = hp->dmai_rdip;
7147b51bbbf5SVikram Hegde 	if (dip != ddi_root_node())
7148b51bbbf5SVikram Hegde 		dip = (dev_info_t *)DEVI(dip)->devi_bus_dma_bindhdl;
7149b51bbbf5SVikram Hegde 	funcp = DEVI(rdip)->devi_bus_dma_bindfunc;
7150b51bbbf5SVikram Hegde 	return ((*funcp)(dip, rdip, handle, &dmareq, cookiep, ccountp));
71517c478bd9Sstevel@tonic-gate }
71527c478bd9Sstevel@tonic-gate 
71537c478bd9Sstevel@tonic-gate void
71547c478bd9Sstevel@tonic-gate ddi_dma_nextcookie(ddi_dma_handle_t handle, ddi_dma_cookie_t *cookiep)
71557c478bd9Sstevel@tonic-gate {
71567c478bd9Sstevel@tonic-gate 	ddi_dma_impl_t *hp = (ddi_dma_impl_t *)handle;
71577c478bd9Sstevel@tonic-gate 	ddi_dma_cookie_t *cp;
71587c478bd9Sstevel@tonic-gate 
71597c478bd9Sstevel@tonic-gate 	cp = hp->dmai_cookie;
71607c478bd9Sstevel@tonic-gate 	ASSERT(cp);
71617c478bd9Sstevel@tonic-gate 
71627c478bd9Sstevel@tonic-gate 	cookiep->dmac_notused = cp->dmac_notused;
71637c478bd9Sstevel@tonic-gate 	cookiep->dmac_type = cp->dmac_type;
71647c478bd9Sstevel@tonic-gate 	cookiep->dmac_address = cp->dmac_address;
71657c478bd9Sstevel@tonic-gate 	cookiep->dmac_size = cp->dmac_size;
71667c478bd9Sstevel@tonic-gate 	hp->dmai_cookie++;
71677c478bd9Sstevel@tonic-gate }
71687c478bd9Sstevel@tonic-gate 
71697c478bd9Sstevel@tonic-gate int
71707c478bd9Sstevel@tonic-gate ddi_dma_numwin(ddi_dma_handle_t handle, uint_t *nwinp)
71717c478bd9Sstevel@tonic-gate {
71727c478bd9Sstevel@tonic-gate 	ddi_dma_impl_t *hp = (ddi_dma_impl_t *)handle;
71737c478bd9Sstevel@tonic-gate 	if ((hp->dmai_rflags & DDI_DMA_PARTIAL) == 0) {
71747c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
71757c478bd9Sstevel@tonic-gate 	} else {
71767c478bd9Sstevel@tonic-gate 		*nwinp = hp->dmai_nwin;
71777c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
71787c478bd9Sstevel@tonic-gate 	}
71797c478bd9Sstevel@tonic-gate }
71807c478bd9Sstevel@tonic-gate 
71817c478bd9Sstevel@tonic-gate int
71827c478bd9Sstevel@tonic-gate ddi_dma_getwin(ddi_dma_handle_t h, uint_t win, off_t *offp,
71837c478bd9Sstevel@tonic-gate 	size_t *lenp, ddi_dma_cookie_t *cookiep, uint_t *ccountp)
71847c478bd9Sstevel@tonic-gate {
71857c478bd9Sstevel@tonic-gate 	int (*funcp)() = ddi_dma_win;
71867c478bd9Sstevel@tonic-gate 	struct bus_ops *bop;
71877c478bd9Sstevel@tonic-gate 
71887c478bd9Sstevel@tonic-gate 	bop = DEVI(HD)->devi_ops->devo_bus_ops;
71897c478bd9Sstevel@tonic-gate 	if (bop && bop->bus_dma_win)
71907c478bd9Sstevel@tonic-gate 		funcp = bop->bus_dma_win;
71917c478bd9Sstevel@tonic-gate 
71927c478bd9Sstevel@tonic-gate 	return ((*funcp)(HD, HD, h, win, offp, lenp, cookiep, ccountp));
71937c478bd9Sstevel@tonic-gate }
71947c478bd9Sstevel@tonic-gate 
71957c478bd9Sstevel@tonic-gate int
71967c478bd9Sstevel@tonic-gate ddi_dma_set_sbus64(ddi_dma_handle_t h, ulong_t burstsizes)
71977c478bd9Sstevel@tonic-gate {
71987c478bd9Sstevel@tonic-gate 	return (ddi_dma_mctl(HD, HD, h, DDI_DMA_SET_SBUS64, 0,
71997c478bd9Sstevel@tonic-gate 	    &burstsizes, 0, 0));
72007c478bd9Sstevel@tonic-gate }
72017c478bd9Sstevel@tonic-gate 
72027c478bd9Sstevel@tonic-gate int
72037c478bd9Sstevel@tonic-gate i_ddi_dma_fault_check(ddi_dma_impl_t *hp)
72047c478bd9Sstevel@tonic-gate {
72057c478bd9Sstevel@tonic-gate 	return (hp->dmai_fault);
72067c478bd9Sstevel@tonic-gate }
72077c478bd9Sstevel@tonic-gate 
72087c478bd9Sstevel@tonic-gate int
72097c478bd9Sstevel@tonic-gate ddi_check_dma_handle(ddi_dma_handle_t handle)
72107c478bd9Sstevel@tonic-gate {
72117c478bd9Sstevel@tonic-gate 	ddi_dma_impl_t *hp = (ddi_dma_impl_t *)handle;
72127c478bd9Sstevel@tonic-gate 	int (*check)(ddi_dma_impl_t *);
72137c478bd9Sstevel@tonic-gate 
72147c478bd9Sstevel@tonic-gate 	if ((check = hp->dmai_fault_check) == NULL)
72157c478bd9Sstevel@tonic-gate 		check = i_ddi_dma_fault_check;
72167c478bd9Sstevel@tonic-gate 
72177c478bd9Sstevel@tonic-gate 	return (((*check)(hp) == DDI_SUCCESS) ? DDI_SUCCESS : DDI_FAILURE);
72187c478bd9Sstevel@tonic-gate }
72197c478bd9Sstevel@tonic-gate 
72207c478bd9Sstevel@tonic-gate void
72217c478bd9Sstevel@tonic-gate i_ddi_dma_set_fault(ddi_dma_handle_t handle)
72227c478bd9Sstevel@tonic-gate {
72237c478bd9Sstevel@tonic-gate 	ddi_dma_impl_t *hp = (ddi_dma_impl_t *)handle;
72247c478bd9Sstevel@tonic-gate 	void (*notify)(ddi_dma_impl_t *);
72257c478bd9Sstevel@tonic-gate 
72267c478bd9Sstevel@tonic-gate 	if (!hp->dmai_fault) {
72277c478bd9Sstevel@tonic-gate 		hp->dmai_fault = 1;
72287c478bd9Sstevel@tonic-gate 		if ((notify = hp->dmai_fault_notify) != NULL)
72297c478bd9Sstevel@tonic-gate 			(*notify)(hp);
72307c478bd9Sstevel@tonic-gate 	}
72317c478bd9Sstevel@tonic-gate }
72327c478bd9Sstevel@tonic-gate 
72337c478bd9Sstevel@tonic-gate void
72347c478bd9Sstevel@tonic-gate i_ddi_dma_clr_fault(ddi_dma_handle_t handle)
72357c478bd9Sstevel@tonic-gate {
72367c478bd9Sstevel@tonic-gate 	ddi_dma_impl_t *hp = (ddi_dma_impl_t *)handle;
72377c478bd9Sstevel@tonic-gate 	void (*notify)(ddi_dma_impl_t *);
72387c478bd9Sstevel@tonic-gate 
72397c478bd9Sstevel@tonic-gate 	if (hp->dmai_fault) {
72407c478bd9Sstevel@tonic-gate 		hp->dmai_fault = 0;
72417c478bd9Sstevel@tonic-gate 		if ((notify = hp->dmai_fault_notify) != NULL)
72427c478bd9Sstevel@tonic-gate 			(*notify)(hp);
72437c478bd9Sstevel@tonic-gate 	}
72447c478bd9Sstevel@tonic-gate }
72457c478bd9Sstevel@tonic-gate 
72467c478bd9Sstevel@tonic-gate /*
72477c478bd9Sstevel@tonic-gate  * register mapping routines.
72487c478bd9Sstevel@tonic-gate  */
72497c478bd9Sstevel@tonic-gate int
72507c478bd9Sstevel@tonic-gate ddi_regs_map_setup(dev_info_t *dip, uint_t rnumber, caddr_t *addrp,
72517c478bd9Sstevel@tonic-gate 	offset_t offset, offset_t len, ddi_device_acc_attr_t *accattrp,
72527c478bd9Sstevel@tonic-gate 	ddi_acc_handle_t *handle)
72537c478bd9Sstevel@tonic-gate {
72547c478bd9Sstevel@tonic-gate 	ddi_map_req_t mr;
72557c478bd9Sstevel@tonic-gate 	ddi_acc_hdl_t *hp;
72567c478bd9Sstevel@tonic-gate 	int result;
72577c478bd9Sstevel@tonic-gate 
72587c478bd9Sstevel@tonic-gate 	/*
72597c478bd9Sstevel@tonic-gate 	 * Allocate and initialize the common elements of data access handle.
72607c478bd9Sstevel@tonic-gate 	 */
72617c478bd9Sstevel@tonic-gate 	*handle = impl_acc_hdl_alloc(KM_SLEEP, NULL);
72627c478bd9Sstevel@tonic-gate 	hp = impl_acc_hdl_get(*handle);
72637c478bd9Sstevel@tonic-gate 	hp->ah_vers = VERS_ACCHDL;
72647c478bd9Sstevel@tonic-gate 	hp->ah_dip = dip;
72657c478bd9Sstevel@tonic-gate 	hp->ah_rnumber = rnumber;
72667c478bd9Sstevel@tonic-gate 	hp->ah_offset = offset;
72677c478bd9Sstevel@tonic-gate 	hp->ah_len = len;
72687c478bd9Sstevel@tonic-gate 	hp->ah_acc = *accattrp;
72697c478bd9Sstevel@tonic-gate 
72707c478bd9Sstevel@tonic-gate 	/*
72717c478bd9Sstevel@tonic-gate 	 * Set up the mapping request and call to parent.
72727c478bd9Sstevel@tonic-gate 	 */
72737c478bd9Sstevel@tonic-gate 	mr.map_op = DDI_MO_MAP_LOCKED;
72747c478bd9Sstevel@tonic-gate 	mr.map_type = DDI_MT_RNUMBER;
72757c478bd9Sstevel@tonic-gate 	mr.map_obj.rnumber = rnumber;
72767c478bd9Sstevel@tonic-gate 	mr.map_prot = PROT_READ | PROT_WRITE;
72777c478bd9Sstevel@tonic-gate 	mr.map_flags = DDI_MF_KERNEL_MAPPING;
72787c478bd9Sstevel@tonic-gate 	mr.map_handlep = hp;
72797c478bd9Sstevel@tonic-gate 	mr.map_vers = DDI_MAP_VERSION;
72807c478bd9Sstevel@tonic-gate 	result = ddi_map(dip, &mr, offset, len, addrp);
72817c478bd9Sstevel@tonic-gate 
72827c478bd9Sstevel@tonic-gate 	/*
72837c478bd9Sstevel@tonic-gate 	 * check for end result
72847c478bd9Sstevel@tonic-gate 	 */
72857c478bd9Sstevel@tonic-gate 	if (result != DDI_SUCCESS) {
72867c478bd9Sstevel@tonic-gate 		impl_acc_hdl_free(*handle);
72877c478bd9Sstevel@tonic-gate 		*handle = (ddi_acc_handle_t)NULL;
72887c478bd9Sstevel@tonic-gate 	} else {
72897c478bd9Sstevel@tonic-gate 		hp->ah_addr = *addrp;
72907c478bd9Sstevel@tonic-gate 	}
72917c478bd9Sstevel@tonic-gate 
72927c478bd9Sstevel@tonic-gate 	return (result);
72937c478bd9Sstevel@tonic-gate }
72947c478bd9Sstevel@tonic-gate 
72957c478bd9Sstevel@tonic-gate void
72967c478bd9Sstevel@tonic-gate ddi_regs_map_free(ddi_acc_handle_t *handlep)
72977c478bd9Sstevel@tonic-gate {
72987c478bd9Sstevel@tonic-gate 	ddi_map_req_t mr;
72997c478bd9Sstevel@tonic-gate 	ddi_acc_hdl_t *hp;
73007c478bd9Sstevel@tonic-gate 
73017c478bd9Sstevel@tonic-gate 	hp = impl_acc_hdl_get(*handlep);
73027c478bd9Sstevel@tonic-gate 	ASSERT(hp);
73037c478bd9Sstevel@tonic-gate 
73047c478bd9Sstevel@tonic-gate 	mr.map_op = DDI_MO_UNMAP;
73057c478bd9Sstevel@tonic-gate 	mr.map_type = DDI_MT_RNUMBER;
73067c478bd9Sstevel@tonic-gate 	mr.map_obj.rnumber = hp->ah_rnumber;
73077c478bd9Sstevel@tonic-gate 	mr.map_prot = PROT_READ | PROT_WRITE;
73087c478bd9Sstevel@tonic-gate 	mr.map_flags = DDI_MF_KERNEL_MAPPING;
73097c478bd9Sstevel@tonic-gate 	mr.map_handlep = hp;
73107c478bd9Sstevel@tonic-gate 	mr.map_vers = DDI_MAP_VERSION;
73117c478bd9Sstevel@tonic-gate 
73127c478bd9Sstevel@tonic-gate 	/*
73137c478bd9Sstevel@tonic-gate 	 * Call my parent to unmap my regs.
73147c478bd9Sstevel@tonic-gate 	 */
73157c478bd9Sstevel@tonic-gate 	(void) ddi_map(hp->ah_dip, &mr, hp->ah_offset,
73167c478bd9Sstevel@tonic-gate 	    hp->ah_len, &hp->ah_addr);
73177c478bd9Sstevel@tonic-gate 	/*
73187c478bd9Sstevel@tonic-gate 	 * free the handle
73197c478bd9Sstevel@tonic-gate 	 */
73207c478bd9Sstevel@tonic-gate 	impl_acc_hdl_free(*handlep);
73217c478bd9Sstevel@tonic-gate 	*handlep = (ddi_acc_handle_t)NULL;
73227c478bd9Sstevel@tonic-gate }
73237c478bd9Sstevel@tonic-gate 
73247c478bd9Sstevel@tonic-gate int
73257c478bd9Sstevel@tonic-gate ddi_device_zero(ddi_acc_handle_t handle, caddr_t dev_addr, size_t bytecount,
73267c478bd9Sstevel@tonic-gate 	ssize_t dev_advcnt, uint_t dev_datasz)
73277c478bd9Sstevel@tonic-gate {
73287c478bd9Sstevel@tonic-gate 	uint8_t *b;
73297c478bd9Sstevel@tonic-gate 	uint16_t *w;
73307c478bd9Sstevel@tonic-gate 	uint32_t *l;
73317c478bd9Sstevel@tonic-gate 	uint64_t *ll;
73327c478bd9Sstevel@tonic-gate 
73337c478bd9Sstevel@tonic-gate 	/* check for total byte count is multiple of data transfer size */
73347c478bd9Sstevel@tonic-gate 	if (bytecount != ((bytecount / dev_datasz) * dev_datasz))
73357c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
73367c478bd9Sstevel@tonic-gate 
73377c478bd9Sstevel@tonic-gate 	switch (dev_datasz) {
73387c478bd9Sstevel@tonic-gate 	case DDI_DATA_SZ01_ACC:
73397c478bd9Sstevel@tonic-gate 		for (b = (uint8_t *)dev_addr;
73407c478bd9Sstevel@tonic-gate 		    bytecount != 0; bytecount -= 1, b += dev_advcnt)
73417c478bd9Sstevel@tonic-gate 			ddi_put8(handle, b, 0);
73427c478bd9Sstevel@tonic-gate 		break;
73437c478bd9Sstevel@tonic-gate 	case DDI_DATA_SZ02_ACC:
73447c478bd9Sstevel@tonic-gate 		for (w = (uint16_t *)dev_addr;
73457c478bd9Sstevel@tonic-gate 		    bytecount != 0; bytecount -= 2, w += dev_advcnt)
73467c478bd9Sstevel@tonic-gate 			ddi_put16(handle, w, 0);
73477c478bd9Sstevel@tonic-gate 		break;
73487c478bd9Sstevel@tonic-gate 	case DDI_DATA_SZ04_ACC:
73497c478bd9Sstevel@tonic-gate 		for (l = (uint32_t *)dev_addr;
73507c478bd9Sstevel@tonic-gate 		    bytecount != 0; bytecount -= 4, l += dev_advcnt)
73517c478bd9Sstevel@tonic-gate 			ddi_put32(handle, l, 0);
73527c478bd9Sstevel@tonic-gate 		break;
73537c478bd9Sstevel@tonic-gate 	case DDI_DATA_SZ08_ACC:
73547c478bd9Sstevel@tonic-gate 		for (ll = (uint64_t *)dev_addr;
73557c478bd9Sstevel@tonic-gate 		    bytecount != 0; bytecount -= 8, ll += dev_advcnt)
73567c478bd9Sstevel@tonic-gate 			ddi_put64(handle, ll, 0x0ll);
73577c478bd9Sstevel@tonic-gate 		break;
73587c478bd9Sstevel@tonic-gate 	default:
73597c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
73607c478bd9Sstevel@tonic-gate 	}
73617c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
73627c478bd9Sstevel@tonic-gate }
73637c478bd9Sstevel@tonic-gate 
73647c478bd9Sstevel@tonic-gate int
73657c478bd9Sstevel@tonic-gate ddi_device_copy(
73667c478bd9Sstevel@tonic-gate 	ddi_acc_handle_t src_handle, caddr_t src_addr, ssize_t src_advcnt,
73677c478bd9Sstevel@tonic-gate 	ddi_acc_handle_t dest_handle, caddr_t dest_addr, ssize_t dest_advcnt,
73687c478bd9Sstevel@tonic-gate 	size_t bytecount, uint_t dev_datasz)
73697c478bd9Sstevel@tonic-gate {
73707c478bd9Sstevel@tonic-gate 	uint8_t *b_src, *b_dst;
73717c478bd9Sstevel@tonic-gate 	uint16_t *w_src, *w_dst;
73727c478bd9Sstevel@tonic-gate 	uint32_t *l_src, *l_dst;
73737c478bd9Sstevel@tonic-gate 	uint64_t *ll_src, *ll_dst;
73747c478bd9Sstevel@tonic-gate 
73757c478bd9Sstevel@tonic-gate 	/* check for total byte count is multiple of data transfer size */
73767c478bd9Sstevel@tonic-gate 	if (bytecount != ((bytecount / dev_datasz) * dev_datasz))
73777c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
73787c478bd9Sstevel@tonic-gate 
73797c478bd9Sstevel@tonic-gate 	switch (dev_datasz) {
73807c478bd9Sstevel@tonic-gate 	case DDI_DATA_SZ01_ACC:
73817c478bd9Sstevel@tonic-gate 		b_src = (uint8_t *)src_addr;
73827c478bd9Sstevel@tonic-gate 		b_dst = (uint8_t *)dest_addr;
73837c478bd9Sstevel@tonic-gate 
73847c478bd9Sstevel@tonic-gate 		for (; bytecount != 0; bytecount -= 1) {
73857c478bd9Sstevel@tonic-gate 			ddi_put8(dest_handle, b_dst,
73867c478bd9Sstevel@tonic-gate 			    ddi_get8(src_handle, b_src));
73877c478bd9Sstevel@tonic-gate 			b_dst += dest_advcnt;
73887c478bd9Sstevel@tonic-gate 			b_src += src_advcnt;
73897c478bd9Sstevel@tonic-gate 		}
73907c478bd9Sstevel@tonic-gate 		break;
73917c478bd9Sstevel@tonic-gate 	case DDI_DATA_SZ02_ACC:
73927c478bd9Sstevel@tonic-gate 		w_src = (uint16_t *)src_addr;
73937c478bd9Sstevel@tonic-gate 		w_dst = (uint16_t *)dest_addr;
73947c478bd9Sstevel@tonic-gate 
73957c478bd9Sstevel@tonic-gate 		for (; bytecount != 0; bytecount -= 2) {
73967c478bd9Sstevel@tonic-gate 			ddi_put16(dest_handle, w_dst,
73977c478bd9Sstevel@tonic-gate 			    ddi_get16(src_handle, w_src));
73987c478bd9Sstevel@tonic-gate 			w_dst += dest_advcnt;
73997c478bd9Sstevel@tonic-gate 			w_src += src_advcnt;
74007c478bd9Sstevel@tonic-gate 		}
74017c478bd9Sstevel@tonic-gate 		break;
74027c478bd9Sstevel@tonic-gate 	case DDI_DATA_SZ04_ACC:
74037c478bd9Sstevel@tonic-gate 		l_src = (uint32_t *)src_addr;
74047c478bd9Sstevel@tonic-gate 		l_dst = (uint32_t *)dest_addr;
74057c478bd9Sstevel@tonic-gate 
74067c478bd9Sstevel@tonic-gate 		for (; bytecount != 0; bytecount -= 4) {
74077c478bd9Sstevel@tonic-gate 			ddi_put32(dest_handle, l_dst,
74087c478bd9Sstevel@tonic-gate 			    ddi_get32(src_handle, l_src));
74097c478bd9Sstevel@tonic-gate 			l_dst += dest_advcnt;
74107c478bd9Sstevel@tonic-gate 			l_src += src_advcnt;
74117c478bd9Sstevel@tonic-gate 		}
74127c478bd9Sstevel@tonic-gate 		break;
74137c478bd9Sstevel@tonic-gate 	case DDI_DATA_SZ08_ACC:
74147c478bd9Sstevel@tonic-gate 		ll_src = (uint64_t *)src_addr;
74157c478bd9Sstevel@tonic-gate 		ll_dst = (uint64_t *)dest_addr;
74167c478bd9Sstevel@tonic-gate 
74177c478bd9Sstevel@tonic-gate 		for (; bytecount != 0; bytecount -= 8) {
74187c478bd9Sstevel@tonic-gate 			ddi_put64(dest_handle, ll_dst,
74197c478bd9Sstevel@tonic-gate 			    ddi_get64(src_handle, ll_src));
74207c478bd9Sstevel@tonic-gate 			ll_dst += dest_advcnt;
74217c478bd9Sstevel@tonic-gate 			ll_src += src_advcnt;
74227c478bd9Sstevel@tonic-gate 		}
74237c478bd9Sstevel@tonic-gate 		break;
74247c478bd9Sstevel@tonic-gate 	default:
74257c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
74267c478bd9Sstevel@tonic-gate 	}
74277c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
74287c478bd9Sstevel@tonic-gate }
74297c478bd9Sstevel@tonic-gate 
74307c478bd9Sstevel@tonic-gate #define	swap16(value)  \
74317c478bd9Sstevel@tonic-gate 	((((value) & 0xff) << 8) | ((value) >> 8))
74327c478bd9Sstevel@tonic-gate 
74337c478bd9Sstevel@tonic-gate #define	swap32(value)	\
74347c478bd9Sstevel@tonic-gate 	(((uint32_t)swap16((uint16_t)((value) & 0xffff)) << 16) | \
74357c478bd9Sstevel@tonic-gate 	(uint32_t)swap16((uint16_t)((value) >> 16)))
74367c478bd9Sstevel@tonic-gate 
74377c478bd9Sstevel@tonic-gate #define	swap64(value)	\
74387c478bd9Sstevel@tonic-gate 	(((uint64_t)swap32((uint32_t)((value) & 0xffffffff)) \
74397c478bd9Sstevel@tonic-gate 	    << 32) | \
74407c478bd9Sstevel@tonic-gate 	(uint64_t)swap32((uint32_t)((value) >> 32)))
74417c478bd9Sstevel@tonic-gate 
74427c478bd9Sstevel@tonic-gate uint16_t
74437c478bd9Sstevel@tonic-gate ddi_swap16(uint16_t value)
74447c478bd9Sstevel@tonic-gate {
74457c478bd9Sstevel@tonic-gate 	return (swap16(value));
74467c478bd9Sstevel@tonic-gate }
74477c478bd9Sstevel@tonic-gate 
74487c478bd9Sstevel@tonic-gate uint32_t
74497c478bd9Sstevel@tonic-gate ddi_swap32(uint32_t value)
74507c478bd9Sstevel@tonic-gate {
74517c478bd9Sstevel@tonic-gate 	return (swap32(value));
74527c478bd9Sstevel@tonic-gate }
74537c478bd9Sstevel@tonic-gate 
74547c478bd9Sstevel@tonic-gate uint64_t
74557c478bd9Sstevel@tonic-gate ddi_swap64(uint64_t value)
74567c478bd9Sstevel@tonic-gate {
74577c478bd9Sstevel@tonic-gate 	return (swap64(value));
74587c478bd9Sstevel@tonic-gate }
74597c478bd9Sstevel@tonic-gate 
74607c478bd9Sstevel@tonic-gate /*
74617c478bd9Sstevel@tonic-gate  * Convert a binding name to a driver name.
74627c478bd9Sstevel@tonic-gate  * A binding name is the name used to determine the driver for a
74637c478bd9Sstevel@tonic-gate  * device - it may be either an alias for the driver or the name
74647c478bd9Sstevel@tonic-gate  * of the driver itself.
74657c478bd9Sstevel@tonic-gate  */
74667c478bd9Sstevel@tonic-gate char *
74677c478bd9Sstevel@tonic-gate i_binding_to_drv_name(char *bname)
74687c478bd9Sstevel@tonic-gate {
74697c478bd9Sstevel@tonic-gate 	major_t major_no;
74707c478bd9Sstevel@tonic-gate 
74717c478bd9Sstevel@tonic-gate 	ASSERT(bname != NULL);
74727c478bd9Sstevel@tonic-gate 
74737c478bd9Sstevel@tonic-gate 	if ((major_no = ddi_name_to_major(bname)) == -1)
74747c478bd9Sstevel@tonic-gate 		return (NULL);
74757c478bd9Sstevel@tonic-gate 	return (ddi_major_to_name(major_no));
74767c478bd9Sstevel@tonic-gate }
74777c478bd9Sstevel@tonic-gate 
74787c478bd9Sstevel@tonic-gate /*
74797c478bd9Sstevel@tonic-gate  * Search for minor name that has specified dev_t and spec_type.
74807c478bd9Sstevel@tonic-gate  * If spec_type is zero then any dev_t match works.  Since we
74817c478bd9Sstevel@tonic-gate  * are returning a pointer to the minor name string, we require the
74827c478bd9Sstevel@tonic-gate  * caller to do the locking.
74837c478bd9Sstevel@tonic-gate  */
74847c478bd9Sstevel@tonic-gate char *
74857c478bd9Sstevel@tonic-gate i_ddi_devtspectype_to_minorname(dev_info_t *dip, dev_t dev, int spec_type)
74867c478bd9Sstevel@tonic-gate {
74877c478bd9Sstevel@tonic-gate 	struct ddi_minor_data	*dmdp;
74887c478bd9Sstevel@tonic-gate 
74897c478bd9Sstevel@tonic-gate 	/*
74907c478bd9Sstevel@tonic-gate 	 * The did layered driver currently intentionally returns a
74917c478bd9Sstevel@tonic-gate 	 * devinfo ptr for an underlying sd instance based on a did
74927c478bd9Sstevel@tonic-gate 	 * dev_t. In this case it is not an error.
74937c478bd9Sstevel@tonic-gate 	 *
74947c478bd9Sstevel@tonic-gate 	 * The did layered driver is associated with Sun Cluster.
74957c478bd9Sstevel@tonic-gate 	 */
74967c478bd9Sstevel@tonic-gate 	ASSERT((ddi_driver_major(dip) == getmajor(dev)) ||
74977c478bd9Sstevel@tonic-gate 	    (strcmp(ddi_major_to_name(getmajor(dev)), "did") == 0));
74987c478bd9Sstevel@tonic-gate 
7499b9ccdc5aScth 	ASSERT(DEVI_BUSY_OWNED(dip));
75007c478bd9Sstevel@tonic-gate 	for (dmdp = DEVI(dip)->devi_minor; dmdp; dmdp = dmdp->next) {
75017c478bd9Sstevel@tonic-gate 		if (((dmdp->type == DDM_MINOR) ||
75027c478bd9Sstevel@tonic-gate 		    (dmdp->type == DDM_INTERNAL_PATH) ||
75037c478bd9Sstevel@tonic-gate 		    (dmdp->type == DDM_DEFAULT)) &&
75047c478bd9Sstevel@tonic-gate 		    (dmdp->ddm_dev == dev) &&
75057c478bd9Sstevel@tonic-gate 		    ((((spec_type & (S_IFCHR|S_IFBLK))) == 0) ||
75067c478bd9Sstevel@tonic-gate 		    (dmdp->ddm_spec_type == spec_type)))
75077c478bd9Sstevel@tonic-gate 			return (dmdp->ddm_name);
75087c478bd9Sstevel@tonic-gate 	}
75097c478bd9Sstevel@tonic-gate 
75107c478bd9Sstevel@tonic-gate 	return (NULL);
75117c478bd9Sstevel@tonic-gate }
75127c478bd9Sstevel@tonic-gate 
75137c478bd9Sstevel@tonic-gate /*
75147c478bd9Sstevel@tonic-gate  * Find the devt and spectype of the specified minor_name.
75157c478bd9Sstevel@tonic-gate  * Return DDI_FAILURE if minor_name not found. Since we are
75167c478bd9Sstevel@tonic-gate  * returning everything via arguments we can do the locking.
75177c478bd9Sstevel@tonic-gate  */
75187c478bd9Sstevel@tonic-gate int
75197c478bd9Sstevel@tonic-gate i_ddi_minorname_to_devtspectype(dev_info_t *dip, char *minor_name,
75207c478bd9Sstevel@tonic-gate 	dev_t *devtp, int *spectypep)
75217c478bd9Sstevel@tonic-gate {
7522b9ccdc5aScth 	int			circ;
75237c478bd9Sstevel@tonic-gate 	struct ddi_minor_data	*dmdp;
75247c478bd9Sstevel@tonic-gate 
75257c478bd9Sstevel@tonic-gate 	/* deal with clone minor nodes */
75267c478bd9Sstevel@tonic-gate 	if (dip == clone_dip) {
75277c478bd9Sstevel@tonic-gate 		major_t	major;
75287c478bd9Sstevel@tonic-gate 		/*
75297c478bd9Sstevel@tonic-gate 		 * Make sure minor_name is a STREAMS driver.
75307c478bd9Sstevel@tonic-gate 		 * We load the driver but don't attach to any instances.
75317c478bd9Sstevel@tonic-gate 		 */
75327c478bd9Sstevel@tonic-gate 
75337c478bd9Sstevel@tonic-gate 		major = ddi_name_to_major(minor_name);
7534a204de77Scth 		if (major == DDI_MAJOR_T_NONE)
75357c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
75367c478bd9Sstevel@tonic-gate 
75377c478bd9Sstevel@tonic-gate 		if (ddi_hold_driver(major) == NULL)
75387c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
75397c478bd9Sstevel@tonic-gate 
75407c478bd9Sstevel@tonic-gate 		if (STREAMSTAB(major) == NULL) {
75417c478bd9Sstevel@tonic-gate 			ddi_rele_driver(major);
75427c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
75437c478bd9Sstevel@tonic-gate 		}
75447c478bd9Sstevel@tonic-gate 		ddi_rele_driver(major);
75457c478bd9Sstevel@tonic-gate 
75467c478bd9Sstevel@tonic-gate 		if (devtp)
75477c478bd9Sstevel@tonic-gate 			*devtp = makedevice(clone_major, (minor_t)major);
75487c478bd9Sstevel@tonic-gate 
75497c478bd9Sstevel@tonic-gate 		if (spectypep)
75507c478bd9Sstevel@tonic-gate 			*spectypep = S_IFCHR;
75517c478bd9Sstevel@tonic-gate 
75527c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
75537c478bd9Sstevel@tonic-gate 	}
75547c478bd9Sstevel@tonic-gate 
7555b9ccdc5aScth 	ndi_devi_enter(dip, &circ);
75567c478bd9Sstevel@tonic-gate 	for (dmdp = DEVI(dip)->devi_minor; dmdp; dmdp = dmdp->next) {
75577c478bd9Sstevel@tonic-gate 		if (((dmdp->type != DDM_MINOR) &&
75587c478bd9Sstevel@tonic-gate 		    (dmdp->type != DDM_INTERNAL_PATH) &&
75597c478bd9Sstevel@tonic-gate 		    (dmdp->type != DDM_DEFAULT)) ||
75607c478bd9Sstevel@tonic-gate 		    strcmp(minor_name, dmdp->ddm_name))
75617c478bd9Sstevel@tonic-gate 			continue;
75627c478bd9Sstevel@tonic-gate 
75637c478bd9Sstevel@tonic-gate 		if (devtp)
75647c478bd9Sstevel@tonic-gate 			*devtp = dmdp->ddm_dev;
75657c478bd9Sstevel@tonic-gate 
75667c478bd9Sstevel@tonic-gate 		if (spectypep)
75677c478bd9Sstevel@tonic-gate 			*spectypep = dmdp->ddm_spec_type;
75687c478bd9Sstevel@tonic-gate 
7569b9ccdc5aScth 		ndi_devi_exit(dip, circ);
75707c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
75717c478bd9Sstevel@tonic-gate 	}
7572b9ccdc5aScth 	ndi_devi_exit(dip, circ);
75737c478bd9Sstevel@tonic-gate 
75747c478bd9Sstevel@tonic-gate 	return (DDI_FAILURE);
75757c478bd9Sstevel@tonic-gate }
75767c478bd9Sstevel@tonic-gate 
75777c478bd9Sstevel@tonic-gate static kmutex_t devid_gen_mutex;
75787c478bd9Sstevel@tonic-gate static short	devid_gen_number;
75797c478bd9Sstevel@tonic-gate 
75807c478bd9Sstevel@tonic-gate #ifdef DEBUG
75817c478bd9Sstevel@tonic-gate 
75827c478bd9Sstevel@tonic-gate static int	devid_register_corrupt = 0;
75837c478bd9Sstevel@tonic-gate static int	devid_register_corrupt_major = 0;
75847c478bd9Sstevel@tonic-gate static int	devid_register_corrupt_hint = 0;
75857c478bd9Sstevel@tonic-gate static int	devid_register_corrupt_hint_major = 0;
75867c478bd9Sstevel@tonic-gate 
75877c478bd9Sstevel@tonic-gate static int devid_lyr_debug = 0;
75887c478bd9Sstevel@tonic-gate 
75897c478bd9Sstevel@tonic-gate #define	DDI_DEBUG_DEVID_DEVTS(msg, ndevs, devs)		\
75907c478bd9Sstevel@tonic-gate 	if (devid_lyr_debug)					\
75917c478bd9Sstevel@tonic-gate 		ddi_debug_devid_devts(msg, ndevs, devs)
75927c478bd9Sstevel@tonic-gate 
75937c478bd9Sstevel@tonic-gate #else
75947c478bd9Sstevel@tonic-gate 
75957c478bd9Sstevel@tonic-gate #define	DDI_DEBUG_DEVID_DEVTS(msg, ndevs, devs)
75967c478bd9Sstevel@tonic-gate 
75977c478bd9Sstevel@tonic-gate #endif /* DEBUG */
75987c478bd9Sstevel@tonic-gate 
75997c478bd9Sstevel@tonic-gate 
76007c478bd9Sstevel@tonic-gate #ifdef	DEBUG
76017c478bd9Sstevel@tonic-gate 
76027c478bd9Sstevel@tonic-gate static void
76037c478bd9Sstevel@tonic-gate ddi_debug_devid_devts(char *msg, int ndevs, dev_t *devs)
76047c478bd9Sstevel@tonic-gate {
76057c478bd9Sstevel@tonic-gate 	int i;
76067c478bd9Sstevel@tonic-gate 
76077c478bd9Sstevel@tonic-gate 	cmn_err(CE_CONT, "%s:\n", msg);
76087c478bd9Sstevel@tonic-gate 	for (i = 0; i < ndevs; i++) {
76097c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "    0x%lx\n", devs[i]);
76107c478bd9Sstevel@tonic-gate 	}
76117c478bd9Sstevel@tonic-gate }
76127c478bd9Sstevel@tonic-gate 
76137c478bd9Sstevel@tonic-gate static void
76147c478bd9Sstevel@tonic-gate ddi_debug_devid_paths(char *msg, int npaths, char **paths)
76157c478bd9Sstevel@tonic-gate {
76167c478bd9Sstevel@tonic-gate 	int i;
76177c478bd9Sstevel@tonic-gate 
76187c478bd9Sstevel@tonic-gate 	cmn_err(CE_CONT, "%s:\n", msg);
76197c478bd9Sstevel@tonic-gate 	for (i = 0; i < npaths; i++) {
76207c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "    %s\n", paths[i]);
76217c478bd9Sstevel@tonic-gate 	}
76227c478bd9Sstevel@tonic-gate }
76237c478bd9Sstevel@tonic-gate 
76247c478bd9Sstevel@tonic-gate static void
76257c478bd9Sstevel@tonic-gate ddi_debug_devid_devts_per_path(char *path, int ndevs, dev_t *devs)
76267c478bd9Sstevel@tonic-gate {
76277c478bd9Sstevel@tonic-gate 	int i;
76287c478bd9Sstevel@tonic-gate 
76297c478bd9Sstevel@tonic-gate 	cmn_err(CE_CONT, "dev_ts per path %s\n", path);
76307c478bd9Sstevel@tonic-gate 	for (i = 0; i < ndevs; i++) {
76317c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "    0x%lx\n", devs[i]);
76327c478bd9Sstevel@tonic-gate 	}
76337c478bd9Sstevel@tonic-gate }
76347c478bd9Sstevel@tonic-gate 
76357c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
76367c478bd9Sstevel@tonic-gate 
76377c478bd9Sstevel@tonic-gate /*
76387c478bd9Sstevel@tonic-gate  * Register device id into DDI framework.
76394f1e984dSReed  * Must be called when the driver is bound.
76407c478bd9Sstevel@tonic-gate  */
76417c478bd9Sstevel@tonic-gate static int
76427c478bd9Sstevel@tonic-gate i_ddi_devid_register(dev_info_t *dip, ddi_devid_t devid)
76437c478bd9Sstevel@tonic-gate {
76447c478bd9Sstevel@tonic-gate 	impl_devid_t	*i_devid = (impl_devid_t *)devid;
76457c478bd9Sstevel@tonic-gate 	size_t		driver_len;
76467c478bd9Sstevel@tonic-gate 	const char	*driver_name;
76477c478bd9Sstevel@tonic-gate 	char		*devid_str;
76487c478bd9Sstevel@tonic-gate 	major_t		major;
76497c478bd9Sstevel@tonic-gate 
76507c478bd9Sstevel@tonic-gate 	if ((dip == NULL) ||
7651a204de77Scth 	    ((major = ddi_driver_major(dip)) == DDI_MAJOR_T_NONE))
76527c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
76537c478bd9Sstevel@tonic-gate 
76547c478bd9Sstevel@tonic-gate 	/* verify that the devid is valid */
76557c478bd9Sstevel@tonic-gate 	if (ddi_devid_valid(devid) != DDI_SUCCESS)
76567c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
76577c478bd9Sstevel@tonic-gate 
76587c478bd9Sstevel@tonic-gate 	/* Updating driver name hint in devid */
76597c478bd9Sstevel@tonic-gate 	driver_name = ddi_driver_name(dip);
76607c478bd9Sstevel@tonic-gate 	driver_len = strlen(driver_name);
76617c478bd9Sstevel@tonic-gate 	if (driver_len > DEVID_HINT_SIZE) {
76627c478bd9Sstevel@tonic-gate 		/* Pick up last four characters of driver name */
76637c478bd9Sstevel@tonic-gate 		driver_name += driver_len - DEVID_HINT_SIZE;
76647c478bd9Sstevel@tonic-gate 		driver_len = DEVID_HINT_SIZE;
76657c478bd9Sstevel@tonic-gate 	}
76667c478bd9Sstevel@tonic-gate 	bzero(i_devid->did_driver, DEVID_HINT_SIZE);
76677c478bd9Sstevel@tonic-gate 	bcopy(driver_name, i_devid->did_driver, driver_len);
76687c478bd9Sstevel@tonic-gate 
76697c478bd9Sstevel@tonic-gate #ifdef DEBUG
76707c478bd9Sstevel@tonic-gate 	/* Corrupt the devid for testing. */
76717c478bd9Sstevel@tonic-gate 	if (devid_register_corrupt)
76727c478bd9Sstevel@tonic-gate 		i_devid->did_id[0] += devid_register_corrupt;
76737c478bd9Sstevel@tonic-gate 	if (devid_register_corrupt_major &&
76747c478bd9Sstevel@tonic-gate 	    (major == devid_register_corrupt_major))
76757c478bd9Sstevel@tonic-gate 		i_devid->did_id[0] += 1;
76767c478bd9Sstevel@tonic-gate 	if (devid_register_corrupt_hint)
76777c478bd9Sstevel@tonic-gate 		i_devid->did_driver[0] += devid_register_corrupt_hint;
76787c478bd9Sstevel@tonic-gate 	if (devid_register_corrupt_hint_major &&
76797c478bd9Sstevel@tonic-gate 	    (major == devid_register_corrupt_hint_major))
76807c478bd9Sstevel@tonic-gate 		i_devid->did_driver[0] += 1;
76817c478bd9Sstevel@tonic-gate #endif /* DEBUG */
76827c478bd9Sstevel@tonic-gate 
76837c478bd9Sstevel@tonic-gate 	/* encode the devid as a string */
76847c478bd9Sstevel@tonic-gate 	if ((devid_str = ddi_devid_str_encode(devid, NULL)) == NULL)
76857c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
76867c478bd9Sstevel@tonic-gate 
76877c478bd9Sstevel@tonic-gate 	/* add string as a string property */
76887c478bd9Sstevel@tonic-gate 	if (ndi_prop_update_string(DDI_DEV_T_NONE, dip,
76897c478bd9Sstevel@tonic-gate 	    DEVID_PROP_NAME, devid_str) != DDI_SUCCESS) {
76907c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d: devid property update failed",
76917c478bd9Sstevel@tonic-gate 		    ddi_driver_name(dip), ddi_get_instance(dip));
76927c478bd9Sstevel@tonic-gate 		ddi_devid_str_free(devid_str);
76937c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
76947c478bd9Sstevel@tonic-gate 	}
76957c478bd9Sstevel@tonic-gate 
7696602ca9eaScth 	/* keep pointer to devid string for interrupt context fma code */
7697602ca9eaScth 	if (DEVI(dip)->devi_devid_str)
7698602ca9eaScth 		ddi_devid_str_free(DEVI(dip)->devi_devid_str);
7699602ca9eaScth 	DEVI(dip)->devi_devid_str = devid_str;
77007c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
77017c478bd9Sstevel@tonic-gate }
77027c478bd9Sstevel@tonic-gate 
77037c478bd9Sstevel@tonic-gate int
77047c478bd9Sstevel@tonic-gate ddi_devid_register(dev_info_t *dip, ddi_devid_t devid)
77057c478bd9Sstevel@tonic-gate {
77067c478bd9Sstevel@tonic-gate 	int rval;
77077c478bd9Sstevel@tonic-gate 
77087c478bd9Sstevel@tonic-gate 	rval = i_ddi_devid_register(dip, devid);
77097c478bd9Sstevel@tonic-gate 	if (rval == DDI_SUCCESS) {
77107c478bd9Sstevel@tonic-gate 		/*
77117c478bd9Sstevel@tonic-gate 		 * Register devid in devid-to-path cache
77127c478bd9Sstevel@tonic-gate 		 */
77137c478bd9Sstevel@tonic-gate 		if (e_devid_cache_register(dip, devid) == DDI_SUCCESS) {
77147c478bd9Sstevel@tonic-gate 			mutex_enter(&DEVI(dip)->devi_lock);
77158451e9c3SGavin Maltby 			DEVI(dip)->devi_flags |= DEVI_CACHED_DEVID;
77167c478bd9Sstevel@tonic-gate 			mutex_exit(&DEVI(dip)->devi_lock);
77174f1e984dSReed 		} else if (ddi_get_name_addr(dip)) {
77184f1e984dSReed 			/*
77194f1e984dSReed 			 * We only expect cache_register DDI_FAILURE when we
77204f1e984dSReed 			 * can't form the full path because of NULL devi_addr.
77214f1e984dSReed 			 */
77227c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s%d: failed to cache devid",
77237c478bd9Sstevel@tonic-gate 			    ddi_driver_name(dip), ddi_get_instance(dip));
77247c478bd9Sstevel@tonic-gate 		}
77257c478bd9Sstevel@tonic-gate 	} else {
77267c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d: failed to register devid",
77277c478bd9Sstevel@tonic-gate 		    ddi_driver_name(dip), ddi_get_instance(dip));
77287c478bd9Sstevel@tonic-gate 	}
77297c478bd9Sstevel@tonic-gate 	return (rval);
77307c478bd9Sstevel@tonic-gate }
77317c478bd9Sstevel@tonic-gate 
77327c478bd9Sstevel@tonic-gate /*
77337c478bd9Sstevel@tonic-gate  * Remove (unregister) device id from DDI framework.
77347c478bd9Sstevel@tonic-gate  * Must be called when device is detached.
77357c478bd9Sstevel@tonic-gate  */
77367c478bd9Sstevel@tonic-gate static void
77377c478bd9Sstevel@tonic-gate i_ddi_devid_unregister(dev_info_t *dip)
77387c478bd9Sstevel@tonic-gate {
7739602ca9eaScth 	if (DEVI(dip)->devi_devid_str) {
7740602ca9eaScth 		ddi_devid_str_free(DEVI(dip)->devi_devid_str);
7741602ca9eaScth 		DEVI(dip)->devi_devid_str = NULL;
7742602ca9eaScth 	}
77437c478bd9Sstevel@tonic-gate 
77447c478bd9Sstevel@tonic-gate 	/* remove the devid property */
77457c478bd9Sstevel@tonic-gate 	(void) ndi_prop_remove(DDI_DEV_T_NONE, dip, DEVID_PROP_NAME);
77467c478bd9Sstevel@tonic-gate }
77477c478bd9Sstevel@tonic-gate 
77487c478bd9Sstevel@tonic-gate void
77497c478bd9Sstevel@tonic-gate ddi_devid_unregister(dev_info_t *dip)
77507c478bd9Sstevel@tonic-gate {
77517c478bd9Sstevel@tonic-gate 	mutex_enter(&DEVI(dip)->devi_lock);
77528451e9c3SGavin Maltby 	DEVI(dip)->devi_flags &= ~DEVI_CACHED_DEVID;
77537c478bd9Sstevel@tonic-gate 	mutex_exit(&DEVI(dip)->devi_lock);
77547c478bd9Sstevel@tonic-gate 	e_devid_cache_unregister(dip);
77557c478bd9Sstevel@tonic-gate 	i_ddi_devid_unregister(dip);
77567c478bd9Sstevel@tonic-gate }
77577c478bd9Sstevel@tonic-gate 
77587c478bd9Sstevel@tonic-gate /*
77597c478bd9Sstevel@tonic-gate  * Allocate and initialize a device id.
77607c478bd9Sstevel@tonic-gate  */
77617c478bd9Sstevel@tonic-gate int
77627c478bd9Sstevel@tonic-gate ddi_devid_init(
77637c478bd9Sstevel@tonic-gate 	dev_info_t	*dip,
77647c478bd9Sstevel@tonic-gate 	ushort_t	devid_type,
77657c478bd9Sstevel@tonic-gate 	ushort_t	nbytes,
77667c478bd9Sstevel@tonic-gate 	void		*id,
77677c478bd9Sstevel@tonic-gate 	ddi_devid_t	*ret_devid)
77687c478bd9Sstevel@tonic-gate {
77697c478bd9Sstevel@tonic-gate 	impl_devid_t	*i_devid;
77707c478bd9Sstevel@tonic-gate 	int		sz = sizeof (*i_devid) + nbytes - sizeof (char);
77717c478bd9Sstevel@tonic-gate 	int		driver_len;
77727c478bd9Sstevel@tonic-gate 	const char	*driver_name;
77737c478bd9Sstevel@tonic-gate 
77747c478bd9Sstevel@tonic-gate 	switch (devid_type) {
77757c478bd9Sstevel@tonic-gate 	case DEVID_SCSI3_WWN:
77767c478bd9Sstevel@tonic-gate 		/*FALLTHRU*/
77777c478bd9Sstevel@tonic-gate 	case DEVID_SCSI_SERIAL:
77787c478bd9Sstevel@tonic-gate 		/*FALLTHRU*/
77797c478bd9Sstevel@tonic-gate 	case DEVID_ATA_SERIAL:
77807c478bd9Sstevel@tonic-gate 		/*FALLTHRU*/
77817c478bd9Sstevel@tonic-gate 	case DEVID_ENCAP:
77827c478bd9Sstevel@tonic-gate 		if (nbytes == 0)
77837c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
77847c478bd9Sstevel@tonic-gate 		if (id == NULL)
77857c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
77867c478bd9Sstevel@tonic-gate 		break;
77877c478bd9Sstevel@tonic-gate 	case DEVID_FAB:
77887c478bd9Sstevel@tonic-gate 		if (nbytes != 0)
77897c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
77907c478bd9Sstevel@tonic-gate 		if (id != NULL)
77917c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
77927c478bd9Sstevel@tonic-gate 		nbytes = sizeof (int) +
77937c478bd9Sstevel@tonic-gate 		    sizeof (struct timeval32) + sizeof (short);
77947c478bd9Sstevel@tonic-gate 		sz += nbytes;
77957c478bd9Sstevel@tonic-gate 		break;
77967c478bd9Sstevel@tonic-gate 	default:
77977c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
77987c478bd9Sstevel@tonic-gate 	}
77997c478bd9Sstevel@tonic-gate 
78007c478bd9Sstevel@tonic-gate 	if ((i_devid = kmem_zalloc(sz, KM_SLEEP)) == NULL)
78017c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
78027c478bd9Sstevel@tonic-gate 
78037c478bd9Sstevel@tonic-gate 	i_devid->did_magic_hi = DEVID_MAGIC_MSB;
78047c478bd9Sstevel@tonic-gate 	i_devid->did_magic_lo = DEVID_MAGIC_LSB;
78057c478bd9Sstevel@tonic-gate 	i_devid->did_rev_hi = DEVID_REV_MSB;
78067c478bd9Sstevel@tonic-gate 	i_devid->did_rev_lo = DEVID_REV_LSB;
78077c478bd9Sstevel@tonic-gate 	DEVID_FORMTYPE(i_devid, devid_type);
78087c478bd9Sstevel@tonic-gate 	DEVID_FORMLEN(i_devid, nbytes);
78097c478bd9Sstevel@tonic-gate 
78107c478bd9Sstevel@tonic-gate 	/* Fill in driver name hint */
78117c478bd9Sstevel@tonic-gate 	driver_name = ddi_driver_name(dip);
78127c478bd9Sstevel@tonic-gate 	driver_len = strlen(driver_name);
78137c478bd9Sstevel@tonic-gate 	if (driver_len > DEVID_HINT_SIZE) {
78147c478bd9Sstevel@tonic-gate 		/* Pick up last four characters of driver name */
78157c478bd9Sstevel@tonic-gate 		driver_name += driver_len - DEVID_HINT_SIZE;
78167c478bd9Sstevel@tonic-gate 		driver_len = DEVID_HINT_SIZE;
78177c478bd9Sstevel@tonic-gate 	}
78187c478bd9Sstevel@tonic-gate 
78197c478bd9Sstevel@tonic-gate 	bcopy(driver_name, i_devid->did_driver, driver_len);
78207c478bd9Sstevel@tonic-gate 
78217c478bd9Sstevel@tonic-gate 	/* Fill in id field */
78227c478bd9Sstevel@tonic-gate 	if (devid_type == DEVID_FAB) {
78237c478bd9Sstevel@tonic-gate 		char		*cp;
78245679c89fSjv227347 		uint32_t	hostid;
78257c478bd9Sstevel@tonic-gate 		struct timeval32 timestamp32;
78267c478bd9Sstevel@tonic-gate 		int		i;
78277c478bd9Sstevel@tonic-gate 		int		*ip;
78287c478bd9Sstevel@tonic-gate 		short		gen;
78297c478bd9Sstevel@tonic-gate 
78307c478bd9Sstevel@tonic-gate 		/* increase the generation number */
78317c478bd9Sstevel@tonic-gate 		mutex_enter(&devid_gen_mutex);
78327c478bd9Sstevel@tonic-gate 		gen = devid_gen_number++;
78337c478bd9Sstevel@tonic-gate 		mutex_exit(&devid_gen_mutex);
78347c478bd9Sstevel@tonic-gate 
78357c478bd9Sstevel@tonic-gate 		cp = i_devid->did_id;
78367c478bd9Sstevel@tonic-gate 
78377c478bd9Sstevel@tonic-gate 		/* Fill in host id (big-endian byte ordering) */
78385679c89fSjv227347 		hostid = zone_get_hostid(NULL);
78397c478bd9Sstevel@tonic-gate 		*cp++ = hibyte(hiword(hostid));
78407c478bd9Sstevel@tonic-gate 		*cp++ = lobyte(hiword(hostid));
78417c478bd9Sstevel@tonic-gate 		*cp++ = hibyte(loword(hostid));
78427c478bd9Sstevel@tonic-gate 		*cp++ = lobyte(loword(hostid));
78437c478bd9Sstevel@tonic-gate 
78447c478bd9Sstevel@tonic-gate 		/*
78457c478bd9Sstevel@tonic-gate 		 * Fill in timestamp (big-endian byte ordering)
78467c478bd9Sstevel@tonic-gate 		 *
78477c478bd9Sstevel@tonic-gate 		 * (Note that the format may have to be changed
78487c478bd9Sstevel@tonic-gate 		 * before 2038 comes around, though it's arguably
78497c478bd9Sstevel@tonic-gate 		 * unique enough as it is..)
78507c478bd9Sstevel@tonic-gate 		 */
78517c478bd9Sstevel@tonic-gate 		uniqtime32(&timestamp32);
78527c478bd9Sstevel@tonic-gate 		ip = (int *)&timestamp32;
78537c478bd9Sstevel@tonic-gate 		for (i = 0;
78547c478bd9Sstevel@tonic-gate 		    i < sizeof (timestamp32) / sizeof (int); i++, ip++) {
78557c478bd9Sstevel@tonic-gate 			int	val;
78567c478bd9Sstevel@tonic-gate 			val = *ip;
78577c478bd9Sstevel@tonic-gate 			*cp++ = hibyte(hiword(val));
78587c478bd9Sstevel@tonic-gate 			*cp++ = lobyte(hiword(val));
78597c478bd9Sstevel@tonic-gate 			*cp++ = hibyte(loword(val));
78607c478bd9Sstevel@tonic-gate 			*cp++ = lobyte(loword(val));
78617c478bd9Sstevel@tonic-gate 		}
78627c478bd9Sstevel@tonic-gate 
78637c478bd9Sstevel@tonic-gate 		/* fill in the generation number */
78647c478bd9Sstevel@tonic-gate 		*cp++ = hibyte(gen);
78657c478bd9Sstevel@tonic-gate 		*cp++ = lobyte(gen);
78667c478bd9Sstevel@tonic-gate 	} else
78677c478bd9Sstevel@tonic-gate 		bcopy(id, i_devid->did_id, nbytes);
78687c478bd9Sstevel@tonic-gate 
78697c478bd9Sstevel@tonic-gate 	/* return device id */
78707c478bd9Sstevel@tonic-gate 	*ret_devid = (ddi_devid_t)i_devid;
78717c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
78727c478bd9Sstevel@tonic-gate }
78737c478bd9Sstevel@tonic-gate 
78747c478bd9Sstevel@tonic-gate int
78758d483882Smlf ddi_devid_get(dev_info_t *dip, ddi_devid_t *ret_devid)
78768d483882Smlf {
78778d483882Smlf 	return (i_ddi_devi_get_devid(DDI_DEV_T_ANY, dip, ret_devid));
78788d483882Smlf }
78798d483882Smlf 
78808d483882Smlf int
78817c478bd9Sstevel@tonic-gate i_ddi_devi_get_devid(dev_t dev, dev_info_t *dip, ddi_devid_t *ret_devid)
78827c478bd9Sstevel@tonic-gate {
78837c478bd9Sstevel@tonic-gate 	char		*devidstr;
78847c478bd9Sstevel@tonic-gate 
78857c478bd9Sstevel@tonic-gate 	ASSERT(dev != DDI_DEV_T_NONE);
78867c478bd9Sstevel@tonic-gate 
78877c478bd9Sstevel@tonic-gate 	/* look up the property, devt specific first */
78887c478bd9Sstevel@tonic-gate 	if (ddi_prop_lookup_string(dev, dip, DDI_PROP_DONTPASS,
78897c478bd9Sstevel@tonic-gate 	    DEVID_PROP_NAME, &devidstr) != DDI_PROP_SUCCESS) {
78907c478bd9Sstevel@tonic-gate 		if ((dev == DDI_DEV_T_ANY) ||
78917c478bd9Sstevel@tonic-gate 		    (ddi_prop_lookup_string(DDI_DEV_T_ANY, dip,
78927c478bd9Sstevel@tonic-gate 		    DDI_PROP_DONTPASS, DEVID_PROP_NAME, &devidstr) !=
78937c478bd9Sstevel@tonic-gate 		    DDI_PROP_SUCCESS)) {
78947c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
78957c478bd9Sstevel@tonic-gate 		}
78967c478bd9Sstevel@tonic-gate 	}
78977c478bd9Sstevel@tonic-gate 
78987c478bd9Sstevel@tonic-gate 	/* convert to binary form */
78997c478bd9Sstevel@tonic-gate 	if (ddi_devid_str_decode(devidstr, ret_devid, NULL) == -1) {
79007c478bd9Sstevel@tonic-gate 		ddi_prop_free(devidstr);
79017c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
79027c478bd9Sstevel@tonic-gate 	}
79037c478bd9Sstevel@tonic-gate 	ddi_prop_free(devidstr);
79047c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
79057c478bd9Sstevel@tonic-gate }
79067c478bd9Sstevel@tonic-gate 
79077c478bd9Sstevel@tonic-gate /*
79087c478bd9Sstevel@tonic-gate  * Return a copy of the device id for dev_t
79097c478bd9Sstevel@tonic-gate  */
79107c478bd9Sstevel@tonic-gate int
79117c478bd9Sstevel@tonic-gate ddi_lyr_get_devid(dev_t dev, ddi_devid_t *ret_devid)
79127c478bd9Sstevel@tonic-gate {
79137c478bd9Sstevel@tonic-gate 	dev_info_t	*dip;
79147c478bd9Sstevel@tonic-gate 	int		rval;
79157c478bd9Sstevel@tonic-gate 
79167c478bd9Sstevel@tonic-gate 	/* get the dip */
79177c478bd9Sstevel@tonic-gate 	if ((dip = e_ddi_hold_devi_by_dev(dev, 0)) == NULL)
79187c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
79197c478bd9Sstevel@tonic-gate 
79207c478bd9Sstevel@tonic-gate 	rval = i_ddi_devi_get_devid(dev, dip, ret_devid);
79217c478bd9Sstevel@tonic-gate 
79227c478bd9Sstevel@tonic-gate 	ddi_release_devi(dip);		/* e_ddi_hold_devi_by_dev() */
79237c478bd9Sstevel@tonic-gate 	return (rval);
79247c478bd9Sstevel@tonic-gate }
79257c478bd9Sstevel@tonic-gate 
79267c478bd9Sstevel@tonic-gate /*
79277c478bd9Sstevel@tonic-gate  * Return a copy of the minor name for dev_t and spec_type
79287c478bd9Sstevel@tonic-gate  */
79297c478bd9Sstevel@tonic-gate int
79307c478bd9Sstevel@tonic-gate ddi_lyr_get_minor_name(dev_t dev, int spec_type, char **minor_name)
79317c478bd9Sstevel@tonic-gate {
7932b9ccdc5aScth 	char		*buf;
7933b9ccdc5aScth 	int		circ;
79347c478bd9Sstevel@tonic-gate 	dev_info_t	*dip;
79357c478bd9Sstevel@tonic-gate 	char		*nm;
7936b9ccdc5aScth 	int		rval;
79377c478bd9Sstevel@tonic-gate 
7938b9ccdc5aScth 	if ((dip = e_ddi_hold_devi_by_dev(dev, 0)) == NULL) {
79397c478bd9Sstevel@tonic-gate 		*minor_name = NULL;
79407c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
79417c478bd9Sstevel@tonic-gate 	}
79427c478bd9Sstevel@tonic-gate 
7943b9ccdc5aScth 	/* Find the minor name and copy into max size buf */
7944b9ccdc5aScth 	buf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
7945b9ccdc5aScth 	ndi_devi_enter(dip, &circ);
7946b9ccdc5aScth 	nm = i_ddi_devtspectype_to_minorname(dip, dev, spec_type);
7947b9ccdc5aScth 	if (nm)
7948b9ccdc5aScth 		(void) strcpy(buf, nm);
7949b9ccdc5aScth 	ndi_devi_exit(dip, circ);
7950b9ccdc5aScth 	ddi_release_devi(dip);	/* e_ddi_hold_devi_by_dev() */
7951b9ccdc5aScth 
7952b9ccdc5aScth 	if (nm) {
7953b9ccdc5aScth 		/* duplicate into min size buf for return result */
7954b9ccdc5aScth 		*minor_name = i_ddi_strdup(buf, KM_SLEEP);
7955b9ccdc5aScth 		rval = DDI_SUCCESS;
7956b9ccdc5aScth 	} else {
7957b9ccdc5aScth 		*minor_name = NULL;
7958b9ccdc5aScth 		rval = DDI_FAILURE;
79597c478bd9Sstevel@tonic-gate 	}
79607c478bd9Sstevel@tonic-gate 
7961b9ccdc5aScth 	/* free max size buf and return */
7962b9ccdc5aScth 	kmem_free(buf, MAXNAMELEN);
7963b9ccdc5aScth 	return (rval);
79647c478bd9Sstevel@tonic-gate }
79657c478bd9Sstevel@tonic-gate 
79667c478bd9Sstevel@tonic-gate int
79677c478bd9Sstevel@tonic-gate ddi_lyr_devid_to_devlist(
79687c478bd9Sstevel@tonic-gate 	ddi_devid_t	devid,
79697c478bd9Sstevel@tonic-gate 	char		*minor_name,
79707c478bd9Sstevel@tonic-gate 	int		*retndevs,
79717c478bd9Sstevel@tonic-gate 	dev_t		**retdevs)
79727c478bd9Sstevel@tonic-gate {
79737c478bd9Sstevel@tonic-gate 	ASSERT(ddi_devid_valid(devid) == DDI_SUCCESS);
79747c478bd9Sstevel@tonic-gate 
79757c478bd9Sstevel@tonic-gate 	if (e_devid_cache_to_devt_list(devid, minor_name,
79767c478bd9Sstevel@tonic-gate 	    retndevs, retdevs) == DDI_SUCCESS) {
79777c478bd9Sstevel@tonic-gate 		ASSERT(*retndevs > 0);
79787c478bd9Sstevel@tonic-gate 		DDI_DEBUG_DEVID_DEVTS("ddi_lyr_devid_to_devlist",
79797c478bd9Sstevel@tonic-gate 		    *retndevs, *retdevs);
79807c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
79817c478bd9Sstevel@tonic-gate 	}
79827c478bd9Sstevel@tonic-gate 
79837c478bd9Sstevel@tonic-gate 	if (e_ddi_devid_discovery(devid) == DDI_FAILURE) {
79847c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
79857c478bd9Sstevel@tonic-gate 	}
79867c478bd9Sstevel@tonic-gate 
79877c478bd9Sstevel@tonic-gate 	if (e_devid_cache_to_devt_list(devid, minor_name,
79887c478bd9Sstevel@tonic-gate 	    retndevs, retdevs) == DDI_SUCCESS) {
79897c478bd9Sstevel@tonic-gate 		ASSERT(*retndevs > 0);
79907c478bd9Sstevel@tonic-gate 		DDI_DEBUG_DEVID_DEVTS("ddi_lyr_devid_to_devlist",
79917c478bd9Sstevel@tonic-gate 		    *retndevs, *retdevs);
79927c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
79937c478bd9Sstevel@tonic-gate 	}
79947c478bd9Sstevel@tonic-gate 
79957c478bd9Sstevel@tonic-gate 	return (DDI_FAILURE);
79967c478bd9Sstevel@tonic-gate }
79977c478bd9Sstevel@tonic-gate 
79987c478bd9Sstevel@tonic-gate void
79997c478bd9Sstevel@tonic-gate ddi_lyr_free_devlist(dev_t *devlist, int ndevs)
80007c478bd9Sstevel@tonic-gate {
80017c478bd9Sstevel@tonic-gate 	kmem_free(devlist, sizeof (dev_t) * ndevs);
80027c478bd9Sstevel@tonic-gate }
80037c478bd9Sstevel@tonic-gate 
80047c478bd9Sstevel@tonic-gate /*
80057c478bd9Sstevel@tonic-gate  * Note: This will need to be fixed if we ever allow processes to
80067c478bd9Sstevel@tonic-gate  * have more than one data model per exec.
80077c478bd9Sstevel@tonic-gate  */
80087c478bd9Sstevel@tonic-gate model_t
80097c478bd9Sstevel@tonic-gate ddi_mmap_get_model(void)
80107c478bd9Sstevel@tonic-gate {
80117c478bd9Sstevel@tonic-gate 	return (get_udatamodel());
80127c478bd9Sstevel@tonic-gate }
80137c478bd9Sstevel@tonic-gate 
80147c478bd9Sstevel@tonic-gate model_t
80157c478bd9Sstevel@tonic-gate ddi_model_convert_from(model_t model)
80167c478bd9Sstevel@tonic-gate {
80177c478bd9Sstevel@tonic-gate 	return ((model & DDI_MODEL_MASK) & ~DDI_MODEL_NATIVE);
80187c478bd9Sstevel@tonic-gate }
80197c478bd9Sstevel@tonic-gate 
80207c478bd9Sstevel@tonic-gate /*
80217c478bd9Sstevel@tonic-gate  * ddi interfaces managing storage and retrieval of eventcookies.
80227c478bd9Sstevel@tonic-gate  */
80237c478bd9Sstevel@tonic-gate 
80247c478bd9Sstevel@tonic-gate /*
80257c478bd9Sstevel@tonic-gate  * Invoke bus nexus driver's implementation of the
80267c478bd9Sstevel@tonic-gate  * (*bus_remove_eventcall)() interface to remove a registered
80277c478bd9Sstevel@tonic-gate  * callback handler for "event".
80287c478bd9Sstevel@tonic-gate  */
80297c478bd9Sstevel@tonic-gate int
80307c478bd9Sstevel@tonic-gate ddi_remove_event_handler(ddi_callback_id_t id)
80317c478bd9Sstevel@tonic-gate {
80327c478bd9Sstevel@tonic-gate 	ndi_event_callbacks_t *cb = (ndi_event_callbacks_t *)id;
80337c478bd9Sstevel@tonic-gate 	dev_info_t *ddip;
80347c478bd9Sstevel@tonic-gate 
80357c478bd9Sstevel@tonic-gate 	ASSERT(cb);
80367c478bd9Sstevel@tonic-gate 	if (!cb) {
80377c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
80387c478bd9Sstevel@tonic-gate 	}
80397c478bd9Sstevel@tonic-gate 
80407c478bd9Sstevel@tonic-gate 	ddip = NDI_EVENT_DDIP(cb->ndi_evtcb_cookie);
80417c478bd9Sstevel@tonic-gate 	return (ndi_busop_remove_eventcall(ddip, id));
80427c478bd9Sstevel@tonic-gate }
80437c478bd9Sstevel@tonic-gate 
80447c478bd9Sstevel@tonic-gate /*
80457c478bd9Sstevel@tonic-gate  * Invoke bus nexus driver's implementation of the
80467c478bd9Sstevel@tonic-gate  * (*bus_add_eventcall)() interface to register a callback handler
80477c478bd9Sstevel@tonic-gate  * for "event".
80487c478bd9Sstevel@tonic-gate  */
80497c478bd9Sstevel@tonic-gate int
80507c478bd9Sstevel@tonic-gate ddi_add_event_handler(dev_info_t *dip, ddi_eventcookie_t event,
80517c478bd9Sstevel@tonic-gate     void (*handler)(dev_info_t *, ddi_eventcookie_t, void *, void *),
80527c478bd9Sstevel@tonic-gate     void *arg, ddi_callback_id_t *id)
80537c478bd9Sstevel@tonic-gate {
80547c478bd9Sstevel@tonic-gate 	return (ndi_busop_add_eventcall(dip, dip, event, handler, arg, id));
80557c478bd9Sstevel@tonic-gate }
80567c478bd9Sstevel@tonic-gate 
80577c478bd9Sstevel@tonic-gate 
80587c478bd9Sstevel@tonic-gate /*
80597c478bd9Sstevel@tonic-gate  * Return a handle for event "name" by calling up the device tree
80607c478bd9Sstevel@tonic-gate  * hierarchy via  (*bus_get_eventcookie)() interface until claimed
80617c478bd9Sstevel@tonic-gate  * by a bus nexus or top of dev_info tree is reached.
80627c478bd9Sstevel@tonic-gate  */
80637c478bd9Sstevel@tonic-gate int
80647c478bd9Sstevel@tonic-gate ddi_get_eventcookie(dev_info_t *dip, char *name,
80657c478bd9Sstevel@tonic-gate     ddi_eventcookie_t *event_cookiep)
80667c478bd9Sstevel@tonic-gate {
80677c478bd9Sstevel@tonic-gate 	return (ndi_busop_get_eventcookie(dip, dip,
80687c478bd9Sstevel@tonic-gate 	    name, event_cookiep));
80697c478bd9Sstevel@tonic-gate }
80707c478bd9Sstevel@tonic-gate 
80717c478bd9Sstevel@tonic-gate /*
80727c478bd9Sstevel@tonic-gate  * This procedure is provided as the general callback function when
80737c478bd9Sstevel@tonic-gate  * umem_lockmemory calls as_add_callback for long term memory locking.
80747c478bd9Sstevel@tonic-gate  * When as_unmap, as_setprot, or as_free encounter segments which have
80757c478bd9Sstevel@tonic-gate  * locked memory, this callback will be invoked.
80767c478bd9Sstevel@tonic-gate  */
80777c478bd9Sstevel@tonic-gate void
80787c478bd9Sstevel@tonic-gate umem_lock_undo(struct as *as, void *arg, uint_t event)
80797c478bd9Sstevel@tonic-gate {
80807c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(as, event))
80817c478bd9Sstevel@tonic-gate 	struct ddi_umem_cookie *cp = (struct ddi_umem_cookie *)arg;
80827c478bd9Sstevel@tonic-gate 
80837c478bd9Sstevel@tonic-gate 	/*
80847c478bd9Sstevel@tonic-gate 	 * Call the cleanup function.  Decrement the cookie reference
80857c478bd9Sstevel@tonic-gate 	 * count, if it goes to zero, return the memory for the cookie.
80867c478bd9Sstevel@tonic-gate 	 * The i_ddi_umem_unlock for this cookie may or may not have been
80877c478bd9Sstevel@tonic-gate 	 * called already.  It is the responsibility of the caller of
80887c478bd9Sstevel@tonic-gate 	 * umem_lockmemory to handle the case of the cleanup routine
80897c478bd9Sstevel@tonic-gate 	 * being called after a ddi_umem_unlock for the cookie
80907c478bd9Sstevel@tonic-gate 	 * was called.
80917c478bd9Sstevel@tonic-gate 	 */
80927c478bd9Sstevel@tonic-gate 
80937c478bd9Sstevel@tonic-gate 	(*cp->callbacks.cbo_umem_lock_cleanup)((ddi_umem_cookie_t)cp);
80947c478bd9Sstevel@tonic-gate 
80957c478bd9Sstevel@tonic-gate 	/* remove the cookie if reference goes to zero */
80961a5e258fSJosef 'Jeff' Sipek 	if (atomic_dec_ulong_nv((ulong_t *)(&(cp->cook_refcnt))) == 0) {
80977c478bd9Sstevel@tonic-gate 		kmem_free(cp, sizeof (struct ddi_umem_cookie));
80987c478bd9Sstevel@tonic-gate 	}
80997c478bd9Sstevel@tonic-gate }
81007c478bd9Sstevel@tonic-gate 
81017c478bd9Sstevel@tonic-gate /*
81027c478bd9Sstevel@tonic-gate  * The following two Consolidation Private routines provide generic
81037c478bd9Sstevel@tonic-gate  * interfaces to increase/decrease the amount of device-locked memory.
81047c478bd9Sstevel@tonic-gate  *
81057c478bd9Sstevel@tonic-gate  * To keep project_rele and project_hold consistent, i_ddi_decr_locked_memory()
81067c478bd9Sstevel@tonic-gate  * must be called every time i_ddi_incr_locked_memory() is called.
81077c478bd9Sstevel@tonic-gate  */
81087c478bd9Sstevel@tonic-gate int
81097c478bd9Sstevel@tonic-gate /* ARGSUSED */
8110c6939658Ssl108498 i_ddi_incr_locked_memory(proc_t *procp, rctl_qty_t inc)
81117c478bd9Sstevel@tonic-gate {
8112c6939658Ssl108498 	ASSERT(procp != NULL);
8113c6939658Ssl108498 	mutex_enter(&procp->p_lock);
8114c6939658Ssl108498 	if (rctl_incr_locked_mem(procp, NULL, inc, 1)) {
8115c6939658Ssl108498 		mutex_exit(&procp->p_lock);
81167c478bd9Sstevel@tonic-gate 		return (ENOMEM);
81177c478bd9Sstevel@tonic-gate 	}
8118c6939658Ssl108498 	mutex_exit(&procp->p_lock);
81197c478bd9Sstevel@tonic-gate 	return (0);
81207c478bd9Sstevel@tonic-gate }
81217c478bd9Sstevel@tonic-gate 
81227c478bd9Sstevel@tonic-gate /*
81237c478bd9Sstevel@tonic-gate  * To keep project_rele and project_hold consistent, i_ddi_incr_locked_memory()
81247c478bd9Sstevel@tonic-gate  * must be called every time i_ddi_decr_locked_memory() is called.
81257c478bd9Sstevel@tonic-gate  */
81267c478bd9Sstevel@tonic-gate /* ARGSUSED */
81277c478bd9Sstevel@tonic-gate void
8128c6939658Ssl108498 i_ddi_decr_locked_memory(proc_t *procp, rctl_qty_t dec)
81297c478bd9Sstevel@tonic-gate {
8130c6939658Ssl108498 	ASSERT(procp != NULL);
8131c6939658Ssl108498 	mutex_enter(&procp->p_lock);
8132c6939658Ssl108498 	rctl_decr_locked_mem(procp, NULL, dec, 1);
8133c6939658Ssl108498 	mutex_exit(&procp->p_lock);
81347c478bd9Sstevel@tonic-gate }
81357c478bd9Sstevel@tonic-gate 
81367c478bd9Sstevel@tonic-gate /*
8137c3ffbee3S  * The cookie->upd_max_lock_rctl flag is used to determine if we should
8138c3ffbee3S  * charge device locked memory to the max-locked-memory rctl.  Tracking
8139c3ffbee3S  * device locked memory causes the rctl locks to get hot under high-speed
8140c3ffbee3S  * I/O such as RDSv3 over IB.  If there is no max-locked-memory rctl limit,
8141c3ffbee3S  * we bypass charging the locked memory to the rctl altogether.  The cookie's
8142c3ffbee3S  * flag tells us if the rctl value should be updated when unlocking the memory,
8143c3ffbee3S  * in case the rctl gets changed after the memory was locked.  Any device
8144c3ffbee3S  * locked memory in that rare case will not be counted toward the rctl limit.
8145c3ffbee3S  *
8146c3ffbee3S  * When tracking the locked memory, the kproject_t parameter is always NULL
8147c3ffbee3S  * in the code paths:
8148c3ffbee3S  *	i_ddi_incr_locked_memory -> rctl_incr_locked_mem
8149c3ffbee3S  *	i_ddi_decr_locked_memory -> rctl_decr_locked_mem
8150c3ffbee3S  * Thus, we always use the tk_proj member to check the projp setting.
8151c3ffbee3S  */
8152c3ffbee3S static void
8153c3ffbee3S init_lockedmem_rctl_flag(struct ddi_umem_cookie *cookie)
8154c3ffbee3S {
8155c3ffbee3S 	proc_t		*p;
8156c3ffbee3S 	kproject_t	*projp;
8157c3ffbee3S 	zone_t		*zonep;
8158c3ffbee3S 
8159c3ffbee3S 	ASSERT(cookie);
8160c3ffbee3S 	p = cookie->procp;
8161c3ffbee3S 	ASSERT(p);
8162c3ffbee3S 
8163c3ffbee3S 	zonep = p->p_zone;
8164c3ffbee3S 	projp = p->p_task->tk_proj;
8165c3ffbee3S 
8166c3ffbee3S 	ASSERT(zonep);
8167c3ffbee3S 	ASSERT(projp);
8168c3ffbee3S 
8169c3ffbee3S 	if (zonep->zone_locked_mem_ctl == UINT64_MAX &&
8170c3ffbee3S 	    projp->kpj_data.kpd_locked_mem_ctl == UINT64_MAX)
8171c3ffbee3S 		cookie->upd_max_lock_rctl = 0;
8172c3ffbee3S 	else
8173c3ffbee3S 		cookie->upd_max_lock_rctl = 1;
8174c3ffbee3S }
8175c3ffbee3S 
8176c3ffbee3S /*
8177c6939658Ssl108498  * This routine checks if the max-locked-memory resource ctl is
81787c478bd9Sstevel@tonic-gate  * exceeded, if not increments it, grabs a hold on the project.
81797c478bd9Sstevel@tonic-gate  * Returns 0 if successful otherwise returns error code
81807c478bd9Sstevel@tonic-gate  */
81817c478bd9Sstevel@tonic-gate static int
81827c478bd9Sstevel@tonic-gate umem_incr_devlockmem(struct ddi_umem_cookie *cookie)
81837c478bd9Sstevel@tonic-gate {
81847c478bd9Sstevel@tonic-gate 	proc_t		*procp;
81857c478bd9Sstevel@tonic-gate 	int		ret;
81867c478bd9Sstevel@tonic-gate 
81877c478bd9Sstevel@tonic-gate 	ASSERT(cookie);
8188c3ffbee3S 	if (cookie->upd_max_lock_rctl == 0)
8189c3ffbee3S 		return (0);
8190c3ffbee3S 
81917c478bd9Sstevel@tonic-gate 	procp = cookie->procp;
81927c478bd9Sstevel@tonic-gate 	ASSERT(procp);
81937c478bd9Sstevel@tonic-gate 
8194c6939658Ssl108498 	if ((ret = i_ddi_incr_locked_memory(procp,
8195c6939658Ssl108498 	    cookie->size)) != 0) {
81967c478bd9Sstevel@tonic-gate 		return (ret);
81977c478bd9Sstevel@tonic-gate 	}
81987c478bd9Sstevel@tonic-gate 	return (0);
81997c478bd9Sstevel@tonic-gate }
82007c478bd9Sstevel@tonic-gate 
82017c478bd9Sstevel@tonic-gate /*
8202c6939658Ssl108498  * Decrements the max-locked-memory resource ctl and releases
82037c478bd9Sstevel@tonic-gate  * the hold on the project that was acquired during umem_incr_devlockmem
82047c478bd9Sstevel@tonic-gate  */
82057c478bd9Sstevel@tonic-gate static void
82067c478bd9Sstevel@tonic-gate umem_decr_devlockmem(struct ddi_umem_cookie *cookie)
82077c478bd9Sstevel@tonic-gate {
8208c6939658Ssl108498 	proc_t		*proc;
82097c478bd9Sstevel@tonic-gate 
8210c3ffbee3S 	if (cookie->upd_max_lock_rctl == 0)
8211c3ffbee3S 		return;
8212c3ffbee3S 
8213c6939658Ssl108498 	proc = (proc_t *)cookie->procp;
8214c6939658Ssl108498 	if (!proc)
82157c478bd9Sstevel@tonic-gate 		return;
82167c478bd9Sstevel@tonic-gate 
8217c6939658Ssl108498 	i_ddi_decr_locked_memory(proc, cookie->size);
82187c478bd9Sstevel@tonic-gate }
82197c478bd9Sstevel@tonic-gate 
82207c478bd9Sstevel@tonic-gate /*
82217c478bd9Sstevel@tonic-gate  * A consolidation private function which is essentially equivalent to
82227c478bd9Sstevel@tonic-gate  * ddi_umem_lock but with the addition of arguments ops_vector and procp.
82237c478bd9Sstevel@tonic-gate  * A call to as_add_callback is done if DDI_UMEMLOCK_LONGTERM is set, and
82247c478bd9Sstevel@tonic-gate  * the ops_vector is valid.
82257c478bd9Sstevel@tonic-gate  *
82267c478bd9Sstevel@tonic-gate  * Lock the virtual address range in the current process and create a
82277c478bd9Sstevel@tonic-gate  * ddi_umem_cookie (of type UMEM_LOCKED). This can be used to pass to
82287c478bd9Sstevel@tonic-gate  * ddi_umem_iosetup to create a buf or do devmap_umem_setup/remap to export
82297c478bd9Sstevel@tonic-gate  * to user space.
82307c478bd9Sstevel@tonic-gate  *
82317c478bd9Sstevel@tonic-gate  * Note: The resource control accounting currently uses a full charge model
82327c478bd9Sstevel@tonic-gate  * in other words attempts to lock the same/overlapping areas of memory
82337c478bd9Sstevel@tonic-gate  * will deduct the full size of the buffer from the projects running
82347c478bd9Sstevel@tonic-gate  * counter for the device locked memory.
82357c478bd9Sstevel@tonic-gate  *
82367c478bd9Sstevel@tonic-gate  * addr, size should be PAGESIZE aligned
82377c478bd9Sstevel@tonic-gate  *
82387c478bd9Sstevel@tonic-gate  * flags - DDI_UMEMLOCK_READ, DDI_UMEMLOCK_WRITE or both
82397c478bd9Sstevel@tonic-gate  *	identifies whether the locked memory will be read or written or both
82407c478bd9Sstevel@tonic-gate  *      DDI_UMEMLOCK_LONGTERM  must be set when the locking will
82417c478bd9Sstevel@tonic-gate  * be maintained for an indefinitely long period (essentially permanent),
82427c478bd9Sstevel@tonic-gate  * rather than for what would be required for a typical I/O completion.
82437c478bd9Sstevel@tonic-gate  * When DDI_UMEMLOCK_LONGTERM is set, umem_lockmemory will return EFAULT
82447c478bd9Sstevel@tonic-gate  * if the memory pertains to a regular file which is mapped MAP_SHARED.
82457c478bd9Sstevel@tonic-gate  * This is to prevent a deadlock if a file truncation is attempted after
82467c478bd9Sstevel@tonic-gate  * after the locking is done.
82477c478bd9Sstevel@tonic-gate  *
82487c478bd9Sstevel@tonic-gate  * Returns 0 on success
82497c478bd9Sstevel@tonic-gate  *	EINVAL - for invalid parameters
82507c478bd9Sstevel@tonic-gate  *	EPERM, ENOMEM and other error codes returned by as_pagelock
82517c478bd9Sstevel@tonic-gate  *	ENOMEM - is returned if the current request to lock memory exceeds
8252c6939658Ssl108498  *		*.max-locked-memory resource control value.
82537c478bd9Sstevel@tonic-gate  *      EFAULT - memory pertains to a regular file mapped shared and
82547c478bd9Sstevel@tonic-gate  *		and DDI_UMEMLOCK_LONGTERM flag is set
82557c478bd9Sstevel@tonic-gate  *	EAGAIN - could not start the ddi_umem_unlock list processing thread
82567c478bd9Sstevel@tonic-gate  */
82577c478bd9Sstevel@tonic-gate int
82587c478bd9Sstevel@tonic-gate umem_lockmemory(caddr_t addr, size_t len, int flags, ddi_umem_cookie_t *cookie,
82597c478bd9Sstevel@tonic-gate 		struct umem_callback_ops *ops_vector,
82607c478bd9Sstevel@tonic-gate 		proc_t *procp)
82617c478bd9Sstevel@tonic-gate {
82627c478bd9Sstevel@tonic-gate 	int	error;
82637c478bd9Sstevel@tonic-gate 	struct ddi_umem_cookie *p;
82647c478bd9Sstevel@tonic-gate 	void	(*driver_callback)() = NULL;
8265d863b343SBill Taylor 	struct as *as;
82667c478bd9Sstevel@tonic-gate 	struct seg		*seg;
82677c478bd9Sstevel@tonic-gate 	vnode_t			*vp;
82687c478bd9Sstevel@tonic-gate 
8269d863b343SBill Taylor 	/* Allow device drivers to not have to reference "curproc" */
8270d863b343SBill Taylor 	if (procp == NULL)
8271d863b343SBill Taylor 		procp = curproc;
8272d863b343SBill Taylor 	as = procp->p_as;
82737c478bd9Sstevel@tonic-gate 	*cookie = NULL;		/* in case of any error return */
82747c478bd9Sstevel@tonic-gate 
82757c478bd9Sstevel@tonic-gate 	/* These are the only three valid flags */
82767c478bd9Sstevel@tonic-gate 	if ((flags & ~(DDI_UMEMLOCK_READ | DDI_UMEMLOCK_WRITE |
82777c478bd9Sstevel@tonic-gate 	    DDI_UMEMLOCK_LONGTERM)) != 0)
82787c478bd9Sstevel@tonic-gate 		return (EINVAL);
82797c478bd9Sstevel@tonic-gate 
82807c478bd9Sstevel@tonic-gate 	/* At least one (can be both) of the two access flags must be set */
82817c478bd9Sstevel@tonic-gate 	if ((flags & (DDI_UMEMLOCK_READ | DDI_UMEMLOCK_WRITE)) == 0)
82827c478bd9Sstevel@tonic-gate 		return (EINVAL);
82837c478bd9Sstevel@tonic-gate 
82847c478bd9Sstevel@tonic-gate 	/* addr and len must be page-aligned */
82857c478bd9Sstevel@tonic-gate 	if (((uintptr_t)addr & PAGEOFFSET) != 0)
82867c478bd9Sstevel@tonic-gate 		return (EINVAL);
82877c478bd9Sstevel@tonic-gate 
82887c478bd9Sstevel@tonic-gate 	if ((len & PAGEOFFSET) != 0)
82897c478bd9Sstevel@tonic-gate 		return (EINVAL);
82907c478bd9Sstevel@tonic-gate 
82917c478bd9Sstevel@tonic-gate 	/*
82927c478bd9Sstevel@tonic-gate 	 * For longterm locking a driver callback must be specified; if
82937c478bd9Sstevel@tonic-gate 	 * not longterm then a callback is optional.
82947c478bd9Sstevel@tonic-gate 	 */
82957c478bd9Sstevel@tonic-gate 	if (ops_vector != NULL) {
82967c478bd9Sstevel@tonic-gate 		if (ops_vector->cbo_umem_callback_version !=
82977c478bd9Sstevel@tonic-gate 		    UMEM_CALLBACK_VERSION)
82987c478bd9Sstevel@tonic-gate 			return (EINVAL);
82997c478bd9Sstevel@tonic-gate 		else
83007c478bd9Sstevel@tonic-gate 			driver_callback = ops_vector->cbo_umem_lock_cleanup;
83017c478bd9Sstevel@tonic-gate 	}
83027c478bd9Sstevel@tonic-gate 	if ((driver_callback == NULL) && (flags & DDI_UMEMLOCK_LONGTERM))
83037c478bd9Sstevel@tonic-gate 		return (EINVAL);
83047c478bd9Sstevel@tonic-gate 
83057c478bd9Sstevel@tonic-gate 	/*
83067c478bd9Sstevel@tonic-gate 	 * Call i_ddi_umem_unlock_thread_start if necessary.  It will
83077c478bd9Sstevel@tonic-gate 	 * be called on first ddi_umem_lock or umem_lockmemory call.
83087c478bd9Sstevel@tonic-gate 	 */
83097c478bd9Sstevel@tonic-gate 	if (ddi_umem_unlock_thread == NULL)
83107c478bd9Sstevel@tonic-gate 		i_ddi_umem_unlock_thread_start();
83117c478bd9Sstevel@tonic-gate 
83127c478bd9Sstevel@tonic-gate 	/* Allocate memory for the cookie */
83137c478bd9Sstevel@tonic-gate 	p = kmem_zalloc(sizeof (struct ddi_umem_cookie), KM_SLEEP);
83147c478bd9Sstevel@tonic-gate 
83157c478bd9Sstevel@tonic-gate 	/* Convert the flags to seg_rw type */
83167c478bd9Sstevel@tonic-gate 	if (flags & DDI_UMEMLOCK_WRITE) {
83177c478bd9Sstevel@tonic-gate 		p->s_flags = S_WRITE;
83187c478bd9Sstevel@tonic-gate 	} else {
83197c478bd9Sstevel@tonic-gate 		p->s_flags = S_READ;
83207c478bd9Sstevel@tonic-gate 	}
83217c478bd9Sstevel@tonic-gate 
83227c478bd9Sstevel@tonic-gate 	/* Store procp in cookie for later iosetup/unlock */
83237c478bd9Sstevel@tonic-gate 	p->procp = (void *)procp;
83247c478bd9Sstevel@tonic-gate 
83257c478bd9Sstevel@tonic-gate 	/*
83267c478bd9Sstevel@tonic-gate 	 * Store the struct as pointer in cookie for later use by
83277c478bd9Sstevel@tonic-gate 	 * ddi_umem_unlock.  The proc->p_as will be stale if ddi_umem_unlock
83287c478bd9Sstevel@tonic-gate 	 * is called after relvm is called.
83297c478bd9Sstevel@tonic-gate 	 */
83307c478bd9Sstevel@tonic-gate 	p->asp = as;
83317c478bd9Sstevel@tonic-gate 
83327c478bd9Sstevel@tonic-gate 	/*
83337c478bd9Sstevel@tonic-gate 	 * The size field is needed for lockmem accounting.
83347c478bd9Sstevel@tonic-gate 	 */
83357c478bd9Sstevel@tonic-gate 	p->size = len;
8336c3ffbee3S 	init_lockedmem_rctl_flag(p);
83377c478bd9Sstevel@tonic-gate 
83387c478bd9Sstevel@tonic-gate 	if (umem_incr_devlockmem(p) != 0) {
83397c478bd9Sstevel@tonic-gate 		/*
83407c478bd9Sstevel@tonic-gate 		 * The requested memory cannot be locked
83417c478bd9Sstevel@tonic-gate 		 */
83427c478bd9Sstevel@tonic-gate 		kmem_free(p, sizeof (struct ddi_umem_cookie));
83437c478bd9Sstevel@tonic-gate 		*cookie = (ddi_umem_cookie_t)NULL;
83447c478bd9Sstevel@tonic-gate 		return (ENOMEM);
83457c478bd9Sstevel@tonic-gate 	}
83467c478bd9Sstevel@tonic-gate 
83477c478bd9Sstevel@tonic-gate 	/* Lock the pages corresponding to addr, len in memory */
83487c478bd9Sstevel@tonic-gate 	error = as_pagelock(as, &(p->pparray), addr, len, p->s_flags);
83497c478bd9Sstevel@tonic-gate 	if (error != 0) {
83507c478bd9Sstevel@tonic-gate 		umem_decr_devlockmem(p);
83517c478bd9Sstevel@tonic-gate 		kmem_free(p, sizeof (struct ddi_umem_cookie));
83527c478bd9Sstevel@tonic-gate 		*cookie = (ddi_umem_cookie_t)NULL;
83537c478bd9Sstevel@tonic-gate 		return (error);
83547c478bd9Sstevel@tonic-gate 	}
83557c478bd9Sstevel@tonic-gate 
83567c478bd9Sstevel@tonic-gate 	/*
83577c478bd9Sstevel@tonic-gate 	 * For longterm locking the addr must pertain to a seg_vn segment or
83587c478bd9Sstevel@tonic-gate 	 * or a seg_spt segment.
83597c478bd9Sstevel@tonic-gate 	 * If the segment pertains to a regular file, it cannot be
83607c478bd9Sstevel@tonic-gate 	 * mapped MAP_SHARED.
83617c478bd9Sstevel@tonic-gate 	 * This is to prevent a deadlock if a file truncation is attempted
83627c478bd9Sstevel@tonic-gate 	 * after the locking is done.
83637c478bd9Sstevel@tonic-gate 	 * Doing this after as_pagelock guarantees persistence of the as; if
83647c478bd9Sstevel@tonic-gate 	 * an unacceptable segment is found, the cleanup includes calling
83657c478bd9Sstevel@tonic-gate 	 * as_pageunlock before returning EFAULT.
8366d863b343SBill Taylor 	 *
8367d863b343SBill Taylor 	 * segdev is allowed here as it is already locked.  This allows
8368d863b343SBill Taylor 	 * for memory exported by drivers through mmap() (which is already
8369d863b343SBill Taylor 	 * locked) to be allowed for LONGTERM.
83707c478bd9Sstevel@tonic-gate 	 */
83717c478bd9Sstevel@tonic-gate 	if (flags & DDI_UMEMLOCK_LONGTERM) {
83727c478bd9Sstevel@tonic-gate 		extern  struct seg_ops segspt_shmops;
8373d863b343SBill Taylor 		extern	struct seg_ops segdev_ops;
8374dc32d872SJosef 'Jeff' Sipek 		AS_LOCK_ENTER(as, RW_READER);
83757c478bd9Sstevel@tonic-gate 		for (seg = as_segat(as, addr); ; seg = AS_SEGNEXT(as, seg)) {
83767c478bd9Sstevel@tonic-gate 			if (seg == NULL || seg->s_base > addr + len)
83777c478bd9Sstevel@tonic-gate 				break;
8378d863b343SBill Taylor 			if (seg->s_ops == &segdev_ops)
8379d863b343SBill Taylor 				continue;
83807c478bd9Sstevel@tonic-gate 			if (((seg->s_ops != &segvn_ops) &&
83817c478bd9Sstevel@tonic-gate 			    (seg->s_ops != &segspt_shmops)) ||
83827c478bd9Sstevel@tonic-gate 			    ((SEGOP_GETVP(seg, addr, &vp) == 0 &&
83837c478bd9Sstevel@tonic-gate 			    vp != NULL && vp->v_type == VREG) &&
83847c478bd9Sstevel@tonic-gate 			    (SEGOP_GETTYPE(seg, addr) & MAP_SHARED))) {
83857c478bd9Sstevel@tonic-gate 				as_pageunlock(as, p->pparray,
83867c478bd9Sstevel@tonic-gate 				    addr, len, p->s_flags);
8387dc32d872SJosef 'Jeff' Sipek 				AS_LOCK_EXIT(as);
83887c478bd9Sstevel@tonic-gate 				umem_decr_devlockmem(p);
83897c478bd9Sstevel@tonic-gate 				kmem_free(p, sizeof (struct ddi_umem_cookie));
83907c478bd9Sstevel@tonic-gate 				*cookie = (ddi_umem_cookie_t)NULL;
83917c478bd9Sstevel@tonic-gate 				return (EFAULT);
83927c478bd9Sstevel@tonic-gate 			}
83937c478bd9Sstevel@tonic-gate 		}
8394dc32d872SJosef 'Jeff' Sipek 		AS_LOCK_EXIT(as);
83957c478bd9Sstevel@tonic-gate 	}
83967c478bd9Sstevel@tonic-gate 
83977c478bd9Sstevel@tonic-gate 
83987c478bd9Sstevel@tonic-gate 	/* Initialize the fields in the ddi_umem_cookie */
83997c478bd9Sstevel@tonic-gate 	p->cvaddr = addr;
84007c478bd9Sstevel@tonic-gate 	p->type = UMEM_LOCKED;
84017c478bd9Sstevel@tonic-gate 	if (driver_callback != NULL) {
84027c478bd9Sstevel@tonic-gate 		/* i_ddi_umem_unlock and umem_lock_undo may need the cookie */
84037c478bd9Sstevel@tonic-gate 		p->cook_refcnt = 2;
84047c478bd9Sstevel@tonic-gate 		p->callbacks = *ops_vector;
84057c478bd9Sstevel@tonic-gate 	} else {
84067c478bd9Sstevel@tonic-gate 		/* only i_ddi_umme_unlock needs the cookie */
84077c478bd9Sstevel@tonic-gate 		p->cook_refcnt = 1;
84087c478bd9Sstevel@tonic-gate 	}
84097c478bd9Sstevel@tonic-gate 
84107c478bd9Sstevel@tonic-gate 	*cookie = (ddi_umem_cookie_t)p;
84117c478bd9Sstevel@tonic-gate 
84127c478bd9Sstevel@tonic-gate 	/*
84137c478bd9Sstevel@tonic-gate 	 * If a driver callback was specified, add an entry to the
84147c478bd9Sstevel@tonic-gate 	 * as struct callback list. The as_pagelock above guarantees
84157c478bd9Sstevel@tonic-gate 	 * the persistence of as.
84167c478bd9Sstevel@tonic-gate 	 */
84177c478bd9Sstevel@tonic-gate 	if (driver_callback) {
84187c478bd9Sstevel@tonic-gate 		error = as_add_callback(as, umem_lock_undo, p, AS_ALL_EVENT,
84197c478bd9Sstevel@tonic-gate 		    addr, len, KM_SLEEP);
84207c478bd9Sstevel@tonic-gate 		if (error != 0) {
84217c478bd9Sstevel@tonic-gate 			as_pageunlock(as, p->pparray,
84227c478bd9Sstevel@tonic-gate 			    addr, len, p->s_flags);
84237c478bd9Sstevel@tonic-gate 			umem_decr_devlockmem(p);
84247c478bd9Sstevel@tonic-gate 			kmem_free(p, sizeof (struct ddi_umem_cookie));
84257c478bd9Sstevel@tonic-gate 			*cookie = (ddi_umem_cookie_t)NULL;
84267c478bd9Sstevel@tonic-gate 		}
84277c478bd9Sstevel@tonic-gate 	}
84287c478bd9Sstevel@tonic-gate 	return (error);
84297c478bd9Sstevel@tonic-gate }
84307c478bd9Sstevel@tonic-gate 
84317c478bd9Sstevel@tonic-gate /*
84327c478bd9Sstevel@tonic-gate  * Unlock the pages locked by ddi_umem_lock or umem_lockmemory and free
84337c478bd9Sstevel@tonic-gate  * the cookie.  Called from i_ddi_umem_unlock_thread.
84347c478bd9Sstevel@tonic-gate  */
84357c478bd9Sstevel@tonic-gate 
84367c478bd9Sstevel@tonic-gate static void
84377c478bd9Sstevel@tonic-gate i_ddi_umem_unlock(struct ddi_umem_cookie *p)
84387c478bd9Sstevel@tonic-gate {
84397c478bd9Sstevel@tonic-gate 	uint_t	rc;
84407c478bd9Sstevel@tonic-gate 
84417c478bd9Sstevel@tonic-gate 	/*
84427c478bd9Sstevel@tonic-gate 	 * There is no way to determine whether a callback to
84437c478bd9Sstevel@tonic-gate 	 * umem_lock_undo was registered via as_add_callback.
84447c478bd9Sstevel@tonic-gate 	 * (i.e. umem_lockmemory was called with DDI_MEMLOCK_LONGTERM and
84457c478bd9Sstevel@tonic-gate 	 * a valid callback function structure.)  as_delete_callback
84467c478bd9Sstevel@tonic-gate 	 * is called to delete a possible registered callback.  If the
84477c478bd9Sstevel@tonic-gate 	 * return from as_delete_callbacks is AS_CALLBACK_DELETED, it
84487c478bd9Sstevel@tonic-gate 	 * indicates that there was a callback registered, and that is was
84497c478bd9Sstevel@tonic-gate 	 * successfully deleted.  Thus, the cookie reference count
84507c478bd9Sstevel@tonic-gate 	 * will never be decremented by umem_lock_undo.  Just return the
84517c478bd9Sstevel@tonic-gate 	 * memory for the cookie, since both users of the cookie are done.
84527c478bd9Sstevel@tonic-gate 	 * A return of AS_CALLBACK_NOTFOUND indicates a callback was
84537c478bd9Sstevel@tonic-gate 	 * never registered.  A return of AS_CALLBACK_DELETE_DEFERRED
84547c478bd9Sstevel@tonic-gate 	 * indicates that callback processing is taking place and, and
84557c478bd9Sstevel@tonic-gate 	 * umem_lock_undo is, or will be, executing, and thus decrementing
84567c478bd9Sstevel@tonic-gate 	 * the cookie reference count when it is complete.
84577c478bd9Sstevel@tonic-gate 	 *
84587c478bd9Sstevel@tonic-gate 	 * This needs to be done before as_pageunlock so that the
84597c478bd9Sstevel@tonic-gate 	 * persistence of as is guaranteed because of the locked pages.
84607c478bd9Sstevel@tonic-gate 	 *
84617c478bd9Sstevel@tonic-gate 	 */
84627c478bd9Sstevel@tonic-gate 	rc = as_delete_callback(p->asp, p);
84637c478bd9Sstevel@tonic-gate 
84647c478bd9Sstevel@tonic-gate 
84657c478bd9Sstevel@tonic-gate 	/*
84667c478bd9Sstevel@tonic-gate 	 * The proc->p_as will be stale if i_ddi_umem_unlock is called
84677c478bd9Sstevel@tonic-gate 	 * after relvm is called so use p->asp.
84687c478bd9Sstevel@tonic-gate 	 */
84697c478bd9Sstevel@tonic-gate 	as_pageunlock(p->asp, p->pparray, p->cvaddr, p->size, p->s_flags);
84707c478bd9Sstevel@tonic-gate 
84717c478bd9Sstevel@tonic-gate 	/*
84727c478bd9Sstevel@tonic-gate 	 * Now that we have unlocked the memory decrement the
8473c6939658Ssl108498 	 * *.max-locked-memory rctl
84747c478bd9Sstevel@tonic-gate 	 */
84757c478bd9Sstevel@tonic-gate 	umem_decr_devlockmem(p);
84767c478bd9Sstevel@tonic-gate 
84777c478bd9Sstevel@tonic-gate 	if (rc == AS_CALLBACK_DELETED) {
84787c478bd9Sstevel@tonic-gate 		/* umem_lock_undo will not happen, return the cookie memory */
84797c478bd9Sstevel@tonic-gate 		ASSERT(p->cook_refcnt == 2);
84807c478bd9Sstevel@tonic-gate 		kmem_free(p, sizeof (struct ddi_umem_cookie));
84817c478bd9Sstevel@tonic-gate 	} else {
84827c478bd9Sstevel@tonic-gate 		/*
84837c478bd9Sstevel@tonic-gate 		 * umem_undo_lock may happen if as_delete_callback returned
84847c478bd9Sstevel@tonic-gate 		 * AS_CALLBACK_DELETE_DEFERRED.  In that case, decrement the
84857c478bd9Sstevel@tonic-gate 		 * reference count, atomically, and return the cookie
84867c478bd9Sstevel@tonic-gate 		 * memory if the reference count goes to zero.  The only
84877c478bd9Sstevel@tonic-gate 		 * other value for rc is AS_CALLBACK_NOTFOUND.  In that
84887c478bd9Sstevel@tonic-gate 		 * case, just return the cookie memory.
84897c478bd9Sstevel@tonic-gate 		 */
84907c478bd9Sstevel@tonic-gate 		if ((rc != AS_CALLBACK_DELETE_DEFERRED) ||
84911a5e258fSJosef 'Jeff' Sipek 		    (atomic_dec_ulong_nv((ulong_t *)(&(p->cook_refcnt)))
84927c478bd9Sstevel@tonic-gate 		    == 0)) {
84937c478bd9Sstevel@tonic-gate 			kmem_free(p, sizeof (struct ddi_umem_cookie));
84947c478bd9Sstevel@tonic-gate 		}
84957c478bd9Sstevel@tonic-gate 	}
84967c478bd9Sstevel@tonic-gate }
84977c478bd9Sstevel@tonic-gate 
84987c478bd9Sstevel@tonic-gate /*
84997c478bd9Sstevel@tonic-gate  * i_ddi_umem_unlock_thread - deferred ddi_umem_unlock list handler.
85007c478bd9Sstevel@tonic-gate  *
85017c478bd9Sstevel@tonic-gate  * Call i_ddi_umem_unlock for entries in the ddi_umem_unlock list
85027c478bd9Sstevel@tonic-gate  * until it is empty.  Then, wait for more to be added.  This thread is awoken
85037c478bd9Sstevel@tonic-gate  * via calls to ddi_umem_unlock.
85047c478bd9Sstevel@tonic-gate  */
85057c478bd9Sstevel@tonic-gate 
85067c478bd9Sstevel@tonic-gate static void
85077c478bd9Sstevel@tonic-gate i_ddi_umem_unlock_thread(void)
85087c478bd9Sstevel@tonic-gate {
85097c478bd9Sstevel@tonic-gate 	struct ddi_umem_cookie	*ret_cookie;
85107c478bd9Sstevel@tonic-gate 	callb_cpr_t	cprinfo;
85117c478bd9Sstevel@tonic-gate 
85127c478bd9Sstevel@tonic-gate 	/* process the ddi_umem_unlock list */
85137c478bd9Sstevel@tonic-gate 	CALLB_CPR_INIT(&cprinfo, &ddi_umem_unlock_mutex,
85147c478bd9Sstevel@tonic-gate 	    callb_generic_cpr, "unlock_thread");
85157c478bd9Sstevel@tonic-gate 	for (;;) {
85167c478bd9Sstevel@tonic-gate 		mutex_enter(&ddi_umem_unlock_mutex);
85177c478bd9Sstevel@tonic-gate 		if (ddi_umem_unlock_head != NULL) {	/* list not empty */
85187c478bd9Sstevel@tonic-gate 			ret_cookie = ddi_umem_unlock_head;
85197c478bd9Sstevel@tonic-gate 			/* take if off the list */
85207c478bd9Sstevel@tonic-gate 			if ((ddi_umem_unlock_head =
85217c478bd9Sstevel@tonic-gate 			    ddi_umem_unlock_head->unl_forw) == NULL) {
85227c478bd9Sstevel@tonic-gate 				ddi_umem_unlock_tail = NULL;
85237c478bd9Sstevel@tonic-gate 			}
85247c478bd9Sstevel@tonic-gate 			mutex_exit(&ddi_umem_unlock_mutex);
85257c478bd9Sstevel@tonic-gate 			/* unlock the pages in this cookie */
85267c478bd9Sstevel@tonic-gate 			(void) i_ddi_umem_unlock(ret_cookie);
85277c478bd9Sstevel@tonic-gate 		} else {   /* list is empty, wait for next ddi_umem_unlock */
85287c478bd9Sstevel@tonic-gate 			CALLB_CPR_SAFE_BEGIN(&cprinfo);
85297c478bd9Sstevel@tonic-gate 			cv_wait(&ddi_umem_unlock_cv, &ddi_umem_unlock_mutex);
85307c478bd9Sstevel@tonic-gate 			CALLB_CPR_SAFE_END(&cprinfo, &ddi_umem_unlock_mutex);
85317c478bd9Sstevel@tonic-gate 			mutex_exit(&ddi_umem_unlock_mutex);
85327c478bd9Sstevel@tonic-gate 		}
85337c478bd9Sstevel@tonic-gate 	}
85347c478bd9Sstevel@tonic-gate 	/* ddi_umem_unlock_thread does not exit */
85357c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
85367c478bd9Sstevel@tonic-gate }
85377c478bd9Sstevel@tonic-gate 
85387c478bd9Sstevel@tonic-gate /*
85397c478bd9Sstevel@tonic-gate  * Start the thread that will process the ddi_umem_unlock list if it is
85407c478bd9Sstevel@tonic-gate  * not already started (i_ddi_umem_unlock_thread).
85417c478bd9Sstevel@tonic-gate  */
85427c478bd9Sstevel@tonic-gate static void
85437c478bd9Sstevel@tonic-gate i_ddi_umem_unlock_thread_start(void)
85447c478bd9Sstevel@tonic-gate {
85457c478bd9Sstevel@tonic-gate 	mutex_enter(&ddi_umem_unlock_mutex);
85467c478bd9Sstevel@tonic-gate 	if (ddi_umem_unlock_thread == NULL) {
85477c478bd9Sstevel@tonic-gate 		ddi_umem_unlock_thread = thread_create(NULL, 0,
85487c478bd9Sstevel@tonic-gate 		    i_ddi_umem_unlock_thread, NULL, 0, &p0,
85497c478bd9Sstevel@tonic-gate 		    TS_RUN, minclsyspri);
85507c478bd9Sstevel@tonic-gate 	}
85517c478bd9Sstevel@tonic-gate 	mutex_exit(&ddi_umem_unlock_mutex);
85527c478bd9Sstevel@tonic-gate }
85537c478bd9Sstevel@tonic-gate 
85547c478bd9Sstevel@tonic-gate /*
85557c478bd9Sstevel@tonic-gate  * Lock the virtual address range in the current process and create a
85567c478bd9Sstevel@tonic-gate  * ddi_umem_cookie (of type UMEM_LOCKED). This can be used to pass to
85577c478bd9Sstevel@tonic-gate  * ddi_umem_iosetup to create a buf or do devmap_umem_setup/remap to export
85587c478bd9Sstevel@tonic-gate  * to user space.
85597c478bd9Sstevel@tonic-gate  *
85607c478bd9Sstevel@tonic-gate  * Note: The resource control accounting currently uses a full charge model
85617c478bd9Sstevel@tonic-gate  * in other words attempts to lock the same/overlapping areas of memory
85627c478bd9Sstevel@tonic-gate  * will deduct the full size of the buffer from the projects running
85637c478bd9Sstevel@tonic-gate  * counter for the device locked memory. This applies to umem_lockmemory too.
85647c478bd9Sstevel@tonic-gate  *
85657c478bd9Sstevel@tonic-gate  * addr, size should be PAGESIZE aligned
85667c478bd9Sstevel@tonic-gate  * flags - DDI_UMEMLOCK_READ, DDI_UMEMLOCK_WRITE or both
85677c478bd9Sstevel@tonic-gate  *	identifies whether the locked memory will be read or written or both
85687c478bd9Sstevel@tonic-gate  *
85697c478bd9Sstevel@tonic-gate  * Returns 0 on success
85707c478bd9Sstevel@tonic-gate  *	EINVAL - for invalid parameters
85717c478bd9Sstevel@tonic-gate  *	EPERM, ENOMEM and other error codes returned by as_pagelock
85727c478bd9Sstevel@tonic-gate  *	ENOMEM - is returned if the current request to lock memory exceeds
8573c6939658Ssl108498  *		*.max-locked-memory resource control value.
85747c478bd9Sstevel@tonic-gate  *	EAGAIN - could not start the ddi_umem_unlock list processing thread
85757c478bd9Sstevel@tonic-gate  */
85767c478bd9Sstevel@tonic-gate int
85777c478bd9Sstevel@tonic-gate ddi_umem_lock(caddr_t addr, size_t len, int flags, ddi_umem_cookie_t *cookie)
85787c478bd9Sstevel@tonic-gate {
85797c478bd9Sstevel@tonic-gate 	int	error;
85807c478bd9Sstevel@tonic-gate 	struct ddi_umem_cookie *p;
85817c478bd9Sstevel@tonic-gate 
85827c478bd9Sstevel@tonic-gate 	*cookie = NULL;		/* in case of any error return */
85837c478bd9Sstevel@tonic-gate 
85847c478bd9Sstevel@tonic-gate 	/* These are the only two valid flags */
85857c478bd9Sstevel@tonic-gate 	if ((flags & ~(DDI_UMEMLOCK_READ | DDI_UMEMLOCK_WRITE)) != 0) {
85867c478bd9Sstevel@tonic-gate 		return (EINVAL);
85877c478bd9Sstevel@tonic-gate 	}
85887c478bd9Sstevel@tonic-gate 
85897c478bd9Sstevel@tonic-gate 	/* At least one of the two flags (or both) must be set */
85907c478bd9Sstevel@tonic-gate 	if ((flags & (DDI_UMEMLOCK_READ | DDI_UMEMLOCK_WRITE)) == 0) {
85917c478bd9Sstevel@tonic-gate 		return (EINVAL);
85927c478bd9Sstevel@tonic-gate 	}
85937c478bd9Sstevel@tonic-gate 
85947c478bd9Sstevel@tonic-gate 	/* addr and len must be page-aligned */
85957c478bd9Sstevel@tonic-gate 	if (((uintptr_t)addr & PAGEOFFSET) != 0) {
85967c478bd9Sstevel@tonic-gate 		return (EINVAL);
85977c478bd9Sstevel@tonic-gate 	}
85987c478bd9Sstevel@tonic-gate 
85997c478bd9Sstevel@tonic-gate 	if ((len & PAGEOFFSET) != 0) {
86007c478bd9Sstevel@tonic-gate 		return (EINVAL);
86017c478bd9Sstevel@tonic-gate 	}
86027c478bd9Sstevel@tonic-gate 
86037c478bd9Sstevel@tonic-gate 	/*
86047c478bd9Sstevel@tonic-gate 	 * Call i_ddi_umem_unlock_thread_start if necessary.  It will
86057c478bd9Sstevel@tonic-gate 	 * be called on first ddi_umem_lock or umem_lockmemory call.
86067c478bd9Sstevel@tonic-gate 	 */
86077c478bd9Sstevel@tonic-gate 	if (ddi_umem_unlock_thread == NULL)
86087c478bd9Sstevel@tonic-gate 		i_ddi_umem_unlock_thread_start();
86097c478bd9Sstevel@tonic-gate 
86107c478bd9Sstevel@tonic-gate 	/* Allocate memory for the cookie */
86117c478bd9Sstevel@tonic-gate 	p = kmem_zalloc(sizeof (struct ddi_umem_cookie), KM_SLEEP);
86127c478bd9Sstevel@tonic-gate 
86137c478bd9Sstevel@tonic-gate 	/* Convert the flags to seg_rw type */
86147c478bd9Sstevel@tonic-gate 	if (flags & DDI_UMEMLOCK_WRITE) {
86157c478bd9Sstevel@tonic-gate 		p->s_flags = S_WRITE;
86167c478bd9Sstevel@tonic-gate 	} else {
86177c478bd9Sstevel@tonic-gate 		p->s_flags = S_READ;
86187c478bd9Sstevel@tonic-gate 	}
86197c478bd9Sstevel@tonic-gate 
86207c478bd9Sstevel@tonic-gate 	/* Store curproc in cookie for later iosetup/unlock */
86217c478bd9Sstevel@tonic-gate 	p->procp = (void *)curproc;
86227c478bd9Sstevel@tonic-gate 
86237c478bd9Sstevel@tonic-gate 	/*
86247c478bd9Sstevel@tonic-gate 	 * Store the struct as pointer in cookie for later use by
86257c478bd9Sstevel@tonic-gate 	 * ddi_umem_unlock.  The proc->p_as will be stale if ddi_umem_unlock
86267c478bd9Sstevel@tonic-gate 	 * is called after relvm is called.
86277c478bd9Sstevel@tonic-gate 	 */
86287c478bd9Sstevel@tonic-gate 	p->asp = curproc->p_as;
86297c478bd9Sstevel@tonic-gate 	/*
86307c478bd9Sstevel@tonic-gate 	 * The size field is needed for lockmem accounting.
86317c478bd9Sstevel@tonic-gate 	 */
86327c478bd9Sstevel@tonic-gate 	p->size = len;
8633c3ffbee3S 	init_lockedmem_rctl_flag(p);
86347c478bd9Sstevel@tonic-gate 
86357c478bd9Sstevel@tonic-gate 	if (umem_incr_devlockmem(p) != 0) {
86367c478bd9Sstevel@tonic-gate 		/*
86377c478bd9Sstevel@tonic-gate 		 * The requested memory cannot be locked
86387c478bd9Sstevel@tonic-gate 		 */
86397c478bd9Sstevel@tonic-gate 		kmem_free(p, sizeof (struct ddi_umem_cookie));
86407c478bd9Sstevel@tonic-gate 		*cookie = (ddi_umem_cookie_t)NULL;
86417c478bd9Sstevel@tonic-gate 		return (ENOMEM);
86427c478bd9Sstevel@tonic-gate 	}
86437c478bd9Sstevel@tonic-gate 
86447c478bd9Sstevel@tonic-gate 	/* Lock the pages corresponding to addr, len in memory */
86457c478bd9Sstevel@tonic-gate 	error = as_pagelock(((proc_t *)p->procp)->p_as, &(p->pparray),
86467c478bd9Sstevel@tonic-gate 	    addr, len, p->s_flags);
86477c478bd9Sstevel@tonic-gate 	if (error != 0) {
86487c478bd9Sstevel@tonic-gate 		umem_decr_devlockmem(p);
86497c478bd9Sstevel@tonic-gate 		kmem_free(p, sizeof (struct ddi_umem_cookie));
86507c478bd9Sstevel@tonic-gate 		*cookie = (ddi_umem_cookie_t)NULL;
86517c478bd9Sstevel@tonic-gate 		return (error);
86527c478bd9Sstevel@tonic-gate 	}
86537c478bd9Sstevel@tonic-gate 
86547c478bd9Sstevel@tonic-gate 	/* Initialize the fields in the ddi_umem_cookie */
86557c478bd9Sstevel@tonic-gate 	p->cvaddr = addr;
86567c478bd9Sstevel@tonic-gate 	p->type = UMEM_LOCKED;
86577c478bd9Sstevel@tonic-gate 	p->cook_refcnt = 1;
86587c478bd9Sstevel@tonic-gate 
86597c478bd9Sstevel@tonic-gate 	*cookie = (ddi_umem_cookie_t)p;
86607c478bd9Sstevel@tonic-gate 	return (error);
86617c478bd9Sstevel@tonic-gate }
86627c478bd9Sstevel@tonic-gate 
86637c478bd9Sstevel@tonic-gate /*
86647c478bd9Sstevel@tonic-gate  * Add the cookie to the ddi_umem_unlock list.  Pages will be
86657c478bd9Sstevel@tonic-gate  * unlocked by i_ddi_umem_unlock_thread.
86667c478bd9Sstevel@tonic-gate  */
86677c478bd9Sstevel@tonic-gate 
86687c478bd9Sstevel@tonic-gate void
86697c478bd9Sstevel@tonic-gate ddi_umem_unlock(ddi_umem_cookie_t cookie)
86707c478bd9Sstevel@tonic-gate {
86717c478bd9Sstevel@tonic-gate 	struct ddi_umem_cookie	*p = (struct ddi_umem_cookie *)cookie;
86727c478bd9Sstevel@tonic-gate 
86737c478bd9Sstevel@tonic-gate 	ASSERT(p->type == UMEM_LOCKED);
86747c478bd9Sstevel@tonic-gate 	ASSERT(CPU_ON_INTR(CPU) == 0); /* cannot be high level */
86757c478bd9Sstevel@tonic-gate 	ASSERT(ddi_umem_unlock_thread != NULL);
86767c478bd9Sstevel@tonic-gate 
86777c478bd9Sstevel@tonic-gate 	p->unl_forw = (struct ddi_umem_cookie *)NULL;	/* end of list */
86789bbaf432Seota 	/*
86799bbaf432Seota 	 * Queue the unlock request and notify i_ddi_umem_unlock thread
86809bbaf432Seota 	 * if it's called in the interrupt context. Otherwise, unlock pages
86819bbaf432Seota 	 * immediately.
86829bbaf432Seota 	 */
86839bbaf432Seota 	if (servicing_interrupt()) {
86849bbaf432Seota 		/* queue the unlock request and notify the thread */
86857c478bd9Sstevel@tonic-gate 		mutex_enter(&ddi_umem_unlock_mutex);
86867c478bd9Sstevel@tonic-gate 		if (ddi_umem_unlock_head == NULL) {
86877c478bd9Sstevel@tonic-gate 			ddi_umem_unlock_head = ddi_umem_unlock_tail = p;
86887c478bd9Sstevel@tonic-gate 			cv_broadcast(&ddi_umem_unlock_cv);
86897c478bd9Sstevel@tonic-gate 		} else {
86907c478bd9Sstevel@tonic-gate 			ddi_umem_unlock_tail->unl_forw = p;
86917c478bd9Sstevel@tonic-gate 			ddi_umem_unlock_tail = p;
86927c478bd9Sstevel@tonic-gate 		}
86937c478bd9Sstevel@tonic-gate 		mutex_exit(&ddi_umem_unlock_mutex);
86949bbaf432Seota 	} else {
86959bbaf432Seota 		/* unlock the pages right away */
86969bbaf432Seota 		(void) i_ddi_umem_unlock(p);
86979bbaf432Seota 	}
86987c478bd9Sstevel@tonic-gate }
86997c478bd9Sstevel@tonic-gate 
87007c478bd9Sstevel@tonic-gate /*
87017c478bd9Sstevel@tonic-gate  * Create a buf structure from a ddi_umem_cookie
87027c478bd9Sstevel@tonic-gate  * cookie - is a ddi_umem_cookie for from ddi_umem_lock and ddi_umem_alloc
87037c478bd9Sstevel@tonic-gate  *		(only UMEM_LOCKED & KMEM_NON_PAGEABLE types supported)
87047c478bd9Sstevel@tonic-gate  * off, len - identifies the portion of the memory represented by the cookie
87057c478bd9Sstevel@tonic-gate  *		that the buf points to.
87067c478bd9Sstevel@tonic-gate  *	NOTE: off, len need to follow the alignment/size restrictions of the
87077c478bd9Sstevel@tonic-gate  *		device (dev) that this buf will be passed to. Some devices
87087c478bd9Sstevel@tonic-gate  *		will accept unrestricted alignment/size, whereas others (such as
87097c478bd9Sstevel@tonic-gate  *		st) require some block-size alignment/size. It is the caller's
87107c478bd9Sstevel@tonic-gate  *		responsibility to ensure that the alignment/size restrictions
87117c478bd9Sstevel@tonic-gate  *		are met (we cannot assert as we do not know the restrictions)
87127c478bd9Sstevel@tonic-gate  *
87137c478bd9Sstevel@tonic-gate  * direction - is one of B_READ or B_WRITE and needs to be compatible with
87147c478bd9Sstevel@tonic-gate  *		the flags used in ddi_umem_lock
87157c478bd9Sstevel@tonic-gate  *
87167c478bd9Sstevel@tonic-gate  * The following three arguments are used to initialize fields in the
87177c478bd9Sstevel@tonic-gate  * buf structure and are uninterpreted by this routine.
87187c478bd9Sstevel@tonic-gate  *
87197c478bd9Sstevel@tonic-gate  * dev
87207c478bd9Sstevel@tonic-gate  * blkno
87217c478bd9Sstevel@tonic-gate  * iodone
87227c478bd9Sstevel@tonic-gate  *
87237c478bd9Sstevel@tonic-gate  * sleepflag - is one of DDI_UMEM_SLEEP or DDI_UMEM_NOSLEEP
87247c478bd9Sstevel@tonic-gate  *
87257c478bd9Sstevel@tonic-gate  * Returns a buf structure pointer on success (to be freed by freerbuf)
87267c478bd9Sstevel@tonic-gate  *	NULL on any parameter error or memory alloc failure
87277c478bd9Sstevel@tonic-gate  *
87287c478bd9Sstevel@tonic-gate  */
87297c478bd9Sstevel@tonic-gate struct buf *
87307c478bd9Sstevel@tonic-gate ddi_umem_iosetup(ddi_umem_cookie_t cookie, off_t off, size_t len,
87317c478bd9Sstevel@tonic-gate 	int direction, dev_t dev, daddr_t blkno,
87327c478bd9Sstevel@tonic-gate 	int (*iodone)(struct buf *), int sleepflag)
87337c478bd9Sstevel@tonic-gate {
87347c478bd9Sstevel@tonic-gate 	struct ddi_umem_cookie *p = (struct ddi_umem_cookie *)cookie;
87357c478bd9Sstevel@tonic-gate 	struct buf *bp;
87367c478bd9Sstevel@tonic-gate 
87377c478bd9Sstevel@tonic-gate 	/*
87387c478bd9Sstevel@tonic-gate 	 * check for valid cookie offset, len
87397c478bd9Sstevel@tonic-gate 	 */
87407c478bd9Sstevel@tonic-gate 	if ((off + len) > p->size) {
87417c478bd9Sstevel@tonic-gate 		return (NULL);
87427c478bd9Sstevel@tonic-gate 	}
87437c478bd9Sstevel@tonic-gate 
87447c478bd9Sstevel@tonic-gate 	if (len > p->size) {
87457c478bd9Sstevel@tonic-gate 		return (NULL);
87467c478bd9Sstevel@tonic-gate 	}
87477c478bd9Sstevel@tonic-gate 
87487c478bd9Sstevel@tonic-gate 	/* direction has to be one of B_READ or B_WRITE */
87497c478bd9Sstevel@tonic-gate 	if ((direction != B_READ) && (direction != B_WRITE)) {
87507c478bd9Sstevel@tonic-gate 		return (NULL);
87517c478bd9Sstevel@tonic-gate 	}
87527c478bd9Sstevel@tonic-gate 
87537c478bd9Sstevel@tonic-gate 	/* These are the only two valid sleepflags */
87547c478bd9Sstevel@tonic-gate 	if ((sleepflag != DDI_UMEM_SLEEP) && (sleepflag != DDI_UMEM_NOSLEEP)) {
87557c478bd9Sstevel@tonic-gate 		return (NULL);
87567c478bd9Sstevel@tonic-gate 	}
87577c478bd9Sstevel@tonic-gate 
87587c478bd9Sstevel@tonic-gate 	/*
87597c478bd9Sstevel@tonic-gate 	 * Only cookies of type UMEM_LOCKED and KMEM_NON_PAGEABLE are supported
87607c478bd9Sstevel@tonic-gate 	 */
87617c478bd9Sstevel@tonic-gate 	if ((p->type != UMEM_LOCKED) && (p->type != KMEM_NON_PAGEABLE)) {
87627c478bd9Sstevel@tonic-gate 		return (NULL);
87637c478bd9Sstevel@tonic-gate 	}
87647c478bd9Sstevel@tonic-gate 
87657c478bd9Sstevel@tonic-gate 	/* If type is KMEM_NON_PAGEABLE procp is NULL */
87667c478bd9Sstevel@tonic-gate 	ASSERT((p->type == KMEM_NON_PAGEABLE) ?
87677c478bd9Sstevel@tonic-gate 	    (p->procp == NULL) : (p->procp != NULL));
87687c478bd9Sstevel@tonic-gate 
87697c478bd9Sstevel@tonic-gate 	bp = kmem_alloc(sizeof (struct buf), sleepflag);
87707c478bd9Sstevel@tonic-gate 	if (bp == NULL) {
87717c478bd9Sstevel@tonic-gate 		return (NULL);
87727c478bd9Sstevel@tonic-gate 	}
87737c478bd9Sstevel@tonic-gate 	bioinit(bp);
87747c478bd9Sstevel@tonic-gate 
87757c478bd9Sstevel@tonic-gate 	bp->b_flags = B_BUSY | B_PHYS | direction;
87767c478bd9Sstevel@tonic-gate 	bp->b_edev = dev;
87777c478bd9Sstevel@tonic-gate 	bp->b_lblkno = blkno;
87787c478bd9Sstevel@tonic-gate 	bp->b_iodone = iodone;
87797c478bd9Sstevel@tonic-gate 	bp->b_bcount = len;
87807c478bd9Sstevel@tonic-gate 	bp->b_proc = (proc_t *)p->procp;
87817c478bd9Sstevel@tonic-gate 	ASSERT(((uintptr_t)(p->cvaddr) & PAGEOFFSET) == 0);
87827c478bd9Sstevel@tonic-gate 	bp->b_un.b_addr = (caddr_t)((uintptr_t)(p->cvaddr) + off);
87837c478bd9Sstevel@tonic-gate 	if (p->pparray != NULL) {
87847c478bd9Sstevel@tonic-gate 		bp->b_flags |= B_SHADOW;
87857c478bd9Sstevel@tonic-gate 		ASSERT(((uintptr_t)(p->cvaddr) & PAGEOFFSET) == 0);
87867c478bd9Sstevel@tonic-gate 		bp->b_shadow = p->pparray + btop(off);
87877c478bd9Sstevel@tonic-gate 	}
87887c478bd9Sstevel@tonic-gate 	return (bp);
87897c478bd9Sstevel@tonic-gate }
87907c478bd9Sstevel@tonic-gate 
87917c478bd9Sstevel@tonic-gate /*
87927c478bd9Sstevel@tonic-gate  * Fault-handling and related routines
87937c478bd9Sstevel@tonic-gate  */
87947c478bd9Sstevel@tonic-gate 
87957c478bd9Sstevel@tonic-gate ddi_devstate_t
87967c478bd9Sstevel@tonic-gate ddi_get_devstate(dev_info_t *dip)
87977c478bd9Sstevel@tonic-gate {
87987c478bd9Sstevel@tonic-gate 	if (DEVI_IS_DEVICE_OFFLINE(dip))
87997c478bd9Sstevel@tonic-gate 		return (DDI_DEVSTATE_OFFLINE);
88007c478bd9Sstevel@tonic-gate 	else if (DEVI_IS_DEVICE_DOWN(dip) || DEVI_IS_BUS_DOWN(dip))
88017c478bd9Sstevel@tonic-gate 		return (DDI_DEVSTATE_DOWN);
88027c478bd9Sstevel@tonic-gate 	else if (DEVI_IS_BUS_QUIESCED(dip))
88037c478bd9Sstevel@tonic-gate 		return (DDI_DEVSTATE_QUIESCED);
88047c478bd9Sstevel@tonic-gate 	else if (DEVI_IS_DEVICE_DEGRADED(dip))
88057c478bd9Sstevel@tonic-gate 		return (DDI_DEVSTATE_DEGRADED);
88067c478bd9Sstevel@tonic-gate 	else
88077c478bd9Sstevel@tonic-gate 		return (DDI_DEVSTATE_UP);
88087c478bd9Sstevel@tonic-gate }
88097c478bd9Sstevel@tonic-gate 
88107c478bd9Sstevel@tonic-gate void
88117c478bd9Sstevel@tonic-gate ddi_dev_report_fault(dev_info_t *dip, ddi_fault_impact_t impact,
88127c478bd9Sstevel@tonic-gate 	ddi_fault_location_t location, const char *message)
88137c478bd9Sstevel@tonic-gate {
88147c478bd9Sstevel@tonic-gate 	struct ddi_fault_event_data fd;
88157c478bd9Sstevel@tonic-gate 	ddi_eventcookie_t ec;
88167c478bd9Sstevel@tonic-gate 
88177c478bd9Sstevel@tonic-gate 	/*
88187c478bd9Sstevel@tonic-gate 	 * Assemble all the information into a fault-event-data structure
88197c478bd9Sstevel@tonic-gate 	 */
88207c478bd9Sstevel@tonic-gate 	fd.f_dip = dip;
88217c478bd9Sstevel@tonic-gate 	fd.f_impact = impact;
88227c478bd9Sstevel@tonic-gate 	fd.f_location = location;
88237c478bd9Sstevel@tonic-gate 	fd.f_message = message;
88247c478bd9Sstevel@tonic-gate 	fd.f_oldstate = ddi_get_devstate(dip);
88257c478bd9Sstevel@tonic-gate 
88267c478bd9Sstevel@tonic-gate 	/*
88277c478bd9Sstevel@tonic-gate 	 * Get eventcookie from defining parent.
88287c478bd9Sstevel@tonic-gate 	 */
88297c478bd9Sstevel@tonic-gate 	if (ddi_get_eventcookie(dip, DDI_DEVI_FAULT_EVENT, &ec) !=
88307c478bd9Sstevel@tonic-gate 	    DDI_SUCCESS)
88317c478bd9Sstevel@tonic-gate 		return;
88327c478bd9Sstevel@tonic-gate 
88337c478bd9Sstevel@tonic-gate 	(void) ndi_post_event(dip, dip, ec, &fd);
88347c478bd9Sstevel@tonic-gate }
88357c478bd9Sstevel@tonic-gate 
88367c478bd9Sstevel@tonic-gate char *
88377c478bd9Sstevel@tonic-gate i_ddi_devi_class(dev_info_t *dip)
88387c478bd9Sstevel@tonic-gate {
88397c478bd9Sstevel@tonic-gate 	return (DEVI(dip)->devi_device_class);
88407c478bd9Sstevel@tonic-gate }
88417c478bd9Sstevel@tonic-gate 
88427c478bd9Sstevel@tonic-gate int
88437c478bd9Sstevel@tonic-gate i_ddi_set_devi_class(dev_info_t *dip, char *devi_class, int flag)
88447c478bd9Sstevel@tonic-gate {
88457c478bd9Sstevel@tonic-gate 	struct dev_info *devi = DEVI(dip);
88467c478bd9Sstevel@tonic-gate 
88477c478bd9Sstevel@tonic-gate 	mutex_enter(&devi->devi_lock);
88487c478bd9Sstevel@tonic-gate 
88497c478bd9Sstevel@tonic-gate 	if (devi->devi_device_class)
88507c478bd9Sstevel@tonic-gate 		kmem_free(devi->devi_device_class,
88517c478bd9Sstevel@tonic-gate 		    strlen(devi->devi_device_class) + 1);
88527c478bd9Sstevel@tonic-gate 
88537c478bd9Sstevel@tonic-gate 	if ((devi->devi_device_class = i_ddi_strdup(devi_class, flag))
88547c478bd9Sstevel@tonic-gate 	    != NULL) {
88557c478bd9Sstevel@tonic-gate 		mutex_exit(&devi->devi_lock);
88567c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
88577c478bd9Sstevel@tonic-gate 	}
88587c478bd9Sstevel@tonic-gate 
88597c478bd9Sstevel@tonic-gate 	mutex_exit(&devi->devi_lock);
88607c478bd9Sstevel@tonic-gate 
88617c478bd9Sstevel@tonic-gate 	return (DDI_FAILURE);
88627c478bd9Sstevel@tonic-gate }
88637c478bd9Sstevel@tonic-gate 
88647c478bd9Sstevel@tonic-gate 
88657c478bd9Sstevel@tonic-gate /*
88667c478bd9Sstevel@tonic-gate  * Task Queues DDI interfaces.
88677c478bd9Sstevel@tonic-gate  */
88687c478bd9Sstevel@tonic-gate 
88697c478bd9Sstevel@tonic-gate /* ARGSUSED */
88707c478bd9Sstevel@tonic-gate ddi_taskq_t *
88717c478bd9Sstevel@tonic-gate ddi_taskq_create(dev_info_t *dip, const char *name, int nthreads,
88727c478bd9Sstevel@tonic-gate     pri_t pri, uint_t cflags)
88737c478bd9Sstevel@tonic-gate {
88747c478bd9Sstevel@tonic-gate 	char full_name[TASKQ_NAMELEN];
88757c478bd9Sstevel@tonic-gate 	const char *tq_name;
88767c478bd9Sstevel@tonic-gate 	int nodeid = 0;
88777c478bd9Sstevel@tonic-gate 
88787c478bd9Sstevel@tonic-gate 	if (dip == NULL)
88797c478bd9Sstevel@tonic-gate 		tq_name = name;
88807c478bd9Sstevel@tonic-gate 	else {
88817c478bd9Sstevel@tonic-gate 		nodeid = ddi_get_instance(dip);
88827c478bd9Sstevel@tonic-gate 
88837c478bd9Sstevel@tonic-gate 		if (name == NULL)
88847c478bd9Sstevel@tonic-gate 			name = "tq";
88857c478bd9Sstevel@tonic-gate 
88867c478bd9Sstevel@tonic-gate 		(void) snprintf(full_name, sizeof (full_name), "%s_%s",
88877c478bd9Sstevel@tonic-gate 		    ddi_driver_name(dip), name);
88887c478bd9Sstevel@tonic-gate 
88897c478bd9Sstevel@tonic-gate 		tq_name = full_name;
88907c478bd9Sstevel@tonic-gate 	}
88917c478bd9Sstevel@tonic-gate 
88927c478bd9Sstevel@tonic-gate 	return ((ddi_taskq_t *)taskq_create_instance(tq_name, nodeid, nthreads,
88937c478bd9Sstevel@tonic-gate 	    pri == TASKQ_DEFAULTPRI ? minclsyspri : pri,
88947c478bd9Sstevel@tonic-gate 	    nthreads, INT_MAX, TASKQ_PREPOPULATE));
88957c478bd9Sstevel@tonic-gate }
88967c478bd9Sstevel@tonic-gate 
88977c478bd9Sstevel@tonic-gate void
88987c478bd9Sstevel@tonic-gate ddi_taskq_destroy(ddi_taskq_t *tq)
88997c478bd9Sstevel@tonic-gate {
89007c478bd9Sstevel@tonic-gate 	taskq_destroy((taskq_t *)tq);
89017c478bd9Sstevel@tonic-gate }
89027c478bd9Sstevel@tonic-gate 
89037c478bd9Sstevel@tonic-gate int
89047c478bd9Sstevel@tonic-gate ddi_taskq_dispatch(ddi_taskq_t *tq, void (* func)(void *),
89057c478bd9Sstevel@tonic-gate     void *arg, uint_t dflags)
89067c478bd9Sstevel@tonic-gate {
89077c478bd9Sstevel@tonic-gate 	taskqid_t id = taskq_dispatch((taskq_t *)tq, func, arg,
89087c478bd9Sstevel@tonic-gate 	    dflags == DDI_SLEEP ? TQ_SLEEP : TQ_NOSLEEP);
89097c478bd9Sstevel@tonic-gate 
89107c478bd9Sstevel@tonic-gate 	return (id != 0 ? DDI_SUCCESS : DDI_FAILURE);
89117c478bd9Sstevel@tonic-gate }
89127c478bd9Sstevel@tonic-gate 
89137c478bd9Sstevel@tonic-gate void
89147c478bd9Sstevel@tonic-gate ddi_taskq_wait(ddi_taskq_t *tq)
89157c478bd9Sstevel@tonic-gate {
89167c478bd9Sstevel@tonic-gate 	taskq_wait((taskq_t *)tq);
89177c478bd9Sstevel@tonic-gate }
89187c478bd9Sstevel@tonic-gate 
89197c478bd9Sstevel@tonic-gate void
89207c478bd9Sstevel@tonic-gate ddi_taskq_suspend(ddi_taskq_t *tq)
89217c478bd9Sstevel@tonic-gate {
89227c478bd9Sstevel@tonic-gate 	taskq_suspend((taskq_t *)tq);
89237c478bd9Sstevel@tonic-gate }
89247c478bd9Sstevel@tonic-gate 
89257c478bd9Sstevel@tonic-gate boolean_t
89267c478bd9Sstevel@tonic-gate ddi_taskq_suspended(ddi_taskq_t *tq)
89277c478bd9Sstevel@tonic-gate {
89287c478bd9Sstevel@tonic-gate 	return (taskq_suspended((taskq_t *)tq));
89297c478bd9Sstevel@tonic-gate }
89307c478bd9Sstevel@tonic-gate 
89317c478bd9Sstevel@tonic-gate void
89327c478bd9Sstevel@tonic-gate ddi_taskq_resume(ddi_taskq_t *tq)
89337c478bd9Sstevel@tonic-gate {
89347c478bd9Sstevel@tonic-gate 	taskq_resume((taskq_t *)tq);
89357c478bd9Sstevel@tonic-gate }
89367c478bd9Sstevel@tonic-gate 
89377c478bd9Sstevel@tonic-gate int
8938*ccdeb6b6SDan McDonald ddi_parse(const char *ifname, char *alnum, uint_t *nump)
8939*ccdeb6b6SDan McDonald {
8940*ccdeb6b6SDan McDonald 	/*
8941*ccdeb6b6SDan McDonald 	 * Cap "alnum" size at LIFNAMSIZ, as callers use that in most/all
8942*ccdeb6b6SDan McDonald 	 * cases.
8943*ccdeb6b6SDan McDonald 	 */
8944*ccdeb6b6SDan McDonald 	return (ddi_parse_dlen(ifname, alnum, LIFNAMSIZ, nump));
8945*ccdeb6b6SDan McDonald }
8946*ccdeb6b6SDan McDonald 
8947*ccdeb6b6SDan McDonald int
8948*ccdeb6b6SDan McDonald ddi_parse_dlen(const char *ifname, char *alnum, size_t alnumsize, uint_t *nump)
89497c478bd9Sstevel@tonic-gate {
89507c478bd9Sstevel@tonic-gate 	const char	*p;
8951*ccdeb6b6SDan McDonald 	int		copy_len;
89527c478bd9Sstevel@tonic-gate 	ulong_t		num;
89537c478bd9Sstevel@tonic-gate 	boolean_t	nonum = B_TRUE;
89547c478bd9Sstevel@tonic-gate 	char		c;
89557c478bd9Sstevel@tonic-gate 
8956*ccdeb6b6SDan McDonald 	copy_len = strlen(ifname);
8957*ccdeb6b6SDan McDonald 	for (p = ifname + copy_len; p != ifname; copy_len--) {
89587c478bd9Sstevel@tonic-gate 		c = *--p;
89597c478bd9Sstevel@tonic-gate 		if (!isdigit(c)) {
8960*ccdeb6b6SDan McDonald 			/*
8961*ccdeb6b6SDan McDonald 			 * At this point, copy_len is the length of ifname
8962*ccdeb6b6SDan McDonald 			 * WITHOUT the PPA number. For "e1000g10" copy_len is 6.
8963*ccdeb6b6SDan McDonald 			 *
8964*ccdeb6b6SDan McDonald 			 * We must first make sure we HAVE a PPA, and we
8965*ccdeb6b6SDan McDonald 			 * aren't exceeding alnumsize with copy_len and a '\0'
8966*ccdeb6b6SDan McDonald 			 * terminator...
8967*ccdeb6b6SDan McDonald 			 */
8968*ccdeb6b6SDan McDonald 			int copy_len_nul = copy_len + 1;
8969*ccdeb6b6SDan McDonald 
8970*ccdeb6b6SDan McDonald 			if (nonum || alnumsize < copy_len_nul)
8971*ccdeb6b6SDan McDonald 				return (DDI_FAILURE);
8972*ccdeb6b6SDan McDonald 
8973*ccdeb6b6SDan McDonald 			/*
8974*ccdeb6b6SDan McDonald 			 * ... then we abuse strlcpy() to copy over the
8975*ccdeb6b6SDan McDonald 			 * driver name portion AND '\0'-terminate it.
8976*ccdeb6b6SDan McDonald 			 */
8977*ccdeb6b6SDan McDonald 			(void) strlcpy(alnum, ifname, copy_len_nul);
89787c478bd9Sstevel@tonic-gate 			if (ddi_strtoul(p + 1, NULL, 10, &num) != 0)
89797c478bd9Sstevel@tonic-gate 				return (DDI_FAILURE);
89807c478bd9Sstevel@tonic-gate 			break;
89817c478bd9Sstevel@tonic-gate 		}
89827c478bd9Sstevel@tonic-gate 		nonum = B_FALSE;
89837c478bd9Sstevel@tonic-gate 	}
8984*ccdeb6b6SDan McDonald 
8985*ccdeb6b6SDan McDonald 	if (copy_len == 0)
89867c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
89877c478bd9Sstevel@tonic-gate 
89887c478bd9Sstevel@tonic-gate 	*nump = num;
89897c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
89907c478bd9Sstevel@tonic-gate }
899119397407SSherry Moore 
899219397407SSherry Moore /*
899319397407SSherry Moore  * Default initialization function for drivers that don't need to quiesce.
899419397407SSherry Moore  */
899519397407SSherry Moore /* ARGSUSED */
899619397407SSherry Moore int
899719397407SSherry Moore ddi_quiesce_not_needed(dev_info_t *dip)
899819397407SSherry Moore {
899919397407SSherry Moore 	return (DDI_SUCCESS);
900019397407SSherry Moore }
900119397407SSherry Moore 
900219397407SSherry Moore /*
900319397407SSherry Moore  * Initialization function for drivers that should implement quiesce()
900419397407SSherry Moore  * but haven't yet.
900519397407SSherry Moore  */
900619397407SSherry Moore /* ARGSUSED */
900719397407SSherry Moore int
900819397407SSherry Moore ddi_quiesce_not_supported(dev_info_t *dip)
900919397407SSherry Moore {
901019397407SSherry Moore 	return (DDI_FAILURE);
901119397407SSherry Moore }
90125febcb4aSScott Carter, SD IOSW 
90137f0b8309SEdward Pilatowicz char *
90147f0b8309SEdward Pilatowicz ddi_strdup(const char *str, int flag)
90157f0b8309SEdward Pilatowicz {
90167f0b8309SEdward Pilatowicz 	int	n;
90177f0b8309SEdward Pilatowicz 	char	*ptr;
90187f0b8309SEdward Pilatowicz 
90197f0b8309SEdward Pilatowicz 	ASSERT(str != NULL);
90207f0b8309SEdward Pilatowicz 	ASSERT((flag == KM_SLEEP) || (flag == KM_NOSLEEP));
90217f0b8309SEdward Pilatowicz 
90227f0b8309SEdward Pilatowicz 	n = strlen(str);
90237f0b8309SEdward Pilatowicz 	if ((ptr = kmem_alloc(n + 1, flag)) == NULL)
90247f0b8309SEdward Pilatowicz 		return (NULL);
90257f0b8309SEdward Pilatowicz 	bcopy(str, ptr, n + 1);
90267f0b8309SEdward Pilatowicz 	return (ptr);
90277f0b8309SEdward Pilatowicz }
90287f0b8309SEdward Pilatowicz 
90297f0b8309SEdward Pilatowicz char *
90307f0b8309SEdward Pilatowicz strdup(const char *str)
90317f0b8309SEdward Pilatowicz {
90327f0b8309SEdward Pilatowicz 	return (ddi_strdup(str, KM_SLEEP));
90337f0b8309SEdward Pilatowicz }
90347f0b8309SEdward Pilatowicz 
90357f0b8309SEdward Pilatowicz void
90367f0b8309SEdward Pilatowicz strfree(char *str)
90377f0b8309SEdward Pilatowicz {
90387f0b8309SEdward Pilatowicz 	ASSERT(str != NULL);
90397f0b8309SEdward Pilatowicz 	kmem_free(str, strlen(str) + 1);
90407f0b8309SEdward Pilatowicz }
90417f0b8309SEdward Pilatowicz 
90425febcb4aSScott Carter, SD IOSW /*
90435febcb4aSScott Carter, SD IOSW  * Generic DDI callback interfaces.
90445febcb4aSScott Carter, SD IOSW  */
90455febcb4aSScott Carter, SD IOSW 
90465febcb4aSScott Carter, SD IOSW int
90475febcb4aSScott Carter, SD IOSW ddi_cb_register(dev_info_t *dip, ddi_cb_flags_t flags, ddi_cb_func_t cbfunc,
90485febcb4aSScott Carter, SD IOSW     void *arg1, void *arg2, ddi_cb_handle_t *ret_hdlp)
90495febcb4aSScott Carter, SD IOSW {
90505febcb4aSScott Carter, SD IOSW 	ddi_cb_t	*cbp;
90515febcb4aSScott Carter, SD IOSW 
90525febcb4aSScott Carter, SD IOSW 	ASSERT(dip != NULL);
90535febcb4aSScott Carter, SD IOSW 	ASSERT(DDI_CB_FLAG_VALID(flags));
90545febcb4aSScott Carter, SD IOSW 	ASSERT(cbfunc != NULL);
90555febcb4aSScott Carter, SD IOSW 	ASSERT(ret_hdlp != NULL);
90565febcb4aSScott Carter, SD IOSW 
90575febcb4aSScott Carter, SD IOSW 	/* Sanity check the context */
90585febcb4aSScott Carter, SD IOSW 	ASSERT(!servicing_interrupt());
90595febcb4aSScott Carter, SD IOSW 	if (servicing_interrupt())
90605febcb4aSScott Carter, SD IOSW 		return (DDI_FAILURE);
90615febcb4aSScott Carter, SD IOSW 
90625febcb4aSScott Carter, SD IOSW 	/* Validate parameters */
90635febcb4aSScott Carter, SD IOSW 	if ((dip == NULL) || !DDI_CB_FLAG_VALID(flags) ||
90645febcb4aSScott Carter, SD IOSW 	    (cbfunc == NULL) || (ret_hdlp == NULL))
90655febcb4aSScott Carter, SD IOSW 		return (DDI_EINVAL);
90665febcb4aSScott Carter, SD IOSW 
90675febcb4aSScott Carter, SD IOSW 	/* Check for previous registration */
90685febcb4aSScott Carter, SD IOSW 	if (DEVI(dip)->devi_cb_p != NULL)
90695febcb4aSScott Carter, SD IOSW 		return (DDI_EALREADY);
90705febcb4aSScott Carter, SD IOSW 
90715febcb4aSScott Carter, SD IOSW 	/* Allocate and initialize callback */
90725febcb4aSScott Carter, SD IOSW 	cbp = kmem_zalloc(sizeof (ddi_cb_t), KM_SLEEP);
90735febcb4aSScott Carter, SD IOSW 	cbp->cb_dip = dip;
90745febcb4aSScott Carter, SD IOSW 	cbp->cb_func = cbfunc;
90755febcb4aSScott Carter, SD IOSW 	cbp->cb_arg1 = arg1;
90765febcb4aSScott Carter, SD IOSW 	cbp->cb_arg2 = arg2;
90775febcb4aSScott Carter, SD IOSW 	cbp->cb_flags = flags;
90785febcb4aSScott Carter, SD IOSW 	DEVI(dip)->devi_cb_p = cbp;
90795febcb4aSScott Carter, SD IOSW 
90805febcb4aSScott Carter, SD IOSW 	/* If adding an IRM callback, notify IRM */
90815febcb4aSScott Carter, SD IOSW 	if (flags & DDI_CB_FLAG_INTR)
90825febcb4aSScott Carter, SD IOSW 		i_ddi_irm_set_cb(dip, B_TRUE);
90835febcb4aSScott Carter, SD IOSW 
90845febcb4aSScott Carter, SD IOSW 	*ret_hdlp = (ddi_cb_handle_t)&(DEVI(dip)->devi_cb_p);
90855febcb4aSScott Carter, SD IOSW 	return (DDI_SUCCESS);
90865febcb4aSScott Carter, SD IOSW }
90875febcb4aSScott Carter, SD IOSW 
90885febcb4aSScott Carter, SD IOSW int
90895febcb4aSScott Carter, SD IOSW ddi_cb_unregister(ddi_cb_handle_t hdl)
90905febcb4aSScott Carter, SD IOSW {
90915febcb4aSScott Carter, SD IOSW 	ddi_cb_t	*cbp;
90925febcb4aSScott Carter, SD IOSW 	dev_info_t	*dip;
90935febcb4aSScott Carter, SD IOSW 
90945febcb4aSScott Carter, SD IOSW 	ASSERT(hdl != NULL);
90955febcb4aSScott Carter, SD IOSW 
90965febcb4aSScott Carter, SD IOSW 	/* Sanity check the context */
90975febcb4aSScott Carter, SD IOSW 	ASSERT(!servicing_interrupt());
90985febcb4aSScott Carter, SD IOSW 	if (servicing_interrupt())
90995febcb4aSScott Carter, SD IOSW 		return (DDI_FAILURE);
91005febcb4aSScott Carter, SD IOSW 
91015febcb4aSScott Carter, SD IOSW 	/* Validate parameters */
91025febcb4aSScott Carter, SD IOSW 	if ((hdl == NULL) || ((cbp = *(ddi_cb_t **)hdl) == NULL) ||
91035febcb4aSScott Carter, SD IOSW 	    ((dip = cbp->cb_dip) == NULL))
91045febcb4aSScott Carter, SD IOSW 		return (DDI_EINVAL);
91055febcb4aSScott Carter, SD IOSW 
91065febcb4aSScott Carter, SD IOSW 	/* If removing an IRM callback, notify IRM */
91075febcb4aSScott Carter, SD IOSW 	if (cbp->cb_flags & DDI_CB_FLAG_INTR)
91085febcb4aSScott Carter, SD IOSW 		i_ddi_irm_set_cb(dip, B_FALSE);
91095febcb4aSScott Carter, SD IOSW 
91105febcb4aSScott Carter, SD IOSW 	/* Destroy the callback */
91115febcb4aSScott Carter, SD IOSW 	kmem_free(cbp, sizeof (ddi_cb_t));
91125febcb4aSScott Carter, SD IOSW 	DEVI(dip)->devi_cb_p = NULL;
91135febcb4aSScott Carter, SD IOSW 
91145febcb4aSScott Carter, SD IOSW 	return (DDI_SUCCESS);
91155febcb4aSScott Carter, SD IOSW }
9116a3114836SGerry Liu 
9117a3114836SGerry Liu /*
9118a3114836SGerry Liu  * Platform independent DR routines
9119a3114836SGerry Liu  */
9120a3114836SGerry Liu 
9121a3114836SGerry Liu static int
9122a3114836SGerry Liu ndi2errno(int n)
9123a3114836SGerry Liu {
9124a3114836SGerry Liu 	int err = 0;
9125a3114836SGerry Liu 
9126a3114836SGerry Liu 	switch (n) {
9127a3114836SGerry Liu 		case NDI_NOMEM:
9128a3114836SGerry Liu 			err = ENOMEM;
9129a3114836SGerry Liu 			break;
9130a3114836SGerry Liu 		case NDI_BUSY:
9131a3114836SGerry Liu 			err = EBUSY;
9132a3114836SGerry Liu 			break;
9133a3114836SGerry Liu 		case NDI_FAULT:
9134a3114836SGerry Liu 			err = EFAULT;
9135a3114836SGerry Liu 			break;
9136a3114836SGerry Liu 		case NDI_FAILURE:
9137a3114836SGerry Liu 			err = EIO;
9138a3114836SGerry Liu 			break;
9139a3114836SGerry Liu 		case NDI_SUCCESS:
9140a3114836SGerry Liu 			break;
9141a3114836SGerry Liu 		case NDI_BADHANDLE:
9142a3114836SGerry Liu 		default:
9143a3114836SGerry Liu 			err = EINVAL;
9144a3114836SGerry Liu 			break;
9145a3114836SGerry Liu 	}
9146a3114836SGerry Liu 	return (err);
9147a3114836SGerry Liu }
9148a3114836SGerry Liu 
9149a3114836SGerry Liu /*
9150a3114836SGerry Liu  * Prom tree node list
9151a3114836SGerry Liu  */
9152a3114836SGerry Liu struct ptnode {
9153a3114836SGerry Liu 	pnode_t		nodeid;
9154a3114836SGerry Liu 	struct ptnode	*next;
9155a3114836SGerry Liu };
9156a3114836SGerry Liu 
9157a3114836SGerry Liu /*
9158a3114836SGerry Liu  * Prom tree walk arg
9159a3114836SGerry Liu  */
9160a3114836SGerry Liu struct pta {
9161a3114836SGerry Liu 	dev_info_t	*pdip;
9162a3114836SGerry Liu 	devi_branch_t	*bp;
9163a3114836SGerry Liu 	uint_t		flags;
9164a3114836SGerry Liu 	dev_info_t	*fdip;
9165a3114836SGerry Liu 	struct ptnode	*head;
9166a3114836SGerry Liu };
9167a3114836SGerry Liu 
9168a3114836SGerry Liu static void
9169a3114836SGerry Liu visit_node(pnode_t nodeid, struct pta *ap)
9170a3114836SGerry Liu {
9171a3114836SGerry Liu 	struct ptnode	**nextp;
9172a3114836SGerry Liu 	int		(*select)(pnode_t, void *, uint_t);
9173a3114836SGerry Liu 
9174a3114836SGerry Liu 	ASSERT(nodeid != OBP_NONODE && nodeid != OBP_BADNODE);
9175a3114836SGerry Liu 
9176a3114836SGerry Liu 	select = ap->bp->create.prom_branch_select;
9177a3114836SGerry Liu 
9178a3114836SGerry Liu 	ASSERT(select);
9179a3114836SGerry Liu 
9180a3114836SGerry Liu 	if (select(nodeid, ap->bp->arg, 0) == DDI_SUCCESS) {
9181a3114836SGerry Liu 
9182a3114836SGerry Liu 		for (nextp = &ap->head; *nextp; nextp = &(*nextp)->next)
9183a3114836SGerry Liu 			;
9184a3114836SGerry Liu 
9185a3114836SGerry Liu 		*nextp = kmem_zalloc(sizeof (struct ptnode), KM_SLEEP);
9186a3114836SGerry Liu 
9187a3114836SGerry Liu 		(*nextp)->nodeid = nodeid;
9188a3114836SGerry Liu 	}
9189a3114836SGerry Liu 
9190a3114836SGerry Liu 	if ((ap->flags & DEVI_BRANCH_CHILD) == DEVI_BRANCH_CHILD)
9191a3114836SGerry Liu 		return;
9192a3114836SGerry Liu 
9193a3114836SGerry Liu 	nodeid = prom_childnode(nodeid);
9194a3114836SGerry Liu 	while (nodeid != OBP_NONODE && nodeid != OBP_BADNODE) {
9195a3114836SGerry Liu 		visit_node(nodeid, ap);
9196a3114836SGerry Liu 		nodeid = prom_nextnode(nodeid);
9197a3114836SGerry Liu 	}
9198a3114836SGerry Liu }
9199a3114836SGerry Liu 
9200a3114836SGerry Liu /*
9201a3114836SGerry Liu  * NOTE: The caller of this function must check for device contracts
9202a3114836SGerry Liu  * or LDI callbacks against this dip before setting the dip offline.
9203a3114836SGerry Liu  */
9204a3114836SGerry Liu static int
9205a3114836SGerry Liu set_infant_dip_offline(dev_info_t *dip, void *arg)
9206a3114836SGerry Liu {
9207a3114836SGerry Liu 	char	*path = (char *)arg;
9208a3114836SGerry Liu 
9209a3114836SGerry Liu 	ASSERT(dip);
9210a3114836SGerry Liu 	ASSERT(arg);
9211a3114836SGerry Liu 
9212a3114836SGerry Liu 	if (i_ddi_node_state(dip) >= DS_ATTACHED) {
9213a3114836SGerry Liu 		(void) ddi_pathname(dip, path);
9214a3114836SGerry Liu 		cmn_err(CE_WARN, "Attempt to set offline flag on attached "
9215a3114836SGerry Liu 		    "node: %s", path);
9216a3114836SGerry Liu 		return (DDI_FAILURE);
9217a3114836SGerry Liu 	}
9218a3114836SGerry Liu 
9219a3114836SGerry Liu 	mutex_enter(&(DEVI(dip)->devi_lock));
9220a3114836SGerry Liu 	if (!DEVI_IS_DEVICE_OFFLINE(dip))
9221a3114836SGerry Liu 		DEVI_SET_DEVICE_OFFLINE(dip);
9222a3114836SGerry Liu 	mutex_exit(&(DEVI(dip)->devi_lock));
9223a3114836SGerry Liu 
9224a3114836SGerry Liu 	return (DDI_SUCCESS);
9225a3114836SGerry Liu }
9226a3114836SGerry Liu 
9227a3114836SGerry Liu typedef struct result {
9228a3114836SGerry Liu 	char	*path;
9229a3114836SGerry Liu 	int	result;
9230a3114836SGerry Liu } result_t;
9231a3114836SGerry Liu 
9232a3114836SGerry Liu static int
9233a3114836SGerry Liu dip_set_offline(dev_info_t *dip, void *arg)
9234a3114836SGerry Liu {
9235a3114836SGerry Liu 	int end;
9236a3114836SGerry Liu 	result_t *resp = (result_t *)arg;
9237a3114836SGerry Liu 
9238a3114836SGerry Liu 	ASSERT(dip);
9239a3114836SGerry Liu 	ASSERT(resp);
9240a3114836SGerry Liu 
9241a3114836SGerry Liu 	/*
9242a3114836SGerry Liu 	 * We stop the walk if e_ddi_offline_notify() returns
9243a3114836SGerry Liu 	 * failure, because this implies that one or more consumers
9244a3114836SGerry Liu 	 * (either LDI or contract based) has blocked the offline.
9245a3114836SGerry Liu 	 * So there is no point in conitnuing the walk
9246a3114836SGerry Liu 	 */
9247a3114836SGerry Liu 	if (e_ddi_offline_notify(dip) == DDI_FAILURE) {
9248a3114836SGerry Liu 		resp->result = DDI_FAILURE;
9249a3114836SGerry Liu 		return (DDI_WALK_TERMINATE);
9250a3114836SGerry Liu 	}
9251a3114836SGerry Liu 
9252a3114836SGerry Liu 	/*
9253a3114836SGerry Liu 	 * If set_infant_dip_offline() returns failure, it implies
9254a3114836SGerry Liu 	 * that we failed to set a particular dip offline. This
9255a3114836SGerry Liu 	 * does not imply that the offline as a whole should fail.
9256a3114836SGerry Liu 	 * We want to do the best we can, so we continue the walk.
9257a3114836SGerry Liu 	 */
9258a3114836SGerry Liu 	if (set_infant_dip_offline(dip, resp->path) == DDI_SUCCESS)
9259a3114836SGerry Liu 		end = DDI_SUCCESS;
9260a3114836SGerry Liu 	else
9261a3114836SGerry Liu 		end = DDI_FAILURE;
9262a3114836SGerry Liu 
9263a3114836SGerry Liu 	e_ddi_offline_finalize(dip, end);
9264a3114836SGerry Liu 
9265a3114836SGerry Liu 	return (DDI_WALK_CONTINUE);
9266a3114836SGerry Liu }
9267a3114836SGerry Liu 
9268a3114836SGerry Liu /*
9269a3114836SGerry Liu  * The call to e_ddi_offline_notify() exists for the
9270a3114836SGerry Liu  * unlikely error case that a branch we are trying to
9271a3114836SGerry Liu  * create already exists and has device contracts or LDI
9272a3114836SGerry Liu  * event callbacks against it.
9273a3114836SGerry Liu  *
9274a3114836SGerry Liu  * We allow create to succeed for such branches only if
9275a3114836SGerry Liu  * no constraints block the offline.
9276a3114836SGerry Liu  */
9277a3114836SGerry Liu static int
9278a3114836SGerry Liu branch_set_offline(dev_info_t *dip, char *path)
9279a3114836SGerry Liu {
9280a3114836SGerry Liu 	int		circ;
9281a3114836SGerry Liu 	int		end;
9282a3114836SGerry Liu 	result_t	res;
9283a3114836SGerry Liu 
9284a3114836SGerry Liu 
9285a3114836SGerry Liu 	if (e_ddi_offline_notify(dip) == DDI_FAILURE) {
9286a3114836SGerry Liu 		return (DDI_FAILURE);
9287a3114836SGerry Liu 	}
9288a3114836SGerry Liu 
9289a3114836SGerry Liu 	if (set_infant_dip_offline(dip, path) == DDI_SUCCESS)
9290a3114836SGerry Liu 		end = DDI_SUCCESS;
9291a3114836SGerry Liu 	else
9292a3114836SGerry Liu 		end = DDI_FAILURE;
9293a3114836SGerry Liu 
9294a3114836SGerry Liu 	e_ddi_offline_finalize(dip, end);
9295a3114836SGerry Liu 
9296a3114836SGerry Liu 	if (end == DDI_FAILURE)
9297a3114836SGerry Liu 		return (DDI_FAILURE);
9298a3114836SGerry Liu 
9299a3114836SGerry Liu 	res.result = DDI_SUCCESS;
9300a3114836SGerry Liu 	res.path = path;
9301a3114836SGerry Liu 
9302a3114836SGerry Liu 	ndi_devi_enter(dip, &circ);
9303a3114836SGerry Liu 	ddi_walk_devs(ddi_get_child(dip), dip_set_offline, &res);
9304a3114836SGerry Liu 	ndi_devi_exit(dip, circ);
9305a3114836SGerry Liu 
9306a3114836SGerry Liu 	return (res.result);
9307a3114836SGerry Liu }
9308a3114836SGerry Liu 
9309a3114836SGerry Liu /*ARGSUSED*/
9310a3114836SGerry Liu static int
9311a3114836SGerry Liu create_prom_branch(void *arg, int has_changed)
9312a3114836SGerry Liu {
9313a3114836SGerry Liu 	int		circ;
9314a3114836SGerry Liu 	int		exists, rv;
9315a3114836SGerry Liu 	pnode_t		nodeid;
9316a3114836SGerry Liu 	struct ptnode	*tnp;
9317a3114836SGerry Liu 	dev_info_t	*dip;
9318a3114836SGerry Liu 	struct pta	*ap = arg;
9319a3114836SGerry Liu 	devi_branch_t	*bp;
9320a3114836SGerry Liu 	char		*path;
9321a3114836SGerry Liu 
9322a3114836SGerry Liu 	ASSERT(ap);
9323a3114836SGerry Liu 	ASSERT(ap->fdip == NULL);
9324a3114836SGerry Liu 	ASSERT(ap->pdip && ndi_dev_is_prom_node(ap->pdip));
9325a3114836SGerry Liu 
9326a3114836SGerry Liu 	bp = ap->bp;
9327a3114836SGerry Liu 
9328a3114836SGerry Liu 	nodeid = ddi_get_nodeid(ap->pdip);
9329a3114836SGerry Liu 	if (nodeid == OBP_NONODE || nodeid == OBP_BADNODE) {
9330a3114836SGerry Liu 		cmn_err(CE_WARN, "create_prom_branch: invalid "
9331a3114836SGerry Liu 		    "nodeid: 0x%x", nodeid);
9332a3114836SGerry Liu 		return (EINVAL);
9333a3114836SGerry Liu 	}
9334a3114836SGerry Liu 
9335a3114836SGerry Liu 	ap->head = NULL;
9336a3114836SGerry Liu 
9337a3114836SGerry Liu 	nodeid = prom_childnode(nodeid);
9338a3114836SGerry Liu 	while (nodeid != OBP_NONODE && nodeid != OBP_BADNODE) {
9339a3114836SGerry Liu 		visit_node(nodeid, ap);
9340a3114836SGerry Liu 		nodeid = prom_nextnode(nodeid);
9341a3114836SGerry Liu 	}
9342a3114836SGerry Liu 
9343a3114836SGerry Liu 	if (ap->head == NULL)
9344a3114836SGerry Liu 		return (ENODEV);
9345a3114836SGerry Liu 
9346a3114836SGerry Liu 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
9347a3114836SGerry Liu 	rv = 0;
9348a3114836SGerry Liu 	while ((tnp = ap->head) != NULL) {
9349a3114836SGerry Liu 		ap->head = tnp->next;
9350a3114836SGerry Liu 
9351a3114836SGerry Liu 		ndi_devi_enter(ap->pdip, &circ);
9352a3114836SGerry Liu 
9353a3114836SGerry Liu 		/*
9354a3114836SGerry Liu 		 * Check if the branch already exists.
9355a3114836SGerry Liu 		 */
9356a3114836SGerry Liu 		exists = 0;
9357a3114836SGerry Liu 		dip = e_ddi_nodeid_to_dip(tnp->nodeid);
9358a3114836SGerry Liu 		if (dip != NULL) {
9359a3114836SGerry Liu 			exists = 1;
9360a3114836SGerry Liu 
9361a3114836SGerry Liu 			/* Parent is held busy, so release hold */
9362a3114836SGerry Liu 			ndi_rele_devi(dip);
9363a3114836SGerry Liu #ifdef	DEBUG
9364a3114836SGerry Liu 			cmn_err(CE_WARN, "create_prom_branch: dip(%p) exists"
9365a3114836SGerry Liu 			    " for nodeid 0x%x", (void *)dip, tnp->nodeid);
9366a3114836SGerry Liu #endif
9367a3114836SGerry Liu 		} else {
9368a3114836SGerry Liu 			dip = i_ddi_create_branch(ap->pdip, tnp->nodeid);
9369a3114836SGerry Liu 		}
9370a3114836SGerry Liu 
9371a3114836SGerry Liu 		kmem_free(tnp, sizeof (struct ptnode));
9372a3114836SGerry Liu 
9373a3114836SGerry Liu 		/*
9374a3114836SGerry Liu 		 * Hold the branch if it is not already held
9375a3114836SGerry Liu 		 */
9376a3114836SGerry Liu 		if (dip && !exists) {
9377a3114836SGerry Liu 			e_ddi_branch_hold(dip);
9378a3114836SGerry Liu 		}
9379a3114836SGerry Liu 
9380a3114836SGerry Liu 		ASSERT(dip == NULL || e_ddi_branch_held(dip));
9381a3114836SGerry Liu 
9382a3114836SGerry Liu 		/*
9383a3114836SGerry Liu 		 * Set all dips in the newly created branch offline so that
9384a3114836SGerry Liu 		 * only a "configure" operation can attach
9385a3114836SGerry Liu 		 * the branch
9386a3114836SGerry Liu 		 */
9387a3114836SGerry Liu 		if (dip == NULL || branch_set_offline(dip, path)
9388a3114836SGerry Liu 		    == DDI_FAILURE) {
9389a3114836SGerry Liu 			ndi_devi_exit(ap->pdip, circ);
9390a3114836SGerry Liu 			rv = EIO;
9391a3114836SGerry Liu 			continue;
9392a3114836SGerry Liu 		}
9393a3114836SGerry Liu 
9394a3114836SGerry Liu 		ASSERT(ddi_get_parent(dip) == ap->pdip);
9395a3114836SGerry Liu 
9396a3114836SGerry Liu 		ndi_devi_exit(ap->pdip, circ);
9397a3114836SGerry Liu 
9398a3114836SGerry Liu 		if (ap->flags & DEVI_BRANCH_CONFIGURE) {
9399a3114836SGerry Liu 			int error = e_ddi_branch_configure(dip, &ap->fdip, 0);
9400a3114836SGerry Liu 			if (error && rv == 0)
9401a3114836SGerry Liu 				rv = error;
9402a3114836SGerry Liu 		}
9403a3114836SGerry Liu 
9404a3114836SGerry Liu 		/*
9405a3114836SGerry Liu 		 * Invoke devi_branch_callback() (if it exists) only for
9406a3114836SGerry Liu 		 * newly created branches
9407a3114836SGerry Liu 		 */
9408a3114836SGerry Liu 		if (bp->devi_branch_callback && !exists)
9409a3114836SGerry Liu 			bp->devi_branch_callback(dip, bp->arg, 0);
9410a3114836SGerry Liu 	}
9411a3114836SGerry Liu 
9412a3114836SGerry Liu 	kmem_free(path, MAXPATHLEN);
9413a3114836SGerry Liu 
9414a3114836SGerry Liu 	return (rv);
9415a3114836SGerry Liu }
9416a3114836SGerry Liu 
9417a3114836SGerry Liu static int
9418a3114836SGerry Liu sid_node_create(dev_info_t *pdip, devi_branch_t *bp, dev_info_t **rdipp)
9419a3114836SGerry Liu {
9420a3114836SGerry Liu 	int			rv, circ, len;
9421a3114836SGerry Liu 	int			i, flags, ret;
9422a3114836SGerry Liu 	dev_info_t		*dip;
9423a3114836SGerry Liu 	char			*nbuf;
9424a3114836SGerry Liu 	char			*path;
9425a3114836SGerry Liu 	static const char	*noname = "<none>";
9426a3114836SGerry Liu 
9427a3114836SGerry Liu 	ASSERT(pdip);
9428a3114836SGerry Liu 	ASSERT(DEVI_BUSY_OWNED(pdip));
9429a3114836SGerry Liu 
9430a3114836SGerry Liu 	flags = 0;
9431a3114836SGerry Liu 
9432a3114836SGerry Liu 	/*
9433a3114836SGerry Liu 	 * Creating the root of a branch ?
9434a3114836SGerry Liu 	 */
9435a3114836SGerry Liu 	if (rdipp) {
9436a3114836SGerry Liu 		*rdipp = NULL;
9437a3114836SGerry Liu 		flags = DEVI_BRANCH_ROOT;
9438a3114836SGerry Liu 	}
9439a3114836SGerry Liu 
9440a3114836SGerry Liu 	ndi_devi_alloc_sleep(pdip, (char *)noname, DEVI_SID_NODEID, &dip);
9441a3114836SGerry Liu 	rv = bp->create.sid_branch_create(dip, bp->arg, flags);
9442a3114836SGerry Liu 
9443a3114836SGerry Liu 	nbuf = kmem_alloc(OBP_MAXDRVNAME, KM_SLEEP);
9444a3114836SGerry Liu 
9445a3114836SGerry Liu 	if (rv == DDI_WALK_ERROR) {
9446a3114836SGerry Liu 		cmn_err(CE_WARN, "e_ddi_branch_create: Error setting"
9447a3114836SGerry Liu 		    " properties on devinfo node %p",  (void *)dip);
9448a3114836SGerry Liu 		goto fail;
9449a3114836SGerry Liu 	}
9450a3114836SGerry Liu 
9451a3114836SGerry Liu 	len = OBP_MAXDRVNAME;
9452a3114836SGerry Liu 	if (ddi_getlongprop_buf(DDI_DEV_T_ANY, dip,
9453a3114836SGerry Liu 	    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, "name", nbuf, &len)
9454a3114836SGerry Liu 	    != DDI_PROP_SUCCESS) {
9455a3114836SGerry Liu 		cmn_err(CE_WARN, "e_ddi_branch_create: devinfo node %p has"
9456a3114836SGerry Liu 		    "no name property", (void *)dip);
9457a3114836SGerry Liu 		goto fail;
9458a3114836SGerry Liu 	}
9459a3114836SGerry Liu 
9460a3114836SGerry Liu 	ASSERT(i_ddi_node_state(dip) == DS_PROTO);
9461a3114836SGerry Liu 	if (ndi_devi_set_nodename(dip, nbuf, 0) != NDI_SUCCESS) {
9462a3114836SGerry Liu 		cmn_err(CE_WARN, "e_ddi_branch_create: cannot set name (%s)"
9463a3114836SGerry Liu 		    " for devinfo node %p", nbuf, (void *)dip);
9464a3114836SGerry Liu 		goto fail;
9465a3114836SGerry Liu 	}
9466a3114836SGerry Liu 
9467a3114836SGerry Liu 	kmem_free(nbuf, OBP_MAXDRVNAME);
9468a3114836SGerry Liu 
9469a3114836SGerry Liu 	/*
9470a3114836SGerry Liu 	 * Ignore bind failures just like boot does
9471a3114836SGerry Liu 	 */
9472a3114836SGerry Liu 	(void) ndi_devi_bind_driver(dip, 0);
9473a3114836SGerry Liu 
9474a3114836SGerry Liu 	switch (rv) {
9475a3114836SGerry Liu 	case DDI_WALK_CONTINUE:
9476a3114836SGerry Liu 	case DDI_WALK_PRUNESIB:
9477a3114836SGerry Liu 		ndi_devi_enter(dip, &circ);
9478a3114836SGerry Liu 
9479a3114836SGerry Liu 		i = DDI_WALK_CONTINUE;
9480a3114836SGerry Liu 		for (; i == DDI_WALK_CONTINUE; ) {
9481a3114836SGerry Liu 			i = sid_node_create(dip, bp, NULL);
9482a3114836SGerry Liu 		}
9483a3114836SGerry Liu 
9484a3114836SGerry Liu 		ASSERT(i == DDI_WALK_ERROR || i == DDI_WALK_PRUNESIB);
9485a3114836SGerry Liu 		if (i == DDI_WALK_ERROR)
9486a3114836SGerry Liu 			rv = i;
9487a3114836SGerry Liu 		/*
9488a3114836SGerry Liu 		 * If PRUNESIB stop creating siblings
9489a3114836SGerry Liu 		 * of dip's child. Subsequent walk behavior
9490a3114836SGerry Liu 		 * is determined by rv returned by dip.
9491a3114836SGerry Liu 		 */
9492a3114836SGerry Liu 
9493a3114836SGerry Liu 		ndi_devi_exit(dip, circ);
9494a3114836SGerry Liu 		break;
9495a3114836SGerry Liu 	case DDI_WALK_TERMINATE:
9496a3114836SGerry Liu 		/*
9497a3114836SGerry Liu 		 * Don't create children and ask our parent
9498a3114836SGerry Liu 		 * to not create siblings either.
9499a3114836SGerry Liu 		 */
9500a3114836SGerry Liu 		rv = DDI_WALK_PRUNESIB;
9501a3114836SGerry Liu 		break;
9502a3114836SGerry Liu 	case DDI_WALK_PRUNECHILD:
9503a3114836SGerry Liu 		/*
9504a3114836SGerry Liu 		 * Don't create children, but ask parent to continue
9505a3114836SGerry Liu 		 * with siblings.
9506a3114836SGerry Liu 		 */
9507a3114836SGerry Liu 		rv = DDI_WALK_CONTINUE;
9508a3114836SGerry Liu 		break;
9509a3114836SGerry Liu 	default:
9510a3114836SGerry Liu 		ASSERT(0);
9511a3114836SGerry Liu 		break;
9512a3114836SGerry Liu 	}
9513a3114836SGerry Liu 
9514a3114836SGerry Liu 	if (rdipp)
9515a3114836SGerry Liu 		*rdipp = dip;
9516a3114836SGerry Liu 
9517a3114836SGerry Liu 	/*
9518a3114836SGerry Liu 	 * Set device offline - only the "configure" op should cause an attach.
9519a3114836SGerry Liu 	 * Note that it is safe to set the dip offline without checking
9520a3114836SGerry Liu 	 * for either device contract or layered driver (LDI) based constraints
9521a3114836SGerry Liu 	 * since there cannot be any contracts or LDI opens of this device.
9522a3114836SGerry Liu 	 * This is because this node is a newly created dip with the parent busy
9523a3114836SGerry Liu 	 * held, so no other thread can come in and attach this dip. A dip that
9524a3114836SGerry Liu 	 * has never been attached cannot have contracts since by definition
9525a3114836SGerry Liu 	 * a device contract (an agreement between a process and a device minor
9526a3114836SGerry Liu 	 * node) can only be created against a device that has minor nodes
9527a3114836SGerry Liu 	 * i.e is attached. Similarly an LDI open will only succeed if the
9528a3114836SGerry Liu 	 * dip is attached. We assert below that the dip is not attached.
9529a3114836SGerry Liu 	 */
9530a3114836SGerry Liu 	ASSERT(i_ddi_node_state(dip) < DS_ATTACHED);
9531a3114836SGerry Liu 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
9532a3114836SGerry Liu 	ret = set_infant_dip_offline(dip, path);
9533a3114836SGerry Liu 	ASSERT(ret == DDI_SUCCESS);
9534a3114836SGerry Liu 	kmem_free(path, MAXPATHLEN);
9535a3114836SGerry Liu 
9536a3114836SGerry Liu 	return (rv);
9537a3114836SGerry Liu fail:
9538a3114836SGerry Liu 	(void) ndi_devi_free(dip);
9539a3114836SGerry Liu 	kmem_free(nbuf, OBP_MAXDRVNAME);
9540a3114836SGerry Liu 	return (DDI_WALK_ERROR);
9541a3114836SGerry Liu }
9542a3114836SGerry Liu 
9543a3114836SGerry Liu static int
9544a3114836SGerry Liu create_sid_branch(
9545a3114836SGerry Liu 	dev_info_t	*pdip,
9546a3114836SGerry Liu 	devi_branch_t	*bp,
9547a3114836SGerry Liu 	dev_info_t	**dipp,
9548a3114836SGerry Liu 	uint_t		flags)
9549a3114836SGerry Liu {
9550a3114836SGerry Liu 	int		rv = 0, state = DDI_WALK_CONTINUE;
9551a3114836SGerry Liu 	dev_info_t	*rdip;
9552a3114836SGerry Liu 
9553a3114836SGerry Liu 	while (state == DDI_WALK_CONTINUE) {
9554a3114836SGerry Liu 		int	circ;
9555a3114836SGerry Liu 
9556a3114836SGerry Liu 		ndi_devi_enter(pdip, &circ);
9557a3114836SGerry Liu 
9558a3114836SGerry Liu 		state = sid_node_create(pdip, bp, &rdip);
9559a3114836SGerry Liu 		if (rdip == NULL) {
9560a3114836SGerry Liu 			ndi_devi_exit(pdip, circ);
9561a3114836SGerry Liu 			ASSERT(state == DDI_WALK_ERROR);
9562a3114836SGerry Liu 			break;
9563a3114836SGerry Liu 		}
9564a3114836SGerry Liu 
9565a3114836SGerry Liu 		e_ddi_branch_hold(rdip);
9566a3114836SGerry Liu 
9567a3114836SGerry Liu 		ndi_devi_exit(pdip, circ);
9568a3114836SGerry Liu 
9569a3114836SGerry Liu 		if (flags & DEVI_BRANCH_CONFIGURE) {
9570a3114836SGerry Liu 			int error = e_ddi_branch_configure(rdip, dipp, 0);
9571a3114836SGerry Liu 			if (error && rv == 0)
9572a3114836SGerry Liu 				rv = error;
9573a3114836SGerry Liu 		}
9574a3114836SGerry Liu 
9575a3114836SGerry Liu 		/*
9576a3114836SGerry Liu 		 * devi_branch_callback() is optional
9577a3114836SGerry Liu 		 */
9578a3114836SGerry Liu 		if (bp->devi_branch_callback)
9579a3114836SGerry Liu 			bp->devi_branch_callback(rdip, bp->arg, 0);
9580a3114836SGerry Liu 	}
9581a3114836SGerry Liu 
9582a3114836SGerry Liu 	ASSERT(state == DDI_WALK_ERROR || state == DDI_WALK_PRUNESIB);
9583a3114836SGerry Liu 
9584a3114836SGerry Liu 	return (state == DDI_WALK_ERROR ? EIO : rv);
9585a3114836SGerry Liu }
9586a3114836SGerry Liu 
9587a3114836SGerry Liu int
9588a3114836SGerry Liu e_ddi_branch_create(
9589a3114836SGerry Liu 	dev_info_t	*pdip,
9590a3114836SGerry Liu 	devi_branch_t	*bp,
9591a3114836SGerry Liu 	dev_info_t	**dipp,
9592a3114836SGerry Liu 	uint_t		flags)
9593a3114836SGerry Liu {
9594a3114836SGerry Liu 	int prom_devi, sid_devi, error;
9595a3114836SGerry Liu 
9596a3114836SGerry Liu 	if (pdip == NULL || bp == NULL || bp->type == 0)
9597a3114836SGerry Liu 		return (EINVAL);
9598a3114836SGerry Liu 
9599a3114836SGerry Liu 	prom_devi = (bp->type == DEVI_BRANCH_PROM) ? 1 : 0;
9600a3114836SGerry Liu 	sid_devi = (bp->type == DEVI_BRANCH_SID) ? 1 : 0;
9601a3114836SGerry Liu 
9602a3114836SGerry Liu 	if (prom_devi && bp->create.prom_branch_select == NULL)
9603a3114836SGerry Liu 		return (EINVAL);
9604a3114836SGerry Liu 	else if (sid_devi && bp->create.sid_branch_create == NULL)
9605a3114836SGerry Liu 		return (EINVAL);
9606a3114836SGerry Liu 	else if (!prom_devi && !sid_devi)
9607a3114836SGerry Liu 		return (EINVAL);
9608a3114836SGerry Liu 
9609a3114836SGerry Liu 	if (flags & DEVI_BRANCH_EVENT)
9610a3114836SGerry Liu 		return (EINVAL);
9611a3114836SGerry Liu 
9612a3114836SGerry Liu 	if (prom_devi) {
9613a3114836SGerry Liu 		struct pta pta = {0};
9614a3114836SGerry Liu 
9615a3114836SGerry Liu 		pta.pdip = pdip;
9616a3114836SGerry Liu 		pta.bp = bp;
9617a3114836SGerry Liu 		pta.flags = flags;
9618a3114836SGerry Liu 
9619a3114836SGerry Liu 		error = prom_tree_access(create_prom_branch, &pta, NULL);
9620a3114836SGerry Liu 
9621a3114836SGerry Liu 		if (dipp)
9622a3114836SGerry Liu 			*dipp = pta.fdip;
9623a3114836SGerry Liu 		else if (pta.fdip)
9624a3114836SGerry Liu 			ndi_rele_devi(pta.fdip);
9625a3114836SGerry Liu 	} else {
9626a3114836SGerry Liu 		error = create_sid_branch(pdip, bp, dipp, flags);
9627a3114836SGerry Liu 	}
9628a3114836SGerry Liu 
9629a3114836SGerry Liu 	return (error);
9630a3114836SGerry Liu }
9631a3114836SGerry Liu 
9632a3114836SGerry Liu int
9633a3114836SGerry Liu e_ddi_branch_configure(dev_info_t *rdip, dev_info_t **dipp, uint_t flags)
9634a3114836SGerry Liu {
9635a3114836SGerry Liu 	int		rv;
9636a3114836SGerry Liu 	char		*devnm;
9637a3114836SGerry Liu 	dev_info_t	*pdip;
9638a3114836SGerry Liu 
9639a3114836SGerry Liu 	if (dipp)
9640a3114836SGerry Liu 		*dipp = NULL;
9641a3114836SGerry Liu 
9642a3114836SGerry Liu 	if (rdip == NULL || flags != 0 || (flags & DEVI_BRANCH_EVENT))
9643a3114836SGerry Liu 		return (EINVAL);
9644a3114836SGerry Liu 
9645a3114836SGerry Liu 	pdip = ddi_get_parent(rdip);
9646a3114836SGerry Liu 
9647a3114836SGerry Liu 	ndi_hold_devi(pdip);
9648a3114836SGerry Liu 
9649a3114836SGerry Liu 	if (!e_ddi_branch_held(rdip)) {
9650a3114836SGerry Liu 		ndi_rele_devi(pdip);
9651a3114836SGerry Liu 		cmn_err(CE_WARN, "e_ddi_branch_configure: "
9652a3114836SGerry Liu 		    "dip(%p) not held", (void *)rdip);
9653a3114836SGerry Liu 		return (EINVAL);
9654a3114836SGerry Liu 	}
9655a3114836SGerry Liu 
9656a3114836SGerry Liu 	if (i_ddi_node_state(rdip) < DS_INITIALIZED) {
9657a3114836SGerry Liu 		/*
9658a3114836SGerry Liu 		 * First attempt to bind a driver. If we fail, return
9659a3114836SGerry Liu 		 * success (On some platforms, dips for some device
9660a3114836SGerry Liu 		 * types (CPUs) may not have a driver)
9661a3114836SGerry Liu 		 */
9662a3114836SGerry Liu 		if (ndi_devi_bind_driver(rdip, 0) != NDI_SUCCESS) {
9663a3114836SGerry Liu 			ndi_rele_devi(pdip);
9664a3114836SGerry Liu 			return (0);
9665a3114836SGerry Liu 		}
9666a3114836SGerry Liu 
9667a3114836SGerry Liu 		if (ddi_initchild(pdip, rdip) != DDI_SUCCESS) {
9668a3114836SGerry Liu 			rv = NDI_FAILURE;
9669a3114836SGerry Liu 			goto out;
9670a3114836SGerry Liu 		}
9671a3114836SGerry Liu 	}
9672a3114836SGerry Liu 
9673a3114836SGerry Liu 	ASSERT(i_ddi_node_state(rdip) >= DS_INITIALIZED);
9674a3114836SGerry Liu 
9675a3114836SGerry Liu 	devnm = kmem_alloc(MAXNAMELEN + 1, KM_SLEEP);
9676a3114836SGerry Liu 
9677a3114836SGerry Liu 	(void) ddi_deviname(rdip, devnm);
9678a3114836SGerry Liu 
9679a3114836SGerry Liu 	if ((rv = ndi_devi_config_one(pdip, devnm+1, &rdip,
9680a3114836SGerry Liu 	    NDI_DEVI_ONLINE | NDI_CONFIG)) == NDI_SUCCESS) {
9681a3114836SGerry Liu 		/* release hold from ndi_devi_config_one() */
9682a3114836SGerry Liu 		ndi_rele_devi(rdip);
9683a3114836SGerry Liu 	}
9684a3114836SGerry Liu 
9685a3114836SGerry Liu 	kmem_free(devnm, MAXNAMELEN + 1);
9686a3114836SGerry Liu out:
9687a3114836SGerry Liu 	if (rv != NDI_SUCCESS && dipp && rdip) {
9688a3114836SGerry Liu 		ndi_hold_devi(rdip);
9689a3114836SGerry Liu 		*dipp = rdip;
9690a3114836SGerry Liu 	}
9691a3114836SGerry Liu 	ndi_rele_devi(pdip);
9692a3114836SGerry Liu 	return (ndi2errno(rv));
9693a3114836SGerry Liu }
9694a3114836SGerry Liu 
9695a3114836SGerry Liu void
9696a3114836SGerry Liu e_ddi_branch_hold(dev_info_t *rdip)
9697a3114836SGerry Liu {
9698a3114836SGerry Liu 	if (e_ddi_branch_held(rdip)) {
9699a3114836SGerry Liu 		cmn_err(CE_WARN, "e_ddi_branch_hold: branch already held");
9700a3114836SGerry Liu 		return;
9701a3114836SGerry Liu 	}
9702a3114836SGerry Liu 
9703a3114836SGerry Liu 	mutex_enter(&DEVI(rdip)->devi_lock);
9704a3114836SGerry Liu 	if ((DEVI(rdip)->devi_flags & DEVI_BRANCH_HELD) == 0) {
9705a3114836SGerry Liu 		DEVI(rdip)->devi_flags |= DEVI_BRANCH_HELD;
9706a3114836SGerry Liu 		DEVI(rdip)->devi_ref++;
9707a3114836SGerry Liu 	}
9708a3114836SGerry Liu 	ASSERT(DEVI(rdip)->devi_ref > 0);
9709a3114836SGerry Liu 	mutex_exit(&DEVI(rdip)->devi_lock);
9710a3114836SGerry Liu }
9711a3114836SGerry Liu 
9712a3114836SGerry Liu int
9713a3114836SGerry Liu e_ddi_branch_held(dev_info_t *rdip)
9714a3114836SGerry Liu {
9715a3114836SGerry Liu 	int rv = 0;
9716a3114836SGerry Liu 
9717a3114836SGerry Liu 	mutex_enter(&DEVI(rdip)->devi_lock);
9718a3114836SGerry Liu 	if ((DEVI(rdip)->devi_flags & DEVI_BRANCH_HELD) &&
9719a3114836SGerry Liu 	    DEVI(rdip)->devi_ref > 0) {
9720a3114836SGerry Liu 		rv = 1;
9721a3114836SGerry Liu 	}
9722a3114836SGerry Liu 	mutex_exit(&DEVI(rdip)->devi_lock);
9723a3114836SGerry Liu 
9724a3114836SGerry Liu 	return (rv);
9725a3114836SGerry Liu }
9726a3114836SGerry Liu 
9727a3114836SGerry Liu void
9728a3114836SGerry Liu e_ddi_branch_rele(dev_info_t *rdip)
9729a3114836SGerry Liu {
9730a3114836SGerry Liu 	mutex_enter(&DEVI(rdip)->devi_lock);
9731a3114836SGerry Liu 	DEVI(rdip)->devi_flags &= ~DEVI_BRANCH_HELD;
9732a3114836SGerry Liu 	DEVI(rdip)->devi_ref--;
9733a3114836SGerry Liu 	mutex_exit(&DEVI(rdip)->devi_lock);
9734a3114836SGerry Liu }
9735a3114836SGerry Liu 
9736a3114836SGerry Liu int
9737a3114836SGerry Liu e_ddi_branch_unconfigure(
9738a3114836SGerry Liu 	dev_info_t *rdip,
9739a3114836SGerry Liu 	dev_info_t **dipp,
9740a3114836SGerry Liu 	uint_t flags)
9741a3114836SGerry Liu {
9742a3114836SGerry Liu 	int	circ, rv;
9743a3114836SGerry Liu 	int	destroy;
9744a3114836SGerry Liu 	char	*devnm;
9745a3114836SGerry Liu 	uint_t	nflags;
9746a3114836SGerry Liu 	dev_info_t *pdip;
9747a3114836SGerry Liu 
9748a3114836SGerry Liu 	if (dipp)
9749a3114836SGerry Liu 		*dipp = NULL;
9750a3114836SGerry Liu 
9751a3114836SGerry Liu 	if (rdip == NULL)
9752a3114836SGerry Liu 		return (EINVAL);
9753a3114836SGerry Liu 
9754a3114836SGerry Liu 	pdip = ddi_get_parent(rdip);
9755a3114836SGerry Liu 
9756a3114836SGerry Liu 	ASSERT(pdip);
9757a3114836SGerry Liu 
9758a3114836SGerry Liu 	/*
9759a3114836SGerry Liu 	 * Check if caller holds pdip busy - can cause deadlocks during
9760a3114836SGerry Liu 	 * devfs_clean()
9761a3114836SGerry Liu 	 */
9762a3114836SGerry Liu 	if (DEVI_BUSY_OWNED(pdip)) {
9763a3114836SGerry Liu 		cmn_err(CE_WARN, "e_ddi_branch_unconfigure: failed: parent"
9764a3114836SGerry Liu 		    " devinfo node(%p) is busy held", (void *)pdip);
9765a3114836SGerry Liu 		return (EINVAL);
9766a3114836SGerry Liu 	}
9767a3114836SGerry Liu 
9768a3114836SGerry Liu 	destroy = (flags & DEVI_BRANCH_DESTROY) ? 1 : 0;
9769a3114836SGerry Liu 
9770a3114836SGerry Liu 	devnm = kmem_alloc(MAXNAMELEN + 1, KM_SLEEP);
9771a3114836SGerry Liu 
9772a3114836SGerry Liu 	ndi_devi_enter(pdip, &circ);
9773a3114836SGerry Liu 	(void) ddi_deviname(rdip, devnm);
9774a3114836SGerry Liu 	ndi_devi_exit(pdip, circ);
9775a3114836SGerry Liu 
9776a3114836SGerry Liu 	/*
9777a3114836SGerry Liu 	 * ddi_deviname() returns a component name with / prepended.
9778a3114836SGerry Liu 	 */
9779a3114836SGerry Liu 	(void) devfs_clean(pdip, devnm + 1, DV_CLEAN_FORCE);
9780a3114836SGerry Liu 
9781a3114836SGerry Liu 	ndi_devi_enter(pdip, &circ);
9782a3114836SGerry Liu 
9783a3114836SGerry Liu 	/*
9784a3114836SGerry Liu 	 * Recreate device name as it may have changed state (init/uninit)
9785a3114836SGerry Liu 	 * when parent busy lock was dropped for devfs_clean()
9786a3114836SGerry Liu 	 */
9787a3114836SGerry Liu 	(void) ddi_deviname(rdip, devnm);
9788a3114836SGerry Liu 
9789a3114836SGerry Liu 	if (!e_ddi_branch_held(rdip)) {
9790a3114836SGerry Liu 		kmem_free(devnm, MAXNAMELEN + 1);
9791a3114836SGerry Liu 		ndi_devi_exit(pdip, circ);
9792a3114836SGerry Liu 		cmn_err(CE_WARN, "e_ddi_%s_branch: dip(%p) not held",
9793a3114836SGerry Liu 		    destroy ? "destroy" : "unconfigure", (void *)rdip);
9794a3114836SGerry Liu 		return (EINVAL);
9795a3114836SGerry Liu 	}
9796a3114836SGerry Liu 
9797a3114836SGerry Liu 	/*
9798a3114836SGerry Liu 	 * Release hold on the branch. This is ok since we are holding the
9799a3114836SGerry Liu 	 * parent busy. If rdip is not removed, we must do a hold on the
9800a3114836SGerry Liu 	 * branch before returning.
9801a3114836SGerry Liu 	 */
9802a3114836SGerry Liu 	e_ddi_branch_rele(rdip);
9803a3114836SGerry Liu 
9804a3114836SGerry Liu 	nflags = NDI_DEVI_OFFLINE;
9805a3114836SGerry Liu 	if (destroy || (flags & DEVI_BRANCH_DESTROY)) {
9806a3114836SGerry Liu 		nflags |= NDI_DEVI_REMOVE;
9807a3114836SGerry Liu 		destroy = 1;
9808a3114836SGerry Liu 	} else {
9809a3114836SGerry Liu 		nflags |= NDI_UNCONFIG;		/* uninit but don't remove */
9810a3114836SGerry Liu 	}
9811a3114836SGerry Liu 
9812a3114836SGerry Liu 	if (flags & DEVI_BRANCH_EVENT)
9813a3114836SGerry Liu 		nflags |= NDI_POST_EVENT;
9814a3114836SGerry Liu 
9815a3114836SGerry Liu 	if (i_ddi_devi_attached(pdip) &&
9816a3114836SGerry Liu 	    (i_ddi_node_state(rdip) >= DS_INITIALIZED)) {
9817a3114836SGerry Liu 		rv = ndi_devi_unconfig_one(pdip, devnm+1, dipp, nflags);
9818a3114836SGerry Liu 	} else {
9819a3114836SGerry Liu 		rv = e_ddi_devi_unconfig(rdip, dipp, nflags);
9820a3114836SGerry Liu 		if (rv == NDI_SUCCESS) {
9821a3114836SGerry Liu 			ASSERT(!destroy || ddi_get_child(rdip) == NULL);
9822a3114836SGerry Liu 			rv = ndi_devi_offline(rdip, nflags);
9823a3114836SGerry Liu 		}
9824a3114836SGerry Liu 	}
9825a3114836SGerry Liu 
9826a3114836SGerry Liu 	if (!destroy || rv != NDI_SUCCESS) {
9827a3114836SGerry Liu 		/* The dip still exists, so do a hold */
9828a3114836SGerry Liu 		e_ddi_branch_hold(rdip);
9829a3114836SGerry Liu 	}
9830a3114836SGerry Liu out:
9831a3114836SGerry Liu 	kmem_free(devnm, MAXNAMELEN + 1);
9832a3114836SGerry Liu 	ndi_devi_exit(pdip, circ);
9833a3114836SGerry Liu 	return (ndi2errno(rv));
9834a3114836SGerry Liu }
9835a3114836SGerry Liu 
9836a3114836SGerry Liu int
9837a3114836SGerry Liu e_ddi_branch_destroy(dev_info_t *rdip, dev_info_t **dipp, uint_t flag)
9838a3114836SGerry Liu {
9839a3114836SGerry Liu 	return (e_ddi_branch_unconfigure(rdip, dipp,
9840a3114836SGerry Liu 	    flag|DEVI_BRANCH_DESTROY));
9841a3114836SGerry Liu }
9842a3114836SGerry Liu 
9843a3114836SGerry Liu /*
9844a3114836SGerry Liu  * Number of chains for hash table
9845a3114836SGerry Liu  */
9846a3114836SGerry Liu #define	NUMCHAINS	17
9847a3114836SGerry Liu 
9848a3114836SGerry Liu /*
9849a3114836SGerry Liu  * Devinfo busy arg
9850a3114836SGerry Liu  */
9851a3114836SGerry Liu struct devi_busy {
9852a3114836SGerry Liu 	int dv_total;
9853a3114836SGerry Liu 	int s_total;
9854a3114836SGerry Liu 	mod_hash_t *dv_hash;
9855a3114836SGerry Liu 	mod_hash_t *s_hash;
9856a3114836SGerry Liu 	int (*callback)(dev_info_t *, void *, uint_t);
9857a3114836SGerry Liu 	void *arg;
9858a3114836SGerry Liu };
9859a3114836SGerry Liu 
9860a3114836SGerry Liu static int
9861a3114836SGerry Liu visit_dip(dev_info_t *dip, void *arg)
9862a3114836SGerry Liu {
9863a3114836SGerry Liu 	uintptr_t sbusy, dvbusy, ref;
9864a3114836SGerry Liu 	struct devi_busy *bsp = arg;
9865a3114836SGerry Liu 
9866a3114836SGerry Liu 	ASSERT(bsp->callback);
9867a3114836SGerry Liu 
9868a3114836SGerry Liu 	/*
9869a3114836SGerry Liu 	 * A dip cannot be busy if its reference count is 0
9870a3114836SGerry Liu 	 */
9871a3114836SGerry Liu 	if ((ref = e_ddi_devi_holdcnt(dip)) == 0) {
9872a3114836SGerry Liu 		return (bsp->callback(dip, bsp->arg, 0));
9873a3114836SGerry Liu 	}
9874a3114836SGerry Liu 
9875a3114836SGerry Liu 	if (mod_hash_find(bsp->dv_hash, dip, (mod_hash_val_t *)&dvbusy))
9876a3114836SGerry Liu 		dvbusy = 0;
9877a3114836SGerry Liu 
9878a3114836SGerry Liu 	/*
9879a3114836SGerry Liu 	 * To catch device opens currently maintained on specfs common snodes.
9880a3114836SGerry Liu 	 */
9881a3114836SGerry Liu 	if (mod_hash_find(bsp->s_hash, dip, (mod_hash_val_t *)&sbusy))
9882a3114836SGerry Liu 		sbusy = 0;
9883a3114836SGerry Liu 
9884a3114836SGerry Liu #ifdef	DEBUG
9885a3114836SGerry Liu 	if (ref < sbusy || ref < dvbusy) {
9886a3114836SGerry Liu 		cmn_err(CE_WARN, "dip(%p): sopen = %lu, dvopen = %lu "
9887a3114836SGerry Liu 		    "dip ref = %lu\n", (void *)dip, sbusy, dvbusy, ref);
9888a3114836SGerry Liu 	}
9889a3114836SGerry Liu #endif
9890a3114836SGerry Liu 
9891a3114836SGerry Liu 	dvbusy = (sbusy > dvbusy) ? sbusy : dvbusy;
9892a3114836SGerry Liu 
9893a3114836SGerry Liu 	return (bsp->callback(dip, bsp->arg, dvbusy));
9894a3114836SGerry Liu }
9895a3114836SGerry Liu 
9896a3114836SGerry Liu static int
9897a3114836SGerry Liu visit_snode(struct snode *sp, void *arg)
9898a3114836SGerry Liu {
9899a3114836SGerry Liu 	uintptr_t sbusy;
9900a3114836SGerry Liu 	dev_info_t *dip;
9901a3114836SGerry Liu 	int count;
9902a3114836SGerry Liu 	struct devi_busy *bsp = arg;
9903a3114836SGerry Liu 
9904a3114836SGerry Liu 	ASSERT(sp);
9905a3114836SGerry Liu 
9906a3114836SGerry Liu 	/*
9907a3114836SGerry Liu 	 * The stable lock is held. This prevents
9908a3114836SGerry Liu 	 * the snode and its associated dip from
9909a3114836SGerry Liu 	 * going away.
9910a3114836SGerry Liu 	 */
9911a3114836SGerry Liu 	dip = NULL;
9912a3114836SGerry Liu 	count = spec_devi_open_count(sp, &dip);
9913a3114836SGerry Liu 
9914a3114836SGerry Liu 	if (count <= 0)
9915a3114836SGerry Liu 		return (DDI_WALK_CONTINUE);
9916a3114836SGerry Liu 
9917a3114836SGerry Liu 	ASSERT(dip);
9918a3114836SGerry Liu 
9919a3114836SGerry Liu 	if (mod_hash_remove(bsp->s_hash, dip, (mod_hash_val_t *)&sbusy))
9920a3114836SGerry Liu 		sbusy = count;
9921a3114836SGerry Liu 	else
9922a3114836SGerry Liu 		sbusy += count;
9923a3114836SGerry Liu 
9924a3114836SGerry Liu 	if (mod_hash_insert(bsp->s_hash, dip, (mod_hash_val_t)sbusy)) {
9925a3114836SGerry Liu 		cmn_err(CE_WARN, "%s: s_hash insert failed: dip=0x%p, "
9926a3114836SGerry Liu 		    "sbusy = %lu", "e_ddi_branch_referenced",
9927a3114836SGerry Liu 		    (void *)dip, sbusy);
9928a3114836SGerry Liu 	}
9929a3114836SGerry Liu 
9930a3114836SGerry Liu 	bsp->s_total += count;
9931a3114836SGerry Liu 
9932a3114836SGerry Liu 	return (DDI_WALK_CONTINUE);
9933a3114836SGerry Liu }
9934a3114836SGerry Liu 
9935a3114836SGerry Liu static void
9936a3114836SGerry Liu visit_dvnode(struct dv_node *dv, void *arg)
9937a3114836SGerry Liu {
9938a3114836SGerry Liu 	uintptr_t dvbusy;
9939a3114836SGerry Liu 	uint_t count;
9940a3114836SGerry Liu 	struct vnode *vp;
9941a3114836SGerry Liu 	struct devi_busy *bsp = arg;
9942a3114836SGerry Liu 
9943a3114836SGerry Liu 	ASSERT(dv && dv->dv_devi);
9944a3114836SGerry Liu 
9945a3114836SGerry Liu 	vp = DVTOV(dv);
9946a3114836SGerry Liu 
9947a3114836SGerry Liu 	mutex_enter(&vp->v_lock);
9948a3114836SGerry Liu 	count = vp->v_count;
9949a3114836SGerry Liu 	mutex_exit(&vp->v_lock);
9950a3114836SGerry Liu 
9951a3114836SGerry Liu 	if (!count)
9952a3114836SGerry Liu 		return;
9953a3114836SGerry Liu 
9954a3114836SGerry Liu 	if (mod_hash_remove(bsp->dv_hash, dv->dv_devi,
9955a3114836SGerry Liu 	    (mod_hash_val_t *)&dvbusy))
9956a3114836SGerry Liu 		dvbusy = count;
9957a3114836SGerry Liu 	else
9958a3114836SGerry Liu 		dvbusy += count;
9959a3114836SGerry Liu 
9960a3114836SGerry Liu 	if (mod_hash_insert(bsp->dv_hash, dv->dv_devi,
9961a3114836SGerry Liu 	    (mod_hash_val_t)dvbusy)) {
9962a3114836SGerry Liu 		cmn_err(CE_WARN, "%s: dv_hash insert failed: dip=0x%p, "
9963a3114836SGerry Liu 		    "dvbusy=%lu", "e_ddi_branch_referenced",
9964a3114836SGerry Liu 		    (void *)dv->dv_devi, dvbusy);
9965a3114836SGerry Liu 	}
9966a3114836SGerry Liu 
9967a3114836SGerry Liu 	bsp->dv_total += count;
9968a3114836SGerry Liu }
9969a3114836SGerry Liu 
9970a3114836SGerry Liu /*
9971a3114836SGerry Liu  * Returns reference count on success or -1 on failure.
9972a3114836SGerry Liu  */
9973a3114836SGerry Liu int
9974a3114836SGerry Liu e_ddi_branch_referenced(
9975a3114836SGerry Liu 	dev_info_t *rdip,
9976a3114836SGerry Liu 	int (*callback)(dev_info_t *dip, void *arg, uint_t ref),
9977a3114836SGerry Liu 	void *arg)
9978a3114836SGerry Liu {
9979a3114836SGerry Liu 	int circ;
9980a3114836SGerry Liu 	char *path;
9981a3114836SGerry Liu 	dev_info_t *pdip;
9982a3114836SGerry Liu 	struct devi_busy bsa = {0};
9983a3114836SGerry Liu 
9984a3114836SGerry Liu 	ASSERT(rdip);
9985a3114836SGerry Liu 
9986a3114836SGerry Liu 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
9987a3114836SGerry Liu 
9988a3114836SGerry Liu 	ndi_hold_devi(rdip);
9989a3114836SGerry Liu 
9990a3114836SGerry Liu 	pdip = ddi_get_parent(rdip);
9991a3114836SGerry Liu 
9992a3114836SGerry Liu 	ASSERT(pdip);
9993a3114836SGerry Liu 
9994a3114836SGerry Liu 	/*
9995a3114836SGerry Liu 	 * Check if caller holds pdip busy - can cause deadlocks during
9996a3114836SGerry Liu 	 * devfs_walk()
9997a3114836SGerry Liu 	 */
9998a3114836SGerry Liu 	if (!e_ddi_branch_held(rdip) || DEVI_BUSY_OWNED(pdip)) {
9999a3114836SGerry Liu 		cmn_err(CE_WARN, "e_ddi_branch_referenced: failed: "
10000a3114836SGerry Liu 		    "devinfo branch(%p) not held or parent busy held",
10001a3114836SGerry Liu 		    (void *)rdip);
10002a3114836SGerry Liu 		ndi_rele_devi(rdip);
10003a3114836SGerry Liu 		kmem_free(path, MAXPATHLEN);
10004a3114836SGerry Liu 		return (-1);
10005a3114836SGerry Liu 	}
10006a3114836SGerry Liu 
10007a3114836SGerry Liu 	ndi_devi_enter(pdip, &circ);
10008a3114836SGerry Liu 	(void) ddi_pathname(rdip, path);
10009a3114836SGerry Liu 	ndi_devi_exit(pdip, circ);
10010a3114836SGerry Liu 
10011a3114836SGerry Liu 	bsa.dv_hash = mod_hash_create_ptrhash("dv_node busy hash", NUMCHAINS,
10012a3114836SGerry Liu 	    mod_hash_null_valdtor, sizeof (struct dev_info));
10013a3114836SGerry Liu 
10014a3114836SGerry Liu 	bsa.s_hash = mod_hash_create_ptrhash("snode busy hash", NUMCHAINS,
10015a3114836SGerry Liu 	    mod_hash_null_valdtor, sizeof (struct snode));
10016a3114836SGerry Liu 
10017a3114836SGerry Liu 	if (devfs_walk(path, visit_dvnode, &bsa)) {
10018a3114836SGerry Liu 		cmn_err(CE_WARN, "e_ddi_branch_referenced: "
10019a3114836SGerry Liu 		    "devfs walk failed for: %s", path);
10020a3114836SGerry Liu 		kmem_free(path, MAXPATHLEN);
10021a3114836SGerry Liu 		bsa.s_total = bsa.dv_total = -1;
10022a3114836SGerry Liu 		goto out;
10023a3114836SGerry Liu 	}
10024a3114836SGerry Liu 
10025a3114836SGerry Liu 	kmem_free(path, MAXPATHLEN);
10026a3114836SGerry Liu 
10027a3114836SGerry Liu 	/*
10028a3114836SGerry Liu 	 * Walk the snode table to detect device opens, which are currently
10029a3114836SGerry Liu 	 * maintained on specfs common snodes.
10030a3114836SGerry Liu 	 */
10031a3114836SGerry Liu 	spec_snode_walk(visit_snode, &bsa);
10032a3114836SGerry Liu 
10033a3114836SGerry Liu 	if (callback == NULL)
10034a3114836SGerry Liu 		goto out;
10035a3114836SGerry Liu 
10036a3114836SGerry Liu 	bsa.callback = callback;
10037a3114836SGerry Liu 	bsa.arg = arg;
10038a3114836SGerry Liu 
10039a3114836SGerry Liu 	if (visit_dip(rdip, &bsa) == DDI_WALK_CONTINUE) {
10040a3114836SGerry Liu 		ndi_devi_enter(rdip, &circ);
10041a3114836SGerry Liu 		ddi_walk_devs(ddi_get_child(rdip), visit_dip, &bsa);
10042a3114836SGerry Liu 		ndi_devi_exit(rdip, circ);
10043a3114836SGerry Liu 	}
10044a3114836SGerry Liu 
10045a3114836SGerry Liu out:
10046a3114836SGerry Liu 	ndi_rele_devi(rdip);
10047a3114836SGerry Liu 	mod_hash_destroy_ptrhash(bsa.s_hash);
10048a3114836SGerry Liu 	mod_hash_destroy_ptrhash(bsa.dv_hash);
10049a3114836SGerry Liu 	return (bsa.s_total > bsa.dv_total ? bsa.s_total : bsa.dv_total);
10050a3114836SGerry Liu }
10051