xref: /onnv-gate/usr/src/uts/common/io/busra.c (revision 11260:eb8c6f2097e8)
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
53446Smrj  * Common Development and Distribution License (the "License").
63446Smrj  * 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 /*
2210923SEvan.Yan@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #if defined(DEBUG)
270Sstevel@tonic-gate #define	BUSRA_DEBUG
280Sstevel@tonic-gate #endif
290Sstevel@tonic-gate 
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate  * This module provides a set of resource management interfaces
320Sstevel@tonic-gate  * to manage bus resources globally in the system.
330Sstevel@tonic-gate  *
340Sstevel@tonic-gate  * The bus nexus drivers are typically responsible to setup resource
350Sstevel@tonic-gate  * maps for the bus resources available for a bus instance. However
360Sstevel@tonic-gate  * this module also provides resource setup functions for PCI bus
370Sstevel@tonic-gate  * (used by both SPARC and X86 platforms) and ISA bus instances (used
380Sstevel@tonic-gate  * only for X86 platforms).
390Sstevel@tonic-gate  */
400Sstevel@tonic-gate 
410Sstevel@tonic-gate #include <sys/types.h>
420Sstevel@tonic-gate #include <sys/systm.h>
430Sstevel@tonic-gate #include <sys/ddi.h>
440Sstevel@tonic-gate #include <sys/sunddi.h>
450Sstevel@tonic-gate #include <sys/sunndi.h>
460Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
470Sstevel@tonic-gate #include <sys/ndi_impldefs.h>
480Sstevel@tonic-gate #include <sys/kmem.h>
490Sstevel@tonic-gate #include <sys/pctypes.h>
500Sstevel@tonic-gate #include <sys/modctl.h>
510Sstevel@tonic-gate #include <sys/debug.h>
520Sstevel@tonic-gate #include <sys/spl.h>
530Sstevel@tonic-gate #include <sys/pci.h>
540Sstevel@tonic-gate #include <sys/autoconf.h>
550Sstevel@tonic-gate 
560Sstevel@tonic-gate #if defined(BUSRA_DEBUG)
570Sstevel@tonic-gate int busra_debug = 0;
580Sstevel@tonic-gate #define	DEBUGPRT \
590Sstevel@tonic-gate 	if (busra_debug) cmn_err
600Sstevel@tonic-gate 
610Sstevel@tonic-gate #else
620Sstevel@tonic-gate #define	DEBUGPRT \
630Sstevel@tonic-gate 	if (0) cmn_err
640Sstevel@tonic-gate #endif
650Sstevel@tonic-gate 
660Sstevel@tonic-gate 
670Sstevel@tonic-gate /*
680Sstevel@tonic-gate  * global mutex that protects the global list of resource maps.
690Sstevel@tonic-gate  */
700Sstevel@tonic-gate kmutex_t ra_lock;
710Sstevel@tonic-gate 
720Sstevel@tonic-gate /*
730Sstevel@tonic-gate  * basic resource element
740Sstevel@tonic-gate  */
750Sstevel@tonic-gate struct ra_resource {
760Sstevel@tonic-gate 	struct ra_resource *ra_next;
770Sstevel@tonic-gate 	uint64_t	ra_base;
780Sstevel@tonic-gate 	uint64_t 	ra_len;
790Sstevel@tonic-gate };
800Sstevel@tonic-gate 
810Sstevel@tonic-gate /*
820Sstevel@tonic-gate  * link list element for the list of dips (and their resource ranges)
830Sstevel@tonic-gate  * for a particular resource type.
840Sstevel@tonic-gate  * ra_rangeset points to the list of resources available
850Sstevel@tonic-gate  * for this type and this dip.
860Sstevel@tonic-gate  */
870Sstevel@tonic-gate struct ra_dip_type  {
880Sstevel@tonic-gate 	struct ra_dip_type *ra_next;
890Sstevel@tonic-gate 	struct ra_resource  *ra_rangeset;
900Sstevel@tonic-gate 	dev_info_t *ra_dip;
910Sstevel@tonic-gate };
920Sstevel@tonic-gate 
930Sstevel@tonic-gate 
940Sstevel@tonic-gate /*
950Sstevel@tonic-gate  * link list element for list of types resources. Each element
960Sstevel@tonic-gate  * has all resources for a particular type.
970Sstevel@tonic-gate  */
980Sstevel@tonic-gate struct ra_type_map {
990Sstevel@tonic-gate 	struct ra_type_map *ra_next;
1000Sstevel@tonic-gate 	struct ra_dip_type *ra_dip_list;
1010Sstevel@tonic-gate 	char *type;
1020Sstevel@tonic-gate };
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate /*
1060Sstevel@tonic-gate  * place holder to keep the head of the whole global list.
1070Sstevel@tonic-gate  * the address of the first typemap would be stored in it.
1080Sstevel@tonic-gate  */
1090Sstevel@tonic-gate static struct ra_type_map	*ra_map_list_head = NULL;
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate /*
1130Sstevel@tonic-gate  * This is the loadable module wrapper.
1140Sstevel@tonic-gate  * It is essentially boilerplate so isn't documented
1150Sstevel@tonic-gate  */
1160Sstevel@tonic-gate extern struct mod_ops mod_miscops;
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate #ifdef BUSRA_DEBUG
1190Sstevel@tonic-gate void ra_dump_all();
1200Sstevel@tonic-gate #endif
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate /* internal function prototypes */
1230Sstevel@tonic-gate static struct ra_dip_type *find_dip_map_resources(dev_info_t *dip, char *type,
1240Sstevel@tonic-gate     struct ra_dip_type ***backdip, struct ra_type_map ***backtype,
1250Sstevel@tonic-gate     uint32_t flag);
1260Sstevel@tonic-gate static int isnot_pow2(uint64_t value);
1270Sstevel@tonic-gate static int claim_pci_busnum(dev_info_t *dip, void *arg);
1280Sstevel@tonic-gate static int ra_map_exist(dev_info_t *dip, char *type);
1290Sstevel@tonic-gate 
13010923SEvan.Yan@Sun.COM static int pci_get_available_prop(dev_info_t *dip, uint64_t base,
13110923SEvan.Yan@Sun.COM     uint64_t len, char *busra_type);
13210923SEvan.Yan@Sun.COM static int pci_put_available_prop(dev_info_t *dip, uint64_t base,
13310923SEvan.Yan@Sun.COM     uint64_t len, char *busra_type);
13410923SEvan.Yan@Sun.COM static uint32_t pci_type_ra2pci(char *type);
13510923SEvan.Yan@Sun.COM static boolean_t is_pcie_fabric(dev_info_t *dip);
13610923SEvan.Yan@Sun.COM 
13710923SEvan.Yan@Sun.COM #define	PCI_ADDR_TYPE_MASK	(PCI_REG_ADDR_M | PCI_REG_PF_M)
13810923SEvan.Yan@Sun.COM #define	PCI_ADDR_TYPE_INVAL	0xffffffff
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate #define	RA_INSERT(prev, el) \
1410Sstevel@tonic-gate 	el->ra_next = *prev; \
1420Sstevel@tonic-gate 	*prev = el;
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate #define	RA_REMOVE(prev, el) \
1450Sstevel@tonic-gate 	*prev = el->ra_next;
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate static struct modlmisc modlmisc = {
1490Sstevel@tonic-gate 	&mod_miscops,		/* Type of module. This one is a module */
1507862SRichard.Bean@Sun.COM 	"Bus Resource Allocator (BUSRA)",	/* Name of the module. */
1510Sstevel@tonic-gate };
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate static struct modlinkage modlinkage = {
1540Sstevel@tonic-gate 	MODREV_1, (void *)&modlmisc, NULL
1550Sstevel@tonic-gate };
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate int
_init()1580Sstevel@tonic-gate _init()
1590Sstevel@tonic-gate {
1600Sstevel@tonic-gate 	int	ret;
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate 	mutex_init(&ra_lock, NULL, MUTEX_DRIVER,
16310923SEvan.Yan@Sun.COM 	    (void *)(intptr_t)__ipltospl(SPL7 - 1));
1640Sstevel@tonic-gate 	if ((ret = mod_install(&modlinkage)) != 0) {
1650Sstevel@tonic-gate 		mutex_destroy(&ra_lock);
1660Sstevel@tonic-gate 	}
1670Sstevel@tonic-gate 	return (ret);
1680Sstevel@tonic-gate }
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate int
_fini()1710Sstevel@tonic-gate _fini()
1720Sstevel@tonic-gate {
1730Sstevel@tonic-gate 	int	ret;
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate 	mutex_enter(&ra_lock);
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 	if (ra_map_list_head != NULL) {
1780Sstevel@tonic-gate 		mutex_exit(&ra_lock);
1790Sstevel@tonic-gate 		return (EBUSY);
1800Sstevel@tonic-gate 	}
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 	ret = mod_remove(&modlinkage);
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate 	mutex_exit(&ra_lock);
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate 	if (ret == 0)
1870Sstevel@tonic-gate 		mutex_destroy(&ra_lock);
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 	return (ret);
1900Sstevel@tonic-gate }
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate int
_info(struct modinfo * modinfop)1930Sstevel@tonic-gate _info(struct modinfo *modinfop)
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate {
1960Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
1970Sstevel@tonic-gate }
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate /*
2000Sstevel@tonic-gate  * set up an empty resource map for a given type and dip
2010Sstevel@tonic-gate  */
2020Sstevel@tonic-gate int
ndi_ra_map_setup(dev_info_t * dip,char * type)2030Sstevel@tonic-gate ndi_ra_map_setup(dev_info_t *dip, char *type)
2040Sstevel@tonic-gate {
2050Sstevel@tonic-gate 	struct ra_type_map  *typemapp;
2060Sstevel@tonic-gate 	struct ra_dip_type  *dipmap;
2070Sstevel@tonic-gate 	struct ra_dip_type  **backdip;
2080Sstevel@tonic-gate 	struct ra_type_map  **backtype;
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate 	mutex_enter(&ra_lock);
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate 	dipmap = find_dip_map_resources(dip, type, &backdip, &backtype, 0);
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate 	if (dipmap == NULL) {
2160Sstevel@tonic-gate 		if (backtype == NULL) {
2170Sstevel@tonic-gate 			typemapp = (struct ra_type_map *)
21810923SEvan.Yan@Sun.COM 			    kmem_zalloc(sizeof (*typemapp), KM_SLEEP);
2190Sstevel@tonic-gate 			typemapp->type = (char *)kmem_zalloc(strlen(type) + 1,
22010923SEvan.Yan@Sun.COM 			    KM_SLEEP);
2210Sstevel@tonic-gate 			(void) strcpy(typemapp->type, type);
2220Sstevel@tonic-gate 			RA_INSERT(&ra_map_list_head, typemapp);
2230Sstevel@tonic-gate 		} else {
2240Sstevel@tonic-gate 			typemapp = *backtype;
2250Sstevel@tonic-gate 		}
2260Sstevel@tonic-gate 		if (backdip == NULL) {
2270Sstevel@tonic-gate 			/* allocate and insert in list of dips for this type */
2280Sstevel@tonic-gate 			dipmap = (struct ra_dip_type *)
22910923SEvan.Yan@Sun.COM 			    kmem_zalloc(sizeof (*dipmap), KM_SLEEP);
2300Sstevel@tonic-gate 			dipmap->ra_dip = dip;
2310Sstevel@tonic-gate 			RA_INSERT(&typemapp->ra_dip_list, dipmap);
2320Sstevel@tonic-gate 		}
2330Sstevel@tonic-gate 	}
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate 	mutex_exit(&ra_lock);
2360Sstevel@tonic-gate 	return (NDI_SUCCESS);
2370Sstevel@tonic-gate }
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate /*
2400Sstevel@tonic-gate  * destroys a resource map for a given dip and type
2410Sstevel@tonic-gate  */
2420Sstevel@tonic-gate int
ndi_ra_map_destroy(dev_info_t * dip,char * type)2430Sstevel@tonic-gate ndi_ra_map_destroy(dev_info_t *dip, char *type)
2440Sstevel@tonic-gate {
2450Sstevel@tonic-gate 	struct ra_dip_type	*dipmap;
2460Sstevel@tonic-gate 	struct ra_dip_type	**backdip;
2470Sstevel@tonic-gate 	struct ra_type_map  	**backtype, *typemap;
2480Sstevel@tonic-gate 	struct ra_resource	*range;
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate 	mutex_enter(&ra_lock);
2510Sstevel@tonic-gate 	dipmap = find_dip_map_resources(dip, type, &backdip, &backtype, 0);
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate 	if (dipmap == NULL) {
2540Sstevel@tonic-gate 		mutex_exit(&ra_lock);
2550Sstevel@tonic-gate 		return (NDI_FAILURE);
2560Sstevel@tonic-gate 	}
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate 	/*
2590Sstevel@tonic-gate 	 * destroy all resources for this dip
2600Sstevel@tonic-gate 	 * remove dip from type list
2610Sstevel@tonic-gate 	 */
2620Sstevel@tonic-gate 	ASSERT((backdip != NULL) && (backtype != NULL));
2630Sstevel@tonic-gate 	while (dipmap->ra_rangeset != NULL) {
2640Sstevel@tonic-gate 		range = dipmap->ra_rangeset;
2650Sstevel@tonic-gate 		RA_REMOVE(&dipmap->ra_rangeset, range);
2660Sstevel@tonic-gate 		kmem_free((caddr_t)range, sizeof (*range));
2670Sstevel@tonic-gate 	}
2680Sstevel@tonic-gate 	/* remove from dip list */
2690Sstevel@tonic-gate 	RA_REMOVE(backdip, dipmap);
2700Sstevel@tonic-gate 	kmem_free((caddr_t)dipmap, sizeof (*dipmap));
2710Sstevel@tonic-gate 	if ((*backtype)->ra_dip_list == NULL) {
2720Sstevel@tonic-gate 		/*
2730Sstevel@tonic-gate 		 * This was the last dip with this resource type.
2740Sstevel@tonic-gate 		 * Remove the type from the global list.
2750Sstevel@tonic-gate 		 */
2760Sstevel@tonic-gate 		typemap = *backtype;
2770Sstevel@tonic-gate 		RA_REMOVE(backtype, (*backtype));
2780Sstevel@tonic-gate 		kmem_free((caddr_t)typemap->type, strlen(typemap->type) + 1);
2790Sstevel@tonic-gate 		kmem_free((caddr_t)typemap, sizeof (*typemap));
2800Sstevel@tonic-gate 	}
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate 	mutex_exit(&ra_lock);
2830Sstevel@tonic-gate 	return (NDI_SUCCESS);
2840Sstevel@tonic-gate }
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate static int
ra_map_exist(dev_info_t * dip,char * type)2870Sstevel@tonic-gate ra_map_exist(dev_info_t *dip, char *type)
2880Sstevel@tonic-gate {
2890Sstevel@tonic-gate 	struct ra_dip_type  **backdip;
2900Sstevel@tonic-gate 	struct ra_type_map  **backtype;
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate 	mutex_enter(&ra_lock);
2930Sstevel@tonic-gate 	if (find_dip_map_resources(dip, type, &backdip, &backtype, 0) == NULL) {
2940Sstevel@tonic-gate 		mutex_exit(&ra_lock);
2950Sstevel@tonic-gate 		return (NDI_FAILURE);
2960Sstevel@tonic-gate 	}
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate 	mutex_exit(&ra_lock);
2990Sstevel@tonic-gate 	return (NDI_SUCCESS);
3000Sstevel@tonic-gate }
3010Sstevel@tonic-gate /*
3020Sstevel@tonic-gate  * Find a dip map for the specified type, if NDI_RA_PASS will go up on dev tree
3030Sstevel@tonic-gate  * if found, backdip and backtype will be updated to point to the previous
3040Sstevel@tonic-gate  * dip in the list and previous type for this dip in the list.
3050Sstevel@tonic-gate  * If no such type at all in the resource list both backdip and backtype
3060Sstevel@tonic-gate  * will be null. If the type found but no dip, back dip will be null.
3070Sstevel@tonic-gate  */
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate static struct ra_dip_type *
find_dip_map_resources(dev_info_t * dip,char * type,struct ra_dip_type *** backdip,struct ra_type_map *** backtype,uint32_t flag)3100Sstevel@tonic-gate find_dip_map_resources(dev_info_t *dip, char *type,
3110Sstevel@tonic-gate     struct ra_dip_type ***backdip, struct ra_type_map ***backtype,
3120Sstevel@tonic-gate     uint32_t flag)
3130Sstevel@tonic-gate {
3140Sstevel@tonic-gate 	struct ra_type_map **prevmap;
3150Sstevel@tonic-gate 	struct ra_dip_type *dipmap, **prevdip;
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate 	ASSERT(mutex_owned(&ra_lock));
3180Sstevel@tonic-gate 	prevdip = NULL;
3190Sstevel@tonic-gate 	dipmap = NULL;
3200Sstevel@tonic-gate 	prevmap = &ra_map_list_head;
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate 	while (*prevmap) {
3230Sstevel@tonic-gate 		if (strcmp((*prevmap)->type, type) == 0)
3240Sstevel@tonic-gate 			break;
3250Sstevel@tonic-gate 		prevmap = &(*prevmap)->ra_next;
3260Sstevel@tonic-gate 	}
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate 	if (*prevmap) {
3290Sstevel@tonic-gate 		for (; dip != NULL; dip = ddi_get_parent(dip)) {
3300Sstevel@tonic-gate 			prevdip = &(*prevmap)->ra_dip_list;
3310Sstevel@tonic-gate 			dipmap = *prevdip;
3320Sstevel@tonic-gate 
3330Sstevel@tonic-gate 			while (dipmap) {
3340Sstevel@tonic-gate 				if (dipmap->ra_dip == dip)
3350Sstevel@tonic-gate 					break;
3360Sstevel@tonic-gate 				prevdip =  &dipmap->ra_next;
3370Sstevel@tonic-gate 				dipmap = dipmap->ra_next;
3380Sstevel@tonic-gate 			}
3390Sstevel@tonic-gate 
3400Sstevel@tonic-gate 			if (dipmap != NULL) {
3410Sstevel@tonic-gate 				/* found it */
3420Sstevel@tonic-gate 				break;
3430Sstevel@tonic-gate 			}
3440Sstevel@tonic-gate 
3450Sstevel@tonic-gate 			if (!(flag & NDI_RA_PASS)) {
3460Sstevel@tonic-gate 				break;
3470Sstevel@tonic-gate 			}
3480Sstevel@tonic-gate 		}
3490Sstevel@tonic-gate 	}
3500Sstevel@tonic-gate 
3510Sstevel@tonic-gate 	*backtype = (*prevmap == NULL) ?  NULL: prevmap;
3520Sstevel@tonic-gate 	*backdip = (dipmap == NULL) ?  NULL: prevdip;
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate 	return (dipmap);
3550Sstevel@tonic-gate }
3560Sstevel@tonic-gate 
3570Sstevel@tonic-gate int
ndi_ra_free(dev_info_t * dip,uint64_t base,uint64_t len,char * type,uint32_t flag)3580Sstevel@tonic-gate ndi_ra_free(dev_info_t *dip, uint64_t base, uint64_t len, char *type,
3590Sstevel@tonic-gate     uint32_t flag)
3600Sstevel@tonic-gate {
3610Sstevel@tonic-gate 	struct ra_dip_type *dipmap;
3620Sstevel@tonic-gate 	struct ra_resource *newmap, *overlapmap, *oldmap = NULL;
3630Sstevel@tonic-gate 	struct ra_resource  *mapp, **backp;
3640Sstevel@tonic-gate 	uint64_t newend, mapend;
3650Sstevel@tonic-gate 	struct ra_dip_type **backdip;
3660Sstevel@tonic-gate 	struct ra_type_map **backtype;
3670Sstevel@tonic-gate 
3680Sstevel@tonic-gate 	if (len == 0) {
3690Sstevel@tonic-gate 		return (NDI_SUCCESS);
3700Sstevel@tonic-gate 	}
3710Sstevel@tonic-gate 
3720Sstevel@tonic-gate 	mutex_enter(&ra_lock);
3730Sstevel@tonic-gate 
3740Sstevel@tonic-gate 	if ((dipmap = find_dip_map_resources(dip, type, &backdip, &backtype,
3750Sstevel@tonic-gate 	    flag)) == NULL) {
3760Sstevel@tonic-gate 		mutex_exit(&ra_lock);
3770Sstevel@tonic-gate 		return (NDI_FAILURE);
3780Sstevel@tonic-gate 	}
3790Sstevel@tonic-gate 
3800Sstevel@tonic-gate 	mapp = dipmap->ra_rangeset;
3810Sstevel@tonic-gate 	backp = &dipmap->ra_rangeset;
3820Sstevel@tonic-gate 
3830Sstevel@tonic-gate 	/* now find where range lies and fix things up */
3840Sstevel@tonic-gate 	newend = base + len;
3850Sstevel@tonic-gate 	for (; mapp != NULL; backp = &(mapp->ra_next), mapp = mapp->ra_next) {
3860Sstevel@tonic-gate 		mapend = mapp->ra_base + mapp->ra_len;
3870Sstevel@tonic-gate 
3880Sstevel@tonic-gate 		/* check for overlap first */
3890Sstevel@tonic-gate 		if ((base <= mapp->ra_base && newend > mapp->ra_base) ||
3900Sstevel@tonic-gate 		    (base > mapp->ra_base && base < mapend)) {
3910Sstevel@tonic-gate 			/* overlap with mapp */
3920Sstevel@tonic-gate 			overlapmap = mapp;
3930Sstevel@tonic-gate 			goto overlap;
3940Sstevel@tonic-gate 		} else if ((base == mapend && mapp->ra_next) &&
3950Sstevel@tonic-gate 		    (newend > mapp->ra_next->ra_base)) {
3960Sstevel@tonic-gate 			/* overlap with mapp->ra_next */
3970Sstevel@tonic-gate 			overlapmap = mapp->ra_next;
3980Sstevel@tonic-gate 			goto overlap;
3990Sstevel@tonic-gate 		}
4000Sstevel@tonic-gate 
4010Sstevel@tonic-gate 		if (newend == mapp->ra_base) {
4020Sstevel@tonic-gate 			/* simple - on front */
4030Sstevel@tonic-gate 			mapp->ra_base = base;
4040Sstevel@tonic-gate 			mapp->ra_len += len;
4050Sstevel@tonic-gate 			/*
4060Sstevel@tonic-gate 			 * don't need to check if it merges with
4070Sstevel@tonic-gate 			 * previous since that would match on on end
4080Sstevel@tonic-gate 			 */
4090Sstevel@tonic-gate 			break;
4100Sstevel@tonic-gate 		} else if (base == mapend) {
4110Sstevel@tonic-gate 			/* simple - on end */
4120Sstevel@tonic-gate 			mapp->ra_len += len;
4130Sstevel@tonic-gate 			if (mapp->ra_next &&
4140Sstevel@tonic-gate 			    (newend == mapp->ra_next->ra_base)) {
4150Sstevel@tonic-gate 				/* merge with next node */
4160Sstevel@tonic-gate 				oldmap = mapp->ra_next;
4170Sstevel@tonic-gate 				mapp->ra_len += oldmap->ra_len;
4180Sstevel@tonic-gate 				RA_REMOVE(&mapp->ra_next, oldmap);
4190Sstevel@tonic-gate 				kmem_free((caddr_t)oldmap, sizeof (*oldmap));
4200Sstevel@tonic-gate 			}
4210Sstevel@tonic-gate 			break;
4220Sstevel@tonic-gate 		} else if (base < mapp->ra_base) {
4230Sstevel@tonic-gate 			/* somewhere in between so just an insert */
4240Sstevel@tonic-gate 			newmap = (struct ra_resource *)
42510923SEvan.Yan@Sun.COM 			    kmem_zalloc(sizeof (*newmap), KM_SLEEP);
4260Sstevel@tonic-gate 			newmap->ra_base = base;
4270Sstevel@tonic-gate 			newmap->ra_len = len;
4280Sstevel@tonic-gate 			RA_INSERT(backp, newmap);
4290Sstevel@tonic-gate 			break;
4300Sstevel@tonic-gate 		}
4310Sstevel@tonic-gate 	}
4320Sstevel@tonic-gate 	if (mapp == NULL) {
4330Sstevel@tonic-gate 		/* stick on end */
4340Sstevel@tonic-gate 		newmap = (struct ra_resource *)
43510923SEvan.Yan@Sun.COM 		    kmem_zalloc(sizeof (*newmap), KM_SLEEP);
4360Sstevel@tonic-gate 		newmap->ra_base = base;
4370Sstevel@tonic-gate 		newmap->ra_len = len;
4380Sstevel@tonic-gate 		RA_INSERT(backp, newmap);
4390Sstevel@tonic-gate 	}
4400Sstevel@tonic-gate 
4410Sstevel@tonic-gate 	mutex_exit(&ra_lock);
44210923SEvan.Yan@Sun.COM 
44310923SEvan.Yan@Sun.COM 	/*
44410923SEvan.Yan@Sun.COM 	 * Update dip's "available" property, adding this piece of
44510923SEvan.Yan@Sun.COM 	 * resource to the pool.
44610923SEvan.Yan@Sun.COM 	 */
447*11260SMiao.Chen@Sun.COM 	(void) pci_put_available_prop(dipmap->ra_dip, base, len, type);
44810923SEvan.Yan@Sun.COM done:
4490Sstevel@tonic-gate 	return (NDI_SUCCESS);
4500Sstevel@tonic-gate 
4510Sstevel@tonic-gate overlap:
4520Sstevel@tonic-gate 	/*
4530Sstevel@tonic-gate 	 * Bad free may happen on some x86 platforms with BIOS exporting
4540Sstevel@tonic-gate 	 * incorrect resource maps. The system is otherwise functioning
4550Sstevel@tonic-gate 	 * normally. We send such messages to syslog only.
4560Sstevel@tonic-gate 	 */
4570Sstevel@tonic-gate 	cmn_err(CE_NOTE, "!ndi_ra_free: bad free, dip %p, resource type %s \n",
4580Sstevel@tonic-gate 	    (void *)dip, type);
4590Sstevel@tonic-gate 	cmn_err(CE_NOTE, "!ndi_ra_free: freeing base 0x%" PRIx64 ", len 0x%"
4600Sstevel@tonic-gate 	    PRIX64 " overlaps with existing resource base 0x%" PRIx64
4610Sstevel@tonic-gate 	    ", len 0x%" PRIx64 "\n", base, len, overlapmap->ra_base,
4620Sstevel@tonic-gate 	    overlapmap->ra_len);
4630Sstevel@tonic-gate 
4640Sstevel@tonic-gate 	mutex_exit(&ra_lock);
4650Sstevel@tonic-gate 	return (NDI_FAILURE);
4660Sstevel@tonic-gate }
4670Sstevel@tonic-gate 
4680Sstevel@tonic-gate /* check to see if value is power of 2 or not. */
4690Sstevel@tonic-gate static int
isnot_pow2(uint64_t value)4700Sstevel@tonic-gate isnot_pow2(uint64_t value)
4710Sstevel@tonic-gate {
4720Sstevel@tonic-gate 	uint32_t low;
4730Sstevel@tonic-gate 	uint32_t hi;
4740Sstevel@tonic-gate 
4750Sstevel@tonic-gate 	low = value & 0xffffffff;
4760Sstevel@tonic-gate 	hi = value >> 32;
4770Sstevel@tonic-gate 
4780Sstevel@tonic-gate 	/*
4790Sstevel@tonic-gate 	 * ddi_ffs and ddi_fls gets long values, so in 32bit environment
4800Sstevel@tonic-gate 	 * won't work correctly for 64bit values
4810Sstevel@tonic-gate 	 */
4820Sstevel@tonic-gate 	if ((ddi_ffs(low) == ddi_fls(low)) &&
4830Sstevel@tonic-gate 	    (ddi_ffs(hi) == ddi_fls(hi)))
4840Sstevel@tonic-gate 		return (0);
4850Sstevel@tonic-gate 	return (1);
4860Sstevel@tonic-gate }
4870Sstevel@tonic-gate 
4880Sstevel@tonic-gate static  void
adjust_link(struct ra_resource ** backp,struct ra_resource * mapp,uint64_t base,uint64_t len)4890Sstevel@tonic-gate adjust_link(struct ra_resource **backp, struct ra_resource *mapp,
4900Sstevel@tonic-gate 	    uint64_t base, uint64_t len)
4910Sstevel@tonic-gate {
4920Sstevel@tonic-gate 	struct ra_resource *newmap;
4930Sstevel@tonic-gate 	uint64_t newlen;
4940Sstevel@tonic-gate 
4950Sstevel@tonic-gate 	if (base != mapp->ra_base) {
4960Sstevel@tonic-gate 		/* in the middle or end */
4970Sstevel@tonic-gate 		newlen = base - mapp->ra_base;
4980Sstevel@tonic-gate 		if ((mapp->ra_len - newlen) == len) {
4990Sstevel@tonic-gate 			/* on the end */
5000Sstevel@tonic-gate 			mapp->ra_len = newlen;
5010Sstevel@tonic-gate 		} else {
5020Sstevel@tonic-gate 			/* in the middle */
5030Sstevel@tonic-gate 			newmap = (struct ra_resource *)
50410923SEvan.Yan@Sun.COM 			    kmem_zalloc(sizeof (*newmap), KM_SLEEP);
5050Sstevel@tonic-gate 			newmap->ra_base = base + len;
50610923SEvan.Yan@Sun.COM 			newmap->ra_len = mapp->ra_len - (len + newlen);
5070Sstevel@tonic-gate 			mapp->ra_len = newlen;
5080Sstevel@tonic-gate 			RA_INSERT(&(mapp->ra_next), newmap);
5090Sstevel@tonic-gate 		}
5100Sstevel@tonic-gate 	} else {
5110Sstevel@tonic-gate 		/* at the beginning */
5120Sstevel@tonic-gate 		mapp->ra_base += len;
5130Sstevel@tonic-gate 		mapp->ra_len -= len;
5140Sstevel@tonic-gate 		if (mapp->ra_len == 0) {
5150Sstevel@tonic-gate 			/* remove the whole node */
5160Sstevel@tonic-gate 			RA_REMOVE(backp, mapp);
5170Sstevel@tonic-gate 			kmem_free((caddr_t)mapp, sizeof (*mapp));
5180Sstevel@tonic-gate 		}
5190Sstevel@tonic-gate 	}
5200Sstevel@tonic-gate }
5210Sstevel@tonic-gate 
5220Sstevel@tonic-gate int
ndi_ra_alloc(dev_info_t * dip,ndi_ra_request_t * req,uint64_t * retbasep,uint64_t * retlenp,char * type,uint32_t flag)5230Sstevel@tonic-gate ndi_ra_alloc(dev_info_t *dip, ndi_ra_request_t *req, uint64_t *retbasep,
5240Sstevel@tonic-gate     uint64_t *retlenp, char *type, uint32_t flag)
5250Sstevel@tonic-gate {
5260Sstevel@tonic-gate 	struct ra_dip_type *dipmap;
5270Sstevel@tonic-gate 	struct ra_resource *mapp, **backp, **backlargestp;
5280Sstevel@tonic-gate 	uint64_t mask = 0;
5290Sstevel@tonic-gate 	uint64_t len, remlen, largestbase, largestlen;
5300Sstevel@tonic-gate 	uint64_t base, oldbase, lower, upper;
5310Sstevel@tonic-gate 	struct ra_dip_type  **backdip;
5320Sstevel@tonic-gate 	struct ra_type_map  **backtype;
5330Sstevel@tonic-gate 	int  rval = NDI_FAILURE;
5340Sstevel@tonic-gate 
5350Sstevel@tonic-gate 
5360Sstevel@tonic-gate 	len = req->ra_len;
5370Sstevel@tonic-gate 
5380Sstevel@tonic-gate 	if (req->ra_flags & NDI_RA_ALIGN_SIZE) {
5390Sstevel@tonic-gate 		if (isnot_pow2(req->ra_len)) {
5400Sstevel@tonic-gate 			DEBUGPRT(CE_WARN, "ndi_ra_alloc: bad length(pow2) 0x%"
54110923SEvan.Yan@Sun.COM 			    PRIx64, req->ra_len);
5420Sstevel@tonic-gate 			*retbasep = 0;
5430Sstevel@tonic-gate 			*retlenp = 0;
5440Sstevel@tonic-gate 			return (NDI_FAILURE);
5450Sstevel@tonic-gate 		}
5460Sstevel@tonic-gate 	}
5470Sstevel@tonic-gate 
5480Sstevel@tonic-gate 	mask = (req->ra_flags & NDI_RA_ALIGN_SIZE) ? (len - 1) :
5490Sstevel@tonic-gate 	    req->ra_align_mask;
5500Sstevel@tonic-gate 
5510Sstevel@tonic-gate 
5520Sstevel@tonic-gate 	mutex_enter(&ra_lock);
5530Sstevel@tonic-gate 	dipmap = find_dip_map_resources(dip, type, &backdip, &backtype, flag);
5540Sstevel@tonic-gate 	if ((dipmap == NULL) || ((mapp = dipmap->ra_rangeset) == NULL)) {
5550Sstevel@tonic-gate 		mutex_exit(&ra_lock);
5560Sstevel@tonic-gate 		DEBUGPRT(CE_CONT, "ndi_ra_alloc no map found for this type\n");
5570Sstevel@tonic-gate 		return (NDI_FAILURE);
5580Sstevel@tonic-gate 	}
5590Sstevel@tonic-gate 
5600Sstevel@tonic-gate 	DEBUGPRT(CE_CONT, "ndi_ra_alloc: mapp = %p len=%" PRIx64 ", mask=%"
56110923SEvan.Yan@Sun.COM 	    PRIx64 "\n", (void *)mapp, len, mask);
5620Sstevel@tonic-gate 
5630Sstevel@tonic-gate 	backp = &(dipmap->ra_rangeset);
5640Sstevel@tonic-gate 	backlargestp = NULL;
5650Sstevel@tonic-gate 	largestbase = 0;
5660Sstevel@tonic-gate 	largestlen = 0;
5670Sstevel@tonic-gate 
5680Sstevel@tonic-gate 	lower = 0;
5690Sstevel@tonic-gate 	upper = ~(uint64_t)0;
5700Sstevel@tonic-gate 
5710Sstevel@tonic-gate 	if (req->ra_flags & NDI_RA_ALLOC_BOUNDED) {
5720Sstevel@tonic-gate 		/* bounded so skip to first possible */
5730Sstevel@tonic-gate 		lower = req->ra_boundbase;
5740Sstevel@tonic-gate 		upper = req->ra_boundlen + lower;
5750Sstevel@tonic-gate 		if ((upper == 0) || (upper < req->ra_boundlen))
5760Sstevel@tonic-gate 			upper = ~(uint64_t)0;
5770Sstevel@tonic-gate 		DEBUGPRT(CE_CONT, "ndi_ra_alloc: ra_len = %" PRIx64 ", len = %"
57810923SEvan.Yan@Sun.COM 		    PRIx64 " ra_base=%" PRIx64 ", mask=%" PRIx64
57910923SEvan.Yan@Sun.COM 		    "\n", mapp->ra_len, len, mapp->ra_base, mask);
58010923SEvan.Yan@Sun.COM 		for (; mapp != NULL && (mapp->ra_base + mapp->ra_len) < lower;
58110923SEvan.Yan@Sun.COM 		    backp = &(mapp->ra_next), mapp = mapp->ra_next) {
5820Sstevel@tonic-gate 			if (((mapp->ra_len + mapp->ra_base) == 0) ||
5830Sstevel@tonic-gate 			    ((mapp->ra_len + mapp->ra_base) < mapp->ra_len))
5840Sstevel@tonic-gate 				/*
5850Sstevel@tonic-gate 				 * This elements end goes beyond max uint64_t.
5860Sstevel@tonic-gate 				 * potential candidate, check end against lower
5870Sstevel@tonic-gate 				 * would not be precise.
5880Sstevel@tonic-gate 				 */
5890Sstevel@tonic-gate 				break;
5900Sstevel@tonic-gate 
5910Sstevel@tonic-gate 			DEBUGPRT(CE_CONT, " ra_len = %" PRIx64 ", ra_base=%"
5920Sstevel@tonic-gate 			    PRIx64 "\n", mapp->ra_len, mapp->ra_base);
5930Sstevel@tonic-gate 			}
5940Sstevel@tonic-gate 
5950Sstevel@tonic-gate 	}
5960Sstevel@tonic-gate 
5970Sstevel@tonic-gate 	if (!(req->ra_flags & NDI_RA_ALLOC_SPECIFIED)) {
5980Sstevel@tonic-gate 		/* first fit - not user specified */
5990Sstevel@tonic-gate 		DEBUGPRT(CE_CONT, "ndi_ra_alloc(unspecified request)"
60010923SEvan.Yan@Sun.COM 		    "lower=%" PRIx64 ", upper=%" PRIx64 "\n", lower, upper);
6010Sstevel@tonic-gate 		for (; mapp != NULL && mapp->ra_base <= upper;
60210923SEvan.Yan@Sun.COM 		    backp = &(mapp->ra_next), mapp = mapp->ra_next) {
6030Sstevel@tonic-gate 
6040Sstevel@tonic-gate 			DEBUGPRT(CE_CONT, "ndi_ra_alloc: ra_len = %" PRIx64
6050Sstevel@tonic-gate 			    ", len = %" PRIx64 "", mapp->ra_len, len);
6060Sstevel@tonic-gate 			base = mapp->ra_base;
6070Sstevel@tonic-gate 			if (base < lower) {
6080Sstevel@tonic-gate 				base = lower;
6090Sstevel@tonic-gate 				DEBUGPRT(CE_CONT, "\tbase=%" PRIx64
6100Sstevel@tonic-gate 				    ", ra_base=%" PRIx64 ", mask=%" PRIx64,
6110Sstevel@tonic-gate 				    base, mapp->ra_base, mask);
6120Sstevel@tonic-gate 			}
6130Sstevel@tonic-gate 
6140Sstevel@tonic-gate 			if ((base & mask) != 0) {
6150Sstevel@tonic-gate 				oldbase = base;
6160Sstevel@tonic-gate 				/*
6170Sstevel@tonic-gate 				 * failed a critical constraint
6180Sstevel@tonic-gate 				 * adjust and see if it still fits
6190Sstevel@tonic-gate 				 */
6200Sstevel@tonic-gate 				base = base & ~mask;
6210Sstevel@tonic-gate 				base += (mask + 1);
6220Sstevel@tonic-gate 				DEBUGPRT(CE_CONT, "\tnew base=%" PRIx64 "\n",
62310923SEvan.Yan@Sun.COM 				    base);
6240Sstevel@tonic-gate 
6250Sstevel@tonic-gate 				/*
6260Sstevel@tonic-gate 				 * Check to see if the new base is past
6270Sstevel@tonic-gate 				 * the end of the resource.
6280Sstevel@tonic-gate 				 */
6290Sstevel@tonic-gate 				if (base >= (oldbase + mapp->ra_len + 1)) {
6300Sstevel@tonic-gate 					continue;
6310Sstevel@tonic-gate 				}
6320Sstevel@tonic-gate 			}
6330Sstevel@tonic-gate 
6340Sstevel@tonic-gate 			if (req->ra_flags & NDI_RA_ALLOC_PARTIAL_OK) {
6350Sstevel@tonic-gate 				if ((upper - mapp->ra_base)  <  mapp->ra_len)
6360Sstevel@tonic-gate 					remlen = upper - base;
6370Sstevel@tonic-gate 				else
6380Sstevel@tonic-gate 					remlen = mapp->ra_len -
63910923SEvan.Yan@Sun.COM 					    (base - mapp->ra_base);
6400Sstevel@tonic-gate 
6410Sstevel@tonic-gate 				if ((backlargestp == NULL) ||
6420Sstevel@tonic-gate 				    (largestlen < remlen)) {
6430Sstevel@tonic-gate 
6440Sstevel@tonic-gate 					backlargestp = backp;
6450Sstevel@tonic-gate 					largestbase = base;
6460Sstevel@tonic-gate 					largestlen = remlen;
6470Sstevel@tonic-gate 				}
6480Sstevel@tonic-gate 			}
6490Sstevel@tonic-gate 
6500Sstevel@tonic-gate 			if (mapp->ra_len >= len) {
6510Sstevel@tonic-gate 				/* a candidate -- apply constraints */
6520Sstevel@tonic-gate 				if ((len > (mapp->ra_len -
6530Sstevel@tonic-gate 				    (base - mapp->ra_base))) ||
6540Sstevel@tonic-gate 				    ((len - 1 + base) > upper)) {
6550Sstevel@tonic-gate 					continue;
6560Sstevel@tonic-gate 				}
6570Sstevel@tonic-gate 
6580Sstevel@tonic-gate 				/* we have a fit */
6590Sstevel@tonic-gate 
6600Sstevel@tonic-gate 				DEBUGPRT(CE_CONT, "\thave a fit\n");
6610Sstevel@tonic-gate 
6620Sstevel@tonic-gate 				adjust_link(backp, mapp, base, len);
6630Sstevel@tonic-gate 				rval = NDI_SUCCESS;
6640Sstevel@tonic-gate 				break;
6650Sstevel@tonic-gate 
6660Sstevel@tonic-gate 			}
6670Sstevel@tonic-gate 		}
6680Sstevel@tonic-gate 	} else {
6690Sstevel@tonic-gate 		/* want an exact value/fit */
6700Sstevel@tonic-gate 		base = req->ra_addr;
6710Sstevel@tonic-gate 		len = req->ra_len;
6720Sstevel@tonic-gate 		for (; mapp != NULL && mapp->ra_base <= upper;
67310923SEvan.Yan@Sun.COM 		    backp = &(mapp->ra_next), mapp = mapp->ra_next) {
6740Sstevel@tonic-gate 			if (base >= mapp->ra_base &&
6750Sstevel@tonic-gate 			    ((base - mapp->ra_base) < mapp->ra_len)) {
6760Sstevel@tonic-gate 				/*
6770Sstevel@tonic-gate 				 * This is the node with he requested base in
6780Sstevel@tonic-gate 				 * its range
6790Sstevel@tonic-gate 				 */
6800Sstevel@tonic-gate 				if ((len > mapp->ra_len) ||
6810Sstevel@tonic-gate 				    (base - mapp->ra_base >
6820Sstevel@tonic-gate 				    mapp->ra_len - len)) {
6830Sstevel@tonic-gate 					/* length requirement not satisfied */
6840Sstevel@tonic-gate 					if (req->ra_flags &
6850Sstevel@tonic-gate 					    NDI_RA_ALLOC_PARTIAL_OK) {
6860Sstevel@tonic-gate 						if ((upper - mapp->ra_base)
6870Sstevel@tonic-gate 						    < mapp->ra_len)
6880Sstevel@tonic-gate 							remlen = upper - base;
6890Sstevel@tonic-gate 						else
6900Sstevel@tonic-gate 							remlen =
6910Sstevel@tonic-gate 							    mapp->ra_len -
6920Sstevel@tonic-gate 							    (base -
6930Sstevel@tonic-gate 							    mapp->ra_base);
6940Sstevel@tonic-gate 					}
6950Sstevel@tonic-gate 					backlargestp = backp;
6960Sstevel@tonic-gate 					largestbase = base;
6970Sstevel@tonic-gate 					largestlen = remlen;
6980Sstevel@tonic-gate 					base = 0;
6990Sstevel@tonic-gate 				} else {
7000Sstevel@tonic-gate 					/* We have a match */
7010Sstevel@tonic-gate 					adjust_link(backp, mapp, base, len);
7020Sstevel@tonic-gate 					rval = NDI_SUCCESS;
7030Sstevel@tonic-gate 				}
7040Sstevel@tonic-gate 				break;
7050Sstevel@tonic-gate 			}
7060Sstevel@tonic-gate 		}
7070Sstevel@tonic-gate 	}
7080Sstevel@tonic-gate 
7090Sstevel@tonic-gate 	if ((rval != NDI_SUCCESS) &&
7100Sstevel@tonic-gate 	    (req->ra_flags & NDI_RA_ALLOC_PARTIAL_OK) &&
7110Sstevel@tonic-gate 	    (backlargestp != NULL)) {
7120Sstevel@tonic-gate 		adjust_link(backlargestp, *backlargestp, largestbase,
71310923SEvan.Yan@Sun.COM 		    largestlen);
7140Sstevel@tonic-gate 
7150Sstevel@tonic-gate 		base = largestbase;
7160Sstevel@tonic-gate 		len = largestlen;
7170Sstevel@tonic-gate 		rval = NDI_RA_PARTIAL_REQ;
7180Sstevel@tonic-gate 	}
7190Sstevel@tonic-gate 
7200Sstevel@tonic-gate 	mutex_exit(&ra_lock);
7210Sstevel@tonic-gate 
7220Sstevel@tonic-gate 	if (rval == NDI_FAILURE) {
7230Sstevel@tonic-gate 		*retbasep = 0;
7240Sstevel@tonic-gate 		*retlenp = 0;
7250Sstevel@tonic-gate 	} else {
7260Sstevel@tonic-gate 		*retbasep = base;
7270Sstevel@tonic-gate 		*retlenp = len;
7280Sstevel@tonic-gate 	}
72910923SEvan.Yan@Sun.COM 
73010923SEvan.Yan@Sun.COM 	/*
73110923SEvan.Yan@Sun.COM 	 * Update dip's "available" property, substract this piece of
73210923SEvan.Yan@Sun.COM 	 * resource from the pool.
73310923SEvan.Yan@Sun.COM 	 */
73410923SEvan.Yan@Sun.COM 	if ((rval == NDI_SUCCESS) || (rval == NDI_RA_PARTIAL_REQ))
735*11260SMiao.Chen@Sun.COM 		(void) pci_get_available_prop(dipmap->ra_dip,
736*11260SMiao.Chen@Sun.COM 		    *retbasep, *retlenp, type);
73710923SEvan.Yan@Sun.COM 
7380Sstevel@tonic-gate 	return (rval);
7390Sstevel@tonic-gate }
7400Sstevel@tonic-gate 
7410Sstevel@tonic-gate /*
7420Sstevel@tonic-gate  * isa_resource_setup
7430Sstevel@tonic-gate  *	check for /used-resources and initialize
7440Sstevel@tonic-gate  *	based on info there.  If no /used-resources,
7450Sstevel@tonic-gate  *	fail.
7460Sstevel@tonic-gate  */
7470Sstevel@tonic-gate int
isa_resource_setup()7480Sstevel@tonic-gate isa_resource_setup()
7490Sstevel@tonic-gate {
7500Sstevel@tonic-gate 	dev_info_t *used, *usedpdip;
7510Sstevel@tonic-gate 	/*
7520Sstevel@tonic-gate 	 * note that at this time bootconf creates 32 bit properties for
7530Sstevel@tonic-gate 	 * io-space and device-memory
7540Sstevel@tonic-gate 	 */
7550Sstevel@tonic-gate 	struct iorange {
7560Sstevel@tonic-gate 		uint32_t	base;
7570Sstevel@tonic-gate 		uint32_t	len;
7580Sstevel@tonic-gate 	} *iorange;
7590Sstevel@tonic-gate 	struct memrange {
7600Sstevel@tonic-gate 		uint32_t	base;
7610Sstevel@tonic-gate 		uint32_t	len;
7620Sstevel@tonic-gate 	} *memrange;
7630Sstevel@tonic-gate 	uint32_t *irq;
7640Sstevel@tonic-gate 	int proplen;
7650Sstevel@tonic-gate 	int i, len;
7660Sstevel@tonic-gate 	int maxrange;
7670Sstevel@tonic-gate 	ndi_ra_request_t req;
7680Sstevel@tonic-gate 	uint64_t retbase;
7690Sstevel@tonic-gate 	uint64_t retlen;
7700Sstevel@tonic-gate 
7710Sstevel@tonic-gate 	used = ddi_find_devinfo("used-resources", -1, 0);
7720Sstevel@tonic-gate 	if (used == NULL) {
7730Sstevel@tonic-gate 		DEBUGPRT(CE_CONT,
77410923SEvan.Yan@Sun.COM 		    "isa_resource_setup: used-resources not found");
7750Sstevel@tonic-gate 		return (NDI_FAILURE);
7760Sstevel@tonic-gate 	}
7770Sstevel@tonic-gate 
7780Sstevel@tonic-gate 	/*
7790Sstevel@tonic-gate 	 * initialize to all resources being present
7800Sstevel@tonic-gate 	 * and then remove the ones in use.
7810Sstevel@tonic-gate 	 */
7820Sstevel@tonic-gate 
7830Sstevel@tonic-gate 	usedpdip = ddi_root_node();
7840Sstevel@tonic-gate 
7850Sstevel@tonic-gate 	DEBUGPRT(CE_CONT, "isa_resource_setup: used = %p usedpdip = %p\n",
7860Sstevel@tonic-gate 	    (void *)used, (void *)usedpdip);
7870Sstevel@tonic-gate 
7880Sstevel@tonic-gate 	if (ndi_ra_map_setup(usedpdip, NDI_RA_TYPE_IO) == NDI_FAILURE) {
7890Sstevel@tonic-gate 		return (NDI_FAILURE);
7900Sstevel@tonic-gate 	}
7910Sstevel@tonic-gate 
7920Sstevel@tonic-gate 	/* initialize io space, highest end base is 0xffff */
7930Sstevel@tonic-gate 	/* note that length is highest addr + 1 since starts from 0 */
7940Sstevel@tonic-gate 
7950Sstevel@tonic-gate 	(void) ndi_ra_free(usedpdip, 0, 0xffff + 1,  NDI_RA_TYPE_IO, 0);
7960Sstevel@tonic-gate 
797506Scth 	if (ddi_getlongprop(DDI_DEV_T_ANY, used, DDI_PROP_DONTPASS,
7980Sstevel@tonic-gate 	    "io-space", (caddr_t)&iorange, &proplen) == DDI_SUCCESS) {
7990Sstevel@tonic-gate 		maxrange = proplen / sizeof (struct iorange);
8000Sstevel@tonic-gate 		/* remove the "used" I/O resources */
8010Sstevel@tonic-gate 		for (i = 0; i < maxrange; i++) {
8020Sstevel@tonic-gate 			bzero((caddr_t)&req, sizeof (req));
8030Sstevel@tonic-gate 			req.ra_addr =  (uint64_t)iorange[i].base;
8040Sstevel@tonic-gate 			req.ra_len = (uint64_t)iorange[i].len;
8050Sstevel@tonic-gate 			req.ra_flags = NDI_RA_ALLOC_SPECIFIED;
8060Sstevel@tonic-gate 			(void) ndi_ra_alloc(usedpdip, &req, &retbase, &retlen,
8070Sstevel@tonic-gate 			    NDI_RA_TYPE_IO, 0);
8080Sstevel@tonic-gate 		}
8090Sstevel@tonic-gate 
8100Sstevel@tonic-gate 		kmem_free((caddr_t)iorange, proplen);
8110Sstevel@tonic-gate 	}
8120Sstevel@tonic-gate 
8130Sstevel@tonic-gate 	if (ndi_ra_map_setup(usedpdip, NDI_RA_TYPE_MEM) == NDI_FAILURE) {
8140Sstevel@tonic-gate 		return (NDI_FAILURE);
8150Sstevel@tonic-gate 	}
8160Sstevel@tonic-gate 	/* initialize memory space where highest end base is 0xffffffff */
8170Sstevel@tonic-gate 	/* note that length is highest addr + 1 since starts from 0 */
8180Sstevel@tonic-gate 	(void) ndi_ra_free(usedpdip, 0, ((uint64_t)((uint32_t)~0)) + 1,
8190Sstevel@tonic-gate 	    NDI_RA_TYPE_MEM, 0);
8200Sstevel@tonic-gate 
821506Scth 	if (ddi_getlongprop(DDI_DEV_T_ANY, used, DDI_PROP_DONTPASS,
8220Sstevel@tonic-gate 	    "device-memory", (caddr_t)&memrange, &proplen) == DDI_SUCCESS) {
8230Sstevel@tonic-gate 		maxrange = proplen / sizeof (struct memrange);
8240Sstevel@tonic-gate 		/* remove the "used" memory resources */
8250Sstevel@tonic-gate 		for (i = 0; i < maxrange; i++) {
8260Sstevel@tonic-gate 			bzero((caddr_t)&req, sizeof (req));
8270Sstevel@tonic-gate 			req.ra_addr = (uint64_t)memrange[i].base;
8280Sstevel@tonic-gate 			req.ra_len = (uint64_t)memrange[i].len;
8290Sstevel@tonic-gate 			req.ra_flags = NDI_RA_ALLOC_SPECIFIED;
8300Sstevel@tonic-gate 			(void) ndi_ra_alloc(usedpdip, &req, &retbase, &retlen,
8310Sstevel@tonic-gate 			    NDI_RA_TYPE_MEM, 0);
8320Sstevel@tonic-gate 		}
8330Sstevel@tonic-gate 
8340Sstevel@tonic-gate 		kmem_free((caddr_t)memrange, proplen);
8350Sstevel@tonic-gate 	}
8360Sstevel@tonic-gate 
8370Sstevel@tonic-gate 	if (ndi_ra_map_setup(usedpdip, NDI_RA_TYPE_INTR) == NDI_FAILURE) {
8380Sstevel@tonic-gate 		return (NDI_FAILURE);
8390Sstevel@tonic-gate 	}
8400Sstevel@tonic-gate 
8410Sstevel@tonic-gate 	/* initialize the interrupt space */
8420Sstevel@tonic-gate 	(void) ndi_ra_free(usedpdip, 0, 16, NDI_RA_TYPE_INTR, 0);
8430Sstevel@tonic-gate 
8440Sstevel@tonic-gate #if defined(__i386) || defined(__amd64)
8450Sstevel@tonic-gate 	bzero(&req, sizeof (req));
8460Sstevel@tonic-gate 	req.ra_addr = 2;	/* 2 == 9 so never allow */
8470Sstevel@tonic-gate 	req.ra_len = 1;
8480Sstevel@tonic-gate 	req.ra_flags = NDI_RA_ALLOC_SPECIFIED;
8490Sstevel@tonic-gate 	(void) ndi_ra_alloc(usedpdip, &req, &retbase, &retlen,
8500Sstevel@tonic-gate 	    NDI_RA_TYPE_INTR, 0);
8510Sstevel@tonic-gate #endif
8520Sstevel@tonic-gate 
853506Scth 	if (ddi_getlongprop(DDI_DEV_T_ANY, used, DDI_PROP_DONTPASS,
8540Sstevel@tonic-gate 	    "interrupts", (caddr_t)&irq, &proplen) == DDI_SUCCESS) {
8550Sstevel@tonic-gate 		/* Initialize available interrupts by negating the used */
8560Sstevel@tonic-gate 		len = (proplen / sizeof (uint32_t));
8570Sstevel@tonic-gate 		for (i = 0; i < len; i++) {
8580Sstevel@tonic-gate 			bzero((caddr_t)&req, sizeof (req));
8590Sstevel@tonic-gate 			req.ra_addr = (uint64_t)irq[i];
8600Sstevel@tonic-gate 			req.ra_len = 1;
8610Sstevel@tonic-gate 			req.ra_flags = NDI_RA_ALLOC_SPECIFIED;
8620Sstevel@tonic-gate 			(void) ndi_ra_alloc(usedpdip, &req, &retbase, &retlen,
8630Sstevel@tonic-gate 			    NDI_RA_TYPE_INTR, 0);
8640Sstevel@tonic-gate 		}
8650Sstevel@tonic-gate 		kmem_free((caddr_t)irq, proplen);
8660Sstevel@tonic-gate 	}
8670Sstevel@tonic-gate 
8680Sstevel@tonic-gate #ifdef BUSRA_DEBUG
8690Sstevel@tonic-gate 	if (busra_debug) {
8700Sstevel@tonic-gate 		(void) ra_dump_all(NULL, usedpdip);
8710Sstevel@tonic-gate 	}
8720Sstevel@tonic-gate #endif
8730Sstevel@tonic-gate 	return (NDI_SUCCESS);
8740Sstevel@tonic-gate 
8750Sstevel@tonic-gate }
8760Sstevel@tonic-gate 
8770Sstevel@tonic-gate #ifdef BUSRA_DEBUG
8780Sstevel@tonic-gate void
ra_dump_all(char * type,dev_info_t * dip)8790Sstevel@tonic-gate ra_dump_all(char *type, dev_info_t *dip)
8800Sstevel@tonic-gate {
8810Sstevel@tonic-gate 
8820Sstevel@tonic-gate 	struct ra_type_map *typemap;
8830Sstevel@tonic-gate 	struct ra_dip_type *dipmap;
8840Sstevel@tonic-gate 	struct ra_resource *res;
8850Sstevel@tonic-gate 
8860Sstevel@tonic-gate 	typemap =  (struct ra_type_map *)ra_map_list_head;
8870Sstevel@tonic-gate 
8880Sstevel@tonic-gate 	for (; typemap != NULL; typemap = typemap->ra_next) {
8890Sstevel@tonic-gate 		if (type != NULL) {
8900Sstevel@tonic-gate 			if (strcmp(typemap->type, type) != 0)
8910Sstevel@tonic-gate 				continue;
8920Sstevel@tonic-gate 		}
8930Sstevel@tonic-gate 		cmn_err(CE_CONT, "type is %s\n", typemap->type);
8940Sstevel@tonic-gate 		for (dipmap = typemap->ra_dip_list; dipmap != NULL;
89510923SEvan.Yan@Sun.COM 		    dipmap = dipmap->ra_next) {
8960Sstevel@tonic-gate 			if (dip != NULL) {
8970Sstevel@tonic-gate 				if ((dipmap->ra_dip) != dip)
8980Sstevel@tonic-gate 					continue;
8990Sstevel@tonic-gate 			}
9000Sstevel@tonic-gate 			cmn_err(CE_CONT, "  dip is %p\n",
9010Sstevel@tonic-gate 			    (void *)dipmap->ra_dip);
9020Sstevel@tonic-gate 			for (res = dipmap->ra_rangeset; res != NULL;
90310923SEvan.Yan@Sun.COM 			    res = res->ra_next) {
9040Sstevel@tonic-gate 				cmn_err(CE_CONT, "\t  range is %" PRIx64
9050Sstevel@tonic-gate 				    " %" PRIx64 "\n", res->ra_base,
9060Sstevel@tonic-gate 				    res->ra_len);
9070Sstevel@tonic-gate 			}
9080Sstevel@tonic-gate 			if (dip != NULL)
9090Sstevel@tonic-gate 				break;
9100Sstevel@tonic-gate 		}
9110Sstevel@tonic-gate 		if (type != NULL)
9120Sstevel@tonic-gate 			break;
9130Sstevel@tonic-gate 	}
9140Sstevel@tonic-gate }
9150Sstevel@tonic-gate #endif
9160Sstevel@tonic-gate 
9170Sstevel@tonic-gate struct bus_range {	/* 1275 "bus-range" property definition */
9180Sstevel@tonic-gate 	uint32_t lo;
9190Sstevel@tonic-gate 	uint32_t hi;
9200Sstevel@tonic-gate } pci_bus_range;
9210Sstevel@tonic-gate 
9220Sstevel@tonic-gate struct busnum_ctrl {
9230Sstevel@tonic-gate 	int	rv;
9240Sstevel@tonic-gate 	dev_info_t *dip;
9250Sstevel@tonic-gate 	struct	bus_range *range;
9260Sstevel@tonic-gate };
9270Sstevel@tonic-gate 
9280Sstevel@tonic-gate 
9290Sstevel@tonic-gate /*
9300Sstevel@tonic-gate  * Setup resource map for the pci bus node based on the "available"
9310Sstevel@tonic-gate  * property and "bus-range" property.
9320Sstevel@tonic-gate  */
9330Sstevel@tonic-gate int
pci_resource_setup(dev_info_t * dip)9340Sstevel@tonic-gate pci_resource_setup(dev_info_t *dip)
9350Sstevel@tonic-gate {
9360Sstevel@tonic-gate 	pci_regspec_t *regs;
9370Sstevel@tonic-gate 	int rlen, rcount, i;
9380Sstevel@tonic-gate 	char bus_type[16] = "(unknown)";
9390Sstevel@tonic-gate 	int len;
9400Sstevel@tonic-gate 	struct busnum_ctrl ctrl;
9410Sstevel@tonic-gate 	int circular_count;
9420Sstevel@tonic-gate 	int rval = NDI_SUCCESS;
9430Sstevel@tonic-gate 
9440Sstevel@tonic-gate 	/*
9450Sstevel@tonic-gate 	 * If this is a pci bus node then look for "available" property
9460Sstevel@tonic-gate 	 * to find the available resources on this bus.
9470Sstevel@tonic-gate 	 */
9480Sstevel@tonic-gate 	len = sizeof (bus_type);
9490Sstevel@tonic-gate 	if (ddi_prop_op(DDI_DEV_T_ANY, dip, PROP_LEN_AND_VAL_BUF,
9500Sstevel@tonic-gate 	    DDI_PROP_CANSLEEP | DDI_PROP_DONTPASS, "device_type",
9510Sstevel@tonic-gate 	    (caddr_t)&bus_type, &len) != DDI_SUCCESS)
9520Sstevel@tonic-gate 		return (NDI_FAILURE);
9530Sstevel@tonic-gate 
954881Sjohnny 	/* it is not a pci/pci-ex bus type */
955226Set142600 	if ((strcmp(bus_type, "pci") != 0) && (strcmp(bus_type, "pciex") != 0))
9560Sstevel@tonic-gate 		return (NDI_FAILURE);
9570Sstevel@tonic-gate 
9580Sstevel@tonic-gate 	/*
9590Sstevel@tonic-gate 	 * The pci-hotplug project addresses adding the call
9600Sstevel@tonic-gate 	 * to pci_resource_setup from pci nexus driver.
9610Sstevel@tonic-gate 	 * However that project would initially be only for x86,
9620Sstevel@tonic-gate 	 * so for sparc pcmcia-pci support we still need to call
9630Sstevel@tonic-gate 	 * pci_resource_setup in pcic driver. Once all pci nexus drivers
9640Sstevel@tonic-gate 	 * are updated to call pci_resource_setup this portion of the
9650Sstevel@tonic-gate 	 * code would really become an assert to make sure this
9660Sstevel@tonic-gate 	 * function is not called for the same dip twice.
9670Sstevel@tonic-gate 	 */
96810923SEvan.Yan@Sun.COM 	/*
96910923SEvan.Yan@Sun.COM 	 * Another user for the check below is hotplug PCI/PCIe bridges.
97010923SEvan.Yan@Sun.COM 	 *
97110923SEvan.Yan@Sun.COM 	 * For PCI/PCIE devices under a PCIE hierarchy, ndi_ra_alloc/free
97210923SEvan.Yan@Sun.COM 	 * will update the devinfo node's "available" property, to reflect
97310923SEvan.Yan@Sun.COM 	 * the fact that a piece of resource has been removed/added to
97410923SEvan.Yan@Sun.COM 	 * a devinfo node.
97510923SEvan.Yan@Sun.COM 	 * During probe of a new PCI bridge in the hotplug case, PCI
97610923SEvan.Yan@Sun.COM 	 * configurator firstly allocates maximum MEM/IO from its parent,
97710923SEvan.Yan@Sun.COM 	 * then calls ndi_ra_free() to use these resources to setup busra
97810923SEvan.Yan@Sun.COM 	 * pool for the new bridge, as well as adding these resources to
97910923SEvan.Yan@Sun.COM 	 * the "available" property of the new devinfo node. Then configu-
98010923SEvan.Yan@Sun.COM 	 * rator will attach driver for the bridge before probing its
98110923SEvan.Yan@Sun.COM 	 * children, and the bridge driver will then initialize its hotplug
98210923SEvan.Yan@Sun.COM 	 * contollers (if it supports hotplug) and HPC driver will call
98310923SEvan.Yan@Sun.COM 	 * this function to setup the busra pool, but the resource pool
98410923SEvan.Yan@Sun.COM 	 * has already been setup at the first of pcicfg_probe_bridge(),
98510923SEvan.Yan@Sun.COM 	 * thus we need the check below to return directly in this case.
98610923SEvan.Yan@Sun.COM 	 * Otherwise the ndi_ra_free() below will see overlapping resources.
98710923SEvan.Yan@Sun.COM 	 */
9880Sstevel@tonic-gate 	{
9890Sstevel@tonic-gate 		if (ra_map_exist(dip, NDI_RA_TYPE_MEM) == NDI_SUCCESS) {
9900Sstevel@tonic-gate 			return (NDI_FAILURE);
9910Sstevel@tonic-gate 		}
9920Sstevel@tonic-gate 	}
9930Sstevel@tonic-gate 
9940Sstevel@tonic-gate 
995881Sjohnny 	/*
996881Sjohnny 	 * Create empty resource maps first.
997881Sjohnny 	 *
998881Sjohnny 	 * NOTE: If all the allocated resources are already assigned to
999881Sjohnny 	 * device(s) in the hot plug slot then "available" property may not
1000881Sjohnny 	 * be present. But, subsequent hot plug operation may unconfigure
1001881Sjohnny 	 * the device in the slot and try to free up it's resources. So,
1002881Sjohnny 	 * at the minimum we should create empty maps here.
1003881Sjohnny 	 */
10040Sstevel@tonic-gate 	if (ndi_ra_map_setup(dip, NDI_RA_TYPE_MEM) == NDI_FAILURE) {
10050Sstevel@tonic-gate 		return (NDI_FAILURE);
10060Sstevel@tonic-gate 	}
10070Sstevel@tonic-gate 
10080Sstevel@tonic-gate 	if (ndi_ra_map_setup(dip, NDI_RA_TYPE_IO) == NDI_FAILURE) {
10090Sstevel@tonic-gate 		return (NDI_FAILURE);
10100Sstevel@tonic-gate 	}
10110Sstevel@tonic-gate 
10120Sstevel@tonic-gate 	if (ndi_ra_map_setup(dip, NDI_RA_TYPE_PCI_BUSNUM) == NDI_FAILURE) {
10130Sstevel@tonic-gate 		return (NDI_FAILURE);
10140Sstevel@tonic-gate 	}
10150Sstevel@tonic-gate 
10160Sstevel@tonic-gate 	if (ndi_ra_map_setup(dip, NDI_RA_TYPE_PCI_PREFETCH_MEM) ==
10170Sstevel@tonic-gate 	    NDI_FAILURE) {
10180Sstevel@tonic-gate 		return (NDI_FAILURE);
10190Sstevel@tonic-gate 	}
10200Sstevel@tonic-gate 
1021881Sjohnny 	/* read the "available" property if it is available */
1022881Sjohnny 	if (ddi_getlongprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
1023881Sjohnny 	    "available", (caddr_t)&regs, &rlen) == DDI_SUCCESS) {
1024881Sjohnny 		/*
102510923SEvan.Yan@Sun.COM 		 * Remove "available" property as the entries will be
102610923SEvan.Yan@Sun.COM 		 * re-created in ndi_ra_free() below, note prom based
102710923SEvan.Yan@Sun.COM 		 * property will not be removed. But in ndi_ra_free()
102810923SEvan.Yan@Sun.COM 		 * we'll be creating non prom based property entries.
102910923SEvan.Yan@Sun.COM 		 */
103010923SEvan.Yan@Sun.COM 		(void) ndi_prop_remove(DDI_DEV_T_NONE, dip, "available");
103110923SEvan.Yan@Sun.COM 		/*
1032881Sjohnny 		 * create the available resource list for both memory and
1033881Sjohnny 		 * io space
1034881Sjohnny 		 */
1035881Sjohnny 		rcount = rlen / sizeof (pci_regspec_t);
1036881Sjohnny 		for (i = 0; i < rcount; i++) {
103710923SEvan.Yan@Sun.COM 			switch (PCI_REG_ADDR_G(regs[i].pci_phys_hi)) {
103810923SEvan.Yan@Sun.COM 			case PCI_REG_ADDR_G(PCI_ADDR_MEM32):
103910923SEvan.Yan@Sun.COM 				(void) ndi_ra_free(dip,
104010923SEvan.Yan@Sun.COM 				    (uint64_t)regs[i].pci_phys_low,
104110923SEvan.Yan@Sun.COM 				    (uint64_t)regs[i].pci_size_low,
104210923SEvan.Yan@Sun.COM 				    (regs[i].pci_phys_hi & PCI_REG_PF_M) ?
104310923SEvan.Yan@Sun.COM 				    NDI_RA_TYPE_PCI_PREFETCH_MEM :
104410923SEvan.Yan@Sun.COM 				    NDI_RA_TYPE_MEM,
104510923SEvan.Yan@Sun.COM 				    0);
104610923SEvan.Yan@Sun.COM 				break;
104710923SEvan.Yan@Sun.COM 			case PCI_REG_ADDR_G(PCI_ADDR_MEM64):
104810923SEvan.Yan@Sun.COM 				(void) ndi_ra_free(dip,
104910923SEvan.Yan@Sun.COM 				    ((uint64_t)(regs[i].pci_phys_mid) << 32) |
105010923SEvan.Yan@Sun.COM 				    ((uint64_t)(regs[i].pci_phys_low)),
105110923SEvan.Yan@Sun.COM 				    ((uint64_t)(regs[i].pci_size_hi) << 32) |
105210923SEvan.Yan@Sun.COM 				    ((uint64_t)(regs[i].pci_size_low)),
105310923SEvan.Yan@Sun.COM 				    (regs[i].pci_phys_hi & PCI_REG_PF_M) ?
105410923SEvan.Yan@Sun.COM 				    NDI_RA_TYPE_PCI_PREFETCH_MEM :
105510923SEvan.Yan@Sun.COM 				    NDI_RA_TYPE_MEM,
105610923SEvan.Yan@Sun.COM 				    0);
105710923SEvan.Yan@Sun.COM 				break;
105810923SEvan.Yan@Sun.COM 			case PCI_REG_ADDR_G(PCI_ADDR_IO):
105910923SEvan.Yan@Sun.COM 				(void) ndi_ra_free(dip,
106010923SEvan.Yan@Sun.COM 				    (uint64_t)regs[i].pci_phys_low,
106110923SEvan.Yan@Sun.COM 				    (uint64_t)regs[i].pci_size_low,
106210923SEvan.Yan@Sun.COM 				    NDI_RA_TYPE_IO,
106310923SEvan.Yan@Sun.COM 				    0);
106410923SEvan.Yan@Sun.COM 				break;
106510923SEvan.Yan@Sun.COM 			case PCI_REG_ADDR_G(PCI_ADDR_CONFIG):
106610923SEvan.Yan@Sun.COM 				break;
106710923SEvan.Yan@Sun.COM 			default:
106810923SEvan.Yan@Sun.COM 				cmn_err(CE_WARN,
106910923SEvan.Yan@Sun.COM 				    "pci_resource_setup: bad addr type: %x\n",
107010923SEvan.Yan@Sun.COM 				    PCI_REG_ADDR_G(regs[i].pci_phys_hi));
107110923SEvan.Yan@Sun.COM 				break;
107210923SEvan.Yan@Sun.COM 			}
10730Sstevel@tonic-gate 		}
10743446Smrj 		kmem_free(regs, rlen);
10750Sstevel@tonic-gate 	}
10760Sstevel@tonic-gate 
10770Sstevel@tonic-gate 	/*
1078881Sjohnny 	 * update resource map for available bus numbers if the node
10790Sstevel@tonic-gate 	 * has available-bus-range or bus-range property.
10800Sstevel@tonic-gate 	 */
10810Sstevel@tonic-gate 	len = sizeof (struct bus_range);
1082506Scth 	if (ddi_getlongprop_buf(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
10830Sstevel@tonic-gate 	    "available-bus-range", (caddr_t)&pci_bus_range, &len) ==
10840Sstevel@tonic-gate 	    DDI_SUCCESS) {
10850Sstevel@tonic-gate 		/*
10860Sstevel@tonic-gate 		 * Add bus numbers in the range to the free list.
10870Sstevel@tonic-gate 		 */
10880Sstevel@tonic-gate 		(void) ndi_ra_free(dip, (uint64_t)pci_bus_range.lo,
10890Sstevel@tonic-gate 		    (uint64_t)pci_bus_range.hi - (uint64_t)pci_bus_range.lo +
10900Sstevel@tonic-gate 		    1, NDI_RA_TYPE_PCI_BUSNUM, 0);
10910Sstevel@tonic-gate 	} else {
10920Sstevel@tonic-gate 		/*
10930Sstevel@tonic-gate 		 * We don't have an available-bus-range property. If, instead,
10940Sstevel@tonic-gate 		 * we have a bus-range property we add all the bus numbers
10950Sstevel@tonic-gate 		 * in that range to the free list but we must then scan
10960Sstevel@tonic-gate 		 * for pci-pci bridges on this bus to find out the if there
10970Sstevel@tonic-gate 		 * are any of those bus numbers already in use. If so, we can
10980Sstevel@tonic-gate 		 * reclaim them.
10990Sstevel@tonic-gate 		 */
11000Sstevel@tonic-gate 		len = sizeof (struct bus_range);
1101506Scth 		if (ddi_getlongprop_buf(DDI_DEV_T_ANY, dip,
11020Sstevel@tonic-gate 		    DDI_PROP_DONTPASS, "bus-range", (caddr_t)&pci_bus_range,
11030Sstevel@tonic-gate 		    &len) == DDI_SUCCESS) {
11040Sstevel@tonic-gate 			if (pci_bus_range.lo != pci_bus_range.hi) {
11050Sstevel@tonic-gate 				/*
11060Sstevel@tonic-gate 				 * Add bus numbers other than the secondary
11070Sstevel@tonic-gate 				 * bus number to the free list.
11080Sstevel@tonic-gate 				 */
11090Sstevel@tonic-gate 				(void) ndi_ra_free(dip,
11100Sstevel@tonic-gate 				    (uint64_t)pci_bus_range.lo + 1,
11110Sstevel@tonic-gate 				    (uint64_t)pci_bus_range.hi -
11120Sstevel@tonic-gate 				    (uint64_t)pci_bus_range.lo,
11130Sstevel@tonic-gate 				    NDI_RA_TYPE_PCI_BUSNUM, 0);
11140Sstevel@tonic-gate 
11150Sstevel@tonic-gate 				/* scan for pci-pci bridges */
11160Sstevel@tonic-gate 				ctrl.rv = DDI_SUCCESS;
11170Sstevel@tonic-gate 				ctrl.dip = dip;
11180Sstevel@tonic-gate 				ctrl.range = &pci_bus_range;
11190Sstevel@tonic-gate 				ndi_devi_enter(dip, &circular_count);
11200Sstevel@tonic-gate 				ddi_walk_devs(ddi_get_child(dip),
11210Sstevel@tonic-gate 				    claim_pci_busnum, (void *)&ctrl);
11220Sstevel@tonic-gate 				ndi_devi_exit(dip, circular_count);
11230Sstevel@tonic-gate 				if (ctrl.rv != DDI_SUCCESS) {
11240Sstevel@tonic-gate 					/* failed to create the map */
11250Sstevel@tonic-gate 					(void) ndi_ra_map_destroy(dip,
11260Sstevel@tonic-gate 					    NDI_RA_TYPE_PCI_BUSNUM);
11270Sstevel@tonic-gate 					rval = NDI_FAILURE;
11280Sstevel@tonic-gate 				}
11290Sstevel@tonic-gate 			}
11300Sstevel@tonic-gate 		}
11310Sstevel@tonic-gate 	}
11320Sstevel@tonic-gate 
11330Sstevel@tonic-gate #ifdef BUSRA_DEBUG
11340Sstevel@tonic-gate 	if (busra_debug) {
11350Sstevel@tonic-gate 		(void) ra_dump_all(NULL, dip);
11360Sstevel@tonic-gate 	}
11370Sstevel@tonic-gate #endif
11380Sstevel@tonic-gate 
11390Sstevel@tonic-gate 	return (rval);
11400Sstevel@tonic-gate }
11410Sstevel@tonic-gate 
11420Sstevel@tonic-gate /*
11430Sstevel@tonic-gate  * If the device is a PCI bus device (i.e bus-range property exists) then
11440Sstevel@tonic-gate  * claim the bus numbers used by the device from the specified bus
11450Sstevel@tonic-gate  * resource map.
11460Sstevel@tonic-gate  */
11470Sstevel@tonic-gate static int
claim_pci_busnum(dev_info_t * dip,void * arg)11480Sstevel@tonic-gate claim_pci_busnum(dev_info_t *dip, void *arg)
11490Sstevel@tonic-gate {
11500Sstevel@tonic-gate 	struct bus_range pci_bus_range;
11510Sstevel@tonic-gate 	struct busnum_ctrl *ctrl;
11520Sstevel@tonic-gate 	ndi_ra_request_t req;
11530Sstevel@tonic-gate 	char bus_type[16] = "(unknown)";
11540Sstevel@tonic-gate 	int len;
11550Sstevel@tonic-gate 	uint64_t base;
11560Sstevel@tonic-gate 	uint64_t retlen;
11570Sstevel@tonic-gate 
11580Sstevel@tonic-gate 	ctrl = (struct busnum_ctrl *)arg;
11590Sstevel@tonic-gate 
11600Sstevel@tonic-gate 	/* check if this is a PCI bus node */
11610Sstevel@tonic-gate 	len = sizeof (bus_type);
11620Sstevel@tonic-gate 	if (ddi_prop_op(DDI_DEV_T_ANY, dip, PROP_LEN_AND_VAL_BUF,
11630Sstevel@tonic-gate 	    DDI_PROP_CANSLEEP | DDI_PROP_DONTPASS, "device_type",
11640Sstevel@tonic-gate 	    (caddr_t)&bus_type, &len) != DDI_SUCCESS)
11650Sstevel@tonic-gate 		return (DDI_WALK_PRUNECHILD);
11660Sstevel@tonic-gate 
1167881Sjohnny 	/* it is not a pci/pci-ex bus type */
1168226Set142600 	if ((strcmp(bus_type, "pci") != 0) && (strcmp(bus_type, "pciex") != 0))
11690Sstevel@tonic-gate 		return (DDI_WALK_PRUNECHILD);
11700Sstevel@tonic-gate 
11710Sstevel@tonic-gate 	/* look for the bus-range property */
11720Sstevel@tonic-gate 	len = sizeof (struct bus_range);
1173506Scth 	if (ddi_getlongprop_buf(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
11740Sstevel@tonic-gate 	    "bus-range", (caddr_t)&pci_bus_range, &len) == DDI_SUCCESS) {
11750Sstevel@tonic-gate 		if ((pci_bus_range.lo >= ctrl->range->lo) &&
11760Sstevel@tonic-gate 		    (pci_bus_range.hi <= ctrl->range->hi)) {
11770Sstevel@tonic-gate 
11780Sstevel@tonic-gate 			/* claim the bus range from the bus resource map */
11790Sstevel@tonic-gate 			bzero((caddr_t)&req, sizeof (req));
11800Sstevel@tonic-gate 			req.ra_addr = (uint64_t)pci_bus_range.lo;
11810Sstevel@tonic-gate 			req.ra_flags |= NDI_RA_ALLOC_SPECIFIED;
11820Sstevel@tonic-gate 			req.ra_len = (uint64_t)pci_bus_range.hi -
11830Sstevel@tonic-gate 			    (uint64_t)pci_bus_range.lo + 1;
11840Sstevel@tonic-gate 			if (ndi_ra_alloc(ctrl->dip, &req, &base, &retlen,
11850Sstevel@tonic-gate 			    NDI_RA_TYPE_PCI_BUSNUM, 0) == NDI_SUCCESS)
11860Sstevel@tonic-gate 				return (DDI_WALK_PRUNECHILD);
11870Sstevel@tonic-gate 		}
11880Sstevel@tonic-gate 	}
11890Sstevel@tonic-gate 
11900Sstevel@tonic-gate 	/*
11910Sstevel@tonic-gate 	 * Error return.
11920Sstevel@tonic-gate 	 */
11930Sstevel@tonic-gate 	ctrl->rv = DDI_FAILURE;
11940Sstevel@tonic-gate 	return (DDI_WALK_TERMINATE);
11950Sstevel@tonic-gate }
11960Sstevel@tonic-gate 
11970Sstevel@tonic-gate void
pci_resource_destroy(dev_info_t * dip)11980Sstevel@tonic-gate pci_resource_destroy(dev_info_t *dip)
11990Sstevel@tonic-gate {
12000Sstevel@tonic-gate 	(void) ndi_ra_map_destroy(dip, NDI_RA_TYPE_IO);
12010Sstevel@tonic-gate 
12020Sstevel@tonic-gate 	(void) ndi_ra_map_destroy(dip, NDI_RA_TYPE_MEM);
12030Sstevel@tonic-gate 
12040Sstevel@tonic-gate 	(void) ndi_ra_map_destroy(dip, NDI_RA_TYPE_PCI_BUSNUM);
12050Sstevel@tonic-gate 
12060Sstevel@tonic-gate 	(void) ndi_ra_map_destroy(dip, NDI_RA_TYPE_PCI_PREFETCH_MEM);
12070Sstevel@tonic-gate }
12080Sstevel@tonic-gate 
12090Sstevel@tonic-gate 
12100Sstevel@tonic-gate int
pci_resource_setup_avail(dev_info_t * dip,pci_regspec_t * avail_p,int entries)12110Sstevel@tonic-gate pci_resource_setup_avail(dev_info_t *dip, pci_regspec_t *avail_p, int entries)
12120Sstevel@tonic-gate {
12130Sstevel@tonic-gate 	int i;
12140Sstevel@tonic-gate 
12150Sstevel@tonic-gate 	if (ndi_ra_map_setup(dip, NDI_RA_TYPE_MEM) == NDI_FAILURE)
12160Sstevel@tonic-gate 		return (NDI_FAILURE);
12170Sstevel@tonic-gate 	if (ndi_ra_map_setup(dip, NDI_RA_TYPE_IO) == NDI_FAILURE)
12180Sstevel@tonic-gate 		return (NDI_FAILURE);
12190Sstevel@tonic-gate 	if (ndi_ra_map_setup(dip, NDI_RA_TYPE_PCI_PREFETCH_MEM) == NDI_FAILURE)
12200Sstevel@tonic-gate 		return (NDI_FAILURE);
12210Sstevel@tonic-gate 
12220Sstevel@tonic-gate 	/* for each entry in the PCI "available" property */
12230Sstevel@tonic-gate 	for (i = 0; i < entries; i++, avail_p++) {
12240Sstevel@tonic-gate 		if (avail_p->pci_phys_hi == -1u)
12250Sstevel@tonic-gate 			goto err;
12260Sstevel@tonic-gate 
12270Sstevel@tonic-gate 		switch (PCI_REG_ADDR_G(avail_p->pci_phys_hi)) {
12280Sstevel@tonic-gate 		case PCI_REG_ADDR_G(PCI_ADDR_MEM32): {
122910923SEvan.Yan@Sun.COM 			(void) ndi_ra_free(dip, (uint64_t)avail_p->pci_phys_low,
123010923SEvan.Yan@Sun.COM 			    (uint64_t)avail_p->pci_size_low,
123110923SEvan.Yan@Sun.COM 			    (avail_p->pci_phys_hi & PCI_REG_PF_M) ?
123210923SEvan.Yan@Sun.COM 			    NDI_RA_TYPE_PCI_PREFETCH_MEM : NDI_RA_TYPE_MEM,
123310923SEvan.Yan@Sun.COM 			    0);
12340Sstevel@tonic-gate 			}
12350Sstevel@tonic-gate 			break;
12360Sstevel@tonic-gate 		case PCI_REG_ADDR_G(PCI_ADDR_IO):
123710923SEvan.Yan@Sun.COM 			(void) ndi_ra_free(dip, (uint64_t)avail_p->pci_phys_low,
123810923SEvan.Yan@Sun.COM 			    (uint64_t)avail_p->pci_size_low, NDI_RA_TYPE_IO, 0);
12390Sstevel@tonic-gate 			break;
12400Sstevel@tonic-gate 		default:
12410Sstevel@tonic-gate 			goto err;
12420Sstevel@tonic-gate 		}
12430Sstevel@tonic-gate 	}
12440Sstevel@tonic-gate #ifdef BUSRA_DEBUG
12450Sstevel@tonic-gate 	if (busra_debug) {
12460Sstevel@tonic-gate 		(void) ra_dump_all(NULL, dip);
12470Sstevel@tonic-gate 	}
12480Sstevel@tonic-gate #endif
12490Sstevel@tonic-gate 	return (NDI_SUCCESS);
12500Sstevel@tonic-gate 
12510Sstevel@tonic-gate err:
12520Sstevel@tonic-gate 	cmn_err(CE_WARN, "pci_resource_setup_avail: bad entry[%d]=%x\n",
125310923SEvan.Yan@Sun.COM 	    i, avail_p->pci_phys_hi);
12540Sstevel@tonic-gate 	return (NDI_FAILURE);
12550Sstevel@tonic-gate }
125610923SEvan.Yan@Sun.COM 
125710923SEvan.Yan@Sun.COM /*
125810923SEvan.Yan@Sun.COM  * Return true if the devinfo node resides on PCI or PCI Express bus,
125910923SEvan.Yan@Sun.COM  * sitting in a PCI Express hierarchy.
126010923SEvan.Yan@Sun.COM  */
126110923SEvan.Yan@Sun.COM static boolean_t
is_pcie_fabric(dev_info_t * dip)126210923SEvan.Yan@Sun.COM is_pcie_fabric(dev_info_t *dip)
126310923SEvan.Yan@Sun.COM {
126410923SEvan.Yan@Sun.COM 	dev_info_t *root = ddi_root_node();
126510923SEvan.Yan@Sun.COM 	dev_info_t *pdip;
126610923SEvan.Yan@Sun.COM 	boolean_t found = B_FALSE;
126710923SEvan.Yan@Sun.COM 	char *bus;
126810923SEvan.Yan@Sun.COM 
126910923SEvan.Yan@Sun.COM 	/*
127010923SEvan.Yan@Sun.COM 	 * Is this pci/pcie ?
127110923SEvan.Yan@Sun.COM 	 */
127210923SEvan.Yan@Sun.COM 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, dip,
127310923SEvan.Yan@Sun.COM 	    DDI_PROP_DONTPASS, "device_type", &bus) !=
127410923SEvan.Yan@Sun.COM 	    DDI_PROP_SUCCESS) {
127510923SEvan.Yan@Sun.COM 		DEBUGPRT(CE_WARN, "is_pcie_fabric: cannot find "
127610923SEvan.Yan@Sun.COM 		    "\"device_type\" property for dip %p\n", (void *)dip);
127710923SEvan.Yan@Sun.COM 		return (B_FALSE);
127810923SEvan.Yan@Sun.COM 	}
127910923SEvan.Yan@Sun.COM 
128010923SEvan.Yan@Sun.COM 	if (strcmp(bus, "pciex") == 0) {
128110923SEvan.Yan@Sun.COM 		/* pcie bus, done */
128210923SEvan.Yan@Sun.COM 		ddi_prop_free(bus);
128310923SEvan.Yan@Sun.COM 		return (B_TRUE);
128410923SEvan.Yan@Sun.COM 	} else if (strcmp(bus, "pci") == 0) {
128510923SEvan.Yan@Sun.COM 		/*
128610923SEvan.Yan@Sun.COM 		 * pci bus, fall through to check if it resides in
128710923SEvan.Yan@Sun.COM 		 * a pcie hierarchy.
128810923SEvan.Yan@Sun.COM 		 */
128910923SEvan.Yan@Sun.COM 		ddi_prop_free(bus);
129010923SEvan.Yan@Sun.COM 	} else {
129110923SEvan.Yan@Sun.COM 		/* other bus, return failure */
129210923SEvan.Yan@Sun.COM 		ddi_prop_free(bus);
129310923SEvan.Yan@Sun.COM 		return (B_FALSE);
129410923SEvan.Yan@Sun.COM 	}
129510923SEvan.Yan@Sun.COM 
129610923SEvan.Yan@Sun.COM 	/*
129710923SEvan.Yan@Sun.COM 	 * Does this device reside in a pcie fabric ?
129810923SEvan.Yan@Sun.COM 	 */
129910923SEvan.Yan@Sun.COM 	for (pdip = ddi_get_parent(dip); pdip && (pdip != root) &&
130010923SEvan.Yan@Sun.COM 	    !found; pdip = ddi_get_parent(pdip)) {
130110923SEvan.Yan@Sun.COM 		if (ddi_prop_lookup_string(DDI_DEV_T_ANY, pdip,
130210923SEvan.Yan@Sun.COM 		    DDI_PROP_DONTPASS, "device_type", &bus) !=
130310923SEvan.Yan@Sun.COM 		    DDI_PROP_SUCCESS)
130410923SEvan.Yan@Sun.COM 			break;
130510923SEvan.Yan@Sun.COM 
130610923SEvan.Yan@Sun.COM 		if (strcmp(bus, "pciex") == 0)
130710923SEvan.Yan@Sun.COM 			found = B_TRUE;
130810923SEvan.Yan@Sun.COM 
130910923SEvan.Yan@Sun.COM 		ddi_prop_free(bus);
131010923SEvan.Yan@Sun.COM 	}
131110923SEvan.Yan@Sun.COM 
131210923SEvan.Yan@Sun.COM 	return (found);
131310923SEvan.Yan@Sun.COM }
131410923SEvan.Yan@Sun.COM 
131510923SEvan.Yan@Sun.COM /*
131610923SEvan.Yan@Sun.COM  * Remove a piece of IO/MEM resource from "available" property of 'dip'.
131710923SEvan.Yan@Sun.COM  */
131810923SEvan.Yan@Sun.COM static int
pci_get_available_prop(dev_info_t * dip,uint64_t base,uint64_t len,char * busra_type)131910923SEvan.Yan@Sun.COM pci_get_available_prop(dev_info_t *dip, uint64_t base, uint64_t len,
132010923SEvan.Yan@Sun.COM     char *busra_type)
132110923SEvan.Yan@Sun.COM {
132210923SEvan.Yan@Sun.COM 	pci_regspec_t	*regs, *newregs;
132310923SEvan.Yan@Sun.COM 	uint_t		status;
132410923SEvan.Yan@Sun.COM 	int		rlen, rcount;
132510923SEvan.Yan@Sun.COM 	int		i, j, k;
132610923SEvan.Yan@Sun.COM 	uint64_t	dlen;
132710923SEvan.Yan@Sun.COM 	boolean_t	found = B_FALSE;
132810923SEvan.Yan@Sun.COM 	uint32_t	type;
132910923SEvan.Yan@Sun.COM 
133010923SEvan.Yan@Sun.COM 	/* check if we're manipulating MEM/IO resource */
133110923SEvan.Yan@Sun.COM 	if ((type = pci_type_ra2pci(busra_type)) == PCI_ADDR_TYPE_INVAL)
133210923SEvan.Yan@Sun.COM 		return (DDI_SUCCESS);
133310923SEvan.Yan@Sun.COM 
133410923SEvan.Yan@Sun.COM 	/* check if dip is a pci/pcie device resides in a pcie fabric */
133510923SEvan.Yan@Sun.COM 	if (!is_pcie_fabric(dip))
133610923SEvan.Yan@Sun.COM 		return (DDI_SUCCESS);
133710923SEvan.Yan@Sun.COM 
133810923SEvan.Yan@Sun.COM 	status = ddi_getlongprop(DDI_DEV_T_ANY, dip,
133910923SEvan.Yan@Sun.COM 	    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
134010923SEvan.Yan@Sun.COM 	    "available", (caddr_t)&regs, &rlen);
134110923SEvan.Yan@Sun.COM 
134210923SEvan.Yan@Sun.COM 	ASSERT(status == DDI_SUCCESS);
134310923SEvan.Yan@Sun.COM 	if (status != DDI_SUCCESS)
134410923SEvan.Yan@Sun.COM 		return (status);
134510923SEvan.Yan@Sun.COM 
134610923SEvan.Yan@Sun.COM 	/*
134710923SEvan.Yan@Sun.COM 	 * The updated "available" property will at most have one more entry
134810923SEvan.Yan@Sun.COM 	 * than existing one (when the requested range is in the middle of
134910923SEvan.Yan@Sun.COM 	 * the matched property entry)
135010923SEvan.Yan@Sun.COM 	 */
135110923SEvan.Yan@Sun.COM 	newregs = kmem_alloc(rlen + sizeof (pci_regspec_t), KM_SLEEP);
135210923SEvan.Yan@Sun.COM 
135310923SEvan.Yan@Sun.COM 	rcount = rlen / sizeof (pci_regspec_t);
135410923SEvan.Yan@Sun.COM 	for (i = 0, j = 0; i < rcount; i++) {
135510923SEvan.Yan@Sun.COM 		if (type == (regs[i].pci_phys_hi & PCI_ADDR_TYPE_MASK)) {
135610923SEvan.Yan@Sun.COM 			uint64_t range_base, range_len;
135710923SEvan.Yan@Sun.COM 
135810923SEvan.Yan@Sun.COM 			range_base = ((uint64_t)(regs[i].pci_phys_mid) << 32) |
135910923SEvan.Yan@Sun.COM 			    ((uint64_t)(regs[i].pci_phys_low));
136010923SEvan.Yan@Sun.COM 			range_len = ((uint64_t)(regs[i].pci_size_hi) << 32) |
136110923SEvan.Yan@Sun.COM 			    ((uint64_t)(regs[i].pci_size_low));
136210923SEvan.Yan@Sun.COM 
136310923SEvan.Yan@Sun.COM 			if ((base < range_base) ||
136410923SEvan.Yan@Sun.COM 			    (base + len > range_base + range_len)) {
136510923SEvan.Yan@Sun.COM 				/*
136610923SEvan.Yan@Sun.COM 				 * not a match, copy the entry
136710923SEvan.Yan@Sun.COM 				 */
136810923SEvan.Yan@Sun.COM 				goto copy_entry;
136910923SEvan.Yan@Sun.COM 			}
137010923SEvan.Yan@Sun.COM 
137110923SEvan.Yan@Sun.COM 			/*
137210923SEvan.Yan@Sun.COM 			 * range_base	base	base+len	range_base
137310923SEvan.Yan@Sun.COM 			 *					+range_len
137410923SEvan.Yan@Sun.COM 			 *   +------------+-----------+----------+
137510923SEvan.Yan@Sun.COM 			 *   |		  |///////////|		 |
137610923SEvan.Yan@Sun.COM 			 *   +------------+-----------+----------+
137710923SEvan.Yan@Sun.COM 			 */
137810923SEvan.Yan@Sun.COM 			/*
137910923SEvan.Yan@Sun.COM 			 * Found a match, remove the range out of this entry.
138010923SEvan.Yan@Sun.COM 			 */
138110923SEvan.Yan@Sun.COM 			found = B_TRUE;
138210923SEvan.Yan@Sun.COM 
138310923SEvan.Yan@Sun.COM 			dlen = base - range_base;
138410923SEvan.Yan@Sun.COM 			if (dlen != 0) {
138510923SEvan.Yan@Sun.COM 				newregs[j].pci_phys_hi = regs[i].pci_phys_hi;
138610923SEvan.Yan@Sun.COM 				newregs[j].pci_phys_mid =
138710923SEvan.Yan@Sun.COM 				    (uint32_t)(range_base >> 32);
138810923SEvan.Yan@Sun.COM 				newregs[j].pci_phys_low =
138910923SEvan.Yan@Sun.COM 				    (uint32_t)(range_base);
139010923SEvan.Yan@Sun.COM 				newregs[j].pci_size_hi = (uint32_t)(dlen >> 32);
139110923SEvan.Yan@Sun.COM 				newregs[j].pci_size_low = (uint32_t)dlen;
139210923SEvan.Yan@Sun.COM 				j++;
139310923SEvan.Yan@Sun.COM 			}
139410923SEvan.Yan@Sun.COM 
139510923SEvan.Yan@Sun.COM 			dlen = (range_base + range_len) - (base + len);
139610923SEvan.Yan@Sun.COM 			if (dlen != 0) {
139710923SEvan.Yan@Sun.COM 				newregs[j].pci_phys_hi = regs[i].pci_phys_hi;
139810923SEvan.Yan@Sun.COM 				newregs[j].pci_phys_mid =
139910923SEvan.Yan@Sun.COM 				    (uint32_t)((base + len)>> 32);
140010923SEvan.Yan@Sun.COM 				newregs[j].pci_phys_low =
140110923SEvan.Yan@Sun.COM 				    (uint32_t)(base + len);
140210923SEvan.Yan@Sun.COM 				newregs[j].pci_size_hi = (uint32_t)(dlen >> 32);
140310923SEvan.Yan@Sun.COM 				newregs[j].pci_size_low = (uint32_t)dlen;
140410923SEvan.Yan@Sun.COM 				j++;
140510923SEvan.Yan@Sun.COM 			}
140610923SEvan.Yan@Sun.COM 
140710923SEvan.Yan@Sun.COM 			/*
140810923SEvan.Yan@Sun.COM 			 * We've allocated the resource from the matched
140910923SEvan.Yan@Sun.COM 			 * entry, almost finished but still need to copy
141010923SEvan.Yan@Sun.COM 			 * the rest entries from the original property
141110923SEvan.Yan@Sun.COM 			 * array.
141210923SEvan.Yan@Sun.COM 			 */
141310923SEvan.Yan@Sun.COM 			for (k = i + 1; k < rcount; k++) {
141410923SEvan.Yan@Sun.COM 				newregs[j] = regs[k];
141510923SEvan.Yan@Sun.COM 				j++;
141610923SEvan.Yan@Sun.COM 			}
141710923SEvan.Yan@Sun.COM 
141810923SEvan.Yan@Sun.COM 			goto done;
141910923SEvan.Yan@Sun.COM 
142010923SEvan.Yan@Sun.COM 		} else {
142110923SEvan.Yan@Sun.COM copy_entry:
142210923SEvan.Yan@Sun.COM 			newregs[j] = regs[i];
142310923SEvan.Yan@Sun.COM 			j++;
142410923SEvan.Yan@Sun.COM 		}
142510923SEvan.Yan@Sun.COM 	}
142610923SEvan.Yan@Sun.COM 
142710923SEvan.Yan@Sun.COM done:
142810923SEvan.Yan@Sun.COM 	/*
142910923SEvan.Yan@Sun.COM 	 * This should not fail so assert it. For non-debug kernel we don't
143010923SEvan.Yan@Sun.COM 	 * want to panic thus only logging a warning message.
143110923SEvan.Yan@Sun.COM 	 */
143210923SEvan.Yan@Sun.COM 	ASSERT(found == B_TRUE);
143310923SEvan.Yan@Sun.COM 	if (!found) {
143410923SEvan.Yan@Sun.COM 		cmn_err(CE_WARN, "pci_get_available_prop: failed to remove "
143510923SEvan.Yan@Sun.COM 		    "resource from dip %p : base 0x%" PRIx64 ", len 0x%" PRIX64
143610923SEvan.Yan@Sun.COM 		    ", type 0x%x\n", (void *)dip, base, len, type);
143710923SEvan.Yan@Sun.COM 		kmem_free(newregs, rlen + sizeof (pci_regspec_t));
143810923SEvan.Yan@Sun.COM 		kmem_free(regs, rlen);
143910923SEvan.Yan@Sun.COM 
144010923SEvan.Yan@Sun.COM 		return (DDI_FAILURE);
144110923SEvan.Yan@Sun.COM 	}
144210923SEvan.Yan@Sun.COM 
144310923SEvan.Yan@Sun.COM 	/*
144410923SEvan.Yan@Sun.COM 	 * Found the resources from parent, update the "available"
144510923SEvan.Yan@Sun.COM 	 * property.
144610923SEvan.Yan@Sun.COM 	 */
144710923SEvan.Yan@Sun.COM 	if (j == 0) {
144810923SEvan.Yan@Sun.COM 		/* all the resources are consumed, remove the property */
144910923SEvan.Yan@Sun.COM 		(void) ndi_prop_remove(DDI_DEV_T_NONE, dip, "available");
145010923SEvan.Yan@Sun.COM 	} else {
145110923SEvan.Yan@Sun.COM 		/*
145210923SEvan.Yan@Sun.COM 		 * There are still resource available in the parent dip,
145310923SEvan.Yan@Sun.COM 		 * update with the remaining resources.
145410923SEvan.Yan@Sun.COM 		 */
145510923SEvan.Yan@Sun.COM 		(void) ndi_prop_update_int_array(DDI_DEV_T_NONE, dip,
145610923SEvan.Yan@Sun.COM 		    "available", (int *)newregs,
145710923SEvan.Yan@Sun.COM 		    (j * sizeof (pci_regspec_t)) / sizeof (int));
145810923SEvan.Yan@Sun.COM 	}
145910923SEvan.Yan@Sun.COM 
146010923SEvan.Yan@Sun.COM 	kmem_free(newregs, rlen + sizeof (pci_regspec_t));
146110923SEvan.Yan@Sun.COM 	kmem_free(regs, rlen);
146210923SEvan.Yan@Sun.COM 
146310923SEvan.Yan@Sun.COM 	return (DDI_SUCCESS);
146410923SEvan.Yan@Sun.COM }
146510923SEvan.Yan@Sun.COM 
146610923SEvan.Yan@Sun.COM /*
146710923SEvan.Yan@Sun.COM  * Add a piece of IO/MEM resource to "available" property of 'dip'.
146810923SEvan.Yan@Sun.COM  */
146910923SEvan.Yan@Sun.COM static int
pci_put_available_prop(dev_info_t * dip,uint64_t base,uint64_t len,char * busra_type)147010923SEvan.Yan@Sun.COM pci_put_available_prop(dev_info_t *dip, uint64_t base, uint64_t len,
147110923SEvan.Yan@Sun.COM     char *busra_type)
147210923SEvan.Yan@Sun.COM {
147310923SEvan.Yan@Sun.COM 	pci_regspec_t	*regs, *newregs;
147410923SEvan.Yan@Sun.COM 	uint_t		status;
147510923SEvan.Yan@Sun.COM 	int		rlen, rcount;
147610923SEvan.Yan@Sun.COM 	int		i, j, k;
147710923SEvan.Yan@Sun.COM 	int		matched = 0;
147810923SEvan.Yan@Sun.COM 	uint64_t	orig_base = base;
147910923SEvan.Yan@Sun.COM 	uint64_t	orig_len = len;
148010923SEvan.Yan@Sun.COM 	uint32_t	type;
148110923SEvan.Yan@Sun.COM 
148210923SEvan.Yan@Sun.COM 	/* check if we're manipulating MEM/IO resource */
148310923SEvan.Yan@Sun.COM 	if ((type = pci_type_ra2pci(busra_type)) == PCI_ADDR_TYPE_INVAL)
148410923SEvan.Yan@Sun.COM 		return (DDI_SUCCESS);
148510923SEvan.Yan@Sun.COM 
148610923SEvan.Yan@Sun.COM 	/* check if dip is a pci/pcie device resides in a pcie fabric */
148710923SEvan.Yan@Sun.COM 	if (!is_pcie_fabric(dip))
148810923SEvan.Yan@Sun.COM 		return (DDI_SUCCESS);
148910923SEvan.Yan@Sun.COM 
149010923SEvan.Yan@Sun.COM 	status = ddi_getlongprop(DDI_DEV_T_ANY, dip,
149110923SEvan.Yan@Sun.COM 	    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
149210923SEvan.Yan@Sun.COM 	    "available", (caddr_t)&regs, &rlen);
149310923SEvan.Yan@Sun.COM 
149410923SEvan.Yan@Sun.COM 	switch (status) {
149510923SEvan.Yan@Sun.COM 		case DDI_PROP_NOT_FOUND:
149610923SEvan.Yan@Sun.COM 			goto not_found;
149710923SEvan.Yan@Sun.COM 
149810923SEvan.Yan@Sun.COM 		case DDI_PROP_SUCCESS:
149910923SEvan.Yan@Sun.COM 			break;
150010923SEvan.Yan@Sun.COM 
150110923SEvan.Yan@Sun.COM 		default:
150210923SEvan.Yan@Sun.COM 			return (status);
150310923SEvan.Yan@Sun.COM 	}
150410923SEvan.Yan@Sun.COM 
150510923SEvan.Yan@Sun.COM 	/*
150610923SEvan.Yan@Sun.COM 	 * The "available" property exist on the node, try to put this
150710923SEvan.Yan@Sun.COM 	 * resource back, merge if there are adjacent resources.
150810923SEvan.Yan@Sun.COM 	 *
150910923SEvan.Yan@Sun.COM 	 * The updated "available" property will at most have one more entry
151010923SEvan.Yan@Sun.COM 	 * than existing one (when there is no adjacent entries thus the new
151110923SEvan.Yan@Sun.COM 	 * resource is appended at the end)
151210923SEvan.Yan@Sun.COM 	 */
151310923SEvan.Yan@Sun.COM 	newregs = kmem_alloc(rlen + sizeof (pci_regspec_t), KM_SLEEP);
151410923SEvan.Yan@Sun.COM 
151510923SEvan.Yan@Sun.COM 	rcount = rlen / sizeof (pci_regspec_t);
151610923SEvan.Yan@Sun.COM 	for (i = 0, j = 0; i < rcount; i++) {
151710923SEvan.Yan@Sun.COM 		if (type == (regs[i].pci_phys_hi & PCI_ADDR_TYPE_MASK)) {
151810923SEvan.Yan@Sun.COM 			uint64_t range_base, range_len;
151910923SEvan.Yan@Sun.COM 
152010923SEvan.Yan@Sun.COM 			range_base = ((uint64_t)(regs[i].pci_phys_mid) << 32) |
152110923SEvan.Yan@Sun.COM 			    ((uint64_t)(regs[i].pci_phys_low));
152210923SEvan.Yan@Sun.COM 			range_len = ((uint64_t)(regs[i].pci_size_hi) << 32) |
152310923SEvan.Yan@Sun.COM 			    ((uint64_t)(regs[i].pci_size_low));
152410923SEvan.Yan@Sun.COM 
152510923SEvan.Yan@Sun.COM 			if ((base + len < range_base) ||
152610923SEvan.Yan@Sun.COM 			    (base > range_base + range_len)) {
152710923SEvan.Yan@Sun.COM 				/*
152810923SEvan.Yan@Sun.COM 				 * Not adjacent, copy the entry and contiue
152910923SEvan.Yan@Sun.COM 				 */
153010923SEvan.Yan@Sun.COM 				goto copy_entry;
153110923SEvan.Yan@Sun.COM 			}
153210923SEvan.Yan@Sun.COM 
153310923SEvan.Yan@Sun.COM 			/*
153410923SEvan.Yan@Sun.COM 			 * Adjacent or overlap?
153510923SEvan.Yan@Sun.COM 			 *
153610923SEvan.Yan@Sun.COM 			 * Should not have overlapping resources so assert it.
153710923SEvan.Yan@Sun.COM 			 * For non-debug kernel we don't want to panic thus
153810923SEvan.Yan@Sun.COM 			 * only logging a warning message.
153910923SEvan.Yan@Sun.COM 			 */
154010923SEvan.Yan@Sun.COM #if 0
154110923SEvan.Yan@Sun.COM 			ASSERT((base + len == range_base) ||
154210923SEvan.Yan@Sun.COM 			    (base == range_base + range_len));
154310923SEvan.Yan@Sun.COM #endif
154410923SEvan.Yan@Sun.COM 			if ((base + len != range_base) &&
154510923SEvan.Yan@Sun.COM 			    (base != range_base + range_len)) {
154610923SEvan.Yan@Sun.COM 				cmn_err(CE_WARN, "pci_put_available_prop: "
154710923SEvan.Yan@Sun.COM 				    "failed to add resource to dip %p : "
154810923SEvan.Yan@Sun.COM 				    "base 0x%" PRIx64 ", len 0x%" PRIx64 " "
154910923SEvan.Yan@Sun.COM 				    "overlaps with existing resource "
155010923SEvan.Yan@Sun.COM 				    "base 0x%" PRIx64 ", len 0x%" PRIx64 "\n",
155110923SEvan.Yan@Sun.COM 				    (void *)dip, orig_base, orig_len,
155210923SEvan.Yan@Sun.COM 				    range_base, range_len);
155310923SEvan.Yan@Sun.COM 
155410923SEvan.Yan@Sun.COM 				goto failure;
155510923SEvan.Yan@Sun.COM 			}
155610923SEvan.Yan@Sun.COM 
155710923SEvan.Yan@Sun.COM 			/*
155810923SEvan.Yan@Sun.COM 			 * On the left:
155910923SEvan.Yan@Sun.COM 			 *
156010923SEvan.Yan@Sun.COM 			 * base		range_base
156110923SEvan.Yan@Sun.COM 			 *   +-------------+-------------+
156210923SEvan.Yan@Sun.COM 			 *   |/////////////|		 |
156310923SEvan.Yan@Sun.COM 			 *   +-------------+-------------+
156410923SEvan.Yan@Sun.COM 			 *	len		range_len
156510923SEvan.Yan@Sun.COM 			 *
156610923SEvan.Yan@Sun.COM 			 * On the right:
156710923SEvan.Yan@Sun.COM 			 *
156810923SEvan.Yan@Sun.COM 			 * range_base	 base
156910923SEvan.Yan@Sun.COM 			 *   +-------------+-------------+
157010923SEvan.Yan@Sun.COM 			 *   |		   |/////////////|
157110923SEvan.Yan@Sun.COM 			 *   +-------------+-------------+
157210923SEvan.Yan@Sun.COM 			 *	range_len	len
157310923SEvan.Yan@Sun.COM 			 */
157410923SEvan.Yan@Sun.COM 			/*
157510923SEvan.Yan@Sun.COM 			 * There are at most two piece of resources adjacent
157610923SEvan.Yan@Sun.COM 			 * with this resource, assert it.
157710923SEvan.Yan@Sun.COM 			 */
157810923SEvan.Yan@Sun.COM 			ASSERT(matched < 2);
157910923SEvan.Yan@Sun.COM 
158010923SEvan.Yan@Sun.COM 			if (!(matched < 2)) {
158110923SEvan.Yan@Sun.COM 				cmn_err(CE_WARN, "pci_put_available_prop: "
158210923SEvan.Yan@Sun.COM 				    "failed to add resource to dip %p : "
158310923SEvan.Yan@Sun.COM 				    "base 0x%" PRIx64 ", len 0x%" PRIx64 " "
158410923SEvan.Yan@Sun.COM 				    "found overlaps in existing resources\n",
158510923SEvan.Yan@Sun.COM 				    (void *)dip, orig_base, orig_len);
158610923SEvan.Yan@Sun.COM 
158710923SEvan.Yan@Sun.COM 				goto failure;
158810923SEvan.Yan@Sun.COM 			}
158910923SEvan.Yan@Sun.COM 
159010923SEvan.Yan@Sun.COM 			/* setup base & len to refer to the merged range */
159110923SEvan.Yan@Sun.COM 			len += range_len;
159210923SEvan.Yan@Sun.COM 			if (base == range_base + range_len)
159310923SEvan.Yan@Sun.COM 				base = range_base;
159410923SEvan.Yan@Sun.COM 
159510923SEvan.Yan@Sun.COM 			if (matched == 0) {
159610923SEvan.Yan@Sun.COM 				/*
159710923SEvan.Yan@Sun.COM 				 * One adjacent entry, add this resource in
159810923SEvan.Yan@Sun.COM 				 */
159910923SEvan.Yan@Sun.COM 				newregs[j].pci_phys_hi = regs[i].pci_phys_hi;
160010923SEvan.Yan@Sun.COM 				newregs[j].pci_phys_mid =
160110923SEvan.Yan@Sun.COM 				    (uint32_t)(base >> 32);
160210923SEvan.Yan@Sun.COM 				newregs[j].pci_phys_low = (uint32_t)(base);
160310923SEvan.Yan@Sun.COM 				newregs[j].pci_size_hi = (uint32_t)(len >> 32);
160410923SEvan.Yan@Sun.COM 				newregs[j].pci_size_low = (uint32_t)len;
160510923SEvan.Yan@Sun.COM 
160610923SEvan.Yan@Sun.COM 				matched = 1;
160710923SEvan.Yan@Sun.COM 				k = j;
160810923SEvan.Yan@Sun.COM 				j++;
160910923SEvan.Yan@Sun.COM 			} else { /* matched == 1 */
161010923SEvan.Yan@Sun.COM 				/*
161110923SEvan.Yan@Sun.COM 				 * Two adjacent entries, merge them together
161210923SEvan.Yan@Sun.COM 				 */
161310923SEvan.Yan@Sun.COM 				newregs[k].pci_phys_hi = regs[i].pci_phys_hi;
161410923SEvan.Yan@Sun.COM 				newregs[k].pci_phys_mid =
161510923SEvan.Yan@Sun.COM 				    (uint32_t)(base >> 32);
161610923SEvan.Yan@Sun.COM 				newregs[k].pci_phys_low = (uint32_t)(base);
161710923SEvan.Yan@Sun.COM 				newregs[k].pci_size_hi = (uint32_t)(len >> 32);
161810923SEvan.Yan@Sun.COM 				newregs[k].pci_size_low = (uint32_t)len;
161910923SEvan.Yan@Sun.COM 
162010923SEvan.Yan@Sun.COM 				matched = 2;
162110923SEvan.Yan@Sun.COM 			}
162210923SEvan.Yan@Sun.COM 		} else {
162310923SEvan.Yan@Sun.COM copy_entry:
162410923SEvan.Yan@Sun.COM 			newregs[j] = regs[i];
162510923SEvan.Yan@Sun.COM 			j++;
162610923SEvan.Yan@Sun.COM 		}
162710923SEvan.Yan@Sun.COM 	}
162810923SEvan.Yan@Sun.COM 
162910923SEvan.Yan@Sun.COM 	if (matched == 0) {
163010923SEvan.Yan@Sun.COM 		/* No adjacent entries, append at end */
163110923SEvan.Yan@Sun.COM 		ASSERT(j == rcount);
163210923SEvan.Yan@Sun.COM 
163310923SEvan.Yan@Sun.COM 		/*
163410923SEvan.Yan@Sun.COM 		 * According to page 15 of 1275 spec, bit "n" of "available"
163510923SEvan.Yan@Sun.COM 		 * should be set to 1.
163610923SEvan.Yan@Sun.COM 		 */
163710923SEvan.Yan@Sun.COM 		newregs[j].pci_phys_hi = type;
163810923SEvan.Yan@Sun.COM 		newregs[j].pci_phys_hi |= PCI_REG_REL_M;
163910923SEvan.Yan@Sun.COM 
164010923SEvan.Yan@Sun.COM 		newregs[j].pci_phys_mid = (uint32_t)(base >> 32);
164110923SEvan.Yan@Sun.COM 		newregs[j].pci_phys_low = (uint32_t)base;
164210923SEvan.Yan@Sun.COM 		newregs[j].pci_size_hi = (uint32_t)(len >> 32);
164310923SEvan.Yan@Sun.COM 		newregs[j].pci_size_low = (uint32_t)len;
164410923SEvan.Yan@Sun.COM 
164510923SEvan.Yan@Sun.COM 		j++;
164610923SEvan.Yan@Sun.COM 	}
164710923SEvan.Yan@Sun.COM 
164810923SEvan.Yan@Sun.COM 	(void) ndi_prop_update_int_array(DDI_DEV_T_NONE, dip,
164910923SEvan.Yan@Sun.COM 	    "available", (int *)newregs,
165010923SEvan.Yan@Sun.COM 	    (j * sizeof (pci_regspec_t)) / sizeof (int));
165110923SEvan.Yan@Sun.COM 
165210923SEvan.Yan@Sun.COM 	kmem_free(newregs, rlen + sizeof (pci_regspec_t));
165310923SEvan.Yan@Sun.COM 	kmem_free(regs, rlen);
165410923SEvan.Yan@Sun.COM 	return (DDI_SUCCESS);
165510923SEvan.Yan@Sun.COM 
165610923SEvan.Yan@Sun.COM not_found:
165710923SEvan.Yan@Sun.COM 	/*
165810923SEvan.Yan@Sun.COM 	 * There is no "available" property on the parent node, create it.
165910923SEvan.Yan@Sun.COM 	 */
166010923SEvan.Yan@Sun.COM 	newregs = kmem_alloc(sizeof (pci_regspec_t), KM_SLEEP);
166110923SEvan.Yan@Sun.COM 
166210923SEvan.Yan@Sun.COM 	/*
166310923SEvan.Yan@Sun.COM 	 * According to page 15 of 1275 spec, bit "n" of "available" should
166410923SEvan.Yan@Sun.COM 	 * be set to 1.
166510923SEvan.Yan@Sun.COM 	 */
166610923SEvan.Yan@Sun.COM 	newregs[0].pci_phys_hi = type;
166710923SEvan.Yan@Sun.COM 	newregs[0].pci_phys_hi |= PCI_REG_REL_M;
166810923SEvan.Yan@Sun.COM 
166910923SEvan.Yan@Sun.COM 	newregs[0].pci_phys_mid = (uint32_t)(base >> 32);
167010923SEvan.Yan@Sun.COM 	newregs[0].pci_phys_low = (uint32_t)base;
167110923SEvan.Yan@Sun.COM 	newregs[0].pci_size_hi = (uint32_t)(len >> 32);
167210923SEvan.Yan@Sun.COM 	newregs[0].pci_size_low = (uint32_t)len;
167310923SEvan.Yan@Sun.COM 
167410923SEvan.Yan@Sun.COM 	(void) ndi_prop_update_int_array(DDI_DEV_T_NONE, dip,
167510923SEvan.Yan@Sun.COM 	    "available", (int *)newregs,
167610923SEvan.Yan@Sun.COM 	    sizeof (pci_regspec_t) / sizeof (int));
167710923SEvan.Yan@Sun.COM 	kmem_free(newregs, sizeof (pci_regspec_t));
167810923SEvan.Yan@Sun.COM 	return (DDI_SUCCESS);
167910923SEvan.Yan@Sun.COM 
168010923SEvan.Yan@Sun.COM failure:
168110923SEvan.Yan@Sun.COM 	kmem_free(newregs, rlen + sizeof (pci_regspec_t));
168210923SEvan.Yan@Sun.COM 	kmem_free(regs, rlen);
168310923SEvan.Yan@Sun.COM 	return (DDI_FAILURE);
168410923SEvan.Yan@Sun.COM }
168510923SEvan.Yan@Sun.COM 
168610923SEvan.Yan@Sun.COM static uint32_t
pci_type_ra2pci(char * type)168710923SEvan.Yan@Sun.COM pci_type_ra2pci(char *type)
168810923SEvan.Yan@Sun.COM {
168910923SEvan.Yan@Sun.COM 	uint32_t	pci_type = PCI_ADDR_TYPE_INVAL;
169010923SEvan.Yan@Sun.COM 
169110923SEvan.Yan@Sun.COM 	/*
169210923SEvan.Yan@Sun.COM 	 * No 64 bit mem support for now
169310923SEvan.Yan@Sun.COM 	 */
169410923SEvan.Yan@Sun.COM 	if (strcmp(type, NDI_RA_TYPE_IO) == 0) {
169510923SEvan.Yan@Sun.COM 		pci_type = PCI_ADDR_IO;
169610923SEvan.Yan@Sun.COM 
169710923SEvan.Yan@Sun.COM 	} else if (strcmp(type, NDI_RA_TYPE_MEM) == 0) {
169810923SEvan.Yan@Sun.COM 		pci_type = PCI_ADDR_MEM32;
169910923SEvan.Yan@Sun.COM 
170010923SEvan.Yan@Sun.COM 	} else if (strcmp(type, NDI_RA_TYPE_PCI_PREFETCH_MEM)  == 0) {
170110923SEvan.Yan@Sun.COM 		pci_type = PCI_ADDR_MEM32;
170210923SEvan.Yan@Sun.COM 		pci_type |= PCI_REG_PF_M;
170310923SEvan.Yan@Sun.COM 	}
170410923SEvan.Yan@Sun.COM 
170510923SEvan.Yan@Sun.COM 	return (pci_type);
170610923SEvan.Yan@Sun.COM }
1707