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