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