xref: /onnv-gate/usr/src/uts/sun4/os/ddi_impl.c (revision 1991:f29baf5bf770)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51865Sdilpreet  * Common Development and Distribution License (the "License").
61865Sdilpreet  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
211900Seota 
220Sstevel@tonic-gate /*
231865Sdilpreet  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * sun4 specific DDI implementation
310Sstevel@tonic-gate  */
320Sstevel@tonic-gate #include <sys/cpuvar.h>
330Sstevel@tonic-gate #include <sys/ddi_subrdefs.h>
340Sstevel@tonic-gate #include <sys/machsystm.h>
350Sstevel@tonic-gate #include <sys/sunndi.h>
360Sstevel@tonic-gate #include <sys/sysmacros.h>
370Sstevel@tonic-gate #include <sys/ontrap.h>
380Sstevel@tonic-gate #include <vm/seg_kmem.h>
390Sstevel@tonic-gate #include <sys/membar.h>
400Sstevel@tonic-gate #include <sys/dditypes.h>
410Sstevel@tonic-gate #include <sys/ndifm.h>
420Sstevel@tonic-gate #include <sys/fm/io/ddi.h>
430Sstevel@tonic-gate #include <sys/ivintr.h>
440Sstevel@tonic-gate #include <sys/bootconf.h>
450Sstevel@tonic-gate #include <sys/conf.h>
460Sstevel@tonic-gate #include <sys/ethernet.h>
470Sstevel@tonic-gate #include <sys/idprom.h>
480Sstevel@tonic-gate #include <sys/promif.h>
490Sstevel@tonic-gate #include <sys/prom_plat.h>
500Sstevel@tonic-gate #include <sys/systeminfo.h>
510Sstevel@tonic-gate #include <sys/fpu/fpusystm.h>
520Sstevel@tonic-gate #include <sys/vm.h>
530Sstevel@tonic-gate #include <sys/fs/dv_node.h>
540Sstevel@tonic-gate #include <sys/fs/snode.h>
550Sstevel@tonic-gate #include <sys/ddi_isa.h>
56*1991Sheppo #include <sys/modhash.h>
570Sstevel@tonic-gate 
580Sstevel@tonic-gate dev_info_t *get_intr_parent(dev_info_t *, dev_info_t *,
59693Sgovinda     ddi_intr_handle_impl_t *);
600Sstevel@tonic-gate #pragma weak get_intr_parent
610Sstevel@tonic-gate 
620Sstevel@tonic-gate int process_intr_ops(dev_info_t *, dev_info_t *, ddi_intr_op_t,
630Sstevel@tonic-gate     ddi_intr_handle_impl_t *, void *);
640Sstevel@tonic-gate #pragma weak process_intr_ops
650Sstevel@tonic-gate 
660Sstevel@tonic-gate void cells_1275_copy(prop_1275_cell_t *, prop_1275_cell_t *, int32_t);
670Sstevel@tonic-gate     prop_1275_cell_t *cells_1275_cmp(prop_1275_cell_t *, prop_1275_cell_t *,
680Sstevel@tonic-gate     int32_t len);
690Sstevel@tonic-gate #pragma weak cells_1275_copy
700Sstevel@tonic-gate 
710Sstevel@tonic-gate /*
720Sstevel@tonic-gate  * Wrapper for ddi_prop_lookup_int_array().
730Sstevel@tonic-gate  * This is handy because it returns the prop length in
740Sstevel@tonic-gate  * bytes which is what most of the callers require.
750Sstevel@tonic-gate  */
760Sstevel@tonic-gate 
770Sstevel@tonic-gate static int
780Sstevel@tonic-gate get_prop_int_array(dev_info_t *di, char *pname, int **pval, uint_t *plen)
790Sstevel@tonic-gate {
800Sstevel@tonic-gate 	int ret;
810Sstevel@tonic-gate 
820Sstevel@tonic-gate 	if ((ret = ddi_prop_lookup_int_array(DDI_DEV_T_ANY, di,
830Sstevel@tonic-gate 	    DDI_PROP_DONTPASS, pname, pval, plen)) == DDI_PROP_SUCCESS) {
840Sstevel@tonic-gate 		*plen = (*plen) * (uint_t)sizeof (int);
850Sstevel@tonic-gate 	}
860Sstevel@tonic-gate 	return (ret);
870Sstevel@tonic-gate }
880Sstevel@tonic-gate 
890Sstevel@tonic-gate /*
900Sstevel@tonic-gate  * SECTION: DDI Node Configuration
910Sstevel@tonic-gate  */
920Sstevel@tonic-gate 
930Sstevel@tonic-gate /*
940Sstevel@tonic-gate  * init_regspec_64:
950Sstevel@tonic-gate  *
960Sstevel@tonic-gate  * If the parent #size-cells is 2, convert the upa-style or
970Sstevel@tonic-gate  * safari-style reg property from 2-size cells to 1 size cell
980Sstevel@tonic-gate  * format, ignoring the size_hi, which must be zero for devices.
990Sstevel@tonic-gate  * (It won't be zero in the memory list properties in the memory
1000Sstevel@tonic-gate  * nodes, but that doesn't matter here.)
1010Sstevel@tonic-gate  */
1020Sstevel@tonic-gate struct ddi_parent_private_data *
1030Sstevel@tonic-gate init_regspec_64(dev_info_t *dip)
1040Sstevel@tonic-gate {
1050Sstevel@tonic-gate 	struct ddi_parent_private_data *pd;
1060Sstevel@tonic-gate 	dev_info_t *parent;
1070Sstevel@tonic-gate 	int size_cells;
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate 	/*
1100Sstevel@tonic-gate 	 * If there are no "reg"s in the child node, return.
1110Sstevel@tonic-gate 	 */
1120Sstevel@tonic-gate 	pd = ddi_get_parent_data(dip);
1130Sstevel@tonic-gate 	if ((pd == NULL) || (pd->par_nreg == 0)) {
1140Sstevel@tonic-gate 		return (pd);
1150Sstevel@tonic-gate 	}
1160Sstevel@tonic-gate 	parent = ddi_get_parent(dip);
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate 	size_cells = ddi_prop_get_int(DDI_DEV_T_ANY, parent,
1190Sstevel@tonic-gate 	    DDI_PROP_DONTPASS, "#size-cells", 1);
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate 	if (size_cells != 1)  {
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate 		int n, j;
1240Sstevel@tonic-gate 		struct regspec *irp;
1250Sstevel@tonic-gate 		struct reg_64 {
1260Sstevel@tonic-gate 			uint_t addr_hi, addr_lo, size_hi, size_lo;
1270Sstevel@tonic-gate 		};
1280Sstevel@tonic-gate 		struct reg_64 *r64_rp;
1290Sstevel@tonic-gate 		struct regspec *rp;
1300Sstevel@tonic-gate 		uint_t len = 0;
1310Sstevel@tonic-gate 		int *reg_prop;
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate 		ASSERT(size_cells == 2);
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate 		/*
1360Sstevel@tonic-gate 		 * We already looked the property up once before if
1370Sstevel@tonic-gate 		 * pd is non-NULL.
1380Sstevel@tonic-gate 		 */
1390Sstevel@tonic-gate 		(void) ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dip,
1400Sstevel@tonic-gate 		    DDI_PROP_DONTPASS, OBP_REG, &reg_prop, &len);
1410Sstevel@tonic-gate 		ASSERT(len != 0);
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate 		n = sizeof (struct reg_64) / sizeof (int);
1440Sstevel@tonic-gate 		n = len / n;
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate 		/*
1470Sstevel@tonic-gate 		 * We're allocating a buffer the size of the PROM's property,
1480Sstevel@tonic-gate 		 * but we're only using a smaller portion when we assign it
1490Sstevel@tonic-gate 		 * to a regspec.  We do this so that in the
1500Sstevel@tonic-gate 		 * impl_ddi_sunbus_removechild function, we will
1510Sstevel@tonic-gate 		 * always free the right amount of memory.
1520Sstevel@tonic-gate 		 */
1530Sstevel@tonic-gate 		irp = rp = (struct regspec *)reg_prop;
1540Sstevel@tonic-gate 		r64_rp = (struct reg_64 *)pd->par_reg;
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate 		for (j = 0; j < n; ++j, ++rp, ++r64_rp) {
1570Sstevel@tonic-gate 			ASSERT(r64_rp->size_hi == 0);
1580Sstevel@tonic-gate 			rp->regspec_bustype = r64_rp->addr_hi;
1590Sstevel@tonic-gate 			rp->regspec_addr = r64_rp->addr_lo;
1600Sstevel@tonic-gate 			rp->regspec_size = r64_rp->size_lo;
1610Sstevel@tonic-gate 		}
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate 		ddi_prop_free((void *)pd->par_reg);
1640Sstevel@tonic-gate 		pd->par_nreg = n;
1650Sstevel@tonic-gate 		pd->par_reg = irp;
1660Sstevel@tonic-gate 	}
1670Sstevel@tonic-gate 	return (pd);
1680Sstevel@tonic-gate }
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate /*
1710Sstevel@tonic-gate  * Create a ddi_parent_private_data structure from the ddi properties of
1720Sstevel@tonic-gate  * the dev_info node.
1730Sstevel@tonic-gate  *
1740Sstevel@tonic-gate  * The "reg" is required if the driver wishes to create mappings on behalf
1750Sstevel@tonic-gate  * of the device. The "reg" property is assumed to be a list of at least
1760Sstevel@tonic-gate  * one triplet
1770Sstevel@tonic-gate  *
1780Sstevel@tonic-gate  *	<bustype, address, size>*1
1790Sstevel@tonic-gate  *
1800Sstevel@tonic-gate  * The "interrupt" property is no longer part of parent private data on
1810Sstevel@tonic-gate  * sun4u. The interrupt parent is may not be the device tree parent.
1820Sstevel@tonic-gate  *
1830Sstevel@tonic-gate  * The "ranges" property describes the mapping of child addresses to parent
1840Sstevel@tonic-gate  * addresses.
1850Sstevel@tonic-gate  *
1860Sstevel@tonic-gate  * N.B. struct rangespec is defined for the following default values:
1870Sstevel@tonic-gate  *			parent  child
1880Sstevel@tonic-gate  *	#address-cells	2	2
1890Sstevel@tonic-gate  *	#size-cells	1	1
1900Sstevel@tonic-gate  * This function doesn't deal with non-default cells and will not create
1910Sstevel@tonic-gate  * ranges in such cases.
1920Sstevel@tonic-gate  */
1930Sstevel@tonic-gate void
1940Sstevel@tonic-gate make_ddi_ppd(dev_info_t *child, struct ddi_parent_private_data **ppd)
1950Sstevel@tonic-gate {
1960Sstevel@tonic-gate 	struct ddi_parent_private_data *pdptr;
1970Sstevel@tonic-gate 	int *reg_prop, *rng_prop;
1980Sstevel@tonic-gate 	uint_t reg_len = 0, rng_len = 0;
1990Sstevel@tonic-gate 	dev_info_t *parent;
2000Sstevel@tonic-gate 	int parent_addr_cells, parent_size_cells;
2010Sstevel@tonic-gate 	int child_addr_cells, child_size_cells;
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate 	*ppd = pdptr = kmem_zalloc(sizeof (*pdptr), KM_SLEEP);
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate 	/*
2060Sstevel@tonic-gate 	 * root node has no parent private data, so *ppd should
2070Sstevel@tonic-gate 	 * be initialized for naming to work properly.
2080Sstevel@tonic-gate 	 */
2090Sstevel@tonic-gate 	if ((parent = ddi_get_parent(child)) == NULL)
2100Sstevel@tonic-gate 		return;
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate 	/*
2130Sstevel@tonic-gate 	 * Set reg field of parent data from "reg" property
2140Sstevel@tonic-gate 	 */
2150Sstevel@tonic-gate 	if ((get_prop_int_array(child, OBP_REG, &reg_prop, &reg_len)
2160Sstevel@tonic-gate 	    == DDI_PROP_SUCCESS) && (reg_len != 0)) {
2170Sstevel@tonic-gate 		pdptr->par_nreg = (int)(reg_len / sizeof (struct regspec));
2180Sstevel@tonic-gate 		pdptr->par_reg = (struct regspec *)reg_prop;
2190Sstevel@tonic-gate 	}
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate 	/*
2220Sstevel@tonic-gate 	 * "ranges" property ...
2230Sstevel@tonic-gate 	 *
2240Sstevel@tonic-gate 	 * This function does not handle cases where #address-cells != 2
2250Sstevel@tonic-gate 	 * and * min(parent, child) #size-cells != 1 (see bugid 4211124).
2260Sstevel@tonic-gate 	 *
2270Sstevel@tonic-gate 	 * Nexus drivers with such exceptions (e.g. pci ranges)
2280Sstevel@tonic-gate 	 * should either create a separate function for handling
2290Sstevel@tonic-gate 	 * ranges or not use parent private data to store ranges.
2300Sstevel@tonic-gate 	 */
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate 	/* root node has no ranges */
2330Sstevel@tonic-gate 	if ((parent = ddi_get_parent(child)) == NULL)
2340Sstevel@tonic-gate 		return;
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate 	child_addr_cells = ddi_prop_get_int(DDI_DEV_T_ANY, child,
2370Sstevel@tonic-gate 	    DDI_PROP_DONTPASS, "#address-cells", 2);
2380Sstevel@tonic-gate 	child_size_cells = ddi_prop_get_int(DDI_DEV_T_ANY, child,
2390Sstevel@tonic-gate 	    DDI_PROP_DONTPASS, "#size-cells", 1);
2400Sstevel@tonic-gate 	parent_addr_cells = ddi_prop_get_int(DDI_DEV_T_ANY, parent,
2410Sstevel@tonic-gate 	    DDI_PROP_DONTPASS, "#address-cells", 2);
2420Sstevel@tonic-gate 	parent_size_cells = ddi_prop_get_int(DDI_DEV_T_ANY, parent,
2430Sstevel@tonic-gate 	    DDI_PROP_DONTPASS, "#size-cells", 1);
2440Sstevel@tonic-gate 	if (child_addr_cells != 2 || parent_addr_cells != 2 ||
2450Sstevel@tonic-gate 	    (child_size_cells != 1 && parent_size_cells != 1)) {
2460Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_NOTE, "!ranges not made in parent data; "
2470Sstevel@tonic-gate 		    "#address-cells or #size-cells have non-default value"));
2480Sstevel@tonic-gate 		return;
2490Sstevel@tonic-gate 	}
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate 	if (get_prop_int_array(child, OBP_RANGES, &rng_prop, &rng_len)
2520Sstevel@tonic-gate 	    == DDI_PROP_SUCCESS) {
2530Sstevel@tonic-gate 		pdptr->par_nrng = rng_len / (int)(sizeof (struct rangespec));
2540Sstevel@tonic-gate 		pdptr->par_rng = (struct rangespec *)rng_prop;
2550Sstevel@tonic-gate 	}
2560Sstevel@tonic-gate }
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate /*
2590Sstevel@tonic-gate  * Free ddi_parent_private_data structure
2600Sstevel@tonic-gate  */
2610Sstevel@tonic-gate void
2620Sstevel@tonic-gate impl_free_ddi_ppd(dev_info_t *dip)
2630Sstevel@tonic-gate {
2640Sstevel@tonic-gate 	struct ddi_parent_private_data *pdptr = ddi_get_parent_data(dip);
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate 	if (pdptr == NULL)
2670Sstevel@tonic-gate 		return;
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate 	if (pdptr->par_nrng != 0)
2700Sstevel@tonic-gate 		ddi_prop_free((void *)pdptr->par_rng);
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate 	if (pdptr->par_nreg != 0)
2730Sstevel@tonic-gate 		ddi_prop_free((void *)pdptr->par_reg);
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate 	kmem_free(pdptr, sizeof (*pdptr));
2760Sstevel@tonic-gate 	ddi_set_parent_data(dip, NULL);
2770Sstevel@tonic-gate }
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate /*
2800Sstevel@tonic-gate  * Name a child of sun busses based on the reg spec.
2810Sstevel@tonic-gate  * Handles the following properties:
2820Sstevel@tonic-gate  *
2830Sstevel@tonic-gate  *	Property	value
2840Sstevel@tonic-gate  *	Name		type
2850Sstevel@tonic-gate  *
2860Sstevel@tonic-gate  *	reg		register spec
2870Sstevel@tonic-gate  *	interrupts	new (bus-oriented) interrupt spec
2880Sstevel@tonic-gate  *	ranges		range spec
2890Sstevel@tonic-gate  *
2900Sstevel@tonic-gate  * This may be called multiple times, independent of
2910Sstevel@tonic-gate  * initchild calls.
2920Sstevel@tonic-gate  */
2930Sstevel@tonic-gate static int
2940Sstevel@tonic-gate impl_sunbus_name_child(dev_info_t *child, char *name, int namelen)
2950Sstevel@tonic-gate {
2960Sstevel@tonic-gate 	struct ddi_parent_private_data *pdptr;
2970Sstevel@tonic-gate 	struct regspec *rp;
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate 	/*
3000Sstevel@tonic-gate 	 * Fill in parent-private data and this function returns to us
3010Sstevel@tonic-gate 	 * an indication if it used "registers" to fill in the data.
3020Sstevel@tonic-gate 	 */
3030Sstevel@tonic-gate 	if (ddi_get_parent_data(child) == NULL) {
3040Sstevel@tonic-gate 		make_ddi_ppd(child, &pdptr);
3050Sstevel@tonic-gate 		ddi_set_parent_data(child, pdptr);
3060Sstevel@tonic-gate 	}
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate 	/*
3090Sstevel@tonic-gate 	 * No reg property, return null string as address
3100Sstevel@tonic-gate 	 * (e.g. root node)
3110Sstevel@tonic-gate 	 */
3120Sstevel@tonic-gate 	name[0] = '\0';
3130Sstevel@tonic-gate 	if (sparc_pd_getnreg(child) == 0) {
3140Sstevel@tonic-gate 		return (DDI_SUCCESS);
3150Sstevel@tonic-gate 	}
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate 	rp = sparc_pd_getreg(child, 0);
3180Sstevel@tonic-gate 	(void) snprintf(name, namelen, "%x,%x",
3190Sstevel@tonic-gate 	    rp->regspec_bustype, rp->regspec_addr);
3200Sstevel@tonic-gate 	return (DDI_SUCCESS);
3210Sstevel@tonic-gate }
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate 
3240Sstevel@tonic-gate /*
3250Sstevel@tonic-gate  * Called from the bus_ctl op of some drivers.
3260Sstevel@tonic-gate  * to implement the DDI_CTLOPS_INITCHILD operation.
3270Sstevel@tonic-gate  *
3280Sstevel@tonic-gate  * NEW drivers should NOT use this function, but should declare
3290Sstevel@tonic-gate  * there own initchild/uninitchild handlers. (This function assumes
3300Sstevel@tonic-gate  * the layout of the parent private data and the format of "reg",
3310Sstevel@tonic-gate  * "ranges", "interrupts" properties and that #address-cells and
3320Sstevel@tonic-gate  * #size-cells of the parent bus are defined to be default values.)
3330Sstevel@tonic-gate  */
3340Sstevel@tonic-gate int
3350Sstevel@tonic-gate impl_ddi_sunbus_initchild(dev_info_t *child)
3360Sstevel@tonic-gate {
3370Sstevel@tonic-gate 	char name[MAXNAMELEN];
3380Sstevel@tonic-gate 
3390Sstevel@tonic-gate 	(void) impl_sunbus_name_child(child, name, MAXNAMELEN);
3400Sstevel@tonic-gate 	ddi_set_name_addr(child, name);
3410Sstevel@tonic-gate 
3420Sstevel@tonic-gate 	/*
3430Sstevel@tonic-gate 	 * Try to merge .conf node. If successful, return failure to
3440Sstevel@tonic-gate 	 * remove this child.
3450Sstevel@tonic-gate 	 */
3460Sstevel@tonic-gate 	if ((ndi_dev_is_persistent_node(child) == 0) &&
3470Sstevel@tonic-gate 	    (ndi_merge_node(child, impl_sunbus_name_child) == DDI_SUCCESS)) {
3480Sstevel@tonic-gate 		impl_ddi_sunbus_removechild(child);
3490Sstevel@tonic-gate 		return (DDI_FAILURE);
3500Sstevel@tonic-gate 	}
3510Sstevel@tonic-gate 	return (DDI_SUCCESS);
3520Sstevel@tonic-gate }
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate /*
3550Sstevel@tonic-gate  * A better name for this function would be impl_ddi_sunbus_uninitchild()
3560Sstevel@tonic-gate  * It does not remove the child, it uninitializes it, reclaiming the
3570Sstevel@tonic-gate  * resources taken by impl_ddi_sunbus_initchild.
3580Sstevel@tonic-gate  */
3590Sstevel@tonic-gate void
3600Sstevel@tonic-gate impl_ddi_sunbus_removechild(dev_info_t *dip)
3610Sstevel@tonic-gate {
3620Sstevel@tonic-gate 	impl_free_ddi_ppd(dip);
3630Sstevel@tonic-gate 	ddi_set_name_addr(dip, NULL);
3640Sstevel@tonic-gate 	/*
3650Sstevel@tonic-gate 	 * Strip the node to properly convert it back to prototype form
3660Sstevel@tonic-gate 	 */
3670Sstevel@tonic-gate 	impl_rem_dev_props(dip);
3680Sstevel@tonic-gate }
3690Sstevel@tonic-gate 
3700Sstevel@tonic-gate /*
3710Sstevel@tonic-gate  * SECTION: DDI Interrupt
3720Sstevel@tonic-gate  */
3730Sstevel@tonic-gate 
3740Sstevel@tonic-gate void
3750Sstevel@tonic-gate cells_1275_copy(prop_1275_cell_t *from, prop_1275_cell_t *to, int32_t len)
3760Sstevel@tonic-gate {
3770Sstevel@tonic-gate 	int i;
3780Sstevel@tonic-gate 	for (i = 0; i < len; i++)
3790Sstevel@tonic-gate 		*to = *from;
3800Sstevel@tonic-gate }
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate prop_1275_cell_t *
3830Sstevel@tonic-gate cells_1275_cmp(prop_1275_cell_t *cell1, prop_1275_cell_t *cell2, int32_t len)
3840Sstevel@tonic-gate {
3850Sstevel@tonic-gate 	prop_1275_cell_t *match_cell = 0;
3860Sstevel@tonic-gate 	int32_t i;
3870Sstevel@tonic-gate 
3880Sstevel@tonic-gate 	for (i = 0; i < len; i++)
3890Sstevel@tonic-gate 		if (cell1[i] != cell2[i]) {
3900Sstevel@tonic-gate 			match_cell = &cell1[i];
3910Sstevel@tonic-gate 			break;
3920Sstevel@tonic-gate 		}
3930Sstevel@tonic-gate 
3940Sstevel@tonic-gate 	return (match_cell);
3950Sstevel@tonic-gate }
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate /*
398693Sgovinda  * get_intr_parent() is a generic routine that process a 1275 interrupt
399693Sgovinda  * map (imap) property.  This function returns a dev_info_t structure
400693Sgovinda  * which claims ownership of the interrupt domain.
401693Sgovinda  * It also returns the new interrupt translation within this new domain.
402693Sgovinda  * If an interrupt-parent or interrupt-map property are not found,
403693Sgovinda  * then we fallback to using the device tree's parent.
404693Sgovinda  *
405693Sgovinda  * imap entry format:
406693Sgovinda  * <reg>,<interrupt>,<phandle>,<translated interrupt>
407693Sgovinda  * reg - The register specification in the interrupts domain
408693Sgovinda  * interrupt - The interrupt specification
409693Sgovinda  * phandle - PROM handle of the device that owns the xlated interrupt domain
410693Sgovinda  * translated interrupt - interrupt specifier in the parents domain
411693Sgovinda  * note: <reg>,<interrupt> - The reg and interrupt can be combined to create
412693Sgovinda  *	a unique entry called a unit interrupt specifier.
413693Sgovinda  *
414693Sgovinda  * Here's the processing steps:
415693Sgovinda  * step1 - If the interrupt-parent property exists, create the ispec and
416693Sgovinda  *	return the dip of the interrupt parent.
417693Sgovinda  * step2 - Extract the interrupt-map property and the interrupt-map-mask
418693Sgovinda  *	If these don't exist, just return the device tree parent.
419693Sgovinda  * step3 - build up the unit interrupt specifier to match against the
420693Sgovinda  *	interrupt map property
421693Sgovinda  * step4 - Scan the interrupt-map property until a match is found
422693Sgovinda  * step4a - Extract the interrupt parent
423693Sgovinda  * step4b - Compare the unit interrupt specifier
4240Sstevel@tonic-gate  */
425693Sgovinda dev_info_t *
426693Sgovinda get_intr_parent(dev_info_t *pdip, dev_info_t *dip, ddi_intr_handle_impl_t *hdlp)
4270Sstevel@tonic-gate {
428693Sgovinda 	prop_1275_cell_t *imap, *imap_mask, *scan, *reg_p, *match_req;
429693Sgovinda 	int32_t imap_sz, imap_cells, imap_scan_cells, imap_mask_sz,
430693Sgovinda 	    addr_cells, intr_cells, reg_len, i, j;
431693Sgovinda 	int32_t match_found = 0;
432693Sgovinda 	dev_info_t *intr_parent_dip = NULL;
433693Sgovinda 	uint32_t *intr = &hdlp->ih_vector;
434693Sgovinda 	uint32_t nodeid;
435693Sgovinda #ifdef DEBUG
436693Sgovinda 	static int debug = 0;
437693Sgovinda #endif
4380Sstevel@tonic-gate 
439693Sgovinda 	/*
440693Sgovinda 	 * step1
441693Sgovinda 	 * If we have an interrupt-parent property, this property represents
442693Sgovinda 	 * the nodeid of our interrupt parent.
443693Sgovinda 	 */
444693Sgovinda 	if ((nodeid = ddi_getprop(DDI_DEV_T_ANY, dip, 0,
445693Sgovinda 	    "interrupt-parent", -1)) != -1) {
446693Sgovinda 		intr_parent_dip = e_ddi_nodeid_to_dip(nodeid);
447693Sgovinda 		ASSERT(intr_parent_dip);
4480Sstevel@tonic-gate 
4490Sstevel@tonic-gate 		/*
450693Sgovinda 		 * Attach the interrupt parent.
451693Sgovinda 		 *
452693Sgovinda 		 * N.B. e_ddi_nodeid_to_dip() isn't safe under DR.
453693Sgovinda 		 *	Also, interrupt parent isn't held. This needs
454693Sgovinda 		 *	to be revisited if DR-capable platforms implement
455693Sgovinda 		 *	interrupt redirection.
456693Sgovinda 		 */
457693Sgovinda 		if (i_ddi_attach_node_hierarchy(intr_parent_dip)
458693Sgovinda 		    != DDI_SUCCESS) {
459693Sgovinda 			ndi_rele_devi(intr_parent_dip);
460693Sgovinda 			return (NULL);
461693Sgovinda 		}
462693Sgovinda 
463693Sgovinda 		return (intr_parent_dip);
464693Sgovinda 	}
465693Sgovinda 
466693Sgovinda 	/*
467693Sgovinda 	 * step2
468693Sgovinda 	 * Get interrupt map structure from PROM property
469693Sgovinda 	 */
470693Sgovinda 	if (ddi_getlongprop(DDI_DEV_T_ANY, pdip, DDI_PROP_DONTPASS,
471693Sgovinda 	    "interrupt-map", (caddr_t)&imap, &imap_sz)
472693Sgovinda 	    != DDI_PROP_SUCCESS) {
473693Sgovinda 		/*
474693Sgovinda 		 * If we don't have an imap property, default to using the
475693Sgovinda 		 * device tree.
4760Sstevel@tonic-gate 		 */
477693Sgovinda 
478693Sgovinda 		ndi_hold_devi(pdip);
479693Sgovinda 		return (pdip);
480693Sgovinda 	}
481693Sgovinda 
482693Sgovinda 	/* Get the interrupt mask property */
483693Sgovinda 	if (ddi_getlongprop(DDI_DEV_T_ANY, pdip, DDI_PROP_DONTPASS,
484693Sgovinda 	    "interrupt-map-mask", (caddr_t)&imap_mask, &imap_mask_sz)
485693Sgovinda 	    != DDI_PROP_SUCCESS) {
486693Sgovinda 		/*
487693Sgovinda 		 * If we don't find this property, we have to fail the request
488693Sgovinda 		 * because the 1275 imap property wasn't defined correctly.
489693Sgovinda 		 */
490693Sgovinda 		ASSERT(intr_parent_dip == NULL);
491693Sgovinda 		goto exit2;
492693Sgovinda 	}
493693Sgovinda 
494693Sgovinda 	/* Get the address cell size */
495693Sgovinda 	addr_cells = ddi_getprop(DDI_DEV_T_ANY, pdip, 0,
496693Sgovinda 	    "#address-cells", 2);
497693Sgovinda 
498693Sgovinda 	/* Get the interrupts cell size */
499693Sgovinda 	intr_cells = ddi_getprop(DDI_DEV_T_ANY, pdip, 0,
500693Sgovinda 	    "#interrupt-cells", 1);
501693Sgovinda 
502693Sgovinda 	/*
503693Sgovinda 	 * step3
504693Sgovinda 	 * Now lets build up the unit interrupt specifier e.g. reg,intr
505693Sgovinda 	 * and apply the imap mask.  match_req will hold this when we're
506693Sgovinda 	 * through.
507693Sgovinda 	 */
508693Sgovinda 	if (ddi_getlongprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, "reg",
509693Sgovinda 	    (caddr_t)&reg_p, &reg_len) != DDI_SUCCESS) {
510693Sgovinda 		ASSERT(intr_parent_dip == NULL);
511693Sgovinda 		goto exit3;
5120Sstevel@tonic-gate 	}
5130Sstevel@tonic-gate 
514693Sgovinda 	match_req = kmem_alloc(CELLS_1275_TO_BYTES(addr_cells) +
515693Sgovinda 	    CELLS_1275_TO_BYTES(intr_cells), KM_SLEEP);
516693Sgovinda 
517693Sgovinda 	for (i = 0; i < addr_cells; i++)
518693Sgovinda 		match_req[i] = (reg_p[i] & imap_mask[i]);
519693Sgovinda 
520693Sgovinda 	for (j = 0; j < intr_cells; i++, j++)
521693Sgovinda 		match_req[i] = (intr[j] & imap_mask[i]);
522693Sgovinda 
523693Sgovinda 	/* Calculate the imap size in cells */
524693Sgovinda 	imap_cells = BYTES_TO_1275_CELLS(imap_sz);
525693Sgovinda 
526693Sgovinda #ifdef DEBUG
527693Sgovinda 	if (debug)
528693Sgovinda 		prom_printf("reg cell size 0x%x, intr cell size 0x%x, "
529693Sgovinda 		    "match_request 0x%p, imap 0x%p\n", addr_cells, intr_cells,
530693Sgovinda 		    match_req, imap);
531693Sgovinda #endif
532693Sgovinda 
533693Sgovinda 	/*
534693Sgovinda 	 * Scan the imap property looking for a match of the interrupt unit
535693Sgovinda 	 * specifier.  This loop is rather complex since the data within the
536693Sgovinda 	 * imap property may vary in size.
537693Sgovinda 	 */
538693Sgovinda 	for (scan = imap, imap_scan_cells = i = 0;
539693Sgovinda 	    imap_scan_cells < imap_cells; scan += i, imap_scan_cells += i) {
540693Sgovinda 		int new_intr_cells;
541693Sgovinda 
542693Sgovinda 		/* Set the index to the nodeid field */
543693Sgovinda 		i = addr_cells + intr_cells;
544693Sgovinda 
545693Sgovinda 		/*
546693Sgovinda 		 * step4a
547693Sgovinda 		 * Translate the nodeid field to a dip
548693Sgovinda 		 */
549693Sgovinda 		ASSERT(intr_parent_dip == NULL);
550693Sgovinda 		intr_parent_dip = e_ddi_nodeid_to_dip((uint_t)scan[i++]);
551693Sgovinda 
552693Sgovinda 		ASSERT(intr_parent_dip != 0);
553693Sgovinda #ifdef DEBUG
554693Sgovinda 		if (debug)
555693Sgovinda 			prom_printf("scan 0x%p\n", scan);
556693Sgovinda #endif
557693Sgovinda 		/*
558693Sgovinda 		 * The tmp_dip describes the new domain, get it's interrupt
559693Sgovinda 		 * cell size
560693Sgovinda 		 */
561693Sgovinda 		new_intr_cells = ddi_getprop(DDI_DEV_T_ANY, intr_parent_dip, 0,
562693Sgovinda 		    "#interrupts-cells", 1);
5630Sstevel@tonic-gate 
564693Sgovinda 		/*
565693Sgovinda 		 * step4b
566693Sgovinda 		 * See if we have a match on the interrupt unit specifier
567693Sgovinda 		 */
568693Sgovinda 		if (cells_1275_cmp(match_req, scan, addr_cells + intr_cells)
569693Sgovinda 		    == 0) {
570693Sgovinda 			uint32_t *intr;
571693Sgovinda 
572693Sgovinda 			match_found = 1;
573693Sgovinda 
574693Sgovinda 			/*
575693Sgovinda 			 * If we have an imap parent whose not in our device
576693Sgovinda 			 * tree path, we need to hold and install that driver.
577693Sgovinda 			 */
578693Sgovinda 			if (i_ddi_attach_node_hierarchy(intr_parent_dip)
579693Sgovinda 			    != DDI_SUCCESS) {
580693Sgovinda 				ndi_rele_devi(intr_parent_dip);
581693Sgovinda 				intr_parent_dip = (dev_info_t *)NULL;
582693Sgovinda 				goto exit4;
583693Sgovinda 			}
5840Sstevel@tonic-gate 
585693Sgovinda 			/*
586693Sgovinda 			 * We need to handcraft an ispec along with a bus
587693Sgovinda 			 * interrupt value, so we can dup it into our
588693Sgovinda 			 * standard ispec structure.
589693Sgovinda 			 */
590693Sgovinda 			/* Extract the translated interrupt information */
591693Sgovinda 			intr = kmem_alloc(
592693Sgovinda 			    CELLS_1275_TO_BYTES(new_intr_cells), KM_SLEEP);
593693Sgovinda 
594693Sgovinda 			for (j = 0; j < new_intr_cells; j++, i++)
595693Sgovinda 				intr[j] = scan[i];
596693Sgovinda 
597693Sgovinda 			cells_1275_copy(intr, &hdlp->ih_vector, new_intr_cells);
5980Sstevel@tonic-gate 
599693Sgovinda 			kmem_free(intr, CELLS_1275_TO_BYTES(new_intr_cells));
600693Sgovinda 
601693Sgovinda #ifdef DEBUG
602693Sgovinda 			if (debug)
603693Sgovinda 				prom_printf("dip 0x%p\n", intr_parent_dip);
604693Sgovinda #endif
605693Sgovinda 			break;
606693Sgovinda 		} else {
607693Sgovinda #ifdef DEBUG
608693Sgovinda 			if (debug)
609693Sgovinda 				prom_printf("dip 0x%p\n", intr_parent_dip);
610693Sgovinda #endif
611693Sgovinda 			ndi_rele_devi(intr_parent_dip);
612693Sgovinda 			intr_parent_dip = NULL;
613693Sgovinda 			i += new_intr_cells;
6140Sstevel@tonic-gate 		}
6150Sstevel@tonic-gate 	}
6160Sstevel@tonic-gate 
617693Sgovinda 	/*
618693Sgovinda 	 * If we haven't found our interrupt parent at this point, fallback
619693Sgovinda 	 * to using the device tree.
620693Sgovinda 	 */
621693Sgovinda 	if (!match_found) {
622693Sgovinda 		ndi_hold_devi(pdip);
623693Sgovinda 		ASSERT(intr_parent_dip == NULL);
624693Sgovinda 		intr_parent_dip = pdip;
625693Sgovinda 	}
626693Sgovinda 
627693Sgovinda 	ASSERT(intr_parent_dip != NULL);
628693Sgovinda 
629693Sgovinda exit4:
630693Sgovinda 	kmem_free(reg_p, reg_len);
631693Sgovinda 	kmem_free(match_req, CELLS_1275_TO_BYTES(addr_cells) +
632693Sgovinda 	    CELLS_1275_TO_BYTES(intr_cells));
633693Sgovinda 
634693Sgovinda exit3:
635693Sgovinda 	kmem_free(imap_mask, imap_mask_sz);
636693Sgovinda 
637693Sgovinda exit2:
638693Sgovinda 	kmem_free(imap, imap_sz);
639693Sgovinda 
640693Sgovinda 	return (intr_parent_dip);
6410Sstevel@tonic-gate }
6420Sstevel@tonic-gate 
6430Sstevel@tonic-gate /*
6440Sstevel@tonic-gate  * process_intr_ops:
6450Sstevel@tonic-gate  *
6460Sstevel@tonic-gate  * Process the interrupt op via the interrupt parent.
6470Sstevel@tonic-gate  */
6480Sstevel@tonic-gate int
6490Sstevel@tonic-gate process_intr_ops(dev_info_t *pdip, dev_info_t *rdip, ddi_intr_op_t op,
6500Sstevel@tonic-gate     ddi_intr_handle_impl_t *hdlp, void *result)
6510Sstevel@tonic-gate {
6520Sstevel@tonic-gate 	int		ret = DDI_FAILURE;
6530Sstevel@tonic-gate 
6540Sstevel@tonic-gate 	if (NEXUS_HAS_INTR_OP(pdip)) {
6550Sstevel@tonic-gate 		ret = (*(DEVI(pdip)->devi_ops->devo_bus_ops->
6560Sstevel@tonic-gate 		    bus_intr_op)) (pdip, rdip, op, hdlp, result);
6570Sstevel@tonic-gate 	} else {
6580Sstevel@tonic-gate 		cmn_err(CE_WARN, "Failed to process interrupt "
6590Sstevel@tonic-gate 		    "for %s%d due to down-rev nexus driver %s%d",
6600Sstevel@tonic-gate 		    ddi_get_name(rdip), ddi_get_instance(rdip),
6610Sstevel@tonic-gate 		    ddi_get_name(pdip), ddi_get_instance(pdip));
6620Sstevel@tonic-gate 	}
6630Sstevel@tonic-gate 
6640Sstevel@tonic-gate 	return (ret);
6650Sstevel@tonic-gate }
6660Sstevel@tonic-gate 
667693Sgovinda /*ARGSUSED*/
668693Sgovinda uint_t
669693Sgovinda softlevel1(caddr_t arg)
670693Sgovinda {
671693Sgovinda 	softint();
672693Sgovinda 	return (1);
673693Sgovinda }
674693Sgovinda 
675693Sgovinda /*
676693Sgovinda  * indirection table, to save us some large switch statements
677693Sgovinda  * NOTE: This must agree with "INTLEVEL_foo" constants in
678693Sgovinda  *	<sys/avintr.h>
679693Sgovinda  */
680693Sgovinda struct autovec *const vectorlist[] = { 0 };
681693Sgovinda 
682693Sgovinda /*
683693Sgovinda  * This value is exported here for the functions in avintr.c
684693Sgovinda  */
685693Sgovinda const uint_t maxautovec = (sizeof (vectorlist) / sizeof (vectorlist[0]));
686693Sgovinda 
687693Sgovinda /*
688693Sgovinda  * Check for machine specific interrupt levels which cannot be reassigned by
689693Sgovinda  * settrap(), sun4u version.
690693Sgovinda  *
691693Sgovinda  * sun4u does not support V8 SPARC "fast trap" handlers.
692693Sgovinda  */
693693Sgovinda /*ARGSUSED*/
694693Sgovinda int
695693Sgovinda exclude_settrap(int lvl)
696693Sgovinda {
697693Sgovinda 	return (1);
698693Sgovinda }
699693Sgovinda 
700693Sgovinda /*
701693Sgovinda  * Check for machine specific interrupt levels which cannot have interrupt
702693Sgovinda  * handlers added. We allow levels 1 through 15; level 0 is nonsense.
703693Sgovinda  */
704693Sgovinda /*ARGSUSED*/
705693Sgovinda int
706693Sgovinda exclude_level(int lvl)
707693Sgovinda {
708693Sgovinda 	return ((lvl < 1) || (lvl > 15));
709693Sgovinda }
710693Sgovinda 
711693Sgovinda /*
712693Sgovinda  * Wrapper functions used by New DDI interrupt framework.
713693Sgovinda  */
714693Sgovinda 
715693Sgovinda /*
716693Sgovinda  * i_ddi_intr_ops:
717693Sgovinda  */
718693Sgovinda int
719693Sgovinda i_ddi_intr_ops(dev_info_t *dip, dev_info_t *rdip, ddi_intr_op_t op,
720693Sgovinda     ddi_intr_handle_impl_t *hdlp, void *result)
721693Sgovinda {
722693Sgovinda 	dev_info_t	*pdip = ddi_get_parent(dip);
723693Sgovinda 	int		ret = DDI_FAILURE;
724693Sgovinda 
725693Sgovinda 	/*
726693Sgovinda 	 * The following check is required to address
727693Sgovinda 	 * one of the test case of ADDI test suite.
728693Sgovinda 	 */
729693Sgovinda 	if (pdip == NULL)
730693Sgovinda 		return (DDI_FAILURE);
731693Sgovinda 
732693Sgovinda 	if (hdlp->ih_type != DDI_INTR_TYPE_FIXED)
733693Sgovinda 		return (process_intr_ops(pdip, rdip, op, hdlp, result));
734693Sgovinda 
735693Sgovinda 	if (hdlp->ih_vector == 0)
736693Sgovinda 		hdlp->ih_vector = i_ddi_get_inum(rdip, hdlp->ih_inum);
737693Sgovinda 
738693Sgovinda 	if (hdlp->ih_pri == 0)
739693Sgovinda 		hdlp->ih_pri = i_ddi_get_intr_pri(rdip, hdlp->ih_inum);
740693Sgovinda 
741693Sgovinda 	switch (op) {
742693Sgovinda 	case DDI_INTROP_ADDISR:
743693Sgovinda 	case DDI_INTROP_REMISR:
744693Sgovinda 	case DDI_INTROP_ENABLE:
745693Sgovinda 	case DDI_INTROP_DISABLE:
746693Sgovinda 	case DDI_INTROP_BLOCKENABLE:
747693Sgovinda 	case DDI_INTROP_BLOCKDISABLE:
748693Sgovinda 		/*
749693Sgovinda 		 * Try and determine our parent and possibly an interrupt
750693Sgovinda 		 * translation. intr parent dip returned held
751693Sgovinda 		 */
752693Sgovinda 		if ((pdip = get_intr_parent(pdip, dip, hdlp)) == NULL)
753693Sgovinda 			goto done;
754693Sgovinda 	}
755693Sgovinda 
756693Sgovinda 	ret = process_intr_ops(pdip, rdip, op, hdlp, result);
757693Sgovinda 
758693Sgovinda done:
759693Sgovinda 	switch (op) {
760693Sgovinda 	case DDI_INTROP_ADDISR:
761693Sgovinda 	case DDI_INTROP_REMISR:
762693Sgovinda 	case DDI_INTROP_ENABLE:
763693Sgovinda 	case DDI_INTROP_DISABLE:
764693Sgovinda 	case DDI_INTROP_BLOCKENABLE:
765693Sgovinda 	case DDI_INTROP_BLOCKDISABLE:
766693Sgovinda 		/* Release hold acquired in get_intr_parent() */
767693Sgovinda 		if (pdip)
768693Sgovinda 			ndi_rele_devi(pdip);
769693Sgovinda 	}
770693Sgovinda 
771693Sgovinda 	hdlp->ih_vector = 0;
772693Sgovinda 
773693Sgovinda 	return (ret);
774693Sgovinda }
775693Sgovinda 
7760Sstevel@tonic-gate /*
7770Sstevel@tonic-gate  * i_ddi_add_ivintr:
7780Sstevel@tonic-gate  */
7790Sstevel@tonic-gate /*ARGSUSED*/
7800Sstevel@tonic-gate int
7810Sstevel@tonic-gate i_ddi_add_ivintr(ddi_intr_handle_impl_t *hdlp)
7820Sstevel@tonic-gate {
7830Sstevel@tonic-gate 	/* Sanity check the entry we're about to add */
7840Sstevel@tonic-gate 	if (GET_IVINTR(hdlp->ih_vector)) {
7850Sstevel@tonic-gate 		cmn_err(CE_WARN, "mondo 0x%x in use", hdlp->ih_vector);
7860Sstevel@tonic-gate 		return (DDI_FAILURE);
7870Sstevel@tonic-gate 	}
7880Sstevel@tonic-gate 
7890Sstevel@tonic-gate 	/*
7900Sstevel@tonic-gate 	 * If the PIL was set and is valid use it, otherwise
7910Sstevel@tonic-gate 	 * default it to 1
7920Sstevel@tonic-gate 	 */
7930Sstevel@tonic-gate 	if ((hdlp->ih_pri < 1) || (hdlp->ih_pri > PIL_MAX))
7940Sstevel@tonic-gate 		hdlp->ih_pri = 1;
7950Sstevel@tonic-gate 
7960Sstevel@tonic-gate 	VERIFY(add_ivintr(hdlp->ih_vector, hdlp->ih_pri,
7970Sstevel@tonic-gate 	    (intrfunc)hdlp->ih_cb_func, hdlp->ih_cb_arg1, NULL) == 0);
7980Sstevel@tonic-gate 
7990Sstevel@tonic-gate 	return (DDI_SUCCESS);
8000Sstevel@tonic-gate }
8010Sstevel@tonic-gate 
8020Sstevel@tonic-gate /*
8030Sstevel@tonic-gate  * i_ddi_rem_ivintr:
8040Sstevel@tonic-gate  */
8050Sstevel@tonic-gate /*ARGSUSED*/
8060Sstevel@tonic-gate void
8070Sstevel@tonic-gate i_ddi_rem_ivintr(ddi_intr_handle_impl_t *hdlp)
8080Sstevel@tonic-gate {
8090Sstevel@tonic-gate 	rem_ivintr(hdlp->ih_vector, NULL);
8100Sstevel@tonic-gate }
8110Sstevel@tonic-gate 
8120Sstevel@tonic-gate /*
813693Sgovinda  * i_ddi_get_inum - Get the interrupt number property from the
814693Sgovinda  * specified device. Note that this function is called only for
815693Sgovinda  * the FIXED interrupt type.
816693Sgovinda  */
817693Sgovinda uint32_t
818693Sgovinda i_ddi_get_inum(dev_info_t *dip, uint_t inumber)
819693Sgovinda {
820693Sgovinda 	int32_t			intrlen, intr_cells, max_intrs;
821693Sgovinda 	prop_1275_cell_t	*ip, intr_sz;
822693Sgovinda 	uint32_t		intr = 0;
823693Sgovinda 
824693Sgovinda 	if (ddi_getlongprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS |
825693Sgovinda 	    DDI_PROP_CANSLEEP,
826693Sgovinda 	    "interrupts", (caddr_t)&ip, &intrlen) == DDI_SUCCESS) {
827693Sgovinda 
828693Sgovinda 		intr_cells = ddi_getprop(DDI_DEV_T_ANY, dip, 0,
829693Sgovinda 		    "#interrupt-cells", 1);
830693Sgovinda 
831693Sgovinda 		/* adjust for number of bytes */
832693Sgovinda 		intr_sz = CELLS_1275_TO_BYTES(intr_cells);
833693Sgovinda 
834693Sgovinda 		/* Calculate the number of interrupts */
835693Sgovinda 		max_intrs = intrlen / intr_sz;
836693Sgovinda 
837693Sgovinda 		if (inumber < max_intrs) {
838693Sgovinda 			prop_1275_cell_t *intrp = ip;
839693Sgovinda 
840693Sgovinda 			/* Index into interrupt property */
841693Sgovinda 			intrp += (inumber * intr_cells);
842693Sgovinda 
843693Sgovinda 			cells_1275_copy(intrp, &intr, intr_cells);
844693Sgovinda 		}
845693Sgovinda 
846693Sgovinda 		kmem_free(ip, intrlen);
847693Sgovinda 	}
848693Sgovinda 
849693Sgovinda 	return (intr);
850693Sgovinda }
851693Sgovinda 
852693Sgovinda /*
853693Sgovinda  * i_ddi_get_intr_pri - Get the interrupt-priorities property from
854693Sgovinda  * the specified device. Note that this function is called only for
855693Sgovinda  * the FIXED interrupt type.
856693Sgovinda  */
857693Sgovinda uint32_t
858693Sgovinda i_ddi_get_intr_pri(dev_info_t *dip, uint_t inumber)
859693Sgovinda {
860693Sgovinda 	uint32_t	*intr_prio_p;
861693Sgovinda 	uint32_t	pri = 0;
862693Sgovinda 	int32_t		i;
863693Sgovinda 
864693Sgovinda 	/*
865693Sgovinda 	 * Use the "interrupt-priorities" property to determine the
866693Sgovinda 	 * the pil/ipl for the interrupt handler.
867693Sgovinda 	 */
868693Sgovinda 	if (ddi_getlongprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
869693Sgovinda 	    "interrupt-priorities", (caddr_t)&intr_prio_p,
870693Sgovinda 	    &i) == DDI_SUCCESS) {
871693Sgovinda 		if (inumber < (i / sizeof (int32_t)))
872693Sgovinda 			pri = intr_prio_p[inumber];
873693Sgovinda 		kmem_free(intr_prio_p, i);
874693Sgovinda 	}
875693Sgovinda 
876693Sgovinda 	return (pri);
877693Sgovinda }
878693Sgovinda 
879693Sgovinda int
880693Sgovinda i_ddi_get_nintrs(dev_info_t *dip)
881693Sgovinda {
882693Sgovinda 	int32_t intrlen;
883693Sgovinda 	prop_1275_cell_t intr_sz;
884693Sgovinda 	prop_1275_cell_t *ip;
885693Sgovinda 	int32_t ret = 0;
886693Sgovinda 
887693Sgovinda 	if (ddi_getlongprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS |
888693Sgovinda 	    DDI_PROP_CANSLEEP,
889693Sgovinda 	    "interrupts", (caddr_t)&ip, &intrlen) == DDI_SUCCESS) {
890693Sgovinda 
891693Sgovinda 		intr_sz = ddi_getprop(DDI_DEV_T_ANY, dip, 0,
892693Sgovinda 		    "#interrupt-cells", 1);
893693Sgovinda 		/* adjust for number of bytes */
894693Sgovinda 		intr_sz = CELLS_1275_TO_BYTES(intr_sz);
895693Sgovinda 
896693Sgovinda 		ret = intrlen / intr_sz;
897693Sgovinda 
898693Sgovinda 		kmem_free(ip, intrlen);
899693Sgovinda 	}
900693Sgovinda 
901693Sgovinda 	return (ret);
902693Sgovinda }
903693Sgovinda 
904693Sgovinda /*
9050Sstevel@tonic-gate  * i_ddi_add_softint - allocate and add a soft interrupt to the system
9060Sstevel@tonic-gate  */
9070Sstevel@tonic-gate int
9080Sstevel@tonic-gate i_ddi_add_softint(ddi_softint_hdl_impl_t *hdlp)
9090Sstevel@tonic-gate {
9100Sstevel@tonic-gate 	uint_t		rval;
9110Sstevel@tonic-gate 
912568Seota 	if ((rval = add_softintr(hdlp->ih_pri, hdlp->ih_cb_func,
913568Seota 	    hdlp->ih_cb_arg1)) == 0) {
9140Sstevel@tonic-gate 
9150Sstevel@tonic-gate 		return (DDI_FAILURE);
9160Sstevel@tonic-gate 	}
9170Sstevel@tonic-gate 
918568Seota 	/* use uintptr_t to suppress the gcc warning */
919568Seota 	hdlp->ih_private = (void *)(uintptr_t)rval;
9200Sstevel@tonic-gate 
9210Sstevel@tonic-gate 	return (DDI_SUCCESS);
9220Sstevel@tonic-gate }
9230Sstevel@tonic-gate 
9240Sstevel@tonic-gate void
9250Sstevel@tonic-gate i_ddi_remove_softint(ddi_softint_hdl_impl_t *hdlp)
9260Sstevel@tonic-gate {
9270Sstevel@tonic-gate 	uint_t		intr_id;
9280Sstevel@tonic-gate 
9290Sstevel@tonic-gate 	/* disable */
9300Sstevel@tonic-gate 	ASSERT(hdlp->ih_private != NULL);
931693Sgovinda 
932568Seota 	/* use uintptr_t to suppress the gcc warning */
933568Seota 	intr_id = (uint_t)(uintptr_t)hdlp->ih_private;
934693Sgovinda 
9350Sstevel@tonic-gate 	rem_softintr(intr_id);
9360Sstevel@tonic-gate 	hdlp->ih_private = NULL;
9370Sstevel@tonic-gate }
9380Sstevel@tonic-gate 
9390Sstevel@tonic-gate int
940278Sgovinda i_ddi_trigger_softint(ddi_softint_hdl_impl_t *hdlp, void *arg2)
9410Sstevel@tonic-gate {
9420Sstevel@tonic-gate 	uint_t		intr_id;
9430Sstevel@tonic-gate 	int		ret;
9440Sstevel@tonic-gate 
9450Sstevel@tonic-gate 	ASSERT(hdlp != NULL);
9460Sstevel@tonic-gate 	ASSERT(hdlp->ih_private != NULL);
9470Sstevel@tonic-gate 
9481009Smathue 	/* use uintptr_t to suppress the gcc warning */
9491009Smathue 	intr_id = (uint_t)(uintptr_t)hdlp->ih_private;
9500Sstevel@tonic-gate 
9510Sstevel@tonic-gate 	/* update the vector table for the 2nd arg */
952278Sgovinda 	ret = update_softint_arg2(intr_id, arg2);
9530Sstevel@tonic-gate 	if (ret == DDI_SUCCESS)
9540Sstevel@tonic-gate 		setsoftint(intr_id);
9550Sstevel@tonic-gate 
9560Sstevel@tonic-gate 	return (ret);
9570Sstevel@tonic-gate }
9580Sstevel@tonic-gate 
9590Sstevel@tonic-gate /* ARGSUSED */
9600Sstevel@tonic-gate int
9610Sstevel@tonic-gate i_ddi_set_softint_pri(ddi_softint_hdl_impl_t *hdlp, uint_t old_pri)
9620Sstevel@tonic-gate {
9630Sstevel@tonic-gate 	uint_t		intr_id;
9640Sstevel@tonic-gate 	int		ret;
9650Sstevel@tonic-gate 
9660Sstevel@tonic-gate 	ASSERT(hdlp != NULL);
9670Sstevel@tonic-gate 	ASSERT(hdlp->ih_private != NULL);
9680Sstevel@tonic-gate 
9691009Smathue 	/* use uintptr_t to suppress the gcc warning */
9701009Smathue 	intr_id = (uint_t)(uintptr_t)hdlp->ih_private;
9710Sstevel@tonic-gate 
9720Sstevel@tonic-gate 	/* update the vector table for the new priority */
9730Sstevel@tonic-gate 	ret = update_softint_pri(intr_id, hdlp->ih_pri);
9740Sstevel@tonic-gate 
9750Sstevel@tonic-gate 	return (ret);
9760Sstevel@tonic-gate }
9770Sstevel@tonic-gate 
978916Sschwartz /*ARGSUSED*/
979916Sschwartz void
980916Sschwartz i_ddi_alloc_intr_phdl(ddi_intr_handle_impl_t *hdlp)
981916Sschwartz {
982916Sschwartz }
983916Sschwartz 
984916Sschwartz /*ARGSUSED*/
985916Sschwartz void
986916Sschwartz i_ddi_free_intr_phdl(ddi_intr_handle_impl_t *hdlp)
987916Sschwartz {
988916Sschwartz }
989916Sschwartz 
9900Sstevel@tonic-gate /*
9910Sstevel@tonic-gate  * SECTION: DDI Memory/DMA
9920Sstevel@tonic-gate  */
9930Sstevel@tonic-gate 
9941900Seota /* set HAT endianess attributes from ddi_device_acc_attr */
9951900Seota void
9961900Seota i_ddi_devacc_to_hatacc(ddi_device_acc_attr_t *devaccp, uint_t *hataccp)
9971900Seota {
9981900Seota 	if (devaccp != NULL) {
9991900Seota 		if (devaccp->devacc_attr_endian_flags == DDI_STRUCTURE_LE_ACC) {
10001900Seota 			*hataccp &= ~HAT_ENDIAN_MASK;
10011900Seota 			*hataccp |= HAT_STRUCTURE_LE;
10021900Seota 		}
10031900Seota 	}
10041900Seota }
10051900Seota 
10061900Seota /*
10071900Seota  * Check if the specified cache attribute is supported on the platform.
10081900Seota  * This function must be called before i_ddi_cacheattr_to_hatacc().
10091900Seota  */
10101900Seota boolean_t
10111900Seota i_ddi_check_cache_attr(uint_t flags)
10121900Seota {
10131900Seota 	/*
10141900Seota 	 * The cache attributes are mutually exclusive. Any combination of
10151900Seota 	 * the attributes leads to a failure.
10161900Seota 	 */
10171900Seota 	uint_t cache_attr = IOMEM_CACHE_ATTR(flags);
10181900Seota 	if ((cache_attr != 0) && ((cache_attr & (cache_attr - 1)) != 0))
10191900Seota 		return (B_FALSE);
10201900Seota 
10211900Seota 	/*
10221900Seota 	 * On the sparc architecture, only IOMEM_DATA_CACHED is meaningful,
10231900Seota 	 * but others lead to a failure.
10241900Seota 	 */
10251900Seota 	if (cache_attr & IOMEM_DATA_CACHED)
10261900Seota 		return (B_TRUE);
10271900Seota 	else
10281900Seota 		return (B_FALSE);
10291900Seota }
10301900Seota 
10311900Seota /* set HAT cache attributes from the cache attributes */
10321900Seota void
10331900Seota i_ddi_cacheattr_to_hatacc(uint_t flags, uint_t *hataccp)
10341900Seota {
10351900Seota 	uint_t cache_attr = IOMEM_CACHE_ATTR(flags);
10361900Seota 	static char *fname = "i_ddi_cacheattr_to_hatacc";
10371900Seota #if defined(lint)
10381900Seota 	*hataccp = *hataccp;
10391900Seota #endif
10401900Seota 	/*
10411900Seota 	 * set HAT attrs according to the cache attrs.
10421900Seota 	 */
10431900Seota 	switch (cache_attr) {
10441900Seota 	/*
10451900Seota 	 * The cache coherency is always maintained on SPARC, and
10461900Seota 	 * nothing is required.
10471900Seota 	 */
10481900Seota 	case IOMEM_DATA_CACHED:
10491900Seota 		break;
10501900Seota 	/*
10511900Seota 	 * Both IOMEM_DATA_UC_WRITE_COMBINED and IOMEM_DATA_UNCACHED are
10521900Seota 	 * not supported on SPARC -- this case must not occur because the
10531900Seota 	 * cache attribute is scrutinized before this function is called.
10541900Seota 	 */
10551900Seota 	case IOMEM_DATA_UNCACHED:
10561900Seota 	case IOMEM_DATA_UC_WR_COMBINE:
10571900Seota 	default:
10581900Seota 		cmn_err(CE_WARN, "%s: cache_attr=0x%x is ignored.",
10591900Seota 		    fname, cache_attr);
10601900Seota 	}
10611900Seota }
10621900Seota 
10630Sstevel@tonic-gate static vmem_t *little_endian_arena;
10640Sstevel@tonic-gate static vmem_t *big_endian_arena;
10650Sstevel@tonic-gate 
10660Sstevel@tonic-gate static void *
10670Sstevel@tonic-gate segkmem_alloc_le(vmem_t *vmp, size_t size, int flag)
10680Sstevel@tonic-gate {
10690Sstevel@tonic-gate 	return (segkmem_xalloc(vmp, NULL, size, flag, HAT_STRUCTURE_LE,
10700Sstevel@tonic-gate 	    segkmem_page_create, NULL));
10710Sstevel@tonic-gate }
10720Sstevel@tonic-gate 
10730Sstevel@tonic-gate static void *
10740Sstevel@tonic-gate segkmem_alloc_be(vmem_t *vmp, size_t size, int flag)
10750Sstevel@tonic-gate {
10760Sstevel@tonic-gate 	return (segkmem_xalloc(vmp, NULL, size, flag, HAT_STRUCTURE_BE,
10770Sstevel@tonic-gate 	    segkmem_page_create, NULL));
10780Sstevel@tonic-gate }
10790Sstevel@tonic-gate 
10800Sstevel@tonic-gate void
10810Sstevel@tonic-gate ka_init(void)
10820Sstevel@tonic-gate {
10830Sstevel@tonic-gate 	little_endian_arena = vmem_create("little_endian", NULL, 0, 1,
10840Sstevel@tonic-gate 	    segkmem_alloc_le, segkmem_free, heap_arena, 0, VM_SLEEP);
10850Sstevel@tonic-gate 	big_endian_arena = vmem_create("big_endian", NULL, 0, 1,
10860Sstevel@tonic-gate 	    segkmem_alloc_be, segkmem_free, heap_arena, 0, VM_SLEEP);
10870Sstevel@tonic-gate }
10880Sstevel@tonic-gate 
10890Sstevel@tonic-gate /*
10900Sstevel@tonic-gate  * Allocate from the system, aligned on a specific boundary.
10910Sstevel@tonic-gate  * The alignment, if non-zero, must be a power of 2.
10920Sstevel@tonic-gate  */
10930Sstevel@tonic-gate static void *
10940Sstevel@tonic-gate kalloca(size_t size, size_t align, int cansleep, uint_t endian_flags)
10950Sstevel@tonic-gate {
10960Sstevel@tonic-gate 	size_t *addr, *raddr, rsize;
10970Sstevel@tonic-gate 	size_t hdrsize = 4 * sizeof (size_t);	/* must be power of 2 */
10980Sstevel@tonic-gate 
10990Sstevel@tonic-gate 	align = MAX(align, hdrsize);
11000Sstevel@tonic-gate 	ASSERT((align & (align - 1)) == 0);
11010Sstevel@tonic-gate 
11020Sstevel@tonic-gate 	/*
11030Sstevel@tonic-gate 	 * We need to allocate
11040Sstevel@tonic-gate 	 *    rsize = size + hdrsize + align - MIN(hdrsize, buffer_alignment)
11050Sstevel@tonic-gate 	 * bytes to be sure we have enough freedom to satisfy the request.
11060Sstevel@tonic-gate 	 * Since the buffer alignment depends on the request size, this is
11070Sstevel@tonic-gate 	 * not straightforward to use directly.
11080Sstevel@tonic-gate 	 *
11090Sstevel@tonic-gate 	 * kmem guarantees that any allocation of a 64-byte multiple will be
11100Sstevel@tonic-gate 	 * 64-byte aligned.  Since rounding up the request could add more
11110Sstevel@tonic-gate 	 * than we save, we compute the size with and without alignment, and
11120Sstevel@tonic-gate 	 * use the smaller of the two.
11130Sstevel@tonic-gate 	 */
11140Sstevel@tonic-gate 	rsize = size + hdrsize + align;
11150Sstevel@tonic-gate 
11160Sstevel@tonic-gate 	if (endian_flags == DDI_STRUCTURE_LE_ACC) {
11170Sstevel@tonic-gate 		raddr = vmem_alloc(little_endian_arena, rsize,
11180Sstevel@tonic-gate 		    cansleep ? VM_SLEEP : VM_NOSLEEP);
11190Sstevel@tonic-gate 	} else {
11200Sstevel@tonic-gate 		raddr = vmem_alloc(big_endian_arena, rsize,
11210Sstevel@tonic-gate 		    cansleep ? VM_SLEEP : VM_NOSLEEP);
11220Sstevel@tonic-gate 	}
11230Sstevel@tonic-gate 
11240Sstevel@tonic-gate 	if (raddr == NULL)
11250Sstevel@tonic-gate 		return (NULL);
11260Sstevel@tonic-gate 
11270Sstevel@tonic-gate 	addr = (size_t *)P2ROUNDUP((uintptr_t)raddr + hdrsize, align);
11280Sstevel@tonic-gate 	ASSERT((uintptr_t)addr + size - (uintptr_t)raddr <= rsize);
11290Sstevel@tonic-gate 
11300Sstevel@tonic-gate 	addr[-3] = (size_t)endian_flags;
11310Sstevel@tonic-gate 	addr[-2] = (size_t)raddr;
11320Sstevel@tonic-gate 	addr[-1] = rsize;
11330Sstevel@tonic-gate 
11340Sstevel@tonic-gate 	return (addr);
11350Sstevel@tonic-gate }
11360Sstevel@tonic-gate 
11370Sstevel@tonic-gate static void
11380Sstevel@tonic-gate kfreea(void *addr)
11390Sstevel@tonic-gate {
11400Sstevel@tonic-gate 	size_t *saddr = addr;
11410Sstevel@tonic-gate 
11420Sstevel@tonic-gate 	if (saddr[-3] == DDI_STRUCTURE_LE_ACC)
11430Sstevel@tonic-gate 		vmem_free(little_endian_arena, (void *)saddr[-2], saddr[-1]);
11440Sstevel@tonic-gate 	else
11450Sstevel@tonic-gate 		vmem_free(big_endian_arena, (void *)saddr[-2], saddr[-1]);
11460Sstevel@tonic-gate }
11470Sstevel@tonic-gate 
11480Sstevel@tonic-gate int
11490Sstevel@tonic-gate i_ddi_mem_alloc(dev_info_t *dip, ddi_dma_attr_t *attr,
11501900Seota     size_t length, int cansleep, int flags,
11510Sstevel@tonic-gate     ddi_device_acc_attr_t *accattrp,
11520Sstevel@tonic-gate     caddr_t *kaddrp, size_t *real_length, ddi_acc_hdl_t *handlep)
11530Sstevel@tonic-gate {
11540Sstevel@tonic-gate 	caddr_t a;
11551900Seota 	int iomin, align, streaming;
11560Sstevel@tonic-gate 	uint_t endian_flags = DDI_NEVERSWAP_ACC;
11570Sstevel@tonic-gate 
11580Sstevel@tonic-gate #if defined(lint)
11590Sstevel@tonic-gate 	*handlep = *handlep;
11600Sstevel@tonic-gate #endif
11610Sstevel@tonic-gate 
11620Sstevel@tonic-gate 	/*
11630Sstevel@tonic-gate 	 * Check legality of arguments
11640Sstevel@tonic-gate 	 */
11650Sstevel@tonic-gate 	if (length == 0 || kaddrp == NULL || attr == NULL) {
11660Sstevel@tonic-gate 		return (DDI_FAILURE);
11670Sstevel@tonic-gate 	}
11681900Seota 
11690Sstevel@tonic-gate 	if (attr->dma_attr_minxfer == 0 || attr->dma_attr_align == 0 ||
11700Sstevel@tonic-gate 	    (attr->dma_attr_align & (attr->dma_attr_align - 1)) ||
11710Sstevel@tonic-gate 	    (attr->dma_attr_minxfer & (attr->dma_attr_minxfer - 1))) {
11720Sstevel@tonic-gate 		return (DDI_FAILURE);
11730Sstevel@tonic-gate 	}
11740Sstevel@tonic-gate 
11750Sstevel@tonic-gate 	/*
11761900Seota 	 * check if a streaming sequential xfer is requested.
11771900Seota 	 */
11781900Seota 	streaming = (flags & DDI_DMA_STREAMING) ? 1 : 0;
11791900Seota 
11801900Seota 	/*
11810Sstevel@tonic-gate 	 * Drivers for 64-bit capable SBus devices will encode
11820Sstevel@tonic-gate 	 * the burtsizes for 64-bit xfers in the upper 16-bits.
11830Sstevel@tonic-gate 	 * For DMA alignment, we use the most restrictive
11840Sstevel@tonic-gate 	 * alignment of 32-bit and 64-bit xfers.
11850Sstevel@tonic-gate 	 */
11860Sstevel@tonic-gate 	iomin = (attr->dma_attr_burstsizes & 0xffff) |
11870Sstevel@tonic-gate 	    ((attr->dma_attr_burstsizes >> 16) & 0xffff);
11880Sstevel@tonic-gate 	/*
11890Sstevel@tonic-gate 	 * If a driver set burtsizes to 0, we give him byte alignment.
11900Sstevel@tonic-gate 	 * Otherwise align at the burtsizes boundary.
11910Sstevel@tonic-gate 	 */
11920Sstevel@tonic-gate 	if (iomin == 0)
11930Sstevel@tonic-gate 		iomin = 1;
11940Sstevel@tonic-gate 	else
11950Sstevel@tonic-gate 		iomin = 1 << (ddi_fls(iomin) - 1);
11960Sstevel@tonic-gate 	iomin = maxbit(iomin, attr->dma_attr_minxfer);
11970Sstevel@tonic-gate 	iomin = maxbit(iomin, attr->dma_attr_align);
11980Sstevel@tonic-gate 	iomin = ddi_iomin(dip, iomin, streaming);
11990Sstevel@tonic-gate 	if (iomin == 0)
12000Sstevel@tonic-gate 		return (DDI_FAILURE);
12010Sstevel@tonic-gate 
12020Sstevel@tonic-gate 	ASSERT((iomin & (iomin - 1)) == 0);
12030Sstevel@tonic-gate 	ASSERT(iomin >= attr->dma_attr_minxfer);
12040Sstevel@tonic-gate 	ASSERT(iomin >= attr->dma_attr_align);
12050Sstevel@tonic-gate 
12060Sstevel@tonic-gate 	length = P2ROUNDUP(length, iomin);
12070Sstevel@tonic-gate 	align = iomin;
12080Sstevel@tonic-gate 
12090Sstevel@tonic-gate 	if (accattrp != NULL)
12100Sstevel@tonic-gate 		endian_flags = accattrp->devacc_attr_endian_flags;
12110Sstevel@tonic-gate 
12120Sstevel@tonic-gate 	a = kalloca(length, align, cansleep, endian_flags);
12130Sstevel@tonic-gate 	if ((*kaddrp = a) == 0) {
12140Sstevel@tonic-gate 		return (DDI_FAILURE);
12150Sstevel@tonic-gate 	} else {
12160Sstevel@tonic-gate 		if (real_length) {
12170Sstevel@tonic-gate 			*real_length = length;
12180Sstevel@tonic-gate 		}
12190Sstevel@tonic-gate 		if (handlep) {
12200Sstevel@tonic-gate 			/*
12210Sstevel@tonic-gate 			 * assign handle information
12220Sstevel@tonic-gate 			 */
12230Sstevel@tonic-gate 			impl_acc_hdl_init(handlep);
12240Sstevel@tonic-gate 		}
12250Sstevel@tonic-gate 		return (DDI_SUCCESS);
12260Sstevel@tonic-gate 	}
12270Sstevel@tonic-gate }
12280Sstevel@tonic-gate 
12290Sstevel@tonic-gate /*
12300Sstevel@tonic-gate  * covert old DMA limits structure to DMA attribute structure
12310Sstevel@tonic-gate  * and continue
12320Sstevel@tonic-gate  */
12330Sstevel@tonic-gate int
12340Sstevel@tonic-gate i_ddi_mem_alloc_lim(dev_info_t *dip, ddi_dma_lim_t *limits,
12350Sstevel@tonic-gate     size_t length, int cansleep, int streaming,
12360Sstevel@tonic-gate     ddi_device_acc_attr_t *accattrp, caddr_t *kaddrp,
12370Sstevel@tonic-gate     uint_t *real_length, ddi_acc_hdl_t *ap)
12380Sstevel@tonic-gate {
12390Sstevel@tonic-gate 	ddi_dma_attr_t dma_attr, *attrp;
12400Sstevel@tonic-gate 	size_t rlen;
12410Sstevel@tonic-gate 	int ret;
12420Sstevel@tonic-gate 
12430Sstevel@tonic-gate 	ASSERT(limits);
12440Sstevel@tonic-gate 	attrp = &dma_attr;
12450Sstevel@tonic-gate 	attrp->dma_attr_version = DMA_ATTR_V0;
12460Sstevel@tonic-gate 	attrp->dma_attr_addr_lo = (uint64_t)limits->dlim_addr_lo;
12470Sstevel@tonic-gate 	attrp->dma_attr_addr_hi = (uint64_t)limits->dlim_addr_hi;
12480Sstevel@tonic-gate 	attrp->dma_attr_count_max = (uint64_t)-1;
12490Sstevel@tonic-gate 	attrp->dma_attr_align = 1;
12500Sstevel@tonic-gate 	attrp->dma_attr_burstsizes = (uint_t)limits->dlim_burstsizes;
12510Sstevel@tonic-gate 	attrp->dma_attr_minxfer = (uint32_t)limits->dlim_minxfer;
12520Sstevel@tonic-gate 	attrp->dma_attr_maxxfer = (uint64_t)-1;
12530Sstevel@tonic-gate 	attrp->dma_attr_seg = (uint64_t)limits->dlim_cntr_max;
12540Sstevel@tonic-gate 	attrp->dma_attr_sgllen = 1;
12550Sstevel@tonic-gate 	attrp->dma_attr_granular = 1;
12560Sstevel@tonic-gate 	attrp->dma_attr_flags = 0;
12570Sstevel@tonic-gate 
12580Sstevel@tonic-gate 	ret = i_ddi_mem_alloc(dip, attrp, length, cansleep, streaming,
12590Sstevel@tonic-gate 	    accattrp, kaddrp, &rlen, ap);
12600Sstevel@tonic-gate 	if (ret == DDI_SUCCESS) {
12610Sstevel@tonic-gate 		if (real_length)
12620Sstevel@tonic-gate 			*real_length = (uint_t)rlen;
12630Sstevel@tonic-gate 	}
12640Sstevel@tonic-gate 	return (ret);
12650Sstevel@tonic-gate }
12660Sstevel@tonic-gate 
12670Sstevel@tonic-gate /* ARGSUSED */
12680Sstevel@tonic-gate void
12691900Seota i_ddi_mem_free(caddr_t kaddr, ddi_acc_hdl_t *ap)
12700Sstevel@tonic-gate {
12710Sstevel@tonic-gate 	kfreea(kaddr);
12720Sstevel@tonic-gate }
12730Sstevel@tonic-gate 
12740Sstevel@tonic-gate /*
12750Sstevel@tonic-gate  * SECTION: DDI Data Access
12760Sstevel@tonic-gate  */
12770Sstevel@tonic-gate 
12780Sstevel@tonic-gate static uintptr_t impl_acc_hdl_id = 0;
12790Sstevel@tonic-gate 
12800Sstevel@tonic-gate /*
12810Sstevel@tonic-gate  * access handle allocator
12820Sstevel@tonic-gate  */
12830Sstevel@tonic-gate ddi_acc_hdl_t *
12840Sstevel@tonic-gate impl_acc_hdl_get(ddi_acc_handle_t hdl)
12850Sstevel@tonic-gate {
12860Sstevel@tonic-gate 	/*
12870Sstevel@tonic-gate 	 * Extract the access handle address from the DDI implemented
12880Sstevel@tonic-gate 	 * access handle
12890Sstevel@tonic-gate 	 */
12900Sstevel@tonic-gate 	return (&((ddi_acc_impl_t *)hdl)->ahi_common);
12910Sstevel@tonic-gate }
12920Sstevel@tonic-gate 
12930Sstevel@tonic-gate ddi_acc_handle_t
12940Sstevel@tonic-gate impl_acc_hdl_alloc(int (*waitfp)(caddr_t), caddr_t arg)
12950Sstevel@tonic-gate {
12960Sstevel@tonic-gate 	ddi_acc_impl_t *hp;
12970Sstevel@tonic-gate 	on_trap_data_t *otp;
12980Sstevel@tonic-gate 	int sleepflag;
12990Sstevel@tonic-gate 
13000Sstevel@tonic-gate 	sleepflag = ((waitfp == (int (*)())KM_SLEEP) ? KM_SLEEP : KM_NOSLEEP);
13010Sstevel@tonic-gate 
13020Sstevel@tonic-gate 	/*
13030Sstevel@tonic-gate 	 * Allocate and initialize the data access handle and error status.
13040Sstevel@tonic-gate 	 */
13050Sstevel@tonic-gate 	if ((hp = kmem_zalloc(sizeof (ddi_acc_impl_t), sleepflag)) == NULL)
13060Sstevel@tonic-gate 		goto fail;
13070Sstevel@tonic-gate 	if ((hp->ahi_err = (ndi_err_t *)kmem_zalloc(
13080Sstevel@tonic-gate 	    sizeof (ndi_err_t), sleepflag)) == NULL) {
13090Sstevel@tonic-gate 		kmem_free(hp, sizeof (ddi_acc_impl_t));
13100Sstevel@tonic-gate 		goto fail;
13110Sstevel@tonic-gate 	}
13120Sstevel@tonic-gate 	if ((otp = (on_trap_data_t *)kmem_zalloc(
13130Sstevel@tonic-gate 	    sizeof (on_trap_data_t), sleepflag)) == NULL) {
13140Sstevel@tonic-gate 		kmem_free(hp->ahi_err, sizeof (ndi_err_t));
13150Sstevel@tonic-gate 		kmem_free(hp, sizeof (ddi_acc_impl_t));
13160Sstevel@tonic-gate 		goto fail;
13170Sstevel@tonic-gate 	}
13180Sstevel@tonic-gate 	hp->ahi_err->err_ontrap = otp;
13190Sstevel@tonic-gate 	hp->ahi_common.ah_platform_private = (void *)hp;
13200Sstevel@tonic-gate 
13210Sstevel@tonic-gate 	return ((ddi_acc_handle_t)hp);
13220Sstevel@tonic-gate fail:
13230Sstevel@tonic-gate 	if ((waitfp != (int (*)())KM_SLEEP) &&
13240Sstevel@tonic-gate 	    (waitfp != (int (*)())KM_NOSLEEP))
13250Sstevel@tonic-gate 		ddi_set_callback(waitfp, arg, &impl_acc_hdl_id);
13260Sstevel@tonic-gate 	return (NULL);
13270Sstevel@tonic-gate }
13280Sstevel@tonic-gate 
13290Sstevel@tonic-gate void
13300Sstevel@tonic-gate impl_acc_hdl_free(ddi_acc_handle_t handle)
13310Sstevel@tonic-gate {
13320Sstevel@tonic-gate 	ddi_acc_impl_t *hp;
13330Sstevel@tonic-gate 
13340Sstevel@tonic-gate 	/*
13350Sstevel@tonic-gate 	 * The supplied (ddi_acc_handle_t) is actually a (ddi_acc_impl_t *),
13360Sstevel@tonic-gate 	 * because that's what we allocated in impl_acc_hdl_alloc() above.
13370Sstevel@tonic-gate 	 */
13380Sstevel@tonic-gate 	hp = (ddi_acc_impl_t *)handle;
13390Sstevel@tonic-gate 	if (hp) {
13400Sstevel@tonic-gate 		kmem_free(hp->ahi_err->err_ontrap, sizeof (on_trap_data_t));
13410Sstevel@tonic-gate 		kmem_free(hp->ahi_err, sizeof (ndi_err_t));
13420Sstevel@tonic-gate 		kmem_free(hp, sizeof (ddi_acc_impl_t));
13430Sstevel@tonic-gate 		if (impl_acc_hdl_id)
13440Sstevel@tonic-gate 			ddi_run_callback(&impl_acc_hdl_id);
13450Sstevel@tonic-gate 	}
13460Sstevel@tonic-gate }
13470Sstevel@tonic-gate 
13481865Sdilpreet #define	PCI_GET_MP_PFN(mp, page_no)	((mp)->dmai_ndvmapages == 1 ? \
13491865Sdilpreet 	(pfn_t)(mp)->dmai_iopte:(((pfn_t *)(mp)->dmai_iopte)[page_no]))
13501865Sdilpreet 
13511865Sdilpreet /*
13521865Sdilpreet  * Function called after a dma fault occurred to find out whether the
13531865Sdilpreet  * fault address is associated with a driver that is able to handle faults
13541865Sdilpreet  * and recover from faults.
13551865Sdilpreet  */
13561865Sdilpreet /* ARGSUSED */
13571865Sdilpreet int
13581865Sdilpreet impl_dma_check(dev_info_t *dip, const void *handle, const void *addr,
13591865Sdilpreet     const void *not_used)
13601865Sdilpreet {
13611865Sdilpreet 	ddi_dma_impl_t *mp = (ddi_dma_impl_t *)handle;
13621865Sdilpreet 	pfn_t fault_pfn = mmu_btop(*(uint64_t *)addr);
13631865Sdilpreet 	pfn_t comp_pfn;
13641865Sdilpreet 
13651865Sdilpreet 	/*
13661865Sdilpreet 	 * The driver has to set DDI_DMA_FLAGERR to recover from dma faults.
13671865Sdilpreet 	 */
13681865Sdilpreet 	int page;
13691865Sdilpreet 
13701865Sdilpreet 	ASSERT(mp);
13711865Sdilpreet 	for (page = 0; page < mp->dmai_ndvmapages; page++) {
13721865Sdilpreet 		comp_pfn = PCI_GET_MP_PFN(mp, page);
13731865Sdilpreet 		if (fault_pfn == comp_pfn)
13741865Sdilpreet 			return (DDI_FM_NONFATAL);
13751865Sdilpreet 	}
13761865Sdilpreet 	return (DDI_FM_UNKNOWN);
13771865Sdilpreet }
13781865Sdilpreet 
13791865Sdilpreet /*
13801865Sdilpreet  * Function used to check if a given access handle owns the failing address.
13811865Sdilpreet  * Called by ndi_fmc_error, when we detect a PIO error.
13821865Sdilpreet  */
13831865Sdilpreet /* ARGSUSED */
13841865Sdilpreet static int
13851865Sdilpreet impl_acc_check(dev_info_t *dip, const void *handle, const void *addr,
13861865Sdilpreet     const void *not_used)
13871865Sdilpreet {
13881865Sdilpreet 	pfn_t pfn, fault_pfn;
13891865Sdilpreet 	ddi_acc_hdl_t *hp;
13901865Sdilpreet 
13911865Sdilpreet 	hp = impl_acc_hdl_get((ddi_acc_handle_t)handle);
13921865Sdilpreet 
13931865Sdilpreet 	ASSERT(hp);
13941865Sdilpreet 
13951865Sdilpreet 	if (addr != NULL) {
13961865Sdilpreet 		pfn = hp->ah_pfn;
13971865Sdilpreet 		fault_pfn = mmu_btop(*(uint64_t *)addr);
13981865Sdilpreet 		if (fault_pfn >= pfn && fault_pfn < (pfn + hp->ah_pnum))
13991865Sdilpreet 			return (DDI_FM_NONFATAL);
14001865Sdilpreet 	}
14011865Sdilpreet 	return (DDI_FM_UNKNOWN);
14021865Sdilpreet }
14031865Sdilpreet 
14040Sstevel@tonic-gate void
14050Sstevel@tonic-gate impl_acc_err_init(ddi_acc_hdl_t *handlep)
14060Sstevel@tonic-gate {
14070Sstevel@tonic-gate 	int fmcap;
14080Sstevel@tonic-gate 	ndi_err_t *errp;
14090Sstevel@tonic-gate 	on_trap_data_t *otp;
14100Sstevel@tonic-gate 	ddi_acc_impl_t *hp = (ddi_acc_impl_t *)handlep;
14110Sstevel@tonic-gate 
14120Sstevel@tonic-gate 	fmcap = ddi_fm_capable(handlep->ah_dip);
14130Sstevel@tonic-gate 
14140Sstevel@tonic-gate 	if (handlep->ah_acc.devacc_attr_version < DDI_DEVICE_ATTR_V1 ||
14150Sstevel@tonic-gate 	    !DDI_FM_ACC_ERR_CAP(fmcap)) {
14160Sstevel@tonic-gate 		handlep->ah_acc.devacc_attr_access = DDI_DEFAULT_ACC;
14170Sstevel@tonic-gate 	} else if (DDI_FM_ACC_ERR_CAP(fmcap)) {
14180Sstevel@tonic-gate 		if (handlep->ah_acc.devacc_attr_access == DDI_DEFAULT_ACC) {
14190Sstevel@tonic-gate 			i_ddi_drv_ereport_post(handlep->ah_dip, DVR_EFMCAP,
14200Sstevel@tonic-gate 			    NULL, DDI_NOSLEEP);
14210Sstevel@tonic-gate 		} else {
14220Sstevel@tonic-gate 			errp = hp->ahi_err;
14230Sstevel@tonic-gate 			otp = (on_trap_data_t *)errp->err_ontrap;
14240Sstevel@tonic-gate 			otp->ot_handle = (void *)(hp);
14250Sstevel@tonic-gate 			otp->ot_prot = OT_DATA_ACCESS;
14260Sstevel@tonic-gate 			if (handlep->ah_acc.devacc_attr_access ==
14270Sstevel@tonic-gate 			    DDI_CAUTIOUS_ACC)
14280Sstevel@tonic-gate 				otp->ot_trampoline =
14290Sstevel@tonic-gate 				    (uintptr_t)&i_ddi_caut_trampoline;
14300Sstevel@tonic-gate 			else
14310Sstevel@tonic-gate 				otp->ot_trampoline =
14320Sstevel@tonic-gate 				    (uintptr_t)&i_ddi_prot_trampoline;
14330Sstevel@tonic-gate 			errp->err_status = DDI_FM_OK;
14340Sstevel@tonic-gate 			errp->err_expected = DDI_FM_ERR_UNEXPECTED;
14351865Sdilpreet 			errp->err_cf = impl_acc_check;
14360Sstevel@tonic-gate 		}
14370Sstevel@tonic-gate 	}
14380Sstevel@tonic-gate }
14390Sstevel@tonic-gate 
14400Sstevel@tonic-gate void
14410Sstevel@tonic-gate impl_acc_hdl_init(ddi_acc_hdl_t *handlep)
14420Sstevel@tonic-gate {
14430Sstevel@tonic-gate 	ddi_acc_impl_t *hp;
14440Sstevel@tonic-gate 
14450Sstevel@tonic-gate 	ASSERT(handlep);
14460Sstevel@tonic-gate 
14470Sstevel@tonic-gate 	hp = (ddi_acc_impl_t *)handlep;
14480Sstevel@tonic-gate 
14490Sstevel@tonic-gate 	/*
14500Sstevel@tonic-gate 	 * check for SW byte-swapping
14510Sstevel@tonic-gate 	 */
14520Sstevel@tonic-gate 	hp->ahi_get8 = i_ddi_get8;
14530Sstevel@tonic-gate 	hp->ahi_put8 = i_ddi_put8;
14540Sstevel@tonic-gate 	hp->ahi_rep_get8 = i_ddi_rep_get8;
14550Sstevel@tonic-gate 	hp->ahi_rep_put8 = i_ddi_rep_put8;
14560Sstevel@tonic-gate 	if (handlep->ah_acc.devacc_attr_endian_flags & DDI_STRUCTURE_LE_ACC) {
14570Sstevel@tonic-gate 		hp->ahi_get16 = i_ddi_swap_get16;
14580Sstevel@tonic-gate 		hp->ahi_get32 = i_ddi_swap_get32;
14590Sstevel@tonic-gate 		hp->ahi_get64 = i_ddi_swap_get64;
14600Sstevel@tonic-gate 		hp->ahi_put16 = i_ddi_swap_put16;
14610Sstevel@tonic-gate 		hp->ahi_put32 = i_ddi_swap_put32;
14620Sstevel@tonic-gate 		hp->ahi_put64 = i_ddi_swap_put64;
14630Sstevel@tonic-gate 		hp->ahi_rep_get16 = i_ddi_swap_rep_get16;
14640Sstevel@tonic-gate 		hp->ahi_rep_get32 = i_ddi_swap_rep_get32;
14650Sstevel@tonic-gate 		hp->ahi_rep_get64 = i_ddi_swap_rep_get64;
14660Sstevel@tonic-gate 		hp->ahi_rep_put16 = i_ddi_swap_rep_put16;
14670Sstevel@tonic-gate 		hp->ahi_rep_put32 = i_ddi_swap_rep_put32;
14680Sstevel@tonic-gate 		hp->ahi_rep_put64 = i_ddi_swap_rep_put64;
14690Sstevel@tonic-gate 	} else {
14700Sstevel@tonic-gate 		hp->ahi_get16 = i_ddi_get16;
14710Sstevel@tonic-gate 		hp->ahi_get32 = i_ddi_get32;
14720Sstevel@tonic-gate 		hp->ahi_get64 = i_ddi_get64;
14730Sstevel@tonic-gate 		hp->ahi_put16 = i_ddi_put16;
14740Sstevel@tonic-gate 		hp->ahi_put32 = i_ddi_put32;
14750Sstevel@tonic-gate 		hp->ahi_put64 = i_ddi_put64;
14760Sstevel@tonic-gate 		hp->ahi_rep_get16 = i_ddi_rep_get16;
14770Sstevel@tonic-gate 		hp->ahi_rep_get32 = i_ddi_rep_get32;
14780Sstevel@tonic-gate 		hp->ahi_rep_get64 = i_ddi_rep_get64;
14790Sstevel@tonic-gate 		hp->ahi_rep_put16 = i_ddi_rep_put16;
14800Sstevel@tonic-gate 		hp->ahi_rep_put32 = i_ddi_rep_put32;
14810Sstevel@tonic-gate 		hp->ahi_rep_put64 = i_ddi_rep_put64;
14820Sstevel@tonic-gate 	}
14830Sstevel@tonic-gate 
14840Sstevel@tonic-gate 	/* Legacy fault flags and support */
14850Sstevel@tonic-gate 	hp->ahi_fault_check = i_ddi_acc_fault_check;
14860Sstevel@tonic-gate 	hp->ahi_fault_notify = i_ddi_acc_fault_notify;
14870Sstevel@tonic-gate 	hp->ahi_fault = 0;
14880Sstevel@tonic-gate 	impl_acc_err_init(handlep);
14890Sstevel@tonic-gate }
14900Sstevel@tonic-gate 
14910Sstevel@tonic-gate void
14920Sstevel@tonic-gate i_ddi_acc_set_fault(ddi_acc_handle_t handle)
14930Sstevel@tonic-gate {
14940Sstevel@tonic-gate 	ddi_acc_impl_t *hp = (ddi_acc_impl_t *)handle;
14950Sstevel@tonic-gate 
14960Sstevel@tonic-gate 	if (!hp->ahi_fault) {
14970Sstevel@tonic-gate 		hp->ahi_fault = 1;
14980Sstevel@tonic-gate 			(*hp->ahi_fault_notify)(hp);
14990Sstevel@tonic-gate 	}
15000Sstevel@tonic-gate }
15010Sstevel@tonic-gate 
15020Sstevel@tonic-gate void
15030Sstevel@tonic-gate i_ddi_acc_clr_fault(ddi_acc_handle_t handle)
15040Sstevel@tonic-gate {
15050Sstevel@tonic-gate 	ddi_acc_impl_t *hp = (ddi_acc_impl_t *)handle;
15060Sstevel@tonic-gate 
15070Sstevel@tonic-gate 	if (hp->ahi_fault) {
15080Sstevel@tonic-gate 		hp->ahi_fault = 0;
15090Sstevel@tonic-gate 			(*hp->ahi_fault_notify)(hp);
15100Sstevel@tonic-gate 	}
15110Sstevel@tonic-gate }
15120Sstevel@tonic-gate 
15130Sstevel@tonic-gate /* ARGSUSED */
15140Sstevel@tonic-gate void
15150Sstevel@tonic-gate i_ddi_acc_fault_notify(ddi_acc_impl_t *hp)
15160Sstevel@tonic-gate {
15170Sstevel@tonic-gate 	/* Default version, does nothing */
15180Sstevel@tonic-gate }
15190Sstevel@tonic-gate 
15200Sstevel@tonic-gate /*
15210Sstevel@tonic-gate  * SECTION: Misc functions
15220Sstevel@tonic-gate  */
15230Sstevel@tonic-gate 
15240Sstevel@tonic-gate /*
15250Sstevel@tonic-gate  * instance wrappers
15260Sstevel@tonic-gate  */
15270Sstevel@tonic-gate /*ARGSUSED*/
15280Sstevel@tonic-gate uint_t
15290Sstevel@tonic-gate impl_assign_instance(dev_info_t *dip)
15300Sstevel@tonic-gate {
15310Sstevel@tonic-gate 	return ((uint_t)-1);
15320Sstevel@tonic-gate }
15330Sstevel@tonic-gate 
15340Sstevel@tonic-gate /*ARGSUSED*/
15350Sstevel@tonic-gate int
15360Sstevel@tonic-gate impl_keep_instance(dev_info_t *dip)
15370Sstevel@tonic-gate {
15380Sstevel@tonic-gate 	return (DDI_FAILURE);
15390Sstevel@tonic-gate }
15400Sstevel@tonic-gate 
15410Sstevel@tonic-gate /*ARGSUSED*/
15420Sstevel@tonic-gate int
15430Sstevel@tonic-gate impl_free_instance(dev_info_t *dip)
15440Sstevel@tonic-gate {
15450Sstevel@tonic-gate 	return (DDI_FAILURE);
15460Sstevel@tonic-gate }
15470Sstevel@tonic-gate 
15480Sstevel@tonic-gate /*ARGSUSED*/
15490Sstevel@tonic-gate int
15500Sstevel@tonic-gate impl_check_cpu(dev_info_t *devi)
15510Sstevel@tonic-gate {
15520Sstevel@tonic-gate 	return (DDI_SUCCESS);
15530Sstevel@tonic-gate }
15540Sstevel@tonic-gate 
15550Sstevel@tonic-gate 
15560Sstevel@tonic-gate static const char *nocopydevs[] = {
15570Sstevel@tonic-gate 	"SUNW,ffb",
15580Sstevel@tonic-gate 	"SUNW,afb",
15590Sstevel@tonic-gate 	NULL
15600Sstevel@tonic-gate };
15610Sstevel@tonic-gate 
15620Sstevel@tonic-gate /*
15630Sstevel@tonic-gate  * Perform a copy from a memory mapped device (whose devinfo pointer is devi)
15640Sstevel@tonic-gate  * separately mapped at devaddr in the kernel to a kernel buffer at kaddr.
15650Sstevel@tonic-gate  */
15660Sstevel@tonic-gate /*ARGSUSED*/
15670Sstevel@tonic-gate int
15680Sstevel@tonic-gate e_ddi_copyfromdev(dev_info_t *devi,
15690Sstevel@tonic-gate     off_t off, const void *devaddr, void *kaddr, size_t len)
15700Sstevel@tonic-gate {
15710Sstevel@tonic-gate 	const char **argv;
15720Sstevel@tonic-gate 
15730Sstevel@tonic-gate 	for (argv = nocopydevs; *argv; argv++)
15740Sstevel@tonic-gate 		if (strcmp(ddi_binding_name(devi), *argv) == 0) {
15750Sstevel@tonic-gate 			bzero(kaddr, len);
15760Sstevel@tonic-gate 			return (0);
15770Sstevel@tonic-gate 		}
15780Sstevel@tonic-gate 
15790Sstevel@tonic-gate 	bcopy(devaddr, kaddr, len);
15800Sstevel@tonic-gate 	return (0);
15810Sstevel@tonic-gate }
15820Sstevel@tonic-gate 
15830Sstevel@tonic-gate /*
15840Sstevel@tonic-gate  * Perform a copy to a memory mapped device (whose devinfo pointer is devi)
15850Sstevel@tonic-gate  * separately mapped at devaddr in the kernel from a kernel buffer at kaddr.
15860Sstevel@tonic-gate  */
15870Sstevel@tonic-gate /*ARGSUSED*/
15880Sstevel@tonic-gate int
15890Sstevel@tonic-gate e_ddi_copytodev(dev_info_t *devi,
15900Sstevel@tonic-gate     off_t off, const void *kaddr, void *devaddr, size_t len)
15910Sstevel@tonic-gate {
15920Sstevel@tonic-gate 	const char **argv;
15930Sstevel@tonic-gate 
15940Sstevel@tonic-gate 	for (argv = nocopydevs; *argv; argv++)
15950Sstevel@tonic-gate 		if (strcmp(ddi_binding_name(devi), *argv) == 0)
15960Sstevel@tonic-gate 			return (1);
15970Sstevel@tonic-gate 
15980Sstevel@tonic-gate 	bcopy(kaddr, devaddr, len);
15990Sstevel@tonic-gate 	return (0);
16000Sstevel@tonic-gate }
16010Sstevel@tonic-gate 
16020Sstevel@tonic-gate /*
16030Sstevel@tonic-gate  * Boot Configuration
16040Sstevel@tonic-gate  */
16050Sstevel@tonic-gate idprom_t idprom;
16060Sstevel@tonic-gate 
16070Sstevel@tonic-gate /*
16080Sstevel@tonic-gate  * Configure the hardware on the system.
16090Sstevel@tonic-gate  * Called before the rootfs is mounted
16100Sstevel@tonic-gate  */
16110Sstevel@tonic-gate void
16120Sstevel@tonic-gate configure(void)
16130Sstevel@tonic-gate {
16140Sstevel@tonic-gate 	extern void i_ddi_init_root();
16150Sstevel@tonic-gate 
16160Sstevel@tonic-gate 	/* We better have released boot by this time! */
16170Sstevel@tonic-gate 	ASSERT(!bootops);
16180Sstevel@tonic-gate 
16190Sstevel@tonic-gate 	/*
16200Sstevel@tonic-gate 	 * Determine whether or not to use the fpu, V9 SPARC cpus
16210Sstevel@tonic-gate 	 * always have one. Could check for existence of a fp queue,
16220Sstevel@tonic-gate 	 * Ultra I, II and IIa do not have a fp queue.
16230Sstevel@tonic-gate 	 */
16240Sstevel@tonic-gate 	if (fpu_exists)
16250Sstevel@tonic-gate 		fpu_probe();
16260Sstevel@tonic-gate 	else
16270Sstevel@tonic-gate 		cmn_err(CE_CONT, "FPU not in use\n");
16280Sstevel@tonic-gate 
16290Sstevel@tonic-gate #if 0 /* XXXQ - not necessary for sun4u */
16300Sstevel@tonic-gate 	/*
16310Sstevel@tonic-gate 	 * This following line fixes bugid 1041296; we need to do a
16320Sstevel@tonic-gate 	 * prom_nextnode(0) because this call ALSO patches the DMA+
16330Sstevel@tonic-gate 	 * bug in Campus-B and Phoenix. The prom uncaches the traptable
16340Sstevel@tonic-gate 	 * page as a side-effect of devr_next(0) (which prom_nextnode calls),
16350Sstevel@tonic-gate 	 * so this *must* be executed early on. (XXX This is untrue for sun4u)
16360Sstevel@tonic-gate 	 */
1637789Sahrens 	(void) prom_nextnode((pnode_t)0);
16380Sstevel@tonic-gate #endif
16390Sstevel@tonic-gate 
16400Sstevel@tonic-gate 	/*
16410Sstevel@tonic-gate 	 * Initialize devices on the machine.
16420Sstevel@tonic-gate 	 * Uses configuration tree built by the PROMs to determine what
16430Sstevel@tonic-gate 	 * is present, and builds a tree of prototype dev_info nodes
16440Sstevel@tonic-gate 	 * corresponding to the hardware which identified itself.
16450Sstevel@tonic-gate 	 */
16460Sstevel@tonic-gate 	i_ddi_init_root();
16470Sstevel@tonic-gate 
16480Sstevel@tonic-gate #ifdef	DDI_PROP_DEBUG
16490Sstevel@tonic-gate 	(void) ddi_prop_debug(1);	/* Enable property debugging */
16500Sstevel@tonic-gate #endif	/* DDI_PROP_DEBUG */
16510Sstevel@tonic-gate }
16520Sstevel@tonic-gate 
16530Sstevel@tonic-gate /*
16540Sstevel@tonic-gate  * The "status" property indicates the operational status of a device.
16550Sstevel@tonic-gate  * If this property is present, the value is a string indicating the
16560Sstevel@tonic-gate  * status of the device as follows:
16570Sstevel@tonic-gate  *
16580Sstevel@tonic-gate  *	"okay"		operational.
16590Sstevel@tonic-gate  *	"disabled"	not operational, but might become operational.
16600Sstevel@tonic-gate  *	"fail"		not operational because a fault has been detected,
16610Sstevel@tonic-gate  *			and it is unlikely that the device will become
16620Sstevel@tonic-gate  *			operational without repair. no additional details
16630Sstevel@tonic-gate  *			are available.
16640Sstevel@tonic-gate  *	"fail-xxx"	not operational because a fault has been detected,
16650Sstevel@tonic-gate  *			and it is unlikely that the device will become
16660Sstevel@tonic-gate  *			operational without repair. "xxx" is additional
16670Sstevel@tonic-gate  *			human-readable information about the particular
16680Sstevel@tonic-gate  *			fault condition that was detected.
16690Sstevel@tonic-gate  *
16700Sstevel@tonic-gate  * The absence of this property means that the operational status is
16710Sstevel@tonic-gate  * unknown or okay.
16720Sstevel@tonic-gate  *
16730Sstevel@tonic-gate  * This routine checks the status property of the specified device node
16740Sstevel@tonic-gate  * and returns 0 if the operational status indicates failure, and 1 otherwise.
16750Sstevel@tonic-gate  *
16760Sstevel@tonic-gate  * The property may exist on plug-in cards the existed before IEEE 1275-1994.
16770Sstevel@tonic-gate  * And, in that case, the property may not even be a string. So we carefully
16780Sstevel@tonic-gate  * check for the value "fail", in the beginning of the string, noting
16790Sstevel@tonic-gate  * the property length.
16800Sstevel@tonic-gate  */
16810Sstevel@tonic-gate int
16820Sstevel@tonic-gate status_okay(int id, char *buf, int buflen)
16830Sstevel@tonic-gate {
16840Sstevel@tonic-gate 	char status_buf[OBP_MAXPROPNAME];
16850Sstevel@tonic-gate 	char *bufp = buf;
16860Sstevel@tonic-gate 	int len = buflen;
16870Sstevel@tonic-gate 	int proplen;
16880Sstevel@tonic-gate 	static const char *status = "status";
16890Sstevel@tonic-gate 	static const char *fail = "fail";
16900Sstevel@tonic-gate 	size_t fail_len = strlen(fail);
16910Sstevel@tonic-gate 
16920Sstevel@tonic-gate 	/*
16930Sstevel@tonic-gate 	 * Get the proplen ... if it's smaller than "fail",
16940Sstevel@tonic-gate 	 * or doesn't exist ... then we don't care, since
16950Sstevel@tonic-gate 	 * the value can't begin with the char string "fail".
16960Sstevel@tonic-gate 	 *
16970Sstevel@tonic-gate 	 * NB: proplen, if it's a string, includes the NULL in the
16980Sstevel@tonic-gate 	 * the size of the property, and fail_len does not.
16990Sstevel@tonic-gate 	 */
1700789Sahrens 	proplen = prom_getproplen((pnode_t)id, (caddr_t)status);
17010Sstevel@tonic-gate 	if (proplen <= fail_len)	/* nonexistent or uninteresting len */
17020Sstevel@tonic-gate 		return (1);
17030Sstevel@tonic-gate 
17040Sstevel@tonic-gate 	/*
17050Sstevel@tonic-gate 	 * if a buffer was provided, use it
17060Sstevel@tonic-gate 	 */
17070Sstevel@tonic-gate 	if ((buf == (char *)NULL) || (buflen <= 0)) {
17080Sstevel@tonic-gate 		bufp = status_buf;
17090Sstevel@tonic-gate 		len = sizeof (status_buf);
17100Sstevel@tonic-gate 	}
17110Sstevel@tonic-gate 	*bufp = (char)0;
17120Sstevel@tonic-gate 
17130Sstevel@tonic-gate 	/*
17140Sstevel@tonic-gate 	 * Get the property into the buffer, to the extent of the buffer,
17150Sstevel@tonic-gate 	 * and in case the buffer is smaller than the property size,
17160Sstevel@tonic-gate 	 * NULL terminate the buffer. (This handles the case where
17170Sstevel@tonic-gate 	 * a buffer was passed in and the caller wants to print the
17180Sstevel@tonic-gate 	 * value, but the buffer was too small).
17190Sstevel@tonic-gate 	 */
1720789Sahrens 	(void) prom_bounded_getprop((pnode_t)id, (caddr_t)status,
17210Sstevel@tonic-gate 	    (caddr_t)bufp, len);
17220Sstevel@tonic-gate 	*(bufp + len - 1) = (char)0;
17230Sstevel@tonic-gate 
17240Sstevel@tonic-gate 	/*
17250Sstevel@tonic-gate 	 * If the value begins with the char string "fail",
17260Sstevel@tonic-gate 	 * then it means the node is failed. We don't care
17270Sstevel@tonic-gate 	 * about any other values. We assume the node is ok
17280Sstevel@tonic-gate 	 * although it might be 'disabled'.
17290Sstevel@tonic-gate 	 */
17300Sstevel@tonic-gate 	if (strncmp(bufp, fail, fail_len) == 0)
17310Sstevel@tonic-gate 		return (0);
17320Sstevel@tonic-gate 
17330Sstevel@tonic-gate 	return (1);
17340Sstevel@tonic-gate }
17350Sstevel@tonic-gate 
17360Sstevel@tonic-gate 
17370Sstevel@tonic-gate /*
17380Sstevel@tonic-gate  * We set the cpu type from the idprom, if we can.
17390Sstevel@tonic-gate  * Note that we just read out the contents of it, for the most part.
17400Sstevel@tonic-gate  */
17410Sstevel@tonic-gate void
17420Sstevel@tonic-gate setcputype(void)
17430Sstevel@tonic-gate {
17440Sstevel@tonic-gate 	/*
17450Sstevel@tonic-gate 	 * We cache the idprom info early on so that we don't
17460Sstevel@tonic-gate 	 * rummage through the NVRAM unnecessarily later.
17470Sstevel@tonic-gate 	 */
17480Sstevel@tonic-gate 	(void) prom_getidprom((caddr_t)&idprom, sizeof (idprom));
17490Sstevel@tonic-gate }
17500Sstevel@tonic-gate 
17510Sstevel@tonic-gate /*
17520Sstevel@tonic-gate  *  Here is where we actually infer meanings to the members of idprom_t
17530Sstevel@tonic-gate  */
17540Sstevel@tonic-gate void
17550Sstevel@tonic-gate parse_idprom(void)
17560Sstevel@tonic-gate {
17570Sstevel@tonic-gate 	if (idprom.id_format == IDFORM_1) {
17580Sstevel@tonic-gate 		uint_t i;
17590Sstevel@tonic-gate 
17600Sstevel@tonic-gate 		(void) localetheraddr((struct ether_addr *)idprom.id_ether,
17610Sstevel@tonic-gate 		    (struct ether_addr *)NULL);
17620Sstevel@tonic-gate 
17630Sstevel@tonic-gate 		i = idprom.id_machine << 24;
17640Sstevel@tonic-gate 		i = i + idprom.id_serial;
17650Sstevel@tonic-gate 		numtos((ulong_t)i, hw_serial);
17660Sstevel@tonic-gate 	} else
17670Sstevel@tonic-gate 		prom_printf("Invalid format code in IDprom.\n");
17680Sstevel@tonic-gate }
17690Sstevel@tonic-gate 
17700Sstevel@tonic-gate /*
17710Sstevel@tonic-gate  * Allow for implementation specific correction of PROM property values.
17720Sstevel@tonic-gate  */
17730Sstevel@tonic-gate /*ARGSUSED*/
17740Sstevel@tonic-gate void
17750Sstevel@tonic-gate impl_fix_props(dev_info_t *dip, dev_info_t *ch_dip, char *name, int len,
17760Sstevel@tonic-gate     caddr_t buffer)
17770Sstevel@tonic-gate {
17780Sstevel@tonic-gate 	/*
17790Sstevel@tonic-gate 	 * There are no adjustments needed in this implementation.
17800Sstevel@tonic-gate 	 */
17810Sstevel@tonic-gate }
17820Sstevel@tonic-gate 
17830Sstevel@tonic-gate /*
17840Sstevel@tonic-gate  * The following functions ready a cautious request to go up to the nexus
17850Sstevel@tonic-gate  * driver.  It is up to the nexus driver to decide how to process the request.
17860Sstevel@tonic-gate  * It may choose to call i_ddi_do_caut_get/put in this file, or do it
17870Sstevel@tonic-gate  * differently.
17880Sstevel@tonic-gate  */
17890Sstevel@tonic-gate 
17900Sstevel@tonic-gate static void
17910Sstevel@tonic-gate i_ddi_caut_getput_ctlops(
17920Sstevel@tonic-gate     ddi_acc_impl_t *hp, uint64_t host_addr, uint64_t dev_addr, size_t size,
17930Sstevel@tonic-gate     size_t repcount, uint_t flags, ddi_ctl_enum_t cmd)
17940Sstevel@tonic-gate {
17950Sstevel@tonic-gate 	peekpoke_ctlops_t	cautacc_ctlops_arg;
17960Sstevel@tonic-gate 
17970Sstevel@tonic-gate 	cautacc_ctlops_arg.size = size;
17980Sstevel@tonic-gate 	cautacc_ctlops_arg.dev_addr = dev_addr;
17990Sstevel@tonic-gate 	cautacc_ctlops_arg.host_addr = host_addr;
18000Sstevel@tonic-gate 	cautacc_ctlops_arg.handle = (ddi_acc_handle_t)hp;
18010Sstevel@tonic-gate 	cautacc_ctlops_arg.repcount = repcount;
18020Sstevel@tonic-gate 	cautacc_ctlops_arg.flags = flags;
18030Sstevel@tonic-gate 
18040Sstevel@tonic-gate 	(void) ddi_ctlops(hp->ahi_common.ah_dip, hp->ahi_common.ah_dip, cmd,
18050Sstevel@tonic-gate 	    &cautacc_ctlops_arg, NULL);
18060Sstevel@tonic-gate }
18070Sstevel@tonic-gate 
18080Sstevel@tonic-gate uint8_t
18090Sstevel@tonic-gate i_ddi_caut_get8(ddi_acc_impl_t *hp, uint8_t *addr)
18100Sstevel@tonic-gate {
18110Sstevel@tonic-gate 	uint8_t value;
18120Sstevel@tonic-gate 	i_ddi_caut_getput_ctlops(hp, (uint64_t)&value, (uint64_t)addr,
18130Sstevel@tonic-gate 	    sizeof (uint8_t), 1, 0, DDI_CTLOPS_PEEK);
18140Sstevel@tonic-gate 
18150Sstevel@tonic-gate 	return (value);
18160Sstevel@tonic-gate }
18170Sstevel@tonic-gate 
18180Sstevel@tonic-gate uint16_t
18190Sstevel@tonic-gate i_ddi_caut_get16(ddi_acc_impl_t *hp, uint16_t *addr)
18200Sstevel@tonic-gate {
18210Sstevel@tonic-gate 	uint16_t value;
18220Sstevel@tonic-gate 	i_ddi_caut_getput_ctlops(hp, (uint64_t)&value, (uint64_t)addr,
18230Sstevel@tonic-gate 	    sizeof (uint16_t), 1, 0, DDI_CTLOPS_PEEK);
18240Sstevel@tonic-gate 
18250Sstevel@tonic-gate 	return (value);
18260Sstevel@tonic-gate }
18270Sstevel@tonic-gate 
18280Sstevel@tonic-gate uint32_t
18290Sstevel@tonic-gate i_ddi_caut_get32(ddi_acc_impl_t *hp, uint32_t *addr)
18300Sstevel@tonic-gate {
18310Sstevel@tonic-gate 	uint32_t value;
18320Sstevel@tonic-gate 	i_ddi_caut_getput_ctlops(hp, (uint64_t)&value, (uint64_t)addr,
18330Sstevel@tonic-gate 	    sizeof (uint32_t), 1, 0, DDI_CTLOPS_PEEK);
18340Sstevel@tonic-gate 
18350Sstevel@tonic-gate 	return (value);
18360Sstevel@tonic-gate }
18370Sstevel@tonic-gate 
18380Sstevel@tonic-gate uint64_t
18390Sstevel@tonic-gate i_ddi_caut_get64(ddi_acc_impl_t *hp, uint64_t *addr)
18400Sstevel@tonic-gate {
18410Sstevel@tonic-gate 	uint64_t value;
18420Sstevel@tonic-gate 	i_ddi_caut_getput_ctlops(hp, (uint64_t)&value, (uint64_t)addr,
18430Sstevel@tonic-gate 	    sizeof (uint64_t), 1, 0, DDI_CTLOPS_PEEK);
18440Sstevel@tonic-gate 
18450Sstevel@tonic-gate 	return (value);
18460Sstevel@tonic-gate }
18470Sstevel@tonic-gate 
18480Sstevel@tonic-gate void
18490Sstevel@tonic-gate i_ddi_caut_put8(ddi_acc_impl_t *hp, uint8_t *addr, uint8_t value)
18500Sstevel@tonic-gate {
18510Sstevel@tonic-gate 	i_ddi_caut_getput_ctlops(hp, (uint64_t)&value, (uint64_t)addr,
18520Sstevel@tonic-gate 	    sizeof (uint8_t), 1, 0, DDI_CTLOPS_POKE);
18530Sstevel@tonic-gate }
18540Sstevel@tonic-gate 
18550Sstevel@tonic-gate void
18560Sstevel@tonic-gate i_ddi_caut_put16(ddi_acc_impl_t *hp, uint16_t *addr, uint16_t value)
18570Sstevel@tonic-gate {
18580Sstevel@tonic-gate 	i_ddi_caut_getput_ctlops(hp, (uint64_t)&value, (uint64_t)addr,
18590Sstevel@tonic-gate 	    sizeof (uint16_t), 1, 0, DDI_CTLOPS_POKE);
18600Sstevel@tonic-gate }
18610Sstevel@tonic-gate 
18620Sstevel@tonic-gate void
18630Sstevel@tonic-gate i_ddi_caut_put32(ddi_acc_impl_t *hp, uint32_t *addr, uint32_t value)
18640Sstevel@tonic-gate {
18650Sstevel@tonic-gate 	i_ddi_caut_getput_ctlops(hp, (uint64_t)&value, (uint64_t)addr,
18660Sstevel@tonic-gate 	    sizeof (uint32_t), 1, 0, DDI_CTLOPS_POKE);
18670Sstevel@tonic-gate }
18680Sstevel@tonic-gate 
18690Sstevel@tonic-gate void
18700Sstevel@tonic-gate i_ddi_caut_put64(ddi_acc_impl_t *hp, uint64_t *addr, uint64_t value)
18710Sstevel@tonic-gate {
18720Sstevel@tonic-gate 	i_ddi_caut_getput_ctlops(hp, (uint64_t)&value, (uint64_t)addr,
18730Sstevel@tonic-gate 	    sizeof (uint64_t), 1, 0, DDI_CTLOPS_POKE);
18740Sstevel@tonic-gate }
18750Sstevel@tonic-gate 
18760Sstevel@tonic-gate void
18770Sstevel@tonic-gate i_ddi_caut_rep_get8(ddi_acc_impl_t *hp, uint8_t *host_addr, uint8_t *dev_addr,
18780Sstevel@tonic-gate 	size_t repcount, uint_t flags)
18790Sstevel@tonic-gate {
18800Sstevel@tonic-gate 	i_ddi_caut_getput_ctlops(hp, (uint64_t)host_addr, (uint64_t)dev_addr,
18810Sstevel@tonic-gate 	    sizeof (uint8_t), repcount, flags, DDI_CTLOPS_PEEK);
18820Sstevel@tonic-gate }
18830Sstevel@tonic-gate 
18840Sstevel@tonic-gate void
18850Sstevel@tonic-gate i_ddi_caut_rep_get16(ddi_acc_impl_t *hp, uint16_t *host_addr,
18860Sstevel@tonic-gate     uint16_t *dev_addr, size_t repcount, uint_t flags)
18870Sstevel@tonic-gate {
18880Sstevel@tonic-gate 	i_ddi_caut_getput_ctlops(hp, (uint64_t)host_addr, (uint64_t)dev_addr,
18890Sstevel@tonic-gate 	    sizeof (uint16_t), repcount, flags, DDI_CTLOPS_PEEK);
18900Sstevel@tonic-gate }
18910Sstevel@tonic-gate 
18920Sstevel@tonic-gate void
18930Sstevel@tonic-gate i_ddi_caut_rep_get32(ddi_acc_impl_t *hp, uint32_t *host_addr,
18940Sstevel@tonic-gate     uint32_t *dev_addr, size_t repcount, uint_t flags)
18950Sstevel@tonic-gate {
18960Sstevel@tonic-gate 	i_ddi_caut_getput_ctlops(hp, (uint64_t)host_addr, (uint64_t)dev_addr,
18970Sstevel@tonic-gate 	    sizeof (uint32_t), repcount, flags, DDI_CTLOPS_PEEK);
18980Sstevel@tonic-gate }
18990Sstevel@tonic-gate 
19000Sstevel@tonic-gate void
19010Sstevel@tonic-gate i_ddi_caut_rep_get64(ddi_acc_impl_t *hp, uint64_t *host_addr,
19020Sstevel@tonic-gate     uint64_t *dev_addr, size_t repcount, uint_t flags)
19030Sstevel@tonic-gate {
19040Sstevel@tonic-gate 	i_ddi_caut_getput_ctlops(hp, (uint64_t)host_addr, (uint64_t)dev_addr,
19050Sstevel@tonic-gate 	    sizeof (uint64_t), repcount, flags, DDI_CTLOPS_PEEK);
19060Sstevel@tonic-gate }
19070Sstevel@tonic-gate 
19080Sstevel@tonic-gate void
19090Sstevel@tonic-gate i_ddi_caut_rep_put8(ddi_acc_impl_t *hp, uint8_t *host_addr, uint8_t *dev_addr,
19100Sstevel@tonic-gate 	size_t repcount, uint_t flags)
19110Sstevel@tonic-gate {
19120Sstevel@tonic-gate 	i_ddi_caut_getput_ctlops(hp, (uint64_t)host_addr, (uint64_t)dev_addr,
19130Sstevel@tonic-gate 	    sizeof (uint8_t), repcount, flags, DDI_CTLOPS_POKE);
19140Sstevel@tonic-gate }
19150Sstevel@tonic-gate 
19160Sstevel@tonic-gate void
19170Sstevel@tonic-gate i_ddi_caut_rep_put16(ddi_acc_impl_t *hp, uint16_t *host_addr,
19180Sstevel@tonic-gate     uint16_t *dev_addr, size_t repcount, uint_t flags)
19190Sstevel@tonic-gate {
19200Sstevel@tonic-gate 	i_ddi_caut_getput_ctlops(hp, (uint64_t)host_addr, (uint64_t)dev_addr,
19210Sstevel@tonic-gate 	    sizeof (uint16_t), repcount, flags, DDI_CTLOPS_POKE);
19220Sstevel@tonic-gate }
19230Sstevel@tonic-gate 
19240Sstevel@tonic-gate void
19250Sstevel@tonic-gate i_ddi_caut_rep_put32(ddi_acc_impl_t *hp, uint32_t *host_addr,
19260Sstevel@tonic-gate     uint32_t *dev_addr, size_t repcount, uint_t flags)
19270Sstevel@tonic-gate {
19280Sstevel@tonic-gate 	i_ddi_caut_getput_ctlops(hp, (uint64_t)host_addr, (uint64_t)dev_addr,
19290Sstevel@tonic-gate 	    sizeof (uint32_t), repcount, flags, DDI_CTLOPS_POKE);
19300Sstevel@tonic-gate }
19310Sstevel@tonic-gate 
19320Sstevel@tonic-gate void
19330Sstevel@tonic-gate i_ddi_caut_rep_put64(ddi_acc_impl_t *hp, uint64_t *host_addr,
19340Sstevel@tonic-gate     uint64_t *dev_addr, size_t repcount, uint_t flags)
19350Sstevel@tonic-gate {
19360Sstevel@tonic-gate 	i_ddi_caut_getput_ctlops(hp, (uint64_t)host_addr, (uint64_t)dev_addr,
19370Sstevel@tonic-gate 	    sizeof (uint64_t), repcount, flags, DDI_CTLOPS_POKE);
19380Sstevel@tonic-gate }
19390Sstevel@tonic-gate 
19400Sstevel@tonic-gate /*
19410Sstevel@tonic-gate  * This is called only to process peek/poke when the DIP is NULL.
19420Sstevel@tonic-gate  * Assume that this is for memory, as nexi take care of device safe accesses.
19430Sstevel@tonic-gate  */
19440Sstevel@tonic-gate int
19450Sstevel@tonic-gate peekpoke_mem(ddi_ctl_enum_t cmd, peekpoke_ctlops_t *in_args)
19460Sstevel@tonic-gate {
19470Sstevel@tonic-gate 	int err = DDI_SUCCESS;
19480Sstevel@tonic-gate 	on_trap_data_t otd;
19490Sstevel@tonic-gate 
19500Sstevel@tonic-gate 	/* Set up protected environment. */
19510Sstevel@tonic-gate 	if (!on_trap(&otd, OT_DATA_ACCESS)) {
19520Sstevel@tonic-gate 		uintptr_t tramp = otd.ot_trampoline;
19530Sstevel@tonic-gate 
19540Sstevel@tonic-gate 		if (cmd == DDI_CTLOPS_POKE) {
19550Sstevel@tonic-gate 			otd.ot_trampoline = (uintptr_t)&poke_fault;
19560Sstevel@tonic-gate 			err = do_poke(in_args->size, (void *)in_args->dev_addr,
19570Sstevel@tonic-gate 			    (void *)in_args->host_addr);
19580Sstevel@tonic-gate 		} else {
19590Sstevel@tonic-gate 			otd.ot_trampoline = (uintptr_t)&peek_fault;
19600Sstevel@tonic-gate 			err = do_peek(in_args->size, (void *)in_args->dev_addr,
19610Sstevel@tonic-gate 			    (void *)in_args->host_addr);
19620Sstevel@tonic-gate 		}
19630Sstevel@tonic-gate 		otd.ot_trampoline = tramp;
19640Sstevel@tonic-gate 	} else
19650Sstevel@tonic-gate 		err = DDI_FAILURE;
19660Sstevel@tonic-gate 
19670Sstevel@tonic-gate 	/* Take down protected environment. */
19680Sstevel@tonic-gate 	no_trap();
19690Sstevel@tonic-gate 
19700Sstevel@tonic-gate 	return (err);
19710Sstevel@tonic-gate }
1972*1991Sheppo 
1973*1991Sheppo /*
1974*1991Sheppo  * Platform independent DR routines
1975*1991Sheppo  */
1976*1991Sheppo 
1977*1991Sheppo static int
1978*1991Sheppo ndi2errno(int n)
1979*1991Sheppo {
1980*1991Sheppo 	int err = 0;
1981*1991Sheppo 
1982*1991Sheppo 	switch (n) {
1983*1991Sheppo 		case NDI_NOMEM:
1984*1991Sheppo 			err = ENOMEM;
1985*1991Sheppo 			break;
1986*1991Sheppo 		case NDI_BUSY:
1987*1991Sheppo 			err = EBUSY;
1988*1991Sheppo 			break;
1989*1991Sheppo 		case NDI_FAULT:
1990*1991Sheppo 			err = EFAULT;
1991*1991Sheppo 			break;
1992*1991Sheppo 		case NDI_FAILURE:
1993*1991Sheppo 			err = EIO;
1994*1991Sheppo 			break;
1995*1991Sheppo 		case NDI_SUCCESS:
1996*1991Sheppo 			break;
1997*1991Sheppo 		case NDI_BADHANDLE:
1998*1991Sheppo 		default:
1999*1991Sheppo 			err = EINVAL;
2000*1991Sheppo 			break;
2001*1991Sheppo 	}
2002*1991Sheppo 	return (err);
2003*1991Sheppo }
2004*1991Sheppo 
2005*1991Sheppo /*
2006*1991Sheppo  * Prom tree node list
2007*1991Sheppo  */
2008*1991Sheppo struct ptnode {
2009*1991Sheppo 	pnode_t		nodeid;
2010*1991Sheppo 	struct ptnode	*next;
2011*1991Sheppo };
2012*1991Sheppo 
2013*1991Sheppo /*
2014*1991Sheppo  * Prom tree walk arg
2015*1991Sheppo  */
2016*1991Sheppo struct pta {
2017*1991Sheppo 	dev_info_t	*pdip;
2018*1991Sheppo 	devi_branch_t	*bp;
2019*1991Sheppo 	uint_t		flags;
2020*1991Sheppo 	dev_info_t	*fdip;
2021*1991Sheppo 	struct ptnode	*head;
2022*1991Sheppo };
2023*1991Sheppo 
2024*1991Sheppo static void
2025*1991Sheppo visit_node(pnode_t nodeid, struct pta *ap)
2026*1991Sheppo {
2027*1991Sheppo 	struct ptnode	**nextp;
2028*1991Sheppo 	int		(*select)(pnode_t, void *, uint_t);
2029*1991Sheppo 
2030*1991Sheppo 	ASSERT(nodeid != OBP_NONODE && nodeid != OBP_BADNODE);
2031*1991Sheppo 
2032*1991Sheppo 	select = ap->bp->create.prom_branch_select;
2033*1991Sheppo 
2034*1991Sheppo 	ASSERT(select);
2035*1991Sheppo 
2036*1991Sheppo 	if (select(nodeid, ap->bp->arg, 0) == DDI_SUCCESS) {
2037*1991Sheppo 
2038*1991Sheppo 		for (nextp = &ap->head; *nextp; nextp = &(*nextp)->next)
2039*1991Sheppo 			;
2040*1991Sheppo 
2041*1991Sheppo 		*nextp = kmem_zalloc(sizeof (struct ptnode), KM_SLEEP);
2042*1991Sheppo 
2043*1991Sheppo 		(*nextp)->nodeid = nodeid;
2044*1991Sheppo 	}
2045*1991Sheppo 
2046*1991Sheppo 	if ((ap->flags & DEVI_BRANCH_CHILD) == DEVI_BRANCH_CHILD)
2047*1991Sheppo 		return;
2048*1991Sheppo 
2049*1991Sheppo 	nodeid = prom_childnode(nodeid);
2050*1991Sheppo 	while (nodeid != OBP_NONODE && nodeid != OBP_BADNODE) {
2051*1991Sheppo 		visit_node(nodeid, ap);
2052*1991Sheppo 		nodeid = prom_nextnode(nodeid);
2053*1991Sheppo 	}
2054*1991Sheppo }
2055*1991Sheppo 
2056*1991Sheppo /*ARGSUSED*/
2057*1991Sheppo static int
2058*1991Sheppo set_dip_offline(dev_info_t *dip, void *arg)
2059*1991Sheppo {
2060*1991Sheppo 	ASSERT(dip);
2061*1991Sheppo 
2062*1991Sheppo 	mutex_enter(&(DEVI(dip)->devi_lock));
2063*1991Sheppo 	if (!DEVI_IS_DEVICE_OFFLINE(dip))
2064*1991Sheppo 		DEVI_SET_DEVICE_OFFLINE(dip);
2065*1991Sheppo 	mutex_exit(&(DEVI(dip)->devi_lock));
2066*1991Sheppo 
2067*1991Sheppo 	return (DDI_WALK_CONTINUE);
2068*1991Sheppo }
2069*1991Sheppo 
2070*1991Sheppo /*ARGSUSED*/
2071*1991Sheppo static int
2072*1991Sheppo create_prom_branch(void *arg, int has_changed)
2073*1991Sheppo {
2074*1991Sheppo 	int		circ, c;
2075*1991Sheppo 	int		exists, rv;
2076*1991Sheppo 	pnode_t		nodeid;
2077*1991Sheppo 	struct ptnode	*tnp;
2078*1991Sheppo 	dev_info_t	*dip;
2079*1991Sheppo 	struct pta	*ap = arg;
2080*1991Sheppo 	devi_branch_t	*bp;
2081*1991Sheppo 
2082*1991Sheppo 	ASSERT(ap);
2083*1991Sheppo 	ASSERT(ap->fdip == NULL);
2084*1991Sheppo 	ASSERT(ap->pdip && ndi_dev_is_prom_node(ap->pdip));
2085*1991Sheppo 
2086*1991Sheppo 	bp = ap->bp;
2087*1991Sheppo 
2088*1991Sheppo 	nodeid = ddi_get_nodeid(ap->pdip);
2089*1991Sheppo 	if (nodeid == OBP_NONODE || nodeid == OBP_BADNODE) {
2090*1991Sheppo 		cmn_err(CE_WARN, "create_prom_branch: invalid "
2091*1991Sheppo 		    "nodeid: 0x%x", nodeid);
2092*1991Sheppo 		return (EINVAL);
2093*1991Sheppo 	}
2094*1991Sheppo 
2095*1991Sheppo 	ap->head = NULL;
2096*1991Sheppo 
2097*1991Sheppo 	nodeid = prom_childnode(nodeid);
2098*1991Sheppo 	while (nodeid != OBP_NONODE && nodeid != OBP_BADNODE) {
2099*1991Sheppo 		visit_node(nodeid, ap);
2100*1991Sheppo 		nodeid = prom_nextnode(nodeid);
2101*1991Sheppo 	}
2102*1991Sheppo 
2103*1991Sheppo 	if (ap->head == NULL)
2104*1991Sheppo 		return (ENODEV);
2105*1991Sheppo 
2106*1991Sheppo 	rv = 0;
2107*1991Sheppo 	while ((tnp = ap->head) != NULL) {
2108*1991Sheppo 		ap->head = tnp->next;
2109*1991Sheppo 
2110*1991Sheppo 		ndi_devi_enter(ap->pdip, &circ);
2111*1991Sheppo 
2112*1991Sheppo 		/*
2113*1991Sheppo 		 * Check if the branch already exists.
2114*1991Sheppo 		 */
2115*1991Sheppo 		exists = 0;
2116*1991Sheppo 		dip = e_ddi_nodeid_to_dip(tnp->nodeid);
2117*1991Sheppo 		if (dip != NULL) {
2118*1991Sheppo 			exists = 1;
2119*1991Sheppo 
2120*1991Sheppo 			/* Parent is held busy, so release hold */
2121*1991Sheppo 			ndi_rele_devi(dip);
2122*1991Sheppo #ifdef	DEBUG
2123*1991Sheppo 			cmn_err(CE_WARN, "create_prom_branch: dip(%p) exists"
2124*1991Sheppo 			    " for nodeid 0x%x", (void *)dip, tnp->nodeid);
2125*1991Sheppo #endif
2126*1991Sheppo 		} else {
2127*1991Sheppo 			dip = i_ddi_create_branch(ap->pdip, tnp->nodeid);
2128*1991Sheppo 		}
2129*1991Sheppo 
2130*1991Sheppo 		kmem_free(tnp, sizeof (struct ptnode));
2131*1991Sheppo 
2132*1991Sheppo 		if (dip == NULL) {
2133*1991Sheppo 			ndi_devi_exit(ap->pdip, circ);
2134*1991Sheppo 			rv = EIO;
2135*1991Sheppo 			continue;
2136*1991Sheppo 		}
2137*1991Sheppo 
2138*1991Sheppo 		ASSERT(ddi_get_parent(dip) == ap->pdip);
2139*1991Sheppo 
2140*1991Sheppo 		/*
2141*1991Sheppo 		 * Hold the branch if it is not already held
2142*1991Sheppo 		 */
2143*1991Sheppo 		if (!exists)
2144*1991Sheppo 			e_ddi_branch_hold(dip);
2145*1991Sheppo 
2146*1991Sheppo 		ASSERT(e_ddi_branch_held(dip));
2147*1991Sheppo 
2148*1991Sheppo 		/*
2149*1991Sheppo 		 * Set all dips in the branch offline so that
2150*1991Sheppo 		 * only a "configure" operation can attach
2151*1991Sheppo 		 * the branch
2152*1991Sheppo 		 */
2153*1991Sheppo 		(void) set_dip_offline(dip, NULL);
2154*1991Sheppo 
2155*1991Sheppo 		ndi_devi_enter(dip, &c);
2156*1991Sheppo 		ddi_walk_devs(ddi_get_child(dip), set_dip_offline, NULL);
2157*1991Sheppo 		ndi_devi_exit(dip, c);
2158*1991Sheppo 
2159*1991Sheppo 		ndi_devi_exit(ap->pdip, circ);
2160*1991Sheppo 
2161*1991Sheppo 		if (ap->flags & DEVI_BRANCH_CONFIGURE) {
2162*1991Sheppo 			int error = e_ddi_branch_configure(dip, &ap->fdip, 0);
2163*1991Sheppo 			if (error && rv == 0)
2164*1991Sheppo 				rv = error;
2165*1991Sheppo 		}
2166*1991Sheppo 
2167*1991Sheppo 		/*
2168*1991Sheppo 		 * Invoke devi_branch_callback() (if it exists) only for
2169*1991Sheppo 		 * newly created branches
2170*1991Sheppo 		 */
2171*1991Sheppo 		if (bp->devi_branch_callback && !exists)
2172*1991Sheppo 			bp->devi_branch_callback(dip, bp->arg, 0);
2173*1991Sheppo 	}
2174*1991Sheppo 
2175*1991Sheppo 	return (rv);
2176*1991Sheppo }
2177*1991Sheppo 
2178*1991Sheppo static int
2179*1991Sheppo sid_node_create(dev_info_t *pdip, devi_branch_t *bp, dev_info_t **rdipp)
2180*1991Sheppo {
2181*1991Sheppo 	int			rv, circ, len;
2182*1991Sheppo 	int			i, flags;
2183*1991Sheppo 	dev_info_t		*dip;
2184*1991Sheppo 	char			*nbuf;
2185*1991Sheppo 	static const char	*noname = "<none>";
2186*1991Sheppo 
2187*1991Sheppo 	ASSERT(pdip);
2188*1991Sheppo 	ASSERT(DEVI_BUSY_OWNED(pdip));
2189*1991Sheppo 
2190*1991Sheppo 	flags = 0;
2191*1991Sheppo 
2192*1991Sheppo 	/*
2193*1991Sheppo 	 * Creating the root of a branch ?
2194*1991Sheppo 	 */
2195*1991Sheppo 	if (rdipp) {
2196*1991Sheppo 		*rdipp = NULL;
2197*1991Sheppo 		flags = DEVI_BRANCH_ROOT;
2198*1991Sheppo 	}
2199*1991Sheppo 
2200*1991Sheppo 	ndi_devi_alloc_sleep(pdip, (char *)noname, DEVI_SID_NODEID, &dip);
2201*1991Sheppo 	rv = bp->create.sid_branch_create(dip, bp->arg, flags);
2202*1991Sheppo 
2203*1991Sheppo 	nbuf = kmem_alloc(OBP_MAXDRVNAME, KM_SLEEP);
2204*1991Sheppo 
2205*1991Sheppo 	if (rv == DDI_WALK_ERROR) {
2206*1991Sheppo 		cmn_err(CE_WARN, "e_ddi_branch_create: Error setting"
2207*1991Sheppo 		    " properties on devinfo node %p",  (void *)dip);
2208*1991Sheppo 		goto fail;
2209*1991Sheppo 	}
2210*1991Sheppo 
2211*1991Sheppo 	len = OBP_MAXDRVNAME;
2212*1991Sheppo 	if (ddi_getlongprop_buf(DDI_DEV_T_ANY, dip,
2213*1991Sheppo 	    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, "name", nbuf, &len)
2214*1991Sheppo 	    != DDI_PROP_SUCCESS) {
2215*1991Sheppo 		cmn_err(CE_WARN, "e_ddi_branch_create: devinfo node %p has"
2216*1991Sheppo 		    "no name property", (void *)dip);
2217*1991Sheppo 		goto fail;
2218*1991Sheppo 	}
2219*1991Sheppo 
2220*1991Sheppo 	ASSERT(i_ddi_node_state(dip) == DS_PROTO);
2221*1991Sheppo 	if (ndi_devi_set_nodename(dip, nbuf, 0) != NDI_SUCCESS) {
2222*1991Sheppo 		cmn_err(CE_WARN, "e_ddi_branch_create: cannot set name (%s)"
2223*1991Sheppo 		    " for devinfo node %p", nbuf, (void *)dip);
2224*1991Sheppo 		goto fail;
2225*1991Sheppo 	}
2226*1991Sheppo 
2227*1991Sheppo 	kmem_free(nbuf, OBP_MAXDRVNAME);
2228*1991Sheppo 
2229*1991Sheppo 	/*
2230*1991Sheppo 	 * Ignore bind failures just like boot does
2231*1991Sheppo 	 */
2232*1991Sheppo 	(void) ndi_devi_bind_driver(dip, 0);
2233*1991Sheppo 
2234*1991Sheppo 	switch (rv) {
2235*1991Sheppo 	case DDI_WALK_CONTINUE:
2236*1991Sheppo 	case DDI_WALK_PRUNESIB:
2237*1991Sheppo 		ndi_devi_enter(dip, &circ);
2238*1991Sheppo 
2239*1991Sheppo 		i = DDI_WALK_CONTINUE;
2240*1991Sheppo 		for (; i == DDI_WALK_CONTINUE; ) {
2241*1991Sheppo 			i = sid_node_create(dip, bp, NULL);
2242*1991Sheppo 		}
2243*1991Sheppo 
2244*1991Sheppo 		ASSERT(i == DDI_WALK_ERROR || i == DDI_WALK_PRUNESIB);
2245*1991Sheppo 		if (i == DDI_WALK_ERROR)
2246*1991Sheppo 			rv = i;
2247*1991Sheppo 		/*
2248*1991Sheppo 		 * If PRUNESIB stop creating siblings
2249*1991Sheppo 		 * of dip's child. Subsequent walk behavior
2250*1991Sheppo 		 * is determined by rv returned by dip.
2251*1991Sheppo 		 */
2252*1991Sheppo 
2253*1991Sheppo 		ndi_devi_exit(dip, circ);
2254*1991Sheppo 		break;
2255*1991Sheppo 	case DDI_WALK_TERMINATE:
2256*1991Sheppo 		/*
2257*1991Sheppo 		 * Don't create children and ask our parent
2258*1991Sheppo 		 * to not create siblings either.
2259*1991Sheppo 		 */
2260*1991Sheppo 		rv = DDI_WALK_PRUNESIB;
2261*1991Sheppo 		break;
2262*1991Sheppo 	case DDI_WALK_PRUNECHILD:
2263*1991Sheppo 		/*
2264*1991Sheppo 		 * Don't create children, but ask parent to continue
2265*1991Sheppo 		 * with siblings.
2266*1991Sheppo 		 */
2267*1991Sheppo 		rv = DDI_WALK_CONTINUE;
2268*1991Sheppo 		break;
2269*1991Sheppo 	default:
2270*1991Sheppo 		ASSERT(0);
2271*1991Sheppo 		break;
2272*1991Sheppo 	}
2273*1991Sheppo 
2274*1991Sheppo 	if (rdipp)
2275*1991Sheppo 		*rdipp = dip;
2276*1991Sheppo 
2277*1991Sheppo 	/*
2278*1991Sheppo 	 * Set device offline - only the "configure" op should cause an attach
2279*1991Sheppo 	 */
2280*1991Sheppo 	(void) set_dip_offline(dip, NULL);
2281*1991Sheppo 
2282*1991Sheppo 	return (rv);
2283*1991Sheppo fail:
2284*1991Sheppo 	(void) ndi_devi_free(dip);
2285*1991Sheppo 	kmem_free(nbuf, OBP_MAXDRVNAME);
2286*1991Sheppo 	return (DDI_WALK_ERROR);
2287*1991Sheppo }
2288*1991Sheppo 
2289*1991Sheppo static int
2290*1991Sheppo create_sid_branch(
2291*1991Sheppo 	dev_info_t	*pdip,
2292*1991Sheppo 	devi_branch_t	*bp,
2293*1991Sheppo 	dev_info_t	**dipp,
2294*1991Sheppo 	uint_t		flags)
2295*1991Sheppo {
2296*1991Sheppo 	int		rv = 0, state = DDI_WALK_CONTINUE;
2297*1991Sheppo 	dev_info_t	*rdip;
2298*1991Sheppo 
2299*1991Sheppo 	while (state == DDI_WALK_CONTINUE) {
2300*1991Sheppo 		int	circ;
2301*1991Sheppo 
2302*1991Sheppo 		ndi_devi_enter(pdip, &circ);
2303*1991Sheppo 
2304*1991Sheppo 		state = sid_node_create(pdip, bp, &rdip);
2305*1991Sheppo 		if (rdip == NULL) {
2306*1991Sheppo 			ndi_devi_exit(pdip, circ);
2307*1991Sheppo 			ASSERT(state == DDI_WALK_ERROR);
2308*1991Sheppo 			break;
2309*1991Sheppo 		}
2310*1991Sheppo 
2311*1991Sheppo 		e_ddi_branch_hold(rdip);
2312*1991Sheppo 
2313*1991Sheppo 		ndi_devi_exit(pdip, circ);
2314*1991Sheppo 
2315*1991Sheppo 		if (flags & DEVI_BRANCH_CONFIGURE) {
2316*1991Sheppo 			int error = e_ddi_branch_configure(rdip, dipp, 0);
2317*1991Sheppo 			if (error && rv == 0)
2318*1991Sheppo 				rv = error;
2319*1991Sheppo 		}
2320*1991Sheppo 
2321*1991Sheppo 		/*
2322*1991Sheppo 		 * devi_branch_callback() is optional
2323*1991Sheppo 		 */
2324*1991Sheppo 		if (bp->devi_branch_callback)
2325*1991Sheppo 			bp->devi_branch_callback(rdip, bp->arg, 0);
2326*1991Sheppo 	}
2327*1991Sheppo 
2328*1991Sheppo 	ASSERT(state == DDI_WALK_ERROR || state == DDI_WALK_PRUNESIB);
2329*1991Sheppo 
2330*1991Sheppo 	return (state == DDI_WALK_ERROR ? EIO : rv);
2331*1991Sheppo }
2332*1991Sheppo 
2333*1991Sheppo int
2334*1991Sheppo e_ddi_branch_create(
2335*1991Sheppo 	dev_info_t	*pdip,
2336*1991Sheppo 	devi_branch_t	*bp,
2337*1991Sheppo 	dev_info_t	**dipp,
2338*1991Sheppo 	uint_t		flags)
2339*1991Sheppo {
2340*1991Sheppo 	int prom_devi, sid_devi, error;
2341*1991Sheppo 
2342*1991Sheppo 	if (pdip == NULL || bp == NULL || bp->type == 0)
2343*1991Sheppo 		return (EINVAL);
2344*1991Sheppo 
2345*1991Sheppo 	prom_devi = (bp->type == DEVI_BRANCH_PROM) ? 1 : 0;
2346*1991Sheppo 	sid_devi = (bp->type == DEVI_BRANCH_SID) ? 1 : 0;
2347*1991Sheppo 
2348*1991Sheppo 	if (prom_devi && bp->create.prom_branch_select == NULL)
2349*1991Sheppo 		return (EINVAL);
2350*1991Sheppo 	else if (sid_devi && bp->create.sid_branch_create == NULL)
2351*1991Sheppo 		return (EINVAL);
2352*1991Sheppo 	else if (!prom_devi && !sid_devi)
2353*1991Sheppo 		return (EINVAL);
2354*1991Sheppo 
2355*1991Sheppo 	if (flags & DEVI_BRANCH_EVENT)
2356*1991Sheppo 		return (EINVAL);
2357*1991Sheppo 
2358*1991Sheppo 	if (prom_devi) {
2359*1991Sheppo 		struct pta pta = {0};
2360*1991Sheppo 
2361*1991Sheppo 		pta.pdip = pdip;
2362*1991Sheppo 		pta.bp = bp;
2363*1991Sheppo 		pta.flags = flags;
2364*1991Sheppo 
2365*1991Sheppo 		error = prom_tree_access(create_prom_branch, &pta, NULL);
2366*1991Sheppo 
2367*1991Sheppo 		if (dipp)
2368*1991Sheppo 			*dipp = pta.fdip;
2369*1991Sheppo 		else if (pta.fdip)
2370*1991Sheppo 			ndi_rele_devi(pta.fdip);
2371*1991Sheppo 	} else {
2372*1991Sheppo 		error = create_sid_branch(pdip, bp, dipp, flags);
2373*1991Sheppo 	}
2374*1991Sheppo 
2375*1991Sheppo 	return (error);
2376*1991Sheppo }
2377*1991Sheppo 
2378*1991Sheppo int
2379*1991Sheppo e_ddi_branch_configure(dev_info_t *rdip, dev_info_t **dipp, uint_t flags)
2380*1991Sheppo {
2381*1991Sheppo 	int		circ, rv;
2382*1991Sheppo 	char		*devnm;
2383*1991Sheppo 	dev_info_t	*pdip;
2384*1991Sheppo 
2385*1991Sheppo 	if (dipp)
2386*1991Sheppo 		*dipp = NULL;
2387*1991Sheppo 
2388*1991Sheppo 	if (rdip == NULL || flags != 0 || (flags & DEVI_BRANCH_EVENT))
2389*1991Sheppo 		return (EINVAL);
2390*1991Sheppo 
2391*1991Sheppo 	pdip = ddi_get_parent(rdip);
2392*1991Sheppo 
2393*1991Sheppo 	ndi_devi_enter(pdip, &circ);
2394*1991Sheppo 
2395*1991Sheppo 	if (!e_ddi_branch_held(rdip)) {
2396*1991Sheppo 		ndi_devi_exit(pdip, circ);
2397*1991Sheppo 		cmn_err(CE_WARN, "e_ddi_branch_configure: "
2398*1991Sheppo 		    "dip(%p) not held", (void *)rdip);
2399*1991Sheppo 		return (EINVAL);
2400*1991Sheppo 	}
2401*1991Sheppo 
2402*1991Sheppo 	if (i_ddi_node_state(rdip) < DS_INITIALIZED) {
2403*1991Sheppo 		/*
2404*1991Sheppo 		 * First attempt to bind a driver. If we fail, return
2405*1991Sheppo 		 * success (On some platforms, dips for some device
2406*1991Sheppo 		 * types (CPUs) may not have a driver)
2407*1991Sheppo 		 */
2408*1991Sheppo 		if (ndi_devi_bind_driver(rdip, 0) != NDI_SUCCESS) {
2409*1991Sheppo 			ndi_devi_exit(pdip, circ);
2410*1991Sheppo 			return (0);
2411*1991Sheppo 		}
2412*1991Sheppo 
2413*1991Sheppo 		if (ddi_initchild(pdip, rdip) != DDI_SUCCESS) {
2414*1991Sheppo 			rv = NDI_FAILURE;
2415*1991Sheppo 			goto out;
2416*1991Sheppo 		}
2417*1991Sheppo 	}
2418*1991Sheppo 
2419*1991Sheppo 	ASSERT(i_ddi_node_state(rdip) >= DS_INITIALIZED);
2420*1991Sheppo 
2421*1991Sheppo 	devnm = kmem_alloc(MAXNAMELEN + 1, KM_SLEEP);
2422*1991Sheppo 
2423*1991Sheppo 	(void) ddi_deviname(rdip, devnm);
2424*1991Sheppo 
2425*1991Sheppo 	if ((rv = ndi_devi_config_one(pdip, devnm+1, &rdip,
2426*1991Sheppo 	    NDI_DEVI_ONLINE | NDI_CONFIG)) == NDI_SUCCESS) {
2427*1991Sheppo 		/* release hold from ndi_devi_config_one() */
2428*1991Sheppo 		ndi_rele_devi(rdip);
2429*1991Sheppo 	}
2430*1991Sheppo 
2431*1991Sheppo 	kmem_free(devnm, MAXNAMELEN + 1);
2432*1991Sheppo out:
2433*1991Sheppo 	if (rv != NDI_SUCCESS && dipp) {
2434*1991Sheppo 		ndi_hold_devi(rdip);
2435*1991Sheppo 		*dipp = rdip;
2436*1991Sheppo 	}
2437*1991Sheppo 	ndi_devi_exit(pdip, circ);
2438*1991Sheppo 	return (ndi2errno(rv));
2439*1991Sheppo }
2440*1991Sheppo 
2441*1991Sheppo void
2442*1991Sheppo e_ddi_branch_hold(dev_info_t *rdip)
2443*1991Sheppo {
2444*1991Sheppo 	if (e_ddi_branch_held(rdip)) {
2445*1991Sheppo 		cmn_err(CE_WARN, "e_ddi_branch_hold: branch already held");
2446*1991Sheppo 		return;
2447*1991Sheppo 	}
2448*1991Sheppo 
2449*1991Sheppo 	mutex_enter(&DEVI(rdip)->devi_lock);
2450*1991Sheppo 	if ((DEVI(rdip)->devi_flags & DEVI_BRANCH_HELD) == 0) {
2451*1991Sheppo 		DEVI(rdip)->devi_flags |= DEVI_BRANCH_HELD;
2452*1991Sheppo 		DEVI(rdip)->devi_ref++;
2453*1991Sheppo 	}
2454*1991Sheppo 	ASSERT(DEVI(rdip)->devi_ref > 0);
2455*1991Sheppo 	mutex_exit(&DEVI(rdip)->devi_lock);
2456*1991Sheppo }
2457*1991Sheppo 
2458*1991Sheppo int
2459*1991Sheppo e_ddi_branch_held(dev_info_t *rdip)
2460*1991Sheppo {
2461*1991Sheppo 	int rv = 0;
2462*1991Sheppo 
2463*1991Sheppo 	mutex_enter(&DEVI(rdip)->devi_lock);
2464*1991Sheppo 	if ((DEVI(rdip)->devi_flags & DEVI_BRANCH_HELD) &&
2465*1991Sheppo 	    DEVI(rdip)->devi_ref > 0) {
2466*1991Sheppo 		rv = 1;
2467*1991Sheppo 	}
2468*1991Sheppo 	mutex_exit(&DEVI(rdip)->devi_lock);
2469*1991Sheppo 
2470*1991Sheppo 	return (rv);
2471*1991Sheppo }
2472*1991Sheppo void
2473*1991Sheppo e_ddi_branch_rele(dev_info_t *rdip)
2474*1991Sheppo {
2475*1991Sheppo 	mutex_enter(&DEVI(rdip)->devi_lock);
2476*1991Sheppo 	DEVI(rdip)->devi_flags &= ~DEVI_BRANCH_HELD;
2477*1991Sheppo 	DEVI(rdip)->devi_ref--;
2478*1991Sheppo 	mutex_exit(&DEVI(rdip)->devi_lock);
2479*1991Sheppo }
2480*1991Sheppo 
2481*1991Sheppo int
2482*1991Sheppo e_ddi_branch_unconfigure(
2483*1991Sheppo 	dev_info_t *rdip,
2484*1991Sheppo 	dev_info_t **dipp,
2485*1991Sheppo 	uint_t flags)
2486*1991Sheppo {
2487*1991Sheppo 	int	circ, rv;
2488*1991Sheppo 	int	destroy;
2489*1991Sheppo 	char	*devnm;
2490*1991Sheppo 	uint_t	nflags;
2491*1991Sheppo 	dev_info_t *pdip;
2492*1991Sheppo 
2493*1991Sheppo 	if (dipp)
2494*1991Sheppo 		*dipp = NULL;
2495*1991Sheppo 
2496*1991Sheppo 	if (rdip == NULL)
2497*1991Sheppo 		return (EINVAL);
2498*1991Sheppo 
2499*1991Sheppo 	pdip = ddi_get_parent(rdip);
2500*1991Sheppo 
2501*1991Sheppo 	ASSERT(pdip);
2502*1991Sheppo 
2503*1991Sheppo 	/*
2504*1991Sheppo 	 * Check if caller holds pdip busy - can cause deadlocks during
2505*1991Sheppo 	 * devfs_clean()
2506*1991Sheppo 	 */
2507*1991Sheppo 	if (DEVI_BUSY_OWNED(pdip)) {
2508*1991Sheppo 		cmn_err(CE_WARN, "e_ddi_branch_unconfigure: failed: parent"
2509*1991Sheppo 		    " devinfo node(%p) is busy held", (void *)pdip);
2510*1991Sheppo 		return (EINVAL);
2511*1991Sheppo 	}
2512*1991Sheppo 
2513*1991Sheppo 	destroy = (flags & DEVI_BRANCH_DESTROY) ? 1 : 0;
2514*1991Sheppo 
2515*1991Sheppo 	devnm = kmem_alloc(MAXNAMELEN + 1, KM_SLEEP);
2516*1991Sheppo 
2517*1991Sheppo 	ndi_devi_enter(pdip, &circ);
2518*1991Sheppo 	(void) ddi_deviname(rdip, devnm);
2519*1991Sheppo 	ndi_devi_exit(pdip, circ);
2520*1991Sheppo 
2521*1991Sheppo 	/*
2522*1991Sheppo 	 * ddi_deviname() returns a component name with / prepended.
2523*1991Sheppo 	 */
2524*1991Sheppo 	rv = devfs_clean(pdip, devnm + 1, DV_CLEAN_FORCE);
2525*1991Sheppo 	if (rv) {
2526*1991Sheppo 		kmem_free(devnm, MAXNAMELEN + 1);
2527*1991Sheppo 		return (rv);
2528*1991Sheppo 	}
2529*1991Sheppo 
2530*1991Sheppo 	ndi_devi_enter(pdip, &circ);
2531*1991Sheppo 
2532*1991Sheppo 	/*
2533*1991Sheppo 	 * Recreate device name as it may have changed state (init/uninit)
2534*1991Sheppo 	 * when parent busy lock was dropped for devfs_clean()
2535*1991Sheppo 	 */
2536*1991Sheppo 	(void) ddi_deviname(rdip, devnm);
2537*1991Sheppo 
2538*1991Sheppo 	if (!e_ddi_branch_held(rdip)) {
2539*1991Sheppo 		kmem_free(devnm, MAXNAMELEN + 1);
2540*1991Sheppo 		ndi_devi_exit(pdip, circ);
2541*1991Sheppo 		cmn_err(CE_WARN, "e_ddi_%s_branch: dip(%p) not held",
2542*1991Sheppo 		    destroy ? "destroy" : "unconfigure", (void *)rdip);
2543*1991Sheppo 		return (EINVAL);
2544*1991Sheppo 	}
2545*1991Sheppo 
2546*1991Sheppo 	/*
2547*1991Sheppo 	 * Release hold on the branch. This is ok since we are holding the
2548*1991Sheppo 	 * parent busy. If rdip is not removed, we must do a hold on the
2549*1991Sheppo 	 * branch before returning.
2550*1991Sheppo 	 */
2551*1991Sheppo 	e_ddi_branch_rele(rdip);
2552*1991Sheppo 
2553*1991Sheppo 	nflags = NDI_DEVI_OFFLINE;
2554*1991Sheppo 	if (destroy || (flags & DEVI_BRANCH_DESTROY)) {
2555*1991Sheppo 		nflags |= NDI_DEVI_REMOVE;
2556*1991Sheppo 		destroy = 1;
2557*1991Sheppo 	} else {
2558*1991Sheppo 		nflags |= NDI_UNCONFIG;		/* uninit but don't remove */
2559*1991Sheppo 	}
2560*1991Sheppo 
2561*1991Sheppo 	if (flags & DEVI_BRANCH_EVENT)
2562*1991Sheppo 		nflags |= NDI_POST_EVENT;
2563*1991Sheppo 
2564*1991Sheppo 	if (i_ddi_devi_attached(pdip) &&
2565*1991Sheppo 	    (i_ddi_node_state(rdip) >= DS_INITIALIZED)) {
2566*1991Sheppo 		rv = ndi_devi_unconfig_one(pdip, devnm+1, dipp, nflags);
2567*1991Sheppo 	} else {
2568*1991Sheppo 		rv = e_ddi_devi_unconfig(rdip, dipp, nflags);
2569*1991Sheppo 		if (rv == NDI_SUCCESS) {
2570*1991Sheppo 			ASSERT(!destroy || ddi_get_child(rdip) == NULL);
2571*1991Sheppo 			rv = ndi_devi_offline(rdip, nflags);
2572*1991Sheppo 		}
2573*1991Sheppo 	}
2574*1991Sheppo 
2575*1991Sheppo 	if (!destroy || rv != NDI_SUCCESS) {
2576*1991Sheppo 		/* The dip still exists, so do a hold */
2577*1991Sheppo 		e_ddi_branch_hold(rdip);
2578*1991Sheppo 	}
2579*1991Sheppo out:
2580*1991Sheppo 	kmem_free(devnm, MAXNAMELEN + 1);
2581*1991Sheppo 	ndi_devi_exit(pdip, circ);
2582*1991Sheppo 	return (ndi2errno(rv));
2583*1991Sheppo }
2584*1991Sheppo 
2585*1991Sheppo int
2586*1991Sheppo e_ddi_branch_destroy(dev_info_t *rdip, dev_info_t **dipp, uint_t flag)
2587*1991Sheppo {
2588*1991Sheppo 	return (e_ddi_branch_unconfigure(rdip, dipp,
2589*1991Sheppo 	    flag|DEVI_BRANCH_DESTROY));
2590*1991Sheppo }
2591*1991Sheppo 
2592*1991Sheppo /*
2593*1991Sheppo  * Number of chains for hash table
2594*1991Sheppo  */
2595*1991Sheppo #define	NUMCHAINS	17
2596*1991Sheppo 
2597*1991Sheppo /*
2598*1991Sheppo  * Devinfo busy arg
2599*1991Sheppo  */
2600*1991Sheppo struct devi_busy {
2601*1991Sheppo 	int dv_total;
2602*1991Sheppo 	int s_total;
2603*1991Sheppo 	mod_hash_t *dv_hash;
2604*1991Sheppo 	mod_hash_t *s_hash;
2605*1991Sheppo 	int (*callback)(dev_info_t *, void *, uint_t);
2606*1991Sheppo 	void *arg;
2607*1991Sheppo };
2608*1991Sheppo 
2609*1991Sheppo static int
2610*1991Sheppo visit_dip(dev_info_t *dip, void *arg)
2611*1991Sheppo {
2612*1991Sheppo 	uintptr_t sbusy, dvbusy, ref;
2613*1991Sheppo 	struct devi_busy *bsp = arg;
2614*1991Sheppo 
2615*1991Sheppo 	ASSERT(bsp->callback);
2616*1991Sheppo 
2617*1991Sheppo 	/*
2618*1991Sheppo 	 * A dip cannot be busy if its reference count is 0
2619*1991Sheppo 	 */
2620*1991Sheppo 	if ((ref = e_ddi_devi_holdcnt(dip)) == 0) {
2621*1991Sheppo 		return (bsp->callback(dip, bsp->arg, 0));
2622*1991Sheppo 	}
2623*1991Sheppo 
2624*1991Sheppo 	if (mod_hash_find(bsp->dv_hash, dip, (mod_hash_val_t *)&dvbusy))
2625*1991Sheppo 		dvbusy = 0;
2626*1991Sheppo 
2627*1991Sheppo 	/*
2628*1991Sheppo 	 * To catch device opens currently maintained on specfs common snodes.
2629*1991Sheppo 	 */
2630*1991Sheppo 	if (mod_hash_find(bsp->s_hash, dip, (mod_hash_val_t *)&sbusy))
2631*1991Sheppo 		sbusy = 0;
2632*1991Sheppo 
2633*1991Sheppo #ifdef	DEBUG
2634*1991Sheppo 	if (ref < sbusy || ref < dvbusy) {
2635*1991Sheppo 		cmn_err(CE_WARN, "dip(%p): sopen = %lu, dvopen = %lu "
2636*1991Sheppo 		    "dip ref = %lu\n", (void *)dip, sbusy, dvbusy, ref);
2637*1991Sheppo 	}
2638*1991Sheppo #endif
2639*1991Sheppo 
2640*1991Sheppo 	dvbusy = (sbusy > dvbusy) ? sbusy : dvbusy;
2641*1991Sheppo 
2642*1991Sheppo 	return (bsp->callback(dip, bsp->arg, dvbusy));
2643*1991Sheppo }
2644*1991Sheppo 
2645*1991Sheppo static int
2646*1991Sheppo visit_snode(struct snode *sp, void *arg)
2647*1991Sheppo {
2648*1991Sheppo 	uintptr_t sbusy;
2649*1991Sheppo 	dev_info_t *dip;
2650*1991Sheppo 	int count;
2651*1991Sheppo 	struct devi_busy *bsp = arg;
2652*1991Sheppo 
2653*1991Sheppo 	ASSERT(sp);
2654*1991Sheppo 
2655*1991Sheppo 	/*
2656*1991Sheppo 	 * The stable lock is held. This prevents
2657*1991Sheppo 	 * the snode and its associated dip from
2658*1991Sheppo 	 * going away.
2659*1991Sheppo 	 */
2660*1991Sheppo 	dip = NULL;
2661*1991Sheppo 	count = spec_devi_open_count(sp, &dip);
2662*1991Sheppo 
2663*1991Sheppo 	if (count <= 0)
2664*1991Sheppo 		return (DDI_WALK_CONTINUE);
2665*1991Sheppo 
2666*1991Sheppo 	ASSERT(dip);
2667*1991Sheppo 
2668*1991Sheppo 	if (mod_hash_remove(bsp->s_hash, dip, (mod_hash_val_t *)&sbusy))
2669*1991Sheppo 		sbusy = count;
2670*1991Sheppo 	else
2671*1991Sheppo 		sbusy += count;
2672*1991Sheppo 
2673*1991Sheppo 	if (mod_hash_insert(bsp->s_hash, dip, (mod_hash_val_t)sbusy)) {
2674*1991Sheppo 		cmn_err(CE_WARN, "%s: s_hash insert failed: dip=0x%p, "
2675*1991Sheppo 		    "sbusy = %lu", "e_ddi_branch_referenced",
2676*1991Sheppo 		    (void *)dip, sbusy);
2677*1991Sheppo 	}
2678*1991Sheppo 
2679*1991Sheppo 	bsp->s_total += count;
2680*1991Sheppo 
2681*1991Sheppo 	return (DDI_WALK_CONTINUE);
2682*1991Sheppo }
2683*1991Sheppo 
2684*1991Sheppo static void
2685*1991Sheppo visit_dvnode(struct dv_node *dv, void *arg)
2686*1991Sheppo {
2687*1991Sheppo 	uintptr_t dvbusy;
2688*1991Sheppo 	uint_t count;
2689*1991Sheppo 	struct vnode *vp;
2690*1991Sheppo 	struct devi_busy *bsp = arg;
2691*1991Sheppo 
2692*1991Sheppo 	ASSERT(dv && dv->dv_devi);
2693*1991Sheppo 
2694*1991Sheppo 	vp = DVTOV(dv);
2695*1991Sheppo 
2696*1991Sheppo 	mutex_enter(&vp->v_lock);
2697*1991Sheppo 	count = vp->v_count;
2698*1991Sheppo 	mutex_exit(&vp->v_lock);
2699*1991Sheppo 
2700*1991Sheppo 	if (!count)
2701*1991Sheppo 		return;
2702*1991Sheppo 
2703*1991Sheppo 	if (mod_hash_remove(bsp->dv_hash, dv->dv_devi,
2704*1991Sheppo 	    (mod_hash_val_t *)&dvbusy))
2705*1991Sheppo 		dvbusy = count;
2706*1991Sheppo 	else
2707*1991Sheppo 		dvbusy += count;
2708*1991Sheppo 
2709*1991Sheppo 	if (mod_hash_insert(bsp->dv_hash, dv->dv_devi,
2710*1991Sheppo 	    (mod_hash_val_t)dvbusy)) {
2711*1991Sheppo 		cmn_err(CE_WARN, "%s: dv_hash insert failed: dip=0x%p, "
2712*1991Sheppo 		    "dvbusy=%lu", "e_ddi_branch_referenced",
2713*1991Sheppo 		    (void *)dv->dv_devi, dvbusy);
2714*1991Sheppo 	}
2715*1991Sheppo 
2716*1991Sheppo 	bsp->dv_total += count;
2717*1991Sheppo }
2718*1991Sheppo 
2719*1991Sheppo /*
2720*1991Sheppo  * Returns reference count on success or -1 on failure.
2721*1991Sheppo  */
2722*1991Sheppo int
2723*1991Sheppo e_ddi_branch_referenced(
2724*1991Sheppo 	dev_info_t *rdip,
2725*1991Sheppo 	int (*callback)(dev_info_t *dip, void *arg, uint_t ref),
2726*1991Sheppo 	void *arg)
2727*1991Sheppo {
2728*1991Sheppo 	int circ;
2729*1991Sheppo 	char *path;
2730*1991Sheppo 	dev_info_t *pdip;
2731*1991Sheppo 	struct devi_busy bsa = {0};
2732*1991Sheppo 
2733*1991Sheppo 	ASSERT(rdip);
2734*1991Sheppo 
2735*1991Sheppo 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
2736*1991Sheppo 
2737*1991Sheppo 	ndi_hold_devi(rdip);
2738*1991Sheppo 
2739*1991Sheppo 	pdip = ddi_get_parent(rdip);
2740*1991Sheppo 
2741*1991Sheppo 	ASSERT(pdip);
2742*1991Sheppo 
2743*1991Sheppo 	/*
2744*1991Sheppo 	 * Check if caller holds pdip busy - can cause deadlocks during
2745*1991Sheppo 	 * devfs_walk()
2746*1991Sheppo 	 */
2747*1991Sheppo 	if (!e_ddi_branch_held(rdip) || DEVI_BUSY_OWNED(pdip)) {
2748*1991Sheppo 		cmn_err(CE_WARN, "e_ddi_branch_referenced: failed: "
2749*1991Sheppo 		    "devinfo branch(%p) not held or parent busy held",
2750*1991Sheppo 		    (void *)rdip);
2751*1991Sheppo 		ndi_rele_devi(rdip);
2752*1991Sheppo 		kmem_free(path, MAXPATHLEN);
2753*1991Sheppo 		return (-1);
2754*1991Sheppo 	}
2755*1991Sheppo 
2756*1991Sheppo 	ndi_devi_enter(pdip, &circ);
2757*1991Sheppo 	(void) ddi_pathname(rdip, path);
2758*1991Sheppo 	ndi_devi_exit(pdip, circ);
2759*1991Sheppo 
2760*1991Sheppo 	bsa.dv_hash = mod_hash_create_ptrhash("dv_node busy hash", NUMCHAINS,
2761*1991Sheppo 	    mod_hash_null_valdtor, sizeof (struct dev_info));
2762*1991Sheppo 
2763*1991Sheppo 	bsa.s_hash = mod_hash_create_ptrhash("snode busy hash", NUMCHAINS,
2764*1991Sheppo 	    mod_hash_null_valdtor, sizeof (struct snode));
2765*1991Sheppo 
2766*1991Sheppo 	if (devfs_walk(path, visit_dvnode, &bsa)) {
2767*1991Sheppo 		cmn_err(CE_WARN, "e_ddi_branch_referenced: "
2768*1991Sheppo 		    "devfs walk failed for: %s", path);
2769*1991Sheppo 		kmem_free(path, MAXPATHLEN);
2770*1991Sheppo 		bsa.s_total = bsa.dv_total = -1;
2771*1991Sheppo 		goto out;
2772*1991Sheppo 	}
2773*1991Sheppo 
2774*1991Sheppo 	kmem_free(path, MAXPATHLEN);
2775*1991Sheppo 
2776*1991Sheppo 	/*
2777*1991Sheppo 	 * Walk the snode table to detect device opens, which are currently
2778*1991Sheppo 	 * maintained on specfs common snodes.
2779*1991Sheppo 	 */
2780*1991Sheppo 	spec_snode_walk(visit_snode, &bsa);
2781*1991Sheppo 
2782*1991Sheppo 	if (callback == NULL)
2783*1991Sheppo 		goto out;
2784*1991Sheppo 
2785*1991Sheppo 	bsa.callback = callback;
2786*1991Sheppo 	bsa.arg = arg;
2787*1991Sheppo 
2788*1991Sheppo 	if (visit_dip(rdip, &bsa) == DDI_WALK_CONTINUE) {
2789*1991Sheppo 		ndi_devi_enter(rdip, &circ);
2790*1991Sheppo 		ddi_walk_devs(ddi_get_child(rdip), visit_dip, &bsa);
2791*1991Sheppo 		ndi_devi_exit(rdip, circ);
2792*1991Sheppo 	}
2793*1991Sheppo 
2794*1991Sheppo out:
2795*1991Sheppo 	ndi_rele_devi(rdip);
2796*1991Sheppo 	mod_hash_destroy_ptrhash(bsa.s_hash);
2797*1991Sheppo 	mod_hash_destroy_ptrhash(bsa.dv_hash);
2798*1991Sheppo 	return (bsa.s_total > bsa.dv_total ? bsa.s_total : bsa.dv_total);
2799*1991Sheppo }
2800