xref: /illumos-gate/usr/src/uts/common/io/pciex/pcieb.c (revision aef83d42)
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 2010 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*
27  * Common x86 and SPARC PCI-E to PCI bus bridge nexus driver
28  */
29 
30 #include <sys/sysmacros.h>
31 #include <sys/conf.h>
32 #include <sys/kmem.h>
33 #include <sys/debug.h>
34 #include <sys/modctl.h>
35 #include <sys/autoconf.h>
36 #include <sys/ddi_impldefs.h>
37 #include <sys/pci.h>
38 #include <sys/ddi.h>
39 #include <sys/sunddi.h>
40 #include <sys/sunndi.h>
41 #include <sys/fm/util.h>
42 #include <sys/pci_cap.h>
43 #include <sys/pci_impl.h>
44 #include <sys/pcie_impl.h>
45 #include <sys/open.h>
46 #include <sys/stat.h>
47 #include <sys/file.h>
48 #include <sys/promif.h>		/* prom_printf */
49 #include <sys/disp.h>
50 #include <sys/pcie_pwr.h>
51 #include <sys/hotplug/pci/pcie_hp.h>
52 #include "pcieb.h"
53 #ifdef PX_PLX
54 #include <io/pciex/pcieb_plx.h>
55 #endif /* PX_PLX */
56 
57 /*LINTLIBRARY*/
58 
59 /* panic flag */
60 int pcieb_die = PF_ERR_FATAL_FLAGS;
61 
62 /* flag to turn on MSI support */
63 int pcieb_enable_msi = 1;
64 
65 #if defined(DEBUG)
66 uint_t pcieb_dbg_print = 0;
67 
68 static char *pcieb_debug_sym [] = {	/* same sequence as pcieb_debug_bit */
69 	/*  0 */ "attach",
70 	/*  1 */ "pwr",
71 	/*  2 */ "intr"
72 };
73 #endif /* DEBUG */
74 
75 static int pcieb_bus_map(dev_info_t *, dev_info_t *, ddi_map_req_t *, off_t,
76 	off_t, caddr_t *);
77 static int pcieb_ctlops(dev_info_t *, dev_info_t *, ddi_ctl_enum_t, void *,
78 	void *);
79 static int pcieb_fm_init(pcieb_devstate_t *pcieb_p);
80 static void pcieb_fm_fini(pcieb_devstate_t *pcieb_p);
81 static int pcieb_fm_init_child(dev_info_t *dip, dev_info_t *cdip, int cap,
82     ddi_iblock_cookie_t *ibc_p);
83 static int pcieb_dma_allochdl(dev_info_t *dip, dev_info_t *rdip,
84 	ddi_dma_attr_t *attr_p, int (*waitfp)(caddr_t), caddr_t arg,
85 	ddi_dma_handle_t *handlep);
86 static int pcieb_dma_mctl(dev_info_t *dip, dev_info_t *rdip,
87 	ddi_dma_handle_t handle, enum ddi_dma_ctlops cmd, off_t *offp,
88 	size_t *lenp, caddr_t *objp, uint_t cache_flags);
89 static int pcieb_intr_ops(dev_info_t *dip, dev_info_t *rdip,
90 	ddi_intr_op_t intr_op, ddi_intr_handle_impl_t *hdlp, void *result);
91 
92 static struct bus_ops pcieb_bus_ops = {
93 	BUSO_REV,
94 	pcieb_bus_map,
95 	0,
96 	0,
97 	0,
98 	i_ddi_map_fault,
99 	ddi_dma_map,
100 	pcieb_dma_allochdl,
101 	ddi_dma_freehdl,
102 	ddi_dma_bindhdl,
103 	ddi_dma_unbindhdl,
104 	ddi_dma_flush,
105 	ddi_dma_win,
106 	pcieb_dma_mctl,
107 	pcieb_ctlops,
108 	ddi_bus_prop_op,
109 	ndi_busop_get_eventcookie,	/* (*bus_get_eventcookie)();	*/
110 	ndi_busop_add_eventcall,	/* (*bus_add_eventcall)();	*/
111 	ndi_busop_remove_eventcall,	/* (*bus_remove_eventcall)();	*/
112 	ndi_post_event,			/* (*bus_post_event)();		*/
113 	NULL,				/* (*bus_intr_ctl)();		*/
114 	NULL,				/* (*bus_config)(); 		*/
115 	NULL,				/* (*bus_unconfig)(); 		*/
116 	pcieb_fm_init_child,		/* (*bus_fm_init)(); 		*/
117 	NULL,				/* (*bus_fm_fini)(); 		*/
118 	i_ndi_busop_access_enter,	/* (*bus_fm_access_enter)(); 	*/
119 	i_ndi_busop_access_exit,	/* (*bus_fm_access_exit)(); 	*/
120 	pcie_bus_power,			/* (*bus_power)(); 	*/
121 	pcieb_intr_ops,			/* (*bus_intr_op)(); 		*/
122 	pcie_hp_common_ops		/* (*bus_hp_op)(); 		*/
123 };
124 
125 static int	pcieb_open(dev_t *, int, int, cred_t *);
126 static int	pcieb_close(dev_t, int, int, cred_t *);
127 static int	pcieb_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
128 static int	pcieb_info(dev_info_t *, ddi_info_cmd_t, void *, void **);
129 static uint_t 	pcieb_intr_handler(caddr_t arg1, caddr_t arg2);
130 
131 /* PM related functions */
132 static int	pcieb_pwr_setup(dev_info_t *dip);
133 static int	pcieb_pwr_init_and_raise(dev_info_t *dip, pcie_pwr_t *pwr_p);
134 static void	pcieb_pwr_teardown(dev_info_t *dip);
135 static int	pcieb_pwr_disable(dev_info_t *dip);
136 
137 /* Hotplug related functions */
138 static void pcieb_id_props(pcieb_devstate_t *pcieb);
139 
140 /*
141  * soft state pointer
142  */
143 void *pcieb_state;
144 
145 static struct cb_ops pcieb_cb_ops = {
146 	pcieb_open,			/* open */
147 	pcieb_close,			/* close */
148 	nodev,				/* strategy */
149 	nodev,				/* print */
150 	nodev,				/* dump */
151 	nodev,				/* read */
152 	nodev,				/* write */
153 	pcieb_ioctl,			/* ioctl */
154 	nodev,				/* devmap */
155 	nodev,				/* mmap */
156 	nodev,				/* segmap */
157 	nochpoll,			/* poll */
158 	pcie_prop_op,			/* cb_prop_op */
159 	NULL,				/* streamtab */
160 	D_NEW | D_MP | D_HOTPLUG,	/* Driver compatibility flag */
161 	CB_REV,				/* rev */
162 	nodev,				/* int (*cb_aread)() */
163 	nodev				/* int (*cb_awrite)() */
164 };
165 
166 static int	pcieb_probe(dev_info_t *);
167 static int	pcieb_attach(dev_info_t *devi, ddi_attach_cmd_t cmd);
168 static int	pcieb_detach(dev_info_t *devi, ddi_detach_cmd_t cmd);
169 
170 static struct dev_ops pcieb_ops = {
171 	DEVO_REV,		/* devo_rev */
172 	0,			/* refcnt  */
173 	pcieb_info,		/* info */
174 	nulldev,		/* identify */
175 	pcieb_probe,		/* probe */
176 	pcieb_attach,		/* attach */
177 	pcieb_detach,		/* detach */
178 	nulldev,		/* reset */
179 	&pcieb_cb_ops,		/* driver operations */
180 	&pcieb_bus_ops,		/* bus operations */
181 	pcie_power,		/* power */
182 	ddi_quiesce_not_needed,		/* quiesce */
183 };
184 
185 /*
186  * Module linkage information for the kernel.
187  */
188 
189 static struct modldrv modldrv = {
190 	&mod_driverops, /* Type of module */
191 	"PCIe bridge/switch driver",
192 	&pcieb_ops,	/* driver ops */
193 };
194 
195 static struct modlinkage modlinkage = {
196 	MODREV_1,
197 	(void *)&modldrv,
198 	NULL
199 };
200 
201 /*
202  * forward function declarations:
203  */
204 static void	pcieb_uninitchild(dev_info_t *);
205 static int 	pcieb_initchild(dev_info_t *child);
206 static void	pcieb_create_ranges_prop(dev_info_t *, ddi_acc_handle_t);
207 static boolean_t pcieb_is_pcie_device_type(dev_info_t *dip);
208 
209 /* interrupt related declarations */
210 static int	pcieb_msi_supported(dev_info_t *);
211 static int	pcieb_intr_attach(pcieb_devstate_t *pcieb);
212 static int	pcieb_intr_init(pcieb_devstate_t *pcieb_p, int intr_type);
213 static void	pcieb_intr_fini(pcieb_devstate_t *pcieb_p);
214 
215 int
216 _init(void)
217 {
218 	int e;
219 
220 	if ((e = ddi_soft_state_init(&pcieb_state, sizeof (pcieb_devstate_t),
221 	    1)) == 0 && (e = mod_install(&modlinkage)) != 0)
222 		ddi_soft_state_fini(&pcieb_state);
223 	return (e);
224 }
225 
226 int
227 _fini(void)
228 {
229 	int e;
230 
231 	if ((e = mod_remove(&modlinkage)) == 0) {
232 		ddi_soft_state_fini(&pcieb_state);
233 	}
234 	return (e);
235 }
236 
237 int
238 _info(struct modinfo *modinfop)
239 {
240 	return (mod_info(&modlinkage, modinfop));
241 }
242 
243 /* ARGSUSED */
244 static int
245 pcieb_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
246 {
247 	minor_t		minor = getminor((dev_t)arg);
248 	int		instance = PCI_MINOR_NUM_TO_INSTANCE(minor);
249 	pcieb_devstate_t *pcieb = ddi_get_soft_state(pcieb_state, instance);
250 	int		ret = DDI_SUCCESS;
251 
252 	switch (infocmd) {
253 	case DDI_INFO_DEVT2INSTANCE:
254 		*result = (void *)(intptr_t)instance;
255 		break;
256 	case DDI_INFO_DEVT2DEVINFO:
257 		if (pcieb == NULL) {
258 			ret = DDI_FAILURE;
259 			break;
260 		}
261 
262 		*result = (void *)pcieb->pcieb_dip;
263 		break;
264 	default:
265 		ret = DDI_FAILURE;
266 		break;
267 	}
268 
269 	return (ret);
270 }
271 
272 
273 /*ARGSUSED*/
274 static int
275 pcieb_probe(dev_info_t *devi)
276 {
277 	return (DDI_PROBE_SUCCESS);
278 }
279 
280 static int
281 pcieb_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
282 {
283 	int			instance;
284 	char			device_type[8];
285 	pcieb_devstate_t	*pcieb;
286 	pcie_bus_t		*bus_p = PCIE_DIP2UPBUS(devi);
287 	ddi_acc_handle_t	config_handle = bus_p->bus_cfg_hdl;
288 
289 	switch (cmd) {
290 	case DDI_RESUME:
291 		(void) pcie_pwr_resume(devi);
292 		return (DDI_SUCCESS);
293 
294 	default:
295 		return (DDI_FAILURE);
296 
297 	case DDI_ATTACH:
298 		break;
299 	}
300 
301 	if (!(PCIE_IS_BDG(bus_p))) {
302 		PCIEB_DEBUG(DBG_ATTACH, devi, "This is not a switch or"
303 		" bridge\n");
304 		return (DDI_FAILURE);
305 	}
306 
307 	/*
308 	 * If PCIE_LINKCTL_LINK_DISABLE bit in the PCIe Config
309 	 * Space (PCIe Capability Link Control Register) is set,
310 	 * then do not bind the driver.
311 	 */
312 	if (PCIE_CAP_GET(16, bus_p, PCIE_LINKCTL) & PCIE_LINKCTL_LINK_DISABLE)
313 		return (DDI_FAILURE);
314 
315 	/*
316 	 * Allocate and get soft state structure.
317 	 */
318 	instance = ddi_get_instance(devi);
319 	if (ddi_soft_state_zalloc(pcieb_state, instance) != DDI_SUCCESS)
320 		return (DDI_FAILURE);
321 	pcieb = ddi_get_soft_state(pcieb_state, instance);
322 	pcieb->pcieb_dip = devi;
323 
324 	if ((pcieb_fm_init(pcieb)) != DDI_SUCCESS) {
325 		PCIEB_DEBUG(DBG_ATTACH, devi, "Failed in pcieb_fm_init\n");
326 		goto fail;
327 	}
328 	pcieb->pcieb_init_flags |= PCIEB_INIT_FM;
329 
330 	mutex_init(&pcieb->pcieb_mutex, NULL, MUTEX_DRIVER, NULL);
331 	mutex_init(&pcieb->pcieb_err_mutex, NULL, MUTEX_DRIVER,
332 	    (void *)pcieb->pcieb_fm_ibc);
333 	mutex_init(&pcieb->pcieb_peek_poke_mutex, NULL, MUTEX_DRIVER,
334 	    (void *)pcieb->pcieb_fm_ibc);
335 
336 	/* create special properties for device identification */
337 	pcieb_id_props(pcieb);
338 
339 	/*
340 	 * Power management setup. This also makes sure that switch/bridge
341 	 * is at D0 during attach.
342 	 */
343 	if (pwr_common_setup(devi) != DDI_SUCCESS) {
344 		PCIEB_DEBUG(DBG_PWR, devi, "pwr_common_setup failed\n");
345 		goto fail;
346 	}
347 
348 	if (pcieb_pwr_setup(devi) != DDI_SUCCESS) {
349 		PCIEB_DEBUG(DBG_PWR, devi, "pxb_pwr_setup failed \n");
350 		goto fail;
351 	}
352 
353 	/*
354 	 * Make sure the "device_type" property exists.
355 	 */
356 	if (pcieb_is_pcie_device_type(devi))
357 		(void) strcpy(device_type, "pciex");
358 	else
359 		(void) strcpy(device_type, "pci");
360 
361 	(void) ddi_prop_update_string(DDI_DEV_T_NONE, devi,
362 	    "device_type", device_type);
363 
364 	/*
365 	 * Check whether the "ranges" property is present.
366 	 * Otherwise create the ranges property by reading
367 	 * the configuration registers
368 	 */
369 	if (ddi_prop_exists(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
370 	    "ranges") == 0) {
371 		pcieb_create_ranges_prop(devi, config_handle);
372 	}
373 
374 	if (PCIE_IS_PCI_BDG(bus_p))
375 		pcieb_set_pci_perf_parameters(devi, config_handle);
376 
377 #ifdef PX_PLX
378 	pcieb_attach_plx_workarounds(pcieb);
379 #endif /* PX_PLX */
380 
381 	if (pcie_init(devi, NULL) != DDI_SUCCESS)
382 		goto fail;
383 
384 	/*
385 	 * Initialize interrupt handlers. Ignore return value.
386 	 */
387 	(void) pcieb_intr_attach(pcieb);
388 
389 	(void) pcie_hpintr_enable(devi);
390 
391 	/* Do any platform specific workarounds needed at this time */
392 	pcieb_plat_attach_workaround(devi);
393 
394 	/*
395 	 * If this is a root port, determine and set the max payload size.
396 	 * Since this will involve scanning the fabric, all error enabling
397 	 * and sw workarounds should be in place before doing this.
398 	 */
399 	if (PCIE_IS_RP(bus_p))
400 		pcie_init_root_port_mps(devi);
401 
402 	ddi_report_dev(devi);
403 	return (DDI_SUCCESS);
404 
405 fail:
406 	(void) pcieb_detach(devi, DDI_DETACH);
407 	return (DDI_FAILURE);
408 }
409 
410 static int
411 pcieb_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
412 {
413 	pcieb_devstate_t *pcieb;
414 	int error = DDI_SUCCESS;
415 
416 	switch (cmd) {
417 	case DDI_SUSPEND:
418 		error = pcie_pwr_suspend(devi);
419 		return (error);
420 
421 	case DDI_DETACH:
422 		break;
423 
424 	default:
425 		return (DDI_FAILURE);
426 	}
427 
428 	pcieb = ddi_get_soft_state(pcieb_state, ddi_get_instance(devi));
429 
430 	/* disable hotplug interrupt */
431 	(void) pcie_hpintr_disable(devi);
432 
433 	/* remove interrupt handlers */
434 	pcieb_intr_fini(pcieb);
435 
436 	/* uninitialize inband PCI-E HPC if present */
437 	(void) pcie_uninit(devi);
438 
439 	(void) ddi_prop_remove(DDI_DEV_T_NONE, devi, "device_type");
440 
441 	(void) ndi_prop_remove(DDI_DEV_T_NONE, pcieb->pcieb_dip,
442 	    "pcie_ce_mask");
443 
444 	if (pcieb->pcieb_init_flags & PCIEB_INIT_FM)
445 		pcieb_fm_fini(pcieb);
446 
447 	pcieb_pwr_teardown(devi);
448 	pwr_common_teardown(devi);
449 
450 	mutex_destroy(&pcieb->pcieb_peek_poke_mutex);
451 	mutex_destroy(&pcieb->pcieb_err_mutex);
452 	mutex_destroy(&pcieb->pcieb_mutex);
453 
454 	/*
455 	 * And finally free the per-pci soft state.
456 	 */
457 	ddi_soft_state_free(pcieb_state, ddi_get_instance(devi));
458 
459 	return (DDI_SUCCESS);
460 }
461 
462 static int
463 pcieb_bus_map(dev_info_t *dip, dev_info_t *rdip, ddi_map_req_t *mp,
464     off_t offset, off_t len, caddr_t *vaddrp)
465 {
466 	dev_info_t *pdip;
467 
468 	if (PCIE_IS_RP(PCIE_DIP2BUS(dip)) && mp->map_handlep != NULL) {
469 		ddi_acc_impl_t *hdlp =
470 		    (ddi_acc_impl_t *)(mp->map_handlep)->ah_platform_private;
471 
472 		pcieb_set_prot_scan(dip, hdlp);
473 	}
474 	pdip = (dev_info_t *)DEVI(dip)->devi_parent;
475 	return ((DEVI(pdip)->devi_ops->devo_bus_ops->bus_map)(pdip, rdip, mp,
476 	    offset, len, vaddrp));
477 }
478 
479 static int
480 pcieb_ctlops(dev_info_t *dip, dev_info_t *rdip, ddi_ctl_enum_t ctlop,
481     void *arg, void *result)
482 {
483 	pci_regspec_t *drv_regp;
484 	int	reglen;
485 	int	rn;
486 	int	totreg;
487 	pcieb_devstate_t *pcieb = ddi_get_soft_state(pcieb_state,
488 	    ddi_get_instance(dip));
489 	struct detachspec *ds;
490 	struct attachspec *as;
491 
492 	switch (ctlop) {
493 	case DDI_CTLOPS_REPORTDEV:
494 		if (rdip == (dev_info_t *)0)
495 			return (DDI_FAILURE);
496 		cmn_err(CE_CONT, "?PCIE-device: %s@%s, %s%d\n",
497 		    ddi_node_name(rdip), ddi_get_name_addr(rdip),
498 		    ddi_driver_name(rdip),
499 		    ddi_get_instance(rdip));
500 		return (DDI_SUCCESS);
501 
502 	case DDI_CTLOPS_INITCHILD:
503 		return (pcieb_initchild((dev_info_t *)arg));
504 
505 	case DDI_CTLOPS_UNINITCHILD:
506 		pcieb_uninitchild((dev_info_t *)arg);
507 		return (DDI_SUCCESS);
508 
509 	case DDI_CTLOPS_SIDDEV:
510 		return (DDI_SUCCESS);
511 
512 	case DDI_CTLOPS_REGSIZE:
513 	case DDI_CTLOPS_NREGS:
514 		if (rdip == (dev_info_t *)0)
515 			return (DDI_FAILURE);
516 		break;
517 
518 	case DDI_CTLOPS_PEEK:
519 	case DDI_CTLOPS_POKE:
520 		return (pcieb_plat_peekpoke(dip, rdip, ctlop, arg, result));
521 	case DDI_CTLOPS_ATTACH:
522 		if (!pcie_is_child(dip, rdip))
523 			return (DDI_SUCCESS);
524 
525 		as = (struct attachspec *)arg;
526 		switch (as->when) {
527 		case DDI_PRE:
528 			if (as->cmd == DDI_RESUME) {
529 				pcie_clear_errors(rdip);
530 				if (pcieb_plat_ctlops(rdip, ctlop, arg) !=
531 				    DDI_SUCCESS)
532 					return (DDI_FAILURE);
533 			}
534 
535 			if (as->cmd == DDI_ATTACH)
536 				return (pcie_pm_hold(dip));
537 
538 			return (DDI_SUCCESS);
539 
540 		case DDI_POST:
541 			if (as->cmd == DDI_ATTACH &&
542 			    as->result != DDI_SUCCESS) {
543 				/*
544 				 * Attach failed for the child device. The child
545 				 * driver may have made PM calls before the
546 				 * attach failed. pcie_pm_remove_child() should
547 				 * cleanup PM state and holds (if any)
548 				 * associated with the child device.
549 				 */
550 				return (pcie_pm_remove_child(dip, rdip));
551 			}
552 
553 			if (as->result == DDI_SUCCESS) {
554 				pf_init(rdip, (void *)pcieb->pcieb_fm_ibc,
555 				    as->cmd);
556 
557 				(void) pcieb_plat_ctlops(rdip, ctlop, arg);
558 			}
559 
560 			/*
561 			 * For empty hotplug-capable slots, we should explicitly
562 			 * disable the errors, so that we won't panic upon
563 			 * unsupported hotplug messages.
564 			 */
565 			if ((!ddi_prop_exists(DDI_DEV_T_ANY, rdip,
566 			    DDI_PROP_DONTPASS, "hotplug-capable")) ||
567 			    ddi_get_child(rdip)) {
568 				(void) pcie_postattach_child(rdip);
569 				return (DDI_SUCCESS);
570 			}
571 
572 			pcie_disable_errors(rdip);
573 
574 			return (DDI_SUCCESS);
575 		default:
576 			break;
577 		}
578 		return (DDI_SUCCESS);
579 
580 	case DDI_CTLOPS_DETACH:
581 		if (!pcie_is_child(dip, rdip))
582 			return (DDI_SUCCESS);
583 
584 		ds = (struct detachspec *)arg;
585 		switch (ds->when) {
586 		case DDI_PRE:
587 			pf_fini(rdip, ds->cmd);
588 			return (DDI_SUCCESS);
589 
590 		case DDI_POST:
591 			if (pcieb_plat_ctlops(rdip, ctlop, arg) != DDI_SUCCESS)
592 				return (DDI_FAILURE);
593 			if (ds->cmd == DDI_DETACH &&
594 			    ds->result == DDI_SUCCESS) {
595 				return (pcie_pm_remove_child(dip, rdip));
596 			}
597 			return (DDI_SUCCESS);
598 		default:
599 			break;
600 		}
601 		return (DDI_SUCCESS);
602 	default:
603 		return (ddi_ctlops(dip, rdip, ctlop, arg, result));
604 	}
605 
606 	*(int *)result = 0;
607 	if (ddi_getlongprop(DDI_DEV_T_ANY, rdip,
608 	    DDI_PROP_DONTPASS | DDI_PROP_CANSLEEP, "reg", (caddr_t)&drv_regp,
609 	    &reglen) != DDI_SUCCESS)
610 		return (DDI_FAILURE);
611 
612 	totreg = reglen / sizeof (pci_regspec_t);
613 	if (ctlop == DDI_CTLOPS_NREGS)
614 		*(int *)result = totreg;
615 	else if (ctlop == DDI_CTLOPS_REGSIZE) {
616 		rn = *(int *)arg;
617 		if (rn >= totreg) {
618 			kmem_free(drv_regp, reglen);
619 			return (DDI_FAILURE);
620 		}
621 
622 		*(off_t *)result = drv_regp[rn].pci_size_low |
623 		    ((uint64_t)drv_regp[rn].pci_size_hi << 32);
624 	}
625 
626 	kmem_free(drv_regp, reglen);
627 	return (DDI_SUCCESS);
628 }
629 
630 /*
631  * name_child
632  *
633  * This function is called from init_child to name a node. It is
634  * also passed as a callback for node merging functions.
635  *
636  * return value: DDI_SUCCESS, DDI_FAILURE
637  */
638 static int
639 pcieb_name_child(dev_info_t *child, char *name, int namelen)
640 {
641 	pci_regspec_t *pci_rp;
642 	uint_t device, func;
643 	char **unit_addr;
644 	uint_t n;
645 
646 	/*
647 	 * For .conf nodes, use unit-address property as name
648 	 */
649 	if (ndi_dev_is_persistent_node(child) == 0) {
650 		if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY, child,
651 		    DDI_PROP_DONTPASS, "unit-address", &unit_addr, &n) !=
652 		    DDI_PROP_SUCCESS) {
653 			cmn_err(CE_WARN,
654 			    "cannot find unit-address in %s.conf",
655 			    ddi_driver_name(child));
656 			return (DDI_FAILURE);
657 		}
658 		if (n != 1 || *unit_addr == NULL || **unit_addr == 0) {
659 			cmn_err(CE_WARN, "unit-address property in %s.conf"
660 			    " not well-formed", ddi_driver_name(child));
661 			ddi_prop_free(unit_addr);
662 			return (DDI_FAILURE);
663 		}
664 		(void) snprintf(name, namelen, "%s", *unit_addr);
665 		ddi_prop_free(unit_addr);
666 		return (DDI_SUCCESS);
667 	}
668 
669 	/*
670 	 * Get the address portion of the node name based on
671 	 * the function and device number.
672 	 */
673 	if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, child,
674 	    DDI_PROP_DONTPASS, "reg", (int **)&pci_rp, &n) != DDI_SUCCESS) {
675 		return (DDI_FAILURE);
676 	}
677 
678 	/* copy the device identifications */
679 	device = PCI_REG_DEV_G(pci_rp[0].pci_phys_hi);
680 	func = PCI_REG_FUNC_G(pci_rp[0].pci_phys_hi);
681 
682 	if (pcie_ari_is_enabled(ddi_get_parent(child))
683 	    == PCIE_ARI_FORW_ENABLED) {
684 		func = (device << 3) | func;
685 		device = 0;
686 	}
687 
688 	if (func != 0)
689 		(void) snprintf(name, namelen, "%x,%x", device, func);
690 	else
691 		(void) snprintf(name, namelen, "%x", device);
692 
693 	ddi_prop_free(pci_rp);
694 	return (DDI_SUCCESS);
695 }
696 
697 static int
698 pcieb_initchild(dev_info_t *child)
699 {
700 	char name[MAXNAMELEN];
701 	int result = DDI_FAILURE;
702 	pcieb_devstate_t *pcieb =
703 	    (pcieb_devstate_t *)ddi_get_soft_state(pcieb_state,
704 	    ddi_get_instance(ddi_get_parent(child)));
705 
706 	/*
707 	 * Name the child
708 	 */
709 	if (pcieb_name_child(child, name, MAXNAMELEN) != DDI_SUCCESS) {
710 		result = DDI_FAILURE;
711 		goto done;
712 	}
713 	ddi_set_name_addr(child, name);
714 
715 	/*
716 	 * Pseudo nodes indicate a prototype node with per-instance
717 	 * properties to be merged into the real h/w device node.
718 	 * The interpretation of the unit-address is DD[,F]
719 	 * where DD is the device id and F is the function.
720 	 */
721 	if (ndi_dev_is_persistent_node(child) == 0) {
722 		extern int pci_allow_pseudo_children;
723 
724 		/*
725 		 * Try to merge the properties from this prototype
726 		 * node into real h/w nodes.
727 		 */
728 		if (ndi_merge_node(child, pcieb_name_child) != DDI_SUCCESS) {
729 			/*
730 			 * Merged ok - return failure to remove the node.
731 			 */
732 			ddi_set_name_addr(child, NULL);
733 			result = DDI_FAILURE;
734 			goto done;
735 		}
736 
737 		/* workaround for ddivs to run under PCI-E */
738 		if (pci_allow_pseudo_children) {
739 			result = DDI_SUCCESS;
740 			goto done;
741 		}
742 
743 		/*
744 		 * The child was not merged into a h/w node,
745 		 * but there's not much we can do with it other
746 		 * than return failure to cause the node to be removed.
747 		 */
748 		cmn_err(CE_WARN, "!%s@%s: %s.conf properties not merged",
749 		    ddi_driver_name(child), ddi_get_name_addr(child),
750 		    ddi_driver_name(child));
751 		ddi_set_name_addr(child, NULL);
752 		result = DDI_NOT_WELL_FORMED;
753 		goto done;
754 	}
755 
756 	/* platform specific initchild */
757 	pcieb_plat_initchild(child);
758 
759 	if (pcie_pm_hold(pcieb->pcieb_dip) != DDI_SUCCESS) {
760 		PCIEB_DEBUG(DBG_PWR, pcieb->pcieb_dip,
761 		    "INITCHILD: px_pm_hold failed\n");
762 		result = DDI_FAILURE;
763 		goto done;
764 	}
765 	/* Any return from here must call pcie_pm_release */
766 
767 	/*
768 	 * If configuration registers were previously saved by
769 	 * child (before it entered D3), then let the child do the
770 	 * restore to set up the config regs as it'll first need to
771 	 * power the device out of D3.
772 	 */
773 	if (ddi_prop_exists(DDI_DEV_T_ANY, child, DDI_PROP_DONTPASS,
774 	    "config-regs-saved-by-child") == 1) {
775 		PCIEB_DEBUG(DBG_PWR, ddi_get_parent(child),
776 		    "INITCHILD: config regs to be restored by child"
777 		    " for %s@%s\n", ddi_node_name(child),
778 		    ddi_get_name_addr(child));
779 
780 		result = DDI_SUCCESS;
781 		goto cleanup;
782 	}
783 
784 	PCIEB_DEBUG(DBG_PWR, ddi_get_parent(child),
785 	    "INITCHILD: config regs setup for %s@%s\n",
786 	    ddi_node_name(child), ddi_get_name_addr(child));
787 
788 	if (pcie_initchild(child) != DDI_SUCCESS) {
789 		result = DDI_FAILURE;
790 		goto cleanup;
791 	}
792 
793 #ifdef PX_PLX
794 	if (pcieb_init_plx_workarounds(pcieb, child) == DDI_FAILURE) {
795 		result = DDI_FAILURE;
796 		goto cleanup;
797 	}
798 #endif /* PX_PLX */
799 
800 	result = DDI_SUCCESS;
801 cleanup:
802 	pcie_pm_release(pcieb->pcieb_dip);
803 done:
804 	return (result);
805 }
806 
807 static void
808 pcieb_uninitchild(dev_info_t *dip)
809 {
810 
811 	pcie_uninitchild(dip);
812 
813 	pcieb_plat_uninitchild(dip);
814 
815 	ddi_set_name_addr(dip, NULL);
816 
817 	/*
818 	 * Strip the node to properly convert it back to prototype form
819 	 */
820 	ddi_remove_minor_node(dip, NULL);
821 
822 	ddi_prop_remove_all(dip);
823 }
824 
825 static boolean_t
826 pcieb_is_pcie_device_type(dev_info_t *dip)
827 {
828 	pcie_bus_t	*bus_p = PCIE_DIP2BUS(dip);
829 
830 	if (PCIE_IS_SW(bus_p) || PCIE_IS_RP(bus_p) || PCIE_IS_PCI2PCIE(bus_p))
831 		return (B_TRUE);
832 
833 	return (B_FALSE);
834 }
835 
836 static int
837 pcieb_intr_attach(pcieb_devstate_t *pcieb)
838 {
839 	int			intr_types;
840 	dev_info_t		*dip = pcieb->pcieb_dip;
841 
842 	/* Allow platform specific code to do any initialization first */
843 	pcieb_plat_intr_attach(pcieb);
844 
845 	/*
846 	 * Initialize interrupt handlers.
847 	 * If both MSI and FIXED are supported, try to attach MSI first.
848 	 * If MSI fails for any reason, then try FIXED, but only allow one
849 	 * type to be attached.
850 	 */
851 	if (ddi_intr_get_supported_types(dip, &intr_types) != DDI_SUCCESS) {
852 		PCIEB_DEBUG(DBG_ATTACH, dip, "ddi_intr_get_supported_types"
853 		    " failed\n");
854 		goto FAIL;
855 	}
856 
857 	if ((intr_types & DDI_INTR_TYPE_MSI) &&
858 	    (pcieb_msi_supported(dip) == DDI_SUCCESS)) {
859 		if (pcieb_intr_init(pcieb, DDI_INTR_TYPE_MSI) == DDI_SUCCESS)
860 			intr_types = DDI_INTR_TYPE_MSI;
861 		else {
862 			PCIEB_DEBUG(DBG_ATTACH, dip, "Unable to attach MSI"
863 			    " handler\n");
864 		}
865 	}
866 
867 	if (intr_types != DDI_INTR_TYPE_MSI) {
868 		/*
869 		 * MSIs are not supported or MSI initialization failed. For Root
870 		 * Ports mark this so error handling might try to fallback to
871 		 * some other mechanism if available (machinecheck etc.).
872 		 */
873 		if (PCIE_IS_RP(PCIE_DIP2UPBUS(dip)))
874 			pcieb->pcieb_no_aer_msi = B_TRUE;
875 	}
876 
877 	if (intr_types & DDI_INTR_TYPE_FIXED) {
878 		if (pcieb_intr_init(pcieb, DDI_INTR_TYPE_FIXED) !=
879 		    DDI_SUCCESS) {
880 			PCIEB_DEBUG(DBG_ATTACH, dip,
881 			    "Unable to attach INTx handler\n");
882 			goto FAIL;
883 		}
884 	}
885 	return (DDI_SUCCESS);
886 
887 FAIL:
888 	return (DDI_FAILURE);
889 }
890 
891 /*
892  * This function initializes internally generated interrupts only.
893  * It does not affect any interrupts generated by downstream devices
894  * or the forwarding of them.
895  *
896  * Enable Device Specific Interrupts or Hotplug features here.
897  * Enabling features may change how many interrupts are requested
898  * by the device.  If features are not enabled first, the
899  * device might not ask for any interrupts.
900  */
901 
902 static int
903 pcieb_intr_init(pcieb_devstate_t *pcieb, int intr_type)
904 {
905 	dev_info_t	*dip = pcieb->pcieb_dip;
906 	int		nintrs, request, count, x;
907 	int		intr_cap = 0;
908 	int		inum = 0;
909 	int		ret, hp_msi_off;
910 	pcie_bus_t	*bus_p = PCIE_DIP2UPBUS(dip);
911 	uint16_t	vendorid = bus_p->bus_dev_ven_id & 0xFFFF;
912 	boolean_t	is_hp = B_FALSE;
913 	boolean_t	is_pme = B_FALSE;
914 
915 	PCIEB_DEBUG(DBG_ATTACH, dip, "pcieb_intr_init: Attaching %s handler\n",
916 	    (intr_type == DDI_INTR_TYPE_MSI) ? "MSI" : "INTx");
917 
918 	request = 0;
919 	if (PCIE_IS_HOTPLUG_ENABLED(dip)) {
920 		request++;
921 		is_hp = B_TRUE;
922 	}
923 
924 	/*
925 	 * Hotplug and PME share the same MSI vector. If hotplug is not
926 	 * supported check if MSI is needed for PME.
927 	 */
928 	if ((intr_type == DDI_INTR_TYPE_MSI) && PCIE_IS_RP(bus_p) &&
929 	    (vendorid == NVIDIA_VENDOR_ID)) {
930 		is_pme = B_TRUE;
931 		if (!is_hp)
932 			request++;
933 	}
934 
935 	/*
936 	 * Setup MSI if this device is a Rootport and has AER. Currently no
937 	 * SPARC Root Port supports fabric errors being reported through it.
938 	 */
939 	if (intr_type == DDI_INTR_TYPE_MSI) {
940 		if (PCIE_IS_RP(bus_p) && PCIE_HAS_AER(bus_p))
941 			request++;
942 	}
943 
944 	if (request == 0)
945 		return (DDI_SUCCESS);
946 
947 	/*
948 	 * Get number of supported interrupts.
949 	 *
950 	 * Several Bridges/Switches will not have this property set, resulting
951 	 * in a FAILURE, if the device is not configured in a way that
952 	 * interrupts are needed. (eg. hotplugging)
953 	 */
954 	ret = ddi_intr_get_nintrs(dip, intr_type, &nintrs);
955 	if ((ret != DDI_SUCCESS) || (nintrs == 0)) {
956 		PCIEB_DEBUG(DBG_ATTACH, dip, "ddi_intr_get_nintrs ret:%d"
957 		    " req:%d\n", ret, nintrs);
958 		return (DDI_FAILURE);
959 	}
960 
961 	PCIEB_DEBUG(DBG_ATTACH, dip, "bdf 0x%x: ddi_intr_get_nintrs: nintrs %d",
962 	    " request %d\n", bus_p->bus_bdf, nintrs, request);
963 
964 	if (request > nintrs)
965 		request = nintrs;
966 
967 	/* Allocate an array of interrupt handlers */
968 	pcieb->pcieb_htable_size = sizeof (ddi_intr_handle_t) * request;
969 	pcieb->pcieb_htable = kmem_zalloc(pcieb->pcieb_htable_size,
970 	    KM_SLEEP);
971 	pcieb->pcieb_init_flags |= PCIEB_INIT_HTABLE;
972 
973 	ret = ddi_intr_alloc(dip, pcieb->pcieb_htable, intr_type, inum,
974 	    request, &count, DDI_INTR_ALLOC_NORMAL);
975 	if ((ret != DDI_SUCCESS) || (count == 0)) {
976 		PCIEB_DEBUG(DBG_ATTACH, dip, "ddi_intr_alloc() ret: %d ask: %d"
977 		    " actual: %d\n", ret, request, count);
978 		goto FAIL;
979 	}
980 	pcieb->pcieb_init_flags |= PCIEB_INIT_ALLOC;
981 
982 	/* Save the actual number of interrupts allocated */
983 	pcieb->pcieb_intr_count = count;
984 	if (count < request) {
985 		PCIEB_DEBUG(DBG_ATTACH, dip, "bdf 0%x: Requested Intr: %d"
986 		    " Received: %d\n", bus_p->bus_bdf, request, count);
987 	}
988 
989 	/*
990 	 * NVidia (MCP55 and other) chipsets have a errata that if the number
991 	 * of requested MSI intrs is not allocated we have to fall back to INTx.
992 	 */
993 	if (intr_type == DDI_INTR_TYPE_MSI) {
994 		if (PCIE_IS_RP(bus_p) && (vendorid == NVIDIA_VENDOR_ID)) {
995 			if (request != count)
996 				goto FAIL;
997 		}
998 	}
999 
1000 	/* Get interrupt priority */
1001 	ret = ddi_intr_get_pri(pcieb->pcieb_htable[0],
1002 	    &pcieb->pcieb_intr_priority);
1003 	if (ret != DDI_SUCCESS) {
1004 		PCIEB_DEBUG(DBG_ATTACH, dip, "ddi_intr_get_pri() ret: %d\n",
1005 		    ret);
1006 		goto FAIL;
1007 	}
1008 
1009 	if (pcieb->pcieb_intr_priority >= LOCK_LEVEL) {
1010 		pcieb->pcieb_intr_priority = LOCK_LEVEL - 1;
1011 		ret = ddi_intr_set_pri(pcieb->pcieb_htable[0],
1012 		    pcieb->pcieb_intr_priority);
1013 		if (ret != DDI_SUCCESS) {
1014 			PCIEB_DEBUG(DBG_ATTACH, dip, "ddi_intr_set_pri() ret:"
1015 			" %d\n", ret);
1016 
1017 			goto FAIL;
1018 		}
1019 	}
1020 
1021 	mutex_init(&pcieb->pcieb_intr_mutex, NULL, MUTEX_DRIVER, NULL);
1022 
1023 	pcieb->pcieb_init_flags |= PCIEB_INIT_MUTEX;
1024 
1025 	for (count = 0; count < pcieb->pcieb_intr_count; count++) {
1026 		ret = ddi_intr_add_handler(pcieb->pcieb_htable[count],
1027 		    pcieb_intr_handler, (caddr_t)pcieb,
1028 		    (caddr_t)(uintptr_t)(inum + count));
1029 
1030 		if (ret != DDI_SUCCESS) {
1031 			PCIEB_DEBUG(DBG_ATTACH, dip, "Cannot add "
1032 			    "interrupt(%d)\n", ret);
1033 			break;
1034 		}
1035 	}
1036 
1037 	/* If unsucessful, remove the added handlers */
1038 	if (ret != DDI_SUCCESS) {
1039 		for (x = 0; x < count; x++) {
1040 			(void) ddi_intr_remove_handler(pcieb->pcieb_htable[x]);
1041 		}
1042 		goto FAIL;
1043 	}
1044 
1045 	pcieb->pcieb_init_flags |= PCIEB_INIT_HANDLER;
1046 
1047 	(void) ddi_intr_get_cap(pcieb->pcieb_htable[0], &intr_cap);
1048 
1049 	/*
1050 	 * Get this intr lock because we are not quite ready to handle
1051 	 * interrupts immediately after enabling it. The MSI multi register
1052 	 * gets programmed in ddi_intr_enable after which we need to get the
1053 	 * MSI offsets for Hotplug/AER.
1054 	 */
1055 	mutex_enter(&pcieb->pcieb_intr_mutex);
1056 
1057 	if (intr_cap & DDI_INTR_FLAG_BLOCK) {
1058 		(void) ddi_intr_block_enable(pcieb->pcieb_htable,
1059 		    pcieb->pcieb_intr_count);
1060 		pcieb->pcieb_init_flags |= PCIEB_INIT_BLOCK;
1061 	} else {
1062 		for (count = 0; count < pcieb->pcieb_intr_count; count++) {
1063 			(void) ddi_intr_enable(pcieb->pcieb_htable[count]);
1064 		}
1065 	}
1066 	pcieb->pcieb_init_flags |= PCIEB_INIT_ENABLE;
1067 
1068 	/* Save the interrupt type */
1069 	pcieb->pcieb_intr_type = intr_type;
1070 
1071 	/* Get the MSI offset for hotplug/PME from the PCIe cap reg */
1072 	if (intr_type == DDI_INTR_TYPE_MSI) {
1073 		hp_msi_off = PCI_CAP_GET16(bus_p->bus_cfg_hdl, NULL,
1074 		    bus_p->bus_pcie_off, PCIE_PCIECAP) &
1075 		    PCIE_PCIECAP_INT_MSG_NUM;
1076 
1077 		if (hp_msi_off >= count) {
1078 			PCIEB_DEBUG(DBG_ATTACH, dip, "MSI number %d in PCIe "
1079 			    "cap > max allocated %d\n", hp_msi_off, count);
1080 			mutex_exit(&pcieb->pcieb_intr_mutex);
1081 			goto FAIL;
1082 		}
1083 
1084 		if (is_hp)
1085 			pcieb->pcieb_isr_tab[hp_msi_off] |= PCIEB_INTR_SRC_HP;
1086 
1087 		if (is_pme)
1088 			pcieb->pcieb_isr_tab[hp_msi_off] |= PCIEB_INTR_SRC_PME;
1089 	} else {
1090 		/* INTx handles only Hotplug interrupts */
1091 		if (is_hp)
1092 			pcieb->pcieb_isr_tab[0] |= PCIEB_INTR_SRC_HP;
1093 	}
1094 
1095 
1096 	/*
1097 	 * Get the MSI offset for errors from the AER Root Error status
1098 	 * register.
1099 	 */
1100 	if ((intr_type == DDI_INTR_TYPE_MSI) && PCIE_IS_RP(bus_p)) {
1101 		if (PCIE_HAS_AER(bus_p)) {
1102 			int aer_msi_off;
1103 			aer_msi_off = (PCI_XCAP_GET32(bus_p->bus_cfg_hdl, NULL,
1104 			    bus_p->bus_aer_off, PCIE_AER_RE_STS) >>
1105 			    PCIE_AER_RE_STS_MSG_NUM_SHIFT) &
1106 			    PCIE_AER_RE_STS_MSG_NUM_MASK;
1107 
1108 			if (aer_msi_off >= count) {
1109 				PCIEB_DEBUG(DBG_ATTACH, dip, "MSI number %d in"
1110 				    " AER cap > max allocated %d\n",
1111 				    aer_msi_off, count);
1112 				mutex_exit(&pcieb->pcieb_intr_mutex);
1113 				goto FAIL;
1114 			}
1115 			pcieb->pcieb_isr_tab[aer_msi_off] |= PCIEB_INTR_SRC_AER;
1116 		} else {
1117 			/*
1118 			 * This RP does not have AER. Fallback to the
1119 			 * SERR+Machinecheck approach if available.
1120 			 */
1121 			pcieb->pcieb_no_aer_msi = B_TRUE;
1122 		}
1123 	}
1124 
1125 	mutex_exit(&pcieb->pcieb_intr_mutex);
1126 	return (DDI_SUCCESS);
1127 
1128 FAIL:
1129 	pcieb_intr_fini(pcieb);
1130 	return (DDI_FAILURE);
1131 }
1132 
1133 static void
1134 pcieb_intr_fini(pcieb_devstate_t *pcieb)
1135 {
1136 	int x;
1137 	int count = pcieb->pcieb_intr_count;
1138 	int flags = pcieb->pcieb_init_flags;
1139 
1140 	if ((flags & PCIEB_INIT_ENABLE) &&
1141 	    (flags & PCIEB_INIT_BLOCK)) {
1142 		(void) ddi_intr_block_disable(pcieb->pcieb_htable, count);
1143 		flags &= ~(PCIEB_INIT_ENABLE |
1144 		    PCIEB_INIT_BLOCK);
1145 	}
1146 
1147 	if (flags & PCIEB_INIT_MUTEX)
1148 		mutex_destroy(&pcieb->pcieb_intr_mutex);
1149 
1150 	for (x = 0; x < count; x++) {
1151 		if (flags & PCIEB_INIT_ENABLE)
1152 			(void) ddi_intr_disable(pcieb->pcieb_htable[x]);
1153 
1154 		if (flags & PCIEB_INIT_HANDLER)
1155 			(void) ddi_intr_remove_handler(pcieb->pcieb_htable[x]);
1156 
1157 		if (flags & PCIEB_INIT_ALLOC)
1158 			(void) ddi_intr_free(pcieb->pcieb_htable[x]);
1159 	}
1160 
1161 	flags &= ~(PCIEB_INIT_ENABLE | PCIEB_INIT_HANDLER | PCIEB_INIT_ALLOC |
1162 	    PCIEB_INIT_MUTEX);
1163 
1164 	if (flags & PCIEB_INIT_HTABLE)
1165 		kmem_free(pcieb->pcieb_htable, pcieb->pcieb_htable_size);
1166 
1167 	flags &= ~PCIEB_INIT_HTABLE;
1168 
1169 	pcieb->pcieb_init_flags &= flags;
1170 }
1171 
1172 /*
1173  * Checks if this device needs MSIs enabled or not.
1174  */
1175 /*ARGSUSED*/
1176 static int
1177 pcieb_msi_supported(dev_info_t *dip)
1178 {
1179 	return ((pcieb_enable_msi && pcieb_plat_msi_supported(dip)) ?
1180 	    DDI_SUCCESS: DDI_FAILURE);
1181 }
1182 
1183 /*ARGSUSED*/
1184 static int
1185 pcieb_fm_init_child(dev_info_t *dip, dev_info_t *tdip, int cap,
1186     ddi_iblock_cookie_t *ibc)
1187 {
1188 	pcieb_devstate_t  *pcieb = ddi_get_soft_state(pcieb_state,
1189 	    ddi_get_instance(dip));
1190 
1191 	ASSERT(ibc != NULL);
1192 	*ibc = pcieb->pcieb_fm_ibc;
1193 
1194 	return (DEVI(dip)->devi_fmhdl->fh_cap | DDI_FM_ACCCHK_CAPABLE |
1195 	    DDI_FM_DMACHK_CAPABLE);
1196 }
1197 
1198 static int
1199 pcieb_fm_init(pcieb_devstate_t *pcieb_p)
1200 {
1201 	dev_info_t	*dip = pcieb_p->pcieb_dip;
1202 	int		fm_cap = DDI_FM_EREPORT_CAPABLE;
1203 
1204 	/*
1205 	 * Request our capability level and get our parents capability
1206 	 * and ibc.
1207 	 */
1208 	ddi_fm_init(dip, &fm_cap, &pcieb_p->pcieb_fm_ibc);
1209 
1210 	return (DDI_SUCCESS);
1211 }
1212 
1213 /*
1214  * Breakdown our FMA resources
1215  */
1216 static void
1217 pcieb_fm_fini(pcieb_devstate_t *pcieb_p)
1218 {
1219 	/*
1220 	 * Clean up allocated fm structures
1221 	 */
1222 	ddi_fm_fini(pcieb_p->pcieb_dip);
1223 }
1224 
1225 static int
1226 pcieb_open(dev_t *devp, int flags, int otyp, cred_t *credp)
1227 {
1228 	int		inst = PCI_MINOR_NUM_TO_INSTANCE(getminor(*devp));
1229 	pcieb_devstate_t	*pcieb = ddi_get_soft_state(pcieb_state, inst);
1230 	int	rv;
1231 
1232 	if (pcieb == NULL)
1233 		return (ENXIO);
1234 
1235 	mutex_enter(&pcieb->pcieb_mutex);
1236 	rv = pcie_open(pcieb->pcieb_dip, devp, flags, otyp, credp);
1237 	mutex_exit(&pcieb->pcieb_mutex);
1238 
1239 	return (rv);
1240 }
1241 
1242 static int
1243 pcieb_close(dev_t dev, int flags, int otyp, cred_t *credp)
1244 {
1245 	int		inst = PCI_MINOR_NUM_TO_INSTANCE(getminor(dev));
1246 	pcieb_devstate_t	*pcieb = ddi_get_soft_state(pcieb_state, inst);
1247 	int	rv;
1248 
1249 	if (pcieb == NULL)
1250 		return (ENXIO);
1251 
1252 	mutex_enter(&pcieb->pcieb_mutex);
1253 	rv = pcie_close(pcieb->pcieb_dip, dev, flags, otyp, credp);
1254 	mutex_exit(&pcieb->pcieb_mutex);
1255 
1256 	return (rv);
1257 }
1258 
1259 static int
1260 pcieb_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp,
1261 	int *rvalp)
1262 {
1263 	int		inst = PCI_MINOR_NUM_TO_INSTANCE(getminor(dev));
1264 	pcieb_devstate_t	*pcieb = ddi_get_soft_state(pcieb_state, inst);
1265 	int		rv;
1266 
1267 	if (pcieb == NULL)
1268 		return (ENXIO);
1269 
1270 	/* To handle devctl and hotplug related ioctls */
1271 	rv = pcie_ioctl(pcieb->pcieb_dip, dev, cmd, arg, mode, credp, rvalp);
1272 
1273 	return (rv);
1274 }
1275 
1276 /*
1277  * Common interrupt handler for hotplug, PME and errors.
1278  */
1279 static uint_t
1280 pcieb_intr_handler(caddr_t arg1, caddr_t arg2)
1281 {
1282 	pcieb_devstate_t *pcieb_p = (pcieb_devstate_t *)arg1;
1283 	dev_info_t	*dip = pcieb_p->pcieb_dip;
1284 	ddi_fm_error_t	derr;
1285 	int		sts = 0;
1286 	int		ret = DDI_INTR_UNCLAIMED;
1287 	int		isrc;
1288 
1289 	if (!(pcieb_p->pcieb_init_flags & PCIEB_INIT_ENABLE))
1290 		goto FAIL;
1291 
1292 	mutex_enter(&pcieb_p->pcieb_intr_mutex);
1293 	isrc = pcieb_p->pcieb_isr_tab[(int)(uintptr_t)arg2];
1294 	mutex_exit(&pcieb_p->pcieb_intr_mutex);
1295 
1296 	PCIEB_DEBUG(DBG_INTR, dip, "Received intr number %d\n",
1297 	    (int)(uintptr_t)arg2);
1298 
1299 	if (isrc == PCIEB_INTR_SRC_UNKNOWN)
1300 		goto FAIL;
1301 
1302 	if (isrc & PCIEB_INTR_SRC_HP)
1303 		ret = pcie_intr(dip);
1304 
1305 	if (isrc & PCIEB_INTR_SRC_PME)
1306 		ret = DDI_INTR_CLAIMED;
1307 
1308 	/* AER Error */
1309 	if (isrc & PCIEB_INTR_SRC_AER) {
1310 		/*
1311 		 *  If MSI is shared with PME/hotplug then check Root Error
1312 		 *  Status Reg before claiming it. For now it's ok since
1313 		 *  we know we get 2 MSIs.
1314 		 */
1315 		ret = DDI_INTR_CLAIMED;
1316 		bzero(&derr, sizeof (ddi_fm_error_t));
1317 		derr.fme_version = DDI_FME_VERSION;
1318 		mutex_enter(&pcieb_p->pcieb_peek_poke_mutex);
1319 		mutex_enter(&pcieb_p->pcieb_err_mutex);
1320 
1321 		if ((DEVI(dip)->devi_fmhdl->fh_cap) & DDI_FM_EREPORT_CAPABLE)
1322 			sts = pf_scan_fabric(dip, &derr, NULL);
1323 
1324 		mutex_exit(&pcieb_p->pcieb_err_mutex);
1325 		mutex_exit(&pcieb_p->pcieb_peek_poke_mutex);
1326 		if (pcieb_die & sts)
1327 			fm_panic("%s-%d: PCI(-X) Express Fatal Error. (0x%x)",
1328 			    ddi_driver_name(dip), ddi_get_instance(dip), sts);
1329 	}
1330 FAIL:
1331 	return (ret);
1332 }
1333 
1334 /*
1335  * Some PCI-X to PCI-E bridges do not support full 64-bit addressing on the
1336  * PCI-X side of the bridge.  We build a special version of this driver for
1337  * those bridges, which uses PCIEB_ADDR_LIMIT_LO and/or PCIEB_ADDR_LIMIT_HI
1338  * to define the range of values which the chip can handle.  The code below
1339  * then clamps the DMA address range supplied by the driver, preventing the
1340  * PCI-E nexus driver from allocating any memory the bridge can't deal
1341  * with.
1342  */
1343 static int
1344 pcieb_dma_allochdl(dev_info_t *dip, dev_info_t *rdip,
1345 	ddi_dma_attr_t *attr_p, int (*waitfp)(caddr_t), caddr_t arg,
1346 	ddi_dma_handle_t *handlep)
1347 {
1348 	int		ret;
1349 #ifdef	BCM_SW_WORKAROUNDS
1350 	uint64_t	lim;
1351 
1352 	/*
1353 	 * If the leaf device's limits are outside than what the Broadcom
1354 	 * bridge can handle, we need to clip the values passed up the chain.
1355 	 */
1356 	lim = attr_p->dma_attr_addr_lo;
1357 	attr_p->dma_attr_addr_lo = MAX(lim, PCIEB_ADDR_LIMIT_LO);
1358 
1359 	lim = attr_p->dma_attr_addr_hi;
1360 	attr_p->dma_attr_addr_hi = MIN(lim, PCIEB_ADDR_LIMIT_HI);
1361 
1362 #endif	/* BCM_SW_WORKAROUNDS */
1363 
1364 	/*
1365 	 * This is a software workaround to fix the Broadcom 5714/5715 PCIe-PCI
1366 	 * bridge prefetch bug. Intercept the DMA alloc handle request and set
1367 	 * PX_DMAI_FLAGS_MAP_BUFZONE flag in the handle. If this flag is set,
1368 	 * the px nexus driver will allocate an extra page & make it valid one,
1369 	 * for any DVMA request that comes from any of the Broadcom bridge child
1370 	 * devices.
1371 	 */
1372 	if ((ret = ddi_dma_allochdl(dip, rdip, attr_p, waitfp, arg,
1373 	    handlep)) == DDI_SUCCESS) {
1374 		ddi_dma_impl_t	*mp = (ddi_dma_impl_t *)*handlep;
1375 #ifdef	BCM_SW_WORKAROUNDS
1376 		mp->dmai_inuse |= PX_DMAI_FLAGS_MAP_BUFZONE;
1377 #endif	/* BCM_SW_WORKAROUNDS */
1378 		/*
1379 		 * For a given rdip, update mp->dmai_bdf with the bdf value
1380 		 * of pcieb's immediate child or secondary bus-id of the
1381 		 * PCIe2PCI bridge.
1382 		 */
1383 		mp->dmai_minxfer = pcie_get_bdf_for_dma_xfer(dip, rdip);
1384 	}
1385 
1386 	return (ret);
1387 }
1388 
1389 /*
1390  * FDVMA feature is not supported for any child device of Broadcom 5714/5715
1391  * PCIe-PCI bridge due to prefetch bug. Return failure immediately, so that
1392  * these drivers will switch to regular DVMA path.
1393  */
1394 /*ARGSUSED*/
1395 static int
1396 pcieb_dma_mctl(dev_info_t *dip, dev_info_t *rdip, ddi_dma_handle_t handle,
1397 	enum ddi_dma_ctlops cmd, off_t *offp, size_t *lenp, caddr_t *objp,
1398 	uint_t cache_flags)
1399 {
1400 	int	ret;
1401 
1402 #ifdef	BCM_SW_WORKAROUNDS
1403 	if (cmd == DDI_DMA_RESERVE)
1404 		return (DDI_FAILURE);
1405 #endif	/* BCM_SW_WORKAROUNDS */
1406 
1407 	if (((ret = ddi_dma_mctl(dip, rdip, handle, cmd, offp, lenp, objp,
1408 	    cache_flags)) == DDI_SUCCESS) && (cmd == DDI_DMA_RESERVE)) {
1409 		ddi_dma_impl_t	*mp = (ddi_dma_impl_t *)*objp;
1410 
1411 		/*
1412 		 * For a given rdip, update mp->dmai_bdf with the bdf value
1413 		 * of pcieb's immediate child or secondary bus-id of the
1414 		 * PCIe2PCI bridge.
1415 		 */
1416 		mp->dmai_minxfer = pcie_get_bdf_for_dma_xfer(dip, rdip);
1417 	}
1418 
1419 	return (ret);
1420 }
1421 
1422 static int
1423 pcieb_intr_ops(dev_info_t *dip, dev_info_t *rdip, ddi_intr_op_t intr_op,
1424     ddi_intr_handle_impl_t *hdlp, void *result)
1425 {
1426 	return (pcieb_plat_intr_ops(dip, rdip, intr_op, hdlp, result));
1427 
1428 }
1429 
1430 /*
1431  * Power management related initialization specific to pcieb.
1432  * Called by pcieb_attach()
1433  */
1434 static int
1435 pcieb_pwr_setup(dev_info_t *dip)
1436 {
1437 	char *comp_array[5];
1438 	int i;
1439 	ddi_acc_handle_t conf_hdl;
1440 	uint16_t pmcap, cap_ptr;
1441 	pcie_pwr_t *pwr_p;
1442 
1443 	/* Some platforms/devices may choose to disable PM */
1444 	if (pcieb_plat_pwr_disable(dip)) {
1445 		(void) pcieb_pwr_disable(dip);
1446 		return (DDI_SUCCESS);
1447 	}
1448 
1449 	ASSERT(PCIE_PMINFO(dip));
1450 	pwr_p = PCIE_NEXUS_PMINFO(dip);
1451 	ASSERT(pwr_p);
1452 
1453 	/* Code taken from pci_pci driver */
1454 	if (pci_config_setup(dip, &pwr_p->pwr_conf_hdl) != DDI_SUCCESS) {
1455 		PCIEB_DEBUG(DBG_PWR, dip, "pcieb_pwr_setup: pci_config_setup "
1456 		    "failed\n");
1457 		return (DDI_FAILURE);
1458 	}
1459 	conf_hdl = pwr_p->pwr_conf_hdl;
1460 
1461 	/*
1462 	 * Walk the capabilities searching for a PM entry.
1463 	 */
1464 	if ((PCI_CAP_LOCATE(conf_hdl, PCI_CAP_ID_PM, &cap_ptr)) ==
1465 	    DDI_FAILURE) {
1466 		PCIEB_DEBUG(DBG_PWR, dip, "switch/bridge does not support PM. "
1467 		    " PCI PM data structure not found in config header\n");
1468 		pci_config_teardown(&conf_hdl);
1469 		return (DDI_SUCCESS);
1470 	}
1471 	/*
1472 	 * Save offset to pmcsr for future references.
1473 	 */
1474 	pwr_p->pwr_pmcsr_offset = cap_ptr + PCI_PMCSR;
1475 	pmcap = PCI_CAP_GET16(conf_hdl, NULL, cap_ptr, PCI_PMCAP);
1476 	if (pmcap & PCI_PMCAP_D1) {
1477 		PCIEB_DEBUG(DBG_PWR, dip, "D1 state supported\n");
1478 		pwr_p->pwr_pmcaps |= PCIE_SUPPORTS_D1;
1479 	}
1480 	if (pmcap & PCI_PMCAP_D2) {
1481 		PCIEB_DEBUG(DBG_PWR, dip, "D2 state supported\n");
1482 		pwr_p->pwr_pmcaps |= PCIE_SUPPORTS_D2;
1483 	}
1484 
1485 	i = 0;
1486 	comp_array[i++] = "NAME=PCIe switch/bridge PM";
1487 	comp_array[i++] = "0=Power Off (D3)";
1488 	if (pwr_p->pwr_pmcaps & PCIE_SUPPORTS_D2)
1489 		comp_array[i++] = "1=D2";
1490 	if (pwr_p->pwr_pmcaps & PCIE_SUPPORTS_D1)
1491 		comp_array[i++] = "2=D1";
1492 	comp_array[i++] = "3=Full Power D0";
1493 
1494 	/*
1495 	 * Create pm-components property, if it does not exist already.
1496 	 */
1497 	if (ddi_prop_update_string_array(DDI_DEV_T_NONE, dip,
1498 	    "pm-components", comp_array, i) != DDI_PROP_SUCCESS) {
1499 		PCIEB_DEBUG(DBG_PWR, dip, "could not create pm-components "
1500 		    " prop\n");
1501 		pci_config_teardown(&conf_hdl);
1502 		return (DDI_FAILURE);
1503 	}
1504 	return (pcieb_pwr_init_and_raise(dip, pwr_p));
1505 }
1506 
1507 /*
1508  * undo whatever is done in pcieb_pwr_setup. called by pcieb_detach()
1509  */
1510 static void
1511 pcieb_pwr_teardown(dev_info_t *dip)
1512 {
1513 	pcie_pwr_t	*pwr_p;
1514 
1515 	if (!PCIE_PMINFO(dip) || !(pwr_p = PCIE_NEXUS_PMINFO(dip)))
1516 		return;
1517 
1518 	(void) ddi_prop_remove(DDI_DEV_T_NONE, dip, "pm-components");
1519 	if (pwr_p->pwr_conf_hdl)
1520 		pci_config_teardown(&pwr_p->pwr_conf_hdl);
1521 }
1522 
1523 /*
1524  * Initializes the power level and raise the power to D0, if it is
1525  * not at D0.
1526  */
1527 static int
1528 pcieb_pwr_init_and_raise(dev_info_t *dip, pcie_pwr_t *pwr_p)
1529 {
1530 	uint16_t pmcsr;
1531 	int ret = DDI_SUCCESS;
1532 
1533 	/*
1534 	 * Intialize our power level from PMCSR. The common code initializes
1535 	 * this to UNKNOWN. There is no guarantee that we will be at full
1536 	 * power at attach. If we are not at D0, raise the power.
1537 	 */
1538 	pmcsr = pci_config_get16(pwr_p->pwr_conf_hdl, pwr_p->pwr_pmcsr_offset);
1539 	pmcsr &= PCI_PMCSR_STATE_MASK;
1540 	switch (pmcsr) {
1541 	case PCI_PMCSR_D0:
1542 		pwr_p->pwr_func_lvl = PM_LEVEL_D0;
1543 		break;
1544 
1545 	case PCI_PMCSR_D1:
1546 		pwr_p->pwr_func_lvl = PM_LEVEL_D1;
1547 		break;
1548 
1549 	case PCI_PMCSR_D2:
1550 		pwr_p->pwr_func_lvl = PM_LEVEL_D2;
1551 		break;
1552 
1553 	case PCI_PMCSR_D3HOT:
1554 		pwr_p->pwr_func_lvl = PM_LEVEL_D3;
1555 		break;
1556 
1557 	default:
1558 		break;
1559 	}
1560 
1561 	/* Raise the power to D0. */
1562 	if (pwr_p->pwr_func_lvl != PM_LEVEL_D0 &&
1563 	    ((ret = pm_raise_power(dip, 0, PM_LEVEL_D0)) != DDI_SUCCESS)) {
1564 		/*
1565 		 * Read PMCSR again. If it is at D0, ignore the return
1566 		 * value from pm_raise_power.
1567 		 */
1568 		pmcsr = pci_config_get16(pwr_p->pwr_conf_hdl,
1569 		    pwr_p->pwr_pmcsr_offset);
1570 		if ((pmcsr & PCI_PMCSR_STATE_MASK) == PCI_PMCSR_D0)
1571 			ret = DDI_SUCCESS;
1572 		else {
1573 			PCIEB_DEBUG(DBG_PWR, dip, "pcieb_pwr_setup: could not "
1574 			    "raise power to D0 \n");
1575 		}
1576 	}
1577 	if (ret == DDI_SUCCESS)
1578 		pwr_p->pwr_func_lvl = PM_LEVEL_D0;
1579 	return (ret);
1580 }
1581 
1582 /*
1583  * Disable PM for x86 and PLX 8532 switch.
1584  * For PLX Transitioning one port on this switch to low power causes links
1585  * on other ports on the same station to die. Due to PLX erratum #34, we
1586  * can't allow the downstream device go to non-D0 state.
1587  */
1588 static int
1589 pcieb_pwr_disable(dev_info_t *dip)
1590 {
1591 	pcie_pwr_t *pwr_p;
1592 
1593 	ASSERT(PCIE_PMINFO(dip));
1594 	pwr_p = PCIE_NEXUS_PMINFO(dip);
1595 	ASSERT(pwr_p);
1596 	PCIEB_DEBUG(DBG_PWR, dip, "pcieb_pwr_disable: disabling PM\n");
1597 	pwr_p->pwr_func_lvl = PM_LEVEL_D0;
1598 	pwr_p->pwr_flags = PCIE_NO_CHILD_PM;
1599 	return (DDI_SUCCESS);
1600 }
1601 
1602 #ifdef DEBUG
1603 int pcieb_dbg_intr_print = 0;
1604 void
1605 pcieb_dbg(uint_t bit, dev_info_t *dip, char *fmt, ...)
1606 {
1607 	va_list ap;
1608 
1609 	if (!pcieb_dbg_print)
1610 		return;
1611 
1612 	if (dip)
1613 		prom_printf("%s(%d): %s", ddi_driver_name(dip),
1614 		    ddi_get_instance(dip), pcieb_debug_sym[bit]);
1615 
1616 	va_start(ap, fmt);
1617 	if (servicing_interrupt()) {
1618 		if (pcieb_dbg_intr_print)
1619 			prom_vprintf(fmt, ap);
1620 	} else {
1621 		prom_vprintf(fmt, ap);
1622 	}
1623 
1624 	va_end(ap);
1625 }
1626 #endif
1627 
1628 static void
1629 pcieb_id_props(pcieb_devstate_t *pcieb)
1630 {
1631 	uint64_t serialid = 0;	/* 40b field of EUI-64 serial no. register */
1632 	uint16_t cap_ptr;
1633 	uint8_t fic = 0;	/* 1 = first in chassis device */
1634 	pcie_bus_t *bus_p = PCIE_DIP2BUS(pcieb->pcieb_dip);
1635 	ddi_acc_handle_t config_handle = bus_p->bus_cfg_hdl;
1636 
1637 	/*
1638 	 * Identify first in chassis.  In the special case of a Sun branded
1639 	 * PLX device, it obviously is first in chassis.  Otherwise, in the
1640 	 * general case, look for an Expansion Slot Register and check its
1641 	 * first-in-chassis bit.
1642 	 */
1643 #ifdef	PX_PLX
1644 	uint16_t vendor_id = bus_p->bus_dev_ven_id & 0xFFFF;
1645 	uint16_t device_id = bus_p->bus_dev_ven_id >> 16;
1646 	if ((vendor_id == PXB_VENDOR_SUN) &&
1647 	    ((device_id == PXB_DEVICE_PLX_PCIX) ||
1648 	    (device_id == PXB_DEVICE_PLX_PCIE))) {
1649 		fic = 1;
1650 	}
1651 #endif	/* PX_PLX */
1652 	if ((fic == 0) && ((PCI_CAP_LOCATE(config_handle,
1653 	    PCI_CAP_ID_SLOT_ID, &cap_ptr)) != DDI_FAILURE)) {
1654 		uint8_t esr = PCI_CAP_GET8(config_handle, NULL,
1655 		    cap_ptr, PCI_CAP_ID_REGS_OFF);
1656 		if (PCI_CAPSLOT_FIC(esr))
1657 			fic = 1;
1658 	}
1659 
1660 	if ((PCI_CAP_LOCATE(config_handle,
1661 	    PCI_CAP_XCFG_SPC(PCIE_EXT_CAP_ID_SER), &cap_ptr)) != DDI_FAILURE) {
1662 		/* Serialid can be 0 thru a full 40b number */
1663 		serialid = PCI_XCAP_GET32(config_handle, NULL,
1664 		    cap_ptr, PCIE_SER_SID_UPPER_DW);
1665 		serialid <<= 32;
1666 		serialid |= PCI_XCAP_GET32(config_handle, NULL,
1667 		    cap_ptr, PCIE_SER_SID_LOWER_DW);
1668 	}
1669 
1670 	if (fic)
1671 		(void) ndi_prop_create_boolean(DDI_DEV_T_NONE, pcieb->pcieb_dip,
1672 		    "first-in-chassis");
1673 	if (serialid)
1674 		(void) ddi_prop_update_int64(DDI_DEV_T_NONE, pcieb->pcieb_dip,
1675 		    "serialid#", serialid);
1676 }
1677 
1678 static void
1679 pcieb_create_ranges_prop(dev_info_t *dip,
1680 	ddi_acc_handle_t config_handle)
1681 {
1682 	uint32_t base, limit;
1683 	ppb_ranges_t	ranges[PCIEB_RANGE_LEN];
1684 	uint8_t io_base_lo, io_limit_lo;
1685 	uint16_t io_base_hi, io_limit_hi, mem_base, mem_limit;
1686 	int i = 0, rangelen = sizeof (ppb_ranges_t)/sizeof (int);
1687 
1688 	io_base_lo = pci_config_get8(config_handle, PCI_BCNF_IO_BASE_LOW);
1689 	io_limit_lo = pci_config_get8(config_handle, PCI_BCNF_IO_LIMIT_LOW);
1690 	io_base_hi = pci_config_get16(config_handle, PCI_BCNF_IO_BASE_HI);
1691 	io_limit_hi = pci_config_get16(config_handle, PCI_BCNF_IO_LIMIT_HI);
1692 	mem_base = pci_config_get16(config_handle, PCI_BCNF_MEM_BASE);
1693 	mem_limit = pci_config_get16(config_handle, PCI_BCNF_MEM_LIMIT);
1694 
1695 	/*
1696 	 * Create ranges for IO space
1697 	 */
1698 	ranges[i].size_low = ranges[i].size_high = 0;
1699 	ranges[i].parent_mid = ranges[i].child_mid = ranges[i].parent_high = 0;
1700 	ranges[i].child_high = ranges[i].parent_high |=
1701 	    (PCI_REG_REL_M | PCI_ADDR_IO);
1702 	base = PCIEB_16bit_IOADDR(io_base_lo);
1703 	limit = PCIEB_16bit_IOADDR(io_limit_lo);
1704 
1705 	if ((io_base_lo & 0xf) == PCIEB_32BIT_IO) {
1706 		base = PCIEB_LADDR(base, io_base_hi);
1707 	}
1708 	if ((io_limit_lo & 0xf) == PCIEB_32BIT_IO) {
1709 		limit = PCIEB_LADDR(limit, io_limit_hi);
1710 	}
1711 
1712 	if ((io_base_lo & PCIEB_32BIT_IO) && (io_limit_hi > 0)) {
1713 		base = PCIEB_LADDR(base, io_base_hi);
1714 		limit = PCIEB_LADDR(limit, io_limit_hi);
1715 	}
1716 
1717 	/*
1718 	 * Create ranges for 32bit memory space
1719 	 */
1720 	base = PCIEB_32bit_MEMADDR(mem_base);
1721 	limit = PCIEB_32bit_MEMADDR(mem_limit);
1722 	ranges[i].size_low = ranges[i].size_high = 0;
1723 	ranges[i].parent_mid = ranges[i].child_mid = ranges[i].parent_high = 0;
1724 	ranges[i].child_high = ranges[i].parent_high |=
1725 	    (PCI_REG_REL_M | PCI_ADDR_MEM32);
1726 	ranges[i].child_low = ranges[i].parent_low = base;
1727 	if (limit >= base) {
1728 		ranges[i].size_low = limit - base + PCIEB_MEMGRAIN;
1729 		i++;
1730 	}
1731 
1732 	if (i) {
1733 		(void) ndi_prop_update_int_array(DDI_DEV_T_NONE, dip, "ranges",
1734 		    (int *)ranges, i * rangelen);
1735 	}
1736 }
1737 
1738 /*
1739  * For PCI and PCI-X devices including PCIe2PCI bridge, initialize
1740  * cache-line-size and latency timer configuration registers.
1741  */
1742 void
1743 pcieb_set_pci_perf_parameters(dev_info_t *dip, ddi_acc_handle_t cfg_hdl)
1744 {
1745 	uint_t	n;
1746 
1747 	/* Initialize cache-line-size configuration register if needed */
1748 	if (ddi_getprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
1749 	    "cache-line-size", 0) == 0) {
1750 		pci_config_put8(cfg_hdl, PCI_CONF_CACHE_LINESZ,
1751 		    PCIEB_CACHE_LINE_SIZE);
1752 		n = pci_config_get8(cfg_hdl, PCI_CONF_CACHE_LINESZ);
1753 		if (n != 0) {
1754 			(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
1755 			    "cache-line-size", n);
1756 		}
1757 	}
1758 
1759 	/* Initialize latency timer configuration registers if needed */
1760 	if (ddi_getprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
1761 	    "latency-timer", 0) == 0) {
1762 		uchar_t	min_gnt, latency_timer;
1763 		uchar_t header_type;
1764 
1765 		/* Determine the configuration header type */
1766 		header_type = pci_config_get8(cfg_hdl, PCI_CONF_HEADER);
1767 
1768 		if ((header_type & PCI_HEADER_TYPE_M) == PCI_HEADER_ONE) {
1769 			latency_timer = PCIEB_LATENCY_TIMER;
1770 			pci_config_put8(cfg_hdl, PCI_BCNF_LATENCY_TIMER,
1771 			    latency_timer);
1772 		} else {
1773 			min_gnt = pci_config_get8(cfg_hdl, PCI_CONF_MIN_G);
1774 			latency_timer = min_gnt * 8;
1775 		}
1776 
1777 		pci_config_put8(cfg_hdl, PCI_CONF_LATENCY_TIMER,
1778 		    latency_timer);
1779 		n = pci_config_get8(cfg_hdl, PCI_CONF_LATENCY_TIMER);
1780 		if (n != 0) {
1781 			(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
1782 			    "latency-timer", n);
1783 		}
1784 	}
1785 }
1786