xref: /titanic_52/usr/src/uts/common/io/busra.c (revision 5e298e8fe759ff38ee8bb74a75a43cf80f57d057)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #if defined(DEBUG)
27 #define	BUSRA_DEBUG
28 #endif
29 
30 /*
31  * This module provides a set of resource management interfaces
32  * to manage bus resources globally in the system.
33  *
34  * The bus nexus drivers are typically responsible to setup resource
35  * maps for the bus resources available for a bus instance. However
36  * this module also provides resource setup functions for PCI bus
37  * (used by both SPARC and X86 platforms) and ISA bus instances (used
38  * only for X86 platforms).
39  */
40 
41 #include <sys/types.h>
42 #include <sys/systm.h>
43 #include <sys/ddi.h>
44 #include <sys/sunddi.h>
45 #include <sys/sunndi.h>
46 #include <sys/ddi_impldefs.h>
47 #include <sys/ndi_impldefs.h>
48 #include <sys/kmem.h>
49 #include <sys/pctypes.h>
50 #include <sys/modctl.h>
51 #include <sys/debug.h>
52 #include <sys/spl.h>
53 #include <sys/pci.h>
54 #include <sys/autoconf.h>
55 
56 #if defined(BUSRA_DEBUG)
57 int busra_debug = 0;
58 #define	DEBUGPRT \
59 	if (busra_debug) cmn_err
60 
61 #else
62 #define	DEBUGPRT \
63 	if (0) cmn_err
64 #endif
65 
66 
67 /*
68  * global mutex that protects the global list of resource maps.
69  */
70 kmutex_t ra_lock;
71 
72 /*
73  * basic resource element
74  */
75 struct ra_resource {
76 	struct ra_resource *ra_next;
77 	uint64_t	ra_base;
78 	uint64_t 	ra_len;
79 };
80 
81 /*
82  * link list element for the list of dips (and their resource ranges)
83  * for a particular resource type.
84  * ra_rangeset points to the list of resources available
85  * for this type and this dip.
86  */
87 struct ra_dip_type  {
88 	struct ra_dip_type *ra_next;
89 	struct ra_resource  *ra_rangeset;
90 	dev_info_t *ra_dip;
91 };
92 
93 
94 /*
95  * link list element for list of types resources. Each element
96  * has all resources for a particular type.
97  */
98 struct ra_type_map {
99 	struct ra_type_map *ra_next;
100 	struct ra_dip_type *ra_dip_list;
101 	char *type;
102 };
103 
104 
105 /*
106  * place holder to keep the head of the whole global list.
107  * the address of the first typemap would be stored in it.
108  */
109 static struct ra_type_map	*ra_map_list_head = NULL;
110 
111 
112 /*
113  * This is the loadable module wrapper.
114  * It is essentially boilerplate so isn't documented
115  */
116 extern struct mod_ops mod_miscops;
117 
118 #ifdef BUSRA_DEBUG
119 void ra_dump_all();
120 #endif
121 
122 /* internal function prototypes */
123 static struct ra_dip_type *find_dip_map_resources(dev_info_t *dip, char *type,
124     struct ra_dip_type ***backdip, struct ra_type_map ***backtype,
125     uint32_t flag);
126 static int isnot_pow2(uint64_t value);
127 static int claim_pci_busnum(dev_info_t *dip, void *arg);
128 static int ra_map_exist(dev_info_t *dip, char *type);
129 
130 static int pci_get_available_prop(dev_info_t *dip, uint64_t base,
131     uint64_t len, char *busra_type);
132 static int pci_put_available_prop(dev_info_t *dip, uint64_t base,
133     uint64_t len, char *busra_type);
134 static uint32_t pci_type_ra2pci(char *type);
135 static boolean_t is_pcie_fabric(dev_info_t *dip);
136 
137 #define	PCI_ADDR_TYPE_MASK	(PCI_REG_ADDR_M | PCI_REG_PF_M)
138 #define	PCI_ADDR_TYPE_INVAL	0xffffffff
139 
140 #define	RA_INSERT(prev, el) \
141 	el->ra_next = *prev; \
142 	*prev = el;
143 
144 #define	RA_REMOVE(prev, el) \
145 	*prev = el->ra_next;
146 
147 
148 static struct modlmisc modlmisc = {
149 	&mod_miscops,		/* Type of module. This one is a module */
150 	"Bus Resource Allocator (BUSRA)",	/* Name of the module. */
151 };
152 
153 static struct modlinkage modlinkage = {
154 	MODREV_1, (void *)&modlmisc, NULL
155 };
156 
157 int
158 _init()
159 {
160 	int	ret;
161 
162 	mutex_init(&ra_lock, NULL, MUTEX_DRIVER,
163 	    (void *)(intptr_t)__ipltospl(SPL7 - 1));
164 	if ((ret = mod_install(&modlinkage)) != 0) {
165 		mutex_destroy(&ra_lock);
166 	}
167 	return (ret);
168 }
169 
170 int
171 _fini()
172 {
173 	int	ret;
174 
175 	mutex_enter(&ra_lock);
176 
177 	if (ra_map_list_head != NULL) {
178 		mutex_exit(&ra_lock);
179 		return (EBUSY);
180 	}
181 
182 	ret = mod_remove(&modlinkage);
183 
184 	mutex_exit(&ra_lock);
185 
186 	if (ret == 0)
187 		mutex_destroy(&ra_lock);
188 
189 	return (ret);
190 }
191 
192 int
193 _info(struct modinfo *modinfop)
194 
195 {
196 	return (mod_info(&modlinkage, modinfop));
197 }
198 
199 /*
200  * set up an empty resource map for a given type and dip
201  */
202 int
203 ndi_ra_map_setup(dev_info_t *dip, char *type)
204 {
205 	struct ra_type_map  *typemapp;
206 	struct ra_dip_type  *dipmap;
207 	struct ra_dip_type  **backdip;
208 	struct ra_type_map  **backtype;
209 
210 
211 	mutex_enter(&ra_lock);
212 
213 	dipmap = find_dip_map_resources(dip, type, &backdip, &backtype, 0);
214 
215 	if (dipmap == NULL) {
216 		if (backtype == NULL) {
217 			typemapp = (struct ra_type_map *)
218 			    kmem_zalloc(sizeof (*typemapp), KM_SLEEP);
219 			typemapp->type = (char *)kmem_zalloc(strlen(type) + 1,
220 			    KM_SLEEP);
221 			(void) strcpy(typemapp->type, type);
222 			RA_INSERT(&ra_map_list_head, typemapp);
223 		} else {
224 			typemapp = *backtype;
225 		}
226 		if (backdip == NULL) {
227 			/* allocate and insert in list of dips for this type */
228 			dipmap = (struct ra_dip_type *)
229 			    kmem_zalloc(sizeof (*dipmap), KM_SLEEP);
230 			dipmap->ra_dip = dip;
231 			RA_INSERT(&typemapp->ra_dip_list, dipmap);
232 		}
233 	}
234 
235 	mutex_exit(&ra_lock);
236 	return (NDI_SUCCESS);
237 }
238 
239 /*
240  * destroys a resource map for a given dip and type
241  */
242 int
243 ndi_ra_map_destroy(dev_info_t *dip, char *type)
244 {
245 	struct ra_dip_type	*dipmap;
246 	struct ra_dip_type	**backdip;
247 	struct ra_type_map  	**backtype, *typemap;
248 	struct ra_resource	*range;
249 
250 	mutex_enter(&ra_lock);
251 	dipmap = find_dip_map_resources(dip, type, &backdip, &backtype, 0);
252 
253 	if (dipmap == NULL) {
254 		mutex_exit(&ra_lock);
255 		return (NDI_FAILURE);
256 	}
257 
258 	/*
259 	 * destroy all resources for this dip
260 	 * remove dip from type list
261 	 */
262 	ASSERT((backdip != NULL) && (backtype != NULL));
263 	while (dipmap->ra_rangeset != NULL) {
264 		range = dipmap->ra_rangeset;
265 		RA_REMOVE(&dipmap->ra_rangeset, range);
266 		kmem_free((caddr_t)range, sizeof (*range));
267 	}
268 	/* remove from dip list */
269 	RA_REMOVE(backdip, dipmap);
270 	kmem_free((caddr_t)dipmap, sizeof (*dipmap));
271 	if ((*backtype)->ra_dip_list == NULL) {
272 		/*
273 		 * This was the last dip with this resource type.
274 		 * Remove the type from the global list.
275 		 */
276 		typemap = *backtype;
277 		RA_REMOVE(backtype, (*backtype));
278 		kmem_free((caddr_t)typemap->type, strlen(typemap->type) + 1);
279 		kmem_free((caddr_t)typemap, sizeof (*typemap));
280 	}
281 
282 	mutex_exit(&ra_lock);
283 	return (NDI_SUCCESS);
284 }
285 
286 static int
287 ra_map_exist(dev_info_t *dip, char *type)
288 {
289 	struct ra_dip_type  **backdip;
290 	struct ra_type_map  **backtype;
291 
292 	mutex_enter(&ra_lock);
293 	if (find_dip_map_resources(dip, type, &backdip, &backtype, 0) == NULL) {
294 		mutex_exit(&ra_lock);
295 		return (NDI_FAILURE);
296 	}
297 
298 	mutex_exit(&ra_lock);
299 	return (NDI_SUCCESS);
300 }
301 /*
302  * Find a dip map for the specified type, if NDI_RA_PASS will go up on dev tree
303  * if found, backdip and backtype will be updated to point to the previous
304  * dip in the list and previous type for this dip in the list.
305  * If no such type at all in the resource list both backdip and backtype
306  * will be null. If the type found but no dip, back dip will be null.
307  */
308 
309 static struct ra_dip_type *
310 find_dip_map_resources(dev_info_t *dip, char *type,
311     struct ra_dip_type ***backdip, struct ra_type_map ***backtype,
312     uint32_t flag)
313 {
314 	struct ra_type_map **prevmap;
315 	struct ra_dip_type *dipmap, **prevdip;
316 
317 	ASSERT(mutex_owned(&ra_lock));
318 	prevdip = NULL;
319 	dipmap = NULL;
320 	prevmap = &ra_map_list_head;
321 
322 	while (*prevmap) {
323 		if (strcmp((*prevmap)->type, type) == 0)
324 			break;
325 		prevmap = &(*prevmap)->ra_next;
326 	}
327 
328 	if (*prevmap) {
329 		for (; dip != NULL; dip = ddi_get_parent(dip)) {
330 			prevdip = &(*prevmap)->ra_dip_list;
331 			dipmap = *prevdip;
332 
333 			while (dipmap) {
334 				if (dipmap->ra_dip == dip)
335 					break;
336 				prevdip =  &dipmap->ra_next;
337 				dipmap = dipmap->ra_next;
338 			}
339 
340 			if (dipmap != NULL) {
341 				/* found it */
342 				break;
343 			}
344 
345 			if (!(flag & NDI_RA_PASS)) {
346 				break;
347 			}
348 		}
349 	}
350 
351 	*backtype = (*prevmap == NULL) ?  NULL: prevmap;
352 	*backdip = (dipmap == NULL) ?  NULL: prevdip;
353 
354 	return (dipmap);
355 }
356 
357 int
358 ndi_ra_free(dev_info_t *dip, uint64_t base, uint64_t len, char *type,
359     uint32_t flag)
360 {
361 	struct ra_dip_type *dipmap;
362 	struct ra_resource *newmap, *overlapmap, *oldmap = NULL;
363 	struct ra_resource  *mapp, **backp;
364 	uint64_t newend, mapend;
365 	struct ra_dip_type **backdip;
366 	struct ra_type_map **backtype;
367 
368 	if (len == 0) {
369 		return (NDI_SUCCESS);
370 	}
371 
372 	mutex_enter(&ra_lock);
373 
374 	if ((dipmap = find_dip_map_resources(dip, type, &backdip, &backtype,
375 	    flag)) == NULL) {
376 		mutex_exit(&ra_lock);
377 		return (NDI_FAILURE);
378 	}
379 
380 	mapp = dipmap->ra_rangeset;
381 	backp = &dipmap->ra_rangeset;
382 
383 	/* now find where range lies and fix things up */
384 	newend = base + len;
385 	for (; mapp != NULL; backp = &(mapp->ra_next), mapp = mapp->ra_next) {
386 		mapend = mapp->ra_base + mapp->ra_len;
387 
388 		/* check for overlap first */
389 		if ((base <= mapp->ra_base && newend > mapp->ra_base) ||
390 		    (base > mapp->ra_base && base < mapend)) {
391 			/* overlap with mapp */
392 			overlapmap = mapp;
393 			goto overlap;
394 		} else if ((base == mapend && mapp->ra_next) &&
395 		    (newend > mapp->ra_next->ra_base)) {
396 			/* overlap with mapp->ra_next */
397 			overlapmap = mapp->ra_next;
398 			goto overlap;
399 		}
400 
401 		if (newend == mapp->ra_base) {
402 			/* simple - on front */
403 			mapp->ra_base = base;
404 			mapp->ra_len += len;
405 			/*
406 			 * don't need to check if it merges with
407 			 * previous since that would match on on end
408 			 */
409 			break;
410 		} else if (base == mapend) {
411 			/* simple - on end */
412 			mapp->ra_len += len;
413 			if (mapp->ra_next &&
414 			    (newend == mapp->ra_next->ra_base)) {
415 				/* merge with next node */
416 				oldmap = mapp->ra_next;
417 				mapp->ra_len += oldmap->ra_len;
418 				RA_REMOVE(&mapp->ra_next, oldmap);
419 				kmem_free((caddr_t)oldmap, sizeof (*oldmap));
420 			}
421 			break;
422 		} else if (base < mapp->ra_base) {
423 			/* somewhere in between so just an insert */
424 			newmap = (struct ra_resource *)
425 			    kmem_zalloc(sizeof (*newmap), KM_SLEEP);
426 			newmap->ra_base = base;
427 			newmap->ra_len = len;
428 			RA_INSERT(backp, newmap);
429 			break;
430 		}
431 	}
432 	if (mapp == NULL) {
433 		/* stick on end */
434 		newmap = (struct ra_resource *)
435 		    kmem_zalloc(sizeof (*newmap), KM_SLEEP);
436 		newmap->ra_base = base;
437 		newmap->ra_len = len;
438 		RA_INSERT(backp, newmap);
439 	}
440 
441 	mutex_exit(&ra_lock);
442 
443 	/*
444 	 * Update dip's "available" property, adding this piece of
445 	 * resource to the pool.
446 	 */
447 	(void) pci_put_available_prop(dipmap->ra_dip, base, len, type);
448 done:
449 	return (NDI_SUCCESS);
450 
451 overlap:
452 	/*
453 	 * Bad free may happen on some x86 platforms with BIOS exporting
454 	 * incorrect resource maps. The system is otherwise functioning
455 	 * normally. We send such messages to syslog only.
456 	 */
457 	cmn_err(CE_NOTE, "!ndi_ra_free: bad free, dip %p, resource type %s \n",
458 	    (void *)dip, type);
459 	cmn_err(CE_NOTE, "!ndi_ra_free: freeing base 0x%" PRIx64 ", len 0x%"
460 	    PRIX64 " overlaps with existing resource base 0x%" PRIx64
461 	    ", len 0x%" PRIx64 "\n", base, len, overlapmap->ra_base,
462 	    overlapmap->ra_len);
463 
464 	mutex_exit(&ra_lock);
465 	return (NDI_FAILURE);
466 }
467 
468 /* check to see if value is power of 2 or not. */
469 static int
470 isnot_pow2(uint64_t value)
471 {
472 	uint32_t low;
473 	uint32_t hi;
474 
475 	low = value & 0xffffffff;
476 	hi = value >> 32;
477 
478 	/*
479 	 * ddi_ffs and ddi_fls gets long values, so in 32bit environment
480 	 * won't work correctly for 64bit values
481 	 */
482 	if ((ddi_ffs(low) == ddi_fls(low)) &&
483 	    (ddi_ffs(hi) == ddi_fls(hi)))
484 		return (0);
485 	return (1);
486 }
487 
488 static  void
489 adjust_link(struct ra_resource **backp, struct ra_resource *mapp,
490 	    uint64_t base, uint64_t len)
491 {
492 	struct ra_resource *newmap;
493 	uint64_t newlen;
494 
495 	if (base != mapp->ra_base) {
496 		/* in the middle or end */
497 		newlen = base - mapp->ra_base;
498 		if ((mapp->ra_len - newlen) == len) {
499 			/* on the end */
500 			mapp->ra_len = newlen;
501 		} else {
502 			/* in the middle */
503 			newmap = (struct ra_resource *)
504 			    kmem_zalloc(sizeof (*newmap), KM_SLEEP);
505 			newmap->ra_base = base + len;
506 			newmap->ra_len = mapp->ra_len - (len + newlen);
507 			mapp->ra_len = newlen;
508 			RA_INSERT(&(mapp->ra_next), newmap);
509 		}
510 	} else {
511 		/* at the beginning */
512 		mapp->ra_base += len;
513 		mapp->ra_len -= len;
514 		if (mapp->ra_len == 0) {
515 			/* remove the whole node */
516 			RA_REMOVE(backp, mapp);
517 			kmem_free((caddr_t)mapp, sizeof (*mapp));
518 		}
519 	}
520 }
521 
522 int
523 ndi_ra_alloc(dev_info_t *dip, ndi_ra_request_t *req, uint64_t *retbasep,
524     uint64_t *retlenp, char *type, uint32_t flag)
525 {
526 	struct ra_dip_type *dipmap;
527 	struct ra_resource *mapp, **backp, **backlargestp;
528 	uint64_t mask = 0;
529 	uint64_t len, remlen, largestbase, largestlen;
530 	uint64_t base, oldbase, lower, upper;
531 	struct ra_dip_type  **backdip;
532 	struct ra_type_map  **backtype;
533 	int  rval = NDI_FAILURE;
534 
535 
536 	len = req->ra_len;
537 
538 	if (req->ra_flags & NDI_RA_ALIGN_SIZE) {
539 		if (isnot_pow2(req->ra_len)) {
540 			DEBUGPRT(CE_WARN, "ndi_ra_alloc: bad length(pow2) 0x%"
541 			    PRIx64, req->ra_len);
542 			*retbasep = 0;
543 			*retlenp = 0;
544 			return (NDI_FAILURE);
545 		}
546 	}
547 
548 	mask = (req->ra_flags & NDI_RA_ALIGN_SIZE) ? (len - 1) :
549 	    req->ra_align_mask;
550 
551 
552 	mutex_enter(&ra_lock);
553 	dipmap = find_dip_map_resources(dip, type, &backdip, &backtype, flag);
554 	if ((dipmap == NULL) || ((mapp = dipmap->ra_rangeset) == NULL)) {
555 		mutex_exit(&ra_lock);
556 		DEBUGPRT(CE_CONT, "ndi_ra_alloc no map found for this type\n");
557 		return (NDI_FAILURE);
558 	}
559 
560 	DEBUGPRT(CE_CONT, "ndi_ra_alloc: mapp = %p len=%" PRIx64 ", mask=%"
561 	    PRIx64 "\n", (void *)mapp, len, mask);
562 
563 	backp = &(dipmap->ra_rangeset);
564 	backlargestp = NULL;
565 	largestbase = 0;
566 	largestlen = 0;
567 
568 	lower = 0;
569 	upper = ~(uint64_t)0;
570 
571 	if (req->ra_flags & NDI_RA_ALLOC_BOUNDED) {
572 		/* bounded so skip to first possible */
573 		lower = req->ra_boundbase;
574 		upper = req->ra_boundlen + lower;
575 		if ((upper == 0) || (upper < req->ra_boundlen))
576 			upper = ~(uint64_t)0;
577 		DEBUGPRT(CE_CONT, "ndi_ra_alloc: ra_len = %" PRIx64 ", len = %"
578 		    PRIx64 " ra_base=%" PRIx64 ", mask=%" PRIx64
579 		    "\n", mapp->ra_len, len, mapp->ra_base, mask);
580 		for (; mapp != NULL && (mapp->ra_base + mapp->ra_len) < lower;
581 		    backp = &(mapp->ra_next), mapp = mapp->ra_next) {
582 			if (((mapp->ra_len + mapp->ra_base) == 0) ||
583 			    ((mapp->ra_len + mapp->ra_base) < mapp->ra_len))
584 				/*
585 				 * This elements end goes beyond max uint64_t.
586 				 * potential candidate, check end against lower
587 				 * would not be precise.
588 				 */
589 				break;
590 
591 			DEBUGPRT(CE_CONT, " ra_len = %" PRIx64 ", ra_base=%"
592 			    PRIx64 "\n", mapp->ra_len, mapp->ra_base);
593 			}
594 
595 	}
596 
597 	if (!(req->ra_flags & NDI_RA_ALLOC_SPECIFIED)) {
598 		/* first fit - not user specified */
599 		DEBUGPRT(CE_CONT, "ndi_ra_alloc(unspecified request)"
600 		    "lower=%" PRIx64 ", upper=%" PRIx64 "\n", lower, upper);
601 		for (; mapp != NULL && mapp->ra_base <= upper;
602 		    backp = &(mapp->ra_next), mapp = mapp->ra_next) {
603 
604 			DEBUGPRT(CE_CONT, "ndi_ra_alloc: ra_len = %" PRIx64
605 			    ", len = %" PRIx64 "", mapp->ra_len, len);
606 			base = mapp->ra_base;
607 			if (base < lower) {
608 				base = lower;
609 				DEBUGPRT(CE_CONT, "\tbase=%" PRIx64
610 				    ", ra_base=%" PRIx64 ", mask=%" PRIx64,
611 				    base, mapp->ra_base, mask);
612 			}
613 
614 			if ((base & mask) != 0) {
615 				oldbase = base;
616 				/*
617 				 * failed a critical constraint
618 				 * adjust and see if it still fits
619 				 */
620 				base = base & ~mask;
621 				base += (mask + 1);
622 				DEBUGPRT(CE_CONT, "\tnew base=%" PRIx64 "\n",
623 				    base);
624 
625 				/*
626 				 * Check to see if the new base is past
627 				 * the end of the resource.
628 				 */
629 				if (base >= (oldbase + mapp->ra_len + 1)) {
630 					continue;
631 				}
632 			}
633 
634 			if (req->ra_flags & NDI_RA_ALLOC_PARTIAL_OK) {
635 				if ((upper - mapp->ra_base)  <  mapp->ra_len)
636 					remlen = upper - base;
637 				else
638 					remlen = mapp->ra_len -
639 					    (base - mapp->ra_base);
640 
641 				if ((backlargestp == NULL) ||
642 				    (largestlen < remlen)) {
643 
644 					backlargestp = backp;
645 					largestbase = base;
646 					largestlen = remlen;
647 				}
648 			}
649 
650 			if (mapp->ra_len >= len) {
651 				/* a candidate -- apply constraints */
652 				if ((len > (mapp->ra_len -
653 				    (base - mapp->ra_base))) ||
654 				    ((len - 1 + base) > upper)) {
655 					continue;
656 				}
657 
658 				/* we have a fit */
659 
660 				DEBUGPRT(CE_CONT, "\thave a fit\n");
661 
662 				adjust_link(backp, mapp, base, len);
663 				rval = NDI_SUCCESS;
664 				break;
665 
666 			}
667 		}
668 	} else {
669 		/* want an exact value/fit */
670 		base = req->ra_addr;
671 		len = req->ra_len;
672 		for (; mapp != NULL && mapp->ra_base <= upper;
673 		    backp = &(mapp->ra_next), mapp = mapp->ra_next) {
674 			if (base >= mapp->ra_base &&
675 			    ((base - mapp->ra_base) < mapp->ra_len)) {
676 				/*
677 				 * This is the node with he requested base in
678 				 * its range
679 				 */
680 				if ((len > mapp->ra_len) ||
681 				    (base - mapp->ra_base >
682 				    mapp->ra_len - len)) {
683 					/* length requirement not satisfied */
684 					if (req->ra_flags &
685 					    NDI_RA_ALLOC_PARTIAL_OK) {
686 						if ((upper - mapp->ra_base)
687 						    < mapp->ra_len)
688 							remlen = upper - base;
689 						else
690 							remlen =
691 							    mapp->ra_len -
692 							    (base -
693 							    mapp->ra_base);
694 					}
695 					backlargestp = backp;
696 					largestbase = base;
697 					largestlen = remlen;
698 					base = 0;
699 				} else {
700 					/* We have a match */
701 					adjust_link(backp, mapp, base, len);
702 					rval = NDI_SUCCESS;
703 				}
704 				break;
705 			}
706 		}
707 	}
708 
709 	if ((rval != NDI_SUCCESS) &&
710 	    (req->ra_flags & NDI_RA_ALLOC_PARTIAL_OK) &&
711 	    (backlargestp != NULL)) {
712 		adjust_link(backlargestp, *backlargestp, largestbase,
713 		    largestlen);
714 
715 		base = largestbase;
716 		len = largestlen;
717 		rval = NDI_RA_PARTIAL_REQ;
718 	}
719 
720 	mutex_exit(&ra_lock);
721 
722 	if (rval == NDI_FAILURE) {
723 		*retbasep = 0;
724 		*retlenp = 0;
725 	} else {
726 		*retbasep = base;
727 		*retlenp = len;
728 	}
729 
730 	/*
731 	 * Update dip's "available" property, substract this piece of
732 	 * resource from the pool.
733 	 */
734 	if ((rval == NDI_SUCCESS) || (rval == NDI_RA_PARTIAL_REQ))
735 		(void) pci_get_available_prop(dipmap->ra_dip,
736 		    *retbasep, *retlenp, type);
737 
738 	return (rval);
739 }
740 
741 /*
742  * isa_resource_setup
743  *	check for /used-resources and initialize
744  *	based on info there.  If no /used-resources,
745  *	fail.
746  */
747 int
748 isa_resource_setup()
749 {
750 	dev_info_t *used, *usedpdip;
751 	/*
752 	 * note that at this time bootconf creates 32 bit properties for
753 	 * io-space and device-memory
754 	 */
755 	struct iorange {
756 		uint32_t	base;
757 		uint32_t	len;
758 	} *iorange;
759 	struct memrange {
760 		uint32_t	base;
761 		uint32_t	len;
762 	} *memrange;
763 	uint32_t *irq;
764 	int proplen;
765 	int i, len;
766 	int maxrange;
767 	ndi_ra_request_t req;
768 	uint64_t retbase;
769 	uint64_t retlen;
770 
771 	used = ddi_find_devinfo("used-resources", -1, 0);
772 	if (used == NULL) {
773 		DEBUGPRT(CE_CONT,
774 		    "isa_resource_setup: used-resources not found");
775 		return (NDI_FAILURE);
776 	}
777 
778 	/*
779 	 * initialize to all resources being present
780 	 * and then remove the ones in use.
781 	 */
782 
783 	usedpdip = ddi_root_node();
784 
785 	DEBUGPRT(CE_CONT, "isa_resource_setup: used = %p usedpdip = %p\n",
786 	    (void *)used, (void *)usedpdip);
787 
788 	if (ndi_ra_map_setup(usedpdip, NDI_RA_TYPE_IO) == NDI_FAILURE) {
789 		return (NDI_FAILURE);
790 	}
791 
792 	/* initialize io space, highest end base is 0xffff */
793 	/* note that length is highest addr + 1 since starts from 0 */
794 
795 	(void) ndi_ra_free(usedpdip, 0, 0xffff + 1,  NDI_RA_TYPE_IO, 0);
796 
797 	if (ddi_getlongprop(DDI_DEV_T_ANY, used, DDI_PROP_DONTPASS,
798 	    "io-space", (caddr_t)&iorange, &proplen) == DDI_SUCCESS) {
799 		maxrange = proplen / sizeof (struct iorange);
800 		/* remove the "used" I/O resources */
801 		for (i = 0; i < maxrange; i++) {
802 			bzero((caddr_t)&req, sizeof (req));
803 			req.ra_addr =  (uint64_t)iorange[i].base;
804 			req.ra_len = (uint64_t)iorange[i].len;
805 			req.ra_flags = NDI_RA_ALLOC_SPECIFIED;
806 			(void) ndi_ra_alloc(usedpdip, &req, &retbase, &retlen,
807 			    NDI_RA_TYPE_IO, 0);
808 		}
809 
810 		kmem_free((caddr_t)iorange, proplen);
811 	}
812 
813 	if (ndi_ra_map_setup(usedpdip, NDI_RA_TYPE_MEM) == NDI_FAILURE) {
814 		return (NDI_FAILURE);
815 	}
816 	/* initialize memory space where highest end base is 0xffffffff */
817 	/* note that length is highest addr + 1 since starts from 0 */
818 	(void) ndi_ra_free(usedpdip, 0, ((uint64_t)((uint32_t)~0)) + 1,
819 	    NDI_RA_TYPE_MEM, 0);
820 
821 	if (ddi_getlongprop(DDI_DEV_T_ANY, used, DDI_PROP_DONTPASS,
822 	    "device-memory", (caddr_t)&memrange, &proplen) == DDI_SUCCESS) {
823 		maxrange = proplen / sizeof (struct memrange);
824 		/* remove the "used" memory resources */
825 		for (i = 0; i < maxrange; i++) {
826 			bzero((caddr_t)&req, sizeof (req));
827 			req.ra_addr = (uint64_t)memrange[i].base;
828 			req.ra_len = (uint64_t)memrange[i].len;
829 			req.ra_flags = NDI_RA_ALLOC_SPECIFIED;
830 			(void) ndi_ra_alloc(usedpdip, &req, &retbase, &retlen,
831 			    NDI_RA_TYPE_MEM, 0);
832 		}
833 
834 		kmem_free((caddr_t)memrange, proplen);
835 	}
836 
837 	if (ndi_ra_map_setup(usedpdip, NDI_RA_TYPE_INTR) == NDI_FAILURE) {
838 		return (NDI_FAILURE);
839 	}
840 
841 	/* initialize the interrupt space */
842 	(void) ndi_ra_free(usedpdip, 0, 16, NDI_RA_TYPE_INTR, 0);
843 
844 #if defined(__i386) || defined(__amd64)
845 	bzero(&req, sizeof (req));
846 	req.ra_addr = 2;	/* 2 == 9 so never allow */
847 	req.ra_len = 1;
848 	req.ra_flags = NDI_RA_ALLOC_SPECIFIED;
849 	(void) ndi_ra_alloc(usedpdip, &req, &retbase, &retlen,
850 	    NDI_RA_TYPE_INTR, 0);
851 #endif
852 
853 	if (ddi_getlongprop(DDI_DEV_T_ANY, used, DDI_PROP_DONTPASS,
854 	    "interrupts", (caddr_t)&irq, &proplen) == DDI_SUCCESS) {
855 		/* Initialize available interrupts by negating the used */
856 		len = (proplen / sizeof (uint32_t));
857 		for (i = 0; i < len; i++) {
858 			bzero((caddr_t)&req, sizeof (req));
859 			req.ra_addr = (uint64_t)irq[i];
860 			req.ra_len = 1;
861 			req.ra_flags = NDI_RA_ALLOC_SPECIFIED;
862 			(void) ndi_ra_alloc(usedpdip, &req, &retbase, &retlen,
863 			    NDI_RA_TYPE_INTR, 0);
864 		}
865 		kmem_free((caddr_t)irq, proplen);
866 	}
867 
868 #ifdef BUSRA_DEBUG
869 	if (busra_debug) {
870 		(void) ra_dump_all(NULL, usedpdip);
871 	}
872 #endif
873 	return (NDI_SUCCESS);
874 
875 }
876 
877 #ifdef BUSRA_DEBUG
878 void
879 ra_dump_all(char *type, dev_info_t *dip)
880 {
881 
882 	struct ra_type_map *typemap;
883 	struct ra_dip_type *dipmap;
884 	struct ra_resource *res;
885 
886 	typemap =  (struct ra_type_map *)ra_map_list_head;
887 
888 	for (; typemap != NULL; typemap = typemap->ra_next) {
889 		if (type != NULL) {
890 			if (strcmp(typemap->type, type) != 0)
891 				continue;
892 		}
893 		cmn_err(CE_CONT, "type is %s\n", typemap->type);
894 		for (dipmap = typemap->ra_dip_list; dipmap != NULL;
895 		    dipmap = dipmap->ra_next) {
896 			if (dip != NULL) {
897 				if ((dipmap->ra_dip) != dip)
898 					continue;
899 			}
900 			cmn_err(CE_CONT, "  dip is %p\n",
901 			    (void *)dipmap->ra_dip);
902 			for (res = dipmap->ra_rangeset; res != NULL;
903 			    res = res->ra_next) {
904 				cmn_err(CE_CONT, "\t  range is %" PRIx64
905 				    " %" PRIx64 "\n", res->ra_base,
906 				    res->ra_len);
907 			}
908 			if (dip != NULL)
909 				break;
910 		}
911 		if (type != NULL)
912 			break;
913 	}
914 }
915 #endif
916 
917 struct bus_range {	/* 1275 "bus-range" property definition */
918 	uint32_t lo;
919 	uint32_t hi;
920 } pci_bus_range;
921 
922 struct busnum_ctrl {
923 	int	rv;
924 	dev_info_t *dip;
925 	struct	bus_range *range;
926 };
927 
928 
929 /*
930  * Setup resource map for the pci bus node based on the "available"
931  * property and "bus-range" property.
932  */
933 int
934 pci_resource_setup(dev_info_t *dip)
935 {
936 	pci_regspec_t *regs;
937 	int rlen, rcount, i;
938 	char bus_type[16] = "(unknown)";
939 	int len;
940 	struct busnum_ctrl ctrl;
941 	int circular_count;
942 	int rval = NDI_SUCCESS;
943 
944 	/*
945 	 * If this is a pci bus node then look for "available" property
946 	 * to find the available resources on this bus.
947 	 */
948 	len = sizeof (bus_type);
949 	if (ddi_prop_op(DDI_DEV_T_ANY, dip, PROP_LEN_AND_VAL_BUF,
950 	    DDI_PROP_CANSLEEP | DDI_PROP_DONTPASS, "device_type",
951 	    (caddr_t)&bus_type, &len) != DDI_SUCCESS)
952 		return (NDI_FAILURE);
953 
954 	/* it is not a pci/pci-ex bus type */
955 	if ((strcmp(bus_type, "pci") != 0) && (strcmp(bus_type, "pciex") != 0))
956 		return (NDI_FAILURE);
957 
958 	/*
959 	 * The pci-hotplug project addresses adding the call
960 	 * to pci_resource_setup from pci nexus driver.
961 	 * However that project would initially be only for x86,
962 	 * so for sparc pcmcia-pci support we still need to call
963 	 * pci_resource_setup in pcic driver. Once all pci nexus drivers
964 	 * are updated to call pci_resource_setup this portion of the
965 	 * code would really become an assert to make sure this
966 	 * function is not called for the same dip twice.
967 	 */
968 	/*
969 	 * Another user for the check below is hotplug PCI/PCIe bridges.
970 	 *
971 	 * For PCI/PCIE devices under a PCIE hierarchy, ndi_ra_alloc/free
972 	 * will update the devinfo node's "available" property, to reflect
973 	 * the fact that a piece of resource has been removed/added to
974 	 * a devinfo node.
975 	 * During probe of a new PCI bridge in the hotplug case, PCI
976 	 * configurator firstly allocates maximum MEM/IO from its parent,
977 	 * then calls ndi_ra_free() to use these resources to setup busra
978 	 * pool for the new bridge, as well as adding these resources to
979 	 * the "available" property of the new devinfo node. Then configu-
980 	 * rator will attach driver for the bridge before probing its
981 	 * children, and the bridge driver will then initialize its hotplug
982 	 * contollers (if it supports hotplug) and HPC driver will call
983 	 * this function to setup the busra pool, but the resource pool
984 	 * has already been setup at the first of pcicfg_probe_bridge(),
985 	 * thus we need the check below to return directly in this case.
986 	 * Otherwise the ndi_ra_free() below will see overlapping resources.
987 	 */
988 	{
989 		if (ra_map_exist(dip, NDI_RA_TYPE_MEM) == NDI_SUCCESS) {
990 			return (NDI_FAILURE);
991 		}
992 	}
993 
994 
995 	/*
996 	 * Create empty resource maps first.
997 	 *
998 	 * NOTE: If all the allocated resources are already assigned to
999 	 * device(s) in the hot plug slot then "available" property may not
1000 	 * be present. But, subsequent hot plug operation may unconfigure
1001 	 * the device in the slot and try to free up it's resources. So,
1002 	 * at the minimum we should create empty maps here.
1003 	 */
1004 	if (ndi_ra_map_setup(dip, NDI_RA_TYPE_MEM) == NDI_FAILURE) {
1005 		return (NDI_FAILURE);
1006 	}
1007 
1008 	if (ndi_ra_map_setup(dip, NDI_RA_TYPE_IO) == NDI_FAILURE) {
1009 		return (NDI_FAILURE);
1010 	}
1011 
1012 	if (ndi_ra_map_setup(dip, NDI_RA_TYPE_PCI_BUSNUM) == NDI_FAILURE) {
1013 		return (NDI_FAILURE);
1014 	}
1015 
1016 	if (ndi_ra_map_setup(dip, NDI_RA_TYPE_PCI_PREFETCH_MEM) ==
1017 	    NDI_FAILURE) {
1018 		return (NDI_FAILURE);
1019 	}
1020 
1021 	/* read the "available" property if it is available */
1022 	if (ddi_getlongprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
1023 	    "available", (caddr_t)&regs, &rlen) == DDI_SUCCESS) {
1024 		/*
1025 		 * Remove "available" property as the entries will be
1026 		 * re-created in ndi_ra_free() below, note prom based
1027 		 * property will not be removed. But in ndi_ra_free()
1028 		 * we'll be creating non prom based property entries.
1029 		 */
1030 		(void) ndi_prop_remove(DDI_DEV_T_NONE, dip, "available");
1031 		/*
1032 		 * create the available resource list for both memory and
1033 		 * io space
1034 		 */
1035 		rcount = rlen / sizeof (pci_regspec_t);
1036 		for (i = 0; i < rcount; i++) {
1037 			switch (PCI_REG_ADDR_G(regs[i].pci_phys_hi)) {
1038 			case PCI_REG_ADDR_G(PCI_ADDR_MEM32):
1039 				(void) ndi_ra_free(dip,
1040 				    (uint64_t)regs[i].pci_phys_low,
1041 				    (uint64_t)regs[i].pci_size_low,
1042 				    (regs[i].pci_phys_hi & PCI_REG_PF_M) ?
1043 				    NDI_RA_TYPE_PCI_PREFETCH_MEM :
1044 				    NDI_RA_TYPE_MEM,
1045 				    0);
1046 				break;
1047 			case PCI_REG_ADDR_G(PCI_ADDR_MEM64):
1048 				(void) ndi_ra_free(dip,
1049 				    ((uint64_t)(regs[i].pci_phys_mid) << 32) |
1050 				    ((uint64_t)(regs[i].pci_phys_low)),
1051 				    ((uint64_t)(regs[i].pci_size_hi) << 32) |
1052 				    ((uint64_t)(regs[i].pci_size_low)),
1053 				    (regs[i].pci_phys_hi & PCI_REG_PF_M) ?
1054 				    NDI_RA_TYPE_PCI_PREFETCH_MEM :
1055 				    NDI_RA_TYPE_MEM,
1056 				    0);
1057 				break;
1058 			case PCI_REG_ADDR_G(PCI_ADDR_IO):
1059 				(void) ndi_ra_free(dip,
1060 				    (uint64_t)regs[i].pci_phys_low,
1061 				    (uint64_t)regs[i].pci_size_low,
1062 				    NDI_RA_TYPE_IO,
1063 				    0);
1064 				break;
1065 			case PCI_REG_ADDR_G(PCI_ADDR_CONFIG):
1066 				break;
1067 			default:
1068 				cmn_err(CE_WARN,
1069 				    "pci_resource_setup: bad addr type: %x\n",
1070 				    PCI_REG_ADDR_G(regs[i].pci_phys_hi));
1071 				break;
1072 			}
1073 		}
1074 		kmem_free(regs, rlen);
1075 	}
1076 
1077 	/*
1078 	 * update resource map for available bus numbers if the node
1079 	 * has available-bus-range or bus-range property.
1080 	 */
1081 	len = sizeof (struct bus_range);
1082 	if (ddi_getlongprop_buf(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
1083 	    "available-bus-range", (caddr_t)&pci_bus_range, &len) ==
1084 	    DDI_SUCCESS) {
1085 		/*
1086 		 * Add bus numbers in the range to the free list.
1087 		 */
1088 		(void) ndi_ra_free(dip, (uint64_t)pci_bus_range.lo,
1089 		    (uint64_t)pci_bus_range.hi - (uint64_t)pci_bus_range.lo +
1090 		    1, NDI_RA_TYPE_PCI_BUSNUM, 0);
1091 	} else {
1092 		/*
1093 		 * We don't have an available-bus-range property. If, instead,
1094 		 * we have a bus-range property we add all the bus numbers
1095 		 * in that range to the free list but we must then scan
1096 		 * for pci-pci bridges on this bus to find out the if there
1097 		 * are any of those bus numbers already in use. If so, we can
1098 		 * reclaim them.
1099 		 */
1100 		len = sizeof (struct bus_range);
1101 		if (ddi_getlongprop_buf(DDI_DEV_T_ANY, dip,
1102 		    DDI_PROP_DONTPASS, "bus-range", (caddr_t)&pci_bus_range,
1103 		    &len) == DDI_SUCCESS) {
1104 			if (pci_bus_range.lo != pci_bus_range.hi) {
1105 				/*
1106 				 * Add bus numbers other than the secondary
1107 				 * bus number to the free list.
1108 				 */
1109 				(void) ndi_ra_free(dip,
1110 				    (uint64_t)pci_bus_range.lo + 1,
1111 				    (uint64_t)pci_bus_range.hi -
1112 				    (uint64_t)pci_bus_range.lo,
1113 				    NDI_RA_TYPE_PCI_BUSNUM, 0);
1114 
1115 				/* scan for pci-pci bridges */
1116 				ctrl.rv = DDI_SUCCESS;
1117 				ctrl.dip = dip;
1118 				ctrl.range = &pci_bus_range;
1119 				ndi_devi_enter(dip, &circular_count);
1120 				ddi_walk_devs(ddi_get_child(dip),
1121 				    claim_pci_busnum, (void *)&ctrl);
1122 				ndi_devi_exit(dip, circular_count);
1123 				if (ctrl.rv != DDI_SUCCESS) {
1124 					/* failed to create the map */
1125 					(void) ndi_ra_map_destroy(dip,
1126 					    NDI_RA_TYPE_PCI_BUSNUM);
1127 					rval = NDI_FAILURE;
1128 				}
1129 			}
1130 		}
1131 	}
1132 
1133 #ifdef BUSRA_DEBUG
1134 	if (busra_debug) {
1135 		(void) ra_dump_all(NULL, dip);
1136 	}
1137 #endif
1138 
1139 	return (rval);
1140 }
1141 
1142 /*
1143  * If the device is a PCI bus device (i.e bus-range property exists) then
1144  * claim the bus numbers used by the device from the specified bus
1145  * resource map.
1146  */
1147 static int
1148 claim_pci_busnum(dev_info_t *dip, void *arg)
1149 {
1150 	struct bus_range pci_bus_range;
1151 	struct busnum_ctrl *ctrl;
1152 	ndi_ra_request_t req;
1153 	char bus_type[16] = "(unknown)";
1154 	int len;
1155 	uint64_t base;
1156 	uint64_t retlen;
1157 
1158 	ctrl = (struct busnum_ctrl *)arg;
1159 
1160 	/* check if this is a PCI bus node */
1161 	len = sizeof (bus_type);
1162 	if (ddi_prop_op(DDI_DEV_T_ANY, dip, PROP_LEN_AND_VAL_BUF,
1163 	    DDI_PROP_CANSLEEP | DDI_PROP_DONTPASS, "device_type",
1164 	    (caddr_t)&bus_type, &len) != DDI_SUCCESS)
1165 		return (DDI_WALK_PRUNECHILD);
1166 
1167 	/* it is not a pci/pci-ex bus type */
1168 	if ((strcmp(bus_type, "pci") != 0) && (strcmp(bus_type, "pciex") != 0))
1169 		return (DDI_WALK_PRUNECHILD);
1170 
1171 	/* look for the bus-range property */
1172 	len = sizeof (struct bus_range);
1173 	if (ddi_getlongprop_buf(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
1174 	    "bus-range", (caddr_t)&pci_bus_range, &len) == DDI_SUCCESS) {
1175 		if ((pci_bus_range.lo >= ctrl->range->lo) &&
1176 		    (pci_bus_range.hi <= ctrl->range->hi)) {
1177 
1178 			/* claim the bus range from the bus resource map */
1179 			bzero((caddr_t)&req, sizeof (req));
1180 			req.ra_addr = (uint64_t)pci_bus_range.lo;
1181 			req.ra_flags |= NDI_RA_ALLOC_SPECIFIED;
1182 			req.ra_len = (uint64_t)pci_bus_range.hi -
1183 			    (uint64_t)pci_bus_range.lo + 1;
1184 			if (ndi_ra_alloc(ctrl->dip, &req, &base, &retlen,
1185 			    NDI_RA_TYPE_PCI_BUSNUM, 0) == NDI_SUCCESS)
1186 				return (DDI_WALK_PRUNECHILD);
1187 		}
1188 	}
1189 
1190 	/*
1191 	 * Error return.
1192 	 */
1193 	ctrl->rv = DDI_FAILURE;
1194 	return (DDI_WALK_TERMINATE);
1195 }
1196 
1197 void
1198 pci_resource_destroy(dev_info_t *dip)
1199 {
1200 	(void) ndi_ra_map_destroy(dip, NDI_RA_TYPE_IO);
1201 
1202 	(void) ndi_ra_map_destroy(dip, NDI_RA_TYPE_MEM);
1203 
1204 	(void) ndi_ra_map_destroy(dip, NDI_RA_TYPE_PCI_BUSNUM);
1205 
1206 	(void) ndi_ra_map_destroy(dip, NDI_RA_TYPE_PCI_PREFETCH_MEM);
1207 }
1208 
1209 
1210 int
1211 pci_resource_setup_avail(dev_info_t *dip, pci_regspec_t *avail_p, int entries)
1212 {
1213 	int i;
1214 
1215 	if (ndi_ra_map_setup(dip, NDI_RA_TYPE_MEM) == NDI_FAILURE)
1216 		return (NDI_FAILURE);
1217 	if (ndi_ra_map_setup(dip, NDI_RA_TYPE_IO) == NDI_FAILURE)
1218 		return (NDI_FAILURE);
1219 	if (ndi_ra_map_setup(dip, NDI_RA_TYPE_PCI_PREFETCH_MEM) == NDI_FAILURE)
1220 		return (NDI_FAILURE);
1221 
1222 	/* for each entry in the PCI "available" property */
1223 	for (i = 0; i < entries; i++, avail_p++) {
1224 		if (avail_p->pci_phys_hi == -1u)
1225 			goto err;
1226 
1227 		switch (PCI_REG_ADDR_G(avail_p->pci_phys_hi)) {
1228 		case PCI_REG_ADDR_G(PCI_ADDR_MEM32): {
1229 			(void) ndi_ra_free(dip, (uint64_t)avail_p->pci_phys_low,
1230 			    (uint64_t)avail_p->pci_size_low,
1231 			    (avail_p->pci_phys_hi & PCI_REG_PF_M) ?
1232 			    NDI_RA_TYPE_PCI_PREFETCH_MEM : NDI_RA_TYPE_MEM,
1233 			    0);
1234 			}
1235 			break;
1236 		case PCI_REG_ADDR_G(PCI_ADDR_IO):
1237 			(void) ndi_ra_free(dip, (uint64_t)avail_p->pci_phys_low,
1238 			    (uint64_t)avail_p->pci_size_low, NDI_RA_TYPE_IO, 0);
1239 			break;
1240 		default:
1241 			goto err;
1242 		}
1243 	}
1244 #ifdef BUSRA_DEBUG
1245 	if (busra_debug) {
1246 		(void) ra_dump_all(NULL, dip);
1247 	}
1248 #endif
1249 	return (NDI_SUCCESS);
1250 
1251 err:
1252 	cmn_err(CE_WARN, "pci_resource_setup_avail: bad entry[%d]=%x\n",
1253 	    i, avail_p->pci_phys_hi);
1254 	return (NDI_FAILURE);
1255 }
1256 
1257 /*
1258  * Return true if the devinfo node resides on PCI or PCI Express bus,
1259  * sitting in a PCI Express hierarchy.
1260  */
1261 static boolean_t
1262 is_pcie_fabric(dev_info_t *dip)
1263 {
1264 	dev_info_t *root = ddi_root_node();
1265 	dev_info_t *pdip;
1266 	boolean_t found = B_FALSE;
1267 	char *bus;
1268 
1269 	/*
1270 	 * Is this pci/pcie ?
1271 	 */
1272 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, dip,
1273 	    DDI_PROP_DONTPASS, "device_type", &bus) !=
1274 	    DDI_PROP_SUCCESS) {
1275 		DEBUGPRT(CE_WARN, "is_pcie_fabric: cannot find "
1276 		    "\"device_type\" property for dip %p\n", (void *)dip);
1277 		return (B_FALSE);
1278 	}
1279 
1280 	if (strcmp(bus, "pciex") == 0) {
1281 		/* pcie bus, done */
1282 		ddi_prop_free(bus);
1283 		return (B_TRUE);
1284 	} else if (strcmp(bus, "pci") == 0) {
1285 		/*
1286 		 * pci bus, fall through to check if it resides in
1287 		 * a pcie hierarchy.
1288 		 */
1289 		ddi_prop_free(bus);
1290 	} else {
1291 		/* other bus, return failure */
1292 		ddi_prop_free(bus);
1293 		return (B_FALSE);
1294 	}
1295 
1296 	/*
1297 	 * Does this device reside in a pcie fabric ?
1298 	 */
1299 	for (pdip = ddi_get_parent(dip); pdip && (pdip != root) &&
1300 	    !found; pdip = ddi_get_parent(pdip)) {
1301 		if (ddi_prop_lookup_string(DDI_DEV_T_ANY, pdip,
1302 		    DDI_PROP_DONTPASS, "device_type", &bus) !=
1303 		    DDI_PROP_SUCCESS)
1304 			break;
1305 
1306 		if (strcmp(bus, "pciex") == 0)
1307 			found = B_TRUE;
1308 
1309 		ddi_prop_free(bus);
1310 	}
1311 
1312 	return (found);
1313 }
1314 
1315 /*
1316  * Remove a piece of IO/MEM resource from "available" property of 'dip'.
1317  */
1318 static int
1319 pci_get_available_prop(dev_info_t *dip, uint64_t base, uint64_t len,
1320     char *busra_type)
1321 {
1322 	pci_regspec_t	*regs, *newregs;
1323 	uint_t		status;
1324 	int		rlen, rcount;
1325 	int		i, j, k;
1326 	uint64_t	dlen;
1327 	boolean_t	found = B_FALSE;
1328 	uint32_t	type;
1329 
1330 	/* check if we're manipulating MEM/IO resource */
1331 	if ((type = pci_type_ra2pci(busra_type)) == PCI_ADDR_TYPE_INVAL)
1332 		return (DDI_SUCCESS);
1333 
1334 	/* check if dip is a pci/pcie device resides in a pcie fabric */
1335 	if (!is_pcie_fabric(dip))
1336 		return (DDI_SUCCESS);
1337 
1338 	status = ddi_getlongprop(DDI_DEV_T_ANY, dip,
1339 	    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
1340 	    "available", (caddr_t)&regs, &rlen);
1341 
1342 	ASSERT(status == DDI_SUCCESS);
1343 	if (status != DDI_SUCCESS)
1344 		return (status);
1345 
1346 	/*
1347 	 * The updated "available" property will at most have one more entry
1348 	 * than existing one (when the requested range is in the middle of
1349 	 * the matched property entry)
1350 	 */
1351 	newregs = kmem_alloc(rlen + sizeof (pci_regspec_t), KM_SLEEP);
1352 
1353 	rcount = rlen / sizeof (pci_regspec_t);
1354 	for (i = 0, j = 0; i < rcount; i++) {
1355 		if (type == (regs[i].pci_phys_hi & PCI_ADDR_TYPE_MASK)) {
1356 			uint64_t range_base, range_len;
1357 
1358 			range_base = ((uint64_t)(regs[i].pci_phys_mid) << 32) |
1359 			    ((uint64_t)(regs[i].pci_phys_low));
1360 			range_len = ((uint64_t)(regs[i].pci_size_hi) << 32) |
1361 			    ((uint64_t)(regs[i].pci_size_low));
1362 
1363 			if ((base < range_base) ||
1364 			    (base + len > range_base + range_len)) {
1365 				/*
1366 				 * not a match, copy the entry
1367 				 */
1368 				goto copy_entry;
1369 			}
1370 
1371 			/*
1372 			 * range_base	base	base+len	range_base
1373 			 *					+range_len
1374 			 *   +------------+-----------+----------+
1375 			 *   |		  |///////////|		 |
1376 			 *   +------------+-----------+----------+
1377 			 */
1378 			/*
1379 			 * Found a match, remove the range out of this entry.
1380 			 */
1381 			found = B_TRUE;
1382 
1383 			dlen = base - range_base;
1384 			if (dlen != 0) {
1385 				newregs[j].pci_phys_hi = regs[i].pci_phys_hi;
1386 				newregs[j].pci_phys_mid =
1387 				    (uint32_t)(range_base >> 32);
1388 				newregs[j].pci_phys_low =
1389 				    (uint32_t)(range_base);
1390 				newregs[j].pci_size_hi = (uint32_t)(dlen >> 32);
1391 				newregs[j].pci_size_low = (uint32_t)dlen;
1392 				j++;
1393 			}
1394 
1395 			dlen = (range_base + range_len) - (base + len);
1396 			if (dlen != 0) {
1397 				newregs[j].pci_phys_hi = regs[i].pci_phys_hi;
1398 				newregs[j].pci_phys_mid =
1399 				    (uint32_t)((base + len)>> 32);
1400 				newregs[j].pci_phys_low =
1401 				    (uint32_t)(base + len);
1402 				newregs[j].pci_size_hi = (uint32_t)(dlen >> 32);
1403 				newregs[j].pci_size_low = (uint32_t)dlen;
1404 				j++;
1405 			}
1406 
1407 			/*
1408 			 * We've allocated the resource from the matched
1409 			 * entry, almost finished but still need to copy
1410 			 * the rest entries from the original property
1411 			 * array.
1412 			 */
1413 			for (k = i + 1; k < rcount; k++) {
1414 				newregs[j] = regs[k];
1415 				j++;
1416 			}
1417 
1418 			goto done;
1419 
1420 		} else {
1421 copy_entry:
1422 			newregs[j] = regs[i];
1423 			j++;
1424 		}
1425 	}
1426 
1427 done:
1428 	/*
1429 	 * This should not fail so assert it. For non-debug kernel we don't
1430 	 * want to panic thus only logging a warning message.
1431 	 */
1432 	ASSERT(found == B_TRUE);
1433 	if (!found) {
1434 		cmn_err(CE_WARN, "pci_get_available_prop: failed to remove "
1435 		    "resource from dip %p : base 0x%" PRIx64 ", len 0x%" PRIX64
1436 		    ", type 0x%x\n", (void *)dip, base, len, type);
1437 		kmem_free(newregs, rlen + sizeof (pci_regspec_t));
1438 		kmem_free(regs, rlen);
1439 
1440 		return (DDI_FAILURE);
1441 	}
1442 
1443 	/*
1444 	 * Found the resources from parent, update the "available"
1445 	 * property.
1446 	 */
1447 	if (j == 0) {
1448 		/* all the resources are consumed, remove the property */
1449 		(void) ndi_prop_remove(DDI_DEV_T_NONE, dip, "available");
1450 	} else {
1451 		/*
1452 		 * There are still resource available in the parent dip,
1453 		 * update with the remaining resources.
1454 		 */
1455 		(void) ndi_prop_update_int_array(DDI_DEV_T_NONE, dip,
1456 		    "available", (int *)newregs,
1457 		    (j * sizeof (pci_regspec_t)) / sizeof (int));
1458 	}
1459 
1460 	kmem_free(newregs, rlen + sizeof (pci_regspec_t));
1461 	kmem_free(regs, rlen);
1462 
1463 	return (DDI_SUCCESS);
1464 }
1465 
1466 /*
1467  * Add a piece of IO/MEM resource to "available" property of 'dip'.
1468  */
1469 static int
1470 pci_put_available_prop(dev_info_t *dip, uint64_t base, uint64_t len,
1471     char *busra_type)
1472 {
1473 	pci_regspec_t	*regs, *newregs;
1474 	uint_t		status;
1475 	int		rlen, rcount;
1476 	int		i, j, k;
1477 	int		matched = 0;
1478 	uint64_t	orig_base = base;
1479 	uint64_t	orig_len = len;
1480 	uint32_t	type;
1481 
1482 	/* check if we're manipulating MEM/IO resource */
1483 	if ((type = pci_type_ra2pci(busra_type)) == PCI_ADDR_TYPE_INVAL)
1484 		return (DDI_SUCCESS);
1485 
1486 	/* check if dip is a pci/pcie device resides in a pcie fabric */
1487 	if (!is_pcie_fabric(dip))
1488 		return (DDI_SUCCESS);
1489 
1490 	status = ddi_getlongprop(DDI_DEV_T_ANY, dip,
1491 	    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
1492 	    "available", (caddr_t)&regs, &rlen);
1493 
1494 	switch (status) {
1495 		case DDI_PROP_NOT_FOUND:
1496 			goto not_found;
1497 
1498 		case DDI_PROP_SUCCESS:
1499 			break;
1500 
1501 		default:
1502 			return (status);
1503 	}
1504 
1505 	/*
1506 	 * The "available" property exist on the node, try to put this
1507 	 * resource back, merge if there are adjacent resources.
1508 	 *
1509 	 * The updated "available" property will at most have one more entry
1510 	 * than existing one (when there is no adjacent entries thus the new
1511 	 * resource is appended at the end)
1512 	 */
1513 	newregs = kmem_alloc(rlen + sizeof (pci_regspec_t), KM_SLEEP);
1514 
1515 	rcount = rlen / sizeof (pci_regspec_t);
1516 	for (i = 0, j = 0; i < rcount; i++) {
1517 		if (type == (regs[i].pci_phys_hi & PCI_ADDR_TYPE_MASK)) {
1518 			uint64_t range_base, range_len;
1519 
1520 			range_base = ((uint64_t)(regs[i].pci_phys_mid) << 32) |
1521 			    ((uint64_t)(regs[i].pci_phys_low));
1522 			range_len = ((uint64_t)(regs[i].pci_size_hi) << 32) |
1523 			    ((uint64_t)(regs[i].pci_size_low));
1524 
1525 			if ((base + len < range_base) ||
1526 			    (base > range_base + range_len)) {
1527 				/*
1528 				 * Not adjacent, copy the entry and contiue
1529 				 */
1530 				goto copy_entry;
1531 			}
1532 
1533 			/*
1534 			 * Adjacent or overlap?
1535 			 *
1536 			 * Should not have overlapping resources so assert it.
1537 			 * For non-debug kernel we don't want to panic thus
1538 			 * only logging a warning message.
1539 			 */
1540 #if 0
1541 			ASSERT((base + len == range_base) ||
1542 			    (base == range_base + range_len));
1543 #endif
1544 			if ((base + len != range_base) &&
1545 			    (base != range_base + range_len)) {
1546 				cmn_err(CE_WARN, "pci_put_available_prop: "
1547 				    "failed to add resource to dip %p : "
1548 				    "base 0x%" PRIx64 ", len 0x%" PRIx64 " "
1549 				    "overlaps with existing resource "
1550 				    "base 0x%" PRIx64 ", len 0x%" PRIx64 "\n",
1551 				    (void *)dip, orig_base, orig_len,
1552 				    range_base, range_len);
1553 
1554 				goto failure;
1555 			}
1556 
1557 			/*
1558 			 * On the left:
1559 			 *
1560 			 * base		range_base
1561 			 *   +-------------+-------------+
1562 			 *   |/////////////|		 |
1563 			 *   +-------------+-------------+
1564 			 *	len		range_len
1565 			 *
1566 			 * On the right:
1567 			 *
1568 			 * range_base	 base
1569 			 *   +-------------+-------------+
1570 			 *   |		   |/////////////|
1571 			 *   +-------------+-------------+
1572 			 *	range_len	len
1573 			 */
1574 			/*
1575 			 * There are at most two piece of resources adjacent
1576 			 * with this resource, assert it.
1577 			 */
1578 			ASSERT(matched < 2);
1579 
1580 			if (!(matched < 2)) {
1581 				cmn_err(CE_WARN, "pci_put_available_prop: "
1582 				    "failed to add resource to dip %p : "
1583 				    "base 0x%" PRIx64 ", len 0x%" PRIx64 " "
1584 				    "found overlaps in existing resources\n",
1585 				    (void *)dip, orig_base, orig_len);
1586 
1587 				goto failure;
1588 			}
1589 
1590 			/* setup base & len to refer to the merged range */
1591 			len += range_len;
1592 			if (base == range_base + range_len)
1593 				base = range_base;
1594 
1595 			if (matched == 0) {
1596 				/*
1597 				 * One adjacent entry, add this resource in
1598 				 */
1599 				newregs[j].pci_phys_hi = regs[i].pci_phys_hi;
1600 				newregs[j].pci_phys_mid =
1601 				    (uint32_t)(base >> 32);
1602 				newregs[j].pci_phys_low = (uint32_t)(base);
1603 				newregs[j].pci_size_hi = (uint32_t)(len >> 32);
1604 				newregs[j].pci_size_low = (uint32_t)len;
1605 
1606 				matched = 1;
1607 				k = j;
1608 				j++;
1609 			} else { /* matched == 1 */
1610 				/*
1611 				 * Two adjacent entries, merge them together
1612 				 */
1613 				newregs[k].pci_phys_hi = regs[i].pci_phys_hi;
1614 				newregs[k].pci_phys_mid =
1615 				    (uint32_t)(base >> 32);
1616 				newregs[k].pci_phys_low = (uint32_t)(base);
1617 				newregs[k].pci_size_hi = (uint32_t)(len >> 32);
1618 				newregs[k].pci_size_low = (uint32_t)len;
1619 
1620 				matched = 2;
1621 			}
1622 		} else {
1623 copy_entry:
1624 			newregs[j] = regs[i];
1625 			j++;
1626 		}
1627 	}
1628 
1629 	if (matched == 0) {
1630 		/* No adjacent entries, append at end */
1631 		ASSERT(j == rcount);
1632 
1633 		/*
1634 		 * According to page 15 of 1275 spec, bit "n" of "available"
1635 		 * should be set to 1.
1636 		 */
1637 		newregs[j].pci_phys_hi = type;
1638 		newregs[j].pci_phys_hi |= PCI_REG_REL_M;
1639 
1640 		newregs[j].pci_phys_mid = (uint32_t)(base >> 32);
1641 		newregs[j].pci_phys_low = (uint32_t)base;
1642 		newregs[j].pci_size_hi = (uint32_t)(len >> 32);
1643 		newregs[j].pci_size_low = (uint32_t)len;
1644 
1645 		j++;
1646 	}
1647 
1648 	(void) ndi_prop_update_int_array(DDI_DEV_T_NONE, dip,
1649 	    "available", (int *)newregs,
1650 	    (j * sizeof (pci_regspec_t)) / sizeof (int));
1651 
1652 	kmem_free(newregs, rlen + sizeof (pci_regspec_t));
1653 	kmem_free(regs, rlen);
1654 	return (DDI_SUCCESS);
1655 
1656 not_found:
1657 	/*
1658 	 * There is no "available" property on the parent node, create it.
1659 	 */
1660 	newregs = kmem_alloc(sizeof (pci_regspec_t), KM_SLEEP);
1661 
1662 	/*
1663 	 * According to page 15 of 1275 spec, bit "n" of "available" should
1664 	 * be set to 1.
1665 	 */
1666 	newregs[0].pci_phys_hi = type;
1667 	newregs[0].pci_phys_hi |= PCI_REG_REL_M;
1668 
1669 	newregs[0].pci_phys_mid = (uint32_t)(base >> 32);
1670 	newregs[0].pci_phys_low = (uint32_t)base;
1671 	newregs[0].pci_size_hi = (uint32_t)(len >> 32);
1672 	newregs[0].pci_size_low = (uint32_t)len;
1673 
1674 	(void) ndi_prop_update_int_array(DDI_DEV_T_NONE, dip,
1675 	    "available", (int *)newregs,
1676 	    sizeof (pci_regspec_t) / sizeof (int));
1677 	kmem_free(newregs, sizeof (pci_regspec_t));
1678 	return (DDI_SUCCESS);
1679 
1680 failure:
1681 	kmem_free(newregs, rlen + sizeof (pci_regspec_t));
1682 	kmem_free(regs, rlen);
1683 	return (DDI_FAILURE);
1684 }
1685 
1686 static uint32_t
1687 pci_type_ra2pci(char *type)
1688 {
1689 	uint32_t	pci_type = PCI_ADDR_TYPE_INVAL;
1690 
1691 	/*
1692 	 * No 64 bit mem support for now
1693 	 */
1694 	if (strcmp(type, NDI_RA_TYPE_IO) == 0) {
1695 		pci_type = PCI_ADDR_IO;
1696 
1697 	} else if (strcmp(type, NDI_RA_TYPE_MEM) == 0) {
1698 		pci_type = PCI_ADDR_MEM32;
1699 
1700 	} else if (strcmp(type, NDI_RA_TYPE_PCI_PREFETCH_MEM)  == 0) {
1701 		pci_type = PCI_ADDR_MEM32;
1702 		pci_type |= PCI_REG_PF_M;
1703 	}
1704 
1705 	return (pci_type);
1706 }
1707