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 (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
23  * Copyright 2019 Joyent, Inc.
24  */
25 
26 /*
27  *     PCI configurator (pcicfg)
28  */
29 
30 #include <sys/sysmacros.h>
31 #include <sys/conf.h>
32 #include <sys/kmem.h>
33 #include <sys/debug.h>
34 #include <sys/modctl.h>
35 #include <sys/autoconf.h>
36 #include <sys/hwconf.h>
37 #include <sys/pcie.h>
38 #include <sys/pcie_impl.h>
39 #include <sys/pci_cap.h>
40 #include <sys/ddi.h>
41 #include <sys/sunndi.h>
42 #include <sys/hotplug/pci/pcicfg.h>
43 #include <sys/ndi_impldefs.h>
44 #include <sys/pci_cfgacc.h>
45 #include <sys/pcie_impl.h>
46 
47 /*
48  * ************************************************************************
49  * *** Implementation specific local data structures/definitions.	***
50  * ************************************************************************
51  */
52 
53 static	int	pcicfg_start_devno = 0;	/* for Debug only */
54 
55 #define	PCICFG_MAX_ARI_FUNCTION 256
56 
57 #define	PCICFG_NODEVICE 42
58 #define	PCICFG_NOMEMORY 43
59 #define	PCICFG_NOMULTI	44
60 #define	PCICFG_NORESRC	45
61 
62 #define	PCICFG_HIADDR(n) ((uint32_t)(((uint64_t)(n) & \
63 	0xFFFFFFFF00000000ULL)>> 32))
64 #define	PCICFG_LOADDR(n) ((uint32_t)((uint64_t)(n) & 0x00000000FFFFFFFF))
65 #define	PCICFG_LADDR(lo, hi)	(((uint64_t)(hi) << 32) | (uint32_t)(lo))
66 
67 #define	PCICFG_HIWORD(n) ((uint16_t)(((uint32_t)(n) & 0xFFFF0000)>> 16))
68 #define	PCICFG_LOWORD(n) ((uint16_t)((uint32_t)(n) & 0x0000FFFF))
69 #define	PCICFG_HIBYTE(n) ((uint8_t)(((uint16_t)(n) & 0xFF00)>> 8))
70 #define	PCICFG_LOBYTE(n) ((uint8_t)((uint16_t)(n) & 0x00FF))
71 
72 #define	PCICFG_ROUND_UP(addr, gran) ((uintptr_t)((gran+addr-1)&(~(gran-1))))
73 #define	PCICFG_ROUND_DOWN(addr, gran) ((uintptr_t)((addr) & ~(gran-1)))
74 
75 #define	PCICFG_MEMGRAN 0x100000
76 #define	PCICFG_IOGRAN 0x1000
77 #define	PCICFG_4GIG_LIMIT 0xFFFFFFFFUL
78 
79 #define	PCICFG_MEM_MULT 4
80 #define	PCICFG_IO_MULT 4
81 #define	PCICFG_RANGE_LEN 3 /* Number of range entries */
82 
83 static int pcicfg_slot_busnums = 8;
84 static int pcicfg_slot_memsize = 32 * PCICFG_MEMGRAN; /* 32MB per slot */
85 static int pcicfg_slot_pf_memsize = 32 * PCICFG_MEMGRAN; /* 32MB per slot */
86 static int pcicfg_slot_iosize = 64 * PCICFG_IOGRAN; /* 64K per slot */
87 static int pcicfg_sec_reset_delay = 3000000;
88 static int pcicfg_do_legacy_props = 1;	/* create legacy compatible prop */
89 
90 typedef struct hole hole_t;
91 
92 struct hole {
93 	uint64_t	start;
94 	uint64_t	len;
95 	hole_t		*next;
96 };
97 
98 typedef struct pcicfg_phdl pcicfg_phdl_t;
99 
100 struct pcicfg_phdl {
101 
102 	dev_info_t	*dip;		/* Associated with the bridge */
103 	dev_info_t	*top_dip;	/* top node of the attach point */
104 	pcicfg_phdl_t	*next;
105 
106 	/* non-prefetchable memory space */
107 	uint64_t	memory_base;	/* Memory base for this attach point */
108 	uint64_t	memory_last;
109 	uint64_t	memory_len;
110 
111 	/* prefetchable memory space */
112 	uint64_t	pf_memory_base;	/* PF Memory base for this Connection */
113 	uint64_t	pf_memory_last;
114 	uint64_t	pf_memory_len;
115 
116 	/* io space */
117 	uint32_t	io_base;	/* I/O base for this attach point */
118 	uint32_t	io_last;
119 	uint32_t	io_len;
120 
121 	int		error;
122 	uint_t		highest_bus;	/* Highest bus seen on the probe */
123 
124 	hole_t		mem_hole;	/* Memory hole linked list. */
125 	hole_t		pf_mem_hole;	/* PF Memory hole linked list. */
126 	hole_t		io_hole;	/* IO hole linked list */
127 
128 	ndi_ra_request_t mem_req;	/* allocator request for memory */
129 	ndi_ra_request_t pf_mem_req;	/* allocator request for PF memory */
130 	ndi_ra_request_t io_req;	/* allocator request for I/O */
131 };
132 
133 struct pcicfg_standard_prop_entry {
134     uchar_t *name;
135     uint_t  config_offset;
136     uint_t  size;
137 };
138 
139 
140 struct pcicfg_name_entry {
141     uint32_t class_code;
142     char  *name;
143 };
144 
145 struct pcicfg_find_ctrl {
146 	uint_t		device;
147 	uint_t		function;
148 	dev_info_t	*dip;
149 };
150 
151 /*
152  * List of Indirect Config Map Devices. At least the intent of the
153  * design is to look for a device in this list during the configure
154  * operation, and if the device is listed here, then it is a nontransparent
155  * bridge, hence load the driver and avail the config map services from
156  * the driver. Class and Subclass should be as defined in the PCI specs
157  * ie. class is 0x6, and subclass is 0x9.
158  */
159 static struct {
160 	uint8_t		mem_range_bar_offset;
161 	uint8_t		io_range_bar_offset;
162 	uint8_t		prefetch_mem_range_bar_offset;
163 } pcicfg_indirect_map_devs[] = {
164 	PCI_CONF_BASE3, PCI_CONF_BASE2, PCI_CONF_BASE3,
165 	0,	0,	0,
166 };
167 
168 #define	PCICFG_MAKE_REG_HIGH(busnum, devnum, funcnum, register)\
169 	(\
170 	((ulong_t)(busnum & 0xff) << 16)    |\
171 	((ulong_t)(devnum & 0x1f) << 11)    |\
172 	((ulong_t)(funcnum & 0x7) <<  8)    |\
173 	((ulong_t)(register & 0x3f)))
174 
175 /*
176  * debug macros:
177  */
178 #if	defined(DEBUG)
179 extern void prom_printf(const char *, ...);
180 
181 /*
182  * Following values are defined for this debug flag.
183  *
184  * 1 = dump configuration header only.
185  * 2 = dump generic debug data only (no config header dumped)
186  * 3 = dump everything (both 1 and 2)
187  */
188 int pcicfg_debug = 0;
189 
190 static void debug(char *, uintptr_t, uintptr_t,
191 	uintptr_t, uintptr_t, uintptr_t);
192 
193 #define	DEBUG0(fmt)\
194 	debug(fmt, 0, 0, 0, 0, 0);
195 #define	DEBUG1(fmt, a1)\
196 	debug(fmt, (uintptr_t)(a1), 0, 0, 0, 0);
197 #define	DEBUG2(fmt, a1, a2)\
198 	debug(fmt, (uintptr_t)(a1), (uintptr_t)(a2), 0, 0, 0);
199 #define	DEBUG3(fmt, a1, a2, a3)\
200 	debug(fmt, (uintptr_t)(a1), (uintptr_t)(a2),\
201 		(uintptr_t)(a3), 0, 0);
202 #define	DEBUG4(fmt, a1, a2, a3, a4)\
203 	debug(fmt, (uintptr_t)(a1), (uintptr_t)(a2),\
204 		(uintptr_t)(a3), (uintptr_t)(a4), 0);
205 #define	DEBUG5(fmt, a1, a2, a3, a4, a5)\
206 	debug(fmt, (uintptr_t)(a1), (uintptr_t)(a2),\
207 		(uintptr_t)(a3), (uintptr_t)(a4), (uintptr_t)(a5));
208 #else
209 #define	DEBUG0(fmt)
210 #define	DEBUG1(fmt, a1)
211 #define	DEBUG2(fmt, a1, a2)
212 #define	DEBUG3(fmt, a1, a2, a3)
213 #define	DEBUG4(fmt, a1, a2, a3, a4)
214 #define	DEBUG5(fmt, a1, a2, a3, a4, a5)
215 #endif
216 
217 /*
218  * forward declarations for routines defined in this module (called here)
219  */
220 
221 static int pcicfg_add_config_reg(dev_info_t *,
222     uint_t, uint_t, uint_t);
223 static int pcicfg_probe_children(dev_info_t *, uint_t, uint_t, uint_t,
224     uint_t *, pcicfg_flags_t, boolean_t);
225 static int pcicfg_match_dev(dev_info_t *, void *);
226 static dev_info_t *pcicfg_devi_find(dev_info_t *, uint_t, uint_t);
227 static pcicfg_phdl_t *pcicfg_find_phdl(dev_info_t *);
228 static pcicfg_phdl_t *pcicfg_create_phdl(dev_info_t *);
229 static int pcicfg_destroy_phdl(dev_info_t *);
230 static int pcicfg_sum_resources(dev_info_t *, void *);
231 static int pcicfg_device_assign(dev_info_t *);
232 static int pcicfg_bridge_assign(dev_info_t *, void *);
233 static int pcicfg_device_assign_readonly(dev_info_t *);
234 static int pcicfg_free_resources(dev_info_t *, pcicfg_flags_t);
235 static void pcicfg_setup_bridge(pcicfg_phdl_t *, ddi_acc_handle_t);
236 static void pcicfg_update_bridge(pcicfg_phdl_t *, ddi_acc_handle_t);
237 static int pcicfg_update_assigned_prop(dev_info_t *, pci_regspec_t *);
238 static void pcicfg_device_on(ddi_acc_handle_t);
239 static void pcicfg_device_off(ddi_acc_handle_t);
240 static int pcicfg_set_busnode_props(dev_info_t *, uint8_t);
241 static int pcicfg_free_bridge_resources(dev_info_t *);
242 static int pcicfg_free_device_resources(dev_info_t *);
243 static int pcicfg_teardown_device(dev_info_t *, pcicfg_flags_t, boolean_t);
244 static void pcicfg_reparent_node(dev_info_t *, dev_info_t *);
245 static int pcicfg_config_setup(dev_info_t *, ddi_acc_handle_t *);
246 static void pcicfg_config_teardown(ddi_acc_handle_t *);
247 static void pcicfg_get_mem(pcicfg_phdl_t *, uint32_t, uint64_t *);
248 static void pcicfg_get_pf_mem(pcicfg_phdl_t *, uint32_t, uint64_t *);
249 static void pcicfg_get_io(pcicfg_phdl_t *, uint32_t, uint32_t *);
250 static int pcicfg_update_ranges_prop(dev_info_t *, ppb_ranges_t *);
251 static int pcicfg_configure_ntbridge(dev_info_t *, uint_t, uint_t);
252 static uint_t pcicfg_ntbridge_child(dev_info_t *);
253 static uint_t pcicfg_get_ntbridge_child_range(dev_info_t *, uint64_t *,
254     uint64_t *, uint_t);
255 static int pcicfg_is_ntbridge(dev_info_t *);
256 static int pcicfg_ntbridge_allocate_resources(dev_info_t *);
257 static int pcicfg_ntbridge_configure_done(dev_info_t *);
258 static int pcicfg_ntbridge_program_child(dev_info_t *);
259 static uint_t pcicfg_ntbridge_unconfigure(dev_info_t *);
260 static int pcicfg_ntbridge_unconfigure_child(dev_info_t *, uint_t);
261 static void pcicfg_free_hole(hole_t *);
262 static uint64_t pcicfg_alloc_hole(hole_t *, uint64_t *, uint32_t);
263 static int pcicfg_device_type(dev_info_t *, ddi_acc_handle_t *);
264 static void pcicfg_update_phdl(dev_info_t *, uint8_t, uint8_t);
265 static int pcicfg_get_cap(ddi_acc_handle_t, uint8_t);
266 static uint8_t pcicfg_get_nslots(dev_info_t *, ddi_acc_handle_t);
267 static int pcicfg_pcie_dev(dev_info_t *, ddi_acc_handle_t);
268 static int pcicfg_pcie_device_type(dev_info_t *, ddi_acc_handle_t);
269 static int pcicfg_pcie_port_type(dev_info_t *, ddi_acc_handle_t);
270 static int pcicfg_probe_bridge(dev_info_t *, ddi_acc_handle_t, uint_t,
271 	uint_t *, boolean_t);
272 static int pcicfg_find_resource_end(dev_info_t *, void *);
273 static boolean_t is_pcie_fabric(dev_info_t *);
274 
275 static int pcicfg_populate_reg_props(dev_info_t *, ddi_acc_handle_t);
276 static int pcicfg_populate_props_from_bar(dev_info_t *, ddi_acc_handle_t);
277 static int pcicfg_update_assigned_prop_value(dev_info_t *, uint32_t,
278     uint32_t, uint32_t, uint_t);
279 static int pcicfg_ari_configure(dev_info_t *);
280 
281 #ifdef DEBUG
282 static void pcicfg_dump_common_config(ddi_acc_handle_t config_handle);
283 static void pcicfg_dump_device_config(ddi_acc_handle_t);
284 static void pcicfg_dump_bridge_config(ddi_acc_handle_t config_handle);
285 static uint64_t pcicfg_unused_space(hole_t *, uint32_t *);
286 
287 #define	PCICFG_DUMP_COMMON_CONFIG(hdl) (void)pcicfg_dump_common_config(hdl)
288 #define	PCICFG_DUMP_DEVICE_CONFIG(hdl) (void)pcicfg_dump_device_config(hdl)
289 #define	PCICFG_DUMP_BRIDGE_CONFIG(hdl) (void)pcicfg_dump_bridge_config(hdl)
290 #else
291 #define	PCICFG_DUMP_COMMON_CONFIG(handle)
292 #define	PCICFG_DUMP_DEVICE_CONFIG(handle)
293 #define	PCICFG_DUMP_BRIDGE_CONFIG(handle)
294 #endif
295 
296 static kmutex_t pcicfg_list_mutex; /* Protects the probe handle list */
297 static pcicfg_phdl_t *pcicfg_phdl_list = NULL;
298 
299 #ifndef _DONT_USE_1275_GENERIC_NAMES
300 /*
301  * Class code table
302  */
303 static struct pcicfg_name_entry pcicfg_class_lookup [] = {
304 
305 	{ 0x001, "display" },
306 	{ 0x100, "scsi" },
307 	{ 0x101, "ide" },
308 	{ 0x102, "fdc" },
309 	{ 0x103, "ipi" },
310 	{ 0x104, "raid" },
311 	{ 0x105, "ata" },
312 	{ 0x106, "sata" },
313 	{ 0x200, "ethernet" },
314 	{ 0x201, "token-ring" },
315 	{ 0x202, "fddi" },
316 	{ 0x203, "atm" },
317 	{ 0x204, "isdn" },
318 	{ 0x206, "mcd" },
319 	{ 0x300, "display" },
320 	{ 0x400, "video" },
321 	{ 0x401, "sound" },
322 	{ 0x500, "memory" },
323 	{ 0x501, "flash" },
324 	{ 0x600, "host" },
325 	{ 0x601, "isa" },
326 	{ 0x602, "eisa" },
327 	{ 0x603, "mca" },
328 	{ 0x604, "pci" },
329 	{ 0x605, "pcmcia" },
330 	{ 0x606, "nubus" },
331 	{ 0x607, "cardbus" },
332 	{ 0x609, "pci" },
333 	{ 0x60a, "ib-pci" },
334 	{ 0x700, "serial" },
335 	{ 0x701, "parallel" },
336 	{ 0x800, "interrupt-controller" },
337 	{ 0x801, "dma-controller" },
338 	{ 0x802, "timer" },
339 	{ 0x803, "rtc" },
340 	{ 0x900, "keyboard" },
341 	{ 0x901, "pen" },
342 	{ 0x902, "mouse" },
343 	{ 0xa00, "dock" },
344 	{ 0xb00, "cpu" },
345 	{ 0xb01, "cpu" },
346 	{ 0xb02, "cpu" },
347 	{ 0xb10, "cpu" },
348 	{ 0xb20, "cpu" },
349 	{ 0xb30, "cpu" },
350 	{ 0xb40, "coproc" },
351 	{ 0xc00, "firewire" },
352 	{ 0xc01, "access-bus" },
353 	{ 0xc02, "ssa" },
354 	{ 0xc03, "usb" },
355 	{ 0xc04, "fibre-channel" },
356 	{ 0xc05, "smbus" },
357 	{ 0xc06, "ib" },
358 	{ 0xd00, "irda" },
359 	{ 0xd01, "ir" },
360 	{ 0xd10, "rf" },
361 	{ 0xd11, "btooth" },
362 	{ 0xd12, "brdband" },
363 	{ 0xd20, "802.11a" },
364 	{ 0xd21, "802.11b" },
365 	{ 0xe00, "i2o" },
366 	{ 0xf01, "tv" },
367 	{ 0xf02, "audio" },
368 	{ 0xf03, "voice" },
369 	{ 0xf04, "data" },
370 	{ 0, 0 }
371 };
372 #endif /* _DONT_USE_1275_GENERIC_NAMES */
373 
374 /*
375  * Module control operations
376  */
377 
378 extern struct mod_ops mod_miscops;
379 
380 static struct modlmisc modlmisc = {
381 	&mod_miscops, /* Type of module */
382 	"PCI configurator"
383 };
384 
385 static struct modlinkage modlinkage = {
386 	MODREV_1, (void *)&modlmisc, NULL
387 };
388 
389 
390 #ifdef DEBUG
391 
392 static void
393 pcicfg_dump_common_config(ddi_acc_handle_t config_handle)
394 {
395 	if ((pcicfg_debug & 1) == 0)
396 		return;
397 	prom_printf(" Vendor ID   = [0x%x]\n",
398 	    pci_config_get16(config_handle, PCI_CONF_VENID));
399 	prom_printf(" Device ID   = [0x%x]\n",
400 	    pci_config_get16(config_handle, PCI_CONF_DEVID));
401 	prom_printf(" Command REG = [0x%x]\n",
402 	    pci_config_get16(config_handle, PCI_CONF_COMM));
403 	prom_printf(" Status  REG = [0x%x]\n",
404 	    pci_config_get16(config_handle, PCI_CONF_STAT));
405 	prom_printf(" Revision ID = [0x%x]\n",
406 	    pci_config_get8(config_handle, PCI_CONF_REVID));
407 	prom_printf(" Prog Class  = [0x%x]\n",
408 	    pci_config_get8(config_handle, PCI_CONF_PROGCLASS));
409 	prom_printf(" Dev Class   = [0x%x]\n",
410 	    pci_config_get8(config_handle, PCI_CONF_SUBCLASS));
411 	prom_printf(" Base Class  = [0x%x]\n",
412 	    pci_config_get8(config_handle, PCI_CONF_BASCLASS));
413 	prom_printf(" Device ID   = [0x%x]\n",
414 	    pci_config_get8(config_handle, PCI_CONF_CACHE_LINESZ));
415 	prom_printf(" Header Type = [0x%x]\n",
416 	    pci_config_get8(config_handle, PCI_CONF_HEADER));
417 	prom_printf(" BIST        = [0x%x]\n",
418 	    pci_config_get8(config_handle, PCI_CONF_BIST));
419 	prom_printf(" BASE 0      = [0x%x]\n",
420 	    pci_config_get32(config_handle, PCI_CONF_BASE0));
421 	prom_printf(" BASE 1      = [0x%x]\n",
422 	    pci_config_get32(config_handle, PCI_CONF_BASE1));
423 
424 }
425 
426 static void
427 pcicfg_dump_device_config(ddi_acc_handle_t config_handle)
428 {
429 	if ((pcicfg_debug & 1) == 0)
430 		return;
431 	pcicfg_dump_common_config(config_handle);
432 
433 	prom_printf(" BASE 2      = [0x%x]\n",
434 	    pci_config_get32(config_handle, PCI_CONF_BASE2));
435 	prom_printf(" BASE 3      = [0x%x]\n",
436 	    pci_config_get32(config_handle, PCI_CONF_BASE3));
437 	prom_printf(" BASE 4      = [0x%x]\n",
438 	    pci_config_get32(config_handle, PCI_CONF_BASE4));
439 	prom_printf(" BASE 5      = [0x%x]\n",
440 	    pci_config_get32(config_handle, PCI_CONF_BASE5));
441 	prom_printf(" Cardbus CIS = [0x%x]\n",
442 	    pci_config_get32(config_handle, PCI_CONF_CIS));
443 	prom_printf(" Sub VID     = [0x%x]\n",
444 	    pci_config_get16(config_handle, PCI_CONF_SUBVENID));
445 	prom_printf(" Sub SID     = [0x%x]\n",
446 	    pci_config_get16(config_handle, PCI_CONF_SUBSYSID));
447 	prom_printf(" ROM         = [0x%x]\n",
448 	    pci_config_get32(config_handle, PCI_CONF_ROM));
449 	prom_printf(" I Line      = [0x%x]\n",
450 	    pci_config_get8(config_handle, PCI_CONF_ILINE));
451 	prom_printf(" I Pin       = [0x%x]\n",
452 	    pci_config_get8(config_handle, PCI_CONF_IPIN));
453 	prom_printf(" Max Grant   = [0x%x]\n",
454 	    pci_config_get8(config_handle, PCI_CONF_MIN_G));
455 	prom_printf(" Max Latent  = [0x%x]\n",
456 	    pci_config_get8(config_handle, PCI_CONF_MAX_L));
457 }
458 
459 static void
460 pcicfg_dump_bridge_config(ddi_acc_handle_t config_handle)
461 {
462 	if ((pcicfg_debug & 1) == 0)
463 		return;
464 	pcicfg_dump_common_config(config_handle);
465 
466 	prom_printf("........................................\n");
467 
468 	prom_printf(" Pri Bus     = [0x%x]\n",
469 	    pci_config_get8(config_handle, PCI_BCNF_PRIBUS));
470 	prom_printf(" Sec Bus     = [0x%x]\n",
471 	    pci_config_get8(config_handle, PCI_BCNF_SECBUS));
472 	prom_printf(" Sub Bus     = [0x%x]\n",
473 	    pci_config_get8(config_handle, PCI_BCNF_SUBBUS));
474 	prom_printf(" Latency     = [0x%x]\n",
475 	    pci_config_get8(config_handle, PCI_BCNF_LATENCY_TIMER));
476 	prom_printf(" I/O Base LO = [0x%x]\n",
477 	    pci_config_get8(config_handle, PCI_BCNF_IO_BASE_LOW));
478 	prom_printf(" I/O Lim LO  = [0x%x]\n",
479 	    pci_config_get8(config_handle, PCI_BCNF_IO_LIMIT_LOW));
480 	prom_printf(" Sec. Status = [0x%x]\n",
481 	    pci_config_get16(config_handle, PCI_BCNF_SEC_STATUS));
482 	prom_printf(" Mem Base    = [0x%x]\n",
483 	    pci_config_get16(config_handle, PCI_BCNF_MEM_BASE));
484 	prom_printf(" Mem Limit   = [0x%x]\n",
485 	    pci_config_get16(config_handle, PCI_BCNF_MEM_LIMIT));
486 	prom_printf(" PF Mem Base = [0x%x]\n",
487 	    pci_config_get16(config_handle, PCI_BCNF_PF_BASE_LOW));
488 	prom_printf(" PF Mem Lim  = [0x%x]\n",
489 	    pci_config_get16(config_handle, PCI_BCNF_PF_LIMIT_LOW));
490 	prom_printf(" PF Base HI  = [0x%x]\n",
491 	    pci_config_get32(config_handle, PCI_BCNF_PF_BASE_HIGH));
492 	prom_printf(" PF Lim  HI  = [0x%x]\n",
493 	    pci_config_get32(config_handle, PCI_BCNF_PF_LIMIT_HIGH));
494 	prom_printf(" I/O Base HI = [0x%x]\n",
495 	    pci_config_get16(config_handle, PCI_BCNF_IO_BASE_HI));
496 	prom_printf(" I/O Lim HI  = [0x%x]\n",
497 	    pci_config_get16(config_handle, PCI_BCNF_IO_LIMIT_HI));
498 	prom_printf(" ROM addr    = [0x%x]\n",
499 	    pci_config_get32(config_handle, PCI_BCNF_ROM));
500 	prom_printf(" Intr Line   = [0x%x]\n",
501 	    pci_config_get8(config_handle, PCI_BCNF_ILINE));
502 	prom_printf(" Intr Pin    = [0x%x]\n",
503 	    pci_config_get8(config_handle, PCI_BCNF_IPIN));
504 	prom_printf(" Bridge Ctrl = [0x%x]\n",
505 	    pci_config_get16(config_handle, PCI_BCNF_BCNTRL));
506 }
507 #endif
508 
509 int
510 _init()
511 {
512 	DEBUG0(" PCI configurator installed\n");
513 	mutex_init(&pcicfg_list_mutex, NULL, MUTEX_DRIVER, NULL);
514 	return (mod_install(&modlinkage));
515 }
516 
517 int
518 _fini(void)
519 {
520 	int error;
521 
522 	error = mod_remove(&modlinkage);
523 	if (error != 0) {
524 		return (error);
525 	}
526 	mutex_destroy(&pcicfg_list_mutex);
527 	return (0);
528 }
529 
530 int
531 _info(struct modinfo *modinfop)
532 {
533 	return (mod_info(&modlinkage, modinfop));
534 }
535 
536 /*
537  * In the following functions ndi_devi_enter() without holding the
538  * parent dip is sufficient. This is because  pci dr is driven through
539  * opens on the nexus which is in the device tree path above the node
540  * being operated on, and implicitly held due to the open.
541  */
542 
543 /*
544  * This entry point is called to configure a device (and
545  * all its children) on the given bus. It is called when
546  * a new device is added to the PCI domain.  This routine
547  * will create the device tree and program the devices
548  * registers.
549  */
550 int
551 pcicfg_configure(dev_info_t *devi, uint_t device, uint_t function,
552     pcicfg_flags_t flags)
553 {
554 	uint_t bus;
555 	int len;
556 	int func;
557 	dev_info_t *attach_point;
558 	pci_bus_range_t pci_bus_range;
559 	int rv;
560 	int circ;
561 	uint_t highest_bus, visited = 0;
562 	int ari_mode = B_FALSE;
563 	int max_function = PCI_MAX_FUNCTIONS;
564 	int trans_device;
565 	dev_info_t *new_device;
566 	boolean_t is_pcie;
567 
568 	if (flags == PCICFG_FLAG_ENABLE_ARI)
569 		return (pcicfg_ari_configure(devi));
570 
571 	/*
572 	 * Start probing at the device specified in "device" on the
573 	 * "bus" specified.
574 	 */
575 	len = sizeof (pci_bus_range_t);
576 	if (ddi_getlongprop_buf(DDI_DEV_T_ANY, devi, 0, "bus-range",
577 	    (caddr_t)&pci_bus_range, &len) != DDI_SUCCESS) {
578 		DEBUG0("no bus-range property\n");
579 		return (PCICFG_FAILURE);
580 	}
581 
582 	bus = pci_bus_range.lo; /* primary bus number of this bus node */
583 
584 	attach_point = devi;
585 
586 	is_pcie = is_pcie_fabric(devi);
587 
588 	ndi_devi_enter(devi, &circ);
589 	for (func = 0; func < max_function; ) {
590 
591 		if ((function != PCICFG_ALL_FUNC) && (function != func))
592 			goto next;
593 
594 		if (ari_mode)
595 			trans_device = func >> 3;
596 		else
597 			trans_device = device;
598 
599 		switch (rv = pcicfg_probe_children(attach_point,
600 		    bus, trans_device, func & 7, &highest_bus,
601 		    flags, is_pcie)) {
602 			case PCICFG_NORESRC:
603 			case PCICFG_FAILURE:
604 				DEBUG2("configure failed: bus [0x%x] device "
605 				    "[0x%x]\n", bus, trans_device);
606 				goto cleanup;
607 			case PCICFG_NODEVICE:
608 				DEBUG3("no device : bus "
609 				    "[0x%x] slot [0x%x] func [0x%x]\n",
610 				    bus, trans_device, func &7);
611 
612 				/*
613 				 * When walking the list of ARI functions
614 				 * we don't expect to see a non-present
615 				 * function, so we will stop walking
616 				 * the function list.
617 				 */
618 				if (ari_mode == B_TRUE)
619 					break;
620 
621 				if (func)
622 					goto next;
623 				break;
624 			default:
625 				DEBUG3("configure: bus => [%d] "
626 				    "slot => [%d] func => [%d]\n",
627 				    bus, trans_device, func & 7);
628 			break;
629 		}
630 
631 		if (rv != PCICFG_SUCCESS)
632 			break;
633 
634 		if ((new_device = pcicfg_devi_find(attach_point,
635 		    trans_device, func & 7)) == NULL) {
636 			DEBUG0("Did'nt find device node just created\n");
637 			goto cleanup;
638 		}
639 
640 		/*
641 		 * Up until now, we have detected a non transparent bridge
642 		 * (ntbridge) as a part of the generic probe code and
643 		 * configured only one configuration
644 		 * header which is the side facing the host bus.
645 		 * Now, configure the other side and create children.
646 		 *
647 		 * In order to make the process simpler, lets load the device
648 		 * driver for the non transparent bridge as this is a
649 		 * Solaris bundled driver, and use its configuration map
650 		 * services rather than programming it here.
651 		 * If the driver is not bundled into Solaris, it must be
652 		 * first loaded and configured before performing any
653 		 * hotplug operations.
654 		 *
655 		 * This not only makes the code here simpler but also more
656 		 * generic.
657 		 *
658 		 * So here we go.
659 		 */
660 
661 		/*
662 		 * check if this is a bridge in nontransparent mode
663 		 */
664 		if (pcicfg_is_ntbridge(new_device) != DDI_FAILURE) {
665 			DEBUG0("pcicfg: Found nontransparent bridge.\n");
666 
667 			rv = pcicfg_configure_ntbridge(new_device, bus,
668 			    trans_device);
669 			if (rv != PCICFG_SUCCESS)
670 				goto cleanup;
671 		}
672 
673 		/*
674 		 * Note that we've successfully gone through and visited at
675 		 * least one node.
676 		 */
677 		visited++;
678 next:
679 		/*
680 		 * Determine if ARI Forwarding should be enabled.
681 		 */
682 		if (func == 0) {
683 			if ((pcie_ari_supported(devi)
684 			    == PCIE_ARI_FORW_SUPPORTED) &&
685 			    (pcie_ari_device(new_device) == PCIE_ARI_DEVICE)) {
686 				if (pcie_ari_enable(devi) == DDI_SUCCESS) {
687 					(void) ddi_prop_create(DDI_DEV_T_NONE,
688 					    devi,  DDI_PROP_CANSLEEP,
689 					    "ari-enabled", NULL, 0);
690 
691 					ari_mode = B_TRUE;
692 					max_function = PCICFG_MAX_ARI_FUNCTION;
693 				}
694 			}
695 		}
696 		if (ari_mode == B_TRUE) {
697 			int next_function;
698 
699 			DEBUG0("Next Function - ARI Device\n");
700 			if (pcie_ari_get_next_function(new_device,
701 			    &next_function) != DDI_SUCCESS)
702 				goto cleanup;
703 
704 			/*
705 			 * Check if there are more functions to probe.
706 			 */
707 			if (next_function == 0) {
708 				DEBUG0("Next Function - "
709 				    "No more ARI Functions\n");
710 				break;
711 			}
712 			func = next_function;
713 		} else {
714 			func++;
715 		}
716 		DEBUG1("Next Function - %x\n", func);
717 	}
718 
719 	ndi_devi_exit(devi, circ);
720 
721 	if (visited == 0)
722 		return (PCICFG_FAILURE);	/* probe failed */
723 	else
724 		return (PCICFG_SUCCESS);
725 
726 cleanup:
727 	/*
728 	 * Clean up a partially created "probe state" tree.
729 	 * There are no resources allocated to the in the
730 	 * probe state.
731 	 */
732 
733 	for (func = 0; func < PCI_MAX_FUNCTIONS; func++) {
734 		if ((function != PCICFG_ALL_FUNC) && (function != func))
735 			continue;
736 
737 		if ((new_device = pcicfg_devi_find(devi, device, func))
738 		    == NULL) {
739 			continue;
740 		}
741 
742 		DEBUG2("Cleaning up device [0x%x] function [0x%x]\n",
743 		    device, func);
744 		/*
745 		 * If this was a bridge device it will have a
746 		 * probe handle - if not, no harm in calling this.
747 		 */
748 		(void) pcicfg_destroy_phdl(new_device);
749 		if (is_pcie) {
750 			/*
751 			 * free pcie_bus_t for the sub-tree
752 			 */
753 			if (ddi_get_child(new_device) != NULL)
754 				pcie_fab_fini_bus(new_device, PCIE_BUS_ALL);
755 
756 			pcie_fini_bus(new_device, PCIE_BUS_ALL);
757 		}
758 		/*
759 		 * This will free up the node
760 		 */
761 		(void) ndi_devi_offline(new_device, NDI_DEVI_REMOVE);
762 	}
763 	ndi_devi_exit(devi, circ);
764 
765 	/*
766 	 * Use private return codes to help identify issues without debugging
767 	 * enabled.  Resource limitations and mis-configurations are
768 	 * probably the most likely caue of configuration failures on x86.
769 	 * Convert return code back to values expected by the external
770 	 * consumer before returning so we will warn only once on the first
771 	 * encountered failure.
772 	 */
773 	if (rv == PCICFG_NORESRC) {
774 		char *path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
775 
776 		(void) ddi_pathname(devi, path);
777 		cmn_err(CE_CONT, "?Not enough PCI resources to "
778 		    "configure: %s\n", path);
779 
780 		kmem_free(path, MAXPATHLEN);
781 		rv = PCICFG_FAILURE;
782 	}
783 
784 	return (rv);
785 }
786 
787 /*
788  * configure the child nodes of ntbridge. new_device points to ntbridge itself
789  */
790 /*ARGSUSED*/
791 static int
792 pcicfg_configure_ntbridge(dev_info_t *new_device, uint_t bus, uint_t device)
793 {
794 	int bus_range[2], rc = PCICFG_FAILURE, rc1, max_devs = 0;
795 	int			devno;
796 	dev_info_t		*new_ntbridgechild;
797 	ddi_acc_handle_t	config_handle;
798 	uint16_t		vid;
799 	uint64_t		next_bus;
800 	uint64_t		blen;
801 	ndi_ra_request_t	req;
802 	uint8_t			pcie_device_type = 0;
803 
804 	/*
805 	 * If we need to do indirect config, lets create a property here
806 	 * to let the child conf map routine know that it has to
807 	 * go through the DDI calls, and not assume the devices are
808 	 * mapped directly under the host.
809 	 */
810 	if ((rc = ndi_prop_update_int(DDI_DEV_T_NONE, new_device,
811 	    PCI_DEV_CONF_MAP_PROP, (int)DDI_SUCCESS)) != DDI_SUCCESS) {
812 		DEBUG0("Cannot create indirect conf map property.\n");
813 		return ((int)PCICFG_FAILURE);
814 	}
815 
816 	if (pci_config_setup(new_device, &config_handle) != DDI_SUCCESS)
817 		return (PCICFG_FAILURE);
818 	/* check if we are PCIe device */
819 	if (pcicfg_pcie_device_type(new_device, config_handle) == DDI_SUCCESS) {
820 		DEBUG0("PCIe device detected\n");
821 		pcie_device_type = 1;
822 	}
823 	pci_config_teardown(&config_handle);
824 	/* create Bus node properties for ntbridge. */
825 	if (pcicfg_set_busnode_props(new_device, pcie_device_type)
826 	    != PCICFG_SUCCESS) {
827 		DEBUG0("Failed to set busnode props\n");
828 		return (rc);
829 	}
830 
831 	/* For now: Lets only support one layer of child */
832 	bzero((caddr_t)&req, sizeof (ndi_ra_request_t));
833 	req.ra_len = 1;
834 	if (ndi_ra_alloc(ddi_get_parent(new_device), &req, &next_bus, &blen,
835 	    NDI_RA_TYPE_PCI_BUSNUM, NDI_RA_PASS) != NDI_SUCCESS) {
836 		DEBUG0("ntbridge: Failed to get a bus number\n");
837 		return (PCICFG_NORESRC);
838 	}
839 
840 	DEBUG1("ntbridge bus range start  ->[%d]\n", next_bus);
841 
842 	/*
843 	 * Following will change, as we detect more bridges
844 	 * on the way.
845 	 */
846 	bus_range[0] = (int)next_bus;
847 	bus_range[1] = (int)next_bus;
848 
849 	if (ndi_prop_update_int_array(DDI_DEV_T_NONE, new_device, "bus-range",
850 	    bus_range, 2) != DDI_SUCCESS) {
851 		DEBUG0("Cannot set ntbridge bus-range property");
852 		return (rc);
853 	}
854 
855 	/*
856 	 * The other interface (away from the host) will be
857 	 * initialized by the nexus driver when it loads.
858 	 * We just have to set the registers and the nexus driver
859 	 * figures out the rest.
860 	 */
861 
862 	/*
863 	 * finally, lets load and attach the driver
864 	 * before configuring children of ntbridge.
865 	 */
866 	rc = ndi_devi_online(new_device, NDI_ONLINE_ATTACH|NDI_CONFIG);
867 	if (rc != NDI_SUCCESS) {
868 		cmn_err(CE_WARN,
869 		"pcicfg: Fail:cant load nontransparent bridgd driver..\n");
870 		rc = PCICFG_FAILURE;
871 		return (rc);
872 	}
873 	DEBUG0("pcicfg: Success loading nontransparent bridge nexus driver..");
874 
875 	/* Now set aside pci resource allocation requests for our children */
876 	if (pcicfg_ntbridge_allocate_resources(new_device) != PCICFG_SUCCESS) {
877 		max_devs = 0;
878 		rc = PCICFG_FAILURE;
879 	} else
880 		max_devs = PCI_MAX_DEVICES;
881 
882 	/* Probe devices on 2nd bus */
883 	rc = PCICFG_SUCCESS;
884 	for (devno = pcicfg_start_devno; devno < max_devs; devno++) {
885 
886 		ndi_devi_alloc_sleep(new_device, DEVI_PSEUDO_NEXNAME,
887 		    (pnode_t)DEVI_SID_NODEID, &new_ntbridgechild);
888 
889 		if (pcicfg_add_config_reg(new_ntbridgechild, next_bus, devno, 0)
890 		    != DDI_PROP_SUCCESS) {
891 			cmn_err(CE_WARN,
892 			    "Failed to add conf reg for ntbridge child.\n");
893 			(void) ndi_devi_free(new_ntbridgechild);
894 			rc = PCICFG_FAILURE;
895 			break;
896 		}
897 
898 		if (pci_config_setup(new_ntbridgechild, &config_handle)
899 		    != DDI_SUCCESS) {
900 			cmn_err(CE_WARN,
901 			    "Cannot map ntbridge child %x\n", devno);
902 			(void) ndi_devi_free(new_ntbridgechild);
903 			rc = PCICFG_FAILURE;
904 			break;
905 		}
906 
907 		/*
908 		 * See if there is any PCI HW at this location
909 		 * by reading the Vendor ID.  If it returns with 0xffff
910 		 * then there is no hardware at this location.
911 		 */
912 		vid = pci_config_get16(config_handle, PCI_CONF_VENID);
913 
914 		pci_config_teardown(&config_handle);
915 		(void) ndi_devi_free(new_ntbridgechild);
916 		if (vid	== 0xffff)
917 			continue;
918 
919 		/* Lets fake attachments points for each child, */
920 		rc = pcicfg_configure(new_device, devno, PCICFG_ALL_FUNC, 0);
921 		if (rc != PCICFG_SUCCESS) {
922 			int old_dev = pcicfg_start_devno;
923 
924 			cmn_err(CE_WARN,
925 			    "Error configuring ntbridge child dev=%d\n", devno);
926 
927 			while (old_dev != devno) {
928 				if (pcicfg_ntbridge_unconfigure_child(
929 				    new_device, old_dev) == PCICFG_FAILURE)
930 					cmn_err(CE_WARN, "Unconfig Error "
931 					    "ntbridge child dev=%d\n", old_dev);
932 				old_dev++;
933 			}
934 			break;
935 		}
936 	} /* devno loop */
937 	DEBUG1("ntbridge: finish probing 2nd bus, rc=%d\n", rc);
938 
939 	if (rc == PCICFG_SUCCESS)
940 		rc = pcicfg_ntbridge_configure_done(new_device);
941 	else {
942 		pcicfg_phdl_t *entry = pcicfg_find_phdl(new_device);
943 		uint_t			*bus;
944 		int			k;
945 
946 		if (ddi_getlongprop(DDI_DEV_T_ANY, new_device,
947 		    DDI_PROP_DONTPASS, "bus-range", (caddr_t)&bus, &k)
948 		    != DDI_PROP_SUCCESS) {
949 			DEBUG0("Failed to read bus-range property\n");
950 			rc = PCICFG_FAILURE;
951 			return (rc);
952 		}
953 
954 		DEBUG2("Need to free bus [%d] range [%d]\n",
955 		    bus[0], bus[1] - bus[0] + 1);
956 
957 		if (ndi_ra_free(ddi_get_parent(new_device), (uint64_t)bus[0],
958 		    (uint64_t)(bus[1] - bus[0] + 1), NDI_RA_TYPE_PCI_BUSNUM,
959 		    NDI_RA_PASS) != NDI_SUCCESS) {
960 			DEBUG0("Failed to free a bus number\n");
961 			rc = PCICFG_FAILURE;
962 			kmem_free(bus, k);
963 			return (rc);
964 		}
965 
966 		/*
967 		 * Since no memory allocations are done for non transparent
968 		 * bridges (but instead we just set the handle with the
969 		 * already allocated memory, we just need to reset the
970 		 * following values before calling the destroy_phdl()
971 		 * function next, otherwise the it will try to free
972 		 * memory allocated as in case of a transparent bridge.
973 		 */
974 		entry->memory_len = 0;
975 		entry->pf_memory_len = 0;
976 		entry->io_len = 0;
977 		kmem_free(bus, k);
978 		/* the following will free hole data. */
979 		(void) pcicfg_destroy_phdl(new_device);
980 	}
981 
982 	/*
983 	 * Unload driver just in case child configure failed!
984 	 */
985 	rc1 = ndi_devi_offline(new_device, 0);
986 	DEBUG1("pcicfg: now unloading the ntbridge driver. rc1=%d\n", rc1);
987 	if (rc1 != NDI_SUCCESS) {
988 		cmn_err(CE_WARN,
989 		"pcicfg: cant unload ntbridge driver..children.\n");
990 		rc = PCICFG_FAILURE;
991 	}
992 
993 	return (rc);
994 }
995 
996 static int
997 pcicfg_ntbridge_allocate_resources(dev_info_t *dip)
998 {
999 	pcicfg_phdl_t		*phdl;
1000 	ndi_ra_request_t	*mem_request;
1001 	ndi_ra_request_t	*pf_mem_request;
1002 	ndi_ra_request_t	*io_request;
1003 	uint64_t		boundbase, boundlen;
1004 
1005 	phdl = pcicfg_find_phdl(dip);
1006 	ASSERT(phdl);
1007 
1008 	mem_request = &phdl->mem_req;
1009 	pf_mem_request = &phdl->pf_mem_req;
1010 	io_request  = &phdl->io_req;
1011 
1012 	phdl->error = PCICFG_SUCCESS;
1013 
1014 	/* Set Memory space handle for ntbridge */
1015 	if (pcicfg_get_ntbridge_child_range(dip, &boundbase, &boundlen,
1016 	    PCI_BASE_SPACE_MEM) != DDI_SUCCESS) {
1017 		cmn_err(CE_WARN,
1018 		    "ntbridge: Mem resource information failure\n");
1019 		phdl->memory_len  = 0;
1020 		return (PCICFG_FAILURE);
1021 	}
1022 	mem_request->ra_boundbase = boundbase;
1023 	mem_request->ra_boundlen = boundbase + boundlen;
1024 	mem_request->ra_len = boundlen;
1025 	mem_request->ra_align_mask =
1026 	    PCICFG_MEMGRAN - 1; /* 1M alignment on memory space */
1027 	mem_request->ra_flags |= NDI_RA_ALLOC_BOUNDED;
1028 
1029 	/*
1030 	 * mem_request->ra_len =
1031 	 * PCICFG_ROUND_UP(mem_request->ra_len, PCICFG_MEMGRAN);
1032 	 */
1033 
1034 	phdl->memory_base = phdl->memory_last = boundbase;
1035 	phdl->memory_len  = boundlen;
1036 	phdl->mem_hole.start = phdl->memory_base;
1037 	phdl->mem_hole.len = mem_request->ra_len;
1038 	phdl->mem_hole.next = (hole_t *)NULL;
1039 
1040 	DEBUG2("Connector requested [0x%llx], needs [0x%llx] bytes of memory\n",
1041 	    boundlen, mem_request->ra_len);
1042 
1043 	/* Set IO space handle for ntbridge */
1044 	if (pcicfg_get_ntbridge_child_range(dip, &boundbase, &boundlen,
1045 	    PCI_BASE_SPACE_IO) != DDI_SUCCESS) {
1046 		cmn_err(CE_WARN, "ntbridge: IO resource information failure\n");
1047 		phdl->io_len  = 0;
1048 		return (PCICFG_FAILURE);
1049 	}
1050 	io_request->ra_len = boundlen;
1051 	io_request->ra_align_mask =
1052 	    PCICFG_IOGRAN - 1;   /* 4K alignment on I/O space */
1053 	io_request->ra_boundbase = boundbase;
1054 	io_request->ra_boundlen = boundbase + boundlen;
1055 	io_request->ra_flags |= NDI_RA_ALLOC_BOUNDED;
1056 
1057 	/*
1058 	 * io_request->ra_len =
1059 	 * PCICFG_ROUND_UP(io_request->ra_len, PCICFG_IOGRAN);
1060 	 */
1061 
1062 	phdl->io_base = phdl->io_last = (uint32_t)boundbase;
1063 	phdl->io_len  = (uint32_t)boundlen;
1064 	phdl->io_hole.start = phdl->io_base;
1065 	phdl->io_hole.len = io_request->ra_len;
1066 	phdl->io_hole.next = (hole_t *)NULL;
1067 
1068 	DEBUG2("Connector requested [0x%llx], needs [0x%llx] bytes of IO\n",
1069 	    boundlen, io_request->ra_len);
1070 
1071 	/* Set Prefetchable Memory space handle for ntbridge */
1072 	if (pcicfg_get_ntbridge_child_range(dip, &boundbase, &boundlen,
1073 	    PCI_BASE_SPACE_MEM | PCI_BASE_PREF_M) != DDI_SUCCESS) {
1074 		cmn_err(CE_WARN,
1075 		    "ntbridge: PF Mem resource information failure\n");
1076 		phdl->pf_memory_len  = 0;
1077 		return (PCICFG_FAILURE);
1078 	}
1079 	pf_mem_request->ra_boundbase = boundbase;
1080 	pf_mem_request->ra_boundlen = boundbase + boundlen;
1081 	pf_mem_request->ra_len = boundlen;
1082 	pf_mem_request->ra_align_mask =
1083 	    PCICFG_MEMGRAN - 1; /* 1M alignment on memory space */
1084 	pf_mem_request->ra_flags |= NDI_RA_ALLOC_BOUNDED;
1085 
1086 	/*
1087 	 * pf_mem_request->ra_len =
1088 	 * PCICFG_ROUND_UP(pf_mem_request->ra_len, PCICFG_MEMGRAN);
1089 	 */
1090 
1091 	phdl->pf_memory_base = phdl->pf_memory_last = boundbase;
1092 	phdl->pf_memory_len  = boundlen;
1093 	phdl->pf_mem_hole.start = phdl->pf_memory_base;
1094 	phdl->pf_mem_hole.len = pf_mem_request->ra_len;
1095 	phdl->pf_mem_hole.next = (hole_t *)NULL;
1096 
1097 	DEBUG2("Connector requested [0x%llx], needs [0x%llx] bytes of PF "
1098 	    "memory\n", boundlen, pf_mem_request->ra_len);
1099 
1100 	DEBUG2("MEMORY BASE = [0x%lx] length [0x%lx]\n",
1101 	    phdl->memory_base, phdl->memory_len);
1102 	DEBUG2("IO     BASE = [0x%x] length [0x%x]\n",
1103 	    phdl->io_base, phdl->io_len);
1104 	DEBUG2("PF MEMORY BASE = [0x%lx] length [0x%lx]\n",
1105 	    phdl->pf_memory_base, phdl->pf_memory_len);
1106 
1107 	return (PCICFG_SUCCESS);
1108 }
1109 
1110 static int
1111 pcicfg_ntbridge_configure_done(dev_info_t *dip)
1112 {
1113 	ppb_ranges_t range[PCICFG_RANGE_LEN];
1114 	pcicfg_phdl_t		*entry;
1115 	uint_t			len;
1116 	pci_bus_range_t		bus_range;
1117 	int			new_bus_range[2];
1118 
1119 	DEBUG1("Configuring children for %p\n", dip);
1120 
1121 	entry = pcicfg_find_phdl(dip);
1122 	ASSERT(entry);
1123 
1124 	bzero((caddr_t)range, sizeof (ppb_ranges_t) * PCICFG_RANGE_LEN);
1125 	range[1].child_high = range[1].parent_high |=
1126 	    (PCI_REG_REL_M | PCI_ADDR_MEM32);
1127 	range[1].child_low = range[1].parent_low = (uint32_t)entry->memory_base;
1128 
1129 	range[0].child_high = range[0].parent_high |=
1130 	    (PCI_REG_REL_M | PCI_ADDR_IO);
1131 	range[0].child_low = range[0].parent_low = (uint32_t)entry->io_base;
1132 
1133 	range[2].child_high = range[2].parent_high |=
1134 	    (PCI_REG_REL_M | PCI_ADDR_MEM32 | PCI_REG_PF_M);
1135 	range[2].child_low = range[2].parent_low =
1136 	    (uint32_t)entry->pf_memory_base;
1137 
1138 	len = sizeof (pci_bus_range_t);
1139 	if (ddi_getlongprop_buf(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
1140 	    "bus-range", (caddr_t)&bus_range, (int *)&len) != DDI_SUCCESS) {
1141 		DEBUG0("no bus-range property\n");
1142 		return (PCICFG_FAILURE);
1143 	}
1144 
1145 	new_bus_range[0] = bus_range.lo;	/* primary bus number */
1146 	if (entry->highest_bus) {	/* secondary bus number */
1147 		if (entry->highest_bus < bus_range.lo) {
1148 			cmn_err(CE_WARN,
1149 			    "ntbridge bus range invalid !(%d,%d)\n",
1150 			    bus_range.lo, entry->highest_bus);
1151 			new_bus_range[1] = bus_range.lo + entry->highest_bus;
1152 		}
1153 		else
1154 			new_bus_range[1] = entry->highest_bus;
1155 	}
1156 	else
1157 		new_bus_range[1] = bus_range.hi;
1158 
1159 	DEBUG2("ntbridge: bus range lo=%x, hi=%x\n", new_bus_range[0],
1160 	    new_bus_range[1]);
1161 
1162 	if (ndi_prop_update_int_array(DDI_DEV_T_NONE, dip, "bus-range",
1163 	    new_bus_range, 2) != DDI_SUCCESS) {
1164 		DEBUG0("Failed to set bus-range property");
1165 		entry->error = PCICFG_FAILURE;
1166 		return (PCICFG_FAILURE);
1167 	}
1168 
1169 #ifdef DEBUG
1170 	{
1171 		uint64_t	unused;
1172 		unused = pcicfg_unused_space(&entry->io_hole, &len);
1173 		DEBUG2("ntbridge: Unused IO space %llx bytes over %d holes\n",
1174 		    unused, len);
1175 	}
1176 #endif
1177 
1178 	range[0].size_low = entry->io_len;
1179 	if (pcicfg_update_ranges_prop(dip, &range[0])) {
1180 		DEBUG0("Failed to update ranges (i/o)\n");
1181 		entry->error = PCICFG_FAILURE;
1182 		return (PCICFG_FAILURE);
1183 	}
1184 
1185 #ifdef DEBUG
1186 	{
1187 		uint64_t	unused;
1188 		unused = pcicfg_unused_space(&entry->mem_hole, &len);
1189 		DEBUG2("ntbridge: Unused Mem space %llx bytes over %d holes\n",
1190 		    unused, len);
1191 	}
1192 #endif
1193 
1194 	range[1].size_low = entry->memory_len;
1195 	if (pcicfg_update_ranges_prop(dip, &range[1])) {
1196 		DEBUG0("Failed to update ranges (memory)\n");
1197 		entry->error = PCICFG_FAILURE;
1198 		return (PCICFG_FAILURE);
1199 	}
1200 
1201 #ifdef DEBUG
1202 	{
1203 		uint64_t	unused;
1204 		unused = pcicfg_unused_space(&entry->pf_mem_hole, &len);
1205 		DEBUG2("ntbridge: Unused PF Mem space %llx bytes over"
1206 		    " %d holes\n", unused, len);
1207 	}
1208 #endif
1209 
1210 	range[2].size_low = entry->pf_memory_len;
1211 	if (pcicfg_update_ranges_prop(dip, &range[2])) {
1212 		DEBUG0("Failed to update ranges (PF memory)\n");
1213 		entry->error = PCICFG_FAILURE;
1214 		return (PCICFG_FAILURE);
1215 	}
1216 
1217 	return (PCICFG_SUCCESS);
1218 }
1219 
1220 static int
1221 pcicfg_ntbridge_program_child(dev_info_t *dip)
1222 {
1223 	pcicfg_phdl_t	*entry;
1224 	int		rc = PCICFG_SUCCESS;
1225 	dev_info_t	*anode = dip;
1226 
1227 	/* Find the Hotplug Connection (CN) node */
1228 	while ((anode != NULL) &&
1229 	    (strcmp(ddi_binding_name(anode), "hp_attachment") != 0)) {
1230 		anode = ddi_get_parent(anode);
1231 	}
1232 
1233 	if (anode == NULL) {
1234 		DEBUG0("ntbridge child tree not in PROBE state\n");
1235 		return (PCICFG_FAILURE);
1236 	}
1237 	entry = pcicfg_find_phdl(ddi_get_parent(anode));
1238 	ASSERT(entry);
1239 
1240 	if (pcicfg_bridge_assign(dip, entry) == DDI_WALK_TERMINATE) {
1241 		cmn_err(CE_WARN,
1242 		    "ntbridge: Error assigning range for child %s\n",
1243 		    ddi_get_name(dip));
1244 		rc = PCICFG_FAILURE;
1245 	}
1246 	return (rc);
1247 }
1248 
1249 static int
1250 pcicfg_ntbridge_unconfigure_child(dev_info_t *new_device, uint_t devno)
1251 {
1252 
1253 	dev_info_t	*new_ntbridgechild;
1254 	int		len, bus;
1255 	uint16_t	vid;
1256 	ddi_acc_handle_t	config_handle;
1257 	pci_bus_range_t pci_bus_range;
1258 
1259 	len = sizeof (pci_bus_range_t);
1260 	if (ddi_getlongprop_buf(DDI_DEV_T_ANY, new_device, DDI_PROP_DONTPASS,
1261 	    "bus-range", (caddr_t)&pci_bus_range, &len) != DDI_SUCCESS) {
1262 		DEBUG0("no bus-range property\n");
1263 		return (PCICFG_FAILURE);
1264 	}
1265 
1266 	bus = pci_bus_range.lo; /* primary bus number of this bus node */
1267 
1268 	ndi_devi_alloc_sleep(new_device, DEVI_PSEUDO_NEXNAME,
1269 	    (pnode_t)DEVI_SID_NODEID, &new_ntbridgechild);
1270 
1271 	if (pcicfg_add_config_reg(new_ntbridgechild, bus, devno, 0)
1272 	    != DDI_PROP_SUCCESS) {
1273 		cmn_err(CE_WARN, "Unconfigure: Failed to add conf reg prop for "
1274 		    "ntbridge child.\n");
1275 		(void) ndi_devi_free(new_ntbridgechild);
1276 		return (PCICFG_FAILURE);
1277 	}
1278 
1279 	if (pci_config_setup(new_ntbridgechild, &config_handle)
1280 	    != DDI_SUCCESS) {
1281 		cmn_err(CE_WARN, "pcicfg: Cannot map ntbridge child %x\n",
1282 		    devno);
1283 		(void) ndi_devi_free(new_ntbridgechild);
1284 		return (PCICFG_FAILURE);
1285 	}
1286 
1287 	/*
1288 	 * See if there is any PCI HW at this location
1289 	 * by reading the Vendor ID.  If it returns with 0xffff
1290 	 * then there is no hardware at this location.
1291 	 */
1292 	vid = pci_config_get16(config_handle, PCI_CONF_VENID);
1293 
1294 	pci_config_teardown(&config_handle);
1295 	(void) ndi_devi_free(new_ntbridgechild);
1296 	if (vid	== 0xffff)
1297 		return (PCICFG_NODEVICE);
1298 
1299 	return (pcicfg_unconfigure(new_device, devno, PCICFG_ALL_FUNC, 0));
1300 }
1301 
1302 static uint_t
1303 pcicfg_ntbridge_unconfigure(dev_info_t *dip)
1304 {
1305 	pcicfg_phdl_t *entry = pcicfg_find_phdl(dip);
1306 	uint_t			*bus;
1307 	int			k, rc = DDI_FAILURE;
1308 
1309 	if (ddi_getlongprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, "bus-range",
1310 	    (caddr_t)&bus, &k) != DDI_PROP_SUCCESS) {
1311 		DEBUG0("ntbridge: Failed to read bus-range property\n");
1312 		return (rc);
1313 	}
1314 
1315 	DEBUG2("ntbridge: Need to free bus [%d] range [%d]\n",
1316 	    bus[0], bus[1] - bus[0] + 1);
1317 
1318 	if (ndi_ra_free(ddi_get_parent(dip), (uint64_t)bus[0],
1319 	    (uint64_t)(bus[1] - bus[0] + 1),
1320 	    NDI_RA_TYPE_PCI_BUSNUM, NDI_RA_PASS) != NDI_SUCCESS) {
1321 		DEBUG0("ntbridge: Failed to free a bus number\n");
1322 		kmem_free(bus, k);
1323 		return (rc);
1324 	}
1325 
1326 	/*
1327 	 * Since our resources will be freed at the parent level,
1328 	 * just reset these values.
1329 	 */
1330 	entry->memory_len = 0;
1331 	entry->io_len = 0;
1332 	entry->pf_memory_len = 0;
1333 
1334 	kmem_free(bus, k);
1335 
1336 	/* the following will also free hole data. */
1337 	return (pcicfg_destroy_phdl(dip));
1338 
1339 }
1340 
1341 static int
1342 pcicfg_is_ntbridge(dev_info_t *dip)
1343 {
1344 	ddi_acc_handle_t	config_handle;
1345 	uint8_t		class, subclass;
1346 	int		rc = DDI_SUCCESS;
1347 
1348 	if (pci_config_setup(dip, &config_handle) != DDI_SUCCESS) {
1349 		cmn_err(CE_WARN,
1350 		    "pcicfg: cannot map config space, to get map type\n");
1351 		return (DDI_FAILURE);
1352 	}
1353 	class = pci_config_get8(config_handle, PCI_CONF_BASCLASS);
1354 	subclass = pci_config_get8(config_handle, PCI_CONF_SUBCLASS);
1355 
1356 	/* check for class=6, subclass=9, for non transparent bridges.  */
1357 	if ((class != PCI_CLASS_BRIDGE) || (subclass != PCI_BRIDGE_STBRIDGE))
1358 		rc = DDI_FAILURE;
1359 
1360 	DEBUG3("pcicfg: checking device %x,%x for indirect map. rc=%d\n",
1361 	    pci_config_get16(config_handle, PCI_CONF_VENID),
1362 	    pci_config_get16(config_handle, PCI_CONF_DEVID),
1363 	    rc);
1364 	pci_config_teardown(&config_handle);
1365 	return (rc);
1366 }
1367 
1368 static uint_t
1369 pcicfg_ntbridge_child(dev_info_t *dip)
1370 {
1371 	int		len, val, rc = DDI_FAILURE;
1372 	dev_info_t	*anode = dip;
1373 
1374 	/*
1375 	 * Find the Hotplug Connection (CN) node
1376 	 */
1377 	while ((anode != NULL) && (strcmp(ddi_binding_name(anode),
1378 	    "hp_attachment") != 0)) {
1379 		anode = ddi_get_parent(anode);
1380 	}
1381 
1382 	if (anode == NULL) {
1383 		DEBUG0("ntbridge child tree not in PROBE state\n");
1384 		return (rc);
1385 	}
1386 	len = sizeof (int);
1387 	if (ddi_getlongprop_buf(DDI_DEV_T_ANY, ddi_get_parent(anode),
1388 	    DDI_PROP_DONTPASS, PCI_DEV_CONF_MAP_PROP, (caddr_t)&val, &len)
1389 	    != DDI_SUCCESS) {
1390 
1391 		DEBUG1("ntbridge child: no \"%s\" property\n",
1392 		    PCI_DEV_CONF_MAP_PROP);
1393 		return (rc);
1394 	}
1395 	DEBUG0("ntbridge child: success\n");
1396 	return (DDI_SUCCESS);
1397 }
1398 
1399 static uint_t
1400 pcicfg_get_ntbridge_child_range(dev_info_t *dip, uint64_t *boundbase,
1401     uint64_t *boundlen, uint_t space_type)
1402 {
1403 	int		length, found = DDI_FAILURE, acount, i, ibridge;
1404 	pci_regspec_t	*assigned;
1405 
1406 	if ((ibridge = pcicfg_is_ntbridge(dip)) == DDI_FAILURE)
1407 		return (found);
1408 
1409 	if (ddi_getlongprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
1410 	    "assigned-addresses", (caddr_t)&assigned, &length)
1411 	    != DDI_PROP_SUCCESS) {
1412 		DEBUG1("Failed to get assigned-addresses property %llx\n", dip);
1413 		return (found);
1414 	}
1415 	DEBUG1("pcicfg: ntbridge child range: dip = %s\n",
1416 	    ddi_driver_name(dip));
1417 
1418 	acount = length / sizeof (pci_regspec_t);
1419 
1420 	for (i = 0; i < acount; i++) {
1421 		if ((PCI_REG_REG_G(assigned[i].pci_phys_hi) ==
1422 		    pcicfg_indirect_map_devs[ibridge].mem_range_bar_offset) &&
1423 		    (space_type == PCI_BASE_SPACE_MEM)) {
1424 			found = DDI_SUCCESS;
1425 			break;
1426 		} else if ((PCI_REG_REG_G(assigned[i].pci_phys_hi) ==
1427 		    pcicfg_indirect_map_devs[ibridge].io_range_bar_offset) &&
1428 		    (space_type == PCI_BASE_SPACE_IO)) {
1429 			found = DDI_SUCCESS;
1430 			break;
1431 		} else if ((PCI_REG_REG_G(assigned[i].pci_phys_hi) ==
1432 		    pcicfg_indirect_map_devs[ibridge].
1433 		    prefetch_mem_range_bar_offset) &&
1434 		    (space_type == (PCI_BASE_SPACE_MEM |
1435 		    PCI_BASE_PREF_M))) {
1436 			found = DDI_SUCCESS;
1437 			break;
1438 		}
1439 	}
1440 	DEBUG3("pcicfg: ntbridge child range: space=%x, base=%lx, len=%lx\n",
1441 	    space_type, assigned[i].pci_phys_low, assigned[i].pci_size_low);
1442 
1443 	if (found == DDI_SUCCESS)  {
1444 		*boundbase = assigned[i].pci_phys_low;
1445 		*boundlen = assigned[i].pci_size_low;
1446 	}
1447 
1448 	kmem_free(assigned, length);
1449 	return (found);
1450 }
1451 
1452 /*
1453  * This will turn  resources allocated by pcicfg_configure()
1454  * and remove the device tree from the Hotplug Connection (CN)
1455  * and below.  The routine assumes the devices have their
1456  * drivers detached.
1457  */
1458 int
1459 pcicfg_unconfigure(dev_info_t *devi, uint_t device, uint_t function,
1460     pcicfg_flags_t flags)
1461 {
1462 	dev_info_t *child_dip;
1463 	int func;
1464 	int i;
1465 	int max_function, trans_device;
1466 	int circ;
1467 	boolean_t is_pcie;
1468 
1469 	if (pcie_ari_is_enabled(devi) == PCIE_ARI_FORW_ENABLED)
1470 		max_function = PCICFG_MAX_ARI_FUNCTION;
1471 	else
1472 		max_function = PCI_MAX_FUNCTIONS;
1473 
1474 	/*
1475 	 * Cycle through devices to make sure none are busy.
1476 	 * If a single device is busy fail the whole unconfigure.
1477 	 */
1478 	is_pcie = is_pcie_fabric(devi);
1479 
1480 	ndi_devi_enter(devi, &circ);
1481 	for (func = 0; func < max_function; func++) {
1482 		if ((function != PCICFG_ALL_FUNC) && (function != func))
1483 			continue;
1484 
1485 		if (max_function == PCICFG_MAX_ARI_FUNCTION)
1486 			trans_device = func >> 3; /* ARI Device */
1487 		else
1488 			trans_device = device;
1489 
1490 		if ((child_dip = pcicfg_devi_find(devi, trans_device,
1491 		    func & 7)) == NULL)
1492 			continue;
1493 
1494 		if (ndi_devi_offline(child_dip, NDI_UNCONFIG) == NDI_SUCCESS)
1495 			continue;
1496 
1497 		/*
1498 		 * Device function is busy. Before returning we have to
1499 		 * put all functions back online which were taken
1500 		 * offline during the process.
1501 		 */
1502 		DEBUG2("Device [0x%x] function [0x%x] is busy\n",
1503 		    trans_device, func & 7);
1504 		/*
1505 		 * If we are only asked to offline one specific function,
1506 		 * and that fails, we just simply return.
1507 		 */
1508 		if (function != PCICFG_ALL_FUNC)
1509 			return (PCICFG_FAILURE);
1510 
1511 		for (i = 0; i < func; i++) {
1512 			if (max_function == PCICFG_MAX_ARI_FUNCTION)
1513 				trans_device = i >> 3;
1514 
1515 			if ((child_dip = pcicfg_devi_find(devi, trans_device,
1516 			    i & 7)) == NULL) {
1517 				DEBUG0("No more devices to put back "
1518 				    "on line!!\n");
1519 				/*
1520 				 * Made it through all functions
1521 				 */
1522 				continue;
1523 			}
1524 			if (ndi_devi_online(child_dip, NDI_CONFIG)
1525 			    != NDI_SUCCESS) {
1526 				DEBUG0("Failed to put back devices state\n");
1527 				goto fail;
1528 			}
1529 		}
1530 		goto fail;
1531 	}
1532 
1533 	/*
1534 	 * Now, tear down all devinfo nodes for this Connector.
1535 	 */
1536 	for (func = 0; func < max_function; func++) {
1537 		if ((function != PCICFG_ALL_FUNC) && (function != func))
1538 			continue;
1539 
1540 		if (max_function == PCICFG_MAX_ARI_FUNCTION)
1541 			trans_device = func >> 3; /* ARI Device */
1542 		else
1543 			trans_device = device;
1544 
1545 		if ((child_dip = pcicfg_devi_find(devi, trans_device, func & 7))
1546 		    == NULL) {
1547 			DEBUG2("No device at %x,%x\n", trans_device, func & 7);
1548 			continue;
1549 		}
1550 
1551 		DEBUG2("Tearing down device [0x%x] function [0x%x]\n",
1552 		    trans_device, func & 7);
1553 
1554 		if (pcicfg_is_ntbridge(child_dip) != DDI_FAILURE)
1555 			if (pcicfg_ntbridge_unconfigure(child_dip) !=
1556 			    PCICFG_SUCCESS) {
1557 				cmn_err(CE_WARN,
1558 				    "ntbridge: unconfigure failed\n");
1559 				goto fail;
1560 			}
1561 
1562 		if (pcicfg_teardown_device(child_dip, flags, is_pcie)
1563 		    != PCICFG_SUCCESS) {
1564 			DEBUG2("Failed to tear down device [0x%x]"
1565 			    "function [0x%x]\n", trans_device, func & 7);
1566 			goto fail;
1567 		}
1568 	}
1569 
1570 	if (pcie_ari_is_enabled(devi) == PCIE_ARI_FORW_ENABLED) {
1571 		(void) ddi_prop_remove(DDI_DEV_T_NONE, devi, "ari-enabled");
1572 		(void) pcie_ari_disable(devi);
1573 	}
1574 
1575 	ndi_devi_exit(devi, circ);
1576 	return (PCICFG_SUCCESS);
1577 
1578 fail:
1579 	ndi_devi_exit(devi, circ);
1580 	return (PCICFG_FAILURE);
1581 }
1582 
1583 static int
1584 pcicfg_teardown_device(dev_info_t *dip, pcicfg_flags_t flags, boolean_t is_pcie)
1585 {
1586 	ddi_acc_handle_t	handle;
1587 	int			ret;
1588 
1589 	/*
1590 	 * Free up resources associated with 'dip'
1591 	 */
1592 	if (pcicfg_free_resources(dip, flags) != PCICFG_SUCCESS) {
1593 		DEBUG0("Failed to free resources\n");
1594 		return (PCICFG_FAILURE);
1595 	}
1596 
1597 	/*
1598 	 * disable the device
1599 	 */
1600 
1601 	ret = pcicfg_config_setup(dip, &handle);
1602 	if (ret == PCICFG_SUCCESS) {
1603 		pcicfg_device_off(handle);
1604 		pcicfg_config_teardown(&handle);
1605 	} else if (ret != PCICFG_NODEVICE) {
1606 		/*
1607 		 * It is possible the device no longer exists -- for instance,
1608 		 * if the device has been pulled from a hotpluggable slot on the
1609 		 * system. In this case, do not fail the teardown, though there
1610 		 * is less to clean up.
1611 		 */
1612 		return (PCICFG_FAILURE);
1613 	}
1614 
1615 	if (is_pcie) {
1616 		/*
1617 		 * free pcie_bus_t for the sub-tree
1618 		 */
1619 		if (ddi_get_child(dip) != NULL)
1620 			pcie_fab_fini_bus(dip, PCIE_BUS_ALL);
1621 
1622 		pcie_fini_bus(dip, PCIE_BUS_ALL);
1623 	}
1624 
1625 	/*
1626 	 * The framework provides this routine which can
1627 	 * tear down a sub-tree.
1628 	 */
1629 	if (ndi_devi_offline(dip, NDI_DEVI_REMOVE) != NDI_SUCCESS) {
1630 		DEBUG0("Failed to offline and remove node\n");
1631 		return (PCICFG_FAILURE);
1632 	}
1633 
1634 	return (PCICFG_SUCCESS);
1635 }
1636 
1637 /*
1638  * BEGIN GENERIC SUPPORT ROUTINES
1639  */
1640 static pcicfg_phdl_t *
1641 pcicfg_find_phdl(dev_info_t *dip)
1642 {
1643 	pcicfg_phdl_t *entry;
1644 	mutex_enter(&pcicfg_list_mutex);
1645 	for (entry = pcicfg_phdl_list; entry != NULL; entry = entry->next) {
1646 		if (entry->dip == dip) {
1647 			mutex_exit(&pcicfg_list_mutex);
1648 			return (entry);
1649 		}
1650 	}
1651 	mutex_exit(&pcicfg_list_mutex);
1652 
1653 	/*
1654 	 * Did'nt find entry - create one
1655 	 */
1656 	return (pcicfg_create_phdl(dip));
1657 }
1658 
1659 static pcicfg_phdl_t *
1660 pcicfg_create_phdl(dev_info_t *dip)
1661 {
1662 	pcicfg_phdl_t *new;
1663 
1664 	new = (pcicfg_phdl_t *)kmem_zalloc(sizeof (pcicfg_phdl_t), KM_SLEEP);
1665 
1666 	new->dip = dip;
1667 	mutex_enter(&pcicfg_list_mutex);
1668 	new->next = pcicfg_phdl_list;
1669 	pcicfg_phdl_list = new;
1670 	mutex_exit(&pcicfg_list_mutex);
1671 
1672 	return (new);
1673 }
1674 
1675 static int
1676 pcicfg_destroy_phdl(dev_info_t *dip)
1677 {
1678 	pcicfg_phdl_t *entry;
1679 	pcicfg_phdl_t *follow = NULL;
1680 
1681 	mutex_enter(&pcicfg_list_mutex);
1682 	for (entry = pcicfg_phdl_list; entry != NULL; follow = entry,
1683 	    entry = entry->next) {
1684 		if (entry->dip == dip) {
1685 			if (entry == pcicfg_phdl_list) {
1686 				pcicfg_phdl_list = entry->next;
1687 			} else {
1688 				follow->next = entry->next;
1689 			}
1690 			/*
1691 			 * If this entry has any allocated memory
1692 			 * or IO space associated with it, that
1693 			 * must be freed up.
1694 			 */
1695 			if (entry->memory_len > 0) {
1696 				(void) ndi_ra_free(ddi_get_parent(dip),
1697 				    entry->memory_base, entry->memory_len,
1698 				    NDI_RA_TYPE_MEM, NDI_RA_PASS);
1699 			}
1700 			pcicfg_free_hole(&entry->mem_hole);
1701 
1702 			if (entry->io_len > 0) {
1703 				(void) ndi_ra_free(ddi_get_parent(dip),
1704 				    entry->io_base, entry->io_len,
1705 				    NDI_RA_TYPE_IO, NDI_RA_PASS);
1706 			}
1707 			pcicfg_free_hole(&entry->io_hole);
1708 
1709 			if (entry->pf_memory_len > 0) {
1710 				(void) ndi_ra_free(ddi_get_parent(dip),
1711 				    entry->pf_memory_base, entry->pf_memory_len,
1712 				    NDI_RA_TYPE_PCI_PREFETCH_MEM, NDI_RA_PASS);
1713 			}
1714 			pcicfg_free_hole(&entry->pf_mem_hole);
1715 
1716 			/*
1717 			 * Destroy this entry
1718 			 */
1719 			kmem_free((caddr_t)entry, sizeof (pcicfg_phdl_t));
1720 			mutex_exit(&pcicfg_list_mutex);
1721 			return (PCICFG_SUCCESS);
1722 		}
1723 	}
1724 	mutex_exit(&pcicfg_list_mutex);
1725 	/*
1726 	 * Did'nt find the entry
1727 	 */
1728 	return (PCICFG_FAILURE);
1729 }
1730 
1731 static int
1732 pcicfg_bridge_assign(dev_info_t *dip, void *hdl)
1733 {
1734 	ddi_acc_handle_t handle;
1735 	pci_regspec_t *reg;
1736 	int length;
1737 	int rcount;
1738 	int i;
1739 	int offset;
1740 	uint64_t mem_answer;
1741 	uint32_t io_answer;
1742 	int count;
1743 	uint8_t header_type;
1744 	ppb_ranges_t range[PCICFG_RANGE_LEN];
1745 	int bus_range[2];
1746 	uint64_t mem_residual;
1747 	uint64_t pf_mem_residual;
1748 	uint64_t io_residual;
1749 
1750 	pcicfg_phdl_t *entry = (pcicfg_phdl_t *)hdl;
1751 
1752 	DEBUG1("bridge assign: assigning addresses to %s\n", ddi_get_name(dip));
1753 
1754 	entry->error = PCICFG_SUCCESS;
1755 
1756 	if (entry == NULL) {
1757 		DEBUG0("Failed to get entry\n");
1758 		entry->error = PCICFG_FAILURE;
1759 		return (DDI_WALK_TERMINATE);
1760 	}
1761 
1762 	if (pcicfg_config_setup(dip, &handle) != DDI_SUCCESS) {
1763 		DEBUG0("Failed to map config space!\n");
1764 		entry->error = PCICFG_FAILURE;
1765 		return (DDI_WALK_TERMINATE);
1766 	}
1767 
1768 	header_type = pci_config_get8(handle, PCI_CONF_HEADER);
1769 
1770 	if ((header_type & PCI_HEADER_TYPE_M) == PCI_HEADER_PPB) {
1771 
1772 		bzero((caddr_t)range, sizeof (ppb_ranges_t) * PCICFG_RANGE_LEN);
1773 
1774 		(void) pcicfg_setup_bridge(entry, handle);
1775 
1776 		range[0].child_high = range[0].parent_high |=
1777 		    (PCI_REG_REL_M | PCI_ADDR_IO);
1778 		range[0].child_low = range[0].parent_low = entry->io_last;
1779 		range[1].child_high = range[1].parent_high |=
1780 		    (PCI_REG_REL_M | PCI_ADDR_MEM32);
1781 		range[1].child_low = range[1].parent_low =
1782 		    entry->memory_last;
1783 		range[2].child_high = range[2].parent_high |=
1784 		    (PCI_REG_REL_M | PCI_ADDR_MEM32 | PCI_REG_PF_M);
1785 		range[2].child_low = range[2].parent_low =
1786 		    entry->pf_memory_last;
1787 
1788 		ndi_devi_enter(dip, &count);
1789 		ddi_walk_devs(ddi_get_child(dip),
1790 		    pcicfg_bridge_assign, (void *)entry);
1791 		ndi_devi_exit(dip, count);
1792 
1793 		(void) pcicfg_update_bridge(entry, handle);
1794 
1795 		bus_range[0] = pci_config_get8(handle, PCI_BCNF_SECBUS);
1796 		bus_range[1] = pci_config_get8(handle, PCI_BCNF_SUBBUS);
1797 
1798 		if (ndi_prop_update_int_array(DDI_DEV_T_NONE, dip,
1799 		    "bus-range", bus_range, 2) != DDI_SUCCESS) {
1800 			DEBUG0("Failed to set bus-range property");
1801 			entry->error = PCICFG_FAILURE;
1802 			(void) pcicfg_config_teardown(&handle);
1803 			return (DDI_WALK_TERMINATE);
1804 		}
1805 
1806 		/*
1807 		 * Put back memory and I/O space not allocated
1808 		 * under the bridge.
1809 		 */
1810 		mem_residual = entry->memory_len -
1811 		    (entry->memory_last - entry->memory_base);
1812 		if (mem_residual > 0) {
1813 			(void) ndi_ra_free(ddi_get_parent(dip),
1814 			    entry->memory_last, mem_residual,
1815 			    NDI_RA_TYPE_MEM, NDI_RA_PASS);
1816 		}
1817 
1818 		io_residual = entry->io_len - (entry->io_last - entry->io_base);
1819 		if (io_residual > 0) {
1820 			(void) ndi_ra_free(ddi_get_parent(dip), entry->io_last,
1821 			    io_residual, NDI_RA_TYPE_IO, NDI_RA_PASS);
1822 		}
1823 
1824 		pf_mem_residual = entry->pf_memory_len -
1825 		    (entry->pf_memory_last - entry->pf_memory_base);
1826 		if (pf_mem_residual > 0) {
1827 			(void) ndi_ra_free(ddi_get_parent(dip),
1828 			    entry->pf_memory_last, pf_mem_residual,
1829 			    NDI_RA_TYPE_PCI_PREFETCH_MEM, NDI_RA_PASS);
1830 		}
1831 
1832 		if (entry->io_len > 0) {
1833 			range[0].size_low = entry->io_last - entry->io_base;
1834 			if (pcicfg_update_ranges_prop(dip, &range[0])) {
1835 				DEBUG0("Failed to update ranges (i/o)\n");
1836 				entry->error = PCICFG_FAILURE;
1837 				(void) pcicfg_config_teardown(&handle);
1838 				return (DDI_WALK_TERMINATE);
1839 			}
1840 		}
1841 		if (entry->memory_len > 0) {
1842 			range[1].size_low =
1843 			    entry->memory_last - entry->memory_base;
1844 			if (pcicfg_update_ranges_prop(dip, &range[1])) {
1845 				DEBUG0("Failed to update ranges (memory)\n");
1846 				entry->error = PCICFG_FAILURE;
1847 				(void) pcicfg_config_teardown(&handle);
1848 				return (DDI_WALK_TERMINATE);
1849 			}
1850 		}
1851 		if (entry->pf_memory_len > 0) {
1852 			range[2].size_low =
1853 			    entry->pf_memory_last - entry->pf_memory_base;
1854 			if (pcicfg_update_ranges_prop(dip, &range[2])) {
1855 				DEBUG0("Failed to update ranges (PF memory)\n");
1856 				entry->error = PCICFG_FAILURE;
1857 				(void) pcicfg_config_teardown(&handle);
1858 				return (DDI_WALK_TERMINATE);
1859 			}
1860 		}
1861 
1862 		(void) pcicfg_device_on(handle);
1863 
1864 		PCICFG_DUMP_BRIDGE_CONFIG(handle);
1865 
1866 		(void) pcicfg_config_teardown(&handle);
1867 
1868 		return (DDI_WALK_PRUNECHILD);
1869 	}
1870 
1871 	/*
1872 	 * If there is an interrupt pin set program
1873 	 * interrupt line with default values.
1874 	 */
1875 	if (pci_config_get8(handle, PCI_CONF_IPIN)) {
1876 		pci_config_put8(handle, PCI_CONF_ILINE, 0xf);
1877 	}
1878 
1879 	/*
1880 	 * A single device (under a bridge).
1881 	 * For each "reg" property with a length, allocate memory
1882 	 * and program the base registers.
1883 	 */
1884 	if (ddi_getlongprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, "reg",
1885 	    (caddr_t)&reg, &length) != DDI_PROP_SUCCESS) {
1886 		DEBUG0("Failed to read reg property\n");
1887 		entry->error = PCICFG_FAILURE;
1888 		(void) pcicfg_config_teardown(&handle);
1889 		return (DDI_WALK_TERMINATE);
1890 	}
1891 
1892 	rcount = length / sizeof (pci_regspec_t);
1893 	offset = PCI_CONF_BASE0;
1894 	for (i = 0; i < rcount; i++) {
1895 		if ((reg[i].pci_size_low != 0) || (reg[i].pci_size_hi != 0)) {
1896 
1897 			offset = PCI_REG_REG_G(reg[i].pci_phys_hi);
1898 
1899 			switch (PCI_REG_ADDR_G(reg[i].pci_phys_hi)) {
1900 			case PCI_REG_ADDR_G(PCI_ADDR_MEM64):
1901 
1902 				if (reg[i].pci_phys_hi & PCI_REG_PF_M) {
1903 					/* allocate prefetchable memory */
1904 					pcicfg_get_pf_mem(entry,
1905 					    reg[i].pci_size_low, &mem_answer);
1906 				} else { /* get non prefetchable memory */
1907 					pcicfg_get_mem(entry,
1908 					    reg[i].pci_size_low, &mem_answer);
1909 				}
1910 				pci_config_put64(handle, offset, mem_answer);
1911 				DEBUG2("REGISTER off %x (64)LO ----> [0x%x]\n",
1912 				    offset, pci_config_get32(handle, offset));
1913 				DEBUG2("REGISTER off %x (64)HI ----> [0x%x]\n",
1914 				    offset + 4,
1915 				    pci_config_get32(handle, offset + 4));
1916 
1917 				reg[i].pci_phys_hi |= PCI_REG_REL_M;
1918 				reg[i].pci_phys_low = PCICFG_LOADDR(mem_answer);
1919 				reg[i].pci_phys_mid = PCICFG_HIADDR(mem_answer);
1920 				break;
1921 
1922 			case PCI_REG_ADDR_G(PCI_ADDR_MEM32):
1923 				if (reg[i].pci_phys_hi & PCI_REG_PF_M) {
1924 					/* allocate prefetchable memory */
1925 					pcicfg_get_pf_mem(entry,
1926 					    reg[i].pci_size_low, &mem_answer);
1927 				} else {
1928 					/* get non prefetchable memory */
1929 					pcicfg_get_mem(entry,
1930 					    reg[i].pci_size_low, &mem_answer);
1931 				}
1932 
1933 				pci_config_put32(handle, offset,
1934 				    (uint32_t)mem_answer);
1935 
1936 				DEBUG2("REGISTER off %x(32)LO ----> [0x%x]\n",
1937 				    offset, pci_config_get32(handle, offset));
1938 
1939 				reg[i].pci_phys_hi |= PCI_REG_REL_M;
1940 				reg[i].pci_phys_low = (uint32_t)mem_answer;
1941 
1942 				break;
1943 			case PCI_REG_ADDR_G(PCI_ADDR_IO):
1944 				/* allocate I/O space from the allocator */
1945 
1946 				(void) pcicfg_get_io(entry, reg[i].pci_size_low,
1947 				    &io_answer);
1948 				pci_config_put32(handle, offset, io_answer);
1949 
1950 				DEBUG2("REGISTER off %x (I/O)LO ----> [0x%x]\n",
1951 				    offset, pci_config_get32(handle, offset));
1952 
1953 				reg[i].pci_phys_hi |= PCI_REG_REL_M;
1954 				reg[i].pci_phys_low = io_answer;
1955 
1956 				break;
1957 			default:
1958 				DEBUG0("Unknown register type\n");
1959 				kmem_free(reg, length);
1960 				(void) pcicfg_config_teardown(&handle);
1961 				entry->error = PCICFG_FAILURE;
1962 				return (DDI_WALK_TERMINATE);
1963 			} /* switch */
1964 
1965 			/*
1966 			 * Now that memory locations are assigned,
1967 			 * update the assigned address property.
1968 			 */
1969 			if (pcicfg_update_assigned_prop(dip, &reg[i])
1970 			    != PCICFG_SUCCESS) {
1971 				kmem_free(reg, length);
1972 				(void) pcicfg_config_teardown(&handle);
1973 				entry->error = PCICFG_FAILURE;
1974 				return (DDI_WALK_TERMINATE);
1975 			}
1976 		}
1977 	}
1978 	(void) pcicfg_device_on(handle);
1979 
1980 	PCICFG_DUMP_DEVICE_CONFIG(handle);
1981 
1982 	(void) pcicfg_config_teardown(&handle);
1983 	kmem_free((caddr_t)reg, length);
1984 	return (DDI_WALK_CONTINUE);
1985 }
1986 
1987 static int
1988 pcicfg_device_assign(dev_info_t *dip)
1989 {
1990 	ddi_acc_handle_t	handle;
1991 	pci_regspec_t		*reg;
1992 	int			length;
1993 	int			rcount;
1994 	int			i;
1995 	int			offset;
1996 	ndi_ra_request_t	request;
1997 	uint64_t		answer;
1998 	uint64_t		alen;
1999 
2000 	DEBUG1("%llx now under configuration\n", dip);
2001 
2002 	/* request.ra_len = PCICFG_ROUND_UP(request.ra_len, PCICFG_IOGRAN); */
2003 	if (pcicfg_ntbridge_child(dip) == DDI_SUCCESS) {
2004 
2005 		return (pcicfg_ntbridge_program_child(dip));
2006 	}
2007 	/*
2008 	 * XXX Failure here should be noted
2009 	 */
2010 	if (ddi_getlongprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, "reg",
2011 	    (caddr_t)&reg, &length) != DDI_PROP_SUCCESS) {
2012 		DEBUG0("Failed to read reg property\n");
2013 		return (PCICFG_FAILURE);
2014 	}
2015 
2016 	if (pcicfg_config_setup(dip, &handle) != DDI_SUCCESS) {
2017 		DEBUG0("Failed to map config space!\n");
2018 		kmem_free(reg, length);
2019 		return (PCICFG_FAILURE);
2020 	}
2021 
2022 	/*
2023 	 * A single device
2024 	 *
2025 	 * For each "reg" property with a length, allocate memory
2026 	 * and program the base registers.
2027 	 */
2028 
2029 	/*
2030 	 * If there is an interrupt pin set program
2031 	 * interrupt line with default values.
2032 	 */
2033 	if (pci_config_get8(handle, PCI_CONF_IPIN)) {
2034 		pci_config_put8(handle, PCI_CONF_ILINE, 0xf);
2035 	}
2036 
2037 	bzero((caddr_t)&request, sizeof (ndi_ra_request_t));
2038 
2039 	/*
2040 	 * Note: Both non-prefetchable and prefetchable memory space
2041 	 * allocations are made within 32bit space. Currently, BIOSs
2042 	 * allocate device memory for PCI devices within the 32bit space
2043 	 * so this will not be a problem.
2044 	 */
2045 	request.ra_flags |= NDI_RA_ALIGN_SIZE | NDI_RA_ALLOC_BOUNDED;
2046 	request.ra_boundbase = 0;
2047 	request.ra_boundlen = PCICFG_4GIG_LIMIT;
2048 
2049 	rcount = length / sizeof (pci_regspec_t);
2050 	offset = PCI_CONF_BASE0;
2051 	for (i = 0; i < rcount; i++) {
2052 		char	*mem_type;
2053 
2054 		if ((reg[i].pci_size_low != 0)|| (reg[i].pci_size_hi != 0)) {
2055 
2056 			offset = PCI_REG_REG_G(reg[i].pci_phys_hi);
2057 			request.ra_len = reg[i].pci_size_low;
2058 
2059 			switch (PCI_REG_ADDR_G(reg[i].pci_phys_hi)) {
2060 			case PCI_REG_ADDR_G(PCI_ADDR_MEM64):
2061 				if (reg[i].pci_phys_hi & PCI_REG_PF_M) {
2062 					mem_type = NDI_RA_TYPE_PCI_PREFETCH_MEM;
2063 				} else {
2064 					mem_type = NDI_RA_TYPE_MEM;
2065 				}
2066 				/* allocate memory space from the allocator */
2067 				if (ndi_ra_alloc(ddi_get_parent(dip), &request,
2068 				    &answer, &alen, mem_type, NDI_RA_PASS)
2069 				    != NDI_SUCCESS) {
2070 					DEBUG0("Failed to allocate 64b mem\n");
2071 					kmem_free(reg, length);
2072 					(void) pcicfg_config_teardown(&handle);
2073 					return (PCICFG_NORESRC);
2074 				}
2075 				DEBUG3("64 addr = [0x%x.0x%x] len [0x%x]\n",
2076 				    PCICFG_HIADDR(answer),
2077 				    PCICFG_LOADDR(answer), alen);
2078 				/* program the low word */
2079 				pci_config_put32(handle, offset,
2080 				    PCICFG_LOADDR(answer));
2081 				/* program the high word */
2082 				pci_config_put32(handle, offset + 4,
2083 				    PCICFG_HIADDR(answer));
2084 
2085 				reg[i].pci_phys_hi |= PCI_REG_REL_M;
2086 				reg[i].pci_phys_low = PCICFG_LOADDR(answer);
2087 				reg[i].pci_phys_mid = PCICFG_HIADDR(answer);
2088 				/*
2089 				 * currently support 32b address space
2090 				 * assignments only.
2091 				 */
2092 				reg[i].pci_phys_hi ^=
2093 				    PCI_ADDR_MEM64 ^ PCI_ADDR_MEM32;
2094 
2095 				offset += 8;
2096 				break;
2097 
2098 			case PCI_REG_ADDR_G(PCI_ADDR_MEM32):
2099 				if (reg[i].pci_phys_hi & PCI_REG_PF_M)
2100 					mem_type = NDI_RA_TYPE_PCI_PREFETCH_MEM;
2101 				else
2102 					mem_type = NDI_RA_TYPE_MEM;
2103 				/* allocate memory space from the allocator */
2104 				if (ndi_ra_alloc(ddi_get_parent(dip), &request,
2105 				    &answer, &alen, mem_type, NDI_RA_PASS)
2106 				    != NDI_SUCCESS) {
2107 					DEBUG0("Failed to allocate 32b mem\n");
2108 					kmem_free(reg, length);
2109 					(void) pcicfg_config_teardown(&handle);
2110 					return (PCICFG_NORESRC);
2111 				}
2112 				DEBUG3("32 addr = [0x%x.0x%x] len [0x%x]\n",
2113 				    PCICFG_HIADDR(answer),
2114 				    PCICFG_LOADDR(answer),
2115 				    alen);
2116 				/* program the low word */
2117 				pci_config_put32(handle, offset,
2118 				    PCICFG_LOADDR(answer));
2119 
2120 				reg[i].pci_phys_hi |= PCI_REG_REL_M;
2121 				reg[i].pci_phys_low = PCICFG_LOADDR(answer);
2122 				reg[i].pci_phys_mid = 0;
2123 
2124 				offset += 4;
2125 				break;
2126 			case PCI_REG_ADDR_G(PCI_ADDR_IO):
2127 				/*
2128 				 * Try to allocate I/O space. If it fails,
2129 				 * continue here instead of returning failure
2130 				 * so that the hotplug for drivers that don't
2131 				 * use I/O space can succeed, For drivers
2132 				 * that need to use I/O space, the hotplug
2133 				 * will still fail later during driver attach.
2134 				 */
2135 				if (ndi_ra_alloc(ddi_get_parent(dip), &request,
2136 				    &answer, &alen, NDI_RA_TYPE_IO, NDI_RA_PASS)
2137 				    != NDI_SUCCESS) {
2138 					DEBUG0("Failed to allocate I/O\n");
2139 					continue;
2140 				}
2141 				DEBUG3("I/O addr = [0x%x.0x%x] len [0x%x]\n",
2142 				    PCICFG_HIADDR(answer),
2143 				    PCICFG_LOADDR(answer), alen);
2144 				pci_config_put32(handle, offset,
2145 				    PCICFG_LOADDR(answer));
2146 
2147 				reg[i].pci_phys_hi |= PCI_REG_REL_M;
2148 				reg[i].pci_phys_low = PCICFG_LOADDR(answer);
2149 
2150 				offset += 4;
2151 				break;
2152 			default:
2153 				DEBUG0("Unknown register type\n");
2154 				kmem_free(reg, length);
2155 				(void) pcicfg_config_teardown(&handle);
2156 				return (PCICFG_FAILURE);
2157 			} /* switch */
2158 
2159 			/*
2160 			 * Now that memory locations are assigned,
2161 			 * update the assigned address property.
2162 			 */
2163 
2164 			if (pcicfg_update_assigned_prop(dip, &reg[i])
2165 			    != PCICFG_SUCCESS) {
2166 				kmem_free(reg, length);
2167 				(void) pcicfg_config_teardown(&handle);
2168 				return (PCICFG_FAILURE);
2169 			}
2170 		}
2171 	}
2172 
2173 	(void) pcicfg_device_on(handle);
2174 	kmem_free(reg, length);
2175 
2176 	PCICFG_DUMP_DEVICE_CONFIG(handle);
2177 
2178 	(void) pcicfg_config_teardown(&handle);
2179 	return (PCICFG_SUCCESS);
2180 }
2181 
2182 static int
2183 pcicfg_device_assign_readonly(dev_info_t *dip)
2184 {
2185 	ddi_acc_handle_t	handle;
2186 	pci_regspec_t		*assigned;
2187 	int			length;
2188 	int			acount;
2189 	int			i;
2190 	ndi_ra_request_t	request;
2191 	uint64_t		answer;
2192 	uint64_t		alen;
2193 
2194 	DEBUG1("%llx now under configuration\n", dip);
2195 
2196 	/*
2197 	 * we don't support ntbridges for readonly probe.
2198 	 */
2199 	if (pcicfg_ntbridge_child(dip) == DDI_SUCCESS) {
2200 		return (PCICFG_FAILURE);
2201 	}
2202 
2203 	if (ddi_getlongprop(DDI_DEV_T_ANY, dip,
2204 	    DDI_PROP_DONTPASS, "assigned-addresses", (caddr_t)&assigned,
2205 	    &length) != DDI_PROP_SUCCESS) {
2206 		DEBUG0("Failed to read assigned-addresses property\n");
2207 		return (PCICFG_FAILURE);
2208 	}
2209 
2210 	if (pcicfg_config_setup(dip, &handle) != DDI_SUCCESS) {
2211 		DEBUG0("Failed to map config space!\n");
2212 		kmem_free(assigned, length);
2213 		return (PCICFG_FAILURE);
2214 	}
2215 
2216 	/*
2217 	 * If there is an interrupt pin set program
2218 	 * interrupt line with default values.
2219 	 */
2220 	if (pci_config_get8(handle, PCI_CONF_IPIN)) {
2221 		pci_config_put8(handle, PCI_CONF_ILINE, 0xf);
2222 	}
2223 	/*
2224 	 * Note: Both non-prefetchable and prefetchable memory space
2225 	 * allocations are made within 32bit space. Currently, BIOSs
2226 	 * allocate device memory for PCI devices within the 32bit space
2227 	 * so this will not be a problem.
2228 	 */
2229 	bzero((caddr_t)&request, sizeof (ndi_ra_request_t));
2230 
2231 	request.ra_flags = NDI_RA_ALLOC_SPECIFIED;  /* specified addr */
2232 	request.ra_boundbase = 0;
2233 	request.ra_boundlen = PCICFG_4GIG_LIMIT;
2234 
2235 	acount = length / sizeof (pci_regspec_t);
2236 	for (i = 0; i < acount; i++) {
2237 		char	*mem_type;
2238 
2239 		if ((assigned[i].pci_size_low != 0)||
2240 		    (assigned[i].pci_size_hi != 0)) {
2241 
2242 			request.ra_len = assigned[i].pci_size_low;
2243 
2244 			switch (PCI_REG_ADDR_G(assigned[i].pci_phys_hi)) {
2245 			case PCI_REG_ADDR_G(PCI_ADDR_MEM64):
2246 				request.ra_addr = (uint64_t)PCICFG_LADDR(
2247 				    assigned[i].pci_phys_low,
2248 				    assigned[i].pci_phys_mid);
2249 
2250 				if (assigned[i].pci_phys_hi & PCI_REG_PF_M) {
2251 					mem_type = NDI_RA_TYPE_PCI_PREFETCH_MEM;
2252 				} else {
2253 					mem_type = NDI_RA_TYPE_MEM;
2254 				}
2255 				/* allocate memory space from the allocator */
2256 				if (ndi_ra_alloc(ddi_get_parent(dip), &request,
2257 				    &answer, &alen, mem_type, NDI_RA_PASS)
2258 				    != NDI_SUCCESS) {
2259 					DEBUG0("Failed to allocate 64b mem\n");
2260 					kmem_free(assigned, length);
2261 					return (PCICFG_NORESRC);
2262 				}
2263 
2264 				break;
2265 			case PCI_REG_ADDR_G(PCI_ADDR_MEM32):
2266 				request.ra_addr = (uint64_t)
2267 				    assigned[i].pci_phys_low;
2268 
2269 				if (assigned[i].pci_phys_hi & PCI_REG_PF_M)
2270 					mem_type = NDI_RA_TYPE_PCI_PREFETCH_MEM;
2271 				else
2272 					mem_type = NDI_RA_TYPE_MEM;
2273 				/* allocate memory space from the allocator */
2274 				if (ndi_ra_alloc(ddi_get_parent(dip), &request,
2275 				    &answer, &alen, mem_type, NDI_RA_PASS)
2276 				    != NDI_SUCCESS) {
2277 					DEBUG0("Failed to allocate 32b mem\n");
2278 					kmem_free(assigned, length);
2279 					return (PCICFG_NORESRC);
2280 				}
2281 
2282 				break;
2283 			case PCI_REG_ADDR_G(PCI_ADDR_IO):
2284 				request.ra_addr = (uint64_t)
2285 				    assigned[i].pci_phys_low;
2286 
2287 				/* allocate I/O space from the allocator */
2288 				if (ndi_ra_alloc(ddi_get_parent(dip), &request,
2289 				    &answer, &alen, NDI_RA_TYPE_IO, NDI_RA_PASS)
2290 				    != NDI_SUCCESS) {
2291 					DEBUG0("Failed to allocate I/O\n");
2292 					kmem_free(assigned, length);
2293 					return (PCICFG_NORESRC);
2294 				}
2295 
2296 				break;
2297 			default:
2298 				DEBUG0("Unknown register type\n");
2299 				kmem_free(assigned, length);
2300 				return (PCICFG_FAILURE);
2301 			} /* switch */
2302 		}
2303 	}
2304 
2305 	(void) pcicfg_device_on(handle);
2306 	kmem_free(assigned, length);
2307 
2308 	PCICFG_DUMP_DEVICE_CONFIG(handle);
2309 
2310 	(void) pcicfg_config_teardown(&handle);
2311 	return (PCICFG_SUCCESS);
2312 }
2313 
2314 #ifdef	DEBUG
2315 /*
2316  * This function is useful in debug mode, where we can measure how
2317  * much memory was wasted/unallocated in bridge device's domain.
2318  */
2319 static uint64_t
2320 pcicfg_unused_space(hole_t *hole, uint32_t *hole_count)
2321 {
2322 	uint64_t len = 0;
2323 	uint32_t count = 0;
2324 
2325 	do {
2326 		len += hole->len;
2327 		hole = hole->next;
2328 		count++;
2329 	} while (hole);
2330 	*hole_count = count;
2331 	return (len);
2332 }
2333 #endif
2334 
2335 /*
2336  * This function frees data structures that hold the hole information
2337  * which are allocated in pcicfg_alloc_hole(). This is not freeing
2338  * any memory allocated through NDI calls.
2339  */
2340 static void
2341 pcicfg_free_hole(hole_t *addr_hole)
2342 {
2343 	hole_t *nhole, *hole = addr_hole->next;
2344 
2345 	while (hole) {
2346 		nhole = hole->next;
2347 		kmem_free(hole, sizeof (hole_t));
2348 		hole = nhole;
2349 	}
2350 }
2351 
2352 static uint64_t
2353 pcicfg_alloc_hole(hole_t *addr_hole, uint64_t *alast, uint32_t length)
2354 {
2355 	uint64_t actual_hole_start, ostart, olen;
2356 	hole_t	*hole = addr_hole, *thole, *nhole;
2357 
2358 	do {
2359 		actual_hole_start = PCICFG_ROUND_UP(hole->start, length);
2360 		if (((actual_hole_start - hole->start) + length) <= hole->len) {
2361 			DEBUG3("hole found. start %llx, len %llx, req=0x%x\n",
2362 			    hole->start, hole->len, length);
2363 			ostart = hole->start;
2364 			olen = hole->len;
2365 			/* current hole parameters adjust */
2366 			if ((actual_hole_start - hole->start) == 0) {
2367 				hole->start += length;
2368 				hole->len -= length;
2369 				if (hole->start > *alast)
2370 					*alast = hole->start;
2371 			} else {
2372 				hole->len = actual_hole_start - hole->start;
2373 				nhole = (hole_t *)kmem_zalloc(sizeof (hole_t),
2374 				    KM_SLEEP);
2375 				nhole->start = actual_hole_start + length;
2376 				nhole->len = (ostart + olen) - nhole->start;
2377 				nhole->next = NULL;
2378 				thole = hole->next;
2379 				hole->next = nhole;
2380 				nhole->next = thole;
2381 				if (nhole->start > *alast)
2382 					*alast = nhole->start;
2383 				DEBUG2("put new hole to %llx, %llx\n",
2384 				    nhole->start, nhole->len);
2385 			}
2386 			DEBUG2("adjust current hole to %llx, %llx\n",
2387 			    hole->start, hole->len);
2388 			break;
2389 		}
2390 		actual_hole_start = 0;
2391 		hole = hole->next;
2392 	} while (hole);
2393 
2394 	DEBUG1("return hole at %llx\n", actual_hole_start);
2395 	return (actual_hole_start);
2396 }
2397 
2398 static void
2399 pcicfg_get_mem(pcicfg_phdl_t *entry, uint32_t length, uint64_t *ans)
2400 {
2401 	uint64_t new_mem;
2402 
2403 	/* See if there is a hole, that can hold this request. */
2404 	new_mem = pcicfg_alloc_hole(&entry->mem_hole, &entry->memory_last,
2405 	    length);
2406 	if (new_mem) {	/* if non-zero, found a hole. */
2407 		if (ans != NULL)
2408 			*ans = new_mem;
2409 	} else
2410 		cmn_err(CE_WARN, "No %u bytes memory window for %s\n",
2411 		    length, ddi_get_name(entry->dip));
2412 }
2413 
2414 static void
2415 pcicfg_get_io(pcicfg_phdl_t *entry, uint32_t length, uint32_t *ans)
2416 {
2417 	uint32_t new_io;
2418 	uint64_t io_last;
2419 
2420 	/*
2421 	 * See if there is a hole, that can hold this request.
2422 	 * Pass 64 bit parameters and then truncate to 32 bit.
2423 	 */
2424 	io_last = entry->io_last;
2425 	new_io = (uint32_t)pcicfg_alloc_hole(&entry->io_hole, &io_last, length);
2426 	if (new_io) {	/* if non-zero, found a hole. */
2427 		entry->io_last = (uint32_t)io_last;
2428 		if (ans != NULL)
2429 			*ans = new_io;
2430 	} else
2431 		cmn_err(CE_WARN, "No %u bytes IO space window for %s\n",
2432 		    length, ddi_get_name(entry->dip));
2433 }
2434 
2435 static void
2436 pcicfg_get_pf_mem(pcicfg_phdl_t *entry, uint32_t length, uint64_t *ans)
2437 {
2438 	uint64_t new_mem;
2439 
2440 	/* See if there is a hole, that can hold this request. */
2441 	new_mem = pcicfg_alloc_hole(&entry->pf_mem_hole, &entry->pf_memory_last,
2442 	    length);
2443 	if (new_mem) {	/* if non-zero, found a hole. */
2444 		if (ans != NULL)
2445 			*ans = new_mem;
2446 	} else
2447 		cmn_err(CE_WARN, "No %u bytes PF memory window for %s\n",
2448 		    length, ddi_get_name(entry->dip));
2449 }
2450 
2451 #ifdef __sparc
2452 static int
2453 pcicfg_sum_resources(dev_info_t *dip, void *hdl)
2454 {
2455 	pcicfg_phdl_t *entry = (pcicfg_phdl_t *)hdl;
2456 	pci_regspec_t *pci_rp;
2457 	int length;
2458 	int rcount;
2459 	int i;
2460 	ndi_ra_request_t *pf_mem_request;
2461 	ndi_ra_request_t *mem_request;
2462 	ndi_ra_request_t *io_request;
2463 	uint8_t header_type;
2464 	ddi_acc_handle_t handle;
2465 
2466 	entry->error = PCICFG_SUCCESS;
2467 
2468 	pf_mem_request = &entry->pf_mem_req;
2469 	mem_request = &entry->mem_req;
2470 	io_request =  &entry->io_req;
2471 
2472 	if (pcicfg_config_setup(dip, &handle) != DDI_SUCCESS) {
2473 		DEBUG0("Failed to map config space!\n");
2474 		entry->error = PCICFG_FAILURE;
2475 		return (DDI_WALK_TERMINATE);
2476 	}
2477 
2478 	header_type = pci_config_get8(handle, PCI_CONF_HEADER);
2479 
2480 	/*
2481 	 * If its a bridge - just record the highest bus seen
2482 	 */
2483 	if ((header_type & PCI_HEADER_TYPE_M) == PCI_HEADER_PPB) {
2484 
2485 		if (entry->highest_bus < pci_config_get8(handle,
2486 		    PCI_BCNF_SECBUS)) {
2487 			entry->highest_bus =
2488 			    pci_config_get8(handle, PCI_BCNF_SECBUS);
2489 		}
2490 		(void) pcicfg_config_teardown(&handle);
2491 		entry->error = PCICFG_FAILURE;
2492 		return (DDI_WALK_CONTINUE);
2493 	} else {
2494 		if (ddi_getlongprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
2495 		    "reg", (caddr_t)&pci_rp, &length) != DDI_PROP_SUCCESS) {
2496 			/*
2497 			 * If one node in (the subtree of nodes)
2498 			 * doesn't have a "reg" property fail the
2499 			 * allocation.
2500 			 */
2501 			entry->memory_len = 0;
2502 			entry->io_len = 0;
2503 			entry->pf_memory_len = 0;
2504 			entry->error = PCICFG_FAILURE;
2505 			(void) pcicfg_config_teardown(&handle);
2506 			return (DDI_WALK_TERMINATE);
2507 		}
2508 		/*
2509 		 * For each "reg" property with a length, add that to the
2510 		 * total memory (or I/O) to allocate.
2511 		 */
2512 		rcount = length / sizeof (pci_regspec_t);
2513 
2514 		for (i = 0; i < rcount; i++) {
2515 
2516 			switch (PCI_REG_ADDR_G(pci_rp[i].pci_phys_hi)) {
2517 
2518 			case PCI_REG_ADDR_G(PCI_ADDR_MEM32):
2519 				if (pci_rp[i].pci_phys_hi & PCI_REG_PF_M) {
2520 					pf_mem_request->ra_len =
2521 					    pci_rp[i].pci_size_low +
2522 					    PCICFG_ROUND_UP(
2523 					    pf_mem_request->ra_len,
2524 					    pci_rp[i].pci_size_low);
2525 					DEBUG1("ADDING 32 --->0x%x\n",
2526 					    pci_rp[i].pci_size_low);
2527 				} else {
2528 					mem_request->ra_len =
2529 					    pci_rp[i].pci_size_low +
2530 					    PCICFG_ROUND_UP(mem_request->ra_len,
2531 					    pci_rp[i].pci_size_low);
2532 					DEBUG1("ADDING 32 --->0x%x\n",
2533 					    pci_rp[i].pci_size_low);
2534 				}
2535 
2536 				break;
2537 			case PCI_REG_ADDR_G(PCI_ADDR_MEM64):
2538 				if (pci_rp[i].pci_phys_hi & PCI_REG_PF_M) {
2539 					pf_mem_request->ra_len =
2540 					    pci_rp[i].pci_size_low +
2541 					    PCICFG_ROUND_UP(
2542 					    pf_mem_request->ra_len,
2543 					    pci_rp[i].pci_size_low);
2544 					DEBUG1("ADDING 64 --->0x%x\n",
2545 					    pci_rp[i].pci_size_low);
2546 				} else {
2547 					mem_request->ra_len =
2548 					    pci_rp[i].pci_size_low +
2549 					    PCICFG_ROUND_UP(mem_request->ra_len,
2550 					    pci_rp[i].pci_size_low);
2551 					DEBUG1("ADDING 64 --->0x%x\n",
2552 					    pci_rp[i].pci_size_low);
2553 				}
2554 
2555 				break;
2556 			case PCI_REG_ADDR_G(PCI_ADDR_IO):
2557 				io_request->ra_len =
2558 				    pci_rp[i].pci_size_low +
2559 				    PCICFG_ROUND_UP(io_request->ra_len,
2560 				    pci_rp[i].pci_size_low);
2561 				DEBUG1("ADDING I/O --->0x%x\n",
2562 				    pci_rp[i].pci_size_low);
2563 				break;
2564 			default:
2565 				/* Config space register - not included */
2566 				break;
2567 			}
2568 		}
2569 
2570 		/*
2571 		 * free the memory allocated by ddi_getlongprop
2572 		 */
2573 		kmem_free(pci_rp, length);
2574 
2575 		/*
2576 		 * continue the walk to the next sibling to sum memory
2577 		 */
2578 
2579 		(void) pcicfg_config_teardown(&handle);
2580 
2581 		return (DDI_WALK_CONTINUE);
2582 	}
2583 }
2584 #endif /* __sparc */
2585 
2586 static int
2587 pcicfg_free_bridge_resources(dev_info_t *dip)
2588 {
2589 	ppb_ranges_t		*ranges;
2590 	uint_t			*bus;
2591 	int			k;
2592 	int			length = 0;
2593 	int			i;
2594 
2595 
2596 	if ((i = ddi_getlongprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
2597 	    "ranges", (caddr_t)&ranges, &length)) != DDI_PROP_SUCCESS) {
2598 		DEBUG0("Failed to read ranges property\n");
2599 		if (ddi_get_child(dip)) {
2600 			cmn_err(CE_WARN, "No ranges property found for %s",
2601 			    ddi_get_name(dip));
2602 			/*
2603 			 * strictly speaking, we can check for children with
2604 			 * assigned-addresses but for now it is better to
2605 			 * be conservative and assume that if there are child
2606 			 * nodes, then they do consume PCI memory or IO
2607 			 * resources, Hence return failure.
2608 			 */
2609 			return (PCICFG_FAILURE);
2610 		}
2611 		length = 0;
2612 	}
2613 
2614 	for (i = 0; i < length / sizeof (ppb_ranges_t); i++) {
2615 		char *mem_type;
2616 
2617 		if (ranges[i].size_low != 0 || ranges[i].size_high != 0) {
2618 			switch (ranges[i].parent_high & PCI_REG_ADDR_M) {
2619 			case PCI_ADDR_IO:
2620 				DEBUG2("Free I/O    base/length = "
2621 				    "[0x%x]/[0x%x]\n", ranges[i].child_low,
2622 				    ranges[i].size_low);
2623 				if (ndi_ra_free(ddi_get_parent(dip),
2624 				    (uint64_t)ranges[i].child_low,
2625 				    (uint64_t)ranges[i].size_low,
2626 				    NDI_RA_TYPE_IO, NDI_RA_PASS)
2627 				    != NDI_SUCCESS) {
2628 					DEBUG0("Trouble freeing "
2629 					    "PCI i/o space\n");
2630 					kmem_free(ranges, length);
2631 					return (PCICFG_FAILURE);
2632 				}
2633 				break;
2634 			case PCI_ADDR_MEM32:
2635 			case PCI_ADDR_MEM64:
2636 				if (ranges[i].parent_high & PCI_REG_PF_M) {
2637 					DEBUG3("Free PF Memory base/length = "
2638 					    "[0x%x.0x%x]/[0x%x]\n",
2639 					    ranges[i].child_mid,
2640 					    ranges[i].child_low,
2641 					    ranges[i].size_low);
2642 					mem_type = NDI_RA_TYPE_PCI_PREFETCH_MEM;
2643 				} else {
2644 					DEBUG3("Free Memory base/length"
2645 					    " = [0x%x.0x%x]/[0x%x]\n",
2646 					    ranges[i].child_mid,
2647 					    ranges[i].child_low,
2648 					    ranges[i].size_low)
2649 					mem_type = NDI_RA_TYPE_MEM;
2650 				}
2651 				if (ndi_ra_free(ddi_get_parent(dip),
2652 				    PCICFG_LADDR(ranges[i].child_low,
2653 				    ranges[i].child_mid),
2654 				    (uint64_t)ranges[i].size_low,
2655 				    mem_type, NDI_RA_PASS) != NDI_SUCCESS) {
2656 					DEBUG0("Trouble freeing "
2657 					    "PCI memory space\n");
2658 					kmem_free(ranges, length);
2659 					return (PCICFG_FAILURE);
2660 				}
2661 				break;
2662 			default:
2663 				DEBUG0("Unknown memory space\n");
2664 				break;
2665 			}
2666 		}
2667 	}
2668 
2669 	if (length)
2670 		kmem_free(ranges, length);
2671 
2672 	if (ddi_getlongprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
2673 	    "bus-range", (caddr_t)&bus, &k) != DDI_PROP_SUCCESS) {
2674 		DEBUG0("Failed to read bus-range property\n");
2675 		return (PCICFG_FAILURE);
2676 	}
2677 
2678 	DEBUG2("Need to free bus [%d] range [%d]\n",
2679 	    bus[0], bus[1] - bus[0] + 1);
2680 
2681 	if (ndi_ra_free(ddi_get_parent(dip), (uint64_t)bus[0],
2682 	    (uint64_t)(bus[1] - bus[0] + 1), NDI_RA_TYPE_PCI_BUSNUM,
2683 	    NDI_RA_PASS) != NDI_SUCCESS) {
2684 		DEBUG0("Failed to free a bus number\n");
2685 		kmem_free(bus, k);
2686 		return (PCICFG_FAILURE);
2687 	}
2688 
2689 	kmem_free(bus, k);
2690 	return (PCICFG_SUCCESS);
2691 }
2692 
2693 static int
2694 pcicfg_free_device_resources(dev_info_t *dip)
2695 {
2696 	pci_regspec_t *assigned;
2697 
2698 	int length;
2699 	int acount;
2700 	int i;
2701 
2702 	if (ddi_getlongprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
2703 	    "assigned-addresses", (caddr_t)&assigned, &length)
2704 	    != DDI_PROP_SUCCESS) {
2705 		DEBUG0("Failed to read assigned-addresses property\n");
2706 		return (PCICFG_FAILURE);
2707 	}
2708 
2709 	/*
2710 	 * For each "assigned-addresses" property entry with a length,
2711 	 * call the memory allocation routines to return the
2712 	 * resource.
2713 	 */
2714 	acount = length / sizeof (pci_regspec_t);
2715 	for (i = 0; i < acount; i++) {
2716 		char *mem_type;
2717 
2718 		/*
2719 		 * Free the resource if the size of it is not zero.
2720 		 */
2721 		if ((assigned[i].pci_size_low != 0)||
2722 		    (assigned[i].pci_size_hi != 0)) {
2723 			switch (PCI_REG_ADDR_G(assigned[i].pci_phys_hi)) {
2724 			case PCI_REG_ADDR_G(PCI_ADDR_MEM32):
2725 				/*
2726 				 * Check the assigned address for zero.
2727 				 * (Workaround for Devconf (x86) bug to
2728 				 * skip bogus entry for ROM base address
2729 				 * register. If the assigned address is
2730 				 * zero then ignore the entry
2731 				 * (see bugid 4281306)).
2732 				 */
2733 				if (assigned[i].pci_phys_low == 0)
2734 					break; /* ignore the entry */
2735 
2736 				if (assigned[i].pci_phys_hi & PCI_REG_PF_M)
2737 					mem_type = NDI_RA_TYPE_PCI_PREFETCH_MEM;
2738 				else
2739 					mem_type = NDI_RA_TYPE_MEM;
2740 
2741 				if (ndi_ra_free(ddi_get_parent(dip),
2742 				    (uint64_t)assigned[i].pci_phys_low,
2743 				    (uint64_t)assigned[i].pci_size_low,
2744 				    mem_type, NDI_RA_PASS) != NDI_SUCCESS) {
2745 					DEBUG0("Trouble freeing "
2746 					    "PCI memory space\n");
2747 					kmem_free(assigned, length);
2748 					return (PCICFG_FAILURE);
2749 				}
2750 
2751 				DEBUG4("Returned 0x%x of 32 bit %s space"
2752 				    " @ 0x%x from register 0x%x\n",
2753 				    assigned[i].pci_size_low, mem_type,
2754 				    assigned[i].pci_phys_low,
2755 				    PCI_REG_REG_G(assigned[i].pci_phys_hi));
2756 
2757 			break;
2758 			case PCI_REG_ADDR_G(PCI_ADDR_MEM64):
2759 				if (assigned[i].pci_phys_hi & PCI_REG_PF_M)
2760 					mem_type = NDI_RA_TYPE_PCI_PREFETCH_MEM;
2761 				else
2762 					mem_type = NDI_RA_TYPE_MEM;
2763 
2764 				if (ndi_ra_free(ddi_get_parent(dip),
2765 				    PCICFG_LADDR(assigned[i].pci_phys_low,
2766 				    assigned[i].pci_phys_mid),
2767 				    (uint64_t)assigned[i].pci_size_low,
2768 				    mem_type, NDI_RA_PASS) != NDI_SUCCESS) {
2769 					DEBUG0("Trouble freeing "
2770 					    "PCI memory space\n");
2771 					kmem_free(assigned, length);
2772 					return (PCICFG_FAILURE);
2773 				}
2774 
2775 				DEBUG5("Returned 0x%x of 64 bit %s space"
2776 				    " @ 0x%x.0x%x from register 0x%x\n",
2777 				    assigned[i].pci_size_low,
2778 				    mem_type, assigned[i].pci_phys_mid,
2779 				    assigned[i].pci_phys_low,
2780 				    PCI_REG_REG_G(assigned[i].pci_phys_hi));
2781 
2782 			break;
2783 			case PCI_REG_ADDR_G(PCI_ADDR_IO):
2784 				if (ndi_ra_free(ddi_get_parent(dip),
2785 				    (uint64_t)assigned[i].pci_phys_low,
2786 				    (uint64_t)assigned[i].pci_size_low,
2787 				    NDI_RA_TYPE_IO, NDI_RA_PASS) !=
2788 				    NDI_SUCCESS) {
2789 					DEBUG0("Trouble freeing "
2790 					    "PCI IO space\n");
2791 					kmem_free(assigned, length);
2792 					return (PCICFG_FAILURE);
2793 				}
2794 				DEBUG3("Returned 0x%x of IO space @ 0x%x from "
2795 				    "register 0x%x\n", assigned[i].pci_size_low,
2796 				    assigned[i].pci_phys_low,
2797 				    PCI_REG_REG_G(assigned[i].pci_phys_hi));
2798 			break;
2799 			default:
2800 				DEBUG0("Unknown register type\n");
2801 				kmem_free(assigned, length);
2802 				return (PCICFG_FAILURE);
2803 			} /* switch */
2804 		}
2805 	}
2806 	kmem_free(assigned, length);
2807 	return (PCICFG_SUCCESS);
2808 }
2809 
2810 static int
2811 pcicfg_free_resources(dev_info_t *dip, pcicfg_flags_t flags)
2812 {
2813 	ddi_acc_handle_t handle;
2814 	uint8_t header_type;
2815 
2816 	if (pci_config_setup(dip, &handle) != DDI_SUCCESS) {
2817 		DEBUG0("Failed to map config space!\n");
2818 		return (PCICFG_FAILURE);
2819 	}
2820 
2821 	header_type = pci_config_get8(handle, PCI_CONF_HEADER);
2822 
2823 	(void) pci_config_teardown(&handle);
2824 
2825 	/*
2826 	 * A different algorithm is used for bridges and leaf devices.
2827 	 */
2828 	if ((header_type & PCI_HEADER_TYPE_M) == PCI_HEADER_PPB) {
2829 		/*
2830 		 * We only support readonly probing for leaf devices.
2831 		 */
2832 		if (flags & PCICFG_FLAG_READ_ONLY)
2833 			return (PCICFG_FAILURE);
2834 
2835 		if (pcicfg_free_bridge_resources(dip) != PCICFG_SUCCESS) {
2836 			DEBUG0("Failed freeing up bridge resources\n");
2837 			return (PCICFG_FAILURE);
2838 		}
2839 	} else {
2840 		if (pcicfg_free_device_resources(dip) != PCICFG_SUCCESS) {
2841 			DEBUG0("Failed freeing up device resources\n");
2842 			return (PCICFG_FAILURE);
2843 		}
2844 	}
2845 
2846 	return (PCICFG_SUCCESS);
2847 }
2848 
2849 #ifndef _DONT_USE_1275_GENERIC_NAMES
2850 static char *
2851 pcicfg_get_class_name(uint32_t classcode)
2852 {
2853 	struct pcicfg_name_entry *ptr;
2854 
2855 	for (ptr = &pcicfg_class_lookup[0]; ptr->name != NULL; ptr++) {
2856 		if (ptr->class_code == classcode) {
2857 			return (ptr->name);
2858 		}
2859 	}
2860 	return (NULL);
2861 }
2862 #endif /* _DONT_USE_1275_GENERIC_NAMES */
2863 
2864 static dev_info_t *
2865 pcicfg_devi_find(dev_info_t *dip, uint_t device, uint_t function)
2866 {
2867 	struct pcicfg_find_ctrl ctrl;
2868 	int count;
2869 
2870 	ctrl.device = device;
2871 	ctrl.function = function;
2872 	ctrl.dip = NULL;
2873 
2874 	ndi_devi_enter(dip, &count);
2875 	ddi_walk_devs(ddi_get_child(dip), pcicfg_match_dev, (void *)&ctrl);
2876 	ndi_devi_exit(dip, count);
2877 
2878 	return (ctrl.dip);
2879 }
2880 
2881 static int
2882 pcicfg_match_dev(dev_info_t *dip, void *hdl)
2883 {
2884 	struct pcicfg_find_ctrl *ctrl = (struct pcicfg_find_ctrl *)hdl;
2885 	pci_regspec_t *pci_rp;
2886 	int length;
2887 	int pci_dev;
2888 	int pci_func;
2889 
2890 	if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
2891 	    "reg", (int **)&pci_rp, (uint_t *)&length) != DDI_PROP_SUCCESS) {
2892 		ctrl->dip = NULL;
2893 		return (DDI_WALK_TERMINATE);
2894 	}
2895 
2896 	/* get the PCI device address info */
2897 	pci_dev = PCI_REG_DEV_G(pci_rp->pci_phys_hi);
2898 	pci_func = PCI_REG_FUNC_G(pci_rp->pci_phys_hi);
2899 
2900 	/*
2901 	 * free the memory allocated by ddi_prop_lookup_int_array
2902 	 */
2903 	ddi_prop_free(pci_rp);
2904 
2905 
2906 	if ((pci_dev == ctrl->device) && (pci_func == ctrl->function)) {
2907 		/* found the match for the specified device address */
2908 		ctrl->dip = dip;
2909 		return (DDI_WALK_TERMINATE);
2910 	}
2911 
2912 	/*
2913 	 * continue the walk to the next sibling to look for a match.
2914 	 */
2915 	return (DDI_WALK_PRUNECHILD);
2916 }
2917 
2918 static int
2919 pcicfg_update_assigned_prop(dev_info_t *dip, pci_regspec_t *newone)
2920 {
2921 	int		alen;
2922 	pci_regspec_t	*assigned;
2923 	caddr_t		newreg;
2924 	uint_t		status;
2925 
2926 	status = ddi_getlongprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
2927 	    "assigned-addresses", (caddr_t)&assigned, &alen);
2928 	switch (status) {
2929 		case DDI_PROP_SUCCESS:
2930 		break;
2931 		case DDI_PROP_NO_MEMORY:
2932 			DEBUG0("no memory for assigned-addresses property\n");
2933 			return (PCICFG_FAILURE);
2934 		default:
2935 			(void) ndi_prop_update_int_array(DDI_DEV_T_NONE, dip,
2936 			    "assigned-addresses", (int *)newone,
2937 			    sizeof (*newone)/sizeof (int));
2938 			return (PCICFG_SUCCESS);
2939 	}
2940 
2941 	/*
2942 	 * Allocate memory for the existing
2943 	 * assigned-addresses(s) plus one and then
2944 	 * build it.
2945 	 */
2946 
2947 	newreg = kmem_zalloc(alen+sizeof (*newone), KM_SLEEP);
2948 
2949 	bcopy(assigned, newreg, alen);
2950 	bcopy(newone, newreg + alen, sizeof (*newone));
2951 
2952 	/*
2953 	 * Write out the new "assigned-addresses" spec
2954 	 */
2955 	(void) ndi_prop_update_int_array(DDI_DEV_T_NONE, dip,
2956 	    "assigned-addresses", (int *)newreg,
2957 	    (alen + sizeof (*newone))/sizeof (int));
2958 
2959 	kmem_free((caddr_t)newreg, alen+sizeof (*newone));
2960 	kmem_free(assigned, alen);
2961 
2962 	return (PCICFG_SUCCESS);
2963 }
2964 
2965 static int
2966 pcicfg_update_ranges_prop(dev_info_t *dip, ppb_ranges_t *addition)
2967 {
2968 	int		rlen;
2969 	ppb_ranges_t	*ranges;
2970 	caddr_t		newreg;
2971 	uint_t		status;
2972 
2973 	status = ddi_getlongprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
2974 	    "ranges", (caddr_t)&ranges, &rlen);
2975 
2976 
2977 	switch (status) {
2978 		case DDI_PROP_SUCCESS:
2979 			break;
2980 		case DDI_PROP_NO_MEMORY:
2981 			DEBUG0("ranges present, but unable to get memory\n");
2982 			return (PCICFG_FAILURE);
2983 		default:
2984 			DEBUG0("no ranges property - creating one\n");
2985 			if (ndi_prop_update_int_array(DDI_DEV_T_NONE,
2986 			    dip, "ranges", (int *)addition,
2987 			    sizeof (ppb_ranges_t)/sizeof (int))
2988 			    != DDI_SUCCESS) {
2989 				DEBUG0("Did'nt create ranges property\n");
2990 				return (PCICFG_FAILURE);
2991 			}
2992 			return (PCICFG_SUCCESS);
2993 	}
2994 
2995 	/*
2996 	 * Allocate memory for the existing ranges plus one and then
2997 	 * build it.
2998 	 */
2999 	newreg = kmem_zalloc(rlen+sizeof (ppb_ranges_t), KM_SLEEP);
3000 
3001 	bcopy(ranges, newreg, rlen);
3002 	bcopy(addition, newreg + rlen, sizeof (ppb_ranges_t));
3003 
3004 	/*
3005 	 * Write out the new "ranges" property
3006 	 */
3007 	(void) ndi_prop_update_int_array(DDI_DEV_T_NONE, dip, "ranges",
3008 	    (int *)newreg, (rlen + sizeof (ppb_ranges_t))/sizeof (int));
3009 
3010 	DEBUG1("Updating ranges property for %d entries",
3011 	    rlen / sizeof (ppb_ranges_t) + 1);
3012 
3013 	kmem_free((caddr_t)newreg, rlen+sizeof (ppb_ranges_t));
3014 
3015 	kmem_free((caddr_t)ranges, rlen);
3016 
3017 	return (PCICFG_SUCCESS);
3018 }
3019 
3020 static int
3021 pcicfg_update_reg_prop(dev_info_t *dip, uint32_t regvalue, uint_t reg_offset)
3022 {
3023 	int		rlen;
3024 	pci_regspec_t	*reg;
3025 	caddr_t		newreg;
3026 	uint32_t	hiword;
3027 	pci_regspec_t	addition;
3028 	uint32_t	size;
3029 	uint_t		status;
3030 
3031 	status = ddi_getlongprop(DDI_DEV_T_ANY,
3032 	    dip, DDI_PROP_DONTPASS, "reg", (caddr_t)&reg, &rlen);
3033 
3034 	switch (status) {
3035 		case DDI_PROP_SUCCESS:
3036 		break;
3037 		case DDI_PROP_NO_MEMORY:
3038 			DEBUG0("reg present, but unable to get memory\n");
3039 			return (PCICFG_FAILURE);
3040 		default:
3041 			DEBUG0("no reg property\n");
3042 			return (PCICFG_FAILURE);
3043 	}
3044 
3045 	/*
3046 	 * Allocate memory for the existing reg(s) plus one and then
3047 	 * build it.
3048 	 */
3049 	newreg = kmem_zalloc(rlen+sizeof (pci_regspec_t), KM_SLEEP);
3050 
3051 	/*
3052 	 * Build the regspec, then add it to the existing one(s)
3053 	 */
3054 
3055 	hiword = PCICFG_MAKE_REG_HIGH(PCI_REG_BUS_G(reg->pci_phys_hi),
3056 	    PCI_REG_DEV_G(reg->pci_phys_hi),
3057 	    PCI_REG_FUNC_G(reg->pci_phys_hi), reg_offset);
3058 
3059 	if (reg_offset == PCI_CONF_ROM) {
3060 		size = (~(PCI_BASE_ROM_ADDR_M & regvalue))+1;
3061 		hiword |= PCI_ADDR_MEM32;
3062 	} else {
3063 		size = (~(PCI_BASE_M_ADDR_M & regvalue))+1;
3064 
3065 		if ((PCI_BASE_SPACE_M & regvalue) == PCI_BASE_SPACE_MEM) {
3066 			if ((PCI_BASE_TYPE_M & regvalue) == PCI_BASE_TYPE_MEM) {
3067 				hiword |= PCI_ADDR_MEM32;
3068 			} else if ((PCI_BASE_TYPE_M & regvalue)
3069 			    == PCI_BASE_TYPE_ALL) {
3070 				hiword |= PCI_ADDR_MEM64;
3071 			}
3072 			if (regvalue & PCI_BASE_PREF_M)
3073 				hiword |= PCI_REG_PF_M;
3074 		} else {
3075 			hiword |= PCI_ADDR_IO;
3076 		}
3077 	}
3078 
3079 	addition.pci_phys_hi = hiword;
3080 	addition.pci_phys_mid = 0;
3081 	addition.pci_phys_low = 0;
3082 	addition.pci_size_hi = 0;
3083 	addition.pci_size_low = size;
3084 
3085 	bcopy(reg, newreg, rlen);
3086 	bcopy(&addition, newreg + rlen, sizeof (pci_regspec_t));
3087 
3088 	DEBUG3("updating BAR@off %x with %x,%x\n", reg_offset, hiword, size);
3089 	/*
3090 	 * Write out the new "reg" property
3091 	 */
3092 	(void) ndi_prop_update_int_array(DDI_DEV_T_NONE, dip, "reg",
3093 	    (int *)newreg, (rlen + sizeof (pci_regspec_t))/sizeof (int));
3094 
3095 	kmem_free((caddr_t)newreg, rlen+sizeof (pci_regspec_t));
3096 	kmem_free((caddr_t)reg, rlen);
3097 
3098 	return (PCICFG_SUCCESS);
3099 }
3100 
3101 static int
3102 pcicfg_update_assigned_prop_value(dev_info_t *dip, uint32_t size,
3103     uint32_t base, uint32_t base_hi, uint_t reg_offset)
3104 {
3105 	int		rlen;
3106 	pci_regspec_t	*reg;
3107 	uint32_t	hiword;
3108 	pci_regspec_t	addition;
3109 	uint_t		status;
3110 
3111 	status = ddi_getlongprop(DDI_DEV_T_ANY,
3112 	    dip, DDI_PROP_DONTPASS, "reg", (caddr_t)&reg, &rlen);
3113 
3114 	switch (status) {
3115 		case DDI_PROP_SUCCESS:
3116 		break;
3117 		case DDI_PROP_NO_MEMORY:
3118 			DEBUG0("reg present, but unable to get memory\n");
3119 			return (PCICFG_FAILURE);
3120 		default:
3121 			/*
3122 			 * Since the config space "reg" entry should have been
3123 			 * created, we expect a "reg" property already
3124 			 * present here.
3125 			 */
3126 			DEBUG0("no reg property\n");
3127 			return (PCICFG_FAILURE);
3128 	}
3129 
3130 	/*
3131 	 * Build the regspec, then add it to the existing one(s)
3132 	 */
3133 
3134 	hiword = PCICFG_MAKE_REG_HIGH(PCI_REG_BUS_G(reg->pci_phys_hi),
3135 	    PCI_REG_DEV_G(reg->pci_phys_hi),
3136 	    PCI_REG_FUNC_G(reg->pci_phys_hi), reg_offset);
3137 
3138 	hiword |= PCI_REG_REL_M;
3139 
3140 	if (reg_offset == PCI_CONF_ROM) {
3141 		hiword |= PCI_ADDR_MEM32;
3142 
3143 		base = PCI_BASE_ROM_ADDR_M & base;
3144 	} else {
3145 		if ((PCI_BASE_SPACE_M & base) == PCI_BASE_SPACE_MEM) {
3146 			if ((PCI_BASE_TYPE_M & base) == PCI_BASE_TYPE_MEM) {
3147 				hiword |= PCI_ADDR_MEM32;
3148 			} else if ((PCI_BASE_TYPE_M & base)
3149 			    == PCI_BASE_TYPE_ALL) {
3150 				hiword |= PCI_ADDR_MEM64;
3151 			}
3152 			if (base & PCI_BASE_PREF_M)
3153 				hiword |= PCI_REG_PF_M;
3154 
3155 			base = PCI_BASE_M_ADDR_M & base;
3156 		} else {
3157 			hiword |= PCI_ADDR_IO;
3158 
3159 			base = PCI_BASE_IO_ADDR_M & base;
3160 			base_hi = 0;
3161 		}
3162 	}
3163 
3164 	addition.pci_phys_hi = hiword;
3165 	addition.pci_phys_mid = base_hi;
3166 	addition.pci_phys_low = base;
3167 	addition.pci_size_hi = 0;
3168 	addition.pci_size_low = size;
3169 
3170 	DEBUG3("updating BAR@off %x with %x,%x\n", reg_offset, hiword, size);
3171 
3172 	kmem_free((caddr_t)reg, rlen);
3173 
3174 	return (pcicfg_update_assigned_prop(dip, &addition));
3175 }
3176 
3177 static void
3178 pcicfg_device_on(ddi_acc_handle_t config_handle)
3179 {
3180 	/*
3181 	 * Enable memory, IO, and bus mastership
3182 	 * XXX should we enable parity, SERR#,
3183 	 * fast back-to-back, and addr. stepping?
3184 	 */
3185 	pci_config_put16(config_handle, PCI_CONF_COMM,
3186 	    pci_config_get16(config_handle, PCI_CONF_COMM) | 0x7);
3187 }
3188 
3189 static void
3190 pcicfg_device_off(ddi_acc_handle_t config_handle)
3191 {
3192 	/*
3193 	 * Disable I/O and memory traffic through the bridge
3194 	 */
3195 	pci_config_put16(config_handle, PCI_CONF_COMM, 0x0);
3196 }
3197 
3198 /*
3199  * Setup the basic 1275 properties based on information found in the config
3200  * header of the PCI device
3201  */
3202 static int
3203 pcicfg_set_standard_props(dev_info_t *dip, ddi_acc_handle_t config_handle,
3204     uint8_t pcie_dev)
3205 {
3206 	int ret;
3207 	uint16_t cap_id_loc, val;
3208 	uint32_t wordval;
3209 	uint8_t byteval;
3210 
3211 	/* These two exists only for non-bridges */
3212 	if (((pci_config_get8(config_handle, PCI_CONF_HEADER) &
3213 	    PCI_HEADER_TYPE_M) == PCI_HEADER_ZERO) && !pcie_dev) {
3214 		byteval = pci_config_get8(config_handle, PCI_CONF_MIN_G);
3215 		if ((ret = ndi_prop_update_int(DDI_DEV_T_NONE, dip,
3216 		    "min-grant", byteval)) != DDI_SUCCESS) {
3217 			return (ret);
3218 		}
3219 
3220 		byteval = pci_config_get8(config_handle, PCI_CONF_MAX_L);
3221 		if ((ret = ndi_prop_update_int(DDI_DEV_T_NONE, dip,
3222 		    "max-latency", byteval)) != DDI_SUCCESS) {
3223 			return (ret);
3224 		}
3225 	}
3226 
3227 	/*
3228 	 * These should always exist and have the value of the
3229 	 * corresponding register value
3230 	 */
3231 	val = pci_config_get16(config_handle, PCI_CONF_VENID);
3232 
3233 	if ((ret = ndi_prop_update_int(DDI_DEV_T_NONE, dip, "vendor-id", val))
3234 	    != DDI_SUCCESS) {
3235 		return (ret);
3236 	}
3237 	val = pci_config_get16(config_handle, PCI_CONF_DEVID);
3238 	if ((ret = ndi_prop_update_int(DDI_DEV_T_NONE, dip, "device-id", val))
3239 	    != DDI_SUCCESS) {
3240 		return (ret);
3241 	}
3242 	byteval = pci_config_get8(config_handle, PCI_CONF_REVID);
3243 	if ((ret = ndi_prop_update_int(DDI_DEV_T_NONE, dip,
3244 	    "revision-id", byteval)) != DDI_SUCCESS) {
3245 		return (ret);
3246 	}
3247 
3248 	wordval = (pci_config_get16(config_handle, PCI_CONF_SUBCLASS)<< 8) |
3249 	    (pci_config_get8(config_handle, PCI_CONF_PROGCLASS));
3250 
3251 	if ((ret = ndi_prop_update_int(DDI_DEV_T_NONE, dip,
3252 	    "class-code", wordval)) != DDI_SUCCESS) {
3253 		return (ret);
3254 	}
3255 	val = (pci_config_get16(config_handle, PCI_CONF_STAT) &
3256 	    PCI_STAT_DEVSELT);
3257 	if ((ret = ndi_prop_update_int(DDI_DEV_T_NONE, dip,
3258 	    "devsel-speed", val)) != DDI_SUCCESS) {
3259 		return (ret);
3260 	}
3261 
3262 	/*
3263 	 * The next three are bits set in the status register.  The property is
3264 	 * present (but with no value other than its own existence) if the bit
3265 	 * is set, non-existent otherwise
3266 	 */
3267 	if ((!pcie_dev) &&
3268 	    (pci_config_get16(config_handle, PCI_CONF_STAT) & PCI_STAT_FBBC)) {
3269 		if ((ret = ndi_prop_update_int(DDI_DEV_T_NONE, dip,
3270 		    "fast-back-to-back", 0)) != DDI_SUCCESS) {
3271 			return (ret);
3272 		}
3273 	}
3274 	if ((!pcie_dev) &&
3275 	    (pci_config_get16(config_handle, PCI_CONF_STAT) & PCI_STAT_66MHZ)) {
3276 		if ((ret = ndi_prop_update_int(DDI_DEV_T_NONE, dip,
3277 		    "66mhz-capable", 0)) != DDI_SUCCESS) {
3278 			return (ret);
3279 		}
3280 	}
3281 	if (pci_config_get16(config_handle, PCI_CONF_STAT) & PCI_STAT_UDF) {
3282 		if ((ret = ndi_prop_update_int(DDI_DEV_T_NONE, dip,
3283 		    "udf-supported", 0)) != DDI_SUCCESS) {
3284 			return (ret);
3285 		}
3286 	}
3287 
3288 	/*
3289 	 * These next three are optional and are not present
3290 	 * if the corresponding register is zero.  If the value
3291 	 * is non-zero then the property exists with the value
3292 	 * of the register.
3293 	 */
3294 	if ((val = pci_config_get16(config_handle, PCI_CONF_SUBVENID)) != 0) {
3295 		if ((ret = ndi_prop_update_int(DDI_DEV_T_NONE, dip,
3296 		    "subsystem-vendor-id", val)) != DDI_SUCCESS) {
3297 			return (ret);
3298 		}
3299 	}
3300 	if ((val = pci_config_get16(config_handle, PCI_CONF_SUBSYSID)) != 0) {
3301 		if ((ret = ndi_prop_update_int(DDI_DEV_T_NONE, dip,
3302 		    "subsystem-id", val)) != DDI_SUCCESS) {
3303 			return (ret);
3304 		}
3305 	}
3306 	if ((val = pci_config_get16(config_handle, PCI_CONF_CACHE_LINESZ))
3307 	    != 0) {
3308 		if ((ret = ndi_prop_update_int(DDI_DEV_T_NONE, dip,
3309 		    "cache-line-size", val)) != DDI_SUCCESS) {
3310 			return (ret);
3311 		}
3312 	}
3313 
3314 	/*
3315 	 * If the Interrupt Pin register is non-zero then the
3316 	 * interrupts property exists
3317 	 */
3318 	if ((byteval = pci_config_get8(config_handle, PCI_CONF_IPIN)) != 0) {
3319 		/*
3320 		 * If interrupt pin is non-zero,
3321 		 * record the interrupt line used
3322 		 */
3323 		if ((ret = ndi_prop_update_int(DDI_DEV_T_NONE, dip,
3324 		    "interrupts", byteval)) != DDI_SUCCESS) {
3325 			return (ret);
3326 		}
3327 	}
3328 	(void) PCI_CAP_LOCATE(config_handle, PCI_CAP_ID_PCI_E, &cap_id_loc);
3329 	if (pcie_dev && cap_id_loc != PCI_CAP_NEXT_PTR_NULL) {
3330 		val = pci_config_get16(config_handle, cap_id_loc + PCIE_PCIECAP)
3331 		    & PCIE_PCIECAP_SLOT_IMPL;
3332 		/* if slot implemented, get physical slot number */
3333 		if (val) {
3334 			wordval = pci_config_get32(config_handle, cap_id_loc +
3335 			    PCIE_SLOTCAP);
3336 			/* create the property only if slotnum set correctly? */
3337 			if ((ret = ndi_prop_update_int(DDI_DEV_T_NONE, dip,
3338 			    "physical-slot#", PCIE_SLOTCAP_PHY_SLOT_NUM(
3339 			    wordval))) != DDI_SUCCESS) {
3340 				return (ret);
3341 			}
3342 		}
3343 	}
3344 
3345 	return (PCICFG_SUCCESS);
3346 }
3347 
3348 static int
3349 pcicfg_set_busnode_props(dev_info_t *dip, uint8_t pcie_device_type)
3350 {
3351 	int ret;
3352 	char device_type[8];
3353 
3354 	if (pcie_device_type)
3355 		(void) strcpy(device_type, "pciex");
3356 	else
3357 		(void) strcpy(device_type, "pci");
3358 
3359 	if ((ret = ndi_prop_update_string(DDI_DEV_T_NONE, dip,
3360 	    "device_type", device_type)) != DDI_SUCCESS) {
3361 		return (ret);
3362 	}
3363 	if ((ret = ndi_prop_update_int(DDI_DEV_T_NONE, dip,
3364 	    "#address-cells", 3)) != DDI_SUCCESS) {
3365 		return (ret);
3366 	}
3367 	if ((ret = ndi_prop_update_int(DDI_DEV_T_NONE, dip, "#size-cells", 2))
3368 	    != DDI_SUCCESS) {
3369 		return (ret);
3370 	}
3371 	return (PCICFG_SUCCESS);
3372 }
3373 
3374 static int
3375 pcicfg_set_childnode_props(dev_info_t *dip, ddi_acc_handle_t config_handle,
3376     uint8_t pcie_dev)
3377 {
3378 
3379 	int		ret;
3380 	char		*name;
3381 	char		buffer[64], pprefix[8], nprefix[8];
3382 	uint16_t	classcode;
3383 	uint8_t		revid, pif, pclass, psubclass;
3384 	char		*compat[24];
3385 	int		i;
3386 	int		n;
3387 	uint16_t		sub_vid, sub_sid, vid, did;
3388 	/* set the property prefix based on the device type */
3389 	if (pcie_dev) {
3390 		(void) sprintf(pprefix, "pciex");
3391 	} else
3392 		(void) sprintf(pprefix, "pci");
3393 
3394 	/* set the prefix right for name property */
3395 	/* x86 platforms need to go with pci for upgrade purposes */
3396 	(void) sprintf(nprefix, "pci");
3397 
3398 	/*
3399 	 * NOTE: These are for both a child and PCI-PCI bridge node
3400 	 */
3401 	sub_vid = pci_config_get16(config_handle, PCI_CONF_SUBVENID);
3402 	sub_sid = pci_config_get16(config_handle, PCI_CONF_SUBSYSID);
3403 	vid = pci_config_get16(config_handle, PCI_CONF_VENID);
3404 	did = pci_config_get16(config_handle, PCI_CONF_DEVID);
3405 	revid = pci_config_get8(config_handle, PCI_CONF_REVID);
3406 	pif = pci_config_get8(config_handle, PCI_CONF_PROGCLASS);
3407 	classcode = pci_config_get16(config_handle, PCI_CONF_SUBCLASS);
3408 	pclass = pci_config_get8(config_handle, PCI_CONF_BASCLASS);
3409 	psubclass = pci_config_get8(config_handle, PCI_CONF_SUBCLASS);
3410 
3411 	if (!sub_vid)
3412 		(void) sprintf(buffer, "%s%x,%x", nprefix, vid, did);
3413 	else
3414 		(void) sprintf(buffer, "%s%x,%x", nprefix, sub_vid, sub_sid);
3415 
3416 	/*
3417 	 * In some environments, trying to use "generic" 1275 names is
3418 	 * not the convention.  In those cases use the name as created
3419 	 * above.  In all the rest of the cases, check to see if there
3420 	 * is a generic name first.
3421 	 */
3422 #ifdef _DONT_USE_1275_GENERIC_NAMES
3423 	name = buffer;
3424 #else
3425 	if ((name = pcicfg_get_class_name(classcode)) == NULL) {
3426 		/*
3427 		 * Set name to the above fabricated name
3428 		 */
3429 		name = buffer;
3430 	}
3431 #endif
3432 
3433 	/*
3434 	 * The node name field needs to be filled in with the name
3435 	 */
3436 	if (ndi_devi_set_nodename(dip, name, 0) != NDI_SUCCESS) {
3437 		DEBUG0("Failed to set nodename for node\n");
3438 		return (PCICFG_FAILURE);
3439 	}
3440 
3441 	/*
3442 	 * Create the compatible property as an array of pointers
3443 	 * to strings.  Start with the buffer created above.
3444 	 */
3445 	n = 0;
3446 
3447 	/*
3448 	 * Setup 'compatible' as per the PCI2.1 bindings document.
3449 	 *	pci[ex]VVVV,DDDD.SSSS.ssss.RR
3450 	 *	pci[ex]VVVV,DDDD.SSSS.ssss
3451 	 *	pciSSSS.ssss  -> not created for PCIe as per PCIe bindings
3452 	 *	pci[ex]VVVV,DDDD.RR
3453 	 *	pci[ex]VVVV,DDDD
3454 	 *	pci[ex]class,CCSSPP
3455 	 *	pci[ex]class,CCSS
3456 	 * Add legacy entries for compatibility with legacy devices and OS
3457 	 * for x86.
3458 	 *	pciVVVV,DDDD.SSSS.ssss.RR
3459 	 *	pciVVVV,DDDD.SSSS.ssss
3460 	 *	pciSSSS.ssss
3461 	 *	pciVVVV,DDDD.RR
3462 	 *	pciVVVV,DDDD
3463 	 *	pciclass,CCSSPP
3464 	 *	pciclass,CCSS
3465 	 */
3466 
3467 	do {
3468 		if (sub_vid) {
3469 			/* pci[ex]VVVV,DDDD.SSSS.ssss.RR */
3470 			(void) sprintf(buffer, "%s%x,%x.%x.%x.%x", pprefix, vid,
3471 			    did, sub_vid, sub_sid, revid);
3472 			compat[n] = kmem_alloc(strlen(buffer) + 1, KM_SLEEP);
3473 			(void) strcpy(compat[n++], buffer);
3474 
3475 			/* pci[ex]VVVV,DDDD.SSSS.ssss */
3476 			(void) sprintf(buffer, "%s%x,%x.%x.%x", pprefix,  vid,
3477 			    did, sub_vid, sub_sid);
3478 			compat[n] = kmem_alloc(strlen(buffer) + 1, KM_SLEEP);
3479 			(void) strcpy(compat[n++], buffer);
3480 
3481 			/* pciSSSS.ssss  -> not created for PCIe as per PCIe */
3482 			/* binding to IEEE 1275 spec.			 */
3483 			if (!pcie_dev && pcicfg_do_legacy_props) {
3484 				(void) sprintf(buffer, "pci%x,%x", sub_vid,
3485 				    sub_sid);
3486 				compat[n] = kmem_alloc(strlen(buffer) + 1,
3487 				    KM_SLEEP);
3488 				(void) strcpy(compat[n++], buffer);
3489 			}
3490 		}
3491 
3492 		/* pci[ex]VVVV,DDDD.RR */
3493 		(void) sprintf(buffer, "%s%x,%x.%x", pprefix,  vid, did, revid);
3494 		compat[n] = kmem_alloc(strlen(buffer) + 1, KM_SLEEP);
3495 		(void) strcpy(compat[n++], buffer);
3496 
3497 		/* pci[ex]VVVV,DDDD */
3498 		(void) sprintf(buffer, "%s%x,%x", pprefix, vid, did);
3499 		compat[n] = kmem_alloc(strlen(buffer) + 1, KM_SLEEP);
3500 		(void) strcpy(compat[n++], buffer);
3501 
3502 		/* pci[ex]class,CCSSPP */
3503 		(void) sprintf(buffer, "%sclass,%02x%02x%02x", pprefix, pclass,
3504 		    psubclass, pif);
3505 		compat[n] = kmem_alloc(strlen(buffer) + 1, KM_SLEEP);
3506 		(void) strcpy(compat[n++], buffer);
3507 
3508 		/* pci[ex]class,CCSS */
3509 		(void) sprintf(buffer, "%sclass,%04x", pprefix, classcode);
3510 		compat[n] = kmem_alloc(strlen(buffer) + 1, KM_SLEEP);
3511 		(void) strcpy(compat[n++], buffer);
3512 
3513 		if (!pcie_dev)
3514 			break;
3515 
3516 		/* also add compatible names using "pci" prefix */
3517 		(void) sprintf(pprefix, "pci");
3518 		pcie_dev = 0;
3519 
3520 	} while (pcicfg_do_legacy_props);
3521 
3522 	ret = ndi_prop_update_string_array(DDI_DEV_T_NONE, dip, "compatible",
3523 	    (char **)compat, n);
3524 
3525 	for (i = 0; i < n; i++) {
3526 		kmem_free(compat[i], strlen(compat[i]) + 1);
3527 	}
3528 
3529 	return (ret);
3530 }
3531 
3532 /*
3533  * Program the bus numbers into the bridge
3534  */
3535 static void
3536 pcicfg_set_bus_numbers(ddi_acc_handle_t config_handle, uint_t primary,
3537     uint_t secondary, uint_t subordinate)
3538 {
3539 	DEBUG3("Setting bridge bus-range %d,%d,%d\n", primary, secondary,
3540 	    subordinate);
3541 	/*
3542 	 * Primary bus#
3543 	 */
3544 	pci_config_put8(config_handle, PCI_BCNF_PRIBUS, primary);
3545 
3546 	/*
3547 	 * Secondary bus#
3548 	 */
3549 	pci_config_put8(config_handle, PCI_BCNF_SECBUS, secondary);
3550 
3551 	/*
3552 	 * Set the subordinate bus number to ff in order to pass through any
3553 	 * type 1 cycle with a bus number higher than the secondary bus#
3554 	 */
3555 	pci_config_put8(config_handle, PCI_BCNF_SUBBUS, subordinate);
3556 }
3557 
3558 /*
3559  * Put bridge registers into initial state
3560  */
3561 static void
3562 pcicfg_setup_bridge(pcicfg_phdl_t *entry, ddi_acc_handle_t handle)
3563 {
3564 	/*
3565 	 * The highest bus seen during probing is the max-subordinate bus
3566 	 */
3567 	pci_config_put8(handle, PCI_BCNF_SUBBUS, entry->highest_bus);
3568 
3569 	/*
3570 	 * Reset the secondary bus
3571 	 */
3572 	pci_config_put16(handle, PCI_BCNF_BCNTRL,
3573 	    pci_config_get16(handle, PCI_BCNF_BCNTRL) | 0x40);
3574 	drv_usecwait(1000);
3575 	pci_config_put16(handle, PCI_BCNF_BCNTRL,
3576 	    pci_config_get16(handle, PCI_BCNF_BCNTRL) & ~0x40);
3577 	drv_usecwait(1000);
3578 
3579 	/*
3580 	 * Program the memory base register with the
3581 	 * start of the memory range
3582 	 */
3583 	pci_config_put16(handle, PCI_BCNF_MEM_BASE,
3584 	    PCICFG_HIWORD(PCICFG_LOADDR(entry->memory_last)));
3585 
3586 	/*
3587 	 * Program the I/O base register with the start of the I/O range
3588 	 */
3589 	pci_config_put8(handle, PCI_BCNF_IO_BASE_LOW,
3590 	    PCICFG_HIBYTE(PCICFG_LOWORD(PCICFG_LOADDR(entry->io_last))));
3591 	pci_config_put16(handle, PCI_BCNF_IO_BASE_HI,
3592 	    PCICFG_HIWORD(PCICFG_LOADDR(entry->io_last)));
3593 
3594 	/*
3595 	 * Program the PF memory base register with the start of
3596 	 * PF memory range
3597 	 */
3598 	pci_config_put16(handle, PCI_BCNF_PF_BASE_LOW,
3599 	    PCICFG_HIWORD(PCICFG_LOADDR(entry->pf_memory_last)));
3600 	pci_config_put32(handle, PCI_BCNF_PF_BASE_HIGH,
3601 	    PCICFG_HIADDR(entry->pf_memory_last));
3602 
3603 	/*
3604 	 * Clear status bits
3605 	 */
3606 	pci_config_put16(handle, PCI_BCNF_SEC_STATUS, 0xffff);
3607 
3608 	/*
3609 	 * Needs to be set to this value
3610 	 */
3611 	pci_config_put8(handle, PCI_CONF_ILINE, 0xf);
3612 
3613 	/*
3614 	 * XXX - may be delay should be used since noone configures
3615 	 * devices in the interrupt context
3616 	 */
3617 	drv_usecwait(pcicfg_sec_reset_delay);	/* 1 sec wait */
3618 }
3619 
3620 static void
3621 pcicfg_update_bridge(pcicfg_phdl_t *entry, ddi_acc_handle_t handle)
3622 {
3623 	uint_t length;
3624 
3625 	/*
3626 	 * Program the memory limit register with the end of the memory range
3627 	 */
3628 
3629 	DEBUG1("DOWN ROUNDED ===>[0x%x]\n",
3630 	    PCICFG_ROUND_DOWN(entry->memory_last, PCICFG_MEMGRAN));
3631 
3632 	pci_config_put16(handle, PCI_BCNF_MEM_LIMIT,
3633 	    PCICFG_HIWORD(PCICFG_LOADDR(
3634 	    PCICFG_ROUND_DOWN(entry->memory_last, PCICFG_MEMGRAN))));
3635 	/*
3636 	 * Since this is a bridge, the rest of this range will
3637 	 * be responded to by the bridge.  We have to round up
3638 	 * so no other device claims it.
3639 	 */
3640 	if ((length = (PCICFG_ROUND_UP(entry->memory_last, PCICFG_MEMGRAN)
3641 	    - entry->memory_last)) > 0) {
3642 		(void) pcicfg_get_mem(entry, length, NULL);
3643 		DEBUG1("Added [0x%x]at the top of the bridge (mem)\n", length);
3644 	}
3645 
3646 	/*
3647 	 * Program the PF memory limit register with the end of the memory range
3648 	 */
3649 
3650 	DEBUG1("DOWN ROUNDED ===>[0x%x]\n",
3651 	    PCICFG_ROUND_DOWN(entry->pf_memory_last, PCICFG_MEMGRAN));
3652 
3653 	pci_config_put16(handle, PCI_BCNF_PF_LIMIT_LOW,
3654 	    PCICFG_HIWORD(PCICFG_LOADDR(PCICFG_ROUND_DOWN(
3655 	    entry->pf_memory_last, PCICFG_MEMGRAN))));
3656 	pci_config_put32(handle, PCI_BCNF_PF_LIMIT_HIGH, PCICFG_HIADDR(
3657 	    PCICFG_ROUND_DOWN(entry->pf_memory_last, PCICFG_MEMGRAN)));
3658 	if ((length = (PCICFG_ROUND_UP(entry->pf_memory_last, PCICFG_MEMGRAN)
3659 	    - entry->pf_memory_last)) > 0) {
3660 		(void) pcicfg_get_pf_mem(entry, length, NULL);
3661 		DEBUG1("Added [0x%x]at the top of the bridge (PF mem)\n",
3662 		    length);
3663 	}
3664 
3665 	/*
3666 	 * Program the I/O limit register with the end of the I/O range
3667 	 */
3668 	pci_config_put8(handle, PCI_BCNF_IO_LIMIT_LOW,
3669 	    PCICFG_HIBYTE(PCICFG_LOWORD(
3670 	    PCICFG_LOADDR(PCICFG_ROUND_DOWN(entry->io_last, PCICFG_IOGRAN)))));
3671 
3672 	pci_config_put16(handle, PCI_BCNF_IO_LIMIT_HI, PCICFG_HIWORD(
3673 	    PCICFG_LOADDR(PCICFG_ROUND_DOWN(entry->io_last, PCICFG_IOGRAN))));
3674 
3675 	/*
3676 	 * Same as above for I/O space. Since this is a
3677 	 * bridge, the rest of this range will be responded
3678 	 * to by the bridge.  We have to round up so no
3679 	 * other device claims it.
3680 	 */
3681 	if ((length = (PCICFG_ROUND_UP(entry->io_last, PCICFG_IOGRAN)
3682 	    - entry->io_last)) > 0) {
3683 		(void) pcicfg_get_io(entry, length, NULL);
3684 		DEBUG1("Added [0x%x]at the top of the bridge (I/O)\n", length);
3685 	}
3686 }
3687 
3688 static int
3689 pcicfg_probe_children(dev_info_t *parent, uint_t bus, uint_t device,
3690     uint_t func, uint_t *highest_bus, pcicfg_flags_t flags, boolean_t is_pcie)
3691 {
3692 	dev_info_t		*new_child;
3693 	ddi_acc_handle_t	config_handle;
3694 	uint8_t			header_type, pcie_dev = 0;
3695 	int			ret = PCICFG_FAILURE;
3696 
3697 	/*
3698 	 * This node will be put immediately below
3699 	 * "parent". Allocate a blank device node.  It will either
3700 	 * be filled in or freed up based on further probing.
3701 	 */
3702 
3703 	ndi_devi_alloc_sleep(parent, DEVI_PSEUDO_NEXNAME,
3704 	    (pnode_t)DEVI_SID_NODEID, &new_child);
3705 
3706 	if (pcicfg_add_config_reg(new_child, bus, device, func)
3707 	    != DDI_SUCCESS) {
3708 		DEBUG0("pcicfg_probe_children():Failed to add candidate REG\n");
3709 		goto failedconfig;
3710 	}
3711 
3712 	if ((ret = pcicfg_config_setup(new_child, &config_handle))
3713 	    != PCICFG_SUCCESS) {
3714 		if (ret == PCICFG_NODEVICE) {
3715 			(void) ndi_devi_free(new_child);
3716 			return (ret);
3717 		}
3718 		DEBUG0("pcicfg_probe_children():"
3719 		"Failed to setup config space\n");
3720 		goto failedconfig;
3721 	}
3722 
3723 	if (is_pcie)
3724 		(void) pcie_init_bus(new_child, PCI_GETBDF(bus, device, func),
3725 		    PCIE_BUS_INITIAL);
3726 
3727 	/*
3728 	 * As soon as we have access to config space,
3729 	 * turn off device. It will get turned on
3730 	 * later (after memory is assigned).
3731 	 */
3732 	(void) pcicfg_device_off(config_handle);
3733 
3734 	/* check if we are PCIe device */
3735 	if (pcicfg_pcie_dev(new_child, config_handle) == DDI_SUCCESS) {
3736 		DEBUG0("PCIe device detected\n");
3737 		pcie_dev = 1;
3738 	}
3739 
3740 	/*
3741 	 * Set 1275 properties common to all devices
3742 	 */
3743 	if (pcicfg_set_standard_props(new_child, config_handle, pcie_dev)
3744 	    != PCICFG_SUCCESS) {
3745 		DEBUG0("Failed to set standard properties\n");
3746 		goto failedchild;
3747 	}
3748 
3749 	/*
3750 	 * Child node properties  NOTE: Both for PCI-PCI bridge and child node
3751 	 */
3752 	if (pcicfg_set_childnode_props(new_child, config_handle, pcie_dev)
3753 	    != PCICFG_SUCCESS) {
3754 		goto failedchild;
3755 	}
3756 
3757 	header_type = pci_config_get8(config_handle, PCI_CONF_HEADER);
3758 
3759 	/*
3760 	 * If this is not a multi-function card only probe function zero.
3761 	 */
3762 	if ((!(header_type & PCI_HEADER_MULTI)) && (func != 0)) {
3763 
3764 		ret = PCICFG_NODEVICE;
3765 		goto failedchild;
3766 	}
3767 
3768 	/*
3769 	 * Attach the child to its parent
3770 	 */
3771 	(void) i_ndi_config_node(new_child, DS_LINKED, 0);
3772 
3773 	DEVI_SET_PCI(new_child);
3774 
3775 	if ((header_type & PCI_HEADER_TYPE_M) == PCI_HEADER_PPB) {
3776 
3777 		DEBUG3("--Bridge found bus [0x%x] device[0x%x] func [0x%x]\n",
3778 		    bus, device, func);
3779 
3780 		/* Only support read-only probe for leaf device */
3781 		if (flags & PCICFG_FLAG_READ_ONLY)
3782 			goto failedchild;
3783 
3784 		ret = pcicfg_probe_bridge(new_child, config_handle, bus,
3785 		    highest_bus, is_pcie);
3786 		if (ret != PCICFG_SUCCESS) {
3787 			(void) pcicfg_free_bridge_resources(new_child);
3788 			goto failedchild;
3789 		}
3790 
3791 	} else {
3792 
3793 		DEBUG3("--Leaf device found bus [0x%x] device"
3794 		    "[0x%x] func [0x%x]\n", bus, device, func);
3795 
3796 		if (flags & PCICFG_FLAG_READ_ONLY) {
3797 			/*
3798 			 * with read-only probe, don't do any resource
3799 			 * allocation, just read the BARs and update props.
3800 			 */
3801 			ret = pcicfg_populate_props_from_bar(new_child,
3802 			    config_handle);
3803 			if (ret != PCICFG_SUCCESS)
3804 				goto failedchild;
3805 
3806 			/*
3807 			 * now allocate the resources, just remove the
3808 			 * resources from the parent busra pool.
3809 			 */
3810 			ret = pcicfg_device_assign_readonly(new_child);
3811 			if (ret != PCICFG_SUCCESS) {
3812 				(void) pcicfg_free_device_resources(new_child);
3813 				goto failedchild;
3814 			}
3815 
3816 		} else {
3817 			/*
3818 			 * update "reg" property by sizing the BARs.
3819 			 */
3820 			ret = pcicfg_populate_reg_props(new_child,
3821 			    config_handle);
3822 			if (ret != PCICFG_SUCCESS)
3823 				goto failedchild;
3824 
3825 			/* now allocate & program the resources */
3826 			ret = pcicfg_device_assign(new_child);
3827 			if (ret != PCICFG_SUCCESS) {
3828 				(void) pcicfg_free_device_resources(new_child);
3829 				goto failedchild;
3830 			}
3831 		}
3832 
3833 		(void) ndi_devi_bind_driver(new_child, 0);
3834 	}
3835 
3836 	(void) pcicfg_config_teardown(&config_handle);
3837 
3838 	/*
3839 	 * Properties have been setted up, so initialize the remaining
3840 	 * bus_t fields
3841 	 */
3842 	if (is_pcie)
3843 		(void) pcie_init_bus(new_child, 0, PCIE_BUS_FINAL);
3844 
3845 	return (PCICFG_SUCCESS);
3846 
3847 failedchild:
3848 	/*
3849 	 * XXX check if it should be taken offline (if online)
3850 	 */
3851 	(void) pcicfg_config_teardown(&config_handle);
3852 
3853 	if (is_pcie)
3854 		pcie_fini_bus(new_child, PCIE_BUS_FINAL);
3855 
3856 failedconfig:
3857 
3858 	(void) ndi_devi_free(new_child);
3859 	return (ret);
3860 }
3861 
3862 /*
3863  * Sizing the BARs and update "reg" property
3864  */
3865 static int
3866 pcicfg_populate_reg_props(dev_info_t *new_child, ddi_acc_handle_t config_handle)
3867 {
3868 	int		i;
3869 	uint32_t	request;
3870 
3871 	i = PCI_CONF_BASE0;
3872 
3873 	while (i <= PCI_CONF_BASE5) {
3874 
3875 		pci_config_put32(config_handle, i, 0xffffffff);
3876 
3877 		request = pci_config_get32(config_handle, i);
3878 		/*
3879 		 * If its a zero length, don't do
3880 		 * any programming.
3881 		 */
3882 		if (request != 0) {
3883 			/*
3884 			 * Add to the "reg" property
3885 			 */
3886 			if (pcicfg_update_reg_prop(new_child,
3887 			    request, i) != PCICFG_SUCCESS) {
3888 				goto failedchild;
3889 			}
3890 		} else {
3891 			DEBUG1("BASE register [0x%x] asks for "
3892 			    "[0x0]=[0x0](32)\n", i);
3893 			i += 4;
3894 			continue;
3895 		}
3896 
3897 		/*
3898 		 * Increment by eight if it is 64 bit address space
3899 		 */
3900 		if ((PCI_BASE_TYPE_M & request) == PCI_BASE_TYPE_ALL) {
3901 			DEBUG3("BASE register [0x%x] asks for "
3902 			    "[0x%x]=[0x%x] (64)\n",
3903 			    i, request, (~(PCI_BASE_M_ADDR_M & request))+1);
3904 			i += 8;
3905 		} else {
3906 			DEBUG3("BASE register [0x%x] asks for "
3907 			    "[0x%x]=[0x%x](32)\n",
3908 			    i, request, (~(PCI_BASE_M_ADDR_M & request))+1);
3909 			i += 4;
3910 		}
3911 	}
3912 
3913 	/*
3914 	 * Get the ROM size and create register for it
3915 	 */
3916 	pci_config_put32(config_handle, PCI_CONF_ROM, 0xfffffffe);
3917 
3918 	request = pci_config_get32(config_handle, PCI_CONF_ROM);
3919 	/*
3920 	 * If its a zero length, don't do
3921 	 * any programming.
3922 	 */
3923 
3924 	if (request != 0) {
3925 		DEBUG3("BASE register [0x%x] asks for [0x%x]=[0x%x]\n",
3926 		    PCI_CONF_ROM, request,
3927 		    (~(PCI_BASE_ROM_ADDR_M & request)) + 1);
3928 		/*
3929 		 * Add to the "reg" property
3930 		 */
3931 		if (pcicfg_update_reg_prop(new_child, request, PCI_CONF_ROM)
3932 		    != PCICFG_SUCCESS) {
3933 			goto failedchild;
3934 		}
3935 	}
3936 
3937 	return (PCICFG_SUCCESS);
3938 
3939 failedchild:
3940 	return (PCICFG_FAILURE);
3941 }
3942 
3943 /*
3944  * Read the BARs and update properties. Used in virtual hotplug.
3945  */
3946 static int
3947 pcicfg_populate_props_from_bar(dev_info_t *new_child,
3948     ddi_acc_handle_t config_handle)
3949 {
3950 	uint32_t request, base, base_hi, size;
3951 	int i;
3952 
3953 	i = PCI_CONF_BASE0;
3954 
3955 	while (i <= PCI_CONF_BASE5) {
3956 		/*
3957 		 * determine the size of the address space
3958 		 */
3959 		base = pci_config_get32(config_handle, i);
3960 		pci_config_put32(config_handle, i, 0xffffffff);
3961 		request = pci_config_get32(config_handle, i);
3962 		pci_config_put32(config_handle, i, base);
3963 
3964 		/*
3965 		 * If its a zero length, don't do any programming.
3966 		 */
3967 		if (request != 0) {
3968 			/*
3969 			 * Add to the "reg" property
3970 			 */
3971 			if (pcicfg_update_reg_prop(new_child,
3972 			    request, i) != PCICFG_SUCCESS) {
3973 				goto failedchild;
3974 			}
3975 
3976 			if ((PCI_BASE_SPACE_IO & request) == 0 &&
3977 			    (PCI_BASE_TYPE_M & request) == PCI_BASE_TYPE_ALL) {
3978 				base_hi = pci_config_get32(config_handle, i+4);
3979 			} else {
3980 				base_hi = 0;
3981 			}
3982 			/*
3983 			 * Add to "assigned-addresses" property
3984 			 */
3985 			size = (~(PCI_BASE_M_ADDR_M & request))+1;
3986 			if (pcicfg_update_assigned_prop_value(new_child,
3987 			    size, base, base_hi, i) != PCICFG_SUCCESS) {
3988 				goto failedchild;
3989 			}
3990 		} else {
3991 			DEBUG1("BASE register [0x%x] asks for [0x0]=[0x0]"
3992 			    "(32)\n", i);
3993 			i += 4;
3994 			continue;
3995 		}
3996 
3997 		/*
3998 		 * Increment by eight if it is 64 bit address space
3999 		 */
4000 		if ((PCI_BASE_TYPE_M & request) == PCI_BASE_TYPE_ALL) {
4001 			DEBUG3("BASE register [0x%x] asks for [0x%x]=[0x%x]"
4002 			    "(64)\n", i, request,
4003 			    (~(PCI_BASE_M_ADDR_M & request)) + 1);
4004 			i += 8;
4005 		} else {
4006 			DEBUG3("BASE register [0x%x] asks for [0x%x]=[0x%x]"
4007 			    "(32)\n", i, request,
4008 			    (~(PCI_BASE_M_ADDR_M & request)) + 1);
4009 			i += 4;
4010 		}
4011 	}
4012 
4013 	/*
4014 	 * Get the ROM size and create register for it
4015 	 */
4016 	base = pci_config_get32(config_handle, PCI_CONF_ROM);
4017 	pci_config_put32(config_handle, PCI_CONF_ROM, 0xfffffffe);
4018 	request = pci_config_get32(config_handle, PCI_CONF_ROM);
4019 	pci_config_put32(config_handle, PCI_CONF_ROM, base);
4020 
4021 	/*
4022 	 * If its a zero length, don't do
4023 	 * any programming.
4024 	 */
4025 	if (request != 0) {
4026 		DEBUG3("BASE register [0x%x] asks for [0x%x]=[0x%x]\n",
4027 		    PCI_CONF_ROM, request,
4028 		    (~(PCI_BASE_ROM_ADDR_M & request)) + 1);
4029 		/*
4030 		 * Add to the "reg" property
4031 		 */
4032 		if (pcicfg_update_reg_prop(new_child, request, PCI_CONF_ROM)
4033 		    != PCICFG_SUCCESS) {
4034 			goto failedchild;
4035 		}
4036 		/*
4037 		 * Add to "assigned-addresses" property
4038 		 */
4039 		size = (~(PCI_BASE_ROM_ADDR_M & request))+1;
4040 		if (pcicfg_update_assigned_prop_value(new_child, size,
4041 		    base, 0, PCI_CONF_ROM) != PCICFG_SUCCESS) {
4042 			goto failedchild;
4043 		}
4044 	}
4045 
4046 	return (PCICFG_SUCCESS);
4047 
4048 failedchild:
4049 	return (PCICFG_FAILURE);
4050 }
4051 
4052 static int
4053 pcicfg_probe_bridge(dev_info_t *new_child, ddi_acc_handle_t h, uint_t bus,
4054     uint_t *highest_bus, boolean_t is_pcie)
4055 {
4056 	uint64_t next_bus;
4057 	uint_t new_bus, num_slots;
4058 	ndi_ra_request_t req;
4059 	int rval, i, j;
4060 	uint64_t mem_answer, io_answer, mem_base, io_base, mem_alen, io_alen;
4061 	uint64_t pf_mem_answer, pf_mem_base, pf_mem_alen;
4062 	uint64_t mem_size, io_size, pf_mem_size;
4063 	uint64_t mem_end, pf_mem_end, io_end;
4064 	uint64_t round_answer, round_len;
4065 	ppb_ranges_t range[PCICFG_RANGE_LEN];
4066 	int bus_range[2];
4067 	pcicfg_phdl_t phdl;
4068 	int count;
4069 	uint64_t pcibus_base, pcibus_alen;
4070 	uint64_t max_bus;
4071 	uint8_t pcie_device_type = 0;
4072 	uint_t pf_mem_supported = 0;
4073 	dev_info_t *new_device;
4074 	int trans_device;
4075 	int ari_mode = B_FALSE;
4076 	int max_function = PCI_MAX_FUNCTIONS;
4077 
4078 	io_answer = io_base = io_alen = io_size = 0;
4079 	pf_mem_answer = pf_mem_base = pf_mem_size = pf_mem_alen = 0;
4080 
4081 	/*
4082 	 * Set "device_type" to "pci", the actual type will be set later
4083 	 * by pcicfg_set_busnode_props() below. This is needed as the
4084 	 * pcicfg_ra_free() below would update "available" property based
4085 	 * on "device_type".
4086 	 *
4087 	 * This code can be removed later after PCI configurator is changed
4088 	 * to use PCIRM, which automatically update properties upon allocation
4089 	 * and free, at that time we'll be able to remove the code inside
4090 	 * ndi_ra_alloc/free() which currently updates "available" property
4091 	 * for pci/pcie devices in pcie fabric.
4092 	 */
4093 	if (ndi_prop_update_string(DDI_DEV_T_NONE, new_child,
4094 	    "device_type", "pci") != DDI_SUCCESS) {
4095 		DEBUG0("Failed to set \"device_type\" props\n");
4096 		return (PCICFG_FAILURE);
4097 	}
4098 
4099 	/*
4100 	 * setup resource maps for the bridge node
4101 	 */
4102 	if (ndi_ra_map_setup(new_child, NDI_RA_TYPE_PCI_BUSNUM)
4103 	    == NDI_FAILURE) {
4104 		DEBUG0("Can not setup resource map - NDI_RA_TYPE_PCI_BUSNUM\n");
4105 		rval = PCICFG_FAILURE;
4106 		goto cleanup;
4107 	}
4108 	if (ndi_ra_map_setup(new_child, NDI_RA_TYPE_MEM) == NDI_FAILURE) {
4109 		DEBUG0("Can not setup resource map - NDI_RA_TYPE_MEM\n");
4110 		rval = PCICFG_FAILURE;
4111 		goto cleanup;
4112 	}
4113 	if (ndi_ra_map_setup(new_child, NDI_RA_TYPE_IO) == NDI_FAILURE) {
4114 		DEBUG0("Can not setup resource map - NDI_RA_TYPE_IO\n");
4115 		rval = PCICFG_FAILURE;
4116 		goto cleanup;
4117 	}
4118 	if (ndi_ra_map_setup(new_child, NDI_RA_TYPE_PCI_PREFETCH_MEM) ==
4119 	    NDI_FAILURE) {
4120 		DEBUG0("Can not setup resource map -"
4121 		    " NDI_RA_TYPE_PCI_PREFETCH_MEM\n");
4122 		rval = PCICFG_FAILURE;
4123 		goto cleanup;
4124 	}
4125 
4126 	/*
4127 	 * Allocate bus range pool for the bridge.
4128 	 */
4129 	bzero((caddr_t)&req, sizeof (ndi_ra_request_t));
4130 	req.ra_flags = (NDI_RA_ALLOC_BOUNDED | NDI_RA_ALLOC_PARTIAL_OK);
4131 	req.ra_boundbase = 0;
4132 	req.ra_boundlen = req.ra_len = (PCI_MAX_BUS_NUM -1);
4133 	req.ra_align_mask = 0;  /* no alignment needed */
4134 
4135 	rval = ndi_ra_alloc(ddi_get_parent(new_child), &req,
4136 	    &pcibus_base, &pcibus_alen, NDI_RA_TYPE_PCI_BUSNUM, NDI_RA_PASS);
4137 
4138 	if (rval != NDI_SUCCESS) {
4139 		if (rval == NDI_RA_PARTIAL_REQ) {
4140 			/*EMPTY*/
4141 			DEBUG0("NDI_RA_PARTIAL_REQ returned for bus range\n");
4142 		} else {
4143 			DEBUG0(
4144 			    "Failed to allocate bus range for bridge\n");
4145 			rval = PCICFG_NORESRC;
4146 			goto cleanup;
4147 		}
4148 	}
4149 
4150 	DEBUG2("Bus Range Allocated [base=%d] [len=%d]\n",
4151 	    pcibus_base, pcibus_alen);
4152 
4153 	/*
4154 	 * Put available bus range into the pool.
4155 	 * Take the first one for this bridge to use and don't give
4156 	 * to child.
4157 	 */
4158 	(void) ndi_ra_free(new_child, pcibus_base+1, pcibus_alen-1,
4159 	    NDI_RA_TYPE_PCI_BUSNUM, NDI_RA_PASS);
4160 
4161 	next_bus = pcibus_base;
4162 	max_bus = pcibus_base + pcibus_alen - 1;
4163 
4164 	new_bus = next_bus;
4165 
4166 	DEBUG1("NEW bus found  ->[%d]\n", new_bus);
4167 
4168 	/* Keep track of highest bus for subordinate bus programming */
4169 	*highest_bus = new_bus;
4170 
4171 	/*
4172 	 * Allocate (non-prefetchable) Memory Space for Bridge
4173 	 */
4174 	bzero((caddr_t)&req, sizeof (ndi_ra_request_t));
4175 	req.ra_flags = (NDI_RA_ALLOC_BOUNDED | NDI_RA_ALLOC_PARTIAL_OK);
4176 	req.ra_boundbase = 0;
4177 	/*
4178 	 * limit the boundlen,len to a 32b quantity. It should be Ok to
4179 	 * lose alignment-based-size of resource due to this.
4180 	 */
4181 	req.ra_boundlen = PCICFG_4GIG_LIMIT;
4182 	req.ra_len = PCICFG_4GIG_LIMIT; /* Get as big as possible */
4183 	req.ra_align_mask =
4184 	    PCICFG_MEMGRAN - 1; /* 1M alignment on memory space */
4185 
4186 	rval = ndi_ra_alloc(ddi_get_parent(new_child), &req,
4187 	    &mem_answer, &mem_alen,  NDI_RA_TYPE_MEM, NDI_RA_PASS);
4188 
4189 	if (rval != NDI_SUCCESS) {
4190 		if (rval == NDI_RA_PARTIAL_REQ) {
4191 			/*EMPTY*/
4192 			DEBUG0("NDI_RA_PARTIAL_REQ returned\n");
4193 		} else {
4194 			DEBUG0(
4195 			    "Failed to allocate memory for bridge\n");
4196 			rval = PCICFG_NORESRC;
4197 			goto cleanup;
4198 		}
4199 	}
4200 
4201 	DEBUG3("Bridge Memory Allocated [0x%x.%x] len [0x%x]\n",
4202 	    PCICFG_HIADDR(mem_answer),
4203 	    PCICFG_LOADDR(mem_answer),
4204 	    mem_alen);
4205 
4206 	/*
4207 	 * Put available memory into the pool.
4208 	 */
4209 	(void) ndi_ra_free(new_child, mem_answer, mem_alen, NDI_RA_TYPE_MEM,
4210 	    NDI_RA_PASS);
4211 
4212 	mem_base = mem_answer;
4213 
4214 	/*
4215 	 * Allocate I/O Space for Bridge
4216 	 */
4217 	bzero((caddr_t)&req, sizeof (ndi_ra_request_t));
4218 	req.ra_align_mask = PCICFG_IOGRAN - 1; /* 4k alignment */
4219 	req.ra_boundbase = 0;
4220 	req.ra_boundlen = PCICFG_4GIG_LIMIT;
4221 	req.ra_flags = (NDI_RA_ALLOC_BOUNDED | NDI_RA_ALLOC_PARTIAL_OK);
4222 	req.ra_len = PCICFG_4GIG_LIMIT; /* Get as big as possible */
4223 
4224 	rval = ndi_ra_alloc(ddi_get_parent(new_child), &req, &io_answer,
4225 	    &io_alen, NDI_RA_TYPE_IO, NDI_RA_PASS);
4226 
4227 	if (rval != NDI_SUCCESS) {
4228 		if (rval == NDI_RA_PARTIAL_REQ) {
4229 			/*EMPTY*/
4230 			DEBUG0("NDI_RA_PARTIAL_REQ returned\n");
4231 		} else {
4232 			DEBUG0("Failed to allocate io space for bridge\n");
4233 			/* i/o space is an optional requirement so continue */
4234 		}
4235 	}
4236 
4237 	DEBUG3("Bridge IO Space Allocated [0x%x.%x] len [0x%x]\n",
4238 	    PCICFG_HIADDR(io_answer), PCICFG_LOADDR(io_answer), io_alen);
4239 
4240 	/*
4241 	 * Put available I/O into the pool.
4242 	 */
4243 	(void) ndi_ra_free(new_child, io_answer, io_alen, NDI_RA_TYPE_IO,
4244 	    NDI_RA_PASS);
4245 
4246 	io_base = io_answer;
4247 
4248 	/*
4249 	 * Check if the bridge supports Prefetchable memory range.
4250 	 * If it does, then we setup PF memory range for the bridge.
4251 	 * Otherwise, we skip the step of setting up PF memory
4252 	 * range for it. This could cause config operation to
4253 	 * fail if any devices under the bridge need PF memory.
4254 	 */
4255 	/* write a non zero value to the PF BASE register */
4256 	pci_config_put16(h, PCI_BCNF_PF_BASE_LOW, 0xfff0);
4257 	/* if the read returns zero then PF range is not supported */
4258 	if (pci_config_get16(h, PCI_BCNF_PF_BASE_LOW) == 0) {
4259 		/* bridge doesn't support PF memory range */
4260 		goto pf_setup_end;
4261 	} else {
4262 		pf_mem_supported = 1;
4263 		/* reset the PF BASE register */
4264 		pci_config_put16(h, PCI_BCNF_PF_BASE_LOW, 0);
4265 	}
4266 
4267 	/*
4268 	 * Bridge supports PF mem range; Allocate PF Memory Space for it.
4269 	 *
4270 	 * Note: Both non-prefetchable and prefetchable memory space
4271 	 * allocations are made within 32bit space. Currently, BIOSs
4272 	 * allocate device memory for PCI devices within the 32bit space
4273 	 * so this will not be a problem.
4274 	 */
4275 	bzero((caddr_t)&req, sizeof (ndi_ra_request_t));
4276 	req.ra_flags = NDI_RA_ALLOC_PARTIAL_OK | NDI_RA_ALLOC_BOUNDED;
4277 	req.ra_boundbase = 0;
4278 	req.ra_len = PCICFG_4GIG_LIMIT; /* Get as big as possible */
4279 	req.ra_align_mask =
4280 	    PCICFG_MEMGRAN - 1; /* 1M alignment on memory space */
4281 
4282 	rval = ndi_ra_alloc(ddi_get_parent(new_child), &req,
4283 	    &pf_mem_answer, &pf_mem_alen,  NDI_RA_TYPE_PCI_PREFETCH_MEM,
4284 	    NDI_RA_PASS);
4285 
4286 	if (rval != NDI_SUCCESS) {
4287 		if (rval == NDI_RA_PARTIAL_REQ) {
4288 			/*EMPTY*/
4289 			DEBUG0("NDI_RA_PARTIAL_REQ returned\n");
4290 		} else {
4291 			DEBUG0(
4292 			    "Failed to allocate PF memory for bridge\n");
4293 			/* PF mem is an optional requirement so continue */
4294 		}
4295 	}
4296 
4297 	DEBUG3("Bridge PF Memory Allocated [0x%x.%x] len [0x%x]\n",
4298 	    PCICFG_HIADDR(pf_mem_answer),
4299 	    PCICFG_LOADDR(pf_mem_answer),
4300 	    pf_mem_alen);
4301 
4302 	/*
4303 	 * Put available PF memory into the pool.
4304 	 */
4305 	(void) ndi_ra_free(new_child, pf_mem_answer, pf_mem_alen,
4306 	    NDI_RA_TYPE_PCI_PREFETCH_MEM, NDI_RA_PASS);
4307 
4308 	pf_mem_base = pf_mem_answer;
4309 
4310 	/*
4311 	 * Program the PF memory base register with the
4312 	 * start of the memory range
4313 	 */
4314 	pci_config_put16(h, PCI_BCNF_PF_BASE_LOW,
4315 	    PCICFG_HIWORD(PCICFG_LOADDR(pf_mem_answer)));
4316 	pci_config_put32(h, PCI_BCNF_PF_BASE_HIGH,
4317 	    PCICFG_HIADDR(pf_mem_answer));
4318 
4319 	/*
4320 	 * Program the PF memory limit register with the
4321 	 * end of the memory range.
4322 	 */
4323 	pci_config_put16(h, PCI_BCNF_PF_LIMIT_LOW,
4324 	    PCICFG_HIWORD(PCICFG_LOADDR(
4325 	    PCICFG_ROUND_DOWN((pf_mem_answer + pf_mem_alen),
4326 	    PCICFG_MEMGRAN) - 1)));
4327 	pci_config_put32(h, PCI_BCNF_PF_LIMIT_HIGH,
4328 	    PCICFG_HIADDR(PCICFG_ROUND_DOWN((pf_mem_answer + pf_mem_alen),
4329 	    PCICFG_MEMGRAN) - 1));
4330 
4331 	/*
4332 	 * Allocate the chunk of PF memory (if any) not programmed into the
4333 	 * bridge because of the round down.
4334 	 */
4335 	if (PCICFG_ROUND_DOWN((pf_mem_answer + pf_mem_alen), PCICFG_MEMGRAN)
4336 	    != (pf_mem_answer + pf_mem_alen)) {
4337 		DEBUG0("Need to allocate Memory round off chunk\n");
4338 		bzero((caddr_t)&req, sizeof (ndi_ra_request_t));
4339 		req.ra_flags = NDI_RA_ALLOC_SPECIFIED;
4340 		req.ra_addr = PCICFG_ROUND_DOWN((pf_mem_answer + pf_mem_alen),
4341 		    PCICFG_MEMGRAN);
4342 		req.ra_len =  (pf_mem_answer + pf_mem_alen) -
4343 		    (PCICFG_ROUND_DOWN((pf_mem_answer + pf_mem_alen),
4344 		    PCICFG_MEMGRAN));
4345 
4346 		(void) ndi_ra_alloc(new_child, &req,
4347 		    &round_answer, &round_len,  NDI_RA_TYPE_PCI_PREFETCH_MEM,
4348 		    NDI_RA_PASS);
4349 	}
4350 
4351 pf_setup_end:
4352 
4353 	/*
4354 	 * Program the memory base register with the
4355 	 * start of the memory range
4356 	 */
4357 	pci_config_put16(h, PCI_BCNF_MEM_BASE,
4358 	    PCICFG_HIWORD(PCICFG_LOADDR(mem_answer)));
4359 
4360 	/*
4361 	 * Program the memory limit register with the
4362 	 * end of the memory range.
4363 	 */
4364 
4365 	pci_config_put16(h, PCI_BCNF_MEM_LIMIT,
4366 	    PCICFG_HIWORD(PCICFG_LOADDR(
4367 	    PCICFG_ROUND_DOWN((mem_answer + mem_alen), PCICFG_MEMGRAN) - 1)));
4368 
4369 	/*
4370 	 * Allocate the chunk of memory (if any) not programmed into the
4371 	 * bridge because of the round down.
4372 	 */
4373 	if (PCICFG_ROUND_DOWN((mem_answer + mem_alen), PCICFG_MEMGRAN)
4374 	    != (mem_answer + mem_alen)) {
4375 		DEBUG0("Need to allocate Memory round off chunk\n");
4376 		bzero((caddr_t)&req, sizeof (ndi_ra_request_t));
4377 		req.ra_flags = NDI_RA_ALLOC_SPECIFIED;
4378 		req.ra_addr = PCICFG_ROUND_DOWN((mem_answer + mem_alen),
4379 		    PCICFG_MEMGRAN);
4380 		req.ra_len =  (mem_answer + mem_alen) -
4381 		    (PCICFG_ROUND_DOWN((mem_answer + mem_alen),
4382 		    PCICFG_MEMGRAN));
4383 
4384 		(void) ndi_ra_alloc(new_child, &req,
4385 		    &round_answer, &round_len,  NDI_RA_TYPE_MEM, NDI_RA_PASS);
4386 	}
4387 
4388 	/*
4389 	 * Program the I/O Space Base
4390 	 */
4391 	pci_config_put8(h, PCI_BCNF_IO_BASE_LOW,
4392 	    PCICFG_HIBYTE(PCICFG_LOWORD(
4393 	    PCICFG_LOADDR(io_answer))));
4394 
4395 	pci_config_put16(h, PCI_BCNF_IO_BASE_HI,
4396 	    PCICFG_HIWORD(PCICFG_LOADDR(io_answer)));
4397 
4398 	/*
4399 	 * Program the I/O Space Limit
4400 	 */
4401 	pci_config_put8(h, PCI_BCNF_IO_LIMIT_LOW,
4402 	    PCICFG_HIBYTE(PCICFG_LOWORD(
4403 	    PCICFG_LOADDR(PCICFG_ROUND_DOWN(io_answer + io_alen,
4404 	    PCICFG_IOGRAN)))) - 1);
4405 
4406 	pci_config_put16(h, PCI_BCNF_IO_LIMIT_HI,
4407 	    PCICFG_HIWORD(PCICFG_LOADDR(
4408 	    PCICFG_ROUND_DOWN(io_answer + io_alen, PCICFG_IOGRAN)))
4409 	    - 1);
4410 
4411 	/*
4412 	 * Allocate the chunk of I/O (if any) not programmed into the
4413 	 * bridge because of the round down.
4414 	 */
4415 	if (PCICFG_ROUND_DOWN((io_answer + io_alen), PCICFG_IOGRAN)
4416 	    != (io_answer + io_alen)) {
4417 		DEBUG0("Need to allocate I/O round off chunk\n");
4418 		bzero((caddr_t)&req, sizeof (ndi_ra_request_t));
4419 		req.ra_flags = NDI_RA_ALLOC_SPECIFIED;
4420 		req.ra_addr = PCICFG_ROUND_DOWN((io_answer + io_alen),
4421 		    PCICFG_IOGRAN);
4422 		req.ra_len =  (io_answer + io_alen) -
4423 		    (PCICFG_ROUND_DOWN((io_answer + io_alen),
4424 		    PCICFG_IOGRAN));
4425 
4426 		(void) ndi_ra_alloc(new_child, &req,
4427 		    &round_answer, &round_len,  NDI_RA_TYPE_IO, NDI_RA_PASS);
4428 	}
4429 
4430 	(void) pcicfg_set_bus_numbers(h, bus, new_bus, max_bus);
4431 
4432 	/*
4433 	 * Setup "ranges" and "bus-range" properties before onlining
4434 	 * the bridge.
4435 	 */
4436 	bzero((caddr_t)range, sizeof (ppb_ranges_t) * PCICFG_RANGE_LEN);
4437 
4438 	range[0].child_high = range[0].parent_high |= (PCI_REG_REL_M |
4439 	    PCI_ADDR_IO);
4440 	range[0].child_low = range[0].parent_low = io_base;
4441 	range[1].child_high = range[1].parent_high |=
4442 	    (PCI_REG_REL_M | PCI_ADDR_MEM32);
4443 	range[1].child_low = range[1].parent_low = mem_base;
4444 	range[2].child_high = range[2].parent_high |=
4445 	    (PCI_REG_REL_M | PCI_ADDR_MEM64 | PCI_REG_PF_M);
4446 	range[2].child_low = range[2].parent_low = pf_mem_base;
4447 
4448 	range[0].size_low = io_alen;
4449 	(void) pcicfg_update_ranges_prop(new_child, &range[0]);
4450 	range[1].size_low = mem_alen;
4451 	(void) pcicfg_update_ranges_prop(new_child, &range[1]);
4452 	range[2].size_low = pf_mem_alen;
4453 	(void) pcicfg_update_ranges_prop(new_child, &range[2]);
4454 
4455 	bus_range[0] = new_bus;
4456 	bus_range[1] = max_bus;
4457 	(void) ndi_prop_update_int_array(DDI_DEV_T_NONE, new_child,
4458 	    "bus-range", bus_range, 2);
4459 
4460 	/*
4461 	 * Reset the secondary bus
4462 	 */
4463 	pci_config_put16(h, PCI_BCNF_BCNTRL,
4464 	    pci_config_get16(h, PCI_BCNF_BCNTRL) | 0x40);
4465 
4466 	drv_usecwait(100);
4467 
4468 	pci_config_put16(h, PCI_BCNF_BCNTRL,
4469 	    pci_config_get16(h, PCI_BCNF_BCNTRL) & ~0x40);
4470 
4471 	/*
4472 	 * Clear status bits
4473 	 */
4474 	pci_config_put16(h, PCI_BCNF_SEC_STATUS, 0xffff);
4475 
4476 	/*
4477 	 * Needs to be set to this value
4478 	 */
4479 	pci_config_put8(h, PCI_CONF_ILINE, 0xf);
4480 
4481 	/* check our device_type as defined by Open Firmware */
4482 	if (pcicfg_pcie_device_type(new_child, h) == DDI_SUCCESS)
4483 		pcie_device_type = 1;
4484 
4485 	/*
4486 	 * Set bus properties
4487 	 */
4488 	if (pcicfg_set_busnode_props(new_child, pcie_device_type)
4489 	    != PCICFG_SUCCESS) {
4490 		DEBUG0("Failed to set busnode props\n");
4491 		rval = PCICFG_FAILURE;
4492 		goto cleanup;
4493 	}
4494 
4495 	(void) pcicfg_device_on(h);
4496 
4497 	if (is_pcie)
4498 		(void) pcie_init_bus(new_child, 0, PCIE_BUS_FINAL);
4499 	if (ndi_devi_online(new_child, NDI_NO_EVENT|NDI_CONFIG)
4500 	    != NDI_SUCCESS) {
4501 		DEBUG0("Unable to online bridge\n");
4502 		rval = PCICFG_FAILURE;
4503 		goto cleanup;
4504 	}
4505 
4506 	DEBUG0("Bridge is ONLINE\n");
4507 
4508 	/*
4509 	 * After a Reset, we need to wait 2^25 clock cycles before the
4510 	 * first Configuration access.  The worst case is 33MHz, which
4511 	 * is a 1 second wait.
4512 	 */
4513 	drv_usecwait(pcicfg_sec_reset_delay);
4514 
4515 	/*
4516 	 * Probe all children devices
4517 	 */
4518 	DEBUG0("Bridge Programming Complete - probe children\n");
4519 	ndi_devi_enter(new_child, &count);
4520 	for (i = 0; ((i < PCI_MAX_DEVICES) && (ari_mode == B_FALSE));
4521 	    i++) {
4522 		for (j = 0; j < max_function; ) {
4523 			if (ari_mode)
4524 				trans_device = j >> 3;
4525 			else
4526 				trans_device = i;
4527 
4528 			if ((rval = pcicfg_probe_children(new_child,
4529 			    new_bus, trans_device, j & 7, highest_bus,
4530 			    0, is_pcie)) != PCICFG_SUCCESS) {
4531 				if (rval == PCICFG_NODEVICE) {
4532 					DEBUG3("No Device at bus [0x%x]"
4533 					    "device [0x%x] "
4534 					    "func [0x%x]\n", new_bus,
4535 					    trans_device, j & 7);
4536 
4537 					if (j)
4538 						goto next;
4539 				} else
4540 					/*EMPTY*/
4541 					DEBUG3("Failed to configure bus "
4542 					    "[0x%x] device [0x%x] "
4543 					    "func [0x%x]\n", new_bus,
4544 					    trans_device, j & 7);
4545 				break;
4546 			}
4547 next:
4548 			new_device = pcicfg_devi_find(new_child, trans_device,
4549 			    (j & 7));
4550 
4551 			/*
4552 			 * Determine if ARI Forwarding should be enabled.
4553 			 */
4554 			if (j == 0) {
4555 				if (new_device == NULL)
4556 					break;
4557 
4558 				if ((pcie_ari_supported(new_child) ==
4559 				    PCIE_ARI_FORW_SUPPORTED) &&
4560 				    (pcie_ari_device(new_device) ==
4561 				    PCIE_ARI_DEVICE)) {
4562 					if (pcie_ari_enable(new_child) ==
4563 					    DDI_SUCCESS) {
4564 						(void) ddi_prop_create(
4565 						    DDI_DEV_T_NONE,
4566 						    new_child,
4567 						    DDI_PROP_CANSLEEP,
4568 						    "ari-enabled", NULL, 0);
4569 						ari_mode = B_TRUE;
4570 						max_function =
4571 						    PCICFG_MAX_ARI_FUNCTION;
4572 					}
4573 				}
4574 			}
4575 			if (ari_mode == B_TRUE) {
4576 				int next_function;
4577 
4578 				if (new_device == NULL)
4579 					break;
4580 
4581 				if (pcie_ari_get_next_function(new_device,
4582 				    &next_function) != DDI_SUCCESS)
4583 					break;
4584 
4585 				j = next_function;
4586 
4587 				if (next_function == 0)
4588 					break;
4589 			} else
4590 				j++;
4591 
4592 		}
4593 		/* if any function fails to be configured, no need to proceed */
4594 		if (rval != PCICFG_NODEVICE)
4595 			break;
4596 	}
4597 	ndi_devi_exit(new_child, count);
4598 
4599 	/*
4600 	 * Offline the bridge to allow reprogramming of resources.
4601 	 *
4602 	 * This should always succeed since nobody else has started to
4603 	 * use it yet, failing to detach the driver would indicate a bug.
4604 	 * Also in that case it's better just panic than allowing the
4605 	 * configurator to proceed with BAR reprogramming without bridge
4606 	 * driver detached.
4607 	 */
4608 	VERIFY(ndi_devi_offline(new_child, NDI_NO_EVENT|NDI_UNCONFIG)
4609 	    == NDI_SUCCESS);
4610 	if (is_pcie)
4611 		pcie_fini_bus(new_child, PCIE_BUS_INITIAL);
4612 
4613 	phdl.dip = new_child;
4614 	phdl.memory_base = mem_answer;
4615 	phdl.io_base = io_answer;
4616 	phdl.pf_memory_base = pf_mem_answer;
4617 	phdl.error = PCICFG_SUCCESS;	/* in case of empty child tree */
4618 
4619 	ndi_devi_enter(ddi_get_parent(new_child), &count);
4620 	ddi_walk_devs(new_child, pcicfg_find_resource_end, (void *)&phdl);
4621 	ndi_devi_exit(ddi_get_parent(new_child), count);
4622 
4623 	num_slots = pcicfg_get_nslots(new_child, h);
4624 	mem_end = PCICFG_ROUND_UP(phdl.memory_base, PCICFG_MEMGRAN);
4625 	io_end = PCICFG_ROUND_UP(phdl.io_base, PCICFG_IOGRAN);
4626 	pf_mem_end = PCICFG_ROUND_UP(phdl.pf_memory_base, PCICFG_MEMGRAN);
4627 
4628 	DEBUG4("Start of Unallocated Bridge(%d slots) Resources Mem=0x%lx "
4629 	    "I/O=0x%lx PF_mem=%x%lx\n", num_slots, mem_end, io_end, pf_mem_end);
4630 
4631 	/*
4632 	 * Before probing the children we've allocated maximum MEM/IO
4633 	 * resources from parent, and updated "available" property
4634 	 * accordingly. Later we'll be giving up unused resources to
4635 	 * the parent, thus we need to destroy "available" property
4636 	 * here otherwise it will be out-of-sync with the actual free
4637 	 * resources this bridge has. This property will be rebuilt below
4638 	 * with the actual free resources reserved for hotplug slots
4639 	 * (if any).
4640 	 */
4641 	(void) ndi_prop_remove(DDI_DEV_T_NONE, new_child, "available");
4642 	/*
4643 	 * if the bridge a slots, then preallocate. If not, assume static
4644 	 * configuration. Also check for preallocation limits and spit
4645 	 * warning messages appropriately (perhaps some can be in debug mode).
4646 	 */
4647 	if (num_slots) {
4648 		uint64_t mem_reqd = mem_answer +
4649 		    (num_slots * pcicfg_slot_memsize);
4650 		uint64_t io_reqd = io_answer +
4651 		    (num_slots * pcicfg_slot_iosize);
4652 		uint64_t pf_mem_reqd = pf_mem_answer +
4653 		    (num_slots * pcicfg_slot_pf_memsize);
4654 		uint8_t highest_bus_reqd = new_bus +
4655 		    (num_slots * pcicfg_slot_busnums);
4656 #ifdef DEBUG
4657 		if (mem_end > mem_reqd)
4658 			DEBUG3("Memory space consumed by bridge more "
4659 			    "than planned for %d slot(s)(%" PRIx64 ",%"
4660 			    PRIx64 ")", num_slots, mem_answer, mem_end);
4661 		if (io_end > io_reqd)
4662 			DEBUG3("IO space consumed by bridge more than"
4663 			    " planned for %d slot(s)(%" PRIx64 ",%" PRIx64 ")",
4664 			    num_slots, io_answer, io_end);
4665 		if (pf_mem_end > pf_mem_reqd)
4666 			DEBUG3("PF Memory space consumed by bridge"
4667 			    " more than planned for %d slot(s)(%" PRIx64 ",%"
4668 			    PRIx64 ")", num_slots, pf_mem_answer, pf_mem_end);
4669 		if (*highest_bus > highest_bus_reqd)
4670 			DEBUG3("Buses consumed by bridge more "
4671 			    "than planned for %d slot(s)(%x, %x)",
4672 			    num_slots, new_bus, *highest_bus);
4673 
4674 		if (mem_reqd > (mem_answer + mem_alen))
4675 			DEBUG3("Memory space required by bridge more "
4676 			    "than available for %d slot(s)(%" PRIx64 ",%"
4677 			    PRIx64 ")", num_slots, mem_answer, mem_end);
4678 		if (io_reqd > (io_answer + io_alen))
4679 			DEBUG3("IO space required by bridge more than"
4680 			    "available for %d slot(s)(%" PRIx64 ",%" PRIx64 ")",
4681 			    num_slots, io_answer, io_end);
4682 		if (pf_mem_reqd > (pf_mem_answer + pf_mem_alen))
4683 			DEBUG3("PF Memory space required by bridge"
4684 			    " more than available for %d slot(s)(%" PRIx64 ",%"
4685 			    PRIx64 ")", num_slots, pf_mem_answer, pf_mem_end);
4686 		if (highest_bus_reqd > max_bus)
4687 			DEBUG3("Bus numbers required by bridge more "
4688 			    "than available for %d slot(s)(%x, %x)",
4689 			    num_slots, new_bus, *highest_bus);
4690 #endif
4691 		mem_end = MAX((MIN(mem_reqd, (mem_answer + mem_alen))),
4692 		    mem_end);
4693 		io_end = MAX((MIN(io_reqd, (io_answer + io_alen))), io_end);
4694 		pf_mem_end = MAX((MIN(pf_mem_reqd, (pf_mem_answer +
4695 		    pf_mem_alen))), pf_mem_end);
4696 		*highest_bus = MAX((MIN(highest_bus_reqd, max_bus)),
4697 		    *highest_bus);
4698 		DEBUG4("mem_end %lx, io_end %lx, pf_mem_end %lx"
4699 		    " highest_bus %x\n", mem_end, io_end, pf_mem_end,
4700 		    *highest_bus);
4701 	}
4702 
4703 	/*
4704 	 * Give back unused memory space to parent.
4705 	 */
4706 	(void) ndi_ra_free(ddi_get_parent(new_child), mem_end,
4707 	    (mem_answer + mem_alen) - mem_end, NDI_RA_TYPE_MEM, NDI_RA_PASS);
4708 
4709 	if (mem_end == mem_answer) {
4710 		DEBUG0("No memory resources used\n");
4711 		/*
4712 		 * To prevent the bridge from forwarding any Memory
4713 		 * transactions, the Memory Limit will be programmed
4714 		 * with a smaller value than the Memory Base.
4715 		 */
4716 		pci_config_put16(h, PCI_BCNF_MEM_BASE, 0xffff);
4717 		pci_config_put16(h, PCI_BCNF_MEM_LIMIT, 0);
4718 
4719 		mem_size = 0;
4720 	} else {
4721 		/*
4722 		 * Reprogram the end of the memory.
4723 		 */
4724 		pci_config_put16(h, PCI_BCNF_MEM_LIMIT,
4725 		    PCICFG_HIWORD(mem_end) - 1);
4726 		mem_size = mem_end - mem_base;
4727 	}
4728 
4729 	/*
4730 	 * Give back unused io space to parent.
4731 	 */
4732 	(void) ndi_ra_free(ddi_get_parent(new_child),
4733 	    io_end, (io_answer + io_alen) - io_end,
4734 	    NDI_RA_TYPE_IO, NDI_RA_PASS);
4735 
4736 	if (io_end == io_answer) {
4737 		DEBUG0("No IO Space resources used\n");
4738 
4739 		/*
4740 		 * To prevent the bridge from forwarding any I/O
4741 		 * transactions, the I/O Limit will be programmed
4742 		 * with a smaller value than the I/O Base.
4743 		 */
4744 		pci_config_put8(h, PCI_BCNF_IO_LIMIT_LOW, 0);
4745 		pci_config_put16(h, PCI_BCNF_IO_LIMIT_HI, 0);
4746 		pci_config_put8(h, PCI_BCNF_IO_BASE_LOW, 0xff);
4747 		pci_config_put16(h, PCI_BCNF_IO_BASE_HI, 0);
4748 
4749 		io_size = 0;
4750 	} else {
4751 		/*
4752 		 * Reprogram the end of the io space.
4753 		 */
4754 		pci_config_put8(h, PCI_BCNF_IO_LIMIT_LOW,
4755 		    PCICFG_HIBYTE(PCICFG_LOWORD(
4756 		    PCICFG_LOADDR(io_end) - 1)));
4757 
4758 		pci_config_put16(h, PCI_BCNF_IO_LIMIT_HI,
4759 		    PCICFG_HIWORD(PCICFG_LOADDR(io_end - 1)));
4760 
4761 		io_size = io_end - io_base;
4762 	}
4763 
4764 	/*
4765 	 * Give back unused PF memory space to parent.
4766 	 */
4767 	if (pf_mem_supported) {
4768 		(void) ndi_ra_free(ddi_get_parent(new_child),
4769 		    pf_mem_end, (pf_mem_answer + pf_mem_alen) - pf_mem_end,
4770 		    NDI_RA_TYPE_PCI_PREFETCH_MEM, NDI_RA_PASS);
4771 
4772 		if (pf_mem_end == pf_mem_answer) {
4773 			DEBUG0("No PF memory resources used\n");
4774 			/*
4775 			 * To prevent the bridge from forwarding any PF Memory
4776 			 * transactions, the PF Memory Limit will be programmed
4777 			 * with a smaller value than the Memory Base.
4778 			 */
4779 			pci_config_put16(h, PCI_BCNF_PF_BASE_LOW, 0xfff0);
4780 			pci_config_put32(h, PCI_BCNF_PF_BASE_HIGH, 0xffffffff);
4781 			pci_config_put16(h, PCI_BCNF_PF_LIMIT_LOW, 0);
4782 			pci_config_put32(h, PCI_BCNF_PF_LIMIT_HIGH, 0);
4783 
4784 			pf_mem_size = 0;
4785 		} else {
4786 			/*
4787 			 * Reprogram the end of the PF memory range.
4788 			 */
4789 			pci_config_put16(h, PCI_BCNF_PF_LIMIT_LOW,
4790 			    PCICFG_HIWORD(PCICFG_LOADDR(pf_mem_end - 1)));
4791 			pci_config_put32(h, PCI_BCNF_PF_LIMIT_HIGH,
4792 			    PCICFG_HIADDR(pf_mem_end - 1));
4793 			pf_mem_size = pf_mem_end - pf_mem_base;
4794 		}
4795 	}
4796 
4797 	if ((max_bus - *highest_bus) > 0) {
4798 		/*
4799 		 * Give back unused bus numbers
4800 		 */
4801 		(void) ndi_ra_free(ddi_get_parent(new_child),
4802 		    *highest_bus+1, max_bus - *highest_bus,
4803 		    NDI_RA_TYPE_PCI_BUSNUM, NDI_RA_PASS);
4804 	}
4805 
4806 	/*
4807 	 * Set bus numbers to ranges encountered during scan
4808 	 */
4809 	(void) pcicfg_set_bus_numbers(h, bus, new_bus, *highest_bus);
4810 
4811 	/*
4812 	 * Remove the ranges property if it exists since we will create
4813 	 * a new one.
4814 	 */
4815 	(void) ndi_prop_remove(DDI_DEV_T_NONE, new_child, "ranges");
4816 
4817 	DEBUG2("Creating Ranges property - Mem Address %lx Mem Size %x\n",
4818 	    mem_base, mem_size);
4819 	DEBUG2("                         - I/O Address %lx I/O Size %x\n",
4820 	    io_base, io_size);
4821 	DEBUG2("                         - PF Mem address %lx PF Mem Size %x\n",
4822 	    pf_mem_base, pf_mem_size);
4823 
4824 	bzero((caddr_t)range, sizeof (ppb_ranges_t) * PCICFG_RANGE_LEN);
4825 
4826 	range[0].child_high = range[0].parent_high |= (PCI_REG_REL_M |
4827 	    PCI_ADDR_IO);
4828 	range[0].child_low = range[0].parent_low = io_base;
4829 	range[1].child_high = range[1].parent_high |=
4830 	    (PCI_REG_REL_M | PCI_ADDR_MEM32);
4831 	range[1].child_low = range[1].parent_low = mem_base;
4832 	range[2].child_high = range[2].parent_high |=
4833 	    (PCI_REG_REL_M | PCI_ADDR_MEM64 | PCI_REG_PF_M);
4834 	range[2].child_low = range[2].parent_low = pf_mem_base;
4835 
4836 	if (io_size > 0) {
4837 		range[0].size_low = io_size;
4838 		(void) pcicfg_update_ranges_prop(new_child, &range[0]);
4839 	}
4840 	if (mem_size > 0) {
4841 		range[1].size_low = mem_size;
4842 		(void) pcicfg_update_ranges_prop(new_child, &range[1]);
4843 	}
4844 	if (pf_mem_size > 0) {
4845 		range[2].size_low = pf_mem_size;
4846 		(void) pcicfg_update_ranges_prop(new_child, &range[2]);
4847 	}
4848 
4849 	bus_range[0] = pci_config_get8(h, PCI_BCNF_SECBUS);
4850 	bus_range[1] = pci_config_get8(h, PCI_BCNF_SUBBUS);
4851 	DEBUG1("End of bridge probe: bus_range[0] =  %d\n", bus_range[0]);
4852 	DEBUG1("End of bridge probe: bus_range[1] =  %d\n", bus_range[1]);
4853 
4854 	(void) ndi_prop_update_int_array(DDI_DEV_T_NONE, new_child,
4855 	    "bus-range", bus_range, 2);
4856 
4857 	rval = PCICFG_SUCCESS;
4858 
4859 	PCICFG_DUMP_BRIDGE_CONFIG(h);
4860 
4861 cleanup:
4862 	/* free up resources (for error return case only) */
4863 	if (rval != PCICFG_SUCCESS) {
4864 		if (mem_alen)
4865 			(void) ndi_ra_free(ddi_get_parent(new_child), mem_base,
4866 			    mem_alen, NDI_RA_TYPE_MEM, NDI_RA_PASS);
4867 		if (io_alen)
4868 			(void) ndi_ra_free(ddi_get_parent(new_child), io_base,
4869 			    io_alen, NDI_RA_TYPE_IO, NDI_RA_PASS);
4870 		if (pf_mem_alen)
4871 			(void) ndi_ra_free(ddi_get_parent(new_child),
4872 			    pf_mem_base, pf_mem_alen,
4873 			    NDI_RA_TYPE_PCI_PREFETCH_MEM, NDI_RA_PASS);
4874 		if (pcibus_alen)
4875 			(void) ndi_ra_free(ddi_get_parent(new_child),
4876 			    pcibus_base, pcibus_alen, NDI_RA_TYPE_PCI_BUSNUM,
4877 			    NDI_RA_PASS);
4878 	}
4879 
4880 	/* free up any resource maps setup for the bridge node */
4881 	(void) ndi_ra_map_destroy(new_child, NDI_RA_TYPE_PCI_BUSNUM);
4882 	(void) ndi_ra_map_destroy(new_child, NDI_RA_TYPE_IO);
4883 	(void) ndi_ra_map_destroy(new_child, NDI_RA_TYPE_MEM);
4884 	(void) ndi_ra_map_destroy(new_child, NDI_RA_TYPE_PCI_PREFETCH_MEM);
4885 
4886 	return (rval);
4887 }
4888 
4889 static int
4890 pcicfg_find_resource_end(dev_info_t *dip, void *hdl)
4891 {
4892 	pcicfg_phdl_t *entry = (pcicfg_phdl_t *)hdl;
4893 	pci_regspec_t *pci_ap;
4894 	int length;
4895 	int rcount;
4896 	int i;
4897 
4898 	entry->error = PCICFG_SUCCESS;
4899 
4900 	if (dip == entry->dip) {
4901 		DEBUG0("Don't include parent bridge node\n");
4902 		return (DDI_WALK_CONTINUE);
4903 	} else {
4904 		if (ddi_getlongprop(DDI_DEV_T_ANY, dip,
4905 		    DDI_PROP_DONTPASS, "assigned-addresses",
4906 		    (caddr_t)&pci_ap,  &length) != DDI_PROP_SUCCESS) {
4907 			DEBUG0("Node doesn't have assigned-addresses\n");
4908 			return (DDI_WALK_CONTINUE);
4909 		}
4910 
4911 		rcount = length / sizeof (pci_regspec_t);
4912 
4913 		for (i = 0; i < rcount; i++) {
4914 
4915 			switch (PCI_REG_ADDR_G(pci_ap[i].pci_phys_hi)) {
4916 
4917 			case PCI_REG_ADDR_G(PCI_ADDR_MEM32):
4918 				if (pci_ap[i].pci_phys_hi & PCI_REG_PF_M) {
4919 					if ((pci_ap[i].pci_phys_low +
4920 					    pci_ap[i].pci_size_low) >
4921 					    entry->pf_memory_base) {
4922 						entry->pf_memory_base =
4923 						    pci_ap[i].pci_phys_low +
4924 						    pci_ap[i].pci_size_low;
4925 					}
4926 				} else {
4927 					if ((pci_ap[i].pci_phys_low +
4928 					    pci_ap[i].pci_size_low) >
4929 					    entry->memory_base) {
4930 						entry->memory_base =
4931 						    pci_ap[i].pci_phys_low +
4932 						    pci_ap[i].pci_size_low;
4933 					}
4934 				}
4935 				break;
4936 			case PCI_REG_ADDR_G(PCI_ADDR_MEM64):
4937 				if (pci_ap[i].pci_phys_hi & PCI_REG_PF_M) {
4938 					if ((PCICFG_LADDR(
4939 					    pci_ap[i].pci_phys_low,
4940 					    pci_ap[i].pci_phys_mid) +
4941 					    pci_ap[i].pci_size_low) >
4942 					    entry->pf_memory_base) {
4943 						entry->pf_memory_base =
4944 						    PCICFG_LADDR(
4945 						    pci_ap[i].pci_phys_low,
4946 						    pci_ap[i].pci_phys_mid) +
4947 						    pci_ap[i].pci_size_low;
4948 					}
4949 				} else {
4950 					if ((PCICFG_LADDR(
4951 					    pci_ap[i].pci_phys_low,
4952 					    pci_ap[i].pci_phys_mid) +
4953 					    pci_ap[i].pci_size_low) >
4954 					    entry->memory_base) {
4955 						entry->memory_base =
4956 						    PCICFG_LADDR(
4957 						    pci_ap[i].pci_phys_low,
4958 						    pci_ap[i].pci_phys_mid) +
4959 						    pci_ap[i].pci_size_low;
4960 					}
4961 				}
4962 				break;
4963 			case PCI_REG_ADDR_G(PCI_ADDR_IO):
4964 				if ((pci_ap[i].pci_phys_low +
4965 				    pci_ap[i].pci_size_low) >
4966 				    entry->io_base) {
4967 					entry->io_base =
4968 					    pci_ap[i].pci_phys_low +
4969 					    pci_ap[i].pci_size_low;
4970 				}
4971 				break;
4972 			}
4973 		}
4974 
4975 		/*
4976 		 * free the memory allocated by ddi_getlongprop
4977 		 */
4978 		kmem_free(pci_ap, length);
4979 
4980 		/*
4981 		 * continue the walk to the next sibling to sum memory
4982 		 */
4983 		return (DDI_WALK_CONTINUE);
4984 	}
4985 }
4986 
4987 /*
4988  * Make "parent" be the parent of the "child" dip
4989  */
4990 static void
4991 pcicfg_reparent_node(dev_info_t *child, dev_info_t *parent)
4992 {
4993 	int circ;
4994 	dev_info_t *opdip;
4995 
4996 	ASSERT(i_ddi_node_state(child) <= DS_LINKED);
4997 	/*
4998 	 * Unlink node from tree before reparenting
4999 	 */
5000 	opdip = ddi_get_parent(child);
5001 	ndi_devi_enter(opdip, &circ);
5002 	(void) i_ndi_unconfig_node(child, DS_PROTO, 0);
5003 	ndi_devi_exit(opdip, circ);
5004 
5005 	DEVI(child)->devi_parent = DEVI(parent);
5006 	DEVI(child)->devi_bus_ctl = DEVI(parent);
5007 	(void) ndi_devi_bind_driver(child, 0);
5008 }
5009 
5010 /*
5011  * Return PCICFG_SUCCESS if device exists at the specified address.
5012  * Return PCICFG_NODEVICE is no device exists at the specified address.
5013  */
5014 int
5015 pcicfg_config_setup(dev_info_t *dip, ddi_acc_handle_t *handle)
5016 {
5017 	caddr_t	cfgaddr;
5018 	ddi_device_acc_attr_t attr;
5019 	dev_info_t *anode;
5020 	int status;
5021 	int		rlen;
5022 	pci_regspec_t	*reg;
5023 	int		ret = DDI_SUCCESS;
5024 	int16_t		tmp;
5025 
5026 	/*
5027 	 * Get the pci register spec from the node
5028 	 */
5029 	status = ddi_getlongprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, "reg",
5030 	    (caddr_t)&reg, &rlen);
5031 
5032 	switch (status) {
5033 		case DDI_PROP_SUCCESS:
5034 			break;
5035 		case DDI_PROP_NO_MEMORY:
5036 			DEBUG0("reg present, but unable to get memory\n");
5037 			return (PCICFG_FAILURE);
5038 		default:
5039 			DEBUG0("no reg property\n");
5040 			return (PCICFG_FAILURE);
5041 	}
5042 
5043 	anode = dip;
5044 	DEBUG2("conf_map: dip=%p, anode=%p\n", dip, anode);
5045 
5046 	attr.devacc_attr_version = DDI_DEVICE_ATTR_V0;
5047 	attr.devacc_attr_endian_flags = DDI_STRUCTURE_LE_ACC;
5048 	attr.devacc_attr_dataorder = DDI_STRICTORDER_ACC;
5049 
5050 	if (ddi_regs_map_setup(anode, 0, &cfgaddr, 0, 0, &attr, handle)
5051 	    != DDI_SUCCESS) {
5052 		DEBUG0("Failed to setup registers\n");
5053 		kmem_free((caddr_t)reg, rlen);
5054 		return (PCICFG_FAILURE);
5055 	}
5056 
5057 	/*
5058 	 * need to use DDI interfaces as the conf space is
5059 	 * cannot be directly accessed by the host.
5060 	 */
5061 	tmp = (int16_t)ddi_get16(*handle, (uint16_t *)cfgaddr);
5062 	if ((tmp == (int16_t)0xffff) || (tmp == -1)) {
5063 		DEBUG1("NO DEVICEFOUND, read %x\n", tmp);
5064 		ret = PCICFG_NODEVICE;
5065 	} else {
5066 		if (tmp == 0) {
5067 			DEBUG0("Device Not Ready yet ?");
5068 			ret = PCICFG_NODEVICE;
5069 		} else {
5070 			DEBUG1("DEVICEFOUND, read %x\n", tmp);
5071 			ret = PCICFG_SUCCESS;
5072 		}
5073 	}
5074 
5075 	if (ret == PCICFG_NODEVICE)
5076 		ddi_regs_map_free(handle);
5077 	kmem_free((caddr_t)reg, rlen);
5078 
5079 	return (ret);
5080 
5081 }
5082 
5083 static void
5084 pcicfg_config_teardown(ddi_acc_handle_t *handle)
5085 {
5086 	(void) ddi_regs_map_free(handle);
5087 }
5088 
5089 static int
5090 pcicfg_add_config_reg(dev_info_t *dip,
5091     uint_t bus, uint_t device, uint_t func)
5092 {
5093 	int reg[10] = { PCI_ADDR_CONFIG, 0, 0, 0, 0};
5094 
5095 	reg[0] = PCICFG_MAKE_REG_HIGH(bus, device, func, 0);
5096 
5097 	return (ndi_prop_update_int_array(DDI_DEV_T_NONE, dip, "reg", reg, 5));
5098 }
5099 
5100 static int
5101 pcicfg_ari_configure(dev_info_t *dip)
5102 {
5103 	if (pcie_ari_supported(dip) == PCIE_ARI_FORW_NOT_SUPPORTED)
5104 		return (DDI_FAILURE);
5105 
5106 	/*
5107 	 * Until we have resource balancing, dynamically configure
5108 	 * ARI functions without firmware assistamce.
5109 	 */
5110 	return (DDI_FAILURE);
5111 }
5112 
5113 
5114 #ifdef DEBUG
5115 static void
5116 debug(char *fmt, uintptr_t a1, uintptr_t a2, uintptr_t a3, uintptr_t a4,
5117     uintptr_t a5)
5118 {
5119 	if (pcicfg_debug > 1) {
5120 		prom_printf("pcicfg: ");
5121 		prom_printf(fmt, a1, a2, a3, a4, a5);
5122 	}
5123 }
5124 #endif
5125 
5126 /*ARGSUSED*/
5127 static uint8_t
5128 pcicfg_get_nslots(dev_info_t *dip, ddi_acc_handle_t handle)
5129 {
5130 	uint16_t cap_id_loc, slot_id_loc;
5131 	uint8_t num_slots = 0;
5132 
5133 	/* just depend on the pcie_cap for now. */
5134 	(void) PCI_CAP_LOCATE(handle, PCI_CAP_ID_PCI_E, &cap_id_loc);
5135 	(void) PCI_CAP_LOCATE(handle, PCI_CAP_ID_SLOT_ID, &slot_id_loc);
5136 	if (cap_id_loc != PCI_CAP_NEXT_PTR_NULL) {
5137 		if (pci_config_get8(handle, cap_id_loc + PCI_CAP_ID_REGS_OFF) &
5138 		    PCIE_PCIECAP_SLOT_IMPL)
5139 			num_slots = 1;
5140 	} else /* not a PCIe switch/bridge. Must be a PCI-PCI[-X] bridge */
5141 	if (slot_id_loc != PCI_CAP_NEXT_PTR_NULL) {
5142 		uint8_t esr_reg = pci_config_get8(handle, slot_id_loc + 2);
5143 		num_slots = PCI_CAPSLOT_NSLOTS(esr_reg);
5144 	}
5145 	/* XXX - need to cover PCI-PCIe bridge with n slots */
5146 	return (num_slots);
5147 }
5148 
5149 /*ARGSUSED*/
5150 static int
5151 pcicfg_pcie_dev(dev_info_t *dip, ddi_acc_handle_t handle)
5152 {
5153 	/* get parent device's device_type property */
5154 	char *device_type;
5155 	int val;
5156 	dev_info_t *pdip = ddi_get_parent(dip);
5157 
5158 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, pdip, DDI_PROP_DONTPASS,
5159 	    "device_type", &device_type) != DDI_PROP_SUCCESS) {
5160 		DEBUG2("device_type property missing for %s#%d",
5161 		    ddi_get_name(pdip), ddi_get_instance(pdip));
5162 		return (DDI_FAILURE);
5163 	}
5164 	DEBUG1("device_type=<%s>\n", device_type);
5165 
5166 	val = DDI_FAILURE;
5167 	if (strcmp(device_type, "pciex") == 0)
5168 		val = DDI_SUCCESS;
5169 	ddi_prop_free(device_type);
5170 	return (val);
5171 }
5172 
5173 static int
5174 pcicfg_pcie_device_type(dev_info_t *dip, ddi_acc_handle_t handle)
5175 {
5176 	int port_type = pcicfg_pcie_port_type(dip, handle);
5177 
5178 	DEBUG1("device port_type = %x\n", port_type);
5179 	/* No PCIe CAP regs, we are not PCIe device_type */
5180 	if (port_type < 0)
5181 		return (DDI_FAILURE);
5182 
5183 	/* check for all PCIe device_types */
5184 	if ((port_type == PCIE_PCIECAP_DEV_TYPE_UP) ||
5185 	    (port_type == PCIE_PCIECAP_DEV_TYPE_DOWN) ||
5186 	    (port_type == PCIE_PCIECAP_DEV_TYPE_ROOT) ||
5187 	    (port_type == PCIE_PCIECAP_DEV_TYPE_PCI2PCIE))
5188 		return (DDI_SUCCESS);
5189 
5190 	return (DDI_FAILURE);
5191 
5192 }
5193 
5194 /*ARGSUSED*/
5195 static int
5196 pcicfg_pcie_port_type(dev_info_t *dip, ddi_acc_handle_t handle)
5197 {
5198 	int port_type = -1;
5199 	uint16_t cap_loc;
5200 
5201 	/* Note: need to look at the port type information here */
5202 	(void) PCI_CAP_LOCATE(handle, PCI_CAP_ID_PCI_E, &cap_loc);
5203 	if (cap_loc != PCI_CAP_NEXT_PTR_NULL)
5204 		port_type = pci_config_get16(handle,
5205 		    cap_loc + PCIE_PCIECAP) & PCIE_PCIECAP_DEV_TYPE_MASK;
5206 
5207 	return (port_type);
5208 }
5209 
5210 /*
5211  * Return true if the devinfo node is in a PCI Express hierarchy.
5212  */
5213 static boolean_t
5214 is_pcie_fabric(dev_info_t *dip)
5215 {
5216 	dev_info_t *root = ddi_root_node();
5217 	dev_info_t *pdip;
5218 	boolean_t found = B_FALSE;
5219 	char *bus;
5220 
5221 	/*
5222 	 * Does this device reside in a pcie fabric ?
5223 	 */
5224 	for (pdip = dip; pdip && (pdip != root) && !found;
5225 	    pdip = ddi_get_parent(pdip)) {
5226 		if (ddi_prop_lookup_string(DDI_DEV_T_ANY, pdip,
5227 		    DDI_PROP_DONTPASS, "device_type", &bus) !=
5228 		    DDI_PROP_SUCCESS)
5229 			break;
5230 
5231 		if (strcmp(bus, "pciex") == 0)
5232 			found = B_TRUE;
5233 
5234 		ddi_prop_free(bus);
5235 	}
5236 
5237 	return (found);
5238 }
5239