xref: /onnv-gate/usr/src/uts/sun4/io/px/px_util.c (revision 11596:e9010337bcd3)
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
51501Sgovinda  * Common Development and Distribution License (the "License").
61501Sgovinda  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*11596SJason.Beloro@Sun.COM  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate  * PCI nexus utility routines:
280Sstevel@tonic-gate  *	property and config routines for attach()
290Sstevel@tonic-gate  *	reg/intr/range/assigned-address property routines for bus_map()
300Sstevel@tonic-gate  *	init_child()
310Sstevel@tonic-gate  *	fault handling
320Sstevel@tonic-gate  */
330Sstevel@tonic-gate 
340Sstevel@tonic-gate #include <sys/types.h>
350Sstevel@tonic-gate #include <sys/kmem.h>
360Sstevel@tonic-gate #include <sys/async.h>
370Sstevel@tonic-gate #include <sys/sysmacros.h>
380Sstevel@tonic-gate #include <sys/sunddi.h>
390Sstevel@tonic-gate #include <sys/sunndi.h>
400Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
410Sstevel@tonic-gate #include "px_obj.h"
4210187SKrishna.Elango@Sun.COM #include <sys/pcie_pwr.h>
430Sstevel@tonic-gate 
440Sstevel@tonic-gate /*LINTLIBRARY*/
450Sstevel@tonic-gate 
460Sstevel@tonic-gate /*
470Sstevel@tonic-gate  * px_get_props
480Sstevel@tonic-gate  *
490Sstevel@tonic-gate  * This function is called from the attach routine to get the key
500Sstevel@tonic-gate  * properties of the pci nodes.
510Sstevel@tonic-gate  *
520Sstevel@tonic-gate  * used by: px_attach()
530Sstevel@tonic-gate  *
540Sstevel@tonic-gate  * return value: DDI_FAILURE on failure
550Sstevel@tonic-gate  */
560Sstevel@tonic-gate int
px_get_props(px_t * px_p,dev_info_t * dip)570Sstevel@tonic-gate px_get_props(px_t *px_p, dev_info_t *dip)
580Sstevel@tonic-gate {
590Sstevel@tonic-gate 	int i, no_of_intrs;
600Sstevel@tonic-gate 
610Sstevel@tonic-gate 	/*
620Sstevel@tonic-gate 	 * Get the bus-ranges property.
630Sstevel@tonic-gate 	 */
640Sstevel@tonic-gate 	i = sizeof (px_p->px_bus_range);
650Sstevel@tonic-gate 	if (ddi_getlongprop_buf(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
660Sstevel@tonic-gate 	    "bus-range", (caddr_t)&px_p->px_bus_range, &i) != DDI_SUCCESS) {
670Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d: no bus-range property\n",
680Sstevel@tonic-gate 		    ddi_driver_name(dip), ddi_get_instance(dip));
690Sstevel@tonic-gate 		return (DDI_FAILURE);
700Sstevel@tonic-gate 	}
710Sstevel@tonic-gate 	DBG(DBG_ATTACH, dip, "get_px_properties: bus-range (%x,%x)\n",
726313Skrishnae 	    px_p->px_bus_range.lo, px_p->px_bus_range.hi);
730Sstevel@tonic-gate 
740Sstevel@tonic-gate 	/*
750Sstevel@tonic-gate 	 * Get the interrupts property.
760Sstevel@tonic-gate 	 */
770Sstevel@tonic-gate 	if (ddi_getlongprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
786313Skrishnae 	    "interrupts", (caddr_t)&px_p->px_inos,
796313Skrishnae 	    &px_p->px_inos_len) != DDI_SUCCESS) {
800Sstevel@tonic-gate 
810Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d: no interrupts property\n",
826313Skrishnae 		    ddi_driver_name(dip), ddi_get_instance(dip));
830Sstevel@tonic-gate 		return (DDI_FAILURE);
840Sstevel@tonic-gate 	}
850Sstevel@tonic-gate 
860Sstevel@tonic-gate 	/*
870Sstevel@tonic-gate 	 * figure out number of interrupts in the "interrupts" property
880Sstevel@tonic-gate 	 * and convert them all into ino.
890Sstevel@tonic-gate 	 */
900Sstevel@tonic-gate 	i = ddi_getprop(DDI_DEV_T_ANY, dip, 0, "#interrupt-cells", 1);
910Sstevel@tonic-gate 	i = CELLS_1275_TO_BYTES(i);
920Sstevel@tonic-gate 	no_of_intrs = px_p->px_inos_len / i;
930Sstevel@tonic-gate 	for (i = 0; i < no_of_intrs; i++)
940Sstevel@tonic-gate 		px_p->px_inos[i] = px_p->px_inos[i] & 0x3F;
950Sstevel@tonic-gate 
960Sstevel@tonic-gate 	/*
970Sstevel@tonic-gate 	 * Get the ranges property.
980Sstevel@tonic-gate 	 */
990Sstevel@tonic-gate 	if (ddi_getlongprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, "ranges",
1006313Skrishnae 	    (caddr_t)&px_p->px_ranges_p, &px_p->px_ranges_length) !=
1016313Skrishnae 	    DDI_SUCCESS) {
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d: no ranges property\n",
1046313Skrishnae 		    ddi_driver_name(dip), ddi_get_instance(dip));
1050Sstevel@tonic-gate 		kmem_free(px_p->px_inos, px_p->px_inos_len);
1060Sstevel@tonic-gate 		return (DDI_FAILURE);
1070Sstevel@tonic-gate 	}
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate 	return (DDI_SUCCESS);
1100Sstevel@tonic-gate }
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate /*
1130Sstevel@tonic-gate  * px_free_props:
1140Sstevel@tonic-gate  *
1150Sstevel@tonic-gate  * This routine frees the memory used to cache the "interrupts"
1160Sstevel@tonic-gate  * and "ranges" properties of the pci bus device node.
1170Sstevel@tonic-gate  *
1180Sstevel@tonic-gate  * used by: px_detach()
1190Sstevel@tonic-gate  *
1200Sstevel@tonic-gate  * return value: none
1210Sstevel@tonic-gate  */
1220Sstevel@tonic-gate void
px_free_props(px_t * px_p)1230Sstevel@tonic-gate px_free_props(px_t *px_p)
1240Sstevel@tonic-gate {
1250Sstevel@tonic-gate 	kmem_free(px_p->px_inos, px_p->px_inos_len);
1260Sstevel@tonic-gate 	kmem_free(px_p->px_ranges_p, px_p->px_ranges_length);
1270Sstevel@tonic-gate }
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate /*
1300Sstevel@tonic-gate  * px_reloc_reg
1310Sstevel@tonic-gate  *
1320Sstevel@tonic-gate  * If the "reg" entry (*px_rp) is relocatable, lookup "assigned-addresses"
1330Sstevel@tonic-gate  * property to fetch corresponding relocated address.
1340Sstevel@tonic-gate  *
1350Sstevel@tonic-gate  * used by: px_map()
1360Sstevel@tonic-gate  *
1370Sstevel@tonic-gate  * return value:
1380Sstevel@tonic-gate  *
1390Sstevel@tonic-gate  *	DDI_SUCCESS		- on success
1400Sstevel@tonic-gate  *	DDI_ME_INVAL		- regspec is invalid
1410Sstevel@tonic-gate  */
1420Sstevel@tonic-gate int
px_reloc_reg(dev_info_t * dip,dev_info_t * rdip,px_t * px_p,pci_regspec_t * rp)1430Sstevel@tonic-gate px_reloc_reg(dev_info_t *dip, dev_info_t *rdip, px_t *px_p,
1440Sstevel@tonic-gate 	pci_regspec_t *rp)
1450Sstevel@tonic-gate {
1460Sstevel@tonic-gate 	int assign_len, assign_entries, i;
1470Sstevel@tonic-gate 	pci_regspec_t *assign_p;
1480Sstevel@tonic-gate 	uint32_t phys_hi = rp->pci_phys_hi;
1490Sstevel@tonic-gate 	uint32_t space_type = phys_hi & PCI_REG_ADDR_M;	/* 28-bit */
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate 	DBG(DBG_MAP | DBG_CONT, dip, "\tpx_reloc_reg fr: %x.%x.%x %x.%x\n",
1526313Skrishnae 	    rp->pci_phys_hi, rp->pci_phys_mid, rp->pci_phys_low,
1536313Skrishnae 	    rp->pci_size_hi, rp->pci_size_low);
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate 	if (space_type == PCI_ADDR_CONFIG || phys_hi & PCI_RELOCAT_B)
1560Sstevel@tonic-gate 		return (DDI_SUCCESS);
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate 	/*
1590Sstevel@tonic-gate 	 * Hot plug will be taken care of later
1600Sstevel@tonic-gate 	 * if (px_p->hotplug_capable == B_FALSE)
1610Sstevel@tonic-gate 	 */
1620Sstevel@tonic-gate 	{
1630Sstevel@tonic-gate 		uint32_t bus = PCI_REG_BUS_G(phys_hi);
1640Sstevel@tonic-gate 		if (bus < px_p->px_bus_range.lo ||
1650Sstevel@tonic-gate 		    bus > px_p->px_bus_range.hi) {
1660Sstevel@tonic-gate 			DBG(DBG_MAP | DBG_CONT, dip, "bad bus# (%x)\n", bus);
1670Sstevel@tonic-gate 			return (DDI_ME_INVAL);
1680Sstevel@tonic-gate 		}
1690Sstevel@tonic-gate 	}
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate 	i = ddi_getlongprop(DDI_DEV_T_ANY, rdip, DDI_PROP_DONTPASS,
1728413SDaniel.Ice@Sun.COM 	    "assigned-addresses", (caddr_t)&assign_p, &assign_len);
17310923SEvan.Yan@Sun.COM 
1740Sstevel@tonic-gate 	if (i) {
1750Sstevel@tonic-gate 		DBG(DBG_MAP | DBG_CONT, dip, "%s%d: assigned-addresses %d\n",
1768413SDaniel.Ice@Sun.COM 		    ddi_driver_name(rdip), ddi_get_instance(rdip), i);
1770Sstevel@tonic-gate 		return (DDI_ME_INVAL);
1780Sstevel@tonic-gate 	}
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate 	assign_entries = assign_len / sizeof (pci_regspec_t);
1810Sstevel@tonic-gate 	for (i = 0; i < assign_entries; i++, assign_p++) {
1820Sstevel@tonic-gate 		uint32_t assign_type = assign_p->pci_phys_hi & PCI_REG_ADDR_M;
1830Sstevel@tonic-gate 		uint32_t assign_addr = PCI_REG_BDFR_G(assign_p->pci_phys_hi);
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate 		if (PCI_REG_BDFR_G(phys_hi) != assign_addr)
1860Sstevel@tonic-gate 			continue;
1870Sstevel@tonic-gate 		if (space_type == assign_type) { /* exact match */
1880Sstevel@tonic-gate 			rp->pci_phys_low += assign_p->pci_phys_low;
1893549Sgovinda 			if (space_type == PCI_ADDR_MEM64)
1903549Sgovinda 				rp->pci_phys_mid += assign_p->pci_phys_mid;
1910Sstevel@tonic-gate 			break;
1920Sstevel@tonic-gate 		}
1930Sstevel@tonic-gate 		if (space_type == PCI_ADDR_MEM64 &&
1948413SDaniel.Ice@Sun.COM 		    assign_type == PCI_ADDR_MEM32) {
1950Sstevel@tonic-gate 			rp->pci_phys_low += assign_p->pci_phys_low;
1960Sstevel@tonic-gate 			rp->pci_phys_hi ^= PCI_ADDR_MEM64 ^ PCI_ADDR_MEM32;
1970Sstevel@tonic-gate 			break;
1980Sstevel@tonic-gate 		}
1990Sstevel@tonic-gate 	}
2000Sstevel@tonic-gate 	kmem_free(assign_p - i, assign_len);
2010Sstevel@tonic-gate 	DBG(DBG_MAP | DBG_CONT, dip, "\tpx_reloc_reg to: %x.%x.%x %x.%x <%d>\n",
2028413SDaniel.Ice@Sun.COM 	    rp->pci_phys_hi, rp->pci_phys_mid, rp->pci_phys_low,
2038413SDaniel.Ice@Sun.COM 	    rp->pci_size_hi, rp->pci_size_low, i);
2040Sstevel@tonic-gate 	return (i < assign_entries ? DDI_SUCCESS : DDI_ME_INVAL);
2050Sstevel@tonic-gate }
2060Sstevel@tonic-gate 
2072053Sschwartz /*
2082053Sschwartz  * use "ranges" to translate relocated pci regspec into parent space
2092053Sschwartz  */
2100Sstevel@tonic-gate int
px_xlate_reg(px_t * px_p,pci_regspec_t * px_rp,struct regspec * new_rp)2112053Sschwartz px_xlate_reg(px_t *px_p, pci_regspec_t *px_rp, struct regspec *new_rp)
2120Sstevel@tonic-gate {
2132053Sschwartz 	int n;
21410923SEvan.Yan@Sun.COM 	pci_ranges_t *rng_p = px_p->px_ranges_p;
21510923SEvan.Yan@Sun.COM 	int rng_n = px_p->px_ranges_length / sizeof (pci_ranges_t);
2163549Sgovinda 	uint32_t space_type = PCI_REG_ADDR_G(px_rp->pci_phys_hi);
2173549Sgovinda 	uint64_t reg_begin, reg_end, reg_sz;
2183549Sgovinda 	uint64_t rng_begin, rng_end, rng_sz;
2193549Sgovinda 	uint64_t addr;
2202053Sschwartz 
2213549Sgovinda 	reg_begin = (uint64_t)px_rp->pci_phys_mid << 32 | px_rp->pci_phys_low;
2223549Sgovinda 	reg_sz = (uint64_t)px_rp->pci_size_hi << 32 | px_rp->pci_size_low;
2232053Sschwartz 	if (space_type == PCI_REG_ADDR_G(PCI_ADDR_CONFIG)) {
2242053Sschwartz 		if (reg_begin > PCI_CONF_HDR_SIZE)
2252053Sschwartz 			return (DDI_ME_INVAL);
2263549Sgovinda 		reg_sz = reg_sz ? MIN(reg_sz, PCI_CONF_HDR_SIZE) :
2273549Sgovinda 		    PCI_CONF_HDR_SIZE;
2282053Sschwartz 		reg_begin += px_rp->pci_phys_hi << 4;
2292053Sschwartz 	}
2303549Sgovinda 	reg_end = reg_begin + reg_sz - 1;
2312053Sschwartz 
2320Sstevel@tonic-gate 	for (n = 0; n < rng_n; n++, rng_p++) {
2330Sstevel@tonic-gate 		if (space_type != PCI_REG_ADDR_G(rng_p->child_high))
2340Sstevel@tonic-gate 			continue;	/* not the same space type */
2350Sstevel@tonic-gate 
2363549Sgovinda 		rng_begin = (uint64_t)rng_p->child_mid << 32 | rng_p->child_low;
2373549Sgovinda 		rng_sz = (uint64_t)rng_p->size_high << 32 | rng_p->size_low;
2380Sstevel@tonic-gate 		if (space_type == PCI_REG_ADDR_G(PCI_ADDR_CONFIG))
2390Sstevel@tonic-gate 			rng_begin += rng_p->child_high;
2400Sstevel@tonic-gate 
2413549Sgovinda 		rng_end = rng_begin + rng_sz - 1;
2420Sstevel@tonic-gate 		if (reg_begin >= rng_begin && reg_end <= rng_end)
2430Sstevel@tonic-gate 			break;
2440Sstevel@tonic-gate 	}
2450Sstevel@tonic-gate 	if (n >= rng_n)
2460Sstevel@tonic-gate 		return (DDI_ME_REGSPEC_RANGE);
2470Sstevel@tonic-gate 
2483549Sgovinda 	addr = reg_begin - rng_begin + ((uint64_t)rng_p->parent_high << 32 |
2493549Sgovinda 	    rng_p->parent_low);
2503549Sgovinda 	new_rp->regspec_addr = (uint32_t)addr;
2513549Sgovinda 	new_rp->regspec_bustype = (uint32_t)(addr >> 32);
2523549Sgovinda 	new_rp->regspec_size = (uint32_t)reg_sz;
2532053Sschwartz 	DBG(DBG_MAP | DBG_CONT, px_p->px_dip,
2548413SDaniel.Ice@Sun.COM 	    "\tpx_xlate_reg: entry %d new_rp %x.%x %x\n",
2558413SDaniel.Ice@Sun.COM 	    n, new_rp->regspec_bustype, new_rp->regspec_addr, reg_sz);
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate 	return (DDI_SUCCESS);
2580Sstevel@tonic-gate }
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate /*
2610Sstevel@tonic-gate  * px_report_dev
2620Sstevel@tonic-gate  *
2630Sstevel@tonic-gate  * This function is called from our control ops routine on a
2640Sstevel@tonic-gate  * DDI_CTLOPS_REPORTDEV request.
2650Sstevel@tonic-gate  *
2660Sstevel@tonic-gate  * The display format is
2670Sstevel@tonic-gate  *
2680Sstevel@tonic-gate  *	<name><inst> at <pname><pinst> device <dev> function <func>
2690Sstevel@tonic-gate  *
2700Sstevel@tonic-gate  * where
2710Sstevel@tonic-gate  *
2720Sstevel@tonic-gate  *	<name>		this device's name property
2730Sstevel@tonic-gate  *	<inst>		this device's instance number
2740Sstevel@tonic-gate  *	<name>		parent device's name property
2750Sstevel@tonic-gate  *	<inst>		parent device's instance number
2760Sstevel@tonic-gate  *	<dev>		this device's device number
2770Sstevel@tonic-gate  *	<func>		this device's function number
2780Sstevel@tonic-gate  */
2790Sstevel@tonic-gate int
px_report_dev(dev_info_t * dip)2800Sstevel@tonic-gate px_report_dev(dev_info_t *dip)
2810Sstevel@tonic-gate {
2820Sstevel@tonic-gate 	if (dip == (dev_info_t *)0)
2830Sstevel@tonic-gate 		return (DDI_FAILURE);
2840Sstevel@tonic-gate 	cmn_err(CE_CONT, "?PCI Express-device: %s@%s, %s%d\n",
2850Sstevel@tonic-gate 	    ddi_node_name(dip), ddi_get_name_addr(dip),
2860Sstevel@tonic-gate 	    ddi_driver_name(dip),
2870Sstevel@tonic-gate 	    ddi_get_instance(dip));
2880Sstevel@tonic-gate 	return (DDI_SUCCESS);
2890Sstevel@tonic-gate }
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate /*
2930Sstevel@tonic-gate  * reg property for pcimem nodes that covers the entire address
2940Sstevel@tonic-gate  * space for the node:  config, io, or memory.
2950Sstevel@tonic-gate  */
2960Sstevel@tonic-gate pci_regspec_t pci_pcimem_reg[3] =
2970Sstevel@tonic-gate {
2980Sstevel@tonic-gate 	{PCI_ADDR_CONFIG,			0, 0, 0, 0x800000	},
2990Sstevel@tonic-gate 	{(uint_t)(PCI_ADDR_IO|PCI_RELOCAT_B),	0, 0, 0, PX_IO_SIZE	},
3000Sstevel@tonic-gate 	{(uint_t)(PCI_ADDR_MEM32|PCI_RELOCAT_B), 0, 0, 0, PX_MEM_SIZE	}
3010Sstevel@tonic-gate };
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate /*
3040Sstevel@tonic-gate  * px_name_child
3050Sstevel@tonic-gate  *
3060Sstevel@tonic-gate  * This function is called from init_child to name a node. It is
3070Sstevel@tonic-gate  * also passed as a callback for node merging functions.
3080Sstevel@tonic-gate  *
3090Sstevel@tonic-gate  * return value: DDI_SUCCESS, DDI_FAILURE
3100Sstevel@tonic-gate  */
3110Sstevel@tonic-gate static int
px_name_child(dev_info_t * child,char * name,int namelen)3120Sstevel@tonic-gate px_name_child(dev_info_t *child, char *name, int namelen)
3130Sstevel@tonic-gate {
3140Sstevel@tonic-gate 	pci_regspec_t *pci_rp;
3150Sstevel@tonic-gate 	int reglen;
3160Sstevel@tonic-gate 	uint_t func;
3170Sstevel@tonic-gate 	char **unit_addr;
3180Sstevel@tonic-gate 	uint_t n;
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate 	/*
3210Sstevel@tonic-gate 	 * Set the address portion of the node name based on
3220Sstevel@tonic-gate 	 * unit-address property, if it exists.
3230Sstevel@tonic-gate 	 * The interpretation of the unit-address is DD[,F]
3240Sstevel@tonic-gate 	 * where DD is the device id and F is the function.
3250Sstevel@tonic-gate 	 */
3260Sstevel@tonic-gate 	if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY, child,
3270Sstevel@tonic-gate 	    DDI_PROP_DONTPASS, "unit-address", &unit_addr, &n) ==
3280Sstevel@tonic-gate 	    DDI_PROP_SUCCESS) {
3290Sstevel@tonic-gate 		if (n != 1 || *unit_addr == NULL || **unit_addr == 0) {
3300Sstevel@tonic-gate 			cmn_err(CE_WARN, "unit-address property in %s.conf"
3310Sstevel@tonic-gate 			    " not well-formed", ddi_driver_name(child));
3320Sstevel@tonic-gate 			ddi_prop_free(unit_addr);
3330Sstevel@tonic-gate 			return (DDI_FAILURE);
3340Sstevel@tonic-gate 		}
3350Sstevel@tonic-gate 		(void) snprintf(name, namelen, "%s", *unit_addr);
3360Sstevel@tonic-gate 		ddi_prop_free(unit_addr);
3370Sstevel@tonic-gate 		return (DDI_SUCCESS);
3380Sstevel@tonic-gate 	}
3390Sstevel@tonic-gate 
3400Sstevel@tonic-gate 	/*
3410Sstevel@tonic-gate 	 * The unit-address property is does not exist. Set the address
3420Sstevel@tonic-gate 	 * portion of the node name based on the function and device number.
3430Sstevel@tonic-gate 	 */
3440Sstevel@tonic-gate 	if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, child, DDI_PROP_DONTPASS,
3450Sstevel@tonic-gate 	    "reg", (int **)&pci_rp, (uint_t *)&reglen) == DDI_SUCCESS) {
3460Sstevel@tonic-gate 		if (((reglen * sizeof (int)) % sizeof (pci_regspec_t)) != 0) {
3470Sstevel@tonic-gate 			cmn_err(CE_WARN, "reg property not well-formed");
3480Sstevel@tonic-gate 			return (DDI_FAILURE);
3490Sstevel@tonic-gate 		}
3500Sstevel@tonic-gate 
3510Sstevel@tonic-gate 		func = PCI_REG_FUNC_G(pci_rp[0].pci_phys_hi);
3520Sstevel@tonic-gate 		if (func != 0)
3530Sstevel@tonic-gate 			(void) snprintf(name, namelen, "%x,%x",
3548413SDaniel.Ice@Sun.COM 			    PCI_REG_DEV_G(pci_rp[0].pci_phys_hi), func);
3550Sstevel@tonic-gate 		else
3560Sstevel@tonic-gate 			(void) snprintf(name, namelen, "%x",
3578413SDaniel.Ice@Sun.COM 			    PCI_REG_DEV_G(pci_rp[0].pci_phys_hi));
3580Sstevel@tonic-gate 		ddi_prop_free(pci_rp);
3590Sstevel@tonic-gate 		return (DDI_SUCCESS);
3600Sstevel@tonic-gate 	}
3610Sstevel@tonic-gate 
3620Sstevel@tonic-gate 	cmn_err(CE_WARN, "cannot name pci child '%s'", ddi_node_name(child));
3630Sstevel@tonic-gate 	return (DDI_FAILURE);
3640Sstevel@tonic-gate }
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate int
px_uninit_child(px_t * px_p,dev_info_t * child)3670Sstevel@tonic-gate px_uninit_child(px_t *px_p, dev_info_t *child)
3680Sstevel@tonic-gate {
3690Sstevel@tonic-gate 	DBG(DBG_INIT_CLD, px_p->px_dip,
3700Sstevel@tonic-gate 	    "DDI_CTLOPS_UNINITCHILD: arg=%s%d\n",
3710Sstevel@tonic-gate 	    ddi_driver_name(child), ddi_get_instance(child));
3720Sstevel@tonic-gate 
3730Sstevel@tonic-gate 	ddi_set_name_addr(child, NULL);
3740Sstevel@tonic-gate 	ddi_remove_minor_node(child, NULL);
3758413SDaniel.Ice@Sun.COM 
3768413SDaniel.Ice@Sun.COM 	/*
3778413SDaniel.Ice@Sun.COM 	 * XXX Clear parent private data used as a flag to disable
3788413SDaniel.Ice@Sun.COM 	 * iommu BDF protection
3798413SDaniel.Ice@Sun.COM 	 */
3808413SDaniel.Ice@Sun.COM 	if ((intptr_t)ddi_get_parent_data(child) == 1)
3818413SDaniel.Ice@Sun.COM 		ddi_set_parent_data(child, NULL);
3828413SDaniel.Ice@Sun.COM 
3830Sstevel@tonic-gate 	impl_rem_dev_props(child);
38427Sjchu 
38527Sjchu 	DBG(DBG_PWR, ddi_get_parent(child), "\n\n");
3860Sstevel@tonic-gate 
38727Sjchu 	pcie_uninitchild(child);
38827Sjchu 
38927Sjchu 	return (DDI_SUCCESS);
3900Sstevel@tonic-gate }
3910Sstevel@tonic-gate 
3920Sstevel@tonic-gate /*
3930Sstevel@tonic-gate  * px_init_child
3940Sstevel@tonic-gate  *
3950Sstevel@tonic-gate  * This function is called from our control ops routine on a
3960Sstevel@tonic-gate  * DDI_CTLOPS_INITCHILD request.  It builds and sets the device's
3970Sstevel@tonic-gate  * parent private data area.
3980Sstevel@tonic-gate  *
3990Sstevel@tonic-gate  * used by: pci_ctlops()
4000Sstevel@tonic-gate  *
4010Sstevel@tonic-gate  * return value: none
4020Sstevel@tonic-gate  */
4030Sstevel@tonic-gate int
px_init_child(px_t * px_p,dev_info_t * child)4040Sstevel@tonic-gate px_init_child(px_t *px_p, dev_info_t *child)
4050Sstevel@tonic-gate {
40627Sjchu 	dev_info_t	*parent_dip = px_p->px_dip;
40727Sjchu 	pci_regspec_t	*pci_rp;
40827Sjchu 	char		name[10];
40927Sjchu 	int		i, no_config;
4108413SDaniel.Ice@Sun.COM 	intptr_t	ppd = NULL;
4110Sstevel@tonic-gate 
4120Sstevel@tonic-gate 	/*
4130Sstevel@tonic-gate 	 * The following is a special case for pcimem nodes.
4140Sstevel@tonic-gate 	 * For these nodes we create a reg property with a
4150Sstevel@tonic-gate 	 * single entry that covers the entire address space
4160Sstevel@tonic-gate 	 * for the node (config, io or memory).
4170Sstevel@tonic-gate 	 */
4180Sstevel@tonic-gate 	if (strcmp(ddi_driver_name(child), "pcimem") == 0) {
4190Sstevel@tonic-gate 		(void) ddi_prop_create(DDI_DEV_T_NONE, child,
4200Sstevel@tonic-gate 		    DDI_PROP_CANSLEEP, "reg", (caddr_t)pci_pcimem_reg,
4210Sstevel@tonic-gate 		    sizeof (pci_pcimem_reg));
4220Sstevel@tonic-gate 		ddi_set_name_addr(child, "0");
4230Sstevel@tonic-gate 		ddi_set_parent_data(child, NULL);
4240Sstevel@tonic-gate 		return (DDI_SUCCESS);
4250Sstevel@tonic-gate 	}
4260Sstevel@tonic-gate 
4270Sstevel@tonic-gate 	/*
4280Sstevel@tonic-gate 	 * Check whether the node has config space or is a hard decode
4290Sstevel@tonic-gate 	 * node (possibly created by a driver.conf file).
4300Sstevel@tonic-gate 	 */
4310Sstevel@tonic-gate 	no_config = ddi_prop_get_int(DDI_DEV_T_ANY, child, DDI_PROP_DONTPASS,
4320Sstevel@tonic-gate 	    "no-config", 0);
4330Sstevel@tonic-gate 
4340Sstevel@tonic-gate 	/*
4358413SDaniel.Ice@Sun.COM 	 * XXX set ppd to 1 to disable iommu BDF protection
4368413SDaniel.Ice@Sun.COM 	 * It relies on unused parent private data for PCI devices.
4378413SDaniel.Ice@Sun.COM 	 */
4388413SDaniel.Ice@Sun.COM 	if (ddi_prop_exists(DDI_DEV_T_NONE, child, DDI_PROP_DONTPASS,
4398413SDaniel.Ice@Sun.COM 	    "dvma-share"))
4408413SDaniel.Ice@Sun.COM 		ppd = 1;
4418413SDaniel.Ice@Sun.COM 
4428413SDaniel.Ice@Sun.COM 	/*
4430Sstevel@tonic-gate 	 * Pseudo nodes indicate a prototype node with per-instance
4440Sstevel@tonic-gate 	 * properties to be merged into the real h/w device node.
4450Sstevel@tonic-gate 	 * However, do not merge if the no-config property is set
4460Sstevel@tonic-gate 	 * (see PSARC 2000/088).
4470Sstevel@tonic-gate 	 */
4480Sstevel@tonic-gate 	if ((ndi_dev_is_persistent_node(child) == 0) && (no_config == 0)) {
4490Sstevel@tonic-gate 		extern int pci_allow_pseudo_children;
4500Sstevel@tonic-gate 
4510Sstevel@tonic-gate 		if (ddi_getlongprop(DDI_DEV_T_ANY, child,
4520Sstevel@tonic-gate 		    DDI_PROP_DONTPASS, "reg", (caddr_t)&pci_rp, &i) ==
4530Sstevel@tonic-gate 		    DDI_SUCCESS) {
4540Sstevel@tonic-gate 			cmn_err(CE_WARN, "cannot merge prototype from %s.conf",
4550Sstevel@tonic-gate 			    ddi_driver_name(child));
4560Sstevel@tonic-gate 			kmem_free(pci_rp, i);
4570Sstevel@tonic-gate 			return (DDI_NOT_WELL_FORMED);
4580Sstevel@tonic-gate 		}
4590Sstevel@tonic-gate 		/*
4600Sstevel@tonic-gate 		 * Name the child
4610Sstevel@tonic-gate 		 */
4620Sstevel@tonic-gate 		if (px_name_child(child, name, 10) != DDI_SUCCESS)
4630Sstevel@tonic-gate 			return (DDI_FAILURE);
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate 		ddi_set_name_addr(child, name);
4668413SDaniel.Ice@Sun.COM 		ddi_set_parent_data(child, (void *)ppd);
4670Sstevel@tonic-gate 
4680Sstevel@tonic-gate 		/*
4690Sstevel@tonic-gate 		 * Try to merge the properties from this prototype
4700Sstevel@tonic-gate 		 * node into real h/w nodes.
4710Sstevel@tonic-gate 		 */
4720Sstevel@tonic-gate 		if (ndi_merge_node(child, px_name_child) == DDI_SUCCESS) {
4730Sstevel@tonic-gate 			/*
4740Sstevel@tonic-gate 			 * Merged ok - return failure to remove the node.
4750Sstevel@tonic-gate 			 */
4760Sstevel@tonic-gate 			ddi_set_name_addr(child, NULL);
4770Sstevel@tonic-gate 			return (DDI_FAILURE);
4780Sstevel@tonic-gate 		}
4790Sstevel@tonic-gate 
4800Sstevel@tonic-gate 		/* workaround for ddivs to run under PCI */
4810Sstevel@tonic-gate 		if (pci_allow_pseudo_children)
4820Sstevel@tonic-gate 			return (DDI_SUCCESS);
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate 		cmn_err(CE_WARN, "!%s@%s: %s.conf properties not merged",
4850Sstevel@tonic-gate 		    ddi_driver_name(child), ddi_get_name_addr(child),
4860Sstevel@tonic-gate 		    ddi_driver_name(child));
4870Sstevel@tonic-gate 		ddi_set_name_addr(child, NULL);
4880Sstevel@tonic-gate 		return (DDI_NOT_WELL_FORMED);
4890Sstevel@tonic-gate 	}
4900Sstevel@tonic-gate 
4910Sstevel@tonic-gate 	if (px_name_child(child, name, 10) != DDI_SUCCESS)
4920Sstevel@tonic-gate 		return (DDI_FAILURE);
4930Sstevel@tonic-gate 	ddi_set_name_addr(child, name);
4940Sstevel@tonic-gate 
4950Sstevel@tonic-gate 	if (no_config != 0) {
4960Sstevel@tonic-gate 		/*
4970Sstevel@tonic-gate 		 * There is no config space so there's nothing more to do.
4980Sstevel@tonic-gate 		 */
4990Sstevel@tonic-gate 		return (DDI_SUCCESS);
5000Sstevel@tonic-gate 	}
5010Sstevel@tonic-gate 
50227Sjchu 	if (pcie_pm_hold(parent_dip) != DDI_SUCCESS) {
50327Sjchu 		DBG(DBG_PWR, parent_dip,
5040Sstevel@tonic-gate 		    "INITCHILD: px_pm_hold failed\n");
5050Sstevel@tonic-gate 		return (DDI_FAILURE);
5060Sstevel@tonic-gate 	}
5070Sstevel@tonic-gate 	/* Any return of DDI_FAILURE after this must call px_pm_release */
5080Sstevel@tonic-gate 
5090Sstevel@tonic-gate 	/*
5100Sstevel@tonic-gate 	 * If configuration registers were previously saved by
5110Sstevel@tonic-gate 	 * child (before it went to D3), then let the child do the
5120Sstevel@tonic-gate 	 * restore to set up the config regs as it'll first need to
5130Sstevel@tonic-gate 	 * power the device out of D3.
5140Sstevel@tonic-gate 	 */
5150Sstevel@tonic-gate 	if (ddi_prop_exists(DDI_DEV_T_ANY, child, DDI_PROP_DONTPASS,
5160Sstevel@tonic-gate 	    "config-regs-saved-by-child") == 1) {
5170Sstevel@tonic-gate 		DBG(DBG_PWR, child,
5180Sstevel@tonic-gate 		    "INITCHILD: config regs to be restored by child\n");
5190Sstevel@tonic-gate 
5200Sstevel@tonic-gate 		return (DDI_SUCCESS);
5210Sstevel@tonic-gate 	}
5220Sstevel@tonic-gate 
52327Sjchu 	DBG(DBG_PWR, parent_dip,
5240Sstevel@tonic-gate 	    "INITCHILD: config regs setup for %s@%s\n",
5250Sstevel@tonic-gate 	    ddi_node_name(child), ddi_get_name_addr(child));
5260Sstevel@tonic-gate 
5278413SDaniel.Ice@Sun.COM 	ddi_set_parent_data(child, (void *)ppd);
528*11596SJason.Beloro@Sun.COM 	pcie_init_dom(child);
52911245SZhijun.Fu@Sun.COM 	(void) pcie_initchild(child);
5300Sstevel@tonic-gate 
5310Sstevel@tonic-gate 	/*
5320Sstevel@tonic-gate 	 * Handle chip specific init-child tasks.
5330Sstevel@tonic-gate 	 */
53427Sjchu 	pcie_pm_release(parent_dip);
5350Sstevel@tonic-gate 
5360Sstevel@tonic-gate 	return (DDI_SUCCESS);
5370Sstevel@tonic-gate }
5380Sstevel@tonic-gate 
5390Sstevel@tonic-gate /*
5400Sstevel@tonic-gate  * px_get_reg_set_size
5410Sstevel@tonic-gate  *
5420Sstevel@tonic-gate  * Given a dev info pointer to a pci child and a register number, this
5430Sstevel@tonic-gate  * routine returns the size element of that reg set property.
5440Sstevel@tonic-gate  *
5450Sstevel@tonic-gate  * used by: pci_ctlops() - DDI_CTLOPS_REGSIZE
5460Sstevel@tonic-gate  *
54727Sjchu  * return value: size of reg set on success, 0 on error
5480Sstevel@tonic-gate  */
5490Sstevel@tonic-gate off_t
px_get_reg_set_size(dev_info_t * child,int rnumber)5500Sstevel@tonic-gate px_get_reg_set_size(dev_info_t *child, int rnumber)
5510Sstevel@tonic-gate {
5520Sstevel@tonic-gate 	pci_regspec_t *pci_rp;
55327Sjchu 	off_t size = 0;
5540Sstevel@tonic-gate 	int i;
5550Sstevel@tonic-gate 
5560Sstevel@tonic-gate 	if (rnumber < 0)
55727Sjchu 		return (0);
5580Sstevel@tonic-gate 
5590Sstevel@tonic-gate 	/*
5600Sstevel@tonic-gate 	 * Get the reg property for the device.
5610Sstevel@tonic-gate 	 */
5620Sstevel@tonic-gate 	if (ddi_getlongprop(DDI_DEV_T_ANY, child, DDI_PROP_DONTPASS, "reg",
5630Sstevel@tonic-gate 	    (caddr_t)&pci_rp, &i) != DDI_SUCCESS)
56427Sjchu 		return (0);
5650Sstevel@tonic-gate 
5660Sstevel@tonic-gate 	if (rnumber >= (i / (int)sizeof (pci_regspec_t)))
5670Sstevel@tonic-gate 		goto done;
5680Sstevel@tonic-gate 
5690Sstevel@tonic-gate 	size = pci_rp[rnumber].pci_size_low |
5706313Skrishnae 	    ((uint64_t)pci_rp[rnumber].pci_size_hi << 32);
5710Sstevel@tonic-gate done:
5720Sstevel@tonic-gate 	kmem_free(pci_rp, i);
5730Sstevel@tonic-gate 	return (size);
5740Sstevel@tonic-gate }
5750Sstevel@tonic-gate 
5760Sstevel@tonic-gate 
5770Sstevel@tonic-gate /*
5780Sstevel@tonic-gate  * px_get_nreg_set
5790Sstevel@tonic-gate  *
5800Sstevel@tonic-gate  * Given a dev info pointer to a pci child, this routine returns the
5810Sstevel@tonic-gate  * number of sets in its "reg" property.
5820Sstevel@tonic-gate  *
5830Sstevel@tonic-gate  * used by: pci_ctlops() - DDI_CTLOPS_NREGS
5840Sstevel@tonic-gate  *
5850Sstevel@tonic-gate  * return value: # of reg sets on success, zero on error
5860Sstevel@tonic-gate  */
5870Sstevel@tonic-gate uint_t
px_get_nreg_set(dev_info_t * child)5880Sstevel@tonic-gate px_get_nreg_set(dev_info_t *child)
5890Sstevel@tonic-gate {
5900Sstevel@tonic-gate 	pci_regspec_t *pci_rp;
5910Sstevel@tonic-gate 	int i, n;
5920Sstevel@tonic-gate 
5930Sstevel@tonic-gate 	/*
5940Sstevel@tonic-gate 	 * Get the reg property for the device.
5950Sstevel@tonic-gate 	 */
5960Sstevel@tonic-gate 	if (ddi_getlongprop(DDI_DEV_T_ANY, child, DDI_PROP_DONTPASS, "reg",
5970Sstevel@tonic-gate 	    (caddr_t)&pci_rp, &i) != DDI_SUCCESS)
5980Sstevel@tonic-gate 		return (0);
5990Sstevel@tonic-gate 
6000Sstevel@tonic-gate 	n = i / (int)sizeof (pci_regspec_t);
6010Sstevel@tonic-gate 	kmem_free(pci_rp, i);
6020Sstevel@tonic-gate 	return (n);
6030Sstevel@tonic-gate }
6040Sstevel@tonic-gate 
6050Sstevel@tonic-gate 
6060Sstevel@tonic-gate /*
6070Sstevel@tonic-gate  * px_get_nintr
6080Sstevel@tonic-gate  *
6090Sstevel@tonic-gate  * Given a dev info pointer to a pci child, this routine returns the
6100Sstevel@tonic-gate  * number of items in its "interrupts" property.
6110Sstevel@tonic-gate  *
6120Sstevel@tonic-gate  * used by: pci_ctlops() - DDI_CTLOPS_NREGS
6130Sstevel@tonic-gate  *
6140Sstevel@tonic-gate  * return value: # of interrupts on success, zero on error
6150Sstevel@tonic-gate  */
6160Sstevel@tonic-gate uint_t
px_get_nintr(dev_info_t * child)6170Sstevel@tonic-gate px_get_nintr(dev_info_t *child)
6180Sstevel@tonic-gate {
6190Sstevel@tonic-gate 	int *pci_ip;
6200Sstevel@tonic-gate 	int i, n;
6210Sstevel@tonic-gate 
6220Sstevel@tonic-gate 	if (ddi_getlongprop(DDI_DEV_T_ANY, child, DDI_PROP_DONTPASS,
6230Sstevel@tonic-gate 	    "interrupts", (caddr_t)&pci_ip, &i) != DDI_SUCCESS)
6240Sstevel@tonic-gate 		return (0);
6250Sstevel@tonic-gate 
6260Sstevel@tonic-gate 	n = i / (int)sizeof (uint_t);
6270Sstevel@tonic-gate 	kmem_free(pci_ip, i);
6280Sstevel@tonic-gate 	return (n);
6290Sstevel@tonic-gate }
6300Sstevel@tonic-gate 
6310Sstevel@tonic-gate uint64_t
px_get_cfg_pabase(px_t * px_p)6320Sstevel@tonic-gate px_get_cfg_pabase(px_t *px_p)
6330Sstevel@tonic-gate {
6340Sstevel@tonic-gate 	int i;
63510923SEvan.Yan@Sun.COM 	pci_ranges_t *rangep = px_p->px_ranges_p;
63610923SEvan.Yan@Sun.COM 	int nrange = px_p->px_ranges_length / sizeof (pci_ranges_t);
6370Sstevel@tonic-gate 	uint32_t cfg_space_type = PCI_REG_ADDR_G(PCI_ADDR_CONFIG);
6380Sstevel@tonic-gate 
6390Sstevel@tonic-gate 	ASSERT(cfg_space_type == 0);
6400Sstevel@tonic-gate 
6410Sstevel@tonic-gate 	for (i = 0; i < nrange; i++, rangep++) {
6420Sstevel@tonic-gate 		if (PCI_REG_ADDR_G(rangep->child_high) == cfg_space_type)
6430Sstevel@tonic-gate 			break;
6440Sstevel@tonic-gate 	}
6450Sstevel@tonic-gate 
6460Sstevel@tonic-gate 	if (i >= nrange)
647671Skrishnae 		cmn_err(CE_PANIC, "no cfg space in px(%p) ranges prop.\n",
6488413SDaniel.Ice@Sun.COM 		    px_p);
6490Sstevel@tonic-gate 
6500Sstevel@tonic-gate 	return (((uint64_t)rangep->parent_high << 32) | rangep->parent_low);
6510Sstevel@tonic-gate }
6520Sstevel@tonic-gate 
6530Sstevel@tonic-gate /*
6540Sstevel@tonic-gate  * decodes standard PCI config space 16bit error status reg
6550Sstevel@tonic-gate  */
6560Sstevel@tonic-gate int
px_log_cfg_err(dev_info_t * dip,ushort_t status_reg,char * err_msg)6570Sstevel@tonic-gate px_log_cfg_err(dev_info_t *dip, ushort_t status_reg, char *err_msg)
6580Sstevel@tonic-gate {
6590Sstevel@tonic-gate 	int nerr = ddi_get_instance(dip); /* temp for instance */
6600Sstevel@tonic-gate 	uint64_t perr_fatal = px_perr_fatal & (1 << nerr);
6610Sstevel@tonic-gate 	uint64_t serr_fatal = px_serr_fatal & (1 << nerr);
6620Sstevel@tonic-gate 	nerr = 0;
6630Sstevel@tonic-gate 
6640Sstevel@tonic-gate 	if ((status_reg & PCI_STAT_PERROR) && perr_fatal)
6650Sstevel@tonic-gate 		nerr++;
6660Sstevel@tonic-gate 	if ((status_reg & PCI_STAT_S_SYSERR) && serr_fatal)
6670Sstevel@tonic-gate 		nerr++;
6680Sstevel@tonic-gate 	if (status_reg & PCI_STAT_R_MAST_AB)
6690Sstevel@tonic-gate 		nerr++;
6700Sstevel@tonic-gate 	if ((status_reg & PCI_STAT_S_PERROR) && perr_fatal)
6710Sstevel@tonic-gate 		nerr++;
6720Sstevel@tonic-gate 
6730Sstevel@tonic-gate 	cmn_err(CE_WARN, "%s%d: %sPCI Express config space CSR=0x%b",
6740Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip), err_msg,
6750Sstevel@tonic-gate 	    (uint32_t)status_reg, PX_STATUS_BITS);
6760Sstevel@tonic-gate 
6770Sstevel@tonic-gate 	return (nerr);
6780Sstevel@tonic-gate }
679