xref: /titanic_52/usr/src/uts/i86pc/io/pci/pci.c (revision 0a0e9771ca0211c15f3ac4466b661c145feeb9e4)
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  *	Host to PCI local bus driver
28  */
29 
30 #include <sys/conf.h>
31 #include <sys/modctl.h>
32 #include <sys/pci.h>
33 #include <sys/pci_impl.h>
34 #include <sys/sysmacros.h>
35 #include <sys/sunndi.h>
36 #include <sys/ddifm.h>
37 #include <sys/ndifm.h>
38 #include <sys/fm/protocol.h>
39 #include <sys/hotplug/pci/pcihp.h>
40 #include <io/pci/pci_common.h>
41 #include <io/pci/pci_tools_ext.h>
42 
43 /* Save minimal state. */
44 void *pci_statep;
45 
46 /*
47  * Bus Operation functions
48  */
49 static int	pci_bus_map(dev_info_t *, dev_info_t *, ddi_map_req_t *,
50 		    off_t, off_t, caddr_t *);
51 static int	pci_ctlops(dev_info_t *, dev_info_t *, ddi_ctl_enum_t,
52 		    void *, void *);
53 static int	pci_intr_ops(dev_info_t *, dev_info_t *, ddi_intr_op_t,
54 		    ddi_intr_handle_impl_t *, void *);
55 static int	pci_fm_init(dev_info_t *, dev_info_t *, int,
56 		    ddi_iblock_cookie_t *);
57 static int	pci_fm_callback(dev_info_t *, ddi_fm_error_t *, const void *);
58 
59 struct bus_ops pci_bus_ops = {
60 	BUSO_REV,
61 	pci_bus_map,
62 	NULL,
63 	NULL,
64 	NULL,
65 	i_ddi_map_fault,
66 	ddi_dma_map,
67 	ddi_dma_allochdl,
68 	ddi_dma_freehdl,
69 	ddi_dma_bindhdl,
70 	ddi_dma_unbindhdl,
71 	ddi_dma_flush,
72 	ddi_dma_win,
73 	ddi_dma_mctl,
74 	pci_ctlops,
75 	ddi_bus_prop_op,
76 	0,		/* (*bus_get_eventcookie)();	*/
77 	0,		/* (*bus_add_eventcall)();	*/
78 	0,		/* (*bus_remove_eventcall)();	*/
79 	0,		/* (*bus_post_event)();		*/
80 	0,		/* (*bus_intr_ctl)(); */
81 	0,		/* (*bus_config)(); */
82 	0,		/* (*bus_unconfig)(); */
83 	pci_fm_init,	/* (*bus_fm_init)(); */
84 	NULL,		/* (*bus_fm_fini)(); */
85 	NULL,		/* (*bus_fm_access_enter)(); */
86 	NULL,		/* (*bus_fm_access_exit)(); */
87 	NULL,		/* (*bus_power)(); */
88 	pci_intr_ops	/* (*bus_intr_op)(); */
89 };
90 
91 /*
92  * One goal here is to leverage off of the pcihp.c source without making
93  * changes to it.  Call into it's cb_ops directly if needed, piggybacking
94  * anything else needed by the pci_tools.c module.  Only pci_tools and pcihp
95  * will be opening PCI nexus driver file descriptors.
96  */
97 static int	pci_open(dev_t *, int, int, cred_t *);
98 static int	pci_close(dev_t, int, int, cred_t *);
99 static int	pci_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
100 static int	pci_prop_op(dev_t, dev_info_t *, ddi_prop_op_t, int, char *,
101 		    caddr_t, int *);
102 static int	pci_info(dev_info_t *, ddi_info_cmd_t, void *, void **);
103 static void	pci_peekpoke_cb(dev_info_t *, ddi_fm_error_t *);
104 
105 struct cb_ops pci_cb_ops = {
106 	pci_open,			/* open */
107 	pci_close,			/* close */
108 	nodev,				/* strategy */
109 	nodev,				/* print */
110 	nodev,				/* dump */
111 	nodev,				/* read */
112 	nodev,				/* write */
113 	pci_ioctl,			/* ioctl */
114 	nodev,				/* devmap */
115 	nodev,				/* mmap */
116 	nodev,				/* segmap */
117 	nochpoll,			/* poll */
118 	pci_prop_op,			/* cb_prop_op */
119 	NULL,				/* streamtab */
120 	D_NEW | D_MP | D_HOTPLUG,	/* Driver compatibility flag */
121 	CB_REV,				/* rev */
122 	nodev,				/* int (*cb_aread)() */
123 	nodev				/* int (*cb_awrite)() */
124 };
125 
126 /*
127  * Device Node Operation functions
128  */
129 static int pci_attach(dev_info_t *devi, ddi_attach_cmd_t cmd);
130 static int pci_detach(dev_info_t *devi, ddi_detach_cmd_t cmd);
131 
132 struct dev_ops pci_ops = {
133 	DEVO_REV,		/* devo_rev */
134 	0,			/* refcnt  */
135 	pci_info,		/* info */
136 	nulldev,		/* identify */
137 	nulldev,		/* probe */
138 	pci_attach,		/* attach */
139 	pci_detach,		/* detach */
140 	nulldev,		/* reset */
141 	&pci_cb_ops,		/* driver operations */
142 	&pci_bus_ops,		/* bus operations */
143 	NULL,			/* power */
144 	ddi_quiesce_not_needed		/* quiesce */
145 };
146 
147 /*
148  * This variable controls the default setting of the command register
149  * for pci devices.  See pci_initchild() for details.
150  */
151 static ushort_t pci_command_default = PCI_COMM_ME |
152 					PCI_COMM_MAE |
153 					PCI_COMM_IO;
154 
155 /*
156  * Internal routines in support of particular pci_ctlops.
157  */
158 static int pci_removechild(dev_info_t *child);
159 static int pci_initchild(dev_info_t *child);
160 
161 /*
162  * Module linkage information for the kernel.
163  */
164 
165 static struct modldrv modldrv = {
166 	&mod_driverops, /* Type of module */
167 	"host to PCI nexus driver",
168 	&pci_ops,	/* driver ops */
169 };
170 
171 static struct modlinkage modlinkage = {
172 	MODREV_1,
173 	(void *)&modldrv,
174 	NULL
175 };
176 
177 int
178 _init(void)
179 {
180 	int e;
181 
182 	/*
183 	 * Initialize per-pci bus soft state pointer.
184 	 */
185 	e = ddi_soft_state_init(&pci_statep, sizeof (pci_state_t), 1);
186 	if (e != 0)
187 		return (e);
188 
189 	if ((e = mod_install(&modlinkage)) != 0)
190 		ddi_soft_state_fini(&pci_statep);
191 
192 	return (e);
193 }
194 
195 int
196 _fini(void)
197 {
198 	int rc;
199 
200 	rc = mod_remove(&modlinkage);
201 	if (rc != 0)
202 		return (rc);
203 
204 	ddi_soft_state_fini(&pci_statep);
205 
206 	return (rc);
207 }
208 
209 int
210 _info(struct modinfo *modinfop)
211 {
212 	return (mod_info(&modlinkage, modinfop));
213 }
214 
215 /*ARGSUSED*/
216 static int
217 pci_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
218 {
219 	/*
220 	 * Use the minor number as constructed by pcihp, as the index value to
221 	 * ddi_soft_state_zalloc.
222 	 */
223 	int instance = ddi_get_instance(devi);
224 	pci_state_t *pcip = NULL;
225 	switch (cmd) {
226 	case DDI_ATTACH:
227 		break;
228 
229 	case DDI_RESUME:
230 		return (DDI_SUCCESS);
231 
232 	default:
233 		return (DDI_FAILURE);
234 	}
235 
236 	if (ddi_prop_update_string(DDI_DEV_T_NONE, devi, "device_type", "pci")
237 	    != DDI_PROP_SUCCESS) {
238 		cmn_err(CE_WARN, "pci:  'device_type' prop create failed");
239 	}
240 
241 	if (ddi_soft_state_zalloc(pci_statep, instance) == DDI_SUCCESS) {
242 		pcip = ddi_get_soft_state(pci_statep, instance);
243 	}
244 
245 	if (pcip == NULL) {
246 		goto bad_soft_state;
247 	}
248 
249 	pcip->pci_dip = devi;
250 
251 	/*
252 	 * Initialize hotplug support on this bus. At minimum
253 	 * (for non hotplug bus) this would create ":devctl" minor
254 	 * node to support DEVCTL_DEVICE_* and DEVCTL_BUS_* ioctls
255 	 * to this bus.
256 	 */
257 	if (pcihp_init(devi) != DDI_SUCCESS) {
258 		cmn_err(CE_WARN, "pci: Failed to setup hotplug framework");
259 		goto bad_pcihp_init;
260 	}
261 
262 	/* Second arg: initialize for pci, not pci_express */
263 	if (pcitool_init(devi, B_FALSE) != DDI_SUCCESS) {
264 		goto bad_pcitool_init;
265 	}
266 
267 	pcip->pci_fmcap = DDI_FM_ERRCB_CAPABLE |
268 	    DDI_FM_ACCCHK_CAPABLE | DDI_FM_DMACHK_CAPABLE;
269 	ddi_fm_init(devi, &pcip->pci_fmcap, &pcip->pci_fm_ibc);
270 	mutex_init(&pcip->pci_err_mutex, NULL, MUTEX_DRIVER,
271 	    (void *)pcip->pci_fm_ibc);
272 	mutex_init(&pcip->pci_peek_poke_mutex, NULL, MUTEX_DRIVER,
273 	    (void *)pcip->pci_fm_ibc);
274 	if (pcip->pci_fmcap & DDI_FM_ERRCB_CAPABLE) {
275 		pci_ereport_setup(devi);
276 		ddi_fm_handler_register(devi, pci_fm_callback, NULL);
277 	}
278 
279 	ddi_report_dev(devi);
280 
281 	return (DDI_SUCCESS);
282 
283 bad_pcitool_init:
284 	(void) pcihp_uninit(devi);
285 bad_pcihp_init:
286 	ddi_soft_state_free(pci_statep, instance);
287 bad_soft_state:
288 	return (DDI_FAILURE);
289 }
290 
291 /*ARGSUSED*/
292 static int
293 pci_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
294 {
295 	int instance = ddi_get_instance(devi);
296 	pci_state_t *pcip;
297 
298 	pcip = ddi_get_soft_state(pci_statep, ddi_get_instance(devi));
299 
300 
301 	switch (cmd) {
302 	case DDI_DETACH:
303 		if (pcip->pci_fmcap & DDI_FM_ERRCB_CAPABLE) {
304 			ddi_fm_handler_unregister(devi);
305 			pci_ereport_teardown(devi);
306 		}
307 		mutex_destroy(&pcip->pci_peek_poke_mutex);
308 		mutex_destroy(&pcip->pci_err_mutex);
309 		ddi_fm_fini(devi);	/* Uninitialize pcitool support. */
310 		pcitool_uninit(devi);
311 
312 		/* Uninitialize hotplug support on this bus. */
313 		(void) pcihp_uninit(devi);
314 
315 		ddi_soft_state_free(pci_statep, instance);
316 
317 		return (DDI_SUCCESS);
318 	case DDI_SUSPEND:
319 		return (DDI_SUCCESS);
320 	default:
321 		return (DDI_FAILURE);
322 	}
323 }
324 
325 static int
326 pci_bus_map(dev_info_t *dip, dev_info_t *rdip, ddi_map_req_t *mp,
327 	off_t offset, off_t len, caddr_t *vaddrp)
328 {
329 	struct regspec reg;
330 	ddi_map_req_t mr;
331 	ddi_acc_hdl_t *hp;
332 	pci_regspec_t pci_reg;
333 	pci_regspec_t *pci_rp;
334 	int 	rnumber;
335 	int	length;
336 	pci_acc_cfblk_t *cfp;
337 	int	space;
338 
339 
340 	mr = *mp; /* Get private copy of request */
341 	mp = &mr;
342 
343 	/*
344 	 * check for register number
345 	 */
346 	switch (mp->map_type) {
347 	case DDI_MT_REGSPEC:
348 		pci_reg = *(pci_regspec_t *)(mp->map_obj.rp);
349 		pci_rp = &pci_reg;
350 		if (pci_common_get_reg_prop(rdip, pci_rp) != DDI_SUCCESS)
351 			return (DDI_FAILURE);
352 		break;
353 	case DDI_MT_RNUMBER:
354 		rnumber = mp->map_obj.rnumber;
355 		/*
356 		 * get ALL "reg" properties for dip, select the one of
357 		 * of interest. In x86, "assigned-addresses" property
358 		 * is identical to the "reg" property, so there is no
359 		 * need to cross check the two to determine the physical
360 		 * address of the registers.
361 		 * This routine still performs some validity checks to
362 		 * make sure that everything is okay.
363 		 */
364 		if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, rdip,
365 		    DDI_PROP_DONTPASS, "reg", (int **)&pci_rp,
366 		    (uint_t *)&length) != DDI_PROP_SUCCESS)
367 			return (DDI_FAILURE);
368 
369 		/*
370 		 * validate the register number.
371 		 */
372 		length /= (sizeof (pci_regspec_t) / sizeof (int));
373 		if (rnumber >= length) {
374 			ddi_prop_free(pci_rp);
375 			return (DDI_FAILURE);
376 		}
377 
378 		/*
379 		 * copy the required entry.
380 		 */
381 		pci_reg = pci_rp[rnumber];
382 
383 		/*
384 		 * free the memory allocated by ddi_prop_lookup_int_array
385 		 */
386 		ddi_prop_free(pci_rp);
387 
388 		pci_rp = &pci_reg;
389 		if (pci_common_get_reg_prop(rdip, pci_rp) != DDI_SUCCESS)
390 			return (DDI_FAILURE);
391 		mp->map_type = DDI_MT_REGSPEC;
392 		break;
393 	default:
394 		return (DDI_ME_INVAL);
395 	}
396 
397 	space = pci_rp->pci_phys_hi & PCI_REG_ADDR_M;
398 
399 	/*
400 	 * check for unmap and unlock of address space
401 	 */
402 	if ((mp->map_op == DDI_MO_UNMAP) || (mp->map_op == DDI_MO_UNLOCK)) {
403 		/*
404 		 * Adjust offset and length
405 		 * A non-zero length means override the one in the regspec.
406 		 */
407 		pci_rp->pci_phys_low += (uint_t)offset;
408 		if (len != 0)
409 			pci_rp->pci_size_low = len;
410 
411 		switch (space) {
412 		case PCI_ADDR_CONFIG:
413 			/* No work required on unmap of Config space */
414 			return (DDI_SUCCESS);
415 
416 		case PCI_ADDR_IO:
417 			reg.regspec_bustype = 1;
418 			break;
419 
420 		case PCI_ADDR_MEM64:
421 			/*
422 			 * MEM64 requires special treatment on map, to check
423 			 * that the device is below 4G.  On unmap, however,
424 			 * we can assume that everything is OK... the map
425 			 * must have succeeded.
426 			 */
427 			/* FALLTHROUGH */
428 		case PCI_ADDR_MEM32:
429 			reg.regspec_bustype = 0;
430 			break;
431 
432 		default:
433 			return (DDI_FAILURE);
434 		}
435 		reg.regspec_addr = pci_rp->pci_phys_low;
436 		reg.regspec_size = pci_rp->pci_size_low;
437 
438 		mp->map_obj.rp = &reg;
439 		return (ddi_map(dip, mp, (off_t)0, (off_t)0, vaddrp));
440 
441 	}
442 
443 	/* check for user mapping request - not legal for Config */
444 	if (mp->map_op == DDI_MO_MAP_HANDLE && space == PCI_ADDR_CONFIG) {
445 		return (DDI_FAILURE);
446 	}
447 
448 	/*
449 	 * check for config space
450 	 * On x86, CONFIG is not mapped via MMU and there is
451 	 * no endian-ness issues. Set the attr field in the handle to
452 	 * indicate that the common routines to call the nexus driver.
453 	 */
454 	if (space == PCI_ADDR_CONFIG) {
455 		/* Can't map config space without a handle */
456 		hp = (ddi_acc_hdl_t *)mp->map_handlep;
457 		if (hp == NULL)
458 			return (DDI_FAILURE);
459 
460 		/* record the device address for future reference */
461 		cfp = (pci_acc_cfblk_t *)&hp->ah_bus_private;
462 		cfp->c_busnum = PCI_REG_BUS_G(pci_rp->pci_phys_hi);
463 		cfp->c_devnum = PCI_REG_DEV_G(pci_rp->pci_phys_hi);
464 		cfp->c_funcnum = PCI_REG_FUNC_G(pci_rp->pci_phys_hi);
465 
466 		*vaddrp = (caddr_t)offset;
467 		return (pci_fm_acc_setup(hp, offset, len));
468 	}
469 
470 	/*
471 	 * range check
472 	 */
473 	if ((offset >= pci_rp->pci_size_low) ||
474 	    (len > pci_rp->pci_size_low) ||
475 	    (offset + len > pci_rp->pci_size_low)) {
476 		return (DDI_FAILURE);
477 	}
478 
479 	/*
480 	 * Adjust offset and length
481 	 * A non-zero length means override the one in the regspec.
482 	 */
483 	pci_rp->pci_phys_low += (uint_t)offset;
484 	if (len != 0)
485 		pci_rp->pci_size_low = len;
486 
487 	/*
488 	 * convert the pci regsec into the generic regspec used by the
489 	 * parent root nexus driver.
490 	 */
491 	switch (space) {
492 	case PCI_ADDR_IO:
493 		reg.regspec_bustype = 1;
494 		break;
495 	case PCI_ADDR_MEM64:
496 		/*
497 		 * We can't handle 64-bit devices that are mapped above
498 		 * 4G or that are larger than 4G.
499 		 */
500 		if (pci_rp->pci_phys_mid != 0 ||
501 		    pci_rp->pci_size_hi != 0)
502 			return (DDI_FAILURE);
503 		/*
504 		 * Other than that, we can treat them as 32-bit mappings
505 		 */
506 		/* FALLTHROUGH */
507 	case PCI_ADDR_MEM32:
508 		reg.regspec_bustype = 0;
509 		break;
510 	default:
511 		return (DDI_FAILURE);
512 	}
513 	reg.regspec_addr = pci_rp->pci_phys_low;
514 	reg.regspec_size = pci_rp->pci_size_low;
515 
516 	mp->map_obj.rp = &reg;
517 	return (ddi_map(dip, mp, (off_t)0, (off_t)0, vaddrp));
518 }
519 
520 
521 /*ARGSUSED*/
522 static int
523 pci_ctlops(dev_info_t *dip, dev_info_t *rdip,
524 	ddi_ctl_enum_t ctlop, void *arg, void *result)
525 {
526 	pci_regspec_t *drv_regp;
527 	uint_t	reglen;
528 	int	rn;
529 	int	totreg;
530 	pci_state_t *pcip;
531 	struct  attachspec *asp;
532 
533 	switch (ctlop) {
534 	case DDI_CTLOPS_REPORTDEV:
535 		if (rdip == (dev_info_t *)0)
536 			return (DDI_FAILURE);
537 		cmn_err(CE_CONT, "?PCI-device: %s@%s, %s%d\n",
538 		    ddi_node_name(rdip), ddi_get_name_addr(rdip),
539 		    ddi_driver_name(rdip),
540 		    ddi_get_instance(rdip));
541 		return (DDI_SUCCESS);
542 
543 	case DDI_CTLOPS_INITCHILD:
544 		return (pci_initchild((dev_info_t *)arg));
545 
546 	case DDI_CTLOPS_UNINITCHILD:
547 		return (pci_removechild((dev_info_t *)arg));
548 
549 	case DDI_CTLOPS_SIDDEV:
550 		return (DDI_SUCCESS);
551 
552 	case DDI_CTLOPS_REGSIZE:
553 	case DDI_CTLOPS_NREGS:
554 		if (rdip == (dev_info_t *)0)
555 			return (DDI_FAILURE);
556 
557 		*(int *)result = 0;
558 		if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, rdip,
559 		    DDI_PROP_DONTPASS, "reg", (int **)&drv_regp,
560 		    &reglen) != DDI_PROP_SUCCESS) {
561 			return (DDI_FAILURE);
562 		}
563 
564 		totreg = (reglen * sizeof (int)) / sizeof (pci_regspec_t);
565 		if (ctlop == DDI_CTLOPS_NREGS)
566 			*(int *)result = totreg;
567 		else if (ctlop == DDI_CTLOPS_REGSIZE) {
568 			rn = *(int *)arg;
569 			if (rn >= totreg) {
570 				ddi_prop_free(drv_regp);
571 				return (DDI_FAILURE);
572 			}
573 			*(off_t *)result = drv_regp[rn].pci_size_low;
574 		}
575 		ddi_prop_free(drv_regp);
576 
577 		return (DDI_SUCCESS);
578 
579 	case DDI_CTLOPS_POWER: {
580 		power_req_t	*reqp = (power_req_t *)arg;
581 		/*
582 		 * We currently understand reporting of PCI_PM_IDLESPEED
583 		 * capability. Everything else is passed up.
584 		 */
585 		if ((reqp->request_type == PMR_REPORT_PMCAP) &&
586 		    (reqp->req.report_pmcap_req.cap ==  PCI_PM_IDLESPEED)) {
587 
588 			return (DDI_SUCCESS);
589 		}
590 		return (ddi_ctlops(dip, rdip, ctlop, arg, result));
591 	}
592 
593 	case DDI_CTLOPS_PEEK:
594 	case DDI_CTLOPS_POKE:
595 		pcip = ddi_get_soft_state(pci_statep, ddi_get_instance(dip));
596 		return (pci_peekpoke_check(dip, rdip, ctlop, arg, result,
597 		    pci_common_peekpoke, &pcip->pci_err_mutex,
598 		    &pcip->pci_peek_poke_mutex, pci_peekpoke_cb));
599 
600 	/* for now only X86 systems support PME wakeup from suspended state */
601 	case DDI_CTLOPS_ATTACH:
602 		asp = (struct attachspec *)arg;
603 		if (asp->cmd == DDI_RESUME && asp->when == DDI_PRE)
604 			if (pci_pre_resume(rdip) != DDI_SUCCESS)
605 				return (DDI_FAILURE);
606 		return (ddi_ctlops(dip, rdip, ctlop, arg, result));
607 
608 	case DDI_CTLOPS_DETACH:
609 		asp = (struct attachspec *)arg;
610 		if (asp->cmd == DDI_SUSPEND && asp->when == DDI_POST)
611 			if (pci_post_suspend(rdip) != DDI_SUCCESS)
612 				return (DDI_FAILURE);
613 		return (ddi_ctlops(dip, rdip, ctlop, arg, result));
614 
615 	default:
616 		return (ddi_ctlops(dip, rdip, ctlop, arg, result));
617 	}
618 
619 	/* NOTREACHED */
620 
621 }
622 
623 /*
624  * pci_intr_ops
625  */
626 static int
627 pci_intr_ops(dev_info_t *pdip, dev_info_t *rdip, ddi_intr_op_t intr_op,
628     ddi_intr_handle_impl_t *hdlp, void *result)
629 {
630 	return (pci_common_intr_ops(pdip, rdip, intr_op, hdlp, result));
631 }
632 
633 
634 static int
635 pci_initchild(dev_info_t *child)
636 {
637 	char name[80];
638 	ddi_acc_handle_t config_handle;
639 	ushort_t command_preserve, command;
640 
641 	if (pci_common_name_child(child, name, 80) != DDI_SUCCESS) {
642 		return (DDI_FAILURE);
643 	}
644 	ddi_set_name_addr(child, name);
645 
646 	/*
647 	 * Pseudo nodes indicate a prototype node with per-instance
648 	 * properties to be merged into the real h/w device node.
649 	 * The interpretation of the unit-address is DD[,F]
650 	 * where DD is the device id and F is the function.
651 	 */
652 	if (ndi_dev_is_persistent_node(child) == 0) {
653 		extern int pci_allow_pseudo_children;
654 
655 		ddi_set_parent_data(child, NULL);
656 
657 		/*
658 		 * Try to merge the properties from this prototype
659 		 * node into real h/w nodes.
660 		 */
661 		if (ndi_merge_node(child, pci_common_name_child) ==
662 		    DDI_SUCCESS) {
663 			/*
664 			 * Merged ok - return failure to remove the node.
665 			 */
666 			ddi_set_name_addr(child, NULL);
667 			return (DDI_FAILURE);
668 		}
669 
670 		/* workaround for ddivs to run under PCI */
671 		if (pci_allow_pseudo_children) {
672 			/*
673 			 * If the "interrupts" property doesn't exist,
674 			 * this must be the ddivs no-intr case, and it returns
675 			 * DDI_SUCCESS instead of DDI_FAILURE.
676 			 */
677 			if (ddi_prop_get_int(DDI_DEV_T_ANY, child,
678 			    DDI_PROP_DONTPASS, "interrupts", -1) == -1)
679 				return (DDI_SUCCESS);
680 			/*
681 			 * Create the ddi_parent_private_data for a pseudo
682 			 * child.
683 			 */
684 			pci_common_set_parent_private_data(child);
685 			return (DDI_SUCCESS);
686 		}
687 
688 		/*
689 		 * The child was not merged into a h/w node,
690 		 * but there's not much we can do with it other
691 		 * than return failure to cause the node to be removed.
692 		 */
693 		cmn_err(CE_WARN, "!%s@%s: %s.conf properties not merged",
694 		    ddi_get_name(child), ddi_get_name_addr(child),
695 		    ddi_get_name(child));
696 		ddi_set_name_addr(child, NULL);
697 		return (DDI_NOT_WELL_FORMED);
698 	}
699 
700 	if (ddi_prop_get_int(DDI_DEV_T_ANY, child, DDI_PROP_DONTPASS,
701 	    "interrupts", -1) != -1)
702 		pci_common_set_parent_private_data(child);
703 	else
704 		ddi_set_parent_data(child, NULL);
705 
706 	/*
707 	 * initialize command register
708 	 */
709 	if (pci_config_setup(child, &config_handle) != DDI_SUCCESS)
710 		return (DDI_FAILURE);
711 
712 	/*
713 	 * Support for the "command-preserve" property.
714 	 */
715 	command_preserve = ddi_prop_get_int(DDI_DEV_T_ANY, child,
716 	    DDI_PROP_DONTPASS, "command-preserve", 0);
717 	command = pci_config_get16(config_handle, PCI_CONF_COMM);
718 	command &= (command_preserve | PCI_COMM_BACK2BACK_ENAB);
719 	command |= (pci_command_default & ~command_preserve);
720 	pci_config_put16(config_handle, PCI_CONF_COMM, command);
721 
722 	pci_config_teardown(&config_handle);
723 	return (DDI_SUCCESS);
724 }
725 
726 static int
727 pci_removechild(dev_info_t *dip)
728 {
729 	struct ddi_parent_private_data *pdptr;
730 
731 	if ((pdptr = ddi_get_parent_data(dip)) != NULL) {
732 		kmem_free(pdptr, (sizeof (*pdptr) + sizeof (struct intrspec)));
733 		ddi_set_parent_data(dip, NULL);
734 	}
735 	ddi_set_name_addr(dip, NULL);
736 
737 	/*
738 	 * Strip the node to properly convert it back to prototype form
739 	 */
740 	ddi_remove_minor_node(dip, NULL);
741 
742 	impl_rem_dev_props(dip);
743 
744 	return (DDI_SUCCESS);
745 }
746 
747 
748 /*
749  * When retrofitting this module for pci_tools, functions such as open, close,
750  * and ioctl are now pulled into this module.  Before this, the functions in
751  * the pcihp module were referenced directly.  Now they are called or
752  * referenced through the pcihp cb_ops structure from functions in this module.
753  */
754 
755 static int
756 pci_open(dev_t *devp, int flags, int otyp, cred_t *credp)
757 {
758 	return ((pcihp_get_cb_ops())->cb_open(devp, flags, otyp, credp));
759 }
760 
761 static int
762 pci_close(dev_t dev, int flags, int otyp, cred_t *credp)
763 {
764 	return ((pcihp_get_cb_ops())->cb_close(dev, flags, otyp, credp));
765 }
766 
767 static int
768 pci_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp, int *rvalp)
769 {
770 	minor_t		minor = getminor(dev);
771 	int		instance = PCIHP_AP_MINOR_NUM_TO_INSTANCE(minor);
772 	pci_state_t	*pci_p = ddi_get_soft_state(pci_statep, instance);
773 
774 	if (pci_p == NULL)
775 		return (ENXIO);
776 
777 	return (pci_common_ioctl(pci_p->pci_dip,
778 	    dev, cmd, arg, mode, credp, rvalp));
779 }
780 
781 
782 static int
783 pci_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op,
784 	int flags, char *name, caddr_t valuep, int *lengthp)
785 {
786 	return ((pcihp_get_cb_ops())->cb_prop_op(dev, dip, prop_op, flags,
787 	    name, valuep, lengthp));
788 }
789 
790 static int
791 pci_info(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **result)
792 {
793 	return (pcihp_info(dip, cmd, arg, result));
794 }
795 
796 void pci_peekpoke_cb(dev_info_t *dip, ddi_fm_error_t *derr) {
797 	(void) pci_ereport_post(dip, derr, NULL);
798 }
799 
800 /*ARGSUSED*/
801 static int
802 pci_fm_init(dev_info_t *dip, dev_info_t *tdip, int cap,
803     ddi_iblock_cookie_t *ibc)
804 {
805 	pci_state_t  *pcip = ddi_get_soft_state(pci_statep,
806 	    ddi_get_instance(dip));
807 
808 	ASSERT(ibc != NULL);
809 	*ibc = pcip->pci_fm_ibc;
810 
811 	return (pcip->pci_fmcap);
812 }
813 
814 /*ARGSUSED*/
815 static int
816 pci_fm_callback(dev_info_t *dip, ddi_fm_error_t *derr, const void *no_used)
817 {
818 	pci_state_t  *pcip = ddi_get_soft_state(pci_statep,
819 	    ddi_get_instance(dip));
820 
821 	mutex_enter(&pcip->pci_err_mutex);
822 	pci_ereport_post(dip, derr, NULL);
823 	mutex_exit(&pcip->pci_err_mutex);
824 	return (derr->fme_status);
825 }
826