xref: /titanic_41/usr/src/uts/sun4u/io/pci/pci_pci.c (revision d89fccd8788afe1e920f842edd883fe192a1b8fe)
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 2006 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  *	Sun4u PCI to PCI bus bridge nexus driver
30  */
31 
32 #include <sys/conf.h>
33 #include <sys/kmem.h>
34 #include <sys/debug.h>
35 #include <sys/modctl.h>
36 #include <sys/autoconf.h>
37 #include <sys/ddi_impldefs.h>
38 #include <sys/ddi_subrdefs.h>
39 #include <sys/pci.h>
40 #include <sys/pci_cap.h>
41 #include <sys/pci/pci_nexus.h>
42 #include <sys/pci/pci_regs.h>
43 #include <sys/ddi.h>
44 #include <sys/sunndi.h>
45 #include <sys/sunddi.h>
46 #include <sys/fm/protocol.h>
47 #include <sys/ddifm.h>
48 #include <sys/pci/pci_pwr.h>
49 #include <sys/pci/pci_debug.h>
50 #include <sys/hotplug/pci/pcihp.h>
51 #include <sys/open.h>
52 #include <sys/stat.h>
53 #include <sys/file.h>
54 
55 #define	NUM_LOGICAL_SLOTS	32
56 
57 #define	PPB_RANGE_LEN 2
58 
59 #define	PPB_32BIT_IO 1
60 #define	PPB_32bit_MEM 1
61 
62 #define	PPB_MEMGRAIN 0x100000
63 #define	PPB_IOGRAIN 0x1000
64 
65 #define	PPB_16bit_IOADDR(addr) ((uint16_t)(((uint8_t)(addr) & 0xF0) << 8))
66 #define	PPB_LADDR(lo, hi) (((uint16_t)(hi) << 16) | (uint16_t)(lo))
67 #define	PPB_32bit_MEMADDR(addr) (PPB_LADDR(0, ((uint16_t)(addr) & 0xFFF0)))
68 
69 typedef struct	slot_table {
70 	uchar_t		bus_id[128];
71 	uchar_t		slot_name[32];
72 	uint8_t		device_no;
73 	uint8_t		phys_slot_num;
74 } slot_table_t;
75 
76 /*
77  * The following typedef is used to represent an entry in the "ranges"
78  * property of a device node.
79  */
80 typedef struct {
81 	uint32_t child_high;
82 	uint32_t child_mid;
83 	uint32_t child_low;
84 	uint32_t parent_high;
85 	uint32_t parent_mid;
86 	uint32_t parent_low;
87 	uint32_t size_high;
88 	uint32_t size_low;
89 } ppb_ranges_t;
90 
91 /*
92  * The variable controls the default setting of the command register
93  * for pci devices.  See ppb_initchild() for details.
94  */
95 static ushort_t ppb_command_default = PCI_COMM_SERR_ENABLE |
96 					PCI_COMM_WAIT_CYC_ENAB |
97 					PCI_COMM_PARITY_DETECT |
98 					PCI_COMM_ME |
99 					PCI_COMM_MAE |
100 					PCI_COMM_IO;
101 
102 static int ppb_bus_map(dev_info_t *, dev_info_t *, ddi_map_req_t *,
103 	off_t, off_t, caddr_t *);
104 static int ppb_ctlops(dev_info_t *, dev_info_t *, ddi_ctl_enum_t,
105 	void *, void *);
106 static int ppb_intr_ops(dev_info_t *dip, dev_info_t *rdip,
107 	ddi_intr_op_t intr_op, ddi_intr_handle_impl_t *hdlp, void *result);
108 
109 /*
110  * fm_init busop to initialize our children
111  */
112 static int ppb_fm_init_child(dev_info_t *dip, dev_info_t *tdip, int cap,
113 		ddi_iblock_cookie_t *ibc);
114 static void ppb_bus_enter(dev_info_t *dip, ddi_acc_handle_t handle);
115 static void ppb_bus_exit(dev_info_t *dip, ddi_acc_handle_t handle);
116 static int ppb_bus_power(dev_info_t *dip, void *impl_arg, pm_bus_power_op_t op,
117     void *arg, void *result);
118 
119 struct bus_ops ppb_bus_ops = {
120 	BUSO_REV,
121 	ppb_bus_map,
122 	0,
123 	0,
124 	0,
125 	i_ddi_map_fault,
126 	ddi_dma_map,
127 	ddi_dma_allochdl,
128 	ddi_dma_freehdl,
129 	ddi_dma_bindhdl,
130 	ddi_dma_unbindhdl,
131 	ddi_dma_flush,
132 	ddi_dma_win,
133 	ddi_dma_mctl,
134 	ppb_ctlops,
135 	ddi_bus_prop_op,
136 	ndi_busop_get_eventcookie,	/* (*bus_get_eventcookie)();    */
137 	ndi_busop_add_eventcall,	/* (*bus_add_eventcall)();	*/
138 	ndi_busop_remove_eventcall,	/* (*bus_remove_eventcall)();   */
139 	ndi_post_event,			/* (*bus_post_event)();		*/
140 	0,				/* (*bus_intr_ctl)();		*/
141 	0,				/* (*bus_config)(); 		*/
142 	0,				/* (*bus_unconfig)(); 		*/
143 	ppb_fm_init_child,		/* (*bus_fm_init)(); 		*/
144 	NULL,				/* (*bus_fm_fini)(); 		*/
145 	ppb_bus_enter,			/* (*bus_enter)()		*/
146 	ppb_bus_exit,			/* (*bus_exit)()		*/
147 	ppb_bus_power,			/* (*bus_power)()		*/
148 	ppb_intr_ops			/* (*bus_intr_op)(); 		*/
149 };
150 
151 static int ppb_open(dev_t *devp, int flags, int otyp, cred_t *credp);
152 static int ppb_close(dev_t dev, int flags, int otyp, cred_t *credp);
153 static int ppb_ioctl(dev_t dev, int cmd, intptr_t arg, int mode,
154 						cred_t *credp, int *rvalp);
155 static int ppb_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op,
156     int flags, char *name, caddr_t valuep, int *lengthp);
157 
158 static struct cb_ops ppb_cb_ops = {
159 	ppb_open,			/* open */
160 	ppb_close,			/* close */
161 	nulldev,			/* strategy */
162 	nulldev,			/* print */
163 	nulldev,			/* dump */
164 	nulldev,			/* read */
165 	nulldev,			/* write */
166 	ppb_ioctl,			/* ioctl */
167 	nodev,				/* devmap */
168 	nodev,				/* mmap */
169 	nodev,				/* segmap */
170 	nochpoll,			/* poll */
171 	ppb_prop_op,			/* cb_prop_op */
172 	NULL,				/* streamtab */
173 	D_NEW | D_MP | D_HOTPLUG,	/* Driver compatibility flag */
174 	CB_REV,				/* rev */
175 	nodev,				/* int (*cb_aread)() */
176 	nodev				/* int (*cb_awrite)() */
177 };
178 
179 static int ppb_probe(dev_info_t *);
180 static int ppb_attach(dev_info_t *devi, ddi_attach_cmd_t cmd);
181 static int ppb_detach(dev_info_t *devi, ddi_detach_cmd_t cmd);
182 static int ppb_info(dev_info_t *dip, ddi_info_cmd_t infocmd,
183     void *arg, void **result);
184 static int ppb_pwr(dev_info_t *dip, int component, int level);
185 
186 struct dev_ops ppb_ops = {
187 	DEVO_REV,		/* devo_rev */
188 	0,			/* refcnt  */
189 	ppb_info,		/* info */
190 	nulldev,		/* identify */
191 	ppb_probe,		/* probe */
192 	ppb_attach,		/* attach */
193 	ppb_detach,		/* detach */
194 	nulldev,		/* reset */
195 	&ppb_cb_ops,		/* driver operations */
196 	&ppb_bus_ops,		/* bus operations */
197 	ppb_pwr
198 };
199 
200 /*
201  * Module linkage information for the kernel.
202  */
203 
204 static struct modldrv modldrv = {
205 	&mod_driverops, /* Type of module */
206 	"Standard PCI to PCI bridge nexus driver %I%",
207 	&ppb_ops,	/* driver ops */
208 };
209 
210 static struct modlinkage modlinkage = {
211 	MODREV_1,
212 	(void *)&modldrv,
213 	NULL
214 };
215 
216 /*
217  * soft state pointer and structure template:
218  */
219 static void *ppb_state;
220 
221 struct ppb_cfg_state {
222 	dev_info_t *dip;
223 	ushort_t command;
224 	uchar_t cache_line_size;
225 	uchar_t latency_timer;
226 	uchar_t header_type;
227 	uchar_t sec_latency_timer;
228 	ushort_t bridge_control;
229 };
230 
231 typedef struct {
232 
233 	dev_info_t *dip;
234 
235 	/*
236 	 * configuration register state for the bus:
237 	 */
238 	uchar_t ppb_cache_line_size;
239 	uchar_t ppb_latency_timer;
240 
241 	/*
242 	 * PM support
243 	 */
244 	ddi_acc_handle_t	ppb_conf_hdl;
245 	uint16_t		ppb_pm_cap_ptr;
246 	pci_pwr_t		*ppb_pwr_p;
247 
248 	/*
249 	 * HP support
250 	 */
251 	boolean_t		hotplug_capable;
252 
253 	kmutex_t ppb_mutex;
254 	uint_t ppb_soft_state;
255 #define	PPB_SOFT_STATE_CLOSED		0x00
256 #define	PPB_SOFT_STATE_OPEN		0x01
257 #define	PPB_SOFT_STATE_OPEN_EXCL	0x02
258 	int fm_cap;
259 	ddi_iblock_cookie_t fm_ibc;
260 } ppb_devstate_t;
261 
262 /*
263  * The following variable enables a workaround for the following obp bug:
264  *
265  *	1234181 - obp should set latency timer registers in pci
266  *		configuration header
267  *
268  * Until this bug gets fixed in the obp, the following workaround should
269  * be enabled.
270  */
271 static uint_t ppb_set_latency_timer_register = 1;
272 
273 /*
274  * The following variable enables a workaround for an obp bug to be
275  * submitted.  A bug requesting a workaround fof this problem has
276  * been filed:
277  *
278  *	1235094 - need workarounds on positron nexus drivers to set cache
279  *		line size registers
280  *
281  * Until this bug gets fixed in the obp, the following workaround should
282  * be enabled.
283  */
284 static uint_t ppb_set_cache_line_size_register = 1;
285 
286 /*
287  * forward function declarations:
288  */
289 
290 /*
291  * FMA error callback
292  * Register error handling callback with our parent. We will just call
293  * our children's error callbacks and return their status.
294  */
295 static int ppb_err_callback(dev_info_t *dip, ddi_fm_error_t *derr,
296 		const void *impl_data);
297 
298 /*
299  * init/fini routines to alloc/dealloc fm structures and
300  * register/unregister our callback.
301  */
302 static void ppb_fm_init(ppb_devstate_t *ppb_p);
303 static void ppb_fm_fini(ppb_devstate_t *ppb_p);
304 
305 static void ppb_removechild(dev_info_t *);
306 static int ppb_initchild(dev_info_t *child);
307 static dev_info_t *get_my_childs_dip(dev_info_t *dip, dev_info_t *rdip);
308 static void ppb_pwr_setup(ppb_devstate_t *ppb, dev_info_t *dip);
309 static void ppb_pwr_teardown(ppb_devstate_t *ppb, dev_info_t *dip);
310 static void ppb_init_hotplug(ppb_devstate_t *ppb);
311 static void ppb_create_ranges_prop(dev_info_t *, ddi_acc_handle_t);
312 uint64_t pci_debug_flags = 0;
313 
314 int
315 _init(void)
316 {
317 	int e;
318 	if ((e = ddi_soft_state_init(&ppb_state, sizeof (ppb_devstate_t),
319 	    1)) == 0 && (e = mod_install(&modlinkage)) != 0)
320 		ddi_soft_state_fini(&ppb_state);
321 	return (e);
322 }
323 
324 int
325 _fini(void)
326 {
327 	int e;
328 
329 	if ((e = mod_remove(&modlinkage)) == 0)
330 		ddi_soft_state_fini(&ppb_state);
331 	return (e);
332 }
333 
334 int
335 _info(struct modinfo *modinfop)
336 {
337 	return (mod_info(&modlinkage, modinfop));
338 }
339 
340 /*ARGSUSED*/
341 static int
342 ppb_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
343 {
344 	ppb_devstate_t *ppb_p;	/* per ppb state pointer */
345 	minor_t		minor = getminor((dev_t)arg);
346 	int		instance = PCIHP_AP_MINOR_NUM_TO_INSTANCE(minor);
347 
348 	ppb_p = (ppb_devstate_t *)ddi_get_soft_state(ppb_state,
349 	    instance);
350 
351 	switch (infocmd) {
352 	default:
353 		return (DDI_FAILURE);
354 
355 	case DDI_INFO_DEVT2INSTANCE:
356 		*result = (void *)(uintptr_t)instance;
357 		return (DDI_SUCCESS);
358 
359 	case DDI_INFO_DEVT2DEVINFO:
360 		if (ppb_p == NULL)
361 			return (DDI_FAILURE);
362 		*result = (void *)ppb_p->dip;
363 		return (DDI_SUCCESS);
364 	}
365 }
366 
367 /*ARGSUSED*/
368 static int
369 ppb_probe(register dev_info_t *devi)
370 {
371 	return (DDI_PROBE_SUCCESS);
372 }
373 
374 /*ARGSUSED*/
375 static int
376 ppb_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
377 {
378 	int instance;
379 	ppb_devstate_t *ppb;
380 	ddi_acc_handle_t config_handle;
381 
382 	switch (cmd) {
383 	case DDI_ATTACH:
384 
385 		/*
386 		 * Make sure the "device_type" property exists.
387 		 */
388 		(void) ddi_prop_update_string(DDI_DEV_T_NONE, devi,
389 		    "device_type", "pci");
390 
391 		/*
392 		 * Allocate and get soft state structure.
393 		 */
394 		instance = ddi_get_instance(devi);
395 		if (ddi_soft_state_zalloc(ppb_state, instance) != DDI_SUCCESS)
396 			return (DDI_FAILURE);
397 		ppb = (ppb_devstate_t *)ddi_get_soft_state(ppb_state, instance);
398 		ppb->dip = devi;
399 		mutex_init(&ppb->ppb_mutex, NULL, MUTEX_DRIVER, NULL);
400 		ppb->ppb_soft_state = PPB_SOFT_STATE_CLOSED;
401 		if (pci_config_setup(devi, &config_handle) != DDI_SUCCESS) {
402 			mutex_destroy(&ppb->ppb_mutex);
403 			ddi_soft_state_free(ppb_state, instance);
404 			return (DDI_FAILURE);
405 		}
406 		ppb_pwr_setup(ppb, devi);
407 
408 		if (PM_CAPABLE(ppb->ppb_pwr_p)) {
409 			mutex_enter(&ppb->ppb_pwr_p->pwr_mutex);
410 
411 			/*
412 			 * Before reading config registers, make sure power is
413 			 * on, and remains on.
414 			 */
415 			ppb->ppb_pwr_p->pwr_fp++;
416 
417 			pci_pwr_change(ppb->ppb_pwr_p,
418 			    ppb->ppb_pwr_p->current_lvl,
419 			    pci_pwr_new_lvl(ppb->ppb_pwr_p));
420 		}
421 
422 		ppb->ppb_cache_line_size =
423 		    pci_config_get8(config_handle, PCI_CONF_CACHE_LINESZ);
424 		ppb->ppb_latency_timer =
425 		    pci_config_get8(config_handle, PCI_CONF_LATENCY_TIMER);
426 
427 		/*
428 		 * Check whether the "ranges" property is present.
429 		 * Otherwise create the ranges property by reading
430 		 * the configuration registers
431 		 */
432 		if (ddi_prop_exists(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
433 		    "ranges") == 0) {
434 			ppb_create_ranges_prop(devi, config_handle);
435 		}
436 
437 		pci_config_teardown(&config_handle);
438 
439 		if (PM_CAPABLE(ppb->ppb_pwr_p)) {
440 			ppb->ppb_pwr_p->pwr_fp--;
441 
442 			pci_pwr_change(ppb->ppb_pwr_p,
443 			    ppb->ppb_pwr_p->current_lvl,
444 			    pci_pwr_new_lvl(ppb->ppb_pwr_p));
445 
446 			mutex_exit(&ppb->ppb_pwr_p->pwr_mutex);
447 		}
448 
449 		/*
450 		 * Initialize hotplug support on this bus. At minimum
451 		 * (for non hotplug bus) this would create ":devctl" minor
452 		 * node to support DEVCTL_DEVICE_* and DEVCTL_BUS_* ioctls
453 		 * to this bus. This all takes place if this nexus has hot-plug
454 		 * slots and successfully initializes Hot Plug Framework.
455 		 */
456 		ppb->hotplug_capable = B_FALSE;
457 		ppb_init_hotplug(ppb);
458 		if (ppb->hotplug_capable == B_FALSE) {
459 			/*
460 			 * create minor node for devctl interfaces
461 			 */
462 			if (ddi_create_minor_node(devi, "devctl", S_IFCHR,
463 			    PCIHP_AP_MINOR_NUM(instance, PCIHP_DEVCTL_MINOR),
464 			    DDI_NT_NEXUS, 0) != DDI_SUCCESS) {
465 				if (ppb->ppb_pwr_p != NULL) {
466 					ppb_pwr_teardown(ppb, devi);
467 				}
468 				mutex_destroy(&ppb->ppb_mutex);
469 				ddi_soft_state_free(ppb_state, instance);
470 				return (DDI_FAILURE);
471 			}
472 		}
473 
474 		DEBUG1(DBG_ATTACH, devi,
475 			"ppb_attach(): this nexus %s hotplug slots\n",
476 			ppb->hotplug_capable == B_TRUE ? "has":"has no");
477 
478 		ppb_fm_init(ppb);
479 		ddi_report_dev(devi);
480 
481 		return (DDI_SUCCESS);
482 
483 	case DDI_RESUME:
484 		/*
485 		 * Get the soft state structure for the bridge.
486 		 */
487 		ppb = (ppb_devstate_t *)
488 			ddi_get_soft_state(ppb_state, ddi_get_instance(devi));
489 
490 		pci_pwr_resume(devi, ppb->ppb_pwr_p);
491 
492 		return (DDI_SUCCESS);
493 	}
494 	return (DDI_FAILURE);
495 }
496 
497 /*ARGSUSED*/
498 static int
499 ppb_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
500 {
501 	ppb_devstate_t *ppb;
502 
503 	switch (cmd) {
504 	case DDI_DETACH:
505 		/*
506 		 * And finally free the per-pci soft state after
507 		 * uninitializing hotplug support for this bus.
508 		 */
509 		ppb = (ppb_devstate_t *)
510 		    ddi_get_soft_state(ppb_state, ddi_get_instance(devi));
511 
512 		ppb_fm_fini(ppb);
513 
514 		if (ppb->hotplug_capable == B_TRUE)
515 			if (pcihp_uninit(devi) == DDI_FAILURE)
516 				return (DDI_FAILURE);
517 		else
518 			ddi_remove_minor_node(devi, "devctl");
519 
520 		(void) ddi_prop_remove(DDI_DEV_T_NONE, devi, "device_type");
521 
522 		if (ppb->ppb_pwr_p != NULL) {
523 			ppb_pwr_teardown(ppb, devi);
524 		}
525 		mutex_destroy(&ppb->ppb_mutex);
526 		ddi_soft_state_free(ppb_state, ddi_get_instance(devi));
527 
528 		return (DDI_SUCCESS);
529 
530 	case DDI_SUSPEND:
531 		ppb = (ppb_devstate_t *)
532 			ddi_get_soft_state(ppb_state, ddi_get_instance(devi));
533 
534 		pci_pwr_suspend(devi, ppb->ppb_pwr_p);
535 
536 		return (DDI_SUCCESS);
537 	}
538 	return (DDI_FAILURE);
539 }
540 
541 /*ARGSUSED*/
542 static int
543 ppb_bus_map(dev_info_t *dip, dev_info_t *rdip, ddi_map_req_t *mp,
544 	off_t offset, off_t len, caddr_t *vaddrp)
545 {
546 	register dev_info_t *pdip;
547 
548 	pdip = (dev_info_t *)DEVI(dip)->devi_parent;
549 	return ((DEVI(pdip)->devi_ops->devo_bus_ops->bus_map)
550 	    (pdip, rdip, mp, offset, len, vaddrp));
551 }
552 
553 /*ARGSUSED*/
554 static int
555 ppb_ctlops(dev_info_t *dip, dev_info_t *rdip,
556 	ddi_ctl_enum_t ctlop, void *arg, void *result)
557 {
558 	pci_regspec_t *drv_regp;
559 	int	reglen;
560 	int	rn;
561 
562 	int	totreg;
563 
564 	switch (ctlop) {
565 	case DDI_CTLOPS_REPORTDEV:
566 		if (rdip == (dev_info_t *)0)
567 			return (DDI_FAILURE);
568 		cmn_err(CE_CONT, "?PCI-device: %s@%s, %s%d\n",
569 		    ddi_node_name(rdip), ddi_get_name_addr(rdip),
570 		    ddi_driver_name(rdip),
571 		    ddi_get_instance(rdip));
572 		return (DDI_SUCCESS);
573 
574 	case DDI_CTLOPS_INITCHILD:
575 		return (ppb_initchild((dev_info_t *)arg));
576 
577 	case DDI_CTLOPS_UNINITCHILD:
578 		ppb_removechild((dev_info_t *)arg);
579 		return (DDI_SUCCESS);
580 
581 	case DDI_CTLOPS_SIDDEV:
582 		return (DDI_SUCCESS);
583 
584 	case DDI_CTLOPS_REGSIZE:
585 	case DDI_CTLOPS_NREGS:
586 		if (rdip == (dev_info_t *)0)
587 			return (DDI_FAILURE);
588 		break;
589 	default:
590 		return (ddi_ctlops(dip, rdip, ctlop, arg, result));
591 	}
592 
593 	*(int *)result = 0;
594 	if (ddi_getlongprop(DDI_DEV_T_ANY, rdip,
595 		DDI_PROP_DONTPASS | DDI_PROP_CANSLEEP, "reg",
596 		(caddr_t)&drv_regp, &reglen) != DDI_SUCCESS)
597 		return (DDI_FAILURE);
598 
599 	totreg = reglen / sizeof (pci_regspec_t);
600 	if (ctlop == DDI_CTLOPS_NREGS)
601 		*(int *)result = totreg;
602 	else if (ctlop == DDI_CTLOPS_REGSIZE) {
603 		rn = *(int *)arg;
604 		if (rn >= totreg) {
605 			kmem_free(drv_regp, reglen);
606 			return (DDI_FAILURE);
607 		}
608 		*(off_t *)result = drv_regp[rn].pci_size_low |
609 			((uint64_t)drv_regp[rn].pci_size_hi << 32);
610 	}
611 
612 	kmem_free(drv_regp, reglen);
613 	return (DDI_SUCCESS);
614 }
615 
616 
617 static dev_info_t *
618 get_my_childs_dip(dev_info_t *dip, dev_info_t *rdip)
619 {
620 	dev_info_t *cdip = rdip;
621 
622 	for (; ddi_get_parent(cdip) != dip; cdip = ddi_get_parent(cdip))
623 		;
624 
625 	return (cdip);
626 }
627 
628 
629 static int
630 ppb_intr_ops(dev_info_t *dip, dev_info_t *rdip, ddi_intr_op_t intr_op,
631     ddi_intr_handle_impl_t *hdlp, void *result)
632 {
633 	dev_info_t	*cdip = rdip;
634 	pci_regspec_t	*pci_rp;
635 	int		reglen, len;
636 	uint32_t	d, intr;
637 
638 	if (hdlp->ih_type != DDI_INTR_TYPE_FIXED)
639 		goto done;
640 
641 	/*
642 	 * If the interrupt-map property is defined at this
643 	 * node, it will have performed the interrupt
644 	 * translation as part of the property, so no
645 	 * rotation needs to be done.
646 	 */
647 	if (ddi_getproplen(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
648 	    "interrupt-map", &len) == DDI_PROP_SUCCESS)
649 		goto done;
650 
651 	cdip = get_my_childs_dip(dip, rdip);
652 
653 	/*
654 	 * Use the devices reg property to determine its
655 	 * PCI bus number and device number.
656 	 */
657 	if (ddi_getlongprop(DDI_DEV_T_ANY, cdip, DDI_PROP_DONTPASS,
658 	    "reg", (caddr_t)&pci_rp, &reglen) != DDI_SUCCESS)
659 		return (DDI_FAILURE);
660 
661 	intr = hdlp->ih_vector;
662 
663 	/* Spin the interrupt */
664 	d = PCI_REG_DEV_G(pci_rp[0].pci_phys_hi);
665 
666 	if ((intr >= PCI_INTA) && (intr <= PCI_INTD))
667 		hdlp->ih_vector = ((intr - 1 + (d % 4)) % 4 + 1);
668 	else
669 		cmn_err(CE_WARN, "%s%d: %s: PCI intr=%x out of range",
670 		    ddi_driver_name(rdip), ddi_get_instance(rdip),
671 		    ddi_driver_name(dip), intr);
672 
673 	kmem_free(pci_rp, reglen);
674 
675 done:
676 	/* Pass up the request to our parent. */
677 	return (i_ddi_intr_ops(dip, rdip, intr_op, hdlp, result));
678 }
679 
680 static int
681 ppb_bus_power(dev_info_t *dip, void *impl_arg, pm_bus_power_op_t op,
682     void *arg, void *result)
683 {
684 	ppb_devstate_t *ppb;
685 
686 	ppb = (ppb_devstate_t *)ddi_get_soft_state(ppb_state,
687 	    ddi_get_instance(dip));
688 
689 	return (pci_pwr_ops(ppb->ppb_pwr_p, dip, impl_arg, op, arg, result));
690 }
691 
692 
693 /*
694  * name_child
695  *
696  * This function is called from init_child to name a node. It is
697  * also passed as a callback for node merging functions.
698  *
699  * return value: DDI_SUCCESS, DDI_FAILURE
700  */
701 static int
702 ppb_name_child(dev_info_t *child, char *name, int namelen)
703 {
704 	pci_regspec_t *pci_rp;
705 	uint_t slot, func;
706 	char **unit_addr;
707 	uint_t n;
708 
709 	/*
710 	 * Pseudo nodes indicate a prototype node with per-instance
711 	 * properties to be merged into the real h/w device node.
712 	 * The interpretation of the unit-address is DD[,F]
713 	 * where DD is the device id and F is the function.
714 	 */
715 	if (ndi_dev_is_persistent_node(child) == 0) {
716 		if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY, child,
717 		    DDI_PROP_DONTPASS, "unit-address", &unit_addr, &n) !=
718 		    DDI_PROP_SUCCESS) {
719 			cmn_err(CE_WARN, "cannot name node from %s.conf",
720 			    ddi_driver_name(child));
721 			return (DDI_FAILURE);
722 		}
723 		if (n != 1 || *unit_addr == NULL || **unit_addr == 0) {
724 			cmn_err(CE_WARN, "unit-address property in %s.conf"
725 			    " not well-formed", ddi_driver_name(child));
726 			ddi_prop_free(unit_addr);
727 			return (DDI_FAILURE);
728 		}
729 		(void) snprintf(name, namelen, "%s", *unit_addr);
730 		ddi_prop_free(unit_addr);
731 		return (DDI_SUCCESS);
732 	}
733 
734 	/*
735 	 * Get the address portion of the node name based on
736 	 * the function and device number.
737 	 */
738 	if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, child, DDI_PROP_DONTPASS,
739 	    "reg", (int **)&pci_rp, &n) != DDI_SUCCESS) {
740 		return (DDI_FAILURE);
741 	}
742 
743 	slot = PCI_REG_DEV_G(pci_rp[0].pci_phys_hi);
744 	func = PCI_REG_FUNC_G(pci_rp[0].pci_phys_hi);
745 
746 	if (func != 0)
747 		(void) snprintf(name, namelen, "%x,%x", slot, func);
748 	else
749 		(void) snprintf(name, namelen, "%x", slot);
750 
751 	ddi_prop_free(pci_rp);
752 	return (DDI_SUCCESS);
753 }
754 
755 static int
756 ppb_initchild(dev_info_t *child)
757 {
758 	char name[MAXNAMELEN];
759 	ddi_acc_handle_t config_handle;
760 	ushort_t command_preserve, command;
761 	uint_t n;
762 	ushort_t bcr;
763 	uchar_t header_type;
764 	uchar_t min_gnt, latency_timer;
765 	ppb_devstate_t *ppb;
766 
767 	/*
768 	 * Name the child
769 	 */
770 	if (ppb_name_child(child, name, MAXNAMELEN) != DDI_SUCCESS)
771 		return (DDI_FAILURE);
772 
773 	ddi_set_name_addr(child, name);
774 	ddi_set_parent_data(child, NULL);
775 
776 	/*
777 	 * Pseudo nodes indicate a prototype node with per-instance
778 	 * properties to be merged into the real h/w device node.
779 	 * The interpretation of the unit-address is DD[,F]
780 	 * where DD is the device id and F is the function.
781 	 */
782 	if (ndi_dev_is_persistent_node(child) == 0) {
783 		extern int pci_allow_pseudo_children;
784 
785 		/*
786 		 * Try to merge the properties from this prototype
787 		 * node into real h/w nodes.
788 		 */
789 		if (ndi_merge_node(child, ppb_name_child) == DDI_SUCCESS) {
790 			/*
791 			 * Merged ok - return failure to remove the node.
792 			 */
793 			ppb_removechild(child);
794 			return (DDI_FAILURE);
795 		}
796 
797 		/* workaround for ddivs to run under PCI */
798 		if (pci_allow_pseudo_children)
799 			return (DDI_SUCCESS);
800 
801 		/*
802 		 * The child was not merged into a h/w node,
803 		 * but there's not much we can do with it other
804 		 * than return failure to cause the node to be removed.
805 		 */
806 		cmn_err(CE_WARN, "!%s@%s: %s.conf properties not merged",
807 		    ddi_driver_name(child), ddi_get_name_addr(child),
808 		    ddi_driver_name(child));
809 		ppb_removechild(child);
810 		return (DDI_NOT_WELL_FORMED);
811 	}
812 
813 	ddi_set_parent_data(child, NULL);
814 
815 	ppb = (ppb_devstate_t *)ddi_get_soft_state(ppb_state,
816 	    ddi_get_instance(ddi_get_parent(child)));
817 
818 	/*
819 	 * If hardware is PM capable, set up the power info structure.
820 	 * This also ensures the the bus will not be off (0MHz) otherwise
821 	 * system panics during a bus access.
822 	 */
823 	if (PM_CAPABLE(ppb->ppb_pwr_p)) {
824 		/*
825 		 * Create a pwr_info struct for child.  Bus will be
826 		 * at full speed after creating info.
827 		 */
828 		pci_pwr_create_info(ppb->ppb_pwr_p, child);
829 #ifdef DEBUG
830 		ASSERT(ppb->ppb_pwr_p->current_lvl == PM_LEVEL_B0);
831 #endif
832 	}
833 
834 	/*
835 	 * If configuration registers were previously saved by
836 	 * child (before it entered D3), then let the child do the
837 	 * restore to set up the config regs as it'll first need to
838 	 * power the device out of D3.
839 	 */
840 	if (ddi_prop_exists(DDI_DEV_T_ANY, child, DDI_PROP_DONTPASS,
841 	    "config-regs-saved-by-child") == 1) {
842 		DEBUG2(DBG_PWR, ddi_get_parent(child),
843 			"INITCHILD: config regs to be restored by child"
844 			" for %s@%s\n", ddi_node_name(child),
845 				ddi_get_name_addr(child));
846 
847 		return (DDI_SUCCESS);
848 	}
849 
850 	DEBUG2(DBG_PWR, ddi_get_parent(child),
851 	    "INITCHILD: config regs setup for %s@%s\n",
852 	    ddi_node_name(child), ddi_get_name_addr(child));
853 
854 	if (pci_config_setup(child, &config_handle) != DDI_SUCCESS) {
855 		if (PM_CAPABLE(ppb->ppb_pwr_p)) {
856 			pci_pwr_rm_info(ppb->ppb_pwr_p, child);
857 		}
858 
859 		return (DDI_FAILURE);
860 	}
861 
862 	/*
863 	 * Determine the configuration header type.
864 	 */
865 	header_type = pci_config_get8(config_handle, PCI_CONF_HEADER);
866 
867 	/*
868 	 * Support for the "command-preserve" property.
869 	 */
870 	command_preserve = ddi_prop_get_int(DDI_DEV_T_ANY, child,
871 		DDI_PROP_DONTPASS, "command-preserve", 0);
872 	command = pci_config_get16(config_handle, PCI_CONF_COMM);
873 	command &= (command_preserve | PCI_COMM_BACK2BACK_ENAB);
874 	command |= (ppb_command_default & ~command_preserve);
875 	pci_config_put16(config_handle, PCI_CONF_COMM, command);
876 
877 	/*
878 	 * If the device has a bus control register then program it
879 	 * based on the settings in the command register.
880 	 */
881 	if ((header_type & PCI_HEADER_TYPE_M) == PCI_HEADER_ONE) {
882 		bcr = pci_config_get8(config_handle, PCI_BCNF_BCNTRL);
883 		if (ppb_command_default & PCI_COMM_PARITY_DETECT)
884 			bcr |= PCI_BCNF_BCNTRL_PARITY_ENABLE;
885 		if (ppb_command_default & PCI_COMM_SERR_ENABLE)
886 			bcr |= PCI_BCNF_BCNTRL_SERR_ENABLE;
887 		bcr |= PCI_BCNF_BCNTRL_MAST_AB_MODE;
888 		pci_config_put8(config_handle, PCI_BCNF_BCNTRL, bcr);
889 	}
890 
891 	/*
892 	 * Initialize cache-line-size configuration register if needed.
893 	 */
894 	if (ppb_set_cache_line_size_register &&
895 	    ddi_getprop(DDI_DEV_T_ANY, child, DDI_PROP_DONTPASS,
896 		"cache-line-size", 0) == 0) {
897 		pci_config_put8(config_handle, PCI_CONF_CACHE_LINESZ,
898 			ppb->ppb_cache_line_size);
899 		n = pci_config_get8(config_handle, PCI_CONF_CACHE_LINESZ);
900 		if (n != 0) {
901 			(void) ndi_prop_update_int(DDI_DEV_T_NONE, child,
902 					"cache-line-size", n);
903 		}
904 	}
905 
906 	/*
907 	 * Initialize latency timer configuration registers if needed.
908 	 */
909 	if (ppb_set_latency_timer_register &&
910 	    ddi_getprop(DDI_DEV_T_ANY, child, DDI_PROP_DONTPASS,
911 		"latency-timer", 0) == 0) {
912 
913 		if ((header_type & PCI_HEADER_TYPE_M) == PCI_HEADER_ONE) {
914 			latency_timer = ppb->ppb_latency_timer;
915 			pci_config_put8(config_handle, PCI_BCNF_LATENCY_TIMER,
916 				ppb->ppb_latency_timer);
917 		} else {
918 			min_gnt = pci_config_get8(config_handle,
919 				PCI_CONF_MIN_G);
920 			latency_timer = min_gnt * 8;
921 		}
922 		pci_config_put8(config_handle, PCI_CONF_LATENCY_TIMER,
923 			latency_timer);
924 		n = pci_config_get8(config_handle, PCI_CONF_LATENCY_TIMER);
925 		if (n != 0) {
926 			(void) ndi_prop_update_int(DDI_DEV_T_NONE, child,
927 					"latency-timer", n);
928 		}
929 	}
930 
931 	/*
932 	 * Check to see if the XMITS/PCI-X workaround applies.
933 	 */
934 	n = ddi_getprop(DDI_DEV_T_ANY, child, DDI_PROP_NOTPROM,
935 	    "pcix-update-cmd-reg", -1);
936 
937 	if (n != -1) {
938 		extern void pcix_set_cmd_reg(dev_info_t *child, uint16_t value);
939 		DEBUG1(DBG_INIT_CLD, child, "Turning on XMITS NCPQ "
940 		    "Workaround: value = %x\n", n);
941 		pcix_set_cmd_reg(child, n);
942 	}
943 
944 	pci_config_teardown(&config_handle);
945 
946 	return (DDI_SUCCESS);
947 }
948 
949 static void
950 ppb_removechild(dev_info_t *dip)
951 {
952 	ppb_devstate_t *ppb;
953 
954 	ppb = (ppb_devstate_t *)ddi_get_soft_state(ppb_state,
955 	    ddi_get_instance(ddi_get_parent(dip)));
956 
957 	if (PM_CAPABLE(ppb->ppb_pwr_p)) {
958 
959 		DEBUG2(DBG_PWR, ddi_get_parent(dip),
960 		    "UNINITCHILD: removing pwr_info for %s@%s\n",
961 		    ddi_node_name(dip), ddi_get_name_addr(dip));
962 		pci_pwr_rm_info(ppb->ppb_pwr_p, dip);
963 	}
964 
965 	ddi_set_name_addr(dip, NULL);
966 
967 	/*
968 	 * Strip the node to properly convert it back to prototype form
969 	 */
970 	ddi_remove_minor_node(dip, NULL);
971 
972 	impl_rem_dev_props(dip);
973 }
974 
975 /*
976  * If bridge is PM capable, set up PM state for nexus.
977  */
978 static void
979 ppb_pwr_setup(ppb_devstate_t *ppb, dev_info_t *pdip)
980 {
981 	char *comp_array[5];
982 	int i;
983 	ddi_acc_handle_t conf_hdl;
984 	uint8_t pmcsr_bse;
985 	uint16_t pmcap;
986 
987 	/*
988 	 * Determine if bridge is PM capable.  If not, leave ppb_pwr_p NULL
989 	 * and return.
990 	 */
991 	if (pci_config_setup(pdip, &ppb->ppb_conf_hdl) != DDI_SUCCESS) {
992 
993 		return;
994 	}
995 
996 	conf_hdl = ppb->ppb_conf_hdl;
997 
998 	/*
999 	 * Locate and store the power management cap_ptr for future references.
1000 	 */
1001 	if ((PCI_CAP_LOCATE(conf_hdl, PCI_CAP_ID_PM, &ppb->ppb_pm_cap_ptr))
1002 		== DDI_FAILURE) {
1003 		DEBUG0(DBG_PWR, pdip, "bridge does not support PM. PCI"
1004 		    " PM data structure not found in config header\n");
1005 		pci_config_teardown(&conf_hdl);
1006 
1007 		return;
1008 	}
1009 
1010 	/*
1011 	 * Allocate PM state structure for ppb.
1012 	 */
1013 	ppb->ppb_pwr_p = (pci_pwr_t *)
1014 	    kmem_zalloc(sizeof (pci_pwr_t), KM_SLEEP);
1015 	ppb->ppb_pwr_p->pwr_fp = 0;
1016 
1017 	pmcsr_bse = PCI_CAP_GET8(conf_hdl, NULL, ppb->ppb_pm_cap_ptr,
1018 		PCI_PMCSR_BSE);
1019 
1020 	pmcap = PCI_CAP_GET16(conf_hdl, NULL, ppb->ppb_pm_cap_ptr,
1021 		PCI_PMCAP);
1022 
1023 	if (pmcap == PCI_CAP_EINVAL16 || pmcsr_bse == PCI_CAP_EINVAL8) {
1024 		pci_config_teardown(&conf_hdl);
1025 		return;
1026 	}
1027 
1028 	if (pmcap & PCI_PMCAP_D1) {
1029 		DEBUG0(DBG_PWR, pdip, "setup: B1 state supported\n");
1030 		ppb->ppb_pwr_p->pwr_flags |= PCI_PWR_B1_CAPABLE;
1031 	} else {
1032 		DEBUG0(DBG_PWR, pdip, "setup: B1 state NOT supported\n");
1033 	}
1034 	if (pmcap & PCI_PMCAP_D2) {
1035 		DEBUG0(DBG_PWR, pdip, "setup: B2 state supported\n");
1036 		ppb->ppb_pwr_p->pwr_flags |= PCI_PWR_B2_CAPABLE;
1037 	} else {
1038 		DEBUG0(DBG_PWR, pdip, "setup: B2 via D2 NOT supported\n");
1039 	}
1040 
1041 	if (pmcsr_bse & PCI_PMCSR_BSE_BPCC_EN) {
1042 		DEBUG0(DBG_PWR, pdip,
1043 		"setup: bridge power/clock control enable\n");
1044 	} else {
1045 		DEBUG0(DBG_PWR, pdip,
1046 		"setup: bridge power/clock control disabled\n");
1047 
1048 		kmem_free(ppb->ppb_pwr_p, sizeof (pci_pwr_t));
1049 		ppb->ppb_pwr_p = NULL;
1050 		pci_config_teardown(&conf_hdl);
1051 
1052 		return;
1053 	}
1054 
1055 	/*
1056 	 * PCI states D0 and D3 always are supported for normal PCI
1057 	 * devices.  D1 and D2 are optional which are checked for above.
1058 	 * Bridge function states D0-D3 correspond to secondary bus states
1059 	 * B0-B3, EXCEPT if PCI_PMCSR_BSE_B2_B3 is set.  In this case, setting
1060 	 * the bridge function to D3 will set the bridge bus to state B2 instead
1061 	 * of B3.  D2 will not correspond to B2 (and in fact, probably
1062 	 * won't be D2 capable).  Implicitly, this means that if
1063 	 * PCI_PMCSR_BSE_B2_B3 is set, the bus will not be B3 capable.
1064 	 */
1065 	if (pmcsr_bse & PCI_PMCSR_BSE_B2_B3) {
1066 		ppb->ppb_pwr_p->pwr_flags |= PCI_PWR_B2_CAPABLE;
1067 		DEBUG0(DBG_PWR, pdip, "B2 supported via D3\n");
1068 	} else {
1069 		ppb->ppb_pwr_p->pwr_flags |= PCI_PWR_B3_CAPABLE;
1070 		DEBUG0(DBG_PWR, pdip, "B3 supported via D3\n");
1071 	}
1072 
1073 	ppb->ppb_pwr_p->pwr_dip = pdip;
1074 	mutex_init(&ppb->ppb_pwr_p->pwr_mutex, NULL, MUTEX_DRIVER, NULL);
1075 
1076 	i = 0;
1077 	comp_array[i++] = "NAME=PCI bridge PM";
1078 	if (ppb->ppb_pwr_p->pwr_flags & PCI_PWR_B3_CAPABLE) {
1079 		comp_array[i++] = "0=Clock/Power Off (B3)";
1080 	}
1081 	if (ppb->ppb_pwr_p->pwr_flags & PCI_PWR_B2_CAPABLE) {
1082 		comp_array[i++] = "1=Clock Off (B2)";
1083 	}
1084 	if (ppb->ppb_pwr_p->pwr_flags & PCI_PWR_B1_CAPABLE) {
1085 		comp_array[i++] = "2=Bus Inactive (B1)";
1086 	}
1087 	comp_array[i++] = "3=Full Power (B0)";
1088 
1089 	/*
1090 	 * Create pm-components property. It does not already exist.
1091 	 */
1092 	if (ddi_prop_update_string_array(DDI_DEV_T_NONE, pdip,
1093 	    "pm-components", comp_array, i) != DDI_PROP_SUCCESS) {
1094 		cmn_err(CE_WARN,
1095 		    "%s%d pm-components prop update failed",
1096 		    ddi_driver_name(pdip), ddi_get_instance(pdip));
1097 		pci_config_teardown(&conf_hdl);
1098 		mutex_destroy(&ppb->ppb_pwr_p->pwr_mutex);
1099 		kmem_free(ppb->ppb_pwr_p, sizeof (pci_pwr_t));
1100 		ppb->ppb_pwr_p = NULL;
1101 
1102 		return;
1103 	}
1104 
1105 	if (ddi_prop_create(DDI_DEV_T_NONE, pdip, DDI_PROP_CANSLEEP,
1106 	    "pm-want-child-notification?", NULL, NULL) != DDI_PROP_SUCCESS) {
1107 		cmn_err(CE_WARN,
1108 			"%s%d fail to create pm-want-child-notification? prop",
1109 			ddi_driver_name(pdip), ddi_get_instance(pdip));
1110 
1111 		(void) ddi_prop_remove(DDI_DEV_T_NONE, pdip, "pm-components");
1112 		pci_config_teardown(&conf_hdl);
1113 		mutex_destroy(&ppb->ppb_pwr_p->pwr_mutex);
1114 		kmem_free(ppb->ppb_pwr_p, sizeof (pci_pwr_t));
1115 		ppb->ppb_pwr_p = NULL;
1116 
1117 		return;
1118 	}
1119 
1120 	ppb->ppb_pwr_p->current_lvl =
1121 		pci_pwr_current_lvl(ppb->ppb_pwr_p);
1122 }
1123 
1124 /*
1125  * Remove PM state for nexus.
1126  */
1127 static void
1128 ppb_pwr_teardown(ppb_devstate_t *ppb, dev_info_t *dip)
1129 {
1130 	int low_lvl;
1131 
1132 	/*
1133 	 * Determine the lowest power level supported.
1134 	 */
1135 	if (ppb->ppb_pwr_p->pwr_flags & PCI_PWR_B3_CAPABLE) {
1136 		low_lvl = PM_LEVEL_B3;
1137 	} else {
1138 		low_lvl = PM_LEVEL_B2;
1139 	}
1140 
1141 	if (pm_lower_power(dip, PCI_PM_COMP_0, low_lvl) != DDI_SUCCESS) {
1142 		cmn_err(CE_WARN, "%s%d failed to lower power",
1143 		    ddi_driver_name(dip), ddi_get_instance(dip));
1144 	}
1145 
1146 	pci_config_teardown(&ppb->ppb_conf_hdl);
1147 	mutex_destroy(&ppb->ppb_pwr_p->pwr_mutex);
1148 	kmem_free(ppb->ppb_pwr_p, sizeof (pci_pwr_t));
1149 
1150 	if (ddi_prop_remove(DDI_DEV_T_NONE, dip, "pm-components") !=
1151 		DDI_PROP_SUCCESS) {
1152 		cmn_err(CE_WARN, "%s%d unable to remove prop pm-components",
1153 		    ddi_driver_name(dip), ddi_get_instance(dip));
1154 	}
1155 
1156 	if (ddi_prop_remove(DDI_DEV_T_NONE, dip,
1157 	    "pm-want-child-notification?") != DDI_PROP_SUCCESS) {
1158 		cmn_err(CE_WARN,
1159 		    "%s%d unable to remove prop pm-want_child_notification?",
1160 		    ddi_driver_name(dip), ddi_get_instance(dip));
1161 	}
1162 }
1163 
1164 /*
1165  * Examine the pmcsr register and return the software defined
1166  * state (the difference being whether D3 means B2 or B3).
1167  */
1168 int
1169 pci_pwr_current_lvl(pci_pwr_t *pwr_p)
1170 {
1171 	ppb_devstate_t *ppb;
1172 	uint16_t pmcsr;
1173 
1174 	/*
1175 	 * Find out current power level
1176 	 */
1177 	ppb = (ppb_devstate_t *)ddi_get_soft_state(ppb_state,
1178 	    ddi_get_instance(pwr_p->pwr_dip));
1179 
1180 	if ((pmcsr = PCI_CAP_GET16(ppb->ppb_conf_hdl, NULL,
1181 		ppb->ppb_pm_cap_ptr, PCI_PMCSR)) == PCI_CAP_EINVAL16)
1182 		return (DDI_FAILURE);
1183 
1184 	switch (pmcsr & PCI_PMCSR_STATE_MASK) {
1185 	case PCI_PMCSR_D0:
1186 
1187 		return (PM_LEVEL_B0);
1188 	case PCI_PMCSR_D1:
1189 
1190 		return (PM_LEVEL_B1);
1191 	case PCI_PMCSR_D2:
1192 
1193 		return (PM_LEVEL_B2);
1194 	case PCI_PMCSR_D3HOT:
1195 		if ((ppb->ppb_pwr_p->pwr_flags & PCI_PWR_B3_CAPABLE) == 0) {
1196 
1197 			return (PM_LEVEL_B2);
1198 		} else {
1199 
1200 			return (PM_LEVEL_B3);
1201 		}
1202 	}
1203 	/*NOTREACHED*/
1204 	return (PM_LEVEL_B3);
1205 }
1206 
1207 /*
1208  * Power entry point.  Called by the PM framework to change the
1209  * current power state of the bus.  This function must first verify that
1210  * the requested power change is still valid.
1211  */
1212 /*ARGSUSED*/
1213 static int
1214 ppb_pwr(dev_info_t *dip, int component, int lvl)
1215 {
1216 	ppb_devstate_t *ppb;
1217 	uint16_t pmcsr;
1218 	char *str;
1219 	int lowest_lvl;
1220 	int old_lvl;
1221 	int new_lvl;
1222 
1223 	ppb = (ppb_devstate_t *)ddi_get_soft_state(ppb_state,
1224 	    ddi_get_instance(dip));
1225 	if (ppb == NULL) {
1226 		cmn_err(CE_WARN, "%s%d ppb_pwr: can't get soft state",
1227 		    ddi_driver_name(dip), ddi_get_instance(dip));
1228 
1229 		return (DDI_FAILURE);
1230 	}
1231 
1232 	DEBUG1(DBG_PWR, dip, "ppb_pwr(): ENTER level = %d\n", lvl);
1233 
1234 	mutex_enter(&ppb->ppb_pwr_p->pwr_mutex);
1235 
1236 	/*
1237 	 * Find out if the power setting is possible.  If it is not,
1238 	 * set component busy and return failure.  If it is possible,
1239 	 * and it is the lowest pwr setting possible, set component
1240 	 * busy so that the framework does not try to lower any further.
1241 	 */
1242 	lowest_lvl = pci_pwr_new_lvl(ppb->ppb_pwr_p);
1243 	if (lowest_lvl > lvl) {
1244 		pci_pwr_component_busy(ppb->ppb_pwr_p);
1245 		DEBUG2(DBG_PWR, dip, "ppb_pwr: failing power request "
1246 			"lowest allowed is %d requested is %d\n",
1247 				lowest_lvl, lvl);
1248 		mutex_exit(&ppb->ppb_pwr_p->pwr_mutex);
1249 
1250 		return (DDI_FAILURE);
1251 	} else if (lowest_lvl == lvl) {
1252 		pci_pwr_component_busy(ppb->ppb_pwr_p);
1253 	} else {
1254 		pci_pwr_component_idle(ppb->ppb_pwr_p);
1255 	}
1256 
1257 	if ((pmcsr = PCI_CAP_GET16(ppb->ppb_conf_hdl, NULL,
1258 		ppb->ppb_pm_cap_ptr, PCI_PMCSR)) == PCI_CAP_EINVAL16)
1259 		return (DDI_FAILURE);
1260 
1261 	/*
1262 	 * Save the current power level.  This is the actual function level,
1263 	 * not the translated bridge level stored in pwr_p->current_lvl
1264 	 */
1265 	old_lvl = pmcsr & PCI_PMCSR_STATE_MASK;
1266 
1267 	pmcsr &= ~PCI_PMCSR_STATE_MASK;
1268 	switch (lvl) {
1269 	case PM_LEVEL_B0:
1270 		str = "PM_LEVEL_B0 (full speed)";
1271 		pmcsr |= PCI_PMCSR_D0;
1272 		break;
1273 	case PM_LEVEL_B1:
1274 		str = "PM_LEVEL_B1 (light sleep. No bus traffic allowed)";
1275 		if ((ppb->ppb_pwr_p->pwr_flags & PCI_PWR_B1_CAPABLE) == 0) {
1276 			cmn_err(CE_WARN, "%s%d PCI PM state B1 not supported",
1277 			    ddi_driver_name(dip), ddi_get_instance(dip));
1278 
1279 			mutex_exit(&ppb->ppb_pwr_p->pwr_mutex);
1280 			return (DDI_FAILURE);
1281 		}
1282 		pmcsr |= PCI_PMCSR_D1;
1283 		break;
1284 	case PM_LEVEL_B2:
1285 		str = "PM_LEVEL_B2 (clock off)";
1286 		if ((ppb->ppb_pwr_p->pwr_flags & PCI_PWR_B2_CAPABLE) == 0) {
1287 			cmn_err(CE_WARN, "%s%d PM state B2 not supported...",
1288 			    ddi_driver_name(dip),
1289 			    ddi_get_instance(dip));
1290 			mutex_exit(&ppb->ppb_pwr_p->pwr_mutex);
1291 
1292 			return (DDI_FAILURE);
1293 		}
1294 
1295 		if ((ppb->ppb_pwr_p->pwr_flags & PCI_PWR_B3_CAPABLE) == 0) {
1296 			/*
1297 			 * If B3 isn't supported, use D3 for B2 to avoid the
1298 			 * possible case that D2 for B2 isn't supported.
1299 			 * Saves and extra check and state flag..
1300 			 */
1301 			pmcsr |= PCI_PMCSR_D3HOT;
1302 		} else {
1303 			pmcsr |= PCI_PMCSR_D2;
1304 		}
1305 		break;
1306 	case PM_LEVEL_B3:
1307 		str = "PM_LEVEL_B30 (clock and power off)";
1308 		if ((ppb->ppb_pwr_p->pwr_flags & PCI_PWR_B3_CAPABLE) == 0) {
1309 			cmn_err(CE_WARN, "%s%d PM state B3 not supported...",
1310 			    ddi_driver_name(dip),
1311 			    ddi_get_instance(dip));
1312 			mutex_exit(&ppb->ppb_pwr_p->pwr_mutex);
1313 
1314 			return (DDI_FAILURE);
1315 		}
1316 		pmcsr |= PCI_PMCSR_D3HOT;
1317 
1318 		break;
1319 
1320 	default:
1321 		cmn_err(CE_WARN, "%s%d Unknown PM state %d",
1322 		    ddi_driver_name(dip), ddi_get_instance(dip), lvl);
1323 		mutex_exit(&ppb->ppb_pwr_p->pwr_mutex);
1324 
1325 		return (DDI_FAILURE);
1326 	}
1327 
1328 	new_lvl = pmcsr & PCI_PMCSR_STATE_MASK;
1329 
1330 	/*
1331 	 * Save config regs if going into HW state D3 (B2 or B3)
1332 	 */
1333 	if ((old_lvl != PCI_PMCSR_D3HOT) && (new_lvl == PCI_PMCSR_D3HOT)) {
1334 		DEBUG0(DBG_PWR, dip, "ppb_pwr(): SAVING CONFIG REGS\n");
1335 		if (pci_save_config_regs(dip) != DDI_SUCCESS) {
1336 			cmn_err(CE_WARN, "%s%d Save config regs failed",
1337 				ddi_driver_name(dip), ddi_get_instance(dip));
1338 			mutex_exit(&ppb->ppb_pwr_p->pwr_mutex);
1339 
1340 			return (DDI_FAILURE);
1341 		}
1342 	}
1343 
1344 	PCI_CAP_PUT16(ppb->ppb_conf_hdl, NULL, ppb->ppb_pm_cap_ptr, PCI_PMCSR,
1345 		pmcsr);
1346 
1347 	/*
1348 	 * No bus transactions should occur without waiting for
1349 	 * settle time specified in PCI PM spec rev 2.1 sec 5.6.1
1350 	 * To make things simple, just use the max time specified for
1351 	 * all state transitions.
1352 	 */
1353 	delay(drv_usectohz(PCI_CLK_SETTLE_TIME));
1354 
1355 	/*
1356 	 * Restore configuration registers if coming out of HW state D3
1357 	 */
1358 	if ((old_lvl == PCI_PMCSR_D3HOT) && (new_lvl != PCI_PMCSR_D3HOT)) {
1359 		DEBUG0(DBG_PWR, dip, "ppb_pwr(): RESTORING CONFIG REGS\n");
1360 		if (pci_restore_config_regs(dip) != DDI_SUCCESS) {
1361 			panic("%s%d restore config regs failed",
1362 			    ddi_driver_name(dip), ddi_get_instance(dip));
1363 		}
1364 		/*NOTREACHED*/
1365 	}
1366 
1367 	ppb->ppb_pwr_p->current_lvl = lvl;
1368 
1369 	mutex_exit(&ppb->ppb_pwr_p->pwr_mutex);
1370 
1371 	DEBUG1(DBG_PWR, dip, "ppb_set_pwr: set PM state to %s\n\n", str);
1372 
1373 	return (DDI_SUCCESS);
1374 }
1375 
1376 /*
1377  * Initialize hotplug framework if we are hotpluggable.
1378  * Sets flag in the soft state if Hot Plug is supported and initialized
1379  * properly.
1380  */
1381 /*ARGSUSED*/
1382 static void
1383 ppb_init_hotplug(ppb_devstate_t *ppb)
1384 {
1385 	if (ddi_prop_exists(DDI_DEV_T_ANY, ppb->dip, DDI_PROP_DONTPASS,
1386 	    "hotplug-capable")) {
1387 		(void) modload("misc", "pcihp");
1388 
1389 		if (pcihp_init(ppb->dip) != DDI_SUCCESS) {
1390 			cmn_err(CE_WARN,
1391 			    "%s #%d: Failed setting hotplug framework",
1392 			    ddi_driver_name(ppb->dip),
1393 			    ddi_get_instance(ppb->dip));
1394 		} else
1395 			ppb->hotplug_capable = B_TRUE;
1396 	}
1397 
1398 }
1399 
1400 static void
1401 ppb_create_ranges_prop(dev_info_t *dip,
1402 	ddi_acc_handle_t config_handle)
1403 {
1404 	uint32_t base, limit;
1405 	ppb_ranges_t	ranges[PPB_RANGE_LEN];
1406 	uint8_t io_base_lo, io_limit_lo;
1407 	uint16_t io_base_hi, io_limit_hi, mem_base, mem_limit;
1408 	int i = 0, rangelen = sizeof (ppb_ranges_t)/sizeof (int);
1409 
1410 	io_base_lo = pci_config_get8(config_handle, PCI_BCNF_IO_BASE_LOW);
1411 	io_limit_lo = pci_config_get8(config_handle, PCI_BCNF_IO_LIMIT_LOW);
1412 	io_base_hi = pci_config_get16(config_handle, PCI_BCNF_IO_BASE_HI);
1413 	io_limit_hi = pci_config_get16(config_handle, PCI_BCNF_IO_LIMIT_HI);
1414 	mem_base = pci_config_get16(config_handle, PCI_BCNF_MEM_BASE);
1415 	mem_limit = pci_config_get16(config_handle, PCI_BCNF_MEM_LIMIT);
1416 
1417 	/*
1418 	 * Create ranges for IO space
1419 	 */
1420 	ranges[i].size_low = ranges[i].size_high = 0;
1421 	ranges[i].parent_mid = ranges[i].child_mid =
1422 		ranges[i].parent_high = 0;
1423 	ranges[i].child_high = ranges[i].parent_high |=
1424 		(PCI_REG_REL_M | PCI_ADDR_IO);
1425 	base = PPB_16bit_IOADDR(io_base_lo);
1426 	limit = PPB_16bit_IOADDR(io_limit_lo);
1427 
1428 	if ((io_base_lo & 0xf) == PPB_32BIT_IO) {
1429 		base = PPB_LADDR(base, io_base_hi);
1430 	}
1431 	if ((io_limit_lo & 0xf) == PPB_32BIT_IO) {
1432 		limit = PPB_LADDR(limit, io_limit_hi);
1433 	}
1434 
1435 	if ((io_base_lo & PPB_32BIT_IO) && (io_limit_hi > 0)) {
1436 		base = PPB_LADDR(base, io_base_hi);
1437 		limit = PPB_LADDR(limit, io_limit_hi);
1438 	}
1439 
1440 	/*
1441 	 * Create ranges for 32bit memory space
1442 	 */
1443 	base = PPB_32bit_MEMADDR(mem_base);
1444 	limit = PPB_32bit_MEMADDR(mem_limit);
1445 	ranges[i].size_low = ranges[i].size_high = 0;
1446 	ranges[i].parent_mid = ranges[i].child_mid =
1447 		ranges[i].parent_high = 0;
1448 	ranges[i].child_high = ranges[i].parent_high |=
1449 		(PCI_REG_REL_M | PCI_ADDR_MEM32);
1450 	ranges[i].child_low = ranges[i].parent_low = base;
1451 	if (limit >= base) {
1452 		ranges[i].size_low = limit - base + PPB_MEMGRAIN;
1453 		i++;
1454 	}
1455 
1456 	if (i) {
1457 		(void) ndi_prop_update_int_array(DDI_DEV_T_NONE, dip, "ranges",
1458 		    (int *)ranges, i * rangelen);
1459 	}
1460 }
1461 
1462 /* ARGSUSED */
1463 static int
1464 ppb_open(dev_t *devp, int flags, int otyp, cred_t *credp)
1465 {
1466 	ppb_devstate_t *ppb_p;
1467 	minor_t		minor = getminor(*devp);
1468 	int		instance = PCIHP_AP_MINOR_NUM_TO_INSTANCE(minor);
1469 
1470 	/*
1471 	 * Make sure the open is for the right file type.
1472 	 */
1473 	if (otyp != OTYP_CHR)
1474 		return (EINVAL);
1475 
1476 	/*
1477 	 * Get the soft state structure for the device.
1478 	 */
1479 	ppb_p = (ppb_devstate_t *)ddi_get_soft_state(ppb_state,
1480 	    instance);
1481 
1482 	if (ppb_p == NULL)
1483 		return (ENXIO);
1484 
1485 	if (ppb_p->hotplug_capable == B_TRUE)
1486 		return ((pcihp_get_cb_ops())->cb_open(devp, flags,
1487 		    otyp, credp));
1488 
1489 	/*
1490 	 * Handle the open by tracking the device state.
1491 	 */
1492 	mutex_enter(&ppb_p->ppb_mutex);
1493 	if (flags & FEXCL) {
1494 		if (ppb_p->ppb_soft_state != PPB_SOFT_STATE_CLOSED) {
1495 			mutex_exit(&ppb_p->ppb_mutex);
1496 			return (EBUSY);
1497 		}
1498 		ppb_p->ppb_soft_state = PPB_SOFT_STATE_OPEN_EXCL;
1499 	} else {
1500 		if (ppb_p->ppb_soft_state == PPB_SOFT_STATE_OPEN_EXCL) {
1501 			mutex_exit(&ppb_p->ppb_mutex);
1502 			return (EBUSY);
1503 		}
1504 		ppb_p->ppb_soft_state = PPB_SOFT_STATE_OPEN;
1505 	}
1506 	mutex_exit(&ppb_p->ppb_mutex);
1507 	return (0);
1508 }
1509 
1510 
1511 /* ARGSUSED */
1512 static int
1513 ppb_close(dev_t dev, int flags, int otyp, cred_t *credp)
1514 {
1515 	ppb_devstate_t *ppb_p;
1516 	minor_t		minor = getminor(dev);
1517 	int		instance = PCIHP_AP_MINOR_NUM_TO_INSTANCE(minor);
1518 
1519 	if (otyp != OTYP_CHR)
1520 		return (EINVAL);
1521 
1522 	ppb_p = (ppb_devstate_t *)ddi_get_soft_state(ppb_state,
1523 	    instance);
1524 
1525 	if (ppb_p == NULL)
1526 		return (ENXIO);
1527 
1528 	if (ppb_p->hotplug_capable == B_TRUE)
1529 		return ((pcihp_get_cb_ops())->cb_close(dev, flags,
1530 		    otyp, credp));
1531 
1532 	mutex_enter(&ppb_p->ppb_mutex);
1533 	ppb_p->ppb_soft_state = PPB_SOFT_STATE_CLOSED;
1534 	mutex_exit(&ppb_p->ppb_mutex);
1535 	return (0);
1536 }
1537 
1538 
1539 /*
1540  * ppb_ioctl: devctl hotplug controls
1541  */
1542 /* ARGSUSED */
1543 static int
1544 ppb_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp,
1545 	int *rvalp)
1546 {
1547 	ppb_devstate_t *ppb_p;
1548 	dev_info_t *self;
1549 	struct devctl_iocdata *dcp;
1550 	uint_t bus_state;
1551 	int rv = 0;
1552 	minor_t		minor = getminor(dev);
1553 	int		instance = PCIHP_AP_MINOR_NUM_TO_INSTANCE(minor);
1554 
1555 	ppb_p = (ppb_devstate_t *)ddi_get_soft_state(ppb_state,
1556 	    instance);
1557 
1558 	if (ppb_p == NULL)
1559 		return (ENXIO);
1560 
1561 	if (ppb_p->hotplug_capable == B_TRUE)
1562 		return ((pcihp_get_cb_ops())->cb_ioctl(dev, cmd,
1563 		    arg, mode, credp, rvalp));
1564 
1565 	self = ppb_p->dip;
1566 
1567 	/*
1568 	 * We can use the generic implementation for these ioctls
1569 	 */
1570 	switch (cmd) {
1571 	case DEVCTL_DEVICE_GETSTATE:
1572 	case DEVCTL_DEVICE_ONLINE:
1573 	case DEVCTL_DEVICE_OFFLINE:
1574 	case DEVCTL_BUS_GETSTATE:
1575 		return (ndi_devctl_ioctl(self, cmd, arg, mode, 0));
1576 	}
1577 
1578 	/*
1579 	 * read devctl ioctl data
1580 	 */
1581 	if (ndi_dc_allochdl((void *)arg, &dcp) != NDI_SUCCESS)
1582 		return (EFAULT);
1583 
1584 	switch (cmd) {
1585 
1586 	case DEVCTL_DEVICE_RESET:
1587 		rv = ENOTSUP;
1588 		break;
1589 
1590 	case DEVCTL_BUS_QUIESCE:
1591 		if (ndi_get_bus_state(self, &bus_state) == NDI_SUCCESS)
1592 			if (bus_state == BUS_QUIESCED)
1593 				break;
1594 		(void) ndi_set_bus_state(self, BUS_QUIESCED);
1595 		break;
1596 
1597 	case DEVCTL_BUS_UNQUIESCE:
1598 		if (ndi_get_bus_state(self, &bus_state) == NDI_SUCCESS)
1599 			if (bus_state == BUS_ACTIVE)
1600 				break;
1601 		(void) ndi_set_bus_state(self, BUS_ACTIVE);
1602 		break;
1603 
1604 	case DEVCTL_BUS_RESET:
1605 		rv = ENOTSUP;
1606 		break;
1607 
1608 	case DEVCTL_BUS_RESETALL:
1609 		rv = ENOTSUP;
1610 		break;
1611 
1612 	default:
1613 		rv = ENOTTY;
1614 	}
1615 
1616 	ndi_dc_freehdl(dcp);
1617 	return (rv);
1618 }
1619 
1620 static int ppb_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op,
1621     int flags, char *name, caddr_t valuep, int *lengthp)
1622 {
1623 	ppb_devstate_t *ppb_p;
1624 	minor_t		minor = getminor(dev);
1625 	int		instance = PCIHP_AP_MINOR_NUM_TO_INSTANCE(minor);
1626 
1627 	ppb_p = (ppb_devstate_t *)ddi_get_soft_state(ppb_state,
1628 	    instance);
1629 
1630 	if (ppb_p == NULL)
1631 		return (ENXIO);
1632 
1633 	if (ppb_p->hotplug_capable == B_TRUE)
1634 		return ((pcihp_get_cb_ops())->cb_prop_op(dev, dip, prop_op,
1635 		    flags, name, valuep, lengthp));
1636 
1637 	return (ddi_prop_op(dev, dip, prop_op, flags, name, valuep, lengthp));
1638 }
1639 
1640 /*
1641  * Initialize our FMA resources
1642  */
1643 static void
1644 ppb_fm_init(ppb_devstate_t *ppb_p)
1645 {
1646 	ddi_fm_error_t derr;
1647 
1648 	ppb_p->fm_cap = DDI_FM_EREPORT_CAPABLE | DDI_FM_ERRCB_CAPABLE |
1649 		DDI_FM_ACCCHK_CAPABLE | DDI_FM_DMACHK_CAPABLE;
1650 
1651 	/*
1652 	 * Request our capability level and get our parents capability
1653 	 * and ibc.
1654 	 */
1655 	ddi_fm_init(ppb_p->dip, &ppb_p->fm_cap, &ppb_p->fm_ibc);
1656 	ASSERT((ppb_p->fm_cap & DDI_FM_EREPORT_CAPABLE) &&
1657 	    (ppb_p->fm_cap & DDI_FM_ERRCB_CAPABLE));
1658 
1659 	pci_ereport_setup(ppb_p->dip);
1660 
1661 	/*
1662 	 * clear any outstanding error bits
1663 	 */
1664 	bzero(&derr, sizeof (ddi_fm_error_t));
1665 	derr.fme_version = DDI_FME_VERSION;
1666 	derr.fme_flag = DDI_FM_ERR_EXPECTED;
1667 	pci_ereport_post(ppb_p->dip, &derr, NULL);
1668 	pci_bdg_ereport_post(ppb_p->dip, &derr, NULL);
1669 
1670 	/*
1671 	 * Register error callback with our parent.
1672 	 */
1673 	ddi_fm_handler_register(ppb_p->dip, ppb_err_callback, NULL);
1674 }
1675 
1676 /*
1677  * Breakdown our FMA resources
1678  */
1679 static void
1680 ppb_fm_fini(ppb_devstate_t *ppb_p)
1681 {
1682 	/*
1683 	 * Clean up allocated fm structures
1684 	 */
1685 	ddi_fm_handler_unregister(ppb_p->dip);
1686 	pci_ereport_teardown(ppb_p->dip);
1687 	ddi_fm_fini(ppb_p->dip);
1688 }
1689 
1690 /*
1691  * Initialize FMA resources for children devices. Called when
1692  * child calls ddi_fm_init().
1693  */
1694 /*ARGSUSED*/
1695 static int
1696 ppb_fm_init_child(dev_info_t *dip, dev_info_t *tdip, int cap,
1697 		ddi_iblock_cookie_t *ibc)
1698 {
1699 	ppb_devstate_t *ppb_p = (ppb_devstate_t *)ddi_get_soft_state(ppb_state,
1700 			ddi_get_instance(dip));
1701 	*ibc = ppb_p->fm_ibc;
1702 	return (ppb_p->fm_cap);
1703 }
1704 
1705 /*
1706  * FMA registered error callback
1707  */
1708 static int
1709 ppb_err_callback(dev_info_t *dip, ddi_fm_error_t *derr, const void *impl_data)
1710 {
1711 	uint16_t pci_cfg_stat, pci_cfg_sec_stat;
1712 
1713 	ASSERT(impl_data == NULL);
1714 	pci_ereport_post(dip, derr, &pci_cfg_stat);
1715 	pci_bdg_ereport_post(dip, derr, &pci_cfg_sec_stat);
1716 	return (pci_bdg_check_status(dip, derr, pci_cfg_stat,
1717 	    pci_cfg_sec_stat));
1718 }
1719 
1720 static void
1721 ppb_bus_enter(dev_info_t *dip, ddi_acc_handle_t handle)
1722 {
1723 	i_ndi_busop_access_enter(dip, handle);
1724 }
1725 
1726 /* ARGSUSED */
1727 static void
1728 ppb_bus_exit(dev_info_t *dip, ddi_acc_handle_t handle)
1729 {
1730 	i_ndi_busop_access_exit(dip, handle);
1731 }
1732