xref: /illumos-gate/usr/src/uts/intel/io/pci/pci_pci.c (revision bea83d026ee1bd1b2a2419e1d0232f107a5d7d9b)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * PCI 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/pci.h>
39 #include <sys/ddi.h>
40 #include <sys/sunddi.h>
41 #include <sys/sunndi.h>
42 #include <sys/ddifm.h>
43 #include <sys/ndifm.h>
44 #include <sys/fm/protocol.h>
45 #include <sys/hotplug/pci/pcihp.h>
46 #include <sys/pci_intr_lib.h>
47 #include <sys/psm.h>
48 
49 /*
50  * The variable controls the default setting of the command register
51  * for pci devices.  See ppb_initchild() for details.
52  */
53 static ushort_t ppb_command_default = PCI_COMM_ME | PCI_COMM_MAE | PCI_COMM_IO;
54 
55 
56 static int	ppb_bus_map(dev_info_t *, dev_info_t *, ddi_map_req_t *,
57 		    off_t, off_t, caddr_t *);
58 static int	ppb_ctlops(dev_info_t *, dev_info_t *, ddi_ctl_enum_t,
59 		    void *, void *);
60 static int	ppb_fm_init(dev_info_t *, dev_info_t *, int,
61 		    ddi_iblock_cookie_t *);
62 static int	ppb_fm_callback(dev_info_t *, ddi_fm_error_t *, const void *);
63 static int	ppb_intr_ops(dev_info_t *, dev_info_t *, ddi_intr_op_t,
64 		    ddi_intr_handle_impl_t *, void *);
65 
66 /*
67  * ppb_support_msi: Flag that controls MSI support across P2P Bridges.
68  * By default, MSI is not supported except for special cases like HT
69  * bridges/tunnels that have HT MSI mapping enabled.
70  *
71  * However, MSI support behavior can be patched on a system by changing
72  * the value of this flag as shown below:-
73  *	 0 = default value, MSI is allowed by this driver for special cases
74  *	 1 = MSI supported without any checks for this driver
75  *	-1 = MSI not supported at all
76  */
77 int ppb_support_msi = 0;
78 
79 /*
80  * Controls the usage of the Hypertransport MSI mapping capability
81  *	0 = default value, leave hardware function as it is
82  *	1 = always enable HT MSI mapping
83  *     -1 = always disable HT MSI mapping
84  */
85 int ppb_support_ht_msimap = 0;
86 
87 /*
88  * masks and values for the upper 16-bits of hypertransport cap headers
89  */
90 #define	PCI_CAP_HT_MSIMAP_TYPE			0xA800
91 #define	PCI_CAP_HT_MSIMAP_TYPE_MASK		0xFF00
92 #define	PCI_CAP_HT_MSIMAP_ENABLE		0x0001
93 #define	PCI_CAP_HT_MSIMAP_ENABLE_MASK		0x0001
94 
95 
96 struct bus_ops ppb_bus_ops = {
97 	BUSO_REV,
98 	ppb_bus_map,
99 	0,
100 	0,
101 	0,
102 	i_ddi_map_fault,
103 	ddi_dma_map,
104 	ddi_dma_allochdl,
105 	ddi_dma_freehdl,
106 	ddi_dma_bindhdl,
107 	ddi_dma_unbindhdl,
108 	ddi_dma_flush,
109 	ddi_dma_win,
110 	ddi_dma_mctl,
111 	ppb_ctlops,
112 	ddi_bus_prop_op,
113 	0,		/* (*bus_get_eventcookie)();	*/
114 	0,		/* (*bus_add_eventcall)();	*/
115 	0,		/* (*bus_remove_eventcall)();	*/
116 	0,		/* (*bus_post_event)();		*/
117 	0,		/* (*bus_intr_ctl)();		*/
118 	0,		/* (*bus_config)(); 		*/
119 	0,		/* (*bus_unconfig)(); 		*/
120 	ppb_fm_init,	/* (*bus_fm_init)(); 		*/
121 	NULL,		/* (*bus_fm_fini)(); 		*/
122 	NULL,		/* (*bus_fm_access_enter)(); 	*/
123 	NULL,		/* (*bus_fm_access_exit)(); 	*/
124 	NULL,		/* (*bus_power)(); 	*/
125 	ppb_intr_ops	/* (*bus_intr_op)(); 		*/
126 };
127 
128 /*
129  * The goal here is to leverage off of the pcihp.c source without making
130  * changes to it.  Call into it's cb_ops directly if needed.
131  */
132 static int	ppb_open(dev_t *, int, int, cred_t *);
133 static int	ppb_close(dev_t, int, int, cred_t *);
134 static int	ppb_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
135 static int	ppb_prop_op(dev_t, dev_info_t *, ddi_prop_op_t, int, char *,
136 		    caddr_t, int *);
137 static int	ppb_info(dev_info_t *, ddi_info_cmd_t, void *, void **);
138 
139 struct cb_ops ppb_cb_ops = {
140 	ppb_open,			/* open */
141 	ppb_close,			/* close */
142 	nodev,				/* strategy */
143 	nodev,				/* print */
144 	nodev,				/* dump */
145 	nodev,				/* read */
146 	nodev,				/* write */
147 	ppb_ioctl,			/* ioctl */
148 	nodev,				/* devmap */
149 	nodev,				/* mmap */
150 	nodev,				/* segmap */
151 	nochpoll,			/* poll */
152 	ppb_prop_op,			/* cb_prop_op */
153 	NULL,				/* streamtab */
154 	D_NEW | D_MP | D_HOTPLUG,	/* Driver compatibility flag */
155 	CB_REV,				/* rev */
156 	nodev,				/* int (*cb_aread)() */
157 	nodev				/* int (*cb_awrite)() */
158 };
159 
160 
161 static int ppb_probe(dev_info_t *);
162 static int ppb_attach(dev_info_t *devi, ddi_attach_cmd_t cmd);
163 static int ppb_detach(dev_info_t *devi, ddi_detach_cmd_t cmd);
164 
165 struct dev_ops ppb_ops = {
166 	DEVO_REV,		/* devo_rev */
167 	0,			/* refcnt  */
168 	ppb_info,		/* info */
169 	nulldev,		/* identify */
170 	ppb_probe,		/* probe */
171 	ppb_attach,		/* attach */
172 	ppb_detach,		/* detach */
173 	nulldev,		/* reset */
174 	&ppb_cb_ops,		/* driver operations */
175 	&ppb_bus_ops		/* bus operations */
176 };
177 
178 /*
179  * Module linkage information for the kernel.
180  */
181 
182 static struct modldrv modldrv = {
183 	&mod_driverops, /* Type of module */
184 	"PCI to PCI bridge nexus driver %I%",
185 	&ppb_ops,	/* driver ops */
186 };
187 
188 static struct modlinkage modlinkage = {
189 	MODREV_1,
190 	(void *)&modldrv,
191 	NULL
192 };
193 
194 /*
195  * soft state pointer and structure template:
196  */
197 static void *ppb_state;
198 
199 typedef struct {
200 	dev_info_t *dip;
201 	int ppb_fmcap;
202 	ddi_iblock_cookie_t ppb_fm_ibc;
203 	kmutex_t ppb_peek_poke_mutex;
204 	kmutex_t ppb_err_mutex;
205 
206 	/*
207 	 * cpr support:
208 	 */
209 	uint_t config_state_index;
210 	struct {
211 		dev_info_t *dip;
212 		ushort_t command;
213 		uchar_t cache_line_size;
214 		uchar_t latency_timer;
215 		uchar_t header_type;
216 		uchar_t sec_latency_timer;
217 		ushort_t bridge_control;
218 	} config_state[PCI_MAX_CHILDREN];
219 } ppb_devstate_t;
220 
221 
222 /*
223  * forward function declarations:
224  */
225 static void	ppb_removechild(dev_info_t *);
226 static int	ppb_initchild(dev_info_t *child);
227 static void	ppb_save_config_regs(ppb_devstate_t *ppb_p);
228 static void	ppb_restore_config_regs(ppb_devstate_t *ppb_p);
229 static uint8_t	ppb_find_ht_cap(ddi_acc_handle_t cfg_hdl, uint16_t reg_mask,
230 		    uint16_t reg_val);
231 static boolean_t	ppb_ht_msimap_check(ddi_acc_handle_t cfg_hdl);
232 static int	ppb_ht_msimap_set(ddi_acc_handle_t cfg_hdl, int cmd);
233 
234 /*
235  * for <cmd> in ppb_ht_msimap_set
236  */
237 #define	HT_MSIMAP_ENABLE	1
238 #define	HT_MSIMAP_DISABLE	0
239 
240 
241 int
242 _init(void)
243 {
244 	int e;
245 
246 	if ((e = ddi_soft_state_init(&ppb_state, sizeof (ppb_devstate_t),
247 	    1)) == 0 && (e = mod_install(&modlinkage)) != 0)
248 		ddi_soft_state_fini(&ppb_state);
249 	return (e);
250 }
251 
252 int
253 _fini(void)
254 {
255 	int e;
256 
257 	if ((e = mod_remove(&modlinkage)) == 0)
258 		ddi_soft_state_fini(&ppb_state);
259 	return (e);
260 }
261 
262 int
263 _info(struct modinfo *modinfop)
264 {
265 	return (mod_info(&modlinkage, modinfop));
266 }
267 
268 /*ARGSUSED*/
269 static int
270 ppb_probe(dev_info_t *devi)
271 {
272 	return (DDI_PROBE_SUCCESS);
273 }
274 
275 /*ARGSUSED*/
276 static int
277 ppb_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
278 {
279 	int instance;
280 	ppb_devstate_t *ppb;
281 	ddi_acc_handle_t config_handle;
282 
283 	switch (cmd) {
284 	case DDI_ATTACH:
285 
286 		/*
287 		 * Make sure the "device_type" property exists.
288 		 */
289 		(void) ddi_prop_update_string(DDI_DEV_T_NONE, devi,
290 		    "device_type", "pci");
291 
292 		/*
293 		 * Allocate and get soft state structure.
294 		 */
295 		instance = ddi_get_instance(devi);
296 		if (ddi_soft_state_zalloc(ppb_state, instance) != DDI_SUCCESS)
297 			return (DDI_FAILURE);
298 		ppb = ddi_get_soft_state(ppb_state, instance);
299 		ppb->dip = devi;
300 
301 		/*
302 		 * don't enable ereports if immediate child of npe
303 		 */
304 		if (strcmp(ddi_driver_name(ddi_get_parent(devi)), "npe") == 0)
305 			ppb->ppb_fmcap = DDI_FM_ERRCB_CAPABLE |
306 			    DDI_FM_ACCCHK_CAPABLE | DDI_FM_DMACHK_CAPABLE;
307 		else
308 			ppb->ppb_fmcap = DDI_FM_EREPORT_CAPABLE |
309 			    DDI_FM_ERRCB_CAPABLE | DDI_FM_ACCCHK_CAPABLE |
310 			    DDI_FM_DMACHK_CAPABLE;
311 
312 		ddi_fm_init(devi, &ppb->ppb_fmcap, &ppb->ppb_fm_ibc);
313 		mutex_init(&ppb->ppb_err_mutex, NULL, MUTEX_DRIVER,
314 		    (void *)ppb->ppb_fm_ibc);
315 		mutex_init(&ppb->ppb_peek_poke_mutex, NULL, MUTEX_DRIVER,
316 		    (void *)ppb->ppb_fm_ibc);
317 
318 		if (ppb->ppb_fmcap & (DDI_FM_ERRCB_CAPABLE |
319 		    DDI_FM_EREPORT_CAPABLE))
320 			pci_ereport_setup(devi);
321 		if (ppb->ppb_fmcap & DDI_FM_ERRCB_CAPABLE)
322 			ddi_fm_handler_register(devi, ppb_fm_callback, NULL);
323 
324 		if (pci_config_setup(devi, &config_handle) != DDI_SUCCESS) {
325 			if (ppb->ppb_fmcap & DDI_FM_ERRCB_CAPABLE)
326 				ddi_fm_handler_unregister(devi);
327 			if (ppb->ppb_fmcap & (DDI_FM_ERRCB_CAPABLE |
328 			    DDI_FM_EREPORT_CAPABLE))
329 				pci_ereport_teardown(devi);
330 			ddi_fm_fini(devi);
331 			ddi_soft_state_free(ppb_state, instance);
332 			return (DDI_FAILURE);
333 		}
334 
335 		if (ppb_support_ht_msimap == 1)
336 			(void) ppb_ht_msimap_set(config_handle,
337 			    HT_MSIMAP_ENABLE);
338 		else if (ppb_support_ht_msimap == -1)
339 			(void) ppb_ht_msimap_set(config_handle,
340 			    HT_MSIMAP_DISABLE);
341 
342 		pci_config_teardown(&config_handle);
343 
344 		/*
345 		 * Initialize hotplug support on this bus. At minimum
346 		 * (for non hotplug bus) this would create ":devctl" minor
347 		 * node to support DEVCTL_DEVICE_* and DEVCTL_BUS_* ioctls
348 		 * to this bus.
349 		 */
350 		if (pcihp_init(devi) != DDI_SUCCESS)
351 			cmn_err(CE_WARN,
352 			    "pci: Failed to setup hotplug framework");
353 
354 		ddi_report_dev(devi);
355 		return (DDI_SUCCESS);
356 
357 	case DDI_RESUME:
358 
359 		/*
360 		 * Get the soft state structure for the bridge.
361 		 */
362 		ppb = ddi_get_soft_state(ppb_state, ddi_get_instance(devi));
363 		ppb_restore_config_regs(ppb);
364 		return (DDI_SUCCESS);
365 
366 	default:
367 		break;
368 	}
369 	return (DDI_FAILURE);
370 }
371 
372 /*ARGSUSED*/
373 static int
374 ppb_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
375 {
376 	ppb_devstate_t *ppb;
377 
378 	switch (cmd) {
379 	case DDI_DETACH:
380 		(void) ddi_prop_remove(DDI_DEV_T_NONE, devi, "device_type");
381 
382 		ppb = ddi_get_soft_state(ppb_state, ddi_get_instance(devi));
383 		if (ppb->ppb_fmcap & DDI_FM_ERRCB_CAPABLE)
384 			ddi_fm_handler_unregister(devi);
385 		if (ppb->ppb_fmcap & (DDI_FM_ERRCB_CAPABLE |
386 		    DDI_FM_EREPORT_CAPABLE))
387 			pci_ereport_teardown(devi);
388 		mutex_destroy(&ppb->ppb_peek_poke_mutex);
389 		mutex_destroy(&ppb->ppb_err_mutex);
390 		ddi_fm_fini(devi);
391 
392 		/*
393 		 * And finally free the per-pci soft state.
394 		 */
395 		ddi_soft_state_free(ppb_state, ddi_get_instance(devi));
396 
397 		/*
398 		 * Uninitialize hotplug support on this bus.
399 		 */
400 		(void) pcihp_uninit(devi);
401 		return (DDI_SUCCESS);
402 
403 	case DDI_SUSPEND:
404 		ppb = ddi_get_soft_state(ppb_state, ddi_get_instance(devi));
405 		ppb_save_config_regs(ppb);
406 		return (DDI_SUCCESS);
407 
408 	default:
409 		break;
410 	}
411 	return (DDI_FAILURE);
412 }
413 
414 /*ARGSUSED*/
415 static int
416 ppb_bus_map(dev_info_t *dip, dev_info_t *rdip, ddi_map_req_t *mp,
417 	off_t offset, off_t len, caddr_t *vaddrp)
418 {
419 	dev_info_t *pdip;
420 
421 	pdip = (dev_info_t *)DEVI(dip)->devi_parent;
422 	return ((DEVI(pdip)->devi_ops->devo_bus_ops->bus_map)(pdip,
423 	    rdip, mp, offset, len, vaddrp));
424 }
425 
426 /*ARGSUSED*/
427 static int
428 ppb_ctlops(dev_info_t *dip, dev_info_t *rdip,
429 	ddi_ctl_enum_t ctlop, void *arg, void *result)
430 {
431 	pci_regspec_t *drv_regp;
432 	int	reglen;
433 	int	rn;
434 	int	totreg;
435 	ppb_devstate_t *ppb;
436 	struct attachspec *asp;
437 
438 	switch (ctlop) {
439 	case DDI_CTLOPS_REPORTDEV:
440 		if (rdip == (dev_info_t *)0)
441 			return (DDI_FAILURE);
442 		cmn_err(CE_CONT, "?PCI-device: %s@%s, %s%d\n",
443 		    ddi_node_name(rdip), ddi_get_name_addr(rdip),
444 		    ddi_driver_name(rdip),
445 		    ddi_get_instance(rdip));
446 		return (DDI_SUCCESS);
447 
448 	case DDI_CTLOPS_INITCHILD:
449 		return (ppb_initchild((dev_info_t *)arg));
450 
451 	case DDI_CTLOPS_UNINITCHILD:
452 		ppb_removechild((dev_info_t *)arg);
453 		return (DDI_SUCCESS);
454 
455 	case DDI_CTLOPS_SIDDEV:
456 		return (DDI_SUCCESS);
457 
458 	case DDI_CTLOPS_REGSIZE:
459 	case DDI_CTLOPS_NREGS:
460 		if (rdip == (dev_info_t *)0)
461 			return (DDI_FAILURE);
462 		break;
463 
464 	/* X86 systems support PME wakeup from suspend */
465 	case DDI_CTLOPS_ATTACH:
466 		asp = (struct attachspec *)arg;
467 		if (asp->cmd == DDI_RESUME && asp->when == DDI_PRE)
468 			if (pci_pre_resume(rdip) != DDI_SUCCESS)
469 				return (DDI_FAILURE);
470 		return (ddi_ctlops(dip, rdip, ctlop, arg, result));
471 
472 
473 	case DDI_CTLOPS_DETACH:
474 		asp = (struct attachspec *)arg;
475 		if (asp->cmd == DDI_SUSPEND && asp->when == DDI_POST)
476 			if (pci_post_suspend(rdip) != DDI_SUCCESS)
477 				return (DDI_FAILURE);
478 		return (ddi_ctlops(dip, rdip, ctlop, arg, result));
479 
480 	case DDI_CTLOPS_PEEK:
481 	case DDI_CTLOPS_POKE:
482 		ppb = ddi_get_soft_state(ppb_state, ddi_get_instance(dip));
483 		if (strcmp(ddi_driver_name(ddi_get_parent(dip)), "npe") != 0)
484 			return (ddi_ctlops(dip, rdip, ctlop, arg, result));
485 		return (pci_peekpoke_check(dip, rdip, ctlop, arg, result,
486 		    ddi_ctlops, &ppb->ppb_err_mutex,
487 		    &ppb->ppb_peek_poke_mutex));
488 
489 	default:
490 		return (ddi_ctlops(dip, rdip, ctlop, arg, result));
491 	}
492 
493 	*(int *)result = 0;
494 	if (ddi_getlongprop(DDI_DEV_T_ANY, rdip,
495 	    DDI_PROP_DONTPASS | DDI_PROP_CANSLEEP, "reg",
496 	    (caddr_t)&drv_regp, &reglen) != DDI_SUCCESS)
497 		return (DDI_FAILURE);
498 
499 	totreg = reglen / sizeof (pci_regspec_t);
500 	if (ctlop == DDI_CTLOPS_NREGS)
501 		*(int *)result = totreg;
502 	else if (ctlop == DDI_CTLOPS_REGSIZE) {
503 		rn = *(int *)arg;
504 		if (rn >= totreg) {
505 			kmem_free(drv_regp, reglen);
506 			return (DDI_FAILURE);
507 		}
508 		*(off_t *)result = drv_regp[rn].pci_size_low;
509 	}
510 
511 	kmem_free(drv_regp, reglen);
512 	return (DDI_SUCCESS);
513 }
514 
515 static int
516 ppb_name_child(dev_info_t *child, char *name, int namelen)
517 {
518 	pci_regspec_t *pci_rp;
519 	uint_t slot, func;
520 	char **unit_addr;
521 	uint_t n;
522 
523 	/*
524 	 * For .conf nodes, use unit-address property as name
525 	 */
526 	if (ndi_dev_is_persistent_node(child) == 0) {
527 		if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY, child,
528 		    DDI_PROP_DONTPASS, "unit-address", &unit_addr, &n) !=
529 		    DDI_PROP_SUCCESS) {
530 			cmn_err(CE_WARN,
531 			    "cannot find unit-address in %s.conf",
532 			    ddi_driver_name(child));
533 			return (DDI_FAILURE);
534 		}
535 		if (n != 1 || *unit_addr == NULL || **unit_addr == 0) {
536 			cmn_err(CE_WARN, "unit-address property in %s.conf"
537 			    " not well-formed", ddi_driver_name(child));
538 			ddi_prop_free(unit_addr);
539 			return (DDI_SUCCESS);
540 		}
541 		(void) snprintf(name, namelen, "%s", *unit_addr);
542 		ddi_prop_free(unit_addr);
543 		return (DDI_SUCCESS);
544 	}
545 
546 	/* get child "reg" property */
547 	if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, child,
548 	    DDI_PROP_DONTPASS, "reg", (int **)&pci_rp, &n) != DDI_SUCCESS) {
549 		return (DDI_FAILURE);
550 	}
551 
552 	/* copy the device identifications */
553 	slot = PCI_REG_DEV_G(pci_rp->pci_phys_hi);
554 	func = PCI_REG_FUNC_G(pci_rp->pci_phys_hi);
555 
556 	if (func != 0)
557 		(void) snprintf(name, namelen, "%x,%x", slot, func);
558 	else
559 		(void) snprintf(name, namelen, "%x", slot);
560 
561 	ddi_prop_free(pci_rp);
562 	return (DDI_SUCCESS);
563 }
564 
565 static int
566 ppb_initchild(dev_info_t *child)
567 {
568 	struct ddi_parent_private_data *pdptr;
569 	char name[MAXNAMELEN];
570 	ddi_acc_handle_t config_handle;
571 	ushort_t command_preserve, command;
572 
573 	if (ppb_name_child(child, name, MAXNAMELEN) != DDI_SUCCESS)
574 		return (DDI_FAILURE);
575 	ddi_set_name_addr(child, name);
576 
577 	/*
578 	 * Pseudo nodes indicate a prototype node with per-instance
579 	 * properties to be merged into the real h/w device node.
580 	 * The interpretation of the unit-address is DD[,F]
581 	 * where DD is the device id and F is the function.
582 	 */
583 	if (ndi_dev_is_persistent_node(child) == 0) {
584 		extern int pci_allow_pseudo_children;
585 
586 		ddi_set_parent_data(child, NULL);
587 
588 		/*
589 		 * Try to merge the properties from this prototype
590 		 * node into real h/w nodes.
591 		 */
592 		if (ndi_merge_node(child, ppb_name_child) == DDI_SUCCESS) {
593 			/*
594 			 * Merged ok - return failure to remove the node.
595 			 */
596 			ddi_set_name_addr(child, NULL);
597 			return (DDI_FAILURE);
598 		}
599 
600 		/* workaround for ddivs to run under PCI */
601 		if (pci_allow_pseudo_children)
602 			return (DDI_SUCCESS);
603 
604 		/*
605 		 * The child was not merged into a h/w node,
606 		 * but there's not much we can do with it other
607 		 * than return failure to cause the node to be removed.
608 		 */
609 		cmn_err(CE_WARN, "!%s@%s: %s.conf properties not merged",
610 		    ddi_driver_name(child), ddi_get_name_addr(child),
611 		    ddi_driver_name(child));
612 		ddi_set_name_addr(child, NULL);
613 		return (DDI_NOT_WELL_FORMED);
614 	}
615 
616 	/* transfer select properties from PROM to kernel */
617 	if (ddi_getprop(DDI_DEV_T_NONE, child, DDI_PROP_DONTPASS,
618 	    "interrupts", -1) != -1) {
619 		pdptr = kmem_zalloc((sizeof (struct ddi_parent_private_data) +
620 		    sizeof (struct intrspec)), KM_SLEEP);
621 		pdptr->par_intr = (struct intrspec *)(pdptr + 1);
622 		pdptr->par_nintr = 1;
623 		ddi_set_parent_data(child, pdptr);
624 	} else
625 		ddi_set_parent_data(child, NULL);
626 
627 	if (pci_config_setup(child, &config_handle) != DDI_SUCCESS)
628 		return (DDI_FAILURE);
629 
630 	/*
631 	 * Support for the "command-preserve" property.
632 	 */
633 	command_preserve = ddi_prop_get_int(DDI_DEV_T_ANY, child,
634 	    DDI_PROP_DONTPASS, "command-preserve", 0);
635 	command = pci_config_get16(config_handle, PCI_CONF_COMM);
636 	command &= (command_preserve | PCI_COMM_BACK2BACK_ENAB);
637 	command |= (ppb_command_default & ~command_preserve);
638 	pci_config_put16(config_handle, PCI_CONF_COMM, command);
639 
640 	pci_config_teardown(&config_handle);
641 	return (DDI_SUCCESS);
642 }
643 
644 static void
645 ppb_removechild(dev_info_t *dip)
646 {
647 	struct ddi_parent_private_data *pdptr;
648 
649 	if ((pdptr = ddi_get_parent_data(dip)) != NULL) {
650 		kmem_free(pdptr, (sizeof (*pdptr) + sizeof (struct intrspec)));
651 		ddi_set_parent_data(dip, NULL);
652 	}
653 	ddi_set_name_addr(dip, NULL);
654 
655 	/*
656 	 * Strip the node to properly convert it back to prototype form
657 	 */
658 	ddi_remove_minor_node(dip, NULL);
659 
660 	impl_rem_dev_props(dip);
661 }
662 
663 /*
664  * ppb_save_config_regs
665  *
666  * This routine saves the state of the configuration registers of all
667  * the child nodes of each PBM.
668  *
669  * used by: ppb_detach() on suspends
670  *
671  * return value: none
672  */
673 static void
674 ppb_save_config_regs(ppb_devstate_t *ppb_p)
675 {
676 	int i;
677 	dev_info_t *dip;
678 	ddi_acc_handle_t config_handle;
679 
680 	for (i = 0, dip = ddi_get_child(ppb_p->dip); dip != NULL;
681 	    i++, dip = ddi_get_next_sibling(dip)) {
682 
683 		if (pci_config_setup(dip, &config_handle) != DDI_SUCCESS) {
684 			cmn_err(CE_WARN, "%s%d: can't config space for %s%d\n",
685 			    ddi_driver_name(ppb_p->dip),
686 			    ddi_get_instance(ppb_p->dip),
687 			    ddi_driver_name(dip),
688 			    ddi_get_instance(dip));
689 			continue;
690 		}
691 
692 		ppb_p->config_state[i].dip = dip;
693 		ppb_p->config_state[i].command =
694 		    pci_config_get16(config_handle, PCI_CONF_COMM);
695 		pci_config_teardown(&config_handle);
696 	}
697 	ppb_p->config_state_index = i;
698 }
699 
700 
701 /*
702  * ppb_restore_config_regs
703  *
704  * This routine restores the state of the configuration registers of all
705  * the child nodes of each PBM.
706  *
707  * used by: ppb_attach() on resume
708  *
709  * return value: none
710  */
711 static void
712 ppb_restore_config_regs(ppb_devstate_t *ppb_p)
713 {
714 	int i;
715 	dev_info_t *dip;
716 	ddi_acc_handle_t config_handle;
717 
718 	for (i = 0; i < ppb_p->config_state_index; i++) {
719 		dip = ppb_p->config_state[i].dip;
720 		if (pci_config_setup(dip, &config_handle) != DDI_SUCCESS) {
721 			cmn_err(CE_WARN, "%s%d: can't config space for %s%d\n",
722 			    ddi_driver_name(ppb_p->dip),
723 			    ddi_get_instance(ppb_p->dip),
724 			    ddi_driver_name(dip),
725 			    ddi_get_instance(dip));
726 			continue;
727 		}
728 		pci_config_put16(config_handle, PCI_CONF_COMM,
729 		    ppb_p->config_state[i].command);
730 		pci_config_teardown(&config_handle);
731 	}
732 }
733 
734 
735 /*
736  * returns the location of a hypertransport capability whose upper 16-bit
737  * register of the cap header matches <reg_val> after masking the register
738  * with <reg_mask>; if both <reg_mask> and <reg_val> are 0, it will return the
739  * first HT cap found
740  */
741 static uint8_t
742 ppb_find_ht_cap(ddi_acc_handle_t cfg_hdl, uint16_t reg_mask, uint16_t reg_val)
743 {
744 	uint16_t status, reg;
745 	uint8_t ptr, id;
746 
747 	status = pci_config_get16(cfg_hdl, PCI_CONF_STAT);
748 	if (status == 0xffff || !((status & PCI_STAT_CAP)))
749 		return (PCI_CAP_NEXT_PTR_NULL);
750 
751 	ptr = pci_config_get8(cfg_hdl, PCI_CONF_CAP_PTR);
752 	while (ptr != 0xFF &&
753 	    ptr != PCI_CAP_NEXT_PTR_NULL &&
754 	    ptr >= PCI_CAP_PTR_OFF) {
755 
756 		ptr &= PCI_CAP_PTR_MASK;
757 		id = pci_config_get8(cfg_hdl, ptr + PCI_CAP_ID);
758 
759 		if (id == PCI_CAP_ID_HT) {
760 			reg = pci_config_get16(cfg_hdl,
761 			    ptr + PCI_CAP_ID_REGS_OFF);
762 			if ((reg & reg_mask) == reg_val)
763 				return (ptr);
764 		}
765 		ptr = pci_config_get8(cfg_hdl, ptr + PCI_CAP_NEXT_PTR);
766 	}
767 
768 	return (PCI_CAP_NEXT_PTR_NULL);
769 }
770 
771 
772 static boolean_t
773 ppb_ht_msimap_check(ddi_acc_handle_t cfg_hdl)
774 {
775 	uint8_t ptr;
776 
777 	ptr = ppb_find_ht_cap(cfg_hdl,
778 	    PCI_CAP_HT_MSIMAP_TYPE_MASK | PCI_CAP_HT_MSIMAP_ENABLE_MASK,
779 	    PCI_CAP_HT_MSIMAP_TYPE | PCI_CAP_HT_MSIMAP_ENABLE);
780 
781 	if (ptr == PCI_CAP_NEXT_PTR_NULL)
782 		return (B_FALSE);
783 
784 	return (B_TRUE);
785 }
786 
787 
788 static int
789 ppb_ht_msimap_set(ddi_acc_handle_t cfg_hdl, int cmd)
790 {
791 	uint8_t ptr;
792 	uint16_t reg;
793 
794 	ptr = ppb_find_ht_cap(cfg_hdl, PCI_CAP_HT_MSIMAP_TYPE_MASK,
795 	    PCI_CAP_HT_MSIMAP_TYPE);
796 	if (ptr == PCI_CAP_NEXT_PTR_NULL)
797 		return (0);
798 
799 	reg = pci_config_get16(cfg_hdl, ptr + PCI_CAP_ID_REGS_OFF);
800 	switch (cmd) {
801 	case HT_MSIMAP_ENABLE:
802 		reg |= PCI_CAP_HT_MSIMAP_ENABLE;
803 		break;
804 	case HT_MSIMAP_DISABLE:
805 	default:
806 		reg &= ~(uint16_t)PCI_CAP_HT_MSIMAP_ENABLE;
807 	}
808 
809 	pci_config_put16(cfg_hdl, ptr + PCI_CAP_ID_REGS_OFF, reg);
810 	return (1);
811 }
812 
813 
814 /*
815  * intercept certain interrupt services to handle special cases
816  */
817 static int
818 ppb_intr_ops(dev_info_t *pdip, dev_info_t *rdip, ddi_intr_op_t intr_op,
819     ddi_intr_handle_impl_t *hdlp, void *result)
820 {
821 	ddi_acc_handle_t cfg_hdl;
822 	int rv = DDI_SUCCESS;
823 
824 	if (intr_op != DDI_INTROP_SUPPORTED_TYPES)
825 		return (i_ddi_intr_ops(pdip, rdip, intr_op, hdlp, result));
826 
827 	DDI_INTR_NEXDBG((CE_CONT,
828 	    "ppb_intr_ops: pdip 0x%p, rdip 0x%p, op %x handle 0x%p\n",
829 	    (void *)pdip, (void *)rdip, intr_op, (void *)hdlp));
830 
831 	/* Fixed interrupt is supported by default */
832 	*(int *)result = DDI_INTR_TYPE_FIXED;
833 
834 	if (ppb_support_msi == -1) {
835 		DDI_INTR_NEXDBG((CE_CONT,
836 		    "ppb_intr_ops: MSI is not allowed\n"));
837 		goto OUT;
838 	}
839 
840 	if (ppb_support_msi == 1) {
841 		DDI_INTR_NEXDBG((CE_CONT,
842 		    "ppb_intr_ops: MSI is always allowed\n"));
843 		rv = i_ddi_intr_ops(pdip, rdip, intr_op, hdlp, result);
844 		goto OUT;
845 	}
846 
847 	if (pci_config_setup(pdip, &cfg_hdl) != DDI_SUCCESS) {
848 		DDI_INTR_NEXDBG((CE_CONT,
849 		    "ppb_intr_ops: pci_config_setup() failed\n"));
850 		goto OUT;
851 	}
852 
853 	/*
854 	 * check for hypertransport msi mapping capability
855 	 */
856 	if (ppb_ht_msimap_check(cfg_hdl)) {
857 		DDI_INTR_NEXDBG((CE_CONT,
858 		    "ppb_intr_ops: HT MSI mapping enabled\n"));
859 		rv = i_ddi_intr_ops(pdip, rdip, intr_op, hdlp, result);
860 	}
861 
862 	/*
863 	 * if we add failure conditions after pci_config_setup, move this to
864 	 * OUT and use an extra flag to indicate the need to teardown cfg_hdl
865 	 */
866 	pci_config_teardown(&cfg_hdl);
867 
868 OUT:
869 	DDI_INTR_NEXDBG((CE_CONT,
870 	    "ppb_intr_ops: rdip 0x%p, returns supported types: 0x%x\n",
871 	    (void *)rdip, *(int *)result));
872 	return (rv);
873 }
874 
875 static int
876 ppb_open(dev_t *devp, int flags, int otyp, cred_t *credp)
877 {
878 	return ((pcihp_get_cb_ops())->cb_open(devp, flags, otyp, credp));
879 }
880 
881 static int
882 ppb_close(dev_t dev, int flags, int otyp, cred_t *credp)
883 {
884 	return ((pcihp_get_cb_ops())->cb_close(dev, flags, otyp, credp));
885 }
886 
887 static int
888 ppb_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp, int *rvalp)
889 {
890 	return ((pcihp_get_cb_ops())->cb_ioctl(dev, cmd, arg, mode, credp,
891 	    rvalp));
892 }
893 
894 static int
895 ppb_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op,
896 	int flags, char *name, caddr_t valuep, int *lengthp)
897 {
898 	return ((pcihp_get_cb_ops())->cb_prop_op(dev, dip, prop_op, flags,
899 	    name, valuep, lengthp));
900 }
901 
902 static int
903 ppb_info(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **result)
904 {
905 	return (pcihp_info(dip, cmd, arg, result));
906 }
907 
908 /*ARGSUSED*/
909 static int
910 ppb_fm_init(dev_info_t *dip, dev_info_t *tdip, int cap,
911     ddi_iblock_cookie_t *ibc)
912 {
913 	ppb_devstate_t  *ppb = ddi_get_soft_state(ppb_state,
914 	    ddi_get_instance(dip));
915 
916 	ASSERT(ibc != NULL);
917 	*ibc = ppb->ppb_fm_ibc;
918 
919 	return (ppb->ppb_fmcap);
920 }
921 
922 /*ARGSUSED*/
923 static int
924 ppb_fm_callback(dev_info_t *dip, ddi_fm_error_t *derr, const void *no_used)
925 {
926 	ppb_devstate_t  *ppb = ddi_get_soft_state(ppb_state,
927 	    ddi_get_instance(dip));
928 
929 	mutex_enter(&ppb->ppb_err_mutex);
930 	pci_ereport_post(dip, derr, NULL);
931 	mutex_exit(&ppb->ppb_err_mutex);
932 	return (derr->fme_status);
933 }
934