xref: /titanic_52/usr/src/uts/i86pc/io/pci/pci.c (revision bd670b35a010421b6e1a5536c34453a827007c81)
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 2009 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 	"x86 Host to PCI nexus driver",		/* Name of module */
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 	pcip->pci_soft_state = PCI_SOFT_STATE_CLOSED;
251 
252 	/*
253 	 * Initialize hotplug support on this bus. At minimum
254 	 * (for non hotplug bus) this would create ":devctl" minor
255 	 * node to support DEVCTL_DEVICE_* and DEVCTL_BUS_* ioctls
256 	 * to this bus.
257 	 */
258 	if (pcihp_init(devi) != DDI_SUCCESS) {
259 		cmn_err(CE_WARN, "pci: Failed to setup hotplug framework");
260 		goto bad_pcihp_init;
261 	}
262 
263 	/* Second arg: initialize for pci, not pci_express */
264 	if (pcitool_init(devi, B_FALSE) != DDI_SUCCESS) {
265 		goto bad_pcitool_init;
266 	}
267 
268 	pcip->pci_fmcap = DDI_FM_ERRCB_CAPABLE |
269 	    DDI_FM_ACCCHK_CAPABLE | DDI_FM_DMACHK_CAPABLE;
270 	ddi_fm_init(devi, &pcip->pci_fmcap, &pcip->pci_fm_ibc);
271 	mutex_init(&pcip->pci_mutex, NULL, MUTEX_DRIVER, NULL);
272 	mutex_init(&pcip->pci_err_mutex, NULL, MUTEX_DRIVER,
273 	    (void *)pcip->pci_fm_ibc);
274 	mutex_init(&pcip->pci_peek_poke_mutex, NULL, MUTEX_DRIVER,
275 	    (void *)pcip->pci_fm_ibc);
276 	if (pcip->pci_fmcap & DDI_FM_ERRCB_CAPABLE) {
277 		pci_ereport_setup(devi);
278 		ddi_fm_handler_register(devi, pci_fm_callback, NULL);
279 	}
280 
281 	ddi_report_dev(devi);
282 
283 	return (DDI_SUCCESS);
284 
285 bad_pcitool_init:
286 	(void) pcihp_uninit(devi);
287 bad_pcihp_init:
288 	ddi_soft_state_free(pci_statep, instance);
289 bad_soft_state:
290 	return (DDI_FAILURE);
291 }
292 
293 /*ARGSUSED*/
294 static int
295 pci_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
296 {
297 	int instance = ddi_get_instance(devi);
298 	pci_state_t *pcip;
299 
300 	pcip = ddi_get_soft_state(pci_statep, ddi_get_instance(devi));
301 
302 
303 	switch (cmd) {
304 	case DDI_DETACH:
305 		if (pcip->pci_fmcap & DDI_FM_ERRCB_CAPABLE) {
306 			ddi_fm_handler_unregister(devi);
307 			pci_ereport_teardown(devi);
308 		}
309 		mutex_destroy(&pcip->pci_peek_poke_mutex);
310 		mutex_destroy(&pcip->pci_err_mutex);
311 		mutex_destroy(&pcip->pci_mutex);
312 		ddi_fm_fini(devi);	/* Uninitialize pcitool support. */
313 		pcitool_uninit(devi);
314 
315 		/* Uninitialize hotplug support on this bus. */
316 		(void) pcihp_uninit(devi);
317 
318 		ddi_soft_state_free(pci_statep, instance);
319 
320 		return (DDI_SUCCESS);
321 	case DDI_SUSPEND:
322 		return (DDI_SUCCESS);
323 	default:
324 		return (DDI_FAILURE);
325 	}
326 }
327 
328 static int
329 pci_bus_map(dev_info_t *dip, dev_info_t *rdip, ddi_map_req_t *mp,
330 	off_t offset, off_t len, caddr_t *vaddrp)
331 {
332 	struct regspec reg;
333 	ddi_map_req_t mr;
334 	ddi_acc_hdl_t *hp;
335 	pci_regspec_t pci_reg;
336 	pci_regspec_t *pci_rp;
337 	int 	rnumber;
338 	int	length;
339 	pci_acc_cfblk_t *cfp;
340 	int	space;
341 
342 
343 	mr = *mp; /* Get private copy of request */
344 	mp = &mr;
345 
346 	/*
347 	 * check for register number
348 	 */
349 	switch (mp->map_type) {
350 	case DDI_MT_REGSPEC:
351 		pci_reg = *(pci_regspec_t *)(mp->map_obj.rp);
352 		pci_rp = &pci_reg;
353 		if (pci_common_get_reg_prop(rdip, pci_rp) != DDI_SUCCESS)
354 			return (DDI_FAILURE);
355 		break;
356 	case DDI_MT_RNUMBER:
357 		rnumber = mp->map_obj.rnumber;
358 		/*
359 		 * get ALL "reg" properties for dip, select the one of
360 		 * of interest. In x86, "assigned-addresses" property
361 		 * is identical to the "reg" property, so there is no
362 		 * need to cross check the two to determine the physical
363 		 * address of the registers.
364 		 * This routine still performs some validity checks to
365 		 * make sure that everything is okay.
366 		 */
367 		if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, rdip,
368 		    DDI_PROP_DONTPASS, "reg", (int **)&pci_rp,
369 		    (uint_t *)&length) != DDI_PROP_SUCCESS)
370 			return (DDI_FAILURE);
371 
372 		/*
373 		 * validate the register number.
374 		 */
375 		length /= (sizeof (pci_regspec_t) / sizeof (int));
376 		if (rnumber >= length) {
377 			ddi_prop_free(pci_rp);
378 			return (DDI_FAILURE);
379 		}
380 
381 		/*
382 		 * copy the required entry.
383 		 */
384 		pci_reg = pci_rp[rnumber];
385 
386 		/*
387 		 * free the memory allocated by ddi_prop_lookup_int_array
388 		 */
389 		ddi_prop_free(pci_rp);
390 
391 		pci_rp = &pci_reg;
392 		if (pci_common_get_reg_prop(rdip, pci_rp) != DDI_SUCCESS)
393 			return (DDI_FAILURE);
394 		mp->map_type = DDI_MT_REGSPEC;
395 		break;
396 	default:
397 		return (DDI_ME_INVAL);
398 	}
399 
400 	space = pci_rp->pci_phys_hi & PCI_REG_ADDR_M;
401 
402 	/*
403 	 * check for unmap and unlock of address space
404 	 */
405 	if ((mp->map_op == DDI_MO_UNMAP) || (mp->map_op == DDI_MO_UNLOCK)) {
406 		/*
407 		 * Adjust offset and length
408 		 * A non-zero length means override the one in the regspec.
409 		 */
410 		pci_rp->pci_phys_low += (uint_t)offset;
411 		if (len != 0)
412 			pci_rp->pci_size_low = len;
413 
414 		switch (space) {
415 		case PCI_ADDR_CONFIG:
416 			/* No work required on unmap of Config space */
417 			return (DDI_SUCCESS);
418 
419 		case PCI_ADDR_IO:
420 			reg.regspec_bustype = 1;
421 			break;
422 
423 		case PCI_ADDR_MEM64:
424 			/*
425 			 * MEM64 requires special treatment on map, to check
426 			 * that the device is below 4G.  On unmap, however,
427 			 * we can assume that everything is OK... the map
428 			 * must have succeeded.
429 			 */
430 			/* FALLTHROUGH */
431 		case PCI_ADDR_MEM32:
432 			reg.regspec_bustype = 0;
433 			break;
434 
435 		default:
436 			return (DDI_FAILURE);
437 		}
438 		reg.regspec_addr = pci_rp->pci_phys_low;
439 		reg.regspec_size = pci_rp->pci_size_low;
440 
441 		mp->map_obj.rp = &reg;
442 		return (ddi_map(dip, mp, (off_t)0, (off_t)0, vaddrp));
443 
444 	}
445 
446 	/* check for user mapping request - not legal for Config */
447 	if (mp->map_op == DDI_MO_MAP_HANDLE && space == PCI_ADDR_CONFIG) {
448 		return (DDI_FAILURE);
449 	}
450 
451 	/*
452 	 * check for config space
453 	 * On x86, CONFIG is not mapped via MMU and there is
454 	 * no endian-ness issues. Set the attr field in the handle to
455 	 * indicate that the common routines to call the nexus driver.
456 	 */
457 	if (space == PCI_ADDR_CONFIG) {
458 		/* Can't map config space without a handle */
459 		hp = (ddi_acc_hdl_t *)mp->map_handlep;
460 		if (hp == NULL)
461 			return (DDI_FAILURE);
462 
463 		/* record the device address for future reference */
464 		cfp = (pci_acc_cfblk_t *)&hp->ah_bus_private;
465 		cfp->c_busnum = PCI_REG_BUS_G(pci_rp->pci_phys_hi);
466 		cfp->c_devnum = PCI_REG_DEV_G(pci_rp->pci_phys_hi);
467 		cfp->c_funcnum = PCI_REG_FUNC_G(pci_rp->pci_phys_hi);
468 
469 		*vaddrp = (caddr_t)offset;
470 		return (pci_fm_acc_setup(hp, offset, len));
471 	}
472 
473 	/*
474 	 * range check
475 	 */
476 	if ((offset >= pci_rp->pci_size_low) ||
477 	    (len > pci_rp->pci_size_low) ||
478 	    (offset + len > pci_rp->pci_size_low)) {
479 		return (DDI_FAILURE);
480 	}
481 
482 	/*
483 	 * Adjust offset and length
484 	 * A non-zero length means override the one in the regspec.
485 	 */
486 	pci_rp->pci_phys_low += (uint_t)offset;
487 	if (len != 0)
488 		pci_rp->pci_size_low = len;
489 
490 	/*
491 	 * convert the pci regsec into the generic regspec used by the
492 	 * parent root nexus driver.
493 	 */
494 	switch (space) {
495 	case PCI_ADDR_IO:
496 		reg.regspec_bustype = 1;
497 		break;
498 	case PCI_ADDR_MEM64:
499 		/*
500 		 * We can't handle 64-bit devices that are mapped above
501 		 * 4G or that are larger than 4G.
502 		 */
503 		if (pci_rp->pci_phys_mid != 0 ||
504 		    pci_rp->pci_size_hi != 0)
505 			return (DDI_FAILURE);
506 		/*
507 		 * Other than that, we can treat them as 32-bit mappings
508 		 */
509 		/* FALLTHROUGH */
510 	case PCI_ADDR_MEM32:
511 		reg.regspec_bustype = 0;
512 		break;
513 	default:
514 		return (DDI_FAILURE);
515 	}
516 	reg.regspec_addr = pci_rp->pci_phys_low;
517 	reg.regspec_size = pci_rp->pci_size_low;
518 
519 	mp->map_obj.rp = &reg;
520 	return (ddi_map(dip, mp, (off_t)0, (off_t)0, vaddrp));
521 }
522 
523 
524 /*ARGSUSED*/
525 static int
526 pci_ctlops(dev_info_t *dip, dev_info_t *rdip,
527 	ddi_ctl_enum_t ctlop, void *arg, void *result)
528 {
529 	pci_regspec_t *drv_regp;
530 	uint_t	reglen;
531 	int	rn;
532 	int	totreg;
533 	pci_state_t *pcip;
534 	struct  attachspec *asp;
535 
536 	switch (ctlop) {
537 	case DDI_CTLOPS_REPORTDEV:
538 		if (rdip == (dev_info_t *)0)
539 			return (DDI_FAILURE);
540 		cmn_err(CE_CONT, "?PCI-device: %s@%s, %s%d\n",
541 		    ddi_node_name(rdip), ddi_get_name_addr(rdip),
542 		    ddi_driver_name(rdip),
543 		    ddi_get_instance(rdip));
544 		return (DDI_SUCCESS);
545 
546 	case DDI_CTLOPS_INITCHILD:
547 		return (pci_initchild((dev_info_t *)arg));
548 
549 	case DDI_CTLOPS_UNINITCHILD:
550 		return (pci_removechild((dev_info_t *)arg));
551 
552 	case DDI_CTLOPS_SIDDEV:
553 		return (DDI_SUCCESS);
554 
555 	case DDI_CTLOPS_REGSIZE:
556 	case DDI_CTLOPS_NREGS:
557 		if (rdip == (dev_info_t *)0)
558 			return (DDI_FAILURE);
559 
560 		*(int *)result = 0;
561 		if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, rdip,
562 		    DDI_PROP_DONTPASS, "reg", (int **)&drv_regp,
563 		    &reglen) != DDI_PROP_SUCCESS) {
564 			return (DDI_FAILURE);
565 		}
566 
567 		totreg = (reglen * sizeof (int)) / sizeof (pci_regspec_t);
568 		if (ctlop == DDI_CTLOPS_NREGS)
569 			*(int *)result = totreg;
570 		else if (ctlop == DDI_CTLOPS_REGSIZE) {
571 			rn = *(int *)arg;
572 			if (rn >= totreg) {
573 				ddi_prop_free(drv_regp);
574 				return (DDI_FAILURE);
575 			}
576 			*(off_t *)result = drv_regp[rn].pci_size_low;
577 		}
578 		ddi_prop_free(drv_regp);
579 
580 		return (DDI_SUCCESS);
581 
582 	case DDI_CTLOPS_POWER: {
583 		power_req_t	*reqp = (power_req_t *)arg;
584 		/*
585 		 * We currently understand reporting of PCI_PM_IDLESPEED
586 		 * capability. Everything else is passed up.
587 		 */
588 		if ((reqp->request_type == PMR_REPORT_PMCAP) &&
589 		    (reqp->req.report_pmcap_req.cap ==  PCI_PM_IDLESPEED)) {
590 
591 			return (DDI_SUCCESS);
592 		}
593 		return (ddi_ctlops(dip, rdip, ctlop, arg, result));
594 	}
595 
596 	case DDI_CTLOPS_PEEK:
597 	case DDI_CTLOPS_POKE:
598 		pcip = ddi_get_soft_state(pci_statep, ddi_get_instance(dip));
599 		return (pci_peekpoke_check(dip, rdip, ctlop, arg, result,
600 		    pci_common_peekpoke, &pcip->pci_err_mutex,
601 		    &pcip->pci_peek_poke_mutex, pci_peekpoke_cb));
602 
603 	/* for now only X86 systems support PME wakeup from suspended state */
604 	case DDI_CTLOPS_ATTACH:
605 		asp = (struct attachspec *)arg;
606 		if (asp->cmd == DDI_RESUME && asp->when == DDI_PRE)
607 			if (pci_pre_resume(rdip) != DDI_SUCCESS)
608 				return (DDI_FAILURE);
609 		return (ddi_ctlops(dip, rdip, ctlop, arg, result));
610 
611 	case DDI_CTLOPS_DETACH:
612 		asp = (struct attachspec *)arg;
613 		if (asp->cmd == DDI_SUSPEND && asp->when == DDI_POST)
614 			if (pci_post_suspend(rdip) != DDI_SUCCESS)
615 				return (DDI_FAILURE);
616 		return (ddi_ctlops(dip, rdip, ctlop, arg, result));
617 
618 	default:
619 		return (ddi_ctlops(dip, rdip, ctlop, arg, result));
620 	}
621 
622 	/* NOTREACHED */
623 
624 }
625 
626 /*
627  * pci_intr_ops
628  */
629 static int
630 pci_intr_ops(dev_info_t *pdip, dev_info_t *rdip, ddi_intr_op_t intr_op,
631     ddi_intr_handle_impl_t *hdlp, void *result)
632 {
633 	return (pci_common_intr_ops(pdip, rdip, intr_op, hdlp, result));
634 }
635 
636 
637 static int
638 pci_initchild(dev_info_t *child)
639 {
640 	char name[80];
641 	ddi_acc_handle_t config_handle;
642 	ushort_t command_preserve, command;
643 
644 	if (pci_common_name_child(child, name, 80) != DDI_SUCCESS) {
645 		return (DDI_FAILURE);
646 	}
647 	ddi_set_name_addr(child, name);
648 
649 	/*
650 	 * Pseudo nodes indicate a prototype node with per-instance
651 	 * properties to be merged into the real h/w device node.
652 	 * The interpretation of the unit-address is DD[,F]
653 	 * where DD is the device id and F is the function.
654 	 */
655 	if (ndi_dev_is_persistent_node(child) == 0) {
656 		extern int pci_allow_pseudo_children;
657 
658 		ddi_set_parent_data(child, NULL);
659 
660 		/*
661 		 * Try to merge the properties from this prototype
662 		 * node into real h/w nodes.
663 		 */
664 		if (ndi_merge_node(child, pci_common_name_child) ==
665 		    DDI_SUCCESS) {
666 			/*
667 			 * Merged ok - return failure to remove the node.
668 			 */
669 			ddi_set_name_addr(child, NULL);
670 			return (DDI_FAILURE);
671 		}
672 
673 		/* workaround for ddivs to run under PCI */
674 		if (pci_allow_pseudo_children) {
675 			/*
676 			 * If the "interrupts" property doesn't exist,
677 			 * this must be the ddivs no-intr case, and it returns
678 			 * DDI_SUCCESS instead of DDI_FAILURE.
679 			 */
680 			if (ddi_prop_get_int(DDI_DEV_T_ANY, child,
681 			    DDI_PROP_DONTPASS, "interrupts", -1) == -1)
682 				return (DDI_SUCCESS);
683 			/*
684 			 * Create the ddi_parent_private_data for a pseudo
685 			 * child.
686 			 */
687 			pci_common_set_parent_private_data(child);
688 			return (DDI_SUCCESS);
689 		}
690 
691 		/*
692 		 * The child was not merged into a h/w node,
693 		 * but there's not much we can do with it other
694 		 * than return failure to cause the node to be removed.
695 		 */
696 		cmn_err(CE_WARN, "!%s@%s: %s.conf properties not merged",
697 		    ddi_get_name(child), ddi_get_name_addr(child),
698 		    ddi_get_name(child));
699 		ddi_set_name_addr(child, NULL);
700 		return (DDI_NOT_WELL_FORMED);
701 	}
702 
703 	if (ddi_prop_get_int(DDI_DEV_T_ANY, child, DDI_PROP_DONTPASS,
704 	    "interrupts", -1) != -1)
705 		pci_common_set_parent_private_data(child);
706 	else
707 		ddi_set_parent_data(child, NULL);
708 
709 	/*
710 	 * initialize command register
711 	 */
712 	if (pci_config_setup(child, &config_handle) != DDI_SUCCESS)
713 		return (DDI_FAILURE);
714 
715 	/*
716 	 * Support for the "command-preserve" property.
717 	 */
718 	command_preserve = ddi_prop_get_int(DDI_DEV_T_ANY, child,
719 	    DDI_PROP_DONTPASS, "command-preserve", 0);
720 	command = pci_config_get16(config_handle, PCI_CONF_COMM);
721 	command &= (command_preserve | PCI_COMM_BACK2BACK_ENAB);
722 	command |= (pci_command_default & ~command_preserve);
723 	pci_config_put16(config_handle, PCI_CONF_COMM, command);
724 
725 	pci_config_teardown(&config_handle);
726 	return (DDI_SUCCESS);
727 }
728 
729 static int
730 pci_removechild(dev_info_t *dip)
731 {
732 	struct ddi_parent_private_data *pdptr;
733 
734 	if ((pdptr = ddi_get_parent_data(dip)) != NULL) {
735 		kmem_free(pdptr, (sizeof (*pdptr) + sizeof (struct intrspec)));
736 		ddi_set_parent_data(dip, NULL);
737 	}
738 	ddi_set_name_addr(dip, NULL);
739 
740 	/*
741 	 * Strip the node to properly convert it back to prototype form
742 	 */
743 	ddi_remove_minor_node(dip, NULL);
744 
745 	impl_rem_dev_props(dip);
746 
747 	return (DDI_SUCCESS);
748 }
749 
750 
751 /*
752  * When retrofitting this module for pci_tools, functions such as open, close,
753  * and ioctl are now pulled into this module.  Before this, the functions in
754  * the pcihp module were referenced directly.  Now they are called or
755  * referenced through the pcihp cb_ops structure from functions in this module.
756  */
757 
758 static int
759 pci_open(dev_t *devp, int flags, int otyp, cred_t *credp)
760 {
761 	return ((pcihp_get_cb_ops())->cb_open(devp, flags, otyp, credp));
762 }
763 
764 static int
765 pci_close(dev_t dev, int flags, int otyp, cred_t *credp)
766 {
767 	return ((pcihp_get_cb_ops())->cb_close(dev, flags, otyp, credp));
768 }
769 
770 static int
771 pci_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp, int *rvalp)
772 {
773 	minor_t		minor = getminor(dev);
774 	int		instance = PCI_MINOR_NUM_TO_INSTANCE(minor);
775 	pci_state_t	*pci_p = ddi_get_soft_state(pci_statep, instance);
776 	int		ret = ENOTTY;
777 
778 	if (pci_p == NULL)
779 		return (ENXIO);
780 
781 	switch (PCI_MINOR_NUM_TO_PCI_DEVNUM(minor)) {
782 	case PCI_TOOL_REG_MINOR_NUM:
783 	case PCI_TOOL_INTR_MINOR_NUM:
784 		/* To handle pcitool related ioctls */
785 		ret =  pci_common_ioctl(pci_p->pci_dip, dev, cmd, arg, mode,
786 		    credp, rvalp);
787 		break;
788 	default:
789 		/* To handle devctl and hotplug related ioctls */
790 		ret = (pcihp_get_cb_ops())->cb_ioctl(dev, cmd, arg, mode,
791 		    credp, rvalp);
792 		break;
793 	}
794 
795 	return (ret);
796 }
797 
798 
799 static int
800 pci_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op,
801 	int flags, char *name, caddr_t valuep, int *lengthp)
802 {
803 	return ((pcihp_get_cb_ops())->cb_prop_op(dev, dip, prop_op, flags,
804 	    name, valuep, lengthp));
805 }
806 
807 static int
808 pci_info(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **result)
809 {
810 	return (pcihp_info(dip, cmd, arg, result));
811 }
812 
813 void pci_peekpoke_cb(dev_info_t *dip, ddi_fm_error_t *derr) {
814 	(void) pci_ereport_post(dip, derr, NULL);
815 }
816 
817 /*ARGSUSED*/
818 static int
819 pci_fm_init(dev_info_t *dip, dev_info_t *tdip, int cap,
820     ddi_iblock_cookie_t *ibc)
821 {
822 	pci_state_t  *pcip = ddi_get_soft_state(pci_statep,
823 	    ddi_get_instance(dip));
824 
825 	ASSERT(ibc != NULL);
826 	*ibc = pcip->pci_fm_ibc;
827 
828 	return (pcip->pci_fmcap);
829 }
830 
831 /*ARGSUSED*/
832 static int
833 pci_fm_callback(dev_info_t *dip, ddi_fm_error_t *derr, const void *no_used)
834 {
835 	pci_state_t  *pcip = ddi_get_soft_state(pci_statep,
836 	    ddi_get_instance(dip));
837 
838 	mutex_enter(&pcip->pci_err_mutex);
839 	pci_ereport_post(dip, derr, NULL);
840 	mutex_exit(&pcip->pci_err_mutex);
841 	return (derr->fme_status);
842 }
843