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