xref: /titanic_41/usr/src/uts/sun4/io/px/px.c (revision 692d834dec3070f47507bc0f44d3d92620b49cfa)
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 2010 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*
27  * SPARC Host to PCI Express nexus driver
28  */
29 
30 #include <sys/types.h>
31 #include <sys/conf.h>		/* nulldev */
32 #include <sys/stat.h>		/* devctl */
33 #include <sys/kmem.h>
34 #include <sys/sunddi.h>
35 #include <sys/sunndi.h>
36 #include <sys/ddi_subrdefs.h>
37 #include <sys/spl.h>
38 #include <sys/epm.h>
39 #include <sys/iommutsb.h>
40 #include "px_obj.h"
41 #include <sys/hotplug/pci/pcie_hp.h>
42 #include <sys/pci_tools.h>
43 #include "px_tools_ext.h"
44 #include <sys/pcie_pwr.h>
45 #include <sys/pci_cfgacc.h>
46 
47 /*LINTLIBRARY*/
48 
49 /*
50  * function prototypes for dev ops routines:
51  */
52 static int px_attach(dev_info_t *dip, ddi_attach_cmd_t cmd);
53 static int px_detach(dev_info_t *dip, ddi_detach_cmd_t cmd);
54 static int px_info(dev_info_t *dip, ddi_info_cmd_t infocmd,
55 	void *arg, void **result);
56 static int px_cb_attach(px_t *);
57 static void px_cb_detach(px_t *);
58 static int px_pwr_setup(dev_info_t *dip);
59 static void px_pwr_teardown(dev_info_t *dip);
60 static void px_set_mps(px_t *px_p);
61 
62 extern void pci_cfgacc_acc(pci_cfgacc_req_t *);
63 extern int pcie_max_mps;
64 extern void (*pci_cfgacc_acc_p)(pci_cfgacc_req_t *);
65 /*
66  * bus ops and dev ops structures:
67  */
68 static struct bus_ops px_bus_ops = {
69 	BUSO_REV,
70 	px_map,
71 	0,
72 	0,
73 	0,
74 	i_ddi_map_fault,
75 	px_dma_setup,
76 	px_dma_allochdl,
77 	px_dma_freehdl,
78 	px_dma_bindhdl,
79 	px_dma_unbindhdl,
80 	px_lib_dma_sync,
81 	px_dma_win,
82 	px_dma_ctlops,
83 	px_ctlops,
84 	ddi_bus_prop_op,
85 	ndi_busop_get_eventcookie,
86 	ndi_busop_add_eventcall,
87 	ndi_busop_remove_eventcall,
88 	ndi_post_event,
89 	NULL,
90 	NULL,			/* (*bus_config)(); */
91 	NULL,			/* (*bus_unconfig)(); */
92 	px_fm_init_child,	/* (*bus_fm_init)(); */
93 	NULL,			/* (*bus_fm_fini)(); */
94 	px_bus_enter,		/* (*bus_fm_access_enter)(); */
95 	px_bus_exit,		/* (*bus_fm_access_fini)(); */
96 	pcie_bus_power,		/* (*bus_power)(); */
97 	px_intr_ops,		/* (*bus_intr_op)(); */
98 	pcie_hp_common_ops	/* (*bus_hp_op)(); */
99 };
100 
101 extern struct cb_ops px_cb_ops;
102 
103 static struct dev_ops px_ops = {
104 	DEVO_REV,
105 	0,
106 	px_info,
107 	nulldev,
108 	0,
109 	px_attach,
110 	px_detach,
111 	nodev,
112 	&px_cb_ops,
113 	&px_bus_ops,
114 	nulldev,
115 	ddi_quiesce_not_needed,		/* quiesce */
116 };
117 
118 /*
119  * module definitions:
120  */
121 #include <sys/modctl.h>
122 extern struct mod_ops mod_driverops;
123 
124 static struct modldrv modldrv = {
125 	&mod_driverops, 			/* Type of module - driver */
126 #if defined(sun4u)
127 	"Sun4u Host to PCIe nexus driver",	/* Name of module. */
128 #elif defined(sun4v)
129 	"Sun4v Host to PCIe nexus driver",	/* Name of module. */
130 #endif
131 	&px_ops,				/* driver ops */
132 };
133 
134 static struct modlinkage modlinkage = {
135 	MODREV_1, (void *)&modldrv, NULL
136 };
137 
138 /* driver soft state */
139 void *px_state_p;
140 
141 int
142 _init(void)
143 {
144 	int e;
145 
146 	/*
147 	 * Initialize per-px bus soft state pointer.
148 	 */
149 	e = ddi_soft_state_init(&px_state_p, sizeof (px_t), 1);
150 	if (e != DDI_SUCCESS)
151 		return (e);
152 
153 	/*
154 	 * Install the module.
155 	 */
156 	e = mod_install(&modlinkage);
157 	if (e != DDI_SUCCESS)
158 		ddi_soft_state_fini(&px_state_p);
159 	return (e);
160 }
161 
162 int
163 _fini(void)
164 {
165 	int e;
166 
167 	/*
168 	 * Remove the module.
169 	 */
170 	e = mod_remove(&modlinkage);
171 	if (e != DDI_SUCCESS)
172 		return (e);
173 
174 	/* Free px soft state */
175 	ddi_soft_state_fini(&px_state_p);
176 
177 	return (e);
178 }
179 
180 int
181 _info(struct modinfo *modinfop)
182 {
183 	return (mod_info(&modlinkage, modinfop));
184 }
185 
186 /* ARGSUSED */
187 static int
188 px_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
189 {
190 	minor_t	minor = getminor((dev_t)arg);
191 	int	instance = PCI_MINOR_NUM_TO_INSTANCE(minor);
192 	px_t	*px_p = INST_TO_STATE(instance);
193 	int	ret = DDI_SUCCESS;
194 
195 	switch (infocmd) {
196 	case DDI_INFO_DEVT2INSTANCE:
197 		*result = (void *)(intptr_t)instance;
198 		break;
199 	case DDI_INFO_DEVT2DEVINFO:
200 		if (px_p == NULL) {
201 			ret = DDI_FAILURE;
202 			break;
203 		}
204 
205 		*result = (void *)px_p->px_dip;
206 		break;
207 	default:
208 		ret = DDI_FAILURE;
209 		break;
210 	}
211 
212 	return (ret);
213 }
214 
215 /* device driver entry points */
216 /*
217  * attach entry point:
218  */
219 /*ARGSUSED*/
220 static int
221 px_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
222 {
223 	px_t		*px_p;	/* per bus state pointer */
224 	int		instance = DIP_TO_INST(dip);
225 	int		ret = DDI_SUCCESS;
226 	devhandle_t	dev_hdl = NULL;
227 	pcie_hp_regops_t regops;
228 	pcie_bus_t	*bus_p;
229 
230 	switch (cmd) {
231 	case DDI_ATTACH:
232 		DBG(DBG_ATTACH, dip, "DDI_ATTACH\n");
233 
234 		/* See pci_cfgacc.c */
235 		pci_cfgacc_acc_p = pci_cfgacc_acc;
236 
237 		/*
238 		 * Allocate and get the per-px soft state structure.
239 		 */
240 		if (ddi_soft_state_zalloc(px_state_p, instance)
241 		    != DDI_SUCCESS) {
242 			cmn_err(CE_WARN, "%s%d: can't allocate px state",
243 			    ddi_driver_name(dip), instance);
244 			goto err_bad_px_softstate;
245 		}
246 		px_p = INST_TO_STATE(instance);
247 		px_p->px_dip = dip;
248 		mutex_init(&px_p->px_mutex, NULL, MUTEX_DRIVER, NULL);
249 		px_p->px_soft_state = PCI_SOFT_STATE_CLOSED;
250 
251 		(void) ddi_prop_update_string(DDI_DEV_T_NONE, dip,
252 		    "device_type", "pciex");
253 
254 		/* Initialize px_dbg for high pil printing */
255 		px_dbg_attach(dip, &px_p->px_dbg_hdl);
256 		pcie_rc_init_bus(dip);
257 
258 		/*
259 		 * Get key properties of the pci bridge node and
260 		 * determine it's type (psycho, schizo, etc ...).
261 		 */
262 		if (px_get_props(px_p, dip) == DDI_FAILURE)
263 			goto err_bad_px_prop;
264 
265 		if (px_lib_dev_init(dip, &dev_hdl) != DDI_SUCCESS)
266 			goto err_bad_dev_init;
267 
268 		/* Initialize device handle */
269 		px_p->px_dev_hdl = dev_hdl;
270 
271 		/* Cache the BDF of the root port nexus */
272 		px_p->px_bdf = px_lib_get_bdf(px_p);
273 
274 		/*
275 		 * Initialize interrupt block.  Note that this
276 		 * initialize error handling for the PEC as well.
277 		 */
278 		if ((ret = px_ib_attach(px_p)) != DDI_SUCCESS)
279 			goto err_bad_ib;
280 
281 		if (px_cb_attach(px_p) != DDI_SUCCESS)
282 			goto err_bad_cb;
283 
284 		/*
285 		 * Start creating the modules.
286 		 * Note that attach() routines should
287 		 * register and enable their own interrupts.
288 		 */
289 
290 		if ((px_mmu_attach(px_p)) != DDI_SUCCESS)
291 			goto err_bad_mmu;
292 
293 		if ((px_msiq_attach(px_p)) != DDI_SUCCESS)
294 			goto err_bad_msiq;
295 
296 		if ((px_msi_attach(px_p)) != DDI_SUCCESS)
297 			goto err_bad_msi;
298 
299 		if ((px_pec_attach(px_p)) != DDI_SUCCESS)
300 			goto err_bad_pec;
301 
302 		if ((px_dma_attach(px_p)) != DDI_SUCCESS)
303 			goto err_bad_dma; /* nothing to uninitialize on DMA */
304 
305 		if ((px_fm_attach(px_p)) != DDI_SUCCESS)
306 			goto err_bad_dma;
307 
308 		/*
309 		 * All of the error handlers have been registered
310 		 * by now so it's time to activate the interrupt.
311 		 */
312 		if ((ret = px_err_add_intr(&px_p->px_fault)) != DDI_SUCCESS)
313 			goto err_bad_intr;
314 
315 		if (px_lib_hotplug_init(dip, (void *)&regops) == DDI_SUCCESS) {
316 			pcie_bus_t	*bus_p = PCIE_DIP2BUS(dip);
317 
318 			bus_p->bus_hp_sup_modes |= PCIE_NATIVE_HP_MODE;
319 		}
320 
321 		(void) px_set_mps(px_p);
322 
323 		if (pcie_init(dip, (caddr_t)&regops) != DDI_SUCCESS)
324 			goto err_bad_hotplug;
325 
326 		(void) pcie_hpintr_enable(dip);
327 
328 		if (pxtool_init(dip) != DDI_SUCCESS)
329 			goto err_bad_pcitool_node;
330 
331 		/*
332 		 * power management setup. Even if it fails, attach will
333 		 * succeed as this is a optional feature. Since we are
334 		 * always at full power, this is not critical.
335 		 */
336 		if (pwr_common_setup(dip) != DDI_SUCCESS) {
337 			DBG(DBG_PWR, dip, "pwr_common_setup failed\n");
338 		} else if (px_pwr_setup(dip) != DDI_SUCCESS) {
339 			DBG(DBG_PWR, dip, "px_pwr_setup failed \n");
340 			pwr_common_teardown(dip);
341 		}
342 
343 		/*
344 		 * add cpr callback
345 		 */
346 		px_cpr_add_callb(px_p);
347 
348 		ddi_report_dev(dip);
349 
350 		px_p->px_state = PX_ATTACHED;
351 
352 		/*
353 		 * save base addr in bus_t for pci_cfgacc_xxx(), this
354 		 * depends of px structure being properly initialized.
355 		 */
356 		bus_p = PCIE_DIP2BUS(dip);
357 		bus_p->bus_cfgacc_base = px_lib_get_cfgacc_base(dip);
358 
359 		/*
360 		 * Partially populate bus_t for all devices in this fabric
361 		 * for device type macros to work.
362 		 */
363 		/*
364 		 * Populate bus_t for all devices in this fabric, after FMA
365 		 * is initializated, so that config access errors could
366 		 * trigger panic.
367 		 */
368 		pcie_fab_init_bus(dip, PCIE_BUS_ALL);
369 
370 		DBG(DBG_ATTACH, dip, "attach success\n");
371 		break;
372 
373 err_bad_pcitool_node:
374 		(void) pcie_hpintr_disable(dip);
375 		(void) pcie_uninit(dip);
376 err_bad_hotplug:
377 		(void) px_lib_hotplug_uninit(dip);
378 		px_err_rem_intr(&px_p->px_fault);
379 err_bad_intr:
380 		px_fm_detach(px_p);
381 err_bad_dma:
382 		px_pec_detach(px_p);
383 err_bad_pec:
384 		px_msi_detach(px_p);
385 err_bad_msi:
386 		px_msiq_detach(px_p);
387 err_bad_msiq:
388 		px_mmu_detach(px_p);
389 err_bad_mmu:
390 		px_cb_detach(px_p);
391 err_bad_cb:
392 		px_ib_detach(px_p);
393 err_bad_ib:
394 		if (px_lib_dev_fini(dip) != DDI_SUCCESS) {
395 			DBG(DBG_ATTACH, dip, "px_lib_dev_fini failed\n");
396 		}
397 err_bad_dev_init:
398 		px_free_props(px_p);
399 err_bad_px_prop:
400 		pcie_rc_fini_bus(dip);
401 		px_dbg_detach(dip, &px_p->px_dbg_hdl);
402 		mutex_destroy(&px_p->px_mutex);
403 		ddi_soft_state_free(px_state_p, instance);
404 err_bad_px_softstate:
405 		ret = DDI_FAILURE;
406 		break;
407 
408 	case DDI_RESUME:
409 		DBG(DBG_ATTACH, dip, "DDI_RESUME\n");
410 
411 		px_p = INST_TO_STATE(instance);
412 
413 		mutex_enter(&px_p->px_mutex);
414 
415 		/* suspend might have not succeeded */
416 		if (px_p->px_state != PX_SUSPENDED) {
417 			DBG(DBG_ATTACH, px_p->px_dip,
418 			    "instance NOT suspended\n");
419 			ret = DDI_FAILURE;
420 			break;
421 		}
422 
423 		px_msiq_resume(px_p);
424 		px_lib_resume(dip);
425 		(void) pcie_pwr_resume(dip);
426 		px_p->px_state = PX_ATTACHED;
427 
428 		mutex_exit(&px_p->px_mutex);
429 
430 		break;
431 	default:
432 		DBG(DBG_ATTACH, dip, "unsupported attach op\n");
433 		ret = DDI_FAILURE;
434 		break;
435 	}
436 
437 	return (ret);
438 }
439 
440 /*
441  * detach entry point:
442  */
443 /*ARGSUSED*/
444 static int
445 px_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
446 {
447 	int		instance = ddi_get_instance(dip);
448 	px_t		*px_p = INST_TO_STATE(instance);
449 	pcie_bus_t	*bus_p = PCIE_DIP2BUS(dip);
450 	int		ret;
451 
452 	/*
453 	 * Make sure we are currently attached
454 	 */
455 	if (px_p->px_state != PX_ATTACHED) {
456 		DBG(DBG_DETACH, dip, "Instance not attached\n");
457 		return (DDI_FAILURE);
458 	}
459 
460 	mutex_enter(&px_p->px_mutex);
461 
462 	switch (cmd) {
463 	case DDI_DETACH:
464 		DBG(DBG_DETACH, dip, "DDI_DETACH\n");
465 
466 		/*
467 		 * remove cpr callback
468 		 */
469 		px_cpr_rem_callb(px_p);
470 
471 		(void) pcie_hpintr_disable(dip);
472 
473 		if (PCIE_IS_PCIE_HOTPLUG_ENABLED(bus_p))
474 			(void) px_lib_hotplug_uninit(dip);
475 
476 		if (pcie_uninit(dip) != DDI_SUCCESS) {
477 			mutex_exit(&px_p->px_mutex);
478 			return (DDI_FAILURE);
479 		}
480 
481 		/* Destroy bus_t for the whole fabric */
482 		pcie_fab_fini_bus(dip, PCIE_BUS_ALL);
483 
484 		/*
485 		 * things which used to be done in obj_destroy
486 		 * are now in-lined here.
487 		 */
488 
489 		px_p->px_state = PX_DETACHED;
490 
491 		pxtool_uninit(dip);
492 
493 		px_err_rem_intr(&px_p->px_fault);
494 		px_fm_detach(px_p);
495 		px_pec_detach(px_p);
496 		px_pwr_teardown(dip);
497 		pwr_common_teardown(dip);
498 		px_msi_detach(px_p);
499 		px_msiq_detach(px_p);
500 		px_mmu_detach(px_p);
501 		px_cb_detach(px_p);
502 		px_ib_detach(px_p);
503 		if (px_lib_dev_fini(dip) != DDI_SUCCESS) {
504 			DBG(DBG_DETACH, dip, "px_lib_dev_fini failed\n");
505 		}
506 
507 		/*
508 		 * Free the px soft state structure and the rest of the
509 		 * resources it's using.
510 		 */
511 		px_free_props(px_p);
512 		pcie_rc_fini_bus(dip);
513 		px_dbg_detach(dip, &px_p->px_dbg_hdl);
514 		mutex_exit(&px_p->px_mutex);
515 		mutex_destroy(&px_p->px_mutex);
516 
517 		px_p->px_dev_hdl = NULL;
518 		ddi_soft_state_free(px_state_p, instance);
519 
520 		return (DDI_SUCCESS);
521 
522 	case DDI_SUSPEND:
523 		if (pcie_pwr_suspend(dip) != DDI_SUCCESS) {
524 			mutex_exit(&px_p->px_mutex);
525 			return (DDI_FAILURE);
526 		}
527 		if ((ret = px_lib_suspend(dip)) == DDI_SUCCESS)
528 			px_p->px_state = PX_SUSPENDED;
529 		mutex_exit(&px_p->px_mutex);
530 
531 		return (ret);
532 
533 	default:
534 		DBG(DBG_DETACH, dip, "unsupported detach op\n");
535 		mutex_exit(&px_p->px_mutex);
536 		return (DDI_FAILURE);
537 	}
538 }
539 
540 int
541 px_cb_attach(px_t *px_p)
542 {
543 	px_fault_t	*fault_p = &px_p->px_cb_fault;
544 	dev_info_t	*dip = px_p->px_dip;
545 	sysino_t	sysino;
546 
547 	if (px_lib_intr_devino_to_sysino(dip,
548 	    px_p->px_inos[PX_INTR_XBC], &sysino) != DDI_SUCCESS)
549 		return (DDI_FAILURE);
550 
551 	fault_p->px_fh_dip = dip;
552 	fault_p->px_fh_sysino = sysino;
553 	fault_p->px_err_func = px_err_cb_intr;
554 	fault_p->px_intr_ino = px_p->px_inos[PX_INTR_XBC];
555 
556 	return (px_cb_add_intr(fault_p));
557 }
558 
559 void
560 px_cb_detach(px_t *px_p)
561 {
562 	px_cb_rem_intr(&px_p->px_cb_fault);
563 }
564 
565 /*
566  * power management related initialization specific to px
567  * called by px_attach()
568  */
569 static int
570 px_pwr_setup(dev_info_t *dip)
571 {
572 	pcie_pwr_t *pwr_p;
573 	int instance = ddi_get_instance(dip);
574 	px_t *px_p = INST_TO_STATE(instance);
575 	ddi_intr_handle_impl_t hdl;
576 
577 	ASSERT(PCIE_PMINFO(dip));
578 	pwr_p = PCIE_NEXUS_PMINFO(dip);
579 	ASSERT(pwr_p);
580 
581 	/*
582 	 * indicate support LDI (Layered Driver Interface)
583 	 * Create the property, if it is not already there
584 	 */
585 	if (!ddi_prop_exists(DDI_DEV_T_NONE, dip, DDI_PROP_DONTPASS,
586 	    DDI_KERNEL_IOCTL)) {
587 		if (ddi_prop_create(DDI_DEV_T_NONE, dip, DDI_PROP_CANSLEEP,
588 		    DDI_KERNEL_IOCTL, NULL, 0) != DDI_PROP_SUCCESS) {
589 			DBG(DBG_PWR, dip, "can't create kernel ioctl prop\n");
590 			return (DDI_FAILURE);
591 		}
592 	}
593 	/* No support for device PM. We are always at full power */
594 	pwr_p->pwr_func_lvl = PM_LEVEL_D0;
595 
596 	mutex_init(&px_p->px_l23ready_lock, NULL, MUTEX_DRIVER,
597 	    DDI_INTR_PRI(px_pwr_pil));
598 	cv_init(&px_p->px_l23ready_cv, NULL, CV_DRIVER, NULL);
599 
600 	/* Initialize handle */
601 	bzero(&hdl, sizeof (ddi_intr_handle_impl_t));
602 	hdl.ih_cb_arg1 = px_p;
603 	hdl.ih_ver = DDI_INTR_VERSION;
604 	hdl.ih_state = DDI_IHDL_STATE_ALLOC;
605 	hdl.ih_dip = dip;
606 	hdl.ih_pri = px_pwr_pil;
607 
608 	/* Add PME_TO_ACK message handler */
609 	hdl.ih_cb_func = (ddi_intr_handler_t *)px_pmeq_intr;
610 	if (px_add_msiq_intr(dip, dip, &hdl, MSG_REC,
611 	    (msgcode_t)PCIE_PME_ACK_MSG, -1,
612 	    &px_p->px_pm_msiq_id) != DDI_SUCCESS) {
613 		DBG(DBG_PWR, dip, "px_pwr_setup: couldn't add "
614 		    " PME_TO_ACK intr\n");
615 		goto pwr_setup_err1;
616 	}
617 	px_lib_msg_setmsiq(dip, PCIE_PME_ACK_MSG, px_p->px_pm_msiq_id);
618 	px_lib_msg_setvalid(dip, PCIE_PME_ACK_MSG, PCIE_MSG_VALID);
619 
620 	if (px_ib_update_intr_state(px_p, px_p->px_dip, hdl.ih_inum,
621 	    px_msiqid_to_devino(px_p, px_p->px_pm_msiq_id), px_pwr_pil,
622 	    PX_INTR_STATE_ENABLE, MSG_REC, PCIE_PME_ACK_MSG) != DDI_SUCCESS) {
623 		DBG(DBG_PWR, dip, "px_pwr_setup: PME_TO_ACK update interrupt"
624 		    " state failed\n");
625 		goto px_pwrsetup_err_state;
626 	}
627 
628 	return (DDI_SUCCESS);
629 
630 px_pwrsetup_err_state:
631 	px_lib_msg_setvalid(dip, PCIE_PME_ACK_MSG, PCIE_MSG_INVALID);
632 	(void) px_rem_msiq_intr(dip, dip, &hdl, MSG_REC, PCIE_PME_ACK_MSG,
633 	    px_p->px_pm_msiq_id);
634 pwr_setup_err1:
635 	mutex_destroy(&px_p->px_l23ready_lock);
636 	cv_destroy(&px_p->px_l23ready_cv);
637 
638 	return (DDI_FAILURE);
639 }
640 
641 /*
642  * undo whatever is done in px_pwr_setup. called by px_detach()
643  */
644 static void
645 px_pwr_teardown(dev_info_t *dip)
646 {
647 	int instance = ddi_get_instance(dip);
648 	px_t *px_p = INST_TO_STATE(instance);
649 	ddi_intr_handle_impl_t	hdl;
650 
651 	if (!PCIE_PMINFO(dip) || !PCIE_NEXUS_PMINFO(dip))
652 		return;
653 
654 	/* Initialize handle */
655 	bzero(&hdl, sizeof (ddi_intr_handle_impl_t));
656 	hdl.ih_ver = DDI_INTR_VERSION;
657 	hdl.ih_state = DDI_IHDL_STATE_ALLOC;
658 	hdl.ih_dip = dip;
659 	hdl.ih_pri = px_pwr_pil;
660 
661 	px_lib_msg_setvalid(dip, PCIE_PME_ACK_MSG, PCIE_MSG_INVALID);
662 	(void) px_rem_msiq_intr(dip, dip, &hdl, MSG_REC, PCIE_PME_ACK_MSG,
663 	    px_p->px_pm_msiq_id);
664 
665 	(void) px_ib_update_intr_state(px_p, px_p->px_dip, hdl.ih_inum,
666 	    px_msiqid_to_devino(px_p, px_p->px_pm_msiq_id), px_pwr_pil,
667 	    PX_INTR_STATE_DISABLE, MSG_REC, PCIE_PME_ACK_MSG);
668 
669 	px_p->px_pm_msiq_id = (msiqid_t)-1;
670 
671 	cv_destroy(&px_p->px_l23ready_cv);
672 	mutex_destroy(&px_p->px_l23ready_lock);
673 }
674 
675 /* bus driver entry points */
676 
677 /*
678  * bus map entry point:
679  *
680  * 	if map request is for an rnumber
681  *		get the corresponding regspec from device node
682  * 	build a new regspec in our parent's format
683  *	build a new map_req with the new regspec
684  *	call up the tree to complete the mapping
685  */
686 int
687 px_map(dev_info_t *dip, dev_info_t *rdip, ddi_map_req_t *mp,
688 	off_t off, off_t len, caddr_t *addrp)
689 {
690 	px_t *px_p = DIP_TO_STATE(dip);
691 	struct regspec p_regspec;
692 	ddi_map_req_t p_mapreq;
693 	int reglen, rval, r_no;
694 	pci_regspec_t reloc_reg, *rp = &reloc_reg;
695 
696 	DBG(DBG_MAP, dip, "rdip=%s%d:",
697 	    ddi_driver_name(rdip), ddi_get_instance(rdip));
698 
699 	if (mp->map_flags & DDI_MF_USER_MAPPING)
700 		return (DDI_ME_UNIMPLEMENTED);
701 
702 	switch (mp->map_type) {
703 	case DDI_MT_REGSPEC:
704 		reloc_reg = *(pci_regspec_t *)mp->map_obj.rp;	/* dup whole */
705 		break;
706 
707 	case DDI_MT_RNUMBER:
708 		r_no = mp->map_obj.rnumber;
709 		DBG(DBG_MAP | DBG_CONT, dip, " r#=%x", r_no);
710 
711 		if (ddi_getlongprop(DDI_DEV_T_ANY, rdip, DDI_PROP_DONTPASS,
712 		    "reg", (caddr_t)&rp, &reglen) != DDI_SUCCESS)
713 			return (DDI_ME_RNUMBER_RANGE);
714 
715 		if (r_no < 0 || r_no >= reglen / sizeof (pci_regspec_t)) {
716 			kmem_free(rp, reglen);
717 			return (DDI_ME_RNUMBER_RANGE);
718 		}
719 		rp += r_no;
720 		break;
721 
722 	default:
723 		return (DDI_ME_INVAL);
724 	}
725 	DBG(DBG_MAP | DBG_CONT, dip, "\n");
726 
727 	if ((rp->pci_phys_hi & PCI_REG_ADDR_M) == PCI_ADDR_CONFIG) {
728 		/*
729 		 * There may be a need to differentiate between PCI
730 		 * and PCI-Ex devices so the following range check is
731 		 * done correctly, depending on the implementation of
732 		 * pcieb bridge nexus driver.
733 		 */
734 		if ((off >= PCIE_CONF_HDR_SIZE) ||
735 		    (len > PCIE_CONF_HDR_SIZE) ||
736 		    (off + len > PCIE_CONF_HDR_SIZE))
737 			return (DDI_ME_INVAL);
738 		/*
739 		 * the following function returning a DDI_FAILURE assumes
740 		 * that there are no virtual config space access services
741 		 * defined in this layer. Otherwise it is availed right
742 		 * here and we return.
743 		 */
744 		rval = px_lib_map_vconfig(dip, mp, off, rp, addrp);
745 		if (rval == DDI_SUCCESS)
746 			goto done;
747 	}
748 
749 	/*
750 	 * No virtual config space services or we are mapping
751 	 * a region of memory mapped config/IO/memory space, so proceed
752 	 * to the parent.
753 	 */
754 
755 	/* relocate within 64-bit pci space through "assigned-addresses" */
756 	if (rval = px_reloc_reg(dip, rdip, px_p, rp))
757 		goto done;
758 
759 	if (len)	/* adjust regspec according to mapping request */
760 		rp->pci_size_low = len;	/* MIN ? */
761 	rp->pci_phys_low += off;
762 
763 	/* translate relocated pci regspec into parent space through "ranges" */
764 	if (rval = px_xlate_reg(px_p, rp, &p_regspec))
765 		goto done;
766 
767 	p_mapreq = *mp;		/* dup the whole structure */
768 	p_mapreq.map_type = DDI_MT_REGSPEC;
769 	p_mapreq.map_obj.rp = &p_regspec;
770 	px_lib_map_attr_check(&p_mapreq);
771 	rval = ddi_map(dip, &p_mapreq, 0, 0, addrp);
772 
773 	if (rval == DDI_SUCCESS) {
774 		/*
775 		 * Set-up access functions for FM access error capable drivers.
776 		 */
777 		if (DDI_FM_ACC_ERR_CAP(ddi_fm_capable(rdip)))
778 			px_fm_acc_setup(mp, rdip, rp);
779 	}
780 
781 done:
782 	if (mp->map_type == DDI_MT_RNUMBER)
783 		kmem_free(rp - r_no, reglen);
784 
785 	return (rval);
786 }
787 
788 /*
789  * bus dma map entry point
790  * return value:
791  *	DDI_DMA_PARTIAL_MAP	 1
792  *	DDI_DMA_MAPOK		 0
793  *	DDI_DMA_MAPPED		 0
794  *	DDI_DMA_NORESOURCES	-1
795  *	DDI_DMA_NOMAPPING	-2
796  *	DDI_DMA_TOOBIG		-3
797  */
798 int
799 px_dma_setup(dev_info_t *dip, dev_info_t *rdip, ddi_dma_req_t *dmareq,
800 	ddi_dma_handle_t *handlep)
801 {
802 	px_t *px_p = DIP_TO_STATE(dip);
803 	px_mmu_t *mmu_p = px_p->px_mmu_p;
804 	ddi_dma_impl_t *mp;
805 	int ret;
806 
807 	DBG(DBG_DMA_MAP, dip, "mapping - rdip=%s%d type=%s\n",
808 	    ddi_driver_name(rdip), ddi_get_instance(rdip),
809 	    handlep ? "alloc" : "advisory");
810 
811 	if (!(mp = px_dma_lmts2hdl(dip, rdip, mmu_p, dmareq)))
812 		return (DDI_DMA_NORESOURCES);
813 	if (mp == (ddi_dma_impl_t *)DDI_DMA_NOMAPPING)
814 		return (DDI_DMA_NOMAPPING);
815 	if (ret = px_dma_type(px_p, dmareq, mp))
816 		goto freehandle;
817 	if (ret = px_dma_pfn(px_p, dmareq, mp))
818 		goto freehandle;
819 
820 	switch (PX_DMA_TYPE(mp)) {
821 	case PX_DMAI_FLAGS_DVMA:	/* LINTED E_EQUALITY_NOT_ASSIGNMENT */
822 		if ((ret = px_dvma_win(px_p, dmareq, mp)) || !handlep)
823 			goto freehandle;
824 		if (!PX_DMA_CANCACHE(mp)) {	/* try fast track */
825 			if (PX_DMA_CANFAST(mp)) {
826 				if (!px_dvma_map_fast(mmu_p, mp))
827 					break;
828 			/* LINTED E_NOP_ELSE_STMT */
829 			} else {
830 				PX_DVMA_FASTTRAK_PROF(mp);
831 			}
832 		}
833 		if (ret = px_dvma_map(mp, dmareq, mmu_p))
834 			goto freehandle;
835 		break;
836 	case PX_DMAI_FLAGS_PTP:	/* LINTED E_EQUALITY_NOT_ASSIGNMENT */
837 		if ((ret = px_dma_physwin(px_p, dmareq, mp)) || !handlep)
838 			goto freehandle;
839 		break;
840 	case PX_DMAI_FLAGS_BYPASS:
841 	default:
842 		cmn_err(CE_PANIC, "%s%d: px_dma_setup: bad dma type 0x%x",
843 		    ddi_driver_name(rdip), ddi_get_instance(rdip),
844 		    PX_DMA_TYPE(mp));
845 		/*NOTREACHED*/
846 	}
847 	*handlep = (ddi_dma_handle_t)mp;
848 	mp->dmai_flags |= PX_DMAI_FLAGS_INUSE;
849 	px_dump_dma_handle(DBG_DMA_MAP, dip, mp);
850 
851 	return ((mp->dmai_nwin == 1) ? DDI_DMA_MAPPED : DDI_DMA_PARTIAL_MAP);
852 freehandle:
853 	if (ret == DDI_DMA_NORESOURCES)
854 		px_dma_freemp(mp); /* don't run_callback() */
855 	else
856 		(void) px_dma_freehdl(dip, rdip, (ddi_dma_handle_t)mp);
857 	return (ret);
858 }
859 
860 
861 /*
862  * bus dma alloc handle entry point:
863  */
864 int
865 px_dma_allochdl(dev_info_t *dip, dev_info_t *rdip, ddi_dma_attr_t *attrp,
866 	int (*waitfp)(caddr_t), caddr_t arg, ddi_dma_handle_t *handlep)
867 {
868 	px_t *px_p = DIP_TO_STATE(dip);
869 	ddi_dma_impl_t *mp;
870 	int rval;
871 
872 	DBG(DBG_DMA_ALLOCH, dip, "rdip=%s%d\n",
873 	    ddi_driver_name(rdip), ddi_get_instance(rdip));
874 
875 	if (attrp->dma_attr_version != DMA_ATTR_V0)
876 		return (DDI_DMA_BADATTR);
877 
878 	if (!(mp = px_dma_allocmp(dip, rdip, waitfp, arg)))
879 		return (DDI_DMA_NORESOURCES);
880 
881 	/*
882 	 * Save requestor's information
883 	 */
884 	mp->dmai_attr	= *attrp; /* whole object - augmented later  */
885 	*PX_DEV_ATTR(mp)	= *attrp; /* whole object - device orig attr */
886 	DBG(DBG_DMA_ALLOCH, dip, "mp=%p\n", mp);
887 
888 	/* check and convert dma attributes to handle parameters */
889 	if (rval = px_dma_attr2hdl(px_p, mp)) {
890 		px_dma_freehdl(dip, rdip, (ddi_dma_handle_t)mp);
891 		*handlep = NULL;
892 		return (rval);
893 	}
894 	*handlep = (ddi_dma_handle_t)mp;
895 	return (DDI_SUCCESS);
896 }
897 
898 
899 /*
900  * bus dma free handle entry point:
901  */
902 /*ARGSUSED*/
903 int
904 px_dma_freehdl(dev_info_t *dip, dev_info_t *rdip, ddi_dma_handle_t handle)
905 {
906 	DBG(DBG_DMA_FREEH, dip, "rdip=%s%d mp=%p\n",
907 	    ddi_driver_name(rdip), ddi_get_instance(rdip), handle);
908 	px_dma_freemp((ddi_dma_impl_t *)handle);
909 
910 	if (px_kmem_clid) {
911 		DBG(DBG_DMA_FREEH, dip, "run handle callback\n");
912 		ddi_run_callback(&px_kmem_clid);
913 	}
914 	return (DDI_SUCCESS);
915 }
916 
917 
918 /*
919  * bus dma bind handle entry point:
920  */
921 int
922 px_dma_bindhdl(dev_info_t *dip, dev_info_t *rdip,
923 	ddi_dma_handle_t handle, ddi_dma_req_t *dmareq,
924 	ddi_dma_cookie_t *cookiep, uint_t *ccountp)
925 {
926 	px_t *px_p = DIP_TO_STATE(dip);
927 	px_mmu_t *mmu_p = px_p->px_mmu_p;
928 	ddi_dma_impl_t *mp = (ddi_dma_impl_t *)handle;
929 	int ret;
930 
931 	DBG(DBG_DMA_BINDH, dip, "rdip=%s%d mp=%p dmareq=%p\n",
932 	    ddi_driver_name(rdip), ddi_get_instance(rdip), mp, dmareq);
933 
934 	if (mp->dmai_flags & PX_DMAI_FLAGS_INUSE)
935 		return (DDI_DMA_INUSE);
936 
937 	ASSERT((mp->dmai_flags & ~PX_DMAI_FLAGS_PRESERVE) == 0);
938 	mp->dmai_flags |= PX_DMAI_FLAGS_INUSE;
939 
940 	if (ret = px_dma_type(px_p, dmareq, mp))
941 		goto err;
942 	if (ret = px_dma_pfn(px_p, dmareq, mp))
943 		goto err;
944 
945 	switch (PX_DMA_TYPE(mp)) {
946 	case PX_DMAI_FLAGS_DVMA:
947 		if (ret = px_dvma_win(px_p, dmareq, mp))
948 			goto map_err;
949 		if (!PX_DMA_CANCACHE(mp)) {	/* try fast track */
950 			if (PX_DMA_CANFAST(mp)) {
951 				if (!px_dvma_map_fast(mmu_p, mp))
952 					goto mapped; /*LINTED E_NOP_ELSE_STMT*/
953 			} else {
954 				PX_DVMA_FASTTRAK_PROF(mp);
955 			}
956 		}
957 		if (ret = px_dvma_map(mp, dmareq, mmu_p))
958 			goto map_err;
959 mapped:
960 		*ccountp = 1;
961 		MAKE_DMA_COOKIE(cookiep, mp->dmai_mapping, mp->dmai_size);
962 		break;
963 	case PX_DMAI_FLAGS_BYPASS:
964 	case PX_DMAI_FLAGS_PTP:
965 		if (ret = px_dma_physwin(px_p, dmareq, mp))
966 			goto map_err;
967 		*ccountp = PX_WINLST(mp)->win_ncookies;
968 		*cookiep =
969 		    *(ddi_dma_cookie_t *)(PX_WINLST(mp) + 1); /* wholeobj */
970 		break;
971 	default:
972 		cmn_err(CE_PANIC, "%s%d: px_dma_bindhdl(%p): bad dma type",
973 		    ddi_driver_name(rdip), ddi_get_instance(rdip), mp);
974 		/*NOTREACHED*/
975 	}
976 	DBG(DBG_DMA_BINDH, dip, "cookie %" PRIx64 "+%x\n",
977 	    cookiep->dmac_address, cookiep->dmac_size);
978 	px_dump_dma_handle(DBG_DMA_MAP, dip, mp);
979 
980 	/* insert dma handle into FMA cache */
981 	if (mp->dmai_attr.dma_attr_flags & DDI_DMA_FLAGERR) {
982 		(void) ndi_fmc_insert(rdip, DMA_HANDLE, mp, NULL);
983 		mp->dmai_error.err_cf = px_err_dma_hdl_check;
984 	}
985 
986 	return (mp->dmai_nwin == 1 ? DDI_DMA_MAPPED : DDI_DMA_PARTIAL_MAP);
987 map_err:
988 	px_dma_freepfn(mp);
989 err:
990 	mp->dmai_flags &= PX_DMAI_FLAGS_PRESERVE;
991 	return (ret);
992 }
993 
994 
995 /*
996  * bus dma unbind handle entry point:
997  */
998 /*ARGSUSED*/
999 int
1000 px_dma_unbindhdl(dev_info_t *dip, dev_info_t *rdip, ddi_dma_handle_t handle)
1001 {
1002 	ddi_dma_impl_t *mp = (ddi_dma_impl_t *)handle;
1003 	px_t *px_p = DIP_TO_STATE(dip);
1004 	px_mmu_t *mmu_p = px_p->px_mmu_p;
1005 
1006 	DBG(DBG_DMA_UNBINDH, dip, "rdip=%s%d, mp=%p\n",
1007 	    ddi_driver_name(rdip), ddi_get_instance(rdip), handle);
1008 	if ((mp->dmai_flags & PX_DMAI_FLAGS_INUSE) == 0) {
1009 		DBG(DBG_DMA_UNBINDH, dip, "handle not inuse\n");
1010 		return (DDI_FAILURE);
1011 	}
1012 
1013 	/* remove dma handle from FMA cache */
1014 	if (mp->dmai_attr.dma_attr_flags & DDI_DMA_FLAGERR) {
1015 		if (DEVI(rdip)->devi_fmhdl != NULL &&
1016 		    DDI_FM_DMA_ERR_CAP(DEVI(rdip)->devi_fmhdl->fh_cap)) {
1017 			(void) ndi_fmc_remove(rdip, DMA_HANDLE, mp);
1018 		}
1019 	}
1020 
1021 	/*
1022 	 * Here if the handle is using the iommu.  Unload all the iommu
1023 	 * translations.
1024 	 */
1025 	switch (PX_DMA_TYPE(mp)) {
1026 	case PX_DMAI_FLAGS_DVMA:
1027 		px_mmu_unmap_window(mmu_p, mp);
1028 		px_dvma_unmap(mmu_p, mp);
1029 		px_dma_freepfn(mp);
1030 		break;
1031 	case PX_DMAI_FLAGS_BYPASS:
1032 	case PX_DMAI_FLAGS_PTP:
1033 		px_dma_freewin(mp);
1034 		break;
1035 	default:
1036 		cmn_err(CE_PANIC, "%s%d: px_dma_unbindhdl:bad dma type %p",
1037 		    ddi_driver_name(rdip), ddi_get_instance(rdip), mp);
1038 		/*NOTREACHED*/
1039 	}
1040 	if (mmu_p->mmu_dvma_clid != 0) {
1041 		DBG(DBG_DMA_UNBINDH, dip, "run dvma callback\n");
1042 		ddi_run_callback(&mmu_p->mmu_dvma_clid);
1043 	}
1044 	if (px_kmem_clid) {
1045 		DBG(DBG_DMA_UNBINDH, dip, "run handle callback\n");
1046 		ddi_run_callback(&px_kmem_clid);
1047 	}
1048 	mp->dmai_flags &= PX_DMAI_FLAGS_PRESERVE;
1049 
1050 	return (DDI_SUCCESS);
1051 }
1052 
1053 /*
1054  * bus dma win entry point:
1055  */
1056 int
1057 px_dma_win(dev_info_t *dip, dev_info_t *rdip,
1058 	ddi_dma_handle_t handle, uint_t win, off_t *offp,
1059 	size_t *lenp, ddi_dma_cookie_t *cookiep, uint_t *ccountp)
1060 {
1061 	ddi_dma_impl_t	*mp = (ddi_dma_impl_t *)handle;
1062 	int		ret;
1063 
1064 	DBG(DBG_DMA_WIN, dip, "rdip=%s%d\n",
1065 	    ddi_driver_name(rdip), ddi_get_instance(rdip));
1066 
1067 	px_dump_dma_handle(DBG_DMA_WIN, dip, mp);
1068 	if (win >= mp->dmai_nwin) {
1069 		DBG(DBG_DMA_WIN, dip, "%x out of range\n", win);
1070 		return (DDI_FAILURE);
1071 	}
1072 
1073 	switch (PX_DMA_TYPE(mp)) {
1074 	case PX_DMAI_FLAGS_DVMA:
1075 		if (win != PX_DMA_CURWIN(mp)) {
1076 			px_t *px_p = DIP_TO_STATE(dip);
1077 			px_mmu_t *mmu_p = px_p->px_mmu_p;
1078 			px_mmu_unmap_window(mmu_p, mp);
1079 
1080 			/* map_window sets dmai_mapping/size/offset */
1081 			px_mmu_map_window(mmu_p, mp, win);
1082 			if ((ret = px_mmu_map_window(mmu_p,
1083 			    mp, win)) != DDI_SUCCESS)
1084 				return (ret);
1085 		}
1086 		if (cookiep)
1087 			MAKE_DMA_COOKIE(cookiep, mp->dmai_mapping,
1088 			    mp->dmai_size);
1089 		if (ccountp)
1090 			*ccountp = 1;
1091 		break;
1092 	case PX_DMAI_FLAGS_PTP:
1093 	case PX_DMAI_FLAGS_BYPASS: {
1094 		int i;
1095 		ddi_dma_cookie_t *ck_p;
1096 		px_dma_win_t *win_p = mp->dmai_winlst;
1097 
1098 		for (i = 0; i < win; win_p = win_p->win_next, i++) {};
1099 		ck_p = (ddi_dma_cookie_t *)(win_p + 1);
1100 		*cookiep = *ck_p;
1101 		mp->dmai_offset = win_p->win_offset;
1102 		mp->dmai_size   = win_p->win_size;
1103 		mp->dmai_mapping = ck_p->dmac_laddress;
1104 		mp->dmai_cookie = ck_p + 1;
1105 		win_p->win_curseg = 0;
1106 		if (ccountp)
1107 			*ccountp = win_p->win_ncookies;
1108 		}
1109 		break;
1110 	default:
1111 		cmn_err(CE_WARN, "%s%d: px_dma_win:bad dma type 0x%x",
1112 		    ddi_driver_name(rdip), ddi_get_instance(rdip),
1113 		    PX_DMA_TYPE(mp));
1114 		return (DDI_FAILURE);
1115 	}
1116 	if (cookiep)
1117 		DBG(DBG_DMA_WIN, dip,
1118 		    "cookie - dmac_address=%x dmac_size=%x\n",
1119 		    cookiep->dmac_address, cookiep->dmac_size);
1120 	if (offp)
1121 		*offp = (off_t)mp->dmai_offset;
1122 	if (lenp)
1123 		*lenp = mp->dmai_size;
1124 	return (DDI_SUCCESS);
1125 }
1126 
1127 #ifdef	DEBUG
1128 static char *px_dmactl_str[] = {
1129 	"DDI_DMA_FREE",
1130 	"DDI_DMA_SYNC",
1131 	"DDI_DMA_HTOC",
1132 	"DDI_DMA_KVADDR",
1133 	"DDI_DMA_MOVWIN",
1134 	"DDI_DMA_REPWIN",
1135 	"DDI_DMA_GETERR",
1136 	"DDI_DMA_COFF",
1137 	"DDI_DMA_NEXTWIN",
1138 	"DDI_DMA_NEXTSEG",
1139 	"DDI_DMA_SEGTOC",
1140 	"DDI_DMA_RESERVE",
1141 	"DDI_DMA_RELEASE",
1142 	"DDI_DMA_RESETH",
1143 	"DDI_DMA_CKSYNC",
1144 	"DDI_DMA_IOPB_ALLOC",
1145 	"DDI_DMA_IOPB_FREE",
1146 	"DDI_DMA_SMEM_ALLOC",
1147 	"DDI_DMA_SMEM_FREE",
1148 	"DDI_DMA_SET_SBUS64"
1149 };
1150 #endif	/* DEBUG */
1151 
1152 /*
1153  * bus dma control entry point:
1154  */
1155 /*ARGSUSED*/
1156 int
1157 px_dma_ctlops(dev_info_t *dip, dev_info_t *rdip, ddi_dma_handle_t handle,
1158 	enum ddi_dma_ctlops cmd, off_t *offp, size_t *lenp, caddr_t *objp,
1159 	uint_t cache_flags)
1160 {
1161 	ddi_dma_impl_t *mp = (ddi_dma_impl_t *)handle;
1162 
1163 #ifdef	DEBUG
1164 	DBG(DBG_DMA_CTL, dip, "%s: rdip=%s%d\n", px_dmactl_str[cmd],
1165 	    ddi_driver_name(rdip), ddi_get_instance(rdip));
1166 #endif	/* DEBUG */
1167 
1168 	switch (cmd) {
1169 	case DDI_DMA_FREE:
1170 		(void) px_dma_unbindhdl(dip, rdip, handle);
1171 		(void) px_dma_freehdl(dip, rdip, handle);
1172 		return (DDI_SUCCESS);
1173 	case DDI_DMA_RESERVE: {
1174 		px_t *px_p = DIP_TO_STATE(dip);
1175 		return (px_fdvma_reserve(dip, rdip, px_p,
1176 		    (ddi_dma_req_t *)offp, (ddi_dma_handle_t *)objp));
1177 		}
1178 	case DDI_DMA_RELEASE: {
1179 		px_t *px_p = DIP_TO_STATE(dip);
1180 		return (px_fdvma_release(dip, px_p, mp));
1181 		}
1182 	default:
1183 		break;
1184 	}
1185 
1186 	switch (PX_DMA_TYPE(mp)) {
1187 	case PX_DMAI_FLAGS_DVMA:
1188 		return (px_dvma_ctl(dip, rdip, mp, cmd, offp, lenp, objp,
1189 		    cache_flags));
1190 	case PX_DMAI_FLAGS_PTP:
1191 	case PX_DMAI_FLAGS_BYPASS:
1192 		return (px_dma_ctl(dip, rdip, mp, cmd, offp, lenp, objp,
1193 		    cache_flags));
1194 	default:
1195 		cmn_err(CE_PANIC, "%s%d: px_dma_ctlops(%x):bad dma type %x",
1196 		    ddi_driver_name(rdip), ddi_get_instance(rdip), cmd,
1197 		    mp->dmai_flags);
1198 		/*NOTREACHED*/
1199 	}
1200 	return (0);
1201 }
1202 
1203 /*
1204  * control ops entry point:
1205  *
1206  * Requests handled completely:
1207  *	DDI_CTLOPS_INITCHILD	see init_child() for details
1208  *	DDI_CTLOPS_UNINITCHILD
1209  *	DDI_CTLOPS_REPORTDEV	see report_dev() for details
1210  *	DDI_CTLOPS_IOMIN	cache line size if streaming otherwise 1
1211  *	DDI_CTLOPS_REGSIZE
1212  *	DDI_CTLOPS_NREGS
1213  *	DDI_CTLOPS_DVMAPAGESIZE
1214  *	DDI_CTLOPS_POKE
1215  *	DDI_CTLOPS_PEEK
1216  *
1217  * All others passed to parent.
1218  */
1219 int
1220 px_ctlops(dev_info_t *dip, dev_info_t *rdip,
1221 	ddi_ctl_enum_t op, void *arg, void *result)
1222 {
1223 	px_t *px_p = DIP_TO_STATE(dip);
1224 	struct detachspec *ds;
1225 	struct attachspec *as;
1226 
1227 	switch (op) {
1228 	case DDI_CTLOPS_INITCHILD:
1229 		return (px_init_child(px_p, (dev_info_t *)arg));
1230 
1231 	case DDI_CTLOPS_UNINITCHILD:
1232 		return (px_uninit_child(px_p, (dev_info_t *)arg));
1233 
1234 	case DDI_CTLOPS_ATTACH:
1235 		if (!pcie_is_child(dip, rdip))
1236 			return (DDI_SUCCESS);
1237 
1238 		as = (struct attachspec *)arg;
1239 		switch (as->when) {
1240 		case DDI_PRE:
1241 			if (as->cmd == DDI_ATTACH) {
1242 				DBG(DBG_PWR, dip, "PRE_ATTACH for %s@%d\n",
1243 				    ddi_driver_name(rdip),
1244 				    ddi_get_instance(rdip));
1245 				return (pcie_pm_hold(dip));
1246 			}
1247 			if (as->cmd == DDI_RESUME) {
1248 				DBG(DBG_PWR, dip, "PRE_RESUME for %s@%d\n",
1249 				    ddi_driver_name(rdip),
1250 				    ddi_get_instance(rdip));
1251 
1252 				pcie_clear_errors(rdip);
1253 			}
1254 			return (DDI_SUCCESS);
1255 
1256 		case DDI_POST:
1257 			DBG(DBG_PWR, dip, "POST_ATTACH for %s@%d\n",
1258 			    ddi_driver_name(rdip), ddi_get_instance(rdip));
1259 			if (as->cmd == DDI_ATTACH &&
1260 			    as->result != DDI_SUCCESS) {
1261 				/*
1262 				 * Attach failed for the child device. The child
1263 				 * driver may have made PM calls before the
1264 				 * attach failed. pcie_pm_remove_child() should
1265 				 * cleanup PM state and holds (if any)
1266 				 * associated with the child device.
1267 				 */
1268 				return (pcie_pm_remove_child(dip, rdip));
1269 			}
1270 
1271 			if (as->result == DDI_SUCCESS)
1272 				pf_init(rdip, (void *)px_p->px_fm_ibc, as->cmd);
1273 
1274 			(void) pcie_postattach_child(rdip);
1275 
1276 			return (DDI_SUCCESS);
1277 		default:
1278 			break;
1279 		}
1280 		break;
1281 
1282 	case DDI_CTLOPS_DETACH:
1283 		if (!pcie_is_child(dip, rdip))
1284 			return (DDI_SUCCESS);
1285 
1286 		ds = (struct detachspec *)arg;
1287 		switch (ds->when) {
1288 		case DDI_POST:
1289 			if (ds->cmd == DDI_DETACH &&
1290 			    ds->result == DDI_SUCCESS) {
1291 				DBG(DBG_PWR, dip, "POST_DETACH for %s@%d\n",
1292 				    ddi_driver_name(rdip),
1293 				    ddi_get_instance(rdip));
1294 				return (pcie_pm_remove_child(dip, rdip));
1295 			}
1296 			return (DDI_SUCCESS);
1297 		case DDI_PRE:
1298 			pf_fini(rdip, ds->cmd);
1299 			return (DDI_SUCCESS);
1300 		default:
1301 			break;
1302 		}
1303 		break;
1304 
1305 	case DDI_CTLOPS_REPORTDEV:
1306 		return (px_report_dev(rdip));
1307 
1308 	case DDI_CTLOPS_IOMIN:
1309 		return (DDI_SUCCESS);
1310 
1311 	case DDI_CTLOPS_REGSIZE:
1312 		*((off_t *)result) = px_get_reg_set_size(rdip, *((int *)arg));
1313 		return (*((off_t *)result) == 0 ? DDI_FAILURE : DDI_SUCCESS);
1314 
1315 	case DDI_CTLOPS_NREGS:
1316 		*((uint_t *)result) = px_get_nreg_set(rdip);
1317 		return (DDI_SUCCESS);
1318 
1319 	case DDI_CTLOPS_DVMAPAGESIZE:
1320 		*((ulong_t *)result) = MMU_PAGE_SIZE;
1321 		return (DDI_SUCCESS);
1322 
1323 	case DDI_CTLOPS_POKE:	/* platform dependent implementation. */
1324 		return (px_lib_ctlops_poke(dip, rdip,
1325 		    (peekpoke_ctlops_t *)arg));
1326 
1327 	case DDI_CTLOPS_PEEK:	/* platform dependent implementation. */
1328 		return (px_lib_ctlops_peek(dip, rdip,
1329 		    (peekpoke_ctlops_t *)arg, result));
1330 
1331 	case DDI_CTLOPS_POWER:
1332 	default:
1333 		break;
1334 	}
1335 
1336 	/*
1337 	 * Now pass the request up to our parent.
1338 	 */
1339 	DBG(DBG_CTLOPS, dip, "passing request to parent: rdip=%s%d\n",
1340 	    ddi_driver_name(rdip), ddi_get_instance(rdip));
1341 	return (ddi_ctlops(dip, rdip, op, arg, result));
1342 }
1343 
1344 /* ARGSUSED */
1345 int
1346 px_intr_ops(dev_info_t *dip, dev_info_t *rdip, ddi_intr_op_t intr_op,
1347     ddi_intr_handle_impl_t *hdlp, void *result)
1348 {
1349 	int	intr_types, ret = DDI_SUCCESS;
1350 
1351 	DBG(DBG_INTROPS, dip, "px_intr_ops: rdip=%s%d\n",
1352 	    ddi_driver_name(rdip), ddi_get_instance(rdip));
1353 
1354 	/* Process DDI_INTROP_SUPPORTED_TYPES request here */
1355 	if (intr_op == DDI_INTROP_SUPPORTED_TYPES) {
1356 		*(int *)result = i_ddi_get_intx_nintrs(rdip) ?
1357 		    DDI_INTR_TYPE_FIXED : 0;
1358 
1359 		if ((pci_msi_get_supported_type(rdip,
1360 		    &intr_types)) == DDI_SUCCESS) {
1361 			/*
1362 			 * Double check supported interrupt types vs.
1363 			 * what the host bridge supports.
1364 			 */
1365 			*(int *)result |= intr_types;
1366 		}
1367 
1368 		return (ret);
1369 	}
1370 
1371 	/*
1372 	 * PCI-E nexus driver supports fixed, MSI and MSI-X interrupts.
1373 	 * Return failure if interrupt type is not supported.
1374 	 */
1375 	switch (hdlp->ih_type) {
1376 	case DDI_INTR_TYPE_FIXED:
1377 		ret = px_intx_ops(dip, rdip, intr_op, hdlp, result);
1378 		break;
1379 	case DDI_INTR_TYPE_MSI:
1380 	case DDI_INTR_TYPE_MSIX:
1381 		ret = px_msix_ops(dip, rdip, intr_op, hdlp, result);
1382 		break;
1383 	default:
1384 		ret = DDI_ENOTSUP;
1385 		break;
1386 	}
1387 
1388 	return (ret);
1389 }
1390 
1391 static void
1392 px_set_mps(px_t *px_p)
1393 {
1394 	dev_info_t	*dip;
1395 	pcie_bus_t	*bus_p;
1396 	int		max_supported;
1397 
1398 	dip = px_p->px_dip;
1399 	bus_p = PCIE_DIP2BUS(dip);
1400 
1401 	bus_p->bus_mps = -1;
1402 
1403 	if (pcie_root_port(dip) == DDI_FAILURE) {
1404 		if (px_lib_get_root_complex_mps(px_p, dip,
1405 		    &max_supported) < 0) {
1406 
1407 			DBG(DBG_MPS, dip, "MPS:  Can not get RC MPS\n");
1408 			return;
1409 		}
1410 
1411 		DBG(DBG_MPS, dip, "MPS: Root Complex MPS Cap of = %x\n",
1412 		    max_supported);
1413 
1414 		if (pcie_max_mps < max_supported)
1415 			max_supported = pcie_max_mps;
1416 
1417 		(void) pcie_get_fabric_mps(dip, ddi_get_child(dip),
1418 		    &max_supported);
1419 
1420 		bus_p->bus_mps = max_supported;
1421 
1422 		(void) px_lib_set_root_complex_mps(px_p, dip, bus_p->bus_mps);
1423 
1424 		DBG(DBG_MPS, dip, "MPS: Root Complex MPS Set to = %x\n",
1425 		    bus_p->bus_mps);
1426 	}
1427 }
1428