xref: /titanic_52/usr/src/uts/common/io/busra.c (revision 70025d765b044c6d8594bb965a2247a61e991a99)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #if defined(DEBUG)
307c478bd9Sstevel@tonic-gate #define	BUSRA_DEBUG
317c478bd9Sstevel@tonic-gate #endif
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate /*
347c478bd9Sstevel@tonic-gate  * This module provides a set of resource management interfaces
357c478bd9Sstevel@tonic-gate  * to manage bus resources globally in the system.
367c478bd9Sstevel@tonic-gate  *
377c478bd9Sstevel@tonic-gate  * The bus nexus drivers are typically responsible to setup resource
387c478bd9Sstevel@tonic-gate  * maps for the bus resources available for a bus instance. However
397c478bd9Sstevel@tonic-gate  * this module also provides resource setup functions for PCI bus
407c478bd9Sstevel@tonic-gate  * (used by both SPARC and X86 platforms) and ISA bus instances (used
417c478bd9Sstevel@tonic-gate  * only for X86 platforms).
427c478bd9Sstevel@tonic-gate  */
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate #include <sys/types.h>
457c478bd9Sstevel@tonic-gate #include <sys/systm.h>
467c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
477c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
487c478bd9Sstevel@tonic-gate #include <sys/sunndi.h>
497c478bd9Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
507c478bd9Sstevel@tonic-gate #include <sys/ndi_impldefs.h>
517c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
527c478bd9Sstevel@tonic-gate #include <sys/pctypes.h>
537c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
547c478bd9Sstevel@tonic-gate #include <sys/debug.h>
557c478bd9Sstevel@tonic-gate #include <sys/spl.h>
567c478bd9Sstevel@tonic-gate #include <sys/pci.h>
577c478bd9Sstevel@tonic-gate #include <sys/autoconf.h>
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate #if defined(BUSRA_DEBUG)
607c478bd9Sstevel@tonic-gate int busra_debug = 0;
617c478bd9Sstevel@tonic-gate #define	DEBUGPRT \
627c478bd9Sstevel@tonic-gate 	if (busra_debug) cmn_err
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate #else
657c478bd9Sstevel@tonic-gate #define	DEBUGPRT \
667c478bd9Sstevel@tonic-gate 	if (0) cmn_err
677c478bd9Sstevel@tonic-gate #endif
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate /*
717c478bd9Sstevel@tonic-gate  * global mutex that protects the global list of resource maps.
727c478bd9Sstevel@tonic-gate  */
737c478bd9Sstevel@tonic-gate kmutex_t ra_lock;
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate /*
767c478bd9Sstevel@tonic-gate  * basic resource element
777c478bd9Sstevel@tonic-gate  */
787c478bd9Sstevel@tonic-gate struct ra_resource {
797c478bd9Sstevel@tonic-gate 	struct ra_resource *ra_next;
807c478bd9Sstevel@tonic-gate 	uint64_t	ra_base;
817c478bd9Sstevel@tonic-gate 	uint64_t 	ra_len;
827c478bd9Sstevel@tonic-gate };
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate /*
857c478bd9Sstevel@tonic-gate  * link list element for the list of dips (and their resource ranges)
867c478bd9Sstevel@tonic-gate  * for a particular resource type.
877c478bd9Sstevel@tonic-gate  * ra_rangeset points to the list of resources available
887c478bd9Sstevel@tonic-gate  * for this type and this dip.
897c478bd9Sstevel@tonic-gate  */
907c478bd9Sstevel@tonic-gate struct ra_dip_type  {
917c478bd9Sstevel@tonic-gate 	struct ra_dip_type *ra_next;
927c478bd9Sstevel@tonic-gate 	struct ra_resource  *ra_rangeset;
937c478bd9Sstevel@tonic-gate 	dev_info_t *ra_dip;
947c478bd9Sstevel@tonic-gate };
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate /*
987c478bd9Sstevel@tonic-gate  * link list element for list of types resources. Each element
997c478bd9Sstevel@tonic-gate  * has all resources for a particular type.
1007c478bd9Sstevel@tonic-gate  */
1017c478bd9Sstevel@tonic-gate struct ra_type_map {
1027c478bd9Sstevel@tonic-gate 	struct ra_type_map *ra_next;
1037c478bd9Sstevel@tonic-gate 	struct ra_dip_type *ra_dip_list;
1047c478bd9Sstevel@tonic-gate 	char *type;
1057c478bd9Sstevel@tonic-gate };
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate /*
1097c478bd9Sstevel@tonic-gate  * place holder to keep the head of the whole global list.
1107c478bd9Sstevel@tonic-gate  * the address of the first typemap would be stored in it.
1117c478bd9Sstevel@tonic-gate  */
1127c478bd9Sstevel@tonic-gate static struct ra_type_map	*ra_map_list_head = NULL;
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate /*
1167c478bd9Sstevel@tonic-gate  * This is the loadable module wrapper.
1177c478bd9Sstevel@tonic-gate  * It is essentially boilerplate so isn't documented
1187c478bd9Sstevel@tonic-gate  */
1197c478bd9Sstevel@tonic-gate extern struct mod_ops mod_miscops;
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate #ifdef BUSRA_DEBUG
1227c478bd9Sstevel@tonic-gate void ra_dump_all();
1237c478bd9Sstevel@tonic-gate #endif
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate /* internal function prototypes */
1267c478bd9Sstevel@tonic-gate static struct ra_dip_type *find_dip_map_resources(dev_info_t *dip, char *type,
1277c478bd9Sstevel@tonic-gate     struct ra_dip_type ***backdip, struct ra_type_map ***backtype,
1287c478bd9Sstevel@tonic-gate     uint32_t flag);
1297c478bd9Sstevel@tonic-gate static int isnot_pow2(uint64_t value);
1307c478bd9Sstevel@tonic-gate static int claim_pci_busnum(dev_info_t *dip, void *arg);
1317c478bd9Sstevel@tonic-gate static int ra_map_exist(dev_info_t *dip, char *type);
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate #define	RA_INSERT(prev, el) \
1357c478bd9Sstevel@tonic-gate 	el->ra_next = *prev; \
1367c478bd9Sstevel@tonic-gate 	*prev = el;
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate #define	RA_REMOVE(prev, el) \
1397c478bd9Sstevel@tonic-gate 	*prev = el->ra_next;
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate static struct modlmisc modlmisc = {
1437c478bd9Sstevel@tonic-gate 	&mod_miscops,		/* Type of module. This one is a module */
144*70025d76Sjohnny 	"Bus Resource Allocator (BUSRA) 1.36",	/* Name of the module. */
1457c478bd9Sstevel@tonic-gate };
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
1487c478bd9Sstevel@tonic-gate 	MODREV_1, (void *)&modlmisc, NULL
1497c478bd9Sstevel@tonic-gate };
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate int
1527c478bd9Sstevel@tonic-gate _init()
1537c478bd9Sstevel@tonic-gate {
1547c478bd9Sstevel@tonic-gate 	int	ret;
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 	mutex_init(&ra_lock, NULL, MUTEX_DRIVER,
1577c478bd9Sstevel@tonic-gate 		(void *)(intptr_t)__ipltospl(SPL7 - 1));
1587c478bd9Sstevel@tonic-gate 	if ((ret = mod_install(&modlinkage)) != 0) {
1597c478bd9Sstevel@tonic-gate 		mutex_destroy(&ra_lock);
1607c478bd9Sstevel@tonic-gate 	}
1617c478bd9Sstevel@tonic-gate 	return (ret);
1627c478bd9Sstevel@tonic-gate }
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate int
1657c478bd9Sstevel@tonic-gate _fini()
1667c478bd9Sstevel@tonic-gate {
1677c478bd9Sstevel@tonic-gate 	int	ret;
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	mutex_enter(&ra_lock);
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 	if (ra_map_list_head != NULL) {
1727c478bd9Sstevel@tonic-gate 		mutex_exit(&ra_lock);
1737c478bd9Sstevel@tonic-gate 		return (EBUSY);
1747c478bd9Sstevel@tonic-gate 	}
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	ret = mod_remove(&modlinkage);
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 	mutex_exit(&ra_lock);
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 	if (ret == 0)
1817c478bd9Sstevel@tonic-gate 		mutex_destroy(&ra_lock);
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	return (ret);
1847c478bd9Sstevel@tonic-gate }
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate int
1877c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate {
1907c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
1917c478bd9Sstevel@tonic-gate }
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate /*
1947c478bd9Sstevel@tonic-gate  * set up an empty resource map for a given type and dip
1957c478bd9Sstevel@tonic-gate  */
1967c478bd9Sstevel@tonic-gate int
1977c478bd9Sstevel@tonic-gate ndi_ra_map_setup(dev_info_t *dip, char *type)
1987c478bd9Sstevel@tonic-gate {
1997c478bd9Sstevel@tonic-gate 	struct ra_type_map  *typemapp;
2007c478bd9Sstevel@tonic-gate 	struct ra_dip_type  *dipmap;
2017c478bd9Sstevel@tonic-gate 	struct ra_dip_type  **backdip;
2027c478bd9Sstevel@tonic-gate 	struct ra_type_map  **backtype;
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 	mutex_enter(&ra_lock);
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 	dipmap = find_dip_map_resources(dip, type, &backdip, &backtype, 0);
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 	if (dipmap == NULL) {
2107c478bd9Sstevel@tonic-gate 		if (backtype == NULL) {
2117c478bd9Sstevel@tonic-gate 			typemapp = (struct ra_type_map *)
2127c478bd9Sstevel@tonic-gate 			kmem_zalloc(sizeof (*typemapp), KM_SLEEP);
2137c478bd9Sstevel@tonic-gate 			typemapp->type = (char *)kmem_zalloc(strlen(type) + 1,
2147c478bd9Sstevel@tonic-gate 				KM_SLEEP);
2157c478bd9Sstevel@tonic-gate 			(void) strcpy(typemapp->type, type);
2167c478bd9Sstevel@tonic-gate 			RA_INSERT(&ra_map_list_head, typemapp);
2177c478bd9Sstevel@tonic-gate 		} else {
2187c478bd9Sstevel@tonic-gate 			typemapp = *backtype;
2197c478bd9Sstevel@tonic-gate 		}
2207c478bd9Sstevel@tonic-gate 		if (backdip == NULL) {
2217c478bd9Sstevel@tonic-gate 			/* allocate and insert in list of dips for this type */
2227c478bd9Sstevel@tonic-gate 			dipmap = (struct ra_dip_type *)
2237c478bd9Sstevel@tonic-gate 			kmem_zalloc(sizeof (*dipmap), KM_SLEEP);
2247c478bd9Sstevel@tonic-gate 			dipmap->ra_dip = dip;
2257c478bd9Sstevel@tonic-gate 			RA_INSERT(&typemapp->ra_dip_list, dipmap);
2267c478bd9Sstevel@tonic-gate 		}
2277c478bd9Sstevel@tonic-gate 	}
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 	mutex_exit(&ra_lock);
2307c478bd9Sstevel@tonic-gate 	return (NDI_SUCCESS);
2317c478bd9Sstevel@tonic-gate }
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate /*
2347c478bd9Sstevel@tonic-gate  * destroys a resource map for a given dip and type
2357c478bd9Sstevel@tonic-gate  */
2367c478bd9Sstevel@tonic-gate int
2377c478bd9Sstevel@tonic-gate ndi_ra_map_destroy(dev_info_t *dip, char *type)
2387c478bd9Sstevel@tonic-gate {
2397c478bd9Sstevel@tonic-gate 	struct ra_dip_type	*dipmap;
2407c478bd9Sstevel@tonic-gate 	struct ra_dip_type	**backdip;
2417c478bd9Sstevel@tonic-gate 	struct ra_type_map  	**backtype, *typemap;
2427c478bd9Sstevel@tonic-gate 	struct ra_resource	*range;
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 	mutex_enter(&ra_lock);
2457c478bd9Sstevel@tonic-gate 	dipmap = find_dip_map_resources(dip, type, &backdip, &backtype, 0);
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 	if (dipmap == NULL) {
2487c478bd9Sstevel@tonic-gate 		mutex_exit(&ra_lock);
2497c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
2507c478bd9Sstevel@tonic-gate 	}
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 	/*
2537c478bd9Sstevel@tonic-gate 	 * destroy all resources for this dip
2547c478bd9Sstevel@tonic-gate 	 * remove dip from type list
2557c478bd9Sstevel@tonic-gate 	 */
2567c478bd9Sstevel@tonic-gate 	ASSERT((backdip != NULL) && (backtype != NULL));
2577c478bd9Sstevel@tonic-gate 	while (dipmap->ra_rangeset != NULL) {
2587c478bd9Sstevel@tonic-gate 		range = dipmap->ra_rangeset;
2597c478bd9Sstevel@tonic-gate 		RA_REMOVE(&dipmap->ra_rangeset, range);
2607c478bd9Sstevel@tonic-gate 		kmem_free((caddr_t)range, sizeof (*range));
2617c478bd9Sstevel@tonic-gate 	}
2627c478bd9Sstevel@tonic-gate 	/* remove from dip list */
2637c478bd9Sstevel@tonic-gate 	RA_REMOVE(backdip, dipmap);
2647c478bd9Sstevel@tonic-gate 	kmem_free((caddr_t)dipmap, sizeof (*dipmap));
2657c478bd9Sstevel@tonic-gate 	if ((*backtype)->ra_dip_list == NULL) {
2667c478bd9Sstevel@tonic-gate 		/*
2677c478bd9Sstevel@tonic-gate 		 * This was the last dip with this resource type.
2687c478bd9Sstevel@tonic-gate 		 * Remove the type from the global list.
2697c478bd9Sstevel@tonic-gate 		 */
2707c478bd9Sstevel@tonic-gate 		typemap = *backtype;
2717c478bd9Sstevel@tonic-gate 		RA_REMOVE(backtype, (*backtype));
2727c478bd9Sstevel@tonic-gate 		kmem_free((caddr_t)typemap->type, strlen(typemap->type) + 1);
2737c478bd9Sstevel@tonic-gate 		kmem_free((caddr_t)typemap, sizeof (*typemap));
2747c478bd9Sstevel@tonic-gate 	}
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 	mutex_exit(&ra_lock);
2777c478bd9Sstevel@tonic-gate 	return (NDI_SUCCESS);
2787c478bd9Sstevel@tonic-gate }
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate static int
2817c478bd9Sstevel@tonic-gate ra_map_exist(dev_info_t *dip, char *type)
2827c478bd9Sstevel@tonic-gate {
2837c478bd9Sstevel@tonic-gate 	struct ra_dip_type  **backdip;
2847c478bd9Sstevel@tonic-gate 	struct ra_type_map  **backtype;
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate 	mutex_enter(&ra_lock);
2877c478bd9Sstevel@tonic-gate 	if (find_dip_map_resources(dip, type, &backdip, &backtype, 0) == NULL) {
2887c478bd9Sstevel@tonic-gate 		mutex_exit(&ra_lock);
2897c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
2907c478bd9Sstevel@tonic-gate 	}
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 	mutex_exit(&ra_lock);
2937c478bd9Sstevel@tonic-gate 	return (NDI_SUCCESS);
2947c478bd9Sstevel@tonic-gate }
2957c478bd9Sstevel@tonic-gate /*
2967c478bd9Sstevel@tonic-gate  * Find a dip map for the specified type, if NDI_RA_PASS will go up on dev tree
2977c478bd9Sstevel@tonic-gate  * if found, backdip and backtype will be updated to point to the previous
2987c478bd9Sstevel@tonic-gate  * dip in the list and previous type for this dip in the list.
2997c478bd9Sstevel@tonic-gate  * If no such type at all in the resource list both backdip and backtype
3007c478bd9Sstevel@tonic-gate  * will be null. If the type found but no dip, back dip will be null.
3017c478bd9Sstevel@tonic-gate  */
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate static struct ra_dip_type *
3047c478bd9Sstevel@tonic-gate find_dip_map_resources(dev_info_t *dip, char *type,
3057c478bd9Sstevel@tonic-gate     struct ra_dip_type ***backdip, struct ra_type_map ***backtype,
3067c478bd9Sstevel@tonic-gate     uint32_t flag)
3077c478bd9Sstevel@tonic-gate {
3087c478bd9Sstevel@tonic-gate 	struct ra_type_map **prevmap;
3097c478bd9Sstevel@tonic-gate 	struct ra_dip_type *dipmap, **prevdip;
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&ra_lock));
3127c478bd9Sstevel@tonic-gate 	prevdip = NULL;
3137c478bd9Sstevel@tonic-gate 	dipmap = NULL;
3147c478bd9Sstevel@tonic-gate 	prevmap = &ra_map_list_head;
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate 	while (*prevmap) {
3177c478bd9Sstevel@tonic-gate 		if (strcmp((*prevmap)->type, type) == 0)
3187c478bd9Sstevel@tonic-gate 			break;
3197c478bd9Sstevel@tonic-gate 		prevmap = &(*prevmap)->ra_next;
3207c478bd9Sstevel@tonic-gate 	}
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 	if (*prevmap) {
3237c478bd9Sstevel@tonic-gate 		for (; dip != NULL; dip = ddi_get_parent(dip)) {
3247c478bd9Sstevel@tonic-gate 			prevdip = &(*prevmap)->ra_dip_list;
3257c478bd9Sstevel@tonic-gate 			dipmap = *prevdip;
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate 			while (dipmap) {
3287c478bd9Sstevel@tonic-gate 				if (dipmap->ra_dip == dip)
3297c478bd9Sstevel@tonic-gate 					break;
3307c478bd9Sstevel@tonic-gate 				prevdip =  &dipmap->ra_next;
3317c478bd9Sstevel@tonic-gate 				dipmap = dipmap->ra_next;
3327c478bd9Sstevel@tonic-gate 			}
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 			if (dipmap != NULL) {
3357c478bd9Sstevel@tonic-gate 				/* found it */
3367c478bd9Sstevel@tonic-gate 				break;
3377c478bd9Sstevel@tonic-gate 			}
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 			if (!(flag & NDI_RA_PASS)) {
3407c478bd9Sstevel@tonic-gate 				break;
3417c478bd9Sstevel@tonic-gate 			}
3427c478bd9Sstevel@tonic-gate 		}
3437c478bd9Sstevel@tonic-gate 	}
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate 	*backtype = (*prevmap == NULL) ?  NULL: prevmap;
3467c478bd9Sstevel@tonic-gate 	*backdip = (dipmap == NULL) ?  NULL: prevdip;
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate 	return (dipmap);
3497c478bd9Sstevel@tonic-gate }
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate int
3527c478bd9Sstevel@tonic-gate ndi_ra_free(dev_info_t *dip, uint64_t base, uint64_t len, char *type,
3537c478bd9Sstevel@tonic-gate     uint32_t flag)
3547c478bd9Sstevel@tonic-gate {
3557c478bd9Sstevel@tonic-gate 	struct ra_dip_type *dipmap;
3567c478bd9Sstevel@tonic-gate 	struct ra_resource *newmap, *overlapmap, *oldmap = NULL;
3577c478bd9Sstevel@tonic-gate 	struct ra_resource  *mapp, **backp;
3587c478bd9Sstevel@tonic-gate 	uint64_t newend, mapend;
3597c478bd9Sstevel@tonic-gate 	struct ra_dip_type **backdip;
3607c478bd9Sstevel@tonic-gate 	struct ra_type_map **backtype;
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 	if (len == 0) {
3637c478bd9Sstevel@tonic-gate 		return (NDI_SUCCESS);
3647c478bd9Sstevel@tonic-gate 	}
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate 	mutex_enter(&ra_lock);
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 	if ((dipmap = find_dip_map_resources(dip, type, &backdip, &backtype,
3697c478bd9Sstevel@tonic-gate 	    flag)) == NULL) {
3707c478bd9Sstevel@tonic-gate 		mutex_exit(&ra_lock);
3717c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
3727c478bd9Sstevel@tonic-gate 	}
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 	mapp = dipmap->ra_rangeset;
3757c478bd9Sstevel@tonic-gate 	backp = &dipmap->ra_rangeset;
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate 	/* now find where range lies and fix things up */
3787c478bd9Sstevel@tonic-gate 	newend = base + len;
3797c478bd9Sstevel@tonic-gate 	for (; mapp != NULL; backp = &(mapp->ra_next), mapp = mapp->ra_next) {
3807c478bd9Sstevel@tonic-gate 		mapend = mapp->ra_base + mapp->ra_len;
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 		/* check for overlap first */
3837c478bd9Sstevel@tonic-gate 		if ((base <= mapp->ra_base && newend > mapp->ra_base) ||
3847c478bd9Sstevel@tonic-gate 		    (base > mapp->ra_base && base < mapend)) {
3857c478bd9Sstevel@tonic-gate 			/* overlap with mapp */
3867c478bd9Sstevel@tonic-gate 			overlapmap = mapp;
3877c478bd9Sstevel@tonic-gate 			goto overlap;
3887c478bd9Sstevel@tonic-gate 		} else if ((base == mapend && mapp->ra_next) &&
3897c478bd9Sstevel@tonic-gate 		    (newend > mapp->ra_next->ra_base)) {
3907c478bd9Sstevel@tonic-gate 			/* overlap with mapp->ra_next */
3917c478bd9Sstevel@tonic-gate 			overlapmap = mapp->ra_next;
3927c478bd9Sstevel@tonic-gate 			goto overlap;
3937c478bd9Sstevel@tonic-gate 		}
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate 		if (newend == mapp->ra_base) {
3967c478bd9Sstevel@tonic-gate 			/* simple - on front */
3977c478bd9Sstevel@tonic-gate 			mapp->ra_base = base;
3987c478bd9Sstevel@tonic-gate 			mapp->ra_len += len;
3997c478bd9Sstevel@tonic-gate 			/*
4007c478bd9Sstevel@tonic-gate 			 * don't need to check if it merges with
4017c478bd9Sstevel@tonic-gate 			 * previous since that would match on on end
4027c478bd9Sstevel@tonic-gate 			 */
4037c478bd9Sstevel@tonic-gate 			break;
4047c478bd9Sstevel@tonic-gate 		} else if (base == mapend) {
4057c478bd9Sstevel@tonic-gate 			/* simple - on end */
4067c478bd9Sstevel@tonic-gate 			mapp->ra_len += len;
4077c478bd9Sstevel@tonic-gate 			if (mapp->ra_next &&
4087c478bd9Sstevel@tonic-gate 			    (newend == mapp->ra_next->ra_base)) {
4097c478bd9Sstevel@tonic-gate 				/* merge with next node */
4107c478bd9Sstevel@tonic-gate 				oldmap = mapp->ra_next;
4117c478bd9Sstevel@tonic-gate 				mapp->ra_len += oldmap->ra_len;
4127c478bd9Sstevel@tonic-gate 				RA_REMOVE(&mapp->ra_next, oldmap);
4137c478bd9Sstevel@tonic-gate 				kmem_free((caddr_t)oldmap, sizeof (*oldmap));
4147c478bd9Sstevel@tonic-gate 			}
4157c478bd9Sstevel@tonic-gate 			break;
4167c478bd9Sstevel@tonic-gate 		} else if (base < mapp->ra_base) {
4177c478bd9Sstevel@tonic-gate 			/* somewhere in between so just an insert */
4187c478bd9Sstevel@tonic-gate 			newmap = (struct ra_resource *)
4197c478bd9Sstevel@tonic-gate 				kmem_zalloc(sizeof (*newmap), KM_SLEEP);
4207c478bd9Sstevel@tonic-gate 			newmap->ra_base = base;
4217c478bd9Sstevel@tonic-gate 			newmap->ra_len = len;
4227c478bd9Sstevel@tonic-gate 			RA_INSERT(backp, newmap);
4237c478bd9Sstevel@tonic-gate 			break;
4247c478bd9Sstevel@tonic-gate 		}
4257c478bd9Sstevel@tonic-gate 	}
4267c478bd9Sstevel@tonic-gate 	if (mapp == NULL) {
4277c478bd9Sstevel@tonic-gate 		/* stick on end */
4287c478bd9Sstevel@tonic-gate 		newmap = (struct ra_resource *)
4297c478bd9Sstevel@tonic-gate 				kmem_zalloc(sizeof (*newmap), KM_SLEEP);
4307c478bd9Sstevel@tonic-gate 		newmap->ra_base = base;
4317c478bd9Sstevel@tonic-gate 		newmap->ra_len = len;
4327c478bd9Sstevel@tonic-gate 		RA_INSERT(backp, newmap);
4337c478bd9Sstevel@tonic-gate 	}
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate 	mutex_exit(&ra_lock);
4367c478bd9Sstevel@tonic-gate 	return (NDI_SUCCESS);
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate overlap:
4397c478bd9Sstevel@tonic-gate 	/*
4407c478bd9Sstevel@tonic-gate 	 * Bad free may happen on some x86 platforms with BIOS exporting
4417c478bd9Sstevel@tonic-gate 	 * incorrect resource maps. The system is otherwise functioning
4427c478bd9Sstevel@tonic-gate 	 * normally. We send such messages to syslog only.
4437c478bd9Sstevel@tonic-gate 	 */
4447c478bd9Sstevel@tonic-gate 	cmn_err(CE_NOTE, "!ndi_ra_free: bad free, dip %p, resource type %s \n",
4457c478bd9Sstevel@tonic-gate 	    (void *)dip, type);
4467c478bd9Sstevel@tonic-gate 	cmn_err(CE_NOTE, "!ndi_ra_free: freeing base 0x%" PRIx64 ", len 0x%"
4477c478bd9Sstevel@tonic-gate 	    PRIX64 " overlaps with existing resource base 0x%" PRIx64
4487c478bd9Sstevel@tonic-gate 	    ", len 0x%" PRIx64 "\n", base, len, overlapmap->ra_base,
4497c478bd9Sstevel@tonic-gate 	    overlapmap->ra_len);
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate 	mutex_exit(&ra_lock);
4527c478bd9Sstevel@tonic-gate 	return (NDI_FAILURE);
4537c478bd9Sstevel@tonic-gate }
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate /* check to see if value is power of 2 or not. */
4567c478bd9Sstevel@tonic-gate static int
4577c478bd9Sstevel@tonic-gate isnot_pow2(uint64_t value)
4587c478bd9Sstevel@tonic-gate {
4597c478bd9Sstevel@tonic-gate 	uint32_t low;
4607c478bd9Sstevel@tonic-gate 	uint32_t hi;
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate 	low = value & 0xffffffff;
4637c478bd9Sstevel@tonic-gate 	hi = value >> 32;
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate 	/*
4667c478bd9Sstevel@tonic-gate 	 * ddi_ffs and ddi_fls gets long values, so in 32bit environment
4677c478bd9Sstevel@tonic-gate 	 * won't work correctly for 64bit values
4687c478bd9Sstevel@tonic-gate 	 */
4697c478bd9Sstevel@tonic-gate 	if ((ddi_ffs(low) == ddi_fls(low)) &&
4707c478bd9Sstevel@tonic-gate 	    (ddi_ffs(hi) == ddi_fls(hi)))
4717c478bd9Sstevel@tonic-gate 		return (0);
4727c478bd9Sstevel@tonic-gate 	return (1);
4737c478bd9Sstevel@tonic-gate }
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate static  void
4767c478bd9Sstevel@tonic-gate adjust_link(struct ra_resource **backp, struct ra_resource *mapp,
4777c478bd9Sstevel@tonic-gate 	    uint64_t base, uint64_t len)
4787c478bd9Sstevel@tonic-gate {
4797c478bd9Sstevel@tonic-gate 	struct ra_resource *newmap;
4807c478bd9Sstevel@tonic-gate 	uint64_t newlen;
4817c478bd9Sstevel@tonic-gate 
4827c478bd9Sstevel@tonic-gate 	if (base != mapp->ra_base) {
4837c478bd9Sstevel@tonic-gate 		/* in the middle or end */
4847c478bd9Sstevel@tonic-gate 		newlen = base - mapp->ra_base;
4857c478bd9Sstevel@tonic-gate 		if ((mapp->ra_len - newlen) == len) {
4867c478bd9Sstevel@tonic-gate 			/* on the end */
4877c478bd9Sstevel@tonic-gate 			mapp->ra_len = newlen;
4887c478bd9Sstevel@tonic-gate 		} else {
4897c478bd9Sstevel@tonic-gate 			/* in the middle */
4907c478bd9Sstevel@tonic-gate 			newmap = (struct ra_resource *)
4917c478bd9Sstevel@tonic-gate 					kmem_zalloc(sizeof (*newmap), KM_SLEEP);
4927c478bd9Sstevel@tonic-gate 			newmap->ra_base = base + len;
4937c478bd9Sstevel@tonic-gate 			newmap->ra_len = mapp->ra_len -
4947c478bd9Sstevel@tonic-gate 				(len + newlen);
4957c478bd9Sstevel@tonic-gate 			mapp->ra_len = newlen;
4967c478bd9Sstevel@tonic-gate 			RA_INSERT(&(mapp->ra_next), newmap);
4977c478bd9Sstevel@tonic-gate 		}
4987c478bd9Sstevel@tonic-gate 	} else {
4997c478bd9Sstevel@tonic-gate 		/* at the beginning */
5007c478bd9Sstevel@tonic-gate 		mapp->ra_base += len;
5017c478bd9Sstevel@tonic-gate 		mapp->ra_len -= len;
5027c478bd9Sstevel@tonic-gate 		if (mapp->ra_len == 0) {
5037c478bd9Sstevel@tonic-gate 			/* remove the whole node */
5047c478bd9Sstevel@tonic-gate 			RA_REMOVE(backp, mapp);
5057c478bd9Sstevel@tonic-gate 			kmem_free((caddr_t)mapp, sizeof (*mapp));
5067c478bd9Sstevel@tonic-gate 		}
5077c478bd9Sstevel@tonic-gate 	}
5087c478bd9Sstevel@tonic-gate }
5097c478bd9Sstevel@tonic-gate 
5107c478bd9Sstevel@tonic-gate int
5117c478bd9Sstevel@tonic-gate ndi_ra_alloc(dev_info_t *dip, ndi_ra_request_t *req, uint64_t *retbasep,
5127c478bd9Sstevel@tonic-gate     uint64_t *retlenp, char *type, uint32_t flag)
5137c478bd9Sstevel@tonic-gate {
5147c478bd9Sstevel@tonic-gate 	struct ra_dip_type *dipmap;
5157c478bd9Sstevel@tonic-gate 	struct ra_resource *mapp, **backp, **backlargestp;
5167c478bd9Sstevel@tonic-gate 	uint64_t mask = 0;
5177c478bd9Sstevel@tonic-gate 	uint64_t len, remlen, largestbase, largestlen;
5187c478bd9Sstevel@tonic-gate 	uint64_t base, oldbase, lower, upper;
5197c478bd9Sstevel@tonic-gate 	struct ra_dip_type  **backdip;
5207c478bd9Sstevel@tonic-gate 	struct ra_type_map  **backtype;
5217c478bd9Sstevel@tonic-gate 	int  rval = NDI_FAILURE;
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate 	len = req->ra_len;
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate 	if (req->ra_flags & NDI_RA_ALIGN_SIZE) {
5277c478bd9Sstevel@tonic-gate 		if (isnot_pow2(req->ra_len)) {
5287c478bd9Sstevel@tonic-gate 			DEBUGPRT(CE_WARN, "ndi_ra_alloc: bad length(pow2) 0x%"
5297c478bd9Sstevel@tonic-gate 				PRIx64, req->ra_len);
5307c478bd9Sstevel@tonic-gate 			*retbasep = 0;
5317c478bd9Sstevel@tonic-gate 			*retlenp = 0;
5327c478bd9Sstevel@tonic-gate 			return (NDI_FAILURE);
5337c478bd9Sstevel@tonic-gate 		}
5347c478bd9Sstevel@tonic-gate 	}
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate 	mask = (req->ra_flags & NDI_RA_ALIGN_SIZE) ? (len - 1) :
5377c478bd9Sstevel@tonic-gate 	    req->ra_align_mask;
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate 
5407c478bd9Sstevel@tonic-gate 	mutex_enter(&ra_lock);
5417c478bd9Sstevel@tonic-gate 	dipmap = find_dip_map_resources(dip, type, &backdip, &backtype, flag);
5427c478bd9Sstevel@tonic-gate 	if ((dipmap == NULL) || ((mapp = dipmap->ra_rangeset) == NULL)) {
5437c478bd9Sstevel@tonic-gate 		mutex_exit(&ra_lock);
5447c478bd9Sstevel@tonic-gate 		DEBUGPRT(CE_CONT, "ndi_ra_alloc no map found for this type\n");
5457c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
5467c478bd9Sstevel@tonic-gate 	}
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate 	DEBUGPRT(CE_CONT, "ndi_ra_alloc: mapp = %p len=%" PRIx64 ", mask=%"
5497c478bd9Sstevel@tonic-gate 			PRIx64 "\n", (void *)mapp, len, mask);
5507c478bd9Sstevel@tonic-gate 
5517c478bd9Sstevel@tonic-gate 	backp = &(dipmap->ra_rangeset);
5527c478bd9Sstevel@tonic-gate 	backlargestp = NULL;
5537c478bd9Sstevel@tonic-gate 	largestbase = 0;
5547c478bd9Sstevel@tonic-gate 	largestlen = 0;
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate 	lower = 0;
5577c478bd9Sstevel@tonic-gate 	upper = ~(uint64_t)0;
5587c478bd9Sstevel@tonic-gate 
5597c478bd9Sstevel@tonic-gate 	if (req->ra_flags & NDI_RA_ALLOC_BOUNDED) {
5607c478bd9Sstevel@tonic-gate 		/* bounded so skip to first possible */
5617c478bd9Sstevel@tonic-gate 		lower = req->ra_boundbase;
5627c478bd9Sstevel@tonic-gate 		upper = req->ra_boundlen + lower;
5637c478bd9Sstevel@tonic-gate 		if ((upper == 0) || (upper < req->ra_boundlen))
5647c478bd9Sstevel@tonic-gate 			upper = ~(uint64_t)0;
5657c478bd9Sstevel@tonic-gate 		DEBUGPRT(CE_CONT, "ndi_ra_alloc: ra_len = %" PRIx64 ", len = %"
5667c478bd9Sstevel@tonic-gate 				PRIx64 " ra_base=%" PRIx64 ", mask=%" PRIx64
5677c478bd9Sstevel@tonic-gate 				"\n", mapp->ra_len, len, mapp->ra_base, mask);
5687c478bd9Sstevel@tonic-gate 		for (; mapp != NULL &&
5697c478bd9Sstevel@tonic-gate 			(mapp->ra_base + mapp->ra_len) < lower;
5707c478bd9Sstevel@tonic-gate 			backp = &(mapp->ra_next), mapp = mapp->ra_next) {
5717c478bd9Sstevel@tonic-gate 			if (((mapp->ra_len + mapp->ra_base) == 0) ||
5727c478bd9Sstevel@tonic-gate 			    ((mapp->ra_len + mapp->ra_base) < mapp->ra_len))
5737c478bd9Sstevel@tonic-gate 				/*
5747c478bd9Sstevel@tonic-gate 				 * This elements end goes beyond max uint64_t.
5757c478bd9Sstevel@tonic-gate 				 * potential candidate, check end against lower
5767c478bd9Sstevel@tonic-gate 				 * would not be precise.
5777c478bd9Sstevel@tonic-gate 				 */
5787c478bd9Sstevel@tonic-gate 				break;
5797c478bd9Sstevel@tonic-gate 
5807c478bd9Sstevel@tonic-gate 			DEBUGPRT(CE_CONT, " ra_len = %" PRIx64 ", ra_base=%"
5817c478bd9Sstevel@tonic-gate 			    PRIx64 "\n", mapp->ra_len, mapp->ra_base);
5827c478bd9Sstevel@tonic-gate 			}
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate 	}
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate 	if (!(req->ra_flags & NDI_RA_ALLOC_SPECIFIED)) {
5877c478bd9Sstevel@tonic-gate 		/* first fit - not user specified */
5887c478bd9Sstevel@tonic-gate 		DEBUGPRT(CE_CONT, "ndi_ra_alloc(unspecified request)"
5897c478bd9Sstevel@tonic-gate 			"lower=%" PRIx64 ", upper=%" PRIx64 "\n", lower, upper);
5907c478bd9Sstevel@tonic-gate 		for (; mapp != NULL && mapp->ra_base <= upper;
5917c478bd9Sstevel@tonic-gate 			backp = &(mapp->ra_next), mapp = mapp->ra_next) {
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate 			DEBUGPRT(CE_CONT, "ndi_ra_alloc: ra_len = %" PRIx64
5947c478bd9Sstevel@tonic-gate 			    ", len = %" PRIx64 "", mapp->ra_len, len);
5957c478bd9Sstevel@tonic-gate 			base = mapp->ra_base;
5967c478bd9Sstevel@tonic-gate 			if (base < lower) {
5977c478bd9Sstevel@tonic-gate 				base = lower;
5987c478bd9Sstevel@tonic-gate 				DEBUGPRT(CE_CONT, "\tbase=%" PRIx64
5997c478bd9Sstevel@tonic-gate 				    ", ra_base=%" PRIx64 ", mask=%" PRIx64,
6007c478bd9Sstevel@tonic-gate 				    base, mapp->ra_base, mask);
6017c478bd9Sstevel@tonic-gate 			}
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate 			if ((base & mask) != 0) {
6047c478bd9Sstevel@tonic-gate 				oldbase = base;
6057c478bd9Sstevel@tonic-gate 				/*
6067c478bd9Sstevel@tonic-gate 				 * failed a critical constraint
6077c478bd9Sstevel@tonic-gate 				 * adjust and see if it still fits
6087c478bd9Sstevel@tonic-gate 				 */
6097c478bd9Sstevel@tonic-gate 				base = base & ~mask;
6107c478bd9Sstevel@tonic-gate 				base += (mask + 1);
6117c478bd9Sstevel@tonic-gate 				DEBUGPRT(CE_CONT, "\tnew base=%" PRIx64 "\n",
6127c478bd9Sstevel@tonic-gate 					base);
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate 				/*
6157c478bd9Sstevel@tonic-gate 				 * Check to see if the new base is past
6167c478bd9Sstevel@tonic-gate 				 * the end of the resource.
6177c478bd9Sstevel@tonic-gate 				 */
6187c478bd9Sstevel@tonic-gate 				if (base >= (oldbase + mapp->ra_len + 1)) {
6197c478bd9Sstevel@tonic-gate 					continue;
6207c478bd9Sstevel@tonic-gate 				}
6217c478bd9Sstevel@tonic-gate 			}
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate 			if (req->ra_flags & NDI_RA_ALLOC_PARTIAL_OK) {
6247c478bd9Sstevel@tonic-gate 				if ((upper - mapp->ra_base)  <  mapp->ra_len)
6257c478bd9Sstevel@tonic-gate 					remlen = upper - base;
6267c478bd9Sstevel@tonic-gate 				else
6277c478bd9Sstevel@tonic-gate 					remlen = mapp->ra_len -
6287c478bd9Sstevel@tonic-gate 						(base - mapp->ra_base);
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate 				if ((backlargestp == NULL) ||
6317c478bd9Sstevel@tonic-gate 				    (largestlen < remlen)) {
6327c478bd9Sstevel@tonic-gate 
6337c478bd9Sstevel@tonic-gate 					backlargestp = backp;
6347c478bd9Sstevel@tonic-gate 					largestbase = base;
6357c478bd9Sstevel@tonic-gate 					largestlen = remlen;
6367c478bd9Sstevel@tonic-gate 				}
6377c478bd9Sstevel@tonic-gate 			}
6387c478bd9Sstevel@tonic-gate 
6397c478bd9Sstevel@tonic-gate 			if (mapp->ra_len >= len) {
6407c478bd9Sstevel@tonic-gate 				/* a candidate -- apply constraints */
6417c478bd9Sstevel@tonic-gate 				if ((len > (mapp->ra_len -
6427c478bd9Sstevel@tonic-gate 				    (base - mapp->ra_base))) ||
6437c478bd9Sstevel@tonic-gate 				    ((len - 1 + base) > upper)) {
6447c478bd9Sstevel@tonic-gate 					continue;
6457c478bd9Sstevel@tonic-gate 				}
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate 				/* we have a fit */
6487c478bd9Sstevel@tonic-gate 
6497c478bd9Sstevel@tonic-gate 				DEBUGPRT(CE_CONT, "\thave a fit\n");
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate 				adjust_link(backp, mapp, base, len);
6527c478bd9Sstevel@tonic-gate 				rval = NDI_SUCCESS;
6537c478bd9Sstevel@tonic-gate 				break;
6547c478bd9Sstevel@tonic-gate 
6557c478bd9Sstevel@tonic-gate 			}
6567c478bd9Sstevel@tonic-gate 		}
6577c478bd9Sstevel@tonic-gate 	} else {
6587c478bd9Sstevel@tonic-gate 		/* want an exact value/fit */
6597c478bd9Sstevel@tonic-gate 		base = req->ra_addr;
6607c478bd9Sstevel@tonic-gate 		len = req->ra_len;
6617c478bd9Sstevel@tonic-gate 		for (; mapp != NULL && mapp->ra_base <= upper;
6627c478bd9Sstevel@tonic-gate 			backp = &(mapp->ra_next), mapp = mapp->ra_next) {
6637c478bd9Sstevel@tonic-gate 			if (base >= mapp->ra_base &&
6647c478bd9Sstevel@tonic-gate 			    ((base - mapp->ra_base) < mapp->ra_len)) {
6657c478bd9Sstevel@tonic-gate 				/*
6667c478bd9Sstevel@tonic-gate 				 * This is the node with he requested base in
6677c478bd9Sstevel@tonic-gate 				 * its range
6687c478bd9Sstevel@tonic-gate 				 */
6697c478bd9Sstevel@tonic-gate 				if ((len > mapp->ra_len) ||
6707c478bd9Sstevel@tonic-gate 				    (base - mapp->ra_base >
6717c478bd9Sstevel@tonic-gate 				    mapp->ra_len - len)) {
6727c478bd9Sstevel@tonic-gate 					/* length requirement not satisfied */
6737c478bd9Sstevel@tonic-gate 					if (req->ra_flags &
6747c478bd9Sstevel@tonic-gate 					    NDI_RA_ALLOC_PARTIAL_OK) {
6757c478bd9Sstevel@tonic-gate 						if ((upper - mapp->ra_base)
6767c478bd9Sstevel@tonic-gate 						    < mapp->ra_len)
6777c478bd9Sstevel@tonic-gate 							remlen = upper - base;
6787c478bd9Sstevel@tonic-gate 						else
6797c478bd9Sstevel@tonic-gate 							remlen =
6807c478bd9Sstevel@tonic-gate 							    mapp->ra_len -
6817c478bd9Sstevel@tonic-gate 							    (base -
6827c478bd9Sstevel@tonic-gate 							    mapp->ra_base);
6837c478bd9Sstevel@tonic-gate 					}
6847c478bd9Sstevel@tonic-gate 					backlargestp = backp;
6857c478bd9Sstevel@tonic-gate 					largestbase = base;
6867c478bd9Sstevel@tonic-gate 					largestlen = remlen;
6877c478bd9Sstevel@tonic-gate 					base = 0;
6887c478bd9Sstevel@tonic-gate 				} else {
6897c478bd9Sstevel@tonic-gate 					/* We have a match */
6907c478bd9Sstevel@tonic-gate 					adjust_link(backp, mapp, base, len);
6917c478bd9Sstevel@tonic-gate 					rval = NDI_SUCCESS;
6927c478bd9Sstevel@tonic-gate 				}
6937c478bd9Sstevel@tonic-gate 				break;
6947c478bd9Sstevel@tonic-gate 			}
6957c478bd9Sstevel@tonic-gate 		}
6967c478bd9Sstevel@tonic-gate 	}
6977c478bd9Sstevel@tonic-gate 
6987c478bd9Sstevel@tonic-gate 	if ((rval != NDI_SUCCESS) &&
6997c478bd9Sstevel@tonic-gate 	    (req->ra_flags & NDI_RA_ALLOC_PARTIAL_OK) &&
7007c478bd9Sstevel@tonic-gate 	    (backlargestp != NULL)) {
7017c478bd9Sstevel@tonic-gate 		adjust_link(backlargestp, *backlargestp, largestbase,
7027c478bd9Sstevel@tonic-gate 			largestlen);
7037c478bd9Sstevel@tonic-gate 
7047c478bd9Sstevel@tonic-gate 		base = largestbase;
7057c478bd9Sstevel@tonic-gate 		len = largestlen;
7067c478bd9Sstevel@tonic-gate 		rval = NDI_RA_PARTIAL_REQ;
7077c478bd9Sstevel@tonic-gate 	}
7087c478bd9Sstevel@tonic-gate 
7097c478bd9Sstevel@tonic-gate 	mutex_exit(&ra_lock);
7107c478bd9Sstevel@tonic-gate 
7117c478bd9Sstevel@tonic-gate 	if (rval == NDI_FAILURE) {
7127c478bd9Sstevel@tonic-gate 		*retbasep = 0;
7137c478bd9Sstevel@tonic-gate 		*retlenp = 0;
7147c478bd9Sstevel@tonic-gate 	} else {
7157c478bd9Sstevel@tonic-gate 		*retbasep = base;
7167c478bd9Sstevel@tonic-gate 		*retlenp = len;
7177c478bd9Sstevel@tonic-gate 	}
7187c478bd9Sstevel@tonic-gate 	return (rval);
7197c478bd9Sstevel@tonic-gate }
7207c478bd9Sstevel@tonic-gate 
7217c478bd9Sstevel@tonic-gate /*
7227c478bd9Sstevel@tonic-gate  * isa_resource_setup
7237c478bd9Sstevel@tonic-gate  *	check for /used-resources and initialize
7247c478bd9Sstevel@tonic-gate  *	based on info there.  If no /used-resources,
7257c478bd9Sstevel@tonic-gate  *	fail.
7267c478bd9Sstevel@tonic-gate  */
7277c478bd9Sstevel@tonic-gate int
7287c478bd9Sstevel@tonic-gate isa_resource_setup()
7297c478bd9Sstevel@tonic-gate {
7307c478bd9Sstevel@tonic-gate 	dev_info_t *used, *usedpdip;
7317c478bd9Sstevel@tonic-gate 	/*
7327c478bd9Sstevel@tonic-gate 	 * note that at this time bootconf creates 32 bit properties for
7337c478bd9Sstevel@tonic-gate 	 * io-space and device-memory
7347c478bd9Sstevel@tonic-gate 	 */
7357c478bd9Sstevel@tonic-gate 	struct iorange {
7367c478bd9Sstevel@tonic-gate 		uint32_t	base;
7377c478bd9Sstevel@tonic-gate 		uint32_t	len;
7387c478bd9Sstevel@tonic-gate 	} *iorange;
7397c478bd9Sstevel@tonic-gate 	struct memrange {
7407c478bd9Sstevel@tonic-gate 		uint32_t	base;
7417c478bd9Sstevel@tonic-gate 		uint32_t	len;
7427c478bd9Sstevel@tonic-gate 	} *memrange;
7437c478bd9Sstevel@tonic-gate 	uint32_t *irq;
7447c478bd9Sstevel@tonic-gate 	int proplen;
7457c478bd9Sstevel@tonic-gate 	int i, len;
7467c478bd9Sstevel@tonic-gate 	int maxrange;
7477c478bd9Sstevel@tonic-gate 	ndi_ra_request_t req;
7487c478bd9Sstevel@tonic-gate 	uint64_t retbase;
7497c478bd9Sstevel@tonic-gate 	uint64_t retlen;
7507c478bd9Sstevel@tonic-gate 
7517c478bd9Sstevel@tonic-gate 	used = ddi_find_devinfo("used-resources", -1, 0);
7527c478bd9Sstevel@tonic-gate 	if (used == NULL) {
7537c478bd9Sstevel@tonic-gate 		DEBUGPRT(CE_CONT,
7547c478bd9Sstevel@tonic-gate 			"isa_resource_setup: used-resources not found");
7557c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
7567c478bd9Sstevel@tonic-gate 	}
7577c478bd9Sstevel@tonic-gate 
7587c478bd9Sstevel@tonic-gate 	/*
7597c478bd9Sstevel@tonic-gate 	 * initialize to all resources being present
7607c478bd9Sstevel@tonic-gate 	 * and then remove the ones in use.
7617c478bd9Sstevel@tonic-gate 	 */
7627c478bd9Sstevel@tonic-gate 
7637c478bd9Sstevel@tonic-gate 	usedpdip = ddi_root_node();
7647c478bd9Sstevel@tonic-gate 
7657c478bd9Sstevel@tonic-gate 	DEBUGPRT(CE_CONT, "isa_resource_setup: used = %p usedpdip = %p\n",
7667c478bd9Sstevel@tonic-gate 	    (void *)used, (void *)usedpdip);
7677c478bd9Sstevel@tonic-gate 
7687c478bd9Sstevel@tonic-gate 	if (ndi_ra_map_setup(usedpdip, NDI_RA_TYPE_IO) == NDI_FAILURE) {
7697c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
7707c478bd9Sstevel@tonic-gate 	}
7717c478bd9Sstevel@tonic-gate 
7727c478bd9Sstevel@tonic-gate 	/* initialize io space, highest end base is 0xffff */
7737c478bd9Sstevel@tonic-gate 	/* note that length is highest addr + 1 since starts from 0 */
7747c478bd9Sstevel@tonic-gate 
7757c478bd9Sstevel@tonic-gate 	(void) ndi_ra_free(usedpdip, 0, 0xffff + 1,  NDI_RA_TYPE_IO, 0);
7767c478bd9Sstevel@tonic-gate 
777a3282898Scth 	if (ddi_getlongprop(DDI_DEV_T_ANY, used, DDI_PROP_DONTPASS,
7787c478bd9Sstevel@tonic-gate 	    "io-space", (caddr_t)&iorange, &proplen) == DDI_SUCCESS) {
7797c478bd9Sstevel@tonic-gate 		maxrange = proplen / sizeof (struct iorange);
7807c478bd9Sstevel@tonic-gate 		/* remove the "used" I/O resources */
7817c478bd9Sstevel@tonic-gate 		for (i = 0; i < maxrange; i++) {
7827c478bd9Sstevel@tonic-gate 			bzero((caddr_t)&req, sizeof (req));
7837c478bd9Sstevel@tonic-gate 			req.ra_addr =  (uint64_t)iorange[i].base;
7847c478bd9Sstevel@tonic-gate 			req.ra_len = (uint64_t)iorange[i].len;
7857c478bd9Sstevel@tonic-gate 			req.ra_flags = NDI_RA_ALLOC_SPECIFIED;
7867c478bd9Sstevel@tonic-gate 			(void) ndi_ra_alloc(usedpdip, &req, &retbase, &retlen,
7877c478bd9Sstevel@tonic-gate 			    NDI_RA_TYPE_IO, 0);
7887c478bd9Sstevel@tonic-gate 		}
7897c478bd9Sstevel@tonic-gate 
7907c478bd9Sstevel@tonic-gate 		kmem_free((caddr_t)iorange, proplen);
7917c478bd9Sstevel@tonic-gate 	}
7927c478bd9Sstevel@tonic-gate 
7937c478bd9Sstevel@tonic-gate 	if (ndi_ra_map_setup(usedpdip, NDI_RA_TYPE_MEM) == NDI_FAILURE) {
7947c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
7957c478bd9Sstevel@tonic-gate 	}
7967c478bd9Sstevel@tonic-gate 	/* initialize memory space where highest end base is 0xffffffff */
7977c478bd9Sstevel@tonic-gate 	/* note that length is highest addr + 1 since starts from 0 */
7987c478bd9Sstevel@tonic-gate 	(void) ndi_ra_free(usedpdip, 0, ((uint64_t)((uint32_t)~0)) + 1,
7997c478bd9Sstevel@tonic-gate 	    NDI_RA_TYPE_MEM, 0);
8007c478bd9Sstevel@tonic-gate 
801a3282898Scth 	if (ddi_getlongprop(DDI_DEV_T_ANY, used, DDI_PROP_DONTPASS,
8027c478bd9Sstevel@tonic-gate 	    "device-memory", (caddr_t)&memrange, &proplen) == DDI_SUCCESS) {
8037c478bd9Sstevel@tonic-gate 		maxrange = proplen / sizeof (struct memrange);
8047c478bd9Sstevel@tonic-gate 		/* remove the "used" memory resources */
8057c478bd9Sstevel@tonic-gate 		for (i = 0; i < maxrange; i++) {
8067c478bd9Sstevel@tonic-gate 			bzero((caddr_t)&req, sizeof (req));
8077c478bd9Sstevel@tonic-gate 			req.ra_addr = (uint64_t)memrange[i].base;
8087c478bd9Sstevel@tonic-gate 			req.ra_len = (uint64_t)memrange[i].len;
8097c478bd9Sstevel@tonic-gate 			req.ra_flags = NDI_RA_ALLOC_SPECIFIED;
8107c478bd9Sstevel@tonic-gate 			(void) ndi_ra_alloc(usedpdip, &req, &retbase, &retlen,
8117c478bd9Sstevel@tonic-gate 			    NDI_RA_TYPE_MEM, 0);
8127c478bd9Sstevel@tonic-gate 		}
8137c478bd9Sstevel@tonic-gate 
8147c478bd9Sstevel@tonic-gate 		kmem_free((caddr_t)memrange, proplen);
8157c478bd9Sstevel@tonic-gate 	}
8167c478bd9Sstevel@tonic-gate 
8177c478bd9Sstevel@tonic-gate 	if (ndi_ra_map_setup(usedpdip, NDI_RA_TYPE_INTR) == NDI_FAILURE) {
8187c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
8197c478bd9Sstevel@tonic-gate 	}
8207c478bd9Sstevel@tonic-gate 
8217c478bd9Sstevel@tonic-gate 	/* initialize the interrupt space */
8227c478bd9Sstevel@tonic-gate 	(void) ndi_ra_free(usedpdip, 0, 16, NDI_RA_TYPE_INTR, 0);
8237c478bd9Sstevel@tonic-gate 
8247c478bd9Sstevel@tonic-gate #if defined(__i386) || defined(__amd64)
8257c478bd9Sstevel@tonic-gate 	bzero(&req, sizeof (req));
8267c478bd9Sstevel@tonic-gate 	req.ra_addr = 2;	/* 2 == 9 so never allow */
8277c478bd9Sstevel@tonic-gate 	req.ra_len = 1;
8287c478bd9Sstevel@tonic-gate 	req.ra_flags = NDI_RA_ALLOC_SPECIFIED;
8297c478bd9Sstevel@tonic-gate 	(void) ndi_ra_alloc(usedpdip, &req, &retbase, &retlen,
8307c478bd9Sstevel@tonic-gate 	    NDI_RA_TYPE_INTR, 0);
8317c478bd9Sstevel@tonic-gate #endif
8327c478bd9Sstevel@tonic-gate 
833a3282898Scth 	if (ddi_getlongprop(DDI_DEV_T_ANY, used, DDI_PROP_DONTPASS,
8347c478bd9Sstevel@tonic-gate 	    "interrupts", (caddr_t)&irq, &proplen) == DDI_SUCCESS) {
8357c478bd9Sstevel@tonic-gate 		/* Initialize available interrupts by negating the used */
8367c478bd9Sstevel@tonic-gate 		len = (proplen / sizeof (uint32_t));
8377c478bd9Sstevel@tonic-gate 		for (i = 0; i < len; i++) {
8387c478bd9Sstevel@tonic-gate 			bzero((caddr_t)&req, sizeof (req));
8397c478bd9Sstevel@tonic-gate 			req.ra_addr = (uint64_t)irq[i];
8407c478bd9Sstevel@tonic-gate 			req.ra_len = 1;
8417c478bd9Sstevel@tonic-gate 			req.ra_flags = NDI_RA_ALLOC_SPECIFIED;
8427c478bd9Sstevel@tonic-gate 			(void) ndi_ra_alloc(usedpdip, &req, &retbase, &retlen,
8437c478bd9Sstevel@tonic-gate 			    NDI_RA_TYPE_INTR, 0);
8447c478bd9Sstevel@tonic-gate 		}
8457c478bd9Sstevel@tonic-gate 		kmem_free((caddr_t)irq, proplen);
8467c478bd9Sstevel@tonic-gate 	}
8477c478bd9Sstevel@tonic-gate 
8487c478bd9Sstevel@tonic-gate #ifdef BUSRA_DEBUG
8497c478bd9Sstevel@tonic-gate 	if (busra_debug) {
8507c478bd9Sstevel@tonic-gate 		(void) ra_dump_all(NULL, usedpdip);
8517c478bd9Sstevel@tonic-gate 	}
8527c478bd9Sstevel@tonic-gate #endif
8537c478bd9Sstevel@tonic-gate 	return (NDI_SUCCESS);
8547c478bd9Sstevel@tonic-gate 
8557c478bd9Sstevel@tonic-gate }
8567c478bd9Sstevel@tonic-gate 
8577c478bd9Sstevel@tonic-gate #ifdef BUSRA_DEBUG
8587c478bd9Sstevel@tonic-gate void
8597c478bd9Sstevel@tonic-gate ra_dump_all(char *type, dev_info_t *dip)
8607c478bd9Sstevel@tonic-gate {
8617c478bd9Sstevel@tonic-gate 
8627c478bd9Sstevel@tonic-gate 	struct ra_type_map *typemap;
8637c478bd9Sstevel@tonic-gate 	struct ra_dip_type *dipmap;
8647c478bd9Sstevel@tonic-gate 	struct ra_resource *res;
8657c478bd9Sstevel@tonic-gate 
8667c478bd9Sstevel@tonic-gate 	typemap =  (struct ra_type_map *)ra_map_list_head;
8677c478bd9Sstevel@tonic-gate 
8687c478bd9Sstevel@tonic-gate 	for (; typemap != NULL; typemap = typemap->ra_next) {
8697c478bd9Sstevel@tonic-gate 		if (type != NULL) {
8707c478bd9Sstevel@tonic-gate 			if (strcmp(typemap->type, type) != 0)
8717c478bd9Sstevel@tonic-gate 				continue;
8727c478bd9Sstevel@tonic-gate 		}
8737c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "type is %s\n", typemap->type);
8747c478bd9Sstevel@tonic-gate 		for (dipmap = typemap->ra_dip_list; dipmap != NULL;
8757c478bd9Sstevel@tonic-gate 			dipmap = dipmap->ra_next) {
8767c478bd9Sstevel@tonic-gate 			if (dip != NULL) {
8777c478bd9Sstevel@tonic-gate 				if ((dipmap->ra_dip) != dip)
8787c478bd9Sstevel@tonic-gate 					continue;
8797c478bd9Sstevel@tonic-gate 			}
8807c478bd9Sstevel@tonic-gate 			cmn_err(CE_CONT, "  dip is %p\n",
8817c478bd9Sstevel@tonic-gate 			    (void *)dipmap->ra_dip);
8827c478bd9Sstevel@tonic-gate 			for (res = dipmap->ra_rangeset; res != NULL;
8837c478bd9Sstevel@tonic-gate 				res = res->ra_next) {
8847c478bd9Sstevel@tonic-gate 				cmn_err(CE_CONT, "\t  range is %" PRIx64
8857c478bd9Sstevel@tonic-gate 				    " %" PRIx64 "\n", res->ra_base,
8867c478bd9Sstevel@tonic-gate 				    res->ra_len);
8877c478bd9Sstevel@tonic-gate 			}
8887c478bd9Sstevel@tonic-gate 			if (dip != NULL)
8897c478bd9Sstevel@tonic-gate 				break;
8907c478bd9Sstevel@tonic-gate 		}
8917c478bd9Sstevel@tonic-gate 		if (type != NULL)
8927c478bd9Sstevel@tonic-gate 			break;
8937c478bd9Sstevel@tonic-gate 	}
8947c478bd9Sstevel@tonic-gate }
8957c478bd9Sstevel@tonic-gate #endif
8967c478bd9Sstevel@tonic-gate 
8977c478bd9Sstevel@tonic-gate struct bus_range {	/* 1275 "bus-range" property definition */
8987c478bd9Sstevel@tonic-gate 	uint32_t lo;
8997c478bd9Sstevel@tonic-gate 	uint32_t hi;
9007c478bd9Sstevel@tonic-gate } pci_bus_range;
9017c478bd9Sstevel@tonic-gate 
9027c478bd9Sstevel@tonic-gate struct busnum_ctrl {
9037c478bd9Sstevel@tonic-gate 	int	rv;
9047c478bd9Sstevel@tonic-gate 	dev_info_t *dip;
9057c478bd9Sstevel@tonic-gate 	struct	bus_range *range;
9067c478bd9Sstevel@tonic-gate };
9077c478bd9Sstevel@tonic-gate 
9087c478bd9Sstevel@tonic-gate 
9097c478bd9Sstevel@tonic-gate /*
9107c478bd9Sstevel@tonic-gate  * Setup resource map for the pci bus node based on the "available"
9117c478bd9Sstevel@tonic-gate  * property and "bus-range" property.
9127c478bd9Sstevel@tonic-gate  */
9137c478bd9Sstevel@tonic-gate int
9147c478bd9Sstevel@tonic-gate pci_resource_setup(dev_info_t *dip)
9157c478bd9Sstevel@tonic-gate {
9167c478bd9Sstevel@tonic-gate 	pci_regspec_t *regs;
9177c478bd9Sstevel@tonic-gate 	int rlen, rcount, i;
9187c478bd9Sstevel@tonic-gate 	char bus_type[16] = "(unknown)";
9197c478bd9Sstevel@tonic-gate 	int len;
9207c478bd9Sstevel@tonic-gate 	struct busnum_ctrl ctrl;
9217c478bd9Sstevel@tonic-gate 	int circular_count;
9227c478bd9Sstevel@tonic-gate 	int rval = NDI_SUCCESS;
9237c478bd9Sstevel@tonic-gate 
9247c478bd9Sstevel@tonic-gate 	/*
9257c478bd9Sstevel@tonic-gate 	 * If this is a pci bus node then look for "available" property
9267c478bd9Sstevel@tonic-gate 	 * to find the available resources on this bus.
9277c478bd9Sstevel@tonic-gate 	 */
9287c478bd9Sstevel@tonic-gate 	len = sizeof (bus_type);
9297c478bd9Sstevel@tonic-gate 	if (ddi_prop_op(DDI_DEV_T_ANY, dip, PROP_LEN_AND_VAL_BUF,
9307c478bd9Sstevel@tonic-gate 	    DDI_PROP_CANSLEEP | DDI_PROP_DONTPASS, "device_type",
9317c478bd9Sstevel@tonic-gate 	    (caddr_t)&bus_type, &len) != DDI_SUCCESS)
9327c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
9337c478bd9Sstevel@tonic-gate 
934*70025d76Sjohnny 	/* it is not a pci/pci-ex bus type */
935c9f965e3Set142600 	if ((strcmp(bus_type, "pci") != 0) && (strcmp(bus_type, "pciex") != 0))
9367c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
9377c478bd9Sstevel@tonic-gate 
9387c478bd9Sstevel@tonic-gate 	/*
9397c478bd9Sstevel@tonic-gate 	 * The pci-hotplug project addresses adding the call
9407c478bd9Sstevel@tonic-gate 	 * to pci_resource_setup from pci nexus driver.
9417c478bd9Sstevel@tonic-gate 	 * However that project would initially be only for x86,
9427c478bd9Sstevel@tonic-gate 	 * so for sparc pcmcia-pci support we still need to call
9437c478bd9Sstevel@tonic-gate 	 * pci_resource_setup in pcic driver. Once all pci nexus drivers
9447c478bd9Sstevel@tonic-gate 	 * are updated to call pci_resource_setup this portion of the
9457c478bd9Sstevel@tonic-gate 	 * code would really become an assert to make sure this
9467c478bd9Sstevel@tonic-gate 	 * function is not called for the same dip twice.
9477c478bd9Sstevel@tonic-gate 	 */
9487c478bd9Sstevel@tonic-gate 	{
9497c478bd9Sstevel@tonic-gate 		if (ra_map_exist(dip, NDI_RA_TYPE_MEM) == NDI_SUCCESS) {
9507c478bd9Sstevel@tonic-gate 			return (NDI_FAILURE);
9517c478bd9Sstevel@tonic-gate 		}
9527c478bd9Sstevel@tonic-gate 	}
9537c478bd9Sstevel@tonic-gate 
9547c478bd9Sstevel@tonic-gate 
955*70025d76Sjohnny 	/*
956*70025d76Sjohnny 	 * Create empty resource maps first.
957*70025d76Sjohnny 	 *
958*70025d76Sjohnny 	 * NOTE: If all the allocated resources are already assigned to
959*70025d76Sjohnny 	 * device(s) in the hot plug slot then "available" property may not
960*70025d76Sjohnny 	 * be present. But, subsequent hot plug operation may unconfigure
961*70025d76Sjohnny 	 * the device in the slot and try to free up it's resources. So,
962*70025d76Sjohnny 	 * at the minimum we should create empty maps here.
963*70025d76Sjohnny 	 */
9647c478bd9Sstevel@tonic-gate 	if (ndi_ra_map_setup(dip, NDI_RA_TYPE_MEM) == NDI_FAILURE) {
9657c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
9667c478bd9Sstevel@tonic-gate 	}
9677c478bd9Sstevel@tonic-gate 
9687c478bd9Sstevel@tonic-gate 	if (ndi_ra_map_setup(dip, NDI_RA_TYPE_IO) == NDI_FAILURE) {
9697c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
9707c478bd9Sstevel@tonic-gate 	}
9717c478bd9Sstevel@tonic-gate 
9727c478bd9Sstevel@tonic-gate 	if (ndi_ra_map_setup(dip, NDI_RA_TYPE_PCI_BUSNUM) == NDI_FAILURE) {
9737c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
9747c478bd9Sstevel@tonic-gate 	}
9757c478bd9Sstevel@tonic-gate 
9767c478bd9Sstevel@tonic-gate 	if (ndi_ra_map_setup(dip, NDI_RA_TYPE_PCI_PREFETCH_MEM) ==
9777c478bd9Sstevel@tonic-gate 	    NDI_FAILURE) {
9787c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
9797c478bd9Sstevel@tonic-gate 	}
9807c478bd9Sstevel@tonic-gate 
981*70025d76Sjohnny 	/* read the "available" property if it is available */
982*70025d76Sjohnny 	if (ddi_getlongprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
983*70025d76Sjohnny 	    "available", (caddr_t)&regs, &rlen) == DDI_SUCCESS) {
984*70025d76Sjohnny 		/*
985*70025d76Sjohnny 		 * create the available resource list for both memory and
986*70025d76Sjohnny 		 * io space
987*70025d76Sjohnny 		 */
9887c478bd9Sstevel@tonic-gate 		rcount = rlen / sizeof (pci_regspec_t);
9897c478bd9Sstevel@tonic-gate 		for (i = 0; i < rcount; i++) {
9907c478bd9Sstevel@tonic-gate 		    switch (PCI_REG_ADDR_G(regs[i].pci_phys_hi)) {
9917c478bd9Sstevel@tonic-gate 		    case PCI_REG_ADDR_G(PCI_ADDR_MEM32):
9927c478bd9Sstevel@tonic-gate 			(void) ndi_ra_free(dip,
9937c478bd9Sstevel@tonic-gate 			    (uint64_t)regs[i].pci_phys_low,
9947c478bd9Sstevel@tonic-gate 			    (uint64_t)regs[i].pci_size_low,
9957c478bd9Sstevel@tonic-gate 			    (regs[i].pci_phys_hi & PCI_REG_PF_M) ?
9967c478bd9Sstevel@tonic-gate 			    NDI_RA_TYPE_PCI_PREFETCH_MEM : NDI_RA_TYPE_MEM,
9977c478bd9Sstevel@tonic-gate 			    0);
9987c478bd9Sstevel@tonic-gate 			break;
9997c478bd9Sstevel@tonic-gate 		    case PCI_REG_ADDR_G(PCI_ADDR_MEM64):
10007c478bd9Sstevel@tonic-gate 			(void) ndi_ra_free(dip,
10017c478bd9Sstevel@tonic-gate 			    ((uint64_t)(regs[i].pci_phys_mid) << 32) |
10027c478bd9Sstevel@tonic-gate 			    ((uint64_t)(regs[i].pci_phys_low)),
10037c478bd9Sstevel@tonic-gate 			    ((uint64_t)(regs[i].pci_size_hi) << 32) |
10047c478bd9Sstevel@tonic-gate 			    ((uint64_t)(regs[i].pci_size_low)),
10057c478bd9Sstevel@tonic-gate 			    (regs[i].pci_phys_hi & PCI_REG_PF_M) ?
10067c478bd9Sstevel@tonic-gate 			    NDI_RA_TYPE_PCI_PREFETCH_MEM : NDI_RA_TYPE_MEM,
10077c478bd9Sstevel@tonic-gate 			    0);
10087c478bd9Sstevel@tonic-gate 			break;
10097c478bd9Sstevel@tonic-gate 		    case PCI_REG_ADDR_G(PCI_ADDR_IO):
10107c478bd9Sstevel@tonic-gate 			(void) ndi_ra_free(dip,
10117c478bd9Sstevel@tonic-gate 			    (uint64_t)regs[i].pci_phys_low,
10127c478bd9Sstevel@tonic-gate 			    (uint64_t)regs[i].pci_size_low,
10137c478bd9Sstevel@tonic-gate 			    NDI_RA_TYPE_IO,
10147c478bd9Sstevel@tonic-gate 			    0);
10157c478bd9Sstevel@tonic-gate 			break;
10167c478bd9Sstevel@tonic-gate 		    case PCI_REG_ADDR_G(PCI_ADDR_CONFIG):
10177c478bd9Sstevel@tonic-gate 			break;
10187c478bd9Sstevel@tonic-gate 		    default:
10197c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN,
10207c478bd9Sstevel@tonic-gate 			    "pci_resource_setup: bad addr type: %x\n",
10217c478bd9Sstevel@tonic-gate 			    PCI_REG_ADDR_G(regs[i].pci_phys_hi));
10227c478bd9Sstevel@tonic-gate 			break;
10237c478bd9Sstevel@tonic-gate 		    }
10247c478bd9Sstevel@tonic-gate 		}
10257c478bd9Sstevel@tonic-gate 		kmem_free((caddr_t)regs, rlen);
1026*70025d76Sjohnny 	}
10277c478bd9Sstevel@tonic-gate 
10287c478bd9Sstevel@tonic-gate 	/*
1029*70025d76Sjohnny 	 * update resource map for available bus numbers if the node
10307c478bd9Sstevel@tonic-gate 	 * has available-bus-range or bus-range property.
10317c478bd9Sstevel@tonic-gate 	 */
10327c478bd9Sstevel@tonic-gate 	len = sizeof (struct bus_range);
1033a3282898Scth 	if (ddi_getlongprop_buf(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
10347c478bd9Sstevel@tonic-gate 	    "available-bus-range", (caddr_t)&pci_bus_range, &len) ==
10357c478bd9Sstevel@tonic-gate 	    DDI_SUCCESS) {
10367c478bd9Sstevel@tonic-gate 		/*
10377c478bd9Sstevel@tonic-gate 		 * Add bus numbers in the range to the free list.
10387c478bd9Sstevel@tonic-gate 		 */
10397c478bd9Sstevel@tonic-gate 		(void) ndi_ra_free(dip, (uint64_t)pci_bus_range.lo,
10407c478bd9Sstevel@tonic-gate 		    (uint64_t)pci_bus_range.hi - (uint64_t)pci_bus_range.lo +
10417c478bd9Sstevel@tonic-gate 		    1, NDI_RA_TYPE_PCI_BUSNUM, 0);
10427c478bd9Sstevel@tonic-gate 	} else {
10437c478bd9Sstevel@tonic-gate 		/*
10447c478bd9Sstevel@tonic-gate 		 * We don't have an available-bus-range property. If, instead,
10457c478bd9Sstevel@tonic-gate 		 * we have a bus-range property we add all the bus numbers
10467c478bd9Sstevel@tonic-gate 		 * in that range to the free list but we must then scan
10477c478bd9Sstevel@tonic-gate 		 * for pci-pci bridges on this bus to find out the if there
10487c478bd9Sstevel@tonic-gate 		 * are any of those bus numbers already in use. If so, we can
10497c478bd9Sstevel@tonic-gate 		 * reclaim them.
10507c478bd9Sstevel@tonic-gate 		 */
10517c478bd9Sstevel@tonic-gate 		len = sizeof (struct bus_range);
1052a3282898Scth 		if (ddi_getlongprop_buf(DDI_DEV_T_ANY, dip,
10537c478bd9Sstevel@tonic-gate 		    DDI_PROP_DONTPASS, "bus-range", (caddr_t)&pci_bus_range,
10547c478bd9Sstevel@tonic-gate 		    &len) == DDI_SUCCESS) {
10557c478bd9Sstevel@tonic-gate 			if (pci_bus_range.lo != pci_bus_range.hi) {
10567c478bd9Sstevel@tonic-gate 				/*
10577c478bd9Sstevel@tonic-gate 				 * Add bus numbers other than the secondary
10587c478bd9Sstevel@tonic-gate 				 * bus number to the free list.
10597c478bd9Sstevel@tonic-gate 				 */
10607c478bd9Sstevel@tonic-gate 				(void) ndi_ra_free(dip,
10617c478bd9Sstevel@tonic-gate 				    (uint64_t)pci_bus_range.lo + 1,
10627c478bd9Sstevel@tonic-gate 				    (uint64_t)pci_bus_range.hi -
10637c478bd9Sstevel@tonic-gate 				    (uint64_t)pci_bus_range.lo,
10647c478bd9Sstevel@tonic-gate 				    NDI_RA_TYPE_PCI_BUSNUM, 0);
10657c478bd9Sstevel@tonic-gate 
10667c478bd9Sstevel@tonic-gate 				/* scan for pci-pci bridges */
10677c478bd9Sstevel@tonic-gate 				ctrl.rv = DDI_SUCCESS;
10687c478bd9Sstevel@tonic-gate 				ctrl.dip = dip;
10697c478bd9Sstevel@tonic-gate 				ctrl.range = &pci_bus_range;
10707c478bd9Sstevel@tonic-gate 				ndi_devi_enter(dip, &circular_count);
10717c478bd9Sstevel@tonic-gate 				ddi_walk_devs(ddi_get_child(dip),
10727c478bd9Sstevel@tonic-gate 				    claim_pci_busnum, (void *)&ctrl);
10737c478bd9Sstevel@tonic-gate 				ndi_devi_exit(dip, circular_count);
10747c478bd9Sstevel@tonic-gate 				if (ctrl.rv != DDI_SUCCESS) {
10757c478bd9Sstevel@tonic-gate 					/* failed to create the map */
10767c478bd9Sstevel@tonic-gate 					(void) ndi_ra_map_destroy(dip,
10777c478bd9Sstevel@tonic-gate 					    NDI_RA_TYPE_PCI_BUSNUM);
10787c478bd9Sstevel@tonic-gate 					rval = NDI_FAILURE;
10797c478bd9Sstevel@tonic-gate 				}
10807c478bd9Sstevel@tonic-gate 			}
10817c478bd9Sstevel@tonic-gate 		}
10827c478bd9Sstevel@tonic-gate 	}
10837c478bd9Sstevel@tonic-gate 
10847c478bd9Sstevel@tonic-gate #ifdef BUSRA_DEBUG
10857c478bd9Sstevel@tonic-gate 	if (busra_debug) {
10867c478bd9Sstevel@tonic-gate 		(void) ra_dump_all(NULL, dip);
10877c478bd9Sstevel@tonic-gate 	}
10887c478bd9Sstevel@tonic-gate #endif
10897c478bd9Sstevel@tonic-gate 
10907c478bd9Sstevel@tonic-gate 	return (rval);
10917c478bd9Sstevel@tonic-gate }
10927c478bd9Sstevel@tonic-gate 
10937c478bd9Sstevel@tonic-gate /*
10947c478bd9Sstevel@tonic-gate  * If the device is a PCI bus device (i.e bus-range property exists) then
10957c478bd9Sstevel@tonic-gate  * claim the bus numbers used by the device from the specified bus
10967c478bd9Sstevel@tonic-gate  * resource map.
10977c478bd9Sstevel@tonic-gate  */
10987c478bd9Sstevel@tonic-gate static int
10997c478bd9Sstevel@tonic-gate claim_pci_busnum(dev_info_t *dip, void *arg)
11007c478bd9Sstevel@tonic-gate {
11017c478bd9Sstevel@tonic-gate 	struct bus_range pci_bus_range;
11027c478bd9Sstevel@tonic-gate 	struct busnum_ctrl *ctrl;
11037c478bd9Sstevel@tonic-gate 	ndi_ra_request_t req;
11047c478bd9Sstevel@tonic-gate 	char bus_type[16] = "(unknown)";
11057c478bd9Sstevel@tonic-gate 	int len;
11067c478bd9Sstevel@tonic-gate 	uint64_t base;
11077c478bd9Sstevel@tonic-gate 	uint64_t retlen;
11087c478bd9Sstevel@tonic-gate 
11097c478bd9Sstevel@tonic-gate 	ctrl = (struct busnum_ctrl *)arg;
11107c478bd9Sstevel@tonic-gate 
11117c478bd9Sstevel@tonic-gate 	/* check if this is a PCI bus node */
11127c478bd9Sstevel@tonic-gate 	len = sizeof (bus_type);
11137c478bd9Sstevel@tonic-gate 	if (ddi_prop_op(DDI_DEV_T_ANY, dip, PROP_LEN_AND_VAL_BUF,
11147c478bd9Sstevel@tonic-gate 	    DDI_PROP_CANSLEEP | DDI_PROP_DONTPASS, "device_type",
11157c478bd9Sstevel@tonic-gate 	    (caddr_t)&bus_type, &len) != DDI_SUCCESS)
11167c478bd9Sstevel@tonic-gate 		return (DDI_WALK_PRUNECHILD);
11177c478bd9Sstevel@tonic-gate 
1118*70025d76Sjohnny 	/* it is not a pci/pci-ex bus type */
1119c9f965e3Set142600 	if ((strcmp(bus_type, "pci") != 0) && (strcmp(bus_type, "pciex") != 0))
11207c478bd9Sstevel@tonic-gate 		return (DDI_WALK_PRUNECHILD);
11217c478bd9Sstevel@tonic-gate 
11227c478bd9Sstevel@tonic-gate 	/* look for the bus-range property */
11237c478bd9Sstevel@tonic-gate 	len = sizeof (struct bus_range);
1124a3282898Scth 	if (ddi_getlongprop_buf(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
11257c478bd9Sstevel@tonic-gate 	    "bus-range", (caddr_t)&pci_bus_range, &len) == DDI_SUCCESS) {
11267c478bd9Sstevel@tonic-gate 		if ((pci_bus_range.lo >= ctrl->range->lo) &&
11277c478bd9Sstevel@tonic-gate 		    (pci_bus_range.hi <= ctrl->range->hi)) {
11287c478bd9Sstevel@tonic-gate 
11297c478bd9Sstevel@tonic-gate 			/* claim the bus range from the bus resource map */
11307c478bd9Sstevel@tonic-gate 			bzero((caddr_t)&req, sizeof (req));
11317c478bd9Sstevel@tonic-gate 			req.ra_addr = (uint64_t)pci_bus_range.lo;
11327c478bd9Sstevel@tonic-gate 			req.ra_flags |= NDI_RA_ALLOC_SPECIFIED;
11337c478bd9Sstevel@tonic-gate 			req.ra_len = (uint64_t)pci_bus_range.hi -
11347c478bd9Sstevel@tonic-gate 			    (uint64_t)pci_bus_range.lo + 1;
11357c478bd9Sstevel@tonic-gate 			if (ndi_ra_alloc(ctrl->dip, &req, &base, &retlen,
11367c478bd9Sstevel@tonic-gate 			    NDI_RA_TYPE_PCI_BUSNUM, 0) == NDI_SUCCESS)
11377c478bd9Sstevel@tonic-gate 				return (DDI_WALK_PRUNECHILD);
11387c478bd9Sstevel@tonic-gate 		}
11397c478bd9Sstevel@tonic-gate 	}
11407c478bd9Sstevel@tonic-gate 
11417c478bd9Sstevel@tonic-gate 	/*
11427c478bd9Sstevel@tonic-gate 	 * Error return.
11437c478bd9Sstevel@tonic-gate 	 */
11447c478bd9Sstevel@tonic-gate 	ctrl->rv = DDI_FAILURE;
11457c478bd9Sstevel@tonic-gate 	return (DDI_WALK_TERMINATE);
11467c478bd9Sstevel@tonic-gate }
11477c478bd9Sstevel@tonic-gate 
11487c478bd9Sstevel@tonic-gate void
11497c478bd9Sstevel@tonic-gate pci_resource_destroy(dev_info_t *dip)
11507c478bd9Sstevel@tonic-gate {
11517c478bd9Sstevel@tonic-gate 	(void) ndi_ra_map_destroy(dip, NDI_RA_TYPE_IO);
11527c478bd9Sstevel@tonic-gate 
11537c478bd9Sstevel@tonic-gate 	(void) ndi_ra_map_destroy(dip, NDI_RA_TYPE_MEM);
11547c478bd9Sstevel@tonic-gate 
11557c478bd9Sstevel@tonic-gate 	(void) ndi_ra_map_destroy(dip, NDI_RA_TYPE_PCI_BUSNUM);
11567c478bd9Sstevel@tonic-gate 
11577c478bd9Sstevel@tonic-gate 	(void) ndi_ra_map_destroy(dip, NDI_RA_TYPE_PCI_PREFETCH_MEM);
11587c478bd9Sstevel@tonic-gate }
11597c478bd9Sstevel@tonic-gate 
11607c478bd9Sstevel@tonic-gate 
11617c478bd9Sstevel@tonic-gate int
11627c478bd9Sstevel@tonic-gate pci_resource_setup_avail(dev_info_t *dip, pci_regspec_t *avail_p, int entries)
11637c478bd9Sstevel@tonic-gate {
11647c478bd9Sstevel@tonic-gate 	int i;
11657c478bd9Sstevel@tonic-gate 
11667c478bd9Sstevel@tonic-gate 	if (ndi_ra_map_setup(dip, NDI_RA_TYPE_MEM) == NDI_FAILURE)
11677c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
11687c478bd9Sstevel@tonic-gate 	if (ndi_ra_map_setup(dip, NDI_RA_TYPE_IO) == NDI_FAILURE)
11697c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
11707c478bd9Sstevel@tonic-gate 	if (ndi_ra_map_setup(dip, NDI_RA_TYPE_PCI_PREFETCH_MEM) == NDI_FAILURE)
11717c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
11727c478bd9Sstevel@tonic-gate 
11737c478bd9Sstevel@tonic-gate 	/* for each entry in the PCI "available" property */
11747c478bd9Sstevel@tonic-gate 	for (i = 0; i < entries; i++, avail_p++) {
11757c478bd9Sstevel@tonic-gate 		if (avail_p->pci_phys_hi == -1u)
11767c478bd9Sstevel@tonic-gate 			goto err;
11777c478bd9Sstevel@tonic-gate 
11787c478bd9Sstevel@tonic-gate 		switch (PCI_REG_ADDR_G(avail_p->pci_phys_hi)) {
11797c478bd9Sstevel@tonic-gate 		case PCI_REG_ADDR_G(PCI_ADDR_MEM32): {
11807c478bd9Sstevel@tonic-gate 			(void) ndi_ra_free(dip,
11817c478bd9Sstevel@tonic-gate 				(uint64_t)avail_p->pci_phys_low,
11827c478bd9Sstevel@tonic-gate 				(uint64_t)avail_p->pci_size_low,
11837c478bd9Sstevel@tonic-gate 				(avail_p->pci_phys_hi &
11847c478bd9Sstevel@tonic-gate 					PCI_REG_PF_M) ?
11857c478bd9Sstevel@tonic-gate 					NDI_RA_TYPE_PCI_PREFETCH_MEM :
11867c478bd9Sstevel@tonic-gate 					NDI_RA_TYPE_MEM,
11877c478bd9Sstevel@tonic-gate 				0);
11887c478bd9Sstevel@tonic-gate 			}
11897c478bd9Sstevel@tonic-gate 			break;
11907c478bd9Sstevel@tonic-gate 		case PCI_REG_ADDR_G(PCI_ADDR_IO):
11917c478bd9Sstevel@tonic-gate 			(void) ndi_ra_free(dip,
11927c478bd9Sstevel@tonic-gate 				(uint64_t)avail_p->pci_phys_low,
11937c478bd9Sstevel@tonic-gate 				(uint64_t)avail_p->pci_size_low,
11947c478bd9Sstevel@tonic-gate 				NDI_RA_TYPE_IO,
11957c478bd9Sstevel@tonic-gate 				0);
11967c478bd9Sstevel@tonic-gate 			break;
11977c478bd9Sstevel@tonic-gate 		default:
11987c478bd9Sstevel@tonic-gate 			goto err;
11997c478bd9Sstevel@tonic-gate 		}
12007c478bd9Sstevel@tonic-gate 	}
12017c478bd9Sstevel@tonic-gate #ifdef BUSRA_DEBUG
12027c478bd9Sstevel@tonic-gate 	if (busra_debug) {
12037c478bd9Sstevel@tonic-gate 		(void) ra_dump_all(NULL, dip);
12047c478bd9Sstevel@tonic-gate 	}
12057c478bd9Sstevel@tonic-gate #endif
12067c478bd9Sstevel@tonic-gate 	return (NDI_SUCCESS);
12077c478bd9Sstevel@tonic-gate 
12087c478bd9Sstevel@tonic-gate err:
12097c478bd9Sstevel@tonic-gate 	cmn_err(CE_WARN, "pci_resource_setup_avail: bad entry[%d]=%x\n",
12107c478bd9Sstevel@tonic-gate 		i, avail_p->pci_phys_hi);
12117c478bd9Sstevel@tonic-gate 	return (NDI_FAILURE);
12127c478bd9Sstevel@tonic-gate }
1213